1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Media Controller ancillary functions
5  *
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.
10  */
11 
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>
21 
22 int v4l2_mc_create_media_graph(struct media_device *mdev)
23 
24 {
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;
30 	u32 flags;
31 	int ret, pad_sink, pad_source;
32 
33 	if (!mdev)
34 		return 0;
35 
36 	media_device_for_each_entity(entity, mdev) {
37 		switch (entity->function) {
38 		case MEDIA_ENT_F_IF_VID_DECODER:
39 			if_vid = entity;
40 			break;
41 		case MEDIA_ENT_F_IF_AUD_DECODER:
42 			if_aud = entity;
43 			break;
44 		case MEDIA_ENT_F_TUNER:
45 			tuner = entity;
46 			break;
47 		case MEDIA_ENT_F_ATV_DECODER:
48 			decoder = entity;
49 			break;
50 		case MEDIA_ENT_F_IO_V4L:
51 			io_v4l = entity;
52 			break;
53 		case MEDIA_ENT_F_IO_VBI:
54 			io_vbi = entity;
55 			break;
56 		case MEDIA_ENT_F_IO_SWRADIO:
57 			io_swradio = entity;
58 			break;
59 		case MEDIA_ENT_F_CAM_SENSOR:
60 			is_webcam = true;
61 			break;
62 		}
63 	}
64 
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");
68 		return -EINVAL;
69 	}
70 
71 	/*
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.
77 	 */
78 	if (is_webcam) {
79 		if (!io_v4l) {
80 			dev_warn(mdev->dev, "Didn't find a MEDIA_ENT_F_IO_V4L\n");
81 			return -EINVAL;
82 		}
83 
84 		media_device_for_each_entity(entity, mdev) {
85 			if (entity->function != MEDIA_ENT_F_CAM_SENSOR)
86 				continue;
87 			ret = media_create_pad_link(entity, 0,
88 						    io_v4l, 0,
89 						    MEDIA_LNK_FL_ENABLED);
90 			if (ret) {
91 				dev_warn(mdev->dev, "Failed to create a sensor link\n");
92 				return ret;
93 			}
94 		}
95 		if (!decoder)
96 			return 0;
97 	}
98 
99 	/* The device isn't a webcam. So, it should have a decoder */
100 	if (!decoder) {
101 		dev_warn(mdev->dev, "Decoder not found\n");
102 		return -EINVAL;
103 	}
104 
105 	/* Link the tuner and IF video output pads */
106 	if (tuner) {
107 		if (if_vid) {
108 			pad_source = media_get_pad_index(tuner, false,
109 							 PAD_SIGNAL_ANALOG);
110 			pad_sink = media_get_pad_index(if_vid, true,
111 						       PAD_SIGNAL_ANALOG);
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);
115 				return -EINVAL;
116 			}
117 			ret = media_create_pad_link(tuner, pad_source,
118 						    if_vid, pad_sink,
119 						    MEDIA_LNK_FL_ENABLED);
120 			if (ret) {
121 				dev_warn(mdev->dev, "Couldn't create tuner->PLL link)\n");
122 				return ret;
123 			}
124 
125 			pad_source = media_get_pad_index(if_vid, false,
126 							 PAD_SIGNAL_ANALOG);
127 			pad_sink = media_get_pad_index(decoder, true,
128 						       PAD_SIGNAL_ANALOG);
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);
132 				return -EINVAL;
133 			}
134 			ret = media_create_pad_link(if_vid, pad_source,
135 						    decoder, pad_sink,
136 						    MEDIA_LNK_FL_ENABLED);
137 			if (ret) {
138 				dev_warn(mdev->dev, "couldn't link PLL to decoder\n");
139 				return ret;
140 			}
141 		} else {
142 			pad_source = media_get_pad_index(tuner, false,
143 							 PAD_SIGNAL_ANALOG);
144 			pad_sink = media_get_pad_index(decoder, true,
145 						       PAD_SIGNAL_ANALOG);
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);
149 				return -EINVAL;
150 			}
151 			ret = media_create_pad_link(tuner, pad_source,
152 						    decoder, pad_sink,
153 						    MEDIA_LNK_FL_ENABLED);
154 			if (ret)
155 				return ret;
156 		}
157 
158 		if (if_aud) {
159 			pad_source = media_get_pad_index(tuner, false,
160 							 PAD_SIGNAL_AUDIO);
161 			pad_sink = media_get_pad_index(if_aud, true,
162 						       PAD_SIGNAL_AUDIO);
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);
166 				return -EINVAL;
167 			}
168 			ret = media_create_pad_link(tuner, pad_source,
169 						    if_aud, pad_sink,
170 						    MEDIA_LNK_FL_ENABLED);
171 			if (ret) {
172 				dev_warn(mdev->dev, "couldn't link tuner->audio PLL\n");
173 				return ret;
174 			}
175 		} else {
176 			if_aud = tuner;
177 		}
178 
179 	}
180 
181 	/* Create demod to V4L, VBI and SDR radio links */
182 	if (io_v4l) {
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");
186 			return -EINVAL;
187 		}
188 		ret = media_create_pad_link(decoder, pad_source,
189 					    io_v4l, 0,
190 					    MEDIA_LNK_FL_ENABLED);
191 		if (ret) {
192 			dev_warn(mdev->dev, "couldn't link decoder output to V4L I/O\n");
193 			return ret;
194 		}
195 	}
196 
197 	if (io_swradio) {
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");
201 			return -EINVAL;
202 		}
203 		ret = media_create_pad_link(decoder, pad_source,
204 					    io_swradio, 0,
205 					    MEDIA_LNK_FL_ENABLED);
206 		if (ret) {
207 			dev_warn(mdev->dev, "couldn't link decoder output to SDR\n");
208 			return ret;
209 		}
210 	}
211 
212 	if (io_vbi) {
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");
216 			return -EINVAL;
217 		}
218 		ret = media_create_pad_link(decoder, pad_source,
219 					    io_vbi, 0,
220 					    MEDIA_LNK_FL_ENABLED);
221 		if (ret) {
222 			dev_warn(mdev->dev, "couldn't link decoder output to VBI\n");
223 			return ret;
224 		}
225 	}
226 
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:
232 			if (!tuner)
233 				continue;
234 			pad_sink = media_get_pad_index(tuner, true,
235 						       PAD_SIGNAL_ANALOG);
236 			if (pad_sink < 0) {
237 				dev_warn(mdev->dev, "couldn't get tuner analog pad sink\n");
238 				return -EINVAL;
239 			}
240 			ret = media_create_pad_link(entity, 0, tuner,
241 						    pad_sink,
242 						    flags);
243 			break;
244 		case MEDIA_ENT_F_CONN_SVIDEO:
245 		case MEDIA_ENT_F_CONN_COMPOSITE:
246 			pad_sink = media_get_pad_index(decoder, true,
247 						       PAD_SIGNAL_ANALOG);
248 			if (pad_sink < 0) {
249 				dev_warn(mdev->dev, "couldn't get decoder analog pad sink\n");
250 				return -EINVAL;
251 			}
252 			ret = media_create_pad_link(entity, 0, decoder,
253 						    pad_sink,
254 						    flags);
255 			break;
256 		default:
257 			continue;
258 		}
259 		if (ret)
260 			return ret;
261 
262 		flags = 0;
263 	}
264 
265 	return 0;
266 }
267 EXPORT_SYMBOL_GPL(v4l2_mc_create_media_graph);
268 
269 int v4l_enable_media_source(struct video_device *vdev)
270 {
271 	struct media_device *mdev = vdev->entity.graph_obj.mdev;
272 	int ret = 0, err;
273 
274 	if (!mdev)
275 		return 0;
276 
277 	mutex_lock(&mdev->graph_mutex);
278 	if (!mdev->enable_source)
279 		goto end;
280 	err = mdev->enable_source(&vdev->entity, &vdev->pipe);
281 	if (err)
282 		ret = -EBUSY;
283 end:
284 	mutex_unlock(&mdev->graph_mutex);
285 	return ret;
286 }
287 EXPORT_SYMBOL_GPL(v4l_enable_media_source);
288 
289 void v4l_disable_media_source(struct video_device *vdev)
290 {
291 	struct media_device *mdev = vdev->entity.graph_obj.mdev;
292 
293 	if (mdev) {
294 		mutex_lock(&mdev->graph_mutex);
295 		if (mdev->disable_source)
296 			mdev->disable_source(&vdev->entity);
297 		mutex_unlock(&mdev->graph_mutex);
298 	}
299 }
300 EXPORT_SYMBOL_GPL(v4l_disable_media_source);
301 
302 int v4l_vb2q_enable_media_source(struct vb2_queue *q)
303 {
304 	struct v4l2_fh *fh = q->owner;
305 
306 	if (fh && fh->vdev)
307 		return v4l_enable_media_source(fh->vdev);
308 	return 0;
309 }
310 EXPORT_SYMBOL_GPL(v4l_vb2q_enable_media_source);
311 
312 int v4l2_create_fwnode_links_to_pad(struct v4l2_subdev *src_sd,
313 				    struct media_pad *sink, u32 flags)
314 {
315 	struct fwnode_handle *endpoint;
316 
317 	if (!(sink->flags & MEDIA_PAD_FL_SINK) ||
318 	    !is_media_entity_v4l2_subdev(sink->entity))
319 		return -EINVAL;
320 
321 	fwnode_graph_for_each_endpoint(dev_fwnode(src_sd->dev), endpoint) {
322 		struct fwnode_handle *remote_ep;
323 		int src_idx, sink_idx, ret;
324 		struct media_pad *src;
325 
326 		src_idx = media_entity_get_fwnode_pad(&src_sd->entity,
327 						      endpoint,
328 						      MEDIA_PAD_FL_SOURCE);
329 		if (src_idx < 0)
330 			continue;
331 
332 		remote_ep = fwnode_graph_get_remote_endpoint(endpoint);
333 		if (!remote_ep)
334 			continue;
335 
336 		/*
337 		 * ask the sink to verify it owns the remote endpoint,
338 		 * and translate to a sink pad.
339 		 */
340 		sink_idx = media_entity_get_fwnode_pad(sink->entity,
341 						       remote_ep,
342 						       MEDIA_PAD_FL_SINK);
343 		fwnode_handle_put(remote_ep);
344 
345 		if (sink_idx < 0 || sink_idx != sink->index)
346 			continue;
347 
348 		/*
349 		 * the source endpoint corresponds to one of its source pads,
350 		 * the source endpoint connects to an endpoint at the sink
351 		 * entity, and the sink endpoint corresponds to the sink
352 		 * pad requested, so we have found an endpoint connection
353 		 * that works, create the media link for it.
354 		 */
355 
356 		src = &src_sd->entity.pads[src_idx];
357 
358 		/* skip if link already exists */
359 		if (media_entity_find_link(src, sink))
360 			continue;
361 
362 		dev_dbg(src_sd->dev, "creating link %s:%d -> %s:%d\n",
363 			src_sd->entity.name, src_idx,
364 			sink->entity->name, sink_idx);
365 
366 		ret = media_create_pad_link(&src_sd->entity, src_idx,
367 					    sink->entity, sink_idx, flags);
368 		if (ret) {
369 			dev_err(src_sd->dev,
370 				"link %s:%d -> %s:%d failed with %d\n",
371 				src_sd->entity.name, src_idx,
372 				sink->entity->name, sink_idx, ret);
373 
374 			fwnode_handle_put(endpoint);
375 			return ret;
376 		}
377 	}
378 
379 	return 0;
380 }
381 EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links_to_pad);
382 
383 int v4l2_create_fwnode_links(struct v4l2_subdev *src_sd,
384 			     struct v4l2_subdev *sink_sd)
385 {
386 	unsigned int i;
387 
388 	for (i = 0; i < sink_sd->entity.num_pads; i++) {
389 		struct media_pad *pad = &sink_sd->entity.pads[i];
390 		int ret;
391 
392 		if (!(pad->flags & MEDIA_PAD_FL_SINK))
393 			continue;
394 
395 		ret = v4l2_create_fwnode_links_to_pad(src_sd, pad, 0);
396 		if (ret)
397 			return ret;
398 	}
399 
400 	return 0;
401 }
402 EXPORT_SYMBOL_GPL(v4l2_create_fwnode_links);
403 
404 /* -----------------------------------------------------------------------------
405  * Pipeline power management
406  *
407  * Entities must be powered up when part of a pipeline that contains at least
408  * one open video device node.
409  *
410  * To achieve this use the entity use_count field to track the number of users.
411  * For entities corresponding to video device nodes the use_count field stores
412  * the users count of the node. For entities corresponding to subdevs the
413  * use_count field stores the total number of users of all video device nodes
414  * in the pipeline.
415  *
416  * The v4l2_pipeline_pm_{get, put}() functions must be called in the open() and
417  * close() handlers of video device nodes. It increments or decrements the use
418  * count of all subdev entities in the pipeline.
419  *
420  * To react to link management on powered pipelines, the link setup notification
421  * callback updates the use count of all entities in the source and sink sides
422  * of the link.
423  */
424 
425 /*
426  * pipeline_pm_use_count - Count the number of users of a pipeline
427  * @entity: The entity
428  *
429  * Return the total number of users of all video device nodes in the pipeline.
430  */
431 static int pipeline_pm_use_count(struct media_entity *entity,
432 	struct media_graph *graph)
433 {
434 	int use = 0;
435 
436 	media_graph_walk_start(graph, entity);
437 
438 	while ((entity = media_graph_walk_next(graph))) {
439 		if (is_media_entity_v4l2_video_device(entity))
440 			use += entity->use_count;
441 	}
442 
443 	return use;
444 }
445 
446 /*
447  * pipeline_pm_power_one - Apply power change to an entity
448  * @entity: The entity
449  * @change: Use count change
450  *
451  * Change the entity use count by @change. If the entity is a subdev update its
452  * power state by calling the core::s_power operation when the use count goes
453  * from 0 to != 0 or from != 0 to 0.
454  *
455  * Return 0 on success or a negative error code on failure.
456  */
457 static int pipeline_pm_power_one(struct media_entity *entity, int change)
458 {
459 	struct v4l2_subdev *subdev;
460 	int ret;
461 
462 	subdev = is_media_entity_v4l2_subdev(entity)
463 	       ? media_entity_to_v4l2_subdev(entity) : NULL;
464 
465 	if (entity->use_count == 0 && change > 0 && subdev != NULL) {
466 		ret = v4l2_subdev_call(subdev, core, s_power, 1);
467 		if (ret < 0 && ret != -ENOIOCTLCMD)
468 			return ret;
469 	}
470 
471 	entity->use_count += change;
472 	WARN_ON(entity->use_count < 0);
473 
474 	if (entity->use_count == 0 && change < 0 && subdev != NULL)
475 		v4l2_subdev_call(subdev, core, s_power, 0);
476 
477 	return 0;
478 }
479 
480 /*
481  * pipeline_pm_power - Apply power change to all entities in a pipeline
482  * @entity: The entity
483  * @change: Use count change
484  *
485  * Walk the pipeline to update the use count and the power state of all non-node
486  * entities.
487  *
488  * Return 0 on success or a negative error code on failure.
489  */
490 static int pipeline_pm_power(struct media_entity *entity, int change,
491 	struct media_graph *graph)
492 {
493 	struct media_entity *first = entity;
494 	int ret = 0;
495 
496 	if (!change)
497 		return 0;
498 
499 	media_graph_walk_start(graph, entity);
500 
501 	while (!ret && (entity = media_graph_walk_next(graph)))
502 		if (is_media_entity_v4l2_subdev(entity))
503 			ret = pipeline_pm_power_one(entity, change);
504 
505 	if (!ret)
506 		return ret;
507 
508 	media_graph_walk_start(graph, first);
509 
510 	while ((first = media_graph_walk_next(graph))
511 	       && first != entity)
512 		if (is_media_entity_v4l2_subdev(first))
513 			pipeline_pm_power_one(first, -change);
514 
515 	return ret;
516 }
517 
518 static int v4l2_pipeline_pm_use(struct media_entity *entity, unsigned int use)
519 {
520 	struct media_device *mdev = entity->graph_obj.mdev;
521 	int change = use ? 1 : -1;
522 	int ret;
523 
524 	mutex_lock(&mdev->graph_mutex);
525 
526 	/* Apply use count to node. */
527 	entity->use_count += change;
528 	WARN_ON(entity->use_count < 0);
529 
530 	/* Apply power change to connected non-nodes. */
531 	ret = pipeline_pm_power(entity, change, &mdev->pm_count_walk);
532 	if (ret < 0)
533 		entity->use_count -= change;
534 
535 	mutex_unlock(&mdev->graph_mutex);
536 
537 	return ret;
538 }
539 
540 int v4l2_pipeline_pm_get(struct media_entity *entity)
541 {
542 	return v4l2_pipeline_pm_use(entity, 1);
543 }
544 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_get);
545 
546 void v4l2_pipeline_pm_put(struct media_entity *entity)
547 {
548 	/* Powering off entities shouldn't fail. */
549 	WARN_ON(v4l2_pipeline_pm_use(entity, 0));
550 }
551 EXPORT_SYMBOL_GPL(v4l2_pipeline_pm_put);
552 
553 int v4l2_pipeline_link_notify(struct media_link *link, u32 flags,
554 			      unsigned int notification)
555 {
556 	struct media_graph *graph = &link->graph_obj.mdev->pm_count_walk;
557 	struct media_entity *source = link->source->entity;
558 	struct media_entity *sink = link->sink->entity;
559 	int source_use;
560 	int sink_use;
561 	int ret = 0;
562 
563 	source_use = pipeline_pm_use_count(source, graph);
564 	sink_use = pipeline_pm_use_count(sink, graph);
565 
566 	if (notification == MEDIA_DEV_NOTIFY_POST_LINK_CH &&
567 	    !(flags & MEDIA_LNK_FL_ENABLED)) {
568 		/* Powering off entities is assumed to never fail. */
569 		pipeline_pm_power(source, -sink_use, graph);
570 		pipeline_pm_power(sink, -source_use, graph);
571 		return 0;
572 	}
573 
574 	if (notification == MEDIA_DEV_NOTIFY_PRE_LINK_CH &&
575 		(flags & MEDIA_LNK_FL_ENABLED)) {
576 
577 		ret = pipeline_pm_power(source, sink_use, graph);
578 		if (ret < 0)
579 			return ret;
580 
581 		ret = pipeline_pm_power(sink, source_use, graph);
582 		if (ret < 0)
583 			pipeline_pm_power(source, -sink_use, graph);
584 	}
585 
586 	return ret;
587 }
588 EXPORT_SYMBOL_GPL(v4l2_pipeline_link_notify);
589