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