blob: 6130c88fa5acac85387d0544c7ea53b7d92fca68 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* We can only use 1 card due to the /dev/watchdog restriction */
64static int dev_count;
65
Alan Cox9f2d1f02008-08-04 17:55:35 +010066static unsigned long open_lock;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -070067static DEFINE_SPINLOCK(wdtpci_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static char expect_close;
69
Randy Dunlapd1833c22009-08-07 15:02:00 -070070static resource_size_t io;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int irq;
72
73/* Default timeout */
74#define WD_TIMO 60 /* Default heartbeat = 60 seconds */
75
76static int heartbeat = WD_TIMO;
77static int wd_heartbeat;
78module_param(heartbeat, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010079MODULE_PARM_DESC(heartbeat,
80 "Watchdog heartbeat in seconds. (0<heartbeat<65536, default="
81 __MODULE_STRING(WD_TIMO) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Andrey Panin4bfdf372005-07-27 11:43:58 -070083static int nowayout = WATCHDOG_NOWAYOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084module_param(nowayout, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010085MODULE_PARM_DESC(nowayout,
86 "Watchdog cannot be stopped once started (default="
87 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Support for the Fan Tachometer on the PCI-WDT501 */
90static int tachometer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091module_param(tachometer, int, 0);
Alan Cox9f2d1f02008-08-04 17:55:35 +010092MODULE_PARM_DESC(tachometer,
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +000093 "PCI-WDT501 Fan Tachometer support (0=disable, default=0)");
94
95static int type = 500;
96module_param(type, int, 0);
97MODULE_PARM_DESC(type,
98 "PCI-WDT501 Card type (500 or 501 , default=500)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Programming support
102 */
103
104static void wdtpci_ctr_mode(int ctr, int mode)
105{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100106 ctr <<= 6;
107 ctr |= 0x30;
108 ctr |= (mode << 1);
109 outb(ctr, WDT_CR);
110 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113static void wdtpci_ctr_load(int ctr, int val)
114{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100115 outb(val & 0xFF, WDT_COUNT0 + ctr);
116 udelay(8);
117 outb(val >> 8, WDT_COUNT0 + ctr);
118 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/**
122 * wdtpci_start:
123 *
124 * Start the watchdog driver.
125 */
126
127static int wdtpci_start(void)
128{
129 unsigned long flags;
130
131 spin_lock_irqsave(&wdtpci_lock, flags);
132
133 /*
134 * "pet" the watchdog, as Access says.
135 * This resets the clock outputs.
136 */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100137 inb(WDT_DC); /* Disable watchdog */
138 udelay(8);
139 wdtpci_ctr_mode(2, 0); /* Program CTR2 for Mode 0:
140 Pulse on Terminal Count */
141 outb(0, WDT_DC); /* Enable watchdog */
142 udelay(8);
143 inb(WDT_DC); /* Disable watchdog */
144 udelay(8);
145 outb(0, WDT_CLOCK); /* 2.0833MHz clock */
146 udelay(8);
147 inb(WDT_BUZZER); /* disable */
148 udelay(8);
149 inb(WDT_OPTONOTRST); /* disable */
150 udelay(8);
151 inb(WDT_OPTORST); /* disable */
152 udelay(8);
153 inb(WDT_PROGOUT); /* disable */
154 udelay(8);
155 wdtpci_ctr_mode(0, 3); /* Program CTR0 for Mode 3:
156 Square Wave Generator */
157 wdtpci_ctr_mode(1, 2); /* Program CTR1 for Mode 2:
158 Rate Generator */
159 wdtpci_ctr_mode(2, 1); /* Program CTR2 for Mode 1:
160 Retriggerable One-Shot */
161 wdtpci_ctr_load(0, 20833); /* count at 100Hz */
162 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100164 outb(0, WDT_DC); /* Enable watchdog */
165 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 spin_unlock_irqrestore(&wdtpci_lock, flags);
168 return 0;
169}
170
171/**
172 * wdtpci_stop:
173 *
174 * Stop the watchdog driver.
175 */
176
Alan Cox9f2d1f02008-08-04 17:55:35 +0100177static int wdtpci_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 unsigned long flags;
180
181 /* Turn the card off */
182 spin_lock_irqsave(&wdtpci_lock, flags);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100183 inb(WDT_DC); /* Disable watchdog */
184 udelay(8);
185 wdtpci_ctr_load(2, 0); /* 0 length reset pulses now */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 spin_unlock_irqrestore(&wdtpci_lock, flags);
187 return 0;
188}
189
190/**
191 * wdtpci_ping:
192 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100193 * Reload counter one with the watchdog heartbeat. We don't bother
194 * reloading the cascade counter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196
197static int wdtpci_ping(void)
198{
199 unsigned long flags;
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 spin_lock_irqsave(&wdtpci_lock, flags);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100202 /* Write a watchdog value */
203 inb(WDT_DC); /* Disable watchdog */
204 udelay(8);
205 wdtpci_ctr_mode(1, 2); /* Re-Program CTR1 for Mode 2:
206 Rate Generator */
207 wdtpci_ctr_load(1, wd_heartbeat);/* Heartbeat */
208 outb(0, WDT_DC); /* Enable watchdog */
209 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 spin_unlock_irqrestore(&wdtpci_lock, flags);
211 return 0;
212}
213
214/**
215 * wdtpci_set_heartbeat:
216 * @t: the new heartbeat value that needs to be set.
217 *
Alan Cox9f2d1f02008-08-04 17:55:35 +0100218 * Set a new heartbeat value for the watchdog device. If the heartbeat
219 * value is incorrect we keep the old value and return -EINVAL.
220 * If successful we return 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
222static int wdtpci_set_heartbeat(int t)
223{
224 /* Arbitrary, can't find the card's limits */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100225 if (t < 1 || t > 65535)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EINVAL;
227
228 heartbeat = t;
229 wd_heartbeat = t * 100;
230 return 0;
231}
232
233/**
234 * wdtpci_get_status:
235 * @status: the new status.
236 *
237 * Extract the status information from a WDT watchdog device. There are
238 * several board variants so we have to know which bits are valid. Some
239 * bits default to one and some to zero in order to be maximally painful.
240 *
241 * we then map the bits onto the status ioctl flags.
242 */
243
244static int wdtpci_get_status(int *status)
245{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100246 unsigned char new_status;
247 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Alan Cox9f2d1f02008-08-04 17:55:35 +0100249 spin_lock_irqsave(&wdtpci_lock, flags);
250 new_status = inb(WDT_SR);
251 spin_unlock_irqrestore(&wdtpci_lock, flags);
252
253 *status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (new_status & WDC_SR_ISOI0)
255 *status |= WDIOF_EXTERN1;
256 if (new_status & WDC_SR_ISII1)
257 *status |= WDIOF_EXTERN2;
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000258 if (type == 501) {
259 if (!(new_status & WDC_SR_TGOOD))
260 *status |= WDIOF_OVERHEAT;
261 if (!(new_status & WDC_SR_PSUOVER))
262 *status |= WDIOF_POWEROVER;
263 if (!(new_status & WDC_SR_PSUUNDR))
264 *status |= WDIOF_POWERUNDER;
265 if (tachometer) {
266 if (!(new_status & WDC_SR_FANGOOD))
267 *status |= WDIOF_FANFAULT;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273/**
274 * wdtpci_get_temperature:
275 *
276 * Reports the temperature in degrees Fahrenheit. The API is in
277 * farenheit. It was designed by an imperial measurement luddite.
278 */
279
280static int wdtpci_get_temperature(int *temperature)
281{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100282 unsigned short c;
283 unsigned long flags;
284 spin_lock_irqsave(&wdtpci_lock, flags);
285 c = inb(WDT_RT);
286 udelay(8);
287 spin_unlock_irqrestore(&wdtpci_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *temperature = (c * 11 / 15) + 7;
289 return 0;
290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
293 * wdtpci_interrupt:
294 * @irq: Interrupt number
295 * @dev_id: Unused as we don't allow multiple devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 *
297 * Handle an interrupt from the board. These are raised when the status
298 * map changes in what the board considers an interesting way. That means
299 * a failure condition occurring.
300 */
301
David Howells7d12e782006-10-05 14:55:46 +0100302static irqreturn_t wdtpci_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 /*
305 * Read the status register see what is up and
306 * then printk it.
307 */
Alan Cox9f2d1f02008-08-04 17:55:35 +0100308 unsigned char status;
309
310 spin_lock(&wdtpci_lock);
311
312 status = inb(WDT_SR);
313 udelay(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 printk(KERN_CRIT PFX "status %d\n", status);
316
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000317 if (type == 501) {
318 if (!(status & WDC_SR_TGOOD)) {
319 printk(KERN_CRIT PFX "Overheat alarm.(%d)\n",
320 inb(WDT_RT));
321 udelay(8);
322 }
323 if (!(status & WDC_SR_PSUOVER))
324 printk(KERN_CRIT PFX "PSU over voltage.\n");
325 if (!(status & WDC_SR_PSUUNDR))
326 printk(KERN_CRIT PFX "PSU under voltage.\n");
327 if (tachometer) {
328 if (!(status & WDC_SR_FANGOOD))
329 printk(KERN_CRIT PFX "Possible fan fault.\n");
330 }
Alan Cox9f2d1f02008-08-04 17:55:35 +0100331 }
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000332 if (!(status & WDC_SR_WCCR)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333#ifdef SOFTWARE_REBOOT
334#ifdef ONLY_TESTING
335 printk(KERN_CRIT PFX "Would Reboot.\n");
336#else
337 printk(KERN_CRIT PFX "Initiating system reboot.\n");
Eric W. Biedermanf82567e2005-07-26 11:53:19 -0600338 emergency_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#endif
340#else
341 printk(KERN_CRIT PFX "Reset in 5ms.\n");
342#endif
Ilpo Jarvinen59338d42007-10-23 13:40:54 -0700343 }
Alan Cox9f2d1f02008-08-04 17:55:35 +0100344 spin_unlock(&wdtpci_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return IRQ_HANDLED;
346}
347
348
349/**
350 * wdtpci_write:
351 * @file: file handle to the watchdog
352 * @buf: buffer to write (unused as data does not matter here
353 * @count: count of bytes
354 * @ppos: pointer to the position to write. No seeks allowed
355 *
356 * A write to a watchdog device is defined as a keepalive signal. Any
357 * write of data will do, as we we don't define content meaning.
358 */
359
Alan Cox9f2d1f02008-08-04 17:55:35 +0100360static ssize_t wdtpci_write(struct file *file, const char __user *buf,
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000361 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 if (count) {
364 if (!nowayout) {
365 size_t i;
366
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000367 /* In case it was set long ago */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 expect_close = 0;
369
370 for (i = 0; i != count; i++) {
371 char c;
Wim Van Sebroeck7944d3a2008-08-06 20:19:41 +0000372 if (get_user(c, buf + i))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EFAULT;
374 if (c == 'V')
375 expect_close = 42;
376 }
377 }
378 wdtpci_ping();
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return count;
381}
382
383/**
384 * wdtpci_ioctl:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 * @file: file handle to the device
386 * @cmd: watchdog command
387 * @arg: argument pointer
388 *
389 * The watchdog API defines a common set of functions for all watchdogs
390 * according to their available features. We only actually usefully support
391 * querying capabilities and current status.
392 */
393
Alan Cox9f2d1f02008-08-04 17:55:35 +0100394static long wdtpci_ioctl(struct file *file, unsigned int cmd,
395 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 void __user *argp = (void __user *)arg;
398 int __user *p = argp;
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000399 int new_heartbeat;
400 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Andrew Mortonc1bf3ac2010-03-03 11:57:40 -0800402 struct watchdog_info ident = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .options = WDIOF_SETTIMEOUT|
404 WDIOF_MAGICCLOSE|
405 WDIOF_KEEPALIVEPING,
406 .firmware_version = 1,
407 .identity = "PCI-WDT500/501",
408 };
409
410 /* Add options according to the card we have */
411 ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000412 if (type == 501) {
413 ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|
414 WDIOF_POWEROVER);
415 if (tachometer)
416 ident.options |= WDIOF_FANFAULT;
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alan Cox9f2d1f02008-08-04 17:55:35 +0100419 switch (cmd) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100420 case WDIOC_GETSUPPORT:
421 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
422 case WDIOC_GETSTATUS:
423 wdtpci_get_status(&status);
424 return put_user(status, p);
425 case WDIOC_GETBOOTSTATUS:
426 return put_user(0, p);
427 case WDIOC_KEEPALIVE:
428 wdtpci_ping();
429 return 0;
430 case WDIOC_SETTIMEOUT:
431 if (get_user(new_heartbeat, p))
432 return -EFAULT;
433 if (wdtpci_set_heartbeat(new_heartbeat))
434 return -EINVAL;
435 wdtpci_ping();
436 /* Fall */
437 case WDIOC_GETTIMEOUT:
438 return put_user(heartbeat, p);
Wim Van Sebroeck0c06090c2008-07-18 11:41:17 +0000439 default:
440 return -ENOTTY;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/**
445 * wdtpci_open:
446 * @inode: inode of device
447 * @file: file handle to device
448 *
449 * The watchdog device has been opened. The watchdog device is single
450 * open and on opening we load the counters. Counter zero is a 100Hz
451 * cascade, into counter 1 which downcounts to reboot. When the counter
452 * triggers counter 2 downcounts the length of the reset pulse which
453 * set set to be as long as possible.
454 */
455
456static int wdtpci_open(struct inode *inode, struct file *file)
457{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100458 if (test_and_set_bit(0, &open_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -EBUSY;
460
Alan Cox9f2d1f02008-08-04 17:55:35 +0100461 if (nowayout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 __module_get(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Activate
465 */
466 wdtpci_start();
467 return nonseekable_open(inode, file);
468}
469
470/**
471 * wdtpci_release:
472 * @inode: inode to board
473 * @file: file handle to board
474 *
475 * The watchdog has a configurable API. There is a religious dispute
476 * between people who want their watchdog to be able to shut down and
477 * those who want to be sure if the watchdog manager dies the machine
478 * reboots. In the former case we disable the counters, in the latter
479 * case you have to open it again very soon.
480 */
481
482static int wdtpci_release(struct inode *inode, struct file *file)
483{
484 if (expect_close == 42) {
485 wdtpci_stop();
486 } else {
487 printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
488 wdtpci_ping();
489 }
490 expect_close = 0;
Alan Cox9f2d1f02008-08-04 17:55:35 +0100491 clear_bit(0, &open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/**
496 * wdtpci_temp_read:
497 * @file: file handle to the watchdog board
498 * @buf: buffer to write 1 byte into
499 * @count: length of buffer
500 * @ptr: offset (no seek allowed)
501 *
502 * Read reports the temperature in degrees Fahrenheit. The API is in
503 * fahrenheit. It was designed by an imperial measurement luddite.
504 */
505
Alan Cox9f2d1f02008-08-04 17:55:35 +0100506static ssize_t wdtpci_temp_read(struct file *file, char __user *buf,
507 size_t count, loff_t *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int temperature;
510
511 if (wdtpci_get_temperature(&temperature))
512 return -EFAULT;
513
Alan Cox9f2d1f02008-08-04 17:55:35 +0100514 if (copy_to_user(buf, &temperature, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EFAULT;
516
517 return 1;
518}
519
520/**
521 * wdtpci_temp_open:
522 * @inode: inode of device
523 * @file: file handle to device
524 *
525 * The temperature device has been opened.
526 */
527
528static int wdtpci_temp_open(struct inode *inode, struct file *file)
529{
530 return nonseekable_open(inode, file);
531}
532
533/**
534 * wdtpci_temp_release:
535 * @inode: inode to board
536 * @file: file handle to board
537 *
538 * The temperature device has been closed.
539 */
540
541static int wdtpci_temp_release(struct inode *inode, struct file *file)
542{
543 return 0;
544}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/**
547 * notify_sys:
548 * @this: our notifier block
549 * @code: the event being reported
550 * @unused: unused
551 *
552 * Our notifier is called on system shutdowns. We want to turn the card
553 * off at reboot otherwise the machine will reboot again during memory
554 * test or worse yet during the following fsck. This would suck, in fact
555 * trust me - if it happens it does suck.
556 */
557
558static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
Alan Cox9f2d1f02008-08-04 17:55:35 +0100559 void *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100561 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 wdtpci_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return NOTIFY_DONE;
564}
565
566/*
567 * Kernel Interfaces
568 */
569
570
Arjan van de Ven62322d22006-07-03 00:24:21 -0700571static const struct file_operations wdtpci_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 .owner = THIS_MODULE,
573 .llseek = no_llseek,
574 .write = wdtpci_write,
Alan Cox9f2d1f02008-08-04 17:55:35 +0100575 .unlocked_ioctl = wdtpci_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 .open = wdtpci_open,
577 .release = wdtpci_release,
578};
579
580static struct miscdevice wdtpci_miscdev = {
581 .minor = WATCHDOG_MINOR,
582 .name = "watchdog",
583 .fops = &wdtpci_fops,
584};
585
Arjan van de Ven62322d22006-07-03 00:24:21 -0700586static const struct file_operations wdtpci_temp_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .owner = THIS_MODULE,
588 .llseek = no_llseek,
589 .read = wdtpci_temp_read,
590 .open = wdtpci_temp_open,
591 .release = wdtpci_temp_release,
592};
593
594static struct miscdevice temp_miscdev = {
595 .minor = TEMP_MINOR,
596 .name = "temperature",
597 .fops = &wdtpci_temp_fops,
598};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600/*
601 * The WDT card needs to learn about soft shutdowns in order to
602 * turn the timebomb registers off.
603 */
604
605static struct notifier_block wdtpci_notifier = {
606 .notifier_call = wdtpci_notify_sys,
607};
608
609
Alan Cox9f2d1f02008-08-04 17:55:35 +0100610static int __devinit wdtpci_init_one(struct pci_dev *dev,
611 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 int ret = -EIO;
614
615 dev_count++;
616 if (dev_count > 1) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100617 printk(KERN_ERR PFX "This driver only supports one device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -ENODEV;
619 }
620
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000621 if (type != 500 && type != 501) {
622 printk(KERN_ERR PFX "unknown card type '%d'.\n", type);
623 return -ENODEV;
624 }
625
Alan Cox9f2d1f02008-08-04 17:55:35 +0100626 if (pci_enable_device(dev)) {
627 printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ENODEV;
629 }
630
Alan Cox9f2d1f02008-08-04 17:55:35 +0100631 if (pci_resource_start(dev, 2) == 0x0000) {
632 printk(KERN_ERR PFX "No I/O-Address for card detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ret = -ENODEV;
634 goto out_pci;
635 }
636
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000637 if (pci_request_region(dev, 2, "wdt_pci")) {
Randy Dunlapd1833c22009-08-07 15:02:00 -0700638 printk(KERN_ERR PFX "I/O address 0x%llx already in use\n",
639 (unsigned long long)pci_resource_start(dev, 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto out_pci;
641 }
642
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000643 irq = dev->irq;
644 io = pci_resource_start(dev, 2);
645
Alan Cox9f2d1f02008-08-04 17:55:35 +0100646 if (request_irq(irq, wdtpci_interrupt, IRQF_DISABLED | IRQF_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 "wdt_pci", &wdtpci_miscdev)) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100648 printk(KERN_ERR PFX "IRQ %d is not free\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto out_reg;
650 }
651
Alan Cox9f2d1f02008-08-04 17:55:35 +0100652 printk(KERN_INFO
Randy Dunlapd1833c22009-08-07 15:02:00 -0700653 "PCI-WDT500/501 (PCI-WDG-CSM) driver 0.10 at 0x%llx (Interrupt %d)\n",
654 (unsigned long long)io, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Alan Cox9f2d1f02008-08-04 17:55:35 +0100656 /* Check that the heartbeat value is within its range;
657 if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (wdtpci_set_heartbeat(heartbeat)) {
659 wdtpci_set_heartbeat(WD_TIMO);
Alan Cox9f2d1f02008-08-04 17:55:35 +0100660 printk(KERN_INFO PFX
661 "heartbeat value must be 0 < heartbeat < 65536, using %d\n",
662 WD_TIMO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Alan Cox9f2d1f02008-08-04 17:55:35 +0100665 ret = register_reboot_notifier(&wdtpci_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (ret) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100667 printk(KERN_ERR PFX
668 "cannot register reboot notifier (err=%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto out_irq;
670 }
671
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000672 if (type == 501) {
673 ret = misc_register(&temp_miscdev);
674 if (ret) {
675 printk(KERN_ERR PFX
Alan Cox9f2d1f02008-08-04 17:55:35 +0100676 "cannot register miscdev on minor=%d (err=%d)\n",
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000677 TEMP_MINOR, ret);
678 goto out_rbt;
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Alan Cox9f2d1f02008-08-04 17:55:35 +0100682 ret = misc_register(&wdtpci_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ret) {
Alan Cox9f2d1f02008-08-04 17:55:35 +0100684 printk(KERN_ERR PFX
685 "cannot register miscdev on minor=%d (err=%d)\n",
686 WATCHDOG_MINOR, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto out_misc;
688 }
689
690 printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
691 heartbeat, nowayout);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000692 if (type == 501)
693 printk(KERN_INFO "wdt: Fan Tachometer is %s\n",
Alan Cox9f2d1f02008-08-04 17:55:35 +0100694 (tachometer ? "Enabled" : "Disabled"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 ret = 0;
697out:
698 return ret;
699
700out_misc:
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000701 if (type == 501)
702 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703out_rbt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unregister_reboot_notifier(&wdtpci_notifier);
705out_irq:
706 free_irq(irq, &wdtpci_miscdev);
707out_reg:
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000708 pci_release_region(dev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709out_pci:
710 pci_disable_device(dev);
711 goto out;
712}
713
714
Alan Cox9f2d1f02008-08-04 17:55:35 +0100715static void __devexit wdtpci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 /* here we assume only one device will ever have
718 * been picked up and registered by probe function */
719 misc_deregister(&wdtpci_miscdev);
Wim Van Sebroeck9b901ee2009-06-19 09:32:57 +0000720 if (type == 501)
721 misc_deregister(&temp_miscdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unregister_reboot_notifier(&wdtpci_notifier);
723 free_irq(irq, &wdtpci_miscdev);
Wim Van Sebroeck119d3e52009-08-06 18:57:49 +0000724 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 pci_disable_device(pdev);
726 dev_count--;
727}
728
729
730static struct pci_device_id wdtpci_pci_tbl[] = {
731 {
732 .vendor = PCI_VENDOR_ID_ACCESSIO,
H Hartley Sweeten9f2cc6f2010-05-21 18:38:52 -0500733 .device = PCI_DEVICE_ID_ACCESSIO_WDG_CSM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 .subvendor = PCI_ANY_ID,
735 .subdevice = PCI_ANY_ID,
736 },
737 { 0, }, /* terminate list */
738};
739MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
740
741
742static struct pci_driver wdtpci_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .name = "wdt_pci",
744 .id_table = wdtpci_pci_tbl,
745 .probe = wdtpci_init_one,
746 .remove = __devexit_p(wdtpci_remove_one),
747};
748
749
750/**
751 * wdtpci_cleanup:
752 *
753 * Unload the watchdog. You cannot do this with any file handles open.
754 * If your watchdog is set to continue ticking on close and you unload
755 * it, well it keeps ticking. We won't get the interrupt but the board
756 * will not touch PC memory so all is fine. You just have to load a new
757 * module in xx seconds or reboot.
758 */
759
760static void __exit wdtpci_cleanup(void)
761{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100762 pci_unregister_driver(&wdtpci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765
766/**
767 * wdtpci_init:
768 *
769 * Set up the WDT watchdog board. All we have to do is grab the
770 * resources we require and bitch if anyone beat us to them.
771 * The open() function will actually kick the board off.
772 */
773
774static int __init wdtpci_init(void)
775{
Alan Cox9f2d1f02008-08-04 17:55:35 +0100776 return pci_register_driver(&wdtpci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
779
780module_init(wdtpci_init);
781module_exit(wdtpci_cleanup);
782
783MODULE_AUTHOR("JP Nollmann, Alan Cox");
784MODULE_DESCRIPTION("Driver for the ICS PCI-WDT500/501 watchdog cards");
785MODULE_LICENSE("GPL");
786MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
787MODULE_ALIAS_MISCDEV(TEMP_MINOR);