pcwd.c 27 KB

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