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