4 * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
7 * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
10 * This source code is part of the generic code that can be used
11 * by all the watchdog timer drivers.
13 * This part of the generic code takes care of the following
14 * misc device: /dev/watchdog.
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>
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.
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.
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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/... */
45 #include "watchdog_core.h"
47 /* the dev_t structure to store the dynamically allocated watchdog devices */
48 static dev_t watchdog_devt;
49 /* the watchdog device behind /dev/watchdog */
50 static struct watchdog_device *old_wdd;
53 * watchdog_ping: ping the watchdog.
54 * @wddev: the watchdog device to ping
56 * If the watchdog has no own ping operation then it needs to be
57 * restarted via the start operation. This wrapper function does
59 * We only ping when the watchdog device is running.
62 static int watchdog_ping(struct watchdog_device *wddev)
66 mutex_lock(&wddev->lock);
68 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
73 if (!watchdog_active(wddev))
77 err = wddev->ops->ping(wddev); /* ping the watchdog */
79 err = wddev->ops->start(wddev); /* restart watchdog */
82 mutex_unlock(&wddev->lock);
87 * watchdog_start: wrapper to start the watchdog.
88 * @wddev: the watchdog device to start
90 * Start the watchdog if it is not active and mark it active.
91 * This function returns zero on success or a negative errno code for
95 static int watchdog_start(struct watchdog_device *wddev)
99 mutex_lock(&wddev->lock);
101 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
106 if (watchdog_active(wddev))
109 err = wddev->ops->start(wddev);
111 set_bit(WDOG_ACTIVE, &wddev->status);
114 mutex_unlock(&wddev->lock);
119 * watchdog_stop: wrapper to stop the watchdog.
120 * @wddev: the watchdog device to stop
122 * Stop the watchdog if it is still active and unmark it active.
123 * This function returns zero on success or a negative errno code for
125 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
128 static int watchdog_stop(struct watchdog_device *wddev)
132 mutex_lock(&wddev->lock);
134 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
139 if (!watchdog_active(wddev))
142 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
143 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
148 err = wddev->ops->stop(wddev);
150 clear_bit(WDOG_ACTIVE, &wddev->status);
153 mutex_unlock(&wddev->lock);
158 * watchdog_get_status: wrapper to get the watchdog status
159 * @wddev: the watchdog device to get the status from
160 * @status: the status of the watchdog device
162 * Get the watchdog's status flags.
165 static int watchdog_get_status(struct watchdog_device *wddev,
166 unsigned int *status)
171 if (!wddev->ops->status)
174 mutex_lock(&wddev->lock);
176 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
181 *status = wddev->ops->status(wddev);
184 mutex_unlock(&wddev->lock);
189 * watchdog_set_timeout: set the watchdog timer timeout
190 * @wddev: the watchdog device to set the timeout for
191 * @timeout: timeout to set in seconds
194 static int watchdog_set_timeout(struct watchdog_device *wddev,
195 unsigned int timeout)
199 if ((wddev->ops->set_timeout == NULL) ||
200 !(wddev->info->options & WDIOF_SETTIMEOUT))
203 if (watchdog_timeout_invalid(wddev, timeout))
206 mutex_lock(&wddev->lock);
208 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
213 err = wddev->ops->set_timeout(wddev, timeout);
216 mutex_unlock(&wddev->lock);
221 * watchdog_get_timeleft: wrapper to get the time left before a reboot
222 * @wddev: the watchdog device to get the remaining time from
223 * @timeleft: the time that's left
225 * Get the time before a watchdog will reboot (if not pinged).
228 static int watchdog_get_timeleft(struct watchdog_device *wddev,
229 unsigned int *timeleft)
234 if (!wddev->ops->get_timeleft)
237 mutex_lock(&wddev->lock);
239 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
244 *timeleft = wddev->ops->get_timeleft(wddev);
247 mutex_unlock(&wddev->lock);
252 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
253 * @wddev: the watchdog device to do the ioctl on
254 * @cmd: watchdog command
255 * @arg: argument pointer
258 static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
263 if (!wddev->ops->ioctl)
266 mutex_lock(&wddev->lock);
268 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
273 err = wddev->ops->ioctl(wddev, cmd, arg);
276 mutex_unlock(&wddev->lock);
281 * watchdog_write: writes to the watchdog.
282 * @file: file from VFS
283 * @data: user address of data
284 * @len: length of data
285 * @ppos: pointer to the file offset
287 * A write to a watchdog device is defined as a keepalive ping.
288 * Writing the magic 'V' sequence allows the next close to turn
289 * off the watchdog (if 'nowayout' is not set).
292 static ssize_t watchdog_write(struct file *file, const char __user *data,
293 size_t len, loff_t *ppos)
295 struct watchdog_device *wdd = file->private_data;
303 * Note: just in case someone wrote the magic character
306 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
308 /* scan to see whether or not we got the magic character */
309 for (i = 0; i != len; i++) {
310 if (get_user(c, data + i))
313 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
316 /* someone wrote to us, so we send the watchdog a keepalive ping */
323 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
324 * @file: file handle to the device
325 * @cmd: watchdog command
326 * @arg: argument pointer
328 * The watchdog API defines a common set of functions for all watchdogs
329 * according to their available features.
332 static long watchdog_ioctl(struct file *file, unsigned int cmd,
335 struct watchdog_device *wdd = file->private_data;
336 void __user *argp = (void __user *)arg;
337 int __user *p = argp;
341 err = watchdog_ioctl_op(wdd, cmd, arg);
342 if (err != -ENOIOCTLCMD)
346 case WDIOC_GETSUPPORT:
347 return copy_to_user(argp, wdd->info,
348 sizeof(struct watchdog_info)) ? -EFAULT : 0;
349 case WDIOC_GETSTATUS:
350 err = watchdog_get_status(wdd, &val);
353 return put_user(val, p);
354 case WDIOC_GETBOOTSTATUS:
355 return put_user(wdd->bootstatus, p);
356 case WDIOC_SETOPTIONS:
357 if (get_user(val, p))
359 if (val & WDIOS_DISABLECARD) {
360 err = watchdog_stop(wdd);
364 if (val & WDIOS_ENABLECARD) {
365 err = watchdog_start(wdd);
370 case WDIOC_KEEPALIVE:
371 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
375 case WDIOC_SETTIMEOUT:
376 if (get_user(val, p))
378 err = watchdog_set_timeout(wdd, val);
381 /* If the watchdog is active then we send a keepalive ping
382 * to make sure that the watchdog keep's running (and if
383 * possible that it takes the new timeout) */
386 case WDIOC_GETTIMEOUT:
387 /* timeout == 0 means that we don't know the timeout */
388 if (wdd->timeout == 0)
390 return put_user(wdd->timeout, p);
391 case WDIOC_GETTIMELEFT:
392 err = watchdog_get_timeleft(wdd, &val);
395 return put_user(val, p);
402 * watchdog_open: open the /dev/watchdog* devices.
403 * @inode: inode of device
404 * @file: file handle to device
406 * When the /dev/watchdog* device gets opened, we start the watchdog.
407 * Watch out: the /dev/watchdog device is single open, so we make sure
408 * it can only be opened once.
411 static int watchdog_open(struct inode *inode, struct file *file)
414 struct watchdog_device *wdd;
416 /* Get the corresponding watchdog device */
417 if (imajor(inode) == MISC_MAJOR)
420 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
422 /* the watchdog is single open! */
423 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
427 * If the /dev/watchdog device is open, we don't want the module
430 if (!try_module_get(wdd->ops->owner))
433 err = watchdog_start(wdd);
437 file->private_data = wdd;
442 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
443 return nonseekable_open(inode, file);
446 module_put(wdd->ops->owner);
448 clear_bit(WDOG_DEV_OPEN, &wdd->status);
453 * watchdog_release: release the watchdog device.
454 * @inode: inode of device
455 * @file: file handle to device
457 * This is the code for when /dev/watchdog gets closed. We will only
458 * stop the watchdog when we have received the magic char (and nowayout
459 * was not set), else the watchdog will keep running.
462 static int watchdog_release(struct inode *inode, struct file *file)
464 struct watchdog_device *wdd = file->private_data;
468 * We only stop the watchdog if we received the magic character
469 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
470 * watchdog_stop will fail.
472 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
473 !(wdd->info->options & WDIOF_MAGICCLOSE))
474 err = watchdog_stop(wdd);
476 /* If the watchdog was not stopped, send a keepalive ping */
478 mutex_lock(&wdd->lock);
479 if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
480 dev_crit(wdd->dev, "watchdog did not stop!\n");
481 mutex_unlock(&wdd->lock);
485 /* Allow the owner module to be unloaded again */
486 module_put(wdd->ops->owner);
488 /* make sure that /dev/watchdog can be re-opened */
489 clear_bit(WDOG_DEV_OPEN, &wdd->status);
491 /* Note wdd may be gone after this, do not use after this! */
493 wdd->ops->unref(wdd);
498 static const struct file_operations watchdog_fops = {
499 .owner = THIS_MODULE,
500 .write = watchdog_write,
501 .unlocked_ioctl = watchdog_ioctl,
502 .open = watchdog_open,
503 .release = watchdog_release,
506 static struct miscdevice watchdog_miscdev = {
507 .minor = WATCHDOG_MINOR,
509 .fops = &watchdog_fops,
513 * watchdog_dev_register: register a watchdog device
514 * @watchdog: watchdog device
516 * Register a watchdog device including handling the legacy
517 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
518 * thus we set it up like that.
521 int watchdog_dev_register(struct watchdog_device *watchdog)
525 if (watchdog->id == 0) {
527 watchdog_miscdev.parent = watchdog->parent;
528 err = misc_register(&watchdog_miscdev);
530 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
531 watchdog->info->identity, WATCHDOG_MINOR, err);
533 pr_err("%s: a legacy watchdog module is probably present.\n",
534 watchdog->info->identity);
540 /* Fill in the data structures */
541 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
542 cdev_init(&watchdog->cdev, &watchdog_fops);
543 watchdog->cdev.owner = watchdog->ops->owner;
546 err = cdev_add(&watchdog->cdev, devno, 1);
548 pr_err("watchdog%d unable to add device %d:%d\n",
549 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
550 if (watchdog->id == 0) {
551 misc_deregister(&watchdog_miscdev);
559 * watchdog_dev_unregister: unregister a watchdog device
560 * @watchdog: watchdog device
562 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
565 int watchdog_dev_unregister(struct watchdog_device *watchdog)
567 mutex_lock(&watchdog->lock);
568 set_bit(WDOG_UNREGISTERED, &watchdog->status);
569 mutex_unlock(&watchdog->lock);
571 cdev_del(&watchdog->cdev);
572 if (watchdog->id == 0) {
573 misc_deregister(&watchdog_miscdev);
580 * watchdog_dev_init: init dev part of watchdog core
582 * Allocate a range of chardev nodes to use for watchdog devices
585 int __init watchdog_dev_init(void)
587 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
589 pr_err("watchdog: unable to allocate char dev region\n");
594 * watchdog_dev_exit: exit dev part of watchdog core
596 * Release the range of chardev nodes used for watchdog devices
599 void __exit watchdog_dev_exit(void)
601 unregister_chrdev_region(watchdog_devt, MAX_DOGS);