pcwd_pci.c 21 KB

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