Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ov534-ov7xxx gspca driver
0004  *
0005  * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
0006  * Copyright (C) 2008 Jim Paris <jim@jtan.com>
0007  * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
0008  *
0009  * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
0010  * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
0011  * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
0012  *
0013  * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
0014  * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
0015  *                  added by Max Thrun <bear24rw@gmail.com>
0016  * PS3 Eye camera - FPS range extended by Joseph Howse
0017  *                  <josephhowse@nummist.com> https://nummist.com
0018  */
0019 
0020 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0021 
0022 #define MODULE_NAME "ov534"
0023 
0024 #include "gspca.h"
0025 
0026 #include <linux/fixp-arith.h>
0027 #include <media/v4l2-ctrls.h>
0028 
0029 #define OV534_REG_ADDRESS   0xf1    /* sensor address */
0030 #define OV534_REG_SUBADDR   0xf2
0031 #define OV534_REG_WRITE     0xf3
0032 #define OV534_REG_READ      0xf4
0033 #define OV534_REG_OPERATION 0xf5
0034 #define OV534_REG_STATUS    0xf6
0035 
0036 #define OV534_OP_WRITE_3    0x37
0037 #define OV534_OP_WRITE_2    0x33
0038 #define OV534_OP_READ_2     0xf9
0039 
0040 #define CTRL_TIMEOUT 500
0041 #define DEFAULT_FRAME_RATE 30
0042 
0043 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
0044 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
0045 MODULE_LICENSE("GPL");
0046 
0047 /* specific webcam descriptor */
0048 struct sd {
0049     struct gspca_dev gspca_dev; /* !! must be the first item */
0050 
0051     struct v4l2_ctrl_handler ctrl_handler;
0052     struct v4l2_ctrl *hue;
0053     struct v4l2_ctrl *saturation;
0054     struct v4l2_ctrl *brightness;
0055     struct v4l2_ctrl *contrast;
0056     struct { /* gain control cluster */
0057         struct v4l2_ctrl *autogain;
0058         struct v4l2_ctrl *gain;
0059     };
0060     struct v4l2_ctrl *autowhitebalance;
0061     struct { /* exposure control cluster */
0062         struct v4l2_ctrl *autoexposure;
0063         struct v4l2_ctrl *exposure;
0064     };
0065     struct v4l2_ctrl *sharpness;
0066     struct v4l2_ctrl *hflip;
0067     struct v4l2_ctrl *vflip;
0068     struct v4l2_ctrl *plfreq;
0069 
0070     __u32 last_pts;
0071     u16 last_fid;
0072     u8 frame_rate;
0073 
0074     u8 sensor;
0075 };
0076 enum sensors {
0077     SENSOR_OV767x,
0078     SENSOR_OV772x,
0079     NSENSORS
0080 };
0081 
0082 static int sd_start(struct gspca_dev *gspca_dev);
0083 static void sd_stopN(struct gspca_dev *gspca_dev);
0084 
0085 
0086 static const struct v4l2_pix_format ov772x_mode[] = {
0087     {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
0088      .bytesperline = 320 * 2,
0089      .sizeimage = 320 * 240 * 2,
0090      .colorspace = V4L2_COLORSPACE_SRGB,
0091      .priv = 1},
0092     {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
0093      .bytesperline = 640 * 2,
0094      .sizeimage = 640 * 480 * 2,
0095      .colorspace = V4L2_COLORSPACE_SRGB,
0096      .priv = 0},
0097     {320, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
0098      .bytesperline = 320,
0099      .sizeimage = 320 * 240,
0100      .colorspace = V4L2_COLORSPACE_SRGB,
0101      .priv = 1},
0102     {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
0103      .bytesperline = 640,
0104      .sizeimage = 640 * 480,
0105      .colorspace = V4L2_COLORSPACE_SRGB,
0106      .priv = 0},
0107 };
0108 static const struct v4l2_pix_format ov767x_mode[] = {
0109     {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
0110         .bytesperline = 320,
0111         .sizeimage = 320 * 240 * 3 / 8 + 590,
0112         .colorspace = V4L2_COLORSPACE_JPEG},
0113     {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
0114         .bytesperline = 640,
0115         .sizeimage = 640 * 480 * 3 / 8 + 590,
0116         .colorspace = V4L2_COLORSPACE_JPEG},
0117 };
0118 
0119 static const u8 qvga_rates[] = {187, 150, 137, 125, 100, 75, 60, 50, 37, 30};
0120 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
0121 
0122 static const struct framerates ov772x_framerates[] = {
0123     { /* 320x240 */
0124         .rates = qvga_rates,
0125         .nrates = ARRAY_SIZE(qvga_rates),
0126     },
0127     { /* 640x480 */
0128         .rates = vga_rates,
0129         .nrates = ARRAY_SIZE(vga_rates),
0130     },
0131     { /* 320x240 SGBRG8 */
0132         .rates = qvga_rates,
0133         .nrates = ARRAY_SIZE(qvga_rates),
0134     },
0135     { /* 640x480 SGBRG8 */
0136         .rates = vga_rates,
0137         .nrates = ARRAY_SIZE(vga_rates),
0138     },
0139 };
0140 
0141 struct reg_array {
0142     const u8 (*val)[2];
0143     int len;
0144 };
0145 
0146 static const u8 bridge_init_767x[][2] = {
0147 /* comments from the ms-win file apollo7670.set */
0148 /* str1 */
0149     {0xf1, 0x42},
0150     {0x88, 0xf8},
0151     {0x89, 0xff},
0152     {0x76, 0x03},
0153     {0x92, 0x03},
0154     {0x95, 0x10},
0155     {0xe2, 0x00},
0156     {0xe7, 0x3e},
0157     {0x8d, 0x1c},
0158     {0x8e, 0x00},
0159     {0x8f, 0x00},
0160     {0x1f, 0x00},
0161     {0xc3, 0xf9},
0162     {0x89, 0xff},
0163     {0x88, 0xf8},
0164     {0x76, 0x03},
0165     {0x92, 0x01},
0166     {0x93, 0x18},
0167     {0x1c, 0x00},
0168     {0x1d, 0x48},
0169     {0x1d, 0x00},
0170     {0x1d, 0xff},
0171     {0x1d, 0x02},
0172     {0x1d, 0x58},
0173     {0x1d, 0x00},
0174     {0x1c, 0x0a},
0175     {0x1d, 0x0a},
0176     {0x1d, 0x0e},
0177     {0xc0, 0x50},   /* HSize 640 */
0178     {0xc1, 0x3c},   /* VSize 480 */
0179     {0x34, 0x05},   /* enable Audio Suspend mode */
0180     {0xc2, 0x0c},   /* Input YUV */
0181     {0xc3, 0xf9},   /* enable PRE */
0182     {0x34, 0x05},   /* enable Audio Suspend mode */
0183     {0xe7, 0x2e},   /* this solves failure of "SuspendResumeTest" */
0184     {0x31, 0xf9},   /* enable 1.8V Suspend */
0185     {0x35, 0x02},   /* turn on JPEG */
0186     {0xd9, 0x10},
0187     {0x25, 0x42},   /* GPIO[8]:Input */
0188     {0x94, 0x11},   /* If the default setting is loaded when
0189              * system boots up, this flag is closed here */
0190 };
0191 static const u8 sensor_init_767x[][2] = {
0192     {0x12, 0x80},
0193     {0x11, 0x03},
0194     {0x3a, 0x04},
0195     {0x12, 0x00},
0196     {0x17, 0x13},
0197     {0x18, 0x01},
0198     {0x32, 0xb6},
0199     {0x19, 0x02},
0200     {0x1a, 0x7a},
0201     {0x03, 0x0a},
0202     {0x0c, 0x00},
0203     {0x3e, 0x00},
0204     {0x70, 0x3a},
0205     {0x71, 0x35},
0206     {0x72, 0x11},
0207     {0x73, 0xf0},
0208     {0xa2, 0x02},
0209     {0x7a, 0x2a},   /* set Gamma=1.6 below */
0210     {0x7b, 0x12},
0211     {0x7c, 0x1d},
0212     {0x7d, 0x2d},
0213     {0x7e, 0x45},
0214     {0x7f, 0x50},
0215     {0x80, 0x59},
0216     {0x81, 0x62},
0217     {0x82, 0x6b},
0218     {0x83, 0x73},
0219     {0x84, 0x7b},
0220     {0x85, 0x8a},
0221     {0x86, 0x98},
0222     {0x87, 0xb2},
0223     {0x88, 0xca},
0224     {0x89, 0xe0},
0225     {0x13, 0xe0},
0226     {0x00, 0x00},
0227     {0x10, 0x00},
0228     {0x0d, 0x40},
0229     {0x14, 0x38},   /* gain max 16x */
0230     {0xa5, 0x05},
0231     {0xab, 0x07},
0232     {0x24, 0x95},
0233     {0x25, 0x33},
0234     {0x26, 0xe3},
0235     {0x9f, 0x78},
0236     {0xa0, 0x68},
0237     {0xa1, 0x03},
0238     {0xa6, 0xd8},
0239     {0xa7, 0xd8},
0240     {0xa8, 0xf0},
0241     {0xa9, 0x90},
0242     {0xaa, 0x94},
0243     {0x13, 0xe5},
0244     {0x0e, 0x61},
0245     {0x0f, 0x4b},
0246     {0x16, 0x02},
0247     {0x21, 0x02},
0248     {0x22, 0x91},
0249     {0x29, 0x07},
0250     {0x33, 0x0b},
0251     {0x35, 0x0b},
0252     {0x37, 0x1d},
0253     {0x38, 0x71},
0254     {0x39, 0x2a},
0255     {0x3c, 0x78},
0256     {0x4d, 0x40},
0257     {0x4e, 0x20},
0258     {0x69, 0x00},
0259     {0x6b, 0x4a},
0260     {0x74, 0x10},
0261     {0x8d, 0x4f},
0262     {0x8e, 0x00},
0263     {0x8f, 0x00},
0264     {0x90, 0x00},
0265     {0x91, 0x00},
0266     {0x96, 0x00},
0267     {0x9a, 0x80},
0268     {0xb0, 0x84},
0269     {0xb1, 0x0c},
0270     {0xb2, 0x0e},
0271     {0xb3, 0x82},
0272     {0xb8, 0x0a},
0273     {0x43, 0x0a},
0274     {0x44, 0xf0},
0275     {0x45, 0x34},
0276     {0x46, 0x58},
0277     {0x47, 0x28},
0278     {0x48, 0x3a},
0279     {0x59, 0x88},
0280     {0x5a, 0x88},
0281     {0x5b, 0x44},
0282     {0x5c, 0x67},
0283     {0x5d, 0x49},
0284     {0x5e, 0x0e},
0285     {0x6c, 0x0a},
0286     {0x6d, 0x55},
0287     {0x6e, 0x11},
0288     {0x6f, 0x9f},
0289     {0x6a, 0x40},
0290     {0x01, 0x40},
0291     {0x02, 0x40},
0292     {0x13, 0xe7},
0293     {0x4f, 0x80},
0294     {0x50, 0x80},
0295     {0x51, 0x00},
0296     {0x52, 0x22},
0297     {0x53, 0x5e},
0298     {0x54, 0x80},
0299     {0x58, 0x9e},
0300     {0x41, 0x08},
0301     {0x3f, 0x00},
0302     {0x75, 0x04},
0303     {0x76, 0xe1},
0304     {0x4c, 0x00},
0305     {0x77, 0x01},
0306     {0x3d, 0xc2},
0307     {0x4b, 0x09},
0308     {0xc9, 0x60},
0309     {0x41, 0x38},   /* jfm: auto sharpness + auto de-noise  */
0310     {0x56, 0x40},
0311     {0x34, 0x11},
0312     {0x3b, 0xc2},
0313     {0xa4, 0x8a},   /* Night mode trigger point */
0314     {0x96, 0x00},
0315     {0x97, 0x30},
0316     {0x98, 0x20},
0317     {0x99, 0x20},
0318     {0x9a, 0x84},
0319     {0x9b, 0x29},
0320     {0x9c, 0x03},
0321     {0x9d, 0x4c},
0322     {0x9e, 0x3f},
0323     {0x78, 0x04},
0324     {0x79, 0x01},
0325     {0xc8, 0xf0},
0326     {0x79, 0x0f},
0327     {0xc8, 0x00},
0328     {0x79, 0x10},
0329     {0xc8, 0x7e},
0330     {0x79, 0x0a},
0331     {0xc8, 0x80},
0332     {0x79, 0x0b},
0333     {0xc8, 0x01},
0334     {0x79, 0x0c},
0335     {0xc8, 0x0f},
0336     {0x79, 0x0d},
0337     {0xc8, 0x20},
0338     {0x79, 0x09},
0339     {0xc8, 0x80},
0340     {0x79, 0x02},
0341     {0xc8, 0xc0},
0342     {0x79, 0x03},
0343     {0xc8, 0x20},
0344     {0x79, 0x26},
0345 };
0346 static const u8 bridge_start_vga_767x[][2] = {
0347 /* str59 JPG */
0348     {0x94, 0xaa},
0349     {0xf1, 0x42},
0350     {0xe5, 0x04},
0351     {0xc0, 0x50},
0352     {0xc1, 0x3c},
0353     {0xc2, 0x0c},
0354     {0x35, 0x02},   /* turn on JPEG */
0355     {0xd9, 0x10},
0356     {0xda, 0x00},   /* for higher clock rate(30fps) */
0357     {0x34, 0x05},   /* enable Audio Suspend mode */
0358     {0xc3, 0xf9},   /* enable PRE */
0359     {0x8c, 0x00},   /* CIF VSize LSB[2:0] */
0360     {0x8d, 0x1c},   /* output YUV */
0361 /*  {0x34, 0x05},    * enable Audio Suspend mode (?) */
0362     {0x50, 0x00},   /* H/V divider=0 */
0363     {0x51, 0xa0},   /* input H=640/4 */
0364     {0x52, 0x3c},   /* input V=480/4 */
0365     {0x53, 0x00},   /* offset X=0 */
0366     {0x54, 0x00},   /* offset Y=0 */
0367     {0x55, 0x00},   /* H/V size[8]=0 */
0368     {0x57, 0x00},   /* H-size[9]=0 */
0369     {0x5c, 0x00},   /* output size[9:8]=0 */
0370     {0x5a, 0xa0},   /* output H=640/4 */
0371     {0x5b, 0x78},   /* output V=480/4 */
0372     {0x1c, 0x0a},
0373     {0x1d, 0x0a},
0374     {0x94, 0x11},
0375 };
0376 static const u8 sensor_start_vga_767x[][2] = {
0377     {0x11, 0x01},
0378     {0x1e, 0x04},
0379     {0x19, 0x02},
0380     {0x1a, 0x7a},
0381 };
0382 static const u8 bridge_start_qvga_767x[][2] = {
0383 /* str86 JPG */
0384     {0x94, 0xaa},
0385     {0xf1, 0x42},
0386     {0xe5, 0x04},
0387     {0xc0, 0x80},
0388     {0xc1, 0x60},
0389     {0xc2, 0x0c},
0390     {0x35, 0x02},   /* turn on JPEG */
0391     {0xd9, 0x10},
0392     {0xc0, 0x50},   /* CIF HSize 640 */
0393     {0xc1, 0x3c},   /* CIF VSize 480 */
0394     {0x8c, 0x00},   /* CIF VSize LSB[2:0] */
0395     {0x8d, 0x1c},   /* output YUV */
0396     {0x34, 0x05},   /* enable Audio Suspend mode */
0397     {0xc2, 0x4c},   /* output YUV and Enable DCW */
0398     {0xc3, 0xf9},   /* enable PRE */
0399     {0x1c, 0x00},   /* indirect addressing */
0400     {0x1d, 0x48},   /* output YUV422 */
0401     {0x50, 0x89},   /* H/V divider=/2; plus DCW AVG */
0402     {0x51, 0xa0},   /* DCW input H=640/4 */
0403     {0x52, 0x78},   /* DCW input V=480/4 */
0404     {0x53, 0x00},   /* offset X=0 */
0405     {0x54, 0x00},   /* offset Y=0 */
0406     {0x55, 0x00},   /* H/V size[8]=0 */
0407     {0x57, 0x00},   /* H-size[9]=0 */
0408     {0x5c, 0x00},   /* DCW output size[9:8]=0 */
0409     {0x5a, 0x50},   /* DCW output H=320/4 */
0410     {0x5b, 0x3c},   /* DCW output V=240/4 */
0411     {0x1c, 0x0a},
0412     {0x1d, 0x0a},
0413     {0x94, 0x11},
0414 };
0415 static const u8 sensor_start_qvga_767x[][2] = {
0416     {0x11, 0x01},
0417     {0x1e, 0x04},
0418     {0x19, 0x02},
0419     {0x1a, 0x7a},
0420 };
0421 
0422 static const u8 bridge_init_772x[][2] = {
0423     { 0x88, 0xf8 },
0424     { 0x89, 0xff },
0425     { 0x76, 0x03 },
0426     { 0x92, 0x01 },
0427     { 0x93, 0x18 },
0428     { 0x94, 0x10 },
0429     { 0x95, 0x10 },
0430     { 0xe2, 0x00 },
0431     { 0xe7, 0x3e },
0432 
0433     { 0x96, 0x00 },
0434 
0435     { 0x97, 0x20 },
0436     { 0x97, 0x20 },
0437     { 0x97, 0x20 },
0438     { 0x97, 0x0a },
0439     { 0x97, 0x3f },
0440     { 0x97, 0x4a },
0441     { 0x97, 0x20 },
0442     { 0x97, 0x15 },
0443     { 0x97, 0x0b },
0444 
0445     { 0x8e, 0x40 },
0446     { 0x1f, 0x81 },
0447     { 0x34, 0x05 },
0448     { 0xe3, 0x04 },
0449     { 0x89, 0x00 },
0450     { 0x76, 0x00 },
0451     { 0xe7, 0x2e },
0452     { 0x31, 0xf9 },
0453     { 0x25, 0x42 },
0454     { 0x21, 0xf0 },
0455 
0456     { 0x1c, 0x0a },
0457     { 0x1d, 0x08 }, /* turn on UVC header */
0458     { 0x1d, 0x0e }, /* .. */
0459 };
0460 static const u8 sensor_init_772x[][2] = {
0461     { 0x12, 0x80 },
0462     { 0x11, 0x01 },
0463 /*fixme: better have a delay?*/
0464     { 0x11, 0x01 },
0465     { 0x11, 0x01 },
0466     { 0x11, 0x01 },
0467     { 0x11, 0x01 },
0468     { 0x11, 0x01 },
0469     { 0x11, 0x01 },
0470     { 0x11, 0x01 },
0471     { 0x11, 0x01 },
0472     { 0x11, 0x01 },
0473     { 0x11, 0x01 },
0474 
0475     { 0x3d, 0x03 },
0476     { 0x17, 0x26 },
0477     { 0x18, 0xa0 },
0478     { 0x19, 0x07 },
0479     { 0x1a, 0xf0 },
0480     { 0x32, 0x00 },
0481     { 0x29, 0xa0 },
0482     { 0x2c, 0xf0 },
0483     { 0x65, 0x20 },
0484     { 0x11, 0x01 },
0485     { 0x42, 0x7f },
0486     { 0x63, 0xaa },     /* AWB - was e0 */
0487     { 0x64, 0xff },
0488     { 0x66, 0x00 },
0489     { 0x13, 0xf0 },     /* com8 */
0490     { 0x0d, 0x41 },
0491     { 0x0f, 0xc5 },
0492     { 0x14, 0x11 },
0493 
0494     { 0x22, 0x7f },
0495     { 0x23, 0x03 },
0496     { 0x24, 0x40 },
0497     { 0x25, 0x30 },
0498     { 0x26, 0xa1 },
0499     { 0x2a, 0x00 },
0500     { 0x2b, 0x00 },
0501     { 0x6b, 0xaa },
0502     { 0x13, 0xff },     /* AWB */
0503 
0504     { 0x90, 0x05 },
0505     { 0x91, 0x01 },
0506     { 0x92, 0x03 },
0507     { 0x93, 0x00 },
0508     { 0x94, 0x60 },
0509     { 0x95, 0x3c },
0510     { 0x96, 0x24 },
0511     { 0x97, 0x1e },
0512     { 0x98, 0x62 },
0513     { 0x99, 0x80 },
0514     { 0x9a, 0x1e },
0515     { 0x9b, 0x08 },
0516     { 0x9c, 0x20 },
0517     { 0x9e, 0x81 },
0518 
0519     { 0xa6, 0x07 },
0520     { 0x7e, 0x0c },
0521     { 0x7f, 0x16 },
0522     { 0x80, 0x2a },
0523     { 0x81, 0x4e },
0524     { 0x82, 0x61 },
0525     { 0x83, 0x6f },
0526     { 0x84, 0x7b },
0527     { 0x85, 0x86 },
0528     { 0x86, 0x8e },
0529     { 0x87, 0x97 },
0530     { 0x88, 0xa4 },
0531     { 0x89, 0xaf },
0532     { 0x8a, 0xc5 },
0533     { 0x8b, 0xd7 },
0534     { 0x8c, 0xe8 },
0535     { 0x8d, 0x20 },
0536 
0537     { 0x2b, 0x00 },
0538     { 0x22, 0x7f },
0539     { 0x23, 0x03 },
0540     { 0x11, 0x01 },
0541     { 0x64, 0xff },
0542     { 0x0d, 0x41 },
0543 
0544     { 0x14, 0x41 },
0545     { 0x0e, 0xcd },
0546     { 0xac, 0xbf },
0547     { 0x8e, 0x00 },     /* De-noise threshold */
0548 };
0549 static const u8 bridge_start_vga_yuyv_772x[][2] = {
0550     {0x88, 0x00},
0551     {0x1c, 0x00},
0552     {0x1d, 0x40},
0553     {0x1d, 0x02},
0554     {0x1d, 0x00},
0555     {0x1d, 0x02},
0556     {0x1d, 0x58},
0557     {0x1d, 0x00},
0558     {0x8d, 0x1c},
0559     {0x8e, 0x80},
0560     {0xc0, 0x50},
0561     {0xc1, 0x3c},
0562     {0xc2, 0x0c},
0563     {0xc3, 0x69},
0564 };
0565 static const u8 sensor_start_vga_yuyv_772x[][2] = {
0566     {0x12, 0x00},
0567     {0x17, 0x26},
0568     {0x18, 0xa0},
0569     {0x19, 0x07},
0570     {0x1a, 0xf0},
0571     {0x29, 0xa0},
0572     {0x2c, 0xf0},
0573     {0x65, 0x20},
0574     {0x67, 0x00},
0575 };
0576 static const u8 bridge_start_qvga_yuyv_772x[][2] = {
0577     {0x88, 0x00},
0578     {0x1c, 0x00},
0579     {0x1d, 0x40},
0580     {0x1d, 0x02},
0581     {0x1d, 0x00},
0582     {0x1d, 0x01},
0583     {0x1d, 0x4b},
0584     {0x1d, 0x00},
0585     {0x8d, 0x1c},
0586     {0x8e, 0x80},
0587     {0xc0, 0x28},
0588     {0xc1, 0x1e},
0589     {0xc2, 0x0c},
0590     {0xc3, 0x69},
0591 };
0592 static const u8 sensor_start_qvga_yuyv_772x[][2] = {
0593     {0x12, 0x40},
0594     {0x17, 0x3f},
0595     {0x18, 0x50},
0596     {0x19, 0x03},
0597     {0x1a, 0x78},
0598     {0x29, 0x50},
0599     {0x2c, 0x78},
0600     {0x65, 0x2f},
0601     {0x67, 0x00},
0602 };
0603 static const u8 bridge_start_vga_gbrg_772x[][2] = {
0604     {0x88, 0x08},
0605     {0x1c, 0x00},
0606     {0x1d, 0x00},
0607     {0x1d, 0x02},
0608     {0x1d, 0x00},
0609     {0x1d, 0x01},
0610     {0x1d, 0x2c},
0611     {0x1d, 0x00},
0612     {0x8d, 0x00},
0613     {0x8e, 0x00},
0614     {0xc0, 0x50},
0615     {0xc1, 0x3c},
0616     {0xc2, 0x01},
0617     {0xc3, 0x01},
0618 };
0619 static const u8 sensor_start_vga_gbrg_772x[][2] = {
0620     {0x12, 0x01},
0621     {0x17, 0x26},
0622     {0x18, 0xa0},
0623     {0x19, 0x07},
0624     {0x1a, 0xf0},
0625     {0x29, 0xa0},
0626     {0x2c, 0xf0},
0627     {0x65, 0x20},
0628     {0x67, 0x02},
0629 };
0630 static const u8 bridge_start_qvga_gbrg_772x[][2] = {
0631     {0x88, 0x08},
0632     {0x1c, 0x00},
0633     {0x1d, 0x00},
0634     {0x1d, 0x02},
0635     {0x1d, 0x00},
0636     {0x1d, 0x00},
0637     {0x1d, 0x4b},
0638     {0x1d, 0x00},
0639     {0x8d, 0x00},
0640     {0x8e, 0x00},
0641     {0xc0, 0x28},
0642     {0xc1, 0x1e},
0643     {0xc2, 0x01},
0644     {0xc3, 0x01},
0645 };
0646 static const u8 sensor_start_qvga_gbrg_772x[][2] = {
0647     {0x12, 0x41},
0648     {0x17, 0x3f},
0649     {0x18, 0x50},
0650     {0x19, 0x03},
0651     {0x1a, 0x78},
0652     {0x29, 0x50},
0653     {0x2c, 0x78},
0654     {0x65, 0x2f},
0655     {0x67, 0x02},
0656 };
0657 
0658 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
0659 {
0660     struct usb_device *udev = gspca_dev->dev;
0661     int ret;
0662 
0663     if (gspca_dev->usb_err < 0)
0664         return;
0665 
0666     gspca_dbg(gspca_dev, D_USBO, "SET 01 0000 %04x %02x\n", reg, val);
0667     gspca_dev->usb_buf[0] = val;
0668     ret = usb_control_msg(udev,
0669                   usb_sndctrlpipe(udev, 0),
0670                   0x01,
0671                   USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0672                   0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
0673     if (ret < 0) {
0674         pr_err("write failed %d\n", ret);
0675         gspca_dev->usb_err = ret;
0676     }
0677 }
0678 
0679 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
0680 {
0681     struct usb_device *udev = gspca_dev->dev;
0682     int ret;
0683 
0684     if (gspca_dev->usb_err < 0)
0685         return 0;
0686     ret = usb_control_msg(udev,
0687                   usb_rcvctrlpipe(udev, 0),
0688                   0x01,
0689                   USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0690                   0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
0691     gspca_dbg(gspca_dev, D_USBI, "GET 01 0000 %04x %02x\n",
0692           reg, gspca_dev->usb_buf[0]);
0693     if (ret < 0) {
0694         pr_err("read failed %d\n", ret);
0695         gspca_dev->usb_err = ret;
0696         /*
0697          * Make sure the result is zeroed to avoid uninitialized
0698          * values.
0699          */
0700         gspca_dev->usb_buf[0] = 0;
0701     }
0702     return gspca_dev->usb_buf[0];
0703 }
0704 
0705 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
0706  * (direction and output)? */
0707 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
0708 {
0709     u8 data;
0710 
0711     gspca_dbg(gspca_dev, D_CONF, "led status: %d\n", status);
0712 
0713     data = ov534_reg_read(gspca_dev, 0x21);
0714     data |= 0x80;
0715     ov534_reg_write(gspca_dev, 0x21, data);
0716 
0717     data = ov534_reg_read(gspca_dev, 0x23);
0718     if (status)
0719         data |= 0x80;
0720     else
0721         data &= ~0x80;
0722 
0723     ov534_reg_write(gspca_dev, 0x23, data);
0724 
0725     if (!status) {
0726         data = ov534_reg_read(gspca_dev, 0x21);
0727         data &= ~0x80;
0728         ov534_reg_write(gspca_dev, 0x21, data);
0729     }
0730 }
0731 
0732 static int sccb_check_status(struct gspca_dev *gspca_dev)
0733 {
0734     u8 data;
0735     int i;
0736 
0737     for (i = 0; i < 5; i++) {
0738         usleep_range(10000, 20000);
0739         data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
0740 
0741         switch (data) {
0742         case 0x00:
0743             return 1;
0744         case 0x04:
0745             return 0;
0746         case 0x03:
0747             break;
0748         default:
0749             gspca_err(gspca_dev, "sccb status 0x%02x, attempt %d/5\n",
0750                   data, i + 1);
0751         }
0752     }
0753     return 0;
0754 }
0755 
0756 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
0757 {
0758     gspca_dbg(gspca_dev, D_USBO, "sccb write: %02x %02x\n", reg, val);
0759     ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
0760     ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
0761     ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
0762 
0763     if (!sccb_check_status(gspca_dev)) {
0764         pr_err("sccb_reg_write failed\n");
0765         gspca_dev->usb_err = -EIO;
0766     }
0767 }
0768 
0769 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
0770 {
0771     ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
0772     ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
0773     if (!sccb_check_status(gspca_dev))
0774         pr_err("sccb_reg_read failed 1\n");
0775 
0776     ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
0777     if (!sccb_check_status(gspca_dev))
0778         pr_err("sccb_reg_read failed 2\n");
0779 
0780     return ov534_reg_read(gspca_dev, OV534_REG_READ);
0781 }
0782 
0783 /* output a bridge sequence (reg - val) */
0784 static void reg_w_array(struct gspca_dev *gspca_dev,
0785             const u8 (*data)[2], int len)
0786 {
0787     while (--len >= 0) {
0788         ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
0789         data++;
0790     }
0791 }
0792 
0793 /* output a sensor sequence (reg - val) */
0794 static void sccb_w_array(struct gspca_dev *gspca_dev,
0795             const u8 (*data)[2], int len)
0796 {
0797     while (--len >= 0) {
0798         if ((*data)[0] != 0xff) {
0799             sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
0800         } else {
0801             sccb_reg_read(gspca_dev, (*data)[1]);
0802             sccb_reg_write(gspca_dev, 0xff, 0x00);
0803         }
0804         data++;
0805     }
0806 }
0807 
0808 /* ov772x specific controls */
0809 static void set_frame_rate(struct gspca_dev *gspca_dev)
0810 {
0811     struct sd *sd = (struct sd *) gspca_dev;
0812     int i;
0813     struct rate_s {
0814         u8 fps;
0815         u8 r11;
0816         u8 r0d;
0817         u8 re5;
0818     };
0819     const struct rate_s *r;
0820     static const struct rate_s rate_0[] = { /* 640x480 */
0821         {60, 0x01, 0xc1, 0x04},
0822         {50, 0x01, 0x41, 0x02},
0823         {40, 0x02, 0xc1, 0x04},
0824         {30, 0x04, 0x81, 0x02},
0825         {15, 0x03, 0x41, 0x04},
0826     };
0827     static const struct rate_s rate_1[] = { /* 320x240 */
0828 /*      {205, 0x01, 0xc1, 0x02},  * 205 FPS: video is partly corrupt */
0829         {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
0830         {150, 0x01, 0xc1, 0x04},
0831         {137, 0x02, 0xc1, 0x02},
0832         {125, 0x02, 0x81, 0x02},
0833         {100, 0x02, 0xc1, 0x04},
0834         {75, 0x03, 0xc1, 0x04},
0835         {60, 0x04, 0xc1, 0x04},
0836         {50, 0x02, 0x41, 0x04},
0837         {37, 0x03, 0x41, 0x04},
0838         {30, 0x04, 0x41, 0x04},
0839     };
0840 
0841     if (sd->sensor != SENSOR_OV772x)
0842         return;
0843     if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
0844         r = rate_0;
0845         i = ARRAY_SIZE(rate_0);
0846     } else {
0847         r = rate_1;
0848         i = ARRAY_SIZE(rate_1);
0849     }
0850     while (--i > 0) {
0851         if (sd->frame_rate >= r->fps)
0852             break;
0853         r++;
0854     }
0855 
0856     sccb_reg_write(gspca_dev, 0x11, r->r11);
0857     sccb_reg_write(gspca_dev, 0x0d, r->r0d);
0858     ov534_reg_write(gspca_dev, 0xe5, r->re5);
0859 
0860     gspca_dbg(gspca_dev, D_PROBE, "frame_rate: %d\n", r->fps);
0861 }
0862 
0863 static void sethue(struct gspca_dev *gspca_dev, s32 val)
0864 {
0865     struct sd *sd = (struct sd *) gspca_dev;
0866 
0867     if (sd->sensor == SENSOR_OV767x) {
0868         /* TBD */
0869     } else {
0870         s16 huesin;
0871         s16 huecos;
0872 
0873         /* According to the datasheet the registers expect HUESIN and
0874          * HUECOS to be the result of the trigonometric functions,
0875          * scaled by 0x80.
0876          *
0877          * The 0x7fff here represents the maximum absolute value
0878          * returned byt fixp_sin and fixp_cos, so the scaling will
0879          * consider the result like in the interval [-1.0, 1.0].
0880          */
0881         huesin = fixp_sin16(val) * 0x80 / 0x7fff;
0882         huecos = fixp_cos16(val) * 0x80 / 0x7fff;
0883 
0884         if (huesin < 0) {
0885             sccb_reg_write(gspca_dev, 0xab,
0886                 sccb_reg_read(gspca_dev, 0xab) | 0x2);
0887             huesin = -huesin;
0888         } else {
0889             sccb_reg_write(gspca_dev, 0xab,
0890                 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
0891 
0892         }
0893         sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
0894         sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
0895     }
0896 }
0897 
0898 static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
0899 {
0900     struct sd *sd = (struct sd *) gspca_dev;
0901 
0902     if (sd->sensor == SENSOR_OV767x) {
0903         int i;
0904         static u8 color_tb[][6] = {
0905             {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
0906             {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
0907             {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
0908             {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
0909             {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
0910             {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
0911             {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
0912         };
0913 
0914         for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
0915             sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
0916     } else {
0917         sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
0918         sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
0919     }
0920 }
0921 
0922 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
0923 {
0924     struct sd *sd = (struct sd *) gspca_dev;
0925 
0926     if (sd->sensor == SENSOR_OV767x) {
0927         if (val < 0)
0928             val = 0x80 - val;
0929         sccb_reg_write(gspca_dev, 0x55, val);   /* bright */
0930     } else {
0931         sccb_reg_write(gspca_dev, 0x9b, val);
0932     }
0933 }
0934 
0935 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
0936 {
0937     struct sd *sd = (struct sd *) gspca_dev;
0938 
0939     if (sd->sensor == SENSOR_OV767x)
0940         sccb_reg_write(gspca_dev, 0x56, val);   /* contras */
0941     else
0942         sccb_reg_write(gspca_dev, 0x9c, val);
0943 }
0944 
0945 static void setgain(struct gspca_dev *gspca_dev, s32 val)
0946 {
0947     switch (val & 0x30) {
0948     case 0x00:
0949         val &= 0x0f;
0950         break;
0951     case 0x10:
0952         val &= 0x0f;
0953         val |= 0x30;
0954         break;
0955     case 0x20:
0956         val &= 0x0f;
0957         val |= 0x70;
0958         break;
0959     default:
0960 /*  case 0x30: */
0961         val &= 0x0f;
0962         val |= 0xf0;
0963         break;
0964     }
0965     sccb_reg_write(gspca_dev, 0x00, val);
0966 }
0967 
0968 static s32 getgain(struct gspca_dev *gspca_dev)
0969 {
0970     return sccb_reg_read(gspca_dev, 0x00);
0971 }
0972 
0973 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
0974 {
0975     struct sd *sd = (struct sd *) gspca_dev;
0976 
0977     if (sd->sensor == SENSOR_OV767x) {
0978 
0979         /* set only aec[9:2] */
0980         sccb_reg_write(gspca_dev, 0x10, val);   /* aech */
0981     } else {
0982 
0983         /* 'val' is one byte and represents half of the exposure value
0984          * we are going to set into registers, a two bytes value:
0985          *
0986          *    MSB: ((u16) val << 1) >> 8   == val >> 7
0987          *    LSB: ((u16) val << 1) & 0xff == val << 1
0988          */
0989         sccb_reg_write(gspca_dev, 0x08, val >> 7);
0990         sccb_reg_write(gspca_dev, 0x10, val << 1);
0991     }
0992 }
0993 
0994 static s32 getexposure(struct gspca_dev *gspca_dev)
0995 {
0996     struct sd *sd = (struct sd *) gspca_dev;
0997 
0998     if (sd->sensor == SENSOR_OV767x) {
0999         /* get only aec[9:2] */
1000         return sccb_reg_read(gspca_dev, 0x10);  /* aech */
1001     } else {
1002         u8 hi = sccb_reg_read(gspca_dev, 0x08);
1003         u8 lo = sccb_reg_read(gspca_dev, 0x10);
1004         return (hi << 8 | lo) >> 1;
1005     }
1006 }
1007 
1008 static void setagc(struct gspca_dev *gspca_dev, s32 val)
1009 {
1010     if (val) {
1011         sccb_reg_write(gspca_dev, 0x13,
1012                 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1013         sccb_reg_write(gspca_dev, 0x64,
1014                 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1015     } else {
1016         sccb_reg_write(gspca_dev, 0x13,
1017                 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1018         sccb_reg_write(gspca_dev, 0x64,
1019                 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1020     }
1021 }
1022 
1023 static void setawb(struct gspca_dev *gspca_dev, s32 val)
1024 {
1025     struct sd *sd = (struct sd *) gspca_dev;
1026 
1027     if (val) {
1028         sccb_reg_write(gspca_dev, 0x13,
1029                 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1030         if (sd->sensor == SENSOR_OV772x)
1031             sccb_reg_write(gspca_dev, 0x63,
1032                 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1033     } else {
1034         sccb_reg_write(gspca_dev, 0x13,
1035                 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1036         if (sd->sensor == SENSOR_OV772x)
1037             sccb_reg_write(gspca_dev, 0x63,
1038                 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1039     }
1040 }
1041 
1042 static void setaec(struct gspca_dev *gspca_dev, s32 val)
1043 {
1044     struct sd *sd = (struct sd *) gspca_dev;
1045     u8 data;
1046 
1047     data = sd->sensor == SENSOR_OV767x ?
1048             0x05 :      /* agc + aec */
1049             0x01;       /* agc */
1050     switch (val) {
1051     case V4L2_EXPOSURE_AUTO:
1052         sccb_reg_write(gspca_dev, 0x13,
1053                 sccb_reg_read(gspca_dev, 0x13) | data);
1054         break;
1055     case V4L2_EXPOSURE_MANUAL:
1056         sccb_reg_write(gspca_dev, 0x13,
1057                 sccb_reg_read(gspca_dev, 0x13) & ~data);
1058         break;
1059     }
1060 }
1061 
1062 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1063 {
1064     sccb_reg_write(gspca_dev, 0x91, val);   /* Auto de-noise threshold */
1065     sccb_reg_write(gspca_dev, 0x8e, val);   /* De-noise threshold */
1066 }
1067 
1068 static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
1069 {
1070     struct sd *sd = (struct sd *) gspca_dev;
1071     u8 val;
1072 
1073     if (sd->sensor == SENSOR_OV767x) {
1074         val = sccb_reg_read(gspca_dev, 0x1e);   /* mvfp */
1075         val &= ~0x30;
1076         if (hflip)
1077             val |= 0x20;
1078         if (vflip)
1079             val |= 0x10;
1080         sccb_reg_write(gspca_dev, 0x1e, val);
1081     } else {
1082         val = sccb_reg_read(gspca_dev, 0x0c);
1083         val &= ~0xc0;
1084         if (hflip == 0)
1085             val |= 0x40;
1086         if (vflip == 0)
1087             val |= 0x80;
1088         sccb_reg_write(gspca_dev, 0x0c, val);
1089     }
1090 }
1091 
1092 static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
1093 {
1094     struct sd *sd = (struct sd *) gspca_dev;
1095 
1096     val = val ? 0x9e : 0x00;
1097     if (sd->sensor == SENSOR_OV767x) {
1098         sccb_reg_write(gspca_dev, 0x2a, 0x00);
1099         if (val)
1100             val = 0x9d; /* insert dummy to 25fps for 50Hz */
1101     }
1102     sccb_reg_write(gspca_dev, 0x2b, val);
1103 }
1104 
1105 
1106 /* this function is called at probe time */
1107 static int sd_config(struct gspca_dev *gspca_dev,
1108              const struct usb_device_id *id)
1109 {
1110     struct sd *sd = (struct sd *) gspca_dev;
1111     struct cam *cam;
1112 
1113     cam = &gspca_dev->cam;
1114 
1115     cam->cam_mode = ov772x_mode;
1116     cam->nmodes = ARRAY_SIZE(ov772x_mode);
1117 
1118     sd->frame_rate = DEFAULT_FRAME_RATE;
1119 
1120     return 0;
1121 }
1122 
1123 static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1124 {
1125     struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1126     struct gspca_dev *gspca_dev = &sd->gspca_dev;
1127 
1128     switch (ctrl->id) {
1129     case V4L2_CID_AUTOGAIN:
1130         gspca_dev->usb_err = 0;
1131         if (ctrl->val && sd->gain && gspca_dev->streaming)
1132             sd->gain->val = getgain(gspca_dev);
1133         return gspca_dev->usb_err;
1134 
1135     case V4L2_CID_EXPOSURE_AUTO:
1136         gspca_dev->usb_err = 0;
1137         if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1138             gspca_dev->streaming)
1139             sd->exposure->val = getexposure(gspca_dev);
1140         return gspca_dev->usb_err;
1141     }
1142     return -EINVAL;
1143 }
1144 
1145 static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1146 {
1147     struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1148     struct gspca_dev *gspca_dev = &sd->gspca_dev;
1149 
1150     gspca_dev->usb_err = 0;
1151     if (!gspca_dev->streaming)
1152         return 0;
1153 
1154     switch (ctrl->id) {
1155     case V4L2_CID_HUE:
1156         sethue(gspca_dev, ctrl->val);
1157         break;
1158     case V4L2_CID_SATURATION:
1159         setsaturation(gspca_dev, ctrl->val);
1160         break;
1161     case V4L2_CID_BRIGHTNESS:
1162         setbrightness(gspca_dev, ctrl->val);
1163         break;
1164     case V4L2_CID_CONTRAST:
1165         setcontrast(gspca_dev, ctrl->val);
1166         break;
1167     case V4L2_CID_AUTOGAIN:
1168     /* case V4L2_CID_GAIN: */
1169         setagc(gspca_dev, ctrl->val);
1170         if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1171             setgain(gspca_dev, sd->gain->val);
1172         break;
1173     case V4L2_CID_AUTO_WHITE_BALANCE:
1174         setawb(gspca_dev, ctrl->val);
1175         break;
1176     case V4L2_CID_EXPOSURE_AUTO:
1177     /* case V4L2_CID_EXPOSURE: */
1178         setaec(gspca_dev, ctrl->val);
1179         if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1180             sd->exposure)
1181             setexposure(gspca_dev, sd->exposure->val);
1182         break;
1183     case V4L2_CID_SHARPNESS:
1184         setsharpness(gspca_dev, ctrl->val);
1185         break;
1186     case V4L2_CID_HFLIP:
1187         sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1188         break;
1189     case V4L2_CID_VFLIP:
1190         sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1191         break;
1192     case V4L2_CID_POWER_LINE_FREQUENCY:
1193         setlightfreq(gspca_dev, ctrl->val);
1194         break;
1195     }
1196     return gspca_dev->usb_err;
1197 }
1198 
1199 static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1200     .g_volatile_ctrl = ov534_g_volatile_ctrl,
1201     .s_ctrl = ov534_s_ctrl,
1202 };
1203 
1204 static int sd_init_controls(struct gspca_dev *gspca_dev)
1205 {
1206     struct sd *sd = (struct sd *) gspca_dev;
1207     struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1208     /* parameters with different values between the supported sensors */
1209     int saturation_min;
1210     int saturation_max;
1211     int saturation_def;
1212     int brightness_min;
1213     int brightness_max;
1214     int brightness_def;
1215     int contrast_max;
1216     int contrast_def;
1217     int exposure_min;
1218     int exposure_max;
1219     int exposure_def;
1220     int hflip_def;
1221 
1222     if (sd->sensor == SENSOR_OV767x) {
1223         saturation_min = 0;
1224         saturation_max = 6;
1225         saturation_def = 3;
1226         brightness_min = -127;
1227         brightness_max = 127;
1228         brightness_def = 0;
1229         contrast_max = 0x80;
1230         contrast_def = 0x40;
1231         exposure_min = 0x08;
1232         exposure_max = 0x60;
1233         exposure_def = 0x13;
1234         hflip_def = 1;
1235     } else {
1236         saturation_min = 0;
1237         saturation_max = 255;
1238         saturation_def = 64;
1239         brightness_min = 0;
1240         brightness_max = 255;
1241         brightness_def = 0;
1242         contrast_max = 255;
1243         contrast_def = 32;
1244         exposure_min = 0;
1245         exposure_max = 255;
1246         exposure_def = 120;
1247         hflip_def = 0;
1248     }
1249 
1250     gspca_dev->vdev.ctrl_handler = hdl;
1251 
1252     v4l2_ctrl_handler_init(hdl, 13);
1253 
1254     if (sd->sensor == SENSOR_OV772x)
1255         sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1256                 V4L2_CID_HUE, -90, 90, 1, 0);
1257 
1258     sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1259             V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1260             saturation_def);
1261     sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1262             V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1263             brightness_def);
1264     sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1265             V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1266 
1267     if (sd->sensor == SENSOR_OV772x) {
1268         sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1269                 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1270         sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1271                 V4L2_CID_GAIN, 0, 63, 1, 20);
1272     }
1273 
1274     sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1275             V4L2_CID_EXPOSURE_AUTO,
1276             V4L2_EXPOSURE_MANUAL, 0,
1277             V4L2_EXPOSURE_AUTO);
1278     sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1279             V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1280             exposure_def);
1281 
1282     sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1283             V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1284 
1285     if (sd->sensor == SENSOR_OV772x)
1286         sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1287                 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1288 
1289     sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1290             V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1291     sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1292             V4L2_CID_VFLIP, 0, 1, 1, 0);
1293     sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1294             V4L2_CID_POWER_LINE_FREQUENCY,
1295             V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1296             V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1297 
1298     if (hdl->error) {
1299         pr_err("Could not initialize controls\n");
1300         return hdl->error;
1301     }
1302 
1303     if (sd->sensor == SENSOR_OV772x)
1304         v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1305 
1306     v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1307                    true);
1308 
1309     return 0;
1310 }
1311 
1312 /* this function is called at probe and resume time */
1313 static int sd_init(struct gspca_dev *gspca_dev)
1314 {
1315     struct sd *sd = (struct sd *) gspca_dev;
1316     u16 sensor_id;
1317     static const struct reg_array bridge_init[NSENSORS] = {
1318     [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1319     [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1320     };
1321     static const struct reg_array sensor_init[NSENSORS] = {
1322     [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1323     [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1324     };
1325 
1326     /* reset bridge */
1327     ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1328     ov534_reg_write(gspca_dev, 0xe0, 0x08);
1329     msleep(100);
1330 
1331     /* initialize the sensor address */
1332     ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1333 
1334     /* reset sensor */
1335     sccb_reg_write(gspca_dev, 0x12, 0x80);
1336     usleep_range(10000, 20000);
1337 
1338     /* probe the sensor */
1339     sccb_reg_read(gspca_dev, 0x0a);
1340     sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1341     sccb_reg_read(gspca_dev, 0x0b);
1342     sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1343     gspca_dbg(gspca_dev, D_PROBE, "Sensor ID: %04x\n", sensor_id);
1344 
1345     if ((sensor_id & 0xfff0) == 0x7670) {
1346         sd->sensor = SENSOR_OV767x;
1347         gspca_dev->cam.cam_mode = ov767x_mode;
1348         gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1349     } else {
1350         sd->sensor = SENSOR_OV772x;
1351         gspca_dev->cam.bulk = 1;
1352         gspca_dev->cam.bulk_size = 16384;
1353         gspca_dev->cam.bulk_nurbs = 2;
1354         gspca_dev->cam.mode_framerates = ov772x_framerates;
1355     }
1356 
1357     /* initialize */
1358     reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1359             bridge_init[sd->sensor].len);
1360     ov534_set_led(gspca_dev, 1);
1361     sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1362             sensor_init[sd->sensor].len);
1363 
1364     sd_stopN(gspca_dev);
1365 /*  set_frame_rate(gspca_dev);  */
1366 
1367     return gspca_dev->usb_err;
1368 }
1369 
1370 static int sd_start(struct gspca_dev *gspca_dev)
1371 {
1372     struct sd *sd = (struct sd *) gspca_dev;
1373     int mode;
1374     static const struct reg_array bridge_start[NSENSORS][4] = {
1375     [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1376                     ARRAY_SIZE(bridge_start_qvga_767x)},
1377             {bridge_start_vga_767x,
1378                     ARRAY_SIZE(bridge_start_vga_767x)}},
1379     [SENSOR_OV772x] = {{bridge_start_qvga_yuyv_772x,
1380                 ARRAY_SIZE(bridge_start_qvga_yuyv_772x)},
1381             {bridge_start_vga_yuyv_772x,
1382                 ARRAY_SIZE(bridge_start_vga_yuyv_772x)},
1383             {bridge_start_qvga_gbrg_772x,
1384                 ARRAY_SIZE(bridge_start_qvga_gbrg_772x)},
1385             {bridge_start_vga_gbrg_772x,
1386                 ARRAY_SIZE(bridge_start_vga_gbrg_772x)} },
1387     };
1388     static const struct reg_array sensor_start[NSENSORS][4] = {
1389     [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1390                     ARRAY_SIZE(sensor_start_qvga_767x)},
1391             {sensor_start_vga_767x,
1392                     ARRAY_SIZE(sensor_start_vga_767x)}},
1393     [SENSOR_OV772x] = {{sensor_start_qvga_yuyv_772x,
1394                 ARRAY_SIZE(sensor_start_qvga_yuyv_772x)},
1395             {sensor_start_vga_yuyv_772x,
1396                 ARRAY_SIZE(sensor_start_vga_yuyv_772x)},
1397             {sensor_start_qvga_gbrg_772x,
1398                 ARRAY_SIZE(sensor_start_qvga_gbrg_772x)},
1399             {sensor_start_vga_gbrg_772x,
1400                 ARRAY_SIZE(sensor_start_vga_gbrg_772x)} },
1401     };
1402 
1403     /* (from ms-win trace) */
1404     if (sd->sensor == SENSOR_OV767x)
1405         sccb_reg_write(gspca_dev, 0x1e, 0x04);
1406                     /* black sun enable ? */
1407 
1408     mode = gspca_dev->curr_mode;    /* 0: 320x240, 1: 640x480 */
1409     reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1410                 bridge_start[sd->sensor][mode].len);
1411     sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1412                 sensor_start[sd->sensor][mode].len);
1413 
1414     set_frame_rate(gspca_dev);
1415 
1416     if (sd->hue)
1417         sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1418     setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1419     if (sd->autogain)
1420         setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1421     setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1422     setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1423     if (sd->gain)
1424         setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1425     setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1426     setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1427     setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1428     if (sd->sharpness)
1429         setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1430     sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1431           v4l2_ctrl_g_ctrl(sd->vflip));
1432     setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
1433 
1434     ov534_set_led(gspca_dev, 1);
1435     ov534_reg_write(gspca_dev, 0xe0, 0x00);
1436     return gspca_dev->usb_err;
1437 }
1438 
1439 static void sd_stopN(struct gspca_dev *gspca_dev)
1440 {
1441     ov534_reg_write(gspca_dev, 0xe0, 0x09);
1442     ov534_set_led(gspca_dev, 0);
1443 }
1444 
1445 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1446 #define UVC_STREAM_EOH  (1 << 7)
1447 #define UVC_STREAM_ERR  (1 << 6)
1448 #define UVC_STREAM_STI  (1 << 5)
1449 #define UVC_STREAM_RES  (1 << 4)
1450 #define UVC_STREAM_SCR  (1 << 3)
1451 #define UVC_STREAM_PTS  (1 << 2)
1452 #define UVC_STREAM_EOF  (1 << 1)
1453 #define UVC_STREAM_FID  (1 << 0)
1454 
1455 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1456             u8 *data, int len)
1457 {
1458     struct sd *sd = (struct sd *) gspca_dev;
1459     __u32 this_pts;
1460     u16 this_fid;
1461     int remaining_len = len;
1462     int payload_len;
1463 
1464     payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1465     do {
1466         len = min(remaining_len, payload_len);
1467 
1468         /* Payloads are prefixed with a UVC-style header.  We
1469            consider a frame to start when the FID toggles, or the PTS
1470            changes.  A frame ends when EOF is set, and we've received
1471            the correct number of bytes. */
1472 
1473         /* Verify UVC header.  Header length is always 12 */
1474         if (data[0] != 12 || len < 12) {
1475             gspca_dbg(gspca_dev, D_PACK, "bad header\n");
1476             goto discard;
1477         }
1478 
1479         /* Check errors */
1480         if (data[1] & UVC_STREAM_ERR) {
1481             gspca_dbg(gspca_dev, D_PACK, "payload error\n");
1482             goto discard;
1483         }
1484 
1485         /* Extract PTS and FID */
1486         if (!(data[1] & UVC_STREAM_PTS)) {
1487             gspca_dbg(gspca_dev, D_PACK, "PTS not present\n");
1488             goto discard;
1489         }
1490         this_pts = (data[5] << 24) | (data[4] << 16)
1491                         | (data[3] << 8) | data[2];
1492         this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1493 
1494         /* If PTS or FID has changed, start a new frame. */
1495         if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1496             if (gspca_dev->last_packet_type == INTER_PACKET)
1497                 gspca_frame_add(gspca_dev, LAST_PACKET,
1498                         NULL, 0);
1499             sd->last_pts = this_pts;
1500             sd->last_fid = this_fid;
1501             gspca_frame_add(gspca_dev, FIRST_PACKET,
1502                     data + 12, len - 12);
1503         /* If this packet is marked as EOF, end the frame */
1504         } else if (data[1] & UVC_STREAM_EOF) {
1505             sd->last_pts = 0;
1506             if (gspca_dev->pixfmt.pixelformat != V4L2_PIX_FMT_JPEG
1507              && gspca_dev->image_len + len - 12 !=
1508                 gspca_dev->pixfmt.sizeimage) {
1509                 gspca_dbg(gspca_dev, D_PACK, "wrong sized frame\n");
1510                 goto discard;
1511             }
1512             gspca_frame_add(gspca_dev, LAST_PACKET,
1513                     data + 12, len - 12);
1514         } else {
1515 
1516             /* Add the data from this payload */
1517             gspca_frame_add(gspca_dev, INTER_PACKET,
1518                     data + 12, len - 12);
1519         }
1520 
1521         /* Done this payload */
1522         goto scan_next;
1523 
1524 discard:
1525         /* Discard data until a new frame starts. */
1526         gspca_dev->last_packet_type = DISCARD_PACKET;
1527 
1528 scan_next:
1529         remaining_len -= len;
1530         data += len;
1531     } while (remaining_len > 0);
1532 }
1533 
1534 /* get stream parameters (framerate) */
1535 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1536                  struct v4l2_streamparm *parm)
1537 {
1538     struct v4l2_captureparm *cp = &parm->parm.capture;
1539     struct v4l2_fract *tpf = &cp->timeperframe;
1540     struct sd *sd = (struct sd *) gspca_dev;
1541 
1542     tpf->numerator = 1;
1543     tpf->denominator = sd->frame_rate;
1544 }
1545 
1546 /* set stream parameters (framerate) */
1547 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1548                  struct v4l2_streamparm *parm)
1549 {
1550     struct v4l2_captureparm *cp = &parm->parm.capture;
1551     struct v4l2_fract *tpf = &cp->timeperframe;
1552     struct sd *sd = (struct sd *) gspca_dev;
1553 
1554     if (tpf->numerator == 0 || tpf->denominator == 0)
1555         sd->frame_rate = DEFAULT_FRAME_RATE;
1556     else
1557         sd->frame_rate = tpf->denominator / tpf->numerator;
1558 
1559     if (gspca_dev->streaming)
1560         set_frame_rate(gspca_dev);
1561 
1562     /* Return the actual framerate */
1563     tpf->numerator = 1;
1564     tpf->denominator = sd->frame_rate;
1565 }
1566 
1567 /* sub-driver description */
1568 static const struct sd_desc sd_desc = {
1569     .name     = MODULE_NAME,
1570     .config   = sd_config,
1571     .init     = sd_init,
1572     .init_controls = sd_init_controls,
1573     .start    = sd_start,
1574     .stopN    = sd_stopN,
1575     .pkt_scan = sd_pkt_scan,
1576     .get_streamparm = sd_get_streamparm,
1577     .set_streamparm = sd_set_streamparm,
1578 };
1579 
1580 /* -- module initialisation -- */
1581 static const struct usb_device_id device_table[] = {
1582     {USB_DEVICE(0x1415, 0x2000)},
1583     {USB_DEVICE(0x06f8, 0x3002)},
1584     {}
1585 };
1586 
1587 MODULE_DEVICE_TABLE(usb, device_table);
1588 
1589 /* -- device connect -- */
1590 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1591 {
1592     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1593                 THIS_MODULE);
1594 }
1595 
1596 static struct usb_driver sd_driver = {
1597     .name       = MODULE_NAME,
1598     .id_table   = device_table,
1599     .probe      = sd_probe,
1600     .disconnect = gspca_disconnect,
1601 #ifdef CONFIG_PM
1602     .suspend    = gspca_suspend,
1603     .resume     = gspca_resume,
1604     .reset_resume = gspca_resume,
1605 #endif
1606 };
1607 
1608 module_usb_driver(sd_driver);