Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2006 by Hans Edgington <hans@edgington.nl> * |
| 3 | * Copyright (C) 2007-2009 Hans de Goede <hdegoede@redhat.com> * |
| 4 | * Copyright (C) 2010 Giel van Schijndel <me@mortis.eu> * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/fs.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/ioport.h> |
| 27 | #include <linux/miscdevice.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/notifier.h> |
| 31 | #include <linux/reboot.h> |
| 32 | #include <linux/uaccess.h> |
| 33 | #include <linux/watchdog.h> |
| 34 | |
| 35 | #define DRVNAME "f71808e_wdt" |
| 36 | |
| 37 | #define SIO_F71808FG_LD_WDT 0x07 /* Watchdog timer logical device */ |
| 38 | #define SIO_UNLOCK_KEY 0x87 /* Key to enable Super-I/O */ |
| 39 | #define SIO_LOCK_KEY 0xAA /* Key to diasble Super-I/O */ |
| 40 | |
| 41 | #define SIO_REG_LDSEL 0x07 /* Logical device select */ |
| 42 | #define SIO_REG_DEVID 0x20 /* Device ID (2 bytes) */ |
| 43 | #define SIO_REG_DEVREV 0x22 /* Device revision */ |
| 44 | #define SIO_REG_MANID 0x23 /* Fintek ID (2 bytes) */ |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 45 | #define SIO_REG_MFUNCT1 0x29 /* Multi function select 1 */ |
| 46 | #define SIO_REG_MFUNCT2 0x2a /* Multi function select 2 */ |
| 47 | #define SIO_REG_MFUNCT3 0x2b /* Multi function select 3 */ |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 48 | #define SIO_REG_ENABLE 0x30 /* Logical device enable */ |
| 49 | #define SIO_REG_ADDR 0x60 /* Logical device address (2 bytes) */ |
| 50 | |
| 51 | #define SIO_FINTEK_ID 0x1934 /* Manufacturers ID */ |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 52 | #define SIO_F71808_ID 0x0901 /* Chipset ID */ |
| 53 | #define SIO_F71858_ID 0x0507 /* Chipset ID */ |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 54 | #define SIO_F71862_ID 0x0601 /* Chipset ID */ |
| 55 | #define SIO_F71882_ID 0x0541 /* Chipset ID */ |
| 56 | #define SIO_F71889_ID 0x0723 /* Chipset ID */ |
| 57 | |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 58 | #define F71808FG_REG_WDO_CONF 0xf0 |
| 59 | #define F71808FG_REG_WDT_CONF 0xf5 |
| 60 | #define F71808FG_REG_WD_TIME 0xf6 |
| 61 | |
| 62 | #define F71808FG_FLAG_WDOUT_EN 7 |
| 63 | |
| 64 | #define F71808FG_FLAG_WDTMOUT_STS 5 |
| 65 | #define F71808FG_FLAG_WD_EN 5 |
| 66 | #define F71808FG_FLAG_WD_PULSE 4 |
| 67 | #define F71808FG_FLAG_WD_UNIT 3 |
| 68 | |
| 69 | /* Default values */ |
| 70 | #define WATCHDOG_TIMEOUT 60 /* 1 minute default timeout */ |
| 71 | #define WATCHDOG_MAX_TIMEOUT (60 * 255) |
| 72 | #define WATCHDOG_PULSE_WIDTH 125 /* 125 ms, default pulse width for |
| 73 | watchdog signal */ |
| 74 | |
| 75 | static unsigned short force_id; |
| 76 | module_param(force_id, ushort, 0); |
| 77 | MODULE_PARM_DESC(force_id, "Override the detected device ID"); |
| 78 | |
| 79 | static const int max_timeout = WATCHDOG_MAX_TIMEOUT; |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 80 | static int timeout = WATCHDOG_TIMEOUT; /* default timeout in seconds */ |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 81 | module_param(timeout, int, 0); |
| 82 | MODULE_PARM_DESC(timeout, |
| 83 | "Watchdog timeout in seconds. 1<= timeout <=" |
| 84 | __MODULE_STRING(WATCHDOG_MAX_TIMEOUT) " (default=" |
| 85 | __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); |
| 86 | |
| 87 | static unsigned int pulse_width = WATCHDOG_PULSE_WIDTH; |
| 88 | module_param(pulse_width, uint, 0); |
| 89 | MODULE_PARM_DESC(pulse_width, |
| 90 | "Watchdog signal pulse width. 0(=level), 1 ms, 25 ms, 125 ms or 5000 ms" |
| 91 | " (default=" __MODULE_STRING(WATCHDOG_PULSE_WIDTH) ")"); |
| 92 | |
| 93 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 94 | module_param(nowayout, bool, 0444); |
| 95 | MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close"); |
| 96 | |
| 97 | static unsigned int start_withtimeout; |
| 98 | module_param(start_withtimeout, uint, 0); |
| 99 | MODULE_PARM_DESC(start_withtimeout, "Start watchdog timer on module load with" |
| 100 | " given initial timeout. Zero (default) disables this feature."); |
| 101 | |
| 102 | enum chips { f71808fg, f71858fg, f71862fg, f71882fg, f71889fg }; |
| 103 | |
| 104 | static const char *f71808e_names[] = { |
| 105 | "f71808fg", |
| 106 | "f71858fg", |
| 107 | "f71862fg", |
| 108 | "f71882fg", |
| 109 | "f71889fg", |
| 110 | }; |
| 111 | |
| 112 | /* Super-I/O Function prototypes */ |
| 113 | static inline int superio_inb(int base, int reg); |
| 114 | static inline int superio_inw(int base, int reg); |
| 115 | static inline void superio_outb(int base, int reg, u8 val); |
| 116 | static inline void superio_set_bit(int base, int reg, int bit); |
| 117 | static inline void superio_clear_bit(int base, int reg, int bit); |
| 118 | static inline int superio_enter(int base); |
| 119 | static inline void superio_select(int base, int ld); |
| 120 | static inline void superio_exit(int base); |
| 121 | |
| 122 | struct watchdog_data { |
| 123 | unsigned short sioaddr; |
| 124 | enum chips type; |
| 125 | unsigned long opened; |
| 126 | struct mutex lock; |
| 127 | char expect_close; |
| 128 | struct watchdog_info ident; |
| 129 | |
| 130 | unsigned short timeout; |
| 131 | u8 timer_val; /* content for the wd_time register */ |
| 132 | char minutes_mode; |
| 133 | u8 pulse_val; /* pulse width flag */ |
| 134 | char pulse_mode; /* enable pulse output mode? */ |
| 135 | char caused_reboot; /* last reboot was by the watchdog */ |
| 136 | }; |
| 137 | |
| 138 | static struct watchdog_data watchdog = { |
| 139 | .lock = __MUTEX_INITIALIZER(watchdog.lock), |
| 140 | }; |
| 141 | |
| 142 | /* Super I/O functions */ |
| 143 | static inline int superio_inb(int base, int reg) |
| 144 | { |
| 145 | outb(reg, base); |
| 146 | return inb(base + 1); |
| 147 | } |
| 148 | |
| 149 | static int superio_inw(int base, int reg) |
| 150 | { |
| 151 | int val; |
| 152 | val = superio_inb(base, reg) << 8; |
| 153 | val |= superio_inb(base, reg + 1); |
| 154 | return val; |
| 155 | } |
| 156 | |
| 157 | static inline void superio_outb(int base, int reg, u8 val) |
| 158 | { |
| 159 | outb(reg, base); |
| 160 | outb(val, base + 1); |
| 161 | } |
| 162 | |
| 163 | static inline void superio_set_bit(int base, int reg, int bit) |
| 164 | { |
| 165 | unsigned long val = superio_inb(base, reg); |
| 166 | __set_bit(bit, &val); |
| 167 | superio_outb(base, reg, val); |
| 168 | } |
| 169 | |
| 170 | static inline void superio_clear_bit(int base, int reg, int bit) |
| 171 | { |
| 172 | unsigned long val = superio_inb(base, reg); |
| 173 | __clear_bit(bit, &val); |
| 174 | superio_outb(base, reg, val); |
| 175 | } |
| 176 | |
| 177 | static inline int superio_enter(int base) |
| 178 | { |
| 179 | /* Don't step on other drivers' I/O space by accident */ |
| 180 | if (!request_muxed_region(base, 2, DRVNAME)) { |
| 181 | printk(KERN_ERR DRVNAME ": I/O address 0x%04x already in use\n", |
| 182 | (int)base); |
| 183 | return -EBUSY; |
| 184 | } |
| 185 | |
| 186 | /* according to the datasheet the key must be send twice! */ |
| 187 | outb(SIO_UNLOCK_KEY, base); |
| 188 | outb(SIO_UNLOCK_KEY, base); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static inline void superio_select(int base, int ld) |
| 194 | { |
| 195 | outb(SIO_REG_LDSEL, base); |
| 196 | outb(ld, base + 1); |
| 197 | } |
| 198 | |
| 199 | static inline void superio_exit(int base) |
| 200 | { |
| 201 | outb(SIO_LOCK_KEY, base); |
| 202 | release_region(base, 2); |
| 203 | } |
| 204 | |
| 205 | static int watchdog_set_timeout(int timeout) |
| 206 | { |
| 207 | if (timeout <= 0 |
| 208 | || timeout > max_timeout) { |
| 209 | printk(KERN_ERR DRVNAME ": watchdog timeout out of range\n"); |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
| 213 | mutex_lock(&watchdog.lock); |
| 214 | |
| 215 | watchdog.timeout = timeout; |
| 216 | if (timeout > 0xff) { |
| 217 | watchdog.timer_val = DIV_ROUND_UP(timeout, 60); |
| 218 | watchdog.minutes_mode = true; |
| 219 | } else { |
| 220 | watchdog.timer_val = timeout; |
| 221 | watchdog.minutes_mode = false; |
| 222 | } |
| 223 | |
| 224 | mutex_unlock(&watchdog.lock); |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int watchdog_set_pulse_width(unsigned int pw) |
| 230 | { |
| 231 | int err = 0; |
| 232 | |
| 233 | mutex_lock(&watchdog.lock); |
| 234 | |
| 235 | if (pw <= 1) { |
| 236 | watchdog.pulse_val = 0; |
| 237 | } else if (pw <= 25) { |
| 238 | watchdog.pulse_val = 1; |
| 239 | } else if (pw <= 125) { |
| 240 | watchdog.pulse_val = 2; |
| 241 | } else if (pw <= 5000) { |
| 242 | watchdog.pulse_val = 3; |
| 243 | } else { |
| 244 | printk(KERN_ERR DRVNAME ": pulse width out of range\n"); |
| 245 | err = -EINVAL; |
| 246 | goto exit_unlock; |
| 247 | } |
| 248 | |
| 249 | watchdog.pulse_mode = pw; |
| 250 | |
| 251 | exit_unlock: |
| 252 | mutex_unlock(&watchdog.lock); |
| 253 | return err; |
| 254 | } |
| 255 | |
| 256 | static int watchdog_keepalive(void) |
| 257 | { |
| 258 | int err = 0; |
| 259 | |
| 260 | mutex_lock(&watchdog.lock); |
| 261 | err = superio_enter(watchdog.sioaddr); |
| 262 | if (err) |
| 263 | goto exit_unlock; |
| 264 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 265 | |
| 266 | if (watchdog.minutes_mode) |
| 267 | /* select minutes for timer units */ |
| 268 | superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 269 | F71808FG_FLAG_WD_UNIT); |
| 270 | else |
| 271 | /* select seconds for timer units */ |
| 272 | superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 273 | F71808FG_FLAG_WD_UNIT); |
| 274 | |
| 275 | /* Set timer value */ |
| 276 | superio_outb(watchdog.sioaddr, F71808FG_REG_WD_TIME, |
| 277 | watchdog.timer_val); |
| 278 | |
| 279 | superio_exit(watchdog.sioaddr); |
| 280 | |
| 281 | exit_unlock: |
| 282 | mutex_unlock(&watchdog.lock); |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | static int watchdog_start(void) |
| 287 | { |
| 288 | /* Make sure we don't die as soon as the watchdog is enabled below */ |
| 289 | int err = watchdog_keepalive(); |
| 290 | if (err) |
| 291 | return err; |
| 292 | |
| 293 | mutex_lock(&watchdog.lock); |
| 294 | err = superio_enter(watchdog.sioaddr); |
| 295 | if (err) |
| 296 | goto exit_unlock; |
| 297 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 298 | |
| 299 | /* Watchdog pin configuration */ |
| 300 | switch (watchdog.type) { |
| 301 | case f71808fg: |
| 302 | /* Set pin 21 to GPIO23/WDTRST#, then to WDTRST# */ |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 303 | superio_clear_bit(watchdog.sioaddr, SIO_REG_MFUNCT2, 3); |
| 304 | superio_clear_bit(watchdog.sioaddr, SIO_REG_MFUNCT3, 3); |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 305 | break; |
| 306 | |
| 307 | case f71882fg: |
| 308 | /* Set pin 56 to WDTRST# */ |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 309 | superio_set_bit(watchdog.sioaddr, SIO_REG_MFUNCT1, 1); |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 310 | break; |
| 311 | |
Giel van Schijndel | dee00ab | 2010-10-04 10:45:28 +0200 | [diff] [blame] | 312 | case f71889fg: |
| 313 | /* set pin 40 to WDTRST# */ |
Lutz Ballaschke | f9a9f09 | 2010-09-26 16:38:20 +0200 | [diff] [blame^] | 314 | superio_outb(watchdog.sioaddr, SIO_REG_MFUNCT3, |
| 315 | superio_inb(watchdog.sioaddr, SIO_REG_MFUNCT3) & 0xcf); |
Giel van Schijndel | dee00ab | 2010-10-04 10:45:28 +0200 | [diff] [blame] | 316 | break; |
| 317 | |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 318 | default: |
| 319 | /* |
| 320 | * 'default' label to shut up the compiler and catch |
| 321 | * programmer errors |
| 322 | */ |
| 323 | err = -ENODEV; |
| 324 | goto exit_superio; |
| 325 | } |
| 326 | |
| 327 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 328 | superio_set_bit(watchdog.sioaddr, SIO_REG_ENABLE, 0); |
| 329 | superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDO_CONF, |
| 330 | F71808FG_FLAG_WDOUT_EN); |
| 331 | |
| 332 | superio_set_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 333 | F71808FG_FLAG_WD_EN); |
| 334 | |
| 335 | if (watchdog.pulse_mode) { |
| 336 | /* Select "pulse" output mode with given duration */ |
| 337 | u8 wdt_conf = superio_inb(watchdog.sioaddr, |
| 338 | F71808FG_REG_WDT_CONF); |
| 339 | |
| 340 | /* Set WD_PSWIDTH bits (1:0) */ |
| 341 | wdt_conf = (wdt_conf & 0xfc) | (watchdog.pulse_val & 0x03); |
| 342 | /* Set WD_PULSE to "pulse" mode */ |
| 343 | wdt_conf |= BIT(F71808FG_FLAG_WD_PULSE); |
| 344 | |
| 345 | superio_outb(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 346 | wdt_conf); |
| 347 | } else { |
| 348 | /* Select "level" output mode */ |
| 349 | superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 350 | F71808FG_FLAG_WD_PULSE); |
| 351 | } |
| 352 | |
| 353 | exit_superio: |
| 354 | superio_exit(watchdog.sioaddr); |
| 355 | exit_unlock: |
| 356 | mutex_unlock(&watchdog.lock); |
| 357 | |
| 358 | return err; |
| 359 | } |
| 360 | |
| 361 | static int watchdog_stop(void) |
| 362 | { |
| 363 | int err = 0; |
| 364 | |
| 365 | mutex_lock(&watchdog.lock); |
| 366 | err = superio_enter(watchdog.sioaddr); |
| 367 | if (err) |
| 368 | goto exit_unlock; |
| 369 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 370 | |
| 371 | superio_clear_bit(watchdog.sioaddr, F71808FG_REG_WDT_CONF, |
| 372 | F71808FG_FLAG_WD_EN); |
| 373 | |
| 374 | superio_exit(watchdog.sioaddr); |
| 375 | |
| 376 | exit_unlock: |
| 377 | mutex_unlock(&watchdog.lock); |
| 378 | |
| 379 | return err; |
| 380 | } |
| 381 | |
| 382 | static int watchdog_get_status(void) |
| 383 | { |
| 384 | int status = 0; |
| 385 | |
| 386 | mutex_lock(&watchdog.lock); |
| 387 | status = (watchdog.caused_reboot) ? WDIOF_CARDRESET : 0; |
| 388 | mutex_unlock(&watchdog.lock); |
| 389 | |
| 390 | return status; |
| 391 | } |
| 392 | |
| 393 | static bool watchdog_is_running(void) |
| 394 | { |
| 395 | /* |
| 396 | * if we fail to determine the watchdog's status assume it to be |
| 397 | * running to be on the safe side |
| 398 | */ |
| 399 | bool is_running = true; |
| 400 | |
| 401 | mutex_lock(&watchdog.lock); |
| 402 | if (superio_enter(watchdog.sioaddr)) |
| 403 | goto exit_unlock; |
| 404 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 405 | |
| 406 | is_running = (superio_inb(watchdog.sioaddr, SIO_REG_ENABLE) & BIT(0)) |
| 407 | && (superio_inb(watchdog.sioaddr, F71808FG_REG_WDT_CONF) |
| 408 | & F71808FG_FLAG_WD_EN); |
| 409 | |
| 410 | superio_exit(watchdog.sioaddr); |
| 411 | |
| 412 | exit_unlock: |
| 413 | mutex_unlock(&watchdog.lock); |
| 414 | return is_running; |
| 415 | } |
| 416 | |
| 417 | /* /dev/watchdog api */ |
| 418 | |
| 419 | static int watchdog_open(struct inode *inode, struct file *file) |
| 420 | { |
| 421 | int err; |
| 422 | |
| 423 | /* If the watchdog is alive we don't need to start it again */ |
| 424 | if (test_and_set_bit(0, &watchdog.opened)) |
| 425 | return -EBUSY; |
| 426 | |
| 427 | err = watchdog_start(); |
| 428 | if (err) { |
| 429 | clear_bit(0, &watchdog.opened); |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | if (nowayout) |
| 434 | __module_get(THIS_MODULE); |
| 435 | |
| 436 | watchdog.expect_close = 0; |
| 437 | return nonseekable_open(inode, file); |
| 438 | } |
| 439 | |
| 440 | static int watchdog_release(struct inode *inode, struct file *file) |
| 441 | { |
| 442 | clear_bit(0, &watchdog.opened); |
| 443 | |
| 444 | if (!watchdog.expect_close) { |
| 445 | watchdog_keepalive(); |
| 446 | printk(KERN_CRIT DRVNAME |
| 447 | ": Unexpected close, not stopping watchdog!\n"); |
| 448 | } else if (!nowayout) { |
| 449 | watchdog_stop(); |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * watchdog_write: |
| 456 | * @file: file handle to the watchdog |
| 457 | * @buf: buffer to write |
| 458 | * @count: count of bytes |
| 459 | * @ppos: pointer to the position to write. No seeks allowed |
| 460 | * |
| 461 | * A write to a watchdog device is defined as a keepalive signal. Any |
| 462 | * write of data will do, as we we don't define content meaning. |
| 463 | */ |
| 464 | |
| 465 | static ssize_t watchdog_write(struct file *file, const char __user *buf, |
| 466 | size_t count, loff_t *ppos) |
| 467 | { |
| 468 | if (count) { |
| 469 | if (!nowayout) { |
| 470 | size_t i; |
| 471 | |
| 472 | /* In case it was set long ago */ |
| 473 | bool expect_close = false; |
| 474 | |
| 475 | for (i = 0; i != count; i++) { |
| 476 | char c; |
| 477 | if (get_user(c, buf + i)) |
| 478 | return -EFAULT; |
| 479 | expect_close = (c == 'V'); |
| 480 | } |
| 481 | |
| 482 | /* Properly order writes across fork()ed processes */ |
| 483 | mutex_lock(&watchdog.lock); |
| 484 | watchdog.expect_close = expect_close; |
| 485 | mutex_unlock(&watchdog.lock); |
| 486 | } |
| 487 | |
| 488 | /* someone wrote to us, we should restart timer */ |
| 489 | watchdog_keepalive(); |
| 490 | } |
| 491 | return count; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * watchdog_ioctl: |
| 496 | * @inode: inode of the device |
| 497 | * @file: file handle to the device |
| 498 | * @cmd: watchdog command |
| 499 | * @arg: argument pointer |
| 500 | * |
| 501 | * The watchdog API defines a common set of functions for all watchdogs |
| 502 | * according to their available features. |
| 503 | */ |
| 504 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 505 | unsigned long arg) |
| 506 | { |
| 507 | int status; |
| 508 | int new_options; |
| 509 | int new_timeout; |
| 510 | union { |
| 511 | struct watchdog_info __user *ident; |
| 512 | int __user *i; |
| 513 | } uarg; |
| 514 | |
| 515 | uarg.i = (int __user *)arg; |
| 516 | |
| 517 | switch (cmd) { |
| 518 | case WDIOC_GETSUPPORT: |
| 519 | return copy_to_user(uarg.ident, &watchdog.ident, |
| 520 | sizeof(watchdog.ident)) ? -EFAULT : 0; |
| 521 | |
| 522 | case WDIOC_GETSTATUS: |
| 523 | status = watchdog_get_status(); |
| 524 | if (status < 0) |
| 525 | return status; |
| 526 | return put_user(status, uarg.i); |
| 527 | |
| 528 | case WDIOC_GETBOOTSTATUS: |
| 529 | return put_user(0, uarg.i); |
| 530 | |
| 531 | case WDIOC_SETOPTIONS: |
| 532 | if (get_user(new_options, uarg.i)) |
| 533 | return -EFAULT; |
| 534 | |
| 535 | if (new_options & WDIOS_DISABLECARD) |
| 536 | watchdog_stop(); |
| 537 | |
| 538 | if (new_options & WDIOS_ENABLECARD) |
| 539 | return watchdog_start(); |
| 540 | |
| 541 | |
| 542 | case WDIOC_KEEPALIVE: |
| 543 | watchdog_keepalive(); |
| 544 | return 0; |
| 545 | |
| 546 | case WDIOC_SETTIMEOUT: |
| 547 | if (get_user(new_timeout, uarg.i)) |
| 548 | return -EFAULT; |
| 549 | |
| 550 | if (watchdog_set_timeout(new_timeout)) |
| 551 | return -EINVAL; |
| 552 | |
| 553 | watchdog_keepalive(); |
| 554 | /* Fall */ |
| 555 | |
| 556 | case WDIOC_GETTIMEOUT: |
| 557 | return put_user(watchdog.timeout, uarg.i); |
| 558 | |
| 559 | default: |
| 560 | return -ENOTTY; |
| 561 | |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static int watchdog_notify_sys(struct notifier_block *this, unsigned long code, |
| 566 | void *unused) |
| 567 | { |
| 568 | if (code == SYS_DOWN || code == SYS_HALT) |
| 569 | watchdog_stop(); |
| 570 | return NOTIFY_DONE; |
| 571 | } |
| 572 | |
| 573 | static const struct file_operations watchdog_fops = { |
| 574 | .owner = THIS_MODULE, |
| 575 | .llseek = no_llseek, |
| 576 | .open = watchdog_open, |
| 577 | .release = watchdog_release, |
| 578 | .write = watchdog_write, |
| 579 | .unlocked_ioctl = watchdog_ioctl, |
| 580 | }; |
| 581 | |
| 582 | static struct miscdevice watchdog_miscdev = { |
| 583 | .minor = WATCHDOG_MINOR, |
| 584 | .name = "watchdog", |
| 585 | .fops = &watchdog_fops, |
| 586 | }; |
| 587 | |
| 588 | static struct notifier_block watchdog_notifier = { |
| 589 | .notifier_call = watchdog_notify_sys, |
| 590 | }; |
| 591 | |
| 592 | static int __init watchdog_init(int sioaddr) |
| 593 | { |
| 594 | int wdt_conf, err = 0; |
| 595 | |
| 596 | /* No need to lock watchdog.lock here because no entry points |
| 597 | * into the module have been registered yet. |
| 598 | */ |
| 599 | watchdog.sioaddr = sioaddr; |
| 600 | watchdog.ident.options = WDIOC_SETTIMEOUT |
| 601 | | WDIOF_MAGICCLOSE |
| 602 | | WDIOF_KEEPALIVEPING; |
| 603 | |
| 604 | snprintf(watchdog.ident.identity, |
| 605 | sizeof(watchdog.ident.identity), "%s watchdog", |
| 606 | f71808e_names[watchdog.type]); |
| 607 | |
| 608 | err = superio_enter(sioaddr); |
| 609 | if (err) |
| 610 | return err; |
| 611 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 612 | |
| 613 | wdt_conf = superio_inb(sioaddr, F71808FG_REG_WDT_CONF); |
| 614 | watchdog.caused_reboot = wdt_conf & F71808FG_FLAG_WDTMOUT_STS; |
| 615 | |
| 616 | superio_exit(sioaddr); |
| 617 | |
| 618 | err = watchdog_set_timeout(timeout); |
| 619 | if (err) |
| 620 | return err; |
| 621 | err = watchdog_set_pulse_width(pulse_width); |
| 622 | if (err) |
| 623 | return err; |
| 624 | |
| 625 | err = register_reboot_notifier(&watchdog_notifier); |
| 626 | if (err) |
| 627 | return err; |
| 628 | |
| 629 | err = misc_register(&watchdog_miscdev); |
| 630 | if (err) { |
| 631 | printk(KERN_ERR DRVNAME |
| 632 | ": cannot register miscdev on minor=%d\n", |
| 633 | watchdog_miscdev.minor); |
| 634 | goto exit_reboot; |
| 635 | } |
| 636 | |
| 637 | if (start_withtimeout) { |
| 638 | if (start_withtimeout <= 0 |
| 639 | || start_withtimeout > max_timeout) { |
| 640 | printk(KERN_ERR DRVNAME |
| 641 | ": starting timeout out of range\n"); |
| 642 | err = -EINVAL; |
| 643 | goto exit_miscdev; |
| 644 | } |
| 645 | |
| 646 | err = watchdog_start(); |
| 647 | if (err) { |
| 648 | printk(KERN_ERR DRVNAME |
| 649 | ": cannot start watchdog timer\n"); |
| 650 | goto exit_miscdev; |
| 651 | } |
| 652 | |
| 653 | mutex_lock(&watchdog.lock); |
| 654 | err = superio_enter(sioaddr); |
| 655 | if (err) |
| 656 | goto exit_unlock; |
| 657 | superio_select(watchdog.sioaddr, SIO_F71808FG_LD_WDT); |
| 658 | |
| 659 | if (start_withtimeout > 0xff) { |
| 660 | /* select minutes for timer units */ |
| 661 | superio_set_bit(sioaddr, F71808FG_REG_WDT_CONF, |
| 662 | F71808FG_FLAG_WD_UNIT); |
| 663 | superio_outb(sioaddr, F71808FG_REG_WD_TIME, |
| 664 | DIV_ROUND_UP(start_withtimeout, 60)); |
| 665 | } else { |
| 666 | /* select seconds for timer units */ |
| 667 | superio_clear_bit(sioaddr, F71808FG_REG_WDT_CONF, |
| 668 | F71808FG_FLAG_WD_UNIT); |
| 669 | superio_outb(sioaddr, F71808FG_REG_WD_TIME, |
| 670 | start_withtimeout); |
| 671 | } |
| 672 | |
| 673 | superio_exit(sioaddr); |
| 674 | mutex_unlock(&watchdog.lock); |
| 675 | |
| 676 | if (nowayout) |
| 677 | __module_get(THIS_MODULE); |
| 678 | |
| 679 | printk(KERN_INFO DRVNAME |
| 680 | ": watchdog started with initial timeout of %u sec\n", |
| 681 | start_withtimeout); |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | |
| 686 | exit_unlock: |
| 687 | mutex_unlock(&watchdog.lock); |
| 688 | exit_miscdev: |
| 689 | misc_deregister(&watchdog_miscdev); |
| 690 | exit_reboot: |
| 691 | unregister_reboot_notifier(&watchdog_notifier); |
| 692 | |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | static int __init f71808e_find(int sioaddr) |
| 697 | { |
| 698 | u16 devid; |
| 699 | int err = superio_enter(sioaddr); |
| 700 | if (err) |
| 701 | return err; |
| 702 | |
| 703 | devid = superio_inw(sioaddr, SIO_REG_MANID); |
| 704 | if (devid != SIO_FINTEK_ID) { |
| 705 | pr_debug(DRVNAME ": Not a Fintek device\n"); |
| 706 | err = -ENODEV; |
| 707 | goto exit; |
| 708 | } |
| 709 | |
| 710 | devid = force_id ? force_id : superio_inw(sioaddr, SIO_REG_DEVID); |
| 711 | switch (devid) { |
| 712 | case SIO_F71808_ID: |
| 713 | watchdog.type = f71808fg; |
| 714 | break; |
| 715 | case SIO_F71882_ID: |
| 716 | watchdog.type = f71882fg; |
| 717 | break; |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 718 | case SIO_F71889_ID: |
Giel van Schijndel | dee00ab | 2010-10-04 10:45:28 +0200 | [diff] [blame] | 719 | watchdog.type = f71889fg; |
| 720 | break; |
| 721 | case SIO_F71862_ID: |
Giel van Schijndel | 96cb4eb | 2010-08-01 15:30:55 +0200 | [diff] [blame] | 722 | /* These have a watchdog, though it isn't implemented (yet). */ |
| 723 | err = -ENOSYS; |
| 724 | goto exit; |
| 725 | case SIO_F71858_ID: |
| 726 | /* Confirmed (by datasheet) not to have a watchdog. */ |
| 727 | err = -ENODEV; |
| 728 | goto exit; |
| 729 | default: |
| 730 | printk(KERN_INFO DRVNAME ": Unrecognized Fintek device: %04x\n", |
| 731 | (unsigned int)devid); |
| 732 | err = -ENODEV; |
| 733 | goto exit; |
| 734 | } |
| 735 | |
| 736 | printk(KERN_INFO DRVNAME ": Found %s watchdog chip, revision %d\n", |
| 737 | f71808e_names[watchdog.type], |
| 738 | (int)superio_inb(sioaddr, SIO_REG_DEVREV)); |
| 739 | exit: |
| 740 | superio_exit(sioaddr); |
| 741 | return err; |
| 742 | } |
| 743 | |
| 744 | static int __init f71808e_init(void) |
| 745 | { |
| 746 | static const unsigned short addrs[] = { 0x2e, 0x4e }; |
| 747 | int err = -ENODEV; |
| 748 | int i; |
| 749 | |
| 750 | for (i = 0; i < ARRAY_SIZE(addrs); i++) { |
| 751 | err = f71808e_find(addrs[i]); |
| 752 | if (err == 0) |
| 753 | break; |
| 754 | } |
| 755 | if (i == ARRAY_SIZE(addrs)) |
| 756 | return err; |
| 757 | |
| 758 | return watchdog_init(addrs[i]); |
| 759 | } |
| 760 | |
| 761 | static void __exit f71808e_exit(void) |
| 762 | { |
| 763 | if (watchdog_is_running()) { |
| 764 | printk(KERN_WARNING DRVNAME |
| 765 | ": Watchdog timer still running, stopping it\n"); |
| 766 | watchdog_stop(); |
| 767 | } |
| 768 | misc_deregister(&watchdog_miscdev); |
| 769 | unregister_reboot_notifier(&watchdog_notifier); |
| 770 | } |
| 771 | |
| 772 | MODULE_DESCRIPTION("F71808E Watchdog Driver"); |
| 773 | MODULE_AUTHOR("Giel van Schijndel <me@mortis.eu>"); |
| 774 | MODULE_LICENSE("GPL"); |
| 775 | |
| 776 | module_init(f71808e_init); |
| 777 | module_exit(f71808e_exit); |