[WATCHDOG] Mixcom Watchdog - checkcard
[safe/jmp/linux-2.6] / drivers / char / watchdog / pcwd.c
index 1112ec8..7b41434 100644 (file)
@@ -2,7 +2,7 @@
  * PC Watchdog Driver
  * by Ken Hollis (khollis@bitgate.com)
  *
- * Permission granted from Simon Machell (73244.1270@compuserve.com)
+ * Permission granted from Simon Machell (smachell@berkprod.com)
  * Written for the Linux Kernel, and GPLed by Ken Hollis
  *
  * 960107      Added request_region routines, modulized the whole thing.
@@ -49,7 +49,6 @@
  *     More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
  */
 
-#include <linux/config.h>      /* For CONFIG_WATCHDOG_NOWAYOUT/... */
 #include <linux/module.h>      /* For module specific items */
 #include <linux/moduleparam.h> /* For new moduleparam's */
 #include <linux/types.h>       /* For standard types (like size_t) */
 #include <linux/jiffies.h>     /* For jiffies stuff */
 #include <linux/miscdevice.h>  /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
 #include <linux/watchdog.h>    /* For the watchdog specific items */
-#include <linux/notifier.h>    /* For notifier support */
-#include <linux/reboot.h>      /* For reboot_notifier stuff */
+#include <linux/reboot.h>      /* For kernel_power_off() */
 #include <linux/init.h>                /* For __init/__exit/... */
 #include <linux/fs.h>          /* For file operations */
+#include <linux/isa.h>         /* For isa devices */
 #include <linux/ioport.h>      /* For io-port access */
 #include <linux/spinlock.h>    /* For spin_lock/spin_unlock/... */
-#include <linux/sched.h>       /* TASK_INTERRUPTIBLE, set_current_state() and friends */
-#include <linux/slab.h>                /* For kmalloc */
 
 #include <asm/uaccess.h>       /* For copy_to_user/put_user/... */
 #include <asm/io.h>            /* For inb/outb/... */
 
 /* Module and version information */
-#define WD_VER                  "1.16 (03/01/2006)"
-#define PFX                    "pcwd: "
+#define WATCHDOG_VERSION "1.20"
+#define WATCHDOG_DATE "18 Feb 2007"
+#define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
+#define WATCHDOG_NAME "pcwd"
+#define PFX WATCHDOG_NAME ": "
+#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
+#define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
 
 /*
  * It should be noted that PCWD_REVISION_B was removed because A and B
 #define        PCWD_REVISION_C         2
 
 /*
- * These are the defines that describe the control status #1 bits for the
- * PC Watchdog card, revision A.
+ * These are the auto-probe addresses available.
+ *
+ * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
+ * Revision A has an address range of 2 addresses, while Revision C has 4.
  */
-#define WD_WDRST                0x01   /* Previously reset state */
-#define WD_T110                 0x02   /* Temperature overheat sense */
-#define WD_HRTBT                0x04   /* Heartbeat sense */
-#define WD_RLY2                 0x08   /* External relay triggered */
-#define WD_SRLY2                0x80   /* Software external relay triggered */
+#define PCWD_ISA_NR_CARDS      3
+static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
 
 /*
- * These are the defines that describe the control status #1 bits for the
- * PC Watchdog card, revision C.
- */
-#define WD_REVC_WTRP            0x01   /* Watchdog Trip status */
-#define WD_REVC_HRBT            0x02   /* Watchdog Heartbeat */
-#define WD_REVC_TTRP            0x04   /* Temperature Trip status */
+ * These are the defines that describe the control status bits for the
+ * PCI-PC Watchdog card.
+*/
+/* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
+#define WD_WDRST               0x01    /* Previously reset state */
+#define WD_T110                        0x02    /* Temperature overheat sense */
+#define WD_HRTBT               0x04    /* Heartbeat sense */
+#define WD_RLY2                        0x08    /* External relay triggered */
+#define WD_SRLY2               0x80    /* Software external relay triggered */
+/* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
+#define WD_REVC_WTRP           0x01    /* Watchdog Trip status */
+#define WD_REVC_HRBT           0x02    /* Watchdog Heartbeat */
+#define WD_REVC_TTRP           0x04    /* Temperature Trip status */
+#define WD_REVC_RL2A           0x08    /* Relay 2 activated by on-board processor */
+#define WD_REVC_RL1A           0x10    /* Relay 1 active */
+#define WD_REVC_R2DS           0x40    /* Relay 2 disable */
+#define WD_REVC_RLY2           0x80    /* Relay 2 activated? */
+/* Port 2 : Control Status #2 */
+#define WD_WDIS                        0x10    /* Watchdog Disabled */
+#define WD_ENTP                        0x20    /* Watchdog Enable Temperature Trip */
+#define WD_SSEL                        0x40    /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */
+#define WD_WCMD                        0x80    /* Watchdog Command Mode */
 
 /* max. time we give an ISA watchdog card to process a command */
 /* 500ms for each 4 bit response (according to spec.) */
 #define CMD_ISA_VERSION_HUNDRETH       0x03
 #define CMD_ISA_VERSION_MINOR          0x04
 #define CMD_ISA_SWITCH_SETTINGS                0x05
