media-entity.c 22.7 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
/*
 * Media entity
 *
 * Copyright (C) 2010 Nokia Corporation
 *
 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
 *	     Sakari Ailus <sakari.ailus@iki.fi>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

23
#include <linux/bitmap.h>
24 25 26
#include <linux/module.h>
#include <linux/slab.h>
#include <media/media-entity.h>
27
#include <media/media-device.h>
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 *  dev_dbg_obj - Prints in debug mode a change on some object
 *
 * @event_name:	Name of the event to report. Could be __func__
 * @gobj:	Pointer to the object
 *
 * Enabled only if DEBUG or CONFIG_DYNAMIC_DEBUG. Otherwise, it
 * won't produce any code.
 */
static inline const char *gobj_type(enum media_gobj_type type)
{
	switch (type) {
	case MEDIA_GRAPH_ENTITY:
		return "entity";
	case MEDIA_GRAPH_PAD:
		return "pad";
	case MEDIA_GRAPH_LINK:
		return "link";
47 48
	case MEDIA_GRAPH_INTF_DEVNODE:
		return "intf-devnode";
49 50 51 52 53
	default:
		return "unknown";
	}
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
static inline const char *intf_type(struct media_interface *intf)
{
	switch (intf->type) {
	case MEDIA_INTF_T_DVB_FE:
		return "frontend";
	case MEDIA_INTF_T_DVB_DEMUX:
		return "demux";
	case MEDIA_INTF_T_DVB_DVR:
		return "DVR";
	case MEDIA_INTF_T_DVB_CA:
		return  "CA";
	case MEDIA_INTF_T_DVB_NET:
		return "dvbnet";
	case MEDIA_INTF_T_V4L_VIDEO:
		return "video";
	case MEDIA_INTF_T_V4L_VBI:
		return "vbi";
	case MEDIA_INTF_T_V4L_RADIO:
		return "radio";
	case MEDIA_INTF_T_V4L_SUBDEV:
		return "v4l2-subdev";
	case MEDIA_INTF_T_V4L_SWRADIO:
		return "swradio";
	default:
		return "unknown-intf";
	}
};

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
static void dev_dbg_obj(const char *event_name,  struct media_gobj *gobj)
{
#if defined(DEBUG) || defined (CONFIG_DYNAMIC_DEBUG)
	switch (media_type(gobj)) {
	case MEDIA_GRAPH_ENTITY:
		dev_dbg(gobj->mdev->dev,
			"%s: id 0x%08x entity#%d: '%s'\n",
			event_name, gobj->id, media_localid(gobj),
			gobj_to_entity(gobj)->name);
		break;
	case MEDIA_GRAPH_LINK:
	{
		struct media_link *link = gobj_to_link(gobj);

		dev_dbg(gobj->mdev->dev,
97
			"%s: id 0x%08x link#%d: %s#%d ==> %s#%d\n",
98 99
			event_name, gobj->id, media_localid(gobj),

100 101
			gobj_type(media_type(link->gobj0)),
			media_localid(link->gobj0),
102

103 104
			gobj_type(media_type(link->gobj1)),
			media_localid(link->gobj1));
105 106 107 108 109 110 111 112 113 114
		break;
	}
	case MEDIA_GRAPH_PAD:
	{
		struct media_pad *pad = gobj_to_pad(gobj);

		dev_dbg(gobj->mdev->dev,
			"%s: id 0x%08x pad#%d: '%s':%d\n",
			event_name, gobj->id, media_localid(gobj),
			pad->entity->name, pad->index);
115 116 117 118 119 120 121 122 123 124 125 126 127
		break;
	}
	case MEDIA_GRAPH_INTF_DEVNODE:
	{
		struct media_interface *intf = gobj_to_intf(gobj);
		struct media_intf_devnode *devnode = intf_to_devnode(intf);

		dev_dbg(gobj->mdev->dev,
			"%s: id 0x%08x intf_devnode#%d: %s - major: %d, minor: %d\n",
			event_name, gobj->id, media_localid(gobj),
			intf_type(intf),
			devnode->major, devnode->minor);
		break;
128 129 130 131 132
	}
	}
#endif
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 *  media_gobj_init - Initialize a graph object
 *
 * @mdev:	Pointer to the media_device that contains the object
 * @type:	Type of the object
 * @gobj:	Pointer to the object
 *
 * This routine initializes the embedded struct media_gobj inside a
 * media graph object. It is called automatically if media_*_create()
 * calls are used. However, if the object (entity, link, pad, interface)
 * is embedded on some other object, this function should be called before
 * registering the object at the media controller.
 */
void media_gobj_init(struct media_device *mdev,
			   enum media_gobj_type type,
			   struct media_gobj *gobj)
{
150 151
	BUG_ON(!mdev);

152 153
	gobj->mdev = mdev;

154 155 156 157 158
	/* Create a per-type unique object ID */
	switch (type) {
	case MEDIA_GRAPH_ENTITY:
		gobj->id = media_gobj_gen_id(type, ++mdev->entity_id);
		break;
159 160 161
	case MEDIA_GRAPH_PAD:
		gobj->id = media_gobj_gen_id(type, ++mdev->pad_id);
		break;
162 163 164
	case MEDIA_GRAPH_LINK:
		gobj->id = media_gobj_gen_id(type, ++mdev->link_id);
		break;
165 166 167
	case MEDIA_GRAPH_INTF_DEVNODE:
		gobj->id = media_gobj_gen_id(type, ++mdev->intf_devnode_id);
		break;
168
	}
169
	dev_dbg_obj(__func__, gobj);
170 171 172 173 174 175 176 177 178 179 180
}

/**
 *  media_gobj_remove - Stop using a graph object on a media device
 *
 * @graph_obj:	Pointer to the object
 *
 * This should be called at media_device_unregister_*() routines
 */
void media_gobj_remove(struct media_gobj *gobj)
{
181
	dev_dbg_obj(__func__, gobj);
182 183
}

184 185 186 187 188 189 190 191 192
/**
 * media_entity_init - Initialize a media entity
 *
 * @num_pads: Total number of sink and source pads.
 * @pads: Array of 'num_pads' pads.
 *
 * The total number of pads is an intrinsic property of entities known by the
 * entity driver, while the total number of links depends on hardware design
 * and is an extrinsic property unknown to the entity driver. However, in most
193 194
 * use cases the number of links can safely be assumed to be equal to or
 * larger than the number of pads.
195
 *
196 197
 * For those reasons the links array can be preallocated based on the number
 * of pads and will be reallocated later if extra links need to be created.
198 199
 *
 * This function allocates a links array with enough space to hold at least
200 201
 * 'num_pads' elements. The media_entity::max_links field will be set to the
 * number of allocated elements.
202 203 204 205 206 207
 *
 * The pads array is managed by the entity driver and passed to
 * media_entity_init() where its pointer will be stored in the entity structure.
 */
int
media_entity_init(struct media_entity *entity, u16 num_pads,
208
		  struct media_pad *pads)
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
{
	unsigned int i;

	entity->group_id = 0;
	entity->num_links = 0;
	entity->num_backlinks = 0;
	entity->num_pads = num_pads;
	entity->pads = pads;

	for (i = 0; i < num_pads; i++) {
		pads[i].entity = entity;
		pads[i].index = i;
	}

	return 0;
}
EXPORT_SYMBOL_GPL(media_entity_init);

void
media_entity_cleanup(struct media_entity *entity)
{
230 231 232 233 234 235 236
	struct media_link *link, *tmp;

	list_for_each_entry_safe(link, tmp, &entity->links, list) {
		media_gobj_remove(&link->graph_obj);
		list_del(&link->list);
		kfree(link);
	}
237 238 239
}
EXPORT_SYMBOL_GPL(media_entity_cleanup);

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/* -----------------------------------------------------------------------------
 * Graph traversal
 */

static struct media_entity *
media_entity_other(struct media_entity *entity, struct media_link *link)
{
	if (link->source->entity == entity)
		return link->sink->entity;
	else
		return link->source->entity;
}

/* push an entity to traversal stack */
static void stack_push(struct media_entity_graph *graph,
		       struct media_entity *entity)
{
	if (graph->top == MEDIA_ENTITY_ENUM_MAX_DEPTH - 1) {
		WARN_ON(1);
		return;
	}
	graph->top++;
262
	graph->stack[graph->top].link = (&entity->links)->next;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	graph->stack[graph->top].entity = entity;
}

static struct media_entity *stack_pop(struct media_entity_graph *graph)
{
	struct media_entity *entity;

	entity = graph->stack[graph->top].entity;
	graph->top--;

	return entity;
}

#define link_top(en)	((en)->stack[(en)->top].link)
#define stack_top(en)	((en)->stack[(en)->top].entity)

/**
 * media_entity_graph_walk_start - Start walking the media graph at a given entity
 * @graph: Media graph structure that will be used to walk the graph
 * @entity: Starting entity
 *
 * This function initializes the graph traversal structure to walk the entities
 * graph starting at the given entity. The traversal structure must not be
 * modified by the caller during graph traversal. When done the structure can
 * safely be freed.
 */
void media_entity_graph_walk_start(struct media_entity_graph *graph,
				   struct media_entity *entity)
{
	graph->top = 0;
	graph->stack[graph->top].entity = NULL;
294 295
	bitmap_zero(graph->entities, MEDIA_ENTITY_ENUM_MAX_ID);

296
	if (WARN_ON(media_entity_id(entity) >= MEDIA_ENTITY_ENUM_MAX_ID))
297 298
		return;

299
	__set_bit(media_entity_id(entity), graph->entities);
300 301 302 303
	stack_push(graph, entity);
}
EXPORT_SYMBOL_GPL(media_entity_graph_walk_start);

304

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
/**
 * media_entity_graph_walk_next - Get the next entity in the graph
 * @graph: Media graph structure
 *
 * Perform a depth-first traversal of the given media entities graph.
 *
 * The graph structure must have been previously initialized with a call to
 * media_entity_graph_walk_start().
 *
 * Return the next entity in the graph or NULL if the whole graph have been
 * traversed.
 */
struct media_entity *
media_entity_graph_walk_next(struct media_entity_graph *graph)
{
	if (stack_top(graph) == NULL)
		return NULL;

	/*
	 * Depth first search. Push entity to stack and continue from
	 * top of the stack until no more entities on the level can be
	 * found.
	 */
328
	while (link_top(graph) != &(stack_top(graph)->links)) {
329
		struct media_entity *entity = stack_top(graph);
330
		struct media_link *link;
331 332
		struct media_entity *next;

333 334
		link = list_entry(link_top(graph), typeof(*link), list);

335 336
		/* The link is not enabled so we do not follow. */
		if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
337
			link_top(graph) = link_top(graph)->next;
338 339 340 341 342
			continue;
		}

		/* Get the entity in the other end of the link . */
		next = media_entity_other(entity, link);
343
		if (WARN_ON(media_entity_id(next) >= MEDIA_ENTITY_ENUM_MAX_ID))
344
			return NULL;
345

346
		/* Has the entity already been visited? */
347
		if (__test_and_set_bit(media_entity_id(next), graph->entities)) {
348
			link_top(graph) = link_top(graph)->next;
349 350 351 352
			continue;
		}

		/* Push the new entity to stack and start over. */
353
		link_top(graph) = link_top(graph)->next;
354 355 356 357 358 359 360
		stack_push(graph, next);
	}

	return stack_pop(graph);
}
EXPORT_SYMBOL_GPL(media_entity_graph_walk_next);

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
/* -----------------------------------------------------------------------------
 * Pipeline management
 */

/**
 * media_entity_pipeline_start - Mark a pipeline as streaming
 * @entity: Starting entity
 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
 *
 * Mark all entities connected to a given entity through enabled links, either
 * directly or indirectly, as streaming. The given pipeline object is assigned to
 * every entity in the pipeline and stored in the media_entity pipe field.
 *
 * Calls to this function can be nested, in which case the same number of
 * media_entity_pipeline_stop() calls will be required to stop streaming. The
 * pipeline pointer must be identical for all nested calls to
 * media_entity_pipeline_start().
 */
379 380
__must_check int media_entity_pipeline_start(struct media_entity *entity,
					     struct media_pipeline *pipe)
381
{
382
	struct media_device *mdev = entity->graph_obj.mdev;
383
	struct media_entity_graph graph;
384
	struct media_entity *entity_err = entity;
385
	struct media_link *link;
386
	int ret;
387 388 389 390 391 392

	mutex_lock(&mdev->graph_mutex);

	media_entity_graph_walk_start(&graph, entity);

	while ((entity = media_entity_graph_walk_next(&graph))) {
393 394
		DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
		DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
395

396 397 398
		entity->stream_count++;
		WARN_ON(entity->pipe && entity->pipe != pipe);
		entity->pipe = pipe;
399 400 401 402 403 404 405 406

		/* Already streaming --- no need to check. */
		if (entity->stream_count > 1)
			continue;

		if (!entity->ops || !entity->ops->link_validate)
			continue;

407 408 409
		bitmap_zero(active, entity->num_pads);
		bitmap_fill(has_no_links, entity->num_pads);

410
		list_for_each_entry(link, &entity->links, list) {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
			struct media_pad *pad = link->sink->entity == entity
						? link->sink : link->source;

			/* Mark that a pad is connected by a link. */
			bitmap_clear(has_no_links, pad->index, 1);

			/*
			 * Pads that either do not need to connect or
			 * are connected through an enabled link are
			 * fine.
			 */
			if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
			    link->flags & MEDIA_LNK_FL_ENABLED)
				bitmap_set(active, pad->index, 1);

			/*
			 * Link validation will only take place for
			 * sink ends of the link that are enabled.
			 */
			if (link->sink != pad ||
			    !(link->flags & MEDIA_LNK_FL_ENABLED))
432 433 434
				continue;

			ret = entity->ops->link_validate(link);
435
			if (ret < 0 && ret != -ENOIOCTLCMD) {
436
				dev_dbg(entity->graph_obj.mdev->dev,
437
					"link validation failed for \"%s\":%u -> \"%s\":%u, error %d\n",
438 439 440
					link->source->entity->name,
					link->source->index,
					entity->name, link->sink->index, ret);
441
				goto error;
442
			}
443
		}
444 445 446 447 448 449

		/* Either no links or validated links are fine. */
		bitmap_or(active, active, has_no_links, entity->num_pads);

		if (!bitmap_full(active, entity->num_pads)) {
			ret = -EPIPE;
450
			dev_dbg(entity->graph_obj.mdev->dev,
451 452
				"\"%s\":%u must be connected by an enabled link\n",
				entity->name,
453 454
				(unsigned)find_first_zero_bit(
					active, entity->num_pads));
455 456
			goto error;
		}
457 458 459
	}

	mutex_unlock(&mdev->graph_mutex);
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

	return 0;

error:
	/*
	 * Link validation on graph failed. We revert what we did and
	 * return the error.
	 */
	media_entity_graph_walk_start(&graph, entity_err);

	while ((entity_err = media_entity_graph_walk_next(&graph))) {
		entity_err->stream_count--;
		if (entity_err->stream_count == 0)
			entity_err->pipe = NULL;

		/*
		 * We haven't increased stream_count further than this
		 * so we quit here.
		 */
		if (entity_err == entity)
			break;
	}

	mutex_unlock(&mdev->graph_mutex);

	return ret;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
}
EXPORT_SYMBOL_GPL(media_entity_pipeline_start);

