blob: 059ac9f12de7dcb26ebe6069fa422bb96c37794f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Berkshire PCI-PC Watchdog Card Driver
3 *
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +01004 * (c) Copyright 2003-2007 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 Sebroeck58b519f2006-05-21 12:48:44 +020024 * A bells and whistles driver is available from:
Wim Van Sebroeckb3faed62005-10-22 16:27:19 +020025 * 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 Sebroeckc315b7e82005-09-12 00:21:19 +020034#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/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Wim Van Sebroeckc315b7e82005-09-12 00:21:19 +020050#include <asm/uaccess.h> /* For copy_to_user/put_user/... */
51#include <asm/io.h> /* For inb/outb/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* Module and version information */
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010054#define WATCHDOG_VERSION "1.03"
55#define WATCHDOG_DATE "06 Jan 2007"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#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 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020074/* 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
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 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +020093#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010099/* Watchdog's Dip Switch heartbeat values */
100static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* We can only use 1 card due to the /dev/watchdog restriction */
112static int cards_found;
113
114/* internal variables */
115static int temp_panic;
116static unsigned long is_active;
117static char expect_release;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200118static 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124} pcipcwd_private;
125
126/* module parameters */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200127#define QUIET 0 /* Default */
128#define VERBOSE 1 /* Verbose */
129#define DEBUG 2 /* print fancy stuff too */
130static int debug = QUIET;
131module_param(debug, int, 0);
132MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
133
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100134#define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static int heartbeat = WATCHDOG_HEARTBEAT;
136module_param(heartbeat, int, 0);
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100137MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (0<heartbeat<65536 or 0=delay-time from dip-switches, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Andrey Panin4bfdf372005-07-27 11:43:58 -0700139static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140module_param(nowayout, int, 0);
141MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
142
143/*
144 * Internal functions
145 */
146
147static int send_command(int cmd, int *msb, int *lsb)
148{
149 int got_response, count;
150
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200151 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 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 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200169 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
171 mdelay(1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200172 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200175 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 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);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200191
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_unlock(&pcipcwd_private.io_lock);
198
199 return got_response;
200}
201
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200202static 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
208static 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
216static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static 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
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200262 if (stat_reg & WD_PCI_WDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 printk(KERN_ERR PFX "Card timer not enabled\n");
264 return -1;
265 }
266
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200267 if (debug >= VERBOSE)
268 printk(KERN_DEBUG PFX "Watchdog started\n");
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271}
272
273static 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
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200287 if (!(stat_reg & WD_PCI_WDIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
289 return -1;
290 }
291
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200292 if (debug >= VERBOSE)
293 printk(KERN_DEBUG PFX "Watchdog stopped\n");
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296}
297
298static int pcipcwd_keepalive(void)
299{
300 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200301 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200302
303 if (debug >= DEBUG)
304 printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307}
308
309static int pcipcwd_set_heartbeat(int t)
310{
311 int t_msb = t / 256;
312 int t_lsb = t % 256;
313
314 if ((t < 0x0001) || (t > 0xFFFF))
315 return -EINVAL;
316
317 /* Write new heartbeat to watchdog */
318 send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
319
320 heartbeat = t;
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200321 if (debug >= VERBOSE)
322 printk(KERN_DEBUG PFX "New heartbeat: %d\n",
323 heartbeat);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
328static int pcipcwd_get_status(int *status)
329{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200330 int control_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 *status=0;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200333 control_status = inb_p(pcipcwd_private.io_addr + 1);
334 if (control_status & WD_PCI_WTRP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 *status |= WDIOF_CARDRESET;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200336 if (control_status & WD_PCI_TTRP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 *status |= WDIOF_OVERHEAT;
338 if (temp_panic)
339 panic(PFX "Temperature overheat trip!\n");
340 }
341
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200342 if (debug >= DEBUG)
343 printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
344 control_status);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
349static int pcipcwd_clear_status(void)
350{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200351 int control_status;
352 int msb;
353 int reset_counter;
354
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200355 if (debug >= VERBOSE)
356 printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
357
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200358 control_status = inb_p(pcipcwd_private.io_addr + 1);
359
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200360 if (debug >= DEBUG) {
361 printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
362 printk(KERN_DEBUG PFX "sending: 0x%02x\n",
363 (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
364 }
365
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200366 /* clear trip status & LED and keep mode of relay 2 */
367 outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP, pcipcwd_private.io_addr + 1);
368
369 /* clear reset counter */
370 msb=0;
371 reset_counter=0xff;
372 send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
373
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200374 if (debug >= DEBUG) {
375 printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
376 reset_counter);
377 }
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return 0;
380}
381
382static int pcipcwd_get_temperature(int *temperature)
383{
384 *temperature = 0;
385 if (!pcipcwd_private.supports_temp)
386 return -ENODEV;
387
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200388 *temperature = inb_p(pcipcwd_private.io_addr);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /*
391 * Convert celsius to fahrenheit, since this was
392 * the decided 'standard' for this return value.
393 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200394 *temperature = (*temperature * 9 / 5) + 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200396 if (debug >= DEBUG) {
397 printk(KERN_DEBUG PFX "temperature is: %d F\n",
398 *temperature);
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
402}
403
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200404static int pcipcwd_get_timeleft(int *time_left)
405{
406 int msb;
407 int lsb;
408
409 /* Read the time that's left before rebooting */
410 /* Note: if the board is not yet armed then we will read 0xFFFF */
411 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
412
413 *time_left = (msb << 8) + lsb;
414
415 if (debug >= VERBOSE)
416 printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
417 *time_left);
418
419 return 0;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * /dev/watchdog handling
424 */
425
426static ssize_t pcipcwd_write(struct file *file, const char __user *data,
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200427 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 /* See if we got the magic character 'V' and reload the timer */
430 if (len) {
431 if (!nowayout) {
432 size_t i;
433
434 /* note: just in case someone wrote the magic character
435 * five months ago... */
436 expect_release = 0;
437
438 /* scan to see whether or not we got the magic character */
439 for (i = 0; i != len; i++) {
440 char c;
441 if(get_user(c, data+i))
442 return -EFAULT;
443 if (c == 'V')
444 expect_release = 42;
445 }
446 }
447
448 /* someone wrote to us, we should reload the timer */
449 pcipcwd_keepalive();
450 }
451 return len;
452}
453
454static int pcipcwd_ioctl(struct inode *inode, struct file *file,
455 unsigned int cmd, unsigned long arg)
456{
457 void __user *argp = (void __user *)arg;
458 int __user *p = argp;
459 static struct watchdog_info ident = {
460 .options = WDIOF_OVERHEAT |
461 WDIOF_CARDRESET |
462 WDIOF_KEEPALIVEPING |
463 WDIOF_SETTIMEOUT |
464 WDIOF_MAGICCLOSE,
465 .firmware_version = 1,
466 .identity = WATCHDOG_DRIVER_NAME,
467 };
468
469 switch (cmd) {
470 case WDIOC_GETSUPPORT:
471 return copy_to_user(argp, &ident,
472 sizeof (ident)) ? -EFAULT : 0;
473
474 case WDIOC_GETSTATUS:
475 {
476 int status;
477
478 pcipcwd_get_status(&status);
479
480 return put_user(status, p);
481 }
482
483 case WDIOC_GETBOOTSTATUS:
484 return put_user(pcipcwd_private.boot_status, p);
485
486 case WDIOC_GETTEMP:
487 {
488 int temperature;
489
490 if (pcipcwd_get_temperature(&temperature))
491 return -EFAULT;
492
493 return put_user(temperature, p);
494 }
495
496 case WDIOC_KEEPALIVE:
497 pcipcwd_keepalive();
498 return 0;
499
500 case WDIOC_SETOPTIONS:
501 {
502 int new_options, retval = -EINVAL;
503
504 if (get_user (new_options, p))
505 return -EFAULT;
506
507 if (new_options & WDIOS_DISABLECARD) {
Wim Van Sebroeckc315b7e82005-09-12 00:21:19 +0200508 if (pcipcwd_stop())
509 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 retval = 0;
511 }
512
513 if (new_options & WDIOS_ENABLECARD) {
Wim Van Sebroeckc315b7e82005-09-12 00:21:19 +0200514 if (pcipcwd_start())
515 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 retval = 0;
517 }
518
519 if (new_options & WDIOS_TEMPPANIC) {
520 temp_panic = 1;
521 retval = 0;
522 }
523
524 return retval;
525 }
526
527 case WDIOC_SETTIMEOUT:
528 {
529 int new_heartbeat;
530
531 if (get_user(new_heartbeat, p))
532 return -EFAULT;
533
534 if (pcipcwd_set_heartbeat(new_heartbeat))
535 return -EINVAL;
536
537 pcipcwd_keepalive();
538 /* Fall */
539 }
540
541 case WDIOC_GETTIMEOUT:
542 return put_user(heartbeat, p);
543
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200544 case WDIOC_GETTIMELEFT:
545 {
546 int time_left;
547
548 if (pcipcwd_get_timeleft(&time_left))
549 return -EFAULT;
550
551 return put_user(time_left, p);
552 }
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 default:
Samuel Tardieu795b89d2006-09-09 17:34:31 +0200555 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557}
558
559static int pcipcwd_open(struct inode *inode, struct file *file)
560{
561 /* /dev/watchdog can only be opened once */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200562 if (test_and_set_bit(0, &is_active)) {
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200563 if (debug >= VERBOSE)
564 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -EBUSY;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /* Activate */
569 pcipcwd_start();
570 pcipcwd_keepalive();
571 return nonseekable_open(inode, file);
572}
573
574static int pcipcwd_release(struct inode *inode, struct file *file)
575{
576 /*
577 * Shut off the timer.
578 */
579 if (expect_release == 42) {
580 pcipcwd_stop();
581 } else {
582 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
583 pcipcwd_keepalive();
584 }
585 expect_release = 0;
586 clear_bit(0, &is_active);
587 return 0;
588}
589
590/*
591 * /dev/temperature handling
592 */
593
594static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
595 size_t len, loff_t *ppos)
596{
597 int temperature;
598
599 if (pcipcwd_get_temperature(&temperature))
600 return -EFAULT;
601
602 if (copy_to_user (data, &temperature, 1))
603 return -EFAULT;
604
605 return 1;
606}
607
608static int pcipcwd_temp_open(struct inode *inode, struct file *file)
609{
610 if (!pcipcwd_private.supports_temp)
611 return -ENODEV;
612
613 return nonseekable_open(inode, file);
614}
615
616static int pcipcwd_temp_release(struct inode *inode, struct file *file)
617{
618 return 0;
619}
620
621/*
622 * Notify system
623 */
624
625static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
626{
627 if (code==SYS_DOWN || code==SYS_HALT) {
628 /* Turn the WDT off */
629 pcipcwd_stop();
630 }
631
632 return NOTIFY_DONE;
633}
634
635/*
636 * Kernel Interfaces
637 */
638
Arjan van de Ven62322d22006-07-03 00:24:21 -0700639static const struct file_operations pcipcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 .owner = THIS_MODULE,
641 .llseek = no_llseek,
642 .write = pcipcwd_write,
643 .ioctl = pcipcwd_ioctl,
644 .open = pcipcwd_open,
645 .release = pcipcwd_release,
646};
647
648static struct miscdevice pcipcwd_miscdev = {
649 .minor = WATCHDOG_MINOR,
650 .name = "watchdog",
651 .fops = &pcipcwd_fops,
652};
653
Arjan van de Ven62322d22006-07-03 00:24:21 -0700654static const struct file_operations pcipcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .owner = THIS_MODULE,
656 .llseek = no_llseek,
657 .read = pcipcwd_temp_read,
658 .open = pcipcwd_temp_open,
659 .release = pcipcwd_temp_release,
660};
661
662static struct miscdevice pcipcwd_temp_miscdev = {
663 .minor = TEMP_MINOR,
664 .name = "temperature",
665 .fops = &pcipcwd_temp_fops,
666};
667
668static struct notifier_block pcipcwd_notifier = {
669 .notifier_call = pcipcwd_notify_sys,
670};
671
672/*
673 * Init & exit routines
674 */
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
677 const struct pci_device_id *ent)
678{
679 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 cards_found++;
682 if (cards_found == 1)
683 printk(KERN_INFO PFX DRIVER_VERSION);
684
685 if (cards_found > 1) {
686 printk(KERN_ERR PFX "This driver only supports 1 device\n");
687 return -ENODEV;
688 }
689
690 if (pci_enable_device(pdev)) {
691 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
692 return -ENODEV;
693 }
694
695 if (pci_resource_start(pdev, 0) == 0x0000) {
696 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
697 ret = -ENODEV;
698 goto err_out_disable_device;
699 }
700
701 pcipcwd_private.pdev = pdev;
702 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
703
704 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
705 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
706 (int) pcipcwd_private.io_addr);
707 ret = -EIO;
708 goto err_out_disable_device;
709 }
710
711 /* get the boot_status */
712 pcipcwd_get_status(&pcipcwd_private.boot_status);
713
714 /* clear the "card caused reboot" flag */
715 pcipcwd_clear_status();
716
717 /* disable card */
718 pcipcwd_stop();
719
720 /* Check whether or not the card supports the temperature device */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200721 pcipcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200723 /* Show info about the card itself */
724 pcipcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100726 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
727 if (heartbeat == 0)
728 heartbeat = heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* Check that the heartbeat value is within it's range ; if not reset to the default */
731 if (pcipcwd_set_heartbeat(heartbeat)) {
732 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
733 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
734 WATCHDOG_HEARTBEAT);
735 }
736
737 ret = register_reboot_notifier(&pcipcwd_notifier);
738 if (ret != 0) {
739 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
740 ret);
741 goto err_out_release_region;
742 }
743
744 if (pcipcwd_private.supports_temp) {
745 ret = misc_register(&pcipcwd_temp_miscdev);
746 if (ret != 0) {
747 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
748 TEMP_MINOR, ret);
749 goto err_out_unregister_reboot;
750 }
751 }
752
753 ret = misc_register(&pcipcwd_miscdev);
754 if (ret != 0) {
755 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
756 WATCHDOG_MINOR, ret);
757 goto err_out_misc_deregister;
758 }
759
760 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
761 heartbeat, nowayout);
762
763 return 0;
764
765err_out_misc_deregister:
766 if (pcipcwd_private.supports_temp)
767 misc_deregister(&pcipcwd_temp_miscdev);
768err_out_unregister_reboot:
769 unregister_reboot_notifier(&pcipcwd_notifier);
770err_out_release_region:
771 pci_release_regions(pdev);
772err_out_disable_device:
773 pci_disable_device(pdev);
774 return ret;
775}
776
777static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
778{
779 /* Stop the timer before we leave */
780 if (!nowayout)
781 pcipcwd_stop();
782
783 /* Deregister */
784 misc_deregister(&pcipcwd_miscdev);
785 if (pcipcwd_private.supports_temp)
786 misc_deregister(&pcipcwd_temp_miscdev);
787 unregister_reboot_notifier(&pcipcwd_notifier);
788 pci_release_regions(pdev);
789 pci_disable_device(pdev);
790 cards_found--;
791}
792
793static struct pci_device_id pcipcwd_pci_tbl[] = {
794 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
795 PCI_ANY_ID, PCI_ANY_ID, },
796 { 0 }, /* End of list */
797};
798MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
799
800static struct pci_driver pcipcwd_driver = {
801 .name = WATCHDOG_NAME,
802 .id_table = pcipcwd_pci_tbl,
803 .probe = pcipcwd_card_init,
804 .remove = __devexit_p(pcipcwd_card_exit),
805};
806
807static int __init pcipcwd_init_module(void)
808{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200809 spin_lock_init(&pcipcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 return pci_register_driver(&pcipcwd_driver);
812}
813
814static void __exit pcipcwd_cleanup_module(void)
815{
816 pci_unregister_driver(&pcipcwd_driver);
817}
818
819module_init(pcipcwd_init_module);
820module_exit(pcipcwd_cleanup_module);
821
822MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
823MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
824MODULE_LICENSE("GPL");
825MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
826MODULE_ALIAS_MISCDEV(TEMP_MINOR);