pcwd.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 <asm/uaccess.h> /* For copy_to_user/put_user/... */
  68. #include <asm/io.h> /* For inb/outb/... */
  69. /* Module and version information */
  70. #define WATCHDOG_VERSION "1.17"
  71. #define WATCHDOG_DATE "12 Feb 2006"
  72. #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
  73. #define WATCHDOG_NAME "pcwd"
  74. #define PFX WATCHDOG_NAME ": "
  75. #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION " (" WATCHDOG_DATE ")\n"
  76. #define WD_VER WATCHDOG_VERSION " (" WATCHDOG_DATE ")"
  77. /*
  78. * It should be noted that PCWD_REVISION_B was removed because A and B
  79. * are essentially the same types of card, with the exception that B
  80. * has temperature reporting. Since I didn't receive a Rev.B card,
  81. * the Rev.B card is not supported. (It's a good thing too, as they
  82. * are no longer in production.)
  83. */
  84. #define PCWD_REVISION_A 1
  85. #define PCWD_REVISION_C 2
  86. /*
  87. * These are the defines that describe the control status bits for the
  88. * PCI-PC Watchdog card.
  89. */
  90. /* Port 1 : Control Status #1 for the PC Watchdog card, revision A. */
  91. #define WD_WDRST 0x01 /* Previously reset state */
  92. #define WD_T110 0x02 /* Temperature overheat sense */
  93. #define WD_HRTBT 0x04 /* Heartbeat sense */
  94. #define WD_RLY2 0x08 /* External relay triggered */
  95. #define WD_SRLY2 0x80 /* Software external relay triggered */
  96. /* Port 1 : Control Status #1 for the PC Watchdog card, revision C. */
  97. #define WD_REVC_WTRP 0x01 /* Watchdog Trip status */
  98. #define WD_REVC_HRBT 0x02 /* Watchdog Heartbeat */
  99. #define WD_REVC_TTRP 0x04 /* Temperature Trip status */
  100. #define WD_REVC_RL2A 0x08 /* Relay 2 activated by on-board processor */
  101. #define WD_REVC_RL1A 0x10 /* Relay 1 active */
  102. #define WD_REVC_R2DS 0x40 /* Relay 2 disable */
  103. #define WD_REVC_RLY2 0x80 /* Relay 2 activated? */
  104. /* Port 2 : Control Status #2 */
  105. #define WD_WDIS 0x10 /* Watchdog Disabled */
  106. #define WD_ENTP 0x20 /* Watchdog Enable Temperature Trip */
  107. #define WD_SSEL 0x40 /* Watchdog Switch Select (1:SW1 <-> 0:SW2) */
  108. #define WD_WCMD 0x80 /* Watchdog Command Mode */
  109. /* max. time we give an ISA watchdog card to process a command */
  110. /* 500ms for each 4 bit response (according to spec.) */
  111. #define ISA_COMMAND_TIMEOUT 1000
  112. /* Watchdog's internal commands */
  113. #define CMD_ISA_IDLE 0x00
  114. #define CMD_ISA_VERSION_INTEGER 0x01
  115. #define CMD_ISA_VERSION_TENTH 0x02
  116. #define CMD_ISA_VERSION_HUNDRETH 0x03
  117. #define CMD_ISA_VERSION_MINOR 0x04
  118. #define CMD_ISA_SWITCH_SETTINGS 0x05
  119. #define CMD_ISA_RESET_PC 0x06
  120. #define CMD_ISA_ARM_0 0x07
  121. #define CMD_ISA_ARM_30 0x08
  122. #define CMD_ISA_ARM_60 0x09
  123. #define CMD_ISA_DELAY_TIME_2SECS 0x0A
  124. #define CMD_ISA_DELAY_TIME_4SECS 0x0B
  125. #define CMD_ISA_DELAY_TIME_8SECS 0x0C
  126. #define CMD_ISA_RESET_RELAYS 0x0D
  127. /*
  128. * We are using an kernel timer to do the pinging of the watchdog
  129. * every ~500ms. We try to set the internal heartbeat of the
  130. * watchdog to 2 ms.
  131. */
  132. #define WDT_INTERVAL (HZ/2+1)
  133. /* We can only use 1 card due to the /dev/watchdog restriction */
  134. static int cards_found;
  135. /* internal variables */
  136. static atomic_t open_allowed = ATOMIC_INIT(1);
  137. static char expect_close;
  138. static int temp_panic;
  139. static struct { /* this is private data for each ISA-PC watchdog card */
  140. char fw_ver_str[6]; /* The cards firmware version */
  141. int revision; /* The card's revision */
  142. int supports_temp; /* Wether or not the card has a temperature device */
  143. int command_mode; /* Wether or not the card is in command mode */
  144. int boot_status; /* The card's boot status */
  145. int io_addr; /* The cards I/O address */
  146. spinlock_t io_lock; /* the lock for io operations */
  147. struct timer_list timer; /* The timer that pings the watchdog */
  148. unsigned long next_heartbeat; /* the next_heartbeat for the timer */
  149. } pcwd_private;
  150. /* module parameters */
  151. #define QUIET 0 /* Default */
  152. #define VERBOSE 1 /* Verbose */
  153. #define DEBUG 2 /* print fancy stuff too */
  154. static int debug = QUIET;
  155. module_param(debug, int, 0);
  156. MODULE_PARM_DESC(debug, "Debug level: 0=Quiet, 1=Verbose, 2=Debug (default=0)");
  157. #define WATCHDOG_HEARTBEAT 60 /* 60 sec default heartbeat */
  158. static int heartbeat = WATCHDOG_HEARTBEAT;
  159. module_param(heartbeat, int, 0);
  160. MODULE_PARM_DESC(heartbeat, "Watchdog heartbeat in seconds. (2<=heartbeat<=7200, default=" __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  161. static int nowayout = WATCHDOG_NOWAYOUT;
  162. module_param(nowayout, int, 0);
  163. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  164. /*
  165. * Internal functions
  166. */
  167. static int send_isa_command(int cmd)
  168. {
  169. int i;
  170. int control_status;
  171. int port0, last_port0; /* Double read for stabilising */
  172. if (debug >= DEBUG)
  173. printk(KERN_DEBUG PFX "sending following data cmd=0x%02x\n",
  174. cmd);
  175. /* The WCMD bit must be 1 and the command is only 4 bits in size */
  176. control_status = (cmd & 0x0F) | WD_WCMD;
  177. outb_p(control_status, pcwd_private.io_addr + 2);
  178. udelay(ISA_COMMAND_TIMEOUT);
  179. port0 = inb_p(pcwd_private.io_addr);
  180. for (i = 0; i < 25; ++i) {
  181. last_port0 = port0;
  182. port0 = inb_p(pcwd_private.io_addr);
  183. if (port0 == last_port0)
  184. break; /* Data is stable */
  185. udelay (250);
  186. }
  187. if (debug >= DEBUG)
  188. printk(KERN_DEBUG PFX "received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
  189. cmd, port0, last_port0);
  190. return port0;
  191. }
  192. static int set_command_mode(void)
  193. {
  194. int i, found=0, count=0;
  195. /* Set the card into command mode */
  196. spin_lock(&pcwd_private.io_lock);
  197. while ((!found) && (count < 3)) {
  198. i = send_isa_command(CMD_ISA_IDLE);
  199. if (i == 0x00)
  200. found = 1;
  201. else if (i == 0xF3) {
  202. /* Card does not like what we've done to it */
  203. outb_p(0x00, pcwd_private.io_addr + 2);
  204. udelay(1200); /* Spec says wait 1ms */
  205. outb_p(0x00, pcwd_private.io_addr + 2);
  206. udelay(ISA_COMMAND_TIMEOUT);
  207. }
  208. count++;
  209. }
  210. spin_unlock(&pcwd_private.io_lock);
  211. pcwd_private.command_mode = found;
  212. if (debug >= DEBUG)
  213. printk(KERN_DEBUG PFX "command_mode=%d\n",
  214. pcwd_private.command_mode);
  215. return(found);
  216. }
  217. static void unset_command_mode(void)
  218. {
  219. /* Set the card into normal mode */
  220. spin_lock(&pcwd_private.io_lock);
  221. outb_p(0x00, pcwd_private.io_addr + 2);
  222. udelay(ISA_COMMAND_TIMEOUT);
  223. spin_unlock(&pcwd_private.io_lock);
  224. pcwd_private.command_mode = 0;
  225. if (debug >= DEBUG)
  226. printk(KERN_DEBUG PFX "command_mode=%d\n",
  227. pcwd_private.command_mode);
  228. }
  229. static inline void pcwd_check_temperature_support(void)
  230. {
  231. if (inb(pcwd_private.io_addr) != 0xF0)
  232. pcwd_private.supports_temp = 1;
  233. }
  234. static inline void pcwd_get_firmware(void)
  235. {
  236. int one, ten, hund, minor;
  237. strcpy(pcwd_private.fw_ver_str, "ERROR");
  238. if (set_command_mode()) {
  239. one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  240. ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  241. hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  242. minor = send_isa_command(CMD_ISA_VERSION_MINOR);
  243. sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c", one, ten, hund, minor);
  244. }
  245. unset_command_mode();
  246. return;
  247. }
  248. static inline int pcwd_get_option_switches(void)
  249. {
  250. int option_switches=0;
  251. if (set_command_mode()) {
  252. /* Get switch settings */
  253. option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  254. }
  255. unset_command_mode();
  256. return(option_switches);
  257. }
  258. static void pcwd_show_card_info(void)
  259. {
  260. int option_switches;
  261. /* Get some extra info from the hardware (in command/debug/diag mode) */
  262. if (pcwd_private.revision == PCWD_REVISION_A)
  263. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.A) detected at port 0x%04x\n", pcwd_private.io_addr);
  264. else if (pcwd_private.revision == PCWD_REVISION_C) {
  265. pcwd_get_firmware();
  266. printk(KERN_INFO PFX "ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
  267. pcwd_private.io_addr, pcwd_private.fw_ver_str);
  268. option_switches = pcwd_get_option_switches();
  269. printk(KERN_INFO PFX "Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
  270. option_switches,
  271. ((option_switches & 0x10) ? "ON" : "OFF"),
  272. ((option_switches & 0x08) ? "ON" : "OFF"));
  273. /* Reprogram internal heartbeat to 2 seconds */
  274. if (set_command_mode()) {
  275. send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  276. unset_command_mode();
  277. }
  278. }
  279. if (pcwd_private.supports_temp)
  280. printk(KERN_INFO PFX "Temperature Option Detected\n");
  281. if (pcwd_private.boot_status & WDIOF_CARDRESET)
  282. printk(KERN_INFO PFX "Previous reboot was caused by the card\n");
  283. if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
  284. printk(KERN_EMERG PFX "Card senses a CPU Overheat. Panicking!\n");
  285. printk(KERN_EMERG PFX "CPU Overheat\n");
  286. }
  287. if (pcwd_private.boot_status == 0)
  288. printk(KERN_INFO PFX "No previous trip detected - Cold boot or reset\n");
  289. }
  290. static void pcwd_timer_ping(unsigned long data)
  291. {
  292. int wdrst_stat;
  293. /* If we got a heartbeat pulse within the WDT_INTERVAL
  294. * we agree to ping the WDT */
  295. if(time_before(jiffies, pcwd_private.next_heartbeat)) {
  296. /* Ping the watchdog */
  297. spin_lock(&pcwd_private.io_lock);
  298. if (pcwd_private.revision == PCWD_REVISION_A) {
  299. /* Rev A cards are reset by setting the WD_WDRST bit in register 1 */
  300. wdrst_stat = inb_p(pcwd_private.io_addr);
  301. wdrst_stat &= 0x0F;
  302. wdrst_stat |= WD_WDRST;
  303. outb_p(wdrst_stat, pcwd_private.io_addr + 1);
  304. } else {
  305. /* Re-trigger watchdog by writing to port 0 */
  306. outb_p(0x00, pcwd_private.io_addr);
  307. }
  308. /* Re-set the timer interval */
  309. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  310. spin_unlock(&pcwd_private.io_lock);
  311. } else {
  312. printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
  313. }
  314. }
  315. static int pcwd_start(void)
  316. {
  317. int stat_reg;
  318. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  319. /* Start the timer */
  320. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  321. /* Enable the port */
  322. if (pcwd_private.revision == PCWD_REVISION_C) {
  323. spin_lock(&pcwd_private.io_lock);
  324. outb_p(0x00, pcwd_private.io_addr + 3);
  325. udelay(ISA_COMMAND_TIMEOUT);
  326. stat_reg = inb_p(pcwd_private.io_addr + 2);
  327. spin_unlock(&pcwd_private.io_lock);
  328. if (stat_reg & WD_WDIS) {
  329. printk(KERN_INFO PFX "Could not start watchdog\n");
  330. return -EIO;
  331. }
  332. }
  333. if (debug >= VERBOSE)
  334. printk(KERN_DEBUG PFX "Watchdog started\n");
  335. return 0;
  336. }
  337. static int pcwd_stop(void)
  338. {
  339. int stat_reg;
  340. /* Stop the timer */
  341. del_timer(&pcwd_private.timer);
  342. /* Disable the board */
  343. if (pcwd_private.revision == PCWD_REVISION_C) {
  344. spin_lock(&pcwd_private.io_lock);
  345. outb_p(0xA5, pcwd_private.io_addr + 3);
  346. udelay(ISA_COMMAND_TIMEOUT);
  347. outb_p(0xA5, pcwd_private.io_addr + 3);
  348. udelay(ISA_COMMAND_TIMEOUT);
  349. stat_reg = inb_p(pcwd_private.io_addr + 2);
  350. spin_unlock(&pcwd_private.io_lock);
  351. if ((stat_reg & WD_WDIS) == 0) {
  352. printk(KERN_INFO PFX "Could not stop watchdog\n");
  353. return -EIO;
  354. }
  355. }
  356. if (debug >= VERBOSE)
  357. printk(KERN_DEBUG PFX "Watchdog stopped\n");
  358. return 0;
  359. }
  360. static int pcwd_keepalive(void)
  361. {
  362. /* user land ping */
  363. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  364. if (debug >= DEBUG)
  365. printk(KERN_DEBUG PFX "Watchdog keepalive signal send\n");
  366. return 0;
  367. }
  368. static int pcwd_set_heartbeat(int t)
  369. {
  370. if ((t < 2) || (t > 7200)) /* arbitrary upper limit */
  371. return -EINVAL;
  372. heartbeat = t;
  373. if (debug >= VERBOSE)
  374. printk(KERN_DEBUG PFX "New heartbeat: %d\n",
  375. heartbeat);
  376. return 0;
  377. }
  378. static int pcwd_get_status(int *status)
  379. {
  380. int control_status;
  381. *status=0;
  382. spin_lock(&pcwd_private.io_lock);
  383. if (pcwd_private.revision == PCWD_REVISION_A)
  384. /* Rev A cards return status information from
  385. * the base register, which is used for the
  386. * temperature in other cards. */
  387. control_status = inb(pcwd_private.io_addr);
  388. else {
  389. /* Rev C cards return card status in the base
  390. * address + 1 register. And use different bits
  391. * to indicate a card initiated reset, and an
  392. * over-temperature condition. And the reboot
  393. * status can be reset. */
  394. control_status = inb(pcwd_private.io_addr + 1);
  395. }
  396. spin_unlock(&pcwd_private.io_lock);
  397. if (pcwd_private.revision == PCWD_REVISION_A) {
  398. if (control_status & WD_WDRST)
  399. *status |= WDIOF_CARDRESET;
  400. if (control_status & WD_T110) {
  401. *status |= WDIOF_OVERHEAT;
  402. if (temp_panic) {
  403. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  404. kernel_power_off();
  405. /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
  406. }
  407. }
  408. } else {
  409. if (control_status & WD_REVC_WTRP)
  410. *status |= WDIOF_CARDRESET;
  411. if (control_status & WD_REVC_TTRP) {
  412. *status |= WDIOF_OVERHEAT;
  413. if (temp_panic) {
  414. printk (KERN_INFO PFX "Temperature overheat trip!\n");
  415. kernel_power_off();
  416. /* or should we just do a: panic(PFX "Temperature overheat trip!\n"); */
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422. static int pcwd_clear_status(void)
  423. {
  424. int control_status;
  425. if (pcwd_private.revision == PCWD_REVISION_C) {
  426. spin_lock(&pcwd_private.io_lock);
  427. if (debug >= VERBOSE)
  428. printk(KERN_INFO PFX "clearing watchdog trip status\n");
  429. control_status = inb_p(pcwd_private.io_addr + 1);
  430. if (debug >= DEBUG) {
  431. printk(KERN_DEBUG PFX "status was: 0x%02x\n", control_status);
  432. printk(KERN_DEBUG PFX "sending: 0x%02x\n",
  433. (control_status & WD_REVC_R2DS));
  434. }
  435. /* clear reset status & Keep Relay 2 disable state as it is */
  436. outb_p((control_status & WD_REVC_R2DS), pcwd_private.io_addr + 1);
  437. spin_unlock(&pcwd_private.io_lock);
  438. }
  439. return 0;
  440. }
  441. static int pcwd_get_temperature(int *temperature)
  442. {
  443. /* check that port 0 gives temperature info and no command results */
  444. if (pcwd_private.command_mode)
  445. return -1;
  446. *temperature = 0;
  447. if (!pcwd_private.supports_temp)
  448. return -ENODEV;
  449. /*
  450. * Convert celsius to fahrenheit, since this was
  451. * the decided 'standard' for this return value.
  452. */
  453. spin_lock(&pcwd_private.io_lock);
  454. *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
  455. spin_unlock(&pcwd_private.io_lock);
  456. if (debug >= DEBUG) {
  457. printk(KERN_DEBUG PFX "temperature is: %d F\n",
  458. *temperature);
  459. }
  460. return 0;
  461. }
  462. /*
  463. * /dev/watchdog handling
  464. */
  465. static int pcwd_ioctl(struct inode *inode, struct file *file,
  466. unsigned int cmd, unsigned long arg)
  467. {
  468. int rv;
  469. int status;
  470. int temperature;
  471. int new_heartbeat;
  472. int __user *argp = (int __user *)arg;
  473. static struct watchdog_info ident = {
  474. .options = WDIOF_OVERHEAT |
  475. WDIOF_CARDRESET |
  476. WDIOF_KEEPALIVEPING |
  477. WDIOF_SETTIMEOUT |
  478. WDIOF_MAGICCLOSE,
  479. .firmware_version = 1,
  480. .identity = "PCWD",
  481. };
  482. switch(cmd) {
  483. default:
  484. return -ENOIOCTLCMD;
  485. case WDIOC_GETSUPPORT:
  486. if(copy_to_user(argp, &ident, sizeof(ident)))
  487. return -EFAULT;
  488. return 0;
  489. case WDIOC_GETSTATUS:
  490. pcwd_get_status(&status);
  491. return put_user(status, argp);
  492. case WDIOC_GETBOOTSTATUS:
  493. return put_user(pcwd_private.boot_status, argp);
  494. case WDIOC_GETTEMP:
  495. if (pcwd_get_temperature(&temperature))
  496. return -EFAULT;
  497. return put_user(temperature, argp);
  498. case WDIOC_SETOPTIONS:
  499. if (pcwd_private.revision == PCWD_REVISION_C)
  500. {
  501. if(copy_from_user(&rv, argp, sizeof(int)))
  502. return -EFAULT;
  503. if (rv & WDIOS_DISABLECARD)
  504. {
  505. return pcwd_stop();
  506. }
  507. if (rv & WDIOS_ENABLECARD)
  508. {
  509. return pcwd_start();
  510. }
  511. if (rv & WDIOS_TEMPPANIC)
  512. {
  513. temp_panic = 1;
  514. }
  515. }
  516. return -EINVAL;
  517. case WDIOC_KEEPALIVE:
  518. pcwd_keepalive();
  519. return 0;
  520. case WDIOC_SETTIMEOUT:
  521. if (get_user(new_heartbeat, argp))
  522. return -EFAULT;
  523. if (pcwd_set_heartbeat(new_heartbeat))
  524. return -EINVAL;
  525. pcwd_keepalive();
  526. /* Fall */
  527. case WDIOC_GETTIMEOUT:
  528. return put_user(heartbeat, argp);
  529. }
  530. return 0;
  531. }
  532. static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  533. loff_t *ppos)
  534. {
  535. if (len) {
  536. if (!nowayout) {
  537. size_t i;
  538. /* In case it was set long ago */
  539. expect_close = 0;
  540. for (i = 0; i != len; i++) {
  541. char c;
  542. if (get_user(c, buf + i))
  543. return -EFAULT;
  544. if (c == 'V')
  545. expect_close = 42;
  546. }
  547. }
  548. pcwd_keepalive();
  549. }
  550. return len;
  551. }
  552. static int pcwd_open(struct inode *inode, struct file *file)
  553. {
  554. if (!atomic_dec_and_test(&open_allowed) ) {
  555. if (debug >= VERBOSE)
  556. printk(KERN_ERR PFX "Attempt to open already opened device.\n");
  557. atomic_inc( &open_allowed );
  558. return -EBUSY;
  559. }
  560. if (nowayout)
  561. __module_get(THIS_MODULE);
  562. /* Activate */
  563. pcwd_start();
  564. pcwd_keepalive();
  565. return nonseekable_open(inode, file);
  566. }
  567. static int pcwd_close(struct inode *inode, struct file *file)
  568. {
  569. if (expect_close == 42) {
  570. pcwd_stop();
  571. } else {
  572. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  573. pcwd_keepalive();
  574. }
  575. expect_close = 0;
  576. atomic_inc( &open_allowed );
  577. return 0;
  578. }
  579. /*
  580. * /dev/temperature handling
  581. */
  582. static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  583. loff_t *ppos)
  584. {
  585. int temperature;
  586. if (pcwd_get_temperature(&temperature))
  587. return -EFAULT;
  588. if (copy_to_user(buf, &temperature, 1))
  589. return -EFAULT;
  590. return 1;
  591. }
  592. static int pcwd_temp_open(struct inode *inode, struct file *file)
  593. {
  594. if (!pcwd_private.supports_temp)
  595. return -ENODEV;
  596. return nonseekable_open(inode, file);
  597. }
  598. static int pcwd_temp_close(struct inode *inode, struct file *file)
  599. {
  600. return 0;
  601. }
  602. /*
  603. * Notify system
  604. */
  605. static int pcwd_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
  606. {
  607. if (code==SYS_DOWN || code==SYS_HALT) {
  608. /* Turn the WDT off */
  609. pcwd_stop();
  610. }
  611. return NOTIFY_DONE;
  612. }
  613. /*
  614. * Kernel Interfaces
  615. */
  616. static struct file_operations pcwd_fops = {
  617. .owner = THIS_MODULE,
  618. .llseek = no_llseek,
  619. .write = pcwd_write,
  620. .ioctl = pcwd_ioctl,
  621. .open = pcwd_open,
  622. .release = pcwd_close,
  623. };
  624. static struct miscdevice pcwd_miscdev = {
  625. .minor = WATCHDOG_MINOR,
  626. .name = "watchdog",
  627. .fops = &pcwd_fops,
  628. };
  629. static struct file_operations pcwd_temp_fops = {
  630. .owner = THIS_MODULE,
  631. .llseek = no_llseek,
  632. .read = pcwd_temp_read,
  633. .open = pcwd_temp_open,
  634. .release = pcwd_temp_close,
  635. };
  636. static struct miscdevice temp_miscdev = {
  637. .minor = TEMP_MINOR,
  638. .name = "temperature",
  639. .fops = &pcwd_temp_fops,
  640. };
  641. static struct notifier_block pcwd_notifier = {
  642. .notifier_call = pcwd_notify_sys,
  643. };
  644. /*
  645. * Init & exit routines
  646. */
  647. static inline int get_revision(void)
  648. {
  649. int r = PCWD_REVISION_C;
  650. spin_lock(&pcwd_private.io_lock);
  651. /* REV A cards use only 2 io ports; test
  652. * presumes a floating bus reads as 0xff. */
  653. if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
  654. (inb(pcwd_private.io_addr + 3) == 0xFF))
  655. r=PCWD_REVISION_A;
  656. spin_unlock(&pcwd_private.io_lock);
  657. return r;
  658. }
  659. static int __devinit pcwatchdog_init(int base_addr)
  660. {
  661. int ret;
  662. cards_found++;
  663. if (cards_found == 1)
  664. printk(KERN_INFO PFX "v%s Ken Hollis (kenji@bitgate.com)\n", WD_VER);
  665. if (cards_found > 1) {
  666. printk(KERN_ERR PFX "This driver only supports 1 device\n");
  667. return -ENODEV;
  668. }
  669. if (base_addr == 0x0000) {
  670. printk(KERN_ERR PFX "No I/O-Address for card detected\n");
  671. return -ENODEV;
  672. }
  673. pcwd_private.io_addr = base_addr;
  674. /* Check card's revision */
  675. pcwd_private.revision = get_revision();
  676. if (!request_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
  677. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  678. pcwd_private.io_addr);
  679. pcwd_private.io_addr = 0x0000;
  680. return -EIO;
  681. }
  682. /* Initial variables */
  683. pcwd_private.supports_temp = 0;
  684. temp_panic = 0;
  685. pcwd_private.boot_status = 0x0000;
  686. /* get the boot_status */
  687. pcwd_get_status(&pcwd_private.boot_status);
  688. /* clear the "card caused reboot" flag */
  689. pcwd_clear_status();
  690. init_timer(&pcwd_private.timer);
  691. pcwd_private.timer.function = pcwd_timer_ping;
  692. pcwd_private.timer.data = 0;
  693. /* Disable the board */
  694. pcwd_stop();
  695. /* Check whether or not the card supports the temperature device */
  696. pcwd_check_temperature_support();
  697. /* Show info about the card itself */
  698. pcwd_show_card_info();
  699. /* Check that the heartbeat value is within it's range ; if not reset to the default */
  700. if (pcwd_set_heartbeat(heartbeat)) {
  701. pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  702. printk(KERN_INFO PFX "heartbeat value must be 2<=heartbeat<=7200, using %d\n",
  703. WATCHDOG_HEARTBEAT);
  704. }
  705. ret = register_reboot_notifier(&pcwd_notifier);
  706. if (ret) {
  707. printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  708. ret);
  709. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  710. pcwd_private.io_addr = 0x0000;
  711. return ret;
  712. }
  713. if (pcwd_private.supports_temp) {
  714. ret = misc_register(&temp_miscdev);
  715. if (ret) {
  716. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  717. TEMP_MINOR, ret);
  718. unregister_reboot_notifier(&pcwd_notifier);
  719. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  720. pcwd_private.io_addr = 0x0000;
  721. return ret;
  722. }
  723. }
  724. ret = misc_register(&pcwd_miscdev);
  725. if (ret) {
  726. printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  727. WATCHDOG_MINOR, ret);
  728. if (pcwd_private.supports_temp)
  729. misc_deregister(&temp_miscdev);
  730. unregister_reboot_notifier(&pcwd_notifier);
  731. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  732. pcwd_private.io_addr = 0x0000;
  733. return ret;
  734. }
  735. printk(KERN_INFO PFX "initialized. heartbeat=%d sec (nowayout=%d)\n",
  736. heartbeat, nowayout);
  737. return 0;
  738. }
  739. static void __devexit pcwatchdog_exit(void)
  740. {
  741. /* Disable the board */
  742. if (!nowayout)
  743. pcwd_stop();
  744. /* Deregister */
  745. misc_deregister(&pcwd_miscdev);
  746. if (pcwd_private.supports_temp)
  747. misc_deregister(&temp_miscdev);
  748. unregister_reboot_notifier(&pcwd_notifier);
  749. release_region(pcwd_private.io_addr, (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  750. pcwd_private.io_addr = 0x0000;
  751. cards_found--;
  752. }
  753. /*
  754. * The ISA cards have a heartbeat bit in one of the registers, which
  755. * register is card dependent. The heartbeat bit is monitored, and if
  756. * found, is considered proof that a Berkshire card has been found.
  757. * The initial rate is once per second at board start up, then twice
  758. * per second for normal operation.
  759. */
  760. static int __init pcwd_checkcard(int base_addr)
  761. {
  762. int port0, last_port0; /* Reg 0, in case it's REV A */
  763. int port1, last_port1; /* Register 1 for REV C cards */
  764. int i;
  765. int retval;
  766. if (!request_region (base_addr, 4, "PCWD")) {
  767. printk (KERN_INFO PFX "Port 0x%04x unavailable\n", base_addr);
  768. return 0;
  769. }
  770. retval = 0;
  771. port0 = inb_p(base_addr); /* For REV A boards */
  772. port1 = inb_p(base_addr + 1); /* For REV C boards */
  773. if (port0 != 0xff || port1 != 0xff) {
  774. /* Not an 'ff' from a floating bus, so must be a card! */
  775. for (i = 0; i < 4; ++i) {
  776. msleep(500);
  777. last_port0 = port0;
  778. last_port1 = port1;
  779. port0 = inb_p(base_addr);
  780. port1 = inb_p(base_addr + 1);
  781. /* Has either hearbeat bit changed? */
  782. if ((port0 ^ last_port0) & WD_HRTBT ||
  783. (port1 ^ last_port1) & WD_REVC_HRBT) {
  784. retval = 1;
  785. break;
  786. }
  787. }
  788. }
  789. release_region (base_addr, 4);
  790. return retval;
  791. }
  792. /*
  793. * These are the auto-probe addresses available.
  794. *
  795. * Revision A only uses ports 0x270 and 0x370. Revision C introduced 0x350.
  796. * Revision A has an address range of 2 addresses, while Revision C has 4.
  797. */
  798. static int pcwd_ioports[] = { 0x270, 0x350, 0x370, 0x000 };
  799. static int __init pcwd_init_module(void)
  800. {
  801. int i, found = 0;
  802. spin_lock_init(&pcwd_private.io_lock);
  803. for (i = 0; pcwd_ioports[i] != 0; i++) {
  804. if (pcwd_checkcard(pcwd_ioports[i])) {
  805. if (!(pcwatchdog_init(pcwd_ioports[i])))
  806. found++;
  807. }
  808. }
  809. if (!found) {
  810. printk (KERN_INFO PFX "No card detected, or port not available\n");
  811. return -ENODEV;
  812. }
  813. return 0;
  814. }
  815. static void __exit pcwd_cleanup_module(void)
  816. {
  817. if (pcwd_private.io_addr)
  818. pcwatchdog_exit();
  819. printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
  820. }
  821. module_init(pcwd_init_module);
  822. module_exit(pcwd_cleanup_module);
  823. MODULE_AUTHOR("Ken Hollis <kenji@bitgate.com>");
  824. MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
  825. MODULE_LICENSE("GPL");
  826. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  827. MODULE_ALIAS_MISCDEV(TEMP_MINOR);