pcwd.c 26 KB

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