advantechwdt.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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@lxorguk.ukuu.org.uk>,
  10. * All Rights Reserved.
  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@lxorguk.ukuu.org.uk>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/types.h>
  34. #include <linux/miscdevice.h>
  35. #include <linux/watchdog.h>
  36. #include <linux/fs.h>
  37. #include <linux/ioport.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/init.h>
  40. #include <linux/io.h>
  41. #include <linux/uaccess.h>
  42. #define DRV_NAME "advantechwdt"
  43. #define WATCHDOG_NAME "Advantech WDT"
  44. #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */
  45. /* the watchdog platform device */
  46. static struct platform_device *advwdt_platform_device;
  47. static unsigned long advwdt_is_open;
  48. static char adv_expect_close;
  49. /*
  50. * You must set these - there is no sane way to probe for this board.
  51. *
  52. * To enable or restart, write the timeout value in seconds (1 to 63)
  53. * to I/O port wdt_start. To disable, read I/O port wdt_stop.
  54. * Both are 0x443 for most boards (tested on a PCA-6276VE-00B1), but
  55. * check your manual (at least the PCA-6159 seems to be different -
  56. * the manual says wdt_stop is 0x43, not 0x443).
  57. * (0x43 is also a write-only control register for the 8254 timer!)
  58. */
  59. static int wdt_stop = 0x443;
  60. module_param(wdt_stop, int, 0);
  61. MODULE_PARM_DESC(wdt_stop, "Advantech WDT 'stop' io port (default 0x443)");
  62. static int wdt_start = 0x443;
  63. module_param(wdt_start, int, 0);
  64. MODULE_PARM_DESC(wdt_start, "Advantech WDT 'start' io port (default 0x443)");
  65. static int timeout = WATCHDOG_TIMEOUT; /* in seconds */
  66. module_param(timeout, int, 0);
  67. MODULE_PARM_DESC(timeout,
  68. "Watchdog timeout in seconds. 1<= timeout <=63, default="
  69. __MODULE_STRING(WATCHDOG_TIMEOUT) ".");
  70. static bool nowayout = WATCHDOG_NOWAYOUT;
  71. module_param(nowayout, bool, 0);
  72. MODULE_PARM_DESC(nowayout,
  73. "Watchdog cannot be stopped once started (default="
  74. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  75. /*
  76. * Watchdog Operations
  77. */
  78. static void advwdt_ping(void)
  79. {
  80. /* Write a watchdog value */
  81. outb_p(timeout, wdt_start);
  82. }
  83. static void advwdt_disable(void)
  84. {
  85. inb_p(wdt_stop);
  86. }
  87. static int advwdt_set_heartbeat(int t)
  88. {
  89. if (t < 1 || t > 63)
  90. return -EINVAL;
  91. timeout = t;
  92. return 0;
  93. }
  94. /*
  95. * /dev/watchdog handling
  96. */
  97. static ssize_t advwdt_write(struct file *file, const char __user *buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. if (count) {
  101. if (!nowayout) {
  102. size_t i;
  103. adv_expect_close = 0;
  104. for (i = 0; i != count; i++) {
  105. char c;
  106. if (get_user(c, buf + i))
  107. return -EFAULT;
  108. if (c == 'V')
  109. adv_expect_close = 42;
  110. }
  111. }
  112. advwdt_ping();
  113. }
  114. return count;
  115. }
  116. static long advwdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  117. {
  118. int new_timeout;
  119. void __user *argp = (void __user *)arg;
  120. int __user *p = argp;
  121. static const struct watchdog_info ident = {
  122. .options = WDIOF_KEEPALIVEPING |
  123. WDIOF_SETTIMEOUT |
  124. WDIOF_MAGICCLOSE,
  125. .firmware_version = 1,
  126. .identity = WATCHDOG_NAME,
  127. };
  128. switch (cmd) {
  129. case WDIOC_GETSUPPORT:
  130. if (copy_to_user(argp, &ident, sizeof(ident)))
  131. return -EFAULT;
  132. break;
  133. case WDIOC_GETSTATUS:
  134. case WDIOC_GETBOOTSTATUS:
  135. return put_user(0, 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. case WDIOC_KEEPALIVE:
  152. advwdt_ping();
  153. break;
  154. case WDIOC_SETTIMEOUT:
  155. if (get_user(new_timeout, p))
  156. return -EFAULT;
  157. if (advwdt_set_heartbeat(new_timeout))
  158. return -EINVAL;
  159. advwdt_ping();
  160. /* Fall */
  161. case WDIOC_GETTIMEOUT:
  162. return put_user(timeout, p);
  163. default:
  164. return -ENOTTY;
  165. }
  166. return 0;
  167. }
  168. static int advwdt_open(struct inode *inode, struct file *file)
  169. {
  170. if (test_and_set_bit(0, &advwdt_is_open))
  171. return -EBUSY;
  172. /*
  173. * Activate
  174. */
  175. advwdt_ping();
  176. return nonseekable_open(inode, file);
  177. }
  178. static int advwdt_close(struct inode *inode, struct file *file)
  179. {
  180. if (adv_expect_close == 42) {
  181. advwdt_disable();
  182. } else {
  183. pr_crit("Unexpected close, not stopping watchdog!\n");
  184. advwdt_ping();
  185. }
  186. clear_bit(0, &advwdt_is_open);
  187. adv_expect_close = 0;
  188. return 0;
  189. }
  190. /*
  191. * Kernel Interfaces
  192. */
  193. static const struct file_operations advwdt_fops = {
  194. .owner = THIS_MODULE,
  195. .llseek = no_llseek,
  196. .write = advwdt_write,
  197. .unlocked_ioctl = advwdt_ioctl,
  198. .open = advwdt_open,
  199. .release = advwdt_close,
  200. };
  201. static struct miscdevice advwdt_miscdev = {
  202. .minor = WATCHDOG_MINOR,
  203. .name = "watchdog",
  204. .fops = &advwdt_fops,
  205. };
  206. /*
  207. * Init & exit routines
  208. */
  209. static int __init advwdt_probe(struct platform_device *dev)
  210. {
  211. int ret;
  212. if (wdt_stop != wdt_start) {
  213. if (!request_region(wdt_stop, 1, WATCHDOG_NAME)) {
  214. pr_err("I/O address 0x%04x already in use\n",
  215. wdt_stop);
  216. ret = -EIO;
  217. goto out;
  218. }
  219. }
  220. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  221. pr_err("I/O address 0x%04x already in use\n", wdt_start);
  222. ret = -EIO;
  223. goto unreg_stop;
  224. }
  225. /* Check that the heartbeat value is within it's range ;
  226. * if not reset to the default */
  227. if (advwdt_set_heartbeat(timeout)) {
  228. advwdt_set_heartbeat(WATCHDOG_TIMEOUT);
  229. pr_info("timeout value must be 1<=x<=63, using %d\n", timeout);
  230. }
  231. ret = misc_register(&advwdt_miscdev);
  232. if (ret != 0) {
  233. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  234. WATCHDOG_MINOR, ret);
  235. goto unreg_regions;
  236. }
  237. pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
  238. timeout, nowayout);
  239. out:
  240. return ret;
  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. goto out;
  247. }
  248. static int advwdt_remove(struct platform_device *dev)
  249. {
  250. misc_deregister(&advwdt_miscdev);
  251. release_region(wdt_start, 1);
  252. if (wdt_stop != wdt_start)
  253. release_region(wdt_stop, 1);
  254. return 0;
  255. }
  256. static void advwdt_shutdown(struct platform_device *dev)
  257. {
  258. /* Turn the WDT off if we have a soft shutdown */
  259. advwdt_disable();
  260. }
  261. static struct platform_driver advwdt_driver = {
  262. .remove = advwdt_remove,
  263. .shutdown = advwdt_shutdown,
  264. .driver = {
  265. .name = DRV_NAME,
  266. },
  267. };
  268. static int __init advwdt_init(void)
  269. {
  270. int err;
  271. pr_info("WDT driver for Advantech single board computer initialising\n");
  272. advwdt_platform_device = platform_device_register_simple(DRV_NAME,
  273. -1, NULL, 0);
  274. if (IS_ERR(advwdt_platform_device))
  275. return PTR_ERR(advwdt_platform_device);
  276. err = platform_driver_probe(&advwdt_driver, advwdt_probe);
  277. if (err)
  278. goto unreg_platform_device;
  279. return 0;
  280. unreg_platform_device:
  281. platform_device_unregister(advwdt_platform_device);
  282. return err;
  283. }
  284. static void __exit advwdt_exit(void)
  285. {
  286. platform_device_unregister(advwdt_platform_device);
  287. platform_driver_unregister(&advwdt_driver);
  288. pr_info("Watchdog Module Unloaded\n");
  289. }
  290. module_init(advwdt_init);
  291. module_exit(advwdt_exit);
  292. MODULE_LICENSE("GPL");
  293. MODULE_AUTHOR("Marek Michalkiewicz <marekm@linux.org.pl>");
  294. MODULE_DESCRIPTION("Advantech Single Board Computer WDT driver");