mixcomwd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MixCom Watchdog: A Simple Hardware Watchdog Device
  4. * Based on Softdog driver by Alan Cox and PC Watchdog driver by Ken Hollis
  5. *
  6. * Author: Gergely Madarasz <gorgo@itc.hu>
  7. *
  8. * Copyright (c) 1999 ITConsult-Pro Co. <info@itc.hu>
  9. *
  10. * Version 0.1 (99/04/15):
  11. * - first version
  12. *
  13. * Version 0.2 (99/06/16):
  14. * - added kernel timer watchdog ping after close
  15. * since the hardware does not support watchdog shutdown
  16. *
  17. * Version 0.3 (99/06/21):
  18. * - added WDIOC_GETSTATUS and WDIOC_GETSUPPORT ioctl calls
  19. *
  20. * Version 0.3.1 (99/06/22):
  21. * - allow module removal while internal timer is active,
  22. * print warning about probable reset
  23. *
  24. * Version 0.4 (99/11/15):
  25. * - support for one more type board
  26. *
  27. * Version 0.5 (2001/12/14) Matt Domsch <Matt_Domsch@dell.com>
  28. * - added nowayout module option to override
  29. * CONFIG_WATCHDOG_NOWAYOUT
  30. *
  31. * Version 0.6 (2002/04/12): Rob Radez <rob@osinvestor.com>
  32. * - make mixcomwd_opened unsigned,
  33. * removed lock_kernel/unlock_kernel from mixcomwd_release,
  34. * modified ioctl a bit to conform to API
  35. */
  36. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  37. #define VERSION "0.6"
  38. #define WATCHDOG_NAME "mixcomwd"
  39. #include <linux/module.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/types.h>
  42. #include <linux/miscdevice.h>
  43. #include <linux/ioport.h>
  44. #include <linux/watchdog.h>
  45. #include <linux/fs.h>
  46. #include <linux/reboot.h>
  47. #include <linux/init.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/timer.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/io.h>
  52. /*
  53. * We have two types of cards that can be probed:
  54. * 1) The Mixcom cards: these cards can be found at addresses
  55. * 0x180, 0x280, 0x380 with an additional offset of 0xc10.
  56. * (Or 0xd90, 0xe90, 0xf90).
  57. * 2) The FlashCOM cards: these cards can be set up at
  58. * 0x300 -> 0x378, in 0x8 jumps with an offset of 0x04.
  59. * (Or 0x304 -> 0x37c in 0x8 jumps).
  60. * Each card has it's own ID.
  61. */
  62. #define MIXCOM_ID 0x11
  63. #define FLASHCOM_ID 0x18
  64. static struct {
  65. int ioport;
  66. int id;
  67. } mixcomwd_io_info[] = {
  68. /* The Mixcom cards */
  69. {0x0d90, MIXCOM_ID},
  70. {0x0e90, MIXCOM_ID},
  71. {0x0f90, MIXCOM_ID},
  72. /* The FlashCOM cards */
  73. {0x0304, FLASHCOM_ID},
  74. {0x030c, FLASHCOM_ID},
  75. {0x0314, FLASHCOM_ID},
  76. {0x031c, FLASHCOM_ID},
  77. {0x0324, FLASHCOM_ID},
  78. {0x032c, FLASHCOM_ID},
  79. {0x0334, FLASHCOM_ID},
  80. {0x033c, FLASHCOM_ID},
  81. {0x0344, FLASHCOM_ID},
  82. {0x034c, FLASHCOM_ID},
  83. {0x0354, FLASHCOM_ID},
  84. {0x035c, FLASHCOM_ID},
  85. {0x0364, FLASHCOM_ID},
  86. {0x036c, FLASHCOM_ID},
  87. {0x0374, FLASHCOM_ID},
  88. {0x037c, FLASHCOM_ID},
  89. /* The end of the list */
  90. {0x0000, 0},
  91. };
  92. static void mixcomwd_timerfun(struct timer_list *unused);
  93. static unsigned long mixcomwd_opened; /* long req'd for setbit --RR */
  94. static int watchdog_port;
  95. static int mixcomwd_timer_alive;
  96. static DEFINE_TIMER(mixcomwd_timer, mixcomwd_timerfun);
  97. static char expect_close;
  98. static bool nowayout = WATCHDOG_NOWAYOUT;
  99. module_param(nowayout, bool, 0);
  100. MODULE_PARM_DESC(nowayout,
  101. "Watchdog cannot be stopped once started (default="
  102. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  103. static void mixcomwd_ping(void)
  104. {
  105. outb_p(55, watchdog_port);
  106. return;
  107. }
  108. static void mixcomwd_timerfun(struct timer_list *unused)
  109. {
  110. mixcomwd_ping();
  111. mod_timer(&mixcomwd_timer, jiffies + 5 * HZ);
  112. }
  113. /*
  114. * Allow only one person to hold it open
  115. */
  116. static int mixcomwd_open(struct inode *inode, struct file *file)
  117. {
  118. if (test_and_set_bit(0, &mixcomwd_opened))
  119. return -EBUSY;
  120. mixcomwd_ping();
  121. if (nowayout)
  122. /*
  123. * fops_get() code via open() has already done
  124. * a try_module_get() so it is safe to do the
  125. * __module_get().
  126. */
  127. __module_get(THIS_MODULE);
  128. else {
  129. if (mixcomwd_timer_alive) {
  130. del_timer(&mixcomwd_timer);
  131. mixcomwd_timer_alive = 0;
  132. }
  133. }
  134. return stream_open(inode, file);
  135. }
  136. static int mixcomwd_release(struct inode *inode, struct file *file)
  137. {
  138. if (expect_close == 42) {
  139. if (mixcomwd_timer_alive) {
  140. pr_err("release called while internal timer alive\n");
  141. return -EBUSY;
  142. }
  143. mixcomwd_timer_alive = 1;
  144. mod_timer(&mixcomwd_timer, jiffies + 5 * HZ);
  145. } else
  146. pr_crit("WDT device closed unexpectedly. WDT will not stop!\n");
  147. clear_bit(0, &mixcomwd_opened);
  148. expect_close = 0;
  149. return 0;
  150. }
  151. static ssize_t mixcomwd_write(struct file *file, const char __user *data,
  152. size_t len, loff_t *ppos)
  153. {
  154. if (len) {
  155. if (!nowayout) {
  156. size_t i;
  157. /* In case it was set long ago */
  158. expect_close = 0;
  159. for (i = 0; i != len; i++) {
  160. char c;
  161. if (get_user(c, data + i))
  162. return -EFAULT;
  163. if (c == 'V')
  164. expect_close = 42;
  165. }
  166. }
  167. mixcomwd_ping();
  168. }
  169. return len;
  170. }
  171. static long mixcomwd_ioctl(struct file *file,
  172. unsigned int cmd, unsigned long arg)
  173. {
  174. void __user *argp = (void __user *)arg;
  175. int __user *p = argp;
  176. int status;
  177. static const struct watchdog_info ident = {
  178. .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  179. .firmware_version = 1,
  180. .identity = "MixCOM watchdog",
  181. };
  182. switch (cmd) {
  183. case WDIOC_GETSUPPORT:
  184. if (copy_to_user(argp, &ident, sizeof(ident)))
  185. return -EFAULT;
  186. break;
  187. case WDIOC_GETSTATUS:
  188. status = mixcomwd_opened;
  189. if (!nowayout)
  190. status |= mixcomwd_timer_alive;
  191. return put_user(status, p);
  192. case WDIOC_GETBOOTSTATUS:
  193. return put_user(0, p);
  194. case WDIOC_KEEPALIVE:
  195. mixcomwd_ping();
  196. break;
  197. default:
  198. return -ENOTTY;
  199. }
  200. return 0;
  201. }
  202. static const struct file_operations mixcomwd_fops = {
  203. .owner = THIS_MODULE,
  204. .llseek = no_llseek,
  205. .write = mixcomwd_write,
  206. .unlocked_ioctl = mixcomwd_ioctl,
  207. .compat_ioctl = compat_ptr_ioctl,
  208. .open = mixcomwd_open,
  209. .release = mixcomwd_release,
  210. };
  211. static struct miscdevice mixcomwd_miscdev = {
  212. .minor = WATCHDOG_MINOR,
  213. .name = "watchdog",
  214. .fops = &mixcomwd_fops,
  215. };
  216. static int __init checkcard(int port, int card_id)
  217. {
  218. int id;
  219. if (!request_region(port, 1, "MixCOM watchdog"))
  220. return 0;
  221. id = inb_p(port);
  222. if (card_id == MIXCOM_ID)
  223. id &= 0x3f;
  224. if (id != card_id) {
  225. release_region(port, 1);
  226. return 0;
  227. }
  228. return 1;
  229. }
  230. static int __init mixcomwd_init(void)
  231. {
  232. int i, ret, found = 0;
  233. for (i = 0; !found && mixcomwd_io_info[i].ioport != 0; i++) {
  234. if (checkcard(mixcomwd_io_info[i].ioport,
  235. mixcomwd_io_info[i].id)) {
  236. found = 1;
  237. watchdog_port = mixcomwd_io_info[i].ioport;
  238. }
  239. }
  240. if (!found) {
  241. pr_err("No card detected, or port not available\n");
  242. return -ENODEV;
  243. }
  244. ret = misc_register(&mixcomwd_miscdev);
  245. if (ret) {
  246. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  247. WATCHDOG_MINOR, ret);
  248. goto error_misc_register_watchdog;
  249. }
  250. pr_info("MixCOM watchdog driver v%s, watchdog port at 0x%3x\n",
  251. VERSION, watchdog_port);
  252. return 0;
  253. error_misc_register_watchdog:
  254. release_region(watchdog_port, 1);
  255. watchdog_port = 0x0000;
  256. return ret;
  257. }
  258. static void __exit mixcomwd_exit(void)
  259. {
  260. if (!nowayout) {
  261. if (mixcomwd_timer_alive) {
  262. pr_warn("I quit now, hardware will probably reboot!\n");
  263. del_timer_sync(&mixcomwd_timer);
  264. mixcomwd_timer_alive = 0;
  265. }
  266. }
  267. misc_deregister(&mixcomwd_miscdev);
  268. release_region(watchdog_port, 1);
  269. }
  270. module_init(mixcomwd_init);
  271. module_exit(mixcomwd_exit);
  272. MODULE_AUTHOR("Gergely Madarasz <gorgo@itc.hu>");
  273. MODULE_DESCRIPTION("MixCom Watchdog driver");
  274. MODULE_VERSION(VERSION);
  275. MODULE_LICENSE("GPL");