sbc_fitpc2_wdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Watchdog driver for SBC-FITPC2 board
  3. *
  4. * Author: Denis Turischev <denis@compulab.co.il>
  5. *
  6. * Adapted from the IXP2000 watchdog driver by Deepak Saxena.
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME " WATCHDOG: " fmt
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/watchdog.h>
  17. #include <linux/ioport.h>
  18. #include <linux/delay.h>
  19. #include <linux/fs.h>
  20. #include <linux/init.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/dmi.h>
  23. #include <linux/io.h>
  24. #include <linux/uaccess.h>
  25. static bool nowayout = WATCHDOG_NOWAYOUT;
  26. static unsigned int margin = 60; /* (secs) Default is 1 minute */
  27. static unsigned long wdt_status;
  28. static DEFINE_MUTEX(wdt_lock);
  29. #define WDT_IN_USE 0
  30. #define WDT_OK_TO_CLOSE 1
  31. #define COMMAND_PORT 0x4c
  32. #define DATA_PORT 0x48
  33. #define IFACE_ON_COMMAND 1
  34. #define REBOOT_COMMAND 2
  35. #define WATCHDOG_NAME "SBC-FITPC2 Watchdog"
  36. static void wdt_send_data(unsigned char command, unsigned char data)
  37. {
  38. outb(data, DATA_PORT);
  39. msleep(200);
  40. outb(command, COMMAND_PORT);
  41. msleep(100);
  42. }
  43. static void wdt_enable(void)
  44. {
  45. mutex_lock(&wdt_lock);
  46. wdt_send_data(IFACE_ON_COMMAND, 1);
  47. wdt_send_data(REBOOT_COMMAND, margin);
  48. mutex_unlock(&wdt_lock);
  49. }
  50. static void wdt_disable(void)
  51. {
  52. mutex_lock(&wdt_lock);
  53. wdt_send_data(IFACE_ON_COMMAND, 0);
  54. wdt_send_data(REBOOT_COMMAND, 0);
  55. mutex_unlock(&wdt_lock);
  56. }
  57. static int fitpc2_wdt_open(struct inode *inode, struct file *file)
  58. {
  59. if (test_and_set_bit(WDT_IN_USE, &wdt_status))
  60. return -EBUSY;
  61. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  62. wdt_enable();
  63. return nonseekable_open(inode, file);
  64. }
  65. static ssize_t fitpc2_wdt_write(struct file *file, const char *data,
  66. size_t len, loff_t *ppos)
  67. {
  68. size_t i;
  69. if (!len)
  70. return 0;
  71. if (nowayout) {
  72. len = 0;
  73. goto out;
  74. }
  75. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  76. for (i = 0; i != len; i++) {
  77. char c;
  78. if (get_user(c, data + i))
  79. return -EFAULT;
  80. if (c == 'V')
  81. set_bit(WDT_OK_TO_CLOSE, &wdt_status);
  82. }
  83. out:
  84. wdt_enable();
  85. return len;
  86. }
  87. static const struct watchdog_info ident = {
  88. .options = WDIOF_MAGICCLOSE | WDIOF_SETTIMEOUT |
  89. WDIOF_KEEPALIVEPING,
  90. .identity = WATCHDOG_NAME,
  91. };
  92. static long fitpc2_wdt_ioctl(struct file *file, unsigned int cmd,
  93. unsigned long arg)
  94. {
  95. int ret = -ENOTTY;
  96. int time;
  97. switch (cmd) {
  98. case WDIOC_GETSUPPORT:
  99. ret = copy_to_user((struct watchdog_info *)arg, &ident,
  100. sizeof(ident)) ? -EFAULT : 0;
  101. break;
  102. case WDIOC_GETSTATUS:
  103. ret = put_user(0, (int *)arg);
  104. break;
  105. case WDIOC_GETBOOTSTATUS:
  106. ret = put_user(0, (int *)arg);
  107. break;
  108. case WDIOC_KEEPALIVE:
  109. wdt_enable();
  110. ret = 0;
  111. break;
  112. case WDIOC_SETTIMEOUT:
  113. ret = get_user(time, (int *)arg);
  114. if (ret)
  115. break;
  116. if (time < 31 || time > 255) {
  117. ret = -EINVAL;
  118. break;
  119. }
  120. margin = time;
  121. wdt_enable();
  122. /* Fall through */
  123. case WDIOC_GETTIMEOUT:
  124. ret = put_user(margin, (int *)arg);
  125. break;
  126. }
  127. return ret;
  128. }
  129. static int fitpc2_wdt_release(struct inode *inode, struct file *file)
  130. {
  131. if (test_bit(WDT_OK_TO_CLOSE, &wdt_status)) {
  132. wdt_disable();
  133. pr_info("Device disabled\n");
  134. } else {
  135. pr_warn("Device closed unexpectedly - timer will not stop\n");
  136. wdt_enable();
  137. }
  138. clear_bit(WDT_IN_USE, &wdt_status);
  139. clear_bit(WDT_OK_TO_CLOSE, &wdt_status);
  140. return 0;
  141. }
  142. static const struct file_operations fitpc2_wdt_fops = {
  143. .owner = THIS_MODULE,
  144. .llseek = no_llseek,
  145. .write = fitpc2_wdt_write,
  146. .unlocked_ioctl = fitpc2_wdt_ioctl,
  147. .open = fitpc2_wdt_open,
  148. .release = fitpc2_wdt_release,
  149. };
  150. static struct miscdevice fitpc2_wdt_miscdev = {
  151. .minor = WATCHDOG_MINOR,
  152. .name = "watchdog",
  153. .fops = &fitpc2_wdt_fops,
  154. };
  155. static int __init fitpc2_wdt_init(void)
  156. {
  157. int err;
  158. const char *brd_name;
  159. brd_name = dmi_get_system_info(DMI_BOARD_NAME);
  160. if (!brd_name || !strstr(brd_name, "SBC-FITPC2"))
  161. return -ENODEV;
  162. pr_info("%s found\n", brd_name);
  163. if (!request_region(COMMAND_PORT, 1, WATCHDOG_NAME)) {
  164. pr_err("I/O address 0x%04x already in use\n", COMMAND_PORT);
  165. return -EIO;
  166. }
  167. if (!request_region(DATA_PORT, 1, WATCHDOG_NAME)) {
  168. pr_err("I/O address 0x%04x already in use\n", DATA_PORT);
  169. err = -EIO;
  170. goto err_data_port;
  171. }
  172. if (margin < 31 || margin > 255) {
  173. pr_err("margin must be in range 31 - 255 seconds, you tried to set %d\n",
  174. margin);
  175. err = -EINVAL;
  176. goto err_margin;
  177. }
  178. err = misc_register(&fitpc2_wdt_miscdev);
  179. if (err) {
  180. pr_err("cannot register miscdev on minor=%d (err=%d)\n",
  181. WATCHDOG_MINOR, err);
  182. goto err_margin;
  183. }
  184. return 0;
  185. err_margin:
  186. release_region(DATA_PORT, 1);
  187. err_data_port:
  188. release_region(COMMAND_PORT, 1);
  189. return err;
  190. }
  191. static void __exit fitpc2_wdt_exit(void)
  192. {
  193. misc_deregister(&fitpc2_wdt_miscdev);
  194. release_region(DATA_PORT, 1);
  195. release_region(COMMAND_PORT, 1);
  196. }
  197. module_init(fitpc2_wdt_init);
  198. module_exit(fitpc2_wdt_exit);
  199. MODULE_AUTHOR("Denis Turischev <denis@compulab.co.il>");
  200. MODULE_DESCRIPTION("SBC-FITPC2 Watchdog");
  201. module_param(margin, int, 0);
  202. MODULE_PARM_DESC(margin, "Watchdog margin in seconds (default 60s)");
  203. module_param(nowayout, bool, 0);
  204. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started");
  205. MODULE_LICENSE("GPL");