- Andries Brouwer: final isofs pieces.
[davej-history.git] / drivers / char / mixcomwd.c
blob8b156f69c83ab203f6dde939c6cb430c47eeeab0
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
28 * Version 0.4 (99/11/15):
29 * - support for one more type board
33 #define VERSION "0.4"
35 #include <linux/module.h>
36 #include <linux/config.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/fs.h>
40 #include <linux/mm.h>
41 #include <linux/miscdevice.h>
42 #include <linux/ioport.h>
43 #include <linux/watchdog.h>
44 #include <linux/reboot.h>
45 #include <linux/init.h>
46 #include <linux/smp_lock.h>
47 #include <asm/uaccess.h>
48 #include <asm/io.h>
50 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
57 static long mixcomwd_opened; /* long req'd for setbit --RR */
59 static int watchdog_port;
61 #ifndef CONFIG_WATCHDOG_NOWAYOUT
62 static int mixcomwd_timer_alive;
63 static struct timer_list mixcomwd_timer;
64 #endif
66 static void mixcomwd_ping(void)
68 outb_p(55,watchdog_port);
69 return;
72 #ifndef CONFIG_WATCHDOG_NOWAYOUT
73 static void mixcomwd_timerfun(unsigned long d)
75 mixcomwd_ping();
77 mod_timer(&mixcomwd_timer,jiffies+ 5*HZ);
79 #endif
82 * Allow only one person to hold it open
85 static int mixcomwd_open(struct inode *inode, struct file *file)
87 if(test_and_set_bit(0,&mixcomwd_opened)) {
88 return -EBUSY;
90 mixcomwd_ping();
92 #ifndef CONFIG_WATCHDOG_NOWAYOUT
93 if(mixcomwd_timer_alive) {
94 del_timer(&mixcomwd_timer);
95 mixcomwd_timer_alive=0;
97 #endif
98 return 0;
101 static int mixcomwd_release(struct inode *inode, struct file *file)
104 lock_kernel();
105 #ifndef CONFIG_WATCHDOG_NOWAYOUT
106 if(mixcomwd_timer_alive) {
107 printk(KERN_ERR "mixcomwd: release called while internal timer alive");
108 unlock_kernel();
109 return -EBUSY;
111 init_timer(&mixcomwd_timer);
112 mixcomwd_timer.expires=jiffies + 5 * HZ;
113 mixcomwd_timer.function=mixcomwd_timerfun;
114 mixcomwd_timer.data=0;
115 mixcomwd_timer_alive=1;
116 add_timer(&mixcomwd_timer);
117 #endif
119 clear_bit(0,&mixcomwd_opened);
120 unlock_kernel();
121 return 0;
125 static ssize_t mixcomwd_write(struct file *file, const char *data, size_t len, loff_t *ppos)
127 if (ppos != &file->f_pos) {
128 return -ESPIPE;
131 if(len)
133 mixcomwd_ping();
134 return 1;
136 return 0;
139 static int mixcomwd_ioctl(struct inode *inode, struct file *file,
140 unsigned int cmd, unsigned long arg)
142 int status;
143 static struct watchdog_info ident = {
144 WDIOF_KEEPALIVEPING, 1, "MixCOM watchdog"
147 switch(cmd)
149 case WDIOC_GETSTATUS:
150 status=mixcomwd_opened;
151 #ifndef CONFIG_WATCHDOG_NOWAYOUT
152 status|=mixcomwd_timer_alive;
153 #endif
154 if (copy_to_user((int *)arg, &status, sizeof(int))) {
155 return -EFAULT;
157 break;
158 case WDIOC_GETSUPPORT:
159 if (copy_to_user((struct watchdog_info *)arg, &ident,
160 sizeof(ident))) {
161 return -EFAULT;
163 break;
164 case WDIOC_KEEPALIVE:
165 mixcomwd_ping();
166 break;
167 default:
168 return -ENOIOCTLCMD;
170 return 0;
173 static struct file_operations mixcomwd_fops=
175 owner: THIS_MODULE,
176 write: mixcomwd_write,
177 ioctl: mixcomwd_ioctl,
178 open: mixcomwd_open,
179 release: mixcomwd_release,
182 static struct miscdevice mixcomwd_miscdev=
184 WATCHDOG_MINOR,
185 "watchdog",
186 &mixcomwd_fops
189 static int __init mixcomwd_checkcard(int port)
191 int id;
193 if(check_region(port+MIXCOM_WATCHDOG_OFFSET,1)) {
194 return 0;
197 id=inb_p(port + MIXCOM_WATCHDOG_OFFSET) & 0x3f;
198 if(id!=MIXCOM_ID) {
199 return 0;
201 return 1;
204 static int __init flashcom_checkcard(int port)
206 int id;
208 if(check_region(port + FLASHCOM_WATCHDOG_OFFSET,1)) {
209 return 0;
212 id=inb_p(port + FLASHCOM_WATCHDOG_OFFSET);
213 if(id!=FLASHCOM_ID) {
214 return 0;
216 return 1;
219 static int __init mixcomwd_init(void)
221 int i;
222 int ret;
223 int found=0;
225 for (i = 0; !found && mixcomwd_ioports[i] != 0; i++) {
226 if (mixcomwd_checkcard(mixcomwd_ioports[i])) {
227 found = 1;
228 watchdog_port = mixcomwd_ioports[i] + MIXCOM_WATCHDOG_OFFSET;
232 /* The FlashCOM card can be set up at 0x300 -> 0x378, in 0x8 jumps */
233 for (i = 0x300; !found && i < 0x380; i+=0x8) {
234 if (flashcom_checkcard(i)) {
235 found = 1;
236 watchdog_port = i + FLASHCOM_WATCHDOG_OFFSET;
240 if (!found) {
241 printk("mixcomwd: No card detected, or port not available.\n");
242 return -ENODEV;
245 request_region(watchdog_port,1,"MixCOM watchdog");
247 ret = misc_register(&mixcomwd_miscdev);
248 if (ret)
249 return ret;
251 printk(KERN_INFO "MixCOM watchdog driver v%s, watchdog port at 0x%3x\n",VERSION,watchdog_port);
253 return 0;
256 static void __exit mixcomwd_exit(void)
258 #ifndef CONFIG_WATCHDOG_NOWAYOUT
259 if(mixcomwd_timer_alive) {
260 printk(KERN_WARNING "mixcomwd: I quit now, hardware will"
261 " probably reboot!\n");
262 del_timer(&mixcomwd_timer);
263 mixcomwd_timer_alive=0;
265 #endif
266 release_region(watchdog_port,1);
267 misc_deregister(&mixcomwd_miscdev);
270 module_init(mixcomwd_init);
271 module_exit(mixcomwd_exit);