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