pcwd.c 27 KB

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