Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * PC Watchdog Driver | |
3 | * by Ken Hollis (khollis@bitgate.com) | |
4 | * | |
5 | * Permission granted from Simon Machell (73244.1270@compuserve.com) | |
6 | * Written for the Linux Kernel, and GPLed by Ken Hollis | |
7 | * | |
8 | * 960107 Added request_region routines, modulized the whole thing. | |
9 | * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added | |
10 | * WD_TIMEOUT define. | |
11 | * 960216 Added eof marker on the file, and changed verbose messages. | |
12 | * 960716 Made functional and cosmetic changes to the source for | |
13 | * inclusion in Linux 2.0.x kernels, thanks to Alan Cox. | |
14 | * 960717 Removed read/seek routines, replaced with ioctl. Also, added | |
15 | * check_region command due to Alan's suggestion. | |
16 | * 960821 Made changes to compile in newer 2.0.x kernels. Added | |
17 | * "cold reboot sense" entry. | |
18 | * 960825 Made a few changes to code, deleted some defines and made | |
19 | * typedefs to replace them. Made heartbeat reset only available | |
20 | * via ioctl, and removed the write routine. | |
21 | * 960828 Added new items for PC Watchdog Rev.C card. | |
22 | * 960829 Changed around all of the IOCTLs, added new features, | |
23 | * added watchdog disable/re-enable routines. Added firmware | |
24 | * version reporting. Added read routine for temperature. | |
25 | * Removed some extra defines, added an autodetect Revision | |
26 | * routine. | |
27 | * 961006 Revised some documentation, fixed some cosmetic bugs. Made | |
28 | * drivers to panic the system if it's overheating at bootup. | |
29 | * 961118 Changed some verbiage on some of the output, tidied up | |
30 | * code bits, and added compatibility to 2.1.x. | |
31 | * 970912 Enabled board on open and disable on close. | |
32 | * 971107 Took account of recent VFS changes (broke read). | |
33 | * 971210 Disable board on initialisation in case board already ticking. | |
34 | * 971222 Changed open/close for temperature handling | |
35 | * Michael Meskes <meskes@debian.org>. | |
36 | * 980112 Used minor numbers from include/linux/miscdevice.h | |
37 | * 990403 Clear reset status after reading control status register in | |
38 | * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>] | |
39 | * 990605 Made changes to code to support Firmware 1.22a, added | |
40 | * fairly useless proc entry. | |
41 | * 990610 removed said useless proc code for the merge <alan> | |
42 | * 000403 Removed last traces of proc code. <davej> | |
43 | * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com> | |
44 | * Added timeout module option to override default | |
45 | */ | |
46 | ||
47 | /* | |
48 | * A bells and whistles driver is available from http://www.pcwd.de/ | |
49 | * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/ | |
50 | */ | |
51 | ||
fd41fa61 WVS |
52 | #include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */ |
53 | #include <linux/module.h> /* For module specific items */ | |
54 | #include <linux/moduleparam.h> /* For new moduleparam's */ | |
55 | #include <linux/types.h> /* For standard types (like size_t) */ | |
56 | #include <linux/errno.h> /* For the -ENODEV/... values */ | |
57 | #include <linux/kernel.h> /* For printk/panic/... */ | |
58 | #include <linux/delay.h> /* For mdelay function */ | |
59 | #include <linux/timer.h> /* For timer related operations */ | |
60 | #include <linux/jiffies.h> /* For jiffies stuff */ | |
61 | #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */ | |
62 | #include <linux/watchdog.h> /* For the watchdog specific items */ | |
63 | #include <linux/notifier.h> /* For notifier support */ | |
64 | #include <linux/reboot.h> /* For reboot_notifier stuff */ | |
65 | #include <linux/init.h> /* For __init/__exit/... */ | |
66 | #include <linux/fs.h> /* For file operations */ | |
67 | #include <linux/ioport.h> /* For io-port access */ | |
68 | #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */ | |
4e57b681 | 69 | #include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */ |
fd41fa61 | 70 | #include <linux/slab.h> /* For kmalloc */ |
1da177e4 | 71 | |
fd41fa61 WVS |
72 | #include <asm/uaccess.h> /* For copy_to_user/put_user/... */ |
73 | #include <asm/io.h> /* For inb/outb/... */ | |
74 | ||
75 | /* Module and version information */ | |
a2be8786 | 76 | #define WD_VER "1.16 (03/01/2006)" |
1da177e4 LT |
77 | #define PFX "pcwd: " |
78 | ||
79 | /* | |
80 | * It should be noted that PCWD_REVISION_B was removed because A and B | |
81 | * are essentially the same types of card, with the exception that B | |
82 | * has temperature reporting. Since I didn't receive a Rev.B card, | |
83 | * the Rev.B card is not supported. (It's a good thing too, as they | |
84 | * are no longer in production.) | |
85 | */ | |
86 | #define PCWD_REVISION_A 1 | |
87 | #define PCWD_REVISION_C 2 | |
88 | ||
89 | /* | |
8f0235dc WVS |
90 | * These are the defines that describe the control status bits for the |
91 | * PCI-PC Watchdog card. | |
92 | */ | |
93 | /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */ | |
1da177e4 LT |
94 | #define WD_WDRST 0x01 /* Previously reset state */ |
95 | #define WD_T110 0x02 /* Temperature overheat sense */ | |
96 | #define WD_HRTBT 0x04 /* Heartbeat sense */ | |
97 | #define WD_RLY2 0x08 /* External relay triggered */ | |
98 | #define WD_SRLY2 0x80 /* Software external relay triggered */ | |
8f0235dc | 99 | /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */ |
1da177e4 LT |
100 | #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */ |
101 | #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */ | |
102 | #define WD_REVC_TTRP 0x04 /* Temperature Trip status */ | |
8f0235dc WVS |
103 | /* Port 2 : Control Status #2 */ |
104 | #define WD_WDIS 0x10 /* Watchdog Disabled */ | |
105 | #define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */ | |
106 | #define WD_SSEL 0x40 /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */ | |
107 | #define WD_WCMD 0x80 /* Watchdog Command Mode */ | |
1da177e4 LT |
108 | |
109 | /* max. time we give an ISA watchdog card to process a command */ | |
110 | /* 500ms for each 4 bit response (according to spec.) */ | |
111 | #define ISA_COMMAND_TIMEOUT 1000 | |
112 | ||
113 | /* Watchdog's internal commands */ | |
fd41fa61 WVS |
114 | #define CMD_ISA_IDLE 0x00 |
115 | #define CMD_ISA_VERSION_INTEGER 0x01 | |
116 | #define CMD_ISA_VERSION_TENTH 0x02 | |
117 | #define CMD_ISA_VERSION_HUNDRETH 0x03 | |
118 | #define CMD_ISA_VERSION_MINOR 0x04 | |
119 | #define CMD_ISA_SWITCH_SETTINGS 0x05 | |
120 | #define CMD_ISA_DELAY_TIME_2SECS 0x0A | |
121 | #define CMD_ISA_DELAY_TIME_4SECS 0x0B | |
122 | #define CMD_ISA_DELAY_TIME_8SECS 0x0C | |
1da177e4 LT |
123 | |
124 | /* | |
125 | * We are using an kernel timer to do the pinging of the watchdog | |
126 | * every ~500ms. We try to set the internal heartbeat of the | |
127 | * watchdog to 2 ms. | |
128 | */ | |
129 | ||
130 | #define WDT_INTERVAL (HZ/2+1) | |
131 | ||
132 | /* We can only use 1 card due to the /dev/watchdog restriction */ | |
133 | static int cards_found; | |
134 | ||
135 | /* internal variables */ | |
136 | static atomic_t open_allowed = ATOMIC_INIT(1); | |
137 | static char expect_close; | |
1da177e4 | 138 | static int temp_panic; |
a2be8786 WVS |
139 | static struct { /* this is private data for each ISA-PC watchdog card */ |
140 | int revision; /* The card's revision */ | |
141 | int supports_temp; /* Wether or not the card has a temperature device */ | |
142 | int command_mode; /* Wether or not the card is in command mode */ | |
143 | int boot_status; /* The card's boot status */ | |
144 | int io_addr; /* The cards I/O address */ | |
145 | spinlock_t io_lock; /* the lock for io operations */ | |
146 | struct timer_list timer; /* The timer that pings the watchdog */ | |
147 | unsigned long next_heartbeat; /* the next_heartbeat for the timer */ | |
148 | } pcwd_private; | |
1da177e4 LT |
149 | |
150 | /* module parameters */ | |
151 | #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */ | |
152 | static int heartbeat = WATCHDOG_HEARTBEAT; | |
153 | module_param(heartbeat, int, 0); | |
154 | MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")"); | |
155 | ||
4bfdf378 | 156 | static int nowayout = WATCHDOG_NOWAYOUT; |
1da177e4 LT |
157 | module_param(nowayout, int, 0); |
158 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); | |
159 | ||
160 | /* | |
161 | * Internal functions | |
162 | */ | |
163 | ||
164 | static int send_isa_command(int cmd) | |
165 | { | |
166 | int i; | |
167 | int control_status; | |
168 | int port0, last_port0; /* Double read for stabilising */ | |
169 | ||
170 | /* The WCMD bit must be 1 and the command is only 4 bits in size */ | |
8f0235dc | 171 | control_status = (cmd & 0x0F) | WD_WCMD; |
a2be8786 | 172 | outb_p(control_status, pcwd_private.io_addr + 2); |
1da177e4 LT |
173 | udelay(ISA_COMMAND_TIMEOUT); |
174 | ||
a2be8786 | 175 | port0 = inb_p(pcwd_private.io_addr); |
1da177e4 LT |
176 | for (i = 0; i < 25; ++i) { |
177 | last_port0 = port0; | |
a2be8786 | 178 | port0 = inb_p(pcwd_private.io_addr); |
1da177e4 LT |
179 | |
180 | if (port0 == last_port0) | |
181 | break; /* Data is stable */ | |
182 | ||
183 | udelay (250); | |
184 | } | |
185 | ||
186 | return port0; | |
187 | } | |
188 | ||
189 | static int set_command_mode(void) | |
190 | { | |
191 | int i, found=0, count=0; | |
192 | ||
193 | /* Set the card into command mode */ | |
a2be8786 | 194 | spin_lock(&pcwd_private.io_lock); |
1da177e4 LT |
195 | while ((!found) && (count < 3)) { |
196 | i = send_isa_command(CMD_ISA_IDLE); | |
197 | ||
198 | if (i == 0x00) | |
199 | found = 1; | |
200 | else if (i == 0xF3) { | |
201 | /* Card does not like what we've done to it */ | |
a2be8786 | 202 | outb_p(0x00, pcwd_private.io_addr + 2); |
1da177e4 | 203 | udelay(1200); /* Spec says wait 1ms */ |
a2be8786 | 204 | outb_p(0x00, pcwd_private.io_addr + 2); |
1da177e4 LT |
205 | udelay(ISA_COMMAND_TIMEOUT); |
206 | } | |
207 | count++; | |
208 | } | |
a2be8786 WVS |
209 | spin_unlock(&pcwd_private.io_lock); |
210 | pcwd_private.command_mode = found; | |
1da177e4 LT |
211 | |
212 | return(found); | |
213 | } | |
214 | ||
215 | static void unset_command_mode(void) | |
216 | { | |
217 | /* Set the card into normal mode */ | |
a2be8786 WVS |
218 | spin_lock(&pcwd_private.io_lock); |
219 | outb_p(0x00, pcwd_private.io_addr + 2); | |
1da177e4 | 220 | udelay(ISA_COMMAND_TIMEOUT); |
a2be8786 | 221 | spin_unlock(&pcwd_private.io_lock); |
1da177e4 | 222 | |
a2be8786 | 223 | pcwd_private.command_mode = 0; |
1da177e4 LT |
224 | } |
225 | ||
226 | static void pcwd_timer_ping(unsigned long data) | |
227 | { | |
228 | int wdrst_stat; | |
229 | ||
230 | /* If we got a heartbeat pulse within the WDT_INTERVAL | |
231 | * we agree to ping the WDT */ | |
a2be8786 | 232 | if(time_before(jiffies, pcwd_private.next_heartbeat)) { |
1da177e4 | 233 | /* Ping the watchdog */ |
a2be8786 WVS |
234 | spin_lock(&pcwd_private.io_lock); |
235 | if (pcwd_private.revision == PCWD_REVISION_A) { | |
1da177e4 | 236 | /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */ |
a2be8786 | 237 | wdrst_stat = inb_p(pcwd_private.io_addr); |
1da177e4 LT |
238 | wdrst_stat &= 0x0F; |
239 | wdrst_stat |= WD_WDRST; | |
240 | ||
a2be8786 | 241 | outb_p(wdrst_stat, pcwd_private.io_addr + 1); |
1da177e4 LT |
242 | } else { |
243 | /* Re-trigger watchdog by writing to port 0 */ | |
a2be8786 | 244 | outb_p(0x00, pcwd_private.io_addr); |
1da177e4 LT |
245 | } |
246 | ||
247 | /* Re-set the timer interval */ | |
a2be8786 | 248 | mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL); |
1da177e4 | 249 | |
a2be8786 | 250 | spin_unlock(&pcwd_private.io_lock); |
1da177e4 LT |
251 | } else { |
252 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); | |
253 | } | |
254 | } | |
255 | ||
256 | static int pcwd_start(void) | |
257 | { | |
258 | int stat_reg; | |
259 | ||
a2be8786 | 260 | pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ); |
1da177e4 LT |
261 | |
262 | /* Start the timer */ | |
a2be8786 | 263 | mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL); |
1da177e4 LT |
264 | |
265 | /* Enable the port */ | |
a2be8786 WVS |
266 | if (pcwd_private.revision == PCWD_REVISION_C) { |
267 | spin_lock(&pcwd_private.io_lock); | |
268 | outb_p(0x00, pcwd_private.io_addr + 3); | |
1da177e4 | 269 | udelay(ISA_COMMAND_TIMEOUT); |
a2be8786 WVS |
270 | stat_reg = inb_p(pcwd_private.io_addr + 2); |
271 | spin_unlock(&pcwd_private.io_lock); | |
8f0235dc | 272 | if (stat_reg & WD_WDIS) { |
1da177e4 LT |
273 | printk(KERN_INFO PFX "Could not start watchdog\n"); |
274 | return -EIO; | |
275 | } | |
276 | } | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static int pcwd_stop(void) | |
281 | { | |
282 | int stat_reg; | |
283 | ||
284 | /* Stop the timer */ | |
a2be8786 | 285 | del_timer(&pcwd_private.timer); |
1da177e4 LT |
286 | |
287 | /* Disable the board */ | |
a2be8786 WVS |
288 | if (pcwd_private.revision == PCWD_REVISION_C) { |
289 | spin_lock(&pcwd_private.io_lock); | |
290 | outb_p(0xA5, pcwd_private.io_addr + 3); | |
1da177e4 | 291 | udelay(ISA_COMMAND_TIMEOUT); |
a2be8786 | 292 | outb_p(0xA5, pcwd_private.io_addr + 3); |
1da177e4 | 293 | udelay(ISA_COMMAND_TIMEOUT); |
a2be8786 WVS |
294 | stat_reg = inb_p(pcwd_private.io_addr + 2); |
295 | spin_unlock(&pcwd_private.io_lock); | |
8f0235dc | 296 | if ((stat_reg & WD_WDIS) == 0) { |
1da177e4 LT |
297 | printk(KERN_INFO PFX "Could not stop watchdog\n"); |
298 | return -EIO; | |
299 | } | |
300 | } | |
301 | return 0; | |
302 | } | |
303 | ||
304 | static int pcwd_keepalive(void) | |
305 | { | |
306 | /* user land ping */ | |
a2be8786 | 307 | pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ); |
1da177e4 LT |
308 | return 0; |
309 | } | |
310 | ||
311 | static int pcwd_set_heartbeat(int t) | |
312 | { | |
313 | if ((t < 2) || (t > 7200)) /* arbitrary upper limit */ | |
314 | return -EINVAL; | |
315 | ||
316 | heartbeat = t; | |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int pcwd_get_status(int *status) | |
321 | { | |
322 | int card_status; | |
323 | ||
324 | *status=0; | |
a2be8786 WVS |
325 | spin_lock(&pcwd_private.io_lock); |
326 | if (pcwd_private.revision == PCWD_REVISION_A) | |
1da177e4 LT |
327 | /* Rev A cards return status information from |
328 | * the base register, which is used for the | |
329 | * temperature in other cards. */ | |
a2be8786 | 330 | card_status = inb(pcwd_private.io_addr); |
1da177e4 LT |
331 | else { |
332 | /* Rev C cards return card status in the base | |
333 | * address + 1 register. And use different bits | |
334 | * to indicate a card initiated reset, and an | |
335 | * over-temperature condition. And the reboot | |
336 | * status can be reset. */ | |
a2be8786 | 337 | card_status = inb(pcwd_private.io_addr + 1); |
1da177e4 | 338 | } |
a2be8786 | 339 | spin_unlock(&pcwd_private.io_lock); |
1da177e4 | 340 | |
a2be8786 | 341 | if (pcwd_private.revision == PCWD_REVISION_A) { |
1da177e4 LT |
342 | if (card_status & WD_WDRST) |
343 | *status |= WDIOF_CARDRESET; | |
344 | ||
345 | if (card_status & WD_T110) { | |
346 | *status |= WDIOF_OVERHEAT; | |
347 | if (temp_panic) { | |
348 | printk (KERN_INFO PFX "Temperature overheat trip!\n"); | |
68acc05d | 349 | kernel_power_off(); |
1da177e4 LT |
350 | } |
351 | } | |
352 | } else { | |
353 | if (card_status & WD_REVC_WTRP) | |
354 | *status |= WDIOF_CARDRESET; | |
355 | ||
356 | if (card_status & WD_REVC_TTRP) { | |
357 | *status |= WDIOF_OVERHEAT; | |
358 | if (temp_panic) { | |
359 | printk (KERN_INFO PFX "Temperature overheat trip!\n"); | |
68acc05d | 360 | kernel_power_off(); |
1da177e4 LT |
361 | } |
362 | } | |
363 | } | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | static int pcwd_clear_status(void) | |
369 | { | |
a2be8786 WVS |
370 | if (pcwd_private.revision == PCWD_REVISION_C) { |
371 | spin_lock(&pcwd_private.io_lock); | |
372 | outb_p(0x00, pcwd_private.io_addr + 1); /* clear reset status */ | |
373 | spin_unlock(&pcwd_private.io_lock); | |
1da177e4 LT |
374 | } |
375 | return 0; | |
376 | } | |
377 | ||
378 | static int pcwd_get_temperature(int *temperature) | |
379 | { | |
380 | /* check that port 0 gives temperature info and no command results */ | |
a2be8786 | 381 | if (pcwd_private.command_mode) |
1da177e4 LT |
382 | return -1; |
383 | ||
384 | *temperature = 0; | |
a2be8786 | 385 | if (!pcwd_private.supports_temp) |
1da177e4 LT |
386 | return -ENODEV; |
387 | ||
388 | /* | |
389 | * Convert celsius to fahrenheit, since this was | |
390 | * the decided 'standard' for this return value. | |
391 | */ | |
a2be8786 WVS |
392 | spin_lock(&pcwd_private.io_lock); |
393 | *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32; | |
394 | spin_unlock(&pcwd_private.io_lock); | |
1da177e4 LT |
395 | |
396 | return 0; | |
397 | } | |
398 | ||
399 | /* | |
400 | * /dev/watchdog handling | |
401 | */ | |
402 | ||
403 | static int pcwd_ioctl(struct inode *inode, struct file *file, | |
404 | unsigned int cmd, unsigned long arg) | |
405 | { | |
406 | int rv; | |
407 | int status; | |
408 | int temperature; | |
409 | int new_heartbeat; | |
410 | int __user *argp = (int __user *)arg; | |
411 | static struct watchdog_info ident = { | |
412 | .options = WDIOF_OVERHEAT | | |
413 | WDIOF_CARDRESET | | |
414 | WDIOF_KEEPALIVEPING | | |
415 | WDIOF_SETTIMEOUT | | |
416 | WDIOF_MAGICCLOSE, | |
417 | .firmware_version = 1, | |
418 | .identity = "PCWD", | |
419 | }; | |
420 | ||
421 | switch(cmd) { | |
422 | default: | |
423 | return -ENOIOCTLCMD; | |
424 | ||
425 | case WDIOC_GETSUPPORT: | |
426 | if(copy_to_user(argp, &ident, sizeof(ident))) | |
427 | return -EFAULT; | |
428 | return 0; | |
429 | ||
430 | case WDIOC_GETSTATUS: | |
431 | pcwd_get_status(&status); | |
432 | return put_user(status, argp); | |
433 | ||
434 | case WDIOC_GETBOOTSTATUS: | |
a2be8786 | 435 | return put_user(pcwd_private.boot_status, argp); |
1da177e4 LT |
436 | |
437 | case WDIOC_GETTEMP: | |
438 | if (pcwd_get_temperature(&temperature)) | |
439 | return -EFAULT; | |
440 | ||
441 | return put_user(temperature, argp); | |
442 | ||
443 | case WDIOC_SETOPTIONS: | |
a2be8786 | 444 | if (pcwd_private.revision == PCWD_REVISION_C) |
1da177e4 LT |
445 | { |
446 | if(copy_from_user(&rv, argp, sizeof(int))) | |
447 | return -EFAULT; | |
448 | ||
449 | if (rv & WDIOS_DISABLECARD) | |
450 | { | |
451 | return pcwd_stop(); | |
452 | } | |
453 | ||
454 | if (rv & WDIOS_ENABLECARD) | |
455 | { | |
456 | return pcwd_start(); | |
457 | } | |
458 | ||
459 | if (rv & WDIOS_TEMPPANIC) | |
460 | { | |
461 | temp_panic = 1; | |
462 | } | |
463 | } | |
464 | return -EINVAL; | |
465 | ||
466 | case WDIOC_KEEPALIVE: | |
467 | pcwd_keepalive(); | |
468 | return 0; | |
469 | ||
470 | case WDIOC_SETTIMEOUT: | |
471 | if (get_user(new_heartbeat, argp)) | |
472 | return -EFAULT; | |
473 | ||
474 | if (pcwd_set_heartbeat(new_heartbeat)) | |
475 | return -EINVAL; | |
476 | ||
477 | pcwd_keepalive(); | |
478 | /* Fall */ | |
479 | ||
480 | case WDIOC_GETTIMEOUT: | |
481 | return put_user(heartbeat, argp); | |
482 | } | |
483 | ||
484 | return 0; | |
485 | } | |
486 | ||
487 | static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len, | |
488 | loff_t *ppos) | |
489 | { | |
490 | if (len) { | |
491 | if (!nowayout) { | |
492 | size_t i; | |
493 | ||
494 | /* In case it was set long ago */ | |
495 | expect_close = 0; | |
496 | ||
497 | for (i = 0; i != len; i++) { | |
498 | char c; | |
499 | ||
500 | if (get_user(c, buf + i)) | |
501 | return -EFAULT; | |
502 | if (c == 'V') | |
503 | expect_close = 42; | |
504 | } | |
505 | } | |
506 | pcwd_keepalive(); | |
507 | } | |
508 | return len; | |
509 | } | |
510 | ||
511 | static int pcwd_open(struct inode *inode, struct file *file) | |
512 | { | |
513 | if (!atomic_dec_and_test(&open_allowed) ) { | |
514 | atomic_inc( &open_allowed ); | |
515 | return -EBUSY; | |
516 | } | |
517 | ||
518 | if (nowayout) | |
519 | __module_get(THIS_MODULE); | |
520 | ||
521 | /* Activate */ | |
522 | pcwd_start(); | |
523 | pcwd_keepalive(); | |
524 | return nonseekable_open(inode, file); | |
525 | } | |
526 | ||
527 | static int pcwd_close(struct inode *inode, struct file *file) | |
528 | { | |
529 | if (expect_close == 42) { | |
530 | pcwd_stop(); | |
531 | } else { | |
532 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); | |
533 | pcwd_keepalive(); | |
534 | } | |
535 | expect_close = 0; | |
536 | atomic_inc( &open_allowed ); | |
537 | return 0; | |
538 | } | |
539 | ||
540 | /* | |
541 | * /dev/temperature handling | |
542 | */ | |
543 | ||
544 | static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count, | |
545 | loff_t *ppos) | |
546 | { | |
547 | int temperature; | |
548 | ||
549 | if (pcwd_get_temperature(&temperature)) | |
550 | return -EFAULT; | |
551 | ||
552 | if (copy_to_user(buf, &temperature, 1)) | |
553 | return -EFAULT; | |
554 | ||
555 | return 1; | |
556 | } | |
557 | ||
558 | static int pcwd_temp_open(struct inode *inode, struct file *file) | |
559 | { | |
a2be8786 | 560 | if (!pcwd_private.supports_temp) |
1da177e4 LT |
561 | return -ENODEV; |
562 | ||
563 | return nonseekable_open(inode, file); | |
564 | } | |
565 | ||
566 | static int pcwd_temp_close(struct inode *inode, struct file *file) | |
567 | { | |
568 | return 0; | |
569 | } | |
570 | ||
571 | /* | |
572 | * Notify system | |
573 | */ | |
574 | ||
575 | static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused) | |
576 | { | |
577 | if (code==SYS_DOWN || code==SYS_HALT) { | |
578 | /* Turn the WDT off */ | |
579 | pcwd_stop(); | |
580 | } | |
581 | ||
582 | return NOTIFY_DONE; | |
583 | } | |
584 | ||
585 | /* | |
586 | * Kernel Interfaces | |
587 | */ | |
588 | ||
589 | static struct file_operations pcwd_fops = { | |
590 | .owner = THIS_MODULE, | |
591 | .llseek = no_llseek, | |
592 | .write = pcwd_write, | |
593 | .ioctl = pcwd_ioctl, | |
594 | .open = pcwd_open, | |
595 | .release = pcwd_close, | |
596 | }; | |
597 | ||
598 | static struct miscdevice pcwd_miscdev = { | |
599 | .minor = WATCHDOG_MINOR, | |
600 | .name = "watchdog", | |
601 | .fops = &pcwd_fops, | |
602 | }; | |
603 | ||
604 | static struct file_operations pcwd_temp_fops = { | |
605 | .owner = THIS_MODULE, | |
606 | .llseek = no_llseek, | |
607 | .read = pcwd_temp_read, | |
608 | .open = pcwd_temp_open, | |
609 | .release = pcwd_temp_close, | |
610 | }; | |
611 | ||
612 | static struct miscdevice temp_miscdev = { | |
613 | .minor = TEMP_MINOR, | |
614 | .name = "temperature", | |
615 | .fops = &pcwd_temp_fops, | |
616 | }; | |
617 | ||
618 | static struct notifier_block pcwd_notifier = { | |
619 | .notifier_call = pcwd_notify_sys, | |
620 | }; | |
621 | ||
622 | /* | |
623 | * Init & exit routines | |
624 | */ | |
625 | ||
626 | static inline void get_support(void) | |
627 | { | |
a2be8786 WVS |
628 | if (inb(pcwd_private.io_addr) != 0xF0) |
629 | pcwd_private.supports_temp = 1; | |
1da177e4 LT |
630 | } |
631 | ||
632 | static inline int get_revision(void) | |
633 | { | |
634 | int r = PCWD_REVISION_C; | |
635 | ||
a2be8786 | 636 | spin_lock(&pcwd_private.io_lock); |
1da177e4 LT |
637 | /* REV A cards use only 2 io ports; test |
638 | * presumes a floating bus reads as 0xff. */ | |
a2be8786 WVS |
639 | if ((inb(pcwd_private.io_addr + 2) == 0xFF) || |
640 | (inb(pcwd_private.io_addr + 3) == 0xFF)) | |
1da177e4 | 641 | r=PCWD_REVISION_A; |
a2be8786 | 642 | spin_unlock(&pcwd_private.io_lock); |
1da177e4 LT |
643 | |
644 | return r; | |
645 | } | |
646 | ||
647 | static inline char *get_firmware(void) | |
648 | { | |
649 | int one, ten, hund, minor; | |
650 | char *ret; | |
651 | ||
652 | ret = kmalloc(6, GFP_KERNEL); | |
653 | if(ret == NULL) | |
654 | return NULL; | |
655 | ||
656 | if (set_command_mode()) { | |
657 | one = send_isa_command(CMD_ISA_VERSION_INTEGER); | |
658 | ten = send_isa_command(CMD_ISA_VERSION_TENTH); | |
659 | hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH); | |
660 | minor = send_isa_command(CMD_ISA_VERSION_MINOR); | |
661 | sprintf(ret, "%c.%c%c%c", one, ten, hund, minor); | |
662 | } | |
663 | else | |
664 | sprintf(ret, "ERROR"); | |
665 | ||
666 | unset_command_mode(); | |
667 | return(ret); | |
668 | } | |
669 | ||
670 | static inline int get_option_switches(void) | |
671 | { | |
672 | int rv=0; | |
673 | ||
674 | if (set_command_mode()) { | |
675 | /* Get switch settings */ | |
676 | rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS); | |
677 | } | |
678 | ||
679 | unset_command_mode(); | |
680 | return(rv); | |
681 | } | |
682 | ||
683 | static int __devinit pcwatchdog_init(int base_addr) | |
684 | { | |
685 | int ret; | |
686 | char *firmware; | |
687 | int option_switches; | |
688 | ||
689 | cards_found++; | |
690 | if (cards_found == 1) | |
691 | printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER); | |
692 | ||
693 | if (cards_found > 1) { | |
694 | printk(KERN_ERR PFX "This driver only supports 1 device\n"); | |
695 | return -ENODEV; | |
696 | } | |
697 | ||
698 | if (base_addr == 0x0000) { | |
699 | printk(KERN_ERR PFX "No I/O-Address for card detected\n"); | |
700 | return -ENODEV; | |
701 | } | |
a2be8786 | 702 | pcwd_private.io_addr = base_addr; |
1da177e4 LT |
703 | |
704 | /* Check card's revision */ | |
a2be8786 | 705 | pcwd_private.revision = get_revision(); |
1da177e4 | 706 | |
a2be8786 | 707 | if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) { |
1da177e4 | 708 | printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", |
a2be8786 WVS |
709 | pcwd_private.io_addr); |
710 | pcwd_private.io_addr = 0x0000; | |
1da177e4 LT |
711 | return -EIO; |
712 | } | |
713 | ||
714 | /* Initial variables */ | |
a2be8786 | 715 | pcwd_private.supports_temp = 0; |
1da177e4 | 716 | temp_panic = 0; |
a2be8786 | 717 | pcwd_private.boot_status = 0x0000; |
1da177e4 LT |
718 | |
719 | /* get the boot_status */ | |
a2be8786 | 720 | pcwd_get_status(&pcwd_private.boot_status); |
1da177e4 LT |
721 | |
722 | /* clear the "card caused reboot" flag */ | |
723 | pcwd_clear_status(); | |
724 | ||
a2be8786 WVS |
725 | init_timer(&pcwd_private.timer); |
726 | pcwd_private.timer.function = pcwd_timer_ping; | |
727 | pcwd_private.timer.data = 0; | |
1da177e4 LT |
728 | |
729 | /* Disable the board */ | |
730 | pcwd_stop(); | |
731 | ||
732 | /* Check whether or not the card supports the temperature device */ | |
733 | get_support(); | |
734 | ||
735 | /* Get some extra info from the hardware (in command/debug/diag mode) */ | |
a2be8786 WVS |
736 | if (pcwd_private.revision == PCWD_REVISION_A) |
737 | printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr); | |
738 | else if (pcwd_private.revision == PCWD_REVISION_C) { | |
1da177e4 LT |
739 | firmware = get_firmware(); |
740 | printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n", | |
a2be8786 | 741 | pcwd_private.io_addr, firmware); |
1da177e4 LT |
742 | kfree(firmware); |
743 | option_switches = get_option_switches(); | |
744 | printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n", | |
745 | option_switches, | |
746 | ((option_switches & 0x10) ? "ON" : "OFF"), | |
747 | ((option_switches & 0x08) ? "ON" : "OFF")); | |
748 | ||
749 | /* Reprogram internal heartbeat to 2 seconds */ | |
750 | if (set_command_mode()) { | |
751 | send_isa_command(CMD_ISA_DELAY_TIME_2SECS); | |
752 | unset_command_mode(); | |
753 | } | |
754 | } else { | |
755 | /* Should NEVER happen, unless get_revision() fails. */ | |
756 | printk(KERN_INFO PFX "Unable to get revision\n"); | |
a2be8786 WVS |
757 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
758 | pcwd_private.io_addr = 0x0000; | |
1da177e4 LT |
759 | return -1; |
760 | } | |
761 | ||
a2be8786 | 762 | if (pcwd_private.supports_temp) |
1da177e4 LT |
763 | printk(KERN_INFO PFX "Temperature Option Detected\n"); |
764 | ||
a2be8786 | 765 | if (pcwd_private.boot_status & WDIOF_CARDRESET) |
1da177e4 LT |
766 | printk(KERN_INFO PFX "Previous reboot was caused by the card\n"); |
767 | ||
a2be8786 | 768 | if (pcwd_private.boot_status & WDIOF_OVERHEAT) { |
1da177e4 LT |
769 | printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n"); |
770 | printk(KERN_EMERG PFX "CPU Overheat\n"); | |
771 | } | |
772 | ||
a2be8786 | 773 | if (pcwd_private.boot_status == 0) |
1da177e4 LT |
774 | printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n"); |
775 | ||
776 | /* Check that the heartbeat value is within it's range ; if not reset to the default */ | |
fd41fa61 WVS |
777 | if (pcwd_set_heartbeat(heartbeat)) { |
778 | pcwd_set_heartbeat(WATCHDOG_HEARTBEAT); | |
779 | printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n", | |
780 | WATCHDOG_HEARTBEAT); | |
1da177e4 LT |
781 | } |
782 | ||
783 | ret = register_reboot_notifier(&pcwd_notifier); | |
784 | if (ret) { | |
785 | printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", | |
786 | ret); | |
a2be8786 WVS |
787 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
788 | pcwd_private.io_addr = 0x0000; | |
1da177e4 LT |
789 | return ret; |
790 | } | |
791 | ||
a2be8786 | 792 | if (pcwd_private.supports_temp) { |
1da177e4 LT |
793 | ret = misc_register(&temp_miscdev); |
794 | if (ret) { | |
795 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", | |
796 | TEMP_MINOR, ret); | |
797 | unregister_reboot_notifier(&pcwd_notifier); | |
a2be8786 WVS |
798 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
799 | pcwd_private.io_addr = 0x0000; | |
1da177e4 LT |
800 | return ret; |
801 | } | |
802 | } | |
803 | ||
804 | ret = misc_register(&pcwd_miscdev); | |
805 | if (ret) { | |
806 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", | |
807 | WATCHDOG_MINOR, ret); | |
a2be8786 | 808 | if (pcwd_private.supports_temp) |
1da177e4 LT |
809 | misc_deregister(&temp_miscdev); |
810 | unregister_reboot_notifier(&pcwd_notifier); | |
a2be8786 WVS |
811 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
812 | pcwd_private.io_addr = 0x0000; | |
1da177e4 LT |
813 | return ret; |
814 | } | |
815 | ||
816 | printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n", | |
817 | heartbeat, nowayout); | |
818 | ||
819 | return 0; | |
820 | } | |
821 | ||
822 | static void __devexit pcwatchdog_exit(void) | |
823 | { | |
824 | /* Disable the board */ | |
825 | if (!nowayout) | |
826 | pcwd_stop(); | |
827 | ||
828 | /* Deregister */ | |
829 | misc_deregister(&pcwd_miscdev); | |
a2be8786 | 830 | if (pcwd_private.supports_temp) |
1da177e4 LT |
831 | misc_deregister(&temp_miscdev); |
832 | unregister_reboot_notifier(&pcwd_notifier); | |
a2be8786 WVS |
833 | release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4); |
834 | pcwd_private.io_addr = 0x0000; | |
f1c3a056 | 835 | cards_found--; |
1da177e4 LT |
836 | } |
837 | ||
838 | /* | |
839 | * The ISA cards have a heartbeat bit in one of the registers, which | |
840 | * register is card dependent. The heartbeat bit is monitored, and if | |
841 | * found, is considered proof that a Berkshire card has been found. | |
842 | * The initial rate is once per second at board start up, then twice | |
843 | * per second for normal operation. | |
844 | */ | |
845 | static int __init pcwd_checkcard(int base_addr) | |
846 | { | |
847 | int port0, last_port0; /* Reg 0, in case it's REV A */ | |
848 | int port1, last_port1; /* Register 1 for REV C cards */ | |
849 | int i; | |
850 | int retval; | |
851 | ||
852 | if (!request_region (base_addr, 4, "PCWD")) { | |
853 | printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr); | |
854 | return 0; | |
855 | } | |
856 | ||
857 | retval = 0; | |
858 | ||
859 | port0 = inb_p(base_addr); /* For REV A boards */ | |
860 | port1 = inb_p(base_addr + 1); /* For REV C boards */ | |
861 | if (port0 != 0xff || port1 != 0xff) { | |
862 | /* Not an 'ff' from a floating bus, so must be a card! */ | |
863 | for (i = 0; i < 4; ++i) { | |
864 | ||
865 | msleep(500); | |
866 | ||
867 | last_port0 = port0; | |
868 | last_port1 = port1; | |
869 | ||
870 | port0 = inb_p(base_addr); | |
871 | port1 = inb_p(base_addr + 1); | |
872 | ||
873 | /* Has either hearbeat bit changed? */ | |
874 | if ((port0 ^ last_port0) & WD_HRTBT || | |
875 | (port1 ^ last_port1) & WD_REVC_HRBT) { | |
876 | retval = 1; | |
877 | break; | |
878 | } | |
879 | } | |
880 | } | |
881 | release_region (base_addr, 4); | |
882 | ||
883 | return retval; | |
884 | } | |
885 | ||
886 | /* | |
887 | * These are the auto-probe addresses available. | |
888 | * | |
889 | * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350. | |
890 | * Revision A has an address range of 2 addresses, while Revision C has 4. | |
891 | */ | |
892 | static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 }; | |
893 | ||
894 | static int __init pcwd_init_module(void) | |
895 | { | |
896 | int i, found = 0; | |
897 | ||
a2be8786 | 898 | spin_lock_init(&pcwd_private.io_lock); |
1da177e4 LT |
899 | |
900 | for (i = 0; pcwd_ioports[i] != 0; i++) { | |
901 | if (pcwd_checkcard(pcwd_ioports[i])) { | |
902 | if (!(pcwatchdog_init(pcwd_ioports[i]))) | |
903 | found++; | |
904 | } | |
905 | } | |
906 | ||
907 | if (!found) { | |
908 | printk (KERN_INFO PFX "No card detected, or port not available\n"); | |
909 | return -ENODEV; | |
910 | } | |
911 | ||
912 | return 0; | |
913 | } | |
914 | ||
915 | static void __exit pcwd_cleanup_module(void) | |
916 | { | |
a2be8786 | 917 | if (pcwd_private.io_addr) |
1da177e4 LT |
918 | pcwatchdog_exit(); |
919 | return; | |
920 | } | |
921 | ||
922 | module_init(pcwd_init_module); | |
923 | module_exit(pcwd_cleanup_module); | |
924 | ||
925 | MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>"); | |
926 | MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver"); | |
927 | MODULE_LICENSE("GPL"); | |
928 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | |
929 | MODULE_ALIAS_MISCDEV(TEMP_MINOR); |