watchdog: sunxi_wdt: use core restart handler
[deliverable/linux.git] / drivers / watchdog / watchdog_core.c
CommitLineData
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/... */
2165bf52 35#include <linux/reboot.h> /* For restart handler */
43316044
WVS
36#include <linux/watchdog.h> /* For watchdog specific items */
37#include <linux/init.h> /* For __init/__exit/... */
45f5fed3 38#include <linux/idr.h> /* For ida_* macros */
d6b469d9 39#include <linux/err.h> /* For IS_ERR macros */
3048253e 40#include <linux/of.h> /* For of_get_timeout_sec */
43316044 41
6cfb5aa8 42#include "watchdog_core.h" /* For watchdog_dev_register/... */
43316044 43
45f5fed3 44static DEFINE_IDA(watchdog_ida);
d6b469d9 45static struct class *watchdog_class;
45f5fed3 46
ef90174f
JBT
47/*
48 * Deferred Registration infrastructure.
49 *
50 * Sometimes watchdog drivers needs to be loaded as soon as possible,
51 * for example when it's impossible to disable it. To do so,
52 * raising the initcall level of the watchdog driver is a solution.
53 * But in such case, the miscdev is maybe not ready (subsys_initcall), and
54 * watchdog_core need miscdev to register the watchdog as a char device.
55 *
56 * The deferred registration infrastructure offer a way for the watchdog
57 * subsystem to register a watchdog properly, even before miscdev is ready.
58 */
59
60static DEFINE_MUTEX(wtd_deferred_reg_mutex);
61static LIST_HEAD(wtd_deferred_reg_list);
62static bool wtd_deferred_reg_done;
63
64static int watchdog_deferred_registration_add(struct watchdog_device *wdd)
65{
66 list_add_tail(&wdd->deferred,
67 &wtd_deferred_reg_list);
68 return 0;
69}
70
71static void watchdog_deferred_registration_del(struct watchdog_device *wdd)
72{
73 struct list_head *p, *n;
74 struct watchdog_device *wdd_tmp;
75
76 list_for_each_safe(p, n, &wtd_deferred_reg_list) {
77 wdd_tmp = list_entry(p, struct watchdog_device,
78 deferred);
79 if (wdd_tmp == wdd) {
80 list_del(&wdd_tmp->deferred);
81 break;
82 }
83 }
84}
85
3048253e
FP
86static void watchdog_check_min_max_timeout(struct watchdog_device *wdd)
87{
88 /*
89 * Check that we have valid min and max timeout values, if
90 * not reset them both to 0 (=not used or unknown)
91 */
92 if (wdd->min_timeout > wdd->max_timeout) {
93 pr_info("Invalid min and max timeout values, resetting to 0!\n");
94 wdd->min_timeout = 0;
95 wdd->max_timeout = 0;
96 }
97}
98
99/**
100 * watchdog_init_timeout() - initialize the timeout field
101 * @timeout_parm: timeout module parameter
102 * @dev: Device that stores the timeout-sec property
103 *
104 * Initialize the timeout field of the watchdog_device struct with either the
105 * timeout module parameter (if it is valid value) or the timeout-sec property
106 * (only if it is a valid value and the timeout_parm is out of bounds).
107 * If none of them are valid then we keep the old value (which should normally
108 * be the default timeout value.
109 *
110 * A zero is returned on success and -EINVAL for failure.
111 */
112int watchdog_init_timeout(struct watchdog_device *wdd,
113 unsigned int timeout_parm, struct device *dev)
114{
115 unsigned int t = 0;
116 int ret = 0;
117
118 watchdog_check_min_max_timeout(wdd);
119
6ffcff93 120 /* try to get the timeout module parameter first */
2c34d599 121 if (!watchdog_timeout_invalid(wdd, timeout_parm) && timeout_parm) {
3048253e
FP
122 wdd->timeout = timeout_parm;
123 return ret;
124 }
125 if (timeout_parm)
126 ret = -EINVAL;
127
128 /* try to get the timeout_sec property */
129 if (dev == NULL || dev->of_node == NULL)
130 return ret;
131 of_property_read_u32(dev->of_node, "timeout-sec", &t);
2c34d599 132 if (!watchdog_timeout_invalid(wdd, t) && t)
3048253e
FP
133 wdd->timeout = t;
134 else
135 ret = -EINVAL;
136
137 return ret;
138}
139EXPORT_SYMBOL_GPL(watchdog_init_timeout);
140
2165bf52
DR
141static int watchdog_restart_notifier(struct notifier_block *nb,
142 unsigned long action, void *data)
143{
144 struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
145 restart_nb);
146
147 int ret;
148
149 ret = wdd->ops->restart(wdd);
150 if (ret)
151 return NOTIFY_BAD;
152
153 return NOTIFY_DONE;
154}
155
156/**
157 * watchdog_set_restart_priority - Change priority of restart handler
158 * @wdd: watchdog device
159 * @priority: priority of the restart handler, should follow these guidelines:
160 * 0: use watchdog's restart function as last resort, has limited restart
161 * capabilies
162 * 128: default restart handler, use if no other handler is expected to be
163 * available and/or if restart is sufficient to restart the entire system
164 * 255: preempt all other handlers
165 *
166 * If a wdd->ops->restart function is provided when watchdog_register_device is
167 * called, it will be registered as a restart handler with the priority given
168 * here.
169 */
170void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
171{
172 wdd->restart_nb.priority = priority;
173}
174EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
175
ef90174f 176static int __watchdog_register_device(struct watchdog_device *wdd)
43316044 177{
9dd4e173 178 int ret, id = -1, devno;
43316044
WVS
179
180 if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
181 return -EINVAL;
182
183 /* Mandatory operations need to be supported */
184 if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
185 return -EINVAL;
186
3048253e 187 watchdog_check_min_max_timeout(wdd);
3f43f68e 188
43316044
WVS
189 /*
190 * Note: now that all watchdog_device data has been verified, we
191 * will not check this anymore in other functions. If data gets
192 * corrupted in a later stage then we expect a kernel panic!
193 */
194
f4e9c82f 195 mutex_init(&wdd->lock);
9dd4e173
JC
196
197 /* Use alias for watchdog id if possible */
198 if (wdd->parent) {
199 ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
200 if (ret >= 0)
201 id = ida_simple_get(&watchdog_ida, ret,
202 ret + 1, GFP_KERNEL);
203 }
204
205 if (id < 0)
206 id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
207
45f5fed3
AC
208 if (id < 0)
209 return id;
210 wdd->id = id;
211
43316044
WVS
212 ret = watchdog_dev_register(wdd);
213 if (ret) {
45f5fed3
AC
214 ida_simple_remove(&watchdog_ida, id);
215 if (!(id == 0 && ret == -EBUSY))
216 return ret;
217
218 /* Retry in case a legacy watchdog module exists */
219 id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
220 if (id < 0)
221 return id;
222 wdd->id = id;
223
224 ret = watchdog_dev_register(wdd);
225 if (ret) {
226 ida_simple_remove(&watchdog_ida, id);
227 return ret;
228 }
43316044
WVS
229 }
230
d6b469d9
AC
231 devno = wdd->cdev.dev;
232 wdd->dev = device_create(watchdog_class, wdd->parent, devno,
233 NULL, "watchdog%d", wdd->id);
234 if (IS_ERR(wdd->dev)) {
235 watchdog_dev_unregister(wdd);
236 ida_simple_remove(&watchdog_ida, id);
237 ret = PTR_ERR(wdd->dev);
238 return ret;
239 }
240
2165bf52
DR
241 if (wdd->ops->restart) {
242 wdd->restart_nb.notifier_call = watchdog_restart_notifier;
243
244 ret = register_restart_handler(&wdd->restart_nb);
245 if (ret)
246 dev_warn(wdd->dev, "Cannot register restart handler (%d)\n",
247 ret);
248 }
249
43316044
WVS
250 return 0;
251}
43316044
WVS
252
253/**
ef90174f
JBT
254 * watchdog_register_device() - register a watchdog device
255 * @wdd: watchdog device
43316044 256 *
ef90174f
JBT
257 * Register a watchdog device with the kernel so that the
258 * watchdog timer can be accessed from userspace.
259 *
260 * A zero is returned on success and a negative errno code for
261 * failure.
43316044 262 */
ef90174f
JBT
263
264int watchdog_register_device(struct watchdog_device *wdd)
265{
266 int ret;
267
268 mutex_lock(&wtd_deferred_reg_mutex);
269 if (wtd_deferred_reg_done)
270 ret = __watchdog_register_device(wdd);
271 else
272 ret = watchdog_deferred_registration_add(wdd);
273 mutex_unlock(&wtd_deferred_reg_mutex);
274 return ret;
275}
276EXPORT_SYMBOL_GPL(watchdog_register_device);
277
278static void __watchdog_unregister_device(struct watchdog_device *wdd)
43316044
WVS
279{
280 int ret;
b232a70a 281 int devno;
43316044
WVS
282
283 if (wdd == NULL)
284 return;
285
2165bf52
DR
286 if (wdd->ops->restart)
287 unregister_restart_handler(&wdd->restart_nb);
288
b232a70a 289 devno = wdd->cdev.dev;
43316044
WVS
290 ret = watchdog_dev_unregister(wdd);
291 if (ret)
27c766aa 292 pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
d6b469d9 293 device_destroy(watchdog_class, devno);
45f5fed3 294 ida_simple_remove(&watchdog_ida, wdd->id);
d6b469d9 295 wdd->dev = NULL;
43316044 296}
ef90174f
JBT
297
298/**
299 * watchdog_unregister_device() - unregister a watchdog device
300 * @wdd: watchdog device to unregister
301 *
302 * Unregister a watchdog device that was previously successfully
303 * registered with watchdog_register_device().
304 */
305
306void watchdog_unregister_device(struct watchdog_device *wdd)
307{
308 mutex_lock(&wtd_deferred_reg_mutex);
309 if (wtd_deferred_reg_done)
310 __watchdog_unregister_device(wdd);
311 else
312 watchdog_deferred_registration_del(wdd);
313 mutex_unlock(&wtd_deferred_reg_mutex);
314}
315
43316044
WVS
316EXPORT_SYMBOL_GPL(watchdog_unregister_device);
317
ef90174f
JBT
318static int __init watchdog_deferred_registration(void)
319{
320 mutex_lock(&wtd_deferred_reg_mutex);
321 wtd_deferred_reg_done = true;
322 while (!list_empty(&wtd_deferred_reg_list)) {
323 struct watchdog_device *wdd;
324
325 wdd = list_first_entry(&wtd_deferred_reg_list,
326 struct watchdog_device, deferred);
327 list_del(&wdd->deferred);
328 __watchdog_register_device(wdd);
329 }
330 mutex_unlock(&wtd_deferred_reg_mutex);
331 return 0;
332}
333
45f5fed3
AC
334static int __init watchdog_init(void)
335{
d6b469d9
AC
336 int err;
337
338 watchdog_class = class_create(THIS_MODULE, "watchdog");
339 if (IS_ERR(watchdog_class)) {
340 pr_err("couldn't create class\n");
341 return PTR_ERR(watchdog_class);
342 }
343
344 err = watchdog_dev_init();
345 if (err < 0) {
346 class_destroy(watchdog_class);
347 return err;
348 }
349
ef90174f 350 watchdog_deferred_registration();
d6b469d9 351 return 0;
45f5fed3
AC
352}
353
354static void __exit watchdog_exit(void)
355{
356 watchdog_dev_exit();
d6b469d9 357 class_destroy(watchdog_class);
45f5fed3
AC
358 ida_destroy(&watchdog_ida);
359}
360
ef90174f 361subsys_initcall_sync(watchdog_init);
45f5fed3
AC
362module_exit(watchdog_exit);
363
43316044
WVS
364MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
365MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
366MODULE_DESCRIPTION("WatchDog Timer Driver Core");
367MODULE_LICENSE("GPL");
This page took 0.498287 seconds and 5 git commands to generate.