acquirewdt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Acquire Single Board Computer Watchdog Timer driver
  3. *
  4. * Based on wdt.c. Original copyright messages:
  5. *
  6. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  7. * http://www.redhat.com
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  15. * warranty for any of this software. This material is provided
  16. * "AS-IS" and at no charge.
  17. *
  18. * (c) Copyright 1995 Alan Cox <alan@redhat.com>
  19. *
  20. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  21. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  22. * Can't add timeout - driver doesn't allow changing value
  23. */
  24. /*
  25. * Theory of Operation:
  26. * The Watch-Dog Timer is provided to ensure that standalone
  27. * Systems can always recover from catastrophic conditions that
  28. * caused the CPU to crash. This condition may have occured by
  29. * external EMI or a software bug. When the CPU stops working
  30. * correctly, hardware on the board will either perform a hardware
  31. * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
  32. * system back to a known state.
  33. *
  34. * The Watch-Dog Timer is controlled by two I/O Ports.
  35. * 443 hex - Read - Enable or refresh the Watch-Dog Timer
  36. * 043 hex - Read - Disable the Watch-Dog Timer
  37. *
  38. * To enable the Watch-Dog Timer, a read from I/O port 443h must
  39. * be performed. This will enable and activate the countdown timer
  40. * which will eventually time out and either reset the CPU or cause
  41. * an NMI depending on the setting of a jumper. To ensure that this
  42. * reset condition does not occur, the Watch-Dog Timer must be
  43. * periodically refreshed by reading the same I/O port 443h.
  44. * The Watch-Dog Timer is disabled by reading I/O port 043h.
  45. *
  46. * The Watch-Dog Timer Time-Out Period is set via jumpers.
  47. * It can be 1, 2, 10, 20, 110 or 220 seconds.
  48. */
  49. #include <linux/module.h>
  50. #include <linux/moduleparam.h>
  51. #include <linux/types.h>
  52. #include <linux/miscdevice.h>
  53. #include <linux/watchdog.h>
  54. #include <linux/fs.h>
  55. #include <linux/ioport.h>
  56. #include <linux/notifier.h>
  57. #include <linux/reboot.h>
  58. #include <linux/init.h>
  59. #include <asm/io.h>
  60. #include <asm/uaccess.h>
  61. #include <asm/system.h>
  62. #define WATCHDOG_NAME "Acquire WDT"
  63. #define PFX WATCHDOG_NAME ": "
  64. #define WATCHDOG_HEARTBEAT 0 /* There is no way to see what the correct time-out period is */
  65. static unsigned long acq_is_open;
  66. static char expect_close;
  67. /*
  68. * You must set these - there is no sane way to probe for this board.
  69. */
  70. static int wdt_stop = 0x43;
  71. module_param(wdt_stop, int, 0);
  72. MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
  73. static int wdt_start = 0x443;
  74. module_param(wdt_start, int, 0);
  75. MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
  76. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  77. static int nowayout = 1;
  78. #else
  79. static int nowayout = 0;
  80. #endif
  81. module_param(nowayout, int, 0);
  82. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  83. /*
  84. * Kernel methods.
  85. */
  86. static void acq_keepalive(void)
  87. {
  88. /* Write a watchdog value */
  89. inb_p(wdt_start);
  90. }
  91. static void acq_stop(void)
  92. {
  93. /* Turn the card off */
  94. inb_p(wdt_stop);
  95. }
  96. /*
  97. * /dev/watchdog handling.
  98. */
  99. static ssize_t acq_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  100. {
  101. /* See if we got the magic character 'V' and reload the timer */
  102. if(count) {
  103. if (!nowayout) {
  104. size_t i;
  105. /* note: just in case someone wrote the magic character
  106. * five months ago... */
  107. expect_close = 0;
  108. /* scan to see whether or not we got the magic character */
  109. for (i = 0; i != count; i++) {
  110. char c;
  111. if (get_user(c, buf + i))
  112. return -EFAULT;
  113. if (c == 'V')
  114. expect_close = 42;
  115. }
  116. }
  117. /* Well, anyhow someone wrote to us, we should return that favour */
  118. acq_keepalive();
  119. }
  120. return count;
  121. }
  122. static int acq_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  123. unsigned long arg)
  124. {
  125. int options, retval = -EINVAL;
  126. void __user *argp = (void __user *)arg;
  127. int __user *p = argp;
  128. static struct watchdog_info ident =
  129. {
  130. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  131. .firmware_version = 1,
  132. .identity = "Acquire WDT",
  133. };
  134. switch(cmd)
  135. {
  136. case WDIOC_GETSUPPORT:
  137. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  138. case WDIOC_GETSTATUS:
  139. case WDIOC_GETBOOTSTATUS:
  140. return put_user(0, p);
  141. case WDIOC_KEEPALIVE:
  142. acq_keepalive();
  143. return 0;
  144. case WDIOC_GETTIMEOUT:
  145. return put_user(WATCHDOG_HEARTBEAT, p);
  146. case WDIOC_SETOPTIONS:
  147. {
  148. if (get_user(options, p))
  149. return -EFAULT;
  150. if (options & WDIOS_DISABLECARD)
  151. {
  152. acq_stop();
  153. retval = 0;
  154. }
  155. if (options & WDIOS_ENABLECARD)
  156. {
  157. acq_keepalive();
  158. retval = 0;
  159. }
  160. return retval;
  161. }
  162. default:
  163. return -ENOIOCTLCMD;
  164. }
  165. }
  166. static int acq_open(struct inode *inode, struct file *file)
  167. {
  168. if (test_and_set_bit(0, &acq_is_open))
  169. return -EBUSY;
  170. if (nowayout)
  171. __module_get(THIS_MODULE);
  172. /* Activate */
  173. acq_keepalive();
  174. return nonseekable_open(inode, file);
  175. }
  176. static int acq_close(struct inode *inode, struct file *file)
  177. {
  178. if (expect_close == 42) {
  179. acq_stop();
  180. } else {
  181. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  182. acq_keepalive();
  183. }
  184. clear_bit(0, &acq_is_open);
  185. expect_close = 0;
  186. return 0;
  187. }
  188. /*
  189. * Notifier for system down
  190. */
  191. static int acq_notify_sys(struct notifier_block *this, unsigned long code,
  192. void *unused)
  193. {
  194. if(code==SYS_DOWN || code==SYS_HALT) {
  195. /* Turn the WDT off */
  196. acq_stop();
  197. }
  198. return NOTIFY_DONE;
  199. }
  200. /*
  201. * Kernel Interfaces
  202. */
  203. static struct file_operations acq_fops = {
  204. .owner = THIS_MODULE,
  205. .llseek = no_llseek,
  206. .write = acq_write,
  207. .ioctl = acq_ioctl,
  208. .open = acq_open,
  209. .release = acq_close,
  210. };
  211. static struct miscdevice acq_miscdev=
  212. {
  213. .minor = WATCHDOG_MINOR,
  214. .name = "watchdog",
  215. .fops = &acq_fops,
  216. };
  217. /*
  218. * The WDT card needs to learn about soft shutdowns in order to
  219. * turn the timebomb registers off.
  220. */
  221. static struct notifier_block acq_notifier =
  222. {
  223. .notifier_call = acq_notify_sys,
  224. };
  225. static int __init acq_init(void)
  226. {
  227. int ret;
  228. printk(KERN_INFO "WDT driver for Acquire single board computer initialising.\n");
  229. if (wdt_stop != wdt_start) {
  230. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  231. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  232. wdt_stop);
  233. ret = -EIO;
  234. goto out;
  235. }
  236. }
  237. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  238. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  239. wdt_start);
  240. ret = -EIO;
  241. goto unreg_stop;
  242. }
  243. ret = register_reboot_notifier(&acq_notifier);
  244. if (ret != 0) {
  245. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  246. ret);
  247. goto unreg_regions;
  248. }
  249. ret = misc_register(&acq_miscdev);
  250. if (ret != 0) {
  251. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  252. WATCHDOG_MINOR, ret);
  253. goto unreg_reboot;
  254. }
  255. printk (KERN_INFO PFX "initialized. (nowayout=%d)\n",
  256. nowayout);
  257. return 0;
  258. unreg_reboot:
  259. unregister_reboot_notifier(&acq_notifier);
  260. unreg_regions:
  261. release_region(wdt_start, 1);
  262. unreg_stop:
  263. if (wdt_stop != wdt_start)
  264. release_region(wdt_stop, 1);
  265. out:
  266. return ret;
  267. }
  268. static void __exit acq_exit(void)
  269. {
  270. misc_deregister(&acq_miscdev);
  271. unregister_reboot_notifier(&acq_notifier);
  272. if(wdt_stop != wdt_start)
  273. release_region(wdt_stop,1);
  274. release_region(wdt_start,1);
  275. }
  276. module_init(acq_init);
  277. module_exit(acq_exit);
  278. MODULE_AUTHOR("David Woodhouse");
  279. MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
  280. MODULE_LICENSE("GPL");
  281. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);