+#define CMD_ISA_RESET_PC               0x06
+#define CMD_ISA_ARM_0                  0x07
+#define CMD_ISA_ARM_30                 0x08
+#define CMD_ISA_ARM_60                 0x09
 #define CMD_ISA_DELAY_TIME_2SECS       0x0A
 #define CMD_ISA_DELAY_TIME_4SECS       0x0B
 #define CMD_ISA_DELAY_TIME_8SECS       0x0C
+#define CMD_ISA_RESET_RELAYS           0x0D
+
+/* Watchdog's Dip Switch heartbeat values */
+static const int heartbeat_tbl [] = {
+       20,     /* OFF-OFF-OFF  = 20 Sec  */
+       40,     /* OFF-OFF-ON   = 40 Sec  */
+       60,     /* OFF-ON-OFF   =  1 Min  */
+       300,    /* OFF-ON-ON    =  5 Min  */
+       600,    /* ON-OFF-OFF   = 10 Min  */
+       1800,   /* ON-OFF-ON    = 30 Min  */
+       3600,   /* ON-ON-OFF    =  1 Hour */
+       7200,   /* ON-ON-ON     =  2 hour */
+};
 
 /*
  * We are using an kernel timer to do the pinging of the watchdog
@@ -135,6 +169,7 @@ static atomic_t open_allowed = ATOMIC_INIT(1);
 static char expect_close;
 static int temp_panic;
 static struct {                                /* this is private data for each ISA-PC watchdog card */
+       char fw_ver_str[6];             /* The cards firmware version */
        int revision;                   /* The card's revision */
        int supports_temp;              /* Wether or not the card has a temperature device */
        int command_mode;               /* Wether or not the card is in command mode */
@@ -146,14 +181,21 @@ static struct {                           /* this is private data for each ISA-PC watchdog card */
 } pcwd_private;
 
 /* module parameters */
-#define WATCHDOG_HEARTBEAT 60          /* 60 sec default heartbeat */
+#define QUIET  0       /* Default */
+#define VERBOSE        1       /* Verbose */
+#define DEBUG  2       /* print fancy stuff too */
+static int debug = QUIET;
+module_param(debug, int, 0);
+MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
+
+#define WATCHDOG_HEARTBEAT 0           /* default heartbeat = delay-time from dip-switches */
 static int heartbeat = WATCHDOG_HEARTBEAT;
 module_param(heartbeat, int, 0);
-MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
+MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
 
 static int nowayout = WATCHDOG_NOWAYOUT;
 module_param(nowayout, int, 0);
-MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 /*
  *     Internal functions
@@ -165,8 +207,12 @@ static int send_isa_command(int cmd)
        int control_status;
        int port0, last_port0;  /* Double read for stabilising */
 
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
+                       cmd);
+
        /* The WCMD bit must be 1 and the command is only 4 bits in size */
-       control_status = (cmd & 0x0F) | 0x80;
+       control_status = (cmd & 0x0F) | WD_WCMD;
        outb_p(control_status, pcwd_private.io_addr + 2);
        udelay(ISA_COMMAND_TIMEOUT);
 
@@ -181,6 +227,10 @@ static int send_isa_command(int cmd)
                udelay (250);
        }
 
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
+                       cmd, port0, last_port0);
+
        return port0;
 }
 
