blob: ba2ecce4aae685df4ce30a8263fec56f3e0b729d [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 Roeckb4ffb192015-12-25 16:01:42 -080039#include <linux/kernel.h> /* For printk/panic/... */
40#include <linux/kref.h> /* For data references */
41#include <linux/miscdevice.h> /* For handling misc devices */
42#include <linux/module.h> /* For module stuff/... */
43#include <linux/mutex.h> /* For mutexes */
44#include <linux/slab.h> /* For memory functions */
45#include <linux/types.h> /* For standard types (like size_t) */
46#include <linux/watchdog.h> /* For watchdog specific items */
Wim Van Sebroeck43316042011-07-22 18:55:18 +000047#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
48
Wim Van Sebroeck6cfb5aa2012-05-21 15:31:06 +020049#include "watchdog_core.h"
H Hartley Sweeten09a46e72012-04-20 13:28:24 -070050
Guenter Roeckb4ffb192015-12-25 16:01:42 -080051/*
52 * struct watchdog_core_data - watchdog core internal data
53 * @kref: Reference count.
54 * @cdev: The watchdog's Character device.
55 * @wdd: Pointer to watchdog device.
56 * @lock: Lock for watchdog core.
57 * @status: Watchdog core internal status bits.
58 */
59struct watchdog_core_data {
60 struct kref kref;
61 struct cdev cdev;
62 struct watchdog_device *wdd;
63 struct mutex lock;
64 unsigned long status; /* Internal status bits */
65#define _WDOG_DEV_OPEN 0 /* Opened ? */
66#define _WDOG_ALLOW_RELEASE 1 /* Did we receive the magic char ? */
67};
68
Alan Cox45f5fed2012-05-10 21:48:59 +020069/* the dev_t structure to store the dynamically allocated watchdog devices */
70static dev_t watchdog_devt;
Guenter Roeckb4ffb192015-12-25 16:01:42 -080071/* Reference to watchdog device behind /dev/watchdog */
72static struct watchdog_core_data *old_wd_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000073
74/*
75 * watchdog_ping: ping the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -070076 * @wdd: the watchdog device to ping
Wim Van Sebroeck43316042011-07-22 18:55:18 +000077 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -080078 * The caller must hold wd_data->lock.
79 *
Wim Van Sebroeck43316042011-07-22 18:55:18 +000080 * If the watchdog has no own ping operation then it needs to be
81 * restarted via the start operation. This wrapper function does
82 * exactly that.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000083 * We only ping when the watchdog device is running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +000084 */
85
Guenter Roeckbc794ac2015-09-29 01:27:25 -070086static int watchdog_ping(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +000087{
Guenter Roeckb4ffb192015-12-25 16:01:42 -080088 int err;
Hans de Goedee907df32012-05-22 11:40:26 +020089
Guenter Roeckbc794ac2015-09-29 01:27:25 -070090 if (!watchdog_active(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -080091 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +020092
Guenter Roeckbc794ac2015-09-29 01:27:25 -070093 if (wdd->ops->ping)
94 err = wdd->ops->ping(wdd); /* ping the watchdog */
Hans de Goede7a879822012-05-22 11:40:26 +020095 else
Guenter Roeckbc794ac2015-09-29 01:27:25 -070096 err = wdd->ops->start(wdd); /* restart watchdog */
Hans de Goede7a879822012-05-22 11:40:26 +020097
Hans de Goede7a879822012-05-22 11:40:26 +020098 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000099}
100
101/*
102 * watchdog_start: wrapper to start the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700103 * @wdd: the watchdog device to start
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000104 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800105 * The caller must hold wd_data->lock.
106 *
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000107 * Start the watchdog if it is not active and mark it active.
108 * This function returns zero on success or a negative errno code for
109 * failure.
110 */
111
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700112static int watchdog_start(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000113{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800114 int err;
Hans de Goedee907df32012-05-22 11:40:26 +0200115
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700116 if (watchdog_active(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800117 return 0;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000118
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700119 err = wdd->ops->start(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200120 if (err == 0)
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700121 set_bit(WDOG_ACTIVE, &wdd->status);
Hans de Goede7a879822012-05-22 11:40:26 +0200122
Hans de Goede7a879822012-05-22 11:40:26 +0200123 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000124}
125
126/*
127 * watchdog_stop: wrapper to stop the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700128 * @wdd: the watchdog device to stop
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000129 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800130 * The caller must hold wd_data->lock.
131 *
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000132 * Stop the watchdog if it is still active and unmark it active.
133 * This function returns zero on success or a negative errno code for
134 * failure.
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000135 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000136 */
137
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700138static int watchdog_stop(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000139{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800140 int err;
Hans de Goedee907df32012-05-22 11:40:26 +0200141
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700142 if (!watchdog_active(wdd))
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800143 return 0;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000144
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700145 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
Guenter Roeck0254e952016-01-03 15:11:58 -0800146 pr_info("watchdog%d: nowayout prevents watchdog being stopped!\n",
147 wdd->id);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800148 return -EBUSY;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000149 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000150
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700151 err = wdd->ops->stop(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200152 if (err == 0)
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700153 clear_bit(WDOG_ACTIVE, &wdd->status);
Hans de Goede7a879822012-05-22 11:40:26 +0200154
Hans de Goede7a879822012-05-22 11:40:26 +0200155 return err;
156}
157
158/*
159 * watchdog_get_status: wrapper to get the watchdog status
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700160 * @wdd: the watchdog device to get the status from
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800161 *
162 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200163 *
164 * Get the watchdog's status flags.
165 */
166
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800167static unsigned int watchdog_get_status(struct watchdog_device *wdd)
Hans de Goede7a879822012-05-22 11:40:26 +0200168{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700169 if (!wdd->ops->status)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800170 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +0200171
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800172 return wdd->ops->status(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200173}
174
175/*
176 * watchdog_set_timeout: set the watchdog timer timeout
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700177 * @wdd: the watchdog device to set the timeout for
Hans de Goede7a879822012-05-22 11:40:26 +0200178 * @timeout: timeout to set in seconds
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800179 *
180 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200181 */
182
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700183static int watchdog_set_timeout(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200184 unsigned int timeout)
185{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700186 if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT))
Hans de Goede7a879822012-05-22 11:40:26 +0200187 return -EOPNOTSUPP;
188
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700189 if (watchdog_timeout_invalid(wdd, timeout))
Hans de Goede7a879822012-05-22 11:40:26 +0200190 return -EINVAL;
191
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800192 return wdd->ops->set_timeout(wdd, timeout);
Hans de Goede7a879822012-05-22 11:40:26 +0200193}
194
195/*
196 * watchdog_get_timeleft: wrapper to get the time left before a reboot
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700197 * @wdd: the watchdog device to get the remaining time from
Hans de Goede7a879822012-05-22 11:40:26 +0200198 * @timeleft: the time that's left
199 *
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800200 * The caller must hold wd_data->lock.
201 *
Hans de Goede7a879822012-05-22 11:40:26 +0200202 * Get the time before a watchdog will reboot (if not pinged).
203 */
204
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700205static int watchdog_get_timeleft(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200206 unsigned int *timeleft)
207{
Hans de Goede7a879822012-05-22 11:40:26 +0200208 *timeleft = 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800209
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700210 if (!wdd->ops->get_timeleft)
Hans de Goede7a879822012-05-22 11:40:26 +0200211 return -EOPNOTSUPP;
212
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700213 *timeleft = wdd->ops->get_timeleft(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200214
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800215 return 0;
Hans de Goede7a879822012-05-22 11:40:26 +0200216}
217
Pratyush Anand33b71122015-12-17 17:53:59 +0530218#ifdef CONFIG_WATCHDOG_SYSFS
219static ssize_t nowayout_show(struct device *dev, struct device_attribute *attr,
220 char *buf)
221{
222 struct watchdog_device *wdd = dev_get_drvdata(dev);
223
224 return sprintf(buf, "%d\n", !!test_bit(WDOG_NO_WAY_OUT, &wdd->status));
225}
226static DEVICE_ATTR_RO(nowayout);
227
228static ssize_t status_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
230{
231 struct watchdog_device *wdd = dev_get_drvdata(dev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800232 struct watchdog_core_data *wd_data = wdd->wd_data;
233 unsigned int status;
Pratyush Anand33b71122015-12-17 17:53:59 +0530234
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800235 mutex_lock(&wd_data->lock);
236 status = watchdog_get_status(wdd);
237 mutex_unlock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530238
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800239 return sprintf(buf, "%u\n", status);
Pratyush Anand33b71122015-12-17 17:53:59 +0530240}
241static DEVICE_ATTR_RO(status);
242
243static ssize_t bootstatus_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
245{
246 struct watchdog_device *wdd = dev_get_drvdata(dev);
247
248 return sprintf(buf, "%u\n", wdd->bootstatus);
249}
250static DEVICE_ATTR_RO(bootstatus);
251
252static ssize_t timeleft_show(struct device *dev, struct device_attribute *attr,
253 char *buf)
254{
255 struct watchdog_device *wdd = dev_get_drvdata(dev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800256 struct watchdog_core_data *wd_data = wdd->wd_data;
Pratyush Anand33b71122015-12-17 17:53:59 +0530257 ssize_t status;
258 unsigned int val;
259
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800260 mutex_lock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530261 status = watchdog_get_timeleft(wdd, &val);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800262 mutex_unlock(&wd_data->lock);
Pratyush Anand33b71122015-12-17 17:53:59 +0530263 if (!status)
264 status = sprintf(buf, "%u\n", val);
265
266 return status;
267}
268static DEVICE_ATTR_RO(timeleft);
269
270static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
271 char *buf)
272{
273 struct watchdog_device *wdd = dev_get_drvdata(dev);
274
275 return sprintf(buf, "%u\n", wdd->timeout);
276}
277static DEVICE_ATTR_RO(timeout);
278
279static ssize_t identity_show(struct device *dev, struct device_attribute *attr,
280 char *buf)
281{
282 struct watchdog_device *wdd = dev_get_drvdata(dev);
283
284 return sprintf(buf, "%s\n", wdd->info->identity);
285}
286static DEVICE_ATTR_RO(identity);
287
288static ssize_t state_show(struct device *dev, struct device_attribute *attr,
289 char *buf)
290{
291 struct watchdog_device *wdd = dev_get_drvdata(dev);
292
293 if (watchdog_active(wdd))
294 return sprintf(buf, "active\n");
295
296 return sprintf(buf, "inactive\n");
297}
298static DEVICE_ATTR_RO(state);
299
300static umode_t wdt_is_visible(struct kobject *kobj, struct attribute *attr,
301 int n)
302{
303 struct device *dev = container_of(kobj, struct device, kobj);
304 struct watchdog_device *wdd = dev_get_drvdata(dev);
305 umode_t mode = attr->mode;
306
307 if (attr == &dev_attr_status.attr && !wdd->ops->status)
308 mode = 0;
309 else if (attr == &dev_attr_timeleft.attr && !wdd->ops->get_timeleft)
310 mode = 0;
311
312 return mode;
313}
314static struct attribute *wdt_attrs[] = {
315 &dev_attr_state.attr,
316 &dev_attr_identity.attr,
317 &dev_attr_timeout.attr,
318 &dev_attr_timeleft.attr,
319 &dev_attr_bootstatus.attr,
320 &dev_attr_status.attr,
321 &dev_attr_nowayout.attr,
322 NULL,
323};
324
325static const struct attribute_group wdt_group = {
326 .attrs = wdt_attrs,
327 .is_visible = wdt_is_visible,
328};
329__ATTRIBUTE_GROUPS(wdt);
330#else
331#define wdt_groups NULL
332#endif
333
Hans de Goede7a879822012-05-22 11:40:26 +0200334/*
335 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700336 * @wdd: the watchdog device to do the ioctl on
Hans de Goede7a879822012-05-22 11:40:26 +0200337 * @cmd: watchdog command
338 * @arg: argument pointer
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800339 *
340 * The caller must hold wd_data->lock.
Hans de Goede7a879822012-05-22 11:40:26 +0200341 */
342
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700343static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
Hans de Goede7a879822012-05-22 11:40:26 +0200344 unsigned long arg)
345{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700346 if (!wdd->ops->ioctl)
Hans de Goede7a879822012-05-22 11:40:26 +0200347 return -ENOIOCTLCMD;
348
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800349 return wdd->ops->ioctl(wdd, cmd, arg);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000350}
351
352/*
353 * watchdog_write: writes to the watchdog.
354 * @file: file from VFS
355 * @data: user address of data
356 * @len: length of data
357 * @ppos: pointer to the file offset
358 *
359 * A write to a watchdog device is defined as a keepalive ping.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000360 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000361 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000362 */
363
364static ssize_t watchdog_write(struct file *file, const char __user *data,
365 size_t len, loff_t *ppos)
366{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800367 struct watchdog_core_data *wd_data = file->private_data;
368 struct watchdog_device *wdd;
369 int err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000370 size_t i;
371 char c;
372
373 if (len == 0)
374 return 0;
375
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000376 /*
377 * Note: just in case someone wrote the magic character
378 * five months ago...
379 */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800380 clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000381
382 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000383 for (i = 0; i != len; i++) {
384 if (get_user(c, data + i))
385 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000386 if (c == 'V')
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800387 set_bit(_WDOG_ALLOW_RELEASE, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000388 }
389
390 /* someone wrote to us, so we send the watchdog a keepalive ping */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800391
392 err = -ENODEV;
393 mutex_lock(&wd_data->lock);
394 wdd = wd_data->wdd;
395 if (wdd)
396 err = watchdog_ping(wdd);
397 mutex_unlock(&wd_data->lock);
398
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200399 if (err < 0)
400 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000401
402 return len;
403}
404
405/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000406 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
407 * @file: file handle to the device
408 * @cmd: watchdog command
409 * @arg: argument pointer
410 *
411 * The watchdog API defines a common set of functions for all watchdogs
412 * according to their available features.
413 */
414
415static long watchdog_ioctl(struct file *file, unsigned int cmd,
416 unsigned long arg)
417{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800418 struct watchdog_core_data *wd_data = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000419 void __user *argp = (void __user *)arg;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800420 struct watchdog_device *wdd;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000421 int __user *p = argp;
422 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000423 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000424
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800425 mutex_lock(&wd_data->lock);
426
427 wdd = wd_data->wdd;
428 if (!wdd) {
429 err = -ENODEV;
430 goto out_ioctl;
431 }
432
Hans de Goede7a879822012-05-22 11:40:26 +0200433 err = watchdog_ioctl_op(wdd, cmd, arg);
434 if (err != -ENOIOCTLCMD)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800435 goto out_ioctl;
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000436
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000437 switch (cmd) {
438 case WDIOC_GETSUPPORT:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800439 err = copy_to_user(argp, wdd->info,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000440 sizeof(struct watchdog_info)) ? -EFAULT : 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800441 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000442 case WDIOC_GETSTATUS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800443 val = watchdog_get_status(wdd);
444 err = put_user(val, p);
445 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000446 case WDIOC_GETBOOTSTATUS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800447 err = put_user(wdd->bootstatus, p);
448 break;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000449 case WDIOC_SETOPTIONS:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800450 if (get_user(val, p)) {
451 err = -EFAULT;
452 break;
453 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000454 if (val & WDIOS_DISABLECARD) {
455 err = watchdog_stop(wdd);
456 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800457 break;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000458 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800459 if (val & WDIOS_ENABLECARD)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000460 err = watchdog_start(wdd);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800461 break;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000462 case WDIOC_KEEPALIVE:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800463 if (!(wdd->info->options & WDIOF_KEEPALIVEPING)) {
464 err = -EOPNOTSUPP;
465 break;
466 }
467 err = watchdog_ping(wdd);
468 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000469 case WDIOC_SETTIMEOUT:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800470 if (get_user(val, p)) {
471 err = -EFAULT;
472 break;
473 }
Hans de Goede7a879822012-05-22 11:40:26 +0200474 err = watchdog_set_timeout(wdd, val);
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000475 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800476 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000477 /* If the watchdog is active then we send a keepalive ping
478 * to make sure that the watchdog keep's running (and if
479 * possible that it takes the new timeout) */
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200480 err = watchdog_ping(wdd);
481 if (err < 0)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800482 break;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000483 /* Fall */
484 case WDIOC_GETTIMEOUT:
485 /* timeout == 0 means that we don't know the timeout */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800486 if (wdd->timeout == 0) {
487 err = -EOPNOTSUPP;
488 break;
489 }
490 err = put_user(wdd->timeout, p);
491 break;
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100492 case WDIOC_GETTIMELEFT:
Hans de Goede7a879822012-05-22 11:40:26 +0200493 err = watchdog_get_timeleft(wdd, &val);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800494 if (err < 0)
495 break;
496 err = put_user(val, p);
497 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000498 default:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800499 err = -ENOTTY;
500 break;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000501 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800502
503out_ioctl:
504 mutex_unlock(&wd_data->lock);
505 return err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000506}
507
508/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200509 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000510 * @inode: inode of device
511 * @file: file handle to device
512 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200513 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000514 * Watch out: the /dev/watchdog device is single open, so we make sure
515 * it can only be opened once.
516 */
517
518static int watchdog_open(struct inode *inode, struct file *file)
519{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800520 struct watchdog_core_data *wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200521 struct watchdog_device *wdd;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800522 int err;
Alan Cox45f5fed2012-05-10 21:48:59 +0200523
524 /* Get the corresponding watchdog device */
525 if (imajor(inode) == MISC_MAJOR)
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800526 wd_data = old_wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200527 else
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800528 wd_data = container_of(inode->i_cdev, struct watchdog_core_data,
529 cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000530
531 /* the watchdog is single open! */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800532 if (test_and_set_bit(_WDOG_DEV_OPEN, &wd_data->status))
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000533 return -EBUSY;
534
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800535 wdd = wd_data->wdd;
536
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000537 /*
538 * If the /dev/watchdog device is open, we don't want the module
539 * to be unloaded.
540 */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800541 if (!try_module_get(wdd->ops->owner)) {
542 err = -EBUSY;
543 goto out_clear;
544 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000545
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000546 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000547 if (err < 0)
548 goto out_mod;
549
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800550 file->private_data = wd_data;
Alan Cox45f5fed2012-05-10 21:48:59 +0200551
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800552 kref_get(&wd_data->kref);
Hans de Goedee907df32012-05-22 11:40:26 +0200553
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000554 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
555 return nonseekable_open(inode, file);
556
557out_mod:
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800558 module_put(wd_data->wdd->ops->owner);
559out_clear:
560 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000561 return err;
562}
563
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800564static void watchdog_core_data_release(struct kref *kref)
565{
566 struct watchdog_core_data *wd_data;
567
568 wd_data = container_of(kref, struct watchdog_core_data, kref);
569
570 kfree(wd_data);
571}
572
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000573/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200574 * watchdog_release: release the watchdog device.
575 * @inode: inode of device
576 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000577 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000578 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000579 * stop the watchdog when we have received the magic char (and nowayout
580 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000581 */
582
583static int watchdog_release(struct inode *inode, struct file *file)
584{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800585 struct watchdog_core_data *wd_data = file->private_data;
586 struct watchdog_device *wdd;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000587 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000588
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800589 mutex_lock(&wd_data->lock);
590
591 wdd = wd_data->wdd;
592 if (!wdd)
593 goto done;
594
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000595 /*
596 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000597 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
598 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000599 */
Hector Palaciosfcf95672013-04-08 17:06:32 +0200600 if (!test_bit(WDOG_ACTIVE, &wdd->status))
601 err = 0;
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800602 else if (test_and_clear_bit(_WDOG_ALLOW_RELEASE, &wd_data->status) ||
Hector Palaciosfcf95672013-04-08 17:06:32 +0200603 !(wdd->info->options & WDIOF_MAGICCLOSE))
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000604 err = watchdog_stop(wdd);
605
606 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000607 if (err < 0) {
Guenter Roeck0254e952016-01-03 15:11:58 -0800608 pr_crit("watchdog%d: watchdog did not stop!\n", wdd->id);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000609 watchdog_ping(wdd);
610 }
611
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000612 /* make sure that /dev/watchdog can be re-opened */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800613 clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000614
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800615done:
616 mutex_unlock(&wd_data->lock);
617 /* Allow the owner module to be unloaded again */
618 module_put(wd_data->cdev.owner);
619 kref_put(&wd_data->kref, watchdog_core_data_release);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000620 return 0;
621}
622
623static const struct file_operations watchdog_fops = {
624 .owner = THIS_MODULE,
625 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000626 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000627 .open = watchdog_open,
628 .release = watchdog_release,
629};
630
631static struct miscdevice watchdog_miscdev = {
632 .minor = WATCHDOG_MINOR,
633 .name = "watchdog",
634 .fops = &watchdog_fops,
635};
636
637/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800638 * watchdog_cdev_register: register watchdog character device
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700639 * @wdd: watchdog device
Guenter Roeck32ecc632015-12-25 16:01:40 -0800640 * @devno: character device number
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000641 *
Guenter Roeck32ecc632015-12-25 16:01:40 -0800642 * Register a watchdog character device including handling the legacy
Alan Cox45f5fed2012-05-10 21:48:59 +0200643 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
644 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000645 */
646
Guenter Roeck32ecc632015-12-25 16:01:40 -0800647static int watchdog_cdev_register(struct watchdog_device *wdd, dev_t devno)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000648{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800649 struct watchdog_core_data *wd_data;
Guenter Roeck32ecc632015-12-25 16:01:40 -0800650 int err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000651
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800652 wd_data = kzalloc(sizeof(struct watchdog_core_data), GFP_KERNEL);
653 if (!wd_data)
654 return -ENOMEM;
655 kref_init(&wd_data->kref);
656 mutex_init(&wd_data->lock);
657
658 wd_data->wdd = wdd;
659 wdd->wd_data = wd_data;
660
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700661 if (wdd->id == 0) {
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800662 old_wd_data = wd_data;
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700663 watchdog_miscdev.parent = wdd->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200664 err = misc_register(&watchdog_miscdev);
665 if (err != 0) {
666 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700667 wdd->info->identity, WATCHDOG_MINOR, err);
Alan Cox45f5fed2012-05-10 21:48:59 +0200668 if (err == -EBUSY)
669 pr_err("%s: a legacy watchdog module is probably present.\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700670 wdd->info->identity);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800671 old_wd_data = NULL;
672 kfree(wd_data);
Alan Cox45f5fed2012-05-10 21:48:59 +0200673 return err;
674 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000675 }
676
Alan Cox45f5fed2012-05-10 21:48:59 +0200677 /* Fill in the data structures */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800678 cdev_init(&wd_data->cdev, &watchdog_fops);
679 wd_data->cdev.owner = wdd->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000680
Alan Cox45f5fed2012-05-10 21:48:59 +0200681 /* Add the device */
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800682 err = cdev_add(&wd_data->cdev, devno, 1);
Alan Cox45f5fed2012-05-10 21:48:59 +0200683 if (err) {
684 pr_err("watchdog%d unable to add device %d:%d\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700685 wdd->id, MAJOR(watchdog_devt), wdd->id);
686 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200687 misc_deregister(&watchdog_miscdev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800688 old_wd_data = NULL;
689 kref_put(&wd_data->kref, watchdog_core_data_release);
Alan Cox45f5fed2012-05-10 21:48:59 +0200690 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000691 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000692 return err;
693}
694
695/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800696 * watchdog_cdev_unregister: unregister watchdog character device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000697 * @watchdog: watchdog device
698 *
Guenter Roeck32ecc632015-12-25 16:01:40 -0800699 * Unregister watchdog character device and if needed the legacy
700 * /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000701 */
702
Guenter Roeck32ecc632015-12-25 16:01:40 -0800703static void watchdog_cdev_unregister(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000704{
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800705 struct watchdog_core_data *wd_data = wdd->wd_data;
Hans de Goedee907df32012-05-22 11:40:26 +0200706
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800707 cdev_del(&wd_data->cdev);
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700708 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200709 misc_deregister(&watchdog_miscdev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800710 old_wd_data = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000711 }
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800712
713 mutex_lock(&wd_data->lock);
714 wd_data->wdd = NULL;
715 wdd->wd_data = NULL;
716 mutex_unlock(&wd_data->lock);
717
718 kref_put(&wd_data->kref, watchdog_core_data_release);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000719}
Alan Cox45f5fed2012-05-10 21:48:59 +0200720
Pratyush Anand906d7a52015-12-17 17:53:58 +0530721static struct class watchdog_class = {
722 .name = "watchdog",
723 .owner = THIS_MODULE,
Pratyush Anand33b71122015-12-17 17:53:59 +0530724 .dev_groups = wdt_groups,
Pratyush Anand906d7a52015-12-17 17:53:58 +0530725};
726
Alan Cox45f5fed2012-05-10 21:48:59 +0200727/*
Guenter Roeck32ecc632015-12-25 16:01:40 -0800728 * watchdog_dev_register: register a watchdog device
729 * @wdd: watchdog device
730 *
731 * Register a watchdog device including handling the legacy
732 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
733 * thus we set it up like that.
734 */
735
736int watchdog_dev_register(struct watchdog_device *wdd)
737{
738 struct device *dev;
739 dev_t devno;
740 int ret;
741
742 devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
743
744 ret = watchdog_cdev_register(wdd, devno);
745 if (ret)
746 return ret;
747
Guenter Roeckfaa58472016-01-03 15:11:56 -0800748 dev = device_create_with_groups(&watchdog_class, wdd->parent,
749 devno, wdd, wdd->groups,
750 "watchdog%d", wdd->id);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800751 if (IS_ERR(dev)) {
752 watchdog_cdev_unregister(wdd);
753 return PTR_ERR(dev);
754 }
Guenter Roeck32ecc632015-12-25 16:01:40 -0800755
756 return ret;
757}
758
759/*
760 * watchdog_dev_unregister: unregister a watchdog device
761 * @watchdog: watchdog device
762 *
763 * Unregister watchdog device and if needed the legacy
764 * /dev/watchdog device.
765 */
766
767void watchdog_dev_unregister(struct watchdog_device *wdd)
768{
Guenter Roeck0254e952016-01-03 15:11:58 -0800769 device_destroy(&watchdog_class, wdd->wd_data->cdev.dev);
Guenter Roeckb4ffb192015-12-25 16:01:42 -0800770 watchdog_cdev_unregister(wdd);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800771}
772
773/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200774 * watchdog_dev_init: init dev part of watchdog core
775 *
776 * Allocate a range of chardev nodes to use for watchdog devices
777 */
778
Guenter Roeck32ecc632015-12-25 16:01:40 -0800779int __init watchdog_dev_init(void)
Alan Cox45f5fed2012-05-10 21:48:59 +0200780{
Pratyush Anand906d7a52015-12-17 17:53:58 +0530781 int err;
782
783 err = class_register(&watchdog_class);
784 if (err < 0) {
785 pr_err("couldn't register class\n");
Guenter Roeck32ecc632015-12-25 16:01:40 -0800786 return err;
Pratyush Anand906d7a52015-12-17 17:53:58 +0530787 }
788
789 err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
790 if (err < 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200791 pr_err("watchdog: unable to allocate char dev region\n");
Pratyush Anand906d7a52015-12-17 17:53:58 +0530792 class_unregister(&watchdog_class);
Guenter Roeck32ecc632015-12-25 16:01:40 -0800793 return err;
Pratyush Anand906d7a52015-12-17 17:53:58 +0530794 }
795
Guenter Roeck32ecc632015-12-25 16:01:40 -0800796 return 0;
Alan Cox45f5fed2012-05-10 21:48:59 +0200797}
798
799/*
800 * watchdog_dev_exit: exit dev part of watchdog core
801 *
802 * Release the range of chardev nodes used for watchdog devices
803 */
804
805void __exit watchdog_dev_exit(void)
806{
807 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
Pratyush Anand906d7a52015-12-17 17:53:58 +0530808 class_unregister(&watchdog_class);
Alan Cox45f5fed2012-05-10 21:48:59 +0200809}