linux/drivers/media/usb/gspca/sq930x.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * SQ930x subdriver
   4 *
   5 * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
   6 * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
   7 * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
   8 */
   9
  10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11
  12#define MODULE_NAME "sq930x"
  13
  14#include "gspca.h"
  15
  16MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
  17                "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
  18                "Sam Revitch <samr7@cs.washington.edu>");
  19MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
  20MODULE_LICENSE("GPL");
  21
  22/* Structure to hold all of our device specific stuff */
  23struct sd {
  24        struct gspca_dev gspca_dev;     /* !! must be the first item */
  25
  26        struct { /* exposure/gain control cluster */
  27                struct v4l2_ctrl *exposure;
  28                struct v4l2_ctrl *gain;
  29        };
  30
  31        u8 do_ctrl;
  32        u8 gpio[2];
  33        u8 sensor;
  34        u8 type;
  35#define Generic 0
  36#define Creative_live_motion 1
  37};
  38enum sensors {
  39        SENSOR_ICX098BQ,
  40        SENSOR_LZ24BP,
  41        SENSOR_MI0360,
  42        SENSOR_MT9V111,         /* = MI360SOC */
  43        SENSOR_OV7660,
  44        SENSOR_OV9630,
  45};
  46
  47static struct v4l2_pix_format vga_mode[] = {
  48        {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
  49                .bytesperline = 320,
  50                .sizeimage = 320 * 240,
  51                .colorspace = V4L2_COLORSPACE_SRGB,
  52                .priv = 0},
  53        {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
  54                .bytesperline = 640,
  55                .sizeimage = 640 * 480,
  56                .colorspace = V4L2_COLORSPACE_SRGB,
  57                .priv = 1},
  58};
  59
  60/* sq930x registers */
  61#define SQ930_CTRL_UCBUS_IO     0x0001
  62#define SQ930_CTRL_I2C_IO       0x0002
  63#define SQ930_CTRL_GPIO         0x0005
  64#define SQ930_CTRL_CAP_START    0x0010
  65#define SQ930_CTRL_CAP_STOP     0x0011
  66#define SQ930_CTRL_SET_EXPOSURE 0x001d
  67#define SQ930_CTRL_RESET        0x001e
  68#define SQ930_CTRL_GET_DEV_INFO 0x001f
  69
  70/* gpio 1 (8..15) */
  71#define SQ930_GPIO_DFL_I2C_SDA  0x0001
  72#define SQ930_GPIO_DFL_I2C_SCL  0x0002
  73#define SQ930_GPIO_RSTBAR       0x0004
  74#define SQ930_GPIO_EXTRA1       0x0040
  75#define SQ930_GPIO_EXTRA2       0x0080
  76/* gpio 3 (24..31) */
  77#define SQ930_GPIO_POWER        0x0200
  78#define SQ930_GPIO_DFL_LED      0x1000
  79
  80struct ucbus_write_cmd {
  81        u16     bw_addr;
  82        u8      bw_data;
  83};
  84struct i2c_write_cmd {
  85        u8      reg;
  86        u16     val;
  87};
  88
  89static const struct ucbus_write_cmd icx098bq_start_0[] = {
  90        {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
  91        {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
  92        {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
  93        {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
  94        {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
  95        {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
  96        {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
  97        {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
  98        {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
  99        {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
 100        {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
 101        {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
 102        {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
 103        {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
 104        {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
 105        {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
 106        {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
 107        {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
 108        {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
 109        {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
 110        {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
 111        {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
 112        {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
 113        {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
 114        {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
 115        {0xf800, 0x03}
 116};
 117static const struct ucbus_write_cmd icx098bq_start_1[] = {
 118        {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 119        {0xf5f4, 0xc0},
 120        {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 121        {0xf5f4, 0xc0},
 122        {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
 123        {0xf5f9, 0x00}
 124};
 125
 126static const struct ucbus_write_cmd icx098bq_start_2[] = {
 127        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
 128        {0xf807, 0x7f}, {0xf800, 0x03},
 129        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
 130        {0xf807, 0x7f}, {0xf800, 0x03},
 131        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
 132        {0xf807, 0x7f}, {0xf800, 0x03},
 133        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
 134        {0xf807, 0x7f}, {0xf800, 0x03}
 135};
 136
 137static const struct ucbus_write_cmd lz24bp_start_0[] = {
 138        {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
 139        {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
 140        {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
 141        {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
 142        {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
 143        {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
 144        {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
 145        {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
 146        {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
 147        {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
 148        {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
 149        {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
 150        {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
 151        {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
 152        {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
 153        {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
 154        {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
 155        {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
 156        {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
 157        {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
 158        {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
 159        {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
 160        {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
 161        {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
 162        {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
 163        {0xf800, 0x03}
 164};
 165static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
 166        {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 167        {0xf5f4, 0xb3},
 168        {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 169        {0xf5f4, 0xb3},
 170        {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
 171        {0xf5f9, 0x00}
 172};
 173
 174static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
 175        {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
 176        {0xf5f4, 0xc0},
 177        {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
 178        {0xf5f4, 0xc0},
 179        {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
 180        {0xf5f9, 0x00}
 181};
 182
 183static const struct ucbus_write_cmd lz24bp_start_2[] = {
 184        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
 185        {0xf807, 0x7f}, {0xf800, 0x03},
 186        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
 187        {0xf807, 0x7f}, {0xf800, 0x03},
 188        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
 189        {0xf807, 0x7f}, {0xf800, 0x03},
 190        {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
 191        {0xf807, 0x7f}, {0xf800, 0x03}
 192};
 193
 194static const struct ucbus_write_cmd mi0360_start_0[] = {
 195        {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
 196        {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
 197};
 198static const struct i2c_write_cmd mi0360_init_23[] = {
 199        {0x30, 0x0040},         /* reserved - def 0x0005 */
 200        {0x31, 0x0000},         /* reserved - def 0x002a */
 201        {0x34, 0x0100},         /* reserved - def 0x0100 */
 202        {0x3d, 0x068f},         /* reserved - def 0x068f */
 203};
 204static const struct i2c_write_cmd mi0360_init_24[] = {
 205        {0x03, 0x01e5},         /* window height */
 206        {0x04, 0x0285},         /* window width */
 207};
 208static const struct i2c_write_cmd mi0360_init_25[] = {
 209        {0x35, 0x0020},         /* global gain */
 210        {0x2b, 0x0020},         /* green1 gain */
 211        {0x2c, 0x002a},         /* blue gain */
 212        {0x2d, 0x0028},         /* red gain */
 213        {0x2e, 0x0020},         /* green2 gain */
 214};
 215static const struct ucbus_write_cmd mi0360_start_1[] = {
 216        {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 217        {0xf5f4, 0xa6},
 218        {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 219        {0xf5f4, 0xa6},
 220        {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
 221        {0xf5f9, 0x00}
 222};
 223static const struct i2c_write_cmd mi0360_start_2[] = {
 224        {0x62, 0x041d},         /* reserved - def 0x0418 */
 225};
 226static const struct i2c_write_cmd mi0360_start_3[] = {
 227        {0x05, 0x007b},         /* horiz blanking */
 228};
 229static const struct i2c_write_cmd mi0360_start_4[] = {
 230        {0x05, 0x03f5},         /* horiz blanking */
 231};
 232
 233static const struct i2c_write_cmd mt9v111_init_0[] = {
 234        {0x01, 0x0001},         /* select IFP/SOC registers */
 235        {0x06, 0x300c},         /* operating mode control */
 236        {0x08, 0xcc00},         /* output format control (RGB) */
 237        {0x01, 0x0004},         /* select sensor core registers */
 238};
 239static const struct i2c_write_cmd mt9v111_init_1[] = {
 240        {0x03, 0x01e5},         /* window height */
 241        {0x04, 0x0285},         /* window width */
 242};
 243static const struct i2c_write_cmd mt9v111_init_2[] = {
 244        {0x30, 0x7800},
 245        {0x31, 0x0000},
 246        {0x07, 0x3002},         /* output control */
 247        {0x35, 0x0020},         /* global gain */
 248        {0x2b, 0x0020},         /* green1 gain */
 249        {0x2c, 0x0020},         /* blue gain */
 250        {0x2d, 0x0020},         /* red gain */
 251        {0x2e, 0x0020},         /* green2 gain */
 252};
 253static const struct ucbus_write_cmd mt9v111_start_1[] = {
 254        {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 255        {0xf5f4, 0xaa},
 256        {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
 257        {0xf5f4, 0xaa},
 258        {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
 259        {0xf5f9, 0x0a}
 260};
 261static const struct i2c_write_cmd mt9v111_init_3[] = {
 262        {0x62, 0x0405},
 263};
 264static const struct i2c_write_cmd mt9v111_init_4[] = {
 265/*      {0x05, 0x00ce}, */
 266        {0x05, 0x005d},         /* horizontal blanking */
 267};
 268
 269static const struct ucbus_write_cmd ov7660_start_0[] = {
 270        {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
 271        {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
 272};
 273
 274static const struct ucbus_write_cmd ov9630_start_0[] = {
 275        {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
 276        {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
 277};
 278
 279/* start parameters indexed by [sensor][mode] */
 280static const struct cap_s {
 281        u8      cc_sizeid;
 282        u8      cc_bytes[32];
 283} capconfig[4][2] = {
 284        [SENSOR_ICX098BQ] = {
 285                {2,                             /* Bayer 320x240 */
 286                  {0x05, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
 287                   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 288                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0,
 289                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 290                {4,                             /* Bayer 640x480 */
 291                  {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
 292                   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 293                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 294                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 295        },
 296        [SENSOR_LZ24BP] = {
 297                {2,                             /* Bayer 320x240 */
 298                  {0x05, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
 299                   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 300                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 301                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 302                {4,                             /* Bayer 640x480 */
 303                  {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
 304                   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 305                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 306                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 307        },
 308        [SENSOR_MI0360] = {
 309                {2,                             /* Bayer 320x240 */
 310                  {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
 311                   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 312                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 313                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 314                {4,                             /* Bayer 640x480 */
 315                  {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
 316                   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 317                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 318                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 319        },
 320        [SENSOR_MT9V111] = {
 321                {2,                             /* Bayer 320x240 */
 322                  {0x05, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
 323                   0x01, 0x01, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 324                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 325                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 326                {4,                             /* Bayer 640x480 */
 327                  {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
 328                   0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
 329                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 330                   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} },
 331        },
 332};
 333
 334struct sensor_s {
 335        const char *name;
 336        u8 i2c_addr;
 337        u8 i2c_dum;
 338        u8 gpio[5];
 339        u8 cmd_len;
 340        const struct ucbus_write_cmd *cmd;
 341};
 342
 343static const struct sensor_s sensor_tb[] = {
 344        [SENSOR_ICX098BQ] = {
 345                "icx098bp",
 346                0x00, 0x00,
 347                {0,
 348                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 349                 SQ930_GPIO_DFL_I2C_SDA,
 350                 0,
 351                 SQ930_GPIO_RSTBAR
 352                },
 353                8, icx098bq_start_0
 354            },
 355        [SENSOR_LZ24BP] = {
 356                "lz24bp",
 357                0x00, 0x00,
 358                {0,
 359                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 360                 SQ930_GPIO_DFL_I2C_SDA,
 361                 0,
 362                 SQ930_GPIO_RSTBAR
 363                },
 364                8, lz24bp_start_0
 365            },
 366        [SENSOR_MI0360] = {
 367                "mi0360",
 368                0x5d, 0x80,
 369                {SQ930_GPIO_RSTBAR,
 370                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 371                 SQ930_GPIO_DFL_I2C_SDA,
 372                 0,
 373                 0
 374                },
 375                7, mi0360_start_0
 376            },
 377        [SENSOR_MT9V111] = {
 378                "mt9v111",
 379                0x5c, 0x7f,
 380                {SQ930_GPIO_RSTBAR,
 381                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 382                 SQ930_GPIO_DFL_I2C_SDA,
 383                 0,
 384                 0
 385                },
 386                7, mi0360_start_0
 387            },
 388        [SENSOR_OV7660] = {
 389                "ov7660",
 390                0x21, 0x00,
 391                {0,
 392                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 393                 SQ930_GPIO_DFL_I2C_SDA,
 394                 0,
 395                 SQ930_GPIO_RSTBAR
 396                },
 397                7, ov7660_start_0
 398            },
 399        [SENSOR_OV9630] = {
 400                "ov9630",
 401                0x30, 0x00,
 402                {0,
 403                 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
 404                 SQ930_GPIO_DFL_I2C_SDA,
 405                 0,
 406                 SQ930_GPIO_RSTBAR
 407                },
 408                7, ov9630_start_0
 409            },
 410};
 411
 412static void reg_r(struct gspca_dev *gspca_dev,
 413                u16 value, int len)
 414{
 415        int ret;
 416
 417        if (gspca_dev->usb_err < 0)
 418                return;
 419        ret = usb_control_msg(gspca_dev->dev,
 420                        usb_rcvctrlpipe(gspca_dev->dev, 0),
 421                        0x0c,
 422                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 423                        value, 0, gspca_dev->usb_buf, len,
 424                        500);
 425        if (ret < 0) {
 426                pr_err("reg_r %04x failed %d\n", value, ret);
 427                gspca_dev->usb_err = ret;
 428        }
 429}
 430
 431static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
 432{
 433        int ret;
 434
 435        if (gspca_dev->usb_err < 0)
 436                return;
 437        gspca_dbg(gspca_dev, D_USBO, "reg_w v: %04x i: %04x\n", value, index);
 438        ret = usb_control_msg(gspca_dev->dev,
 439                        usb_sndctrlpipe(gspca_dev->dev, 0),
 440                        0x0c,                   /* request */
 441                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 442                        value, index, NULL, 0,
 443                        500);
 444        msleep(30);
 445        if (ret < 0) {
 446                pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
 447                gspca_dev->usb_err = ret;
 448        }
 449}
 450
 451static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
 452                const u8 *data, int len)
 453{
 454        int ret;
 455
 456        if (gspca_dev->usb_err < 0)
 457                return;
 458        gspca_dbg(gspca_dev, D_USBO, "reg_wb v: %04x i: %04x %02x...%02x\n",
 459                  value, index, *data, data[len - 1]);
 460        memcpy(gspca_dev->usb_buf, data, len);
 461        ret = usb_control_msg(gspca_dev->dev,
 462                        usb_sndctrlpipe(gspca_dev->dev, 0),
 463                        0x0c,                   /* request */
 464                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 465                        value, index, gspca_dev->usb_buf, len,
 466                        1000);
 467        msleep(30);
 468        if (ret < 0) {
 469                pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
 470                gspca_dev->usb_err = ret;
 471        }
 472}
 473
 474static void i2c_write(struct sd *sd,
 475                        const struct i2c_write_cmd *cmd,
 476                        int ncmds)
 477{
 478        struct gspca_dev *gspca_dev = &sd->gspca_dev;
 479        const struct sensor_s *sensor;
 480        u16 val, idx;
 481        u8 *buf;
 482        int ret;
 483
 484        if (gspca_dev->usb_err < 0)
 485                return;
 486
 487        sensor = &sensor_tb[sd->sensor];
 488
 489        val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
 490        idx = (cmd->val & 0xff00) | cmd->reg;
 491
 492        buf = gspca_dev->usb_buf;
 493        *buf++ = sensor->i2c_dum;
 494        *buf++ = cmd->val;
 495
 496        while (--ncmds > 0) {
 497                cmd++;
 498                *buf++ = cmd->reg;
 499                *buf++ = cmd->val >> 8;
 500                *buf++ = sensor->i2c_dum;
 501                *buf++ = cmd->val;
 502        }
 503
 504        gspca_dbg(gspca_dev, D_USBO, "i2c_w v: %04x i: %04x %02x...%02x\n",
 505                  val, idx, gspca_dev->usb_buf[0], buf[-1]);
 506        ret = usb_control_msg(gspca_dev->dev,
 507                        usb_sndctrlpipe(gspca_dev->dev, 0),
 508                        0x0c,                   /* request */
 509                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 510                        val, idx,
 511                        gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
 512                        500);
 513        if (ret < 0) {
 514                pr_err("i2c_write failed %d\n", ret);
 515                gspca_dev->usb_err = ret;
 516        }
 517}
 518
 519static void ucbus_write(struct gspca_dev *gspca_dev,
 520                        const struct ucbus_write_cmd *cmd,
 521                        int ncmds,
 522                        int batchsize)
 523{
 524        u8 *buf;
 525        u16 val, idx;
 526        int len, ret;
 527
 528        if (gspca_dev->usb_err < 0)
 529                return;
 530
 531        if ((batchsize - 1) * 3 > USB_BUF_SZ) {
 532                gspca_err(gspca_dev, "Bug: usb_buf overflow\n");
 533                gspca_dev->usb_err = -ENOMEM;
 534                return;
 535        }
 536
 537        for (;;) {
 538                len = ncmds;
 539                if (len > batchsize)
 540                        len = batchsize;
 541                ncmds -= len;
 542
 543                val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
 544                idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
 545
 546                buf = gspca_dev->usb_buf;
 547                while (--len > 0) {
 548                        cmd++;
 549                        *buf++ = cmd->bw_addr;
 550                        *buf++ = cmd->bw_addr >> 8;
 551                        *buf++ = cmd->bw_data;
 552                }
 553                if (buf != gspca_dev->usb_buf)
 554                        gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
 555                                  val, idx,
 556                                  gspca_dev->usb_buf[0], buf[-1]);
 557                else
 558                        gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
 559                                  val, idx);
 560                ret = usb_control_msg(gspca_dev->dev,
 561                                usb_sndctrlpipe(gspca_dev->dev, 0),
 562                                0x0c,                   /* request */
 563                           USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
 564                                val, idx,
 565                                gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
 566                                500);
 567                if (ret < 0) {
 568                        pr_err("ucbus_write failed %d\n", ret);
 569                        gspca_dev->usb_err = ret;
 570                        return;
 571                }
 572                msleep(30);
 573                if (ncmds <= 0)
 574                        break;
 575                cmd++;
 576        }
 577}
 578
 579static void gpio_set(struct sd *sd, u16 val, u16 mask)
 580{
 581        struct gspca_dev *gspca_dev = &sd->gspca_dev;
 582
 583        if (mask & 0x00ff) {
 584                sd->gpio[0] &= ~mask;
 585                sd->gpio[0] |= val;
 586                reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
 587                        ~sd->gpio[0] << 8);
 588        }
 589        mask >>= 8;
 590        val >>= 8;
 591        if (mask) {
 592                sd->gpio[1] &= ~mask;
 593                sd->gpio[1] |= val;
 594                reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
 595                        ~sd->gpio[1] << 8);
 596        }
 597}
 598
 599static void gpio_init(struct sd *sd,
 600                        const u8 *gpio)
 601{
 602        gpio_set(sd, *gpio++, 0x000f);
 603        gpio_set(sd, *gpio++, 0x000f);
 604        gpio_set(sd, *gpio++, 0x000f);
 605        gpio_set(sd, *gpio++, 0x000f);
 606        gpio_set(sd, *gpio, 0x000f);
 607}
 608
 609static void bridge_init(struct sd *sd)
 610{
 611        static const struct ucbus_write_cmd clkfreq_cmd = {
 612                                0xf031, 0       /* SQ930_CLKFREQ_60MHZ */
 613        };
 614
 615        ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
 616
 617        gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
 618}
 619
 620static void cmos_probe(struct gspca_dev *gspca_dev)
 621{
 622        struct sd *sd = (struct sd *) gspca_dev;
 623        int i;
 624        const struct sensor_s *sensor;
 625        static const u8 probe_order[] = {
 626/*              SENSOR_LZ24BP,          (tested as ccd) */
 627                SENSOR_OV9630,
 628                SENSOR_MI0360,
 629                SENSOR_OV7660,
 630                SENSOR_MT9V111,
 631        };
 632
 633        for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
 634                sensor = &sensor_tb[probe_order[i]];
 635                ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
 636                gpio_init(sd, sensor->gpio);
 637                msleep(100);
 638                reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
 639                msleep(100);
 640                if (gspca_dev->usb_buf[0] != 0)
 641                        break;
 642        }
 643        if (i >= ARRAY_SIZE(probe_order)) {
 644                pr_err("Unknown sensor\n");
 645                gspca_dev->usb_err = -EINVAL;
 646                return;
 647        }
 648        sd->sensor = probe_order[i];
 649        switch (sd->sensor) {
 650        case SENSOR_OV7660:
 651        case SENSOR_OV9630:
 652                pr_err("Sensor %s not yet treated\n",
 653                       sensor_tb[sd->sensor].name);
 654                gspca_dev->usb_err = -EINVAL;
 655                break;
 656        }
 657}
 658
 659static void mt9v111_init(struct gspca_dev *gspca_dev)
 660{
 661        int i, nwait;
 662        static const u8 cmd_001b[] = {
 663                0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
 664                0x00, 0x00, 0x00
 665        };
 666        static const u8 cmd_011b[][7] = {
 667                {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
 668                {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
 669                {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
 670                {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
 671        };
 672
 673        reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
 674        for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
 675                reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
 676                                ARRAY_SIZE(cmd_011b[0]));
 677                msleep(400);
 678                nwait = 20;
 679                for (;;) {
 680                        reg_r(gspca_dev, 0x031b, 1);
 681                        if (gspca_dev->usb_buf[0] == 0
 682                         || gspca_dev->usb_err != 0)
 683                                break;
 684                        if (--nwait < 0) {
 685                                gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
 686                                gspca_dev->usb_err = -ETIME;
 687                                return;
 688                        }
 689                        msleep(50);
 690                }
 691        }
 692}
 693
 694static void global_init(struct sd *sd, int first_time)
 695{
 696        switch (sd->sensor) {
 697        case SENSOR_ICX098BQ:
 698                if (first_time)
 699                        ucbus_write(&sd->gspca_dev,
 700                                        icx098bq_start_0,
 701                                        8, 8);
 702                gpio_init(sd, sensor_tb[sd->sensor].gpio);
 703                break;
 704        case SENSOR_LZ24BP:
 705                if (sd->type != Creative_live_motion)
 706                        gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
 707                else
 708                        gpio_set(sd, 0, 0x00ff);
 709                msleep(50);
 710                if (first_time)
 711                        ucbus_write(&sd->gspca_dev,
 712                                        lz24bp_start_0,
 713                                        8, 8);
 714                gpio_init(sd, sensor_tb[sd->sensor].gpio);
 715                break;
 716        case SENSOR_MI0360:
 717                if (first_time)
 718                        ucbus_write(&sd->gspca_dev,
 719                                        mi0360_start_0,
 720                                        ARRAY_SIZE(mi0360_start_0),
 721                                        8);
 722                gpio_init(sd, sensor_tb[sd->sensor].gpio);
 723                gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
 724                break;
 725        default:
 726/*      case SENSOR_MT9V111: */
 727                if (first_time)
 728                        mt9v111_init(&sd->gspca_dev);
 729                else
 730                        gpio_init(sd, sensor_tb[sd->sensor].gpio);
 731                break;
 732        }
 733}
 734
 735static void lz24bp_ppl(struct sd *sd, u16 ppl)
 736{
 737        struct ucbus_write_cmd cmds[2] = {
 738                {0xf810, ppl >> 8},
 739                {0xf811, ppl}
 740        };
 741
 742        ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
 743}
 744
 745static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
 746{
 747        struct sd *sd = (struct sd *) gspca_dev;
 748        int i, integclks, intstartclk, frameclks, min_frclk;
 749        const struct sensor_s *sensor;
 750        u16 cmd;
 751        u8 buf[15];
 752
 753        integclks = expo;
 754        i = 0;
 755        cmd = SQ930_CTRL_SET_EXPOSURE;
 756
 757        switch (sd->sensor) {
 758        case SENSOR_ICX098BQ:                   /* ccd */
 759        case SENSOR_LZ24BP:
 760                min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
 761                if (integclks >= min_frclk) {
 762                        intstartclk = 0;
 763                        frameclks = integclks;
 764                } else {
 765                        intstartclk = min_frclk - integclks;
 766                        frameclks = min_frclk;
 767                }
 768                buf[i++] = intstartclk >> 8;
 769                buf[i++] = intstartclk;
 770                buf[i++] = frameclks >> 8;
 771                buf[i++] = frameclks;
 772                buf[i++] = gain;
 773                break;
 774        default:                                /* cmos */
 775/*      case SENSOR_MI0360: */
 776/*      case SENSOR_MT9V111: */
 777                cmd |= 0x0100;
 778                sensor = &sensor_tb[sd->sensor];
 779                buf[i++] = sensor->i2c_addr;    /* i2c_slave_addr */
 780                buf[i++] = 0x08;        /* 2 * ni2c */
 781                buf[i++] = 0x09;        /* reg = shutter width */
 782                buf[i++] = integclks >> 8; /* val H */
 783                buf[i++] = sensor->i2c_dum;
 784                buf[i++] = integclks;   /* val L */
 785                buf[i++] = 0x35;        /* reg = global gain */
 786                buf[i++] = 0x00;        /* val H */
 787                buf[i++] = sensor->i2c_dum;
 788                buf[i++] = 0x80 + gain / 2; /* val L */
 789                buf[i++] = 0x00;
 790                buf[i++] = 0x00;
 791                buf[i++] = 0x00;
 792                buf[i++] = 0x00;
 793                buf[i++] = 0x83;
 794                break;
 795        }
 796        reg_wb(gspca_dev, cmd, 0, buf, i);
 797}
 798
 799/* This function is called at probe time just before sd_init */
 800static int sd_config(struct gspca_dev *gspca_dev,
 801                const struct usb_device_id *id)
 802{
 803        struct sd *sd = (struct sd *) gspca_dev;
 804        struct cam *cam = &gspca_dev->cam;
 805
 806        sd->sensor = id->driver_info >> 8;
 807        sd->type = id->driver_info;
 808
 809        cam->cam_mode = vga_mode;
 810        cam->nmodes = ARRAY_SIZE(vga_mode);
 811
 812        cam->bulk = 1;
 813
 814        return 0;
 815}
 816
 817/* this function is called at probe and resume time */
 818static int sd_init(struct gspca_dev *gspca_dev)
 819{
 820        struct sd *sd = (struct sd *) gspca_dev;
 821
 822        sd->gpio[0] = sd->gpio[1] = 0xff;       /* force gpio rewrite */
 823
 824/*fixme: is this needed for icx098bp and mi0360?
 825        if (sd->sensor != SENSOR_LZ24BP)
 826                reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
 827 */
 828
 829        reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
 830        if (gspca_dev->usb_err < 0)
 831                return gspca_dev->usb_err;
 832
 833/* it returns:
 834 * 03 00 12 93 0b f6 c9 00      live! ultra
 835 * 03 00 07 93 0b f6 ca 00      live! ultra for notebook
 836 * 03 00 12 93 0b fe c8 00      Trust WB-3500T
 837 * 02 00 06 93 0b fe c8 00      Joy-IT 318S
 838 * 03 00 12 93 0b f6 cf 00      icam tracer - sensor icx098bq
 839 * 02 00 12 93 0b fe cf 00      ProQ Motion Webcam
 840 *
 841 * byte
 842 * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
 843 * 1: 00
 844 * 2: 06 / 07 / 12 = mode webcam? firmware??
 845 * 3: 93 chip = 930b (930b or 930c)
 846 * 4: 0b
 847 * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
 848 * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
 849 * 7: 00
 850 */
 851        gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 8, gspca_dev->usb_buf);
 852
 853        bridge_init(sd);
 854
 855        if (sd->sensor == SENSOR_MI0360) {
 856
 857                /* no sensor probe for icam tracer */
 858                if (gspca_dev->usb_buf[5] == 0xf6)      /* if ccd */
 859                        sd->sensor = SENSOR_ICX098BQ;
 860                else
 861                        cmos_probe(gspca_dev);
 862        }
 863        if (gspca_dev->usb_err >= 0) {
 864                gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
 865                          sensor_tb[sd->sensor].name);
 866                global_init(sd, 1);
 867        }
 868        return gspca_dev->usb_err;
 869}
 870
 871/* send the start/stop commands to the webcam */
 872static void send_start(struct gspca_dev *gspca_dev)
 873{
 874        struct sd *sd = (struct sd *) gspca_dev;
 875        const struct cap_s *cap;
 876        int mode;
 877
 878        mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
 879        cap = &capconfig[sd->sensor][mode];
 880        reg_wb(gspca_dev, 0x0900 | SQ930_CTRL_CAP_START,
 881                        0x0a00 | cap->cc_sizeid,
 882                        cap->cc_bytes, 32);
 883}
 884
 885static void send_stop(struct gspca_dev *gspca_dev)
 886{
 887        reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
 888}
 889
 890/* function called at start time before URB creation */
 891static int sd_isoc_init(struct gspca_dev *gspca_dev)
 892{
 893        struct sd *sd = (struct sd *) gspca_dev;
 894
 895        gspca_dev->cam.bulk_nurbs = 1;  /* there must be one URB only */
 896        sd->do_ctrl = 0;
 897        gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
 898                        gspca_dev->pixfmt.height + 8;
 899        return 0;
 900}
 901
 902/* start the capture */
 903static int sd_start(struct gspca_dev *gspca_dev)
 904{
 905        struct sd *sd = (struct sd *) gspca_dev;
 906        int mode;
 907
 908        bridge_init(sd);
 909        global_init(sd, 0);
 910        msleep(100);
 911
 912        switch (sd->sensor) {
 913        case SENSOR_ICX098BQ:
 914                ucbus_write(gspca_dev, icx098bq_start_0,
 915                                ARRAY_SIZE(icx098bq_start_0),
 916                                8);
 917                ucbus_write(gspca_dev, icx098bq_start_1,
 918                                ARRAY_SIZE(icx098bq_start_1),
 919                                5);
 920                ucbus_write(gspca_dev, icx098bq_start_2,
 921                                ARRAY_SIZE(icx098bq_start_2),
 922                                6);
 923                msleep(50);
 924
 925                /* 1st start */
 926                send_start(gspca_dev);
 927                gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
 928                msleep(70);
 929                reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
 930                gpio_set(sd, 0x7f, 0x00ff);
 931
 932                /* 2nd start */
 933                send_start(gspca_dev);
 934                gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
 935                goto out;
 936        case SENSOR_LZ24BP:
 937                ucbus_write(gspca_dev, lz24bp_start_0,
 938                                ARRAY_SIZE(lz24bp_start_0),
 939                                8);
 940                if (sd->type != Creative_live_motion)
 941                        ucbus_write(gspca_dev, lz24bp_start_1_gen,
 942                                        ARRAY_SIZE(lz24bp_start_1_gen),
 943                                        5);
 944                else
 945                        ucbus_write(gspca_dev, lz24bp_start_1_clm,
 946                                        ARRAY_SIZE(lz24bp_start_1_clm),
 947                                        5);
 948                ucbus_write(gspca_dev, lz24bp_start_2,
 949                                ARRAY_SIZE(lz24bp_start_2),
 950                                6);
 951                mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
 952                lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
 953                msleep(10);
 954                break;
 955        case SENSOR_MI0360:
 956                ucbus_write(gspca_dev, mi0360_start_0,
 957                                ARRAY_SIZE(mi0360_start_0),
 958                                8);
 959                i2c_write(sd, mi0360_init_23,
 960                                ARRAY_SIZE(mi0360_init_23));
 961                i2c_write(sd, mi0360_init_24,
 962                                ARRAY_SIZE(mi0360_init_24));
 963                i2c_write(sd, mi0360_init_25,
 964                                ARRAY_SIZE(mi0360_init_25));
 965                ucbus_write(gspca_dev, mi0360_start_1,
 966                                ARRAY_SIZE(mi0360_start_1),
 967                                5);
 968                i2c_write(sd, mi0360_start_2,
 969                                ARRAY_SIZE(mi0360_start_2));
 970                i2c_write(sd, mi0360_start_3,
 971                                ARRAY_SIZE(mi0360_start_3));
 972
 973                /* 1st start */
 974                send_start(gspca_dev);
 975                msleep(60);
 976                send_stop(gspca_dev);
 977
 978                i2c_write(sd,
 979                        mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
 980                break;
 981        default:
 982/*      case SENSOR_MT9V111: */
 983                ucbus_write(gspca_dev, mi0360_start_0,
 984                                ARRAY_SIZE(mi0360_start_0),
 985                                8);
 986                i2c_write(sd, mt9v111_init_0,
 987                                ARRAY_SIZE(mt9v111_init_0));
 988                i2c_write(sd, mt9v111_init_1,
 989                                ARRAY_SIZE(mt9v111_init_1));
 990                i2c_write(sd, mt9v111_init_2,
 991                                ARRAY_SIZE(mt9v111_init_2));
 992                ucbus_write(gspca_dev, mt9v111_start_1,
 993                                ARRAY_SIZE(mt9v111_start_1),
 994                                5);
 995                i2c_write(sd, mt9v111_init_3,
 996                                ARRAY_SIZE(mt9v111_init_3));
 997                i2c_write(sd, mt9v111_init_4,
 998                                ARRAY_SIZE(mt9v111_init_4));
 999                break;
1000        }
1001
1002        send_start(gspca_dev);
1003out:
1004        msleep(1000);
1005
1006        if (sd->sensor == SENSOR_MT9V111)
1007                gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1008
1009        sd->do_ctrl = 1;        /* set the exposure */
1010
1011        return gspca_dev->usb_err;
1012}
1013
1014static void sd_stopN(struct gspca_dev *gspca_dev)
1015{
1016        struct sd *sd = (struct sd *) gspca_dev;
1017
1018        if (sd->sensor == SENSOR_MT9V111)
1019                gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
1020        send_stop(gspca_dev);
1021}
1022
1023/* function called when the application gets a new frame */
1024/* It sets the exposure if required and restart the bulk transfer. */
1025static void sd_dq_callback(struct gspca_dev *gspca_dev)
1026{
1027        struct sd *sd = (struct sd *) gspca_dev;
1028        int ret;
1029
1030        if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1031                return;
1032        sd->do_ctrl = 0;
1033
1034        setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1035                        v4l2_ctrl_g_ctrl(sd->gain));
1036
1037        gspca_dev->cam.bulk_nurbs = 1;
1038        ret = usb_submit_urb(gspca_dev->urb[0], GFP_KERNEL);
1039        if (ret < 0)
1040                pr_err("sd_dq_callback() err %d\n", ret);
1041
1042        /* wait a little time, otherwise the webcam crashes */
1043        msleep(100);
1044}
1045
1046static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1047                        u8 *data,               /* isoc packet */
1048                        int len)                /* iso packet length */
1049{
1050        struct sd *sd = (struct sd *) gspca_dev;
1051
1052        if (sd->do_ctrl)
1053                gspca_dev->cam.bulk_nurbs = 0;
1054        gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
1055        gspca_frame_add(gspca_dev, INTER_PACKET, data, len - 8);
1056        gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1057}
1058
1059static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1060{
1061        struct gspca_dev *gspca_dev =
1062                container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1063        struct sd *sd = (struct sd *) gspca_dev;
1064
1065        gspca_dev->usb_err = 0;
1066
1067        if (!gspca_dev->streaming)
1068                return 0;
1069
1070        switch (ctrl->id) {
1071        case V4L2_CID_EXPOSURE:
1072                setexposure(gspca_dev, ctrl->val, sd->gain->val);
1073                break;
1074        }
1075        return gspca_dev->usb_err;
1076}
1077
1078static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1079        .s_ctrl = sd_s_ctrl,
1080};
1081
1082static int sd_init_controls(struct gspca_dev *gspca_dev)
1083{
1084        struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1085        struct sd *sd = (struct sd *) gspca_dev;
1086
1087        gspca_dev->vdev.ctrl_handler = hdl;
1088        v4l2_ctrl_handler_init(hdl, 2);
1089        sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1090                        V4L2_CID_EXPOSURE, 1, 0xfff, 1, 0x356);
1091        sd->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1092                        V4L2_CID_GAIN, 1, 255, 1, 0x8d);
1093
1094        if (hdl->error) {
1095                pr_err("Could not initialize controls\n");
1096                return hdl->error;
1097        }
1098        v4l2_ctrl_cluster(2, &sd->exposure);
1099        return 0;
1100}
1101
1102/* sub-driver description */
1103static const struct sd_desc sd_desc = {
1104        .name   = MODULE_NAME,
1105        .config = sd_config,
1106        .init   = sd_init,
1107        .init_controls = sd_init_controls,
1108        .isoc_init = sd_isoc_init,
1109        .start  = sd_start,
1110        .stopN  = sd_stopN,
1111        .pkt_scan = sd_pkt_scan,
1112        .dq_callback = sd_dq_callback,
1113};
1114
1115/* Table of supported USB devices */
1116#define ST(sensor, type) \
1117        .driver_info = (SENSOR_ ## sensor << 8) \
1118                        | (type)
1119static const struct usb_device_id device_table[] = {
1120        {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
1121        {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
1122        {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
1123        {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
1124        {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
1125        {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1126        {}
1127};
1128MODULE_DEVICE_TABLE(usb, device_table);
1129
1130
1131/* -- device connect -- */
1132static int sd_probe(struct usb_interface *intf,
1133                const struct usb_device_id *id)
1134{
1135        return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1136                        THIS_MODULE);
1137}
1138
1139static struct usb_driver sd_driver = {
1140        .name       = MODULE_NAME,
1141        .id_table   = device_table,
1142        .probe      = sd_probe,
1143        .disconnect = gspca_disconnect,
1144#ifdef CONFIG_PM
1145        .suspend    = gspca_suspend,
1146        .resume     = gspca_resume,
1147        .reset_resume = gspca_resume,
1148#endif
1149};
1150
1151module_usb_driver(sd_driver);
1152