pcwd_pci.c 20 KB

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