pcwd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * PC Watchdog Driver
  3. * by Ken Hollis (khollis@bitgate.com)
  4. *
  5. * Permission granted from Simon Machell (73244.1270@compuserve.com)
  6. * Written for the Linux Kernel, and GPLed by Ken Hollis
  7. *
  8. * 960107 Added request_region routines, modulized the whole thing.
  9. * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
  10. * WD_TIMEOUT define.
  11. * 960216 Added eof marker on the file, and changed verbose messages.
  12. * 960716 Made functional and cosmetic changes to the source for
  13. * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
  14. * 960717 Removed read/seek routines, replaced with ioctl. Also, added
  15. * check_region command due to Alan's suggestion.
  16. * 960821 Made changes to compile in newer 2.0.x kernels. Added
  17. * "cold reboot sense" entry.
  18. * 960825 Made a few changes to code, deleted some defines and made
  19. * typedefs to replace them. Made heartbeat reset only available
  20. * via ioctl, and removed the write routine.
  21. * 960828 Added new items for PC Watchdog Rev.C card.
  22. * 960829 Changed around all of the IOCTLs, added new features,
  23. * added watchdog disable/re-enable routines. Added firmware
  24. * version reporting. Added read routine for temperature.
  25. * Removed some extra defines, added an autodetect Revision
  26. * routine.
  27. * 961006 Revised some documentation, fixed some cosmetic bugs. Made
  28. * drivers to panic the system if it's overheating at bootup.
  29. * 961118 Changed some verbiage on some of the output, tidied up
  30. * code bits, and added compatibility to 2.1.x.
  31. * 970912 Enabled board on open and disable on close.
  32. * 971107 Took account of recent VFS changes (broke read).
  33. * 971210 Disable board on initialisation in case board already ticking.
  34. * 971222 Changed open/close for temperature handling
  35. * Michael Meskes <meskes@debian.org>.
  36. * 980112 Used minor numbers from include/linux/miscdevice.h
  37. * 990403 Clear reset status after reading control status register in
  38. * pcwd_showprevstate(). [Marc Boucher <marc@mbsi.ca>]
  39. * 990605 Made changes to code to support Firmware 1.22a, added
  40. * fairly useless proc entry.
  41. * 990610 removed said useless proc code for the merge <alan>
  42. * 000403 Removed last traces of proc code. <davej>
  43. * 011214 Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT <Matt_Domsch@dell.com>
  44. * Added timeout module option to override default
  45. */
  46. /*
  47. * A bells and whistles driver is available from http://www.pcwd.de/
  48. * More info available at http://www.berkprod.com/ or http://www.pcwatchdog.com/
  49. */
  50. #include <linux/config.h> /* For CONFIG_WATCHDOG_NOWAYOUT/... */
  51. #include <linux/module.h> /* For module specific items */
  52. #include <linux/moduleparam.h> /* For new moduleparam's */
  53. #include <linux/types.h> /* For standard types (like size_t) */
  54. #include <linux/errno.h> /* For the -ENODEV/... values */
  55. #include <linux/kernel.h> /* For printk/panic/... */
  56. #include <linux/delay.h> /* For mdelay function */
  57. #include <linux/timer.h> /* For timer related operations */
  58. #include <linux/jiffies.h> /* For jiffies stuff */
  59. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR) */
  60. #include <linux/watchdog.h> /* For the watchdog specific items */
  61. #include <linux/notifier.h> /* For notifier support */
  62. #include <linux/reboot.h> /* For reboot_notifier stuff */
  63. #include <linux/init.h> /* For __init/__exit/... */
  64. #include <linux/fs.h> /* For file operations */
  65. #include <linux/ioport.h> /* For io-port access */
  66. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  67. #include <linux/sched.h> /* TASK_INTERRUPTIBLE, set_current_state() and friends */
  68. #include <linux/slab.h> /* For kmalloc */
  69. #include <asm/uaccess.h> /* For copy_to_user/put_user/... */
  70. #include <asm/io.h> /* For inb/outb/... */
  71. /* Module and version information */
  72. #define WD_VER "1.16 (03/01/2006)"
  73. #define PFX "pcwd: "
  74. /*
  75. * It should be noted that PCWD_REVISION_B was removed because A and B
  76. * are essentially the same types of card, with the exception that B
  77. * has temperature reporting. Since I didn't receive a Rev.B card,
  78. * the Rev.B card is not supported. (It's a good thing too, as they
  79. * are no longer in production.)
  80. */
  81. #define PCWD_REVISION_A 1
  82. #define PCWD_REVISION_C 2
  83. /*
  84. * These are the defines that describe the control status bits for the
  85. * PCI-PC Watchdog card.
  86. */
  87. /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
  88. #define WD_WDRST 0x01 /* Previously reset state */
  89. #define WD_T110 0x02 /* Temperature overheat sense */
  90. #define WD_HRTBT 0x04 /* Heartbeat sense */
  91. #define WD_RLY2 0x08 /* External relay triggered */
  92. #define WD_SRLY2 0x80 /* Software external relay triggered */
  93. /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
  94. #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
  95. #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
  96. #define WD_REVC_TTRP 0x04 /* Temperature Trip status */
  97. /* Port 2 : Control Status #2 */
  98. #define WD_WDIS 0x10 /* Watchdog Disabled */
  99. #define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
  100. #define WD_SSEL 0x40 /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */
  101. #define WD_WCMD 0x80 /* Watchdog Command Mode */
  102. /* max. time we give an ISA watchdog card to process a command */
  103. /* 500ms for each 4 bit response (according to spec.) */
  104. #define ISA_COMMAND_TIMEOUT 1000
  105. /* Watchdog's internal commands */
  106. #define CMD_ISA_IDLE 0x00
  107. #define CMD_ISA_VERSION_INTEGER 0x01
  108. #define CMD_ISA_VERSION_TENTH 0x02
  109. #define CMD_ISA_VERSION_HUNDRETH 0x03
  110. #define CMD_ISA_VERSION_MINOR 0x04
  111. #define CMD_ISA_SWITCH_SETTINGS 0x05
  112. #define CMD_ISA_DELAY_TIME_2SECS 0x0A
  113. #define CMD_ISA_DELAY_TIME_4SECS 0x0B
  114. #define CMD_ISA_DELAY_TIME_8SECS 0x0C
  115. /*
  116. * We are using an kernel timer to do the pinging of the watchdog
  117. * every ~500ms. We try to set the internal heartbeat of the
  118. * watchdog to 2 ms.
  119. */
  120. #define WDT_INTERVAL (HZ/2+1)
  121. /* We can only use 1 card due to the /dev/watchdog restriction */
  122. static int cards_found;
  123. /* internal variables */
  124. static atomic_t open_allowed = ATOMIC_INIT(1);
  125. static char expect_close;
  126. static int temp_panic;
  127. static struct { /* this is private data for each ISA-PC watchdog card */
  128. int revision; /* The card's revision */
  129. int supports_temp; /* Wether or not the card has a temperature device */
  130. int command_mode; /* Wether or not the card is in command mode */
  131. int boot_status; /* The card's boot status */
  132. int io_addr; /* The cards I/O address */
  133. spinlock_t io_lock; /* the lock for io operations */
  134. struct timer_list timer; /* The timer that pings the watchdog */
  135. unsigned long next_heartbeat; /* the next_heartbeat for the timer */
  136. } pcwd_private;
  137. /* module parameters */
  138. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
  139. static int heartbeat = WATCHDOG_HEARTBEAT;
  140. module_param(heartbeat, int, 0);
  141. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  142. static int nowayout = WATCHDOG_NOWAYOUT;
  143. module_param(nowayout, int, 0);
  144. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  145. /*
  146. * Internal functions
  147. */
  148. static int send_isa_command(int cmd)
  149. {
  150. int i;
  151. int control_status;
  152. int port0, last_port0; /* Double read for stabilising */
  153. /* The WCMD bit must be 1 and the command is only 4 bits in size */
  154. control_status = (cmd & 0x0F) | WD_WCMD;
  155. outb_p(control_status, pcwd_private.io_addr + 2);
  156. udelay(ISA_COMMAND_TIMEOUT);
  157. port0 = inb_p(pcwd_private.io_addr);
  158. for (i = 0; i < 25; ++i) {
  159. last_port0 = port0;
  160. port0 = inb_p(pcwd_private.io_addr);
  161. if (port0 == last_port0)
  162. break; /* Data is stable */
  163. udelay (250);
  164. }
  165. return port0;
  166. }
  167. static int set_command_mode(void)
  168. {
  169. int i, found=0, count=0;
  170. /* Set the card into command mode */
  171. spin_lock(&pcwd_private.io_lock);
  172. while ((!found) && (count < 3)) {
  173. i = send_isa_command(CMD_ISA_IDLE);
  174. if (i == 0x00)
  175. found = 1;
  176. else if (i == 0xF3) {
  177. /* Card does not like what we've done to it */
  178. outb_p(0x00, pcwd_private.io_addr + 2);
  179. udelay(1200); /* Spec says wait 1ms */
  180. outb_p(0x00, pcwd_private.io_addr + 2);
  181. udelay(ISA_COMMAND_TIMEOUT);
  182. }
  183. count++;
  184. }
  185. spin_unlock(&pcwd_private.io_lock);
  186. pcwd_private.command_mode = found;
  187. return(found);
  188. }
  189. static void unset_command_mode(void)
  190. {
  191. /* Set the card into normal mode */
  192. spin_lock(&pcwd_private.io_lock);
  193. outb_p(0x00, pcwd_private.io_addr + 2);
  194. udelay(ISA_COMMAND_TIMEOUT);
  195. spin_unlock(&pcwd_private.io_lock);
  196. pcwd_private.command_mode = 0;
  197. }
  198. static void pcwd_timer_ping(unsigned long data)
  199. {
  200. int wdrst_stat;
  201. /* If we got a heartbeat pulse within the WDT_INTERVAL
  202. * we agree to ping the WDT */
  203. if(time_before(jiffies, pcwd_private.next_heartbeat)) {
  204. /* Ping the watchdog */
  205. spin_lock(&pcwd_private.io_lock);
  206. if (pcwd_private.revision == PCWD_REVISION_A) {
  207. /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
  208. wdrst_stat = inb_p(pcwd_private.io_addr);
  209. wdrst_stat &= 0x0F;
  210. wdrst_stat |= WD_WDRST;
  211. outb_p(wdrst_stat, pcwd_private.io_addr + 1);
  212. } else {
  213. /* Re-trigger watchdog by writing to port 0 */
  214. outb_p(0x00, pcwd_private.io_addr);
  215. }
  216. /* Re-set the timer interval */
  217. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  218. spin_unlock(&pcwd_private.io_lock);
  219. } else {
  220. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  221. }
  222. }
  223. static int pcwd_start(void)
  224. {
  225. int stat_reg;
  226. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  227. /* Start the timer */
  228. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  229. /* Enable the port */
  230. if (pcwd_private.revision == PCWD_REVISION_C) {
  231. spin_lock(&pcwd_private.io_lock);
  232. outb_p(0x00, pcwd_private.io_addr + 3);
  233. udelay(ISA_COMMAND_TIMEOUT);
  234. stat_reg = inb_p(pcwd_private.io_addr + 2);
  235. spin_unlock(&pcwd_private.io_lock);
  236. if (stat_reg & WD_WDIS) {
  237. printk(KERN_INFO PFX "Could not start watchdog\n");
  238. return -EIO;
  239. }
  240. }
  241. return 0;
  242. }
  243. static int pcwd_stop(void)
  244. {
  245. int stat_reg;
  246. /* Stop the timer */
  247. del_timer(&pcwd_private.timer);
  248. /* Disable the board */
  249. if (pcwd_private.revision == PCWD_REVISION_C) {
  250. spin_lock(&pcwd_private.io_lock);
  251. outb_p(0xA5, pcwd_private.io_addr + 3);
  252. udelay(ISA_COMMAND_TIMEOUT);
  253. outb_p(0xA5, pcwd_private.io_addr + 3);
  254. udelay(ISA_COMMAND_TIMEOUT);
  255. stat_reg = inb_p(pcwd_private.io_addr + 2);
  256. spin_unlock(&pcwd_private.io_lock);
  257. if ((stat_reg & WD_WDIS) == 0) {
  258. printk(KERN_INFO PFX "Could not stop watchdog\n");
  259. return -EIO;
  260. }
  261. }
  262. return 0;
  263. }
  264. static int pcwd_keepalive(void)
  265. {
  266. /* user land ping */
  267. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  268. return 0;
  269. }
  270. static int pcwd_set_heartbeat(int t)
  271. {
  272. if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
  273. return -EINVAL;
  274. heartbeat = t;
  275. return 0;
  276. }
  277. static int pcwd_get_status(int *status)
  278. {
  279. int card_status;
  280. *status=0;
  281. spin_lock(&pcwd_private.io_lock);
  282. if (pcwd_private.revision == PCWD_REVISION_A)
  283. /* Rev A cards return status information from
  284. * the base register, which is used for the
  285. * temperature in other cards. */
  286. card_status = inb(pcwd_private.io_addr);
  287. else {
  288. /* Rev C cards return card status in the base
  289. * address + 1 register. And use different bits
  290. * to indicate a card initiated reset, and an
  291. * over-temperature condition. And the reboot
  292. * status can be reset. */
  293. card_status = inb(pcwd_private.io_addr + 1);
  294. }
  295. spin_unlock(&pcwd_private.io_lock);
  296. if (pcwd_private.revision == PCWD_REVISION_A) {
  297. if (card_status & WD_WDRST)
  298. *status |= WDIOF_CARDRESET;
  299. if (card_status & WD_T110) {
  300. *status |= WDIOF_OVERHEAT;
  301. if (temp_panic) {
  302. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  303. kernel_power_off();
  304. }
  305. }
  306. } else {
  307. if (card_status & WD_REVC_WTRP)
  308. *status |= WDIOF_CARDRESET;
  309. if (card_status & WD_REVC_TTRP) {
  310. *status |= WDIOF_OVERHEAT;
  311. if (temp_panic) {
  312. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  313. kernel_power_off();
  314. }
  315. }
  316. }
  317. return 0;
  318. }
  319. static int pcwd_clear_status(void)
  320. {
  321. if (pcwd_private.revision == PCWD_REVISION_C) {
  322. spin_lock(&pcwd_private.io_lock);
  323. outb_p(0x00, pcwd_private.io_addr + 1); /* clear reset status */
  324. spin_unlock(&pcwd_private.io_lock);
  325. }
  326. return 0;
  327. }
  328. static int pcwd_get_temperature(int *temperature)
  329. {
  330. /* check that port 0 gives temperature info and no command results */
  331. if (pcwd_private.command_mode)
  332. return -1;
  333. *temperature = 0;
  334. if (!pcwd_private.supports_temp)
  335. return -ENODEV;
  336. /*
  337. * Convert celsius to fahrenheit, since this was
  338. * the decided 'standard' for this return value.
  339. */
  340. spin_lock(&pcwd_private.io_lock);
  341. *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
  342. spin_unlock(&pcwd_private.io_lock);
  343. return 0;
  344. }
  345. /*
  346. * /dev/watchdog handling
  347. */
  348. static int pcwd_ioctl(struct inode *inode, struct file *file,
  349. unsigned int cmd, unsigned long arg)
  350. {
  351. int rv;
  352. int status;
  353. int temperature;
  354. int new_heartbeat;
  355. int __user *argp = (int __user *)arg;
  356. static struct watchdog_info ident = {
  357. .options = WDIOF_OVERHEAT |
  358. WDIOF_CARDRESET |
  359. WDIOF_KEEPALIVEPING |
  360. WDIOF_SETTIMEOUT |
  361. WDIOF_MAGICCLOSE,
  362. .firmware_version = 1,
  363. .identity = "PCWD",
  364. };
  365. switch(cmd) {
  366. default:
  367. return -ENOIOCTLCMD;
  368. case WDIOC_GETSUPPORT:
  369. if(copy_to_user(argp, &ident, sizeof(ident)))
  370. return -EFAULT;
  371. return 0;
  372. case WDIOC_GETSTATUS:
  373. pcwd_get_status(&status);
  374. return put_user(status, argp);
  375. case WDIOC_GETBOOTSTATUS:
  376. return put_user(pcwd_private.boot_status, argp);
  377. case WDIOC_GETTEMP:
  378. if (pcwd_get_temperature(&temperature))
  379. return -EFAULT;
  380. return put_user(temperature, argp);
  381. case WDIOC_SETOPTIONS:
  382. if (pcwd_private.revision == PCWD_REVISION_C)
  383. {
  384. if(copy_from_user(&rv, argp, sizeof(int)))
  385. return -EFAULT;
  386. if (rv & WDIOS_DISABLECARD)
  387. {
  388. return pcwd_stop();
  389. }
  390. if (rv & WDIOS_ENABLECARD)
  391. {
  392. return pcwd_start();
  393. }
  394. if (rv & WDIOS_TEMPPANIC)
  395. {
  396. temp_panic = 1;
  397. }
  398. }
  399. return -EINVAL;
  400. case WDIOC_KEEPALIVE:
  401. pcwd_keepalive();
  402. return 0;
  403. case WDIOC_SETTIMEOUT:
  404. if (get_user(new_heartbeat, argp))
  405. return -EFAULT;
  406. if (pcwd_set_heartbeat(new_heartbeat))
  407. return -EINVAL;
  408. pcwd_keepalive();
  409. /* Fall */
  410. case WDIOC_GETTIMEOUT:
  411. return put_user(heartbeat, argp);
  412. }
  413. return 0;
  414. }
  415. static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  416. loff_t *ppos)
  417. {
  418. if (len) {
  419. if (!nowayout) {
  420. size_t i;
  421. /* In case it was set long ago */
  422. expect_close = 0;
  423. for (i = 0; i != len; i++) {
  424. char c;
  425. if (get_user(c, buf + i))
  426. return -EFAULT;
  427. if (c == 'V')
  428. expect_close = 42;
  429. }
  430. }
  431. pcwd_keepalive();
  432. }
  433. return len;
  434. }
  435. static int pcwd_open(struct inode *inode, struct file *file)
  436. {
  437. if (!atomic_dec_and_test(&open_allowed) ) {
  438. atomic_inc( &open_allowed );
  439. return -EBUSY;
  440. }
  441. if (nowayout)
  442. __module_get(THIS_MODULE);
  443. /* Activate */
  444. pcwd_start();
  445. pcwd_keepalive();
  446. return nonseekable_open(inode, file);
  447. }
  448. static int pcwd_close(struct inode *inode, struct file *file)
  449. {
  450. if (expect_close == 42) {
  451. pcwd_stop();
  452. } else {
  453. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  454. pcwd_keepalive();
  455. }
  456. expect_close = 0;
  457. atomic_inc( &open_allowed );
  458. return 0;
  459. }
  460. /*
  461. * /dev/temperature handling
  462. */
  463. static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  464. loff_t *ppos)
  465. {
  466. int temperature;
  467. if (pcwd_get_temperature(&temperature))
  468. return -EFAULT;
  469. if (copy_to_user(buf, &temperature, 1))
  470. return -EFAULT;
  471. return 1;
  472. }
  473. static int pcwd_temp_open(struct inode *inode, struct file *file)
  474. {
  475. if (!pcwd_private.supports_temp)
  476. return -ENODEV;
  477. return nonseekable_open(inode, file);
  478. }
  479. static int pcwd_temp_close(struct inode *inode, struct file *file)
  480. {
  481. return 0;
  482. }
  483. /*
  484. * Notify system
  485. */
  486. static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  487. {
  488. if (code==SYS_DOWN || code==SYS_HALT) {
  489. /* Turn the WDT off */
  490. pcwd_stop();
  491. }
  492. return NOTIFY_DONE;
  493. }
  494. /*
  495. * Kernel Interfaces
  496. */
  497. static struct file_operations pcwd_fops = {
  498. .owner = THIS_MODULE,
  499. .llseek = no_llseek,
  500. .write = pcwd_write,
  501. .ioctl = pcwd_ioctl,
  502. .open = pcwd_open,
  503. .release = pcwd_close,
  504. };
  505. static struct miscdevice pcwd_miscdev = {
  506. .minor = WATCHDOG_MINOR,
  507. .name = "watchdog",
  508. .fops = &pcwd_fops,
  509. };
  510. static struct file_operations pcwd_temp_fops = {
  511. .owner = THIS_MODULE,
  512. .llseek = no_llseek,
  513. .read = pcwd_temp_read,
  514. .open = pcwd_temp_open,
  515. .release = pcwd_temp_close,
  516. };
  517. static struct miscdevice temp_miscdev = {
  518. .minor = TEMP_MINOR,
  519. .name = "temperature",
  520. .fops = &pcwd_temp_fops,
  521. };
  522. static struct notifier_block pcwd_notifier = {
  523. .notifier_call = pcwd_notify_sys,
  524. };
  525. /*
  526. * Init & exit routines
  527. */
  528. static inline void get_support(void)
  529. {
  530. if (inb(pcwd_private.io_addr) != 0xF0)
  531. pcwd_private.supports_temp = 1;
  532. }
  533. static inline int get_revision(void)
  534. {
  535. int r = PCWD_REVISION_C;
  536. spin_lock(&pcwd_private.io_lock);
  537. /* REV A cards use only 2 io ports; test
  538. * presumes a floating bus reads as 0xff. */
  539. if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
  540. (inb(pcwd_private.io_addr + 3) == 0xFF))
  541. r=PCWD_REVISION_A;
  542. spin_unlock(&pcwd_private.io_lock);
  543. return r;
  544. }
  545. static inline char *get_firmware(void)
  546. {
  547. int one, ten, hund, minor;
  548. char *ret;
  549. ret = kmalloc(6, GFP_KERNEL);
  550. if(ret == NULL)
  551. return NULL;
  552. if (set_command_mode()) {
  553. one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  554. ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  555. hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  556. minor = send_isa_command(CMD_ISA_VERSION_MINOR);
  557. sprintf(ret, "%c.%c%c%c", one, ten, hund, minor);
  558. }
  559. else
  560. sprintf(ret, "ERROR");
  561. unset_command_mode();
  562. return(ret);
  563. }
  564. static inline int get_option_switches(void)
  565. {
  566. int rv=0;
  567. if (set_command_mode()) {
  568. /* Get switch settings */
  569. rv = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  570. }
  571. unset_command_mode();
  572. return(rv);
  573. }
  574. static int __devinit pcwatchdog_init(int base_addr)
  575. {
  576. int ret;
  577. char *firmware;
  578. int option_switches;
  579. cards_found++;
  580. if (cards_found == 1)
  581. printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
  582. if (cards_found > 1) {
  583. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  584. return -ENODEV;
  585. }
  586. if (base_addr == 0x0000) {
  587. printk(KERN_ERR PFX "No I/O-Address for card detected\n");
  588. return -ENODEV;
  589. }
  590. pcwd_private.io_addr = base_addr;
  591. /* Check card's revision */
  592. pcwd_private.revision = get_revision();
  593. if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
  594. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  595. pcwd_private.io_addr);
  596. pcwd_private.io_addr = 0x0000;
  597. return -EIO;
  598. }
  599. /* Initial variables */
  600. pcwd_private.supports_temp = 0;
  601. temp_panic = 0;
  602. pcwd_private.boot_status = 0x0000;
  603. /* get the boot_status */
  604. pcwd_get_status(&pcwd_private.boot_status);
  605. /* clear the "card caused reboot" flag */
  606. pcwd_clear_status();
  607. init_timer(&pcwd_private.timer);
  608. pcwd_private.timer.function = pcwd_timer_ping;
  609. pcwd_private.timer.data = 0;
  610. /* Disable the board */
  611. pcwd_stop();
  612. /* Check whether or not the card supports the temperature device */
  613. get_support();
  614. /* Get some extra info from the hardware (in command/debug/diag mode) */
  615. if (pcwd_private.revision == PCWD_REVISION_A)
  616. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
  617. else if (pcwd_private.revision == PCWD_REVISION_C) {
  618. firmware = get_firmware();
  619. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
  620. pcwd_private.io_addr, firmware);
  621. kfree(firmware);
  622. option_switches = get_option_switches();
  623. printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
  624. option_switches,
  625. ((option_switches & 0x10) ? "ON" : "OFF"),
  626. ((option_switches & 0x08) ? "ON" : "OFF"));
  627. /* Reprogram internal heartbeat to 2 seconds */
  628. if (set_command_mode()) {
  629. send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  630. unset_command_mode();
  631. }
  632. } else {
  633. /* Should NEVER happen, unless get_revision() fails. */
  634. printk(KERN_INFO PFX "Unable to get revision\n");
  635. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  636. pcwd_private.io_addr = 0x0000;
  637. return -1;
  638. }
  639. if (pcwd_private.supports_temp)
  640. printk(KERN_INFO PFX "Temperature Option Detected\n");
  641. if (pcwd_private.boot_status & WDIOF_CARDRESET)
  642. printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
  643. if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
  644. printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
  645. printk(KERN_EMERG PFX "CPU Overheat\n");
  646. }
  647. if (pcwd_private.boot_status == 0)
  648. printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
  649. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  650. if (pcwd_set_heartbeat(heartbeat)) {
  651. pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  652. printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
  653. WATCHDOG_HEARTBEAT);
  654. }
  655. ret = register_reboot_notifier(&pcwd_notifier);
  656. if (ret) {
  657. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  658. ret);
  659. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  660. pcwd_private.io_addr = 0x0000;
  661. return ret;
  662. }
  663. if (pcwd_private.supports_temp) {
  664. ret = misc_register(&temp_miscdev);
  665. if (ret) {
  666. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  667. TEMP_MINOR, ret);
  668. unregister_reboot_notifier(&pcwd_notifier);
  669. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  670. pcwd_private.io_addr = 0x0000;
  671. return ret;
  672. }
  673. }
  674. ret = misc_register(&pcwd_miscdev);
  675. if (ret) {
  676. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  677. WATCHDOG_MINOR, ret);
  678. if (pcwd_private.supports_temp)
  679. misc_deregister(&temp_miscdev);
  680. unregister_reboot_notifier(&pcwd_notifier);
  681. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  682. pcwd_private.io_addr = 0x0000;
  683. return ret;
  684. }
  685. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  686. heartbeat, nowayout);
  687. return 0;
  688. }
  689. static void __devexit pcwatchdog_exit(void)
  690. {
  691. /* Disable the board */
  692. if (!nowayout)
  693. pcwd_stop();
  694. /* Deregister */
  695. misc_deregister(&pcwd_miscdev);
  696. if (pcwd_private.supports_temp)
  697. misc_deregister(&temp_miscdev);
  698. unregister_reboot_notifier(&pcwd_notifier);
  699. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  700. pcwd_private.io_addr = 0x0000;
  701. cards_found--;
  702. }
  703. /*
  704. * The ISA cards have a heartbeat bit in one of the registers, which
  705. * register is card dependent. The heartbeat bit is monitored, and if
  706. * found, is considered proof that a Berkshire card has been found.
  707. * The initial rate is once per second at board start up, then twice
  708. * per second for normal operation.
  709. */
  710. static int __init pcwd_checkcard(int base_addr)
  711. {
  712. int port0, last_port0; /* Reg 0, in case it's REV A */
  713. int port1, last_port1; /* Register 1 for REV C cards */
  714. int i;
  715. int retval;
  716. if (!request_region (base_addr, 4, "PCWD")) {
  717. printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
  718. return 0;
  719. }
  720. retval = 0;
  721. port0 = inb_p(base_addr); /* For REV A boards */
  722. port1 = inb_p(base_addr + 1); /* For REV C boards */
  723. if (port0 != 0xff || port1 != 0xff) {
  724. /* Not an 'ff' from a floating bus, so must be a card! */
  725. for (i = 0; i < 4; ++i) {
  726. msleep(500);
  727. last_port0 = port0;
  728. last_port1 = port1;
  729. port0 = inb_p(base_addr);
  730. port1 = inb_p(base_addr + 1);
  731. /* Has either hearbeat bit changed? */
  732. if ((port0 ^ last_port0) & WD_HRTBT ||
  733. (port1 ^ last_port1) & WD_REVC_HRBT) {
  734. retval = 1;
  735. break;
  736. }
  737. }
  738. }
  739. release_region (base_addr, 4);
  740. return retval;
  741. }
  742. /*
  743. * These are the auto-probe addresses available.
  744. *
  745. * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
  746. * Revision A has an address range of 2 addresses, while Revision C has 4.
  747. */
  748. static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
  749. static int __init pcwd_init_module(void)
  750. {
  751. int i, found = 0;
  752. spin_lock_init(&pcwd_private.io_lock);
  753. for (i = 0; pcwd_ioports[i] != 0; i++) {
  754. if (pcwd_checkcard(pcwd_ioports[i])) {
  755. if (!(pcwatchdog_init(pcwd_ioports[i])))
  756. found++;
  757. }
  758. }
  759. if (!found) {
  760. printk (KERN_INFO PFX "No card detected, or port not available\n");
  761. return -ENODEV;
  762. }
  763. return 0;
  764. }
  765. static void __exit pcwd_cleanup_module(void)
  766. {
  767. if (pcwd_private.io_addr)
  768. pcwatchdog_exit();
  769. return;
  770. }
  771. module_init(pcwd_init_module);
  772. module_exit(pcwd_cleanup_module);
  773. MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
  774. MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
  775. MODULE_LICENSE("GPL");
  776. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  777. MODULE_ALIAS_MISCDEV(TEMP_MINOR);