mixcomwd.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:6k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  */
  35. #define VERSION "0.5" 
  36.   
  37. #include <linux/module.h>
  38. #include <linux/config.h>
  39. #include <linux/types.h>
  40. #include <linux/kernel.h>
  41. #include <linux/fs.h>
  42. #include <linux/mm.h>
  43. #include <linux/miscdevice.h>
  44. #include <linux/ioport.h>
  45. #include <linux/watchdog.h>
  46. #include <linux/reboot.h>
  47. #include <linux/init.h>
  48. #include <linux/smp_lock.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/io.h>
  51. static int mixcomwd_ioports[] = { 0x180, 0x280, 0x380, 0x000 };
  52. #define MIXCOM_WATCHDOG_OFFSET 0xc10
  53. #define MIXCOM_ID 0x11
  54. #define FLASHCOM_WATCHDOG_OFFSET 0x4
  55. #define FLASHCOM_ID 0x18
  56. static long mixcomwd_opened; /* long req'd for setbit --RR */
  57. static int watchdog_port;
  58. static int mixcomwd_timer_alive;
  59. static struct timer_list mixcomwd_timer;
  60. static int expect_close = 0;
  61. #ifdef CONFIG_WATCHDOG_NOWAYOUT
  62. static int nowayout = 1;
  63. #else
  64. static int nowayout = 0;
  65. #endif
  66. MODULE_PARM(nowayout,"i");
  67. MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
  68. static void mixcomwd_ping(void)
  69. {
  70. outb_p(55,watchdog_port);
  71. return;
  72. }
  73. static void mixcomwd_timerfun(unsigned long d)
  74. {
  75. mixcomwd_ping();
  76. mod_timer(&mixcomwd_timer,jiffies+ 5*HZ);
  77. }
  78. /*
  79.  * Allow only one person to hold it open
  80.  */
  81.  
  82. static int mixcomwd_open(struct inode *inode, struct file *file)
  83. {
  84. if(test_and_set_bit(0,&mixcomwd_opened)) {
  85. return -EBUSY;
  86. }
  87. mixcomwd_ping();
  88. if (nowayout) {
  89. MOD_INC_USE_COUNT;
  90. }
  91. if(mixcomwd_timer_alive) {
  92. del_timer(&mixcomwd_timer);
  93. mixcomwd_timer_alive=0;
  94. return 0;
  95. }
  96. static int mixcomwd_release(struct inode *inode, struct file *file)
  97. {
  98. lock_kernel();
  99. if (expect_close) {
  100. if(mixcomwd_timer_alive) {
  101. printk(KERN_ERR "mixcomwd: release called while internal timer alive");
  102. unlock_kernel();
  103. return -EBUSY;
  104. }
  105. init_timer(&mixcomwd_timer);
  106. mixcomwd_timer.expires=jiffies + 5 * HZ;
  107. mixcomwd_timer.function=mixcomwd_timerfun;
  108. mixcomwd_timer.data=0;
  109. mixcomwd_timer_alive=1;
  110. add_timer(&mixcomwd_timer);
  111. } else {
  112. printk(KERN_CRIT "mixcomwd: WDT device closed unexpectedly.  WDT will not stop!n");
  113. }
  114. clear_bit(0,&mixcomwd_opened);
  115. unlock_kernel();
  116. return 0;
  117. }
  118. static ssize_t mixcomwd_write(struct file *file, const char *data, size_t len, loff_t *ppos)
  119. {
  120. if (ppos != &file->f_pos) {
  121. return -ESPIPE;
  122. }
  123. if(len)
  124. {
  125. if (!nowayout) {
  126. size_t i;
  127. /* In case it was set long ago */
  128. expect_close = 0;
  129. for (i = 0; i != len; i++) {
  130. char c;
  131. if (get_user(c, data + i))
  132. return -EFAULT;
  133. if (c == 'V')
  134. expect_close = 1;
  135. }
  136. }
  137. mixcomwd_ping();
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. static int mixcomwd_ioctl(struct inode *inode, struct file *file,
  143. unsigned int cmd, unsigned long arg)
  144. {
  145. int status;
  146.         static struct watchdog_info ident = {
  147. WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
  148. 1, "MixCOM watchdog"
  149. };
  150.                                         
  151. switch(cmd)
  152. {
  153. case WDIOC_GETSTATUS:
  154. status=mixcomwd_opened;
  155. if (!nowayout) {
  156. status|=mixcomwd_timer_alive;
  157. }
  158. if (copy_to_user((int *)arg, &status, sizeof(int))) {
  159. return -EFAULT;
  160. }
  161. break;
  162. case WDIOC_GETSUPPORT:
  163. if (copy_to_user((struct watchdog_info *)arg, &ident, 
  164.     sizeof(ident))) {
  165. return -EFAULT;
  166. }
  167. break;
  168. case WDIOC_KEEPALIVE:
  169. mixcomwd_ping();
  170. break;
  171. default:
  172. return -ENOTTY;
  173. }
  174. return 0;
  175. }
  176. static struct file_operations mixcomwd_fops=
  177. {
  178. owner: THIS_MODULE,
  179. write: mixcomwd_write,
  180. ioctl: mixcomwd_ioctl,
  181. open: mixcomwd_open,
  182. release: mixcomwd_release,
  183. };
  184. static struct miscdevice mixcomwd_miscdev=
  185. {
  186. WATCHDOG_MINOR,
  187. "watchdog",
  188. &mixcomwd_fops
  189. };
  190. static int __init mixcomwd_checkcard(int port)
  191. {
  192. int id;
  193. if(check_region(port+MIXCOM_WATCHDOG_OFFSET,1)) {
  194. return 0;
  195. }
  196. id=inb_p(port + MIXCOM_WATCHDOG_OFFSET) & 0x3f;
  197. if(id!=MIXCOM_ID) {
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. static int __init flashcom_checkcard(int port)
  203. {
  204. int id;
  205. if(check_region(port + FLASHCOM_WATCHDOG_OFFSET,1)) {
  206. return 0;
  207. }
  208. id=inb_p(port + FLASHCOM_WATCHDOG_OFFSET);
  209.   if(id!=FLASHCOM_ID) {
  210. return 0;
  211. }
  212.   return 1;
  213.  }
  214.  
  215. static int __init mixcomwd_init(void)
  216. {
  217. int i;
  218. int ret;
  219. int found=0;
  220. for (i = 0; !found && mixcomwd_ioports[i] != 0; i++) {
  221. if (mixcomwd_checkcard(mixcomwd_ioports[i])) {
  222. found = 1;
  223. watchdog_port = mixcomwd_ioports[i] + MIXCOM_WATCHDOG_OFFSET;
  224. }
  225. }
  226. /* The FlashCOM card can be set up at 0x300 -> 0x378, in 0x8 jumps */
  227. for (i = 0x300; !found && i < 0x380; i+=0x8) {
  228. if (flashcom_checkcard(i)) {
  229. found = 1;
  230. watchdog_port = i + FLASHCOM_WATCHDOG_OFFSET;
  231. }
  232. }
  233. if (!found) {
  234. printk("mixcomwd: No card detected, or port not available.n");
  235. return -ENODEV;
  236. }
  237. request_region(watchdog_port,1,"MixCOM watchdog");
  238. ret = misc_register(&mixcomwd_miscdev);
  239. if (ret)
  240. return ret;
  241. printk(KERN_INFO "MixCOM watchdog driver v%s, watchdog port at 0x%3xn",VERSION,watchdog_port);
  242. return 0;
  243. }
  244. static void __exit mixcomwd_exit(void)
  245. {
  246. if (!nowayout) {
  247. if(mixcomwd_timer_alive) {
  248. printk(KERN_WARNING "mixcomwd: I quit now, hardware will"
  249. " probably reboot!n");
  250. del_timer(&mixcomwd_timer);
  251. mixcomwd_timer_alive=0;
  252. }
  253. }
  254. release_region(watchdog_port,1);
  255. misc_deregister(&mixcomwd_miscdev);
  256. }
  257. module_init(mixcomwd_init);
  258. module_exit(mixcomwd_exit);
  259. MODULE_LICENSE("GPL");
  260. EXPORT_NO_SYMBOLS;