Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * W83877F Computer Watchdog Timer driver |
| 3 | * |
| 4 | * Based on acquirewdt.c by Alan Cox, |
| 5 | * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * The authors do NOT admit liability nor provide warranty for |
| 13 | * any of this software. This material is provided "AS-IS" in |
| 14 | * the hope that it may be useful for others. |
| 15 | * |
| 16 | * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net> |
| 17 | * |
| 18 | * 4/19 - 2001 [Initial revision] |
| 19 | * 9/27 - 2001 Added spinlocking |
| 20 | * 4/12 - 2002 [rob@osinvestor.com] Eliminate extra comments |
| 21 | * Eliminate fop_read |
| 22 | * Eliminate extra spin_unlock |
| 23 | * Added KERN_* tags to printks |
| 24 | * add CONFIG_WATCHDOG_NOWAYOUT support |
| 25 | * fix possible wdt_is_open race |
| 26 | * changed watchdog_info to correctly reflect what the driver offers |
| 27 | * added WDIOC_GETSTATUS, WDIOC_GETBOOTSTATUS, WDIOC_SETTIMEOUT, |
| 28 | * WDIOC_GETTIMEOUT, and WDIOC_SETOPTIONS ioctls |
| 29 | * 09/8 - 2003 [wim@iguana.be] cleanup of trailing spaces |
| 30 | * added extra printk's for startup problems |
| 31 | * use module_param |
| 32 | * made timeout (the emulated heartbeat) a module_param |
| 33 | * made the keepalive ping an internal subroutine |
| 34 | * |
| 35 | * This WDT driver is different from most other Linux WDT |
| 36 | * drivers in that the driver will ping the watchdog by itself, |
| 37 | * because this particular WDT has a very short timeout (1.6 |
| 38 | * seconds) and it would be insane to count on any userspace |
| 39 | * daemon always getting scheduled within that time frame. |
| 40 | */ |
| 41 | |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/moduleparam.h> |
| 44 | #include <linux/types.h> |
| 45 | #include <linux/timer.h> |
| 46 | #include <linux/jiffies.h> |
| 47 | #include <linux/miscdevice.h> |
| 48 | #include <linux/watchdog.h> |
| 49 | #include <linux/fs.h> |
| 50 | #include <linux/ioport.h> |
| 51 | #include <linux/notifier.h> |
| 52 | #include <linux/reboot.h> |
| 53 | #include <linux/init.h> |
| 54 | #include <asm/io.h> |
| 55 | #include <asm/uaccess.h> |
| 56 | #include <asm/system.h> |
| 57 | |
| 58 | #define OUR_NAME "w83877f_wdt" |
| 59 | #define PFX OUR_NAME ": " |
| 60 | |
| 61 | #define ENABLE_W83877F_PORT 0x3F0 |
| 62 | #define ENABLE_W83877F 0x87 |
| 63 | #define DISABLE_W83877F 0xAA |
| 64 | #define WDT_PING 0x443 |
| 65 | #define WDT_REGISTER 0x14 |
| 66 | #define WDT_ENABLE 0x9C |
| 67 | #define WDT_DISABLE 0x8C |
| 68 | |
| 69 | /* |
| 70 | * The W83877F seems to be fixed at 1.6s timeout (at least on the |
| 71 | * EMACS PC-104 board I'm using). If we reset the watchdog every |
| 72 | * ~250ms we should be safe. */ |
| 73 | |
| 74 | #define WDT_INTERVAL (HZ/4+1) |
| 75 | |
| 76 | /* |
| 77 | * We must not require too good response from the userspace daemon. |
| 78 | * Here we require the userspace daemon to send us a heartbeat |
| 79 | * char to /dev/watchdog every 30 seconds. |
| 80 | */ |
| 81 | |
| 82 | #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */ |
| 83 | static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */ |
| 84 | module_param(timeout, int, 0); |
| 85 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); |
| 86 | |
| 87 | |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame^] | 88 | static int nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | module_param(nowayout, int, 0); |
| 90 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); |
| 91 | |
| 92 | static void wdt_timer_ping(unsigned long); |
| 93 | static struct timer_list timer; |
| 94 | static unsigned long next_heartbeat; |
| 95 | static unsigned long wdt_is_open; |
| 96 | static char wdt_expect_close; |
| 97 | static spinlock_t wdt_spinlock; |
| 98 | |
| 99 | /* |
| 100 | * Whack the dog |
| 101 | */ |
| 102 | |
| 103 | static void wdt_timer_ping(unsigned long data) |
| 104 | { |
| 105 | /* If we got a heartbeat pulse within the WDT_US_INTERVAL |
| 106 | * we agree to ping the WDT |
| 107 | */ |
| 108 | if(time_before(jiffies, next_heartbeat)) |
| 109 | { |
| 110 | /* Ping the WDT */ |
| 111 | spin_lock(&wdt_spinlock); |
| 112 | |
| 113 | /* Ping the WDT by reading from WDT_PING */ |
| 114 | inb_p(WDT_PING); |
| 115 | |
| 116 | /* Re-set the timer interval */ |
| 117 | timer.expires = jiffies + WDT_INTERVAL; |
| 118 | add_timer(&timer); |
| 119 | |
| 120 | spin_unlock(&wdt_spinlock); |
| 121 | |
| 122 | } else { |
| 123 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Utility routines |
| 129 | */ |
| 130 | |
| 131 | static void wdt_change(int writeval) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | spin_lock_irqsave(&wdt_spinlock, flags); |
| 135 | |
| 136 | /* buy some time */ |
| 137 | inb_p(WDT_PING); |
| 138 | |
| 139 | /* make W83877F available */ |
| 140 | outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT); |
| 141 | outb_p(ENABLE_W83877F, ENABLE_W83877F_PORT); |
| 142 | |
| 143 | /* enable watchdog */ |
| 144 | outb_p(WDT_REGISTER, ENABLE_W83877F_PORT); |
| 145 | outb_p(writeval, ENABLE_W83877F_PORT+1); |
| 146 | |
| 147 | /* lock the W8387FF away */ |
| 148 | outb_p(DISABLE_W83877F, ENABLE_W83877F_PORT); |
| 149 | |
| 150 | spin_unlock_irqrestore(&wdt_spinlock, flags); |
| 151 | } |
| 152 | |
| 153 | static void wdt_startup(void) |
| 154 | { |
| 155 | next_heartbeat = jiffies + (timeout * HZ); |
| 156 | |
| 157 | /* Start the timer */ |
| 158 | timer.expires = jiffies + WDT_INTERVAL; |
| 159 | add_timer(&timer); |
| 160 | |
| 161 | wdt_change(WDT_ENABLE); |
| 162 | |
| 163 | printk(KERN_INFO PFX "Watchdog timer is now enabled.\n"); |
| 164 | } |
| 165 | |
| 166 | static void wdt_turnoff(void) |
| 167 | { |
| 168 | /* Stop the timer */ |
| 169 | del_timer(&timer); |
| 170 | |
| 171 | wdt_change(WDT_DISABLE); |
| 172 | |
| 173 | printk(KERN_INFO PFX "Watchdog timer is now disabled...\n"); |
| 174 | } |
| 175 | |
| 176 | static void wdt_keepalive(void) |
| 177 | { |
| 178 | /* user land ping */ |
| 179 | next_heartbeat = jiffies + (timeout * HZ); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * /dev/watchdog handling |
| 184 | */ |
| 185 | |
| 186 | static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos) |
| 187 | { |
| 188 | /* See if we got the magic character 'V' and reload the timer */ |
| 189 | if(count) |
| 190 | { |
| 191 | if (!nowayout) |
| 192 | { |
| 193 | size_t ofs; |
| 194 | |
| 195 | /* note: just in case someone wrote the magic character |
| 196 | * five months ago... */ |
| 197 | wdt_expect_close = 0; |
| 198 | |
| 199 | /* scan to see whether or not we got the magic character */ |
| 200 | for(ofs = 0; ofs != count; ofs++) |
| 201 | { |
| 202 | char c; |
| 203 | if (get_user(c, buf + ofs)) |
| 204 | return -EFAULT; |
| 205 | if (c == 'V') |
| 206 | wdt_expect_close = 42; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* someone wrote to us, we should restart timer */ |
| 211 | wdt_keepalive(); |
| 212 | } |
| 213 | return count; |
| 214 | } |
| 215 | |
| 216 | static int fop_open(struct inode * inode, struct file * file) |
| 217 | { |
| 218 | /* Just in case we're already talking to someone... */ |
| 219 | if(test_and_set_bit(0, &wdt_is_open)) |
| 220 | return -EBUSY; |
| 221 | |
| 222 | /* Good, fire up the show */ |
| 223 | wdt_startup(); |
| 224 | return nonseekable_open(inode, file); |
| 225 | } |
| 226 | |
| 227 | static int fop_close(struct inode * inode, struct file * file) |
| 228 | { |
| 229 | if(wdt_expect_close == 42) |
| 230 | wdt_turnoff(); |
| 231 | else { |
| 232 | del_timer(&timer); |
| 233 | printk(KERN_CRIT PFX "device file closed unexpectedly. Will not stop the WDT!\n"); |
| 234 | } |
| 235 | clear_bit(0, &wdt_is_open); |
| 236 | wdt_expect_close = 0; |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 241 | unsigned long arg) |
| 242 | { |
| 243 | void __user *argp = (void __user *)arg; |
| 244 | int __user *p = argp; |
| 245 | static struct watchdog_info ident= |
| 246 | { |
| 247 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, |
| 248 | .firmware_version = 1, |
| 249 | .identity = "W83877F", |
| 250 | }; |
| 251 | |
| 252 | switch(cmd) |
| 253 | { |
| 254 | default: |
| 255 | return -ENOIOCTLCMD; |
| 256 | case WDIOC_GETSUPPORT: |
| 257 | return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0; |
| 258 | case WDIOC_GETSTATUS: |
| 259 | case WDIOC_GETBOOTSTATUS: |
| 260 | return put_user(0, p); |
| 261 | case WDIOC_KEEPALIVE: |
| 262 | wdt_keepalive(); |
| 263 | return 0; |
| 264 | case WDIOC_SETOPTIONS: |
| 265 | { |
| 266 | int new_options, retval = -EINVAL; |
| 267 | |
| 268 | if(get_user(new_options, p)) |
| 269 | return -EFAULT; |
| 270 | |
| 271 | if(new_options & WDIOS_DISABLECARD) { |
| 272 | wdt_turnoff(); |
| 273 | retval = 0; |
| 274 | } |
| 275 | |
| 276 | if(new_options & WDIOS_ENABLECARD) { |
| 277 | wdt_startup(); |
| 278 | retval = 0; |
| 279 | } |
| 280 | |
| 281 | return retval; |
| 282 | } |
| 283 | case WDIOC_SETTIMEOUT: |
| 284 | { |
| 285 | int new_timeout; |
| 286 | |
| 287 | if(get_user(new_timeout, p)) |
| 288 | return -EFAULT; |
| 289 | |
| 290 | if(new_timeout < 1 || new_timeout > 3600) /* arbitrary upper limit */ |
| 291 | return -EINVAL; |
| 292 | |
| 293 | timeout = new_timeout; |
| 294 | wdt_keepalive(); |
| 295 | /* Fall through */ |
| 296 | } |
| 297 | case WDIOC_GETTIMEOUT: |
| 298 | return put_user(timeout, p); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static struct file_operations wdt_fops = { |
| 303 | .owner = THIS_MODULE, |
| 304 | .llseek = no_llseek, |
| 305 | .write = fop_write, |
| 306 | .open = fop_open, |
| 307 | .release = fop_close, |
| 308 | .ioctl = fop_ioctl, |
| 309 | }; |
| 310 | |
| 311 | static struct miscdevice wdt_miscdev = { |
| 312 | .minor = WATCHDOG_MINOR, |
| 313 | .name = "watchdog", |
| 314 | .fops = &wdt_fops, |
| 315 | }; |
| 316 | |
| 317 | /* |
| 318 | * Notifier for system down |
| 319 | */ |
| 320 | |
| 321 | static int wdt_notify_sys(struct notifier_block *this, unsigned long code, |
| 322 | void *unused) |
| 323 | { |
| 324 | if(code==SYS_DOWN || code==SYS_HALT) |
| 325 | wdt_turnoff(); |
| 326 | return NOTIFY_DONE; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * The WDT needs to learn about soft shutdowns in order to |
| 331 | * turn the timebomb registers off. |
| 332 | */ |
| 333 | |
| 334 | static struct notifier_block wdt_notifier= |
| 335 | { |
| 336 | .notifier_call = wdt_notify_sys, |
| 337 | }; |
| 338 | |
| 339 | static void __exit w83877f_wdt_unload(void) |
| 340 | { |
| 341 | wdt_turnoff(); |
| 342 | |
| 343 | /* Deregister */ |
| 344 | misc_deregister(&wdt_miscdev); |
| 345 | |
| 346 | unregister_reboot_notifier(&wdt_notifier); |
| 347 | release_region(WDT_PING,1); |
| 348 | release_region(ENABLE_W83877F_PORT,2); |
| 349 | } |
| 350 | |
| 351 | static int __init w83877f_wdt_init(void) |
| 352 | { |
| 353 | int rc = -EBUSY; |
| 354 | |
| 355 | spin_lock_init(&wdt_spinlock); |
| 356 | |
| 357 | if(timeout < 1 || timeout > 3600) /* arbitrary upper limit */ |
| 358 | { |
| 359 | timeout = WATCHDOG_TIMEOUT; |
| 360 | printk(KERN_INFO PFX "timeout value must be 1<=x<=3600, using %d\n", |
| 361 | timeout); |
| 362 | } |
| 363 | |
| 364 | if (!request_region(ENABLE_W83877F_PORT, 2, "W83877F WDT")) |
| 365 | { |
| 366 | printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", |
| 367 | ENABLE_W83877F_PORT); |
| 368 | rc = -EIO; |
| 369 | goto err_out; |
| 370 | } |
| 371 | |
| 372 | if (!request_region(WDT_PING, 1, "W8387FF WDT")) |
| 373 | { |
| 374 | printk(KERN_ERR PFX "I/O address 0x%04x already in use\n", |
| 375 | WDT_PING); |
| 376 | rc = -EIO; |
| 377 | goto err_out_region1; |
| 378 | } |
| 379 | |
| 380 | init_timer(&timer); |
| 381 | timer.function = wdt_timer_ping; |
| 382 | timer.data = 0; |
| 383 | |
| 384 | rc = misc_register(&wdt_miscdev); |
| 385 | if (rc) |
| 386 | { |
| 387 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", |
| 388 | wdt_miscdev.minor, rc); |
| 389 | goto err_out_region2; |
| 390 | } |
| 391 | |
| 392 | rc = register_reboot_notifier(&wdt_notifier); |
| 393 | if (rc) |
| 394 | { |
| 395 | printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", |
| 396 | rc); |
| 397 | goto err_out_miscdev; |
| 398 | } |
| 399 | |
| 400 | printk(KERN_INFO PFX "WDT driver for W83877F initialised. timeout=%d sec (nowayout=%d)\n", |
| 401 | timeout, nowayout); |
| 402 | |
| 403 | return 0; |
| 404 | |
| 405 | err_out_miscdev: |
| 406 | misc_deregister(&wdt_miscdev); |
| 407 | err_out_region2: |
| 408 | release_region(WDT_PING,1); |
| 409 | err_out_region1: |
| 410 | release_region(ENABLE_W83877F_PORT,2); |
| 411 | err_out: |
| 412 | return rc; |
| 413 | } |
| 414 | |
| 415 | module_init(w83877f_wdt_init); |
| 416 | module_exit(w83877f_wdt_unload); |
| 417 | |
| 418 | MODULE_AUTHOR("Scott and Bill Jennings"); |
| 419 | MODULE_DESCRIPTION("Driver for watchdog timer in w83877f chip"); |
| 420 | MODULE_LICENSE("GPL"); |
| 421 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |