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:
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/module.h> /* For module stuff/... */
36 #include <linux/types.h> /* For standard types (like size_t) */
37 #include <linux/errno.h> /* For the -ENODEV/... values */
38 #include <linux/kernel.h> /* For printk/panic/... */
39 #include <linux/fs.h> /* For file operations */
40 #include <linux/watchdog.h> /* For watchdog specific items */
41 #include <linux/miscdevice.h> /* For handling misc devices */
42 #include <linux/init.h> /* For __init/__exit/... */
43 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
45 #include "watchdog_core.h"
47 /* the dev_t structure to store the dynamically allocated watchdog devices */
48 static dev_t watchdog_devt;
49 /* the watchdog device behind /dev/watchdog */
50 static struct watchdog_device *old_wdd;
53 * watchdog_ping: ping the watchdog.
54 * @wdd: the watchdog device to ping
56 * If the watchdog has no own ping operation then it needs to be
57 * restarted via the start operation. This wrapper function does
59 * We only ping when the watchdog device is running.
62 static int watchdog_ping(struct watchdog_device *wdd)
66 mutex_lock(&wdd->lock);
68 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
73 if (!watchdog_active(wdd))
77 err = wdd->ops->ping(wdd); /* ping the watchdog */
79 err = wdd->ops->start(wdd); /* restart watchdog */
82 mutex_unlock(&wdd->lock);
87 * watchdog_start: wrapper to start the watchdog.
88 * @wdd: the watchdog device to start
90 * Start the watchdog if it is not active and mark it active.
91 * This function returns zero on success or a negative errno code for
95 static int watchdog_start(struct watchdog_device *wdd)
99 mutex_lock(&wdd->lock);
101 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
106 if (watchdog_active(wdd))
109 err = wdd->ops->start(wdd);
111 set_bit(WDOG_ACTIVE, &wdd->status);
114 mutex_unlock(&wdd->lock);
119 * watchdog_stop: wrapper to stop the watchdog.
120 * @wdd: the watchdog device to stop
122 * Stop the watchdog if it is still active and unmark it active.
123 * This function returns zero on success or a negative errno code for
125 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
128 static int watchdog_stop(struct watchdog_device *wdd)
132 mutex_lock(&wdd->lock);
134 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
139 if (!watchdog_active(wdd))
142 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
143 dev_info(wdd->dev, "nowayout prevents watchdog being stopped!\n");
148 err = wdd->ops->stop(wdd);
150 clear_bit(WDOG_ACTIVE, &wdd->status);
153 mutex_unlock(&wdd->lock);
158 * watchdog_get_status: wrapper to get the watchdog status
159 * @wdd: the watchdog device to get the status from
160 * @status: the status of the watchdog device
162 * Get the watchdog's status flags.
165 static int watchdog_get_status(struct watchdog_device *wdd,
166 unsigned int *status)
171 if (!wdd->ops->status)
174 mutex_lock(&wdd->lock);
176 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
181 *status = wdd->ops->status(wdd);
184 mutex_unlock(&wdd->lock);
189 * watchdog_set_timeout: set the watchdog timer timeout
190 * @wdd: the watchdog device to set the timeout for
191 * @timeout: timeout to set in seconds
194 static int watchdog_set_timeout(struct watchdog_device *wdd,
195 unsigned int timeout)
199 if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT))
202 if (watchdog_timeout_invalid(wdd, timeout))
205 mutex_lock(&wdd->lock);
207 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
212 err = wdd->ops->set_timeout(wdd, timeout);
215 mutex_unlock(&wdd->lock);
220 * watchdog_get_timeleft: wrapper to get the time left before a reboot
221 * @wdd: the watchdog device to get the remaining time from
222 * @timeleft: the time that's left
224 * Get the time before a watchdog will reboot (if not pinged).
227 static int watchdog_get_timeleft(struct watchdog_device *wdd,
228 unsigned int *timeleft)
233 if (!wdd->ops->get_timeleft)
236 mutex_lock(&wdd->lock);
238 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
243 *timeleft = wdd->ops->get_timeleft(wdd);
246 mutex_unlock(&wdd->lock);
251 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
252 * @wdd: the watchdog device to do the ioctl on
253 * @cmd: watchdog command
254 * @arg: argument pointer
257 static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
262 if (!wdd->ops->ioctl)
265 mutex_lock(&wdd->lock);
267 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
272 err = wdd->ops->ioctl(wdd, cmd, arg);
275 mutex_unlock(&wdd->lock);
280 * watchdog_write: writes to the watchdog.
281 * @file: file from VFS
282 * @data: user address of data
283 * @len: length of data
284 * @ppos: pointer to the file offset
286 * A write to a watchdog device is defined as a keepalive ping.
287 * Writing the magic 'V' sequence allows the next close to turn
288 * off the watchdog (if 'nowayout' is not set).
291 static ssize_t watchdog_write(struct file *file, const char __user *data,
292 size_t len, loff_t *ppos)
294 struct watchdog_device *wdd = file->private_data;
303 * Note: just in case someone wrote the magic character
306 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
308 /* scan to see whether or not we got the magic character */
309 for (i = 0; i != len; i++) {
310 if (get_user(c, data + i))
313 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
316 /* someone wrote to us, so we send the watchdog a keepalive ping */
317 err = watchdog_ping(wdd);
325 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
326 * @file: file handle to the device
327 * @cmd: watchdog command
328 * @arg: argument pointer
330 * The watchdog API defines a common set of functions for all watchdogs
331 * according to their available features.
334 static long watchdog_ioctl(struct file *file, unsigned int cmd,
337 struct watchdog_device *wdd = file->private_data;
338 void __user *argp = (void __user *)arg;
339 int __user *p = argp;
343 err = watchdog_ioctl_op(wdd, cmd, arg);
344 if (err != -ENOIOCTLCMD)
348 case WDIOC_GETSUPPORT:
349 return copy_to_user(argp, wdd->info,
350 sizeof(struct watchdog_info)) ? -EFAULT : 0;
351 case WDIOC_GETSTATUS:
352 err = watchdog_get_status(wdd, &val);
355 return put_user(val, p);
356 case WDIOC_GETBOOTSTATUS:
357 return put_user(wdd->bootstatus, p);
358 case WDIOC_SETOPTIONS:
359 if (get_user(val, p))
361 if (val & WDIOS_DISABLECARD) {
362 err = watchdog_stop(wdd);
366 if (val & WDIOS_ENABLECARD) {
367 err = watchdog_start(wdd);
372 case WDIOC_KEEPALIVE:
373 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
375 return watchdog_ping(wdd);
376 case WDIOC_SETTIMEOUT:
377 if (get_user(val, p))
379 err = watchdog_set_timeout(wdd, val);
382 /* If the watchdog is active then we send a keepalive ping
383 * to make sure that the watchdog keep's running (and if
384 * possible that it takes the new timeout) */
385 err = watchdog_ping(wdd);
389 case WDIOC_GETTIMEOUT:
390 /* timeout == 0 means that we don't know the timeout */
391 if (wdd->timeout == 0)
393 return put_user(wdd->timeout, p);
394 case WDIOC_GETTIMELEFT:
395 err = watchdog_get_timeleft(wdd, &val);
398 return put_user(val, p);
405 * watchdog_open: open the /dev/watchdog* devices.
406 * @inode: inode of device
407 * @file: file handle to device
409 * When the /dev/watchdog* device gets opened, we start the watchdog.
410 * Watch out: the /dev/watchdog device is single open, so we make sure
411 * it can only be opened once.
414 static int watchdog_open(struct inode *inode, struct file *file)
417 struct watchdog_device *wdd;
419 /* Get the corresponding watchdog device */
420 if (imajor(inode) == MISC_MAJOR)
423 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
425 /* the watchdog is single open! */
426 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
430 * If the /dev/watchdog device is open, we don't want the module
433 if (!try_module_get(wdd->ops->owner))
436 err = watchdog_start(wdd);
440 file->private_data = wdd;
445 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
446 return nonseekable_open(inode, file);
449 module_put(wdd->ops->owner);
451 clear_bit(WDOG_DEV_OPEN, &wdd->status);
456 * watchdog_release: release the watchdog device.
457 * @inode: inode of device
458 * @file: file handle to device
460 * This is the code for when /dev/watchdog gets closed. We will only
461 * stop the watchdog when we have received the magic char (and nowayout
462 * was not set), else the watchdog will keep running.
465 static int watchdog_release(struct inode *inode, struct file *file)
467 struct watchdog_device *wdd = file->private_data;
471 * We only stop the watchdog if we received the magic character
472 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
473 * watchdog_stop will fail.
475 if (!test_bit(WDOG_ACTIVE, &wdd->status))
477 else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
478 !(wdd->info->options & WDIOF_MAGICCLOSE))
479 err = watchdog_stop(wdd);
481 /* If the watchdog was not stopped, send a keepalive ping */
483 mutex_lock(&wdd->lock);
484 if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
485 dev_crit(wdd->dev, "watchdog did not stop!\n");
486 mutex_unlock(&wdd->lock);
490 /* Allow the owner module to be unloaded again */
491 module_put(wdd->ops->owner);
493 /* make sure that /dev/watchdog can be re-opened */
494 clear_bit(WDOG_DEV_OPEN, &wdd->status);
496 /* Note wdd may be gone after this, do not use after this! */
498 wdd->ops->unref(wdd);
503 static const struct file_operations watchdog_fops = {
504 .owner = THIS_MODULE,
505 .write = watchdog_write,
506 .unlocked_ioctl = watchdog_ioctl,
507 .open = watchdog_open,
508 .release = watchdog_release,
511 static struct miscdevice watchdog_miscdev = {
512 .minor = WATCHDOG_MINOR,
514 .fops = &watchdog_fops,
518 * watchdog_dev_register: register a watchdog device
519 * @wdd: watchdog device
521 * Register a watchdog device including handling the legacy
522 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
523 * thus we set it up like that.
526 int watchdog_dev_register(struct watchdog_device *wdd)
532 watchdog_miscdev.parent = wdd->parent;
533 err = misc_register(&watchdog_miscdev);
535 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
536 wdd->info->identity, WATCHDOG_MINOR, err);
538 pr_err("%s: a legacy watchdog module is probably present.\n",
539 wdd->info->identity);
545 /* Fill in the data structures */
546 devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
547 cdev_init(&wdd->cdev, &watchdog_fops);
548 wdd->cdev.owner = wdd->ops->owner;
551 err = cdev_add(&wdd->cdev, devno, 1);
553 pr_err("watchdog%d unable to add device %d:%d\n",
554 wdd->id, MAJOR(watchdog_devt), wdd->id);
556 misc_deregister(&watchdog_miscdev);
564 * watchdog_dev_unregister: unregister a watchdog device
565 * @watchdog: watchdog device
567 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
570 int watchdog_dev_unregister(struct watchdog_device *wdd)
572 mutex_lock(&wdd->lock);
573 set_bit(WDOG_UNREGISTERED, &wdd->status);
574 mutex_unlock(&wdd->lock);
576 cdev_del(&wdd->cdev);
578 misc_deregister(&watchdog_miscdev);
585 * watchdog_dev_init: init dev part of watchdog core
587 * Allocate a range of chardev nodes to use for watchdog devices
590 int __init watchdog_dev_init(void)
592 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
594 pr_err("watchdog: unable to allocate char dev region\n");
599 * watchdog_dev_exit: exit dev part of watchdog core
601 * Release the range of chardev nodes used for watchdog devices
604 void __exit watchdog_dev_exit(void)
606 unregister_chrdev_region(watchdog_devt, MAX_DOGS);