1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35#include <linux/module.h>
36#include <linux/types.h>
37#include <linux/errno.h>
38#include <linux/kernel.h>
39#include <linux/fs.h>
40#include <linux/watchdog.h>
41#include <linux/miscdevice.h>
42#include <linux/init.h>
43#include <linux/uaccess.h>
44
45#include "watchdog_core.h"
46
47
48static dev_t watchdog_devt;
49
50static struct watchdog_device *old_wdd;
51
52
53
54
55
56
57
58
59
60
61
62static int watchdog_ping(struct watchdog_device *wddev)
63{
64 int err = 0;
65
66 mutex_lock(&wddev->lock);
67
68 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
69 err = -ENODEV;
70 goto out_ping;
71 }
72
73 if (!watchdog_active(wddev))
74 goto out_ping;
75
76 if (wddev->ops->ping)
77 err = wddev->ops->ping(wddev);
78 else
79 err = wddev->ops->start(wddev);
80
81out_ping:
82 mutex_unlock(&wddev->lock);
83 return err;
84}
85
86
87
88
89
90
91
92
93
94
95static int watchdog_start(struct watchdog_device *wddev)
96{
97 int err = 0;
98
99 mutex_lock(&wddev->lock);
100
101 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
102 err = -ENODEV;
103 goto out_start;
104 }
105
106 if (watchdog_active(wddev))
107 goto out_start;
108
109 err = wddev->ops->start(wddev);
110 if (err == 0)
111 set_bit(WDOG_ACTIVE, &wddev->status);
112
113out_start:
114 mutex_unlock(&wddev->lock);
115 return err;
116}
117
118
119
120
121
122
123
124
125
126
127
128static int watchdog_stop(struct watchdog_device *wddev)
129{
130 int err = 0;
131
132 mutex_lock(&wddev->lock);
133
134 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
135 err = -ENODEV;
136 goto out_stop;
137 }
138
139 if (!watchdog_active(wddev))
140 goto out_stop;
141
142 if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) {
143 dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n");
144 err = -EBUSY;
145 goto out_stop;
146 }
147
148 err = wddev->ops->stop(wddev);
149 if (err == 0)
150 clear_bit(WDOG_ACTIVE, &wddev->status);
151
152out_stop:
153 mutex_unlock(&wddev->lock);
154 return err;
155}
156
157
158
159
160
161
162
163
164
165static int watchdog_get_status(struct watchdog_device *wddev,
166 unsigned int *status)
167{
168 int err = 0;
169
170 *status = 0;
171 if (!wddev->ops->status)
172 return -EOPNOTSUPP;
173
174 mutex_lock(&wddev->lock);
175
176 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
177 err = -ENODEV;
178 goto out_status;
179 }
180
181 *status = wddev->ops->status(wddev);
182
183out_status:
184 mutex_unlock(&wddev->lock);
185 return err;
186}
187
188
189
190
191
192
193
194static int watchdog_set_timeout(struct watchdog_device *wddev,
195 unsigned int timeout)
196{
197 int err;
198
199 if ((wddev->ops->set_timeout == NULL) ||
200 !(wddev->info->options & WDIOF_SETTIMEOUT))
201 return -EOPNOTSUPP;
202
203 if ((wddev->max_timeout != 0) &&
204 (timeout < wddev->min_timeout || timeout > wddev->max_timeout))
205 return -EINVAL;
206
207 mutex_lock(&wddev->lock);
208
209 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
210 err = -ENODEV;
211 goto out_timeout;
212 }
213
214 err = wddev->ops->set_timeout(wddev, timeout);
215
216out_timeout:
217 mutex_unlock(&wddev->lock);
218 return err;
219}
220
221
222
223
224
225
226
227
228
229static int watchdog_get_timeleft(struct watchdog_device *wddev,
230 unsigned int *timeleft)
231{
232 int err = 0;
233
234 *timeleft = 0;
235 if (!wddev->ops->get_timeleft)
236 return -EOPNOTSUPP;
237
238 mutex_lock(&wddev->lock);
239
240 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
241 err = -ENODEV;
242 goto out_timeleft;
243 }
244
245 *timeleft = wddev->ops->get_timeleft(wddev);
246
247out_timeleft:
248 mutex_unlock(&wddev->lock);
249 return err;
250}
251
252
253
254
255
256
257
258
259static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
260 unsigned long arg)
261{
262 int err;
263
264 if (!wddev->ops->ioctl)
265 return -ENOIOCTLCMD;
266
267 mutex_lock(&wddev->lock);
268
269 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
270 err = -ENODEV;
271 goto out_ioctl;
272 }
273
274 err = wddev->ops->ioctl(wddev, cmd, arg);
275
276out_ioctl:
277 mutex_unlock(&wddev->lock);
278 return err;
279}
280
281
282
283
284
285
286
287
288
289
290
291
292
293static ssize_t watchdog_write(struct file *file, const char __user *data,
294 size_t len, loff_t *ppos)
295{
296 struct watchdog_device *wdd = file->private_data;
297 size_t i;
298 char c;
299
300 if (len == 0)
301 return 0;
302
303
304
305
306
307 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
308
309
310 for (i = 0; i != len; i++) {
311 if (get_user(c, data + i))
312 return -EFAULT;
313 if (c == 'V')
314 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
315 }
316
317
318 watchdog_ping(wdd);
319
320 return len;
321}
322
323
324
325
326
327
328
329
330
331
332
333static long watchdog_ioctl(struct file *file, unsigned int cmd,
334 unsigned long arg)
335{
336 struct watchdog_device *wdd = file->private_data;
337 void __user *argp = (void __user *)arg;
338 int __user *p = argp;
339 unsigned int val;
340 int err;
341
342 err = watchdog_ioctl_op(wdd, cmd, arg);
343 if (err != -ENOIOCTLCMD)
344 return err;
345
346 switch (cmd) {
347 case WDIOC_GETSUPPORT:
348 return copy_to_user(argp, wdd->info,
349 sizeof(struct watchdog_info)) ? -EFAULT : 0;
350 case WDIOC_GETSTATUS:
351 err = watchdog_get_status(wdd, &val);
352 if (err == -ENODEV)
353 return err;
354 return put_user(val, p);
355 case WDIOC_GETBOOTSTATUS:
356 return put_user(wdd->bootstatus, p);
357 case WDIOC_SETOPTIONS:
358 if (get_user(val, p))
359 return -EFAULT;
360 if (val & WDIOS_DISABLECARD) {
361 err = watchdog_stop(wdd);
362 if (err < 0)
363 return err;
364 }
365 if (val & WDIOS_ENABLECARD) {
366 err = watchdog_start(wdd);
367 if (err < 0)
368 return err;
369 }
370 return 0;
371 case WDIOC_KEEPALIVE:
372 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
373 return -EOPNOTSUPP;
374 watchdog_ping(wdd);
375 return 0;
376 case WDIOC_SETTIMEOUT:
377 if (get_user(val, p))
378 return -EFAULT;
379 err = watchdog_set_timeout(wdd, val);
380 if (err < 0)
381 return err;
382
383
384
385 watchdog_ping(wdd);
386
387 case WDIOC_GETTIMEOUT:
388
389 if (wdd->timeout == 0)
390 return -EOPNOTSUPP;
391 return put_user(wdd->timeout, p);
392 case WDIOC_GETTIMELEFT:
393 err = watchdog_get_timeleft(wdd, &val);
394 if (err)
395 return err;
396 return put_user(val, p);
397 default:
398 return -ENOTTY;
399 }
400}
401
402
403
404
405
406
407
408
409
410
411
412static int watchdog_open(struct inode *inode, struct file *file)
413{
414 int err = -EBUSY;
415 struct watchdog_device *wdd;
416
417
418 if (imajor(inode) == MISC_MAJOR)
419 wdd = old_wdd;
420 else
421 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
422
423
424 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
425 return -EBUSY;
426
427
428
429
430
431 if (!try_module_get(wdd->ops->owner))
432 goto out;
433
434 err = watchdog_start(wdd);
435 if (err < 0)
436 goto out_mod;
437
438 file->private_data = wdd;
439
440 if (wdd->ops->ref)
441 wdd->ops->ref(wdd);
442
443
444 return nonseekable_open(inode, file);
445
446out_mod:
447 module_put(wdd->ops->owner);
448out:
449 clear_bit(WDOG_DEV_OPEN, &wdd->status);
450 return err;
451}
452
453
454
455
456
457
458
459
460
461
462
463static int watchdog_release(struct inode *inode, struct file *file)
464{
465 struct watchdog_device *wdd = file->private_data;
466 int err = -EBUSY;
467
468
469
470
471
472
473 if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
474 !(wdd->info->options & WDIOF_MAGICCLOSE))
475 err = watchdog_stop(wdd);
476
477
478 if (err < 0) {
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);
483 watchdog_ping(wdd);
484 }
485
486
487 module_put(wdd->ops->owner);
488
489
490 clear_bit(WDOG_DEV_OPEN, &wdd->status);
491
492
493 if (wdd->ops->unref)
494 wdd->ops->unref(wdd);
495
496 return 0;
497}
498
499static const struct file_operations watchdog_fops = {
500 .owner = THIS_MODULE,
501 .write = watchdog_write,
502 .unlocked_ioctl = watchdog_ioctl,
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
514
515
516
517
518
519
520
521
522int watchdog_dev_register(struct watchdog_device *watchdog)
523{
524 int err, devno;
525
526 if (watchdog->id == 0) {
527 watchdog_miscdev.parent = watchdog->parent;
528 err = misc_register(&watchdog_miscdev);
529 if (err != 0) {
530 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
531 watchdog->info->identity, WATCHDOG_MINOR, err);
532 if (err == -EBUSY)
533 pr_err("%s: a legacy watchdog module is probably present.\n",
534 watchdog->info->identity);
535 return err;
536 }
537 old_wdd = watchdog;
538 }
539
540
541 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
542 cdev_init(&watchdog->cdev, &watchdog_fops);
543 watchdog->cdev.owner = watchdog->ops->owner;
544
545
546 err = cdev_add(&watchdog->cdev, devno, 1);
547 if (err) {
548 pr_err("watchdog%d unable to add device %d:%d\n",
549 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
550 if (watchdog->id == 0) {
551 misc_deregister(&watchdog_miscdev);
552 old_wdd = NULL;
553 }
554 }
555 return err;
556}
557
558
559
560
561
562
563
564
565int watchdog_dev_unregister(struct watchdog_device *watchdog)
566{
567 mutex_lock(&watchdog->lock);
568 set_bit(WDOG_UNREGISTERED, &watchdog->status);
569 mutex_unlock(&watchdog->lock);
570
571 cdev_del(&watchdog->cdev);
572 if (watchdog->id == 0) {
573 misc_deregister(&watchdog_miscdev);
574 old_wdd = NULL;
575 }
576 return 0;
577}
578
579
580
581
582
583
584
585int __init watchdog_dev_init(void)
586{
587 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
588 if (err < 0)
589 pr_err("watchdog: unable to allocate char dev region\n");
590 return err;
591}
592
593
594
595
596
597
598
599void __exit watchdog_dev_exit(void)
600{
601 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
602}
603