Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Wim Van Sebroeck | 0f112a8 | 2007-08-09 19:38:00 +0000 | [diff] [blame] | 2 | * Eurotech CPU-1220/1410/1420 on board WDT driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * (c) Copyright 2001 Ascensit <support@ascensit.com> |
| 5 | * (c) Copyright 2001 Rodolfo Giometti <giometti@ascensit.com> |
| 6 | * (c) Copyright 2002 Rob Radez <rob@osinvestor.com> |
| 7 | * |
| 8 | * Based on wdt.c. |
| 9 | * Original copyright messages: |
| 10 | * |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 11 | * (c) Copyright 1996-1997 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
Alan Cox | 29fa058 | 2008-10-27 15:17:56 +0000 | [diff] [blame] | 12 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 19 | * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide |
| 20 | * warranty for any of this software. This material is provided |
| 21 | * "AS-IS" and at no charge. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 23 | * (c) Copyright 1995 Alan Cox <alan@lxorguk.ukuu.org.uk>* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | /* Changelog: |
| 27 | * |
Wim Van Sebroeck | 0f112a8 | 2007-08-09 19:38:00 +0000 | [diff] [blame] | 28 | * 2001 - Rodolfo Giometti |
| 29 | * Initial release |
| 30 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * 2002/04/25 - Rob Radez |
| 32 | * clean up #includes |
| 33 | * clean up locking |
| 34 | * make __setup param unique |
| 35 | * proper options in watchdog_info |
| 36 | * add WDIOC_GETSTATUS and WDIOC_SETOPTIONS ioctls |
| 37 | * add expect_close support |
| 38 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * 2002.05.30 - Joel Becker <joel.becker@oracle.com> |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 40 | * Added Matt Domsch's nowayout module option. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | */ |
| 42 | |
Wim Van Sebroeck | 0f112a8 | 2007-08-09 19:38:00 +0000 | [diff] [blame] | 43 | /* |
| 44 | * The eurotech CPU-1220/1410/1420's watchdog is a part |
| 45 | * of the on-board SUPER I/O device SMSC FDC 37B782. |
| 46 | */ |
| 47 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 48 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 49 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/interrupt.h> |
| 51 | #include <linux/module.h> |
| 52 | #include <linux/moduleparam.h> |
| 53 | #include <linux/types.h> |
| 54 | #include <linux/miscdevice.h> |
| 55 | #include <linux/watchdog.h> |
| 56 | #include <linux/fs.h> |
| 57 | #include <linux/ioport.h> |
| 58 | #include <linux/notifier.h> |
| 59 | #include <linux/reboot.h> |
| 60 | #include <linux/init.h> |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 61 | #include <linux/io.h> |
| 62 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <asm/system.h> |
| 65 | |
| 66 | static unsigned long eurwdt_is_open; |
| 67 | static int eurwdt_timeout; |
| 68 | static char eur_expect_close; |
Axel Lin | 1334f32 | 2011-11-29 13:54:01 +0800 | [diff] [blame] | 69 | static DEFINE_SPINLOCK(eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * You must set these - there is no sane way to probe for this board. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | |
| 75 | static int io = 0x3f0; |
| 76 | static int irq = 10; |
| 77 | static char *ev = "int"; |
| 78 | |
| 79 | #define WDT_TIMEOUT 60 /* 1 minute */ |
| 80 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame^] | 81 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 82 | module_param(nowayout, bool, 0); |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 83 | MODULE_PARM_DESC(nowayout, |
| 84 | "Watchdog cannot be stopped once started (default=" |
| 85 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Some symbolic names |
| 89 | */ |
| 90 | |
| 91 | #define WDT_CTRL_REG 0x30 |
| 92 | #define WDT_OUTPIN_CFG 0xe2 |
| 93 | #define WDT_EVENT_INT 0x00 |
| 94 | #define WDT_EVENT_REBOOT 0x08 |
| 95 | #define WDT_UNIT_SEL 0xf1 |
| 96 | #define WDT_UNIT_SECS 0x80 |
| 97 | #define WDT_TIMEOUT_VAL 0xf2 |
| 98 | #define WDT_TIMER_CFG 0xf3 |
| 99 | |
| 100 | |
| 101 | module_param(io, int, 0); |
| 102 | MODULE_PARM_DESC(io, "Eurotech WDT io port (default=0x3f0)"); |
| 103 | module_param(irq, int, 0); |
| 104 | MODULE_PARM_DESC(irq, "Eurotech WDT irq (default=10)"); |
| 105 | module_param(ev, charp, 0); |
| 106 | MODULE_PARM_DESC(ev, "Eurotech WDT event type (default is `int')"); |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | * Programming support |
| 111 | */ |
| 112 | |
| 113 | static inline void eurwdt_write_reg(u8 index, u8 data) |
| 114 | { |
| 115 | outb(index, io); |
| 116 | outb(data, io+1); |
| 117 | } |
| 118 | |
| 119 | static inline void eurwdt_lock_chip(void) |
| 120 | { |
| 121 | outb(0xaa, io); |
| 122 | } |
| 123 | |
| 124 | static inline void eurwdt_unlock_chip(void) |
| 125 | { |
| 126 | outb(0x55, io); |
| 127 | eurwdt_write_reg(0x07, 0x08); /* set the logical device */ |
| 128 | } |
| 129 | |
| 130 | static inline void eurwdt_set_timeout(int timeout) |
| 131 | { |
| 132 | eurwdt_write_reg(WDT_TIMEOUT_VAL, (u8) timeout); |
| 133 | } |
| 134 | |
| 135 | static inline void eurwdt_disable_timer(void) |
| 136 | { |
| 137 | eurwdt_set_timeout(0); |
| 138 | } |
| 139 | |
| 140 | static void eurwdt_activate_timer(void) |
| 141 | { |
| 142 | eurwdt_disable_timer(); |
| 143 | eurwdt_write_reg(WDT_CTRL_REG, 0x01); /* activate the WDT */ |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 144 | eurwdt_write_reg(WDT_OUTPIN_CFG, |
| 145 | !strcmp("int", ev) ? WDT_EVENT_INT : WDT_EVENT_REBOOT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | /* Setting interrupt line */ |
| 148 | if (irq == 2 || irq > 15 || irq < 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 149 | pr_err("invalid irq number\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | irq = 0; /* if invalid we disable interrupt */ |
| 151 | } |
| 152 | if (irq == 0) |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 153 | pr_info("interrupt disabled\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 155 | eurwdt_write_reg(WDT_TIMER_CFG, irq << 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
| 157 | eurwdt_write_reg(WDT_UNIT_SEL, WDT_UNIT_SECS); /* we use seconds */ |
| 158 | eurwdt_set_timeout(0); /* the default timeout */ |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* |
| 163 | * Kernel methods. |
| 164 | */ |
| 165 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 166 | static irqreturn_t eurwdt_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 168 | pr_crit("timeout WDT timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | #ifdef ONLY_TESTING |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 171 | pr_crit("Would Reboot\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | #else |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 173 | pr_crit("Initiating system reboot\n"); |
Andrew Morton | cc1d3a9 | 2005-07-26 21:41:37 -0700 | [diff] [blame] | 174 | emergency_restart(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | #endif |
| 176 | return IRQ_HANDLED; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * eurwdt_ping: |
| 182 | * |
| 183 | * Reload counter one with the watchdog timeout. |
| 184 | */ |
| 185 | |
| 186 | static void eurwdt_ping(void) |
| 187 | { |
| 188 | /* Write the watchdog default value */ |
| 189 | eurwdt_set_timeout(eurwdt_timeout); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * eurwdt_write: |
| 194 | * @file: file handle to the watchdog |
| 195 | * @buf: buffer to write (unused as data does not matter here |
| 196 | * @count: count of bytes |
| 197 | * @ppos: pointer to the position to write. No seeks allowed |
| 198 | * |
| 199 | * A write to a watchdog device is defined as a keepalive signal. Any |
| 200 | * write of data will do, as we we don't define content meaning. |
| 201 | */ |
| 202 | |
| 203 | static ssize_t eurwdt_write(struct file *file, const char __user *buf, |
| 204 | size_t count, loff_t *ppos) |
| 205 | { |
Wim Van Sebroeck | 5f3b275 | 2011-02-23 20:04:38 +0000 | [diff] [blame] | 206 | if (count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | if (!nowayout) { |
| 208 | size_t i; |
| 209 | |
| 210 | eur_expect_close = 0; |
| 211 | |
| 212 | for (i = 0; i != count; i++) { |
| 213 | char c; |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 214 | if (get_user(c, buf + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return -EFAULT; |
| 216 | if (c == 'V') |
| 217 | eur_expect_close = 42; |
| 218 | } |
| 219 | } |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 220 | spin_lock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | eurwdt_ping(); /* the default timeout */ |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 222 | spin_unlock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return count; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * eurwdt_ioctl: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | * @file: file handle to the device |
| 230 | * @cmd: watchdog command |
| 231 | * @arg: argument pointer |
| 232 | * |
| 233 | * The watchdog API defines a common set of functions for all watchdogs |
| 234 | * according to their available features. |
| 235 | */ |
| 236 | |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 237 | static long eurwdt_ioctl(struct file *file, |
| 238 | unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | { |
| 240 | void __user *argp = (void __user *)arg; |
| 241 | int __user *p = argp; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 242 | static const struct watchdog_info ident = { |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 243 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT |
| 244 | | WDIOF_MAGICCLOSE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | .firmware_version = 1, |
| 246 | .identity = "WDT Eurotech CPU-1220/1410", |
| 247 | }; |
| 248 | |
| 249 | int time; |
| 250 | int options, retval = -EINVAL; |
| 251 | |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 252 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | case WDIOC_GETSUPPORT: |
| 254 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; |
| 255 | |
| 256 | case WDIOC_GETSTATUS: |
| 257 | case WDIOC_GETBOOTSTATUS: |
| 258 | return put_user(0, p); |
| 259 | |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 260 | case WDIOC_SETOPTIONS: |
| 261 | if (get_user(options, p)) |
| 262 | return -EFAULT; |
| 263 | spin_lock(&eurwdt_lock); |
| 264 | if (options & WDIOS_DISABLECARD) { |
| 265 | eurwdt_disable_timer(); |
| 266 | retval = 0; |
| 267 | } |
| 268 | if (options & WDIOS_ENABLECARD) { |
| 269 | eurwdt_activate_timer(); |
| 270 | eurwdt_ping(); |
| 271 | retval = 0; |
| 272 | } |
| 273 | spin_unlock(&eurwdt_lock); |
| 274 | return retval; |
| 275 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | case WDIOC_KEEPALIVE: |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 277 | spin_lock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | eurwdt_ping(); |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 279 | spin_unlock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return 0; |
| 281 | |
| 282 | case WDIOC_SETTIMEOUT: |
| 283 | if (copy_from_user(&time, p, sizeof(int))) |
| 284 | return -EFAULT; |
| 285 | |
| 286 | /* Sanity check */ |
| 287 | if (time < 0 || time > 255) |
| 288 | return -EINVAL; |
| 289 | |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 290 | spin_lock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | eurwdt_timeout = time; |
| 292 | eurwdt_set_timeout(time); |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 293 | spin_unlock(&eurwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | /* Fall */ |
| 295 | |
| 296 | case WDIOC_GETTIMEOUT: |
| 297 | return put_user(eurwdt_timeout, p); |
| 298 | |
Wim Van Sebroeck | 0c06090c | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 299 | default: |
| 300 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * eurwdt_open: |
| 306 | * @inode: inode of device |
| 307 | * @file: file handle to device |
| 308 | * |
| 309 | * The misc device has been opened. The watchdog device is single |
| 310 | * open and on opening we load the counter. |
| 311 | */ |
| 312 | |
| 313 | static int eurwdt_open(struct inode *inode, struct file *file) |
| 314 | { |
| 315 | if (test_and_set_bit(0, &eurwdt_is_open)) |
| 316 | return -EBUSY; |
| 317 | eurwdt_timeout = WDT_TIMEOUT; /* initial timeout */ |
| 318 | /* Activate the WDT */ |
| 319 | eurwdt_activate_timer(); |
| 320 | return nonseekable_open(inode, file); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * eurwdt_release: |
| 325 | * @inode: inode to board |
| 326 | * @file: file handle to board |
| 327 | * |
| 328 | * The watchdog has a configurable API. There is a religious dispute |
| 329 | * between people who want their watchdog to be able to shut down and |
| 330 | * those who want to be sure if the watchdog manager dies the machine |
| 331 | * reboots. In the former case we disable the counters, in the latter |
| 332 | * case you have to open it again very soon. |
| 333 | */ |
| 334 | |
| 335 | static int eurwdt_release(struct inode *inode, struct file *file) |
| 336 | { |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 337 | if (eur_expect_close == 42) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | eurwdt_disable_timer(); |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 339 | else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 340 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | eurwdt_ping(); |
| 342 | } |
| 343 | clear_bit(0, &eurwdt_is_open); |
| 344 | eur_expect_close = 0; |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * eurwdt_notify_sys: |
| 350 | * @this: our notifier block |
| 351 | * @code: the event being reported |
| 352 | * @unused: unused |
| 353 | * |
| 354 | * Our notifier is called on system shutdowns. We want to turn the card |
| 355 | * off at reboot otherwise the machine will reboot again during memory |
| 356 | * test or worse yet during the following fsck. This would suck, in fact |
| 357 | * trust me - if it happens it does suck. |
| 358 | */ |
| 359 | |
| 360 | static int eurwdt_notify_sys(struct notifier_block *this, unsigned long code, |
| 361 | void *unused) |
| 362 | { |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 363 | if (code == SYS_DOWN || code == SYS_HALT) |
| 364 | eurwdt_disable_timer(); /* Turn the card off */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | return NOTIFY_DONE; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Kernel Interfaces |
| 371 | */ |
| 372 | |
| 373 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 374 | static const struct file_operations eurwdt_fops = { |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 375 | .owner = THIS_MODULE, |
| 376 | .llseek = no_llseek, |
| 377 | .write = eurwdt_write, |
| 378 | .unlocked_ioctl = eurwdt_ioctl, |
| 379 | .open = eurwdt_open, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | .release = eurwdt_release, |
| 381 | }; |
| 382 | |
| 383 | static struct miscdevice eurwdt_miscdev = { |
| 384 | .minor = WATCHDOG_MINOR, |
| 385 | .name = "watchdog", |
| 386 | .fops = &eurwdt_fops, |
| 387 | }; |
| 388 | |
| 389 | /* |
| 390 | * The WDT card needs to learn about soft shutdowns in order to |
| 391 | * turn the timebomb registers off. |
| 392 | */ |
| 393 | |
| 394 | static struct notifier_block eurwdt_notifier = { |
| 395 | .notifier_call = eurwdt_notify_sys, |
| 396 | }; |
| 397 | |
| 398 | /** |
| 399 | * cleanup_module: |
| 400 | * |
| 401 | * Unload the watchdog. You cannot do this with any file handles open. |
| 402 | * If your watchdog is set to continue ticking on close and you unload |
| 403 | * it, well it keeps ticking. We won't get the interrupt but the board |
| 404 | * will not touch PC memory so all is fine. You just have to load a new |
| 405 | * module in 60 seconds or reboot. |
| 406 | */ |
| 407 | |
| 408 | static void __exit eurwdt_exit(void) |
| 409 | { |
| 410 | eurwdt_lock_chip(); |
| 411 | |
| 412 | misc_deregister(&eurwdt_miscdev); |
| 413 | |
| 414 | unregister_reboot_notifier(&eurwdt_notifier); |
| 415 | release_region(io, 2); |
| 416 | free_irq(irq, NULL); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * eurwdt_init: |
| 421 | * |
| 422 | * Set up the WDT watchdog board. After grabbing the resources |
| 423 | * we require we need also to unlock the device. |
| 424 | * The open() function will actually kick the board off. |
| 425 | */ |
| 426 | |
| 427 | static int __init eurwdt_init(void) |
| 428 | { |
| 429 | int ret; |
| 430 | |
Yong Zhang | 86b5912 | 2011-09-07 16:10:55 +0800 | [diff] [blame] | 431 | ret = request_irq(irq, eurwdt_interrupt, 0, "eurwdt", NULL); |
Alan Cox | 89ea242 | 2008-05-19 14:05:41 +0100 | [diff] [blame] | 432 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 433 | pr_err("IRQ %d is not free\n", irq); |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 434 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (!request_region(io, 2, "eurwdt")) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 438 | pr_err("IO %X is not free\n", io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | ret = -EBUSY; |
| 440 | goto outirq; |
| 441 | } |
| 442 | |
| 443 | ret = register_reboot_notifier(&eurwdt_notifier); |
| 444 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 445 | pr_err("can't register reboot notifier (err=%d)\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | goto outreg; |
| 447 | } |
| 448 | |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 449 | ret = misc_register(&eurwdt_miscdev); |
| 450 | if (ret) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 451 | pr_err("can't misc_register on minor=%d\n", WATCHDOG_MINOR); |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 452 | goto outreboot; |
| 453 | } |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | eurwdt_unlock_chip(); |
| 456 | |
| 457 | ret = 0; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 458 | pr_info("Eurotech WDT driver 0.01 at %X (Interrupt %d) - timeout event: %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | io, irq, (!strcmp("int", ev) ? "int" : "reboot")); |
| 460 | |
| 461 | out: |
| 462 | return ret; |
| 463 | |
Alexey Dobriyan | fb8f7ba | 2007-03-24 15:58:12 +0300 | [diff] [blame] | 464 | outreboot: |
| 465 | unregister_reboot_notifier(&eurwdt_notifier); |
| 466 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | outreg: |
| 468 | release_region(io, 2); |
| 469 | |
| 470 | outirq: |
| 471 | free_irq(irq, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | module_init(eurwdt_init); |
| 476 | module_exit(eurwdt_exit); |
| 477 | |
| 478 | MODULE_AUTHOR("Rodolfo Giometti"); |
| 479 | MODULE_DESCRIPTION("Driver for Eurotech CPU-1220/1410 on board watchdog"); |
| 480 | MODULE_LICENSE("GPL"); |
| 481 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |