pcwd_pci.c 20 KB

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