• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Media controller interface library
3  *
4  * Copyright (C) 2010-2014 Ideas on board SPRL
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
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 Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __MEDIA_H__
23 #define __MEDIA_H__
24 
25 #include <linux/media.h>
26 
27 struct media_link {
28 	struct media_pad *source;
29 	struct media_pad *sink;
30 	struct media_link *twin;
31 	__u32 flags;
32 	__u32 padding[3];
33 };
34 
35 struct media_pad {
36 	struct media_entity *entity;
37 	__u32 index;
38 	__u32 flags;
39 	__u32 padding[3];
40 };
41 
42 struct media_device;
43 struct media_entity;
44 
45 /**
46  * @brief Create a new media device.
47  * @param devnode - device node path.
48  *
49  * Create a media device instance for the given device node and return it. The
50  * device node is not accessed by this function, device node access errors will
51  * not be caught and reported here. The media device needs to be enumerated
52  * before it can be accessed, see media_device_enumerate().
53  *
54  * Media devices are reference-counted, see media_device_ref() and
55  * media_device_unref() for more information.
56  *
57  * @return A pointer to the new media device or NULL if memory cannot be
58  * allocated.
59  */
60 struct media_device *media_device_new(const char *devnode);
61 
62 /**
63  * @brief Create a new emulated media device.
64  * @param info - device information.
65  *
66  * Emulated media devices are userspace-only objects not backed by a kernel
67  * media device. They are created for ALSA and V4L2 devices that are not
68  * associated with a media controller device.
69  *
70  * Only device query functions are available for media devices. Enumerating or
71  * setting up links is invalid.
72  *
73  * @return A pointer to the new media device or NULL if memory cannot be
74  * allocated.
75  */
76 struct media_device *media_device_new_emulated(struct media_device_info *info);
77 
78 /**
79  * @brief Take a reference to the device.
80  * @param media - device instance.
81  *
82  * Media devices are reference-counted. Taking a reference to a device prevents
83  * it from being freed until all references are released. The reference count is
84  * initialized to 1 when the device is created.
85  *
86  * @return A pointer to @a media.
87  */
88 struct media_device *media_device_ref(struct media_device *media);
89 
90 /**
91  * @brief Release a reference to the device.
92  * @param media - device instance.
93  *
94  * Release a reference to the media device. When the reference count reaches 0
95  * this function frees the device.
96  */
97 void media_device_unref(struct media_device *media);
98 
99 /**
100  * @brief Add an entity to an existing media device
101  * @param media - device instance.
102  * @param desc - description of the entity to be added
103  * @param devnode - device node corresponding to the entity
104  *
105  * Entities are usually created and added to media devices automatically when
106  * the media device is enumerated through the media controller API. However,
107  * when an emulated media device (thus not backed with a kernel-side media
108  * controller device) is created, entities need to be manually added.
109  *
110  * Entities can also be manually added to a successfully enumerated media device
111  * to group several functions provided by separate kernel devices. The most
112  * common use case is to group the audio and video functions of a USB webcam in
113  * a single media device. Those functions are exposed through separate USB
114  * interfaces and handled through unrelated kernel drivers, they must thus be
115  * manually added to the same media device.
116  *
117  * This function adds a new entity to the given media device and initializes it
118  * from the given entity description and device node name. Only the following
119  * fields of the description are copied over to the new entity:
120  *
121  * - type
122  * - flags (MEDIA_ENT_FL_DEFAULT only)
123  * - name
124  * - v4l, fb, alsa or dvb (depending on the device type)
125  *
126  * All other fields of the newly created entity id are initialized to 0,
127  * including the entity ID.
128  *
129  * @return Zero on success or -ENOMEM if memory cannot be allocated.
130  */
131 int media_device_add_entity(struct media_device *media,
132 			    const struct media_entity_desc *desc,
133 			    const char *devnode);
134 
135 /**
136  * @brief Set a handler for debug messages.
137  * @param media - device instance.
138  * @param debug_handler - debug message handler
139  * @param debug_priv - first argument to debug message handler
140  *
141  * Set a handler for debug messages that will be called whenever
142  * debugging information is to be printed. The handler expects an
143  * fprintf-like function.
144  */
145 void media_debug_set_handler(
146 	struct media_device *media, void (*debug_handler)(void *, ...),
147 	void *debug_priv);
148 
149 /**
150  * @brief Enumerate the device topology
151  * @param media - device instance.
152  *
153  * Enumerate the media device entities, pads and links. Calling this function is
154  * mandatory before accessing the media device contents.
155  *
156  * @return Zero on success or a negative error code on failure.
157  */
158 int media_device_enumerate(struct media_device *media);
159 
160 /**
161  * @brief Locate the pad at the other end of a link.
162  * @param pad - sink pad at one end of the link.
163  *
164  * Locate the source pad connected to @a pad through an enabled link. As only one
165  * link connected to a sink pad can be enabled at a time, the connected source
166  * pad is guaranteed to be unique.
167  *
168  * @return A pointer to the connected source pad, or NULL if all links connected
169  * to @a pad are disabled. Return NULL also if @a pad is not a sink pad.
170  */
171 struct media_pad *media_entity_remote_source(struct media_pad *pad);
172 
173 /**
174  * @brief Get information about a media entity
175  * @param entity - media entity.
176  *
177  * The information structure is owned by the media entity object and will be
178  * freed when the object is destroyed.
179  *
180  * @return A pointer to the media entity information
181  */
182 const struct media_entity_desc *media_entity_get_info(struct media_entity *entity);
183 
184 /**
185  * @brief Get an entity pad
186  * @param entity - media entity.
187  * @param index - pad index.
188  *
189  * This function returns a pointer to the pad object identified by its index
190  * for the given entity. If the pad index is out of bounds it will return NULL.
191  *
192  * @return A pointer to the pad
193  */
194 const struct media_pad *media_entity_get_pad(struct media_entity *entity,
195 					     unsigned int index);
196 
197 /**
198  * @brief Get the number of links
199  * @param entity - media entity.
200  *
201  * This function returns the total number of links that originate from or arrive
202  * at the the media entity.
203  *
204  * @return The number of links for the entity
205  */
206 unsigned int media_entity_get_links_count(struct media_entity *entity);
207 
208 /**
209  * @brief Get an entity link
210  * @param entity - media entity.
211  * @param index - link index.
212  *
213  * This function returns a pointer to the link object identified by its index
214  * for the given entity. If the link index is out of bounds it will return NULL.
215  *
216  * @return A pointer to the link
217  */
218 const struct media_link *media_entity_get_link(struct media_entity *entity,
219 					       unsigned int index);
220 
221 /**
222  * @brief Get the device node name for an entity
223  * @param entity - media entity.
224  *
225  * This function returns the full path and name to the device node corresponding
226  * to the given entity.
227  *
228  * @return A pointer to the device node name or NULL if the entity has no
229  * associated device node
230  */
231 const char *media_entity_get_devname(struct media_entity *entity);
232 
233 /**
234  * @brief Get the type of an entity.
235  * @param entity - the entity.
236  *
237  * @return The type of @a entity.
238  */
media_entity_type(struct media_entity * entity)239 static inline unsigned int media_entity_type(struct media_entity *entity)
240 {
241 	return media_entity_get_info(entity)->type & MEDIA_ENT_TYPE_MASK;
242 }
243 
244 /**
245  * @brief Find an entity by its name.
246  * @param media - media device.
247  * @param name - entity name.
248  *
249  * Search for an entity with a name equal to @a name.
250  *
251  * @return A pointer to the entity if found, or NULL otherwise.
252  */
253 struct media_entity *media_get_entity_by_name(struct media_device *media,
254 	const char *name);
255 
256 /**
257  * @brief Find an entity by its ID.
258  * @param media - media device.
259  * @param id - entity ID.
260  *
261  * This function searches for an entity based on its ID using an exact match or
262  * next ID method based on the given @a id. If @a id is ORed with
263  * MEDIA_ENT_ID_FLAG_NEXT, the function will return the entity with the smallest
264  * ID larger than @a id. Otherwise it will return the entity with an ID equal to
265  * @a id.
266  *
267  * @return A pointer to the entity if found, or NULL otherwise.
268  */
269 struct media_entity *media_get_entity_by_id(struct media_device *media,
270 	__u32 id);
271 
272 /**
273  * @brief Get the number of entities
274  * @param media - media device.
275  *
276  * This function returns the total number of entities in the media device. If
277  * entities haven't been enumerated yet it will return 0.
278  *
279  * @return The number of entities in the media device
280  */
281 unsigned int media_get_entities_count(struct media_device *media);
282 
283 /**
284  * @brief Get the entities
285  * @param media - media device.
286  *
287  * This function returns a pointer to the array of entities for the media
288  * device. If entities haven't been enumerated yet it will return NULL.
289  *
290  * The array of entities is owned by the media device object and will be freed
291  * when the media object is destroyed.
292  *
293  * @return A pointer to an array of entities
294  */
295 struct media_entity *media_get_entity(struct media_device *media, unsigned int index);
296 
297 /**
298  * @brief Get the default entity for a given type
299  * @param media - media device.
300  * @param type - entity type.
301  *
302  * This function returns the default entity of the requested type. @a type must
303  * be one of
304  *
305  *	MEDIA_ENT_T_DEVNODE_V4L
306  *	MEDIA_ENT_T_DEVNODE_FB
307  *	MEDIA_ENT_T_DEVNODE_ALSA
308  *	MEDIA_ENT_T_DEVNODE_DVB
309  *
310  * @return A pointer to the default entity for the type if it exists, or NULL
311  * otherwise.
312  */
313 struct media_entity *media_get_default_entity(struct media_device *media,
314 					      unsigned int type);
315 
316 /**
317  * @brief Get the media device information
318  * @param media - media device.
319  *
320  * The information structure is owned by the media device object and will be freed
321  * when the media object is destroyed.
322  *
323  * @return A pointer to the media device information
324  */
325 const struct media_device_info *media_get_info(struct media_device *media);
326 
327 /**
328  * @brief Get the media device node name
329  * @param media - media device.
330  *
331  * The device node name string is owned by the media device object and will be
332  * freed when the media object is destroyed.
333  *
334  * @return A pointer to the media device node name
335  */
336 const char *media_get_devnode(struct media_device *media);
337 
338 /**
339  * @brief Configure a link.
340  * @param media - media device.
341  * @param source - source pad at the link origin.
342  * @param sink - sink pad at the link target.
343  * @param flags - configuration flags.
344  *
345  * Locate the link between @a source and @a sink, and configure it by applying
346  * the new @a flags.
347  *
348  * Only the MEDIA_LINK_FLAG_ENABLED flag is writable.
349  *
350  * @return 0 on success, -1 on failure:
351  *	   -ENOENT: link not found
352  *	   - other error codes returned by MEDIA_IOC_SETUP_LINK
353  */
354 int media_setup_link(struct media_device *media,
355 	struct media_pad *source, struct media_pad *sink,
356 	__u32 flags);
357 
358 /**
359  * @brief Reset all links to the disabled state.
360  * @param media - media device.
361  *
362  * Disable all links in the media device. This function is usually used after
363  * opening a media device to reset all links to a known state.
364  *
365  * @return 0 on success, or a negative error code on failure.
366  */
367 int media_reset_links(struct media_device *media);
368 
369 /**
370  * @brief Parse string to an entity on the media device.
371  * @param media - media device.
372  * @param p - input string
373  * @param endp - pointer to string where parsing ended
374  *
375  * Parse NULL terminated string describing an entity and return its
376  * struct media_entity instance.
377  *
378  * @return Pointer to struct media_entity on success, NULL on failure.
379  */
380 struct media_entity *media_parse_entity(struct media_device *media,
381 					const char *p, char **endp);
382 
383 /**
384  * @brief Parse string to a pad on the media device.
385  * @param media - media device.
386  * @param p - input string
387  * @param endp - pointer to string where parsing ended
388  *
389  * Parse NULL terminated string describing a pad and return its struct
390  * media_pad instance.
391  *
392  * @return Pointer to struct media_pad on success, NULL on failure.
393  */
394 struct media_pad *media_parse_pad(struct media_device *media,
395 				  const char *p, char **endp);
396 
397 /**
398  * @brief Parse string to a pad and stream on the media device.
399  * @param media - media device.
400  * @param p - input string
401  * @param stream - pointer to uint where the stream number is stored
402  * @param endp - pointer to string where parsing ended
403  *
404  * Parse NULL terminated string describing a pad and stream and return its struct
405  * media_pad instance and the stream number.
406  *
407  * @return Pointer to struct media_pad on success, NULL on failure.
408  */
409 struct media_pad *media_parse_pad_stream(struct media_device *media,
410 					 const char *p, unsigned int *stream,
411 					 char **endp);
412 
413 /**
414  * @brief Parse string to a link on the media device.
415  * @param media - media device.
416  * @param p - input string
417  * @param endp - pointer to p where parsing ended
418  *
419  * Parse NULL terminated string p describing a link and return its struct
420  * media_link instance.
421  *
422  * @return Pointer to struct media_link on success, NULL on failure.
423  */
424 struct media_link *media_parse_link(struct media_device *media,
425 				    const char *p, char **endp);
426 
427 /**
428  * @brief Parse string to a link on the media device and set it up.
429  * @param media - media device.
430  * @param p - input string
431  *
432  * Parse NULL terminated string p describing a link and its configuration
433  * and configure the link.
434  *
435  * @return 0 on success, or a negative error code on failure.
436  */
437 int media_parse_setup_link(struct media_device *media,
438 			   const char *p, char **endp);
439 
440 /**
441  * @brief Parse string to link(s) on the media device and set it up.
442  * @param media - media device.
443  * @param p - input string
444  *
445  * Parse NULL terminated string p describing link(s) separated by
446  * commas (,) and configure the link(s).
447  *
448  * @return 0 on success, or a negative error code on failure.
449  */
450 int media_parse_setup_links(struct media_device *media, const char *p);
451 
452 #endif
453