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