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 (watchdog_timeout_invalid(wddev, timeout))
204 return -EINVAL;
205
206 mutex_lock(&wddev->lock);
207
208 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
209 err = -ENODEV;
210 goto out_timeout;
211 }
212
213 err = wddev->ops->set_timeout(wddev, timeout);
214
215out_timeout:
216 mutex_unlock(&wddev->lock);
217 return err;
218}
219
220
221
222
223
224
225
226
227
228static int watchdog_get_timeleft(struct watchdog_device *wddev,
229 unsigned int *timeleft)
230{
231 int err = 0;
232
233 *timeleft = 0;
234 if (!wddev->ops->get_timeleft)
235 return -EOPNOTSUPP;
236
237 mutex_lock(&wddev->lock);
238
239 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
240 err = -ENODEV;
241 goto out_timeleft;
242 }
243
244 *timeleft = wddev->ops->get_timeleft(wddev);
245
246out_timeleft:
247 mutex_unlock(&wddev->lock);
248 return err;
249}
250
251
252
253
254
255
256
257
258static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd,
259 unsigned long arg)
260{
261 int err;
262
263 if (!wddev->ops->ioctl)
264 return -ENOIOCTLCMD;
265
266 mutex_lock(&wddev->lock);
267
268 if (test_bit(WDOG_UNREGISTERED, &wddev->status)) {
269 err = -ENODEV;
270 goto out_ioctl;
271 }
272
273 err = wddev->ops->ioctl(wddev, cmd, arg);
274
275out_ioctl:
276 mutex_unlock(&wddev->lock);
277 return err;
278}
279
280
281
282
283
284
285
286
287
288
289
290
291
292static ssize_t watchdog_write(struct file *file, const char __user *data,
293 size_t len, loff_t *ppos)
294{
295 struct watchdog_device *wdd = file->private_data;
296 size_t i;
297 char c;
298
299 if (len == 0)
300 return 0;
301
302
303
304
305
306 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
307
308
309 for (i = 0; i != len; i++) {
310 if (get_user(c, data + i))
311 return -EFAULT;
312 if (c == 'V')
313 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
314 }
315
316
317 watchdog_ping(wdd);
318
319 return len;
320}
321
322
323
324
325
326
327
328
329
330
331
332static long watchdog_ioctl(struct file *file, unsigned int cmd,
333 unsigned long arg)
334{
335 struct watchdog_device *wdd = file->private_data;
336 void __user *argp = (void __user *)arg;
337 int __user *p = argp;
338 unsigned int val;
339 int err;
340
341 err = watchdog_ioctl_op(wdd, cmd, arg);
342 if (err != -ENOIOCTLCMD)
343 return err;
344
345 switch (cmd) {
346 case WDIOC_GETSUPPORT:
347 return copy_to_user(argp, wdd->info,
348 sizeof(struct watchdog_info)) ? -EFAULT : 0;
349 case WDIOC_GETSTATUS:
350 err = watchdog_get_status(wdd, &val);
351 if (err == -ENODEV)
352 return err;
353 return put_user(val, p);
354 case WDIOC_GETBOOTSTATUS:
355 return put_user(wdd->bootstatus, p);
356 case WDIOC_SETOPTIONS:
357 if (get_user(val, p))
358 return -EFAULT;
359 if (val & WDIOS_DISABLECARD) {
360 err = watchdog_stop(wdd);
361 if (err < 0)
362 return err;
363 }
364 if (val & WDIOS_ENABLECARD) {
365 err = watchdog_start(wdd);
366 if (err < 0)
367 return err;
368 }
369 return 0;
370 case WDIOC_KEEPALIVE:
371 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
372 return -EOPNOTSUPP;
373 watchdog_ping(wdd);
374 return 0;
375 case WDIOC_SETTIMEOUT:
376 if (get_user(val, p))
377 return -EFAULT;
378 err = watchdog_set_timeout(wdd, val);
379 if (err < 0)
380 return err;
381
382
383
384 watchdog_ping(wdd);
385
386 case WDIOC_GETTIMEOUT:
387
388 if (wdd->timeout == 0)
389 return -EOPNOTSUPP;
390 return put_user(wdd->timeout, p);
391 case WDIOC_GETTIMELEFT:
392 err = watchdog_get_timeleft(wdd, &val);
393 if (err)
394 return err;
395 return put_user(val, p);
396 default:
397 return -ENOTTY;
398 }
399}
400
401
402
403
404
405
406
407
408
409
410
411static int watchdog_open(struct inode *inode, struct file *file)
412{
413 int err = -EBUSY;
414 struct watchdog_device *wdd;
415
416
417 if (imajor(inode) == MISC_MAJOR)
418 wdd = old_wdd;
419 else
420 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
421
422
423 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
424 return -EBUSY;
425
426
427
428
429
430 if (!try_module_get(wdd->ops->owner))
431 goto out;
432
433 err = watchdog_start(wdd);
434 if (err < 0)
435 goto out_mod;
436
437 file->private_data = wdd;
438
439 if (wdd->ops->ref)
440 wdd->ops->ref(wdd);
441
442
443 return nonseekable_open(inode, file);
444
445out_mod:
446 module_put(wdd->ops->owner);
447out:
448 clear_bit(WDOG_DEV_OPEN, &wdd->status);
449 return err;
450}
451
452
453
454
455
456
457
458
459
460
461
462static int watchdog_release(struct inode *inode, struct file *file)
463{
464 struct watchdog_device *wdd = file->private_data;
465 int err = -EBUSY;
466
467
468
469
470
471
472 if (!test_bit(WDOG_ACTIVE, &wdd->status))
473 err = 0;
474 else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
475 !(wdd->info->options & WDIOF_MAGICCLOSE))
476 err = watchdog_stop(wdd);
477
478
479 if (err < 0) {
480 mutex_lock(&wdd->lock);
481 if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
482 dev_crit(wdd->dev, "watchdog did not stop!\n");
483 mutex_unlock(&wdd->lock);
484 watchdog_ping(wdd);
485 }
486
487
488 module_put(wdd->ops->owner);
489
490
491 clear_bit(WDOG_DEV_OPEN, &wdd->status);
492
493
494 if (wdd->ops->unref)
495 wdd->ops->unref(wdd);
496
497 return 0;
498}
499
500static const struct file_operations watchdog_fops = {
501 .owner = THIS_MODULE,
502 .write = watchdog_write,
503 .unlocked_ioctl = watchdog_ioctl,
504 .open = watchdog_open,
505 .release = watchdog_release,
506};
507
508static struct miscdevice watchdog_miscdev = {
509 .minor = WATCHDOG_MINOR,
510 .name = "watchdog",
511 .fops = &watchdog_fops,
512};
513
514
515
516
517
518
519
520
521
522
523int watchdog_dev_register(struct watchdog_device *watchdog)
524{
525 int err, devno;
526
527 if (watchdog->id == 0) {
528 old_wdd = watchdog;
529 watchdog_miscdev.parent = watchdog->parent;
530 err = misc_register(&watchdog_miscdev);
531 if (err != 0) {
532 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
533 watchdog->info->identity, WATCHDOG_MINOR, err);
534 if (err == -EBUSY)
535 pr_err("%s: a legacy watchdog module is probably present.\n",
536 watchdog->info->identity);
537 old_wdd = NULL;
538 return err;
539 }
540 }
541
542
543 devno = MKDEV(MAJOR(watchdog_devt), watchdog->id);
544 cdev_init(&watchdog->cdev, &watchdog_fops);
545 watchdog->cdev.owner = watchdog->ops->owner;
546
547
548 err = cdev_add(&watchdog->cdev, devno, 1);
549 if (err) {
550 pr_err("watchdog%d unable to add device %d:%d\n",
551 watchdog->id, MAJOR(watchdog_devt), watchdog->id);
552 if (watchdog->id == 0) {
553 misc_deregister(&watchdog_miscdev);
554 old_wdd = NULL;
555 }
556 }
557 return err;
558}
559
560
561
562
563
564
565
566
567int watchdog_dev_unregister(struct watchdog_device *watchdog)
568{
569 mutex_lock(&watchdog->lock);
570 set_bit(WDOG_UNREGISTERED, &watchdog->status);
571 mutex_unlock(&watchdog->lock);
572
573 cdev_del(&watchdog->cdev);
574 if (watchdog->id == 0) {
575 misc_deregister(&watchdog_miscdev);
576 old_wdd = NULL;
577 }
578 return 0;
579}
580
581
582
583
584
585
586
587int __init watchdog_dev_init(void)
588{
589 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
590 if (err < 0)
591 pr_err("watchdog: unable to allocate char dev region\n");
592 return err;
593}
594
595
596
597
598
599
600
601void __exit watchdog_dev_exit(void)
602{
603 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
604}
605