/**
 * media_entity_pipeline_stop - Mark a pipeline as not streaming
 * @entity: Starting entity
 *
 * Mark all entities connected to a given entity through enabled links, either
 * directly or indirectly, as not streaming. The media_entity pipe field is
 * reset to NULL.
 *
 * If multiple calls to media_entity_pipeline_start() have been made, the same
 * number of calls to this function are required to mark the pipeline as not
 * streaming.
 */
void media_entity_pipeline_stop(struct media_entity *entity)
{
503
	struct media_device *mdev = entity->graph_obj.mdev;
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
	struct media_entity_graph graph;

	mutex_lock(&mdev->graph_mutex);

	media_entity_graph_walk_start(&graph, entity);

	while ((entity = media_entity_graph_walk_next(&graph))) {
		entity->stream_count--;
		if (entity->stream_count == 0)
			entity->pipe = NULL;
	}

	mutex_unlock(&mdev->graph_mutex);
}
EXPORT_SYMBOL_GPL(media_entity_pipeline_stop);

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
/* -----------------------------------------------------------------------------
 * Module use count
 */

/*
 * media_entity_get - Get a reference to the parent module
 * @entity: The entity
 *
 * Get a reference to the parent media device module.
 *
 * The function will return immediately if @entity is NULL.
 *
 * Return a pointer to the entity on success or NULL on failure.
 */
