blob: 7b22e3cdbc81685a4dbd59ba9daa4e536ca4572d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Industrial Computer Source PCI-WDT500/501 driver
3 *
Alan Cox29fa0582008-10-27 15:17:56 +00004 * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>,
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +00005 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
15 *
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
17 *
18 * Release 0.10.
19 *
20 * Fixes
21 * Dave Gregorich : Modularisation and minor bugs
22 * Alan Cox : Added the watchdog ioctl() stuff
23 * Alan Cox : Fixed the reboot problem (as noted by
24 * Matt Crocker).
25 * Alan Cox : Added wdt= boot option
26 * Alan Cox : Cleaned up copy/user stuff
27 * Tim Hockin : Added insmod parameters, comment cleanup
28 * Parameterized timeout
29 * JP Nollmann : Added support for PCI wdt501p
30 * Alan Cox : Split ISA and PCI cards into two drivers
31 * Jeff Garzik : PCI cleanups
Alan Cox9f2d1f02008-08-04 17:55:35 +010032 * Tigran Aivazian : Restructured wdtpci_init_one() to handle
33 * failures
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Joel Becker : Added WDIOC_GET/SETTIMEOUT
Alan Cox9f2d1f02008-08-04 17:55:35 +010035 * Zwane Mwaikambo : Magic char closing, locking changes,
36 * cleanups
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Matt Domsch : nowayout module option
38 */
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/interrupt.h>
41#include <linux/module.h>
42#include <linux/moduleparam.h>
43#include <linux/types.h>
44#include <linux/miscdevice.h>
45#include <linux/watchdog.h>
46#include <linux/ioport.h>
Andrew Morton81830062008-08-04 17:56:02 +010047#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/notifier.h>
49#include <linux/reboot.h>
50#include <linux/init.h>
51#include <linux/fs.h>
52#include <linux/pci.h>
Alan Cox9f2d1f02008-08-04 17:55:35 +010053#include <linux/io.h>
54#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/system.h>
57
58#define WDT_IS_PCI
59#include "wd501p.h"
60
61#define PFX "wdt_pci: "
62
63/*
64 * Until Access I/O gets their application for a PCI vendor ID approved,
65 * I don't think that it's appropriate to move these constants into the
66 * regular pci_ids.h file. -- JPN 2000/01/18
67 */
68
69#ifndef PCI_VENDOR_ID_ACCESSIO
70#define PCI_VENDOR_ID_ACCESSIO 0x494f
71#endif
72#ifndef PCI_DEVICE_ID_WDG_CSM
73#define PCI_DEVICE_ID_WDG_CSM 0x22c0
74#endif
75
76/* We can only use 1 card due to the /dev/watchdog restriction */
77static int dev_count;
78
Alan Cox9f2d1f02008-08-04 17:55:35 +010079static unsigned long open_lock;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070080static DEFINE_SPINLOCK(wdtpci_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static char expect_close;
82
Randy Dunlapd1833c22009-08-07 15:02:00 -070083static resource_size_t io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static int irq;
85
86/* Default timeout */
87#define WD_TIMO 60 /* Default heartbeat = 60 seconds */
88
89static int heartbeat = WD_TIMO;
90static int wd_heartbeat;
91module_param(heartbeat, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010092MODULE_PARM_DESC(heartbeat,
93 "Watchdog heartbeat in seconds. (0<heartbeat<65536, default="
94 __MODULE_STRING(WD_TIMO) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrey Panin4bfdf372005-07-27 11:43:58 -070096static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(nowayout, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010098MODULE_PARM_DESC(nowayout,
99 "Watchdog cannot be stopped once started (default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* Support for the Fan Tachometer on the PCI-WDT501 */
103static int tachometer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104module_param(tachometer, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100105MODULE_PARM_DESC(tachometer,
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000106 "PCI-WDT501 Fan Tachometer support (0=disable, default=0)");
107
108static int type = 500;
109module_param(type, int, 0);
110MODULE_PARM_DESC(type,
111 "PCI-WDT501 Card type (500 or 501 , default=500)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/*
114 * Programming support
115 */
116
117static void wdtpci_ctr_mode(int ctr, int mode)
118{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100119 ctr <<= 6;
120 ctr |= 0x30;
121 ctr |= (mode << 1);
122 outb(ctr, WDT_CR);
123 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static void wdtpci_ctr_load(int ctr, int val)
127{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100128 outb(val & 0xFF, WDT_COUNT0 + ctr);
129 udelay(8);
130 outb(val >> 8, WDT_COUNT0 + ctr);
131 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/**
135 * wdtpci_start:
136 *
137 * Start the watchdog driver.
138 */
139
140static int wdtpci_start(void)
141{
142 unsigned long flags;
143
144 spin_lock_irqsave(&wdtpci_lock, flags);
145
146 /*
147 * "pet" the watchdog, as Access says.
148 * This resets the clock outputs.
149 */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100150 inb(WDT_DC); /* Disable watchdog */
151 udelay(8);
152 wdtpci_ctr_mode(2, 0); /* Program CTR2 for Mode 0:
153 Pulse on Terminal Count */
154 outb(0, WDT_DC); /* Enable watchdog */
155 udelay(8);
156 inb(WDT_DC); /* Disable watchdog */
157 udelay(8);
158 outb(0, WDT_CLOCK); /* 2.0833MHz clock */
159 udelay(8);
160 inb(WDT_BUZZER); /* disable */
161 udelay(8);
162 inb(WDT_OPTONOTRST); /* disable */
163 udelay(8);
164 inb(WDT_OPTORST); /* disable */
165 udelay(8);
166 inb(WDT_PROGOUT); /* disable */
167 udelay(8);
168 wdtpci_ctr_mode(0, 3); /* Program CTR0 for Mode 3:
169 Square Wave Generator */
170 wdtpci_ctr_mode(1, 2); /* Program CTR1 for Mode 2:
171 Rate Generator */
172 wdtpci_ctr_mode(2, 1); /* Program CTR2 for Mode 1:
173 Retriggerable One-Shot */
174 wdtpci_ctr_load(0, 20833); /* count at 100Hz */
175 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100177 outb(0, WDT_DC); /* Enable watchdog */
178 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 spin_unlock_irqrestore(&wdtpci_lock, flags);
181 return 0;
182}
183
184/**
185 * wdtpci_stop:
186 *
187 * Stop the watchdog driver.
188 */
189
Alan Cox9f2d1f02008-08-04 17:55:35 +0100190static int wdtpci_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 unsigned long flags;
193
194 /* Turn the card off */
195 spin_lock_irqsave(&wdtpci_lock, flags);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100196 inb(WDT_DC); /* Disable watchdog */
197 udelay(8);
198 wdtpci_ctr_load(2, 0); /* 0 length reset pulses now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_unlock_irqrestore(&wdtpci_lock, flags);
200 return 0;
201}
202
203/**
204 * wdtpci_ping:
205 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100206 * Reload counter one with the watchdog heartbeat. We don't bother
207 * reloading the cascade counter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
209
210static int wdtpci_ping(void)
211{
212 unsigned long flags;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 spin_lock_irqsave(&wdtpci_lock, flags);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100215 /* Write a watchdog value */
216 inb(WDT_DC); /* Disable watchdog */
217 udelay(8);
218 wdtpci_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2:
219 Rate Generator */
220 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
221 outb(0, WDT_DC); /* Enable watchdog */
222 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 spin_unlock_irqrestore(&wdtpci_lock, flags);
224 return 0;
225}
226
227/**
228 * wdtpci_set_heartbeat:
229 * @t: the new heartbeat value that needs to be set.
230 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100231 * Set a new heartbeat value for the watchdog device. If the heartbeat
232 * value is incorrect we keep the old value and return -EINVAL.
233 * If successful we return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
235static int wdtpci_set_heartbeat(int t)
236{
237 /* Arbitrary, can't find the card's limits */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100238 if (t < 1 || t > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -EINVAL;
240
241 heartbeat = t;
242 wd_heartbeat = t * 100;
243 return 0;
244}
245
246/**
247 * wdtpci_get_status:
248 * @status: the new status.
249 *
250 * Extract the status information from a WDT watchdog device. There are
251 * several board variants so we have to know which bits are valid. Some
252 * bits default to one and some to zero in order to be maximally painful.
253 *
254 * we then map the bits onto the status ioctl flags.
255 */
256
257static int wdtpci_get_status(int *status)
258{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100259 unsigned char new_status;
260 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Alan Cox9f2d1f02008-08-04 17:55:35 +0100262 spin_lock_irqsave(&wdtpci_lock, flags);
263 new_status = inb(WDT_SR);
264 spin_unlock_irqrestore(&wdtpci_lock, flags);
265
266 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (new_status & WDC_SR_ISOI0)
268 *status |= WDIOF_EXTERN1;
269 if (new_status & WDC_SR_ISII1)
270 *status |= WDIOF_EXTERN2;
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000271 if (type == 501) {
272 if (!(new_status & WDC_SR_TGOOD))
273 *status |= WDIOF_OVERHEAT;
274 if (!(new_status & WDC_SR_PSUOVER))
275 *status |= WDIOF_POWEROVER;
276 if (!(new_status & WDC_SR_PSUUNDR))
277 *status |= WDIOF_POWERUNDER;
278 if (tachometer) {
279 if (!(new_status & WDC_SR_FANGOOD))
280 *status |= WDIOF_FANFAULT;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/**
287 * wdtpci_get_temperature:
288 *
289 * Reports the temperature in degrees Fahrenheit. The API is in
290 * farenheit. It was designed by an imperial measurement luddite.
291 */
292
293static int wdtpci_get_temperature(int *temperature)
294{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100295 unsigned short c;
296 unsigned long flags;
297 spin_lock_irqsave(&wdtpci_lock, flags);
298 c = inb(WDT_RT);
299 udelay(8);
300 spin_unlock_irqrestore(&wdtpci_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *temperature = (c * 11 / 15) + 7;
302 return 0;
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305/**
306 * wdtpci_interrupt:
307 * @irq: Interrupt number
308 * @dev_id: Unused as we don't allow multiple devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
310 * Handle an interrupt from the board. These are raised when the status
311 * map changes in what the board considers an interesting way. That means
312 * a failure condition occurring.
313 */
314
David Howells7d12e782006-10-05 14:55:46 +0100315static irqreturn_t wdtpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 /*
318 * Read the status register see what is up and
319 * then printk it.
320 */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100321 unsigned char status;
322
323 spin_lock(&wdtpci_lock);
324
325 status = inb(WDT_SR);
326 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 printk(KERN_CRIT PFX "status %d\n", status);
329
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000330 if (type == 501) {
331 if (!(status & WDC_SR_TGOOD)) {
332 printk(KERN_CRIT PFX "Overheat alarm.(%d)\n",
333 inb(WDT_RT));
334 udelay(8);
335 }
336 if (!(status & WDC_SR_PSUOVER))
337 printk(KERN_CRIT PFX "PSU over voltage.\n");
338 if (!(status & WDC_SR_PSUUNDR))
339 printk(KERN_CRIT PFX "PSU under voltage.\n");
340 if (tachometer) {
341 if (!(status & WDC_SR_FANGOOD))
342 printk(KERN_CRIT PFX "Possible fan fault.\n");
343 }
Alan Cox9f2d1f02008-08-04 17:55:35 +0100344 }
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000345 if (!(status & WDC_SR_WCCR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346#ifdef SOFTWARE_REBOOT
347#ifdef ONLY_TESTING
348 printk(KERN_CRIT PFX "Would Reboot.\n");
349#else
350 printk(KERN_CRIT PFX "Initiating system reboot.\n");
Eric W. Biedermanf82567e2005-07-26 11:53:19 -0600351 emergency_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#endif
353#else
354 printk(KERN_CRIT PFX "Reset in 5ms.\n");
355#endif
Ilpo Jarvinen59338d42007-10-23 13:40:54 -0700356 }
Alan Cox9f2d1f02008-08-04 17:55:35 +0100357 spin_unlock(&wdtpci_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return IRQ_HANDLED;
359}
360
361
362/**
363 * wdtpci_write:
364 * @file: file handle to the watchdog
365 * @buf: buffer to write (unused as data does not matter here
366 * @count: count of bytes
367 * @ppos: pointer to the position to write. No seeks allowed
368 *
369 * A write to a watchdog device is defined as a keepalive signal. Any
370 * write of data will do, as we we don't define content meaning.
371 */
372
Alan Cox9f2d1f02008-08-04 17:55:35 +0100373static ssize_t wdtpci_write(struct file *file, const char __user *buf,
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000374 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 if (count) {
377 if (!nowayout) {
378 size_t i;
379
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000380 /* In case it was set long ago */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 expect_close = 0;
382
383 for (i = 0; i != count; i++) {
384 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000385 if (get_user(c, buf + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return -EFAULT;
387 if (c == 'V')
388 expect_close = 42;
389 }
390 }
391 wdtpci_ping();
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return count;
394}
395
396/**
397 * wdtpci_ioctl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * @file: file handle to the device
399 * @cmd: watchdog command
400 * @arg: argument pointer
401 *
402 * The watchdog API defines a common set of functions for all watchdogs
403 * according to their available features. We only actually usefully support
404 * querying capabilities and current status.
405 */
406
Alan Cox9f2d1f02008-08-04 17:55:35 +0100407static long wdtpci_ioctl(struct file *file, unsigned int cmd,
408 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 void __user *argp = (void __user *)arg;
411 int __user *p = argp;
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000412 int new_heartbeat;
413 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Andrew Mortonc1bf3ac2010-03-03 11:57:40 -0800415 struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 .options = WDIOF_SETTIMEOUT|
417 WDIOF_MAGICCLOSE|
418 WDIOF_KEEPALIVEPING,
419 .firmware_version = 1,
420 .identity = "PCI-WDT500/501",
421 };
422
423 /* Add options according to the card we have */
424 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000425 if (type == 501) {
426 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
427 WDIOF_POWEROVER);
428 if (tachometer)
429 ident.options |= WDIOF_FANFAULT;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Alan Cox9f2d1f02008-08-04 17:55:35 +0100432 switch (cmd) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100433 case WDIOC_GETSUPPORT:
434 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
435 case WDIOC_GETSTATUS:
436 wdtpci_get_status(&status);
437 return put_user(status, p);
438 case WDIOC_GETBOOTSTATUS:
439 return put_user(0, p);
440 case WDIOC_KEEPALIVE:
441 wdtpci_ping();
442 return 0;
443 case WDIOC_SETTIMEOUT:
444 if (get_user(new_heartbeat, p))
445 return -EFAULT;
446 if (wdtpci_set_heartbeat(new_heartbeat))
447 return -EINVAL;
448 wdtpci_ping();
449 /* Fall */
450 case WDIOC_GETTIMEOUT:
451 return put_user(heartbeat, p);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000452 default:
453 return -ENOTTY;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/**
458 * wdtpci_open:
459 * @inode: inode of device
460 * @file: file handle to device
461 *
462 * The watchdog device has been opened. The watchdog device is single
463 * open and on opening we load the counters. Counter zero is a 100Hz
464 * cascade, into counter 1 which downcounts to reboot. When the counter
465 * triggers counter 2 downcounts the length of the reset pulse which
466 * set set to be as long as possible.
467 */
468
469static int wdtpci_open(struct inode *inode, struct file *file)
470{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100471 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EBUSY;
473
Alan Cox9f2d1f02008-08-04 17:55:35 +0100474 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /*
477 * Activate
478 */
479 wdtpci_start();
480 return nonseekable_open(inode, file);
481}
482
483/**
484 * wdtpci_release:
485 * @inode: inode to board
486 * @file: file handle to board
487 *
488 * The watchdog has a configurable API. There is a religious dispute
489 * between people who want their watchdog to be able to shut down and
490 * those who want to be sure if the watchdog manager dies the machine
491 * reboots. In the former case we disable the counters, in the latter
492 * case you have to open it again very soon.
493 */
494
495static int wdtpci_release(struct inode *inode, struct file *file)
496{
497 if (expect_close == 42) {
498 wdtpci_stop();
499 } else {
500 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
501 wdtpci_ping();
502 }
503 expect_close = 0;
Alan Cox9f2d1f02008-08-04 17:55:35 +0100504 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * wdtpci_temp_read:
510 * @file: file handle to the watchdog board
511 * @buf: buffer to write 1 byte into
512 * @count: length of buffer
513 * @ptr: offset (no seek allowed)
514 *
515 * Read reports the temperature in degrees Fahrenheit. The API is in
516 * fahrenheit. It was designed by an imperial measurement luddite.
517 */
518
Alan Cox9f2d1f02008-08-04 17:55:35 +0100519static ssize_t wdtpci_temp_read(struct file *file, char __user *buf,
520 size_t count, loff_t *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 int temperature;
523
524 if (wdtpci_get_temperature(&temperature))
525 return -EFAULT;
526
Alan Cox9f2d1f02008-08-04 17:55:35 +0100527 if (copy_to_user(buf, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EFAULT;
529
530 return 1;
531}
532
533/**
534 * wdtpci_temp_open:
535 * @inode: inode of device
536 * @file: file handle to device
537 *
538 * The temperature device has been opened.
539 */
540
541static int wdtpci_temp_open(struct inode *inode, struct file *file)
542{
543 return nonseekable_open(inode, file);
544}
545
546/**
547 * wdtpci_temp_release:
548 * @inode: inode to board
549 * @file: file handle to board
550 *
551 * The temperature device has been closed.
552 */
553
554static int wdtpci_temp_release(struct inode *inode, struct file *file)
555{
556 return 0;
557}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559/**
560 * notify_sys:
561 * @this: our notifier block
562 * @code: the event being reported
563 * @unused: unused
564 *
565 * Our notifier is called on system shutdowns. We want to turn the card
566 * off at reboot otherwise the machine will reboot again during memory
567 * test or worse yet during the following fsck. This would suck, in fact
568 * trust me - if it happens it does suck.
569 */
570
571static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
Alan Cox9f2d1f02008-08-04 17:55:35 +0100572 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100574 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 wdtpci_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NOTIFY_DONE;
577}
578
579/*
580 * Kernel Interfaces
581 */
582
583
Arjan van de Ven62322d22006-07-03 00:24:21 -0700584static const struct file_operations wdtpci_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 .owner = THIS_MODULE,
586 .llseek = no_llseek,
587 .write = wdtpci_write,
Alan Cox9f2d1f02008-08-04 17:55:35 +0100588 .unlocked_ioctl = wdtpci_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 .open = wdtpci_open,
590 .release = wdtpci_release,
591};
592
593static struct miscdevice wdtpci_miscdev = {
594 .minor = WATCHDOG_MINOR,
595 .name = "watchdog",
596 .fops = &wdtpci_fops,
597};
598
Arjan van de Ven62322d22006-07-03 00:24:21 -0700599static const struct file_operations wdtpci_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .owner = THIS_MODULE,
601 .llseek = no_llseek,
602 .read = wdtpci_temp_read,
603 .open = wdtpci_temp_open,
604 .release = wdtpci_temp_release,
605};
606
607static struct miscdevice temp_miscdev = {
608 .minor = TEMP_MINOR,
609 .name = "temperature",
610 .fops = &wdtpci_temp_fops,
611};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613/*
614 * The WDT card needs to learn about soft shutdowns in order to
615 * turn the timebomb registers off.
616 */
617
618static struct notifier_block wdtpci_notifier = {
619 .notifier_call = wdtpci_notify_sys,
620};
621
622
Alan Cox9f2d1f02008-08-04 17:55:35 +0100623static int __devinit wdtpci_init_one(struct pci_dev *dev,
624 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int ret = -EIO;
627
628 dev_count++;
629 if (dev_count > 1) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100630 printk(KERN_ERR PFX "This driver only supports one device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return -ENODEV;
632 }
633
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000634 if (type != 500 && type != 501) {
635 printk(KERN_ERR PFX "unknown card type '%d'.\n", type);
636 return -ENODEV;
637 }
638
Alan Cox9f2d1f02008-08-04 17:55:35 +0100639 if (pci_enable_device(dev)) {
640 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -ENODEV;
642 }
643
Alan Cox9f2d1f02008-08-04 17:55:35 +0100644 if (pci_resource_start(dev, 2) == 0x0000) {
645 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ret = -ENODEV;
647 goto out_pci;
648 }
649
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000650 if (pci_request_region(dev, 2, "wdt_pci")) {
Randy Dunlapd1833c22009-08-07 15:02:00 -0700651 printk(KERN_ERR PFX "I/O address 0x%llx already in use\n",
652 (unsigned long long)pci_resource_start(dev, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 goto out_pci;
654 }
655
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000656 irq = dev->irq;
657 io = pci_resource_start(dev, 2);
658
Alan Cox9f2d1f02008-08-04 17:55:35 +0100659 if (request_irq(irq, wdtpci_interrupt, IRQF_DISABLED | IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 "wdt_pci", &wdtpci_miscdev)) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100661 printk(KERN_ERR PFX "IRQ %d is not free\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto out_reg;
663 }
664
Alan Cox9f2d1f02008-08-04 17:55:35 +0100665 printk(KERN_INFO
Randy Dunlapd1833c22009-08-07 15:02:00 -0700666 "PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%llx (Interrupt %d)\n",
667 (unsigned long long)io, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Alan Cox9f2d1f02008-08-04 17:55:35 +0100669 /* Check that the heartbeat value is within its range;
670 if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (wdtpci_set_heartbeat(heartbeat)) {
672 wdtpci_set_heartbeat(WD_TIMO);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100673 printk(KERN_INFO PFX
674 "heartbeat value must be 0 < heartbeat < 65536, using %d\n",
675 WD_TIMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
Alan Cox9f2d1f02008-08-04 17:55:35 +0100678 ret = register_reboot_notifier(&wdtpci_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ret) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100680 printk(KERN_ERR PFX
681 "cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto out_irq;
683 }
684
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000685 if (type == 501) {
686 ret = misc_register(&temp_miscdev);
687 if (ret) {
688 printk(KERN_ERR PFX
Alan Cox9f2d1f02008-08-04 17:55:35 +0100689 "cannot register miscdev on minor=%d (err=%d)\n",
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000690 TEMP_MINOR, ret);
691 goto out_rbt;
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Alan Cox9f2d1f02008-08-04 17:55:35 +0100695 ret = misc_register(&wdtpci_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (ret) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100697 printk(KERN_ERR PFX
698 "cannot register miscdev on minor=%d (err=%d)\n",
699 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out_misc;
701 }
702
703 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
704 heartbeat, nowayout);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000705 if (type == 501)
706 printk(KERN_INFO "wdt: Fan Tachometer is %s\n",
Alan Cox9f2d1f02008-08-04 17:55:35 +0100707 (tachometer ? "Enabled" : "Disabled"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 ret = 0;
710out:
711 return ret;
712
713out_misc:
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000714 if (type == 501)
715 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716out_rbt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unregister_reboot_notifier(&wdtpci_notifier);
718out_irq:
719 free_irq(irq, &wdtpci_miscdev);
720out_reg:
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000721 pci_release_region(dev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722out_pci:
723 pci_disable_device(dev);
724 goto out;
725}
726
727
Alan Cox9f2d1f02008-08-04 17:55:35 +0100728static void __devexit wdtpci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 /* here we assume only one device will ever have
731 * been picked up and registered by probe function */
732 misc_deregister(&wdtpci_miscdev);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000733 if (type == 501)
734 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unregister_reboot_notifier(&wdtpci_notifier);
736 free_irq(irq, &wdtpci_miscdev);
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000737 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 pci_disable_device(pdev);
739 dev_count--;
740}
741
742
743static struct pci_device_id wdtpci_pci_tbl[] = {
744 {
745 .vendor = PCI_VENDOR_ID_ACCESSIO,
746 .device = PCI_DEVICE_ID_WDG_CSM,
747 .subvendor = PCI_ANY_ID,
748 .subdevice = PCI_ANY_ID,
749 },
750 { 0, }, /* terminate list */
751};
752MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
753
754
755static struct pci_driver wdtpci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .name = "wdt_pci",
757 .id_table = wdtpci_pci_tbl,
758 .probe = wdtpci_init_one,
759 .remove = __devexit_p(wdtpci_remove_one),
760};
761
762
763/**
764 * wdtpci_cleanup:
765 *
766 * Unload the watchdog. You cannot do this with any file handles open.
767 * If your watchdog is set to continue ticking on close and you unload
768 * it, well it keeps ticking. We won't get the interrupt but the board
769 * will not touch PC memory so all is fine. You just have to load a new
770 * module in xx seconds or reboot.
771 */
772
773static void __exit wdtpci_cleanup(void)
774{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100775 pci_unregister_driver(&wdtpci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778
779/**
780 * wdtpci_init:
781 *
782 * Set up the WDT watchdog board. All we have to do is grab the
783 * resources we require and bitch if anyone beat us to them.
784 * The open() function will actually kick the board off.
785 */
786
787static int __init wdtpci_init(void)
788{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100789 return pci_register_driver(&wdtpci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792
793module_init(wdtpci_init);
794module_exit(wdtpci_cleanup);
795
796MODULE_AUTHOR("JP Nollmann, Alan Cox");
797MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
798MODULE_LICENSE("GPL");
799MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
800MODULE_ALIAS_MISCDEV(TEMP_MINOR);