blob: 56a649e66eb2461e70a2e073fe85284bf46933aa [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
35#include <linux/module.h> /* For module stuff/... */
36#include <linux/types.h> /* For standard types (like size_t) */
37#include <linux/errno.h> /* For the -ENODEV/... values */
38#include <linux/kernel.h> /* For printk/panic/... */
39#include <linux/fs.h> /* For file operations */
40#include <linux/watchdog.h> /* For watchdog specific items */
41#include <linux/miscdevice.h> /* For handling misc devices */
42#include <linux/init.h> /* For __init/__exit/... */
43#include <linux/uaccess.h> /* For copy_to_user/put_user/... */
44
Wim Van Sebroeck6cfb5aa2012-05-21 15:31:06 +020045#include "watchdog_core.h"
H Hartley Sweeten09a46e72012-04-20 13:28:24 -070046
Alan Cox45f5fed2012-05-10 21:48:59 +020047/* the dev_t structure to store the dynamically allocated watchdog devices */
48static dev_t watchdog_devt;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000049/* the watchdog device behind /dev/watchdog */
Alan Cox45f5fed2012-05-10 21:48:59 +020050static struct watchdog_device *old_wdd;
Wim Van Sebroeck43316042011-07-22 18:55:18 +000051
52/*
53 * watchdog_ping: ping the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -070054 * @wdd: the watchdog device to ping
Wim Van Sebroeck43316042011-07-22 18:55:18 +000055 *
56 * If the watchdog has no own ping operation then it needs to be
57 * restarted via the start operation. This wrapper function does
58 * exactly that.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000059 * We only ping when the watchdog device is running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +000060 */
61
Guenter Roeckbc794ac2015-09-29 01:27:25 -070062static int watchdog_ping(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +000063{
Hans de Goede7a879822012-05-22 11:40:26 +020064 int err = 0;
65
Guenter Roeckbc794ac2015-09-29 01:27:25 -070066 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +020067
Guenter Roeckbc794ac2015-09-29 01:27:25 -070068 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +020069 err = -ENODEV;
70 goto out_ping;
71 }
72
Guenter Roeckbc794ac2015-09-29 01:27:25 -070073 if (!watchdog_active(wdd))
Hans de Goede7a879822012-05-22 11:40:26 +020074 goto out_ping;
75
Guenter Roeckbc794ac2015-09-29 01:27:25 -070076 if (wdd->ops->ping)
77 err = wdd->ops->ping(wdd); /* ping the watchdog */
Hans de Goede7a879822012-05-22 11:40:26 +020078 else
Guenter Roeckbc794ac2015-09-29 01:27:25 -070079 err = wdd->ops->start(wdd); /* restart watchdog */
Hans de Goede7a879822012-05-22 11:40:26 +020080
81out_ping:
Guenter Roeckbc794ac2015-09-29 01:27:25 -070082 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +020083 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000084}
85
86/*
87 * watchdog_start: wrapper to start the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -070088 * @wdd: the watchdog device to start
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000089 *
90 * Start the watchdog if it is not active and mark it active.
91 * This function returns zero on success or a negative errno code for
92 * failure.
93 */
94
Guenter Roeckbc794ac2015-09-29 01:27:25 -070095static int watchdog_start(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000096{
Hans de Goede7a879822012-05-22 11:40:26 +020097 int err = 0;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +000098
Guenter Roeckbc794ac2015-09-29 01:27:25 -070099 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200100
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700101 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200102 err = -ENODEV;
103 goto out_start;
104 }
105
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700106 if (watchdog_active(wdd))
Hans de Goede7a879822012-05-22 11:40:26 +0200107 goto out_start;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000108
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700109 err = wdd->ops->start(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200110 if (err == 0)
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700111 set_bit(WDOG_ACTIVE, &wdd->status);
Hans de Goede7a879822012-05-22 11:40:26 +0200112
113out_start:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700114 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200115 return err;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000116}
117
118/*
119 * watchdog_stop: wrapper to stop the watchdog.
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700120 * @wdd: the watchdog device to stop
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000121 *
122 * Stop the watchdog if it is still active and unmark it active.
123 * This function returns zero on success or a negative errno code for
124 * failure.
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000125 * If the 'nowayout' feature was set, the watchdog cannot be stopped.
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000126 */
127
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700128static int watchdog_stop(struct watchdog_device *wdd)
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000129{
Hans de Goede7a879822012-05-22 11:40:26 +0200130 int err = 0;
131
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700132 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200133
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700134 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200135 err = -ENODEV;
136 goto out_stop;
137 }
138
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700139 if (!watchdog_active(wdd))
Hans de Goede7a879822012-05-22 11:40:26 +0200140 goto out_stop;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000141
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700142 if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
143 dev_info(wdd->dev, "nowayout prevents watchdog being stopped!\n");
Hans de Goede7a879822012-05-22 11:40:26 +0200144 err = -EBUSY;
145 goto out_stop;
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000146 }
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000147
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700148 err = wdd->ops->stop(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200149 if (err == 0)
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700150 clear_bit(WDOG_ACTIVE, &wdd->status);
Hans de Goede7a879822012-05-22 11:40:26 +0200151
152out_stop:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700153 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200154 return err;
155}
156
157/*
158 * watchdog_get_status: wrapper to get the watchdog status
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700159 * @wdd: the watchdog device to get the status from
Hans de Goede7a879822012-05-22 11:40:26 +0200160 * @status: the status of the watchdog device
161 *
162 * Get the watchdog's status flags.
163 */
164
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700165static int watchdog_get_status(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200166 unsigned int *status)
167{
168 int err = 0;
169
170 *status = 0;
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700171 if (!wdd->ops->status)
Hans de Goede7a879822012-05-22 11:40:26 +0200172 return -EOPNOTSUPP;
173
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700174 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200175
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700176 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200177 err = -ENODEV;
178 goto out_status;
179 }
180
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700181 *status = wdd->ops->status(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200182
Hans de Goedee907df32012-05-22 11:40:26 +0200183out_status:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700184 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200185 return err;
186}
187
188/*
189 * watchdog_set_timeout: set the watchdog timer timeout
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700190 * @wdd: the watchdog device to set the timeout for
Hans de Goede7a879822012-05-22 11:40:26 +0200191 * @timeout: timeout to set in seconds
192 */
193
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700194static int watchdog_set_timeout(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200195 unsigned int timeout)
196{
197 int err;
198
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700199 if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT))
Hans de Goede7a879822012-05-22 11:40:26 +0200200 return -EOPNOTSUPP;
201
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700202 if (watchdog_timeout_invalid(wdd, timeout))
Hans de Goede7a879822012-05-22 11:40:26 +0200203 return -EINVAL;
204
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700205 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200206
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700207 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200208 err = -ENODEV;
209 goto out_timeout;
210 }
211
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700212 err = wdd->ops->set_timeout(wdd, timeout);
Hans de Goede7a879822012-05-22 11:40:26 +0200213
Hans de Goedee907df32012-05-22 11:40:26 +0200214out_timeout:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700215 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200216 return err;
217}
218
219/*
220 * watchdog_get_timeleft: wrapper to get the time left before a reboot
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700221 * @wdd: the watchdog device to get the remaining time from
Hans de Goede7a879822012-05-22 11:40:26 +0200222 * @timeleft: the time that's left
223 *
224 * Get the time before a watchdog will reboot (if not pinged).
225 */
226
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700227static int watchdog_get_timeleft(struct watchdog_device *wdd,
Hans de Goede7a879822012-05-22 11:40:26 +0200228 unsigned int *timeleft)
229{
230 int err = 0;
231
232 *timeleft = 0;
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700233 if (!wdd->ops->get_timeleft)
Hans de Goede7a879822012-05-22 11:40:26 +0200234 return -EOPNOTSUPP;
235
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700236 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200237
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700238 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200239 err = -ENODEV;
240 goto out_timeleft;
241 }
242
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700243 *timeleft = wdd->ops->get_timeleft(wdd);
Hans de Goede7a879822012-05-22 11:40:26 +0200244
Hans de Goedee907df32012-05-22 11:40:26 +0200245out_timeleft:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700246 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200247 return err;
248}
249
250/*
251 * watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700252 * @wdd: the watchdog device to do the ioctl on
Hans de Goede7a879822012-05-22 11:40:26 +0200253 * @cmd: watchdog command
254 * @arg: argument pointer
255 */
256
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700257static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
Hans de Goede7a879822012-05-22 11:40:26 +0200258 unsigned long arg)
259{
260 int err;
261
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700262 if (!wdd->ops->ioctl)
Hans de Goede7a879822012-05-22 11:40:26 +0200263 return -ENOIOCTLCMD;
264
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700265 mutex_lock(&wdd->lock);
Hans de Goedef4e9c822012-05-22 11:40:26 +0200266
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700267 if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
Hans de Goedee907df32012-05-22 11:40:26 +0200268 err = -ENODEV;
269 goto out_ioctl;
270 }
271
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700272 err = wdd->ops->ioctl(wdd, cmd, arg);
Hans de Goede7a879822012-05-22 11:40:26 +0200273
Hans de Goedee907df32012-05-22 11:40:26 +0200274out_ioctl:
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700275 mutex_unlock(&wdd->lock);
Hans de Goede7a879822012-05-22 11:40:26 +0200276 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000277}
278
279/*
280 * watchdog_write: writes to the watchdog.
281 * @file: file from VFS
282 * @data: user address of data
283 * @len: length of data
284 * @ppos: pointer to the file offset
285 *
286 * A write to a watchdog device is defined as a keepalive ping.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000287 * Writing the magic 'V' sequence allows the next close to turn
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000288 * off the watchdog (if 'nowayout' is not set).
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000289 */
290
291static ssize_t watchdog_write(struct file *file, const char __user *data,
292 size_t len, loff_t *ppos)
293{
Alan Cox45f5fed2012-05-10 21:48:59 +0200294 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000295 size_t i;
296 char c;
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200297 int err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000298
299 if (len == 0)
300 return 0;
301
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000302 /*
303 * Note: just in case someone wrote the magic character
304 * five months ago...
305 */
306 clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);
307
308 /* scan to see whether or not we got the magic character */
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000309 for (i = 0; i != len; i++) {
310 if (get_user(c, data + i))
311 return -EFAULT;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000312 if (c == 'V')
313 set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000314 }
315
316 /* someone wrote to us, so we send the watchdog a keepalive ping */
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200317 err = watchdog_ping(wdd);
318 if (err < 0)
319 return err;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000320
321 return len;
322}
323
324/*
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000325 * watchdog_ioctl: handle the different ioctl's for the watchdog device.
326 * @file: file handle to the device
327 * @cmd: watchdog command
328 * @arg: argument pointer
329 *
330 * The watchdog API defines a common set of functions for all watchdogs
331 * according to their available features.
332 */
333
334static long watchdog_ioctl(struct file *file, unsigned int cmd,
335 unsigned long arg)
336{
Alan Cox45f5fed2012-05-10 21:48:59 +0200337 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000338 void __user *argp = (void __user *)arg;
339 int __user *p = argp;
340 unsigned int val;
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000341 int err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000342
Hans de Goede7a879822012-05-22 11:40:26 +0200343 err = watchdog_ioctl_op(wdd, cmd, arg);
344 if (err != -ENOIOCTLCMD)
345 return err;
Wim Van Sebroeck78d88fc2011-07-22 18:59:49 +0000346
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000347 switch (cmd) {
348 case WDIOC_GETSUPPORT:
349 return copy_to_user(argp, wdd->info,
350 sizeof(struct watchdog_info)) ? -EFAULT : 0;
351 case WDIOC_GETSTATUS:
Hans de Goede7a879822012-05-22 11:40:26 +0200352 err = watchdog_get_status(wdd, &val);
Wim Van Sebroeck8b9468d2012-06-26 20:07:21 +0200353 if (err == -ENODEV)
Hans de Goede7a879822012-05-22 11:40:26 +0200354 return err;
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000355 return put_user(val, p);
356 case WDIOC_GETBOOTSTATUS:
357 return put_user(wdd->bootstatus, p);
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000358 case WDIOC_SETOPTIONS:
359 if (get_user(val, p))
360 return -EFAULT;
361 if (val & WDIOS_DISABLECARD) {
362 err = watchdog_stop(wdd);
363 if (err < 0)
364 return err;
365 }
366 if (val & WDIOS_ENABLECARD) {
367 err = watchdog_start(wdd);
368 if (err < 0)
369 return err;
370 }
371 return 0;
Wim Van Sebroeckc2dc00e2011-07-22 18:57:23 +0000372 case WDIOC_KEEPALIVE:
373 if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
374 return -EOPNOTSUPP;
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200375 return watchdog_ping(wdd);
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000376 case WDIOC_SETTIMEOUT:
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000377 if (get_user(val, p))
378 return -EFAULT;
Hans de Goede7a879822012-05-22 11:40:26 +0200379 err = watchdog_set_timeout(wdd, val);
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000380 if (err < 0)
381 return err;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000382 /* If the watchdog is active then we send a keepalive ping
383 * to make sure that the watchdog keep's running (and if
384 * possible that it takes the new timeout) */
Alexander Usyskin5ef79662015-10-26 14:07:58 +0200385 err = watchdog_ping(wdd);
386 if (err < 0)
387 return err;
Wim Van Sebroeck014d6942011-07-22 18:58:21 +0000388 /* Fall */
389 case WDIOC_GETTIMEOUT:
390 /* timeout == 0 means that we don't know the timeout */
391 if (wdd->timeout == 0)
392 return -EOPNOTSUPP;
393 return put_user(wdd->timeout, p);
Viresh Kumarfd7b6732012-03-16 09:14:00 +0100394 case WDIOC_GETTIMELEFT:
Hans de Goede7a879822012-05-22 11:40:26 +0200395 err = watchdog_get_timeleft(wdd, &val);
396 if (err)
397 return err;
398 return put_user(val, p);
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000399 default:
400 return -ENOTTY;
401 }
402}
403
404/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200405 * watchdog_open: open the /dev/watchdog* devices.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000406 * @inode: inode of device
407 * @file: file handle to device
408 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200409 * When the /dev/watchdog* device gets opened, we start the watchdog.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000410 * Watch out: the /dev/watchdog device is single open, so we make sure
411 * it can only be opened once.
412 */
413
414static int watchdog_open(struct inode *inode, struct file *file)
415{
416 int err = -EBUSY;
Alan Cox45f5fed2012-05-10 21:48:59 +0200417 struct watchdog_device *wdd;
418
419 /* Get the corresponding watchdog device */
420 if (imajor(inode) == MISC_MAJOR)
421 wdd = old_wdd;
422 else
423 wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000424
425 /* the watchdog is single open! */
426 if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
427 return -EBUSY;
428
429 /*
430 * If the /dev/watchdog device is open, we don't want the module
431 * to be unloaded.
432 */
433 if (!try_module_get(wdd->ops->owner))
434 goto out;
435
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000436 err = watchdog_start(wdd);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000437 if (err < 0)
438 goto out_mod;
439
Alan Cox45f5fed2012-05-10 21:48:59 +0200440 file->private_data = wdd;
441
Hans de Goedee907df32012-05-22 11:40:26 +0200442 if (wdd->ops->ref)
443 wdd->ops->ref(wdd);
444
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000445 /* dev/watchdog is a virtual (and thus non-seekable) filesystem */
446 return nonseekable_open(inode, file);
447
448out_mod:
449 module_put(wdd->ops->owner);
450out:
451 clear_bit(WDOG_DEV_OPEN, &wdd->status);
452 return err;
453}
454
455/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200456 * watchdog_release: release the watchdog device.
457 * @inode: inode of device
458 * @file: file handle to device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000459 *
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000460 * This is the code for when /dev/watchdog gets closed. We will only
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000461 * stop the watchdog when we have received the magic char (and nowayout
462 * was not set), else the watchdog will keep running.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000463 */
464
465static int watchdog_release(struct inode *inode, struct file *file)
466{
Alan Cox45f5fed2012-05-10 21:48:59 +0200467 struct watchdog_device *wdd = file->private_data;
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000468 int err = -EBUSY;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000469
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000470 /*
471 * We only stop the watchdog if we received the magic character
Wim Van Sebroeck7e192b92011-07-22 18:59:17 +0000472 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
473 * watchdog_stop will fail.
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000474 */
Hector Palaciosfcf95672013-04-08 17:06:32 +0200475 if (!test_bit(WDOG_ACTIVE, &wdd->status))
476 err = 0;
477 else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
478 !(wdd->info->options & WDIOF_MAGICCLOSE))
Wim Van Sebroeck017cf082011-07-22 18:58:54 +0000479 err = watchdog_stop(wdd);
480
481 /* If the watchdog was not stopped, send a keepalive ping */
Wim Van Sebroeck234445b2011-07-22 18:57:55 +0000482 if (err < 0) {
Hans de Goedee907df32012-05-22 11:40:26 +0200483 mutex_lock(&wdd->lock);
484 if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
485 dev_crit(wdd->dev, "watchdog did not stop!\n");
486 mutex_unlock(&wdd->lock);
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000487 watchdog_ping(wdd);
488 }
489
490 /* Allow the owner module to be unloaded again */
491 module_put(wdd->ops->owner);
492
493 /* make sure that /dev/watchdog can be re-opened */
494 clear_bit(WDOG_DEV_OPEN, &wdd->status);
495
Hans de Goedee907df32012-05-22 11:40:26 +0200496 /* Note wdd may be gone after this, do not use after this! */
497 if (wdd->ops->unref)
498 wdd->ops->unref(wdd);
499
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000500 return 0;
501}
502
503static const struct file_operations watchdog_fops = {
504 .owner = THIS_MODULE,
505 .write = watchdog_write,
Wim Van Sebroeck2fa03562011-07-22 18:56:38 +0000506 .unlocked_ioctl = watchdog_ioctl,
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000507 .open = watchdog_open,
508 .release = watchdog_release,
509};
510
511static struct miscdevice watchdog_miscdev = {
512 .minor = WATCHDOG_MINOR,
513 .name = "watchdog",
514 .fops = &watchdog_fops,
515};
516
517/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200518 * watchdog_dev_register: register a watchdog device
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700519 * @wdd: watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000520 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200521 * Register a watchdog device including handling the legacy
522 * /dev/watchdog node. /dev/watchdog is actually a miscdevice and
523 * thus we set it up like that.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000524 */
525
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700526int watchdog_dev_register(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000527{
Alan Cox45f5fed2012-05-10 21:48:59 +0200528 int err, devno;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000529
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700530 if (wdd->id == 0) {
531 old_wdd = wdd;
532 watchdog_miscdev.parent = wdd->parent;
Alan Cox45f5fed2012-05-10 21:48:59 +0200533 err = misc_register(&watchdog_miscdev);
534 if (err != 0) {
535 pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700536 wdd->info->identity, WATCHDOG_MINOR, err);
Alan Cox45f5fed2012-05-10 21:48:59 +0200537 if (err == -EBUSY)
538 pr_err("%s: a legacy watchdog module is probably present.\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700539 wdd->info->identity);
Guenter Roeck60403f72013-04-05 21:22:43 -0700540 old_wdd = NULL;
Alan Cox45f5fed2012-05-10 21:48:59 +0200541 return err;
542 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000543 }
544
Alan Cox45f5fed2012-05-10 21:48:59 +0200545 /* Fill in the data structures */
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700546 devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
547 cdev_init(&wdd->cdev, &watchdog_fops);
548 wdd->cdev.owner = wdd->ops->owner;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000549
Alan Cox45f5fed2012-05-10 21:48:59 +0200550 /* Add the device */
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700551 err = cdev_add(&wdd->cdev, devno, 1);
Alan Cox45f5fed2012-05-10 21:48:59 +0200552 if (err) {
553 pr_err("watchdog%d unable to add device %d:%d\n",
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700554 wdd->id, MAJOR(watchdog_devt), wdd->id);
555 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200556 misc_deregister(&watchdog_miscdev);
557 old_wdd = NULL;
558 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000559 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000560 return err;
561}
562
563/*
Alan Cox45f5fed2012-05-10 21:48:59 +0200564 * watchdog_dev_unregister: unregister a watchdog device
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000565 * @watchdog: watchdog device
566 *
Alan Cox45f5fed2012-05-10 21:48:59 +0200567 * Unregister the watchdog and if needed the legacy /dev/watchdog device.
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000568 */
569
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700570int watchdog_dev_unregister(struct watchdog_device *wdd)
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000571{
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700572 mutex_lock(&wdd->lock);
573 set_bit(WDOG_UNREGISTERED, &wdd->status);
574 mutex_unlock(&wdd->lock);
Hans de Goedee907df32012-05-22 11:40:26 +0200575
Guenter Roeckbc794ac2015-09-29 01:27:25 -0700576 cdev_del(&wdd->cdev);
577 if (wdd->id == 0) {
Alan Cox45f5fed2012-05-10 21:48:59 +0200578 misc_deregister(&watchdog_miscdev);
579 old_wdd = NULL;
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000580 }
Wim Van Sebroeck43316042011-07-22 18:55:18 +0000581 return 0;
582}
Alan Cox45f5fed2012-05-10 21:48:59 +0200583
584/*
585 * watchdog_dev_init: init dev part of watchdog core
586 *
587 * Allocate a range of chardev nodes to use for watchdog devices
588 */
589
590int __init watchdog_dev_init(void)
591{
592 int err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
593 if (err < 0)
594 pr_err("watchdog: unable to allocate char dev region\n");
595 return err;
596}
597
598/*
599 * watchdog_dev_exit: exit dev part of watchdog core
600 *
601 * Release the range of chardev nodes used for watchdog devices
602 */
603
604void __exit watchdog_dev_exit(void)
605{
606 unregister_chrdev_region(watchdog_devt, MAX_DOGS);
607}