pcwd_pci.c 19 KB

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