Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * SQ930x subdriver
0004  *
0005  * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
0006  * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
0007  * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
0008  */
0009 
0010 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0011 
0012 #define MODULE_NAME "sq930x"
0013 
0014 #include "gspca.h"
0015 
0016 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
0017         "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
0018         "Sam Revitch <samr7@cs.washington.edu>");
0019 MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
0020 MODULE_LICENSE("GPL");
0021 
0022 /* Structure to hold all of our device specific stuff */
0023 struct sd {
0024     struct gspca_dev gspca_dev; /* !! must be the first item */
0025 
0026     struct { /* exposure/gain control cluster */
0027         struct v4l2_ctrl *exposure;
0028         struct v4l2_ctrl *gain;
0029     };
0030 
0031     u8 do_ctrl;
0032     u8 gpio[2];
0033     u8 sensor;
0034     u8 type;
0035 #define Generic 0
0036 #define Creative_live_motion 1
0037 };
0038 enum sensors {
0039     SENSOR_ICX098BQ,
0040     SENSOR_LZ24BP,
0041     SENSOR_MI0360,
0042     SENSOR_MT9V111,     /* = MI360SOC */
0043     SENSOR_OV7660,
0044     SENSOR_OV9630,
0045 };
0046 
0047 static struct v4l2_pix_format vga_mode[] = {
0048     {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
0049         .bytesperline = 320,
0050         .sizeimage = 320 * 240,
0051         .colorspace = V4L2_COLORSPACE_SRGB,
0052         .priv = 0},
0053     {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
0054         .bytesperline = 640,
0055         .sizeimage = 640 * 480,
0056         .colorspace = V4L2_COLORSPACE_SRGB,
0057         .priv = 1},
0058 };
0059 
0060 /* sq930x registers */
0061 #define SQ930_CTRL_UCBUS_IO 0x0001
0062 #define SQ930_CTRL_I2C_IO   0x0002
0063 #define SQ930_CTRL_GPIO     0x0005
0064 #define SQ930_CTRL_CAP_START    0x0010
0065 #define SQ930_CTRL_CAP_STOP 0x0011
0066 #define SQ930_CTRL_SET_EXPOSURE 0x001d
0067 #define SQ930_CTRL_RESET    0x001e
0068 #define SQ930_CTRL_GET_DEV_INFO 0x001f
0069 
0070 /* gpio 1 (8..15) */
0071 #define SQ930_GPIO_DFL_I2C_SDA  0x0001
0072 #define SQ930_GPIO_DFL_I2C_SCL  0x0002
0073 #define SQ930_GPIO_RSTBAR   0x0004
0074 #define SQ930_GPIO_EXTRA1   0x0040
0075 #define SQ930_GPIO_EXTRA2   0x0080
0076 /* gpio 3 (24..31) */
0077 #define SQ930_GPIO_POWER    0x0200
0078 #define SQ930_GPIO_DFL_LED  0x1000
0079 
0080 struct ucbus_write_cmd {
0081     u16 bw_addr;
0082     u8  bw_data;
0083 };
0084 struct i2c_write_cmd {
0085     u8  reg;
0086     u16 val;
0087 };
0088 
0089 static const struct ucbus_write_cmd icx098bq_start_0[] = {
0090     {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
0091     {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
0092     {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
0093     {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
0094     {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
0095     {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
0096     {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
0097     {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
0098     {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
0099     {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
0100     {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
0101     {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
0102     {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
0103     {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
0104     {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
0105     {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
0106     {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
0107     {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
0108     {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
0109     {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
0110     {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
0111     {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
0112     {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
0113     {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
0114     {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
0115     {0xf800, 0x03}
0116 };
0117 static const struct ucbus_write_cmd icx098bq_start_1[] = {
0118     {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0119     {0xf5f4, 0xc0},
0120     {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0121     {0xf5f4, 0xc0},
0122     {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
0123     {0xf5f9, 0x00}
0124 };
0125 
0126 static const struct ucbus_write_cmd icx098bq_start_2[] = {
0127     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
0128     {0xf807, 0x7f}, {0xf800, 0x03},
0129     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
0130     {0xf807, 0x7f}, {0xf800, 0x03},
0131     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
0132     {0xf807, 0x7f}, {0xf800, 0x03},
0133     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
0134     {0xf807, 0x7f}, {0xf800, 0x03}
0135 };
0136 
0137 static const struct ucbus_write_cmd lz24bp_start_0[] = {
0138     {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
0139     {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
0140     {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
0141     {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
0142     {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
0143     {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
0144     {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
0145     {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
0146     {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
0147     {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
0148     {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
0149     {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
0150     {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
0151     {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
0152     {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
0153     {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
0154     {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
0155     {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
0156     {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
0157     {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
0158     {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
0159     {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
0160     {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
0161     {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
0162     {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
0163     {0xf800, 0x03}
0164 };
0165 static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
0166     {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0167     {0xf5f4, 0xb3},
0168     {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0169     {0xf5f4, 0xb3},
0170     {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
0171     {0xf5f9, 0x00}
0172 };
0173 
0174 static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
0175     {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
0176     {0xf5f4, 0xc0},
0177     {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
0178     {0xf5f4, 0xc0},
0179     {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
0180     {0xf5f9, 0x00}
0181 };
0182 
0183 static const struct ucbus_write_cmd lz24bp_start_2[] = {
0184     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
0185     {0xf807, 0x7f}, {0xf800, 0x03},
0186     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
0187     {0xf807, 0x7f}, {0xf800, 0x03},
0188     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
0189     {0xf807, 0x7f}, {0xf800, 0x03},
0190     {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
0191     {0xf807, 0x7f}, {0xf800, 0x03}
0192 };
0193 
0194 static const struct ucbus_write_cmd mi0360_start_0[] = {
0195     {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
0196     {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
0197 };
0198 static const struct i2c_write_cmd mi0360_init_23[] = {
0199     {0x30, 0x0040},     /* reserved - def 0x0005 */
0200     {0x31, 0x0000},     /* reserved - def 0x002a */
0201     {0x34, 0x0100},     /* reserved - def 0x0100 */
0202     {0x3d, 0x068f},     /* reserved - def 0x068f */
0203 };
0204 static const struct i2c_write_cmd mi0360_init_24[] = {
0205     {0x03, 0x01e5},     /* window height */
0206     {0x04, 0x0285},     /* window width */
0207 };
0208 static const struct i2c_write_cmd mi0360_init_25[] = {
0209     {0x35, 0x0020},     /* global gain */
0210     {0x2b, 0x0020},     /* green1 gain */
0211     {0x2c, 0x002a},     /* blue gain */
0212     {0x2d, 0x0028},     /* red gain */
0213     {0x2e, 0x0020},     /* green2 gain */
0214 };
0215 static const struct ucbus_write_cmd mi0360_start_1[] = {
0216     {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0217     {0xf5f4, 0xa6},
0218     {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0219     {0xf5f4, 0xa6},
0220     {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
0221     {0xf5f9, 0x00}
0222 };
0223 static const struct i2c_write_cmd mi0360_start_2[] = {
0224     {0x62, 0x041d},     /* reserved - def 0x0418 */
0225 };
0226 static const struct i2c_write_cmd mi0360_start_3[] = {
0227     {0x05, 0x007b},     /* horiz blanking */
0228 };
0229 static const struct i2c_write_cmd mi0360_start_4[] = {
0230     {0x05, 0x03f5},     /* horiz blanking */
0231 };
0232 
0233 static const struct i2c_write_cmd mt9v111_init_0[] = {
0234     {0x01, 0x0001},     /* select IFP/SOC registers */
0235     {0x06, 0x300c},     /* operating mode control */
0236     {0x08, 0xcc00},     /* output format control (RGB) */
0237     {0x01, 0x0004},     /* select sensor core registers */
0238 };
0239 static const struct i2c_write_cmd mt9v111_init_1[] = {
0240     {0x03, 0x01e5},     /* window height */
0241     {0x04, 0x0285},     /* window width */
0242 };
0243 static const struct i2c_write_cmd mt9v111_init_2[] = {
0244     {0x30, 0x7800},
0245     {0x31, 0x0000},
0246     {0x07, 0x3002},     /* output control */
0247     {0x35, 0x0020},     /* global gain */
0248     {0x2b, 0x0020},     /* green1 gain */
0249     {0x2c, 0x0020},     /* blue gain */
0250     {0x2d, 0x0020},     /* red gain */
0251     {0x2e, 0x0020},     /* green2 gain */
0252 };
0253 static const struct ucbus_write_cmd mt9v111_start_1[] = {
0254     {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0255     {0xf5f4, 0xaa},
0256     {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
0257     {0xf5f4, 0xaa},
0258     {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
0259     {0xf5f9, 0x0a}
0260 };
0261 static const struct i2c_write_cmd mt9v111_init_3[] = {
0262     {0x62, 0x0405},
0263 };
0264 static const struct i2c_write_cmd mt9v111_init_4[] = {
0265 /*  {0x05, 0x00ce}, */
0266     {0x05, 0x005d},     /* horizontal blanking */
0267 };
0268 
0269 static const struct ucbus_write_cmd ov7660_start_0[] = {
0270     {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
0271     {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
0272 };
0273 
0274 static const struct ucbus_write_cmd ov9630_start_0[] = {
0275     {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
0276     {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
0277 };
0278 
0279 /* start parameters indexed by [sensor][mode] */
0280 static const struct cap_s {
0281     u8  cc_sizeid;
0282     u8  cc_bytes[32];
0283 } capconfig[4][2] = {
0284     [SENSOR_ICX098BQ] = {
0285         {2,             /* Bayer 320x240 */
0286           {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
0287            0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0288            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
0289            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0290         {4,             /* Bayer 640x480 */
0291           {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
0292            0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0293            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0294            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0295     },
0296     [SENSOR_LZ24BP] = {
0297         {2,             /* Bayer 320x240 */
0298           {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
0299            0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0300            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0301            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0302         {4,             /* Bayer 640x480 */
0303           {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
0304            0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0305            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0306            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0307     },
0308     [SENSOR_MI0360] = {
0309         {2,             /* Bayer 320x240 */
0310           {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
0311            0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0312            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0313            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0314         {4,             /* Bayer 640x480 */
0315           {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
0316            0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0317            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0318            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0319     },
0320     [SENSOR_MT9V111] = {
0321         {2,             /* Bayer 320x240 */
0322           {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
0323            0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0324            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0325            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0326         {4,             /* Bayer 640x480 */
0327           {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
0328            0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
0329            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0330            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
0331     },
0332 };
0333 
0334 struct sensor_s {
0335     const char *name;
0336     u8 i2c_addr;
0337     u8 i2c_dum;
0338     u8 gpio[5];
0339     u8 cmd_len;
0340     const struct ucbus_write_cmd *cmd;
0341 };
0342 
0343 static const struct sensor_s sensor_tb[] = {
0344     [SENSOR_ICX098BQ] = {
0345         "icx098bp",
0346         0x00, 0x00,
0347         {0,
0348          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0349          SQ930_GPIO_DFL_I2C_SDA,
0350          0,
0351          SQ930_GPIO_RSTBAR
0352         },
0353         8, icx098bq_start_0
0354         },
0355     [SENSOR_LZ24BP] = {
0356         "lz24bp",
0357         0x00, 0x00,
0358         {0,
0359          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0360          SQ930_GPIO_DFL_I2C_SDA,
0361          0,
0362          SQ930_GPIO_RSTBAR
0363         },
0364         8, lz24bp_start_0
0365         },
0366     [SENSOR_MI0360] = {
0367         "mi0360",
0368         0x5d, 0x80,
0369         {SQ930_GPIO_RSTBAR,
0370          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0371          SQ930_GPIO_DFL_I2C_SDA,
0372          0,
0373          0
0374         },
0375         7, mi0360_start_0
0376         },
0377     [SENSOR_MT9V111] = {
0378         "mt9v111",
0379         0x5c, 0x7f,
0380         {SQ930_GPIO_RSTBAR,
0381          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0382          SQ930_GPIO_DFL_I2C_SDA,
0383          0,
0384          0
0385         },
0386         7, mi0360_start_0
0387         },
0388     [SENSOR_OV7660] = {
0389         "ov7660",
0390         0x21, 0x00,
0391         {0,
0392          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0393          SQ930_GPIO_DFL_I2C_SDA,
0394          0,
0395          SQ930_GPIO_RSTBAR
0396         },
0397         7, ov7660_start_0
0398         },
0399     [SENSOR_OV9630] = {
0400         "ov9630",
0401         0x30, 0x00,
0402         {0,
0403          SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
0404          SQ930_GPIO_DFL_I2C_SDA,
0405          0,
0406          SQ930_GPIO_RSTBAR
0407         },
0408         7, ov9630_start_0
0409         },
0410 };
0411 
0412 static void reg_r(struct gspca_dev *gspca_dev,
0413         u16 value, int len)
0414 {
0415     int ret;
0416 
0417     if (gspca_dev->usb_err < 0)
0418         return;
0419     ret = usb_control_msg(gspca_dev->dev,
0420             usb_rcvctrlpipe(gspca_dev->dev, 0),
0421             0x0c,
0422             USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0423             value, 0, gspca_dev->usb_buf, len,
0424             500);
0425     if (ret < 0) {
0426         pr_err("reg_r %04x failed %d\n", value, ret);
0427         gspca_dev->usb_err = ret;
0428         /*
0429          * Make sure the buffer is zeroed to avoid uninitialized
0430          * values.
0431          */
0432         memset(gspca_dev->usb_buf, 0, USB_BUF_SZ);
0433     }
0434 }
0435 
0436 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
0437 {
0438     int ret;
0439 
0440     if (gspca_dev->usb_err < 0)
0441         return;
0442     gspca_dbg(gspca_dev, D_USBO, "reg_w v: %04x i: %04x\n", value, index);
0443     ret = usb_control_msg(gspca_dev->dev,
0444             usb_sndctrlpipe(gspca_dev->dev, 0),
0445             0x0c,           /* request */
0446             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0447             value, index, NULL, 0,
0448             500);
0449     msleep(30);
0450     if (ret < 0) {
0451         pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
0452         gspca_dev->usb_err = ret;
0453     }
0454 }
0455 
0456 static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
0457         const u8 *data, int len)
0458 {
0459     int ret;
0460 
0461     if (gspca_dev->usb_err < 0)
0462         return;
0463     gspca_dbg(gspca_dev, D_USBO, "reg_wb v: %04x i: %04x %02x...%02x\n",
0464           value, index, *data, data[len - 1]);
0465     memcpy(gspca_dev->usb_buf, data, len);
0466     ret = usb_control_msg(gspca_dev->dev,
0467             usb_sndctrlpipe(gspca_dev->dev, 0),
0468             0x0c,           /* request */
0469             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0470             value, index, gspca_dev->usb_buf, len,
0471             1000);
0472     msleep(30);
0473     if (ret < 0) {
0474         pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
0475         gspca_dev->usb_err = ret;
0476     }
0477 }
0478 
0479 static void i2c_write(struct sd *sd,
0480             const struct i2c_write_cmd *cmd,
0481             int ncmds)
0482 {
0483     struct gspca_dev *gspca_dev = &sd->gspca_dev;
0484     const struct sensor_s *sensor;
0485     u16 val, idx;
0486     u8 *buf;
0487     int ret;
0488 
0489     if (gspca_dev->usb_err < 0)
0490         return;
0491 
0492     sensor = &sensor_tb[sd->sensor];
0493 
0494     val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
0495     idx = (cmd->val & 0xff00) | cmd->reg;
0496 
0497     buf = gspca_dev->usb_buf;
0498     *buf++ = sensor->i2c_dum;
0499     *buf++ = cmd->val;
0500 
0501     while (--ncmds > 0) {
0502         cmd++;
0503         *buf++ = cmd->reg;
0504         *buf++ = cmd->val >> 8;
0505         *buf++ = sensor->i2c_dum;
0506         *buf++ = cmd->val;
0507     }
0508 
0509     gspca_dbg(gspca_dev, D_USBO, "i2c_w v: %04x i: %04x %02x...%02x\n",
0510           val, idx, gspca_dev->usb_buf[0], buf[-1]);
0511     ret = usb_control_msg(gspca_dev->dev,
0512             usb_sndctrlpipe(gspca_dev->dev, 0),
0513             0x0c,           /* request */
0514             USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0515             val, idx,
0516             gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
0517             500);
0518     if (ret < 0) {
0519         pr_err("i2c_write failed %d\n", ret);
0520         gspca_dev->usb_err = ret;
0521     }
0522 }
0523 
0524 static void ucbus_write(struct gspca_dev *gspca_dev,
0525             const struct ucbus_write_cmd *cmd,
0526             int ncmds,
0527             int batchsize)
0528 {
0529     u8 *buf;
0530     u16 val, idx;
0531     int len, ret;
0532 
0533     if (gspca_dev->usb_err < 0)
0534         return;
0535 
0536     if ((batchsize - 1) * 3 > USB_BUF_SZ) {
0537         gspca_err(gspca_dev, "Bug: usb_buf overflow\n");
0538         gspca_dev->usb_err = -ENOMEM;
0539         return;
0540     }
0541 
0542     for (;;) {
0543         len = ncmds;
0544         if (len > batchsize)
0545             len = batchsize;
0546         ncmds -= len;
0547 
0548         val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
0549         idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
0550 
0551         buf = gspca_dev->usb_buf;
0552         while (--len > 0) {
0553             cmd++;
0554             *buf++ = cmd->bw_addr;
0555             *buf++ = cmd->bw_addr >> 8;
0556             *buf++ = cmd->bw_data;
0557         }
0558         if (buf != gspca_dev->usb_buf)
0559             gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
0560                   val, idx,
0561                   gspca_dev->usb_buf[0], buf[-1]);
0562         else
0563             gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
0564                   val, idx);
0565         ret = usb_control_msg(gspca_dev->dev,
0566                 usb_sndctrlpipe(gspca_dev->dev, 0),
0567                 0x0c,           /* request */
0568                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0569                 val, idx,
0570                 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
0571                 500);
0572         if (ret < 0) {
0573             pr_err("ucbus_write failed %d\n", ret);
0574             gspca_dev->usb_err = ret;
0575             return;
0576         }
0577         msleep(30);
0578         if (ncmds <= 0)
0579             break;
0580         cmd++;
0581     }
0582 }
0583 
0584 static void gpio_set(struct sd *sd, u16 val, u16 mask)
0585 {
0586     struct gspca_dev *gspca_dev = &sd->gspca_dev;
0587 
0588     if (mask & 0x00ff) {
0589         sd->gpio[0] &= ~mask;
0590         sd->gpio[0] |= val;
0591         reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
0592             ~sd->gpio[0] << 8);
0593     }
0594     mask >>= 8;
0595     val >>= 8;
0596     if (mask) {
0597         sd->gpio[1] &= ~mask;
0598         sd->gpio[1] |= val;
0599         reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
0600             ~sd->gpio[1] << 8);
0601     }
0602 }
0603 
0604 static void gpio_init(struct sd *sd,
0605             const u8 *gpio)
0606 {
0607     gpio_set(sd, *gpio++, 0x000f);
0608     gpio_set(sd, *gpio++, 0x000f);
0609     gpio_set(sd, *gpio++, 0x000f);
0610     gpio_set(sd, *gpio++, 0x000f);
0611     gpio_set(sd, *gpio, 0x000f);
0612 }
0613 
0614 static void bridge_init(struct sd *sd)
0615 {
0616     static const struct ucbus_write_cmd clkfreq_cmd = {
0617                 0xf031, 0   /* SQ930_CLKFREQ_60MHZ */
0618     };
0619 
0620     ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
0621 
0622     gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
0623 }
0624 
0625 static void cmos_probe(struct gspca_dev *gspca_dev)
0626 {
0627     struct sd *sd = (struct sd *) gspca_dev;
0628     int i;
0629     const struct sensor_s *sensor;
0630     static const u8 probe_order[] = {
0631 /*      SENSOR_LZ24BP,      (tested as ccd) */
0632         SENSOR_OV9630,
0633         SENSOR_MI0360,
0634         SENSOR_OV7660,
0635         SENSOR_MT9V111,
0636     };
0637 
0638     for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
0639         sensor = &sensor_tb[probe_order[i]];
0640         ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
0641         gpio_init(sd, sensor->gpio);
0642         msleep(100);
0643         reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
0644         msleep(100);
0645         if (gspca_dev->usb_buf[0] != 0)
0646             break;
0647     }
0648     if (i >= ARRAY_SIZE(probe_order)) {
0649         pr_err("Unknown sensor\n");
0650         gspca_dev->usb_err = -EINVAL;
0651         return;
0652     }
0653     sd->sensor = probe_order[i];
0654     switch (sd->sensor) {
0655     case SENSOR_OV7660:
0656     case SENSOR_OV9630:
0657         pr_err("Sensor %s not yet treated\n",
0658                sensor_tb[sd->sensor].name);
0659         gspca_dev->usb_err = -EINVAL;
0660         break;
0661     }
0662 }
0663 
0664 static void mt9v111_init(struct gspca_dev *gspca_dev)
0665 {
0666     int i, nwait;
0667     static const u8 cmd_001b[] = {
0668         0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
0669         0x00, 0x00, 0x00
0670     };
0671     static const u8 cmd_011b[][7] = {
0672         {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
0673         {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
0674         {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
0675         {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
0676     };
0677 
0678     reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
0679     for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
0680         reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
0681                 ARRAY_SIZE(cmd_011b[0]));
0682         msleep(400);
0683         nwait = 20;
0684         for (;;) {
0685             reg_r(gspca_dev, 0x031b, 1);
0686             if (gspca_dev->usb_buf[0] == 0
0687              || gspca_dev->usb_err != 0)
0688                 break;
0689             if (--nwait < 0) {
0690                 gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
0691                 gspca_dev->usb_err = -ETIME;
0692                 return;
0693             }
0694             msleep(50);
0695         }
0696     }
0697 }
0698 
0699 static void global_init(struct sd *sd, int first_time)
0700 {
0701     switch (sd->sensor) {
0702     case SENSOR_ICX098BQ:
0703         if (first_time)
0704             ucbus_write(&sd->gspca_dev,
0705                     icx098bq_start_0,
0706                     8, 8);
0707         gpio_init(sd, sensor_tb[sd->sensor].gpio);
0708         break;
0709     case SENSOR_LZ24BP:
0710         if (sd->type != Creative_live_motion)
0711             gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
0712         else
0713             gpio_set(sd, 0, 0x00ff);
0714         msleep(50);
0715         if (first_time)
0716             ucbus_write(&sd->gspca_dev,
0717                     lz24bp_start_0,
0718                     8, 8);
0719         gpio_init(sd, sensor_tb[sd->sensor].gpio);
0720         break;
0721     case SENSOR_MI0360:
0722         if (first_time)
0723             ucbus_write(&sd->gspca_dev,
0724                     mi0360_start_0,
0725                     ARRAY_SIZE(mi0360_start_0),
0726                     8);
0727         gpio_init(sd, sensor_tb[sd->sensor].gpio);
0728         gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
0729         break;
0730     default:
0731 /*  case SENSOR_MT9V111: */
0732         if (first_time)
0733             mt9v111_init(&sd->gspca_dev);
0734         else
0735             gpio_init(sd, sensor_tb[sd->sensor].gpio);
0736         break;
0737     }
0738 }
0739 
0740 static void lz24bp_ppl(struct sd *sd, u16 ppl)
0741 {
0742     struct ucbus_write_cmd cmds[2] = {
0743         {0xf810, ppl >> 8},
0744         {0xf811, ppl}
0745     };
0746 
0747     ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
0748 }
0749 
0750 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
0751 {
0752     struct sd *sd = (struct sd *) gspca_dev;
0753     int i, integclks, intstartclk, frameclks, min_frclk;
0754     const struct sensor_s *sensor;
0755     u16 cmd;
0756     u8 buf[15];
0757 
0758     integclks = expo;
0759     i = 0;
0760     cmd = SQ930_CTRL_SET_EXPOSURE;
0761 
0762     switch (sd->sensor) {
0763     case SENSOR_ICX098BQ:           /* ccd */
0764     case SENSOR_LZ24BP:
0765         min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
0766         if (integclks >= min_frclk) {
0767             intstartclk = 0;
0768             frameclks = integclks;
0769         } else {
0770             intstartclk = min_frclk - integclks;
0771             frameclks = min_frclk;
0772         }
0773         buf[i++] = intstartclk >> 8;
0774         buf[i++] = intstartclk;
0775         buf[i++] = frameclks >> 8;
0776         buf[i++] = frameclks;
0777         buf[i++] = gain;
0778         break;
0779     default:                /* cmos */
0780 /*  case SENSOR_MI0360: */
0781 /*  case SENSOR_MT9V111: */
0782         cmd |= 0x0100;
0783         sensor = &sensor_tb[sd->sensor];
0784         buf[i++] = sensor->i2c_addr;    /* i2c_slave_addr */
0785         buf[i++] = 0x08;    /* 2 * ni2c */
0786         buf[i++] = 0x09;    /* reg = shutter width */
0787         buf[i++] = integclks >> 8; /* val H */
0788         buf[i++] = sensor->i2c_dum;
0789         buf[i++] = integclks;   /* val L */
0790         buf[i++] = 0x35;    /* reg = global gain */
0791         buf[i++] = 0x00;    /* val H */
0792         buf[i++] = sensor->i2c_dum;
0793         buf[i++] = 0x80 + gain / 2; /* val L */
0794         buf[i++] = 0x00;
0795         buf[i++] = 0x00;
0796         buf[i++] = 0x00;
0797         buf[i++] = 0x00;
0798         buf[i++] = 0x83;
0799         break;
0800     }
0801     reg_wb(gspca_dev, cmd, 0, buf, i);
0802 }
0803 
0804 /* This function is called at probe time just before sd_init */
0805 static int sd_config(struct gspca_dev *gspca_dev,
0806         const struct usb_device_id *id)
0807 {
0808     struct sd *sd = (struct sd *) gspca_dev;
0809     struct cam *cam = &gspca_dev->cam;
0810 
0811     sd->sensor = id->driver_info >> 8;
0812     sd->type = id->driver_info;
0813 
0814     cam->cam_mode = vga_mode;
0815     cam->nmodes = ARRAY_SIZE(vga_mode);
0816 
0817     cam->bulk = 1;
0818 
0819     return 0;
0820 }
0821 
0822 /* this function is called at probe and resume time */
0823 static int sd_init(struct gspca_dev *gspca_dev)
0824 {
0825     struct sd *sd = (struct sd *) gspca_dev;
0826 
0827     sd->gpio[0] = sd->gpio[1] = 0xff;   /* force gpio rewrite */
0828 
0829 /*fixme: is this needed for icx098bp and mi0360?
0830     if (sd->sensor != SENSOR_LZ24BP)
0831         reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
0832  */
0833 
0834     reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
0835     if (gspca_dev->usb_err < 0)
0836         return gspca_dev->usb_err;
0837 
0838 /* it returns:
0839  * 03 00 12 93 0b f6 c9 00  live! ultra
0840  * 03 00 07 93 0b f6 ca 00  live! ultra for notebook
0841  * 03 00 12 93 0b fe c8 00  Trust WB-3500T
0842  * 02 00 06 93 0b fe c8 00  Joy-IT 318S
0843  * 03 00 12 93 0b f6 cf 00  icam tracer - sensor icx098bq
0844  * 02 00 12 93 0b fe cf 00  ProQ Motion Webcam
0845  *
0846  * byte
0847  * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
0848  * 1: 00
0849  * 2: 06 / 07 / 12 = mode webcam? firmware??
0850  * 3: 93 chip = 930b (930b or 930c)
0851  * 4: 0b
0852  * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
0853  * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
0854  * 7: 00
0855  */
0856     gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 8, gspca_dev->usb_buf);
0857 
0858     bridge_init(sd);
0859 
0860     if (sd->sensor == SENSOR_MI0360) {
0861 
0862         /* no sensor probe for icam tracer */
0863         if (gspca_dev->usb_buf[5] == 0xf6)  /* if ccd */
0864             sd->sensor = SENSOR_ICX098BQ;
0865         else
0866             cmos_probe(gspca_dev);
0867     }
0868     if (gspca_dev->usb_err >= 0) {
0869         gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
0870               sensor_tb[sd->sensor].name);
0871         global_init(sd, 1);
0872     }
0873     return gspca_dev->usb_err;
0874 }
0875 
0876 /* send the start/stop commands to the webcam */
0877 static void send_start(struct gspca_dev *gspca_dev)
0878 {
0879     struct sd *sd = (struct sd *) gspca_dev;
0880     const struct cap_s *cap;
0881     int mode;
0882 
0883     mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
0884     cap = &capconfig[sd->sensor][mode];
0885     reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
0886             0x0a00 | cap->cc_sizeid,
0887             cap->cc_bytes, 32);
0888 }
0889 
0890 static void send_stop(struct gspca_dev *gspca_dev)
0891 {
0892     reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
0893 }
0894 
0895 /* function called at start time before URB creation */
0896 static int sd_isoc_init(struct gspca_dev *gspca_dev)
0897 {
0898     struct sd *sd = (struct sd *) gspca_dev;
0899 
0900     gspca_dev->cam.bulk_nurbs = 1;  /* there must be one URB only */
0901     sd->do_ctrl = 0;
0902     gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
0903             gspca_dev->pixfmt.height + 8;
0904     return 0;
0905 }
0906 
0907 /* start the capture */
0908 static int sd_start(struct gspca_dev *gspca_dev)
0909 {
0910     struct sd *sd = (struct sd *) gspca_dev;
0911     int mode;
0912 
0913     bridge_init(sd);
0914     global_init(sd, 0);
0915     msleep(100);
0916 
0917     switch (sd->sensor) {
0918     case SENSOR_ICX098BQ:
0919         ucbus_write(gspca_dev, icx098bq_start_0,
0920                 ARRAY_SIZE(icx098bq_start_0),
0921                 8);
0922         ucbus_write(gspca_dev, icx098bq_start_1,
0923                 ARRAY_SIZE(icx098bq_start_1),
0924                 5);
0925         ucbus_write(gspca_dev, icx098bq_start_2,
0926                 ARRAY_SIZE(icx098bq_start_2),
0927                 6);
0928         msleep(50);
0929 
0930         /* 1st start */
0931         send_start(gspca_dev);
0932         gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
0933         msleep(70);
0934         reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
0935         gpio_set(sd, 0x7f, 0x00ff);
0936 
0937         /* 2nd start */
0938         send_start(gspca_dev);
0939         gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
0940         goto out;
0941     case SENSOR_LZ24BP:
0942         ucbus_write(gspca_dev, lz24bp_start_0,
0943                 ARRAY_SIZE(lz24bp_start_0),
0944                 8);
0945         if (sd->type != Creative_live_motion)
0946             ucbus_write(gspca_dev, lz24bp_start_1_gen,
0947                     ARRAY_SIZE(lz24bp_start_1_gen),
0948                     5);
0949         else
0950             ucbus_write(gspca_dev, lz24bp_start_1_clm,
0951                     ARRAY_SIZE(lz24bp_start_1_clm),
0952                     5);
0953         ucbus_write(gspca_dev, lz24bp_start_2,
0954                 ARRAY_SIZE(lz24bp_start_2),
0955                 6);
0956         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
0957         lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
0958         msleep(10);
0959         break;
0960     case SENSOR_MI0360:
0961         ucbus_write(gspca_dev, mi0360_start_0,
0962                 ARRAY_SIZE(mi0360_start_0),
0963                 8);
0964         i2c_write(sd, mi0360_init_23,
0965                 ARRAY_SIZE(mi0360_init_23));
0966         i2c_write(sd, mi0360_init_24,
0967                 ARRAY_SIZE(mi0360_init_24));
0968         i2c_write(sd, mi0360_init_25,
0969                 ARRAY_SIZE(mi0360_init_25));
0970         ucbus_write(gspca_dev, mi0360_start_1,
0971                 ARRAY_SIZE(mi0360_start_1),
0972                 5);
0973         i2c_write(sd, mi0360_start_2,
0974                 ARRAY_SIZE(mi0360_start_2));
0975         i2c_write(sd, mi0360_start_3,
0976                 ARRAY_SIZE(mi0360_start_3));
0977 
0978         /* 1st start */
0979         send_start(gspca_dev);
0980         msleep(60);
0981         send_stop(gspca_dev);
0982 
0983         i2c_write(sd,
0984             mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
0985         break;
0986     default:
0987 /*  case SENSOR_MT9V111: */
0988         ucbus_write(gspca_dev, mi0360_start_0,
0989                 ARRAY_SIZE(mi0360_start_0),
0990                 8);
0991         i2c_write(sd, mt9v111_init_0,
0992                 ARRAY_SIZE(mt9v111_init_0));
0993         i2c_write(sd, mt9v111_init_1,
0994                 ARRAY_SIZE(mt9v111_init_1));
0995         i2c_write(sd, mt9v111_init_2,
0996                 ARRAY_SIZE(mt9v111_init_2));
0997         ucbus_write(gspca_dev, mt9v111_start_1,
0998                 ARRAY_SIZE(mt9v111_start_1),
0999                 5);
1000         i2c_write(sd, mt9v111_init_3,
1001                 ARRAY_SIZE(mt9v111_init_3));
1002         i2c_write(sd, mt9v111_init_4,
1003                 ARRAY_SIZE(mt9v111_init_4));
1004         break;
1005     }
1006 
1007     send_start(gspca_dev);
1008 out:
1009     msleep(1000);
1010 
1011     if (sd->sensor == SENSOR_MT9V111)
1012         gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1013 
1014     sd->do_ctrl = 1;    /* set the exposure */
1015 
1016     return gspca_dev->usb_err;
1017 }
1018 
1019 static void sd_stopN(struct gspca_dev *gspca_dev)
1020 {
1021     struct sd *sd = (struct sd *) gspca_dev;
1022 
1023     if (sd->sensor == SENSOR_MT9V111)
1024         gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
1025     send_stop(gspca_dev);
1026 }
1027 
1028 /* function called when the application gets a new frame */
1029 /* It sets the exposure if required and restart the bulk transfer. */
1030 static void sd_dq_callback(struct gspca_dev *gspca_dev)
1031 {
1032     struct sd *sd = (struct sd *) gspca_dev;
1033     int ret;
1034 
1035     if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1036         return;
1037     sd->do_ctrl = 0;
1038 
1039     setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1040             v4l2_ctrl_g_ctrl(sd->gain));
1041 
1042     gspca_dev->cam.bulk_nurbs = 1;
1043     ret = usb_submit_urb(gspca_dev->urb[0], GFP_KERNEL);
1044     if (ret < 0)
1045         pr_err("sd_dq_callback() err %d\n", ret);
1046 
1047     /* wait a little time, otherwise the webcam crashes */
1048     msleep(100);
1049 }
1050 
1051 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1052             u8 *data,       /* isoc packet */
1053             int len)        /* iso packet length */
1054 {
1055     struct sd *sd = (struct sd *) gspca_dev;
1056 
1057     if (sd->do_ctrl)
1058         gspca_dev->cam.bulk_nurbs = 0;
1059     gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
1060     gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
1061     gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1062 }
1063 
1064 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1065 {
1066     struct gspca_dev *gspca_dev =
1067         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1068     struct sd *sd = (struct sd *) gspca_dev;
1069 
1070     gspca_dev->usb_err = 0;
1071 
1072     if (!gspca_dev->streaming)
1073         return 0;
1074 
1075     switch (ctrl->id) {
1076     case V4L2_CID_EXPOSURE:
1077         setexposure(gspca_dev, ctrl->val, sd->gain->val);
1078         break;
1079     }
1080     return gspca_dev->usb_err;
1081 }
1082 
1083 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1084     .s_ctrl = sd_s_ctrl,
1085 };
1086 
1087 static int sd_init_controls(struct gspca_dev *gspca_dev)
1088 {
1089     struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1090     struct sd *sd = (struct sd *) gspca_dev;
1091 
1092     gspca_dev->vdev.ctrl_handler = hdl;
1093     v4l2_ctrl_handler_init(hdl, 2);
1094     sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1095             V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
1096     sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1097             V4L2_CID_GAIN, 1, 255, 1, 0x8d);
1098 
1099     if (hdl->error) {
1100         pr_err("Could not initialize controls\n");
1101         return hdl->error;
1102     }
1103     v4l2_ctrl_cluster(2, &sd->exposure);
1104     return 0;
1105 }
1106 
1107 /* sub-driver description */
1108 static const struct sd_desc sd_desc = {
1109     .name   = MODULE_NAME,
1110     .config = sd_config,
1111     .init   = sd_init,
1112     .init_controls = sd_init_controls,
1113     .isoc_init = sd_isoc_init,
1114     .start  = sd_start,
1115     .stopN  = sd_stopN,
1116     .pkt_scan = sd_pkt_scan,
1117     .dq_callback = sd_dq_callback,
1118 };
1119 
1120 /* Table of supported USB devices */
1121 #define ST(sensor, type) \
1122     .driver_info = (SENSOR_ ## sensor << 8) \
1123             | (type)
1124 static const struct usb_device_id device_table[] = {
1125     {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
1126     {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
1127     {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
1128     {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
1129     {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
1130     {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1131     {}
1132 };
1133 MODULE_DEVICE_TABLE(usb, device_table);
1134 
1135 
1136 /* -- device connect -- */
1137 static int sd_probe(struct usb_interface *intf,
1138         const struct usb_device_id *id)
1139 {
1140     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1141             THIS_MODULE);
1142 }
1143 
1144 static struct usb_driver sd_driver = {
1145     .name       = MODULE_NAME,
1146     .id_table   = device_table,
1147     .probe      = sd_probe,
1148     .disconnect = gspca_disconnect,
1149 #ifdef CONFIG_PM
1150     .suspend    = gspca_suspend,
1151     .resume     = gspca_resume,
1152     .reset_resume = gspca_resume,
1153 #endif
1154 };
1155 
1156 module_usb_driver(sd_driver);