blob: cec3dafd2f55df4311d825d619c794a38810fa02 [file] [log] [blame]
Jean-François Moine618a8642010-06-05 07:45:04 -03001/*
2 * SQ930x subdriver
3 *
4 * Copyright (C) 2010 Jean-François Moine <http://moinejf.free.fr>
5 * Copyright (C) 2006 -2008 Gerard Klaver <gerard at gkall dot hobby dot nl>
6 * Copyright (C) 2007 Sam Revitch <samr7@cs.washington.edu>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23#define MODULE_NAME "sq930x"
24
25#include "gspca.h"
26#include "jpeg.h"
27
28MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>\n"
29 "Gerard Klaver <gerard at gkall dot hobby dot nl\n"
30 "Sam Revitch <samr7@cs.washington.edu>");
31MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
32MODULE_LICENSE("GPL");
33
34#define BULK_TRANSFER_LEN 5128
35
36/* Structure to hold all of our device specific stuff */
37struct sd {
38 struct gspca_dev gspca_dev; /* !! must be the first item */
39
40 u16 expo;
41 u8 gain;
42
43 u8 quality; /* webcam quality 0..3 */
44#define QUALITY_DEF 1
45
46 u8 gpio[2];
47
48 u8 eof_len;
49 u8 do_ctrl;
50
51 u8 sensor;
Jean-François Moine11ce8842010-07-26 06:39:40 -030052 u8 type;
53#define Generic 0
54#define Creative_live_motion 1
55};
56enum sensors {
Jean-François Moine618a8642010-06-05 07:45:04 -030057 SENSOR_ICX098BQ,
Jean-François Moine618a8642010-06-05 07:45:04 -030058 SENSOR_LZ24BP,
Jean-François Moinea68f7232010-06-24 05:02:57 -030059 SENSOR_MI0360,
60 SENSOR_MT9V111,
61 SENSOR_OV7660,
62 SENSOR_OV9630,
Jean-François Moine618a8642010-06-05 07:45:04 -030063};
64
65static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
66static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val);
67static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
68static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
69
70static const struct ctrl sd_ctrls[] = {
71 {
72 {
73 .id = V4L2_CID_EXPOSURE,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 .name = "Exposure",
76 .minimum = 0x0001,
77 .maximum = 0x0fff,
78 .step = 1,
Jean-François Moined6f5bd62010-07-26 07:09:32 -030079#define EXPO_DEF 0x0356
Jean-François Moine618a8642010-06-05 07:45:04 -030080 .default_value = EXPO_DEF,
81 },
82 .set = sd_setexpo,
83 .get = sd_getexpo,
84 },
85 {
86 {
87 .id = V4L2_CID_GAIN,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Gain",
90 .minimum = 0x01,
91 .maximum = 0xff,
92 .step = 1,
Jean-François Moined6f5bd62010-07-26 07:09:32 -030093#define GAIN_DEF 0x8d
Jean-François Moine618a8642010-06-05 07:45:04 -030094 .default_value = GAIN_DEF,
95 },
96 .set = sd_setgain,
97 .get = sd_getgain,
98 },
99};
100
101static struct v4l2_pix_format vga_mode[] = {
102 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
103 .bytesperline = 160,
104 .sizeimage = 160 * 120 * 5 / 8 + 590,
105 .colorspace = V4L2_COLORSPACE_JPEG,
106 .priv = 0},
107 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
108 .bytesperline = 320,
109 .sizeimage = 320 * 240 * 4 / 8 + 590,
110 .colorspace = V4L2_COLORSPACE_JPEG,
111 .priv = 1},
112 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
113 .bytesperline = 640,
114 .sizeimage = 640 * 480 * 3 / 8 + 590,
115 .colorspace = V4L2_COLORSPACE_JPEG,
116 .priv = 2},
117};
118
119/* JPEG quality indexed by webcam quality */
120#define QUAL_0 90
121#define QUAL_1 85
122#define QUAL_2 75
123#define QUAL_3 70
124static const u8 quality_tb[4] = { QUAL_0, QUAL_1, QUAL_2, QUAL_3 };
125
126/* sq930x registers */
127#define SQ930_CTRL_UCBUS_IO 0x0001
128#define SQ930_CTRL_I2C_IO 0x0002
129#define SQ930_CTRL_GPIO 0x0005
130#define SQ930_CTRL_CAP_START 0x0010
131#define SQ930_CTRL_CAP_STOP 0x0011
132#define SQ930_CTRL_SET_EXPOSURE 0x001d
133#define SQ930_CTRL_RESET 0x001e
134#define SQ930_CTRL_GET_DEV_INFO 0x001f
135
136/* gpio 1 (8..15) */
137#define SQ930_GPIO_DFL_I2C_SDA 0x0001
138#define SQ930_GPIO_DFL_I2C_SCL 0x0002
139#define SQ930_GPIO_RSTBAR 0x0004
140#define SQ930_GPIO_EXTRA1 0x0040
141#define SQ930_GPIO_EXTRA2 0x0080
142/* gpio 3 (24..31) */
143#define SQ930_GPIO_POWER 0x0200
144#define SQ930_GPIO_DFL_LED 0x1000
145
146struct ucbus_write_cmd {
147 u16 bw_addr;
148 u8 bw_data;
149};
150struct i2c_write_cmd {
151 u8 reg;
152 u16 val;
153};
154
155static const struct ucbus_write_cmd icx098bq_start_0[] = {
156 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xce},
157 {0xf802, 0xc1}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x0e},
158 {0xf80a, 0x01}, {0xf80b, 0xee}, {0xf807, 0x60}, {0xf80c, 0x02},
159 {0xf80d, 0xf0}, {0xf80e, 0x03}, {0xf80f, 0x0a}, {0xf81c, 0x02},
160 {0xf81d, 0xf0}, {0xf81e, 0x03}, {0xf81f, 0x0a}, {0xf83a, 0x00},
161 {0xf83b, 0x10}, {0xf83c, 0x00}, {0xf83d, 0x4e}, {0xf810, 0x04},
162 {0xf811, 0x00}, {0xf812, 0x02}, {0xf813, 0x10}, {0xf803, 0x00},
163 {0xf814, 0x01}, {0xf815, 0x18}, {0xf816, 0x00}, {0xf817, 0x48},
164 {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
165 {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
166 {0xf823, 0x07}, {0xf824, 0xff}, {0xf825, 0x03}, {0xf826, 0xff},
167 {0xf827, 0x06}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
168 {0xf82b, 0x0c}, {0xf82c, 0xfd}, {0xf82d, 0x01}, {0xf82e, 0x00},
169 {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
170 {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
171 {0xf854, 0x00}, {0xf855, 0x18}, {0xf856, 0x00}, {0xf857, 0x3c},
172 {0xf858, 0x00}, {0xf859, 0x0c}, {0xf85a, 0x00}, {0xf85b, 0x30},
173 {0xf85c, 0x00}, {0xf85d, 0x0c}, {0xf85e, 0x00}, {0xf85f, 0x30},
174 {0xf860, 0x00}, {0xf861, 0x48}, {0xf862, 0x01}, {0xf863, 0xdc},
175 {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
176 {0xf868, 0xff}, {0xf869, 0x70}, {0xf86c, 0xff}, {0xf86d, 0x00},
177 {0xf86a, 0xff}, {0xf86b, 0x48}, {0xf86e, 0xff}, {0xf86f, 0x00},
178 {0xf870, 0x01}, {0xf871, 0xdb}, {0xf872, 0x01}, {0xf873, 0xfa},
179 {0xf874, 0x01}, {0xf875, 0xdb}, {0xf876, 0x01}, {0xf877, 0xfa},
180 {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
181 {0xf800, 0x03}
182};
183static const struct ucbus_write_cmd icx098bq_start_1[] = {
184 {0xf5f0, 0x00}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
185 {0xf5f4, 0xc0},
186 {0xf5f0, 0x49}, {0xf5f1, 0xcd}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
187 {0xf5f4, 0xc0},
188 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
189 {0xf5f9, 0x00}
190};
191
192static const struct ucbus_write_cmd icx098bq_start_2[] = {
193 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x82}, {0xf806, 0x00},
194 {0xf807, 0x7f}, {0xf800, 0x03},
195 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x40}, {0xf806, 0x00},
196 {0xf807, 0x7f}, {0xf800, 0x03},
197 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xcf}, {0xf806, 0xd0},
198 {0xf807, 0x7f}, {0xf800, 0x03},
199 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
200 {0xf807, 0x7f}, {0xf800, 0x03}
201};
202
203static const struct ucbus_write_cmd lz24bp_start_0[] = {
204 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf800, 0x02}, {0xf801, 0xbe},
205 {0xf802, 0xc6}, {0xf804, 0x00}, {0xf808, 0x00}, {0xf809, 0x06},
206 {0xf80a, 0x01}, {0xf80b, 0xfe}, {0xf807, 0x84}, {0xf80c, 0x02},
207 {0xf80d, 0xf7}, {0xf80e, 0x03}, {0xf80f, 0x0b}, {0xf81c, 0x00},
208 {0xf81d, 0x49}, {0xf81e, 0x03}, {0xf81f, 0x0b}, {0xf83a, 0x00},
209 {0xf83b, 0x01}, {0xf83c, 0x00}, {0xf83d, 0x6b}, {0xf810, 0x03},
210 {0xf811, 0x10}, {0xf812, 0x02}, {0xf813, 0x6f}, {0xf803, 0x00},
211 {0xf814, 0x00}, {0xf815, 0x44}, {0xf816, 0x00}, {0xf817, 0x48},
212 {0xf818, 0x00}, {0xf819, 0x25}, {0xf81a, 0x00}, {0xf81b, 0x3c},
213 {0xf82f, 0x03}, {0xf820, 0xff}, {0xf821, 0x0d}, {0xf822, 0xff},
214 {0xf823, 0x07}, {0xf824, 0xfd}, {0xf825, 0x07}, {0xf826, 0xf0},
215 {0xf827, 0x0c}, {0xf828, 0xff}, {0xf829, 0x03}, {0xf82a, 0xff},
216 {0xf82b, 0x0c}, {0xf82c, 0xfc}, {0xf82d, 0x01}, {0xf82e, 0x00},
217 {0xf830, 0x00}, {0xf831, 0x47}, {0xf832, 0x00}, {0xf833, 0x00},
218 {0xf850, 0x00}, {0xf851, 0x00}, {0xf852, 0x00}, {0xf853, 0x24},
219 {0xf854, 0x00}, {0xf855, 0x0c}, {0xf856, 0x00}, {0xf857, 0x30},
220 {0xf858, 0x00}, {0xf859, 0x18}, {0xf85a, 0x00}, {0xf85b, 0x3c},
221 {0xf85c, 0x00}, {0xf85d, 0x18}, {0xf85e, 0x00}, {0xf85f, 0x3c},
222 {0xf860, 0xff}, {0xf861, 0x37}, {0xf862, 0xff}, {0xf863, 0x1d},
223 {0xf864, 0xff}, {0xf865, 0x98}, {0xf866, 0xff}, {0xf867, 0xc0},
224 {0xf868, 0x00}, {0xf869, 0x37}, {0xf86c, 0x02}, {0xf86d, 0x1d},
225 {0xf86a, 0x00}, {0xf86b, 0x37}, {0xf86e, 0x02}, {0xf86f, 0x1d},
226 {0xf870, 0x01}, {0xf871, 0xc6}, {0xf872, 0x02}, {0xf873, 0x04},
227 {0xf874, 0x01}, {0xf875, 0xc6}, {0xf876, 0x02}, {0xf877, 0x04},
228 {0xf878, 0x0f}, {0xf879, 0x0f}, {0xf87a, 0xff}, {0xf87b, 0xff},
229 {0xf800, 0x03}
230};
231static const struct ucbus_write_cmd lz24bp_start_1_gen[] = {
232 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
233 {0xf5f4, 0xb3},
234 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
235 {0xf5f4, 0xb3},
236 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
237 {0xf5f9, 0x00}
238};
239
240static const struct ucbus_write_cmd lz24bp_start_1_clm[] = {
241 {0xf5f0, 0x00}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
242 {0xf5f4, 0xc0},
243 {0xf5f0, 0x40}, {0xf5f1, 0xff}, {0xf5f2, 0x88}, {0xf5f3, 0x88},
244 {0xf5f4, 0xc0},
245 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
246 {0xf5f9, 0x00}
247};
248
249static const struct ucbus_write_cmd lz24bp_start_2[] = {
250 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x80}, {0xf806, 0x00},
251 {0xf807, 0x7f}, {0xf800, 0x03},
252 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x4e}, {0xf806, 0x00},
253 {0xf807, 0x7f}, {0xf800, 0x03},
254 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0xc0}, {0xf806, 0x48},
255 {0xf807, 0x7f}, {0xf800, 0x03},
256 {0xf800, 0x02}, {0xf807, 0xff}, {0xf805, 0x00}, {0xf806, 0x00},
257 {0xf807, 0x7f}, {0xf800, 0x03}
258};
259
260static const struct ucbus_write_cmd mi0360_start_0[] = {
261 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
262 {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
263};
264static const struct i2c_write_cmd mi0360_init_23[] = {
265 {0x30, 0x0040}, /* reserved - def 0x0005 */
266 {0x31, 0x0000}, /* reserved - def 0x002a */
267 {0x34, 0x0100}, /* reserved - def 0x0100 */
268 {0x3d, 0x068f}, /* reserved - def 0x068f */
269};
270static const struct i2c_write_cmd mi0360_init_24[] = {
271 {0x03, 0x01e5}, /* window height */
272 {0x04, 0x0285}, /* window width */
273};
274static const struct i2c_write_cmd mi0360_init_25[] = {
275 {0x35, 0x0020}, /* global gain */
276 {0x2b, 0x0020}, /* green1 gain */
277 {0x2c, 0x002a}, /* blue gain */
278 {0x2d, 0x0028}, /* red gain */
279 {0x2e, 0x0020}, /* green2 gain */
280};
281static const struct ucbus_write_cmd mi0360_start_1[] = {
282 {0xf5f0, 0x11}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
283 {0xf5f4, 0xa6},
284 {0xf5f0, 0x51}, {0xf5f1, 0x99}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
285 {0xf5f4, 0xa6},
286 {0xf5fa, 0x00}, {0xf5f6, 0x00}, {0xf5f7, 0x00}, {0xf5f8, 0x00},
287 {0xf5f9, 0x00}
288};
289static const struct i2c_write_cmd mi0360_start_2[] = {
290 {0x62, 0x041d}, /* reserved - def 0x0418 */
291};
292static const struct i2c_write_cmd mi0360_start_3[] = {
293 {0x05, 0x007b}, /* horiz blanking */
294};
295static const struct i2c_write_cmd mi0360_start_4[] = {
296 {0x05, 0x03f5}, /* horiz blanking */
297};
298
Jean-François Moinea68f7232010-06-24 05:02:57 -0300299static const struct i2c_write_cmd mt9v111_init_0[] = {
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300300 {0x01, 0x0001}, /* select IFP/SOC registers */
301 {0x06, 0x300c}, /* operating mode control */
302 {0x08, 0xcc00}, /* output format control (RGB) */
303 {0x01, 0x0004}, /* select core registers */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300304};
305static const struct i2c_write_cmd mt9v111_init_1[] = {
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300306 {0x03, 0x01e5}, /* window height */
307 {0x04, 0x0285}, /* window width */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300308};
309static const struct i2c_write_cmd mt9v111_init_2[] = {
310 {0x30, 0x7800},
311 {0x31, 0x0000},
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300312 {0x07, 0x3002}, /* output control */
313 {0x35, 0x0020}, /* global gain */
314 {0x2b, 0x0020}, /* green1 gain */
315 {0x2c, 0x0020}, /* blue gain */
316 {0x2d, 0x0020}, /* red gain */
317 {0x2e, 0x0020}, /* green2 gain */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300318};
319static const struct ucbus_write_cmd mt9v111_start_1[] = {
320 {0xf5f0, 0x11}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
321 {0xf5f4, 0xaa},
322 {0xf5f0, 0x51}, {0xf5f1, 0x96}, {0xf5f2, 0x80}, {0xf5f3, 0x80},
323 {0xf5f4, 0xaa},
324 {0xf5fa, 0x00}, {0xf5f6, 0x0a}, {0xf5f7, 0x0a}, {0xf5f8, 0x0a},
325 {0xf5f9, 0x0a}
326};
327static const struct i2c_write_cmd mt9v111_init_3[] = {
328 {0x62, 0x0405},
329};
330static const struct i2c_write_cmd mt9v111_init_4[] = {
Jean-François Moine1676e4a2010-07-06 05:14:57 -0300331 {0x05, 0x00ce}, /* horizontal blanking */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300332};
333
334static const struct ucbus_write_cmd ov7660_start_0[] = {
335 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
336 {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
337};
338
339static const struct ucbus_write_cmd ov9630_start_0[] = {
340 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
341 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
342};
343
Jean-François Moine618a8642010-06-05 07:45:04 -0300344static const struct cap_s {
345 u8 cc_sizeid;
346 u8 cc_bytes[32];
Jean-François Moinea68f7232010-06-24 05:02:57 -0300347} capconfig[4][3] = {
Jean-François Moine618a8642010-06-05 07:45:04 -0300348 [SENSOR_ICX098BQ] = {
349 {0, /* JPEG, 160x120 */
350 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
351 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
352 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
353 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
354 {2, /* JPEG, 320x240 */
355 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
356 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
357 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
358 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
359 {4, /* JPEG, 640x480 */
360 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
361 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
362 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
363 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
364 },
365 [SENSOR_LZ24BP] = {
366 {0, /* JPEG, 160x120 */
367 {0x01, 0x1f, 0x20, 0x0e, 0x00, 0x9f, 0x02, 0xee,
368 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
369 0x02, 0x8b, 0x00, 0x8b, 0x00, 0x41, 0x01, 0x41,
370 0x01, 0x41, 0x01, 0x05, 0x40, 0x01, 0xf0, 0x00} },
371 {2, /* JPEG, 320x240 */
372 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xee,
373 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
374 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
375 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
376 {4, /* JPEG, 640x480 */
377 {0x01, 0x22, 0x20, 0x0e, 0x00, 0xa2, 0x02, 0xf0,
378 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
379 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
380 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
381 },
382 [SENSOR_MI0360] = {
383 {0, /* JPEG, 160x120 */
384 {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b,
385 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
386 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f,
387 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} },
388 {2, /* JPEG, 320x240 */
389 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe1,
390/*fixme 03 e3 */
391 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
392 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
393 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
394 {4, /* JPEG, 640x480 */
395 {0x01, 0x02, 0x20, 0x01, 0x20, 0x82, 0x02, 0xe3,
396 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
397 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
398 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
399 },
Jean-François Moinea68f7232010-06-24 05:02:57 -0300400 [SENSOR_MT9V111] = {
401 {0, /* JPEG, 160x120 */
402 {0x05, 0x3d, 0x20, 0x0b, 0x00, 0xbd, 0x02, 0x0b,
403 0x02, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
404 0x02, 0x01, 0x01, 0x01, 0x01, 0x9f, 0x00, 0x9f,
405 0x00, 0x9f, 0x01, 0x05, 0xa0, 0x00, 0x80, 0x00} },
406 {2, /* JPEG, 320x240 */
407 {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3,
408 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
409 0x02, 0xdf, 0x01, 0x00, 0x00, 0x3f, 0x01, 0x3f,
410 0x01, 0x00, 0x00, 0x05, 0x40, 0x01, 0xf0, 0x00} },
411 {4, /* JPEG, 640x480 */
412 {0x01, 0x02, 0x20, 0x03, 0x20, 0x82, 0x02, 0xe3,
413 0x01, 0x02, 0x00, 0x08, 0x18, 0x12, 0x78, 0xc8,
414 0x07, 0xe1, 0x01, 0xe1, 0x01, 0x3f, 0x01, 0x3f,
415 0x01, 0x3f, 0x01, 0x05, 0x80, 0x02, 0xe0, 0x01} },
416 },
417};
418
419struct sensor_s {
420 const char *name;
421 u8 i2c_addr;
422 u8 i2c_dum;
423 u8 gpio[5];
424 u8 cmd_len;
425 const struct ucbus_write_cmd *cmd;
426};
427
428static const struct sensor_s sensor_tb[] = {
429 [SENSOR_ICX098BQ] = {
430 "icx098bp",
431 0x00, 0x00,
432 {0,
433 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
434 SQ930_GPIO_DFL_I2C_SDA,
435 0,
436 SQ930_GPIO_RSTBAR
437 },
438 8, icx098bq_start_0
439 },
440 [SENSOR_LZ24BP] = {
441 "lz24bp",
442 0x00, 0x00,
443 {0,
444 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
445 SQ930_GPIO_DFL_I2C_SDA,
446 0,
447 SQ930_GPIO_RSTBAR
448 },
449 8, lz24bp_start_0
450 },
451 [SENSOR_MI0360] = {
452 "mi0360",
453 0x5d, 0x80,
454 {SQ930_GPIO_RSTBAR,
455 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
456 SQ930_GPIO_DFL_I2C_SDA,
457 0,
458 0
459 },
460 7, mi0360_start_0
461 },
462 [SENSOR_MT9V111] = {
463 "mt9v111",
464 0x5c, 0x7f,
465 {SQ930_GPIO_RSTBAR,
466 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
467 SQ930_GPIO_DFL_I2C_SDA,
468 0,
469 0
470 },
471 7, mi0360_start_0
472 },
473 [SENSOR_OV7660] = {
474 "ov7660",
475 0x21, 0x00,
476 {0,
477 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
478 SQ930_GPIO_DFL_I2C_SDA,
479 0,
480 SQ930_GPIO_RSTBAR
481 },
482 7, ov7660_start_0
483 },
484 [SENSOR_OV9630] = {
485 "ov9630",
486 0x30, 0x00,
487 {0,
488 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
489 SQ930_GPIO_DFL_I2C_SDA,
490 0,
491 SQ930_GPIO_RSTBAR
492 },
493 7, ov9630_start_0
494 },
Jean-François Moine618a8642010-06-05 07:45:04 -0300495};
496
497static void reg_r(struct gspca_dev *gspca_dev,
498 u16 value, int len)
499{
Jean-François Moine7d716a32010-06-24 04:57:12 -0300500 int ret;
501
502 if (gspca_dev->usb_err < 0)
503 return;
504 ret = usb_control_msg(gspca_dev->dev,
Jean-François Moine618a8642010-06-05 07:45:04 -0300505 usb_rcvctrlpipe(gspca_dev->dev, 0),
506 0x0c,
507 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
508 value, 0, gspca_dev->usb_buf, len,
509 500);
Jean-François Moine7d716a32010-06-24 04:57:12 -0300510 if (ret < 0) {
511 PDEBUG(D_ERR, "reg_r %04x failed %d", value, ret);
512 gspca_dev->usb_err = ret;
513 }
Jean-François Moine618a8642010-06-05 07:45:04 -0300514}
515
516static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
517{
518 int ret;
519
520 if (gspca_dev->usb_err < 0)
521 return;
522 PDEBUG(D_USBO, "reg_w v: %04x i: %04x", value, index);
523 ret = usb_control_msg(gspca_dev->dev,
524 usb_sndctrlpipe(gspca_dev->dev, 0),
525 0x0c, /* request */
526 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
527 value, index, NULL, 0,
528 500);
529 msleep(30);
530 if (ret < 0) {
531 PDEBUG(D_ERR, "reg_w %04x %04x failed %d", value, index, ret);
532 gspca_dev->usb_err = ret;
533 }
534}
535
536static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
537 const u8 *data, int len)
538{
539 int ret;
540
541 if (gspca_dev->usb_err < 0)
542 return;
543 PDEBUG(D_USBO, "reg_wb v: %04x i: %04x %02x...%02x",
544 value, index, *data, data[len - 1]);
545 memcpy(gspca_dev->usb_buf, data, len);
546 ret = usb_control_msg(gspca_dev->dev,
547 usb_sndctrlpipe(gspca_dev->dev, 0),
548 0x0c, /* request */
549 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
550 value, index, gspca_dev->usb_buf, len,
551 1000);
552 msleep(30);
553 if (ret < 0) {
554 PDEBUG(D_ERR, "reg_wb %04x %04x failed %d", value, index, ret);
555 gspca_dev->usb_err = ret;
556 }
557}
558
Jean-François Moinea68f7232010-06-24 05:02:57 -0300559static void i2c_write(struct sd *sd,
Jean-François Moine618a8642010-06-05 07:45:04 -0300560 const struct i2c_write_cmd *cmd,
561 int ncmds)
562{
Jean-François Moinea68f7232010-06-24 05:02:57 -0300563 struct gspca_dev *gspca_dev = &sd->gspca_dev;
564 const struct sensor_s *sensor;
Jean-François Moine618a8642010-06-05 07:45:04 -0300565 u16 val, idx;
566 u8 *buf;
567 int ret;
568
569 if (gspca_dev->usb_err < 0)
570 return;
571
Jean-François Moinea68f7232010-06-24 05:02:57 -0300572 sensor = &sensor_tb[sd->sensor];
573
574 val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
Jean-François Moine618a8642010-06-05 07:45:04 -0300575 idx = (cmd->val & 0xff00) | cmd->reg;
576
577 buf = gspca_dev->usb_buf;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300578 *buf++ = sensor->i2c_dum;
Jean-François Moine618a8642010-06-05 07:45:04 -0300579 *buf++ = cmd->val;
580
581 while (--ncmds > 0) {
582 cmd++;
583 *buf++ = cmd->reg;
584 *buf++ = cmd->val >> 8;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300585 *buf++ = sensor->i2c_dum;
Jean-François Moine618a8642010-06-05 07:45:04 -0300586 *buf++ = cmd->val;
587 }
588
589 PDEBUG(D_USBO, "i2c_w v: %04x i: %04x %02x...%02x",
590 val, idx, gspca_dev->usb_buf[0], buf[-1]);
591 ret = usb_control_msg(gspca_dev->dev,
592 usb_sndctrlpipe(gspca_dev->dev, 0),
593 0x0c, /* request */
594 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
595 val, idx,
596 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
597 500);
598 if (ret < 0) {
599 PDEBUG(D_ERR, "i2c_write failed %d", ret);
600 gspca_dev->usb_err = ret;
601 }
602}
603
604static void ucbus_write(struct gspca_dev *gspca_dev,
605 const struct ucbus_write_cmd *cmd,
606 int ncmds,
607 int batchsize)
608{
609 u8 *buf;
610 u16 val, idx;
611 int len, ret;
612
613 if (gspca_dev->usb_err < 0)
614 return;
615
616#ifdef GSPCA_DEBUG
617 if ((batchsize - 1) * 3 > USB_BUF_SZ) {
618 err("Bug: usb_buf overflow");
619 gspca_dev->usb_err = -ENOMEM;
620 return;
621 }
622#endif
623
624 for (;;) {
625 len = ncmds;
626 if (len > batchsize)
627 len = batchsize;
628 ncmds -= len;
629
630 val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
631 idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
632
633 buf = gspca_dev->usb_buf;
634 while (--len > 0) {
635 cmd++;
636 *buf++ = cmd->bw_addr;
637 *buf++ = cmd->bw_addr >> 8;
638 *buf++ = cmd->bw_data;
639 }
640 if (buf != gspca_dev->usb_buf)
641 PDEBUG(D_USBO, "ucbus v: %04x i: %04x %02x...%02x",
642 val, idx,
643 gspca_dev->usb_buf[0], buf[-1]);
644 else
645 PDEBUG(D_USBO, "ucbus v: %04x i: %04x",
646 val, idx);
647 ret = usb_control_msg(gspca_dev->dev,
648 usb_sndctrlpipe(gspca_dev->dev, 0),
649 0x0c, /* request */
650 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
651 val, idx,
652 gspca_dev->usb_buf, buf - gspca_dev->usb_buf,
653 500);
654 if (ret < 0) {
655 PDEBUG(D_ERR, "ucbus_write failed %d", ret);
656 gspca_dev->usb_err = ret;
657 return;
658 }
659 msleep(30);
660 if (ncmds <= 0)
661 break;
662 cmd++;
663 }
664}
665
666static void gpio_set(struct sd *sd, u16 val, u16 mask)
667{
668 struct gspca_dev *gspca_dev = &sd->gspca_dev;
669
670 if (mask & 0x00ff) {
671 sd->gpio[0] &= ~mask;
672 sd->gpio[0] |= val;
673 reg_w(gspca_dev, 0x0100 | SQ930_CTRL_GPIO,
674 ~sd->gpio[0] << 8);
675 }
676 mask >>= 8;
677 val >>= 8;
678 if (mask) {
679 sd->gpio[1] &= ~mask;
680 sd->gpio[1] |= val;
681 reg_w(gspca_dev, 0x0300 | SQ930_CTRL_GPIO,
682 ~sd->gpio[1] << 8);
683 }
684}
685
Jean-François Moinea68f7232010-06-24 05:02:57 -0300686static void gpio_init(struct sd *sd,
687 const u8 *gpio)
688{
689 gpio_set(sd, *gpio++, 0x000f);
690 gpio_set(sd, *gpio++, 0x000f);
691 gpio_set(sd, *gpio++, 0x000f);
692 gpio_set(sd, *gpio++, 0x000f);
693 gpio_set(sd, *gpio, 0x000f);
694}
695
696static void bridge_init(struct sd *sd)
Jean-François Moine618a8642010-06-05 07:45:04 -0300697{
698 static const struct ucbus_write_cmd clkfreq_cmd = {
699 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
700 };
701
702 ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
703
704 gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300705}
706
707static void cmos_probe(struct gspca_dev *gspca_dev)
708{
709 struct sd *sd = (struct sd *) gspca_dev;
710 int i;
711 const struct sensor_s *sensor;
712 static const u8 probe_order[] = {
713/* SENSOR_LZ24BP, (tested as ccd) */
714 SENSOR_OV9630,
715 SENSOR_MI0360,
716 SENSOR_OV7660,
717 SENSOR_MT9V111,
718 };
719
720 for (i = 0; i < ARRAY_SIZE(probe_order); i++) {
721 sensor = &sensor_tb[probe_order[i]];
722 ucbus_write(&sd->gspca_dev, sensor->cmd, sensor->cmd_len, 8);
723 gpio_init(sd, sensor->gpio);
724 msleep(100);
725 reg_r(gspca_dev, (sensor->i2c_addr << 8) | 0x001c, 1);
726 msleep(100);
727 if (gspca_dev->usb_buf[0] != 0)
728 break;
729 }
730 if (i >= ARRAY_SIZE(probe_order))
731 PDEBUG(D_PROBE, "Unknown sensor");
732 else
733 sd->sensor = probe_order[i];
734}
735
736static void mt9v111_init(struct gspca_dev *gspca_dev)
737{
738 int i, nwait;
739 static const u8 cmd_001b[] = {
740 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
741 0x00, 0x00, 0x00
742 };
743 static const u8 cmd_011b[][7] = {
744 {0x10, 0x01, 0x66, 0x08, 0x00, 0x00, 0x00},
745 {0x01, 0x00, 0x1a, 0x04, 0x00, 0x00, 0x00},
746 {0x20, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00},
747 {0x02, 0x01, 0xae, 0x01, 0x00, 0x00, 0x00},
748 };
749
750 reg_wb(gspca_dev, 0x001b, 0x0000, cmd_001b, sizeof cmd_001b);
751 for (i = 0; i < ARRAY_SIZE(cmd_011b); i++) {
752 reg_wb(gspca_dev, 0x001b, 0x0000, cmd_011b[i],
753 ARRAY_SIZE(cmd_011b[0]));
754 msleep(400);
755 nwait = 20;
756 for (;;) {
757 reg_r(gspca_dev, 0x031b, 1);
758 if (gspca_dev->usb_buf[0] == 0
759 || gspca_dev->usb_err != 0)
760 break;
761 if (--nwait < 0) {
762 PDEBUG(D_PROBE, "mt9v111_init timeout");
763 gspca_dev->usb_err = -ETIME;
764 return;
765 }
766 msleep(50);
767 }
768 }
769}
770
771static void global_init(struct sd *sd, int first_time)
772{
Jean-François Moine618a8642010-06-05 07:45:04 -0300773 switch (sd->sensor) {
774 case SENSOR_ICX098BQ:
775 if (first_time)
776 ucbus_write(&sd->gspca_dev,
777 icx098bq_start_0,
778 8, 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300779 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300780 break;
781 case SENSOR_LZ24BP:
782 if (sd->type != Creative_live_motion)
783 gpio_set(sd, SQ930_GPIO_EXTRA1, 0x00ff);
784 else
785 gpio_set(sd, 0, 0x00ff);
786 msleep(50);
787 if (first_time)
788 ucbus_write(&sd->gspca_dev,
789 lz24bp_start_0,
790 8, 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300791 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300792 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300793 case SENSOR_MI0360:
794 if (first_time)
Jean-François Moine618a8642010-06-05 07:45:04 -0300795 ucbus_write(&sd->gspca_dev,
796 mi0360_start_0,
797 ARRAY_SIZE(mi0360_start_0),
798 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300799 gpio_init(sd, sensor_tb[sd->sensor].gpio);
Jean-François Moine618a8642010-06-05 07:45:04 -0300800 gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
801 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300802 default:
803/* case SENSOR_MT9V111: */
804 if (first_time)
805 mt9v111_init(&sd->gspca_dev);
806 else
807 gpio_init(sd, sensor_tb[sd->sensor].gpio);
808 break;
Jean-François Moine618a8642010-06-05 07:45:04 -0300809 }
810}
811
812static void lz24bp_ppl(struct sd *sd, u16 ppl)
813{
814 struct ucbus_write_cmd cmds[2] = {
815 {0xf810, ppl >> 8},
816 {0xf811, ppl}
817 };
818
819 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
820}
821
822static void setexposure(struct gspca_dev *gspca_dev)
823{
824 struct sd *sd = (struct sd *) gspca_dev;
825 int i, integclks, intstartclk, frameclks, min_frclk;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300826 const struct sensor_s *sensor;
Jean-François Moine618a8642010-06-05 07:45:04 -0300827 u16 cmd;
828 u8 buf[15];
829
830 integclks = sd->expo;
831 i = 0;
832 cmd = SQ930_CTRL_SET_EXPOSURE;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300833
834 switch (sd->sensor) {
835 case SENSOR_ICX098BQ: /* ccd */
836 case SENSOR_LZ24BP:
Jean-François Moine618a8642010-06-05 07:45:04 -0300837 min_frclk = sd->sensor == SENSOR_ICX098BQ ? 0x210 : 0x26f;
838 if (integclks >= min_frclk) {
839 intstartclk = 0;
840 frameclks = integclks;
841 } else {
842 intstartclk = min_frclk - integclks;
843 frameclks = min_frclk;
844 }
845 buf[i++] = intstartclk >> 8;
846 buf[i++] = intstartclk;
847 buf[i++] = frameclks >> 8;
848 buf[i++] = frameclks;
849 buf[i++] = sd->gain;
Jean-François Moinea68f7232010-06-24 05:02:57 -0300850 break;
851 default: /* cmos */
852/* case SENSOR_MI0360: */
853/* case SENSOR_MT9V111: */
854 cmd |= 0x0100;
855 sensor = &sensor_tb[sd->sensor];
856 buf[i++] = sensor->i2c_addr; /* i2c_slave_addr */
857 buf[i++] = 0x08; /* 2 * ni2c */
858 buf[i++] = 0x09; /* reg = shutter width */
859 buf[i++] = integclks >> 8; /* val H */
860 buf[i++] = sensor->i2c_dum;
861 buf[i++] = integclks; /* val L */
862 buf[i++] = 0x35; /* reg = global gain */
863 buf[i++] = 0x00; /* val H */
864 buf[i++] = sensor->i2c_dum;
Jean-François Moineb3c0af62010-07-26 06:58:18 -0300865 buf[i++] = 0x80 + sd->gain / 2; /* val L */
Jean-François Moinea68f7232010-06-24 05:02:57 -0300866 buf[i++] = 0x00;
867 buf[i++] = 0x00;
868 buf[i++] = 0x00;
869 buf[i++] = 0x00;
870 buf[i++] = 0x83;
871 break;
Jean-François Moine618a8642010-06-05 07:45:04 -0300872 }
873 reg_wb(gspca_dev, cmd, 0, buf, i);
874}
875
876/* This function is called at probe time just before sd_init */
877static int sd_config(struct gspca_dev *gspca_dev,
878 const struct usb_device_id *id)
879{
880 struct sd *sd = (struct sd *) gspca_dev;
881 struct cam *cam = &gspca_dev->cam;
882
883 sd->sensor = id->driver_info >> 8;
884 sd->type = id->driver_info;
885
886 cam->cam_mode = vga_mode;
887 cam->nmodes = ARRAY_SIZE(vga_mode);
888
889 cam->bulk = 1;
890 cam->bulk_size = BULK_TRANSFER_LEN;
891/* cam->bulk_nurbs = 2; fixme: if no setexpo sync */
892
893 sd->quality = QUALITY_DEF;
894 sd->gain = GAIN_DEF;
895 sd->expo = EXPO_DEF;
896
897 return 0;
898}
899
900/* this function is called at probe and resume time */
901static int sd_init(struct gspca_dev *gspca_dev)
902{
903 struct sd *sd = (struct sd *) gspca_dev;
904
905 sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
906
Jean-François Moinea68f7232010-06-24 05:02:57 -0300907/*fixme: is this needed for icx098bp and mi0360?
Jean-François Moine618a8642010-06-05 07:45:04 -0300908 if (sd->sensor != SENSOR_LZ24BP)
909 reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
Jean-François Moinea68f7232010-06-24 05:02:57 -0300910 */
Jean-François Moine618a8642010-06-05 07:45:04 -0300911
912 reg_r(gspca_dev, SQ930_CTRL_GET_DEV_INFO, 8);
913/* it returns:
914 * 03 00 12 93 0b f6 c9 00 live! ultra
915 * 03 00 07 93 0b f6 ca 00 live! ultra for notebook
916 * 03 00 12 93 0b fe c8 00 Trust WB-3500T
917 * 02 00 06 93 0b fe c8 00 Joy-IT 318S
918 * 03 00 12 93 0b f6 cf 00 icam tracer - sensor icx098bq
919 * 02 00 12 93 0b fe cf 00 ProQ Motion Webcam
920 *
921 * byte
922 * 0: 02 = usb 1.0 (12Mbit) / 03 = usb2.0 (480Mbit)
923 * 1: 00
924 * 2: 06 / 07 / 12 = mode webcam? firmware??
925 * 3: 93 chip = 930b (930b or 930c)
926 * 4: 0b
Jean-François Moinea68f7232010-06-24 05:02:57 -0300927 * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
Jean-François Moine618a8642010-06-05 07:45:04 -0300928 * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
929 * 7: 00
930 */
931 PDEBUG(D_PROBE, "info: %02x %02x %02x %02x %02x %02x %02x %02x",
932 gspca_dev->usb_buf[0],
933 gspca_dev->usb_buf[1],
934 gspca_dev->usb_buf[2],
935 gspca_dev->usb_buf[3],
936 gspca_dev->usb_buf[4],
937 gspca_dev->usb_buf[5],
938 gspca_dev->usb_buf[6],
939 gspca_dev->usb_buf[7]);
940
Jean-François Moinea68f7232010-06-24 05:02:57 -0300941 bridge_init(sd);
942
943 if (sd->sensor == SENSOR_MI0360) {
944
945 /* no sensor probe for icam tracer */
946 if (gspca_dev->usb_buf[5] == 0xf6) { /* if CMOS */
947 sd->sensor = SENSOR_ICX098BQ;
948 gspca_dev->cam.cam_mode = &vga_mode[1];
949 gspca_dev->cam.nmodes = 1; /* only 320x240 */
950 } else {
951 cmos_probe(gspca_dev);
952 }
Jean-François Moine618a8642010-06-05 07:45:04 -0300953 }
954
Jean-François Moinea68f7232010-06-24 05:02:57 -0300955 PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
956
Jean-François Moine618a8642010-06-05 07:45:04 -0300957 global_init(sd, 1);
958 return gspca_dev->usb_err;
959}
960
961/* special function to create the quantization tables of the JPEG header */
962static void sd_jpeg_set_qual(u8 *jpeg_hdr,
963 int quality)
964{
965 int i, sc1, sc2;
966
967 quality = quality_tb[quality]; /* convert to JPEG quality */
968/*
969 * approximative qualities for Y and U/V:
970 * quant = 0:94%/91% 1:91%/87% 2:82%/73% 3:69%/56%
971 * should have:
972 * quant = 0:94%/91% 1:91%/87.5% 2:81.5%/72% 3:69%/54.5%
973 */
974 sc1 = 200 - quality * 2;
975 quality = quality * 7 / 5 - 40; /* UV quality */
976 sc2 = 200 - quality * 2;
977 for (i = 0; i < 64; i++) {
978 jpeg_hdr[JPEG_QT0_OFFSET + i] =
979 (jpeg_head[JPEG_QT0_OFFSET + i] * sc1 + 50) / 100;
980 jpeg_hdr[JPEG_QT1_OFFSET + i] =
981 (jpeg_head[JPEG_QT1_OFFSET + i] * sc2 + 50) / 100;
982 }
983}
984
985/* send the start/stop commands to the webcam */
986static void send_start(struct gspca_dev *gspca_dev)
987{
988 struct sd *sd = (struct sd *) gspca_dev;
989 const struct cap_s *cap;
990 int mode, quality;
991
992 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
993 cap = &capconfig[sd->sensor][mode];
994 quality = sd->quality;
995 reg_wb(gspca_dev, (quality << 12)
996 | 0x0a00 /* 900 for Bayer */
997 | SQ930_CTRL_CAP_START,
998 0x0500 /* a00 for Bayer */
999 | cap->cc_sizeid,
1000 cap->cc_bytes, 32);
1001};
1002static void send_stop(struct gspca_dev *gspca_dev)
1003{
1004 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
1005};
1006
1007/* function called at start time before URB creation */
1008static int sd_isoc_init(struct gspca_dev *gspca_dev)
1009{
1010 struct sd *sd = (struct sd *) gspca_dev;
1011
1012 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
1013 sd->do_ctrl = 0;
1014 return 0;
1015}
1016
1017/* start the capture */
1018static int sd_start(struct gspca_dev *gspca_dev)
1019{
1020 struct sd *sd = (struct sd *) gspca_dev;
1021 int mode;
1022
1023 /* initialize the JPEG header */
1024 jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
1025 0x21); /* JPEG 422 */
1026 sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1027
Jean-François Moinea68f7232010-06-24 05:02:57 -03001028 bridge_init(sd);
Jean-François Moine618a8642010-06-05 07:45:04 -03001029 global_init(sd, 0);
1030 msleep(100);
1031
1032 switch (sd->sensor) {
1033 case SENSOR_ICX098BQ:
1034 ucbus_write(gspca_dev, icx098bq_start_0,
1035 ARRAY_SIZE(icx098bq_start_0),
1036 8);
1037 ucbus_write(gspca_dev, icx098bq_start_1,
1038 ARRAY_SIZE(icx098bq_start_1),
1039 5);
1040 ucbus_write(gspca_dev, icx098bq_start_2,
1041 ARRAY_SIZE(icx098bq_start_2),
1042 6);
1043 msleep(50);
1044
1045 /* 1st start */
1046 send_start(gspca_dev);
1047 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
1048 msleep(70);
1049 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
1050 gpio_set(sd, 0x7f, 0x00ff);
1051
1052 /* 2nd start */
1053 send_start(gspca_dev);
1054 gpio_set(sd, SQ930_GPIO_EXTRA2 | SQ930_GPIO_RSTBAR, 0x00ff);
1055 goto out;
1056 case SENSOR_LZ24BP:
1057 ucbus_write(gspca_dev, lz24bp_start_0,
1058 ARRAY_SIZE(lz24bp_start_0),
1059 8);
1060 if (sd->type != Creative_live_motion)
1061 ucbus_write(gspca_dev, lz24bp_start_1_gen,
1062 ARRAY_SIZE(lz24bp_start_1_gen),
1063 5);
1064 else
1065 ucbus_write(gspca_dev, lz24bp_start_1_clm,
1066 ARRAY_SIZE(lz24bp_start_1_clm),
1067 5);
1068 ucbus_write(gspca_dev, lz24bp_start_2,
1069 ARRAY_SIZE(lz24bp_start_2),
1070 6);
1071 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
1072 lz24bp_ppl(sd, mode == 2 ? 0x0564 : 0x0310);
1073 msleep(10);
1074 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -03001075 case SENSOR_MI0360:
Jean-François Moine618a8642010-06-05 07:45:04 -03001076 ucbus_write(gspca_dev, mi0360_start_0,
1077 ARRAY_SIZE(mi0360_start_0),
1078 8);
Jean-François Moinea68f7232010-06-24 05:02:57 -03001079 i2c_write(sd, mi0360_init_23,
Jean-François Moine618a8642010-06-05 07:45:04 -03001080 ARRAY_SIZE(mi0360_init_23));
Jean-François Moinea68f7232010-06-24 05:02:57 -03001081 i2c_write(sd, mi0360_init_24,
Jean-François Moine618a8642010-06-05 07:45:04 -03001082 ARRAY_SIZE(mi0360_init_24));
Jean-François Moinea68f7232010-06-24 05:02:57 -03001083 i2c_write(sd, mi0360_init_25,
Jean-François Moine618a8642010-06-05 07:45:04 -03001084 ARRAY_SIZE(mi0360_init_25));
1085 ucbus_write(gspca_dev, mi0360_start_1,
1086 ARRAY_SIZE(mi0360_start_1),
1087 5);
Jean-François Moinea68f7232010-06-24 05:02:57 -03001088 i2c_write(sd, mi0360_start_2,
Jean-François Moine618a8642010-06-05 07:45:04 -03001089 ARRAY_SIZE(mi0360_start_2));
Jean-François Moinea68f7232010-06-24 05:02:57 -03001090 i2c_write(sd, mi0360_start_3,
Jean-François Moine618a8642010-06-05 07:45:04 -03001091 ARRAY_SIZE(mi0360_start_3));
1092
1093 /* 1st start */
1094 send_start(gspca_dev);
1095 msleep(60);
1096 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
1097
Jean-François Moinea68f7232010-06-24 05:02:57 -03001098 i2c_write(sd,
Jean-François Moine618a8642010-06-05 07:45:04 -03001099 mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
1100 break;
Jean-François Moinea68f7232010-06-24 05:02:57 -03001101 default:
1102/* case SENSOR_MT9V111: */
1103 ucbus_write(gspca_dev, mi0360_start_0,
1104 ARRAY_SIZE(mi0360_start_0),
1105 8);
1106 i2c_write(sd, mt9v111_init_0,
1107 ARRAY_SIZE(mt9v111_init_0));
1108 i2c_write(sd, mt9v111_init_1,
1109 ARRAY_SIZE(mt9v111_init_1));
1110 i2c_write(sd, mt9v111_init_2,
1111 ARRAY_SIZE(mt9v111_init_2));
1112 ucbus_write(gspca_dev, mt9v111_start_1,
1113 ARRAY_SIZE(mt9v111_start_1),
Jean-François Moine46631172010-07-26 06:50:31 -03001114 5);
Jean-François Moinea68f7232010-06-24 05:02:57 -03001115 i2c_write(sd, mt9v111_init_3,
1116 ARRAY_SIZE(mt9v111_init_3));
1117 i2c_write(sd, mt9v111_init_4,
1118 ARRAY_SIZE(mt9v111_init_4));
1119 break;
Jean-François Moine618a8642010-06-05 07:45:04 -03001120 }
1121
1122 send_start(gspca_dev);
1123out:
1124 msleep(1000);
1125
1126 sd->eof_len = 0; /* init packet scan */
1127
Jean-François Moinea68f7232010-06-24 05:02:57 -03001128 if (sd->sensor == SENSOR_MT9V111)
1129 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1130
Jean-François Moine618a8642010-06-05 07:45:04 -03001131 sd->do_ctrl = 1; /* set the exposure */
1132
1133 return gspca_dev->usb_err;
1134}
1135
1136static void sd_stopN(struct gspca_dev *gspca_dev)
1137{
Jean-François Moinea68f7232010-06-24 05:02:57 -03001138 struct sd *sd = (struct sd *) gspca_dev;
1139
1140 if (sd->sensor == SENSOR_MT9V111)
1141 gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
Jean-François Moine618a8642010-06-05 07:45:04 -03001142 send_stop(gspca_dev);
1143}
1144
1145/* function called when the application gets a new frame */
1146/* It sets the exposure if required and restart the bulk transfer. */
1147static void sd_dq_callback(struct gspca_dev *gspca_dev)
1148{
1149 struct sd *sd = (struct sd *) gspca_dev;
1150 int ret;
1151
1152 if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1153 return;
1154 sd->do_ctrl = 0;
1155
1156 setexposure(gspca_dev);
1157
1158 gspca_dev->cam.bulk_nurbs = 1;
1159 ret = usb_submit_urb(gspca_dev->urb[0], GFP_ATOMIC);
1160 if (ret < 0)
1161 PDEBUG(D_ERR|D_PACK, "sd_dq_callback() err %d", ret);
1162
1163 /* wait a little time, otherwise the webcam crashes */
1164 msleep(100);
1165}
1166
1167/* move a packet adding 0x00 after 0xff */
1168static void add_packet(struct gspca_dev *gspca_dev,
1169 u8 *data,
1170 int len)
1171{
1172 int i;
1173
1174 i = 0;
1175 do {
1176 if (data[i] == 0xff) {
1177 gspca_frame_add(gspca_dev, INTER_PACKET,
1178 data, i + 1);
1179 len -= i;
1180 data += i;
1181 *data = 0x00;
1182 i = 0;
1183 }
1184 } while (++i < len);
1185 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1186}
1187
1188/* end a frame and start a new one */
1189static void eof_sof(struct gspca_dev *gspca_dev)
1190{
1191 struct sd *sd = (struct sd *) gspca_dev;
1192 static const u8 ffd9[] = {0xff, 0xd9};
1193
1194 /* if control set, stop bulk transfer */
1195 if (sd->do_ctrl
1196 && gspca_dev->last_packet_type == INTER_PACKET)
1197 gspca_dev->cam.bulk_nurbs = 0;
1198 gspca_frame_add(gspca_dev, LAST_PACKET,
1199 ffd9, 2);
1200 gspca_frame_add(gspca_dev, FIRST_PACKET,
1201 sd->jpeg_hdr, JPEG_HDR_SZ);
1202}
1203
1204static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1205 u8 *data, /* isoc packet */
1206 int len) /* iso packet length */
1207{
1208 struct sd *sd = (struct sd *) gspca_dev;
1209 u8 *p;
1210 int l;
1211
1212 len -= 8; /* ignore last 8 bytes (00 00 55 aa 55 aa 00 00) */
1213
1214 /*
1215 * the end/start of frame is indicated by
1216 * 0x00 * 16 - 0xab * 8
1217 * aligned on 8 bytes boundary
1218 */
1219 if (sd->eof_len != 0) { /* if 'abababab' in previous pkt */
1220 if (*((u32 *) data) == 0xabababab) {
1221 /*fixme: should remove previous 0000ababab*/
1222 eof_sof(gspca_dev);
1223 data += 4;
1224 len -= 4;
1225 }
1226 sd->eof_len = 0;
1227 }
1228 p = data;
1229 l = len;
1230 for (;;) {
1231 if (*((u32 *) p) == 0xabababab) {
1232 if (l < 8) { /* (may be 4 only) */
1233 sd->eof_len = 1;
1234 break;
1235 }
1236 if (*((u32 *) p + 1) == 0xabababab) {
1237 add_packet(gspca_dev, data, p - data - 16);
1238 /* remove previous zeros */
1239 eof_sof(gspca_dev);
1240 p += 8;
1241 l -= 8;
1242 if (l <= 0)
1243 return;
1244 len = l;
1245 data = p;
1246 continue;
1247 }
1248 }
1249 p += 4;
1250 l -= 4;
1251 if (l <= 0)
1252 break;
1253 }
1254 add_packet(gspca_dev, data, len);
1255}
1256
1257static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1258{
1259 struct sd *sd = (struct sd *) gspca_dev;
1260
1261 sd->gain = val;
1262 if (gspca_dev->streaming)
1263 sd->do_ctrl = 1;
1264 return 0;
1265}
1266
1267static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1268{
1269 struct sd *sd = (struct sd *) gspca_dev;
1270
1271 *val = sd->gain;
1272 return 0;
1273}
1274static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val)
1275{
1276 struct sd *sd = (struct sd *) gspca_dev;
1277
1278 sd->expo = val;
1279 if (gspca_dev->streaming)
1280 sd->do_ctrl = 1;
1281 return 0;
1282}
1283
1284static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val)
1285{
1286 struct sd *sd = (struct sd *) gspca_dev;
1287
1288 *val = sd->expo;
1289 return 0;
1290}
1291
1292static int sd_set_jcomp(struct gspca_dev *gspca_dev,
1293 struct v4l2_jpegcompression *jcomp)
1294{
1295 struct sd *sd = (struct sd *) gspca_dev;
1296 int quality;
1297
1298 if (jcomp->quality >= (QUAL_0 + QUAL_1) / 2)
1299 quality = 0;
1300 else if (jcomp->quality >= (QUAL_1 + QUAL_2) / 2)
1301 quality = 1;
1302 else if (jcomp->quality >= (QUAL_2 + QUAL_3) / 2)
1303 quality = 2;
1304 else
1305 quality = 3;
1306
1307 if (quality != sd->quality) {
1308 sd->quality = quality;
1309 if (gspca_dev->streaming) {
1310 send_stop(gspca_dev);
1311 sd_jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1312 msleep(70);
1313 send_start(gspca_dev);
1314 }
1315 }
1316 return gspca_dev->usb_err;
1317}
1318
1319static int sd_get_jcomp(struct gspca_dev *gspca_dev,
1320 struct v4l2_jpegcompression *jcomp)
1321{
1322 struct sd *sd = (struct sd *) gspca_dev;
1323
1324 memset(jcomp, 0, sizeof *jcomp);
1325 jcomp->quality = quality_tb[sd->quality];
1326 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
1327 | V4L2_JPEG_MARKER_DQT;
1328 return 0;
1329}
1330
1331/* sub-driver description */
1332static const struct sd_desc sd_desc = {
1333 .name = MODULE_NAME,
1334 .ctrls = sd_ctrls,
1335 .nctrls = ARRAY_SIZE(sd_ctrls),
1336 .config = sd_config,
1337 .init = sd_init,
1338 .isoc_init = sd_isoc_init,
1339 .start = sd_start,
1340 .stopN = sd_stopN,
1341 .pkt_scan = sd_pkt_scan,
1342 .dq_callback = sd_dq_callback,
1343 .get_jcomp = sd_get_jcomp,
1344 .set_jcomp = sd_set_jcomp,
1345};
1346
1347/* Table of supported USB devices */
1348#define ST(sensor, type) \
1349 .driver_info = (SENSOR_ ## sensor << 8) \
1350 | (type)
1351static const __devinitdata struct usb_device_id device_table[] = {
1352 {USB_DEVICE(0x041e, 0x4038), ST(MI0360, 0)},
1353 {USB_DEVICE(0x041e, 0x403c), ST(LZ24BP, 0)},
1354 {USB_DEVICE(0x041e, 0x403d), ST(LZ24BP, 0)},
1355 {USB_DEVICE(0x041e, 0x4041), ST(LZ24BP, Creative_live_motion)},
Jean-François Moinea68f7232010-06-24 05:02:57 -03001356 {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
Jean-François Moine618a8642010-06-05 07:45:04 -03001357 {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1358 {}
1359};
1360MODULE_DEVICE_TABLE(usb, device_table);
1361
1362
1363/* -- device connect -- */
1364static int sd_probe(struct usb_interface *intf,
1365 const struct usb_device_id *id)
1366{
1367 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1368 THIS_MODULE);
1369}
1370
1371static struct usb_driver sd_driver = {
1372 .name = MODULE_NAME,
1373 .id_table = device_table,
1374 .probe = sd_probe,
1375 .disconnect = gspca_disconnect,
1376#ifdef CONFIG_PM
1377 .suspend = gspca_suspend,
1378 .resume = gspca_resume,
1379#endif
1380};
1381
1382/* -- module insert / remove -- */
1383static int __init sd_mod_init(void)
1384{
1385 int ret;
1386
1387 ret = usb_register(&sd_driver);
1388 if (ret < 0)
1389 return ret;
1390 info("registered");
1391 return 0;
1392}
1393static void __exit sd_mod_exit(void)
1394{
1395 usb_deregister(&sd_driver);
1396 info("deregistered");
1397}
1398
1399module_init(sd_mod_init);
1400module_exit(sd_mod_exit);