sbc_fitpc2_wdt.c 5.3 KB

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