watchdog_dev.c 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 *	watchdog_dev.c
 *
 *	(c) Copyright 2008-2011 Alan Cox <alan@lxorguk.ukuu.org.uk>,
 *						All Rights Reserved.
 *
 *	(c) Copyright 2008-2011 Wim Van Sebroeck <wim@iguana.be>.
 *
 *
 *	This source code is part of the generic code that can be used
 *	by all the watchdog timer drivers.
 *
 *	This part of the generic code takes care of the following
 *	misc device: /dev/watchdog.
 *
 *	Based on source code of the following authors:
 *	  Matt Domsch <Matt_Domsch@dell.com>,
 *	  Rob Radez <rob@osinvestor.com>,
 *	  Rusty Lynch <rusty@linux.co.intel.com>
 *	  Satyam Sharma <satyam@infradead.org>
 *	  Randy Dunlap <randy.dunlap@oracle.com>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *
 *	Neither Alan Cox, CymruNet Ltd., Wim Van Sebroeck nor Iguana vzw.
 *	admit liability nor provide warranty for any of this software.
 *	This material is provided "AS-IS" and at no charge.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>	/* For module stuff/... */
#include <linux/types.h>	/* For standard types (like size_t) */
#include <linux/errno.h>	/* For the -ENODEV/... values */
#include <linux/kernel.h>	/* For printk/panic/... */
#include <linux/fs.h>		/* For file operations */
#include <linux/watchdog.h>	/* For watchdog specific items */
#include <linux/miscdevice.h>	/* For handling misc devices */
#include <linux/init.h>		/* For __init/__exit/... */
#include <linux/uaccess.h>	/* For copy_to_user/put_user/... */

45
#include "watchdog_core.h"
46

A
Alan Cox 已提交
47 48
/* the dev_t structure to store the dynamically allocated watchdog devices */
static dev_t watchdog_devt;
49
/* the watchdog device behind /dev/watchdog */
A
Alan Cox 已提交
50
static struct watchdog_device *old_wdd;
51 52 53

/*
 *	watchdog_ping: ping the watchdog.
54
 *	@wdd: the watchdog device to ping
55 56 57 58
 *
 *	If the watchdog has no own ping operation then it needs to be
 *	restarted via the start operation. This wrapper function does
 *	exactly that.
59
 *	We only ping when the watchdog device is running.
60 61
 */

62
static int watchdog_ping(struct watchdog_device *wdd)
63
{
64 65
	int err = 0;

66
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
67

68
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
69 70 71 72
		err = -ENODEV;
		goto out_ping;
	}

73
	if (!watchdog_active(wdd))
74 75
		goto out_ping;

76 77
	if (wdd->ops->ping)
		err = wdd->ops->ping(wdd);	/* ping the watchdog */
78
	else
79
		err = wdd->ops->start(wdd);	/* restart watchdog */
80 81

out_ping:
82
	mutex_unlock(&wdd->lock);
83
	return err;
84 85 86 87
}

/*
 *	watchdog_start: wrapper to start the watchdog.
88
 *	@wdd: the watchdog device to start
89 90 91 92 93 94
 *
 *	Start the watchdog if it is not active and mark it active.
 *	This function returns zero on success or a negative errno code for
 *	failure.
 */

95
static int watchdog_start(struct watchdog_device *wdd)
96
{
97
	int err = 0;
98

99
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
100

101
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
102 103 104 105
		err = -ENODEV;
		goto out_start;
	}

106
	if (watchdog_active(wdd))
107
		goto out_start;
108

109
	err = wdd->ops->start(wdd);
110
	if (err == 0)
111
		set_bit(WDOG_ACTIVE, &wdd->status);
112 113

out_start:
114
	mutex_unlock(&wdd->lock);
115
	return err;
116 117 118 119
}

/*
 *	watchdog_stop: wrapper to stop the watchdog.
120
 *	@wdd: the watchdog device to stop
121 122 123 124
 *
 *	Stop the watchdog if it is still active and unmark it active.
 *	This function returns zero on success or a negative errno code for
 *	failure.
125
 *	If the 'nowayout' feature was set, the watchdog cannot be stopped.
126 127
 */

128
static int watchdog_stop(struct watchdog_device *wdd)
129
{
130 131
	int err = 0;

132
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
133

134
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
135 136 137 138
		err = -ENODEV;
		goto out_stop;
	}