@@ -207,6 +257,10 @@ static int set_command_mode(void)
        spin_unlock(&pcwd_private.io_lock);
        pcwd_private.command_mode = found;
 
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "command_mode=%d\n",
+                               pcwd_private.command_mode);
+
        return(found);
 }
 
@@ -219,6 +273,86 @@ static void unset_command_mode(void)
        spin_unlock(&pcwd_private.io_lock);
 
        pcwd_private.command_mode = 0;
+
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "command_mode=%d\n",
+                               pcwd_private.command_mode);
+}
+
+static inline void pcwd_check_temperature_support(void)
+{
+       if (inb(pcwd_private.io_addr) != 0xF0)
+               pcwd_private.supports_temp = 1;
+}
+
+static inline void pcwd_get_firmware(void)
+{
+       int one, ten, hund, minor;
+
+       strcpy(pcwd_private.fw_ver_str, "ERROR");
+
+       if (set_command_mode()) {
+               one = send_isa_command(CMD_ISA_VERSION_INTEGER);
+               ten = send_isa_command(CMD_ISA_VERSION_TENTH);
+               hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
+               minor = send_isa_command(CMD_ISA_VERSION_MINOR);
+               sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c", one, ten, hund, minor);
+       }
+       unset_command_mode();
+
+       return;
+}
+
+static inline int pcwd_get_option_switches(void)
+{
+       int option_switches=0;
+
+       if (set_command_mode()) {
+               /* Get switch settings */
+               option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
+       }
+
+       unset_command_mode();
+       return(option_switches);
+}
+
+static void pcwd_show_card_info(void)
+{
+       int option_switches;
+
+       /* Get some extra info from the hardware (in command/debug/diag mode) */
+       if (pcwd_private.revision == PCWD_REVISION_A)
+               printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
+       else if (pcwd_private.revision == PCWD_REVISION_C) {
+               pcwd_get_firmware();
+               printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
+                       pcwd_private.io_addr, pcwd_private.fw_ver_str);
+               option_switches = pcwd_get_option_switches();
+               printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
+                       option_switches,
+                       ((option_switches & 0x10) ? "ON" : "OFF"),
+                       ((option_switches & 0x08) ? "ON" : "OFF"));
+
+               /* Reprogram internal heartbeat to 2 seconds */
+               if (set_command_mode()) {
+                       send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
+                       unset_command_mode();
+               }
+       }
+
+       if (pcwd_private.supports_temp)
+               printk(KERN_INFO PFX "Temperature Option Detected\n");
+
+       if (pcwd_private.boot_status & WDIOF_CARDRESET)
+               printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
+
+       if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
+               printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
+               printk(KERN_EMERG PFX "CPU Overheat\n");
+       }
+
+       if (pcwd_private.boot_status == 0)
+               printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
 }
 
 static void pcwd_timer_ping(unsigned long data)
@@ -267,11 +401,15 @@ static int pcwd_start(void)
                udelay(ISA_COMMAND_TIMEOUT);
                stat_reg = inb_p(pcwd_private.io_addr + 2);
                spin_unlock(&pcwd_private.io_lock);
-               if (stat_reg & 0x10) {
+               if (stat_reg & WD_WDIS) {
                        printk(KERN_INFO PFX "Could not start watchdog\n");
                        return -EIO;
                }
        }
+
+       if (debug >= VERBOSE)
+               printk(KERN_DEBUG PFX "Watchdog started\n");
+
        return 0;
 }
 
@@ -291,11 +429,15 @@ static int pcwd_stop(void)
                udelay(ISA_COMMAND_TIMEOUT);
                stat_reg = inb_p(pcwd_private.io_addr + 2);
                spin_unlock(&pcwd_private.io_lock);
-               if ((stat_reg & 0x10) == 0) {
+               if ((stat_reg & WD_WDIS) == 0) {
                        printk(KERN_INFO PFX "Could not stop watchdog\n");
                        return -EIO;
                }
        }
+
+       if (debug >= VERBOSE)
+               printk(KERN_DEBUG PFX "Watchdog stopped\n");
+
        return 0;
 }
 
@@ -303,6 +445,10 @@ static int pcwd_keepalive(void)
 {
        /* user land ping */
        pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
+
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
+
        return 0;
 }
 
@@ -312,12 +458,17 @@ static int pcwd_set_heartbeat(int t)
                return -EINVAL;
 
        heartbeat = t;
