]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/usb/uvc/uvcvideo.h
media: uvcvideo: Refactor URB descriptors
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / usb / uvc / uvcvideo.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
c0efd232
LP
2#ifndef _USB_VIDEO_H_
3#define _USB_VIDEO_H_
4
5f708812 5#ifndef __KERNEL__
227bd5b3
LP
6#error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
7#endif /* __KERNEL__ */
c0efd232 8
227bd5b3 9#include <linux/kernel.h>
c0efd232 10#include <linux/poll.h>
4ffc2d89 11#include <linux/usb.h>
de05f634 12#include <linux/usb/video.h>
5f708812 13#include <linux/uvcvideo.h>
227bd5b3 14#include <linux/videodev2.h>
e5225c82 15#include <linux/workqueue.h>
5a254d75
LP
16#include <media/media-device.h>
17#include <media/v4l2-device.h>
b4012002
HG
18#include <media/v4l2-event.h>
19#include <media/v4l2-fh.h>
c139990e 20#include <media/videobuf2-v4l2.h>
c0efd232
LP
21
22/* --------------------------------------------------------------------------
23 * UVC constants
24 */
25
c0efd232
LP
26#define UVC_TERM_INPUT 0x0000
27#define UVC_TERM_OUTPUT 0x8000
6241d8ca 28#define UVC_TERM_DIRECTION(term) ((term)->type & 0x8000)
c0efd232
LP
29
30#define UVC_ENTITY_TYPE(entity) ((entity)->type & 0x7fff)
31#define UVC_ENTITY_IS_UNIT(entity) (((entity)->type & 0xff00) == 0)
32#define UVC_ENTITY_IS_TERM(entity) (((entity)->type & 0xff00) != 0)
33#define UVC_ENTITY_IS_ITERM(entity) \
8e113595
LP
34 (UVC_ENTITY_IS_TERM(entity) && \
35 ((entity)->type & 0x8000) == UVC_TERM_INPUT)
c0efd232 36#define UVC_ENTITY_IS_OTERM(entity) \
8e113595
LP
37 (UVC_ENTITY_IS_TERM(entity) && \
38 ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
c0efd232 39
c0efd232
LP
40
41/* ------------------------------------------------------------------------
42 * GUIDs
43 */
44#define UVC_GUID_UVC_CAMERA \
45 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
47#define UVC_GUID_UVC_OUTPUT \
48 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
50#define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
51 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
53#define UVC_GUID_UVC_PROCESSING \
54 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
56#define UVC_GUID_UVC_SELECTOR \
57 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
59
c0efd232
LP
60#define UVC_GUID_FORMAT_MJPEG \
61 { 'M', 'J', 'P', 'G', 0x00, 0x00, 0x10, 0x00, \
62 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
63#define UVC_GUID_FORMAT_YUY2 \
64 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
65 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
68f194e0
DR
66#define UVC_GUID_FORMAT_YUY2_ISIGHT \
67 { 'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, \
68 0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
c0efd232
LP
69#define UVC_GUID_FORMAT_NV12 \
70 { 'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
71 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
72#define UVC_GUID_FORMAT_YV12 \
73 { 'Y', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, \
74 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
75#define UVC_GUID_FORMAT_I420 \
76 { 'I', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
77 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
78#define UVC_GUID_FORMAT_UYVY \
79 { 'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, \
80 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
81#define UVC_GUID_FORMAT_Y800 \
82 { 'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, \
ca47e719
SM
83 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
84#define UVC_GUID_FORMAT_Y8 \
85 { 'Y', '8', ' ', ' ', 0x00, 0x00, 0x10, 0x00, \
86 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
87#define UVC_GUID_FORMAT_Y10 \
88 { 'Y', '1', '0', ' ', 0x00, 0x00, 0x10, 0x00, \
89 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
90#define UVC_GUID_FORMAT_Y12 \
91 { 'Y', '1', '2', ' ', 0x00, 0x00, 0x10, 0x00, \
c0efd232 92 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
61421206
LP
93#define UVC_GUID_FORMAT_Y16 \
94 { 'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
95 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
c0efd232
LP
96#define UVC_GUID_FORMAT_BY8 \
97 { 'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, \
98 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
e72ed08e
ET
99#define UVC_GUID_FORMAT_BA81 \
100 { 'B', 'A', '8', '1', 0x00, 0x00, 0x10, 0x00, \
101 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
102#define UVC_GUID_FORMAT_GBRG \
103 { 'G', 'B', 'R', 'G', 0x00, 0x00, 0x10, 0x00, \
104 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
105#define UVC_GUID_FORMAT_GRBG \
106 { 'G', 'R', 'B', 'G', 0x00, 0x00, 0x10, 0x00, \
107 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
108#define UVC_GUID_FORMAT_RGGB \
109 { 'R', 'G', 'G', 'B', 0x00, 0x00, 0x10, 0x00, \
110 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
eb165a20
ET
111#define UVC_GUID_FORMAT_BG16 \
112 { 'B', 'G', '1', '6', 0x00, 0x00, 0x10, 0x00, \
113 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
114#define UVC_GUID_FORMAT_GB16 \
115 { 'G', 'B', '1', '6', 0x00, 0x00, 0x10, 0x00, \
116 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
117#define UVC_GUID_FORMAT_RG16 \
118 { 'R', 'G', '1', '6', 0x00, 0x00, 0x10, 0x00, \
119 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
120#define UVC_GUID_FORMAT_GR16 \
121 { 'G', 'R', '1', '6', 0x00, 0x00, 0x10, 0x00, \
122 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
50791079
LP
123#define UVC_GUID_FORMAT_RGBP \
124 { 'R', 'G', 'B', 'P', 0x00, 0x00, 0x10, 0x00, \
125 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
1b8dc322
WM
126#define UVC_GUID_FORMAT_BGR3 \
127 { 0x7d, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, \
128 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}
7225a1dc
HG
129#define UVC_GUID_FORMAT_M420 \
130 { 'M', '4', '2', '0', 0x00, 0x00, 0x10, 0x00, \
131 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
c0efd232 132
25ad8a8d
SL
133#define UVC_GUID_FORMAT_H264 \
134 { 'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, \
135 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
5d8d8db8
AG
136#define UVC_GUID_FORMAT_Y8I \
137 { 'Y', '8', 'I', ' ', 0x00, 0x00, 0x10, 0x00, \
138 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
139#define UVC_GUID_FORMAT_Y12I \
140 { 'Y', '1', '2', 'I', 0x00, 0x00, 0x10, 0x00, \
141 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
142#define UVC_GUID_FORMAT_Z16 \
143 { 'Z', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, \
144 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
145#define UVC_GUID_FORMAT_RW10 \
146 { 'R', 'W', '1', '0', 0x00, 0x00, 0x10, 0x00, \
147 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
c4a0968a
DPJ
148#define UVC_GUID_FORMAT_INVZ \
149 { 'I', 'N', 'V', 'Z', 0x90, 0x2d, 0x58, 0x4a, \
150 0x92, 0x0b, 0x77, 0x3f, 0x1f, 0x2c, 0x55, 0x6b}
151#define UVC_GUID_FORMAT_INZI \
152 { 'I', 'N', 'Z', 'I', 0x66, 0x1a, 0x42, 0xa2, \
153 0x90, 0x65, 0xd0, 0x18, 0x14, 0xa8, 0xef, 0x8a}
154#define UVC_GUID_FORMAT_INVI \
155 { 'I', 'N', 'V', 'I', 0xdb, 0x57, 0x49, 0x5e, \
156 0x8e, 0x3f, 0xf4, 0x79, 0x53, 0x2b, 0x94, 0x6f}
38e9b928
SD
157#define UVC_GUID_FORMAT_CNF4 \
158 { 'C', ' ', ' ', ' ', 0x00, 0x00, 0x10, 0x00, \
159 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
25ad8a8d 160
e96cdc9a
ND
161#define UVC_GUID_FORMAT_D3DFMT_L8 \
162 {0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, \
163 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
557a5c7f
LP
164#define UVC_GUID_FORMAT_KSMEDIA_L8_IR \
165 {0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, \
166 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
e96cdc9a
ND
167
168
c0efd232
LP
169/* ------------------------------------------------------------------------
170 * Driver specific constants.
171 */
172
fd3e5824 173#define DRIVER_VERSION "1.1.1"
c0efd232
LP
174
175/* Number of isochronous URBs. */
176#define UVC_URBS 5
efdc8a95
LP
177/* Maximum number of packets per URB. */
178#define UVC_MAX_PACKETS 32
a31a4055
ML
179/* Maximum status buffer size in bytes of interrupt URB. */
180#define UVC_MAX_STATUS_SIZE 16
c0efd232 181
da6df1c4 182#define UVC_CTRL_CONTROL_TIMEOUT 500
56d15cd3 183#define UVC_CTRL_STREAMING_TIMEOUT 5000
c0efd232 184
8fb91b33
MR
185/* Maximum allowed number of control mappings per device */
186#define UVC_MAX_CONTROL_MAPPINGS 1024
806e23e9 187#define UVC_MAX_CONTROL_MENU_ENTRIES 32
8fb91b33 188
c0efd232
LP
189/* Devices quirks */
190#define UVC_QUIRK_STATUS_INTERVAL 0x00000001
191#define UVC_QUIRK_PROBE_MINMAX 0x00000002
192#define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
193#define UVC_QUIRK_BUILTIN_ISIGHT 0x00000008
194#define UVC_QUIRK_STREAM_NO_FID 0x00000010
195#define UVC_QUIRK_IGNORE_SELECTOR_UNIT 0x00000020
50144aee 196#define UVC_QUIRK_FIX_BANDWIDTH 0x00000080
bab6f66c 197#define UVC_QUIRK_PROBE_DEF 0x00000100
86d8b6ab 198#define UVC_QUIRK_RESTRICT_FRAME_RATE 0x00000200
17e1319f 199#define UVC_QUIRK_RESTORE_CTRLS_ON_INIT 0x00000400
e1b78a33 200#define UVC_QUIRK_FORCE_Y8 0x00000800
c0efd232
LP
201
202/* Format flags */
203#define UVC_FMT_FLAG_COMPRESSED 0x00000001
204#define UVC_FMT_FLAG_STREAM 0x00000002
205
206/* ------------------------------------------------------------------------
207 * Structures.
208 */
209
210struct uvc_device;
211
212/* TODO: Put the most frequently accessed fields at the beginning of
213 * structures to maximize cache efficiency.
214 */
c0efd232 215struct uvc_control_info {
c0efd232
LP
216 struct list_head mappings;
217
2c6b222c
LP
218 u8 entity[16];
219 u8 index; /* Bit index in bmControls */
220 u8 selector;
c0efd232 221
2c6b222c
LP
222 u16 size;
223 u32 flags;
c0efd232
LP
224};
225
226struct uvc_control_mapping {
227 struct list_head list;
b4012002 228 struct list_head ev_subs;
c0efd232 229
2c6b222c
LP
230 u32 id;
231 u8 name[32];
232 u8 entity[16];
233 u8 selector;
c0efd232 234
2c6b222c
LP
235 u8 size;
236 u8 offset;
c0efd232 237 enum v4l2_ctrl_type v4l2_type;
2c6b222c 238 u32 data_type;
c0efd232 239
52a4eb31 240 const struct uvc_menu_info *menu_info;
2c6b222c 241 u32 menu_count;
9768352a 242
2c6b222c
LP
243 u32 master_id;
244 s32 master_manual;
245 u32 slave_ids[2];
55afeb8a 246
2c6b222c
LP
247 s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
248 const u8 *data);
249 void (*set)(struct uvc_control_mapping *mapping, s32 value,
250 u8 *data);
c0efd232
LP
251};
252
253struct uvc_control {
254 struct uvc_entity *entity;
071c8bb8 255 struct uvc_control_info info;
c0efd232 256
2c6b222c 257 u8 index; /* Used to match the uvc_control entry with a
c0efd232 258 uvc_control_info. */
2c6b222c
LP
259 u8 dirty:1,
260 loaded:1,
261 modified:1,
262 cached:1,
263 initialized:1;
c0efd232 264
2c6b222c 265 u8 *uvc_data;
e5225c82
GL
266
267 struct uvc_fh *handle; /* File handle that last changed the control. */
c0efd232
LP
268};
269
270struct uvc_format_desc {
271 char *name;
2c6b222c
LP
272 u8 guid[16];
273 u32 fcc;
c0efd232
LP
274};
275
276/* The term 'entity' refers to both UVC units and UVC terminals.
277 *
278 * The type field is either the terminal type (wTerminalType in the terminal
279 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
280 * As the bDescriptorSubtype field is one byte long, the type value will
281 * always have a null MSB for units. All terminal types defined by the UVC
282 * specification have a non-null MSB, so it is safe to use the MSB to
283 * differentiate between units and terminals as long as the descriptor parsing
284 * code makes sure terminal types have a non-null MSB.
285 *
286 * For terminals, the type's most significant bit stores the terminal
287 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
288 * always be accessed with the UVC_ENTITY_* macros and never directly.
289 */
290
8be8ec6e
LP
291#define UVC_ENTITY_FLAG_DEFAULT (1 << 0)
292
c0efd232
LP
293struct uvc_entity {
294 struct list_head list; /* Entity as part of a UVC device. */
295 struct list_head chain; /* Entity as part of a video device
296 * chain. */
8be8ec6e
LP
297 unsigned int flags;
298
2c6b222c
LP
299 u8 id;
300 u16 type;
c0efd232
LP
301 char name[64];
302
4ffc2d89 303 /* Media controller-related fields. */
8a65a948 304 struct video_device *vdev;
4ffc2d89
LP
305 struct v4l2_subdev subdev;
306 unsigned int num_pads;
307 unsigned int num_links;
308 struct media_pad *pads;
309
c0efd232
LP
310 union {
311 struct {
2c6b222c
LP
312 u16 wObjectiveFocalLengthMin;
313 u16 wObjectiveFocalLengthMax;
314 u16 wOcularFocalLength;
315 u8 bControlSize;
316 u8 *bmControls;
c0efd232
LP
317 } camera;
318
319 struct {
2c6b222c
LP
320 u8 bControlSize;
321 u8 *bmControls;
322 u8 bTransportModeSize;
323 u8 *bmTransportModes;
c0efd232
LP
324 } media;
325
326 struct {
c0efd232
LP
327 } output;
328
329 struct {
2c6b222c
LP
330 u16 wMaxMultiplier;
331 u8 bControlSize;
332 u8 *bmControls;
333 u8 bmVideoStandards;
c0efd232
LP
334 } processing;
335
336 struct {
c0efd232
LP
337 } selector;
338
339 struct {
2c6b222c
LP
340 u8 guidExtensionCode[16];
341 u8 bNumControls;
342 u8 bControlSize;
343 u8 *bmControls;
344 u8 *bmControlsType;
c0efd232
LP
345 } extension;
346 };
347
2c6b222c
LP
348 u8 bNrInPins;
349 u8 *baSourceID;
8ca5a639 350
c0efd232
LP
351 unsigned int ncontrols;
352 struct uvc_control *controls;
353};
354
355struct uvc_frame {
2c6b222c
LP
356 u8 bFrameIndex;
357 u8 bmCapabilities;
358 u16 wWidth;
359 u16 wHeight;
360 u32 dwMinBitRate;
361 u32 dwMaxBitRate;
362 u32 dwMaxVideoFrameBufferSize;
363 u8 bFrameIntervalType;
364 u32 dwDefaultFrameInterval;
365 u32 *dwFrameInterval;
c0efd232
LP
366};
367
368struct uvc_format {
2c6b222c
LP
369 u8 type;
370 u8 index;
371 u8 bpp;
372 u8 colorspace;
373 u32 fcc;
374 u32 flags;
c0efd232
LP
375
376 char name[32];
377
378 unsigned int nframes;
379 struct uvc_frame *frame;
380};
381
382struct uvc_streaming_header {
2c6b222c
LP
383 u8 bNumFormats;
384 u8 bEndpointAddress;
385 u8 bTerminalLink;
386 u8 bControlSize;
387 u8 *bmaControls;
c0efd232 388 /* The following fields are used by input headers only. */
2c6b222c
LP
389 u8 bmInfo;
390 u8 bStillCaptureMethod;
391 u8 bTriggerSupport;
392 u8 bTriggerUsage;
c0efd232
LP
393};
394
c0efd232 395enum uvc_buffer_state {
2c2d264b
LP
396 UVC_BUF_STATE_IDLE = 0,
397 UVC_BUF_STATE_QUEUED = 1,
398 UVC_BUF_STATE_ACTIVE = 2,
d7c0d439
LP
399 UVC_BUF_STATE_READY = 3,
400 UVC_BUF_STATE_DONE = 4,
401 UVC_BUF_STATE_ERROR = 5,
c0efd232
LP
402};
403
404struct uvc_buffer {
2d700715 405 struct vb2_v4l2_buffer buf;
c0efd232 406 struct list_head queue;
6998b6fb 407
c0efd232 408 enum uvc_buffer_state state;
9bde9f26 409 unsigned int error;
3d95e932
LP
410
411 void *mem;
412 unsigned int length;
413 unsigned int bytesused;
66847ef0
LP
414
415 u32 pts;
c0efd232
LP
416};
417
6998b6fb
LP
418#define UVC_QUEUE_DISCONNECTED (1 << 0)
419#define UVC_QUEUE_DROP_CORRUPTED (1 << 1)
c0efd232
LP
420
421struct uvc_video_queue {
6998b6fb
LP
422 struct vb2_queue queue;
423 struct mutex mutex; /* Protects queue */
ff924203 424
c0efd232 425 unsigned int flags;
ff924203 426 unsigned int buf_used;
c0efd232 427
6998b6fb 428 spinlock_t irqlock; /* Protects irqqueue */
c0efd232
LP
429 struct list_head irqqueue;
430};
431
8e113595
LP
432struct uvc_video_chain {
433 struct uvc_device *dev;
434 struct list_head list;
435
6241d8ca 436 struct list_head entities; /* All entities */
8e113595
LP
437 struct uvc_entity *processing; /* Processing unit */
438 struct uvc_entity *selector; /* Selector unit */
8e113595 439
27a61c13 440 struct mutex ctrl_mutex; /* Protects ctrl.info */
f887e99a 441
0550513c 442 struct v4l2_prio_state prio; /* V4L2 priority state */
f887e99a 443 u32 caps; /* V4L2 chain-wide caps */
8e113595
LP
444};
445
7bc5edb0
OR
446struct uvc_stats_frame {
447 unsigned int size; /* Number of bytes captured */
448 unsigned int first_data; /* Index of the first non-empty packet */
449
450 unsigned int nb_packets; /* Number of packets */
451 unsigned int nb_empty; /* Number of empty packets */
452 unsigned int nb_invalid; /* Number of packets with an invalid header */
453 unsigned int nb_errors; /* Number of packets with the error bit set */
25738cbd
LP
454
455 unsigned int nb_pts; /* Number of packets with a PTS timestamp */
456 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */
457 unsigned int last_pts_diff; /* Index of the last PTS difference */
458 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */
459 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */
460 u32 pts; /* PTS of the last packet */
461
462 unsigned int nb_scr; /* Number of packets with a SCR timestamp */
463 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */
464 u16 scr_sof; /* SCR.SOF of the last packet */
465 u32 scr_stc; /* SCR.STC of the last packet */
7bc5edb0
OR
466};
467
468struct uvc_stats_stream {
a70b8b24
AB
469 ktime_t start_ts; /* Stream start timestamp */
470 ktime_t stop_ts; /* Stream stop timestamp */
7bc5edb0
OR
471
472 unsigned int nb_frames; /* Number of frames */
473
474 unsigned int nb_packets; /* Number of packets */
475 unsigned int nb_empty; /* Number of empty packets */
476 unsigned int nb_invalid; /* Number of packets with an invalid header */
477 unsigned int nb_errors; /* Number of packets with the error bit set */
25738cbd
LP
478
479 unsigned int nb_pts_constant; /* Number of frames with constant PTS */
480 unsigned int nb_pts_early; /* Number of frames with early PTS */
481 unsigned int nb_pts_initial; /* Number of frames with initial PTS */
482
483 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */
484 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */
485 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
486 unsigned int scr_sof; /* STC.SOF of the last packet */
487 unsigned int min_sof; /* Minimum STC.SOF value */
488 unsigned int max_sof; /* Maximum STC.SOF value */
7bc5edb0
OR
489};
490
088ead25
GL
491#define UVC_METATADA_BUF_SIZE 1024
492
811496c9
KB
493/**
494 * struct uvc_urb - URB context management structure
495 *
496 * @urb: the URB described by this context structure
497 * @buffer: memory storage for the URB
498 * @dma: DMA coherent addressing for the urb_buffer
499 */
500struct uvc_urb {
501 struct urb *urb;
502
503 char *buffer;
504 dma_addr_t dma;
505};
506
35f02a68
LP
507struct uvc_streaming {
508 struct list_head list;
c0efd232 509 struct uvc_device *dev;
d8da7513 510 struct video_device vdev;
8e113595 511 struct uvc_video_chain *chain;
c0efd232 512 atomic_t active;
c0efd232 513
35f02a68
LP
514 struct usb_interface *intf;
515 int intfnum;
2c6b222c 516 u16 maxpsize;
c0efd232 517
35f02a68
LP
518 struct uvc_streaming_header header;
519 enum v4l2_buf_type type;
520
521 unsigned int nformats;
522 struct uvc_format *format;
c0efd232 523
35f02a68 524 struct uvc_streaming_control ctrl;
815adc46 525 struct uvc_format *def_format;
35f02a68
LP
526 struct uvc_format *cur_format;
527 struct uvc_frame *cur_frame;
e93e7fd9 528
6947756d
LP
529 /* Protect access to ctrl, cur_format, cur_frame and hardware video
530 * probe control.
531 */
35f02a68 532 struct mutex mutex;
c0efd232 533
6998b6fb 534 /* Buffers queue. */
35f02a68
LP
535 unsigned int frozen : 1;
536 struct uvc_video_queue queue;
537 void (*decode) (struct urb *urb, struct uvc_streaming *video,
088ead25
GL
538 struct uvc_buffer *buf, struct uvc_buffer *meta_buf);
539
540 struct {
541 struct video_device vdev;
542 struct uvc_video_queue queue;
2c6b222c 543 u32 format;
088ead25 544 } meta;
c0efd232
LP
545
546 /* Context data used by the bulk completion handler. */
547 struct {
2c6b222c 548 u8 header[256];
c0efd232
LP
549 unsigned int header_size;
550 int skip_payload;
2c6b222c
LP
551 u32 payload_size;
552 u32 max_payload_size;
c0efd232
LP
553 } bulk;
554
811496c9 555 struct uvc_urb uvc_urb[UVC_URBS];
e01117c8 556 unsigned int urb_size;
c0efd232 557
2c6b222c
LP
558 u32 sequence;
559 u8 last_fid;
edbaa398
OR
560
561 /* debugfs */
562 struct dentry *debugfs_dir;
7bc5edb0
OR
563 struct {
564 struct uvc_stats_frame frame;
565 struct uvc_stats_stream stream;
566 } stats;
66847ef0
LP
567
568 /* Timestamps support. */
569 struct uvc_clock {
570 struct uvc_clock_sample {
571 u32 dev_stc;
572 u16 dev_sof;
66847ef0 573 u16 host_sof;
828ee8c7 574 ktime_t host_time;
66847ef0
LP
575 } *samples;
576
577 unsigned int head;
578 unsigned int count;
579 unsigned int size;
580
581 u16 last_sof;
582 u16 sof_offset;
583
088ead25
GL
584 u8 last_scr[6];
585
66847ef0
LP
586 spinlock_t lock;
587 } clock;
c0efd232
LP
588};
589
3a03284d
LP
590struct uvc_device_info {
591 u32 quirks;
592 u32 meta_format;
593};
594
c0efd232
LP
595struct uvc_device {
596 struct usb_device *udev;
597 struct usb_interface *intf;
44f0079e 598 unsigned long warnings;
2c6b222c 599 u32 quirks;
c0efd232
LP
600 int intfnum;
601 char name[32];
602
3a03284d
LP
603 const struct uvc_device_info *info;
604
17706f56
LP
605 struct mutex lock; /* Protects users */
606 unsigned int users;
8fb91b33 607 atomic_t nmappings;
c0efd232
LP
608
609 /* Video control interface */
5a254d75
LP
610#ifdef CONFIG_MEDIA_CONTROLLER
611 struct media_device mdev;
612#endif
613 struct v4l2_device vdev;
2c6b222c
LP
614 u16 uvc_version;
615 u32 clock_frequency;
c0efd232
LP
616
617 struct list_head entities;
8e113595 618 struct list_head chains;
c0efd232 619
35f02a68
LP
620 /* Video Streaming interfaces */
621 struct list_head streams;
9d15cd95 622 struct kref ref;
35f02a68 623
c0efd232
LP
624 /* Status Interrupt Endpoint */
625 struct usb_host_endpoint *int_ep;
626 struct urb *int_urb;
2c6b222c 627 u8 *status;
c0efd232 628 struct input_dev *input;
f1801523 629 char input_phys[64];
e5225c82
GL
630
631 struct uvc_ctrl_work {
632 struct work_struct work;
633 struct urb *urb;
634 struct uvc_video_chain *chain;
635 struct uvc_control *ctrl;
636 const void *data;
637 } async_ctrl;
c0efd232
LP
638};
639
640enum uvc_handle_state {
641 UVC_HANDLE_PASSIVE = 0,
642 UVC_HANDLE_ACTIVE = 1,
643};
644
645struct uvc_fh {
b4012002 646 struct v4l2_fh vfh;
8e113595 647 struct uvc_video_chain *chain;
35f02a68 648 struct uvc_streaming *stream;
c0efd232
LP
649 enum uvc_handle_state state;
650};
651
652struct uvc_driver {
653 struct usb_driver driver;
c0efd232
LP
654};
655
656/* ------------------------------------------------------------------------
657 * Debugging, printing and logging
658 */
659
660#define UVC_TRACE_PROBE (1 << 0)
661#define UVC_TRACE_DESCR (1 << 1)
662#define UVC_TRACE_CONTROL (1 << 2)
663#define UVC_TRACE_FORMAT (1 << 3)
664#define UVC_TRACE_CAPTURE (1 << 4)
665#define UVC_TRACE_CALLS (1 << 5)
c0efd232
LP
666#define UVC_TRACE_FRAME (1 << 7)
667#define UVC_TRACE_SUSPEND (1 << 8)
668#define UVC_TRACE_STATUS (1 << 9)
663a4192 669#define UVC_TRACE_VIDEO (1 << 10)
7bc5edb0 670#define UVC_TRACE_STATS (1 << 11)
66847ef0 671#define UVC_TRACE_CLOCK (1 << 12)
c0efd232 672
44f0079e
LP
673#define UVC_WARN_MINMAX 0
674#define UVC_WARN_PROBE_DEF 1
c4d99f89 675#define UVC_WARN_XU_GET_RES 2
44f0079e 676
310fe524 677extern unsigned int uvc_clock_param;
0fbd8ee6 678extern unsigned int uvc_no_drop_param;
c0efd232 679extern unsigned int uvc_trace_param;
b232a012 680extern unsigned int uvc_timeout_param;
5d0fd3c8 681extern unsigned int uvc_hw_timestamps_param;
c0efd232
LP
682
683#define uvc_trace(flag, msg...) \
684 do { \
685 if (uvc_trace_param & flag) \
686 printk(KERN_DEBUG "uvcvideo: " msg); \
687 } while (0)
688
44f0079e
LP
689#define uvc_warn_once(dev, warn, msg...) \
690 do { \
691 if (!test_and_set_bit(warn, &dev->warnings)) \
692 printk(KERN_INFO "uvcvideo: " msg); \
693 } while (0)
694
c0efd232
LP
695#define uvc_printk(level, msg...) \
696 printk(level "uvcvideo: " msg)
697
c0efd232
LP
698/* --------------------------------------------------------------------------
699 * Internal functions.
700 */
701
702/* Core driver */
703extern struct uvc_driver uvc_driver;
c0efd232 704
cb9cd6c5 705struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
4ffc2d89 706
c0efd232 707/* Video buffers queue management. */
cb9cd6c5
LP
708int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
709 int drop_corrupted);
710void uvc_queue_release(struct uvc_video_queue *queue);
711int uvc_request_buffers(struct uvc_video_queue *queue,
712 struct v4l2_requestbuffers *rb);
713int uvc_query_buffer(struct uvc_video_queue *queue,
714 struct v4l2_buffer *v4l2_buf);
715int uvc_create_buffers(struct uvc_video_queue *queue,
716 struct v4l2_create_buffers *v4l2_cb);
717int uvc_queue_buffer(struct uvc_video_queue *queue,
394dc588 718 struct media_device *mdev,
cb9cd6c5
LP
719 struct v4l2_buffer *v4l2_buf);
720int uvc_export_buffer(struct uvc_video_queue *queue,
721 struct v4l2_exportbuffer *exp);
722int uvc_dequeue_buffer(struct uvc_video_queue *queue,
723 struct v4l2_buffer *v4l2_buf, int nonblocking);
724int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
725int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
726void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
727struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
728 struct uvc_buffer *buf);
729int uvc_queue_mmap(struct uvc_video_queue *queue,
730 struct vm_area_struct *vma);
731__poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
732 poll_table *wait);
72969447 733#ifndef CONFIG_MMU
cb9cd6c5
LP
734unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
735 unsigned long pgoff);
72969447 736#endif
cb9cd6c5 737int uvc_queue_allocated(struct uvc_video_queue *queue);
c0efd232
LP
738static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
739{
6998b6fb 740 return vb2_is_streaming(&queue->queue);
c0efd232
LP
741}
742
743/* V4L2 interface */
d5e90b7a 744extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
bec43661 745extern const struct v4l2_file_operations uvc_fops;
c0efd232 746
4ffc2d89 747/* Media controller */
cb9cd6c5
LP
748int uvc_mc_register_entities(struct uvc_video_chain *chain);
749void uvc_mc_cleanup_entity(struct uvc_entity *entity);
4ffc2d89 750
c0efd232 751/* Video */
cb9cd6c5
LP
752int uvc_video_init(struct uvc_streaming *stream);
753int uvc_video_suspend(struct uvc_streaming *stream);
754int uvc_video_resume(struct uvc_streaming *stream, int reset);
755int uvc_video_enable(struct uvc_streaming *stream, int enable);
756int uvc_probe_video(struct uvc_streaming *stream,
757 struct uvc_streaming_control *probe);
2c6b222c
LP
758int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
759 u8 intfnum, u8 cs, void *data, u16 size);
66847ef0 760void uvc_video_clock_update(struct uvc_streaming *stream,
2d700715 761 struct vb2_v4l2_buffer *vbuf,
66847ef0 762 struct uvc_buffer *buf);
088ead25 763int uvc_meta_register(struct uvc_streaming *stream);
c0efd232 764
31a96f4c
LP
765int uvc_register_video_device(struct uvc_device *dev,
766 struct uvc_streaming *stream,
767 struct video_device *vdev,
768 struct uvc_video_queue *queue,
769 enum v4l2_buf_type type,
770 const struct v4l2_file_operations *fops,
771 const struct v4l2_ioctl_ops *ioctl_ops);
c0efd232
LP
772
773/* Status */
cb9cd6c5 774int uvc_status_init(struct uvc_device *dev);
10e1fdb9 775void uvc_status_unregister(struct uvc_device *dev);
cb9cd6c5
LP
776void uvc_status_cleanup(struct uvc_device *dev);
777int uvc_status_start(struct uvc_device *dev, gfp_t flags);
778void uvc_status_stop(struct uvc_device *dev);
c0efd232
LP
779
780/* Controls */
b4012002
HG
781extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
782
cb9cd6c5
LP
783int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
784 struct v4l2_queryctrl *v4l2_ctrl);
785int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
786 struct v4l2_querymenu *query_menu);
787
788int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
789 const struct uvc_control_mapping *mapping);
790int uvc_ctrl_init_device(struct uvc_device *dev);
791void uvc_ctrl_cleanup_device(struct uvc_device *dev);
792int uvc_ctrl_restore_values(struct uvc_device *dev);
e5225c82
GL
793bool uvc_ctrl_status_event(struct urb *urb, struct uvc_video_chain *chain,
794 struct uvc_control *ctrl, const u8 *data);
cb9cd6c5
LP
795
796int uvc_ctrl_begin(struct uvc_video_chain *chain);
797int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
798 const struct v4l2_ext_control *xctrls,
799 unsigned int xctrls_count);
b4012002 800static inline int uvc_ctrl_commit(struct uvc_fh *handle,
cb9cd6c5
LP
801 const struct v4l2_ext_control *xctrls,
802 unsigned int xctrls_count)
c0efd232 803{
b4012002 804 return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count);
c0efd232 805}
b4012002 806static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
c0efd232 807{
b4012002 808 return __uvc_ctrl_commit(handle, 1, NULL, 0);
c0efd232
LP
809}
810
cb9cd6c5 811int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
e5225c82 812int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
c0efd232 813
cb9cd6c5
LP
814int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
815 struct uvc_xu_control_query *xqry);
c0efd232
LP
816
817/* Utility functions */
1e304c47 818void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
cb9cd6c5 819 unsigned int n_terms, unsigned int threshold);
1e304c47 820u32 uvc_fraction_to_interval(u32 numerator, u32 denominator);
cb9cd6c5 821struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
2c6b222c 822 u8 epaddr);
c0efd232
LP
823
824/* Quirks support */
35f02a68 825void uvc_video_decode_isight(struct urb *urb, struct uvc_streaming *stream,
cb9cd6c5
LP
826 struct uvc_buffer *buf,
827 struct uvc_buffer *meta_buf);
c0efd232 828
7bc5edb0 829/* debugfs and statistics */
39dc3aae 830void uvc_debugfs_init(void);
edbaa398 831void uvc_debugfs_cleanup(void);
39dc3aae 832void uvc_debugfs_init_stream(struct uvc_streaming *stream);
edbaa398
OR
833void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
834
7bc5edb0
OR
835size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
836 size_t size);
837
c0efd232 838#endif