Fedora Core 2 - 1.492
[linux-2.6.git] / drivers / char / watchdog / indydog.c
1 /*
2  *      IndyDog 0.3     A Hardware Watchdog Device for SGI IP22
3  *
4  *      (c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>, All Rights Reserved.
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  *
11  *      based on softdog.c by Alan Cox <alan@redhat.com>
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/config.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/fs.h>
20 #include <linux/mm.h>
21 #include <linux/miscdevice.h>
22 #include <linux/watchdog.h>
23 #include <linux/notifier.h>
24 #include <linux/reboot.h>
25 #include <linux/init.h>
26 #include <asm/uaccess.h>
27 #include <asm/sgi/mc.h>
28
29 #define PFX "indydog: "
30 static int indydog_alive;
31
32 #ifdef CONFIG_WATCHDOG_NOWAYOUT
33 static int nowayout = 1;
34 #else
35 static int nowayout = 0;
36 #endif
37
38 #define WATCHDOG_TIMEOUT 30             /* 30 sec default timeout */
39
40 module_param(nowayout, int, 0);
41 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
42
43 static void indydog_start(void)
44 {
45         u32 mc_ctrl0 = sgimc->cpuctrl0;
46
47         mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
48         sgimc->cpuctrl0 = mc_ctrl0;
49 }
50
51 static void indydog_stop(void)
52 {
53         u32 mc_ctrl0 = sgimc->cpuctrl0;
54
55         mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
56         sgimc->cpuctrl0 = mc_ctrl0;
57
58         printk(KERN_INFO PFX "Stopped watchdog timer.\n");
59 }
60
61 static void indydog_ping(void)
62 {
63         sgimc->watchdogt = 0;
64 }
65
66 /*
67  *      Allow only one person to hold it open
68  */
69 static int indydog_open(struct inode *inode, struct file *file)
70 {
71         if (indydog_alive)
72                 return -EBUSY;
73
74         if (nowayout)
75                 __module_get(THIS_MODULE);
76
77         /* Activate timer */
78         indydog_start();
79         indydog_ping();
80
81         indydog_alive = 1;
82         printk(KERN_INFO "Started watchdog timer.\n");
83
84         return 0;
85 }
86
87 static int indydog_release(struct inode *inode, struct file *file)
88 {
89         /* Shut off the timer.
90          * Lock it in if it's a module and we defined ...NOWAYOUT */
91         if (!nowayout)
92                 indydog_stop();         /* Turn the WDT off */
93
94         indydog_alive = 0;
95
96         return 0;
97 }
98
99 static ssize_t indydog_write(struct file *file, const char *data, size_t len, loff_t *ppos)
100 {
101         /* Can't seek (pwrite) on this device */
102         if (ppos != &file->f_pos)
103                 return -ESPIPE;
104
105         /* Refresh the timer. */
106         if (len) {
107                 indydog_ping();
108         }
109         return len;
110 }
111
112 static int indydog_ioctl(struct inode *inode, struct file *file,
113         unsigned int cmd, unsigned long arg)
114 {
115         int options, retval = -EINVAL;
116         static struct watchdog_info ident = {
117                 .options                = WDIOF_KEEPALIVEPING |
118                                           WDIOF_MAGICCLOSE,
119                 .firmware_version       = 0,
120                 .identity               = "Hardware Watchdog for SGI IP22",
121         };
122
123         switch (cmd) {
124                 default:
125                         return -ENOIOCTLCMD;
126                 case WDIOC_GETSUPPORT:
127                         if (copy_to_user((struct watchdog_info *)arg,
128                                          &ident, sizeof(ident)))
129                                 return -EFAULT;
130                         return 0;
131                 case WDIOC_GETSTATUS:
132                 case WDIOC_GETBOOTSTATUS:
133                         return put_user(0,(int *)arg);
134                 case WDIOC_KEEPALIVE:
135                         indydog_ping();
136                         return 0;
137                 case WDIOC_GETTIMEOUT:
138                         return put_user(WATCHDOG_TIMEOUT,(int *)arg);
139                 case WDIOC_SETOPTIONS:
140                 {
141                         if (get_user(options, (int *)arg))
142                                 return -EFAULT;
143
144                         if (options & WDIOS_DISABLECARD) {
145                                 indydog_stop();
146                                 retval = 0;
147                         }
148
149                         if (options & WDIOS_ENABLECARD) {
150                                 indydog_start();
151                                 retval = 0;
152                         }
153
154                         return retval;
155                 }
156         }
157 }
158
159 static int indydog_notify_sys(struct notifier_block *this, unsigned long code, void *unused)
160 {
161         if (code == SYS_DOWN || code == SYS_HALT)
162                 indydog_stop();         /* Turn the WDT off */
163
164         return NOTIFY_DONE;
165 }
166
167 static struct file_operations indydog_fops = {
168         .owner          = THIS_MODULE,
169         .write          = indydog_write,
170         .ioctl          = indydog_ioctl,
171         .open           = indydog_open,
172         .release        = indydog_release,
173 };
174
175 static struct miscdevice indydog_miscdev = {
176         .minor          = WATCHDOG_MINOR,
177         .name           = "watchdog",
178         .fops           = &indydog_fops,
179 };
180
181 static struct notifier_block indydog_notifier = {
182         .notifier_call = indydog_notify_sys,
183 };
184
185 static char banner[] __initdata =
186         KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";
187
188 static int __init watchdog_init(void)
189 {
190         int ret;
191
192         ret = register_reboot_notifier(&indydog_notifier);
193         if (ret) {
194                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
195                         ret);
196                 return ret;
197         }
198
199         ret = misc_register(&indydog_miscdev);
200         if (ret) {
201                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
202                         WATCHDOG_MINOR, ret);
203                 unregister_reboot_notifier(&indydog_notifier);
204                 return ret;
205         }
206
207         printk(banner);
208
209         return 0;
210 }
211
212 static void __exit watchdog_exit(void)
213 {
214         misc_deregister(&indydog_miscdev);
215         unregister_reboot_notifier(&indydog_notifier);
216 }
217
218 module_init(watchdog_init);
219 module_exit(watchdog_exit);
220
221 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
222 MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
223 MODULE_LICENSE("GPL");
224 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);