+
+       if (debug >= VERBOSE)
+               printk(KERN_DEBUG PFX "New heartbeat: %d\n",
+                      heartbeat);
+
        return 0;
 }
 
 static int pcwd_get_status(int *status)
 {
-       int card_status;
+       int control_status;
 
        *status=0;
        spin_lock(&pcwd_private.io_lock);
@@ -325,37 +476,39 @@ static int pcwd_get_status(int *status)
                /* Rev A cards return status information from
                 * the base register, which is used for the
                 * temperature in other cards. */
-               card_status = inb(pcwd_private.io_addr);
+               control_status = inb(pcwd_private.io_addr);
        else {
                /* Rev C cards return card status in the base
                 * address + 1 register. And use different bits
                 * to indicate a card initiated reset, and an
                 * over-temperature condition. And the reboot
                 * status can be reset. */
-               card_status = inb(pcwd_private.io_addr + 1);
+               control_status = inb(pcwd_private.io_addr + 1);
        }
        spin_unlock(&pcwd_private.io_lock);
 
        if (pcwd_private.revision == PCWD_REVISION_A) {
-               if (card_status & WD_WDRST)
+               if (control_status & WD_WDRST)
                        *status |= WDIOF_CARDRESET;
 
-               if (card_status & WD_T110) {
+               if (control_status & WD_T110) {
                        *status |= WDIOF_OVERHEAT;
                        if (temp_panic) {
-                               printk (KERN_INFO PFX "Temperature overheat trip!\n");
+                               printk(KERN_INFO PFX "Temperature overheat trip!\n");
                                kernel_power_off();
+                               /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
                        }
                }
        } else {
-               if (card_status & WD_REVC_WTRP)
+               if (control_status & WD_REVC_WTRP)
                        *status |= WDIOF_CARDRESET;
 
-               if (card_status & WD_REVC_TTRP) {
+               if (control_status & WD_REVC_TTRP) {
                        *status |= WDIOF_OVERHEAT;
                        if (temp_panic) {
-                               printk (KERN_INFO PFX "Temperature overheat trip!\n");
+                               printk(KERN_INFO PFX "Temperature overheat trip!\n");
                                kernel_power_off();
+                               /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
                        }
                }
        }
@@ -365,9 +518,25 @@ static int pcwd_get_status(int *status)
 
 static int pcwd_clear_status(void)
 {
+       int control_status;
+
        if (pcwd_private.revision == PCWD_REVISION_C) {
                spin_lock(&pcwd_private.io_lock);
-               outb_p(0x00, pcwd_private.io_addr + 1); /* clear reset status */
+
+               if (debug >= VERBOSE)
+                       printk(KERN_INFO PFX "clearing watchdog trip status\n");
+
+               control_status = inb_p(pcwd_private.io_addr + 1);
+
+               if (debug >= DEBUG) {
+                       printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
+                       printk(KERN_DEBUG PFX "sending: 0x%02x\n",
+                               (control_status & WD_REVC_R2DS));
+               }
+
+               /* clear reset status & Keep Relay 2 disable state as it is */
+               outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1);
+
                spin_unlock(&pcwd_private.io_lock);
        }
        return 0;
@@ -391,6 +560,11 @@ static int pcwd_get_temperature(int *temperature)
        *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
        spin_unlock(&pcwd_private.io_lock);
 
+       if (debug >= DEBUG) {
+               printk(KERN_DEBUG PFX "temperature is: %d F\n",
+                       *temperature);
+       }
+
        return 0;
 }
 
@@ -418,7 +592,7 @@ static int pcwd_ioctl(struct inode *inode, struct file *file,
 
        switch(cmd) {
        default:
-               return -ENOIOCTLCMD;
+               return -ENOTTY;
 
        case WDIOC_GETSUPPORT:
                if(copy_to_user(argp, &ident, sizeof(ident)))
@@ -509,6 +683,8 @@ static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
 static int pcwd_open(struct inode *inode, struct file *file)
 {
        if (!atomic_dec_and_test(&open_allowed) ) {
+               if (debug >= VERBOSE)
+                       printk(KERN_ERR PFX "Attempt to open already opened device.\n");
                atomic_inc( &open_allowed );
                return -EBUSY;
        }
@@ -567,24 +743,10 @@ static int pcwd_temp_close(struct inode *inode, struct file *file)
 }
 
 /*
- *     Notify system
- */
-
-static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
-{
-       if (code==SYS_DOWN || code==SYS_HALT) {
-               /* Turn the WDT off */
-               pcwd_stop();
-       }
-
-       return NOTIFY_DONE;
-}
-
-/*
  *     Kernel Interfaces
  */
 
-static struct file_operations pcwd_fops = {
+static const struct file_operations pcwd_fops = {
        .owner          = THIS_MODULE,
        .llseek         = no_llseek,
        .write          = pcwd_write,
@@ -599,7 +761,7 @@ static struct miscdevice pcwd_miscdev = {
        .fops =         &pcwd_fops,
 };
 
-static struct file_operations pcwd_temp_fops = {
+static const struct file_operations pcwd_temp_fops = {
        .owner          = THIS_MODULE,
        .llseek         = no_llseek,
        .read           = pcwd_temp_read,
@@ -613,20 +775,10 @@ static struct miscdevice temp_miscdev = {
        .fops =         &pcwd_temp_fops,
 };
 
-static struct notifier_block pcwd_notifier = {
-       .notifier_call =        pcwd_notify_sys,
-};
-
 /*
  *     Init & exit routines
  */
 
-static inline void get_support(void)
-{
-       if (inb(pcwd_private.io_addr) != 0xF0)
-               pcwd_private.supports_temp = 1;
-}
-
 static inline int get_revision(void)
 {
        int r = PCWD_REVISION_C;
@@ -642,47 +794,66 @@ static inline int get_revision(void)
        return r;
 }
 
-static inline char *get_firmware(void)
+/*
+ *  The ISA cards have a heartbeat bit in one of the registers, which
+ *  register is card dependent.  The heartbeat bit is monitored, and if
+ *  found, is considered proof that a Berkshire card has been found.
+ *  The initial rate is once per second at board start up, then twice
+ *  per second for normal operation.
+ */
+static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
 {
-       int one, ten, hund, minor;
-       char *ret;
+       int base_addr=pcwd_ioports[id];
+       int port0, last_port0;  /* Reg 0, in case it's REV A */
+       int port1, last_port1;  /* Register 1 for REV C cards */
+       int i;
+       int retval;
 
-       ret = kmalloc(6, GFP_KERNEL);
-       if(ret == NULL)
-               return NULL;
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
+                       id);
 
-       if (set_command_mode()) {
-               one = send_isa_command(CMD_ISA_VERSION_INTEGER);
-               ten = send_isa_command(CMD_ISA_VERSION_TENTH);
-               hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
-               minor = send_isa_command(CMD_ISA_VERSION_MINOR);
-               sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
+       if (!request_region (base_addr, 4, "PCWD")) {
+               printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
+               return 0;
        }
-       else
-               sprintf(ret, "ERROR");
 
-       unset_command_mode();
-       return(ret);
-}
+       retval = 0;
 
-static inline int get_option_switches(void)
-{
-       int rv=0;
+       port0 = inb_p(base_addr);       /* For REV A boards */
+       port1 = inb_p(base_addr + 1);   /* For REV C boards */
+       if (port0 != 0xff || port1 != 0xff) {
+               /* Not an 'ff' from a floating bus, so must be a card! */
+               for (i = 0; i < 4; ++i) {
 
-       if (set_command_mode()) {
-               /* Get switch settings */
-               rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
+                       msleep(500);
+
+                       last_port0 = port0;
+                       last_port1 = port1;
+
+                       port0 = inb_p(base_addr);
+                       port1 = inb_p(base_addr + 1);
+
+                       /* Has either hearbeat bit changed?  */
+                       if ((port0 ^ last_port0) & WD_HRTBT ||
+                           (port1 ^ last_port1) & WD_REVC_HRBT) {
+                               retval = 1;
+                               break;
+                       }
+               }
        }
+       release_region (base_addr, 4);
 
-       unset_command_mode();
-       return(rv);
+       return retval;
 }
 
-static int __devinit pcwatchdog_init(int base_addr)
+static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
 {
        int ret;
-       char *firmware;
-       int option_switches;
+
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
+                       id);
 
        cards_found++;
        if (cards_found == 1)
@@ -693,11 +864,13 @@ static int __devinit pcwatchdog_init(int base_addr)
                return -ENODEV;
        }
 
-       if (base_addr == 0x0000) {
+       if (pcwd_ioports[id] == 0x0000) {
                printk(KERN_ERR PFX "No I/O-Address for card detected\n");
                return -ENODEV;
        }
-       pcwd_private.io_addr = base_addr;
+       pcwd_private.io_addr = pcwd_ioports[id];
+
+       spin_lock_init(&pcwd_private.io_lock);
 
        /* Check card's revision */
        pcwd_private.revision = get_revision();
@@ -705,8 +878,8 @@ static int __devinit pcwatchdog_init(int base_addr)
        if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
                printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
                        pcwd_private.io_addr);
-               pcwd_private.io_addr = 0x0000;
-               return -EIO;
+               ret=-EIO;
+               goto error_request_region;
        }
 
        /* Initial variables */
@@ -720,56 +893,20 @@ static int __devinit pcwatchdog_init(int base_addr)
        /* clear the "card caused reboot" flag */
        pcwd_clear_status();
 
-       init_timer(&pcwd_private.timer);
-       pcwd_private.timer.function = pcwd_timer_ping;
-       pcwd_private.timer.data = 0;
+       setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
 
        /*  Disable the board  */
        pcwd_stop();
 
        /*  Check whether or not the card supports the temperature device */
-       get_support();
+       pcwd_check_temperature_support();
 
-       /* Get some extra info from the hardware (in command/debug/diag mode) */
-       if (pcwd_private.revision == PCWD_REVISION_A)
-               printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
-       else if (pcwd_private.revision == PCWD_REVISION_C) {
-               firmware = get_firmware();
-               printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
-                       pcwd_private.io_addr, firmware);
-               kfree(firmware);
-               option_switches = get_option_switches();
-               printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
-                       option_switches,
-                       ((option_switches & 0x10) ? "ON" : "OFF"),
-                       ((option_switches & 0x08) ? "ON" : "OFF"));
+       /* Show info about the card itself */
+       pcwd_show_card_info();
 
-               /* Reprogram internal heartbeat to 2 seconds */
-               if (set_command_mode()) {
-                       send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
-                       unset_command_mode();
-               }
-       } else {
-               /* Should NEVER happen, unless get_revision() fails. */
-               printk(KERN_INFO PFX "Unable to get revision\n");
-               release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
-               pcwd_private.io_addr = 0x0000;
-               return -1;
-       }
-
-       if (pcwd_private.supports_temp)
-               printk(KERN_INFO PFX "Temperature Option Detected\n");
-
-       if (pcwd_private.boot_status & WDIOF_CARDRESET)
-               printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
-
-       if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
-               printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
-               printk(KERN_EMERG PFX "CPU Overheat\n");
-       }
-
-       if (pcwd_private.boot_status == 0)
-               printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
+       /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
+       if (heartbeat == 0)
+               heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
 
        /* Check that the heartbeat value is within it's range ; if not reset to the default */
        if (pcwd_set_heartbeat(heartbeat)) {
@@ -778,24 +915,12 @@ static int __devinit pcwatchdog_init(int base_addr)
                        WATCHDOG_HEARTBEAT);
        }
 
-       ret = register_reboot_notifier(&pcwd_notifier);
-       if (ret) {
-               printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
-                       ret);
-               release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
-               pcwd_private.io_addr = 0x0000;
-               return ret;
-       }
-
        if (pcwd_private.supports_temp) {
                ret = misc_register(&temp_miscdev);
                if (ret) {
                        printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
                                TEMP_MINOR, ret);
-                       unregister_reboot_notifier(&pcwd_notifier);
-                       release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
-                       pcwd_private.io_addr = 0x0000;
-                       return ret;
+                       goto error_misc_register_temp;
                }
        }
 
@@ -803,22 +928,34 @@ static int __devinit pcwatchdog_init(int base_addr)
        if (ret) {
                printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
                        WATCHDOG_MINOR, ret);
-               if (pcwd_private.supports_temp)
-                       misc_deregister(&temp_miscdev);
-               unregister_reboot_notifier(&pcwd_notifier);
-               release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
-               pcwd_private.io_addr = 0x0000;
-               return ret;
+               goto error_misc_register_watchdog;
        }
 
        printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
                heartbeat, nowayout);
 
        return 0;
