]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/watchdog/pcwd.c
[WATCHDOG] more coding style clean-up's
[net-next-2.6.git] / drivers / watchdog / pcwd.c
CommitLineData
1da177e4
LT
1/*
2 * PC Watchdog Driver
3 * by Ken Hollis (khollis@bitgate.com)
4 *
f9146f26 5 * Permission granted from Simon Machell (smachell@berkprod.com)
1da177e4
LT
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>
261dcc70
AC
43 * 011214 Added nowayout module option to override
44 * CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
1da177e4
LT
45 * Added timeout module option to override default
46 */
47
48/*
49 * A bells and whistles driver is available from http://www.pcwd.de/
261dcc70
AC
50 * More info available at http://www.berkprod.com/ or
51 * http://www.pcwatchdog.com/
1da177e4
LT
52 */
53
fd41fa61
WVS
54#include <linux/module.h> /* For module specific items */
55#include <linux/moduleparam.h> /* For new moduleparam's */
56#include <linux/types.h> /* For standard types (like size_t) */
57#include <linux/errno.h> /* For the -ENODEV/... values */
58#include <linux/kernel.h> /* For printk/panic/... */
59#include <linux/delay.h> /* For mdelay function */
60#include <linux/timer.h> /* For timer related operations */
61#include <linux/jiffies.h> /* For jiffies stuff */
62#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
63#include <linux/watchdog.h> /* For the watchdog specific items */
5aa15050 64#include <linux/reboot.h> /* For kernel_power_off() */
fd41fa61
WVS
65#include <linux/init.h> /* For __init/__exit/... */
66#include <linux/fs.h> /* For file operations */
5aa15050 67#include <linux/isa.h> /* For isa devices */
fd41fa61
WVS
68#include <linux/ioport.h> /* For io-port access */
69#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
261dcc70
AC
70#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
71#include <linux/io.h> /* For inb/outb/... */
fd41fa61
WVS
72
73/* Module and version information */
5aa15050
WVS
74#define WATCHDOG_VERSION "1.20"
75#define WATCHDOG_DATE "18 Feb 2007"
a7122f91
WVS
76#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
77#define WATCHDOG_NAME "pcwd"
78#define PFX WATCHDOG_NAME ": "
79#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
80#define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
1da177e4
LT
81
82/*
83 * It should be noted that PCWD_REVISION_B was removed because A and B
84 * are essentially the same types of card, with the exception that B
85 * has temperature reporting. Since I didn't receive a Rev.B card,
86 * the Rev.B card is not supported. (It's a good thing too, as they
87 * are no longer in production.)
88 */
89#define PCWD_REVISION_A 1
90#define PCWD_REVISION_C 2
91
5aa15050
WVS
92/*
93 * These are the auto-probe addresses available.
94 *
95 * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
96 * Revision A has an address range of 2 addresses, while Revision C has 4.
97 */
98#define PCWD_ISA_NR_CARDS 3
99static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
100
1da177e4 101/*
8f0235dc
WVS
102 * These are the defines that describe the control status bits for the
103 * PCI-PC Watchdog card.
104*/
105/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
4206f0c4
WVS
106#define WD_WDRST 0x01 /* Previously reset state */
107#define WD_T110 0x02 /* Temperature overheat sense */
108#define WD_HRTBT 0x04 /* Heartbeat sense */
109#define WD_RLY2 0x08 /* External relay triggered */
110#define WD_SRLY2 0x80 /* Software external relay triggered */
8f0235dc 111/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
4206f0c4
WVS
112#define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
113#define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
114#define WD_REVC_TTRP 0x04 /* Temperature Trip status */
261dcc70
AC
115#define WD_REVC_RL2A 0x08 /* Relay 2 activated by
116 on-board processor */
4206f0c4
WVS
117#define WD_REVC_RL1A 0x10 /* Relay 1 active */
118#define WD_REVC_R2DS 0x40 /* Relay 2 disable */
119#define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
8f0235dc
WVS
120/* Port 2 : Control Status #2 */
121#define WD_WDIS 0x10 /* Watchdog Disabled */
122#define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
261dcc70
AC
123#define WD_SSEL 0x40 /* Watchdog Switch Select
124 (1:SW1 <-> 0:SW2) */
8f0235dc 125#define WD_WCMD 0x80 /* Watchdog Command Mode */
1da177e4
LT
126
127/* max. time we give an ISA watchdog card to process a command */
128/* 500ms for each 4 bit response (according to spec.) */
129#define ISA_COMMAND_TIMEOUT 1000
130
131/* Watchdog's internal commands */
fd41fa61
WVS
132#define CMD_ISA_IDLE 0x00
133#define CMD_ISA_VERSION_INTEGER 0x01
134#define CMD_ISA_VERSION_TENTH 0x02
135#define CMD_ISA_VERSION_HUNDRETH 0x03
136#define CMD_ISA_VERSION_MINOR 0x04
137#define CMD_ISA_SWITCH_SETTINGS 0x05
369fa252
WVS
138#define CMD_ISA_RESET_PC 0x06
139#define CMD_ISA_ARM_0 0x07
140#define CMD_ISA_ARM_30 0x08
141#define CMD_ISA_ARM_60 0x09
fd41fa61
WVS
142#define CMD_ISA_DELAY_TIME_2SECS 0x0A
143#define CMD_ISA_DELAY_TIME_4SECS 0x0B
144#define CMD_ISA_DELAY_TIME_8SECS 0x0C
369fa252 145#define CMD_ISA_RESET_RELAYS 0x0D
1da177e4 146
f3dc0733 147/* Watchdog's Dip Switch heartbeat values */
7944d3a5 148static const int heartbeat_tbl[] = {
f3dc0733
WVS
149 20, /* OFF-OFF-OFF = 20 Sec */
150 40, /* OFF-OFF-ON = 40 Sec */
151 60, /* OFF-ON-OFF = 1 Min */
152 300, /* OFF-ON-ON = 5 Min */
153 600, /* ON-OFF-OFF = 10 Min */
154 1800, /* ON-OFF-ON = 30 Min */
155 3600, /* ON-ON-OFF = 1 Hour */
156 7200, /* ON-ON-ON = 2 hour */
157};
158
1da177e4
LT
159/*
160 * We are using an kernel timer to do the pinging of the watchdog
161 * every ~500ms. We try to set the internal heartbeat of the
162 * watchdog to 2 ms.
163 */
164
165#define WDT_INTERVAL (HZ/2+1)
166
167/* We can only use 1 card due to the /dev/watchdog restriction */
168static int cards_found;
169
170/* internal variables */
171static atomic_t open_allowed = ATOMIC_INIT(1);
172static char expect_close;
1da177e4 173static int temp_panic;
261dcc70
AC
174
175/* this is private data for each ISA-PC watchdog card */
176static struct {
2891b6ad 177 char fw_ver_str[6]; /* The cards firmware version */
a2be8786 178 int revision; /* The card's revision */
261dcc70
AC
179 int supports_temp; /* Whether or not the card has
180 a temperature device */
181 int command_mode; /* Whether or not the card is in
182 command mode */
a2be8786
WVS
183 int boot_status; /* The card's boot status */
184 int io_addr; /* The cards I/O address */
185 spinlock_t io_lock; /* the lock for io operations */
186 struct timer_list timer; /* The timer that pings the watchdog */
187 unsigned long next_heartbeat; /* the next_heartbeat for the timer */
188} pcwd_private;
1da177e4
LT
189
190/* module parameters */
c324ab42
WVS
191#define QUIET 0 /* Default */
192#define VERBOSE 1 /* Verbose */
193#define DEBUG 2 /* print fancy stuff too */
194static int debug = QUIET;
195module_param(debug, int, 0);
261dcc70
AC
196MODULE_PARM_DESC(debug,
197 "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
c324ab42 198
261dcc70
AC
199/* default heartbeat = delay-time from dip-switches */
200#define WATCHDOG_HEARTBEAT 0
1da177e4
LT
201static int heartbeat = WATCHDOG_HEARTBEAT;
202module_param(heartbeat, int, 0);
261dcc70 203MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
1da177e4 204
4bfdf378 205static int nowayout = WATCHDOG_NOWAYOUT;
1da177e4 206module_param(nowayout, int, 0);
261dcc70
AC
207MODULE_PARM_DESC(nowayout,
208 "Watchdog cannot be stopped once started (default="
209 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
1da177e4
LT
210
211/*
212 * Internal functions
213 */
214
215static int send_isa_command(int cmd)
216{
217 int i;
218 int control_status;
219 int port0, last_port0; /* Double read for stabilising */
220
c324ab42
WVS
221 if (debug >= DEBUG)
222 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
223 cmd);
224
1da177e4 225 /* The WCMD bit must be 1 and the command is only 4 bits in size */
8f0235dc 226 control_status = (cmd & 0x0F) | WD_WCMD;
a2be8786 227 outb_p(control_status, pcwd_private.io_addr + 2);
1da177e4
LT
228 udelay(ISA_COMMAND_TIMEOUT);
229
a2be8786 230 port0 = inb_p(pcwd_private.io_addr);
1da177e4
LT
231 for (i = 0; i < 25; ++i) {
232 last_port0 = port0;
a2be8786 233 port0 = inb_p(pcwd_private.io_addr);
1da177e4
LT
234
235 if (port0 == last_port0)
236 break; /* Data is stable */
237
261dcc70 238 udelay(250);
1da177e4
LT
239 }
240
c324ab42
WVS
241 if (debug >= DEBUG)
242 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
243 cmd, port0, last_port0);
244
1da177e4
LT
245 return port0;
246}
247
248static int set_command_mode(void)
249{
261dcc70 250 int i, found = 0, count = 0;
1da177e4
LT
251
252 /* Set the card into command mode */
a2be8786 253 spin_lock(&pcwd_private.io_lock);
1da177e4
LT
254 while ((!found) && (count < 3)) {
255 i = send_isa_command(CMD_ISA_IDLE);
256
257 if (i == 0x00)
258 found = 1;
259 else if (i == 0xF3) {
260 /* Card does not like what we've done to it */
a2be8786 261 outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4 262 udelay(1200); /* Spec says wait 1ms */
a2be8786 263 outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4
LT
264 udelay(ISA_COMMAND_TIMEOUT);
265 }
266 count++;
267 }
a2be8786
WVS
268 spin_unlock(&pcwd_private.io_lock);
269 pcwd_private.command_mode = found;
1da177e4 270
c324ab42
WVS
271 if (debug >= DEBUG)
272 printk(KERN_DEBUG PFX "command_mode=%d\n",
273 pcwd_private.command_mode);
274
7944d3a5 275 return found;
1da177e4
LT
276}
277
278static void unset_command_mode(void)
279{
280 /* Set the card into normal mode */
a2be8786
WVS
281 spin_lock(&pcwd_private.io_lock);
282 outb_p(0x00, pcwd_private.io_addr + 2);
1da177e4 283 udelay(ISA_COMMAND_TIMEOUT);
a2be8786 284 spin_unlock(&pcwd_private.io_lock);
1da177e4 285
a2be8786 286 pcwd_private.command_mode = 0;
c324ab42
WVS
287
288 if (debug >= DEBUG)
289 printk(KERN_DEBUG PFX "command_mode=%d\n",
290 pcwd_private.command_mode);
1da177e4
LT
291}
292
85875211
WVS
293static inline void pcwd_check_temperature_support(void)
294{
295 if (inb(pcwd_private.io_addr) != 0xF0)
296 pcwd_private.supports_temp = 1;
297}
298
2891b6ad 299static inline void pcwd_get_firmware(void)
af3b38d9
WVS
300{
301 int one, ten, hund, minor;
af3b38d9 302
6bbc20bc 303 strcpy(pcwd_private.fw_ver_str, "ERROR");
af3b38d9
WVS
304
305 if (set_command_mode()) {
306 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
307 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
308 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
309 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
261dcc70
AC
310 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
311 one, ten, hund, minor);
af3b38d9 312 }
af3b38d9 313 unset_command_mode();
2891b6ad
WVS
314
315 return;
af3b38d9
WVS
316}
317
318static inline int pcwd_get_option_switches(void)
319{
261dcc70 320 int option_switches = 0;
af3b38d9
WVS
321
322 if (set_command_mode()) {
323 /* Get switch settings */
324 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
325 }
326
327 unset_command_mode();
7944d3a5 328 return option_switches;
af3b38d9
WVS
329}
330
331static void pcwd_show_card_info(void)
332{
af3b38d9
WVS
333 int option_switches;
334
335 /* Get some extra info from the hardware (in command/debug/diag mode) */
336 if (pcwd_private.revision == PCWD_REVISION_A)
261dcc70
AC
337 printk(KERN_INFO PFX
338 "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
339 pcwd_private.io_addr);
af3b38d9 340 else if (pcwd_private.revision == PCWD_REVISION_C) {
2891b6ad 341 pcwd_get_firmware();
af3b38d9 342 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
2891b6ad 343 pcwd_private.io_addr, pcwd_private.fw_ver_str);
af3b38d9
WVS
344 option_switches = pcwd_get_option_switches();
345 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
346 option_switches,
347 ((option_switches & 0x10) ? "ON" : "OFF"),
348 ((option_switches & 0x08) ? "ON" : "OFF"));
349
350 /* Reprogram internal heartbeat to 2 seconds */
351 if (set_command_mode()) {
352 send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
353 unset_command_mode();
354 }
355 }
356
357 if (pcwd_private.supports_temp)
358 printk(KERN_INFO PFX "Temperature Option Detected\n");
359
360 if (pcwd_private.boot_status & WDIOF_CARDRESET)
361 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
362
363 if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
261dcc70
AC
364 printk(KERN_EMERG PFX
365 "Card senses a CPU Overheat. Panicking!\n");
366 printk(KERN_EMERG PFX
367 "CPU Overheat\n");
af3b38d9
WVS
368 }
369
370 if (pcwd_private.boot_status == 0)
261dcc70
AC
371 printk(KERN_INFO PFX
372 "No previous trip detected - Cold boot or reset\n");
af3b38d9
WVS
373}
374
1da177e4
LT
375static void pcwd_timer_ping(unsigned long data)
376{
377 int wdrst_stat;
378
379 /* If we got a heartbeat pulse within the WDT_INTERVAL
380 * we agree to ping the WDT */
261dcc70 381 if (time_before(jiffies, pcwd_private.next_heartbeat)) {
1da177e4 382 /* Ping the watchdog */
a2be8786
WVS
383 spin_lock(&pcwd_private.io_lock);
384 if (pcwd_private.revision == PCWD_REVISION_A) {
261dcc70
AC
385 /* Rev A cards are reset by setting the
386 WD_WDRST bit in register 1 */
a2be8786 387 wdrst_stat = inb_p(pcwd_private.io_addr);
1da177e4
LT
388 wdrst_stat &= 0x0F;
389 wdrst_stat |= WD_WDRST;
390
a2be8786 391 outb_p(wdrst_stat, pcwd_private.io_addr + 1);
1da177e4
LT
392 } else {
393 /* Re-trigger watchdog by writing to port 0 */
a2be8786 394 outb_p(0x00, pcwd_private.io_addr);
1da177e4
LT
395 }
396
397 /* Re-set the timer interval */
a2be8786 398 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
1da177e4 399
a2be8786 400 spin_unlock(&pcwd_private.io_lock);
1da177e4 401 } else {
261dcc70
AC
402 printk(KERN_WARNING PFX
403 "Heartbeat lost! Will not ping the watchdog\n");
1da177e4
LT
404 }
405}
406
407static int pcwd_start(void)
408{
409 int stat_reg;
410
a2be8786 411 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
1da177e4
LT
412
413 /* Start the timer */
a2be8786 414 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
1da177e4
LT
415
416 /* Enable the port */
a2be8786
WVS
417 if (pcwd_private.revision == PCWD_REVISION_C) {
418 spin_lock(&pcwd_private.io_lock);
419 outb_p(0x00, pcwd_private.io_addr + 3);
1da177e4 420 udelay(ISA_COMMAND_TIMEOUT);
a2be8786
WVS
421 stat_reg = inb_p(pcwd_private.io_addr + 2);
422 spin_unlock(&pcwd_private.io_lock);
8f0235dc 423 if (stat_reg & WD_WDIS) {
1da177e4
LT
424 printk(KERN_INFO PFX "Could not start watchdog\n");
425 return -EIO;
426 }
427 }
c324ab42
WVS
428
429 if (debug >= VERBOSE)
430 printk(KERN_DEBUG PFX "Watchdog started\n");
431
1da177e4
LT
432 return 0;
433}
434
435static int pcwd_stop(void)
436{
437 int stat_reg;
438
439 /* Stop the timer */
a2be8786 440 del_timer(&pcwd_private.timer);
1da177e4
LT
441
442 /* Disable the board */
a2be8786
WVS
443 if (pcwd_private.revision == PCWD_REVISION_C) {
444 spin_lock(&pcwd_private.io_lock);
445 outb_p(0xA5, pcwd_private.io_addr + 3);
1da177e4 446 udelay(ISA_COMMAND_TIMEOUT);
a2be8786 447 outb_p(0xA5, pcwd_private.io_addr + 3);
1da177e4 448 udelay(ISA_COMMAND_TIMEOUT);
a2be8786
WVS
449 stat_reg = inb_p(pcwd_private.io_addr + 2);
450 spin_unlock(&pcwd_private.io_lock);
8f0235dc 451 if ((stat_reg & WD_WDIS) == 0) {
1da177e4
LT
452 printk(KERN_INFO PFX "Could not stop watchdog\n");
453 return -EIO;
454 }
455 }
c324ab42
WVS
456
457 if (debug >= VERBOSE)
458 printk(KERN_DEBUG PFX "Watchdog stopped\n");
459
1da177e4
LT
460 return 0;
461}
462
463static int pcwd_keepalive(void)
464{
465 /* user land ping */
a2be8786 466 pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
c324ab42
WVS
467
468 if (debug >= DEBUG)
469 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
470
1da177e4
LT
471 return 0;
472}
473
474static int pcwd_set_heartbeat(int t)
475{
261dcc70 476 if (t < 2 || t > 7200) /* arbitrary upper limit */
1da177e4
LT
477 return -EINVAL;
478
479 heartbeat = t;
c324ab42
WVS
480
481 if (debug >= VERBOSE)
482 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
483 heartbeat);
484
1da177e4
LT
485 return 0;
486}
487
488static int pcwd_get_status(int *status)
489{
4206f0c4 490 int control_status;
1da177e4 491
261dcc70 492 *status = 0;
a2be8786
WVS
493 spin_lock(&pcwd_private.io_lock);
494 if (pcwd_private.revision == PCWD_REVISION_A)
1da177e4
LT
495 /* Rev A cards return status information from
496 * the base register, which is used for the
497 * temperature in other cards. */
4206f0c4 498 control_status = inb(pcwd_private.io_addr);
1da177e4
LT
499 else {
500 /* Rev C cards return card status in the base
501 * address + 1 register. And use different bits
502 * to indicate a card initiated reset, and an
503 * over-temperature condition. And the reboot
504 * status can be reset. */
4206f0c4 505 control_status = inb(pcwd_private.io_addr + 1);
1da177e4 506 }
a2be8786 507 spin_unlock(&pcwd_private.io_lock);
1da177e4 508
a2be8786 509 if (pcwd_private.revision == PCWD_REVISION_A) {
4206f0c4 510 if (control_status & WD_WDRST)
1da177e4
LT
511 *status |= WDIOF_CARDRESET;
512
4206f0c4 513 if (control_status & WD_T110) {
1da177e4
LT
514 *status |= WDIOF_OVERHEAT;
515 if (temp_panic) {
261dcc70
AC
516 printk(KERN_INFO PFX
517 "Temperature overheat trip!\n");
68acc05d 518 kernel_power_off();
1da177e4
LT
519 }
520 }
521 } else {
4206f0c4 522 if (control_status & WD_REVC_WTRP)
1da177e4
LT
523 *status |= WDIOF_CARDRESET;
524
4206f0c4 525 if (control_status & WD_REVC_TTRP) {
1da177e4
LT
526 *status |= WDIOF_OVERHEAT;
527 if (temp_panic) {
261dcc70
AC
528 printk(KERN_INFO PFX
529 "Temperature overheat trip!\n");
68acc05d 530 kernel_power_off();
1da177e4
LT
531 }
532 }
533 }
534
535 return 0;
536}
537
538static int pcwd_clear_status(void)
539{
4206f0c4
WVS
540 int control_status;
541
a2be8786
WVS
542 if (pcwd_private.revision == PCWD_REVISION_C) {
543 spin_lock(&pcwd_private.io_lock);
4206f0c4 544
c324ab42 545 if (debug >= VERBOSE)
261dcc70
AC
546 printk(KERN_INFO PFX
547 "clearing watchdog trip status\n");
c324ab42 548
4206f0c4
WVS
549 control_status = inb_p(pcwd_private.io_addr + 1);
550
c324ab42 551 if (debug >= DEBUG) {
261dcc70
AC
552 printk(KERN_DEBUG PFX "status was: 0x%02x\n",
553 control_status);
c324ab42
WVS
554 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
555 (control_status & WD_REVC_R2DS));
556 }
557
4206f0c4 558 /* clear reset status & Keep Relay 2 disable state as it is */
261dcc70
AC
559 outb_p((control_status & WD_REVC_R2DS),
560 pcwd_private.io_addr + 1);
4206f0c4 561
a2be8786 562 spin_unlock(&pcwd_private.io_lock);
1da177e4
LT
563 }
564 return 0;
565}
566
567static int pcwd_get_temperature(int *temperature)
568{
569 /* check that port 0 gives temperature info and no command results */
a2be8786 570 if (pcwd_private.command_mode)
1da177e4
LT
571 return -1;
572
573 *temperature = 0;
a2be8786 574 if (!pcwd_private.supports_temp)
1da177e4
LT
575 return -ENODEV;
576
577 /*
578 * Convert celsius to fahrenheit, since this was
579 * the decided 'standard' for this return value.
580 */
a2be8786
WVS
581 spin_lock(&pcwd_private.io_lock);
582 *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
583 spin_unlock(&pcwd_private.io_lock);
1da177e4 584
c324ab42
WVS
585 if (debug >= DEBUG) {
586 printk(KERN_DEBUG PFX "temperature is: %d F\n",
587 *temperature);
588 }
589
1da177e4
LT
590 return 0;
591}
592
593/*
594 * /dev/watchdog handling
595 */
596
261dcc70 597static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1da177e4
LT
598{
599 int rv;
600 int status;
601 int temperature;
602 int new_heartbeat;
603 int __user *argp = (int __user *)arg;
604 static struct watchdog_info ident = {
605 .options = WDIOF_OVERHEAT |
606 WDIOF_CARDRESET |
607 WDIOF_KEEPALIVEPING |
608 WDIOF_SETTIMEOUT |
609 WDIOF_MAGICCLOSE,
610 .firmware_version = 1,
611 .identity = "PCWD",
612 };
613
261dcc70 614 switch (cmd) {
1da177e4 615 case WDIOC_GETSUPPORT:
261dcc70 616 if (copy_to_user(argp, &ident, sizeof(ident)))
1da177e4
LT
617 return -EFAULT;
618 return 0;
619
620 case WDIOC_GETSTATUS:
621 pcwd_get_status(&status);
622 return put_user(status, argp);
623
624 case WDIOC_GETBOOTSTATUS:
a2be8786 625 return put_user(pcwd_private.boot_status, argp);
1da177e4
LT
626
627 case WDIOC_GETTEMP:
628 if (pcwd_get_temperature(&temperature))
629 return -EFAULT;
630
631 return put_user(temperature, argp);
632
633 case WDIOC_SETOPTIONS:
261dcc70
AC
634 if (pcwd_private.revision == PCWD_REVISION_C) {
635 if (get_user(rv, argp))
1da177e4
LT
636 return -EFAULT;
637
261dcc70
AC
638 if (rv & WDIOS_DISABLECARD) {
639 status = pcwd_stop();
640 if (status < 0)
641 return status;
1da177e4 642 }
261dcc70
AC
643 if (rv & WDIOS_ENABLECARD) {
644 status = pcwd_start();
645 if (status < 0)
646 return status;
1da177e4 647 }
1da177e4 648 if (rv & WDIOS_TEMPPANIC)
1da177e4 649 temp_panic = 1;
1da177e4
LT
650 }
651 return -EINVAL;
652
653 case WDIOC_KEEPALIVE:
654 pcwd_keepalive();
655 return 0;
656
657 case WDIOC_SETTIMEOUT:
658 if (get_user(new_heartbeat, argp))
659 return -EFAULT;
660
661 if (pcwd_set_heartbeat(new_heartbeat))
662 return -EINVAL;
663
664 pcwd_keepalive();
665 /* Fall */
666
667 case WDIOC_GETTIMEOUT:
668 return put_user(heartbeat, argp);
0c06090c
WVS
669
670 default:
671 return -ENOTTY;
1da177e4
LT
672 }
673
674 return 0;
675}
676
677static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
678 loff_t *ppos)
679{
680 if (len) {
681 if (!nowayout) {
682 size_t i;
683
684 /* In case it was set long ago */
685 expect_close = 0;
686
687 for (i = 0; i != len; i++) {
688 char c;
689
690 if (get_user(c, buf + i))
691 return -EFAULT;
692 if (c == 'V')
693 expect_close = 42;
694 }
695 }
696 pcwd_keepalive();
697 }
698 return len;
699}
700
701static int pcwd_open(struct inode *inode, struct file *file)
702{
261dcc70 703 if (test_and_set_bit(0, &open_allowed))
1da177e4 704 return -EBUSY;
1da177e4
LT
705 if (nowayout)
706 __module_get(THIS_MODULE);
1da177e4
LT
707 /* Activate */
708 pcwd_start();
709 pcwd_keepalive();
710 return nonseekable_open(inode, file);
711}
712
713static int pcwd_close(struct inode *inode, struct file *file)
714{
261dcc70 715 if (expect_close == 42)
1da177e4 716 pcwd_stop();
261dcc70
AC
717 else {
718 printk(KERN_CRIT PFX
719 "Unexpected close, not stopping watchdog!\n");
1da177e4
LT
720 pcwd_keepalive();
721 }
722 expect_close = 0;
261dcc70 723 clear_bit(0, &open_allowed);
1da177e4
LT
724 return 0;
725}
726
727/*
728 * /dev/temperature handling
729 */
730
731static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
732 loff_t *ppos)
733{
734 int temperature;
735
736 if (pcwd_get_temperature(&temperature))
737 return -EFAULT;
738
739 if (copy_to_user(buf, &temperature, 1))
740 return -EFAULT;
741
742 return 1;
743}
744
745static int pcwd_temp_open(struct inode *inode, struct file *file)
746{
a2be8786 747 if (!pcwd_private.supports_temp)
1da177e4
LT
748 return -ENODEV;
749
750 return nonseekable_open(inode, file);
751}
752
753static int pcwd_temp_close(struct inode *inode, struct file *file)
754{
755 return 0;
756}
757
1da177e4
LT
758/*
759 * Kernel Interfaces
760 */
761
62322d25 762static const struct file_operations pcwd_fops = {
1da177e4
LT
763 .owner = THIS_MODULE,
764 .llseek = no_llseek,
765 .write = pcwd_write,
261dcc70 766 .unlocked_ioctl = pcwd_ioctl,
1da177e4
LT
767 .open = pcwd_open,
768 .release = pcwd_close,
769};
770
771static struct miscdevice pcwd_miscdev = {
772 .minor = WATCHDOG_MINOR,
773 .name = "watchdog",
774 .fops = &pcwd_fops,
775};
776
62322d25 777static const struct file_operations pcwd_temp_fops = {
1da177e4
LT
778 .owner = THIS_MODULE,
779 .llseek = no_llseek,
780 .read = pcwd_temp_read,
781 .open = pcwd_temp_open,
782 .release = pcwd_temp_close,
783};
784
785static struct miscdevice temp_miscdev = {
786 .minor = TEMP_MINOR,
787 .name = "temperature",
788 .fops = &pcwd_temp_fops,
789};
790
1da177e4
LT
791/*
792 * Init & exit routines
793 */
794
1da177e4
LT
795static inline int get_revision(void)
796{
797 int r = PCWD_REVISION_C;
798
a2be8786 799 spin_lock(&pcwd_private.io_lock);
1da177e4
LT
800 /* REV A cards use only 2 io ports; test
801 * presumes a floating bus reads as 0xff. */
a2be8786
WVS
802 if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
803 (inb(pcwd_private.io_addr + 3) == 0xFF))
261dcc70 804 r = PCWD_REVISION_A;
a2be8786 805 spin_unlock(&pcwd_private.io_lock);
1da177e4
LT
806
807 return r;
808}
809
5aa15050
WVS
810/*
811 * The ISA cards have a heartbeat bit in one of the registers, which
812 * register is card dependent. The heartbeat bit is monitored, and if
813 * found, is considered proof that a Berkshire card has been found.
814 * The initial rate is once per second at board start up, then twice
815 * per second for normal operation.
816 */
817static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
818{
261dcc70 819 int base_addr = pcwd_ioports[id];
5aa15050
WVS
820 int port0, last_port0; /* Reg 0, in case it's REV A */
821 int port1, last_port1; /* Register 1 for REV C cards */
822 int i;
823 int retval;
824
825 if (debug >= DEBUG)
826 printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
827 id);
828
261dcc70 829 if (!request_region(base_addr, 4, "PCWD")) {
5aa15050
WVS
830 printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
831 return 0;
832 }
833
834 retval = 0;
835
836 port0 = inb_p(base_addr); /* For REV A boards */
837 port1 = inb_p(base_addr + 1); /* For REV C boards */
838 if (port0 != 0xff || port1 != 0xff) {
839 /* Not an 'ff' from a floating bus, so must be a card! */
840 for (i = 0; i < 4; ++i) {
841
842 msleep(500);
843
844 last_port0 = port0;
845 last_port1 = port1;
846
847 port0 = inb_p(base_addr);
848 port1 = inb_p(base_addr + 1);
849
850 /* Has either hearbeat bit changed? */
851 if ((port0 ^ last_port0) & WD_HRTBT ||
852 (port1 ^ last_port1) & WD_REVC_HRBT) {
853 retval = 1;
854 break;
855 }
856 }
857 }
261dcc70 858 release_region(base_addr, 4);
5aa15050
WVS
859
860 return retval;
861}
862
863static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
1da177e4
LT
864{
865 int ret;
1da177e4 866
5aa15050
WVS
867 if (debug >= DEBUG)
868 printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
869 id);
870
1da177e4
LT
871 cards_found++;
872 if (cards_found == 1)
261dcc70
AC
873 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n",
874 WD_VER);
1da177e4
LT
875
876 if (cards_found > 1) {
877 printk(KERN_ERR PFX "This driver only supports 1 device\n");
878 return -ENODEV;
879 }
880
5aa15050 881 if (pcwd_ioports[id] == 0x0000) {
1da177e4
LT
882 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
883 return -ENODEV;
884 }
5aa15050
WVS
885 pcwd_private.io_addr = pcwd_ioports[id];
886
887 spin_lock_init(&pcwd_private.io_lock);
1da177e4
LT
888
889 /* Check card's revision */
a2be8786 890 pcwd_private.revision = get_revision();
1da177e4 891
261dcc70
AC
892 if (!request_region(pcwd_private.io_addr,
893 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
1da177e4 894 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
a2be8786 895 pcwd_private.io_addr);
261dcc70 896 ret = -EIO;
5aa15050 897 goto error_request_region;
1da177e4
LT
898 }
899
900 /* Initial variables */
a2be8786 901 pcwd_private.supports_temp = 0;
1da177e4 902 temp_panic = 0;
a2be8786 903 pcwd_private.boot_status = 0x0000;
1da177e4
LT
904
905 /* get the boot_status */
a2be8786 906 pcwd_get_status(&pcwd_private.boot_status);
1da177e4
LT
907
908 /* clear the "card caused reboot" flag */
909 pcwd_clear_status();
910
82eb7c50 911 setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
1da177e4
LT
912
913 /* Disable the board */
914 pcwd_stop();
915
916 /* Check whether or not the card supports the temperature device */
85875211 917 pcwd_check_temperature_support();
1da177e4 918
af3b38d9
WVS
919 /* Show info about the card itself */
920 pcwd_show_card_info();
1da177e4 921
f3dc0733
WVS
922 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
923 if (heartbeat == 0)
924 heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
925
261dcc70
AC
926 /* Check that the heartbeat value is within it's range;
927 if not reset to the default */
fd41fa61
WVS
928 if (pcwd_set_heartbeat(heartbeat)) {
929 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
261dcc70
AC
930 printk(KERN_INFO PFX
931 "heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
932 WATCHDOG_HEARTBEAT);
1da177e4
LT
933 }
934
a2be8786 935 if (pcwd_private.supports_temp) {
1da177e4
LT
936 ret = misc_register(&temp_miscdev);
937 if (ret) {
261dcc70
AC
938 printk(KERN_ERR PFX
939 "cannot register miscdev on minor=%d (err=%d)\n",
940 TEMP_MINOR, ret);
5aa15050 941 goto error_misc_register_temp;
1da177e4
LT
942 }
943 }
944
945 ret = misc_register(&pcwd_miscdev);
946 if (ret) {
261dcc70
AC
947 printk(KERN_ERR PFX
948 "cannot register miscdev on minor=%d (err=%d)\n",
949 WATCHDOG_MINOR, ret);
5aa15050 950 goto error_misc_register_watchdog;
1da177e4
LT
951 }
952
953 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
954 heartbeat, nowayout);
955
956 return 0;
5aa15050
WVS
957
958error_misc_register_watchdog:
959 if (pcwd_private.supports_temp)
960 misc_deregister(&temp_miscdev);
961error_misc_register_temp:
261dcc70
AC
962 release_region(pcwd_private.io_addr,
963 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
5aa15050
WVS
964error_request_region:
965 pcwd_private.io_addr = 0x0000;
966 cards_found--;
967 return ret;
1da177e4
LT
968}
969
5aa15050 970static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
1da177e4 971{
5aa15050
WVS
972 if (debug >= DEBUG)
973 printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
974 id);
975
976 if (!pcwd_private.io_addr)
977 return 1;
978
1da177e4
LT
979 /* Disable the board */
980 if (!nowayout)
981 pcwd_stop();
982
983 /* Deregister */
984 misc_deregister(&pcwd_miscdev);
a2be8786 985 if (pcwd_private.supports_temp)
1da177e4 986 misc_deregister(&temp_miscdev);
261dcc70
AC
987 release_region(pcwd_private.io_addr,
988 (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
a2be8786 989 pcwd_private.io_addr = 0x0000;
f1c3a056 990 cards_found--;
5aa15050
WVS
991
992 return 0;
1da177e4
LT
993}
994
5aa15050 995static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
1da177e4 996{
5aa15050
WVS
997 if (debug >= DEBUG)
998 printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
999 id);
1da177e4 1000
5aa15050 1001 pcwd_stop();
1da177e4
LT
1002}
1003
5aa15050
WVS
1004static struct isa_driver pcwd_isa_driver = {
1005 .match = pcwd_isa_match,
1006 .probe = pcwd_isa_probe,
1007 .remove = __devexit_p(pcwd_isa_remove),
1008 .shutdown = pcwd_isa_shutdown,
1009 .driver = {
1010 .owner = THIS_MODULE,
1011 .name = WATCHDOG_NAME,
1012 },
1013};
1da177e4
LT
1014
1015static int __init pcwd_init_module(void)
1016{
5aa15050 1017 return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
1da177e4
LT
1018}
1019
1020static void __exit pcwd_cleanup_module(void)
1021{
5aa15050 1022 isa_unregister_driver(&pcwd_isa_driver);
369fa252 1023 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
1da177e4
LT
1024}
1025
1026module_init(pcwd_init_module);
1027module_exit(pcwd_cleanup_module);
1028
5aa15050 1029MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
1da177e4 1030MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
5aa15050 1031MODULE_VERSION(WATCHDOG_VERSION);
1da177e4
LT
1032MODULE_LICENSE("GPL");
1033MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
1034MODULE_ALIAS_MISCDEV(TEMP_MINOR);