blob: 76d2572fed258a733e42d26725963f029e89a76a [file] [log] [blame]
Wim Van Sebroeck43316042011-07-22 18:55:18 +00001/*
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
Wim Van Sebroeck6cfb5aa2012-05-21 15:31:06 +020045#include "watchdog_core.h"
H Hartley Sweeten09a46e72012-04-20 13:28:24 -070046
Alan Cox45f5fed2012-05-10 21:48:59 +020047/* the dev_t structure to store the dynamically allocated watchdog devices */
48static dev_t watchdog_devt;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000049/* the watchdog device behind /dev/watchdog */
Alan Cox45f5fed2012-05-10 21:48:59 +020050static struct watchdog_device *old_wdd;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000051
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.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000059 * We only ping when the watchdog device is running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +000060 */
61
62static int watchdog_ping(struct watchdog_device *wddev)
63{
Hans de Goede7a879822012-05-22 11:40:26 +020064 int err = 0;
65
66 if (!watchdog_active(wddev))
67 goto out_ping;
68
69 if (wddev->ops->ping)
70 err = wddev->ops->ping(wddev); /* ping the watchdog */
71 else
72 err = wddev->ops->start(wddev); /* restart watchdog */
73
74out_ping:
75 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000076}
77
78/*
79 * watchdog_start: wrapper to start the watchdog.
80 * @wddev: the watchdog device to start
81 *
82 * Start the watchdog if it is not active and mark it active.
83 * This function returns zero on success or a negative errno code for
84 * failure.
85 */
86
87static int watchdog_start(struct watchdog_device *wddev)
88{
Hans de Goede7a879822012-05-22 11:40:26 +020089 int err = 0;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000090
Hans de Goede7a879822012-05-22 11:40:26 +020091 if (watchdog_active(wddev))
92 goto out_start;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000093
Hans de Goede7a879822012-05-22 11:40:26 +020094 err = wddev->ops->start(wddev);
95 if (err == 0)
H Hartley Sweetencb7efc02011-08-03 15:38:20 -070096 set_bit(WDOG_ACTIVE, &wddev->status);
Hans de Goede7a879822012-05-22 11:40:26 +020097
98out_start:
99 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000100}
101
102/*
103 * watchdog_stop: wrapper to stop the watchdog.
104 * @wddev: the watchdog device to stop
105 *
106 * Stop the watchdog if it is still active and unmark it active.
107 * This function returns zero on success or a negative errno code for
108 * failure.
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000109 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000110 */
111
112static int watchdog_stop(struct watchdog_device *wddev)
113{
Hans de Goede7a879822012-05-22 11:40:26 +0200114 int err = 0;
115
116 if (!watchdog_active(wddev))
117 goto out_stop;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000118
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700119 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
Alan Cox3dfd6212012-05-11 12:00:22 +0200120 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
Hans de Goede7a879822012-05-22 11:40:26 +0200121 err = -EBUSY;
122 goto out_stop;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000123 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000124
Hans de Goede7a879822012-05-22 11:40:26 +0200125 err = wddev->ops->stop(wddev);
126 if (err == 0)
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700127 clear_bit(WDOG_ACTIVE, &wddev->status);
Hans de Goede7a879822012-05-22 11:40:26 +0200128
129out_stop:
130 return err;
131}
132
133/*
134 * watchdog_get_status: wrapper to get the watchdog status
135 * @wddev: the watchdog device to get the status from
136 * @status: the status of the watchdog device
137 *
138 * Get the watchdog's status flags.
139 */
140
141static int watchdog_get_status(struct watchdog_device *wddev,
142 unsigned int *status)
143{
144 int err = 0;
145
146 *status = 0;
147 if (!wddev->ops->status)
148 return -EOPNOTSUPP;
149
150 *status = wddev->ops->status(wddev);
151
152 return err;
153}
154
155/*
156 * watchdog_set_timeout: set the watchdog timer timeout
157 * @wddev: the watchdog device to set the timeout for
158 * @timeout: timeout to set in seconds
159 */
160
161static int watchdog_set_timeout(struct watchdog_device *wddev,
162 unsigned int timeout)
163{
164 int err;
165
166 if ((wddev->ops->set_timeout == NULL) ||
167 !(wddev->info->options & WDIOF_SETTIMEOUT))
168 return -EOPNOTSUPP;
169
170 if ((wddev->max_timeout != 0) &&
171 (timeout < wddev->min_timeout || timeout > wddev->max_timeout))
172 return -EINVAL;
173
174 err = wddev->ops->set_timeout(wddev, timeout);
175
176 return err;
177}
178
179/*
180 * watchdog_get_timeleft: wrapper to get the time left before a reboot
181 * @wddev: the watchdog device to get the remaining time from
182 * @timeleft: the time that's left
183 *
184 * Get the time before a watchdog will reboot (if not pinged).
185 */
186
187static int watchdog_get_timeleft(struct watchdog_device *wddev,
188 unsigned int *timeleft)
189{
190 int err = 0;
191
192 *timeleft = 0;
193 if (!wddev->ops->get_timeleft)
194 return -EOPNOTSUPP;
195
196 *timeleft = wddev->ops->get_timeleft(wddev);
197
198 return err;
199}
200
201/*
202 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
203 * @wddev: the watchdog device to do the ioctl on
204 * @cmd: watchdog command
205 * @arg: argument pointer
206 */
207
208static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
209 unsigned long arg)
210{
211 int err;
212
213 if (!wddev->ops->ioctl)
214 return -ENOIOCTLCMD;
215
216 err = wddev->ops->ioctl(wddev, cmd, arg);
217
218 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000219}
220
221/*
222 * watchdog_write: writes to the watchdog.
223 * @file: file from VFS
224 * @data: user address of data
225 * @len: length of data
226 * @ppos: pointer to the file offset
227 *
228 * A write to a watchdog device is defined as a keepalive ping.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000229 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000230 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000231 */
232
233static ssize_t watchdog_write(struct file *file, const char __user *data,
234 size_t len, loff_t *ppos)
235{
Alan Cox45f5fed2012-05-10 21:48:59 +0200236 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000237 size_t i;
238 char c;
239
240 if (len == 0)
241 return 0;
242
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000243 /*
244 * Note: just in case someone wrote the magic character
245 * five months ago...
246 */
247 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
248
249 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000250 for (i = 0; i != len; i++) {
251 if (get_user(c, data + i))
252 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000253 if (c == 'V')
254 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000255 }
256
257 /* someone wrote to us, so we send the watchdog a keepalive ping */
258 watchdog_ping(wdd);
259
260 return len;
261}
262
263/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000264 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
265 * @file: file handle to the device
266 * @cmd: watchdog command
267 * @arg: argument pointer
268 *
269 * The watchdog API defines a common set of functions for all watchdogs
270 * according to their available features.
271 */
272
273static long watchdog_ioctl(struct file *file, unsigned int cmd,
274 unsigned long arg)
275{
Alan Cox45f5fed2012-05-10 21:48:59 +0200276 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000277 void __user *argp = (void __user *)arg;
278 int __user *p = argp;
279 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000280 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000281
Hans de Goede7a879822012-05-22 11:40:26 +0200282 err = watchdog_ioctl_op(wdd, cmd, arg);
283 if (err != -ENOIOCTLCMD)
284 return err;
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000285
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000286 switch (cmd) {
287 case WDIOC_GETSUPPORT:
288 return copy_to_user(argp, wdd->info,
289 sizeof(struct watchdog_info)) ? -EFAULT : 0;
290 case WDIOC_GETSTATUS:
Hans de Goede7a879822012-05-22 11:40:26 +0200291 err = watchdog_get_status(wdd, &val);
292 if (err)
293 return err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000294 return put_user(val, p);
295 case WDIOC_GETBOOTSTATUS:
296 return put_user(wdd->bootstatus, p);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000297 case WDIOC_SETOPTIONS:
298 if (get_user(val, p))
299 return -EFAULT;
300 if (val & WDIOS_DISABLECARD) {
301 err = watchdog_stop(wdd);
302 if (err < 0)
303 return err;
304 }
305 if (val & WDIOS_ENABLECARD) {
306 err = watchdog_start(wdd);
307 if (err < 0)
308 return err;
309 }
310 return 0;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000311 case WDIOC_KEEPALIVE:
312 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
313 return -EOPNOTSUPP;
314 watchdog_ping(wdd);
315 return 0;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000316 case WDIOC_SETTIMEOUT:
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000317 if (get_user(val, p))
318 return -EFAULT;
Hans de Goede7a879822012-05-22 11:40:26 +0200319 err = watchdog_set_timeout(wdd, val);
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000320 if (err < 0)
321 return err;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000322 /* If the watchdog is active then we send a keepalive ping
323 * to make sure that the watchdog keep's running (and if
324 * possible that it takes the new timeout) */
325 watchdog_ping(wdd);
326 /* Fall */
327 case WDIOC_GETTIMEOUT:
328 /* timeout == 0 means that we don't know the timeout */
329 if (wdd->timeout == 0)
330 return -EOPNOTSUPP;
331 return put_user(wdd->timeout, p);
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100332 case WDIOC_GETTIMELEFT:
Hans de Goede7a879822012-05-22 11:40:26 +0200333 err = watchdog_get_timeleft(wdd, &val);
334 if (err)
335 return err;
336 return put_user(val, p);
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000337 default:
338 return -ENOTTY;
339 }
340}
341
342/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200343 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000344 * @inode: inode of device
345 * @file: file handle to device
346 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200347 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000348 * Watch out: the /dev/watchdog device is single open, so we make sure
349 * it can only be opened once.
350 */
351
352static int watchdog_open(struct inode *inode, struct file *file)
353{
354 int err = -EBUSY;
Alan Cox45f5fed2012-05-10 21:48:59 +0200355 struct watchdog_device *wdd;
356
357 /* Get the corresponding watchdog device */
358 if (imajor(inode) == MISC_MAJOR)
359 wdd = old_wdd;
360 else
361 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000362
363 /* the watchdog is single open! */
364 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
365 return -EBUSY;
366
367 /*
368 * If the /dev/watchdog device is open, we don't want the module
369 * to be unloaded.
370 */
371 if (!try_module_get(wdd->ops->owner))
372 goto out;
373
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000374 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000375 if (err < 0)
376 goto out_mod;
377
Alan Cox45f5fed2012-05-10 21:48:59 +0200378 file->private_data = wdd;
379
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000380 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
381 return nonseekable_open(inode, file);
382
383out_mod:
384 module_put(wdd->ops->owner);
385out:
386 clear_bit(WDOG_DEV_OPEN, &wdd->status);
387 return err;
388}
389
390/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200391 * watchdog_release: release the watchdog device.
392 * @inode: inode of device
393 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000394 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000395 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000396 * stop the watchdog when we have received the magic char (and nowayout
397 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000398 */
399
400static int watchdog_release(struct inode *inode, struct file *file)
401{
Alan Cox45f5fed2012-05-10 21:48:59 +0200402 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000403 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000404
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000405 /*
406 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000407 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
408 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000409 */
410 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
411 !(wdd->info->options & WDIOF_MAGICCLOSE))
412 err = watchdog_stop(wdd);
413
414 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000415 if (err < 0) {
Alan Cox3dfd6212012-05-11 12:00:22 +0200416 dev_crit(wdd->dev, "watchdog did not stop!\n");
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000417 watchdog_ping(wdd);
418 }
419
420 /* Allow the owner module to be unloaded again */
421 module_put(wdd->ops->owner);
422
423 /* make sure that /dev/watchdog can be re-opened */
424 clear_bit(WDOG_DEV_OPEN, &wdd->status);
425
426 return 0;
427}
428
429static const struct file_operations watchdog_fops = {
430 .owner = THIS_MODULE,
431 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000432 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000433 .open = watchdog_open,
434 .release = watchdog_release,
435};
436
437static struct miscdevice watchdog_miscdev = {
438 .minor = WATCHDOG_MINOR,
439 .name = "watchdog",
440 .fops = &watchdog_fops,
441};
442
443/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200444 * watchdog_dev_register: register a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000445 * @watchdog: watchdog device
446 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200447 * Register a watchdog device including handling the legacy
448 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
449 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000450 */
451
452int watchdog_dev_register(struct watchdog_device *watchdog)
453{
Alan Cox45f5fed2012-05-10 21:48:59 +0200454 int err, devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000455
Alan Cox45f5fed2012-05-10 21:48:59 +0200456 if (watchdog->id == 0) {
Alan Coxd6b469d2012-05-11 12:00:20 +0200457 watchdog_miscdev.parent = watchdog->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200458 err = misc_register(&watchdog_miscdev);
459 if (err != 0) {
460 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
461 watchdog->info->identity, WATCHDOG_MINOR, err);
462 if (err == -EBUSY)
463 pr_err("%s: a legacy watchdog module is probably present.\n",
464 watchdog->info->identity);
465 return err;
466 }
467 old_wdd = watchdog;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000468 }
469
Alan Cox45f5fed2012-05-10 21:48:59 +0200470 /* Fill in the data structures */
471 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
472 cdev_init(&watchdog->cdev, &watchdog_fops);
473 watchdog->cdev.owner = watchdog->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000474
Alan Cox45f5fed2012-05-10 21:48:59 +0200475 /* Add the device */
476 err = cdev_add(&watchdog->cdev, devno, 1);
477 if (err) {
478 pr_err("watchdog%d unable to add device %d:%d\n",
479 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
480 if (watchdog->id == 0) {
481 misc_deregister(&watchdog_miscdev);
482 old_wdd = NULL;
483 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000484 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000485 return err;
486}
487
488/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200489 * watchdog_dev_unregister: unregister a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000490 * @watchdog: watchdog device
491 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200492 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000493 */
494
495int watchdog_dev_unregister(struct watchdog_device *watchdog)
496{
Alan Cox45f5fed2012-05-10 21:48:59 +0200497 cdev_del(&watchdog->cdev);
498 if (watchdog->id == 0) {
499 misc_deregister(&watchdog_miscdev);
500 old_wdd = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000501 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000502 return 0;
503}
Alan Cox45f5fed2012-05-10 21:48:59 +0200504
505/*
506 * watchdog_dev_init: init dev part of watchdog core
507 *
508 * Allocate a range of chardev nodes to use for watchdog devices
509 */
510
511int __init watchdog_dev_init(void)
512{
513 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
514 if (err < 0)
515 pr_err("watchdog: unable to allocate char dev region\n");
516 return err;
517}
518
519/*
520 * watchdog_dev_exit: exit dev part of watchdog core
521 *
522 * Release the range of chardev nodes used for watchdog devices
523 */
524
525void __exit watchdog_dev_exit(void)
526{
527 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
528}