139
	if (!watchdog_active(wdd))
140
		goto out_stop;
141

142 143
	if (test_bit(WDOG_NO_WAY_OUT, &wdd->status)) {
		dev_info(wdd->dev, "nowayout prevents watchdog being stopped!\n");
144 145
		err = -EBUSY;
		goto out_stop;
146
	}
147

148
	err = wdd->ops->stop(wdd);
149
	if (err == 0)
150
		clear_bit(WDOG_ACTIVE, &wdd->status);
151 152

out_stop:
153
	mutex_unlock(&wdd->lock);
154 155 156 157 158
	return err;
}

/*
 *	watchdog_get_status: wrapper to get the watchdog status
159
 *	@wdd: the watchdog device to get the status from
160 161 162 163 164
 *	@status: the status of the watchdog device
 *
 *	Get the watchdog's status flags.
 */

165
static int watchdog_get_status(struct watchdog_device *wdd,
166 167 168 169 170
							unsigned int *status)
{
	int err = 0;

	*status = 0;
171
	if (!wdd->ops->status)
172 173
		return -EOPNOTSUPP;

174
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
175

176
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
177 178 179 180
		err = -ENODEV;
		goto out_status;
	}

181
	*status = wdd->ops->status(wdd);
182

183
out_status:
184
	mutex_unlock(&wdd->lock);
185 186 187 188 189
	return err;
}

/*
 *	watchdog_set_timeout: set the watchdog timer timeout
190
 *	@wdd: the watchdog device to set the timeout for
191 192 193
 *	@timeout: timeout to set in seconds
 */

194
static int watchdog_set_timeout(struct watchdog_device *wdd,
195 196 197 198
							unsigned int timeout)
{
	int err;

199
	if (!wdd->ops->set_timeout || !(wdd->info->options & WDIOF_SETTIMEOUT))
200 201
		return -EOPNOTSUPP;

202
	if (watchdog_timeout_invalid(wdd, timeout))
203 204
		return -EINVAL;

205
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
206

207
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
208 209 210 211
		err = -ENODEV;
		goto out_timeout;
	}

212
	err = wdd->ops->set_timeout(wdd, timeout);
213

214
out_timeout:
215
	mutex_unlock(&wdd->lock);
216 217 218 219 220
	return err;
}

/*
 *	watchdog_get_timeleft: wrapper to get the time left before a reboot
221
 *	@wdd: the watchdog device to get the remaining time from
222 223 224 225 226
 *	@timeleft: the time that's left
 *
 *	Get the time before a watchdog will reboot (if not pinged).
 */

227
static int watchdog_get_timeleft(struct watchdog_device *wdd,
228 229 230 231 232
							unsigned int *timeleft)
{
	int err = 0;

	*timeleft = 0;
233
	if (!wdd->ops->get_timeleft)
234 235
		return -EOPNOTSUPP;

236
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
237

238
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
239 240 241 242
		err = -ENODEV;
		goto out_timeleft;
	}

243
	*timeleft = wdd->ops->get_timeleft(wdd);
244

245
out_timeleft:
246
	mutex_unlock(&wdd->lock);
247 248 249 250 251
	return err;
}

/*
 *	watchdog_ioctl_op: call the watchdog drivers ioctl op if defined
252
 *	@wdd: the watchdog device to do the ioctl on
253 254 255 256
 *	@cmd: watchdog command
 *	@arg: argument pointer
 */

257
static int watchdog_ioctl_op(struct watchdog_device *wdd, unsigned int cmd,
258 259 260 261
							unsigned long arg)
{
	int err;

262
	if (!wdd->ops->ioctl)
263 264
		return -ENOIOCTLCMD;

265
	mutex_lock(&wdd->lock);
H
Hans de Goede 已提交
266

267
	if (test_bit(WDOG_UNREGISTERED, &wdd->status)) {
268 269 270 271
		err = -ENODEV;
		goto out_ioctl;
	}

272
	err = wdd->ops->ioctl(wdd, cmd, arg);
273

274
out_ioctl:
275
	mutex_unlock(&wdd->lock);
276
	return err;
277 278 279 280 281 282 283 284 285 286
}

