Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Watchdog for the 7101 PMU version found in the ALi M1535 chipsets |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/moduleparam.h> |
| 10 | #include <linux/types.h> |
| 11 | #include <linux/miscdevice.h> |
| 12 | #include <linux/watchdog.h> |
| 13 | #include <linux/ioport.h> |
| 14 | #include <linux/notifier.h> |
| 15 | #include <linux/reboot.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/pci.h> |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 19 | #include <linux/uaccess.h> |
| 20 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define WATCHDOG_NAME "ALi_M1535" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */ |
| 24 | |
| 25 | /* internal variables */ |
| 26 | static unsigned long ali_is_open; |
| 27 | static char ali_expect_release; |
| 28 | static struct pci_dev *ali_pci; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 29 | static u32 ali_timeout_bits; /* stores the computed timeout */ |
Alexey Dobriyan | c7dfd0c | 2007-11-01 16:27:08 -0700 | [diff] [blame] | 30 | static DEFINE_SPINLOCK(ali_lock); /* Guards the hardware */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* module parameters */ |
| 33 | static int timeout = WATCHDOG_TIMEOUT; |
| 34 | module_param(timeout, int, 0); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 35 | MODULE_PARM_DESC(timeout, |
| 36 | "Watchdog timeout in seconds. (0 < timeout < 18000, default=" |
| 37 | __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Wim Van Sebroeck | 86a1e18 | 2012-03-05 16:51:11 +0100 | [diff] [blame] | 39 | static bool nowayout = WATCHDOG_NOWAYOUT; |
| 40 | module_param(nowayout, bool, 0); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 41 | MODULE_PARM_DESC(nowayout, |
| 42 | "Watchdog cannot be stopped once started (default=" |
| 43 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * ali_start - start watchdog countdown |
| 47 | * |
| 48 | * Starts the timer running providing the timer has a counter |
| 49 | * configuration set. |
| 50 | */ |
| 51 | |
| 52 | static void ali_start(void) |
| 53 | { |
| 54 | u32 val; |
| 55 | |
| 56 | spin_lock(&ali_lock); |
| 57 | |
| 58 | pci_read_config_dword(ali_pci, 0xCC, &val); |
| 59 | val &= ~0x3F; /* Mask count */ |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 60 | val |= (1 << 25) | ali_timeout_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | pci_write_config_dword(ali_pci, 0xCC, val); |
| 62 | |
| 63 | spin_unlock(&ali_lock); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * ali_stop - stop the timer countdown |
| 68 | * |
| 69 | * Stop the ALi watchdog countdown |
| 70 | */ |
| 71 | |
| 72 | static void ali_stop(void) |
| 73 | { |
| 74 | u32 val; |
| 75 | |
| 76 | spin_lock(&ali_lock); |
| 77 | |
| 78 | pci_read_config_dword(ali_pci, 0xCC, &val); |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 79 | val &= ~0x3F; /* Mask count to zero (disabled) */ |
| 80 | val &= ~(1 << 25); /* and for safety mask the reset enable */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | pci_write_config_dword(ali_pci, 0xCC, val); |
| 82 | |
| 83 | spin_unlock(&ali_lock); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * ali_keepalive - send a keepalive to the watchdog |
| 88 | * |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 89 | * Send a keepalive to the timer (actually we restart the timer). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | */ |
| 91 | |
| 92 | static void ali_keepalive(void) |
| 93 | { |
| 94 | ali_start(); |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * ali_settimer - compute the timer reload value |
| 99 | * @t: time in seconds |
| 100 | * |
| 101 | * Computes the timeout values needed |
| 102 | */ |
| 103 | |
| 104 | static int ali_settimer(int t) |
| 105 | { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 106 | if (t < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | return -EINVAL; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 108 | else if (t < 60) |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 109 | ali_timeout_bits = t|(1 << 6); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 110 | else if (t < 3600) |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 111 | ali_timeout_bits = (t / 60)|(1 << 7); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 112 | else if (t < 18000) |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 113 | ali_timeout_bits = (t / 300)|(1 << 6)|(1 << 7); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 114 | else |
| 115 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | timeout = t; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * /dev/watchdog handling |
| 123 | */ |
| 124 | |
| 125 | /* |
| 126 | * ali_write - writes to ALi watchdog |
| 127 | * @file: file from VFS |
| 128 | * @data: user address of data |
| 129 | * @len: length of data |
| 130 | * @ppos: pointer to the file offset |
| 131 | * |
| 132 | * Handle a write to the ALi watchdog. Writing to the file pings |
| 133 | * the watchdog and resets it. Writing the magic 'V' sequence allows |
| 134 | * the next close to turn off the watchdog. |
| 135 | */ |
| 136 | |
| 137 | static ssize_t ali_write(struct file *file, const char __user *data, |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 138 | size_t len, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | /* See if we got the magic character 'V' and reload the timer */ |
| 141 | if (len) { |
| 142 | if (!nowayout) { |
| 143 | size_t i; |
| 144 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 145 | /* note: just in case someone wrote the |
| 146 | magic character five months ago... */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | ali_expect_release = 0; |
| 148 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 149 | /* scan to see whether or not we got |
| 150 | the magic character */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | for (i = 0; i != len; i++) { |
| 152 | char c; |
Wim Van Sebroeck | 7944d3a | 2008-08-06 20:19:41 +0000 | [diff] [blame] | 153 | if (get_user(c, data + i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return -EFAULT; |
| 155 | if (c == 'V') |
| 156 | ali_expect_release = 42; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* someone wrote to us, we should reload the timer */ |
| 161 | ali_start(); |
| 162 | } |
| 163 | return len; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * ali_ioctl - handle watchdog ioctls |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * @file: VFS file pointer |
| 169 | * @cmd: ioctl number |
| 170 | * @arg: arguments to the ioctl |
| 171 | * |
| 172 | * Handle the watchdog ioctls supported by the ALi driver. Really |
| 173 | * we want an extension to enable irq ack monitoring and the like |
| 174 | */ |
| 175 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 176 | static long ali_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | void __user *argp = (void __user *)arg; |
| 179 | int __user *p = argp; |
Wim Van Sebroeck | 42747d7 | 2009-12-26 18:55:22 +0000 | [diff] [blame] | 180 | static const struct watchdog_info ident = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | .options = WDIOF_KEEPALIVEPING | |
| 182 | WDIOF_SETTIMEOUT | |
| 183 | WDIOF_MAGICCLOSE, |
| 184 | .firmware_version = 0, |
| 185 | .identity = "ALi M1535 WatchDog Timer", |
| 186 | }; |
| 187 | |
| 188 | switch (cmd) { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 189 | case WDIOC_GETSUPPORT: |
| 190 | return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 192 | case WDIOC_GETSTATUS: |
| 193 | case WDIOC_GETBOOTSTATUS: |
| 194 | return put_user(0, p); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 195 | case WDIOC_SETOPTIONS: |
| 196 | { |
| 197 | int new_options, retval = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 199 | if (get_user(new_options, p)) |
| 200 | return -EFAULT; |
| 201 | if (new_options & WDIOS_DISABLECARD) { |
| 202 | ali_stop(); |
| 203 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 205 | if (new_options & WDIOS_ENABLECARD) { |
| 206 | ali_start(); |
| 207 | retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 209 | return retval; |
| 210 | } |
Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 211 | case WDIOC_KEEPALIVE: |
| 212 | ali_keepalive(); |
| 213 | return 0; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 214 | case WDIOC_SETTIMEOUT: |
| 215 | { |
| 216 | int new_timeout; |
| 217 | if (get_user(new_timeout, p)) |
| 218 | return -EFAULT; |
| 219 | if (ali_settimer(new_timeout)) |
| 220 | return -EINVAL; |
| 221 | ali_keepalive(); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 222 | } |
Gustavo A. R. Silva | bd490f8 | 2020-07-07 12:11:21 -0500 | [diff] [blame] | 223 | fallthrough; |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 224 | case WDIOC_GETTIMEOUT: |
| 225 | return put_user(timeout, p); |
| 226 | default: |
| 227 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * ali_open - handle open of ali watchdog |
| 233 | * @inode: inode from VFS |
| 234 | * @file: file from VFS |
| 235 | * |
| 236 | * Open the ALi watchdog device. Ensure only one person opens it |
| 237 | * at a time. Also start the watchdog running. |
| 238 | */ |
| 239 | |
| 240 | static int ali_open(struct inode *inode, struct file *file) |
| 241 | { |
| 242 | /* /dev/watchdog can only be opened once */ |
| 243 | if (test_and_set_bit(0, &ali_is_open)) |
| 244 | return -EBUSY; |
| 245 | |
| 246 | /* Activate */ |
| 247 | ali_start(); |
Kirill Smelkov | c5bf68f | 2019-03-26 23:51:19 +0300 | [diff] [blame] | 248 | return stream_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * ali_release - close an ALi watchdog |
| 253 | * @inode: inode from VFS |
| 254 | * @file: file from VFS |
| 255 | * |
| 256 | * Close the ALi watchdog device. Actual shutdown of the timer |
| 257 | * only occurs if the magic sequence has been set. |
| 258 | */ |
| 259 | |
| 260 | static int ali_release(struct inode *inode, struct file *file) |
| 261 | { |
| 262 | /* |
| 263 | * Shut off the timer. |
| 264 | */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 265 | if (ali_expect_release == 42) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | ali_stop(); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 267 | else { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 268 | pr_crit("Unexpected close, not stopping watchdog!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | ali_keepalive(); |
| 270 | } |
| 271 | clear_bit(0, &ali_is_open); |
| 272 | ali_expect_release = 0; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * ali_notify_sys - System down notifier |
| 278 | * |
| 279 | * Notifier for system down |
| 280 | */ |
| 281 | |
| 282 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 283 | static int ali_notify_sys(struct notifier_block *this, |
| 284 | unsigned long code, void *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 286 | if (code == SYS_DOWN || code == SYS_HALT) |
| 287 | ali_stop(); /* Turn the WDT off */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return NOTIFY_DONE; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Data for PCI driver interface |
| 293 | * |
| 294 | * This data only exists for exporting the supported |
| 295 | * PCI ids via MODULE_DEVICE_TABLE. We do not actually |
| 296 | * register a pci_driver, because someone else might one day |
| 297 | * want to register another driver on the same PCI id. |
| 298 | */ |
| 299 | |
Jingoo Han | bc17f9d | 2013-12-03 08:30:22 +0900 | [diff] [blame] | 300 | static const struct pci_device_id ali_pci_tbl[] __used = { |
Andrey Borzenkov | 6e420b7 | 2007-08-16 19:32:19 +0000 | [diff] [blame] | 301 | { PCI_VENDOR_ID_AL, 0x1533, PCI_ANY_ID, PCI_ANY_ID,}, |
Rolf Eike Beer | b7343f0 | 2005-07-27 11:43:42 -0700 | [diff] [blame] | 302 | { PCI_VENDOR_ID_AL, 0x1535, PCI_ANY_ID, PCI_ANY_ID,}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | { 0, }, |
| 304 | }; |
| 305 | MODULE_DEVICE_TABLE(pci, ali_pci_tbl); |
| 306 | |
| 307 | /* |
| 308 | * ali_find_watchdog - find a 1535 and 7101 |
| 309 | * |
| 310 | * Scans the PCI hardware for a 1535 series bridge and matching 7101 |
| 311 | * watchdog device. This may be overtight but it is better to be safe |
| 312 | */ |
| 313 | |
| 314 | static int __init ali_find_watchdog(void) |
| 315 | { |
| 316 | struct pci_dev *pdev; |
| 317 | u32 wdog; |
| 318 | |
Andrey Borzenkov | 6e420b7 | 2007-08-16 19:32:19 +0000 | [diff] [blame] | 319 | /* Check for a 1533/1535 series bridge */ |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 320 | pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1535, NULL); |
Andrey Borzenkov | 6e420b7 | 2007-08-16 19:32:19 +0000 | [diff] [blame] | 321 | if (pdev == NULL) |
| 322 | pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1533, NULL); |
| 323 | if (pdev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return -ENODEV; |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 325 | pci_dev_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
| 327 | /* Check for the a 7101 PMU */ |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 328 | pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x7101, NULL); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 329 | if (pdev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | return -ENODEV; |
| 331 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 332 | if (pci_enable_device(pdev)) { |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 333 | pci_dev_put(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return -EIO; |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 335 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | ali_pci = pdev; |
| 338 | |
| 339 | /* |
| 340 | * Initialize the timer bits |
| 341 | */ |
| 342 | pci_read_config_dword(pdev, 0xCC, &wdog); |
| 343 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 344 | /* Timer bits */ |
| 345 | wdog &= ~0x3F; |
| 346 | /* Issued events */ |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 347 | wdog &= ~((1 << 27)|(1 << 26)|(1 << 25)|(1 << 24)); |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 348 | /* No monitor bits */ |
Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 349 | wdog &= ~((1 << 16)|(1 << 13)|(1 << 12)|(1 << 11)|(1 << 10)|(1 << 9)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | pci_write_config_dword(pdev, 0xCC, wdog); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Kernel Interfaces |
| 358 | */ |
| 359 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 360 | static const struct file_operations ali_fops = { |
Wim Van Sebroeck | 5f3b275 | 2011-02-23 20:04:38 +0000 | [diff] [blame] | 361 | .owner = THIS_MODULE, |
| 362 | .llseek = no_llseek, |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 363 | .write = ali_write, |
| 364 | .unlocked_ioctl = ali_ioctl, |
Arnd Bergmann | b6dfb24 | 2019-06-03 14:23:09 +0200 | [diff] [blame] | 365 | .compat_ioctl = compat_ptr_ioctl, |
Wim Van Sebroeck | 5f3b275 | 2011-02-23 20:04:38 +0000 | [diff] [blame] | 366 | .open = ali_open, |
| 367 | .release = ali_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | static struct miscdevice ali_miscdev = { |
| 371 | .minor = WATCHDOG_MINOR, |
| 372 | .name = "watchdog", |
| 373 | .fops = &ali_fops, |
| 374 | }; |
| 375 | |
| 376 | static struct notifier_block ali_notifier = { |
| 377 | .notifier_call = ali_notify_sys, |
| 378 | }; |
| 379 | |
| 380 | /* |
| 381 | * watchdog_init - module initialiser |
| 382 | * |
| 383 | * Scan for a suitable watchdog and if so initialize it. Return an error |
| 384 | * if we cannot, the error causes the module to unload |
| 385 | */ |
| 386 | |
| 387 | static int __init watchdog_init(void) |
| 388 | { |
| 389 | int ret; |
| 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | /* Check whether or not the hardware watchdog is there */ |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 392 | if (ali_find_watchdog() != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
Alan Cox | 173d95b | 2008-05-19 14:04:57 +0100 | [diff] [blame] | 395 | /* Check that the timeout value is within it's range; |
| 396 | if not reset to the default */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (timeout < 1 || timeout >= 18000) { |
| 398 | timeout = WATCHDOG_TIMEOUT; |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 399 | pr_info("timeout value must be 0 < timeout < 18000, using %d\n", |
| 400 | timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Calculate the watchdog's timeout */ |
| 404 | ali_settimer(timeout); |
| 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | ret = register_reboot_notifier(&ali_notifier); |
| 407 | if (ret != 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 408 | pr_err("cannot register reboot notifier (err=%d)\n", ret); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 409 | goto out; |
| 410 | } |
| 411 | |
| 412 | ret = misc_register(&ali_miscdev); |
| 413 | if (ret != 0) { |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 414 | pr_err("cannot register miscdev on minor=%d (err=%d)\n", |
| 415 | WATCHDOG_MINOR, ret); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 416 | goto unreg_reboot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Joe Perches | 27c766a | 2012-02-15 15:06:19 -0800 | [diff] [blame] | 419 | pr_info("initialized. timeout=%d sec (nowayout=%d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | timeout, nowayout); |
| 421 | |
| 422 | out: |
| 423 | return ret; |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 424 | unreg_reboot: |
| 425 | unregister_reboot_notifier(&ali_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * watchdog_exit - module de-initialiser |
| 431 | * |
| 432 | * Called while unloading a successfully installed watchdog module. |
| 433 | */ |
| 434 | |
| 435 | static void __exit watchdog_exit(void) |
| 436 | { |
| 437 | /* Stop the timer before we leave */ |
| 438 | ali_stop(); |
| 439 | |
| 440 | /* Deregister */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | misc_deregister(&ali_miscdev); |
Wim Van Sebroeck | c6cb13a | 2007-12-26 20:32:51 +0000 | [diff] [blame] | 442 | unregister_reboot_notifier(&ali_notifier); |
Jiri Slaby | 02be2ee | 2006-07-18 18:29:00 +0159 | [diff] [blame] | 443 | pci_dev_put(ali_pci); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | module_init(watchdog_init); |
| 447 | module_exit(watchdog_exit); |
| 448 | |
| 449 | MODULE_AUTHOR("Alan Cox"); |
| 450 | MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver"); |
| 451 | MODULE_LICENSE("GPL"); |