linux/drivers/media/usb/gspca/touptek.c
<<
>>
Prefs
   1/*
   2 * ToupTek UCMOS / AmScope MU series camera driver
   3 * TODO: contrast with ScopeTek / AmScope MDC cameras
   4 *
   5 * Copyright (C) 2012-2014 John McMaster <JohnDMcMaster@gmail.com>
   6 *
   7 * Special thanks to Bushing for helping with the decrypt algorithm and
   8 * Sean O'Sullivan / the Rensselaer Center for Open Source
   9 * Software (RCOS) for helping me learn kernel development
  10 *
  11 * This program is free software; you can redistribute it and/or modify
  12 * it under the terms of the GNU General Public License as published by
  13 * the Free Software Foundation; either version 2 of the License, or
  14 * any later version.
  15 *
  16 * This program is distributed in the hope that it will be useful,
  17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 * GNU General Public License for more details.
  20 */
  21
  22#include "gspca.h"
  23
  24#define MODULE_NAME "touptek"
  25
  26MODULE_AUTHOR("John McMaster");
  27MODULE_DESCRIPTION("ToupTek UCMOS / Amscope MU microscope camera driver");
  28MODULE_LICENSE("GPL");
  29
  30/*
  31 * Exposure reg is linear with exposure time
  32 * Exposure (sec), E (reg)
  33 * 0.000400, 0x0002
  34 * 0.001000, 0x0005
  35 * 0.005000, 0x0019
  36 * 0.020000, 0x0064
  37 * 0.080000, 0x0190
  38 * 0.400000, 0x07D0
  39 * 1.000000, 0x1388
  40 * 2.000000, 0x2710
  41 *
  42 * Three gain stages
  43 * 0x1000: master channel enable bit
  44 * 0x007F: low gain bits
  45 * 0x0080: medium gain bit
  46 * 0x0100: high gain bit
  47 * gain = enable * (1 + regH) * (1 + regM) * z * regL
  48 *
  49 * Gain implementation
  50 * Want to do something similar to mt9v011.c's set_balance
  51 *
  52 * Gain does not vary with resolution (checked 640x480 vs 1600x1200)
  53 *
  54 * Constant derivation:
  55 *
  56 * Raw data:
  57 * Gain,   GTOP,   B,     R,      GBOT
  58 * 1.00,   0x105C, 0x1068, 0x10C8, 0x105C
  59 * 1.20,   0x106E, 0x107E, 0x10D6, 0x106E
  60 * 1.40,   0x10C0, 0x10CA, 0x10E5, 0x10C0
  61 * 1.60,   0x10C9, 0x10D4, 0x10F3, 0x10C9
  62 * 1.80,   0x10D2, 0x10DE, 0x11C1, 0x10D2
  63 * 2.00,   0x10DC, 0x10E9, 0x11C8, 0x10DC
  64 * 2.20,   0x10E5, 0x10F3, 0x11CF, 0x10E5
  65 * 2.40,   0x10EE, 0x10FE, 0x11D7, 0x10EE
  66 * 2.60,   0x10F7, 0x11C4, 0x11DE, 0x10F7
  67 * 2.80,   0x11C0, 0x11CA, 0x11E5, 0x11C0
  68 * 3.00,   0x11C5, 0x11CF, 0x11ED, 0x11C5
  69 *
  70 * zR = 0.0069605943152454778
  71 *      about 3/431 = 0.0069605568445475635
  72 * zB = 0.0095695970695970703
  73 *      about 6/627 = 0.0095693779904306216
  74 * zG = 0.010889328063241107
  75 *      about 6/551 = 0.010889292196007259
  76 * about 10 bits for constant + 7 bits for value => at least 17 bit
  77 * intermediate with 32 bit ints should be fine for overflow etc
  78 * Essentially gains are in range 0-0x001FF
  79 *
  80 * However, V4L expects a main gain channel + R and B balance
  81 * To keep things simple for now saturate the values of balance is too high/low
  82 * This isn't really ideal but easy way to fit the Linux model
  83 *
  84 * Converted using gain model turns out to be quite linear:
  85 * Gain, GTOP, B, R, GBOT
  86 * 1.00, 92, 104, 144, 92
  87 * 1.20, 110, 126, 172, 110
  88 * 1.40, 128, 148, 202, 128
  89 * 1.60, 146, 168, 230, 146
  90 * 1.80, 164, 188, 260, 164
  91 * 2.00, 184, 210, 288, 184
  92 * 2.20, 202, 230, 316, 202
  93 * 2.40, 220, 252, 348, 220
  94 * 2.60, 238, 272, 376, 238
  95 * 2.80, 256, 296, 404, 256
  96 * 3.00, 276, 316, 436, 276
  97 *
  98 * Maximum gain is 0x7FF * 2 * 2 => 0x1FFC (8188)
  99 * or about 13 effective bits of gain
 100 * The highest the commercial driver goes in my setup 436
 101 * However, because could *maybe* damage circuits
 102 * limit the gain until have a reason to go higher
 103 * Solution: gain clipped and warning emitted
 104 */
 105#define GAIN_MAX                511
 106
 107/* Frame sync is a short read */
 108#define BULK_SIZE               0x4000
 109
 110/* MT9E001 reg names to give a rough approximation */
 111#define REG_COARSE_INTEGRATION_TIME_    0x3012
 112#define REG_GROUPED_PARAMETER_HOLD_     0x3022
 113#define REG_MODE_SELECT                 0x0100
 114#define REG_OP_SYS_CLK_DIV              0x030A
 115#define REG_VT_SYS_CLK_DIV              0x0302
 116#define REG_PRE_PLL_CLK_DIV             0x0304
 117#define REG_VT_PIX_CLK_DIV              0x0300
 118#define REG_OP_PIX_CLK_DIV              0x0308
 119#define REG_PLL_MULTIPLIER              0x0306
 120#define REG_COARSE_INTEGRATION_TIME_    0x3012
 121#define REG_FRAME_LENGTH_LINES          0x0340
 122#define REG_FRAME_LENGTH_LINES_         0x300A
 123#define REG_GREEN1_GAIN                 0x3056
 124#define REG_GREEN2_GAIN                 0x305C
 125#define REG_GROUPED_PARAMETER_HOLD      0x0104
 126#define REG_LINE_LENGTH_PCK_            0x300C
 127#define REG_MODE_SELECT                 0x0100
 128#define REG_PLL_MULTIPLIER              0x0306
 129#define REG_READ_MODE                   0x3040
 130#define REG_BLUE_GAIN                   0x3058
 131#define REG_RED_GAIN                    0x305A
 132#define REG_RESET_REGISTER              0x301A
 133#define REG_SCALE_M                     0x0404
 134#define REG_SCALING_MODE                0x0400
 135#define REG_SOFTWARE_RESET              0x0103
 136#define REG_X_ADDR_END                  0x0348
 137#define REG_X_ADDR_START                0x0344
 138#define REG_X_ADDR_START                0x0344
 139#define REG_X_OUTPUT_SIZE               0x034C
 140#define REG_Y_ADDR_END                  0x034A
 141#define REG_Y_ADDR_START                0x0346
 142#define REG_Y_OUTPUT_SIZE               0x034E
 143
 144
 145/* specific webcam descriptor */
 146struct sd {
 147        struct gspca_dev gspca_dev;     /* !! must be the first item */
 148        /* How many bytes this frame */
 149        unsigned int this_f;
 150
 151        /*
 152        Device has separate gains for each Bayer quadrant
 153        V4L supports master gain which is referenced to G1/G2 and supplies
 154        individual balance controls for R/B
 155        */
 156        struct v4l2_ctrl *blue;
 157        struct v4l2_ctrl *red;
 158};
 159
 160/* Used to simplify reg write error handling */
 161struct cmd {
 162        u16 value;
 163        u16 index;
 164};
 165
 166static const struct v4l2_pix_format vga_mode[] = {
 167        {800, 600,
 168                V4L2_PIX_FMT_SGRBG8,
 169                V4L2_FIELD_NONE,
 170                .bytesperline = 800,
 171                .sizeimage = 800 * 600,
 172                .colorspace = V4L2_COLORSPACE_SRGB},
 173        {1600, 1200,
 174                V4L2_PIX_FMT_SGRBG8,
 175                V4L2_FIELD_NONE,
 176                .bytesperline = 1600,
 177                .sizeimage = 1600 * 1200,
 178                .colorspace = V4L2_COLORSPACE_SRGB},
 179        {3264, 2448,
 180                V4L2_PIX_FMT_SGRBG8,
 181                V4L2_FIELD_NONE,
 182                .bytesperline = 3264,
 183                .sizeimage = 3264 * 2448,
 184                .colorspace = V4L2_COLORSPACE_SRGB},
 185};
 186
 187/*
 188 * As theres no known frame sync, the only way to keep synced is to try hard
 189 * to never miss any packets
 190 */
 191#if MAX_NURBS < 4
 192#error "Not enough URBs in the gspca table"
 193#endif
 194
 195static int val_reply(struct gspca_dev *gspca_dev, const char *reply, int rc)
 196{
 197        if (rc < 0) {
 198                gspca_err(gspca_dev, "reply has error %d\n", rc);
 199                return -EIO;
 200        }
 201        if (rc != 1) {
 202                gspca_err(gspca_dev, "Bad reply size %d\n", rc);
 203                return -EIO;
 204        }
 205        if (reply[0] != 0x08) {
 206                gspca_err(gspca_dev, "Bad reply 0x%02x\n", (int)reply[0]);
 207                return -EIO;
 208        }
 209        return 0;
 210}
 211
 212static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
 213{
 214        char *buff = gspca_dev->usb_buf;
 215        int rc;
 216
 217        gspca_dbg(gspca_dev, D_USBO,
 218                  "reg_w bReq=0x0B, bReqT=0xC0, wVal=0x%04X, wInd=0x%04X\n\n",
 219                  value, index);
 220        rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
 221                0x0B, 0xC0, value, index, buff, 1, 500);
 222        gspca_dbg(gspca_dev, D_USBO, "rc=%d, ret={0x%02x}\n", rc, (int)buff[0]);
 223        if (rc < 0) {
 224                gspca_err(gspca_dev, "Failed reg_w(0x0B, 0xC0, 0x%04X, 0x%04X) w/ rc %d\n",
 225                          value, index, rc);
 226                gspca_dev->usb_err = rc;
 227                return;
 228        }
 229        if (val_reply(gspca_dev, buff, rc)) {
 230                gspca_err(gspca_dev, "Bad reply to reg_w(0x0B, 0xC0, 0x%04X, 0x%04X\n",
 231                          value, index);
 232                gspca_dev->usb_err = -EIO;
 233        }
 234}
 235
 236static void reg_w_buf(struct gspca_dev *gspca_dev,
 237                const struct cmd *p, int l)
 238{
 239        do {
 240                reg_w(gspca_dev, p->value, p->index);
 241                p++;
 242        } while (--l > 0);
 243}
 244
 245static void setexposure(struct gspca_dev *gspca_dev, s32 val)
 246{
 247        u16 value;
 248        unsigned int w = gspca_dev->pixfmt.width;
 249
 250        if (w == 800)
 251                value = val * 5;
 252        else if (w == 1600)
 253                value = val * 3;
 254        else if (w == 3264)
 255                value = val * 3 / 2;
 256        else {
 257                gspca_err(gspca_dev, "Invalid width %u\n", w);
 258                gspca_dev->usb_err = -EINVAL;
 259                return;
 260        }
 261        gspca_dbg(gspca_dev, D_STREAM, "exposure: 0x%04X ms\n\n", value);
 262        /* Wonder if theres a good reason for sending it twice */
 263        /* probably not but leave it in because...why not */
 264        reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
 265        reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
 266}
 267
 268static int gainify(int in)
 269{
 270        /*
 271         * TODO: check if there are any issues with corner cases
 272         * 0x000 (0):0x07F (127): regL
 273         * 0x080 (128) - 0x0FF (255): regM, regL
 274         * 0x100 (256) - max: regH, regM, regL
 275         */
 276        if (in <= 0x7F)
 277                return 0x1000 | in;
 278        else if (in <= 0xFF)
 279                return 0x1080 | in / 2;
 280        else
 281                return 0x1180 | in / 4;
 282}
 283
 284static void setggain(struct gspca_dev *gspca_dev, u16 global_gain)
 285{
 286        u16 normalized;
 287
 288        normalized = gainify(global_gain);
 289        gspca_dbg(gspca_dev, D_STREAM, "gain G1/G2 (0x%04X): 0x%04X (src 0x%04X)\n\n",
 290                  REG_GREEN1_GAIN,
 291                  normalized, global_gain);
 292
 293        reg_w(gspca_dev, normalized, REG_GREEN1_GAIN);
 294        reg_w(gspca_dev, normalized, REG_GREEN2_GAIN);
 295}
 296
 297static void setbgain(struct gspca_dev *gspca_dev,
 298                u16 gain, u16 global_gain)
 299{
 300        u16 normalized;
 301
 302        normalized = global_gain +
 303                ((u32)global_gain) * gain / GAIN_MAX;
 304        if (normalized > GAIN_MAX) {
 305                gspca_dbg(gspca_dev, D_STREAM, "Truncating blue 0x%04X w/ value 0x%04X\n\n",
 306                          GAIN_MAX, normalized);
 307                normalized = GAIN_MAX;
 308        }
 309        normalized = gainify(normalized);
 310        gspca_dbg(gspca_dev, D_STREAM, "gain B (0x%04X): 0x%04X w/ source 0x%04X\n\n",
 311                  REG_BLUE_GAIN, normalized, gain);
 312
 313        reg_w(gspca_dev, normalized, REG_BLUE_GAIN);
 314}
 315
 316static void setrgain(struct gspca_dev *gspca_dev,
 317                u16 gain, u16 global_gain)
 318{
 319        u16 normalized;
 320
 321        normalized = global_gain +
 322                ((u32)global_gain) * gain / GAIN_MAX;
 323        if (normalized > GAIN_MAX) {
 324                gspca_dbg(gspca_dev, D_STREAM, "Truncating gain 0x%04X w/ value 0x%04X\n\n",
 325                          GAIN_MAX, normalized);
 326                normalized = GAIN_MAX;
 327        }
 328        normalized = gainify(normalized);
 329        gspca_dbg(gspca_dev, D_STREAM, "gain R (0x%04X): 0x%04X w / source 0x%04X\n\n",
 330                  REG_RED_GAIN, normalized, gain);
 331
 332        reg_w(gspca_dev, normalized, REG_RED_GAIN);
 333}
 334
 335static void configure_wh(struct gspca_dev *gspca_dev)
 336{
 337        unsigned int w = gspca_dev->pixfmt.width;
 338
 339        gspca_dbg(gspca_dev, D_STREAM, "configure_wh\n\n");
 340
 341        if (w == 800) {
 342                static const struct cmd reg_init_res[] = {
 343                        {0x0060, REG_X_ADDR_START},
 344                        {0x0CD9, REG_X_ADDR_END},
 345                        {0x0036, REG_Y_ADDR_START},
 346                        {0x098F, REG_Y_ADDR_END},
 347                        {0x07C7, REG_READ_MODE},
 348                };
 349
 350                reg_w_buf(gspca_dev,
 351                               reg_init_res, ARRAY_SIZE(reg_init_res));
 352        } else if (w == 1600) {
 353                static const struct cmd reg_init_res[] = {
 354                        {0x009C, REG_X_ADDR_START},
 355                        {0x0D19, REG_X_ADDR_END},
 356                        {0x0068, REG_Y_ADDR_START},
 357                        {0x09C5, REG_Y_ADDR_END},
 358                        {0x06C3, REG_READ_MODE},
 359                };
 360
 361                reg_w_buf(gspca_dev,
 362                               reg_init_res, ARRAY_SIZE(reg_init_res));
 363        } else if (w == 3264) {
 364                static const struct cmd reg_init_res[] = {
 365                        {0x00E8, REG_X_ADDR_START},
 366                        {0x0DA7, REG_X_ADDR_END},
 367                        {0x009E, REG_Y_ADDR_START},
 368                        {0x0A2D, REG_Y_ADDR_END},
 369                        {0x0241, REG_READ_MODE},
 370                };
 371
 372                reg_w_buf(gspca_dev,
 373                               reg_init_res, ARRAY_SIZE(reg_init_res));
 374        } else {
 375                gspca_err(gspca_dev, "bad width %u\n", w);
 376                gspca_dev->usb_err = -EINVAL;
 377                return;
 378        }
 379
 380        reg_w(gspca_dev, 0x0000, REG_SCALING_MODE);
 381        reg_w(gspca_dev, 0x0010, REG_SCALE_M);
 382        reg_w(gspca_dev, w, REG_X_OUTPUT_SIZE);
 383        reg_w(gspca_dev, gspca_dev->pixfmt.height, REG_Y_OUTPUT_SIZE);
 384
 385        if (w == 800) {
 386                reg_w(gspca_dev, 0x0384, REG_FRAME_LENGTH_LINES_);
 387                reg_w(gspca_dev, 0x0960, REG_LINE_LENGTH_PCK_);
 388        } else if (w == 1600) {
 389                reg_w(gspca_dev, 0x0640, REG_FRAME_LENGTH_LINES_);
 390                reg_w(gspca_dev, 0x0FA0, REG_LINE_LENGTH_PCK_);
 391        } else if (w == 3264) {
 392                reg_w(gspca_dev, 0x0B4B, REG_FRAME_LENGTH_LINES_);
 393                reg_w(gspca_dev, 0x1F40, REG_LINE_LENGTH_PCK_);
 394        } else {
 395                gspca_err(gspca_dev, "bad width %u\n", w);
 396                gspca_dev->usb_err = -EINVAL;
 397                return;
 398        }
 399}
 400
 401/* Packets that were encrypted, no idea if the grouping is significant */
 402static void configure_encrypted(struct gspca_dev *gspca_dev)
 403{
 404        static const struct cmd reg_init_begin[] = {
 405                {0x0100, REG_SOFTWARE_RESET},
 406                {0x0000, REG_MODE_SELECT},
 407                {0x0100, REG_GROUPED_PARAMETER_HOLD},
 408                {0x0004, REG_VT_PIX_CLK_DIV},
 409                {0x0001, REG_VT_SYS_CLK_DIV},
 410                {0x0008, REG_OP_PIX_CLK_DIV},
 411                {0x0001, REG_OP_SYS_CLK_DIV},
 412                {0x0004, REG_PRE_PLL_CLK_DIV},
 413                {0x0040, REG_PLL_MULTIPLIER},
 414                {0x0000, REG_GROUPED_PARAMETER_HOLD},
 415                {0x0100, REG_GROUPED_PARAMETER_HOLD},
 416        };
 417        static const struct cmd reg_init_end[] = {
 418                {0x0000, REG_GROUPED_PARAMETER_HOLD},
 419                {0x0301, 0x31AE},
 420                {0x0805, 0x3064},
 421                {0x0071, 0x3170},
 422                {0x10DE, REG_RESET_REGISTER},
 423                {0x0000, REG_MODE_SELECT},
 424                {0x0010, REG_PLL_MULTIPLIER},
 425                {0x0100, REG_MODE_SELECT},
 426        };
 427
 428        gspca_dbg(gspca_dev, D_STREAM, "Encrypted begin, w = %u\n\n",
 429                  gspca_dev->pixfmt.width);
 430        reg_w_buf(gspca_dev, reg_init_begin, ARRAY_SIZE(reg_init_begin));
 431        configure_wh(gspca_dev);
 432        reg_w_buf(gspca_dev, reg_init_end, ARRAY_SIZE(reg_init_end));
 433        reg_w(gspca_dev, 0x0100, REG_GROUPED_PARAMETER_HOLD);
 434        reg_w(gspca_dev, 0x0000, REG_GROUPED_PARAMETER_HOLD);
 435
 436        gspca_dbg(gspca_dev, D_STREAM, "Encrypted end\n\n");
 437}
 438
 439static int configure(struct gspca_dev *gspca_dev)
 440{
 441        int rc;
 442        char *buff = gspca_dev->usb_buf;
 443
 444        gspca_dbg(gspca_dev, D_STREAM, "configure()\n\n");
 445
 446        /*
 447         * First driver sets a sort of encryption key
 448         * A number of futur requests of this type have wValue and wIndex
 449         * encrypted as follows:
 450         * -Compute key = this wValue rotate left by 4 bits
 451         *      (decrypt.py rotates right because we are decrypting)
 452         * -Later packets encrypt packets by XOR'ing with key
 453         *      XOR encrypt/decrypt is symmetrical
 454         *      wValue, and wIndex are encrypted
 455         *      bRequest is not and bRequestType is always 0xC0
 456         *              This allows resyncing if key is unknown?
 457         * By setting 0 we XOR with 0 and the shifting and XOR drops out
 458         */
 459        rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
 460                             0x16, 0xC0, 0x0000, 0x0000, buff, 2, 500);
 461        if (val_reply(gspca_dev, buff, rc)) {
 462                gspca_err(gspca_dev, "failed key req\n");
 463                return -EIO;
 464        }
 465
 466        /*
 467         * Next does some sort of 2 packet challenge / response
 468         * evidence suggests its an Atmel I2C crypto part but nobody cares to
 469         * look
 470         * (to make sure its not cloned hardware?)
 471         * Ignore: I want to work with their hardware, not clone it
 472         * 16 bytes out challenge, requestType: 0x40
 473         * 16 bytes in response, requestType: 0xC0
 474         */
 475
 476        rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
 477                             0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
 478        if (rc < 0) {
 479                gspca_err(gspca_dev, "failed to replay packet 176 w/ rc %d\n",
 480                          rc);
 481                return rc;
 482        }
 483
 484        rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
 485                             0x01, 0x40, 0x0000, 0x000F, NULL, 0, 500);
 486        if (rc < 0) {
 487                gspca_err(gspca_dev, "failed to replay packet 178 w/ rc %d\n",
 488                          rc);
 489                return rc;
 490        }
 491
 492        rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
 493                             0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
 494        if (rc < 0) {
 495                gspca_err(gspca_dev, "failed to replay packet 180 w/ rc %d\n",
 496                          rc);
 497                return rc;
 498        }
 499
 500        /*
 501         * Serial number?  Doesn't seem to be required
 502         * cam1: \xE6\x0D\x00\x00, cam2: \x70\x19\x00\x00
 503         * rc = usb_control_msg(gspca_dev->dev,
 504         *                      usb_rcvctrlpipe(gspca_dev->dev, 0),
 505         *                      0x20, 0xC0, 0x0000, 0x0000, buff, 4, 500);
 506         */
 507
 508        /* Large (EEPROM?) read, skip it since no idea what to do with it */
 509        gspca_dev->usb_err = 0;
 510        configure_encrypted(gspca_dev);
 511        if (gspca_dev->usb_err)
 512                return gspca_dev->usb_err;
 513
 514        /* Omitted this by accident, does not work without it */
 515        rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
 516                             0x01, 0x40, 0x0003, 0x000F, NULL, 0, 500);
 517        if (rc < 0) {
 518                gspca_err(gspca_dev, "failed to replay final packet w/ rc %d\n",
 519                          rc);
 520                return rc;
 521        }
 522
 523        gspca_dbg(gspca_dev, D_STREAM, "Configure complete\n\n");
 524        return 0;
 525}
 526
 527static int sd_config(struct gspca_dev *gspca_dev,
 528                     const struct usb_device_id *id)
 529{
 530        gspca_dev->cam.cam_mode = vga_mode;
 531        gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
 532
 533        /* Yes we want URBs and we want them now! */
 534        gspca_dev->cam.no_urb_create = 0;
 535        gspca_dev->cam.bulk_nurbs = 4;
 536        /* Largest size the windows driver uses */
 537        gspca_dev->cam.bulk_size = BULK_SIZE;
 538        /* Def need to use bulk transfers */
 539        gspca_dev->cam.bulk = 1;
 540
 541        return 0;
 542}
 543
 544static int sd_start(struct gspca_dev *gspca_dev)
 545{
 546        struct sd *sd = (struct sd *) gspca_dev;
 547        int rc;
 548
 549        sd->this_f = 0;
 550
 551        rc = configure(gspca_dev);
 552        if (rc < 0) {
 553                gspca_err(gspca_dev, "Failed configure\n");
 554                return rc;
 555        }
 556        /* First two frames have messed up gains
 557        Drop them to avoid special cases in user apps? */
 558        return 0;
 559}
 560
 561static void sd_pkt_scan(struct gspca_dev *gspca_dev,
 562                        u8 *data,       /* isoc packet */
 563                        int len)        /* iso packet length */
 564{
 565        struct sd *sd = (struct sd *) gspca_dev;
 566
 567        if (len != BULK_SIZE) {
 568                /* can we finish a frame? */
 569                if (sd->this_f + len == gspca_dev->pixfmt.sizeimage) {
 570                        gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
 571                        gspca_dbg(gspca_dev, D_FRAM, "finish frame sz %u/%u w/ len %u\n\n",
 572                                  sd->this_f, gspca_dev->pixfmt.sizeimage, len);
 573                /* lost some data, discard the frame */
 574                } else {
 575                        gspca_frame_add(gspca_dev, DISCARD_PACKET, NULL, 0);
 576                        gspca_dbg(gspca_dev, D_FRAM, "abort frame sz %u/%u w/ len %u\n\n",
 577                                  sd->this_f, gspca_dev->pixfmt.sizeimage, len);
 578                }
 579                sd->this_f = 0;
 580        } else {
 581                if (sd->this_f == 0)
 582                        gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
 583                else
 584                        gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
 585                sd->this_f += len;
 586        }
 587}
 588
 589static int sd_init(struct gspca_dev *gspca_dev)
 590{
 591        return 0;
 592}
 593
 594static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
 595{
 596        struct gspca_dev *gspca_dev =
 597                container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
 598        struct sd *sd = (struct sd *) gspca_dev;
 599
 600        gspca_dev->usb_err = 0;
 601
 602        if (!gspca_dev->streaming)
 603                return 0;
 604
 605        switch (ctrl->id) {
 606        case V4L2_CID_EXPOSURE:
 607                setexposure(gspca_dev, ctrl->val);
 608                break;
 609        case V4L2_CID_GAIN:
 610                /* gspca_dev->gain automatically updated */
 611                setggain(gspca_dev, gspca_dev->gain->val);
 612                break;
 613        case V4L2_CID_BLUE_BALANCE:
 614                sd->blue->val = ctrl->val;
 615                setbgain(gspca_dev, sd->blue->val, gspca_dev->gain->val);
 616                break;
 617        case V4L2_CID_RED_BALANCE:
 618                sd->red->val = ctrl->val;
 619                setrgain(gspca_dev, sd->red->val, gspca_dev->gain->val);
 620                break;
 621        }
 622        return gspca_dev->usb_err;
 623}
 624
 625static const struct v4l2_ctrl_ops sd_ctrl_ops = {
 626        .s_ctrl = sd_s_ctrl,
 627};
 628
 629static int sd_init_controls(struct gspca_dev *gspca_dev)
 630{
 631        struct sd *sd = (struct sd *) gspca_dev;
 632        struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
 633
 634        gspca_dev->vdev.ctrl_handler = hdl;
 635        v4l2_ctrl_handler_init(hdl, 4);
 636
 637        gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
 638        /* Mostly limited by URB timeouts */
 639        /* XXX: make dynamic based on frame rate? */
 640                V4L2_CID_EXPOSURE, 0, 800, 1, 350);
 641        gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
 642                        V4L2_CID_GAIN, 0, 511, 1, 128);
 643        sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
 644                        V4L2_CID_BLUE_BALANCE, 0, 1023, 1, 80);
 645        sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
 646                        V4L2_CID_RED_BALANCE, 0, 1023, 1, 295);
 647
 648        if (hdl->error) {
 649                gspca_err(gspca_dev, "Could not initialize controls\n");
 650                return hdl->error;
 651        }
 652        return 0;
 653}
 654
 655/* sub-driver description */
 656static const struct sd_desc sd_desc = {
 657        .name = MODULE_NAME,
 658        .config = sd_config,
 659        .init = sd_init,
 660        .init_controls = sd_init_controls,
 661        .start = sd_start,
 662        .pkt_scan = sd_pkt_scan,
 663};
 664
 665/* Table of supported USB devices */
 666static const struct usb_device_id device_table[] = {
 667        /* Commented out devices should be related */
 668        /* AS: AmScope, TT: ToupTek */
 669        /* { USB_DEVICE(0x0547, 0x6035) },  TT UCMOS00350KPA */
 670        /* { USB_DEVICE(0x0547, 0x6130) },  TT UCMOS01300KPA */
 671        /* { USB_DEVICE(0x0547, 0x6200) },  TT UCMOS02000KPA */
 672        /* { USB_DEVICE(0x0547, 0x6310) },  TT UCMOS03100KPA */
 673        /* { USB_DEVICE(0x0547, 0x6510) },  TT UCMOS05100KPA */
 674        /* { USB_DEVICE(0x0547, 0x6800) },  TT UCMOS08000KPA */
 675        /* { USB_DEVICE(0x0547, 0x6801) },  TT UCMOS08000KPB */
 676        { USB_DEVICE(0x0547, 0x6801) }, /* TT UCMOS08000KPB, AS MU800 */
 677        /* { USB_DEVICE(0x0547, 0x6900) },  TT UCMOS09000KPA */
 678        /* { USB_DEVICE(0x0547, 0x6901) },  TT UCMOS09000KPB */
 679        /* { USB_DEVICE(0x0547, 0x6010) },  TT UCMOS10000KPA */
 680        /* { USB_DEVICE(0x0547, 0x6014) },  TT UCMOS14000KPA */
 681        /* { USB_DEVICE(0x0547, 0x6131) },  TT UCMOS01300KMA */
 682        /* { USB_DEVICE(0x0547, 0x6511) },  TT UCMOS05100KMA */
 683        /* { USB_DEVICE(0x0547, 0x8080) },  TT UHCCD00800KPA */
 684        /* { USB_DEVICE(0x0547, 0x8140) },  TT UHCCD01400KPA */
 685        /* { USB_DEVICE(0x0547, 0x8141) },  TT EXCCD01400KPA */
 686        /* { USB_DEVICE(0x0547, 0x8200) },  TT UHCCD02000KPA */
 687        /* { USB_DEVICE(0x0547, 0x8201) },  TT UHCCD02000KPB */
 688        /* { USB_DEVICE(0x0547, 0x8310) },  TT UHCCD03100KPA */
 689        /* { USB_DEVICE(0x0547, 0x8500) },  TT UHCCD05000KPA */
 690        /* { USB_DEVICE(0x0547, 0x8510) },  TT UHCCD05100KPA */
 691        /* { USB_DEVICE(0x0547, 0x8600) },  TT UHCCD06000KPA */
 692        /* { USB_DEVICE(0x0547, 0x8800) },  TT UHCCD08000KPA */
 693        /* { USB_DEVICE(0x0547, 0x8315) },  TT UHCCD03150KPA */
 694        /* { USB_DEVICE(0x0547, 0x7800) },  TT UHCCD00800KMA */
 695        /* { USB_DEVICE(0x0547, 0x7140) },  TT UHCCD01400KMA */
 696        /* { USB_DEVICE(0x0547, 0x7141) },  TT UHCCD01400KMB */
 697        /* { USB_DEVICE(0x0547, 0x7200) },  TT UHCCD02000KMA */
 698        /* { USB_DEVICE(0x0547, 0x7315) },  TT UHCCD03150KMA */
 699        { }
 700};
 701MODULE_DEVICE_TABLE(usb, device_table);
 702
 703static int sd_probe(struct usb_interface *intf,
 704                    const struct usb_device_id *id)
 705{
 706        return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
 707                             THIS_MODULE);
 708}
 709
 710static struct usb_driver sd_driver = {
 711        .name = MODULE_NAME,
 712        .id_table = device_table,
 713        .probe = sd_probe,
 714        .disconnect = gspca_disconnect,
 715#ifdef CONFIG_PM
 716        .suspend = gspca_suspend,
 717        .resume = gspca_resume,
 718#endif
 719};
 720
 721static int __init sd_mod_init(void)
 722{
 723        int ret;
 724
 725        ret = usb_register(&sd_driver);
 726        if (ret < 0)
 727                return ret;
 728        return 0;
 729}
 730static void __exit sd_mod_exit(void)
 731{
 732        usb_deregister(&sd_driver);
 733}
 734
 735module_init(sd_mod_init);
 736module_exit(sd_mod_exit);
 737