Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / char / watchdog / cpu5wdt.c
blob00bdabb90f27b958a76ab7b8d39c4b505892411e
1 /*
2 * sma cpu5 watchdog driver
4 * Copyright (C) 2003 Heiko Ronsdorf <hero@ihg.uni-duisburg.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/miscdevice.h>
27 #include <linux/fs.h>
28 #include <linux/init.h>
29 #include <linux/ioport.h>
30 #include <linux/timer.h>
31 #include <linux/completion.h>
32 #include <linux/jiffies.h>
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
36 #include <linux/watchdog.h>
38 /* adjustable parameters */
40 static int verbose = 0;
41 static int port = 0x91;
42 static int ticks = 10000;
44 #define PFX "cpu5wdt: "
46 #define CPU5WDT_EXTENT 0x0A
48 #define CPU5WDT_STATUS_REG 0x00
49 #define CPU5WDT_TIME_A_REG 0x02
50 #define CPU5WDT_TIME_B_REG 0x03
51 #define CPU5WDT_MODE_REG 0x04
52 #define CPU5WDT_TRIGGER_REG 0x07
53 #define CPU5WDT_ENABLE_REG 0x08
54 #define CPU5WDT_RESET_REG 0x09
56 #define CPU5WDT_INTERVAL (HZ/10+1)
58 /* some device data */
60 static struct {
61 struct completion stop;
62 volatile int running;
63 struct timer_list timer;
64 volatile int queue;
65 int default_ticks;
66 unsigned long inuse;
67 } cpu5wdt_device;
69 /* generic helper functions */
71 static void cpu5wdt_trigger(unsigned long unused)
73 if ( verbose > 2 )
74 printk(KERN_DEBUG PFX "trigger at %i ticks\n", ticks);
76 if( cpu5wdt_device.running )
77 ticks--;
79 /* keep watchdog alive */
80 outb(1, port + CPU5WDT_TRIGGER_REG);
82 /* requeue?? */
83 if( cpu5wdt_device.queue && ticks ) {
84 cpu5wdt_device.timer.expires = jiffies + CPU5WDT_INTERVAL;
85 add_timer(&cpu5wdt_device.timer);
87 else {
88 /* ticks doesn't matter anyway */
89 complete(&cpu5wdt_device.stop);
94 static void cpu5wdt_reset(void)
96 ticks = cpu5wdt_device.default_ticks;
98 if ( verbose )
99 printk(KERN_DEBUG PFX "reset (%i ticks)\n", (int) ticks);
103 static void cpu5wdt_start(void)
105 if ( !cpu5wdt_device.queue ) {
106 cpu5wdt_device.queue = 1;
107 outb(0, port + CPU5WDT_TIME_A_REG);
108 outb(0, port + CPU5WDT_TIME_B_REG);
109 outb(1, port + CPU5WDT_MODE_REG);
110 outb(0, port + CPU5WDT_RESET_REG);
111 outb(0, port + CPU5WDT_ENABLE_REG);
112 cpu5wdt_device.timer.expires = jiffies + CPU5WDT_INTERVAL;
113 add_timer(&cpu5wdt_device.timer);
115 /* if process dies, counter is not decremented */
116 cpu5wdt_device.running++;
119 static int cpu5wdt_stop(void)
121 if ( cpu5wdt_device.running )
122 cpu5wdt_device.running = 0;
124 ticks = cpu5wdt_device.default_ticks;
126 if ( verbose )
127 printk(KERN_CRIT PFX "stop not possible\n");
129 return -EIO;
132 /* filesystem operations */
134 static int cpu5wdt_open(struct inode *inode, struct file *file)
136 if ( test_and_set_bit(0, &cpu5wdt_device.inuse) )
137 return -EBUSY;
139 return nonseekable_open(inode, file);
142 static int cpu5wdt_release(struct inode *inode, struct file *file)
144 clear_bit(0, &cpu5wdt_device.inuse);
145 return 0;
148 static int cpu5wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
150 void __user *argp = (void __user *)arg;
151 unsigned int value;
152 static struct watchdog_info ident =
154 .options = WDIOF_CARDRESET,
155 .identity = "CPU5 WDT",
158 switch(cmd) {
159 case WDIOC_KEEPALIVE:
160 cpu5wdt_reset();
161 break;
162 case WDIOC_GETSTATUS:
163 value = inb(port + CPU5WDT_STATUS_REG);
164 value = (value >> 2) & 1;
165 if ( copy_to_user(argp, &value, sizeof(int)) )
166 return -EFAULT;
167 break;
168 case WDIOC_GETSUPPORT:
169 if ( copy_to_user(argp, &ident, sizeof(ident)) )
170 return -EFAULT;
171 break;
172 case WDIOC_SETOPTIONS:
173 if ( copy_from_user(&value, argp, sizeof(int)) )
174 return -EFAULT;
175 switch(value) {
176 case WDIOS_ENABLECARD:
177 cpu5wdt_start();
178 break;
179 case WDIOS_DISABLECARD:
180 return cpu5wdt_stop();
181 default:
182 return -EINVAL;
184 break;
185 default:
186 return -ENOTTY;
188 return 0;
191 static ssize_t cpu5wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
193 if ( !count )
194 return -EIO;
196 cpu5wdt_reset();
198 return count;
201 static const struct file_operations cpu5wdt_fops = {
202 .owner = THIS_MODULE,
203 .llseek = no_llseek,
204 .ioctl = cpu5wdt_ioctl,
205 .open = cpu5wdt_open,
206 .write = cpu5wdt_write,
207 .release = cpu5wdt_release,
210 static struct miscdevice cpu5wdt_misc = {
211 .minor = WATCHDOG_MINOR,
212 .name = "watchdog",
213 .fops = &cpu5wdt_fops,
216 /* init/exit function */
218 static int __devinit cpu5wdt_init(void)
220 unsigned int val;
221 int err;
223 if ( verbose )
224 printk(KERN_DEBUG PFX "port=0x%x, verbose=%i\n", port, verbose);
226 if ( (err = misc_register(&cpu5wdt_misc)) < 0 ) {
227 printk(KERN_ERR PFX "misc_register failed\n");
228 goto no_misc;
231 if ( !request_region(port, CPU5WDT_EXTENT, PFX) ) {
232 printk(KERN_ERR PFX "request_region failed\n");
233 err = -EBUSY;
234 goto no_port;
237 /* watchdog reboot? */
238 val = inb(port + CPU5WDT_STATUS_REG);
239 val = (val >> 2) & 1;
240 if ( !val )
241 printk(KERN_INFO PFX "sorry, was my fault\n");
243 init_completion(&cpu5wdt_device.stop);
244 cpu5wdt_device.queue = 0;
246 clear_bit(0, &cpu5wdt_device.inuse);
248 init_timer(&cpu5wdt_device.timer);
249 cpu5wdt_device.timer.function = cpu5wdt_trigger;
250 cpu5wdt_device.timer.data = 0;
252 cpu5wdt_device.default_ticks = ticks;
254 printk(KERN_INFO PFX "init success\n");
256 return 0;
258 no_port:
259 misc_deregister(&cpu5wdt_misc);
260 no_misc:
261 return err;
264 static int __devinit cpu5wdt_init_module(void)
266 return cpu5wdt_init();
269 static void __devexit cpu5wdt_exit(void)
271 if ( cpu5wdt_device.queue ) {
272 cpu5wdt_device.queue = 0;
273 wait_for_completion(&cpu5wdt_device.stop);
276 misc_deregister(&cpu5wdt_misc);
278 release_region(port, CPU5WDT_EXTENT);
282 static void __devexit cpu5wdt_exit_module(void)
284 cpu5wdt_exit();
287 /* module entry points */
289 module_init(cpu5wdt_init_module);
290 module_exit(cpu5wdt_exit_module);
292 MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
293 MODULE_DESCRIPTION("sma cpu5 watchdog driver");
294 MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
298 module_param(port, int, 0);
299 MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
301 module_param(verbose, int, 0);
302 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
304 module_param(ticks, int, 0);
305 MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");