pcwd.c 26 KB

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