watchdog: Add Locking support
[deliverable/linux.git] / drivers / watchdog / watchdog_dev.c
CommitLineData
43316044
WVS
1/*
2 * watchdog_dev.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 *
10 * This source code is part of the generic code that can be used
11 * by all the watchdog timer drivers.
12 *
13 * This part of the generic code takes care of the following
14 * misc device: /dev/watchdog.
15 *
16 * Based on source code of the following authors:
17 * Matt Domsch <Matt_Domsch@dell.com>,
18 * Rob Radez <rob@osinvestor.com>,
19 * Rusty Lynch <rusty@linux.co.intel.com>
20 * Satyam Sharma <satyam@infradead.org>
21 * Randy Dunlap <randy.dunlap@oracle.com>
22 *
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.
27 *
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.
31 */
32
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
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/... */
44
6cfb5aa8 45#include "watchdog_core.h"
09a46e73 46
45f5fed3
AC
47/* the dev_t structure to store the dynamically allocated watchdog devices */
48static dev_t watchdog_devt;
43316044 49/* the watchdog device behind /dev/watchdog */
45f5fed3 50static struct watchdog_device *old_wdd;
43316044
WVS
51
52/*
53 * watchdog_ping: ping the watchdog.
54 * @wddev: the watchdog device to ping
55 *
56 * If the watchdog has no own ping operation then it needs to be
57 * restarted via the start operation. This wrapper function does
58 * exactly that.
234445b4 59 * We only ping when the watchdog device is running.
43316044
WVS
60 */
61
62static int watchdog_ping(struct watchdog_device *wddev)
63{
7a879824
HG
64 int err = 0;
65
f4e9c82f
HG
66 mutex_lock(&wddev->lock);
67
7a879824
HG
68 if (!watchdog_active(wddev))
69 goto out_ping;
70
71 if (wddev->ops->ping)
72 err = wddev->ops->ping(wddev); /* ping the watchdog */
73 else
74 err = wddev->ops->start(wddev); /* restart watchdog */
75
76out_ping:
f4e9c82f 77 mutex_unlock(&wddev->lock);
7a879824 78 return err;
234445b4
WVS
79}
80
81/*
82 * watchdog_start: wrapper to start the watchdog.
83 * @wddev: the watchdog device to start
84 *
85 * Start the watchdog if it is not active and mark it active.
86 * This function returns zero on success or a negative errno code for
87 * failure.
88 */
89
90static int watchdog_start(struct watchdog_device *wddev)
91{
7a879824 92 int err = 0;
234445b4 93
f4e9c82f
HG
94 mutex_lock(&wddev->lock);
95
7a879824
HG
96 if (watchdog_active(wddev))
97 goto out_start;
234445b4 98
7a879824
HG
99 err = wddev->ops->start(wddev);
100 if (err == 0)
cb7efc02 101 set_bit(WDOG_ACTIVE, &wddev->status);
7a879824
HG
102
103out_start:
f4e9c82f 104 mutex_unlock(&wddev->lock);
7a879824 105 return err;
234445b4
WVS
106}
107
108/*
109 * watchdog_stop: wrapper to stop the watchdog.
110 * @wddev: the watchdog device to stop
111 *
112 * Stop the watchdog if it is still active and unmark it active.
113 * This function returns zero on success or a negative errno code for
114 * failure.
7e192b9c 115 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
234445b4
WVS
116 */
117
118static int watchdog_stop(struct watchdog_device *wddev)
119{
7a879824
HG
120 int err = 0;
121
f4e9c82f
HG
122 mutex_lock(&wddev->lock);
123
7a879824
HG
124 if (!watchdog_active(wddev))
125 goto out_stop;
7e192b9c 126
cb7efc02 127 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
3dfd6218 128 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
7a879824
HG
129 err = -EBUSY;
130 goto out_stop;
7e192b9c 131 }
234445b4 132
7a879824
HG
133 err = wddev->ops->stop(wddev);
134 if (err == 0)
cb7efc02 135 clear_bit(WDOG_ACTIVE, &wddev->status);
7a879824
HG
136
137out_stop:
f4e9c82f 138 mutex_unlock(&wddev->lock);
7a879824
HG
139 return err;
140}
141
142/*
143 * watchdog_get_status: wrapper to get the watchdog status
144 * @wddev: the watchdog device to get the status from
145 * @status: the status of the watchdog device
146 *
147 * Get the watchdog's status flags.
148 */
149
150static int watchdog_get_status(struct watchdog_device *wddev,
151 unsigned int *status)
152{
153 int err = 0;
154
155 *status = 0;
156 if (!wddev->ops->status)
157 return -EOPNOTSUPP;
158
f4e9c82f
HG
159 mutex_lock(&wddev->lock);
160
7a879824
HG
161 *status = wddev->ops->status(wddev);
162
f4e9c82f 163 mutex_unlock(&wddev->lock);
7a879824
HG
164 return err;
165}
166
167/*
168 * watchdog_set_timeout: set the watchdog timer timeout
169 * @wddev: the watchdog device to set the timeout for
170 * @timeout: timeout to set in seconds
171 */
172
173static int watchdog_set_timeout(struct watchdog_device *wddev,
174 unsigned int timeout)
175{
176 int err;
177
178 if ((wddev->ops->set_timeout == NULL) ||
179 !(wddev->info->options & WDIOF_SETTIMEOUT))
180 return -EOPNOTSUPP;
181
182 if ((wddev->max_timeout != 0) &&
183 (timeout < wddev->min_timeout || timeout > wddev->max_timeout))
184 return -EINVAL;
185
f4e9c82f
HG
186 mutex_lock(&wddev->lock);
187
7a879824
HG
188 err = wddev->ops->set_timeout(wddev, timeout);
189
f4e9c82f 190 mutex_unlock(&wddev->lock);
7a879824
HG
191 return err;
192}
193
194/*
195 * watchdog_get_timeleft: wrapper to get the time left before a reboot
196 * @wddev: the watchdog device to get the remaining time from
197 * @timeleft: the time that's left
198 *
199 * Get the time before a watchdog will reboot (if not pinged).
200 */
201
202static int watchdog_get_timeleft(struct watchdog_device *wddev,
203 unsigned int *timeleft)
204{
205 int err = 0;
206
207 *timeleft = 0;
208 if (!wddev->ops->get_timeleft)
209 return -EOPNOTSUPP;
210
f4e9c82f
HG
211 mutex_lock(&wddev->lock);
212
7a879824
HG
213 *timeleft = wddev->ops->get_timeleft(wddev);
214
f4e9c82f 215 mutex_unlock(&wddev->lock);
7a879824
HG
216 return err;
217}
218
219/*
220 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
221 * @wddev: the watchdog device to do the ioctl on
222 * @cmd: watchdog command
223 * @arg: argument pointer
224 */
225
226static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
227 unsigned long arg)
228{
229 int err;
230
231 if (!wddev->ops->ioctl)
232 return -ENOIOCTLCMD;
233
f4e9c82f
HG
234 mutex_lock(&wddev->lock);
235
7a879824
HG
236 err = wddev->ops->ioctl(wddev, cmd, arg);
237
f4e9c82f 238 mutex_unlock(&wddev->lock);
7a879824 239 return err;
43316044
WVS
240}
241
242/*
243 * watchdog_write: writes to the watchdog.
244 * @file: file from VFS
245 * @data: user address of data
246 * @len: length of data
247 * @ppos: pointer to the file offset
248 *
249 * A write to a watchdog device is defined as a keepalive ping.
017cf080 250 * Writing the magic 'V' sequence allows the next close to turn
7e192b9c 251 * off the watchdog (if 'nowayout' is not set).
43316044
WVS
252 */
253
254static ssize_t watchdog_write(struct file *file, const char __user *data,
255 size_t len, loff_t *ppos)
256{
45f5fed3 257 struct watchdog_device *wdd = file->private_data;
43316044
WVS
258 size_t i;
259 char c;
260
261 if (len == 0)
262 return 0;
263
017cf080
WVS
264 /*
265 * Note: just in case someone wrote the magic character
266 * five months ago...
267 */
268 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
269
270 /* scan to see whether or not we got the magic character */
43316044
WVS
271 for (i = 0; i != len; i++) {
272 if (get_user(c, data + i))
273 return -EFAULT;
017cf080
WVS
274 if (c == 'V')
275 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
43316044
WVS
276 }
277
278 /* someone wrote to us, so we send the watchdog a keepalive ping */
279 watchdog_ping(wdd);
280
281 return len;
282}
283
2fa03560
WVS
284/*
285 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
286 * @file: file handle to the device
287 * @cmd: watchdog command
288 * @arg: argument pointer
289 *
290 * The watchdog API defines a common set of functions for all watchdogs
291 * according to their available features.
292 */
293
294static long watchdog_ioctl(struct file *file, unsigned int cmd,
295 unsigned long arg)
296{
45f5fed3 297 struct watchdog_device *wdd = file->private_data;
2fa03560
WVS
298 void __user *argp = (void __user *)arg;
299 int __user *p = argp;
300 unsigned int val;
234445b4 301 int err;
2fa03560 302
7a879824
HG
303 err = watchdog_ioctl_op(wdd, cmd, arg);
304 if (err != -ENOIOCTLCMD)
305 return err;
78d88fc0 306
2fa03560
WVS
307 switch (cmd) {
308 case WDIOC_GETSUPPORT:
309 return copy_to_user(argp, wdd->info,
310 sizeof(struct watchdog_info)) ? -EFAULT : 0;
311 case WDIOC_GETSTATUS:
7a879824
HG
312 err = watchdog_get_status(wdd, &val);
313 if (err)
314 return err;
2fa03560
WVS
315 return put_user(val, p);
316 case WDIOC_GETBOOTSTATUS:
317 return put_user(wdd->bootstatus, p);
234445b4
WVS
318 case WDIOC_SETOPTIONS:
319 if (get_user(val, p))
320 return -EFAULT;
321 if (val & WDIOS_DISABLECARD) {
322 err = watchdog_stop(wdd);
323 if (err < 0)
324 return err;
325 }
326 if (val & WDIOS_ENABLECARD) {
327 err = watchdog_start(wdd);
328 if (err < 0)
329 return err;
330 }
331 return 0;
c2dc00e4
WVS
332 case WDIOC_KEEPALIVE:
333 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
334 return -EOPNOTSUPP;
335 watchdog_ping(wdd);
336 return 0;
014d694e 337 case WDIOC_SETTIMEOUT:
014d694e
WVS
338 if (get_user(val, p))
339 return -EFAULT;
7a879824 340 err = watchdog_set_timeout(wdd, val);
014d694e
WVS
341 if (err < 0)
342 return err;
014d694e
WVS
343 /* If the watchdog is active then we send a keepalive ping
344 * to make sure that the watchdog keep's running (and if
345 * possible that it takes the new timeout) */
346 watchdog_ping(wdd);
347 /* Fall */
348 case WDIOC_GETTIMEOUT:
349 /* timeout == 0 means that we don't know the timeout */
350 if (wdd->timeout == 0)
351 return -EOPNOTSUPP;
352 return put_user(wdd->timeout, p);
fd7b673c 353 case WDIOC_GETTIMELEFT:
7a879824
HG
354 err = watchdog_get_timeleft(wdd, &val);
355 if (err)
356 return err;
357 return put_user(val, p);
2fa03560
WVS
358 default:
359 return -ENOTTY;
360 }
361}
362
43316044 363/*
45f5fed3 364 * watchdog_open: open the /dev/watchdog* devices.
43316044
WVS
365 * @inode: inode of device
366 * @file: file handle to device
367 *
45f5fed3 368 * When the /dev/watchdog* device gets opened, we start the watchdog.
43316044
WVS
369 * Watch out: the /dev/watchdog device is single open, so we make sure
370 * it can only be opened once.
371 */
372
373static int watchdog_open(struct inode *inode, struct file *file)
374{
375 int err = -EBUSY;
45f5fed3
AC
376 struct watchdog_device *wdd;
377
378 /* Get the corresponding watchdog device */
379 if (imajor(inode) == MISC_MAJOR)
380 wdd = old_wdd;
381 else
382 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
43316044
WVS
383
384 /* the watchdog is single open! */
385 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
386 return -EBUSY;
387
388 /*
389 * If the /dev/watchdog device is open, we don't want the module
390 * to be unloaded.
391 */
392 if (!try_module_get(wdd->ops->owner))
393 goto out;
394
234445b4 395 err = watchdog_start(wdd);
43316044
WVS
396 if (err < 0)
397 goto out_mod;
398
45f5fed3
AC
399 file->private_data = wdd;
400
43316044
WVS
401 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
402 return nonseekable_open(inode, file);
403
404out_mod:
405 module_put(wdd->ops->owner);
406out:
407 clear_bit(WDOG_DEV_OPEN, &wdd->status);
408 return err;
409}
410
411/*
45f5fed3
AC
412 * watchdog_release: release the watchdog device.
413 * @inode: inode of device
414 * @file: file handle to device
43316044 415 *
017cf080 416 * This is the code for when /dev/watchdog gets closed. We will only
7e192b9c
WVS
417 * stop the watchdog when we have received the magic char (and nowayout
418 * was not set), else the watchdog will keep running.
43316044
WVS
419 */
420
421static int watchdog_release(struct inode *inode, struct file *file)
422{
45f5fed3 423 struct watchdog_device *wdd = file->private_data;
017cf080
WVS
424 int err = -EBUSY;
425
426 /*
427 * We only stop the watchdog if we received the magic character
7e192b9c
WVS
428 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
429 * watchdog_stop will fail.
017cf080
WVS
430 */
431 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
432 !(wdd->info->options & WDIOF_MAGICCLOSE))
433 err = watchdog_stop(wdd);
43316044 434
017cf080 435 /* If the watchdog was not stopped, send a keepalive ping */
234445b4 436 if (err < 0) {
3dfd6218 437 dev_crit(wdd->dev, "watchdog did not stop!\n");
43316044
WVS
438 watchdog_ping(wdd);
439 }
440
441 /* Allow the owner module to be unloaded again */
442 module_put(wdd->ops->owner);
443
444 /* make sure that /dev/watchdog can be re-opened */
445 clear_bit(WDOG_DEV_OPEN, &wdd->status);
446
447 return 0;
448}
449
450static const struct file_operations watchdog_fops = {
451 .owner = THIS_MODULE,
452 .write = watchdog_write,
2fa03560 453 .unlocked_ioctl = watchdog_ioctl,
43316044
WVS
454 .open = watchdog_open,
455 .release = watchdog_release,
456};
457
458static struct miscdevice watchdog_miscdev = {
459 .minor = WATCHDOG_MINOR,
460 .name = "watchdog",
461 .fops = &watchdog_fops,
462};
463
464/*
45f5fed3 465 * watchdog_dev_register: register a watchdog device
43316044
WVS
466 * @watchdog: watchdog device
467 *
45f5fed3
AC
468 * Register a watchdog device including handling the legacy
469 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
470 * thus we set it up like that.
43316044
WVS
471 */
472
473int watchdog_dev_register(struct watchdog_device *watchdog)
474{
45f5fed3
AC
475 int err, devno;
476
477 if (watchdog->id == 0) {
d6b469d9 478 watchdog_miscdev.parent = watchdog->parent;
45f5fed3
AC
479 err = misc_register(&watchdog_miscdev);
480 if (err != 0) {
481 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
482 watchdog->info->identity, WATCHDOG_MINOR, err);
483 if (err == -EBUSY)
484 pr_err("%s: a legacy watchdog module is probably present.\n",
485 watchdog->info->identity);
486 return err;
487 }
488 old_wdd = watchdog;
43316044
WVS
489 }
490
45f5fed3
AC
491 /* Fill in the data structures */
492 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
493 cdev_init(&watchdog->cdev, &watchdog_fops);
494 watchdog->cdev.owner = watchdog->ops->owner;
495
496 /* Add the device */
497 err = cdev_add(&watchdog->cdev, devno, 1);
498 if (err) {
499 pr_err("watchdog%d unable to add device %d:%d\n",
500 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
501 if (watchdog->id == 0) {
502 misc_deregister(&watchdog_miscdev);
503 old_wdd = NULL;
504 }
43316044 505 }
43316044
WVS
506 return err;
507}
508
509/*
45f5fed3 510 * watchdog_dev_unregister: unregister a watchdog device
43316044
WVS
511 * @watchdog: watchdog device
512 *
45f5fed3 513 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
43316044
WVS
514 */
515
516int watchdog_dev_unregister(struct watchdog_device *watchdog)
517{
45f5fed3
AC
518 cdev_del(&watchdog->cdev);
519 if (watchdog->id == 0) {
520 misc_deregister(&watchdog_miscdev);
521 old_wdd = NULL;
43316044 522 }
43316044
WVS
523 return 0;
524}
45f5fed3
AC
525
526/*
527 * watchdog_dev_init: init dev part of watchdog core
528 *
529 * Allocate a range of chardev nodes to use for watchdog devices
530 */
531
532int __init watchdog_dev_init(void)
533{
534 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
535 if (err < 0)
536 pr_err("watchdog: unable to allocate char dev region\n");
537 return err;
538}
539
540/*
541 * watchdog_dev_exit: exit dev part of watchdog core
542 *
543 * Release the range of chardev nodes used for watchdog devices
544 */
545
546void __exit watchdog_dev_exit(void)
547{
548 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
549}
This page took 0.115423 seconds and 5 git commands to generate.