Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 2 | * Wdt977 0.04: A Watchdog Device for Netwinder W83977AF chip |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * (c) Copyright 1998 Rebel.com (Woody Suwalski <woody@netwinder.org>) |
| 5 | * |
| 6 | * ----------------------- |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * ----------------------- |
| 14 | * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> |
| 15 | * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT |
| 16 | * 19-Dec-2001 Woody Suwalski: Netwinder fixes, ioctl interface |
| 17 | * 06-Jan-2002 Woody Suwalski: For compatibility, convert all timeouts |
| 18 | * from minutes to seconds. |
| 19 | * 07-Jul-2003 Daniele Bellucci: Audit return code of misc_register in |
| 20 | * nwwatchdog_init. |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 21 | * 25-Oct-2005 Woody Suwalski: Convert addresses to #defs, add spinlocks |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 22 | * remove limitiation to be used on |
| 23 | * Netwinders only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 26 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/moduleparam.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/types.h> |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/fs.h> |
| 33 | #include <linux/miscdevice.h> |
| 34 | #include <linux/init.h> |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 35 | #include <linux/ioport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/watchdog.h> |
| 37 | #include <linux/notifier.h> |
| 38 | #include <linux/reboot.h> |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 39 | #include <linux/io.h> |
| 40 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/system.h> |
| 43 | #include <asm/mach-types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 45 | #define WATCHDOG_VERSION "0.04" |
| 46 | #define WATCHDOG_NAME "Wdt977" |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 47 | |
| 48 | #define IO_INDEX_PORT 0x370 /* on some systems it can be 0x3F0 */ |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 49 | #define IO_DATA_PORT (IO_INDEX_PORT + 1) |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 50 | |
| 51 | #define UNLOCK_DATA 0x87 |
| 52 | #define LOCK_DATA 0xAA |
| 53 | #define DEVICE_REGISTER 0x07 |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */ |
| 57 | |
| 58 | static int timeout = DEFAULT_TIMEOUT; |
| 59 | static int timeoutM; /* timeout in minutes */ |
| 60 | static unsigned long timer_alive; |
| 61 | static int testmode; |
| 62 | static char expect_close; |
Alexey Dobriyan | c7dfd0c | 2007-11-01 16:27:08 -0700 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(spinlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | module_param(timeout, int, 0); |
Randy Dunlap | 76550d3 | 2010-05-01 09:46:15 -0700 | [diff] [blame] | 66 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (60..15300, default=" |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 67 | __MODULE_STRING(DEFAULT_TIMEOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | module_param(testmode, int, 0); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 69 | MODULE_PARM_DESC(testmode, "Watchdog testmode (1 = no reboot), default=0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 71 | static int nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | module_param(nowayout, int, 0); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 73 | MODULE_PARM_DESC(nowayout, |
| 74 | "Watchdog cannot be stopped once started (default=" |
| 75 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Start the watchdog |
| 79 | */ |
| 80 | |
| 81 | static int wdt977_start(void) |
| 82 | { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 83 | unsigned long flags; |
| 84 | |
| 85 | spin_lock_irqsave(&spinlock, flags); |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | /* unlock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 88 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
| 89 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4 |
| 92 | * F2 has the timeout in minutes |
| 93 | * F3 could be set to the POWER LED blink (with GP17 set to PowerLed) |
| 94 | * at timeout, and to reset timer on kbd/mouse activity (not impl.) |
| 95 | * F4 is used to just clear the TIMEOUT'ed state (bit 0) |
| 96 | */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 97 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 98 | outb_p(0x08, IO_DATA_PORT); |
| 99 | outb_p(0xF2, IO_INDEX_PORT); |
| 100 | outb_p(timeoutM, IO_DATA_PORT); |
| 101 | outb_p(0xF3, IO_INDEX_PORT); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 102 | outb_p(0x00, IO_DATA_PORT); /* another setting is 0E for |
| 103 | kbd/mouse/LED */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 104 | outb_p(0xF4, IO_INDEX_PORT); |
| 105 | outb_p(0x00, IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 107 | /* At last select device Aux1 (dev=7) and set GP16 as a |
| 108 | * watchdog output. In test mode watch the bit 1 on F4 to |
| 109 | * indicate "triggered" |
| 110 | */ |
| 111 | if (!testmode) { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 112 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 113 | outb_p(0x07, IO_DATA_PORT); |
| 114 | outb_p(0xE6, IO_INDEX_PORT); |
| 115 | outb_p(0x08, IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* lock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 119 | outb_p(LOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 121 | spin_unlock_irqrestore(&spinlock, flags); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 122 | pr_info("activated\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Stop the watchdog |
| 129 | */ |
| 130 | |
| 131 | static int wdt977_stop(void) |
| 132 | { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 133 | unsigned long flags; |
| 134 | spin_lock_irqsave(&spinlock, flags); |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* unlock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 137 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
| 138 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4 |
| 141 | * F3 is reset to its default state |
| 142 | * F4 can clear the TIMEOUT'ed state (bit 0) - back to default |
| 143 | * We can not use GP17 as a PowerLed, as we use its usage as a RedLed |
| 144 | */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 145 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 146 | outb_p(0x08, IO_DATA_PORT); |
| 147 | outb_p(0xF2, IO_INDEX_PORT); |
| 148 | outb_p(0xFF, IO_DATA_PORT); |
| 149 | outb_p(0xF3, IO_INDEX_PORT); |
| 150 | outb_p(0x00, IO_DATA_PORT); |
| 151 | outb_p(0xF4, IO_INDEX_PORT); |
| 152 | outb_p(0x00, IO_DATA_PORT); |
| 153 | outb_p(0xF2, IO_INDEX_PORT); |
| 154 | outb_p(0x00, IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 156 | /* at last select device Aux1 (dev=7) and set |
| 157 | GP16 as a watchdog output */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 158 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 159 | outb_p(0x07, IO_DATA_PORT); |
| 160 | outb_p(0xE6, IO_INDEX_PORT); |
| 161 | outb_p(0x08, IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | /* lock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 164 | outb_p(LOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 166 | spin_unlock_irqrestore(&spinlock, flags); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 167 | pr_info("shutdown\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Send a keepalive ping to the watchdog |
| 174 | * This is done by simply re-writing the timeout to reg. 0xF2 |
| 175 | */ |
| 176 | |
| 177 | static int wdt977_keepalive(void) |
| 178 | { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 179 | unsigned long flags; |
| 180 | spin_lock_irqsave(&spinlock, flags); |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | /* unlock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 183 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
| 184 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* select device Aux2 (device=8) and kicks watchdog reg F2 */ |
| 187 | /* F2 has the timeout in minutes */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 188 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 189 | outb_p(0x08, IO_DATA_PORT); |
| 190 | outb_p(0xF2, IO_INDEX_PORT); |
| 191 | outb_p(timeoutM, IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | |
| 193 | /* lock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 194 | outb_p(LOCK_DATA, IO_INDEX_PORT); |
| 195 | spin_unlock_irqrestore(&spinlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Set the watchdog timeout value |
| 202 | */ |
| 203 | |
| 204 | static int wdt977_set_timeout(int t) |
| 205 | { |
| 206 | int tmrval; |
| 207 | |
| 208 | /* convert seconds to minutes, rounding up */ |
| 209 | tmrval = (t + 59) / 60; |
| 210 | |
| 211 | if (machine_is_netwinder()) { |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 212 | /* we have a hw bug somewhere, so each 977 minute is actually |
| 213 | * only 30sec. This limits the max timeout to half of device |
| 214 | * max of 255 minutes... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | */ |
| 216 | tmrval += tmrval; |
| 217 | } |
| 218 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 219 | if (tmrval < 1 || tmrval > 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 222 | /* timeout is the timeout in seconds, timeoutM is |
| 223 | the timeout in minutes) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | timeout = t; |
| 225 | timeoutM = tmrval; |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Get the watchdog status |
| 231 | */ |
| 232 | |
| 233 | static int wdt977_get_status(int *status) |
| 234 | { |
| 235 | int new_status; |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 236 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 238 | spin_lock_irqsave(&spinlock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | /* unlock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 241 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
| 242 | outb_p(UNLOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | /* select device Aux2 (device=8) and read watchdog reg F4 */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 245 | outb_p(DEVICE_REGISTER, IO_INDEX_PORT); |
| 246 | outb_p(0x08, IO_DATA_PORT); |
| 247 | outb_p(0xF4, IO_INDEX_PORT); |
| 248 | new_status = inb_p(IO_DATA_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | /* lock the SuperIO chip */ |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 251 | outb_p(LOCK_DATA, IO_INDEX_PORT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 253 | spin_unlock_irqrestore(&spinlock, flags); |
| 254 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 255 | *status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (new_status & 1) |
| 257 | *status |= WDIOF_CARDRESET; |
| 258 | |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* |
| 264 | * /dev/watchdog handling |
| 265 | */ |
| 266 | |
| 267 | static int wdt977_open(struct inode *inode, struct file *file) |
| 268 | { |
| 269 | /* If the watchdog is alive we don't need to start it again */ |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 270 | if (test_and_set_bit(0, &timer_alive)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | return -EBUSY; |
| 272 | |
| 273 | if (nowayout) |
| 274 | __module_get(THIS_MODULE); |
| 275 | |
| 276 | wdt977_start(); |
| 277 | return nonseekable_open(inode, file); |
| 278 | } |
| 279 | |
| 280 | static int wdt977_release(struct inode *inode, struct file *file) |
| 281 | { |
| 282 | /* |
| 283 | * Shut off the timer. |
Wim Van Sebroeck | 5f3b275 | 2011-02-23 20:04:38 +0000 | [diff] [blame] | 284 | * Lock it in if it's a module and we set nowayout |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | */ |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 286 | if (expect_close == 42) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | wdt977_stop(); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 288 | clear_bit(0, &timer_alive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | wdt977_keepalive(); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 291 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | expect_close = 0; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /* |
| 299 | * wdt977_write: |
| 300 | * @file: file handle to the watchdog |
| 301 | * @buf: buffer to write (unused as data does not matter here |
| 302 | * @count: count of bytes |
| 303 | * @ppos: pointer to the position to write. No seeks allowed |
| 304 | * |
| 305 | * A write to a watchdog device is defined as a keepalive signal. Any |
| 306 | * write of data will do, as we we don't define content meaning. |
| 307 | */ |
| 308 | |
| 309 | static ssize_t wdt977_write(struct file *file, const char __user *buf, |
| 310 | size_t count, loff_t *ppos) |
| 311 | { |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 312 | if (count) { |
| 313 | if (!nowayout) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | size_t i; |
| 315 | |
| 316 | /* In case it was set long ago */ |
| 317 | expect_close = 0; |
| 318 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 319 | for (i = 0; i != count; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | char c; |
| 321 | if (get_user(c, buf + i)) |
| 322 | return -EFAULT; |
| 323 | if (c == 'V') |
| 324 | expect_close = 42; |
| 325 | } |
| 326 | } |
| 327 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 328 | /* someone wrote to us, we should restart timer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | wdt977_keepalive(); |
| 330 | } |
| 331 | return count; |
| 332 | } |
| 333 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 334 | static const struct watchdog_info ident = { |
| 335 | .options = WDIOF_SETTIMEOUT | |
| 336 | WDIOF_MAGICCLOSE | |
| 337 | WDIOF_KEEPALIVEPING, |
| 338 | .firmware_version = 1, |
| 339 | .identity = WATCHDOG_NAME, |
| 340 | }; |
| 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | /* |
| 343 | * wdt977_ioctl: |
| 344 | * @inode: inode of the device |
| 345 | * @file: file handle to the device |
| 346 | * @cmd: watchdog command |
| 347 | * @arg: argument pointer |
| 348 | * |
| 349 | * The watchdog API defines a common set of functions for all watchdogs |
| 350 | * according to their available features. |
| 351 | */ |
| 352 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 353 | static long wdt977_ioctl(struct file *file, unsigned int cmd, |
| 354 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | int status; |
| 357 | int new_options, retval = -EINVAL; |
| 358 | int new_timeout; |
| 359 | union { |
| 360 | struct watchdog_info __user *ident; |
| 361 | int __user *i; |
| 362 | } uarg; |
| 363 | |
| 364 | uarg.i = (int __user *)arg; |
| 365 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 366 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | case WDIOC_GETSUPPORT: |
| 368 | return copy_to_user(uarg.ident, &ident, |
| 369 | sizeof(ident)) ? -EFAULT : 0; |
| 370 | |
| 371 | case WDIOC_GETSTATUS: |
| 372 | wdt977_get_status(&status); |
| 373 | return put_user(status, uarg.i); |
| 374 | |
| 375 | case WDIOC_GETBOOTSTATUS: |
| 376 | return put_user(0, uarg.i); |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | case WDIOC_SETOPTIONS: |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 379 | if (get_user(new_options, uarg.i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return -EFAULT; |
| 381 | |
| 382 | if (new_options & WDIOS_DISABLECARD) { |
| 383 | wdt977_stop(); |
| 384 | retval = 0; |
| 385 | } |
| 386 | |
| 387 | if (new_options & WDIOS_ENABLECARD) { |
| 388 | wdt977_start(); |
| 389 | retval = 0; |
| 390 | } |
| 391 | |
| 392 | return retval; |
| 393 | |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 394 | case WDIOC_KEEPALIVE: |
| 395 | wdt977_keepalive(); |
| 396 | return 0; |
| 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | case WDIOC_SETTIMEOUT: |
| 399 | if (get_user(new_timeout, uarg.i)) |
| 400 | return -EFAULT; |
| 401 | |
| 402 | if (wdt977_set_timeout(new_timeout)) |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 403 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | wdt977_keepalive(); |
| 406 | /* Fall */ |
| 407 | |
| 408 | case WDIOC_GETTIMEOUT: |
| 409 | return put_user(timeout, uarg.i); |
| 410 | |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 411 | default: |
| 412 | return -ENOTTY; |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | static int wdt977_notify_sys(struct notifier_block *this, unsigned long code, |
| 418 | void *unused) |
| 419 | { |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 420 | if (code == SYS_DOWN || code == SYS_HALT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | wdt977_stop(); |
| 422 | return NOTIFY_DONE; |
| 423 | } |
| 424 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 425 | static const struct file_operations wdt977_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | .owner = THIS_MODULE, |
| 427 | .llseek = no_llseek, |
| 428 | .write = wdt977_write, |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 429 | .unlocked_ioctl = wdt977_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | .open = wdt977_open, |
| 431 | .release = wdt977_release, |
| 432 | }; |
| 433 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 434 | static struct miscdevice wdt977_miscdev = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | .minor = WATCHDOG_MINOR, |
| 436 | .name = "watchdog", |
| 437 | .fops = &wdt977_fops, |
| 438 | }; |
| 439 | |
| 440 | static struct notifier_block wdt977_notifier = { |
| 441 | .notifier_call = wdt977_notify_sys, |
| 442 | }; |
| 443 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 444 | static int __init wd977_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 446 | int rc; |
| 447 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 448 | pr_info("driver v%s\n", WATCHDOG_VERSION); |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 449 | |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 450 | /* Check that the timeout value is within its range; |
| 451 | if not reset to the default */ |
| 452 | if (wdt977_set_timeout(timeout)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | wdt977_set_timeout(DEFAULT_TIMEOUT); |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 454 | pr_info("timeout value must be 60 < timeout < 15300, using %d\n", |
| 455 | DEFAULT_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 458 | /* on Netwinder the IOports are already reserved by |
| 459 | * arch/arm/mach-footbridge/netwinder-hw.c |
| 460 | */ |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 461 | if (!machine_is_netwinder()) { |
| 462 | if (!request_region(IO_INDEX_PORT, 2, WATCHDOG_NAME)) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 463 | pr_err("I/O address 0x%04x already in use\n", |
| 464 | IO_INDEX_PORT); |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 465 | rc = -EIO; |
| 466 | goto err_out; |
| 467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 470 | rc = register_reboot_notifier(&wdt977_notifier); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 471 | if (rc) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 472 | pr_err("cannot register reboot notifier (err=%d)\n", rc); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 473 | goto err_out_region; |
| 474 | } |
| 475 | |
| 476 | rc = misc_register(&wdt977_miscdev); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 477 | if (rc) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 478 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
| 479 | wdt977_miscdev.minor, rc); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 480 | goto err_out_reboot; |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame^] | 483 | pr_info("initialized. timeout=%d sec (nowayout=%d, testmode=%i)\n", |
| 484 | timeout, nowayout, testmode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
| 486 | return 0; |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 487 | |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 488 | err_out_reboot: |
| 489 | unregister_reboot_notifier(&wdt977_notifier); |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 490 | err_out_region: |
| 491 | if (!machine_is_netwinder()) |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 492 | release_region(IO_INDEX_PORT, 2); |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 493 | err_out: |
| 494 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 497 | static void __exit wd977_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 499 | wdt977_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | misc_deregister(&wdt977_miscdev); |
| 501 | unregister_reboot_notifier(&wdt977_notifier); |
Alan Cox | f2b79c6 | 2008-05-19 14:09:57 +0100 | [diff] [blame] | 502 | release_region(IO_INDEX_PORT, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 505 | module_init(wd977_init); |
| 506 | module_exit(wd977_exit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Woody Suwalski | 4dab06f | 2006-01-08 01:00:31 -0800 | [diff] [blame] | 508 | MODULE_AUTHOR("Woody Suwalski <woodys@xandros.com>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | MODULE_DESCRIPTION("W83977AF Watchdog driver"); |
| 510 | MODULE_LICENSE("GPL"); |
| 511 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |