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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52
53
54#include <linux/module.h>
55#include <linux/moduleparam.h>
56#include <linux/types.h>
57#include <linux/errno.h>
58#include <linux/kernel.h>
59#include <linux/miscdevice.h>
60#include <linux/watchdog.h>
61#include <linux/fs.h>
62#include <linux/ioport.h>
63#include <linux/platform_device.h>
64#include <linux/init.h>
65#include <linux/uaccess.h>
66#include <linux/io.h>
67
68
69#define DRV_NAME "acquirewdt"
70#define WATCHDOG_NAME "Acquire WDT"
71
72#define WATCHDOG_HEARTBEAT 0
73
74
75
76static struct platform_device *acq_platform_device;
77static unsigned long acq_is_open;
78static char expect_close;
79
80
81
82static int wdt_stop = 0x43;
83module_param(wdt_stop, int, 0);
84MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
85
86
87static int wdt_start = 0x443;
88module_param(wdt_start, int, 0);
89MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
90
91static bool nowayout = WATCHDOG_NOWAYOUT;
92module_param(nowayout, bool, 0);
93MODULE_PARM_DESC(nowayout,
94 "Watchdog cannot be stopped once started (default="
95 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
96
97
98
99
100
101static void acq_keepalive(void)
102{
103
104 inb_p(wdt_start);
105}
106
107static void acq_stop(void)
108{
109
110 inb_p(wdt_stop);
111}
112
113
114
115
116
117static ssize_t acq_write(struct file *file, const char __user *buf,
118 size_t count, loff_t *ppos)
119{
120
121 if (count) {
122 if (!nowayout) {
123 size_t i;
124
125
126 expect_close = 0;
127
128
129 for (i = 0; i != count; i++) {
130 char c;
131 if (get_user(c, buf + i))
132 return -EFAULT;
133 if (c == 'V')
134 expect_close = 42;
135 }
136 }
137
138
139 acq_keepalive();
140 }
141 return count;
142}
143
144static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
145{
146 int options, retval = -EINVAL;
147 void __user *argp = (void __user *)arg;
148 int __user *p = argp;
149 static const struct watchdog_info ident = {
150 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
151 .firmware_version = 1,
152 .identity = WATCHDOG_NAME,
153 };
154
155 switch (cmd) {
156 case WDIOC_GETSUPPORT:
157 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
158
159 case WDIOC_GETSTATUS:
160 case WDIOC_GETBOOTSTATUS:
161 return put_user(0, p);
162
163 case WDIOC_SETOPTIONS:
164 {
165 if (get_user(options, p))
166 return -EFAULT;
167 if (options & WDIOS_DISABLECARD) {
168 acq_stop();
169 retval = 0;
170 }
171 if (options & WDIOS_ENABLECARD) {
172 acq_keepalive();
173 retval = 0;
174 }
175 return retval;
176 }
177 case WDIOC_KEEPALIVE:
178 acq_keepalive();
179 return 0;
180
181 case WDIOC_GETTIMEOUT:
182 return put_user(WATCHDOG_HEARTBEAT, p);
183
184 default:
185 return -ENOTTY;
186 }
187}
188
189static int acq_open(struct inode *inode, struct file *file)
190{
191 if (test_and_set_bit(0, &acq_is_open))
192 return -EBUSY;
193
194 if (nowayout)
195 __module_get(THIS_MODULE);
196
197
198 acq_keepalive();
199 return stream_open(inode, file);
200}
201
202static int acq_close(struct inode *inode, struct file *file)
203{
204 if (expect_close == 42) {
205 acq_stop();
206 } else {
207 pr_crit("Unexpected close, not stopping watchdog!\n");
208 acq_keepalive();
209 }
210 clear_bit(0, &acq_is_open);
211 expect_close = 0;
212 return 0;
213}
214
215
216
217
218
219static const struct file_operations acq_fops = {
220 .owner = THIS_MODULE,
221 .llseek = no_llseek,
222 .write = acq_write,
223 .unlocked_ioctl = acq_ioctl,
224 .compat_ioctl = compat_ptr_ioctl,
225 .open = acq_open,
226 .release = acq_close,
227};
228
229static struct miscdevice acq_miscdev = {
230 .minor = WATCHDOG_MINOR,
231 .name = "watchdog",
232 .fops = &acq_fops,
233};
234
235
236
237
238
239static int __init acq_probe(struct platform_device *dev)
240{
241 int ret;
242
243 if (wdt_stop != wdt_start) {
244 if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
245 pr_err("I/O address 0x%04x already in use\n", wdt_stop);
246 ret = -EIO;
247 goto out;
248 }
249 }
250
251 if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
252 pr_err("I/O address 0x%04x already in use\n", wdt_start);
253 ret = -EIO;
254 goto unreg_stop;
255 }
256 ret = misc_register(&acq_miscdev);
257 if (ret != 0) {
258 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
259 WATCHDOG_MINOR, ret);
260 goto unreg_regions;
261 }
262 pr_info("initialized. (nowayout=%d)\n", nowayout);
263
264 return 0;
265unreg_regions:
266 release_region(wdt_start, 1);
267unreg_stop:
268 if (wdt_stop != wdt_start)
269 release_region(wdt_stop, 1);
270out:
271 return ret;
272}
273
274static int acq_remove(struct platform_device *dev)
275{
276 misc_deregister(&acq_miscdev);
277 release_region(wdt_start, 1);
278 if (wdt_stop != wdt_start)
279 release_region(wdt_stop, 1);
280
281 return 0;
282}
283
284static void acq_shutdown(struct platform_device *dev)
285{
286
287 acq_stop();
288}
289
290static struct platform_driver acquirewdt_driver = {
291 .remove = acq_remove,
292 .shutdown = acq_shutdown,
293 .driver = {
294 .name = DRV_NAME,
295 },
296};
297
298static int __init acq_init(void)
299{
300 int err;
301
302 pr_info("WDT driver for Acquire single board computer initialising\n");
303
304 acq_platform_device = platform_device_register_simple(DRV_NAME,
305 -1, NULL, 0);
306 if (IS_ERR(acq_platform_device))
307 return PTR_ERR(acq_platform_device);
308
309 err = platform_driver_probe(&acquirewdt_driver, acq_probe);
310 if (err)
311 goto unreg_platform_device;
312 return 0;
313
314unreg_platform_device:
315 platform_device_unregister(acq_platform_device);
316 return err;
317}
318
319static void __exit acq_exit(void)
320{
321 platform_device_unregister(acq_platform_device);
322 platform_driver_unregister(&acquirewdt_driver);
323 pr_info("Watchdog Module Unloaded\n");
324}
325
326module_init(acq_init);
327module_exit(acq_exit);
328
329MODULE_AUTHOR("David Woodhouse");
330MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
331MODULE_LICENSE("GPL");
332