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/cdev.h> /* For character device */
36 #include <linux/errno.h> /* For the -ENODEV/... values */
37 #include <linux/fs.h> /* For file operations */
38 #include <linux/init.h> /* For __init/__exit/... */
39 #include <linux/jiffies.h> /* For timeout functions */
40 #include <linux/kernel.h> /* For printk/panic/... */
41 #include <linux/kref.h> /* For data references */
42 #include <linux/miscdevice.h> /* For handling misc devices */
43 #include <linux/module.h> /* For module stuff/... */
44 #include <linux/mutex.h> /* For mutexes */
45 #include <linux/slab.h> /* For memory functions */
46 #include <linux/types.h> /* For standard types (like size_t) */
47 #include <linux/watchdog.h> /* For watchdog specific items */
48 #include <linux/workqueue.h> /* For workqueue */
49 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
51 #include "watchdog_core.h"
54 * struct watchdog_core_data - watchdog core internal data
55 * @kref: Reference count.
56 * @cdev: The watchdog's Character device.
57 * @wdd: Pointer to watchdog device.
58 * @lock: Lock for watchdog core.
59 * @status: Watchdog core internal status bits.
61 struct watchdog_core_data {
64 struct watchdog_device *wdd;
66 unsigned long last_keepalive;
67 struct delayed_work work;
68 unsigned long status; /* Internal status bits */
69 #define _WDOG_DEV_OPEN 0 /* Opened ? */
70 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
73 /* the dev_t structure to store the dynamically allocated watchdog devices */
74 static dev_t watchdog_devt;
75 /* Reference to watchdog device behind /dev/watchdog */
76 static struct watchdog_core_data *old_wd_data;
78 static struct workqueue_struct *watchdog_wq;
80 static inline bool watchdog_need_worker(struct watchdog_device *wdd)
82 /* All variables in milli-seconds */
83 unsigned int hm = wdd->max_hw_heartbeat_ms;
84 unsigned int t = wdd->timeout * 1000;
87 * A worker to generate heartbeat requests is needed if all of the
88 * following conditions are true.
89 * - Userspace activated the watchdog.
90 * - The driver provided a value for the maximum hardware timeout, and
91 * thus is aware that the framework supports generating heartbeat
93 * - Userspace requests a longer timeout than the hardware can handle.
95 return watchdog_active(wdd) && hm && t > hm;
98 static long watchdog_next_keepalive(struct watchdog_device *wdd)
100 struct watchdog_core_data *wd_data = wdd->wd_data;
101 unsigned int timeout_ms = wdd->timeout * 1000;
102 unsigned long keepalive_interval;
103 unsigned long last_heartbeat;
104 unsigned long virt_timeout;
105 unsigned int hw_heartbeat_ms;
107 virt_timeout = wd_data->last_keepalive + msecs_to_jiffies(timeout_ms);
108 hw_heartbeat_ms = min(timeout_ms, wdd->max_hw_heartbeat_ms);
109 keepalive_interval = msecs_to_jiffies(hw_heartbeat_ms / 2);
112 * To ensure that the watchdog times out wdd->timeout seconds
113 * after the most recent ping from userspace, the last
114 * worker ping has to come in hw_heartbeat_ms before this timeout.
116 last_heartbeat = virt_timeout - msecs_to_jiffies(hw_heartbeat_ms);
117 return min_t(long, last_heartbeat - jiffies, keepalive_interval);
120 static inline void watchdog_update_worker(struct watchdog_device *wdd)
122 struct watchdog_core_data *wd_data = wdd->wd_data;
124 if (watchdog_need_worker(wdd)) {
125 long t = watchdog_next_keepalive(wdd);
128 mod_delayed_work(watchdog_wq, &wd_data->work, t);
130 cancel_delayed_work(&wd_data->work);
134 static int __watchdog_ping(struct watchdog_device *wdd)
139 err = wdd->ops->ping(wdd); /* ping the watchdog */
141 err = wdd->ops->start(wdd); /* restart watchdog */
143 watchdog_update_worker(wdd);
149 * watchdog_ping: ping the watchdog.
150 * @wdd: the watchdog device to ping
152 * The caller must hold wd_data->lock.
154 * If the watchdog has no own ping operation then it needs to be
155 * restarted via the start operation. This wrapper function does
157 * We only ping when the watchdog device is running.
160 static int watchdog_ping(struct watchdog_device *wdd)
162 struct watchdog_core_data *wd_data = wdd->wd_data;
164 if (!watchdog_active(wdd))
167 wd_data->last_keepalive = jiffies;
168 return __watchdog_ping(wdd);
171 static void watchdog_ping_work(struct work_struct *work)
173 struct watchdog_core_data *wd_data;
174 struct watchdog_device *wdd;
176 wd_data = container_of(to_delayed_work(work), struct watchdog_core_data,
179 mutex_lock(&wd_data->lock);
181 if (wdd && watchdog_active(wdd))
182 __watchdog_ping(wdd);
183 mutex_unlock(&wd_data->lock);
187 * watchdog_start: wrapper to start the watchdog.
188 * @wdd: the watchdog device to start
190 * The caller must hold wd_data->lock.
192 * Start the watchdog if it is not active and mark it active.
193 * This function returns zero on success or a negative errno code for
197 static int watchdog_start(struct watchdog_device *wdd)
199 struct watchdog_core_data *wd_data = wdd->wd_data;
200 unsigned long started_at;
203 if (watchdog_active(wdd))
206 started_at = jiffies;
207 err = wdd->ops->start(wdd);
209 set_bit(WDOG_ACTIVE, &wdd->status);
210 wd_data->last_keepalive = started_at;
211 watchdog_update_worker(wdd);
218 * watchdog_stop: wrapper to stop the watchdog.
219 * @wdd: the watchdog device to stop
221 * The caller must hold wd_data->lock.
223 * Stop the watchdog if it is still active and unmark it active.
224 * This function returns zero on success or a negative errno code for
226 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
229 static int watchdog_stop(struct watchdog_device *wdd)
231 struct watchdog_core_data *wd_data = wdd->wd_data;
234 if (!watchdog_active(wdd))
237 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
238 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
243 err = wdd->ops->stop(wdd);
245 clear_bit(WDOG_ACTIVE, &wdd->status);
246 cancel_delayed_work(&wd_data->work);
253 * watchdog_get_status: wrapper to get the watchdog status
254 * @wdd: the watchdog device to get the status from
256 * The caller must hold wd_data->lock.
258 * Get the watchdog's status flags.
261 static unsigned int watchdog_get_status(struct watchdog_device *wdd)
263 if (!wdd->ops->status)
266 return wdd->ops->status(wdd);
270 * watchdog_set_timeout: set the watchdog timer timeout
271 * @wdd: the watchdog device to set the timeout for
272 * @timeout: timeout to set in seconds
274 * The caller must hold wd_data->lock.
277 static int watchdog_set_timeout(struct watchdog_device *wdd,
278 unsigned int timeout)
282 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
285 if (watchdog_timeout_invalid(wdd, timeout))
288 if (wdd->ops->set_timeout)
289 err = wdd->ops->set_timeout(wdd, timeout);
291 wdd->timeout = timeout;
293 watchdog_update_worker(wdd);
299 * watchdog_get_timeleft: wrapper to get the time left before a reboot
300 * @wdd: the watchdog device to get the remaining time from
301 * @timeleft: the time that's left
303 * The caller must hold wd_data->lock.
305 * Get the time before a watchdog will reboot (if not pinged).
308 static int watchdog_get_timeleft(struct watchdog_device *wdd,
309 unsigned int *timeleft)
313 if (!wdd->ops->get_timeleft)
316 *timeleft = wdd->ops->get_timeleft(wdd);
321 #ifdef CONFIG_WATCHDOG_SYSFS
322 static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
325 struct watchdog_device *wdd = dev_get_drvdata(dev);
327 return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
329 static DEVICE_ATTR_RO(nowayout);
331 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
334 struct watchdog_device *wdd = dev_get_drvdata(dev);
335 struct watchdog_core_data *wd_data = wdd->wd_data;
338 mutex_lock(&wd_data->lock);
339 status = watchdog_get_status(wdd);
340 mutex_unlock(&wd_data->lock);
342 return sprintf(buf, "%u\n", status);
344 static DEVICE_ATTR_RO(status);
346 static ssize_t bootstatus_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct watchdog_device *wdd = dev_get_drvdata(dev);
351 return sprintf(buf, "%u\n", wdd->bootstatus);
353 static DEVICE_ATTR_RO(bootstatus);
355 static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
358 struct watchdog_device *wdd = dev_get_drvdata(dev);
359 struct watchdog_core_data *wd_data = wdd->wd_data;
363 mutex_lock(&wd_data->lock);
364 status = watchdog_get_timeleft(wdd, &val);
365 mutex_unlock(&wd_data->lock);
367 status = sprintf(buf, "%u\n", val);
371 static DEVICE_ATTR_RO(timeleft);
373 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
376 struct watchdog_device *wdd = dev_get_drvdata(dev);
378 return sprintf(buf, "%u\n", wdd->timeout);
380 static DEVICE_ATTR_RO(timeout);
382 static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
385 struct watchdog_device *wdd = dev_get_drvdata(dev);
387 return sprintf(buf, "%s\n", wdd->info->identity);
389 static DEVICE_ATTR_RO(identity);
391 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
394 struct watchdog_device *wdd = dev_get_drvdata(dev);
396 if (watchdog_active(wdd))
397 return sprintf(buf, "active\n");
399 return sprintf(buf, "inactive\n");
401 static DEVICE_ATTR_RO(state);
403 static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
406 struct device *dev = container_of(kobj, struct device, kobj);
407 struct watchdog_device *wdd = dev_get_drvdata(dev);
408 umode_t mode = attr->mode;
410 if (attr == &dev_attr_status.attr && !wdd->ops->status)
412 else if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
417 static struct attribute *wdt_attrs[] = {
418 &dev_attr_state.attr,
419 &dev_attr_identity.attr,
420 &dev_attr_timeout.attr,
421 &dev_attr_timeleft.attr,
422 &dev_attr_bootstatus.attr,
423 &dev_attr_status.attr,
424 &dev_attr_nowayout.attr,
428 static const struct attribute_group wdt_group = {
430 .is_visible = wdt_is_visible,
432 __ATTRIBUTE_GROUPS(wdt);
434 #define wdt_groups NULL
438 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
439 * @wdd: the watchdog device to do the ioctl on
440 * @cmd: watchdog command
441 * @arg: argument pointer
443 * The caller must hold wd_data->lock.
446 static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
449 if (!wdd->ops->ioctl)
452 return wdd->ops->ioctl(wdd, cmd, arg);
456 * watchdog_write: writes to the watchdog.
457 * @file: file from VFS
458 * @data: user address of data
459 * @len: length of data
460 * @ppos: pointer to the file offset
462 * A write to a watchdog device is defined as a keepalive ping.
463 * Writing the magic 'V' sequence allows the next close to turn
464 * off the watchdog (if 'nowayout' is not set).
467 static ssize_t watchdog_write(struct file *file, const char __user *data,
468 size_t len, loff_t *ppos)
470 struct watchdog_core_data *wd_data = file->private_data;
471 struct watchdog_device *wdd;
480 * Note: just in case someone wrote the magic character
483 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
485 /* scan to see whether or not we got the magic character */
486 for (i = 0; i != len; i++) {
487 if (get_user(c, data + i))
490 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
493 /* someone wrote to us, so we send the watchdog a keepalive ping */
496 mutex_lock(&wd_data->lock);
499 err = watchdog_ping(wdd);
500 mutex_unlock(&wd_data->lock);
509 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
510 * @file: file handle to the device
511 * @cmd: watchdog command
512 * @arg: argument pointer
514 * The watchdog API defines a common set of functions for all watchdogs
515 * according to their available features.
518 static long watchdog_ioctl(struct file *file, unsigned int cmd,
521 struct watchdog_core_data *wd_data = file->private_data;
522 void __user *argp = (void __user *)arg;
523 struct watchdog_device *wdd;
524 int __user *p = argp;
528 mutex_lock(&wd_data->lock);
536 err = watchdog_ioctl_op(wdd, cmd, arg);
537 if (err != -ENOIOCTLCMD)
541 case WDIOC_GETSUPPORT:
542 err = copy_to_user(argp, wdd->info,
543 sizeof(struct watchdog_info)) ? -EFAULT : 0;
545 case WDIOC_GETSTATUS:
546 val = watchdog_get_status(wdd);
547 err = put_user(val, p);
549 case WDIOC_GETBOOTSTATUS:
550 err = put_user(wdd->bootstatus, p);
552 case WDIOC_SETOPTIONS:
553 if (get_user(val, p)) {
557 if (val & WDIOS_DISABLECARD) {
558 err = watchdog_stop(wdd);
562 if (val & WDIOS_ENABLECARD)
563 err = watchdog_start(wdd);
565 case WDIOC_KEEPALIVE:
566 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
570 err = watchdog_ping(wdd);
572 case WDIOC_SETTIMEOUT:
573 if (get_user(val, p)) {
577 err = watchdog_set_timeout(wdd, val);
580 /* If the watchdog is active then we send a keepalive ping
581 * to make sure that the watchdog keep's running (and if
582 * possible that it takes the new timeout) */
583 err = watchdog_ping(wdd);
587 case WDIOC_GETTIMEOUT:
588 /* timeout == 0 means that we don't know the timeout */
589 if (wdd->timeout == 0) {
593 err = put_user(wdd->timeout, p);
595 case WDIOC_GETTIMELEFT:
596 err = watchdog_get_timeleft(wdd, &val);
599 err = put_user(val, p);
607 mutex_unlock(&wd_data->lock);
612 * watchdog_open: open the /dev/watchdog* devices.
613 * @inode: inode of device
614 * @file: file handle to device
616 * When the /dev/watchdog* device gets opened, we start the watchdog.
617 * Watch out: the /dev/watchdog device is single open, so we make sure
618 * it can only be opened once.
621 static int watchdog_open(struct inode *inode, struct file *file)
623 struct watchdog_core_data *wd_data;
624 struct watchdog_device *wdd;
627 /* Get the corresponding watchdog device */
628 if (imajor(inode) == MISC_MAJOR)
629 wd_data = old_wd_data;
631 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
634 /* the watchdog is single open! */
635 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
641 * If the /dev/watchdog device is open, we don't want the module
644 if (!try_module_get(wdd->ops->owner)) {
649 err = watchdog_start(wdd);
653 file->private_data = wd_data;
655 kref_get(&wd_data->kref);
657 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
658 return nonseekable_open(inode, file);
661 module_put(wd_data->wdd->ops->owner);
663 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
667 static void watchdog_core_data_release(struct kref *kref)
669 struct watchdog_core_data *wd_data;
671 wd_data = container_of(kref, struct watchdog_core_data, kref);
677 * watchdog_release: release the watchdog device.
678 * @inode: inode of device
679 * @file: file handle to device
681 * This is the code for when /dev/watchdog gets closed. We will only
682 * stop the watchdog when we have received the magic char (and nowayout
683 * was not set), else the watchdog will keep running.
686 static int watchdog_release(struct inode *inode, struct file *file)
688 struct watchdog_core_data *wd_data = file->private_data;
689 struct watchdog_device *wdd;
692 mutex_lock(&wd_data->lock);
699 * We only stop the watchdog if we received the magic character
700 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
701 * watchdog_stop will fail.
703 if (!test_bit(WDOG_ACTIVE, &wdd->status))
705 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
706 !(wdd->info->options & WDIOF_MAGICCLOSE))
707 err = watchdog_stop(wdd);
709 /* If the watchdog was not stopped, send a keepalive ping */
711 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
715 cancel_delayed_work_sync(&wd_data->work);
717 /* make sure that /dev/watchdog can be re-opened */
718 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
721 mutex_unlock(&wd_data->lock);
722 /* Allow the owner module to be unloaded again */
723 module_put(wd_data->cdev.owner);
724 kref_put(&wd_data->kref, watchdog_core_data_release);
728 static const struct file_operations watchdog_fops = {
729 .owner = THIS_MODULE,
730 .write = watchdog_write,
731 .unlocked_ioctl = watchdog_ioctl,
732 .open = watchdog_open,
733 .release = watchdog_release,
736 static struct miscdevice watchdog_miscdev = {
737 .minor = WATCHDOG_MINOR,
739 .fops = &watchdog_fops,
743 * watchdog_cdev_register: register watchdog character device
744 * @wdd: watchdog device
745 * @devno: character device number
747 * Register a watchdog character device including handling the legacy
748 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
749 * thus we set it up like that.
752 static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
754 struct watchdog_core_data *wd_data;
757 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
760 kref_init(&wd_data->kref);
761 mutex_init(&wd_data->lock);
764 wdd->wd_data = wd_data;
769 INIT_DELAYED_WORK(&wd_data->work, watchdog_ping_work);
772 old_wd_data = wd_data;
773 watchdog_miscdev.parent = wdd->parent;
774 err = misc_register(&watchdog_miscdev);
776 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
777 wdd->info->identity, WATCHDOG_MINOR, err);
779 pr_err("%s: a legacy watchdog module is probably present.\n",
780 wdd->info->identity);
787 /* Fill in the data structures */
788 cdev_init(&wd_data->cdev, &watchdog_fops);
789 wd_data->cdev.owner = wdd->ops->owner;
792 err = cdev_add(&wd_data->cdev, devno, 1);
794 pr_err("watchdog%d unable to add device %d:%d\n",
795 wdd->id, MAJOR(watchdog_devt), wdd->id);
797 misc_deregister(&watchdog_miscdev);
799 kref_put(&wd_data->kref, watchdog_core_data_release);
806 * watchdog_cdev_unregister: unregister watchdog character device
807 * @watchdog: watchdog device
809 * Unregister watchdog character device and if needed the legacy
810 * /dev/watchdog device.
813 static void watchdog_cdev_unregister(struct watchdog_device *wdd)
815 struct watchdog_core_data *wd_data = wdd->wd_data;
817 cdev_del(&wd_data->cdev);
819 misc_deregister(&watchdog_miscdev);
823 mutex_lock(&wd_data->lock);
826 mutex_unlock(&wd_data->lock);
828 cancel_delayed_work_sync(&wd_data->work);
830 kref_put(&wd_data->kref, watchdog_core_data_release);
833 static struct class watchdog_class = {
835 .owner = THIS_MODULE,
836 .dev_groups = wdt_groups,
840 * watchdog_dev_register: register a watchdog device
841 * @wdd: watchdog device
843 * Register a watchdog device including handling the legacy
844 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
845 * thus we set it up like that.
848 int watchdog_dev_register(struct watchdog_device *wdd)
854 devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
856 ret = watchdog_cdev_register(wdd, devno);
860 dev = device_create_with_groups(&watchdog_class, wdd->parent,
861 devno, wdd, wdd->groups,
862 "watchdog%d", wdd->id);
864 watchdog_cdev_unregister(wdd);
872 * watchdog_dev_unregister: unregister a watchdog device
873 * @watchdog: watchdog device
875 * Unregister watchdog device and if needed the legacy
876 * /dev/watchdog device.
879 void watchdog_dev_unregister(struct watchdog_device *wdd)
881 device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
882 watchdog_cdev_unregister(wdd);
886 * watchdog_dev_init: init dev part of watchdog core
888 * Allocate a range of chardev nodes to use for watchdog devices
891 int __init watchdog_dev_init(void)
895 watchdog_wq = alloc_workqueue("watchdogd",
896 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
898 pr_err("Failed to create watchdog workqueue\n");
902 err = class_register(&watchdog_class);
904 pr_err("couldn't register class\n");
908 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
910 pr_err("watchdog: unable to allocate char dev region\n");
911 class_unregister(&watchdog_class);
919 * watchdog_dev_exit: exit dev part of watchdog core
921 * Release the range of chardev nodes used for watchdog devices
924 void __exit watchdog_dev_exit(void)
926 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
927 class_unregister(&watchdog_class);
928 destroy_workqueue(watchdog_wq);