pcwd_pci.c 21 KB

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