Commit | Line | Data |
---|---|---|
43316044 WVS |
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/... */ | |
45f5fed3 | 37 | #include <linux/idr.h> /* For ida_* macros */ |
43316044 | 38 | |
6cfb5aa8 | 39 | #include "watchdog_core.h" /* For watchdog_dev_register/... */ |
43316044 | 40 | |
45f5fed3 AC |
41 | static DEFINE_IDA(watchdog_ida); |
42 | ||
43316044 WVS |
43 | /** |
44 | * watchdog_register_device() - register a watchdog device | |
45 | * @wdd: watchdog device | |
46 | * | |
47 | * Register a watchdog device with the kernel so that the | |
48 | * watchdog timer can be accessed from userspace. | |
49 | * | |
50 | * A zero is returned on success and a negative errno code for | |
51 | * failure. | |
52 | */ | |
53 | int watchdog_register_device(struct watchdog_device *wdd) | |
54 | { | |
45f5fed3 | 55 | int ret, id; |
43316044 WVS |
56 | |
57 | if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL) | |
58 | return -EINVAL; | |
59 | ||
60 | /* Mandatory operations need to be supported */ | |
61 | if (wdd->ops->start == NULL || wdd->ops->stop == NULL) | |
62 | return -EINVAL; | |
63 | ||
3f43f68e WVS |
64 | /* |
65 | * Check that we have valid min and max timeout values, if | |
66 | * not reset them both to 0 (=not used or unknown) | |
67 | */ | |
68 | if (wdd->min_timeout > wdd->max_timeout) { | |
69 | pr_info("Invalid min and max timeout values, resetting to 0!\n"); | |
70 | wdd->min_timeout = 0; | |
71 | wdd->max_timeout = 0; | |
72 | } | |
73 | ||
43316044 WVS |
74 | /* |
75 | * Note: now that all watchdog_device data has been verified, we | |
76 | * will not check this anymore in other functions. If data gets | |
77 | * corrupted in a later stage then we expect a kernel panic! | |
78 | */ | |
79 | ||
45f5fed3 AC |
80 | id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL); |
81 | if (id < 0) | |
82 | return id; | |
83 | wdd->id = id; | |
84 | ||
43316044 WVS |
85 | ret = watchdog_dev_register(wdd); |
86 | if (ret) { | |
45f5fed3 AC |
87 | ida_simple_remove(&watchdog_ida, id); |
88 | if (!(id == 0 && ret == -EBUSY)) | |
89 | return ret; | |
90 | ||
91 | /* Retry in case a legacy watchdog module exists */ | |
92 | id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL); | |
93 | if (id < 0) | |
94 | return id; | |
95 | wdd->id = id; | |
96 | ||
97 | ret = watchdog_dev_register(wdd); | |
98 | if (ret) { | |
99 | ida_simple_remove(&watchdog_ida, id); | |
100 | return ret; | |
101 | } | |
43316044 WVS |
102 | } |
103 | ||
104 | return 0; | |
105 | } | |
106 | EXPORT_SYMBOL_GPL(watchdog_register_device); | |
107 | ||
108 | /** | |
109 | * watchdog_unregister_device() - unregister a watchdog device | |
110 | * @wdd: watchdog device to unregister | |
111 | * | |
112 | * Unregister a watchdog device that was previously successfully | |
113 | * registered with watchdog_register_device(). | |
114 | */ | |
115 | void watchdog_unregister_device(struct watchdog_device *wdd) | |
116 | { | |
117 | int ret; | |
118 | ||
119 | if (wdd == NULL) | |
120 | return; | |
121 | ||
122 | ret = watchdog_dev_unregister(wdd); | |
123 | if (ret) | |
27c766aa | 124 | pr_err("error unregistering /dev/watchdog (err=%d)\n", ret); |
45f5fed3 | 125 | ida_simple_remove(&watchdog_ida, wdd->id); |
43316044 WVS |
126 | } |
127 | EXPORT_SYMBOL_GPL(watchdog_unregister_device); | |
128 | ||
45f5fed3 AC |
129 | static int __init watchdog_init(void) |
130 | { | |
131 | return watchdog_dev_init(); | |
132 | } | |
133 | ||
134 | static void __exit watchdog_exit(void) | |
135 | { | |
136 | watchdog_dev_exit(); | |
137 | ida_destroy(&watchdog_ida); | |
138 | } | |
139 | ||
140 | subsys_initcall(watchdog_init); | |
141 | module_exit(watchdog_exit); | |
142 | ||
43316044 WVS |
143 | MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>"); |
144 | MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>"); | |
145 | MODULE_DESCRIPTION("WatchDog Timer Driver Core"); | |
146 | MODULE_LICENSE("GPL"); |