| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	Industrial Computer Source WDT500/501 driver | 
|  | 3 | * | 
| Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 4 | *	(c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>, | 
|  | 5 | *						All Rights Reserved. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * | 
|  | 7 | *	This program is free software; you can redistribute it and/or | 
|  | 8 | *	modify it under the terms of the GNU General Public License | 
|  | 9 | *	as published by the Free Software Foundation; either version | 
|  | 10 | *	2 of the License, or (at your option) any later version. | 
|  | 11 | * | 
|  | 12 | *	Neither Alan Cox nor CymruNet Ltd. admit liability nor provide | 
|  | 13 | *	warranty for any of this software. This material is provided | 
|  | 14 | *	"AS-IS" and at no charge. | 
|  | 15 | * | 
|  | 16 | *	(c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk> | 
|  | 17 | * | 
|  | 18 | *	Release 0.10. | 
|  | 19 | * | 
|  | 20 | *	Fixes | 
|  | 21 | *		Dave Gregorich	:	Modularisation and minor bugs | 
|  | 22 | *		Alan Cox	:	Added the watchdog ioctl() stuff | 
|  | 23 | *		Alan Cox	:	Fixed the reboot problem (as noted by | 
|  | 24 | *					Matt Crocker). | 
|  | 25 | *		Alan Cox	:	Added wdt= boot option | 
|  | 26 | *		Alan Cox	:	Cleaned up copy/user stuff | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 27 | *		Tim Hockin	:	Added insmod parameters, comment | 
|  | 28 | *					cleanup, parameterized timeout | 
|  | 29 | *		Tigran Aivazian	:	Restructured wdt_init() to handle | 
|  | 30 | *					failures | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | *		Joel Becker	:	Added WDIOC_GET/SETTIMEOUT | 
|  | 32 | *		Matt Domsch	:	Added nowayout module option | 
|  | 33 | */ | 
|  | 34 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/interrupt.h> | 
|  | 36 | #include <linux/module.h> | 
|  | 37 | #include <linux/moduleparam.h> | 
|  | 38 | #include <linux/types.h> | 
|  | 39 | #include <linux/miscdevice.h> | 
|  | 40 | #include <linux/watchdog.h> | 
|  | 41 | #include <linux/fs.h> | 
|  | 42 | #include <linux/ioport.h> | 
|  | 43 | #include <linux/notifier.h> | 
|  | 44 | #include <linux/reboot.h> | 
|  | 45 | #include <linux/init.h> | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 46 | #include <linux/io.h> | 
|  | 47 | #include <linux/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <asm/system.h> | 
|  | 50 | #include "wd501p.h" | 
|  | 51 |  | 
|  | 52 | static unsigned long wdt_is_open; | 
|  | 53 | static char expect_close; | 
|  | 54 |  | 
|  | 55 | /* | 
|  | 56 | *	Module parameters | 
|  | 57 | */ | 
|  | 58 |  | 
|  | 59 | #define WD_TIMO 60			/* Default heartbeat = 60 seconds */ | 
|  | 60 |  | 
|  | 61 | static int heartbeat = WD_TIMO; | 
|  | 62 | static int wd_heartbeat; | 
|  | 63 | module_param(heartbeat, int, 0); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 64 | MODULE_PARM_DESC(heartbeat, | 
|  | 65 | "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default=" | 
|  | 66 | __MODULE_STRING(WD_TIMO) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 |  | 
| Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 68 | static int nowayout = WATCHDOG_NOWAYOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | module_param(nowayout, int, 0); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 70 | MODULE_PARM_DESC(nowayout, | 
|  | 71 | "Watchdog cannot be stopped once started (default=" | 
|  | 72 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 |  | 
|  | 74 | /* You must set these - there is no sane way to probe for this board. */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 75 | static int io = 0x240; | 
|  | 76 | static int irq = 11; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 |  | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 78 | static DEFINE_SPINLOCK(wdt_lock); | 
|  | 79 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | module_param(io, int, 0); | 
|  | 81 | MODULE_PARM_DESC(io, "WDT io port (default=0x240)"); | 
|  | 82 | module_param(irq, int, 0); | 
|  | 83 | MODULE_PARM_DESC(irq, "WDT irq (default=11)"); | 
|  | 84 |  | 
|  | 85 | #ifdef CONFIG_WDT_501 | 
|  | 86 | /* Support for the Fan Tachometer on the WDT501-P */ | 
|  | 87 | static int tachometer; | 
|  | 88 |  | 
|  | 89 | module_param(tachometer, int, 0); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 90 | MODULE_PARM_DESC(tachometer, | 
|  | 91 | "WDT501-P Fan Tachometer support (0=disable, default=0)"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #endif /* CONFIG_WDT_501 */ | 
|  | 93 |  | 
|  | 94 | /* | 
|  | 95 | *	Programming support | 
|  | 96 | */ | 
|  | 97 |  | 
|  | 98 | static void wdt_ctr_mode(int ctr, int mode) | 
|  | 99 | { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 100 | ctr <<= 6; | 
|  | 101 | ctr |= 0x30; | 
|  | 102 | ctr |= (mode << 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | outb_p(ctr, WDT_CR); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | static void wdt_ctr_load(int ctr, int val) | 
|  | 107 | { | 
|  | 108 | outb_p(val&0xFF, WDT_COUNT0+ctr); | 
|  | 109 | outb_p(val>>8, WDT_COUNT0+ctr); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | /** | 
|  | 113 | *	wdt_start: | 
|  | 114 | * | 
|  | 115 | *	Start the watchdog driver. | 
|  | 116 | */ | 
|  | 117 |  | 
|  | 118 | static int wdt_start(void) | 
|  | 119 | { | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 120 | unsigned long flags; | 
|  | 121 | spin_lock_irqsave(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | inb_p(WDT_DC);			/* Disable watchdog */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 123 | wdt_ctr_mode(0, 3);		/* Program CTR0 for Mode 3: | 
|  | 124 | Square Wave Generator */ | 
|  | 125 | wdt_ctr_mode(1, 2);		/* Program CTR1 for Mode 2: | 
|  | 126 | Rate Generator */ | 
|  | 127 | wdt_ctr_mode(2, 0);		/* Program CTR2 for Mode 0: | 
|  | 128 | Pulse on Terminal Count */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | wdt_ctr_load(0, 8948);		/* Count at 100Hz */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 130 | wdt_ctr_load(1, wd_heartbeat);	/* Heartbeat */ | 
|  | 131 | wdt_ctr_load(2, 65535);		/* Length of reset pulse */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | outb_p(0, WDT_DC);		/* Enable watchdog */ | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 133 | spin_unlock_irqrestore(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | return 0; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | /** | 
|  | 138 | *	wdt_stop: | 
|  | 139 | * | 
|  | 140 | *	Stop the watchdog driver. | 
|  | 141 | */ | 
|  | 142 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 143 | static int wdt_stop(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 145 | unsigned long flags; | 
|  | 146 | spin_lock_irqsave(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* Turn the card off */ | 
|  | 148 | inb_p(WDT_DC);			/* Disable watchdog */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 149 | wdt_ctr_load(2, 0);		/* 0 length reset pulses now */ | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 150 | spin_unlock_irqrestore(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return 0; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | /** | 
|  | 155 | *	wdt_ping: | 
|  | 156 | * | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 157 | *	Reload counter one with the watchdog heartbeat. We don't bother | 
|  | 158 | *	reloading the cascade counter. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | */ | 
|  | 160 |  | 
|  | 161 | static int wdt_ping(void) | 
|  | 162 | { | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 163 | unsigned long flags; | 
|  | 164 | spin_lock_irqsave(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | /* Write a watchdog value */ | 
|  | 166 | inb_p(WDT_DC);			/* Disable watchdog */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 167 | wdt_ctr_mode(1, 2);		/* Re-Program CTR1 for Mode 2: | 
|  | 168 | Rate Generator */ | 
|  | 169 | wdt_ctr_load(1, wd_heartbeat);	/* Heartbeat */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | outb_p(0, WDT_DC);		/* Enable watchdog */ | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 171 | spin_unlock_irqrestore(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return 0; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | /** | 
|  | 176 | *	wdt_set_heartbeat: | 
|  | 177 | *	@t:		the new heartbeat value that needs to be set. | 
|  | 178 | * | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 179 | *	Set a new heartbeat value for the watchdog device. If the heartbeat | 
|  | 180 | *	value is incorrect we keep the old value and return -EINVAL. If | 
|  | 181 | *	successful we return 0. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | */ | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 183 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | static int wdt_set_heartbeat(int t) | 
|  | 185 | { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 186 | if (t < 1 || t > 65535) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return -EINVAL; | 
|  | 188 |  | 
|  | 189 | heartbeat = t; | 
|  | 190 | wd_heartbeat = t * 100; | 
|  | 191 | return 0; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | /** | 
|  | 195 | *	wdt_get_status: | 
|  | 196 | *	@status:		the new status. | 
|  | 197 | * | 
|  | 198 | *	Extract the status information from a WDT watchdog device. There are | 
|  | 199 | *	several board variants so we have to know which bits are valid. Some | 
|  | 200 | *	bits default to one and some to zero in order to be maximally painful. | 
|  | 201 | * | 
|  | 202 | *	we then map the bits onto the status ioctl flags. | 
|  | 203 | */ | 
|  | 204 |  | 
|  | 205 | static int wdt_get_status(int *status) | 
|  | 206 | { | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 207 | unsigned char new_status; | 
|  | 208 | unsigned long flags; | 
|  | 209 |  | 
|  | 210 | spin_lock_irqsave(&wdt_lock, flags); | 
|  | 211 | new_status = inb_p(WDT_SR); | 
|  | 212 | spin_unlock_irqrestore(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 214 | *status = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (new_status & WDC_SR_ISOI0) | 
|  | 216 | *status |= WDIOF_EXTERN1; | 
|  | 217 | if (new_status & WDC_SR_ISII1) | 
|  | 218 | *status |= WDIOF_EXTERN2; | 
|  | 219 | #ifdef CONFIG_WDT_501 | 
|  | 220 | if (!(new_status & WDC_SR_TGOOD)) | 
|  | 221 | *status |= WDIOF_OVERHEAT; | 
|  | 222 | if (!(new_status & WDC_SR_PSUOVER)) | 
|  | 223 | *status |= WDIOF_POWEROVER; | 
|  | 224 | if (!(new_status & WDC_SR_PSUUNDR)) | 
|  | 225 | *status |= WDIOF_POWERUNDER; | 
|  | 226 | if (tachometer) { | 
|  | 227 | if (!(new_status & WDC_SR_FANGOOD)) | 
|  | 228 | *status |= WDIOF_FANFAULT; | 
|  | 229 | } | 
|  | 230 | #endif /* CONFIG_WDT_501 */ | 
|  | 231 | return 0; | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | #ifdef CONFIG_WDT_501 | 
|  | 235 | /** | 
|  | 236 | *	wdt_get_temperature: | 
|  | 237 | * | 
|  | 238 | *	Reports the temperature in degrees Fahrenheit. The API is in | 
|  | 239 | *	farenheit. It was designed by an imperial measurement luddite. | 
|  | 240 | */ | 
|  | 241 |  | 
|  | 242 | static int wdt_get_temperature(int *temperature) | 
|  | 243 | { | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 244 | unsigned short c; | 
|  | 245 | unsigned long flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 |  | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 247 | spin_lock_irqsave(&wdt_lock, flags); | 
|  | 248 | c = inb_p(WDT_RT); | 
|  | 249 | spin_unlock_irqrestore(&wdt_lock, flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | *temperature = (c * 11 / 15) + 7; | 
|  | 251 | return 0; | 
|  | 252 | } | 
|  | 253 | #endif /* CONFIG_WDT_501 */ | 
|  | 254 |  | 
|  | 255 | /** | 
|  | 256 | *	wdt_interrupt: | 
|  | 257 | *	@irq:		Interrupt number | 
|  | 258 | *	@dev_id:	Unused as we don't allow multiple devices. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | * | 
|  | 260 | *	Handle an interrupt from the board. These are raised when the status | 
|  | 261 | *	map changes in what the board considers an interesting way. That means | 
|  | 262 | *	a failure condition occurring. | 
|  | 263 | */ | 
|  | 264 |  | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 265 | static irqreturn_t wdt_interrupt(int irq, void *dev_id) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { | 
|  | 267 | /* | 
|  | 268 | *	Read the status register see what is up and | 
|  | 269 | *	then printk it. | 
|  | 270 | */ | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 271 | unsigned char status; | 
|  | 272 |  | 
|  | 273 | spin_lock(&wdt_lock); | 
|  | 274 | status = inb_p(WDT_SR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 |  | 
|  | 276 | printk(KERN_CRIT "WDT status %d\n", status); | 
|  | 277 |  | 
|  | 278 | #ifdef CONFIG_WDT_501 | 
|  | 279 | if (!(status & WDC_SR_TGOOD)) | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 280 | printk(KERN_CRIT "Overheat alarm.(%d)\n", inb_p(WDT_RT)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (!(status & WDC_SR_PSUOVER)) | 
|  | 282 | printk(KERN_CRIT "PSU over voltage.\n"); | 
|  | 283 | if (!(status & WDC_SR_PSUUNDR)) | 
|  | 284 | printk(KERN_CRIT "PSU under voltage.\n"); | 
|  | 285 | if (tachometer) { | 
|  | 286 | if (!(status & WDC_SR_FANGOOD)) | 
|  | 287 | printk(KERN_CRIT "Possible fan fault.\n"); | 
|  | 288 | } | 
|  | 289 | #endif /* CONFIG_WDT_501 */ | 
| Ilpo Jarvinen | 59338d4 | 2007-10-23 13:40:54 -0700 | [diff] [blame] | 290 | if (!(status & WDC_SR_WCCR)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | #ifdef SOFTWARE_REBOOT | 
|  | 292 | #ifdef ONLY_TESTING | 
|  | 293 | printk(KERN_CRIT "Would Reboot.\n"); | 
|  | 294 | #else | 
|  | 295 | printk(KERN_CRIT "Initiating system reboot.\n"); | 
| Eric W. Biederman | f82567e | 2005-07-26 11:53:19 -0600 | [diff] [blame] | 296 | emergency_restart(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | #endif | 
|  | 298 | #else | 
|  | 299 | printk(KERN_CRIT "Reset in 5ms.\n"); | 
|  | 300 | #endif | 
| Ilpo Jarvinen | 59338d4 | 2007-10-23 13:40:54 -0700 | [diff] [blame] | 301 | } | 
| Alan Cox | 01c785d | 2008-01-09 21:36:01 -0800 | [diff] [blame] | 302 | spin_unlock(&wdt_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | return IRQ_HANDLED; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 |  | 
|  | 307 | /** | 
|  | 308 | *	wdt_write: | 
|  | 309 | *	@file: file handle to the watchdog | 
|  | 310 | *	@buf: buffer to write (unused as data does not matter here | 
|  | 311 | *	@count: count of bytes | 
|  | 312 | *	@ppos: pointer to the position to write. No seeks allowed | 
|  | 313 | * | 
|  | 314 | *	A write to a watchdog device is defined as a keepalive signal. Any | 
|  | 315 | *	write of data will do, as we we don't define content meaning. | 
|  | 316 | */ | 
|  | 317 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 318 | static ssize_t wdt_write(struct file *file, const char __user *buf, | 
|  | 319 | size_t count, loff_t *ppos) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 321 | if (count) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | if (!nowayout) { | 
|  | 323 | size_t i; | 
|  | 324 |  | 
|  | 325 | /* In case it was set long ago */ | 
|  | 326 | expect_close = 0; | 
|  | 327 |  | 
|  | 328 | for (i = 0; i != count; i++) { | 
|  | 329 | char c; | 
|  | 330 | if (get_user(c, buf + i)) | 
|  | 331 | return -EFAULT; | 
|  | 332 | if (c == 'V') | 
|  | 333 | expect_close = 42; | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 | wdt_ping(); | 
|  | 337 | } | 
|  | 338 | return count; | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | /** | 
|  | 342 | *	wdt_ioctl: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | *	@file: file handle to the device | 
|  | 344 | *	@cmd: watchdog command | 
|  | 345 | *	@arg: argument pointer | 
|  | 346 | * | 
|  | 347 | *	The watchdog API defines a common set of functions for all watchdogs | 
|  | 348 | *	according to their available features. We only actually usefully support | 
|  | 349 | *	querying capabilities and current status. | 
|  | 350 | */ | 
|  | 351 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 352 | static long wdt_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { | 
|  | 354 | void __user *argp = (void __user *)arg; | 
|  | 355 | int __user *p = argp; | 
|  | 356 | int new_heartbeat; | 
|  | 357 | int status; | 
|  | 358 |  | 
|  | 359 | static struct watchdog_info ident = { | 
|  | 360 | .options =		WDIOF_SETTIMEOUT| | 
|  | 361 | WDIOF_MAGICCLOSE| | 
|  | 362 | WDIOF_KEEPALIVEPING, | 
|  | 363 | .firmware_version =	1, | 
|  | 364 | .identity =		"WDT500/501", | 
|  | 365 | }; | 
|  | 366 |  | 
|  | 367 | /* Add options according to the card we have */ | 
|  | 368 | ident.options |= (WDIOF_EXTERN1|WDIOF_EXTERN2); | 
|  | 369 | #ifdef CONFIG_WDT_501 | 
|  | 370 | ident.options |= (WDIOF_OVERHEAT|WDIOF_POWERUNDER|WDIOF_POWEROVER); | 
|  | 371 | if (tachometer) | 
|  | 372 | ident.options |= WDIOF_FANFAULT; | 
|  | 373 | #endif /* CONFIG_WDT_501 */ | 
|  | 374 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 375 | switch (cmd) { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 376 | case WDIOC_GETSUPPORT: | 
|  | 377 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; | 
|  | 378 | case WDIOC_GETSTATUS: | 
|  | 379 | wdt_get_status(&status); | 
|  | 380 | return put_user(status, p); | 
|  | 381 | case WDIOC_GETBOOTSTATUS: | 
|  | 382 | return put_user(0, p); | 
|  | 383 | case WDIOC_KEEPALIVE: | 
|  | 384 | wdt_ping(); | 
|  | 385 | return 0; | 
|  | 386 | case WDIOC_SETTIMEOUT: | 
|  | 387 | if (get_user(new_heartbeat, p)) | 
|  | 388 | return -EFAULT; | 
|  | 389 | if (wdt_set_heartbeat(new_heartbeat)) | 
|  | 390 | return -EINVAL; | 
|  | 391 | wdt_ping(); | 
|  | 392 | /* Fall */ | 
|  | 393 | case WDIOC_GETTIMEOUT: | 
|  | 394 | return put_user(heartbeat, p); | 
| Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 395 | default: | 
|  | 396 | return -ENOTTY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } | 
|  | 398 | } | 
|  | 399 |  | 
|  | 400 | /** | 
|  | 401 | *	wdt_open: | 
|  | 402 | *	@inode: inode of device | 
|  | 403 | *	@file: file handle to device | 
|  | 404 | * | 
|  | 405 | *	The watchdog device has been opened. The watchdog device is single | 
|  | 406 | *	open and on opening we load the counters. Counter zero is a 100Hz | 
|  | 407 | *	cascade, into counter 1 which downcounts to reboot. When the counter | 
|  | 408 | *	triggers counter 2 downcounts the length of the reset pulse which | 
|  | 409 | *	set set to be as long as possible. | 
|  | 410 | */ | 
|  | 411 |  | 
|  | 412 | static int wdt_open(struct inode *inode, struct file *file) | 
|  | 413 | { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 414 | if (test_and_set_bit(0, &wdt_is_open)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return -EBUSY; | 
|  | 416 | /* | 
|  | 417 | *	Activate | 
|  | 418 | */ | 
|  | 419 | wdt_start(); | 
|  | 420 | return nonseekable_open(inode, file); | 
|  | 421 | } | 
|  | 422 |  | 
|  | 423 | /** | 
|  | 424 | *	wdt_release: | 
|  | 425 | *	@inode: inode to board | 
|  | 426 | *	@file: file handle to board | 
|  | 427 | * | 
|  | 428 | *	The watchdog has a configurable API. There is a religious dispute | 
|  | 429 | *	between people who want their watchdog to be able to shut down and | 
|  | 430 | *	those who want to be sure if the watchdog manager dies the machine | 
|  | 431 | *	reboots. In the former case we disable the counters, in the latter | 
|  | 432 | *	case you have to open it again very soon. | 
|  | 433 | */ | 
|  | 434 |  | 
|  | 435 | static int wdt_release(struct inode *inode, struct file *file) | 
|  | 436 | { | 
|  | 437 | if (expect_close == 42) { | 
|  | 438 | wdt_stop(); | 
|  | 439 | clear_bit(0, &wdt_is_open); | 
|  | 440 | } else { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 441 | printk(KERN_CRIT | 
|  | 442 | "wdt: WDT device closed unexpectedly.  WDT will not stop!\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | wdt_ping(); | 
|  | 444 | } | 
|  | 445 | expect_close = 0; | 
|  | 446 | return 0; | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | #ifdef CONFIG_WDT_501 | 
|  | 450 | /** | 
|  | 451 | *	wdt_temp_read: | 
|  | 452 | *	@file: file handle to the watchdog board | 
|  | 453 | *	@buf: buffer to write 1 byte into | 
|  | 454 | *	@count: length of buffer | 
|  | 455 | *	@ptr: offset (no seek allowed) | 
|  | 456 | * | 
|  | 457 | *	Temp_read reports the temperature in degrees Fahrenheit. The API is in | 
|  | 458 | *	farenheit. It was designed by an imperial measurement luddite. | 
|  | 459 | */ | 
|  | 460 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 461 | static ssize_t wdt_temp_read(struct file *file, char __user *buf, | 
|  | 462 | size_t count, loff_t *ptr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { | 
|  | 464 | int temperature; | 
|  | 465 |  | 
|  | 466 | if (wdt_get_temperature(&temperature)) | 
|  | 467 | return -EFAULT; | 
|  | 468 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 469 | if (copy_to_user(buf, &temperature, 1)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | return -EFAULT; | 
|  | 471 |  | 
|  | 472 | return 1; | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | /** | 
|  | 476 | *	wdt_temp_open: | 
|  | 477 | *	@inode: inode of device | 
|  | 478 | *	@file: file handle to device | 
|  | 479 | * | 
|  | 480 | *	The temperature device has been opened. | 
|  | 481 | */ | 
|  | 482 |  | 
|  | 483 | static int wdt_temp_open(struct inode *inode, struct file *file) | 
|  | 484 | { | 
|  | 485 | return nonseekable_open(inode, file); | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | /** | 
|  | 489 | *	wdt_temp_release: | 
|  | 490 | *	@inode: inode to board | 
|  | 491 | *	@file: file handle to board | 
|  | 492 | * | 
|  | 493 | *	The temperature device has been closed. | 
|  | 494 | */ | 
|  | 495 |  | 
|  | 496 | static int wdt_temp_release(struct inode *inode, struct file *file) | 
|  | 497 | { | 
|  | 498 | return 0; | 
|  | 499 | } | 
|  | 500 | #endif /* CONFIG_WDT_501 */ | 
|  | 501 |  | 
|  | 502 | /** | 
|  | 503 | *	notify_sys: | 
|  | 504 | *	@this: our notifier block | 
|  | 505 | *	@code: the event being reported | 
|  | 506 | *	@unused: unused | 
|  | 507 | * | 
|  | 508 | *	Our notifier is called on system shutdowns. We want to turn the card | 
|  | 509 | *	off at reboot otherwise the machine will reboot again during memory | 
|  | 510 | *	test or worse yet during the following fsck. This would suck, in fact | 
|  | 511 | *	trust me - if it happens it does suck. | 
|  | 512 | */ | 
|  | 513 |  | 
|  | 514 | static int wdt_notify_sys(struct notifier_block *this, unsigned long code, | 
|  | 515 | void *unused) | 
|  | 516 | { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 517 | if (code == SYS_DOWN || code == SYS_HALT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | wdt_stop(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | return NOTIFY_DONE; | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | /* | 
|  | 523 | *	Kernel Interfaces | 
|  | 524 | */ | 
|  | 525 |  | 
|  | 526 |  | 
| Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 527 | static const struct file_operations wdt_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | .owner		= THIS_MODULE, | 
|  | 529 | .llseek		= no_llseek, | 
|  | 530 | .write		= wdt_write, | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 531 | .unlocked_ioctl	= wdt_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | .open		= wdt_open, | 
|  | 533 | .release	= wdt_release, | 
|  | 534 | }; | 
|  | 535 |  | 
|  | 536 | static struct miscdevice wdt_miscdev = { | 
|  | 537 | .minor	= WATCHDOG_MINOR, | 
|  | 538 | .name	= "watchdog", | 
|  | 539 | .fops	= &wdt_fops, | 
|  | 540 | }; | 
|  | 541 |  | 
|  | 542 | #ifdef CONFIG_WDT_501 | 
| Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 543 | static const struct file_operations wdt_temp_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | .owner		= THIS_MODULE, | 
|  | 545 | .llseek		= no_llseek, | 
|  | 546 | .read		= wdt_temp_read, | 
|  | 547 | .open		= wdt_temp_open, | 
|  | 548 | .release	= wdt_temp_release, | 
|  | 549 | }; | 
|  | 550 |  | 
|  | 551 | static struct miscdevice temp_miscdev = { | 
|  | 552 | .minor	= TEMP_MINOR, | 
|  | 553 | .name	= "temperature", | 
|  | 554 | .fops	= &wdt_temp_fops, | 
|  | 555 | }; | 
|  | 556 | #endif /* CONFIG_WDT_501 */ | 
|  | 557 |  | 
|  | 558 | /* | 
|  | 559 | *	The WDT card needs to learn about soft shutdowns in order to | 
|  | 560 | *	turn the timebomb registers off. | 
|  | 561 | */ | 
|  | 562 |  | 
|  | 563 | static struct notifier_block wdt_notifier = { | 
|  | 564 | .notifier_call = wdt_notify_sys, | 
|  | 565 | }; | 
|  | 566 |  | 
|  | 567 | /** | 
|  | 568 | *	cleanup_module: | 
|  | 569 | * | 
|  | 570 | *	Unload the watchdog. You cannot do this with any file handles open. | 
|  | 571 | *	If your watchdog is set to continue ticking on close and you unload | 
|  | 572 | *	it, well it keeps ticking. We won't get the interrupt but the board | 
|  | 573 | *	will not touch PC memory so all is fine. You just have to load a new | 
|  | 574 | *	module in 60 seconds or reboot. | 
|  | 575 | */ | 
|  | 576 |  | 
|  | 577 | static void __exit wdt_exit(void) | 
|  | 578 | { | 
|  | 579 | misc_deregister(&wdt_miscdev); | 
|  | 580 | #ifdef CONFIG_WDT_501 | 
|  | 581 | misc_deregister(&temp_miscdev); | 
|  | 582 | #endif /* CONFIG_WDT_501 */ | 
|  | 583 | unregister_reboot_notifier(&wdt_notifier); | 
|  | 584 | free_irq(irq, NULL); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 585 | release_region(io, 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | } | 
|  | 587 |  | 
|  | 588 | /** | 
|  | 589 | * 	wdt_init: | 
|  | 590 | * | 
|  | 591 | *	Set up the WDT watchdog board. All we have to do is grab the | 
|  | 592 | *	resources we require and bitch if anyone beat us to them. | 
|  | 593 | *	The open() function will actually kick the board off. | 
|  | 594 | */ | 
|  | 595 |  | 
|  | 596 | static int __init wdt_init(void) | 
|  | 597 | { | 
|  | 598 | int ret; | 
|  | 599 |  | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 600 | /* Check that the heartbeat value is within it's range; | 
|  | 601 | if not reset to the default */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | if (wdt_set_heartbeat(heartbeat)) { | 
|  | 603 | wdt_set_heartbeat(WD_TIMO); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 604 | printk(KERN_INFO "wdt: heartbeat value must be 0 < heartbeat < 65536, using %d\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | WD_TIMO); | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | if (!request_region(io, 8, "wdt501p")) { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 609 | printk(KERN_ERR | 
|  | 610 | "wdt: I/O address 0x%04x already in use\n", io); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | ret = -EBUSY; | 
|  | 612 | goto out; | 
|  | 613 | } | 
|  | 614 |  | 
| Thomas Gleixner | 0f2ed4c | 2006-07-01 19:29:33 -0700 | [diff] [blame] | 615 | ret = request_irq(irq, wdt_interrupt, IRQF_DISABLED, "wdt501p", NULL); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 616 | if (ret) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | printk(KERN_ERR "wdt: IRQ %d is not free.\n", irq); | 
|  | 618 | goto outreg; | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | ret = register_reboot_notifier(&wdt_notifier); | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 622 | if (ret) { | 
|  | 623 | printk(KERN_ERR | 
|  | 624 | "wdt: cannot register reboot notifier (err=%d)\n", ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | goto outirq; | 
|  | 626 | } | 
|  | 627 |  | 
|  | 628 | #ifdef CONFIG_WDT_501 | 
|  | 629 | ret = misc_register(&temp_miscdev); | 
|  | 630 | if (ret) { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 631 | printk(KERN_ERR | 
|  | 632 | "wdt: cannot register miscdev on minor=%d (err=%d)\n", | 
|  | 633 | TEMP_MINOR, ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | goto outrbt; | 
|  | 635 | } | 
|  | 636 | #endif /* CONFIG_WDT_501 */ | 
|  | 637 |  | 
|  | 638 | ret = misc_register(&wdt_miscdev); | 
|  | 639 | if (ret) { | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 640 | printk(KERN_ERR | 
|  | 641 | "wdt: cannot register miscdev on minor=%d (err=%d)\n", | 
|  | 642 | WATCHDOG_MINOR, ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | goto outmisc; | 
|  | 644 | } | 
|  | 645 |  | 
|  | 646 | ret = 0; | 
|  | 647 | printk(KERN_INFO "WDT500/501-P driver 0.10 at 0x%04x (Interrupt %d). heartbeat=%d sec (nowayout=%d)\n", | 
|  | 648 | io, irq, heartbeat, nowayout); | 
|  | 649 | #ifdef CONFIG_WDT_501 | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 650 | printk(KERN_INFO "wdt: Fan Tachometer is %s\n", | 
|  | 651 | (tachometer ? "Enabled" : "Disabled")); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | #endif /* CONFIG_WDT_501 */ | 
|  | 653 |  | 
|  | 654 | out: | 
|  | 655 | return ret; | 
|  | 656 |  | 
|  | 657 | outmisc: | 
|  | 658 | #ifdef CONFIG_WDT_501 | 
|  | 659 | misc_deregister(&temp_miscdev); | 
|  | 660 | outrbt: | 
|  | 661 | #endif /* CONFIG_WDT_501 */ | 
|  | 662 | unregister_reboot_notifier(&wdt_notifier); | 
|  | 663 | outirq: | 
|  | 664 | free_irq(irq, NULL); | 
|  | 665 | outreg: | 
| Alan Cox | 9f2d1f0 | 2008-08-04 17:55:35 +0100 | [diff] [blame] | 666 | release_region(io, 8); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | goto out; | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | module_init(wdt_init); | 
|  | 671 | module_exit(wdt_exit); | 
|  | 672 |  | 
|  | 673 | MODULE_AUTHOR("Alan Cox"); | 
|  | 674 | MODULE_DESCRIPTION("Driver for ISA ICS watchdog cards (WDT500/501)"); | 
|  | 675 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | 
|  | 676 | MODULE_ALIAS_MISCDEV(TEMP_MINOR); | 
|  | 677 | MODULE_LICENSE("GPL"); |