- Kai Germaschewski: ymfpci cleanups and resource leak fixes
[davej-history.git] / drivers / char / softdog.c
blobe3f68ba30631f80eed41fa50ba35e888bc89ede8
1 /*
2 * SoftDog 0.05: A Software Watchdog Device
4 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
5 * http://www.redhat.com
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
13 * warranty for any of this software. This material is provided
14 * "AS-IS" and at no charge.
16 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18 * Software only watchdog driver. Unlike its big brother the WDT501P
19 * driver this won't always recover a failed machine.
21 * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
22 * Modularised.
23 * Added soft_margin; use upon insmod to change the timer delay.
24 * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
25 * minors.
27 * 19980911 Alan Cox
28 * Made SMP safe for 2.3.x
31 #include <linux/module.h>
32 #include <linux/config.h>
33 #include <linux/types.h>
34 #include <linux/kernel.h>
35 #include <linux/fs.h>
36 #include <linux/mm.h>
37 #include <linux/miscdevice.h>
38 #include <linux/watchdog.h>
39 #include <linux/reboot.h>
40 #include <linux/smp_lock.h>
41 #include <linux/init.h>
42 #include <asm/uaccess.h>
44 #define TIMER_MARGIN 60 /* (secs) Default is 1 minute */
46 static int soft_margin = TIMER_MARGIN; /* in seconds */
48 #ifdef MODULE
49 MODULE_PARM(soft_margin,"i");
50 #endif
53 * Our timer
56 static void watchdog_fire(unsigned long);
58 static struct timer_list watchdog_ticktock = {
59 function: watchdog_fire,
61 static int timer_alive;
65 * If the timer expires..
68 static void watchdog_fire(unsigned long data)
70 #ifdef ONLY_TESTING
71 printk(KERN_CRIT "SOFTDOG: Would Reboot.\n");
72 #else
73 printk(KERN_CRIT "SOFTDOG: Initiating system reboot.\n");
74 machine_restart(NULL);
75 printk("WATCHDOG: Reboot didn't ?????\n");
76 #endif
80 * Allow only one person to hold it open
83 static int softdog_open(struct inode *inode, struct file *file)
85 if(timer_alive)
86 return -EBUSY;
87 #ifdef CONFIG_WATCHDOG_NOWAYOUT
88 MOD_INC_USE_COUNT;
89 #endif
91 * Activate timer
93 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
94 timer_alive=1;
95 return 0;
98 static int softdog_release(struct inode *inode, struct file *file)
101 * Shut off the timer.
102 * Lock it in if it's a module and we defined ...NOWAYOUT
104 lock_kernel();
105 #ifndef CONFIG_WATCHDOG_NOWAYOUT
106 del_timer(&watchdog_ticktock);
107 #endif
108 timer_alive=0;
109 unlock_kernel();
110 return 0;
113 static ssize_t softdog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
115 /* Can't seek (pwrite) on this device */
116 if (ppos != &file->f_pos)
117 return -ESPIPE;
120 * Refresh the timer.
122 if(len) {
123 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
124 return 1;
126 return 0;
129 static int softdog_ioctl(struct inode *inode, struct file *file,
130 unsigned int cmd, unsigned long arg)
132 static struct watchdog_info ident=
136 "Software Watchdog"
138 switch(cmd)
140 default:
141 return -ENOIOCTLCMD;
142 case WDIOC_GETSUPPORT:
143 if(copy_to_user((struct watchdog_info *)arg, &ident, sizeof(ident)))
144 return -EFAULT;
145 return 0;
146 case WDIOC_GETSTATUS:
147 case WDIOC_GETBOOTSTATUS:
148 return put_user(0,(int *)arg);
149 case WDIOC_KEEPALIVE:
150 mod_timer(&watchdog_ticktock, jiffies+(soft_margin*HZ));
151 return 0;
155 static struct file_operations softdog_fops=
157 owner: THIS_MODULE,
158 write: softdog_write,
159 ioctl: softdog_ioctl,
160 open: softdog_open,
161 release: softdog_release,
164 static struct miscdevice softdog_miscdev=
166 WATCHDOG_MINOR,
167 "watchdog",
168 &softdog_fops
171 static int __init watchdog_init(void)
173 int ret;
175 ret = misc_register(&softdog_miscdev);
177 if (ret)
178 return ret;
180 printk("Software Watchdog Timer: 0.05, timer margin: %d sec\n", soft_margin);
182 return 0;
185 static void __exit watchdog_exit(void)
187 misc_deregister(&softdog_miscdev);
190 module_init(watchdog_init);
191 module_exit(watchdog_exit);