Commit | Line | Data |
---|---|---|
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 */ |
48 | static dev_t watchdog_devt; | |
43316044 | 49 | /* the watchdog device behind /dev/watchdog */ |
45f5fed3 | 50 | static 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 | ||
62 | static int watchdog_ping(struct watchdog_device *wddev) | |
63 | { | |
7a879824 HG |
64 | 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 | ||
74 | out_ping: | |
75 | return err; | |
234445b4 WVS |
76 | } |
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 | ||
87 | static int watchdog_start(struct watchdog_device *wddev) | |
88 | { | |
7a879824 | 89 | int err = 0; |
234445b4 | 90 | |
7a879824 HG |
91 | if (watchdog_active(wddev)) |
92 | goto out_start; | |
234445b4 | 93 | |
7a879824 HG |
94 | err = wddev->ops->start(wddev); |
95 | if (err == 0) | |
cb7efc02 | 96 | set_bit(WDOG_ACTIVE, &wddev->status); |
7a879824 HG |
97 | |
98 | out_start: | |
99 | return err; | |
234445b4 WVS |
100 | } |
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. | |
7e192b9c | 109 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
234445b4 WVS |
110 | */ |
111 | ||
112 | static int watchdog_stop(struct watchdog_device *wddev) | |
113 | { | |
7a879824 HG |
114 | int err = 0; |
115 | ||
116 | if (!watchdog_active(wddev)) | |
117 | goto out_stop; | |
7e192b9c | 118 | |
cb7efc02 | 119 | if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { |
3dfd6218 | 120 | dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n"); |
7a879824 HG |
121 | err = -EBUSY; |
122 | goto out_stop; | |
7e192b9c | 123 | } |
234445b4 | 124 | |
7a879824 HG |
125 | err = wddev->ops->stop(wddev); |
126 | if (err == 0) | |
cb7efc02 | 127 | clear_bit(WDOG_ACTIVE, &wddev->status); |
7a879824 HG |
128 | |
129 | out_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 | ||
141 | static 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 | ||
161 | static 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 | ||
187 | static 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 | ||
208 | static 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; | |
43316044 WVS |
219 | } |
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. | |
017cf080 | 229 | * Writing the magic 'V' sequence allows the next close to turn |
7e192b9c | 230 | * off the watchdog (if 'nowayout' is not set). |
43316044 WVS |
231 | */ |
232 | ||
233 | static ssize_t watchdog_write(struct file *file, const char __user *data, | |
234 | size_t len, loff_t *ppos) | |
235 | { | |
45f5fed3 | 236 | struct watchdog_device *wdd = file->private_data; |
43316044 WVS |
237 | size_t i; |
238 | char c; | |
239 | ||
240 | if (len == 0) | |
241 | return 0; | |
242 | ||
017cf080 WVS |
243 | /* |
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 */ | |
43316044 WVS |
250 | for (i = 0; i != len; i++) { |
251 | if (get_user(c, data + i)) | |
252 | return -EFAULT; | |
017cf080 WVS |
253 | if (c == 'V') |
254 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); | |
43316044 WVS |
255 | } |
256 | ||
257 | /* someone wrote to us, so we send the watchdog a keepalive ping */ | |
258 | watchdog_ping(wdd); | |
259 | ||
260 | return len; | |
261 | } | |
262 | ||
2fa03560 WVS |
263 | /* |
264 | * 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 | ||
273 | static long watchdog_ioctl(struct file *file, unsigned int cmd, | |
274 | unsigned long arg) | |
275 | { | |
45f5fed3 | 276 | struct watchdog_device *wdd = file->private_data; |
2fa03560 WVS |
277 | void __user *argp = (void __user *)arg; |
278 | int __user *p = argp; | |
279 | unsigned int val; | |
234445b4 | 280 | int err; |
2fa03560 | 281 | |
7a879824 HG |
282 | err = watchdog_ioctl_op(wdd, cmd, arg); |
283 | if (err != -ENOIOCTLCMD) | |
284 | return err; | |
78d88fc0 | 285 | |
2fa03560 WVS |
286 | 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: | |
7a879824 HG |
291 | err = watchdog_get_status(wdd, &val); |
292 | if (err) | |
293 | return err; | |
2fa03560 WVS |
294 | return put_user(val, p); |
295 | case WDIOC_GETBOOTSTATUS: | |
296 | return put_user(wdd->bootstatus, p); | |
234445b4 WVS |
297 | 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; | |
c2dc00e4 WVS |
311 | case WDIOC_KEEPALIVE: |
312 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) | |
313 | return -EOPNOTSUPP; | |
314 | watchdog_ping(wdd); | |
315 | return 0; | |
014d694e | 316 | case WDIOC_SETTIMEOUT: |
014d694e WVS |
317 | if (get_user(val, p)) |
318 | return -EFAULT; | |
7a879824 | 319 | err = watchdog_set_timeout(wdd, val); |
014d694e WVS |
320 | if (err < 0) |
321 | return err; | |
014d694e WVS |
322 | /* 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); | |
fd7b673c | 332 | case WDIOC_GETTIMELEFT: |
7a879824 HG |
333 | err = watchdog_get_timeleft(wdd, &val); |
334 | if (err) | |
335 | return err; | |
336 | return put_user(val, p); | |
2fa03560 WVS |
337 | default: |
338 | return -ENOTTY; | |
339 | } | |
340 | } | |
341 | ||
43316044 | 342 | /* |
45f5fed3 | 343 | * watchdog_open: open the /dev/watchdog* devices. |
43316044 WVS |
344 | * @inode: inode of device |
345 | * @file: file handle to device | |
346 | * | |
45f5fed3 | 347 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
43316044 WVS |
348 | * Watch out: the /dev/watchdog device is single open, so we make sure |
349 | * it can only be opened once. | |
350 | */ | |
351 | ||
352 | static int watchdog_open(struct inode *inode, struct file *file) | |
353 | { | |
354 | int err = -EBUSY; | |
45f5fed3 AC |
355 | 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); | |
43316044 WVS |
362 | |
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 | ||
234445b4 | 374 | err = watchdog_start(wdd); |
43316044 WVS |
375 | if (err < 0) |
376 | goto out_mod; | |
377 | ||
45f5fed3 AC |
378 | file->private_data = wdd; |
379 | ||
43316044 WVS |
380 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
381 | return nonseekable_open(inode, file); | |
382 | ||
383 | out_mod: | |
384 | module_put(wdd->ops->owner); | |
385 | out: | |
386 | clear_bit(WDOG_DEV_OPEN, &wdd->status); | |
387 | return err; | |
388 | } | |
389 | ||
390 | /* | |
45f5fed3 AC |
391 | * watchdog_release: release the watchdog device. |
392 | * @inode: inode of device | |
393 | * @file: file handle to device | |
43316044 | 394 | * |
017cf080 | 395 | * This is the code for when /dev/watchdog gets closed. We will only |
7e192b9c WVS |
396 | * stop the watchdog when we have received the magic char (and nowayout |
397 | * was not set), else the watchdog will keep running. | |
43316044 WVS |
398 | */ |
399 | ||
400 | static int watchdog_release(struct inode *inode, struct file *file) | |
401 | { | |
45f5fed3 | 402 | struct watchdog_device *wdd = file->private_data; |
017cf080 WVS |
403 | int err = -EBUSY; |
404 | ||
405 | /* | |
406 | * We only stop the watchdog if we received the magic character | |
7e192b9c WVS |
407 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
408 | * watchdog_stop will fail. | |
017cf080 WVS |
409 | */ |
410 | if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || | |
411 | !(wdd->info->options & WDIOF_MAGICCLOSE)) | |
412 | err = watchdog_stop(wdd); | |
43316044 | 413 | |
017cf080 | 414 | /* If the watchdog was not stopped, send a keepalive ping */ |
234445b4 | 415 | if (err < 0) { |
3dfd6218 | 416 | dev_crit(wdd->dev, "watchdog did not stop!\n"); |
43316044 WVS |
417 | 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 | ||
429 | static const struct file_operations watchdog_fops = { | |
430 | .owner = THIS_MODULE, | |
431 | .write = watchdog_write, | |
2fa03560 | 432 | .unlocked_ioctl = watchdog_ioctl, |
43316044 WVS |
433 | .open = watchdog_open, |
434 | .release = watchdog_release, | |
435 | }; | |
436 | ||
437 | static struct miscdevice watchdog_miscdev = { | |
438 | .minor = WATCHDOG_MINOR, | |
439 | .name = "watchdog", | |
440 | .fops = &watchdog_fops, | |
441 | }; | |
442 | ||
443 | /* | |
45f5fed3 | 444 | * watchdog_dev_register: register a watchdog device |
43316044 WVS |
445 | * @watchdog: watchdog device |
446 | * | |
45f5fed3 AC |
447 | * 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. | |
43316044 WVS |
450 | */ |
451 | ||
452 | int watchdog_dev_register(struct watchdog_device *watchdog) | |
453 | { | |
45f5fed3 AC |
454 | int err, devno; |
455 | ||
456 | if (watchdog->id == 0) { | |
d6b469d9 | 457 | watchdog_miscdev.parent = watchdog->parent; |
45f5fed3 AC |
458 | 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; | |
43316044 WVS |
468 | } |
469 | ||
45f5fed3 AC |
470 | /* 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; | |
474 | ||
475 | /* 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 | } | |
43316044 | 484 | } |
43316044 WVS |
485 | return err; |
486 | } | |
487 | ||
488 | /* | |
45f5fed3 | 489 | * watchdog_dev_unregister: unregister a watchdog device |
43316044 WVS |
490 | * @watchdog: watchdog device |
491 | * | |
45f5fed3 | 492 | * Unregister the watchdog and if needed the legacy /dev/watchdog device. |
43316044 WVS |
493 | */ |
494 | ||
495 | int watchdog_dev_unregister(struct watchdog_device *watchdog) | |
496 | { | |
45f5fed3 AC |
497 | cdev_del(&watchdog->cdev); |
498 | if (watchdog->id == 0) { | |
499 | misc_deregister(&watchdog_miscdev); | |
500 | old_wdd = NULL; | |
43316044 | 501 | } |
43316044 WVS |
502 | return 0; |
503 | } | |
45f5fed3 AC |
504 | |
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 | ||
511 | int __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 | ||
525 | void __exit watchdog_dev_exit(void) | |
526 | { | |
527 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); | |
528 | } |