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