Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * watchdog_dev.c |
| 3 | * |
| 4 | * (c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>, |
| 5 | * All Rights Reserved. |
| 6 | * |
| 7 | * (c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>. |
| 8 | * |
| 9 | * |
| 10 | * This source code is part of the generic code that can be used |
| 11 | * by all the watchdog timer drivers. |
| 12 | * |
| 13 | * This part of the generic code takes care of the following |
| 14 | * misc device: /dev/watchdog. |
| 15 | * |
| 16 | * Based on source code of the following authors: |
| 17 | * Matt Domsch <Matt_Domsch@dell.com>, |
| 18 | * Rob Radez <rob@osinvestor.com>, |
| 19 | * Rusty Lynch <rusty@linux.co.intel.com> |
| 20 | * Satyam Sharma <satyam@infradead.org> |
| 21 | * Randy Dunlap <randy.dunlap@oracle.com> |
| 22 | * |
| 23 | * This program is free software; you can redistribute it and/or |
| 24 | * modify it under the terms of the GNU General Public License |
| 25 | * as published by the Free Software Foundation; either version |
| 26 | * 2 of the License, or (at your option) any later version. |
| 27 | * |
| 28 | * Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw. |
| 29 | * admit liability nor provide warranty for any of this software. |
| 30 | * This material is provided "AS-IS" and at no charge. |
| 31 | */ |
| 32 | |
| 33 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 34 | |
| 35 | #include <linux/module.h> /* For module stuff/... */ |
| 36 | #include <linux/types.h> /* For standard types (like size_t) */ |
| 37 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
| 38 | #include <linux/kernel.h> /* For printk/panic/... */ |
| 39 | #include <linux/fs.h> /* For file operations */ |
| 40 | #include <linux/watchdog.h> /* For watchdog specific items */ |
| 41 | #include <linux/miscdevice.h> /* For handling misc devices */ |
| 42 | #include <linux/init.h> /* For __init/__exit/... */ |
| 43 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ |
| 44 | |
Wim Van Sebroeck | 6cfb5aa | 2012-05-21 15:31:06 +0200 | [diff] [blame] | 45 | #include "watchdog_core.h" |
H Hartley Sweeten | 09a46e7 | 2012-04-20 13:28:24 -0700 | [diff] [blame] | 46 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 47 | /* the dev_t structure to store the dynamically allocated watchdog devices */ |
| 48 | static dev_t watchdog_devt; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 49 | /* the watchdog device behind /dev/watchdog */ |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 50 | static struct watchdog_device *old_wdd; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * watchdog_ping: ping the watchdog. |
| 54 | * @wddev: the watchdog device to ping |
| 55 | * |
| 56 | * If the watchdog has no own ping operation then it needs to be |
| 57 | * restarted via the start operation. This wrapper function does |
| 58 | * exactly that. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 59 | * We only ping when the watchdog device is running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | static int watchdog_ping(struct watchdog_device *wddev) |
| 63 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 64 | int err = 0; |
| 65 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 66 | mutex_lock(&wddev->lock); |
| 67 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 68 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 69 | err = -ENODEV; |
| 70 | goto out_ping; |
| 71 | } |
| 72 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 73 | if (!watchdog_active(wddev)) |
| 74 | goto out_ping; |
| 75 | |
| 76 | if (wddev->ops->ping) |
| 77 | err = wddev->ops->ping(wddev); /* ping the watchdog */ |
| 78 | else |
| 79 | err = wddev->ops->start(wddev); /* restart watchdog */ |
| 80 | |
| 81 | out_ping: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 82 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 83 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
| 87 | * watchdog_start: wrapper to start the watchdog. |
| 88 | * @wddev: the watchdog device to start |
| 89 | * |
| 90 | * Start the watchdog if it is not active and mark it active. |
| 91 | * This function returns zero on success or a negative errno code for |
| 92 | * failure. |
| 93 | */ |
| 94 | |
| 95 | static int watchdog_start(struct watchdog_device *wddev) |
| 96 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 97 | int err = 0; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 98 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 99 | mutex_lock(&wddev->lock); |
| 100 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 101 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 102 | err = -ENODEV; |
| 103 | goto out_start; |
| 104 | } |
| 105 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 106 | if (watchdog_active(wddev)) |
| 107 | goto out_start; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 108 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 109 | err = wddev->ops->start(wddev); |
| 110 | if (err == 0) |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 111 | set_bit(WDOG_ACTIVE, &wddev->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 112 | |
| 113 | out_start: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 114 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 115 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * watchdog_stop: wrapper to stop the watchdog. |
| 120 | * @wddev: the watchdog device to stop |
| 121 | * |
| 122 | * Stop the watchdog if it is still active and unmark it active. |
| 123 | * This function returns zero on success or a negative errno code for |
| 124 | * failure. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 125 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 126 | */ |
| 127 | |
| 128 | static int watchdog_stop(struct watchdog_device *wddev) |
| 129 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 130 | int err = 0; |
| 131 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 132 | mutex_lock(&wddev->lock); |
| 133 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 134 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 135 | err = -ENODEV; |
| 136 | goto out_stop; |
| 137 | } |
| 138 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 139 | if (!watchdog_active(wddev)) |
| 140 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 141 | |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 142 | if (test_bit(WDOG_NO_WAY_OUT, &wddev->status)) { |
Alan Cox | 3dfd621 | 2012-05-11 12:00:22 +0200 | [diff] [blame] | 143 | dev_info(wddev->dev, "nowayout prevents watchdog being stopped!\n"); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 144 | err = -EBUSY; |
| 145 | goto out_stop; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 146 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 147 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 148 | err = wddev->ops->stop(wddev); |
| 149 | if (err == 0) |
H Hartley Sweeten | cb7efc0 | 2011-08-03 15:38:20 -0700 | [diff] [blame] | 150 | clear_bit(WDOG_ACTIVE, &wddev->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 151 | |
| 152 | out_stop: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 153 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 154 | return err; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * watchdog_get_status: wrapper to get the watchdog status |
| 159 | * @wddev: the watchdog device to get the status from |
| 160 | * @status: the status of the watchdog device |
| 161 | * |
| 162 | * Get the watchdog's status flags. |
| 163 | */ |
| 164 | |
| 165 | static int watchdog_get_status(struct watchdog_device *wddev, |
| 166 | unsigned int *status) |
| 167 | { |
| 168 | int err = 0; |
| 169 | |
| 170 | *status = 0; |
| 171 | if (!wddev->ops->status) |
| 172 | return -EOPNOTSUPP; |
| 173 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 174 | mutex_lock(&wddev->lock); |
| 175 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 176 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 177 | err = -ENODEV; |
| 178 | goto out_status; |
| 179 | } |
| 180 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 181 | *status = wddev->ops->status(wddev); |
| 182 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 183 | out_status: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 184 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 185 | return err; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * watchdog_set_timeout: set the watchdog timer timeout |
| 190 | * @wddev: the watchdog device to set the timeout for |
| 191 | * @timeout: timeout to set in seconds |
| 192 | */ |
| 193 | |
| 194 | static int watchdog_set_timeout(struct watchdog_device *wddev, |
| 195 | unsigned int timeout) |
| 196 | { |
| 197 | int err; |
| 198 | |
| 199 | if ((wddev->ops->set_timeout == NULL) || |
| 200 | !(wddev->info->options & WDIOF_SETTIMEOUT)) |
| 201 | return -EOPNOTSUPP; |
| 202 | |
Fabio Porcedda | 3048253 | 2013-01-08 11:04:10 +0100 | [diff] [blame] | 203 | if (watchdog_timeout_invalid(wddev, timeout)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 206 | mutex_lock(&wddev->lock); |
| 207 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 208 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 209 | err = -ENODEV; |
| 210 | goto out_timeout; |
| 211 | } |
| 212 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 213 | err = wddev->ops->set_timeout(wddev, timeout); |
| 214 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 215 | out_timeout: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 216 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 217 | return err; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * watchdog_get_timeleft: wrapper to get the time left before a reboot |
| 222 | * @wddev: the watchdog device to get the remaining time from |
| 223 | * @timeleft: the time that's left |
| 224 | * |
| 225 | * Get the time before a watchdog will reboot (if not pinged). |
| 226 | */ |
| 227 | |
| 228 | static int watchdog_get_timeleft(struct watchdog_device *wddev, |
| 229 | unsigned int *timeleft) |
| 230 | { |
| 231 | int err = 0; |
| 232 | |
| 233 | *timeleft = 0; |
| 234 | if (!wddev->ops->get_timeleft) |
| 235 | return -EOPNOTSUPP; |
| 236 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 237 | mutex_lock(&wddev->lock); |
| 238 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 239 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 240 | err = -ENODEV; |
| 241 | goto out_timeleft; |
| 242 | } |
| 243 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 244 | *timeleft = wddev->ops->get_timeleft(wddev); |
| 245 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 246 | out_timeleft: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 247 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 248 | return err; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined |
| 253 | * @wddev: the watchdog device to do the ioctl on |
| 254 | * @cmd: watchdog command |
| 255 | * @arg: argument pointer |
| 256 | */ |
| 257 | |
| 258 | static int watchdog_ioctl_op(struct watchdog_device *wddev, unsigned int cmd, |
| 259 | unsigned long arg) |
| 260 | { |
| 261 | int err; |
| 262 | |
| 263 | if (!wddev->ops->ioctl) |
| 264 | return -ENOIOCTLCMD; |
| 265 | |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 266 | mutex_lock(&wddev->lock); |
| 267 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 268 | if (test_bit(WDOG_UNREGISTERED, &wddev->status)) { |
| 269 | err = -ENODEV; |
| 270 | goto out_ioctl; |
| 271 | } |
| 272 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 273 | err = wddev->ops->ioctl(wddev, cmd, arg); |
| 274 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 275 | out_ioctl: |
Hans de Goede | f4e9c82 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 276 | mutex_unlock(&wddev->lock); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 277 | return err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
| 281 | * watchdog_write: writes to the watchdog. |
| 282 | * @file: file from VFS |
| 283 | * @data: user address of data |
| 284 | * @len: length of data |
| 285 | * @ppos: pointer to the file offset |
| 286 | * |
| 287 | * A write to a watchdog device is defined as a keepalive ping. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 288 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 289 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 290 | */ |
| 291 | |
| 292 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 293 | size_t len, loff_t *ppos) |
| 294 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 295 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 296 | size_t i; |
| 297 | char c; |
| 298 | |
| 299 | if (len == 0) |
| 300 | return 0; |
| 301 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 302 | /* |
| 303 | * Note: just in case someone wrote the magic character |
| 304 | * five months ago... |
| 305 | */ |
| 306 | clear_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
| 307 | |
| 308 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 309 | for (i = 0; i != len; i++) { |
| 310 | if (get_user(c, data + i)) |
| 311 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 312 | if (c == 'V') |
| 313 | set_bit(WDOG_ALLOW_RELEASE, &wdd->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
| 317 | watchdog_ping(wdd); |
| 318 | |
| 319 | return len; |
| 320 | } |
| 321 | |
| 322 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 323 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 324 | * @file: file handle to the device |
| 325 | * @cmd: watchdog command |
| 326 | * @arg: argument pointer |
| 327 | * |
| 328 | * The watchdog API defines a common set of functions for all watchdogs |
| 329 | * according to their available features. |
| 330 | */ |
| 331 | |
| 332 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 333 | unsigned long arg) |
| 334 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 335 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 336 | void __user *argp = (void __user *)arg; |
| 337 | int __user *p = argp; |
| 338 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 339 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 340 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 341 | err = watchdog_ioctl_op(wdd, cmd, arg); |
| 342 | if (err != -ENOIOCTLCMD) |
| 343 | return err; |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 344 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 345 | switch (cmd) { |
| 346 | case WDIOC_GETSUPPORT: |
| 347 | return copy_to_user(argp, wdd->info, |
| 348 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
| 349 | case WDIOC_GETSTATUS: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 350 | err = watchdog_get_status(wdd, &val); |
Wim Van Sebroeck | 8b9468d | 2012-06-26 20:07:21 +0200 | [diff] [blame] | 351 | if (err == -ENODEV) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 352 | return err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 353 | return put_user(val, p); |
| 354 | case WDIOC_GETBOOTSTATUS: |
| 355 | return put_user(wdd->bootstatus, p); |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 356 | case WDIOC_SETOPTIONS: |
| 357 | if (get_user(val, p)) |
| 358 | return -EFAULT; |
| 359 | if (val & WDIOS_DISABLECARD) { |
| 360 | err = watchdog_stop(wdd); |
| 361 | if (err < 0) |
| 362 | return err; |
| 363 | } |
| 364 | if (val & WDIOS_ENABLECARD) { |
| 365 | err = watchdog_start(wdd); |
| 366 | if (err < 0) |
| 367 | return err; |
| 368 | } |
| 369 | return 0; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 370 | case WDIOC_KEEPALIVE: |
| 371 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) |
| 372 | return -EOPNOTSUPP; |
| 373 | watchdog_ping(wdd); |
| 374 | return 0; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 375 | case WDIOC_SETTIMEOUT: |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 376 | if (get_user(val, p)) |
| 377 | return -EFAULT; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 378 | err = watchdog_set_timeout(wdd, val); |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 379 | if (err < 0) |
| 380 | return err; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 381 | /* If the watchdog is active then we send a keepalive ping |
| 382 | * to make sure that the watchdog keep's running (and if |
| 383 | * possible that it takes the new timeout) */ |
| 384 | watchdog_ping(wdd); |
| 385 | /* Fall */ |
| 386 | case WDIOC_GETTIMEOUT: |
| 387 | /* timeout == 0 means that we don't know the timeout */ |
| 388 | if (wdd->timeout == 0) |
| 389 | return -EOPNOTSUPP; |
| 390 | return put_user(wdd->timeout, p); |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 391 | case WDIOC_GETTIMELEFT: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 392 | err = watchdog_get_timeleft(wdd, &val); |
| 393 | if (err) |
| 394 | return err; |
| 395 | return put_user(val, p); |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 396 | default: |
| 397 | return -ENOTTY; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 402 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 403 | * @inode: inode of device |
| 404 | * @file: file handle to device |
| 405 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 406 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 407 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 408 | * it can only be opened once. |
| 409 | */ |
| 410 | |
| 411 | static int watchdog_open(struct inode *inode, struct file *file) |
| 412 | { |
| 413 | int err = -EBUSY; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 414 | struct watchdog_device *wdd; |
| 415 | |
| 416 | /* Get the corresponding watchdog device */ |
| 417 | if (imajor(inode) == MISC_MAJOR) |
| 418 | wdd = old_wdd; |
| 419 | else |
| 420 | wdd = container_of(inode->i_cdev, struct watchdog_device, cdev); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 421 | |
| 422 | /* the watchdog is single open! */ |
| 423 | if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status)) |
| 424 | return -EBUSY; |
| 425 | |
| 426 | /* |
| 427 | * If the /dev/watchdog device is open, we don't want the module |
| 428 | * to be unloaded. |
| 429 | */ |
| 430 | if (!try_module_get(wdd->ops->owner)) |
| 431 | goto out; |
| 432 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 433 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 434 | if (err < 0) |
| 435 | goto out_mod; |
| 436 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 437 | file->private_data = wdd; |
| 438 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 439 | if (wdd->ops->ref) |
| 440 | wdd->ops->ref(wdd); |
| 441 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 442 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 443 | return nonseekable_open(inode, file); |
| 444 | |
| 445 | out_mod: |
| 446 | module_put(wdd->ops->owner); |
| 447 | out: |
| 448 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 449 | return err; |
| 450 | } |
| 451 | |
| 452 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 453 | * watchdog_release: release the watchdog device. |
| 454 | * @inode: inode of device |
| 455 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 456 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 457 | * This is the code for when /dev/watchdog gets closed. We will only |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 458 | * stop the watchdog when we have received the magic char (and nowayout |
| 459 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 460 | */ |
| 461 | |
| 462 | static int watchdog_release(struct inode *inode, struct file *file) |
| 463 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 464 | struct watchdog_device *wdd = file->private_data; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 465 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 466 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 467 | /* |
| 468 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 469 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 470 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 471 | */ |
| 472 | if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) || |
| 473 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
| 474 | err = watchdog_stop(wdd); |
| 475 | |
| 476 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 477 | if (err < 0) { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 478 | mutex_lock(&wdd->lock); |
| 479 | if (!test_bit(WDOG_UNREGISTERED, &wdd->status)) |
| 480 | dev_crit(wdd->dev, "watchdog did not stop!\n"); |
| 481 | mutex_unlock(&wdd->lock); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 482 | watchdog_ping(wdd); |
| 483 | } |
| 484 | |
| 485 | /* Allow the owner module to be unloaded again */ |
| 486 | module_put(wdd->ops->owner); |
| 487 | |
| 488 | /* make sure that /dev/watchdog can be re-opened */ |
| 489 | clear_bit(WDOG_DEV_OPEN, &wdd->status); |
| 490 | |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 491 | /* Note wdd may be gone after this, do not use after this! */ |
| 492 | if (wdd->ops->unref) |
| 493 | wdd->ops->unref(wdd); |
| 494 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static const struct file_operations watchdog_fops = { |
| 499 | .owner = THIS_MODULE, |
| 500 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 501 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 502 | .open = watchdog_open, |
| 503 | .release = watchdog_release, |
| 504 | }; |
| 505 | |
| 506 | static struct miscdevice watchdog_miscdev = { |
| 507 | .minor = WATCHDOG_MINOR, |
| 508 | .name = "watchdog", |
| 509 | .fops = &watchdog_fops, |
| 510 | }; |
| 511 | |
| 512 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 513 | * watchdog_dev_register: register a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 514 | * @watchdog: watchdog device |
| 515 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 516 | * Register a watchdog device including handling the legacy |
| 517 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 518 | * thus we set it up like that. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 519 | */ |
| 520 | |
| 521 | int watchdog_dev_register(struct watchdog_device *watchdog) |
| 522 | { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 523 | int err, devno; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 524 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 525 | if (watchdog->id == 0) { |
Guenter Roeck | 60403f7 | 2013-04-05 21:22:43 -0700 | [diff] [blame] | 526 | old_wdd = watchdog; |
Alan Cox | d6b469d | 2012-05-11 12:00:20 +0200 | [diff] [blame] | 527 | watchdog_miscdev.parent = watchdog->parent; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 528 | err = misc_register(&watchdog_miscdev); |
| 529 | if (err != 0) { |
| 530 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", |
| 531 | watchdog->info->identity, WATCHDOG_MINOR, err); |
| 532 | if (err == -EBUSY) |
| 533 | pr_err("%s: a legacy watchdog module is probably present.\n", |
| 534 | watchdog->info->identity); |
Guenter Roeck | 60403f7 | 2013-04-05 21:22:43 -0700 | [diff] [blame] | 535 | old_wdd = NULL; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 536 | return err; |
| 537 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 540 | /* Fill in the data structures */ |
| 541 | devno = MKDEV(MAJOR(watchdog_devt), watchdog->id); |
| 542 | cdev_init(&watchdog->cdev, &watchdog_fops); |
| 543 | watchdog->cdev.owner = watchdog->ops->owner; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 544 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 545 | /* Add the device */ |
| 546 | err = cdev_add(&watchdog->cdev, devno, 1); |
| 547 | if (err) { |
| 548 | pr_err("watchdog%d unable to add device %d:%d\n", |
| 549 | watchdog->id, MAJOR(watchdog_devt), watchdog->id); |
| 550 | if (watchdog->id == 0) { |
| 551 | misc_deregister(&watchdog_miscdev); |
| 552 | old_wdd = NULL; |
| 553 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 554 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 555 | return err; |
| 556 | } |
| 557 | |
| 558 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 559 | * watchdog_dev_unregister: unregister a watchdog device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 560 | * @watchdog: watchdog device |
| 561 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 562 | * Unregister the watchdog and if needed the legacy /dev/watchdog device. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 563 | */ |
| 564 | |
| 565 | int watchdog_dev_unregister(struct watchdog_device *watchdog) |
| 566 | { |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 567 | mutex_lock(&watchdog->lock); |
| 568 | set_bit(WDOG_UNREGISTERED, &watchdog->status); |
| 569 | mutex_unlock(&watchdog->lock); |
| 570 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 571 | cdev_del(&watchdog->cdev); |
| 572 | if (watchdog->id == 0) { |
| 573 | misc_deregister(&watchdog_miscdev); |
| 574 | old_wdd = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 575 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 576 | return 0; |
| 577 | } |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 578 | |
| 579 | /* |
| 580 | * watchdog_dev_init: init dev part of watchdog core |
| 581 | * |
| 582 | * Allocate a range of chardev nodes to use for watchdog devices |
| 583 | */ |
| 584 | |
| 585 | int __init watchdog_dev_init(void) |
| 586 | { |
| 587 | int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); |
| 588 | if (err < 0) |
| 589 | pr_err("watchdog: unable to allocate char dev region\n"); |
| 590 | return err; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * watchdog_dev_exit: exit dev part of watchdog core |
| 595 | * |
| 596 | * Release the range of chardev nodes used for watchdog devices |
| 597 | */ |
| 598 | |
| 599 | void __exit watchdog_dev_exit(void) |
| 600 | { |
| 601 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); |
| 602 | } |