vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / watchdog / watchdog_dev.c
blobe64aa88e99dabaf5fff7ebf0d1a07e0b1e897584
1 /*
2 * watchdog_dev.c
4 * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5 * All Rights Reserved.
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 {
64 struct device dev;
65 struct cdev cdev;
66 struct watchdog_device *wdd;
67 struct mutex lock;
68 ktime_t last_keepalive;
69 ktime_t last_hw_keepalive;
70 struct hrtimer timer;
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
100 * requests.
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
105 * running.
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);
147 if (t > 0)
148 hrtimer_start(&wd_data->timer, t, HRTIMER_MODE_REL);
149 } else {
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;
158 int err;
160 earliest_keepalive = ktime_add(wd_data->last_hw_keepalive,
161 ms_to_ktime(wdd->min_hw_heartbeat_ms));
162 now = ktime_get();
164 if (ktime_after(earliest_keepalive, now)) {
165 hrtimer_start(&wd_data->timer,
166 ktime_sub(earliest_keepalive, now),
167 HRTIMER_MODE_REL);
168 return 0;
171 wd_data->last_hw_keepalive = now;
173 if (wdd->ops->ping)
174 err = wdd->ops->ping(wdd); /* ping the watchdog */
175 else
176 err = wdd->ops->start(wdd); /* restart watchdog */
178 watchdog_update_worker(wdd);
180 return err;
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
191 * exactly that.
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))
200 return 0;
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
245 * failure.
248 static int watchdog_start(struct watchdog_device *wdd)
250 struct watchdog_core_data *wd_data = wdd->wd_data;
251 ktime_t started_at;
252 int err;
254 if (watchdog_active(wdd))
255 return 0;
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);
262 else
263 err = wdd->ops->start(wdd);
264 if (err == 0) {
265 set_bit(WDOG_ACTIVE, &wdd->status);
266 wd_data->last_keepalive = started_at;
267 watchdog_update_worker(wdd);
270 return err;
274 * watchdog_stop: wrapper to stop the watchdog.
275 * @wdd: the watchdog device to stop
277 * The caller must hold wd_data->lock.
279 * Stop the watchdog if it is still active and unmark it active.
280 * This function returns zero on success or a negative errno code for
281 * failure.
282 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
285 static int watchdog_stop(struct watchdog_device *wdd)
287 int err = 0;
289 if (!watchdog_active(wdd))
290 return 0;
292 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
293 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
294 wdd->id);
295 return -EBUSY;
298 if (wdd->ops->stop) {
299 clear_bit(WDOG_HW_RUNNING, &wdd->status);
300 err = wdd->ops->stop(wdd);
301 } else {
302 set_bit(WDOG_HW_RUNNING, &wdd->status);
305 if (err == 0) {
306 clear_bit(WDOG_ACTIVE, &wdd->status);
307 watchdog_update_worker(wdd);
310 return err;
314 * watchdog_get_status: wrapper to get the watchdog status
315 * @wdd: the watchdog device to get the status from
317 * The caller must hold wd_data->lock.
319 * Get the watchdog's status flags.
322 static unsigned int watchdog_get_status(struct watchdog_device *wdd)
324 struct watchdog_core_data *wd_data = wdd->wd_data;
325 unsigned int status;
327 if (wdd->ops->status)
328 status = wdd->ops->status(wdd);
329 else
330 status = wdd->bootstatus & (WDIOF_CARDRESET |
331 WDIOF_OVERHEAT |
332 WDIOF_FANFAULT |
333 WDIOF_EXTERN1 |
334 WDIOF_EXTERN2 |
335 WDIOF_POWERUNDER |
336 WDIOF_POWEROVER);
338 if (test_bit(_WDOG_ALLOW_RELEASE, &wd_data->status))
339 status |= WDIOF_MAGICCLOSE;
341 if (test_and_clear_bit(_WDOG_KEEPALIVE, &wd_data->status))
342 status |= WDIOF_KEEPALIVEPING;
344 return status;
348 * watchdog_set_timeout: set the watchdog timer timeout
349 * @wdd: the watchdog device to set the timeout for
350 * @timeout: timeout to set in seconds
352 * The caller must hold wd_data->lock.
355 static int watchdog_set_timeout(struct watchdog_device *wdd,
356 unsigned int timeout)
358 int err = 0;
360 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
361 return -EOPNOTSUPP;
363 if (watchdog_timeout_invalid(wdd, timeout))
364 return -EINVAL;
366 if (wdd->ops->set_timeout) {
367 err = wdd->ops->set_timeout(wdd, timeout);
368 } else {
369 wdd->timeout = timeout;
370 /* Disable pretimeout if it doesn't fit the new timeout */
371 if (wdd->pretimeout >= wdd->timeout)
372 wdd->pretimeout = 0;
375 watchdog_update_worker(wdd);
377 return err;
381 * watchdog_set_pretimeout: set the watchdog timer pretimeout
382 * @wdd: the watchdog device to set the timeout for
383 * @timeout: pretimeout to set in seconds
386 static int watchdog_set_pretimeout(struct watchdog_device *wdd,
387 unsigned int timeout)
389 int err = 0;
391 if (!(wdd->info->options & WDIOF_PRETIMEOUT))
392 return -EOPNOTSUPP;
394 if (watchdog_pretimeout_invalid(wdd, timeout))
395 return -EINVAL;
397 if (wdd->ops->set_pretimeout)
398 err = wdd->ops->set_pretimeout(wdd, timeout);
399 else
400 wdd->pretimeout = timeout;
402 return err;
406 * watchdog_get_timeleft: wrapper to get the time left before a reboot
407 * @wdd: the watchdog device to get the remaining time from
408 * @timeleft: the time that's left
410 * The caller must hold wd_data->lock.
412 * Get the time before a watchdog will reboot (if not pinged).
415 static int watchdog_get_timeleft(struct watchdog_device *wdd,
416 unsigned int *timeleft)
418 *timeleft = 0;
420 if (!wdd->ops->get_timeleft)
421 return -EOPNOTSUPP;
423 *timeleft = wdd->ops->get_timeleft(wdd);
425 return 0;
428 #ifdef CONFIG_WATCHDOG_SYSFS
429 static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
430 char *buf)
432 struct watchdog_device *wdd = dev_get_drvdata(dev);
434 return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
436 static DEVICE_ATTR_RO(nowayout);
438 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
439 char *buf)
441 struct watchdog_device *wdd = dev_get_drvdata(dev);
442 struct watchdog_core_data *wd_data = wdd->wd_data;
443 unsigned int status;
445 mutex_lock(&wd_data->lock);
446 status = watchdog_get_status(wdd);
447 mutex_unlock(&wd_data->lock);
449 return sprintf(buf, "0x%x\n", status);
451 static DEVICE_ATTR_RO(status);
453 static ssize_t bootstatus_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct watchdog_device *wdd = dev_get_drvdata(dev);
458 return sprintf(buf, "%u\n", wdd->bootstatus);
460 static DEVICE_ATTR_RO(bootstatus);
462 static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
463 char *buf)
465 struct watchdog_device *wdd = dev_get_drvdata(dev);
466 struct watchdog_core_data *wd_data = wdd->wd_data;
467 ssize_t status;
468 unsigned int val;
470 mutex_lock(&wd_data->lock);
471 status = watchdog_get_timeleft(wdd, &val);
472 mutex_unlock(&wd_data->lock);
473 if (!status)
474 status = sprintf(buf, "%u\n", val);
476 return status;
478 static DEVICE_ATTR_RO(timeleft);
480 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
481 char *buf)
483 struct watchdog_device *wdd = dev_get_drvdata(dev);
485 return sprintf(buf, "%u\n", wdd->timeout);
487 static DEVICE_ATTR_RO(timeout);
489 static ssize_t pretimeout_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct watchdog_device *wdd = dev_get_drvdata(dev);
494 return sprintf(buf, "%u\n", wdd->pretimeout);
496 static DEVICE_ATTR_RO(pretimeout);
498 static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
499 char *buf)
501 struct watchdog_device *wdd = dev_get_drvdata(dev);
503 return sprintf(buf, "%s\n", wdd->info->identity);
505 static DEVICE_ATTR_RO(identity);
507 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
508 char *buf)
510 struct watchdog_device *wdd = dev_get_drvdata(dev);
512 if (watchdog_active(wdd))
513 return sprintf(buf, "active\n");
515 return sprintf(buf, "inactive\n");
517 static DEVICE_ATTR_RO(state);
519 static ssize_t pretimeout_available_governors_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 return watchdog_pretimeout_available_governors_get(buf);
524 static DEVICE_ATTR_RO(pretimeout_available_governors);
526 static ssize_t pretimeout_governor_show(struct device *dev,
527 struct device_attribute *attr,
528 char *buf)
530 struct watchdog_device *wdd = dev_get_drvdata(dev);
532 return watchdog_pretimeout_governor_get(wdd, buf);
535 static ssize_t pretimeout_governor_store(struct device *dev,
536 struct device_attribute *attr,
537 const char *buf, size_t count)
539 struct watchdog_device *wdd = dev_get_drvdata(dev);
540 int ret = watchdog_pretimeout_governor_set(wdd, buf);
542 if (!ret)
543 ret = count;
545 return ret;
547 static DEVICE_ATTR_RW(pretimeout_governor);
549 static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
550 int n)
552 struct device *dev = container_of(kobj, struct device, kobj);
553 struct watchdog_device *wdd = dev_get_drvdata(dev);
554 umode_t mode = attr->mode;
556 if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
557 mode = 0;
558 else if (attr == &dev_attr_pretimeout.attr &&
559 !(wdd->info->options & WDIOF_PRETIMEOUT))
560 mode = 0;
561 else if ((attr == &dev_attr_pretimeout_governor.attr ||
562 attr == &dev_attr_pretimeout_available_governors.attr) &&
563 (!(wdd->info->options & WDIOF_PRETIMEOUT) ||
564 !IS_ENABLED(CONFIG_WATCHDOG_PRETIMEOUT_GOV)))
565 mode = 0;
567 return mode;
569 static struct attribute *wdt_attrs[] = {
570 &dev_attr_state.attr,
571 &dev_attr_identity.attr,
572 &dev_attr_timeout.attr,
573 &dev_attr_pretimeout.attr,
574 &dev_attr_timeleft.attr,
575 &dev_attr_bootstatus.attr,
576 &dev_attr_status.attr,
577 &dev_attr_nowayout.attr,
578 &dev_attr_pretimeout_governor.attr,
579 &dev_attr_pretimeout_available_governors.attr,
580 NULL,
583 static const struct attribute_group wdt_group = {
584 .attrs = wdt_attrs,
585 .is_visible = wdt_is_visible,
587 __ATTRIBUTE_GROUPS(wdt);
588 #else
589 #define wdt_groups NULL
590 #endif
593 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
594 * @wdd: the watchdog device to do the ioctl on
595 * @cmd: watchdog command
596 * @arg: argument pointer
598 * The caller must hold wd_data->lock.
601 static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
602 unsigned long arg)
604 if (!wdd->ops->ioctl)
605 return -ENOIOCTLCMD;
607 return wdd->ops->ioctl(wdd, cmd, arg);
611 * watchdog_write: writes to the watchdog.
612 * @file: file from VFS
613 * @data: user address of data
614 * @len: length of data
615 * @ppos: pointer to the file offset
617 * A write to a watchdog device is defined as a keepalive ping.
618 * Writing the magic 'V' sequence allows the next close to turn
619 * off the watchdog (if 'nowayout' is not set).
622 static ssize_t watchdog_write(struct file *file, const char __user *data,
623 size_t len, loff_t *ppos)
625 struct watchdog_core_data *wd_data = file->private_data;
626 struct watchdog_device *wdd;
627 int err;
628 size_t i;
629 char c;
631 if (len == 0)
632 return 0;
635 * Note: just in case someone wrote the magic character
636 * five months ago...
638 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
640 /* scan to see whether or not we got the magic character */
641 for (i = 0; i != len; i++) {
642 if (get_user(c, data + i))
643 return -EFAULT;
644 if (c == 'V')
645 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
648 /* someone wrote to us, so we send the watchdog a keepalive ping */
650 err = -ENODEV;
651 mutex_lock(&wd_data->lock);
652 wdd = wd_data->wdd;
653 if (wdd)
654 err = watchdog_ping(wdd);
655 mutex_unlock(&wd_data->lock);
657 if (err < 0)
658 return err;
660 return len;
664 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
665 * @file: file handle to the device
666 * @cmd: watchdog command
667 * @arg: argument pointer
669 * The watchdog API defines a common set of functions for all watchdogs
670 * according to their available features.
673 static long watchdog_ioctl(struct file *file, unsigned int cmd,
674 unsigned long arg)
676 struct watchdog_core_data *wd_data = file->private_data;
677 void __user *argp = (void __user *)arg;
678 struct watchdog_device *wdd;
679 int __user *p = argp;
680 unsigned int val;
681 int err;
683 mutex_lock(&wd_data->lock);
685 wdd = wd_data->wdd;
686 if (!wdd) {
687 err = -ENODEV;
688 goto out_ioctl;
691 err = watchdog_ioctl_op(wdd, cmd, arg);
692 if (err != -ENOIOCTLCMD)
693 goto out_ioctl;
695 switch (cmd) {
696 case WDIOC_GETSUPPORT:
697 err = copy_to_user(argp, wdd->info,
698 sizeof(struct watchdog_info)) ? -EFAULT : 0;
699 break;
700 case WDIOC_GETSTATUS:
701 val = watchdog_get_status(wdd);
702 err = put_user(val, p);
703 break;
704 case WDIOC_GETBOOTSTATUS:
705 err = put_user(wdd->bootstatus, p);
706 break;
707 case WDIOC_SETOPTIONS:
708 if (get_user(val, p)) {
709 err = -EFAULT;
710 break;
712 if (val & WDIOS_DISABLECARD) {
713 err = watchdog_stop(wdd);
714 if (err < 0)
715 break;
717 if (val & WDIOS_ENABLECARD)
718 err = watchdog_start(wdd);
719 break;
720 case WDIOC_KEEPALIVE:
721 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
722 err = -EOPNOTSUPP;
723 break;
725 err = watchdog_ping(wdd);
726 break;
727 case WDIOC_SETTIMEOUT:
728 if (get_user(val, p)) {
729 err = -EFAULT;
730 break;
732 err = watchdog_set_timeout(wdd, val);
733 if (err < 0)
734 break;
735 /* If the watchdog is active then we send a keepalive ping
736 * to make sure that the watchdog keep's running (and if
737 * possible that it takes the new timeout) */
738 err = watchdog_ping(wdd);
739 if (err < 0)
740 break;
741 /* fall through */
742 case WDIOC_GETTIMEOUT:
743 /* timeout == 0 means that we don't know the timeout */
744 if (wdd->timeout == 0) {
745 err = -EOPNOTSUPP;
746 break;
748 err = put_user(wdd->timeout, p);
749 break;
750 case WDIOC_GETTIMELEFT:
751 err = watchdog_get_timeleft(wdd, &val);
752 if (err < 0)
753 break;
754 err = put_user(val, p);
755 break;
756 case WDIOC_SETPRETIMEOUT:
757 if (get_user(val, p)) {
758 err = -EFAULT;
759 break;
761 err = watchdog_set_pretimeout(wdd, val);
762 break;
763 case WDIOC_GETPRETIMEOUT:
764 err = put_user(wdd->pretimeout, p);
765 break;
766 default:
767 err = -ENOTTY;
768 break;
771 out_ioctl:
772 mutex_unlock(&wd_data->lock);
773 return err;
777 * watchdog_open: open the /dev/watchdog* devices.
778 * @inode: inode of device
779 * @file: file handle to device
781 * When the /dev/watchdog* device gets opened, we start the watchdog.
782 * Watch out: the /dev/watchdog device is single open, so we make sure
783 * it can only be opened once.
786 static int watchdog_open(struct inode *inode, struct file *file)
788 struct watchdog_core_data *wd_data;
789 struct watchdog_device *wdd;
790 bool hw_running;
791 int err;
793 /* Get the corresponding watchdog device */
794 if (imajor(inode) == MISC_MAJOR)
795 wd_data = old_wd_data;
796 else
797 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
798 cdev);
800 /* the watchdog is single open! */
801 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
802 return -EBUSY;
804 wdd = wd_data->wdd;
807 * If the /dev/watchdog device is open, we don't want the module
808 * to be unloaded.
810 hw_running = watchdog_hw_running(wdd);
811 if (!hw_running && !try_module_get(wdd->ops->owner)) {
812 err = -EBUSY;
813 goto out_clear;
816 err = watchdog_start(wdd);
817 if (err < 0)
818 goto out_mod;
820 file->private_data = wd_data;
822 if (!hw_running)
823 get_device(&wd_data->dev);
825 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
826 return nonseekable_open(inode, file);
828 out_mod:
829 module_put(wd_data->wdd->ops->owner);
830 out_clear:
831 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
832 return err;
835 static void watchdog_core_data_release(struct device *dev)
837 struct watchdog_core_data *wd_data;
839 wd_data = container_of(dev, struct watchdog_core_data, dev);
841 kfree(wd_data);
845 * watchdog_release: release the watchdog device.
846 * @inode: inode of device
847 * @file: file handle to device
849 * This is the code for when /dev/watchdog gets closed. We will only
850 * stop the watchdog when we have received the magic char (and nowayout
851 * was not set), else the watchdog will keep running.
854 static int watchdog_release(struct inode *inode, struct file *file)
856 struct watchdog_core_data *wd_data = file->private_data;
857 struct watchdog_device *wdd;
858 int err = -EBUSY;
859 bool running;
861 mutex_lock(&wd_data->lock);
863 wdd = wd_data->wdd;
864 if (!wdd)
865 goto done;
868 * We only stop the watchdog if we received the magic character
869 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
870 * watchdog_stop will fail.
872 if (!test_bit(WDOG_ACTIVE, &wdd->status))
873 err = 0;
874 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
875 !(wdd->info->options & WDIOF_MAGICCLOSE))
876 err = watchdog_stop(wdd);
878 /* If the watchdog was not stopped, send a keepalive ping */
879 if (err < 0) {
880 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
881 watchdog_ping(wdd);
884 watchdog_update_worker(wdd);
886 /* make sure that /dev/watchdog can be re-opened */
887 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
889 done:
890 running = wdd && watchdog_hw_running(wdd);
891 mutex_unlock(&wd_data->lock);
893 * Allow the owner module to be unloaded again unless the watchdog
894 * is still running. If the watchdog is still running, it can not
895 * be stopped, and its driver must not be unloaded.
897 if (!running) {
898 module_put(wd_data->cdev.owner);
899 put_device(&wd_data->dev);
901 return 0;
904 static const struct file_operations watchdog_fops = {
905 .owner = THIS_MODULE,
906 .write = watchdog_write,
907 .unlocked_ioctl = watchdog_ioctl,
908 .open = watchdog_open,
909 .release = watchdog_release,
912 static struct miscdevice watchdog_miscdev = {
913 .minor = WATCHDOG_MINOR,
914 .name = "watchdog",
915 .fops = &watchdog_fops,
918 static struct class watchdog_class = {
919 .name = "watchdog",
920 .owner = THIS_MODULE,
921 .dev_groups = wdt_groups,
925 * watchdog_cdev_register: register watchdog character device
926 * @wdd: watchdog device
928 * Register a watchdog character device including handling the legacy
929 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
930 * thus we set it up like that.
933 static int watchdog_cdev_register(struct watchdog_device *wdd)
935 struct watchdog_core_data *wd_data;
936 int err;
938 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
939 if (!wd_data)
940 return -ENOMEM;
941 mutex_init(&wd_data->lock);
943 wd_data->wdd = wdd;
944 wdd->wd_data = wd_data;
946 if (IS_ERR_OR_NULL(watchdog_kworker))
947 return -ENODEV;
949 kthread_init_work(&wd_data->work, watchdog_ping_work);
950 hrtimer_init(&wd_data->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
951 wd_data->timer.function = watchdog_timer_expired;
953 if (wdd->id == 0) {
954 old_wd_data = wd_data;
955 watchdog_miscdev.parent = wdd->parent;
956 err = misc_register(&watchdog_miscdev);
957 if (err != 0) {
958 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
959 wdd->info->identity, WATCHDOG_MINOR, err);
960 if (err == -EBUSY)
961 pr_err("%s: a legacy watchdog module is probably present.\n",
962 wdd->info->identity);
963 old_wd_data = NULL;
964 kfree(wd_data);
965 return err;
969 device_initialize(&wd_data->dev);
970 wd_data->dev.devt = MKDEV(MAJOR(watchdog_devt), wdd->id);
971 wd_data->dev.class = &watchdog_class;
972 wd_data->dev.parent = wdd->parent;
973 wd_data->dev.groups = wdd->groups;
974 wd_data->dev.release = watchdog_core_data_release;
975 dev_set_drvdata(&wd_data->dev, wdd);
976 dev_set_name(&wd_data->dev, "watchdog%d", wdd->id);
978 /* Fill in the data structures */
979 cdev_init(&wd_data->cdev, &watchdog_fops);
981 /* Add the device */
982 err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
983 if (err) {
984 pr_err("watchdog%d unable to add device %d:%d\n",
985 wdd->id, MAJOR(watchdog_devt), wdd->id);
986 if (wdd->id == 0) {
987 misc_deregister(&watchdog_miscdev);
988 old_wd_data = NULL;
989 put_device(&wd_data->dev);
991 return err;
994 wd_data->cdev.owner = wdd->ops->owner;
996 /* Record time of most recent heartbeat as 'just before now'. */
997 wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
1000 * If the watchdog is running, prevent its driver from being unloaded,
1001 * and schedule an immediate ping.
1003 if (watchdog_hw_running(wdd)) {
1004 __module_get(wdd->ops->owner);
1005 get_device(&wd_data->dev);
1006 if (handle_boot_enabled)
1007 hrtimer_start(&wd_data->timer, 0, HRTIMER_MODE_REL);
1008 else
1009 pr_info("watchdog%d running and kernel based pre-userspace handler disabled\n",
1010 wdd->id);
1013 return 0;
1017 * watchdog_cdev_unregister: unregister watchdog character device
1018 * @watchdog: watchdog device
1020 * Unregister watchdog character device and if needed the legacy
1021 * /dev/watchdog device.
1024 static void watchdog_cdev_unregister(struct watchdog_device *wdd)
1026 struct watchdog_core_data *wd_data = wdd->wd_data;
1028 cdev_device_del(&wd_data->cdev, &wd_data->dev);
1029 if (wdd->id == 0) {
1030 misc_deregister(&watchdog_miscdev);
1031 old_wd_data = NULL;
1034 if (watchdog_active(wdd) &&
1035 test_bit(WDOG_STOP_ON_UNREGISTER, &wdd->status)) {
1036 watchdog_stop(wdd);
1039 mutex_lock(&wd_data->lock);
1040 wd_data->wdd = NULL;
1041 wdd->wd_data = NULL;
1042 mutex_unlock(&wd_data->lock);
1044 hrtimer_cancel(&wd_data->timer);
1045 kthread_cancel_work_sync(&wd_data->work);
1047 put_device(&wd_data->dev);
1051 * watchdog_dev_register: register a watchdog device
1052 * @wdd: watchdog device
1054 * Register a watchdog device including handling the legacy
1055 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
1056 * thus we set it up like that.
1059 int watchdog_dev_register(struct watchdog_device *wdd)
1061 int ret;
1063 ret = watchdog_cdev_register(wdd);
1064 if (ret)
1065 return ret;
1067 ret = watchdog_register_pretimeout(wdd);
1068 if (ret)
1069 watchdog_cdev_unregister(wdd);
1071 return ret;
1075 * watchdog_dev_unregister: unregister a watchdog device
1076 * @watchdog: watchdog device
1078 * Unregister watchdog device and if needed the legacy
1079 * /dev/watchdog device.
1082 void watchdog_dev_unregister(struct watchdog_device *wdd)
1084 watchdog_unregister_pretimeout(wdd);
1085 watchdog_cdev_unregister(wdd);
1089 * watchdog_dev_init: init dev part of watchdog core
1091 * Allocate a range of chardev nodes to use for watchdog devices
1094 int __init watchdog_dev_init(void)
1096 int err;
1097 struct sched_param param = {.sched_priority = MAX_RT_PRIO - 1,};
1099 watchdog_kworker = kthread_create_worker(0, "watchdogd");
1100 if (IS_ERR(watchdog_kworker)) {
1101 pr_err("Failed to create watchdog kworker\n");
1102 return PTR_ERR(watchdog_kworker);
1104 sched_setscheduler(watchdog_kworker->task, SCHED_FIFO, &param);
1106 err = class_register(&watchdog_class);
1107 if (err < 0) {
1108 pr_err("couldn't register class\n");
1109 goto err_register;
1112 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
1113 if (err < 0) {
1114 pr_err("watchdog: unable to allocate char dev region\n");
1115 goto err_alloc;
1118 return 0;
1120 err_alloc:
1121 class_unregister(&watchdog_class);
1122 err_register:
1123 kthread_destroy_worker(watchdog_kworker);
1124 return err;
1128 * watchdog_dev_exit: exit dev part of watchdog core
1130 * Release the range of chardev nodes used for watchdog devices
1133 void __exit watchdog_dev_exit(void)
1135 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
1136 class_unregister(&watchdog_class);
1137 kthread_destroy_worker(watchdog_kworker);
1140 module_param(handle_boot_enabled, bool, 0444);
1141 MODULE_PARM_DESC(handle_boot_enabled,
1142 "Watchdog core auto-updates boot enabled watchdogs before userspace takes over (default="
1143 __MODULE_STRING(IS_ENABLED(CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED)) ")");