/*
 *	watchdog_write: writes to the watchdog.
 *	@file: file from VFS
 *	@data: user address of data
 *	@len: length of data
 *	@ppos: pointer to the file offset
 *
 *	A write to a watchdog device is defined as a keepalive ping.
287
 *	Writing the magic 'V' sequence allows the next close to turn
288
 *	off the watchdog (if 'nowayout' is not set).
289 290 291 292 293
 */

static ssize_t watchdog_write(struct file *file, const char __user *data,
						size_t len, loff_t *ppos)
{
A
Alan Cox 已提交
294
	struct watchdog_device *wdd = file->private_data;
295 296
	size_t i;
	char c;
297
	int err;
298 299 300 301

	if (len == 0)
		return 0;

302 303 304 305 306 307 308
	/*
	 * Note: just in case someone wrote the magic character
	 * five months ago...
	 */
	clear_bit(WDOG_ALLOW_RELEASE, &wdd->status);

	/* scan to see whether or not we got the magic character */
309 310 311
	for (i = 0; i != len; i++) {
		if (get_user(c, data + i))
			return -EFAULT;
312 313
		if (c == 'V')
			set_bit(WDOG_ALLOW_RELEASE, &wdd->status);
314 315 316
	}

	/* someone wrote to us, so we send the watchdog a keepalive ping */
317 318 319
	err = watchdog_ping(wdd);
	if (err < 0)
		return err;
320 321 322 323

	return len;
}

324 325 326 327 328 329 330 331 332 333 334 335 336
/*
 *	watchdog_ioctl: handle the different ioctl's for the watchdog device.
 *	@file: file handle to the device
 *	@cmd: watchdog command
 *	@arg: argument pointer
 *
 *	The watchdog API defines a common set of functions for all watchdogs
 *	according to their available features.
 */

static long watchdog_ioctl(struct file *file, unsigned int cmd,
							unsigned long arg)
{
A
Alan Cox 已提交
337
	struct watchdog_device *wdd = file->private_data;
338 339 340
	void __user *argp = (void __user *)arg;
	int __user *p = argp;
	unsigned int val;
341
	int err;
342

343 344 345
	err = watchdog_ioctl_op(wdd, cmd, arg);
	if (err != -ENOIOCTLCMD)
		return err;
346

347 348 349 350 351
	switch (cmd) {
	case WDIOC_GETSUPPORT:
		return copy_to_user(argp, wdd->info,
			sizeof(struct watchdog_info)) ? -EFAULT : 0;
	case WDIOC_GETSTATUS:
352
		err = watchdog_get_status(wdd, &val);
353
		if (err == -ENODEV)
354
			return err;
355 356 357
		return put_user(val, p);
	case WDIOC_GETBOOTSTATUS:
		return put_user(wdd->bootstatus, p);
358 359 360 361 362 363 364 365 366 367 368 369 370 371
	case WDIOC_SETOPTIONS:
		if (get_user(val, p))
			return -EFAULT;
		if (val & WDIOS_DISABLECARD) {
			err = watchdog_stop(wdd);
			if (err < 0)
				return err;
		}
		if (val & WDIOS_ENABLECARD) {
			err = watchdog_start(wdd);
			if (err < 0)
				return err;
		}
		return 0;
372 373 374
	case WDIOC_KEEPALIVE:
		if (!(wdd->info->options & WDIOF_KEEPALIVEPING))
			return -EOPNOTSUPP;
375
		return watchdog_ping(wdd);
376 377 378
	case WDIOC_SETTIMEOUT:
		if (get_user(val, p))
			return -EFAULT;
379
		err = watchdog_set_timeout(wdd, val);
380 381 382 383 384
		if (err < 0)
			return err;
		/* If the watchdog is active then we send a keepalive ping
		 * to make sure that the watchdog keep's running (and if
		 * possible that it takes the new timeout) */
385 386 387
		err = watchdog_ping(wdd);
		if (err < 0)
			return err;
388 389 390 391 392 393
		/* Fall */
	case WDIOC_GETTIMEOUT:
		/* timeout == 0 means that we don't know the timeout */
		if (wdd->timeout == 0)
			return -EOPNOTSUPP;
		return put_user(wdd->timeout, p);
394
	case WDIOC_GETTIMELEFT:
395 396 397 398
		err = watchdog_get_timeleft(wdd, &val);
		if (err)
			return err;
		return put_user(val, p);
399 400 401 402 403
	default:
		return -ENOTTY;
	}
}

