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