1 /* 2 * uvc_driver.c -- USB Video Class driver 3 * 4 * Copyright (C) 2005-2010 5 * Laurent Pinchart (laurent.pinchart@ideasonboard.com) 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 */ 13 14 #include <linux/atomic.h> 15 #include <linux/kernel.h> 16 #include <linux/list.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/usb.h> 20 #include <linux/videodev2.h> 21 #include <linux/vmalloc.h> 22 #include <linux/wait.h> 23 #include <linux/version.h> 24 #include <asm/unaligned.h> 25 26 #include <media/v4l2-common.h> 27 #include <media/v4l2-ioctl.h> 28 29 #include "uvcvideo.h" 30 31 #define DRIVER_AUTHOR "Laurent Pinchart " \ 32 "<laurent.pinchart@ideasonboard.com>" 33 #define DRIVER_DESC "USB Video Class driver" 34 35 unsigned int uvc_clock_param = CLOCK_MONOTONIC; 36 unsigned int uvc_hw_timestamps_param; 37 unsigned int uvc_no_drop_param; 38 static unsigned int uvc_quirks_param = -1; 39 unsigned int uvc_trace_param; 40 unsigned int uvc_timeout_param = UVC_CTRL_STREAMING_TIMEOUT; 41 42 /* ------------------------------------------------------------------------ 43 * Video formats 44 */ 45 46 static struct uvc_format_desc uvc_fmts[] = { 47 { 48 .name = "YUV 4:2:2 (YUYV)", 49 .guid = UVC_GUID_FORMAT_YUY2, 50 .fcc = V4L2_PIX_FMT_YUYV, 51 }, 52 { 53 .name = "YUV 4:2:2 (YUYV)", 54 .guid = UVC_GUID_FORMAT_YUY2_ISIGHT, 55 .fcc = V4L2_PIX_FMT_YUYV, 56 }, 57 { 58 .name = "YUV 4:2:0 (NV12)", 59 .guid = UVC_GUID_FORMAT_NV12, 60 .fcc = V4L2_PIX_FMT_NV12, 61 }, 62 { 63 .name = "MJPEG", 64 .guid = UVC_GUID_FORMAT_MJPEG, 65 .fcc = V4L2_PIX_FMT_MJPEG, 66 }, 67 { 68 .name = "YVU 4:2:0 (YV12)", 69 .guid = UVC_GUID_FORMAT_YV12, 70 .fcc = V4L2_PIX_FMT_YVU420, 71 }, 72 { 73 .name = "YUV 4:2:0 (I420)", 74 .guid = UVC_GUID_FORMAT_I420, 75 .fcc = V4L2_PIX_FMT_YUV420, 76 }, 77 { 78 .name = "YUV 4:2:0 (M420)", 79 .guid = UVC_GUID_FORMAT_M420, 80 .fcc = V4L2_PIX_FMT_M420, 81 }, 82 { 83 .name = "YUV 4:2:2 (UYVY)", 84 .guid = UVC_GUID_FORMAT_UYVY, 85 .fcc = V4L2_PIX_FMT_UYVY, 86 }, 87 { 88 .name = "Greyscale 8-bit (Y800)", 89 .guid = UVC_GUID_FORMAT_Y800, 90 .fcc = V4L2_PIX_FMT_GREY, 91 }, 92 { 93 .name = "Greyscale 8-bit (Y8 )", 94 .guid = UVC_GUID_FORMAT_Y8, 95 .fcc = V4L2_PIX_FMT_GREY, 96 }, 97 { 98 .name = "Greyscale 8-bit (D3DFMT_L8)", 99 .guid = UVC_GUID_FORMAT_D3DFMT_L8, 100 .fcc = V4L2_PIX_FMT_GREY, 101 }, 102 { 103 .name = "IR 8-bit (L8_IR)", 104 .guid = UVC_GUID_FORMAT_KSMEDIA_L8_IR, 105 .fcc = V4L2_PIX_FMT_GREY, 106 }, 107 { 108 .name = "Greyscale 10-bit (Y10 )", 109 .guid = UVC_GUID_FORMAT_Y10, 110 .fcc = V4L2_PIX_FMT_Y10, 111 }, 112 { 113 .name = "Greyscale 12-bit (Y12 )", 114 .guid = UVC_GUID_FORMAT_Y12, 115 .fcc = V4L2_PIX_FMT_Y12, 116 }, 117 { 118 .name = "Greyscale 16-bit (Y16 )", 119 .guid = UVC_GUID_FORMAT_Y16, 120 .fcc = V4L2_PIX_FMT_Y16, 121 }, 122 { 123 .name = "BGGR Bayer (BY8 )", 124 .guid = UVC_GUID_FORMAT_BY8, 125 .fcc = V4L2_PIX_FMT_SBGGR8, 126 }, 127 { 128 .name = "BGGR Bayer (BA81)", 129 .guid = UVC_GUID_FORMAT_BA81, 130 .fcc = V4L2_PIX_FMT_SBGGR8, 131 }, 132 { 133 .name = "GBRG Bayer (GBRG)", 134 .guid = UVC_GUID_FORMAT_GBRG, 135 .fcc = V4L2_PIX_FMT_SGBRG8, 136 }, 137 { 138 .name = "GRBG Bayer (GRBG)", 139 .guid = UVC_GUID_FORMAT_GRBG, 140 .fcc = V4L2_PIX_FMT_SGRBG8, 141 }, 142 { 143 .name = "RGGB Bayer (RGGB)", 144 .guid = UVC_GUID_FORMAT_RGGB, 145 .fcc = V4L2_PIX_FMT_SRGGB8, 146 }, 147 { 148 .name = "RGB565", 149 .guid = UVC_GUID_FORMAT_RGBP, 150 .fcc = V4L2_PIX_FMT_RGB565, 151 }, 152 { 153 .name = "BGR 8:8:8 (BGR3)", 154 .guid = UVC_GUID_FORMAT_BGR3, 155 .fcc = V4L2_PIX_FMT_BGR24, 156 }, 157 { 158 .name = "H.264", 159 .guid = UVC_GUID_FORMAT_H264, 160 .fcc = V4L2_PIX_FMT_H264, 161 }, 162 { 163 .name = "Greyscale 8 L/R (Y8I)", 164 .guid = UVC_GUID_FORMAT_Y8I, 165 .fcc = V4L2_PIX_FMT_Y8I, 166 }, 167 { 168 .name = "Greyscale 12 L/R (Y12I)", 169 .guid = UVC_GUID_FORMAT_Y12I, 170 .fcc = V4L2_PIX_FMT_Y12I, 171 }, 172 { 173 .name = "Depth data 16-bit (Z16)", 174 .guid = UVC_GUID_FORMAT_Z16, 175 .fcc = V4L2_PIX_FMT_Z16, 176 }, 177 { 178 .name = "Bayer 10-bit (SRGGB10P)", 179 .guid = UVC_GUID_FORMAT_RW10, 180 .fcc = V4L2_PIX_FMT_SRGGB10P, 181 }, 182 { 183 .name = "Bayer 16-bit (SBGGR16)", 184 .guid = UVC_GUID_FORMAT_BG16, 185 .fcc = V4L2_PIX_FMT_SBGGR16, 186 }, 187 { 188 .name = "Bayer 16-bit (SGBRG16)", 189 .guid = UVC_GUID_FORMAT_GB16, 190 .fcc = V4L2_PIX_FMT_SGBRG16, 191 }, 192 { 193 .name = "Bayer 16-bit (SRGGB16)", 194 .guid = UVC_GUID_FORMAT_RG16, 195 .fcc = V4L2_PIX_FMT_SRGGB16, 196 }, 197 { 198 .name = "Bayer 16-bit (SGRBG16)", 199 .guid = UVC_GUID_FORMAT_GR16, 200 .fcc = V4L2_PIX_FMT_SGRBG16, 201 }, 202 { 203 .name = "Depth data 16-bit (Z16)", 204 .guid = UVC_GUID_FORMAT_INVZ, 205 .fcc = V4L2_PIX_FMT_Z16, 206 }, 207 { 208 .name = "Greyscale 10-bit (Y10 )", 209 .guid = UVC_GUID_FORMAT_INVI, 210 .fcc = V4L2_PIX_FMT_Y10, 211 }, 212 { 213 .name = "IR:Depth 26-bit (INZI)", 214 .guid = UVC_GUID_FORMAT_INZI, 215 .fcc = V4L2_PIX_FMT_INZI, 216 }, 217 { 218 .name = "4-bit Depth Confidence (Packed)", 219 .guid = UVC_GUID_FORMAT_CNF4, 220 .fcc = V4L2_PIX_FMT_CNF4, 221 }, 222 }; 223 224 /* ------------------------------------------------------------------------ 225 * Utility functions 226 */ 227 228 struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts, 229 u8 epaddr) 230 { 231 struct usb_host_endpoint *ep; 232 unsigned int i; 233 234 for (i = 0; i < alts->desc.bNumEndpoints; ++i) { 235 ep = &alts->endpoint[i]; 236 if (ep->desc.bEndpointAddress == epaddr) 237 return ep; 238 } 239 240 return NULL; 241 } 242 243 static struct uvc_format_desc *uvc_format_by_guid(const u8 guid[16]) 244 { 245 unsigned int len = ARRAY_SIZE(uvc_fmts); 246 unsigned int i; 247 248 for (i = 0; i < len; ++i) { 249 if (memcmp(guid, uvc_fmts[i].guid, 16) == 0) 250 return &uvc_fmts[i]; 251 } 252 253 return NULL; 254 } 255 256 static u32 uvc_colorspace(const u8 primaries) 257 { 258 static const u8 colorprimaries[] = { 259 0, 260 V4L2_COLORSPACE_SRGB, 261 V4L2_COLORSPACE_470_SYSTEM_M, 262 V4L2_COLORSPACE_470_SYSTEM_BG, 263 V4L2_COLORSPACE_SMPTE170M, 264 V4L2_COLORSPACE_SMPTE240M, 265 }; 266 267 if (primaries < ARRAY_SIZE(colorprimaries)) 268 return colorprimaries[primaries]; 269 270 return 0; 271 } 272 273 /* Simplify a fraction using a simple continued fraction decomposition. The 274 * idea here is to convert fractions such as 333333/10000000 to 1/30 using 275 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two 276 * arbitrary parameters to remove non-significative terms from the simple 277 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold 278 * respectively seems to give nice results. 279 */ 280 void uvc_simplify_fraction(u32 *numerator, u32 *denominator, 281 unsigned int n_terms, unsigned int threshold) 282 { 283 u32 *an; 284 u32 x, y, r; 285 unsigned int i, n; 286 287 an = kmalloc_array(n_terms, sizeof(*an), GFP_KERNEL); 288 if (an == NULL) 289 return; 290 291 /* Convert the fraction to a simple continued fraction. See 292 * http://mathforum.org/dr.math/faq/faq.fractions.html 293 * Stop if the current term is bigger than or equal to the given 294 * threshold. 295 */ 296 x = *numerator; 297 y = *denominator; 298 299 for (n = 0; n < n_terms && y != 0; ++n) { 300 an[n] = x / y; 301 if (an[n] >= threshold) { 302 if (n < 2) 303 n++; 304 break; 305 } 306 307 r = x - an[n] * y; 308 x = y; 309 y = r; 310 } 311 312 /* Expand the simple continued fraction back to an integer fraction. */ 313 x = 0; 314 y = 1; 315 316 for (i = n; i > 0; --i) { 317 r = y; 318 y = an[i-1] * y + x; 319 x = r; 320 } 321 322 *numerator = y; 323 *denominator = x; 324 kfree(an); 325 } 326 327 /* Convert a fraction to a frame interval in 100ns multiples. The idea here is 328 * to compute numerator / denominator * 10000000 using 32 bit fixed point 329 * arithmetic only. 330 */ 331 u32 uvc_fraction_to_interval(u32 numerator, u32 denominator) 332 { 333 u32 multiplier; 334 335 /* Saturate the result if the operation would overflow. */ 336 if (denominator == 0 || 337 numerator/denominator >= ((u32)-1)/10000000) 338 return (u32)-1; 339 340 /* Divide both the denominator and the multiplier by two until 341 * numerator * multiplier doesn't overflow. If anyone knows a better 342 * algorithm please let me know. 343 */ 344 multiplier = 10000000; 345 while (numerator > ((u32)-1)/multiplier) { 346 multiplier /= 2; 347 denominator /= 2; 348 } 349 350 return denominator ? numerator * multiplier / denominator : 0; 351 } 352 353 /* ------------------------------------------------------------------------ 354 * Terminal and unit management 355 */ 356 357 struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id) 358 { 359 struct uvc_entity *entity; 360 361 list_for_each_entry(entity, &dev->entities, list) { 362 if (entity->id == id) 363 return entity; 364 } 365 366 return NULL; 367 } 368 369 static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev, 370 int id, struct uvc_entity *entity) 371 { 372 unsigned int i; 373 374 if (entity == NULL) 375 entity = list_entry(&dev->entities, struct uvc_entity, list); 376 377 list_for_each_entry_continue(entity, &dev->entities, list) { 378 for (i = 0; i < entity->bNrInPins; ++i) 379 if (entity->baSourceID[i] == id) 380 return entity; 381 } 382 383 return NULL; 384 } 385 386 static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id) 387 { 388 struct uvc_streaming *stream; 389 390 list_for_each_entry(stream, &dev->streams, list) { 391 if (stream->header.bTerminalLink == id) 392 return stream; 393 } 394 395 return NULL; 396 } 397 398 /* ------------------------------------------------------------------------ 399 * Streaming Object Management 400 */ 401 402 static void uvc_stream_delete(struct uvc_streaming *stream) 403 { 404 if (stream->async_wq) 405 destroy_workqueue(stream->async_wq); 406 407 mutex_destroy(&stream->mutex); 408 409 usb_put_intf(stream->intf); 410 411 kfree(stream->format); 412 kfree(stream->header.bmaControls); 413 kfree(stream); 414 } 415 416 static struct uvc_streaming *uvc_stream_new(struct uvc_device *dev, 417 struct usb_interface *intf) 418 { 419 struct uvc_streaming *stream; 420 421 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 422 if (stream == NULL) 423 return NULL; 424 425 mutex_init(&stream->mutex); 426 427 stream->dev = dev; 428 stream->intf = usb_get_intf(intf); 429 stream->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; 430 431 /* Allocate a stream specific work queue for asynchronous tasks. */ 432 stream->async_wq = alloc_workqueue("uvcvideo", WQ_UNBOUND | WQ_HIGHPRI, 433 0); 434 if (!stream->async_wq) { 435 uvc_stream_delete(stream); 436 return NULL; 437 } 438 439 return stream; 440 } 441 442 /* ------------------------------------------------------------------------ 443 * Descriptors parsing 444 */ 445 446 static int uvc_parse_format(struct uvc_device *dev, 447 struct uvc_streaming *streaming, struct uvc_format *format, 448 u32 **intervals, unsigned char *buffer, int buflen) 449 { 450 struct usb_interface *intf = streaming->intf; 451 struct usb_host_interface *alts = intf->cur_altsetting; 452 struct uvc_format_desc *fmtdesc; 453 struct uvc_frame *frame; 454 const unsigned char *start = buffer; 455 unsigned int width_multiplier = 1; 456 unsigned int interval; 457 unsigned int i, n; 458 u8 ftype; 459 460 format->type = buffer[2]; 461 format->index = buffer[3]; 462 463 switch (buffer[2]) { 464 case UVC_VS_FORMAT_UNCOMPRESSED: 465 case UVC_VS_FORMAT_FRAME_BASED: 466 n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28; 467 if (buflen < n) { 468 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 469 "interface %d FORMAT error\n", 470 dev->udev->devnum, 471 alts->desc.bInterfaceNumber); 472 return -EINVAL; 473 } 474 475 /* Find the format descriptor from its GUID. */ 476 fmtdesc = uvc_format_by_guid(&buffer[5]); 477 478 if (fmtdesc != NULL) { 479 strscpy(format->name, fmtdesc->name, 480 sizeof(format->name)); 481 format->fcc = fmtdesc->fcc; 482 } else { 483 uvc_printk(KERN_INFO, "Unknown video format %pUl\n", 484 &buffer[5]); 485 snprintf(format->name, sizeof(format->name), "%pUl\n", 486 &buffer[5]); 487 format->fcc = 0; 488 } 489 490 format->bpp = buffer[21]; 491 492 /* Some devices report a format that doesn't match what they 493 * really send. 494 */ 495 if (dev->quirks & UVC_QUIRK_FORCE_Y8) { 496 if (format->fcc == V4L2_PIX_FMT_YUYV) { 497 strscpy(format->name, "Greyscale 8-bit (Y8 )", 498 sizeof(format->name)); 499 format->fcc = V4L2_PIX_FMT_GREY; 500 format->bpp = 8; 501 width_multiplier = 2; 502 } 503 } 504 505 if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) { 506 ftype = UVC_VS_FRAME_UNCOMPRESSED; 507 } else { 508 ftype = UVC_VS_FRAME_FRAME_BASED; 509 if (buffer[27]) 510 format->flags = UVC_FMT_FLAG_COMPRESSED; 511 } 512 break; 513 514 case UVC_VS_FORMAT_MJPEG: 515 if (buflen < 11) { 516 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 517 "interface %d FORMAT error\n", 518 dev->udev->devnum, 519 alts->desc.bInterfaceNumber); 520 return -EINVAL; 521 } 522 523 strscpy(format->name, "MJPEG", sizeof(format->name)); 524 format->fcc = V4L2_PIX_FMT_MJPEG; 525 format->flags = UVC_FMT_FLAG_COMPRESSED; 526 format->bpp = 0; 527 ftype = UVC_VS_FRAME_MJPEG; 528 break; 529 530 case UVC_VS_FORMAT_DV: 531 if (buflen < 9) { 532 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 533 "interface %d FORMAT error\n", 534 dev->udev->devnum, 535 alts->desc.bInterfaceNumber); 536 return -EINVAL; 537 } 538 539 switch (buffer[8] & 0x7f) { 540 case 0: 541 strscpy(format->name, "SD-DV", sizeof(format->name)); 542 break; 543 case 1: 544 strscpy(format->name, "SDL-DV", sizeof(format->name)); 545 break; 546 case 2: 547 strscpy(format->name, "HD-DV", sizeof(format->name)); 548 break; 549 default: 550 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 551 "interface %d: unknown DV format %u\n", 552 dev->udev->devnum, 553 alts->desc.bInterfaceNumber, buffer[8]); 554 return -EINVAL; 555 } 556 557 strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz", 558 sizeof(format->name)); 559 560 format->fcc = V4L2_PIX_FMT_DV; 561 format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM; 562 format->bpp = 0; 563 ftype = 0; 564 565 /* Create a dummy frame descriptor. */ 566 frame = &format->frame[0]; 567 memset(&format->frame[0], 0, sizeof(format->frame[0])); 568 frame->bFrameIntervalType = 1; 569 frame->dwDefaultFrameInterval = 1; 570 frame->dwFrameInterval = *intervals; 571 *(*intervals)++ = 1; 572 format->nframes = 1; 573 break; 574 575 case UVC_VS_FORMAT_MPEG2TS: 576 case UVC_VS_FORMAT_STREAM_BASED: 577 /* Not supported yet. */ 578 default: 579 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 580 "interface %d unsupported format %u\n", 581 dev->udev->devnum, alts->desc.bInterfaceNumber, 582 buffer[2]); 583 return -EINVAL; 584 } 585 586 uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name); 587 588 buflen -= buffer[0]; 589 buffer += buffer[0]; 590 591 /* Parse the frame descriptors. Only uncompressed, MJPEG and frame 592 * based formats have frame descriptors. 593 */ 594 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE && 595 buffer[2] == ftype) { 596 frame = &format->frame[format->nframes]; 597 if (ftype != UVC_VS_FRAME_FRAME_BASED) 598 n = buflen > 25 ? buffer[25] : 0; 599 else 600 n = buflen > 21 ? buffer[21] : 0; 601 602 n = n ? n : 3; 603 604 if (buflen < 26 + 4*n) { 605 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 606 "interface %d FRAME error\n", dev->udev->devnum, 607 alts->desc.bInterfaceNumber); 608 return -EINVAL; 609 } 610 611 frame->bFrameIndex = buffer[3]; 612 frame->bmCapabilities = buffer[4]; 613 frame->wWidth = get_unaligned_le16(&buffer[5]) 614 * width_multiplier; 615 frame->wHeight = get_unaligned_le16(&buffer[7]); 616 frame->dwMinBitRate = get_unaligned_le32(&buffer[9]); 617 frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]); 618 if (ftype != UVC_VS_FRAME_FRAME_BASED) { 619 frame->dwMaxVideoFrameBufferSize = 620 get_unaligned_le32(&buffer[17]); 621 frame->dwDefaultFrameInterval = 622 get_unaligned_le32(&buffer[21]); 623 frame->bFrameIntervalType = buffer[25]; 624 } else { 625 frame->dwMaxVideoFrameBufferSize = 0; 626 frame->dwDefaultFrameInterval = 627 get_unaligned_le32(&buffer[17]); 628 frame->bFrameIntervalType = buffer[21]; 629 } 630 frame->dwFrameInterval = *intervals; 631 632 /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize 633 * completely. Observed behaviours range from setting the 634 * value to 1.1x the actual frame size to hardwiring the 635 * 16 low bits to 0. This results in a higher than necessary 636 * memory usage as well as a wrong image size information. For 637 * uncompressed formats this can be fixed by computing the 638 * value from the frame size. 639 */ 640 if (!(format->flags & UVC_FMT_FLAG_COMPRESSED)) 641 frame->dwMaxVideoFrameBufferSize = format->bpp 642 * frame->wWidth * frame->wHeight / 8; 643 644 /* Some bogus devices report dwMinFrameInterval equal to 645 * dwMaxFrameInterval and have dwFrameIntervalStep set to 646 * zero. Setting all null intervals to 1 fixes the problem and 647 * some other divisions by zero that could happen. 648 */ 649 for (i = 0; i < n; ++i) { 650 interval = get_unaligned_le32(&buffer[26+4*i]); 651 *(*intervals)++ = interval ? interval : 1; 652 } 653 654 /* Make sure that the default frame interval stays between 655 * the boundaries. 656 */ 657 n -= frame->bFrameIntervalType ? 1 : 2; 658 frame->dwDefaultFrameInterval = 659 min(frame->dwFrameInterval[n], 660 max(frame->dwFrameInterval[0], 661 frame->dwDefaultFrameInterval)); 662 663 if (dev->quirks & UVC_QUIRK_RESTRICT_FRAME_RATE) { 664 frame->bFrameIntervalType = 1; 665 frame->dwFrameInterval[0] = 666 frame->dwDefaultFrameInterval; 667 } 668 669 uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n", 670 frame->wWidth, frame->wHeight, 671 10000000/frame->dwDefaultFrameInterval, 672 (100000000/frame->dwDefaultFrameInterval)%10); 673 674 format->nframes++; 675 buflen -= buffer[0]; 676 buffer += buffer[0]; 677 } 678 679 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE && 680 buffer[2] == UVC_VS_STILL_IMAGE_FRAME) { 681 buflen -= buffer[0]; 682 buffer += buffer[0]; 683 } 684 685 if (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE && 686 buffer[2] == UVC_VS_COLORFORMAT) { 687 if (buflen < 6) { 688 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 689 "interface %d COLORFORMAT error\n", 690 dev->udev->devnum, 691 alts->desc.bInterfaceNumber); 692 return -EINVAL; 693 } 694 695 format->colorspace = uvc_colorspace(buffer[3]); 696 697 buflen -= buffer[0]; 698 buffer += buffer[0]; 699 } 700 701 return buffer - start; 702 } 703 704 static int uvc_parse_streaming(struct uvc_device *dev, 705 struct usb_interface *intf) 706 { 707 struct uvc_streaming *streaming = NULL; 708 struct uvc_format *format; 709 struct uvc_frame *frame; 710 struct usb_host_interface *alts = &intf->altsetting[0]; 711 unsigned char *_buffer, *buffer = alts->extra; 712 int _buflen, buflen = alts->extralen; 713 unsigned int nformats = 0, nframes = 0, nintervals = 0; 714 unsigned int size, i, n, p; 715 u32 *interval; 716 u16 psize; 717 int ret = -EINVAL; 718 719 if (intf->cur_altsetting->desc.bInterfaceSubClass 720 != UVC_SC_VIDEOSTREAMING) { 721 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a " 722 "video streaming interface\n", dev->udev->devnum, 723 intf->altsetting[0].desc.bInterfaceNumber); 724 return -EINVAL; 725 } 726 727 if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) { 728 uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already " 729 "claimed\n", dev->udev->devnum, 730 intf->altsetting[0].desc.bInterfaceNumber); 731 return -EINVAL; 732 } 733 734 streaming = uvc_stream_new(dev, intf); 735 if (streaming == NULL) { 736 usb_driver_release_interface(&uvc_driver.driver, intf); 737 return -ENOMEM; 738 } 739 740 /* The Pico iMage webcam has its class-specific interface descriptors 741 * after the endpoint descriptors. 742 */ 743 if (buflen == 0) { 744 for (i = 0; i < alts->desc.bNumEndpoints; ++i) { 745 struct usb_host_endpoint *ep = &alts->endpoint[i]; 746 747 if (ep->extralen == 0) 748 continue; 749 750 if (ep->extralen > 2 && 751 ep->extra[1] == USB_DT_CS_INTERFACE) { 752 uvc_trace(UVC_TRACE_DESCR, "trying extra data " 753 "from endpoint %u.\n", i); 754 buffer = alts->endpoint[i].extra; 755 buflen = alts->endpoint[i].extralen; 756 break; 757 } 758 } 759 } 760 761 /* Skip the standard interface descriptors. */ 762 while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) { 763 buflen -= buffer[0]; 764 buffer += buffer[0]; 765 } 766 767 if (buflen <= 2) { 768 uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming " 769 "interface descriptors found.\n"); 770 goto error; 771 } 772 773 /* Parse the header descriptor. */ 774 switch (buffer[2]) { 775 case UVC_VS_OUTPUT_HEADER: 776 streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 777 size = 9; 778 break; 779 780 case UVC_VS_INPUT_HEADER: 781 streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 782 size = 13; 783 break; 784 785 default: 786 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " 787 "%d HEADER descriptor not found.\n", dev->udev->devnum, 788 alts->desc.bInterfaceNumber); 789 goto error; 790 } 791 792 p = buflen >= 4 ? buffer[3] : 0; 793 n = buflen >= size ? buffer[size-1] : 0; 794 795 if (buflen < size + p*n) { 796 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 797 "interface %d HEADER descriptor is invalid.\n", 798 dev->udev->devnum, alts->desc.bInterfaceNumber); 799 goto error; 800 } 801 802 streaming->header.bNumFormats = p; 803 streaming->header.bEndpointAddress = buffer[6]; 804 if (buffer[2] == UVC_VS_INPUT_HEADER) { 805 streaming->header.bmInfo = buffer[7]; 806 streaming->header.bTerminalLink = buffer[8]; 807 streaming->header.bStillCaptureMethod = buffer[9]; 808 streaming->header.bTriggerSupport = buffer[10]; 809 streaming->header.bTriggerUsage = buffer[11]; 810 } else { 811 streaming->header.bTerminalLink = buffer[7]; 812 } 813 streaming->header.bControlSize = n; 814 815 streaming->header.bmaControls = kmemdup(&buffer[size], p * n, 816 GFP_KERNEL); 817 if (streaming->header.bmaControls == NULL) { 818 ret = -ENOMEM; 819 goto error; 820 } 821 822 buflen -= buffer[0]; 823 buffer += buffer[0]; 824 825 _buffer = buffer; 826 _buflen = buflen; 827 828 /* Count the format and frame descriptors. */ 829 while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) { 830 switch (_buffer[2]) { 831 case UVC_VS_FORMAT_UNCOMPRESSED: 832 case UVC_VS_FORMAT_MJPEG: 833 case UVC_VS_FORMAT_FRAME_BASED: 834 nformats++; 835 break; 836 837 case UVC_VS_FORMAT_DV: 838 /* DV format has no frame descriptor. We will create a 839 * dummy frame descriptor with a dummy frame interval. 840 */ 841 nformats++; 842 nframes++; 843 nintervals++; 844 break; 845 846 case UVC_VS_FORMAT_MPEG2TS: 847 case UVC_VS_FORMAT_STREAM_BASED: 848 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming " 849 "interface %d FORMAT %u is not supported.\n", 850 dev->udev->devnum, 851 alts->desc.bInterfaceNumber, _buffer[2]); 852 break; 853 854 case UVC_VS_FRAME_UNCOMPRESSED: 855 case UVC_VS_FRAME_MJPEG: 856 nframes++; 857 if (_buflen > 25) 858 nintervals += _buffer[25] ? _buffer[25] : 3; 859 break; 860 861 case UVC_VS_FRAME_FRAME_BASED: 862 nframes++; 863 if (_buflen > 21) 864 nintervals += _buffer[21] ? _buffer[21] : 3; 865 break; 866 } 867 868 _buflen -= _buffer[0]; 869 _buffer += _buffer[0]; 870 } 871 872 if (nformats == 0) { 873 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " 874 "%d has no supported formats defined.\n", 875 dev->udev->devnum, alts->desc.bInterfaceNumber); 876 goto error; 877 } 878 879 size = nformats * sizeof(*format) + nframes * sizeof(*frame) 880 + nintervals * sizeof(*interval); 881 format = kzalloc(size, GFP_KERNEL); 882 if (format == NULL) { 883 ret = -ENOMEM; 884 goto error; 885 } 886 887 frame = (struct uvc_frame *)&format[nformats]; 888 interval = (u32 *)&frame[nframes]; 889 890 streaming->format = format; 891 streaming->nformats = nformats; 892 893 /* Parse the format descriptors. */ 894 while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) { 895 switch (buffer[2]) { 896 case UVC_VS_FORMAT_UNCOMPRESSED: 897 case UVC_VS_FORMAT_MJPEG: 898 case UVC_VS_FORMAT_DV: 899 case UVC_VS_FORMAT_FRAME_BASED: 900 format->frame = frame; 901 ret = uvc_parse_format(dev, streaming, format, 902 &interval, buffer, buflen); 903 if (ret < 0) 904 goto error; 905 906 frame += format->nframes; 907 format++; 908 909 buflen -= ret; 910 buffer += ret; 911 continue; 912 913 default: 914 break; 915 } 916 917 buflen -= buffer[0]; 918 buffer += buffer[0]; 919 } 920 921 if (buflen) 922 uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface " 923 "%d has %u bytes of trailing descriptor garbage.\n", 924 dev->udev->devnum, alts->desc.bInterfaceNumber, buflen); 925 926 /* Parse the alternate settings to find the maximum bandwidth. */ 927 for (i = 0; i < intf->num_altsetting; ++i) { 928 struct usb_host_endpoint *ep; 929 alts = &intf->altsetting[i]; 930 ep = uvc_find_endpoint(alts, 931 streaming->header.bEndpointAddress); 932 if (ep == NULL) 933 continue; 934 935 psize = le16_to_cpu(ep->desc.wMaxPacketSize); 936 psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); 937 if (psize > streaming->maxpsize) 938 streaming->maxpsize = psize; 939 } 940 941 list_add_tail(&streaming->list, &dev->streams); 942 return 0; 943 944 error: 945 usb_driver_release_interface(&uvc_driver.driver, intf); 946 uvc_stream_delete(streaming); 947 return ret; 948 } 949 950 static struct uvc_entity *uvc_alloc_entity(u16 type, u8 id, 951 unsigned int num_pads, unsigned int extra_size) 952 { 953 struct uvc_entity *entity; 954 unsigned int num_inputs; 955 unsigned int size; 956 unsigned int i; 957 958 extra_size = roundup(extra_size, sizeof(*entity->pads)); 959 num_inputs = (type & UVC_TERM_OUTPUT) ? num_pads : num_pads - 1; 960 size = sizeof(*entity) + extra_size + sizeof(*entity->pads) * num_pads 961 + num_inputs; 962 entity = kzalloc(size, GFP_KERNEL); 963 if (entity == NULL) 964 return NULL; 965 966 entity->id = id; 967 entity->type = type; 968 969 entity->num_links = 0; 970 entity->num_pads = num_pads; 971 entity->pads = ((void *)(entity + 1)) + extra_size; 972 973 for (i = 0; i < num_inputs; ++i) 974 entity->pads[i].flags = MEDIA_PAD_FL_SINK; 975 if (!UVC_ENTITY_IS_OTERM(entity)) 976 entity->pads[num_pads-1].flags = MEDIA_PAD_FL_SOURCE; 977 978 entity->bNrInPins = num_inputs; 979 entity->baSourceID = (u8 *)(&entity->pads[num_pads]); 980 981 return entity; 982 } 983 984 /* Parse vendor-specific extensions. */ 985 static int uvc_parse_vendor_control(struct uvc_device *dev, 986 const unsigned char *buffer, int buflen) 987 { 988 struct usb_device *udev = dev->udev; 989 struct usb_host_interface *alts = dev->intf->cur_altsetting; 990 struct uvc_entity *unit; 991 unsigned int n, p; 992 int handled = 0; 993 994 switch (le16_to_cpu(dev->udev->descriptor.idVendor)) { 995 case 0x046d: /* Logitech */ 996 if (buffer[1] != 0x41 || buffer[2] != 0x01) 997 break; 998 999 /* Logitech implements several vendor specific functions 1000 * through vendor specific extension units (LXU). 1001 * 1002 * The LXU descriptors are similar to XU descriptors 1003 * (see "USB Device Video Class for Video Devices", section 1004 * 3.7.2.6 "Extension Unit Descriptor") with the following 1005 * differences: 1006 * 1007 * ---------------------------------------------------------- 1008 * 0 bLength 1 Number 1009 * Size of this descriptor, in bytes: 24+p+n*2 1010 * ---------------------------------------------------------- 1011 * 23+p+n bmControlsType N Bitmap 1012 * Individual bits in the set are defined: 1013 * 0: Absolute 1014 * 1: Relative 1015 * 1016 * This bitset is mapped exactly the same as bmControls. 1017 * ---------------------------------------------------------- 1018 * 23+p+n*2 bReserved 1 Boolean 1019 * ---------------------------------------------------------- 1020 * 24+p+n*2 iExtension 1 Index 1021 * Index of a string descriptor that describes this 1022 * extension unit. 1023 * ---------------------------------------------------------- 1024 */ 1025 p = buflen >= 22 ? buffer[21] : 0; 1026 n = buflen >= 25 + p ? buffer[22+p] : 0; 1027 1028 if (buflen < 25 + p + 2*n) { 1029 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1030 "interface %d EXTENSION_UNIT error\n", 1031 udev->devnum, alts->desc.bInterfaceNumber); 1032 break; 1033 } 1034 1035 unit = uvc_alloc_entity(UVC_VC_EXTENSION_UNIT, buffer[3], 1036 p + 1, 2*n); 1037 if (unit == NULL) 1038 return -ENOMEM; 1039 1040 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); 1041 unit->extension.bNumControls = buffer[20]; 1042 memcpy(unit->baSourceID, &buffer[22], p); 1043 unit->extension.bControlSize = buffer[22+p]; 1044 unit->extension.bmControls = (u8 *)unit + sizeof(*unit); 1045 unit->extension.bmControlsType = (u8 *)unit + sizeof(*unit) 1046 + n; 1047 memcpy(unit->extension.bmControls, &buffer[23+p], 2*n); 1048 1049 if (buffer[24+p+2*n] != 0) 1050 usb_string(udev, buffer[24+p+2*n], unit->name, 1051 sizeof(unit->name)); 1052 else 1053 sprintf(unit->name, "Extension %u", buffer[3]); 1054 1055 list_add_tail(&unit->list, &dev->entities); 1056 handled = 1; 1057 break; 1058 } 1059 1060 return handled; 1061 } 1062 1063 static int uvc_parse_standard_control(struct uvc_device *dev, 1064 const unsigned char *buffer, int buflen) 1065 { 1066 struct usb_device *udev = dev->udev; 1067 struct uvc_entity *unit, *term; 1068 struct usb_interface *intf; 1069 struct usb_host_interface *alts = dev->intf->cur_altsetting; 1070 unsigned int i, n, p, len; 1071 u16 type; 1072 1073 switch (buffer[2]) { 1074 case UVC_VC_HEADER: 1075 n = buflen >= 12 ? buffer[11] : 0; 1076 1077 if (buflen < 12 + n) { 1078 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1079 "interface %d HEADER error\n", udev->devnum, 1080 alts->desc.bInterfaceNumber); 1081 return -EINVAL; 1082 } 1083 1084 dev->uvc_version = get_unaligned_le16(&buffer[3]); 1085 dev->clock_frequency = get_unaligned_le32(&buffer[7]); 1086 1087 /* Parse all USB Video Streaming interfaces. */ 1088 for (i = 0; i < n; ++i) { 1089 intf = usb_ifnum_to_if(udev, buffer[12+i]); 1090 if (intf == NULL) { 1091 uvc_trace(UVC_TRACE_DESCR, "device %d " 1092 "interface %d doesn't exists\n", 1093 udev->devnum, i); 1094 continue; 1095 } 1096 1097 uvc_parse_streaming(dev, intf); 1098 } 1099 break; 1100 1101 case UVC_VC_INPUT_TERMINAL: 1102 if (buflen < 8) { 1103 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1104 "interface %d INPUT_TERMINAL error\n", 1105 udev->devnum, alts->desc.bInterfaceNumber); 1106 return -EINVAL; 1107 } 1108 1109 /* 1110 * Reject invalid terminal types that would cause issues: 1111 * 1112 * - The high byte must be non-zero, otherwise it would be 1113 * confused with a unit. 1114 * 1115 * - Bit 15 must be 0, as we use it internally as a terminal 1116 * direction flag. 1117 * 1118 * Other unknown types are accepted. 1119 */ 1120 type = get_unaligned_le16(&buffer[4]); 1121 if ((type & 0x7f00) == 0 || (type & 0x8000) != 0) { 1122 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1123 "interface %d INPUT_TERMINAL %d has invalid " 1124 "type 0x%04x, skipping\n", udev->devnum, 1125 alts->desc.bInterfaceNumber, 1126 buffer[3], type); 1127 return 0; 1128 } 1129 1130 n = 0; 1131 p = 0; 1132 len = 8; 1133 1134 if (type == UVC_ITT_CAMERA) { 1135 n = buflen >= 15 ? buffer[14] : 0; 1136 len = 15; 1137 1138 } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) { 1139 n = buflen >= 9 ? buffer[8] : 0; 1140 p = buflen >= 10 + n ? buffer[9+n] : 0; 1141 len = 10; 1142 } 1143 1144 if (buflen < len + n + p) { 1145 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1146 "interface %d INPUT_TERMINAL error\n", 1147 udev->devnum, alts->desc.bInterfaceNumber); 1148 return -EINVAL; 1149 } 1150 1151 term = uvc_alloc_entity(type | UVC_TERM_INPUT, buffer[3], 1152 1, n + p); 1153 if (term == NULL) 1154 return -ENOMEM; 1155 1156 if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) { 1157 term->camera.bControlSize = n; 1158 term->camera.bmControls = (u8 *)term + sizeof(*term); 1159 term->camera.wObjectiveFocalLengthMin = 1160 get_unaligned_le16(&buffer[8]); 1161 term->camera.wObjectiveFocalLengthMax = 1162 get_unaligned_le16(&buffer[10]); 1163 term->camera.wOcularFocalLength = 1164 get_unaligned_le16(&buffer[12]); 1165 memcpy(term->camera.bmControls, &buffer[15], n); 1166 } else if (UVC_ENTITY_TYPE(term) == 1167 UVC_ITT_MEDIA_TRANSPORT_INPUT) { 1168 term->media.bControlSize = n; 1169 term->media.bmControls = (u8 *)term + sizeof(*term); 1170 term->media.bTransportModeSize = p; 1171 term->media.bmTransportModes = (u8 *)term 1172 + sizeof(*term) + n; 1173 memcpy(term->media.bmControls, &buffer[9], n); 1174 memcpy(term->media.bmTransportModes, &buffer[10+n], p); 1175 } 1176 1177 if (buffer[7] != 0) 1178 usb_string(udev, buffer[7], term->name, 1179 sizeof(term->name)); 1180 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) 1181 sprintf(term->name, "Camera %u", buffer[3]); 1182 else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT) 1183 sprintf(term->name, "Media %u", buffer[3]); 1184 else 1185 sprintf(term->name, "Input %u", buffer[3]); 1186 1187 list_add_tail(&term->list, &dev->entities); 1188 break; 1189 1190 case UVC_VC_OUTPUT_TERMINAL: 1191 if (buflen < 9) { 1192 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1193 "interface %d OUTPUT_TERMINAL error\n", 1194 udev->devnum, alts->desc.bInterfaceNumber); 1195 return -EINVAL; 1196 } 1197 1198 /* Make sure the terminal type MSB is not null, otherwise it 1199 * could be confused with a unit. 1200 */ 1201 type = get_unaligned_le16(&buffer[4]); 1202 if ((type & 0xff00) == 0) { 1203 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1204 "interface %d OUTPUT_TERMINAL %d has invalid " 1205 "type 0x%04x, skipping\n", udev->devnum, 1206 alts->desc.bInterfaceNumber, buffer[3], type); 1207 return 0; 1208 } 1209 1210 term = uvc_alloc_entity(type | UVC_TERM_OUTPUT, buffer[3], 1211 1, 0); 1212 if (term == NULL) 1213 return -ENOMEM; 1214 1215 memcpy(term->baSourceID, &buffer[7], 1); 1216 1217 if (buffer[8] != 0) 1218 usb_string(udev, buffer[8], term->name, 1219 sizeof(term->name)); 1220 else 1221 sprintf(term->name, "Output %u", buffer[3]); 1222 1223 list_add_tail(&term->list, &dev->entities); 1224 break; 1225 1226 case UVC_VC_SELECTOR_UNIT: 1227 p = buflen >= 5 ? buffer[4] : 0; 1228 1229 if (buflen < 5 || buflen < 6 + p) { 1230 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1231 "interface %d SELECTOR_UNIT error\n", 1232 udev->devnum, alts->desc.bInterfaceNumber); 1233 return -EINVAL; 1234 } 1235 1236 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, 0); 1237 if (unit == NULL) 1238 return -ENOMEM; 1239 1240 memcpy(unit->baSourceID, &buffer[5], p); 1241 1242 if (buffer[5+p] != 0) 1243 usb_string(udev, buffer[5+p], unit->name, 1244 sizeof(unit->name)); 1245 else 1246 sprintf(unit->name, "Selector %u", buffer[3]); 1247 1248 list_add_tail(&unit->list, &dev->entities); 1249 break; 1250 1251 case UVC_VC_PROCESSING_UNIT: 1252 n = buflen >= 8 ? buffer[7] : 0; 1253 p = dev->uvc_version >= 0x0110 ? 10 : 9; 1254 1255 if (buflen < p + n) { 1256 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1257 "interface %d PROCESSING_UNIT error\n", 1258 udev->devnum, alts->desc.bInterfaceNumber); 1259 return -EINVAL; 1260 } 1261 1262 unit = uvc_alloc_entity(buffer[2], buffer[3], 2, n); 1263 if (unit == NULL) 1264 return -ENOMEM; 1265 1266 memcpy(unit->baSourceID, &buffer[4], 1); 1267 unit->processing.wMaxMultiplier = 1268 get_unaligned_le16(&buffer[5]); 1269 unit->processing.bControlSize = buffer[7]; 1270 unit->processing.bmControls = (u8 *)unit + sizeof(*unit); 1271 memcpy(unit->processing.bmControls, &buffer[8], n); 1272 if (dev->uvc_version >= 0x0110) 1273 unit->processing.bmVideoStandards = buffer[9+n]; 1274 1275 if (buffer[8+n] != 0) 1276 usb_string(udev, buffer[8+n], unit->name, 1277 sizeof(unit->name)); 1278 else 1279 sprintf(unit->name, "Processing %u", buffer[3]); 1280 1281 list_add_tail(&unit->list, &dev->entities); 1282 break; 1283 1284 case UVC_VC_EXTENSION_UNIT: 1285 p = buflen >= 22 ? buffer[21] : 0; 1286 n = buflen >= 24 + p ? buffer[22+p] : 0; 1287 1288 if (buflen < 24 + p + n) { 1289 uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol " 1290 "interface %d EXTENSION_UNIT error\n", 1291 udev->devnum, alts->desc.bInterfaceNumber); 1292 return -EINVAL; 1293 } 1294 1295 unit = uvc_alloc_entity(buffer[2], buffer[3], p + 1, n); 1296 if (unit == NULL) 1297 return -ENOMEM; 1298 1299 memcpy(unit->extension.guidExtensionCode, &buffer[4], 16); 1300 unit->extension.bNumControls = buffer[20]; 1301 memcpy(unit->baSourceID, &buffer[22], p); 1302 unit->extension.bControlSize = buffer[22+p]; 1303 unit->extension.bmControls = (u8 *)unit + sizeof(*unit); 1304 memcpy(unit->extension.bmControls, &buffer[23+p], n); 1305 1306 if (buffer[23+p+n] != 0) 1307 usb_string(udev, buffer[23+p+n], unit->name, 1308 sizeof(unit->name)); 1309 else 1310 sprintf(unit->name, "Extension %u", buffer[3]); 1311 1312 list_add_tail(&unit->list, &dev->entities); 1313 break; 1314 1315 default: 1316 uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE " 1317 "descriptor (%u)\n", buffer[2]); 1318 break; 1319 } 1320 1321 return 0; 1322 } 1323 1324 static int uvc_parse_control(struct uvc_device *dev) 1325 { 1326 struct usb_host_interface *alts = dev->intf->cur_altsetting; 1327 unsigned char *buffer = alts->extra; 1328 int buflen = alts->extralen; 1329 int ret; 1330 1331 /* Parse the default alternate setting only, as the UVC specification 1332 * defines a single alternate setting, the default alternate setting 1333 * zero. 1334 */ 1335 1336 while (buflen > 2) { 1337 if (uvc_parse_vendor_control(dev, buffer, buflen) || 1338 buffer[1] != USB_DT_CS_INTERFACE) 1339 goto next_descriptor; 1340 1341 if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0) 1342 return ret; 1343 1344 next_descriptor: 1345 buflen -= buffer[0]; 1346 buffer += buffer[0]; 1347 } 1348 1349 /* Check if the optional status endpoint is present. Built-in iSight 1350 * webcams have an interrupt endpoint but spit proprietary data that 1351 * don't conform to the UVC status endpoint messages. Don't try to 1352 * handle the interrupt endpoint for those cameras. 1353 */ 1354 if (alts->desc.bNumEndpoints == 1 && 1355 !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) { 1356 struct usb_host_endpoint *ep = &alts->endpoint[0]; 1357 struct usb_endpoint_descriptor *desc = &ep->desc; 1358 1359 if (usb_endpoint_is_int_in(desc) && 1360 le16_to_cpu(desc->wMaxPacketSize) >= 8 && 1361 desc->bInterval != 0) { 1362 uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint " 1363 "(addr %02x).\n", desc->bEndpointAddress); 1364 dev->int_ep = ep; 1365 } 1366 } 1367 1368 return 0; 1369 } 1370 1371 /* ------------------------------------------------------------------------ 1372 * UVC device scan 1373 */ 1374 1375 /* 1376 * Scan the UVC descriptors to locate a chain starting at an Output Terminal 1377 * and containing the following units: 1378 * 1379 * - one or more Output Terminals (USB Streaming or Display) 1380 * - zero or one Processing Unit 1381 * - zero, one or more single-input Selector Units 1382 * - zero or one multiple-input Selector Units, provided all inputs are 1383 * connected to input terminals 1384 * - zero, one or mode single-input Extension Units 1385 * - one or more Input Terminals (Camera, External or USB Streaming) 1386 * 1387 * The terminal and units must match on of the following structures: 1388 * 1389 * ITT_*(0) -> +---------+ +---------+ +---------+ -> TT_STREAMING(0) 1390 * ... | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} | ... 1391 * ITT_*(n) -> +---------+ +---------+ +---------+ -> TT_STREAMING(n) 1392 * 1393 * +---------+ +---------+ -> OTT_*(0) 1394 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} | ... 1395 * +---------+ +---------+ -> OTT_*(n) 1396 * 1397 * The Processing Unit and Extension Units can be in any order. Additional 1398 * Extension Units connected to the main chain as single-unit branches are 1399 * also supported. Single-input Selector Units are ignored. 1400 */ 1401 static int uvc_scan_chain_entity(struct uvc_video_chain *chain, 1402 struct uvc_entity *entity) 1403 { 1404 switch (UVC_ENTITY_TYPE(entity)) { 1405 case UVC_VC_EXTENSION_UNIT: 1406 if (uvc_trace_param & UVC_TRACE_PROBE) 1407 printk(KERN_CONT " <- XU %d", entity->id); 1408 1409 if (entity->bNrInPins != 1) { 1410 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more " 1411 "than 1 input pin.\n", entity->id); 1412 return -1; 1413 } 1414 1415 break; 1416 1417 case UVC_VC_PROCESSING_UNIT: 1418 if (uvc_trace_param & UVC_TRACE_PROBE) 1419 printk(KERN_CONT " <- PU %d", entity->id); 1420 1421 if (chain->processing != NULL) { 1422 uvc_trace(UVC_TRACE_DESCR, "Found multiple " 1423 "Processing Units in chain.\n"); 1424 return -1; 1425 } 1426 1427 chain->processing = entity; 1428 break; 1429 1430 case UVC_VC_SELECTOR_UNIT: 1431 if (uvc_trace_param & UVC_TRACE_PROBE) 1432 printk(KERN_CONT " <- SU %d", entity->id); 1433 1434 /* Single-input selector units are ignored. */ 1435 if (entity->bNrInPins == 1) 1436 break; 1437 1438 if (chain->selector != NULL) { 1439 uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector " 1440 "Units in chain.\n"); 1441 return -1; 1442 } 1443 1444 chain->selector = entity; 1445 break; 1446 1447 case UVC_ITT_VENDOR_SPECIFIC: 1448 case UVC_ITT_CAMERA: 1449 case UVC_ITT_MEDIA_TRANSPORT_INPUT: 1450 if (uvc_trace_param & UVC_TRACE_PROBE) 1451 printk(KERN_CONT " <- IT %d\n", entity->id); 1452 1453 break; 1454 1455 case UVC_OTT_VENDOR_SPECIFIC: 1456 case UVC_OTT_DISPLAY: 1457 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT: 1458 if (uvc_trace_param & UVC_TRACE_PROBE) 1459 printk(KERN_CONT " OT %d", entity->id); 1460 1461 break; 1462 1463 case UVC_TT_STREAMING: 1464 if (UVC_ENTITY_IS_ITERM(entity)) { 1465 if (uvc_trace_param & UVC_TRACE_PROBE) 1466 printk(KERN_CONT " <- IT %d\n", entity->id); 1467 } else { 1468 if (uvc_trace_param & UVC_TRACE_PROBE) 1469 printk(KERN_CONT " OT %d", entity->id); 1470 } 1471 1472 break; 1473 1474 default: 1475 uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type " 1476 "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity)); 1477 return -1; 1478 } 1479 1480 list_add_tail(&entity->chain, &chain->entities); 1481 return 0; 1482 } 1483 1484 static int uvc_scan_chain_forward(struct uvc_video_chain *chain, 1485 struct uvc_entity *entity, struct uvc_entity *prev) 1486 { 1487 struct uvc_entity *forward; 1488 int found; 1489 1490 /* Forward scan */ 1491 forward = NULL; 1492 found = 0; 1493 1494 while (1) { 1495 forward = uvc_entity_by_reference(chain->dev, entity->id, 1496 forward); 1497 if (forward == NULL) 1498 break; 1499 if (forward == prev) 1500 continue; 1501 1502 switch (UVC_ENTITY_TYPE(forward)) { 1503 case UVC_VC_EXTENSION_UNIT: 1504 if (forward->bNrInPins != 1) { 1505 uvc_trace(UVC_TRACE_DESCR, "Extension unit %d " 1506 "has more than 1 input pin.\n", 1507 entity->id); 1508 return -EINVAL; 1509 } 1510 1511 list_add_tail(&forward->chain, &chain->entities); 1512 if (uvc_trace_param & UVC_TRACE_PROBE) { 1513 if (!found) 1514 printk(KERN_CONT " (->"); 1515 1516 printk(KERN_CONT " XU %d", forward->id); 1517 found = 1; 1518 } 1519 break; 1520 1521 case UVC_OTT_VENDOR_SPECIFIC: 1522 case UVC_OTT_DISPLAY: 1523 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT: 1524 case UVC_TT_STREAMING: 1525 if (UVC_ENTITY_IS_ITERM(forward)) { 1526 uvc_trace(UVC_TRACE_DESCR, "Unsupported input " 1527 "terminal %u.\n", forward->id); 1528 return -EINVAL; 1529 } 1530 1531 list_add_tail(&forward->chain, &chain->entities); 1532 if (uvc_trace_param & UVC_TRACE_PROBE) { 1533 if (!found) 1534 printk(KERN_CONT " (->"); 1535 1536 printk(KERN_CONT " OT %d", forward->id); 1537 found = 1; 1538 } 1539 break; 1540 } 1541 } 1542 if (found) 1543 printk(KERN_CONT ")"); 1544 1545 return 0; 1546 } 1547 1548 static int uvc_scan_chain_backward(struct uvc_video_chain *chain, 1549 struct uvc_entity **_entity) 1550 { 1551 struct uvc_entity *entity = *_entity; 1552 struct uvc_entity *term; 1553 int id = -EINVAL, i; 1554 1555 switch (UVC_ENTITY_TYPE(entity)) { 1556 case UVC_VC_EXTENSION_UNIT: 1557 case UVC_VC_PROCESSING_UNIT: 1558 id = entity->baSourceID[0]; 1559 break; 1560 1561 case UVC_VC_SELECTOR_UNIT: 1562 /* Single-input selector units are ignored. */ 1563 if (entity->bNrInPins == 1) { 1564 id = entity->baSourceID[0]; 1565 break; 1566 } 1567 1568 if (uvc_trace_param & UVC_TRACE_PROBE) 1569 printk(KERN_CONT " <- IT"); 1570 1571 chain->selector = entity; 1572 for (i = 0; i < entity->bNrInPins; ++i) { 1573 id = entity->baSourceID[i]; 1574 term = uvc_entity_by_id(chain->dev, id); 1575 if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) { 1576 uvc_trace(UVC_TRACE_DESCR, "Selector unit %d " 1577 "input %d isn't connected to an " 1578 "input terminal\n", entity->id, i); 1579 return -1; 1580 } 1581 1582 if (uvc_trace_param & UVC_TRACE_PROBE) 1583 printk(KERN_CONT " %d", term->id); 1584 1585 list_add_tail(&term->chain, &chain->entities); 1586 uvc_scan_chain_forward(chain, term, entity); 1587 } 1588 1589 if (uvc_trace_param & UVC_TRACE_PROBE) 1590 printk(KERN_CONT "\n"); 1591 1592 id = 0; 1593 break; 1594 1595 case UVC_ITT_VENDOR_SPECIFIC: 1596 case UVC_ITT_CAMERA: 1597 case UVC_ITT_MEDIA_TRANSPORT_INPUT: 1598 case UVC_OTT_VENDOR_SPECIFIC: 1599 case UVC_OTT_DISPLAY: 1600 case UVC_OTT_MEDIA_TRANSPORT_OUTPUT: 1601 case UVC_TT_STREAMING: 1602 id = UVC_ENTITY_IS_OTERM(entity) ? entity->baSourceID[0] : 0; 1603 break; 1604 } 1605 1606 if (id <= 0) { 1607 *_entity = NULL; 1608 return id; 1609 } 1610 1611 entity = uvc_entity_by_id(chain->dev, id); 1612 if (entity == NULL) { 1613 uvc_trace(UVC_TRACE_DESCR, "Found reference to " 1614 "unknown entity %d.\n", id); 1615 return -EINVAL; 1616 } 1617 1618 *_entity = entity; 1619 return 0; 1620 } 1621 1622 static int uvc_scan_chain(struct uvc_video_chain *chain, 1623 struct uvc_entity *term) 1624 { 1625 struct uvc_entity *entity, *prev; 1626 1627 uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain:"); 1628 1629 entity = term; 1630 prev = NULL; 1631 1632 while (entity != NULL) { 1633 /* Entity must not be part of an existing chain */ 1634 if (entity->chain.next || entity->chain.prev) { 1635 uvc_trace(UVC_TRACE_DESCR, "Found reference to " 1636 "entity %d already in chain.\n", entity->id); 1637 return -EINVAL; 1638 } 1639 1640 /* Process entity */ 1641 if (uvc_scan_chain_entity(chain, entity) < 0) 1642 return -EINVAL; 1643 1644 /* Forward scan */ 1645 if (uvc_scan_chain_forward(chain, entity, prev) < 0) 1646 return -EINVAL; 1647 1648 /* Backward scan */ 1649 prev = entity; 1650 if (uvc_scan_chain_backward(chain, &entity) < 0) 1651 return -EINVAL; 1652 } 1653 1654 return 0; 1655 } 1656 1657 static unsigned int uvc_print_terms(struct list_head *terms, u16 dir, 1658 char *buffer) 1659 { 1660 struct uvc_entity *term; 1661 unsigned int nterms = 0; 1662 char *p = buffer; 1663 1664 list_for_each_entry(term, terms, chain) { 1665 if (!UVC_ENTITY_IS_TERM(term) || 1666 UVC_TERM_DIRECTION(term) != dir) 1667 continue; 1668 1669 if (nterms) 1670 p += sprintf(p, ","); 1671 if (++nterms >= 4) { 1672 p += sprintf(p, "..."); 1673 break; 1674 } 1675 p += sprintf(p, "%u", term->id); 1676 } 1677 1678 return p - buffer; 1679 } 1680 1681 static const char *uvc_print_chain(struct uvc_video_chain *chain) 1682 { 1683 static char buffer[43]; 1684 char *p = buffer; 1685 1686 p += uvc_print_terms(&chain->entities, UVC_TERM_INPUT, p); 1687 p += sprintf(p, " -> "); 1688 uvc_print_terms(&chain->entities, UVC_TERM_OUTPUT, p); 1689 1690 return buffer; 1691 } 1692 1693 static struct uvc_video_chain *uvc_alloc_chain(struct uvc_device *dev) 1694 { 1695 struct uvc_video_chain *chain; 1696 1697 chain = kzalloc(sizeof(*chain), GFP_KERNEL); 1698 if (chain == NULL) 1699 return NULL; 1700 1701 INIT_LIST_HEAD(&chain->entities); 1702 mutex_init(&chain->ctrl_mutex); 1703 chain->dev = dev; 1704 v4l2_prio_init(&chain->prio); 1705 1706 return chain; 1707 } 1708 1709 /* 1710 * Fallback heuristic for devices that don't connect units and terminals in a 1711 * valid chain. 1712 * 1713 * Some devices have invalid baSourceID references, causing uvc_scan_chain() 1714 * to fail, but if we just take the entities we can find and put them together 1715 * in the most sensible chain we can think of, turns out they do work anyway. 1716 * Note: This heuristic assumes there is a single chain. 1717 * 1718 * At the time of writing, devices known to have such a broken chain are 1719 * - Acer Integrated Camera (5986:055a) 1720 * - Realtek rtl157a7 (0bda:57a7) 1721 */ 1722 static int uvc_scan_fallback(struct uvc_device *dev) 1723 { 1724 struct uvc_video_chain *chain; 1725 struct uvc_entity *iterm = NULL; 1726 struct uvc_entity *oterm = NULL; 1727 struct uvc_entity *entity; 1728 struct uvc_entity *prev; 1729 1730 /* 1731 * Start by locating the input and output terminals. We only support 1732 * devices with exactly one of each for now. 1733 */ 1734 list_for_each_entry(entity, &dev->entities, list) { 1735 if (UVC_ENTITY_IS_ITERM(entity)) { 1736 if (iterm) 1737 return -EINVAL; 1738 iterm = entity; 1739 } 1740 1741 if (UVC_ENTITY_IS_OTERM(entity)) { 1742 if (oterm) 1743 return -EINVAL; 1744 oterm = entity; 1745 } 1746 } 1747 1748 if (iterm == NULL || oterm == NULL) 1749 return -EINVAL; 1750 1751 /* Allocate the chain and fill it. */ 1752 chain = uvc_alloc_chain(dev); 1753 if (chain == NULL) 1754 return -ENOMEM; 1755 1756 if (uvc_scan_chain_entity(chain, oterm) < 0) 1757 goto error; 1758 1759 prev = oterm; 1760 1761 /* 1762 * Add all Processing and Extension Units with two pads. The order 1763 * doesn't matter much, use reverse list traversal to connect units in 1764 * UVC descriptor order as we build the chain from output to input. This 1765 * leads to units appearing in the order meant by the manufacturer for 1766 * the cameras known to require this heuristic. 1767 */ 1768 list_for_each_entry_reverse(entity, &dev->entities, list) { 1769 if (entity->type != UVC_VC_PROCESSING_UNIT && 1770 entity->type != UVC_VC_EXTENSION_UNIT) 1771 continue; 1772 1773 if (entity->num_pads != 2) 1774 continue; 1775 1776 if (uvc_scan_chain_entity(chain, entity) < 0) 1777 goto error; 1778 1779 prev->baSourceID[0] = entity->id; 1780 prev = entity; 1781 } 1782 1783 if (uvc_scan_chain_entity(chain, iterm) < 0) 1784 goto error; 1785 1786 prev->baSourceID[0] = iterm->id; 1787 1788 list_add_tail(&chain->list, &dev->chains); 1789 1790 uvc_trace(UVC_TRACE_PROBE, 1791 "Found a video chain by fallback heuristic (%s).\n", 1792 uvc_print_chain(chain)); 1793 1794 return 0; 1795 1796 error: 1797 kfree(chain); 1798 return -EINVAL; 1799 } 1800 1801 /* 1802 * Scan the device for video chains and register video devices. 1803 * 1804 * Chains are scanned starting at their output terminals and walked backwards. 1805 */ 1806 static int uvc_scan_device(struct uvc_device *dev) 1807 { 1808 struct uvc_video_chain *chain; 1809 struct uvc_entity *term; 1810 1811 list_for_each_entry(term, &dev->entities, list) { 1812 if (!UVC_ENTITY_IS_OTERM(term)) 1813 continue; 1814 1815 /* If the terminal is already included in a chain, skip it. 1816 * This can happen for chains that have multiple output 1817 * terminals, where all output terminals beside the first one 1818 * will be inserted in the chain in forward scans. 1819 */ 1820 if (term->chain.next || term->chain.prev) 1821 continue; 1822 1823 chain = uvc_alloc_chain(dev); 1824 if (chain == NULL) 1825 return -ENOMEM; 1826 1827 term->flags |= UVC_ENTITY_FLAG_DEFAULT; 1828 1829 if (uvc_scan_chain(chain, term) < 0) { 1830 kfree(chain); 1831 continue; 1832 } 1833 1834 uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n", 1835 uvc_print_chain(chain)); 1836 1837 list_add_tail(&chain->list, &dev->chains); 1838 } 1839 1840 if (list_empty(&dev->chains)) 1841 uvc_scan_fallback(dev); 1842 1843 if (list_empty(&dev->chains)) { 1844 uvc_printk(KERN_INFO, "No valid video chain found.\n"); 1845 return -1; 1846 } 1847 1848 return 0; 1849 } 1850 1851 /* ------------------------------------------------------------------------ 1852 * Video device registration and unregistration 1853 */ 1854 1855 /* 1856 * Delete the UVC device. 1857 * 1858 * Called by the kernel when the last reference to the uvc_device structure 1859 * is released. 1860 * 1861 * As this function is called after or during disconnect(), all URBs have 1862 * already been cancelled by the USB core. There is no need to kill the 1863 * interrupt URB manually. 1864 */ 1865 static void uvc_delete(struct kref *kref) 1866 { 1867 struct uvc_device *dev = container_of(kref, struct uvc_device, ref); 1868 struct list_head *p, *n; 1869 1870 uvc_status_cleanup(dev); 1871 uvc_ctrl_cleanup_device(dev); 1872 1873 usb_put_intf(dev->intf); 1874 usb_put_dev(dev->udev); 1875 1876 #ifdef CONFIG_MEDIA_CONTROLLER 1877 media_device_cleanup(&dev->mdev); 1878 #endif 1879 1880 list_for_each_safe(p, n, &dev->chains) { 1881 struct uvc_video_chain *chain; 1882 chain = list_entry(p, struct uvc_video_chain, list); 1883 kfree(chain); 1884 } 1885 1886 list_for_each_safe(p, n, &dev->entities) { 1887 struct uvc_entity *entity; 1888 entity = list_entry(p, struct uvc_entity, list); 1889 #ifdef CONFIG_MEDIA_CONTROLLER 1890 uvc_mc_cleanup_entity(entity); 1891 #endif 1892 kfree(entity); 1893 } 1894 1895 list_for_each_safe(p, n, &dev->streams) { 1896 struct uvc_streaming *streaming; 1897 streaming = list_entry(p, struct uvc_streaming, list); 1898 usb_driver_release_interface(&uvc_driver.driver, 1899 streaming->intf); 1900 uvc_stream_delete(streaming); 1901 } 1902 1903 kfree(dev); 1904 } 1905 1906 static void uvc_release(struct video_device *vdev) 1907 { 1908 struct uvc_streaming *stream = video_get_drvdata(vdev); 1909 struct uvc_device *dev = stream->dev; 1910 1911 kref_put(&dev->ref, uvc_delete); 1912 } 1913 1914 /* 1915 * Unregister the video devices. 1916 */ 1917 static void uvc_unregister_video(struct uvc_device *dev) 1918 { 1919 struct uvc_streaming *stream; 1920 1921 list_for_each_entry(stream, &dev->streams, list) { 1922 if (!video_is_registered(&stream->vdev)) 1923 continue; 1924 1925 video_unregister_device(&stream->vdev); 1926 video_unregister_device(&stream->meta.vdev); 1927 1928 uvc_debugfs_cleanup_stream(stream); 1929 } 1930 1931 uvc_status_unregister(dev); 1932 1933 if (dev->vdev.dev) 1934 v4l2_device_unregister(&dev->vdev); 1935 #ifdef CONFIG_MEDIA_CONTROLLER 1936 if (media_devnode_is_registered(dev->mdev.devnode)) 1937 media_device_unregister(&dev->mdev); 1938 #endif 1939 } 1940 1941 int uvc_register_video_device(struct uvc_device *dev, 1942 struct uvc_streaming *stream, 1943 struct video_device *vdev, 1944 struct uvc_video_queue *queue, 1945 enum v4l2_buf_type type, 1946 const struct v4l2_file_operations *fops, 1947 const struct v4l2_ioctl_ops *ioctl_ops) 1948 { 1949 int ret; 1950 1951 /* Initialize the video buffers queue. */ 1952 ret = uvc_queue_init(queue, type, !uvc_no_drop_param); 1953 if (ret) 1954 return ret; 1955 1956 /* Register the device with V4L. */ 1957 1958 /* 1959 * We already hold a reference to dev->udev. The video device will be 1960 * unregistered before the reference is released, so we don't need to 1961 * get another one. 1962 */ 1963 vdev->v4l2_dev = &dev->vdev; 1964 vdev->fops = fops; 1965 vdev->ioctl_ops = ioctl_ops; 1966 vdev->release = uvc_release; 1967 vdev->prio = &stream->chain->prio; 1968 if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 1969 vdev->vfl_dir = VFL_DIR_TX; 1970 else 1971 vdev->vfl_dir = VFL_DIR_RX; 1972 1973 switch (type) { 1974 case V4L2_BUF_TYPE_VIDEO_CAPTURE: 1975 default: 1976 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; 1977 break; 1978 case V4L2_BUF_TYPE_VIDEO_OUTPUT: 1979 vdev->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; 1980 break; 1981 case V4L2_BUF_TYPE_META_CAPTURE: 1982 vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING; 1983 break; 1984 } 1985 1986 strscpy(vdev->name, dev->name, sizeof(vdev->name)); 1987 1988 /* 1989 * Set the driver data before calling video_register_device, otherwise 1990 * the file open() handler might race us. 1991 */ 1992 video_set_drvdata(vdev, stream); 1993 1994 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 1995 if (ret < 0) { 1996 uvc_printk(KERN_ERR, "Failed to register %s device (%d).\n", 1997 v4l2_type_names[type], ret); 1998 return ret; 1999 } 2000 2001 kref_get(&dev->ref); 2002 return 0; 2003 } 2004 2005 static int uvc_register_video(struct uvc_device *dev, 2006 struct uvc_streaming *stream) 2007 { 2008 int ret; 2009 2010 /* Initialize the streaming interface with default parameters. */ 2011 ret = uvc_video_init(stream); 2012 if (ret < 0) { 2013 uvc_printk(KERN_ERR, "Failed to initialize the device (%d).\n", 2014 ret); 2015 return ret; 2016 } 2017 2018 if (stream->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 2019 stream->chain->caps |= V4L2_CAP_VIDEO_CAPTURE 2020 | V4L2_CAP_META_CAPTURE; 2021 else 2022 stream->chain->caps |= V4L2_CAP_VIDEO_OUTPUT; 2023 2024 uvc_debugfs_init_stream(stream); 2025 2026 /* Register the device with V4L. */ 2027 return uvc_register_video_device(dev, stream, &stream->vdev, 2028 &stream->queue, stream->type, 2029 &uvc_fops, &uvc_ioctl_ops); 2030 } 2031 2032 /* 2033 * Register all video devices in all chains. 2034 */ 2035 static int uvc_register_terms(struct uvc_device *dev, 2036 struct uvc_video_chain *chain) 2037 { 2038 struct uvc_streaming *stream; 2039 struct uvc_entity *term; 2040 int ret; 2041 2042 list_for_each_entry(term, &chain->entities, chain) { 2043 if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING) 2044 continue; 2045 2046 stream = uvc_stream_by_id(dev, term->id); 2047 if (stream == NULL) { 2048 uvc_printk(KERN_INFO, "No streaming interface found " 2049 "for terminal %u.", term->id); 2050 continue; 2051 } 2052 2053 stream->chain = chain; 2054 ret = uvc_register_video(dev, stream); 2055 if (ret < 0) 2056 return ret; 2057 2058 /* Register a metadata node, but ignore a possible failure, 2059 * complete registration of video nodes anyway. 2060 */ 2061 uvc_meta_register(stream); 2062 2063 term->vdev = &stream->vdev; 2064 } 2065 2066 return 0; 2067 } 2068 2069 static int uvc_register_chains(struct uvc_device *dev) 2070 { 2071 struct uvc_video_chain *chain; 2072 int ret; 2073 2074 list_for_each_entry(chain, &dev->chains, list) { 2075 ret = uvc_register_terms(dev, chain); 2076 if (ret < 0) 2077 return ret; 2078 2079 #ifdef CONFIG_MEDIA_CONTROLLER 2080 ret = uvc_mc_register_entities(chain); 2081 if (ret < 0) 2082 uvc_printk(KERN_INFO, 2083 "Failed to register entities (%d).\n", ret); 2084 #endif 2085 } 2086 2087 return 0; 2088 } 2089 2090 /* ------------------------------------------------------------------------ 2091 * USB probe, disconnect, suspend and resume 2092 */ 2093 2094 static const struct uvc_device_info uvc_quirk_none = { 0 }; 2095 2096 static int uvc_probe(struct usb_interface *intf, 2097 const struct usb_device_id *id) 2098 { 2099 struct usb_device *udev = interface_to_usbdev(intf); 2100 struct uvc_device *dev; 2101 const struct uvc_device_info *info = 2102 (const struct uvc_device_info *)id->driver_info; 2103 int function; 2104 int ret; 2105 2106 if (id->idVendor && id->idProduct) 2107 uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s " 2108 "(%04x:%04x)\n", udev->devpath, id->idVendor, 2109 id->idProduct); 2110 else 2111 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n", 2112 udev->devpath); 2113 2114 /* Allocate memory for the device and initialize it. */ 2115 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2116 if (dev == NULL) 2117 return -ENOMEM; 2118 2119 INIT_LIST_HEAD(&dev->entities); 2120 INIT_LIST_HEAD(&dev->chains); 2121 INIT_LIST_HEAD(&dev->streams); 2122 kref_init(&dev->ref); 2123 atomic_set(&dev->nmappings, 0); 2124 mutex_init(&dev->lock); 2125 2126 dev->udev = usb_get_dev(udev); 2127 dev->intf = usb_get_intf(intf); 2128 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; 2129 dev->info = info ? info : &uvc_quirk_none; 2130 dev->quirks = uvc_quirks_param == -1 2131 ? dev->info->quirks : uvc_quirks_param; 2132 2133 if (udev->product != NULL) 2134 strscpy(dev->name, udev->product, sizeof(dev->name)); 2135 else 2136 snprintf(dev->name, sizeof(dev->name), 2137 "UVC Camera (%04x:%04x)", 2138 le16_to_cpu(udev->descriptor.idVendor), 2139 le16_to_cpu(udev->descriptor.idProduct)); 2140 2141 /* 2142 * Add iFunction or iInterface to names when available as additional 2143 * distinguishers between interfaces. iFunction is prioritized over 2144 * iInterface which matches Windows behavior at the point of writing. 2145 */ 2146 if (intf->intf_assoc && intf->intf_assoc->iFunction != 0) 2147 function = intf->intf_assoc->iFunction; 2148 else 2149 function = intf->cur_altsetting->desc.iInterface; 2150 if (function != 0) { 2151 size_t len; 2152 2153 strlcat(dev->name, ": ", sizeof(dev->name)); 2154 len = strlen(dev->name); 2155 usb_string(udev, function, dev->name + len, 2156 sizeof(dev->name) - len); 2157 } 2158 2159 /* Parse the Video Class control descriptor. */ 2160 if (uvc_parse_control(dev) < 0) { 2161 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC " 2162 "descriptors.\n"); 2163 goto error; 2164 } 2165 2166 uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n", 2167 dev->uvc_version >> 8, dev->uvc_version & 0xff, 2168 udev->product ? udev->product : "<unnamed>", 2169 le16_to_cpu(udev->descriptor.idVendor), 2170 le16_to_cpu(udev->descriptor.idProduct)); 2171 2172 if (dev->quirks != dev->info->quirks) { 2173 uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module " 2174 "parameter for testing purpose.\n", dev->quirks); 2175 uvc_printk(KERN_INFO, "Please report required quirks to the " 2176 "linux-uvc-devel mailing list.\n"); 2177 } 2178 2179 /* Initialize the media device and register the V4L2 device. */ 2180 #ifdef CONFIG_MEDIA_CONTROLLER 2181 dev->mdev.dev = &intf->dev; 2182 strscpy(dev->mdev.model, dev->name, sizeof(dev->mdev.model)); 2183 if (udev->serial) 2184 strscpy(dev->mdev.serial, udev->serial, 2185 sizeof(dev->mdev.serial)); 2186 usb_make_path(udev, dev->mdev.bus_info, sizeof(dev->mdev.bus_info)); 2187 dev->mdev.hw_revision = le16_to_cpu(udev->descriptor.bcdDevice); 2188 media_device_init(&dev->mdev); 2189 2190 dev->vdev.mdev = &dev->mdev; 2191 #endif 2192 if (v4l2_device_register(&intf->dev, &dev->vdev) < 0) 2193 goto error; 2194 2195 /* Initialize controls. */ 2196 if (uvc_ctrl_init_device(dev) < 0) 2197 goto error; 2198 2199 /* Scan the device for video chains. */ 2200 if (uvc_scan_device(dev) < 0) 2201 goto error; 2202 2203 /* Register video device nodes. */ 2204 if (uvc_register_chains(dev) < 0) 2205 goto error; 2206 2207 #ifdef CONFIG_MEDIA_CONTROLLER 2208 /* Register the media device node */ 2209 if (media_device_register(&dev->mdev) < 0) 2210 goto error; 2211 #endif 2212 /* Save our data pointer in the interface data. */ 2213 usb_set_intfdata(intf, dev); 2214 2215 /* Initialize the interrupt URB. */ 2216 if ((ret = uvc_status_init(dev)) < 0) { 2217 uvc_printk(KERN_INFO, "Unable to initialize the status " 2218 "endpoint (%d), status interrupt will not be " 2219 "supported.\n", ret); 2220 } 2221 2222 uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n"); 2223 usb_enable_autosuspend(udev); 2224 return 0; 2225 2226 error: 2227 uvc_unregister_video(dev); 2228 kref_put(&dev->ref, uvc_delete); 2229 return -ENODEV; 2230 } 2231 2232 static void uvc_disconnect(struct usb_interface *intf) 2233 { 2234 struct uvc_device *dev = usb_get_intfdata(intf); 2235 2236 /* Set the USB interface data to NULL. This can be done outside the 2237 * lock, as there's no other reader. 2238 */ 2239 usb_set_intfdata(intf, NULL); 2240 2241 if (intf->cur_altsetting->desc.bInterfaceSubClass == 2242 UVC_SC_VIDEOSTREAMING) 2243 return; 2244 2245 uvc_unregister_video(dev); 2246 kref_put(&dev->ref, uvc_delete); 2247 } 2248 2249 static int uvc_suspend(struct usb_interface *intf, pm_message_t message) 2250 { 2251 struct uvc_device *dev = usb_get_intfdata(intf); 2252 struct uvc_streaming *stream; 2253 2254 uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n", 2255 intf->cur_altsetting->desc.bInterfaceNumber); 2256 2257 /* Controls are cached on the fly so they don't need to be saved. */ 2258 if (intf->cur_altsetting->desc.bInterfaceSubClass == 2259 UVC_SC_VIDEOCONTROL) { 2260 mutex_lock(&dev->lock); 2261 if (dev->users) 2262 uvc_status_stop(dev); 2263 mutex_unlock(&dev->lock); 2264 return 0; 2265 } 2266 2267 list_for_each_entry(stream, &dev->streams, list) { 2268 if (stream->intf == intf) 2269 return uvc_video_suspend(stream); 2270 } 2271 2272 uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface " 2273 "mismatch.\n"); 2274 return -EINVAL; 2275 } 2276 2277 static int __uvc_resume(struct usb_interface *intf, int reset) 2278 { 2279 struct uvc_device *dev = usb_get_intfdata(intf); 2280 struct uvc_streaming *stream; 2281 int ret = 0; 2282 2283 uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n", 2284 intf->cur_altsetting->desc.bInterfaceNumber); 2285 2286 if (intf->cur_altsetting->desc.bInterfaceSubClass == 2287 UVC_SC_VIDEOCONTROL) { 2288 if (reset) { 2289 ret = uvc_ctrl_restore_values(dev); 2290 if (ret < 0) 2291 return ret; 2292 } 2293 2294 mutex_lock(&dev->lock); 2295 if (dev->users) 2296 ret = uvc_status_start(dev, GFP_NOIO); 2297 mutex_unlock(&dev->lock); 2298 2299 return ret; 2300 } 2301 2302 list_for_each_entry(stream, &dev->streams, list) { 2303 if (stream->intf == intf) { 2304 ret = uvc_video_resume(stream, reset); 2305 if (ret < 0) 2306 uvc_queue_streamoff(&stream->queue, 2307 stream->queue.queue.type); 2308 return ret; 2309 } 2310 } 2311 2312 uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface " 2313 "mismatch.\n"); 2314 return -EINVAL; 2315 } 2316 2317 static int uvc_resume(struct usb_interface *intf) 2318 { 2319 return __uvc_resume(intf, 0); 2320 } 2321 2322 static int uvc_reset_resume(struct usb_interface *intf) 2323 { 2324 return __uvc_resume(intf, 1); 2325 } 2326 2327 /* ------------------------------------------------------------------------ 2328 * Module parameters 2329 */ 2330 2331 static int uvc_clock_param_get(char *buffer, const struct kernel_param *kp) 2332 { 2333 if (uvc_clock_param == CLOCK_MONOTONIC) 2334 return sprintf(buffer, "CLOCK_MONOTONIC"); 2335 else 2336 return sprintf(buffer, "CLOCK_REALTIME"); 2337 } 2338 2339 static int uvc_clock_param_set(const char *val, const struct kernel_param *kp) 2340 { 2341 if (strncasecmp(val, "clock_", strlen("clock_")) == 0) 2342 val += strlen("clock_"); 2343 2344 if (strcasecmp(val, "monotonic") == 0) 2345 uvc_clock_param = CLOCK_MONOTONIC; 2346 else if (strcasecmp(val, "realtime") == 0) 2347 uvc_clock_param = CLOCK_REALTIME; 2348 else 2349 return -EINVAL; 2350 2351 return 0; 2352 } 2353 2354 module_param_call(clock, uvc_clock_param_set, uvc_clock_param_get, 2355 &uvc_clock_param, S_IRUGO|S_IWUSR); 2356 MODULE_PARM_DESC(clock, "Video buffers timestamp clock"); 2357 module_param_named(hwtimestamps, uvc_hw_timestamps_param, uint, S_IRUGO|S_IWUSR); 2358 MODULE_PARM_DESC(hwtimestamps, "Use hardware timestamps"); 2359 module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR); 2360 MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames"); 2361 module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR); 2362 MODULE_PARM_DESC(quirks, "Forced device quirks"); 2363 module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR); 2364 MODULE_PARM_DESC(trace, "Trace level bitmask"); 2365 module_param_named(timeout, uvc_timeout_param, uint, S_IRUGO|S_IWUSR); 2366 MODULE_PARM_DESC(timeout, "Streaming control requests timeout"); 2367 2368 /* ------------------------------------------------------------------------ 2369 * Driver initialization and cleanup 2370 */ 2371 2372 static const struct uvc_device_info uvc_quirk_probe_minmax = { 2373 .quirks = UVC_QUIRK_PROBE_MINMAX, 2374 }; 2375 2376 static const struct uvc_device_info uvc_quirk_fix_bandwidth = { 2377 .quirks = UVC_QUIRK_FIX_BANDWIDTH, 2378 }; 2379 2380 static const struct uvc_device_info uvc_quirk_probe_def = { 2381 .quirks = UVC_QUIRK_PROBE_DEF, 2382 }; 2383 2384 static const struct uvc_device_info uvc_quirk_stream_no_fid = { 2385 .quirks = UVC_QUIRK_STREAM_NO_FID, 2386 }; 2387 2388 static const struct uvc_device_info uvc_quirk_force_y8 = { 2389 .quirks = UVC_QUIRK_FORCE_Y8, 2390 }; 2391 2392 #define UVC_INFO_QUIRK(q) (kernel_ulong_t)&(struct uvc_device_info){.quirks = q} 2393 #define UVC_INFO_META(m) (kernel_ulong_t)&(struct uvc_device_info) \ 2394 {.meta_format = m} 2395 2396 /* 2397 * The Logitech cameras listed below have their interface class set to 2398 * VENDOR_SPEC because they don't announce themselves as UVC devices, even 2399 * though they are compliant. 2400 */ 2401 static const struct usb_device_id uvc_ids[] = { 2402 /* LogiLink Wireless Webcam */ 2403 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2404 | USB_DEVICE_ID_MATCH_INT_INFO, 2405 .idVendor = 0x0416, 2406 .idProduct = 0xa91a, 2407 .bInterfaceClass = USB_CLASS_VIDEO, 2408 .bInterfaceSubClass = 1, 2409 .bInterfaceProtocol = 0, 2410 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2411 /* Genius eFace 2025 */ 2412 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2413 | USB_DEVICE_ID_MATCH_INT_INFO, 2414 .idVendor = 0x0458, 2415 .idProduct = 0x706e, 2416 .bInterfaceClass = USB_CLASS_VIDEO, 2417 .bInterfaceSubClass = 1, 2418 .bInterfaceProtocol = 0, 2419 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2420 /* Microsoft Lifecam NX-6000 */ 2421 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2422 | USB_DEVICE_ID_MATCH_INT_INFO, 2423 .idVendor = 0x045e, 2424 .idProduct = 0x00f8, 2425 .bInterfaceClass = USB_CLASS_VIDEO, 2426 .bInterfaceSubClass = 1, 2427 .bInterfaceProtocol = 0, 2428 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2429 /* Microsoft Lifecam NX-3000 */ 2430 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2431 | USB_DEVICE_ID_MATCH_INT_INFO, 2432 .idVendor = 0x045e, 2433 .idProduct = 0x0721, 2434 .bInterfaceClass = USB_CLASS_VIDEO, 2435 .bInterfaceSubClass = 1, 2436 .bInterfaceProtocol = 0, 2437 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2438 /* Microsoft Lifecam VX-7000 */ 2439 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2440 | USB_DEVICE_ID_MATCH_INT_INFO, 2441 .idVendor = 0x045e, 2442 .idProduct = 0x0723, 2443 .bInterfaceClass = USB_CLASS_VIDEO, 2444 .bInterfaceSubClass = 1, 2445 .bInterfaceProtocol = 0, 2446 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2447 /* Logitech Quickcam Fusion */ 2448 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2449 | USB_DEVICE_ID_MATCH_INT_INFO, 2450 .idVendor = 0x046d, 2451 .idProduct = 0x08c1, 2452 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2453 .bInterfaceSubClass = 1, 2454 .bInterfaceProtocol = 0 }, 2455 /* Logitech Quickcam Orbit MP */ 2456 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2457 | USB_DEVICE_ID_MATCH_INT_INFO, 2458 .idVendor = 0x046d, 2459 .idProduct = 0x08c2, 2460 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2461 .bInterfaceSubClass = 1, 2462 .bInterfaceProtocol = 0 }, 2463 /* Logitech Quickcam Pro for Notebook */ 2464 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2465 | USB_DEVICE_ID_MATCH_INT_INFO, 2466 .idVendor = 0x046d, 2467 .idProduct = 0x08c3, 2468 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2469 .bInterfaceSubClass = 1, 2470 .bInterfaceProtocol = 0 }, 2471 /* Logitech Quickcam Pro 5000 */ 2472 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2473 | USB_DEVICE_ID_MATCH_INT_INFO, 2474 .idVendor = 0x046d, 2475 .idProduct = 0x08c5, 2476 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2477 .bInterfaceSubClass = 1, 2478 .bInterfaceProtocol = 0 }, 2479 /* Logitech Quickcam OEM Dell Notebook */ 2480 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2481 | USB_DEVICE_ID_MATCH_INT_INFO, 2482 .idVendor = 0x046d, 2483 .idProduct = 0x08c6, 2484 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2485 .bInterfaceSubClass = 1, 2486 .bInterfaceProtocol = 0 }, 2487 /* Logitech Quickcam OEM Cisco VT Camera II */ 2488 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2489 | USB_DEVICE_ID_MATCH_INT_INFO, 2490 .idVendor = 0x046d, 2491 .idProduct = 0x08c7, 2492 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2493 .bInterfaceSubClass = 1, 2494 .bInterfaceProtocol = 0 }, 2495 /* Logitech HD Pro Webcam C920 */ 2496 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2497 | USB_DEVICE_ID_MATCH_INT_INFO, 2498 .idVendor = 0x046d, 2499 .idProduct = 0x082d, 2500 .bInterfaceClass = USB_CLASS_VIDEO, 2501 .bInterfaceSubClass = 1, 2502 .bInterfaceProtocol = 0, 2503 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTORE_CTRLS_ON_INIT) }, 2504 /* Chicony CNF7129 (Asus EEE 100HE) */ 2505 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2506 | USB_DEVICE_ID_MATCH_INT_INFO, 2507 .idVendor = 0x04f2, 2508 .idProduct = 0xb071, 2509 .bInterfaceClass = USB_CLASS_VIDEO, 2510 .bInterfaceSubClass = 1, 2511 .bInterfaceProtocol = 0, 2512 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_RESTRICT_FRAME_RATE) }, 2513 /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */ 2514 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2515 | USB_DEVICE_ID_MATCH_INT_INFO, 2516 .idVendor = 0x058f, 2517 .idProduct = 0x3820, 2518 .bInterfaceClass = USB_CLASS_VIDEO, 2519 .bInterfaceSubClass = 1, 2520 .bInterfaceProtocol = 0, 2521 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2522 /* Dell XPS m1530 */ 2523 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2524 | USB_DEVICE_ID_MATCH_INT_INFO, 2525 .idVendor = 0x05a9, 2526 .idProduct = 0x2640, 2527 .bInterfaceClass = USB_CLASS_VIDEO, 2528 .bInterfaceSubClass = 1, 2529 .bInterfaceProtocol = 0, 2530 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2531 /* Dell SP2008WFP Monitor */ 2532 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2533 | USB_DEVICE_ID_MATCH_INT_INFO, 2534 .idVendor = 0x05a9, 2535 .idProduct = 0x2641, 2536 .bInterfaceClass = USB_CLASS_VIDEO, 2537 .bInterfaceSubClass = 1, 2538 .bInterfaceProtocol = 0, 2539 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2540 /* Dell Alienware X51 */ 2541 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2542 | USB_DEVICE_ID_MATCH_INT_INFO, 2543 .idVendor = 0x05a9, 2544 .idProduct = 0x2643, 2545 .bInterfaceClass = USB_CLASS_VIDEO, 2546 .bInterfaceSubClass = 1, 2547 .bInterfaceProtocol = 0, 2548 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2549 /* Dell Studio Hybrid 140g (OmniVision webcam) */ 2550 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2551 | USB_DEVICE_ID_MATCH_INT_INFO, 2552 .idVendor = 0x05a9, 2553 .idProduct = 0x264a, 2554 .bInterfaceClass = USB_CLASS_VIDEO, 2555 .bInterfaceSubClass = 1, 2556 .bInterfaceProtocol = 0, 2557 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2558 /* Dell XPS M1330 (OmniVision OV7670 webcam) */ 2559 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2560 | USB_DEVICE_ID_MATCH_INT_INFO, 2561 .idVendor = 0x05a9, 2562 .idProduct = 0x7670, 2563 .bInterfaceClass = USB_CLASS_VIDEO, 2564 .bInterfaceSubClass = 1, 2565 .bInterfaceProtocol = 0, 2566 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2567 /* Apple Built-In iSight */ 2568 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2569 | USB_DEVICE_ID_MATCH_INT_INFO, 2570 .idVendor = 0x05ac, 2571 .idProduct = 0x8501, 2572 .bInterfaceClass = USB_CLASS_VIDEO, 2573 .bInterfaceSubClass = 1, 2574 .bInterfaceProtocol = 0, 2575 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX 2576 | UVC_QUIRK_BUILTIN_ISIGHT) }, 2577 /* Apple Built-In iSight via iBridge */ 2578 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2579 | USB_DEVICE_ID_MATCH_INT_INFO, 2580 .idVendor = 0x05ac, 2581 .idProduct = 0x8600, 2582 .bInterfaceClass = USB_CLASS_VIDEO, 2583 .bInterfaceSubClass = 1, 2584 .bInterfaceProtocol = 0, 2585 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2586 /* Foxlink ("HP Webcam" on HP Mini 5103) */ 2587 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2588 | USB_DEVICE_ID_MATCH_INT_INFO, 2589 .idVendor = 0x05c8, 2590 .idProduct = 0x0403, 2591 .bInterfaceClass = USB_CLASS_VIDEO, 2592 .bInterfaceSubClass = 1, 2593 .bInterfaceProtocol = 0, 2594 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth }, 2595 /* Genesys Logic USB 2.0 PC Camera */ 2596 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2597 | USB_DEVICE_ID_MATCH_INT_INFO, 2598 .idVendor = 0x05e3, 2599 .idProduct = 0x0505, 2600 .bInterfaceClass = USB_CLASS_VIDEO, 2601 .bInterfaceSubClass = 1, 2602 .bInterfaceProtocol = 0, 2603 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2604 /* Hercules Classic Silver */ 2605 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2606 | USB_DEVICE_ID_MATCH_INT_INFO, 2607 .idVendor = 0x06f8, 2608 .idProduct = 0x300c, 2609 .bInterfaceClass = USB_CLASS_VIDEO, 2610 .bInterfaceSubClass = 1, 2611 .bInterfaceProtocol = 0, 2612 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth }, 2613 /* ViMicro Vega */ 2614 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2615 | USB_DEVICE_ID_MATCH_INT_INFO, 2616 .idVendor = 0x0ac8, 2617 .idProduct = 0x332d, 2618 .bInterfaceClass = USB_CLASS_VIDEO, 2619 .bInterfaceSubClass = 1, 2620 .bInterfaceProtocol = 0, 2621 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth }, 2622 /* ViMicro - Minoru3D */ 2623 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2624 | USB_DEVICE_ID_MATCH_INT_INFO, 2625 .idVendor = 0x0ac8, 2626 .idProduct = 0x3410, 2627 .bInterfaceClass = USB_CLASS_VIDEO, 2628 .bInterfaceSubClass = 1, 2629 .bInterfaceProtocol = 0, 2630 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth }, 2631 /* ViMicro Venus - Minoru3D */ 2632 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2633 | USB_DEVICE_ID_MATCH_INT_INFO, 2634 .idVendor = 0x0ac8, 2635 .idProduct = 0x3420, 2636 .bInterfaceClass = USB_CLASS_VIDEO, 2637 .bInterfaceSubClass = 1, 2638 .bInterfaceProtocol = 0, 2639 .driver_info = (kernel_ulong_t)&uvc_quirk_fix_bandwidth }, 2640 /* Ophir Optronics - SPCAM 620U */ 2641 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2642 | USB_DEVICE_ID_MATCH_INT_INFO, 2643 .idVendor = 0x0bd3, 2644 .idProduct = 0x0555, 2645 .bInterfaceClass = USB_CLASS_VIDEO, 2646 .bInterfaceSubClass = 1, 2647 .bInterfaceProtocol = 0, 2648 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2649 /* MT6227 */ 2650 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2651 | USB_DEVICE_ID_MATCH_INT_INFO, 2652 .idVendor = 0x0e8d, 2653 .idProduct = 0x0004, 2654 .bInterfaceClass = USB_CLASS_VIDEO, 2655 .bInterfaceSubClass = 1, 2656 .bInterfaceProtocol = 0, 2657 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX 2658 | UVC_QUIRK_PROBE_DEF) }, 2659 /* IMC Networks (Medion Akoya) */ 2660 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2661 | USB_DEVICE_ID_MATCH_INT_INFO, 2662 .idVendor = 0x13d3, 2663 .idProduct = 0x5103, 2664 .bInterfaceClass = USB_CLASS_VIDEO, 2665 .bInterfaceSubClass = 1, 2666 .bInterfaceProtocol = 0, 2667 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2668 /* JMicron USB2.0 XGA WebCam */ 2669 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2670 | USB_DEVICE_ID_MATCH_INT_INFO, 2671 .idVendor = 0x152d, 2672 .idProduct = 0x0310, 2673 .bInterfaceClass = USB_CLASS_VIDEO, 2674 .bInterfaceSubClass = 1, 2675 .bInterfaceProtocol = 0, 2676 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2677 /* Syntek (HP Spartan) */ 2678 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2679 | USB_DEVICE_ID_MATCH_INT_INFO, 2680 .idVendor = 0x174f, 2681 .idProduct = 0x5212, 2682 .bInterfaceClass = USB_CLASS_VIDEO, 2683 .bInterfaceSubClass = 1, 2684 .bInterfaceProtocol = 0, 2685 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2686 /* Syntek (Samsung Q310) */ 2687 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2688 | USB_DEVICE_ID_MATCH_INT_INFO, 2689 .idVendor = 0x174f, 2690 .idProduct = 0x5931, 2691 .bInterfaceClass = USB_CLASS_VIDEO, 2692 .bInterfaceSubClass = 1, 2693 .bInterfaceProtocol = 0, 2694 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2695 /* Syntek (Packard Bell EasyNote MX52 */ 2696 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2697 | USB_DEVICE_ID_MATCH_INT_INFO, 2698 .idVendor = 0x174f, 2699 .idProduct = 0x8a12, 2700 .bInterfaceClass = USB_CLASS_VIDEO, 2701 .bInterfaceSubClass = 1, 2702 .bInterfaceProtocol = 0, 2703 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2704 /* Syntek (Asus F9SG) */ 2705 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2706 | USB_DEVICE_ID_MATCH_INT_INFO, 2707 .idVendor = 0x174f, 2708 .idProduct = 0x8a31, 2709 .bInterfaceClass = USB_CLASS_VIDEO, 2710 .bInterfaceSubClass = 1, 2711 .bInterfaceProtocol = 0, 2712 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2713 /* Syntek (Asus U3S) */ 2714 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2715 | USB_DEVICE_ID_MATCH_INT_INFO, 2716 .idVendor = 0x174f, 2717 .idProduct = 0x8a33, 2718 .bInterfaceClass = USB_CLASS_VIDEO, 2719 .bInterfaceSubClass = 1, 2720 .bInterfaceProtocol = 0, 2721 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2722 /* Syntek (JAOtech Smart Terminal) */ 2723 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2724 | USB_DEVICE_ID_MATCH_INT_INFO, 2725 .idVendor = 0x174f, 2726 .idProduct = 0x8a34, 2727 .bInterfaceClass = USB_CLASS_VIDEO, 2728 .bInterfaceSubClass = 1, 2729 .bInterfaceProtocol = 0, 2730 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2731 /* Miricle 307K */ 2732 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2733 | USB_DEVICE_ID_MATCH_INT_INFO, 2734 .idVendor = 0x17dc, 2735 .idProduct = 0x0202, 2736 .bInterfaceClass = USB_CLASS_VIDEO, 2737 .bInterfaceSubClass = 1, 2738 .bInterfaceProtocol = 0, 2739 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2740 /* Lenovo Thinkpad SL400/SL500 */ 2741 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2742 | USB_DEVICE_ID_MATCH_INT_INFO, 2743 .idVendor = 0x17ef, 2744 .idProduct = 0x480b, 2745 .bInterfaceClass = USB_CLASS_VIDEO, 2746 .bInterfaceSubClass = 1, 2747 .bInterfaceProtocol = 0, 2748 .driver_info = (kernel_ulong_t)&uvc_quirk_stream_no_fid }, 2749 /* Aveo Technology USB 2.0 Camera */ 2750 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2751 | USB_DEVICE_ID_MATCH_INT_INFO, 2752 .idVendor = 0x1871, 2753 .idProduct = 0x0306, 2754 .bInterfaceClass = USB_CLASS_VIDEO, 2755 .bInterfaceSubClass = 1, 2756 .bInterfaceProtocol = 0, 2757 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX 2758 | UVC_QUIRK_PROBE_EXTRAFIELDS) }, 2759 /* Aveo Technology USB 2.0 Camera (Tasco USB Microscope) */ 2760 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2761 | USB_DEVICE_ID_MATCH_INT_INFO, 2762 .idVendor = 0x1871, 2763 .idProduct = 0x0516, 2764 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2765 .bInterfaceSubClass = 1, 2766 .bInterfaceProtocol = 0 }, 2767 /* Ecamm Pico iMage */ 2768 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2769 | USB_DEVICE_ID_MATCH_INT_INFO, 2770 .idVendor = 0x18cd, 2771 .idProduct = 0xcafe, 2772 .bInterfaceClass = USB_CLASS_VIDEO, 2773 .bInterfaceSubClass = 1, 2774 .bInterfaceProtocol = 0, 2775 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_PROBE_EXTRAFIELDS) }, 2776 /* Manta MM-353 Plako */ 2777 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2778 | USB_DEVICE_ID_MATCH_INT_INFO, 2779 .idVendor = 0x18ec, 2780 .idProduct = 0x3188, 2781 .bInterfaceClass = USB_CLASS_VIDEO, 2782 .bInterfaceSubClass = 1, 2783 .bInterfaceProtocol = 0, 2784 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2785 /* FSC WebCam V30S */ 2786 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2787 | USB_DEVICE_ID_MATCH_INT_INFO, 2788 .idVendor = 0x18ec, 2789 .idProduct = 0x3288, 2790 .bInterfaceClass = USB_CLASS_VIDEO, 2791 .bInterfaceSubClass = 1, 2792 .bInterfaceProtocol = 0, 2793 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2794 /* Arkmicro unbranded */ 2795 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2796 | USB_DEVICE_ID_MATCH_INT_INFO, 2797 .idVendor = 0x18ec, 2798 .idProduct = 0x3290, 2799 .bInterfaceClass = USB_CLASS_VIDEO, 2800 .bInterfaceSubClass = 1, 2801 .bInterfaceProtocol = 0, 2802 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_def }, 2803 /* The Imaging Source USB CCD cameras */ 2804 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2805 | USB_DEVICE_ID_MATCH_INT_INFO, 2806 .idVendor = 0x199e, 2807 .idProduct = 0x8102, 2808 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2809 .bInterfaceSubClass = 1, 2810 .bInterfaceProtocol = 0 }, 2811 /* Bodelin ProScopeHR */ 2812 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2813 | USB_DEVICE_ID_MATCH_DEV_HI 2814 | USB_DEVICE_ID_MATCH_INT_INFO, 2815 .idVendor = 0x19ab, 2816 .idProduct = 0x1000, 2817 .bcdDevice_hi = 0x0126, 2818 .bInterfaceClass = USB_CLASS_VIDEO, 2819 .bInterfaceSubClass = 1, 2820 .bInterfaceProtocol = 0, 2821 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_STATUS_INTERVAL) }, 2822 /* MSI StarCam 370i */ 2823 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2824 | USB_DEVICE_ID_MATCH_INT_INFO, 2825 .idVendor = 0x1b3b, 2826 .idProduct = 0x2951, 2827 .bInterfaceClass = USB_CLASS_VIDEO, 2828 .bInterfaceSubClass = 1, 2829 .bInterfaceProtocol = 0, 2830 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2831 /* Generalplus Technology Inc. 808 Camera */ 2832 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2833 | USB_DEVICE_ID_MATCH_INT_INFO, 2834 .idVendor = 0x1b3f, 2835 .idProduct = 0x2002, 2836 .bInterfaceClass = USB_CLASS_VIDEO, 2837 .bInterfaceSubClass = 1, 2838 .bInterfaceProtocol = 0, 2839 .driver_info = (kernel_ulong_t)&uvc_quirk_probe_minmax }, 2840 /* SiGma Micro USB Web Camera */ 2841 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2842 | USB_DEVICE_ID_MATCH_INT_INFO, 2843 .idVendor = 0x1c4f, 2844 .idProduct = 0x3000, 2845 .bInterfaceClass = USB_CLASS_VIDEO, 2846 .bInterfaceSubClass = 1, 2847 .bInterfaceProtocol = 0, 2848 .driver_info = UVC_INFO_QUIRK(UVC_QUIRK_PROBE_MINMAX 2849 | UVC_QUIRK_IGNORE_SELECTOR_UNIT) }, 2850 /* Oculus VR Positional Tracker DK2 */ 2851 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2852 | USB_DEVICE_ID_MATCH_INT_INFO, 2853 .idVendor = 0x2833, 2854 .idProduct = 0x0201, 2855 .bInterfaceClass = USB_CLASS_VIDEO, 2856 .bInterfaceSubClass = 1, 2857 .bInterfaceProtocol = 0, 2858 .driver_info = (kernel_ulong_t)&uvc_quirk_force_y8 }, 2859 /* Oculus VR Rift Sensor */ 2860 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2861 | USB_DEVICE_ID_MATCH_INT_INFO, 2862 .idVendor = 0x2833, 2863 .idProduct = 0x0211, 2864 .bInterfaceClass = USB_CLASS_VENDOR_SPEC, 2865 .bInterfaceSubClass = 1, 2866 .bInterfaceProtocol = 0, 2867 .driver_info = (kernel_ulong_t)&uvc_quirk_force_y8 }, 2868 /* Intel RealSense D4M */ 2869 { .match_flags = USB_DEVICE_ID_MATCH_DEVICE 2870 | USB_DEVICE_ID_MATCH_INT_INFO, 2871 .idVendor = 0x8086, 2872 .idProduct = 0x0b03, 2873 .bInterfaceClass = USB_CLASS_VIDEO, 2874 .bInterfaceSubClass = 1, 2875 .bInterfaceProtocol = 0, 2876 .driver_info = UVC_INFO_META(V4L2_META_FMT_D4XX) }, 2877 /* Generic USB Video Class */ 2878 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_UNDEFINED) }, 2879 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, UVC_PC_PROTOCOL_15) }, 2880 {} 2881 }; 2882 2883 MODULE_DEVICE_TABLE(usb, uvc_ids); 2884 2885 struct uvc_driver uvc_driver = { 2886 .driver = { 2887 .name = "uvcvideo", 2888 .probe = uvc_probe, 2889 .disconnect = uvc_disconnect, 2890 .suspend = uvc_suspend, 2891 .resume = uvc_resume, 2892 .reset_resume = uvc_reset_resume, 2893 .id_table = uvc_ids, 2894 .supports_autosuspend = 1, 2895 }, 2896 }; 2897 2898 static int __init uvc_init(void) 2899 { 2900 int ret; 2901 2902 uvc_debugfs_init(); 2903 2904 ret = usb_register(&uvc_driver.driver); 2905 if (ret < 0) { 2906 uvc_debugfs_cleanup(); 2907 return ret; 2908 } 2909 2910 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n"); 2911 return 0; 2912 } 2913 2914 static void __exit uvc_cleanup(void) 2915 { 2916 usb_deregister(&uvc_driver.driver); 2917 uvc_debugfs_cleanup(); 2918 } 2919 2920 module_init(uvc_init); 2921 module_exit(uvc_cleanup); 2922 2923 MODULE_AUTHOR(DRIVER_AUTHOR); 2924 MODULE_DESCRIPTION(DRIVER_DESC); 2925 MODULE_LICENSE("GPL"); 2926 MODULE_VERSION(DRIVER_VERSION); 2927 2928