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