blob: 96626356b8f37040d41bd58fbe0e1e4015db0106 [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 *
36 */
37enum media_gobj_type {
38 /* FIXME: add the types here, as we embed media_gobj */
39 MEDIA_GRAPH_NONE
40};
41
42#define MEDIA_BITS_PER_TYPE 8
43#define MEDIA_BITS_PER_LOCAL_ID (32 - MEDIA_BITS_PER_TYPE)
44#define MEDIA_LOCAL_ID_MASK GENMASK(MEDIA_BITS_PER_LOCAL_ID - 1, 0)
45
46/* Structs to represent the objects that belong to a media graph */
47
48/**
49 * struct media_gobj - Define a graph object.
50 *
51 * @id: Non-zero object ID identifier. The ID should be unique
52 * inside a media_device, as it is composed by
53 * MEDIA_BITS_PER_TYPE to store the type plus
54 * MEDIA_BITS_PER_LOCAL_ID to store a per-type ID
55 * (called as "local ID").
56 *
57 * All objects on the media graph should have this struct embedded
58 */
59struct media_gobj {
60 u32 id;
61};
62
63
Laurent Pincharte02188c2010-08-25 09:00:41 -030064struct media_pipeline {
65};
66
Laurent Pinchart53e269c2009-12-09 08:40:00 -030067struct media_link {
68 struct media_pad *source; /* Source pad */
69 struct media_pad *sink; /* Sink pad */
70 struct media_link *reverse; /* Link in the reverse direction */
71 unsigned long flags; /* Link flags (MEDIA_LNK_FL_*) */
72};
73
74struct media_pad {
75 struct media_entity *entity; /* Entity this pad belongs to */
76 u16 index; /* Pad index in the entity pads array */
77 unsigned long flags; /* Pad flags (MEDIA_PAD_FL_*) */
78};
79
Laurent Pinchart5a5394b2014-03-26 00:01:44 -030080/**
81 * struct media_entity_operations - Media entity operations
82 * @link_setup: Notify the entity of link changes. The operation can
83 * return an error, in which case link setup will be
84 * cancelled. Optional.
85 * @link_validate: Return whether a link is valid from the entity point of
86 * view. The media_entity_pipeline_start() function
87 * validates all links by calling this operation. Optional.
88 */
Laurent Pinchart97548ed2009-12-09 08:40:03 -030089struct media_entity_operations {
90 int (*link_setup)(struct media_entity *entity,
91 const struct media_pad *local,
92 const struct media_pad *remote, u32 flags);
Sakari Ailusaf88be32012-01-11 06:25:15 -030093 int (*link_validate)(struct media_link *link);
Laurent Pinchart97548ed2009-12-09 08:40:03 -030094};
95
Laurent Pinchart53e269c2009-12-09 08:40:00 -030096struct media_entity {
97 struct list_head list;
98 struct media_device *parent; /* Media device this entity belongs to*/
99 u32 id; /* Entity ID, unique in the parent media
100 * device context */
101 const char *name; /* Entity name */
102 u32 type; /* Entity type (MEDIA_ENT_T_*) */
103 u32 revision; /* Entity revision, driver specific */
104 unsigned long flags; /* Entity flags (MEDIA_ENT_FL_*) */
105 u32 group_id; /* Entity group ID */
106
107 u16 num_pads; /* Number of sink and source pads */
108 u16 num_links; /* Number of existing links, both
109 * enabled and disabled */
110 u16 num_backlinks; /* Number of backlinks */
111 u16 max_links; /* Maximum number of links */
112
113 struct media_pad *pads; /* Pads array (num_pads elements) */
114 struct media_link *links; /* Links array (max_links elements)*/
115
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300116 const struct media_entity_operations *ops; /* Entity operations */
117
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300118 /* Reference counts must never be negative, but are signed integers on
119 * purpose: a simple WARN_ON(<0) check can be used to detect reference
120 * count bugs that would make them negative.
121 */
Laurent Pincharte02188c2010-08-25 09:00:41 -0300122 int stream_count; /* Stream count for the entity. */
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300123 int use_count; /* Use count for the entity. */
124
Laurent Pincharte02188c2010-08-25 09:00:41 -0300125 struct media_pipeline *pipe; /* Pipeline this entity belongs to. */
126
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300127 union {
128 /* Node specifications */
129 struct {
130 u32 major;
131 u32 minor;
Mauro Carvalho Chehabe31a0ba2015-01-02 12:18:23 -0300132 } dev;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300133
134 /* Sub-device specifications */
135 /* Nothing needed yet */
Clemens Ladischfa5034c2011-11-05 18:42:01 -0300136 } info;
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300137};
138
139static inline u32 media_entity_type(struct media_entity *entity)
140{
141 return entity->type & MEDIA_ENT_TYPE_MASK;
142}
143
144static inline u32 media_entity_subtype(struct media_entity *entity)
145{
146 return entity->type & MEDIA_ENT_SUBTYPE_MASK;
147}
148
Mauro Carvalho Chehabfa762392015-08-14 10:42:05 -0300149static inline u32 media_entity_id(struct media_entity *entity)
150{
151 return entity->id;
152}
153
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300154static inline enum media_gobj_type media_type(struct media_gobj *gobj)
155{
156 return gobj->id >> MEDIA_BITS_PER_LOCAL_ID;
157}
158
159static inline u32 media_localid(struct media_gobj *gobj)
160{
161 return gobj->id & MEDIA_LOCAL_ID_MASK;
162}
163
164static inline u32 media_gobj_gen_id(enum media_gobj_type type, u32 local_id)
165{
166 u32 id;
167
168 id = type << MEDIA_BITS_PER_LOCAL_ID;
169 id |= local_id & MEDIA_LOCAL_ID_MASK;
170
171 return id;
172}
173
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300174#define MEDIA_ENTITY_ENUM_MAX_DEPTH 16
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300175#define MEDIA_ENTITY_ENUM_MAX_ID 64
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300176
Mauro Carvalho Chehabef69ee12015-10-01 18:07:53 -0300177/*
178 * The number of pads can't be bigger than the number of entities,
179 * as the worse-case scenario is to have one entity linked up to
180 * MEDIA_ENTITY_ENUM_MAX_ID - 1 entities.
181 */
182#define MEDIA_ENTITY_MAX_PADS (MEDIA_ENTITY_ENUM_MAX_ID - 1)
183
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300184struct media_entity_graph {
185 struct {
186 struct media_entity *entity;
187 int link;
188 } stack[MEDIA_ENTITY_ENUM_MAX_DEPTH];
Laurent Pinchart5c7b25b2013-06-07 12:45:11 -0300189
190 DECLARE_BITMAP(entities, MEDIA_ENTITY_ENUM_MAX_ID);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300191 int top;
192};
193
Mauro Carvalho Chehabec6e4c92015-08-25 10:28:36 -0300194#define gobj_to_entity(gobj) \
195 container_of(gobj, struct media_entity, graph_obj)
196
197void media_gobj_init(struct media_device *mdev,
198 enum media_gobj_type type,
199 struct media_gobj *gobj);
200void media_gobj_remove(struct media_gobj *gobj);
201
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300202int media_entity_init(struct media_entity *entity, u16 num_pads,
Mauro Carvalho Chehab18095102015-08-06 09:25:57 -0300203 struct media_pad *pads);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300204void media_entity_cleanup(struct media_entity *entity);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300205
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300206int media_entity_create_link(struct media_entity *source, u16 source_pad,
207 struct media_entity *sink, u16 sink_pad, u32 flags);
Sylwester Nawrocki7349cec2013-05-09 08:29:32 -0300208void __media_entity_remove_links(struct media_entity *entity);
209void media_entity_remove_links(struct media_entity *entity);
210
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300211int __media_entity_setup_link(struct media_link *link, u32 flags);
212int media_entity_setup_link(struct media_link *link, u32 flags);
213struct media_link *media_entity_find_link(struct media_pad *source,
214 struct media_pad *sink);
Andrzej Hajda1bddf1b2013-06-03 05:16:13 -0300215struct media_pad *media_entity_remote_pad(struct media_pad *pad);
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300216
Laurent Pinchart503c3d822010-03-07 15:04:59 -0300217struct media_entity *media_entity_get(struct media_entity *entity);
218void media_entity_put(struct media_entity *entity);
219
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300220void media_entity_graph_walk_start(struct media_entity_graph *graph,
221 struct media_entity *entity);
222struct media_entity *
223media_entity_graph_walk_next(struct media_entity_graph *graph);
Sakari Ailusaf88be32012-01-11 06:25:15 -0300224__must_check int media_entity_pipeline_start(struct media_entity *entity,
225 struct media_pipeline *pipe);
Laurent Pincharte02188c2010-08-25 09:00:41 -0300226void media_entity_pipeline_stop(struct media_entity *entity);
Sakari Ailusa5ccc482010-03-07 16:14:14 -0300227
Laurent Pinchart97548ed2009-12-09 08:40:03 -0300228#define media_entity_call(entity, operation, args...) \
229 (((entity)->ops && (entity)->ops->operation) ? \
230 (entity)->ops->operation((entity) , ##args) : -ENOIOCTLCMD)
231
Laurent Pinchart53e269c2009-12-09 08:40:00 -0300232#endif