sbc_epx_c3.c 5.1 KB

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