Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / wdt_pci.c
blob2bd09930a14a2c6604aad7a29b1036b059b2610a
1 /*
2 * Industrial Computer Source WDT500/501 driver for Linux 2.1.x
4 * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
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.
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.
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18 * Release 0.08.
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
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/version.h>
37 #include <linux/types.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/miscdevice.h>
42 #include <linux/watchdog.h>
43 #define WDT_IS_PCI
44 #include "wd501p.h"
45 #include <linux/malloc.h>
46 #include <linux/ioport.h>
47 #include <linux/fcntl.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
50 #include <asm/system.h>
51 #include <linux/notifier.h>
52 #include <linux/reboot.h>
53 #include <linux/init.h>
55 #include <linux/pci.h>
57 #define PFX "wdt_pci: "
60 * Until Access I/O gets their application for a PCI vendor ID approved,
61 * I don't think that it's appropriate to move these constants into the
62 * regular pci_ids.h file. -- JPN 2000/01/18
65 #ifndef PCI_VENDOR_ID_ACCESSIO
66 #define PCI_VENDOR_ID_ACCESSIO 0x494f
67 #endif
68 #ifndef PCI_DEVICE_ID_WDG_CSM
69 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
70 #endif
72 static int wdt_is_open=0;
75 * You must set these - there is no sane way to probe for this board.
76 * You can use wdt=x,y to set these now.
79 static int io=0x240;
80 static int irq=11;
82 #define WD_TIMO (100*60) /* 1 minute */
84 #ifndef MODULE
86 /**
87 * wdtpci_setup:
88 * @str: command line string
90 * Setup options. The board isn't really probe-able so we have to
91 * get the user to tell us the configuration. Sane people build it
92 * modular but the others come here.
95 static int __init wdtpci_setup(char *str)
97 int ints[4];
99 str = get_options (str, ARRAY_SIZE(ints), ints);
101 if (ints[0] > 0)
103 io = ints[1];
104 if(ints[0] > 1)
105 irq = ints[2];
108 return 1;
111 __setup("wdt=", wdtpci_setup);
113 #endif /* !MODULE */
116 * Programming support
119 static void wdtpci_ctr_mode(int ctr, int mode)
121 ctr<<=6;
122 ctr|=0x30;
123 ctr|=(mode<<1);
124 outb_p(ctr, WDT_CR);
127 static void wdtpci_ctr_load(int ctr, int val)
129 outb_p(val&0xFF, WDT_COUNT0+ctr);
130 outb_p(val>>8, WDT_COUNT0+ctr);
134 * Kernel methods.
139 * wdtpci_status:
141 * Extract the status information from a WDT watchdog device. There are
142 * several board variants so we have to know which bits are valid. Some
143 * bits default to one and some to zero in order to be maximally painful.
145 * we then map the bits onto the status ioctl flags.
148 static int wdtpci_status(void)
151 * Status register to bit flags
154 int flag=0;
155 unsigned char status=inb_p(WDT_SR);
156 status|=FEATUREMAP1;
157 status&=~FEATUREMAP2;
159 if(!(status&WDC_SR_TGOOD))
160 flag|=WDIOF_OVERHEAT;
161 if(!(status&WDC_SR_PSUOVER))
162 flag|=WDIOF_POWEROVER;
163 if(!(status&WDC_SR_PSUUNDR))
164 flag|=WDIOF_POWERUNDER;
165 if(!(status&WDC_SR_FANGOOD))
166 flag|=WDIOF_FANFAULT;
167 if(status&WDC_SR_ISOI0)
168 flag|=WDIOF_EXTERN1;
169 if(status&WDC_SR_ISII1)
170 flag|=WDIOF_EXTERN2;
171 return flag;
175 * wdtpci_interrupt:
176 * @irq: Interrupt number
177 * @dev_id: Unused as we don't allow multiple devices.
178 * @regs: Unused.
180 * Handle an interrupt from the board. These are raised when the status
181 * map changes in what the board considers an interesting way. That means
182 * a failure condition occuring.
185 static void wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
188 * Read the status register see what is up and
189 * then printk it.
192 unsigned char status=inb_p(WDT_SR);
194 status|=FEATUREMAP1;
195 status&=~FEATUREMAP2;
197 printk(KERN_CRIT "WDT status %d\n", status);
199 if(!(status&WDC_SR_TGOOD))
200 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
201 if(!(status&WDC_SR_PSUOVER))
202 printk(KERN_CRIT "PSU over voltage.\n");
203 if(!(status&WDC_SR_PSUUNDR))
204 printk(KERN_CRIT "PSU under voltage.\n");
205 if(!(status&WDC_SR_FANGOOD))
206 printk(KERN_CRIT "Possible fan fault.\n");
207 if(!(status&WDC_SR_WCCR))
208 #ifdef SOFTWARE_REBOOT
209 #ifdef ONLY_TESTING
210 printk(KERN_CRIT "Would Reboot.\n");
211 #else
212 printk(KERN_CRIT "Initiating system reboot.\n");
213 machine_restart(NULL);
214 #endif
215 #else
216 printk(KERN_CRIT "Reset in 5ms.\n");
217 #endif
221 static long long wdtpci_llseek(struct file *file, long long offset, int origin)
223 return -ESPIPE;
227 * wdtpci_ping:
229 * Reload counter one with the watchdog timeout. We don't bother reloading
230 * the cascade counter.
233 static void wdtpci_ping(void)
235 /* Write a watchdog value */
236 inb_p(WDT_DC);
237 wdtpci_ctr_mode(1,2);
238 wdtpci_ctr_load(1,WD_TIMO); /* Timeout */
239 outb_p(0, WDT_DC);
243 * wdtpci_write:
244 * @file: file handle to the watchdog
245 * @buf: buffer to write (unused as data does not matter here
246 * @count: count of bytes
247 * @ppos: pointer to the position to write. No seeks allowed
249 * A write to a watchdog device is defined as a keepalive signal. Any
250 * write of data will do, as we we don't define content meaning.
253 static ssize_t wdtpci_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
255 /* Can't seek (pwrite) on this device */
256 if (ppos != &file->f_pos)
257 return -ESPIPE;
259 if(count)
261 wdtpci_ping();
262 return 1;
264 return 0;
268 * wdtpci_read:
269 * @file: file handle to the watchdog board
270 * @buf: buffer to write 1 byte into
271 * @count: length of buffer
272 * @ptr: offset (no seek allowed)
274 * Read reports the temperature in degrees Fahrenheit. The API is in
275 * farenheit. It was designed by an imperial measurement luddite.
278 static ssize_t wdtpci_read(struct file *file, char *buf, size_t count, loff_t *ptr)
280 unsigned short c=inb_p(WDT_RT);
281 unsigned char cp;
283 /* Can't seek (pread) on this device */
284 if (ptr != &file->f_pos)
285 return -ESPIPE;
287 switch(MINOR(file->f_dentry->d_inode->i_rdev))
289 case TEMP_MINOR:
290 c*=11;
291 c/=15;
292 cp=c+7;
293 if(copy_to_user(buf,&cp,1))
294 return -EFAULT;
295 return 1;
296 default:
297 return -EINVAL;
302 * wdtpci_ioctl:
303 * @inode: inode of the device
304 * @file: file handle to the device
305 * @cmd: watchdog command
306 * @arg: argument pointer
308 * The watchdog API defines a common set of functions for all watchdogs
309 * according to their available features. We only actually usefully support
310 * querying capabilities and current status.
313 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
314 unsigned long arg)
316 static struct watchdog_info ident=
318 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
319 |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
321 "WDT500/501PCI"
324 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
325 switch(cmd)
327 default:
328 return -ENOIOCTLCMD;
329 case WDIOC_GETSUPPORT:
330 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
332 case WDIOC_GETSTATUS:
333 return put_user(wdtpci_status(),(int *)arg);
334 case WDIOC_GETBOOTSTATUS:
335 return put_user(0, (int *)arg);
336 case WDIOC_KEEPALIVE:
337 wdtpci_ping();
338 return 0;
343 * wdtpci_open:
344 * @inode: inode of device
345 * @file: file handle to device
347 * One of our two misc devices has been opened. The watchdog device is
348 * single open and on opening we load the counters. Counter zero is a
349 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
350 * counter triggers counter 2 downcounts the length of the reset pulse
351 * which set set to be as long as possible.
354 static int wdtpci_open(struct inode *inode, struct file *file)
356 switch(MINOR(inode->i_rdev))
358 case WATCHDOG_MINOR:
359 if(wdt_is_open)
360 return -EBUSY;
361 MOD_INC_USE_COUNT;
363 * Activate
366 wdt_is_open=1;
368 inb_p(WDT_DC); /* Disable */
371 * "pet" the watchdog, as Access says.
372 * This resets the clock outputs.
375 wdtpci_ctr_mode(2,0);
376 outb_p(0, WDT_DC);
378 inb_p(WDT_DC);
380 outb_p(0, WDT_CLOCK); /* 2.0833MHz clock */
381 inb_p(WDT_BUZZER); /* disable */
382 inb_p(WDT_OPTONOTRST); /* disable */
383 inb_p(WDT_OPTORST); /* disable */
384 inb_p(WDT_PROGOUT); /* disable */
385 wdtpci_ctr_mode(0,3);
386 wdtpci_ctr_mode(1,2);
387 wdtpci_ctr_mode(2,1);
388 wdtpci_ctr_load(0,20833); /* count at 100Hz */
389 wdtpci_ctr_load(1,WD_TIMO);/* Timeout 60 seconds */
390 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
391 outb_p(0, WDT_DC); /* Enable */
392 return 0;
393 case TEMP_MINOR:
394 MOD_INC_USE_COUNT;
395 return 0;
396 default:
397 return -ENODEV;
402 * wdtpci_close:
403 * @inode: inode to board
404 * @file: file handle to board
406 * The watchdog has a configurable API. There is a religious dispute
407 * between people who want their watchdog to be able to shut down and
408 * those who want to be sure if the watchdog manager dies the machine
409 * reboots. In the former case we disable the counters, in the latter
410 * case you have to open it again very soon.
413 static int wdtpci_release(struct inode *inode, struct file *file)
415 if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
417 #ifndef CONFIG_WATCHDOG_NOWAYOUT
418 inb_p(WDT_DC); /* Disable counters */
419 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
420 #endif
421 wdt_is_open=0;
423 MOD_DEC_USE_COUNT;
424 return 0;
428 * notify_sys:
429 * @this: our notifier block
430 * @code: the event being reported
431 * @unused: unused
433 * Our notifier is called on system shutdowns. We want to turn the card
434 * off at reboot otherwise the machine will reboot again during memory
435 * test or worse yet during the following fsck. This would suck, in fact
436 * trust me - if it happens it does suck.
439 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
440 void *unused)
442 if(code==SYS_DOWN || code==SYS_HALT)
444 /* Turn the card off */
445 inb_p(WDT_DC);
446 wdtpci_ctr_load(2,0);
448 return NOTIFY_DONE;
452 * Kernel Interfaces
456 static struct file_operations wdtpci_fops = {
457 llseek: wdtpci_llseek,
458 read: wdtpci_read,
459 write: wdtpci_write,
460 ioctl: wdtpci_ioctl,
461 open: wdtpci_open,
462 release: wdtpci_release,
465 static struct miscdevice wdtpci_miscdev=
467 WATCHDOG_MINOR,
468 "watchdog",
469 &wdtpci_fops
472 #ifdef CONFIG_WDT_501
473 static struct miscdevice temp_miscdev=
475 TEMP_MINOR,
476 "temperature",
477 &wdtpci_fops
479 #endif
482 * The WDT card needs to learn about soft shutdowns in order to
483 * turn the timebomb registers off.
486 static struct notifier_block wdtpci_notifier=
488 wdtpci_notify_sys,
489 NULL,
494 static int __init wdtpci_init_one (struct pci_dev *dev,
495 const struct pci_device_id *ent)
497 static int dev_count = 0;
499 dev_count++;
500 if (dev_count > 1) {
501 printk (KERN_ERR PFX
502 "this driver only supports 1 device\n");
503 return -ENODEV;
506 irq = dev->irq;
507 io = pci_resource_start (dev, 2);
508 printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
509 "(Interrupt %d)\n", io, irq);
511 if (pci_enable_device (dev))
512 goto err_out;
514 if (request_region (io, 16, "wdt-pci") == NULL) {
515 printk (KERN_ERR PFX "I/O %d is not free.\n", io);
516 goto err_out;
519 if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
520 "wdt-pci", &wdtpci_miscdev)) {
521 printk (KERN_ERR PFX "IRQ %d is not free.\n", irq);
522 goto err_out_free_res;
525 misc_register (&wdtpci_miscdev);
527 #ifdef CONFIG_WDT_501
528 misc_register (&temp_miscdev);
529 #endif
531 register_reboot_notifier (&wdtpci_notifier);
533 return 0;
535 err_out_free_res:
536 release_region (io, 16);
537 err_out:
538 return -EIO;
542 static void __exit wdtpci_remove_one (struct pci_dev *pdev)
544 /* here we assume only one device will ever have
545 * been picked up and registered by probe function */
546 unregister_reboot_notifier(&wdtpci_notifier);
547 #ifdef CONFIG_WDT_501_PCI
548 misc_deregister(&temp_miscdev);
549 #endif
550 misc_deregister(&wdtpci_miscdev);
551 free_irq(irq, &wdtpci_miscdev);
552 release_region(io, 16);
556 static struct pci_device_id wdtpci_pci_tbl[] __initdata = {
557 { PCI_VENDOR_ID_ACCESSIO, PCI_DEVICE_ID_WDG_CSM, PCI_ANY_ID, PCI_ANY_ID, },
558 { 0, }, /* terminate list */
560 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
563 static struct pci_driver wdtpci_driver = {
564 name: "wdt-pci",
565 id_table: wdtpci_pci_tbl,
566 probe: wdtpci_init_one,
567 remove: wdtpci_remove_one,
572 * wdtpci_cleanup:
574 * Unload the watchdog. You cannot do this with any file handles open.
575 * If your watchdog is set to continue ticking on close and you unload
576 * it, well it keeps ticking. We won't get the interrupt but the board
577 * will not touch PC memory so all is fine. You just have to load a new
578 * module in 60 seconds or reboot.
581 static void __exit wdtpci_cleanup(void)
583 pci_unregister_driver (&wdtpci_driver);
588 * wdtpci_init:
590 * Set up the WDT watchdog board. All we have to do is grab the
591 * resources we require and bitch if anyone beat us to them.
592 * The open() function will actually kick the board off.
595 static int __init wdtpci_init(void)
597 int rc = pci_register_driver (&wdtpci_driver);
599 if (rc < 1)
600 return -ENODEV;
602 return 0;
606 module_init(wdtpci_init);
607 module_exit(wdtpci_cleanup);