404
/*
A
Alan Cox 已提交
405
 *	watchdog_open: open the /dev/watchdog* devices.
406 407 408
 *	@inode: inode of device
 *	@file: file handle to device
 *
A
Alan Cox 已提交
409
 *	When the /dev/watchdog* device gets opened, we start the watchdog.
410 411 412 413 414 415 416
 *	Watch out: the /dev/watchdog device is single open, so we make sure
 *	it can only be opened once.
 */

static int watchdog_open(struct inode *inode, struct file *file)
{
	int err = -EBUSY;
A
Alan Cox 已提交
417 418 419 420 421 422 423
	struct watchdog_device *wdd;

	/* Get the corresponding watchdog device */
	if (imajor(inode) == MISC_MAJOR)
		wdd = old_wdd;
	else
		wdd = container_of(inode->i_cdev, struct watchdog_device, cdev);
424 425 426 427 428 429 430 431 432 433 434 435

	/* the watchdog is single open! */
	if (test_and_set_bit(WDOG_DEV_OPEN, &wdd->status))
		return -EBUSY;

	/*
	 * If the /dev/watchdog device is open, we don't want the module
	 * to be unloaded.
	 */
	if (!try_module_get(wdd->ops->owner))
		goto out;

436
	err = watchdog_start(wdd);
437 438 439
	if (err < 0)
		goto out_mod;

A
Alan Cox 已提交
440 441
	file->private_data = wdd;

442 443 444
	if (wdd->ops->ref)
		wdd->ops->ref(wdd);

445 446 447 448 449 450 451 452 453 454 455
	/* dev/watchdog is a virtual (and thus non-seekable) filesystem */
	return nonseekable_open(inode, file);

out_mod:
	module_put(wdd->ops->owner);
out:
	clear_bit(WDOG_DEV_OPEN, &wdd->status);
	return err;
}

/*
A
Alan Cox 已提交
456 457 458
 *	watchdog_release: release the watchdog device.
 *	@inode: inode of device
 *	@file: file handle to device
459
 *
460
 *	This is the code for when /dev/watchdog gets closed. We will only
461 462
 *	stop the watchdog when we have received the magic char (and nowayout
 *	was not set), else the watchdog will keep running.
463 464 465 466
 */

static int watchdog_release(struct inode *inode, struct file *file)
{
A
Alan Cox 已提交
467
	struct watchdog_device *wdd = file->private_data;
468 469 470 471
	int err = -EBUSY;

	/*
	 * We only stop the watchdog if we received the magic character
472 473
	 * or if WDIOF_MAGICCLOSE is not set. If nowayout was set then
	 * watchdog_stop will fail.
474
	 */
475 476 477 478
	if (!test_bit(WDOG_ACTIVE, &wdd->status))
		err = 0;
	else if (test_and_clear_bit(WDOG_ALLOW_RELEASE, &wdd->status) ||
		 !(wdd->info->options & WDIOF_MAGICCLOSE))
479
		err = watchdog_stop(wdd);
480

481
	/* If the watchdog was not stopped, send a keepalive ping */
482
	if (err < 0) {
483 484 485 486
		mutex_lock(&wdd->lock);
		if (!test_bit(WDOG_UNREGISTERED, &wdd->status))
			dev_crit(wdd->dev, "watchdog did not stop!\n");
		mutex_unlock(&wdd->lock);
487 488 489 490 491 492 493 494 495
		watchdog_ping(wdd);
	}

	/* Allow the owner module to be unloaded again */
	module_put(wdd->ops->owner);

	/* make sure that /dev/watchdog can be re-opened */
	clear_bit(WDOG_DEV_OPEN, &wdd->status);

496 497 498 499
	/* Note wdd may be gone after this, do not use after this! */
	if (wdd->ops->unref)
		wdd->ops->unref(wdd);

500 501 502 503 504 505
	return 0;
}

static const struct file_operations watchdog_fops = {
	.owner		= THIS_MODULE,
	.write		= watchdog_write,
506
	.unlocked_ioctl	= watchdog_ioctl,
507 508 509 510 511 512 513 514 515 516 517
	.open		= watchdog_open,
	.release	= watchdog_release,
};

static struct miscdevice watchdog_miscdev = {
	.minor		= WATCHDOG_MINOR,
	.name		= "watchdog",
	.fops		= &watchdog_fops,
};

