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