wdt_pci.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Industrial Computer Source WDT500/501 driver for Linux 2.1.x
  3.  *
  4.  * (c) Copyright 1996-1997 Alan Cox <alan@redhat.com>, All Rights Reserved.
  5.  * http://www.redhat.com
  6.  *
  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.08.
  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
  32.  * Tigran Aivazian : Restructured wdtpci_init_one() to handle failures
  33.  * Joel Becker : Added WDIOC_GET/SETTIMEOUT
  34.  * Zwane Mwaikambo : Magic char closing, locking changes, cleanups
  35.  * Matt Domsch : nowayout module option
  36.  */
  37. #include <linux/config.h>
  38. #include <linux/module.h>
  39. #include <linux/version.h>
  40. #include <linux/types.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/sched.h>
  44. #include <linux/miscdevice.h>
  45. #include <linux/watchdog.h>
  46. #define WDT_IS_PCI
  47. #include "wd501p.h"
  48. #include <linux/slab.h>
  49. #include <linux/ioport.h>
  50. #include <linux/fcntl.h>
  51. #include <asm/io.h>
  52. #include <asm/uaccess.h>
  53. #include <asm/system.h>
  54. #include <linux/notifier.h>
  55. #include <linux/reboot.h>
  56. #include <linux/init.h>
  57. #include <linux/spinlock.h>
  58. #include <asm/semaphore.h>
  59. #include <linux/pci.h>
  60. #define PFX "wdt_pci: "
  61. /*
  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
  65.  */
  66. #ifndef PCI_VENDOR_ID_ACCESSIO
  67. #define PCI_VENDOR_ID_ACCESSIO 0x494f
  68. #endif
  69. #ifndef PCI_DEVICE_ID_WDG_CSM
  70. #define PCI_DEVICE_ID_WDG_CSM 0x22c0
  71. #endif
  72. static struct semaphore open_sem;
  73. static spinlock_t wdtpci_lock;
  74. static int expect_close = 0;
  75. static int io;
  76. static int irq;
  77. /* Default timeout */
  78. #define WD_TIMO (100*60) /* 1 minute */
  79. #define WD_TIMO_MAX (WD_TIMO*60) /* 1 hour(?) */
  80. static int wd_margin = WD_TIMO;
  81. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  82. static int nowayout = 1;
  83. #else
  84. static int nowayout = 0;
  85. #endif
  86. MODULE_PARM(nowayout,"i");
  87. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  88. /*
  89.  * Programming support
  90.  */
  91.  
  92. static void wdtpci_ctr_mode(int ctr, int mode)
  93. {
  94. ctr<<=6;
  95. ctr|=0x30;
  96. ctr|=(mode<<1);
  97. outb_p(ctr, WDT_CR);
  98. }
  99. static void wdtpci_ctr_load(int ctr, int val)
  100. {
  101. outb_p(val&0xFF, WDT_COUNT0+ctr);
  102. outb_p(val>>8, WDT_COUNT0+ctr);
  103. }
  104. /*
  105.  * Kernel methods.
  106.  */
  107.  
  108.  
  109. /**
  110.  * wdtpci_status:
  111.  *
  112.  * Extract the status information from a WDT watchdog device. There are
  113.  * several board variants so we have to know which bits are valid. Some
  114.  * bits default to one and some to zero in order to be maximally painful.
  115.  *
  116.  * we then map the bits onto the status ioctl flags.
  117.  */
  118.  
  119. static int wdtpci_status(void)
  120. {
  121. /*
  122.  * Status register to bit flags
  123.  */
  124.  
  125. int flag=0;
  126. unsigned char status=inb_p(WDT_SR);
  127. status|=FEATUREMAP1;
  128. status&=~FEATUREMAP2;
  129. if(!(status&WDC_SR_TGOOD))
  130. flag|=WDIOF_OVERHEAT;
  131. if(!(status&WDC_SR_PSUOVER))
  132. flag|=WDIOF_POWEROVER;
  133. if(!(status&WDC_SR_PSUUNDR))
  134. flag|=WDIOF_POWERUNDER;
  135. if(!(status&WDC_SR_FANGOOD))
  136. flag|=WDIOF_FANFAULT;
  137. if(status&WDC_SR_ISOI0)
  138. flag|=WDIOF_EXTERN1;
  139. if(status&WDC_SR_ISII1)
  140. flag|=WDIOF_EXTERN2;
  141. return flag;
  142. }
  143. /**
  144.  * wdtpci_interrupt:
  145.  * @irq: Interrupt number
  146.  * @dev_id: Unused as we don't allow multiple devices.
  147.  * @regs: Unused.
  148.  *
  149.  * Handle an interrupt from the board. These are raised when the status
  150.  * map changes in what the board considers an interesting way. That means
  151.  * a failure condition occuring.
  152.  */
  153.  
  154. static void wdtpci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  155. {
  156. /*
  157.  * Read the status register see what is up and
  158.  * then printk it. 
  159.  */
  160.  
  161. unsigned char status=inb_p(WDT_SR);
  162. status|=FEATUREMAP1;
  163. status&=~FEATUREMAP2;
  164. printk(KERN_CRIT "WDT status %dn", status);
  165. if(!(status&WDC_SR_TGOOD))
  166. printk(KERN_CRIT "Overheat alarm.(%d)n",inb_p(WDT_RT));
  167. if(!(status&WDC_SR_PSUOVER))
  168. printk(KERN_CRIT "PSU over voltage.n");
  169. if(!(status&WDC_SR_PSUUNDR))
  170. printk(KERN_CRIT "PSU under voltage.n");
  171. if(!(status&WDC_SR_FANGOOD))
  172. printk(KERN_CRIT "Possible fan fault.n");
  173. if(!(status&WDC_SR_WCCR))
  174. #ifdef SOFTWARE_REBOOT
  175. #ifdef ONLY_TESTING
  176. printk(KERN_CRIT "Would Reboot.n");
  177. #else
  178. printk(KERN_CRIT "Initiating system reboot.n");
  179. machine_restart(NULL);
  180. #endif
  181. #else
  182. printk(KERN_CRIT "Reset in 5ms.n");
  183. #endif
  184. }
  185. /**
  186.  * wdtpci_ping:
  187.  *
  188.  * Reload counter one with the watchdog timeout. We don't bother reloading
  189.  * the cascade counter. 
  190.  */
  191.  
  192. static void wdtpci_ping(void)
  193. {
  194. unsigned long flags;
  195. /* Write a watchdog value */
  196. spin_lock_irqsave(&wdtpci_lock, flags);
  197. inb_p(WDT_DC);
  198. wdtpci_ctr_mode(1,2);
  199. wdtpci_ctr_load(1,wd_margin); /* Timeout */
  200. outb_p(0, WDT_DC);
  201. spin_unlock_irqrestore(&wdtpci_lock, flags);
  202. }
  203. /**
  204.  * wdtpci_write:
  205.  * @file: file handle to the watchdog
  206.  * @buf: buffer to write (unused as data does not matter here 
  207.  * @count: count of bytes
  208.  * @ppos: pointer to the position to write. No seeks allowed
  209.  *
  210.  * A write to a watchdog device is defined as a keepalive signal. Any
  211.  * write of data will do, as we we don't define content meaning.
  212.  */
  213.  
  214. static ssize_t wdtpci_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
  215. {
  216. /*  Can't seek (pwrite) on this device  */
  217. if (ppos != &file->f_pos)
  218. return -ESPIPE;
  219. if (count) {
  220. if (!nowayout) {
  221. size_t i;
  222. expect_close = 0;
  223. for (i = 0; i != count; i++) {
  224. char c;
  225. if(get_user(c, buf+i))
  226. return -EFAULT;
  227. if (c == 'V')
  228. expect_close = 1;
  229. }
  230. }
  231. wdtpci_ping();
  232. }
  233. return count;
  234. }
  235. /**
  236.  * wdtpci_read:
  237.  * @file: file handle to the watchdog board
  238.  * @buf: buffer to write 1 byte into
  239.  * @count: length of buffer
  240.  * @ptr: offset (no seek allowed)
  241.  *
  242.  * Read reports the temperature in degrees Fahrenheit. The API is in
  243.  * fahrenheit. It was designed by an imperial measurement luddite.
  244.  */
  245.  
  246. static ssize_t wdtpci_read(struct file *file, char *buf, size_t count, loff_t *ptr)
  247. {
  248. unsigned short c=inb_p(WDT_RT);
  249. unsigned char cp;
  250. /*  Can't seek (pread) on this device  */
  251. if (ptr != &file->f_pos)
  252. return -ESPIPE;
  253. switch(MINOR(file->f_dentry->d_inode->i_rdev))
  254. {
  255. case TEMP_MINOR:
  256. c*=11;
  257. c/=15;
  258. cp=c+7;
  259. if(copy_to_user(buf,&cp,1))
  260. return -EFAULT;
  261. return 1;
  262. default:
  263. return -EINVAL;
  264. }
  265. }
  266. /**
  267.  * wdtpci_ioctl:
  268.  * @inode: inode of the device
  269.  * @file: file handle to the device
  270.  * @cmd: watchdog command
  271.  * @arg: argument pointer
  272.  *
  273.  * The watchdog API defines a common set of functions for all watchdogs
  274.  * according to their available features. We only actually usefully support
  275.  * querying capabilities and current status. 
  276.  */
  277.  
  278. static int wdtpci_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  279. unsigned long arg)
  280. {
  281. int new_margin;
  282. static struct watchdog_info ident=
  283. {
  284. WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER
  285. |WDIOF_EXTERN1|WDIOF_EXTERN2|WDIOF_FANFAULT
  286. |WDIOF_SETTIMEOUT|WDIOF_MAGICCLOSE,
  287. 1,
  288. "WDT500/501PCI"
  289. };
  290. ident.options&=WDT_OPTION_MASK; /* Mask down to the card we have */
  291. switch(cmd)
  292. {
  293. default:
  294. return -ENOTTY;
  295. case WDIOC_GETSUPPORT:
  296. return copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident))?-EFAULT:0;
  297. case WDIOC_GETSTATUS:
  298. return put_user(wdtpci_status(),(int *)arg);
  299. case WDIOC_GETBOOTSTATUS:
  300. return put_user(0, (int *)arg);
  301. case WDIOC_KEEPALIVE:
  302. wdtpci_ping();
  303. return 0;
  304. case WDIOC_SETTIMEOUT:
  305. if (get_user(new_margin, (int *)arg))
  306. return -EFAULT;
  307. /* Arbitrary, can't find the card's limits */
  308. new_margin *= 100;
  309. if ((new_margin < 0) || (new_margin > WD_TIMO_MAX))
  310. return -EINVAL;
  311. wd_margin = new_margin;
  312. wdtpci_ping();
  313. /* Fall */
  314. case WDIOC_GETTIMEOUT:
  315. return put_user(wd_margin / 100, (int *)arg);
  316. }
  317. }
  318. /**
  319.  * wdtpci_open:
  320.  * @inode: inode of device
  321.  * @file: file handle to device
  322.  *
  323.  * One of our two misc devices has been opened. The watchdog device is
  324.  * single open and on opening we load the counters. Counter zero is a 
  325.  * 100Hz cascade, into counter 1 which downcounts to reboot. When the
  326.  * counter triggers counter 2 downcounts the length of the reset pulse
  327.  * which set set to be as long as possible. 
  328.  */
  329.  
  330. static int wdtpci_open(struct inode *inode, struct file *file)
  331. {
  332. unsigned long flags;
  333. switch(MINOR(inode->i_rdev))
  334. {
  335. case WATCHDOG_MINOR:
  336. if (down_trylock(&open_sem))
  337. return -EBUSY;
  338. if (nowayout) {
  339. MOD_INC_USE_COUNT;
  340. }
  341. /*
  342.  * Activate 
  343.  */
  344. spin_lock_irqsave(&wdtpci_lock, flags);
  345. inb_p(WDT_DC); /* Disable */
  346. /*
  347.  * "pet" the watchdog, as Access says.
  348.  * This resets the clock outputs.
  349.  */
  350. wdtpci_ctr_mode(2,0);
  351. outb_p(0, WDT_DC);
  352. inb_p(WDT_DC);
  353. outb_p(0, WDT_CLOCK); /* 2.0833MHz clock */
  354. inb_p(WDT_BUZZER); /* disable */
  355. inb_p(WDT_OPTONOTRST); /* disable */
  356. inb_p(WDT_OPTORST); /* disable */
  357. inb_p(WDT_PROGOUT); /* disable */
  358. wdtpci_ctr_mode(0,3);
  359. wdtpci_ctr_mode(1,2);
  360. wdtpci_ctr_mode(2,1);
  361. wdtpci_ctr_load(0,20833); /* count at 100Hz */
  362. wdtpci_ctr_load(1,wd_margin);/* Timeout 60 seconds */
  363. /* DO NOT LOAD CTR2 on PCI card! -- JPN */
  364. outb_p(0, WDT_DC); /* Enable */
  365. spin_unlock_irqrestore(&wdtpci_lock, flags);
  366. return 0;
  367. case TEMP_MINOR:
  368. return 0;
  369. default:
  370. return -ENODEV;
  371. }
  372. }
  373. /**
  374.  * wdtpci_close:
  375.  * @inode: inode to board
  376.  * @file: file handle to board
  377.  *
  378.  * The watchdog has a configurable API. There is a religious dispute 
  379.  * between people who want their watchdog to be able to shut down and 
  380.  * those who want to be sure if the watchdog manager dies the machine
  381.  * reboots. In the former case we disable the counters, in the latter
  382.  * case you have to open it again very soon.
  383.  */
  384.  
  385. static int wdtpci_release(struct inode *inode, struct file *file)
  386. {
  387. if (MINOR(inode->i_rdev)==WATCHDOG_MINOR) {
  388. unsigned long flags;
  389. if (expect_close) {
  390. spin_lock_irqsave(&wdtpci_lock, flags);
  391. inb_p(WDT_DC); /* Disable counters */
  392. wdtpci_ctr_load(2,0); /* 0 length reset pulses now */
  393. spin_unlock_irqrestore(&wdtpci_lock, flags);
  394. } else {
  395. printk(KERN_CRIT PFX "Unexpected close, not stopping timer!");
  396. wdtpci_ping();
  397. }
  398. up(&open_sem);
  399. }
  400. return 0;
  401. }
  402. /**
  403.  * notify_sys:
  404.  * @this: our notifier block
  405.  * @code: the event being reported
  406.  * @unused: unused
  407.  *
  408.  * Our notifier is called on system shutdowns. We want to turn the card
  409.  * off at reboot otherwise the machine will reboot again during memory
  410.  * test or worse yet during the following fsck. This would suck, in fact
  411.  * trust me - if it happens it does suck.
  412.  */
  413. static int wdtpci_notify_sys(struct notifier_block *this, unsigned long code,
  414. void *unused)
  415. {
  416. unsigned long flags;
  417. if (code==SYS_DOWN || code==SYS_HALT) {
  418. /* Turn the card off */
  419. spin_lock_irqsave(&wdtpci_lock, flags);
  420. inb_p(WDT_DC);
  421. wdtpci_ctr_load(2,0);
  422. spin_unlock_irqrestore(&wdtpci_lock, flags);
  423. }
  424. return NOTIFY_DONE;
  425. }
  426.  
  427. /*
  428.  * Kernel Interfaces
  429.  */
  430.  
  431.  
  432. static struct file_operations wdtpci_fops = {
  433. owner: THIS_MODULE,
  434. llseek: no_llseek,
  435. read: wdtpci_read,
  436. write: wdtpci_write,
  437. ioctl: wdtpci_ioctl,
  438. open: wdtpci_open,
  439. release: wdtpci_release,
  440. };
  441. static struct miscdevice wdtpci_miscdev=
  442. {
  443. WATCHDOG_MINOR,
  444. "watchdog",
  445. &wdtpci_fops
  446. };
  447. #ifdef CONFIG_WDT_501
  448. static struct miscdevice temp_miscdev=
  449. {
  450. TEMP_MINOR,
  451. "temperature",
  452. &wdtpci_fops
  453. };
  454. #endif
  455. /*
  456.  * The WDT card needs to learn about soft shutdowns in order to
  457.  * turn the timebomb registers off. 
  458.  */
  459.  
  460. static struct notifier_block wdtpci_notifier=
  461. {
  462. wdtpci_notify_sys,
  463. NULL,
  464. 0
  465. };
  466. static int __init wdtpci_init_one (struct pci_dev *dev,
  467.    const struct pci_device_id *ent)
  468. {
  469. static int dev_count = 0;
  470. int ret = -EIO;
  471. dev_count++;
  472. if (dev_count > 1) {
  473. printk (KERN_ERR PFX
  474. "this driver only supports 1 devicen");
  475. return -ENODEV;
  476. }
  477. sema_init(&open_sem, 1);
  478. spin_lock_init(&wdtpci_lock);
  479. irq = dev->irq;
  480. io = pci_resource_start (dev, 2);
  481. printk ("WDT501-P(PCI-WDG-CSM) driver 0.07 at %X "
  482. "(Interrupt %d)n", io, irq);
  483. if (pci_enable_device (dev))
  484. goto out;
  485. if (request_region (io, 16, "wdt-pci") == NULL) {
  486. printk (KERN_ERR PFX "I/O %d is not free.n", io);
  487. goto out;
  488. }
  489. if (request_irq (irq, wdtpci_interrupt, SA_INTERRUPT | SA_SHIRQ,
  490.  "wdt-pci", &wdtpci_miscdev)) {
  491. printk (KERN_ERR PFX "IRQ %d is not free.n", irq);
  492. goto out_reg;
  493. }
  494. ret = misc_register (&wdtpci_miscdev);
  495. if (ret) {
  496. printk (KERN_ERR PFX "can't misc_register on minor=%dn", WATCHDOG_MINOR);
  497. goto out_irq;
  498. }
  499. ret = register_reboot_notifier (&wdtpci_notifier);
  500. if (ret) {
  501. printk (KERN_ERR PFX "can't misc_register on minor=%dn", WATCHDOG_MINOR);
  502. goto out_misc;
  503. }
  504. #ifdef CONFIG_WDT_501
  505. ret = misc_register (&temp_miscdev);
  506. if (ret) {
  507. printk (KERN_ERR PFX "can't misc_register (temp) on minor=%dn", TEMP_MINOR);
  508. goto out_rbt;
  509. }
  510. #endif
  511. ret = 0;
  512. out:
  513. return ret;
  514. #ifdef CONFIG_WDT_501
  515. out_rbt:
  516. unregister_reboot_notifier(&wdtpci_notifier);
  517. #endif
  518. out_misc:
  519. misc_deregister(&wdtpci_miscdev);
  520. out_irq:
  521. free_irq(irq, &wdtpci_miscdev);
  522. out_reg:
  523. release_region (io, 16);
  524. goto out;
  525. }
  526. static void __devexit wdtpci_remove_one (struct pci_dev *pdev)
  527. {
  528. /* here we assume only one device will ever have
  529.  * been picked up and registered by probe function */
  530. unregister_reboot_notifier(&wdtpci_notifier);
  531. #ifdef CONFIG_WDT_501_PCI
  532. misc_deregister(&temp_miscdev);
  533. #endif
  534. misc_deregister(&wdtpci_miscdev);
  535. free_irq(irq, &wdtpci_miscdev);
  536. release_region(io, 16);
  537. }
  538. static struct pci_device_id wdtpci_pci_tbl[] __initdata = {
  539. { PCI_VENDOR_ID_ACCESSIO, PCI_DEVICE_ID_WDG_CSM, PCI_ANY_ID, PCI_ANY_ID, },
  540. { 0, }, /* terminate list */
  541. };
  542. MODULE_DEVICE_TABLE(pci, wdtpci_pci_tbl);
  543. static struct pci_driver wdtpci_driver = {
  544. name: "wdt-pci",
  545. id_table: wdtpci_pci_tbl,
  546. probe: wdtpci_init_one,
  547. remove: __devexit_p(wdtpci_remove_one),
  548. };
  549. /**
  550.  * wdtpci_cleanup:
  551.  *
  552.  * Unload the watchdog. You cannot do this with any file handles open.
  553.  * If your watchdog is set to continue ticking on close and you unload
  554.  * it, well it keeps ticking. We won't get the interrupt but the board
  555.  * will not touch PC memory so all is fine. You just have to load a new
  556.  * module in 60 seconds or reboot.
  557.  */
  558.  
  559. static void __exit wdtpci_cleanup(void)
  560. {
  561. pci_unregister_driver (&wdtpci_driver);
  562. }
  563. /**
  564.  *  wdtpci_init:
  565.  *
  566.  * Set up the WDT watchdog board. All we have to do is grab the
  567.  * resources we require and bitch if anyone beat us to them.
  568.  * The open() function will actually kick the board off.
  569.  */
  570.  
  571. static int __init wdtpci_init(void)
  572. {
  573. int rc = pci_register_driver (&wdtpci_driver);
  574. if (rc < 1)
  575. return -ENODEV;
  576. return 0;
  577. }
  578. module_init(wdtpci_init);
  579. module_exit(wdtpci_cleanup);
  580. MODULE_AUTHOR("JP Nollmann, Alan Cox");
  581. MODULE_DESCRIPTION("Driver for the ICS PCI watchdog cards");
  582. MODULE_LICENSE("GPL");
  583. EXPORT_NO_SYMBOLS;