Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *      sonix sn9c102 (bayer) library
0004  *
0005  * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
0006  * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
0007  * Add Pas106 Stefano Mozzi (C) 2004
0008  */
0009 
0010 /* Some documentation on known sonixb registers:
0011 
0012 Reg Use
0013 sn9c101 / sn9c102:
0014 0x10    high nibble red gain low nibble blue gain
0015 0x11    low nibble green gain
0016 sn9c103:
0017 0x05    red gain 0-127
0018 0x06    blue gain 0-127
0019 0x07    green gain 0-127
0020 all:
0021 0x08-0x0f i2c / 3wire registers
0022 0x12    hstart
0023 0x13    vstart
0024 0x15    hsize (hsize = register-value * 16)
0025 0x16    vsize (vsize = register-value * 16)
0026 0x17    bit 0 toggle compression quality (according to sn9c102 driver)
0027 0x18    bit 7 enables compression, bit 4-5 set image down scaling:
0028     00 scale 1, 01 scale 1/2, 10, scale 1/4
0029 0x19    high-nibble is sensor clock divider, changes exposure on sensors which
0030     use a clock generated by the bridge. Some sensors have their own clock.
0031 0x1c    auto_exposure area (for avg_lum) startx (startx = register-value * 32)
0032 0x1d    auto_exposure area (for avg_lum) starty (starty = register-value * 32)
0033 0x1e    auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
0034 0x1f    auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
0035 */
0036 
0037 #define MODULE_NAME "sonixb"
0038 
0039 #include <linux/input.h>
0040 #include "gspca.h"
0041 
0042 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
0043 MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
0044 MODULE_LICENSE("GPL");
0045 
0046 /* specific webcam descriptor */
0047 struct sd {
0048     struct gspca_dev gspca_dev; /* !! must be the first item */
0049 
0050     struct v4l2_ctrl *brightness;
0051     struct v4l2_ctrl *plfreq;
0052 
0053     atomic_t avg_lum;
0054     int prev_avg_lum;
0055     int exposure_knee;
0056     int header_read;
0057     u8 header[12]; /* Header without sof marker */
0058 
0059     unsigned char autogain_ignore_frames;
0060     unsigned char frames_to_drop;
0061 
0062     __u8 bridge;            /* Type of bridge */
0063 #define BRIDGE_101 0
0064 #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
0065 #define BRIDGE_103 1
0066 
0067     __u8 sensor;            /* Type of image sensor chip */
0068 #define SENSOR_HV7131D 0
0069 #define SENSOR_HV7131R 1
0070 #define SENSOR_OV6650 2
0071 #define SENSOR_OV7630 3
0072 #define SENSOR_PAS106 4
0073 #define SENSOR_PAS202 5
0074 #define SENSOR_TAS5110C 6
0075 #define SENSOR_TAS5110D 7
0076 #define SENSOR_TAS5130CXX 8
0077     __u8 reg11;
0078 };
0079 
0080 typedef const __u8 sensor_init_t[8];
0081 
0082 struct sensor_data {
0083     const __u8 *bridge_init;
0084     sensor_init_t *sensor_init;
0085     int sensor_init_size;
0086     int flags;
0087     __u8 sensor_addr;
0088 };
0089 
0090 /* sensor_data flags */
0091 #define F_SIF       0x01    /* sif or vga */
0092 
0093 /* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
0094 #define MODE_RAW 0x10       /* raw bayer mode */
0095 #define MODE_REDUCED_SIF 0x20   /* vga mode (320x240 / 160x120) on sif cam */
0096 
0097 #define COMP 0xc7       /* 0x87 //0x07 */
0098 #define COMP1 0xc9      /* 0x89 //0x09 */
0099 
0100 #define MCK_INIT 0x63
0101 #define MCK_INIT1 0x20      /*fixme: Bayer - 0x50 for JPEG ??*/
0102 
0103 #define SYS_CLK 0x04
0104 
0105 #define SENS(bridge, sensor, _flags, _sensor_addr) \
0106 { \
0107     .bridge_init = bridge, \
0108     .sensor_init = sensor, \
0109     .sensor_init_size = sizeof(sensor), \
0110     .flags = _flags, .sensor_addr = _sensor_addr \
0111 }
0112 
0113 /* We calculate the autogain at the end of the transfer of a frame, at this
0114    moment a frame with the old settings is being captured and transmitted. So
0115    if we adjust the gain or exposure we must ignore at least the next frame for
0116    the new settings to come into effect before doing any other adjustments. */
0117 #define AUTOGAIN_IGNORE_FRAMES 1
0118 
0119 static const struct v4l2_pix_format vga_mode[] = {
0120     {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0121         .bytesperline = 160,
0122         .sizeimage = 160 * 120,
0123         .colorspace = V4L2_COLORSPACE_SRGB,
0124         .priv = 2 | MODE_RAW},
0125     {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0126         .bytesperline = 160,
0127         .sizeimage = 160 * 120 * 5 / 4,
0128         .colorspace = V4L2_COLORSPACE_SRGB,
0129         .priv = 2},
0130     {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0131         .bytesperline = 320,
0132         .sizeimage = 320 * 240 * 5 / 4,
0133         .colorspace = V4L2_COLORSPACE_SRGB,
0134         .priv = 1},
0135     {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0136         .bytesperline = 640,
0137         .sizeimage = 640 * 480 * 5 / 4,
0138         .colorspace = V4L2_COLORSPACE_SRGB,
0139         .priv = 0},
0140 };
0141 static const struct v4l2_pix_format sif_mode[] = {
0142     {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0143         .bytesperline = 160,
0144         .sizeimage = 160 * 120,
0145         .colorspace = V4L2_COLORSPACE_SRGB,
0146         .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
0147     {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0148         .bytesperline = 160,
0149         .sizeimage = 160 * 120 * 5 / 4,
0150         .colorspace = V4L2_COLORSPACE_SRGB,
0151         .priv = 1 | MODE_REDUCED_SIF},
0152     {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
0153         .bytesperline = 176,
0154         .sizeimage = 176 * 144,
0155         .colorspace = V4L2_COLORSPACE_SRGB,
0156         .priv = 1 | MODE_RAW},
0157     {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0158         .bytesperline = 176,
0159         .sizeimage = 176 * 144 * 5 / 4,
0160         .colorspace = V4L2_COLORSPACE_SRGB,
0161         .priv = 1},
0162     {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0163         .bytesperline = 320,
0164         .sizeimage = 320 * 240 * 5 / 4,
0165         .colorspace = V4L2_COLORSPACE_SRGB,
0166         .priv = 0 | MODE_REDUCED_SIF},
0167     {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
0168         .bytesperline = 352,
0169         .sizeimage = 352 * 288 * 5 / 4,
0170         .colorspace = V4L2_COLORSPACE_SRGB,
0171         .priv = 0},
0172 };
0173 
0174 static const __u8 initHv7131d[] = {
0175     0x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
0176     0x00, 0x00,
0177     0x00, 0x00, 0x00, 0x02, 0x02, 0x00,
0178     0x28, 0x1e, 0x60, 0x8e, 0x42,
0179 };
0180 static const __u8 hv7131d_sensor_init[][8] = {
0181     {0xa0, 0x11, 0x01, 0x04, 0x00, 0x00, 0x00, 0x17},
0182     {0xa0, 0x11, 0x02, 0x00, 0x00, 0x00, 0x00, 0x17},
0183     {0xa0, 0x11, 0x28, 0x00, 0x00, 0x00, 0x00, 0x17},
0184     {0xa0, 0x11, 0x30, 0x30, 0x00, 0x00, 0x00, 0x17}, /* reset level */
0185     {0xa0, 0x11, 0x34, 0x02, 0x00, 0x00, 0x00, 0x17}, /* pixel bias volt */
0186 };
0187 
0188 static const __u8 initHv7131r[] = {
0189     0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
0190     0x00, 0x00,
0191     0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
0192     0x28, 0x1e, 0x60, 0x8a, 0x20,
0193 };
0194 static const __u8 hv7131r_sensor_init[][8] = {
0195     {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
0196     {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
0197     {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
0198     {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
0199     {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
0200 };
0201 static const __u8 initOv6650[] = {
0202     0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
0203     0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0204     0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
0205     0x10,
0206 };
0207 static const __u8 ov6650_sensor_init[][8] = {
0208     /* Bright, contrast, etc are set through SCBB interface.
0209      * AVCAP on win2 do not send any data on this controls. */
0210     /* Anyway, some registers appears to alter bright and constrat */
0211 
0212     /* Reset sensor */
0213     {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
0214     /* Set clock register 0x11 low nibble is clock divider */
0215     {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
0216     /* Next some unknown stuff */
0217     {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
0218 /*  {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
0219          * THIS SET GREEN SCREEN
0220          * (pixels could be innverted in decode kind of "brg",
0221          * but blue wont be there. Avoid this data ... */
0222     {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
0223     {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
0224     {0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},
0225     /* Enable rgb brightness control */
0226     {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
0227     /* HDG: Note windows uses the line below, which sets both register 0x60
0228        and 0x61 I believe these registers of the ov6650 are identical as
0229        those of the ov7630, because if this is true the windows settings
0230        add a bit additional red gain and a lot additional blue gain, which
0231        matches my findings that the windows settings make blue much too
0232        blue and red a little too red.
0233     {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
0234     /* Some more unknown stuff */
0235     {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
0236     {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
0237 };
0238 
0239 static const __u8 initOv7630[] = {
0240     0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
0241     0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
0242     0x00, 0x01, 0x01, 0x0a,             /* r11 .. r14 */
0243     0x28, 0x1e,         /* H & V sizes     r15 .. r16 */
0244     0x68, 0x8f, MCK_INIT1,              /* r17 .. r19 */
0245 };
0246 static const __u8 ov7630_sensor_init[][8] = {
0247     {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
0248     {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
0249 /*  {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10},      jfm */
0250     {0xd0, 0x21, 0x12, 0x5c, 0x00, 0x80, 0x34, 0x10},   /* jfm */
0251     {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
0252     {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
0253     {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
0254     {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
0255     {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
0256     {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
0257     {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
0258     {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
0259 /*  {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10},    * jfm */
0260     {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
0261     {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
0262     {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
0263     {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
0264     {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
0265     {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
0266 };
0267 
0268 static const __u8 initPas106[] = {
0269     0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
0270     0x00, 0x00,
0271     0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
0272     0x16, 0x12, 0x24, COMP1, MCK_INIT1,
0273 };
0274 /* compression 0x86 mckinit1 0x2b */
0275 
0276 /* "Known" PAS106B registers:
0277   0x02 clock divider
0278   0x03 Variable framerate bits 4-11
0279   0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!
0280        The variable framerate control must never be set lower then 300,
0281        which sets the framerate at 90 / reg02, otherwise vsync is lost.
0282   0x05 Shutter Time Line Offset, this can be used as an exposure control:
0283        0 = use full frame time, 255 = no exposure at all
0284        Note this may never be larger then "var-framerate control" / 2 - 2.
0285        When var-framerate control is < 514, no exposure is reached at the max
0286        allowed value for the framerate control value, rather then at 255.
0287   0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but
0288        only a very little bit, leave at 0xcd
0289   0x07 offset sign bit (bit0 1 > negative offset)
0290   0x08 offset
0291   0x09 Blue Gain
0292   0x0a Green1 Gain
0293   0x0b Green2 Gain
0294   0x0c Red Gain
0295   0x0e Global gain
0296   0x13 Write 1 to commit settings to sensor
0297 */
0298 
0299 static const __u8 pas106_sensor_init[][8] = {
0300     /* Pixel Clock Divider 6 */
0301     { 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
0302     /* Frame Time MSB (also seen as 0x12) */
0303     { 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
0304     /* Frame Time LSB (also seen as 0x05) */
0305     { 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
0306     /* Shutter Time Line Offset (also seen as 0x6d) */
0307     { 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
0308     /* Shutter Time Pixel Offset (also seen as 0xb1) */
0309     { 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
0310     /* Black Level Subtract Sign (also seen 0x00) */
0311     { 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
0312     /* Black Level Subtract Level (also seen 0x01) */
0313     { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
0314     { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
0315     /* Color Gain B Pixel 5 a */
0316     { 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
0317     /* Color Gain G1 Pixel 1 5 */
0318     { 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
0319     /* Color Gain G2 Pixel 1 0 5 */
0320     { 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
0321     /* Color Gain R Pixel 3 1 */
0322     { 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
0323     /* Color GainH  Pixel */
0324     { 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
0325     /* Global Gain */
0326     { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
0327     /* Contrast */
0328     { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
0329     /* H&V synchro polarity */
0330     { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
0331     /* ?default */
0332     { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
0333     /* DAC scale */
0334     { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
0335     /* ?default */
0336     { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
0337     /* Validate Settings */
0338     { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
0339 };
0340 
0341 static const __u8 initPas202[] = {
0342     0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
0343     0x00, 0x00,
0344     0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
0345     0x28, 0x1e, 0x20, 0x89, 0x20,
0346 };
0347 
0348 /* "Known" PAS202BCB registers:
0349   0x02 clock divider
0350   0x04 Variable framerate bits 6-11 (*)
0351   0x05 Var framerate  bits 0-5, one must leave the 2 msb's at 0 !!
0352   0x07 Blue Gain
0353   0x08 Green Gain
0354   0x09 Red Gain
0355   0x0b offset sign bit (bit0 1 > negative offset)
0356   0x0c offset
0357   0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
0358        leave at 1 otherwise we get a jump in our exposure control
0359   0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
0360   0x10 Master gain 0 - 31
0361   0x11 write 1 to apply changes
0362   (*) The variable framerate control must never be set lower then 500
0363       which sets the framerate at 30 / reg02, otherwise vsync is lost.
0364 */
0365 static const __u8 pas202_sensor_init[][8] = {
0366     /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
0367        to set it lower, but for some reason the bridge starts missing
0368        vsync's then */
0369     {0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
0370     {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
0371     {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
0372     {0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
0373     {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
0374     {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
0375     {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
0376     {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
0377     {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
0378     {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
0379 };
0380 
0381 static const __u8 initTas5110c[] = {
0382     0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
0383     0x00, 0x00,
0384     0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
0385     0x16, 0x12, 0x60, 0x86, 0x2b,
0386 };
0387 /* Same as above, except a different hstart */
0388 static const __u8 initTas5110d[] = {
0389     0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
0390     0x00, 0x00,
0391     0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
0392     0x16, 0x12, 0x60, 0x86, 0x2b,
0393 };
0394 /* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */
0395 static const __u8 tas5110c_sensor_init[][8] = {
0396     {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
0397     {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
0398 };
0399 /* Known TAS5110D registers
0400  * reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain
0401  * reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted)
0402  *        Note: writing reg03 seems to only work when written together with 02
0403  */
0404 static const __u8 tas5110d_sensor_init[][8] = {
0405     {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
0406 };
0407 
0408 static const __u8 initTas5130[] = {
0409     0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
0410     0x00, 0x00,
0411     0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
0412     0x28, 0x1e, 0x60, COMP, MCK_INIT,
0413 };
0414 static const __u8 tas5130_sensor_init[][8] = {
0415 /*  {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
0416                     * shutter 0x47 short exposure? */
0417     {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
0418                     /* shutter 0x01 long exposure */
0419     {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
0420 };
0421 
0422 static const struct sensor_data sensor_data[] = {
0423     SENS(initHv7131d, hv7131d_sensor_init, 0, 0),
0424     SENS(initHv7131r, hv7131r_sensor_init, 0, 0),
0425     SENS(initOv6650, ov6650_sensor_init, F_SIF, 0x60),
0426     SENS(initOv7630, ov7630_sensor_init, 0, 0x21),
0427     SENS(initPas106, pas106_sensor_init, F_SIF, 0),
0428     SENS(initPas202, pas202_sensor_init, 0, 0),
0429     SENS(initTas5110c, tas5110c_sensor_init, F_SIF, 0),
0430     SENS(initTas5110d, tas5110d_sensor_init, F_SIF, 0),
0431     SENS(initTas5130, tas5130_sensor_init, 0, 0),
0432 };
0433 
0434 /* get one byte in gspca_dev->usb_buf */
0435 static void reg_r(struct gspca_dev *gspca_dev,
0436           __u16 value)
0437 {
0438     int res;
0439 
0440     if (gspca_dev->usb_err < 0)
0441         return;
0442 
0443     res = usb_control_msg(gspca_dev->dev,
0444             usb_rcvctrlpipe(gspca_dev->dev, 0),
0445             0,          /* request */
0446             USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0447             value,
0448             0,          /* index */
0449             gspca_dev->usb_buf, 1,
0450             500);
0451 
0452     if (res < 0) {
0453         dev_err(gspca_dev->v4l2_dev.dev,
0454             "Error reading register %02x: %d\n", value, res);
0455         gspca_dev->usb_err = res;
0456         /*
0457          * Make sure the result is zeroed to avoid uninitialized
0458          * values.
0459          */
0460         gspca_dev->usb_buf[0] = 0;
0461     }
0462 }
0463 
0464 static void reg_w(struct gspca_dev *gspca_dev,
0465           __u16 value,
0466           const __u8 *buffer,
0467           int len)
0468 {
0469     int res;
0470 
0471     if (gspca_dev->usb_err < 0)
0472         return;
0473 
0474     memcpy(gspca_dev->usb_buf, buffer, len);
0475     res = usb_control_msg(gspca_dev->dev,
0476             usb_sndctrlpipe(gspca_dev->dev, 0),
0477             0x08,           /* request */
0478             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
0479             value,
0480             0,          /* index */
0481             gspca_dev->usb_buf, len,
0482             500);
0483 
0484     if (res < 0) {
0485         dev_err(gspca_dev->v4l2_dev.dev,
0486             "Error writing register %02x: %d\n", value, res);
0487         gspca_dev->usb_err = res;
0488     }
0489 }
0490 
0491 static void i2c_w(struct gspca_dev *gspca_dev, const u8 *buf)
0492 {
0493     int retry = 60;
0494 
0495     if (gspca_dev->usb_err < 0)
0496         return;
0497 
0498     /* is i2c ready */
0499     reg_w(gspca_dev, 0x08, buf, 8);
0500     while (retry--) {
0501         if (gspca_dev->usb_err < 0)
0502             return;
0503         msleep(1);
0504         reg_r(gspca_dev, 0x08);
0505         if (gspca_dev->usb_buf[0] & 0x04) {
0506             if (gspca_dev->usb_buf[0] & 0x08) {
0507                 dev_err(gspca_dev->v4l2_dev.dev,
0508                     "i2c error writing %8ph\n", buf);
0509                 gspca_dev->usb_err = -EIO;
0510             }
0511             return;
0512         }
0513     }
0514 
0515     dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
0516     gspca_dev->usb_err = -EIO;
0517 }
0518 
0519 static void i2c_w_vector(struct gspca_dev *gspca_dev,
0520             const __u8 buffer[][8], int len)
0521 {
0522     for (;;) {
0523         if (gspca_dev->usb_err < 0)
0524             return;
0525         i2c_w(gspca_dev, *buffer);
0526         len -= 8;
0527         if (len <= 0)
0528             break;
0529         buffer++;
0530     }
0531 }
0532 
0533 static void setbrightness(struct gspca_dev *gspca_dev)
0534 {
0535     struct sd *sd = (struct sd *) gspca_dev;
0536 
0537     switch (sd->sensor) {
0538     case  SENSOR_OV6650:
0539     case  SENSOR_OV7630: {
0540         __u8 i2cOV[] =
0541             {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
0542 
0543         /* change reg 0x06 */
0544         i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
0545         i2cOV[3] = sd->brightness->val;
0546         i2c_w(gspca_dev, i2cOV);
0547         break;
0548     }
0549     case SENSOR_PAS106:
0550     case SENSOR_PAS202: {
0551         __u8 i2cpbright[] =
0552             {0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
0553         __u8 i2cpdoit[] =
0554             {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
0555 
0556         /* PAS106 uses reg 7 and 8 instead of b and c */
0557         if (sd->sensor == SENSOR_PAS106) {
0558             i2cpbright[2] = 7;
0559             i2cpdoit[2] = 0x13;
0560         }
0561 
0562         if (sd->brightness->val < 127) {
0563             /* change reg 0x0b, signreg */
0564             i2cpbright[3] = 0x01;
0565             /* set reg 0x0c, offset */
0566             i2cpbright[4] = 127 - sd->brightness->val;
0567         } else
0568             i2cpbright[4] = sd->brightness->val - 127;
0569 
0570         i2c_w(gspca_dev, i2cpbright);
0571         i2c_w(gspca_dev, i2cpdoit);
0572         break;
0573     }
0574     default:
0575         break;
0576     }
0577 }
0578 
0579 static void setgain(struct gspca_dev *gspca_dev)
0580 {
0581     struct sd *sd = (struct sd *) gspca_dev;
0582     u8 gain = gspca_dev->gain->val;
0583 
0584     switch (sd->sensor) {
0585     case SENSOR_HV7131D: {
0586         __u8 i2c[] =
0587             {0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
0588 
0589         i2c[3] = 0x3f - gain;
0590         i2c[4] = 0x3f - gain;
0591         i2c[5] = 0x3f - gain;
0592 
0593         i2c_w(gspca_dev, i2c);
0594         break;
0595     }
0596     case SENSOR_TAS5110C:
0597     case SENSOR_TAS5130CXX: {
0598         __u8 i2c[] =
0599             {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
0600 
0601         i2c[4] = 255 - gain;
0602         i2c_w(gspca_dev, i2c);
0603         break;
0604     }
0605     case SENSOR_TAS5110D: {
0606         __u8 i2c[] = {
0607             0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
0608         gain = 255 - gain;
0609         /* The bits in the register are the wrong way around!! */
0610         i2c[3] |= (gain & 0x80) >> 7;
0611         i2c[3] |= (gain & 0x40) >> 5;
0612         i2c[3] |= (gain & 0x20) >> 3;
0613         i2c[3] |= (gain & 0x10) >> 1;
0614         i2c[3] |= (gain & 0x08) << 1;
0615         i2c[3] |= (gain & 0x04) << 3;
0616         i2c[3] |= (gain & 0x02) << 5;
0617         i2c[3] |= (gain & 0x01) << 7;
0618         i2c_w(gspca_dev, i2c);
0619         break;
0620     }
0621     case SENSOR_OV6650:
0622     case SENSOR_OV7630: {
0623         __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
0624 
0625         /*
0626          * The ov7630's gain is weird, at 32 the gain drops to the
0627          * same level as at 16, so skip 32-47 (of the 0-63 scale).
0628          */
0629         if (sd->sensor == SENSOR_OV7630 && gain >= 32)
0630             gain += 16;
0631 
0632         i2c[1] = sensor_data[sd->sensor].sensor_addr;
0633         i2c[3] = gain;
0634         i2c_w(gspca_dev, i2c);
0635         break;
0636     }
0637     case SENSOR_PAS106:
0638     case SENSOR_PAS202: {
0639         __u8 i2cpgain[] =
0640             {0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
0641         __u8 i2cpcolorgain[] =
0642             {0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
0643         __u8 i2cpdoit[] =
0644             {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
0645 
0646         /* PAS106 uses different regs (and has split green gains) */
0647         if (sd->sensor == SENSOR_PAS106) {
0648             i2cpgain[2] = 0x0e;
0649             i2cpcolorgain[0] = 0xd0;
0650             i2cpcolorgain[2] = 0x09;
0651             i2cpdoit[2] = 0x13;
0652         }
0653 
0654         i2cpgain[3] = gain;
0655         i2cpcolorgain[3] = gain >> 1;
0656         i2cpcolorgain[4] = gain >> 1;
0657         i2cpcolorgain[5] = gain >> 1;
0658         i2cpcolorgain[6] = gain >> 1;
0659 
0660         i2c_w(gspca_dev, i2cpgain);
0661         i2c_w(gspca_dev, i2cpcolorgain);
0662         i2c_w(gspca_dev, i2cpdoit);
0663         break;
0664     }
0665     default:
0666         if (sd->bridge == BRIDGE_103) {
0667             u8 buf[3] = { gain, gain, gain }; /* R, G, B */
0668             reg_w(gspca_dev, 0x05, buf, 3);
0669         } else {
0670             u8 buf[2];
0671             buf[0] = gain << 4 | gain; /* Red and blue */
0672             buf[1] = gain; /* Green */
0673             reg_w(gspca_dev, 0x10, buf, 2);
0674         }
0675     }
0676 }
0677 
0678 static void setexposure(struct gspca_dev *gspca_dev)
0679 {
0680     struct sd *sd = (struct sd *) gspca_dev;
0681 
0682     switch (sd->sensor) {
0683     case SENSOR_HV7131D: {
0684         /* Note the datasheet wrongly says line mode exposure uses reg
0685            0x26 and 0x27, testing has shown 0x25 + 0x26 */
0686         __u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
0687         u16 reg = gspca_dev->exposure->val;
0688 
0689         i2c[3] = reg >> 8;
0690         i2c[4] = reg & 0xff;
0691         i2c_w(gspca_dev, i2c);
0692         break;
0693     }
0694     case SENSOR_TAS5110C:
0695     case SENSOR_TAS5110D: {
0696         /* register 19's high nibble contains the sn9c10x clock divider
0697            The high nibble configures the no fps according to the
0698            formula: 60 / high_nibble. With a maximum of 30 fps */
0699         u8 reg = gspca_dev->exposure->val;
0700 
0701         reg = (reg << 4) | 0x0b;
0702         reg_w(gspca_dev, 0x19, &reg, 1);
0703         break;
0704     }
0705     case SENSOR_OV6650:
0706     case SENSOR_OV7630: {
0707         /* The ov6650 / ov7630 have 2 registers which both influence
0708            exposure, register 11, whose low nibble sets the nr off fps
0709            according to: fps = 30 / (low_nibble + 1)
0710 
0711            The fps configures the maximum exposure setting, but it is
0712            possible to use less exposure then what the fps maximum
0713            allows by setting register 10. register 10 configures the
0714            actual exposure as quotient of the full exposure, with 0
0715            being no exposure at all (not very useful) and reg10_max
0716            being max exposure possible at that framerate.
0717 
0718            The code maps our 0 - 510 ms exposure ctrl to these 2
0719            registers, trying to keep fps as high as possible.
0720         */
0721         __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
0722         int reg10, reg11, reg10_max;
0723 
0724         /* ov6645 datasheet says reg10_max is 9a, but that uses
0725            tline * 2 * reg10 as formula for calculating texpo, the
0726            ov6650 probably uses the same formula as the 7730 which uses
0727            tline * 4 * reg10, which explains why the reg10max we've
0728            found experimentally for the ov6650 is exactly half that of
0729            the ov6645. The ov7630 datasheet says the max is 0x41. */
0730         if (sd->sensor == SENSOR_OV6650) {
0731             reg10_max = 0x4d;
0732             i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
0733         } else
0734             reg10_max = 0x41;
0735 
0736         reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
0737         if (reg11 < 1)
0738             reg11 = 1;
0739         else if (reg11 > 16)
0740             reg11 = 16;
0741 
0742         /* In 640x480, if the reg11 has less than 4, the image is
0743            unstable (the bridge goes into a higher compression mode
0744            which we have not reverse engineered yet). */
0745         if (gspca_dev->pixfmt.width == 640 && reg11 < 4)
0746             reg11 = 4;
0747 
0748         /* frame exposure time in ms = 1000 * reg11 / 30    ->
0749         reg10 = (gspca_dev->exposure->val / 2) * reg10_max
0750                 / (1000 * reg11 / 30) */
0751         reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
0752                 / (1000 * reg11);
0753 
0754         /* Don't allow this to get below 10 when using autogain, the
0755            steps become very large (relatively) when below 10 causing
0756            the image to oscillate from much too dark, to much too bright
0757            and back again. */
0758         if (gspca_dev->autogain->val && reg10 < 10)
0759             reg10 = 10;
0760         else if (reg10 > reg10_max)
0761             reg10 = reg10_max;
0762 
0763         /* Write reg 10 and reg11 low nibble */
0764         i2c[1] = sensor_data[sd->sensor].sensor_addr;
0765         i2c[3] = reg10;
0766         i2c[4] |= reg11 - 1;
0767 
0768         /* If register 11 didn't change, don't change it */
0769         if (sd->reg11 == reg11)
0770             i2c[0] = 0xa0;
0771 
0772         i2c_w(gspca_dev, i2c);
0773         if (gspca_dev->usb_err == 0)
0774             sd->reg11 = reg11;
0775         break;
0776     }
0777     case SENSOR_PAS202: {
0778         __u8 i2cpframerate[] =
0779             {0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
0780         __u8 i2cpexpo[] =
0781             {0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
0782         const __u8 i2cpdoit[] =
0783             {0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
0784         int framerate_ctrl;
0785 
0786         /* The exposure knee for the autogain algorithm is 200
0787            (100 ms / 10 fps on other sensors), for values below this
0788            use the control for setting the partial frame expose time,
0789            above that use variable framerate. This way we run at max
0790            framerate (640x480@7.5 fps, 320x240@10fps) until the knee
0791            is reached. Using the variable framerate control above 200
0792            is better then playing around with both clockdiv + partial
0793            frame exposure times (like we are doing with the ov chips),
0794            as that sometimes leads to jumps in the exposure control,
0795            which are bad for auto exposure. */
0796         if (gspca_dev->exposure->val < 200) {
0797             i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
0798                         / 200;
0799             framerate_ctrl = 500;
0800         } else {
0801             /* The PAS202's exposure control goes from 0 - 4095,
0802                but anything below 500 causes vsync issues, so scale
0803                our 200-1023 to 500-4095 */
0804             framerate_ctrl = (gspca_dev->exposure->val - 200)
0805                             * 1000 / 229 +  500;
0806         }
0807 
0808         i2cpframerate[3] = framerate_ctrl >> 6;
0809         i2cpframerate[4] = framerate_ctrl & 0x3f;
0810         i2c_w(gspca_dev, i2cpframerate);
0811         i2c_w(gspca_dev, i2cpexpo);
0812         i2c_w(gspca_dev, i2cpdoit);
0813         break;
0814     }
0815     case SENSOR_PAS106: {
0816         __u8 i2cpframerate[] =
0817             {0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
0818         __u8 i2cpexpo[] =
0819             {0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
0820         const __u8 i2cpdoit[] =
0821             {0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
0822         int framerate_ctrl;
0823 
0824         /* For values below 150 use partial frame exposure, above
0825            that use framerate ctrl */
0826         if (gspca_dev->exposure->val < 150) {
0827             i2cpexpo[3] = 150 - gspca_dev->exposure->val;
0828             framerate_ctrl = 300;
0829         } else {
0830             /* The PAS106's exposure control goes from 0 - 4095,
0831                but anything below 300 causes vsync issues, so scale
0832                our 150-1023 to 300-4095 */
0833             framerate_ctrl = (gspca_dev->exposure->val - 150)
0834                         * 1000 / 230 + 300;
0835         }
0836 
0837         i2cpframerate[3] = framerate_ctrl >> 4;
0838         i2cpframerate[4] = framerate_ctrl & 0x0f;
0839         i2c_w(gspca_dev, i2cpframerate);
0840         i2c_w(gspca_dev, i2cpexpo);
0841         i2c_w(gspca_dev, i2cpdoit);
0842         break;
0843     }
0844     default:
0845         break;
0846     }
0847 }
0848 
0849 static void setfreq(struct gspca_dev *gspca_dev)
0850 {
0851     struct sd *sd = (struct sd *) gspca_dev;
0852 
0853     if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
0854         /* Framerate adjust register for artificial light 50 hz flicker
0855            compensation, for the ov6650 this is identical to ov6630
0856            0x2b register, see ov6630 datasheet.
0857            0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
0858         __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
0859         switch (sd->plfreq->val) {
0860         default:
0861 /*      case 0:          * no filter*/
0862 /*      case 2:          * 60 hz */
0863             i2c[3] = 0;
0864             break;
0865         case 1:         /* 50 hz */
0866             i2c[3] = (sd->sensor == SENSOR_OV6650)
0867                     ? 0x4f : 0x8a;
0868             break;
0869         }
0870         i2c[1] = sensor_data[sd->sensor].sensor_addr;
0871         i2c_w(gspca_dev, i2c);
0872     }
0873 }
0874 
0875 static void do_autogain(struct gspca_dev *gspca_dev)
0876 {
0877     struct sd *sd = (struct sd *) gspca_dev;
0878     int deadzone, desired_avg_lum, avg_lum;
0879 
0880     avg_lum = atomic_read(&sd->avg_lum);
0881     if (avg_lum == -1)
0882         return;
0883 
0884     if (sd->autogain_ignore_frames > 0) {
0885         sd->autogain_ignore_frames--;
0886         return;
0887     }
0888 
0889     /* SIF / VGA sensors have a different autoexposure area and thus
0890        different avg_lum values for the same picture brightness */
0891     if (sensor_data[sd->sensor].flags & F_SIF) {
0892         deadzone = 500;
0893         /* SIF sensors tend to overexpose, so keep this small */
0894         desired_avg_lum = 5000;
0895     } else {
0896         deadzone = 1500;
0897         desired_avg_lum = 13000;
0898     }
0899 
0900     if (sd->brightness)
0901         desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
0902 
0903     if (gspca_dev->exposure->maximum < 500) {
0904         if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
0905                 desired_avg_lum, deadzone))
0906             sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
0907     } else {
0908         int gain_knee = (s32)gspca_dev->gain->maximum * 9 / 10;
0909         if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
0910                 deadzone, gain_knee, sd->exposure_knee))
0911             sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
0912     }
0913 }
0914 
0915 /* this function is called at probe time */
0916 static int sd_config(struct gspca_dev *gspca_dev,
0917             const struct usb_device_id *id)
0918 {
0919     struct sd *sd = (struct sd *) gspca_dev;
0920     struct cam *cam;
0921 
0922     reg_r(gspca_dev, 0x00);
0923     if (gspca_dev->usb_buf[0] != 0x10)
0924         return -ENODEV;
0925 
0926     /* copy the webcam info from the device id */
0927     sd->sensor = id->driver_info >> 8;
0928     sd->bridge = id->driver_info & 0xff;
0929 
0930     cam = &gspca_dev->cam;
0931     if (!(sensor_data[sd->sensor].flags & F_SIF)) {
0932         cam->cam_mode = vga_mode;
0933         cam->nmodes = ARRAY_SIZE(vga_mode);
0934     } else {
0935         cam->cam_mode = sif_mode;
0936         cam->nmodes = ARRAY_SIZE(sif_mode);
0937     }
0938     cam->npkt = 36;         /* 36 packets per ISOC message */
0939 
0940     return 0;
0941 }
0942 
0943 /* this function is called at probe and resume time */
0944 static int sd_init(struct gspca_dev *gspca_dev)
0945 {
0946     const __u8 stop = 0x09; /* Disable stream turn of LED */
0947 
0948     reg_w(gspca_dev, 0x01, &stop, 1);
0949 
0950     return gspca_dev->usb_err;
0951 }
0952 
0953 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
0954 {
0955     struct gspca_dev *gspca_dev =
0956         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
0957     struct sd *sd = (struct sd *)gspca_dev;
0958 
0959     gspca_dev->usb_err = 0;
0960 
0961     if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
0962         /* when switching to autogain set defaults to make sure
0963            we are on a valid point of the autogain gain /
0964            exposure knee graph, and give this change time to
0965            take effect before doing autogain. */
0966         gspca_dev->gain->val = gspca_dev->gain->default_value;
0967         gspca_dev->exposure->val = gspca_dev->exposure->default_value;
0968         sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
0969     }
0970 
0971     if (!gspca_dev->streaming)
0972         return 0;
0973 
0974     switch (ctrl->id) {
0975     case V4L2_CID_BRIGHTNESS:
0976         setbrightness(gspca_dev);
0977         break;
0978     case V4L2_CID_AUTOGAIN:
0979         if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
0980             setexposure(gspca_dev);
0981         if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
0982             setgain(gspca_dev);
0983         break;
0984     case V4L2_CID_POWER_LINE_FREQUENCY:
0985         setfreq(gspca_dev);
0986         break;
0987     default:
0988         return -EINVAL;
0989     }
0990     return gspca_dev->usb_err;
0991 }
0992 
0993 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
0994     .s_ctrl = sd_s_ctrl,
0995 };
0996 
0997 /* this function is called at probe time */
0998 static int sd_init_controls(struct gspca_dev *gspca_dev)
0999 {
1000     struct sd *sd = (struct sd *) gspca_dev;
1001     struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1002 
1003     gspca_dev->vdev.ctrl_handler = hdl;
1004     v4l2_ctrl_handler_init(hdl, 5);
1005 
1006     if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1007         sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1008         sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1009                     V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1010 
1011     /* Gain range is sensor dependent */
1012     switch (sd->sensor) {
1013     case SENSOR_OV6650:
1014     case SENSOR_PAS106:
1015     case SENSOR_PAS202:
1016         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1017                     V4L2_CID_GAIN, 0, 31, 1, 15);
1018         break;
1019     case SENSOR_OV7630:
1020         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1021                     V4L2_CID_GAIN, 0, 47, 1, 31);
1022         break;
1023     case SENSOR_HV7131D:
1024         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1025                     V4L2_CID_GAIN, 0, 63, 1, 31);
1026         break;
1027     case SENSOR_TAS5110C:
1028     case SENSOR_TAS5110D:
1029     case SENSOR_TAS5130CXX:
1030         gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1031                     V4L2_CID_GAIN, 0, 255, 1, 127);
1032         break;
1033     default:
1034         if (sd->bridge == BRIDGE_103) {
1035             gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1036                         V4L2_CID_GAIN, 0, 127, 1, 63);
1037         } else {
1038             gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1039                         V4L2_CID_GAIN, 0, 15, 1, 7);
1040         }
1041     }
1042 
1043     /* Exposure range is sensor dependent, and not all have exposure */
1044     switch (sd->sensor) {
1045     case SENSOR_HV7131D:
1046         gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1047                     V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1048         sd->exposure_knee = 964;
1049         break;
1050     case SENSOR_OV6650:
1051     case SENSOR_OV7630:
1052     case SENSOR_PAS106:
1053     case SENSOR_PAS202:
1054         gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1055                     V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1056         sd->exposure_knee = 200;
1057         break;
1058     case SENSOR_TAS5110C:
1059     case SENSOR_TAS5110D:
1060         gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1061                     V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1062         break;
1063     }
1064 
1065     if (gspca_dev->exposure) {
1066         gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1067                         V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1068     }
1069 
1070     if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1071         sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1072             V4L2_CID_POWER_LINE_FREQUENCY,
1073             V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1074             V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1075 
1076     if (hdl->error) {
1077         pr_err("Could not initialize controls\n");
1078         return hdl->error;
1079     }
1080 
1081     if (gspca_dev->autogain)
1082         v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1083 
1084     return 0;
1085 }
1086 
1087 /* -- start the camera -- */
1088 static int sd_start(struct gspca_dev *gspca_dev)
1089 {
1090     struct sd *sd = (struct sd *) gspca_dev;
1091     struct cam *cam = &gspca_dev->cam;
1092     int i, mode;
1093     __u8 regs[0x31];
1094 
1095     mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1096     /* Copy registers 0x01 - 0x19 from the template */
1097     memcpy(&regs[0x01], sensor_data[sd->sensor].bridge_init, 0x19);
1098     /* Set the mode */
1099     regs[0x18] |= mode << 4;
1100 
1101     /* Set bridge gain to 1.0 */
1102     if (sd->bridge == BRIDGE_103) {
1103         regs[0x05] = 0x20; /* Red */
1104         regs[0x06] = 0x20; /* Green */
1105         regs[0x07] = 0x20; /* Blue */
1106     } else {
1107         regs[0x10] = 0x00; /* Red and blue */
1108         regs[0x11] = 0x00; /* Green */
1109     }
1110 
1111     /* Setup pixel numbers and auto exposure window */
1112     if (sensor_data[sd->sensor].flags & F_SIF) {
1113         regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
1114         regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
1115         regs[0x1c] = 0x02; /* AE H-start 64 */
1116         regs[0x1d] = 0x02; /* AE V-start 64 */
1117         regs[0x1e] = 0x09; /* AE H-end 288 */
1118         regs[0x1f] = 0x07; /* AE V-end 224 */
1119     } else {
1120         regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
1121         regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
1122         regs[0x1c] = 0x05; /* AE H-start 160 */
1123         regs[0x1d] = 0x03; /* AE V-start 96 */
1124         regs[0x1e] = 0x0f; /* AE H-end 480 */
1125         regs[0x1f] = 0x0c; /* AE V-end 384 */
1126     }
1127 
1128     /* Setup the gamma table (only used with the sn9c103 bridge) */
1129     for (i = 0; i < 16; i++)
1130         regs[0x20 + i] = i * 16;
1131     regs[0x20 + i] = 255;
1132 
1133     /* Special cases where some regs depend on mode or bridge */
1134     switch (sd->sensor) {
1135     case SENSOR_TAS5130CXX:
1136         /* FIXME / TESTME
1137            probably not mode specific at all most likely the upper
1138            nibble of 0x19 is exposure (clock divider) just as with
1139            the tas5110, we need someone to test this. */
1140         regs[0x19] = mode ? 0x23 : 0x43;
1141         break;
1142     case SENSOR_OV7630:
1143         /* FIXME / TESTME for some reason with the 101/102 bridge the
1144            clock is set to 12 Mhz (reg1 == 0x04), rather then 24.
1145            Also the hstart needs to go from 1 to 2 when using a 103,
1146            which is likely related. This does not seem right. */
1147         if (sd->bridge == BRIDGE_103) {
1148             regs[0x01] = 0x44; /* Select 24 Mhz clock */
1149             regs[0x12] = 0x02; /* Set hstart to 2 */
1150         }
1151         break;
1152     case SENSOR_PAS202:
1153         /* For some unknown reason we need to increase hstart by 1 on
1154            the sn9c103, otherwise we get wrong colors (bayer shift). */
1155         if (sd->bridge == BRIDGE_103)
1156             regs[0x12] += 1;
1157         break;
1158     }
1159     /* Disable compression when the raw bayer format has been selected */
1160     if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1161         regs[0x18] &= ~0x80;
1162 
1163     /* Vga mode emulation on SIF sensor? */
1164     if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1165         regs[0x12] += 16;   /* hstart adjust */
1166         regs[0x13] += 24;   /* vstart adjust */
1167         regs[0x15]  = 320 / 16; /* hsize */
1168         regs[0x16]  = 240 / 16; /* vsize */
1169     }
1170 
1171     /* reg 0x01 bit 2 video transfert on */
1172     reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1173     /* reg 0x17 SensorClk enable inv Clk 0x60 */
1174     reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1175     /* Set the registers from the template */
1176     reg_w(gspca_dev, 0x01, &regs[0x01],
1177           (sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);
1178 
1179     /* Init the sensor */
1180     i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1181             sensor_data[sd->sensor].sensor_init_size);
1182 
1183     /* Mode / bridge specific sensor setup */
1184     switch (sd->sensor) {
1185     case SENSOR_PAS202: {
1186         const __u8 i2cpclockdiv[] =
1187             {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1188         /* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1189         if (mode)
1190             i2c_w(gspca_dev, i2cpclockdiv);
1191         break;
1192         }
1193     case SENSOR_OV7630:
1194         /* FIXME / TESTME We should be able to handle this identical
1195            for the 101/102 and the 103 case */
1196         if (sd->bridge == BRIDGE_103) {
1197             const __u8 i2c[] = { 0xa0, 0x21, 0x13,
1198                          0x80, 0x00, 0x00, 0x00, 0x10 };
1199             i2c_w(gspca_dev, i2c);
1200         }
1201         break;
1202     }
1203     /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1204     reg_w(gspca_dev, 0x15, &regs[0x15], 2);
1205     /* compression register */
1206     reg_w(gspca_dev, 0x18, &regs[0x18], 1);
1207     /* H_start */
1208     reg_w(gspca_dev, 0x12, &regs[0x12], 1);
1209     /* V_START */
1210     reg_w(gspca_dev, 0x13, &regs[0x13], 1);
1211     /* reset 0x17 SensorClk enable inv Clk 0x60 */
1212                 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1213     reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1214     /*MCKSIZE ->3 */    /*fixme: not ov7630*/
1215     reg_w(gspca_dev, 0x19, &regs[0x19], 1);
1216     /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1217     reg_w(gspca_dev, 0x1c, &regs[0x1c], 4);
1218     /* Enable video transfert */
1219     reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1220     /* Compression */
1221     reg_w(gspca_dev, 0x18, &regs[0x18], 2);
1222     msleep(20);
1223 
1224     sd->reg11 = -1;
1225 
1226     setgain(gspca_dev);
1227     setbrightness(gspca_dev);
1228     setexposure(gspca_dev);
1229     setfreq(gspca_dev);
1230 
1231     sd->frames_to_drop = 0;
1232     sd->autogain_ignore_frames = 0;
1233     gspca_dev->exp_too_high_cnt = 0;
1234     gspca_dev->exp_too_low_cnt = 0;
1235     atomic_set(&sd->avg_lum, -1);
1236     return gspca_dev->usb_err;
1237 }
1238 
1239 static void sd_stopN(struct gspca_dev *gspca_dev)
1240 {
1241     sd_init(gspca_dev);
1242 }
1243 
1244 static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)
1245 {
1246     struct sd *sd = (struct sd *) gspca_dev;
1247     int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;
1248 
1249     /* frames start with:
1250      *  ff ff 00 c4 c4 96   synchro
1251      *  00      (unknown)
1252      *  xx      (frame sequence / size / compression)
1253      *  (xx)        (idem - extra byte for sn9c103)
1254      *  ll mm       brightness sum inside auto exposure
1255      *  ll mm       brightness sum outside auto exposure
1256      *  (xx xx xx xx xx)    audio values for snc103
1257      */
1258     for (i = 0; i < len; i++) {
1259         switch (sd->header_read) {
1260         case 0:
1261             if (data[i] == 0xff)
1262                 sd->header_read++;
1263             break;
1264         case 1:
1265             if (data[i] == 0xff)
1266                 sd->header_read++;
1267             else
1268                 sd->header_read = 0;
1269             break;
1270         case 2:
1271             if (data[i] == 0x00)
1272                 sd->header_read++;
1273             else if (data[i] != 0xff)
1274                 sd->header_read = 0;
1275             break;
1276         case 3:
1277             if (data[i] == 0xc4)
1278                 sd->header_read++;
1279             else if (data[i] == 0xff)
1280                 sd->header_read = 1;
1281             else
1282                 sd->header_read = 0;
1283             break;
1284         case 4:
1285             if (data[i] == 0xc4)
1286                 sd->header_read++;
1287             else if (data[i] == 0xff)
1288                 sd->header_read = 1;
1289             else
1290                 sd->header_read = 0;
1291             break;
1292         case 5:
1293             if (data[i] == 0x96)
1294                 sd->header_read++;
1295             else if (data[i] == 0xff)
1296                 sd->header_read = 1;
1297             else
1298                 sd->header_read = 0;
1299             break;
1300         default:
1301             sd->header[sd->header_read - 6] = data[i];
1302             sd->header_read++;
1303             if (sd->header_read == header_size) {
1304                 sd->header_read = 0;
1305                 return data + i + 1;
1306             }
1307         }
1308     }
1309     return NULL;
1310 }
1311 
1312 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1313             u8 *data,           /* isoc packet */
1314             int len)            /* iso packet length */
1315 {
1316     int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;
1317     struct sd *sd = (struct sd *) gspca_dev;
1318     struct cam *cam = &gspca_dev->cam;
1319     u8 *sof;
1320 
1321     sof = find_sof(gspca_dev, data, len);
1322     if (sof) {
1323         if (sd->bridge == BRIDGE_103) {
1324             fr_h_sz = 18;
1325             lum_offset = 3;
1326         } else {
1327             fr_h_sz = 12;
1328             lum_offset = 2;
1329         }
1330 
1331         len_after_sof = len - (sof - data);
1332         len = (sof - data) - fr_h_sz;
1333         if (len < 0)
1334             len = 0;
1335     }
1336 
1337     if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1338         /* In raw mode we sometimes get some garbage after the frame
1339            ignore this */
1340         int used;
1341         int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1342 
1343         used = gspca_dev->image_len;
1344         if (used + len > size)
1345             len = size - used;
1346     }
1347 
1348     gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1349 
1350     if (sof) {
1351         int  lum = sd->header[lum_offset] +
1352               (sd->header[lum_offset + 1] << 8);
1353 
1354         /* When exposure changes midway a frame we
1355            get a lum of 0 in this case drop 2 frames
1356            as the frames directly after an exposure
1357            change have an unstable image. Sometimes lum
1358            *really* is 0 (cam used in low light with
1359            low exposure setting), so do not drop frames
1360            if the previous lum was 0 too. */
1361         if (lum == 0 && sd->prev_avg_lum != 0) {
1362             lum = -1;
1363             sd->frames_to_drop = 2;
1364             sd->prev_avg_lum = 0;
1365         } else
1366             sd->prev_avg_lum = lum;
1367         atomic_set(&sd->avg_lum, lum);
1368 
1369         if (sd->frames_to_drop)
1370             sd->frames_to_drop--;
1371         else
1372             gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1373 
1374         gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);
1375     }
1376 }
1377 
1378 #if IS_ENABLED(CONFIG_INPUT)
1379 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1380             u8 *data,       /* interrupt packet data */
1381             int len)        /* interrupt packet length */
1382 {
1383     int ret = -EINVAL;
1384 
1385     if (len == 1 && data[0] == 1) {
1386         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1387         input_sync(gspca_dev->input_dev);
1388         input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1389         input_sync(gspca_dev->input_dev);
1390         ret = 0;
1391     }
1392 
1393     return ret;
1394 }
1395 #endif
1396 
1397 /* sub-driver description */
1398 static const struct sd_desc sd_desc = {
1399     .name = MODULE_NAME,
1400     .config = sd_config,
1401     .init = sd_init,
1402     .init_controls = sd_init_controls,
1403     .start = sd_start,
1404     .stopN = sd_stopN,
1405     .pkt_scan = sd_pkt_scan,
1406     .dq_callback = do_autogain,
1407 #if IS_ENABLED(CONFIG_INPUT)
1408     .int_pkt_scan = sd_int_pkt_scan,
1409 #endif
1410 };
1411 
1412 /* -- module initialisation -- */
1413 #define SB(sensor, bridge) \
1414     .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1415 
1416 
1417 static const struct usb_device_id device_table[] = {
1418     {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1419     {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1420     {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1421     {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1422     {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1423     {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1424     {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1425     {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1426     {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1427     {USB_DEVICE(0x0c45, 0x6027), SB(OV7630, 101)}, /* Genius Eye 310 */
1428     {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1429     {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1430     {USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},
1431     /* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */
1432     {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1433     {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1434     {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1435     /* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */
1436     /* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */
1437     {USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},
1438     {USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},
1439     /* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */
1440     {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1441     {USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},
1442     {USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},
1443     {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1444     {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1445     {}
1446 };
1447 MODULE_DEVICE_TABLE(usb, device_table);
1448 
1449 /* -- device connect -- */
1450 static int sd_probe(struct usb_interface *intf,
1451             const struct usb_device_id *id)
1452 {
1453     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1454                 THIS_MODULE);
1455 }
1456 
1457 static struct usb_driver sd_driver = {
1458     .name = MODULE_NAME,
1459     .id_table = device_table,
1460     .probe = sd_probe,
1461     .disconnect = gspca_disconnect,
1462 #ifdef CONFIG_PM
1463     .suspend = gspca_suspend,
1464     .resume = gspca_resume,
1465     .reset_resume = gspca_resume,
1466 #endif
1467 };
1468 
1469 module_usb_driver(sd_driver);