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 hm
&& ((watchdog_active(wdd
) && t
> hm
) ||
96 (t
&& !watchdog_active(wdd
) && watchdog_hw_running(wdd
)));
99 static long watchdog_next_keepalive(struct watchdog_device
*wdd
)
101 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
102 unsigned int timeout_ms
= wdd
->timeout
* 1000;
103 unsigned long keepalive_interval
;
104 unsigned long last_heartbeat
;
105 unsigned long virt_timeout
;
106 unsigned int hw_heartbeat_ms
;
108 virt_timeout
= wd_data
->last_keepalive
+ msecs_to_jiffies(timeout_ms
);
109 hw_heartbeat_ms
= min(timeout_ms
, wdd
->max_hw_heartbeat_ms
);
110 keepalive_interval
= msecs_to_jiffies(hw_heartbeat_ms
/ 2);
112 if (!watchdog_active(wdd
))
113 return keepalive_interval
;
116 * To ensure that the watchdog times out wdd->timeout seconds
117 * after the most recent ping from userspace, the last
118 * worker ping has to come in hw_heartbeat_ms before this timeout.
120 last_heartbeat
= virt_timeout
- msecs_to_jiffies(hw_heartbeat_ms
);
121 return min_t(long, last_heartbeat
- jiffies
, keepalive_interval
);
124 static inline void watchdog_update_worker(struct watchdog_device
*wdd
)
126 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
128 if (watchdog_need_worker(wdd
)) {
129 long t
= watchdog_next_keepalive(wdd
);
132 mod_delayed_work(watchdog_wq
, &wd_data
->work
, t
);
134 cancel_delayed_work(&wd_data
->work
);
138 static int __watchdog_ping(struct watchdog_device
*wdd
)
143 err
= wdd
->ops
->ping(wdd
); /* ping the watchdog */
145 err
= wdd
->ops
->start(wdd
); /* restart watchdog */
147 watchdog_update_worker(wdd
);
153 * watchdog_ping: ping the watchdog.
154 * @wdd: the watchdog device to ping
156 * The caller must hold wd_data->lock.
158 * If the watchdog has no own ping operation then it needs to be
159 * restarted via the start operation. This wrapper function does
161 * We only ping when the watchdog device is running.
164 static int watchdog_ping(struct watchdog_device
*wdd
)
166 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
168 if (!watchdog_active(wdd
) && !watchdog_hw_running(wdd
))
171 wd_data
->last_keepalive
= jiffies
;
172 return __watchdog_ping(wdd
);
175 static void watchdog_ping_work(struct work_struct
*work
)
177 struct watchdog_core_data
*wd_data
;
178 struct watchdog_device
*wdd
;
180 wd_data
= container_of(to_delayed_work(work
), struct watchdog_core_data
,
183 mutex_lock(&wd_data
->lock
);
185 if (wdd
&& (watchdog_active(wdd
) || watchdog_hw_running(wdd
)))
186 __watchdog_ping(wdd
);
187 mutex_unlock(&wd_data
->lock
);
191 * watchdog_start: wrapper to start the watchdog.
192 * @wdd: the watchdog device to start
194 * The caller must hold wd_data->lock.
196 * Start the watchdog if it is not active and mark it active.
197 * This function returns zero on success or a negative errno code for
201 static int watchdog_start(struct watchdog_device
*wdd
)
203 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
204 unsigned long started_at
;
207 if (watchdog_active(wdd
))
210 started_at
= jiffies
;
211 if (watchdog_hw_running(wdd
) && wdd
->ops
->ping
)
212 err
= wdd
->ops
->ping(wdd
);
214 err
= wdd
->ops
->start(wdd
);
216 set_bit(WDOG_ACTIVE
, &wdd
->status
);
217 wd_data
->last_keepalive
= started_at
;
218 watchdog_update_worker(wdd
);
225 * watchdog_stop: wrapper to stop the watchdog.
226 * @wdd: the watchdog device to stop
228 * The caller must hold wd_data->lock.
230 * Stop the watchdog if it is still active and unmark it active.
231 * This function returns zero on success or a negative errno code for
233 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
236 static int watchdog_stop(struct watchdog_device
*wdd
)
240 if (!watchdog_active(wdd
))
243 if (test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
)) {
244 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
249 err
= wdd
->ops
->stop(wdd
);
251 clear_bit(WDOG_ACTIVE
, &wdd
->status
);
252 watchdog_update_worker(wdd
);
259 * watchdog_get_status: wrapper to get the watchdog status
260 * @wdd: the watchdog device to get the status from
262 * The caller must hold wd_data->lock.
264 * Get the watchdog's status flags.
267 static unsigned int watchdog_get_status(struct watchdog_device
*wdd
)
269 if (!wdd
->ops
->status
)
272 return wdd
->ops
->status(wdd
);
276 * watchdog_set_timeout: set the watchdog timer timeout
277 * @wdd: the watchdog device to set the timeout for
278 * @timeout: timeout to set in seconds
280 * The caller must hold wd_data->lock.
283 static int watchdog_set_timeout(struct watchdog_device
*wdd
,
284 unsigned int timeout
)
288 if (!(wdd
->info
->options
& WDIOF_SETTIMEOUT
))
291 if (watchdog_timeout_invalid(wdd
, timeout
))
294 if (wdd
->ops
->set_timeout
)
295 err
= wdd
->ops
->set_timeout(wdd
, timeout
);
297 wdd
->timeout
= timeout
;
299 watchdog_update_worker(wdd
);
305 * watchdog_get_timeleft: wrapper to get the time left before a reboot
306 * @wdd: the watchdog device to get the remaining time from
307 * @timeleft: the time that's left
309 * The caller must hold wd_data->lock.
311 * Get the time before a watchdog will reboot (if not pinged).
314 static int watchdog_get_timeleft(struct watchdog_device
*wdd
,
315 unsigned int *timeleft
)
319 if (!wdd
->ops
->get_timeleft
)
322 *timeleft
= wdd
->ops
->get_timeleft(wdd
);
327 #ifdef CONFIG_WATCHDOG_SYSFS
328 static ssize_t
nowayout_show(struct device
*dev
, struct device_attribute
*attr
,
331 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
333 return sprintf(buf
, "%d\n", !!test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
));
335 static DEVICE_ATTR_RO(nowayout
);
337 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
340 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
341 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
344 mutex_lock(&wd_data
->lock
);
345 status
= watchdog_get_status(wdd
);
346 mutex_unlock(&wd_data
->lock
);
348 return sprintf(buf
, "%u\n", status
);
350 static DEVICE_ATTR_RO(status
);
352 static ssize_t
bootstatus_show(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
355 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
357 return sprintf(buf
, "%u\n", wdd
->bootstatus
);
359 static DEVICE_ATTR_RO(bootstatus
);
361 static ssize_t
timeleft_show(struct device
*dev
, struct device_attribute
*attr
,
364 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
365 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
369 mutex_lock(&wd_data
->lock
);
370 status
= watchdog_get_timeleft(wdd
, &val
);
371 mutex_unlock(&wd_data
->lock
);
373 status
= sprintf(buf
, "%u\n", val
);
377 static DEVICE_ATTR_RO(timeleft
);
379 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
382 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
384 return sprintf(buf
, "%u\n", wdd
->timeout
);
386 static DEVICE_ATTR_RO(timeout
);
388 static ssize_t
identity_show(struct device
*dev
, struct device_attribute
*attr
,
391 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
393 return sprintf(buf
, "%s\n", wdd
->info
->identity
);
395 static DEVICE_ATTR_RO(identity
);
397 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
400 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
402 if (watchdog_active(wdd
))
403 return sprintf(buf
, "active\n");
405 return sprintf(buf
, "inactive\n");
407 static DEVICE_ATTR_RO(state
);
409 static umode_t
wdt_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
412 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
413 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
414 umode_t mode
= attr
->mode
;
416 if (attr
== &dev_attr_status
.attr
&& !wdd
->ops
->status
)
418 else if (attr
== &dev_attr_timeleft
.attr
&& !wdd
->ops
->get_timeleft
)
423 static struct attribute
*wdt_attrs
[] = {
424 &dev_attr_state
.attr
,
425 &dev_attr_identity
.attr
,
426 &dev_attr_timeout
.attr
,
427 &dev_attr_timeleft
.attr
,
428 &dev_attr_bootstatus
.attr
,
429 &dev_attr_status
.attr
,
430 &dev_attr_nowayout
.attr
,
434 static const struct attribute_group wdt_group
= {
436 .is_visible
= wdt_is_visible
,
438 __ATTRIBUTE_GROUPS(wdt
);
440 #define wdt_groups NULL
444 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
445 * @wdd: the watchdog device to do the ioctl on
446 * @cmd: watchdog command
447 * @arg: argument pointer
449 * The caller must hold wd_data->lock.
452 static int watchdog_ioctl_op(struct watchdog_device
*wdd
, unsigned int cmd
,
455 if (!wdd
->ops
->ioctl
)
458 return wdd
->ops
->ioctl(wdd
, cmd
, arg
);
462 * watchdog_write: writes to the watchdog.
463 * @file: file from VFS
464 * @data: user address of data
465 * @len: length of data
466 * @ppos: pointer to the file offset
468 * A write to a watchdog device is defined as a keepalive ping.
469 * Writing the magic 'V' sequence allows the next close to turn
470 * off the watchdog (if 'nowayout' is not set).
473 static ssize_t
watchdog_write(struct file
*file
, const char __user
*data
,
474 size_t len
, loff_t
*ppos
)
476 struct watchdog_core_data
*wd_data
= file
->private_data
;
477 struct watchdog_device
*wdd
;
486 * Note: just in case someone wrote the magic character
489 clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
491 /* scan to see whether or not we got the magic character */
492 for (i
= 0; i
!= len
; i
++) {
493 if (get_user(c
, data
+ i
))
496 set_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
499 /* someone wrote to us, so we send the watchdog a keepalive ping */
502 mutex_lock(&wd_data
->lock
);
505 err
= watchdog_ping(wdd
);
506 mutex_unlock(&wd_data
->lock
);
515 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
516 * @file: file handle to the device
517 * @cmd: watchdog command
518 * @arg: argument pointer
520 * The watchdog API defines a common set of functions for all watchdogs
521 * according to their available features.
524 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
527 struct watchdog_core_data
*wd_data
= file
->private_data
;
528 void __user
*argp
= (void __user
*)arg
;
529 struct watchdog_device
*wdd
;
530 int __user
*p
= argp
;
534 mutex_lock(&wd_data
->lock
);
542 err
= watchdog_ioctl_op(wdd
, cmd
, arg
);
543 if (err
!= -ENOIOCTLCMD
)
547 case WDIOC_GETSUPPORT
:
548 err
= copy_to_user(argp
, wdd
->info
,
549 sizeof(struct watchdog_info
)) ? -EFAULT
: 0;
551 case WDIOC_GETSTATUS
:
552 val
= watchdog_get_status(wdd
);
553 err
= put_user(val
, p
);
555 case WDIOC_GETBOOTSTATUS
:
556 err
= put_user(wdd
->bootstatus
, p
);
558 case WDIOC_SETOPTIONS
:
559 if (get_user(val
, p
)) {
563 if (val
& WDIOS_DISABLECARD
) {
564 err
= watchdog_stop(wdd
);
568 if (val
& WDIOS_ENABLECARD
)
569 err
= watchdog_start(wdd
);
571 case WDIOC_KEEPALIVE
:
572 if (!(wdd
->info
->options
& WDIOF_KEEPALIVEPING
)) {
576 err
= watchdog_ping(wdd
);
578 case WDIOC_SETTIMEOUT
:
579 if (get_user(val
, p
)) {
583 err
= watchdog_set_timeout(wdd
, val
);
586 /* If the watchdog is active then we send a keepalive ping
587 * to make sure that the watchdog keep's running (and if
588 * possible that it takes the new timeout) */
589 err
= watchdog_ping(wdd
);
593 case WDIOC_GETTIMEOUT
:
594 /* timeout == 0 means that we don't know the timeout */
595 if (wdd
->timeout
== 0) {
599 err
= put_user(wdd
->timeout
, p
);
601 case WDIOC_GETTIMELEFT
:
602 err
= watchdog_get_timeleft(wdd
, &val
);
605 err
= put_user(val
, p
);
613 mutex_unlock(&wd_data
->lock
);
618 * watchdog_open: open the /dev/watchdog* devices.
619 * @inode: inode of device
620 * @file: file handle to device
622 * When the /dev/watchdog* device gets opened, we start the watchdog.
623 * Watch out: the /dev/watchdog device is single open, so we make sure
624 * it can only be opened once.
627 static int watchdog_open(struct inode
*inode
, struct file
*file
)
629 struct watchdog_core_data
*wd_data
;
630 struct watchdog_device
*wdd
;
633 /* Get the corresponding watchdog device */
634 if (imajor(inode
) == MISC_MAJOR
)
635 wd_data
= old_wd_data
;
637 wd_data
= container_of(inode
->i_cdev
, struct watchdog_core_data
,
640 /* the watchdog is single open! */
641 if (test_and_set_bit(_WDOG_DEV_OPEN
, &wd_data
->status
))
647 * If the /dev/watchdog device is open, we don't want the module
650 if (!watchdog_hw_running(wdd
) && !try_module_get(wdd
->ops
->owner
)) {
655 err
= watchdog_start(wdd
);
659 file
->private_data
= wd_data
;
661 if (!watchdog_hw_running(wdd
))
662 kref_get(&wd_data
->kref
);
664 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
665 return nonseekable_open(inode
, file
);
668 module_put(wd_data
->wdd
->ops
->owner
);
670 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
674 static void watchdog_core_data_release(struct kref
*kref
)
676 struct watchdog_core_data
*wd_data
;
678 wd_data
= container_of(kref
, struct watchdog_core_data
, kref
);
684 * watchdog_release: release the watchdog device.
685 * @inode: inode of device
686 * @file: file handle to device
688 * This is the code for when /dev/watchdog gets closed. We will only
689 * stop the watchdog when we have received the magic char (and nowayout
690 * was not set), else the watchdog will keep running.
693 static int watchdog_release(struct inode
*inode
, struct file
*file
)
695 struct watchdog_core_data
*wd_data
= file
->private_data
;
696 struct watchdog_device
*wdd
;
699 mutex_lock(&wd_data
->lock
);
706 * We only stop the watchdog if we received the magic character
707 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
708 * watchdog_stop will fail.
710 if (!test_bit(WDOG_ACTIVE
, &wdd
->status
))
712 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
) ||
713 !(wdd
->info
->options
& WDIOF_MAGICCLOSE
))
714 err
= watchdog_stop(wdd
);
716 /* If the watchdog was not stopped, send a keepalive ping */
718 pr_crit("watchdog%d: watchdog did not stop!\n", wdd
->id
);
722 cancel_delayed_work_sync(&wd_data
->work
);
723 watchdog_update_worker(wdd
);
725 /* make sure that /dev/watchdog can be re-opened */
726 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
729 mutex_unlock(&wd_data
->lock
);
731 * Allow the owner module to be unloaded again unless the watchdog
732 * is still running. If the watchdog is still running, it can not
733 * be stopped, and its driver must not be unloaded.
735 if (!watchdog_hw_running(wdd
)) {
736 module_put(wdd
->ops
->owner
);
737 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
742 static const struct file_operations watchdog_fops
= {
743 .owner
= THIS_MODULE
,
744 .write
= watchdog_write
,
745 .unlocked_ioctl
= watchdog_ioctl
,
746 .open
= watchdog_open
,
747 .release
= watchdog_release
,
750 static struct miscdevice watchdog_miscdev
= {
751 .minor
= WATCHDOG_MINOR
,
753 .fops
= &watchdog_fops
,
757 * watchdog_cdev_register: register watchdog character device
758 * @wdd: watchdog device
759 * @devno: character device number
761 * Register a watchdog character device including handling the legacy
762 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
763 * thus we set it up like that.
766 static int watchdog_cdev_register(struct watchdog_device
*wdd
, dev_t devno
)
768 struct watchdog_core_data
*wd_data
;
771 wd_data
= kzalloc(sizeof(struct watchdog_core_data
), GFP_KERNEL
);
774 kref_init(&wd_data
->kref
);
775 mutex_init(&wd_data
->lock
);
778 wdd
->wd_data
= wd_data
;
783 INIT_DELAYED_WORK(&wd_data
->work
, watchdog_ping_work
);
786 old_wd_data
= wd_data
;
787 watchdog_miscdev
.parent
= wdd
->parent
;
788 err
= misc_register(&watchdog_miscdev
);
790 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
791 wdd
->info
->identity
, WATCHDOG_MINOR
, err
);
793 pr_err("%s: a legacy watchdog module is probably present.\n",
794 wdd
->info
->identity
);
801 /* Fill in the data structures */
802 cdev_init(&wd_data
->cdev
, &watchdog_fops
);
803 wd_data
->cdev
.owner
= wdd
->ops
->owner
;
806 err
= cdev_add(&wd_data
->cdev
, devno
, 1);
808 pr_err("watchdog%d unable to add device %d:%d\n",
809 wdd
->id
, MAJOR(watchdog_devt
), wdd
->id
);
811 misc_deregister(&watchdog_miscdev
);
813 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
819 * If the watchdog is running, prevent its driver from being unloaded,
820 * and schedule an immediate ping.
822 if (watchdog_hw_running(wdd
)) {
823 __module_get(wdd
->ops
->owner
);
824 kref_get(&wd_data
->kref
);
825 queue_delayed_work(watchdog_wq
, &wd_data
->work
, 0);
832 * watchdog_cdev_unregister: unregister watchdog character device
833 * @watchdog: watchdog device
835 * Unregister watchdog character device and if needed the legacy
836 * /dev/watchdog device.
839 static void watchdog_cdev_unregister(struct watchdog_device
*wdd
)
841 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
843 cdev_del(&wd_data
->cdev
);
845 misc_deregister(&watchdog_miscdev
);
849 mutex_lock(&wd_data
->lock
);
852 mutex_unlock(&wd_data
->lock
);
854 cancel_delayed_work_sync(&wd_data
->work
);
856 kref_put(&wd_data
->kref
, watchdog_core_data_release
);
859 static struct class watchdog_class
= {
861 .owner
= THIS_MODULE
,
862 .dev_groups
= wdt_groups
,
866 * watchdog_dev_register: register a watchdog device
867 * @wdd: watchdog device
869 * Register a watchdog device including handling the legacy
870 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
871 * thus we set it up like that.
874 int watchdog_dev_register(struct watchdog_device
*wdd
)
880 devno
= MKDEV(MAJOR(watchdog_devt
), wdd
->id
);
882 ret
= watchdog_cdev_register(wdd
, devno
);
886 dev
= device_create_with_groups(&watchdog_class
, wdd
->parent
,
887 devno
, wdd
, wdd
->groups
,
888 "watchdog%d", wdd
->id
);
890 watchdog_cdev_unregister(wdd
);
898 * watchdog_dev_unregister: unregister a watchdog device
899 * @watchdog: watchdog device
901 * Unregister watchdog device and if needed the legacy
902 * /dev/watchdog device.
905 void watchdog_dev_unregister(struct watchdog_device
*wdd
)
907 device_destroy(&watchdog_class
, wdd
->wd_data
->cdev
.dev
);
908 watchdog_cdev_unregister(wdd
);
912 * watchdog_dev_init: init dev part of watchdog core
914 * Allocate a range of chardev nodes to use for watchdog devices
917 int __init
watchdog_dev_init(void)
921 watchdog_wq
= alloc_workqueue("watchdogd",
922 WQ_HIGHPRI
| WQ_MEM_RECLAIM
, 0);
924 pr_err("Failed to create watchdog workqueue\n");
928 err
= class_register(&watchdog_class
);
930 pr_err("couldn't register class\n");
934 err
= alloc_chrdev_region(&watchdog_devt
, 0, MAX_DOGS
, "watchdog");
936 pr_err("watchdog: unable to allocate char dev region\n");
937 class_unregister(&watchdog_class
);
945 * watchdog_dev_exit: exit dev part of watchdog core
947 * Release the range of chardev nodes used for watchdog devices
950 void __exit
watchdog_dev_exit(void)
952 unregister_chrdev_region(watchdog_devt
, MAX_DOGS
);
953 class_unregister(&watchdog_class
);
954 destroy_workqueue(watchdog_wq
);