xref: /openbmc/linux/arch/um/drivers/harddog_kern.c (revision 55b24334)
1 /* 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/types.h>
39 #include <linux/kernel.h>
40 #include <linux/fs.h>
41 #include <linux/mm.h>
42 #include <linux/miscdevice.h>
43 #include <linux/watchdog.h>
44 #include <linux/reboot.h>
45 #include <linux/mutex.h>
46 #include <linux/init.h>
47 #include <linux/spinlock.h>
48 #include <linux/uaccess.h>
49 #include "mconsole.h"
50 #include "harddog.h"
51 
52 MODULE_LICENSE("GPL");
53 
54 static DEFINE_MUTEX(harddog_mutex);
55 static DEFINE_SPINLOCK(lock);
56 static int timer_alive;
57 static int harddog_in_fd = -1;
58 static int harddog_out_fd = -1;
59 
60 /*
61  *	Allow only one person to hold it open
62  */
63 
64 static int harddog_open(struct inode *inode, struct file *file)
65 {
66 	int err = -EBUSY;
67 	char *sock = NULL;
68 
69 	mutex_lock(&harddog_mutex);
70 	spin_lock(&lock);
71 	if(timer_alive)
72 		goto err;
73 #ifdef CONFIG_WATCHDOG_NOWAYOUT
74 	__module_get(THIS_MODULE);
75 #endif
76 
77 #ifdef CONFIG_MCONSOLE
78 	sock = mconsole_notify_socket();
79 #endif
80 	err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
81 	if(err)
82 		goto err;
83 
84 	timer_alive = 1;
85 	spin_unlock(&lock);
86 	mutex_unlock(&harddog_mutex);
87 	return stream_open(inode, file);
88 err:
89 	spin_unlock(&lock);
90 	mutex_unlock(&harddog_mutex);
91 	return err;
92 }
93 
94 static int harddog_release(struct inode *inode, struct file *file)
95 {
96 	/*
97 	 *	Shut off the timer.
98 	 */
99 
100 	spin_lock(&lock);
101 
102 	stop_watchdog(harddog_in_fd, harddog_out_fd);
103 	harddog_in_fd = -1;
104 	harddog_out_fd = -1;
105 
106 	timer_alive=0;
107 	spin_unlock(&lock);
108 
109 	return 0;
110 }
111 
112 static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
113 			     loff_t *ppos)
114 {
115 	/*
116 	 *	Refresh the timer.
117 	 */
118 	if(len)
119 		return ping_watchdog(harddog_out_fd);
120 	return 0;
121 }
122 
123 static int harddog_ioctl_unlocked(struct file *file,
124 				  unsigned int cmd, unsigned long arg)
125 {
126 	void __user *argp= (void __user *)arg;
127 	static struct watchdog_info ident = {
128 		WDIOC_SETTIMEOUT,
129 		0,
130 		"UML Hardware Watchdog"
131 	};
132 	switch (cmd) {
133 		default:
134 			return -ENOTTY;
135 		case WDIOC_GETSUPPORT:
136 			if(copy_to_user(argp, &ident, sizeof(ident)))
137 				return -EFAULT;
138 			return 0;
139 		case WDIOC_GETSTATUS:
140 		case WDIOC_GETBOOTSTATUS:
141 			return put_user(0,(int __user *)argp);
142 		case WDIOC_KEEPALIVE:
143 			return ping_watchdog(harddog_out_fd);
144 	}
145 }
146 
147 static long harddog_ioctl(struct file *file,
148 			  unsigned int cmd, unsigned long arg)
149 {
150 	long ret;
151 
152 	mutex_lock(&harddog_mutex);
153 	ret = harddog_ioctl_unlocked(file, cmd, arg);
154 	mutex_unlock(&harddog_mutex);
155 
156 	return ret;
157 }
158 
159 static const struct file_operations harddog_fops = {
160 	.owner		= THIS_MODULE,
161 	.write		= harddog_write,
162 	.unlocked_ioctl	= harddog_ioctl,
163 	.compat_ioctl	= compat_ptr_ioctl,
164 	.open		= harddog_open,
165 	.release	= harddog_release,
166 	.llseek		= no_llseek,
167 };
168 
169 static struct miscdevice harddog_miscdev = {
170 	.minor		= WATCHDOG_MINOR,
171 	.name		= "watchdog",
172 	.fops		= &harddog_fops,
173 };
174 module_misc_device(harddog_miscdev);
175