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