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#include <linux/module.h>
40#include <linux/types.h>
41#include <linux/miscdevice.h>
42#include <linux/watchdog.h>
43#include <linux/ioport.h>
44#include <linux/delay.h>
45#include <linux/notifier.h>
46#include <linux/fs.h>
47#include <linux/reboot.h>
48#include <linux/init.h>
49#include <linux/spinlock.h>
50#include <linux/moduleparam.h>
51#include <linux/io.h>
52#include <linux/uaccess.h>
53
54#include <asm/system.h>
55
56static unsigned long sbc8360_is_open;
57static char expect_close;
58
59#define PFX "sbc8360: "
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127static int wd_times[64][2] = {
128 {0, 1},
129 {1, 1},
130 {2, 1},
131 {3, 1},
132 {4, 1},
133 {5, 1},
134 {6, 1},
135 {7, 1},
136 {8, 1},
137 {9, 1},
138 {0xA, 1},
139 {0xB, 1},
140 {0xC, 1},
141 {0xD, 1},
142 {0xE, 1},
143 {0xF, 1},
144 {0, 2},
145 {1, 2},
146 {2, 2},
147 {3, 2},
148 {4, 2},
149 {5, 2},
150 {6, 2},
151 {7, 2},
152 {8, 2},
153 {9, 2},
154 {0xA, 2},
155 {0xB, 2},
156 {0xC, 2},
157 {0xD, 2},
158 {0xE, 2},
159 {0xF, 2},
160 {0, 3},
161 {1, 3},
162 {2, 3},
163 {3, 3},
164 {4, 3},
165 {5, 3},
166 {6, 3},
167 {7, 3},
168 {8, 3},
169 {9, 3},
170 {0xA, 3},
171 {0xB, 3},
172 {0xC, 3},
173 {0xD, 3},
174 {0xE, 3},
175 {0xF, 3},
176 {0, 4},
177 {1, 4},
178 {2, 4},
179 {3, 4},
180 {4, 4},
181 {5, 4},
182 {6, 4},
183 {7, 4},
184 {8, 4},
185 {9, 4},
186 {0xA, 4},
187 {0xB, 4},
188 {0xC, 4},
189 {0xD, 4},
190 {0xE, 4},
191 {0xF, 4}
192};
193
194#define SBC8360_ENABLE 0x120
195#define SBC8360_BASETIME 0x121
196
197static int timeout = 27;
198static int wd_margin = 0xB;
199static int wd_multiplier = 2;
200static int nowayout = WATCHDOG_NOWAYOUT;
201
202module_param(timeout, int, 0);
203MODULE_PARM_DESC(timeout, "Index into timeout table (0-63) (default=27 (60s))");
204module_param(nowayout, int, 0);
205MODULE_PARM_DESC(nowayout,
206 "Watchdog cannot be stopped once started (default="
207 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
208
209
210
211
212
213
214static void sbc8360_activate(void)
215{
216
217 outb(0x0A, SBC8360_ENABLE);
218 msleep_interruptible(100);
219 outb(0x0B, SBC8360_ENABLE);
220 msleep_interruptible(100);
221
222 outb(wd_multiplier, SBC8360_ENABLE);
223 msleep_interruptible(100);
224
225}
226
227
228static void sbc8360_ping(void)
229{
230
231 outb(wd_margin, SBC8360_BASETIME);
232}
233
234
235static void sbc8360_stop(void)
236{
237
238 outb(0, SBC8360_ENABLE);
239}
240
241
242static ssize_t sbc8360_write(struct file *file, const char __user *buf,
243 size_t count, loff_t *ppos)
244{
245 if (count) {
246 if (!nowayout) {
247 size_t i;
248
249
250 expect_close = 0;
251
252 for (i = 0; i != count; i++) {
253 char c;
254 if (get_user(c, buf + i))
255 return -EFAULT;
256 if (c == 'V')
257 expect_close = 42;
258 }
259 }
260 sbc8360_ping();
261 }
262 return count;
263}
264
265static int sbc8360_open(struct inode *inode, struct file *file)
266{
267 if (test_and_set_bit(0, &sbc8360_is_open))
268 return -EBUSY;
269 if (nowayout)
270 __module_get(THIS_MODULE);
271
272
273 sbc8360_activate();
274 sbc8360_ping();
275 return nonseekable_open(inode, file);
276}
277
278static int sbc8360_close(struct inode *inode, struct file *file)
279{
280 if (expect_close == 42)
281 sbc8360_stop();
282 else
283 printk(KERN_CRIT PFX
284 "SBC8360 device closed unexpectedly. SBC8360 will not stop!\n");
285
286 clear_bit(0, &sbc8360_is_open);
287 expect_close = 0;
288 return 0;
289}
290
291
292
293
294
295static int sbc8360_notify_sys(struct notifier_block *this, unsigned long code,
296 void *unused)
297{
298 if (code == SYS_DOWN || code == SYS_HALT)
299 sbc8360_stop();
300
301 return NOTIFY_DONE;
302}
303
304
305
306
307
308static const struct file_operations sbc8360_fops = {
309 .owner = THIS_MODULE,
310 .llseek = no_llseek,
311 .write = sbc8360_write,
312 .open = sbc8360_open,
313 .release = sbc8360_close,
314};
315
316static struct miscdevice sbc8360_miscdev = {
317 .minor = WATCHDOG_MINOR,
318 .name = "watchdog",
319 .fops = &sbc8360_fops,
320};
321
322
323
324
325
326
327static struct notifier_block sbc8360_notifier = {
328 .notifier_call = sbc8360_notify_sys,
329};
330
331static int __init sbc8360_init(void)
332{
333 int res;
334 unsigned long int mseconds = 60000;
335
336 if (timeout < 0 || timeout > 63) {
337 printk(KERN_ERR PFX "Invalid timeout index (must be 0-63).\n");
338 res = -EINVAL;
339 goto out;
340 }
341
342 if (!request_region(SBC8360_ENABLE, 1, "SBC8360")) {
343 printk(KERN_ERR PFX "ENABLE method I/O %X is not available.\n",
344 SBC8360_ENABLE);
345 res = -EIO;
346 goto out;
347 }
348 if (!request_region(SBC8360_BASETIME, 1, "SBC8360")) {
349 printk(KERN_ERR PFX
350 "BASETIME method I/O %X is not available.\n",
351 SBC8360_BASETIME);
352 res = -EIO;
353 goto out_nobasetimereg;
354 }
355
356 res = register_reboot_notifier(&sbc8360_notifier);
357 if (res) {
358 printk(KERN_ERR PFX "Failed to register reboot notifier.\n");
359 goto out_noreboot;
360 }
361
362 res = misc_register(&sbc8360_miscdev);
363 if (res) {
364 printk(KERN_ERR PFX "failed to register misc device\n");
365 goto out_nomisc;
366 }
367
368 wd_margin = wd_times[timeout][0];
369 wd_multiplier = wd_times[timeout][1];
370
371 if (wd_multiplier == 1)
372 mseconds = (wd_margin + 1) * 500;
373 else if (wd_multiplier == 2)
374 mseconds = (wd_margin + 1) * 5000;
375 else if (wd_multiplier == 3)
376 mseconds = (wd_margin + 1) * 50000;
377 else if (wd_multiplier == 4)
378 mseconds = (wd_margin + 1) * 100000;
379
380
381 printk(KERN_INFO PFX "Timeout set at %ld ms.\n", mseconds);
382
383 return 0;
384
385out_nomisc:
386 unregister_reboot_notifier(&sbc8360_notifier);
387out_noreboot:
388 release_region(SBC8360_BASETIME, 1);
389out_nobasetimereg:
390 release_region(SBC8360_ENABLE, 1);
391out:
392 return res;
393}
394
395static void __exit sbc8360_exit(void)
396{
397 misc_deregister(&sbc8360_miscdev);
398 unregister_reboot_notifier(&sbc8360_notifier);
399 release_region(SBC8360_ENABLE, 1);
400 release_region(SBC8360_BASETIME, 1);
401}
402
403module_init(sbc8360_init);
404module_exit(sbc8360_exit);
405
406MODULE_AUTHOR("Ian E. Morgan <imorgan@webcon.ca>");
407MODULE_DESCRIPTION("SBC8360 watchdog driver");
408MODULE_LICENSE("GPL");
409MODULE_VERSION("1.01");
410MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
411
412
413