blob: 5d76422c402c837ca58a516df7cb9cda2e6a25d9 [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>,
Alan Cox29fa0582008-10-27 15:17:56 +00009 * Alan Cox <alan@lxorguk.ukuu.org.uk>,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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/... */
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000049#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
50#include <linux/io.h> /* For inb/outb/... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/* Module and version information */
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010053#define WATCHDOG_VERSION "1.03"
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +010054#define WATCHDOG_DATE "21 Jan 2007"
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
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +010098/* Watchdog's Dip Switch heartbeat values */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +000099static const int heartbeat_tbl[] = {
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100100 5, /* OFF-OFF-OFF = 5 Sec */
101 10, /* OFF-OFF-ON = 10 Sec */
102 30, /* OFF-ON-OFF = 30 Sec */
103 60, /* OFF-ON-ON = 1 Min */
104 300, /* ON-OFF-OFF = 5 Min */
105 600, /* ON-OFF-ON = 10 Min */
106 1800, /* ON-ON-OFF = 30 Min */
107 3600, /* ON-ON-ON = 1 hour */
108};
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/* We can only use 1 card due to the /dev/watchdog restriction */
111static int cards_found;
112
113/* internal variables */
114static int temp_panic;
115static unsigned long is_active;
116static char expect_release;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200117static struct { /* this is private data for each PCI-PC watchdog card */
118 int supports_temp; /* Wether or not the card has a temperature device */
119 int boot_status; /* The card's boot status */
120 unsigned long io_addr; /* The cards I/O address */
121 spinlock_t io_lock; /* the lock for io operations */
122 struct pci_dev *pdev; /* the PCI-device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123} pcipcwd_private;
124
125/* module parameters */
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200126#define QUIET 0 /* Default */
127#define VERBOSE 1 /* Verbose */
128#define DEBUG 2 /* print fancy stuff too */
129static int debug = QUIET;
130module_param(debug, int, 0);
131MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
132
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100133#define WATCHDOG_HEARTBEAT 0 /* default heartbeat = delay-time from dip-switches */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int heartbeat = WATCHDOG_HEARTBEAT;
135module_param(heartbeat, int, 0);
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100136MODULE_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 -0700137
Andrey Panin4bfdf372005-07-27 11:43:58 -0700138static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139module_param(nowayout, int, 0);
Wim Van Sebroeckbffda5c2007-01-27 20:54:24 +0100140MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142/*
143 * Internal functions
144 */
145
146static int send_command(int cmd, int *msb, int *lsb)
147{
148 int got_response, count;
149
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200150 if (debug >= DEBUG)
151 printk(KERN_DEBUG PFX "sending following data cmd=0x%02x msb=0x%02x lsb=0x%02x\n",
152 cmd, *msb, *lsb);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spin_lock(&pcipcwd_private.io_lock);
155 /* If a command requires data it should be written first.
156 * Data for commands with 8 bits of data should be written to port 4.
157 * Commands with 16 bits of data, should be written as LSB to port 4
158 * and MSB to port 5.
159 * After the required data has been written then write the command to
160 * port 6. */
161 outb_p(*lsb, pcipcwd_private.io_addr + 4);
162 outb_p(*msb, pcipcwd_private.io_addr + 5);
163 outb_p(cmd, pcipcwd_private.io_addr + 6);
164
165 /* wait till the pci card processed the command, signaled by
166 * the WRSP bit in port 2 and give it a max. timeout of
167 * PCI_COMMAND_TIMEOUT to process */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200168 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response); count++) {
170 mdelay(1);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200171 got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200174 if (debug >= DEBUG) {
175 if (got_response) {
176 printk(KERN_DEBUG PFX "time to process command was: %d ms\n",
177 count);
178 } else {
179 printk(KERN_DEBUG PFX "card did not respond on command!\n");
180 }
181 }
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (got_response) {
184 /* read back response */
185 *lsb = inb_p(pcipcwd_private.io_addr + 4);
186 *msb = inb_p(pcipcwd_private.io_addr + 5);
187
188 /* clear WRSP bit */
189 inb_p(pcipcwd_private.io_addr + 6);
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200190
191 if (debug >= DEBUG)
192 printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
193 cmd, *msb, *lsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_unlock(&pcipcwd_private.io_lock);
197
198 return got_response;
199}
200
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200201static inline void pcipcwd_check_temperature_support(void)
202{
203 if (inb_p(pcipcwd_private.io_addr) != 0xF0)
204 pcipcwd_private.supports_temp = 1;
205}
206
207static int pcipcwd_get_option_switches(void)
208{
209 int option_switches;
210
211 option_switches = inb_p(pcipcwd_private.io_addr + 3);
212 return option_switches;
213}
214
215static void pcipcwd_show_card_info(void)
216{
217 int got_fw_rev, fw_rev_major, fw_rev_minor;
218 char fw_ver_str[20]; /* The cards firmware version */
219 int option_switches;
220
221 got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major, &fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000222 if (got_fw_rev)
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200223 sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000224 else
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200225 sprintf(fw_ver_str, "<card no answer>");
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200226
227 /* Get switch settings */
228 option_switches = pcipcwd_get_option_switches();
229
230 printk(KERN_INFO PFX "Found card at port 0x%04x (Firmware: %s) %s temp option\n",
231 (int) pcipcwd_private.io_addr, fw_ver_str,
232 (pcipcwd_private.supports_temp ? "with" : "without"));
233
234 printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
235 option_switches,
236 ((option_switches & 0x10) ? "ON" : "OFF"),
237 ((option_switches & 0x08) ? "ON" : "OFF"));
238
239 if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
240 printk(KERN_INFO PFX "Previous reset was caused by the Watchdog card\n");
241
242 if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
243 printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
244
245 if (pcipcwd_private.boot_status == 0)
246 printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int pcipcwd_start(void)
250{
251 int stat_reg;
252
253 spin_lock(&pcipcwd_private.io_lock);
254 outb_p(0x00, pcipcwd_private.io_addr + 3);
255 udelay(1000);
256
257 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
258 spin_unlock(&pcipcwd_private.io_lock);
259
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200260 if (stat_reg & WD_PCI_WDIS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 printk(KERN_ERR PFX "Card timer not enabled\n");
262 return -1;
263 }
264
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200265 if (debug >= VERBOSE)
266 printk(KERN_DEBUG PFX "Watchdog started\n");
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return 0;
269}
270
271static int pcipcwd_stop(void)
272{
273 int stat_reg;
274
275 spin_lock(&pcipcwd_private.io_lock);
276 outb_p(0xA5, pcipcwd_private.io_addr + 3);
277 udelay(1000);
278
279 outb_p(0xA5, pcipcwd_private.io_addr + 3);
280 udelay(1000);
281
282 stat_reg = inb_p(pcipcwd_private.io_addr + 2);
283 spin_unlock(&pcipcwd_private.io_lock);
284
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200285 if (!(stat_reg & WD_PCI_WDIS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk(KERN_ERR PFX "Card did not acknowledge disable attempt\n");
287 return -1;
288 }
289
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200290 if (debug >= VERBOSE)
291 printk(KERN_DEBUG PFX "Watchdog stopped\n");
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return 0;
294}
295
296static int pcipcwd_keepalive(void)
297{
298 /* Re-trigger watchdog by writing to port 0 */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100299 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200300 outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100301 spin_unlock(&pcipcwd_private.io_lock);
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
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000332 *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 */
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000370 msb = 0;
371 reset_counter = 0xff;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200372 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 Sebroeck045798b2007-01-07 21:57:03 +0100388 spin_lock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200389 *temperature = inb_p(pcipcwd_private.io_addr);
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100390 spin_unlock(&pcipcwd_private.io_lock);
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /*
393 * Convert celsius to fahrenheit, since this was
394 * the decided 'standard' for this return value.
395 */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200396 *temperature = (*temperature * 9 / 5) + 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200398 if (debug >= DEBUG) {
399 printk(KERN_DEBUG PFX "temperature is: %d F\n",
400 *temperature);
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return 0;
404}
405
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200406static int pcipcwd_get_timeleft(int *time_left)
407{
408 int msb;
409 int lsb;
410
411 /* Read the time that's left before rebooting */
412 /* Note: if the board is not yet armed then we will read 0xFFFF */
413 send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
414
415 *time_left = (msb << 8) + lsb;
416
417 if (debug >= VERBOSE)
418 printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
419 *time_left);
420
421 return 0;
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*
425 * /dev/watchdog handling
426 */
427
428static ssize_t pcipcwd_write(struct file *file, const char __user *data,
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200429 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 /* See if we got the magic character 'V' and reload the timer */
432 if (len) {
433 if (!nowayout) {
434 size_t i;
435
436 /* note: just in case someone wrote the magic character
437 * five months ago... */
438 expect_release = 0;
439
440 /* scan to see whether or not we got the magic character */
441 for (i = 0; i != len; i++) {
442 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000443 if (get_user(c, data + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return -EFAULT;
445 if (c == 'V')
446 expect_release = 42;
447 }
448 }
449
450 /* someone wrote to us, we should reload the timer */
451 pcipcwd_keepalive();
452 }
453 return len;
454}
455
Alan Coxc9488522008-07-03 23:51:32 -0700456static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
457 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 void __user *argp = (void __user *)arg;
460 int __user *p = argp;
461 static struct watchdog_info ident = {
462 .options = WDIOF_OVERHEAT |
463 WDIOF_CARDRESET |
464 WDIOF_KEEPALIVEPING |
465 WDIOF_SETTIMEOUT |
466 WDIOF_MAGICCLOSE,
467 .firmware_version = 1,
468 .identity = WATCHDOG_DRIVER_NAME,
469 };
470
471 switch (cmd) {
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000472 case WDIOC_GETSUPPORT:
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000473 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000475 case WDIOC_GETSTATUS:
476 {
477 int status;
478 pcipcwd_get_status(&status);
479 return put_user(status, p);
480 }
481
482 case WDIOC_GETBOOTSTATUS:
483 return put_user(pcipcwd_private.boot_status, p);
484
485 case WDIOC_GETTEMP:
486 {
487 int temperature;
488
489 if (pcipcwd_get_temperature(&temperature))
490 return -EFAULT;
491
492 return put_user(temperature, p);
493 }
494
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000495 case WDIOC_SETOPTIONS:
496 {
497 int new_options, retval = -EINVAL;
498
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000499 if (get_user(new_options, p))
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000500 return -EFAULT;
501
502 if (new_options & WDIOS_DISABLECARD) {
503 if (pcipcwd_stop())
504 return -EIO;
505 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000508 if (new_options & WDIOS_ENABLECARD) {
509 if (pcipcwd_start())
510 return -EIO;
511 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000514 if (new_options & WDIOS_TEMPPANIC) {
515 temp_panic = 1;
516 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000519 return retval;
520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000522 case WDIOC_KEEPALIVE:
523 pcipcwd_keepalive();
524 return 0;
525
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000526 case WDIOC_SETTIMEOUT:
527 {
528 int new_heartbeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000530 if (get_user(new_heartbeat, p))
531 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000533 if (pcipcwd_set_heartbeat(new_heartbeat))
534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000536 pcipcwd_keepalive();
537 /* Fall */
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000540 case WDIOC_GETTIMEOUT:
541 return put_user(heartbeat, p);
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200542
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000543 case WDIOC_GETTIMELEFT:
544 {
545 int time_left;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200546
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000547 if (pcipcwd_get_timeleft(&time_left))
548 return -EFAULT;
Wim Van Sebroeck58b519f2006-05-21 12:48:44 +0200549
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000550 return put_user(time_left, p);
551 }
552
553 default:
554 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556}
557
558static int pcipcwd_open(struct inode *inode, struct file *file)
559{
560 /* /dev/watchdog can only be opened once */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200561 if (test_and_set_bit(0, &is_active)) {
Wim Van Sebroeck195331d2005-09-29 16:22:30 +0200562 if (debug >= VERBOSE)
563 printk(KERN_ERR PFX "Attempt to open already opened device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EBUSY;
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 /* Activate */
568 pcipcwd_start();
569 pcipcwd_keepalive();
570 return nonseekable_open(inode, file);
571}
572
573static int pcipcwd_release(struct inode *inode, struct file *file)
574{
575 /*
576 * Shut off the timer.
577 */
578 if (expect_release == 42) {
579 pcipcwd_stop();
580 } else {
581 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
582 pcipcwd_keepalive();
583 }
584 expect_release = 0;
585 clear_bit(0, &is_active);
586 return 0;
587}
588
589/*
590 * /dev/temperature handling
591 */
592
593static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
594 size_t len, loff_t *ppos)
595{
596 int temperature;
597
598 if (pcipcwd_get_temperature(&temperature))
599 return -EFAULT;
600
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000601 if (copy_to_user(data, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EFAULT;
603
604 return 1;
605}
606
607static int pcipcwd_temp_open(struct inode *inode, struct file *file)
608{
609 if (!pcipcwd_private.supports_temp)
610 return -ENODEV;
611
612 return nonseekable_open(inode, file);
613}
614
615static int pcipcwd_temp_release(struct inode *inode, struct file *file)
616{
617 return 0;
618}
619
620/*
621 * Notify system
622 */
623
624static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
625{
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000626 if (code == SYS_DOWN || code == SYS_HALT)
627 pcipcwd_stop(); /* Turn the WDT off */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return NOTIFY_DONE;
630}
631
632/*
633 * Kernel Interfaces
634 */
635
Arjan van de Ven62322d22006-07-03 00:24:21 -0700636static const struct file_operations pcipcwd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .owner = THIS_MODULE,
638 .llseek = no_llseek,
639 .write = pcipcwd_write,
Alan Coxc9488522008-07-03 23:51:32 -0700640 .unlocked_ioctl = pcipcwd_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 .open = pcipcwd_open,
642 .release = pcipcwd_release,
643};
644
645static struct miscdevice pcipcwd_miscdev = {
646 .minor = WATCHDOG_MINOR,
647 .name = "watchdog",
648 .fops = &pcipcwd_fops,
649};
650
Arjan van de Ven62322d22006-07-03 00:24:21 -0700651static const struct file_operations pcipcwd_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 .owner = THIS_MODULE,
653 .llseek = no_llseek,
654 .read = pcipcwd_temp_read,
655 .open = pcipcwd_temp_open,
656 .release = pcipcwd_temp_release,
657};
658
659static struct miscdevice pcipcwd_temp_miscdev = {
660 .minor = TEMP_MINOR,
661 .name = "temperature",
662 .fops = &pcipcwd_temp_fops,
663};
664
665static struct notifier_block pcipcwd_notifier = {
666 .notifier_call = pcipcwd_notify_sys,
667};
668
669/*
670 * Init & exit routines
671 */
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
674 const struct pci_device_id *ent)
675{
676 int ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 cards_found++;
679 if (cards_found == 1)
680 printk(KERN_INFO PFX DRIVER_VERSION);
681
682 if (cards_found > 1) {
683 printk(KERN_ERR PFX "This driver only supports 1 device\n");
684 return -ENODEV;
685 }
686
687 if (pci_enable_device(pdev)) {
688 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
689 return -ENODEV;
690 }
691
692 if (pci_resource_start(pdev, 0) == 0x0000) {
693 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
694 ret = -ENODEV;
695 goto err_out_disable_device;
696 }
697
698 pcipcwd_private.pdev = pdev;
699 pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
700
701 if (pci_request_regions(pdev, WATCHDOG_NAME)) {
702 printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
703 (int) pcipcwd_private.io_addr);
704 ret = -EIO;
705 goto err_out_disable_device;
706 }
707
708 /* get the boot_status */
709 pcipcwd_get_status(&pcipcwd_private.boot_status);
710
711 /* clear the "card caused reboot" flag */
712 pcipcwd_clear_status();
713
714 /* disable card */
715 pcipcwd_stop();
716
717 /* Check whether or not the card supports the temperature device */
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200718 pcipcwd_check_temperature_support();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200720 /* Show info about the card itself */
721 pcipcwd_show_card_info();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Wim Van Sebroeck39e3a052007-01-07 21:49:11 +0100723 /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
724 if (heartbeat == 0)
725 heartbeat = heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Check that the heartbeat value is within it's range ; if not reset to the default */
728 if (pcipcwd_set_heartbeat(heartbeat)) {
729 pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
730 printk(KERN_INFO PFX "heartbeat value must be 0<heartbeat<65536, using %d\n",
731 WATCHDOG_HEARTBEAT);
732 }
733
734 ret = register_reboot_notifier(&pcipcwd_notifier);
735 if (ret != 0) {
736 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
737 ret);
738 goto err_out_release_region;
739 }
740
741 if (pcipcwd_private.supports_temp) {
742 ret = misc_register(&pcipcwd_temp_miscdev);
743 if (ret != 0) {
744 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
745 TEMP_MINOR, ret);
746 goto err_out_unregister_reboot;
747 }
748 }
749
750 ret = misc_register(&pcipcwd_miscdev);
751 if (ret != 0) {
752 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
753 WATCHDOG_MINOR, ret);
754 goto err_out_misc_deregister;
755 }
756
757 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
758 heartbeat, nowayout);
759
760 return 0;
761
762err_out_misc_deregister:
763 if (pcipcwd_private.supports_temp)
764 misc_deregister(&pcipcwd_temp_miscdev);
765err_out_unregister_reboot:
766 unregister_reboot_notifier(&pcipcwd_notifier);
767err_out_release_region:
768 pci_release_regions(pdev);
769err_out_disable_device:
770 pci_disable_device(pdev);
771 return ret;
772}
773
774static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
775{
776 /* Stop the timer before we leave */
777 if (!nowayout)
778 pcipcwd_stop();
779
780 /* Deregister */
781 misc_deregister(&pcipcwd_miscdev);
782 if (pcipcwd_private.supports_temp)
783 misc_deregister(&pcipcwd_temp_miscdev);
784 unregister_reboot_notifier(&pcipcwd_notifier);
785 pci_release_regions(pdev);
786 pci_disable_device(pdev);
787 cards_found--;
788}
789
790static struct pci_device_id pcipcwd_pci_tbl[] = {
791 { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
792 PCI_ANY_ID, PCI_ANY_ID, },
793 { 0 }, /* End of list */
794};
795MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
796
797static struct pci_driver pcipcwd_driver = {
798 .name = WATCHDOG_NAME,
799 .id_table = pcipcwd_pci_tbl,
800 .probe = pcipcwd_card_init,
801 .remove = __devexit_p(pcipcwd_card_exit),
802};
803
804static int __init pcipcwd_init_module(void)
805{
Wim Van Sebroecka0800f62005-09-29 16:21:50 +0200806 spin_lock_init(&pcipcwd_private.io_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 return pci_register_driver(&pcipcwd_driver);
809}
810
811static void __exit pcipcwd_cleanup_module(void)
812{
813 pci_unregister_driver(&pcipcwd_driver);
Wim Van Sebroeck045798b2007-01-07 21:57:03 +0100814
815 printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818module_init(pcipcwd_init_module);
819module_exit(pcipcwd_cleanup_module);
820
821MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
822MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
823MODULE_LICENSE("GPL");
824MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
825MODULE_ALIAS_MISCDEV(TEMP_MINOR);