1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Media Controller ancillary functions
6 * Copyright (c) 2016 Mauro Carvalho Chehab <mchehab@kernel.org>
7 * Copyright (C) 2016 Shuah Khan <shuahkh@osg.samsung.com>
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;
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
)
70 * Here, webcams are modelled on a very simple way: the sensor is
71 * connected directly to the I/O entity. All dirty details, like
72 * scaler and crop HW are hidden. While such mapping is not enough
73 * for mc-centric hardware, it is enough for v4l2 interface centric
74 * PC-consumer's hardware.
80 media_device_for_each_entity(entity
, mdev
) {
81 if (entity
->function
!= MEDIA_ENT_F_CAM_SENSOR
)
83 ret
= media_create_pad_link(entity
, 0,
85 MEDIA_LNK_FL_ENABLED
);
93 /* The device isn't a webcam. So, it should have a decoder */
97 /* Link the tuner and IF video output pads */
100 ret
= media_create_pad_link(tuner
, TUNER_PAD_OUTPUT
,
102 IF_VID_DEC_PAD_IF_INPUT
,
103 MEDIA_LNK_FL_ENABLED
);
106 ret
= media_create_pad_link(if_vid
, IF_VID_DEC_PAD_OUT
,
107 decoder
, DEMOD_PAD_IF_INPUT
,
108 MEDIA_LNK_FL_ENABLED
);
112 ret
= media_create_pad_link(tuner
, TUNER_PAD_OUTPUT
,
113 decoder
, DEMOD_PAD_IF_INPUT
,
114 MEDIA_LNK_FL_ENABLED
);
120 ret
= media_create_pad_link(tuner
, TUNER_PAD_AUD_OUT
,
122 IF_AUD_DEC_PAD_IF_INPUT
,
123 MEDIA_LNK_FL_ENABLED
);
132 /* Create demod to V4L, VBI and SDR radio links */
134 ret
= media_create_pad_link(decoder
, DEMOD_PAD_VID_OUT
,
136 MEDIA_LNK_FL_ENABLED
);
142 ret
= media_create_pad_link(decoder
, DEMOD_PAD_VID_OUT
,
144 MEDIA_LNK_FL_ENABLED
);
150 ret
= media_create_pad_link(decoder
, DEMOD_PAD_VBI_OUT
,
152 MEDIA_LNK_FL_ENABLED
);
157 /* Create links for the media connectors */
158 flags
= MEDIA_LNK_FL_ENABLED
;
159 media_device_for_each_entity(entity
, mdev
) {
160 switch (entity
->function
) {
161 case MEDIA_ENT_F_CONN_RF
:
165 ret
= media_create_pad_link(entity
, 0, tuner
,
169 case MEDIA_ENT_F_CONN_SVIDEO
:
170 case MEDIA_ENT_F_CONN_COMPOSITE
:
171 ret
= media_create_pad_link(entity
, 0, decoder
,
186 EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph
);
188 int v4l_enable_media_source(struct video_device
*vdev
)
190 struct media_device
*mdev
= vdev
->entity
.graph_obj
.mdev
;
196 mutex_lock(&mdev
->graph_mutex
);
197 if (!mdev
->enable_source
)
199 err
= mdev
->enable_source(&vdev
->entity
, &vdev
->pipe
);
203 mutex_unlock(&mdev
->graph_mutex
);
206 EXPORT_SYMBOL_GPL(v4l_enable_media_source
);
208 void v4l_disable_media_source(struct video_device
*vdev
)
210 struct media_device
*mdev
= vdev
->entity
.graph_obj
.mdev
;
213 mutex_lock(&mdev
->graph_mutex
);
214 if (mdev
->disable_source
)
215 mdev
->disable_source(&vdev
->entity
);
216 mutex_unlock(&mdev
->graph_mutex
);
219 EXPORT_SYMBOL_GPL(v4l_disable_media_source
);
221 int v4l_vb2q_enable_media_source(struct vb2_queue
*q
)
223 struct v4l2_fh
*fh
= q
->owner
;
226 return v4l_enable_media_source(fh
->vdev
);
229 EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source
);
231 /* -----------------------------------------------------------------------------
232 * Pipeline power management
234 * Entities must be powered up when part of a pipeline that contains at least
235 * one open video device node.
237 * To achieve this use the entity use_count field to track the number of users.
238 * For entities corresponding to video device nodes the use_count field stores
239 * the users count of the node. For entities corresponding to subdevs the
240 * use_count field stores the total number of users of all video device nodes
243 * The v4l2_pipeline_pm_use() function must be called in the open() and
244 * close() handlers of video device nodes. It increments or decrements the use
245 * count of all subdev entities in the pipeline.
247 * To react to link management on powered pipelines, the link setup notification
248 * callback updates the use count of all entities in the source and sink sides
253 * pipeline_pm_use_count - Count the number of users of a pipeline
254 * @entity: The entity
256 * Return the total number of users of all video device nodes in the pipeline.
258 static int pipeline_pm_use_count(struct media_entity
*entity
,
259 struct media_graph
*graph
)
263 media_graph_walk_start(graph
, entity
);
265 while ((entity
= media_graph_walk_next(graph
))) {
266 if (is_media_entity_v4l2_video_device(entity
))
267 use
+= entity
->use_count
;
274 * pipeline_pm_power_one - Apply power change to an entity
275 * @entity: The entity
276 * @change: Use count change
278 * Change the entity use count by @change. If the entity is a subdev update its
279 * power state by calling the core::s_power operation when the use count goes
280 * from 0 to != 0 or from != 0 to 0.
282 * Return 0 on success or a negative error code on failure.
284 static int pipeline_pm_power_one(struct media_entity
*entity
, int change
)
286 struct v4l2_subdev
*subdev
;
289 subdev
= is_media_entity_v4l2_subdev(entity
)
290 ? media_entity_to_v4l2_subdev(entity
) : NULL
;
292 if (entity
->use_count
== 0 && change
> 0 && subdev
!= NULL
) {
293 ret
= v4l2_subdev_call(subdev
, core
, s_power
, 1);
294 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
298 entity
->use_count
+= change
;
299 WARN_ON(entity
->use_count
< 0);
301 if (entity
->use_count
== 0 && change
< 0 && subdev
!= NULL
)
302 v4l2_subdev_call(subdev
, core
, s_power
, 0);
308 * pipeline_pm_power - Apply power change to all entities in a pipeline
309 * @entity: The entity
310 * @change: Use count change
312 * Walk the pipeline to update the use count and the power state of all non-node
315 * Return 0 on success or a negative error code on failure.
317 static int pipeline_pm_power(struct media_entity
*entity
, int change
,
318 struct media_graph
*graph
)
320 struct media_entity
*first
= entity
;
326 media_graph_walk_start(graph
, entity
);
328 while (!ret
&& (entity
= media_graph_walk_next(graph
)))
329 if (is_media_entity_v4l2_subdev(entity
))
330 ret
= pipeline_pm_power_one(entity
, change
);
335 media_graph_walk_start(graph
, first
);
337 while ((first
= media_graph_walk_next(graph
))
339 if (is_media_entity_v4l2_subdev(first
))
340 pipeline_pm_power_one(first
, -change
);
345 int v4l2_pipeline_pm_use(struct media_entity
*entity
, int use
)
347 struct media_device
*mdev
= entity
->graph_obj
.mdev
;
348 int change
= use
? 1 : -1;
351 mutex_lock(&mdev
->graph_mutex
);
353 /* Apply use count to node. */
354 entity
->use_count
+= change
;
355 WARN_ON(entity
->use_count
< 0);
357 /* Apply power change to connected non-nodes. */
358 ret
= pipeline_pm_power(entity
, change
, &mdev
->pm_count_walk
);
360 entity
->use_count
-= change
;
362 mutex_unlock(&mdev
->graph_mutex
);
366 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_use
);
368 int v4l2_pipeline_link_notify(struct media_link
*link
, u32 flags
,
369 unsigned int notification
)
371 struct media_graph
*graph
= &link
->graph_obj
.mdev
->pm_count_walk
;
372 struct media_entity
*source
= link
->source
->entity
;
373 struct media_entity
*sink
= link
->sink
->entity
;
378 source_use
= pipeline_pm_use_count(source
, graph
);
379 sink_use
= pipeline_pm_use_count(sink
, graph
);
381 if (notification
== MEDIA_DEV_NOTIFY_POST_LINK_CH
&&
382 !(flags
& MEDIA_LNK_FL_ENABLED
)) {
383 /* Powering off entities is assumed to never fail. */
384 pipeline_pm_power(source
, -sink_use
, graph
);
385 pipeline_pm_power(sink
, -source_use
, graph
);
389 if (notification
== MEDIA_DEV_NOTIFY_PRE_LINK_CH
&&
390 (flags
& MEDIA_LNK_FL_ENABLED
)) {
392 ret
= pipeline_pm_power(source
, sink_use
, graph
);
396 ret
= pipeline_pm_power(sink
, source_use
, graph
);
398 pipeline_pm_power(source
, -sink_use
, graph
);
403 EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify
);