struct media_entity *media_entity_get(struct media_entity *entity)
{
	if (entity == NULL)
		return NULL;

539 540
	if (entity->graph_obj.mdev->dev &&
	    !try_module_get(entity->graph_obj.mdev->dev->driver->owner))
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
		return NULL;

	return entity;
}
EXPORT_SYMBOL_GPL(media_entity_get);

/*
 * media_entity_put - Release the reference to the parent module
 * @entity: The entity
 *
 * Release the reference count acquired by media_entity_get().
 *
 * The function will return immediately if @entity is NULL.
 */
void media_entity_put(struct media_entity *entity)
{
	if (entity == NULL)
		return;

560 561
	if (entity->graph_obj.mdev->dev)
		module_put(entity->graph_obj.mdev->dev->driver->owner);
562 563 564
}
EXPORT_SYMBOL_GPL(media_entity_put);

565 566 567 568
/* -----------------------------------------------------------------------------
 * Links management
 */

569
static struct media_link *media_add_link(struct list_head *head)
570
{
571
	struct media_link *link;
572

573 574 575
	link = kzalloc(sizeof(*link), GFP_KERNEL);
	if (link == NULL)
		return NULL;
576

577
	list_add_tail(&link->list, head);
578

579
	return link;
580 581
}

582 583 584
static void __media_entity_remove_link(struct media_entity *entity,
				       struct media_link *link);

585
int
586
media_create_pad_link(struct media_entity *source, u16 source_pad,
587 588 589 590 591 592 593 594 595
			 struct media_entity *sink, u16 sink_pad, u32 flags)
{
	struct media_link *link;
	struct media_link *backlink;

	BUG_ON(source == NULL || sink == NULL);
	BUG_ON(source_pad >= source->num_pads);
	BUG_ON(sink_pad >= sink->num_pads);

596
	link = media_add_link(&source->links);
597 598 599 600 601 602 603
	if (link == NULL)
		return -ENOMEM;

	link->source = &source->pads[source_pad];
	link->sink = &sink->pads[sink_pad];
	link->flags = flags;

604
	/* Initialize graph object embedded at the new link */
605 606
	media_gobj_init(source->graph_obj.mdev, MEDIA_GRAPH_LINK,
			&link->graph_obj);
607

608 609 610
	/* Create the backlink. Backlinks are used to help graph traversal and
	 * are not reported to userspace.
	 */
611
	backlink = media_add_link(&sink->links);
612
	if (backlink == NULL) {
613
		__media_entity_remove_link(source, link);
614 615 616 617 618 619 620
		return -ENOMEM;
	}

	backlink->source = &source->pads[source_pad];
	backlink->sink = &sink->pads[sink_pad];
	backlink->flags = flags;

621
	/* Initialize graph object embedded at the new link */
622 623
	media_gobj_init(sink->graph_obj.mdev, MEDIA_GRAPH_LINK,
			&backlink->graph_obj);
624

625 626 627 628
	link->reverse = backlink;
	backlink->reverse = link;

	sink->num_backlinks++;
629 630
	sink->num_links++;
	source->num_links++;
631 632 633

	return 0;
}
634
EXPORT_SYMBOL_GPL(media_create_pad_link);
L
Laurent Pinchart 已提交
635

636 637
static void __media_entity_remove_link(struct media_entity *entity,
				       struct media_link *link)
638
{
639 640 641 642 643 644 645 646
	struct media_link *rlink, *tmp;
	struct media_entity *remote;
	unsigned int r = 0;

	if (link->source->entity == entity)
		remote = link->sink->entity;
	else
		remote = link->source->entity;
647

648 649 650 651 652
	list_for_each_entry_safe(rlink, tmp, &remote->links, list) {
		if (rlink != link->reverse) {
			r++;
			continue;
		}
653 654

		if (link->source->entity == entity)
655
			remote->num_backlinks--;
656

657 658
		if (--remote->num_links == 0)
			break;
659

660 661 662 663 664 665 666
		/* Remove the remote link */
		list_del(&rlink->list);
		kfree(rlink);
	}
	list_del(&link->list);
	kfree(link);
}
667

668 669 670
void __media_entity_remove_links(struct media_entity *entity)
{
	struct media_link *link, *tmp;
671

672 673
	list_for_each_entry_safe(link, tmp, &entity->links, list)
		__media_entity_remove_link(entity, link);
674 675 676 677 678 679 680 681 682

	entity->num_links = 0;
	entity->num_backlinks = 0;
}
EXPORT_SYMBOL_GPL(__media_entity_remove_links);

