linux/drivers/media/video/uvc/uvc_driver.c
<<
>>
Prefs
   1/*
   2 *      uvc_driver.c  --  USB Video Class driver
   3 *
   4 *      Copyright (C) 2005-2009
   5 *          Laurent Pinchart (laurent.pinchart@skynet.be)
   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/*
  15 * This driver aims to support video input and ouput devices compliant with the
  16 * 'USB Video Class' specification.
  17 *
  18 * The driver doesn't support the deprecated v4l1 interface. It implements the
  19 * mmap capture method only, and doesn't do any image format conversion in
  20 * software. If your user-space application doesn't support YUYV or MJPEG, fix
  21 * it :-). Please note that the MJPEG data have been stripped from their
  22 * Huffman tables (DHT marker), you will need to add it back if your JPEG
  23 * codec can't handle MJPEG data.
  24 */
  25
  26#include <linux/kernel.h>
  27#include <linux/list.h>
  28#include <linux/module.h>
  29#include <linux/usb.h>
  30#include <linux/videodev2.h>
  31#include <linux/vmalloc.h>
  32#include <linux/wait.h>
  33#include <asm/atomic.h>
  34#include <asm/unaligned.h>
  35
  36#include <media/v4l2-common.h>
  37
  38#include "uvcvideo.h"
  39
  40#define DRIVER_AUTHOR           "Laurent Pinchart <laurent.pinchart@skynet.be>"
  41#define DRIVER_DESC             "USB Video Class driver"
  42#ifndef DRIVER_VERSION
  43#define DRIVER_VERSION          "v0.1.0"
  44#endif
  45
  46unsigned int uvc_no_drop_param;
  47static unsigned int uvc_quirks_param;
  48unsigned int uvc_trace_param;
  49
  50/* ------------------------------------------------------------------------
  51 * Video formats
  52 */
  53
  54static struct uvc_format_desc uvc_fmts[] = {
  55        {
  56                .name           = "YUV 4:2:2 (YUYV)",
  57                .guid           = UVC_GUID_FORMAT_YUY2,
  58                .fcc            = V4L2_PIX_FMT_YUYV,
  59        },
  60        {
  61                .name           = "YUV 4:2:0 (NV12)",
  62                .guid           = UVC_GUID_FORMAT_NV12,
  63                .fcc            = V4L2_PIX_FMT_NV12,
  64        },
  65        {
  66                .name           = "MJPEG",
  67                .guid           = UVC_GUID_FORMAT_MJPEG,
  68                .fcc            = V4L2_PIX_FMT_MJPEG,
  69        },
  70        {
  71                .name           = "YVU 4:2:0 (YV12)",
  72                .guid           = UVC_GUID_FORMAT_YV12,
  73                .fcc            = V4L2_PIX_FMT_YVU420,
  74        },
  75        {
  76                .name           = "YUV 4:2:0 (I420)",
  77                .guid           = UVC_GUID_FORMAT_I420,
  78                .fcc            = V4L2_PIX_FMT_YUV420,
  79        },
  80        {
  81                .name           = "YUV 4:2:2 (UYVY)",
  82                .guid           = UVC_GUID_FORMAT_UYVY,
  83                .fcc            = V4L2_PIX_FMT_UYVY,
  84        },
  85        {
  86                .name           = "Greyscale",
  87                .guid           = UVC_GUID_FORMAT_Y800,
  88                .fcc            = V4L2_PIX_FMT_GREY,
  89        },
  90        {
  91                .name           = "RGB Bayer",
  92                .guid           = UVC_GUID_FORMAT_BY8,
  93                .fcc            = V4L2_PIX_FMT_SBGGR8,
  94        },
  95};
  96
  97/* ------------------------------------------------------------------------
  98 * Utility functions
  99 */
 100
 101struct usb_host_endpoint *uvc_find_endpoint(struct usb_host_interface *alts,
 102                __u8 epaddr)
 103{
 104        struct usb_host_endpoint *ep;
 105        unsigned int i;
 106
 107        for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
 108                ep = &alts->endpoint[i];
 109                if (ep->desc.bEndpointAddress == epaddr)
 110                        return ep;
 111        }
 112
 113        return NULL;
 114}
 115
 116static struct uvc_format_desc *uvc_format_by_guid(const __u8 guid[16])
 117{
 118        unsigned int len = ARRAY_SIZE(uvc_fmts);
 119        unsigned int i;
 120
 121        for (i = 0; i < len; ++i) {
 122                if (memcmp(guid, uvc_fmts[i].guid, 16) == 0)
 123                        return &uvc_fmts[i];
 124        }
 125
 126        return NULL;
 127}
 128
 129static __u32 uvc_colorspace(const __u8 primaries)
 130{
 131        static const __u8 colorprimaries[] = {
 132                0,
 133                V4L2_COLORSPACE_SRGB,
 134                V4L2_COLORSPACE_470_SYSTEM_M,
 135                V4L2_COLORSPACE_470_SYSTEM_BG,
 136                V4L2_COLORSPACE_SMPTE170M,
 137                V4L2_COLORSPACE_SMPTE240M,
 138        };
 139
 140        if (primaries < ARRAY_SIZE(colorprimaries))
 141                return colorprimaries[primaries];
 142
 143        return 0;
 144}
 145
 146/* Simplify a fraction using a simple continued fraction decomposition. The
 147 * idea here is to convert fractions such as 333333/10000000 to 1/30 using
 148 * 32 bit arithmetic only. The algorithm is not perfect and relies upon two
 149 * arbitrary parameters to remove non-significative terms from the simple
 150 * continued fraction decomposition. Using 8 and 333 for n_terms and threshold
 151 * respectively seems to give nice results.
 152 */
 153void uvc_simplify_fraction(uint32_t *numerator, uint32_t *denominator,
 154                unsigned int n_terms, unsigned int threshold)
 155{
 156        uint32_t *an;
 157        uint32_t x, y, r;
 158        unsigned int i, n;
 159
 160        an = kmalloc(n_terms * sizeof *an, GFP_KERNEL);
 161        if (an == NULL)
 162                return;
 163
 164        /* Convert the fraction to a simple continued fraction. See
 165         * http://mathforum.org/dr.math/faq/faq.fractions.html
 166         * Stop if the current term is bigger than or equal to the given
 167         * threshold.
 168         */
 169        x = *numerator;
 170        y = *denominator;
 171
 172        for (n = 0; n < n_terms && y != 0; ++n) {
 173                an[n] = x / y;
 174                if (an[n] >= threshold) {
 175                        if (n < 2)
 176                                n++;
 177                        break;
 178                }
 179
 180                r = x - an[n] * y;
 181                x = y;
 182                y = r;
 183        }
 184
 185        /* Expand the simple continued fraction back to an integer fraction. */
 186        x = 0;
 187        y = 1;
 188
 189        for (i = n; i > 0; --i) {
 190                r = y;
 191                y = an[i-1] * y + x;
 192                x = r;
 193        }
 194
 195        *numerator = y;
 196        *denominator = x;
 197        kfree(an);
 198}
 199
 200/* Convert a fraction to a frame interval in 100ns multiples. The idea here is
 201 * to compute numerator / denominator * 10000000 using 32 bit fixed point
 202 * arithmetic only.
 203 */
 204uint32_t uvc_fraction_to_interval(uint32_t numerator, uint32_t denominator)
 205{
 206        uint32_t multiplier;
 207
 208        /* Saturate the result if the operation would overflow. */
 209        if (denominator == 0 ||
 210            numerator/denominator >= ((uint32_t)-1)/10000000)
 211                return (uint32_t)-1;
 212
 213        /* Divide both the denominator and the multiplier by two until
 214         * numerator * multiplier doesn't overflow. If anyone knows a better
 215         * algorithm please let me know.
 216         */
 217        multiplier = 10000000;
 218        while (numerator > ((uint32_t)-1)/multiplier) {
 219                multiplier /= 2;
 220                denominator /= 2;
 221        }
 222
 223        return denominator ? numerator * multiplier / denominator : 0;
 224}
 225
 226/* ------------------------------------------------------------------------
 227 * Terminal and unit management
 228 */
 229
 230static struct uvc_entity *uvc_entity_by_id(struct uvc_device *dev, int id)
 231{
 232        struct uvc_entity *entity;
 233
 234        list_for_each_entry(entity, &dev->entities, list) {
 235                if (entity->id == id)
 236                        return entity;
 237        }
 238
 239        return NULL;
 240}
 241
 242static struct uvc_entity *uvc_entity_by_reference(struct uvc_device *dev,
 243        int id, struct uvc_entity *entity)
 244{
 245        unsigned int i;
 246
 247        if (entity == NULL)
 248                entity = list_entry(&dev->entities, struct uvc_entity, list);
 249
 250        list_for_each_entry_continue(entity, &dev->entities, list) {
 251                switch (UVC_ENTITY_TYPE(entity)) {
 252                case UVC_TT_STREAMING:
 253                        if (entity->output.bSourceID == id)
 254                                return entity;
 255                        break;
 256
 257                case UVC_VC_PROCESSING_UNIT:
 258                        if (entity->processing.bSourceID == id)
 259                                return entity;
 260                        break;
 261
 262                case UVC_VC_SELECTOR_UNIT:
 263                        for (i = 0; i < entity->selector.bNrInPins; ++i)
 264                                if (entity->selector.baSourceID[i] == id)
 265                                        return entity;
 266                        break;
 267
 268                case UVC_VC_EXTENSION_UNIT:
 269                        for (i = 0; i < entity->extension.bNrInPins; ++i)
 270                                if (entity->extension.baSourceID[i] == id)
 271                                        return entity;
 272                        break;
 273                }
 274        }
 275
 276        return NULL;
 277}
 278
 279static struct uvc_streaming *uvc_stream_by_id(struct uvc_device *dev, int id)
 280{
 281        struct uvc_streaming *stream;
 282
 283        list_for_each_entry(stream, &dev->streams, list) {
 284                if (stream->header.bTerminalLink == id)
 285                        return stream;
 286        }
 287
 288        return NULL;
 289}
 290
 291/* ------------------------------------------------------------------------
 292 * Descriptors parsing
 293 */
 294
 295static int uvc_parse_format(struct uvc_device *dev,
 296        struct uvc_streaming *streaming, struct uvc_format *format,
 297        __u32 **intervals, unsigned char *buffer, int buflen)
 298{
 299        struct usb_interface *intf = streaming->intf;
 300        struct usb_host_interface *alts = intf->cur_altsetting;
 301        struct uvc_format_desc *fmtdesc;
 302        struct uvc_frame *frame;
 303        const unsigned char *start = buffer;
 304        unsigned int interval;
 305        unsigned int i, n;
 306        __u8 ftype;
 307
 308        format->type = buffer[2];
 309        format->index = buffer[3];
 310
 311        switch (buffer[2]) {
 312        case UVC_VS_FORMAT_UNCOMPRESSED:
 313        case UVC_VS_FORMAT_FRAME_BASED:
 314                n = buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED ? 27 : 28;
 315                if (buflen < n) {
 316                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 317                               "interface %d FORMAT error\n",
 318                               dev->udev->devnum,
 319                               alts->desc.bInterfaceNumber);
 320                        return -EINVAL;
 321                }
 322
 323                /* Find the format descriptor from its GUID. */
 324                fmtdesc = uvc_format_by_guid(&buffer[5]);
 325
 326                if (fmtdesc != NULL) {
 327                        strlcpy(format->name, fmtdesc->name,
 328                                sizeof format->name);
 329                        format->fcc = fmtdesc->fcc;
 330                } else {
 331                        uvc_printk(KERN_INFO, "Unknown video format "
 332                                UVC_GUID_FORMAT "\n",
 333                                UVC_GUID_ARGS(&buffer[5]));
 334                        snprintf(format->name, sizeof format->name,
 335                                UVC_GUID_FORMAT, UVC_GUID_ARGS(&buffer[5]));
 336                        format->fcc = 0;
 337                }
 338
 339                format->bpp = buffer[21];
 340                if (buffer[2] == UVC_VS_FORMAT_UNCOMPRESSED) {
 341                        ftype = UVC_VS_FRAME_UNCOMPRESSED;
 342                } else {
 343                        ftype = UVC_VS_FRAME_FRAME_BASED;
 344                        if (buffer[27])
 345                                format->flags = UVC_FMT_FLAG_COMPRESSED;
 346                }
 347                break;
 348
 349        case UVC_VS_FORMAT_MJPEG:
 350                if (buflen < 11) {
 351                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 352                               "interface %d FORMAT error\n",
 353                               dev->udev->devnum,
 354                               alts->desc.bInterfaceNumber);
 355                        return -EINVAL;
 356                }
 357
 358                strlcpy(format->name, "MJPEG", sizeof format->name);
 359                format->fcc = V4L2_PIX_FMT_MJPEG;
 360                format->flags = UVC_FMT_FLAG_COMPRESSED;
 361                format->bpp = 0;
 362                ftype = UVC_VS_FRAME_MJPEG;
 363                break;
 364
 365        case UVC_VS_FORMAT_DV:
 366                if (buflen < 9) {
 367                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 368                               "interface %d FORMAT error\n",
 369                               dev->udev->devnum,
 370                               alts->desc.bInterfaceNumber);
 371                        return -EINVAL;
 372                }
 373
 374                switch (buffer[8] & 0x7f) {
 375                case 0:
 376                        strlcpy(format->name, "SD-DV", sizeof format->name);
 377                        break;
 378                case 1:
 379                        strlcpy(format->name, "SDL-DV", sizeof format->name);
 380                        break;
 381                case 2:
 382                        strlcpy(format->name, "HD-DV", sizeof format->name);
 383                        break;
 384                default:
 385                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 386                               "interface %d: unknown DV format %u\n",
 387                               dev->udev->devnum,
 388                               alts->desc.bInterfaceNumber, buffer[8]);
 389                        return -EINVAL;
 390                }
 391
 392                strlcat(format->name, buffer[8] & (1 << 7) ? " 60Hz" : " 50Hz",
 393                        sizeof format->name);
 394
 395                format->fcc = V4L2_PIX_FMT_DV;
 396                format->flags = UVC_FMT_FLAG_COMPRESSED | UVC_FMT_FLAG_STREAM;
 397                format->bpp = 0;
 398                ftype = 0;
 399
 400                /* Create a dummy frame descriptor. */
 401                frame = &format->frame[0];
 402                memset(&format->frame[0], 0, sizeof format->frame[0]);
 403                frame->bFrameIntervalType = 1;
 404                frame->dwDefaultFrameInterval = 1;
 405                frame->dwFrameInterval = *intervals;
 406                *(*intervals)++ = 1;
 407                format->nframes = 1;
 408                break;
 409
 410        case UVC_VS_FORMAT_MPEG2TS:
 411        case UVC_VS_FORMAT_STREAM_BASED:
 412                /* Not supported yet. */
 413        default:
 414                uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 415                       "interface %d unsupported format %u\n",
 416                       dev->udev->devnum, alts->desc.bInterfaceNumber,
 417                       buffer[2]);
 418                return -EINVAL;
 419        }
 420
 421        uvc_trace(UVC_TRACE_DESCR, "Found format %s.\n", format->name);
 422
 423        buflen -= buffer[0];
 424        buffer += buffer[0];
 425
 426        /* Parse the frame descriptors. Only uncompressed, MJPEG and frame
 427         * based formats have frame descriptors.
 428         */
 429        while (buflen > 2 && buffer[2] == ftype) {
 430                frame = &format->frame[format->nframes];
 431                if (ftype != UVC_VS_FRAME_FRAME_BASED)
 432                        n = buflen > 25 ? buffer[25] : 0;
 433                else
 434                        n = buflen > 21 ? buffer[21] : 0;
 435
 436                n = n ? n : 3;
 437
 438                if (buflen < 26 + 4*n) {
 439                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 440                               "interface %d FRAME error\n", dev->udev->devnum,
 441                               alts->desc.bInterfaceNumber);
 442                        return -EINVAL;
 443                }
 444
 445                frame->bFrameIndex = buffer[3];
 446                frame->bmCapabilities = buffer[4];
 447                frame->wWidth = get_unaligned_le16(&buffer[5]);
 448                frame->wHeight = get_unaligned_le16(&buffer[7]);
 449                frame->dwMinBitRate = get_unaligned_le32(&buffer[9]);
 450                frame->dwMaxBitRate = get_unaligned_le32(&buffer[13]);
 451                if (ftype != UVC_VS_FRAME_FRAME_BASED) {
 452                        frame->dwMaxVideoFrameBufferSize =
 453                                get_unaligned_le32(&buffer[17]);
 454                        frame->dwDefaultFrameInterval =
 455                                get_unaligned_le32(&buffer[21]);
 456                        frame->bFrameIntervalType = buffer[25];
 457                } else {
 458                        frame->dwMaxVideoFrameBufferSize = 0;
 459                        frame->dwDefaultFrameInterval =
 460                                get_unaligned_le32(&buffer[17]);
 461                        frame->bFrameIntervalType = buffer[21];
 462                }
 463                frame->dwFrameInterval = *intervals;
 464
 465                /* Several UVC chipsets screw up dwMaxVideoFrameBufferSize
 466                 * completely. Observed behaviours range from setting the
 467                 * value to 1.1x the actual frame size to hardwiring the
 468                 * 16 low bits to 0. This results in a higher than necessary
 469                 * memory usage as well as a wrong image size information. For
 470                 * uncompressed formats this can be fixed by computing the
 471                 * value from the frame size.
 472                 */
 473                if (!(format->flags & UVC_FMT_FLAG_COMPRESSED))
 474                        frame->dwMaxVideoFrameBufferSize = format->bpp
 475                                * frame->wWidth * frame->wHeight / 8;
 476
 477                /* Some bogus devices report dwMinFrameInterval equal to
 478                 * dwMaxFrameInterval and have dwFrameIntervalStep set to
 479                 * zero. Setting all null intervals to 1 fixes the problem and
 480                 * some other divisions by zero that could happen.
 481                 */
 482                for (i = 0; i < n; ++i) {
 483                        interval = get_unaligned_le32(&buffer[26+4*i]);
 484                        *(*intervals)++ = interval ? interval : 1;
 485                }
 486
 487                /* Make sure that the default frame interval stays between
 488                 * the boundaries.
 489                 */
 490                n -= frame->bFrameIntervalType ? 1 : 2;
 491                frame->dwDefaultFrameInterval =
 492                        min(frame->dwFrameInterval[n],
 493                            max(frame->dwFrameInterval[0],
 494                                frame->dwDefaultFrameInterval));
 495
 496                uvc_trace(UVC_TRACE_DESCR, "- %ux%u (%u.%u fps)\n",
 497                        frame->wWidth, frame->wHeight,
 498                        10000000/frame->dwDefaultFrameInterval,
 499                        (100000000/frame->dwDefaultFrameInterval)%10);
 500
 501                format->nframes++;
 502                buflen -= buffer[0];
 503                buffer += buffer[0];
 504        }
 505
 506        if (buflen > 2 && buffer[2] == UVC_VS_STILL_IMAGE_FRAME) {
 507                buflen -= buffer[0];
 508                buffer += buffer[0];
 509        }
 510
 511        if (buflen > 2 && buffer[2] == UVC_VS_COLORFORMAT) {
 512                if (buflen < 6) {
 513                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 514                               "interface %d COLORFORMAT error\n",
 515                               dev->udev->devnum,
 516                               alts->desc.bInterfaceNumber);
 517                        return -EINVAL;
 518                }
 519
 520                format->colorspace = uvc_colorspace(buffer[3]);
 521
 522                buflen -= buffer[0];
 523                buffer += buffer[0];
 524        }
 525
 526        return buffer - start;
 527}
 528
 529static int uvc_parse_streaming(struct uvc_device *dev,
 530        struct usb_interface *intf)
 531{
 532        struct uvc_streaming *streaming = NULL;
 533        struct uvc_format *format;
 534        struct uvc_frame *frame;
 535        struct usb_host_interface *alts = &intf->altsetting[0];
 536        unsigned char *_buffer, *buffer = alts->extra;
 537        int _buflen, buflen = alts->extralen;
 538        unsigned int nformats = 0, nframes = 0, nintervals = 0;
 539        unsigned int size, i, n, p;
 540        __u32 *interval;
 541        __u16 psize;
 542        int ret = -EINVAL;
 543
 544        if (intf->cur_altsetting->desc.bInterfaceSubClass
 545                != UVC_SC_VIDEOSTREAMING) {
 546                uvc_trace(UVC_TRACE_DESCR, "device %d interface %d isn't a "
 547                        "video streaming interface\n", dev->udev->devnum,
 548                        intf->altsetting[0].desc.bInterfaceNumber);
 549                return -EINVAL;
 550        }
 551
 552        if (usb_driver_claim_interface(&uvc_driver.driver, intf, dev)) {
 553                uvc_trace(UVC_TRACE_DESCR, "device %d interface %d is already "
 554                        "claimed\n", dev->udev->devnum,
 555                        intf->altsetting[0].desc.bInterfaceNumber);
 556                return -EINVAL;
 557        }
 558
 559        streaming = kzalloc(sizeof *streaming, GFP_KERNEL);
 560        if (streaming == NULL) {
 561                usb_driver_release_interface(&uvc_driver.driver, intf);
 562                return -EINVAL;
 563        }
 564
 565        mutex_init(&streaming->mutex);
 566        streaming->dev = dev;
 567        streaming->intf = usb_get_intf(intf);
 568        streaming->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
 569
 570        /* The Pico iMage webcam has its class-specific interface descriptors
 571         * after the endpoint descriptors.
 572         */
 573        if (buflen == 0) {
 574                for (i = 0; i < alts->desc.bNumEndpoints; ++i) {
 575                        struct usb_host_endpoint *ep = &alts->endpoint[i];
 576
 577                        if (ep->extralen == 0)
 578                                continue;
 579
 580                        if (ep->extralen > 2 &&
 581                            ep->extra[1] == USB_DT_CS_INTERFACE) {
 582                                uvc_trace(UVC_TRACE_DESCR, "trying extra data "
 583                                        "from endpoint %u.\n", i);
 584                                buffer = alts->endpoint[i].extra;
 585                                buflen = alts->endpoint[i].extralen;
 586                                break;
 587                        }
 588                }
 589        }
 590
 591        /* Skip the standard interface descriptors. */
 592        while (buflen > 2 && buffer[1] != USB_DT_CS_INTERFACE) {
 593                buflen -= buffer[0];
 594                buffer += buffer[0];
 595        }
 596
 597        if (buflen <= 2) {
 598                uvc_trace(UVC_TRACE_DESCR, "no class-specific streaming "
 599                        "interface descriptors found.\n");
 600                goto error;
 601        }
 602
 603        /* Parse the header descriptor. */
 604        switch (buffer[2]) {
 605        case UVC_VS_OUTPUT_HEADER:
 606                streaming->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
 607                size = 9;
 608                break;
 609
 610        case UVC_VS_INPUT_HEADER:
 611                streaming->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
 612                size = 13;
 613                break;
 614
 615        default:
 616                uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
 617                        "%d HEADER descriptor not found.\n", dev->udev->devnum,
 618                        alts->desc.bInterfaceNumber);
 619                goto error;
 620        }
 621
 622        p = buflen >= 4 ? buffer[3] : 0;
 623        n = buflen >= size ? buffer[size-1] : 0;
 624
 625        if (buflen < size + p*n) {
 626                uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 627                        "interface %d HEADER descriptor is invalid.\n",
 628                        dev->udev->devnum, alts->desc.bInterfaceNumber);
 629                goto error;
 630        }
 631
 632        streaming->header.bNumFormats = p;
 633        streaming->header.bEndpointAddress = buffer[6];
 634        if (buffer[2] == UVC_VS_INPUT_HEADER) {
 635                streaming->header.bmInfo = buffer[7];
 636                streaming->header.bTerminalLink = buffer[8];
 637                streaming->header.bStillCaptureMethod = buffer[9];
 638                streaming->header.bTriggerSupport = buffer[10];
 639                streaming->header.bTriggerUsage = buffer[11];
 640        } else {
 641                streaming->header.bTerminalLink = buffer[7];
 642        }
 643        streaming->header.bControlSize = n;
 644
 645        streaming->header.bmaControls = kmalloc(p*n, GFP_KERNEL);
 646        if (streaming->header.bmaControls == NULL) {
 647                ret = -ENOMEM;
 648                goto error;
 649        }
 650
 651        memcpy(streaming->header.bmaControls, &buffer[size], p*n);
 652
 653        buflen -= buffer[0];
 654        buffer += buffer[0];
 655
 656        _buffer = buffer;
 657        _buflen = buflen;
 658
 659        /* Count the format and frame descriptors. */
 660        while (_buflen > 2 && _buffer[1] == USB_DT_CS_INTERFACE) {
 661                switch (_buffer[2]) {
 662                case UVC_VS_FORMAT_UNCOMPRESSED:
 663                case UVC_VS_FORMAT_MJPEG:
 664                case UVC_VS_FORMAT_FRAME_BASED:
 665                        nformats++;
 666                        break;
 667
 668                case UVC_VS_FORMAT_DV:
 669                        /* DV format has no frame descriptor. We will create a
 670                         * dummy frame descriptor with a dummy frame interval.
 671                         */
 672                        nformats++;
 673                        nframes++;
 674                        nintervals++;
 675                        break;
 676
 677                case UVC_VS_FORMAT_MPEG2TS:
 678                case UVC_VS_FORMAT_STREAM_BASED:
 679                        uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming "
 680                                "interface %d FORMAT %u is not supported.\n",
 681                                dev->udev->devnum,
 682                                alts->desc.bInterfaceNumber, _buffer[2]);
 683                        break;
 684
 685                case UVC_VS_FRAME_UNCOMPRESSED:
 686                case UVC_VS_FRAME_MJPEG:
 687                        nframes++;
 688                        if (_buflen > 25)
 689                                nintervals += _buffer[25] ? _buffer[25] : 3;
 690                        break;
 691
 692                case UVC_VS_FRAME_FRAME_BASED:
 693                        nframes++;
 694                        if (_buflen > 21)
 695                                nintervals += _buffer[21] ? _buffer[21] : 3;
 696                        break;
 697                }
 698
 699                _buflen -= _buffer[0];
 700                _buffer += _buffer[0];
 701        }
 702
 703        if (nformats == 0) {
 704                uvc_trace(UVC_TRACE_DESCR, "device %d videostreaming interface "
 705                        "%d has no supported formats defined.\n",
 706                        dev->udev->devnum, alts->desc.bInterfaceNumber);
 707                goto error;
 708        }
 709
 710        size = nformats * sizeof *format + nframes * sizeof *frame
 711             + nintervals * sizeof *interval;
 712        format = kzalloc(size, GFP_KERNEL);
 713        if (format == NULL) {
 714                ret = -ENOMEM;
 715                goto error;
 716        }
 717
 718        frame = (struct uvc_frame *)&format[nformats];
 719        interval = (__u32 *)&frame[nframes];
 720
 721        streaming->format = format;
 722        streaming->nformats = nformats;
 723
 724        /* Parse the format descriptors. */
 725        while (buflen > 2 && buffer[1] == USB_DT_CS_INTERFACE) {
 726                switch (buffer[2]) {
 727                case UVC_VS_FORMAT_UNCOMPRESSED:
 728                case UVC_VS_FORMAT_MJPEG:
 729                case UVC_VS_FORMAT_DV:
 730                case UVC_VS_FORMAT_FRAME_BASED:
 731                        format->frame = frame;
 732                        ret = uvc_parse_format(dev, streaming, format,
 733                                &interval, buffer, buflen);
 734                        if (ret < 0)
 735                                goto error;
 736
 737                        frame += format->nframes;
 738                        format++;
 739
 740                        buflen -= ret;
 741                        buffer += ret;
 742                        continue;
 743
 744                default:
 745                        break;
 746                }
 747
 748                buflen -= buffer[0];
 749                buffer += buffer[0];
 750        }
 751
 752        /* Parse the alternate settings to find the maximum bandwidth. */
 753        for (i = 0; i < intf->num_altsetting; ++i) {
 754                struct usb_host_endpoint *ep;
 755                alts = &intf->altsetting[i];
 756                ep = uvc_find_endpoint(alts,
 757                                streaming->header.bEndpointAddress);
 758                if (ep == NULL)
 759                        continue;
 760
 761                psize = le16_to_cpu(ep->desc.wMaxPacketSize);
 762                psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3));
 763                if (psize > streaming->maxpsize)
 764                        streaming->maxpsize = psize;
 765        }
 766
 767        list_add_tail(&streaming->list, &dev->streams);
 768        return 0;
 769
 770error:
 771        usb_driver_release_interface(&uvc_driver.driver, intf);
 772        usb_put_intf(intf);
 773        kfree(streaming->format);
 774        kfree(streaming->header.bmaControls);
 775        kfree(streaming);
 776        return ret;
 777}
 778
 779/* Parse vendor-specific extensions. */
 780static int uvc_parse_vendor_control(struct uvc_device *dev,
 781        const unsigned char *buffer, int buflen)
 782{
 783        struct usb_device *udev = dev->udev;
 784        struct usb_host_interface *alts = dev->intf->cur_altsetting;
 785        struct uvc_entity *unit;
 786        unsigned int n, p;
 787        int handled = 0;
 788
 789        switch (le16_to_cpu(dev->udev->descriptor.idVendor)) {
 790        case 0x046d:            /* Logitech */
 791                if (buffer[1] != 0x41 || buffer[2] != 0x01)
 792                        break;
 793
 794                /* Logitech implements several vendor specific functions
 795                 * through vendor specific extension units (LXU).
 796                 *
 797                 * The LXU descriptors are similar to XU descriptors
 798                 * (see "USB Device Video Class for Video Devices", section
 799                 * 3.7.2.6 "Extension Unit Descriptor") with the following
 800                 * differences:
 801                 *
 802                 * ----------------------------------------------------------
 803                 * 0            bLength         1        Number
 804                 *      Size of this descriptor, in bytes: 24+p+n*2
 805                 * ----------------------------------------------------------
 806                 * 23+p+n       bmControlsType  N       Bitmap
 807                 *      Individual bits in the set are defined:
 808                 *      0: Absolute
 809                 *      1: Relative
 810                 *
 811                 *      This bitset is mapped exactly the same as bmControls.
 812                 * ----------------------------------------------------------
 813                 * 23+p+n*2     bReserved       1       Boolean
 814                 * ----------------------------------------------------------
 815                 * 24+p+n*2     iExtension      1       Index
 816                 *      Index of a string descriptor that describes this
 817                 *      extension unit.
 818                 * ----------------------------------------------------------
 819                 */
 820                p = buflen >= 22 ? buffer[21] : 0;
 821                n = buflen >= 25 + p ? buffer[22+p] : 0;
 822
 823                if (buflen < 25 + p + 2*n) {
 824                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 825                                "interface %d EXTENSION_UNIT error\n",
 826                                udev->devnum, alts->desc.bInterfaceNumber);
 827                        break;
 828                }
 829
 830                unit = kzalloc(sizeof *unit + p + 2*n, GFP_KERNEL);
 831                if (unit == NULL)
 832                        return -ENOMEM;
 833
 834                unit->id = buffer[3];
 835                unit->type = UVC_VC_EXTENSION_UNIT;
 836                memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
 837                unit->extension.bNumControls = buffer[20];
 838                unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
 839                unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
 840                memcpy(unit->extension.baSourceID, &buffer[22], p);
 841                unit->extension.bControlSize = buffer[22+p];
 842                unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
 843                unit->extension.bmControlsType = (__u8 *)unit + sizeof *unit
 844                                               + p + n;
 845                memcpy(unit->extension.bmControls, &buffer[23+p], 2*n);
 846
 847                if (buffer[24+p+2*n] != 0)
 848                        usb_string(udev, buffer[24+p+2*n], unit->name,
 849                                   sizeof unit->name);
 850                else
 851                        sprintf(unit->name, "Extension %u", buffer[3]);
 852
 853                list_add_tail(&unit->list, &dev->entities);
 854                handled = 1;
 855                break;
 856        }
 857
 858        return handled;
 859}
 860
 861static int uvc_parse_standard_control(struct uvc_device *dev,
 862        const unsigned char *buffer, int buflen)
 863{
 864        struct usb_device *udev = dev->udev;
 865        struct uvc_entity *unit, *term;
 866        struct usb_interface *intf;
 867        struct usb_host_interface *alts = dev->intf->cur_altsetting;
 868        unsigned int i, n, p, len;
 869        __u16 type;
 870
 871        switch (buffer[2]) {
 872        case UVC_VC_HEADER:
 873                n = buflen >= 12 ? buffer[11] : 0;
 874
 875                if (buflen < 12 || buflen < 12 + n) {
 876                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 877                                "interface %d HEADER error\n", udev->devnum,
 878                                alts->desc.bInterfaceNumber);
 879                        return -EINVAL;
 880                }
 881
 882                dev->uvc_version = get_unaligned_le16(&buffer[3]);
 883                dev->clock_frequency = get_unaligned_le32(&buffer[7]);
 884
 885                /* Parse all USB Video Streaming interfaces. */
 886                for (i = 0; i < n; ++i) {
 887                        intf = usb_ifnum_to_if(udev, buffer[12+i]);
 888                        if (intf == NULL) {
 889                                uvc_trace(UVC_TRACE_DESCR, "device %d "
 890                                        "interface %d doesn't exists\n",
 891                                        udev->devnum, i);
 892                                continue;
 893                        }
 894
 895                        uvc_parse_streaming(dev, intf);
 896                }
 897                break;
 898
 899        case UVC_VC_INPUT_TERMINAL:
 900                if (buflen < 8) {
 901                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 902                                "interface %d INPUT_TERMINAL error\n",
 903                                udev->devnum, alts->desc.bInterfaceNumber);
 904                        return -EINVAL;
 905                }
 906
 907                /* Make sure the terminal type MSB is not null, otherwise it
 908                 * could be confused with a unit.
 909                 */
 910                type = get_unaligned_le16(&buffer[4]);
 911                if ((type & 0xff00) == 0) {
 912                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 913                                "interface %d INPUT_TERMINAL %d has invalid "
 914                                "type 0x%04x, skipping\n", udev->devnum,
 915                                alts->desc.bInterfaceNumber,
 916                                buffer[3], type);
 917                        return 0;
 918                }
 919
 920                n = 0;
 921                p = 0;
 922                len = 8;
 923
 924                if (type == UVC_ITT_CAMERA) {
 925                        n = buflen >= 15 ? buffer[14] : 0;
 926                        len = 15;
 927
 928                } else if (type == UVC_ITT_MEDIA_TRANSPORT_INPUT) {
 929                        n = buflen >= 9 ? buffer[8] : 0;
 930                        p = buflen >= 10 + n ? buffer[9+n] : 0;
 931                        len = 10;
 932                }
 933
 934                if (buflen < len + n + p) {
 935                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 936                                "interface %d INPUT_TERMINAL error\n",
 937                                udev->devnum, alts->desc.bInterfaceNumber);
 938                        return -EINVAL;
 939                }
 940
 941                term = kzalloc(sizeof *term + n + p, GFP_KERNEL);
 942                if (term == NULL)
 943                        return -ENOMEM;
 944
 945                term->id = buffer[3];
 946                term->type = type | UVC_TERM_INPUT;
 947
 948                if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA) {
 949                        term->camera.bControlSize = n;
 950                        term->camera.bmControls = (__u8 *)term + sizeof *term;
 951                        term->camera.wObjectiveFocalLengthMin =
 952                                get_unaligned_le16(&buffer[8]);
 953                        term->camera.wObjectiveFocalLengthMax =
 954                                get_unaligned_le16(&buffer[10]);
 955                        term->camera.wOcularFocalLength =
 956                                get_unaligned_le16(&buffer[12]);
 957                        memcpy(term->camera.bmControls, &buffer[15], n);
 958                } else if (UVC_ENTITY_TYPE(term) ==
 959                           UVC_ITT_MEDIA_TRANSPORT_INPUT) {
 960                        term->media.bControlSize = n;
 961                        term->media.bmControls = (__u8 *)term + sizeof *term;
 962                        term->media.bTransportModeSize = p;
 963                        term->media.bmTransportModes = (__u8 *)term
 964                                                     + sizeof *term + n;
 965                        memcpy(term->media.bmControls, &buffer[9], n);
 966                        memcpy(term->media.bmTransportModes, &buffer[10+n], p);
 967                }
 968
 969                if (buffer[7] != 0)
 970                        usb_string(udev, buffer[7], term->name,
 971                                   sizeof term->name);
 972                else if (UVC_ENTITY_TYPE(term) == UVC_ITT_CAMERA)
 973                        sprintf(term->name, "Camera %u", buffer[3]);
 974                else if (UVC_ENTITY_TYPE(term) == UVC_ITT_MEDIA_TRANSPORT_INPUT)
 975                        sprintf(term->name, "Media %u", buffer[3]);
 976                else
 977                        sprintf(term->name, "Input %u", buffer[3]);
 978
 979                list_add_tail(&term->list, &dev->entities);
 980                break;
 981
 982        case UVC_VC_OUTPUT_TERMINAL:
 983                if (buflen < 9) {
 984                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 985                                "interface %d OUTPUT_TERMINAL error\n",
 986                                udev->devnum, alts->desc.bInterfaceNumber);
 987                        return -EINVAL;
 988                }
 989
 990                /* Make sure the terminal type MSB is not null, otherwise it
 991                 * could be confused with a unit.
 992                 */
 993                type = get_unaligned_le16(&buffer[4]);
 994                if ((type & 0xff00) == 0) {
 995                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
 996                                "interface %d OUTPUT_TERMINAL %d has invalid "
 997                                "type 0x%04x, skipping\n", udev->devnum,
 998                                alts->desc.bInterfaceNumber, buffer[3], type);
 999                        return 0;
