acquirewdt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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@lxorguk.ukuu.org.uk>,
  7. * All Rights Reserved.
  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@lxorguk.ukuu.org.uk>
  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 occurred 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. /*
  50. * Includes, defines, variables, module parameters, ...
  51. */
  52. /* Includes */
  53. #include <linux/module.h> /* For module specific items */
  54. #include <linux/moduleparam.h> /* For new moduleparam's */
  55. #include <linux/types.h> /* For standard types (like size_t) */
  56. #include <linux/errno.h> /* For the -ENODEV/... values */
  57. #include <linux/kernel.h> /* For printk/panic/... */
  58. #include <linux/miscdevice.h> /* For MODULE_ALIAS_MISCDEV
  59. (WATCHDOG_MINOR) */
  60. #include <linux/watchdog.h> /* For the watchdog specific items */
  61. #include <linux/fs.h> /* For file operations */
  62. #include <linux/ioport.h> /* For io-port access */
  63. #include <linux/platform_device.h> /* For platform_driver framework */
  64. #include <linux/init.h> /* For __init/__exit/... */
  65. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  66. #include <linux/io.h> /* For inb/outb/... */
  67. /* Module information */
  68. #define DRV_NAME "acquirewdt"
  69. #define PFX DRV_NAME ": "
  70. #define WATCHDOG_NAME "Acquire WDT"
  71. /* There is no way to see what the correct time-out period is */
  72. #define WATCHDOG_HEARTBEAT 0
  73. /* internal variables */
  74. /* the watchdog platform device */
  75. static struct platform_device *acq_platform_device;
  76. static unsigned long acq_is_open;
  77. static char expect_close;
  78. /* module parameters */
  79. /* You must set this - there is no sane way to probe for this board. */
  80. static int wdt_stop = 0x43;
  81. module_param(wdt_stop, int, 0);
  82. MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
  83. /* You must set this - there is no sane way to probe for this board. */
  84. static int wdt_start = 0x443;
  85. module_param(wdt_start, int, 0);
  86. MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
  87. static int nowayout = WATCHDOG_NOWAYOUT;
  88. module_param(nowayout, int, 0);
  89. MODULE_PARM_DESC(nowayout,
  90. "Watchdog cannot be stopped once started (default="
  91. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  92. /*
  93. * Watchdog Operations
  94. */
  95. static void acq_keepalive(void)
  96. {
  97. /* Write a watchdog value */
  98. inb_p(wdt_start);
  99. }
  100. static void acq_stop(void)
  101. {
  102. /* Turn the card off */
  103. inb_p(wdt_stop);
  104. }
  105. /*
  106. * /dev/watchdog handling
  107. */
  108. static ssize_t acq_write(struct file *file, const char __user *buf,
  109. size_t count, loff_t *ppos)
  110. {
  111. /* See if we got the magic character 'V' and reload the timer */
  112. if (count) {
  113. if (!nowayout) {
  114. size_t i;
  115. /* note: just in case someone wrote the magic character
  116. five months ago... */
  117. expect_close = 0;
  118. /* scan to see whether or not we got the
  119. magic character */
  120. for (i = 0; i != count; i++) {
  121. char c;
  122. if (get_user(c, buf + i))
  123. return -EFAULT;
  124. if (c == 'V')
  125. expect_close = 42;
  126. }
  127. }
  128. /* Well, anyhow someone wrote to us, we should
  129. return that favour */
  130. acq_keepalive();
  131. }
  132. return count;
  133. }
  134. static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  135. {
  136. int options, retval = -EINVAL;
  137. void __user *argp = (void __user *)arg;
  138. int __user *p = argp;
  139. static const struct watchdog_info ident = {
  140. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  141. .firmware_version = 1,
  142. .identity = WATCHDOG_NAME,
  143. };
  144. switch (cmd) {
  145. case WDIOC_GETSUPPORT:
  146. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  147. case WDIOC_GETSTATUS:
  148. case WDIOC_GETBOOTSTATUS:
  149. return put_user(0, p);
  150. case WDIOC_SETOPTIONS:
  151. {
  152. if (get_user(options, p))
  153. return -EFAULT;
  154. if (options & WDIOS_DISABLECARD) {
  155. acq_stop();
  156. retval = 0;
  157. }
  158. if (options & WDIOS_ENABLECARD) {
  159. acq_keepalive();
  160. retval = 0;
  161. }
  162. return retval;
  163. }
  164. case WDIOC_KEEPALIVE:
  165. acq_keepalive();
  166. return 0;
  167. case WDIOC_GETTIMEOUT:
  168. return put_user(WATCHDOG_HEARTBEAT, p);
  169. default:
  170. return -ENOTTY;
  171. }
  172. }
  173. static int acq_open(struct inode *inode, struct file *file)
  174. {
  175. if (test_and_set_bit(0, &acq_is_open))
  176. return -EBUSY;
  177. if (nowayout)
  178. __module_get(THIS_MODULE);
  179. /* Activate */
  180. acq_keepalive();
  181. return nonseekable_open(inode, file);
  182. }
  183. static int acq_close(struct inode *inode, struct file *file)
  184. {
  185. if (expect_close == 42) {
  186. acq_stop();
  187. } else {
  188. printk(KERN_CRIT PFX
  189. "Unexpected close, not stopping watchdog!\n");
  190. acq_keepalive();
  191. }
  192. clear_bit(0, &acq_is_open);
  193. expect_close = 0;
  194. return 0;
  195. }
  196. /*
  197. * Kernel Interfaces
  198. */
  199. static const struct file_operations acq_fops = {
  200. .owner = THIS_MODULE,
  201. .llseek = no_llseek,
  202. .write = acq_write,
  203. .unlocked_ioctl = acq_ioctl,
  204. .open = acq_open,
  205. .release = acq_close,
  206. };
  207. static struct miscdevice acq_miscdev = {
  208. .minor = WATCHDOG_MINOR,
  209. .name = "watchdog",
  210. .fops = &acq_fops,
  211. };
  212. /*
  213. * Init & exit routines
  214. */
  215. static int __devinit acq_probe(struct platform_device *dev)
  216. {
  217. int ret;
  218. if (wdt_stop != wdt_start) {
  219. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  220. printk(KERN_ERR PFX
  221. "I/O address 0x%04x already in use\n", wdt_stop);
  222. ret = -EIO;
  223. goto out;
  224. }
  225. }
  226. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  227. printk(KERN_ERR PFX "I/O address 0x%04x already in use\n",
  228. wdt_start);
  229. ret = -EIO;
  230. goto unreg_stop;
  231. }
  232. ret = misc_register(&acq_miscdev);
  233. if (ret != 0) {
  234. printk(KERN_ERR PFX
  235. "cannot register miscdev on minor=%d (err=%d)\n",
  236. WATCHDOG_MINOR, ret);
  237. goto unreg_regions;
  238. }
  239. printk(KERN_INFO PFX "initialized. (nowayout=%d)\n", nowayout);
  240. return 0;
  241. unreg_regions:
  242. release_region(wdt_start, 1);
  243. unreg_stop:
  244. if (wdt_stop != wdt_start)
  245. release_region(wdt_stop, 1);
  246. out:
  247. return ret;
  248. }
  249. static int __devexit acq_remove(struct platform_device *dev)
  250. {
  251. misc_deregister(&acq_miscdev);
  252. release_region(wdt_start, 1);
  253. if (wdt_stop != wdt_start)
  254. release_region(wdt_stop, 1);
  255. return 0;
  256. }
  257. static void acq_shutdown(struct platform_device *dev)
  258. {
  259. /* Turn the WDT off if we have a soft shutdown */
  260. acq_stop();
  261. }
  262. static struct platform_driver acquirewdt_driver = {
  263. .probe = acq_probe,
  264. .remove = __devexit_p(acq_remove),
  265. .shutdown = acq_shutdown,
  266. .driver = {
  267. .owner = THIS_MODULE,
  268. .name = DRV_NAME,
  269. },
  270. };
  271. static int __init acq_init(void)
  272. {
  273. int err;
  274. printk(KERN_INFO
  275. "WDT driver for Acquire single board computer initialising.\n");
  276. err = platform_driver_register(&acquirewdt_driver);
  277. if (err)
  278. return err;
  279. acq_platform_device = platform_device_register_simple(DRV_NAME,
  280. -1, NULL, 0);
  281. if (IS_ERR(acq_platform_device)) {
  282. err = PTR_ERR(acq_platform_device);
  283. goto unreg_platform_driver;
  284. }
  285. return 0;
  286. unreg_platform_driver:
  287. platform_driver_unregister(&acquirewdt_driver);
  288. return err;
  289. }
  290. static void __exit acq_exit(void)
  291. {
  292. platform_device_unregister(acq_platform_device);
  293. platform_driver_unregister(&acquirewdt_driver);
  294. printk(KERN_INFO PFX "Watchdog Module Unloaded.\n");
  295. }
  296. module_init(acq_init);
  297. module_exit(acq_exit);
  298. MODULE_AUTHOR("David Woodhouse");
  299. MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
  300. MODULE_LICENSE("GPL");
  301. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);