• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------------
2  * Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
3  * Description: LiteOS USB Driver UVC Protocol HeadFile
4  * Author: huangjieliang
5  * Create: 2017-04-17
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without specific prior written
15  * permission.
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * --------------------------------------------------------------------------- */
28 /* ----------------------------------------------------------------------------
29  * Notice of Export Control Law
30  * ===============================================
31  * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32  * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33  * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34  * applicable export control laws and regulations.
35  * --------------------------------------------------------------------------- */
36 
37 #ifndef _F_UVC_GADGET_H
38 #define _F_UVC_GADGET_H
39 
40 #include "gadget/usbd_video.h"
41 #include "gadget/composite.h"
42 #include "gadget/usbdev.h"
43 #include "implementation/freebsd_sys.h"
44 #include "usb.h"
45 #include "usb_endian.h"
46 #include <linux/spinlock.h>
47 
48 #ifdef __cplusplus
49 #if __cplusplus
50 extern "C" {
51 #endif /* __cplusplus */
52 #endif /* __cplusplus */
53 
54 /*
55  * USB Video Class -- Class-specific VC Interface Header Descriptor
56  * Refer to USB Video Class Specification 1.5, section 3.7.2
57  */
58 
59 struct uvc_vc_header_descriptor
60 {
61   u8    bLength;
62   u8    bDescriptorType;
63   u8    bDescriptorSubtype;
64   u16   bcdUVC;
65   u16   wTotalLength;
66   u32   dwClockFrequency;
67   u8    bInCollection;
68   u8    bInterfaceNr;
69 } __attribute__((packed));
70 
71 /*
72  * UVC Video Class-specific Descriptor Types, UVC spec. 1.5, section A.4
73  */
74 
75 #define USB_UVC_CS_UNDEFINED       0x20
76 #define USB_UVC_CS_DEVICE          0x21
77 #define USB_UVC_CS_CONFIGURATION   0x22
78 #define USB_UVC_CS_STRING          0x23
79 #define USB_UVC_CS_INTERFACE       0x24
80 #define USB_UVC_CS_ENDPOINT        0x25
81 
82 /*
83  * UVC Video Class-specific Descriptor Subtypes, UVC spec. 1.5, section A.5
84  */
85 
86 #define UVC_VC_DESCRIPTOR_UNDEFINED    0x0
87 #define UVC_VC_HEADER                  0x1
88 #define UVC_VC_INPUT_TERMINAL          0x2
89 #define UVC_VC_OUTPUT_TERMINAL         0x3
90 #define UVC_VC_SELECTOR_UNIT           0x4
91 #define UVC_VC_PROCESSING_UNIT         0x5
92 #define UVC_VC_EXTENSION_UNIT          0x6
93 #define UVC_VC_ENCODING_UNIT           0x7
94 
95 /*
96  * UVC input terminal descriptor, refer to UVC Specification
97  * 1.5, section 3.7.2.1 for the details
98  */
99 
100 struct uvc_it_descriptor
101 {
102   u8    bLength;
103   u8    bDescriptorType;
104   u8    bDescriptorSubtype;
105   u8    bTerminalID;
106   u16   wTerminalType;
107   u8    bAssocTerminal;
108   u8    iTerminal;
109 } __attribute__((packed));
110 
111 /*
112  * UVC output terminal descriptor, refer to UVC specification
113  * 1.5, section 3.7.2.2 for the details
114  */
115 
116 struct uvc_ot_descriptor
117 {
118   u8    bLength;
119   u8    bDescriptorType;
120   u8    bDescriptorSubtype;
121   u8    bTerminalID;
122   u16   wTerminalType;
123   u8    bAssocTerminal;
124   u8    bSourceID;
125   u8    iTerminal;
126 } __attribute__((packed));
127 
128 /*
129  * USB Terminal types, UVC spec. 1.5, section B.1
130  */
131 
132 #define USB_UVC_TT_VENDOR_SPECIFIC  0x0100
133 #define USB_UVC_TT_STREAMING        0x0101
134 
135 /*
136  * UVC camera terminal descriptor, refer to UVC specification
137  * 1.5, section 3.7.2.3 for the details
138  */
139 
140 struct uvc_ct_descriptor
141 {
142   u8    bLength;
143   u8    bDescriptorType;
144   u8    bDescriptorSubtype;
145   u8    bTerminalID;
146   u16   wTerminalType;
147   u8    bAssocTerminal;
148   u8    iTerminal;
149   u16   wObjectiveFocalLengthMin;
150   u16   wObjectiveFocalLengthMax;
151   u16   wOcularFocalLength;
152   u8    bControlSize;
153   u8    bmControls[3];
154 } __attribute__((packed));
155 
156 /*
157  * Camera Terminal Control Selectors, UVC spec. 1.5, section A.9.4
158  */
159 
160 #define USBD_UVC_CT_CONTROL_UNDEFINED                0x00
161 #define USBD_UVC_CT_SCANNING_MODE_CONTROL            0x01
162 #define USBD_UVC_CT_AE_MODE_CONTROL                  0x02
163 #define USBD_UVC_CT_AE_PRIORITY_CONTROL              0x03
164 #define USBD_UVC_CT_EXPOSURE_TIME_ABSOLUTE_CONTROL   0x04
165 #define USBD_UVC_CT_EXPOSURE_TIME_RELATIVE_CONTROL   0x05
166 #define USBD_UVC_CT_FOCUS_ABSOLUTE_CONTROL           0x06
167 #define USBD_UVC_CT_FOCUS_RELATIVE_CONTROL           0x07
168 #define USBD_UVC_CT_FOCUS_AUTO_CONTROL               0x08
169 #define USBD_UVC_CT_IRIS_ABSOLUTE_CONTROL            0x09
170 #define USBD_UVC_CT_IRIS_RELATIVE_CONTROL            0x0a
171 #define USBD_UVC_CT_ZOOM_ABSOLUTE_CONTROL            0x0b
172 #define USBD_UVC_CT_ZOOM_RELATIVE_CONTROL            0x0c
173 #define USBD_UVC_CT_PANTILT_ABSOLUTE_CONTROL         0x0d
174 #define USBD_UVC_CT_PANTILT_RELATIVE_CONTROL         0x0e
175 #define USBD_UVC_CT_ROLL_ABSOLUTE_CONTROL            0x0f
176 #define USBD_UVC_CT_ROLL_RELATIVE_CONTROL            0x10
177 #define USBD_UVC_CT_PRIVACY_CONTROL                  0x11
178 #define USBD_UVC_CT_FOCUS_SIMPLE_CONTROL             0x12
179 #define USBD_UVC_CT_WINDOW_CONTROL                   0x13
180 #define USBD_UVC_CT_REGION_OF_INTEREST_CONTROL       0x14
181 
182 /*
183  * Input Terminal Types, UVC spec. 1.5, section B.2
184  */
185 
186 #define USB_UVC_ITT_VENDOR_SPECIFIC        0x0200
187 #define USB_UVC_ITT_CAMERA                 0x0201
188 #define USB_UVC_ITT_MEDIA_TRANSPORT_INPUT  0x0202
189 
190 /*
191  * Selector Unit Descriptor, UVC spec., section 3.7.2.4
192  */
193 
194 #define DEFINE_UVC_SELECTOR_DESCRIPTOR(p_) \
195 struct uvc_selector_descriptor ## p_       \
196 {                                          \
197   u8   bLength;                            \
198   u8   bDescriptorType;                    \
199   u8   bDescriptorSubtype;                 \
200   u8   bUnitID;                            \
201   u8   bNrInPins;                          \
202   u8   baSourceID[p_];                     \
203   u8   iSelector;                          \
204 } __attribute__((packed))
205 
206 DEFINE_UVC_SELECTOR_DESCRIPTOR(2);
207 
208 /*
209  * Processing Unit Descriptor, UVC spec. 1.5, section 3.7.2.5
210  */
211 
212 struct uvc_processing_descriptor
213 {
214   u8    bLength;
215   u8    bDescriptorType;
216   u8    bDescriptorSubtype;
217   u8    bUnitID;
218   u8    bSourceID;
219   u16   wMaxMultiplier;
220   u8    bControlSize;
221   u8    bmControls[3];
222   u8    iProcessing;
223   u8    bmVideoStandards;
224 } __attribute__((packed));
225 
226 /*
227  * UVC Processing Unit Control Selectors, UVC spec. 1.5, section A.9.5
228  */
229 
230 #define USBD_UVC_PU_CONTROL_UNDEFINED                        0x00
231 #define USBD_UVC_PU_BACKLIGHT_COMPENSATION_CONTROL           0x01
232 #define USBD_UVC_PU_BRIGHTNESS_CONTROL                       0x02
233 #define USBD_UVC_PU_CONTRAST_CONTROL                         0x03
234 #define USBD_UVC_PU_GAIN_CONTROL                             0x04
235 #define USBD_UVC_PU_POWER_LINE_FREQUENCY_CONTROL             0x05
236 #define USBD_UVC_PU_HUE_CONTROL                              0x06
237 #define USBD_UVC_PU_SATURATION_CONTROL                       0x07
238 #define USBD_UVC_PU_SHARPNESS_CONTROL                        0x08
239 #define USBD_UVC_PU_GAMMA_CONTROL                            0x09
240 #define USBD_UVC_PU_WHITE_BALANCE_TEMPERATURE_CONTROL        0x0a
241 #define USBD_UVC_PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTORL   0x0b
242 #define USBD_UVC_PU_WHITE_BALANCE_COMPONENT_CONTROL          0x0c
243 #define USBD_UVC_PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL     0x0d
244 #define USBD_UVC_PU_DIGITAL_MULTIPLIER_CONTROL               0x0e
245 #define USBD_UVC_PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL         0x0f
246 #define USBD_UVC_PU_HUE_AUTO_CONTROL                         0x10
247 #define USBD_UVC_PU_ANALOG_VIDEO_STANDARD_CONTROL            0x11
248 #define USBD_UVC_PU_ANALOG_LOCK_STATUS_CONTROL               0x12
249 #define USBD_UVC_PU_CONTRAST_AUTO_CONTROL                    0x13
250 
251 /*
252  * UVC Extension Unit Descriptor, UVC spec. 1.5, section 3.7.2.7
253  */
254 
255 #define DECLARE_UVC_EXTENSION_UNIT(p_, n_)            \
256 struct uvc_extension_unit_descriptor ## p_ ## x ## n_ \
257 {                                                     \
258   u8   bLength;                                       \
259   u8   bDescriptorType;                               \
260   u8   bDescriptorSubtype;                            \
261   u8   bUnitID;                                       \
262   u8   guidExtensionCode[16];                         \
263   u8   bNumControls;                                  \
264   u8   bNrInPins;                                     \
265   u8   baSourceID[ p_ ];                              \
266   u8   bControlSize;                                  \
267   u8   bmControls[ n_ ];                              \
268   u8   iExtension;                                    \
269 } __attribute__((packed))
270 
271 DECLARE_UVC_EXTENSION_UNIT(1, 2);
272 
273 /*
274  * UVC Video Streaming interface header descriptor, UVC spec., section 3.9.2.1
275  */
276 
277 struct uvc_vs_header_descriptor
278 {
279   u8    bLength;
280   u8    bDescriptorType;
281   u8    bDescriptorSubtype;
282   u8    bNumFormats;
283   u16   wTotalLength;
284   u8    bEndpointAddress;
285   u8    bmInfo;
286   u8    bTerminalLink;
287   u8    bStillCaptureMethod;
288   u8    bTriggerSupport;
289   u8    bTriggerUsage;
290   u8    bControlSize;
291   u8    bmaControls[3][1];
292 } __attribute__((packed));
293 
294 /*
295  * UVC stream Header definitions, refer to `USB_Video_Payload_H264_1.5, section 2.2
296  */
297 
298 #define UVC_STREAM_HEADER_FID   0x01
299 #define UVC_STREAM_HEADER_EOF   0x02
300 #define UVC_STREAM_HEADER_PTS   0x04
301 #define UVC_STREAM_HEADER_SCR   0x08
302 #define UVC_STREAM_HEADER_EOS   0x10
303 #define UVC_STREAM_HEADER_STI   0x20
304 #define UVC_STREAM_HEADER_ERR   0x40
305 #define UVC_STREAM_HEADER_EOH   0x80
306 
307 /*
308  * Video Class-specific VS interface Descriptor Subtypes, UVC spec. 1.5, section A.6
309  */
310 
311 #define USB_UVC_VS_UNDEFINED             0x00
312 #define USB_UVC_VS_INPUT_HEADER          0x01
313 #define USB_UVC_VS_OUTPUT_HEADER         0x02
314 #define USB_UVC_VS_STILL_IMAGE_FRAME     0x03
315 #define USB_UVC_VS_FORMAT_UNCOMPRESSED   0x04
316 #define USB_UVC_VS_FRAME_UNCOMPRESSED    0x05
317 #define USB_UVC_VS_FORMAT_MJPEG          0x06
318 #define USB_UVC_VS_FRAME_MJPEG           0x07
319 
320 #define USB_UVC_VS_FORMAT_MPEG2TS        0x0a
321 #define USB_UVC_VS_FORMAT_DV             0x0c
322 #define USB_UVC_VS_COLOR_FORMAT          0x0d
323 #define USB_UVC_VS_FORMAT_FRAME_BASED    0x10
324 #define USB_UVC_VS_FRAME_FRAME_BASED     0x11
325 #define USB_UVC_VS_FORMAT_STREAM_BASED   0x12
326 #define USB_UVC_VS_FORMAT_H264           0x13
327 
328 #define USB_UVC_VS_FRAME_H264            0x14
329 #define USB_UVC_VS_FORMAT_H264_SIMULCAST 0x15
330 #define USB_UVC_VS_FORMAT_VP8            0x16
331 #define USB_UVC_VS_FRAME_VP8             0x17
332 #define USB_UVC_VS_FORMAT_VP8_SIMULCAST  0x18
333 
334 /*
335  * UVC uncompressed video frame descriptor, USB Video Payload Uncompressed 1.5, section 3.1.2
336  */
337 
338 #define DEFINE_UVC_UNCOMPRESSED_FRAME_DESCRIPTOR(p_) \
339 struct uvc_uncompressed_frame_descriptor ## p_       \
340 {                                                    \
341   u8    bLength;                                     \
342   u8    bDescriptorType;                             \
343   u8    bDescriptorSubtype;                          \
344   u8    bFrameIndex;                                 \
345   u8    bmCapabilities;                              \
346   u16   wWidth;                                      \
347   u16   wHeight;                                     \
348   u32   dwMinBitRate;                                \
349   u32   dwMaxBitRate;                                \
350   u32   dwMaxVideoFrameBufferSize;                   \
351   u32   dwDefaultFrameInterval;                      \
352   u8    bFrameIntervalType;                          \
353   u32   dwFrameInterval[p_];                         \
354 } __attribute__((packed))
355 
356 DEFINE_UVC_UNCOMPRESSED_FRAME_DESCRIPTOR(3);
357 
358 /*
359  * UVC uncompressed video format descriptor, USB Video Payload Uncompressed 1.5, section 3.1.1
360  */
361 
362 struct uvc_uncompressed_format_descriptor
363 {
364   u8   bLength;
365   u8   bDescriptorType;
366   u8   bDescriptorSubtype;
367   u8   bFormatIndex;
368   u8   bNumFrameDescriptors;
369   u8   guidFormat[16];
370   u8   bBitsPerPixel;
371   u8   bDefaultFrameIndex;
372   u8   bAspectRatioX;
373   u8   bAspectRatioY;
374   u8   bmInterlaceFlags;
375   u8   bCopyProtect;
376 } __attribute__((packed));
377 
378 /* MJPEG Payload - 3.1.1. MJPEG Video Format Descriptor */
379 
380 struct uvc_format_mjpeg
381 {
382   u8  bLength;
383   u8  bDescriptorType;
384   u8  bDescriptorSubType;
385   u8  bFormatIndex;
386   u8  bNumFrameDescriptors;
387   u8  bmFlags;
388   u8  bDefaultFrameIndex;
389   u8  bAspectRatioX;
390   u8  bAspectRatioY;
391   u8  bmInterfaceFlags;
392   u8  bCopyProtect;
393 } __attribute__((packed));
394 
395 /*
396  * UVC stream based format descriptor
397  * USB_Video_Payload_Stream_Based_1.1, section 3.1.1
398  * USB_Video_Payload_Stream_Based_1.5, section 3.1.1
399  */
400 
401 struct uvc_stream_based_format_descriptor
402 {
403   u8    bLength;
404   u8    bDescriptorType;
405   u8    bDescriptorSubtype;
406   u8    bFormatIndex;
407   u8    guidFormat[16];
408   u32   dwPacketLength;
409 } __attribute__((packed));
410 
411 /*
412  * frame based Payload Video Format Descriptor, USB_Video_Payload_Frame_Based_1.1, section 3.1.1
413  */
414 
415 struct uvc_frame_based_format_descriptor
416 {
417   u8   bLength;
418   u8   bDescriptorType;
419   u8   bDescriptorSubtype;
420   u8   bFormatIndex;
421   u8   bNumFrameDescriptors;
422   u8   guidFormat[16];
423   u8   bBitsPerPixel;
424   u8   bDefaultFrameIndex;
425   u8   bAspectRatioX;
426   u8   bAspectRatioY;
427   u8   bmInterlaceFlags;
428   u8   bCopyProtect;
429   u8   bVariableSize;
430 } __attribute__((packed));
431 
432 /*
433  * Frame Based payload Video Frame Descriptors, USB_Video_Payload_Frame_Based_1.1, section 3.1.2
434  */
435 
436 #define DECLARE_UVC_FRAME_BASED_FRAME_DESCRITPROR(n_) \
437 struct uvc_frame_based_frame_descriptor ## n_         \
438 {                                                     \
439   u8           bLength;                               \
440   u8           bDescriptorType;                       \
441   u8           bDescriptorSubtype;                    \
442   u8           bFrameIndex;                           \
443   u8           bmCapabilities;                        \
444   u16          wWidth;                                \
445   u16          wHeight;                               \
446   u32          dwMinBitRate;                          \
447   u32          dwMaxBitRate;                          \
448   u32          dwDefaultFrameInterval;                \
449   u8           bFrameIntervalType;                    \
450   u32          dwBytesPerLine;                        \
451   u32          dwFrameInterval[n_];                   \
452 } __attribute__((packed))
453 
454 DECLARE_UVC_FRAME_BASED_FRAME_DESCRITPROR(3);
455 
456 /*
457  * H.264 Payload Video Format Descriptor, USB_Video_Payload_H264_1.5, section 3.1.1
458  */
459 
460 struct uvc_h264_format_descriptor
461 {
462   u8    bLength;
463   u8    bDescriptorType;
464   u8    bDescriptorSubtype;
465   u8    bFormatIndex;
466   u8    bNumFrameDescriptors;
467   u8    bDefaultFrameIndex;
468   u8    bMaxCodecConfigDelay;
469   u8    bmSupportedSliceModes;
470   u8    bmSupportedSyncFrameTypes;
471   u8    bResolutionScaling;
472   u8    Reserved1;
473   u8    bmSupportedRateControlModes;
474   u16   wMaxMBperSec_No[4];
475   u16   wMaxMBperSec_Temp[4];
476   u16   wMaxMBperSec_Qual[4];
477   u16   wMaxMBperSec_Spat[4];
478   u16   wMaxMBperSec_Full[4];
479 } __attribute__((packed));
480 
481 /*
482  * H.264 Payload Video Frame Descriptor, section 3.1.2
483  */
484 
485 #define DECLARE_UVC_H264_FRAME_DESCRIPTOR(n_) \
486 struct uvc_h264_frame_descriptor ## n_        \
487 {                                             \
488   u8    bLength;                              \
489   u8    bDescriptorType;                      \
490   u8    bDescriptorSubtype;                   \
491   u8    bFrameIndex;                          \
492   u16   wWidth;                               \
493   u16   wHeight;                              \
494   u16   wSARwidth;                            \
495   u16   wSARheight;                           \
496   u16   wProfile;                             \
497   u8    bLevelIDC;                            \
498   u16   wConstrainedToolset;                  \
499   u32   bmSupportedUsages;                    \
500   u16   bmCapabilities;                       \
501   u32   bmSVCCapabilities;                    \
502   u32   bmMVCCapabilities;                    \
503   u32   dwMinBitRate;                         \
504   u32   dwMaxBitRate;                         \
505   u32   dwDefaultFrameInterval;               \
506   u8    bNumFrameIntervals;                   \
507   u8    dwFrameIntervals[n_];                 \
508 } __attribute__((packed))
509 
510 DECLARE_UVC_H264_FRAME_DESCRIPTOR(1);
511 
512 #define DECLARE_UVC_MJPEG_FRAME_DESCRIPTOR(n_) \
513 struct uvc_mjpg_frame_descriptor ## n_         \
514 {                                              \
515   u8    bLength;                               \
516   u8    bDescriptorType;                       \
517   u8    bDescriptorSubType;                    \
518   u8    bFrameIndex;                           \
519   u8    bmCapabilities;                        \
520   u16   wWidth;                                \
521   u16   wHeight;                               \
522   u32   dwMinBitRate;                          \
523   u32   dwMaxBitRate;                          \
524   u32   dwMaxVideoFrameBufferSize;             \
525   u32   dwDefaultFrameInterval;                \
526   u8    bFrameIntervalType;                    \
527   u32   dwFrameInterval[n_];                   \
528 } __attribute__ ((packed))
529 
530 DECLARE_UVC_MJPEG_FRAME_DESCRIPTOR(1);
531 
532 /*
533  * UVC color-matching descriptor, UVC spec., section 3.9.2.6
534  */
535 
536 struct uvc_color_matching_descriptor
537 {
538   u8   bLength;
539   u8   bDescriptorType;
540   u8   bDescriptorSubtype;
541   u8   bColorPrimaries;
542   u8   bTransferCharacteristics;
543   u8   bMatrixCoefficients;
544 } __attribute__((packed));
545 
546 /* Color Primaries and the reference white */
547 
548 #define UVC_COLOR_UNSPECIFIED      0x0
549 #define UVC_COLOR_BT709_SRGB       0x1
550 #define UVC_COLOR_BT4702M          0x2
551 #define UVC_COLOR_BT4702BG         0x3
552 #define UVC_COLOR_SMPTE_170M       0x4
553 #define UVC_COLOR_SMPTE_240M       0x5
554 
555 /* Color transfer characteristics, gamma function ??? */
556 
557 #define UVC_COLOR_LINEAR           0x6
558 #define UVC_COLOR_SRGB             0x7
559 
560 /* Matrix Coefficients */
561 
562 #define UVC_COLOR_MC_UNSPECIFIED   0x0
563 #define UVC_COLOR_MC_BT709         0x1
564 #define UVC_COLOR_MC_FCC           0x2
565 #define UVC_COLOR_MC_BT4702BG      0x3
566 #define UVC_COLOR_MC_SMPTE_170M    0x4
567 #define UVC_COLOR_MC_SMPTE_240M    0x5
568 
569 #define fourcc(a, b, c, d) \
570   ((u32)(a) | ((u32)(b) << 8) | ((u32)(c) << 16) | ((u32)(d) << 24))
571 
572 #define V4L2_PIX_FMT_YUYV fourcc('Y', 'U', 'Y', 'V')  /* 16  YUV 4:2:2 */
573 #define V4L2_PIX_FMT_MJPEG fourcc('M', 'J', 'P', 'G') /* Motion-JPEG */
574 #define V4L2_PIX_FMT_H264 fourcc('H', '2', '6', '4')  /* H264 with start codes */
575 
576 /*
577  * UVC VideoControl interrupt endpoint descriptor, UVC spec. 1.5, section 3.8.2.2
578  */
579 
580 struct uvc_vc_int_endpoint_descriptor
581 {
582   u8    bLength;
583   u8    bDescriptorType;
584   u8    bDescriptorSubtype;
585   u16   wMaxTransferSize;
586 } __attribute__((packed));
587 
588 /*
589  * Video Class-specific Endpoint Descriptor Subtypes, UVC spec. 1.5, section A.7
590  */
591 
592 #define UVC_EP_UNDEFINED   0x0
593 #define UVC_EP_GENERAL     0x1
594 #define UVC_EP_ENDPOINT    0x2
595 #define UVC_EP_INTERRUPT   0x3
596 
597 /*
598  * VideoStreaming Interface Control Selectors, UVC spec. 1.5, section A.9.8
599  */
600 
601 #define USB_UVC_VS_CONTROL_UNDEFINED              0x00
602 #define USB_UVC_VS_PROBE_CONTROL                  0x01
603 #define USB_UVC_VS_COMMIT_CONTROL                 0x02
604 #define USB_UVC_VS_STILL_PROBE_CONTROL            0x03
605 #define USB_UVC_VS_STILL_COMMIT_CONTROL           0x04
606 #define USB_UVC_VS_STILL_IMAGE_TRIGGER_CONTROL    0x05
607 #define USB_UVC_VS_STREAM_ERROR_CODE_CONTROL      0x06
608 #define USB_UVC_VS_GENERATE_KEY_FRAME_CONTROL     0x07
609 #define USB_UVC_VS_UPDATE_FRAME_SEGMENT_CONTROL   0x08
610 #define USB_UVC_VS_SYNCH_DELAY_CONTROL            0x09
611 
612 #define USB_UVC_INTERFACE_CONTROL                 0x0
613 #define USB_UVC_INTERFACE_STREAMING               0x1
614 
615 /*
616  * Video Class-specific Request Codes, UVC spec. 1.5, section A.8
617  */
618 
619 #define UVC_RC_SETCUR       0x01
620 #define UVC_RC_SETCUR_ALL   0x11
621 #define UVC_RC_GETCUR       0x81
622 #define UVC_RC_GETMIN       0x82
623 #define UVC_RC_GETMAX       0x83
624 #define UVC_RC_GETRES       0x84
625 #define UVC_RC_GETLEN       0x85
626 #define UVC_RC_GETINFO      0x86
627 #define UVC_RC_GETDEF       0x87
628 #define UVC_RC_GETCUR_ALL   0x91
629 #define UVC_RC_GETMIN_ALL   0x92
630 #define UVC_RC_GETMAX_ALL   0x93
631 #define UVC_RC_GETRES_ALL   0x94
632 #define UVC_RC_GETDEF_ALL   0x95
633 
634 /*
635  * Video probe and commit controls, UVC spec. 1.1, section 4.3.1.1
636  */
637 
638 struct uvc_probe_commit_control1_1
639 {
640   u16   bmHint;
641   u8    bFormatIndex;
642   u8    bFrameIndex;
643   u32   dwFrameInterval;
644   u16   wKeyFrameRate;
645   u16   wPFrameRate;
646   u16   wCompQuality;
647   u16   wCompWindowSize;
648   u16   wDelay;
649   u32   dwMaxVideoFrameSize;
650   u32   dwMaxPayloadTransferSize;
651   u32   dwClockFrequency;
652   u8    bmFramingInfo;
653   u8    bPreferedVersion;
654   u8    bMinVersion;
655   u8    bMaxVersion;
656 } __attribute__ ((packed));
657 
658 /*
659  * Video probe and commit controls, UVC spec. 1.5, section 4.3.1.1
660  */
661 
662 struct uvc_probe_commit_control1_5
663 {
664   u16   bmHint;
665   u8    bFormatIndex;
666   u8    bFrameIndex;
667   u32   dwFrameInterval;
668   u16   wKeyFrameRate;
669   u16   wPFrameRate;
670   u16   wCompQuality;
671   u16   wCompWindowSize;
672   u16   wDelay;
673   u32   dwMaxVideoFrameSize;
674   u32   dwMaxPayloadTransferSize;
675   u32   dwClockFrequency;
676   u8    bmFramingInfo;
677   u8    bPreferedVersion;
678   u8    bMinVersion;
679   u8    bMaxVersion;
680   u8    bUsage;
681   u8    bBitDepthLuma;
682   u8    bmSettings;
683   u8    bMaxNumberOfRefFramesPlus1;
684   u8    bmRateControlModes;
685   u8    bmLayoutPerStream;
686 } __attribute__((packed));
687 
688 enum stream_status
689 {
690   STREAM_ON = 0,
691   STREAM_OFF,
692 };
693 
694 struct uvc_probe_t
695 {
696   u8 set;
697   u8 get;
698   u8 max;
699   u8 min;
700 };
701 
702 struct uvc_dev_s
703 {
704   uint8_t                                   *descs;         /* pointer to the concatenated descriptors */
705   uint32_t                                   desl;          /* length of all descriptors */
706   uint32_t                                   control;       /* Class-specific control request value */
707   struct uvc_probe_commit_control1_1         probe;         /* probe control structure */
708   struct uvc_probe_commit_control1_1         commit;        /* probe control structure */
709   struct usbdev_ep_s                        *ctrl_ep;       /* control transfer endpoint */
710   struct usbdev_ep_s                        *vs_ep;         /* Video Streaming endpoint */
711 
712   struct usbdev_req_s                        ctrlreq;       /* control request */
713   struct usbdev_req_s                        vc_ctrlreq;    /* Video Control request */
714   struct usbdev_req_s                        streamreq;     /* Video Streaming request */
715   uint32_t                                   dyn_fc;        /* dynamic format change, non-zero indicates that format change occurred */
716 
717   uint8_t                                   *stream_buf;    /* Buffer used for stream transfer */
718 
719   struct uvc_uncompressed_frame_descriptor3 *frame_desc;    /* pointer to the frame descriptor */
720   int                                        connected;     /* Connected to USB host? */
721 
722   void                                      *uvc_handle;    /* UVC transmission handle */
723   uint32_t                                   fid;           /* Frame Identifier */
724   uint64_t                                   last_complete; /* Last Video stream complete tick */
725 
726   struct uvc_format_info                     format_info;
727   uint32_t                                   bulk_size;
728   uint32_t                                   imgsize;
729   struct uvc_probe_t                         probe_status;
730 
731   uint32_t                                   zero_packet_flag;
732   enum stream_status                         transfer_status;
733   spinlock_t                                 lock;
734   uint32_t                                   event_id;
735   uint32_t                                   unit_id;
736 };
737 
738 struct uvc_driver_s
739 {
740   struct usbdevclass_driver_s  drvr;
741   struct uvc_dev_s            *dev;
742 };
743 
744 /* private UVC device driver instance structure */
745 
746 struct uvc_softc
747 {
748   struct uvc_dev_s    dev;
749   struct uvc_driver_s drvr;
750 } __attribute__((packed));
751 
752 /*
753  * structure used when transimitting video stream with nocp method
754  */
755 
756 struct nocp_reserve
757 {
758   uint8_t  *res;   /* pointer to the reserved data */
759   uint32_t  resl;  /* reserved data length */
760 #define NOCP_RESERVE_MAX   0x4
761   uint8_t res_buf[NOCP_RESERVE_MAX];   /* reserved data */
762 };
763 
764 /*
765  * UVC handle structure, includes various related information to
766  * transmit video stream over USB protocol.
767  */
768 
769 struct uvc_handle
770 {
771   u32 vid_w;                    /* Video Frame width in pixels */
772   u32 vid_h;                    /* Video Frame height in pixels */
773   u32 vid_f;                    /* Video Frame format */
774   u32 state;                    /* state of UVC handle */
775   uvc_continue_func copy_func;  /* video transmission video frame copy function */
776   uvc_continue_func next_func;  /* video transmission video next frame function */
777   void *_priv;                  /* video transmission private pointer */
778 
779   u32 retransmission;
780   u32 used;
781   u32 offset;
782 
783   u32 frame_off;                     /* transfer offset in a single video frame */
784   struct uvc_transfer_data  tdata;   /* transfer data information */
785   struct nocp_reserve       reserve; /* reserve structure */
786   struct uvc_dev_s         *fuvc;    /* pointer to the UVC function structure */
787 };
788 
789 /*
790  * function to check whether we've already connected to USB host,
791  * return zero if not yet connected, non-zero if connected.
792  */
793 
794 extern int fuvc_host_connected(struct uvc_dev_s *fuvc);
795 
796 /*
797  * function to initiate video stream transmission
798  */
799 
800 extern int fuvc_transfer_initiate(struct uvc_dev_s *fuvc);
801 
802 struct uvc_isoc_transfer
803 {
804   uint8_t *data;   /* where the data should be copied or stored */
805   u32 length;      /* length of data expected or valid */
806   u32 reserved;    /* how much data should be reserved */
807   u32 res_next;    /* how much data expected to be reserved upon next request */
808   u32 last;        /* last piece of data in the frame? */
809 };
810 
811 /*
812  * transfer continue function, defined in `usbd_video.c
813  */
814 
815 extern int uvc_continue_transfer(struct uvc_dev_s *fuvc, struct uvc_isoc_transfer *tran);
816 extern void uvc_delete_mutex(void);
817 extern bool uvc_is_running(void);
818 extern int uvc_stream_init(struct uvc_dev_s *fuvc);
819 
820 struct fuvc_frame_info
821 {
822   uint32_t width;
823   uint32_t height;
824   uint32_t intervals[8];
825 };
826 
827 struct fuvc_format_info
828 {
829   uint32_t fcc;
830   const struct fuvc_frame_info *frames;
831 };
832 
833 extern void fuvc_frame_descriptors_get(struct fuvc_format_info *format_info);
834 extern void fuvc_fill_streaming_control(struct uvc_dev_s *fuvc, struct uvc_probe_commit_control1_1 *ctrl,
835                                         int iframe, int iformat);
836 extern void usbdev_uvc_initialize_sub(struct composite_devdesc_s *dev, int ifnobase, int minor);
837 extern void usbclass_uvc_stream_ep_reset(struct uvc_dev_s *fuvc);
838 extern struct usb_endpoint_descriptor g_fuvc_hs_bulk_streaming_ep;
839 extern struct usb_endpoint_descriptor g_fuvc_hs_isoc_streaming_ep;
840 extern volatile unsigned int g_start_transfer;
841 extern EVENT_CB_S g_uvc_event;
842 extern void uvc_unit_control_register(void);
843 
844 #ifdef __cplusplus
845 #if __cplusplus
846 }
847 #endif /* __cplusplus */
848 #endif /* __cplusplus */
849 
850 #endif
851