indydog.c 4.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 *	IndyDog	0.3	A Hardware Watchdog Device for SGI IP22
 *
4 5
 *	(c) Copyright 2002 Guido Guenther <agx@sigxcpu.org>,
 *						All Rights Reserved.
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	based on softdog.c by Alan Cox <alan@redhat.com>
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/notifier.h>
#include <linux/reboot.h>
#include <linux/init.h>
26
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
27 28 29
#include <asm/sgi/mc.h>

#define PFX "indydog: "
30 31
static unsigned long indydog_alive;
static spinlock_t indydog_lock;
L
Linus Torvalds 已提交
32 33 34

#define WATCHDOG_TIMEOUT 30		/* 30 sec default timeout */

35
static int nowayout = WATCHDOG_NOWAYOUT;
L
Linus Torvalds 已提交
36
module_param(nowayout, int, 0);
37 38 39
MODULE_PARM_DESC(nowayout,
		"Watchdog cannot be stopped once started (default="
				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
L
Linus Torvalds 已提交
40 41 42

static void indydog_start(void)
{
43
	u32 mc_ctrl0;
L
Linus Torvalds 已提交
44

45 46
	spin_lock(&indydog_lock);
	mc_ctrl0 = sgimc->cpuctrl0;
L
Linus Torvalds 已提交
47 48
	mc_ctrl0 = sgimc->cpuctrl0 | SGIMC_CCTRL0_WDOG;
	sgimc->cpuctrl0 = mc_ctrl0;
49
	spin_unlock(&indydog_lock);
L
Linus Torvalds 已提交
50 51 52 53
}

static void indydog_stop(void)
{
54
	u32 mc_ctrl0;
L
Linus Torvalds 已提交
55

56 57 58
	spin_lock(&indydog_lock);

	mc_ctrl0 = sgimc->cpuctrl0;
L
Linus Torvalds 已提交
59 60
	mc_ctrl0 &= ~SGIMC_CCTRL0_WDOG;
	sgimc->cpuctrl0 = mc_ctrl0;
61
	spin_unlock(&indydog_lock);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75

	printk(KERN_INFO PFX "Stopped watchdog timer.\n");
}

static void indydog_ping(void)
{
	sgimc->watchdogt = 0;
}

/*
 *	Allow only one person to hold it open
 */
static int indydog_open(struct inode *inode, struct file *file)
{
76
	if (test_and_set_bit(0, &indydog_alive))
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		return -EBUSY;

	if (nowayout)
		__module_get(THIS_MODULE);

	/* Activate timer */
	indydog_start();
	indydog_ping();

	indydog_alive = 1;
	printk(KERN_INFO "Started watchdog timer.\n");

	return nonseekable_open(inode, file);
}

static int indydog_release(struct inode *inode, struct file *file)
{
	/* Shut off the timer.
	 * Lock it in if it's a module and we defined ...NOWAYOUT */
	if (!nowayout)
		indydog_stop();		/* Turn the WDT off */
98
	clear_bit(0, &indydog_alive);
L
Linus Torvalds 已提交
99 100 101
	return 0;
}

102 103
static ssize_t indydog_write(struct file *file, const char *data,
						size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
104 105
{
	/* Refresh the timer. */
106
	if (len)
L
Linus Torvalds 已提交
107 108 109 110
		indydog_ping();
	return len;
}

111 112
static long indydog_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg)
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122
{
	int options, retval = -EINVAL;
	static struct watchdog_info ident = {
		.options		= WDIOF_KEEPALIVEPING |
					  WDIOF_MAGICCLOSE,
		.firmware_version	= 0,
		.identity		= "Hardware Watchdog for SGI IP22",
	};

	switch (cmd) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	case WDIOC_GETSUPPORT:
		if (copy_to_user((struct watchdog_info *)arg,
				 &ident, sizeof(ident)))
			return -EFAULT;
		return 0;
	case WDIOC_GETSTATUS:
	case WDIOC_GETBOOTSTATUS:
		return put_user(0, (int *)arg);
	case WDIOC_SETOPTIONS:
	{
		if (get_user(options, (int *)arg))
			return -EFAULT;
		if (options & WDIOS_DISABLECARD) {
			indydog_stop();
			retval = 0;
		}
		if (options & WDIOS_ENABLECARD) {
			indydog_start();
			retval = 0;
L
Linus Torvalds 已提交
142
		}
143 144
		return retval;
	}
145 146 147 148 149
	case WDIOC_KEEPALIVE:
		indydog_ping();
		return 0;
	case WDIOC_GETTIMEOUT:
		return put_user(WATCHDOG_TIMEOUT, (int *)arg);
150 151
	default:
		return -ENOTTY;
L
Linus Torvalds 已提交
152 153 154
	}
}

155 156
static int indydog_notify_sys(struct notifier_block *this,
					unsigned long code, void *unused)
L
Linus Torvalds 已提交
157 158 159 160 161 162 163
{
	if (code == SYS_DOWN || code == SYS_HALT)
		indydog_stop();		/* Turn the WDT off */

	return NOTIFY_DONE;
}

164
static const struct file_operations indydog_fops = {
L
Linus Torvalds 已提交
165 166 167
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.write		= indydog_write,
168
	.unlocked_ioctl	= indydog_ioctl,
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
	.open		= indydog_open,
	.release	= indydog_release,
};

static struct miscdevice indydog_miscdev = {
	.minor		= WATCHDOG_MINOR,
	.name		= "watchdog",
	.fops		= &indydog_fops,
};

static struct notifier_block indydog_notifier = {
	.notifier_call = indydog_notify_sys,
};

static char banner[] __initdata =
	KERN_INFO PFX "Hardware Watchdog Timer for SGI IP22: 0.3\n";

static int __init watchdog_init(void)
{
	int ret;

190 191
	spin_lock_init(&indydog_lock);

L
Linus Torvalds 已提交
192 193
	ret = register_reboot_notifier(&indydog_notifier);
	if (ret) {
194 195
		printk(KERN_ERR PFX
			"cannot register reboot notifier (err=%d)\n", ret);
L
Linus Torvalds 已提交
196 197 198 199 200
		return ret;
	}

	ret = misc_register(&indydog_miscdev);
	if (ret) {
201 202 203
		printk(KERN_ERR PFX
			"cannot register miscdev on minor=%d (err=%d)\n",
							WATCHDOG_MINOR, ret);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		unregister_reboot_notifier(&indydog_notifier);
		return ret;
	}

	printk(banner);

	return 0;
}

static void __exit watchdog_exit(void)
{
	misc_deregister(&indydog_miscdev);
	unregister_reboot_notifier(&indydog_notifier);
}

module_init(watchdog_init);
module_exit(watchdog_exit);

MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
MODULE_DESCRIPTION("Hardware Watchdog Device for SGI IP22");
MODULE_LICENSE("GPL");
MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);