Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / watchdog / cpu5wdt.c
blobc39206a45238a718f19eb274b01fddc697577aeb
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 <<<<<<< HEAD:drivers/watchdog/cpu5wdt.c
63 volatile int running;
64 =======
65 int running;
66 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/cpu5wdt.c
67 struct timer_list timer;
68 <<<<<<< HEAD:drivers/watchdog/cpu5wdt.c
69 volatile int queue;
70 =======
71 int queue;
72 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/cpu5wdt.c
73 int default_ticks;
74 unsigned long inuse;
75 } cpu5wdt_device;
77 /* generic helper functions */
79 static void cpu5wdt_trigger(unsigned long unused)
81 if ( verbose > 2 )
82 printk(KERN_DEBUG PFX "trigger at %i ticks\n", ticks);
84 if( cpu5wdt_device.running )
85 ticks--;
87 /* keep watchdog alive */
88 outb(1, port + CPU5WDT_TRIGGER_REG);
90 /* requeue?? */
91 if (cpu5wdt_device.queue && ticks)
92 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
93 else {
94 /* ticks doesn't matter anyway */
95 complete(&cpu5wdt_device.stop);
100 static void cpu5wdt_reset(void)
102 ticks = cpu5wdt_device.default_ticks;
104 if ( verbose )
105 printk(KERN_DEBUG PFX "reset (%i ticks)\n", (int) ticks);
109 static void cpu5wdt_start(void)
111 if ( !cpu5wdt_device.queue ) {
112 cpu5wdt_device.queue = 1;
113 outb(0, port + CPU5WDT_TIME_A_REG);
114 outb(0, port + CPU5WDT_TIME_B_REG);
115 outb(1, port + CPU5WDT_MODE_REG);
116 outb(0, port + CPU5WDT_RESET_REG);
117 outb(0, port + CPU5WDT_ENABLE_REG);
118 mod_timer(&cpu5wdt_device.timer, jiffies + CPU5WDT_INTERVAL);
120 /* if process dies, counter is not decremented */
121 cpu5wdt_device.running++;
124 static int cpu5wdt_stop(void)
126 if ( cpu5wdt_device.running )
127 cpu5wdt_device.running = 0;
129 ticks = cpu5wdt_device.default_ticks;
131 if ( verbose )
132 printk(KERN_CRIT PFX "stop not possible\n");
134 return -EIO;
137 /* filesystem operations */
139 static int cpu5wdt_open(struct inode *inode, struct file *file)
141 if ( test_and_set_bit(0, &cpu5wdt_device.inuse) )
142 return -EBUSY;
144 return nonseekable_open(inode, file);
147 static int cpu5wdt_release(struct inode *inode, struct file *file)
149 clear_bit(0, &cpu5wdt_device.inuse);
150 return 0;
153 static int cpu5wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
155 void __user *argp = (void __user *)arg;
156 unsigned int value;
157 static struct watchdog_info ident =
159 .options = WDIOF_CARDRESET,
160 .identity = "CPU5 WDT",
163 switch(cmd) {
164 case WDIOC_KEEPALIVE:
165 cpu5wdt_reset();
166 break;
167 case WDIOC_GETSTATUS:
168 value = inb(port + CPU5WDT_STATUS_REG);
169 value = (value >> 2) & 1;
170 if ( copy_to_user(argp, &value, sizeof(int)) )
171 return -EFAULT;
172 break;
173 case WDIOC_GETBOOTSTATUS:
174 if ( copy_to_user(argp, &value, sizeof(int)) )
175 return -EFAULT;
176 break;
177 case WDIOC_GETSUPPORT:
178 if ( copy_to_user(argp, &ident, sizeof(ident)) )
179 return -EFAULT;
180 break;
181 case WDIOC_SETOPTIONS:
182 if ( copy_from_user(&value, argp, sizeof(int)) )
183 return -EFAULT;
184 switch(value) {
185 case WDIOS_ENABLECARD:
186 cpu5wdt_start();
187 break;
188 case WDIOS_DISABLECARD:
189 return cpu5wdt_stop();
190 default:
191 return -EINVAL;
193 break;
194 default:
195 return -ENOTTY;
197 return 0;
200 static ssize_t cpu5wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
202 if ( !count )
203 return -EIO;
205 cpu5wdt_reset();
207 return count;
210 static const struct file_operations cpu5wdt_fops = {
211 .owner = THIS_MODULE,
212 .llseek = no_llseek,
213 .ioctl = cpu5wdt_ioctl,
214 .open = cpu5wdt_open,
215 .write = cpu5wdt_write,
216 .release = cpu5wdt_release,
219 static struct miscdevice cpu5wdt_misc = {
220 .minor = WATCHDOG_MINOR,
221 .name = "watchdog",
222 .fops = &cpu5wdt_fops,
225 /* init/exit function */
227 static int __devinit cpu5wdt_init(void)
229 unsigned int val;
230 int err;
232 if ( verbose )
233 printk(KERN_DEBUG PFX "port=0x%x, verbose=%i\n", port, verbose);
235 if ( !request_region(port, CPU5WDT_EXTENT, PFX) ) {
236 printk(KERN_ERR PFX "request_region failed\n");
237 err = -EBUSY;
238 goto no_port;
241 if ( (err = misc_register(&cpu5wdt_misc)) < 0 ) {
242 printk(KERN_ERR PFX "misc_register failed\n");
243 goto no_misc;
246 /* watchdog reboot? */
247 val = inb(port + CPU5WDT_STATUS_REG);
248 val = (val >> 2) & 1;
249 if ( !val )
250 printk(KERN_INFO PFX "sorry, was my fault\n");
252 init_completion(&cpu5wdt_device.stop);
253 cpu5wdt_device.queue = 0;
255 clear_bit(0, &cpu5wdt_device.inuse);
257 setup_timer(&cpu5wdt_device.timer, cpu5wdt_trigger, 0);
259 cpu5wdt_device.default_ticks = ticks;
261 printk(KERN_INFO PFX "init success\n");
263 return 0;
265 no_misc:
266 release_region(port, CPU5WDT_EXTENT);
267 no_port:
268 return err;
271 static int __devinit cpu5wdt_init_module(void)
273 return cpu5wdt_init();
276 static void __devexit cpu5wdt_exit(void)
278 if ( cpu5wdt_device.queue ) {
279 cpu5wdt_device.queue = 0;
280 wait_for_completion(&cpu5wdt_device.stop);
283 misc_deregister(&cpu5wdt_misc);
285 release_region(port, CPU5WDT_EXTENT);
289 static void __devexit cpu5wdt_exit_module(void)
291 cpu5wdt_exit();
294 /* module entry points */
296 module_init(cpu5wdt_init_module);
297 module_exit(cpu5wdt_exit_module);
299 MODULE_AUTHOR("Heiko Ronsdorf <hero@ihg.uni-duisburg.de>");
300 MODULE_DESCRIPTION("sma cpu5 watchdog driver");
301 MODULE_SUPPORTED_DEVICE("sma cpu5 watchdog");
302 MODULE_LICENSE("GPL");
303 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
305 module_param(port, int, 0);
306 MODULE_PARM_DESC(port, "base address of watchdog card, default is 0x91");
308 module_param(verbose, int, 0);
309 MODULE_PARM_DESC(verbose, "be verbose, default is 0 (no)");
311 module_param(ticks, int, 0);
312 MODULE_PARM_DESC(ticks, "count down ticks, default is 10000");