1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Media Controller ancillary functions
8 * Copyright (C) 2006-2010 Nokia Corporation
9 * Copyright (c) 2016 Intel Corporation.
12 #include <linux/module.h>
13 #include <linux/pci.h>
14 #include <linux/usb.h>
15 #include <media/media-device.h>
16 #include <media/media-entity.h>
17 #include <media/v4l2-fh.h>
18 #include <media/v4l2-mc.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/videobuf2-core.h>
22 int v4l2_mc_create_media_graph(struct media_device *mdev)
25 struct media_entity *entity;
26 struct media_entity *if_vid = NULL, *if_aud = NULL;
27 struct media_entity *tuner = NULL, *decoder = NULL;
28 struct media_entity *io_v4l = NULL, *io_vbi = NULL, *io_swradio = NULL;
29 bool is_webcam = false;
31 int ret, pad_sink, pad_source;
36 media_device_for_each_entity(entity, mdev) {
37 switch (entity->function) {
38 case MEDIA_ENT_F_IF_VID_DECODER:
41 case MEDIA_ENT_F_IF_AUD_DECODER:
44 case MEDIA_ENT_F_TUNER:
47 case MEDIA_ENT_F_ATV_DECODER:
50 case MEDIA_ENT_F_IO_V4L:
53 case MEDIA_ENT_F_IO_VBI:
56 case MEDIA_ENT_F_IO_SWRADIO:
59 case MEDIA_ENT_F_CAM_SENSOR:
65 /* It should have at least one I/O entity */
66 if (!io_v4l && !io_vbi && !io_swradio) {
67 dev_warn(mdev->dev, "Didn't find any I/O entity\n");
72 * Here, webcams are modelled on a very simple way: the sensor is
73 * connected directly to the I/O entity. All dirty details, like
74 * scaler and crop HW are hidden. While such mapping is not enough
75 * for mc-centric hardware, it is enough for v4l2 interface centric
76 * PC-consumer's hardware.
80 dev_warn(mdev->dev, "Didn't find a MEDIA_ENT_F_IO_V4L\n");
84 media_device_for_each_entity(entity, mdev) {
85 if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
87 ret = media_create_pad_link(entity, 0,
89 MEDIA_LNK_FL_ENABLED);
91 dev_warn(mdev->dev, "Failed to create a sensor link\n");
99 /* The device isn't a webcam. So, it should have a decoder */
101 dev_warn(mdev->dev, "Decoder not found\n");
105 /* Link the tuner and IF video output pads */
108 pad_source = media_get_pad_index(tuner, false,
110 pad_sink = media_get_pad_index(if_vid, true,
112 if (pad_source < 0 || pad_sink < 0) {
113 dev_warn(mdev->dev, "Couldn't get tuner and/or PLL pad(s): (%d, %d)\n",
114 pad_source, pad_sink);
117 ret = media_create_pad_link(tuner, pad_source,
119 MEDIA_LNK_FL_ENABLED);
121 dev_warn(mdev->dev, "Couldn't create tuner->PLL link)\n");
125 pad_source = media_get_pad_index(if_vid, false,
127 pad_sink = media_get_pad_index(decoder, true,
129 if (pad_source < 0 || pad_sink < 0) {
130 dev_warn(mdev->dev, "get decoder and/or PLL pad(s): (%d, %d)\n",
131 pad_source, pad_sink);
134 ret = media_create_pad_link(if_vid, pad_source,
136 MEDIA_LNK_FL_ENABLED);
138 dev_warn(mdev->dev, "couldn't link PLL to decoder\n");
142 pad_source = media_get_pad_index(tuner, false,
144 pad_sink = media_get_pad_index(decoder, true,
146 if (pad_source < 0 || pad_sink < 0) {
147 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s): (%d, %d)\n",
148 pad_source, pad_sink);
151 ret = media_create_pad_link(tuner, pad_source,
153 MEDIA_LNK_FL_ENABLED);
159 pad_source = media_get_pad_index(tuner, false,
161 pad_sink = media_get_pad_index(if_aud, true,
163 if (pad_source < 0 || pad_sink < 0) {
164 dev_warn(mdev->dev, "couldn't get tuner and/or decoder pad(s) for audio: (%d, %d)\n",
165 pad_source, pad_sink);
168 ret = media_create_pad_link(tuner, pad_source,
170 MEDIA_LNK_FL_ENABLED);
172 dev_warn(mdev->dev, "couldn't link tuner->audio PLL\n");
181 /* Create demod to V4L, VBI and SDR radio links */
183 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
184 if (pad_source < 0) {
185 dev_warn(mdev->dev, "couldn't get decoder output pad for V4L I/O\n");
188 ret = media_create_pad_link(decoder, pad_source,
190 MEDIA_LNK_FL_ENABLED);
192 dev_warn(mdev->dev, "couldn't link decoder output to V4L I/O\n");
198 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
199 if (pad_source < 0) {
200 dev_warn(mdev->dev, "couldn't get decoder output pad for SDR\n");
203 ret = media_create_pad_link(decoder, pad_source,
205 MEDIA_LNK_FL_ENABLED);
207 dev_warn(mdev->dev, "couldn't link decoder output to SDR\n");
213 pad_source = media_get_pad_index(decoder, false, PAD_SIGNAL_DV);
214 if (pad_source < 0) {
215 dev_warn(mdev->dev, "couldn't get decoder output pad for VBI\n");
218 ret = media_create_pad_link(decoder, pad_source,
220 MEDIA_LNK_FL_ENABLED);
222 dev_warn(mdev->dev, "couldn't link decoder output to VBI\n");
227 /* Create links for the media connectors */
228 flags = MEDIA_LNK_FL_ENABLED;
229 media_device_for_each_entity(entity, mdev) {
230 switch (entity->function) {
231 case MEDIA_ENT_F_CONN_RF:
234 pad_sink = media_get_pad_index(tuner, true,
237 dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
240 ret = media_create_pad_link(entity, 0, tuner,
244 case MEDIA_ENT_F_CONN_SVIDEO:
245 case MEDIA_ENT_F_CONN_COMPOSITE:
246 pad_sink = media_get_pad_index(decoder, true,
249 dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
252 ret = media_create_pad_link(entity, 0, decoder,
267 EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
269 int v4l_enable_media_source(struct video_device *vdev)
271 struct media_device *mdev = vdev->entity.graph_obj.mdev;
277 mutex_lock(&mdev->graph_mutex);
278 if (!mdev->enable_source)
280 err = mdev->enable_source(&vdev->entity, &vdev->pipe);
284 mutex_unlock(&mdev->graph_mutex);
287 EXPORT_SYMBOL_GPL(v4l_enable_media_source);
289 void v4l_disable_media_source(struct video_device *vdev)
291 struct media_device *mdev = vdev->entity.graph_obj.mdev;
294 mutex_lock(&mdev->graph_mutex);
295 if (mdev->disable_source)
296 mdev->disable_source(&vdev->entity);
297 mutex_unlock(&mdev->graph_mutex);
300 EXPORT_SYMBOL_GPL(v4l_disable_media_source);
302 int v4l_vb2q_enable_media_source(struct vb2_queue *q)
304 struct v4l2_fh *fh = q->owner;
307 return v4l_enable_media_source(fh->vdev);
310 EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
312 /* -----------------------------------------------------------------------------
313 * Pipeline power management
315 * Entities must be powered up when part of a pipeline that contains at least
316 * one open video device node.
318 * To achieve this use the entity use_count field to track the number of users.
319 * For entities corresponding to video device nodes the use_count field stores
320 * the users count of the node. For entities corresponding to subdevs the
321 * use_count field stores the total number of users of all video device nodes
324 * The v4l2_pipeline_pm_use() function must be called in the open() and
325 * close() handlers of video device nodes. It increments or decrements the use
326 * count of all subdev entities in the pipeline.
328 * To react to link management on powered pipelines, the link setup notification
329 * callback updates the use count of all entities in the source and sink sides
334 * pipeline_pm_use_count - Count the number of users of a pipeline
335 * @entity: The entity
337 * Return the total number of users of all video device nodes in the pipeline.
339 static int pipeline_pm_use_count(struct media_entity *entity,
340 struct media_graph *graph)
344 media_graph_walk_start(graph, entity);
346 while ((entity = media_graph_walk_next(graph))) {
347 if (is_media_entity_v4l2_video_device(entity))
348 use += entity->use_count;
355 * pipeline_pm_power_one - Apply power change to an entity
356 * @entity: The entity
357 * @change: Use count change
359 * Change the entity use count by @change. If the entity is a subdev update its
360 * power state by calling the core::s_power operation when the use count goes
361 * from 0 to != 0 or from != 0 to 0.
363 * Return 0 on success or a negative error code on failure.
365 static int pipeline_pm_power_one(struct media_entity *entity, int change)
367 struct v4l2_subdev *subdev;
370 subdev = is_media_entity_v4l2_subdev(entity)
371 ? media_entity_to_v4l2_subdev(entity) : NULL;
373 if (entity->use_count == 0 && change > 0 && subdev != NULL) {
374 ret = v4l2_subdev_call(subdev, core, s_power, 1);
375 if (ret < 0 && ret != -ENOIOCTLCMD)
379 entity->use_count += change;
380 WARN_ON(entity->use_count < 0);
382 if (entity->use_count == 0 && change < 0 && subdev != NULL)
383 v4l2_subdev_call(subdev, core, s_power, 0);
389 * pipeline_pm_power - Apply power change to all entities in a pipeline
390 * @entity: The entity
391 * @change: Use count change
393 * Walk the pipeline to update the use count and the power state of all non-node
396 * Return 0 on success or a negative error code on failure.
398 static int pipeline_pm_power(struct media_entity *entity, int change,
399 struct media_graph *graph)
401 struct media_entity *first = entity;
407 media_graph_walk_start(graph, entity);
409 while (!ret && (entity = media_graph_walk_next(graph)))
410 if (is_media_entity_v4l2_subdev(entity))
411 ret = pipeline_pm_power_one(entity, change);
416 media_graph_walk_start(graph, first);
418 while ((first = media_graph_walk_next(graph))
420 if (is_media_entity_v4l2_subdev(first))
421 pipeline_pm_power_one(first, -change);
426 int v4l2_pipeline_pm_use(struct media_entity *entity, int use)
428 struct media_device *mdev = entity->graph_obj.mdev;
429 int change = use ? 1 : -1;
432 mutex_lock(&mdev->graph_mutex);
434 /* Apply use count to node. */
435 entity->use_count += change;
436 WARN_ON(entity->use_count < 0);
438 /* Apply power change to connected non-nodes. */
439 ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
441 entity->use_count -= change;
443 mutex_unlock(&mdev->graph_mutex);
447 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_use);
449 int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
450 unsigned int notification)
452 struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk;
453 struct media_entity *source = link->source->entity;
454 struct media_entity *sink = link->sink->entity;
459 source_use = pipeline_pm_use_count(source, graph);
460 sink_use = pipeline_pm_use_count(sink, graph);
462 if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
463 !(flags & MEDIA_LNK_FL_ENABLED)) {
464 /* Powering off entities is assumed to never fail. */
465 pipeline_pm_power(source, -sink_use, graph);
466 pipeline_pm_power(sink, -source_use, graph);
470 if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
471 (flags & MEDIA_LNK_FL_ENABLED)) {
473 ret = pipeline_pm_power(source, sink_use, graph);
477 ret = pipeline_pm_power(sink, source_use, graph);
479 pipeline_pm_power(source, -sink_use, graph);
484 EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);