gpiolib: fix off by one errors
[linux-2.6] / drivers / watchdog / pcwd_pci.c
1 /*
2  *      Berkshire PCI-PC Watchdog Card Driver
3  *
4  *      (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
5  *
6  *      Based on source code of the following authors:
7  *        Ken Hollis <kenji@bitgate.com>,
8  *        Lindsay Harris <lindsay@bluegum.com>,
9  *        Alan Cox <alan@redhat.com>,
10  *        Matt Domsch <Matt_Domsch@dell.com>,
11  *        Rob Radez <rob@osinvestor.com>
12  *
13  *      This program is free software; you can redistribute it and/or
14  *      modify it under the terms of the GNU General Public License
15  *      as published by the Free Software Foundation; either version
16  *      2 of the License, or (at your option) any later version.
17  *
18  *      Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
19  *      provide warranty for any of this software. This material is
20  *      provided "AS-IS" and at no charge.
21  */
22
23 /*
24  *      A bells and whistles driver is available from:
25  *      http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
26  *
27  *      More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
28  */
29
30 /*
31  *      Includes, defines, variables, module parameters, ...
32  */
33
34 #include <linux/module.h>       /* For module specific items */
35 #include <linux/moduleparam.h>  /* For new moduleparam's */
36 #include <linux/types.h>        /* For standard types (like size_t) */
37 #include <linux/errno.h>        /* For the -ENODEV/... values */
38 #include <linux/kernel.h>       /* For printk/panic/... */
39 #include <linux/delay.h>        /* For mdelay function */
40 #include <linux/miscdevice.h>   /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
41 #include <linux/watchdog.h>     /* For the watchdog specific items */
42 #include <linux/notifier.h>     /* For notifier support */
43 #include <linux/reboot.h>       /* For reboot_notifier stuff */
44 #include <linux/init.h>         /* For __init/__exit/... */
45 #include <linux/fs.h>           /* For file operations */
46 #include <linux/pci.h>          /* For pci functions */
47 #include <linux/ioport.h>       /* For io-port access */
48 #include <linux/spinlock.h>     /* For spin_lock/spin_unlock/... */
49
50 #include <asm/uaccess.h>        /* For copy_to_user/put_user/... */
51 #include <asm/io.h>             /* For inb/outb/... */
52
53 /* Module and version information */
54 #define WATCHDOG_VERSION "1.03"
55 #define WATCHDOG_DATE "21 Jan 2007"
56 #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
57 #define WATCHDOG_NAME "pcwd_pci"
58 #define PFX WATCHDOG_NAME ": "
59 #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
60
61 /* Stuff for the PCI ID's  */
62 #ifndef PCI_VENDOR_ID_QUICKLOGIC
63 #define PCI_VENDOR_ID_QUICKLOGIC    0x11e3
64 #endif
65
66 #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
67 #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
68 #endif
69
70 /*
71  * These are the defines that describe the control status bits for the
72  * PCI-PC Watchdog card.
73  */
74 /* Port 1 : Control Status #1 */
75 #define WD_PCI_WTRP             0x01    /* Watchdog Trip status */
76 #define WD_PCI_HRBT             0x02    /* Watchdog Heartbeat */
77 #define WD_PCI_TTRP             0x04    /* Temperature Trip status */
78 #define WD_PCI_RL2A             0x08    /* Relay 2 Active */
79 #define WD_PCI_RL1A             0x10    /* Relay 1 Active */
80 #define WD_PCI_R2DS             0x40    /* Relay 2 Disable Temperature-trip/reset */
81 #define WD_PCI_RLY2             0x80    /* Activate Relay 2 on the board */
82 /* Port 2 : Control Status #2 */
83 #define WD_PCI_WDIS             0x10    /* Watchdog Disable */
84 #define WD_PCI_ENTP             0x20    /* Enable Temperature Trip Reset */
85 #define WD_PCI_WRSP             0x40    /* Watchdog wrote response */
86 #define WD_PCI_PCMD             0x80    /* PC has sent command */
87
88 /* according to documentation max. time to process a command for the pci
89  * watchdog card is 100 ms, so we give it 150 ms to do it's job */
90 #define PCI_COMMAND_TIMEOUT     150
91
92 /* Watchdog's internal commands */
93 #define CMD_GET_STATUS                          0x04
94 #define CMD_GET_FIRMWARE_VERSION                0x08
95 #define CMD_READ_WATCHDOG_TIMEOUT               0x18
96 #define CMD_WRITE_WATCHDOG_TIMEOUT              0x19
97 #define CMD_GET_CLEAR_RESET_COUNT               0x84
98
99 /* Watchdog's Dip Switch heartbeat values */
100 static const int heartbeat_tbl [] = {
101         5,      /* OFF-OFF-OFF  =  5 Sec  */
102         10,     /* OFF-OFF-ON   = 10 Sec  */
103         30,     /* OFF-ON-OFF   = 30 Sec  */
104         60,     /* OFF-ON-ON    =  1 Min  */
105         300,    /* ON-OFF-OFF   =  5 Min  */
106         600,    /* ON-OFF-ON    = 10 Min  */
107         1800,   /* ON-ON-OFF    = 30 Min  */
108         3600,   /* ON-ON-ON     =  1 hour */
109 };
110
111 /* We can only use 1 card due to the /dev/watchdog restriction */
112 static int cards_found;
113
114 /* internal variables */
115 static int temp_panic;
116 static unsigned long is_active;
117 static char expect_release;
118 static struct {                         /* this is private data for each PCI-PC watchdog card */
119         int supports_temp;              /* Wether or not the card has a temperature device */
120         int boot_status;                /* The card's boot status */
121         unsigned long io_addr;          /* The cards I/O address */
122         spinlock_t io_lock;             /* the lock for io operations */
123         struct pci_dev *pdev;           /* the PCI-device */
124 } pcipcwd_private;
125
126 /* module parameters */
127 #define QUIET   0       /* Default */
128 #define VERBOSE 1       /* Verbose */
129 #define DEBUG   2       /* print fancy stuff too */
130 static int debug = QUIET;
131 module_param(debug, int, 0);
132 MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
133
134 #define WATCHDOG_HEARTBEAT 0    /* default heartbeat = delay-time from dip-switches */
135 static int heartbeat = WATCHDOG_HEARTBEAT;
136 module_param(heartbeat, int, 0);
137 MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
138
139 static int nowayout = WATCHDOG_NOWAYOUT;
140 module_param(nowayout, int, 0);
141 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
142
143 /*
144  *      Internal functions
145  */
146
147 static int send_command(int cmd, int *msb, int *lsb)
148 {
149         int got_response, count;
150
151         if (debug >= DEBUG)
152                 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
153                 cmd, *msb, *lsb);
154
155         spin_lock(&pcipcwd_private.io_lock);
156         /* If a command requires data it should be written first.
157          * Data for commands with 8 bits of data should be written to port 4.
158          * Commands with 16 bits of data, should be written as LSB to port 4
159          * and MSB to port 5.
160          * After the required data has been written then write the command to
161          * port 6. */
162         outb_p(*lsb, pcipcwd_private.io_addr + 4);
163         outb_p(*msb, pcipcwd_private.io_addr + 5);
164         outb_p(cmd, pcipcwd_private.io_addr + 6);
165
166         /* wait till the pci card processed the command, signaled by
167          * the WRSP bit in port 2 and give it a max. timeout of
168          * PCI_COMMAND_TIMEOUT to process */
169         got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
170         for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
171                 mdelay(1);
172                 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
173         }
174
175         if (debug >= DEBUG) {
176                 if (got_response) {
177                         printk(KERN_DEBUG PFX "time to process command was: %d ms\n",
178                                 count);
179                 } else {
180                         printk(KERN_DEBUG PFX "card did not respond on command!\n");
181                 }
182         }
183
184         if (got_response) {
185                 /* read back response */
186                 *lsb = inb_p(pcipcwd_private.io_addr + 4);
187                 *msb = inb_p(pcipcwd_private.io_addr + 5);
188
189                 /* clear WRSP bit */
190                 inb_p(pcipcwd_private.io_addr + 6);
191
192                 if (debug >= DEBUG)
193                         printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
194                                 cmd, *msb, *lsb);
195         }
196
197         spin_unlock(&pcipcwd_private.io_lock);
198
199         return got_response;
200 }
201
202 static inline void pcipcwd_check_temperature_support(void)
203 {
204         if (inb_p(pcipcwd_private.io_addr) != 0xF0)
205                 pcipcwd_private.supports_temp = 1;
206 }
207
208 static int pcipcwd_get_option_switches(void)
209 {
210         int option_switches;
211
212         option_switches = inb_p(pcipcwd_private.io_addr + 3);
213         return option_switches;
214 }
215
216 static void pcipcwd_show_card_info(void)
217 {
218         int got_fw_rev, fw_rev_major, fw_rev_minor;
219         char fw_ver_str[20];            /* The cards firmware version */
220         int option_switches;
221
222         got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
223         if (got_fw_rev) {
224                 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
225         } else {
226                 sprintf(fw_ver_str, "<card no answer>");
227         }
228
229         /* Get switch settings */
230         option_switches = pcipcwd_get_option_switches();
231
232         printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
233                 (int) pcipcwd_private.io_addr, fw_ver_str,
234                 (pcipcwd_private.supports_temp ? "with" : "without"));
235
236         printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
237                 option_switches,
238                 ((option_switches & 0x10) ? "ON" : "OFF"),
239                 ((option_switches & 0x08) ? "ON" : "OFF"));
240
241         if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
242                 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
243
244         if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
245                 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
246
247         if (pcipcwd_private.boot_status == 0)
248                 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
249 }
250
251 static int pcipcwd_start(void)
252 {
253         int stat_reg;
254
255         spin_lock(&pcipcwd_private.io_lock);
256         outb_p(0x00, pcipcwd_private.io_addr + 3);
257         udelay(1000);
258
259         stat_reg = inb_p(pcipcwd_private.io_addr + 2);
260         spin_unlock(&pcipcwd_private.io_lock);
261
262         if (stat_reg & WD_PCI_WDIS) {
263                 printk(KERN_ERR PFX "Card timer not enabled\n");
264                 return -1;
265         }
266
267         if (debug >= VERBOSE)
268                 printk(KERN_DEBUG PFX "Watchdog started\n");
269
270         return 0;
271 }
272
273 static int pcipcwd_stop(void)
274 {
275         int stat_reg;
276
277         spin_lock(&pcipcwd_private.io_lock);
278         outb_p(0xA5, pcipcwd_private.io_addr + 3);
279         udelay(1000);
280
281         outb_p(0xA5, pcipcwd_private.io_addr + 3);
282         udelay(1000);
283
284         stat_reg = inb_p(pcipcwd_private.io_addr + 2);
285         spin_unlock(&pcipcwd_private.io_lock);
286
287         if (!(stat_reg & WD_PCI_WDIS)) {
288                 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
289                 return -1;
290         }
291
292         if (debug >= VERBOSE)
293                 printk(KERN_DEBUG PFX "Watchdog stopped\n");
294
295         return 0;
296 }
297
298 static int pcipcwd_keepalive(void)
299 {
300         /* Re-trigger watchdog by writing to port 0 */
301         spin_lock(&pcipcwd_private.io_lock);
302         outb_p(0x42, pcipcwd_private.io_addr);  /* send out any data */
303         spin_unlock(&pcipcwd_private.io_lock);
304
305         if (debug >= DEBUG)
306                 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
307
308         return 0;
309 }
310
311 static int pcipcwd_set_heartbeat(int t)
312 {
313         int t_msb = t / 256;
314         int t_lsb = t % 256;
315
316         if ((t < 0x0001) || (t > 0xFFFF))
317                 return -EINVAL;
318
319         /* Write new heartbeat to watchdog */
320         send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
321
322         heartbeat = t;
323         if (debug >= VERBOSE)
324                 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
325                        heartbeat);
326
327         return 0;
328 }
329
330 static int pcipcwd_get_status(int *status)
331 {
332         int control_status;
333
334         *status=0;
335         control_status = inb_p(pcipcwd_private.io_addr + 1);
336         if (control_status & WD_PCI_WTRP)
337                 *status |= WDIOF_CARDRESET;
338         if (control_status & WD_PCI_TTRP) {
339                 *status |= WDIOF_OVERHEAT;
340                 if (temp_panic)
341                         panic(PFX "Temperature overheat trip!\n");
342         }
343
344         if (debug >= DEBUG)
345                 printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
346                        control_status);
347
348         return 0;
349 }
350
351 static int pcipcwd_clear_status(void)
352 {
353         int control_status;
354         int msb;
355         int reset_counter;
356
357         if (debug >= VERBOSE)
358                 printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
359
360         control_status = inb_p(pcipcwd_private.io_addr + 1);
361
362         if (debug >= DEBUG) {
363                 printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
364                 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
365                        (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
366         }
367
368         /* clear trip status & LED and keep mode of relay 2 */
369         outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP, pcipcwd_private.io_addr + 1);
370
371         /* clear reset counter */
372         msb=0;
373         reset_counter=0xff;
374         send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
375
376         if (debug >= DEBUG) {
377                 printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
378                        reset_counter);
379         }
380
381         return 0;
382 }
383
384 static int pcipcwd_get_temperature(int *temperature)
385 {
386         *temperature = 0;
387         if (!pcipcwd_private.supports_temp)
388                 return -ENODEV;
389
390         spin_lock(&pcipcwd_private.io_lock);
391         *temperature = inb_p(pcipcwd_private.io_addr);
392         spin_unlock(&pcipcwd_private.io_lock);
393
394         /*
395          * Convert celsius to fahrenheit, since this was
396          * the decided 'standard' for this return value.
397          */
398         *temperature = (*temperature * 9 / 5) + 32;
399
400         if (debug >= DEBUG) {
401                 printk(KERN_DEBUG PFX "temperature is: %d F\n",
402                        *temperature);
403         }
404
405         return 0;
406 }
407
408 static int pcipcwd_get_timeleft(int *time_left)
409 {
410         int msb;
411         int lsb;
412
413         /* Read the time that's left before rebooting */
414         /* Note: if the board is not yet armed then we will read 0xFFFF */
415         send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
416
417         *time_left = (msb << 8) + lsb;
418
419         if (debug >= VERBOSE)
420                 printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
421                        *time_left);
422
423         return 0;
424 }
425
426 /*
427  *      /dev/watchdog handling
428  */
429
430 static ssize_t pcipcwd_write(struct file *file, const char __user *data,
431                              size_t len, loff_t *ppos)
432 {
433         /* See if we got the magic character 'V' and reload the timer */
434         if (len) {
435                 if (!nowayout) {
436                         size_t i;
437
438                         /* note: just in case someone wrote the magic character
439                          * five months ago... */
440                         expect_release = 0;
441
442                         /* scan to see whether or not we got the magic character */
443                         for (i = 0; i != len; i++) {
444                                 char c;
445                                 if(get_user(c, data+i))
446                                         return -EFAULT;
447                                 if (c == 'V')
448                                         expect_release = 42;
449                         }
450                 }
451
452                 /* someone wrote to us, we should reload the timer */
453                 pcipcwd_keepalive();
454         }
455         return len;
456 }
457
458 static int pcipcwd_ioctl(struct inode *inode, struct file *file,
459                           unsigned int cmd, unsigned long arg)
460 {
461         void __user *argp = (void __user *)arg;
462         int __user *p = argp;
463         static struct watchdog_info ident = {
464                 .options =              WDIOF_OVERHEAT |
465                                         WDIOF_CARDRESET |
466                                         WDIOF_KEEPALIVEPING |
467                                         WDIOF_SETTIMEOUT |
468                                         WDIOF_MAGICCLOSE,
469                 .firmware_version =     1,
470                 .identity =             WATCHDOG_DRIVER_NAME,
471         };
472
473         switch (cmd) {
474                 case WDIOC_GETSUPPORT:
475                         return copy_to_user(argp, &ident,
476                                 sizeof (ident)) ? -EFAULT : 0;
477
478                 case WDIOC_GETSTATUS:
479                 {
480                         int status;
481
482                         pcipcwd_get_status(&status);
483
484                         return put_user(status, p);
485                 }
486
487                 case WDIOC_GETBOOTSTATUS:
488                         return put_user(pcipcwd_private.boot_status, p);
489
490                 case WDIOC_GETTEMP:
491                 {
492                         int temperature;
493
494                         if (pcipcwd_get_temperature(&temperature))
495                                 return -EFAULT;
496
497                         return put_user(temperature, p);
498                 }
499
500                 case WDIOC_KEEPALIVE:
501                         pcipcwd_keepalive();
502                         return 0;
503
504                 case WDIOC_SETOPTIONS:
505                 {
506                         int new_options, retval = -EINVAL;
507
508                         if (get_user (new_options, p))
509                                 return -EFAULT;
510
511                         if (new_options & WDIOS_DISABLECARD) {
512                                 if (pcipcwd_stop())
513                                         return -EIO;
514                                 retval = 0;
515                         }
516
517                         if (new_options & WDIOS_ENABLECARD) {
518                                 if (pcipcwd_start())
519                                         return -EIO;
520                                 retval = 0;
521                         }
522
523                         if (new_options & WDIOS_TEMPPANIC) {
524                                 temp_panic = 1;
525                                 retval = 0;
526                         }
527
528                         return retval;
529                 }
530
531                 case WDIOC_SETTIMEOUT:
532                 {
533                         int new_heartbeat;
534
535                         if (get_user(new_heartbeat, p))
536                                 return -EFAULT;
537
538                         if (pcipcwd_set_heartbeat(new_heartbeat))
539                             return -EINVAL;
540
541                         pcipcwd_keepalive();
542                         /* Fall */
543                 }
544
545                 case WDIOC_GETTIMEOUT:
546                         return put_user(heartbeat, p);
547
548                 case WDIOC_GETTIMELEFT:
549                 {
550                         int time_left;
551
552                         if (pcipcwd_get_timeleft(&time_left))
553                                 return -EFAULT;
554
555                         return put_user(time_left, p);
556                 }
557
558                 default:
559                         return -ENOTTY;
560         }
561 }
562
563 static int pcipcwd_open(struct inode *inode, struct file *file)
564 {
565         /* /dev/watchdog can only be opened once */
566         if (test_and_set_bit(0, &is_active)) {
567                 if (debug >= VERBOSE)
568                         printk(KERN_ERR PFX "Attempt to open already opened device.\n");
569                 return -EBUSY;
570         }
571
572         /* Activate */
573         pcipcwd_start();
574         pcipcwd_keepalive();
575         return nonseekable_open(inode, file);
576 }
577
578 static int pcipcwd_release(struct inode *inode, struct file *file)
579 {
580         /*
581          *      Shut off the timer.
582          */
583         if (expect_release == 42) {
584                 pcipcwd_stop();
585         } else {
586                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
587                 pcipcwd_keepalive();
588         }
589         expect_release = 0;
590         clear_bit(0, &is_active);
591         return 0;
592 }
593
594 /*
595  *      /dev/temperature handling
596  */
597
598 static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
599                                 size_t len, loff_t *ppos)
600 {
601         int temperature;
602
603         if (pcipcwd_get_temperature(&temperature))
604                 return -EFAULT;
605
606         if (copy_to_user (data, &temperature, 1))
607                 return -EFAULT;
608
609         return 1;
610 }
611
612 static int pcipcwd_temp_open(struct inode *inode, struct file *file)
613 {
614         if (!pcipcwd_private.supports_temp)
615                 return -ENODEV;
616
617         return nonseekable_open(inode, file);
618 }
619
620 static int pcipcwd_temp_release(struct inode *inode, struct file *file)
621 {
622         return 0;
623 }
624
625 /*
626  *      Notify system
627  */
628
629 static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
630 {
631         if (code==SYS_DOWN || code==SYS_HALT) {
632                 /* Turn the WDT off */
633                 pcipcwd_stop();
634         }
635
636         return NOTIFY_DONE;
637 }
638
639 /*
640  *      Kernel Interfaces
641  */
642
643 static const struct file_operations pcipcwd_fops = {
644         .owner =        THIS_MODULE,
645         .llseek =       no_llseek,
646         .write =        pcipcwd_write,
647         .ioctl =        pcipcwd_ioctl,
648         .open =         pcipcwd_open,
649         .release =      pcipcwd_release,
650 };
651
652 static struct miscdevice pcipcwd_miscdev = {
653         .minor =        WATCHDOG_MINOR,
654         .name =         "watchdog",
655         .fops =         &pcipcwd_fops,
656 };
657
658 static const struct file_operations pcipcwd_temp_fops = {
659         .owner =        THIS_MODULE,
660         .llseek =       no_llseek,
661         .read =         pcipcwd_temp_read,
662         .open =         pcipcwd_temp_open,
663         .release =      pcipcwd_temp_release,
664 };
665
666 static struct miscdevice pcipcwd_temp_miscdev = {
667         .minor =        TEMP_MINOR,
668         .name =         "temperature",
669         .fops =         &pcipcwd_temp_fops,
670 };
671
672 static struct notifier_block pcipcwd_notifier = {
673         .notifier_call =        pcipcwd_notify_sys,
674 };
675
676 /*
677  *      Init & exit routines
678  */
679
680 static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
681                 const struct pci_device_id *ent)
682 {
683         int ret = -EIO;
684
685         cards_found++;
686         if (cards_found == 1)
687                 printk(KERN_INFO PFX DRIVER_VERSION);
688
689         if (cards_found > 1) {
690                 printk(KERN_ERR PFX "This driver only supports 1 device\n");
691                 return -ENODEV;
692         }
693
694         if (pci_enable_device(pdev)) {
695                 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
696                 return -ENODEV;
697         }
698
699         if (pci_resource_start(pdev, 0) == 0x0000) {
700                 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
701                 ret = -ENODEV;
702                 goto err_out_disable_device;
703         }
704
705         pcipcwd_private.pdev = pdev;
706         pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
707
708         if (pci_request_regions(pdev, WATCHDOG_NAME)) {
709                 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
710                         (int) pcipcwd_private.io_addr);
711                 ret = -EIO;
712                 goto err_out_disable_device;
713         }
714
715         /* get the boot_status */
716         pcipcwd_get_status(&pcipcwd_private.boot_status);
717
718         /* clear the "card caused reboot" flag */
719         pcipcwd_clear_status();
720
721         /* disable card */
722         pcipcwd_stop();
723
724         /* Check whether or not the card supports the temperature device */
725         pcipcwd_check_temperature_support();
726
727         /* Show info about the card itself */
728         pcipcwd_show_card_info();
729
730         /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
731         if (heartbeat == 0)
732                 heartbeat = heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
733
734         /* Check that the heartbeat value is within it's range ; if not reset to the default */
735         if (pcipcwd_set_heartbeat(heartbeat)) {
736                 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
737                 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
738                         WATCHDOG_HEARTBEAT);
739         }
740
741         ret = register_reboot_notifier(&pcipcwd_notifier);
742         if (ret != 0) {
743                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
744                         ret);
745                 goto err_out_release_region;
746         }
747
748         if (pcipcwd_private.supports_temp) {
749                 ret = misc_register(&pcipcwd_temp_miscdev);
750                 if (ret != 0) {
751                         printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
752                                 TEMP_MINOR, ret);
753                         goto err_out_unregister_reboot;
754                 }
755         }
756
757         ret = misc_register(&pcipcwd_miscdev);
758         if (ret != 0) {
759                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
760                         WATCHDOG_MINOR, ret);
761                 goto err_out_misc_deregister;
762         }
763
764         printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
765                 heartbeat, nowayout);
766
767         return 0;
768
769 err_out_misc_deregister:
770         if (pcipcwd_private.supports_temp)
771                 misc_deregister(&pcipcwd_temp_miscdev);
772 err_out_unregister_reboot:
773         unregister_reboot_notifier(&pcipcwd_notifier);
774 err_out_release_region:
775         pci_release_regions(pdev);
776 err_out_disable_device:
777         pci_disable_device(pdev);
778         return ret;
779 }
780
781 static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
782 {
783         /* Stop the timer before we leave */
784         if (!nowayout)
785                 pcipcwd_stop();
786
787         /* Deregister */
788         misc_deregister(&pcipcwd_miscdev);
789         if (pcipcwd_private.supports_temp)
790                 misc_deregister(&pcipcwd_temp_miscdev);
791         unregister_reboot_notifier(&pcipcwd_notifier);
792         pci_release_regions(pdev);
793         pci_disable_device(pdev);
794         cards_found--;
795 }
796
797 static struct pci_device_id pcipcwd_pci_tbl[] = {
798         { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
799                 PCI_ANY_ID, PCI_ANY_ID, },
800         { 0 },                  /* End of list */
801 };
802 MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
803
804 static struct pci_driver pcipcwd_driver = {
805         .name           = WATCHDOG_NAME,
806         .id_table       = pcipcwd_pci_tbl,
807         .probe          = pcipcwd_card_init,
808         .remove         = __devexit_p(pcipcwd_card_exit),
809 };
810
811 static int __init pcipcwd_init_module(void)
812 {
813         spin_lock_init(&pcipcwd_private.io_lock);
814
815         return pci_register_driver(&pcipcwd_driver);
816 }
817
818 static void __exit pcipcwd_cleanup_module(void)
819 {
820         pci_unregister_driver(&pcipcwd_driver);
821
822         printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
823 }
824
825 module_init(pcipcwd_init_module);
826 module_exit(pcipcwd_cleanup_module);
827
828 MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
829 MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
830 MODULE_LICENSE("GPL");
831 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
832 MODULE_ALIAS_MISCDEV(TEMP_MINOR);