blob: 006ae274d22ef14155c98c11bf8ac2a335cb2024 [file] [log] [blame]
Laurent Pinchartc0efd232008-06-30 15:04:50 -03001#ifndef _USB_VIDEO_H_
2#define _USB_VIDEO_H_
3
Laurent Pinchart227bd5b2011-07-30 16:19:49 -03004#ifndef __KERNEL__
5#error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
6#endif /* __KERNEL__ */
7
Laurent Pinchartc0efd232008-06-30 15:04:50 -03008#include <linux/kernel.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -03009#include <linux/poll.h>
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -030010#include <linux/usb.h>
Laurent Pinchartde05f632009-06-26 12:15:38 -030011#include <linux/usb/video.h>
Laurent Pinchart5f708812010-11-21 17:15:44 -030012#include <linux/uvcvideo.h>
Laurent Pinchart227bd5b2011-07-30 16:19:49 -030013#include <linux/videodev2.h>
Laurent Pinchart5a254d72010-01-21 05:39:47 -030014#include <media/media-device.h>
15#include <media/v4l2-device.h>
Hans de Goedeb4012002012-04-08 12:59:51 -030016#include <media/v4l2-event.h>
17#include <media/v4l2-fh.h>
Laurent Pinchart6998b6f2011-10-24 11:53:59 -030018#include <media/videobuf2-core.h>
Laurent Pinchartc0efd232008-06-30 15:04:50 -030019
20/* --------------------------------------------------------------------------
21 * UVC constants
22 */
23
Laurent Pinchartc0efd232008-06-30 15:04:50 -030024#define UVC_TERM_INPUT 0x0000
25#define UVC_TERM_OUTPUT 0x8000
Laurent Pinchart6241d8c2009-11-25 12:00:22 -030026#define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030027
28#define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
29#define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
30#define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
31#define UVC_ENTITY_IS_ITERM(entity) \
Laurent Pinchart8e113592009-07-01 20:24:47 -030032 (UVC_ENTITY_IS_TERM(entity) && \
33 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030034#define UVC_ENTITY_IS_OTERM(entity) \
Laurent Pinchart8e113592009-07-01 20:24:47 -030035 (UVC_ENTITY_IS_TERM(entity) && \
36 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
Laurent Pinchartc0efd232008-06-30 15:04:50 -030037
Laurent Pinchartc0efd232008-06-30 15:04:50 -030038
39/* ------------------------------------------------------------------------
40 * GUIDs
41 */
42#define UVC_GUID_UVC_CAMERA \
43 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
45#define UVC_GUID_UVC_OUTPUT \
46 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
48#define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
49 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
51#define UVC_GUID_UVC_PROCESSING \
52 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
54#define UVC_GUID_UVC_SELECTOR \
55 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
57
Laurent Pinchartc0efd232008-06-30 15:04:50 -030058#define UVC_GUID_FORMAT_MJPEG \
59 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
60 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
61#define UVC_GUID_FORMAT_YUY2 \
62 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
63 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Daniel Ritz68f194e2009-12-12 14:57:17 -030064#define UVC_GUID_FORMAT_YUY2_ISIGHT \
65 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
66 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
Laurent Pinchartc0efd232008-06-30 15:04:50 -030067#define UVC_GUID_FORMAT_NV12 \
68 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
69 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
70#define UVC_GUID_FORMAT_YV12 \
71 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
72 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
73#define UVC_GUID_FORMAT_I420 \
74 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
75 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
76#define UVC_GUID_FORMAT_UYVY \
77 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
78 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
79#define UVC_GUID_FORMAT_Y800 \
80 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
81 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Stefan Muenzelca47e712012-07-28 19:49:14 -030082#define UVC_GUID_FORMAT_Y8 \
83 { 'Y', '8', ' ', ' ', 0x00, 0x00, 0x10, 0x00, \
84 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
85#define UVC_GUID_FORMAT_Y10 \
86 { 'Y', '1', '0', ' ', 0x00, 0x00, 0x10, 0x00, \
87 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
88#define UVC_GUID_FORMAT_Y12 \
89 { 'Y', '1', '2', ' ', 0x00, 0x00, 0x10, 0x00, \
90 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Laurent Pinchart614212062010-04-12 10:41:22 -030091#define UVC_GUID_FORMAT_Y16 \
92 { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
93 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Laurent Pinchartc0efd232008-06-30 15:04:50 -030094#define UVC_GUID_FORMAT_BY8 \
95 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
96 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Laurent Pinchart50791072011-03-06 08:32:04 -030097#define UVC_GUID_FORMAT_RGBP \
98 { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
99 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Hans de Goede7225a1d2011-05-11 09:56:20 -0300100#define UVC_GUID_FORMAT_M420 \
101 { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
102 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300103
Stephan Lachowsky25ad8a82011-01-28 16:38:58 -0300104#define UVC_GUID_FORMAT_H264 \
105 { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, \
106 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
107
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300108/* ------------------------------------------------------------------------
109 * Driver specific constants.
110 */
111
Mauro Carvalho Chehabfd3e5822011-06-25 13:50:37 -0300112#define DRIVER_VERSION "1.1.1"
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300113
114/* Number of isochronous URBs. */
115#define UVC_URBS 5
Laurent Pinchartefdc8a92009-01-18 17:46:30 -0300116/* Maximum number of packets per URB. */
117#define UVC_MAX_PACKETS 32
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300118/* Maximum number of video buffers. */
119#define UVC_MAX_VIDEO_BUFFERS 32
Ming Leia31a4052008-09-16 03:32:20 -0300120/* Maximum status buffer size in bytes of interrupt URB. */
121#define UVC_MAX_STATUS_SIZE 16
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300122
123#define UVC_CTRL_CONTROL_TIMEOUT 300
Laurent Pinchart56d15cd2010-01-19 08:59:21 -0300124#define UVC_CTRL_STREAMING_TIMEOUT 5000
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300125
Martin Rubli8fb91b32010-09-08 04:15:23 -0300126/* Maximum allowed number of control mappings per device */
127#define UVC_MAX_CONTROL_MAPPINGS 1024
Haogang Chen806e23e2011-11-29 18:32:25 -0300128#define UVC_MAX_CONTROL_MENU_ENTRIES 32
Martin Rubli8fb91b32010-09-08 04:15:23 -0300129
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300130/* Devices quirks */
131#define UVC_QUIRK_STATUS_INTERVAL 0x00000001
132#define UVC_QUIRK_PROBE_MINMAX 0x00000002
133#define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
134#define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
135#define UVC_QUIRK_STREAM_NO_FID 0x00000010
136#define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
Laurent Pinchart50144ae2009-02-16 17:41:52 -0300137#define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
Laurent Pinchartbab6f662009-07-19 18:39:56 -0300138#define UVC_QUIRK_PROBE_DEF 0x00000100
Laurent Pinchart86d8b6a2010-09-16 12:37:26 -0300139#define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300140
141/* Format flags */
142#define UVC_FMT_FLAG_COMPRESSED 0x00000001
143#define UVC_FMT_FLAG_STREAM 0x00000002
144
145/* ------------------------------------------------------------------------
146 * Structures.
147 */
148
149struct uvc_device;
150
151/* TODO: Put the most frequently accessed fields at the beginning of
152 * structures to maximize cache efficiency.
153 */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300154struct uvc_control_info {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300155 struct list_head mappings;
156
157 __u8 entity[16];
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300158 __u8 index; /* Bit index in bmControls */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300159 __u8 selector;
160
161 __u16 size;
162 __u32 flags;
163};
164
165struct uvc_control_mapping {
166 struct list_head list;
Hans de Goedeb4012002012-04-08 12:59:51 -0300167 struct list_head ev_subs;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300168
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300169 __u32 id;
170 __u8 name[32];
171 __u8 entity[16];
172 __u8 selector;
173
174 __u8 size;
175 __u8 offset;
176 enum v4l2_ctrl_type v4l2_type;
177 __u32 data_type;
178
179 struct uvc_menu_info *menu_info;
180 __u32 menu_count;
Laurent Pinchart97683522008-12-16 06:46:32 -0300181
Hans de Goede55afeb82012-04-08 12:59:52 -0300182 __u32 master_id;
183 __s32 master_manual;
184 __u32 slave_ids[2];
185
Laurent Pinchart97683522008-12-16 06:46:32 -0300186 __s32 (*get) (struct uvc_control_mapping *mapping, __u8 query,
187 const __u8 *data);
188 void (*set) (struct uvc_control_mapping *mapping, __s32 value,
189 __u8 *data);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300190};
191
192struct uvc_control {
193 struct uvc_entity *entity;
Laurent Pinchart071c8bb2010-09-29 16:00:08 -0300194 struct uvc_control_info info;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300195
196 __u8 index; /* Used to match the uvc_control entry with a
197 uvc_control_info. */
Laurent Pinchart071c8bb2010-09-29 16:00:08 -0300198 __u8 dirty:1,
199 loaded:1,
200 modified:1,
201 cached:1,
202 initialized:1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300203
Laurent Pinchartb30ece52010-06-18 11:31:24 -0300204 __u8 *uvc_data;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300205};
206
207struct uvc_format_desc {
208 char *name;
209 __u8 guid[16];
210 __u32 fcc;
211};
212
213/* The term 'entity' refers to both UVC units and UVC terminals.
214 *
215 * The type field is either the terminal type (wTerminalType in the terminal
216 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
217 * As the bDescriptorSubtype field is one byte long, the type value will
218 * always have a null MSB for units. All terminal types defined by the UVC
219 * specification have a non-null MSB, so it is safe to use the MSB to
220 * differentiate between units and terminals as long as the descriptor parsing
221 * code makes sure terminal types have a non-null MSB.
222 *
223 * For terminals, the type's most significant bit stores the terminal
224 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
225 * always be accessed with the UVC_ENTITY_* macros and never directly.
226 */
227
228struct uvc_entity {
229 struct list_head list; /* Entity as part of a UVC device. */
230 struct list_head chain; /* Entity as part of a video device
231 * chain. */
232 __u8 id;
233 __u16 type;
234 char name[64];
235
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -0300236 /* Media controller-related fields. */
Laurent Pinchart8a65a942010-01-21 08:56:19 -0300237 struct video_device *vdev;
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -0300238 struct v4l2_subdev subdev;
239 unsigned int num_pads;
240 unsigned int num_links;
241 struct media_pad *pads;
242
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300243 union {
244 struct {
245 __u16 wObjectiveFocalLengthMin;
246 __u16 wObjectiveFocalLengthMax;
247 __u16 wOcularFocalLength;
248 __u8 bControlSize;
249 __u8 *bmControls;
250 } camera;
251
252 struct {
253 __u8 bControlSize;
254 __u8 *bmControls;
255 __u8 bTransportModeSize;
256 __u8 *bmTransportModes;
257 } media;
258
259 struct {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300260 } output;
261
262 struct {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300263 __u16 wMaxMultiplier;
264 __u8 bControlSize;
265 __u8 *bmControls;
266 __u8 bmVideoStandards;
267 } processing;
268
269 struct {
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300270 } selector;
271
272 struct {
273 __u8 guidExtensionCode[16];
274 __u8 bNumControls;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300275 __u8 bControlSize;
276 __u8 *bmControls;
277 __u8 *bmControlsType;
278 } extension;
279 };
280
Laurent Pinchart8ca5a632009-11-25 12:00:30 -0300281 __u8 bNrInPins;
282 __u8 *baSourceID;
283
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300284 unsigned int ncontrols;
285 struct uvc_control *controls;
286};
287
288struct uvc_frame {
289 __u8 bFrameIndex;
290 __u8 bmCapabilities;
291 __u16 wWidth;
292 __u16 wHeight;
293 __u32 dwMinBitRate;
294 __u32 dwMaxBitRate;
295 __u32 dwMaxVideoFrameBufferSize;
296 __u8 bFrameIntervalType;
297 __u32 dwDefaultFrameInterval;
298 __u32 *dwFrameInterval;
299};
300
301struct uvc_format {
302 __u8 type;
303 __u8 index;
304 __u8 bpp;
305 __u8 colorspace;
306 __u32 fcc;
307 __u32 flags;
308
309 char name[32];
310
311 unsigned int nframes;
312 struct uvc_frame *frame;
313};
314
315struct uvc_streaming_header {
316 __u8 bNumFormats;
317 __u8 bEndpointAddress;
318 __u8 bTerminalLink;
319 __u8 bControlSize;
320 __u8 *bmaControls;
321 /* The following fields are used by input headers only. */
322 __u8 bmInfo;
323 __u8 bStillCaptureMethod;
324 __u8 bTriggerSupport;
325 __u8 bTriggerUsage;
326};
327
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300328enum uvc_buffer_state {
Laurent Pinchart2c2d2642009-01-03 19:12:40 -0300329 UVC_BUF_STATE_IDLE = 0,
330 UVC_BUF_STATE_QUEUED = 1,
331 UVC_BUF_STATE_ACTIVE = 2,
Laurent Pinchartd7c0d432009-12-16 21:20:45 -0300332 UVC_BUF_STATE_READY = 3,
333 UVC_BUF_STATE_DONE = 4,
334 UVC_BUF_STATE_ERROR = 5,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300335};
336
337struct uvc_buffer {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300338 struct vb2_buffer buf;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300339 struct list_head queue;
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300340
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300341 enum uvc_buffer_state state;
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300342 unsigned int error;
Laurent Pinchart3d95e932011-10-24 11:49:19 -0300343
344 void *mem;
345 unsigned int length;
346 unsigned int bytesused;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300347
348 u32 pts;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300349};
350
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300351#define UVC_QUEUE_DISCONNECTED (1 << 0)
352#define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300353
354struct uvc_video_queue {
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300355 struct vb2_queue queue;
356 struct mutex mutex; /* Protects queue */
Laurent Pinchartff924202008-12-28 22:32:29 -0300357
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300358 unsigned int flags;
Laurent Pinchartff924202008-12-28 22:32:29 -0300359 unsigned int buf_used;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300360
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300361 spinlock_t irqlock; /* Protects irqqueue */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300362 struct list_head irqqueue;
363};
364
Laurent Pinchart8e113592009-07-01 20:24:47 -0300365struct uvc_video_chain {
366 struct uvc_device *dev;
367 struct list_head list;
368
Laurent Pinchart6241d8c2009-11-25 12:00:22 -0300369 struct list_head entities; /* All entities */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300370 struct uvc_entity *processing; /* Processing unit */
371 struct uvc_entity *selector; /* Selector unit */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300372
Laurent Pinchart27a61c12010-10-02 09:04:53 -0300373 struct mutex ctrl_mutex; /* Protects ctrl.info */
Laurent Pinchartf887e992012-08-28 18:38:58 -0300374
Laurent Pinchart05505132012-08-28 20:29:56 -0300375 struct v4l2_prio_state prio; /* V4L2 priority state */
Laurent Pinchartf887e992012-08-28 18:38:58 -0300376 u32 caps; /* V4L2 chain-wide caps */
Laurent Pinchart8e113592009-07-01 20:24:47 -0300377};
378
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300379struct uvc_stats_frame {
380 unsigned int size; /* Number of bytes captured */
381 unsigned int first_data; /* Index of the first non-empty packet */
382
383 unsigned int nb_packets; /* Number of packets */
384 unsigned int nb_empty; /* Number of empty packets */
385 unsigned int nb_invalid; /* Number of packets with an invalid header */
386 unsigned int nb_errors; /* Number of packets with the error bit set */
Laurent Pinchart25738cbd2011-11-03 12:30:17 -0300387
388 unsigned int nb_pts; /* Number of packets with a PTS timestamp */
389 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
390 unsigned int last_pts_diff; /* Index of the last PTS difference */
391 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
392 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
393 u32 pts; /* PTS of the last packet */
394
395 unsigned int nb_scr; /* Number of packets with a SCR timestamp */
396 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
397 u16 scr_sof; /* SCR.SOF of the last packet */
398 u32 scr_stc; /* SCR.STC of the last packet */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300399};
400
401struct uvc_stats_stream {
402 struct timespec start_ts; /* Stream start timestamp */
403 struct timespec stop_ts; /* Stream stop timestamp */
404
405 unsigned int nb_frames; /* Number of frames */
406
407 unsigned int nb_packets; /* Number of packets */
408 unsigned int nb_empty; /* Number of empty packets */
409 unsigned int nb_invalid; /* Number of packets with an invalid header */
410 unsigned int nb_errors; /* Number of packets with the error bit set */
Laurent Pinchart25738cbd2011-11-03 12:30:17 -0300411
412 unsigned int nb_pts_constant; /* Number of frames with constant PTS */
413 unsigned int nb_pts_early; /* Number of frames with early PTS */
414 unsigned int nb_pts_initial; /* Number of frames with initial PTS */
415
416 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
417 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
418 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
419 unsigned int scr_sof; /* STC.SOF of the last packet */
420 unsigned int min_sof; /* Minimum STC.SOF value */
421 unsigned int max_sof; /* Maximum STC.SOF value */
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300422};
423
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300424struct uvc_streaming {
425 struct list_head list;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300426 struct uvc_device *dev;
427 struct video_device *vdev;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300428 struct uvc_video_chain *chain;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300429 atomic_t active;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300430
431 struct usb_interface *intf;
432 int intfnum;
433 __u16 maxpsize;
434
435 struct uvc_streaming_header header;
436 enum v4l2_buf_type type;
437
438 unsigned int nformats;
439 struct uvc_format *format;
440
441 struct uvc_streaming_control ctrl;
Laurent Pinchart815adc42012-08-28 18:38:58 -0300442 struct uvc_format *def_format;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300443 struct uvc_format *cur_format;
444 struct uvc_frame *cur_frame;
Laurent Pinchart69477562010-11-21 13:36:34 -0300445 /* Protect access to ctrl, cur_format, cur_frame and hardware video
446 * probe control.
447 */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300448 struct mutex mutex;
449
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300450 /* Buffers queue. */
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300451 unsigned int frozen : 1;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300452 struct uvc_video_queue queue;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300453 void (*decode) (struct urb *urb, struct uvc_streaming *video,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300454 struct uvc_buffer *buf);
455
456 /* Context data used by the bulk completion handler. */
457 struct {
458 __u8 header[256];
459 unsigned int header_size;
460 int skip_payload;
461 __u32 payload_size;
462 __u32 max_payload_size;
463 } bulk;
464
465 struct urb *urb[UVC_URBS];
466 char *urb_buffer[UVC_URBS];
Laurent Pincharte01117c2008-07-04 00:36:21 -0300467 dma_addr_t urb_dma[UVC_URBS];
468 unsigned int urb_size;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300469
Laurent Pinchart650b95f2010-10-02 11:06:05 -0300470 __u32 sequence;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300471 __u8 last_fid;
Alexey Fisheredbaa392011-11-03 09:39:37 -0300472
473 /* debugfs */
474 struct dentry *debugfs_dir;
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300475 struct {
476 struct uvc_stats_frame frame;
477 struct uvc_stats_stream stream;
478 } stats;
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300479
480 /* Timestamps support. */
481 struct uvc_clock {
482 struct uvc_clock_sample {
483 u32 dev_stc;
484 u16 dev_sof;
485 struct timespec host_ts;
486 u16 host_sof;
487 } *samples;
488
489 unsigned int head;
490 unsigned int count;
491 unsigned int size;
492
493 u16 last_sof;
494 u16 sof_offset;
495
496 spinlock_t lock;
497 } clock;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300498};
499
500enum uvc_device_state {
501 UVC_DEV_DISCONNECTED = 1,
502};
503
504struct uvc_device {
505 struct usb_device *udev;
506 struct usb_interface *intf;
Laurent Pinchart44f00792008-11-08 19:14:50 -0300507 unsigned long warnings;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300508 __u32 quirks;
509 int intfnum;
510 char name[32];
511
512 enum uvc_device_state state;
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300513 atomic_t users;
Martin Rubli8fb91b32010-09-08 04:15:23 -0300514 atomic_t nmappings;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300515
516 /* Video control interface */
Laurent Pinchart5a254d72010-01-21 05:39:47 -0300517#ifdef CONFIG_MEDIA_CONTROLLER
518 struct media_device mdev;
519#endif
520 struct v4l2_device vdev;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300521 __u16 uvc_version;
522 __u32 clock_frequency;
523
524 struct list_head entities;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300525 struct list_head chains;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300526
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300527 /* Video Streaming interfaces */
528 struct list_head streams;
Laurent Pinchart716fdee2009-09-29 21:07:19 -0300529 atomic_t nstreams;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300530
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300531 /* Status Interrupt Endpoint */
532 struct usb_host_endpoint *int_ep;
533 struct urb *int_urb;
Ming Leia31a4052008-09-16 03:32:20 -0300534 __u8 *status;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300535 struct input_dev *input;
Laurent Pinchartf1801522009-01-22 12:45:10 -0300536 char input_phys[64];
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300537};
538
539enum uvc_handle_state {
540 UVC_HANDLE_PASSIVE = 0,
541 UVC_HANDLE_ACTIVE = 1,
542};
543
544struct uvc_fh {
Hans de Goedeb4012002012-04-08 12:59:51 -0300545 struct v4l2_fh vfh;
Laurent Pinchart8e113592009-07-01 20:24:47 -0300546 struct uvc_video_chain *chain;
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300547 struct uvc_streaming *stream;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300548 enum uvc_handle_state state;
549};
550
551struct uvc_driver {
552 struct usb_driver driver;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300553};
554
555/* ------------------------------------------------------------------------
556 * Debugging, printing and logging
557 */
558
559#define UVC_TRACE_PROBE (1 << 0)
560#define UVC_TRACE_DESCR (1 << 1)
561#define UVC_TRACE_CONTROL (1 << 2)
562#define UVC_TRACE_FORMAT (1 << 3)
563#define UVC_TRACE_CAPTURE (1 << 4)
564#define UVC_TRACE_CALLS (1 << 5)
565#define UVC_TRACE_IOCTL (1 << 6)
566#define UVC_TRACE_FRAME (1 << 7)
567#define UVC_TRACE_SUSPEND (1 << 8)
568#define UVC_TRACE_STATUS (1 << 9)
Laurent Pinchart663a4192009-08-06 11:41:17 -0300569#define UVC_TRACE_VIDEO (1 << 10)
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300570#define UVC_TRACE_STATS (1 << 11)
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300571#define UVC_TRACE_CLOCK (1 << 12)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300572
Laurent Pinchart44f00792008-11-08 19:14:50 -0300573#define UVC_WARN_MINMAX 0
574#define UVC_WARN_PROBE_DEF 1
Laurent Pinchartc4d99f82010-09-30 09:04:03 -0300575#define UVC_WARN_XU_GET_RES 2
Laurent Pinchart44f00792008-11-08 19:14:50 -0300576
Laurent Pinchart310fe522009-12-09 22:57:48 -0300577extern unsigned int uvc_clock_param;
Laurent Pinchart0fbd8ee2008-12-06 16:25:14 -0300578extern unsigned int uvc_no_drop_param;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300579extern unsigned int uvc_trace_param;
Laurent Pinchartb232a012009-10-09 20:55:23 -0300580extern unsigned int uvc_timeout_param;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300581
582#define uvc_trace(flag, msg...) \
583 do { \
584 if (uvc_trace_param & flag) \
585 printk(KERN_DEBUG "uvcvideo: " msg); \
586 } while (0)
587
Laurent Pinchart44f00792008-11-08 19:14:50 -0300588#define uvc_warn_once(dev, warn, msg...) \
589 do { \
590 if (!test_and_set_bit(warn, &dev->warnings)) \
591 printk(KERN_INFO "uvcvideo: " msg); \
592 } while (0)
593
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300594#define uvc_printk(level, msg...) \
595 printk(level "uvcvideo: " msg)
596
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300597/* --------------------------------------------------------------------------
598 * Internal functions.
599 */
600
601/* Core driver */
602extern struct uvc_driver uvc_driver;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300603
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -0300604extern struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
605
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300606/* Video buffers queue management. */
Ezequiel Garcia57126612012-09-26 07:30:34 -0300607extern int uvc_queue_init(struct uvc_video_queue *queue,
Laurent Pinchart9bde9f22010-06-17 06:52:37 -0300608 enum v4l2_buf_type type, int drop_corrupted);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300609extern int uvc_alloc_buffers(struct uvc_video_queue *queue,
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300610 struct v4l2_requestbuffers *rb);
611extern void uvc_free_buffers(struct uvc_video_queue *queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300612extern int uvc_query_buffer(struct uvc_video_queue *queue,
613 struct v4l2_buffer *v4l2_buf);
614extern int uvc_queue_buffer(struct uvc_video_queue *queue,
615 struct v4l2_buffer *v4l2_buf);
616extern int uvc_dequeue_buffer(struct uvc_video_queue *queue,
617 struct v4l2_buffer *v4l2_buf, int nonblocking);
618extern int uvc_queue_enable(struct uvc_video_queue *queue, int enable);
619extern void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
620extern struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
621 struct uvc_buffer *buf);
Laurent Pinchart4aa27592010-11-21 15:18:08 -0300622extern int uvc_queue_mmap(struct uvc_video_queue *queue,
623 struct vm_area_struct *vma);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300624extern unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
625 struct file *file, poll_table *wait);
Bob Liu72969442011-04-29 07:11:35 -0300626#ifndef CONFIG_MMU
627extern unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
628 unsigned long pgoff);
629#endif
Laurent Pinchart23ff6042009-06-04 09:26:39 -0300630extern int uvc_queue_allocated(struct uvc_video_queue *queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300631static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
632{
Laurent Pinchart6998b6f2011-10-24 11:53:59 -0300633 return vb2_is_streaming(&queue->queue);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300634}
635
636/* V4L2 interface */
Hans Verkuilbec43662008-12-30 06:58:20 -0300637extern const struct v4l2_file_operations uvc_fops;
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300638
Laurent Pinchart4ffc2d82010-01-21 05:39:52 -0300639/* Media controller */
640extern int uvc_mc_register_entities(struct uvc_video_chain *chain);
641extern void uvc_mc_cleanup_entity(struct uvc_entity *entity);
642
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300643/* Video */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300644extern int uvc_video_init(struct uvc_streaming *stream);
645extern int uvc_video_suspend(struct uvc_streaming *stream);
Ming Leid59a7b12011-07-16 00:51:00 -0300646extern int uvc_video_resume(struct uvc_streaming *stream, int reset);
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300647extern int uvc_video_enable(struct uvc_streaming *stream, int enable);
648extern int uvc_probe_video(struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300649 struct uvc_streaming_control *probe);
650extern int uvc_query_ctrl(struct uvc_device *dev, __u8 query, __u8 unit,
651 __u8 intfnum, __u8 cs, void *data, __u16 size);
Laurent Pinchart66847ef2011-09-24 10:46:55 -0300652void uvc_video_clock_update(struct uvc_streaming *stream,
653 struct v4l2_buffer *v4l2_buf,
654 struct uvc_buffer *buf);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300655
656/* Status */
657extern int uvc_status_init(struct uvc_device *dev);
658extern void uvc_status_cleanup(struct uvc_device *dev);
Laurent Pinchart04a37e02009-05-19 10:08:03 -0300659extern int uvc_status_start(struct uvc_device *dev);
660extern void uvc_status_stop(struct uvc_device *dev);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300661extern int uvc_status_suspend(struct uvc_device *dev);
662extern int uvc_status_resume(struct uvc_device *dev);
663
664/* Controls */
Hans de Goedeb4012002012-04-08 12:59:51 -0300665extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
666
Laurent Pinchart8e113592009-07-01 20:24:47 -0300667extern int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300668 struct v4l2_queryctrl *v4l2_ctrl);
Laurent Pinchart23d9f3e2010-11-21 07:58:54 -0300669extern int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
670 struct v4l2_querymenu *query_menu);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300671
Laurent Pinchartba2fa992010-09-20 05:53:21 -0300672extern int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
673 const struct uvc_control_mapping *mapping);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300674extern int uvc_ctrl_init_device(struct uvc_device *dev);
675extern void uvc_ctrl_cleanup_device(struct uvc_device *dev);
676extern int uvc_ctrl_resume_device(struct uvc_device *dev);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300677
Laurent Pinchart8e113592009-07-01 20:24:47 -0300678extern int uvc_ctrl_begin(struct uvc_video_chain *chain);
Hans de Goedeb4012002012-04-08 12:59:51 -0300679extern int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
680 const struct v4l2_ext_control *xctrls,
681 unsigned int xctrls_count);
682static inline int uvc_ctrl_commit(struct uvc_fh *handle,
683 const struct v4l2_ext_control *xctrls,
684 unsigned int xctrls_count)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300685{
Hans de Goedeb4012002012-04-08 12:59:51 -0300686 return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300687}
Hans de Goedeb4012002012-04-08 12:59:51 -0300688static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300689{
Hans de Goedeb4012002012-04-08 12:59:51 -0300690 return __uvc_ctrl_commit(handle, 1, NULL, 0);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300691}
692
Laurent Pinchart8e113592009-07-01 20:24:47 -0300693extern int uvc_ctrl_get(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300694 struct v4l2_ext_control *xctrl);
Laurent Pinchart8e113592009-07-01 20:24:47 -0300695extern int uvc_ctrl_set(struct uvc_video_chain *chain,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300696 struct v4l2_ext_control *xctrl);
697
Laurent Pinchart8e113592009-07-01 20:24:47 -0300698extern int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
Martin Rublife78d182010-10-02 19:10:16 -0300699 struct uvc_xu_control_query *xqry);
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300700
701/* Utility functions */
702extern void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
703 unsigned int n_terms, unsigned int threshold);
704extern uint32_t uvc_fraction_to_interval(uint32_t numerator,
705 uint32_t denominator);
706extern struct usb_host_endpoint *uvc_find_endpoint(
707 struct usb_host_interface *alts, __u8 epaddr);
708
709/* Quirks support */
Laurent Pinchart35f02a62009-06-28 08:37:50 -0300710void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300711 struct uvc_buffer *buf);
712
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300713/* debugfs and statistics */
Alexey Fisheredbaa392011-11-03 09:39:37 -0300714int uvc_debugfs_init(void);
715void uvc_debugfs_cleanup(void);
716int uvc_debugfs_init_stream(struct uvc_streaming *stream);
717void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
718
Alexey Fisher7bc5edb2011-11-03 11:40:08 -0300719size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
720 size_t size);
721
Laurent Pinchartc0efd232008-06-30 15:04:50 -0300722#endif