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 221 struct gpio_desc; 222 struct uvc_device; 223 224 /* TODO: Put the most frequently accessed fields at the beginning of 225 * structures to maximize cache efficiency. 226 */ 227 struct uvc_control_info { 228 struct list_head mappings; 229 230 u8 entity[16]; 231 u8 index; /* Bit index in bmControls */ 232 u8 selector; 233 234 u16 size; 235 u32 flags; 236 }; 237 238 struct uvc_control_mapping { 239 struct list_head list; 240 struct list_head ev_subs; 241 242 u32 id; 243 u8 name[32]; 244 u8 entity[16]; 245 u8 selector; 246 247 u8 size; 248 u8 offset; 249 enum v4l2_ctrl_type v4l2_type; 250 u32 data_type; 251 252 const struct uvc_menu_info *menu_info; 253 u32 menu_count; 254 255 u32 master_id; 256 s32 master_manual; 257 u32 slave_ids[2]; 258 259 s32 (*get)(struct uvc_control_mapping *mapping, u8 query, 260 const u8 *data); 261 void (*set)(struct uvc_control_mapping *mapping, s32 value, 262 u8 *data); 263 }; 264 265 struct uvc_control { 266 struct uvc_entity *entity; 267 struct uvc_control_info info; 268 269 u8 index; /* Used to match the uvc_control entry with a 270 uvc_control_info. */ 271 u8 dirty:1, 272 loaded:1, 273 modified:1, 274 cached:1, 275 initialized:1; 276 277 u8 *uvc_data; 278 279 struct uvc_fh *handle; /* File handle that last changed the control. */ 280 }; 281 282 struct uvc_format_desc { 283 char *name; 284 u8 guid[16]; 285 u32 fcc; 286 }; 287 288 /* The term 'entity' refers to both UVC units and UVC terminals. 289 * 290 * The type field is either the terminal type (wTerminalType in the terminal 291 * descriptor), or the unit type (bDescriptorSubtype in the unit descriptor). 292 * As the bDescriptorSubtype field is one byte long, the type value will 293 * always have a null MSB for units. All terminal types defined by the UVC 294 * specification have a non-null MSB, so it is safe to use the MSB to 295 * differentiate between units and terminals as long as the descriptor parsing 296 * code makes sure terminal types have a non-null MSB. 297 * 298 * For terminals, the type's most significant bit stores the terminal 299 * direction (either UVC_TERM_INPUT or UVC_TERM_OUTPUT). The type field should 300 * always be accessed with the UVC_ENTITY_* macros and never directly. 301 */ 302 303 #define UVC_ENTITY_FLAG_DEFAULT (1 << 0) 304 305 struct uvc_entity { 306 struct list_head list; /* Entity as part of a UVC device. */ 307 struct list_head chain; /* Entity as part of a video device 308 * chain. */ 309 unsigned int flags; 310 311 /* 312 * Entities exposed by the UVC device use IDs 0-255, extra entities 313 * implemented by the driver (such as the GPIO entity) use IDs 256 and 314 * up. 315 */ 316 u16 id; 317 u16 type; 318 char name[64]; 319 u8 guid[16]; 320 321 /* Media controller-related fields. */ 322 struct video_device *vdev; 323 struct v4l2_subdev subdev; 324 unsigned int num_pads; 325 unsigned int num_links; 326 struct media_pad *pads; 327 328 union { 329 struct { 330 u16 wObjectiveFocalLengthMin; 331 u16 wObjectiveFocalLengthMax; 332 u16 wOcularFocalLength; 333 u8 bControlSize; 334 u8 *bmControls; 335 } camera; 336 337 struct { 338 u8 bControlSize; 339 u8 *bmControls; 340 u8 bTransportModeSize; 341 u8 *bmTransportModes; 342 } media; 343 344 struct { 345 } output; 346 347 struct { 348 u16 wMaxMultiplier; 349 u8 bControlSize; 350 u8 *bmControls; 351 u8 bmVideoStandards; 352 } processing; 353 354 struct { 355 } selector; 356 357 struct { 358 u8 bNumControls; 359 u8 bControlSize; 360 u8 *bmControls; 361 u8 *bmControlsType; 362 } extension; 363 364 struct { 365 u8 bControlSize; 366 u8 *bmControls; 367 struct gpio_desc *gpio_privacy; 368 int irq; 369 } gpio; 370 }; 371 372 u8 bNrInPins; 373 u8 *baSourceID; 374 375 int (*get_info)(struct uvc_device *dev, struct uvc_entity *entity, 376 u8 cs, u8 *caps); 377 int (*get_cur)(struct uvc_device *dev, struct uvc_entity *entity, 378 u8 cs, void *data, u16 size); 379 380 unsigned int ncontrols; 381 struct uvc_control *controls; 382 }; 383 384 struct uvc_frame { 385 u8 bFrameIndex; 386 u8 bmCapabilities; 387 u16 wWidth; 388 u16 wHeight; 389 u32 dwMinBitRate; 390 u32 dwMaxBitRate; 391 u32 dwMaxVideoFrameBufferSize; 392 u8 bFrameIntervalType; 393 u32 dwDefaultFrameInterval; 394 u32 *dwFrameInterval; 395 }; 396 397 struct uvc_format { 398 u8 type; 399 u8 index; 400 u8 bpp; 401 enum v4l2_colorspace colorspace; 402 enum v4l2_xfer_func xfer_func; 403 enum v4l2_ycbcr_encoding ycbcr_enc; 404 u32 fcc; 405 u32 flags; 406 407 char name[32]; 408 409 unsigned int nframes; 410 struct uvc_frame *frame; 411 }; 412 413 struct uvc_streaming_header { 414 u8 bNumFormats; 415 u8 bEndpointAddress; 416 u8 bTerminalLink; 417 u8 bControlSize; 418 u8 *bmaControls; 419 /* The following fields are used by input headers only. */ 420 u8 bmInfo; 421 u8 bStillCaptureMethod; 422 u8 bTriggerSupport; 423 u8 bTriggerUsage; 424 }; 425 426 enum uvc_buffer_state { 427 UVC_BUF_STATE_IDLE = 0, 428 UVC_BUF_STATE_QUEUED = 1, 429 UVC_BUF_STATE_ACTIVE = 2, 430 UVC_BUF_STATE_READY = 3, 431 UVC_BUF_STATE_DONE = 4, 432 UVC_BUF_STATE_ERROR = 5, 433 }; 434 435 struct uvc_buffer { 436 struct vb2_v4l2_buffer buf; 437 struct list_head queue; 438 439 enum uvc_buffer_state state; 440 unsigned int error; 441 442 void *mem; 443 unsigned int length; 444 unsigned int bytesused; 445 446 u32 pts; 447 448 /* Asynchronous buffer handling. */ 449 struct kref ref; 450 }; 451 452 #define UVC_QUEUE_DISCONNECTED (1 << 0) 453 #define UVC_QUEUE_DROP_CORRUPTED (1 << 1) 454 455 struct uvc_video_queue { 456 struct vb2_queue queue; 457 struct mutex mutex; /* Protects queue */ 458 459 unsigned int flags; 460 unsigned int buf_used; 461 462 spinlock_t irqlock; /* Protects irqqueue */ 463 struct list_head irqqueue; 464 }; 465 466 struct uvc_video_chain { 467 struct uvc_device *dev; 468 struct list_head list; 469 470 struct list_head entities; /* All entities */ 471 struct uvc_entity *processing; /* Processing unit */ 472 struct uvc_entity *selector; /* Selector unit */ 473 474 struct mutex ctrl_mutex; /* Protects ctrl.info */ 475 476 struct v4l2_prio_state prio; /* V4L2 priority state */ 477 u32 caps; /* V4L2 chain-wide caps */ 478 }; 479 480 struct uvc_stats_frame { 481 unsigned int size; /* Number of bytes captured */ 482 unsigned int first_data; /* Index of the first non-empty packet */ 483 484 unsigned int nb_packets; /* Number of packets */ 485 unsigned int nb_empty; /* Number of empty packets */ 486 unsigned int nb_invalid; /* Number of packets with an invalid header */ 487 unsigned int nb_errors; /* Number of packets with the error bit set */ 488 489 unsigned int nb_pts; /* Number of packets with a PTS timestamp */ 490 unsigned int nb_pts_diffs; /* Number of PTS differences inside a frame */ 491 unsigned int last_pts_diff; /* Index of the last PTS difference */ 492 bool has_initial_pts; /* Whether the first non-empty packet has a PTS */ 493 bool has_early_pts; /* Whether a PTS is present before the first non-empty packet */ 494 u32 pts; /* PTS of the last packet */ 495 496 unsigned int nb_scr; /* Number of packets with a SCR timestamp */ 497 unsigned int nb_scr_diffs; /* Number of SCR.STC differences inside a frame */ 498 u16 scr_sof; /* SCR.SOF of the last packet */ 499 u32 scr_stc; /* SCR.STC of the last packet */ 500 }; 501 502 struct uvc_stats_stream { 503 ktime_t start_ts; /* Stream start timestamp */ 504 ktime_t stop_ts; /* Stream stop timestamp */ 505 506 unsigned int nb_frames; /* Number of frames */ 507 508 unsigned int nb_packets; /* Number of packets */ 509 unsigned int nb_empty; /* Number of empty packets */ 510 unsigned int nb_invalid; /* Number of packets with an invalid header */ 511 unsigned int nb_errors; /* Number of packets with the error bit set */ 512 513 unsigned int nb_pts_constant; /* Number of frames with constant PTS */ 514 unsigned int nb_pts_early; /* Number of frames with early PTS */ 515 unsigned int nb_pts_initial; /* Number of frames with initial PTS */ 516 517 unsigned int nb_scr_count_ok; /* Number of frames with at least one SCR per non empty packet */ 518 unsigned int nb_scr_diffs_ok; /* Number of frames with varying SCR.STC */ 519 unsigned int scr_sof_count; /* STC.SOF counter accumulated since stream start */ 520 unsigned int scr_sof; /* STC.SOF of the last packet */ 521 unsigned int min_sof; /* Minimum STC.SOF value */ 522 unsigned int max_sof; /* Maximum STC.SOF value */ 523 }; 524 525 #define UVC_METADATA_BUF_SIZE 1024 526 527 /** 528 * struct uvc_copy_op: Context structure to schedule asynchronous memcpy 529 * 530 * @buf: active buf object for this operation 531 * @dst: copy destination address 532 * @src: copy source address 533 * @len: copy length 534 */ 535 struct uvc_copy_op { 536 struct uvc_buffer *buf; 537 void *dst; 538 const __u8 *src; 539 size_t len; 540 }; 541 542 /** 543 * struct uvc_urb - URB context management structure 544 * 545 * @urb: the URB described by this context structure 546 * @stream: UVC streaming context 547 * @buffer: memory storage for the URB 548 * @dma: DMA coherent addressing for the urb_buffer 549 * @async_operations: counter to indicate the number of copy operations 550 * @copy_operations: work descriptors for asynchronous copy operations 551 * @work: work queue entry for asynchronous decode 552 */ 553 struct uvc_urb { 554 struct urb *urb; 555 struct uvc_streaming *stream; 556 557 char *buffer; 558 dma_addr_t dma; 559 560 unsigned int async_operations; 561 struct uvc_copy_op copy_operations[UVC_MAX_PACKETS]; 562 struct work_struct work; 563 }; 564 565 struct uvc_streaming { 566 struct list_head list; 567 struct uvc_device *dev; 568 struct video_device vdev; 569 struct uvc_video_chain *chain; 570 atomic_t active; 571 572 struct usb_interface *intf; 573 int intfnum; 574 u16 maxpsize; 575 576 struct uvc_streaming_header header; 577 enum v4l2_buf_type type; 578 579 unsigned int nformats; 580 struct uvc_format *format; 581 582 struct uvc_streaming_control ctrl; 583 struct uvc_format *def_format; 584 struct uvc_format *cur_format; 585 struct uvc_frame *cur_frame; 586 587 /* Protect access to ctrl, cur_format, cur_frame and hardware video 588 * probe control. 589 */ 590 struct mutex mutex; 591 592 /* Buffers queue. */ 593 unsigned int frozen : 1; 594 struct uvc_video_queue queue; 595 struct workqueue_struct *async_wq; 596 void (*decode)(struct uvc_urb *uvc_urb, struct uvc_buffer *buf, 597 struct uvc_buffer *meta_buf); 598 599 struct { 600 struct video_device vdev; 601 struct uvc_video_queue queue; 602 u32 format; 603 } meta; 604 605 /* Context data used by the bulk completion handler. */ 606 struct { 607 u8 header[256]; 608 unsigned int header_size; 609 int skip_payload; 610 u32 payload_size; 611 u32 max_payload_size; 612 } bulk; 613 614 struct uvc_urb uvc_urb[UVC_URBS]; 615 unsigned int urb_size; 616 617 u32 sequence; 618 u8 last_fid; 619 620 /* debugfs */ 621 struct dentry *debugfs_dir; 622 struct { 623 struct uvc_stats_frame frame; 624 struct uvc_stats_stream stream; 625 } stats; 626 627 /* Timestamps support. */ 628 struct uvc_clock { 629 struct uvc_clock_sample { 630 u32 dev_stc; 631 u16 dev_sof; 632 u16 host_sof; 633 ktime_t host_time; 634 } *samples; 635 636 unsigned int head; 637 unsigned int count; 638 unsigned int size; 639 640 u16 last_sof; 641 u16 sof_offset; 642 643 u8 last_scr[6]; 644 645 spinlock_t lock; 646 } clock; 647 }; 648 649 #define for_each_uvc_urb(uvc_urb, uvc_streaming) \ 650 for ((uvc_urb) = &(uvc_streaming)->uvc_urb[0]; \ 651 (uvc_urb) < &(uvc_streaming)->uvc_urb[UVC_URBS]; \ 652 ++(uvc_urb)) 653 654 static inline u32 uvc_urb_index(const struct uvc_urb *uvc_urb) 655 { 656 return uvc_urb - &uvc_urb->stream->uvc_urb[0]; 657 } 658 659 struct uvc_device_info { 660 u32 quirks; 661 u32 meta_format; 662 u16 uvc_version; 663 }; 664 665 struct uvc_device { 666 struct usb_device *udev; 667 struct usb_interface *intf; 668 unsigned long warnings; 669 u32 quirks; 670 int intfnum; 671 char name[32]; 672 673 const struct uvc_device_info *info; 674 675 struct mutex lock; /* Protects users */ 676 unsigned int users; 677 atomic_t nmappings; 678 679 /* Video control interface */ 680 #ifdef CONFIG_MEDIA_CONTROLLER 681 struct media_device mdev; 682 #endif 683 struct v4l2_device vdev; 684 u16 uvc_version; 685 u32 clock_frequency; 686 687 struct list_head entities; 688 struct list_head chains; 689 690 /* Video Streaming interfaces */ 691 struct list_head streams; 692 struct kref ref; 693 694 /* Status Interrupt Endpoint */ 695 struct usb_host_endpoint *int_ep; 696 struct urb *int_urb; 697 u8 *status; 698 struct input_dev *input; 699 char input_phys[64]; 700 701 struct uvc_ctrl_work { 702 struct work_struct work; 703 struct urb *urb; 704 struct uvc_video_chain *chain; 705 struct uvc_control *ctrl; 706 const void *data; 707 } async_ctrl; 708 709 struct uvc_entity *gpio_unit; 710 }; 711 712 enum uvc_handle_state { 713 UVC_HANDLE_PASSIVE = 0, 714 UVC_HANDLE_ACTIVE = 1, 715 }; 716 717 struct uvc_fh { 718 struct v4l2_fh vfh; 719 struct uvc_video_chain *chain; 720 struct uvc_streaming *stream; 721 enum uvc_handle_state state; 722 }; 723 724 struct uvc_driver { 725 struct usb_driver driver; 726 }; 727 728 /* ------------------------------------------------------------------------ 729 * Debugging, printing and logging 730 */ 731 732 #define UVC_DBG_PROBE (1 << 0) 733 #define UVC_DBG_DESCR (1 << 1) 734 #define UVC_DBG_CONTROL (1 << 2) 735 #define UVC_DBG_FORMAT (1 << 3) 736 #define UVC_DBG_CAPTURE (1 << 4) 737 #define UVC_DBG_CALLS (1 << 5) 738 #define UVC_DBG_FRAME (1 << 7) 739 #define UVC_DBG_SUSPEND (1 << 8) 740 #define UVC_DBG_STATUS (1 << 9) 741 #define UVC_DBG_VIDEO (1 << 10) 742 #define UVC_DBG_STATS (1 << 11) 743 #define UVC_DBG_CLOCK (1 << 12) 744 745 #define UVC_WARN_MINMAX 0 746 #define UVC_WARN_PROBE_DEF 1 747 #define UVC_WARN_XU_GET_RES 2 748 749 extern unsigned int uvc_clock_param; 750 extern unsigned int uvc_no_drop_param; 751 extern unsigned int uvc_dbg_param; 752 extern unsigned int uvc_timeout_param; 753 extern unsigned int uvc_hw_timestamps_param; 754 755 #define uvc_dbg(_dev, flag, fmt, ...) \ 756 do { \ 757 if (uvc_dbg_param & UVC_DBG_##flag) \ 758 dev_printk(KERN_DEBUG, &(_dev)->udev->dev, fmt, \ 759 ##__VA_ARGS__); \ 760 } while (0) 761 762 #define uvc_dbg_cont(flag, fmt, ...) \ 763 do { \ 764 if (uvc_dbg_param & UVC_DBG_##flag) \ 765 pr_cont(fmt, ##__VA_ARGS__); \ 766 } while (0) 767 768 #define uvc_warn_once(_dev, warn, fmt, ...) \ 769 do { \ 770 if (!test_and_set_bit(warn, &(_dev)->warnings)) \ 771 dev_info(&(_dev)->udev->dev, fmt, ##__VA_ARGS__); \ 772 } while (0) 773 774 /* -------------------------------------------------------------------------- 775 * Internal functions. 776 */ 777 778 /* Core driver */ 779 extern struct uvc_driver uvc_driver; 780 781 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id); 782 783 /* Video buffers queue management. */ 784 int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, 785 int drop_corrupted); 786 void uvc_queue_release(struct uvc_video_queue *queue); 787 int uvc_request_buffers(struct uvc_video_queue *queue, 788 struct v4l2_requestbuffers *rb); 789 int uvc_query_buffer(struct uvc_video_queue *queue, 790 struct v4l2_buffer *v4l2_buf); 791 int uvc_create_buffers(struct uvc_video_queue *queue, 792 struct v4l2_create_buffers *v4l2_cb); 793 int uvc_queue_buffer(struct uvc_video_queue *queue, 794 struct media_device *mdev, 795 struct v4l2_buffer *v4l2_buf); 796 int uvc_export_buffer(struct uvc_video_queue *queue, 797 struct v4l2_exportbuffer *exp); 798 int uvc_dequeue_buffer(struct uvc_video_queue *queue, 799 struct v4l2_buffer *v4l2_buf, int nonblocking); 800 int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type); 801 int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type); 802 void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect); 803 struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, 804 struct uvc_buffer *buf); 805 struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue); 806 void uvc_queue_buffer_release(struct uvc_buffer *buf); 807 int uvc_queue_mmap(struct uvc_video_queue *queue, 808 struct vm_area_struct *vma); 809 __poll_t uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, 810 poll_table *wait); 811 #ifndef CONFIG_MMU 812 unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, 813 unsigned long pgoff); 814 #endif 815 int uvc_queue_allocated(struct uvc_video_queue *queue); 816 static inline int uvc_queue_streaming(struct uvc_video_queue *queue) 817 { 818 return vb2_is_streaming(&queue->queue); 819 } 820 821 static inline struct uvc_streaming * 822 uvc_queue_to_stream(struct uvc_video_queue *queue) 823 { 824 return container_of(queue, struct uvc_streaming, queue); 825 } 826 827 /* V4L2 interface */ 828 extern const struct v4l2_ioctl_ops uvc_ioctl_ops; 829 extern const struct v4l2_file_operations uvc_fops; 830 831 /* Media controller */ 832 int uvc_mc_register_entities(struct uvc_video_chain *chain); 833 void uvc_mc_cleanup_entity(struct uvc_entity *entity); 834 835 /* Video */ 836 int uvc_video_init(struct uvc_streaming *stream); 837 int uvc_video_suspend(struct uvc_streaming *stream); 838 int uvc_video_resume(struct uvc_streaming *stream, int reset); 839 int uvc_video_start_streaming(struct uvc_streaming *stream); 840 void uvc_video_stop_streaming(struct uvc_streaming *stream); 841 int uvc_probe_video(struct uvc_streaming *stream, 842 struct uvc_streaming_control *probe); 843 int uvc_query_ctrl(struct uvc_device *dev, u8 query, u8 unit, 844 u8 intfnum, u8 cs, void *data, u16 size); 845 void uvc_video_clock_update(struct uvc_streaming *stream, 846 struct vb2_v4l2_buffer *vbuf, 847 struct uvc_buffer *buf); 848 int uvc_meta_register(struct uvc_streaming *stream); 849 850 int uvc_register_video_device(struct uvc_device *dev, 851 struct uvc_streaming *stream, 852 struct video_device *vdev, 853 struct uvc_video_queue *queue, 854 enum v4l2_buf_type type, 855 const struct v4l2_file_operations *fops, 856 const struct v4l2_ioctl_ops *ioctl_ops); 857 858 /* Status */ 859 int uvc_status_init(struct uvc_device *dev); 860 void uvc_status_unregister(struct uvc_device *dev); 861 void uvc_status_cleanup(struct uvc_device *dev); 862 int uvc_status_start(struct uvc_device *dev, gfp_t flags); 863 void uvc_status_stop(struct uvc_device *dev); 864 865 /* Controls */ 866 extern const struct v4l2_subscribed_event_ops uvc_ctrl_sub_ev_ops; 867 868 int uvc_query_v4l2_ctrl(struct uvc_video_chain *chain, 869 struct v4l2_queryctrl *v4l2_ctrl); 870 int uvc_query_v4l2_menu(struct uvc_video_chain *chain, 871 struct v4l2_querymenu *query_menu); 872 873 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain, 874 const struct uvc_control_mapping *mapping); 875 int uvc_ctrl_init_device(struct uvc_device *dev); 876 void uvc_ctrl_cleanup_device(struct uvc_device *dev); 877 int uvc_ctrl_restore_values(struct uvc_device *dev); 878 bool uvc_ctrl_status_event_async(struct urb *urb, struct uvc_video_chain *chain, 879 struct uvc_control *ctrl, const u8 *data); 880 void uvc_ctrl_status_event(struct uvc_video_chain *chain, 881 struct uvc_control *ctrl, const u8 *data); 882 883 int uvc_ctrl_begin(struct uvc_video_chain *chain); 884 int __uvc_ctrl_commit(struct uvc_fh *handle, int rollback, 885 const struct v4l2_ext_control *xctrls, 886 unsigned int xctrls_count); 887 static inline int uvc_ctrl_commit(struct uvc_fh *handle, 888 const struct v4l2_ext_control *xctrls, 889 unsigned int xctrls_count) 890 { 891 return __uvc_ctrl_commit(handle, 0, xctrls, xctrls_count); 892 } 893 static inline int uvc_ctrl_rollback(struct uvc_fh *handle) 894 { 895 return __uvc_ctrl_commit(handle, 1, NULL, 0); 896 } 897 898 int uvc_ctrl_get(struct uvc_video_chain *chain, struct v4l2_ext_control *xctrl); 899 int uvc_ctrl_set(struct uvc_fh *handle, struct v4l2_ext_control *xctrl); 900 901 int uvc_xu_ctrl_query(struct uvc_video_chain *chain, 902 struct uvc_xu_control_query *xqry); 903 904 /* Utility functions */ 905 void uvc_simplify_fraction(u32 *numerator, u32 *denominator, 906 unsigned int n_terms, unsigned int threshold); 907 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator); 908 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts, 909 u8 epaddr); 910 911 /* Quirks support */ 912 void uvc_video_decode_isight(struct uvc_urb *uvc_urb, 913 struct uvc_buffer *buf, 914 struct uvc_buffer *meta_buf); 915 916 /* debugfs and statistics */ 917 void uvc_debugfs_init(void); 918 void uvc_debugfs_cleanup(void); 919 void uvc_debugfs_init_stream(struct uvc_streaming *stream); 920 void uvc_debugfs_cleanup_stream(struct uvc_streaming *stream); 921 922 size_t uvc_video_stats_dump(struct uvc_streaming *stream, char *buf, 923 size_t size); 924 925 #endif 926