1000                }
1001
1002                term = kzalloc(sizeof *term, GFP_KERNEL);
1003                if (term == NULL)
1004                        return -ENOMEM;
1005
1006                term->id = buffer[3];
1007                term->type = type | UVC_TERM_OUTPUT;
1008                term->output.bSourceID = buffer[7];
1009
1010                if (buffer[8] != 0)
1011                        usb_string(udev, buffer[8], term->name,
1012                                   sizeof term->name);
1013                else
1014                        sprintf(term->name, "Output %u", buffer[3]);
1015
1016                list_add_tail(&term->list, &dev->entities);
1017                break;
1018
1019        case UVC_VC_SELECTOR_UNIT:
1020                p = buflen >= 5 ? buffer[4] : 0;
1021
1022                if (buflen < 5 || buflen < 6 + p) {
1023                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1024                                "interface %d SELECTOR_UNIT error\n",
1025                                udev->devnum, alts->desc.bInterfaceNumber);
1026                        return -EINVAL;
1027                }
1028
1029                unit = kzalloc(sizeof *unit + p, GFP_KERNEL);
1030                if (unit == NULL)
1031                        return -ENOMEM;
1032
1033                unit->id = buffer[3];
1034                unit->type = buffer[2];
1035                unit->selector.bNrInPins = buffer[4];
1036                unit->selector.baSourceID = (__u8 *)unit + sizeof *unit;
1037                memcpy(unit->selector.baSourceID, &buffer[5], p);
1038
1039                if (buffer[5+p] != 0)
1040                        usb_string(udev, buffer[5+p], unit->name,
1041                                   sizeof unit->name);
1042                else
1043                        sprintf(unit->name, "Selector %u", buffer[3]);
1044
1045                list_add_tail(&unit->list, &dev->entities);
1046                break;
1047
1048        case UVC_VC_PROCESSING_UNIT:
1049                n = buflen >= 8 ? buffer[7] : 0;
1050                p = dev->uvc_version >= 0x0110 ? 10 : 9;
1051
1052                if (buflen < p + n) {
1053                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1054                                "interface %d PROCESSING_UNIT error\n",
1055                                udev->devnum, alts->desc.bInterfaceNumber);
1056                        return -EINVAL;
1057                }
1058
1059                unit = kzalloc(sizeof *unit + n, GFP_KERNEL);
1060                if (unit == NULL)
1061                        return -ENOMEM;
1062
1063                unit->id = buffer[3];
1064                unit->type = buffer[2];
1065                unit->processing.bSourceID = buffer[4];
1066                unit->processing.wMaxMultiplier =
1067                        get_unaligned_le16(&buffer[5]);
1068                unit->processing.bControlSize = buffer[7];
1069                unit->processing.bmControls = (__u8 *)unit + sizeof *unit;
1070                memcpy(unit->processing.bmControls, &buffer[8], n);
1071                if (dev->uvc_version >= 0x0110)
1072                        unit->processing.bmVideoStandards = buffer[9+n];
1073
1074                if (buffer[8+n] != 0)
1075                        usb_string(udev, buffer[8+n], unit->name,
1076                                   sizeof unit->name);
1077                else
1078                        sprintf(unit->name, "Processing %u", buffer[3]);
1079
1080                list_add_tail(&unit->list, &dev->entities);
1081                break;
1082
1083        case UVC_VC_EXTENSION_UNIT:
1084                p = buflen >= 22 ? buffer[21] : 0;
1085                n = buflen >= 24 + p ? buffer[22+p] : 0;
1086
1087                if (buflen < 24 + p + n) {
1088                        uvc_trace(UVC_TRACE_DESCR, "device %d videocontrol "
1089                                "interface %d EXTENSION_UNIT error\n",
1090                                udev->devnum, alts->desc.bInterfaceNumber);
1091                        return -EINVAL;
1092                }
1093
1094                unit = kzalloc(sizeof *unit + p + n, GFP_KERNEL);
1095                if (unit == NULL)
1096                        return -ENOMEM;
1097
1098                unit->id = buffer[3];
1099                unit->type = buffer[2];
1100                memcpy(unit->extension.guidExtensionCode, &buffer[4], 16);
1101                unit->extension.bNumControls = buffer[20];
1102                unit->extension.bNrInPins = get_unaligned_le16(&buffer[21]);
1103                unit->extension.baSourceID = (__u8 *)unit + sizeof *unit;
1104                memcpy(unit->extension.baSourceID, &buffer[22], p);
1105                unit->extension.bControlSize = buffer[22+p];
1106                unit->extension.bmControls = (__u8 *)unit + sizeof *unit + p;
1107                memcpy(unit->extension.bmControls, &buffer[23+p], n);
1108
1109                if (buffer[23+p+n] != 0)
1110                        usb_string(udev, buffer[23+p+n], unit->name,
1111                                   sizeof unit->name);
1112                else
1113                        sprintf(unit->name, "Extension %u", buffer[3]);
1114
1115                list_add_tail(&unit->list, &dev->entities);
1116                break;
1117
1118        default:
1119                uvc_trace(UVC_TRACE_DESCR, "Found an unknown CS_INTERFACE "
1120                        "descriptor (%u)\n", buffer[2]);
1121                break;
1122        }
1123
1124        return 0;
1125}
1126
1127static int uvc_parse_control(struct uvc_device *dev)
1128{
1129        struct usb_host_interface *alts = dev->intf->cur_altsetting;
1130        unsigned char *buffer = alts->extra;
1131        int buflen = alts->extralen;
1132        int ret;
1133
1134        /* Parse the default alternate setting only, as the UVC specification
1135         * defines a single alternate setting, the default alternate setting
1136         * zero.
1137         */
1138
1139        while (buflen > 2) {
1140                if (uvc_parse_vendor_control(dev, buffer, buflen) ||
1141                    buffer[1] != USB_DT_CS_INTERFACE)
1142                        goto next_descriptor;
1143
1144                if ((ret = uvc_parse_standard_control(dev, buffer, buflen)) < 0)
1145                        return ret;
1146
1147next_descriptor:
1148                buflen -= buffer[0];
1149                buffer += buffer[0];
1150        }
1151
1152        /* Check if the optional status endpoint is present. Built-in iSight
1153         * webcams have an interrupt endpoint but spit proprietary data that
1154         * don't conform to the UVC status endpoint messages. Don't try to
1155         * handle the interrupt endpoint for those cameras.
1156         */
1157        if (alts->desc.bNumEndpoints == 1 &&
1158            !(dev->quirks & UVC_QUIRK_BUILTIN_ISIGHT)) {
1159                struct usb_host_endpoint *ep = &alts->endpoint[0];
1160                struct usb_endpoint_descriptor *desc = &ep->desc;
1161
1162                if (usb_endpoint_is_int_in(desc) &&
1163                    le16_to_cpu(desc->wMaxPacketSize) >= 8 &&
1164                    desc->bInterval != 0) {
1165                        uvc_trace(UVC_TRACE_DESCR, "Found a Status endpoint "
1166                                "(addr %02x).\n", desc->bEndpointAddress);
1167                        dev->int_ep = ep;
1168                }
1169        }
1170
1171        return 0;
1172}
1173
1174/* ------------------------------------------------------------------------
1175 * UVC device scan
1176 */
1177
1178/*
1179 * Scan the UVC descriptors to locate a chain starting at an Output Terminal
1180 * and containing the following units:
1181 *
1182 * - one or more Output Terminals (USB Streaming or Display)
1183 * - zero or one Processing Unit
1184 * - zero, one or more single-input Selector Units
1185 * - zero or one multiple-input Selector Units, provided all inputs are
1186 *   connected to input terminals
1187 * - zero, one or mode single-input Extension Units
1188 * - one or more Input Terminals (Camera, External or USB Streaming)
1189 *
1190 * The terminal and units must match on of the following structures:
1191 *
1192 * ITT_*(0) -> +---------+    +---------+    +---------+ -> TT_STREAMING(0)
1193 * ...         | SU{0,1} | -> | PU{0,1} | -> | XU{0,n} |    ...
1194 * ITT_*(n) -> +---------+    +---------+    +---------+ -> TT_STREAMING(n)
1195 *
1196 *                 +---------+    +---------+ -> OTT_*(0)
1197 * TT_STREAMING -> | PU{0,1} | -> | XU{0,n} |    ...
1198 *                 +---------+    +---------+ -> OTT_*(n)
1199 *
1200 * The Processing Unit and Extension Units can be in any order. Additional
1201 * Extension Units connected to the main chain as single-unit branches are
1202 * also supported. Single-input Selector Units are ignored.
1203 */
1204static int uvc_scan_chain_entity(struct uvc_video_chain *chain,
1205        struct uvc_entity *entity)
1206{
1207        switch (UVC_ENTITY_TYPE(entity)) {
1208        case UVC_VC_EXTENSION_UNIT:
1209                if (uvc_trace_param & UVC_TRACE_PROBE)
1210                        printk(" <- XU %d", entity->id);
1211
1212                if (entity->extension.bNrInPins != 1) {
1213                        uvc_trace(UVC_TRACE_DESCR, "Extension unit %d has more "
1214                                "than 1 input pin.\n", entity->id);
1215                        return -1;
1216                }
1217
1218                list_add_tail(&entity->chain, &chain->extensions);
1219                break;
1220
1221        case UVC_VC_PROCESSING_UNIT:
1222                if (uvc_trace_param & UVC_TRACE_PROBE)
1223                        printk(" <- PU %d", entity->id);
1224
1225                if (chain->processing != NULL) {
1226                        uvc_trace(UVC_TRACE_DESCR, "Found multiple "
1227                                "Processing Units in chain.\n");
1228                        return -1;
1229                }
1230
1231                chain->processing = entity;
1232                break;
1233
1234        case UVC_VC_SELECTOR_UNIT:
1235                if (uvc_trace_param & UVC_TRACE_PROBE)
1236                        printk(" <- SU %d", entity->id);
1237
1238                /* Single-input selector units are ignored. */
1239                if (entity->selector.bNrInPins == 1)
1240                        break;
1241
1242                if (chain->selector != NULL) {
1243                        uvc_trace(UVC_TRACE_DESCR, "Found multiple Selector "
1244                                "Units in chain.\n");
1245                        return -1;
1246                }
1247
1248                chain->selector = entity;
1249                break;
1250
1251        case UVC_ITT_VENDOR_SPECIFIC:
1252        case UVC_ITT_CAMERA:
1253        case UVC_ITT_MEDIA_TRANSPORT_INPUT:
1254                if (uvc_trace_param & UVC_TRACE_PROBE)
1255                        printk(" <- IT %d\n", entity->id);
1256
1257                list_add_tail(&entity->chain, &chain->iterms);
1258                break;
1259
1260        case UVC_TT_STREAMING:
1261                if (uvc_trace_param & UVC_TRACE_PROBE)
1262                        printk(" <- IT %d\n", entity->id);
1263
1264                if (!UVC_ENTITY_IS_ITERM(entity)) {
1265                        uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1266                                "terminal %u.\n", entity->id);
1267                        return -1;
1268                }
1269
1270                list_add_tail(&entity->chain, &chain->iterms);
1271                break;
1272
1273        default:
1274                uvc_trace(UVC_TRACE_DESCR, "Unsupported entity type "
1275                        "0x%04x found in chain.\n", UVC_ENTITY_TYPE(entity));
1276                return -1;
1277        }
1278
1279        return 0;
1280}
1281
1282static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
1283        struct uvc_entity *entity, struct uvc_entity *prev)
1284{
1285        struct uvc_entity *forward;
1286        int found;
1287
1288        /* Forward scan */
1289        forward = NULL;
1290        found = 0;
1291
1292        while (1) {
1293                forward = uvc_entity_by_reference(chain->dev, entity->id,
1294                        forward);
1295                if (forward == NULL)
1296                        break;
1297                if (forward == prev)
1298                        continue;
1299
1300                switch (UVC_ENTITY_TYPE(forward)) {
1301                case UVC_VC_EXTENSION_UNIT:
1302                        if (forward->extension.bNrInPins != 1) {
1303                                uvc_trace(UVC_TRACE_DESCR, "Extension unit %d "
1304                                          "has more than 1 input pin.\n",
1305                                          entity->id);
1306                                return -EINVAL;
1307                        }
1308
1309                        list_add_tail(&forward->chain, &chain->extensions);
1310                        if (uvc_trace_param & UVC_TRACE_PROBE) {
1311                                if (!found)
1312                                        printk(" (->");
1313
1314                                printk(" XU %d", forward->id);
1315                                found = 1;
1316                        }
1317                        break;
1318
1319                case UVC_OTT_VENDOR_SPECIFIC:
1320                case UVC_OTT_DISPLAY:
1321                case UVC_OTT_MEDIA_TRANSPORT_OUTPUT:
1322                case UVC_TT_STREAMING:
1323                        if (UVC_ENTITY_IS_ITERM(forward)) {
1324                                uvc_trace(UVC_TRACE_DESCR, "Unsupported input "
1325                                        "terminal %u.\n", forward->id);
1326                                return -EINVAL;
1327                        }
1328
1329                        list_add_tail(&forward->chain, &chain->oterms);
1330                        if (uvc_trace_param & UVC_TRACE_PROBE) {
1331                                if (!found)
1332                                        printk(" (->");
1333
1334                                printk(" OT %d", forward->id);
1335                                found = 1;
1336                        }
1337                        break;
1338                }
1339        }
1340        if (found)
1341                printk(")");
1342
1343        return 0;
1344}
1345
1346static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
1347        struct uvc_entity *entity)
1348{
1349        struct uvc_entity *term;
1350        int id = -1, i;
1351
1352        switch (UVC_ENTITY_TYPE(entity)) {
1353        case UVC_VC_EXTENSION_UNIT:
1354                id = entity->extension.baSourceID[0];
1355                break;
1356
1357        case UVC_VC_PROCESSING_UNIT:
1358                id = entity->processing.bSourceID;
1359                break;
1360
1361        case UVC_VC_SELECTOR_UNIT:
1362                /* Single-input selector units are ignored. */
1363                if (entity->selector.bNrInPins == 1) {
1364                        id = entity->selector.baSourceID[0];
1365                        break;
1366                }
1367
1368                if (uvc_trace_param & UVC_TRACE_PROBE)
1369                        printk(" <- IT");
1370
1371                chain->selector = entity;
1372                for (i = 0; i < entity->selector.bNrInPins; ++i) {
1373                        id = entity->selector.baSourceID[i];
1374                        term = uvc_entity_by_id(chain->dev, id);
1375                        if (term == NULL || !UVC_ENTITY_IS_ITERM(term)) {
1376                                uvc_trace(UVC_TRACE_DESCR, "Selector unit %d "
1377                                        "input %d isn't connected to an "
1378                                        "input terminal\n", entity->id, i);
1379                                return -1;
1380                        }
1381
1382                        if (uvc_trace_param & UVC_TRACE_PROBE)
1383                                printk(" %d", term->id);
1384
1385                        list_add_tail(&term->chain, &chain->iterms);
1386                        uvc_scan_chain_forward(chain, term, entity);
1387                }
1388
1389                if (uvc_trace_param & UVC_TRACE_PROBE)
1390                        printk("\n");
1391
1392                id = 0;
1393                break;
1394        }
1395
1396        return id;
1397}
1398
1399static int uvc_scan_chain(struct uvc_video_chain *chain,
1400                          struct uvc_entity *oterm)
1401{
1402        struct uvc_entity *entity, *prev;
1403        int id;
1404
1405        entity = oterm;
1406        list_add_tail(&entity->chain, &chain->oterms);
1407        uvc_trace(UVC_TRACE_PROBE, "Scanning UVC chain: OT %d", entity->id);
1408
1409        id = entity->output.bSourceID;
1410        while (id != 0) {
1411                prev = entity;
1412                entity = uvc_entity_by_id(chain->dev, id);
1413                if (entity == NULL) {
1414                        uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1415                                "unknown entity %d.\n", id);
1416                        return -EINVAL;
1417                }
1418
1419                if (entity->chain.next || entity->chain.prev) {
1420                        uvc_trace(UVC_TRACE_DESCR, "Found reference to "
1421                                "entity %d already in chain.\n", id);
1422                        return -EINVAL;
1423                }
1424
1425                /* Process entity */
1426                if (uvc_scan_chain_entity(chain, entity) < 0)
1427                        return -EINVAL;
1428
1429                /* Forward scan */
1430                if (uvc_scan_chain_forward(chain, entity, prev) < 0)
1431                        return -EINVAL;
1432
1433                /* Stop when a terminal is found. */
1434                if (UVC_ENTITY_IS_TERM(entity))
1435                        break;
1436
1437                /* Backward scan */
1438                id = uvc_scan_chain_backward(chain, entity);
1439                if (id < 0)
1440                        return id;
1441        }
1442
1443        return 0;
1444}
1445
1446static unsigned int uvc_print_terms(struct list_head *terms, char *buffer)
1447{
1448        struct uvc_entity *term;
1449        unsigned int nterms = 0;
1450        char *p = buffer;
1451
1452        list_for_each_entry(term, terms, chain) {
1453                p += sprintf(p, "%u", term->id);
1454                if (term->chain.next != terms) {
1455                        p += sprintf(p, ",");
1456                        if (++nterms >= 4) {
1457                                p += sprintf(p, "...");
1458                                break;
1459                        }
1460                }
1461        }
1462
1463        return p - buffer;
1464}
1465
1466static const char *uvc_print_chain(struct uvc_video_chain *chain)
1467{
1468        static char buffer[43];
1469        char *p = buffer;
1470
1471        p += uvc_print_terms(&chain->iterms, p);
1472        p += sprintf(p, " -> ");
1473        uvc_print_terms(&chain->oterms, p);
1474
1475        return buffer;
1476}
1477
1478/*
1479 * Scan the device for video chains and register video devices.
1480 *
1481 * Chains are scanned starting at their output terminals and walked backwards.
1482 */
1483static int uvc_scan_device(struct uvc_device *dev)
1484{
1485        struct uvc_video_chain *chain;
1486        struct uvc_entity *term;
1487
1488        list_for_each_entry(term, &dev->entities, list) {
1489                if (!UVC_ENTITY_IS_OTERM(term))
1490                        continue;
1491
1492                /* If the terminal is already included in a chain, skip it.
1493                 * This can happen for chains that have multiple output
1494                 * terminals, where all output terminals beside the first one
1495                 * will be inserted in the chain in forward scans.
1496                 */
1497                if (term->chain.next || term->chain.prev)
1498                        continue;
1499
1500                chain = kzalloc(sizeof(*chain), GFP_KERNEL);
1501                if (chain == NULL)
1502                        return -ENOMEM;
1503
1504                INIT_LIST_HEAD(&chain->iterms);
1505                INIT_LIST_HEAD(&chain->oterms);
1506                INIT_LIST_HEAD(&chain->extensions);
1507                mutex_init(&chain->ctrl_mutex);
1508                chain->dev = dev;
1509
1510                if (uvc_scan_chain(chain, term) < 0) {
1511                        kfree(chain);
1512                        continue;
1513                }
1514
1515                uvc_trace(UVC_TRACE_PROBE, "Found a valid video chain (%s).\n",
1516                          uvc_print_chain(chain));
1517
1518                list_add_tail(&chain->list, &dev->chains);
1519        }
1520
1521        if (list_empty(&dev->chains)) {
1522                uvc_printk(KERN_INFO, "No valid video chain found.\n");
1523                return -1;
1524        }
1525
1526        return 0;
1527}
1528
1529/* ------------------------------------------------------------------------
1530 * Video device registration and unregistration
1531 */
1532
1533/*
1534 * Unregister the video devices.
1535 */
1536static void uvc_unregister_video(struct uvc_device *dev)
1537{
1538        struct uvc_streaming *stream;
1539
1540        list_for_each_entry(stream, &dev->streams, list) {
1541                if (stream->vdev == NULL)
1542                        continue;
1543
1544                if (stream->vdev->minor == -1)
1545                        video_device_release(stream->vdev);
1546                else
1547                        video_unregister_device(stream->vdev);
1548                stream->vdev = NULL;
1549        }
1550}
1551
1552static int uvc_register_video(struct uvc_device *dev,
1553                struct uvc_streaming *stream)
1554{
1555        struct video_device *vdev;
1556        int ret;
1557
1558        /* Initialize the streaming interface with default streaming
1559         * parameters.
1560         */
1561        ret = uvc_video_init(stream);
1562        if (ret < 0) {
1563                uvc_printk(KERN_ERR, "Failed to initialize the device "
1564                        "(%d).\n", ret);
1565                return ret;
1566        }
1567
1568        /* Register the device with V4L. */
1569        vdev = video_device_alloc();
1570        if (vdev == NULL) {
1571                uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n",
1572                           ret);
1573                return -ENOMEM;
1574        }
1575
1576        /* We already hold a reference to dev->udev. The video device will be
1577         * unregistered before the reference is released, so we don't need to
1578         * get another one.
1579         */
1580        vdev->parent = &dev->intf->dev;
1581        vdev->minor = -1;
1582        vdev->fops = &uvc_fops;
1583        vdev->release = video_device_release;
1584        strlcpy(vdev->name, dev->name, sizeof vdev->name);
1585
1586        /* Set the driver data before calling video_register_device, otherwise
1587         * uvc_v4l2_open might race us.
1588         */
1589        stream->vdev = vdev;
1590        video_set_drvdata(vdev, stream);
1591
1592        ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1593        if (ret < 0) {
1594                uvc_printk(KERN_ERR, "Failed to register video device (%d).\n",
1595                           ret);
1596                stream->vdev = NULL;
1597                video_device_release(vdev);
1598                return ret;
1599        }
1600
1601        return 0;
1602}
1603
1604/*
1605 * Register all video devices in all chains.
1606 */
1607static int uvc_register_terms(struct uvc_device *dev,
1608        struct uvc_video_chain *chain, struct list_head *terms)
1609{
1610        struct uvc_streaming *stream;
1611        struct uvc_entity *term;
1612        int ret;
1613
1614        list_for_each_entry(term, terms, chain) {
1615                if (UVC_ENTITY_TYPE(term) != UVC_TT_STREAMING)
1616                        continue;
1617
1618                stream = uvc_stream_by_id(dev, term->id);
1619                if (stream == NULL) {
1620                        uvc_printk(KERN_INFO, "No streaming interface found "
1621                                   "for terminal %u.", term->id);
1622                        continue;
1623                }
1624
1625                stream->chain = chain;
1626                ret = uvc_register_video(dev, stream);
1627                if (ret < 0)
1628                        return ret;
1629        }
1630
1631        return 0;
1632}
1633
1634static int uvc_register_chains(struct uvc_device *dev)
1635{
1636        struct uvc_video_chain *chain;
1637        int ret;
1638
1639        list_for_each_entry(chain, &dev->chains, list) {
1640                ret = uvc_register_terms(dev, chain, &chain->iterms);
1641                if (ret < 0)
1642                        return ret;
1643
1644                ret = uvc_register_terms(dev, chain, &chain->oterms);
1645                if (ret < 0)
1646                        return ret;
1647        }
1648
1649        return 0;
1650}
1651
1652/* ------------------------------------------------------------------------
1653 * USB probe, disconnect, suspend and resume
1654 */
1655
1656/*
1657 * Delete the UVC device.
1658 *
1659 * Called by the kernel when the last reference to the uvc_device structure
1660 * is released.
1661 *
1662 * Unregistering the video devices is done here because every opened instance
1663 * must be closed before the device can be unregistered. An alternative would
1664 * have been to use another reference count for uvc_v4l2_open/uvc_release, and
1665 * unregister the video devices on disconnect when that reference count drops
1666 * to zero.
1667 *
1668 * As this function is called after or during disconnect(), all URBs have
1669 * already been canceled by the USB core. There is no need to kill the
1670 * interrupt URB manually.
1671 */
1672void uvc_delete(struct kref *kref)
1673{
1674        struct uvc_device *dev = container_of(kref, struct uvc_device, kref);
1675        struct list_head *p, *n;
1676
1677        /* Unregister the video devices. */
1678        uvc_unregister_video(dev);
1679        usb_put_intf(dev->intf);
1680        usb_put_dev(dev->udev);
1681
1682        uvc_status_cleanup(dev);
1683        uvc_ctrl_cleanup_device(dev);
1684
1685        list_for_each_safe(p, n, &dev->chains) {
1686                struct uvc_video_chain *chain;
1687                chain = list_entry(p, struct uvc_video_chain, list);
1688                kfree(chain);
1689        }
1690
1691        list_for_each_safe(p, n, &dev->entities) {
1692                struct uvc_entity *entity;
1693                entity = list_entry(p, struct uvc_entity, list);
1694                kfree(entity);
1695        }
1696
1697        list_for_each_safe(p, n, &dev->streams) {
1698                struct uvc_streaming *streaming;
1699                streaming = list_entry(p, struct uvc_streaming, list);
1700                usb_driver_release_interface(&uvc_driver.driver,
1701                        streaming->intf);
1702                usb_put_intf(streaming->intf);
1703                kfree(streaming->format);
1704                kfree(streaming->header.bmaControls);
1705                kfree(streaming);
1706        }
1707
1708        kfree(dev);
1709}
1710
1711static int uvc_probe(struct usb_interface *intf,
1712                     const struct usb_device_id *id)
1713{
1714        struct usb_device *udev = interface_to_usbdev(intf);
1715        struct uvc_device *dev;
1716        int ret;
1717
1718        if (id->idVendor && id->idProduct)
1719                uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s "
1720                                "(%04x:%04x)\n", udev->devpath, id->idVendor,
1721                                id->idProduct);
1722        else
1723                uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",
1724                                udev->devpath);
1725
1726        /* Allocate memory for the device and initialize it. */
1727        if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL)
1728                return -ENOMEM;
1729
1730        INIT_LIST_HEAD(&dev->entities);
1731        INIT_LIST_HEAD(&dev->chains);
1732        INIT_LIST_HEAD(&dev->streams);
1733        kref_init(&dev->kref);
1734        atomic_set(&dev->users, 0);
1735
1736        dev->udev = usb_get_dev(udev);
1737        dev->intf = usb_get_intf(intf);
1738        dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber;
1739        dev->quirks = id->driver_info | uvc_quirks_param;
1740
1741        if (udev->product != NULL)
1742                strlcpy(dev->name, udev->product, sizeof dev->name);
1743        else
1744                snprintf(dev->name, sizeof dev->name,
1745                        "UVC Camera (%04x:%04x)",
1746                        le16_to_cpu(udev->descriptor.idVendor),
1747                        le16_to_cpu(udev->descriptor.idProduct));
1748
1749        /* Parse the Video Class control descriptor. */
1750        if (uvc_parse_control(dev) < 0) {
1751                uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC "
1752                        "descriptors.\n");
1753                goto error;
1754        }
1755
1756        uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",
1757                dev->uvc_version >> 8, dev->uvc_version & 0xff,
1758                udev->product ? udev->product : "<unnamed>",
1759                le16_to_cpu(udev->descriptor.idVendor),
1760                le16_to_cpu(udev->descriptor.idProduct));
1761
1762        if (uvc_quirks_param != 0) {
1763                uvc_printk(KERN_INFO, "Forcing device quirks 0x%x by module "
1764                        "parameter for testing purpose.\n", uvc_quirks_param);
1765                uvc_printk(KERN_INFO, "Please report required quirks to the "
1766                        "linux-uvc-devel mailing list.\n");
1767        }
1768
1769        /* Initialize controls. */
1770        if (uvc_ctrl_init_device(dev) < 0)
1771                goto error;
1772
1773        /* Scan the device for video chains. */
1774        if (uvc_scan_device(dev) < 0)
1775                goto error;
1776
1777        /* Register video devices. */
1778        if (uvc_register_chains(dev) < 0)
1779                goto error;
1780
1781        /* Save our data pointer in the interface data. */
1782        usb_set_intfdata(intf, dev);
1783
1784        /* Initialize the interrupt URB. */
1785        if ((ret = uvc_status_init(dev)) < 0) {
1786                uvc_printk(KERN_INFO, "Unable to initialize the status "
1787                        "endpoint (%d), status interrupt will not be "
1788                        "supported.\n", ret);
1789        }
1790
1791        uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n");
1792        return 0;
1793
1794error:
1795        kref_put(&dev->kref, uvc_delete);
1796        return -ENODEV;
1797}
1798
1799static void uvc_disconnect(struct usb_interface *intf)
1800{
1801        struct uvc_device *dev = usb_get_intfdata(intf);
1802
1803        /* Set the USB interface data to NULL. This can be done outside the
1804         * lock, as there's no other reader.
1805         */
1806        usb_set_intfdata(intf, NULL);
1807
1808        if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1809            UVC_SC_VIDEOSTREAMING)
1810                return;
1811
1812        /* uvc_v4l2_open() might race uvc_disconnect(). A static driver-wide
1813         * lock is needed to prevent uvc_disconnect from releasing its
1814         * reference to the uvc_device instance after uvc_v4l2_open() received
1815         * the pointer to the device (video_devdata) but before it got the
1816         * chance to increase the reference count (kref_get).
1817         *
1818         * Note that the reference can't be released with the lock held,
1819         * otherwise a AB-BA deadlock can occur with videodev_lock that
1820         * videodev acquires in videodev_open() and video_unregister_device().
1821         */
1822        mutex_lock(&uvc_driver.open_mutex);
1823        dev->state |= UVC_DEV_DISCONNECTED;
1824        mutex_unlock(&uvc_driver.open_mutex);
1825
1826        kref_put(&dev->kref, uvc_delete);
1827}
1828
1829static int uvc_suspend(struct usb_interface *intf, pm_message_t message)
1830{
1831        struct uvc_device *dev = usb_get_intfdata(intf);
1832        struct uvc_streaming *stream;
1833
1834        uvc_trace(UVC_TRACE_SUSPEND, "Suspending interface %u\n",
1835                intf->cur_altsetting->desc.bInterfaceNumber);
1836
1837        /* Controls are cached on the fly so they don't need to be saved. */
1838        if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1839            UVC_SC_VIDEOCONTROL)
1840                return uvc_status_suspend(dev);
1841
1842        list_for_each_entry(stream, &dev->streams, list) {
1843                if (stream->intf == intf)
1844                        return uvc_video_suspend(stream);
1845        }
1846
1847        uvc_trace(UVC_TRACE_SUSPEND, "Suspend: video streaming USB interface "
1848                        "mismatch.\n");
1849        return -EINVAL;
1850}
1851
1852static int __uvc_resume(struct usb_interface *intf, int reset)
1853{
1854        struct uvc_device *dev = usb_get_intfdata(intf);
1855        struct uvc_streaming *stream;
1856
1857        uvc_trace(UVC_TRACE_SUSPEND, "Resuming interface %u\n",
1858                intf->cur_altsetting->desc.bInterfaceNumber);
1859
1860        if (intf->cur_altsetting->desc.bInterfaceSubClass ==
1861            UVC_SC_VIDEOCONTROL) {
1862                if (reset) {
1863                        int ret = uvc_ctrl_resume_device(dev);
1864
1865                        if (ret < 0)
1866                                return ret;
1867                }
1868
1869                return uvc_status_resume(dev);
1870        }
1871
1872        list_for_each_entry(stream, &dev->streams, list) {
1873                if (stream->intf == intf)
1874                        return uvc_video_resume(stream);
1875        }
1876
1877        uvc_trace(UVC_TRACE_SUSPEND, "Resume: video streaming USB interface "
1878                        "mismatch.\n");
1879        return -EINVAL;
1880}
1881
1882static int uvc_resume(struct usb_interface *intf)
1883{
1884        return __uvc_resume(intf, 0);
1885}
1886
1887static int uvc_reset_resume(struct usb_interface *intf)
1888{
1889        return __uvc_resume(intf, 1);
1890}
1891
1892/* ------------------------------------------------------------------------
1893 * Driver initialization and cleanup
1894 */
1895
1896/*
1897 * The Logitech cameras listed below have their interface class set to
1898 * VENDOR_SPEC because they don't announce themselves as UVC devices, even
1899 * though they are compliant.
1900 */
1901static struct usb_device_id uvc_ids[] = {
1902        /* Microsoft Lifecam NX-6000 */
1903        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1904                                | USB_DEVICE_ID_MATCH_INT_INFO,
1905          .idVendor             = 0x045e,
1906          .idProduct            = 0x00f8,
1907          .bInterfaceClass      = USB_CLASS_VIDEO,
1908          .bInterfaceSubClass   = 1,
1909          .bInterfaceProtocol   = 0,
1910          .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1911        /* Microsoft Lifecam VX-7000 */
1912        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1913                                | USB_DEVICE_ID_MATCH_INT_INFO,
1914          .idVendor             = 0x045e,
1915          .idProduct            = 0x0723,
1916          .bInterfaceClass      = USB_CLASS_VIDEO,
1917          .bInterfaceSubClass   = 1,
1918          .bInterfaceProtocol   = 0,
1919          .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1920        /* Logitech Quickcam Fusion */
1921        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1922                                | USB_DEVICE_ID_MATCH_INT_INFO,
1923          .idVendor             = 0x046d,
1924          .idProduct            = 0x08c1,
1925          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1926          .bInterfaceSubClass   = 1,
1927          .bInterfaceProtocol   = 0 },
1928        /* Logitech Quickcam Orbit MP */
1929        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1930                                | USB_DEVICE_ID_MATCH_INT_INFO,
1931          .idVendor             = 0x046d,
1932          .idProduct            = 0x08c2,
1933          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1934          .bInterfaceSubClass   = 1,
1935          .bInterfaceProtocol   = 0 },
1936        /* Logitech Quickcam Pro for Notebook */
1937        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1938                                | USB_DEVICE_ID_MATCH_INT_INFO,
1939          .idVendor             = 0x046d,
1940          .idProduct            = 0x08c3,
1941          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1942          .bInterfaceSubClass   = 1,
1943          .bInterfaceProtocol   = 0 },
1944        /* Logitech Quickcam Pro 5000 */
1945        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1946                                | USB_DEVICE_ID_MATCH_INT_INFO,
1947          .idVendor             = 0x046d,
1948          .idProduct            = 0x08c5,
1949          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1950          .bInterfaceSubClass   = 1,
1951          .bInterfaceProtocol   = 0 },
1952        /* Logitech Quickcam OEM Dell Notebook */
1953        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1954                                | USB_DEVICE_ID_MATCH_INT_INFO,
1955          .idVendor             = 0x046d,
1956          .idProduct            = 0x08c6,
1957          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1958          .bInterfaceSubClass   = 1,
1959          .bInterfaceProtocol   = 0 },
1960        /* Logitech Quickcam OEM Cisco VT Camera II */
1961        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1962                                | USB_DEVICE_ID_MATCH_INT_INFO,
1963          .idVendor             = 0x046d,
1964          .idProduct            = 0x08c7,
1965          .bInterfaceClass      = USB_CLASS_VENDOR_SPEC,
1966          .bInterfaceSubClass   = 1,
1967          .bInterfaceProtocol   = 0 },
1968        /* Alcor Micro AU3820 (Future Boy PC USB Webcam) */
1969        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1970                                | USB_DEVICE_ID_MATCH_INT_INFO,
1971          .idVendor             = 0x058f,
1972          .idProduct            = 0x3820,
1973          .bInterfaceClass      = USB_CLASS_VIDEO,
1974          .bInterfaceSubClass   = 1,
1975          .bInterfaceProtocol   = 0,
1976          .driver_info          = UVC_QUIRK_PROBE_MINMAX },
1977        /* Apple Built-In iSight */
1978        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1979                                | USB_DEVICE_ID_MATCH_INT_INFO,
1980          .idVendor             = 0x05ac,
1981          .idProduct            = 0x8501,
1982          .bInterfaceClass      = USB_CLASS_VIDEO,
1983          .bInterfaceSubClass   = 1,
1984          .bInterfaceProtocol   = 0,
1985          .driver_info          = UVC_QUIRK_PROBE_MINMAX
1986                                | UVC_QUIRK_BUILTIN_ISIGHT },
1987        /* Genesys Logic USB 2.0 PC Camera */
1988        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1989                                | USB_DEVICE_ID_MATCH_INT_INFO,
1990          .idVendor             = 0x05e3,
1991          .idProduct            = 0x0505,
1992          .bInterfaceClass      = USB_CLASS_VIDEO,
1993          .bInterfaceSubClass   = 1,
1994          .bInterfaceProtocol   = 0,
1995          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
1996        /* ViMicro Vega */
1997        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
1998                                | USB_DEVICE_ID_MATCH_INT_INFO,
1999          .idVendor             = 0x0ac8,
2000          .idProduct            = 0x332d,
2001          .bInterfaceClass      = USB_CLASS_VIDEO,
2002          .bInterfaceSubClass   = 1,
2003          .bInterfaceProtocol   = 0,
2004          .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2005        /* ViMicro - Minoru3D */
2006        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2007                                | USB_DEVICE_ID_MATCH_INT_INFO,
2008          .idVendor             = 0x0ac8,
2009          .idProduct            = 0x3410,
2010          .bInterfaceClass      = USB_CLASS_VIDEO,
2011          .bInterfaceSubClass   = 1,
2012          .bInterfaceProtocol   = 0,
2013          .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2014        /* ViMicro Venus - Minoru3D */
2015        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2016                                | USB_DEVICE_ID_MATCH_INT_INFO,
2017          .idVendor             = 0x0ac8,
2018          .idProduct            = 0x3420,
2019          .bInterfaceClass      = USB_CLASS_VIDEO,
2020          .bInterfaceSubClass   = 1,
2021          .bInterfaceProtocol   = 0,
2022          .driver_info          = UVC_QUIRK_FIX_BANDWIDTH },
2023        /* MT6227 */
2024        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2025                                | USB_DEVICE_ID_MATCH_INT_INFO,
2026          .idVendor             = 0x0e8d,
2027          .idProduct            = 0x0004,
2028          .bInterfaceClass      = USB_CLASS_VIDEO,
2029          .bInterfaceSubClass   = 1,
2030          .bInterfaceProtocol   = 0,
2031          .driver_info          = UVC_QUIRK_PROBE_MINMAX
2032                                | UVC_QUIRK_PROBE_DEF },
2033        /* Syntek (HP Spartan) */
2034        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2035                                | USB_DEVICE_ID_MATCH_INT_INFO,
2036          .idVendor             = 0x174f,
2037          .idProduct            = 0x5212,
2038          .bInterfaceClass      = USB_CLASS_VIDEO,
2039          .bInterfaceSubClass   = 1,
2040          .bInterfaceProtocol   = 0,
2041          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2042        /* Syntek (Samsung Q310) */
2043        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2044                                | USB_DEVICE_ID_MATCH_INT_INFO,
2045          .idVendor             = 0x174f,
2046          .idProduct            = 0x5931,
2047          .bInterfaceClass      = USB_CLASS_VIDEO,
2048          .bInterfaceSubClass   = 1,
2049          .bInterfaceProtocol   = 0,
2050          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2051        /* Syntek (Asus F9SG) */
2052        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2053                                | USB_DEVICE_ID_MATCH_INT_INFO,
2054          .idVendor             = 0x174f,
2055          .idProduct            = 0x8a31,
2056          .bInterfaceClass      = USB_CLASS_VIDEO,
2057          .bInterfaceSubClass   = 1,
2058          .bInterfaceProtocol   = 0,
2059          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2060        /* Syntek (Asus U3S) */
2061        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2062                                | USB_DEVICE_ID_MATCH_INT_INFO,
2063          .idVendor             = 0x174f,
2064          .idProduct            = 0x8a33,
2065          .bInterfaceClass      = USB_CLASS_VIDEO,
2066          .bInterfaceSubClass   = 1,
2067          .bInterfaceProtocol   = 0,
2068          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2069        /* Syntek (JAOtech Smart Terminal) */
2070        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2071                                | USB_DEVICE_ID_MATCH_INT_INFO,
2072          .idVendor             = 0x174f,
2073          .idProduct            = 0x8a34,
2074          .bInterfaceClass      = USB_CLASS_VIDEO,
2075          .bInterfaceSubClass   = 1,
2076          .bInterfaceProtocol   = 0,
2077          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2078        /* Lenovo Thinkpad SL400/SL500 */
2079        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2080                                | USB_DEVICE_ID_MATCH_INT_INFO,
2081          .idVendor             = 0x17ef,
2082          .idProduct            = 0x480b,
2083          .bInterfaceClass      = USB_CLASS_VIDEO,
2084          .bInterfaceSubClass   = 1,
2085          .bInterfaceProtocol   = 0,
2086          .driver_info          = UVC_QUIRK_STREAM_NO_FID },
2087        /* Aveo Technology USB 2.0 Camera */
2088        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2089                                | USB_DEVICE_ID_MATCH_INT_INFO,
2090          .idVendor             = 0x1871,
2091          .idProduct            = 0x0306,
2092          .bInterfaceClass      = USB_CLASS_VIDEO,
2093          .bInterfaceSubClass   = 1,
2094          .bInterfaceProtocol   = 0,
2095          .driver_info          = UVC_QUIRK_PROBE_MINMAX
2096                                | UVC_QUIRK_PROBE_EXTRAFIELDS },
2097        /* Ecamm Pico iMage */
2098        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2099                                | USB_DEVICE_ID_MATCH_INT_INFO,
2100          .idVendor             = 0x18cd,
2101          .idProduct            = 0xcafe,
2102          .bInterfaceClass      = USB_CLASS_VIDEO,
2103          .bInterfaceSubClass   = 1,
2104          .bInterfaceProtocol   = 0,
2105          .driver_info          = UVC_QUIRK_PROBE_EXTRAFIELDS },
2106        /* FSC WebCam V30S */
2107        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2108                                | USB_DEVICE_ID_MATCH_INT_INFO,
2109          .idVendor             = 0x18ec,
2110          .idProduct            = 0x3288,
2111          .bInterfaceClass      = USB_CLASS_VIDEO,
2112          .bInterfaceSubClass   = 1,
2113          .bInterfaceProtocol   = 0,
2114          .driver_info          = UVC_QUIRK_PROBE_MINMAX },
2115        /* Bodelin ProScopeHR */
2116        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2117                                | USB_DEVICE_ID_MATCH_DEV_HI
2118                                | USB_DEVICE_ID_MATCH_INT_INFO,
2119          .idVendor             = 0x19ab,
2120          .idProduct            = 0x1000,
2121          .bcdDevice_hi         = 0x0126,
2122          .bInterfaceClass      = USB_CLASS_VIDEO,
2123          .bInterfaceSubClass   = 1,
2124          .bInterfaceProtocol   = 0,
2125          .driver_info          = UVC_QUIRK_STATUS_INTERVAL },
2126        /* SiGma Micro USB Web Camera */
2127        { .match_flags          = USB_DEVICE_ID_MATCH_DEVICE
2128                                | USB_DEVICE_ID_MATCH_INT_INFO,
2129          .idVendor             = 0x1c4f,
2130          .idProduct            = 0x3000,
2131          .bInterfaceClass      = USB_CLASS_VIDEO,
2132          .bInterfaceSubClass   = 1,
2133          .bInterfaceProtocol   = 0,
2134          .driver_info          = UVC_QUIRK_PROBE_MINMAX
2135                                | UVC_QUIRK_IGNORE_SELECTOR_UNIT },
2136        /* Generic USB Video Class */
2137        { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) },
2138        {}
2139};
2140
2141MODULE_DEVICE_TABLE(usb, uvc_ids);
2142
2143struct uvc_driver uvc_driver = {
2144        .driver = {
2145                .name           = "uvcvideo",
2146                .probe          = uvc_probe,
2147                .disconnect     = uvc_disconnect,
2148                .suspend        = uvc_suspend,
2149                .resume         = uvc_resume,
2150                .reset_resume   = uvc_reset_resume,
2151                .id_table       = uvc_ids,
2152                .supports_autosuspend = 1,
2153        },
2154};
2155
2156static int __init uvc_init(void)
2157{
2158        int result;
2159
2160        INIT_LIST_HEAD(&uvc_driver.devices);
2161        INIT_LIST_HEAD(&uvc_driver.controls);
2162        mutex_init(&uvc_driver.open_mutex);
2163        mutex_init(&uvc_driver.ctrl_mutex);
2164
2165        uvc_ctrl_init();
2166
2167        result = usb_register(&uvc_driver.driver);
2168        if (result == 0)
2169                printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");
2170        return result;
2171}
2172
2173static void __exit uvc_cleanup(void)
2174{
2175        usb_deregister(&uvc_driver.driver);
2176}
2177
2178module_init(uvc_init);
2179module_exit(uvc_cleanup);
2180
2181module_param_named(nodrop, uvc_no_drop_param, uint, S_IRUGO|S_IWUSR);
2182MODULE_PARM_DESC(nodrop, "Don't drop incomplete frames");
2183module_param_named(quirks, uvc_quirks_param, uint, S_IRUGO|S_IWUSR);
2184MODULE_PARM_DESC(quirks, "Forced device quirks");
2185module_param_named(trace, uvc_trace_param, uint, S_IRUGO|S_IWUSR);
2186MODULE_PARM_DESC(trace, "Trace level bitmask");
2187
2188MODULE_AUTHOR(DRIVER_AUTHOR);
2189MODULE_DESCRIPTION(DRIVER_DESC);
2190MODULE_LICENSE("GPL");
2191MODULE_VERSION(DRIVER_VERSION);
2192
2193