Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Wdt977 0.03: A Watchdog Device for Netwinder W83977AF chip |
| 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. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/moduleparam.h> |
| 25 | #include <linux/config.h> |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/miscdevice.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/watchdog.h> |
| 32 | #include <linux/notifier.h> |
| 33 | #include <linux/reboot.h> |
| 34 | |
| 35 | #include <asm/io.h> |
| 36 | #include <asm/system.h> |
| 37 | #include <asm/mach-types.h> |
| 38 | #include <asm/uaccess.h> |
| 39 | |
| 40 | #define PFX "Wdt977: " |
| 41 | #define WATCHDOG_MINOR 130 |
| 42 | |
| 43 | #define DEFAULT_TIMEOUT 60 /* default timeout in seconds */ |
| 44 | |
| 45 | static int timeout = DEFAULT_TIMEOUT; |
| 46 | static int timeoutM; /* timeout in minutes */ |
| 47 | static unsigned long timer_alive; |
| 48 | static int testmode; |
| 49 | static char expect_close; |
| 50 | |
| 51 | module_param(timeout, int, 0); |
| 52 | MODULE_PARM_DESC(timeout,"Watchdog timeout in seconds (60..15300), default=" __MODULE_STRING(DEFAULT_TIMEOUT) ")"); |
| 53 | module_param(testmode, int, 0); |
| 54 | MODULE_PARM_DESC(testmode,"Watchdog testmode (1 = no reboot), default=0"); |
| 55 | |
| 56 | #ifdef CONFIG_WATCHDOG_NOWAYOUT |
| 57 | static int nowayout = 1; |
| 58 | #else |
| 59 | static int nowayout = 0; |
| 60 | #endif |
| 61 | |
| 62 | module_param(nowayout, int, 0); |
| 63 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); |
| 64 | |
| 65 | /* |
| 66 | * Start the watchdog |
| 67 | */ |
| 68 | |
| 69 | static int wdt977_start(void) |
| 70 | { |
| 71 | /* unlock the SuperIO chip */ |
| 72 | outb(0x87,0x370); |
| 73 | outb(0x87,0x370); |
| 74 | |
| 75 | /* select device Aux2 (device=8) and set watchdog regs F2, F3 and F4 |
| 76 | * F2 has the timeout in minutes |
| 77 | * F3 could be set to the POWER LED blink (with GP17 set to PowerLed) |
| 78 | * at timeout, and to reset timer on kbd/mouse activity (not impl.) |
| 79 | * F4 is used to just clear the TIMEOUT'ed state (bit 0) |
| 80 | */ |
| 81 | outb(0x07,0x370); |
| 82 | outb(0x08,0x371); |
| 83 | outb(0xF2,0x370); |
| 84 | outb(timeoutM,0x371); |
| 85 | outb(0xF3,0x370); |
| 86 | outb(0x00,0x371); /* another setting is 0E for kbd/mouse/LED */ |
| 87 | outb(0xF4,0x370); |
| 88 | outb(0x00,0x371); |
| 89 | |
| 90 | /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */ |
| 91 | /* in test mode watch the bit 1 on F4 to indicate "triggered" */ |
| 92 | if (!testmode) |
| 93 | { |
| 94 | outb(0x07,0x370); |
| 95 | outb(0x07,0x371); |
| 96 | outb(0xE6,0x370); |
| 97 | outb(0x08,0x371); |
| 98 | } |
| 99 | |
| 100 | /* lock the SuperIO chip */ |
| 101 | outb(0xAA,0x370); |
| 102 | |
| 103 | printk(KERN_INFO PFX "activated.\n"); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Stop the watchdog |
| 110 | */ |
| 111 | |
| 112 | static int wdt977_stop(void) |
| 113 | { |
| 114 | /* unlock the SuperIO chip */ |
| 115 | outb(0x87,0x370); |
| 116 | outb(0x87,0x370); |
| 117 | |
| 118 | /* select device Aux2 (device=8) and set watchdog regs F2,F3 and F4 |
| 119 | * F3 is reset to its default state |
| 120 | * F4 can clear the TIMEOUT'ed state (bit 0) - back to default |
| 121 | * We can not use GP17 as a PowerLed, as we use its usage as a RedLed |
| 122 | */ |
| 123 | outb(0x07,0x370); |
| 124 | outb(0x08,0x371); |
| 125 | outb(0xF2,0x370); |
| 126 | outb(0xFF,0x371); |
| 127 | outb(0xF3,0x370); |
| 128 | outb(0x00,0x371); |
| 129 | outb(0xF4,0x370); |
| 130 | outb(0x00,0x371); |
| 131 | outb(0xF2,0x370); |
| 132 | outb(0x00,0x371); |
| 133 | |
| 134 | /* at last select device Aux1 (dev=7) and set GP16 as a watchdog output */ |
| 135 | outb(0x07,0x370); |
| 136 | outb(0x07,0x371); |
| 137 | outb(0xE6,0x370); |
| 138 | outb(0x08,0x371); |
| 139 | |
| 140 | /* lock the SuperIO chip */ |
| 141 | outb(0xAA,0x370); |
| 142 | |
| 143 | printk(KERN_INFO PFX "shutdown.\n"); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Send a keepalive ping to the watchdog |
| 150 | * This is done by simply re-writing the timeout to reg. 0xF2 |
| 151 | */ |
| 152 | |
| 153 | static int wdt977_keepalive(void) |
| 154 | { |
| 155 | /* unlock the SuperIO chip */ |
| 156 | outb(0x87,0x370); |
| 157 | outb(0x87,0x370); |
| 158 | |
| 159 | /* select device Aux2 (device=8) and kicks watchdog reg F2 */ |
| 160 | /* F2 has the timeout in minutes */ |
| 161 | outb(0x07,0x370); |
| 162 | outb(0x08,0x371); |
| 163 | outb(0xF2,0x370); |
| 164 | outb(timeoutM,0x371); |
| 165 | |
| 166 | /* lock the SuperIO chip */ |
| 167 | outb(0xAA,0x370); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Set the watchdog timeout value |
| 174 | */ |
| 175 | |
| 176 | static int wdt977_set_timeout(int t) |
| 177 | { |
| 178 | int tmrval; |
| 179 | |
| 180 | /* convert seconds to minutes, rounding up */ |
| 181 | tmrval = (t + 59) / 60; |
| 182 | |
| 183 | if (machine_is_netwinder()) { |
| 184 | /* we have a hw bug somewhere, so each 977 minute is actually only 30sec |
| 185 | * this limits the max timeout to half of device max of 255 minutes... |
| 186 | */ |
| 187 | tmrval += tmrval; |
| 188 | } |
| 189 | |
| 190 | if ((tmrval < 1) || (tmrval > 255)) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | /* timeout is the timeout in seconds, timeoutM is the timeout in minutes) */ |
| 194 | timeout = t; |
| 195 | timeoutM = tmrval; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Get the watchdog status |
| 201 | */ |
| 202 | |
| 203 | static int wdt977_get_status(int *status) |
| 204 | { |
| 205 | int new_status; |
| 206 | |
| 207 | *status=0; |
| 208 | |
| 209 | /* unlock the SuperIO chip */ |
| 210 | outb(0x87,0x370); |
| 211 | outb(0x87,0x370); |
| 212 | |
| 213 | /* select device Aux2 (device=8) and read watchdog reg F4 */ |
| 214 | outb(0x07,0x370); |
| 215 | outb(0x08,0x371); |
| 216 | outb(0xF4,0x370); |
| 217 | new_status = inb(0x371); |
| 218 | |
| 219 | /* lock the SuperIO chip */ |
| 220 | outb(0xAA,0x370); |
| 221 | |
| 222 | if (new_status & 1) |
| 223 | *status |= WDIOF_CARDRESET; |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* |
| 230 | * /dev/watchdog handling |
| 231 | */ |
| 232 | |
| 233 | static int wdt977_open(struct inode *inode, struct file *file) |
| 234 | { |
| 235 | /* If the watchdog is alive we don't need to start it again */ |
| 236 | if( test_and_set_bit(0,&timer_alive) ) |
| 237 | return -EBUSY; |
| 238 | |
| 239 | if (nowayout) |
| 240 | __module_get(THIS_MODULE); |
| 241 | |
| 242 | wdt977_start(); |
| 243 | return nonseekable_open(inode, file); |
| 244 | } |
| 245 | |
| 246 | static int wdt977_release(struct inode *inode, struct file *file) |
| 247 | { |
| 248 | /* |
| 249 | * Shut off the timer. |
| 250 | * Lock it in if it's a module and we set nowayout |
| 251 | */ |
| 252 | if (expect_close == 42) |
| 253 | { |
| 254 | wdt977_stop(); |
| 255 | clear_bit(0,&timer_alive); |
| 256 | } else { |
| 257 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); |
| 258 | wdt977_keepalive(); |
| 259 | } |
| 260 | expect_close = 0; |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | * wdt977_write: |
| 267 | * @file: file handle to the watchdog |
| 268 | * @buf: buffer to write (unused as data does not matter here |
| 269 | * @count: count of bytes |
| 270 | * @ppos: pointer to the position to write. No seeks allowed |
| 271 | * |
| 272 | * A write to a watchdog device is defined as a keepalive signal. Any |
| 273 | * write of data will do, as we we don't define content meaning. |
| 274 | */ |
| 275 | |
| 276 | static ssize_t wdt977_write(struct file *file, const char __user *buf, |
| 277 | size_t count, loff_t *ppos) |
| 278 | { |
| 279 | if (count) { |
| 280 | if (!nowayout) { |
| 281 | size_t i; |
| 282 | |
| 283 | /* In case it was set long ago */ |
| 284 | expect_close = 0; |
| 285 | |
| 286 | for (i = 0; i != count; i++) { |
| 287 | char c; |
| 288 | if (get_user(c, buf + i)) |
| 289 | return -EFAULT; |
| 290 | if (c == 'V') |
| 291 | expect_close = 42; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | wdt977_keepalive(); |
| 296 | } |
| 297 | return count; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * wdt977_ioctl: |
| 302 | * @inode: inode of the device |
| 303 | * @file: file handle to the device |
| 304 | * @cmd: watchdog command |
| 305 | * @arg: argument pointer |
| 306 | * |
| 307 | * The watchdog API defines a common set of functions for all watchdogs |
| 308 | * according to their available features. |
| 309 | */ |
| 310 | |
| 311 | static struct watchdog_info ident = { |
| 312 | .options = WDIOF_SETTIMEOUT | |
| 313 | WDIOF_MAGICCLOSE | |
| 314 | WDIOF_KEEPALIVEPING, |
| 315 | .firmware_version = 1, |
| 316 | .identity = "Winbond 83977", |
| 317 | }; |
| 318 | |
| 319 | static int wdt977_ioctl(struct inode *inode, struct file *file, |
| 320 | unsigned int cmd, unsigned long arg) |
| 321 | { |
| 322 | int status; |
| 323 | int new_options, retval = -EINVAL; |
| 324 | int new_timeout; |
| 325 | union { |
| 326 | struct watchdog_info __user *ident; |
| 327 | int __user *i; |
| 328 | } uarg; |
| 329 | |
| 330 | uarg.i = (int __user *)arg; |
| 331 | |
| 332 | switch(cmd) |
| 333 | { |
| 334 | default: |
| 335 | return -ENOIOCTLCMD; |
| 336 | |
| 337 | case WDIOC_GETSUPPORT: |
| 338 | return copy_to_user(uarg.ident, &ident, |
| 339 | sizeof(ident)) ? -EFAULT : 0; |
| 340 | |
| 341 | case WDIOC_GETSTATUS: |
| 342 | wdt977_get_status(&status); |
| 343 | return put_user(status, uarg.i); |
| 344 | |
| 345 | case WDIOC_GETBOOTSTATUS: |
| 346 | return put_user(0, uarg.i); |
| 347 | |
| 348 | case WDIOC_KEEPALIVE: |
| 349 | wdt977_keepalive(); |
| 350 | return 0; |
| 351 | |
| 352 | case WDIOC_SETOPTIONS: |
| 353 | if (get_user (new_options, uarg.i)) |
| 354 | return -EFAULT; |
| 355 | |
| 356 | if (new_options & WDIOS_DISABLECARD) { |
| 357 | wdt977_stop(); |
| 358 | retval = 0; |
| 359 | } |
| 360 | |
| 361 | if (new_options & WDIOS_ENABLECARD) { |
| 362 | wdt977_start(); |
| 363 | retval = 0; |
| 364 | } |
| 365 | |
| 366 | return retval; |
| 367 | |
| 368 | case WDIOC_SETTIMEOUT: |
| 369 | if (get_user(new_timeout, uarg.i)) |
| 370 | return -EFAULT; |
| 371 | |
| 372 | if (wdt977_set_timeout(new_timeout)) |
| 373 | return -EINVAL; |
| 374 | |
| 375 | wdt977_keepalive(); |
| 376 | /* Fall */ |
| 377 | |
| 378 | case WDIOC_GETTIMEOUT: |
| 379 | return put_user(timeout, uarg.i); |
| 380 | |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static int wdt977_notify_sys(struct notifier_block *this, unsigned long code, |
| 385 | void *unused) |
| 386 | { |
| 387 | if(code==SYS_DOWN || code==SYS_HALT) |
| 388 | wdt977_stop(); |
| 389 | return NOTIFY_DONE; |
| 390 | } |
| 391 | |
| 392 | static struct file_operations wdt977_fops= |
| 393 | { |
| 394 | .owner = THIS_MODULE, |
| 395 | .llseek = no_llseek, |
| 396 | .write = wdt977_write, |
| 397 | .ioctl = wdt977_ioctl, |
| 398 | .open = wdt977_open, |
| 399 | .release = wdt977_release, |
| 400 | }; |
| 401 | |
| 402 | static struct miscdevice wdt977_miscdev= |
| 403 | { |
| 404 | .minor = WATCHDOG_MINOR, |
| 405 | .name = "watchdog", |
| 406 | .fops = &wdt977_fops, |
| 407 | }; |
| 408 | |
| 409 | static struct notifier_block wdt977_notifier = { |
| 410 | .notifier_call = wdt977_notify_sys, |
| 411 | }; |
| 412 | |
| 413 | static int __init nwwatchdog_init(void) |
| 414 | { |
| 415 | int retval; |
| 416 | if (!machine_is_netwinder()) |
| 417 | return -ENODEV; |
| 418 | |
| 419 | /* Check that the timeout value is within it's range ; if not reset to the default */ |
| 420 | if (wdt977_set_timeout(timeout)) { |
| 421 | wdt977_set_timeout(DEFAULT_TIMEOUT); |
| 422 | printk(KERN_INFO PFX "timeout value must be 60<timeout<15300, using %d\n", |
| 423 | DEFAULT_TIMEOUT); |
| 424 | } |
| 425 | |
| 426 | retval = register_reboot_notifier(&wdt977_notifier); |
| 427 | if (retval) { |
| 428 | printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", |
| 429 | retval); |
| 430 | return retval; |
| 431 | } |
| 432 | |
| 433 | retval = misc_register(&wdt977_miscdev); |
| 434 | if (retval) { |
| 435 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", |
| 436 | WATCHDOG_MINOR, retval); |
| 437 | unregister_reboot_notifier(&wdt977_notifier); |
| 438 | return retval; |
| 439 | } |
| 440 | |
| 441 | printk(KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d, testmode = %i)\n", |
| 442 | timeout, nowayout, testmode); |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static void __exit nwwatchdog_exit(void) |
| 448 | { |
| 449 | misc_deregister(&wdt977_miscdev); |
| 450 | unregister_reboot_notifier(&wdt977_notifier); |
| 451 | } |
| 452 | |
| 453 | module_init(nwwatchdog_init); |
| 454 | module_exit(nwwatchdog_exit); |
| 455 | |
| 456 | MODULE_AUTHOR("Woody Suwalski <woody@netwinder.org>"); |
| 457 | MODULE_DESCRIPTION("W83977AF Watchdog driver"); |
| 458 | MODULE_LICENSE("GPL"); |
| 459 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |