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