Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _USB_VIDEO_H_
0003 #define _USB_VIDEO_H_
0004 
0005 #ifndef __KERNEL__
0006 #error "The uvcvideo.h header is deprecated, use linux/uvcvideo.h instead."
0007 #endif /* __KERNEL__ */
0008 
0009 #include <linux/atomic.h>
0010 #include <linux/kernel.h>
0011 #include <linux/poll.h>
0012 #include <linux/usb.h>
0013 #include <linux/usb/video.h>
0014 #include <linux/uvcvideo.h>
0015 #include <linux/videodev2.h>
0016 #include <linux/workqueue.h>
0017 #include <media/media-device.h>
0018 #include <media/v4l2-device.h>
0019 #include <media/v4l2-event.h>
0020 #include <media/v4l2-fh.h>
0021 #include <media/videobuf2-v4l2.h>
0022 
0023 /* --------------------------------------------------------------------------
0024  * UVC constants
0025  */
0026 
0027 #define UVC_TERM_INPUT          0x0000
0028 #define UVC_TERM_OUTPUT         0x8000
0029 #define UVC_TERM_DIRECTION(term)    ((term)->type & 0x8000)
0030 
0031 #define UVC_ENTITY_TYPE(entity)     ((entity)->type & 0x7fff)
0032 #define UVC_ENTITY_IS_UNIT(entity)  (((entity)->type & 0xff00) == 0)
0033 #define UVC_ENTITY_IS_TERM(entity)  (((entity)->type & 0xff00) != 0)
0034 #define UVC_ENTITY_IS_ITERM(entity) \
0035     (UVC_ENTITY_IS_TERM(entity) && \
0036     ((entity)->type & 0x8000) == UVC_TERM_INPUT)
0037 #define UVC_ENTITY_IS_OTERM(entity) \
0038     (UVC_ENTITY_IS_TERM(entity) && \
0039     ((entity)->type & 0x8000) == UVC_TERM_OUTPUT)
0040 
0041 #define UVC_EXT_GPIO_UNIT       0x7ffe
0042 #define UVC_EXT_GPIO_UNIT_ID        0x100
0043 
0044 /* ------------------------------------------------------------------------
0045  * GUIDs
0046  */
0047 #define UVC_GUID_UVC_CAMERA \
0048     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0049      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}
0050 #define UVC_GUID_UVC_OUTPUT \
0051     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0052      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}
0053 #define UVC_GUID_UVC_MEDIA_TRANSPORT_INPUT \
0054     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0055      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
0056 #define UVC_GUID_UVC_PROCESSING \
0057     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0058      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01}
0059 #define UVC_GUID_UVC_SELECTOR \
0060     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0061      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02}
0062 #define UVC_GUID_EXT_GPIO_CONTROLLER \
0063     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
0064      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03}
0065 
0066 #define UVC_GUID_FORMAT_MJPEG \
0067     { 'M',  'J',  'P',  'G', 0x00, 0x00, 0x10, 0x00, \
0068      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0069 #define UVC_GUID_FORMAT_YUY2 \
0070     { 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
0071      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0072 #define UVC_GUID_FORMAT_YUY2_ISIGHT \
0073     { 'Y',  'U',  'Y',  '2', 0x00, 0x00, 0x10, 0x00, \
0074      0x80, 0x00, 0x00, 0x00, 0x00, 0x38, 0x9b, 0x71}
0075 #define UVC_GUID_FORMAT_NV12 \
0076     { 'N',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
0077      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0078 #define UVC_GUID_FORMAT_YV12 \
0079     { 'Y',  'V',  '1',  '2', 0x00, 0x00, 0x10, 0x00, \
0080      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0081 #define UVC_GUID_FORMAT_I420 \
0082     { 'I',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
0083      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0084 #define UVC_GUID_FORMAT_UYVY \
0085     { 'U',  'Y',  'V',  'Y', 0x00, 0x00, 0x10, 0x00, \
0086      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0087 #define UVC_GUID_FORMAT_Y800 \
0088     { 'Y',  '8',  '0',  '0', 0x00, 0x00, 0x10, 0x00, \
0089      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0090 #define UVC_GUID_FORMAT_Y8 \
0091     { 'Y',  '8',  ' ',  ' ', 0x00, 0x00, 0x10, 0x00, \
0092      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0093 #define UVC_GUID_FORMAT_Y10 \
0094     { 'Y',  '1',  '0',  ' ', 0x00, 0x00, 0x10, 0x00, \
0095      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0096 #define UVC_GUID_FORMAT_Y12 \
0097     { 'Y',  '1',  '2',  ' ', 0x00, 0x00, 0x10, 0x00, \
0098      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0099 #define UVC_GUID_FORMAT_Y16 \
0100     { 'Y',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
0101      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0102 #define UVC_GUID_FORMAT_BY8 \
0103     { 'B',  'Y',  '8',  ' ', 0x00, 0x00, 0x10, 0x00, \
0104      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0105 #define UVC_GUID_FORMAT_BA81 \
0106     { 'B',  'A',  '8',  '1', 0x00, 0x00, 0x10, 0x00, \
0107      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0108 #define UVC_GUID_FORMAT_GBRG \
0109     { 'G',  'B',  'R',  'G', 0x00, 0x00, 0x10, 0x00, \
0110      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0111 #define UVC_GUID_FORMAT_GRBG \
0112     { 'G',  'R',  'B',  'G', 0x00, 0x00, 0x10, 0x00, \
0113      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0114 #define UVC_GUID_FORMAT_RGGB \
0115     { 'R',  'G',  'G',  'B', 0x00, 0x00, 0x10, 0x00, \
0116      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0117 #define UVC_GUID_FORMAT_BG16 \
0118     { 'B',  'G',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
0119      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0120 #define UVC_GUID_FORMAT_GB16 \
0121     { 'G',  'B',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
0122      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0123 #define UVC_GUID_FORMAT_RG16 \
0124     { 'R',  'G',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
0125      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0126 #define UVC_GUID_FORMAT_GR16 \
0127     { 'G',  'R',  '1',  '6', 0x00, 0x00, 0x10, 0x00, \
0128      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0129 #define UVC_GUID_FORMAT_RGBP \
0130     { 'R',  'G',  'B',  'P', 0x00, 0x00, 0x10, 0x00, \
0131      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0132 #define UVC_GUID_FORMAT_BGR3 \
0133     { 0x7d, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, \
0134      0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70}
0135 #define UVC_GUID_FORMAT_M420 \
0136     { 'M',  '4',  '2',  '0', 0x00, 0x00, 0x10, 0x00, \
0137      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0138 
0139 #define UVC_GUID_FORMAT_H264 \
0140     { 'H',  '2',  '6',  '4', 0x00, 0x00, 0x10, 0x00, \
0141      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0142 #define UVC_GUID_FORMAT_H265 \
0143     { 'H',  '2',  '6',  '5', 0x00, 0x00, 0x10, 0x00, \
0144      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0145 #define UVC_GUID_FORMAT_Y8I \
0146     { 'Y',  '8',  'I',  ' ', 0x00, 0x00, 0x10, 0x00, \
0147      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0148 #define UVC_GUID_FORMAT_Y12I \
0149     { 'Y',  '1',  '2',  'I', 0x00, 0x00, 0x10, 0x00, \
0150      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0151 #define UVC_GUID_FORMAT_Z16 \
0152     { 'Z',  '1',  '6',  ' ', 0x00, 0x00, 0x10, 0x00, \
0153      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0154 #define UVC_GUID_FORMAT_RW10 \
0155     { 'R',  'W',  '1',  '0', 0x00, 0x00, 0x10, 0x00, \
0156      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0157 #define UVC_GUID_FORMAT_INVZ \
0158     { 'I',  'N',  'V',  'Z', 0x90, 0x2d, 0x58, 0x4a, \
0159      0x92, 0x0b, 0x77, 0x3f, 0x1f, 0x2c, 0x55, 0x6b}
0160 #define UVC_GUID_FORMAT_INZI \
0161     { 'I',  'N',  'Z',  'I', 0x66, 0x1a, 0x42, 0xa2, \
0162      0x90, 0x65, 0xd0, 0x18, 0x14, 0xa8, 0xef, 0x8a}
0163 #define UVC_GUID_FORMAT_INVI \
0164     { 'I',  'N',  'V',  'I', 0xdb, 0x57, 0x49, 0x5e, \
0165      0x8e, 0x3f, 0xf4, 0x79, 0x53, 0x2b, 0x94, 0x6f}
0166 #define UVC_GUID_FORMAT_CNF4 \
0167     { 'C',  ' ',  ' ',  ' ', 0x00, 0x00, 0x10, 0x00, \
0168      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0169 
0170 #define UVC_GUID_FORMAT_D3DFMT_L8 \
0171     {0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, \
0172      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0173 #define UVC_GUID_FORMAT_KSMEDIA_L8_IR \
0174     {0x32, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00, \
0175      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0176 
0177 #define UVC_GUID_FORMAT_HEVC \
0178     { 'H',  'E',  'V',  'C', 0x00, 0x00, 0x10, 0x00, \
0179      0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}
0180 
0181 
0182 /* ------------------------------------------------------------------------
0183  * Driver specific constants.
0184  */
0185 
0186 #define DRIVER_VERSION      "1.1.1"
0187 
0188 /* Number of isochronous URBs. */
0189 #define UVC_URBS        5
0190 /* Maximum number of packets per URB. */
0191 #define UVC_MAX_PACKETS     32
0192 /* Maximum status buffer size in bytes of interrupt URB. */
0193 #define UVC_MAX_STATUS_SIZE 16
0194 
0195 #define UVC_CTRL_CONTROL_TIMEOUT    5000
0196 #define UVC_CTRL_STREAMING_TIMEOUT  5000
0197 
0198 /* Maximum allowed number of control mappings per device */
0199 #define UVC_MAX_CONTROL_MAPPINGS    1024
0200 #define UVC_MAX_CONTROL_MENU_ENTRIES    32
0201 
0202 /* Devices quirks */
0203 #define UVC_QUIRK_STATUS_INTERVAL   0x00000001
0204 #define UVC_QUIRK_PROBE_MINMAX      0x00000002
0205 #define UVC_QUIRK_PROBE_EXTRAFIELDS 0x00000004
0206 #define UVC_QUIRK_BUILTIN_ISIGHT    0x00000008
0207 #define UVC_QUIRK_STREAM_NO_FID     0x00000010
0208 #define UVC_QUIRK_IGNORE_SELECTOR_UNIT  0x00000020
0209 #define UVC_QUIRK_FIX_BANDWIDTH     0x00000080
0210 #define UVC_QUIRK_PROBE_DEF     0x00000100
0211 #define UVC_QUIRK_RESTRICT_FRAME_RATE   0x00000200
0212 #define UVC_QUIRK_RESTORE_CTRLS_ON_INIT 0x00000400
0213 #define UVC_QUIRK_FORCE_Y8      0x00000800
0214 #define UVC_QUIRK_FORCE_BPP     0x00001000
0215 
0216 /* Format flags */
0217 #define UVC_FMT_FLAG_COMPRESSED     0x00000001
0218 #define UVC_FMT_FLAG_STREAM     0x00000002
0219 
0220 /* ------------------------------------------------------------------------
0221  * Structures.
0222  */
0223 
0224 struct gpio_desc;
0225 struct sg_table;
0226 struct uvc_device;
0227 
0228 /*
0229  * TODO: Put the most frequently accessed fields at the beginning of
0230  * structures to maximize cache efficiency.
0231  */
0232 struct uvc_control_info {
0233     struct list_head mappings;
0234 
0235     u8 entity[16];
0236     u8 index;   /* Bit index in bmControls */
0237     u8 selector;
0238 
0239     u16 size;
0240     u32 flags;
0241 };
0242 
0243 struct uvc_control_mapping {
0244     struct list_head list;
0245     struct list_head ev_subs;
0246 
0247     u32 id;
0248     char *name;
0249     u8 entity[16];
0250     u8 selector;
0251 
0252     u8 size;
0253     u8 offset;
0254     enum v4l2_ctrl_type v4l2_type;
0255     u32 data_type;
0256 
0257     const struct uvc_menu_info *menu_info;
0258     u32 menu_count;
0259 
0260     u32 master_id;
0261     s32 master_manual;
0262     u32 slave_ids[2];
0263 
0264     s32 (*get)(struct uvc_control_mapping *mapping, u8 query,
0265            const u8 *data);
0266     void (*set)(struct uvc_control_mapping *mapping, s32 value,
0267             u8 *data);
0268 };
0269 
0270 struct uvc_control {
0271     struct uvc_entity *entity;
0272     struct uvc_control_info info;
0273 
0274     u8 index;   /* Used to match the uvc_control entry with a uvc_control_info. */
0275     u8 dirty:1,
0276        loaded:1,
0277        modified:1,
0278        cached:1,
0279        initialized:1;
0280 
0281     u8 *uvc_data;
0282 
0283     struct uvc_fh *handle;  /* File handle that last changed the control. */
0284 };
0285 
0286 struct uvc_format_desc {
0287     char *name;
0288     u8 guid[16];
0289     u32 fcc;
0290 };
0291 
0292 /*
0293  * The term 'entity' refers to both UVC units and UVC terminals.
0294  *
0295  * The type field is either the terminal type (wTerminalType in the terminal
0296  * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor).
0297  * As the bDescriptorSubtype field is one byte long, the type value will
0298  * always have a null MSB for units. All terminal types defined by the UVC
0299  * specification have a non-null MSB, so it is safe to use the MSB to
0300  * differentiate between units and terminals as long as the descriptor parsing
0301  * code makes sure terminal types have a non-null MSB.
0302  *
0303  * For terminals, the type's most significant bit stores the terminal
0304  * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should
0305  * always be accessed with the UVC_ENTITY_* macros and never directly.
0306  */
0307 
0308 #define UVC_ENTITY_FLAG_DEFAULT     (1 << 0)
0309 
0310 struct uvc_entity {
0311     struct list_head list;      /* Entity as part of a UVC device. */
0312     struct list_head chain;     /* Entity as part of a video device chain. */
0313     unsigned int flags;
0314 
0315     /*
0316      * Entities exposed by the UVC device use IDs 0-255, extra entities
0317      * implemented by the driver (such as the GPIO entity) use IDs 256 and
0318      * up.
0319      */
0320     u16 id;
0321     u16 type;
0322     char name[64];
0323     u8 guid[16];
0324 
0325     /* Media controller-related fields. */
0326     struct video_device *vdev;
0327     struct v4l2_subdev subdev;
0328     unsigned int num_pads;
0329     unsigned int num_links;
0330     struct media_pad *pads;
0331 
0332     union {
0333         struct {
0334             u16 wObjectiveFocalLengthMin;
0335             u16 wObjectiveFocalLengthMax;
0336             u16 wOcularFocalLength;
0337             u8  bControlSize;
0338             u8  *bmControls;
0339         } camera;
0340 
0341         struct {
0342             u8  bControlSize;
0343             u8  *bmControls;
0344             u8  bTransportModeSize;
0345             u8  *bmTransportModes;
0346         } media;
0347 
0348         struct {
0349         } output;
0350 
0351         struct {
0352             u16 wMaxMultiplier;
0353             u8  bControlSize;
0354             u8  *bmControls;
0355             u8  bmVideoStandards;
0356         } processing;
0357 
0358         struct {
0359         } selector;
0360 
0361         struct {
0362             u8  bNumControls;
0363             u8  bControlSize;
0364             u8  *bmControls;
0365             u8  *bmControlsType;
0366         } extension;
0367 
0368         struct {
0369             u8  bControlSize;
0370             u8  *bmControls;
0371             struct gpio_desc *gpio_privacy;
0372             int irq;
0373         } gpio;
0374     };
0375 
0376     u8 bNrInPins;
0377     u8 *baSourceID;
0378 
0379     int (*get_info)(struct uvc_device *dev, struct uvc_entity *entity,
0380             u8 cs, u8 *caps);
0381     int (*get_cur)(struct uvc_device *dev, struct uvc_entity *entity,
0382                u8 cs, void *data, u16 size);
0383 
0384     unsigned int ncontrols;
0385     struct uvc_control *controls;
0386 };
0387 
0388 struct uvc_frame {
0389     u8  bFrameIndex;
0390     u8  bmCapabilities;
0391     u16 wWidth;
0392     u16 wHeight;
0393     u32 dwMinBitRate;
0394     u32 dwMaxBitRate;
0395     u32 dwMaxVideoFrameBufferSize;
0396     u8  bFrameIntervalType;
0397     u32 dwDefaultFrameInterval;
0398     u32 *dwFrameInterval;
0399 };
0400 
0401 struct uvc_format {
0402     u8 type;
0403     u8 index;
0404     u8 bpp;
0405     enum v4l2_colorspace colorspace;
0406     enum v4l2_xfer_func xfer_func;
0407     enum v4l2_ycbcr_encoding ycbcr_enc;
0408     u32 fcc;
0409     u32 flags;
0410 
0411     char name[32];
0412 
0413     unsigned int nframes;
0414     struct uvc_frame *frame;
0415 };
0416 
0417 struct uvc_streaming_header {
0418     u8 bNumFormats;
0419     u8 bEndpointAddress;
0420     u8 bTerminalLink;
0421     u8 bControlSize;
0422     u8 *bmaControls;
0423     /* The following fields are used by input headers only. */
0424     u8 bmInfo;
0425     u8 bStillCaptureMethod;
0426     u8 bTriggerSupport;
0427     u8 bTriggerUsage;
0428 };
0429 
0430 enum uvc_buffer_state {
0431     UVC_BUF_STATE_IDLE  = 0,
0432     UVC_BUF_STATE_QUEUED    = 1,
0433     UVC_BUF_STATE_ACTIVE    = 2,
0434     UVC_BUF_STATE_READY = 3,
0435     UVC_BUF_STATE_DONE  = 4,
0436     UVC_BUF_STATE_ERROR = 5,
0437 };
0438 
0439 struct uvc_buffer {
0440     struct vb2_v4l2_buffer buf;
0441     struct list_head queue;
0442 
0443     enum uvc_buffer_state state;
0444     unsigned int error;
0445 
0446     void *mem;
0447     unsigned int length;
0448     unsigned int bytesused;
0449 
0450     u32 pts;
0451 
0452     /* Asynchronous buffer handling. */
0453     struct kref ref;
0454 };
0455 
0456 #define UVC_QUEUE_DISCONNECTED      (1 << 0)
0457 #define UVC_QUEUE_DROP_CORRUPTED    (1 << 1)
0458 
0459 struct uvc_video_queue {
0460     struct vb2_queue queue;
0461     struct mutex mutex;         /* Protects queue */
0462 
0463     unsigned int flags;
0464     unsigned int buf_used;
0465 
0466     spinlock_t irqlock;         /* Protects irqqueue */
0467     struct list_head irqqueue;
0468 };
0469 
0470 struct uvc_video_chain {
0471     struct uvc_device *dev;
0472     struct list_head list;
0473 
0474     struct list_head entities;      /* All entities */
0475     struct uvc_entity *processing;      /* Processing unit */
0476     struct uvc_entity *selector;        /* Selector unit */
0477 
0478     struct mutex ctrl_mutex;        /* Protects ctrl.info */
0479 
0480     struct v4l2_prio_state prio;        /* V4L2 priority state */
0481     u32 caps;               /* V4L2 chain-wide caps */
0482     u8 ctrl_class_bitmap;           /* Bitmap of valid classes */
0483 };
0484 
0485 struct uvc_stats_frame {
0486     unsigned int size;      /* Number of bytes captured */
0487     unsigned int first_data;    /* Index of the first non-empty packet */
0488 
0489     unsigned int nb_packets;    /* Number of packets */
0490     unsigned int nb_empty;      /* Number of empty packets */
0491     unsigned int nb_invalid;    /* Number of packets with an invalid header */
0492     unsigned int nb_errors;     /* Number of packets with the error bit set */
0493 
0494     unsigned int nb_pts;        /* Number of packets with a PTS timestamp */
0495     unsigned int nb_pts_diffs;  /* Number of PTS differences inside a frame */
0496     unsigned int last_pts_diff; /* Index of the last PTS difference */
0497     bool has_initial_pts;       /* Whether the first non-empty packet has a PTS */
0498     bool has_early_pts;     /* Whether a PTS is present before the first non-empty packet */
0499     u32 pts;            /* PTS of the last packet */
0500 
0501     unsigned int nb_scr;        /* Number of packets with a SCR timestamp */
0502     unsigned int nb_scr_diffs;  /* Number of SCR.STC differences inside a frame */
0503     u16 scr_sof;            /* SCR.SOF of the last packet */
0504     u32 scr_stc;            /* SCR.STC of the last packet */
0505 };
0506 
0507 struct uvc_stats_stream {
0508     ktime_t start_ts;       /* Stream start timestamp */
0509     ktime_t stop_ts;        /* Stream stop timestamp */
0510 
0511     unsigned int nb_frames;     /* Number of frames */
0512 
0513     unsigned int nb_packets;    /* Number of packets */
0514     unsigned int nb_empty;      /* Number of empty packets */
0515     unsigned int nb_invalid;    /* Number of packets with an invalid header */
0516     unsigned int nb_errors;     /* Number of packets with the error bit set */
0517 
0518     unsigned int nb_pts_constant;   /* Number of frames with constant PTS */
0519     unsigned int nb_pts_early;  /* Number of frames with early PTS */
0520     unsigned int nb_pts_initial;    /* Number of frames with initial PTS */
0521 
0522     unsigned int nb_scr_count_ok;   /* Number of frames with at least one SCR per non empty packet */
0523     unsigned int nb_scr_diffs_ok;   /* Number of frames with varying SCR.STC */
0524     unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */
0525     unsigned int scr_sof;       /* STC.SOF of the last packet */
0526     unsigned int min_sof;       /* Minimum STC.SOF value */
0527     unsigned int max_sof;       /* Maximum STC.SOF value */
0528 };
0529 
0530 #define UVC_METADATA_BUF_SIZE 10240
0531 
0532 /**
0533  * struct uvc_copy_op: Context structure to schedule asynchronous memcpy
0534  *
0535  * @buf: active buf object for this operation
0536  * @dst: copy destination address
0537  * @src: copy source address
0538  * @len: copy length
0539  */
0540 struct uvc_copy_op {
0541     struct uvc_buffer *buf;
0542     void *dst;
0543     const __u8 *src;
0544     size_t len;
0545 };
0546 
0547 /**
0548  * struct uvc_urb - URB context management structure
0549  *
0550  * @urb: the URB described by this context structure
0551  * @stream: UVC streaming context
0552  * @buffer: memory storage for the URB
0553  * @dma: Allocated DMA handle
0554  * @sgt: sgt_table with the urb locations in memory
0555  * @async_operations: counter to indicate the number of copy operations
0556  * @copy_operations: work descriptors for asynchronous copy operations
0557  * @work: work queue entry for asynchronous decode
0558  */
0559 struct uvc_urb {
0560     struct urb *urb;
0561     struct uvc_streaming *stream;
0562 
0563     char *buffer;
0564     dma_addr_t dma;
0565     struct sg_table *sgt;
0566 
0567     unsigned int async_operations;
0568     struct uvc_copy_op copy_operations[UVC_MAX_PACKETS];
0569     struct work_struct work;
0570 };
0571 
0572 struct uvc_streaming {
0573     struct list_head list;
0574     struct uvc_device *dev;
0575     struct video_device vdev;
0576     struct uvc_video_chain *chain;
0577     atomic_t active;
0578 
0579     struct usb_interface *intf;
0580     int intfnum;
0581     u16 maxpsize;
0582 
0583     struct uvc_streaming_header header;
0584     enum v4l2_buf_type type;
0585 
0586     unsigned int nformats;
0587     struct uvc_format *format;
0588 
0589     struct uvc_streaming_control ctrl;
0590     struct uvc_format *def_format;
0591     struct uvc_format *cur_format;
0592     struct uvc_frame *cur_frame;
0593 
0594     /*
0595      * Protect access to ctrl, cur_format, cur_frame and hardware video
0596      * probe control.
0597      */
0598     struct mutex mutex;
0599 
0600     /* Buffers queue. */
0601     unsigned int frozen : 1;
0602     struct uvc_video_queue queue;
0603     struct workqueue_struct *async_wq;
0604     void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf,
0605                struct uvc_buffer *meta_buf);
0606 
0607     struct {
0608         struct video_device vdev;
0609         struct uvc_video_queue queue;
0610         u32 format;
0611     } meta;
0612 
0613     /* Context data used by the bulk completion handler. */
0614     struct {
0615         u8 header[256];
0616         unsigned int header_size;
0617         int skip_payload;
0618         u32 payload_size;
0619         u32 max_payload_size;
0620     } bulk;
0621 
0622     struct uvc_urb uvc_urb[UVC_URBS];
0623     unsigned int urb_size;
0624 
0625     u32 sequence;
0626     u8 last_fid;
0627 
0628     /* debugfs */
0629     struct dentry *debugfs_dir;
0630     struct {
0631         struct uvc_stats_frame frame;
0632         struct uvc_stats_stream stream;
0633     } stats;
0634 
0635     /* Timestamps support. */
0636     struct uvc_clock {
0637         struct uvc_clock_sample {
0638             u32 dev_stc;
0639             u16 dev_sof;
0640             u16 host_sof;
0641             ktime_t host_time;
0642         } *samples;
0643 
0644         unsigned int head;
0645         unsigned int count;
0646         unsigned int size;
0647 
0648         u16 last_sof;
0649         u16 sof_offset;
0650 
0651         u8 last_scr[6];
0652 
0653         spinlock_t lock;
0654     } clock;
0655 };
0656 
0657 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \
0658     for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \
0659          (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \
0660          ++(uvc_urb))
0661 
0662 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb)
0663 {
0664     return uvc_urb - &uvc_urb->stream->uvc_urb[0];
0665 }
0666 
0667 struct uvc_device_info {
0668     u32 quirks;
0669     u32 meta_format;
0670     u16 uvc_version;
0671     const struct uvc_control_mapping **mappings;
0672 };
0673 
0674 struct uvc_device {
0675     struct usb_device *udev;
0676     struct usb_interface *intf;
0677     unsigned long warnings;
0678     u32 quirks;
0679     int intfnum;
0680     char name[32];
0681 
0682     const struct uvc_device_info *info;
0683 
0684     struct mutex lock;      /* Protects users */
0685     unsigned int users;
0686     atomic_t nmappings;
0687 
0688     /* Video control interface */
0689 #ifdef CONFIG_MEDIA_CONTROLLER
0690     struct media_device mdev;
0691 #endif
0692     struct v4l2_device vdev;
0693     u16 uvc_version;
0694     u32 clock_frequency;
0695 
0696     struct list_head entities;
0697     struct list_head chains;
0698 
0699     /* Video Streaming interfaces */
0700     struct list_head streams;
0701     struct kref ref;
0702 
0703     /* Status Interrupt Endpoint */
0704     struct usb_host_endpoint *int_ep;
0705     struct urb *int_urb;
0706     u8 *status;
0707     struct input_dev *input;
0708     char input_phys[64];
0709 
0710     struct uvc_ctrl_work {
0711         struct work_struct work;
0712         struct urb *urb;
0713         struct uvc_video_chain *chain;
0714         struct uvc_control *ctrl;
0715         const void *data;
0716     } async_ctrl;
0717 
0718     struct uvc_entity *gpio_unit;
0719 };
0720 
0721 enum uvc_handle_state {
0722     UVC_HANDLE_PASSIVE  = 0,
0723     UVC_HANDLE_ACTIVE   = 1,
0724 };
0725 
0726 struct uvc_fh {
0727     struct v4l2_fh vfh;
0728     struct uvc_video_chain *chain;
0729     struct uvc_streaming *stream;
0730     enum uvc_handle_state state;
0731 };
0732 
0733 struct uvc_driver {
0734     struct usb_driver driver;
0735 };
0736 
0737 /* ------------------------------------------------------------------------
0738  * Debugging, printing and logging
0739  */
0740 
0741 #define UVC_DBG_PROBE       (1 << 0)
0742 #define UVC_DBG_DESCR       (1 << 1)
0743 #define UVC_DBG_CONTROL     (1 << 2)
0744 #define UVC_DBG_FORMAT      (1 << 3)
0745 #define UVC_DBG_CAPTURE     (1 << 4)
0746 #define UVC_DBG_CALLS       (1 << 5)
0747 #define UVC_DBG_FRAME       (1 << 7)
0748 #define UVC_DBG_SUSPEND     (1 << 8)
0749 #define UVC_DBG_STATUS      (1 << 9)
0750 #define UVC_DBG_VIDEO       (1 << 10)
0751 #define UVC_DBG_STATS       (1 << 11)
0752 #define UVC_DBG_CLOCK       (1 << 12)
0753 
0754 #define UVC_WARN_MINMAX     0
0755 #define UVC_WARN_PROBE_DEF  1
0756 #define UVC_WARN_XU_GET_RES 2
0757 
0758 extern unsigned int uvc_clock_param;
0759 extern unsigned int uvc_no_drop_param;
0760 extern unsigned int uvc_dbg_param;
0761 extern unsigned int uvc_timeout_param;
0762 extern unsigned int uvc_hw_timestamps_param;
0763 
0764 #define uvc_dbg(_dev, flag, fmt, ...)                   \
0765 do {                                    \
0766     if (uvc_dbg_param & UVC_DBG_##flag)             \
0767         dev_printk(KERN_DEBUG, &(_dev)->udev->dev, fmt,     \
0768                ##__VA_ARGS__);              \
0769 } while (0)
0770 
0771 #define uvc_dbg_cont(flag, fmt, ...)                    \
0772 do {                                    \
0773     if (uvc_dbg_param & UVC_DBG_##flag)             \
0774         pr_cont(fmt, ##__VA_ARGS__);                \
0775 } while (0)
0776 
0777 #define uvc_warn_once(_dev, warn, fmt, ...)             \
0778 do {                                    \
0779     if (!test_and_set_bit(warn, &(_dev)->warnings))         \
0780         dev_info(&(_dev)->udev->dev, fmt, ##__VA_ARGS__);   \
0781 } while (0)
0782 
0783 /* --------------------------------------------------------------------------
0784  * Internal functions.
0785  */
0786 
0787 /* Core driver */
0788 extern struct uvc_driver uvc_driver;
0789 
0790 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id);
0791 
0792 /* Video buffers queue management. */
0793 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
0794            int drop_corrupted);
0795 void uvc_queue_release(struct uvc_video_queue *queue);
0796 int uvc_request_buffers(struct uvc_video_queue *queue,
0797             struct v4l2_requestbuffers *rb);
0798 int uvc_query_buffer(struct uvc_video_queue *queue,
0799              struct v4l2_buffer *v4l2_buf);
0800 int uvc_create_buffers(struct uvc_video_queue *queue,
0801                struct v4l2_create_buffers *v4l2_cb);
0802 int uvc_queue_buffer(struct uvc_video_queue *queue,
0803              struct media_device *mdev,
0804              struct v4l2_buffer *v4l2_buf);
0805 int uvc_export_buffer(struct uvc_video_queue *queue,
0806               struct v4l2_exportbuffer *exp);
0807 int uvc_dequeue_buffer(struct uvc_video_queue *queue,
0808                struct v4l2_buffer *v4l2_buf, int nonblocking);
0809 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type);
0810 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type);
0811 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect);
0812 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
0813                      struct uvc_buffer *buf);
0814 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue);
0815 void uvc_queue_buffer_release(struct uvc_buffer *buf);
0816 int uvc_queue_mmap(struct uvc_video_queue *queue,
0817            struct vm_area_struct *vma);
0818 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
0819             poll_table *wait);
0820 #ifndef CONFIG_MMU
0821 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
0822                       unsigned long pgoff);
0823 #endif
0824 int uvc_queue_allocated(struct uvc_video_queue *queue);
0825 static inline int uvc_queue_streaming(struct uvc_video_queue *queue)
0826 {
0827     return vb2_is_streaming(&queue->queue);
0828 }
0829 
0830 static inline struct uvc_streaming *
0831 uvc_queue_to_stream(struct uvc_video_queue *queue)
0832 {
0833     return container_of(queue, struct uvc_streaming, queue);
0834 }
0835 
0836 /* V4L2 interface */
0837 extern const struct v4l2_ioctl_ops uvc_ioctl_ops;
0838 extern const struct v4l2_file_operations uvc_fops;
0839 
0840 /* Media controller */
0841 int uvc_mc_register_entities(struct uvc_video_chain *chain);
0842 void uvc_mc_cleanup_entity(struct uvc_entity *entity);
0843 
0844 /* Video */
0845 int uvc_video_init(struct uvc_streaming *stream);
0846 int uvc_video_suspend(struct uvc_streaming *stream);
0847 int uvc_video_resume(struct uvc_streaming *stream, int reset);
0848 int uvc_video_start_streaming(struct uvc_streaming *stream);
0849 void uvc_video_stop_streaming(struct uvc_streaming *stream);
0850 int uvc_probe_video(struct uvc_streaming *stream,
0851             struct uvc_streaming_control *probe);
0852 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit,
0853            u8 intfnum, u8 cs, void *data, u16 size);
0854 void uvc_video_clock_update(struct uvc_streaming *stream,
0855                 struct vb2_v4l2_buffer *vbuf,
0856                 struct uvc_buffer *buf);
0857 int uvc_meta_register(struct uvc_streaming *stream);
0858 
0859 int uvc_register_video_device(struct uvc_device *dev,
0860                   struct uvc_streaming *stream,
0861                   struct video_device *vdev,
0862                   struct uvc_video_queue *queue,
0863                   enum v4l2_buf_type type,
0864                   const struct v4l2_file_operations *fops,
0865                   const struct v4l2_ioctl_ops *ioctl_ops);
0866 
0867 /* Status */
0868 int uvc_status_init(struct uvc_device *dev);
0869 void uvc_status_unregister(struct uvc_device *dev);
0870 void uvc_status_cleanup(struct uvc_device *dev);
0871 int uvc_status_start(struct uvc_device *dev, gfp_t flags);
0872 void uvc_status_stop(struct uvc_device *dev);
0873 
0874 /* Controls */
0875 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops;
0876 
0877 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain,
0878             struct v4l2_queryctrl *v4l2_ctrl);
0879 int uvc_query_v4l2_menu(struct uvc_video_chain *chain,
0880             struct v4l2_querymenu *query_menu);
0881 
0882 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
0883              const struct uvc_control_mapping *mapping);
0884 int uvc_ctrl_init_device(struct uvc_device *dev);
0885 void uvc_ctrl_cleanup_device(struct uvc_device *dev);
0886 int uvc_ctrl_restore_values(struct uvc_device *dev);
0887 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain,
0888                  struct uvc_control *ctrl, const u8 *data);
0889 void uvc_ctrl_status_event(struct uvc_video_chain *chain,
0890                struct uvc_control *ctrl, const u8 *data);
0891 
0892 int uvc_ctrl_begin(struct uvc_video_chain *chain);
0893 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback,
0894               struct v4l2_ext_controls *ctrls);
0895 static inline int uvc_ctrl_commit(struct uvc_fh *handle,
0896                   struct v4l2_ext_controls *ctrls)
0897 {
0898     return __uvc_ctrl_commit(handle, 0, ctrls);
0899 }
0900 static inline int uvc_ctrl_rollback(struct uvc_fh *handle)
0901 {
0902     return __uvc_ctrl_commit(handle, 1, NULL);
0903 }
0904 
0905 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl);
0906 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl);
0907 int uvc_ctrl_is_accessible(struct uvc_video_chain *chain, u32 v4l2_id,
0908                bool read);
0909 
0910 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
0911               struct uvc_xu_control_query *xqry);
0912 
0913 /* Utility functions */
0914 void uvc_simplify_fraction(u32 *numerator, u32 *denominator,
0915                unsigned int n_terms, unsigned int threshold);
0916 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator);
0917 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
0918                         u8 epaddr);
0919 u16 uvc_endpoint_max_bpi(struct usb_device *dev, struct usb_host_endpoint *ep);
0920 
0921 /* Quirks support */
0922 void uvc_video_decode_isight(struct uvc_urb *uvc_urb,
0923                  struct uvc_buffer *buf,
0924                  struct uvc_buffer *meta_buf);
0925 
0926 /* debugfs and statistics */
0927 void uvc_debugfs_init(void);
0928 void uvc_debugfs_cleanup(void);
0929 void uvc_debugfs_init_stream(struct uvc_streaming *stream);
0930 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream);
0931 
0932 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf,
0933                 size_t size);
0934 
0935 #endif