pcwd.c 27 KB

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