pcwd_pci.c 20 KB

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