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#include <linux/module.h>
35#include <linux/types.h>
36#include <linux/miscdevice.h>
37#include <linux/watchdog.h>
38#include <linux/ioport.h>
39#include <linux/fs.h>
40#include <linux/init.h>
41#include <linux/spinlock.h>
42#include <linux/moduleparam.h>
43#include <linux/platform_device.h>
44
45#include <asm/io.h>
46#include <asm/uaccess.h>
47#include <asm/system.h>
48
49static struct platform_device *ibwdt_platform_device;
50static unsigned long ibwdt_is_open;
51static DEFINE_SPINLOCK(ibwdt_lock);
52static char expect_close;
53
54
55#define DRV_NAME "ib700wdt"
56#define PFX DRV_NAME ": "
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94static int wd_times[] = {
95 30,
96 28,
97 26,
98 24,
99 22,
100 20,
101 18,
102 16,
103 14,
104 12,
105 10,
106 8,
107 6,
108 4,
109 2,
110 0,
111};
112
113#define WDT_STOP 0x441
114#define WDT_START 0x443
115
116
117#define WD_TIMO 0
118
119static int wd_margin = WD_TIMO;
120
121static int nowayout = WATCHDOG_NOWAYOUT;
122module_param(nowayout, int, 0);
123MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
124
125
126
127
128
129
130static void
131ibwdt_ping(void)
132{
133 spin_lock(&ibwdt_lock);
134
135
136 outb_p(wd_margin, WDT_START);
137
138 spin_unlock(&ibwdt_lock);
139}
140
141static void
142ibwdt_disable(void)
143{
144 spin_lock(&ibwdt_lock);
145 outb_p(0, WDT_STOP);
146 spin_unlock(&ibwdt_lock);
147}
148
149static int
150ibwdt_set_heartbeat(int t)
151{
152 int i;
153
154 if ((t < 0) || (t > 30))
155 return -EINVAL;
156
157 for (i = 0x0F; i > -1; i--)
158 if (wd_times[i] > t)
159 break;
160 wd_margin = i;
161 return 0;
162}
163
164
165
166
167
168static ssize_t
169ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
170{
171 if (count) {
172 if (!nowayout) {
173 size_t i;
174
175
176 expect_close = 0;
177
178 for (i = 0; i != count; i++) {
179 char c;
180 if (get_user(c, buf + i))
181 return -EFAULT;
182 if (c == 'V')
183 expect_close = 42;
184 }
185 }
186 ibwdt_ping();
187 }
188 return count;
189}
190
191static int
192ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
193 unsigned long arg)
194{
195 int new_margin;
196 void __user *argp = (void __user *)arg;
197 int __user *p = argp;
198
199 static struct watchdog_info ident = {
200 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
201 .firmware_version = 1,
202 .identity = "IB700 WDT",
203 };
204
205 switch (cmd) {
206 case WDIOC_GETSUPPORT:
207 if (copy_to_user(argp, &ident, sizeof(ident)))
208 return -EFAULT;
209 break;
210
211 case WDIOC_GETSTATUS:
212 case WDIOC_GETBOOTSTATUS:
213 return put_user(0, p);
214
215 case WDIOC_KEEPALIVE:
216 ibwdt_ping();
217 break;
218
219 case WDIOC_SETTIMEOUT:
220 if (get_user(new_margin, p))
221 return -EFAULT;
222 if (ibwdt_set_heartbeat(new_margin))
223 return -EINVAL;
224 ibwdt_ping();
225
226
227 case WDIOC_GETTIMEOUT:
228 return put_user(wd_times[wd_margin], p);
229
230 case WDIOC_SETOPTIONS:
231 {
232 int options, retval = -EINVAL;
233
234 if (get_user(options, p))
235 return -EFAULT;
236
237 if (options & WDIOS_DISABLECARD) {
238 ibwdt_disable();
239 retval = 0;
240 }
241
242 if (options & WDIOS_ENABLECARD) {
243 ibwdt_ping();
244 retval = 0;
245 }
246
247 return retval;
248 }
249
250 default:
251 return -ENOTTY;
252 }
253 return 0;
254}
255
256static int
257ibwdt_open(struct inode *inode, struct file *file)
258{
259 if (test_and_set_bit(0, &ibwdt_is_open)) {
260 return -EBUSY;
261 }
262 if (nowayout)
263 __module_get(THIS_MODULE);
264
265
266 ibwdt_ping();
267 return nonseekable_open(inode, file);
268}
269
270static int
271ibwdt_close(struct inode *inode, struct file *file)
272{
273 if (expect_close == 42) {
274 ibwdt_disable();
275 } else {
276 printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n");
277 ibwdt_ping();
278 }
279 clear_bit(0, &ibwdt_is_open);
280 expect_close = 0;
281 return 0;
282}
283
284
285
286
287
288static const struct file_operations ibwdt_fops = {
289 .owner = THIS_MODULE,
290 .llseek = no_llseek,
291 .write = ibwdt_write,
292 .ioctl = ibwdt_ioctl,
293 .open = ibwdt_open,
294 .release = ibwdt_close,
295};
296
297static struct miscdevice ibwdt_miscdev = {
298 .minor = WATCHDOG_MINOR,
299 .name = "watchdog",
300 .fops = &ibwdt_fops,
301};
302
303
304
305
306
307static int __devinit ibwdt_probe(struct platform_device *dev)
308{
309 int res;
310
311#if WDT_START != WDT_STOP
312 if (!request_region(WDT_STOP, 1, "IB700 WDT")) {
313 printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP);
314 res = -EIO;
315 goto out_nostopreg;
316 }
317#endif
318
319 if (!request_region(WDT_START, 1, "IB700 WDT")) {
320 printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START);
321 res = -EIO;
322 goto out_nostartreg;
323 }
324
325 res = misc_register(&ibwdt_miscdev);
326 if (res) {
327 printk (KERN_ERR PFX "failed to register misc device\n");
328 goto out_nomisc;
329 }
330 return 0;
331
332out_nomisc:
333 release_region(WDT_START, 1);
334out_nostartreg:
335#if WDT_START != WDT_STOP
336 release_region(WDT_STOP, 1);
337#endif
338out_nostopreg:
339 return res;
340}
341
342static int __devexit ibwdt_remove(struct platform_device *dev)
343{
344 misc_deregister(&ibwdt_miscdev);
345 release_region(WDT_START,1);
346#if WDT_START != WDT_STOP
347 release_region(WDT_STOP,1);
348#endif
349 return 0;
350}
351
352static void ibwdt_shutdown(struct platform_device *dev)
353{
354
355 ibwdt_disable();
356}
357
358static struct platform_driver ibwdt_driver = {
359 .probe = ibwdt_probe,
360 .remove = __devexit_p(ibwdt_remove),
361 .shutdown = ibwdt_shutdown,
362 .driver = {
363 .owner = THIS_MODULE,
364 .name = DRV_NAME,
365 },
366};
367
368static int __init ibwdt_init(void)
369{
370 int err;
371
372 printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n");
373
374 err = platform_driver_register(&ibwdt_driver);
375 if (err)
376 return err;
377
378 ibwdt_platform_device = platform_device_register_simple(DRV_NAME, -1, NULL, 0);
379 if (IS_ERR(ibwdt_platform_device)) {
380 err = PTR_ERR(ibwdt_platform_device);
381 goto unreg_platform_driver;
382 }
383
384 return 0;
385
386unreg_platform_driver:
387 platform_driver_unregister(&ibwdt_driver);
388 return err;
389}
390
391static void __exit ibwdt_exit(void)
392{
393 platform_device_unregister(ibwdt_platform_device);
394 platform_driver_unregister(&ibwdt_driver);
395 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
396}
397
398module_init(ibwdt_init);
399module_exit(ibwdt_exit);
400
401MODULE_AUTHOR("Charles Howes <chowes@vsol.net>");
402MODULE_DESCRIPTION("IB700 SBC watchdog driver");
403MODULE_LICENSE("GPL");
404MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
405
406
407