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