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/kernel.h> /* For printk/panic/... */
40 #include <linux/kref.h> /* For data references */
41 #include <linux/miscdevice.h> /* For handling misc devices */
42 #include <linux/module.h> /* For module stuff/... */
43 #include <linux/mutex.h> /* For mutexes */
44 #include <linux/slab.h> /* For memory functions */
45 #include <linux/types.h> /* For standard types (like size_t) */
46 #include <linux/watchdog.h> /* For watchdog specific items */
47 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
49 #include "watchdog_core.h"
52 * struct watchdog_core_data - watchdog core internal data
53 * @kref: Reference count.
54 * @cdev: The watchdog's Character device.
55 * @wdd: Pointer to watchdog device.
56 * @lock: Lock for watchdog core.
57 * @status: Watchdog core internal status bits.
59 struct watchdog_core_data
{
62 struct watchdog_device
*wdd
;
64 unsigned long status
; /* Internal status bits */
65 #define _WDOG_DEV_OPEN 0 /* Opened ? */
66 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
69 /* the dev_t structure to store the dynamically allocated watchdog devices */
70 static dev_t watchdog_devt
;
71 /* Reference to watchdog device behind /dev/watchdog */
72 static struct watchdog_core_data
*old_wd_data
;
75 * watchdog_ping: ping the watchdog.
76 * @wdd: the watchdog device to ping
78 * The caller must hold wd_data->lock.
80 * If the watchdog has no own ping operation then it needs to be
81 * restarted via the start operation. This wrapper function does
83 * We only ping when the watchdog device is running.
86 static int watchdog_ping(struct watchdog_device
*wdd
)
90 if (!watchdog_active(wdd
))
94 err
= wdd
->ops
->ping(wdd
); /* ping the watchdog */
96 err
= wdd
->ops
->start(wdd
); /* restart watchdog */
102 * watchdog_start: wrapper to start the watchdog.
103 * @wdd: the watchdog device to start
105 * The caller must hold wd_data->lock.
107 * Start the watchdog if it is not active and mark it active.
108 * This function returns zero on success or a negative errno code for
112 static int watchdog_start(struct watchdog_device
*wdd
)
116 if (watchdog_active(wdd
))
119 err
= wdd
->ops
->start(wdd
);
121 set_bit(WDOG_ACTIVE
, &wdd
->status
);
127 * watchdog_stop: wrapper to stop the watchdog.
128 * @wdd: the watchdog device to stop
130 * The caller must hold wd_data->lock.
132 * Stop the watchdog if it is still active and unmark it active.
133 * This function returns zero on success or a negative errno code for
135 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
138 static int watchdog_stop(struct watchdog_device
*wdd
)
142 if (!watchdog_active(wdd
))
145 if (test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
)) {
146 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
151 err
= wdd
->ops
->stop(wdd
);
153 clear_bit(WDOG_ACTIVE
, &wdd
->status
);
159 * watchdog_get_status: wrapper to get the watchdog status
160 * @wdd: the watchdog device to get the status from
162 * The caller must hold wd_data->lock.
164 * Get the watchdog's status flags.
167 static unsigned int watchdog_get_status(struct watchdog_device
*wdd
)
169 if (!wdd
->ops
->status
)
172 return wdd
->ops
->status(wdd
);
176 * watchdog_set_timeout: set the watchdog timer timeout
177 * @wdd: the watchdog device to set the timeout for
178 * @timeout: timeout to set in seconds
180 * The caller must hold wd_data->lock.
183 static int watchdog_set_timeout(struct watchdog_device
*wdd
,
184 unsigned int timeout
)
188 if (!(wdd
->info
->options
& WDIOF_SETTIMEOUT
))
191 if (watchdog_timeout_invalid(wdd
, timeout
))
194 if (wdd
->ops
->set_timeout
)
195 err
= wdd
->ops
->set_timeout(wdd
, timeout
);
197 wdd
->timeout
= timeout
;
203 * watchdog_get_timeleft: wrapper to get the time left before a reboot
204 * @wdd: the watchdog device to get the remaining time from
205 * @timeleft: the time that's left
207 * The caller must hold wd_data->lock.
209 * Get the time before a watchdog will reboot (if not pinged).
212 static int watchdog_get_timeleft(struct watchdog_device
*wdd
,
213 unsigned int *timeleft
)
217 if (!wdd
->ops
->get_timeleft
)
220 *timeleft
= wdd
->ops
->get_timeleft(wdd
);
225 #ifdef CONFIG_WATCHDOG_SYSFS
226 static ssize_t
nowayout_show(struct device
*dev
, struct device_attribute
*attr
,
229 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
231 return sprintf(buf
, "%d\n", !!test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
));
233 static DEVICE_ATTR_RO(nowayout
);
235 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
238 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
239 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
242 mutex_lock(&wd_data
->lock
);
243 status
= watchdog_get_status(wdd
);
244 mutex_unlock(&wd_data
->lock
);
246 return sprintf(buf
, "%u\n", status
);
248 static DEVICE_ATTR_RO(status
);
250 static ssize_t
bootstatus_show(struct device
*dev
,
251 struct device_attribute
*attr
, char *buf
)
253 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
255 return sprintf(buf
, "%u\n", wdd
->bootstatus
);
257 static DEVICE_ATTR_RO(bootstatus
);
259 static ssize_t
timeleft_show(struct device
*dev
, struct device_attribute
*attr
,
262 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
263 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
267 mutex_lock(&wd_data
->lock
);
268 status
= watchdog_get_timeleft(wdd
, &val
);
269 mutex_unlock(&wd_data
->lock
);
271 status
= sprintf(buf
, "%u\n", val
);
275 static DEVICE_ATTR_RO(timeleft
);
277 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
280 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
282 return sprintf(buf
, "%u\n", wdd
->timeout
);
284 static DEVICE_ATTR_RO(timeout
);
286 static ssize_t
identity_show(struct device
*dev
, struct device_attribute
*attr
,
289 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
291 return sprintf(buf
, "%s\n", wdd
->info
->identity
);
293 static DEVICE_ATTR_RO(identity
);
295 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
298 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
300 if (watchdog_active(wdd
))
301 return sprintf(buf
, "active\n");
303 return sprintf(buf
, "inactive\n");
305 static DEVICE_ATTR_RO(state
);
307 static umode_t
wdt_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
310 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
311 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
312 umode_t mode
= attr
->mode
;
314 if (attr
== &dev_attr_status
.attr
&& !wdd
->ops
->status
)
316 else if (attr
== &dev_attr_timeleft
.attr
&& !wdd
->ops
->get_timeleft
)
321 static struct attribute
*wdt_attrs
[] = {
322 &dev_attr_state
.attr
,
323 &dev_attr_identity
.attr
,
324 &dev_attr_timeout
.attr
,
325 &dev_attr_timeleft
.attr
,
326 &dev_attr_bootstatus
.attr
,
327 &dev_attr_status
.attr
,
328 &dev_attr_nowayout
.attr
,
332 static const struct attribute_group wdt_group
= {
334 .is_visible
= wdt_is_visible
,
336 __ATTRIBUTE_GROUPS(wdt
);
338 #define wdt_groups NULL
342 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
343 * @wdd: the watchdog device to do the ioctl on
344 * @cmd: watchdog command
345 * @arg: argument pointer
347 * The caller must hold wd_data->lock.
350 static int watchdog_ioctl_op(struct watchdog_device
*wdd
, unsigned int cmd
,
353 if (!wdd
->ops
->ioctl
)
356 return wdd
->ops
->ioctl(wdd
, cmd
, arg
);
360 * watchdog_write: writes to the watchdog.
361 * @file: file from VFS
362 * @data: user address of data
363 * @len: length of data
364 * @ppos: pointer to the file offset
366 * A write to a watchdog device is defined as a keepalive ping.
367 * Writing the magic 'V' sequence allows the next close to turn
368 * off the watchdog (if 'nowayout' is not set).
371 static ssize_t
watchdog_write(struct file
*file
, const char __user
*data
,
372 size_t len
, loff_t
*ppos
)
374 struct watchdog_core_data
*wd_data
= file
->private_data
;
375 struct watchdog_device
*wdd
;
384 * Note: just in case someone wrote the magic character
387 clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
389 /* scan to see whether or not we got the magic character */
390 for (i
= 0; i
!= len
; i
++) {
391 if (get_user(c
, data
+ i
))
394 set_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
397 /* someone wrote to us, so we send the watchdog a keepalive ping */
400 mutex_lock(&wd_data
->lock
);
403 err
= watchdog_ping(wdd
);
404 mutex_unlock(&wd_data
->lock
);
413 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
414 * @file: file handle to the device
415 * @cmd: watchdog command
416 * @arg: argument pointer
418 * The watchdog API defines a common set of functions for all watchdogs
419 * according to their available features.
422 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
425 struct watchdog_core_data
*wd_data
= file
->private_data
;
426 void __user
*argp
= (void __user
*)arg
;
427 struct watchdog_device
*wdd
;
428 int __user
*p
= argp
;
432 mutex_lock(&wd_data
->lock
);
440 err
= watchdog_ioctl_op(wdd
, cmd
, arg
);
441 if (err
!= -ENOIOCTLCMD
)
445 case WDIOC_GETSUPPORT
:
446 err
= copy_to_user(argp
, wdd
->info
,
447 sizeof(struct watchdog_info
)) ? -EFAULT
: 0;
449 case WDIOC_GETSTATUS
:
450 val
= watchdog_get_status(wdd
);
451 err
= put_user(val
, p
);
453 case WDIOC_GETBOOTSTATUS
:
454 err
= put_user(wdd
->bootstatus
, p
);
456 case WDIOC_SETOPTIONS
:
457 if (get_user(val
, p
)) {
461 if (val
& WDIOS_DISABLECARD
) {
462 err
= watchdog_stop(wdd
);
466 if (val
& WDIOS_ENABLECARD
)
467 err
= watchdog_start(wdd
);
469 case WDIOC_KEEPALIVE
:
470 if (!(wdd
->info
->options
& WDIOF_KEEPALIVEPING
)) {
474 err
= watchdog_ping(wdd
);
476 case WDIOC_SETTIMEOUT
:
477 if (get_user(val
, p
)) {
481 err
= watchdog_set_timeout(wdd
, val
);
484 /* If the watchdog is active then we send a keepalive ping
485 * to make sure that the watchdog keep's running (and if
486 * possible that it takes the new timeout) */
487 err
= watchdog_ping(wdd
);
491 case WDIOC_GETTIMEOUT
:
492 /* timeout == 0 means that we don't know the timeout */
493 if (wdd
->timeout
== 0) {
497 err
= put_user(wdd
->timeout
, p
);
499 case WDIOC_GETTIMELEFT
:
500 err
= watchdog_get_timeleft(wdd
, &val
);
503 err
= put_user(val
, p
);
511 mutex_unlock(&wd_data
->lock
);
516 * watchdog_open: open the /dev/watchdog* devices.
517 * @inode: inode of device
518 * @file: file handle to device
520 * When the /dev/watchdog* device gets opened, we start the watchdog.
521 * Watch out: the /dev/watchdog device is single open, so we make sure
522 * it can only be opened once.
525 static int watchdog_open(struct inode
*inode
, struct file
*file
)
527 struct watchdog_core_data
*wd_data
;
528 struct watchdog_device
*wdd
;
531 /* Get the corresponding watchdog device */
532 if (imajor(inode
) == MISC_MAJOR
)
533 wd_data
= old_wd_data
;
535 wd_data
= container_of(inode
->i_cdev
, struct watchdog_core_data
,
538 /* the watchdog is single open! */
539 if (test_and_set_bit(_WDOG_DEV_OPEN
, &wd_data
->status
))
545 * If the /dev/watchdog device is open, we don't want the module
548 if (!try_module_get(wdd
->ops
->owner
)) {
553 err
= watchdog_start(wdd
);
557 file
->private_data
= wd_data
;
559 kref_get(&wd_data
->kref
);
561 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
562 return nonseekable_open(inode
, file
);
565 module_put(wd_data
->wdd
->ops
->owner
);
567 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
571 static void watchdog_core_data_release(struct kref
*kref
)
573 struct watchdog_core_data
*wd_data
;
575 wd_data
= container_of(kref
, struct watchdog_core_data
, kref
);
581 * watchdog_release: release the watchdog device.
582 * @inode: inode of device
583 * @file: file handle to device
585 * This is the code for when /dev/watchdog gets closed. We will only
586 * stop the watchdog when we have received the magic char (and nowayout
587 * was not set), else the watchdog will keep running.
590 static int watchdog_release(struct inode
*inode
, struct file
*file
)
592 struct watchdog_core_data
*wd_data
= file
->private_data
;
593 struct watchdog_device
*wdd
;
596 mutex_lock(&wd_data
->lock
);
603 * We only stop the watchdog if we received the magic character
604 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
605 * watchdog_stop will fail.
607 if (!test_bit(WDOG_ACTIVE
, &wdd
->status
))
609 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
) ||
610 !(wdd
->info
->options
& WDIOF_MAGICCLOSE
))
611 err
= watchdog_stop(wdd
);
613 /* If the watchdog was not stopped, send a keepalive ping */
615 pr_crit("watchdog%d: watchdog did not stop!\n", wdd
->id
);
619 /* make sure that /dev/watchdog can be re-opened */
620 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
623 mutex_unlock(&wd_data
->lock
);
624 /* Allow the owner module to be unloaded again */
625 module_put(wd_data
->cdev
.owner
);
626 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
630 static const struct file_operations watchdog_fops
= {
631 .owner
= THIS_MODULE
,
632 .write
= watchdog_write
,
633 .unlocked_ioctl
= watchdog_ioctl
,
634 .open
= watchdog_open
,
635 .release
= watchdog_release
,
638 static struct miscdevice watchdog_miscdev
= {
639 .minor
= WATCHDOG_MINOR
,
641 .fops
= &watchdog_fops
,
645 * watchdog_cdev_register: register watchdog character device
646 * @wdd: watchdog device
647 * @devno: character device number
649 * Register a watchdog character device including handling the legacy
650 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
651 * thus we set it up like that.
654 static int watchdog_cdev_register(struct watchdog_device
*wdd
, dev_t devno
)
656 struct watchdog_core_data
*wd_data
;
659 wd_data
= kzalloc(sizeof(struct watchdog_core_data
), GFP_KERNEL
);
662 kref_init(&wd_data
->kref
);
663 mutex_init(&wd_data
->lock
);
666 wdd
->wd_data
= wd_data
;
669 old_wd_data
= wd_data
;
670 watchdog_miscdev
.parent
= wdd
->parent
;
671 err
= misc_register(&watchdog_miscdev
);
673 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
674 wdd
->info
->identity
, WATCHDOG_MINOR
, err
);
676 pr_err("%s: a legacy watchdog module is probably present.\n",
677 wdd
->info
->identity
);
684 /* Fill in the data structures */
685 cdev_init(&wd_data
->cdev
, &watchdog_fops
);
686 wd_data
->cdev
.owner
= wdd
->ops
->owner
;
689 err
= cdev_add(&wd_data
->cdev
, devno
, 1);
691 pr_err("watchdog%d unable to add device %d:%d\n",
692 wdd
->id
, MAJOR(watchdog_devt
), wdd
->id
);
694 misc_deregister(&watchdog_miscdev
);
696 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
703 * watchdog_cdev_unregister: unregister watchdog character device
704 * @watchdog: watchdog device
706 * Unregister watchdog character device and if needed the legacy
707 * /dev/watchdog device.
710 static void watchdog_cdev_unregister(struct watchdog_device
*wdd
)
712 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
714 cdev_del(&wd_data
->cdev
);
716 misc_deregister(&watchdog_miscdev
);
720 mutex_lock(&wd_data
->lock
);
723 mutex_unlock(&wd_data
->lock
);
725 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
728 static struct class watchdog_class
= {
730 .owner
= THIS_MODULE
,
731 .dev_groups
= wdt_groups
,
735 * watchdog_dev_register: register a watchdog device
736 * @wdd: watchdog device
738 * Register a watchdog device including handling the legacy
739 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
740 * thus we set it up like that.
743 int watchdog_dev_register(struct watchdog_device
*wdd
)
749 devno
= MKDEV(MAJOR(watchdog_devt
), wdd
->id
);
751 ret
= watchdog_cdev_register(wdd
, devno
);
755 dev
= device_create_with_groups(&watchdog_class
, wdd
->parent
,
756 devno
, wdd
, wdd
->groups
,
757 "watchdog%d", wdd
->id
);
759 watchdog_cdev_unregister(wdd
);
767 * watchdog_dev_unregister: unregister a watchdog device
768 * @watchdog: watchdog device
770 * Unregister watchdog device and if needed the legacy
771 * /dev/watchdog device.
774 void watchdog_dev_unregister(struct watchdog_device
*wdd
)
776 device_destroy(&watchdog_class
, wdd
->wd_data
->cdev
.dev
);
777 watchdog_cdev_unregister(wdd
);
781 * watchdog_dev_init: init dev part of watchdog core
783 * Allocate a range of chardev nodes to use for watchdog devices
786 int __init
watchdog_dev_init(void)
790 err
= class_register(&watchdog_class
);
792 pr_err("couldn't register class\n");
796 err
= alloc_chrdev_region(&watchdog_devt
, 0, MAX_DOGS
, "watchdog");
798 pr_err("watchdog: unable to allocate char dev region\n");
799 class_unregister(&watchdog_class
);
807 * watchdog_dev_exit: exit dev part of watchdog core
809 * Release the range of chardev nodes used for watchdog devices
812 void __exit
watchdog_dev_exit(void)
814 unregister_chrdev_region(watchdog_devt
, MAX_DOGS
);
815 class_unregister(&watchdog_class
);