blob: 55191cccf026b96009ca536938ce930571dd4485 [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{
Viresh Kumar257f8c42012-03-12 09:51:56 +053064 if (watchdog_active(wddev)) {
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000065 if (wddev->ops->ping)
66 return wddev->ops->ping(wddev); /* ping the watchdog */
67 else
68 return wddev->ops->start(wddev); /* restart watchdog */
69 }
70 return 0;
71}
72
73/*
74 * watchdog_start: wrapper to start the watchdog.
75 * @wddev: the watchdog device to start
76 *
77 * Start the watchdog if it is not active and mark it active.
78 * This function returns zero on success or a negative errno code for
79 * failure.
80 */
81
82static int watchdog_start(struct watchdog_device *wddev)
83{
84 int err;
85
Viresh Kumar257f8c42012-03-12 09:51:56 +053086 if (!watchdog_active(wddev)) {
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000087 err = wddev->ops->start(wddev);
88 if (err < 0)
89 return err;
90
H Hartley Sweetencb7efc02011-08-03 15:38:20 -070091 set_bit(WDOG_ACTIVE, &wddev->status);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000092 }
93 return 0;
94}
95
96/*
97 * watchdog_stop: wrapper to stop the watchdog.
98 * @wddev: the watchdog device to stop
99 *
100 * Stop the watchdog if it is still active and unmark it active.
101 * This function returns zero on success or a negative errno code for
102 * failure.
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000103 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000104 */
105
106static int watchdog_stop(struct watchdog_device *wddev)
107{
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000108 int err = -EBUSY;
109
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700110 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
Alan Cox3dfd6212012-05-11 12:00:22 +0200111 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000112 return err;
113 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000114
Viresh Kumar257f8c42012-03-12 09:51:56 +0530115 if (watchdog_active(wddev)) {
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000116 err = wddev->ops->stop(wddev);
117 if (err < 0)
118 return err;
119
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700120 clear_bit(WDOG_ACTIVE, &wddev->status);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000121 }
122 return 0;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000123}
124
125/*
126 * watchdog_write: writes to the watchdog.
127 * @file: file from VFS
128 * @data: user address of data
129 * @len: length of data
130 * @ppos: pointer to the file offset
131 *
132 * A write to a watchdog device is defined as a keepalive ping.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000133 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000134 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000135 */
136
137static ssize_t watchdog_write(struct file *file, const char __user *data,
138 size_t len, loff_t *ppos)
139{
Alan Cox45f5fed2012-05-10 21:48:59 +0200140 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000141 size_t i;
142 char c;
143
144 if (len == 0)
145 return 0;
146
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000147 /*
148 * Note: just in case someone wrote the magic character
149 * five months ago...
150 */
151 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
152
153 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000154 for (i = 0; i != len; i++) {
155 if (get_user(c, data + i))
156 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000157 if (c == 'V')
158 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000159 }
160
161 /* someone wrote to us, so we send the watchdog a keepalive ping */
162 watchdog_ping(wdd);
163
164 return len;
165}
166
167/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000168 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
169 * @file: file handle to the device
170 * @cmd: watchdog command
171 * @arg: argument pointer
172 *
173 * The watchdog API defines a common set of functions for all watchdogs
174 * according to their available features.
175 */
176
177static long watchdog_ioctl(struct file *file, unsigned int cmd,
178 unsigned long arg)
179{
Alan Cox45f5fed2012-05-10 21:48:59 +0200180 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000181 void __user *argp = (void __user *)arg;
182 int __user *p = argp;
183 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000184 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000185
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000186 if (wdd->ops->ioctl) {
187 err = wdd->ops->ioctl(wdd, cmd, arg);
188 if (err != -ENOIOCTLCMD)
189 return err;
190 }
191
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000192 switch (cmd) {
193 case WDIOC_GETSUPPORT:
194 return copy_to_user(argp, wdd->info,
195 sizeof(struct watchdog_info)) ? -EFAULT : 0;
196 case WDIOC_GETSTATUS:
197 val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
198 return put_user(val, p);
199 case WDIOC_GETBOOTSTATUS:
200 return put_user(wdd->bootstatus, p);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000201 case WDIOC_SETOPTIONS:
202 if (get_user(val, p))
203 return -EFAULT;
204 if (val & WDIOS_DISABLECARD) {
205 err = watchdog_stop(wdd);
206 if (err < 0)
207 return err;
208 }
209 if (val & WDIOS_ENABLECARD) {
210 err = watchdog_start(wdd);
211 if (err < 0)
212 return err;
213 }
214 return 0;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000215 case WDIOC_KEEPALIVE:
216 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
217 return -EOPNOTSUPP;
218 watchdog_ping(wdd);
219 return 0;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000220 case WDIOC_SETTIMEOUT:
221 if ((wdd->ops->set_timeout == NULL) ||
222 !(wdd->info->options & WDIOF_SETTIMEOUT))
223 return -EOPNOTSUPP;
224 if (get_user(val, p))
225 return -EFAULT;
Wim Van Sebroeck3f43f682011-07-22 19:00:16 +0000226 if ((wdd->max_timeout != 0) &&
227 (val < wdd->min_timeout || val > wdd->max_timeout))
228 return -EINVAL;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000229 err = wdd->ops->set_timeout(wdd, val);
230 if (err < 0)
231 return err;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000232 /* If the watchdog is active then we send a keepalive ping
233 * to make sure that the watchdog keep's running (and if
234 * possible that it takes the new timeout) */
235 watchdog_ping(wdd);
236 /* Fall */
237 case WDIOC_GETTIMEOUT:
238 /* timeout == 0 means that we don't know the timeout */
239 if (wdd->timeout == 0)
240 return -EOPNOTSUPP;
241 return put_user(wdd->timeout, p);
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100242 case WDIOC_GETTIMELEFT:
243 if (!wdd->ops->get_timeleft)
244 return -EOPNOTSUPP;
245
246 return put_user(wdd->ops->get_timeleft(wdd), p);
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000247 default:
248 return -ENOTTY;
249 }
250}
251
252/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200253 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000254 * @inode: inode of device
255 * @file: file handle to device
256 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200257 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000258 * Watch out: the /dev/watchdog device is single open, so we make sure
259 * it can only be opened once.
260 */
261
262static int watchdog_open(struct inode *inode, struct file *file)
263{
264 int err = -EBUSY;
Alan Cox45f5fed2012-05-10 21:48:59 +0200265 struct watchdog_device *wdd;
266
267 /* Get the corresponding watchdog device */
268 if (imajor(inode) == MISC_MAJOR)
269 wdd = old_wdd;
270 else
271 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000272
273 /* the watchdog is single open! */
274 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
275 return -EBUSY;
276
277 /*
278 * If the /dev/watchdog device is open, we don't want the module
279 * to be unloaded.
280 */
281 if (!try_module_get(wdd->ops->owner))
282 goto out;
283
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000284 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000285 if (err < 0)
286 goto out_mod;
287
Alan Cox45f5fed2012-05-10 21:48:59 +0200288 file->private_data = wdd;
289
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000290 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
291 return nonseekable_open(inode, file);
292
293out_mod:
294 module_put(wdd->ops->owner);
295out:
296 clear_bit(WDOG_DEV_OPEN, &wdd->status);
297 return err;
298}
299
300/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200301 * watchdog_release: release the watchdog device.
302 * @inode: inode of device
303 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000304 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000305 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000306 * stop the watchdog when we have received the magic char (and nowayout
307 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000308 */
309
310static int watchdog_release(struct inode *inode, struct file *file)
311{
Alan Cox45f5fed2012-05-10 21:48:59 +0200312 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000313 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000314
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000315 /*
316 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000317 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
318 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000319 */
320 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
321 !(wdd->info->options & WDIOF_MAGICCLOSE))
322 err = watchdog_stop(wdd);
323
324 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000325 if (err < 0) {
Alan Cox3dfd6212012-05-11 12:00:22 +0200326 dev_crit(wdd->dev, "watchdog did not stop!\n");
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000327 watchdog_ping(wdd);
328 }
329
330 /* Allow the owner module to be unloaded again */
331 module_put(wdd->ops->owner);
332
333 /* make sure that /dev/watchdog can be re-opened */
334 clear_bit(WDOG_DEV_OPEN, &wdd->status);
335
336 return 0;
337}
338
339static const struct file_operations watchdog_fops = {
340 .owner = THIS_MODULE,
341 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000342 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000343 .open = watchdog_open,
344 .release = watchdog_release,
345};
346
347static struct miscdevice watchdog_miscdev = {
348 .minor = WATCHDOG_MINOR,
349 .name = "watchdog",
350 .fops = &watchdog_fops,
351};
352
353/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200354 * watchdog_dev_register: register a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000355 * @watchdog: watchdog device
356 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200357 * Register a watchdog device including handling the legacy
358 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
359 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000360 */
361
362int watchdog_dev_register(struct watchdog_device *watchdog)
363{
Alan Cox45f5fed2012-05-10 21:48:59 +0200364 int err, devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000365
Alan Cox45f5fed2012-05-10 21:48:59 +0200366 if (watchdog->id == 0) {
Alan Coxd6b469d2012-05-11 12:00:20 +0200367 watchdog_miscdev.parent = watchdog->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200368 err = misc_register(&watchdog_miscdev);
369 if (err != 0) {
370 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
371 watchdog->info->identity, WATCHDOG_MINOR, err);
372 if (err == -EBUSY)
373 pr_err("%s: a legacy watchdog module is probably present.\n",
374 watchdog->info->identity);
375 return err;
376 }
377 old_wdd = watchdog;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000378 }
379
Alan Cox45f5fed2012-05-10 21:48:59 +0200380 /* Fill in the data structures */
381 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
382 cdev_init(&watchdog->cdev, &watchdog_fops);
383 watchdog->cdev.owner = watchdog->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000384
Alan Cox45f5fed2012-05-10 21:48:59 +0200385 /* Add the device */
386 err = cdev_add(&watchdog->cdev, devno, 1);
387 if (err) {
388 pr_err("watchdog%d unable to add device %d:%d\n",
389 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
390 if (watchdog->id == 0) {
391 misc_deregister(&watchdog_miscdev);
392 old_wdd = NULL;
393 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000394 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000395 return err;
396}
397
398/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200399 * watchdog_dev_unregister: unregister a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000400 * @watchdog: watchdog device
401 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200402 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000403 */
404
405int watchdog_dev_unregister(struct watchdog_device *watchdog)
406{
Alan Cox45f5fed2012-05-10 21:48:59 +0200407 cdev_del(&watchdog->cdev);
408 if (watchdog->id == 0) {
409 misc_deregister(&watchdog_miscdev);
410 old_wdd = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000411 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000412 return 0;
413}
Alan Cox45f5fed2012-05-10 21:48:59 +0200414
415/*
416 * watchdog_dev_init: init dev part of watchdog core
417 *
418 * Allocate a range of chardev nodes to use for watchdog devices
419 */
420
421int __init watchdog_dev_init(void)
422{
423 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
424 if (err < 0)
425 pr_err("watchdog: unable to allocate char dev region\n");
426 return err;
427}
428
429/*
430 * watchdog_dev_exit: exit dev part of watchdog core
431 *
432 * Release the range of chardev nodes used for watchdog devices
433 */
434
435void __exit watchdog_dev_exit(void)
436{
437 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
438}