advantechwdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Advantech Single Board Computer WDT driver
  3. *
  4. * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl>
  5. *
  6. * Based on acquirewdt.c which is based on wdt.c.
  7. * Original copyright messages:
  8. *
  9. * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
  10. * http://www.redhat.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  18. * warranty for any of this software. This material is provided
  19. * "AS-IS" and at no charge.
  20. *
  21. * (c) Copyright 1995 Alan Cox <alan@redhat.com>
  22. *
  23. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  24. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  25. *
  26. * 16-Oct-2002 Rob Radez <rob@osinvestor.com>
  27. * Clean up ioctls, clean up init + exit, add expect close support,
  28. * add wdt_start and wdt_stop as parameters.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/types.h>
  33. #include <linux/miscdevice.h>
  34. #include <linux/watchdog.h>
  35. #include <linux/fs.h>
  36. #include <linux/ioport.h>
  37. #include <linux/notifier.h>
  38. #include <linux/reboot.h>
  39. #include <linux/init.h>
  40. #include <asm/io.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/system.h>
  43. #define WATCHDOG_NAME "Advantech WDT"
  44. #define PFX WATCHDOG_NAME ": "
  45. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  46. static unsigned long advwdt_is_open;
  47. static char adv_expect_close;
  48. /*
  49. * You must set these - there is no sane way to probe for this board.
  50. *
  51. * To enable or restart, write the timeout value in seconds (1 to 63)
  52. * to I/O port wdt_start. To disable, read I/O port wdt_stop.
  53. * Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
  54. * check your manual (at least the PCA-6159 seems to be different -
  55. * the manual says wdt_stop is 0x43, not 0x443).
  56. * (0x43 is also a write-only control register for the 8254 timer!)
  57. */
  58. static int wdt_stop = 0x443;
  59. module_param(wdt_stop, int, 0);
  60. MODULE_PARM_DESC(wdt_stop, "Advantech WDT 'stop' io port (default 0x443)");
  61. static int wdt_start = 0x443;
  62. module_param(wdt_start, int, 0);
  63. MODULE_PARM_DESC(wdt_start, "Advantech WDT 'start' io port (default 0x443)");
  64. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  65. module_param(timeout, int, 0);
  66. MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=63, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  67. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  68. static int nowayout = 1;
  69. #else
  70. static int nowayout = 0;
  71. #endif
  72. module_param(nowayout, int, 0);
  73. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  74. /*
  75. * Kernel methods.
  76. */
  77. static void
  78. advwdt_ping(void)
  79. {
  80. /* Write a watchdog value */
  81. outb_p(timeout, wdt_start);
  82. }
  83. static void
  84. advwdt_disable(void)
  85. {
  86. inb_p(wdt_stop);
  87. }
  88. static ssize_t
  89. advwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  90. {
  91. if (count) {
  92. if (!nowayout) {
  93. size_t i;
  94. adv_expect_close = 0;
  95. for (i = 0; i != count; i++) {
  96. char c;
  97. if (get_user(c, buf+i))
  98. return -EFAULT;
  99. if (c == 'V')
  100. adv_expect_close = 42;
  101. }
  102. }
  103. advwdt_ping();
  104. }
  105. return count;
  106. }
  107. static int
  108. advwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  109. unsigned long arg)
  110. {
  111. int new_timeout;
  112. void __user *argp = (void __user *)arg;
  113. int __user *p = argp;
  114. static struct watchdog_info ident = {
  115. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  116. .firmware_version = 1,
  117. .identity = "Advantech WDT",
  118. };
  119. switch (cmd) {
  120. case WDIOC_GETSUPPORT:
  121. if (copy_to_user(argp, &ident, sizeof(ident)))
  122. return -EFAULT;
  123. break;
  124. case WDIOC_GETSTATUS:
  125. case WDIOC_GETBOOTSTATUS:
  126. return put_user(0, p);
  127. case WDIOC_KEEPALIVE:
  128. advwdt_ping();
  129. break;
  130. case WDIOC_SETTIMEOUT:
  131. if (get_user(new_timeout, p))
  132. return -EFAULT;
  133. if ((new_timeout < 1) || (new_timeout > 63))
  134. return -EINVAL;
  135. timeout = new_timeout;
  136. advwdt_ping();
  137. /* Fall */
  138. case WDIOC_GETTIMEOUT:
  139. return put_user(timeout, p);
  140. case WDIOC_SETOPTIONS:
  141. {
  142. int options, retval = -EINVAL;
  143. if (get_user(options, p))
  144. return -EFAULT;
  145. if (options & WDIOS_DISABLECARD) {
  146. advwdt_disable();
  147. retval = 0;
  148. }
  149. if (options & WDIOS_ENABLECARD) {
  150. advwdt_ping();
  151. retval = 0;
  152. }
  153. return retval;
  154. }
  155. default:
  156. return -ENOIOCTLCMD;
  157. }
  158. return 0;
  159. }
  160. static int
  161. advwdt_open(struct inode *inode, struct file *file)
  162. {
  163. if (test_and_set_bit(0, &advwdt_is_open))
  164. return -EBUSY;
  165. /*
  166. * Activate
  167. */
  168. advwdt_ping();
  169. return nonseekable_open(inode, file);
  170. }
  171. static int
  172. advwdt_close(struct inode *inode, struct file *file)
  173. {
  174. if (adv_expect_close == 42) {
  175. advwdt_disable();
  176. } else {
  177. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  178. advwdt_ping();
  179. }
  180. clear_bit(0, &advwdt_is_open);
  181. adv_expect_close = 0;
  182. return 0;
  183. }
  184. /*
  185. * Notifier for system down
  186. */
  187. static int
  188. advwdt_notify_sys(struct notifier_block *this, unsigned long code,
  189. void *unused)
  190. {
  191. if (code == SYS_DOWN || code == SYS_HALT) {
  192. /* Turn the WDT off */
  193. advwdt_disable();
  194. }
  195. return NOTIFY_DONE;
  196. }
  197. /*
  198. * Kernel Interfaces
  199. */
  200. static struct file_operations advwdt_fops = {
  201. .owner = THIS_MODULE,
  202. .llseek = no_llseek,
  203. .write = advwdt_write,
  204. .ioctl = advwdt_ioctl,
  205. .open = advwdt_open,
  206. .release = advwdt_close,
  207. };
  208. static struct miscdevice advwdt_miscdev = {
  209. .minor = WATCHDOG_MINOR,
  210. .name = "watchdog",
  211. .fops = &advwdt_fops,
  212. };
  213. /*
  214. * The WDT needs to learn about soft shutdowns in order to
  215. * turn the timebomb registers off.
  216. */
  217. static struct notifier_block advwdt_notifier = {
  218. .notifier_call = advwdt_notify_sys,
  219. };
  220. static int __init
  221. advwdt_init(void)
  222. {
  223. int ret;
  224. printk(KERN_INFO "WDT driver for Advantech single board computer initialising.\n");
  225. if (timeout < 1 || timeout > 63) {
  226. timeout = WATCHDOG_TIMEOUT;
  227. printk (KERN_INFO PFX "timeout value must be 1<=x<=63, using %d\n",
  228. timeout);
  229. }
  230. if (wdt_stop != wdt_start) {
  231. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  232. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  233. wdt_stop);
  234. ret = -EIO;
  235. goto out;
  236. }
  237. }
  238. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  239. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  240. wdt_start);
  241. ret = -EIO;
  242. goto unreg_stop;
  243. }
  244. ret = register_reboot_notifier(&advwdt_notifier);
  245. if (ret != 0) {
  246. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  247. ret);
  248. goto unreg_regions;
  249. }
  250. ret = misc_register(&advwdt_miscdev);
  251. if (ret != 0) {
  252. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  253. WATCHDOG_MINOR, ret);
  254. goto unreg_reboot;
  255. }
  256. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  257. timeout, nowayout);
  258. out:
  259. return ret;
  260. unreg_reboot:
  261. unregister_reboot_notifier(&advwdt_notifier);
  262. unreg_regions:
  263. release_region(wdt_start, 1);
  264. unreg_stop:
  265. if (wdt_stop != wdt_start)
  266. release_region(wdt_stop, 1);
  267. goto out;
  268. }
  269. static void __exit
  270. advwdt_exit(void)
  271. {
  272. misc_deregister(&advwdt_miscdev);
  273. unregister_reboot_notifier(&advwdt_notifier);
  274. if(wdt_stop != wdt_start)
  275. release_region(wdt_stop,1);
  276. release_region(wdt_start,1);
  277. }
  278. module_init(advwdt_init);
  279. module_exit(advwdt_exit);
  280. MODULE_LICENSE("GPL");
  281. MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
  282. MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");
  283. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);