1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #define MODULE_NAME "sq930x"
16 MODULE_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>");
19 MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
20 MODULE_LICENSE("GPL");
22 /* Structure to hold all of our device specific stuff */
24 struct gspca_dev gspca_dev; /* !! must be the first item */
26 struct { /* exposure/gain control cluster */
27 struct v4l2_ctrl *exposure;
28 struct v4l2_ctrl *gain;
36 #define Creative_live_motion 1
42 SENSOR_MT9V111, /* = MI360SOC */
47 static struct v4l2_pix_format vga_mode[] = {
48 {320, 240, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
50 .sizeimage = 320 * 240,
51 .colorspace = V4L2_COLORSPACE_SRGB,
53 {640, 480, V4L2_PIX_FMT_SRGGB8, V4L2_FIELD_NONE,
55 .sizeimage = 640 * 480,
56 .colorspace = V4L2_COLORSPACE_SRGB,
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
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
77 #define SQ930_GPIO_POWER 0x0200
78 #define SQ930_GPIO_DFL_LED 0x1000
80 struct ucbus_write_cmd {
84 struct i2c_write_cmd {
89 static 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},
117 static const struct ucbus_write_cmd icx098bq_start_1[] = {
118 {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
120 {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
122 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
126 static 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}
137 static 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},
165 static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
166 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
168 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
170 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
174 static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
175 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
177 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
179 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
183 static 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}
194 static const struct ucbus_write_cmd mi0360_start_0[] = {
195 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
196 {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
198 static 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 */
204 static const struct i2c_write_cmd mi0360_init_24[] = {
205 {0x03, 0x01e5}, /* window height */
206 {0x04, 0x0285}, /* window width */
208 static 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 */
215 static const struct ucbus_write_cmd mi0360_start_1[] = {
216 {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
218 {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
220 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
223 static const struct i2c_write_cmd mi0360_start_2[] = {
224 {0x62, 0x041d}, /* reserved - def 0x0418 */
226 static const struct i2c_write_cmd mi0360_start_3[] = {
227 {0x05, 0x007b}, /* horiz blanking */
229 static const struct i2c_write_cmd mi0360_start_4[] = {
230 {0x05, 0x03f5}, /* horiz blanking */
233 static 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 */
239 static const struct i2c_write_cmd mt9v111_init_1[] = {
240 {0x03, 0x01e5}, /* window height */
241 {0x04, 0x0285}, /* window width */
243 static const struct i2c_write_cmd mt9v111_init_2[] = {
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 */
253 static const struct ucbus_write_cmd mt9v111_start_1[] = {
254 {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
256 {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
258 {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
261 static const struct i2c_write_cmd mt9v111_init_3[] = {
264 static const struct i2c_write_cmd mt9v111_init_4[] = {
265 /* {0x05, 0x00ce}, */
266 {0x05, 0x005d}, /* horizontal blanking */
269 static const struct ucbus_write_cmd ov7660_start_0[] = {
270 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
271 {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
274 static const struct ucbus_write_cmd ov9630_start_0[] = {
275 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
276 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
279 /* start parameters indexed by [sensor][mode] */
280 static const struct cap_s {
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} },
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} },
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} },
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} },
340 const struct ucbus_write_cmd *cmd;
343 static const struct sensor_s sensor_tb[] = {
344 [SENSOR_ICX098BQ] = {
348 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
349 SQ930_GPIO_DFL_I2C_SDA,
359 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
360 SQ930_GPIO_DFL_I2C_SDA,
370 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
371 SQ930_GPIO_DFL_I2C_SDA,
381 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
382 SQ930_GPIO_DFL_I2C_SDA,
392 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
393 SQ930_GPIO_DFL_I2C_SDA,
403 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
404 SQ930_GPIO_DFL_I2C_SDA,
412 static void reg_r(struct gspca_dev *gspca_dev,
417 if (gspca_dev->usb_err < 0)
419 ret = usb_control_msg(gspca_dev->dev,
420 usb_rcvctrlpipe(gspca_dev->dev, 0),
422 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
423 value, 0, gspca_dev->usb_buf, len,
426 pr_err("reg_r %04x failed %d\n", value, ret);
427 gspca_dev->usb_err = ret;
431 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
435 if (gspca_dev->usb_err < 0)
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),
441 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
442 value, index, NULL, 0,
446 pr_err("reg_w %04x %04x failed %d\n", value, index, ret);
447 gspca_dev->usb_err = ret;
451 static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
452 const u8 *data, int len)
456 if (gspca_dev->usb_err < 0)
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),
464 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
465 value, index, gspca_dev->usb_buf, len,
469 pr_err("reg_wb %04x %04x failed %d\n", value, index, ret);
470 gspca_dev->usb_err = ret;
474 static void i2c_write(struct sd *sd,
475 const struct i2c_write_cmd *cmd,
478 struct gspca_dev *gspca_dev = &sd->gspca_dev;
479 const struct sensor_s *sensor;
484 if (gspca_dev->usb_err < 0)
487 sensor = &sensor_tb[sd->sensor];
489 val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
490 idx = (cmd->val & 0xff00) | cmd->reg;
492 buf = gspca_dev->usb_buf;
493 *buf++ = sensor->i2c_dum;
496 while (--ncmds > 0) {
499 *buf++ = cmd->val >> 8;
500 *buf++ = sensor->i2c_dum;
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),
509 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
511 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
514 pr_err("i2c_write failed %d\n", ret);
515 gspca_dev->usb_err = ret;
519 static void ucbus_write(struct gspca_dev *gspca_dev,
520 const struct ucbus_write_cmd *cmd,
528 if (gspca_dev->usb_err < 0)
531 if ((batchsize - 1) * 3 > USB_BUF_SZ) {
532 gspca_err(gspca_dev, "Bug: usb_buf overflow\n");
533 gspca_dev->usb_err = -ENOMEM;
543 val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
544 idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
546 buf = gspca_dev->usb_buf;
549 *buf++ = cmd->bw_addr;
550 *buf++ = cmd->bw_addr >> 8;
551 *buf++ = cmd->bw_data;
553 if (buf != gspca_dev->usb_buf)
554 gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x %02x...%02x\n",
556 gspca_dev->usb_buf[0], buf[-1]);
558 gspca_dbg(gspca_dev, D_USBO, "ucbus v: %04x i: %04x\n",
560 ret = usb_control_msg(gspca_dev->dev,
561 usb_sndctrlpipe(gspca_dev->dev, 0),
563 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
565 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
568 pr_err("ucbus_write failed %d\n", ret);
569 gspca_dev->usb_err = ret;
579 static void gpio_set(struct sd *sd, u16 val, u16 mask)
581 struct gspca_dev *gspca_dev = &sd->gspca_dev;
584 sd->gpio[0] &= ~mask;
586 reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
592 sd->gpio[1] &= ~mask;
594 reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
599 static void gpio_init(struct sd *sd,
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);
609 static void bridge_init(struct sd *sd)
611 static const struct ucbus_write_cmd clkfreq_cmd = {
612 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
615 ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
617 gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
620 static void cmos_probe(struct gspca_dev *gspca_dev)
622 struct sd *sd = (struct sd *) gspca_dev;
624 const struct sensor_s *sensor;
625 static const u8 probe_order[] = {
626 /* SENSOR_LZ24BP, (tested as ccd) */
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);
638 reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
640 if (gspca_dev->usb_buf[0] != 0)
643 if (i >= ARRAY_SIZE(probe_order)) {
644 pr_err("Unknown sensor\n");
645 gspca_dev->usb_err = -EINVAL;
648 sd->sensor = probe_order[i];
649 switch (sd->sensor) {
652 pr_err("Sensor %s not yet treated\n",
653 sensor_tb[sd->sensor].name);
654 gspca_dev->usb_err = -EINVAL;
659 static void mt9v111_init(struct gspca_dev *gspca_dev)
662 static const u8 cmd_001b[] = {
663 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
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},
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]));
680 reg_r(gspca_dev, 0x031b, 1);
681 if (gspca_dev->usb_buf[0] == 0
682 || gspca_dev->usb_err != 0)
685 gspca_dbg(gspca_dev, D_PROBE, "mt9v111_init timeout\n");
686 gspca_dev->usb_err = -ETIME;
694 static void global_init(struct sd *sd, int first_time)
696 switch (sd->sensor) {
697 case SENSOR_ICX098BQ:
699 ucbus_write(&sd->gspca_dev,
702 gpio_init(sd, sensor_tb[sd->sensor].gpio);
705 if (sd->type != Creative_live_motion)
706 gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
708 gpio_set(sd, 0, 0x00ff);
711 ucbus_write(&sd->gspca_dev,
714 gpio_init(sd, sensor_tb[sd->sensor].gpio);
718 ucbus_write(&sd->gspca_dev,
720 ARRAY_SIZE(mi0360_start_0),
722 gpio_init(sd, sensor_tb[sd->sensor].gpio);
723 gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
726 /* case SENSOR_MT9V111: */
728 mt9v111_init(&sd->gspca_dev);
730 gpio_init(sd, sensor_tb[sd->sensor].gpio);
735 static void lz24bp_ppl(struct sd *sd, u16 ppl)
737 struct ucbus_write_cmd cmds[2] = {
742 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
745 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 gain)
747 struct sd *sd = (struct sd *) gspca_dev;
748 int i, integclks, intstartclk, frameclks, min_frclk;
749 const struct sensor_s *sensor;
755 cmd = SQ930_CTRL_SET_EXPOSURE;
757 switch (sd->sensor) {
758 case SENSOR_ICX098BQ: /* ccd */
760 min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
761 if (integclks >= min_frclk) {
763 frameclks = integclks;
765 intstartclk = min_frclk - integclks;
766 frameclks = min_frclk;
768 buf[i++] = intstartclk >> 8;
769 buf[i++] = intstartclk;
770 buf[i++] = frameclks >> 8;
771 buf[i++] = frameclks;
775 /* case SENSOR_MI0360: */
776 /* case SENSOR_MT9V111: */
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 */
796 reg_wb(gspca_dev, cmd, 0, buf, i);
799 /* This function is called at probe time just before sd_init */
800 static int sd_config(struct gspca_dev *gspca_dev,
801 const struct usb_device_id *id)
803 struct sd *sd = (struct sd *) gspca_dev;
804 struct cam *cam = &gspca_dev->cam;
806 sd->sensor = id->driver_info >> 8;
807 sd->type = id->driver_info;
809 cam->cam_mode = vga_mode;
810 cam->nmodes = ARRAY_SIZE(vga_mode);
817 /* this function is called at probe and resume time */
818 static int sd_init(struct gspca_dev *gspca_dev)
820 struct sd *sd = (struct sd *) gspca_dev;
822 sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
824 /*fixme: is this needed for icx098bp and mi0360?
825 if (sd->sensor != SENSOR_LZ24BP)
826 reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
829 reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
830 if (gspca_dev->usb_err < 0)
831 return gspca_dev->usb_err;
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
842 * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
844 * 2: 06 / 07 / 12 = mode webcam? firmware??
845 * 3: 93 chip = 930b (930b or 930c)
847 * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
848 * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
851 gspca_dbg(gspca_dev, D_PROBE, "info: %*ph\n", 8, gspca_dev->usb_buf);
855 if (sd->sensor == SENSOR_MI0360) {
857 /* no sensor probe for icam tracer */
858 if (gspca_dev->usb_buf[5] == 0xf6) /* if ccd */
859 sd->sensor = SENSOR_ICX098BQ;
861 cmos_probe(gspca_dev);
863 if (gspca_dev->usb_err >= 0) {
864 gspca_dbg(gspca_dev, D_PROBE, "Sensor %s\n",
865 sensor_tb[sd->sensor].name);
868 return gspca_dev->usb_err;
871 /* send the start/stop commands to the webcam */
872 static void send_start(struct gspca_dev *gspca_dev)
874 struct sd *sd = (struct sd *) gspca_dev;
875 const struct cap_s *cap;
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,
885 static void send_stop(struct gspca_dev *gspca_dev)
887 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
890 /* function called at start time before URB creation */
891 static int sd_isoc_init(struct gspca_dev *gspca_dev)
893 struct sd *sd = (struct sd *) gspca_dev;
895 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
897 gspca_dev->cam.bulk_size = gspca_dev->pixfmt.width *
898 gspca_dev->pixfmt.height + 8;
902 /* start the capture */
903 static int sd_start(struct gspca_dev *gspca_dev)
905 struct sd *sd = (struct sd *) gspca_dev;
912 switch (sd->sensor) {
913 case SENSOR_ICX098BQ:
914 ucbus_write(gspca_dev, icx098bq_start_0,
915 ARRAY_SIZE(icx098bq_start_0),
917 ucbus_write(gspca_dev, icx098bq_start_1,
918 ARRAY_SIZE(icx098bq_start_1),
920 ucbus_write(gspca_dev, icx098bq_start_2,
921 ARRAY_SIZE(icx098bq_start_2),
926 send_start(gspca_dev);
927 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
929 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
930 gpio_set(sd, 0x7f, 0x00ff);
933 send_start(gspca_dev);
934 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
937 ucbus_write(gspca_dev, lz24bp_start_0,
938 ARRAY_SIZE(lz24bp_start_0),
940 if (sd->type != Creative_live_motion)
941 ucbus_write(gspca_dev, lz24bp_start_1_gen,
942 ARRAY_SIZE(lz24bp_start_1_gen),
945 ucbus_write(gspca_dev, lz24bp_start_1_clm,
946 ARRAY_SIZE(lz24bp_start_1_clm),
948 ucbus_write(gspca_dev, lz24bp_start_2,
949 ARRAY_SIZE(lz24bp_start_2),
951 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
952 lz24bp_ppl(sd, mode == 1 ? 0x0564 : 0x0310);
956 ucbus_write(gspca_dev, mi0360_start_0,
957 ARRAY_SIZE(mi0360_start_0),
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),
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));
974 send_start(gspca_dev);
976 send_stop(gspca_dev);
979 mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
982 /* case SENSOR_MT9V111: */
983 ucbus_write(gspca_dev, mi0360_start_0,
984 ARRAY_SIZE(mi0360_start_0),
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),
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));
1002 send_start(gspca_dev);
1006 if (sd->sensor == SENSOR_MT9V111)
1007 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1009 sd->do_ctrl = 1; /* set the exposure */
1011 return gspca_dev->usb_err;
1014 static void sd_stopN(struct gspca_dev *gspca_dev)
1016 struct sd *sd = (struct sd *) gspca_dev;
1018 if (sd->sensor == SENSOR_MT9V111)
1019 gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
1020 send_stop(gspca_dev);
1023 /* function called when the application gets a new frame */
1024 /* It sets the exposure if required and restart the bulk transfer. */
1025 static void sd_dq_callback(struct gspca_dev *gspca_dev)
1027 struct sd *sd = (struct sd *) gspca_dev;
1030 if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1034 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure),
1035 v4l2_ctrl_g_ctrl(sd->gain));
1037 gspca_dev->cam.bulk_nurbs = 1;
1038 ret = usb_submit_urb(gspca_dev->urb[0], GFP_KERNEL);
1040 pr_err("sd_dq_callback() err %d\n", ret);
1042 /* wait a little time, otherwise the webcam crashes */
1046 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1047 u8 *data, /* isoc packet */
1048 int len) /* iso packet length */
1050 struct sd *sd = (struct sd *) gspca_dev;
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);
1059 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
1061 struct gspca_dev *gspca_dev =
1062 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
1063 struct sd *sd = (struct sd *) gspca_dev;
1065 gspca_dev->usb_err = 0;
1067 if (!gspca_dev->streaming)
1071 case V4L2_CID_EXPOSURE:
1072 setexposure(gspca_dev, ctrl->val, sd->gain->val);
1075 return gspca_dev->usb_err;
1078 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
1079 .s_ctrl = sd_s_ctrl,
1082 static int sd_init_controls(struct gspca_dev *gspca_dev)
1084 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1085 struct sd *sd = (struct sd *) gspca_dev;
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);
1095 pr_err("Could not initialize controls\n");
1098 v4l2_ctrl_cluster(2, &sd->exposure);
1102 /* sub-driver description */
1103 static const struct sd_desc sd_desc = {
1104 .name = MODULE_NAME,
1105 .config = sd_config,
1107 .init_controls = sd_init_controls,
1108 .isoc_init = sd_isoc_init,
1111 .pkt_scan = sd_pkt_scan,
1112 .dq_callback = sd_dq_callback,
1115 /* Table of supported USB devices */
1116 #define ST(sensor, type) \
1117 .driver_info = (SENSOR_ ## sensor << 8) \
1119 static 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)},
1128 MODULE_DEVICE_TABLE(usb, device_table);
1131 /* -- device connect -- */
1132 static int sd_probe(struct usb_interface *intf,
1133 const struct usb_device_id *id)
1135 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1139 static struct usb_driver sd_driver = {
1140 .name = MODULE_NAME,
1141 .id_table = device_table,
1143 .disconnect = gspca_disconnect,
1145 .suspend = gspca_suspend,
1146 .resume = gspca_resume,
1147 .reset_resume = gspca_resume,
1151 module_usb_driver(sd_driver);