watchdog: qcom-wdt: Do not set 'dev' in struct watchdog_device
[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
e1313196
DR
141static int watchdog_reboot_notifier(struct notifier_block *nb,
142 unsigned long code, void *data)
143{
144 struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
145 reboot_nb);
146
147 if (code == SYS_DOWN || code == SYS_HALT) {
148 if (watchdog_active(wdd)) {
149 int ret;
150
151 ret = wdd->ops->stop(wdd);
152 if (ret)
153 return NOTIFY_BAD;
154 }
155 }
156
157 return NOTIFY_DONE;
158}
159
2165bf52
DR
160static int watchdog_restart_notifier(struct notifier_block *nb,
161 unsigned long action, void *data)
162{
163 struct watchdog_device *wdd = container_of(nb, struct watchdog_device,
164 restart_nb);
165
166 int ret;
167
168 ret = wdd->ops->restart(wdd);
169 if (ret)
170 return NOTIFY_BAD;
171
172 return NOTIFY_DONE;
173}
174
175/**
176 * watchdog_set_restart_priority - Change priority of restart handler
177 * @wdd: watchdog device
178 * @priority: priority of the restart handler, should follow these guidelines:
179 * 0: use watchdog's restart function as last resort, has limited restart
180 * capabilies
181 * 128: default restart handler, use if no other handler is expected to be
182 * available and/or if restart is sufficient to restart the entire system
183 * 255: preempt all other handlers
184 *
185 * If a wdd->ops->restart function is provided when watchdog_register_device is
186 * called, it will be registered as a restart handler with the priority given
187 * here.
188 */
189void watchdog_set_restart_priority(struct watchdog_device *wdd, int priority)
190{
191 wdd->restart_nb.priority = priority;
192}
193EXPORT_SYMBOL_GPL(watchdog_set_restart_priority);
194
ef90174f 195static int __watchdog_register_device(struct watchdog_device *wdd)
43316044 196{
9dd4e173 197 int ret, id = -1, devno;
43316044
WVS
198
199 if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
200 return -EINVAL;
201
202 /* Mandatory operations need to be supported */
203 if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
204 return -EINVAL;
205
3048253e 206 watchdog_check_min_max_timeout(wdd);
3f43f68e 207
43316044
WVS
208 /*
209 * Note: now that all watchdog_device data has been verified, we
210 * will not check this anymore in other functions. If data gets
211 * corrupted in a later stage then we expect a kernel panic!
212 */
213
f4e9c82f 214 mutex_init(&wdd->lock);
9dd4e173
JC
215
216 /* Use alias for watchdog id if possible */
217 if (wdd->parent) {
218 ret = of_alias_get_id(wdd->parent->of_node, "watchdog");
219 if (ret >= 0)
220 id = ida_simple_get(&watchdog_ida, ret,
221 ret + 1, GFP_KERNEL);
222 }
223
224 if (id < 0)
225 id = ida_simple_get(&watchdog_ida, 0, MAX_DOGS, GFP_KERNEL);
226
45f5fed3
AC
227 if (id < 0)
228 return id;
229 wdd->id = id;
230
43316044
WVS
231 ret = watchdog_dev_register(wdd);
232 if (ret) {
45f5fed3
AC
233 ida_simple_remove(&watchdog_ida, id);
234 if (!(id == 0 && ret == -EBUSY))
235 return ret;
236
237 /* Retry in case a legacy watchdog module exists */
238 id = ida_simple_get(&watchdog_ida, 1, MAX_DOGS, GFP_KERNEL);
239 if (id < 0)
240 return id;
241 wdd->id = id;
242
243 ret = watchdog_dev_register(wdd);
244 if (ret) {
245 ida_simple_remove(&watchdog_ida, id);
246 return ret;
247 }
43316044
WVS
248 }
249
d6b469d9
AC
250 devno = wdd->cdev.dev;
251 wdd->dev = device_create(watchdog_class, wdd->parent, devno,
33b71126 252 wdd, "watchdog%d", wdd->id);
d6b469d9
AC
253 if (IS_ERR(wdd->dev)) {
254 watchdog_dev_unregister(wdd);
255 ida_simple_remove(&watchdog_ida, id);
256 ret = PTR_ERR(wdd->dev);
257 return ret;
258 }
259
e1313196
DR
260 if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status)) {
261 wdd->reboot_nb.notifier_call = watchdog_reboot_notifier;
262
263 ret = register_reboot_notifier(&wdd->reboot_nb);
264 if (ret) {
265 dev_err(wdd->dev, "Cannot register reboot notifier (%d)\n",
266 ret);
267 watchdog_dev_unregister(wdd);
268 device_destroy(watchdog_class, devno);
269 ida_simple_remove(&watchdog_ida, wdd->id);
270 wdd->dev = NULL;
271 return ret;
272 }
273 }
274
2165bf52
DR
275 if (wdd->ops->restart) {
276 wdd->restart_nb.notifier_call = watchdog_restart_notifier;
277
278 ret = register_restart_handler(&wdd->restart_nb);
279 if (ret)
280 dev_warn(wdd->dev, "Cannot register restart handler (%d)\n",
281 ret);
282 }
283
43316044
WVS
284 return 0;
285}
43316044
WVS
286
287/**
ef90174f
JBT
288 * watchdog_register_device() - register a watchdog device
289 * @wdd: watchdog device
43316044 290 *
ef90174f
JBT
291 * Register a watchdog device with the kernel so that the
292 * watchdog timer can be accessed from userspace.
293 *
294 * A zero is returned on success and a negative errno code for
295 * failure.
43316044 296 */
ef90174f
JBT
297
298int watchdog_register_device(struct watchdog_device *wdd)
299{
300 int ret;
301
302 mutex_lock(&wtd_deferred_reg_mutex);
303 if (wtd_deferred_reg_done)
304 ret = __watchdog_register_device(wdd);
305 else
306 ret = watchdog_deferred_registration_add(wdd);
307 mutex_unlock(&wtd_deferred_reg_mutex);
308 return ret;
309}
310EXPORT_SYMBOL_GPL(watchdog_register_device);
311
312static void __watchdog_unregister_device(struct watchdog_device *wdd)
43316044
WVS
313{
314 int ret;
b232a70a 315 int devno;
43316044
WVS
316
317 if (wdd == NULL)
318 return;
319
2165bf52
DR
320 if (wdd->ops->restart)
321 unregister_restart_handler(&wdd->restart_nb);
322
e1313196
DR
323 if (test_bit(WDOG_STOP_ON_REBOOT, &wdd->status))
324 unregister_reboot_notifier(&wdd->reboot_nb);
325
b232a70a 326 devno = wdd->cdev.dev;
43316044
WVS
327 ret = watchdog_dev_unregister(wdd);
328 if (ret)
27c766aa 329 pr_err("error unregistering /dev/watchdog (err=%d)\n", ret);
d6b469d9 330 device_destroy(watchdog_class, devno);
45f5fed3 331 ida_simple_remove(&watchdog_ida, wdd->id);
d6b469d9 332 wdd->dev = NULL;
43316044 333}
ef90174f
JBT
334
335/**
336 * watchdog_unregister_device() - unregister a watchdog device
337 * @wdd: watchdog device to unregister
338 *
339 * Unregister a watchdog device that was previously successfully
340 * registered with watchdog_register_device().
341 */
342
343void watchdog_unregister_device(struct watchdog_device *wdd)
344{
345 mutex_lock(&wtd_deferred_reg_mutex);
346 if (wtd_deferred_reg_done)
347 __watchdog_unregister_device(wdd);
348 else
349 watchdog_deferred_registration_del(wdd);
350 mutex_unlock(&wtd_deferred_reg_mutex);
351}
352
43316044
WVS
353EXPORT_SYMBOL_GPL(watchdog_unregister_device);
354
ef90174f
JBT
355static int __init watchdog_deferred_registration(void)
356{
357 mutex_lock(&wtd_deferred_reg_mutex);
358 wtd_deferred_reg_done = true;
359 while (!list_empty(&wtd_deferred_reg_list)) {
360 struct watchdog_device *wdd;
361
362 wdd = list_first_entry(&wtd_deferred_reg_list,
363 struct watchdog_device, deferred);
364 list_del(&wdd->deferred);
365 __watchdog_register_device(wdd);
366 }
367 mutex_unlock(&wtd_deferred_reg_mutex);
368 return 0;
369}
370
45f5fed3
AC
371static int __init watchdog_init(void)
372{
906d7a5c
PA
373 watchdog_class = watchdog_dev_init();
374 if (IS_ERR(watchdog_class))
d6b469d9 375 return PTR_ERR(watchdog_class);
d6b469d9 376
ef90174f 377 watchdog_deferred_registration();
d6b469d9 378 return 0;
45f5fed3
AC
379}
380
381static void __exit watchdog_exit(void)
382{
383 watchdog_dev_exit();
384 ida_destroy(&watchdog_ida);
385}
386
ef90174f 387subsys_initcall_sync(watchdog_init);
45f5fed3
AC
388module_exit(watchdog_exit);
389
43316044
WVS
390MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
391MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
392MODULE_DESCRIPTION("WatchDog Timer Driver Core");
393MODULE_LICENSE("GPL");
This page took 0.425746 seconds and 5 git commands to generate.