pcwd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PC Watchdog Driver
  4. * by Ken Hollis ([email protected])
  5. *
  6. * Permission granted from Simon Machell ([email protected])
  7. * Written for the Linux Kernel, and GPLed by Ken Hollis
  8. *
  9. * 960107 Added request_region routines, modulized the whole thing.
  10. * 960108 Fixed end-of-file pointer (Thanks to Dan Hollis), added
  11. * WD_TIMEOUT define.
  12. * 960216 Added eof marker on the file, and changed verbose messages.
  13. * 960716 Made functional and cosmetic changes to the source for
  14. * inclusion in Linux 2.0.x kernels, thanks to Alan Cox.
  15. * 960717 Removed read/seek routines, replaced with ioctl. Also, added
  16. * check_region command due to Alan's suggestion.
  17. * 960821 Made changes to compile in newer 2.0.x kernels. Added
  18. * "cold reboot sense" entry.
  19. * 960825 Made a few changes to code, deleted some defines and made
  20. * typedefs to replace them. Made heartbeat reset only available
  21. * via ioctl, and removed the write routine.
  22. * 960828 Added new items for PC Watchdog Rev.C card.
  23. * 960829 Changed around all of the IOCTLs, added new features,
  24. * added watchdog disable/re-enable routines. Added firmware
  25. * version reporting. Added read routine for temperature.
  26. * Removed some extra defines, added an autodetect Revision
  27. * routine.
  28. * 961006 Revised some documentation, fixed some cosmetic bugs. Made
  29. * drivers to panic the system if it's overheating at bootup.
  30. * 961118 Changed some verbiage on some of the output, tidied up
  31. * code bits, and added compatibility to 2.1.x.
  32. * 970912 Enabled board on open and disable on close.
  33. * 971107 Took account of recent VFS changes (broke read).
  34. * 971210 Disable board on initialisation in case board already ticking.
  35. * 971222 Changed open/close for temperature handling
  36. * Michael Meskes <[email protected]>.
  37. * 980112 Used minor numbers from include/linux/miscdevice.h
  38. * 990403 Clear reset status after reading control status register in
  39. * pcwd_showprevstate(). [Marc Boucher <[email protected]>]
  40. * 990605 Made changes to code to support Firmware 1.22a, added
  41. * fairly useless proc entry.
  42. * 990610 removed said useless proc code for the merge <alan>
  43. * 000403 Removed last traces of proc code. <davej>
  44. * 011214 Added nowayout module option to override
  45. * CONFIG_WATCHDOG_NOWAYOUT <[email protected]>
  46. * Added timeout module option to override default
  47. */
  48. /*
  49. * A bells and whistles driver is available from http://www.pcwd.de/
  50. * More info available at http://www.berkprod.com/ or
  51. * http://www.pcwatchdog.com/
  52. */
  53. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  54. #include <linux/module.h> /* For module specific items */
  55. #include <linux/moduleparam.h> /* For new moduleparam's */
  56. #include <linux/types.h> /* For standard types (like size_t) */
  57. #include <linux/errno.h> /* For the -ENODEV/... values */
  58. #include <linux/kernel.h> /* For printk/panic/... */
  59. #include <linux/delay.h> /* For mdelay function */
  60. #include <linux/timer.h> /* For timer related operations */
  61. #include <linux/jiffies.h> /* For jiffies stuff */
  62. #include <linux/miscdevice.h> /* For struct miscdevice */
  63. #include <linux/watchdog.h> /* For the watchdog specific items */
  64. #include <linux/reboot.h> /* For kernel_power_off() */
  65. #include <linux/init.h> /* For __init/__exit/... */
  66. #include <linux/fs.h> /* For file operations */
  67. #include <linux/isa.h> /* For isa devices */
  68. #include <linux/ioport.h> /* For io-port access */
  69. #include <linux/spinlock.h> /* For spin_lock/spin_unlock/... */
  70. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  71. #include <linux/io.h> /* For inb/outb/... */
  72. /* Module and version information */
  73. #define WATCHDOG_VERSION "1.20"
  74. #define WATCHDOG_DATE "18 Feb 2007"
  75. #define WATCHDOG_DRIVER_NAME "ISA-PC Watchdog"
  76. #define WATCHDOG_NAME "pcwd"
  77. #define DRIVER_VERSION WATCHDOG_DRIVER_NAME " driver, v" WATCHDOG_VERSION "\n"
  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. "
  188. "(2 <= heartbeat <= 7200 or 0=delay-time from dip-switches, default="
  189. __MODULE_STRING(WATCHDOG_HEARTBEAT) ")");
  190. static bool nowayout = WATCHDOG_NOWAYOUT;
  191. module_param(nowayout, bool, 0);
  192. MODULE_PARM_DESC(nowayout,
  193. "Watchdog cannot be stopped once started (default="
  194. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  195. /*
  196. * Internal functions
  197. */
  198. static int send_isa_command(int cmd)
  199. {
  200. int i;
  201. int control_status;
  202. int port0, last_port0; /* Double read for stabilising */
  203. if (debug >= DEBUG)
  204. pr_debug("sending following data cmd=0x%02x\n", cmd);
  205. /* The WCMD bit must be 1 and the command is only 4 bits in size */
  206. control_status = (cmd & 0x0F) | WD_WCMD;
  207. outb_p(control_status, pcwd_private.io_addr + 2);
  208. udelay(ISA_COMMAND_TIMEOUT);
  209. port0 = inb_p(pcwd_private.io_addr);
  210. for (i = 0; i < 25; ++i) {
  211. last_port0 = port0;
  212. port0 = inb_p(pcwd_private.io_addr);
  213. if (port0 == last_port0)
  214. break; /* Data is stable */
  215. udelay(250);
  216. }
  217. if (debug >= DEBUG)
  218. pr_debug("received following data for cmd=0x%02x: port0=0x%02x last_port0=0x%02x\n",
  219. cmd, port0, last_port0);
  220. return port0;
  221. }
  222. static int set_command_mode(void)
  223. {
  224. int i, found = 0, count = 0;
  225. /* Set the card into command mode */
  226. spin_lock(&pcwd_private.io_lock);
  227. while ((!found) && (count < 3)) {
  228. i = send_isa_command(CMD_ISA_IDLE);
  229. if (i == 0x00)
  230. found = 1;
  231. else if (i == 0xF3) {
  232. /* Card does not like what we've done to it */
  233. outb_p(0x00, pcwd_private.io_addr + 2);
  234. udelay(1200); /* Spec says wait 1ms */
  235. outb_p(0x00, pcwd_private.io_addr + 2);
  236. udelay(ISA_COMMAND_TIMEOUT);
  237. }
  238. count++;
  239. }
  240. spin_unlock(&pcwd_private.io_lock);
  241. pcwd_private.command_mode = found;
  242. if (debug >= DEBUG)
  243. pr_debug("command_mode=%d\n", 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. pr_debug("command_mode=%d\n", pcwd_private.command_mode);
  256. }
  257. static inline void pcwd_check_temperature_support(void)
  258. {
  259. if (inb(pcwd_private.io_addr) != 0xF0)
  260. pcwd_private.supports_temp = 1;
  261. }
  262. static inline void pcwd_get_firmware(void)
  263. {
  264. int one, ten, hund, minor;
  265. strcpy(pcwd_private.fw_ver_str, "ERROR");
  266. if (set_command_mode()) {
  267. one = send_isa_command(CMD_ISA_VERSION_INTEGER);
  268. ten = send_isa_command(CMD_ISA_VERSION_TENTH);
  269. hund = send_isa_command(CMD_ISA_VERSION_HUNDRETH);
  270. minor = send_isa_command(CMD_ISA_VERSION_MINOR);
  271. sprintf(pcwd_private.fw_ver_str, "%c.%c%c%c",
  272. one, ten, hund, minor);
  273. }
  274. unset_command_mode();
  275. return;
  276. }
  277. static inline int pcwd_get_option_switches(void)
  278. {
  279. int option_switches = 0;
  280. if (set_command_mode()) {
  281. /* Get switch settings */
  282. option_switches = send_isa_command(CMD_ISA_SWITCH_SETTINGS);
  283. }
  284. unset_command_mode();
  285. return option_switches;
  286. }
  287. static void pcwd_show_card_info(void)
  288. {
  289. int option_switches;
  290. /* Get some extra info from the hardware (in command/debug/diag mode) */
  291. if (pcwd_private.revision == PCWD_REVISION_A)
  292. pr_info("ISA-PC Watchdog (REV.A) detected at port 0x%04x\n",
  293. pcwd_private.io_addr);
  294. else if (pcwd_private.revision == PCWD_REVISION_C) {
  295. pcwd_get_firmware();
  296. pr_info("ISA-PC Watchdog (REV.C) detected at port 0x%04x (Firmware version: %s)\n",
  297. pcwd_private.io_addr, pcwd_private.fw_ver_str);
  298. option_switches = pcwd_get_option_switches();
  299. pr_info("Option switches (0x%02x): Temperature Reset Enable=%s, Power On Delay=%s\n",
  300. option_switches,
  301. ((option_switches & 0x10) ? "ON" : "OFF"),
  302. ((option_switches & 0x08) ? "ON" : "OFF"));
  303. /* Reprogram internal heartbeat to 2 seconds */
  304. if (set_command_mode()) {
  305. send_isa_command(CMD_ISA_DELAY_TIME_2SECS);
  306. unset_command_mode();
  307. }
  308. }
  309. if (pcwd_private.supports_temp)
  310. pr_info("Temperature Option Detected\n");
  311. if (pcwd_private.boot_status & WDIOF_CARDRESET)
  312. pr_info("Previous reboot was caused by the card\n");
  313. if (pcwd_private.boot_status & WDIOF_OVERHEAT) {
  314. pr_emerg("Card senses a CPU Overheat. Panicking!\n");
  315. pr_emerg("CPU Overheat\n");
  316. }
  317. if (pcwd_private.boot_status == 0)
  318. pr_info("No previous trip detected - Cold boot or reset\n");
  319. }
  320. static void pcwd_timer_ping(struct timer_list *unused)
  321. {
  322. int wdrst_stat;
  323. /* If we got a heartbeat pulse within the WDT_INTERVAL
  324. * we agree to ping the WDT */
  325. if (time_before(jiffies, pcwd_private.next_heartbeat)) {
  326. /* Ping the watchdog */
  327. spin_lock(&pcwd_private.io_lock);
  328. if (pcwd_private.revision == PCWD_REVISION_A) {
  329. /* Rev A cards are reset by setting the
  330. WD_WDRST bit in register 1 */
  331. wdrst_stat = inb_p(pcwd_private.io_addr);
  332. wdrst_stat &= 0x0F;
  333. wdrst_stat |= WD_WDRST;
  334. outb_p(wdrst_stat, pcwd_private.io_addr + 1);
  335. } else {
  336. /* Re-trigger watchdog by writing to port 0 */
  337. outb_p(0x00, pcwd_private.io_addr);
  338. }
  339. /* Re-set the timer interval */
  340. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  341. spin_unlock(&pcwd_private.io_lock);
  342. } else {
  343. pr_warn("Heartbeat lost! Will not ping the watchdog\n");
  344. }
  345. }
  346. static int pcwd_start(void)
  347. {
  348. int stat_reg;
  349. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  350. /* Start the timer */
  351. mod_timer(&pcwd_private.timer, jiffies + WDT_INTERVAL);
  352. /* Enable the port */
  353. if (pcwd_private.revision == PCWD_REVISION_C) {
  354. spin_lock(&pcwd_private.io_lock);
  355. outb_p(0x00, pcwd_private.io_addr + 3);
  356. udelay(ISA_COMMAND_TIMEOUT);
  357. stat_reg = inb_p(pcwd_private.io_addr + 2);
  358. spin_unlock(&pcwd_private.io_lock);
  359. if (stat_reg & WD_WDIS) {
  360. pr_info("Could not start watchdog\n");
  361. return -EIO;
  362. }
  363. }
  364. if (debug >= VERBOSE)
  365. pr_debug("Watchdog started\n");
  366. return 0;
  367. }
  368. static int pcwd_stop(void)
  369. {
  370. int stat_reg;
  371. /* Stop the timer */
  372. del_timer(&pcwd_private.timer);
  373. /* Disable the board */
  374. if (pcwd_private.revision == PCWD_REVISION_C) {
  375. spin_lock(&pcwd_private.io_lock);
  376. outb_p(0xA5, pcwd_private.io_addr + 3);
  377. udelay(ISA_COMMAND_TIMEOUT);
  378. outb_p(0xA5, pcwd_private.io_addr + 3);
  379. udelay(ISA_COMMAND_TIMEOUT);
  380. stat_reg = inb_p(pcwd_private.io_addr + 2);
  381. spin_unlock(&pcwd_private.io_lock);
  382. if ((stat_reg & WD_WDIS) == 0) {
  383. pr_info("Could not stop watchdog\n");
  384. return -EIO;
  385. }
  386. }
  387. if (debug >= VERBOSE)
  388. pr_debug("Watchdog stopped\n");
  389. return 0;
  390. }
  391. static int pcwd_keepalive(void)
  392. {
  393. /* user land ping */
  394. pcwd_private.next_heartbeat = jiffies + (heartbeat * HZ);
  395. if (debug >= DEBUG)
  396. pr_debug("Watchdog keepalive signal send\n");
  397. return 0;
  398. }
  399. static int pcwd_set_heartbeat(int t)
  400. {
  401. if (t < 2 || t > 7200) /* arbitrary upper limit */
  402. return -EINVAL;
  403. heartbeat = t;
  404. if (debug >= VERBOSE)
  405. pr_debug("New heartbeat: %d\n", heartbeat);
  406. return 0;
  407. }
  408. static int pcwd_get_status(int *status)
  409. {
  410. int control_status;
  411. *status = 0;
  412. spin_lock(&pcwd_private.io_lock);
  413. if (pcwd_private.revision == PCWD_REVISION_A)
  414. /* Rev A cards return status information from
  415. * the base register, which is used for the
  416. * temperature in other cards. */
  417. control_status = inb(pcwd_private.io_addr);
  418. else {
  419. /* Rev C cards return card status in the base
  420. * address + 1 register. And use different bits
  421. * to indicate a card initiated reset, and an
  422. * over-temperature condition. And the reboot
  423. * status can be reset. */
  424. control_status = inb(pcwd_private.io_addr + 1);
  425. }
  426. spin_unlock(&pcwd_private.io_lock);
  427. if (pcwd_private.revision == PCWD_REVISION_A) {
  428. if (control_status & WD_WDRST)
  429. *status |= WDIOF_CARDRESET;
  430. if (control_status & WD_T110) {
  431. *status |= WDIOF_OVERHEAT;
  432. if (temp_panic) {
  433. pr_info("Temperature overheat trip!\n");
  434. kernel_power_off();
  435. }
  436. }
  437. } else {
  438. if (control_status & WD_REVC_WTRP)
  439. *status |= WDIOF_CARDRESET;
  440. if (control_status & WD_REVC_TTRP) {
  441. *status |= WDIOF_OVERHEAT;
  442. if (temp_panic) {
  443. pr_info("Temperature overheat trip!\n");
  444. kernel_power_off();
  445. }
  446. }
  447. }
  448. return 0;
  449. }
  450. static int pcwd_clear_status(void)
  451. {
  452. int control_status;
  453. if (pcwd_private.revision == PCWD_REVISION_C) {
  454. spin_lock(&pcwd_private.io_lock);
  455. if (debug >= VERBOSE)
  456. pr_info("clearing watchdog trip status\n");
  457. control_status = inb_p(pcwd_private.io_addr + 1);
  458. if (debug >= DEBUG) {
  459. pr_debug("status was: 0x%02x\n", control_status);
  460. pr_debug("sending: 0x%02x\n",
  461. (control_status & WD_REVC_R2DS));
  462. }
  463. /* clear reset status & Keep Relay 2 disable state as it is */
  464. outb_p((control_status & WD_REVC_R2DS),
  465. pcwd_private.io_addr + 1);
  466. spin_unlock(&pcwd_private.io_lock);
  467. }
  468. return 0;
  469. }
  470. static int pcwd_get_temperature(int *temperature)
  471. {
  472. /* check that port 0 gives temperature info and no command results */
  473. if (pcwd_private.command_mode)
  474. return -1;
  475. *temperature = 0;
  476. if (!pcwd_private.supports_temp)
  477. return -ENODEV;
  478. /*
  479. * Convert celsius to fahrenheit, since this was
  480. * the decided 'standard' for this return value.
  481. */
  482. spin_lock(&pcwd_private.io_lock);
  483. *temperature = ((inb(pcwd_private.io_addr)) * 9 / 5) + 32;
  484. spin_unlock(&pcwd_private.io_lock);
  485. if (debug >= DEBUG) {
  486. pr_debug("temperature is: %d F\n", *temperature);
  487. }
  488. return 0;
  489. }
  490. /*
  491. * /dev/watchdog handling
  492. */
  493. static long pcwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  494. {
  495. int rv;
  496. int status;
  497. int temperature;
  498. int new_heartbeat;
  499. int __user *argp = (int __user *)arg;
  500. static const struct watchdog_info ident = {
  501. .options = WDIOF_OVERHEAT |
  502. WDIOF_CARDRESET |
  503. WDIOF_KEEPALIVEPING |
  504. WDIOF_SETTIMEOUT |
  505. WDIOF_MAGICCLOSE,
  506. .firmware_version = 1,
  507. .identity = "PCWD",
  508. };
  509. switch (cmd) {
  510. case WDIOC_GETSUPPORT:
  511. if (copy_to_user(argp, &ident, sizeof(ident)))
  512. return -EFAULT;
  513. return 0;
  514. case WDIOC_GETSTATUS:
  515. pcwd_get_status(&status);
  516. return put_user(status, argp);
  517. case WDIOC_GETBOOTSTATUS:
  518. return put_user(pcwd_private.boot_status, argp);
  519. case WDIOC_GETTEMP:
  520. if (pcwd_get_temperature(&temperature))
  521. return -EFAULT;
  522. return put_user(temperature, argp);
  523. case WDIOC_SETOPTIONS:
  524. if (pcwd_private.revision == PCWD_REVISION_C) {
  525. if (get_user(rv, argp))
  526. return -EFAULT;
  527. if (rv & WDIOS_DISABLECARD) {
  528. status = pcwd_stop();
  529. if (status < 0)
  530. return status;
  531. }
  532. if (rv & WDIOS_ENABLECARD) {
  533. status = pcwd_start();
  534. if (status < 0)
  535. return status;
  536. }
  537. if (rv & WDIOS_TEMPPANIC)
  538. temp_panic = 1;
  539. }
  540. return -EINVAL;
  541. case WDIOC_KEEPALIVE:
  542. pcwd_keepalive();
  543. return 0;
  544. case WDIOC_SETTIMEOUT:
  545. if (get_user(new_heartbeat, argp))
  546. return -EFAULT;
  547. if (pcwd_set_heartbeat(new_heartbeat))
  548. return -EINVAL;
  549. pcwd_keepalive();
  550. fallthrough;
  551. case WDIOC_GETTIMEOUT:
  552. return put_user(heartbeat, argp);
  553. default:
  554. return -ENOTTY;
  555. }
  556. return 0;
  557. }
  558. static ssize_t pcwd_write(struct file *file, const char __user *buf, size_t len,
  559. loff_t *ppos)
  560. {
  561. if (len) {
  562. if (!nowayout) {
  563. size_t i;
  564. /* In case it was set long ago */
  565. expect_close = 0;
  566. for (i = 0; i != len; i++) {
  567. char c;
  568. if (get_user(c, buf + i))
  569. return -EFAULT;
  570. if (c == 'V')
  571. expect_close = 42;
  572. }
  573. }
  574. pcwd_keepalive();
  575. }
  576. return len;
  577. }
  578. static int pcwd_open(struct inode *inode, struct file *file)
  579. {
  580. if (test_and_set_bit(0, &open_allowed))
  581. return -EBUSY;
  582. if (nowayout)
  583. __module_get(THIS_MODULE);
  584. /* Activate */
  585. pcwd_start();
  586. pcwd_keepalive();
  587. return stream_open(inode, file);
  588. }
  589. static int pcwd_close(struct inode *inode, struct file *file)
  590. {
  591. if (expect_close == 42)
  592. pcwd_stop();
  593. else {
  594. pr_crit("Unexpected close, not stopping watchdog!\n");
  595. pcwd_keepalive();
  596. }
  597. expect_close = 0;
  598. clear_bit(0, &open_allowed);
  599. return 0;
  600. }
  601. /*
  602. * /dev/temperature handling
  603. */
  604. static ssize_t pcwd_temp_read(struct file *file, char __user *buf, size_t count,
  605. loff_t *ppos)
  606. {
  607. int temperature;
  608. if (pcwd_get_temperature(&temperature))
  609. return -EFAULT;
  610. if (copy_to_user(buf, &temperature, 1))
  611. return -EFAULT;
  612. return 1;
  613. }
  614. static int pcwd_temp_open(struct inode *inode, struct file *file)
  615. {
  616. if (!pcwd_private.supports_temp)
  617. return -ENODEV;
  618. return stream_open(inode, file);
  619. }
  620. static int pcwd_temp_close(struct inode *inode, struct file *file)
  621. {
  622. return 0;
  623. }
  624. /*
  625. * Kernel Interfaces
  626. */
  627. static const struct file_operations pcwd_fops = {
  628. .owner = THIS_MODULE,
  629. .llseek = no_llseek,
  630. .write = pcwd_write,
  631. .unlocked_ioctl = pcwd_ioctl,
  632. .compat_ioctl = compat_ptr_ioctl,
  633. .open = pcwd_open,
  634. .release = pcwd_close,
  635. };
  636. static struct miscdevice pcwd_miscdev = {
  637. .minor = WATCHDOG_MINOR,
  638. .name = "watchdog",
  639. .fops = &pcwd_fops,
  640. };
  641. static const struct file_operations pcwd_temp_fops = {
  642. .owner = THIS_MODULE,
  643. .llseek = no_llseek,
  644. .read = pcwd_temp_read,
  645. .open = pcwd_temp_open,
  646. .release = pcwd_temp_close,
  647. };
  648. static struct miscdevice temp_miscdev = {
  649. .minor = TEMP_MINOR,
  650. .name = "temperature",
  651. .fops = &pcwd_temp_fops,
  652. };
  653. /*
  654. * Init & exit routines
  655. */
  656. static inline int get_revision(void)
  657. {
  658. int r = PCWD_REVISION_C;
  659. spin_lock(&pcwd_private.io_lock);
  660. /* REV A cards use only 2 io ports; test
  661. * presumes a floating bus reads as 0xff. */
  662. if ((inb(pcwd_private.io_addr + 2) == 0xFF) ||
  663. (inb(pcwd_private.io_addr + 3) == 0xFF))
  664. r = PCWD_REVISION_A;
  665. spin_unlock(&pcwd_private.io_lock);
  666. return r;
  667. }
  668. /*
  669. * The ISA cards have a heartbeat bit in one of the registers, which
  670. * register is card dependent. The heartbeat bit is monitored, and if
  671. * found, is considered proof that a Berkshire card has been found.
  672. * The initial rate is once per second at board start up, then twice
  673. * per second for normal operation.
  674. */
  675. static int pcwd_isa_match(struct device *dev, unsigned int id)
  676. {
  677. int base_addr = pcwd_ioports[id];
  678. int port0, last_port0; /* Reg 0, in case it's REV A */
  679. int port1, last_port1; /* Register 1 for REV C cards */
  680. int i;
  681. int retval;
  682. if (debug >= DEBUG)
  683. pr_debug("pcwd_isa_match id=%d\n", id);
  684. if (!request_region(base_addr, 4, "PCWD")) {
  685. pr_info("Port 0x%04x unavailable\n", base_addr);
  686. return 0;
  687. }
  688. retval = 0;
  689. port0 = inb_p(base_addr); /* For REV A boards */
  690. port1 = inb_p(base_addr + 1); /* For REV C boards */
  691. if (port0 != 0xff || port1 != 0xff) {
  692. /* Not an 'ff' from a floating bus, so must be a card! */
  693. for (i = 0; i < 4; ++i) {
  694. msleep(500);
  695. last_port0 = port0;
  696. last_port1 = port1;
  697. port0 = inb_p(base_addr);
  698. port1 = inb_p(base_addr + 1);
  699. /* Has either hearbeat bit changed? */
  700. if ((port0 ^ last_port0) & WD_HRTBT ||
  701. (port1 ^ last_port1) & WD_REVC_HRBT) {
  702. retval = 1;
  703. break;
  704. }
  705. }
  706. }
  707. release_region(base_addr, 4);
  708. return retval;
  709. }
  710. static int pcwd_isa_probe(struct device *dev, unsigned int id)
  711. {
  712. int ret;
  713. if (debug >= DEBUG)
  714. pr_debug("pcwd_isa_probe id=%d\n", id);
  715. cards_found++;
  716. if (cards_found == 1)
  717. pr_info("v%s Ken Hollis ([email protected])\n",
  718. WATCHDOG_VERSION);
  719. if (cards_found > 1) {
  720. pr_err("This driver only supports 1 device\n");
  721. return -ENODEV;
  722. }
  723. if (pcwd_ioports[id] == 0x0000) {
  724. pr_err("No I/O-Address for card detected\n");
  725. return -ENODEV;
  726. }
  727. pcwd_private.io_addr = pcwd_ioports[id];
  728. spin_lock_init(&pcwd_private.io_lock);
  729. /* Check card's revision */
  730. pcwd_private.revision = get_revision();
  731. if (!request_region(pcwd_private.io_addr,
  732. (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4, "PCWD")) {
  733. pr_err("I/O address 0x%04x already in use\n",
  734. pcwd_private.io_addr);
  735. ret = -EIO;
  736. goto error_request_region;
  737. }
  738. /* Initial variables */
  739. pcwd_private.supports_temp = 0;
  740. temp_panic = 0;
  741. pcwd_private.boot_status = 0x0000;
  742. /* get the boot_status */
  743. pcwd_get_status(&pcwd_private.boot_status);
  744. /* clear the "card caused reboot" flag */
  745. pcwd_clear_status();
  746. timer_setup(&pcwd_private.timer, pcwd_timer_ping, 0);
  747. /* Disable the board */
  748. pcwd_stop();
  749. /* Check whether or not the card supports the temperature device */
  750. pcwd_check_temperature_support();
  751. /* Show info about the card itself */
  752. pcwd_show_card_info();
  753. /* If heartbeat = 0 then we use the heartbeat from the dip-switches */
  754. if (heartbeat == 0)
  755. heartbeat = heartbeat_tbl[(pcwd_get_option_switches() & 0x07)];
  756. /* Check that the heartbeat value is within it's range;
  757. if not reset to the default */
  758. if (pcwd_set_heartbeat(heartbeat)) {
  759. pcwd_set_heartbeat(WATCHDOG_HEARTBEAT);
  760. pr_info("heartbeat value must be 2 <= heartbeat <= 7200, using %d\n",
  761. WATCHDOG_HEARTBEAT);
  762. }
  763. if (pcwd_private.supports_temp) {
  764. ret = misc_register(&temp_miscdev);
  765. if (ret) {
  766. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  767. TEMP_MINOR, ret);
  768. goto error_misc_register_temp;
  769. }
  770. }
  771. ret = misc_register(&pcwd_miscdev);
  772. if (ret) {
  773. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  774. WATCHDOG_MINOR, ret);
  775. goto error_misc_register_watchdog;
  776. }
  777. pr_info("initialized. heartbeat=%d sec (nowayout=%d)\n",
  778. heartbeat, nowayout);
  779. return 0;
  780. error_misc_register_watchdog:
  781. if (pcwd_private.supports_temp)
  782. misc_deregister(&temp_miscdev);
  783. error_misc_register_temp:
  784. release_region(pcwd_private.io_addr,
  785. (pcwd_private.revision == PCWD_REVISION_A) ? 2 : 4);
  786. error_request_region:
  787. pcwd_private.io_addr = 0x0000;
  788. cards_found--;
  789. return ret;
  790. }
  791. static void pcwd_isa_remove(struct device *dev, unsigned int id)
  792. {
  793. if (debug >= DEBUG)
  794. pr_debug("pcwd_isa_remove id=%d\n", id);
  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. }
  807. static void pcwd_isa_shutdown(struct device *dev, unsigned int id)
  808. {
  809. if (debug >= DEBUG)
  810. pr_debug("pcwd_isa_shutdown id=%d\n", id);
  811. pcwd_stop();
  812. }
  813. static struct isa_driver pcwd_isa_driver = {
  814. .match = pcwd_isa_match,
  815. .probe = pcwd_isa_probe,
  816. .remove = pcwd_isa_remove,
  817. .shutdown = pcwd_isa_shutdown,
  818. .driver = {
  819. .owner = THIS_MODULE,
  820. .name = WATCHDOG_NAME,
  821. },
  822. };
  823. module_isa_driver(pcwd_isa_driver, PCWD_ISA_NR_CARDS);
  824. MODULE_AUTHOR("Ken Hollis <[email protected]>, "
  825. "Wim Van Sebroeck <[email protected]>");
  826. MODULE_DESCRIPTION("Berkshire ISA-PC Watchdog driver");
  827. MODULE_VERSION(WATCHDOG_VERSION);
  828. MODULE_LICENSE("GPL");