xref: /openbmc/linux/drivers/media/usb/gspca/sonixj.c (revision b6bec26c)
1 /*
2  * Sonix sn9c102p sn9c105 sn9c120 (jpeg) subdriver
3  *
4  * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
5  * Copyright (C) 2005 Michel Xhaard mxhaard@magic.fr
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 
24 #define MODULE_NAME "sonixj"
25 
26 #include <linux/input.h>
27 #include "gspca.h"
28 #include "jpeg.h"
29 
30 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
31 MODULE_DESCRIPTION("GSPCA/SONIX JPEG USB Camera Driver");
32 MODULE_LICENSE("GPL");
33 
34 /* controls */
35 enum e_ctrl {
36 	BRIGHTNESS,
37 	CONTRAST,
38 	COLORS,
39 	BLUE,
40 	RED,
41 	GAMMA,
42 	EXPOSURE,
43 	AUTOGAIN,
44 	GAIN,
45 	HFLIP,
46 	VFLIP,
47 	SHARPNESS,
48 	ILLUM,
49 	FREQ,
50 	NCTRLS		/* number of controls */
51 };
52 
53 /* specific webcam descriptor */
54 struct sd {
55 	struct gspca_dev gspca_dev;	/* !! must be the first item */
56 
57 	struct gspca_ctrl ctrls[NCTRLS];
58 
59 	atomic_t avg_lum;
60 	u32 exposure;
61 
62 	struct work_struct work;
63 	struct workqueue_struct *work_thread;
64 
65 	u32 pktsz;			/* (used by pkt_scan) */
66 	u16 npkt;
67 	s8 nchg;
68 	s8 short_mark;
69 
70 	u8 quality;			/* image quality */
71 #define QUALITY_MIN 25
72 #define QUALITY_MAX 90
73 #define QUALITY_DEF 70
74 
75 	u8 reg01;
76 	u8 reg17;
77 	u8 reg18;
78 	u8 flags;
79 
80 	s8 ag_cnt;
81 #define AG_CNT_START 13
82 
83 	u8 bridge;
84 #define BRIDGE_SN9C102P 0
85 #define BRIDGE_SN9C105 1
86 #define BRIDGE_SN9C110 2
87 #define BRIDGE_SN9C120 3
88 	u8 sensor;			/* Type of image sensor chip */
89 	u8 i2c_addr;
90 
91 	u8 jpeg_hdr[JPEG_HDR_SZ];
92 };
93 enum sensors {
94 	SENSOR_ADCM1700,
95 	SENSOR_GC0307,
96 	SENSOR_HV7131R,
97 	SENSOR_MI0360,
98 	SENSOR_MI0360B,
99 	SENSOR_MO4000,
100 	SENSOR_MT9V111,
101 	SENSOR_OM6802,
102 	SENSOR_OV7630,
103 	SENSOR_OV7648,
104 	SENSOR_OV7660,
105 	SENSOR_PO1030,
106 	SENSOR_PO2030N,
107 	SENSOR_SOI768,
108 	SENSOR_SP80708,
109 };
110 
111 static void qual_upd(struct work_struct *work);
112 
113 /* device flags */
114 #define F_PDN_INV	0x01	/* inverse pin S_PWR_DN / sn_xxx tables */
115 #define F_ILLUM		0x02	/* presence of illuminator */
116 
117 /* sn9c1xx definitions */
118 /* register 0x01 */
119 #define S_PWR_DN	0x01	/* sensor power down */
120 #define S_PDN_INV	0x02	/* inverse pin S_PWR_DN */
121 #define V_TX_EN		0x04	/* video transfer enable */
122 #define LED		0x08	/* output to pin LED */
123 #define SCL_SEL_OD	0x20	/* open-drain mode */
124 #define SYS_SEL_48M	0x40	/* system clock 0: 24MHz, 1: 48MHz */
125 /* register 0x17 */
126 #define MCK_SIZE_MASK	0x1f	/* sensor master clock */
127 #define SEN_CLK_EN	0x20	/* enable sensor clock */
128 #define DEF_EN		0x80	/* defect pixel by 0: soft, 1: hard */
129 
130 /* V4L2 controls supported by the driver */
131 static void setbrightness(struct gspca_dev *gspca_dev);
132 static void setcontrast(struct gspca_dev *gspca_dev);
133 static void setcolors(struct gspca_dev *gspca_dev);
134 static void setredblue(struct gspca_dev *gspca_dev);
135 static void setgamma(struct gspca_dev *gspca_dev);
136 static void setexposure(struct gspca_dev *gspca_dev);
137 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
138 static void setgain(struct gspca_dev *gspca_dev);
139 static void sethvflip(struct gspca_dev *gspca_dev);
140 static void setsharpness(struct gspca_dev *gspca_dev);
141 static void setillum(struct gspca_dev *gspca_dev);
142 static void setfreq(struct gspca_dev *gspca_dev);
143 
144 static const struct ctrl sd_ctrls[NCTRLS] = {
145 [BRIGHTNESS] = {
146 	    {
147 		.id      = V4L2_CID_BRIGHTNESS,
148 		.type    = V4L2_CTRL_TYPE_INTEGER,
149 		.name    = "Brightness",
150 		.minimum = 0,
151 		.maximum = 0xff,
152 		.step    = 1,
153 		.default_value = 0x80,
154 	    },
155 	    .set_control = setbrightness
156 	},
157 [CONTRAST] = {
158 	    {
159 		.id      = V4L2_CID_CONTRAST,
160 		.type    = V4L2_CTRL_TYPE_INTEGER,
161 		.name    = "Contrast",
162 		.minimum = 0,
163 #define CONTRAST_MAX 127
164 		.maximum = CONTRAST_MAX,
165 		.step    = 1,
166 		.default_value = 20,
167 	    },
168 	    .set_control = setcontrast
169 	},
170 [COLORS] = {
171 	    {
172 		.id      = V4L2_CID_SATURATION,
173 		.type    = V4L2_CTRL_TYPE_INTEGER,
174 		.name    = "Saturation",
175 		.minimum = 0,
176 		.maximum = 40,
177 		.step    = 1,
178 #define COLORS_DEF 25
179 		.default_value = COLORS_DEF,
180 	    },
181 	    .set_control = setcolors
182 	},
183 [BLUE] = {
184 	    {
185 		.id      = V4L2_CID_BLUE_BALANCE,
186 		.type    = V4L2_CTRL_TYPE_INTEGER,
187 		.name    = "Blue Balance",
188 		.minimum = 24,
189 		.maximum = 40,
190 		.step    = 1,
191 		.default_value = 32,
192 	    },
193 	    .set_control = setredblue
194 	},
195 [RED] = {
196 	    {
197 		.id      = V4L2_CID_RED_BALANCE,
198 		.type    = V4L2_CTRL_TYPE_INTEGER,
199 		.name    = "Red Balance",
200 		.minimum = 24,
201 		.maximum = 40,
202 		.step    = 1,
203 		.default_value = 32,
204 	    },
205 	    .set_control = setredblue
206 	},
207 [GAMMA] = {
208 	    {
209 		.id      = V4L2_CID_GAMMA,
210 		.type    = V4L2_CTRL_TYPE_INTEGER,
211 		.name    = "Gamma",
212 		.minimum = 0,
213 		.maximum = 40,
214 		.step    = 1,
215 #define GAMMA_DEF 20
216 		.default_value = GAMMA_DEF,
217 	    },
218 	    .set_control = setgamma
219 	},
220 [EXPOSURE] = {
221 	    {
222 		.id      = V4L2_CID_EXPOSURE,
223 		.type    = V4L2_CTRL_TYPE_INTEGER,
224 		.name    = "Exposure",
225 		.minimum = 500,
226 		.maximum = 1500,
227 		.step    = 1,
228 		.default_value = 1024
229 	    },
230 	    .set_control = setexposure
231 	},
232 [AUTOGAIN] = {
233 	    {
234 		.id      = V4L2_CID_AUTOGAIN,
235 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
236 		.name    = "Auto Gain",
237 		.minimum = 0,
238 		.maximum = 1,
239 		.step    = 1,
240 		.default_value = 1
241 	    },
242 	    .set = sd_setautogain,
243 	},
244 [GAIN] = {
245 	    {
246 		.id      = V4L2_CID_GAIN,
247 		.type    = V4L2_CTRL_TYPE_INTEGER,
248 		.name    = "Gain",
249 		.minimum = 4,
250 		.maximum = 49,
251 		.step    = 1,
252 		.default_value = 15
253 	    },
254 	    .set_control = setgain
255 	},
256 [HFLIP] = {
257 	    {
258 		.id      = V4L2_CID_HFLIP,
259 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
260 		.name    = "Mirror",
261 		.minimum = 0,
262 		.maximum = 1,
263 		.step    = 1,
264 		.default_value = 0,
265 	    },
266 	    .set_control = sethvflip
267 	},
268 [VFLIP] = {
269 	    {
270 		.id      = V4L2_CID_VFLIP,
271 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
272 		.name    = "Vflip",
273 		.minimum = 0,
274 		.maximum = 1,
275 		.step    = 1,
276 		.default_value = 0,
277 	    },
278 	    .set_control = sethvflip
279 	},
280 [SHARPNESS] = {
281 	    {
282 		.id	 = V4L2_CID_SHARPNESS,
283 		.type    = V4L2_CTRL_TYPE_INTEGER,
284 		.name    = "Sharpness",
285 		.minimum = 0,
286 		.maximum = 255,
287 		.step    = 1,
288 		.default_value = 90,
289 	    },
290 	    .set_control = setsharpness
291 	},
292 [ILLUM] = {
293 	    {
294 		.id      = V4L2_CID_ILLUMINATORS_1,
295 		.type    = V4L2_CTRL_TYPE_BOOLEAN,
296 		.name    = "Illuminator / infrared",
297 		.minimum = 0,
298 		.maximum = 1,
299 		.step    = 1,
300 		.default_value = 0,
301 	    },
302 	    .set_control = setillum
303 	},
304 /* ov7630/ov7648/ov7660 only */
305 [FREQ] = {
306 	    {
307 		.id	 = V4L2_CID_POWER_LINE_FREQUENCY,
308 		.type    = V4L2_CTRL_TYPE_MENU,
309 		.name    = "Light frequency filter",
310 		.minimum = 0,
311 		.maximum = 2,	/* 0: 0, 1: 50Hz, 2:60Hz */
312 		.step    = 1,
313 		.default_value = 1,
314 	    },
315 	    .set_control = setfreq
316 	},
317 };
318 
319 /* table of the disabled controls */
320 static const __u32 ctrl_dis[] = {
321 [SENSOR_ADCM1700] =	(1 << EXPOSURE) |
322 			(1 << AUTOGAIN) |
323 			(1 << GAIN) |
324 			(1 << HFLIP) |
325 			(1 << VFLIP) |
326 			(1 << FREQ),
327 
328 [SENSOR_GC0307] =	(1 << EXPOSURE) |
329 			(1 << GAIN) |
330 			(1 << HFLIP) |
331 			(1 << VFLIP) |
332 			(1 << FREQ),
333 
334 [SENSOR_HV7131R] =	(1 << EXPOSURE) |
335 			(1 << GAIN) |
336 			(1 << HFLIP) |
337 			(1 << FREQ),
338 
339 [SENSOR_MI0360] =	(1 << EXPOSURE) |
340 			(1 << GAIN) |
341 			(1 << HFLIP) |
342 			(1 << VFLIP) |
343 			(1 << FREQ),
344 
345 [SENSOR_MI0360B] =	(1 << EXPOSURE) |
346 			(1 << GAIN) |
347 			(1 << HFLIP) |
348 			(1 << VFLIP) |
349 			(1 << FREQ),
350 
351 [SENSOR_MO4000] =	(1 << EXPOSURE) |
352 			(1 << GAIN) |
353 			(1 << HFLIP) |
354 			(1 << VFLIP) |
355 			(1 << FREQ),
356 
357 [SENSOR_MT9V111] =	(1 << EXPOSURE) |
358 			(1 << GAIN) |
359 			(1 << HFLIP) |
360 			(1 << VFLIP) |
361 			(1 << FREQ),
362 
363 [SENSOR_OM6802] =	(1 << EXPOSURE) |
364 			(1 << GAIN) |
365 			(1 << HFLIP) |
366 			(1 << VFLIP) |
367 			(1 << FREQ),
368 
369 [SENSOR_OV7630] =	(1 << EXPOSURE) |
370 			(1 << GAIN) |
371 			(1 << HFLIP),
372 
373 [SENSOR_OV7648] =	(1 << EXPOSURE) |
374 			(1 << GAIN) |
375 			(1 << HFLIP),
376 
377 [SENSOR_OV7660] =	(1 << EXPOSURE) |
378 			(1 << AUTOGAIN) |
379 			(1 << GAIN) |
380 			(1 << HFLIP) |
381 			(1 << VFLIP),
382 
383 [SENSOR_PO1030] =	(1 << EXPOSURE) |
384 			(1 << AUTOGAIN) |
385 			(1 << GAIN) |
386 			(1 << HFLIP) |
387 			(1 << VFLIP) |
388 			(1 << FREQ),
389 
390 [SENSOR_PO2030N] =	(1 << FREQ),
391 
392 [SENSOR_SOI768] =	(1 << EXPOSURE) |
393 			(1 << AUTOGAIN) |
394 			(1 << GAIN) |
395 			(1 << HFLIP) |
396 			(1 << VFLIP) |
397 			(1 << FREQ),
398 
399 [SENSOR_SP80708] =	(1 << EXPOSURE) |
400 			(1 << AUTOGAIN) |
401 			(1 << GAIN) |
402 			(1 << HFLIP) |
403 			(1 << VFLIP) |
404 			(1 << FREQ),
405 };
406 
407 static const struct v4l2_pix_format cif_mode[] = {
408 	{352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
409 		.bytesperline = 352,
410 		.sizeimage = 352 * 288 * 4 / 8 + 590,
411 		.colorspace = V4L2_COLORSPACE_JPEG,
412 		.priv = 0},
413 };
414 static const struct v4l2_pix_format vga_mode[] = {
415 	{160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
416 		.bytesperline = 160,
417 		.sizeimage = 160 * 120 * 4 / 8 + 590,
418 		.colorspace = V4L2_COLORSPACE_JPEG,
419 		.priv = 2},
420 	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
421 		.bytesperline = 320,
422 		.sizeimage = 320 * 240 * 3 / 8 + 590,
423 		.colorspace = V4L2_COLORSPACE_JPEG,
424 		.priv = 1},
425 	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
426 		.bytesperline = 640,
427 		/* Note 3 / 8 is not large enough, not even 5 / 8 is ?! */
428 		.sizeimage = 640 * 480 * 3 / 4 + 590,
429 		.colorspace = V4L2_COLORSPACE_JPEG,
430 		.priv = 0},
431 };
432 
433 static const u8 sn_adcm1700[0x1c] = {
434 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
435 	0x00,	0x43,	0x60,	0x00,	0x1a,	0x00,	0x00,	0x00,
436 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
437 	0x80,	0x51,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
438 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
439 	0x03,	0x00,	0x05,	0x01,	0x05,	0x16,	0x12,	0x42,
440 /*	reg18	reg19	reg1a	reg1b */
441 	0x06,	0x00,	0x00,	0x00
442 };
443 
444 static const u8 sn_gc0307[0x1c] = {
445 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
446 	0x00,	0x61,	0x62,	0x00,	0x1a,	0x00,	0x00,	0x00,
447 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
448 	0x80,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
449 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
450 	0x03,	0x00,	0x03,	0x01,	0x08,	0x28,	0x1e,	0x02,
451 /*	reg18	reg19	reg1a	reg1b */
452 	0x06,	0x00,	0x00,	0x00
453 };
454 
455 static const u8 sn_hv7131[0x1c] = {
456 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
457 	0x00,	0x03,	0x60,	0x00,	0x1a,	0x20,	0x20,	0x20,
458 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
459 	0x81,	0x11,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
460 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
461 	0x03,	0x00,	0x00,	0x01,	0x03,	0x28,	0x1e,	0x41,
462 /*	reg18	reg19	reg1a	reg1b */
463 	0x0a,	0x00,	0x00,	0x00
464 };
465 
466 static const u8 sn_mi0360[0x1c] = {
467 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
468 	0x00,	0x63,	0x40,	0x00,	0x1a,	0x20,	0x20,	0x20,
469 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
470 	0x81,	0x5d,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
471 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
472 	0x03,	0x00,	0x00,	0x02,	0x0a,	0x28,	0x1e,	0x61,
473 /*	reg18	reg19	reg1a	reg1b */
474 	0x06,	0x00,	0x00,	0x00
475 };
476 
477 static const u8 sn_mi0360b[0x1c] = {
478 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
479 	0x00,	0x61,	0x40,	0x00,	0x1a,	0x00,	0x00,	0x00,
480 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
481 	0x81,	0x5d,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
482 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
483 	0x03,	0x00,	0x00,	0x02,	0x0a,	0x28,	0x1e,	0x40,
484 /*	reg18	reg19	reg1a	reg1b */
485 	0x06,	0x00,	0x00,	0x00
486 };
487 
488 static const u8 sn_mo4000[0x1c] = {
489 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
490 	0x00,	0x23,	0x60,	0x00,	0x1a,	0x00,	0x20,	0x18,
491 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
492 	0x81,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
493 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
494 	0x03,	 0x00,	0x0b,	0x0f,	0x14,	0x28,	0x1e,	0x40,
495 /*	reg18	reg19	reg1a	reg1b */
496 	0x08,	0x00,	0x00,	0x00
497 };
498 
499 static const u8 sn_mt9v111[0x1c] = {
500 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
501 	0x00,	0x61,	0x40,	0x00,	0x1a,	0x20,	0x20,	0x20,
502 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
503 	0x81,	0x5c,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
504 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
505 	0x03,	0x00,	0x00,	0x02,	0x1c,	0x28,	0x1e,	0x40,
506 /*	reg18	reg19	reg1a	reg1b */
507 	0x06,	0x00,	0x00,	0x00
508 };
509 
510 static const u8 sn_om6802[0x1c] = {
511 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
512 	0x00,	0x23,	0x72,	0x00,	0x1a,	0x20,	0x20,	0x19,
513 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
514 	0x80,	0x34,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
515 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
516 	0x03,	0x00,	0x51,	0x01,	0x00,	0x28,	0x1e,	0x40,
517 /*	reg18	reg19	reg1a	reg1b */
518 	0x05,	0x00,	0x00,	0x00
519 };
520 
521 static const u8 sn_ov7630[0x1c] = {
522 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
523 	0x00,	0x21,	0x40,	0x00,	0x1a,	0x00,	0x00,	0x00,
524 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
525 	0x81,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
526 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
527 	0x03,	0x00,	0x04,	0x01,	0x0a,	0x28,	0x1e,	0xc2,
528 /*	reg18	reg19	reg1a	reg1b */
529 	0x0b,	0x00,	0x00,	0x00
530 };
531 
532 static const u8 sn_ov7648[0x1c] = {
533 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
534 	0x00,	0x63,	0x40,	0x00,	0x1a,	0x20,	0x20,	0x20,
535 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
536 	0x81,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
537 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
538 	0x03,	0x00,	0x00,	0x01,	0x00,	0x28,	0x1e,	0x00,
539 /*	reg18	reg19	reg1a	reg1b */
540 	0x0b,	0x00,	0x00,	0x00
541 };
542 
543 static const u8 sn_ov7660[0x1c] = {
544 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
545 	0x00,	0x61,	0x40,	0x00,	0x1a,	0x00,	0x00,	0x00,
546 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
547 	0x81,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
548 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
549 	0x03,	0x00,	0x01,	0x01,	0x08,	0x28,	0x1e,	0x20,
550 /*	reg18	reg19	reg1a	reg1b */
551 	0x07,	0x00,	0x00,	0x00
552 };
553 
554 static const u8 sn_po1030[0x1c] = {
555 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
556 	0x00,	0x21,	0x62,	0x00,	0x1a,	0x20,	0x20,	0x20,
557 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
558 	0x81,	0x6e,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
559 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
560 	0x03,	0x00,	0x00,	0x06,	0x06,	0x28,	0x1e,	0x00,
561 /*	reg18	reg19	reg1a	reg1b */
562 	0x07,	0x00,	0x00,	0x00
563 };
564 
565 static const u8 sn_po2030n[0x1c] = {
566 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
567 	0x00,	0x63,	0x40,	0x00,	0x1a,	0x00,	0x00,	0x00,
568 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
569 	0x81,	0x6e,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
570 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
571 	0x03,	0x00,	0x00,	0x01,	0x14,	0x28,	0x1e,	0x00,
572 /*	reg18	reg19	reg1a	reg1b */
573 	0x07,	0x00,	0x00,	0x00
574 };
575 
576 static const u8 sn_soi768[0x1c] = {
577 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
578 	0x00,	0x21,	0x40,	0x00,	0x1a,	0x00,	0x00,	0x00,
579 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
580 	0x81,	0x21,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
581 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
582 	0x03,	0x00,	0x00,	0x01,	0x08,	0x28,	0x1e,	0x00,
583 /*	reg18	reg19	reg1a	reg1b */
584 	0x07,	0x00,	0x00,	0x00
585 };
586 
587 static const u8 sn_sp80708[0x1c] = {
588 /*	reg0	reg1	reg2	reg3	reg4	reg5	reg6	reg7 */
589 	0x00,	0x63,	0x60,	0x00,	0x1a,	0x20,	0x20,	0x20,
590 /*	reg8	reg9	rega	regb	regc	regd	rege	regf */
591 	0x81,	0x18,	0x00,	0x00,	0x00,	0x00,	0x00,	0x00,
592 /*	reg10	reg11	reg12	reg13	reg14	reg15	reg16	reg17 */
593 	0x03,	0x00,	0x00,	0x03,	0x04,	0x28,	0x1e,	0x00,
594 /*	reg18	reg19	reg1a	reg1b */
595 	0x07,	0x00,	0x00,	0x00
596 };
597 
598 /* sequence specific to the sensors - !! index = SENSOR_xxx */
599 static const u8 *sn_tb[] = {
600 [SENSOR_ADCM1700] =	sn_adcm1700,
601 [SENSOR_GC0307] =	sn_gc0307,
602 [SENSOR_HV7131R] =	sn_hv7131,
603 [SENSOR_MI0360] =	sn_mi0360,
604 [SENSOR_MI0360B] =	sn_mi0360b,
605 [SENSOR_MO4000] =	sn_mo4000,
606 [SENSOR_MT9V111] =	sn_mt9v111,
607 [SENSOR_OM6802] =	sn_om6802,
608 [SENSOR_OV7630] =	sn_ov7630,
609 [SENSOR_OV7648] =	sn_ov7648,
610 [SENSOR_OV7660] =	sn_ov7660,
611 [SENSOR_PO1030] =	sn_po1030,
612 [SENSOR_PO2030N] =	sn_po2030n,
613 [SENSOR_SOI768] =	sn_soi768,
614 [SENSOR_SP80708] =	sn_sp80708,
615 };
616 
617 /* default gamma table */
618 static const u8 gamma_def[17] = {
619 	0x00, 0x2d, 0x46, 0x5a, 0x6c, 0x7c, 0x8b, 0x99,
620 	0xa6, 0xb2, 0xbf, 0xca, 0xd5, 0xe0, 0xeb, 0xf5, 0xff
621 };
622 /* gamma for sensor ADCM1700 */
623 static const u8 gamma_spec_0[17] = {
624 	0x0f, 0x39, 0x5a, 0x74, 0x86, 0x95, 0xa6, 0xb4,
625 	0xbd, 0xc4, 0xcc, 0xd4, 0xd5, 0xde, 0xe4, 0xed, 0xf5
626 };
627 /* gamma for sensors HV7131R and MT9V111 */
628 static const u8 gamma_spec_1[17] = {
629 	0x08, 0x3a, 0x52, 0x65, 0x75, 0x83, 0x91, 0x9d,
630 	0xa9, 0xb4, 0xbe, 0xc8, 0xd2, 0xdb, 0xe4, 0xed, 0xf5
631 };
632 /* gamma for sensor GC0307 */
633 static const u8 gamma_spec_2[17] = {
634 	0x14, 0x37, 0x50, 0x6a, 0x7c, 0x8d, 0x9d, 0xab,
635 	0xb5, 0xbf, 0xc2, 0xcb, 0xd1, 0xd6, 0xdb, 0xe1, 0xeb
636 };
637 /* gamma for sensor SP80708 */
638 static const u8 gamma_spec_3[17] = {
639 	0x0a, 0x2d, 0x4e, 0x68, 0x7d, 0x8f, 0x9f, 0xab,
640 	0xb7, 0xc2, 0xcc, 0xd3, 0xd8, 0xde, 0xe2, 0xe5, 0xe6
641 };
642 
643 /* color matrix and offsets */
644 static const u8 reg84[] = {
645 	0x14, 0x00, 0x27, 0x00, 0x07, 0x00,	/* YR YG YB gains */
646 	0xe8, 0x0f, 0xda, 0x0f, 0x40, 0x00,	/* UR UG UB */
647 	0x3e, 0x00, 0xcd, 0x0f, 0xf7, 0x0f,	/* VR VG VB */
648 	0x00, 0x00, 0x00			/* YUV offsets */
649 };
650 
651 #define DELAY	0xdd
652 
653 static const u8 adcm1700_sensor_init[][8] = {
654 	{0xa0, 0x51, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x10},
655 	{0xb0, 0x51, 0x04, 0x08, 0x00, 0x00, 0x00, 0x10},	/* reset */
656 	{DELAY, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
657 	{0xb0, 0x51, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10},
658 	{DELAY, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
659 	{0xb0, 0x51, 0x0c, 0xe0, 0x2e, 0x00, 0x00, 0x10},
660 	{0xb0, 0x51, 0x10, 0x02, 0x02, 0x00, 0x00, 0x10},
661 	{0xb0, 0x51, 0x14, 0x0e, 0x0e, 0x00, 0x00, 0x10},
662 	{0xb0, 0x51, 0x1c, 0x00, 0x80, 0x00, 0x00, 0x10},
663 	{0xb0, 0x51, 0x20, 0x01, 0x00, 0x00, 0x00, 0x10},
664 	{DELAY, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
665 	{0xb0, 0x51, 0x04, 0x04, 0x00, 0x00, 0x00, 0x10},
666 	{DELAY, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
667 	{0xb0, 0x51, 0x04, 0x01, 0x00, 0x00, 0x00, 0x10},
668 	{0xa0, 0x51, 0xfe, 0x10, 0x00, 0x00, 0x00, 0x10},
669 	{0xb0, 0x51, 0x14, 0x01, 0x00, 0x00, 0x00, 0x10},
670 	{0xb0, 0x51, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10},
671 	{}
672 };
673 static const u8 adcm1700_sensor_param1[][8] = {
674 	{0xb0, 0x51, 0x26, 0xf9, 0x01, 0x00, 0x00, 0x10},	/* exposure? */
675 	{0xd0, 0x51, 0x1e, 0x8e, 0x8e, 0x8e, 0x8e, 0x10},
676 
677 	{0xa0, 0x51, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x10},
678 	{0xb0, 0x51, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10},
679 	{0xa0, 0x51, 0xfe, 0x10, 0x00, 0x00, 0x00, 0x10},
680 	{0xb0, 0x51, 0x32, 0x00, 0x72, 0x00, 0x00, 0x10},
681 	{0xd0, 0x51, 0x1e, 0xbe, 0xd7, 0xe8, 0xbe, 0x10},	/* exposure? */
682 
683 	{0xa0, 0x51, 0xfe, 0x01, 0x00, 0x00, 0x00, 0x10},
684 	{0xb0, 0x51, 0x00, 0x02, 0x00, 0x00, 0x00, 0x10},
685 	{0xa0, 0x51, 0xfe, 0x10, 0x00, 0x00, 0x00, 0x10},
686 	{0xb0, 0x51, 0x32, 0x00, 0xa2, 0x00, 0x00, 0x10},
687 	{}
688 };
689 static const u8 gc0307_sensor_init[][8] = {
690 	{0xa0, 0x21, 0x43, 0x00, 0x00, 0x00, 0x00, 0x10},
691 	{0xa0, 0x21, 0x44, 0xa2, 0x00, 0x00, 0x00, 0x10},
692 	{0xa0, 0x21, 0x01, 0x6a, 0x00, 0x00, 0x00, 0x10},
693 	{0xa0, 0x21, 0x02, 0x70, 0x00, 0x00, 0x00, 0x10},
694 	{0xa0, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
695 	{0xa0, 0x21, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x10},
696 	{0xa0, 0x21, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x10},
697 	{0xa0, 0x21, 0x11, 0x05, 0x00, 0x00, 0x00, 0x10},
698 	{0xa0, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10},
699 	{0xa0, 0x21, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10},
700 	{0xa0, 0x21, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10},
701 	{0xa0, 0x21, 0x08, 0x02, 0x00, 0x00, 0x00, 0x10},
702 	{0xa0, 0x21, 0x09, 0x01, 0x00, 0x00, 0x00, 0x10},
703 	{0xa0, 0x21, 0x0a, 0xe8, 0x00, 0x00, 0x00, 0x10},
704 	{0xa0, 0x21, 0x0b, 0x02, 0x00, 0x00, 0x00, 0x10},
705 	{0xa0, 0x21, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x10},
706 	{0xa0, 0x21, 0x0d, 0x22, 0x00, 0x00, 0x00, 0x10},
707 	{0xa0, 0x21, 0x0e, 0x02, 0x00, 0x00, 0x00, 0x10},
708 	{0xa0, 0x21, 0x0f, 0xb2, 0x00, 0x00, 0x00, 0x10},
709 	{0xa0, 0x21, 0x12, 0x70, 0x00, 0x00, 0x00, 0x10},
710 	{DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 10ms*/
711 	{0xa0, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10},
712 	{0xa0, 0x21, 0x15, 0xb8, 0x00, 0x00, 0x00, 0x10},
713 	{0xa0, 0x21, 0x16, 0x13, 0x00, 0x00, 0x00, 0x10},
714 	{0xa0, 0x21, 0x17, 0x52, 0x00, 0x00, 0x00, 0x10},
715 	{0xa0, 0x21, 0x18, 0x50, 0x00, 0x00, 0x00, 0x10},
716 	{0xa0, 0x21, 0x1e, 0x0d, 0x00, 0x00, 0x00, 0x10},
717 	{0xa0, 0x21, 0x1f, 0x32, 0x00, 0x00, 0x00, 0x10},
718 	{0xa0, 0x21, 0x61, 0x90, 0x00, 0x00, 0x00, 0x10},
719 	{0xa0, 0x21, 0x63, 0x70, 0x00, 0x00, 0x00, 0x10},
720 	{0xa0, 0x21, 0x65, 0x98, 0x00, 0x00, 0x00, 0x10},
721 	{0xa0, 0x21, 0x67, 0x90, 0x00, 0x00, 0x00, 0x10},
722 	{0xa0, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
723 	{0xa0, 0x21, 0x04, 0x96, 0x00, 0x00, 0x00, 0x10},
724 	{0xa0, 0x21, 0x45, 0x27, 0x00, 0x00, 0x00, 0x10},
725 	{0xa0, 0x21, 0x47, 0x2c, 0x00, 0x00, 0x00, 0x10},
726 	{0xa0, 0x21, 0x43, 0x47, 0x00, 0x00, 0x00, 0x10},
727 	{0xa0, 0x21, 0x44, 0xd8, 0x00, 0x00, 0x00, 0x10},
728 	{}
729 };
730 static const u8 gc0307_sensor_param1[][8] = {
731 	{0xa0, 0x21, 0x68, 0x13, 0x00, 0x00, 0x00, 0x10},
732 	{0xd0, 0x21, 0x61, 0x80, 0x00, 0x80, 0x00, 0x10},
733 	{0xc0, 0x21, 0x65, 0x80, 0x00, 0x80, 0x00, 0x10},
734 	{0xc0, 0x21, 0x63, 0xa0, 0x00, 0xa6, 0x00, 0x10},
735 /*param3*/
736 	{0xa0, 0x21, 0x01, 0x6e, 0x00, 0x00, 0x00, 0x10},
737 	{0xa0, 0x21, 0x02, 0x88, 0x00, 0x00, 0x00, 0x10},
738 	{}
739 };
740 
741 static const u8 hv7131r_sensor_init[][8] = {
742 	{0xc1, 0x11, 0x01, 0x08, 0x01, 0x00, 0x00, 0x10},
743 	{0xb1, 0x11, 0x34, 0x17, 0x7f, 0x00, 0x00, 0x10},
744 	{0xd1, 0x11, 0x40, 0xff, 0x7f, 0x7f, 0x7f, 0x10},
745 /*	{0x91, 0x11, 0x44, 0x00, 0x00, 0x00, 0x00, 0x10}, */
746 	{0xd1, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
747 	{0xd1, 0x11, 0x14, 0x01, 0xe2, 0x02, 0x82, 0x10},
748 /*	{0x91, 0x11, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10}, */
749 
750 	{0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
751 	{0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
752 	{0xc1, 0x11, 0x25, 0x00, 0x61, 0xa8, 0x00, 0x10},
753 	{0xa1, 0x11, 0x30, 0x22, 0x00, 0x00, 0x00, 0x10},
754 	{0xc1, 0x11, 0x31, 0x20, 0x2e, 0x20, 0x00, 0x10},
755 	{0xc1, 0x11, 0x25, 0x00, 0xc3, 0x50, 0x00, 0x10},
756 	{0xa1, 0x11, 0x30, 0x07, 0x00, 0x00, 0x00, 0x10}, /* gain14 */
757 	{0xc1, 0x11, 0x31, 0x10, 0x10, 0x10, 0x00, 0x10}, /* r g b 101a10 */
758 
759 	{0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
760 	{0xa1, 0x11, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
761 	{0xa1, 0x11, 0x21, 0xd0, 0x00, 0x00, 0x00, 0x10},
762 	{0xa1, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
763 	{0xa1, 0x11, 0x23, 0x09, 0x00, 0x00, 0x00, 0x10},
764 
765 	{0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
766 	{0xa1, 0x11, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
767 	{0xa1, 0x11, 0x21, 0xd0, 0x00, 0x00, 0x00, 0x10},
768 	{0xa1, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
769 	{0xa1, 0x11, 0x23, 0x10, 0x00, 0x00, 0x00, 0x10},
770 	{0xa1, 0x11, 0x01, 0x18, 0x00, 0x00, 0x00, 0x10},
771 							/* set sensor clock */
772 	{}
773 };
774 static const u8 mi0360_sensor_init[][8] = {
775 	{0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
776 	{0xb1, 0x5d, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10},
777 	{0xb1, 0x5d, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
778 	{0xd1, 0x5d, 0x01, 0x00, 0x08, 0x00, 0x16, 0x10},
779 	{0xd1, 0x5d, 0x03, 0x01, 0xe2, 0x02, 0x82, 0x10},
780 	{0xd1, 0x5d, 0x05, 0x00, 0x09, 0x00, 0x53, 0x10},
781 	{0xb1, 0x5d, 0x0d, 0x00, 0x02, 0x00, 0x00, 0x10},
782 	{0xd1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
783 	{0xd1, 0x5d, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10},
784 	{0xd1, 0x5d, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
785 	{0xd1, 0x5d, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
786 	{0xd1, 0x5d, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
787 	{0xd1, 0x5d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x10},
788 	{0xd1, 0x5d, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10},
789 	{0xd1, 0x5d, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10},
790 	{0xd1, 0x5d, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x10},
791 	{0xd1, 0x5d, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x10},
792 	{0xb1, 0x5d, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10},
793 	{0xd1, 0x5d, 0x20, 0x91, 0x01, 0x00, 0x00, 0x10},
794 	{0xd1, 0x5d, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
795 	{0xd1, 0x5d, 0x24, 0x00, 0x00, 0x00, 0x00, 0x10},
796 	{0xd1, 0x5d, 0x26, 0x00, 0x00, 0x00, 0x24, 0x10},
797 	{0xd1, 0x5d, 0x2f, 0xf7, 0xb0, 0x00, 0x04, 0x10},
798 	{0xd1, 0x5d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
799 	{0xd1, 0x5d, 0x33, 0x00, 0x00, 0x01, 0x00, 0x10},
800 	{0xb1, 0x5d, 0x3d, 0x06, 0x8f, 0x00, 0x00, 0x10},
801 	{0xd1, 0x5d, 0x40, 0x01, 0xe0, 0x00, 0xd1, 0x10},
802 	{0xb1, 0x5d, 0x44, 0x00, 0x82, 0x00, 0x00, 0x10},
803 	{0xd1, 0x5d, 0x58, 0x00, 0x78, 0x00, 0x43, 0x10},
804 	{0xd1, 0x5d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x10},
805 	{0xd1, 0x5d, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x10},
806 	{0xd1, 0x5d, 0x5e, 0x00, 0x00, 0xa3, 0x1d, 0x10},
807 	{0xb1, 0x5d, 0x62, 0x04, 0x11, 0x00, 0x00, 0x10},
808 
809 	{0xb1, 0x5d, 0x20, 0x91, 0x01, 0x00, 0x00, 0x10},
810 	{0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
811 	{0xb1, 0x5d, 0x09, 0x00, 0x64, 0x00, 0x00, 0x10},
812 	{0xd1, 0x5d, 0x2b, 0x00, 0xa0, 0x00, 0xb0, 0x10},
813 	{0xd1, 0x5d, 0x2d, 0x00, 0xa0, 0x00, 0xa0, 0x10},
814 
815 	{0xb1, 0x5d, 0x0a, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor clck ?2 */
816 	{0xb1, 0x5d, 0x06, 0x00, 0x30, 0x00, 0x00, 0x10},
817 	{0xb1, 0x5d, 0x05, 0x00, 0x0a, 0x00, 0x00, 0x10},
818 	{0xb1, 0x5d, 0x09, 0x02, 0x35, 0x00, 0x00, 0x10}, /* exposure 2 */
819 
820 	{0xd1, 0x5d, 0x2b, 0x00, 0xb9, 0x00, 0xe3, 0x10},
821 	{0xd1, 0x5d, 0x2d, 0x00, 0x5f, 0x00, 0xb9, 0x10}, /* 42 */
822 /*	{0xb1, 0x5d, 0x35, 0x00, 0x67, 0x00, 0x00, 0x10}, * gain orig */
823 /*	{0xb1, 0x5d, 0x35, 0x00, 0x20, 0x00, 0x00, 0x10}, * gain */
824 	{0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10}, /* update */
825 	{0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor on */
826 	{}
827 };
828 static const u8 mi0360b_sensor_init[][8] = {
829 	{0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
830 	{0xb1, 0x5d, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10},
831 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 20ms*/
832 	{0xb1, 0x5d, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
833 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 20ms*/
834 	{0xd1, 0x5d, 0x01, 0x00, 0x08, 0x00, 0x16, 0x10},
835 	{0xd1, 0x5d, 0x03, 0x01, 0xe2, 0x02, 0x82, 0x10},
836 	{0xd1, 0x5d, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10},
837 	{0xb1, 0x5d, 0x0d, 0x00, 0x02, 0x00, 0x00, 0x10},
838 	{0xd1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
839 	{0xd1, 0x5d, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10},
840 	{0xd1, 0x5d, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
841 	{0xd1, 0x5d, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
842 	{0xd1, 0x5d, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
843 	{0xd1, 0x5d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x10},
844 	{0xd1, 0x5d, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10},
845 	{0xd1, 0x5d, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10},
846 	{0xd1, 0x5d, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x10},
847 	{0xd1, 0x5d, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x10},
848 	{0xb1, 0x5d, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10},
849 	{0xd1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
850 	{0xd1, 0x5d, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
851 	{0xd1, 0x5d, 0x24, 0x00, 0x00, 0x00, 0x00, 0x10},
852 	{0xd1, 0x5d, 0x26, 0x00, 0x00, 0x00, 0x24, 0x10},
853 	{0xd1, 0x5d, 0x2f, 0xf7, 0xb0, 0x00, 0x04, 0x10},
854 	{0xd1, 0x5d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
855 	{0xd1, 0x5d, 0x33, 0x00, 0x00, 0x01, 0x00, 0x10},
856 	{0xb1, 0x5d, 0x3d, 0x06, 0x8f, 0x00, 0x00, 0x10},
857 	{0xd1, 0x5d, 0x40, 0x01, 0xe0, 0x00, 0xd1, 0x10},
858 	{0xb1, 0x5d, 0x44, 0x00, 0x82, 0x00, 0x00, 0x10},
859 	{0xd1, 0x5d, 0x58, 0x00, 0x78, 0x00, 0x43, 0x10},
860 	{0xd1, 0x5d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x10},
861 	{0xd1, 0x5d, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x10},
862 	{0xd1, 0x5d, 0x5e, 0x00, 0x00, 0xa3, 0x1d, 0x10},
863 	{0xb1, 0x5d, 0x62, 0x04, 0x11, 0x00, 0x00, 0x10},
864 
865 	{0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
866 	{0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
867 	{0xb1, 0x5d, 0x09, 0x00, 0x64, 0x00, 0x00, 0x10},
868 	{0xd1, 0x5d, 0x2b, 0x00, 0x33, 0x00, 0xa0, 0x10},
869 	{0xd1, 0x5d, 0x2d, 0x00, 0xa0, 0x00, 0x33, 0x10},
870 	{}
871 };
872 static const u8 mi0360b_sensor_param1[][8] = {
873 	{0xb1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
874 	{0xb1, 0x5d, 0x06, 0x00, 0x53, 0x00, 0x00, 0x10},
875 	{0xb1, 0x5d, 0x05, 0x00, 0x09, 0x00, 0x00, 0x10},
876 	{0xb1, 0x5d, 0x09, 0x02, 0x35, 0x00, 0x00, 0x10}, /* exposure 2 */
877 
878 	{0xd1, 0x5d, 0x2b, 0x00, 0xd1, 0x01, 0xc9, 0x10},
879 	{0xd1, 0x5d, 0x2d, 0x00, 0xed, 0x00, 0xd1, 0x10},
880 	{0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10}, /* update */
881 	{0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor on */
882 	{}
883 };
884 static const u8 mo4000_sensor_init[][8] = {
885 	{0xa1, 0x21, 0x01, 0x02, 0x00, 0x00, 0x00, 0x10},
886 	{0xa1, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10},
887 	{0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
888 	{0xa1, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10},
889 	{0xa1, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10},
890 	{0xa1, 0x21, 0x05, 0x04, 0x00, 0x00, 0x00, 0x10},
891 	{0xa1, 0x21, 0x06, 0x80, 0x00, 0x00, 0x00, 0x10},
892 	{0xa1, 0x21, 0x06, 0x81, 0x00, 0x00, 0x00, 0x10},
893 	{0xa1, 0x21, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
894 	{0xa1, 0x21, 0x11, 0x00, 0x00, 0x00, 0x00, 0x10},
895 	{0xa1, 0x21, 0x11, 0x20, 0x00, 0x00, 0x00, 0x10},
896 	{0xa1, 0x21, 0x11, 0x30, 0x00, 0x00, 0x00, 0x10},
897 	{0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
898 	{0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
899 	{0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
900 	{0xa1, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
901 	{0xa1, 0x21, 0x0f, 0x20, 0x00, 0x00, 0x00, 0x10},
902 	{0xa1, 0x21, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10},
903 	{0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
904 	{0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
905 	{}
906 };
907 static const u8 mt9v111_sensor_init[][8] = {
908 	{0xb1, 0x5c, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10}, /* reset? */
909 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
910 	{0xb1, 0x5c, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
911 	{0xb1, 0x5c, 0x01, 0x00, 0x01, 0x00, 0x00, 0x10}, /* IFP select */
912 	{0xb1, 0x5c, 0x08, 0x04, 0x80, 0x00, 0x00, 0x10}, /* output fmt ctrl */
913 	{0xb1, 0x5c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10}, /* op mode ctrl */
914 	{0xb1, 0x5c, 0x01, 0x00, 0x04, 0x00, 0x00, 0x10}, /* sensor select */
915 	{0xb1, 0x5c, 0x08, 0x00, 0x08, 0x00, 0x00, 0x10}, /* row start */
916 	{0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10}, /* col start */
917 	{0xb1, 0x5c, 0x03, 0x01, 0xe7, 0x00, 0x00, 0x10}, /* window height */
918 	{0xb1, 0x5c, 0x04, 0x02, 0x87, 0x00, 0x00, 0x10}, /* window width */
919 	{0xb1, 0x5c, 0x07, 0x30, 0x02, 0x00, 0x00, 0x10}, /* output ctrl */
920 	{0xb1, 0x5c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10}, /* shutter delay */
921 	{0xb1, 0x5c, 0x12, 0x00, 0xb0, 0x00, 0x00, 0x10}, /* zoom col start */
922 	{0xb1, 0x5c, 0x13, 0x00, 0x7c, 0x00, 0x00, 0x10}, /* zoom row start */
923 	{0xb1, 0x5c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x10}, /* digital zoom */
924 	{0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10}, /* read mode */
925 	{0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
926 	{}
927 };
928 static const u8 mt9v111_sensor_param1[][8] = {
929 	{0xd1, 0x5c, 0x2b, 0x00, 0x33, 0x00, 0xad, 0x10}, /* G1 and B gains */
930 	{0xd1, 0x5c, 0x2d, 0x00, 0xad, 0x00, 0x33, 0x10}, /* R and G2 gains */
931 	{0xb1, 0x5c, 0x06, 0x00, 0x40, 0x00, 0x00, 0x10}, /* vert blanking */
932 	{0xb1, 0x5c, 0x05, 0x00, 0x09, 0x00, 0x00, 0x10}, /* horiz blanking */
933 	{0xb1, 0x5c, 0x35, 0x01, 0xc0, 0x00, 0x00, 0x10}, /* global gain */
934 	{}
935 };
936 static const u8 om6802_init0[2][8] = {
937 /*fixme: variable*/
938 	{0xa0, 0x34, 0x29, 0x0e, 0x00, 0x00, 0x00, 0x10},
939 	{0xa0, 0x34, 0x23, 0xb0, 0x00, 0x00, 0x00, 0x10},
940 };
941 static const u8 om6802_sensor_init[][8] = {
942 	{0xa0, 0x34, 0xdf, 0x6d, 0x00, 0x00, 0x00, 0x10},
943 						/* factory mode */
944 	{0xa0, 0x34, 0xdd, 0x18, 0x00, 0x00, 0x00, 0x10},
945 						/* output raw RGB */
946 	{0xa0, 0x34, 0x5a, 0xc0, 0x00, 0x00, 0x00, 0x10},
947 /*	{0xa0, 0x34, 0xfb, 0x11, 0x00, 0x00, 0x00, 0x10}, */
948 	{0xa0, 0x34, 0xf0, 0x04, 0x00, 0x00, 0x00, 0x10},
949 		/* auto-exposure speed (0) / white balance mode (auto RGB) */
950 /*	{0xa0, 0x34, 0xf1, 0x02, 0x00, 0x00, 0x00, 0x10},
951 							 * set color mode */
952 /*	{0xa0, 0x34, 0xfe, 0x5b, 0x00, 0x00, 0x00, 0x10},
953 						 * max AGC value in AE */
954 /*	{0xa0, 0x34, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x10},
955 							 * preset AGC */
956 /*	{0xa0, 0x34, 0xe6, 0x00, 0x00, 0x00, 0x00, 0x10},
957 						 * preset brightness */
958 /*	{0xa0, 0x34, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x10},
959 							 * preset contrast */
960 /*	{0xa0, 0x34, 0xe8, 0x31, 0x00, 0x00, 0x00, 0x10},
961 							 * preset gamma */
962 	{0xa0, 0x34, 0xe9, 0x0f, 0x00, 0x00, 0x00, 0x10},
963 				/* luminance mode (0x4f -> AutoExpo on) */
964 	{0xa0, 0x34, 0xe4, 0xff, 0x00, 0x00, 0x00, 0x10},
965 							/* preset shutter */
966 /*	{0xa0, 0x34, 0xef, 0x00, 0x00, 0x00, 0x00, 0x10},
967 							 * auto frame rate */
968 /*	{0xa0, 0x34, 0xfb, 0xee, 0x00, 0x00, 0x00, 0x10}, */
969 	{0xa0, 0x34, 0x5d, 0x80, 0x00, 0x00, 0x00, 0x10},
970 	{}
971 };
972 static const u8 om6802_sensor_param1[][8] = {
973 	{0xa0, 0x34, 0x71, 0x84, 0x00, 0x00, 0x00, 0x10},
974 	{0xa0, 0x34, 0x72, 0x05, 0x00, 0x00, 0x00, 0x10},
975 	{0xa0, 0x34, 0x68, 0x80, 0x00, 0x00, 0x00, 0x10},
976 	{0xa0, 0x34, 0x69, 0x01, 0x00, 0x00, 0x00, 0x10},
977 	{}
978 };
979 static const u8 ov7630_sensor_init[][8] = {
980 	{0xa1, 0x21, 0x76, 0x01, 0x00, 0x00, 0x00, 0x10},
981 	{0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
982 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
983 	{0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
984 	{0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
985 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
986 	{0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
987 /* win: i2c_r from 00 to 80 */
988 	{0xd1, 0x21, 0x03, 0x80, 0x10, 0x20, 0x80, 0x10},
989 	{0xb1, 0x21, 0x0c, 0x20, 0x20, 0x00, 0x00, 0x10},
990 /* HDG: 0x11 was 0x00 change to 0x01 for better exposure (15 fps instead of 30)
991 	0x13 was 0xc0 change to 0xc3 for auto gain and exposure */
992 	{0xd1, 0x21, 0x11, 0x01, 0x48, 0xc3, 0x00, 0x10},
993 	{0xb1, 0x21, 0x15, 0x80, 0x03, 0x00, 0x00, 0x10},
994 	{0xd1, 0x21, 0x17, 0x1b, 0xbd, 0x05, 0xf6, 0x10},
995 	{0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
996 	{0xd1, 0x21, 0x1f, 0x00, 0x80, 0x80, 0x80, 0x10},
997 	{0xd1, 0x21, 0x23, 0xde, 0x10, 0x8a, 0xa0, 0x10},
998 	{0xc1, 0x21, 0x27, 0xca, 0xa2, 0x74, 0x00, 0x10},
999 	{0xd1, 0x21, 0x2a, 0x88, 0x00, 0x88, 0x01, 0x10},
1000 	{0xc1, 0x21, 0x2e, 0x80, 0x00, 0x18, 0x00, 0x10},
1001 	{0xa1, 0x21, 0x21, 0x08, 0x00, 0x00, 0x00, 0x10},
1002 	{0xa1, 0x21, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
1003 	{0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10},
1004 	{0xb1, 0x21, 0x32, 0xc2, 0x08, 0x00, 0x00, 0x10},
1005 	{0xb1, 0x21, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x10},
1006 	{0xd1, 0x21, 0x60, 0x05, 0x40, 0x12, 0x57, 0x10},
1007 	{0xa1, 0x21, 0x64, 0x73, 0x00, 0x00, 0x00, 0x10},
1008 	{0xd1, 0x21, 0x65, 0x00, 0x55, 0x01, 0xac, 0x10},
1009 	{0xa1, 0x21, 0x69, 0x38, 0x00, 0x00, 0x00, 0x10},
1010 	{0xd1, 0x21, 0x6f, 0x1f, 0x01, 0x00, 0x10, 0x10},
1011 	{0xd1, 0x21, 0x73, 0x50, 0x20, 0x02, 0x01, 0x10},
1012 	{0xd1, 0x21, 0x77, 0xf3, 0x90, 0x98, 0x98, 0x10},
1013 	{0xc1, 0x21, 0x7b, 0x00, 0x4c, 0xf7, 0x00, 0x10},
1014 	{0xd1, 0x21, 0x17, 0x1b, 0xbd, 0x05, 0xf6, 0x10},
1015 	{0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
1016 	{}
1017 };
1018 static const u8 ov7630_sensor_param1[][8] = {
1019 	{0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
1020 	{0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
1021 /*fixme: + 0x12, 0x04*/
1022 /*	{0xa1, 0x21, 0x75, 0x82, 0x00, 0x00, 0x00, 0x10},  * COMN
1023 							 * set by setvflip */
1024 	{0xa1, 0x21, 0x10, 0x32, 0x00, 0x00, 0x00, 0x10},
1025 	{0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1026 	{0xb1, 0x21, 0x01, 0x80, 0x80, 0x00, 0x00, 0x10},
1027 /* */
1028 /*	{0xa1, 0x21, 0x2a, 0x88, 0x00, 0x00, 0x00, 0x10}, * set by setfreq */
1029 /*	{0xa1, 0x21, 0x2b, 0x34, 0x00, 0x00, 0x00, 0x10}, * set by setfreq */
1030 /* */
1031 	{0xa1, 0x21, 0x10, 0x83, 0x00, 0x00, 0x00, 0x10},
1032 /*	{0xb1, 0x21, 0x01, 0x88, 0x70, 0x00, 0x00, 0x10}, */
1033 	{}
1034 };
1035 
1036 static const u8 ov7648_sensor_init[][8] = {
1037 	{0xa1, 0x21, 0x76, 0x00, 0x00, 0x00, 0x00, 0x10},
1038 	{0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},	/* reset */
1039 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
1040 	{0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
1041 	{0xd1, 0x21, 0x03, 0xa4, 0x30, 0x88, 0x00, 0x10},
1042 	{0xb1, 0x21, 0x11, 0x80, 0x08, 0x00, 0x00, 0x10},
1043 	{0xc1, 0x21, 0x13, 0xa0, 0x04, 0x84, 0x00, 0x10},
1044 	{0xd1, 0x21, 0x17, 0x1a, 0x02, 0xba, 0xf4, 0x10},
1045 	{0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
1046 	{0xd1, 0x21, 0x1f, 0x41, 0xc0, 0x80, 0x80, 0x10},
1047 	{0xd1, 0x21, 0x23, 0xde, 0xa0, 0x80, 0x32, 0x10},
1048 	{0xd1, 0x21, 0x27, 0xfe, 0xa0, 0x00, 0x91, 0x10},
1049 	{0xd1, 0x21, 0x2b, 0x00, 0x88, 0x85, 0x80, 0x10},
1050 	{0xc1, 0x21, 0x2f, 0x9c, 0x00, 0xc4, 0x00, 0x10},
1051 	{0xd1, 0x21, 0x60, 0xa6, 0x60, 0x88, 0x12, 0x10},
1052 	{0xd1, 0x21, 0x64, 0x88, 0x00, 0x00, 0x94, 0x10},
1053 	{0xd1, 0x21, 0x68, 0x7a, 0x0c, 0x00, 0x00, 0x10},
1054 	{0xd1, 0x21, 0x6c, 0x11, 0x33, 0x22, 0x00, 0x10},
1055 	{0xd1, 0x21, 0x70, 0x11, 0x00, 0x10, 0x50, 0x10},
1056 	{0xd1, 0x21, 0x74, 0x20, 0x06, 0x00, 0xb5, 0x10},
1057 	{0xd1, 0x21, 0x78, 0x8a, 0x00, 0x00, 0x00, 0x10},
1058 	{0xb1, 0x21, 0x7c, 0x00, 0x43, 0x00, 0x00, 0x10},
1059 
1060 	{0xd1, 0x21, 0x21, 0x86, 0x00, 0xde, 0xa0, 0x10},
1061 /*	{0xd1, 0x21, 0x25, 0x80, 0x32, 0xfe, 0xa0, 0x10}, jfm done */
1062 /*	{0xd1, 0x21, 0x29, 0x00, 0x91, 0x00, 0x88, 0x10}, jfm done */
1063 /*	{0xb1, 0x21, 0x2d, 0x85, 0x00, 0x00, 0x00, 0x10}, set by setfreq */
1064 	{}
1065 };
1066 static const u8 ov7648_sensor_param1[][8] = {
1067 /*	{0xa1, 0x21, 0x12, 0x08, 0x00, 0x00, 0x00, 0x10}, jfm done */
1068 /*	{0xa1, 0x21, 0x75, 0x06, 0x00, 0x00, 0x00, 0x10},   * COMN
1069 							 * set by setvflip */
1070 	{0xa1, 0x21, 0x19, 0x02, 0x00, 0x00, 0x00, 0x10},
1071 	{0xa1, 0x21, 0x10, 0x32, 0x00, 0x00, 0x00, 0x10},
1072 /*	{0xa1, 0x21, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
1073 /*	{0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},  * GAIN - def */
1074 /*	{0xb1, 0x21, 0x01, 0x6c, 0x6c, 0x00, 0x00, 0x10},  * B R - def: 80 */
1075 /*...*/
1076 	{0xa1, 0x21, 0x11, 0x81, 0x00, 0x00, 0x00, 0x10}, /* CLKRC */
1077 /*	{0xa1, 0x21, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
1078 /*	{0xa1, 0x21, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
1079 /*	{0xa1, 0x21, 0x2a, 0x91, 0x00, 0x00, 0x00, 0x10}, jfm done */
1080 /*	{0xa1, 0x21, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
1081 /*	{0xb1, 0x21, 0x01, 0x64, 0x84, 0x00, 0x00, 0x10},  * B R - def: 80 */
1082 
1083 	{}
1084 };
1085 
1086 static const u8 ov7660_sensor_init[][8] = {
1087 	{0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* reset SCCB */
1088 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
1089 	{0xa1, 0x21, 0x12, 0x05, 0x00, 0x00, 0x00, 0x10},
1090 						/* Outformat = rawRGB */
1091 	{0xa1, 0x21, 0x13, 0xb8, 0x00, 0x00, 0x00, 0x10}, /* init COM8 */
1092 	{0xd1, 0x21, 0x00, 0x01, 0x74, 0x92, 0x00, 0x10},
1093 						/* GAIN BLUE RED VREF */
1094 	{0xd1, 0x21, 0x04, 0x00, 0x7d, 0x62, 0x00, 0x10},
1095 						/* COM 1 BAVE GEAVE AECHH */
1096 	{0xb1, 0x21, 0x08, 0x83, 0x01, 0x00, 0x00, 0x10}, /* RAVE COM2 */
1097 	{0xd1, 0x21, 0x0c, 0x00, 0x08, 0x04, 0x4f, 0x10}, /* COM 3 4 5 6 */
1098 	{0xd1, 0x21, 0x10, 0x7f, 0x40, 0x05, 0xff, 0x10},
1099 						/* AECH CLKRC COM7 COM8 */
1100 	{0xc1, 0x21, 0x14, 0x2c, 0x00, 0x02, 0x00, 0x10}, /* COM9 COM10 */
1101 	{0xd1, 0x21, 0x17, 0x10, 0x60, 0x02, 0x7b, 0x10},
1102 						/* HSTART HSTOP VSTRT VSTOP */
1103 	{0xa1, 0x21, 0x1b, 0x02, 0x00, 0x00, 0x00, 0x10}, /* PSHFT */
1104 	{0xb1, 0x21, 0x1e, 0x01, 0x0e, 0x00, 0x00, 0x10}, /* MVFP LAEC */
1105 	{0xd1, 0x21, 0x20, 0x07, 0x07, 0x07, 0x07, 0x10},
1106 					/* BOS GBOS GROS ROS (BGGR offset) */
1107 /*	{0xd1, 0x21, 0x24, 0x68, 0x58, 0xd4, 0x80, 0x10}, */
1108 	{0xd1, 0x21, 0x24, 0x78, 0x68, 0xd4, 0x80, 0x10},
1109 						/* AEW AEB VPT BBIAS */
1110 	{0xd1, 0x21, 0x28, 0x80, 0x30, 0x00, 0x00, 0x10},
1111 						/* GbBIAS RSVD EXHCH EXHCL */
1112 	{0xd1, 0x21, 0x2c, 0x80, 0x00, 0x00, 0x62, 0x10},
1113 						/* RBIAS ADVFL ASDVFH YAVE */
1114 	{0xc1, 0x21, 0x30, 0x08, 0x30, 0xb4, 0x00, 0x10},
1115 						/* HSYST HSYEN HREF */
1116 	{0xd1, 0x21, 0x33, 0x00, 0x07, 0x84, 0x00, 0x10}, /* reserved */
1117 	{0xd1, 0x21, 0x37, 0x0c, 0x02, 0x43, 0x00, 0x10},
1118 						/* ADC ACOM OFON TSLB */
1119 	{0xd1, 0x21, 0x3b, 0x02, 0x6c, 0x19, 0x0e, 0x10},
1120 						/* COM11 COM12 COM13 COM14 */
1121 	{0xd1, 0x21, 0x3f, 0x41, 0xc1, 0x22, 0x08, 0x10},
1122 						/* EDGE COM15 COM16 COM17 */
1123 	{0xd1, 0x21, 0x43, 0xf0, 0x10, 0x78, 0xa8, 0x10}, /* reserved */
1124 	{0xd1, 0x21, 0x47, 0x60, 0x80, 0x00, 0x00, 0x10}, /* reserved */
1125 	{0xd1, 0x21, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x10}, /* reserved */
1126 	{0xd1, 0x21, 0x4f, 0x46, 0x36, 0x0f, 0x17, 0x10}, /* MTX 1 2 3 4 */
1127 	{0xd1, 0x21, 0x53, 0x7f, 0x96, 0x40, 0x40, 0x10}, /* MTX 5 6 7 8 */
1128 	{0xb1, 0x21, 0x57, 0x40, 0x0f, 0x00, 0x00, 0x10}, /* MTX9 MTXS */
1129 	{0xd1, 0x21, 0x59, 0xba, 0x9a, 0x22, 0xb9, 0x10}, /* reserved */
1130 	{0xd1, 0x21, 0x5d, 0x9b, 0x10, 0xf0, 0x05, 0x10}, /* reserved */
1131 	{0xa1, 0x21, 0x61, 0x60, 0x00, 0x00, 0x00, 0x10}, /* reserved */
1132 	{0xd1, 0x21, 0x62, 0x00, 0x00, 0x50, 0x30, 0x10},
1133 						/* LCC1 LCC2 LCC3 LCC4 */
1134 	{0xa1, 0x21, 0x66, 0x00, 0x00, 0x00, 0x00, 0x10}, /* LCC5 */
1135 	{0xd1, 0x21, 0x67, 0x80, 0x7a, 0x90, 0x80, 0x10}, /* MANU */
1136 	{0xa1, 0x21, 0x6b, 0x0a, 0x00, 0x00, 0x00, 0x10},
1137 					/* band gap reference [0:3] DBLV */
1138 	{0xd1, 0x21, 0x6c, 0x30, 0x48, 0x80, 0x74, 0x10}, /* gamma curve */
1139 	{0xd1, 0x21, 0x70, 0x64, 0x60, 0x5c, 0x58, 0x10}, /* gamma curve */
1140 	{0xd1, 0x21, 0x74, 0x54, 0x4c, 0x40, 0x38, 0x10}, /* gamma curve */
1141 	{0xd1, 0x21, 0x78, 0x34, 0x30, 0x2f, 0x2b, 0x10}, /* gamma curve */
1142 	{0xd1, 0x21, 0x7c, 0x03, 0x07, 0x17, 0x34, 0x10}, /* gamma curve */
1143 	{0xd1, 0x21, 0x80, 0x41, 0x4d, 0x58, 0x63, 0x10}, /* gamma curve */
1144 	{0xd1, 0x21, 0x84, 0x6e, 0x77, 0x87, 0x95, 0x10}, /* gamma curve */
1145 	{0xc1, 0x21, 0x88, 0xaf, 0xc7, 0xdf, 0x00, 0x10}, /* gamma curve */
1146 	{0xc1, 0x21, 0x8b, 0x99, 0x99, 0xcf, 0x00, 0x10}, /* reserved */
1147 	{0xb1, 0x21, 0x92, 0x00, 0x00, 0x00, 0x00, 0x10}, /* DM_LNL/H */
1148 /* not in all ms-win traces*/
1149 	{0xa1, 0x21, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x10},
1150 	{}
1151 };
1152 static const u8 ov7660_sensor_param1[][8] = {
1153 	{0xa1, 0x21, 0x1e, 0x01, 0x00, 0x00, 0x00, 0x10}, /* MVFP */
1154 						/* bits[3..0]reserved */
1155 	{0xa1, 0x21, 0x1e, 0x01, 0x00, 0x00, 0x00, 0x10},
1156 	{0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
1157 						/* VREF vertical frame ctrl */
1158 	{0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
1159 	{0xa1, 0x21, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10}, /* AECH 0x20 */
1160 	{0xa1, 0x21, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x10}, /* ADVFL */
1161 	{0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10}, /* ADVFH */
1162 	{0xa1, 0x21, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x10}, /* GAIN */
1163 /*	{0xb1, 0x21, 0x01, 0x78, 0x78, 0x00, 0x00, 0x10}, * BLUE */
1164 /****** (some exchanges in the win trace) ******/
1165 /*fixme:param2*/
1166 	{0xa1, 0x21, 0x93, 0x00, 0x00, 0x00, 0x00, 0x10},/* dummy line hight */
1167 	{0xa1, 0x21, 0x92, 0x25, 0x00, 0x00, 0x00, 0x10}, /* dummy line low */
1168 	{0xa1, 0x21, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x10}, /* EXHCH */
1169 	{0xa1, 0x21, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10}, /* EXHCL */
1170 /*	{0xa1, 0x21, 0x02, 0x90, 0x00, 0x00, 0x00, 0x10},  * RED */
1171 /****** (some exchanges in the win trace) ******/
1172 /******!! startsensor KO if changed !!****/
1173 /*fixme: param3*/
1174 	{0xa1, 0x21, 0x93, 0x01, 0x00, 0x00, 0x00, 0x10},
1175 	{0xa1, 0x21, 0x92, 0xff, 0x00, 0x00, 0x00, 0x10},
1176 	{0xa1, 0x21, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x10},
1177 	{0xa1, 0x21, 0x2b, 0xc3, 0x00, 0x00, 0x00, 0x10},
1178 	{}
1179 };
1180 
1181 static const u8 po1030_sensor_init[][8] = {
1182 /* the sensor registers are described in m5602/m5602_po1030.h */
1183 	{0xa1, 0x6e, 0x3f, 0x20, 0x00, 0x00, 0x00, 0x10}, /* sensor reset */
1184 	{DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
1185 	{0xa1, 0x6e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x10},
1186 	{0xa1, 0x6e, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x10},
1187 	{0xd1, 0x6e, 0x04, 0x02, 0xb1, 0x02, 0x39, 0x10},
1188 	{0xd1, 0x6e, 0x08, 0x00, 0x01, 0x00, 0x00, 0x10},
1189 	{0xd1, 0x6e, 0x0c, 0x02, 0x7f, 0x01, 0xe0, 0x10},
1190 	{0xd1, 0x6e, 0x12, 0x03, 0x02, 0x00, 0x03, 0x10},
1191 	{0xd1, 0x6e, 0x16, 0x85, 0x40, 0x4a, 0x40, 0x10}, /* r/g1/b/g2 gains */
1192 	{0xc1, 0x6e, 0x1a, 0x00, 0x80, 0x00, 0x00, 0x10},
1193 	{0xd1, 0x6e, 0x1d, 0x08, 0x03, 0x00, 0x00, 0x10},
1194 	{0xd1, 0x6e, 0x23, 0x00, 0xb0, 0x00, 0x94, 0x10},
1195 	{0xd1, 0x6e, 0x27, 0x58, 0x00, 0x00, 0x00, 0x10},
1196 	{0xb1, 0x6e, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10},
1197 	{0xd1, 0x6e, 0x2d, 0x14, 0x35, 0x61, 0x84, 0x10}, /* gamma corr */
1198 	{0xd1, 0x6e, 0x31, 0xa2, 0xbd, 0xd8, 0xff, 0x10},
1199 	{0xd1, 0x6e, 0x35, 0x06, 0x1e, 0x12, 0x02, 0x10}, /* color matrix */
1200 	{0xd1, 0x6e, 0x39, 0xaa, 0x53, 0x37, 0xd5, 0x10},
1201 	{0xa1, 0x6e, 0x3d, 0xf2, 0x00, 0x00, 0x00, 0x10},
1202 	{0xd1, 0x6e, 0x3e, 0x00, 0x00, 0x80, 0x03, 0x10},
1203 	{0xd1, 0x6e, 0x42, 0x03, 0x00, 0x00, 0x00, 0x10},
1204 	{0xc1, 0x6e, 0x46, 0x00, 0x80, 0x80, 0x00, 0x10},
1205 	{0xd1, 0x6e, 0x4b, 0x02, 0xef, 0x08, 0xcd, 0x10},
1206 	{0xd1, 0x6e, 0x4f, 0x00, 0xd0, 0x00, 0xa0, 0x10},
1207 	{0xd1, 0x6e, 0x53, 0x01, 0xaa, 0x01, 0x40, 0x10},
1208 	{0xd1, 0x6e, 0x5a, 0x50, 0x04, 0x30, 0x03, 0x10}, /* raw rgb bayer */
1209 	{0xa1, 0x6e, 0x5e, 0x00, 0x00, 0x00, 0x00, 0x10},
1210 	{0xd1, 0x6e, 0x5f, 0x10, 0x40, 0xff, 0x00, 0x10},
1211 
1212 	{0xd1, 0x6e, 0x63, 0x40, 0x40, 0x00, 0x00, 0x10},
1213 	{0xd1, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x10},
1214 	{0xd1, 0x6e, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x10},
1215 	{0xd1, 0x6e, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x10},
1216 	{0xc1, 0x6e, 0x73, 0x10, 0x80, 0xeb, 0x00, 0x10},
1217 	{}
1218 };
1219 static const u8 po1030_sensor_param1[][8] = {
1220 /* from ms-win traces - these values change with auto gain/expo/wb.. */
1221 	{0xa1, 0x6e, 0x1e, 0x03, 0x00, 0x00, 0x00, 0x10},
1222 	{0xa1, 0x6e, 0x1e, 0x03, 0x00, 0x00, 0x00, 0x10},
1223 /* mean values */
1224 	{0xc1, 0x6e, 0x1a, 0x02, 0xd4, 0xa4, 0x00, 0x10}, /* integlines */
1225 	{0xa1, 0x6e, 0x15, 0x04, 0x00, 0x00, 0x00, 0x10}, /* global gain */
1226 	{0xc1, 0x6e, 0x16, 0x40, 0x40, 0x40, 0x00, 0x10}, /* r/g1/b gains */
1227 
1228 	{0xa1, 0x6e, 0x1d, 0x08, 0x00, 0x00, 0x00, 0x10}, /* control1 */
1229 	{0xa1, 0x6e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x10}, /* frameheight */
1230 	{0xa1, 0x6e, 0x07, 0xd5, 0x00, 0x00, 0x00, 0x10},
1231 /*	{0xc1, 0x6e, 0x16, 0x49, 0x40, 0x45, 0x00, 0x10}, */
1232 	{}
1233 };
1234 
1235 static const u8 po2030n_sensor_init[][8] = {
1236 	{0xa1, 0x6e, 0x1e, 0x1a, 0x00, 0x00, 0x00, 0x10},
1237 	{0xa1, 0x6e, 0x1f, 0x99, 0x00, 0x00, 0x00, 0x10},
1238 	{DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
1239 	{0xa1, 0x6e, 0x1e, 0x0a, 0x00, 0x00, 0x00, 0x10},
1240 	{0xa1, 0x6e, 0x1f, 0x19, 0x00, 0x00, 0x00, 0x10},
1241 	{DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
1242 	{0xa1, 0x6e, 0x20, 0x44, 0x00, 0x00, 0x00, 0x10},
1243 	{0xa1, 0x6e, 0x04, 0x03, 0x00, 0x00, 0x00, 0x10},
1244 	{0xa1, 0x6e, 0x05, 0x70, 0x00, 0x00, 0x00, 0x10},
1245 	{0xa1, 0x6e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x10},
1246 	{0xa1, 0x6e, 0x07, 0x25, 0x00, 0x00, 0x00, 0x10},
1247 	{0xd1, 0x6e, 0x08, 0x00, 0xd0, 0x00, 0x08, 0x10},
1248 	{0xd1, 0x6e, 0x0c, 0x03, 0x50, 0x01, 0xe8, 0x10},
1249 	{0xd1, 0x6e, 0x1d, 0x20, 0x0a, 0x19, 0x44, 0x10},
1250 	{0xd1, 0x6e, 0x21, 0x00, 0x00, 0x00, 0x00, 0x10},
1251 	{0xd1, 0x6e, 0x25, 0x00, 0x00, 0x00, 0x00, 0x10},
1252 	{0xd1, 0x6e, 0x29, 0x00, 0x00, 0x00, 0x00, 0x10},
1253 	{0xd1, 0x6e, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x10},
1254 	{0xd1, 0x6e, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
1255 	{0xd1, 0x6e, 0x35, 0x00, 0x00, 0x00, 0x00, 0x10},
1256 	{0xd1, 0x6e, 0x39, 0x00, 0x00, 0x00, 0x00, 0x10},
1257 	{0xd1, 0x6e, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x10},
1258 	{0xd1, 0x6e, 0x41, 0x00, 0x00, 0x00, 0x00, 0x10},
1259 	{0xd1, 0x6e, 0x45, 0x00, 0x00, 0x00, 0x00, 0x10},
1260 	{0xd1, 0x6e, 0x49, 0x00, 0x00, 0x00, 0x00, 0x10},
1261 	{0xd1, 0x6e, 0x4d, 0x00, 0x00, 0x00, 0xed, 0x10},
1262 	{0xd1, 0x6e, 0x51, 0x17, 0x4a, 0x2f, 0xc0, 0x10},
1263 	{0xd1, 0x6e, 0x55, 0x00, 0x00, 0x00, 0x00, 0x10},
1264 	{0xd1, 0x6e, 0x59, 0x00, 0x00, 0x00, 0x00, 0x10},
1265 	{0xd1, 0x6e, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x10},
1266 	{0xd1, 0x6e, 0x61, 0x00, 0x00, 0x00, 0x00, 0x10},
1267 	{0xd1, 0x6e, 0x65, 0x00, 0x00, 0x00, 0x00, 0x10},
1268 	{0xd1, 0x6e, 0x69, 0x00, 0x00, 0x00, 0x00, 0x10},
1269 	{0xd1, 0x6e, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x10},
1270 	{0xd1, 0x6e, 0x71, 0x00, 0x00, 0x00, 0x00, 0x10},
1271 	{0xd1, 0x6e, 0x75, 0x00, 0x00, 0x00, 0x00, 0x10},
1272 	{0xd1, 0x6e, 0x79, 0x00, 0x00, 0x00, 0x00, 0x10},
1273 	{0xd1, 0x6e, 0x7d, 0x00, 0x00, 0x00, 0x00, 0x10},
1274 	{0xd1, 0x6e, 0x81, 0x00, 0x00, 0x00, 0x00, 0x10},
1275 	{0xd1, 0x6e, 0x85, 0x00, 0x00, 0x00, 0x08, 0x10},
1276 	{0xd1, 0x6e, 0x89, 0x01, 0xe8, 0x00, 0x01, 0x10},
1277 	{0xa1, 0x6e, 0x8d, 0x00, 0x00, 0x00, 0x00, 0x10},
1278 	{0xd1, 0x6e, 0x21, 0x00, 0x00, 0x00, 0x00, 0x10},
1279 	{0xd1, 0x6e, 0x25, 0x00, 0x00, 0x00, 0x01, 0x10},
1280 	{0xd1, 0x6e, 0x29, 0xe6, 0x00, 0xbd, 0x03, 0x10},
1281 	{0xd1, 0x6e, 0x2d, 0x41, 0x38, 0x68, 0x40, 0x10},
1282 	{0xd1, 0x6e, 0x31, 0x2b, 0x00, 0x36, 0x00, 0x10},
1283 	{0xd1, 0x6e, 0x35, 0x30, 0x30, 0x08, 0x00, 0x10},
1284 	{0xd1, 0x6e, 0x39, 0x00, 0x00, 0x33, 0x06, 0x10},
1285 	{0xb1, 0x6e, 0x3d, 0x06, 0x02, 0x00, 0x00, 0x10},
1286 	{}
1287 };
1288 static const u8 po2030n_sensor_param1[][8] = {
1289 	{0xa1, 0x6e, 0x1a, 0x01, 0x00, 0x00, 0x00, 0x10},
1290 	{DELAY, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 8ms */
1291 	{0xa1, 0x6e, 0x1b, 0xf4, 0x00, 0x00, 0x00, 0x10},
1292 	{0xa1, 0x6e, 0x15, 0x04, 0x00, 0x00, 0x00, 0x10},
1293 	{0xd1, 0x6e, 0x16, 0x40, 0x40, 0x40, 0x40, 0x10}, /* RGBG gains */
1294 /*param2*/
1295 	{0xa1, 0x6e, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x10},
1296 	{0xa1, 0x6e, 0x04, 0x03, 0x00, 0x00, 0x00, 0x10},
1297 	{0xa1, 0x6e, 0x05, 0x6f, 0x00, 0x00, 0x00, 0x10},
1298 	{0xa1, 0x6e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x10},
1299 	{0xa1, 0x6e, 0x07, 0x25, 0x00, 0x00, 0x00, 0x10},
1300 	{}
1301 };
1302 
1303 static const u8 soi768_sensor_init[][8] = {
1304 	{0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* reset */
1305 	{DELAY, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 96ms */
1306 	{0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
1307 	{0xa1, 0x21, 0x13, 0x80, 0x00, 0x00, 0x00, 0x10},
1308 	{0xa1, 0x21, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x10},
1309 	{0xa1, 0x21, 0x19, 0x00, 0x00, 0x00, 0x00, 0x10},
1310 	{}
1311 };
1312 static const u8 soi768_sensor_param1[][8] = {
1313 	{0xa1, 0x21, 0x10, 0x10, 0x00, 0x00, 0x00, 0x10},
1314 	{0xa1, 0x21, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x10},
1315 	{0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10},
1316 	{0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1317 	{0xb1, 0x21, 0x01, 0x7f, 0x7f, 0x00, 0x00, 0x10},
1318 /* */
1319 /*	{0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10}, */
1320 /*	{0xa1, 0x21, 0x2d, 0x25, 0x00, 0x00, 0x00, 0x10}, */
1321 	{0xa1, 0x21, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10},
1322 /*	{0xb1, 0x21, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x10}, */
1323 	{0xa1, 0x21, 0x02, 0x8d, 0x00, 0x00, 0x00, 0x10},
1324 /* the next sequence should be used for auto gain */
1325 	{0xa1, 0x21, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10},
1326 			/* global gain ? : 07 - change with 0x15 at the end */
1327 	{0xa1, 0x21, 0x10, 0x3f, 0x00, 0x00, 0x00, 0x10}, /* ???? : 063f */
1328 	{0xa1, 0x21, 0x04, 0x06, 0x00, 0x00, 0x00, 0x10},
1329 	{0xb1, 0x21, 0x2d, 0x63, 0x03, 0x00, 0x00, 0x10},
1330 			/* exposure ? : 0200 - change with 0x1e at the end */
1331 	{}
1332 };
1333 
1334 static const u8 sp80708_sensor_init[][8] = {
1335 	{0xa1, 0x18, 0x06, 0xf9, 0x00, 0x00, 0x00, 0x10},
1336 	{0xa1, 0x18, 0x09, 0x1f, 0x00, 0x00, 0x00, 0x10},
1337 	{0xa1, 0x18, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
1338 	{0xa1, 0x18, 0x0d, 0xc0, 0x00, 0x00, 0x00, 0x10},
1339 	{0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
1340 	{0xa1, 0x18, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x10},
1341 	{0xa1, 0x18, 0x10, 0x40, 0x00, 0x00, 0x00, 0x10},
1342 	{0xa1, 0x18, 0x11, 0x4e, 0x00, 0x00, 0x00, 0x10},
1343 	{0xa1, 0x18, 0x12, 0x53, 0x00, 0x00, 0x00, 0x10},
1344 	{0xa1, 0x18, 0x15, 0x80, 0x00, 0x00, 0x00, 0x10},
1345 	{0xa1, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x10},
1346 	{0xa1, 0x18, 0x19, 0x18, 0x00, 0x00, 0x00, 0x10},
1347 	{0xa1, 0x18, 0x1a, 0x10, 0x00, 0x00, 0x00, 0x10},
1348 	{0xa1, 0x18, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x10},
1349 	{0xa1, 0x18, 0x1c, 0x28, 0x00, 0x00, 0x00, 0x10},
1350 	{0xa1, 0x18, 0x1d, 0x02, 0x00, 0x00, 0x00, 0x10},
1351 	{0xa1, 0x18, 0x1e, 0x10, 0x00, 0x00, 0x00, 0x10},
1352 	{0xa1, 0x18, 0x26, 0x04, 0x00, 0x00, 0x00, 0x10},
1353 	{0xa1, 0x18, 0x27, 0x1e, 0x00, 0x00, 0x00, 0x10},
1354 	{0xa1, 0x18, 0x28, 0x5a, 0x00, 0x00, 0x00, 0x10},
1355 	{0xa1, 0x18, 0x29, 0x28, 0x00, 0x00, 0x00, 0x10},
1356 	{0xa1, 0x18, 0x2a, 0x78, 0x00, 0x00, 0x00, 0x10},
1357 	{0xa1, 0x18, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x10},
1358 	{0xa1, 0x18, 0x2c, 0xf7, 0x00, 0x00, 0x00, 0x10},
1359 	{0xa1, 0x18, 0x2d, 0x2d, 0x00, 0x00, 0x00, 0x10},
1360 	{0xa1, 0x18, 0x2e, 0xd5, 0x00, 0x00, 0x00, 0x10},
1361 	{0xa1, 0x18, 0x39, 0x42, 0x00, 0x00, 0x00, 0x10},
1362 	{0xa1, 0x18, 0x3a, 0x67, 0x00, 0x00, 0x00, 0x10},
1363 	{0xa1, 0x18, 0x3b, 0x87, 0x00, 0x00, 0x00, 0x10},
1364 	{0xa1, 0x18, 0x3c, 0xa3, 0x00, 0x00, 0x00, 0x10},
1365 	{0xa1, 0x18, 0x3d, 0xb0, 0x00, 0x00, 0x00, 0x10},
1366 	{0xa1, 0x18, 0x3e, 0xbc, 0x00, 0x00, 0x00, 0x10},
1367 	{0xa1, 0x18, 0x3f, 0xc8, 0x00, 0x00, 0x00, 0x10},
1368 	{0xa1, 0x18, 0x40, 0xd4, 0x00, 0x00, 0x00, 0x10},
1369 	{0xa1, 0x18, 0x41, 0xdf, 0x00, 0x00, 0x00, 0x10},
1370 	{0xa1, 0x18, 0x42, 0xea, 0x00, 0x00, 0x00, 0x10},
1371 	{0xa1, 0x18, 0x43, 0xf5, 0x00, 0x00, 0x00, 0x10},
1372 	{0xa1, 0x18, 0x45, 0x80, 0x00, 0x00, 0x00, 0x10},
1373 	{0xa1, 0x18, 0x46, 0x60, 0x00, 0x00, 0x00, 0x10},
1374 	{0xa1, 0x18, 0x47, 0x50, 0x00, 0x00, 0x00, 0x10},
1375 	{0xa1, 0x18, 0x48, 0x30, 0x00, 0x00, 0x00, 0x10},
1376 	{0xa1, 0x18, 0x49, 0x01, 0x00, 0x00, 0x00, 0x10},
1377 	{0xa1, 0x18, 0x4d, 0xae, 0x00, 0x00, 0x00, 0x10},
1378 	{0xa1, 0x18, 0x4e, 0x03, 0x00, 0x00, 0x00, 0x10},
1379 	{0xa1, 0x18, 0x4f, 0x66, 0x00, 0x00, 0x00, 0x10},
1380 	{0xa1, 0x18, 0x50, 0x1c, 0x00, 0x00, 0x00, 0x10},
1381 	{0xa1, 0x18, 0x44, 0x10, 0x00, 0x00, 0x00, 0x10},
1382 	{0xa1, 0x18, 0x4a, 0x30, 0x00, 0x00, 0x00, 0x10},
1383 	{0xa1, 0x18, 0x51, 0x80, 0x00, 0x00, 0x00, 0x10},
1384 	{0xa1, 0x18, 0x52, 0x80, 0x00, 0x00, 0x00, 0x10},
1385 	{0xa1, 0x18, 0x53, 0x80, 0x00, 0x00, 0x00, 0x10},
1386 	{0xa1, 0x18, 0x54, 0x80, 0x00, 0x00, 0x00, 0x10},
1387 	{0xa1, 0x18, 0x55, 0x80, 0x00, 0x00, 0x00, 0x10},
1388 	{0xa1, 0x18, 0x56, 0x80, 0x00, 0x00, 0x00, 0x10},
1389 	{0xa1, 0x18, 0x57, 0xe0, 0x00, 0x00, 0x00, 0x10},
1390 	{0xa1, 0x18, 0x58, 0xc0, 0x00, 0x00, 0x00, 0x10},
1391 	{0xa1, 0x18, 0x59, 0xab, 0x00, 0x00, 0x00, 0x10},
1392 	{0xa1, 0x18, 0x5a, 0xa0, 0x00, 0x00, 0x00, 0x10},
1393 	{0xa1, 0x18, 0x5b, 0x99, 0x00, 0x00, 0x00, 0x10},
1394 	{0xa1, 0x18, 0x5c, 0x90, 0x00, 0x00, 0x00, 0x10},
1395 	{0xa1, 0x18, 0x5e, 0x24, 0x00, 0x00, 0x00, 0x10},
1396 	{0xa1, 0x18, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x10},
1397 	{0xa1, 0x18, 0x60, 0x00, 0x00, 0x00, 0x00, 0x10},
1398 	{0xa1, 0x18, 0x61, 0x73, 0x00, 0x00, 0x00, 0x10},
1399 	{0xa1, 0x18, 0x63, 0x42, 0x00, 0x00, 0x00, 0x10},
1400 	{0xa1, 0x18, 0x64, 0x42, 0x00, 0x00, 0x00, 0x10},
1401 	{0xa1, 0x18, 0x65, 0x42, 0x00, 0x00, 0x00, 0x10},
1402 	{0xa1, 0x18, 0x66, 0x24, 0x00, 0x00, 0x00, 0x10},
1403 	{0xa1, 0x18, 0x67, 0x24, 0x00, 0x00, 0x00, 0x10},
1404 	{0xa1, 0x18, 0x68, 0x08, 0x00, 0x00, 0x00, 0x10},
1405 	{0xa1, 0x18, 0x2f, 0xc9, 0x00, 0x00, 0x00, 0x10},
1406 	{}
1407 };
1408 static const u8 sp80708_sensor_param1[][8] = {
1409 	{0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
1410 	{0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
1411 	{0xa1, 0x18, 0x03, 0x01, 0x00, 0x00, 0x00, 0x10},
1412 	{0xa1, 0x18, 0x04, 0xa4, 0x00, 0x00, 0x00, 0x10},
1413 	{0xa1, 0x18, 0x14, 0x3f, 0x00, 0x00, 0x00, 0x10},
1414 	{0xa1, 0x18, 0x5d, 0x80, 0x00, 0x00, 0x00, 0x10},
1415 	{0xb1, 0x18, 0x11, 0x40, 0x40, 0x00, 0x00, 0x10},
1416 	{}
1417 };
1418 
1419 static const u8 (*sensor_init[])[8] = {
1420 [SENSOR_ADCM1700] =	adcm1700_sensor_init,
1421 [SENSOR_GC0307] =	gc0307_sensor_init,
1422 [SENSOR_HV7131R] =	hv7131r_sensor_init,
1423 [SENSOR_MI0360] =	mi0360_sensor_init,
1424 [SENSOR_MI0360B] =	mi0360b_sensor_init,
1425 [SENSOR_MO4000] =	mo4000_sensor_init,
1426 [SENSOR_MT9V111] =	mt9v111_sensor_init,
1427 [SENSOR_OM6802] =	om6802_sensor_init,
1428 [SENSOR_OV7630] =	ov7630_sensor_init,
1429 [SENSOR_OV7648] =	ov7648_sensor_init,
1430 [SENSOR_OV7660] =	ov7660_sensor_init,
1431 [SENSOR_PO1030] =	po1030_sensor_init,
1432 [SENSOR_PO2030N] =	po2030n_sensor_init,
1433 [SENSOR_SOI768] =	soi768_sensor_init,
1434 [SENSOR_SP80708] =	sp80708_sensor_init,
1435 };
1436 
1437 /* read <len> bytes to gspca_dev->usb_buf */
1438 static void reg_r(struct gspca_dev *gspca_dev,
1439 		  u16 value, int len)
1440 {
1441 	int ret;
1442 
1443 	if (gspca_dev->usb_err < 0)
1444 		return;
1445 #ifdef GSPCA_DEBUG
1446 	if (len > USB_BUF_SZ) {
1447 		pr_err("reg_r: buffer overflow\n");
1448 		return;
1449 	}
1450 #endif
1451 	ret = usb_control_msg(gspca_dev->dev,
1452 			usb_rcvctrlpipe(gspca_dev->dev, 0),
1453 			0,
1454 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1455 			value, 0,
1456 			gspca_dev->usb_buf, len,
1457 			500);
1458 	PDEBUG(D_USBI, "reg_r [%02x] -> %02x", value, gspca_dev->usb_buf[0]);
1459 	if (ret < 0) {
1460 		pr_err("reg_r err %d\n", ret);
1461 		gspca_dev->usb_err = ret;
1462 	}
1463 }
1464 
1465 static void reg_w1(struct gspca_dev *gspca_dev,
1466 		   u16 value,
1467 		   u8 data)
1468 {
1469 	int ret;
1470 
1471 	if (gspca_dev->usb_err < 0)
1472 		return;
1473 	PDEBUG(D_USBO, "reg_w1 [%04x] = %02x", value, data);
1474 	gspca_dev->usb_buf[0] = data;
1475 	ret = usb_control_msg(gspca_dev->dev,
1476 			usb_sndctrlpipe(gspca_dev->dev, 0),
1477 			0x08,
1478 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1479 			value,
1480 			0,
1481 			gspca_dev->usb_buf, 1,
1482 			500);
1483 	if (ret < 0) {
1484 		pr_err("reg_w1 err %d\n", ret);
1485 		gspca_dev->usb_err = ret;
1486 	}
1487 }
1488 static void reg_w(struct gspca_dev *gspca_dev,
1489 			  u16 value,
1490 			  const u8 *buffer,
1491 			  int len)
1492 {
1493 	int ret;
1494 
1495 	if (gspca_dev->usb_err < 0)
1496 		return;
1497 	PDEBUG(D_USBO, "reg_w [%04x] = %02x %02x ..",
1498 		value, buffer[0], buffer[1]);
1499 #ifdef GSPCA_DEBUG
1500 	if (len > USB_BUF_SZ) {
1501 		pr_err("reg_w: buffer overflow\n");
1502 		return;
1503 	}
1504 #endif
1505 	memcpy(gspca_dev->usb_buf, buffer, len);
1506 	ret = usb_control_msg(gspca_dev->dev,
1507 			usb_sndctrlpipe(gspca_dev->dev, 0),
1508 			0x08,
1509 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1510 			value, 0,
1511 			gspca_dev->usb_buf, len,
1512 			500);
1513 	if (ret < 0) {
1514 		pr_err("reg_w err %d\n", ret);
1515 		gspca_dev->usb_err = ret;
1516 	}
1517 }
1518 
1519 /* I2C write 1 byte */
1520 static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
1521 {
1522 	struct sd *sd = (struct sd *) gspca_dev;
1523 	int ret;
1524 
1525 	if (gspca_dev->usb_err < 0)
1526 		return;
1527 	PDEBUG(D_USBO, "i2c_w1 [%02x] = %02x", reg, val);
1528 	switch (sd->sensor) {
1529 	case SENSOR_ADCM1700:
1530 	case SENSOR_OM6802:
1531 	case SENSOR_GC0307:		/* i2c command = a0 (100 kHz) */
1532 		gspca_dev->usb_buf[0] = 0x80 | (2 << 4);
1533 		break;
1534 	default:			/* i2c command = a1 (400 kHz) */
1535 		gspca_dev->usb_buf[0] = 0x81 | (2 << 4);
1536 		break;
1537 	}
1538 	gspca_dev->usb_buf[1] = sd->i2c_addr;
1539 	gspca_dev->usb_buf[2] = reg;
1540 	gspca_dev->usb_buf[3] = val;
1541 	gspca_dev->usb_buf[4] = 0;
1542 	gspca_dev->usb_buf[5] = 0;
1543 	gspca_dev->usb_buf[6] = 0;
1544 	gspca_dev->usb_buf[7] = 0x10;
1545 	ret = usb_control_msg(gspca_dev->dev,
1546 			usb_sndctrlpipe(gspca_dev->dev, 0),
1547 			0x08,
1548 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1549 			0x08,			/* value = i2c */
1550 			0,
1551 			gspca_dev->usb_buf, 8,
1552 			500);
1553 	msleep(2);
1554 	if (ret < 0) {
1555 		pr_err("i2c_w1 err %d\n", ret);
1556 		gspca_dev->usb_err = ret;
1557 	}
1558 }
1559 
1560 /* I2C write 8 bytes */
1561 static void i2c_w8(struct gspca_dev *gspca_dev,
1562 		   const u8 *buffer)
1563 {
1564 	int ret;
1565 
1566 	if (gspca_dev->usb_err < 0)
1567 		return;
1568 	PDEBUG(D_USBO, "i2c_w8 [%02x] = %02x ..",
1569 		buffer[2], buffer[3]);
1570 	memcpy(gspca_dev->usb_buf, buffer, 8);
1571 	ret = usb_control_msg(gspca_dev->dev,
1572 			usb_sndctrlpipe(gspca_dev->dev, 0),
1573 			0x08,
1574 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1575 			0x08, 0,		/* value, index */
1576 			gspca_dev->usb_buf, 8,
1577 			500);
1578 	msleep(2);
1579 	if (ret < 0) {
1580 		pr_err("i2c_w8 err %d\n", ret);
1581 		gspca_dev->usb_err = ret;
1582 	}
1583 }
1584 
1585 /* sensor read 'len' (1..5) bytes in gspca_dev->usb_buf */
1586 static void i2c_r(struct gspca_dev *gspca_dev, u8 reg, int len)
1587 {
1588 	struct sd *sd = (struct sd *) gspca_dev;
1589 	u8 mode[8];
1590 
1591 	switch (sd->sensor) {
1592 	case SENSOR_ADCM1700:
1593 	case SENSOR_OM6802:
1594 	case SENSOR_GC0307:		/* i2c command = a0 (100 kHz) */
1595 		mode[0] = 0x80 | 0x10;
1596 		break;
1597 	default:			/* i2c command = 91 (400 kHz) */
1598 		mode[0] = 0x81 | 0x10;
1599 		break;
1600 	}
1601 	mode[1] = sd->i2c_addr;
1602 	mode[2] = reg;
1603 	mode[3] = 0;
1604 	mode[4] = 0;
1605 	mode[5] = 0;
1606 	mode[6] = 0;
1607 	mode[7] = 0x10;
1608 	i2c_w8(gspca_dev, mode);
1609 	msleep(2);
1610 	mode[0] = (mode[0] & 0x81) | (len << 4) | 0x02;
1611 	mode[2] = 0;
1612 	i2c_w8(gspca_dev, mode);
1613 	msleep(2);
1614 	reg_r(gspca_dev, 0x0a, 5);
1615 }
1616 
1617 static void i2c_w_seq(struct gspca_dev *gspca_dev,
1618 			const u8 (*data)[8])
1619 {
1620 	while ((*data)[0] != 0) {
1621 		if ((*data)[0] != DELAY)
1622 			i2c_w8(gspca_dev, *data);
1623 		else
1624 			msleep((*data)[1]);
1625 		data++;
1626 	}
1627 }
1628 
1629 /* check the ID of the hv7131 sensor */
1630 /* this sequence is needed because it activates the sensor */
1631 static void hv7131r_probe(struct gspca_dev *gspca_dev)
1632 {
1633 	i2c_w1(gspca_dev, 0x02, 0);		/* sensor wakeup */
1634 	msleep(10);
1635 	reg_w1(gspca_dev, 0x02, 0x66);		/* Gpio on */
1636 	msleep(10);
1637 	i2c_r(gspca_dev, 0, 5);			/* read sensor id */
1638 	if (gspca_dev->usb_buf[0] == 0x02	/* chip ID (02 is R) */
1639 	    && gspca_dev->usb_buf[1] == 0x09
1640 	    && gspca_dev->usb_buf[2] == 0x01) {
1641 		PDEBUG(D_PROBE, "Sensor HV7131R found");
1642 		return;
1643 	}
1644 	pr_warn("Erroneous HV7131R ID 0x%02x 0x%02x 0x%02x\n",
1645 		gspca_dev->usb_buf[0], gspca_dev->usb_buf[1],
1646 		gspca_dev->usb_buf[2]);
1647 }
1648 
1649 static void mi0360_probe(struct gspca_dev *gspca_dev)
1650 {
1651 	struct sd *sd = (struct sd *) gspca_dev;
1652 	int i, j;
1653 	u16 val = 0;
1654 	static const u8 probe_tb[][4][8] = {
1655 	    {					/* mi0360 */
1656 		{0xb0, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
1657 		{0x90, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1658 		{0xa2, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1659 		{0xb0, 0x5d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10}
1660 	    },
1661 	    {					/* mt9v111 */
1662 		{0xb0, 0x5c, 0x01, 0x00, 0x04, 0x00, 0x00, 0x10},
1663 		{0x90, 0x5c, 0x36, 0x00, 0x00, 0x00, 0x00, 0x10},
1664 		{0xa2, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1665 		{}
1666 	    },
1667 	};
1668 
1669 	for (i = 0; i < ARRAY_SIZE(probe_tb); i++) {
1670 		reg_w1(gspca_dev, 0x17, 0x62);
1671 		reg_w1(gspca_dev, 0x01, 0x08);
1672 		for (j = 0; j < 3; j++)
1673 			i2c_w8(gspca_dev, probe_tb[i][j]);
1674 		msleep(2);
1675 		reg_r(gspca_dev, 0x0a, 5);
1676 		val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1677 		if (probe_tb[i][3][0] != 0)
1678 			i2c_w8(gspca_dev, probe_tb[i][3]);
1679 		reg_w1(gspca_dev, 0x01, 0x29);
1680 		reg_w1(gspca_dev, 0x17, 0x42);
1681 		if (val != 0xffff)
1682 			break;
1683 	}
1684 	if (gspca_dev->usb_err < 0)
1685 		return;
1686 	switch (val) {
1687 	case 0x8221:
1688 		PDEBUG(D_PROBE, "Sensor mi0360b");
1689 		sd->sensor = SENSOR_MI0360B;
1690 		break;
1691 	case 0x823a:
1692 		PDEBUG(D_PROBE, "Sensor mt9v111");
1693 		sd->sensor = SENSOR_MT9V111;
1694 		break;
1695 	case 0x8243:
1696 		PDEBUG(D_PROBE, "Sensor mi0360");
1697 		break;
1698 	default:
1699 		PDEBUG(D_PROBE, "Unknown sensor %04x - forced to mi0360", val);
1700 		break;
1701 	}
1702 }
1703 
1704 static void ov7630_probe(struct gspca_dev *gspca_dev)
1705 {
1706 	struct sd *sd = (struct sd *) gspca_dev;
1707 	u16 val;
1708 
1709 	/* check ov76xx */
1710 	reg_w1(gspca_dev, 0x17, 0x62);
1711 	reg_w1(gspca_dev, 0x01, 0x08);
1712 	sd->i2c_addr = 0x21;
1713 	i2c_r(gspca_dev, 0x0a, 2);
1714 	val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1715 	reg_w1(gspca_dev, 0x01, 0x29);
1716 	reg_w1(gspca_dev, 0x17, 0x42);
1717 	if (gspca_dev->usb_err < 0)
1718 		return;
1719 	if (val == 0x7628) {			/* soi768 */
1720 		sd->sensor = SENSOR_SOI768;
1721 /*fixme: only valid for 0c45:613e?*/
1722 		gspca_dev->cam.input_flags =
1723 				V4L2_IN_ST_VFLIP | V4L2_IN_ST_HFLIP;
1724 		PDEBUG(D_PROBE, "Sensor soi768");
1725 		return;
1726 	}
1727 	PDEBUG(D_PROBE, "Sensor ov%04x", val);
1728 }
1729 
1730 static void ov7648_probe(struct gspca_dev *gspca_dev)
1731 {
1732 	struct sd *sd = (struct sd *) gspca_dev;
1733 	u16 val;
1734 
1735 	/* check ov76xx */
1736 	reg_w1(gspca_dev, 0x17, 0x62);
1737 	reg_w1(gspca_dev, 0x01, 0x08);
1738 	sd->i2c_addr = 0x21;
1739 	i2c_r(gspca_dev, 0x0a, 2);
1740 	val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1741 	reg_w1(gspca_dev, 0x01, 0x29);
1742 	reg_w1(gspca_dev, 0x17, 0x42);
1743 	if ((val & 0xff00) == 0x7600) {		/* ov76xx */
1744 		PDEBUG(D_PROBE, "Sensor ov%04x", val);
1745 		return;
1746 	}
1747 
1748 	/* check po1030 */
1749 	reg_w1(gspca_dev, 0x17, 0x62);
1750 	reg_w1(gspca_dev, 0x01, 0x08);
1751 	sd->i2c_addr = 0x6e;
1752 	i2c_r(gspca_dev, 0x00, 2);
1753 	val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1754 	reg_w1(gspca_dev, 0x01, 0x29);
1755 	reg_w1(gspca_dev, 0x17, 0x42);
1756 	if (gspca_dev->usb_err < 0)
1757 		return;
1758 	if (val == 0x1030) {			/* po1030 */
1759 		PDEBUG(D_PROBE, "Sensor po1030");
1760 		sd->sensor = SENSOR_PO1030;
1761 		return;
1762 	}
1763 	pr_err("Unknown sensor %04x\n", val);
1764 }
1765 
1766 /* 0c45:6142 sensor may be po2030n, gc0305 or gc0307 */
1767 static void po2030n_probe(struct gspca_dev *gspca_dev)
1768 {
1769 	struct sd *sd = (struct sd *) gspca_dev;
1770 	u16 val;
1771 
1772 	/* check gc0307 */
1773 	reg_w1(gspca_dev, 0x17, 0x62);
1774 	reg_w1(gspca_dev, 0x01, 0x08);
1775 	reg_w1(gspca_dev, 0x02, 0x22);
1776 	sd->i2c_addr = 0x21;
1777 	i2c_r(gspca_dev, 0x00, 1);
1778 	val = gspca_dev->usb_buf[4];
1779 	reg_w1(gspca_dev, 0x01, 0x29);		/* reset */
1780 	reg_w1(gspca_dev, 0x17, 0x42);
1781 	if (val == 0x99) {			/* gc0307 (?) */
1782 		PDEBUG(D_PROBE, "Sensor gc0307");
1783 		sd->sensor = SENSOR_GC0307;
1784 		return;
1785 	}
1786 
1787 	/* check po2030n */
1788 	reg_w1(gspca_dev, 0x17, 0x62);
1789 	reg_w1(gspca_dev, 0x01, 0x0a);
1790 	sd->i2c_addr = 0x6e;
1791 	i2c_r(gspca_dev, 0x00, 2);
1792 	val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1793 	reg_w1(gspca_dev, 0x01, 0x29);
1794 	reg_w1(gspca_dev, 0x17, 0x42);
1795 	if (gspca_dev->usb_err < 0)
1796 		return;
1797 	if (val == 0x2030) {
1798 		PDEBUG(D_PROBE, "Sensor po2030n");
1799 /*		sd->sensor = SENSOR_PO2030N; */
1800 	} else {
1801 		pr_err("Unknown sensor ID %04x\n", val);
1802 	}
1803 }
1804 
1805 /* this function is called at probe time */
1806 static int sd_config(struct gspca_dev *gspca_dev,
1807 			const struct usb_device_id *id)
1808 {
1809 	struct sd *sd = (struct sd *) gspca_dev;
1810 	struct cam *cam;
1811 
1812 	sd->bridge = id->driver_info >> 16;
1813 	sd->sensor = id->driver_info >> 8;
1814 	sd->flags = id->driver_info;
1815 
1816 	cam = &gspca_dev->cam;
1817 	if (sd->sensor == SENSOR_ADCM1700) {
1818 		cam->cam_mode = cif_mode;
1819 		cam->nmodes = ARRAY_SIZE(cif_mode);
1820 	} else {
1821 		cam->cam_mode = vga_mode;
1822 		cam->nmodes = ARRAY_SIZE(vga_mode);
1823 	}
1824 	cam->npkt = 24;			/* 24 packets per ISOC message */
1825 	cam->ctrls = sd->ctrls;
1826 
1827 	sd->ag_cnt = -1;
1828 	sd->quality = QUALITY_DEF;
1829 
1830 	INIT_WORK(&sd->work, qual_upd);
1831 
1832 	return 0;
1833 }
1834 
1835 /* this function is called at probe and resume time */
1836 static int sd_init(struct gspca_dev *gspca_dev)
1837 {
1838 	struct sd *sd = (struct sd *) gspca_dev;
1839 	const u8 *sn9c1xx;
1840 	u8 regGpio[] = { 0x29, 0x70 };		/* no audio */
1841 	u8 regF1;
1842 
1843 	/* setup a selector by bridge */
1844 	reg_w1(gspca_dev, 0xf1, 0x01);
1845 	reg_r(gspca_dev, 0x00, 1);
1846 	reg_w1(gspca_dev, 0xf1, 0x00);
1847 	reg_r(gspca_dev, 0x00, 1);		/* get sonix chip id */
1848 	regF1 = gspca_dev->usb_buf[0];
1849 	if (gspca_dev->usb_err < 0)
1850 		return gspca_dev->usb_err;
1851 	PDEBUG(D_PROBE, "Sonix chip id: %02x", regF1);
1852 	if (gspca_dev->audio)
1853 		regGpio[1] |= 0x04;		/* with audio */
1854 	switch (sd->bridge) {
1855 	case BRIDGE_SN9C102P:
1856 	case BRIDGE_SN9C105:
1857 		if (regF1 != 0x11)
1858 			return -ENODEV;
1859 		break;
1860 	default:
1861 /*	case BRIDGE_SN9C110: */
1862 /*	case BRIDGE_SN9C120: */
1863 		if (regF1 != 0x12)
1864 			return -ENODEV;
1865 	}
1866 
1867 	switch (sd->sensor) {
1868 	case SENSOR_MI0360:
1869 		mi0360_probe(gspca_dev);
1870 		break;
1871 	case SENSOR_OV7630:
1872 		ov7630_probe(gspca_dev);
1873 		break;
1874 	case SENSOR_OV7648:
1875 		ov7648_probe(gspca_dev);
1876 		break;
1877 	case SENSOR_PO2030N:
1878 		po2030n_probe(gspca_dev);
1879 		break;
1880 	}
1881 
1882 	switch (sd->bridge) {
1883 	case BRIDGE_SN9C102P:
1884 		reg_w1(gspca_dev, 0x02, regGpio[1]);
1885 		break;
1886 	default:
1887 		reg_w(gspca_dev, 0x01, regGpio, 2);
1888 		break;
1889 	}
1890 
1891 	if (sd->sensor == SENSOR_OM6802)
1892 		sd->ctrls[SHARPNESS].def = 0x10;
1893 
1894 	/* Note we do not disable the sensor clock here (power saving mode),
1895 	   as that also disables the button on the cam. */
1896 	reg_w1(gspca_dev, 0xf1, 0x00);
1897 
1898 	/* set the i2c address */
1899 	sn9c1xx = sn_tb[sd->sensor];
1900 	sd->i2c_addr = sn9c1xx[9];
1901 
1902 	gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
1903 	if (!(sd->flags & F_ILLUM))
1904 		gspca_dev->ctrl_dis |= (1 << ILLUM);
1905 
1906 	return gspca_dev->usb_err;
1907 }
1908 
1909 static u32 expo_adjust(struct gspca_dev *gspca_dev,
1910 			u32 expo)
1911 {
1912 	struct sd *sd = (struct sd *) gspca_dev;
1913 
1914 	switch (sd->sensor) {
1915 	case SENSOR_GC0307: {
1916 		int a, b;
1917 
1918 		/* expo = 0..255 -> a = 19..43 */
1919 		a = 19 + expo * 25 / 256;
1920 		i2c_w1(gspca_dev, 0x68, a);
1921 		a -= 12;
1922 		b = a * a * 4;			/* heuristic */
1923 		i2c_w1(gspca_dev, 0x03, b >> 8);
1924 		i2c_w1(gspca_dev, 0x04, b);
1925 		break;
1926 	    }
1927 	case SENSOR_HV7131R: {
1928 		u8 Expodoit[] =
1929 			{ 0xc1, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x16 };
1930 
1931 		Expodoit[3] = expo >> 16;
1932 		Expodoit[4] = expo >> 8;
1933 		Expodoit[5] = expo;
1934 		i2c_w8(gspca_dev, Expodoit);
1935 		break;
1936 	    }
1937 	case SENSOR_MI0360:
1938 	case SENSOR_MI0360B: {
1939 		u8 expoMi[] =		/* exposure 0x0635 -> 4 fp/s 0x10 */
1940 			{ 0xb1, 0x5d, 0x09, 0x00, 0x00, 0x00, 0x00, 0x16 };
1941 		static const u8 doit[] =		/* update sensor */
1942 			{ 0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10 };
1943 		static const u8 sensorgo[] =		/* sensor on */
1944 			{ 0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10 };
1945 
1946 		if (expo > 0x0635)
1947 			expo = 0x0635;
1948 		else if (expo < 0x0001)
1949 			expo = 0x0001;
1950 		expoMi[3] = expo >> 8;
1951 		expoMi[4] = expo;
1952 		i2c_w8(gspca_dev, expoMi);
1953 		i2c_w8(gspca_dev, doit);
1954 		i2c_w8(gspca_dev, sensorgo);
1955 		break;
1956 	    }
1957 	case SENSOR_MO4000: {
1958 		u8 expoMof[] =
1959 			{ 0xa1, 0x21, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x10 };
1960 		u8 expoMo10[] =
1961 			{ 0xa1, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10 };
1962 		static const u8 gainMo[] =
1963 			{ 0xa1, 0x21, 0x00, 0x10, 0x00, 0x00, 0x00, 0x1d };
1964 
1965 		if (expo > 0x1fff)
1966 			expo = 0x1fff;
1967 		else if (expo < 0x0001)
1968 			expo = 0x0001;
1969 		expoMof[3] = (expo & 0x03fc) >> 2;
1970 		i2c_w8(gspca_dev, expoMof);
1971 		expoMo10[3] = ((expo & 0x1c00) >> 10)
1972 				| ((expo & 0x0003) << 4);
1973 		i2c_w8(gspca_dev, expoMo10);
1974 		i2c_w8(gspca_dev, gainMo);
1975 		PDEBUG(D_FRAM, "set exposure %d",
1976 			((expoMo10[3] & 0x07) << 10)
1977 			| (expoMof[3] << 2)
1978 			| ((expoMo10[3] & 0x30) >> 4));
1979 		break;
1980 	    }
1981 	case SENSOR_MT9V111: {
1982 		u8 expo_c1[] =
1983 			{ 0xb1, 0x5c, 0x09, 0x00, 0x00, 0x00, 0x00, 0x10 };
1984 
1985 		if (expo > 0x0390)
1986 			expo = 0x0390;
1987 		else if (expo < 0x0060)
1988 			expo = 0x0060;
1989 		expo_c1[3] = expo >> 8;
1990 		expo_c1[4] = expo;
1991 		i2c_w8(gspca_dev, expo_c1);
1992 		break;
1993 	    }
1994 	case SENSOR_OM6802: {
1995 		u8 gainOm[] =
1996 			{ 0xa0, 0x34, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x10 };
1997 				/* preset AGC - works when AutoExpo = off */
1998 
1999 		if (expo > 0x03ff)
2000 			expo = 0x03ff;
2001 		 if (expo < 0x0001)
2002 			expo = 0x0001;
2003 		gainOm[3] = expo >> 2;
2004 		i2c_w8(gspca_dev, gainOm);
2005 		reg_w1(gspca_dev, 0x96, expo >> 5);
2006 		PDEBUG(D_FRAM, "set exposure %d", gainOm[3]);
2007 		break;
2008 	    }
2009 	}
2010 	return expo;
2011 }
2012 
2013 static void setbrightness(struct gspca_dev *gspca_dev)
2014 {
2015 	struct sd *sd = (struct sd *) gspca_dev;
2016 	unsigned int expo;
2017 	int brightness;
2018 	u8 k2;
2019 
2020 	brightness = sd->ctrls[BRIGHTNESS].val;
2021 	k2 = (brightness - 0x80) >> 2;
2022 	switch (sd->sensor) {
2023 	case SENSOR_ADCM1700:
2024 		if (k2 > 0x1f)
2025 			k2 = 0;		/* only positive Y offset */
2026 		break;
2027 	case SENSOR_HV7131R:
2028 		expo = brightness << 12;
2029 		if (expo > 0x002dc6c0)
2030 			expo = 0x002dc6c0;
2031 		else if (expo < 0x02a0)
2032 			expo = 0x02a0;
2033 		sd->exposure = expo_adjust(gspca_dev, expo);
2034 		break;
2035 	case SENSOR_MI0360:
2036 	case SENSOR_MO4000:
2037 		expo = brightness << 4;
2038 		sd->exposure = expo_adjust(gspca_dev, expo);
2039 		break;
2040 	case SENSOR_MI0360B:
2041 		expo = brightness << 2;
2042 		sd->exposure = expo_adjust(gspca_dev, expo);
2043 		break;
2044 	case SENSOR_GC0307:
2045 		expo = brightness;
2046 		sd->exposure = expo_adjust(gspca_dev, expo);
2047 		return;			/* don't set the Y offset */
2048 	case SENSOR_MT9V111:
2049 		expo = brightness << 2;
2050 		sd->exposure = expo_adjust(gspca_dev, expo);
2051 		return;			/* don't set the Y offset */
2052 	case SENSOR_OM6802:
2053 		expo = brightness << 2;
2054 		sd->exposure = expo_adjust(gspca_dev, expo);
2055 		return;			/* Y offset already set */
2056 	}
2057 
2058 	reg_w1(gspca_dev, 0x96, k2);	/* color matrix Y offset */
2059 }
2060 
2061 static void setcontrast(struct gspca_dev *gspca_dev)
2062 {
2063 	struct sd *sd = (struct sd *) gspca_dev;
2064 	u8 k2;
2065 	u8 contrast[6];
2066 
2067 	k2 = sd->ctrls[CONTRAST].val * 37 / (CONTRAST_MAX + 1)
2068 				+ 37;		/* 37..73 */
2069 	contrast[0] = (k2 + 1) / 2;		/* red */
2070 	contrast[1] = 0;
2071 	contrast[2] = k2;			/* green */
2072 	contrast[3] = 0;
2073 	contrast[4] = k2 / 5;			/* blue */
2074 	contrast[5] = 0;
2075 	reg_w(gspca_dev, 0x84, contrast, sizeof contrast);
2076 }
2077 
2078 static void setcolors(struct gspca_dev *gspca_dev)
2079 {
2080 	struct sd *sd = (struct sd *) gspca_dev;
2081 	int i, v, colors;
2082 	const s16 *uv;
2083 	u8 reg8a[12];			/* U & V gains */
2084 	static const s16 uv_com[6] = {	/* same as reg84 in signed decimal */
2085 		-24, -38, 64,		/* UR UG UB */
2086 		 62, -51, -9		/* VR VG VB */
2087 	};
2088 	static const s16 uv_mi0360b[6] = {
2089 		-20, -38, 64,		/* UR UG UB */
2090 		 60, -51, -9		/* VR VG VB */
2091 	};
2092 
2093 	colors = sd->ctrls[COLORS].val;
2094 	if (sd->sensor == SENSOR_MI0360B)
2095 		uv = uv_mi0360b;
2096 	else
2097 		uv = uv_com;
2098 	for (i = 0; i < 6; i++) {
2099 		v = uv[i] * colors / COLORS_DEF;
2100 		reg8a[i * 2] = v;
2101 		reg8a[i * 2 + 1] = (v >> 8) & 0x0f;
2102 	}
2103 	reg_w(gspca_dev, 0x8a, reg8a, sizeof reg8a);
2104 }
2105 
2106 static void setredblue(struct gspca_dev *gspca_dev)
2107 {
2108 	struct sd *sd = (struct sd *) gspca_dev;
2109 
2110 	if (sd->sensor == SENSOR_PO2030N) {
2111 		u8 rg1b[] =		/* red  green1 blue (no g2) */
2112 			{0xc1, 0x6e, 0x16, 0x00, 0x40, 0x00, 0x00, 0x10};
2113 
2114 		/* 0x40 = normal value = gain x 1 */
2115 		rg1b[3] = sd->ctrls[RED].val * 2;
2116 		rg1b[5] = sd->ctrls[BLUE].val * 2;
2117 		i2c_w8(gspca_dev, rg1b);
2118 		return;
2119 	}
2120 	reg_w1(gspca_dev, 0x05, sd->ctrls[RED].val);
2121 /*	reg_w1(gspca_dev, 0x07, 32); */
2122 	reg_w1(gspca_dev, 0x06, sd->ctrls[BLUE].val);
2123 }
2124 
2125 static void setgamma(struct gspca_dev *gspca_dev)
2126 {
2127 	struct sd *sd = (struct sd *) gspca_dev;
2128 	int i, val;
2129 	u8 gamma[17];
2130 	const u8 *gamma_base;
2131 	static const u8 delta[17] = {
2132 		0x00, 0x14, 0x1c, 0x1c, 0x1c, 0x1c, 0x1b, 0x1a,
2133 		0x18, 0x13, 0x10, 0x0e, 0x08, 0x07, 0x04, 0x02, 0x00
2134 	};
2135 
2136 	switch (sd->sensor) {
2137 	case SENSOR_ADCM1700:
2138 		gamma_base = gamma_spec_0;
2139 		break;
2140 	case SENSOR_HV7131R:
2141 	case SENSOR_MI0360B:
2142 	case SENSOR_MT9V111:
2143 		gamma_base = gamma_spec_1;
2144 		break;
2145 	case SENSOR_GC0307:
2146 		gamma_base = gamma_spec_2;
2147 		break;
2148 	case SENSOR_SP80708:
2149 		gamma_base = gamma_spec_3;
2150 		break;
2151 	default:
2152 		gamma_base = gamma_def;
2153 		break;
2154 	}
2155 
2156 	val = sd->ctrls[GAMMA].val;
2157 	for (i = 0; i < sizeof gamma; i++)
2158 		gamma[i] = gamma_base[i]
2159 			+ delta[i] * (val - GAMMA_DEF) / 32;
2160 	reg_w(gspca_dev, 0x20, gamma, sizeof gamma);
2161 }
2162 
2163 static void setexposure(struct gspca_dev *gspca_dev)
2164 {
2165 	struct sd *sd = (struct sd *) gspca_dev;
2166 
2167 	if (sd->sensor == SENSOR_PO2030N) {
2168 		u8 rexpo[] =		/* 1a: expo H, 1b: expo M */
2169 			{0xa1, 0x6e, 0x1a, 0x00, 0x40, 0x00, 0x00, 0x10};
2170 
2171 		rexpo[3] = sd->ctrls[EXPOSURE].val >> 8;
2172 		i2c_w8(gspca_dev, rexpo);
2173 		msleep(6);
2174 		rexpo[2] = 0x1b;
2175 		rexpo[3] = sd->ctrls[EXPOSURE].val;
2176 		i2c_w8(gspca_dev, rexpo);
2177 	}
2178 }
2179 
2180 static void setautogain(struct gspca_dev *gspca_dev)
2181 {
2182 	struct sd *sd = (struct sd *) gspca_dev;
2183 
2184 	if (gspca_dev->ctrl_dis & (1 << AUTOGAIN))
2185 		return;
2186 	switch (sd->sensor) {
2187 	case SENSOR_OV7630:
2188 	case SENSOR_OV7648: {
2189 		u8 comb;
2190 
2191 		if (sd->sensor == SENSOR_OV7630)
2192 			comb = 0xc0;
2193 		else
2194 			comb = 0xa0;
2195 		if (sd->ctrls[AUTOGAIN].val)
2196 			comb |= 0x03;
2197 		i2c_w1(&sd->gspca_dev, 0x13, comb);
2198 		return;
2199 	    }
2200 	}
2201 	if (sd->ctrls[AUTOGAIN].val)
2202 		sd->ag_cnt = AG_CNT_START;
2203 	else
2204 		sd->ag_cnt = -1;
2205 }
2206 
2207 static void setgain(struct gspca_dev *gspca_dev)
2208 {
2209 	struct sd *sd = (struct sd *) gspca_dev;
2210 
2211 	if (sd->sensor == SENSOR_PO2030N) {
2212 		u8 rgain[] =		/* 15: gain */
2213 			{0xa1, 0x6e, 0x15, 0x00, 0x40, 0x00, 0x00, 0x15};
2214 
2215 		rgain[3] = sd->ctrls[GAIN].val;
2216 		i2c_w8(gspca_dev, rgain);
2217 	}
2218 }
2219 
2220 static void sethvflip(struct gspca_dev *gspca_dev)
2221 {
2222 	struct sd *sd = (struct sd *) gspca_dev;
2223 	u8 comn;
2224 
2225 	switch (sd->sensor) {
2226 	case SENSOR_HV7131R:
2227 		comn = 0x18;			/* clkdiv = 1, ablcen = 1 */
2228 		if (sd->ctrls[VFLIP].val)
2229 			comn |= 0x01;
2230 		i2c_w1(gspca_dev, 0x01, comn);	/* sctra */
2231 		break;
2232 	case SENSOR_OV7630:
2233 		comn = 0x02;
2234 		if (!sd->ctrls[VFLIP].val)
2235 			comn |= 0x80;
2236 		i2c_w1(gspca_dev, 0x75, comn);
2237 		break;
2238 	case SENSOR_OV7648:
2239 		comn = 0x06;
2240 		if (sd->ctrls[VFLIP].val)
2241 			comn |= 0x80;
2242 		i2c_w1(gspca_dev, 0x75, comn);
2243 		break;
2244 	case SENSOR_PO2030N:
2245 		/* Reg. 0x1E: Timing Generator Control Register 2 (Tgcontrol2)
2246 		 * (reset value: 0x0A)
2247 		 * bit7: HM: Horizontal Mirror: 0: disable, 1: enable
2248 		 * bit6: VM: Vertical Mirror: 0: disable, 1: enable
2249 		 * bit5: ST: Shutter Selection: 0: electrical, 1: mechanical
2250 		 * bit4: FT: Single Frame Transfer: 0: disable, 1: enable
2251 		 * bit3-0: X
2252 		 */
2253 		comn = 0x0a;
2254 		if (sd->ctrls[HFLIP].val)
2255 			comn |= 0x80;
2256 		if (sd->ctrls[VFLIP].val)
2257 			comn |= 0x40;
2258 		i2c_w1(&sd->gspca_dev, 0x1e, comn);
2259 		break;
2260 	}
2261 }
2262 
2263 static void setsharpness(struct gspca_dev *gspca_dev)
2264 {
2265 	struct sd *sd = (struct sd *) gspca_dev;
2266 
2267 	reg_w1(gspca_dev, 0x99, sd->ctrls[SHARPNESS].val);
2268 }
2269 
2270 static void setillum(struct gspca_dev *gspca_dev)
2271 {
2272 	struct sd *sd = (struct sd *) gspca_dev;
2273 
2274 	if (gspca_dev->ctrl_dis & (1 << ILLUM))
2275 		return;
2276 	switch (sd->sensor) {
2277 	case SENSOR_ADCM1700:
2278 		reg_w1(gspca_dev, 0x02,				/* gpio */
2279 			sd->ctrls[ILLUM].val ? 0x64 : 0x60);
2280 		break;
2281 	case SENSOR_MT9V111:
2282 		reg_w1(gspca_dev, 0x02,
2283 			sd->ctrls[ILLUM].val ? 0x77 : 0x74);
2284 /* should have been: */
2285 /*						0x55 : 0x54);	* 370i */
2286 /*						0x66 : 0x64);	* Clip */
2287 		break;
2288 	}
2289 }
2290 
2291 static void setfreq(struct gspca_dev *gspca_dev)
2292 {
2293 	struct sd *sd = (struct sd *) gspca_dev;
2294 
2295 	if (gspca_dev->ctrl_dis & (1 << FREQ))
2296 		return;
2297 	if (sd->sensor == SENSOR_OV7660) {
2298 		u8 com8;
2299 
2300 		com8 = 0xdf;		/* auto gain/wb/expo */
2301 		switch (sd->ctrls[FREQ].val) {
2302 		case 0: /* Banding filter disabled */
2303 			i2c_w1(gspca_dev, 0x13, com8 | 0x20);
2304 			break;
2305 		case 1: /* 50 hz */
2306 			i2c_w1(gspca_dev, 0x13, com8);
2307 			i2c_w1(gspca_dev, 0x3b, 0x0a);
2308 			break;
2309 		case 2: /* 60 hz */
2310 			i2c_w1(gspca_dev, 0x13, com8);
2311 			i2c_w1(gspca_dev, 0x3b, 0x02);
2312 			break;
2313 		}
2314 	} else {
2315 		u8 reg2a = 0, reg2b = 0, reg2d = 0;
2316 
2317 		/* Get reg2a / reg2d base values */
2318 		switch (sd->sensor) {
2319 		case SENSOR_OV7630:
2320 			reg2a = 0x08;
2321 			reg2d = 0x01;
2322 			break;
2323 		case SENSOR_OV7648:
2324 			reg2a = 0x11;
2325 			reg2d = 0x81;
2326 			break;
2327 		}
2328 
2329 		switch (sd->ctrls[FREQ].val) {
2330 		case 0: /* Banding filter disabled */
2331 			break;
2332 		case 1: /* 50 hz (filter on and framerate adj) */
2333 			reg2a |= 0x80;
2334 			reg2b = 0xac;
2335 			reg2d |= 0x04;
2336 			break;
2337 		case 2: /* 60 hz (filter on, no framerate adj) */
2338 			reg2a |= 0x80;
2339 			reg2d |= 0x04;
2340 			break;
2341 		}
2342 		i2c_w1(gspca_dev, 0x2a, reg2a);
2343 		i2c_w1(gspca_dev, 0x2b, reg2b);
2344 		i2c_w1(gspca_dev, 0x2d, reg2d);
2345 	}
2346 }
2347 
2348 static void setjpegqual(struct gspca_dev *gspca_dev)
2349 {
2350 	struct sd *sd = (struct sd *) gspca_dev;
2351 
2352 	jpeg_set_qual(sd->jpeg_hdr, sd->quality);
2353 #if USB_BUF_SZ < 64
2354 #error "No room enough in usb_buf for quantization table"
2355 #endif
2356 	memcpy(gspca_dev->usb_buf, &sd->jpeg_hdr[JPEG_QT0_OFFSET], 64);
2357 	usb_control_msg(gspca_dev->dev,
2358 			usb_sndctrlpipe(gspca_dev->dev, 0),
2359 			0x08,
2360 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
2361 			0x0100, 0,
2362 			gspca_dev->usb_buf, 64,
2363 			500);
2364 	memcpy(gspca_dev->usb_buf, &sd->jpeg_hdr[JPEG_QT1_OFFSET], 64);
2365 	usb_control_msg(gspca_dev->dev,
2366 			usb_sndctrlpipe(gspca_dev->dev, 0),
2367 			0x08,
2368 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
2369 			0x0140, 0,
2370 			gspca_dev->usb_buf, 64,
2371 			500);
2372 
2373 	sd->reg18 ^= 0x40;
2374 	reg_w1(gspca_dev, 0x18, sd->reg18);
2375 }
2376 
2377 /* JPEG quality update */
2378 /* This function is executed from a work queue. */
2379 static void qual_upd(struct work_struct *work)
2380 {
2381 	struct sd *sd = container_of(work, struct sd, work);
2382 	struct gspca_dev *gspca_dev = &sd->gspca_dev;
2383 
2384 	/* To protect gspca_dev->usb_buf and gspca_dev->usb_err */
2385 	mutex_lock(&gspca_dev->usb_lock);
2386 	PDEBUG(D_STREAM, "qual_upd %d%%", sd->quality);
2387 	gspca_dev->usb_err = 0;
2388 	setjpegqual(gspca_dev);
2389 	mutex_unlock(&gspca_dev->usb_lock);
2390 }
2391 
2392 /* -- start the camera -- */
2393 static int sd_start(struct gspca_dev *gspca_dev)
2394 {
2395 	struct sd *sd = (struct sd *) gspca_dev;
2396 	int i;
2397 	u8 reg01, reg17;
2398 	u8 reg0102[2];
2399 	const u8 *sn9c1xx;
2400 	const u8 (*init)[8];
2401 	const u8 *reg9a;
2402 	int mode;
2403 	static const u8 reg9a_def[] =
2404 		{0x00, 0x40, 0x20, 0x00, 0x00, 0x00};
2405 	static const u8 reg9a_spec[] =
2406 		{0x00, 0x40, 0x38, 0x30, 0x00, 0x20};
2407 	static const u8 regd4[] = {0x60, 0x00, 0x00};
2408 	static const u8 C0[] = { 0x2d, 0x2d, 0x3a, 0x05, 0x04, 0x3f };
2409 	static const u8 CA[] = { 0x28, 0xd8, 0x14, 0xec };
2410 	static const u8 CA_adcm1700[] =
2411 				{ 0x14, 0xec, 0x0a, 0xf6 };
2412 	static const u8 CA_po2030n[] =
2413 				{ 0x1e, 0xe2, 0x14, 0xec };
2414 	static const u8 CE[] = { 0x32, 0xdd, 0x2d, 0xdd };	/* MI0360 */
2415 	static const u8 CE_gc0307[] =
2416 				{ 0x32, 0xce, 0x2d, 0xd3 };
2417 	static const u8 CE_ov76xx[] =
2418 				{ 0x32, 0xdd, 0x32, 0xdd };
2419 	static const u8 CE_po2030n[] =
2420 				{ 0x14, 0xe7, 0x1e, 0xdd };
2421 
2422 	/* create the JPEG header */
2423 	jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
2424 			0x21);		/* JPEG 422 */
2425 
2426 	/* initialize the bridge */
2427 	sn9c1xx = sn_tb[sd->sensor];
2428 
2429 	/* sensor clock already enabled in sd_init */
2430 	/* reg_w1(gspca_dev, 0xf1, 0x00); */
2431 	reg01 = sn9c1xx[1];
2432 	if (sd->flags & F_PDN_INV)
2433 		reg01 ^= S_PDN_INV;		/* power down inverted */
2434 	reg_w1(gspca_dev, 0x01, reg01);
2435 
2436 	/* configure gpio */
2437 	reg0102[0] = reg01;
2438 	reg0102[1] = sn9c1xx[2];
2439 	if (gspca_dev->audio)
2440 		reg0102[1] |= 0x04;	/* keep the audio connection */
2441 	reg_w(gspca_dev, 0x01, reg0102, 2);
2442 	reg_w(gspca_dev, 0x08, &sn9c1xx[8], 2);
2443 	reg_w(gspca_dev, 0x17, &sn9c1xx[0x17], 5);
2444 	switch (sd->sensor) {
2445 	case SENSOR_GC0307:
2446 	case SENSOR_OV7660:
2447 	case SENSOR_PO1030:
2448 	case SENSOR_PO2030N:
2449 	case SENSOR_SOI768:
2450 	case SENSOR_SP80708:
2451 		reg9a = reg9a_spec;
2452 		break;
2453 	default:
2454 		reg9a = reg9a_def;
2455 		break;
2456 	}
2457 	reg_w(gspca_dev, 0x9a, reg9a, 6);
2458 
2459 	reg_w(gspca_dev, 0xd4, regd4, sizeof regd4);
2460 
2461 	reg_w(gspca_dev, 0x03, &sn9c1xx[3], 0x0f);
2462 
2463 	reg17 = sn9c1xx[0x17];
2464 	switch (sd->sensor) {
2465 	case SENSOR_GC0307:
2466 		msleep(50);		/*fixme: is it useful? */
2467 		break;
2468 	case SENSOR_OM6802:
2469 		msleep(10);
2470 		reg_w1(gspca_dev, 0x02, 0x73);
2471 		reg17 |= SEN_CLK_EN;
2472 		reg_w1(gspca_dev, 0x17, reg17);
2473 		reg_w1(gspca_dev, 0x01, 0x22);
2474 		msleep(100);
2475 		reg01 = SCL_SEL_OD | S_PDN_INV;
2476 		reg17 &= ~MCK_SIZE_MASK;
2477 		reg17 |= 0x04;		/* clock / 4 */
2478 		break;
2479 	}
2480 	reg01 |= SYS_SEL_48M;
2481 	reg_w1(gspca_dev, 0x01, reg01);
2482 	reg17 |= SEN_CLK_EN;
2483 	reg_w1(gspca_dev, 0x17, reg17);
2484 	reg01 &= ~S_PWR_DN;		/* sensor power on */
2485 	reg_w1(gspca_dev, 0x01, reg01);
2486 	reg01 &= ~SCL_SEL_OD;		/* remove open-drain mode */
2487 	reg_w1(gspca_dev, 0x01, reg01);
2488 
2489 	switch (sd->sensor) {
2490 	case SENSOR_HV7131R:
2491 		hv7131r_probe(gspca_dev);	/*fixme: is it useful? */
2492 		break;
2493 	case SENSOR_OM6802:
2494 		msleep(10);
2495 		reg_w1(gspca_dev, 0x01, reg01);
2496 		i2c_w8(gspca_dev, om6802_init0[0]);
2497 		i2c_w8(gspca_dev, om6802_init0[1]);
2498 		msleep(15);
2499 		reg_w1(gspca_dev, 0x02, 0x71);
2500 		msleep(150);
2501 		break;
2502 	case SENSOR_SP80708:
2503 		msleep(100);
2504 		reg_w1(gspca_dev, 0x02, 0x62);
2505 		break;
2506 	}
2507 
2508 	/* initialize the sensor */
2509 	i2c_w_seq(gspca_dev, sensor_init[sd->sensor]);
2510 
2511 	reg_w1(gspca_dev, 0x15, sn9c1xx[0x15]);
2512 	reg_w1(gspca_dev, 0x16, sn9c1xx[0x16]);
2513 	reg_w1(gspca_dev, 0x12, sn9c1xx[0x12]);
2514 	reg_w1(gspca_dev, 0x13, sn9c1xx[0x13]);
2515 	reg_w1(gspca_dev, 0x18, sn9c1xx[0x18]);
2516 	if (sd->sensor == SENSOR_ADCM1700) {
2517 		reg_w1(gspca_dev, 0xd2, 0x3a);	/* AE_H_SIZE = 116 */
2518 		reg_w1(gspca_dev, 0xd3, 0x30);	/* AE_V_SIZE = 96 */
2519 	} else {
2520 		reg_w1(gspca_dev, 0xd2, 0x6a);	/* AE_H_SIZE = 212 */
2521 		reg_w1(gspca_dev, 0xd3, 0x50);	/* AE_V_SIZE = 160 */
2522 	}
2523 	reg_w1(gspca_dev, 0xc6, 0x00);
2524 	reg_w1(gspca_dev, 0xc7, 0x00);
2525 	if (sd->sensor == SENSOR_ADCM1700) {
2526 		reg_w1(gspca_dev, 0xc8, 0x2c);	/* AW_H_STOP = 352 */
2527 		reg_w1(gspca_dev, 0xc9, 0x24);	/* AW_V_STOP = 288 */
2528 	} else {
2529 		reg_w1(gspca_dev, 0xc8, 0x50);	/* AW_H_STOP = 640 */
2530 		reg_w1(gspca_dev, 0xc9, 0x3c);	/* AW_V_STOP = 480 */
2531 	}
2532 	reg_w1(gspca_dev, 0x18, sn9c1xx[0x18]);
2533 	switch (sd->sensor) {
2534 	case SENSOR_OM6802:
2535 /*	case SENSOR_OV7648:		* fixme: sometimes */
2536 		break;
2537 	default:
2538 		reg17 |= DEF_EN;
2539 		break;
2540 	}
2541 	reg_w1(gspca_dev, 0x17, reg17);
2542 
2543 	reg_w1(gspca_dev, 0x05, 0x00);		/* red */
2544 	reg_w1(gspca_dev, 0x07, 0x00);		/* green */
2545 	reg_w1(gspca_dev, 0x06, 0x00);		/* blue */
2546 	reg_w1(gspca_dev, 0x14, sn9c1xx[0x14]);
2547 
2548 	setgamma(gspca_dev);
2549 
2550 /*fixme: 8 times with all zeroes and 1 or 2 times with normal values */
2551 	for (i = 0; i < 8; i++)
2552 		reg_w(gspca_dev, 0x84, reg84, sizeof reg84);
2553 	switch (sd->sensor) {
2554 	case SENSOR_ADCM1700:
2555 	case SENSOR_OV7660:
2556 	case SENSOR_SP80708:
2557 		reg_w1(gspca_dev, 0x9a, 0x05);
2558 		break;
2559 	case SENSOR_GC0307:
2560 	case SENSOR_MT9V111:
2561 	case SENSOR_MI0360B:
2562 		reg_w1(gspca_dev, 0x9a, 0x07);
2563 		break;
2564 	case SENSOR_OV7630:
2565 	case SENSOR_OV7648:
2566 		reg_w1(gspca_dev, 0x9a, 0x0a);
2567 		break;
2568 	case SENSOR_PO2030N:
2569 	case SENSOR_SOI768:
2570 		reg_w1(gspca_dev, 0x9a, 0x06);
2571 		break;
2572 	default:
2573 		reg_w1(gspca_dev, 0x9a, 0x08);
2574 		break;
2575 	}
2576 	setsharpness(gspca_dev);
2577 
2578 	reg_w(gspca_dev, 0x84, reg84, sizeof reg84);
2579 	reg_w1(gspca_dev, 0x05, 0x20);		/* red */
2580 	reg_w1(gspca_dev, 0x07, 0x20);		/* green */
2581 	reg_w1(gspca_dev, 0x06, 0x20);		/* blue */
2582 
2583 	init = NULL;
2584 	mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
2585 	reg01 |= SYS_SEL_48M | V_TX_EN;
2586 	reg17 &= ~MCK_SIZE_MASK;
2587 	reg17 |= 0x02;			/* clock / 2 */
2588 	switch (sd->sensor) {
2589 	case SENSOR_ADCM1700:
2590 		init = adcm1700_sensor_param1;
2591 		break;
2592 	case SENSOR_GC0307:
2593 		init = gc0307_sensor_param1;
2594 		break;
2595 	case SENSOR_HV7131R:
2596 	case SENSOR_MI0360:
2597 		if (!mode)
2598 			reg01 &= ~SYS_SEL_48M;	/* 640x480: clk 24Mhz */
2599 		reg17 &= ~MCK_SIZE_MASK;
2600 		reg17 |= 0x01;			/* clock / 1 */
2601 		break;
2602 	case SENSOR_MI0360B:
2603 		init = mi0360b_sensor_param1;
2604 		break;
2605 	case SENSOR_MO4000:
2606 		if (mode) {			/* if 320x240 */
2607 			reg01 &= ~SYS_SEL_48M;	/* clk 24Mz */
2608 			reg17 &= ~MCK_SIZE_MASK;
2609 			reg17 |= 0x01;		/* clock / 1 */
2610 		}
2611 		break;
2612 	case SENSOR_MT9V111:
2613 		init = mt9v111_sensor_param1;
2614 		break;
2615 	case SENSOR_OM6802:
2616 		init = om6802_sensor_param1;
2617 		if (!mode) {			/* if 640x480 */
2618 			reg17 &= ~MCK_SIZE_MASK;
2619 			reg17 |= 0x04;		/* clock / 4 */
2620 		} else {
2621 			reg01 &= ~SYS_SEL_48M;	/* clk 24Mz */
2622 			reg17 &= ~MCK_SIZE_MASK;
2623 			reg17 |= 0x02;		/* clock / 2 */
2624 		}
2625 		break;
2626 	case SENSOR_OV7630:
2627 		init = ov7630_sensor_param1;
2628 		break;
2629 	case SENSOR_OV7648:
2630 		init = ov7648_sensor_param1;
2631 		reg17 &= ~MCK_SIZE_MASK;
2632 		reg17 |= 0x01;			/* clock / 1 */
2633 		break;
2634 	case SENSOR_OV7660:
2635 		init = ov7660_sensor_param1;
2636 		break;
2637 	case SENSOR_PO1030:
2638 		init = po1030_sensor_param1;
2639 		break;
2640 	case SENSOR_PO2030N:
2641 		init = po2030n_sensor_param1;
2642 		break;
2643 	case SENSOR_SOI768:
2644 		init = soi768_sensor_param1;
2645 		break;
2646 	case SENSOR_SP80708:
2647 		init = sp80708_sensor_param1;
2648 		break;
2649 	}
2650 
2651 	/* more sensor initialization - param1 */
2652 	if (init != NULL) {
2653 		i2c_w_seq(gspca_dev, init);
2654 /*		init = NULL; */
2655 	}
2656 
2657 	reg_w(gspca_dev, 0xc0, C0, 6);
2658 	switch (sd->sensor) {
2659 	case SENSOR_ADCM1700:
2660 	case SENSOR_GC0307:
2661 	case SENSOR_SOI768:
2662 		reg_w(gspca_dev, 0xca, CA_adcm1700, 4);
2663 		break;
2664 	case SENSOR_PO2030N:
2665 		reg_w(gspca_dev, 0xca, CA_po2030n, 4);
2666 		break;
2667 	default:
2668 		reg_w(gspca_dev, 0xca, CA, 4);
2669 		break;
2670 	}
2671 	switch (sd->sensor) {
2672 	case SENSOR_ADCM1700:
2673 	case SENSOR_OV7630:
2674 	case SENSOR_OV7648:
2675 	case SENSOR_OV7660:
2676 	case SENSOR_SOI768:
2677 		reg_w(gspca_dev, 0xce, CE_ov76xx, 4);
2678 		break;
2679 	case SENSOR_GC0307:
2680 		reg_w(gspca_dev, 0xce, CE_gc0307, 4);
2681 		break;
2682 	case SENSOR_PO2030N:
2683 		reg_w(gspca_dev, 0xce, CE_po2030n, 4);
2684 		break;
2685 	default:
2686 		reg_w(gspca_dev, 0xce, CE, 4);
2687 					/* ?? {0x1e, 0xdd, 0x2d, 0xe7} */
2688 		break;
2689 	}
2690 
2691 	/* here change size mode 0 -> VGA; 1 -> CIF */
2692 	sd->reg18 = sn9c1xx[0x18] | (mode << 4) | 0x40;
2693 	reg_w1(gspca_dev, 0x18, sd->reg18);
2694 	setjpegqual(gspca_dev);
2695 
2696 	reg_w1(gspca_dev, 0x17, reg17);
2697 	reg_w1(gspca_dev, 0x01, reg01);
2698 	sd->reg01 = reg01;
2699 	sd->reg17 = reg17;
2700 
2701 	sethvflip(gspca_dev);
2702 	setbrightness(gspca_dev);
2703 	setcontrast(gspca_dev);
2704 	setcolors(gspca_dev);
2705 	setautogain(gspca_dev);
2706 	if (!(gspca_dev->ctrl_inac & ((1 << EXPOSURE) | (1 << GAIN)))) {
2707 		setexposure(gspca_dev);
2708 		setgain(gspca_dev);
2709 	}
2710 	setfreq(gspca_dev);
2711 
2712 	sd->pktsz = sd->npkt = 0;
2713 	sd->nchg = sd->short_mark = 0;
2714 	sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
2715 
2716 	return gspca_dev->usb_err;
2717 }
2718 
2719 static void sd_stopN(struct gspca_dev *gspca_dev)
2720 {
2721 	struct sd *sd = (struct sd *) gspca_dev;
2722 	static const u8 stophv7131[] =
2723 		{ 0xa1, 0x11, 0x02, 0x09, 0x00, 0x00, 0x00, 0x10 };
2724 	static const u8 stopmi0360[] =
2725 		{ 0xb1, 0x5d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10 };
2726 	static const u8 stopov7648[] =
2727 		{ 0xa1, 0x21, 0x76, 0x20, 0x00, 0x00, 0x00, 0x10 };
2728 	static const u8 stopsoi768[] =
2729 		{ 0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10 };
2730 	u8 reg01;
2731 	u8 reg17;
2732 
2733 	reg01 = sd->reg01;
2734 	reg17 = sd->reg17 & ~SEN_CLK_EN;
2735 	switch (sd->sensor) {
2736 	case SENSOR_ADCM1700:
2737 	case SENSOR_GC0307:
2738 	case SENSOR_PO2030N:
2739 	case SENSOR_SP80708:
2740 		reg01 |= LED;
2741 		reg_w1(gspca_dev, 0x01, reg01);
2742 		reg01 &= ~(LED | V_TX_EN);
2743 		reg_w1(gspca_dev, 0x01, reg01);
2744 /*		reg_w1(gspca_dev, 0x02, 0x??);	 * LED off ? */
2745 		break;
2746 	case SENSOR_HV7131R:
2747 		reg01 &= ~V_TX_EN;
2748 		reg_w1(gspca_dev, 0x01, reg01);
2749 		i2c_w8(gspca_dev, stophv7131);
2750 		break;
2751 	case SENSOR_MI0360:
2752 	case SENSOR_MI0360B:
2753 		reg01 &= ~V_TX_EN;
2754 		reg_w1(gspca_dev, 0x01, reg01);
2755 /*		reg_w1(gspca_dev, 0x02, 0x40);	  * LED off ? */
2756 		i2c_w8(gspca_dev, stopmi0360);
2757 		break;
2758 	case SENSOR_MT9V111:
2759 	case SENSOR_OM6802:
2760 	case SENSOR_PO1030:
2761 		reg01 &= ~V_TX_EN;
2762 		reg_w1(gspca_dev, 0x01, reg01);
2763 		break;
2764 	case SENSOR_OV7630:
2765 	case SENSOR_OV7648:
2766 		reg01 &= ~V_TX_EN;
2767 		reg_w1(gspca_dev, 0x01, reg01);
2768 		i2c_w8(gspca_dev, stopov7648);
2769 		break;
2770 	case SENSOR_OV7660:
2771 		reg01 &= ~V_TX_EN;
2772 		reg_w1(gspca_dev, 0x01, reg01);
2773 		break;
2774 	case SENSOR_SOI768:
2775 		i2c_w8(gspca_dev, stopsoi768);
2776 		break;
2777 	}
2778 
2779 	reg01 |= SCL_SEL_OD;
2780 	reg_w1(gspca_dev, 0x01, reg01);
2781 	reg01 |= S_PWR_DN;		/* sensor power down */
2782 	reg_w1(gspca_dev, 0x01, reg01);
2783 	reg_w1(gspca_dev, 0x17, reg17);
2784 	reg01 &= ~SYS_SEL_48M;		/* clock 24MHz */
2785 	reg_w1(gspca_dev, 0x01, reg01);
2786 	reg01 |= LED;
2787 	reg_w1(gspca_dev, 0x01, reg01);
2788 	/* Don't disable sensor clock as that disables the button on the cam */
2789 	/* reg_w1(gspca_dev, 0xf1, 0x01); */
2790 }
2791 
2792 /* called on streamoff with alt==0 and on disconnect */
2793 /* the usb_lock is held at entry - restore on exit */
2794 static void sd_stop0(struct gspca_dev *gspca_dev)
2795 {
2796 	struct sd *sd = (struct sd *) gspca_dev;
2797 
2798 	if (sd->work_thread != NULL) {
2799 		mutex_unlock(&gspca_dev->usb_lock);
2800 		destroy_workqueue(sd->work_thread);
2801 		mutex_lock(&gspca_dev->usb_lock);
2802 		sd->work_thread = NULL;
2803 	}
2804 }
2805 
2806 #define WANT_REGULAR_AUTOGAIN
2807 #include "autogain_functions.h"
2808 
2809 static void do_autogain(struct gspca_dev *gspca_dev)
2810 {
2811 	struct sd *sd = (struct sd *) gspca_dev;
2812 	int delta;
2813 	int expotimes;
2814 	u8 luma_mean = 130;
2815 	u8 luma_delta = 20;
2816 
2817 	/* Thanks S., without your advice, autobright should not work :) */
2818 	if (sd->ag_cnt < 0)
2819 		return;
2820 	if (--sd->ag_cnt >= 0)
2821 		return;
2822 	sd->ag_cnt = AG_CNT_START;
2823 
2824 	delta = atomic_read(&sd->avg_lum);
2825 	PDEBUG(D_FRAM, "mean lum %d", delta);
2826 
2827 	if (sd->sensor == SENSOR_PO2030N) {
2828 		auto_gain_n_exposure(gspca_dev, delta, luma_mean, luma_delta,
2829 					15, 1024);
2830 		return;
2831 	}
2832 
2833 	if (delta < luma_mean - luma_delta ||
2834 	    delta > luma_mean + luma_delta) {
2835 		switch (sd->sensor) {
2836 		case SENSOR_GC0307:
2837 			expotimes = sd->exposure;
2838 			expotimes += (luma_mean - delta) >> 6;
2839 			if (expotimes < 0)
2840 				expotimes = 0;
2841 			sd->exposure = expo_adjust(gspca_dev,
2842 						   (unsigned int) expotimes);
2843 			break;
2844 		case SENSOR_HV7131R:
2845 			expotimes = sd->exposure >> 8;
2846 			expotimes += (luma_mean - delta) >> 4;
2847 			if (expotimes < 0)
2848 				expotimes = 0;
2849 			sd->exposure = expo_adjust(gspca_dev,
2850 					(unsigned int) (expotimes << 8));
2851 			break;
2852 		case SENSOR_OM6802:
2853 		case SENSOR_MT9V111:
2854 			expotimes = sd->exposure;
2855 			expotimes += (luma_mean - delta) >> 2;
2856 			if (expotimes < 0)
2857 				expotimes = 0;
2858 			sd->exposure = expo_adjust(gspca_dev,
2859 						   (unsigned int) expotimes);
2860 			setredblue(gspca_dev);
2861 			break;
2862 		default:
2863 /*		case SENSOR_MO4000: */
2864 /*		case SENSOR_MI0360: */
2865 /*		case SENSOR_MI0360B: */
2866 			expotimes = sd->exposure;
2867 			expotimes += (luma_mean - delta) >> 6;
2868 			if (expotimes < 0)
2869 				expotimes = 0;
2870 			sd->exposure = expo_adjust(gspca_dev,
2871 						   (unsigned int) expotimes);
2872 			setredblue(gspca_dev);
2873 			break;
2874 		}
2875 	}
2876 }
2877 
2878 /* set the average luminosity from an isoc marker */
2879 static void set_lum(struct sd *sd,
2880 		    u8 *data)
2881 {
2882 	int avg_lum;
2883 
2884 	/*	w0 w1 w2
2885 	 *	w3 w4 w5
2886 	 *	w6 w7 w8
2887 	 */
2888 	avg_lum = (data[27] << 8) + data[28]		/* w3 */
2889 
2890 		+ (data[31] << 8) + data[32]		/* w5 */
2891 
2892 		+ (data[23] << 8) + data[24]		/* w1 */
2893 
2894 		+ (data[35] << 8) + data[36]		/* w7 */
2895 
2896 		+ (data[29] << 10) + (data[30] << 2);	/* w4 * 4 */
2897 	avg_lum >>= 10;
2898 	atomic_set(&sd->avg_lum, avg_lum);
2899 }
2900 
2901 /* scan the URB packets */
2902 /* This function is run at interrupt level. */
2903 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2904 			u8 *data,			/* isoc packet */
2905 			int len)			/* iso packet length */
2906 {
2907 	struct sd *sd = (struct sd *) gspca_dev;
2908 	int i, new_qual;
2909 
2910 	/*
2911 	 * A frame ends on the marker
2912 	 *		ff ff 00 c4 c4 96 ..
2913 	 * which is 62 bytes long and is followed by various information
2914 	 * including statuses and luminosity.
2915 	 *
2916 	 * A marker may be splitted on two packets.
2917 	 *
2918 	 * The 6th byte of a marker contains the bits:
2919 	 *	0x08: USB full
2920 	 *	0xc0: frame sequence
2921 	 * When the bit 'USB full' is set, the frame must be discarded;
2922 	 * this is also the case when the 2 bytes before the marker are
2923 	 * not the JPEG end of frame ('ff d9').
2924 	 */
2925 
2926 	/* count the packets and their size */
2927 	sd->npkt++;
2928 	sd->pktsz += len;
2929 
2930 /*fixme: assumption about the following code:
2931  *	- there can be only one marker in a packet
2932  */
2933 
2934 	/* skip the remaining bytes of a short marker */
2935 	i = sd->short_mark;
2936 	if (i != 0) {
2937 		sd->short_mark = 0;
2938 		if (i < 0	/* if 'ff' at end of previous packet */
2939 		 && data[0] == 0xff
2940 		 && data[1] == 0x00)
2941 			goto marker_found;
2942 		if (data[0] == 0xff && data[1] == 0xff) {
2943 			i = 0;
2944 			goto marker_found;
2945 		}
2946 		len -= i;
2947 		if (len <= 0)
2948 			return;
2949 		data += i;
2950 	}
2951 
2952 	/* search backwards if there is a marker in the packet */
2953 	for (i = len - 1; --i >= 0; ) {
2954 		if (data[i] != 0xff) {
2955 			i--;
2956 			continue;
2957 		}
2958 		if (data[i + 1] == 0xff) {
2959 
2960 			/* (there may be 'ff ff' inside a marker) */
2961 			if (i + 2 >= len || data[i + 2] == 0x00)
2962 				goto marker_found;
2963 		}
2964 	}
2965 
2966 	/* no marker found */
2967 	/* add the JPEG header if first fragment */
2968 	if (data[len - 1] == 0xff)
2969 		sd->short_mark = -1;
2970 	if (gspca_dev->last_packet_type == LAST_PACKET)
2971 		gspca_frame_add(gspca_dev, FIRST_PACKET,
2972 				sd->jpeg_hdr, JPEG_HDR_SZ);
2973 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
2974 	return;
2975 
2976 	/* marker found */
2977 	/* if some error, discard the frame and decrease the quality */
2978 marker_found:
2979 	new_qual = 0;
2980 	if (i > 2) {
2981 		if (data[i - 2] != 0xff || data[i - 1] != 0xd9) {
2982 			gspca_dev->last_packet_type = DISCARD_PACKET;
2983 			new_qual = -3;
2984 		}
2985 	} else if (i + 6 < len) {
2986 		if (data[i + 6] & 0x08) {
2987 			gspca_dev->last_packet_type = DISCARD_PACKET;
2988 			new_qual = -5;
2989 		}
2990 	}
2991 
2992 	gspca_frame_add(gspca_dev, LAST_PACKET, data, i);
2993 
2994 	/* compute the filling rate and a new JPEG quality */
2995 	if (new_qual == 0) {
2996 		int r;
2997 
2998 		r = (sd->pktsz * 100) /
2999 			(sd->npkt *
3000 				gspca_dev->urb[0]->iso_frame_desc[0].length);
3001 		if (r >= 85)
3002 			new_qual = -3;
3003 		else if (r < 75)
3004 			new_qual = 2;
3005 	}
3006 	if (new_qual != 0) {
3007 		sd->nchg += new_qual;
3008 		if (sd->nchg < -6 || sd->nchg >= 12) {
3009 			sd->nchg = 0;
3010 			new_qual += sd->quality;
3011 			if (new_qual < QUALITY_MIN)
3012 				new_qual = QUALITY_MIN;
3013 			else if (new_qual > QUALITY_MAX)
3014 				new_qual = QUALITY_MAX;
3015 			if (new_qual != sd->quality) {
3016 				sd->quality = new_qual;
3017 				queue_work(sd->work_thread, &sd->work);
3018 			}
3019 		}
3020 	} else {
3021 		sd->nchg = 0;
3022 	}
3023 	sd->pktsz = sd->npkt = 0;
3024 
3025 	/* if the marker is smaller than 62 bytes,
3026 	 * memorize the number of bytes to skip in the next packet */
3027 	if (i + 62 > len) {			/* no more usable data */
3028 		sd->short_mark = i + 62 - len;
3029 		return;
3030 	}
3031 	if (sd->ag_cnt >= 0)
3032 		set_lum(sd, data + i);
3033 
3034 	/* if more data, start a new frame */
3035 	i += 62;
3036 	if (i < len) {
3037 		data += i;
3038 		len -= i;
3039 		gspca_frame_add(gspca_dev, FIRST_PACKET,
3040 				sd->jpeg_hdr, JPEG_HDR_SZ);
3041 		gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
3042 	}
3043 }
3044 
3045 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
3046 {
3047 	struct sd *sd = (struct sd *) gspca_dev;
3048 
3049 	sd->ctrls[AUTOGAIN].val = val;
3050 	if (val)
3051 		gspca_dev->ctrl_inac |= (1 << EXPOSURE) | (1 << GAIN);
3052 	else
3053 		gspca_dev->ctrl_inac &= ~(1 << EXPOSURE) & ~(1 << GAIN);
3054 	if (gspca_dev->streaming)
3055 		setautogain(gspca_dev);
3056 	return gspca_dev->usb_err;
3057 }
3058 
3059 static int sd_querymenu(struct gspca_dev *gspca_dev,
3060 			struct v4l2_querymenu *menu)
3061 {
3062 	switch (menu->id) {
3063 	case V4L2_CID_POWER_LINE_FREQUENCY:
3064 		switch (menu->index) {
3065 		case 0:		/* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
3066 			strcpy((char *) menu->name, "NoFliker");
3067 			return 0;
3068 		case 1:		/* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
3069 			strcpy((char *) menu->name, "50 Hz");
3070 			return 0;
3071 		case 2:		/* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
3072 			strcpy((char *) menu->name, "60 Hz");
3073 			return 0;
3074 		}
3075 		break;
3076 	}
3077 	return -EINVAL;
3078 }
3079 
3080 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3081 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
3082 			u8 *data,		/* interrupt packet data */
3083 			int len)		/* interrupt packet length */
3084 {
3085 	int ret = -EINVAL;
3086 
3087 	if (len == 1 && data[0] == 1) {
3088 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
3089 		input_sync(gspca_dev->input_dev);
3090 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
3091 		input_sync(gspca_dev->input_dev);
3092 		ret = 0;
3093 	}
3094 
3095 	return ret;
3096 }
3097 #endif
3098 
3099 /* sub-driver description */
3100 static const struct sd_desc sd_desc = {
3101 	.name = MODULE_NAME,
3102 	.ctrls = sd_ctrls,
3103 	.nctrls = NCTRLS,
3104 	.config = sd_config,
3105 	.init = sd_init,
3106 	.start = sd_start,
3107 	.stopN = sd_stopN,
3108 	.stop0 = sd_stop0,
3109 	.pkt_scan = sd_pkt_scan,
3110 	.dq_callback = do_autogain,
3111 	.querymenu = sd_querymenu,
3112 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3113 	.int_pkt_scan = sd_int_pkt_scan,
3114 #endif
3115 };
3116 
3117 /* -- module initialisation -- */
3118 #define BS(bridge, sensor) \
3119 	.driver_info = (BRIDGE_ ## bridge << 16) \
3120 			| (SENSOR_ ## sensor << 8)
3121 #define BSF(bridge, sensor, flags) \
3122 	.driver_info = (BRIDGE_ ## bridge << 16) \
3123 			| (SENSOR_ ## sensor << 8) \
3124 			| (flags)
3125 static const struct usb_device_id device_table[] = {
3126 	{USB_DEVICE(0x0458, 0x7025), BSF(SN9C120, MI0360B, F_PDN_INV)},
3127 	{USB_DEVICE(0x0458, 0x702e), BS(SN9C120, OV7660)},
3128 	{USB_DEVICE(0x045e, 0x00f5), BSF(SN9C105, OV7660, F_PDN_INV)},
3129 	{USB_DEVICE(0x045e, 0x00f7), BSF(SN9C105, OV7660, F_PDN_INV)},
3130 	{USB_DEVICE(0x0471, 0x0327), BS(SN9C105, MI0360)},
3131 	{USB_DEVICE(0x0471, 0x0328), BS(SN9C105, MI0360)},
3132 	{USB_DEVICE(0x0471, 0x0330), BS(SN9C105, MI0360)},
3133 	{USB_DEVICE(0x06f8, 0x3004), BS(SN9C105, OV7660)},
3134 	{USB_DEVICE(0x06f8, 0x3008), BS(SN9C105, OV7660)},
3135 /*	{USB_DEVICE(0x0c45, 0x603a), BS(SN9C102P, OV7648)}, */
3136 	{USB_DEVICE(0x0c45, 0x6040), BS(SN9C102P, HV7131R)},
3137 /*	{USB_DEVICE(0x0c45, 0x607a), BS(SN9C102P, OV7648)}, */
3138 /*	{USB_DEVICE(0x0c45, 0x607b), BS(SN9C102P, OV7660)}, */
3139 	{USB_DEVICE(0x0c45, 0x607c), BS(SN9C102P, HV7131R)},
3140 /*	{USB_DEVICE(0x0c45, 0x607e), BS(SN9C102P, OV7630)}, */
3141 	{USB_DEVICE(0x0c45, 0x60c0), BSF(SN9C105, MI0360, F_ILLUM)},
3142 						/* or MT9V111 */
3143 /*	{USB_DEVICE(0x0c45, 0x60c2), BS(SN9C105, P1030xC)}, */
3144 /*	{USB_DEVICE(0x0c45, 0x60c8), BS(SN9C105, OM6802)}, */
3145 /*	{USB_DEVICE(0x0c45, 0x60cc), BS(SN9C105, HV7131GP)}, */
3146 	{USB_DEVICE(0x0c45, 0x60ce), BS(SN9C105, SP80708)},
3147 	{USB_DEVICE(0x0c45, 0x60ec), BS(SN9C105, MO4000)},
3148 /*	{USB_DEVICE(0x0c45, 0x60ef), BS(SN9C105, ICM105C)}, */
3149 /*	{USB_DEVICE(0x0c45, 0x60fa), BS(SN9C105, OV7648)}, */
3150 /*	{USB_DEVICE(0x0c45, 0x60f2), BS(SN9C105, OV7660)}, */
3151 	{USB_DEVICE(0x0c45, 0x60fb), BS(SN9C105, OV7660)},
3152 	{USB_DEVICE(0x0c45, 0x60fc), BS(SN9C105, HV7131R)},
3153 	{USB_DEVICE(0x0c45, 0x60fe), BS(SN9C105, OV7630)},
3154 	{USB_DEVICE(0x0c45, 0x6100), BS(SN9C120, MI0360)},	/*sn9c128*/
3155 	{USB_DEVICE(0x0c45, 0x6102), BS(SN9C120, PO2030N)},	/* /GC0305*/
3156 /*	{USB_DEVICE(0x0c45, 0x6108), BS(SN9C120, OM6802)}, */
3157 	{USB_DEVICE(0x0c45, 0x610a), BS(SN9C120, OV7648)},	/*sn9c128*/
3158 	{USB_DEVICE(0x0c45, 0x610b), BS(SN9C120, OV7660)},	/*sn9c128*/
3159 	{USB_DEVICE(0x0c45, 0x610c), BS(SN9C120, HV7131R)},	/*sn9c128*/
3160 	{USB_DEVICE(0x0c45, 0x610e), BS(SN9C120, OV7630)},	/*sn9c128*/
3161 /*	{USB_DEVICE(0x0c45, 0x610f), BS(SN9C120, S5K53BEB)}, */
3162 /*	{USB_DEVICE(0x0c45, 0x6122), BS(SN9C110, ICM105C)}, */
3163 /*	{USB_DEVICE(0x0c45, 0x6123), BS(SN9C110, SanyoCCD)}, */
3164 	{USB_DEVICE(0x0c45, 0x6128), BS(SN9C120, OM6802)},	/*sn9c325?*/
3165 /*bw600.inf:*/
3166 	{USB_DEVICE(0x0c45, 0x612a), BS(SN9C120, OV7648)},	/*sn9c325?*/
3167 	{USB_DEVICE(0x0c45, 0x612b), BS(SN9C110, ADCM1700)},
3168 	{USB_DEVICE(0x0c45, 0x612c), BS(SN9C110, MO4000)},
3169 	{USB_DEVICE(0x0c45, 0x612e), BS(SN9C110, OV7630)},
3170 /*	{USB_DEVICE(0x0c45, 0x612f), BS(SN9C110, ICM105C)}, */
3171 	{USB_DEVICE(0x0c45, 0x6130), BS(SN9C120, MI0360)},
3172 						/* or MT9V111 / MI0360B */
3173 /*	{USB_DEVICE(0x0c45, 0x6132), BS(SN9C120, OV7670)}, */
3174 	{USB_DEVICE(0x0c45, 0x6138), BS(SN9C120, MO4000)},
3175 	{USB_DEVICE(0x0c45, 0x613a), BS(SN9C120, OV7648)},
3176 	{USB_DEVICE(0x0c45, 0x613b), BS(SN9C120, OV7660)},
3177 	{USB_DEVICE(0x0c45, 0x613c), BS(SN9C120, HV7131R)},
3178 	{USB_DEVICE(0x0c45, 0x613e), BS(SN9C120, OV7630)},
3179 	{USB_DEVICE(0x0c45, 0x6142), BS(SN9C120, PO2030N)},	/*sn9c120b*/
3180 						/* or GC0305 / GC0307 */
3181 	{USB_DEVICE(0x0c45, 0x6143), BS(SN9C120, SP80708)},	/*sn9c120b*/
3182 	{USB_DEVICE(0x0c45, 0x6148), BS(SN9C120, OM6802)},	/*sn9c120b*/
3183 	{USB_DEVICE(0x0c45, 0x614a), BSF(SN9C120, ADCM1700, F_ILLUM)},
3184 /*	{USB_DEVICE(0x0c45, 0x614c), BS(SN9C120, GC0306)}, */	/*sn9c120b*/
3185 	{}
3186 };
3187 MODULE_DEVICE_TABLE(usb, device_table);
3188 
3189 /* -- device connect -- */
3190 static int sd_probe(struct usb_interface *intf,
3191 		    const struct usb_device_id *id)
3192 {
3193 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
3194 				THIS_MODULE);
3195 }
3196 
3197 static struct usb_driver sd_driver = {
3198 	.name = MODULE_NAME,
3199 	.id_table = device_table,
3200 	.probe = sd_probe,
3201 	.disconnect = gspca_disconnect,
3202 #ifdef CONFIG_PM
3203 	.suspend = gspca_suspend,
3204 	.resume = gspca_resume,
3205 	.reset_resume = gspca_resume,
3206 #endif
3207 };
3208 
3209 module_usb_driver(sd_driver);
3210