Commit | Line | Data |
---|---|---|
d0173278 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
1da177e4 LT |
2 | /* |
3 | * Industrial Computer Source PCI-WDT500/501 driver | |
4 | * | |
29fa0586 | 5 | * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
9b901ee0 | 6 | * All Rights Reserved. |
1da177e4 | 7 | * |
1da177e4 LT |
8 | * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide |
9 | * warranty for any of this software. This material is provided | |
10 | * "AS-IS" and at no charge. | |
11 | * | |
12 | * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk> | |
13 | * | |
14 | * Release 0.10. | |
15 | * | |
16 | * Fixes | |
17 | * Dave Gregorich : Modularisation and minor bugs | |
18 | * Alan Cox : Added the watchdog ioctl() stuff | |
19 | * Alan Cox : Fixed the reboot problem (as noted by | |
20 | * Matt Crocker). | |
21 | * Alan Cox : Added wdt= boot option | |
22 | * Alan Cox : Cleaned up copy/user stuff | |
23 | * Tim Hockin : Added insmod parameters, comment cleanup | |
24 | * Parameterized timeout | |
25 | * JP Nollmann : Added support for PCI wdt501p | |
26 | * Alan Cox : Split ISA and PCI cards into two drivers | |
27 | * Jeff Garzik : PCI cleanups | |
9f2d1f0d AC |
28 | * Tigran Aivazian : Restructured wdtpci_init_one() to handle |
29 | * failures | |
5f3b2756 | 30 | * Joel Becker : Added WDIOC_GET/SETTIMEOUT |
9f2d1f0d AC |
31 | * Zwane Mwaikambo : Magic char closing, locking changes, |
32 | * cleanups | |
1da177e4 LT |
33 | * Matt Domsch : nowayout module option |
34 | */ | |
35 | ||
27c766aa JP |
36 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
37 | ||
1da177e4 LT |
38 | #include <linux/interrupt.h> |
39 | #include <linux/module.h> | |
40 | #include <linux/moduleparam.h> | |
41 | #include <linux/types.h> | |
42 | #include <linux/miscdevice.h> | |
43 | #include <linux/watchdog.h> | |
44 | #include <linux/ioport.h> | |
81830061 | 45 | #include <linux/delay.h> |
1da177e4 LT |
46 | #include <linux/notifier.h> |
47 | #include <linux/reboot.h> | |
1da177e4 LT |
48 | #include <linux/fs.h> |
49 | #include <linux/pci.h> | |
9f2d1f0d AC |
50 | #include <linux/io.h> |
51 | #include <linux/uaccess.h> | |
1da177e4 | 52 | |
1da177e4 LT |
53 | |
54 | #define WDT_IS_PCI | |
55 | #include "wd501p.h" | |
56 | ||
1da177e4 LT |
57 | /* We can only use 1 card due to the /dev/watchdog restriction */ |
58 | static int dev_count; | |
59 | ||
9f2d1f0d | 60 | static unsigned long open_lock; |
c7dfd0cc | 61 | static DEFINE_SPINLOCK(wdtpci_lock); |
1da177e4 LT |
62 | static char expect_close; |
63 | ||
d1833c21 | 64 | static resource_size_t io; |
1da177e4 LT |
65 | static int irq; |
66 | ||
67 | /* Default timeout */ | |
68 | #define WD_TIMO 60 /* Default heartbeat = 60 seconds */ | |
69 | ||
70 | static int heartbeat = WD_TIMO; | |
71 | static int wd_heartbeat; | |
72 | module_param(heartbeat, int, 0); | |
9f2d1f0d AC |
73 | MODULE_PARM_DESC(heartbeat, |
74 | "Watchdog heartbeat in seconds. (0<heartbeat<65536, default=" | |
75 | __MODULE_STRING(WD_TIMO) ")"); | |
1da177e4 | 76 | |
86a1e189 WVS |
77 | static bool nowayout = WATCHDOG_NOWAYOUT; |
78 | module_param(nowayout, bool, 0); | |
9f2d1f0d AC |
79 | MODULE_PARM_DESC(nowayout, |
80 | "Watchdog cannot be stopped once started (default=" | |
81 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | |
1da177e4 | 82 | |
1da177e4 LT |
83 | /* Support for the Fan Tachometer on the PCI-WDT501 */ |
84 | static int tachometer; | |
1da177e4 | 85 | module_param(tachometer, int, 0); |
9f2d1f0d | 86 | MODULE_PARM_DESC(tachometer, |
9b901ee0 WVS |
87 | "PCI-WDT501 Fan Tachometer support (0=disable, default=0)"); |
88 | ||
89 | static int type = 500; | |
90 | module_param(type, int, 0); | |
91 | MODULE_PARM_DESC(type, | |
92 | "PCI-WDT501 Card type (500 or 501 , default=500)"); | |
1da177e4 LT |
93 | |
94 | /* | |
95 | * Programming support | |
96 | */ | |
97 | ||
98 | static void wdtpci_ctr_mode(int ctr, int mode) | |
99 | { | |
9f2d1f0d AC |
100 | ctr <<= 6; |
101 | ctr |= 0x30; | |
102 | ctr |= (mode << 1); | |
103 | outb(ctr, WDT_CR); | |
104 | udelay(8); | |
1da177e4 LT |
105 | } |
106 | ||
107 | static void wdtpci_ctr_load(int ctr, int val) | |
108 | { | |
9f2d1f0d AC |
109 | outb(val & 0xFF, WDT_COUNT0 + ctr); |
110 | udelay(8); | |
111 | outb(val >> 8, WDT_COUNT0 + ctr); | |
112 | udelay(8); | |
1da177e4 LT |
113 | } |
114 | ||
115 | /** | |
116 | * wdtpci_start: | |
117 | * | |
118 | * Start the watchdog driver. | |
119 | */ | |
120 | ||
121 | static int wdtpci_start(void) | |
122 | { | |
123 | unsigned long flags; | |
124 | ||
125 | spin_lock_irqsave(&wdtpci_lock, flags); | |
126 | ||
127 | /* | |
128 | * "pet" the watchdog, as Access says. | |
129 | * This resets the clock outputs. | |
130 | */ | |
9f2d1f0d AC |
131 | inb(WDT_DC); /* Disable watchdog */ |
132 | udelay(8); | |
133 | wdtpci_ctr_mode(2, 0); /* Program CTR2 for Mode 0: | |
134 | Pulse on Terminal Count */ | |
135 | outb(0, WDT_DC); /* Enable watchdog */ | |
136 | udelay(8); | |
137 | inb(WDT_DC); /* Disable watchdog */ | |
138 | udelay(8); | |
139 | outb(0, WDT_CLOCK); /* 2.0833MHz clock */ | |
140 | udelay(8); | |
141 | inb(WDT_BUZZER); /* disable */ | |
142 | udelay(8); | |
143 | inb(WDT_OPTONOTRST); /* disable */ | |
144 | udelay(8); | |
145 | inb(WDT_OPTORST); /* disable */ | |
146 | udelay(8); | |
147 | inb(WDT_PROGOUT); /* disable */ | |
148 | udelay(8); | |
149 | wdtpci_ctr_mode(0, 3); /* Program CTR0 for Mode 3: | |
150 | Square Wave Generator */ | |
151 | wdtpci_ctr_mode(1, 2); /* Program CTR1 for Mode 2: | |
152 | Rate Generator */ | |
153 | wdtpci_ctr_mode(2, 1); /* Program CTR2 for Mode 1: | |
154 | Retriggerable One-Shot */ | |
155 | wdtpci_ctr_load(0, 20833); /* count at 100Hz */ | |
156 | wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */ | |
1da177e4 | 157 | /* DO NOT LOAD CTR2 on PCI card! -- JPN */ |
9f2d1f0d AC |
158 | outb(0, WDT_DC); /* Enable watchdog */ |
159 | udelay(8); | |
1da177e4 LT |
160 | |
161 | spin_unlock_irqrestore(&wdtpci_lock, flags); | |
162 | return 0; | |
163 | } | |
164 | ||
165 | /** | |
166 | * wdtpci_stop: | |
167 | * | |
168 | * Stop the watchdog driver. | |
169 | */ | |
170 | ||
9f2d1f0d | 171 | static int wdtpci_stop(void) |
1da177e4 LT |
172 | { |
173 | unsigned long flags; | |
174 | ||
175 | /* Turn the card off */ | |
176 | spin_lock_irqsave(&wdtpci_lock, flags); | |
9f2d1f0d AC |
177 | inb(WDT_DC); /* Disable watchdog */ |
178 | udelay(8); | |
179 | wdtpci_ctr_load(2, 0); /* 0 length reset pulses now */ | |
1da177e4 LT |
180 | spin_unlock_irqrestore(&wdtpci_lock, flags); |
181 | return 0; | |
182 | } | |
183 | ||
184 | /** | |
185 | * wdtpci_ping: | |
186 | * | |
9f2d1f0d AC |
187 | * Reload counter one with the watchdog heartbeat. We don't bother |
188 | * reloading the cascade counter. | |
1da177e4 LT |
189 | */ |
190 | ||
191 | static int wdtpci_ping(void) | |
192 | { | |
193 | unsigned long flags; | |
194 | ||
1da177e4 | 195 | spin_lock_irqsave(&wdtpci_lock, flags); |
9f2d1f0d AC |
196 | /* Write a watchdog value */ |
197 | inb(WDT_DC); /* Disable watchdog */ | |
198 | udelay(8); | |
199 | wdtpci_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2: | |
200 | Rate Generator */ | |
201 | wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */ | |
202 | outb(0, WDT_DC); /* Enable watchdog */ | |
203 | udelay(8); | |
1da177e4 LT |
204 | spin_unlock_irqrestore(&wdtpci_lock, flags); |
205 | return 0; | |
206 | } | |
207 | ||
208 | /** | |
209 | * wdtpci_set_heartbeat: | |
210 | * @t: the new heartbeat value that needs to be set. | |
211 | * | |
9f2d1f0d AC |
212 | * Set a new heartbeat value for the watchdog device. If the heartbeat |
213 | * value is incorrect we keep the old value and return -EINVAL. | |
214 | * If successful we return 0. | |
1da177e4 LT |
215 | */ |
216 | static int wdtpci_set_heartbeat(int t) | |
217 | { | |
218 | /* Arbitrary, can't find the card's limits */ | |
9f2d1f0d | 219 | if (t < 1 || t > 65535) |
1da177e4 LT |
220 | return -EINVAL; |
221 | ||
222 | heartbeat = t; | |
223 | wd_heartbeat = t * 100; | |
224 | return 0; | |
225 | } | |
226 | ||
227 | /** | |
228 | * wdtpci_get_status: | |
229 | * @status: the new status. | |
230 | * | |
231 | * Extract the status information from a WDT watchdog device. There are | |
232 | * several board variants so we have to know which bits are valid. Some | |
233 | * bits default to one and some to zero in order to be maximally painful. | |
234 | * | |
235 | * we then map the bits onto the status ioctl flags. | |
236 | */ | |
237 | ||
238 | static int wdtpci_get_status(int *status) | |
239 | { | |
9f2d1f0d AC |
240 | unsigned char new_status; |
241 | unsigned long flags; | |
242 | ||
243 | spin_lock_irqsave(&wdtpci_lock, flags); | |
244 | new_status = inb(WDT_SR); | |
245 | spin_unlock_irqrestore(&wdtpci_lock, flags); | |
1da177e4 | 246 | |
9f2d1f0d | 247 | *status = 0; |
1da177e4 LT |
248 | if (new_status & WDC_SR_ISOI0) |
249 | *status |= WDIOF_EXTERN1; | |
250 | if (new_status & WDC_SR_ISII1) | |
251 | *status |= WDIOF_EXTERN2; | |
9b901ee0 WVS |
252 | if (type == 501) { |
253 | if (!(new_status & WDC_SR_TGOOD)) | |
254 | *status |= WDIOF_OVERHEAT; | |
255 | if (!(new_status & WDC_SR_PSUOVER)) | |
256 | *status |= WDIOF_POWEROVER; | |
257 | if (!(new_status & WDC_SR_PSUUNDR)) | |
258 | *status |= WDIOF_POWERUNDER; | |
259 | if (tachometer) { | |
260 | if (!(new_status & WDC_SR_FANGOOD)) | |
261 | *status |= WDIOF_FANFAULT; | |
262 | } | |
1da177e4 | 263 | } |
1da177e4 LT |
264 | return 0; |
265 | } | |
266 | ||
1da177e4 LT |
267 | /** |
268 | * wdtpci_get_temperature: | |
269 | * | |
270 | * Reports the temperature in degrees Fahrenheit. The API is in | |
271 | * farenheit. It was designed by an imperial measurement luddite. | |
272 | */ | |
273 | ||
274 | static int wdtpci_get_temperature(int *temperature) | |
275 | { | |
9f2d1f0d AC |
276 | unsigned short c; |
277 | unsigned long flags; | |
278 | spin_lock_irqsave(&wdtpci_lock, flags); | |
279 | c = inb(WDT_RT); | |
280 | udelay(8); | |
281 | spin_unlock_irqrestore(&wdtpci_lock, flags); | |
1da177e4 LT |
282 | *temperature = (c * 11 / 15) + 7; |
283 | return 0; | |
284 | } | |
1da177e4 LT |
285 | |
286 | /** | |
287 | * wdtpci_interrupt: | |
288 | * @irq: Interrupt number | |
289 | * @dev_id: Unused as we don't allow multiple devices. | |
1da177e4 LT |
290 | * |
291 | * Handle an interrupt from the board. These are raised when the status | |
292 | * map changes in what the board considers an interesting way. That means | |
293 | * a failure condition occurring. | |
294 | */ | |
295 | ||
7d12e780 | 296 | static irqreturn_t wdtpci_interrupt(int irq, void *dev_id) |
1da177e4 LT |
297 | { |
298 | /* | |
299 | * Read the status register see what is up and | |
300 | * then printk it. | |
301 | */ | |
9f2d1f0d AC |
302 | unsigned char status; |
303 | ||
304 | spin_lock(&wdtpci_lock); | |
305 | ||
306 | status = inb(WDT_SR); | |
307 | udelay(8); | |
1da177e4 | 308 | |
27c766aa | 309 | pr_crit("status %d\n", status); |
1da177e4 | 310 | |
9b901ee0 WVS |
311 | if (type == 501) { |
312 | if (!(status & WDC_SR_TGOOD)) { | |
27c766aa | 313 | pr_crit("Overheat alarm (%d)\n", inb(WDT_RT)); |
9b901ee0 WVS |
314 | udelay(8); |
315 | } | |
316 | if (!(status & WDC_SR_PSUOVER)) | |
27c766aa | 317 | pr_crit("PSU over voltage\n"); |
9b901ee0 | 318 | if (!(status & WDC_SR_PSUUNDR)) |
27c766aa | 319 | pr_crit("PSU under voltage\n"); |
9b901ee0 WVS |
320 | if (tachometer) { |
321 | if (!(status & WDC_SR_FANGOOD)) | |
27c766aa | 322 | pr_crit("Possible fan fault\n"); |
9b901ee0 | 323 | } |
1da177e4 | 324 | } |
9b901ee0 | 325 | if (!(status & WDC_SR_WCCR)) { |
1da177e4 LT |
326 | #ifdef SOFTWARE_REBOOT |
327 | #ifdef ONLY_TESTING | |
27c766aa | 328 | pr_crit("Would Reboot\n"); |
1da177e4 | 329 | #else |
27c766aa | 330 | pr_crit("Initiating system reboot\n"); |
455a9a60 | 331 | emergency_restart(); |
1da177e4 LT |
332 | #endif |
333 | #else | |
27c766aa | 334 | pr_crit("Reset in 5ms\n"); |
1da177e4 | 335 | #endif |
59338d4c | 336 | } |
9f2d1f0d | 337 | spin_unlock(&wdtpci_lock); |
1da177e4 LT |
338 | return IRQ_HANDLED; |
339 | } | |
340 | ||
341 | ||
342 | /** | |
343 | * wdtpci_write: | |
344 | * @file: file handle to the watchdog | |
345 | * @buf: buffer to write (unused as data does not matter here | |
346 | * @count: count of bytes | |
347 | * @ppos: pointer to the position to write. No seeks allowed | |
348 | * | |
349 | * A write to a watchdog device is defined as a keepalive signal. Any | |
350 | * write of data will do, as we we don't define content meaning. | |
351 | */ | |
352 | ||
9f2d1f0d | 353 | static ssize_t wdtpci_write(struct file *file, const char __user *buf, |
9b901ee0 | 354 | size_t count, loff_t *ppos) |
1da177e4 LT |
355 | { |
356 | if (count) { | |
357 | if (!nowayout) { | |
358 | size_t i; | |
359 | ||
9b901ee0 | 360 | /* In case it was set long ago */ |
1da177e4 LT |
361 | expect_close = 0; |
362 | ||
363 | for (i = 0; i != count; i++) { | |
364 | char c; | |
7944d3a5 | 365 | if (get_user(c, buf + i)) |
1da177e4 LT |
366 | return -EFAULT; |
367 | if (c == 'V') | |
368 | expect_close = 42; | |
369 | } | |
370 | } | |
371 | wdtpci_ping(); | |
372 | } | |
1da177e4 LT |
373 | return count; |
374 | } | |
375 | ||
376 | /** | |
377 | * wdtpci_ioctl: | |
1da177e4 LT |
378 | * @file: file handle to the device |
379 | * @cmd: watchdog command | |
380 | * @arg: argument pointer | |
381 | * | |
382 | * The watchdog API defines a common set of functions for all watchdogs | |
383 | * according to their available features. We only actually usefully support | |
384 | * querying capabilities and current status. | |
385 | */ | |
386 | ||
9f2d1f0d AC |
387 | static long wdtpci_ioctl(struct file *file, unsigned int cmd, |
388 | unsigned long arg) | |
1da177e4 | 389 | { |
1da177e4 LT |
390 | void __user *argp = (void __user *)arg; |
391 | int __user *p = argp; | |
9b901ee0 WVS |
392 | int new_heartbeat; |
393 | int status; | |
1da177e4 | 394 | |
c1bf3acf | 395 | struct watchdog_info ident = { |
1da177e4 LT |
396 | .options = WDIOF_SETTIMEOUT| |
397 | WDIOF_MAGICCLOSE| | |
398 | WDIOF_KEEPALIVEPING, | |
399 | .firmware_version = 1, | |
400 | .identity = "PCI-WDT500/501", | |
401 | }; | |
402 | ||
403 | /* Add options according to the card we have */ | |
404 | ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2); | |
9b901ee0 WVS |
405 | if (type == 501) { |
406 | ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER| | |
407 | WDIOF_POWEROVER); | |
408 | if (tachometer) | |
409 | ident.options |= WDIOF_FANFAULT; | |
410 | } | |
1da177e4 | 411 | |
9f2d1f0d | 412 | switch (cmd) { |
9f2d1f0d AC |
413 | case WDIOC_GETSUPPORT: |
414 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; | |
415 | case WDIOC_GETSTATUS: | |
416 | wdtpci_get_status(&status); | |
417 | return put_user(status, p); | |
418 | case WDIOC_GETBOOTSTATUS: | |
419 | return put_user(0, p); | |
420 | case WDIOC_KEEPALIVE: | |
421 | wdtpci_ping(); | |
422 | return 0; | |
423 | case WDIOC_SETTIMEOUT: | |
424 | if (get_user(new_heartbeat, p)) | |
425 | return -EFAULT; | |
426 | if (wdtpci_set_heartbeat(new_heartbeat)) | |
427 | return -EINVAL; | |
428 | wdtpci_ping(); | |
57b6d481 | 429 | /* fall through */ |
9f2d1f0d AC |
430 | case WDIOC_GETTIMEOUT: |
431 | return put_user(heartbeat, p); | |
0c06090c WVS |
432 | default: |
433 | return -ENOTTY; | |
434 | } | |
1da177e4 LT |
435 | } |
436 | ||
437 | /** | |
438 | * wdtpci_open: | |
439 | * @inode: inode of device | |
440 | * @file: file handle to device | |
441 | * | |
442 | * The watchdog device has been opened. The watchdog device is single | |
443 | * open and on opening we load the counters. Counter zero is a 100Hz | |
444 | * cascade, into counter 1 which downcounts to reboot. When the counter | |
445 | * triggers counter 2 downcounts the length of the reset pulse which | |
446 | * set set to be as long as possible. | |
447 | */ | |
448 | ||
449 | static int wdtpci_open(struct inode *inode, struct file *file) | |
450 | { | |
9f2d1f0d | 451 | if (test_and_set_bit(0, &open_lock)) |
1da177e4 LT |
452 | return -EBUSY; |
453 | ||
9f2d1f0d | 454 | if (nowayout) |
1da177e4 | 455 | __module_get(THIS_MODULE); |
1da177e4 LT |
456 | /* |
457 | * Activate | |
458 | */ | |
459 | wdtpci_start(); | |
c5bf68fe | 460 | return stream_open(inode, file); |
1da177e4 LT |
461 | } |
462 | ||
463 | /** | |
464 | * wdtpci_release: | |
465 | * @inode: inode to board | |
466 | * @file: file handle to board | |
467 | * | |
468 | * The watchdog has a configurable API. There is a religious dispute | |
469 | * between people who want their watchdog to be able to shut down and | |
470 | * those who want to be sure if the watchdog manager dies the machine | |
471 | * reboots. In the former case we disable the counters, in the latter | |
472 | * case you have to open it again very soon. | |
473 | */ | |
474 | ||
475 | static int wdtpci_release(struct inode *inode, struct file *file) | |
476 | { | |
477 | if (expect_close == 42) { | |
478 | wdtpci_stop(); | |
479 | } else { | |
27c766aa | 480 | pr_crit("Unexpected close, not stopping timer!\n"); |
1da177e4 LT |
481 | wdtpci_ping(); |
482 | } | |
483 | expect_close = 0; | |
9f2d1f0d | 484 | clear_bit(0, &open_lock); |
1da177e4 LT |
485 | return 0; |
486 | } | |
487 | ||
1da177e4 LT |
488 | /** |
489 | * wdtpci_temp_read: | |
490 | * @file: file handle to the watchdog board | |
491 | * @buf: buffer to write 1 byte into | |
492 | * @count: length of buffer | |
493 | * @ptr: offset (no seek allowed) | |
494 | * | |
495 | * Read reports the temperature in degrees Fahrenheit. The API is in | |
496 | * fahrenheit. It was designed by an imperial measurement luddite. | |
497 | */ | |
498 | ||
9f2d1f0d AC |
499 | static ssize_t wdtpci_temp_read(struct file *file, char __user *buf, |
500 | size_t count, loff_t *ptr) | |
1da177e4 LT |
501 | { |
502 | int temperature; | |
503 | ||
504 | if (wdtpci_get_temperature(&temperature)) | |
505 | return -EFAULT; | |
506 | ||
9f2d1f0d | 507 | if (copy_to_user(buf, &temperature, 1)) |
1da177e4 LT |
508 | return -EFAULT; |
509 | ||
510 | return 1; | |
511 | } | |
512 | ||
513 | /** | |
514 | * wdtpci_temp_open: | |
515 | * @inode: inode of device | |
516 | * @file: file handle to device | |
517 | * | |
518 | * The temperature device has been opened. | |
519 | */ | |
520 | ||
521 | static int wdtpci_temp_open(struct inode *inode, struct file *file) | |
522 | { | |
c5bf68fe | 523 | return stream_open(inode, file); |
1da177e4 LT |
524 | } |
525 | ||
526 | /** | |
527 | * wdtpci_temp_release: | |
528 | * @inode: inode to board | |
529 | * @file: file handle to board | |
530 | * | |
531 | * The temperature device has been closed. | |
532 | */ | |
533 | ||
534 | static int wdtpci_temp_release(struct inode *inode, struct file *file) | |
535 | { | |
536 | return 0; | |
537 | } | |
1da177e4 LT |
538 | |
539 | /** | |
540 | * notify_sys: | |
541 | * @this: our notifier block | |
542 | * @code: the event being reported | |
543 | * @unused: unused | |
544 | * | |
545 | * Our notifier is called on system shutdowns. We want to turn the card | |
546 | * off at reboot otherwise the machine will reboot again during memory | |
547 | * test or worse yet during the following fsck. This would suck, in fact | |
548 | * trust me - if it happens it does suck. | |
549 | */ | |
550 | ||
551 | static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code, | |
9f2d1f0d | 552 | void *unused) |
1da177e4 | 553 | { |
9f2d1f0d | 554 | if (code == SYS_DOWN || code == SYS_HALT) |
1da177e4 | 555 | wdtpci_stop(); |
1da177e4 LT |
556 | return NOTIFY_DONE; |
557 | } | |
558 | ||
559 | /* | |
560 | * Kernel Interfaces | |
561 | */ | |
562 | ||
563 | ||
62322d25 | 564 | static const struct file_operations wdtpci_fops = { |
1da177e4 LT |
565 | .owner = THIS_MODULE, |
566 | .llseek = no_llseek, | |
567 | .write = wdtpci_write, | |
9f2d1f0d | 568 | .unlocked_ioctl = wdtpci_ioctl, |
1da177e4 LT |
569 | .open = wdtpci_open, |
570 | .release = wdtpci_release, | |
571 | }; | |
572 | ||
573 | static struct miscdevice wdtpci_miscdev = { | |
574 | .minor = WATCHDOG_MINOR, | |
575 | .name = "watchdog", | |
576 | .fops = &wdtpci_fops, | |
577 | }; | |
578 | ||
62322d25 | 579 | static const struct file_operations wdtpci_temp_fops = { |
1da177e4 LT |
580 | .owner = THIS_MODULE, |
581 | .llseek = no_llseek, | |
582 | .read = wdtpci_temp_read, | |
583 | .open = wdtpci_temp_open, | |
584 | .release = wdtpci_temp_release, | |
585 | }; | |
586 | ||
587 | static struct miscdevice temp_miscdev = { | |
588 | .minor = TEMP_MINOR, | |
589 | .name = "temperature", | |
590 | .fops = &wdtpci_temp_fops, | |
591 | }; | |
1da177e4 LT |
592 | |
593 | /* | |
594 | * The WDT card needs to learn about soft shutdowns in order to | |
595 | * turn the timebomb registers off. | |
596 | */ | |
597 | ||
598 | static struct notifier_block wdtpci_notifier = { | |
599 | .notifier_call = wdtpci_notify_sys, | |
600 | }; | |
601 | ||
602 | ||
2d991a16 | 603 | static int wdtpci_init_one(struct pci_dev *dev, |
9f2d1f0d | 604 | const struct pci_device_id *ent) |
1da177e4 LT |
605 | { |
606 | int ret = -EIO; | |
607 | ||
608 | dev_count++; | |
609 | if (dev_count > 1) { | |
27c766aa | 610 | pr_err("This driver only supports one device\n"); |
1da177e4 LT |
611 | return -ENODEV; |
612 | } | |
613 | ||
9b901ee0 | 614 | if (type != 500 && type != 501) { |
27c766aa | 615 | pr_err("unknown card type '%d'\n", type); |
9b901ee0 WVS |
616 | return -ENODEV; |
617 | } | |
618 | ||
9f2d1f0d | 619 | if (pci_enable_device(dev)) { |
27c766aa | 620 | pr_err("Not possible to enable PCI Device\n"); |
1da177e4 LT |
621 | return -ENODEV; |
622 | } | |
623 | ||
9f2d1f0d | 624 | if (pci_resource_start(dev, 2) == 0x0000) { |
27c766aa | 625 | pr_err("No I/O-Address for card detected\n"); |
1da177e4 LT |
626 | ret = -ENODEV; |
627 | goto out_pci; | |
628 | } | |
629 | ||
119d3e56 | 630 | if (pci_request_region(dev, 2, "wdt_pci")) { |
27c766aa JP |
631 | pr_err("I/O address 0x%llx already in use\n", |
632 | (unsigned long long)pci_resource_start(dev, 2)); | |
1da177e4 LT |
633 | goto out_pci; |
634 | } | |
635 | ||
119d3e56 WVS |
636 | irq = dev->irq; |
637 | io = pci_resource_start(dev, 2); | |
638 | ||
86b59128 | 639 | if (request_irq(irq, wdtpci_interrupt, IRQF_SHARED, |
1da177e4 | 640 | "wdt_pci", &wdtpci_miscdev)) { |
27c766aa | 641 | pr_err("IRQ %d is not free\n", irq); |
1da177e4 LT |
642 | goto out_reg; |
643 | } | |
644 | ||
27c766aa JP |
645 | pr_info("PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%llx (Interrupt %d)\n", |
646 | (unsigned long long)io, irq); | |
1da177e4 | 647 | |
9f2d1f0d AC |
648 | /* Check that the heartbeat value is within its range; |
649 | if not reset to the default */ | |
1da177e4 LT |
650 | if (wdtpci_set_heartbeat(heartbeat)) { |
651 | wdtpci_set_heartbeat(WD_TIMO); | |
27c766aa JP |
652 | pr_info("heartbeat value must be 0 < heartbeat < 65536, using %d\n", |
653 | WD_TIMO); | |
1da177e4 LT |
654 | } |
655 | ||
9f2d1f0d | 656 | ret = register_reboot_notifier(&wdtpci_notifier); |
1da177e4 | 657 | if (ret) { |
27c766aa | 658 | pr_err("cannot register reboot notifier (err=%d)\n", ret); |
1da177e4 LT |
659 | goto out_irq; |
660 | } | |
661 | ||
9b901ee0 WVS |
662 | if (type == 501) { |
663 | ret = misc_register(&temp_miscdev); | |
664 | if (ret) { | |
27c766aa JP |
665 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
666 | TEMP_MINOR, ret); | |
9b901ee0 WVS |
667 | goto out_rbt; |
668 | } | |
1da177e4 | 669 | } |
1da177e4 | 670 | |
9f2d1f0d | 671 | ret = misc_register(&wdtpci_miscdev); |
1da177e4 | 672 | if (ret) { |
27c766aa JP |
673 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
674 | WATCHDOG_MINOR, ret); | |
1da177e4 LT |
675 | goto out_misc; |
676 | } | |
677 | ||
27c766aa | 678 | pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n", |
1da177e4 | 679 | heartbeat, nowayout); |
9b901ee0 | 680 | if (type == 501) |
27c766aa JP |
681 | pr_info("Fan Tachometer is %s\n", |
682 | tachometer ? "Enabled" : "Disabled"); | |
1da177e4 LT |
683 | |
684 | ret = 0; | |
685 | out: | |
686 | return ret; | |
687 | ||
688 | out_misc: | |
9b901ee0 WVS |
689 | if (type == 501) |
690 | misc_deregister(&temp_miscdev); | |
1da177e4 | 691 | out_rbt: |
1da177e4 LT |
692 | unregister_reboot_notifier(&wdtpci_notifier); |
693 | out_irq: | |
694 | free_irq(irq, &wdtpci_miscdev); | |
695 | out_reg: | |
119d3e56 | 696 | pci_release_region(dev, 2); |
1da177e4 LT |
697 | out_pci: |
698 | pci_disable_device(dev); | |
699 | goto out; | |
700 | } | |
701 | ||
702 | ||
4b12b896 | 703 | static void wdtpci_remove_one(struct pci_dev *pdev) |
1da177e4 LT |
704 | { |
705 | /* here we assume only one device will ever have | |
706 | * been picked up and registered by probe function */ | |
707 | misc_deregister(&wdtpci_miscdev); | |
9b901ee0 WVS |
708 | if (type == 501) |
709 | misc_deregister(&temp_miscdev); | |
1da177e4 LT |
710 | unregister_reboot_notifier(&wdtpci_notifier); |
711 | free_irq(irq, &wdtpci_miscdev); | |
119d3e56 | 712 | pci_release_region(pdev, 2); |
1da177e4 LT |
713 | pci_disable_device(pdev); |
714 | dev_count--; | |
715 | } | |
716 | ||
717 | ||
bc17f9dc | 718 | static const struct pci_device_id wdtpci_pci_tbl[] = { |
1da177e4 LT |
719 | { |
720 | .vendor = PCI_VENDOR_ID_ACCESSIO, | |
9f2cc6f7 | 721 | .device = PCI_DEVICE_ID_ACCESSIO_WDG_CSM, |
1da177e4 LT |
722 | .subvendor = PCI_ANY_ID, |
723 | .subdevice = PCI_ANY_ID, | |
724 | }, | |
725 | { 0, }, /* terminate list */ | |
726 | }; | |
727 | MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl); | |
728 | ||
729 | ||
730 | static struct pci_driver wdtpci_driver = { | |
731 | .name = "wdt_pci", | |
732 | .id_table = wdtpci_pci_tbl, | |
733 | .probe = wdtpci_init_one, | |
82268714 | 734 | .remove = wdtpci_remove_one, |
1da177e4 LT |
735 | }; |
736 | ||
5ce9c371 | 737 | module_pci_driver(wdtpci_driver); |
1da177e4 LT |
738 | |
739 | MODULE_AUTHOR("JP Nollmann, Alan Cox"); | |
740 | MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards"); | |
741 | MODULE_LICENSE("GPL"); |