- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / wdt_pci.c
blob5ff7210aeeaad714eec202c4bbb6fd38b937b963
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
32 * Tigran Aivazian : Restructured wdtpci_init_one() to handle failures
35 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/version.h>
38 #include <linux/types.h>
39 #include <linux/errno.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
44 #define WDT_IS_PCI
45 #include "wd501p.h"
46 #include <linux/malloc.h>
47 #include <linux/ioport.h>
48 #include <linux/fcntl.h>
49 #include <asm/io.h>
50 #include <asm/uaccess.h>
51 #include <asm/system.h>
52 #include <linux/notifier.h>
53 #include <linux/reboot.h>
54 #include <linux/init.h>
55 #include <linux/smp_lock.h>
57 #include <linux/pci.h>
59 #define PFX "wdt_pci: "
62 * Until Access I/O gets their application for a PCI vendor ID approved,
63 * I don't think that it's appropriate to move these constants into the
64 * regular pci_ids.h file. -- JPN 2000/01/18
67 #ifndef PCI_VENDOR_ID_ACCESSIO
68 #define PCI_VENDOR_ID_ACCESSIO 0x494f
69 #endif
70 #ifndef PCI_DEVICE_ID_WDG_CSM
71 #define PCI_DEVICE_ID_WDG_CSM 0x22c0
72 #endif
74 static int wdt_is_open;
77 * You must set these - there is no sane way to probe for this board.
78 * You can use wdt=x,y to set these now.
81 static int io=0x240;
82 static int irq=11;
84 #define WD_TIMO (100*60) /* 1 minute */
86 #ifndef MODULE
88 /**
89 * wdtpci_setup:
90 * @str: command line string
92 * Setup options. The board isn't really probe-able so we have to
93 * get the user to tell us the configuration. Sane people build it
94 * modular but the others come here.
97 static int __init wdtpci_setup(char *str)
99 int ints[4];
101 str = get_options (str, ARRAY_SIZE(ints), ints);
103 if (ints[0] > 0)
105 io = ints[1];
106 if(ints[0] > 1)
107 irq = ints[2];
110 return 1;
113 __setup("wdt=", wdtpci_setup);
115 #endif /* !MODULE */
118 * Programming support
121 static void wdtpci_ctr_mode(int ctr, int mode)
123 ctr<<=6;
124 ctr|=0x30;
125 ctr|=(mode<<1);
126 outb_p(ctr, WDT_CR);
129 static void wdtpci_ctr_load(int ctr, int val)
131 outb_p(val&0xFF, WDT_COUNT0+ctr);
132 outb_p(val>>8, WDT_COUNT0+ctr);
136 * Kernel methods.
141 * wdtpci_status:
143 * Extract the status information from a WDT watchdog device. There are
144 * several board variants so we have to know which bits are valid. Some
145 * bits default to one and some to zero in order to be maximally painful.
147 * we then map the bits onto the status ioctl flags.
150 static int wdtpci_status(void)
153 * Status register to bit flags
156 int flag=0;
157 unsigned char status=inb_p(WDT_SR);
158 status|=FEATUREMAP1;
159 status&=~FEATUREMAP2;
161 if(!(status&WDC_SR_TGOOD))
162 flag|=WDIOF_OVERHEAT;
163 if(!(status&WDC_SR_PSUOVER))
164 flag|=WDIOF_POWEROVER;
165 if(!(status&WDC_SR_PSUUNDR))
166 flag|=WDIOF_POWERUNDER;
167 if(!(status&WDC_SR_FANGOOD))
168 flag|=WDIOF_FANFAULT;
169 if(status&WDC_SR_ISOI0)
170 flag|=WDIOF_EXTERN1;
171 if(status&WDC_SR_ISII1)
172 flag|=WDIOF_EXTERN2;
173 return flag;
177 * wdtpci_interrupt:
178 * @irq: Interrupt number
179 * @dev_id: Unused as we don't allow multiple devices.
180 * @regs: Unused.
182 * Handle an interrupt from the board. These are raised when the status
183 * map changes in what the board considers an interesting way. That means
184 * a failure condition occuring.
187 static void wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
190 * Read the status register see what is up and
191 * then printk it.
194 unsigned char status=inb_p(WDT_SR);
196 status|=FEATUREMAP1;
197 status&=~FEATUREMAP2;
199 printk(KERN_CRIT "WDT status %d\n", status);
201 if(!(status&WDC_SR_TGOOD))
202 printk(KERN_CRIT "Overheat alarm.(%d)\n",inb_p(WDT_RT));
203 if(!(status&WDC_SR_PSUOVER))
204 printk(KERN_CRIT "PSU over voltage.\n");
205 if(!(status&WDC_SR_PSUUNDR))
206 printk(KERN_CRIT "PSU under voltage.\n");
207 if(!(status&WDC_SR_FANGOOD))
208 printk(KERN_CRIT "Possible fan fault.\n");
209 if(!(status&WDC_SR_WCCR))
210 #ifdef SOFTWARE_REBOOT
211 #ifdef ONLY_TESTING
212 printk(KERN_CRIT "Would Reboot.\n");
213 #else
214 printk(KERN_CRIT "Initiating system reboot.\n");
215 machine_restart(NULL);
216 #endif
217 #else
218 printk(KERN_CRIT "Reset in 5ms.\n");
219 #endif
223 static long long wdtpci_llseek(struct file *file, long long offset, int origin)
225 return -ESPIPE;
229 * wdtpci_ping:
231 * Reload counter one with the watchdog timeout. We don't bother reloading
232 * the cascade counter.
235 static void wdtpci_ping(void)
237 /* Write a watchdog value */
238 inb_p(WDT_DC);
239 wdtpci_ctr_mode(1,2);
240 wdtpci_ctr_load(1,WD_TIMO); /* Timeout */
241 outb_p(0, WDT_DC);
245 * wdtpci_write:
246 * @file: file handle to the watchdog
247 * @buf: buffer to write (unused as data does not matter here
248 * @count: count of bytes
249 * @ppos: pointer to the position to write. No seeks allowed
251 * A write to a watchdog device is defined as a keepalive signal. Any
252 * write of data will do, as we we don't define content meaning.
255 static ssize_t wdtpci_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
257 /* Can't seek (pwrite) on this device */
258 if (ppos != &file->f_pos)
259 return -ESPIPE;
261 if(count)
263 wdtpci_ping();
264 return 1;
266 return 0;
270 * wdtpci_read:
271 * @file: file handle to the watchdog board
272 * @buf: buffer to write 1 byte into
273 * @count: length of buffer
274 * @ptr: offset (no seek allowed)
276 * Read reports the temperature in degrees Fahrenheit. The API is in
277 * fahrenheit. It was designed by an imperial measurement luddite.
280 static ssize_t wdtpci_read(struct file *file, char *buf, size_t count, loff_t *ptr)
282 unsigned short c=inb_p(WDT_RT);
283 unsigned char cp;
285 /* Can't seek (pread) on this device */
286 if (ptr != &file->f_pos)
287 return -ESPIPE;
289 switch(MINOR(file->f_dentry->d_inode->i_rdev))
291 case TEMP_MINOR:
292 c*=11;
293 c/=15;
294 cp=c+7;
295 if(copy_to_user(buf,&cp,1))
296 return -EFAULT;
297 return 1;
298 default:
299 return -EINVAL;
304 * wdtpci_ioctl:
305 * @inode: inode of the device
306 * @file: file handle to the device
307 * @cmd: watchdog command
308 * @arg: argument pointer
310 * The watchdog API defines a common set of functions for all watchdogs
311 * according to their available features. We only actually usefully support
312 * querying capabilities and current status.
315 static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
316 unsigned long arg)
318 static struct watchdog_info ident=
320 WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
321 |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT,
323 "WDT500/501PCI"
326 ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
327 switch(cmd)
329 default:
330 return -ENOIOCTLCMD;
331 case WDIOC_GETSUPPORT:
332 return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
334 case WDIOC_GETSTATUS:
335 return put_user(wdtpci_status(),(int *)arg);
336 case WDIOC_GETBOOTSTATUS:
337 return put_user(0, (int *)arg);
338 case WDIOC_KEEPALIVE:
339 wdtpci_ping();
340 return 0;
345 * wdtpci_open:
346 * @inode: inode of device
347 * @file: file handle to device
349 * One of our two misc devices has been opened. The watchdog device is
350 * single open and on opening we load the counters. Counter zero is a
351 * 100Hz cascade, into counter 1 which downcounts to reboot. When the
352 * counter triggers counter 2 downcounts the length of the reset pulse
353 * which set set to be as long as possible.
356 static int wdtpci_open(struct inode *inode, struct file *file)
358 switch(MINOR(inode->i_rdev))
360 case WATCHDOG_MINOR:
361 if(wdt_is_open)
362 return -EBUSY;
363 #ifdef CONFIG_WATCHDOG_NOWAYOUT
364 MOD_INC_USE_COUNT;
365 #endif
367 * Activate
370 wdt_is_open=1;
372 inb_p(WDT_DC); /* Disable */
375 * "pet" the watchdog, as Access says.
376 * This resets the clock outputs.
379 wdtpci_ctr_mode(2,0);
380 outb_p(0, WDT_DC);
382 inb_p(WDT_DC);
384 outb_p(0, WDT_CLOCK); /* 2.0833MHz clock */
385 inb_p(WDT_BUZZER); /* disable */
386 inb_p(WDT_OPTONOTRST); /* disable */
387 inb_p(WDT_OPTORST); /* disable */
388 inb_p(WDT_PROGOUT); /* disable */
389 wdtpci_ctr_mode(0,3);
390 wdtpci_ctr_mode(1,2);
391 wdtpci_ctr_mode(2,1);
392 wdtpci_ctr_load(0,20833); /* count at 100Hz */
393 wdtpci_ctr_load(1,WD_TIMO);/* Timeout 60 seconds */
394 /* DO NOT LOAD CTR2 on PCI card! -- JPN */
395 outb_p(0, WDT_DC); /* Enable */
396 return 0;
397 case TEMP_MINOR:
398 return 0;
399 default:
400 return -ENODEV;
405 * wdtpci_close:
406 * @inode: inode to board
407 * @file: file handle to board
409 * The watchdog has a configurable API. There is a religious dispute
410 * between people who want their watchdog to be able to shut down and
411 * those who want to be sure if the watchdog manager dies the machine
412 * reboots. In the former case we disable the counters, in the latter
413 * case you have to open it again very soon.
416 static int wdtpci_release(struct inode *inode, struct file *file)
418 if(MINOR(inode->i_rdev)==WATCHDOG_MINOR)
420 lock_kernel();
421 #ifndef CONFIG_WATCHDOG_NOWAYOUT
422 inb_p(WDT_DC); /* Disable counters */
423 wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
424 #endif
425 wdt_is_open=0;
426 unlock_kernel();
428 return 0;
432 * notify_sys:
433 * @this: our notifier block
434 * @code: the event being reported
435 * @unused: unused
437 * Our notifier is called on system shutdowns. We want to turn the card
438 * off at reboot otherwise the machine will reboot again during memory
439 * test or worse yet during the following fsck. This would suck, in fact
440 * trust me - if it happens it does suck.
443 static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
444 void *unused)
446 if(code==SYS_DOWN || code==SYS_HALT)
448 /* Turn the card off */
449 inb_p(WDT_DC);
450 wdtpci_ctr_load(2,0);
452 return NOTIFY_DONE;
456 * Kernel Interfaces
460 static struct file_operations wdtpci_fops = {
461 owner: THIS_MODULE,
462 llseek: wdtpci_llseek,
463 read: wdtpci_read,
464 write: wdtpci_write,
465 ioctl: wdtpci_ioctl,
466 open: wdtpci_open,
467 release: wdtpci_release,
470 static struct miscdevice wdtpci_miscdev=
472 WATCHDOG_MINOR,
473 "watchdog",
474 &wdtpci_fops
477 #ifdef CONFIG_WDT_501
478 static struct miscdevice temp_miscdev=
480 TEMP_MINOR,
481 "temperature",
482 &wdtpci_fops
484 #endif
487 * The WDT card needs to learn about soft shutdowns in order to
488 * turn the timebomb registers off.
491 static struct notifier_block wdtpci_notifier=
493 wdtpci_notify_sys,
494 NULL,
499 static int __init wdtpci_init_one (struct pci_dev *dev,
500 const struct pci_device_id *ent)
502 static int dev_count = 0;
503 int ret = -EIO;
505 dev_count++;
506 if (dev_count > 1) {
507 printk (KERN_ERR PFX
508 "this driver only supports 1 device\n");
509 return -ENODEV;
512 irq = dev->irq;
513 io = pci_resource_start (dev, 2);
514 printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
515 "(Interrupt %d)\n", io, irq);
517 if (pci_enable_device (dev))
518 goto out;
520 if (request_region (io, 16, "wdt-pci") == NULL) {
521 printk (KERN_ERR PFX "I/O %d is not free.\n", io);
522 goto out;
525 if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
526 "wdt-pci", &wdtpci_miscdev)) {
527 printk (KERN_ERR PFX "IRQ %d is not free.\n", irq);
528 goto out_reg;
531 ret = misc_register (&wdtpci_miscdev);
532 if (ret) {
533 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
534 goto out_irq;
537 ret = register_reboot_notifier (&wdtpci_notifier);
538 if (ret) {
539 printk (KERN_ERR PFX "can't misc_register on minor=%d\n", WATCHDOG_MINOR);
540 goto out_misc;
542 #ifdef CONFIG_WDT_501
543 ret = misc_register (&temp_miscdev);
544 if (ret) {
545 printk (KERN_ERR PFX "can't misc_register (temp) on minor=%d\n", TEMP_MINOR);
546 goto out_rbt;
548 #endif
550 ret = 0;
551 out:
552 return ret;
554 #ifdef CONFIG_WDT_501
555 out_rbt:
556 unregister_reboot_notifier(&wdtpci_notifier);
557 #endif
558 out_misc:
559 misc_deregister(&wdtpci_miscdev);
560 out_irq:
561 free_irq(irq, &wdtpci_miscdev);
562 out_reg:
563 release_region (io, 16);
564 goto out;
568 static void __exit wdtpci_remove_one (struct pci_dev *pdev)
570 /* here we assume only one device will ever have
571 * been picked up and registered by probe function */
572 unregister_reboot_notifier(&wdtpci_notifier);
573 #ifdef CONFIG_WDT_501_PCI
574 misc_deregister(&temp_miscdev);
575 #endif
576 misc_deregister(&wdtpci_miscdev);
577 free_irq(irq, &wdtpci_miscdev);
578 release_region(io, 16);
582 static struct pci_device_id wdtpci_pci_tbl[] __initdata = {
583 { PCI_VENDOR_ID_ACCESSIO, PCI_DEVICE_ID_WDG_CSM, PCI_ANY_ID, PCI_ANY_ID, },
584 { 0, }, /* terminate list */
586 MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
589 static struct pci_driver wdtpci_driver = {
590 name: "wdt-pci",
591 id_table: wdtpci_pci_tbl,
592 probe: wdtpci_init_one,
593 remove: wdtpci_remove_one,
598 * wdtpci_cleanup:
600 * Unload the watchdog. You cannot do this with any file handles open.
601 * If your watchdog is set to continue ticking on close and you unload
602 * it, well it keeps ticking. We won't get the interrupt but the board
603 * will not touch PC memory so all is fine. You just have to load a new
604 * module in 60 seconds or reboot.
607 static void __exit wdtpci_cleanup(void)
609 pci_unregister_driver (&wdtpci_driver);
614 * wdtpci_init:
616 * Set up the WDT watchdog board. All we have to do is grab the
617 * resources we require and bitch if anyone beat us to them.
618 * The open() function will actually kick the board off.
621 static int __init wdtpci_init(void)
623 int rc = pci_register_driver (&wdtpci_driver);
625 if (rc < 1)
626 return -ENODEV;
628 return 0;
632 module_init(wdtpci_init);
633 module_exit(wdtpci_cleanup);