]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/media/media-device.h
[media] doc-rst: Fix conversion for v4l2 core functions
[mirror_ubuntu-bionic-kernel.git] / include / media / media-device.h
CommitLineData
176fb0d1
LP
1/*
2 * Media device
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_DEVICE_H
24#define _MEDIA_DEVICE_H
25
176fb0d1 26#include <linux/list.h>
503c3d82 27#include <linux/mutex.h>
176fb0d1
LP
28
29#include <media/media-devnode.h>
53e269c1 30#include <media/media-entity.h>
176fb0d1 31
665faa97 32struct ida;
313162d0
PG
33struct device;
34
afcbdb55
SK
35/**
36 * struct media_entity_notify - Media Entity Notify
37 *
38 * @list: List head
39 * @notify_data: Input data to invoke the callback
40 * @notify: Callback function pointer
41 *
42 * Drivers may register a callback to take action when
43 * new entities get registered with the media device.
44 */
45struct media_entity_notify {
46 struct list_head list;
47 void *notify_data;
48 void (*notify)(struct media_entity *entity, void *notify_data);
49};
50
176fb0d1
LP
51/**
52 * struct media_device - Media device
53 * @dev: Parent device
54 * @devnode: Media device node
bb07bd6b
MCC
55 * @driver_name: Optional device driver name. If not set, calls to
56 * %MEDIA_IOC_DEVICE_INFO will return dev->driver->name.
57 * This is needed for USB drivers for example, as otherwise
58 * they'll all appear as if the driver name was "usb".
176fb0d1
LP
59 * @model: Device model name
60 * @serial: Device serial number (optional)
61 * @bus_info: Unique and stable device location identifier
62 * @hw_revision: Hardware device revision
63 * @driver_version: Device driver version
2521fdac
MCC
64 * @topology_version: Monotonic counter for storing the version of the graph
65 * topology. Should be incremented each time the topology changes.
05b3b77c 66 * @id: Unique ID used on the last registered graph object
03e49338
MCC
67 * @entity_internal_idx: Unique internal entity ID used by the graph traversal
68 * algorithms
69 * @entity_internal_idx_max: Allocated internal entity indices
53e269c1 70 * @entities: List of registered entities
57cf79b7 71 * @interfaces: List of registered interfaces
9155d859
MCC
72 * @pads: List of registered pads
73 * @links: List of registered links
afcbdb55 74 * @entity_notify: List of registered entity_notify callbacks
e2c91d4d 75 * @graph_mutex: Protects access to struct media_device data
0c426c47
SA
76 * @pm_count_walk: Graph walk for power state walk. Access serialised using
77 * graph_mutex.
cd87ce87
SK
78 *
79 * @source_priv: Driver Private data for enable/disable source handlers
80 * @enable_source: Enable Source Handler function pointer
81 * @disable_source: Disable Source Handler function pointer
82 *
5ed470fe
MCC
83 * @link_notify: Link state change notification callback. This callback is
84 * called with the graph_mutex held.
176fb0d1
LP
85 *
86 * This structure represents an abstract high-level media device. It allows easy
87 * access to entities and provides basic media device-level support. The
88 * structure can be allocated directly or embedded in a larger structure.
89 *
90 * The parent @dev is a physical device. It must be set before registering the
91 * media device.
92 *
93 * @model is a descriptive model name exported through sysfs. It doesn't have to
94 * be unique.
cd87ce87
SK
95 *
96 * @enable_source is a handler to find source entity for the
97 * sink entity and activate the link between them if source
98 * entity is free. Drivers should call this handler before
99 * accessing the source.
100 *
101 * @disable_source is a handler to find source entity for the
102 * sink entity and deactivate the link between them. Drivers
103 * should call this handler to release the source.
104 *
105 * Note: Bridge driver is expected to implement and set the
106 * handler when media_device is registered or when
107 * bridge driver finds the media_device during probe.
108 * Bridge driver sets source_priv with information
109 * necessary to run enable/disable source handlers.
110 *
111 * Use-case: find tuner entity connected to the decoder
112 * entity and check if it is available, and activate the
113 * the link between them from enable_source and deactivate
114 * from disable_source.
176fb0d1
LP
115 */
116struct media_device {
117 /* dev->driver_data points to this struct. */
118 struct device *dev;
a087ce70 119 struct media_devnode *devnode;
176fb0d1
LP
120
121 char model[32];
bb07bd6b 122 char driver_name[32];
176fb0d1
LP
123 char serial[40];
124 char bus_info[32];
125 u32 hw_revision;
126 u32 driver_version;
53e269c1 127
952f8eef 128 u64 topology_version;
2521fdac 129
05b3b77c 130 u32 id;
665faa97
SA
131 struct ida entity_internal_idx;
132 int entity_internal_idx_max;
bfab2aac 133
53e269c1 134 struct list_head entities;
57cf79b7 135 struct list_head interfaces;
9155d859
MCC
136 struct list_head pads;
137 struct list_head links;
53e269c1 138
afcbdb55
SK
139 /* notify callback list invoked when a new entity is registered */
140 struct list_head entity_notify;
141
503c3d82
LP
142 /* Serializes graph operations. */
143 struct mutex graph_mutex;
0c426c47 144 struct media_entity_graph pm_count_walk;
97548ed4 145
cd87ce87
SK
146 void *source_priv;
147 int (*enable_source)(struct media_entity *entity,
148 struct media_pipeline *pipe);
149 void (*disable_source)(struct media_entity *entity);
150
813f5c0a
SN
151 int (*link_notify)(struct media_link *link, u32 flags,
152 unsigned int notification);
176fb0d1
LP
153};
154
41b44e35
MCC
155/* We don't need to include pci.h or usb.h here */
156struct pci_dev;
157struct usb_device;
158
e576d60b
SK
159#ifdef CONFIG_MEDIA_CONTROLLER
160
813f5c0a
SN
161/* Supported link_notify @notification values. */
162#define MEDIA_DEV_NOTIFY_PRE_LINK_CH 0
163#define MEDIA_DEV_NOTIFY_POST_LINK_CH 1
164
c8d54cd5
SA
165/**
166 * media_entity_enum_init - Initialise an entity enumeration
167 *
03e49338 168 * @ent_enum: Entity enumeration to be initialised
c8d54cd5
SA
169 * @mdev: The related media device
170 *
171 * Returns zero on success or a negative error code.
172 */
173static inline __must_check int media_entity_enum_init(
174 struct media_entity_enum *ent_enum, struct media_device *mdev)
175{
176 return __media_entity_enum_init(ent_enum,
177 mdev->entity_internal_idx_max + 1);
178}
179
9832e155
JMC
180/**
181 * media_device_init() - Initializes a media device element
182 *
183 * @mdev: pointer to struct &media_device
184 *
185 * This function initializes the media device prior to its registration.
186 * The media device initialization and registration is split in two functions
187 * to avoid race conditions and make the media device available to user-space
188 * before the media graph has been completed.
189 *
190 * So drivers need to first initialize the media device, register any entity
191 * within the media device, create pad to pad links and then finally register
192 * the media device by calling media_device_register() as a final step.
193 */
194void media_device_init(struct media_device *mdev);
195
196/**
197 * media_device_cleanup() - Cleanups a media device element
198 *
199 * @mdev: pointer to struct &media_device
200 *
201 * This function that will destroy the graph_mutex that is
202 * initialized in media_device_init().
203 */
204void media_device_cleanup(struct media_device *mdev);
205
db7ee32a
MCC
206/**
207 * __media_device_register() - Registers a media device element
208 *
209 * @mdev: pointer to struct &media_device
210 * @owner: should be filled with %THIS_MODULE
211 *
212 * Users, should, instead, call the media_device_register() macro.
213 *
214 * The caller is responsible for initializing the media_device structure before
215 * registration. The following fields must be set:
216 *
217 * - dev must point to the parent device (usually a &pci_dev, &usb_interface or
218 * &platform_device instance).
219 *
220 * - model must be filled with the device model name as a NUL-terminated UTF-8
221 * string. The device/model revision must not be stored in this field.
222 *
223 * The following fields are optional:
224 *
225 * - serial is a unique serial number stored as a NUL-terminated ASCII string.
226 * The field is big enough to store a GUID in text form. If the hardware
227 * doesn't provide a unique serial number this field must be left empty.
228 *
229 * - bus_info represents the location of the device in the system as a
230 * NUL-terminated ASCII string. For PCI/PCIe devices bus_info must be set to
231 * "PCI:" (or "PCIe:") followed by the value of pci_name(). For USB devices,
232 * the usb_make_path() function must be used. This field is used by
233 * applications to distinguish between otherwise identical devices that don't
234 * provide a serial number.
235 *
236 * - hw_revision is the hardware device revision in a driver-specific format.
237 * When possible the revision should be formatted with the KERNEL_VERSION
238 * macro.
239 *
240 * - driver_version is formatted with the KERNEL_VERSION macro. The version
241 * minor must be incremented when new features are added to the userspace API
242 * without breaking binary compatibility. The version major must be
243 * incremented when binary compatibility is broken.
244 *
245 * Notes:
246 *
247 * Upon successful registration a character device named media[0-9]+ is created.
248 * The device major and minor numbers are dynamic. The model name is exported as
249 * a sysfs attribute.
250 *
251 * Unregistering a media device that hasn't been registered is *NOT* safe.
92777994
MCC
252 *
253 * Return: returns zero on success or a negative error code.
db7ee32a 254 */
85de721c
SA
255int __must_check __media_device_register(struct media_device *mdev,
256 struct module *owner);
257#define media_device_register(mdev) __media_device_register(mdev, THIS_MODULE)
db7ee32a
MCC
258
259/**
3047f3f9 260 * media_device_unregister() - Unregisters a media device element
db7ee32a
MCC
261 *
262 * @mdev: pointer to struct &media_device
92777994
MCC
263 *
264 *
265 * It is safe to call this function on an unregistered (but initialised)
266 * media device.
db7ee32a 267 */
176fb0d1
LP
268void media_device_unregister(struct media_device *mdev);
269
db7ee32a
MCC
270/**
271 * media_device_register_entity() - registers a media entity inside a
272 * previously registered media device.
273 *
274 * @mdev: pointer to struct &media_device
275 * @entity: pointer to struct &media_entity to be registered
276 *
277 * Entities are identified by a unique positive integer ID. The media
278 * controller framework will such ID automatically. IDs are not guaranteed
279 * to be contiguous, and the ID number can change on newer Kernel versions.
280 * So, neither the driver nor userspace should hardcode ID numbers to refer
281 * to the entities, but, instead, use the framework to find the ID, when
282 * needed.
283 *
284 * The media_entity name, type and flags fields should be initialized before
285 * calling media_device_register_entity(). Entities embedded in higher-level
286 * standard structures can have some of those fields set by the higher-level
287 * framework.
288 *
289 * If the device has pads, media_entity_pads_init() should be called before
290 * this function. Otherwise, the &media_entity.@pad and &media_entity.@num_pads
291 * should be zeroed before calling this function.
292 *
293 * Entities have flags that describe the entity capabilities and state:
294 *
295 * %MEDIA_ENT_FL_DEFAULT indicates the default entity for a given type.
296 * This can be used to report the default audio and video devices or the
297 * default camera sensor.
d1b9da2d
MCC
298 *
299 * NOTE: Drivers should set the entity function before calling this function.
300 * Please notice that the values %MEDIA_ENT_F_V4L2_SUBDEV_UNKNOWN and
301 * %MEDIA_ENT_F_UNKNOWN should not be used by the drivers.
db7ee32a 302 */
53e269c1
LP
303int __must_check media_device_register_entity(struct media_device *mdev,
304 struct media_entity *entity);
db7ee32a
MCC
305
306/*
307 * media_device_unregister_entity() - unregisters a media entity.
308 *
309 * @entity: pointer to struct &media_entity to be unregistered
310 *
311 * All links associated with the entity and all PADs are automatically
312 * unregistered from the media_device when this function is called.
313 *
314 * Unregistering an entity will not change the IDs of the other entities and
315 * the previoully used ID will never be reused for a newly registered entities.
316 *
317 * When a media device is unregistered, all its entities are unregistered
318 * automatically. No manual entities unregistration is then required.
319 *
320 * Note: the media_entity instance itself must be freed explicitly by
321 * the driver if required.
322 */
53e269c1 323void media_device_unregister_entity(struct media_entity *entity);
b6e4ca81 324
afcbdb55
SK
325/**
326 * media_device_register_entity_notify() - Registers a media entity_notify
327 * callback
328 *
329 * @mdev: The media device
330 * @nptr: The media_entity_notify
331 *
332 * Note: When a new entity is registered, all the registered
333 * media_entity_notify callbacks are invoked.
334 */
335
336int __must_check media_device_register_entity_notify(struct media_device *mdev,
337 struct media_entity_notify *nptr);
338
339/**
340 * media_device_unregister_entity_notify() - Unregister a media entity notify
341 * callback
342 *
343 * @mdev: The media device
344 * @nptr: The media_entity_notify
345 *
346 */
347void media_device_unregister_entity_notify(struct media_device *mdev,
348 struct media_entity_notify *nptr);
349
b6e4ca81
MCC
350/**
351 * media_device_get_devres() - get media device as device resource
352 * creates if one doesn't exist
353 *
354 * @dev: pointer to struct &device.
355 *
356 * Sometimes, the media controller &media_device needs to be shared by more
357 * than one driver. This function adds support for that, by dynamically
358 * allocating the &media_device and allowing it to be obtained from the
359 * struct &device associated with the common device where all sub-device
360 * components belong. So, for example, on an USB device with multiple
361 * interfaces, each interface may be handled by a separate per-interface
362 * drivers. While each interface have its own &device, they all share a
363 * common &device associated with the hole USB device.
364 */
d062f911 365struct media_device *media_device_get_devres(struct device *dev);
b6e4ca81
MCC
366
367/**
368 * media_device_find_devres() - find media device as device resource
369 *
370 * @dev: pointer to struct &device.
371 */
d062f911 372struct media_device *media_device_find_devres(struct device *dev);
53e269c1
LP
373
374/* Iterate over all entities. */
375#define media_device_for_each_entity(entity, mdev) \
05bfa9fa 376 list_for_each_entry(entity, &(mdev)->entities, graph_obj.list)
53e269c1 377
cf975a4b
MCC
378/* Iterate over all interfaces. */
379#define media_device_for_each_intf(intf, mdev) \
05bfa9fa 380 list_for_each_entry(intf, &(mdev)->interfaces, graph_obj.list)
cf975a4b 381
9155d859
MCC
382/* Iterate over all pads. */
383#define media_device_for_each_pad(pad, mdev) \
384 list_for_each_entry(pad, &(mdev)->pads, graph_obj.list)
385
386/* Iterate over all links. */
387#define media_device_for_each_link(link, mdev) \
388 list_for_each_entry(link, &(mdev)->links, graph_obj.list)
41b44e35
MCC
389
390/**
391 * media_device_pci_init() - create and initialize a
392 * struct &media_device from a PCI device.
393 *
6cf5dad1 394 * @mdev: pointer to struct &media_device
41b44e35
MCC
395 * @pci_dev: pointer to struct pci_dev
396 * @name: media device name. If %NULL, the routine will use the default
397 * name for the pci device, given by pci_name() macro.
398 */
6cf5dad1
MCC
399void media_device_pci_init(struct media_device *mdev,
400 struct pci_dev *pci_dev,
401 const char *name);
41b44e35
MCC
402/**
403 * __media_device_usb_init() - create and initialize a
404 * struct &media_device from a PCI device.
405 *
6cf5dad1 406 * @mdev: pointer to struct &media_device
41b44e35
MCC
407 * @udev: pointer to struct usb_device
408 * @board_name: media device name. If %NULL, the routine will use the usb
409 * product name, if available.
410 * @driver_name: name of the driver. if %NULL, the routine will use the name
411 * given by udev->dev->driver->name, with is usually the wrong
412 * thing to do.
413 *
414 * NOTE: It is better to call media_device_usb_init() instead, as
415 * such macro fills driver_name with %KBUILD_MODNAME.
416 */
6cf5dad1
MCC
417void __media_device_usb_init(struct media_device *mdev,
418 struct usb_device *udev,
419 const char *board_name,
420 const char *driver_name);
41b44e35 421
e576d60b
SK
422#else
423static inline int media_device_register(struct media_device *mdev)
424{
425 return 0;
426}
427static inline void media_device_unregister(struct media_device *mdev)
428{
429}
430static inline int media_device_register_entity(struct media_device *mdev,
431 struct media_entity *entity)
432{
433 return 0;
434}
435static inline void media_device_unregister_entity(struct media_entity *entity)
436{
437}
afcbdb55
SK
438static inline int media_device_register_entity_notify(
439 struct media_device *mdev,
440 struct media_entity_notify *nptr)
441{
442 return 0;
443}
444static inline void media_device_unregister_entity_notify(
445 struct media_device *mdev,
446 struct media_entity_notify *nptr)
447{
448}
e576d60b
SK
449static inline struct media_device *media_device_get_devres(struct device *dev)
450{
451 return NULL;
452}
453static inline struct media_device *media_device_find_devres(struct device *dev)
454{
455 return NULL;
456}
41b44e35 457
6cf5dad1
MCC
458static inline void media_device_pci_init(struct media_device *mdev,
459 struct pci_dev *pci_dev,
460 char *name)
41b44e35 461{
41b44e35
MCC
462}
463
6cf5dad1
MCC
464static inline void __media_device_usb_init(struct media_device *mdev,
465 struct usb_device *udev,
466 char *board_name,
467 char *driver_name)
41b44e35 468{
41b44e35
MCC
469}
470
e576d60b 471#endif /* CONFIG_MEDIA_CONTROLLER */
41b44e35 472
6cf5dad1
MCC
473#define media_device_usb_init(mdev, udev, name) \
474 __media_device_usb_init(mdev, udev, name, KBUILD_MODNAME)
41b44e35 475
176fb0d1 476#endif