blob: 30e8f9fd3efab88480c79437641b6a041c545ccb [file] [log] [blame]
Laurent Pinchart53e269c2009-12-09 08:40:00 -03001/*
2 * Media entity
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 *
6 * Contacts: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7 * Sakari Ailus <sakari.ailus@iki.fi>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#ifndef _MEDIA_ENTITY_H
24#define _MEDIA_ENTITY_H
25
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -030026#include <linux/bitops.h>
Sakari Ailus0149a2e2013-12-13 08:58:37 -030027#include <linux/kernel.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030028#include <linux/list.h>
Laurent Pinchart16513332009-12-09 08:40:01 -030029#include <linux/media.h>
Laurent Pinchart53e269c2009-12-09 08:40:00 -030030
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030031/* Enums used internally at the media controller to represent graphs */
32
33/**
34 * enum media_gobj_type - type of a graph object
35 *
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030036 * @MEDIA_GRAPH_ENTITY: Identify a media entity
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030037 * @MEDIA_GRAPH_PAD: Identify a media pad
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030038 * @MEDIA_GRAPH_LINK: Identify a media link
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030039 * @MEDIA_GRAPH_INTF_DEVNODE: Identify a media Kernel API interface via
40 * a device node
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030041 */
42enum media_gobj_type {
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -030043 MEDIA_GRAPH_ENTITY,
Mauro Carvalho Chehab18710dc2015-08-14 12:50:08 -030044 MEDIA_GRAPH_PAD,
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -030045 MEDIA_GRAPH_LINK,
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -030046 MEDIA_GRAPH_INTF_DEVNODE,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030047};
48
49#define MEDIA_BITS_PER_TYPE 8
50#define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE)
51#define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0)
52
53/* Structs to represent the objects that belong to a media graph */
54
55/**
56 * struct media_gobj - Define a graph object.
57 *
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030058 * @mdev: Pointer to the struct media_device that owns the object
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030059 * @id: Non-zero object ID identifier. The ID should be unique
60 * inside a media_device, as it is composed by
61 * MEDIA_BITS_PER_TYPE to store the type plus
62 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
63 * (called as "local ID").
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030064 * @list: List entry stored in one of the per-type mdev object lists
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030065 *
66 * All objects on the media graph should have this struct embedded
67 */
68struct media_gobj {
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -030069 struct media_device *mdev;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030070 u32 id;
Mauro Carvalho Chehab05bfa9f2015-08-23 07:51:33 -030071 struct list_head list;
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -030072};
73
Laurent Pincharte02188c2010-08-25 09:00:41 -030074struct media_pipeline {
75};
76
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030077/**
78 * struct media_link - A link object part of a media graph.
79 *
80 * @graph_obj: Embedded structure containing the media object common data
81 * @list: Linked list associated with an entity or an interface that
82 * owns the link.
83 * @gobj0: Part of a union. Used to get the pointer for the first
84 * graph_object of the link.
85 * @source: Part of a union. Used only if the first object (gobj0) is
86 * a pad. In that case, it represents the source pad.
87 * @intf: Part of a union. Used only if the first object (gobj0) is
88 * an interface.
89 * @gobj1: Part of a union. Used to get the pointer for the second
90 * graph_object of the link.
91 * @source: Part of a union. Used only if the second object (gobj1) is
92 * a pad. In that case, it represents the sink pad.
93 * @entity: Part of a union. Used only if the second object (gobj1) is
94 * an entity.
95 * @reverse: Pointer to the link for the reverse direction of a pad to pad
96 * link.
97 * @flags: Link flags, as defined in uapi/media.h (MEDIA_LNK_FL_*)
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -030098 * @is_backlink: Indicate if the link is a backlink.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -030099 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300100struct media_link {
Mauro Carvalho Chehab6b6a4272015-08-14 12:54:36 -0300101 struct media_gobj graph_obj;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300102 struct list_head list;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300103 union {
104 struct media_gobj *gobj0;
105 struct media_pad *source;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300106 struct media_interface *intf;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300107 };
108 union {
109 struct media_gobj *gobj1;
110 struct media_pad *sink;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300111 struct media_entity *entity;
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300112 };
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300113 struct media_link *reverse;
114 unsigned long flags;
Mauro Carvalho Chehab39d1ebc62015-08-30 09:53:57 -0300115 bool is_backlink;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300116};
117
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300118/**
119 * struct media_pad - A media pad graph object.
120 *
121 * @graph_obj: Embedded structure containing the media object common data
122 * @entity: Entity this pad belongs to
123 * @index: Pad index in the entity pads array, numbered from 0 to n
124 * @flags: Pad flags, as defined in uapi/media.h (MEDIA_PAD_FL_*)
125 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300126struct media_pad {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300127 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300128 struct media_entity *entity;
129 u16 index;
130 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300131};
132
Laurent Pinchart5a5394b2014-03-26 00:01:44 -0300133/**
134 * struct media_entity_operations - Media entity operations
135 * @link_setup: Notify the entity of link changes. The operation can
136 * return an error, in which case link setup will be
137 * cancelled. Optional.
138 * @link_validate: Return whether a link is valid from the entity point of
139 * view. The media_entity_pipeline_start() function
140 * validates all links by calling this operation. Optional.
141 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300142struct media_entity_operations {
143 int (*link_setup)(struct media_entity *entity,
144 const struct media_pad *local,
145 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300146 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300147};
148
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300149/**
150 * struct media_entity - A media entity graph object.
151 *
152 * @graph_obj: Embedded structure containing the media object common data.
153 * @name: Entity name.
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300154 * @function: Entity main function, as defined in uapi/media.h
155 * (MEDIA_ENT_F_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300156 * @flags: Entity flags, as defined in uapi/media.h (MEDIA_ENT_FL_*)
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300157 * @num_pads: Number of sink and source pads.
158 * @num_links: Total number of links, forward and back, enabled and disabled.
159 * @num_backlinks: Number of backlinks
Sakari Ailus665faa92015-12-16 11:32:17 -0200160 * @internal_idx: An unique internal entity specific number. The numbers are
161 * re-used if entities are unregistered or registered again.
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300162 * @pads: Pads array with the size defined by @num_pads.
163 * @links: List of data links.
164 * @ops: Entity operations.
165 * @stream_count: Stream count for the entity.
166 * @use_count: Use count for the entity.
167 * @pipe: Pipeline this entity belongs to.
168 * @info: Union with devnode information. Kept just for backward
169 * compatibility.
170 * @major: Devnode major number (zero if not applicable). Kept just
171 * for backward compatibility.
172 * @minor: Devnode minor number (zero if not applicable). Kept just
173 * for backward compatibility.
174 *
175 * NOTE: @stream_count and @use_count reference counts must never be
176 * negative, but are signed integers on purpose: a simple WARN_ON(<0) check
177 * can be used to detect reference count bugs that would make them negative.
178 */
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300179struct media_entity {
Mauro Carvalho Chehab4b8a3c02015-08-20 09:10:07 -0300180 struct media_gobj graph_obj; /* must be first field in struct */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300181 const char *name;
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300182 u32 function;
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300183 unsigned long flags;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300184
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300185 u16 num_pads;
186 u16 num_links;
187 u16 num_backlinks;
Sakari Ailus665faa92015-12-16 11:32:17 -0200188 int internal_idx;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300189
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300190 struct media_pad *pads;
191 struct list_head links;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300192
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300193 const struct media_entity_operations *ops;
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300194
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300195 /* Reference counts must never be negative, but are signed integers on
196 * purpose: a simple WARN_ON(<0) check can be used to detect reference
197 * count bugs that would make them negative.
198 */
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300199 int stream_count;
200 int use_count;
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300201
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300202 struct media_pipeline *pipe;
Laurent Pincharte02188c2010-08-25 09:00:41 -0300203
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300204 union {
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300205 struct {
206 u32 major;
207 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300208 } dev;
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300209 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300210};
211
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300212/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300213 * struct media_interface - A media interface graph object.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300214 *
215 * @graph_obj: embedded graph object
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300216 * @links: List of links pointing to graph entities
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300217 * @type: Type of the interface as defined in the
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300218 * uapi/media/media.h header, e. g.
219 * MEDIA_INTF_T_*
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300220 * @flags: Interface flags as defined in uapi/media/media.h
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300221 */
222struct media_interface {
223 struct media_gobj graph_obj;
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300224 struct list_head links;
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300225 u32 type;
226 u32 flags;
227};
228
229/**
Mauro Carvalho Chehabc358e802015-08-29 23:43:03 -0300230 * struct media_intf_devnode - A media interface via a device node.
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300231 *
232 * @intf: embedded interface object
233 * @major: Major number of a device node
234 * @minor: Minor number of a device node
235 */
236struct media_intf_devnode {
237 struct media_interface intf;
Mauro Carvalho Chehabc398bb62015-08-23 08:28:21 -0300238
239 /* Should match the fields at media_v2_intf_devnode */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300240 u32 major;
241 u32 minor;
242};
243
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200244/**
245 * media_entity_id() - return the media entity graph object id
246 *
247 * @entity: pointer to entity
248 */
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300249static inline u32 media_entity_id(struct media_entity *entity)
250{
Mauro Carvalho Chehabbfab2aac2015-08-14 12:47:48 -0300251 return entity->graph_obj.id;
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300252}
253
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200254/**
255 * media_type() - return the media object type
256 *
257 * @gobj: pointer to the media graph object
258 */
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300259static inline enum media_gobj_type media_type(struct media_gobj *gobj)
260{
261 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
262}
263
264static inline u32 media_localid(struct media_gobj *gobj)
265{
266 return gobj->id & MEDIA_LOCAL_ID_MASK;
267}
268
269static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
270{
271 u32 id;
272
273 id = type << MEDIA_BITS_PER_LOCAL_ID;
274 id |= local_id & MEDIA_LOCAL_ID_MASK;
275
276 return id;
277}
278
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200279/**
280 * is_media_entity_v4l2_io() - identify if the entity main function
281 * is a V4L2 I/O
282 *
283 * @entity: pointer to entity
284 *
285 * Return: true if the entity main function is one of the V4L2 I/O types
286 * (video, VBI or SDR radio); false otherwise.
287 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300288static inline bool is_media_entity_v4l2_io(struct media_entity *entity)
289{
290 if (!entity)
291 return false;
292
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300293 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200294 case MEDIA_ENT_F_IO_V4L:
295 case MEDIA_ENT_F_IO_VBI:
296 case MEDIA_ENT_F_IO_SWRADIO:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300297 return true;
298 default:
299 return false;
300 }
301}
302
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200303/**
304 * is_media_entity_v4l2_subdev - return true if the entity main function is
305 * associated with the V4L2 API subdev usage
306 *
307 * @entity: pointer to entity
308 *
309 * This is an ancillary function used by subdev-based V4L2 drivers.
310 * It checks if the entity function is one of functions used by a V4L2 subdev,
311 * e. g. camera-relatef functions, analog TV decoder, TV tuner, V4L2 DSPs.
312 */
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300313static inline bool is_media_entity_v4l2_subdev(struct media_entity *entity)
314{
315 if (!entity)
316 return false;
317
Mauro Carvalho Chehab0e576b72015-09-06 09:33:39 -0300318 switch (entity->function) {
Mauro Carvalho Chehab4ca72ef2015-12-10 17:25:41 -0200319 case MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN:
320 case MEDIA_ENT_F_CAM_SENSOR:
321 case MEDIA_ENT_F_FLASH:
322 case MEDIA_ENT_F_LENS:
323 case MEDIA_ENT_F_ATV_DECODER:
324 case MEDIA_ENT_F_TUNER:
Mauro Carvalho Chehabfa17b462015-08-21 12:17:40 -0300325 return true;
326
327 default:
328 return false;
329 }
330}
331
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300332#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300333#define MEDIA_ENTITY_ENUM_MAX_ID 64
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300334
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300335/*
336 * The number of pads can't be bigger than the number of entities,
337 * as the worse-case scenario is to have one entity linked up to
338 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
339 */
340#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
341
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300342struct media_entity_graph {
343 struct {
344 struct media_entity *entity;
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300345 struct list_head *link;
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300346 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300347
348 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300349 int top;
350};
351
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300352#define gobj_to_entity(gobj) \
353 container_of(gobj, struct media_entity, graph_obj)
354
Mauro Carvalho Chehab39a956c2015-08-13 14:42:42 -0300355#define gobj_to_pad(gobj) \
356 container_of(gobj, struct media_pad, graph_obj)
357
358#define gobj_to_link(gobj) \
359 container_of(gobj, struct media_link, graph_obj)
360
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300361#define gobj_to_link(gobj) \
362 container_of(gobj, struct media_link, graph_obj)
363
364#define gobj_to_pad(gobj) \
365 container_of(gobj, struct media_pad, graph_obj)
366
367#define gobj_to_intf(gobj) \
368 container_of(gobj, struct media_interface, graph_obj)
369
370#define intf_to_devnode(intf) \
371 container_of(intf, struct media_intf_devnode, intf)
372
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200373/**
374 * media_gobj_create - Initialize a graph object
375 *
376 * @mdev: Pointer to the media_device that contains the object
377 * @type: Type of the object
378 * @gobj: Pointer to the graph object
379 *
380 * This routine initializes the embedded struct media_gobj inside a
381 * media graph object. It is called automatically if media_*_create()
382 * calls are used. However, if the object (entity, link, pad, interface)
383 * is embedded on some other object, this function should be called before
384 * registering the object at the media controller.
385 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200386void media_gobj_create(struct media_device *mdev,
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300387 enum media_gobj_type type,
388 struct media_gobj *gobj);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200389
390/**
391 * media_gobj_destroy - Stop using a graph object on a media device
392 *
393 * @gobj: Pointer to the graph object
394 *
395 * This should be called by all routines like media_device_unregister()
396 * that remove/destroy media graph objects.
397 */
Mauro Carvalho Chehabc350ef82015-12-11 11:55:40 -0200398void media_gobj_destroy(struct media_gobj *gobj);
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300399
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200400/**
401 * media_entity_pads_init() - Initialize the entity pads
402 *
403 * @entity: entity where the pads belong
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200404 * @num_pads: total number of sink and source pads
405 * @pads: Array of @num_pads pads.
406 *
407 * The pads array is managed by the entity driver and passed to
408 * media_entity_pads_init() where its pointer will be stored in the entity
409 * structure.
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200410 *
411 * If no pads are needed, drivers could either directly fill
412 * &media_entity->@num_pads with 0 and &media_entity->@pads with NULL or call
413 * this function that will do the same.
414 *
415 * As the number of pads is known in advance, the pads array is not allocated
416 * dynamically but is managed by the entity driver. Most drivers will embed the
417 * pads array in a driver-specific structure, avoiding dynamic allocation.
418 *
419 * Drivers must set the direction of every pad in the pads array before calling
420 * media_entity_pads_init(). The function will initialize the other pads fields.
421 */
Mauro Carvalho Chehabab22e772015-12-11 07:44:40 -0200422int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab57208e52015-08-07 06:55:40 -0300423 struct media_pad *pads);
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200424
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200425/**
426 * media_entity_cleanup() - free resources associated with an entity
427 *
428 * @entity: entity where the pads belong
429 *
430 * This function must be called during the cleanup phase after unregistering
431 * the entity (currently, it does nothing).
432 */
Mauro Carvalho Chehabf1fd3282015-12-11 09:13:23 -0200433static inline void media_entity_cleanup(struct media_entity *entity) {};
Laurent Pincharte02188c2010-08-25 09:00:41 -0300434
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200435/**
436 * media_create_pad_link() - creates a link between two entities.
437 *
438 * @source: pointer to &media_entity of the source pad.
439 * @source_pad: number of the source pad in the pads array
440 * @sink: pointer to &media_entity of the sink pad.
441 * @sink_pad: number of the sink pad in the pads array.
442 * @flags: Link flags, as defined in include/uapi/linux/media.h.
443 *
444 * Valid values for flags:
445 * A %MEDIA_LNK_FL_ENABLED flag indicates that the link is enabled and can be
446 * used to transfer media data. When two or more links target a sink pad,
447 * only one of them can be enabled at a time.
448 *
449 * A %MEDIA_LNK_FL_IMMUTABLE flag indicates that the link enabled state can't
450 * be modified at runtime. If %MEDIA_LNK_FL_IMMUTABLE is set, then
451 * %MEDIA_LNK_FL_ENABLED must also be set since an immutable link is
452 * always enabled.
453 *
454 * NOTE:
455 *
456 * Before calling this function, media_entity_pads_init() and
457 * media_device_register_entity() should be called previously for both ends.
458 */
Mauro Carvalho Chehab77328042015-09-04 16:08:24 -0300459__must_check int media_create_pad_link(struct media_entity *source,
460 u16 source_pad, struct media_entity *sink,
461 u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300462void __media_entity_remove_links(struct media_entity *entity);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200463
464/**
465 * media_entity_remove_links() - remove all links associated with an entity
466 *
467 * @entity: pointer to &media_entity
468 *
469 * Note: this is called automatically when an entity is unregistered via
470 * media_device_register_entity().
471 */
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300472void media_entity_remove_links(struct media_entity *entity);
473
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200474/**
475 * __media_entity_setup_link - Configure a media link without locking
476 * @link: The link being configured
477 * @flags: Link configuration flags
478 *
479 * The bulk of link setup is handled by the two entities connected through the
480 * link. This function notifies both entities of the link configuration change.
481 *
482 * If the link is immutable or if the current and new configuration are
483 * identical, return immediately.
484 *
485 * The user is expected to hold link->source->parent->mutex. If not,
486 * media_entity_setup_link() should be used instead.
487 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300488int __media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200489
490/**
491 * media_entity_setup_link() - changes the link flags properties in runtime
492 *
493 * @link: pointer to &media_link
494 * @flags: the requested new link flags
495 *
496 * The only configurable property is the %MEDIA_LNK_FL_ENABLED link flag
497 * flag to enable/disable a link. Links marked with the
498 * %MEDIA_LNK_FL_IMMUTABLE link flag can not be enabled or disabled.
499 *
500 * When a link is enabled or disabled, the media framework calls the
501 * link_setup operation for the two entities at the source and sink of the
502 * link, in that order. If the second link_setup call fails, another
503 * link_setup call is made on the first entity to restore the original link
504 * flags.
505 *
506 * Media device drivers can be notified of link setup operations by setting the
507 * media_device::link_notify pointer to a callback function. If provided, the
508 * notification callback will be called before enabling and after disabling
509 * links.
510 *
511 * Entity drivers must implement the link_setup operation if any of their links
512 * is non-immutable. The operation must either configure the hardware or store
513 * the configuration information to be applied later.
514 *
515 * Link configuration must not have any side effect on other links. If an
516 * enabled link at a sink pad prevents another link at the same pad from
517 * being enabled, the link_setup operation must return -EBUSY and can't
518 * implicitly disable the first enabled link.
519 *
520 * NOTE: the valid values of the flags for the link is the same as described
521 * on media_create_pad_link(), for pad to pad links or the same as described
522 * on media_create_intf_link(), for interface to entity links.
523 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300524int media_entity_setup_link(struct media_link *link, u32 flags);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200525
526/**
527 * media_entity_find_link - Find a link between two pads
528 * @source: Source pad
529 * @sink: Sink pad
530 *
531 * Return a pointer to the link between the two entities. If no such link
532 * exists, return NULL.
533 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300534struct media_link *media_entity_find_link(struct media_pad *source,
535 struct media_pad *sink);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200536
537/**
538 * media_entity_remote_pad - Find the pad at the remote end of a link
539 * @pad: Pad at the local end of the link
540 *
541 * Search for a remote pad connected to the given pad by iterating over all
542 * links originating or terminating at that pad until an enabled link is found.
543 *
544 * Return a pointer to the pad at the remote end of the first found enabled
545 * link, or NULL if no enabled link has been found.
546 */
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300547struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300548
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200549/**
550 * media_entity_get - Get a reference to the parent module
551 *
552 * @entity: The entity
553 *
554 * Get a reference to the parent media device module.
555 *
556 * The function will return immediately if @entity is NULL.
557 *
558 * Return a pointer to the entity on success or NULL on failure.
559 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300560struct media_entity *media_entity_get(struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200561
562/**
563 * media_entity_put - Release the reference to the parent module
564 *
565 * @entity: The entity
566 *
567 * Release the reference count acquired by media_entity_get().
568 *
569 * The function will return immediately if @entity is NULL.
570 */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300571void media_entity_put(struct media_entity *entity);
572
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200573/**
574 * media_entity_graph_walk_start - Start walking the media graph at a given entity
575 * @graph: Media graph structure that will be used to walk the graph
576 * @entity: Starting entity
577 *
578 * This function initializes the graph traversal structure to walk the entities
579 * graph starting at the given entity. The traversal structure must not be
580 * modified by the caller during graph traversal. When done the structure can
581 * safely be freed.
582 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300583void media_entity_graph_walk_start(struct media_entity_graph *graph,
584 struct media_entity *entity);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200585
586/**
587 * media_entity_graph_walk_next - Get the next entity in the graph
588 * @graph: Media graph structure
589 *
590 * Perform a depth-first traversal of the given media entities graph.
591 *
592 * The graph structure must have been previously initialized with a call to
593 * media_entity_graph_walk_start().
594 *
595 * Return the next entity in the graph or NULL if the whole graph have been
596 * traversed.
597 */
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300598struct media_entity *
599media_entity_graph_walk_next(struct media_entity_graph *graph);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200600
601/**
602 * media_entity_pipeline_start - Mark a pipeline as streaming
603 * @entity: Starting entity
604 * @pipe: Media pipeline to be assigned to all entities in the pipeline.
605 *
606 * Mark all entities connected to a given entity through enabled links, either
607 * directly or indirectly, as streaming. The given pipeline object is assigned to
608 * every entity in the pipeline and stored in the media_entity pipe field.
609 *
610 * Calls to this function can be nested, in which case the same number of
611 * media_entity_pipeline_stop() calls will be required to stop streaming. The
612 * pipeline pointer must be identical for all nested calls to
613 * media_entity_pipeline_start().
614 */
Sakari Ailusaf88be32012-01-11 06:25:15 -0300615__must_check int media_entity_pipeline_start(struct media_entity *entity,
616 struct media_pipeline *pipe);
Mauro Carvalho Chehab1fc25d32015-12-11 12:14:58 -0200617
618/**
619 * media_entity_pipeline_stop - Mark a pipeline as not streaming
620 * @entity: Starting entity
621 *
622 * Mark all entities connected to a given entity through enabled links, either
623 * directly or indirectly, as not streaming. The media_entity pipe field is
624 * reset to NULL.
625 *
626 * If multiple calls to media_entity_pipeline_start() have been made, the same
627 * number of calls to this function are required to mark the pipeline as not
628 * streaming.
629 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300630void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300631
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200632/**
633 * media_devnode_create() - creates and initializes a device node interface
634 *
635 * @mdev: pointer to struct &media_device
636 * @type: type of the interface, as given by MEDIA_INTF_T_* macros
637 * as defined in the uapi/media/media.h header.
638 * @flags: Interface flags as defined in uapi/media/media.h.
639 * @major: Device node major number.
640 * @minor: Device node minor number.
641 *
642 * Return: if succeeded, returns a pointer to the newly allocated
643 * &media_intf_devnode pointer.
644 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300645struct media_intf_devnode *
646__must_check media_devnode_create(struct media_device *mdev,
647 u32 type, u32 flags,
Mauro Carvalho Chehab0b3b72df92015-09-09 08:19:25 -0300648 u32 major, u32 minor);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200649/**
650 * media_devnode_remove() - removes a device node interface
651 *
652 * @devnode: pointer to &media_intf_devnode to be freed.
653 *
654 * When a device node interface is removed, all links to it are automatically
655 * removed.
656 */
Mauro Carvalho Chehab27e543f2015-08-20 09:07:34 -0300657void media_devnode_remove(struct media_intf_devnode *devnode);
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300658struct media_link *
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200659
660/**
661 * media_create_intf_link() - creates a link between an entity and an interface
662 *
663 * @entity: pointer to %media_entity
664 * @intf: pointer to %media_interface
665 * @flags: Link flags, as defined in include/uapi/linux/media.h.
666 *
667 *
668 * Valid values for flags:
669 * The %MEDIA_LNK_FL_ENABLED flag indicates that the interface is connected to
670 * the entity hardware. That's the default value for interfaces. An
671 * interface may be disabled if the hardware is busy due to the usage
672 * of some other interface that it is currently controlling the hardware.
673 * A typical example is an hybrid TV device that handle only one type of
674 * stream on a given time. So, when the digital TV is streaming,
675 * the V4L2 interfaces won't be enabled, as such device is not able to
676 * also stream analog TV or radio.
677 *
678 * Note:
679 *
680 * Before calling this function, media_devnode_create() should be called for
681 * the interface and media_device_register_entity() should be called for the
682 * interface that will be part of the link.
683 */
Mauro Carvalho Chehab5e5387d2015-09-04 15:34:19 -0300684__must_check media_create_intf_link(struct media_entity *entity,
685 struct media_interface *intf,
686 u32 flags);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200687/**
688 * __media_remove_intf_link() - remove a single interface link
689 *
690 * @link: pointer to &media_link.
691 *
692 * Note: this is an unlocked version of media_remove_intf_link()
693 */
Mauro Carvalho Chehabd47109f2015-08-29 21:23:44 -0300694void __media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200695
696/**
697 * media_remove_intf_link() - remove a single interface link
698 *
699 * @link: pointer to &media_link.
700 *
701 * Note: prefer to use this one, instead of __media_remove_intf_link()
702 */
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300703void media_remove_intf_link(struct media_link *link);
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200704
705/**
706 * __media_remove_intf_links() - remove all links associated with an interface
707 *
708 * @intf: pointer to &media_interface
709 *
710 * Note: this is an unlocked version of media_remove_intf_links().
711 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300712void __media_remove_intf_links(struct media_interface *intf);
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200713/**
714 * media_remove_intf_links() - remove all links associated with an interface
715 *
716 * @intf: pointer to &media_interface
717 *
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200718 * Notes:
719 *
720 * this is called automatically when an entity is unregistered via
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200721 * media_device_register_entity() and by media_devnode_remove().
Mauro Carvalho Chehab60266182015-12-13 08:20:46 -0200722 *
723 * Prefer to use this one, instead of __media_remove_intf_links().
Mauro Carvalho Chehabdb7ee322015-12-11 11:06:08 -0200724 */
Mauro Carvalho Chehab7c4696a2015-08-24 08:46:46 -0300725void media_remove_intf_links(struct media_interface *intf);
726
Mauro Carvalho Chehab86e26622015-08-07 10:36:25 -0300727
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300728#define media_entity_call(entity, operation, args...) \
729 (((entity)->ops && (entity)->ops->operation) ? \
730 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
731
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300732#endif