mixcomwd.c 6.9 KB

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