pcwd.c 25 KB

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