acquirewdt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Acquire Single Board Computer Watchdog Timer driver
  4. *
  5. * Based on wdt.c. Original copyright messages:
  6. *
  7. * (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
  8. * All Rights Reserved.
  9. *
  10. * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
  11. * warranty for any of this software. This material is provided
  12. * "AS-IS" and at no charge.
  13. *
  14. * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
  15. *
  16. * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com>
  17. * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT
  18. * Can't add timeout - driver doesn't allow changing value
  19. */
  20. /*
  21. * Theory of Operation:
  22. * The Watch-Dog Timer is provided to ensure that standalone
  23. * Systems can always recover from catastrophic conditions that
  24. * caused the CPU to crash. This condition may have occurred by
  25. * external EMI or a software bug. When the CPU stops working
  26. * correctly, hardware on the board will either perform a hardware
  27. * reset (cold boot) or a non-maskable interrupt (NMI) to bring the
  28. * system back to a known state.
  29. *
  30. * The Watch-Dog Timer is controlled by two I/O Ports.
  31. * 443 hex - Read - Enable or refresh the Watch-Dog Timer
  32. * 043 hex - Read - Disable the Watch-Dog Timer
  33. *
  34. * To enable the Watch-Dog Timer, a read from I/O port 443h must
  35. * be performed. This will enable and activate the countdown timer
  36. * which will eventually time out and either reset the CPU or cause
  37. * an NMI depending on the setting of a jumper. To ensure that this
  38. * reset condition does not occur, the Watch-Dog Timer must be
  39. * periodically refreshed by reading the same I/O port 443h.
  40. * The Watch-Dog Timer is disabled by reading I/O port 043h.
  41. *
  42. * The Watch-Dog Timer Time-Out Period is set via jumpers.
  43. * It can be 1, 2, 10, 20, 110 or 220 seconds.
  44. */
  45. /*
  46. * Includes, defines, variables, module parameters, ...
  47. */
  48. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  49. /* Includes */
  50. #include <linux/module.h> /* For module specific items */
  51. #include <linux/moduleparam.h> /* For new moduleparam's */
  52. #include <linux/types.h> /* For standard types (like size_t) */
  53. #include <linux/errno.h> /* For the -ENODEV/... values */
  54. #include <linux/kernel.h> /* For printk/panic/... */
  55. #include <linux/miscdevice.h> /* For struct miscdevice */
  56. #include <linux/watchdog.h> /* For the watchdog specific items */
  57. #include <linux/fs.h> /* For file operations */
  58. #include <linux/ioport.h> /* For io-port access */
  59. #include <linux/platform_device.h> /* For platform_driver framework */
  60. #include <linux/init.h> /* For __init/__exit/... */
  61. #include <linux/uaccess.h> /* For copy_to_user/put_user/... */
  62. #include <linux/io.h> /* For inb/outb/... */
  63. /* Module information */
  64. #define DRV_NAME "acquirewdt"
  65. #define WATCHDOG_NAME "Acquire WDT"
  66. /* There is no way to see what the correct time-out period is */
  67. #define WATCHDOG_HEARTBEAT 0
  68. /* internal variables */
  69. /* the watchdog platform device */
  70. static struct platform_device *acq_platform_device;
  71. static unsigned long acq_is_open;
  72. static char expect_close;
  73. /* module parameters */
  74. /* You must set this - there is no sane way to probe for this board. */
  75. static int wdt_stop = 0x43;
  76. module_param(wdt_stop, int, 0);
  77. MODULE_PARM_DESC(wdt_stop, "Acquire WDT 'stop' io port (default 0x43)");
  78. /* You must set this - there is no sane way to probe for this board. */
  79. static int wdt_start = 0x443;
  80. module_param(wdt_start, int, 0);
  81. MODULE_PARM_DESC(wdt_start, "Acquire WDT 'start' io port (default 0x443)");
  82. static bool nowayout = WATCHDOG_NOWAYOUT;
  83. module_param(nowayout, bool, 0);
  84. MODULE_PARM_DESC(nowayout,
  85. "Watchdog cannot be stopped once started (default="
  86. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  87. /*
  88. * Watchdog Operations
  89. */
  90. static void acq_keepalive(void)
  91. {
  92. /* Write a watchdog value */
  93. inb_p(wdt_start);
  94. }
  95. static void acq_stop(void)
  96. {
  97. /* Turn the card off */
  98. inb_p(wdt_stop);
  99. }
  100. /*
  101. * /dev/watchdog handling
  102. */
  103. static ssize_t acq_write(struct file *file, const char __user *buf,
  104. size_t count, loff_t *ppos)
  105. {
  106. /* See if we got the magic character 'V' and reload the timer */
  107. if (count) {
  108. if (!nowayout) {
  109. size_t i;
  110. /* note: just in case someone wrote the magic character
  111. five months ago... */
  112. expect_close = 0;
  113. /* scan to see whether or not we got the
  114. magic character */
  115. for (i = 0; i != count; i++) {
  116. char c;
  117. if (get_user(c, buf + i))
  118. return -EFAULT;
  119. if (c == 'V')
  120. expect_close = 42;
  121. }
  122. }
  123. /* Well, anyhow someone wrote to us, we should
  124. return that favour */
  125. acq_keepalive();
  126. }
  127. return count;
  128. }
  129. static long acq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  130. {
  131. int options, retval = -EINVAL;
  132. void __user *argp = (void __user *)arg;
  133. int __user *p = argp;
  134. static const struct watchdog_info ident = {
  135. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  136. .firmware_version = 1,
  137. .identity = WATCHDOG_NAME,
  138. };
  139. switch (cmd) {
  140. case WDIOC_GETSUPPORT:
  141. return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  142. case WDIOC_GETSTATUS:
  143. case WDIOC_GETBOOTSTATUS:
  144. return put_user(0, p);
  145. case WDIOC_SETOPTIONS:
  146. {
  147. if (get_user(options, p))
  148. return -EFAULT;
  149. if (options & WDIOS_DISABLECARD) {
  150. acq_stop();
  151. retval = 0;
  152. }
  153. if (options & WDIOS_ENABLECARD) {
  154. acq_keepalive();
  155. retval = 0;
  156. }
  157. return retval;
  158. }
  159. case WDIOC_KEEPALIVE:
  160. acq_keepalive();
  161. return 0;
  162. case WDIOC_GETTIMEOUT:
  163. return put_user(WATCHDOG_HEARTBEAT, p);
  164. default:
  165. return -ENOTTY;
  166. }
  167. }
  168. static int acq_open(struct inode *inode, struct file *file)
  169. {
  170. if (test_and_set_bit(0, &acq_is_open))
  171. return -EBUSY;
  172. if (nowayout)
  173. __module_get(THIS_MODULE);
  174. /* Activate */
  175. acq_keepalive();
  176. return stream_open(inode, file);
  177. }
  178. static int acq_close(struct inode *inode, struct file *file)
  179. {
  180. if (expect_close == 42) {
  181. acq_stop();
  182. } else {
  183. pr_crit("Unexpected close, not stopping watchdog!\n");
  184. acq_keepalive();
  185. }
  186. clear_bit(0, &acq_is_open);
  187. expect_close = 0;
  188. return 0;
  189. }
  190. /*
  191. * Kernel Interfaces
  192. */
  193. static const struct file_operations acq_fops = {
  194. .owner = THIS_MODULE,
  195. .llseek = no_llseek,
  196. .write = acq_write,
  197. .unlocked_ioctl = acq_ioctl,
  198. .open = acq_open,
  199. .release = acq_close,
  200. };
  201. static struct miscdevice acq_miscdev = {
  202. .minor = WATCHDOG_MINOR,
  203. .name = "watchdog",
  204. .fops = &acq_fops,
  205. };
  206. /*
  207. * Init & exit routines
  208. */
  209. static int __init acq_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", wdt_stop);
  215. ret = -EIO;
  216. goto out;
  217. }
  218. }
  219. if (!request_region(wdt_start, 1, WATCHDOG_NAME)) {
  220. pr_err("I/O address 0x%04x already in use\n", wdt_start);
  221. ret = -EIO;
  222. goto unreg_stop;
  223. }
  224. ret = misc_register(&acq_miscdev);
  225. if (ret != 0) {
  226. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  227. WATCHDOG_MINOR, ret);
  228. goto unreg_regions;
  229. }
  230. pr_info("initialized. (nowayout=%d)\n", nowayout);
  231. return 0;
  232. unreg_regions:
  233. release_region(wdt_start, 1);
  234. unreg_stop:
  235. if (wdt_stop != wdt_start)
  236. release_region(wdt_stop, 1);
  237. out:
  238. return ret;
  239. }
  240. static int acq_remove(struct platform_device *dev)
  241. {
  242. misc_deregister(&acq_miscdev);
  243. release_region(wdt_start, 1);
  244. if (wdt_stop != wdt_start)
  245. release_region(wdt_stop, 1);
  246. return 0;
  247. }
  248. static void acq_shutdown(struct platform_device *dev)
  249. {
  250. /* Turn the WDT off if we have a soft shutdown */
  251. acq_stop();
  252. }
  253. static struct platform_driver acquirewdt_driver = {
  254. .remove = acq_remove,
  255. .shutdown = acq_shutdown,
  256. .driver = {
  257. .name = DRV_NAME,
  258. },
  259. };
  260. static int __init acq_init(void)
  261. {
  262. int err;
  263. pr_info("WDT driver for Acquire single board computer initialising\n");
  264. acq_platform_device = platform_device_register_simple(DRV_NAME,
  265. -1, NULL, 0);
  266. if (IS_ERR(acq_platform_device))
  267. return PTR_ERR(acq_platform_device);
  268. err = platform_driver_probe(&acquirewdt_driver, acq_probe);
  269. if (err)
  270. goto unreg_platform_device;
  271. return 0;
  272. unreg_platform_device:
  273. platform_device_unregister(acq_platform_device);
  274. return err;
  275. }
  276. static void __exit acq_exit(void)
  277. {
  278. platform_device_unregister(acq_platform_device);
  279. platform_driver_unregister(&acquirewdt_driver);
  280. pr_info("Watchdog Module Unloaded\n");
  281. }
  282. module_init(acq_init);
  283. module_exit(acq_exit);
  284. MODULE_AUTHOR("David Woodhouse");
  285. MODULE_DESCRIPTION("Acquire Inc. Single Board Computer Watchdog Timer driver");
  286. MODULE_LICENSE("GPL");