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