advantechwdt.c 8.0 KB

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