1 /*
2  *	watchdog_core.c
3  *
4  *	(c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
5  *						All Rights Reserved.
6  *
7  *	(c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
8  *
9  *	This source code is part of the generic code that can be used
10  *	by all the watchdog timer drivers.
11  *
12  *	Based on source code of the following authors:
13  *	  Matt Domsch <Matt_Domsch@dell.com>,
14  *	  Rob Radez <rob@osinvestor.com>,
15  *	  Rusty Lynch <rusty@linux.co.intel.com>
16  *	  Satyam Sharma <satyam@infradead.org>
17  *	  Randy Dunlap <randy.dunlap@oracle.com>
18  *
19  *	This program is free software; you can redistribute it and/or
20  *	modify it under the terms of the GNU General Public License
21  *	as published by the Free Software Foundation; either version
22  *	2 of the License, or (at your option) any later version.
23  *
24  *	Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
25  *	admit liability nor provide warranty for any of this software.
26  *	This material is provided "AS-IS" and at no charge.
27  */
28 
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 
31 #include <linux/module.h>	/* For EXPORT_SYMBOL/module stuff/... */
32 #include <linux/types.h>	/* For standard types */
33 #include <linux/errno.h>	/* For the -ENODEV/... values */
34 #include <linux/kernel.h>	/* For printk/panic/... */
35 #include <linux/watchdog.h>	/* For watchdog specific items */
36 #include <linux/init.h>		/* For __init/__exit/... */
37 
38 #include "watchdog_dev.h"	/* For watchdog_dev_register/... */
39 
40 /**
41  * watchdog_register_device() - register a watchdog device
42  * @wdd: watchdog device
43  *
44  * Register a watchdog device with the kernel so that the
45  * watchdog timer can be accessed from userspace.
46  *
47  * A zero is returned on success and a negative errno code for
48  * failure.
49  */
watchdog_register_device(struct watchdog_device * wdd)50 int watchdog_register_device(struct watchdog_device *wdd)
51 {
52 	int ret;
53 
54 	if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
55 		return -EINVAL;
56 
57 	/* Mandatory operations need to be supported */
58 	if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
59 		return -EINVAL;
60 
61 	/*
62 	 * Check that we have valid min and max timeout values, if
63 	 * not reset them both to 0 (=not used or unknown)
64 	 */
65 	if (wdd->min_timeout > wdd->max_timeout) {
66 		pr_info("Invalid min and max timeout values, resetting to 0!\n");
67 		wdd->min_timeout = 0;
68 		wdd->max_timeout = 0;
69 	}
70 
71 	/*
72 	 * Note: now that all watchdog_device data has been verified, we
73 	 * will not check this anymore in other functions. If data gets
74 	 * corrupted in a later stage then we expect a kernel panic!
75 	 */
76 
77 	/* We only support 1 watchdog device via the /dev/watchdog interface */
78 	ret = watchdog_dev_register(wdd);
79 	if (ret) {
80 		pr_err("error registering /dev/watchdog (err=%d)\n", ret);
81 		return ret;
82 	}
83 
84 	return 0;
85 }
86 EXPORT_SYMBOL_GPL(watchdog_register_device);
87 
88 /**
89  * watchdog_unregister_device() - unregister a watchdog device
90  * @wdd: watchdog device to unregister
91  *
92  * Unregister a watchdog device that was previously successfully
93  * registered with watchdog_register_device().
94  */
watchdog_unregister_device(struct watchdog_device * wdd)95 void watchdog_unregister_device(struct watchdog_device *wdd)
96 {
97 	int ret;
98 
99 	if (wdd == NULL)
100 		return;
101 
102 	ret = watchdog_dev_unregister(wdd);
103 	if (ret)
104 		pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
105 }
106 EXPORT_SYMBOL_GPL(watchdog_unregister_device);
107 
108 MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
109 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
110 MODULE_DESCRIPTION("WatchDog Timer Driver Core");
111 MODULE_LICENSE("GPL");
112