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 unsigned long last_hw_keepalive
;
68 struct delayed_work work
;
69 unsigned long status
; /* Internal status bits */
70 #define _WDOG_DEV_OPEN 0 /* Opened ? */
71 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
74 /* the dev_t structure to store the dynamically allocated watchdog devices */
75 static dev_t watchdog_devt
;
76 /* Reference to watchdog device behind /dev/watchdog */
77 static struct watchdog_core_data
*old_wd_data
;
79 static struct workqueue_struct
*watchdog_wq
;
81 static inline bool watchdog_need_worker(struct watchdog_device
*wdd
)
83 /* All variables in milli-seconds */
84 unsigned int hm
= wdd
->max_hw_heartbeat_ms
;
85 unsigned int t
= wdd
->timeout
* 1000;
88 * A worker to generate heartbeat requests is needed if all of the
89 * following conditions are true.
90 * - Userspace activated the watchdog.
91 * - The driver provided a value for the maximum hardware timeout, and
92 * thus is aware that the framework supports generating heartbeat
94 * - Userspace requests a longer timeout than the hardware can handle.
96 * Alternatively, if userspace has not opened the watchdog
97 * device, we take care of feeding the watchdog if it is
100 return (hm
&& watchdog_active(wdd
) && t
> hm
) ||
101 (t
&& !watchdog_active(wdd
) && watchdog_hw_running(wdd
));
104 static long watchdog_next_keepalive(struct watchdog_device
*wdd
)
106 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
107 unsigned int timeout_ms
= wdd
->timeout
* 1000;
108 unsigned long keepalive_interval
;
109 unsigned long last_heartbeat
;
110 unsigned long virt_timeout
;
111 unsigned int hw_heartbeat_ms
;
113 virt_timeout
= wd_data
->last_keepalive
+ msecs_to_jiffies(timeout_ms
);
114 hw_heartbeat_ms
= min_not_zero(timeout_ms
, wdd
->max_hw_heartbeat_ms
);
115 keepalive_interval
= msecs_to_jiffies(hw_heartbeat_ms
/ 2);
117 if (!watchdog_active(wdd
))
118 return keepalive_interval
;
121 * To ensure that the watchdog times out wdd->timeout seconds
122 * after the most recent ping from userspace, the last
123 * worker ping has to come in hw_heartbeat_ms before this timeout.
125 last_heartbeat
= virt_timeout
- msecs_to_jiffies(hw_heartbeat_ms
);
126 return min_t(long, last_heartbeat
- jiffies
, keepalive_interval
);
129 static inline void watchdog_update_worker(struct watchdog_device
*wdd
)
131 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
133 if (watchdog_need_worker(wdd
)) {
134 long t
= watchdog_next_keepalive(wdd
);
137 mod_delayed_work(watchdog_wq
, &wd_data
->work
, t
);
139 cancel_delayed_work(&wd_data
->work
);
143 static int __watchdog_ping(struct watchdog_device
*wdd
)
145 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
146 unsigned long earliest_keepalive
= wd_data
->last_hw_keepalive
+
147 msecs_to_jiffies(wdd
->min_hw_heartbeat_ms
);
150 if (time_is_after_jiffies(earliest_keepalive
)) {
151 mod_delayed_work(watchdog_wq
, &wd_data
->work
,
152 earliest_keepalive
- jiffies
);
156 wd_data
->last_hw_keepalive
= jiffies
;
159 err
= wdd
->ops
->ping(wdd
); /* ping the watchdog */
161 err
= wdd
->ops
->start(wdd
); /* restart watchdog */
163 watchdog_update_worker(wdd
);
169 * watchdog_ping: ping the watchdog.
170 * @wdd: the watchdog device to ping
172 * The caller must hold wd_data->lock.
174 * If the watchdog has no own ping operation then it needs to be
175 * restarted via the start operation. This wrapper function does
177 * We only ping when the watchdog device is running.
180 static int watchdog_ping(struct watchdog_device
*wdd
)
182 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
184 if (!watchdog_active(wdd
) && !watchdog_hw_running(wdd
))
187 wd_data
->last_keepalive
= jiffies
;
188 return __watchdog_ping(wdd
);
191 static void watchdog_ping_work(struct work_struct
*work
)
193 struct watchdog_core_data
*wd_data
;
194 struct watchdog_device
*wdd
;
196 wd_data
= container_of(to_delayed_work(work
), struct watchdog_core_data
,
199 mutex_lock(&wd_data
->lock
);
201 if (wdd
&& (watchdog_active(wdd
) || watchdog_hw_running(wdd
)))
202 __watchdog_ping(wdd
);
203 mutex_unlock(&wd_data
->lock
);
207 * watchdog_start: wrapper to start the watchdog.
208 * @wdd: the watchdog device to start
210 * The caller must hold wd_data->lock.
212 * Start the watchdog if it is not active and mark it active.
213 * This function returns zero on success or a negative errno code for
217 static int watchdog_start(struct watchdog_device
*wdd
)
219 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
220 unsigned long started_at
;
223 if (watchdog_active(wdd
))
226 started_at
= jiffies
;
227 if (watchdog_hw_running(wdd
) && wdd
->ops
->ping
)
228 err
= wdd
->ops
->ping(wdd
);
230 err
= wdd
->ops
->start(wdd
);
232 set_bit(WDOG_ACTIVE
, &wdd
->status
);
233 wd_data
->last_keepalive
= started_at
;
234 watchdog_update_worker(wdd
);
241 * watchdog_stop: wrapper to stop the watchdog.
242 * @wdd: the watchdog device to stop
244 * The caller must hold wd_data->lock.
246 * Stop the watchdog if it is still active and unmark it active.
247 * This function returns zero on success or a negative errno code for
249 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
252 static int watchdog_stop(struct watchdog_device
*wdd
)
256 if (!watchdog_active(wdd
))
259 if (test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
)) {
260 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
266 err
= wdd
->ops
->stop(wdd
);
268 set_bit(WDOG_HW_RUNNING
, &wdd
->status
);
271 clear_bit(WDOG_ACTIVE
, &wdd
->status
);
272 watchdog_update_worker(wdd
);
279 * watchdog_get_status: wrapper to get the watchdog status
280 * @wdd: the watchdog device to get the status from
282 * The caller must hold wd_data->lock.
284 * Get the watchdog's status flags.
287 static unsigned int watchdog_get_status(struct watchdog_device
*wdd
)
289 if (!wdd
->ops
->status
)
292 return wdd
->ops
->status(wdd
);
296 * watchdog_set_timeout: set the watchdog timer timeout
297 * @wdd: the watchdog device to set the timeout for
298 * @timeout: timeout to set in seconds
300 * The caller must hold wd_data->lock.
303 static int watchdog_set_timeout(struct watchdog_device
*wdd
,
304 unsigned int timeout
)
308 if (!(wdd
->info
->options
& WDIOF_SETTIMEOUT
))
311 if (watchdog_timeout_invalid(wdd
, timeout
))
314 if (wdd
->ops
->set_timeout
)
315 err
= wdd
->ops
->set_timeout(wdd
, timeout
);
317 wdd
->timeout
= timeout
;
319 watchdog_update_worker(wdd
);
325 * watchdog_get_timeleft: wrapper to get the time left before a reboot
326 * @wdd: the watchdog device to get the remaining time from
327 * @timeleft: the time that's left
329 * The caller must hold wd_data->lock.
331 * Get the time before a watchdog will reboot (if not pinged).
334 static int watchdog_get_timeleft(struct watchdog_device
*wdd
,
335 unsigned int *timeleft
)
339 if (!wdd
->ops
->get_timeleft
)
342 *timeleft
= wdd
->ops
->get_timeleft(wdd
);
347 #ifdef CONFIG_WATCHDOG_SYSFS
348 static ssize_t
nowayout_show(struct device
*dev
, struct device_attribute
*attr
,
351 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
353 return sprintf(buf
, "%d\n", !!test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
));
355 static DEVICE_ATTR_RO(nowayout
);
357 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
360 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
361 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
364 mutex_lock(&wd_data
->lock
);
365 status
= watchdog_get_status(wdd
);
366 mutex_unlock(&wd_data
->lock
);
368 return sprintf(buf
, "%u\n", status
);
370 static DEVICE_ATTR_RO(status
);
372 static ssize_t
bootstatus_show(struct device
*dev
,
373 struct device_attribute
*attr
, char *buf
)
375 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
377 return sprintf(buf
, "%u\n", wdd
->bootstatus
);
379 static DEVICE_ATTR_RO(bootstatus
);
381 static ssize_t
timeleft_show(struct device
*dev
, struct device_attribute
*attr
,
384 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
385 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
389 mutex_lock(&wd_data
->lock
);
390 status
= watchdog_get_timeleft(wdd
, &val
);
391 mutex_unlock(&wd_data
->lock
);
393 status
= sprintf(buf
, "%u\n", val
);
397 static DEVICE_ATTR_RO(timeleft
);
399 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
402 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
404 return sprintf(buf
, "%u\n", wdd
->timeout
);
406 static DEVICE_ATTR_RO(timeout
);
408 static ssize_t
identity_show(struct device
*dev
, struct device_attribute
*attr
,
411 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
413 return sprintf(buf
, "%s\n", wdd
->info
->identity
);
415 static DEVICE_ATTR_RO(identity
);
417 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
420 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
422 if (watchdog_active(wdd
))
423 return sprintf(buf
, "active\n");
425 return sprintf(buf
, "inactive\n");
427 static DEVICE_ATTR_RO(state
);
429 static umode_t
wdt_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
432 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
433 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
434 umode_t mode
= attr
->mode
;
436 if (attr
== &dev_attr_status
.attr
&& !wdd
->ops
->status
)
438 else if (attr
== &dev_attr_timeleft
.attr
&& !wdd
->ops
->get_timeleft
)
443 static struct attribute
*wdt_attrs
[] = {
444 &dev_attr_state
.attr
,
445 &dev_attr_identity
.attr
,
446 &dev_attr_timeout
.attr
,
447 &dev_attr_timeleft
.attr
,
448 &dev_attr_bootstatus
.attr
,
449 &dev_attr_status
.attr
,
450 &dev_attr_nowayout
.attr
,
454 static const struct attribute_group wdt_group
= {
456 .is_visible
= wdt_is_visible
,
458 __ATTRIBUTE_GROUPS(wdt
);
460 #define wdt_groups NULL
464 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
465 * @wdd: the watchdog device to do the ioctl on
466 * @cmd: watchdog command
467 * @arg: argument pointer
469 * The caller must hold wd_data->lock.
472 static int watchdog_ioctl_op(struct watchdog_device
*wdd
, unsigned int cmd
,
475 if (!wdd
->ops
->ioctl
)
478 return wdd
->ops
->ioctl(wdd
, cmd
, arg
);
482 * watchdog_write: writes to the watchdog.
483 * @file: file from VFS
484 * @data: user address of data
485 * @len: length of data
486 * @ppos: pointer to the file offset
488 * A write to a watchdog device is defined as a keepalive ping.
489 * Writing the magic 'V' sequence allows the next close to turn
490 * off the watchdog (if 'nowayout' is not set).
493 static ssize_t
watchdog_write(struct file
*file
, const char __user
*data
,
494 size_t len
, loff_t
*ppos
)
496 struct watchdog_core_data
*wd_data
= file
->private_data
;
497 struct watchdog_device
*wdd
;
506 * Note: just in case someone wrote the magic character
509 clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
511 /* scan to see whether or not we got the magic character */
512 for (i
= 0; i
!= len
; i
++) {
513 if (get_user(c
, data
+ i
))
516 set_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
519 /* someone wrote to us, so we send the watchdog a keepalive ping */
522 mutex_lock(&wd_data
->lock
);
525 err
= watchdog_ping(wdd
);
526 mutex_unlock(&wd_data
->lock
);
535 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
536 * @file: file handle to the device
537 * @cmd: watchdog command
538 * @arg: argument pointer
540 * The watchdog API defines a common set of functions for all watchdogs
541 * according to their available features.
544 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
547 struct watchdog_core_data
*wd_data
= file
->private_data
;
548 void __user
*argp
= (void __user
*)arg
;
549 struct watchdog_device
*wdd
;
550 int __user
*p
= argp
;
554 mutex_lock(&wd_data
->lock
);
562 err
= watchdog_ioctl_op(wdd
, cmd
, arg
);
563 if (err
!= -ENOIOCTLCMD
)
567 case WDIOC_GETSUPPORT
:
568 err
= copy_to_user(argp
, wdd
->info
,
569 sizeof(struct watchdog_info
)) ? -EFAULT
: 0;
571 case WDIOC_GETSTATUS
:
572 val
= watchdog_get_status(wdd
);
573 err
= put_user(val
, p
);
575 case WDIOC_GETBOOTSTATUS
:
576 err
= put_user(wdd
->bootstatus
, p
);
578 case WDIOC_SETOPTIONS
:
579 if (get_user(val
, p
)) {
583 if (val
& WDIOS_DISABLECARD
) {
584 err
= watchdog_stop(wdd
);
588 if (val
& WDIOS_ENABLECARD
)
589 err
= watchdog_start(wdd
);
591 case WDIOC_KEEPALIVE
:
592 if (!(wdd
->info
->options
& WDIOF_KEEPALIVEPING
)) {
596 err
= watchdog_ping(wdd
);
598 case WDIOC_SETTIMEOUT
:
599 if (get_user(val
, p
)) {
603 err
= watchdog_set_timeout(wdd
, val
);
606 /* If the watchdog is active then we send a keepalive ping
607 * to make sure that the watchdog keep's running (and if
608 * possible that it takes the new timeout) */
609 err
= watchdog_ping(wdd
);
613 case WDIOC_GETTIMEOUT
:
614 /* timeout == 0 means that we don't know the timeout */
615 if (wdd
->timeout
== 0) {
619 err
= put_user(wdd
->timeout
, p
);
621 case WDIOC_GETTIMELEFT
:
622 err
= watchdog_get_timeleft(wdd
, &val
);
625 err
= put_user(val
, p
);
633 mutex_unlock(&wd_data
->lock
);
638 * watchdog_open: open the /dev/watchdog* devices.
639 * @inode: inode of device
640 * @file: file handle to device
642 * When the /dev/watchdog* device gets opened, we start the watchdog.
643 * Watch out: the /dev/watchdog device is single open, so we make sure
644 * it can only be opened once.
647 static int watchdog_open(struct inode
*inode
, struct file
*file
)
649 struct watchdog_core_data
*wd_data
;
650 struct watchdog_device
*wdd
;
653 /* Get the corresponding watchdog device */
654 if (imajor(inode
) == MISC_MAJOR
)
655 wd_data
= old_wd_data
;
657 wd_data
= container_of(inode
->i_cdev
, struct watchdog_core_data
,
660 /* the watchdog is single open! */
661 if (test_and_set_bit(_WDOG_DEV_OPEN
, &wd_data
->status
))
667 * If the /dev/watchdog device is open, we don't want the module
670 if (!watchdog_hw_running(wdd
) && !try_module_get(wdd
->ops
->owner
)) {
675 err
= watchdog_start(wdd
);
679 file
->private_data
= wd_data
;
681 if (!watchdog_hw_running(wdd
))
682 kref_get(&wd_data
->kref
);
684 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
685 return nonseekable_open(inode
, file
);
688 module_put(wd_data
->wdd
->ops
->owner
);
690 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
694 static void watchdog_core_data_release(struct kref
*kref
)
696 struct watchdog_core_data
*wd_data
;
698 wd_data
= container_of(kref
, struct watchdog_core_data
, kref
);
704 * watchdog_release: release the watchdog device.
705 * @inode: inode of device
706 * @file: file handle to device
708 * This is the code for when /dev/watchdog gets closed. We will only
709 * stop the watchdog when we have received the magic char (and nowayout
710 * was not set), else the watchdog will keep running.
713 static int watchdog_release(struct inode
*inode
, struct file
*file
)
715 struct watchdog_core_data
*wd_data
= file
->private_data
;
716 struct watchdog_device
*wdd
;
720 mutex_lock(&wd_data
->lock
);
727 * We only stop the watchdog if we received the magic character
728 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
729 * watchdog_stop will fail.
731 if (!test_bit(WDOG_ACTIVE
, &wdd
->status
))
733 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
) ||
734 !(wdd
->info
->options
& WDIOF_MAGICCLOSE
))
735 err
= watchdog_stop(wdd
);
737 /* If the watchdog was not stopped, send a keepalive ping */
739 pr_crit("watchdog%d: watchdog did not stop!\n", wdd
->id
);
743 watchdog_update_worker(wdd
);
745 /* make sure that /dev/watchdog can be re-opened */
746 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
749 running
= wdd
&& watchdog_hw_running(wdd
);
750 mutex_unlock(&wd_data
->lock
);
752 * Allow the owner module to be unloaded again unless the watchdog
753 * is still running. If the watchdog is still running, it can not
754 * be stopped, and its driver must not be unloaded.
757 module_put(wd_data
->cdev
.owner
);
758 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
763 static const struct file_operations watchdog_fops
= {
764 .owner
= THIS_MODULE
,
765 .write
= watchdog_write
,
766 .unlocked_ioctl
= watchdog_ioctl
,
767 .open
= watchdog_open
,
768 .release
= watchdog_release
,
771 static struct miscdevice watchdog_miscdev
= {
772 .minor
= WATCHDOG_MINOR
,
774 .fops
= &watchdog_fops
,
778 * watchdog_cdev_register: register watchdog character device
779 * @wdd: watchdog device
780 * @devno: character device number
782 * Register a watchdog character device including handling the legacy
783 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
784 * thus we set it up like that.
787 static int watchdog_cdev_register(struct watchdog_device
*wdd
, dev_t devno
)
789 struct watchdog_core_data
*wd_data
;
792 wd_data
= kzalloc(sizeof(struct watchdog_core_data
), GFP_KERNEL
);
795 kref_init(&wd_data
->kref
);
796 mutex_init(&wd_data
->lock
);
799 wdd
->wd_data
= wd_data
;
804 INIT_DELAYED_WORK(&wd_data
->work
, watchdog_ping_work
);
807 old_wd_data
= wd_data
;
808 watchdog_miscdev
.parent
= wdd
->parent
;
809 err
= misc_register(&watchdog_miscdev
);
811 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
812 wdd
->info
->identity
, WATCHDOG_MINOR
, err
);
814 pr_err("%s: a legacy watchdog module is probably present.\n",
815 wdd
->info
->identity
);
822 /* Fill in the data structures */
823 cdev_init(&wd_data
->cdev
, &watchdog_fops
);
824 wd_data
->cdev
.owner
= wdd
->ops
->owner
;
827 err
= cdev_add(&wd_data
->cdev
, devno
, 1);
829 pr_err("watchdog%d unable to add device %d:%d\n",
830 wdd
->id
, MAJOR(watchdog_devt
), wdd
->id
);
832 misc_deregister(&watchdog_miscdev
);
834 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
839 /* Record time of most recent heartbeat as 'just before now'. */
840 wd_data
->last_hw_keepalive
= jiffies
- 1;
843 * If the watchdog is running, prevent its driver from being unloaded,
844 * and schedule an immediate ping.
846 if (watchdog_hw_running(wdd
)) {
847 __module_get(wdd
->ops
->owner
);
848 kref_get(&wd_data
->kref
);
849 queue_delayed_work(watchdog_wq
, &wd_data
->work
, 0);
856 * watchdog_cdev_unregister: unregister watchdog character device
857 * @watchdog: watchdog device
859 * Unregister watchdog character device and if needed the legacy
860 * /dev/watchdog device.
863 static void watchdog_cdev_unregister(struct watchdog_device
*wdd
)
865 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
867 cdev_del(&wd_data
->cdev
);
869 misc_deregister(&watchdog_miscdev
);
873 mutex_lock(&wd_data
->lock
);
876 mutex_unlock(&wd_data
->lock
);
878 cancel_delayed_work_sync(&wd_data
->work
);
880 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
883 static struct class watchdog_class
= {
885 .owner
= THIS_MODULE
,
886 .dev_groups
= wdt_groups
,
890 * watchdog_dev_register: register a watchdog device
891 * @wdd: watchdog device
893 * Register a watchdog device including handling the legacy
894 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
895 * thus we set it up like that.
898 int watchdog_dev_register(struct watchdog_device
*wdd
)
904 devno
= MKDEV(MAJOR(watchdog_devt
), wdd
->id
);
906 ret
= watchdog_cdev_register(wdd
, devno
);
910 dev
= device_create_with_groups(&watchdog_class
, wdd
->parent
,
911 devno
, wdd
, wdd
->groups
,
912 "watchdog%d", wdd
->id
);
914 watchdog_cdev_unregister(wdd
);
922 * watchdog_dev_unregister: unregister a watchdog device
923 * @watchdog: watchdog device
925 * Unregister watchdog device and if needed the legacy
926 * /dev/watchdog device.
929 void watchdog_dev_unregister(struct watchdog_device
*wdd
)
931 device_destroy(&watchdog_class
, wdd
->wd_data
->cdev
.dev
);
932 watchdog_cdev_unregister(wdd
);
936 * watchdog_dev_init: init dev part of watchdog core
938 * Allocate a range of chardev nodes to use for watchdog devices
941 int __init
watchdog_dev_init(void)
945 watchdog_wq
= alloc_workqueue("watchdogd",
946 WQ_HIGHPRI
| WQ_MEM_RECLAIM
, 0);
948 pr_err("Failed to create watchdog workqueue\n");
952 err
= class_register(&watchdog_class
);
954 pr_err("couldn't register class\n");
958 err
= alloc_chrdev_region(&watchdog_devt
, 0, MAX_DOGS
, "watchdog");
960 pr_err("watchdog: unable to allocate char dev region\n");
961 class_unregister(&watchdog_class
);
969 * watchdog_dev_exit: exit dev part of watchdog core
971 * Release the range of chardev nodes used for watchdog devices
974 void __exit
watchdog_dev_exit(void)
976 unregister_chrdev_region(watchdog_devt
, MAX_DOGS
);
977 class_unregister(&watchdog_class
);
978 destroy_workqueue(watchdog_wq
);