pcwd_pci.c 20 KB

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