void media_entity_remove_links(struct media_entity *entity)
{
	/* Do nothing if the entity is not registered. */
683
	if (entity->graph_obj.mdev == NULL)
684 685
		return;

686
	mutex_lock(&entity->graph_obj.mdev->graph_mutex);
687
	__media_entity_remove_links(entity);
688
	mutex_unlock(&entity->graph_obj.mdev->graph_mutex);
689 690 691
}
EXPORT_SYMBOL_GPL(media_entity_remove_links);

L
Laurent Pinchart 已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
static int __media_entity_setup_link_notify(struct media_link *link, u32 flags)
{
	int ret;

	/* Notify both entities. */
	ret = media_entity_call(link->source->entity, link_setup,
				link->source, link->sink, flags);
	if (ret < 0 && ret != -ENOIOCTLCMD)
		return ret;

	ret = media_entity_call(link->sink->entity, link_setup,
				link->sink, link->source, flags);
	if (ret < 0 && ret != -ENOIOCTLCMD) {
		media_entity_call(link->source->entity, link_setup,
				  link->source, link->sink, link->flags);
		return ret;
	}

710
	link->flags = flags;
L
Laurent Pinchart 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
	link->reverse->flags = link->flags;

	return 0;
}

/**
 * __media_entity_setup_link - Configure a media link
 * @link: The link being configured
 * @flags: Link configuration flags
 *
 * The bulk of link setup is handled by the two entities connected through the
 * link. This function notifies both entities of the link configuration change.
 *
 * If the link is immutable or if the current and new configuration are
 * identical, return immediately.
 *
 * The user is expected to hold link->source->parent->mutex. If not,
 * media_entity_setup_link() should be used instead.
 */
int __media_entity_setup_link(struct media_link *link, u32 flags)
{
732
	const u32 mask = MEDIA_LNK_FL_ENABLED;
L
Laurent Pinchart 已提交
733 734 735 736 737 738 739
	struct media_device *mdev;
	struct media_entity *source, *sink;
	int ret = -EBUSY;

	if (link == NULL)
		return -EINVAL;

740 741 742 743
	/* The non-modifiable link flags must not be modified. */
	if ((link->flags & ~mask) != (flags & ~mask))
		return -EINVAL;

L
Laurent Pinchart 已提交
744 745 746 747 748 749 750 751 752
	if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
		return link->flags == flags ? 0 : -EINVAL;

	if (link->flags == flags)
		return 0;

	source = link->source->entity;
	sink = link->sink->entity;

753 754 755 756
	if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
	    (source->stream_count || sink->stream_count))
		return -EBUSY;

