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