blob: 1f011f2d6e48f86796be505bf9e4ab3d2756c050 [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)) {
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000111 pr_info("%s: nowayout prevents watchdog to be stopped!\n",
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700112 wddev->info->identity);
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000113 return err;
114 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000115
Viresh Kumar257f8c42012-03-12 09:51:56 +0530116 if (watchdog_active(wddev)) {
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000117 err = wddev->ops->stop(wddev);
118 if (err < 0)
119 return err;
120
H Hartley Sweetencb7efc02011-08-03 15:38:20 -0700121 clear_bit(WDOG_ACTIVE, &wddev->status);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000122 }
123 return 0;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000124}
125
126/*
127 * watchdog_write: writes to the watchdog.
128 * @file: file from VFS
129 * @data: user address of data
130 * @len: length of data
131 * @ppos: pointer to the file offset
132 *
133 * A write to a watchdog device is defined as a keepalive ping.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000134 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000135 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000136 */
137
138static ssize_t watchdog_write(struct file *file, const char __user *data,
139 size_t len, loff_t *ppos)
140{
Alan Cox45f5fed2012-05-10 21:48:59 +0200141 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000142 size_t i;
143 char c;
144
145 if (len == 0)
146 return 0;
147
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000148 /*
149 * Note: just in case someone wrote the magic character
150 * five months ago...
151 */
152 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
153
154 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000155 for (i = 0; i != len; i++) {
156 if (get_user(c, data + i))
157 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000158 if (c == 'V')
159 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000160 }
161
162 /* someone wrote to us, so we send the watchdog a keepalive ping */
163 watchdog_ping(wdd);
164
165 return len;
166}
167
168/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000169 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
170 * @file: file handle to the device
171 * @cmd: watchdog command
172 * @arg: argument pointer
173 *
174 * The watchdog API defines a common set of functions for all watchdogs
175 * according to their available features.
176 */
177
178static long watchdog_ioctl(struct file *file, unsigned int cmd,
179 unsigned long arg)
180{
Alan Cox45f5fed2012-05-10 21:48:59 +0200181 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000182 void __user *argp = (void __user *)arg;
183 int __user *p = argp;
184 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000185 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000186
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000187 if (wdd->ops->ioctl) {
188 err = wdd->ops->ioctl(wdd, cmd, arg);
189 if (err != -ENOIOCTLCMD)
190 return err;
191 }
192
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000193 switch (cmd) {
194 case WDIOC_GETSUPPORT:
195 return copy_to_user(argp, wdd->info,
196 sizeof(struct watchdog_info)) ? -EFAULT : 0;
197 case WDIOC_GETSTATUS:
198 val = wdd->ops->status ? wdd->ops->status(wdd) : 0;
199 return put_user(val, p);
200 case WDIOC_GETBOOTSTATUS:
201 return put_user(wdd->bootstatus, p);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000202 case WDIOC_SETOPTIONS:
203 if (get_user(val, p))
204 return -EFAULT;
205 if (val & WDIOS_DISABLECARD) {
206 err = watchdog_stop(wdd);
207 if (err < 0)
208 return err;
209 }
210 if (val & WDIOS_ENABLECARD) {
211 err = watchdog_start(wdd);
212 if (err < 0)
213 return err;
214 }
215 return 0;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000216 case WDIOC_KEEPALIVE:
217 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
218 return -EOPNOTSUPP;
219 watchdog_ping(wdd);
220 return 0;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000221 case WDIOC_SETTIMEOUT:
222 if ((wdd->ops->set_timeout == NULL) ||
223 !(wdd->info->options & WDIOF_SETTIMEOUT))
224 return -EOPNOTSUPP;
225 if (get_user(val, p))
226 return -EFAULT;
Wim Van Sebroeck3f43f682011-07-22 19:00:16 +0000227 if ((wdd->max_timeout != 0) &&
228 (val < wdd->min_timeout || val > wdd->max_timeout))
229 return -EINVAL;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000230 err = wdd->ops->set_timeout(wdd, val);
231 if (err < 0)
232 return err;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000233 /* If the watchdog is active then we send a keepalive ping
234 * to make sure that the watchdog keep's running (and if
235 * possible that it takes the new timeout) */
236 watchdog_ping(wdd);
237 /* Fall */
238 case WDIOC_GETTIMEOUT:
239 /* timeout == 0 means that we don't know the timeout */
240 if (wdd->timeout == 0)
241 return -EOPNOTSUPP;
242 return put_user(wdd->timeout, p);
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100243 case WDIOC_GETTIMELEFT:
244 if (!wdd->ops->get_timeleft)
245 return -EOPNOTSUPP;
246
247 return put_user(wdd->ops->get_timeleft(wdd), p);
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000248 default:
249 return -ENOTTY;
250 }
251}
252
253/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200254 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000255 * @inode: inode of device
256 * @file: file handle to device
257 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200258 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000259 * Watch out: the /dev/watchdog device is single open, so we make sure
260 * it can only be opened once.
261 */
262
263static int watchdog_open(struct inode *inode, struct file *file)
264{
265 int err = -EBUSY;
Alan Cox45f5fed2012-05-10 21:48:59 +0200266 struct watchdog_device *wdd;
267
268 /* Get the corresponding watchdog device */
269 if (imajor(inode) == MISC_MAJOR)
270 wdd = old_wdd;
271 else
272 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000273
274 /* the watchdog is single open! */
275 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
276 return -EBUSY;
277
278 /*
279 * If the /dev/watchdog device is open, we don't want the module
280 * to be unloaded.
281 */
282 if (!try_module_get(wdd->ops->owner))
283 goto out;
284
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000285 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000286 if (err < 0)
287 goto out_mod;
288
Alan Cox45f5fed2012-05-10 21:48:59 +0200289 file->private_data = wdd;
290
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000291 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
292 return nonseekable_open(inode, file);
293
294out_mod:
295 module_put(wdd->ops->owner);
296out:
297 clear_bit(WDOG_DEV_OPEN, &wdd->status);
298 return err;
299}
300
301/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200302 * watchdog_release: release the watchdog device.
303 * @inode: inode of device
304 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000305 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000306 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000307 * stop the watchdog when we have received the magic char (and nowayout
308 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000309 */
310
311static int watchdog_release(struct inode *inode, struct file *file)
312{
Alan Cox45f5fed2012-05-10 21:48:59 +0200313 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000314 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000315
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000316 /*
317 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000318 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
319 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000320 */
321 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
322 !(wdd->info->options & WDIOF_MAGICCLOSE))
323 err = watchdog_stop(wdd);
324
325 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000326 if (err < 0) {
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000327 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
328 watchdog_ping(wdd);
329 }
330
331 /* Allow the owner module to be unloaded again */
332 module_put(wdd->ops->owner);
333
334 /* make sure that /dev/watchdog can be re-opened */
335 clear_bit(WDOG_DEV_OPEN, &wdd->status);
336
337 return 0;
338}
339
340static const struct file_operations watchdog_fops = {
341 .owner = THIS_MODULE,
342 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000343 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000344 .open = watchdog_open,
345 .release = watchdog_release,
346};
347
348static struct miscdevice watchdog_miscdev = {
349 .minor = WATCHDOG_MINOR,
350 .name = "watchdog",
351 .fops = &watchdog_fops,
352};
353
354/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200355 * watchdog_dev_register: register a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000356 * @watchdog: watchdog device
357 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200358 * Register a watchdog device including handling the legacy
359 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
360 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000361 */
362
363int watchdog_dev_register(struct watchdog_device *watchdog)
364{
Alan Cox45f5fed2012-05-10 21:48:59 +0200365 int err, devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000366
Alan Cox45f5fed2012-05-10 21:48:59 +0200367 if (watchdog->id == 0) {
Alan Coxd6b469d2012-05-11 12:00:20 +0200368 watchdog_miscdev.parent = watchdog->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200369 err = misc_register(&watchdog_miscdev);
370 if (err != 0) {
371 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
372 watchdog->info->identity, WATCHDOG_MINOR, err);
373 if (err == -EBUSY)
374 pr_err("%s: a legacy watchdog module is probably present.\n",
375 watchdog->info->identity);
376 return err;
377 }
378 old_wdd = watchdog;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000379 }
380
Alan Cox45f5fed2012-05-10 21:48:59 +0200381 /* Fill in the data structures */
382 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
383 cdev_init(&watchdog->cdev, &watchdog_fops);
384 watchdog->cdev.owner = watchdog->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000385
Alan Cox45f5fed2012-05-10 21:48:59 +0200386 /* Add the device */
387 err = cdev_add(&watchdog->cdev, devno, 1);
388 if (err) {
389 pr_err("watchdog%d unable to add device %d:%d\n",
390 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
391 if (watchdog->id == 0) {
392 misc_deregister(&watchdog_miscdev);
393 old_wdd = NULL;
394 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000395 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000396 return err;
397}
398
399/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200400 * watchdog_dev_unregister: unregister a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000401 * @watchdog: watchdog device
402 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200403 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000404 */
405
406int watchdog_dev_unregister(struct watchdog_device *watchdog)
407{
Alan Cox45f5fed2012-05-10 21:48:59 +0200408 cdev_del(&watchdog->cdev);
409 if (watchdog->id == 0) {
410 misc_deregister(&watchdog_miscdev);
411 old_wdd = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000412 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000413 return 0;
414}
Alan Cox45f5fed2012-05-10 21:48:59 +0200415
416/*
417 * watchdog_dev_init: init dev part of watchdog core
418 *
419 * Allocate a range of chardev nodes to use for watchdog devices
420 */
421
422int __init watchdog_dev_init(void)
423{
424 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
425 if (err < 0)
426 pr_err("watchdog: unable to allocate char dev region\n");
427 return err;
428}
429
430/*
431 * watchdog_dev_exit: exit dev part of watchdog core
432 *
433 * Release the range of chardev nodes used for watchdog devices
434 */
435
436void __exit watchdog_dev_exit(void)
437{
438 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
439}