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 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 35 | #include <linux/cdev.h> /* For character device */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 36 | #include <linux/errno.h> /* For the -ENODEV/... values */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 37 | #include <linux/fs.h> /* For file operations */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 38 | #include <linux/init.h> /* For __init/__exit/... */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 39 | #include <linux/kernel.h> /* For printk/panic/... */ |
| 40 | #include <linux/kref.h> /* For data references */ |
| 41 | #include <linux/miscdevice.h> /* For handling misc devices */ |
| 42 | #include <linux/module.h> /* For module stuff/... */ |
| 43 | #include <linux/mutex.h> /* For mutexes */ |
| 44 | #include <linux/slab.h> /* For memory functions */ |
| 45 | #include <linux/types.h> /* For standard types (like size_t) */ |
| 46 | #include <linux/watchdog.h> /* For watchdog specific items */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 47 | #include <linux/uaccess.h> /* For copy_to_user/put_user/... */ |
| 48 | |
Wim Van Sebroeck | 6cfb5aa | 2012-05-21 15:31:06 +0200 | [diff] [blame] | 49 | #include "watchdog_core.h" |
H Hartley Sweeten | 09a46e7 | 2012-04-20 13:28:24 -0700 | [diff] [blame] | 50 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 51 | /* |
| 52 | * struct watchdog_core_data - watchdog core internal data |
| 53 | * @kref: Reference count. |
| 54 | * @cdev: The watchdog's Character device. |
| 55 | * @wdd: Pointer to watchdog device. |
| 56 | * @lock: Lock for watchdog core. |
| 57 | * @status: Watchdog core internal status bits. |
| 58 | */ |
| 59 | struct watchdog_core_data { |
| 60 | struct kref kref; |
| 61 | struct cdev cdev; |
| 62 | struct watchdog_device *wdd; |
| 63 | struct mutex lock; |
| 64 | unsigned long status; /* Internal status bits */ |
| 65 | #define _WDOG_DEV_OPEN 0 /* Opened ? */ |
| 66 | #define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */ |
| 67 | }; |
| 68 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 69 | /* the dev_t structure to store the dynamically allocated watchdog devices */ |
| 70 | static dev_t watchdog_devt; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 71 | /* Reference to watchdog device behind /dev/watchdog */ |
| 72 | static struct watchdog_core_data *old_wd_data; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * watchdog_ping: ping the watchdog. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 76 | * @wdd: the watchdog device to ping |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 77 | * |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 78 | * The caller must hold wd_data->lock. |
| 79 | * |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 80 | * If the watchdog has no own ping operation then it needs to be |
| 81 | * restarted via the start operation. This wrapper function does |
| 82 | * exactly that. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 83 | * We only ping when the watchdog device is running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 86 | static int watchdog_ping(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 87 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 88 | int err; |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 89 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 90 | if (!watchdog_active(wdd)) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 91 | return 0; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 92 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 93 | if (wdd->ops->ping) |
| 94 | err = wdd->ops->ping(wdd); /* ping the watchdog */ |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 95 | else |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 96 | err = wdd->ops->start(wdd); /* restart watchdog */ |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 97 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 98 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
| 102 | * watchdog_start: wrapper to start the watchdog. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 103 | * @wdd: the watchdog device to start |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 104 | * |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 105 | * The caller must hold wd_data->lock. |
| 106 | * |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 107 | * Start the watchdog if it is not active and mark it active. |
| 108 | * This function returns zero on success or a negative errno code for |
| 109 | * failure. |
| 110 | */ |
| 111 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 112 | static int watchdog_start(struct watchdog_device *wdd) |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 113 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 114 | int err; |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 115 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 116 | if (watchdog_active(wdd)) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 117 | return 0; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 118 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 119 | err = wdd->ops->start(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 120 | if (err == 0) |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 121 | set_bit(WDOG_ACTIVE, &wdd->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 122 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 123 | return err; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
| 127 | * watchdog_stop: wrapper to stop the watchdog. |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 128 | * @wdd: the watchdog device to stop |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 129 | * |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 130 | * The caller must hold wd_data->lock. |
| 131 | * |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 132 | * Stop the watchdog if it is still active and unmark it active. |
| 133 | * This function returns zero on success or a negative errno code for |
| 134 | * failure. |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 135 | * If the 'nowayout' feature was set, the watchdog cannot be stopped. |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 136 | */ |
| 137 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 138 | static int watchdog_stop(struct watchdog_device *wdd) |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 139 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 140 | int err; |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 141 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 142 | if (!watchdog_active(wdd)) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 143 | return 0; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 144 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 145 | if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) { |
Guenter Roeck | 0254e95 | 2016-01-03 15:11:58 -0800 | [diff] [blame] | 146 | pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n", |
| 147 | wdd->id); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 148 | return -EBUSY; |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 149 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 150 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 151 | err = wdd->ops->stop(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 152 | if (err == 0) |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 153 | clear_bit(WDOG_ACTIVE, &wdd->status); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 154 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 155 | return err; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * watchdog_get_status: wrapper to get the watchdog status |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 160 | * @wdd: the watchdog device to get the status from |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 161 | * |
| 162 | * The caller must hold wd_data->lock. |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 163 | * |
| 164 | * Get the watchdog's status flags. |
| 165 | */ |
| 166 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 167 | static unsigned int watchdog_get_status(struct watchdog_device *wdd) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 168 | { |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 169 | if (!wdd->ops->status) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 170 | return 0; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 171 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 172 | return wdd->ops->status(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * watchdog_set_timeout: set the watchdog timer timeout |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 177 | * @wdd: the watchdog device to set the timeout for |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 178 | * @timeout: timeout to set in seconds |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 179 | * |
| 180 | * The caller must hold wd_data->lock. |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 181 | */ |
| 182 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 183 | static int watchdog_set_timeout(struct watchdog_device *wdd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 184 | unsigned int timeout) |
| 185 | { |
Guenter Roeck | fb32e9b | 2016-02-28 13:12:14 -0800 | [diff] [blame^] | 186 | int err = 0; |
| 187 | |
| 188 | if (!(wdd->info->options & WDIOF_SETTIMEOUT)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 189 | return -EOPNOTSUPP; |
| 190 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 191 | if (watchdog_timeout_invalid(wdd, timeout)) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | |
Guenter Roeck | fb32e9b | 2016-02-28 13:12:14 -0800 | [diff] [blame^] | 194 | if (wdd->ops->set_timeout) |
| 195 | err = wdd->ops->set_timeout(wdd, timeout); |
| 196 | else |
| 197 | wdd->timeout = timeout; |
| 198 | |
| 199 | return err; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* |
| 203 | * watchdog_get_timeleft: wrapper to get the time left before a reboot |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 204 | * @wdd: the watchdog device to get the remaining time from |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 205 | * @timeleft: the time that's left |
| 206 | * |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 207 | * The caller must hold wd_data->lock. |
| 208 | * |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 209 | * Get the time before a watchdog will reboot (if not pinged). |
| 210 | */ |
| 211 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 212 | static int watchdog_get_timeleft(struct watchdog_device *wdd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 213 | unsigned int *timeleft) |
| 214 | { |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 215 | *timeleft = 0; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 216 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 217 | if (!wdd->ops->get_timeleft) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 218 | return -EOPNOTSUPP; |
| 219 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 220 | *timeleft = wdd->ops->get_timeleft(wdd); |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 221 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 222 | return 0; |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 225 | #ifdef CONFIG_WATCHDOG_SYSFS |
| 226 | static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr, |
| 227 | char *buf) |
| 228 | { |
| 229 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 230 | |
| 231 | return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status)); |
| 232 | } |
| 233 | static DEVICE_ATTR_RO(nowayout); |
| 234 | |
| 235 | static ssize_t status_show(struct device *dev, struct device_attribute *attr, |
| 236 | char *buf) |
| 237 | { |
| 238 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 239 | struct watchdog_core_data *wd_data = wdd->wd_data; |
| 240 | unsigned int status; |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 241 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 242 | mutex_lock(&wd_data->lock); |
| 243 | status = watchdog_get_status(wdd); |
| 244 | mutex_unlock(&wd_data->lock); |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 245 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 246 | return sprintf(buf, "%u\n", status); |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 247 | } |
| 248 | static DEVICE_ATTR_RO(status); |
| 249 | |
| 250 | static ssize_t bootstatus_show(struct device *dev, |
| 251 | struct device_attribute *attr, char *buf) |
| 252 | { |
| 253 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 254 | |
| 255 | return sprintf(buf, "%u\n", wdd->bootstatus); |
| 256 | } |
| 257 | static DEVICE_ATTR_RO(bootstatus); |
| 258 | |
| 259 | static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr, |
| 260 | char *buf) |
| 261 | { |
| 262 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 263 | struct watchdog_core_data *wd_data = wdd->wd_data; |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 264 | ssize_t status; |
| 265 | unsigned int val; |
| 266 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 267 | mutex_lock(&wd_data->lock); |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 268 | status = watchdog_get_timeleft(wdd, &val); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 269 | mutex_unlock(&wd_data->lock); |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 270 | if (!status) |
| 271 | status = sprintf(buf, "%u\n", val); |
| 272 | |
| 273 | return status; |
| 274 | } |
| 275 | static DEVICE_ATTR_RO(timeleft); |
| 276 | |
| 277 | static ssize_t timeout_show(struct device *dev, struct device_attribute *attr, |
| 278 | char *buf) |
| 279 | { |
| 280 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 281 | |
| 282 | return sprintf(buf, "%u\n", wdd->timeout); |
| 283 | } |
| 284 | static DEVICE_ATTR_RO(timeout); |
| 285 | |
| 286 | static ssize_t identity_show(struct device *dev, struct device_attribute *attr, |
| 287 | char *buf) |
| 288 | { |
| 289 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 290 | |
| 291 | return sprintf(buf, "%s\n", wdd->info->identity); |
| 292 | } |
| 293 | static DEVICE_ATTR_RO(identity); |
| 294 | |
| 295 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 296 | char *buf) |
| 297 | { |
| 298 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 299 | |
| 300 | if (watchdog_active(wdd)) |
| 301 | return sprintf(buf, "active\n"); |
| 302 | |
| 303 | return sprintf(buf, "inactive\n"); |
| 304 | } |
| 305 | static DEVICE_ATTR_RO(state); |
| 306 | |
| 307 | static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr, |
| 308 | int n) |
| 309 | { |
| 310 | struct device *dev = container_of(kobj, struct device, kobj); |
| 311 | struct watchdog_device *wdd = dev_get_drvdata(dev); |
| 312 | umode_t mode = attr->mode; |
| 313 | |
| 314 | if (attr == &dev_attr_status.attr && !wdd->ops->status) |
| 315 | mode = 0; |
| 316 | else if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft) |
| 317 | mode = 0; |
| 318 | |
| 319 | return mode; |
| 320 | } |
| 321 | static struct attribute *wdt_attrs[] = { |
| 322 | &dev_attr_state.attr, |
| 323 | &dev_attr_identity.attr, |
| 324 | &dev_attr_timeout.attr, |
| 325 | &dev_attr_timeleft.attr, |
| 326 | &dev_attr_bootstatus.attr, |
| 327 | &dev_attr_status.attr, |
| 328 | &dev_attr_nowayout.attr, |
| 329 | NULL, |
| 330 | }; |
| 331 | |
| 332 | static const struct attribute_group wdt_group = { |
| 333 | .attrs = wdt_attrs, |
| 334 | .is_visible = wdt_is_visible, |
| 335 | }; |
| 336 | __ATTRIBUTE_GROUPS(wdt); |
| 337 | #else |
| 338 | #define wdt_groups NULL |
| 339 | #endif |
| 340 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 341 | /* |
| 342 | * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 343 | * @wdd: the watchdog device to do the ioctl on |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 344 | * @cmd: watchdog command |
| 345 | * @arg: argument pointer |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 346 | * |
| 347 | * The caller must hold wd_data->lock. |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 348 | */ |
| 349 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 350 | static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd, |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 351 | unsigned long arg) |
| 352 | { |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 353 | if (!wdd->ops->ioctl) |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 354 | return -ENOIOCTLCMD; |
| 355 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 356 | return wdd->ops->ioctl(wdd, cmd, arg); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * watchdog_write: writes to the watchdog. |
| 361 | * @file: file from VFS |
| 362 | * @data: user address of data |
| 363 | * @len: length of data |
| 364 | * @ppos: pointer to the file offset |
| 365 | * |
| 366 | * 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] | 367 | * Writing the magic 'V' sequence allows the next close to turn |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 368 | * off the watchdog (if 'nowayout' is not set). |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 369 | */ |
| 370 | |
| 371 | static ssize_t watchdog_write(struct file *file, const char __user *data, |
| 372 | size_t len, loff_t *ppos) |
| 373 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 374 | struct watchdog_core_data *wd_data = file->private_data; |
| 375 | struct watchdog_device *wdd; |
| 376 | int err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 377 | size_t i; |
| 378 | char c; |
| 379 | |
| 380 | if (len == 0) |
| 381 | return 0; |
| 382 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 383 | /* |
| 384 | * Note: just in case someone wrote the magic character |
| 385 | * five months ago... |
| 386 | */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 387 | clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status); |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 388 | |
| 389 | /* scan to see whether or not we got the magic character */ |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 390 | for (i = 0; i != len; i++) { |
| 391 | if (get_user(c, data + i)) |
| 392 | return -EFAULT; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 393 | if (c == 'V') |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 394 | set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /* someone wrote to us, so we send the watchdog a keepalive ping */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 398 | |
| 399 | err = -ENODEV; |
| 400 | mutex_lock(&wd_data->lock); |
| 401 | wdd = wd_data->wdd; |
| 402 | if (wdd) |
| 403 | err = watchdog_ping(wdd); |
| 404 | mutex_unlock(&wd_data->lock); |
| 405 | |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 406 | if (err < 0) |
| 407 | return err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 408 | |
| 409 | return len; |
| 410 | } |
| 411 | |
| 412 | /* |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 413 | * watchdog_ioctl: handle the different ioctl's for the watchdog device. |
| 414 | * @file: file handle to the device |
| 415 | * @cmd: watchdog command |
| 416 | * @arg: argument pointer |
| 417 | * |
| 418 | * The watchdog API defines a common set of functions for all watchdogs |
| 419 | * according to their available features. |
| 420 | */ |
| 421 | |
| 422 | static long watchdog_ioctl(struct file *file, unsigned int cmd, |
| 423 | unsigned long arg) |
| 424 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 425 | struct watchdog_core_data *wd_data = file->private_data; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 426 | void __user *argp = (void __user *)arg; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 427 | struct watchdog_device *wdd; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 428 | int __user *p = argp; |
| 429 | unsigned int val; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 430 | int err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 431 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 432 | mutex_lock(&wd_data->lock); |
| 433 | |
| 434 | wdd = wd_data->wdd; |
| 435 | if (!wdd) { |
| 436 | err = -ENODEV; |
| 437 | goto out_ioctl; |
| 438 | } |
| 439 | |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 440 | err = watchdog_ioctl_op(wdd, cmd, arg); |
| 441 | if (err != -ENOIOCTLCMD) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 442 | goto out_ioctl; |
Wim Van Sebroeck | 78d88fc | 2011-07-22 18:59:49 +0000 | [diff] [blame] | 443 | |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 444 | switch (cmd) { |
| 445 | case WDIOC_GETSUPPORT: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 446 | err = copy_to_user(argp, wdd->info, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 447 | sizeof(struct watchdog_info)) ? -EFAULT : 0; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 448 | break; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 449 | case WDIOC_GETSTATUS: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 450 | val = watchdog_get_status(wdd); |
| 451 | err = put_user(val, p); |
| 452 | break; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 453 | case WDIOC_GETBOOTSTATUS: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 454 | err = put_user(wdd->bootstatus, p); |
| 455 | break; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 456 | case WDIOC_SETOPTIONS: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 457 | if (get_user(val, p)) { |
| 458 | err = -EFAULT; |
| 459 | break; |
| 460 | } |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 461 | if (val & WDIOS_DISABLECARD) { |
| 462 | err = watchdog_stop(wdd); |
| 463 | if (err < 0) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 464 | break; |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 465 | } |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 466 | if (val & WDIOS_ENABLECARD) |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 467 | err = watchdog_start(wdd); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 468 | break; |
Wim Van Sebroeck | c2dc00e | 2011-07-22 18:57:23 +0000 | [diff] [blame] | 469 | case WDIOC_KEEPALIVE: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 470 | if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) { |
| 471 | err = -EOPNOTSUPP; |
| 472 | break; |
| 473 | } |
| 474 | err = watchdog_ping(wdd); |
| 475 | break; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 476 | case WDIOC_SETTIMEOUT: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 477 | if (get_user(val, p)) { |
| 478 | err = -EFAULT; |
| 479 | break; |
| 480 | } |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 481 | err = watchdog_set_timeout(wdd, val); |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 482 | if (err < 0) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 483 | break; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 484 | /* If the watchdog is active then we send a keepalive ping |
| 485 | * to make sure that the watchdog keep's running (and if |
| 486 | * possible that it takes the new timeout) */ |
Alexander Usyskin | 5ef7966 | 2015-10-26 14:07:58 +0200 | [diff] [blame] | 487 | err = watchdog_ping(wdd); |
| 488 | if (err < 0) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 489 | break; |
Wim Van Sebroeck | 014d694 | 2011-07-22 18:58:21 +0000 | [diff] [blame] | 490 | /* Fall */ |
| 491 | case WDIOC_GETTIMEOUT: |
| 492 | /* timeout == 0 means that we don't know the timeout */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 493 | if (wdd->timeout == 0) { |
| 494 | err = -EOPNOTSUPP; |
| 495 | break; |
| 496 | } |
| 497 | err = put_user(wdd->timeout, p); |
| 498 | break; |
Viresh Kumar | fd7b673 | 2012-03-16 09:14:00 +0100 | [diff] [blame] | 499 | case WDIOC_GETTIMELEFT: |
Hans de Goede | 7a87982 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 500 | err = watchdog_get_timeleft(wdd, &val); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 501 | if (err < 0) |
| 502 | break; |
| 503 | err = put_user(val, p); |
| 504 | break; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 505 | default: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 506 | err = -ENOTTY; |
| 507 | break; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 508 | } |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 509 | |
| 510 | out_ioctl: |
| 511 | mutex_unlock(&wd_data->lock); |
| 512 | return err; |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 516 | * watchdog_open: open the /dev/watchdog* devices. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 517 | * @inode: inode of device |
| 518 | * @file: file handle to device |
| 519 | * |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 520 | * When the /dev/watchdog* device gets opened, we start the watchdog. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 521 | * Watch out: the /dev/watchdog device is single open, so we make sure |
| 522 | * it can only be opened once. |
| 523 | */ |
| 524 | |
| 525 | static int watchdog_open(struct inode *inode, struct file *file) |
| 526 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 527 | struct watchdog_core_data *wd_data; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 528 | struct watchdog_device *wdd; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 529 | int err; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 530 | |
| 531 | /* Get the corresponding watchdog device */ |
| 532 | if (imajor(inode) == MISC_MAJOR) |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 533 | wd_data = old_wd_data; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 534 | else |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 535 | wd_data = container_of(inode->i_cdev, struct watchdog_core_data, |
| 536 | cdev); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 537 | |
| 538 | /* the watchdog is single open! */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 539 | if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status)) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 540 | return -EBUSY; |
| 541 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 542 | wdd = wd_data->wdd; |
| 543 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 544 | /* |
| 545 | * If the /dev/watchdog device is open, we don't want the module |
| 546 | * to be unloaded. |
| 547 | */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 548 | if (!try_module_get(wdd->ops->owner)) { |
| 549 | err = -EBUSY; |
| 550 | goto out_clear; |
| 551 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 552 | |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 553 | err = watchdog_start(wdd); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 554 | if (err < 0) |
| 555 | goto out_mod; |
| 556 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 557 | file->private_data = wd_data; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 558 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 559 | kref_get(&wd_data->kref); |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 560 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 561 | /* dev/watchdog is a virtual (and thus non-seekable) filesystem */ |
| 562 | return nonseekable_open(inode, file); |
| 563 | |
| 564 | out_mod: |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 565 | module_put(wd_data->wdd->ops->owner); |
| 566 | out_clear: |
| 567 | clear_bit(_WDOG_DEV_OPEN, &wd_data->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 568 | return err; |
| 569 | } |
| 570 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 571 | static void watchdog_core_data_release(struct kref *kref) |
| 572 | { |
| 573 | struct watchdog_core_data *wd_data; |
| 574 | |
| 575 | wd_data = container_of(kref, struct watchdog_core_data, kref); |
| 576 | |
| 577 | kfree(wd_data); |
| 578 | } |
| 579 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 580 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 581 | * watchdog_release: release the watchdog device. |
| 582 | * @inode: inode of device |
| 583 | * @file: file handle to device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 584 | * |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 585 | * 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] | 586 | * stop the watchdog when we have received the magic char (and nowayout |
| 587 | * was not set), else the watchdog will keep running. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 588 | */ |
| 589 | |
| 590 | static int watchdog_release(struct inode *inode, struct file *file) |
| 591 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 592 | struct watchdog_core_data *wd_data = file->private_data; |
| 593 | struct watchdog_device *wdd; |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 594 | int err = -EBUSY; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 595 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 596 | mutex_lock(&wd_data->lock); |
| 597 | |
| 598 | wdd = wd_data->wdd; |
| 599 | if (!wdd) |
| 600 | goto done; |
| 601 | |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 602 | /* |
| 603 | * We only stop the watchdog if we received the magic character |
Wim Van Sebroeck | 7e192b9 | 2011-07-22 18:59:17 +0000 | [diff] [blame] | 604 | * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then |
| 605 | * watchdog_stop will fail. |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 606 | */ |
Hector Palacios | fcf9567 | 2013-04-08 17:06:32 +0200 | [diff] [blame] | 607 | if (!test_bit(WDOG_ACTIVE, &wdd->status)) |
| 608 | err = 0; |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 609 | else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) || |
Hector Palacios | fcf9567 | 2013-04-08 17:06:32 +0200 | [diff] [blame] | 610 | !(wdd->info->options & WDIOF_MAGICCLOSE)) |
Wim Van Sebroeck | 017cf08 | 2011-07-22 18:58:54 +0000 | [diff] [blame] | 611 | err = watchdog_stop(wdd); |
| 612 | |
| 613 | /* If the watchdog was not stopped, send a keepalive ping */ |
Wim Van Sebroeck | 234445b | 2011-07-22 18:57:55 +0000 | [diff] [blame] | 614 | if (err < 0) { |
Guenter Roeck | 0254e95 | 2016-01-03 15:11:58 -0800 | [diff] [blame] | 615 | pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 616 | watchdog_ping(wdd); |
| 617 | } |
| 618 | |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 619 | /* make sure that /dev/watchdog can be re-opened */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 620 | clear_bit(_WDOG_DEV_OPEN, &wd_data->status); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 621 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 622 | done: |
| 623 | mutex_unlock(&wd_data->lock); |
| 624 | /* Allow the owner module to be unloaded again */ |
| 625 | module_put(wd_data->cdev.owner); |
| 626 | kref_put(&wd_data->kref, watchdog_core_data_release); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static const struct file_operations watchdog_fops = { |
| 631 | .owner = THIS_MODULE, |
| 632 | .write = watchdog_write, |
Wim Van Sebroeck | 2fa0356 | 2011-07-22 18:56:38 +0000 | [diff] [blame] | 633 | .unlocked_ioctl = watchdog_ioctl, |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 634 | .open = watchdog_open, |
| 635 | .release = watchdog_release, |
| 636 | }; |
| 637 | |
| 638 | static struct miscdevice watchdog_miscdev = { |
| 639 | .minor = WATCHDOG_MINOR, |
| 640 | .name = "watchdog", |
| 641 | .fops = &watchdog_fops, |
| 642 | }; |
| 643 | |
| 644 | /* |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 645 | * watchdog_cdev_register: register watchdog character device |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 646 | * @wdd: watchdog device |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 647 | * @devno: character device number |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 648 | * |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 649 | * Register a watchdog character device including handling the legacy |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 650 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 651 | * thus we set it up like that. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 652 | */ |
| 653 | |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 654 | static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 655 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 656 | struct watchdog_core_data *wd_data; |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 657 | int err; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 658 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 659 | wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL); |
| 660 | if (!wd_data) |
| 661 | return -ENOMEM; |
| 662 | kref_init(&wd_data->kref); |
| 663 | mutex_init(&wd_data->lock); |
| 664 | |
| 665 | wd_data->wdd = wdd; |
| 666 | wdd->wd_data = wd_data; |
| 667 | |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 668 | if (wdd->id == 0) { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 669 | old_wd_data = wd_data; |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 670 | watchdog_miscdev.parent = wdd->parent; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 671 | err = misc_register(&watchdog_miscdev); |
| 672 | if (err != 0) { |
| 673 | pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 674 | wdd->info->identity, WATCHDOG_MINOR, err); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 675 | if (err == -EBUSY) |
| 676 | pr_err("%s: a legacy watchdog module is probably present.\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 677 | wdd->info->identity); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 678 | old_wd_data = NULL; |
| 679 | kfree(wd_data); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 680 | return err; |
| 681 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 684 | /* Fill in the data structures */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 685 | cdev_init(&wd_data->cdev, &watchdog_fops); |
| 686 | wd_data->cdev.owner = wdd->ops->owner; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 687 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 688 | /* Add the device */ |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 689 | err = cdev_add(&wd_data->cdev, devno, 1); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 690 | if (err) { |
| 691 | pr_err("watchdog%d unable to add device %d:%d\n", |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 692 | wdd->id, MAJOR(watchdog_devt), wdd->id); |
| 693 | if (wdd->id == 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 694 | misc_deregister(&watchdog_miscdev); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 695 | old_wd_data = NULL; |
| 696 | kref_put(&wd_data->kref, watchdog_core_data_release); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 697 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 698 | } |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 699 | return err; |
| 700 | } |
| 701 | |
| 702 | /* |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 703 | * watchdog_cdev_unregister: unregister watchdog character device |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 704 | * @watchdog: watchdog device |
| 705 | * |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 706 | * Unregister watchdog character device and if needed the legacy |
| 707 | * /dev/watchdog device. |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 708 | */ |
| 709 | |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 710 | static void watchdog_cdev_unregister(struct watchdog_device *wdd) |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 711 | { |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 712 | struct watchdog_core_data *wd_data = wdd->wd_data; |
Hans de Goede | e907df3 | 2012-05-22 11:40:26 +0200 | [diff] [blame] | 713 | |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 714 | cdev_del(&wd_data->cdev); |
Guenter Roeck | bc794ac | 2015-09-29 01:27:25 -0700 | [diff] [blame] | 715 | if (wdd->id == 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 716 | misc_deregister(&watchdog_miscdev); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 717 | old_wd_data = NULL; |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 718 | } |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 719 | |
| 720 | mutex_lock(&wd_data->lock); |
| 721 | wd_data->wdd = NULL; |
| 722 | wdd->wd_data = NULL; |
| 723 | mutex_unlock(&wd_data->lock); |
| 724 | |
| 725 | kref_put(&wd_data->kref, watchdog_core_data_release); |
Wim Van Sebroeck | 4331604 | 2011-07-22 18:55:18 +0000 | [diff] [blame] | 726 | } |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 727 | |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 728 | static struct class watchdog_class = { |
| 729 | .name = "watchdog", |
| 730 | .owner = THIS_MODULE, |
Pratyush Anand | 33b7112 | 2015-12-17 17:53:59 +0530 | [diff] [blame] | 731 | .dev_groups = wdt_groups, |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 732 | }; |
| 733 | |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 734 | /* |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 735 | * watchdog_dev_register: register a watchdog device |
| 736 | * @wdd: watchdog device |
| 737 | * |
| 738 | * Register a watchdog device including handling the legacy |
| 739 | * /dev/watchdog node. /dev/watchdog is actually a miscdevice and |
| 740 | * thus we set it up like that. |
| 741 | */ |
| 742 | |
| 743 | int watchdog_dev_register(struct watchdog_device *wdd) |
| 744 | { |
| 745 | struct device *dev; |
| 746 | dev_t devno; |
| 747 | int ret; |
| 748 | |
| 749 | devno = MKDEV(MAJOR(watchdog_devt), wdd->id); |
| 750 | |
| 751 | ret = watchdog_cdev_register(wdd, devno); |
| 752 | if (ret) |
| 753 | return ret; |
| 754 | |
Guenter Roeck | faa5847 | 2016-01-03 15:11:56 -0800 | [diff] [blame] | 755 | dev = device_create_with_groups(&watchdog_class, wdd->parent, |
| 756 | devno, wdd, wdd->groups, |
| 757 | "watchdog%d", wdd->id); |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 758 | if (IS_ERR(dev)) { |
| 759 | watchdog_cdev_unregister(wdd); |
| 760 | return PTR_ERR(dev); |
| 761 | } |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 762 | |
| 763 | return ret; |
| 764 | } |
| 765 | |
| 766 | /* |
| 767 | * watchdog_dev_unregister: unregister a watchdog device |
| 768 | * @watchdog: watchdog device |
| 769 | * |
| 770 | * Unregister watchdog device and if needed the legacy |
| 771 | * /dev/watchdog device. |
| 772 | */ |
| 773 | |
| 774 | void watchdog_dev_unregister(struct watchdog_device *wdd) |
| 775 | { |
Guenter Roeck | 0254e95 | 2016-01-03 15:11:58 -0800 | [diff] [blame] | 776 | device_destroy(&watchdog_class, wdd->wd_data->cdev.dev); |
Guenter Roeck | b4ffb19 | 2015-12-25 16:01:42 -0800 | [diff] [blame] | 777 | watchdog_cdev_unregister(wdd); |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /* |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 781 | * watchdog_dev_init: init dev part of watchdog core |
| 782 | * |
| 783 | * Allocate a range of chardev nodes to use for watchdog devices |
| 784 | */ |
| 785 | |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 786 | int __init watchdog_dev_init(void) |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 787 | { |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 788 | int err; |
| 789 | |
| 790 | err = class_register(&watchdog_class); |
| 791 | if (err < 0) { |
| 792 | pr_err("couldn't register class\n"); |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 793 | return err; |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog"); |
| 797 | if (err < 0) { |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 798 | pr_err("watchdog: unable to allocate char dev region\n"); |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 799 | class_unregister(&watchdog_class); |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 800 | return err; |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 801 | } |
| 802 | |
Guenter Roeck | 32ecc63 | 2015-12-25 16:01:40 -0800 | [diff] [blame] | 803 | return 0; |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * watchdog_dev_exit: exit dev part of watchdog core |
| 808 | * |
| 809 | * Release the range of chardev nodes used for watchdog devices |
| 810 | */ |
| 811 | |
| 812 | void __exit watchdog_dev_exit(void) |
| 813 | { |
| 814 | unregister_chrdev_region(watchdog_devt, MAX_DOGS); |
Pratyush Anand | 906d7a5 | 2015-12-17 17:53:58 +0530 | [diff] [blame] | 815 | class_unregister(&watchdog_class); |
Alan Cox | 45f5fed | 2012-05-10 21:48:59 +0200 | [diff] [blame] | 816 | } |