1 /*
2 * 60xx Single Board Computer Watchdog Timer driver for Linux 2.2.x
3 *
4 * Based on acquirewdt.c by Alan Cox.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 * The author does NOT admit liability nor provide warranty for
12 * any of this software. This material is provided "AS-IS" in
13 * the hope that it may be useful for others.
14 *
15 * (c) Copyright 2000 Jakob Oestergaard <jakob@ostenfeld.dk>
16 *
17 * 12/4 - 2000 [Initial revision]
18 * 25/4 - 2000 Added /dev/watchdog support
19 *
20 *
21 * Theory of operation:
22 * A Watchdog Timer (WDT) is a hardware circuit that can
23 * reset the computer system in case of a software fault.
24 * You probably knew that already.
25 *
26 * Usually a userspace daemon will notify the kernel WDT driver
27 * via the /proc/watchdog special device file that userspace is
28 * still alive, at regular intervals. When such a notification
29 * occurs, the driver will usually tell the hardware watchdog
30 * that everything is in order, and that the watchdog should wait
31 * for yet another little while to reset the system.
32 * If userspace fails (RAM error, kernel bug, whatever), the
33 * notifications cease to occur, and the hardware watchdog will
34 * reset the system (causing a reboot) after the timeout occurs.
35 *
36 * This WDT driver is different from the other Linux WDT
37 * drivers in several ways:
38 * *) The driver will ping the watchdog by itself, because this
39 * particular WDT has a very short timeout (one second) and it
40 * would be insane to count on any userspace daemon always
41 * getting scheduled within that time frame.
42 * *) This driver expects the userspace daemon to send a specific
43 * character code ('V') to /dev/watchdog before closing the
44 * /dev/watchdog file. If the userspace daemon closes the file
45 * without sending this special character, the driver will assume
46 * that the daemon (and userspace in general) died, and will
47 * stop pinging the WDT without disabling it first. This will
48 * cause a reboot.
49 *
50 * Why `V' ? Well, `V' is the character in ASCII for the value 86,
51 * and we all know that 86 is _the_ most random number in the universe.
52 * Therefore it is the letter that has the slightest chance of occuring
53 * by chance, when the system becomes corrupted.
54 *
55 */
56
57 #include <linux/module.h>
58 #include <linux/version.h>
59 #include <linux/types.h>
60 #include <linux/errno.h>
61 #include <linux/kernel.h>
62 #include <linux/timer.h>
63 #include <linux/sched.h>
64 #include <linux/miscdevice.h>
65 #include <linux/watchdog.h>
66 #include <linux/malloc.h>
67 #include <linux/ioport.h>
68 #include <linux/fcntl.h>
69 #include <linux/smp_lock.h>
70 #include <asm/io.h>
71 #include <asm/uaccess.h>
72 #include <asm/system.h>
73 #include <linux/notifier.h>
74 #include <linux/reboot.h>
75 #include <linux/init.h>
76
77 #define OUR_NAME "sbc60xxwdt"
78
79 /*
80 * You must set these - The driver cannot probe for the settings
81 */
82
83 #define WDT_STOP 0x45
84 #define WDT_START 0x443
85
86 /*
87 * The 60xx board can use watchdog timeout values from one second
88 * to several minutes. The default is one second, so if we reset
89 * the watchdog every ~250ms we should be safe.
90 */
91
92 #define WDT_INTERVAL (HZ/4+1)
93
94 /*
95 * We must not require too good response from the userspace daemon.
96 * Here we require the userspace daemon to send us a heartbeat
97 * char to /dev/watchdog every 10 seconds.
98 * If the daemon pulses us every 5 seconds, we can still afford
99 * a 5 second scheduling delay on the (high priority) daemon. That
100 * should be sufficient for a box under any load.
101 */
102
103 #define WDT_HEARTBEAT (HZ * 10)
104
105 static void wdt_timer_ping(unsigned long);
106 static struct timer_list timer;
107 static unsigned long next_heartbeat;
108 static int wdt_is_open;
109 static int wdt_expect_close;
110
111 /*
112 * Whack the dog
113 */
114
115 static void wdt_timer_ping(unsigned long data)
116 {
117 /* If we got a heartbeat pulse within the WDT_US_INTERVAL
118 * we agree to ping the WDT
119 */
120 if(time_before(jiffies, next_heartbeat))
121 {
122 /* Ping the WDT by reading from WDT_START */
123 inb_p(WDT_START);
124 /* Re-set the timer interval */
125 timer.expires = jiffies + WDT_INTERVAL;
126 add_timer(&timer);
127 } else {
128 printk(OUR_NAME ": Heartbeat lost! Will not ping the watchdog\n");
129 }
130 }
131
132 /*
133 * Utility routines
134 */
135
136 static void wdt_startup(void)
137 {
138 next_heartbeat = jiffies + WDT_HEARTBEAT;
139
140 /* Start the timer */
141 timer.expires = jiffies + WDT_INTERVAL;
142 add_timer(&timer);
143 printk(OUR_NAME ": Watchdog timer is now enabled.\n");
144 }
145
146 static void wdt_turnoff(void)
147 {
148 /* Stop the timer */
149 del_timer(&timer);
150 inb_p(WDT_STOP);
151 printk(OUR_NAME ": Watchdog timer is now disabled...\n");
152 }
153
154
155 /*
156 * /dev/watchdog handling
157 */
158
159 static ssize_t fop_write(struct file * file, const char * buf, size_t count, loff_t * ppos)
160 {
161 /* We can't seek */
162 if(ppos != &file->f_pos)
163 return -ESPIPE;
164
165 /* See if we got the magic character */
166 if(count)
167 {
168 size_t ofs;
169
170 /* note: just in case someone wrote the magic character
171 * five months ago... */
172 wdt_expect_close = 0;
173
174 /* now scan */
175 for(ofs = 0; ofs != count; ofs++)
176 if(buf[ofs] == 'V')
177 wdt_expect_close = 1;
178
179 /* Well, anyhow someone wrote to us, we should return that favour */
180 next_heartbeat = jiffies + WDT_HEARTBEAT;
181 }
182 return 0;
183 }
184
185 static ssize_t fop_read(struct file * file, char * buf, size_t count, loff_t * ppos)
186 {
187 /* No can do */
188 return -EINVAL;
189 }
190
191 static int fop_open(struct inode * inode, struct file * file)
192 {
193 switch(MINOR(inode->i_rdev))
194 {
195 case WATCHDOG_MINOR:
196 /* Just in case we're already talking to someone... */
197 if(wdt_is_open)
198 return -EBUSY;
199 /* Good, fire up the show */
200 wdt_is_open = 1;
201 wdt_startup();
202 return 0;
203
204 default:
205 return -ENODEV;
206 }
207 }
208
209 static int fop_close(struct inode * inode, struct file * file)
210 {
211 lock_kernel();
212 if(MINOR(inode->i_rdev) == WATCHDOG_MINOR)
213 {
214 if(wdt_expect_close)
215 wdt_turnoff();
216 else {
217 del_timer(&timer);
218 printk(OUR_NAME ": device file closed unexpectedly. Will not stop the WDT!\n");
219 }
220 }
221 wdt_is_open = 0;
222 unlock_kernel();
223 return 0;
224 }
225
226 static long long fop_llseek(struct file *file, long long offset, int origin)
227 {
228 return -ESPIPE;
229 }
230
231 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
232 unsigned long arg)
233 {
234 static struct watchdog_info ident=
235 {
236 0,
237 1,
238 "SB60xx"
239 };
240
241 switch(cmd)
242 {
243 default:
244 return -ENOIOCTLCMD;
245 case WDIOC_GETSUPPORT:
246 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
247 case WDIOC_KEEPALIVE:
248 next_heartbeat = jiffies + WDT_HEARTBEAT;
249 return 0;
250 }
251 }
252
253 static struct file_operations wdt_fops = {
254 owner: THIS_MODULE,
255 llseek: fop_llseek,
256 read: fop_read,
257 write: fop_write,
258 open: fop_open,
259 release: fop_close,
260 ioctl: fop_ioctl
261 };
262
263 static struct miscdevice wdt_miscdev = {
264 WATCHDOG_MINOR,
265 "watchdog",
266 &wdt_fops
267 };
268
269 /*
270 * Notifier for system down
271 */
272
273 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
274 void *unused)
275 {
276 if(code==SYS_DOWN || code==SYS_HALT)
277 wdt_turnoff();
278 return NOTIFY_DONE;
279 }
280
281 /*
282 * The WDT needs to learn about soft shutdowns in order to
283 * turn the timebomb registers off.
284 */
285
286 static struct notifier_block wdt_notifier=
287 {
288 wdt_notify_sys,
289 0,
290 0
291 };
292
293 static void __exit sbc60xxwdt_unload(void)
294 {
295 wdt_turnoff();
296
297 /* Deregister */
298 misc_deregister(&wdt_miscdev);
299
300 unregister_reboot_notifier(&wdt_notifier);
301 release_region(WDT_START,1);
302 release_region(WDT_STOP,1);
303 }
304
305 static int __init sbc60xxwdt_init(void)
306 {
307 int rc = -EBUSY;
308
309 if (!request_region(WDT_STOP, 1, "SBC 60XX WDT"))
310 goto err_out;
311 if (!request_region(WDT_START, 1, "SBC 60XX WDT"))
312 goto err_out_region1;
313
314 init_timer(&timer);
315 timer.function = wdt_timer_ping;
316 timer.data = 0;
317
318 rc = misc_register(&wdt_miscdev);
319 if (rc)
320 goto err_out_region2;
321
322 rc = register_reboot_notifier(&wdt_notifier);
323 if (rc)
324 goto err_out_miscdev;
325
326 printk(KERN_INFO OUR_NAME ": WDT driver for 60XX single board computer initialised.\n");
327
328 return 0;
329
330 err_out_miscdev:
331 misc_deregister(&wdt_miscdev);
332 err_out_region2:
333 release_region(WDT_START,1);
334 err_out_region1:
335 release_region(WDT_STOP,1);
336 err_out:
337 return rc;
338 }
339
340 module_init(sbc60xxwdt_init);
341 module_exit(sbc60xxwdt_unload);
342
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.