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