V4L/DVB: gspca - sq930x: Change the default values of gain and exposure
[linux-2.6/cjktty.git] / drivers / media / video / gspca / sq930x.c
blobcec3dafd2f55df4311d825d619c794a38810fa02
1 /*
2 * SQ930x subdriver
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>
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.
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.
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
23 #define MODULE_NAME "sq930x"
25 #include "gspca.h"
26 #include "jpeg.h"
28 MODULE_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>");
31 MODULE_DESCRIPTION("GSPCA/SQ930x USB Camera Driver");
32 MODULE_LICENSE("GPL");
34 #define BULK_TRANSFER_LEN 5128
36 /* Structure to hold all of our device specific stuff */
37 struct sd {
38 struct gspca_dev gspca_dev; /* !! must be the first item */
40 u16 expo;
41 u8 gain;
43 u8 quality; /* webcam quality 0..3 */
44 #define QUALITY_DEF 1
46 u8 gpio[2];
48 u8 eof_len;
49 u8 do_ctrl;
51 u8 sensor;
52 u8 type;
53 #define Generic 0
54 #define Creative_live_motion 1
56 enum sensors {
57 SENSOR_ICX098BQ,
58 SENSOR_LZ24BP,
59 SENSOR_MI0360,
60 SENSOR_MT9V111,
61 SENSOR_OV7660,
62 SENSOR_OV9630,
65 static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val);
66 static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val);
67 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
68 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
70 static const struct ctrl sd_ctrls[] = {
73 .id = V4L2_CID_EXPOSURE,
74 .type = V4L2_CTRL_TYPE_INTEGER,
75 .name = "Exposure",
76 .minimum = 0x0001,
77 .maximum = 0x0fff,
78 .step = 1,
79 #define EXPO_DEF 0x0356
80 .default_value = EXPO_DEF,
82 .set = sd_setexpo,
83 .get = sd_getexpo,
87 .id = V4L2_CID_GAIN,
88 .type = V4L2_CTRL_TYPE_INTEGER,
89 .name = "Gain",
90 .minimum = 0x01,
91 .maximum = 0xff,
92 .step = 1,
93 #define GAIN_DEF 0x8d
94 .default_value = GAIN_DEF,
96 .set = sd_setgain,
97 .get = sd_getgain,
101 static 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},
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
124 static const u8 quality_tb[4] = { QUAL_0, QUAL_1, QUAL_2, QUAL_3 };
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
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
146 struct ucbus_write_cmd {
147 u16 bw_addr;
148 u8 bw_data;
150 struct i2c_write_cmd {
151 u8 reg;
152 u16 val;
155 static 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}
183 static 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}
192 static 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}
203 static 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}
231 static 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}
240 static 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}
249 static 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}
260 static const struct ucbus_write_cmd mi0360_start_0[] = {
261 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0xcc}, {0xf333, 0xcc},
262 {0xf334, 0xcc}, {0xf335, 0xcc}, {0xf33f, 0x00}
264 static 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 */
270 static const struct i2c_write_cmd mi0360_init_24[] = {
271 {0x03, 0x01e5}, /* window height */
272 {0x04, 0x0285}, /* window width */
274 static 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 */
281 static 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}
289 static const struct i2c_write_cmd mi0360_start_2[] = {
290 {0x62, 0x041d}, /* reserved - def 0x0418 */
292 static const struct i2c_write_cmd mi0360_start_3[] = {
293 {0x05, 0x007b}, /* horiz blanking */
295 static const struct i2c_write_cmd mi0360_start_4[] = {
296 {0x05, 0x03f5}, /* horiz blanking */
299 static const struct i2c_write_cmd mt9v111_init_0[] = {
300 {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 */
305 static const struct i2c_write_cmd mt9v111_init_1[] = {
306 {0x03, 0x01e5}, /* window height */
307 {0x04, 0x0285}, /* window width */
309 static const struct i2c_write_cmd mt9v111_init_2[] = {
310 {0x30, 0x7800},
311 {0x31, 0x0000},
312 {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 */
319 static 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}
327 static const struct i2c_write_cmd mt9v111_init_3[] = {
328 {0x62, 0x0405},
330 static const struct i2c_write_cmd mt9v111_init_4[] = {
331 {0x05, 0x00ce}, /* horizontal blanking */
334 static const struct ucbus_write_cmd ov7660_start_0[] = {
335 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0xc0},
336 {0xf334, 0x39}, {0xf335, 0xe7}, {0xf33f, 0x03}
339 static const struct ucbus_write_cmd ov9630_start_0[] = {
340 {0x0354, 0x00}, {0x03fa, 0x00}, {0xf332, 0x00}, {0xf333, 0x00},
341 {0xf334, 0x3e}, {0xf335, 0xf8}, {0xf33f, 0x03}
344 static const struct cap_s {
345 u8 cc_sizeid;
346 u8 cc_bytes[32];
347 } capconfig[4][3] = {
348 [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} },
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} },
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} },
400 [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} },
419 struct 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;
428 static const struct sensor_s sensor_tb[] = {
429 [SENSOR_ICX098BQ] = {
430 "icx098bp",
431 0x00, 0x00,
433 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
434 SQ930_GPIO_DFL_I2C_SDA,
436 SQ930_GPIO_RSTBAR
438 8, icx098bq_start_0
440 [SENSOR_LZ24BP] = {
441 "lz24bp",
442 0x00, 0x00,
444 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
445 SQ930_GPIO_DFL_I2C_SDA,
447 SQ930_GPIO_RSTBAR
449 8, lz24bp_start_0
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,
460 7, mi0360_start_0
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,
471 7, mi0360_start_0
473 [SENSOR_OV7660] = {
474 "ov7660",
475 0x21, 0x00,
477 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
478 SQ930_GPIO_DFL_I2C_SDA,
480 SQ930_GPIO_RSTBAR
482 7, ov7660_start_0
484 [SENSOR_OV9630] = {
485 "ov9630",
486 0x30, 0x00,
488 SQ930_GPIO_DFL_I2C_SDA | SQ930_GPIO_DFL_I2C_SCL,
489 SQ930_GPIO_DFL_I2C_SDA,
491 SQ930_GPIO_RSTBAR
493 7, ov9630_start_0
497 static void reg_r(struct gspca_dev *gspca_dev,
498 u16 value, int len)
500 int ret;
502 if (gspca_dev->usb_err < 0)
503 return;
504 ret = usb_control_msg(gspca_dev->dev,
505 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);
510 if (ret < 0) {
511 PDEBUG(D_ERR, "reg_r %04x failed %d", value, ret);
512 gspca_dev->usb_err = ret;
516 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
518 int ret;
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;
536 static void reg_wb(struct gspca_dev *gspca_dev, u16 value, u16 index,
537 const u8 *data, int len)
539 int ret;
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;
559 static void i2c_write(struct sd *sd,
560 const struct i2c_write_cmd *cmd,
561 int ncmds)
563 struct gspca_dev *gspca_dev = &sd->gspca_dev;
564 const struct sensor_s *sensor;
565 u16 val, idx;
566 u8 *buf;
567 int ret;
569 if (gspca_dev->usb_err < 0)
570 return;
572 sensor = &sensor_tb[sd->sensor];
574 val = (sensor->i2c_addr << 8) | SQ930_CTRL_I2C_IO;
575 idx = (cmd->val & 0xff00) | cmd->reg;
577 buf = gspca_dev->usb_buf;
578 *buf++ = sensor->i2c_dum;
579 *buf++ = cmd->val;
581 while (--ncmds > 0) {
582 cmd++;
583 *buf++ = cmd->reg;
584 *buf++ = cmd->val >> 8;
585 *buf++ = sensor->i2c_dum;
586 *buf++ = cmd->val;
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;
604 static void ucbus_write(struct gspca_dev *gspca_dev,
605 const struct ucbus_write_cmd *cmd,
606 int ncmds,
607 int batchsize)
609 u8 *buf;
610 u16 val, idx;
611 int len, ret;
613 if (gspca_dev->usb_err < 0)
614 return;
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;
622 #endif
624 for (;;) {
625 len = ncmds;
626 if (len > batchsize)
627 len = batchsize;
628 ncmds -= len;
630 val = (cmd->bw_addr << 8) | SQ930_CTRL_UCBUS_IO;
631 idx = (cmd->bw_data << 8) | (cmd->bw_addr >> 8);
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;
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;
659 msleep(30);
660 if (ncmds <= 0)
661 break;
662 cmd++;
666 static void gpio_set(struct sd *sd, u16 val, u16 mask)
668 struct gspca_dev *gspca_dev = &sd->gspca_dev;
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);
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);
686 static void gpio_init(struct sd *sd,
687 const u8 *gpio)
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);
696 static void bridge_init(struct sd *sd)
698 static const struct ucbus_write_cmd clkfreq_cmd = {
699 0xf031, 0 /* SQ930_CLKFREQ_60MHZ */
702 ucbus_write(&sd->gspca_dev, &clkfreq_cmd, 1, 1);
704 gpio_set(sd, SQ930_GPIO_POWER, 0xff00);
707 static void cmos_probe(struct gspca_dev *gspca_dev)
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,
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;
730 if (i >= ARRAY_SIZE(probe_order))
731 PDEBUG(D_PROBE, "Unknown sensor");
732 else
733 sd->sensor = probe_order[i];
736 static void mt9v111_init(struct gspca_dev *gspca_dev)
738 int i, nwait;
739 static const u8 cmd_001b[] = {
740 0x00, 0x3b, 0xf6, 0x01, 0x03, 0x02, 0x00, 0x00,
741 0x00, 0x00, 0x00
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},
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;
766 msleep(50);
771 static void global_init(struct sd *sd, int first_time)
773 switch (sd->sensor) {
774 case SENSOR_ICX098BQ:
775 if (first_time)
776 ucbus_write(&sd->gspca_dev,
777 icx098bq_start_0,
778 8, 8);
779 gpio_init(sd, sensor_tb[sd->sensor].gpio);
780 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);
791 gpio_init(sd, sensor_tb[sd->sensor].gpio);
792 break;
793 case SENSOR_MI0360:
794 if (first_time)
795 ucbus_write(&sd->gspca_dev,
796 mi0360_start_0,
797 ARRAY_SIZE(mi0360_start_0),
799 gpio_init(sd, sensor_tb[sd->sensor].gpio);
800 gpio_set(sd, SQ930_GPIO_EXTRA2, SQ930_GPIO_EXTRA2);
801 break;
802 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;
812 static void lz24bp_ppl(struct sd *sd, u16 ppl)
814 struct ucbus_write_cmd cmds[2] = {
815 {0xf810, ppl >> 8},
816 {0xf811, ppl}
819 ucbus_write(&sd->gspca_dev, cmds, ARRAY_SIZE(cmds), 2);
822 static void setexposure(struct gspca_dev *gspca_dev)
824 struct sd *sd = (struct sd *) gspca_dev;
825 int i, integclks, intstartclk, frameclks, min_frclk;
826 const struct sensor_s *sensor;
827 u16 cmd;
828 u8 buf[15];
830 integclks = sd->expo;
831 i = 0;
832 cmd = SQ930_CTRL_SET_EXPOSURE;
834 switch (sd->sensor) {
835 case SENSOR_ICX098BQ: /* ccd */
836 case SENSOR_LZ24BP:
837 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;
845 buf[i++] = intstartclk >> 8;
846 buf[i++] = intstartclk;
847 buf[i++] = frameclks >> 8;
848 buf[i++] = frameclks;
849 buf[i++] = sd->gain;
850 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;
865 buf[i++] = 0x80 + sd->gain / 2; /* val L */
866 buf[i++] = 0x00;
867 buf[i++] = 0x00;
868 buf[i++] = 0x00;
869 buf[i++] = 0x00;
870 buf[i++] = 0x83;
871 break;
873 reg_wb(gspca_dev, cmd, 0, buf, i);
876 /* This function is called at probe time just before sd_init */
877 static int sd_config(struct gspca_dev *gspca_dev,
878 const struct usb_device_id *id)
880 struct sd *sd = (struct sd *) gspca_dev;
881 struct cam *cam = &gspca_dev->cam;
883 sd->sensor = id->driver_info >> 8;
884 sd->type = id->driver_info;
886 cam->cam_mode = vga_mode;
887 cam->nmodes = ARRAY_SIZE(vga_mode);
889 cam->bulk = 1;
890 cam->bulk_size = BULK_TRANSFER_LEN;
891 /* cam->bulk_nurbs = 2; fixme: if no setexpo sync */
893 sd->quality = QUALITY_DEF;
894 sd->gain = GAIN_DEF;
895 sd->expo = EXPO_DEF;
897 return 0;
900 /* this function is called at probe and resume time */
901 static int sd_init(struct gspca_dev *gspca_dev)
903 struct sd *sd = (struct sd *) gspca_dev;
905 sd->gpio[0] = sd->gpio[1] = 0xff; /* force gpio rewrite */
907 /*fixme: is this needed for icx098bp and mi0360?
908 if (sd->sensor != SENSOR_LZ24BP)
909 reg_w(gspca_dev, SQ930_CTRL_RESET, 0x0000);
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
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
927 * 5: f6 = cdd (icx098bq, lz24bp) / fe or de = cmos (i2c) (other sensors)
928 * 6: c8 / c9 / ca / cf = mode webcam?, sensor? webcam?
929 * 7: 00
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]);
941 bridge_init(sd);
943 if (sd->sensor == SENSOR_MI0360) {
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);
955 PDEBUG(D_PROBE, "Sensor %s", sensor_tb[sd->sensor].name);
957 global_init(sd, 1);
958 return gspca_dev->usb_err;
961 /* special function to create the quantization tables of the JPEG header */
962 static void sd_jpeg_set_qual(u8 *jpeg_hdr,
963 int quality)
965 int i, sc1, sc2;
967 quality = quality_tb[quality]; /* convert to JPEG quality */
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%
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;
985 /* send the start/stop commands to the webcam */
986 static void send_start(struct gspca_dev *gspca_dev)
988 struct sd *sd = (struct sd *) gspca_dev;
989 const struct cap_s *cap;
990 int mode, quality;
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);
1002 static void send_stop(struct gspca_dev *gspca_dev)
1004 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0);
1007 /* function called at start time before URB creation */
1008 static int sd_isoc_init(struct gspca_dev *gspca_dev)
1010 struct sd *sd = (struct sd *) gspca_dev;
1012 gspca_dev->cam.bulk_nurbs = 1; /* there must be one URB only */
1013 sd->do_ctrl = 0;
1014 return 0;
1017 /* start the capture */
1018 static int sd_start(struct gspca_dev *gspca_dev)
1020 struct sd *sd = (struct sd *) gspca_dev;
1021 int mode;
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);
1028 bridge_init(sd);
1029 global_init(sd, 0);
1030 msleep(100);
1032 switch (sd->sensor) {
1033 case SENSOR_ICX098BQ:
1034 ucbus_write(gspca_dev, icx098bq_start_0,
1035 ARRAY_SIZE(icx098bq_start_0),
1037 ucbus_write(gspca_dev, icx098bq_start_1,
1038 ARRAY_SIZE(icx098bq_start_1),
1040 ucbus_write(gspca_dev, icx098bq_start_2,
1041 ARRAY_SIZE(icx098bq_start_2),
1043 msleep(50);
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);
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),
1060 if (sd->type != Creative_live_motion)
1061 ucbus_write(gspca_dev, lz24bp_start_1_gen,
1062 ARRAY_SIZE(lz24bp_start_1_gen),
1064 else
1065 ucbus_write(gspca_dev, lz24bp_start_1_clm,
1066 ARRAY_SIZE(lz24bp_start_1_clm),
1068 ucbus_write(gspca_dev, lz24bp_start_2,
1069 ARRAY_SIZE(lz24bp_start_2),
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;
1075 case SENSOR_MI0360:
1076 ucbus_write(gspca_dev, mi0360_start_0,
1077 ARRAY_SIZE(mi0360_start_0),
1079 i2c_write(sd, mi0360_init_23,
1080 ARRAY_SIZE(mi0360_init_23));
1081 i2c_write(sd, mi0360_init_24,
1082 ARRAY_SIZE(mi0360_init_24));
1083 i2c_write(sd, mi0360_init_25,
1084 ARRAY_SIZE(mi0360_init_25));
1085 ucbus_write(gspca_dev, mi0360_start_1,
1086 ARRAY_SIZE(mi0360_start_1),
1088 i2c_write(sd, mi0360_start_2,
1089 ARRAY_SIZE(mi0360_start_2));
1090 i2c_write(sd, mi0360_start_3,
1091 ARRAY_SIZE(mi0360_start_3));
1093 /* 1st start */
1094 send_start(gspca_dev);
1095 msleep(60);
1096 reg_w(gspca_dev, SQ930_CTRL_CAP_STOP, 0x0000);
1098 i2c_write(sd,
1099 mi0360_start_4, ARRAY_SIZE(mi0360_start_4));
1100 break;
1101 default:
1102 /* case SENSOR_MT9V111: */
1103 ucbus_write(gspca_dev, mi0360_start_0,
1104 ARRAY_SIZE(mi0360_start_0),
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),
1115 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;
1122 send_start(gspca_dev);
1123 out:
1124 msleep(1000);
1126 sd->eof_len = 0; /* init packet scan */
1128 if (sd->sensor == SENSOR_MT9V111)
1129 gpio_set(sd, SQ930_GPIO_DFL_LED, SQ930_GPIO_DFL_LED);
1131 sd->do_ctrl = 1; /* set the exposure */
1133 return gspca_dev->usb_err;
1136 static void sd_stopN(struct gspca_dev *gspca_dev)
1138 struct sd *sd = (struct sd *) gspca_dev;
1140 if (sd->sensor == SENSOR_MT9V111)
1141 gpio_set(sd, 0, SQ930_GPIO_DFL_LED);
1142 send_stop(gspca_dev);
1145 /* function called when the application gets a new frame */
1146 /* It sets the exposure if required and restart the bulk transfer. */
1147 static void sd_dq_callback(struct gspca_dev *gspca_dev)
1149 struct sd *sd = (struct sd *) gspca_dev;
1150 int ret;
1152 if (!sd->do_ctrl || gspca_dev->cam.bulk_nurbs != 0)
1153 return;
1154 sd->do_ctrl = 0;
1156 setexposure(gspca_dev);
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);
1163 /* wait a little time, otherwise the webcam crashes */
1164 msleep(100);
1167 /* move a packet adding 0x00 after 0xff */
1168 static void add_packet(struct gspca_dev *gspca_dev,
1169 u8 *data,
1170 int len)
1172 int i;
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;
1184 } while (++i < len);
1185 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1188 /* end a frame and start a new one */
1189 static void eof_sof(struct gspca_dev *gspca_dev)
1191 struct sd *sd = (struct sd *) gspca_dev;
1192 static const u8 ffd9[] = {0xff, 0xd9};
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);
1204 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1205 u8 *data, /* isoc packet */
1206 int len) /* iso packet length */
1208 struct sd *sd = (struct sd *) gspca_dev;
1209 u8 *p;
1210 int l;
1212 len -= 8; /* ignore last 8 bytes (00 00 55 aa 55 aa 00 00) */
1215 * the end/start of frame is indicated by
1216 * 0x00 * 16 - 0xab * 8
1217 * aligned on 8 bytes boundary
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;
1226 sd->eof_len = 0;
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;
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;
1249 p += 4;
1250 l -= 4;
1251 if (l <= 0)
1252 break;
1254 add_packet(gspca_dev, data, len);
1257 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1259 struct sd *sd = (struct sd *) gspca_dev;
1261 sd->gain = val;
1262 if (gspca_dev->streaming)
1263 sd->do_ctrl = 1;
1264 return 0;
1267 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1269 struct sd *sd = (struct sd *) gspca_dev;
1271 *val = sd->gain;
1272 return 0;
1274 static int sd_setexpo(struct gspca_dev *gspca_dev, __s32 val)
1276 struct sd *sd = (struct sd *) gspca_dev;
1278 sd->expo = val;
1279 if (gspca_dev->streaming)
1280 sd->do_ctrl = 1;
1281 return 0;
1284 static int sd_getexpo(struct gspca_dev *gspca_dev, __s32 *val)
1286 struct sd *sd = (struct sd *) gspca_dev;
1288 *val = sd->expo;
1289 return 0;
1292 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
1293 struct v4l2_jpegcompression *jcomp)
1295 struct sd *sd = (struct sd *) gspca_dev;
1296 int quality;
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;
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);
1316 return gspca_dev->usb_err;
1319 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
1320 struct v4l2_jpegcompression *jcomp)
1322 struct sd *sd = (struct sd *) gspca_dev;
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;
1331 /* sub-driver description */
1332 static 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,
1347 /* Table of supported USB devices */
1348 #define ST(sensor, type) \
1349 .driver_info = (SENSOR_ ## sensor << 8) \
1350 | (type)
1351 static 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)},
1356 {USB_DEVICE(0x2770, 0x930b), ST(MI0360, 0)},
1357 {USB_DEVICE(0x2770, 0x930c), ST(MI0360, 0)},
1360 MODULE_DEVICE_TABLE(usb, device_table);
1363 /* -- device connect -- */
1364 static int sd_probe(struct usb_interface *intf,
1365 const struct usb_device_id *id)
1367 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1368 THIS_MODULE);
1371 static 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
1382 /* -- module insert / remove -- */
1383 static int __init sd_mod_init(void)
1385 int ret;
1387 ret = usb_register(&sd_driver);
1388 if (ret < 0)
1389 return ret;
1390 info("registered");
1391 return 0;
1393 static void __exit sd_mod_exit(void)
1395 usb_deregister(&sd_driver);
1396 info("deregistered");
1399 module_init(sd_mod_init);
1400 module_exit(sd_mod_exit);