+
+error_misc_register_watchdog:
+       if (pcwd_private.supports_temp)
+               misc_deregister(&temp_miscdev);
+error_misc_register_temp:
+       release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
+error_request_region:
+       pcwd_private.io_addr = 0x0000;
+       cards_found--;
+       return ret;
 }
 
-static void __devexit pcwatchdog_exit(void)
+static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
 {
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
+                       id);
+
+       if (!pcwd_private.io_addr)
+               return 1;
+
        /*  Disable the board  */
        if (!nowayout)
                pcwd_stop();
@@ -827,101 +964,50 @@ static void __devexit pcwatchdog_exit(void)
        misc_deregister(&pcwd_miscdev);
        if (pcwd_private.supports_temp)
                misc_deregister(&temp_miscdev);
-       unregister_reboot_notifier(&pcwd_notifier);
        release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
        pcwd_private.io_addr = 0x0000;
        cards_found--;
+
+       return 0;
 }
 
-/*
- *  The ISA cards have a heartbeat bit in one of the registers, which
- *  register is card dependent.  The heartbeat bit is monitored, and if
- *  found, is considered proof that a Berkshire card has been found.
- *  The initial rate is once per second at board start up, then twice
- *  per second for normal operation.
- */
-static int __init pcwd_checkcard(int base_addr)
+static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
 {
-       int port0, last_port0;  /* Reg 0, in case it's REV A */
-       int port1, last_port1;  /* Register 1 for REV C cards */
-       int i;
-       int retval;
-
-       if (!request_region (base_addr, 4, "PCWD")) {
-               printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
-               return 0;
-       }
-
-       retval = 0;
+       if (debug >= DEBUG)
+               printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
+                       id);
 
