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