advantechwdt.c 8.0 KB

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