1*4882a593Smuzhiyun /* UML hardware watchdog, shamelessly stolen from:
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * SoftDog 0.05: A Software Watchdog Device
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved.
6*4882a593Smuzhiyun * http://www.redhat.com
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or
9*4882a593Smuzhiyun * modify it under the terms of the GNU General Public License
10*4882a593Smuzhiyun * as published by the Free Software Foundation; either version
11*4882a593Smuzhiyun * 2 of the License, or (at your option) any later version.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
14*4882a593Smuzhiyun * warranty for any of this software. This material is provided
15*4882a593Smuzhiyun * "AS-IS" and at no charge.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Software only watchdog driver. Unlike its big brother the WDT501P
20*4882a593Smuzhiyun * driver this won't always recover a failed machine.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * 03/96: Angelo Haritsis <ah@doc.ic.ac.uk> :
23*4882a593Smuzhiyun * Modularised.
24*4882a593Smuzhiyun * Added soft_margin; use upon insmod to change the timer delay.
25*4882a593Smuzhiyun * NB: uses same minor as wdt (WATCHDOG_MINOR); we could use separate
26*4882a593Smuzhiyun * minors.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * 19980911 Alan Cox
29*4882a593Smuzhiyun * Made SMP safe for 2.3.x
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * 20011127 Joel Becker (jlbec@evilplan.org>
32*4882a593Smuzhiyun * Added soft_noboot; Allows testing the softdog trigger without
33*4882a593Smuzhiyun * requiring a recompile.
34*4882a593Smuzhiyun * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun #include <linux/module.h>
38*4882a593Smuzhiyun #include <linux/types.h>
39*4882a593Smuzhiyun #include <linux/kernel.h>
40*4882a593Smuzhiyun #include <linux/fs.h>
41*4882a593Smuzhiyun #include <linux/mm.h>
42*4882a593Smuzhiyun #include <linux/miscdevice.h>
43*4882a593Smuzhiyun #include <linux/watchdog.h>
44*4882a593Smuzhiyun #include <linux/reboot.h>
45*4882a593Smuzhiyun #include <linux/mutex.h>
46*4882a593Smuzhiyun #include <linux/init.h>
47*4882a593Smuzhiyun #include <linux/spinlock.h>
48*4882a593Smuzhiyun #include <linux/uaccess.h>
49*4882a593Smuzhiyun #include "mconsole.h"
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun MODULE_LICENSE("GPL");
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun static DEFINE_MUTEX(harddog_mutex);
54*4882a593Smuzhiyun static DEFINE_SPINLOCK(lock);
55*4882a593Smuzhiyun static int timer_alive;
56*4882a593Smuzhiyun static int harddog_in_fd = -1;
57*4882a593Smuzhiyun static int harddog_out_fd = -1;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * Allow only one person to hold it open
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun extern int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock);
64*4882a593Smuzhiyun
harddog_open(struct inode * inode,struct file * file)65*4882a593Smuzhiyun static int harddog_open(struct inode *inode, struct file *file)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun int err = -EBUSY;
68*4882a593Smuzhiyun char *sock = NULL;
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun mutex_lock(&harddog_mutex);
71*4882a593Smuzhiyun spin_lock(&lock);
72*4882a593Smuzhiyun if(timer_alive)
73*4882a593Smuzhiyun goto err;
74*4882a593Smuzhiyun #ifdef CONFIG_WATCHDOG_NOWAYOUT
75*4882a593Smuzhiyun __module_get(THIS_MODULE);
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #ifdef CONFIG_MCONSOLE
79*4882a593Smuzhiyun sock = mconsole_notify_socket();
80*4882a593Smuzhiyun #endif
81*4882a593Smuzhiyun err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
82*4882a593Smuzhiyun if(err)
83*4882a593Smuzhiyun goto err;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun timer_alive = 1;
86*4882a593Smuzhiyun spin_unlock(&lock);
87*4882a593Smuzhiyun mutex_unlock(&harddog_mutex);
88*4882a593Smuzhiyun return stream_open(inode, file);
89*4882a593Smuzhiyun err:
90*4882a593Smuzhiyun spin_unlock(&lock);
91*4882a593Smuzhiyun mutex_unlock(&harddog_mutex);
92*4882a593Smuzhiyun return err;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun extern void stop_watchdog(int in_fd, int out_fd);
96*4882a593Smuzhiyun
harddog_release(struct inode * inode,struct file * file)97*4882a593Smuzhiyun static int harddog_release(struct inode *inode, struct file *file)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * Shut off the timer.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun spin_lock(&lock);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun stop_watchdog(harddog_in_fd, harddog_out_fd);
106*4882a593Smuzhiyun harddog_in_fd = -1;
107*4882a593Smuzhiyun harddog_out_fd = -1;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun timer_alive=0;
110*4882a593Smuzhiyun spin_unlock(&lock);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun extern int ping_watchdog(int fd);
116*4882a593Smuzhiyun
harddog_write(struct file * file,const char __user * data,size_t len,loff_t * ppos)117*4882a593Smuzhiyun static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
118*4882a593Smuzhiyun loff_t *ppos)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun /*
121*4882a593Smuzhiyun * Refresh the timer.
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun if(len)
124*4882a593Smuzhiyun return ping_watchdog(harddog_out_fd);
125*4882a593Smuzhiyun return 0;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
harddog_ioctl_unlocked(struct file * file,unsigned int cmd,unsigned long arg)128*4882a593Smuzhiyun static int harddog_ioctl_unlocked(struct file *file,
129*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun void __user *argp= (void __user *)arg;
132*4882a593Smuzhiyun static struct watchdog_info ident = {
133*4882a593Smuzhiyun WDIOC_SETTIMEOUT,
134*4882a593Smuzhiyun 0,
135*4882a593Smuzhiyun "UML Hardware Watchdog"
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun switch (cmd) {
138*4882a593Smuzhiyun default:
139*4882a593Smuzhiyun return -ENOTTY;
140*4882a593Smuzhiyun case WDIOC_GETSUPPORT:
141*4882a593Smuzhiyun if(copy_to_user(argp, &ident, sizeof(ident)))
142*4882a593Smuzhiyun return -EFAULT;
143*4882a593Smuzhiyun return 0;
144*4882a593Smuzhiyun case WDIOC_GETSTATUS:
145*4882a593Smuzhiyun case WDIOC_GETBOOTSTATUS:
146*4882a593Smuzhiyun return put_user(0,(int __user *)argp);
147*4882a593Smuzhiyun case WDIOC_KEEPALIVE:
148*4882a593Smuzhiyun return ping_watchdog(harddog_out_fd);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
harddog_ioctl(struct file * file,unsigned int cmd,unsigned long arg)152*4882a593Smuzhiyun static long harddog_ioctl(struct file *file,
153*4882a593Smuzhiyun unsigned int cmd, unsigned long arg)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun long ret;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun mutex_lock(&harddog_mutex);
158*4882a593Smuzhiyun ret = harddog_ioctl_unlocked(file, cmd, arg);
159*4882a593Smuzhiyun mutex_unlock(&harddog_mutex);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun return ret;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun static const struct file_operations harddog_fops = {
165*4882a593Smuzhiyun .owner = THIS_MODULE,
166*4882a593Smuzhiyun .write = harddog_write,
167*4882a593Smuzhiyun .unlocked_ioctl = harddog_ioctl,
168*4882a593Smuzhiyun .compat_ioctl = compat_ptr_ioctl,
169*4882a593Smuzhiyun .open = harddog_open,
170*4882a593Smuzhiyun .release = harddog_release,
171*4882a593Smuzhiyun .llseek = no_llseek,
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun static struct miscdevice harddog_miscdev = {
175*4882a593Smuzhiyun .minor = WATCHDOG_MINOR,
176*4882a593Smuzhiyun .name = "watchdog",
177*4882a593Smuzhiyun .fops = &harddog_fops,
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun module_misc_device(harddog_miscdev);
180