-       port0 = inb_p(base_addr);       /* For REV A boards */
-       port1 = inb_p(base_addr + 1);   /* For REV C boards */
-       if (port0 != 0xff || port1 != 0xff) {
-               /* Not an 'ff' from a floating bus, so must be a card! */
-               for (i = 0; i < 4; ++i) {
-
-                       msleep(500);
-
-                       last_port0 = port0;
-                       last_port1 = port1;
-
-                       port0 = inb_p(base_addr);
-                       port1 = inb_p(base_addr + 1);
-
-                       /* Has either hearbeat bit changed?  */
-                       if ((port0 ^ last_port0) & WD_HRTBT ||
-                           (port1 ^ last_port1) & WD_REVC_HRBT) {
-                               retval = 1;
-                               break;
-                       }
-               }
-       }
-       release_region (base_addr, 4);
-
-       return retval;
+       pcwd_stop();
 }
 
-/*
- * These are the auto-probe addresses available.
- *
- * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
- * Revision A has an address range of 2 addresses, while Revision C has 4.
- */
-static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
+static struct isa_driver pcwd_isa_driver = {
+       .match          = pcwd_isa_match,
+       .probe          = pcwd_isa_probe,
+       .remove         = __devexit_p(pcwd_isa_remove),
+       .shutdown       = pcwd_isa_shutdown,
+       .driver         = {
+               .owner  = THIS_MODULE,
+               .name   = WATCHDOG_NAME,
+       },
+};
 
 static int __init pcwd_init_module(void)
 {
-       int i, found = 0;
-
-       spin_lock_init(&pcwd_private.io_lock);
-
-       for (i = 0; pcwd_ioports[i] != 0; i++) {
-               if (pcwd_checkcard(pcwd_ioports[i])) {
-                       if (!(pcwatchdog_init(pcwd_ioports[i])))
-                               found++;
-               }
-       }
-
-       if (!found) {
-               printk (KERN_INFO PFX "No card detected, or port not available\n");
-               return -ENODEV;
-       }
-
-       return 0;
+       return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
 }
 
 static void __exit pcwd_cleanup_module(void)
 {
-       if (pcwd_private.io_addr)
-               pcwatchdog_exit();
-       return;
+       isa_unregister_driver(&pcwd_isa_driver);
+       printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
 }
 
 module_init(pcwd_init_module);
 module_exit(pcwd_cleanup_module);
 
-MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
+MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
 MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
+MODULE_VERSION(WATCHDOG_VERSION);
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 MODULE_ALIAS_MISCDEV(TEMP_MINOR);