pcwd.c 27 KB

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