Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * SMsC 37B787 Watchdog Timer driver for Linux 2.6.x.x |
| 3 | * |
| 4 | * Based on acquirewdt.c by Alan Cox <alan@redhat.com> |
| 5 | * and some other existing drivers |
| 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. |
Wim Van Sebroeck | 8386c8c | 2006-09-02 19:32:26 +0200 | [diff] [blame] | 11 | * |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 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 2003-2006 Sven Anders <anders@anduras.de> |
| 17 | * |
| 18 | * History: |
| 19 | * 2003 - Created version 1.0 for Linux 2.4.x. |
| 20 | * 2006 - Ported to Linux 2.6, added nowayout and MAGICCLOSE |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 21 | * features. Released version 1.1 |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 22 | * |
| 23 | * Theory of operation: |
| 24 | * |
| 25 | * A Watchdog Timer (WDT) is a hardware circuit that can |
| 26 | * reset the computer system in case of a software fault. |
| 27 | * You probably knew that already. |
| 28 | * |
| 29 | * Usually a userspace daemon will notify the kernel WDT driver |
| 30 | * via the /dev/watchdog special device file that userspace is |
| 31 | * still alive, at regular intervals. When such a notification |
| 32 | * occurs, the driver will usually tell the hardware watchdog |
| 33 | * that everything is in order, and that the watchdog should wait |
| 34 | * for yet another little while to reset the system. |
| 35 | * If userspace fails (RAM error, kernel bug, whatever), the |
| 36 | * notifications cease to occur, and the hardware watchdog will |
| 37 | * reset the system (causing a reboot) after the timeout occurs. |
| 38 | * |
| 39 | * Create device with: |
| 40 | * mknod /dev/watchdog c 10 130 |
| 41 | * |
| 42 | * For an example userspace keep-alive daemon, see: |
| 43 | * Documentation/watchdog/watchdog.txt |
| 44 | */ |
| 45 | |
| 46 | #include <linux/module.h> |
| 47 | #include <linux/moduleparam.h> |
| 48 | #include <linux/types.h> |
| 49 | #include <linux/miscdevice.h> |
| 50 | #include <linux/watchdog.h> |
| 51 | #include <linux/delay.h> |
| 52 | #include <linux/fs.h> |
| 53 | #include <linux/ioport.h> |
| 54 | #include <linux/notifier.h> |
| 55 | #include <linux/reboot.h> |
| 56 | #include <linux/init.h> |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 57 | #include <linux/spinlock.h> |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 58 | #include <linux/io.h> |
| 59 | #include <linux/uaccess.h> |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 60 | |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 61 | #include <asm/system.h> |
| 62 | |
| 63 | /* enable support for minutes as units? */ |
| 64 | /* (does not always work correctly, so disabled by default!) */ |
| 65 | #define SMSC_SUPPORT_MINUTES |
| 66 | #undef SMSC_SUPPORT_MINUTES |
| 67 | |
| 68 | #define MAX_TIMEOUT 255 |
| 69 | |
| 70 | #define UNIT_SECOND 0 |
| 71 | #define UNIT_MINUTE 1 |
| 72 | |
| 73 | #define MODNAME "smsc37b787_wdt: " |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 74 | #define VERSION "1.1" |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 75 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 76 | #define IOPORT 0x3F0 |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 77 | #define IOPORT_SIZE 2 |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 78 | #define IODEV_NO 8 |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 79 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 80 | static int unit = UNIT_SECOND; /* timer's unit */ |
| 81 | static int timeout = 60; /* timeout value: default is 60 "units" */ |
| 82 | static unsigned long timer_enabled; /* is the timer enabled? */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 83 | |
| 84 | static char expect_close; /* is the close expected? */ |
| 85 | |
Alexey Dobriyan | c7dfd0c | 2007-11-01 16:27:08 -0700 | [diff] [blame] | 86 | static DEFINE_SPINLOCK(io_lock);/* to guard the watchdog from io races */ |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 87 | |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 88 | static int nowayout = WATCHDOG_NOWAYOUT; |
| 89 | |
| 90 | /* -- Low level function ----------------------------------------*/ |
| 91 | |
| 92 | /* unlock the IO chip */ |
| 93 | |
| 94 | static inline void open_io_config(void) |
| 95 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 96 | outb(0x55, IOPORT); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 97 | mdelay(1); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 98 | outb(0x55, IOPORT); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* lock the IO chip */ |
| 102 | static inline void close_io_config(void) |
| 103 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 104 | outb(0xAA, IOPORT); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* select the IO device */ |
| 108 | static inline void select_io_device(unsigned char devno) |
| 109 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 110 | outb(0x07, IOPORT); |
| 111 | outb(devno, IOPORT+1); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* write to the control register */ |
| 115 | static inline void write_io_cr(unsigned char reg, unsigned char data) |
| 116 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 117 | outb(reg, IOPORT); |
| 118 | outb(data, IOPORT+1); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* read from the control register */ |
| 122 | static inline char read_io_cr(unsigned char reg) |
| 123 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 124 | outb(reg, IOPORT); |
| 125 | return inb(IOPORT+1); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* -- Medium level functions ------------------------------------*/ |
| 129 | |
| 130 | static inline void gpio_bit12(unsigned char reg) |
| 131 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 132 | /* -- General Purpose I/O Bit 1.2 -- |
| 133 | * Bit 0, In/Out: 0 = Output, 1 = Input |
| 134 | * Bit 1, Polarity: 0 = No Invert, 1 = Invert |
| 135 | * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable |
| 136 | * Bit 3/4, Function select: 00 = GPI/O, 01 = WDT, 10 = P17, |
| 137 | * 11 = Either Edge Triggered Intr. 2 |
| 138 | * Bit 5/6 (Reserved) |
| 139 | * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain |
| 140 | */ |
| 141 | write_io_cr(0xE2, reg); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static inline void gpio_bit13(unsigned char reg) |
| 145 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 146 | /* -- General Purpose I/O Bit 1.3 -- |
| 147 | * Bit 0, In/Out: 0 = Output, 1 = Input |
| 148 | * Bit 1, Polarity: 0 = No Invert, 1 = Invert |
| 149 | * Bit 2, Group Enable Intr.: 0 = Disable, 1 = Enable |
| 150 | * Bit 3, Function select: 0 = GPI/O, 1 = LED |
| 151 | * Bit 4-6 (Reserved) |
| 152 | * Bit 7, Output Type: 0 = Push Pull Bit, 1 = Open Drain |
| 153 | */ |
| 154 | write_io_cr(0xE3, reg); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static inline void wdt_timer_units(unsigned char new_units) |
| 158 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 159 | /* -- Watchdog timer units -- |
| 160 | * Bit 0-6 (Reserved) |
| 161 | * Bit 7, WDT Time-out Value Units Select |
| 162 | * (0 = Minutes, 1 = Seconds) |
| 163 | */ |
| 164 | write_io_cr(0xF1, new_units); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline void wdt_timeout_value(unsigned char new_timeout) |
| 168 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 169 | /* -- Watchdog Timer Time-out Value -- |
| 170 | * Bit 0-7 Binary coded units (0=Disabled, 1..255) |
| 171 | */ |
| 172 | write_io_cr(0xF2, new_timeout); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static inline void wdt_timer_conf(unsigned char conf) |
| 176 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 177 | /* -- Watchdog timer configuration -- |
| 178 | * Bit 0 Joystick enable: 0* = No Reset, 1 = Reset WDT upon |
| 179 | * Gameport I/O |
| 180 | * Bit 1 Keyboard enable: 0* = No Reset, 1 = Reset WDT upon KBD Intr. |
| 181 | * Bit 2 Mouse enable: 0* = No Reset, 1 = Reset WDT upon Mouse Intr |
| 182 | * Bit 3 Reset the timer |
| 183 | * (Wrong in SMsC documentation? Given as: PowerLED Timout |
| 184 | * Enabled) |
| 185 | * Bit 4-7 WDT Interrupt Mapping: (0000* = Disabled, |
| 186 | * 0001=IRQ1, 0010=(Invalid), 0011=IRQ3 to 1111=IRQ15) |
| 187 | */ |
| 188 | write_io_cr(0xF3, conf); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static inline void wdt_timer_ctrl(unsigned char reg) |
| 192 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 193 | /* -- Watchdog timer control -- |
| 194 | * Bit 0 Status Bit: 0 = Timer counting, 1 = Timeout occured |
| 195 | * Bit 1 Power LED Toggle: 0 = Disable Toggle, 1 = Toggle at 1 Hz |
| 196 | * Bit 2 Force Timeout: 1 = Forces WD timeout event (self-cleaning) |
| 197 | * Bit 3 P20 Force Timeout enabled: |
| 198 | * 0 = P20 activity does not generate the WD timeout event |
| 199 | * 1 = P20 Allows rising edge of P20, from the keyboard |
| 200 | * controller, to force the WD timeout event. |
| 201 | * Bit 4 (Reserved) |
| 202 | * -- Soft power management -- |
| 203 | * Bit 5 Stop Counter: 1 = Stop software power down counter |
| 204 | * set via register 0xB8, (self-cleaning) |
| 205 | * (Upon read: 0 = Counter running, 1 = Counter stopped) |
| 206 | * Bit 6 Restart Counter: 1 = Restart software power down counter |
| 207 | * set via register 0xB8, (self-cleaning) |
| 208 | * Bit 7 SPOFF: 1 = Force software power down (self-cleaning) |
| 209 | */ |
| 210 | write_io_cr(0xF4, reg); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* -- Higher level functions ------------------------------------*/ |
| 214 | |
| 215 | /* initialize watchdog */ |
| 216 | |
| 217 | static void wb_smsc_wdt_initialize(void) |
| 218 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 219 | unsigned char old; |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 220 | |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 221 | spin_lock(&io_lock); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 222 | open_io_config(); |
| 223 | select_io_device(IODEV_NO); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 224 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 225 | /* enable the watchdog */ |
| 226 | gpio_bit13(0x08); /* Select pin 80 = LED not GPIO */ |
| 227 | gpio_bit12(0x0A); /* Set pin 79 = WDT not |
| 228 | GPIO/Output/Polarity=Invert */ |
| 229 | /* disable the timeout */ |
| 230 | wdt_timeout_value(0); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 231 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 232 | /* reset control register */ |
| 233 | wdt_timer_ctrl(0x00); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 234 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 235 | /* reset configuration register */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 236 | wdt_timer_conf(0x00); |
| 237 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 238 | /* read old (timer units) register */ |
| 239 | old = read_io_cr(0xF1) & 0x7F; |
| 240 | if (unit == UNIT_SECOND) |
| 241 | old |= 0x80; /* set to seconds */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 242 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 243 | /* set the watchdog timer units */ |
| 244 | wdt_timer_units(old); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 245 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 246 | close_io_config(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 247 | spin_unlock(&io_lock); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /* shutdown the watchdog */ |
| 251 | |
| 252 | static void wb_smsc_wdt_shutdown(void) |
| 253 | { |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 254 | spin_lock(&io_lock); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 255 | open_io_config(); |
| 256 | select_io_device(IODEV_NO); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 257 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 258 | /* disable the watchdog */ |
| 259 | gpio_bit13(0x09); |
| 260 | gpio_bit12(0x09); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 261 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 262 | /* reset watchdog config register */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 263 | wdt_timer_conf(0x00); |
| 264 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 265 | /* reset watchdog control register */ |
| 266 | wdt_timer_ctrl(0x00); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 267 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 268 | /* disable timeout */ |
| 269 | wdt_timeout_value(0x00); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 270 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 271 | close_io_config(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 272 | spin_unlock(&io_lock); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /* set timeout => enable watchdog */ |
| 276 | |
| 277 | static void wb_smsc_wdt_set_timeout(unsigned char new_timeout) |
| 278 | { |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 279 | spin_lock(&io_lock); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 280 | open_io_config(); |
| 281 | select_io_device(IODEV_NO); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 282 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 283 | /* set Power LED to blink, if we enable the timeout */ |
| 284 | wdt_timer_ctrl((new_timeout == 0) ? 0x00 : 0x02); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 285 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 286 | /* set timeout value */ |
| 287 | wdt_timeout_value(new_timeout); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 288 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 289 | close_io_config(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 290 | spin_unlock(&io_lock); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* get timeout */ |
| 294 | |
| 295 | static unsigned char wb_smsc_wdt_get_timeout(void) |
| 296 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 297 | unsigned char set_timeout; |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 298 | |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 299 | spin_lock(&io_lock); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 300 | open_io_config(); |
| 301 | select_io_device(IODEV_NO); |
| 302 | set_timeout = read_io_cr(0xF2); |
| 303 | close_io_config(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 304 | spin_unlock(&io_lock); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 305 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 306 | return set_timeout; |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* disable watchdog */ |
| 310 | |
| 311 | static void wb_smsc_wdt_disable(void) |
| 312 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 313 | /* set the timeout to 0 to disable the watchdog */ |
| 314 | wb_smsc_wdt_set_timeout(0); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* enable watchdog by setting the current timeout */ |
| 318 | |
| 319 | static void wb_smsc_wdt_enable(void) |
| 320 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 321 | /* set the current timeout... */ |
| 322 | wb_smsc_wdt_set_timeout(timeout); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* reset the timer */ |
| 326 | |
| 327 | static void wb_smsc_wdt_reset_timer(void) |
| 328 | { |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 329 | spin_lock(&io_lock); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 330 | open_io_config(); |
| 331 | select_io_device(IODEV_NO); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 332 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 333 | /* reset the timer */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 334 | wdt_timeout_value(timeout); |
| 335 | wdt_timer_conf(0x08); |
| 336 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 337 | close_io_config(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 338 | spin_unlock(&io_lock); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* return, if the watchdog is enabled (timeout is set...) */ |
| 342 | |
| 343 | static int wb_smsc_wdt_status(void) |
| 344 | { |
| 345 | return (wb_smsc_wdt_get_timeout() == 0) ? 0 : WDIOF_KEEPALIVEPING; |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /* -- File operations -------------------------------------------*/ |
| 350 | |
| 351 | /* open => enable watchdog and set initial timeout */ |
| 352 | |
| 353 | static int wb_smsc_wdt_open(struct inode *inode, struct file *file) |
| 354 | { |
| 355 | /* /dev/watchdog can only be opened once */ |
| 356 | |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 357 | if (test_and_set_bit(0, &timer_enabled)) |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 358 | return -EBUSY; |
| 359 | |
| 360 | if (nowayout) |
| 361 | __module_get(THIS_MODULE); |
| 362 | |
| 363 | /* Reload and activate timer */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 364 | wb_smsc_wdt_enable(); |
| 365 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 366 | printk(KERN_INFO MODNAME |
| 367 | "Watchdog enabled. Timeout set to %d %s.\n", |
| 368 | timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 369 | |
| 370 | return nonseekable_open(inode, file); |
| 371 | } |
| 372 | |
| 373 | /* close => shut off the timer */ |
| 374 | |
| 375 | static int wb_smsc_wdt_release(struct inode *inode, struct file *file) |
| 376 | { |
| 377 | /* Shut off the timer. */ |
| 378 | |
| 379 | if (expect_close == 42) { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 380 | wb_smsc_wdt_disable(); |
| 381 | printk(KERN_INFO MODNAME |
| 382 | "Watchdog disabled, sleeping again...\n"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 383 | } else { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 384 | printk(KERN_CRIT MODNAME |
| 385 | "Unexpected close, not stopping watchdog!\n"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 386 | wb_smsc_wdt_reset_timer(); |
| 387 | } |
| 388 | |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 389 | clear_bit(0, &timer_enabled); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 390 | expect_close = 0; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /* write => update the timer to keep the machine alive */ |
| 395 | |
| 396 | static ssize_t wb_smsc_wdt_write(struct file *file, const char __user *data, |
| 397 | size_t len, loff_t *ppos) |
| 398 | { |
| 399 | /* See if we got the magic character 'V' and reload the timer */ |
| 400 | if (len) { |
| 401 | if (!nowayout) { |
| 402 | size_t i; |
| 403 | |
| 404 | /* reset expect flag */ |
| 405 | expect_close = 0; |
| 406 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 407 | /* scan to see whether or not we got the |
| 408 | magic character */ |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 409 | for (i = 0; i != len; i++) { |
| 410 | char c; |
| 411 | if (get_user(c, data+i)) |
| 412 | return -EFAULT; |
| 413 | if (c == 'V') |
| 414 | expect_close = 42; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* someone wrote to us, we should reload the timer */ |
| 419 | wb_smsc_wdt_reset_timer(); |
| 420 | } |
| 421 | return len; |
| 422 | } |
| 423 | |
| 424 | /* ioctl => control interface */ |
| 425 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 426 | static long wb_smsc_wdt_ioctl(struct file *file, |
| 427 | unsigned int cmd, unsigned long arg) |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 428 | { |
| 429 | int new_timeout; |
| 430 | |
| 431 | union { |
| 432 | struct watchdog_info __user *ident; |
| 433 | int __user *i; |
| 434 | } uarg; |
| 435 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 436 | static const struct watchdog_info ident = { |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 437 | .options = WDIOF_KEEPALIVEPING | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 438 | WDIOF_SETTIMEOUT | |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 439 | WDIOF_MAGICCLOSE, |
| 440 | .firmware_version = 0, |
| 441 | .identity = "SMsC 37B787 Watchdog" |
| 442 | }; |
| 443 | |
| 444 | uarg.i = (int __user *)arg; |
| 445 | |
| 446 | switch (cmd) { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 447 | case WDIOC_GETSUPPORT: |
| 448 | return copy_to_user(uarg.ident, &ident, sizeof(ident)) |
| 449 | ? -EFAULT : 0; |
| 450 | case WDIOC_GETSTATUS: |
| 451 | return put_user(wb_smsc_wdt_status(), uarg.i); |
| 452 | case WDIOC_GETBOOTSTATUS: |
| 453 | return put_user(0, uarg.i); |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame^] | 454 | case WDIOC_SETOPTIONS: |
| 455 | { |
| 456 | int options, retval = -EINVAL; |
| 457 | |
| 458 | if (get_user(options, uarg.i)) |
| 459 | return -EFAULT; |
| 460 | |
| 461 | if (options & WDIOS_DISABLECARD) { |
| 462 | wb_smsc_wdt_disable(); |
| 463 | retval = 0; |
| 464 | } |
| 465 | if (options & WDIOS_ENABLECARD) { |
| 466 | wb_smsc_wdt_enable(); |
| 467 | retval = 0; |
| 468 | } |
| 469 | return retval; |
| 470 | } |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 471 | case WDIOC_KEEPALIVE: |
| 472 | wb_smsc_wdt_reset_timer(); |
| 473 | return 0; |
| 474 | case WDIOC_SETTIMEOUT: |
| 475 | if (get_user(new_timeout, uarg.i)) |
| 476 | return -EFAULT; |
| 477 | /* the API states this is given in secs */ |
| 478 | if (unit == UNIT_MINUTE) |
| 479 | new_timeout /= 60; |
| 480 | if (new_timeout < 0 || new_timeout > MAX_TIMEOUT) |
| 481 | return -EINVAL; |
| 482 | timeout = new_timeout; |
| 483 | wb_smsc_wdt_set_timeout(timeout); |
| 484 | /* fall through and return the new timeout... */ |
| 485 | case WDIOC_GETTIMEOUT: |
| 486 | new_timeout = timeout; |
| 487 | if (unit == UNIT_MINUTE) |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 488 | new_timeout *= 60; |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 489 | return put_user(new_timeout, uarg.i); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 490 | default: |
| 491 | return -ENOTTY; |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | /* -- Notifier funtions -----------------------------------------*/ |
| 496 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 497 | static int wb_smsc_wdt_notify_sys(struct notifier_block *this, |
| 498 | unsigned long code, void *unused) |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 499 | { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 500 | if (code == SYS_DOWN || code == SYS_HALT) { |
| 501 | /* set timeout to 0, to avoid possible race-condition */ |
| 502 | timeout = 0; |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 503 | wb_smsc_wdt_disable(); |
| 504 | } |
| 505 | return NOTIFY_DONE; |
| 506 | } |
| 507 | |
| 508 | /* -- Module's structures ---------------------------------------*/ |
| 509 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 510 | static const struct file_operations wb_smsc_wdt_fops = { |
| 511 | .owner = THIS_MODULE, |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 512 | .llseek = no_llseek, |
| 513 | .write = wb_smsc_wdt_write, |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 514 | .unlocked_ioctl = wb_smsc_wdt_ioctl, |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 515 | .open = wb_smsc_wdt_open, |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 516 | .release = wb_smsc_wdt_release, |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 517 | }; |
| 518 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 519 | static struct notifier_block wb_smsc_wdt_notifier = { |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 520 | .notifier_call = wb_smsc_wdt_notify_sys, |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 521 | }; |
| 522 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 523 | static struct miscdevice wb_smsc_wdt_miscdev = { |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 524 | .minor = WATCHDOG_MINOR, |
| 525 | .name = "watchdog", |
| 526 | .fops = &wb_smsc_wdt_fops, |
| 527 | }; |
| 528 | |
| 529 | /* -- Module init functions -------------------------------------*/ |
| 530 | |
| 531 | /* module's "constructor" */ |
| 532 | |
| 533 | static int __init wb_smsc_wdt_init(void) |
| 534 | { |
| 535 | int ret; |
| 536 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 537 | printk(KERN_INFO "SMsC 37B787 watchdog component driver " |
| 538 | VERSION " initialising...\n"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 539 | |
| 540 | if (!request_region(IOPORT, IOPORT_SIZE, "SMsC 37B787 watchdog")) { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 541 | printk(KERN_ERR MODNAME "Unable to register IO port %#x\n", |
| 542 | IOPORT); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 543 | ret = -EBUSY; |
| 544 | goto out_pnp; |
| 545 | } |
| 546 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 547 | /* set new maximum, if it's too big */ |
| 548 | if (timeout > MAX_TIMEOUT) |
| 549 | timeout = MAX_TIMEOUT; |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 550 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 551 | /* init the watchdog timer */ |
| 552 | wb_smsc_wdt_initialize(); |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 553 | |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 554 | ret = register_reboot_notifier(&wb_smsc_wdt_notifier); |
| 555 | if (ret) { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 556 | printk(KERN_ERR MODNAME |
| 557 | "Unable to register reboot notifier err = %d\n", ret); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 558 | goto out_io; |
| 559 | } |
| 560 | |
| 561 | ret = misc_register(&wb_smsc_wdt_miscdev); |
| 562 | if (ret) { |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 563 | printk(KERN_ERR MODNAME |
| 564 | "Unable to register miscdev on minor %d\n", |
| 565 | WATCHDOG_MINOR); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 566 | goto out_rbt; |
| 567 | } |
| 568 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 569 | /* output info */ |
| 570 | printk(KERN_INFO MODNAME "Timeout set to %d %s.\n", |
| 571 | timeout, (unit == UNIT_SECOND) ? "second(s)" : "minute(s)"); |
| 572 | printk(KERN_INFO MODNAME |
| 573 | "Watchdog initialized and sleeping (nowayout=%d)...\n", |
| 574 | nowayout); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 575 | out_clean: |
| 576 | return ret; |
| 577 | |
| 578 | out_rbt: |
| 579 | unregister_reboot_notifier(&wb_smsc_wdt_notifier); |
| 580 | |
| 581 | out_io: |
| 582 | release_region(IOPORT, IOPORT_SIZE); |
| 583 | |
| 584 | out_pnp: |
| 585 | goto out_clean; |
Wim Van Sebroeck | 8386c8c | 2006-09-02 19:32:26 +0200 | [diff] [blame] | 586 | } |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 587 | |
| 588 | /* module's "destructor" */ |
| 589 | |
| 590 | static void __exit wb_smsc_wdt_exit(void) |
| 591 | { |
| 592 | /* Stop the timer before we leave */ |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 593 | if (!nowayout) { |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 594 | wb_smsc_wdt_shutdown(); |
| 595 | printk(KERN_INFO MODNAME "Watchdog disabled.\n"); |
| 596 | } |
| 597 | |
| 598 | misc_deregister(&wb_smsc_wdt_miscdev); |
| 599 | unregister_reboot_notifier(&wb_smsc_wdt_notifier); |
| 600 | release_region(IOPORT, IOPORT_SIZE); |
| 601 | |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 602 | printk(KERN_INFO "SMsC 37B787 watchdog component driver removed.\n"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | module_init(wb_smsc_wdt_init); |
| 606 | module_exit(wb_smsc_wdt_exit); |
| 607 | |
| 608 | MODULE_AUTHOR("Sven Anders <anders@anduras.de>"); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 609 | MODULE_DESCRIPTION("Driver for SMsC 37B787 watchdog component (Version " |
| 610 | VERSION ")"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 611 | MODULE_LICENSE("GPL"); |
| 612 | |
| 613 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 614 | |
| 615 | #ifdef SMSC_SUPPORT_MINUTES |
| 616 | module_param(unit, int, 0); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 617 | MODULE_PARM_DESC(unit, |
| 618 | "set unit to use, 0=seconds or 1=minutes, default is 0"); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 619 | #endif |
| 620 | |
Wim Van Sebroeck | aa1fd4d | 2006-09-02 20:53:19 +0200 | [diff] [blame] | 621 | module_param(timeout, int, 0); |
Sven Anders | 485ae77 | 2006-08-24 17:11:50 +0200 | [diff] [blame] | 622 | MODULE_PARM_DESC(timeout, "range is 1-255 units, default is 60"); |
| 623 | |
| 624 | module_param(nowayout, int, 0); |
Alan Cox | 5984679 | 2008-05-19 14:09:00 +0100 | [diff] [blame] | 625 | MODULE_PARM_DESC(nowayout, |
| 626 | "Watchdog cannot be stopped once started (default=" |
| 627 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |