v4l2-async.c 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * V4L2 asynchronous subdevice registration API
 *
 * Copyright (C) 2012-2013, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/list.h>
15
#include <linux/mm.h>
16 17
#include <linux/module.h>
#include <linux/mutex.h>
18
#include <linux/of.h>
19 20 21 22 23 24
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/types.h>

#include <media/v4l2-async.h>
#include <media/v4l2-device.h>
25
#include <media/v4l2-fwnode.h>
26 27
#include <media/v4l2-subdev.h>

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static int v4l2_async_notifier_call_bound(struct v4l2_async_notifier *n,
					  struct v4l2_subdev *subdev,
					  struct v4l2_async_subdev *asd)
{
	if (!n->ops || !n->ops->bound)
		return 0;

	return n->ops->bound(n, subdev, asd);
}

static void v4l2_async_notifier_call_unbind(struct v4l2_async_notifier *n,
					    struct v4l2_subdev *subdev,
					    struct v4l2_async_subdev *asd)
{
	if (!n->ops || !n->ops->unbind)
		return;

	n->ops->unbind(n, subdev, asd);
}

static int v4l2_async_notifier_call_complete(struct v4l2_async_notifier *n)
{
	if (!n->ops || !n->ops->complete)
		return 0;

	return n->ops->complete(n);
}

56
static bool match_i2c(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
57
{
58
#if IS_ENABLED(CONFIG_I2C)
59
	struct i2c_client *client = i2c_verify_client(sd->dev);
60 61 62
	return client &&
		asd->match.i2c.adapter_id == client->adapter->nr &&
		asd->match.i2c.address == client->addr;
63 64 65
#else
	return false;
#endif
66 67
}

68 69
static bool match_devname(struct v4l2_subdev *sd,
			  struct v4l2_async_subdev *asd)
70
{
71
	return !strcmp(asd->match.device_name.name, dev_name(sd->dev));
72 73
}

74 75
static bool match_fwnode(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
{
76
	return sd->fwnode == asd->match.fwnode.fwnode;
77 78
}

79 80 81 82 83 84 85
static bool match_custom(struct v4l2_subdev *sd, struct v4l2_async_subdev *asd)
{
	if (!asd->match.custom.match)
		/* Match always */
		return true;

	return asd->match.custom.match(sd->dev, asd);
86 87
}

88 89 90 91
static LIST_HEAD(subdev_list);
static LIST_HEAD(notifier_list);
static DEFINE_MUTEX(list_lock);

92 93
static struct v4l2_async_subdev *v4l2_async_find_match(
	struct v4l2_async_notifier *notifier, struct v4l2_subdev *sd)
94
{
95
	bool (*match)(struct v4l2_subdev *, struct v4l2_async_subdev *);
96 97 98 99
	struct v4l2_async_subdev *asd;

	list_for_each_entry(asd, &notifier->waiting, list) {
		/* bus_type has been verified valid before */
100 101
		switch (asd->match_type) {
		case V4L2_ASYNC_MATCH_CUSTOM:
102
			match = match_custom;
103
			break;
104 105
		case V4L2_ASYNC_MATCH_DEVNAME:
			match = match_devname;
106
			break;
107
		case V4L2_ASYNC_MATCH_I2C:
108 109
			match = match_i2c;
			break;
110 111 112
		case V4L2_ASYNC_MATCH_FWNODE:
			match = match_fwnode;
			break;
113 114 115 116 117 118 119
		default:
			/* Cannot happen, unless someone breaks us */
			WARN_ON(true);
			return NULL;
		}

		/* match cannot be NULL here */
120
		if (match(sd, asd))
121 122 123 124 125 126
			return asd;
	}

	return NULL;
}

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/* Find the sub-device notifier registered by a sub-device driver. */
static struct v4l2_async_notifier *v4l2_async_find_subdev_notifier(
	struct v4l2_subdev *sd)
{
	struct v4l2_async_notifier *n;

	list_for_each_entry(n, &notifier_list, list)
		if (n->sd == sd)
			return n;

	return NULL;
}

/* Get v4l2_device related to the notifier if one can be found. */
static struct v4l2_device *v4l2_async_notifier_find_v4l2_dev(
	struct v4l2_async_notifier *notifier)
{
	while (notifier->parent)
		notifier = notifier->parent;

	return notifier->v4l2_dev;
}

/*
 * Return true if all child sub-device notifiers are complete, false otherwise.
 */
static bool v4l2_async_notifier_can_complete(
	struct v4l2_async_notifier *notifier)
{
	struct v4l2_subdev *sd;

	if (!list_empty(&notifier->waiting))
		return false;

	list_for_each_entry(sd, &notifier->done, async_list) {
		struct v4l2_async_notifier *subdev_notifier =
			v4l2_async_find_subdev_notifier(sd);

		if (subdev_notifier &&
		    !v4l2_async_notifier_can_complete(subdev_notifier))
			return false;
	}

	return true;
}

/*
 * Complete the master notifier if possible. This is done when all async
 * sub-devices have been bound; v4l2_device is also available then.
 */
static int v4l2_async_notifier_try_complete(
	struct v4l2_async_notifier *notifier)
{
	/* Quick check whether there are still more sub-devices here. */
	if (!list_empty(&notifier->waiting))
		return 0;

	/* Check the entire notifier tree; find the root notifier first. */
	while (notifier->parent)
		notifier = notifier->parent;

	/* This is root if it has v4l2_dev. */
	if (!notifier->v4l2_dev)
		return 0;

	/* Is everything ready? */
	if (!v4l2_async_notifier_can_complete(notifier))
		return 0;

	return v4l2_async_notifier_call_complete(notifier);
}

static int v4l2_async_notifier_try_all_subdevs(
	struct v4l2_async_notifier *notifier);

202
static int v4l2_async_match_notify(struct v4l2_async_notifier *notifier,
203
				   struct v4l2_device *v4l2_dev,
204 205
				   struct v4l2_subdev *sd,
				   struct v4l2_async_subdev *asd)
206
{
207
	struct v4l2_async_notifier *subdev_notifier;
208 209
	int ret;

210
	ret = v4l2_device_register_subdev(v4l2_dev, sd);
211 212
	if (ret < 0)
		return ret;
213

214
	ret = v4l2_async_notifier_call_bound(notifier, sd, asd);
215
	if (ret < 0) {
216
		v4l2_device_unregister_subdev(sd);
217 218 219
		return ret;
	}

220 221 222 223 224 225 226 227
	/* Remove from the waiting list */
	list_del(&asd->list);
	sd->asd = asd;
	sd->notifier = notifier;

	/* Move from the global subdevice list to notifier's done */
	list_move(&sd->async_list, &notifier->done);

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	/*
	 * See if the sub-device has a notifier. If not, return here.
	 */
	subdev_notifier = v4l2_async_find_subdev_notifier(sd);
	if (!subdev_notifier || subdev_notifier->parent)
		return 0;

	/*
	 * Proceed with checking for the sub-device notifier's async
	 * sub-devices, and return the result. The error will be handled by the
	 * caller.
	 */
	subdev_notifier->parent = notifier;

	return v4l2_async_notifier_try_all_subdevs(subdev_notifier);
243 244
}

245 246 247 248
/* Test all async sub-devices in a notifier for a match. */
static int v4l2_async_notifier_try_all_subdevs(
	struct v4l2_async_notifier *notifier)
{
249 250 251 252 253 254
	struct v4l2_device *v4l2_dev =
		v4l2_async_notifier_find_v4l2_dev(notifier);
	struct v4l2_subdev *sd;

	if (!v4l2_dev)
		return 0;
255

256 257
again:
	list_for_each_entry(sd, &subdev_list, async_list) {
258 259 260 261 262 263 264 265 266 267
		struct v4l2_async_subdev *asd;
		int ret;

		asd = v4l2_async_find_match(notifier, sd);
		if (!asd)
			continue;

		ret = v4l2_async_match_notify(notifier, v4l2_dev, sd, asd);
		if (ret < 0)
			return ret;
268 269 270 271 272 273 274 275

		/*
		 * v4l2_async_match_notify() may lead to registering a
		 * new notifier and thus changing the async subdevs
		 * list. In order to proceed safely from here, restart
		 * parsing the list from the beginning.
		 */
		goto again;
276 277 278 279 280
	}

	return 0;
}

281
static void v4l2_async_cleanup(struct v4l2_subdev *sd)
282 283
{
	v4l2_device_unregister_subdev(sd);
284 285 286
	/* Subdevice driver will reprobe and put the subdev back onto the list */
	list_del_init(&sd->async_list);
	sd->asd = NULL;
287 288
}

289
/* Unbind all sub-devices in the notifier tree. */
290 291 292 293 294 295
static void v4l2_async_notifier_unbind_all_subdevs(
	struct v4l2_async_notifier *notifier)
{
	struct v4l2_subdev *sd, *tmp;

	list_for_each_entry_safe(sd, tmp, &notifier->done, async_list) {
296 297 298 299 300 301
		struct v4l2_async_notifier *subdev_notifier =
			v4l2_async_find_subdev_notifier(sd);

		if (subdev_notifier)
			v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);

302
		v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
303 304 305 306
		v4l2_async_cleanup(sd);

		list_move(&sd->async_list, &subdev_list);
	}
307 308

	notifier->parent = NULL;
309 310
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/* See if an fwnode can be found in a notifier's lists. */
static bool __v4l2_async_notifier_fwnode_has_async_subdev(
	struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode)
{
	struct v4l2_async_subdev *asd;
	struct v4l2_subdev *sd;

	list_for_each_entry(asd, &notifier->waiting, list) {
		if (asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
			continue;

		if (asd->match.fwnode.fwnode == fwnode)
			return true;
	}

	list_for_each_entry(sd, &notifier->done, async_list) {
		if (WARN_ON(!sd->asd))
			continue;

		if (sd->asd->match_type != V4L2_ASYNC_MATCH_FWNODE)
			continue;

		if (sd->asd->match.fwnode.fwnode == fwnode)
			return true;
	}

	return false;
}

/*
 * Find out whether an async sub-device was set up for an fwnode already or
 * whether it exists in a given notifier before @this_index.
 */
static bool v4l2_async_notifier_fwnode_has_async_subdev(
	struct v4l2_async_notifier *notifier, struct fwnode_handle *fwnode,
	unsigned int this_index)
{
	unsigned int j;

	lockdep_assert_held(&list_lock);

	/* Check that an fwnode is not being added more than once. */
	for (j = 0; j < this_index; j++) {
		struct v4l2_async_subdev *asd = notifier->subdevs[this_index];
		struct v4l2_async_subdev *other_asd = notifier->subdevs[j];

		if (other_asd->match_type == V4L2_ASYNC_MATCH_FWNODE &&
		    asd->match.fwnode.fwnode ==
		    other_asd->match.fwnode.fwnode)
			return true;
	}

	/* Check than an fwnode did not exist in other notifiers. */
	list_for_each_entry(notifier, &notifier_list, list)
		if (__v4l2_async_notifier_fwnode_has_async_subdev(
			    notifier, fwnode))
			return true;

	return false;
}

372
static int __v4l2_async_notifier_register(struct v4l2_async_notifier *notifier)
373
{
374 375
	struct device *dev =
		notifier->v4l2_dev ? notifier->v4l2_dev->dev : NULL;
376
	struct v4l2_async_subdev *asd;
377
	int ret;
378 379
	int i;

380
	if (notifier->num_subdevs > V4L2_MAX_SUBDEVS)
381 382 383 384 385
		return -EINVAL;

	INIT_LIST_HEAD(&notifier->waiting);
	INIT_LIST_HEAD(&notifier->done);

386 387
	mutex_lock(&list_lock);

388
	for (i = 0; i < notifier->num_subdevs; i++) {
389
		asd = notifier->subdevs[i];
390

391 392 393 394
		switch (asd->match_type) {
		case V4L2_ASYNC_MATCH_CUSTOM:
		case V4L2_ASYNC_MATCH_DEVNAME:
		case V4L2_ASYNC_MATCH_I2C:
395
			break;
396
		case V4L2_ASYNC_MATCH_FWNODE:
397 398 399 400 401 402 403
			if (v4l2_async_notifier_fwnode_has_async_subdev(
				    notifier, asd->match.fwnode.fwnode, i)) {
				dev_err(dev,
					"fwnode has already been registered or in notifier's subdev list\n");
				ret = -EEXIST;
				goto err_unlock;
			}
404 405
			break;
		default:
406
			dev_err(dev, "Invalid match type %u on %p\n",
407
				asd->match_type, asd);
408 409
			ret = -EINVAL;
			goto err_unlock;
410 411 412 413
		}
		list_add_tail(&asd->list, &notifier->waiting);
	}

414
	ret = v4l2_async_notifier_try_all_subdevs(notifier);
415
	if (ret < 0)
416
		goto err_unbind;
417

418
	ret = v4l2_async_notifier_try_complete(notifier);
419
	if (ret < 0)
420
		goto err_unbind;
421

422 423 424
	/* Keep also completed notifiers on the list */
	list_add(&notifier->list, &notifier_list);

425 426 427
	mutex_unlock(&list_lock);

	return 0;
428

429 430 431 432
err_unbind:
	/*
	 * On failure, unbind all sub-devices registered through this notifier.
	 */
433 434
	v4l2_async_notifier_unbind_all_subdevs(notifier);

435
err_unlock:
436 437 438
	mutex_unlock(&list_lock);

	return ret;
439
}
440 441 442 443 444 445

int v4l2_async_notifier_register(struct v4l2_device *v4l2_dev,
				 struct v4l2_async_notifier *notifier)
{
	int ret;

446
	if (WARN_ON(!v4l2_dev || notifier->sd))
447 448 449 450 451 452 453 454 455 456
		return -EINVAL;

	notifier->v4l2_dev = v4l2_dev;

	ret = __v4l2_async_notifier_register(notifier);
	if (ret)
		notifier->v4l2_dev = NULL;

	return ret;
}
457 458
EXPORT_SYMBOL(v4l2_async_notifier_register);

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
int v4l2_async_subdev_notifier_register(struct v4l2_subdev *sd,
					struct v4l2_async_notifier *notifier)
{
	int ret;

	if (WARN_ON(!sd || notifier->v4l2_dev))
		return -EINVAL;

	notifier->sd = sd;

	ret = __v4l2_async_notifier_register(notifier);
	if (ret)
		notifier->sd = NULL;

	return ret;
}
EXPORT_SYMBOL(v4l2_async_subdev_notifier_register);

477 478
static void __v4l2_async_notifier_unregister(
	struct v4l2_async_notifier *notifier)
479
{
480
	if (!notifier || (!notifier->v4l2_dev && !notifier->sd))
481 482
		return;

483
	v4l2_async_notifier_unbind_all_subdevs(notifier);
484

485
	notifier->sd = NULL;
486
	notifier->v4l2_dev = NULL;
487 488

	list_del(&notifier->list);
489 490 491 492 493 494 495
}

void v4l2_async_notifier_unregister(struct v4l2_async_notifier *notifier)
{
	mutex_lock(&list_lock);

	__v4l2_async_notifier_unregister(notifier);
496 497

	mutex_unlock(&list_lock);
498 499 500
}
EXPORT_SYMBOL(v4l2_async_notifier_unregister);

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
void v4l2_async_notifier_cleanup(struct v4l2_async_notifier *notifier)
{
	unsigned int i;

	if (!notifier->max_subdevs)
		return;

	for (i = 0; i < notifier->num_subdevs; i++) {
		struct v4l2_async_subdev *asd = notifier->subdevs[i];

		switch (asd->match_type) {
		case V4L2_ASYNC_MATCH_FWNODE:
			fwnode_handle_put(asd->match.fwnode.fwnode);
			break;
		default:
			WARN_ON_ONCE(true);
			break;
		}

		kfree(asd);
	}

	notifier->max_subdevs = 0;
	notifier->num_subdevs = 0;

	kvfree(notifier->subdevs);
	notifier->subdevs = NULL;
}
EXPORT_SYMBOL_GPL(v4l2_async_notifier_cleanup);

531 532
int v4l2_async_register_subdev(struct v4l2_subdev *sd)
{
533
	struct v4l2_async_notifier *subdev_notifier;
534
	struct v4l2_async_notifier *notifier;
535
	int ret;
536

537 538 539 540 541
	/*
	 * No reference taken. The reference is held by the device
	 * (struct v4l2_subdev.dev), and async sub-device does not
	 * exist independently of the device at any point of time.
	 */
542 543
	if (!sd->fwnode && sd->dev)
		sd->fwnode = dev_fwnode(sd->dev);
544

545 546
	mutex_lock(&list_lock);

547
	INIT_LIST_HEAD(&sd->async_list);
548 549

	list_for_each_entry(notifier, &notifier_list, list) {
550 551 552
		struct v4l2_device *v4l2_dev =
			v4l2_async_notifier_find_v4l2_dev(notifier);
		struct v4l2_async_subdev *asd;
553 554
		int ret;

555 556 557 558
		if (!v4l2_dev)
			continue;

		asd = v4l2_async_find_match(notifier, sd);
559 560 561
		if (!asd)
			continue;

562 563
		ret = v4l2_async_match_notify(notifier, notifier->v4l2_dev, sd,
					      asd);
564
		if (ret)
565
			goto err_unbind;
566

567
		ret = v4l2_async_notifier_try_complete(notifier);
568
		if (ret)
569
			goto err_unbind;
570 571

		goto out_unlock;
572 573 574
	}

	/* None matched, wait for hot-plugging */
575
	list_add(&sd->async_list, &subdev_list);
576

577
out_unlock:
578 579 580
	mutex_unlock(&list_lock);

	return 0;
581

582 583 584 585 586 587 588 589 590 591 592
err_unbind:
	/*
	 * Complete failed. Unbind the sub-devices bound through registering
	 * this async sub-device.
	 */
	subdev_notifier = v4l2_async_find_subdev_notifier(sd);
	if (subdev_notifier)
		v4l2_async_notifier_unbind_all_subdevs(subdev_notifier);

	if (sd->asd)
		v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
593 594 595 596 597
	v4l2_async_cleanup(sd);

	mutex_unlock(&list_lock);

	return ret;
598 599 600 601 602 603 604
}
EXPORT_SYMBOL(v4l2_async_register_subdev);

void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
{
	mutex_lock(&list_lock);

605 606 607 608 609
	__v4l2_async_notifier_unregister(sd->subdev_notifier);
	v4l2_async_notifier_cleanup(sd->subdev_notifier);
	kfree(sd->subdev_notifier);
	sd->subdev_notifier = NULL;

610 611
	if (sd->asd) {
		struct v4l2_async_notifier *notifier = sd->notifier;
612

613 614
		list_add(&sd->asd->list, &notifier->waiting);

615
		v4l2_async_notifier_call_unbind(notifier, sd, sd->asd);
616
	}
617

618 619
	v4l2_async_cleanup(sd);

620 621 622
	mutex_unlock(&list_lock);
}
EXPORT_SYMBOL(v4l2_async_unregister_subdev);