blob: 2d0266d0254d5eef5477a7da2d5e9ea36378ca46 [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.
Jeff Dike5bbcbec2007-02-10 01:43:58 -080012 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
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>
Jeff Dike5bbcbec2007-02-10 01:43:58 -080032 * Added soft_noboot; Allows testing the softdog trigger without
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * requiring a recompile.
34 * Added WDIOC_GETTIMEOUT and WDIOC_SETTIMOUT.
35 */
Jeff Dike5bbcbec2007-02-10 01:43:58 -080036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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>
Arnd Bergmann9a181c52010-09-11 18:38:03 +020045#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/init.h>
Jeff Dike42d36112007-02-10 01:43:57 -080047#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "mconsole.h"
50
51MODULE_LICENSE("GPL");
52
Arnd Bergmann9a181c52010-09-11 18:38:03 +020053static DEFINE_MUTEX(harddog_mutex);
Jeff Dike42d36112007-02-10 01:43:57 -080054static DEFINE_SPINLOCK(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static int timer_alive;
56static int harddog_in_fd = -1;
57static int harddog_out_fd = -1;
58
59/*
60 * Allow only one person to hold it open
61 */
Jeff Dike5bbcbec2007-02-10 01:43:58 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063extern int start_watchdog(int *in_fd_ret, int *out_fd_ret, char *sock);
64
65static int harddog_open(struct inode *inode, struct file *file)
66{
Jeff Dike42d36112007-02-10 01:43:57 -080067 int err = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 char *sock = NULL;
69
Arnd Bergmann9a181c52010-09-11 18:38:03 +020070 mutex_lock(&harddog_mutex);
Jeff Dike42d36112007-02-10 01:43:57 -080071 spin_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if(timer_alive)
Jeff Dike42d36112007-02-10 01:43:57 -080073 goto err;
Jeff Dike8d820762007-10-16 01:26:45 -070074#ifdef CONFIG_WATCHDOG_NOWAYOUT
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 __module_get(THIS_MODULE);
76#endif
77
78#ifdef CONFIG_MCONSOLE
79 sock = mconsole_notify_socket();
80#endif
81 err = start_watchdog(&harddog_in_fd, &harddog_out_fd, sock);
Jeff Dike42d36112007-02-10 01:43:57 -080082 if(err)
83 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 timer_alive = 1;
Jeff Dike42d36112007-02-10 01:43:57 -080086 spin_unlock(&lock);
Arnd Bergmann9a181c52010-09-11 18:38:03 +020087 mutex_unlock(&harddog_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return nonseekable_open(inode, file);
Jeff Dike42d36112007-02-10 01:43:57 -080089err:
90 spin_unlock(&lock);
Arnd Bergmann9a181c52010-09-11 18:38:03 +020091 mutex_unlock(&harddog_mutex);
Jeff Dike42d36112007-02-10 01:43:57 -080092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95extern void stop_watchdog(int in_fd, int out_fd);
96
97static int harddog_release(struct inode *inode, struct file *file)
98{
99 /*
100 * Shut off the timer.
101 */
Jeff Dike42d36112007-02-10 01:43:57 -0800102
103 spin_lock(&lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 stop_watchdog(harddog_in_fd, harddog_out_fd);
106 harddog_in_fd = -1;
107 harddog_out_fd = -1;
108
109 timer_alive=0;
Jeff Dike42d36112007-02-10 01:43:57 -0800110 spin_unlock(&lock);
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
113}
114
115extern int ping_watchdog(int fd);
116
Al Viro4d338e12006-03-31 02:30:15 -0800117static ssize_t harddog_write(struct file *file, const char __user *data, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 loff_t *ppos)
119{
120 /*
121 * Refresh the timer.
122 */
123 if(len)
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800124 return ping_watchdog(harddog_out_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Frederic Weisbecker9f37af62010-05-19 15:08:17 +0200128static int harddog_ioctl_unlocked(struct file *file,
129 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Al Viro4d338e12006-03-31 02:30:15 -0800131 void __user *argp= (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 static struct watchdog_info ident = {
133 WDIOC_SETTIMEOUT,
134 0,
135 "UML Hardware Watchdog"
136 };
137 switch (cmd) {
138 default:
139 return -ENOTTY;
140 case WDIOC_GETSUPPORT:
Al Viro4d338e12006-03-31 02:30:15 -0800141 if(copy_to_user(argp, &ident, sizeof(ident)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EFAULT;
143 return 0;
144 case WDIOC_GETSTATUS:
145 case WDIOC_GETBOOTSTATUS:
Al Viro4d338e12006-03-31 02:30:15 -0800146 return put_user(0,(int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case WDIOC_KEEPALIVE:
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800148 return ping_watchdog(harddog_out_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 }
150}
151
Frederic Weisbecker9f37af62010-05-19 15:08:17 +0200152static long harddog_ioctl(struct file *file,
153 unsigned int cmd, unsigned long arg)
154{
155 long ret;
156
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200157 mutex_lock(&harddog_mutex);
Frederic Weisbecker9f37af62010-05-19 15:08:17 +0200158 ret = harddog_ioctl_unlocked(file, cmd, arg);
Arnd Bergmann9a181c52010-09-11 18:38:03 +0200159 mutex_unlock(&harddog_mutex);
Frederic Weisbecker9f37af62010-05-19 15:08:17 +0200160
161 return ret;
162}
163
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800164static const struct file_operations harddog_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .owner = THIS_MODULE,
166 .write = harddog_write,
Frederic Weisbecker9f37af62010-05-19 15:08:17 +0200167 .unlocked_ioctl = harddog_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .open = harddog_open,
169 .release = harddog_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200170 .llseek = no_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171};
172
173static struct miscdevice harddog_miscdev = {
174 .minor = WATCHDOG_MINOR,
175 .name = "watchdog",
176 .fops = &harddog_fops,
177};
178
179static char banner[] __initdata = KERN_INFO "UML Watchdog Timer\n";
180
181static int __init harddog_init(void)
182{
183 int ret;
184
185 ret = misc_register(&harddog_miscdev);
186
187 if (ret)
188 return ret;
189
190 printk(banner);
191
Jeff Dike5bbcbec2007-02-10 01:43:58 -0800192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195static void __exit harddog_exit(void)
196{
197 misc_deregister(&harddog_miscdev);
198}
199
200module_init(harddog_init);
201module_exit(harddog_exit);