Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 1 | Linux kernel media framework |
| 2 | ============================ |
| 3 | |
| 4 | This document describes the Linux kernel media framework, its data structures, |
| 5 | functions and their usage. |
| 6 | |
| 7 | |
| 8 | Introduction |
| 9 | ------------ |
| 10 | |
| 11 | The media controller API is documented in DocBook format in |
| 12 | Documentation/DocBook/v4l/media-controller.xml. This document will focus on |
| 13 | the kernel-side implementation of the media framework. |
| 14 | |
| 15 | |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame^] | 16 | Abstract media device model |
| 17 | --------------------------- |
| 18 | |
| 19 | Discovering a device internal topology, and configuring it at runtime, is one |
| 20 | of the goals of the media framework. To achieve this, hardware devices are |
| 21 | modeled as an oriented graph of building blocks called entities connected |
| 22 | through pads. |
| 23 | |
| 24 | An entity is a basic media hardware building block. It can correspond to |
| 25 | a large variety of logical blocks such as physical hardware devices |
| 26 | (CMOS sensor for instance), logical hardware devices (a building block |
| 27 | in a System-on-Chip image processing pipeline), DMA channels or physical |
| 28 | connectors. |
| 29 | |
| 30 | A pad is a connection endpoint through which an entity can interact with |
| 31 | other entities. Data (not restricted to video) produced by an entity |
| 32 | flows from the entity's output to one or more entity inputs. Pads should |
| 33 | not be confused with physical pins at chip boundaries. |
| 34 | |
| 35 | A link is a point-to-point oriented connection between two pads, either |
| 36 | on the same entity or on different entities. Data flows from a source |
| 37 | pad to a sink pad. |
| 38 | |
| 39 | |
Laurent Pinchart | 176fb0d | 2009-12-09 08:39:58 -0300 | [diff] [blame] | 40 | Media device |
| 41 | ------------ |
| 42 | |
| 43 | A media device is represented by a struct media_device instance, defined in |
| 44 | include/media/media-device.h. Allocation of the structure is handled by the |
| 45 | media device driver, usually by embedding the media_device instance in a |
| 46 | larger driver-specific structure. |
| 47 | |
| 48 | Drivers register media device instances by calling |
| 49 | |
| 50 | media_device_register(struct media_device *mdev); |
| 51 | |
| 52 | The caller is responsible for initializing the media_device structure before |
| 53 | registration. The following fields must be set: |
| 54 | |
| 55 | - dev must point to the parent device (usually a pci_dev, usb_interface or |
| 56 | platform_device instance). |
| 57 | |
| 58 | - model must be filled with the device model name as a NUL-terminated UTF-8 |
| 59 | string. The device/model revision must not be stored in this field. |
| 60 | |
| 61 | The following fields are optional: |
| 62 | |
| 63 | - serial is a unique serial number stored as a NUL-terminated ASCII string. |
| 64 | The field is big enough to store a GUID in text form. If the hardware |
| 65 | doesn't provide a unique serial number this field must be left empty. |
| 66 | |
| 67 | - bus_info represents the location of the device in the system as a |
| 68 | NUL-terminated ASCII string. For PCI/PCIe devices bus_info must be set to |
| 69 | "PCI:" (or "PCIe:") followed by the value of pci_name(). For USB devices, |
| 70 | the usb_make_path() function must be used. This field is used by |
| 71 | applications to distinguish between otherwise identical devices that don't |
| 72 | provide a serial number. |
| 73 | |
| 74 | - hw_revision is the hardware device revision in a driver-specific format. |
| 75 | When possible the revision should be formatted with the KERNEL_VERSION |
| 76 | macro. |
| 77 | |
| 78 | - driver_version is formatted with the KERNEL_VERSION macro. The version |
| 79 | minor must be incremented when new features are added to the userspace API |
| 80 | without breaking binary compatibility. The version major must be |
| 81 | incremented when binary compatibility is broken. |
| 82 | |
| 83 | Upon successful registration a character device named media[0-9]+ is created. |
| 84 | The device major and minor numbers are dynamic. The model name is exported as |
| 85 | a sysfs attribute. |
| 86 | |
| 87 | Drivers unregister media device instances by calling |
| 88 | |
| 89 | media_device_unregister(struct media_device *mdev); |
| 90 | |
| 91 | Unregistering a media device that hasn't been registered is *NOT* safe. |
Laurent Pinchart | 53e269c | 2009-12-09 08:40:00 -0300 | [diff] [blame^] | 92 | |
| 93 | |
| 94 | Entities, pads and links |
| 95 | ------------------------ |
| 96 | |
| 97 | - Entities |
| 98 | |
| 99 | Entities are represented by a struct media_entity instance, defined in |
| 100 | include/media/media-entity.h. The structure is usually embedded into a |
| 101 | higher-level structure, such as a v4l2_subdev or video_device instance, |
| 102 | although drivers can allocate entities directly. |
| 103 | |
| 104 | Drivers initialize entities by calling |
| 105 | |
| 106 | media_entity_init(struct media_entity *entity, u16 num_pads, |
| 107 | struct media_pad *pads, u16 extra_links); |
| 108 | |
| 109 | The media_entity name, type, flags, revision and group_id fields can be |
| 110 | initialized before or after calling media_entity_init. Entities embedded in |
| 111 | higher-level standard structures can have some of those fields set by the |
| 112 | higher-level framework. |
| 113 | |
| 114 | As the number of pads is known in advance, the pads array is not allocated |
| 115 | dynamically but is managed by the entity driver. Most drivers will embed the |
| 116 | pads array in a driver-specific structure, avoiding dynamic allocation. |
| 117 | |
| 118 | Drivers must set the direction of every pad in the pads array before calling |
| 119 | media_entity_init. The function will initialize the other pads fields. |
| 120 | |
| 121 | Unlike the number of pads, the total number of links isn't always known in |
| 122 | advance by the entity driver. As an initial estimate, media_entity_init |
| 123 | pre-allocates a number of links equal to the number of pads plus an optional |
| 124 | number of extra links. The links array will be reallocated if it grows beyond |
| 125 | the initial estimate. |
| 126 | |
| 127 | Drivers register entities with a media device by calling |
| 128 | |
| 129 | media_device_register_entity(struct media_device *mdev, |
| 130 | struct media_entity *entity); |
| 131 | |
| 132 | Entities are identified by a unique positive integer ID. Drivers can provide an |
| 133 | ID by filling the media_entity id field prior to registration, or request the |
| 134 | media controller framework to assign an ID automatically. Drivers that provide |
| 135 | IDs manually must ensure that all IDs are unique. IDs are not guaranteed to be |
| 136 | contiguous even when they are all assigned automatically by the framework. |
| 137 | |
| 138 | Drivers unregister entities by calling |
| 139 | |
| 140 | media_device_unregister_entity(struct media_entity *entity); |
| 141 | |
| 142 | Unregistering an entity will not change the IDs of the other entities, and the |
| 143 | ID will never be reused for a newly registered entity. |
| 144 | |
| 145 | When a media device is unregistered, all its entities are unregistered |
| 146 | automatically. No manual entities unregistration is then required. |
| 147 | |
| 148 | Drivers free resources associated with an entity by calling |
| 149 | |
| 150 | media_entity_cleanup(struct media_entity *entity); |
| 151 | |
| 152 | This function must be called during the cleanup phase after unregistering the |
| 153 | entity. Note that the media_entity instance itself must be freed explicitly by |
| 154 | the driver if required. |
| 155 | |
| 156 | Entities have flags that describe the entity capabilities and state. |
| 157 | |
| 158 | MEDIA_ENT_FL_DEFAULT indicates the default entity for a given type. |
| 159 | This can be used to report the default audio and video devices or the |
| 160 | default camera sensor. |
| 161 | |
| 162 | Logical entity groups can be defined by setting the group ID of all member |
| 163 | entities to the same non-zero value. An entity group serves no purpose in the |
| 164 | kernel, but is reported to userspace during entities enumeration. The group_id |
| 165 | field belongs to the media device driver and must not by touched by entity |
| 166 | drivers. |
| 167 | |
| 168 | Media device drivers should define groups if several entities are logically |
| 169 | bound together. Example usages include reporting |
| 170 | |
| 171 | - ALSA, VBI and video nodes that carry the same media stream |
| 172 | - lens and flash controllers associated with a sensor |
| 173 | |
| 174 | - Pads |
| 175 | |
| 176 | Pads are represented by a struct media_pad instance, defined in |
| 177 | include/media/media-entity.h. Each entity stores its pads in a pads array |
| 178 | managed by the entity driver. Drivers usually embed the array in a |
| 179 | driver-specific structure. |
| 180 | |
| 181 | Pads are identified by their entity and their 0-based index in the pads array. |
| 182 | Both information are stored in the media_pad structure, making the media_pad |
| 183 | pointer the canonical way to store and pass link references. |
| 184 | |
| 185 | Pads have flags that describe the pad capabilities and state. |
| 186 | |
| 187 | MEDIA_PAD_FL_SINK indicates that the pad supports sinking data. |
| 188 | MEDIA_PAD_FL_SOURCE indicates that the pad supports sourcing data. |
| 189 | |
| 190 | One and only one of MEDIA_PAD_FL_SINK and MEDIA_PAD_FL_SOURCE must be set for |
| 191 | each pad. |
| 192 | |
| 193 | - Links |
| 194 | |
| 195 | Links are represented by a struct media_link instance, defined in |
| 196 | include/media/media-entity.h. Each entity stores all links originating at or |
| 197 | targetting any of its pads in a links array. A given link is thus stored |
| 198 | twice, once in the source entity and once in the target entity. The array is |
| 199 | pre-allocated and grows dynamically as needed. |
| 200 | |
| 201 | Drivers create links by calling |
| 202 | |
| 203 | media_entity_create_link(struct media_entity *source, u16 source_pad, |
| 204 | struct media_entity *sink, u16 sink_pad, |
| 205 | u32 flags); |
| 206 | |
| 207 | An entry in the link array of each entity is allocated and stores pointers |
| 208 | to source and sink pads. |
| 209 | |
| 210 | Links have flags that describe the link capabilities and state. |
| 211 | |
| 212 | MEDIA_LNK_FL_ENABLED indicates that the link is enabled and can be used |
| 213 | to transfer media data. When two or more links target a sink pad, only |
| 214 | one of them can be enabled at a time. |
| 215 | MEDIA_LNK_FL_IMMUTABLE indicates that the link enabled state can't be |
| 216 | modified at runtime. If MEDIA_LNK_FL_IMMUTABLE is set, then |
| 217 | MEDIA_LNK_FL_ENABLED must also be set since an immutable link is always |
| 218 | enabled. |