757
	mdev = source->graph_obj.mdev;
L
Laurent Pinchart 已提交
758

759 760 761
	if (mdev->link_notify) {
		ret = mdev->link_notify(link, flags,
					MEDIA_DEV_NOTIFY_PRE_LINK_CH);
L
Laurent Pinchart 已提交
762 763 764 765 766 767
		if (ret < 0)
			return ret;
	}

	ret = __media_entity_setup_link_notify(link, flags);

768 769
	if (mdev->link_notify)
		mdev->link_notify(link, flags, MEDIA_DEV_NOTIFY_POST_LINK_CH);
L
Laurent Pinchart 已提交
770 771 772 773 774 775 776 777

	return ret;
}

int media_entity_setup_link(struct media_link *link, u32 flags)
{
	int ret;

778
	mutex_lock(&link->source->entity->graph_obj.mdev->graph_mutex);
L
Laurent Pinchart 已提交
779
	ret = __media_entity_setup_link(link, flags);
780
	mutex_unlock(&link->source->entity->graph_obj.mdev->graph_mutex);
L
Laurent Pinchart 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

	return ret;
}
EXPORT_SYMBOL_GPL(media_entity_setup_link);

/**
 * media_entity_find_link - Find a link between two pads
 * @source: Source pad
 * @sink: Sink pad
 *
 * Return a pointer to the link between the two entities. If no such link
 * exists, return NULL.
 */
struct media_link *
media_entity_find_link(struct media_pad *source, struct media_pad *sink)
{
	struct media_link *link;

799
	list_for_each_entry(link, &source->entity->links, list) {
L
Laurent Pinchart 已提交
800 801 802 803 804 805 806 807 808 809 810 811
		if (link->source->entity == source->entity &&
		    link->source->index == source->index &&
		    link->sink->entity == sink->entity &&
		    link->sink->index == sink->index)
			return link;
	}

	return NULL;
}
EXPORT_SYMBOL_GPL(media_entity_find_link);

/**
812 813
 * media_entity_remote_pad - Find the pad at the remote end of a link
 * @pad: Pad at the local end of the link
L
Laurent Pinchart 已提交
814
 *
815 816
 * Search for a remote pad connected to the given pad by iterating over all
 * links originating or terminating at that pad until an enabled link is found.
L
Laurent Pinchart 已提交
817 818 819 820
 *
 * Return a pointer to the pad at the remote end of the first found enabled
 * link, or NULL if no enabled link has been found.
 */
821
struct media_pad *media_entity_remote_pad(struct media_pad *pad)
L
Laurent Pinchart 已提交
822
{
823
	struct media_link *link;
L
Laurent Pinchart 已提交
824

825
	list_for_each_entry(link, &pad->entity->links, list) {
L
Laurent Pinchart 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838
		if (!(link->flags & MEDIA_LNK_FL_ENABLED))
			continue;

		if (link->source == pad)
			return link->sink;

		if (link->sink == pad)
			return link->source;
	}

	return NULL;

}
839
EXPORT_SYMBOL_GPL(media_entity_remote_pad);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876


/* Functions related to the media interface via device nodes */

struct media_intf_devnode *media_devnode_create(struct media_device *mdev,
						u32 type, u32 flags,
						u32 major, u32 minor,
						gfp_t gfp_flags)
{
	struct media_intf_devnode *devnode;
	struct media_interface *intf;

	devnode = kzalloc(sizeof(*devnode), gfp_flags);
	if (!devnode)
		return NULL;

	intf = &devnode->intf;

	intf->type = type;
	intf->flags = flags;

	devnode->major = major;
	devnode->minor = minor;

	media_gobj_init(mdev, MEDIA_GRAPH_INTF_DEVNODE,
		       &devnode->intf.graph_obj);

	return devnode;
}
EXPORT_SYMBOL_GPL(media_devnode_create);

void media_devnode_remove(struct media_intf_devnode *devnode)
{
	media_gobj_remove(&devnode->intf.graph_obj);
	kfree(devnode);
}
EXPORT_SYMBOL_GPL(media_devnode_remove);