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/hrtimer.h> /* For hrtimers */
40 #include <linux/kernel.h> /* For printk/panic/... */
41 #include <linux/kthread.h> /* For kthread_work */
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/uaccess.h> /* For copy_to_user/put_user/... */
50 #include <uapi/linux/sched/types.h> /* For struct sched_param */
52 #include "watchdog_core.h"
53 #include "watchdog_pretimeout.h"
56 * struct watchdog_core_data - watchdog core internal data
57 * @dev: The watchdog's internal device
58 * @cdev: The watchdog's Character device.
59 * @wdd: Pointer to watchdog device.
60 * @lock: Lock for watchdog core.
61 * @status: Watchdog core internal status bits.
63 struct watchdog_core_data
{
66 struct watchdog_device
*wdd
;
68 ktime_t last_keepalive
;
69 ktime_t last_hw_keepalive
;
71 struct kthread_work work
;
72 unsigned long status
; /* Internal status bits */
73 #define _WDOG_DEV_OPEN 0 /* Opened ? */
74 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
75 #define _WDOG_KEEPALIVE 2 /* Did we receive a keepalive ? */
78 /* the dev_t structure to store the dynamically allocated watchdog devices */
79 static dev_t watchdog_devt
;
80 /* Reference to watchdog device behind /dev/watchdog */
81 static struct watchdog_core_data
*old_wd_data
;
83 static struct kthread_worker
*watchdog_kworker
;
85 static bool handle_boot_enabled
=
86 IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED
);
88 static inline bool watchdog_need_worker(struct watchdog_device
*wdd
)
90 /* All variables in milli-seconds */
91 unsigned int hm
= wdd
->max_hw_heartbeat_ms
;
92 unsigned int t
= wdd
->timeout
* 1000;
95 * A worker to generate heartbeat requests is needed if all of the
96 * following conditions are true.
97 * - Userspace activated the watchdog.
98 * - The driver provided a value for the maximum hardware timeout, and
99 * thus is aware that the framework supports generating heartbeat
101 * - Userspace requests a longer timeout than the hardware can handle.
103 * Alternatively, if userspace has not opened the watchdog
104 * device, we take care of feeding the watchdog if it is
107 return (hm
&& watchdog_active(wdd
) && t
> hm
) ||
108 (t
&& !watchdog_active(wdd
) && watchdog_hw_running(wdd
));
111 static ktime_t
watchdog_next_keepalive(struct watchdog_device
*wdd
)
113 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
114 unsigned int timeout_ms
= wdd
->timeout
* 1000;
115 ktime_t keepalive_interval
;
116 ktime_t last_heartbeat
, latest_heartbeat
;
117 ktime_t virt_timeout
;
118 unsigned int hw_heartbeat_ms
;
120 virt_timeout
= ktime_add(wd_data
->last_keepalive
,
121 ms_to_ktime(timeout_ms
));
122 hw_heartbeat_ms
= min_not_zero(timeout_ms
, wdd
->max_hw_heartbeat_ms
);
123 keepalive_interval
= ms_to_ktime(hw_heartbeat_ms
/ 2);
125 if (!watchdog_active(wdd
))
126 return keepalive_interval
;
129 * To ensure that the watchdog times out wdd->timeout seconds
130 * after the most recent ping from userspace, the last
131 * worker ping has to come in hw_heartbeat_ms before this timeout.
133 last_heartbeat
= ktime_sub(virt_timeout
, ms_to_ktime(hw_heartbeat_ms
));
134 latest_heartbeat
= ktime_sub(last_heartbeat
, ktime_get());
135 if (ktime_before(latest_heartbeat
, keepalive_interval
))
136 return latest_heartbeat
;
137 return keepalive_interval
;
140 static inline void watchdog_update_worker(struct watchdog_device
*wdd
)
142 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
144 if (watchdog_need_worker(wdd
)) {
145 ktime_t t
= watchdog_next_keepalive(wdd
);
148 hrtimer_start(&wd_data
->timer
, t
, HRTIMER_MODE_REL
);
150 hrtimer_cancel(&wd_data
->timer
);
154 static int __watchdog_ping(struct watchdog_device
*wdd
)
156 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
157 ktime_t earliest_keepalive
, now
;
160 earliest_keepalive
= ktime_add(wd_data
->last_hw_keepalive
,
161 ms_to_ktime(wdd
->min_hw_heartbeat_ms
));
164 if (ktime_after(earliest_keepalive
, now
)) {
165 hrtimer_start(&wd_data
->timer
,
166 ktime_sub(earliest_keepalive
, now
),
171 wd_data
->last_hw_keepalive
= now
;
174 err
= wdd
->ops
->ping(wdd
); /* ping the watchdog */
176 err
= wdd
->ops
->start(wdd
); /* restart watchdog */
178 watchdog_update_worker(wdd
);
184 * watchdog_ping: ping the watchdog.
185 * @wdd: the watchdog device to ping
187 * The caller must hold wd_data->lock.
189 * If the watchdog has no own ping operation then it needs to be
190 * restarted via the start operation. This wrapper function does
192 * We only ping when the watchdog device is running.
195 static int watchdog_ping(struct watchdog_device
*wdd
)
197 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
199 if (!watchdog_active(wdd
) && !watchdog_hw_running(wdd
))
202 set_bit(_WDOG_KEEPALIVE
, &wd_data
->status
);
204 wd_data
->last_keepalive
= ktime_get();
205 return __watchdog_ping(wdd
);
208 static bool watchdog_worker_should_ping(struct watchdog_core_data
*wd_data
)
210 struct watchdog_device
*wdd
= wd_data
->wdd
;
212 return wdd
&& (watchdog_active(wdd
) || watchdog_hw_running(wdd
));
215 static void watchdog_ping_work(struct kthread_work
*work
)
217 struct watchdog_core_data
*wd_data
;
219 wd_data
= container_of(work
, struct watchdog_core_data
, work
);
221 mutex_lock(&wd_data
->lock
);
222 if (watchdog_worker_should_ping(wd_data
))
223 __watchdog_ping(wd_data
->wdd
);
224 mutex_unlock(&wd_data
->lock
);
227 static enum hrtimer_restart
watchdog_timer_expired(struct hrtimer
*timer
)
229 struct watchdog_core_data
*wd_data
;
231 wd_data
= container_of(timer
, struct watchdog_core_data
, timer
);
233 kthread_queue_work(watchdog_kworker
, &wd_data
->work
);
234 return HRTIMER_NORESTART
;
238 * watchdog_start: wrapper to start the watchdog.
239 * @wdd: the watchdog device to start
241 * The caller must hold wd_data->lock.
243 * Start the watchdog if it is not active and mark it active.
244 * This function returns zero on success or a negative errno code for
248 static int watchdog_start(struct watchdog_device
*wdd
)
250 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
254 if (watchdog_active(wdd
))
257 set_bit(_WDOG_KEEPALIVE
, &wd_data
->status
);
259 started_at
= ktime_get();
260 if (watchdog_hw_running(wdd
) && wdd
->ops
->ping
)
261 err
= wdd
->ops
->ping(wdd
);
263 err
= wdd
->ops
->start(wdd
);
265 set_bit(WDOG_ACTIVE
, &wdd
->status
);
266 wd_data
->last_keepalive
= started_at
;
267 wd_data
->last_hw_keepalive
= started_at
;
268 watchdog_update_worker(wdd
);
275 * watchdog_stop: wrapper to stop the watchdog.
276 * @wdd: the watchdog device to stop
278 * The caller must hold wd_data->lock.
280 * Stop the watchdog if it is still active and unmark it active.
281 * This function returns zero on success or a negative errno code for
283 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
286 static int watchdog_stop(struct watchdog_device
*wdd
)
290 if (!watchdog_active(wdd
))
293 if (test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
)) {
294 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
299 if (wdd
->ops
->stop
) {
300 clear_bit(WDOG_HW_RUNNING
, &wdd
->status
);
301 err
= wdd
->ops
->stop(wdd
);
303 set_bit(WDOG_HW_RUNNING
, &wdd
->status
);
307 clear_bit(WDOG_ACTIVE
, &wdd
->status
);
308 watchdog_update_worker(wdd
);
315 * watchdog_get_status: wrapper to get the watchdog status
316 * @wdd: the watchdog device to get the status from
318 * The caller must hold wd_data->lock.
320 * Get the watchdog's status flags.
323 static unsigned int watchdog_get_status(struct watchdog_device
*wdd
)
325 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
328 if (wdd
->ops
->status
)
329 status
= wdd
->ops
->status(wdd
);
331 status
= wdd
->bootstatus
& (WDIOF_CARDRESET
|
339 if (test_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
))
340 status
|= WDIOF_MAGICCLOSE
;
342 if (test_and_clear_bit(_WDOG_KEEPALIVE
, &wd_data
->status
))
343 status
|= WDIOF_KEEPALIVEPING
;
349 * watchdog_set_timeout: set the watchdog timer timeout
350 * @wdd: the watchdog device to set the timeout for
351 * @timeout: timeout to set in seconds
353 * The caller must hold wd_data->lock.
356 static int watchdog_set_timeout(struct watchdog_device
*wdd
,
357 unsigned int timeout
)
361 if (!(wdd
->info
->options
& WDIOF_SETTIMEOUT
))
364 if (watchdog_timeout_invalid(wdd
, timeout
))
367 if (wdd
->ops
->set_timeout
) {
368 err
= wdd
->ops
->set_timeout(wdd
, timeout
);
370 wdd
->timeout
= timeout
;
371 /* Disable pretimeout if it doesn't fit the new timeout */
372 if (wdd
->pretimeout
>= wdd
->timeout
)
376 watchdog_update_worker(wdd
);
382 * watchdog_set_pretimeout: set the watchdog timer pretimeout
383 * @wdd: the watchdog device to set the timeout for
384 * @timeout: pretimeout to set in seconds
387 static int watchdog_set_pretimeout(struct watchdog_device
*wdd
,
388 unsigned int timeout
)
392 if (!(wdd
->info
->options
& WDIOF_PRETIMEOUT
))
395 if (watchdog_pretimeout_invalid(wdd
, timeout
))
398 if (wdd
->ops
->set_pretimeout
)
399 err
= wdd
->ops
->set_pretimeout(wdd
, timeout
);
401 wdd
->pretimeout
= timeout
;
407 * watchdog_get_timeleft: wrapper to get the time left before a reboot
408 * @wdd: the watchdog device to get the remaining time from
409 * @timeleft: the time that's left
411 * The caller must hold wd_data->lock.
413 * Get the time before a watchdog will reboot (if not pinged).
416 static int watchdog_get_timeleft(struct watchdog_device
*wdd
,
417 unsigned int *timeleft
)
421 if (!wdd
->ops
->get_timeleft
)
424 *timeleft
= wdd
->ops
->get_timeleft(wdd
);
429 #ifdef CONFIG_WATCHDOG_SYSFS
430 static ssize_t
nowayout_show(struct device
*dev
, struct device_attribute
*attr
,
433 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
435 return sprintf(buf
, "%d\n", !!test_bit(WDOG_NO_WAY_OUT
, &wdd
->status
));
437 static DEVICE_ATTR_RO(nowayout
);
439 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
442 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
443 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
446 mutex_lock(&wd_data
->lock
);
447 status
= watchdog_get_status(wdd
);
448 mutex_unlock(&wd_data
->lock
);
450 return sprintf(buf
, "0x%x\n", status
);
452 static DEVICE_ATTR_RO(status
);
454 static ssize_t
bootstatus_show(struct device
*dev
,
455 struct device_attribute
*attr
, char *buf
)
457 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
459 return sprintf(buf
, "%u\n", wdd
->bootstatus
);
461 static DEVICE_ATTR_RO(bootstatus
);
463 static ssize_t
timeleft_show(struct device
*dev
, struct device_attribute
*attr
,
466 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
467 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
471 mutex_lock(&wd_data
->lock
);
472 status
= watchdog_get_timeleft(wdd
, &val
);
473 mutex_unlock(&wd_data
->lock
);
475 status
= sprintf(buf
, "%u\n", val
);
479 static DEVICE_ATTR_RO(timeleft
);
481 static ssize_t
timeout_show(struct device
*dev
, struct device_attribute
*attr
,
484 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
486 return sprintf(buf
, "%u\n", wdd
->timeout
);
488 static DEVICE_ATTR_RO(timeout
);
490 static ssize_t
pretimeout_show(struct device
*dev
,
491 struct device_attribute
*attr
, char *buf
)
493 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
495 return sprintf(buf
, "%u\n", wdd
->pretimeout
);
497 static DEVICE_ATTR_RO(pretimeout
);
499 static ssize_t
identity_show(struct device
*dev
, struct device_attribute
*attr
,
502 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
504 return sprintf(buf
, "%s\n", wdd
->info
->identity
);
506 static DEVICE_ATTR_RO(identity
);
508 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
511 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
513 if (watchdog_active(wdd
))
514 return sprintf(buf
, "active\n");
516 return sprintf(buf
, "inactive\n");
518 static DEVICE_ATTR_RO(state
);
520 static ssize_t
pretimeout_available_governors_show(struct device
*dev
,
521 struct device_attribute
*attr
, char *buf
)
523 return watchdog_pretimeout_available_governors_get(buf
);
525 static DEVICE_ATTR_RO(pretimeout_available_governors
);
527 static ssize_t
pretimeout_governor_show(struct device
*dev
,
528 struct device_attribute
*attr
,
531 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
533 return watchdog_pretimeout_governor_get(wdd
, buf
);
536 static ssize_t
pretimeout_governor_store(struct device
*dev
,
537 struct device_attribute
*attr
,
538 const char *buf
, size_t count
)
540 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
541 int ret
= watchdog_pretimeout_governor_set(wdd
, buf
);
548 static DEVICE_ATTR_RW(pretimeout_governor
);
550 static umode_t
wdt_is_visible(struct kobject
*kobj
, struct attribute
*attr
,
553 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
554 struct watchdog_device
*wdd
= dev_get_drvdata(dev
);
555 umode_t mode
= attr
->mode
;
557 if (attr
== &dev_attr_timeleft
.attr
&& !wdd
->ops
->get_timeleft
)
559 else if (attr
== &dev_attr_pretimeout
.attr
&&
560 !(wdd
->info
->options
& WDIOF_PRETIMEOUT
))
562 else if ((attr
== &dev_attr_pretimeout_governor
.attr
||
563 attr
== &dev_attr_pretimeout_available_governors
.attr
) &&
564 (!(wdd
->info
->options
& WDIOF_PRETIMEOUT
) ||
565 !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV
)))
570 static struct attribute
*wdt_attrs
[] = {
571 &dev_attr_state
.attr
,
572 &dev_attr_identity
.attr
,
573 &dev_attr_timeout
.attr
,
574 &dev_attr_pretimeout
.attr
,
575 &dev_attr_timeleft
.attr
,
576 &dev_attr_bootstatus
.attr
,
577 &dev_attr_status
.attr
,
578 &dev_attr_nowayout
.attr
,
579 &dev_attr_pretimeout_governor
.attr
,
580 &dev_attr_pretimeout_available_governors
.attr
,
584 static const struct attribute_group wdt_group
= {
586 .is_visible
= wdt_is_visible
,
588 __ATTRIBUTE_GROUPS(wdt
);
590 #define wdt_groups NULL
594 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
595 * @wdd: the watchdog device to do the ioctl on
596 * @cmd: watchdog command
597 * @arg: argument pointer
599 * The caller must hold wd_data->lock.
602 static int watchdog_ioctl_op(struct watchdog_device
*wdd
, unsigned int cmd
,
605 if (!wdd
->ops
->ioctl
)
608 return wdd
->ops
->ioctl(wdd
, cmd
, arg
);
612 * watchdog_write: writes to the watchdog.
613 * @file: file from VFS
614 * @data: user address of data
615 * @len: length of data
616 * @ppos: pointer to the file offset
618 * A write to a watchdog device is defined as a keepalive ping.
619 * Writing the magic 'V' sequence allows the next close to turn
620 * off the watchdog (if 'nowayout' is not set).
623 static ssize_t
watchdog_write(struct file
*file
, const char __user
*data
,
624 size_t len
, loff_t
*ppos
)
626 struct watchdog_core_data
*wd_data
= file
->private_data
;
627 struct watchdog_device
*wdd
;
636 * Note: just in case someone wrote the magic character
639 clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
641 /* scan to see whether or not we got the magic character */
642 for (i
= 0; i
!= len
; i
++) {
643 if (get_user(c
, data
+ i
))
646 set_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
);
649 /* someone wrote to us, so we send the watchdog a keepalive ping */
652 mutex_lock(&wd_data
->lock
);
655 err
= watchdog_ping(wdd
);
656 mutex_unlock(&wd_data
->lock
);
665 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
666 * @file: file handle to the device
667 * @cmd: watchdog command
668 * @arg: argument pointer
670 * The watchdog API defines a common set of functions for all watchdogs
671 * according to their available features.
674 static long watchdog_ioctl(struct file
*file
, unsigned int cmd
,
677 struct watchdog_core_data
*wd_data
= file
->private_data
;
678 void __user
*argp
= (void __user
*)arg
;
679 struct watchdog_device
*wdd
;
680 int __user
*p
= argp
;
684 mutex_lock(&wd_data
->lock
);
692 err
= watchdog_ioctl_op(wdd
, cmd
, arg
);
693 if (err
!= -ENOIOCTLCMD
)
697 case WDIOC_GETSUPPORT
:
698 err
= copy_to_user(argp
, wdd
->info
,
699 sizeof(struct watchdog_info
)) ? -EFAULT
: 0;
701 case WDIOC_GETSTATUS
:
702 val
= watchdog_get_status(wdd
);
703 err
= put_user(val
, p
);
705 case WDIOC_GETBOOTSTATUS
:
706 err
= put_user(wdd
->bootstatus
, p
);
708 case WDIOC_SETOPTIONS
:
709 if (get_user(val
, p
)) {
713 if (val
& WDIOS_DISABLECARD
) {
714 err
= watchdog_stop(wdd
);
718 if (val
& WDIOS_ENABLECARD
)
719 err
= watchdog_start(wdd
);
721 case WDIOC_KEEPALIVE
:
722 if (!(wdd
->info
->options
& WDIOF_KEEPALIVEPING
)) {
726 err
= watchdog_ping(wdd
);
728 case WDIOC_SETTIMEOUT
:
729 if (get_user(val
, p
)) {
733 err
= watchdog_set_timeout(wdd
, val
);
736 /* If the watchdog is active then we send a keepalive ping
737 * to make sure that the watchdog keep's running (and if
738 * possible that it takes the new timeout) */
739 err
= watchdog_ping(wdd
);
743 case WDIOC_GETTIMEOUT
:
744 /* timeout == 0 means that we don't know the timeout */
745 if (wdd
->timeout
== 0) {
749 err
= put_user(wdd
->timeout
, p
);
751 case WDIOC_GETTIMELEFT
:
752 err
= watchdog_get_timeleft(wdd
, &val
);
755 err
= put_user(val
, p
);
757 case WDIOC_SETPRETIMEOUT
:
758 if (get_user(val
, p
)) {
762 err
= watchdog_set_pretimeout(wdd
, val
);
764 case WDIOC_GETPRETIMEOUT
:
765 err
= put_user(wdd
->pretimeout
, p
);
773 mutex_unlock(&wd_data
->lock
);
778 * watchdog_open: open the /dev/watchdog* devices.
779 * @inode: inode of device
780 * @file: file handle to device
782 * When the /dev/watchdog* device gets opened, we start the watchdog.
783 * Watch out: the /dev/watchdog device is single open, so we make sure
784 * it can only be opened once.
787 static int watchdog_open(struct inode
*inode
, struct file
*file
)
789 struct watchdog_core_data
*wd_data
;
790 struct watchdog_device
*wdd
;
794 /* Get the corresponding watchdog device */
795 if (imajor(inode
) == MISC_MAJOR
)
796 wd_data
= old_wd_data
;
798 wd_data
= container_of(inode
->i_cdev
, struct watchdog_core_data
,
801 /* the watchdog is single open! */
802 if (test_and_set_bit(_WDOG_DEV_OPEN
, &wd_data
->status
))
808 * If the /dev/watchdog device is open, we don't want the module
811 hw_running
= watchdog_hw_running(wdd
);
812 if (!hw_running
&& !try_module_get(wdd
->ops
->owner
)) {
817 err
= watchdog_start(wdd
);
821 file
->private_data
= wd_data
;
824 get_device(&wd_data
->dev
);
826 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
827 return nonseekable_open(inode
, file
);
830 module_put(wd_data
->wdd
->ops
->owner
);
832 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
836 static void watchdog_core_data_release(struct device
*dev
)
838 struct watchdog_core_data
*wd_data
;
840 wd_data
= container_of(dev
, struct watchdog_core_data
, dev
);
846 * watchdog_release: release the watchdog device.
847 * @inode: inode of device
848 * @file: file handle to device
850 * This is the code for when /dev/watchdog gets closed. We will only
851 * stop the watchdog when we have received the magic char (and nowayout
852 * was not set), else the watchdog will keep running.
855 static int watchdog_release(struct inode
*inode
, struct file
*file
)
857 struct watchdog_core_data
*wd_data
= file
->private_data
;
858 struct watchdog_device
*wdd
;
862 mutex_lock(&wd_data
->lock
);
869 * We only stop the watchdog if we received the magic character
870 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
871 * watchdog_stop will fail.
873 if (!test_bit(WDOG_ACTIVE
, &wdd
->status
))
875 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE
, &wd_data
->status
) ||
876 !(wdd
->info
->options
& WDIOF_MAGICCLOSE
))
877 err
= watchdog_stop(wdd
);
879 /* If the watchdog was not stopped, send a keepalive ping */
881 pr_crit("watchdog%d: watchdog did not stop!\n", wdd
->id
);
885 watchdog_update_worker(wdd
);
887 /* make sure that /dev/watchdog can be re-opened */
888 clear_bit(_WDOG_DEV_OPEN
, &wd_data
->status
);
891 running
= wdd
&& watchdog_hw_running(wdd
);
892 mutex_unlock(&wd_data
->lock
);
894 * Allow the owner module to be unloaded again unless the watchdog
895 * is still running. If the watchdog is still running, it can not
896 * be stopped, and its driver must not be unloaded.
899 module_put(wd_data
->cdev
.owner
);
900 put_device(&wd_data
->dev
);
905 static const struct file_operations watchdog_fops
= {
906 .owner
= THIS_MODULE
,
907 .write
= watchdog_write
,
908 .unlocked_ioctl
= watchdog_ioctl
,
909 .open
= watchdog_open
,
910 .release
= watchdog_release
,
913 static struct miscdevice watchdog_miscdev
= {
914 .minor
= WATCHDOG_MINOR
,
916 .fops
= &watchdog_fops
,
919 static struct class watchdog_class
= {
921 .owner
= THIS_MODULE
,
922 .dev_groups
= wdt_groups
,
926 * watchdog_cdev_register: register watchdog character device
927 * @wdd: watchdog device
929 * Register a watchdog character device including handling the legacy
930 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
931 * thus we set it up like that.
934 static int watchdog_cdev_register(struct watchdog_device
*wdd
)
936 struct watchdog_core_data
*wd_data
;
939 wd_data
= kzalloc(sizeof(struct watchdog_core_data
), GFP_KERNEL
);
942 mutex_init(&wd_data
->lock
);
945 wdd
->wd_data
= wd_data
;
947 if (IS_ERR_OR_NULL(watchdog_kworker
))
950 kthread_init_work(&wd_data
->work
, watchdog_ping_work
);
951 hrtimer_init(&wd_data
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
952 wd_data
->timer
.function
= watchdog_timer_expired
;
955 old_wd_data
= wd_data
;
956 watchdog_miscdev
.parent
= wdd
->parent
;
957 err
= misc_register(&watchdog_miscdev
);
959 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
960 wdd
->info
->identity
, WATCHDOG_MINOR
, err
);
962 pr_err("%s: a legacy watchdog module is probably present.\n",
963 wdd
->info
->identity
);
970 device_initialize(&wd_data
->dev
);
971 wd_data
->dev
.devt
= MKDEV(MAJOR(watchdog_devt
), wdd
->id
);
972 wd_data
->dev
.class = &watchdog_class
;
973 wd_data
->dev
.parent
= wdd
->parent
;
974 wd_data
->dev
.groups
= wdd
->groups
;
975 wd_data
->dev
.release
= watchdog_core_data_release
;
976 dev_set_drvdata(&wd_data
->dev
, wdd
);
977 dev_set_name(&wd_data
->dev
, "watchdog%d", wdd
->id
);
979 /* Fill in the data structures */
980 cdev_init(&wd_data
->cdev
, &watchdog_fops
);
983 err
= cdev_device_add(&wd_data
->cdev
, &wd_data
->dev
);
985 pr_err("watchdog%d unable to add device %d:%d\n",
986 wdd
->id
, MAJOR(watchdog_devt
), wdd
->id
);
988 misc_deregister(&watchdog_miscdev
);
990 put_device(&wd_data
->dev
);
995 wd_data
->cdev
.owner
= wdd
->ops
->owner
;
997 /* Record time of most recent heartbeat as 'just before now'. */
998 wd_data
->last_hw_keepalive
= ktime_sub(ktime_get(), 1);
1001 * If the watchdog is running, prevent its driver from being unloaded,
1002 * and schedule an immediate ping.
1004 if (watchdog_hw_running(wdd
)) {
1005 __module_get(wdd
->ops
->owner
);
1006 get_device(&wd_data
->dev
);
1007 if (handle_boot_enabled
)
1008 hrtimer_start(&wd_data
->timer
, 0, HRTIMER_MODE_REL
);
1010 pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
1018 * watchdog_cdev_unregister: unregister watchdog character device
1019 * @watchdog: watchdog device
1021 * Unregister watchdog character device and if needed the legacy
1022 * /dev/watchdog device.
1025 static void watchdog_cdev_unregister(struct watchdog_device
*wdd
)
1027 struct watchdog_core_data
*wd_data
= wdd
->wd_data
;
1029 cdev_device_del(&wd_data
->cdev
, &wd_data
->dev
);
1031 misc_deregister(&watchdog_miscdev
);
1035 if (watchdog_active(wdd
) &&
1036 test_bit(WDOG_STOP_ON_UNREGISTER
, &wdd
->status
)) {
1040 mutex_lock(&wd_data
->lock
);
1041 wd_data
->wdd
= NULL
;
1042 wdd
->wd_data
= NULL
;
1043 mutex_unlock(&wd_data
->lock
);
1045 hrtimer_cancel(&wd_data
->timer
);
1046 kthread_cancel_work_sync(&wd_data
->work
);
1048 put_device(&wd_data
->dev
);
1052 * watchdog_dev_register: register a watchdog device
1053 * @wdd: watchdog device
1055 * Register a watchdog device including handling the legacy
1056 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
1057 * thus we set it up like that.
1060 int watchdog_dev_register(struct watchdog_device
*wdd
)
1064 ret
= watchdog_cdev_register(wdd
);
1068 ret
= watchdog_register_pretimeout(wdd
);
1070 watchdog_cdev_unregister(wdd
);
1076 * watchdog_dev_unregister: unregister a watchdog device
1077 * @watchdog: watchdog device
1079 * Unregister watchdog device and if needed the legacy
1080 * /dev/watchdog device.
1083 void watchdog_dev_unregister(struct watchdog_device
*wdd
)
1085 watchdog_unregister_pretimeout(wdd
);
1086 watchdog_cdev_unregister(wdd
);
1090 * watchdog_dev_init: init dev part of watchdog core
1092 * Allocate a range of chardev nodes to use for watchdog devices
1095 int __init
watchdog_dev_init(void)
1098 struct sched_param param
= {.sched_priority
= MAX_RT_PRIO
- 1,};
1100 watchdog_kworker
= kthread_create_worker(0, "watchdogd");
1101 if (IS_ERR(watchdog_kworker
)) {
1102 pr_err("Failed to create watchdog kworker\n");
1103 return PTR_ERR(watchdog_kworker
);
1105 sched_setscheduler(watchdog_kworker
->task
, SCHED_FIFO
, ¶m
);
1107 err
= class_register(&watchdog_class
);
1109 pr_err("couldn't register class\n");
1113 err
= alloc_chrdev_region(&watchdog_devt
, 0, MAX_DOGS
, "watchdog");
1115 pr_err("watchdog: unable to allocate char dev region\n");
1122 class_unregister(&watchdog_class
);
1124 kthread_destroy_worker(watchdog_kworker
);
1129 * watchdog_dev_exit: exit dev part of watchdog core
1131 * Release the range of chardev nodes used for watchdog devices
1134 void __exit
watchdog_dev_exit(void)
1136 unregister_chrdev_region(watchdog_devt
, MAX_DOGS
);
1137 class_unregister(&watchdog_class
);
1138 kthread_destroy_worker(watchdog_kworker
);
1141 module_param(handle_boot_enabled
, bool, 0444);
1142 MODULE_PARM_DESC(handle_boot_enabled
,
1143 "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
1144 __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED
)) ")");