Import 2.3.41pre2
[davej-history.git] / drivers / char / mixcomwd.c
blob1bb5a5b7f3e2532ef0030f81282eea493866567b
1 /*
2 * MixCom Watchdog: A Simple Hardware Watchdog Device
3 * Based on Softdog driver by Alan Cox and PC Watchdog driver by Ken Hollis
5 * Author: Gergely Madarasz <gorgo@itc.hu>
7 * Copyright (c) 1999 ITConsult-Pro Co. <info@itc.hu>
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.
14 * Version 0.1 (99/04/15):
15 * - first version
17 * Version 0.2 (99/06/16):
18 * - added kernel timer watchdog ping after close
19 * since the hardware does not support watchdog shutdown
21 * Version 0.3 (99/06/21):
22 * - added WDIOC_GETSTATUS and WDIOC_GETSUPPORT ioctl calls
24 * Version 0.3.1 (99/06/22):
25 * - allow module removal while internal timer is active,
26 * print warning about probable reset
30 #define VERSION "0.3.1"
32 #include <linux/module.h>
33 #include <linux/config.h>
34 #include <linux/types.h>
35 #include <linux/kernel.h>
36 #include <linux/fs.h>
37 #include <linux/mm.h>
38 #include <linux/miscdevice.h>
39 #include <linux/ioport.h>
40 #include <linux/watchdog.h>
41 #include <linux/reboot.h>
42 #include <linux/init.h>
43 #include <asm/uaccess.h>
44 #include <asm/io.h>
46 static int mixcomwd_ioports[] = { 0x180, 0x280, 0x380, 0x000 };
48 #define MIXCOM_WATCHDOG_OFFSET 0xc10
49 #define MIXCOM_ID1 0x11
50 #define MIXCOM_ID2 0x13
52 static int mixcomwd_opened;
53 static int mixcomwd_port;
55 #ifndef CONFIG_WATCHDOG_NOWAYOUT
56 static int mixcomwd_timer_alive;
57 static struct timer_list mixcomwd_timer;
58 #endif
60 static void mixcomwd_ping(void)
62 outb_p(55,mixcomwd_port+MIXCOM_WATCHDOG_OFFSET);
63 return;
66 #ifndef CONFIG_WATCHDOG_NOWAYOUT
67 static void mixcomwd_timerfun(unsigned long d)
69 mixcomwd_ping();
71 mod_timer(&mixcomwd_timer,jiffies+ 5*HZ);
73 #endif
76 * Allow only one person to hold it open
79 static int mixcomwd_open(struct inode *inode, struct file *file)
81 if(test_and_set_bit(0,&mixcomwd_opened)) {
82 return -EBUSY;
84 mixcomwd_ping();
86 #ifndef CONFIG_WATCHDOG_NOWAYOUT
87 if(mixcomwd_timer_alive) {
88 del_timer(&mixcomwd_timer);
89 mixcomwd_timer_alive=0;
91 #endif
92 MOD_INC_USE_COUNT;
94 return 0;
97 static int mixcomwd_release(struct inode *inode, struct file *file)
100 #ifndef CONFIG_WATCHDOG_NOWAYOUT
101 if(mixcomwd_timer_alive) {
102 printk(KERN_ERR "mixcomwd: release called while internal timer alive");
103 return -EBUSY;
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 #endif
112 MOD_DEC_USE_COUNT;
114 clear_bit(0,&mixcomwd_opened);
115 return 0;
119 static ssize_t mixcomwd_write(struct file *file, const char *data, size_t len, loff_t *ppos)
121 if (ppos != &file->f_pos) {
122 return -ESPIPE;
125 if(len)
127 mixcomwd_ping();
128 return 1;
130 return 0;
133 static int mixcomwd_ioctl(struct inode *inode, struct file *file,
134 unsigned int cmd, unsigned long arg)
136 int status;
137 static struct watchdog_info ident = {
138 WDIOF_KEEPALIVEPING, 1, "MixCOM watchdog"
141 switch(cmd)
143 case WDIOC_GETSTATUS:
144 status=mixcomwd_opened;
145 #ifndef CONFIG_WATCHDOG_NOWAYOUT
146 status|=mixcomwd_timer_alive;
147 #endif
148 if (copy_to_user((int *)arg, &status, sizeof(int))) {
149 return -EFAULT;
151 break;
152 case WDIOC_GETSUPPORT:
153 if (copy_to_user((struct watchdog_info *)arg, &ident,
154 sizeof(ident))) {
155 return -EFAULT;
157 break;
158 case WDIOC_KEEPALIVE:
159 mixcomwd_ping();
160 break;
161 default:
162 return -ENOIOCTLCMD;
164 return 0;
167 static struct file_operations mixcomwd_fops=
169 NULL, /* Seek */
170 NULL, /* Read */
171 mixcomwd_write, /* Write */
172 NULL, /* Readdir */
173 NULL, /* Select */
174 mixcomwd_ioctl, /* Ioctl */
175 NULL, /* MMap */
176 mixcomwd_open,
177 NULL, /* flush */
178 mixcomwd_release,
179 NULL,
180 NULL /* Fasync */
183 static struct miscdevice mixcomwd_miscdev=
185 WATCHDOG_MINOR,
186 "watchdog",
187 &mixcomwd_fops
190 static int __init mixcomwd_checkcard(int port)
192 int id;
194 if(check_region(port,1)) {
195 return 0;
198 id=inb_p(port + MIXCOM_WATCHDOG_OFFSET) & 0x3f;
199 if(id!=MIXCOM_ID1 && id!=MIXCOM_ID2) {
200 return 0;
202 return 1;
206 void __init mixcomwd_init(void)
208 int i;
209 int found=0;
211 for (i = 0; mixcomwd_ioports[i] != 0; i++) {
212 if (mixcomwd_checkcard(mixcomwd_ioports[i])) {
213 found = 1;
214 mixcomwd_port = mixcomwd_ioports[i];
215 break;
219 if (!found) {
220 printk("mixcomwd: No card detected, or port not available.\n");
221 return;
224 request_region(mixcomwd_port+MIXCOM_WATCHDOG_OFFSET,1,"MixCOM watchdog");
226 misc_register(&mixcomwd_miscdev);
227 printk("MixCOM watchdog driver v%s, MixCOM card at 0x%3x\n",VERSION,mixcomwd_port);
230 #ifdef MODULE
231 int init_module(void)
233 mixcomwd_init();
234 return 0;
237 void cleanup_module(void)
239 #ifndef CONFIG_WATCHDOG_NOWAYOUT
240 if(mixcomwd_timer_alive) {
241 printk(KERN_WARNING "mixcomwd: I quit now, hardware will"
242 " probably reboot!\n");
243 del_timer(&mixcomwd_timer);
244 mixcomwd_timer_alive=0;
246 #endif
247 release_region(mixcomwd_port+MIXCOM_WATCHDOG_OFFSET,1);
248 misc_deregister(&mixcomwd_miscdev);
250 #endif