watchdog: Introduce WDOG_HW_RUNNING flag
[deliverable/linux.git] / drivers / watchdog / watchdog_dev.c
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/cdev.h> /* For character device */
36 #include <linux/errno.h> /* For the -ENODEV/... values */
37 #include <linux/fs.h> /* For file operations */
38 #include <linux/init.h> /* For __init/__exit/... */
39 #include <linux/jiffies.h> /* For timeout functions */
40 #include <linux/kernel.h> /* For printk/panic/... */
41 #include <linux/kref.h> /* For data references */
42 #include <linux/miscdevice.h> /* For handling misc devices */
43 #include <linux/module.h> /* For module stuff/... */
44 #include <linux/mutex.h> /* For mutexes */
45 #include <linux/slab.h> /* For memory functions */
46 #include <linux/types.h> /* For standard types (like size_t) */
47 #include <linux/watchdog.h> /* For watchdog specific items */
48 #include <linux/workqueue.h> /* For workqueue */
49 #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
50
51 #include "watchdog_core.h"
52
53 /*
54 * struct watchdog_core_data - watchdog core internal data
55 * @kref: Reference count.
56 * @cdev: The watchdog's Character device.
57 * @wdd: Pointer to watchdog device.
58 * @lock: Lock for watchdog core.
59 * @status: Watchdog core internal status bits.
60 */
61 struct watchdog_core_data {
62 struct kref kref;
63 struct cdev cdev;
64 struct watchdog_device *wdd;
65 struct mutex lock;
66 unsigned long last_keepalive;
67 struct delayed_work work;
68 unsigned long status; /* Internal status bits */
69 #define _WDOG_DEV_OPEN 0 /* Opened ? */
70 #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
71 };
72
73 /* the dev_t structure to store the dynamically allocated watchdog devices */
74 static dev_t watchdog_devt;
75 /* Reference to watchdog device behind /dev/watchdog */
76 static struct watchdog_core_data *old_wd_data;
77
78 static struct workqueue_struct *watchdog_wq;
79
80 static inline bool watchdog_need_worker(struct watchdog_device *wdd)
81 {
82 /* All variables in milli-seconds */
83 unsigned int hm = wdd->max_hw_heartbeat_ms;
84 unsigned int t = wdd->timeout * 1000;
85
86 /*
87 * A worker to generate heartbeat requests is needed if all of the
88 * following conditions are true.
89 * - Userspace activated the watchdog.
90 * - The driver provided a value for the maximum hardware timeout, and
91 * thus is aware that the framework supports generating heartbeat
92 * requests.
93 * - Userspace requests a longer timeout than the hardware can handle.
94 */
95 return hm && ((watchdog_active(wdd) && t > hm) ||
96 (t && !watchdog_active(wdd) && watchdog_hw_running(wdd)));
97 }
98
99 static long watchdog_next_keepalive(struct watchdog_device *wdd)
100 {
101 struct watchdog_core_data *wd_data = wdd->wd_data;
102 unsigned int timeout_ms = wdd->timeout * 1000;
103 unsigned long keepalive_interval;
104 unsigned long last_heartbeat;
105 unsigned long virt_timeout;
106 unsigned int hw_heartbeat_ms;
107
108 virt_timeout = wd_data->last_keepalive + msecs_to_jiffies(timeout_ms);
109 hw_heartbeat_ms = min(timeout_ms, wdd->max_hw_heartbeat_ms);
110 keepalive_interval = msecs_to_jiffies(hw_heartbeat_ms / 2);
111
112 if (!watchdog_active(wdd))
113 return keepalive_interval;
114
115 /*
116 * To ensure that the watchdog times out wdd->timeout seconds
117 * after the most recent ping from userspace, the last
118 * worker ping has to come in hw_heartbeat_ms before this timeout.
119 */
120 last_heartbeat = virt_timeout - msecs_to_jiffies(hw_heartbeat_ms);
121 return min_t(long, last_heartbeat - jiffies, keepalive_interval);
122 }
123
124 static inline void watchdog_update_worker(struct watchdog_device *wdd)
125 {
126 struct watchdog_core_data *wd_data = wdd->wd_data;
127
128 if (watchdog_need_worker(wdd)) {
129 long t = watchdog_next_keepalive(wdd);
130
131 if (t > 0)
132 mod_delayed_work(watchdog_wq, &wd_data->work, t);
133 } else {
134 cancel_delayed_work(&wd_data->work);
135 }
136 }
137
138 static int __watchdog_ping(struct watchdog_device *wdd)
139 {
140 int err;
141
142 if (wdd->ops->ping)
143 err = wdd->ops->ping(wdd); /* ping the watchdog */
144 else
145 err = wdd->ops->start(wdd); /* restart watchdog */
146
147 watchdog_update_worker(wdd);
148
149 return err;
150 }
151
152 /*
153 * watchdog_ping: ping the watchdog.
154 * @wdd: the watchdog device to ping
155 *
156 * The caller must hold wd_data->lock.
157 *
158 * If the watchdog has no own ping operation then it needs to be
159 * restarted via the start operation. This wrapper function does
160 * exactly that.
161 * We only ping when the watchdog device is running.
162 */
163
164 static int watchdog_ping(struct watchdog_device *wdd)
165 {
166 struct watchdog_core_data *wd_data = wdd->wd_data;
167
168 if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
169 return 0;
170
171 wd_data->last_keepalive = jiffies;
172 return __watchdog_ping(wdd);
173 }
174
175 static void watchdog_ping_work(struct work_struct *work)
176 {
177 struct watchdog_core_data *wd_data;
178 struct watchdog_device *wdd;
179
180 wd_data = container_of(to_delayed_work(work), struct watchdog_core_data,
181 work);
182
183 mutex_lock(&wd_data->lock);
184 wdd = wd_data->wdd;
185 if (wdd && (watchdog_active(wdd) || watchdog_hw_running(wdd)))
186 __watchdog_ping(wdd);
187 mutex_unlock(&wd_data->lock);
188 }
189
190 /*
191 * watchdog_start: wrapper to start the watchdog.
192 * @wdd: the watchdog device to start
193 *
194 * The caller must hold wd_data->lock.
195 *
196 * Start the watchdog if it is not active and mark it active.
197 * This function returns zero on success or a negative errno code for
198 * failure.
199 */
200
201 static int watchdog_start(struct watchdog_device *wdd)
202 {
203 struct watchdog_core_data *wd_data = wdd->wd_data;
204 unsigned long started_at;
205 int err;
206
207 if (watchdog_active(wdd))
208 return 0;
209
210 started_at = jiffies;
211 if (watchdog_hw_running(wdd) && wdd->ops->ping)
212 err = wdd->ops->ping(wdd);
213 else
214 err = wdd->ops->start(wdd);
215 if (err == 0) {
216 set_bit(WDOG_ACTIVE, &wdd->status);
217 wd_data->last_keepalive = started_at;
218 watchdog_update_worker(wdd);
219 }
220
221 return err;
222 }
223
224 /*
225 * watchdog_stop: wrapper to stop the watchdog.
226 * @wdd: the watchdog device to stop
227 *
228 * The caller must hold wd_data->lock.
229 *
230 * Stop the watchdog if it is still active and unmark it active.
231 * This function returns zero on success or a negative errno code for
232 * failure.
233 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
234 */
235
236 static int watchdog_stop(struct watchdog_device *wdd)
237 {
238 int err = 0;
239
240 if (!watchdog_active(wdd))
241 return 0;
242
243 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
244 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
245 wdd->id);
246 return -EBUSY;
247 }
248
249 err = wdd->ops->stop(wdd);
250 if (err == 0) {
251 clear_bit(WDOG_ACTIVE, &wdd->status);
252 watchdog_update_worker(wdd);
253 }
254
255 return err;
256 }
257
258 /*
259 * watchdog_get_status: wrapper to get the watchdog status
260 * @wdd: the watchdog device to get the status from
261 *
262 * The caller must hold wd_data->lock.
263 *
264 * Get the watchdog's status flags.
265 */
266
267 static unsigned int watchdog_get_status(struct watchdog_device *wdd)
268 {
269 if (!wdd->ops->status)
270 return 0;
271
272 return wdd->ops->status(wdd);
273 }
274
275 /*
276 * watchdog_set_timeout: set the watchdog timer timeout
277 * @wdd: the watchdog device to set the timeout for
278 * @timeout: timeout to set in seconds
279 *
280 * The caller must hold wd_data->lock.
281 */
282
283 static int watchdog_set_timeout(struct watchdog_device *wdd,
284 unsigned int timeout)
285 {
286 int err = 0;
287
288 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
289 return -EOPNOTSUPP;
290
291 if (watchdog_timeout_invalid(wdd, timeout))
292 return -EINVAL;
293
294 if (wdd->ops->set_timeout)
295 err = wdd->ops->set_timeout(wdd, timeout);
296 else
297 wdd->timeout = timeout;
298
299 watchdog_update_worker(wdd);
300
301 return err;
302 }
303
304 /*
305 * watchdog_get_timeleft: wrapper to get the time left before a reboot
306 * @wdd: the watchdog device to get the remaining time from
307 * @timeleft: the time that's left
308 *
309 * The caller must hold wd_data->lock.
310 *
311 * Get the time before a watchdog will reboot (if not pinged).
312 */
313
314 static int watchdog_get_timeleft(struct watchdog_device *wdd,
315 unsigned int *timeleft)
316 {
317 *timeleft = 0;
318
319 if (!wdd->ops->get_timeleft)
320 return -EOPNOTSUPP;
321
322 *timeleft = wdd->ops->get_timeleft(wdd);
323
324 return 0;
325 }
326
327 #ifdef CONFIG_WATCHDOG_SYSFS
328 static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
329 char *buf)
330 {
331 struct watchdog_device *wdd = dev_get_drvdata(dev);
332
333 return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
334 }
335 static DEVICE_ATTR_RO(nowayout);
336
337 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
338 char *buf)
339 {
340 struct watchdog_device *wdd = dev_get_drvdata(dev);
341 struct watchdog_core_data *wd_data = wdd->wd_data;
342 unsigned int status;
343
344 mutex_lock(&wd_data->lock);
345 status = watchdog_get_status(wdd);
346 mutex_unlock(&wd_data->lock);
347
348 return sprintf(buf, "%u\n", status);
349 }
350 static DEVICE_ATTR_RO(status);
351
352 static ssize_t bootstatus_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
354 {
355 struct watchdog_device *wdd = dev_get_drvdata(dev);
356
357 return sprintf(buf, "%u\n", wdd->bootstatus);
358 }
359 static DEVICE_ATTR_RO(bootstatus);
360
361 static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
362 char *buf)
363 {
364 struct watchdog_device *wdd = dev_get_drvdata(dev);
365 struct watchdog_core_data *wd_data = wdd->wd_data;
366 ssize_t status;
367 unsigned int val;
368
369 mutex_lock(&wd_data->lock);
370 status = watchdog_get_timeleft(wdd, &val);
371 mutex_unlock(&wd_data->lock);
372 if (!status)
373 status = sprintf(buf, "%u\n", val);
374
375 return status;
376 }
377 static DEVICE_ATTR_RO(timeleft);
378
379 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
380 char *buf)
381 {
382 struct watchdog_device *wdd = dev_get_drvdata(dev);
383
384 return sprintf(buf, "%u\n", wdd->timeout);
385 }
386 static DEVICE_ATTR_RO(timeout);
387
388 static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
389 char *buf)
390 {
391 struct watchdog_device *wdd = dev_get_drvdata(dev);
392
393 return sprintf(buf, "%s\n", wdd->info->identity);
394 }
395 static DEVICE_ATTR_RO(identity);
396
397 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
398 char *buf)
399 {
400 struct watchdog_device *wdd = dev_get_drvdata(dev);
401
402 if (watchdog_active(wdd))
403 return sprintf(buf, "active\n");
404
405 return sprintf(buf, "inactive\n");
406 }
407 static DEVICE_ATTR_RO(state);
408
409 static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
410 int n)
411 {
412 struct device *dev = container_of(kobj, struct device, kobj);
413 struct watchdog_device *wdd = dev_get_drvdata(dev);
414 umode_t mode = attr->mode;
415
416 if (attr == &dev_attr_status.attr && !wdd->ops->status)
417 mode = 0;
418 else if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
419 mode = 0;
420
421 return mode;
422 }
423 static struct attribute *wdt_attrs[] = {
424 &dev_attr_state.attr,
425 &dev_attr_identity.attr,
426 &dev_attr_timeout.attr,
427 &dev_attr_timeleft.attr,
428 &dev_attr_bootstatus.attr,
429 &dev_attr_status.attr,
430 &dev_attr_nowayout.attr,
431 NULL,
432 };
433
434 static const struct attribute_group wdt_group = {
435 .attrs = wdt_attrs,
436 .is_visible = wdt_is_visible,
437 };
438 __ATTRIBUTE_GROUPS(wdt);
439 #else
440 #define wdt_groups NULL
441 #endif
442
443 /*
444 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
445 * @wdd: the watchdog device to do the ioctl on
446 * @cmd: watchdog command
447 * @arg: argument pointer
448 *
449 * The caller must hold wd_data->lock.
450 */
451
452 static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
453 unsigned long arg)
454 {
455 if (!wdd->ops->ioctl)
456 return -ENOIOCTLCMD;
457
458 return wdd->ops->ioctl(wdd, cmd, arg);
459 }
460
461 /*
462 * watchdog_write: writes to the watchdog.
463 * @file: file from VFS
464 * @data: user address of data
465 * @len: length of data
466 * @ppos: pointer to the file offset
467 *
468 * A write to a watchdog device is defined as a keepalive ping.
469 * Writing the magic 'V' sequence allows the next close to turn
470 * off the watchdog (if 'nowayout' is not set).
471 */
472
473 static ssize_t watchdog_write(struct file *file, const char __user *data,
474 size_t len, loff_t *ppos)
475 {
476 struct watchdog_core_data *wd_data = file->private_data;
477 struct watchdog_device *wdd;
478 int err;
479 size_t i;
480 char c;
481
482 if (len == 0)
483 return 0;
484
485 /*
486 * Note: just in case someone wrote the magic character
487 * five months ago...
488 */
489 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
490
491 /* scan to see whether or not we got the magic character */
492 for (i = 0; i != len; i++) {
493 if (get_user(c, data + i))
494 return -EFAULT;
495 if (c == 'V')
496 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
497 }
498
499 /* someone wrote to us, so we send the watchdog a keepalive ping */
500
501 err = -ENODEV;
502 mutex_lock(&wd_data->lock);
503 wdd = wd_data->wdd;
504 if (wdd)
505 err = watchdog_ping(wdd);
506 mutex_unlock(&wd_data->lock);
507
508 if (err < 0)
509 return err;
510
511 return len;
512 }
513
514 /*
515 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
516 * @file: file handle to the device
517 * @cmd: watchdog command
518 * @arg: argument pointer
519 *
520 * The watchdog API defines a common set of functions for all watchdogs
521 * according to their available features.
522 */
523
524 static long watchdog_ioctl(struct file *file, unsigned int cmd,
525 unsigned long arg)
526 {
527 struct watchdog_core_data *wd_data = file->private_data;
528 void __user *argp = (void __user *)arg;
529 struct watchdog_device *wdd;
530 int __user *p = argp;
531 unsigned int val;
532 int err;
533
534 mutex_lock(&wd_data->lock);
535
536 wdd = wd_data->wdd;
537 if (!wdd) {
538 err = -ENODEV;
539 goto out_ioctl;
540 }
541
542 err = watchdog_ioctl_op(wdd, cmd, arg);
543 if (err != -ENOIOCTLCMD)
544 goto out_ioctl;
545
546 switch (cmd) {
547 case WDIOC_GETSUPPORT:
548 err = copy_to_user(argp, wdd->info,
549 sizeof(struct watchdog_info)) ? -EFAULT : 0;
550 break;
551 case WDIOC_GETSTATUS:
552 val = watchdog_get_status(wdd);
553 err = put_user(val, p);
554 break;
555 case WDIOC_GETBOOTSTATUS:
556 err = put_user(wdd->bootstatus, p);
557 break;
558 case WDIOC_SETOPTIONS:
559 if (get_user(val, p)) {
560 err = -EFAULT;
561 break;
562 }
563 if (val & WDIOS_DISABLECARD) {
564 err = watchdog_stop(wdd);
565 if (err < 0)
566 break;
567 }
568 if (val & WDIOS_ENABLECARD)
569 err = watchdog_start(wdd);
570 break;
571 case WDIOC_KEEPALIVE:
572 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
573 err = -EOPNOTSUPP;
574 break;
575 }
576 err = watchdog_ping(wdd);
577 break;
578 case WDIOC_SETTIMEOUT:
579 if (get_user(val, p)) {
580 err = -EFAULT;
581 break;
582 }
583 err = watchdog_set_timeout(wdd, val);
584 if (err < 0)
585 break;
586 /* If the watchdog is active then we send a keepalive ping
587 * to make sure that the watchdog keep's running (and if
588 * possible that it takes the new timeout) */
589 err = watchdog_ping(wdd);
590 if (err < 0)
591 break;
592 /* Fall */
593 case WDIOC_GETTIMEOUT:
594 /* timeout == 0 means that we don't know the timeout */
595 if (wdd->timeout == 0) {
596 err = -EOPNOTSUPP;
597 break;
598 }
599 err = put_user(wdd->timeout, p);
600 break;
601 case WDIOC_GETTIMELEFT:
602 err = watchdog_get_timeleft(wdd, &val);
603 if (err < 0)
604 break;
605 err = put_user(val, p);
606 break;
607 default:
608 err = -ENOTTY;
609 break;
610 }
611
612 out_ioctl:
613 mutex_unlock(&wd_data->lock);
614 return err;
615 }
616
617 /*
618 * watchdog_open: open the /dev/watchdog* devices.
619 * @inode: inode of device
620 * @file: file handle to device
621 *
622 * When the /dev/watchdog* device gets opened, we start the watchdog.
623 * Watch out: the /dev/watchdog device is single open, so we make sure
624 * it can only be opened once.
625 */
626
627 static int watchdog_open(struct inode *inode, struct file *file)
628 {
629 struct watchdog_core_data *wd_data;
630 struct watchdog_device *wdd;
631 int err;
632
633 /* Get the corresponding watchdog device */
634 if (imajor(inode) == MISC_MAJOR)
635 wd_data = old_wd_data;
636 else
637 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
638 cdev);
639
640 /* the watchdog is single open! */
641 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
642 return -EBUSY;
643
644 wdd = wd_data->wdd;
645
646 /*
647 * If the /dev/watchdog device is open, we don't want the module
648 * to be unloaded.
649 */
650 if (!watchdog_hw_running(wdd) && !try_module_get(wdd->ops->owner)) {
651 err = -EBUSY;
652 goto out_clear;
653 }
654
655 err = watchdog_start(wdd);
656 if (err < 0)
657 goto out_mod;
658
659 file->private_data = wd_data;
660
661 if (!watchdog_hw_running(wdd))
662 kref_get(&wd_data->kref);
663
664 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
665 return nonseekable_open(inode, file);
666
667 out_mod:
668 module_put(wd_data->wdd->ops->owner);
669 out_clear:
670 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
671 return err;
672 }
673
674 static void watchdog_core_data_release(struct kref *kref)
675 {
676 struct watchdog_core_data *wd_data;
677
678 wd_data = container_of(kref, struct watchdog_core_data, kref);
679
680 kfree(wd_data);
681 }
682
683 /*
684 * watchdog_release: release the watchdog device.
685 * @inode: inode of device
686 * @file: file handle to device
687 *
688 * This is the code for when /dev/watchdog gets closed. We will only
689 * stop the watchdog when we have received the magic char (and nowayout
690 * was not set), else the watchdog will keep running.
691 */
692
693 static int watchdog_release(struct inode *inode, struct file *file)
694 {
695 struct watchdog_core_data *wd_data = file->private_data;
696 struct watchdog_device *wdd;
697 int err = -EBUSY;
698
699 mutex_lock(&wd_data->lock);
700
701 wdd = wd_data->wdd;
702 if (!wdd)
703 goto done;
704
705 /*
706 * We only stop the watchdog if we received the magic character
707 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
708 * watchdog_stop will fail.
709 */
710 if (!test_bit(WDOG_ACTIVE, &wdd->status))
711 err = 0;
712 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
713 !(wdd->info->options & WDIOF_MAGICCLOSE))
714 err = watchdog_stop(wdd);
715
716 /* If the watchdog was not stopped, send a keepalive ping */
717 if (err < 0) {
718 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
719 watchdog_ping(wdd);
720 }
721
722 cancel_delayed_work_sync(&wd_data->work);
723 watchdog_update_worker(wdd);
724
725 /* make sure that /dev/watchdog can be re-opened */
726 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
727
728 done:
729 mutex_unlock(&wd_data->lock);
730 /*
731 * Allow the owner module to be unloaded again unless the watchdog
732 * is still running. If the watchdog is still running, it can not
733 * be stopped, and its driver must not be unloaded.
734 */
735 if (!watchdog_hw_running(wdd)) {
736 module_put(wdd->ops->owner);
737 kref_put(&wd_data->kref, watchdog_core_data_release);
738 }
739 return 0;
740 }
741
742 static const struct file_operations watchdog_fops = {
743 .owner = THIS_MODULE,
744 .write = watchdog_write,
745 .unlocked_ioctl = watchdog_ioctl,
746 .open = watchdog_open,
747 .release = watchdog_release,
748 };
749
750 static struct miscdevice watchdog_miscdev = {
751 .minor = WATCHDOG_MINOR,
752 .name = "watchdog",
753 .fops = &watchdog_fops,
754 };
755
756 /*
757 * watchdog_cdev_register: register watchdog character device
758 * @wdd: watchdog device
759 * @devno: character device number
760 *
761 * Register a watchdog character device including handling the legacy
762 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
763 * thus we set it up like that.
764 */
765
766 static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
767 {
768 struct watchdog_core_data *wd_data;
769 int err;
770
771 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
772 if (!wd_data)
773 return -ENOMEM;
774 kref_init(&wd_data->kref);
775 mutex_init(&wd_data->lock);
776
777 wd_data->wdd = wdd;
778 wdd->wd_data = wd_data;
779
780 if (!watchdog_wq)
781 return -ENODEV;
782
783 INIT_DELAYED_WORK(&wd_data->work, watchdog_ping_work);
784
785 if (wdd->id == 0) {
786 old_wd_data = wd_data;
787 watchdog_miscdev.parent = wdd->parent;
788 err = misc_register(&watchdog_miscdev);
789 if (err != 0) {
790 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
791 wdd->info->identity, WATCHDOG_MINOR, err);
792 if (err == -EBUSY)
793 pr_err("%s: a legacy watchdog module is probably present.\n",
794 wdd->info->identity);
795 old_wd_data = NULL;
796 kfree(wd_data);
797 return err;
798 }
799 }
800
801 /* Fill in the data structures */
802 cdev_init(&wd_data->cdev, &watchdog_fops);
803 wd_data->cdev.owner = wdd->ops->owner;
804
805 /* Add the device */
806 err = cdev_add(&wd_data->cdev, devno, 1);
807 if (err) {
808 pr_err("watchdog%d unable to add device %d:%d\n",
809 wdd->id, MAJOR(watchdog_devt), wdd->id);
810 if (wdd->id == 0) {
811 misc_deregister(&watchdog_miscdev);
812 old_wd_data = NULL;
813 kref_put(&wd_data->kref, watchdog_core_data_release);
814 }
815 return err;
816 }
817
818 /*
819 * If the watchdog is running, prevent its driver from being unloaded,
820 * and schedule an immediate ping.
821 */
822 if (watchdog_hw_running(wdd)) {
823 __module_get(wdd->ops->owner);
824 kref_get(&wd_data->kref);
825 queue_delayed_work(watchdog_wq, &wd_data->work, 0);
826 }
827
828 return 0;
829 }
830
831 /*
832 * watchdog_cdev_unregister: unregister watchdog character device
833 * @watchdog: watchdog device
834 *
835 * Unregister watchdog character device and if needed the legacy
836 * /dev/watchdog device.
837 */
838
839 static void watchdog_cdev_unregister(struct watchdog_device *wdd)
840 {
841 struct watchdog_core_data *wd_data = wdd->wd_data;
842
843 cdev_del(&wd_data->cdev);
844 if (wdd->id == 0) {
845 misc_deregister(&watchdog_miscdev);
846 old_wd_data = NULL;
847 }
848
849 mutex_lock(&wd_data->lock);
850 wd_data->wdd = NULL;
851 wdd->wd_data = NULL;
852 mutex_unlock(&wd_data->lock);
853
854 cancel_delayed_work_sync(&wd_data->work);
855
856 kref_put(&wd_data->kref, watchdog_core_data_release);
857 }
858
859 static struct class watchdog_class = {
860 .name = "watchdog",
861 .owner = THIS_MODULE,
862 .dev_groups = wdt_groups,
863 };
864
865 /*
866 * watchdog_dev_register: register a watchdog device
867 * @wdd: watchdog device
868 *
869 * Register a watchdog device including handling the legacy
870 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
871 * thus we set it up like that.
872 */
873
874 int watchdog_dev_register(struct watchdog_device *wdd)
875 {
876 struct device *dev;
877 dev_t devno;
878 int ret;
879
880 devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
881
882 ret = watchdog_cdev_register(wdd, devno);
883 if (ret)
884 return ret;
885
886 dev = device_create_with_groups(&watchdog_class, wdd->parent,
887 devno, wdd, wdd->groups,
888 "watchdog%d", wdd->id);
889 if (IS_ERR(dev)) {
890 watchdog_cdev_unregister(wdd);
891 return PTR_ERR(dev);
892 }
893
894 return ret;
895 }
896
897 /*
898 * watchdog_dev_unregister: unregister a watchdog device
899 * @watchdog: watchdog device
900 *
901 * Unregister watchdog device and if needed the legacy
902 * /dev/watchdog device.
903 */
904
905 void watchdog_dev_unregister(struct watchdog_device *wdd)
906 {
907 device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
908 watchdog_cdev_unregister(wdd);
909 }
910
911 /*
912 * watchdog_dev_init: init dev part of watchdog core
913 *
914 * Allocate a range of chardev nodes to use for watchdog devices
915 */
916
917 int __init watchdog_dev_init(void)
918 {
919 int err;
920
921 watchdog_wq = alloc_workqueue("watchdogd",
922 WQ_HIGHPRI | WQ_MEM_RECLAIM, 0);
923 if (!watchdog_wq) {
924 pr_err("Failed to create watchdog workqueue\n");
925 return -ENOMEM;
926 }
927
928 err = class_register(&watchdog_class);
929 if (err < 0) {
930 pr_err("couldn't register class\n");
931 return err;
932 }
933
934 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
935 if (err < 0) {
936 pr_err("watchdog: unable to allocate char dev region\n");
937 class_unregister(&watchdog_class);
938 return err;
939 }
940
941 return 0;
942 }
943
944 /*
945 * watchdog_dev_exit: exit dev part of watchdog core
946 *
947 * Release the range of chardev nodes used for watchdog devices
948 */
949
950 void __exit watchdog_dev_exit(void)
951 {
952 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
953 class_unregister(&watchdog_class);
954 destroy_workqueue(watchdog_wq);
955 }
This page took 0.058685 seconds and 6 git commands to generate.