aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/watchdog
diff options
context:
space:
mode:
authorWim Van Sebroeck <wim@iguana.be>2011-07-22 14:55:18 -0400
committerWim Van Sebroeck <wim@iguana.be>2011-07-28 04:01:04 -0400
commit43316044d4f64da008d6aca7d4b60771b9a24eb8 (patch)
tree66d0d023a8713119d973e3c367efa21fb5a1908f /drivers/watchdog
parent5efc7a6222f6408d29d6beb1142a302f31dc9eac (diff)
watchdog: WatchDog Timer Driver Core - Add basic framework
The WatchDog Timer Driver Core is a framework that contains the common code for all watchdog-driver's. It also introduces a watchdog device structure and the operations that go with it. This is the introduction of this framework. This part supports the minimal watchdog userspace API (or with other words: the functionality to use /dev/watchdog's open, release and write functionality as defined in the simplest watchdog API). Extra functionality will follow in the next set of patches. Signed-off-by: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Wim Van Sebroeck <wim@iguana.be> Acked-by: Arnd Bergmann <arnd@arndb.de> Acked-by: Wolfram Sang <w.sang@pengutronix.de>
Diffstat (limited to 'drivers/watchdog')
-rw-r--r--drivers/watchdog/Kconfig11
-rw-r--r--drivers/watchdog/Makefile4
-rw-r--r--drivers/watchdog/watchdog_core.c101
-rw-r--r--drivers/watchdog/watchdog_dev.c235
-rw-r--r--drivers/watchdog/watchdog_dev.h33
5 files changed, 384 insertions, 0 deletions
diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index 0635e72e079..f441726ddf2 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -28,6 +28,17 @@ menuconfig WATCHDOG
28 28
29if WATCHDOG 29if WATCHDOG
30 30
31config WATCHDOG_CORE
32 bool "WatchDog Timer Driver Core"
33 ---help---
34 Say Y here if you want to use the new watchdog timer driver core.
35 This driver provides a framework for all watchdog timer drivers
36 and gives them the /dev/watchdog interface (and later also the
37 sysfs interface).
38
39 To compile this driver as a module, choose M here: the module will
40 be called watchdog.
41
31config WATCHDOG_NOWAYOUT 42config WATCHDOG_NOWAYOUT
32 bool "Disable watchdog shutdown on close" 43 bool "Disable watchdog shutdown on close"
33 help 44 help
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 9eaa212398d..55bd5740e91 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -2,6 +2,10 @@
2# Makefile for the WatchDog device drivers. 2# Makefile for the WatchDog device drivers.
3# 3#
4 4
5# The WatchDog Timer Driver Core.
6watchdog-objs += watchdog_core.o watchdog_dev.o
7obj-$(CONFIG_WATCHDOG_CORE) += watchdog.o
8
5# Only one watchdog can succeed. We probe the ISA/PCI/USB based 9# Only one watchdog can succeed. We probe the ISA/PCI/USB based
6# watchdog-cards first, then the architecture specific watchdog 10# watchdog-cards first, then the architecture specific watchdog
7# drivers and then the architecture independent "softdog" driver. 11# drivers and then the architecture independent "softdog" driver.
diff --git a/drivers/watchdog/watchdog_core.c b/drivers/watchdog/watchdog_core.c
new file mode 100644
index 00000000000..47fc1267ad4
--- /dev/null
+++ b/drivers/watchdog/watchdog_core.c
@@ -0,0 +1,101 @@
1/*
2 * watchdog_core.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 * This source code is part of the generic code that can be used
10 * by all the watchdog timer drivers.
11 *
12 * Based on source code of the following authors:
13 * Matt Domsch <Matt_Domsch@dell.com>,
14 * Rob Radez <rob@osinvestor.com>,
15 * Rusty Lynch <rusty@linux.co.intel.com>
16 * Satyam Sharma <satyam@infradead.org>
17 * Randy Dunlap <randy.dunlap@oracle.com>
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 *
24 * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
25 * admit liability nor provide warranty for any of this software.
26 * This material is provided "AS-IS" and at no charge.
27 */
28
29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31#include <linux/module.h> /* For EXPORT_SYMBOL/module stuff/... */
32#include <linux/types.h> /* For standard types */
33#include <linux/errno.h> /* For the -ENODEV/... values */
34#include <linux/kernel.h> /* For printk/panic/... */
35#include <linux/watchdog.h> /* For watchdog specific items */
36#include <linux/init.h> /* For __init/__exit/... */
37
38#include "watchdog_dev.h" /* For watchdog_dev_register/... */
39
40/**
41 * watchdog_register_device() - register a watchdog device
42 * @wdd: watchdog device
43 *
44 * Register a watchdog device with the kernel so that the
45 * watchdog timer can be accessed from userspace.
46 *
47 * A zero is returned on success and a negative errno code for
48 * failure.
49 */
50int watchdog_register_device(struct watchdog_device *wdd)
51{
52 int ret;
53
54 if (wdd == NULL || wdd->info == NULL || wdd->ops == NULL)
55 return -EINVAL;
56
57 /* Mandatory operations need to be supported */
58 if (wdd->ops->start == NULL || wdd->ops->stop == NULL)
59 return -EINVAL;
60
61 /*
62 * Note: now that all watchdog_device data has been verified, we
63 * will not check this anymore in other functions. If data gets
64 * corrupted in a later stage then we expect a kernel panic!
65 */
66
67 /* We only support 1 watchdog device via the /dev/watchdog interface */
68 ret = watchdog_dev_register(wdd);
69 if (ret) {
70 pr_err("error registering /dev/watchdog (err=%d).\n", ret);
71 return ret;
72 }
73
74 return 0;
75}
76EXPORT_SYMBOL_GPL(watchdog_register_device);
77
78/**
79 * watchdog_unregister_device() - unregister a watchdog device
80 * @wdd: watchdog device to unregister
81 *
82 * Unregister a watchdog device that was previously successfully
83 * registered with watchdog_register_device().
84 */
85void watchdog_unregister_device(struct watchdog_device *wdd)
86{
87 int ret;
88
89 if (wdd == NULL)
90 return;
91
92 ret = watchdog_dev_unregister(wdd);
93 if (ret)
94 pr_err("error unregistering /dev/watchdog (err=%d).\n", ret);
95}
96EXPORT_SYMBOL_GPL(watchdog_unregister_device);
97
98MODULE_AUTHOR("Alan Cox <alan@lxorguk.ukuu.org.uk>");
99MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
100MODULE_DESCRIPTION("WatchDog Timer Driver Core");
101MODULE_LICENSE("GPL");
diff --git a/drivers/watchdog/watchdog_dev.c b/drivers/watchdog/watchdog_dev.c
new file mode 100644
index 00000000000..366f49ce69b
--- /dev/null
+++ b/drivers/watchdog/watchdog_dev.c
@@ -0,0 +1,235 @@
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
45/* make sure we only register one /dev/watchdog device */
46static unsigned long watchdog_dev_busy;
47/* the watchdog device behind /dev/watchdog */
48static struct watchdog_device *wdd;
49
50/*
51 * watchdog_ping: ping the watchdog.
52 * @wddev: the watchdog device to ping
53 *
54 * If the watchdog has no own ping operation then it needs to be
55 * restarted via the start operation. This wrapper function does
56 * exactly that.
57 */
58
59static int watchdog_ping(struct watchdog_device *wddev)
60{
61 if (wddev->ops->ping)
62 return wddev->ops->ping(wddev); /* ping the watchdog */
63 else
64 return wddev->ops->start(wddev); /* restart the watchdog */
65}
66
67/*
68 * watchdog_write: writes to the watchdog.
69 * @file: file from VFS
70 * @data: user address of data
71 * @len: length of data
72 * @ppos: pointer to the file offset
73 *
74 * A write to a watchdog device is defined as a keepalive ping.
75 */
76
77static ssize_t watchdog_write(struct file *file, const char __user *data,
78 size_t len, loff_t *ppos)
79{
80 size_t i;
81 char c;
82
83 if (len == 0)
84 return 0;
85
86 for (i = 0; i != len; i++) {
87 if (get_user(c, data + i))
88 return -EFAULT;
89 }
90
91 /* someone wrote to us, so we send the watchdog a keepalive ping */
92 watchdog_ping(wdd);
93
94 return len;
95}
96
97/*
98 * watchdog_open: open the /dev/watchdog device.
99 * @inode: inode of device
100 * @file: file handle to device
101 *
102 * When the /dev/watchdog device gets opened, we start the watchdog.
103 * Watch out: the /dev/watchdog device is single open, so we make sure
104 * it can only be opened once.
105 */
106
107static int watchdog_open(struct inode *inode, struct file *file)
108{
109 int err = -EBUSY;
110
111 /* the watchdog is single open! */
112 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
113 return -EBUSY;
114
115 /*
116 * If the /dev/watchdog device is open, we don't want the module
117 * to be unloaded.
118 */
119 if (!try_module_get(wdd->ops->owner))
120 goto out;
121
122 err = wdd->ops->start(wdd);
123 if (err < 0)
124 goto out_mod;
125
126 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
127 return nonseekable_open(inode, file);
128
129out_mod:
130 module_put(wdd->ops->owner);
131out:
132 clear_bit(WDOG_DEV_OPEN, &wdd->status);
133 return err;
134}
135
136/*
137 * watchdog_release: release the /dev/watchdog device.
138 * @inode: inode of device
139 * @file: file handle to device
140 *
141 * This is the code for when /dev/watchdog gets closed.
142 */
143
144static int watchdog_release(struct inode *inode, struct file *file)
145{
146 int err;
147
148 err = wdd->ops->stop(wdd);
149 if (err != 0) {
150 pr_crit("%s: watchdog did not stop!\n", wdd->info->identity);
151 watchdog_ping(wdd);
152 }
153
154 /* Allow the owner module to be unloaded again */
155 module_put(wdd->ops->owner);
156
157 /* make sure that /dev/watchdog can be re-opened */
158 clear_bit(WDOG_DEV_OPEN, &wdd->status);
159
160 return 0;
161}
162
163static const struct file_operations watchdog_fops = {
164 .owner = THIS_MODULE,
165 .write = watchdog_write,
166 .open = watchdog_open,
167 .release = watchdog_release,
168};
169
170static struct miscdevice watchdog_miscdev = {
171 .minor = WATCHDOG_MINOR,
172 .name = "watchdog",
173 .fops = &watchdog_fops,
174};
175
176/*
177 * watchdog_dev_register:
178 * @watchdog: watchdog device
179 *
180 * Register a watchdog device as /dev/watchdog. /dev/watchdog
181 * is actually a miscdevice and thus we set it up like that.
182 */
183
184int watchdog_dev_register(struct watchdog_device *watchdog)
185{
186 int err;
187
188 /* Only one device can register for /dev/watchdog */
189 if (test_and_set_bit(0, &watchdog_dev_busy)) {
190 pr_err("only one watchdog can use /dev/watchdog.\n");
191 return -EBUSY;
192 }
193
194 wdd = watchdog;
195
196 err = misc_register(&watchdog_miscdev);
197 if (err != 0) {
198 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
199 watchdog->info->identity, WATCHDOG_MINOR, err);
200 goto out;
201 }
202
203 return 0;
204
205out:
206 wdd = NULL;
207 clear_bit(0, &watchdog_dev_busy);
208 return err;
209}
210
211/*
212 * watchdog_dev_unregister:
213 * @watchdog: watchdog device
214 *
215 * Deregister the /dev/watchdog device.
216 */
217
218int watchdog_dev_unregister(struct watchdog_device *watchdog)
219{
220 /* Check that a watchdog device was registered in the past */
221 if (!test_bit(0, &watchdog_dev_busy) || !wdd)
222 return -ENODEV;
223
224 /* We can only unregister the watchdog device that was registered */
225 if (watchdog != wdd) {
226 pr_err("%s: watchdog was not registered as /dev/watchdog.\n",
227 watchdog->info->identity);
228 return -ENODEV;
229 }
230
231 misc_deregister(&watchdog_miscdev);
232 wdd = NULL;
233 clear_bit(0, &watchdog_dev_busy);
234 return 0;
235}
diff --git a/drivers/watchdog/watchdog_dev.h b/drivers/watchdog/watchdog_dev.h
new file mode 100644
index 00000000000..bc7612be25c
--- /dev/null
+++ b/drivers/watchdog/watchdog_dev.h
@@ -0,0 +1,33 @@
1/*
2 * watchdog_core.h
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 * This source code is part of the generic code that can be used
10 * by all the watchdog timer drivers.
11 *
12 * Based on source code of the following authors:
13 * Matt Domsch <Matt_Domsch@dell.com>,
14 * Rob Radez <rob@osinvestor.com>,
15 * Rusty Lynch <rusty@linux.co.intel.com>
16 * Satyam Sharma <satyam@infradead.org>
17 * Randy Dunlap <randy.dunlap@oracle.com>
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version
22 * 2 of the License, or (at your option) any later version.
23 *
24 * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
25 * admit liability nor provide warranty for any of this software.
26 * This material is provided "AS-IS" and at no charge.
27 */
28
29/*
30 * Functions/procedures to be called by the core
31 */
32int watchdog_dev_register(struct watchdog_device *);
33int watchdog_dev_unregister(struct watchdog_device *);