pcwd.c 27 KB

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