blob: 5308e5c8f29af139867d36436ccb32314d05cde6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Berkshire PCI-PC Watchdog Card Driver
3 *
4 * (c) Copyright 2003 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 http://www.pcwd.de/
25 * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
26 */
27
28/*
29 * Includes, defines, variables, module parameters, ...
30 */
31
Wim Van Sebroeckc315b7e2005-09-12 00:21:19 +020032#include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
33#include <linux/module.h> /* For module specific items */
34#include <linux/moduleparam.h> /* For new moduleparam's */
35#include <linux/types.h> /* For standard types (like size_t) */
36#include <linux/errno.h> /* For the -ENODEV/... values */
37#include <linux/kernel.h> /* For printk/panic/... */
38#include <linux/delay.h> /* For mdelay function */
39#include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
40#include <linux/watchdog.h> /* For the watchdog specific items */
41#include <linux/notifier.h> /* For notifier support */
42#include <linux/reboot.h> /* For reboot_notifier stuff */
43#include <linux/init.h> /* For __init/__exit/... */
44#include <linux/fs.h> /* For file operations */
45#include <linux/pci.h> /* For pci functions */
46#include <linux/ioport.h> /* For io-port access */
47#include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Wim Van Sebroeckc315b7e2005-09-12 00:21:19 +020049#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
50#include <asm/io.h> /* For inb/outb/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Module and version information */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020053#define WATCHDOG_VERSION "1.02"
54#define WATCHDOG_DATE "03 Sep 2005"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
56#define WATCHDOG_NAME "pcwd_pci"
57#define PFX WATCHDOG_NAME ": "
58#define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
59
60/* Stuff for the PCI ID's */
61#ifndef PCI_VENDOR_ID_QUICKLOGIC
62#define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
63#endif
64
65#ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
66#define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
67#endif
68
69/*
70 * These are the defines that describe the control status bits for the
71 * PCI-PC Watchdog card.
72 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020073/* Port 1 : Control Status #1 */
74#define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
75#define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
76#define WD_PCI_TTRP 0x04 /* Temperature Trip status */
77#define WD_PCI_RL2A 0x08 /* Relay 2 Active */
78#define WD_PCI_RL1A 0x10 /* Relay 1 Active */
79#define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip/reset */
80#define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
81/* Port 2 : Control Status #2 */
82#define WD_PCI_WDIS 0x10 /* Watchdog Disable */
83#define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
84#define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
85#define WD_PCI_PCMD 0x80 /* PC has sent command */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* according to documentation max. time to process a command for the pci
88 * watchdog card is 100 ms, so we give it 150 ms to do it's job */
89#define PCI_COMMAND_TIMEOUT 150
90
91/* Watchdog's internal commands */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020092#define CMD_GET_STATUS 0x04
93#define CMD_GET_FIRMWARE_VERSION 0x08
94#define CMD_READ_WATCHDOG_TIMEOUT 0x18
95#define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
96#define CMD_GET_CLEAR_RESET_COUNT 0x84
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* We can only use 1 card due to the /dev/watchdog restriction */
99static int cards_found;
100
101/* internal variables */
102static int temp_panic;
103static unsigned long is_active;
104static char expect_release;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200105static struct { /* this is private data for each PCI-PC watchdog card */
106 int supports_temp; /* Wether or not the card has a temperature device */
107 int boot_status; /* The card's boot status */
108 unsigned long io_addr; /* The cards I/O address */
109 spinlock_t io_lock; /* the lock for io operations */
110 struct pci_dev *pdev; /* the PCI-device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111} pcipcwd_private;
112
113/* module parameters */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200114#define QUIET 0 /* Default */
115#define VERBOSE 1 /* Verbose */
116#define DEBUG 2 /* print fancy stuff too */
117static int debug = QUIET;
118module_param(debug, int, 0);
119MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#define WATCHDOG_HEARTBEAT 2 /* 2 sec default heartbeat */
122static int heartbeat = WATCHDOG_HEARTBEAT;
123module_param(heartbeat, int, 0);
124MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
125
Andrey Panin4bfdf372005-07-27 11:43:58 -0700126static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127module_param(nowayout, int, 0);
128MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
129
130/*
131 * Internal functions
132 */
133
134static int send_command(int cmd, int *msb, int *lsb)
135{
136 int got_response, count;
137
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200138 if (debug >= DEBUG)
139 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
140 cmd, *msb, *lsb);
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 spin_lock(&pcipcwd_private.io_lock);
143 /* If a command requires data it should be written first.
144 * Data for commands with 8 bits of data should be written to port 4.
145 * Commands with 16 bits of data, should be written as LSB to port 4
146 * and MSB to port 5.
147 * After the required data has been written then write the command to
148 * port 6. */
149 outb_p(*lsb, pcipcwd_private.io_addr + 4);
150 outb_p(*msb, pcipcwd_private.io_addr + 5);
151 outb_p(cmd, pcipcwd_private.io_addr + 6);
152
153 /* wait till the pci card processed the command, signaled by
154 * the WRSP bit in port 2 and give it a max. timeout of
155 * PCI_COMMAND_TIMEOUT to process */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200156 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
158 mdelay(1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200159 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200162 if (debug >= DEBUG) {
163 if (got_response) {
164 printk(KERN_DEBUG PFX "time to process command was: %d ms\n",
165 count);
166 } else {
167 printk(KERN_DEBUG PFX "card did not respond on command!\n");
168 }
169 }
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (got_response) {
172 /* read back response */
173 *lsb = inb_p(pcipcwd_private.io_addr + 4);
174 *msb = inb_p(pcipcwd_private.io_addr + 5);
175
176 /* clear WRSP bit */
177 inb_p(pcipcwd_private.io_addr + 6);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200178
179 if (debug >= DEBUG)
180 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
181 cmd, *msb, *lsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 spin_unlock(&pcipcwd_private.io_lock);
185
186 return got_response;
187}
188
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200189static inline void pcipcwd_check_temperature_support(void)
190{
191 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
192 pcipcwd_private.supports_temp = 1;
193}
194
195static int pcipcwd_get_option_switches(void)
196{
197 int option_switches;
198
199 option_switches = inb_p(pcipcwd_private.io_addr + 3);
200 return option_switches;
201}
202
203static void pcipcwd_show_card_info(void)
204{
205 int got_fw_rev, fw_rev_major, fw_rev_minor;
206 char fw_ver_str[20]; /* The cards firmware version */
207 int option_switches;
208
209 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
210 if (got_fw_rev) {
211 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
212 } else {
213 sprintf(fw_ver_str, "<card no answer>");
214 }
215
216 /* Get switch settings */
217 option_switches = pcipcwd_get_option_switches();
218
219 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
220 (int) pcipcwd_private.io_addr, fw_ver_str,
221 (pcipcwd_private.supports_temp ? "with" : "without"));
222
223 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
224 option_switches,
225 ((option_switches & 0x10) ? "ON" : "OFF"),
226 ((option_switches & 0x08) ? "ON" : "OFF"));
227
228 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
229 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
230
231 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
232 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
233
234 if (pcipcwd_private.boot_status == 0)
235 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int pcipcwd_start(void)
239{
240 int stat_reg;
241
242 spin_lock(&pcipcwd_private.io_lock);
243 outb_p(0x00, pcipcwd_private.io_addr + 3);
244 udelay(1000);
245
246 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
247 spin_unlock(&pcipcwd_private.io_lock);
248
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200249 if (stat_reg & WD_PCI_WDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 printk(KERN_ERR PFX "Card timer not enabled\n");
251 return -1;
252 }
253
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200254 if (debug >= VERBOSE)
255 printk(KERN_DEBUG PFX "Watchdog started\n");
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258}
259
260static int pcipcwd_stop(void)
261{
262 int stat_reg;
263
264 spin_lock(&pcipcwd_private.io_lock);
265 outb_p(0xA5, pcipcwd_private.io_addr + 3);
266 udelay(1000);
267
268 outb_p(0xA5, pcipcwd_private.io_addr + 3);
269 udelay(1000);
270
271 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
272 spin_unlock(&pcipcwd_private.io_lock);
273
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200274 if (!(stat_reg & WD_PCI_WDIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
276 return -1;
277 }
278
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200279 if (debug >= VERBOSE)
280 printk(KERN_DEBUG PFX "Watchdog stopped\n");
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
285static int pcipcwd_keepalive(void)
286{
287 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200288 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200289
290 if (debug >= DEBUG)
291 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296static int pcipcwd_set_heartbeat(int t)
297{
298 int t_msb = t / 256;
299 int t_lsb = t % 256;
300
301 if ((t < 0x0001) || (t > 0xFFFF))
302 return -EINVAL;
303
304 /* Write new heartbeat to watchdog */
305 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
306
307 heartbeat = t;
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200308 if (debug >= VERBOSE)
309 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
310 heartbeat);
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313}
314
315static int pcipcwd_get_status(int *status)
316{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200317 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 *status=0;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200320 control_status = inb_p(pcipcwd_private.io_addr + 1);
321 if (control_status & WD_PCI_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *status |= WDIOF_CARDRESET;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200323 if (control_status & WD_PCI_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *status |= WDIOF_OVERHEAT;
325 if (temp_panic)
326 panic(PFX "Temperature overheat trip!\n");
327 }
328
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200329 if (debug >= DEBUG)
330 printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
331 control_status);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return 0;
334}
335
336static int pcipcwd_clear_status(void)
337{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200338 int control_status;
339 int msb;
340 int reset_counter;
341
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200342 if (debug >= VERBOSE)
343 printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
344
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200345 control_status = inb_p(pcipcwd_private.io_addr + 1);
346
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200347 if (debug >= DEBUG) {
348 printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
349 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
350 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
351 }
352
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200353 /* clear trip status & LED and keep mode of relay 2 */
354 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP, pcipcwd_private.io_addr + 1);
355
356 /* clear reset counter */
357 msb=0;
358 reset_counter=0xff;
359 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
360
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200361 if (debug >= DEBUG) {
362 printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
363 reset_counter);
364 }
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367}
368
369static int pcipcwd_get_temperature(int *temperature)
370{
371 *temperature = 0;
372 if (!pcipcwd_private.supports_temp)
373 return -ENODEV;
374
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200375 *temperature = inb_p(pcipcwd_private.io_addr);
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Convert celsius to fahrenheit, since this was
379 * the decided 'standard' for this return value.
380 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200381 *temperature = (*temperature * 9 / 5) + 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200383 if (debug >= DEBUG) {
384 printk(KERN_DEBUG PFX "temperature is: %d F\n",
385 *temperature);
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
391/*
392 * /dev/watchdog handling
393 */
394
395static ssize_t pcipcwd_write(struct file *file, const char __user *data,
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200396 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 /* See if we got the magic character 'V' and reload the timer */
399 if (len) {
400 if (!nowayout) {
401 size_t i;
402
403 /* note: just in case someone wrote the magic character
404 * five months ago... */
405 expect_release = 0;
406
407 /* scan to see whether or not we got the magic character */
408 for (i = 0; i != len; i++) {
409 char c;
410 if(get_user(c, data+i))
411 return -EFAULT;
412 if (c == 'V')
413 expect_release = 42;
414 }
415 }
416
417 /* someone wrote to us, we should reload the timer */
418 pcipcwd_keepalive();
419 }
420 return len;
421}
422
423static int pcipcwd_ioctl(struct inode *inode, struct file *file,
424 unsigned int cmd, unsigned long arg)
425{
426 void __user *argp = (void __user *)arg;
427 int __user *p = argp;
428 static struct watchdog_info ident = {
429 .options = WDIOF_OVERHEAT |
430 WDIOF_CARDRESET |
431 WDIOF_KEEPALIVEPING |
432 WDIOF_SETTIMEOUT |
433 WDIOF_MAGICCLOSE,
434 .firmware_version = 1,
435 .identity = WATCHDOG_DRIVER_NAME,
436 };
437
438 switch (cmd) {
439 case WDIOC_GETSUPPORT:
440 return copy_to_user(argp, &ident,
441 sizeof (ident)) ? -EFAULT : 0;
442
443 case WDIOC_GETSTATUS:
444 {
445 int status;
446
447 pcipcwd_get_status(&status);
448
449 return put_user(status, p);
450 }
451
452 case WDIOC_GETBOOTSTATUS:
453 return put_user(pcipcwd_private.boot_status, p);
454
455 case WDIOC_GETTEMP:
456 {
457 int temperature;
458
459 if (pcipcwd_get_temperature(&temperature))
460 return -EFAULT;
461
462 return put_user(temperature, p);
463 }
464
465 case WDIOC_KEEPALIVE:
466 pcipcwd_keepalive();
467 return 0;
468
469 case WDIOC_SETOPTIONS:
470 {
471 int new_options, retval = -EINVAL;
472
473 if (get_user (new_options, p))
474 return -EFAULT;
475
476 if (new_options & WDIOS_DISABLECARD) {
Wim Van Sebroeckc315b7e2005-09-12 00:21:19 +0200477 if (pcipcwd_stop())
478 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 retval = 0;
480 }
481
482 if (new_options & WDIOS_ENABLECARD) {
Wim Van Sebroeckc315b7e2005-09-12 00:21:19 +0200483 if (pcipcwd_start())
484 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 retval = 0;
486 }
487
488 if (new_options & WDIOS_TEMPPANIC) {
489 temp_panic = 1;
490 retval = 0;
491 }
492
493 return retval;
494 }
495
496 case WDIOC_SETTIMEOUT:
497 {
498 int new_heartbeat;
499
500 if (get_user(new_heartbeat, p))
501 return -EFAULT;
502
503 if (pcipcwd_set_heartbeat(new_heartbeat))
504 return -EINVAL;
505
506 pcipcwd_keepalive();
507 /* Fall */
508 }
509
510 case WDIOC_GETTIMEOUT:
511 return put_user(heartbeat, p);
512
513 default:
514 return -ENOIOCTLCMD;
515 }
516}
517
518static int pcipcwd_open(struct inode *inode, struct file *file)
519{
520 /* /dev/watchdog can only be opened once */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200521 if (test_and_set_bit(0, &is_active)) {
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200522 if (debug >= VERBOSE)
523 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EBUSY;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 /* Activate */
528 pcipcwd_start();
529 pcipcwd_keepalive();
530 return nonseekable_open(inode, file);
531}
532
533static int pcipcwd_release(struct inode *inode, struct file *file)
534{
535 /*
536 * Shut off the timer.
537 */
538 if (expect_release == 42) {
539 pcipcwd_stop();
540 } else {
541 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
542 pcipcwd_keepalive();
543 }
544 expect_release = 0;
545 clear_bit(0, &is_active);
546 return 0;
547}
548
549/*
550 * /dev/temperature handling
551 */
552
553static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
554 size_t len, loff_t *ppos)
555{
556 int temperature;
557
558 if (pcipcwd_get_temperature(&temperature))
559 return -EFAULT;
560
561 if (copy_to_user (data, &temperature, 1))
562 return -EFAULT;
563
564 return 1;
565}
566
567static int pcipcwd_temp_open(struct inode *inode, struct file *file)
568{
569 if (!pcipcwd_private.supports_temp)
570 return -ENODEV;
571
572 return nonseekable_open(inode, file);
573}
574
575static int pcipcwd_temp_release(struct inode *inode, struct file *file)
576{
577 return 0;
578}
579
580/*
581 * Notify system
582 */
583
584static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
585{
586 if (code==SYS_DOWN || code==SYS_HALT) {
587 /* Turn the WDT off */
588 pcipcwd_stop();
589 }
590
591 return NOTIFY_DONE;
592}
593
594/*
595 * Kernel Interfaces
596 */
597
598static struct file_operations pcipcwd_fops = {
599 .owner = THIS_MODULE,
600 .llseek = no_llseek,
601 .write = pcipcwd_write,
602 .ioctl = pcipcwd_ioctl,
603 .open = pcipcwd_open,
604 .release = pcipcwd_release,
605};
606
607static struct miscdevice pcipcwd_miscdev = {
608 .minor = WATCHDOG_MINOR,
609 .name = "watchdog",
610 .fops = &pcipcwd_fops,
611};
612
613static struct file_operations pcipcwd_temp_fops = {
614 .owner = THIS_MODULE,
615 .llseek = no_llseek,
616 .read = pcipcwd_temp_read,
617 .open = pcipcwd_temp_open,
618 .release = pcipcwd_temp_release,
619};
620
621static struct miscdevice pcipcwd_temp_miscdev = {
622 .minor = TEMP_MINOR,
623 .name = "temperature",
624 .fops = &pcipcwd_temp_fops,
625};
626
627static struct notifier_block pcipcwd_notifier = {
628 .notifier_call = pcipcwd_notify_sys,
629};
630
631/*
632 * Init & exit routines
633 */
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
636 const struct pci_device_id *ent)
637{
638 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 cards_found++;
641 if (cards_found == 1)
642 printk(KERN_INFO PFX DRIVER_VERSION);
643
644 if (cards_found > 1) {
645 printk(KERN_ERR PFX "This driver only supports 1 device\n");
646 return -ENODEV;
647 }
648
649 if (pci_enable_device(pdev)) {
650 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
651 return -ENODEV;
652 }
653
654 if (pci_resource_start(pdev, 0) == 0x0000) {
655 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
656 ret = -ENODEV;
657 goto err_out_disable_device;
658 }
659
660 pcipcwd_private.pdev = pdev;
661 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
662
663 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
664 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
665 (int) pcipcwd_private.io_addr);
666 ret = -EIO;
667 goto err_out_disable_device;
668 }
669
670 /* get the boot_status */
671 pcipcwd_get_status(&pcipcwd_private.boot_status);
672
673 /* clear the "card caused reboot" flag */
674 pcipcwd_clear_status();
675
676 /* disable card */
677 pcipcwd_stop();
678
679 /* Check whether or not the card supports the temperature device */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200680 pcipcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200682 /* Show info about the card itself */
683 pcipcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /* Check that the heartbeat value is within it's range ; if not reset to the default */
686 if (pcipcwd_set_heartbeat(heartbeat)) {
687 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
688 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
689 WATCHDOG_HEARTBEAT);
690 }
691
692 ret = register_reboot_notifier(&pcipcwd_notifier);
693 if (ret != 0) {
694 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
695 ret);
696 goto err_out_release_region;
697 }
698
699 if (pcipcwd_private.supports_temp) {
700 ret = misc_register(&pcipcwd_temp_miscdev);
701 if (ret != 0) {
702 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
703 TEMP_MINOR, ret);
704 goto err_out_unregister_reboot;
705 }
706 }
707
708 ret = misc_register(&pcipcwd_miscdev);
709 if (ret != 0) {
710 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
711 WATCHDOG_MINOR, ret);
712 goto err_out_misc_deregister;
713 }
714
715 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
716 heartbeat, nowayout);
717
718 return 0;
719
720err_out_misc_deregister:
721 if (pcipcwd_private.supports_temp)
722 misc_deregister(&pcipcwd_temp_miscdev);
723err_out_unregister_reboot:
724 unregister_reboot_notifier(&pcipcwd_notifier);
725err_out_release_region:
726 pci_release_regions(pdev);
727err_out_disable_device:
728 pci_disable_device(pdev);
729 return ret;
730}
731
732static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
733{
734 /* Stop the timer before we leave */
735 if (!nowayout)
736 pcipcwd_stop();
737
738 /* Deregister */
739 misc_deregister(&pcipcwd_miscdev);
740 if (pcipcwd_private.supports_temp)
741 misc_deregister(&pcipcwd_temp_miscdev);
742 unregister_reboot_notifier(&pcipcwd_notifier);
743 pci_release_regions(pdev);
744 pci_disable_device(pdev);
745 cards_found--;
746}
747
748static struct pci_device_id pcipcwd_pci_tbl[] = {
749 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
750 PCI_ANY_ID, PCI_ANY_ID, },
751 { 0 }, /* End of list */
752};
753MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
754
755static struct pci_driver pcipcwd_driver = {
Laurent Riffarded8b39d2005-10-30 15:03:09 -0800756 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 .name = WATCHDOG_NAME,
758 .id_table = pcipcwd_pci_tbl,
759 .probe = pcipcwd_card_init,
760 .remove = __devexit_p(pcipcwd_card_exit),
761};
762
763static int __init pcipcwd_init_module(void)
764{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200765 spin_lock_init(&pcipcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 return pci_register_driver(&pcipcwd_driver);
768}
769
770static void __exit pcipcwd_cleanup_module(void)
771{
772 pci_unregister_driver(&pcipcwd_driver);
773}
774
775module_init(pcipcwd_init_module);
776module_exit(pcipcwd_cleanup_module);
777
778MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
779MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
780MODULE_LICENSE("GPL");
781MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
782MODULE_ALIAS_MISCDEV(TEMP_MINOR);