Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * watchdog_dev.c |
| 3 | * |
| 4 | * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>. |
| 8 | * |
| 9 | * |
| 10 | * This source code is part of the generic code that can be used |
| 11 | * by all the watchdog timer drivers. |
| 12 | * |
| 13 | * This part of the generic code takes care of the following |
| 14 | * misc device: /dev/watchdog. |
| 15 | * |
| 16 | * Based on source code of the following authors: |
| 17 | * Matt Domsch <Matt_Domsch@dell.com>, |
| 18 | * Rob Radez <rob@osinvestor.com>, |
| 19 | * Rusty Lynch <rusty@linux.co.intel.com> |
| 20 | * Satyam Sharma <satyam@infradead.org> |
| 21 | * Randy Dunlap <randy.dunlap@oracle.com> |
| 22 | * |
| 23 | * This program is free software; you can redistribute it and/or |
| 24 | * modify it under the terms of the GNU General Public License |
| 25 | * as published by the Free Software Foundation; either version |
| 26 | * 2 of the License, or (at your option) any later version. |
| 27 | * |
| 28 | * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw. |
| 29 | * admit liability nor provide warranty for any of this software. |
| 30 | * This material is provided "AS-IS" and at no charge. |
| 31 | */ |
| 32 | |
| 33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 34 | |
| 35 | #include <linux/module.h> /* For module stuff/... */ |
| 36 | #include <linux/types.h> /* For standard types (like size_t) */ |
| 37 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
| 38 | #include <linux/kernel.h> /* For printk/panic/... */ |
| 39 | #include <linux/fs.h> /* For file operations */ |
| 40 | #include <linux/watchdog.h> /* For watchdog specific items */ |
| 41 | #include <linux/miscdevice.h> /* For handling misc devices */ |
| 42 | #include <linux/init.h> /* For __init/__exit/... */ |
| 43 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ |
| 44 | |
Wim Van Sebroeck | 6cfb5aa | 2012-05-21 15:31:06 +0200 | [diff] [blame] | 45 | #include "watchdog_core.h" |
H Hartley Sweeten | 09a46e7 | 2012-04-20 13:28:24 -0700 | [diff] [blame] | 46 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 47 | /* the dev_t structure to store the dynamically allocated watchdog devices */ |
| 48 | static dev_t watchdog_devt; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 49 | /* the watchdog device behind /dev/watchdog */ |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 50 | static struct watchdog_device *old_wdd; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * watchdog_ping: ping the watchdog. |
| 54 | * @wddev: the watchdog device to ping |
| 55 | * |
| 56 | * If the watchdog has no own ping operation then it needs to be |
| 57 | * restarted via the start operation. This wrapper function does |
| 58 | * exactly that. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 59 | * We only ping when the watchdog device is running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static int watchdog_ping(struct watchdog_device *wddev) |
| 63 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 64 | int err = 0; |
| 65 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 66 | mutex_lock(&wddev->lock); |
| 67 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 68 | if (!watchdog_active(wddev)) |
| 69 | goto out_ping; |
| 70 | |
| 71 | if (wddev->ops->ping) |
| 72 | err = wddev->ops->ping(wddev); /* ping the watchdog */ |
| 73 | else |
| 74 | err = wddev->ops->start(wddev); /* restart watchdog */ |
| 75 | |
| 76 | out_ping: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 77 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 78 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * watchdog_start: wrapper to start the watchdog. |
| 83 | * @wddev: the watchdog device to start |
| 84 | * |
| 85 | * Start the watchdog if it is not active and mark it active. |
| 86 | * This function returns zero on success or a negative errno code for |
| 87 | * failure. |
| 88 | */ |
| 89 | |
| 90 | static int watchdog_start(struct watchdog_device *wddev) |
| 91 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 92 | int err = 0; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 93 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 94 | mutex_lock(&wddev->lock); |
| 95 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 96 | if (watchdog_active(wddev)) |
| 97 | goto out_start; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 98 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 99 | err = wddev->ops->start(wddev); |
| 100 | if (err == 0) |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 101 | set_bit(WDOG_ACTIVE, &wddev->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 102 | |
| 103 | out_start: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 104 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 105 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * watchdog_stop: wrapper to stop the watchdog. |
| 110 | * @wddev: the watchdog device to stop |
| 111 | * |
| 112 | * Stop the watchdog if it is still active and unmark it active. |
| 113 | * This function returns zero on success or a negative errno code for |
| 114 | * failure. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 115 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 116 | */ |
| 117 | |
| 118 | static int watchdog_stop(struct watchdog_device *wddev) |
| 119 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 120 | int err = 0; |
| 121 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 122 | mutex_lock(&wddev->lock); |
| 123 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 124 | if (!watchdog_active(wddev)) |
| 125 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 126 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 127 | if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { |
Alan Cox | 3dfd621 | 2012-05-11 12:00:22 +0200 | [diff] [blame] | 128 | dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n"); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 129 | err = -EBUSY; |
| 130 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 131 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 132 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 133 | err = wddev->ops->stop(wddev); |
| 134 | if (err == 0) |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 135 | clear_bit(WDOG_ACTIVE, &wddev->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 136 | |
| 137 | out_stop: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 138 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 139 | return err; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * watchdog_get_status: wrapper to get the watchdog status |
| 144 | * @wddev: the watchdog device to get the status from |
| 145 | * @status: the status of the watchdog device |
| 146 | * |
| 147 | * Get the watchdog's status flags. |
| 148 | */ |
| 149 | |
| 150 | static int watchdog_get_status(struct watchdog_device *wddev, |
| 151 | unsigned int *status) |
| 152 | { |
| 153 | int err = 0; |
| 154 | |
| 155 | *status = 0; |
| 156 | if (!wddev->ops->status) |
| 157 | return -EOPNOTSUPP; |
| 158 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 159 | mutex_lock(&wddev->lock); |
| 160 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 161 | *status = wddev->ops->status(wddev); |
| 162 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 163 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 164 | return err; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * watchdog_set_timeout: set the watchdog timer timeout |
| 169 | * @wddev: the watchdog device to set the timeout for |
| 170 | * @timeout: timeout to set in seconds |
| 171 | */ |
| 172 | |
| 173 | static int watchdog_set_timeout(struct watchdog_device *wddev, |
| 174 | unsigned int timeout) |
| 175 | { |
| 176 | int err; |
| 177 | |
| 178 | if ((wddev->ops->set_timeout == NULL) || |
| 179 | !(wddev->info->options & WDIOF_SETTIMEOUT)) |
| 180 | return -EOPNOTSUPP; |
| 181 | |
| 182 | if ((wddev->max_timeout != 0) && |
| 183 | (timeout < wddev->min_timeout || timeout > wddev->max_timeout)) |
| 184 | return -EINVAL; |
| 185 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 186 | mutex_lock(&wddev->lock); |
| 187 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 188 | err = wddev->ops->set_timeout(wddev, timeout); |
| 189 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 190 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 191 | return err; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * watchdog_get_timeleft: wrapper to get the time left before a reboot |
| 196 | * @wddev: the watchdog device to get the remaining time from |
| 197 | * @timeleft: the time that's left |
| 198 | * |
| 199 | * Get the time before a watchdog will reboot (if not pinged). |
| 200 | */ |
| 201 | |
| 202 | static int watchdog_get_timeleft(struct watchdog_device *wddev, |
| 203 | unsigned int *timeleft) |
| 204 | { |
| 205 | int err = 0; |
| 206 | |
| 207 | *timeleft = 0; |
| 208 | if (!wddev->ops->get_timeleft) |
| 209 | return -EOPNOTSUPP; |
| 210 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 211 | mutex_lock(&wddev->lock); |
| 212 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 213 | *timeleft = wddev->ops->get_timeleft(wddev); |
| 214 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 215 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 216 | return err; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined |
| 221 | * @wddev: the watchdog device to do the ioctl on |
| 222 | * @cmd: watchdog command |
| 223 | * @arg: argument pointer |
| 224 | */ |
| 225 | |
| 226 | static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd, |
| 227 | unsigned long arg) |
| 228 | { |
| 229 | int err; |
| 230 | |
| 231 | if (!wddev->ops->ioctl) |
| 232 | return -ENOIOCTLCMD; |
| 233 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 234 | mutex_lock(&wddev->lock); |
| 235 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 236 | err = wddev->ops->ioctl(wddev, cmd, arg); |
| 237 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame^] | 238 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 239 | return err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
| 243 | * watchdog_write: writes to the watchdog. |
| 244 | * @file: file from VFS |
| 245 | * @data: user address of data |
| 246 | * @len: length of data |
| 247 | * @ppos: pointer to the file offset |
| 248 | * |
| 249 | * A write to a watchdog device is defined as a keepalive ping. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 250 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 251 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 252 | */ |
| 253 | |
| 254 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 255 | size_t len, loff_t *ppos) |
| 256 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 257 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 258 | size_t i; |
| 259 | char c; |
| 260 | |
| 261 | if (len == 0) |
| 262 | return 0; |
| 263 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 264 | /* |
| 265 | * Note: just in case someone wrote the magic character |
| 266 | * five months ago... |
| 267 | */ |
| 268 | clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
| 269 | |
| 270 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 271 | for (i = 0; i != len; i++) { |
| 272 | if (get_user(c, data + i)) |
| 273 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 274 | if (c == 'V') |
| 275 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
| 279 | watchdog_ping(wdd); |
| 280 | |
| 281 | return len; |
| 282 | } |
| 283 | |
| 284 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 285 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 286 | * @file: file handle to the device |
| 287 | * @cmd: watchdog command |
| 288 | * @arg: argument pointer |
| 289 | * |
| 290 | * The watchdog API defines a common set of functions for all watchdogs |
| 291 | * according to their available features. |
| 292 | */ |
| 293 | |
| 294 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 295 | unsigned long arg) |
| 296 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 297 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 298 | void __user *argp = (void __user *)arg; |
| 299 | int __user *p = argp; |
| 300 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 301 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 302 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 303 | err = watchdog_ioctl_op(wdd, cmd, arg); |
| 304 | if (err != -ENOIOCTLCMD) |
| 305 | return err; |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 306 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 307 | switch (cmd) { |
| 308 | case WDIOC_GETSUPPORT: |
| 309 | return copy_to_user(argp, wdd->info, |
| 310 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
| 311 | case WDIOC_GETSTATUS: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 312 | err = watchdog_get_status(wdd, &val); |
| 313 | if (err) |
| 314 | return err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 315 | return put_user(val, p); |
| 316 | case WDIOC_GETBOOTSTATUS: |
| 317 | return put_user(wdd->bootstatus, p); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 318 | case WDIOC_SETOPTIONS: |
| 319 | if (get_user(val, p)) |
| 320 | return -EFAULT; |
| 321 | if (val & WDIOS_DISABLECARD) { |
| 322 | err = watchdog_stop(wdd); |
| 323 | if (err < 0) |
| 324 | return err; |
| 325 | } |
| 326 | if (val & WDIOS_ENABLECARD) { |
| 327 | err = watchdog_start(wdd); |
| 328 | if (err < 0) |
| 329 | return err; |
| 330 | } |
| 331 | return 0; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 332 | case WDIOC_KEEPALIVE: |
| 333 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 334 | return -EOPNOTSUPP; |
| 335 | watchdog_ping(wdd); |
| 336 | return 0; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 337 | case WDIOC_SETTIMEOUT: |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 338 | if (get_user(val, p)) |
| 339 | return -EFAULT; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 340 | err = watchdog_set_timeout(wdd, val); |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 341 | if (err < 0) |
| 342 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 343 | /* If the watchdog is active then we send a keepalive ping |
| 344 | * to make sure that the watchdog keep's running (and if |
| 345 | * possible that it takes the new timeout) */ |
| 346 | watchdog_ping(wdd); |
| 347 | /* Fall */ |
| 348 | case WDIOC_GETTIMEOUT: |
| 349 | /* timeout == 0 means that we don't know the timeout */ |
| 350 | if (wdd->timeout == 0) |
| 351 | return -EOPNOTSUPP; |
| 352 | return put_user(wdd->timeout, p); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 353 | case WDIOC_GETTIMELEFT: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 354 | err = watchdog_get_timeleft(wdd, &val); |
| 355 | if (err) |
| 356 | return err; |
| 357 | return put_user(val, p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 358 | default: |
| 359 | return -ENOTTY; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 364 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 365 | * @inode: inode of device |
| 366 | * @file: file handle to device |
| 367 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 368 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 369 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 370 | * it can only be opened once. |
| 371 | */ |
| 372 | |
| 373 | static int watchdog_open(struct inode *inode, struct file *file) |
| 374 | { |
| 375 | int err = -EBUSY; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 376 | struct watchdog_device *wdd; |
| 377 | |
| 378 | /* Get the corresponding watchdog device */ |
| 379 | if (imajor(inode) == MISC_MAJOR) |
| 380 | wdd = old_wdd; |
| 381 | else |
| 382 | wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 383 | |
| 384 | /* the watchdog is single open! */ |
| 385 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) |
| 386 | return -EBUSY; |
| 387 | |
| 388 | /* |
| 389 | * If the /dev/watchdog device is open, we don't want the module |
| 390 | * to be unloaded. |
| 391 | */ |
| 392 | if (!try_module_get(wdd->ops->owner)) |
| 393 | goto out; |
| 394 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 395 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 396 | if (err < 0) |
| 397 | goto out_mod; |
| 398 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 399 | file->private_data = wdd; |
| 400 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 401 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 402 | return nonseekable_open(inode, file); |
| 403 | |
| 404 | out_mod: |
| 405 | module_put(wdd->ops->owner); |
| 406 | out: |
| 407 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 408 | return err; |
| 409 | } |
| 410 | |
| 411 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 412 | * watchdog_release: release the watchdog device. |
| 413 | * @inode: inode of device |
| 414 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 415 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 416 | * This is the code for when /dev/watchdog gets closed. We will only |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 417 | * stop the watchdog when we have received the magic char (and nowayout |
| 418 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 419 | */ |
| 420 | |
| 421 | static int watchdog_release(struct inode *inode, struct file *file) |
| 422 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 423 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 424 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 425 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 426 | /* |
| 427 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 428 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 429 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 430 | */ |
| 431 | if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || |
| 432 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
| 433 | err = watchdog_stop(wdd); |
| 434 | |
| 435 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 436 | if (err < 0) { |
Alan Cox | 3dfd621 | 2012-05-11 12:00:22 +0200 | [diff] [blame] | 437 | dev_crit(wdd->dev, "watchdog did not stop!\n"); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 438 | watchdog_ping(wdd); |
| 439 | } |
| 440 | |
| 441 | /* Allow the owner module to be unloaded again */ |
| 442 | module_put(wdd->ops->owner); |
| 443 | |
| 444 | /* make sure that /dev/watchdog can be re-opened */ |
| 445 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static const struct file_operations watchdog_fops = { |
| 451 | .owner = THIS_MODULE, |
| 452 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 453 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 454 | .open = watchdog_open, |
| 455 | .release = watchdog_release, |
| 456 | }; |
| 457 | |
| 458 | static struct miscdevice watchdog_miscdev = { |
| 459 | .minor = WATCHDOG_MINOR, |
| 460 | .name = "watchdog", |
| 461 | .fops = &watchdog_fops, |
| 462 | }; |
| 463 | |
| 464 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 465 | * watchdog_dev_register: register a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 466 | * @watchdog: watchdog device |
| 467 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 468 | * Register a watchdog device including handling the legacy |
| 469 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 470 | * thus we set it up like that. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 471 | */ |
| 472 | |
| 473 | int watchdog_dev_register(struct watchdog_device *watchdog) |
| 474 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 475 | int err, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 476 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 477 | if (watchdog->id == 0) { |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 478 | watchdog_miscdev.parent = watchdog->parent; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 479 | err = misc_register(&watchdog_miscdev); |
| 480 | if (err != 0) { |
| 481 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", |
| 482 | watchdog->info->identity, WATCHDOG_MINOR, err); |
| 483 | if (err == -EBUSY) |
| 484 | pr_err("%s: a legacy watchdog module is probably present.\n", |
| 485 | watchdog->info->identity); |
| 486 | return err; |
| 487 | } |
| 488 | old_wdd = watchdog; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 491 | /* Fill in the data structures */ |
| 492 | devno = MKDEV(MAJOR(watchdog_devt), watchdog->id); |
| 493 | cdev_init(&watchdog->cdev, &watchdog_fops); |
| 494 | watchdog->cdev.owner = watchdog->ops->owner; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 495 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 496 | /* Add the device */ |
| 497 | err = cdev_add(&watchdog->cdev, devno, 1); |
| 498 | if (err) { |
| 499 | pr_err("watchdog%d unable to add device %d:%d\n", |
| 500 | watchdog->id, MAJOR(watchdog_devt), watchdog->id); |
| 501 | if (watchdog->id == 0) { |
| 502 | misc_deregister(&watchdog_miscdev); |
| 503 | old_wdd = NULL; |
| 504 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 505 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 506 | return err; |
| 507 | } |
| 508 | |
| 509 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 510 | * watchdog_dev_unregister: unregister a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 511 | * @watchdog: watchdog device |
| 512 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 513 | * Unregister the watchdog and if needed the legacy /dev/watchdog device. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 514 | */ |
| 515 | |
| 516 | int watchdog_dev_unregister(struct watchdog_device *watchdog) |
| 517 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 518 | cdev_del(&watchdog->cdev); |
| 519 | if (watchdog->id == 0) { |
| 520 | misc_deregister(&watchdog_miscdev); |
| 521 | old_wdd = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 522 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | } |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 525 | |
| 526 | /* |
| 527 | * watchdog_dev_init: init dev part of watchdog core |
| 528 | * |
| 529 | * Allocate a range of chardev nodes to use for watchdog devices |
| 530 | */ |
| 531 | |
| 532 | int __init watchdog_dev_init(void) |
| 533 | { |
| 534 | int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); |
| 535 | if (err < 0) |
| 536 | pr_err("watchdog: unable to allocate char dev region\n"); |
| 537 | return err; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * watchdog_dev_exit: exit dev part of watchdog core |
| 542 | * |
| 543 | * Release the range of chardev nodes used for watchdog devices |
| 544 | */ |
| 545 | |
| 546 | void __exit watchdog_dev_exit(void) |
| 547 | { |
| 548 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); |
| 549 | } |