3  * by Ken Hollis (khollis@bitgate.com)
 
   5  * Permission granted from Simon Machell (smachell@berkprod.com)
 
   6  * Written for the Linux Kernel, and GPLed by Ken Hollis
 
   8  * 960107       Added request_region routines, modulized the whole thing.
 
   9  * 960108       Fixed end-of-file pointer (Thanks to Dan Hollis), added
 
  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
 
  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
 
  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/
 
  52 #include <linux/module.h>       /* For module specific items */
 
  53 #include <linux/moduleparam.h>  /* For new moduleparam's */
 
  54 #include <linux/types.h>        /* For standard types (like size_t) */
 
  55 #include <linux/errno.h>        /* For the -ENODEV/... values */
 
  56 #include <linux/kernel.h>       /* For printk/panic/... */
 
  57 #include <linux/delay.h>        /* For mdelay function */
 
  58 #include <linux/timer.h>        /* For timer related operations */
 
  59 #include <linux/jiffies.h>      /* For jiffies stuff */
 
  60 #include <linux/miscdevice.h>   /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
 
  61 #include <linux/watchdog.h>     /* For the watchdog specific items */
 
  62 #include <linux/reboot.h>       /* For kernel_power_off() */
 
  63 #include <linux/init.h>         /* For __init/__exit/... */
 
  64 #include <linux/fs.h>           /* For file operations */
 
  65 #include <linux/isa.h>          /* For isa devices */
 
  66 #include <linux/ioport.h>       /* For io-port access */
 
  67 #include <linux/spinlock.h>     /* For spin_lock/spin_unlock/... */
 
  69 #include <asm/uaccess.h>        /* For copy_to_user/put_user/... */
 
  70 #include <asm/io.h>             /* For inb/outb/... */
 
  72 /* Module and version information */
 
  73 #define WATCHDOG_VERSION "1.20"
 
  74 #define WATCHDOG_DATE "18 Feb 2007"
 
  75 #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
 
  76 #define WATCHDOG_NAME "pcwd"
 
  77 #define PFX WATCHDOG_NAME ": "
 
  78 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
 
  79 #define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
 
  82  * It should be noted that PCWD_REVISION_B was removed because A and B
 
  83  * are essentially the same types of card, with the exception that B
 
  84  * has temperature reporting.  Since I didn't receive a Rev.B card,
 
  85  * the Rev.B card is not supported.  (It's a good thing too, as they
 
  86  * are no longer in production.)
 
  88 #define PCWD_REVISION_A         1
 
  89 #define PCWD_REVISION_C         2
 
  92  * These are the auto-probe addresses available.
 
  94  * Revision A only uses ports 0x270 and 0x370.  Revision C introduced 0x350.
 
  95  * Revision A has an address range of 2 addresses, while Revision C has 4.
 
  97 #define PCWD_ISA_NR_CARDS       3
 
  98 static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
 
 101  * These are the defines that describe the control status bits for the
 
 102  * PCI-PC Watchdog card.
 
 104 /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
 
 105 #define WD_WDRST                0x01    /* Previously reset state */
 
 106 #define WD_T110                 0x02    /* Temperature overheat sense */
 
 107 #define WD_HRTBT                0x04    /* Heartbeat sense */
 
 108 #define WD_RLY2                 0x08    /* External relay triggered */
 
 109 #define WD_SRLY2                0x80    /* Software external relay triggered */
 
 110 /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
 
 111 #define WD_REVC_WTRP            0x01    /* Watchdog Trip status */
 
 112 #define WD_REVC_HRBT            0x02    /* Watchdog Heartbeat */
 
 113 #define WD_REVC_TTRP            0x04    /* Temperature Trip status */
 
 114 #define WD_REVC_RL2A            0x08    /* Relay 2 activated by on-board processor */
 
 115 #define WD_REVC_RL1A            0x10    /* Relay 1 active */
 
 116 #define WD_REVC_R2DS            0x40    /* Relay 2 disable */
 
 117 #define WD_REVC_RLY2            0x80    /* Relay 2 activated? */
 
 118 /* Port 2 : Control Status #2 */
 
 119 #define WD_WDIS                 0x10    /* Watchdog Disabled */
 
 120 #define WD_ENTP                 0x20    /* Watchdog Enable Temperature Trip */
 
 121 #define WD_SSEL                 0x40    /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */
 
 122 #define WD_WCMD                 0x80    /* Watchdog Command Mode */
 
 124 /* max. time we give an ISA watchdog card to process a command */
 
 125 /* 500ms for each 4 bit response (according to spec.) */
 
 126 #define ISA_COMMAND_TIMEOUT     1000
 
 128 /* Watchdog's internal commands */
 
 129 #define CMD_ISA_IDLE                    0x00
 
 130 #define CMD_ISA_VERSION_INTEGER         0x01
 
 131 #define CMD_ISA_VERSION_TENTH           0x02
 
 132 #define CMD_ISA_VERSION_HUNDRETH        0x03
 
 133 #define CMD_ISA_VERSION_MINOR           0x04
 
 134 #define CMD_ISA_SWITCH_SETTINGS         0x05
 
 135 #define CMD_ISA_RESET_PC                0x06
 
 136 #define CMD_ISA_ARM_0                   0x07
 
 137 #define CMD_ISA_ARM_30                  0x08
 
 138 #define CMD_ISA_ARM_60                  0x09
 
 139 #define CMD_ISA_DELAY_TIME_2SECS        0x0A
 
 140 #define CMD_ISA_DELAY_TIME_4SECS        0x0B
 
 141 #define CMD_ISA_DELAY_TIME_8SECS        0x0C
 
 142 #define CMD_ISA_RESET_RELAYS            0x0D
 
 144 /* Watchdog's Dip Switch heartbeat values */
 
 145 static const int heartbeat_tbl [] = {
 
 146         20,     /* OFF-OFF-OFF  = 20 Sec  */
 
 147         40,     /* OFF-OFF-ON   = 40 Sec  */
 
 148         60,     /* OFF-ON-OFF   =  1 Min  */
 
 149         300,    /* OFF-ON-ON    =  5 Min  */
 
 150         600,    /* ON-OFF-OFF   = 10 Min  */
 
 151         1800,   /* ON-OFF-ON    = 30 Min  */
 
 152         3600,   /* ON-ON-OFF    =  1 Hour */
 
 153         7200,   /* ON-ON-ON     =  2 hour */
 
 157  * We are using an kernel timer to do the pinging of the watchdog
 
 158  * every ~500ms. We try to set the internal heartbeat of the
 
 162 #define WDT_INTERVAL (HZ/2+1)
 
 164 /* We can only use 1 card due to the /dev/watchdog restriction */
 
 165 static int cards_found;
 
 167 /* internal variables */
 
 168 static atomic_t open_allowed = ATOMIC_INIT(1);
 
 169 static char expect_close;
 
 170 static int temp_panic;
 
 171 static struct {                         /* this is private data for each ISA-PC watchdog card */
 
 172         char fw_ver_str[6];             /* The cards firmware version */
 
 173         int revision;                   /* The card's revision */
 
 174         int supports_temp;              /* Wether or not the card has a temperature device */
 
 175         int command_mode;               /* Wether or not the card is in command mode */
 
 176         int boot_status;                /* The card's boot status */
 
 177         int io_addr;                    /* The cards I/O address */
 
 178         spinlock_t io_lock;             /* the lock for io operations */
 
 179         struct timer_list timer;        /* The timer that pings the watchdog */
 
 180         unsigned long next_heartbeat;   /* the next_heartbeat for the timer */
 
 183 /* module parameters */
 
 184 #define QUIET   0       /* Default */
 
 185 #define VERBOSE 1       /* Verbose */
 
 186 #define DEBUG   2       /* print fancy stuff too */
 
 187 static int debug = QUIET;
 
 188 module_param(debug, int, 0);
 
 189 MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
 
 191 #define WATCHDOG_HEARTBEAT 0            /* default heartbeat = delay-time from dip-switches */
 
 192 static int heartbeat = WATCHDOG_HEARTBEAT;
 
 193 module_param(heartbeat, int, 0);
 
 194 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
 
 196 static int nowayout = WATCHDOG_NOWAYOUT;
 
 197 module_param(nowayout, int, 0);
 
 198 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 
 204 static int send_isa_command(int cmd)
 
 208         int port0, last_port0;  /* Double read for stabilising */
 
 211                 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
 
 214         /* The WCMD bit must be 1 and the command is only 4 bits in size */
 
 215         control_status = (cmd & 0x0F) | WD_WCMD;
 
 216         outb_p(control_status, pcwd_private.io_addr + 2);
 
 217         udelay(ISA_COMMAND_TIMEOUT);
 
 219         port0 = inb_p(pcwd_private.io_addr);
 
 220         for (i = 0; i < 25; ++i) {
 
 222                 port0 = inb_p(pcwd_private.io_addr);
 
 224                 if (port0 == last_port0)
 
 225                         break;  /* Data is stable */
 
 231                 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
 
 232                         cmd, port0, last_port0);
 
 237 static int set_command_mode(void)
 
 239         int i, found=0, count=0;
 
 241         /* Set the card into command mode */
 
 242         spin_lock(&pcwd_private.io_lock);
 
 243         while ((!found) && (count < 3)) {
 
 244                 i = send_isa_command(CMD_ISA_IDLE);
 
 248                 else if (i == 0xF3) {
 
 249                         /* Card does not like what we've done to it */
 
 250                         outb_p(0x00, pcwd_private.io_addr + 2);
 
 251                         udelay(1200);   /* Spec says wait 1ms */
 
 252                         outb_p(0x00, pcwd_private.io_addr + 2);
 
 253                         udelay(ISA_COMMAND_TIMEOUT);
 
 257         spin_unlock(&pcwd_private.io_lock);
 
 258         pcwd_private.command_mode = found;
 
 261                 printk(KERN_DEBUG PFX "command_mode=%d\n",
 
 262                                 pcwd_private.command_mode);
 
 267 static void unset_command_mode(void)
 
 269         /* Set the card into normal mode */
 
 270         spin_lock(&pcwd_private.io_lock);
 
 271         outb_p(0x00, pcwd_private.io_addr + 2);
 
 272         udelay(ISA_COMMAND_TIMEOUT);
 
 273         spin_unlock(&pcwd_private.io_lock);
 
 275         pcwd_private.command_mode = 0;
 
 278                 printk(KERN_DEBUG PFX "command_mode=%d\n",
 
 279                                 pcwd_private.command_mode);
 
 282 static inline void pcwd_check_temperature_support(void)
 
 284         if (inb(pcwd_private.io_addr) != 0xF0)
 
 285                 pcwd_private.supports_temp = 1;
 
 288 static inline void pcwd_get_firmware(void)
 
 290         int one, ten, hund, minor;
 
 292         strcpy(pcwd_private.fw_ver_str, "ERROR");
 
 294         if (set_command_mode()) {
 
 295                 one = send_isa_command(CMD_ISA_VERSION_INTEGER);
 
 296                 ten = send_isa_command(CMD_ISA_VERSION_TENTH);
 
 297                 hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
 
 298                 minor = send_isa_command(CMD_ISA_VERSION_MINOR);
 
 299                 sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c", one, ten, hund, minor);
 
 301         unset_command_mode();
 
 306 static inline int pcwd_get_option_switches(void)
 
 308         int option_switches=0;
 
 310         if (set_command_mode()) {
 
 311                 /* Get switch settings */
 
 312                 option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
 
 315         unset_command_mode();
 
 316         return(option_switches);
 
 319 static void pcwd_show_card_info(void)
 
 323         /* Get some extra info from the hardware (in command/debug/diag mode) */
 
 324         if (pcwd_private.revision == PCWD_REVISION_A)
 
 325                 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
 
 326         else if (pcwd_private.revision == PCWD_REVISION_C) {
 
 328                 printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
 
 329                         pcwd_private.io_addr, pcwd_private.fw_ver_str);
 
 330                 option_switches = pcwd_get_option_switches();
 
 331                 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
 
 333                         ((option_switches & 0x10) ? "ON" : "OFF"),
 
 334                         ((option_switches & 0x08) ? "ON" : "OFF"));
 
 336                 /* Reprogram internal heartbeat to 2 seconds */
 
 337                 if (set_command_mode()) {
 
 338                         send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
 
 339                         unset_command_mode();
 
 343         if (pcwd_private.supports_temp)
 
 344                 printk(KERN_INFO PFX "Temperature Option Detected\n");
 
 346         if (pcwd_private.boot_status & WDIOF_CARDRESET)
 
 347                 printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
 
 349         if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
 
 350                 printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
 
 351                 printk(KERN_EMERG PFX "CPU Overheat\n");
 
 354         if (pcwd_private.boot_status == 0)
 
 355                 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
 
 358 static void pcwd_timer_ping(unsigned long data)
 
 362         /* If we got a heartbeat pulse within the WDT_INTERVAL
 
 363          * we agree to ping the WDT */
 
 364         if(time_before(jiffies, pcwd_private.next_heartbeat)) {
 
 365                 /* Ping the watchdog */
 
 366                 spin_lock(&pcwd_private.io_lock);
 
 367                 if (pcwd_private.revision == PCWD_REVISION_A) {
 
 368                         /*  Rev A cards are reset by setting the WD_WDRST bit in register 1 */
 
 369                         wdrst_stat = inb_p(pcwd_private.io_addr);
 
 371                         wdrst_stat |= WD_WDRST;
 
 373                         outb_p(wdrst_stat, pcwd_private.io_addr + 1);
 
 375                         /* Re-trigger watchdog by writing to port 0 */
 
 376                         outb_p(0x00, pcwd_private.io_addr);
 
 379                 /* Re-set the timer interval */
 
 380                 mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
 
 382                 spin_unlock(&pcwd_private.io_lock);
 
 384                 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
 
 388 static int pcwd_start(void)
 
 392         pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
 
 394         /* Start the timer */
 
 395         mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
 
 397         /* Enable the port */
 
 398         if (pcwd_private.revision == PCWD_REVISION_C) {
 
 399                 spin_lock(&pcwd_private.io_lock);
 
 400                 outb_p(0x00, pcwd_private.io_addr + 3);
 
 401                 udelay(ISA_COMMAND_TIMEOUT);
 
 402                 stat_reg = inb_p(pcwd_private.io_addr + 2);
 
 403                 spin_unlock(&pcwd_private.io_lock);
 
 404                 if (stat_reg & WD_WDIS) {
 
 405                         printk(KERN_INFO PFX "Could not start watchdog\n");
 
 410         if (debug >= VERBOSE)
 
 411                 printk(KERN_DEBUG PFX "Watchdog started\n");
 
 416 static int pcwd_stop(void)
 
 421         del_timer(&pcwd_private.timer);
 
 423         /*  Disable the board  */
 
 424         if (pcwd_private.revision == PCWD_REVISION_C) {
 
 425                 spin_lock(&pcwd_private.io_lock);
 
 426                 outb_p(0xA5, pcwd_private.io_addr + 3);
 
 427                 udelay(ISA_COMMAND_TIMEOUT);
 
 428                 outb_p(0xA5, pcwd_private.io_addr + 3);
 
 429                 udelay(ISA_COMMAND_TIMEOUT);
 
 430                 stat_reg = inb_p(pcwd_private.io_addr + 2);
 
 431                 spin_unlock(&pcwd_private.io_lock);
 
 432                 if ((stat_reg & WD_WDIS) == 0) {
 
 433                         printk(KERN_INFO PFX "Could not stop watchdog\n");
 
 438         if (debug >= VERBOSE)
 
 439                 printk(KERN_DEBUG PFX "Watchdog stopped\n");
 
 444 static int pcwd_keepalive(void)
 
 447         pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
 
 450                 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
 
 455 static int pcwd_set_heartbeat(int t)
 
 457         if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
 
 462         if (debug >= VERBOSE)
 
 463                 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
 
 469 static int pcwd_get_status(int *status)
 
 474         spin_lock(&pcwd_private.io_lock);
 
 475         if (pcwd_private.revision == PCWD_REVISION_A)
 
 476                 /* Rev A cards return status information from
 
 477                  * the base register, which is used for the
 
 478                  * temperature in other cards. */
 
 479                 control_status = inb(pcwd_private.io_addr);
 
 481                 /* Rev C cards return card status in the base
 
 482                  * address + 1 register. And use different bits
 
 483                  * to indicate a card initiated reset, and an
 
 484                  * over-temperature condition. And the reboot
 
 485                  * status can be reset. */
 
 486                 control_status = inb(pcwd_private.io_addr + 1);
 
 488         spin_unlock(&pcwd_private.io_lock);
 
 490         if (pcwd_private.revision == PCWD_REVISION_A) {
 
 491                 if (control_status & WD_WDRST)
 
 492                         *status |= WDIOF_CARDRESET;
 
 494                 if (control_status & WD_T110) {
 
 495                         *status |= WDIOF_OVERHEAT;
 
 497                                 printk(KERN_INFO PFX "Temperature overheat trip!\n");
 
 499                                 /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
 
 503                 if (control_status & WD_REVC_WTRP)
 
 504                         *status |= WDIOF_CARDRESET;
 
 506                 if (control_status & WD_REVC_TTRP) {
 
 507                         *status |= WDIOF_OVERHEAT;
 
 509                                 printk(KERN_INFO PFX "Temperature overheat trip!\n");
 
 511                                 /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
 
 519 static int pcwd_clear_status(void)
 
 523         if (pcwd_private.revision == PCWD_REVISION_C) {
 
 524                 spin_lock(&pcwd_private.io_lock);
 
 526                 if (debug >= VERBOSE)
 
 527                         printk(KERN_INFO PFX "clearing watchdog trip status\n");
 
 529                 control_status = inb_p(pcwd_private.io_addr + 1);
 
 531                 if (debug >= DEBUG) {
 
 532                         printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
 
 533                         printk(KERN_DEBUG PFX "sending: 0x%02x\n",
 
 534                                 (control_status & WD_REVC_R2DS));
 
 537                 /* clear reset status & Keep Relay 2 disable state as it is */
 
 538                 outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1);
 
 540                 spin_unlock(&pcwd_private.io_lock);
 
 545 static int pcwd_get_temperature(int *temperature)
 
 547         /* check that port 0 gives temperature info and no command results */
 
 548         if (pcwd_private.command_mode)
 
 552         if (!pcwd_private.supports_temp)
 
 556          * Convert celsius to fahrenheit, since this was
 
 557          * the decided 'standard' for this return value.
 
 559         spin_lock(&pcwd_private.io_lock);
 
 560         *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
 
 561         spin_unlock(&pcwd_private.io_lock);
 
 563         if (debug >= DEBUG) {
 
 564                 printk(KERN_DEBUG PFX "temperature is: %d F\n",
 
 572  *      /dev/watchdog handling
 
 575 static int pcwd_ioctl(struct inode *inode, struct file *file,
 
 576                       unsigned int cmd, unsigned long arg)
 
 582         int __user *argp = (int __user *)arg;
 
 583         static struct watchdog_info ident = {
 
 584                 .options =              WDIOF_OVERHEAT |
 
 586                                         WDIOF_KEEPALIVEPING |
 
 589                 .firmware_version =     1,
 
 597         case WDIOC_GETSUPPORT:
 
 598                 if(copy_to_user(argp, &ident, sizeof(ident)))
 
 602         case WDIOC_GETSTATUS:
 
 603                 pcwd_get_status(&status);
 
 604                 return put_user(status, argp);
 
 606         case WDIOC_GETBOOTSTATUS:
 
 607                 return put_user(pcwd_private.boot_status, argp);
 
 610                 if (pcwd_get_temperature(&temperature))
 
 613                 return put_user(temperature, argp);
 
 615         case WDIOC_SETOPTIONS:
 
 616                 if (pcwd_private.revision == PCWD_REVISION_C)
 
 618                         if(copy_from_user(&rv, argp, sizeof(int)))
 
 621                         if (rv & WDIOS_DISABLECARD)
 
 626                         if (rv & WDIOS_ENABLECARD)
 
 631                         if (rv & WDIOS_TEMPPANIC)
 
 638         case WDIOC_KEEPALIVE:
 
 642         case WDIOC_SETTIMEOUT:
 
 643                 if (get_user(new_heartbeat, argp))
 
 646                 if (pcwd_set_heartbeat(new_heartbeat))
 
 652         case WDIOC_GETTIMEOUT:
 
 653                 return put_user(heartbeat, argp);
 
 659 static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
 
 666                         /* In case it was set long ago */
 
 669                         for (i = 0; i != len; i++) {
 
 672                                 if (get_user(c, buf + i))
 
 683 static int pcwd_open(struct inode *inode, struct file *file)
 
 685         if (!atomic_dec_and_test(&open_allowed) ) {
 
 686                 if (debug >= VERBOSE)
 
 687                         printk(KERN_ERR PFX "Attempt to open already opened device.\n");
 
 688                 atomic_inc( &open_allowed );
 
 693                 __module_get(THIS_MODULE);
 
 698         return nonseekable_open(inode, file);
 
 701 static int pcwd_close(struct inode *inode, struct file *file)
 
 703         if (expect_close == 42) {
 
 706                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
 
 710         atomic_inc( &open_allowed );
 
 715  *      /dev/temperature handling
 
 718 static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
 
 723         if (pcwd_get_temperature(&temperature))
 
 726         if (copy_to_user(buf, &temperature, 1))
 
 732 static int pcwd_temp_open(struct inode *inode, struct file *file)
 
 734         if (!pcwd_private.supports_temp)
 
 737         return nonseekable_open(inode, file);
 
 740 static int pcwd_temp_close(struct inode *inode, struct file *file)
 
 749 static const struct file_operations pcwd_fops = {
 
 750         .owner          = THIS_MODULE,
 
 755         .release        = pcwd_close,
 
 758 static struct miscdevice pcwd_miscdev = {
 
 759         .minor =        WATCHDOG_MINOR,
 
 764 static const struct file_operations pcwd_temp_fops = {
 
 765         .owner          = THIS_MODULE,
 
 767         .read           = pcwd_temp_read,
 
 768         .open           = pcwd_temp_open,
 
 769         .release        = pcwd_temp_close,
 
 772 static struct miscdevice temp_miscdev = {
 
 774         .name =         "temperature",
 
 775         .fops =         &pcwd_temp_fops,
 
 779  *      Init & exit routines
 
 782 static inline int get_revision(void)
 
 784         int r = PCWD_REVISION_C;
 
 786         spin_lock(&pcwd_private.io_lock);
 
 787         /* REV A cards use only 2 io ports; test
 
 788          * presumes a floating bus reads as 0xff. */
 
 789         if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
 
 790             (inb(pcwd_private.io_addr + 3) == 0xFF))
 
 792         spin_unlock(&pcwd_private.io_lock);
 
 798  *  The ISA cards have a heartbeat bit in one of the registers, which
 
 799  *  register is card dependent.  The heartbeat bit is monitored, and if
 
 800  *  found, is considered proof that a Berkshire card has been found.
 
 801  *  The initial rate is once per second at board start up, then twice
 
 802  *  per second for normal operation.
 
 804 static int __devinit pcwd_isa_match(struct device *dev, unsigned int id)
 
 806         int base_addr=pcwd_ioports[id];
 
 807         int port0, last_port0;  /* Reg 0, in case it's REV A */
 
 808         int port1, last_port1;  /* Register 1 for REV C cards */
 
 813                 printk(KERN_DEBUG PFX "pcwd_isa_match id=%d\n",
 
 816         if (!request_region (base_addr, 4, "PCWD")) {
 
 817                 printk(KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
 
 823         port0 = inb_p(base_addr);       /* For REV A boards */
 
 824         port1 = inb_p(base_addr + 1);   /* For REV C boards */
 
 825         if (port0 != 0xff || port1 != 0xff) {
 
 826                 /* Not an 'ff' from a floating bus, so must be a card! */
 
 827                 for (i = 0; i < 4; ++i) {
 
 834                         port0 = inb_p(base_addr);
 
 835                         port1 = inb_p(base_addr + 1);
 
 837                         /* Has either hearbeat bit changed?  */
 
 838                         if ((port0 ^ last_port0) & WD_HRTBT ||
 
 839                             (port1 ^ last_port1) & WD_REVC_HRBT) {
 
 845         release_region (base_addr, 4);
 
 850 static int __devinit pcwd_isa_probe(struct device *dev, unsigned int id)
 
 855                 printk(KERN_DEBUG PFX "pcwd_isa_probe id=%d\n",
 
 859         if (cards_found == 1)
 
 860                 printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
 
 862         if (cards_found > 1) {
 
 863                 printk(KERN_ERR PFX "This driver only supports 1 device\n");
 
 867         if (pcwd_ioports[id] == 0x0000) {
 
 868                 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
 
 871         pcwd_private.io_addr = pcwd_ioports[id];
 
 873         spin_lock_init(&pcwd_private.io_lock);
 
 875         /* Check card's revision */
 
 876         pcwd_private.revision = get_revision();
 
 878         if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
 
 879                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
 
 880                         pcwd_private.io_addr);
 
 882                 goto error_request_region;
 
 885         /* Initial variables */
 
 886         pcwd_private.supports_temp = 0;
 
 888         pcwd_private.boot_status = 0x0000;
 
 890         /* get the boot_status */
 
 891         pcwd_get_status(&pcwd_private.boot_status);
 
 893         /* clear the "card caused reboot" flag */
 
 896         setup_timer(&pcwd_private.timer, pcwd_timer_ping, 0);
 
 898         /*  Disable the board  */
 
 901         /*  Check whether or not the card supports the temperature device */
 
 902         pcwd_check_temperature_support();
 
 904         /* Show info about the card itself */
 
 905         pcwd_show_card_info();
 
 907         /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
 
 909                 heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
 
 911         /* Check that the heartbeat value is within it's range ; if not reset to the default */
 
 912         if (pcwd_set_heartbeat(heartbeat)) {
 
 913                 pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
 
 914                 printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
 
 918         if (pcwd_private.supports_temp) {
 
 919                 ret = misc_register(&temp_miscdev);
 
 921                         printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
 
 923                         goto error_misc_register_temp;
 
 927         ret = misc_register(&pcwd_miscdev);
 
 929                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
 
 930                         WATCHDOG_MINOR, ret);
 
 931                 goto error_misc_register_watchdog;
 
 934         printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
 
 935                 heartbeat, nowayout);
 
 939 error_misc_register_watchdog:
 
 940         if (pcwd_private.supports_temp)
 
 941                 misc_deregister(&temp_miscdev);
 
 942 error_misc_register_temp:
 
 943         release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
 
 944 error_request_region:
 
 945         pcwd_private.io_addr = 0x0000;
 
 950 static int __devexit pcwd_isa_remove(struct device *dev, unsigned int id)
 
 953                 printk(KERN_DEBUG PFX "pcwd_isa_remove id=%d\n",
 
 956         if (!pcwd_private.io_addr)
 
 959         /*  Disable the board  */
 
 964         misc_deregister(&pcwd_miscdev);
 
 965         if (pcwd_private.supports_temp)
 
 966                 misc_deregister(&temp_miscdev);
 
 967         release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
 
 968         pcwd_private.io_addr = 0x0000;
 
 974 static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
 
 977                 printk(KERN_DEBUG PFX "pcwd_isa_shutdown id=%d\n",
 
 983 static struct isa_driver pcwd_isa_driver = {
 
 984         .match          = pcwd_isa_match,
 
 985         .probe          = pcwd_isa_probe,
 
 986         .remove         = __devexit_p(pcwd_isa_remove),
 
 987         .shutdown       = pcwd_isa_shutdown,
 
 989                 .owner  = THIS_MODULE,
 
 990                 .name   = WATCHDOG_NAME,
 
 994 static int __init pcwd_init_module(void)
 
 996         return isa_register_driver(&pcwd_isa_driver, PCWD_ISA_NR_CARDS);
 
 999 static void __exit pcwd_cleanup_module(void)
 
1001         isa_unregister_driver(&pcwd_isa_driver);
 
1002         printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
 
1005 module_init(pcwd_init_module);
 
1006 module_exit(pcwd_cleanup_module);
 
1008 MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>, Wim Van Sebroeck <wim@iguana.be>");
 
1009 MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
 
1010 MODULE_VERSION(WATCHDOG_VERSION);
 
1011 MODULE_LICENSE("GPL");
 
1012 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 
1013 MODULE_ALIAS_MISCDEV(TEMP_MINOR);