advantechwdt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. static int nowayout = WATCHDOG_NOWAYOUT;
  68. module_param(nowayout, int, 0);
  69. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  70. /*
  71. * Kernel methods.
  72. */
  73. static void
  74. advwdt_ping(void)
  75. {
  76. /* Write a watchdog value */
  77. outb_p(timeout, wdt_start);
  78. }
  79. static void
  80. advwdt_disable(void)
  81. {
  82. inb_p(wdt_stop);
  83. }
  84. static ssize_t
  85. advwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
  86. {
  87. if (count) {
  88. if (!nowayout) {
  89. size_t i;
  90. adv_expect_close = 0;
  91. for (i = 0; i != count; i++) {
  92. char c;
  93. if (get_user(c, buf+i))
  94. return -EFAULT;
  95. if (c == 'V')
  96. adv_expect_close = 42;
  97. }
  98. }
  99. advwdt_ping();
  100. }
  101. return count;
  102. }
  103. static int
  104. advwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
  105. unsigned long arg)
  106. {
  107. int new_timeout;
  108. void __user *argp = (void __user *)arg;
  109. int __user *p = argp;
  110. static struct watchdog_info ident = {
  111. .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
  112. .firmware_version = 1,
  113. .identity = "Advantech WDT",
  114. };
  115. switch (cmd) {
  116. case WDIOC_GETSUPPORT:
  117. if (copy_to_user(argp, &ident, sizeof(ident)))
  118. return -EFAULT;
  119. break;
  120. case WDIOC_GETSTATUS:
  121. case WDIOC_GETBOOTSTATUS:
  122. return put_user(0, p);
  123. case WDIOC_KEEPALIVE:
  124. advwdt_ping();
  125. break;
  126. case WDIOC_SETTIMEOUT:
  127. if (get_user(new_timeout, p))
  128. return -EFAULT;
  129. if ((new_timeout < 1) || (new_timeout > 63))
  130. return -EINVAL;
  131. timeout = new_timeout;
  132. advwdt_ping();
  133. /* Fall */
  134. case WDIOC_GETTIMEOUT:
  135. return put_user(timeout, p);
  136. case WDIOC_SETOPTIONS:
  137. {
  138. int options, retval = -EINVAL;
  139. if (get_user(options, p))
  140. return -EFAULT;
  141. if (options & WDIOS_DISABLECARD) {
  142. advwdt_disable();
  143. retval = 0;
  144. }
  145. if (options & WDIOS_ENABLECARD) {
  146. advwdt_ping();
  147. retval = 0;
  148. }
  149. return retval;
  150. }
  151. default:
  152. return -ENOTTY;
  153. }
  154. return 0;
  155. }
  156. static int
  157. advwdt_open(struct inode *inode, struct file *file)
  158. {
  159. if (test_and_set_bit(0, &advwdt_is_open))
  160. return -EBUSY;
  161. /*
  162. * Activate
  163. */
  164. advwdt_ping();
  165. return nonseekable_open(inode, file);
  166. }
  167. static int
  168. advwdt_close(struct inode *inode, struct file *file)
  169. {
  170. if (adv_expect_close == 42) {
  171. advwdt_disable();
  172. } else {
  173. printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
  174. advwdt_ping();
  175. }
  176. clear_bit(0, &advwdt_is_open);
  177. adv_expect_close = 0;
  178. return 0;
  179. }
  180. /*
  181. * Notifier for system down
  182. */
  183. static int
  184. advwdt_notify_sys(struct notifier_block *this, unsigned long code,
  185. void *unused)
  186. {
  187. if (code == SYS_DOWN || code == SYS_HALT) {
  188. /* Turn the WDT off */
  189. advwdt_disable();
  190. }
  191. return NOTIFY_DONE;
  192. }
  193. /*
  194. * Kernel Interfaces
  195. */
  196. static const struct file_operations advwdt_fops = {
  197. .owner = THIS_MODULE,
  198. .llseek = no_llseek,
  199. .write = advwdt_write,
  200. .ioctl = advwdt_ioctl,
  201. .open = advwdt_open,
  202. .release = advwdt_close,
  203. };
  204. static struct miscdevice advwdt_miscdev = {
  205. .minor = WATCHDOG_MINOR,
  206. .name = "watchdog",
  207. .fops = &advwdt_fops,
  208. };
  209. /*
  210. * The WDT needs to learn about soft shutdowns in order to
  211. * turn the timebomb registers off.
  212. */
  213. static struct notifier_block advwdt_notifier = {
  214. .notifier_call = advwdt_notify_sys,
  215. };
  216. static int __init
  217. advwdt_init(void)
  218. {
  219. int ret;
  220. printk(KERN_INFO "WDT driver for Advantech single board computer initialising.\n");
  221. if (timeout < 1 || timeout > 63) {
  222. timeout = WATCHDOG_TIMEOUT;
  223. printk (KERN_INFO PFX "timeout value must be 1<=x<=63, using %d\n",
  224. timeout);
  225. }
  226. if (wdt_stop != wdt_start) {
  227. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  228. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  229. wdt_stop);
  230. ret = -EIO;
  231. goto out;
  232. }
  233. }
  234. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  235. printk (KERN_ERR PFX "I/O address 0x%04x already in use\n",
  236. wdt_start);
  237. ret = -EIO;
  238. goto unreg_stop;
  239. }
  240. ret = register_reboot_notifier(&advwdt_notifier);
  241. if (ret != 0) {
  242. printk (KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
  243. ret);
  244. goto unreg_regions;
  245. }
  246. ret = misc_register(&advwdt_miscdev);
  247. if (ret != 0) {
  248. printk (KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
  249. WATCHDOG_MINOR, ret);
  250. goto unreg_reboot;
  251. }
  252. printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n",
  253. timeout, nowayout);
  254. out:
  255. return ret;
  256. unreg_reboot:
  257. unregister_reboot_notifier(&advwdt_notifier);
  258. unreg_regions:
  259. release_region(wdt_start, 1);
  260. unreg_stop:
  261. if (wdt_stop != wdt_start)
  262. release_region(wdt_stop, 1);
  263. goto out;
  264. }
  265. static void __exit
  266. advwdt_exit(void)
  267. {
  268. misc_deregister(&advwdt_miscdev);
  269. unregister_reboot_notifier(&advwdt_notifier);
  270. if(wdt_stop != wdt_start)
  271. release_region(wdt_stop,1);
  272. release_region(wdt_start,1);
  273. }
  274. module_init(advwdt_init);
  275. module_exit(advwdt_exit);
  276. MODULE_LICENSE("GPL");
  277. MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
  278. MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");
  279. MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);