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 | |
| 45 | /* make sure we only register one /dev/watchdog device */ |
| 46 | static unsigned long watchdog_dev_busy; |
| 47 | /* the watchdog device behind /dev/watchdog */ |
| 48 | static struct watchdog_device *wdd; |
| 49 | |
| 50 | /* |
| 51 | * watchdog_ping: ping the watchdog. |
| 52 | * @wddev: the watchdog device to ping |
| 53 | * |
| 54 | * If the watchdog has no own ping operation then it needs to be |
| 55 | * restarted via the start operation. This wrapper function does |
| 56 | * exactly that. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 57 | * We only ping when the watchdog device is running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 58 | */ |
| 59 | |
| 60 | static int watchdog_ping(struct watchdog_device *wddev) |
| 61 | { |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 62 | if (test_bit(WDOG_ACTIVE, &wddev->status)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 63 | if (wddev->ops->ping) |
| 64 | return wddev->ops->ping(wddev); /* ping the watchdog */ |
| 65 | else |
| 66 | return wddev->ops->start(wddev); /* restart watchdog */ |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* |
| 72 | * watchdog_start: wrapper to start the watchdog. |
| 73 | * @wddev: the watchdog device to start |
| 74 | * |
| 75 | * Start the watchdog if it is not active and mark it active. |
| 76 | * This function returns zero on success or a negative errno code for |
| 77 | * failure. |
| 78 | */ |
| 79 | |
| 80 | static int watchdog_start(struct watchdog_device *wddev) |
| 81 | { |
| 82 | int err; |
| 83 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 84 | if (!test_bit(WDOG_ACTIVE, &wddev->status)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 85 | err = wddev->ops->start(wddev); |
| 86 | if (err < 0) |
| 87 | return err; |
| 88 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 89 | set_bit(WDOG_ACTIVE, &wddev->status); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * watchdog_stop: wrapper to stop the watchdog. |
| 96 | * @wddev: the watchdog device to stop |
| 97 | * |
| 98 | * Stop the watchdog if it is still active and unmark it active. |
| 99 | * This function returns zero on success or a negative errno code for |
| 100 | * failure. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 101 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 102 | */ |
| 103 | |
| 104 | static int watchdog_stop(struct watchdog_device *wddev) |
| 105 | { |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 106 | int err = -EBUSY; |
| 107 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 108 | if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 109 | pr_info("%s: nowayout prevents watchdog to be stopped!\n", |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 110 | wddev->info->identity); |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 111 | return err; |
| 112 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 113 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 114 | if (test_bit(WDOG_ACTIVE, &wddev->status)) { |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 115 | err = wddev->ops->stop(wddev); |
| 116 | if (err < 0) |
| 117 | return err; |
| 118 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 119 | clear_bit(WDOG_ACTIVE, &wddev->status); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 120 | } |
| 121 | return 0; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * watchdog_write: writes to the watchdog. |
| 126 | * @file: file from VFS |
| 127 | * @data: user address of data |
| 128 | * @len: length of data |
| 129 | * @ppos: pointer to the file offset |
| 130 | * |
| 131 | * 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] | 132 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 133 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 134 | */ |
| 135 | |
| 136 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 137 | size_t len, loff_t *ppos) |
| 138 | { |
| 139 | size_t i; |
| 140 | char c; |
| 141 | |
| 142 | if (len == 0) |
| 143 | return 0; |
| 144 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Note: just in case someone wrote the magic character |
| 147 | * five months ago... |
| 148 | */ |
| 149 | clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
| 150 | |
| 151 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 152 | for (i = 0; i != len; i++) { |
| 153 | if (get_user(c, data + i)) |
| 154 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 155 | if (c == 'V') |
| 156 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
| 160 | watchdog_ping(wdd); |
| 161 | |
| 162 | return len; |
| 163 | } |
| 164 | |
| 165 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 166 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 167 | * @file: file handle to the device |
| 168 | * @cmd: watchdog command |
| 169 | * @arg: argument pointer |
| 170 | * |
| 171 | * The watchdog API defines a common set of functions for all watchdogs |
| 172 | * according to their available features. |
| 173 | */ |
| 174 | |
| 175 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 176 | unsigned long arg) |
| 177 | { |
| 178 | void __user *argp = (void __user *)arg; |
| 179 | int __user *p = argp; |
| 180 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 181 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 182 | |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 183 | if (wdd->ops->ioctl) { |
| 184 | err = wdd->ops->ioctl(wdd, cmd, arg); |
| 185 | if (err != -ENOIOCTLCMD) |
| 186 | return err; |
| 187 | } |
| 188 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 189 | switch (cmd) { |
| 190 | case WDIOC_GETSUPPORT: |
| 191 | return copy_to_user(argp, wdd->info, |
| 192 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
| 193 | case WDIOC_GETSTATUS: |
| 194 | val = wdd->ops->status ? wdd->ops->status(wdd) : 0; |
| 195 | return put_user(val, p); |
| 196 | case WDIOC_GETBOOTSTATUS: |
| 197 | return put_user(wdd->bootstatus, p); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 198 | case WDIOC_SETOPTIONS: |
| 199 | if (get_user(val, p)) |
| 200 | return -EFAULT; |
| 201 | if (val & WDIOS_DISABLECARD) { |
| 202 | err = watchdog_stop(wdd); |
| 203 | if (err < 0) |
| 204 | return err; |
| 205 | } |
| 206 | if (val & WDIOS_ENABLECARD) { |
| 207 | err = watchdog_start(wdd); |
| 208 | if (err < 0) |
| 209 | return err; |
| 210 | } |
| 211 | return 0; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 212 | case WDIOC_KEEPALIVE: |
| 213 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 214 | return -EOPNOTSUPP; |
| 215 | watchdog_ping(wdd); |
| 216 | return 0; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 217 | case WDIOC_SETTIMEOUT: |
| 218 | if ((wdd->ops->set_timeout == NULL) || |
| 219 | !(wdd->info->options & WDIOF_SETTIMEOUT)) |
| 220 | return -EOPNOTSUPP; |
| 221 | if (get_user(val, p)) |
| 222 | return -EFAULT; |
Wim Van Sebroeck | 3f43f68 | 2011-07-22 19:00:16 +0000 | [diff] [blame] | 223 | if ((wdd->max_timeout != 0) && |
| 224 | (val < wdd->min_timeout || val > wdd->max_timeout)) |
| 225 | return -EINVAL; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 226 | err = wdd->ops->set_timeout(wdd, val); |
| 227 | if (err < 0) |
| 228 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 229 | /* If the watchdog is active then we send a keepalive ping |
| 230 | * to make sure that the watchdog keep's running (and if |
| 231 | * possible that it takes the new timeout) */ |
| 232 | watchdog_ping(wdd); |
| 233 | /* Fall */ |
| 234 | case WDIOC_GETTIMEOUT: |
| 235 | /* timeout == 0 means that we don't know the timeout */ |
| 236 | if (wdd->timeout == 0) |
| 237 | return -EOPNOTSUPP; |
| 238 | return put_user(wdd->timeout, p); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 239 | case WDIOC_GETTIMELEFT: |
| 240 | if (!wdd->ops->get_timeleft) |
| 241 | return -EOPNOTSUPP; |
| 242 | |
| 243 | return put_user(wdd->ops->get_timeleft(wdd), p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 244 | default: |
| 245 | return -ENOTTY; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /* |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 250 | * watchdog_open: open the /dev/watchdog device. |
| 251 | * @inode: inode of device |
| 252 | * @file: file handle to device |
| 253 | * |
| 254 | * When the /dev/watchdog device gets opened, we start the watchdog. |
| 255 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 256 | * it can only be opened once. |
| 257 | */ |
| 258 | |
| 259 | static int watchdog_open(struct inode *inode, struct file *file) |
| 260 | { |
| 261 | int err = -EBUSY; |
| 262 | |
| 263 | /* the watchdog is single open! */ |
| 264 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) |
| 265 | return -EBUSY; |
| 266 | |
| 267 | /* |
| 268 | * If the /dev/watchdog device is open, we don't want the module |
| 269 | * to be unloaded. |
| 270 | */ |
| 271 | if (!try_module_get(wdd->ops->owner)) |
| 272 | goto out; |
| 273 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 274 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 275 | if (err < 0) |
| 276 | goto out_mod; |
| 277 | |
| 278 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 279 | return nonseekable_open(inode, file); |
| 280 | |
| 281 | out_mod: |
| 282 | module_put(wdd->ops->owner); |
| 283 | out: |
| 284 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 285 | return err; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * watchdog_release: release the /dev/watchdog device. |
| 290 | * @inode: inode of device |
| 291 | * @file: file handle to device |
| 292 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 293 | * 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] | 294 | * stop the watchdog when we have received the magic char (and nowayout |
| 295 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 296 | */ |
| 297 | |
| 298 | static int watchdog_release(struct inode *inode, struct file *file) |
| 299 | { |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 300 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 301 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 302 | /* |
| 303 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 304 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 305 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 306 | */ |
| 307 | if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || |
| 308 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
| 309 | err = watchdog_stop(wdd); |
| 310 | |
| 311 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 312 | if (err < 0) { |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 313 | pr_crit("%s: watchdog did not stop!\n", wdd->info->identity); |
| 314 | watchdog_ping(wdd); |
| 315 | } |
| 316 | |
| 317 | /* Allow the owner module to be unloaded again */ |
| 318 | module_put(wdd->ops->owner); |
| 319 | |
| 320 | /* make sure that /dev/watchdog can be re-opened */ |
| 321 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static const struct file_operations watchdog_fops = { |
| 327 | .owner = THIS_MODULE, |
| 328 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 329 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 330 | .open = watchdog_open, |
| 331 | .release = watchdog_release, |
| 332 | }; |
| 333 | |
| 334 | static struct miscdevice watchdog_miscdev = { |
| 335 | .minor = WATCHDOG_MINOR, |
| 336 | .name = "watchdog", |
| 337 | .fops = &watchdog_fops, |
| 338 | }; |
| 339 | |
| 340 | /* |
| 341 | * watchdog_dev_register: |
| 342 | * @watchdog: watchdog device |
| 343 | * |
| 344 | * Register a watchdog device as /dev/watchdog. /dev/watchdog |
| 345 | * is actually a miscdevice and thus we set it up like that. |
| 346 | */ |
| 347 | |
| 348 | int watchdog_dev_register(struct watchdog_device *watchdog) |
| 349 | { |
| 350 | int err; |
| 351 | |
| 352 | /* Only one device can register for /dev/watchdog */ |
| 353 | if (test_and_set_bit(0, &watchdog_dev_busy)) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 354 | pr_err("only one watchdog can use /dev/watchdog\n"); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 355 | return -EBUSY; |
| 356 | } |
| 357 | |
| 358 | wdd = watchdog; |
| 359 | |
| 360 | err = misc_register(&watchdog_miscdev); |
| 361 | if (err != 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 362 | pr_err("%s: cannot register miscdev on minor=%d (err=%d)\n", |
| 363 | watchdog->info->identity, WATCHDOG_MINOR, err); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | |
| 369 | out: |
| 370 | wdd = NULL; |
| 371 | clear_bit(0, &watchdog_dev_busy); |
| 372 | return err; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * watchdog_dev_unregister: |
| 377 | * @watchdog: watchdog device |
| 378 | * |
| 379 | * Deregister the /dev/watchdog device. |
| 380 | */ |
| 381 | |
| 382 | int watchdog_dev_unregister(struct watchdog_device *watchdog) |
| 383 | { |
| 384 | /* Check that a watchdog device was registered in the past */ |
| 385 | if (!test_bit(0, &watchdog_dev_busy) || !wdd) |
| 386 | return -ENODEV; |
| 387 | |
| 388 | /* We can only unregister the watchdog device that was registered */ |
| 389 | if (watchdog != wdd) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 390 | pr_err("%s: watchdog was not registered as /dev/watchdog\n", |
| 391 | watchdog->info->identity); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 392 | return -ENODEV; |
| 393 | } |
| 394 | |
| 395 | misc_deregister(&watchdog_miscdev); |
| 396 | wdd = NULL; |
| 397 | clear_bit(0, &watchdog_dev_busy); |
| 398 | return 0; |
| 399 | } |