/*
A
Alan Cox 已提交
518
 *	watchdog_dev_register: register a watchdog device
519
 *	@wdd: watchdog device
520
 *
A
Alan Cox 已提交
521 522 523
 *	Register a watchdog device including handling the legacy
 *	/dev/watchdog node. /dev/watchdog is actually a miscdevice and
 *	thus we set it up like that.
524 525
 */

526
int watchdog_dev_register(struct watchdog_device *wdd)
527
{
A
Alan Cox 已提交
528 529
	int err, devno;

530 531 532
	if (wdd->id == 0) {
		old_wdd = wdd;
		watchdog_miscdev.parent = wdd->parent;
A
Alan Cox 已提交
533 534 535
		err = misc_register(&watchdog_miscdev);
		if (err != 0) {
			pr_err("%s: cannot register miscdev on minor=%d (err=%d).\n",
536
				wdd->info->identity, WATCHDOG_MINOR, err);
A
Alan Cox 已提交
537 538
			if (err == -EBUSY)
				pr_err("%s: a legacy watchdog module is probably present.\n",
539
					wdd->info->identity);
540
			old_wdd = NULL;
A
Alan Cox 已提交
541 542
			return err;
		}
543 544
	}

A
Alan Cox 已提交
545
	/* Fill in the data structures */
546 547 548
	devno = MKDEV(MAJOR(watchdog_devt), wdd->id);
	cdev_init(&wdd->cdev, &watchdog_fops);
	wdd->cdev.owner = wdd->ops->owner;
A
Alan Cox 已提交
549 550

	/* Add the device */
551
	err  = cdev_add(&wdd->cdev, devno, 1);
A
Alan Cox 已提交
552 553
	if (err) {
		pr_err("watchdog%d unable to add device %d:%d\n",
554 555
			wdd->id,  MAJOR(watchdog_devt), wdd->id);
		if (wdd->id == 0) {
A
Alan Cox 已提交
556 557 558
			misc_deregister(&watchdog_miscdev);
			old_wdd = NULL;
		}
559 560 561 562 563
	}
	return err;
}

/*
A
Alan Cox 已提交
564
 *	watchdog_dev_unregister: unregister a watchdog device
565 566
 *	@watchdog: watchdog device
 *
A
Alan Cox 已提交
567
 *	Unregister the watchdog and if needed the legacy /dev/watchdog device.
568 569
 */

570
int watchdog_dev_unregister(struct watchdog_device *wdd)
571
{
572 573 574
	mutex_lock(&wdd->lock);
	set_bit(WDOG_UNREGISTERED, &wdd->status);
	mutex_unlock(&wdd->lock);
575

576 577
	cdev_del(&wdd->cdev);
	if (wdd->id == 0) {
A
Alan Cox 已提交
578 579
		misc_deregister(&watchdog_miscdev);
		old_wdd = NULL;
580 581 582
	}
	return 0;
}
A
Alan Cox 已提交
583

584 585 586 587 588
static struct class watchdog_class = {
	.name =		"watchdog",
	.owner =	THIS_MODULE,
};

A
Alan Cox 已提交
589 590 591 592 593 594
/*
 *	watchdog_dev_init: init dev part of watchdog core
 *
 *	Allocate a range of chardev nodes to use for watchdog devices
 */

595
struct class * __init watchdog_dev_init(void)
A
Alan Cox 已提交
596
{
597 598 599 600 601 602 603 604 605 606
	int err;

	err = class_register(&watchdog_class);
	if (err < 0) {
		pr_err("couldn't register class\n");
		return ERR_PTR(err);
	}

	err = alloc_chrdev_region(&watchdog_devt, 0, MAX_DOGS, "watchdog");
	if (err < 0) {
A
Alan Cox 已提交
607
		pr_err("watchdog: unable to allocate char dev region\n");
608 609 610 611 612
		class_unregister(&watchdog_class);
		return ERR_PTR(err);
	}

	return &watchdog_class;
A
Alan Cox 已提交
613 614 615 616 617 618 619 620 621 622 623
}

/*
 *	watchdog_dev_exit: exit dev part of watchdog core
 *
 *	Release the range of chardev nodes used for watchdog devices
 */

void __exit watchdog_dev_exit(void)
{
	unregister_chrdev_region(watchdog_devt, MAX_DOGS);
624
	class_unregister(&watchdog_class);
A
Alan Cox 已提交
625
}