sbc_epx_c3.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SBC EPX C3 0.1 A Hardware Watchdog Device for the Winsystems EPX-C3
  4. * single board computer
  5. *
  6. * (c) Copyright 2006 Calin A. Culianu <calin@ajvar.org>, All Rights
  7. * Reserved.
  8. *
  9. * based on softdog.c by Alan Cox <alan@lxorguk.ukuu.org.uk>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/fs.h>
  17. #include <linux/mm.h>
  18. #include <linux/miscdevice.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/notifier.h>
  21. #include <linux/reboot.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. static int epx_c3_alive;
  27. #define WATCHDOG_TIMEOUT 1 /* 1 sec default timeout */
  28. static bool nowayout = WATCHDOG_NOWAYOUT;
  29. module_param(nowayout, bool, 0);
  30. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
  31. __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  32. #define EPXC3_WATCHDOG_CTL_REG 0x1ee /* write 1 to enable, 0 to disable */
  33. #define EPXC3_WATCHDOG_PET_REG 0x1ef /* write anything to pet once enabled */
  34. static void epx_c3_start(void)
  35. {
  36. outb(1, EPXC3_WATCHDOG_CTL_REG);
  37. }
  38. static void epx_c3_stop(void)
  39. {
  40. outb(0, EPXC3_WATCHDOG_CTL_REG);
  41. pr_info("Stopped watchdog timer\n");
  42. }
  43. static void epx_c3_pet(void)
  44. {
  45. outb(1, EPXC3_WATCHDOG_PET_REG);
  46. }
  47. /*
  48. * Allow only one person to hold it open
  49. */
  50. static int epx_c3_open(struct inode *inode, struct file *file)
  51. {
  52. if (epx_c3_alive)
  53. return -EBUSY;
  54. if (nowayout)
  55. __module_get(THIS_MODULE);
  56. /* Activate timer */
  57. epx_c3_start();
  58. epx_c3_pet();
  59. epx_c3_alive = 1;
  60. pr_info("Started watchdog timer\n");
  61. return stream_open(inode, file);
  62. }
  63. static int epx_c3_release(struct inode *inode, struct file *file)
  64. {
  65. /* Shut off the timer.
  66. * Lock it in if it's a module and we defined ...NOWAYOUT */
  67. if (!nowayout)
  68. epx_c3_stop(); /* Turn the WDT off */
  69. epx_c3_alive = 0;
  70. return 0;
  71. }
  72. static ssize_t epx_c3_write(struct file *file, const char __user *data,
  73. size_t len, loff_t *ppos)
  74. {
  75. /* Refresh the timer. */
  76. if (len)
  77. epx_c3_pet();
  78. return len;
  79. }
  80. static long epx_c3_ioctl(struct file *file, unsigned int cmd,
  81. unsigned long arg)
  82. {
  83. int options, retval = -EINVAL;
  84. int __user *argp = (void __user *)arg;
  85. static const struct watchdog_info ident = {
  86. .options = WDIOF_KEEPALIVEPING,
  87. .firmware_version = 0,
  88. .identity = "Winsystems EPX-C3 H/W Watchdog",
  89. };
  90. switch (cmd) {
  91. case WDIOC_GETSUPPORT:
  92. if (copy_to_user(argp, &ident, sizeof(ident)))
  93. return -EFAULT;
  94. return 0;
  95. case WDIOC_GETSTATUS:
  96. case WDIOC_GETBOOTSTATUS:
  97. return put_user(0, argp);
  98. case WDIOC_SETOPTIONS:
  99. if (get_user(options, argp))
  100. return -EFAULT;
  101. if (options & WDIOS_DISABLECARD) {
  102. epx_c3_stop();
  103. retval = 0;
  104. }
  105. if (options & WDIOS_ENABLECARD) {
  106. epx_c3_start();
  107. retval = 0;
  108. }
  109. return retval;
  110. case WDIOC_KEEPALIVE:
  111. epx_c3_pet();
  112. return 0;
  113. case WDIOC_GETTIMEOUT:
  114. return put_user(WATCHDOG_TIMEOUT, argp);
  115. default:
  116. return -ENOTTY;
  117. }
  118. }
  119. static int epx_c3_notify_sys(struct notifier_block *this, unsigned long code,
  120. void *unused)
  121. {
  122. if (code == SYS_DOWN || code == SYS_HALT)
  123. epx_c3_stop(); /* Turn the WDT off */
  124. return NOTIFY_DONE;
  125. }
  126. static const struct file_operations epx_c3_fops = {
  127. .owner = THIS_MODULE,
  128. .llseek = no_llseek,
  129. .write = epx_c3_write,
  130. .unlocked_ioctl = epx_c3_ioctl,
  131. .compat_ioctl = compat_ptr_ioctl,
  132. .open = epx_c3_open,
  133. .release = epx_c3_release,
  134. };
  135. static struct miscdevice epx_c3_miscdev = {
  136. .minor = WATCHDOG_MINOR,
  137. .name = "watchdog",
  138. .fops = &epx_c3_fops,
  139. };
  140. static struct notifier_block epx_c3_notifier = {
  141. .notifier_call = epx_c3_notify_sys,
  142. };
  143. static int __init watchdog_init(void)
  144. {
  145. int ret;
  146. if (!request_region(EPXC3_WATCHDOG_CTL_REG, 2, "epxc3_watchdog"))
  147. return -EBUSY;
  148. ret = register_reboot_notifier(&epx_c3_notifier);
  149. if (ret) {
  150. pr_err("cannot register reboot notifier (err=%d)\n", ret);
  151. goto out;
  152. }
  153. ret = misc_register(&epx_c3_miscdev);
  154. if (ret) {
  155. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  156. WATCHDOG_MINOR, ret);
  157. unregister_reboot_notifier(&epx_c3_notifier);
  158. goto out;
  159. }
  160. pr_info("Hardware Watchdog Timer for Winsystems EPX-C3 SBC: 0.1\n");
  161. return 0;
  162. out:
  163. release_region(EPXC3_WATCHDOG_CTL_REG, 2);
  164. return ret;
  165. }
  166. static void __exit watchdog_exit(void)
  167. {
  168. misc_deregister(&epx_c3_miscdev);
  169. unregister_reboot_notifier(&epx_c3_notifier);
  170. release_region(EPXC3_WATCHDOG_CTL_REG, 2);
  171. }
  172. module_init(watchdog_init);
  173. module_exit(watchdog_exit);
  174. MODULE_AUTHOR("Calin A. Culianu <calin@ajvar.org>");
  175. MODULE_DESCRIPTION("Hardware Watchdog Device for Winsystems EPX-C3 SBC. "
  176. "Note that there is no way to probe for this device -- "
  177. "so only use it if you are *sure* you are running on this specific "
  178. "SBC system from Winsystems! It writes to IO ports 0x1ee and 0x1ef!");
  179. MODULE_LICENSE("GPL");