blob: d18a974735e649ed25f95159ef19ce274d59d408 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* UML hardware watchdog, shamelessly stolen from:
2 *
3 * SoftDog 0.05: A Software Watchdog Device
4 *
5 * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
6 * http://www.redhat.com
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
14 * warranty for any of this software. This material is provided
15 * "AS-IS" and at no charge.
16 *
17 * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18 *
19 * Software only watchdog driver. Unlike its big brother the WDT501P
20 * driver this won't always recover a failed machine.
21 *
22 * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
23 * Modularised.
24 * Added soft_margin; use upon insmod to change the timer delay.
25 * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
26 * minors.
27 *
28 * 19980911 Alan Cox
29 * Made SMP safe for 2.3.x
30 *
31 * 20011127 Joel Becker (jlbec@evilplan.org>
32 * Added soft_noboot; Allows testing the softdog trigger without
33 * requiring a recompile.
34 * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
35 */
36
37#include <linux/module.h>
38#include <linux/config.h>
39#include <linux/types.h>
40#include <linux/kernel.h>
41#include <linux/fs.h>
42#include <linux/mm.h>
43#include <linux/miscdevice.h>
44#include <linux/watchdog.h>
45#include <linux/reboot.h>
46#include <linux/smp_lock.h>
47#include <linux/init.h>
48#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "mconsole.h"
50
51MODULE_LICENSE("GPL");
52
53/* Locked by the BKL in harddog_open and harddog_release */
54static int timer_alive;
55static int harddog_in_fd = -1;
56static int harddog_out_fd = -1;
57
58/*
59 * Allow only one person to hold it open
60 */
61
62extern int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock);
63
64static int harddog_open(struct inode *inode, struct file *file)
65{
66 int err;
67 char *sock = NULL;
68
69 lock_kernel();
70 if(timer_alive)
71 return -EBUSY;
72#ifdef CONFIG_HARDDOG_NOWAYOUT
73 __module_get(THIS_MODULE);
74#endif
75
76#ifdef CONFIG_MCONSOLE
77 sock = mconsole_notify_socket();
78#endif
79 err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
80 if(err) return(err);
81
82 timer_alive = 1;
83 unlock_kernel();
84 return nonseekable_open(inode, file);
85}
86
87extern void stop_watchdog(int in_fd, int out_fd);
88
89static int harddog_release(struct inode *inode, struct file *file)
90{
91 /*
92 * Shut off the timer.
93 */
94 lock_kernel();
95
96 stop_watchdog(harddog_in_fd, harddog_out_fd);
97 harddog_in_fd = -1;
98 harddog_out_fd = -1;
99
100 timer_alive=0;
101 unlock_kernel();
102 return 0;
103}
104
105extern int ping_watchdog(int fd);
106
Al Viro4d338e12006-03-31 02:30:15 -0800107static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 loff_t *ppos)
109{
110 /*
111 * Refresh the timer.
112 */
113 if(len)
114 return(ping_watchdog(harddog_out_fd));
115 return 0;
116}
117
118static int harddog_ioctl(struct inode *inode, struct file *file,
119 unsigned int cmd, unsigned long arg)
120{
Al Viro4d338e12006-03-31 02:30:15 -0800121 void __user *argp= (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 static struct watchdog_info ident = {
123 WDIOC_SETTIMEOUT,
124 0,
125 "UML Hardware Watchdog"
126 };
127 switch (cmd) {
128 default:
129 return -ENOTTY;
130 case WDIOC_GETSUPPORT:
Al Viro4d338e12006-03-31 02:30:15 -0800131 if(copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return -EFAULT;
133 return 0;
134 case WDIOC_GETSTATUS:
135 case WDIOC_GETBOOTSTATUS:
Al Viro4d338e12006-03-31 02:30:15 -0800136 return put_user(0,(int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 case WDIOC_KEEPALIVE:
138 return(ping_watchdog(harddog_out_fd));
139 }
140}
141
142static struct file_operations harddog_fops = {
143 .owner = THIS_MODULE,
144 .write = harddog_write,
145 .ioctl = harddog_ioctl,
146 .open = harddog_open,
147 .release = harddog_release,
148};
149
150static struct miscdevice harddog_miscdev = {
151 .minor = WATCHDOG_MINOR,
152 .name = "watchdog",
153 .fops = &harddog_fops,
154};
155
156static char banner[] __initdata = KERN_INFO "UML Watchdog Timer\n";
157
158static int __init harddog_init(void)
159{
160 int ret;
161
162 ret = misc_register(&harddog_miscdev);
163
164 if (ret)
165 return ret;
166
167 printk(banner);
168
169 return(0);
170}
171
172static void __exit harddog_exit(void)
173{
174 misc_deregister(&harddog_miscdev);
175}
176
177module_init(harddog_init);
178module_exit(harddog_exit);
179
180/*
181 * Overrides for Emacs so that we follow Linus's tabbing style.
182 * Emacs will notice this stuff at the end of the file and automatically
183 * adjust the settings for this buffer only. This must remain at the end
184 * of the file.
185 * ---------------------------------------------------------------------------
186 * Local variables:
187 * c-file-style: "linux"
188 * End:
189 */