blob: 5d3a9fa4856e0dee62e1c5db64e93cb9633642bb [file] [log] [blame]
Wim Van Sebroeck43316042011-07-22 18:55:18 +00001/*
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 Roeckb4ffb192015-12-25 16:01:42 -080035#include <linux/cdev.h> /* For character device */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000036#include <linux/errno.h> /* For the -ENODEV/... values */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000037#include <linux/fs.h> /* For file operations */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000038#include <linux/init.h> /* For __init/__exit/... */
Guenter Roeck664a3922016-02-28 13:12:15 -080039#include <linux/jiffies.h> /* For timeout functions */
Guenter Roeckb4ffb192015-12-25 16:01:42 -080040#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 Roeck664a3922016-02-28 13:12:15 -080048#include <linux/workqueue.h> /* For workqueue */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000049#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
50
Wim Van Sebroeck6cfb5aa2012-05-21 15:31:06 +020051#include "watchdog_core.h"
H Hartley Sweeten09a46e72012-04-20 13:28:24 -070052
Guenter Roeckb4ffb192015-12-25 16:01:42 -080053/*
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 */
61struct watchdog_core_data {
62 struct kref kref;
63 struct cdev cdev;
64 struct watchdog_device *wdd;
65 struct mutex lock;
Guenter Roeck664a3922016-02-28 13:12:15 -080066 unsigned long last_keepalive;
67 struct delayed_work work;
Guenter Roeckb4ffb192015-12-25 16:01:42 -080068 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 Cox45f5fed2012-05-10 21:48:59 +020073/* the dev_t structure to store the dynamically allocated watchdog devices */
74static dev_t watchdog_devt;
Guenter Roeckb4ffb192015-12-25 16:01:42 -080075/* Reference to watchdog device behind /dev/watchdog */
76static struct watchdog_core_data *old_wd_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000077
Guenter Roeck664a3922016-02-28 13:12:15 -080078static struct workqueue_struct *watchdog_wq;
79
80static 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 Roeckee142882016-02-28 13:12:16 -080095 return hm && ((watchdog_active(wdd) && t > hm) ||
96 (t && !watchdog_active(wdd) && watchdog_hw_running(wdd)));
Guenter Roeck664a3922016-02-28 13:12:15 -080097}
98
99static 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 Roeckee142882016-02-28 13:12:16 -0800112 if (!watchdog_active(wdd))
113 return keepalive_interval;
114
Guenter Roeck664a3922016-02-28 13:12:15 -0800115 /*
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
124static 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
138static 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 Sebroeck43316042011-07-22 18:55:18 +0000152/*
153 * watchdog_ping: ping the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700154 * @wdd: the watchdog device to ping
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000155 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800156 * The caller must hold wd_data->lock.
157 *
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000158 * 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 Sebroeck234445b2011-07-22 18:57:55 +0000161 * We only ping when the watchdog device is running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000162 */
163
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700164static int watchdog_ping(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000165{
Guenter Roeck664a3922016-02-28 13:12:15 -0800166 struct watchdog_core_data *wd_data = wdd->wd_data;
Hans de Goedee907df32012-05-22 11:40:26 +0200167
Guenter Roeckee142882016-02-28 13:12:16 -0800168 if (!watchdog_active(wdd) && !watchdog_hw_running(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800169 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +0200170
Guenter Roeck664a3922016-02-28 13:12:15 -0800171 wd_data->last_keepalive = jiffies;
172 return __watchdog_ping(wdd);
173}
Hans de Goede7a879822012-05-22 11:40:26 +0200174
Guenter Roeck664a3922016-02-28 13:12:15 -0800175static 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 Roeckee142882016-02-28 13:12:16 -0800185 if (wdd && (watchdog_active(wdd) || watchdog_hw_running(wdd)))
Guenter Roeck664a3922016-02-28 13:12:15 -0800186 __watchdog_ping(wdd);
187 mutex_unlock(&wd_data->lock);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000188}
189
190/*
191 * watchdog_start: wrapper to start the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700192 * @wdd: the watchdog device to start
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000193 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800194 * The caller must hold wd_data->lock.
195 *
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000196 * 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 Roeckbc794ac2015-09-29 01:27:25 -0700201static int watchdog_start(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000202{
Guenter Roeck664a3922016-02-28 13:12:15 -0800203 struct watchdog_core_data *wd_data = wdd->wd_data;
204 unsigned long started_at;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800205 int err;
Hans de Goedee907df32012-05-22 11:40:26 +0200206
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700207 if (watchdog_active(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800208 return 0;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000209
Guenter Roeck664a3922016-02-28 13:12:15 -0800210 started_at = jiffies;
Guenter Roeckee142882016-02-28 13:12:16 -0800211 if (watchdog_hw_running(wdd) && wdd->ops->ping)
212 err = wdd->ops->ping(wdd);
213 else
214 err = wdd->ops->start(wdd);
Guenter Roeck664a3922016-02-28 13:12:15 -0800215 if (err == 0) {
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700216 set_bit(WDOG_ACTIVE, &wdd->status);
Guenter Roeck664a3922016-02-28 13:12:15 -0800217 wd_data->last_keepalive = started_at;
218 watchdog_update_worker(wdd);
219 }
Hans de Goede7a879822012-05-22 11:40:26 +0200220
Hans de Goede7a879822012-05-22 11:40:26 +0200221 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000222}
223
224/*
225 * watchdog_stop: wrapper to stop the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700226 * @wdd: the watchdog device to stop
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000227 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800228 * The caller must hold wd_data->lock.
229 *
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000230 * 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 Sebroeck7e192b92011-07-22 18:59:17 +0000233 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000234 */
235
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700236static int watchdog_stop(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000237{
Guenter Roeckee142882016-02-28 13:12:16 -0800238 int err = 0;
Hans de Goedee907df32012-05-22 11:40:26 +0200239
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700240 if (!watchdog_active(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800241 return 0;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000242
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700243 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
Guenter Roeck0254e952016-01-03 15:11:58 -0800244 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
245 wdd->id);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800246 return -EBUSY;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000247 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000248
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700249 err = wdd->ops->stop(wdd);
Guenter Roeck664a3922016-02-28 13:12:15 -0800250 if (err == 0) {
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700251 clear_bit(WDOG_ACTIVE, &wdd->status);
Guenter Roeckee142882016-02-28 13:12:16 -0800252 watchdog_update_worker(wdd);
Guenter Roeck664a3922016-02-28 13:12:15 -0800253 }
Hans de Goede7a879822012-05-22 11:40:26 +0200254
Hans de Goede7a879822012-05-22 11:40:26 +0200255 return err;
256}
257
258/*
259 * watchdog_get_status: wrapper to get the watchdog status
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700260 * @wdd: the watchdog device to get the status from
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800261 *
262 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200263 *
264 * Get the watchdog's status flags.
265 */
266
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800267static unsigned int watchdog_get_status(struct watchdog_device *wdd)
Hans de Goede7a879822012-05-22 11:40:26 +0200268{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700269 if (!wdd->ops->status)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800270 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +0200271
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800272 return wdd->ops->status(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200273}
274
275/*
276 * watchdog_set_timeout: set the watchdog timer timeout
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700277 * @wdd: the watchdog device to set the timeout for
Hans de Goede7a879822012-05-22 11:40:26 +0200278 * @timeout: timeout to set in seconds
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800279 *
280 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200281 */
282
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700283static int watchdog_set_timeout(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200284 unsigned int timeout)
285{
Guenter Roeckfb32e9b2016-02-28 13:12:14 -0800286 int err = 0;
287
288 if (!(wdd->info->options & WDIOF_SETTIMEOUT))
Hans de Goede7a879822012-05-22 11:40:26 +0200289 return -EOPNOTSUPP;
290
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700291 if (watchdog_timeout_invalid(wdd, timeout))
Hans de Goede7a879822012-05-22 11:40:26 +0200292 return -EINVAL;
293
Guenter Roeckfb32e9b2016-02-28 13:12:14 -0800294 if (wdd->ops->set_timeout)
295 err = wdd->ops->set_timeout(wdd, timeout);
296 else
297 wdd->timeout = timeout;
298
Guenter Roeck664a3922016-02-28 13:12:15 -0800299 watchdog_update_worker(wdd);
300
Guenter Roeckfb32e9b2016-02-28 13:12:14 -0800301 return err;
Hans de Goede7a879822012-05-22 11:40:26 +0200302}
303
304/*
305 * watchdog_get_timeleft: wrapper to get the time left before a reboot
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700306 * @wdd: the watchdog device to get the remaining time from
Hans de Goede7a879822012-05-22 11:40:26 +0200307 * @timeleft: the time that's left
308 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800309 * The caller must hold wd_data->lock.
310 *
Hans de Goede7a879822012-05-22 11:40:26 +0200311 * Get the time before a watchdog will reboot (if not pinged).
312 */
313
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700314static int watchdog_get_timeleft(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200315 unsigned int *timeleft)
316{
Hans de Goede7a879822012-05-22 11:40:26 +0200317 *timeleft = 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800318
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700319 if (!wdd->ops->get_timeleft)
Hans de Goede7a879822012-05-22 11:40:26 +0200320 return -EOPNOTSUPP;
321
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700322 *timeleft = wdd->ops->get_timeleft(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200323
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800324 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +0200325}
326
Pratyush Anand33b71122015-12-17 17:53:59 +0530327#ifdef CONFIG_WATCHDOG_SYSFS
328static 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}
335static DEVICE_ATTR_RO(nowayout);
336
337static 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 Roeckb4ffb192015-12-25 16:01:42 -0800341 struct watchdog_core_data *wd_data = wdd->wd_data;
342 unsigned int status;
Pratyush Anand33b71122015-12-17 17:53:59 +0530343
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800344 mutex_lock(&wd_data->lock);
345 status = watchdog_get_status(wdd);
346 mutex_unlock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530347
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800348 return sprintf(buf, "%u\n", status);
Pratyush Anand33b71122015-12-17 17:53:59 +0530349}
350static DEVICE_ATTR_RO(status);
351
352static 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}
359static DEVICE_ATTR_RO(bootstatus);
360
361static 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 Roeckb4ffb192015-12-25 16:01:42 -0800365 struct watchdog_core_data *wd_data = wdd->wd_data;
Pratyush Anand33b71122015-12-17 17:53:59 +0530366 ssize_t status;
367 unsigned int val;
368
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800369 mutex_lock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530370 status = watchdog_get_timeleft(wdd, &val);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800371 mutex_unlock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530372 if (!status)
373 status = sprintf(buf, "%u\n", val);
374
375 return status;
376}
377static DEVICE_ATTR_RO(timeleft);
378
379static 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}
386static DEVICE_ATTR_RO(timeout);
387
388static 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}
395static DEVICE_ATTR_RO(identity);
396
397static 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}
407static DEVICE_ATTR_RO(state);
408
409static 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}
423static 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
434static 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 Goede7a879822012-05-22 11:40:26 +0200443/*
444 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700445 * @wdd: the watchdog device to do the ioctl on
Hans de Goede7a879822012-05-22 11:40:26 +0200446 * @cmd: watchdog command
447 * @arg: argument pointer
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800448 *
449 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200450 */
451
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700452static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
Hans de Goede7a879822012-05-22 11:40:26 +0200453 unsigned long arg)
454{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700455 if (!wdd->ops->ioctl)
Hans de Goede7a879822012-05-22 11:40:26 +0200456 return -ENOIOCTLCMD;
457
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800458 return wdd->ops->ioctl(wdd, cmd, arg);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000459}
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 Sebroeck017cf082011-07-22 18:58:54 +0000469 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000470 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000471 */
472
473static ssize_t watchdog_write(struct file *file, const char __user *data,
474 size_t len, loff_t *ppos)
475{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800476 struct watchdog_core_data *wd_data = file->private_data;
477 struct watchdog_device *wdd;
478 int err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000479 size_t i;
480 char c;
481
482 if (len == 0)
483 return 0;
484
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000485 /*
486 * Note: just in case someone wrote the magic character
487 * five months ago...
488 */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800489 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000490
491 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000492 for (i = 0; i != len; i++) {
493 if (get_user(c, data + i))
494 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000495 if (c == 'V')
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800496 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000497 }
498
499 /* someone wrote to us, so we send the watchdog a keepalive ping */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800500
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 Usyskin5ef79662015-10-26 14:07:58 +0200508 if (err < 0)
509 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000510
511 return len;
512}
513
514/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000515 * 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
524static long watchdog_ioctl(struct file *file, unsigned int cmd,
525 unsigned long arg)
526{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800527 struct watchdog_core_data *wd_data = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000528 void __user *argp = (void __user *)arg;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800529 struct watchdog_device *wdd;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000530 int __user *p = argp;
531 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000532 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000533
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800534 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 Goede7a879822012-05-22 11:40:26 +0200542 err = watchdog_ioctl_op(wdd, cmd, arg);
543 if (err != -ENOIOCTLCMD)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800544 goto out_ioctl;
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000545
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000546 switch (cmd) {
547 case WDIOC_GETSUPPORT:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800548 err = copy_to_user(argp, wdd->info,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000549 sizeof(struct watchdog_info)) ? -EFAULT : 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800550 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000551 case WDIOC_GETSTATUS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800552 val = watchdog_get_status(wdd);
553 err = put_user(val, p);
554 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000555 case WDIOC_GETBOOTSTATUS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800556 err = put_user(wdd->bootstatus, p);
557 break;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000558 case WDIOC_SETOPTIONS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800559 if (get_user(val, p)) {
560 err = -EFAULT;
561 break;
562 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000563 if (val & WDIOS_DISABLECARD) {
564 err = watchdog_stop(wdd);
565 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800566 break;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000567 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800568 if (val & WDIOS_ENABLECARD)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000569 err = watchdog_start(wdd);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800570 break;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000571 case WDIOC_KEEPALIVE:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800572 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
573 err = -EOPNOTSUPP;
574 break;
575 }
576 err = watchdog_ping(wdd);
577 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000578 case WDIOC_SETTIMEOUT:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800579 if (get_user(val, p)) {
580 err = -EFAULT;
581 break;
582 }
Hans de Goede7a879822012-05-22 11:40:26 +0200583 err = watchdog_set_timeout(wdd, val);
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000584 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800585 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000586 /* 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 Usyskin5ef79662015-10-26 14:07:58 +0200589 err = watchdog_ping(wdd);
590 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800591 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000592 /* Fall */
593 case WDIOC_GETTIMEOUT:
594 /* timeout == 0 means that we don't know the timeout */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800595 if (wdd->timeout == 0) {
596 err = -EOPNOTSUPP;
597 break;
598 }
599 err = put_user(wdd->timeout, p);
600 break;
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100601 case WDIOC_GETTIMELEFT:
Hans de Goede7a879822012-05-22 11:40:26 +0200602 err = watchdog_get_timeleft(wdd, &val);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800603 if (err < 0)
604 break;
605 err = put_user(val, p);
606 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000607 default:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800608 err = -ENOTTY;
609 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000610 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800611
612out_ioctl:
613 mutex_unlock(&wd_data->lock);
614 return err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000615}
616
617/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200618 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000619 * @inode: inode of device
620 * @file: file handle to device
621 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200622 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000623 * Watch out: the /dev/watchdog device is single open, so we make sure
624 * it can only be opened once.
625 */
626
627static int watchdog_open(struct inode *inode, struct file *file)
628{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800629 struct watchdog_core_data *wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200630 struct watchdog_device *wdd;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800631 int err;
Alan Cox45f5fed2012-05-10 21:48:59 +0200632
633 /* Get the corresponding watchdog device */
634 if (imajor(inode) == MISC_MAJOR)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800635 wd_data = old_wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200636 else
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800637 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
638 cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000639
640 /* the watchdog is single open! */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800641 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000642 return -EBUSY;
643
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800644 wdd = wd_data->wdd;
645
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000646 /*
647 * If the /dev/watchdog device is open, we don't want the module
648 * to be unloaded.
649 */
Guenter Roeckee142882016-02-28 13:12:16 -0800650 if (!watchdog_hw_running(wdd) && !try_module_get(wdd->ops->owner)) {
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800651 err = -EBUSY;
652 goto out_clear;
653 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000654
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000655 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000656 if (err < 0)
657 goto out_mod;
658
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800659 file->private_data = wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200660
Guenter Roeckee142882016-02-28 13:12:16 -0800661 if (!watchdog_hw_running(wdd))
662 kref_get(&wd_data->kref);
Hans de Goedee907df32012-05-22 11:40:26 +0200663
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000664 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
665 return nonseekable_open(inode, file);
666
667out_mod:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800668 module_put(wd_data->wdd->ops->owner);
669out_clear:
670 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000671 return err;
672}
673
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800674static 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 Sebroeck43316042011-07-22 18:55:18 +0000683/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200684 * watchdog_release: release the watchdog device.
685 * @inode: inode of device
686 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000687 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000688 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000689 * stop the watchdog when we have received the magic char (and nowayout
690 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000691 */
692
693static int watchdog_release(struct inode *inode, struct file *file)
694{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800695 struct watchdog_core_data *wd_data = file->private_data;
696 struct watchdog_device *wdd;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000697 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000698
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800699 mutex_lock(&wd_data->lock);
700
701 wdd = wd_data->wdd;
702 if (!wdd)
703 goto done;
704
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000705 /*
706 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000707 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
708 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000709 */
Hector Palaciosfcf95672013-04-08 17:06:32 +0200710 if (!test_bit(WDOG_ACTIVE, &wdd->status))
711 err = 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800712 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
Hector Palaciosfcf95672013-04-08 17:06:32 +0200713 !(wdd->info->options & WDIOF_MAGICCLOSE))
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000714 err = watchdog_stop(wdd);
715
716 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000717 if (err < 0) {
Guenter Roeck0254e952016-01-03 15:11:58 -0800718 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000719 watchdog_ping(wdd);
720 }
721
Guenter Roeck664a3922016-02-28 13:12:15 -0800722 cancel_delayed_work_sync(&wd_data->work);
Guenter Roeckee142882016-02-28 13:12:16 -0800723 watchdog_update_worker(wdd);
Guenter Roeck664a3922016-02-28 13:12:15 -0800724
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000725 /* make sure that /dev/watchdog can be re-opened */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800726 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000727
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800728done:
729 mutex_unlock(&wd_data->lock);
Guenter Roeckee142882016-02-28 13:12:16 -0800730 /*
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 Sebroeck43316042011-07-22 18:55:18 +0000739 return 0;
740}
741
742static const struct file_operations watchdog_fops = {
743 .owner = THIS_MODULE,
744 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000745 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000746 .open = watchdog_open,
747 .release = watchdog_release,
748};
749
750static struct miscdevice watchdog_miscdev = {
751 .minor = WATCHDOG_MINOR,
752 .name = "watchdog",
753 .fops = &watchdog_fops,
754};
755
756/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800757 * watchdog_cdev_register: register watchdog character device
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700758 * @wdd: watchdog device
Guenter Roeck32ecc632015-12-25 16:01:40 -0800759 * @devno: character device number
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000760 *
Guenter Roeck32ecc632015-12-25 16:01:40 -0800761 * Register a watchdog character device including handling the legacy
Alan Cox45f5fed2012-05-10 21:48:59 +0200762 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
763 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000764 */
765
Guenter Roeck32ecc632015-12-25 16:01:40 -0800766static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000767{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800768 struct watchdog_core_data *wd_data;
Guenter Roeck32ecc632015-12-25 16:01:40 -0800769 int err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000770
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800771 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 Roeck664a3922016-02-28 13:12:15 -0800780 if (!watchdog_wq)
781 return -ENODEV;
782
783 INIT_DELAYED_WORK(&wd_data->work, watchdog_ping_work);
784
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700785 if (wdd->id == 0) {
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800786 old_wd_data = wd_data;
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700787 watchdog_miscdev.parent = wdd->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200788 err = misc_register(&watchdog_miscdev);
789 if (err != 0) {
790 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700791 wdd->info->identity, WATCHDOG_MINOR, err);
Alan Cox45f5fed2012-05-10 21:48:59 +0200792 if (err == -EBUSY)
793 pr_err("%s: a legacy watchdog module is probably present.\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700794 wdd->info->identity);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800795 old_wd_data = NULL;
796 kfree(wd_data);
Alan Cox45f5fed2012-05-10 21:48:59 +0200797 return err;
798 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000799 }
800
Alan Cox45f5fed2012-05-10 21:48:59 +0200801 /* Fill in the data structures */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800802 cdev_init(&wd_data->cdev, &watchdog_fops);
803 wd_data->cdev.owner = wdd->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000804
Alan Cox45f5fed2012-05-10 21:48:59 +0200805 /* Add the device */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800806 err = cdev_add(&wd_data->cdev, devno, 1);
Alan Cox45f5fed2012-05-10 21:48:59 +0200807 if (err) {
808 pr_err("watchdog%d unable to add device %d:%d\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700809 wdd->id, MAJOR(watchdog_devt), wdd->id);
810 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200811 misc_deregister(&watchdog_miscdev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800812 old_wd_data = NULL;
813 kref_put(&wd_data->kref, watchdog_core_data_release);
Alan Cox45f5fed2012-05-10 21:48:59 +0200814 }
Guenter Roeckee142882016-02-28 13:12:16 -0800815 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000816 }
Guenter Roeckee142882016-02-28 13:12:16 -0800817
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 Sebroeck43316042011-07-22 18:55:18 +0000829}
830
831/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800832 * watchdog_cdev_unregister: unregister watchdog character device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000833 * @watchdog: watchdog device
834 *
Guenter Roeck32ecc632015-12-25 16:01:40 -0800835 * Unregister watchdog character device and if needed the legacy
836 * /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000837 */
838
Guenter Roeck32ecc632015-12-25 16:01:40 -0800839static void watchdog_cdev_unregister(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000840{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800841 struct watchdog_core_data *wd_data = wdd->wd_data;
Hans de Goedee907df32012-05-22 11:40:26 +0200842
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800843 cdev_del(&wd_data->cdev);
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700844 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200845 misc_deregister(&watchdog_miscdev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800846 old_wd_data = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000847 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800848
849 mutex_lock(&wd_data->lock);
850 wd_data->wdd = NULL;
851 wdd->wd_data = NULL;
852 mutex_unlock(&wd_data->lock);
853
Guenter Roeck664a3922016-02-28 13:12:15 -0800854 cancel_delayed_work_sync(&wd_data->work);
855
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800856 kref_put(&wd_data->kref, watchdog_core_data_release);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000857}
Alan Cox45f5fed2012-05-10 21:48:59 +0200858
Pratyush Anand906d7a52015-12-17 17:53:58 +0530859static struct class watchdog_class = {
860 .name = "watchdog",
861 .owner = THIS_MODULE,
Pratyush Anand33b71122015-12-17 17:53:59 +0530862 .dev_groups = wdt_groups,
Pratyush Anand906d7a52015-12-17 17:53:58 +0530863};
864
Alan Cox45f5fed2012-05-10 21:48:59 +0200865/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800866 * 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
874int 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 Roeckfaa58472016-01-03 15:11:56 -0800886 dev = device_create_with_groups(&watchdog_class, wdd->parent,
887 devno, wdd, wdd->groups,
888 "watchdog%d", wdd->id);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800889 if (IS_ERR(dev)) {
890 watchdog_cdev_unregister(wdd);
891 return PTR_ERR(dev);
892 }
Guenter Roeck32ecc632015-12-25 16:01:40 -0800893
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
905void watchdog_dev_unregister(struct watchdog_device *wdd)
906{
Guenter Roeck0254e952016-01-03 15:11:58 -0800907 device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800908 watchdog_cdev_unregister(wdd);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800909}
910
911/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200912 * 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 Roeck32ecc632015-12-25 16:01:40 -0800917int __init watchdog_dev_init(void)
Alan Cox45f5fed2012-05-10 21:48:59 +0200918{
Pratyush Anand906d7a52015-12-17 17:53:58 +0530919 int err;
920
Guenter Roeck664a3922016-02-28 13:12:15 -0800921 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 Anand906d7a52015-12-17 17:53:58 +0530928 err = class_register(&watchdog_class);
929 if (err < 0) {
930 pr_err("couldn't register class\n");
Guenter Roeck32ecc632015-12-25 16:01:40 -0800931 return err;
Pratyush Anand906d7a52015-12-17 17:53:58 +0530932 }
933
934 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
935 if (err < 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200936 pr_err("watchdog: unable to allocate char dev region\n");
Pratyush Anand906d7a52015-12-17 17:53:58 +0530937 class_unregister(&watchdog_class);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800938 return err;
Pratyush Anand906d7a52015-12-17 17:53:58 +0530939 }
940
Guenter Roeck32ecc632015-12-25 16:01:40 -0800941 return 0;
Alan Cox45f5fed2012-05-10 21:48:59 +0200942}
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
950void __exit watchdog_dev_exit(void)
951{
952 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
Pratyush Anand906d7a52015-12-17 17:53:58 +0530953 class_unregister(&watchdog_class);
Guenter Roeck664a3922016-02-28 13:12:15 -0800954 destroy_workqueue(watchdog_wq);
Alan Cox45f5fed2012-05-10 21:48:59 +0200955}