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