PageRenderTime 1240ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/watchdog/pcwd_pci.c

https://github.com/Mengqi/linux-2.6
C | 855 lines | 580 code | 159 blank | 116 comment | 83 complexity | e51f28416bed7b0550bfac6c9a4d6c7c MD5 | raw file
  1. /*
  2. * Berkshire PCI-PC Watchdog Card Driver
  3. *
  4. * (c) Copyright 2003-2007 Wim Van Sebroeck <wim@iguana.be>.
  5. *
  6. * Based on source code of the following authors:
  7. * Ken Hollis <kenji@bitgate.com>,
  8. * Lindsay Harris <lindsay@bluegum.com>,
  9. * Alan Cox <alan@lxorguk.ukuu.org.uk>,
  10. * Matt Domsch <Matt_Domsch@dell.com>,
  11. * Rob Radez <rob@osinvestor.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. *
  18. * Neither Wim Van Sebroeck nor Iguana vzw. admit liability nor
  19. * provide warranty for any of this software. This material is
  20. * provided "AS-IS" and at no charge.
  21. */
  22. /*
  23. * A bells and whistles driver is available from:
  24. * http://www.kernel.org/pub/linux/kernel/people/wim/pcwd/pcwd_pci/
  25. *
  26. * More info available at
  27. * http://www.berkprod.com/ or http://www.pcwatchdog.com/
  28. */
  29. /*
  30. * Includes, defines, variables, module parameters, ...
  31. */
  32. #include <linux/module.h> /* For module specific items */
  33. #include <linux/moduleparam.h> /* For new moduleparam's */
  34. #include <linux/types.h> /* For standard types (like size_t) */
  35. #include <linux/errno.h> /* For the -ENODEV/... values */
  36. #include <linux/kernel.h> /* For printk/panic/... */
  37. #include <linux/delay.h> /* For mdelay function */
  38. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
  39. #include <linux/watchdog.h> /* For the watchdog specific items */
  40. #include <linux/notifier.h> /* For notifier support */
  41. #include <linux/reboot.h> /* For reboot_notifier stuff */
  42. #include <linux/init.h> /* For __init/__exit/... */
  43. #include <linux/fs.h> /* For file operations */
  44. #include <linux/pci.h> /* For pci functions */
  45. #include <linux/ioport.h> /* For io-port access */
  46. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  47. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  48. #include <linux/io.h> /* For inb/outb/... */
  49. /* Module and version information */
  50. #define WATCHDOG_VERSION "1.03"
  51. #define WATCHDOG_DRIVER_NAME "PCI-PC Watchdog"
  52. #define WATCHDOG_NAME "pcwd_pci"
  53. #define PFX WATCHDOG_NAME ": "
  54. #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION "\n"
  55. /* Stuff for the PCI ID's */
  56. #ifndef PCI_VENDOR_ID_QUICKLOGIC
  57. #define PCI_VENDOR_ID_QUICKLOGIC 0x11e3
  58. #endif
  59. #ifndef PCI_DEVICE_ID_WATCHDOG_PCIPCWD
  60. #define PCI_DEVICE_ID_WATCHDOG_PCIPCWD 0x5030
  61. #endif
  62. /*
  63. * These are the defines that describe the control status bits for the
  64. * PCI-PC Watchdog card.
  65. */
  66. /* Port 1 : Control Status #1 */
  67. #define WD_PCI_WTRP 0x01 /* Watchdog Trip status */
  68. #define WD_PCI_HRBT 0x02 /* Watchdog Heartbeat */
  69. #define WD_PCI_TTRP 0x04 /* Temperature Trip status */
  70. #define WD_PCI_RL2A 0x08 /* Relay 2 Active */
  71. #define WD_PCI_RL1A 0x10 /* Relay 1 Active */
  72. #define WD_PCI_R2DS 0x40 /* Relay 2 Disable Temperature-trip /
  73. reset */
  74. #define WD_PCI_RLY2 0x80 /* Activate Relay 2 on the board */
  75. /* Port 2 : Control Status #2 */
  76. #define WD_PCI_WDIS 0x10 /* Watchdog Disable */
  77. #define WD_PCI_ENTP 0x20 /* Enable Temperature Trip Reset */
  78. #define WD_PCI_WRSP 0x40 /* Watchdog wrote response */
  79. #define WD_PCI_PCMD 0x80 /* PC has sent command */
  80. /* according to documentation max. time to process a command for the pci
  81. * watchdog card is 100 ms, so we give it 150 ms to do it's job */
  82. #define PCI_COMMAND_TIMEOUT 150
  83. /* Watchdog's internal commands */
  84. #define CMD_GET_STATUS 0x04
  85. #define CMD_GET_FIRMWARE_VERSION 0x08
  86. #define CMD_READ_WATCHDOG_TIMEOUT 0x18
  87. #define CMD_WRITE_WATCHDOG_TIMEOUT 0x19
  88. #define CMD_GET_CLEAR_RESET_COUNT 0x84
  89. /* Watchdog's Dip Switch heartbeat values */
  90. static const int heartbeat_tbl[] = {
  91. 5, /* OFF-OFF-OFF = 5 Sec */
  92. 10, /* OFF-OFF-ON = 10 Sec */
  93. 30, /* OFF-ON-OFF = 30 Sec */
  94. 60, /* OFF-ON-ON = 1 Min */
  95. 300, /* ON-OFF-OFF = 5 Min */
  96. 600, /* ON-OFF-ON = 10 Min */
  97. 1800, /* ON-ON-OFF = 30 Min */
  98. 3600, /* ON-ON-ON = 1 hour */
  99. };
  100. /* We can only use 1 card due to the /dev/watchdog restriction */
  101. static int cards_found;
  102. /* internal variables */
  103. static int temp_panic;
  104. static unsigned long is_active;
  105. static char expect_release;
  106. /* this is private data for each PCI-PC watchdog card */
  107. static struct {
  108. /* Wether or not the card has a temperature device */
  109. int supports_temp;
  110. /* The card's boot status */
  111. int boot_status;
  112. /* The cards I/O address */
  113. unsigned long io_addr;
  114. /* the lock for io operations */
  115. spinlock_t io_lock;
  116. /* the PCI-device */
  117. struct pci_dev *pdev;
  118. } pcipcwd_private;
  119. /* module parameters */
  120. #define QUIET 0 /* Default */
  121. #define VERBOSE 1 /* Verbose */
  122. #define DEBUG 2 /* print fancy stuff too */
  123. static int debug = QUIET;
  124. module_param(debug, int, 0);
  125. MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
  126. #define WATCHDOG_HEARTBEAT 0 /* default heartbeat =
  127. delay-time from dip-switches */
  128. static int heartbeat = WATCHDOG_HEARTBEAT;
  129. module_param(heartbeat, int, 0);
  130. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. "
  131. "(0<heartbeat<65536 or 0=delay-time from dip-switches, default="
  132. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  133. static int nowayout = WATCHDOG_NOWAYOUT;
  134. module_param(nowayout, int, 0);
  135. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  136. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  137. /*
  138. * Internal functions
  139. */
  140. static int send_command(int cmd, int *msb, int *lsb)
  141. {
  142. int got_response, count;
  143. if (debug >= DEBUG)
  144. printk(KERN_DEBUG PFX "sending following data "
  145. "cmd=0x%02x msb=0x%02x lsb=0x%02x\n", cmd, *msb, *lsb);
  146. spin_lock(&pcipcwd_private.io_lock);
  147. /* If a command requires data it should be written first.
  148. * Data for commands with 8 bits of data should be written to port 4.
  149. * Commands with 16 bits of data, should be written as LSB to port 4
  150. * and MSB to port 5.
  151. * After the required data has been written then write the command to
  152. * port 6. */
  153. outb_p(*lsb, pcipcwd_private.io_addr + 4);
  154. outb_p(*msb, pcipcwd_private.io_addr + 5);
  155. outb_p(cmd, pcipcwd_private.io_addr + 6);
  156. /* wait till the pci card processed the command, signaled by
  157. * the WRSP bit in port 2 and give it a max. timeout of
  158. * PCI_COMMAND_TIMEOUT to process */
  159. got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
  160. for (count = 0; (count < PCI_COMMAND_TIMEOUT) && (!got_response);
  161. count++) {
  162. mdelay(1);
  163. got_response = inb_p(pcipcwd_private.io_addr + 2) & WD_PCI_WRSP;
  164. }
  165. if (debug >= DEBUG) {
  166. if (got_response) {
  167. printk(KERN_DEBUG PFX
  168. "time to process command was: %d ms\n",
  169. count);
  170. } else {
  171. printk(KERN_DEBUG PFX
  172. "card did not respond on command!\n");
  173. }
  174. }
  175. if (got_response) {
  176. /* read back response */
  177. *lsb = inb_p(pcipcwd_private.io_addr + 4);
  178. *msb = inb_p(pcipcwd_private.io_addr + 5);
  179. /* clear WRSP bit */
  180. inb_p(pcipcwd_private.io_addr + 6);
  181. if (debug >= DEBUG)
  182. printk(KERN_DEBUG PFX "received following data for "
  183. "cmd=0x%02x: msb=0x%02x lsb=0x%02x\n",
  184. cmd, *msb, *lsb);
  185. }
  186. spin_unlock(&pcipcwd_private.io_lock);
  187. return got_response;
  188. }
  189. static inline void pcipcwd_check_temperature_support(void)
  190. {
  191. if (inb_p(pcipcwd_private.io_addr) != 0xF0)
  192. pcipcwd_private.supports_temp = 1;
  193. }
  194. static int pcipcwd_get_option_switches(void)
  195. {
  196. int option_switches;
  197. option_switches = inb_p(pcipcwd_private.io_addr + 3);
  198. return option_switches;
  199. }
  200. static void pcipcwd_show_card_info(void)
  201. {
  202. int got_fw_rev, fw_rev_major, fw_rev_minor;
  203. char fw_ver_str[20]; /* The cards firmware version */
  204. int option_switches;
  205. got_fw_rev = send_command(CMD_GET_FIRMWARE_VERSION, &fw_rev_major,
  206. &fw_rev_minor);
  207. if (got_fw_rev)
  208. sprintf(fw_ver_str, "%u.%02u", fw_rev_major, fw_rev_minor);
  209. else
  210. sprintf(fw_ver_str, "<card no answer>");
  211. /* Get switch settings */
  212. option_switches = pcipcwd_get_option_switches();
  213. printk(KERN_INFO PFX "Found card at port "
  214. "0x%04x (Firmware: %s) %s temp option\n",
  215. (int) pcipcwd_private.io_addr, fw_ver_str,
  216. (pcipcwd_private.supports_temp ? "with" : "without"));
  217. printk(KERN_INFO PFX "Option switches (0x%02x): "
  218. "Temperature Reset Enable=%s, Power On Delay=%s\n",
  219. option_switches,
  220. ((option_switches & 0x10) ? "ON" : "OFF"),
  221. ((option_switches & 0x08) ? "ON" : "OFF"));
  222. if (pcipcwd_private.boot_status & WDIOF_CARDRESET)
  223. printk(KERN_INFO PFX
  224. "Previous reset was caused by the Watchdog card\n");
  225. if (pcipcwd_private.boot_status & WDIOF_OVERHEAT)
  226. printk(KERN_INFO PFX "Card sensed a CPU Overheat\n");
  227. if (pcipcwd_private.boot_status == 0)
  228. printk(KERN_INFO PFX
  229. "No previous trip detected - Cold boot or reset\n");
  230. }
  231. static int pcipcwd_start(void)
  232. {
  233. int stat_reg;
  234. spin_lock(&pcipcwd_private.io_lock);
  235. outb_p(0x00, pcipcwd_private.io_addr + 3);
  236. udelay(1000);
  237. stat_reg = inb_p(pcipcwd_private.io_addr + 2);
  238. spin_unlock(&pcipcwd_private.io_lock);
  239. if (stat_reg & WD_PCI_WDIS) {
  240. printk(KERN_ERR PFX "Card timer not enabled\n");
  241. return -1;
  242. }
  243. if (debug >= VERBOSE)
  244. printk(KERN_DEBUG PFX "Watchdog started\n");
  245. return 0;
  246. }
  247. static int pcipcwd_stop(void)
  248. {
  249. int stat_reg;
  250. spin_lock(&pcipcwd_private.io_lock);
  251. outb_p(0xA5, pcipcwd_private.io_addr + 3);
  252. udelay(1000);
  253. outb_p(0xA5, pcipcwd_private.io_addr + 3);
  254. udelay(1000);
  255. stat_reg = inb_p(pcipcwd_private.io_addr + 2);
  256. spin_unlock(&pcipcwd_private.io_lock);
  257. if (!(stat_reg & WD_PCI_WDIS)) {
  258. printk(KERN_ERR PFX
  259. "Card did not acknowledge disable attempt\n");
  260. return -1;
  261. }
  262. if (debug >= VERBOSE)
  263. printk(KERN_DEBUG PFX "Watchdog stopped\n");
  264. return 0;
  265. }
  266. static int pcipcwd_keepalive(void)
  267. {
  268. /* Re-trigger watchdog by writing to port 0 */
  269. spin_lock(&pcipcwd_private.io_lock);
  270. outb_p(0x42, pcipcwd_private.io_addr); /* send out any data */
  271. spin_unlock(&pcipcwd_private.io_lock);
  272. if (debug >= DEBUG)
  273. printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
  274. return 0;
  275. }
  276. static int pcipcwd_set_heartbeat(int t)
  277. {
  278. int t_msb = t / 256;
  279. int t_lsb = t % 256;
  280. if ((t < 0x0001) || (t > 0xFFFF))
  281. return -EINVAL;
  282. /* Write new heartbeat to watchdog */
  283. send_command(CMD_WRITE_WATCHDOG_TIMEOUT, &t_msb, &t_lsb);
  284. heartbeat = t;
  285. if (debug >= VERBOSE)
  286. printk(KERN_DEBUG PFX "New heartbeat: %d\n",
  287. heartbeat);
  288. return 0;
  289. }
  290. static int pcipcwd_get_status(int *status)
  291. {
  292. int control_status;
  293. *status = 0;
  294. control_status = inb_p(pcipcwd_private.io_addr + 1);
  295. if (control_status & WD_PCI_WTRP)
  296. *status |= WDIOF_CARDRESET;
  297. if (control_status & WD_PCI_TTRP) {
  298. *status |= WDIOF_OVERHEAT;
  299. if (temp_panic)
  300. panic(PFX "Temperature overheat trip!\n");
  301. }
  302. if (debug >= DEBUG)
  303. printk(KERN_DEBUG PFX "Control Status #1: 0x%02x\n",
  304. control_status);
  305. return 0;
  306. }
  307. static int pcipcwd_clear_status(void)
  308. {
  309. int control_status;
  310. int msb;
  311. int reset_counter;
  312. if (debug >= VERBOSE)
  313. printk(KERN_INFO PFX "clearing watchdog trip status & LED\n");
  314. control_status = inb_p(pcipcwd_private.io_addr + 1);
  315. if (debug >= DEBUG) {
  316. printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
  317. printk(KERN_DEBUG PFX "sending: 0x%02x\n",
  318. (control_status & WD_PCI_R2DS) | WD_PCI_WTRP);
  319. }
  320. /* clear trip status & LED and keep mode of relay 2 */
  321. outb_p((control_status & WD_PCI_R2DS) | WD_PCI_WTRP,
  322. pcipcwd_private.io_addr + 1);
  323. /* clear reset counter */
  324. msb = 0;
  325. reset_counter = 0xff;
  326. send_command(CMD_GET_CLEAR_RESET_COUNT, &msb, &reset_counter);
  327. if (debug >= DEBUG) {
  328. printk(KERN_DEBUG PFX "reset count was: 0x%02x\n",
  329. reset_counter);
  330. }
  331. return 0;
  332. }
  333. static int pcipcwd_get_temperature(int *temperature)
  334. {
  335. *temperature = 0;
  336. if (!pcipcwd_private.supports_temp)
  337. return -ENODEV;
  338. spin_lock(&pcipcwd_private.io_lock);
  339. *temperature = inb_p(pcipcwd_private.io_addr);
  340. spin_unlock(&pcipcwd_private.io_lock);
  341. /*
  342. * Convert celsius to fahrenheit, since this was
  343. * the decided 'standard' for this return value.
  344. */
  345. *temperature = (*temperature * 9 / 5) + 32;
  346. if (debug >= DEBUG) {
  347. printk(KERN_DEBUG PFX "temperature is: %d F\n",
  348. *temperature);
  349. }
  350. return 0;
  351. }
  352. static int pcipcwd_get_timeleft(int *time_left)
  353. {
  354. int msb;
  355. int lsb;
  356. /* Read the time that's left before rebooting */
  357. /* Note: if the board is not yet armed then we will read 0xFFFF */
  358. send_command(CMD_READ_WATCHDOG_TIMEOUT, &msb, &lsb);
  359. *time_left = (msb << 8) + lsb;
  360. if (debug >= VERBOSE)
  361. printk(KERN_DEBUG PFX "Time left before next reboot: %d\n",
  362. *time_left);
  363. return 0;
  364. }
  365. /*
  366. * /dev/watchdog handling
  367. */
  368. static ssize_t pcipcwd_write(struct file *file, const char __user *data,
  369. size_t len, loff_t *ppos)
  370. {
  371. /* See if we got the magic character 'V' and reload the timer */
  372. if (len) {
  373. if (!nowayout) {
  374. size_t i;
  375. /* note: just in case someone wrote the magic character
  376. * five months ago... */
  377. expect_release = 0;
  378. /* scan to see whether or not we got the
  379. * magic character */
  380. for (i = 0; i != len; i++) {
  381. char c;
  382. if (get_user(c, data + i))
  383. return -EFAULT;
  384. if (c == 'V')
  385. expect_release = 42;
  386. }
  387. }
  388. /* someone wrote to us, we should reload the timer */
  389. pcipcwd_keepalive();
  390. }
  391. return len;
  392. }
  393. static long pcipcwd_ioctl(struct file *file, unsigned int cmd,
  394. unsigned long arg)
  395. {
  396. void __user *argp = (void __user *)arg;
  397. int __user *p = argp;
  398. static const struct watchdog_info ident = {
  399. .options = WDIOF_OVERHEAT |
  400. WDIOF_CARDRESET |
  401. WDIOF_KEEPALIVEPING |
  402. WDIOF_SETTIMEOUT |
  403. WDIOF_MAGICCLOSE,
  404. .firmware_version = 1,
  405. .identity = WATCHDOG_DRIVER_NAME,
  406. };
  407. switch (cmd) {
  408. case WDIOC_GETSUPPORT:
  409. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  410. case WDIOC_GETSTATUS:
  411. {
  412. int status;
  413. pcipcwd_get_status(&status);
  414. return put_user(status, p);
  415. }
  416. case WDIOC_GETBOOTSTATUS:
  417. return put_user(pcipcwd_private.boot_status, p);
  418. case WDIOC_GETTEMP:
  419. {
  420. int temperature;
  421. if (pcipcwd_get_temperature(&temperature))
  422. return -EFAULT;
  423. return put_user(temperature, p);
  424. }
  425. case WDIOC_SETOPTIONS:
  426. {
  427. int new_options, retval = -EINVAL;
  428. if (get_user(new_options, p))
  429. return -EFAULT;
  430. if (new_options & WDIOS_DISABLECARD) {
  431. if (pcipcwd_stop())
  432. return -EIO;
  433. retval = 0;
  434. }
  435. if (new_options & WDIOS_ENABLECARD) {
  436. if (pcipcwd_start())
  437. return -EIO;
  438. retval = 0;
  439. }
  440. if (new_options & WDIOS_TEMPPANIC) {
  441. temp_panic = 1;
  442. retval = 0;
  443. }
  444. return retval;
  445. }
  446. case WDIOC_KEEPALIVE:
  447. pcipcwd_keepalive();
  448. return 0;
  449. case WDIOC_SETTIMEOUT:
  450. {
  451. int new_heartbeat;
  452. if (get_user(new_heartbeat, p))
  453. return -EFAULT;
  454. if (pcipcwd_set_heartbeat(new_heartbeat))
  455. return -EINVAL;
  456. pcipcwd_keepalive();
  457. /* Fall */
  458. }
  459. case WDIOC_GETTIMEOUT:
  460. return put_user(heartbeat, p);
  461. case WDIOC_GETTIMELEFT:
  462. {
  463. int time_left;
  464. if (pcipcwd_get_timeleft(&time_left))
  465. return -EFAULT;
  466. return put_user(time_left, p);
  467. }
  468. default:
  469. return -ENOTTY;
  470. }
  471. }
  472. static int pcipcwd_open(struct inode *inode, struct file *file)
  473. {
  474. /* /dev/watchdog can only be opened once */
  475. if (test_and_set_bit(0, &is_active)) {
  476. if (debug >= VERBOSE)
  477. printk(KERN_ERR PFX
  478. "Attempt to open already opened device.\n");
  479. return -EBUSY;
  480. }
  481. /* Activate */
  482. pcipcwd_start();
  483. pcipcwd_keepalive();
  484. return nonseekable_open(inode, file);
  485. }
  486. static int pcipcwd_release(struct inode *inode, struct file *file)
  487. {
  488. /*
  489. * Shut off the timer.
  490. */
  491. if (expect_release == 42) {
  492. pcipcwd_stop();
  493. } else {
  494. printk(KERN_CRIT PFX
  495. "Unexpected close, not stopping watchdog!\n");
  496. pcipcwd_keepalive();
  497. }
  498. expect_release = 0;
  499. clear_bit(0, &is_active);
  500. return 0;
  501. }
  502. /*
  503. * /dev/temperature handling
  504. */
  505. static ssize_t pcipcwd_temp_read(struct file *file, char __user *data,
  506. size_t len, loff_t *ppos)
  507. {
  508. int temperature;
  509. if (pcipcwd_get_temperature(&temperature))
  510. return -EFAULT;
  511. if (copy_to_user(data, &temperature, 1))
  512. return -EFAULT;
  513. return 1;
  514. }
  515. static int pcipcwd_temp_open(struct inode *inode, struct file *file)
  516. {
  517. if (!pcipcwd_private.supports_temp)
  518. return -ENODEV;
  519. return nonseekable_open(inode, file);
  520. }
  521. static int pcipcwd_temp_release(struct inode *inode, struct file *file)
  522. {
  523. return 0;
  524. }
  525. /*
  526. * Notify system
  527. */
  528. static int pcipcwd_notify_sys(struct notifier_block *this, unsigned long code,
  529. void *unused)
  530. {
  531. if (code == SYS_DOWN || code == SYS_HALT)
  532. pcipcwd_stop(); /* Turn the WDT off */
  533. return NOTIFY_DONE;
  534. }
  535. /*
  536. * Kernel Interfaces
  537. */
  538. static const struct file_operations pcipcwd_fops = {
  539. .owner = THIS_MODULE,
  540. .llseek = no_llseek,
  541. .write = pcipcwd_write,
  542. .unlocked_ioctl = pcipcwd_ioctl,
  543. .open = pcipcwd_open,
  544. .release = pcipcwd_release,
  545. };
  546. static struct miscdevice pcipcwd_miscdev = {
  547. .minor = WATCHDOG_MINOR,
  548. .name = "watchdog",
  549. .fops = &pcipcwd_fops,
  550. };
  551. static const struct file_operations pcipcwd_temp_fops = {
  552. .owner = THIS_MODULE,
  553. .llseek = no_llseek,
  554. .read = pcipcwd_temp_read,
  555. .open = pcipcwd_temp_open,
  556. .release = pcipcwd_temp_release,
  557. };
  558. static struct miscdevice pcipcwd_temp_miscdev = {
  559. .minor = TEMP_MINOR,
  560. .name = "temperature",
  561. .fops = &pcipcwd_temp_fops,
  562. };
  563. static struct notifier_block pcipcwd_notifier = {
  564. .notifier_call = pcipcwd_notify_sys,
  565. };
  566. /*
  567. * Init & exit routines
  568. */
  569. static int __devinit pcipcwd_card_init(struct pci_dev *pdev,
  570. const struct pci_device_id *ent)
  571. {
  572. int ret = -EIO;
  573. cards_found++;
  574. if (cards_found == 1)
  575. printk(KERN_INFO PFX DRIVER_VERSION);
  576. if (cards_found > 1) {
  577. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  578. return -ENODEV;
  579. }
  580. if (pci_enable_device(pdev)) {
  581. printk(KERN_ERR PFX "Not possible to enable PCI Device\n");
  582. return -ENODEV;
  583. }
  584. if (pci_resource_start(pdev, 0) == 0x0000) {
  585. printk(KERN_ERR PFX "No I/O-Address for card detected\n");
  586. ret = -ENODEV;
  587. goto err_out_disable_device;
  588. }
  589. pcipcwd_private.pdev = pdev;
  590. pcipcwd_private.io_addr = pci_resource_start(pdev, 0);
  591. if (pci_request_regions(pdev, WATCHDOG_NAME)) {
  592. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  593. (int) pcipcwd_private.io_addr);
  594. ret = -EIO;
  595. goto err_out_disable_device;
  596. }
  597. /* get the boot_status */
  598. pcipcwd_get_status(&pcipcwd_private.boot_status);
  599. /* clear the "card caused reboot" flag */
  600. pcipcwd_clear_status();
  601. /* disable card */
  602. pcipcwd_stop();
  603. /* Check whether or not the card supports the temperature device */
  604. pcipcwd_check_temperature_support();
  605. /* Show info about the card itself */
  606. pcipcwd_show_card_info();
  607. /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
  608. if (heartbeat == 0)
  609. heartbeat =
  610. heartbeat_tbl[(pcipcwd_get_option_switches() & 0x07)];
  611. /* Check that the heartbeat value is within it's range ;
  612. * if not reset to the default */
  613. if (pcipcwd_set_heartbeat(heartbeat)) {
  614. pcipcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  615. printk(KERN_INFO PFX
  616. "heartbeat value must be 0<heartbeat<65536, using %d\n",
  617. WATCHDOG_HEARTBEAT);
  618. }
  619. ret = register_reboot_notifier(&pcipcwd_notifier);
  620. if (ret != 0) {
  621. printk(KERN_ERR PFX
  622. "cannot register reboot notifier (err=%d)\n", ret);
  623. goto err_out_release_region;
  624. }
  625. if (pcipcwd_private.supports_temp) {
  626. ret = misc_register(&pcipcwd_temp_miscdev);
  627. if (ret != 0) {
  628. printk(KERN_ERR PFX "cannot register miscdev on "
  629. "minor=%d (err=%d)\n", TEMP_MINOR, ret);
  630. goto err_out_unregister_reboot;
  631. }
  632. }
  633. ret = misc_register(&pcipcwd_miscdev);
  634. if (ret != 0) {
  635. printk(KERN_ERR PFX
  636. "cannot register miscdev on minor=%d (err=%d)\n",
  637. WATCHDOG_MINOR, ret);
  638. goto err_out_misc_deregister;
  639. }
  640. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  641. heartbeat, nowayout);
  642. return 0;
  643. err_out_misc_deregister:
  644. if (pcipcwd_private.supports_temp)
  645. misc_deregister(&pcipcwd_temp_miscdev);
  646. err_out_unregister_reboot:
  647. unregister_reboot_notifier(&pcipcwd_notifier);
  648. err_out_release_region:
  649. pci_release_regions(pdev);
  650. err_out_disable_device:
  651. pci_disable_device(pdev);
  652. return ret;
  653. }
  654. static void __devexit pcipcwd_card_exit(struct pci_dev *pdev)
  655. {
  656. /* Stop the timer before we leave */
  657. if (!nowayout)
  658. pcipcwd_stop();
  659. /* Deregister */
  660. misc_deregister(&pcipcwd_miscdev);
  661. if (pcipcwd_private.supports_temp)
  662. misc_deregister(&pcipcwd_temp_miscdev);
  663. unregister_reboot_notifier(&pcipcwd_notifier);
  664. pci_release_regions(pdev);
  665. pci_disable_device(pdev);
  666. cards_found--;
  667. }
  668. static DEFINE_PCI_DEVICE_TABLE(pcipcwd_pci_tbl) = {
  669. { PCI_VENDOR_ID_QUICKLOGIC, PCI_DEVICE_ID_WATCHDOG_PCIPCWD,
  670. PCI_ANY_ID, PCI_ANY_ID, },
  671. { 0 }, /* End of list */
  672. };
  673. MODULE_DEVICE_TABLE(pci, pcipcwd_pci_tbl);
  674. static struct pci_driver pcipcwd_driver = {
  675. .name = WATCHDOG_NAME,
  676. .id_table = pcipcwd_pci_tbl,
  677. .probe = pcipcwd_card_init,
  678. .remove = __devexit_p(pcipcwd_card_exit),
  679. };
  680. static int __init pcipcwd_init_module(void)
  681. {
  682. spin_lock_init(&pcipcwd_private.io_lock);
  683. return pci_register_driver(&pcipcwd_driver);
  684. }
  685. static void __exit pcipcwd_cleanup_module(void)
  686. {
  687. pci_unregister_driver(&pcipcwd_driver);
  688. printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
  689. }
  690. module_init(pcipcwd_init_module);
  691. module_exit(pcipcwd_cleanup_module);
  692. MODULE_AUTHOR("Wim Van Sebroeck <wim@iguana.be>");
  693. MODULE_DESCRIPTION("Berkshire PCI-PC Watchdog driver");
  694. MODULE_LICENSE("GPL");
  695. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  696. MODULE_ALIAS_MISCDEV(TEMP_MINOR);