]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/media/video/gspca/sonixj.c
Merge branch 'x86/asm' into x86/urgent
[mv-sheeva.git] / drivers / media / video / gspca / sonixj.c
1 /*
2  * Sonix sn9c102p sn9c105 sn9c120 (jpeg) subdriver
3  *
4  * Copyright (C) 2009 Jean-Francois 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 MODULE_NAME "sonixj"
23
24 #include "gspca.h"
25 #include "jpeg.h"
26
27 #define V4L2_CID_INFRARED (V4L2_CID_PRIVATE_BASE + 0)
28
29 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
30 MODULE_DESCRIPTION("GSPCA/SONIX JPEG USB Camera Driver");
31 MODULE_LICENSE("GPL");
32
33 /* specific webcam descriptor */
34 struct sd {
35         struct gspca_dev gspca_dev;     /* !! must be the first item */
36
37         atomic_t avg_lum;
38         u32 exposure;
39
40         u16 brightness;
41         u8 contrast;
42         u8 colors;
43         u8 autogain;
44         u8 blue;
45         u8 red;
46         u8 gamma;
47         u8 vflip;                       /* ov7630/ov7648 only */
48         u8 infrared;                    /* mt9v111 only */
49         u8 freq;                        /* ov76xx only */
50         u8 quality;                     /* image quality */
51 #define QUALITY_MIN 60
52 #define QUALITY_MAX 95
53 #define QUALITY_DEF 80
54         u8 jpegqual;                    /* webcam quality */
55
56         u8 reg18;
57
58         s8 ag_cnt;
59 #define AG_CNT_START 13
60
61         u8 bridge;
62 #define BRIDGE_SN9C102P 0
63 #define BRIDGE_SN9C105 1
64 #define BRIDGE_SN9C110 2
65 #define BRIDGE_SN9C120 3
66         u8 sensor;                      /* Type of image sensor chip */
67 #define SENSOR_HV7131R 0
68 #define SENSOR_MI0360 1
69 #define SENSOR_MO4000 2
70 #define SENSOR_MT9V111 3
71 #define SENSOR_OM6802 4
72 #define SENSOR_OV7630 5
73 #define SENSOR_OV7648 6
74 #define SENSOR_OV7660 7
75 #define SENSOR_PO1030 8
76 #define SENSOR_SP80708 9
77         u8 i2c_addr;
78
79         u8 *jpeg_hdr;
80 };
81
82 /* V4L2 controls supported by the driver */
83 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
87 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
88 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
89 static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val);
93 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val);
94 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val);
95 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
96 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
97 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
98 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
99 static int sd_setinfrared(struct gspca_dev *gspca_dev, __s32 val);
100 static int sd_getinfrared(struct gspca_dev *gspca_dev, __s32 *val);
101 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
102 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
103
104 static struct ctrl sd_ctrls[] = {
105 #define BRIGHTNESS_IDX 0
106         {
107             {
108                 .id      = V4L2_CID_BRIGHTNESS,
109                 .type    = V4L2_CTRL_TYPE_INTEGER,
110                 .name    = "Brightness",
111                 .minimum = 0,
112 #define BRIGHTNESS_MAX 0xffff
113                 .maximum = BRIGHTNESS_MAX,
114                 .step    = 1,
115 #define BRIGHTNESS_DEF 0x8000
116                 .default_value = BRIGHTNESS_DEF,
117             },
118             .set = sd_setbrightness,
119             .get = sd_getbrightness,
120         },
121 #define CONTRAST_IDX 1
122         {
123             {
124                 .id      = V4L2_CID_CONTRAST,
125                 .type    = V4L2_CTRL_TYPE_INTEGER,
126                 .name    = "Contrast",
127                 .minimum = 0,
128 #define CONTRAST_MAX 127
129                 .maximum = CONTRAST_MAX,
130                 .step    = 1,
131 #define CONTRAST_DEF 63
132                 .default_value = CONTRAST_DEF,
133             },
134             .set = sd_setcontrast,
135             .get = sd_getcontrast,
136         },
137 #define COLOR_IDX 2
138         {
139             {
140                 .id      = V4L2_CID_SATURATION,
141                 .type    = V4L2_CTRL_TYPE_INTEGER,
142                 .name    = "Saturation",
143                 .minimum = 0,
144                 .maximum = 40,
145                 .step    = 1,
146 #define COLOR_DEF 25
147                 .default_value = COLOR_DEF,
148             },
149             .set = sd_setcolors,
150             .get = sd_getcolors,
151         },
152 #define BLUE_BALANCE_IDX 3
153         {
154             {
155                 .id      = V4L2_CID_BLUE_BALANCE,
156                 .type    = V4L2_CTRL_TYPE_INTEGER,
157                 .name    = "Blue Balance",
158                 .minimum = 24,
159                 .maximum = 40,
160                 .step    = 1,
161 #define BLUE_BALANCE_DEF 32
162                 .default_value = BLUE_BALANCE_DEF,
163             },
164             .set = sd_setblue_balance,
165             .get = sd_getblue_balance,
166         },
167 #define RED_BALANCE_IDX 4
168         {
169             {
170                 .id      = V4L2_CID_RED_BALANCE,
171                 .type    = V4L2_CTRL_TYPE_INTEGER,
172                 .name    = "Red Balance",
173                 .minimum = 24,
174                 .maximum = 40,
175                 .step    = 1,
176 #define RED_BALANCE_DEF 32
177                 .default_value = RED_BALANCE_DEF,
178             },
179             .set = sd_setred_balance,
180             .get = sd_getred_balance,
181         },
182 #define GAMMA_IDX 5
183         {
184             {
185                 .id      = V4L2_CID_GAMMA,
186                 .type    = V4L2_CTRL_TYPE_INTEGER,
187                 .name    = "Gamma",
188                 .minimum = 0,
189                 .maximum = 40,
190                 .step    = 1,
191 #define GAMMA_DEF 20
192                 .default_value = GAMMA_DEF,
193             },
194             .set = sd_setgamma,
195             .get = sd_getgamma,
196         },
197 #define AUTOGAIN_IDX 6
198         {
199             {
200                 .id      = V4L2_CID_AUTOGAIN,
201                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
202                 .name    = "Auto Gain",
203                 .minimum = 0,
204                 .maximum = 1,
205                 .step    = 1,
206 #define AUTOGAIN_DEF 1
207                 .default_value = AUTOGAIN_DEF,
208             },
209             .set = sd_setautogain,
210             .get = sd_getautogain,
211         },
212 /* ov7630/ov7648 only */
213 #define VFLIP_IDX 7
214         {
215             {
216                 .id      = V4L2_CID_VFLIP,
217                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
218                 .name    = "Vflip",
219                 .minimum = 0,
220                 .maximum = 1,
221                 .step    = 1,
222 #define VFLIP_DEF 0
223                 .default_value = VFLIP_DEF,
224             },
225             .set = sd_setvflip,
226             .get = sd_getvflip,
227         },
228 /* mt9v111 only */
229 #define INFRARED_IDX 8
230         {
231             {
232                 .id      = V4L2_CID_INFRARED,
233                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
234                 .name    = "Infrared",
235                 .minimum = 0,
236                 .maximum = 1,
237                 .step    = 1,
238 #define INFRARED_DEF 0
239                 .default_value = INFRARED_DEF,
240             },
241             .set = sd_setinfrared,
242             .get = sd_getinfrared,
243         },
244 /* ov7630/ov7648/ov7660 only */
245 #define FREQ_IDX 9
246         {
247             {
248                 .id      = V4L2_CID_POWER_LINE_FREQUENCY,
249                 .type    = V4L2_CTRL_TYPE_MENU,
250                 .name    = "Light frequency filter",
251                 .minimum = 0,
252                 .maximum = 2,   /* 0: 0, 1: 50Hz, 2:60Hz */
253                 .step    = 1,
254 #define FREQ_DEF 1
255                 .default_value = FREQ_DEF,
256             },
257             .set = sd_setfreq,
258             .get = sd_getfreq,
259         },
260 };
261
262 /* table of the disabled controls */
263 static __u32 ctrl_dis[] = {
264         (1 << INFRARED_IDX) | (1 << VFLIP_IDX) | (1 << FREQ_IDX),
265                                                 /* SENSOR_HV7131R 0 */
266         (1 << INFRARED_IDX) | (1 << VFLIP_IDX) | (1 << FREQ_IDX),
267                                                 /* SENSOR_MI0360 1 */
268         (1 << INFRARED_IDX) | (1 << VFLIP_IDX) | (1 << FREQ_IDX),
269                                                 /* SENSOR_MO4000 2 */
270         (1 << VFLIP_IDX) | (1 << FREQ_IDX),
271                                                 /* SENSOR_MT9V111 3 */
272         (1 << INFRARED_IDX) | (1 << VFLIP_IDX) | (1 << FREQ_IDX),
273                                                 /* SENSOR_OM6802 4 */
274         (1 << INFRARED_IDX),
275                                                 /* SENSOR_OV7630 5 */
276         (1 << INFRARED_IDX),
277                                                 /* SENSOR_OV7648 6 */
278         (1 << AUTOGAIN_IDX) | (1 << INFRARED_IDX) | (1 << VFLIP_IDX),
279                                                 /* SENSOR_OV7660 7 */
280         (1 << AUTOGAIN_IDX) | (1 << INFRARED_IDX) | (1 << VFLIP_IDX) |
281                               (1 << FREQ_IDX),  /* SENSOR_PO1030 8 */
282         (1 << AUTOGAIN_IDX) | (1 << INFRARED_IDX) | (1 << VFLIP_IDX) |
283                               (1 << FREQ_IDX),  /* SENSOR_SP80708 9 */
284 };
285
286 static const struct v4l2_pix_format vga_mode[] = {
287         {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
288                 .bytesperline = 160,
289                 .sizeimage = 160 * 120 * 4 / 8 + 590,
290                 .colorspace = V4L2_COLORSPACE_JPEG,
291                 .priv = 2},
292         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
293                 .bytesperline = 320,
294                 .sizeimage = 320 * 240 * 3 / 8 + 590,
295                 .colorspace = V4L2_COLORSPACE_JPEG,
296                 .priv = 1},
297         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
298                 .bytesperline = 640,
299                 /* Note 3 / 8 is not large enough, not even 5 / 8 is ?! */
300                 .sizeimage = 640 * 480 * 3 / 4 + 590,
301                 .colorspace = V4L2_COLORSPACE_JPEG,
302                 .priv = 0},
303 };
304
305 /*Data from sn9c102p+hv7131r */
306 static const u8 sn_hv7131[0x1c] = {
307 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
308         0x00,   0x03,   0x64,   0x00,   0x1a,   0x20,   0x20,   0x20,
309 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
310         0x81,   0x11,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
311 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
312         0x03,   0x00,   0x00,   0x01,   0x03,   0x28,   0x1e,   0x41,
313 /*      reg18   reg19   reg1a   reg1b */
314         0x0a,   0x00,   0x00,   0x00
315 };
316
317 static const u8 sn_mi0360[0x1c] = {
318 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
319         0x00,   0x61,   0x44,   0x00,   0x1a,   0x20,   0x20,   0x20,
320 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
321         0x81,   0x5d,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
322 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
323         0x03,   0x00,   0x00,   0x02,   0x0a,   0x28,   0x1e,   0x61,
324 /*      reg18   reg19   reg1a   reg1b */
325         0x06,   0x00,   0x00,   0x00
326 };
327
328 static const u8 sn_mo4000[0x1c] = {
329 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
330         0x00,   0x23,   0x60,   0x00,   0x1a,   0x00,   0x20,   0x18,
331 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
332         0x81,   0x21,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
333 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
334         0x03,    0x00,  0x0b,   0x0f,   0x14,   0x28,   0x1e,   0x40,
335 /*      reg18   reg19   reg1a   reg1b */
336         0x08,   0x00,   0x00,   0x00
337 };
338
339 static const u8 sn_mt9v111[0x1c] = {
340 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
341         0x00,   0x61,   0x40,   0x00,   0x1a,   0x20,   0x20,   0x20,
342 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
343         0x81,   0x5c,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
344 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
345         0x03,   0x00,   0x00,   0x02,   0x1c,   0x28,   0x1e,   0x40,
346 /*      reg18   reg19   reg1a   reg1b */
347         0x06,   0x00,   0x00,   0x00
348 };
349
350 static const u8 sn_om6802[0x1c] = {
351 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
352         0x00,   0x23,   0x72,   0x00,   0x1a,   0x20,   0x20,   0x19,
353 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
354         0x80,   0x34,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
355 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
356         0x03,   0x00,   0x51,   0x01,   0x00,   0x28,   0x1e,   0x40,
357 /*      reg18   reg19   reg1a   reg1b */
358         0x05,   0x00,   0x00,   0x00
359 };
360
361 static const u8 sn_ov7630[0x1c] = {
362 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
363         0x00,   0x21,   0x40,   0x00,   0x1a,   0x20,   0x1f,   0x20,
364 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
365         0x81,   0x21,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
366 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
367         0x03,   0x00,   0x04,   0x01,   0x0a,   0x28,   0x1e,   0xc2,
368 /*      reg18   reg19   reg1a   reg1b */
369         0x0b,   0x00,   0x00,   0x00
370 };
371
372 static const u8 sn_ov7648[0x1c] = {
373 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
374         0x00,   0x63,   0x40,   0x00,   0x1a,   0x20,   0x20,   0x20,
375 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
376         0x81,   0x21,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
377 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
378         0x03,   0x00,   0x00,   0x01,   0x00,   0x28,   0x1e,   0x00,
379 /*      reg18   reg19   reg1a   reg1b */
380         0x0b,   0x00,   0x00,   0x00
381 };
382
383 static const u8 sn_ov7660[0x1c] = {
384 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
385         0x00,   0x61,   0x40,   0x00,   0x1a,   0x00,   0x00,   0x00,
386 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
387         0x81,   0x21,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
388 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
389         0x03,   0x00,   0x01,   0x01,   0x08,   0x28,   0x1e,   0x20,
390 /*      reg18   reg19   reg1a   reg1b */
391         0x07,   0x00,   0x00,   0x00
392 };
393
394 static const u8 sn_po1030[0x1c] = {
395 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
396         0x00,   0x21,   0x62,   0x00,   0x1a,   0x20,   0x20,   0x20,
397 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
398         0x81,   0x6e,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
399 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
400         0x03,   0x00,   0x00,   0x06,   0x06,   0x28,   0x1e,   0x00,
401 /*      reg18   reg19   reg1a   reg1b */
402         0x07,   0x00,   0x00,   0x00
403 };
404
405 static const u8 sn_sp80708[0x1c] = {
406 /*      reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
407         0x00,   0x63,   0x60,   0x00,   0x1a,   0x20,   0x20,   0x20,
408 /*      reg8    reg9    rega    regb    regc    regd    rege    regf */
409         0x81,   0x18,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
410 /*      reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
411         0x03,   0x00,   0x00,   0x03,   0x04,   0x28,   0x1e,   0x00,
412 /*      reg18   reg19   reg1a   reg1b */
413         0x07,   0x00,   0x00,   0x00
414 };
415
416 /* sequence specific to the sensors - !! index = SENSOR_xxx */
417 static const u8 *sn_tb[] = {
418         sn_hv7131,
419         sn_mi0360,
420         sn_mo4000,
421         sn_mt9v111,
422         sn_om6802,
423         sn_ov7630,
424         sn_ov7648,
425         sn_ov7660,
426         sn_po1030,
427         sn_sp80708
428 };
429
430 /* default gamma table */
431 static const u8 gamma_def[17] = {
432         0x00, 0x2d, 0x46, 0x5a, 0x6c, 0x7c, 0x8b, 0x99,
433         0xa6, 0xb2, 0xbf, 0xca, 0xd5, 0xe0, 0xeb, 0xf5, 0xff
434 };
435 /* gamma for sensors HV7131R and MT9V111 */
436 static const u8 gamma_spec_1[17] = {
437         0x08, 0x3a, 0x52, 0x65, 0x75, 0x83, 0x91, 0x9d,
438         0xa9, 0xb4, 0xbe, 0xc8, 0xd2, 0xdb, 0xe4, 0xed, 0xf5
439 };
440 /* gamma for sensor SP80708 */
441 static const u8 gamma_spec_2[17] = {
442         0x0a, 0x2d, 0x4e, 0x68, 0x7d, 0x8f, 0x9f, 0xab,
443         0xb7, 0xc2, 0xcc, 0xd3, 0xd8, 0xde, 0xe2, 0xe5, 0xe6
444 };
445
446 /* color matrix and offsets */
447 static const u8 reg84[] = {
448         0x14, 0x00, 0x27, 0x00, 0x07, 0x00,     /* YR YG YB gains */
449         0xe8, 0x0f, 0xda, 0x0f, 0x40, 0x00,     /* UR UG UB */
450         0x3e, 0x00, 0xcd, 0x0f, 0xf7, 0x0f,     /* VR VG VB */
451         0x00, 0x00, 0x00                        /* YUV offsets */
452 };
453 static const u8 hv7131r_sensor_init[][8] = {
454         {0xc1, 0x11, 0x01, 0x08, 0x01, 0x00, 0x00, 0x10},
455         {0xb1, 0x11, 0x34, 0x17, 0x7f, 0x00, 0x00, 0x10},
456         {0xd1, 0x11, 0x40, 0xff, 0x7f, 0x7f, 0x7f, 0x10},
457 /*      {0x91, 0x11, 0x44, 0x00, 0x00, 0x00, 0x00, 0x10}, */
458         {0xd1, 0x11, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
459         {0xd1, 0x11, 0x14, 0x01, 0xe2, 0x02, 0x82, 0x10},
460 /*      {0x91, 0x11, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10}, */
461
462         {0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
463         {0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
464         {0xc1, 0x11, 0x25, 0x00, 0x61, 0xa8, 0x00, 0x10},
465         {0xa1, 0x11, 0x30, 0x22, 0x00, 0x00, 0x00, 0x10},
466         {0xc1, 0x11, 0x31, 0x20, 0x2e, 0x20, 0x00, 0x10},
467         {0xc1, 0x11, 0x25, 0x00, 0xc3, 0x50, 0x00, 0x10},
468         {0xa1, 0x11, 0x30, 0x07, 0x00, 0x00, 0x00, 0x10}, /* gain14 */
469         {0xc1, 0x11, 0x31, 0x10, 0x10, 0x10, 0x00, 0x10}, /* r g b 101a10 */
470
471         {0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
472         {0xa1, 0x11, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
473         {0xa1, 0x11, 0x21, 0xd0, 0x00, 0x00, 0x00, 0x10},
474         {0xa1, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
475         {0xa1, 0x11, 0x23, 0x09, 0x00, 0x00, 0x00, 0x10},
476
477         {0xa1, 0x11, 0x01, 0x08, 0x00, 0x00, 0x00, 0x10},
478         {0xa1, 0x11, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
479         {0xa1, 0x11, 0x21, 0xd0, 0x00, 0x00, 0x00, 0x10},
480         {0xa1, 0x11, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
481         {0xa1, 0x11, 0x23, 0x10, 0x00, 0x00, 0x00, 0x10},
482         {0xa1, 0x11, 0x01, 0x18, 0x00, 0x00, 0x00, 0x10},
483                                                         /* set sensor clock */
484         {}
485 };
486 static const u8 mi0360_sensor_init[][8] = {
487         {0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
488         {0xb1, 0x5d, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10},
489         {0xb1, 0x5d, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
490         {0xd1, 0x5d, 0x01, 0x00, 0x08, 0x00, 0x16, 0x10},
491         {0xd1, 0x5d, 0x03, 0x01, 0xe2, 0x02, 0x82, 0x10},
492         {0xd1, 0x5d, 0x05, 0x00, 0x09, 0x00, 0x53, 0x10},
493         {0xb1, 0x5d, 0x0d, 0x00, 0x02, 0x00, 0x00, 0x10},
494         {0xd1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
495         {0xd1, 0x5d, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10},
496         {0xd1, 0x5d, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
497         {0xd1, 0x5d, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
498         {0xd1, 0x5d, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
499         {0xd1, 0x5d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x10},
500         {0xd1, 0x5d, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10},
501         {0xd1, 0x5d, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10},
502         {0xd1, 0x5d, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x10},
503         {0xd1, 0x5d, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x10},
504         {0xb1, 0x5d, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10},
505         {0xd1, 0x5d, 0x20, 0x91, 0x01, 0x00, 0x00, 0x10},
506         {0xd1, 0x5d, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
507         {0xd1, 0x5d, 0x24, 0x00, 0x00, 0x00, 0x00, 0x10},
508         {0xd1, 0x5d, 0x26, 0x00, 0x00, 0x00, 0x24, 0x10},
509         {0xd1, 0x5d, 0x2f, 0xf7, 0xB0, 0x00, 0x04, 0x10},
510         {0xd1, 0x5d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
511         {0xd1, 0x5d, 0x33, 0x00, 0x00, 0x01, 0x00, 0x10},
512         {0xb1, 0x5d, 0x3d, 0x06, 0x8f, 0x00, 0x00, 0x10},
513         {0xd1, 0x5d, 0x40, 0x01, 0xe0, 0x00, 0xd1, 0x10},
514         {0xb1, 0x5d, 0x44, 0x00, 0x82, 0x00, 0x00, 0x10},
515         {0xd1, 0x5d, 0x58, 0x00, 0x78, 0x00, 0x43, 0x10},
516         {0xd1, 0x5d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x10},
517         {0xd1, 0x5d, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x10},
518         {0xd1, 0x5d, 0x5e, 0x00, 0x00, 0xa3, 0x1d, 0x10},
519         {0xb1, 0x5d, 0x62, 0x04, 0x11, 0x00, 0x00, 0x10},
520
521         {0xb1, 0x5d, 0x20, 0x91, 0x01, 0x00, 0x00, 0x10},
522         {0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
523         {0xb1, 0x5d, 0x09, 0x00, 0x64, 0x00, 0x00, 0x10},
524         {0xd1, 0x5d, 0x2b, 0x00, 0xa0, 0x00, 0xb0, 0x10},
525         {0xd1, 0x5d, 0x2d, 0x00, 0xa0, 0x00, 0xa0, 0x10},
526
527         {0xb1, 0x5d, 0x0a, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor clck ?2 */
528         {0xb1, 0x5d, 0x06, 0x00, 0x30, 0x00, 0x00, 0x10},
529         {0xb1, 0x5d, 0x05, 0x00, 0x0a, 0x00, 0x00, 0x10},
530         {0xb1, 0x5d, 0x09, 0x02, 0x35, 0x00, 0x00, 0x10}, /* exposure 2 */
531
532         {0xd1, 0x5d, 0x2b, 0x00, 0xb9, 0x00, 0xe3, 0x10},
533         {0xd1, 0x5d, 0x2d, 0x00, 0x5f, 0x00, 0xb9, 0x10}, /* 42 */
534 /*      {0xb1, 0x5d, 0x35, 0x00, 0x67, 0x00, 0x00, 0x10}, * gain orig */
535 /*      {0xb1, 0x5d, 0x35, 0x00, 0x20, 0x00, 0x00, 0x10}, * gain */
536         {0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10}, /* update */
537         {0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor on */
538         {}
539 };
540 static const u8 mo4000_sensor_init[][8] = {
541         {0xa1, 0x21, 0x01, 0x02, 0x00, 0x00, 0x00, 0x10},
542         {0xa1, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10},
543         {0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
544         {0xa1, 0x21, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10},
545         {0xa1, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10},
546         {0xa1, 0x21, 0x05, 0x04, 0x00, 0x00, 0x00, 0x10},
547         {0xa1, 0x21, 0x06, 0x80, 0x00, 0x00, 0x00, 0x10},
548         {0xa1, 0x21, 0x06, 0x81, 0x00, 0x00, 0x00, 0x10},
549         {0xa1, 0x21, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
550         {0xa1, 0x21, 0x11, 0x00, 0x00, 0x00, 0x00, 0x10},
551         {0xa1, 0x21, 0x11, 0x20, 0x00, 0x00, 0x00, 0x10},
552         {0xa1, 0x21, 0x11, 0x30, 0x00, 0x00, 0x00, 0x10},
553         {0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
554         {0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
555         {0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
556         {0xa1, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
557         {0xa1, 0x21, 0x0f, 0x20, 0x00, 0x00, 0x00, 0x10},
558         {0xa1, 0x21, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10},
559         {0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
560         {0xa1, 0x21, 0x11, 0x38, 0x00, 0x00, 0x00, 0x10},
561         {}
562 };
563 static const u8 mt9v111_sensor_init[][8] = {
564         {0xb1, 0x5c, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10}, /* reset? */
565         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
566         {0xb1, 0x5c, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
567         {0xb1, 0x5c, 0x01, 0x00, 0x01, 0x00, 0x00, 0x10}, /* IFP select */
568         {0xb1, 0x5c, 0x08, 0x04, 0x80, 0x00, 0x00, 0x10}, /* output fmt ctrl */
569         {0xb1, 0x5c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10}, /* op mode ctrl */
570         {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10},
571         {0xb1, 0x5c, 0x03, 0x01, 0xe1, 0x00, 0x00, 0x10},
572         {0xb1, 0x5c, 0x04, 0x02, 0x81, 0x00, 0x00, 0x10},
573         {0xb1, 0x5c, 0x05, 0x00, 0x04, 0x00, 0x00, 0x10},
574         {0xb1, 0x5c, 0x01, 0x00, 0x04, 0x00, 0x00, 0x10}, /* sensor select */
575         {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10},
576         {0xb1, 0x5c, 0x03, 0x01, 0xe6, 0x00, 0x00, 0x10},
577         {0xb1, 0x5c, 0x04, 0x02, 0x86, 0x00, 0x00, 0x10},
578         {0xb1, 0x5c, 0x05, 0x00, 0x04, 0x00, 0x00, 0x10},
579         {0xb1, 0x5c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10},
580         {0xb1, 0x5c, 0x08, 0x00, 0x08, 0x00, 0x00, 0x10}, /* row start */
581         {0xb1, 0x5c, 0x0e, 0x00, 0x08, 0x00, 0x00, 0x10},
582         {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10}, /* col start */
583         {0xb1, 0x5c, 0x03, 0x01, 0xe7, 0x00, 0x00, 0x10}, /* window height */
584         {0xb1, 0x5c, 0x04, 0x02, 0x87, 0x00, 0x00, 0x10}, /* window width */
585         {0xb1, 0x5c, 0x07, 0x30, 0x02, 0x00, 0x00, 0x10}, /* output ctrl */
586         {0xb1, 0x5c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10}, /* shutter delay */
587         {0xb1, 0x5c, 0x12, 0x00, 0xb0, 0x00, 0x00, 0x10}, /* zoom col start */
588         {0xb1, 0x5c, 0x13, 0x00, 0x7c, 0x00, 0x00, 0x10}, /* zoom row start */
589         {0xb1, 0x5c, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x10}, /* digital zoom */
590         {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10}, /* read mode */
591         {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
592         {}
593 };
594 static const u8 mt9v111_sensor_param1[][8] = {
595         {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
596         {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
597         {0xb1, 0x5c, 0x09, 0x01, 0x2c, 0x00, 0x00, 0x10},
598         {0xd1, 0x5c, 0x2b, 0x00, 0x33, 0x00, 0xa0, 0x10}, /* green1 gain */
599         {0xd1, 0x5c, 0x2d, 0x00, 0xa0, 0x00, 0x33, 0x10}, /* red gain */
600         /*******/
601         {0xb1, 0x5c, 0x06, 0x00, 0x1e, 0x00, 0x00, 0x10}, /* vert blanking */
602         {0xb1, 0x5c, 0x05, 0x00, 0x0a, 0x00, 0x00, 0x10}, /* horiz blanking */
603         {0xd1, 0x5c, 0x2c, 0x00, 0xad, 0x00, 0xad, 0x10}, /* blue gain */
604         {0xb1, 0x5c, 0x35, 0x01, 0xc0, 0x00, 0x00, 0x10}, /* global gain */
605         {}
606 };
607 static const u8 om6802_init0[2][8] = {
608 /*fixme: variable*/
609         {0xa0, 0x34, 0x29, 0x0e, 0x00, 0x00, 0x00, 0x10},
610         {0xa0, 0x34, 0x23, 0xb0, 0x00, 0x00, 0x00, 0x10},
611 };
612 static const u8 om6802_sensor_init[][8] = {
613         {0xa0, 0x34, 0xdf, 0x6d, 0x00, 0x00, 0x00, 0x10},
614                                                 /* factory mode */
615         {0xa0, 0x34, 0xdd, 0x18, 0x00, 0x00, 0x00, 0x10},
616                                                 /* output raw RGB */
617         {0xa0, 0x34, 0x5a, 0xc0, 0x00, 0x00, 0x00, 0x10},
618 /*      {0xa0, 0x34, 0xfb, 0x11, 0x00, 0x00, 0x00, 0x10}, */
619         {0xa0, 0x34, 0xf0, 0x04, 0x00, 0x00, 0x00, 0x10},
620                 /* auto-exposure speed (0) / white balance mode (auto RGB) */
621 /*      {0xa0, 0x34, 0xf1, 0x02, 0x00, 0x00, 0x00, 0x10},
622                                                          * set color mode */
623 /*      {0xa0, 0x34, 0xfe, 0x5b, 0x00, 0x00, 0x00, 0x10},
624                                                  * max AGC value in AE */
625 /*      {0xa0, 0x34, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x10},
626                                                          * preset AGC */
627 /*      {0xa0, 0x34, 0xe6, 0x00, 0x00, 0x00, 0x00, 0x10},
628                                                  * preset brightness */
629 /*      {0xa0, 0x34, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x10},
630                                                          * preset contrast */
631 /*      {0xa0, 0x34, 0xe8, 0x31, 0x00, 0x00, 0x00, 0x10},
632                                                          * preset gamma */
633         {0xa0, 0x34, 0xe9, 0x0f, 0x00, 0x00, 0x00, 0x10},
634                                 /* luminance mode (0x4f -> AutoExpo on) */
635         {0xa0, 0x34, 0xe4, 0xff, 0x00, 0x00, 0x00, 0x10},
636                                                         /* preset shutter */
637 /*      {0xa0, 0x34, 0xef, 0x00, 0x00, 0x00, 0x00, 0x10},
638                                                          * auto frame rate */
639 /*      {0xa0, 0x34, 0xfb, 0xee, 0x00, 0x00, 0x00, 0x10}, */
640         {0xa0, 0x34, 0x5d, 0x80, 0x00, 0x00, 0x00, 0x10},
641         {}
642 };
643 static const u8 om6802_sensor_param1[][8] = {
644         {0xa0, 0x34, 0x71, 0x84, 0x00, 0x00, 0x00, 0x10},
645         {0xa0, 0x34, 0x72, 0x05, 0x00, 0x00, 0x00, 0x10},
646         {0xa0, 0x34, 0x68, 0x80, 0x00, 0x00, 0x00, 0x10},
647         {0xa0, 0x34, 0x69, 0x01, 0x00, 0x00, 0x00, 0x10},
648         {}
649 };
650 static const u8 ov7630_sensor_init[][8] = {
651         {0xa1, 0x21, 0x76, 0x01, 0x00, 0x00, 0x00, 0x10},
652         {0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
653         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
654         {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
655         {0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
656         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
657         {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
658 /* win: i2c_r from 00 to 80 */
659         {0xd1, 0x21, 0x03, 0x80, 0x10, 0x20, 0x80, 0x10},
660         {0xb1, 0x21, 0x0c, 0x20, 0x20, 0x00, 0x00, 0x10},
661 /* HDG: 0x11 was 0x00 change to 0x01 for better exposure (15 fps instead of 30)
662         0x13 was 0xc0 change to 0xc3 for auto gain and exposure */
663         {0xd1, 0x21, 0x11, 0x01, 0x48, 0xc3, 0x00, 0x10},
664         {0xb1, 0x21, 0x15, 0x80, 0x03, 0x00, 0x00, 0x10},
665         {0xd1, 0x21, 0x17, 0x1b, 0xbd, 0x05, 0xf6, 0x10},
666         {0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
667         {0xd1, 0x21, 0x1f, 0x00, 0x80, 0x80, 0x80, 0x10},
668         {0xd1, 0x21, 0x23, 0xde, 0x10, 0x8a, 0xa0, 0x10},
669         {0xc1, 0x21, 0x27, 0xca, 0xa2, 0x74, 0x00, 0x10},
670         {0xd1, 0x21, 0x2a, 0x88, 0x00, 0x88, 0x01, 0x10},
671         {0xc1, 0x21, 0x2e, 0x80, 0x00, 0x18, 0x00, 0x10},
672         {0xa1, 0x21, 0x21, 0x08, 0x00, 0x00, 0x00, 0x10},
673         {0xa1, 0x21, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
674         {0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10},
675         {0xb1, 0x21, 0x32, 0xc2, 0x08, 0x00, 0x00, 0x10},
676         {0xb1, 0x21, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x10},
677         {0xd1, 0x21, 0x60, 0x05, 0x40, 0x12, 0x57, 0x10},
678         {0xa1, 0x21, 0x64, 0x73, 0x00, 0x00, 0x00, 0x10},
679         {0xd1, 0x21, 0x65, 0x00, 0x55, 0x01, 0xac, 0x10},
680         {0xa1, 0x21, 0x69, 0x38, 0x00, 0x00, 0x00, 0x10},
681         {0xd1, 0x21, 0x6f, 0x1f, 0x01, 0x00, 0x10, 0x10},
682         {0xd1, 0x21, 0x73, 0x50, 0x20, 0x02, 0x01, 0x10},
683         {0xd1, 0x21, 0x77, 0xf3, 0x90, 0x98, 0x98, 0x10},
684         {0xc1, 0x21, 0x7b, 0x00, 0x4c, 0xf7, 0x00, 0x10},
685         {0xd1, 0x21, 0x17, 0x1b, 0xbd, 0x05, 0xf6, 0x10},
686         {0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
687 /* */
688         {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
689         {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
690 /*fixme: + 0x12, 0x04*/
691 /*      {0xa1, 0x21, 0x75, 0x82, 0x00, 0x00, 0x00, 0x10},  * COMN
692                                                          * set by setvflip */
693         {0xa1, 0x21, 0x10, 0x32, 0x00, 0x00, 0x00, 0x10},
694         {0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
695         {0xb1, 0x21, 0x01, 0x80, 0x80, 0x00, 0x00, 0x10},
696 /* */
697 /*      {0xa1, 0x21, 0x2a, 0x88, 0x00, 0x00, 0x00, 0x10}, * set by setfreq */
698 /*      {0xa1, 0x21, 0x2b, 0x34, 0x00, 0x00, 0x00, 0x10}, * set by setfreq */
699 /* */
700         {0xa1, 0x21, 0x10, 0x83, 0x00, 0x00, 0x00, 0x10},
701 /*      {0xb1, 0x21, 0x01, 0x88, 0x70, 0x00, 0x00, 0x10}, */
702         {}
703 };
704
705 static const u8 ov7648_sensor_init[][8] = {
706         {0xa1, 0x21, 0x76, 0x00, 0x00, 0x00, 0x00, 0x10},
707         {0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},       /* reset */
708         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
709         {0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
710         {0xd1, 0x21, 0x03, 0xa4, 0x30, 0x88, 0x00, 0x10},
711         {0xb1, 0x21, 0x11, 0x80, 0x08, 0x00, 0x00, 0x10},
712         {0xc1, 0x21, 0x13, 0xa0, 0x04, 0x84, 0x00, 0x10},
713         {0xd1, 0x21, 0x17, 0x1a, 0x02, 0xba, 0xf4, 0x10},
714         {0xa1, 0x21, 0x1b, 0x04, 0x00, 0x00, 0x00, 0x10},
715         {0xd1, 0x21, 0x1f, 0x41, 0xc0, 0x80, 0x80, 0x10},
716         {0xd1, 0x21, 0x23, 0xde, 0xa0, 0x80, 0x32, 0x10},
717         {0xd1, 0x21, 0x27, 0xfe, 0xa0, 0x00, 0x91, 0x10},
718         {0xd1, 0x21, 0x2b, 0x00, 0x88, 0x85, 0x80, 0x10},
719         {0xc1, 0x21, 0x2f, 0x9c, 0x00, 0xc4, 0x00, 0x10},
720         {0xd1, 0x21, 0x60, 0xa6, 0x60, 0x88, 0x12, 0x10},
721         {0xd1, 0x21, 0x64, 0x88, 0x00, 0x00, 0x94, 0x10},
722         {0xd1, 0x21, 0x68, 0x7a, 0x0c, 0x00, 0x00, 0x10},
723         {0xd1, 0x21, 0x6c, 0x11, 0x33, 0x22, 0x00, 0x10},
724         {0xd1, 0x21, 0x70, 0x11, 0x00, 0x10, 0x50, 0x10},
725         {0xd1, 0x21, 0x74, 0x20, 0x06, 0x00, 0xb5, 0x10},
726         {0xd1, 0x21, 0x78, 0x8a, 0x00, 0x00, 0x00, 0x10},
727         {0xb1, 0x21, 0x7c, 0x00, 0x43, 0x00, 0x00, 0x10},
728
729         {0xd1, 0x21, 0x21, 0x86, 0x00, 0xde, 0xa0, 0x10},
730 /*      {0xd1, 0x21, 0x25, 0x80, 0x32, 0xfe, 0xa0, 0x10}, jfm done */
731 /*      {0xd1, 0x21, 0x29, 0x00, 0x91, 0x00, 0x88, 0x10}, jfm done */
732 /*      {0xb1, 0x21, 0x2d, 0x85, 0x00, 0x00, 0x00, 0x10}, set by setfreq */
733         {}
734 };
735 static const u8 ov7648_sensor_param1[][8] = {
736 /*      {0xa1, 0x21, 0x12, 0x08, 0x00, 0x00, 0x00, 0x10}, jfm done */
737 /*      {0xa1, 0x21, 0x75, 0x06, 0x00, 0x00, 0x00, 0x10},   * COMN
738                                                          * set by setvflip */
739         {0xa1, 0x21, 0x19, 0x02, 0x00, 0x00, 0x00, 0x10},
740         {0xa1, 0x21, 0x10, 0x32, 0x00, 0x00, 0x00, 0x10},
741 /*      {0xa1, 0x21, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
742 /*      {0xa1, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},  * GAIN - def */
743 /*      {0xb1, 0x21, 0x01, 0x6c, 0x6c, 0x00, 0x00, 0x10},  * B R - def: 80 */
744 /*...*/
745         {0xa1, 0x21, 0x11, 0x81, 0x00, 0x00, 0x00, 0x10}, /* CLKRC */
746 /*      {0xa1, 0x21, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
747 /*      {0xa1, 0x21, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
748 /*      {0xa1, 0x21, 0x2a, 0x91, 0x00, 0x00, 0x00, 0x10}, jfm done */
749 /*      {0xa1, 0x21, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10}, jfm done */
750 /*      {0xb1, 0x21, 0x01, 0x64, 0x84, 0x00, 0x00, 0x10},  * B R - def: 80 */
751
752         {}
753 };
754
755 static const u8 ov7660_sensor_init[][8] = {
756         {0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* reset SCCB */
757         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
758         {0xa1, 0x21, 0x12, 0x05, 0x00, 0x00, 0x00, 0x10},
759                                                 /* Outformat = rawRGB */
760         {0xa1, 0x21, 0x13, 0xb8, 0x00, 0x00, 0x00, 0x10}, /* init COM8 */
761         {0xd1, 0x21, 0x00, 0x01, 0x74, 0x92, 0x00, 0x10},
762                                                 /* GAIN BLUE RED VREF */
763         {0xd1, 0x21, 0x04, 0x00, 0x7d, 0x62, 0x00, 0x10},
764                                                 /* COM 1 BAVE GEAVE AECHH */
765         {0xb1, 0x21, 0x08, 0x83, 0x01, 0x00, 0x00, 0x10}, /* RAVE COM2 */
766         {0xd1, 0x21, 0x0c, 0x00, 0x08, 0x04, 0x4f, 0x10}, /* COM 3 4 5 6 */
767         {0xd1, 0x21, 0x10, 0x7f, 0x40, 0x05, 0xff, 0x10},
768                                                 /* AECH CLKRC COM7 COM8 */
769         {0xc1, 0x21, 0x14, 0x2c, 0x00, 0x02, 0x00, 0x10}, /* COM9 COM10 */
770         {0xd1, 0x21, 0x17, 0x10, 0x60, 0x02, 0x7b, 0x10},
771                                                 /* HSTART HSTOP VSTRT VSTOP */
772         {0xa1, 0x21, 0x1b, 0x02, 0x00, 0x00, 0x00, 0x10}, /* PSHFT */
773         {0xb1, 0x21, 0x1e, 0x01, 0x0e, 0x00, 0x00, 0x10}, /* MVFP LAEC */
774         {0xd1, 0x21, 0x20, 0x07, 0x07, 0x07, 0x07, 0x10},
775                                         /* BOS GBOS GROS ROS (BGGR offset) */
776 /*      {0xd1, 0x21, 0x24, 0x68, 0x58, 0xd4, 0x80, 0x10}, */
777         {0xd1, 0x21, 0x24, 0x78, 0x68, 0xd4, 0x80, 0x10},
778                                                 /* AEW AEB VPT BBIAS */
779         {0xd1, 0x21, 0x28, 0x80, 0x30, 0x00, 0x00, 0x10},
780                                                 /* GbBIAS RSVD EXHCH EXHCL */
781         {0xd1, 0x21, 0x2c, 0x80, 0x00, 0x00, 0x62, 0x10},
782                                                 /* RBIAS ADVFL ASDVFH YAVE */
783         {0xc1, 0x21, 0x30, 0x08, 0x30, 0xb4, 0x00, 0x10},
784                                                 /* HSYST HSYEN HREF */
785         {0xd1, 0x21, 0x33, 0x00, 0x07, 0x84, 0x00, 0x10}, /* reserved */
786         {0xd1, 0x21, 0x37, 0x0c, 0x02, 0x43, 0x00, 0x10},
787                                                 /* ADC ACOM OFON TSLB */
788         {0xd1, 0x21, 0x3b, 0x02, 0x6c, 0x19, 0x0e, 0x10},
789                                                 /* COM11 COM12 COM13 COM14 */
790         {0xd1, 0x21, 0x3f, 0x41, 0xc1, 0x22, 0x08, 0x10},
791                                                 /* EDGE COM15 COM16 COM17 */
792         {0xd1, 0x21, 0x43, 0xf0, 0x10, 0x78, 0xa8, 0x10}, /* reserved */
793         {0xd1, 0x21, 0x47, 0x60, 0x80, 0x00, 0x00, 0x10}, /* reserved */
794         {0xd1, 0x21, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x10}, /* reserved */
795         {0xd1, 0x21, 0x4f, 0x46, 0x36, 0x0f, 0x17, 0x10}, /* MTX 1 2 3 4 */
796         {0xd1, 0x21, 0x53, 0x7f, 0x96, 0x40, 0x40, 0x10}, /* MTX 5 6 7 8 */
797         {0xb1, 0x21, 0x57, 0x40, 0x0f, 0x00, 0x00, 0x10}, /* MTX9 MTXS */
798         {0xd1, 0x21, 0x59, 0xba, 0x9a, 0x22, 0xb9, 0x10}, /* reserved */
799         {0xd1, 0x21, 0x5d, 0x9b, 0x10, 0xf0, 0x05, 0x10}, /* reserved */
800         {0xa1, 0x21, 0x61, 0x60, 0x00, 0x00, 0x00, 0x10}, /* reserved */
801         {0xd1, 0x21, 0x62, 0x00, 0x00, 0x50, 0x30, 0x10},
802                                                 /* LCC1 LCC2 LCC3 LCC4 */
803         {0xa1, 0x21, 0x66, 0x00, 0x00, 0x00, 0x00, 0x10}, /* LCC5 */
804         {0xd1, 0x21, 0x67, 0x80, 0x7a, 0x90, 0x80, 0x10}, /* MANU */
805         {0xa1, 0x21, 0x6b, 0x0a, 0x00, 0x00, 0x00, 0x10},
806                                         /* band gap reference [0:3] DBLV */
807         {0xd1, 0x21, 0x6c, 0x30, 0x48, 0x80, 0x74, 0x10}, /* gamma curve */
808         {0xd1, 0x21, 0x70, 0x64, 0x60, 0x5c, 0x58, 0x10}, /* gamma curve */
809         {0xd1, 0x21, 0x74, 0x54, 0x4c, 0x40, 0x38, 0x10}, /* gamma curve */
810         {0xd1, 0x21, 0x78, 0x34, 0x30, 0x2f, 0x2b, 0x10}, /* gamma curve */
811         {0xd1, 0x21, 0x7c, 0x03, 0x07, 0x17, 0x34, 0x10}, /* gamma curve */
812         {0xd1, 0x21, 0x80, 0x41, 0x4d, 0x58, 0x63, 0x10}, /* gamma curve */
813         {0xd1, 0x21, 0x84, 0x6e, 0x77, 0x87, 0x95, 0x10}, /* gamma curve */
814         {0xc1, 0x21, 0x88, 0xaf, 0xc7, 0xdf, 0x00, 0x10}, /* gamma curve */
815         {0xc1, 0x21, 0x8b, 0x99, 0x99, 0xcf, 0x00, 0x10}, /* reserved */
816         {0xb1, 0x21, 0x92, 0x00, 0x00, 0x00, 0x00, 0x10}, /* DM_LNL/H */
817 /* not in all ms-win traces*/
818         {0xa1, 0x21, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x10},
819         {}
820 };
821 static const u8 ov7660_sensor_param1[][8] = {
822         {0xa1, 0x21, 0x1e, 0x01, 0x00, 0x00, 0x00, 0x10}, /* MVFP */
823                                                 /* bits[3..0]reserved */
824         {0xa1, 0x21, 0x1e, 0x01, 0x00, 0x00, 0x00, 0x10},
825         {0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
826                                                 /* VREF vertical frame ctrl */
827         {0xa1, 0x21, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10},
828         {0xa1, 0x21, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10}, /* AECH 0x20 */
829         {0xa1, 0x21, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x10}, /* ADVFL */
830         {0xa1, 0x21, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x10}, /* ADVFH */
831         {0xa1, 0x21, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x10}, /* GAIN */
832 /*      {0xb1, 0x21, 0x01, 0x78, 0x78, 0x00, 0x00, 0x10}, * BLUE */
833 /****** (some exchanges in the win trace) ******/
834 /*fixme:param2*/
835         {0xa1, 0x21, 0x93, 0x00, 0x00, 0x00, 0x00, 0x10},/* dummy line hight */
836         {0xa1, 0x21, 0x92, 0x25, 0x00, 0x00, 0x00, 0x10}, /* dummy line low */
837         {0xa1, 0x21, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x10}, /* EXHCH */
838         {0xa1, 0x21, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10}, /* EXHCL */
839 /*      {0xa1, 0x21, 0x02, 0x90, 0x00, 0x00, 0x00, 0x10},  * RED */
840 /****** (some exchanges in the win trace) ******/
841 /******!! startsensor KO if changed !!****/
842 /*fixme: param3*/
843         {0xa1, 0x21, 0x93, 0x01, 0x00, 0x00, 0x00, 0x10},
844         {0xa1, 0x21, 0x92, 0xff, 0x00, 0x00, 0x00, 0x10},
845         {0xa1, 0x21, 0x2a, 0x00, 0x00, 0x00, 0x00, 0x10},
846         {0xa1, 0x21, 0x2b, 0xc3, 0x00, 0x00, 0x00, 0x10},
847         {}
848 };
849
850 static const u8 po1030_sensor_init[][8] = {
851 /* the sensor registers are described in m5602/m5602_po1030.h */
852         {0xa1, 0x6e, 0x3f, 0x20, 0x00, 0x00, 0x00, 0x10}, /* sensor reset */
853         {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
854         {0xa1, 0x6e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x10},
855         {0xa1, 0x6e, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x10},
856         {0xd1, 0x6e, 0x04, 0x02, 0xb1, 0x02, 0x39, 0x10},
857         {0xd1, 0x6e, 0x08, 0x00, 0x01, 0x00, 0x00, 0x10},
858         {0xd1, 0x6e, 0x0c, 0x02, 0x7f, 0x01, 0xe0, 0x10},
859         {0xd1, 0x6e, 0x12, 0x03, 0x02, 0x00, 0x03, 0x10},
860         {0xd1, 0x6e, 0x16, 0x85, 0x40, 0x4a, 0x40, 0x10}, /* r/g1/b/g2 gains */
861         {0xc1, 0x6e, 0x1a, 0x00, 0x80, 0x00, 0x00, 0x10},
862         {0xd1, 0x6e, 0x1d, 0x08, 0x03, 0x00, 0x00, 0x10},
863         {0xd1, 0x6e, 0x23, 0x00, 0xb0, 0x00, 0x94, 0x10},
864         {0xd1, 0x6e, 0x27, 0x58, 0x00, 0x00, 0x00, 0x10},
865         {0xb1, 0x6e, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10},
866         {0xd1, 0x6e, 0x2d, 0x14, 0x35, 0x61, 0x84, 0x10}, /* gamma corr */
867         {0xd1, 0x6e, 0x31, 0xa2, 0xbd, 0xd8, 0xff, 0x10},
868         {0xd1, 0x6e, 0x35, 0x06, 0x1e, 0x12, 0x02, 0x10}, /* color matrix */
869         {0xd1, 0x6e, 0x39, 0xaa, 0x53, 0x37, 0xd5, 0x10},
870         {0xa1, 0x6e, 0x3d, 0xf2, 0x00, 0x00, 0x00, 0x10},
871         {0xd1, 0x6e, 0x3e, 0x00, 0x00, 0x80, 0x03, 0x10},
872         {0xd1, 0x6e, 0x42, 0x03, 0x00, 0x00, 0x00, 0x10},
873         {0xc1, 0x6e, 0x46, 0x00, 0x80, 0x80, 0x00, 0x10},
874         {0xd1, 0x6e, 0x4b, 0x02, 0xef, 0x08, 0xcd, 0x10},
875         {0xd1, 0x6e, 0x4f, 0x00, 0xd0, 0x00, 0xa0, 0x10},
876         {0xd1, 0x6e, 0x53, 0x01, 0xaa, 0x01, 0x40, 0x10},
877         {0xd1, 0x6e, 0x5a, 0x50, 0x04, 0x30, 0x03, 0x10}, /* raw rgb bayer */
878         {0xa1, 0x6e, 0x5e, 0x00, 0x00, 0x00, 0x00, 0x10},
879         {0xd1, 0x6e, 0x5f, 0x10, 0x40, 0xff, 0x00, 0x10},
880
881         {0xd1, 0x6e, 0x63, 0x40, 0x40, 0x00, 0x00, 0x10},
882         {0xd1, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x10},
883         {0xd1, 0x6e, 0x6b, 0x00, 0x00, 0x00, 0x00, 0x10},
884         {0xd1, 0x6e, 0x6f, 0x00, 0x00, 0x00, 0x00, 0x10},
885         {0xc1, 0x6e, 0x73, 0x10, 0x80, 0xeb, 0x00, 0x10},
886         {}
887 };
888 static const u8 po1030_sensor_param1[][8] = {
889 /* from ms-win traces - these values change with auto gain/expo/wb.. */
890         {0xa1, 0x6e, 0x1e, 0x03, 0x00, 0x00, 0x00, 0x10},
891         {0xa1, 0x6e, 0x1e, 0x03, 0x00, 0x00, 0x00, 0x10},
892 /* mean values */
893         {0xc1, 0x6e, 0x1a, 0x02, 0xd4, 0xa4, 0x00, 0x10}, /* integlines */
894         {0xa1, 0x6e, 0x15, 0x04, 0x00, 0x00, 0x00, 0x10}, /* global gain */
895         {0xc1, 0x6e, 0x16, 0x40, 0x40, 0x40, 0x00, 0x10}, /* r/g1/b gains */
896
897         {0xa1, 0x6e, 0x1d, 0x08, 0x00, 0x00, 0x00, 0x10}, /* control1 */
898         {0xa1, 0x6e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x10}, /* frameheight */
899         {0xa1, 0x6e, 0x07, 0xd5, 0x00, 0x00, 0x00, 0x10},
900 /*      {0xc1, 0x6e, 0x16, 0x49, 0x40, 0x45, 0x00, 0x10}, */
901         {}
902 };
903
904 static const u8 sp80708_sensor_init[][8] = {
905         {0xa1, 0x18, 0x06, 0xf9, 0x00, 0x00, 0x00, 0x10},
906         {0xa1, 0x18, 0x09, 0x1f, 0x00, 0x00, 0x00, 0x10},
907         {0xa1, 0x18, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
908         {0xa1, 0x18, 0x0d, 0xc0, 0x00, 0x00, 0x00, 0x10},
909         {0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
910         {0xa1, 0x18, 0x0f, 0x0f, 0x00, 0x00, 0x00, 0x10},
911         {0xa1, 0x18, 0x10, 0x40, 0x00, 0x00, 0x00, 0x10},
912         {0xa1, 0x18, 0x11, 0x4e, 0x00, 0x00, 0x00, 0x10},
913         {0xa1, 0x18, 0x12, 0x53, 0x00, 0x00, 0x00, 0x10},
914         {0xa1, 0x18, 0x15, 0x80, 0x00, 0x00, 0x00, 0x10},
915         {0xa1, 0x18, 0x18, 0x18, 0x00, 0x00, 0x00, 0x10},
916         {0xa1, 0x18, 0x19, 0x18, 0x00, 0x00, 0x00, 0x10},
917         {0xa1, 0x18, 0x1a, 0x10, 0x00, 0x00, 0x00, 0x10},
918         {0xa1, 0x18, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x10},
919         {0xa1, 0x18, 0x1c, 0x28, 0x00, 0x00, 0x00, 0x10},
920         {0xa1, 0x18, 0x1d, 0x02, 0x00, 0x00, 0x00, 0x10},
921         {0xa1, 0x18, 0x1e, 0x10, 0x00, 0x00, 0x00, 0x10},
922         {0xa1, 0x18, 0x26, 0x04, 0x00, 0x00, 0x00, 0x10},
923         {0xa1, 0x18, 0x27, 0x1e, 0x00, 0x00, 0x00, 0x10},
924         {0xa1, 0x18, 0x28, 0x5a, 0x00, 0x00, 0x00, 0x10},
925         {0xa1, 0x18, 0x29, 0x28, 0x00, 0x00, 0x00, 0x10},
926         {0xa1, 0x18, 0x2a, 0x78, 0x00, 0x00, 0x00, 0x10},
927         {0xa1, 0x18, 0x2b, 0x01, 0x00, 0x00, 0x00, 0x10},
928         {0xa1, 0x18, 0x2c, 0xf7, 0x00, 0x00, 0x00, 0x10},
929         {0xa1, 0x18, 0x2d, 0x2d, 0x00, 0x00, 0x00, 0x10},
930         {0xa1, 0x18, 0x2e, 0xd5, 0x00, 0x00, 0x00, 0x10},
931         {0xa1, 0x18, 0x39, 0x42, 0x00, 0x00, 0x00, 0x10},
932         {0xa1, 0x18, 0x3a, 0x67, 0x00, 0x00, 0x00, 0x10},
933         {0xa1, 0x18, 0x3b, 0x87, 0x00, 0x00, 0x00, 0x10},
934         {0xa1, 0x18, 0x3c, 0xa3, 0x00, 0x00, 0x00, 0x10},
935         {0xa1, 0x18, 0x3d, 0xb0, 0x00, 0x00, 0x00, 0x10},
936         {0xa1, 0x18, 0x3e, 0xbc, 0x00, 0x00, 0x00, 0x10},
937         {0xa1, 0x18, 0x3f, 0xc8, 0x00, 0x00, 0x00, 0x10},
938         {0xa1, 0x18, 0x40, 0xd4, 0x00, 0x00, 0x00, 0x10},
939         {0xa1, 0x18, 0x41, 0xdf, 0x00, 0x00, 0x00, 0x10},
940         {0xa1, 0x18, 0x42, 0xea, 0x00, 0x00, 0x00, 0x10},
941         {0xa1, 0x18, 0x43, 0xf5, 0x00, 0x00, 0x00, 0x10},
942         {0xa1, 0x18, 0x45, 0x80, 0x00, 0x00, 0x00, 0x10},
943         {0xa1, 0x18, 0x46, 0x60, 0x00, 0x00, 0x00, 0x10},
944         {0xa1, 0x18, 0x47, 0x50, 0x00, 0x00, 0x00, 0x10},
945         {0xa1, 0x18, 0x48, 0x30, 0x00, 0x00, 0x00, 0x10},
946         {0xa1, 0x18, 0x49, 0x01, 0x00, 0x00, 0x00, 0x10},
947         {0xa1, 0x18, 0x4d, 0xae, 0x00, 0x00, 0x00, 0x10},
948         {0xa1, 0x18, 0x4e, 0x03, 0x00, 0x00, 0x00, 0x10},
949         {0xa1, 0x18, 0x4f, 0x66, 0x00, 0x00, 0x00, 0x10},
950         {0xa1, 0x18, 0x50, 0x1c, 0x00, 0x00, 0x00, 0x10},
951         {0xa1, 0x18, 0x44, 0x10, 0x00, 0x00, 0x00, 0x10},
952         {0xa1, 0x18, 0x4a, 0x30, 0x00, 0x00, 0x00, 0x10},
953         {0xa1, 0x18, 0x51, 0x80, 0x00, 0x00, 0x00, 0x10},
954         {0xa1, 0x18, 0x52, 0x80, 0x00, 0x00, 0x00, 0x10},
955         {0xa1, 0x18, 0x53, 0x80, 0x00, 0x00, 0x00, 0x10},
956         {0xa1, 0x18, 0x54, 0x80, 0x00, 0x00, 0x00, 0x10},
957         {0xa1, 0x18, 0x55, 0x80, 0x00, 0x00, 0x00, 0x10},
958         {0xa1, 0x18, 0x56, 0x80, 0x00, 0x00, 0x00, 0x10},
959         {0xa1, 0x18, 0x57, 0xe0, 0x00, 0x00, 0x00, 0x10},
960         {0xa1, 0x18, 0x58, 0xc0, 0x00, 0x00, 0x00, 0x10},
961         {0xa1, 0x18, 0x59, 0xab, 0x00, 0x00, 0x00, 0x10},
962         {0xa1, 0x18, 0x5a, 0xa0, 0x00, 0x00, 0x00, 0x10},
963         {0xa1, 0x18, 0x5b, 0x99, 0x00, 0x00, 0x00, 0x10},
964         {0xa1, 0x18, 0x5c, 0x90, 0x00, 0x00, 0x00, 0x10},
965         {0xa1, 0x18, 0x5e, 0x24, 0x00, 0x00, 0x00, 0x10},
966         {0xa1, 0x18, 0x5f, 0x00, 0x00, 0x00, 0x00, 0x10},
967         {0xa1, 0x18, 0x60, 0x00, 0x00, 0x00, 0x00, 0x10},
968         {0xa1, 0x18, 0x61, 0x73, 0x00, 0x00, 0x00, 0x10},
969         {0xa1, 0x18, 0x63, 0x42, 0x00, 0x00, 0x00, 0x10},
970         {0xa1, 0x18, 0x64, 0x42, 0x00, 0x00, 0x00, 0x10},
971         {0xa1, 0x18, 0x65, 0x42, 0x00, 0x00, 0x00, 0x10},
972         {0xa1, 0x18, 0x66, 0x24, 0x00, 0x00, 0x00, 0x10},
973         {0xa1, 0x18, 0x67, 0x24, 0x00, 0x00, 0x00, 0x10},
974         {0xa1, 0x18, 0x68, 0x08, 0x00, 0x00, 0x00, 0x10},
975         {0xa1, 0x18, 0x2f, 0xc9, 0x00, 0x00, 0x00, 0x10},
976         {}
977 };
978 static const u8 sp80708_sensor_param1[][8] = {
979         {0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
980         {0xa1, 0x18, 0x0c, 0x04, 0x00, 0x00, 0x00, 0x10},
981         {0xa1, 0x18, 0x03, 0x01, 0x00, 0x00, 0x00, 0x10},
982         {0xa1, 0x18, 0x04, 0xa4, 0x00, 0x00, 0x00, 0x10},
983         {0xa1, 0x18, 0x14, 0x3f, 0x00, 0x00, 0x00, 0x10},
984         {0xa1, 0x18, 0x5d, 0x80, 0x00, 0x00, 0x00, 0x10},
985         {0xb1, 0x18, 0x11, 0x40, 0x40, 0x00, 0x00, 0x10},
986         {}
987 };
988
989 static const u8 (*sensor_init[10])[8] = {
990         hv7131r_sensor_init,    /* HV7131R 0 */
991         mi0360_sensor_init,     /* MI0360 1 */
992         mo4000_sensor_init,     /* MO4000 2 */
993         mt9v111_sensor_init,    /* MT9V111 3 */
994         om6802_sensor_init,     /* OM6802 4 */
995         ov7630_sensor_init,     /* OV7630 5 */
996         ov7648_sensor_init,     /* OV7648 6 */
997         ov7660_sensor_init,     /* OV7660 7 */
998         po1030_sensor_init,     /* PO1030 8 */
999         sp80708_sensor_init,    /* SP80708 9 */
1000 };
1001
1002 /* read <len> bytes to gspca_dev->usb_buf */
1003 static void reg_r(struct gspca_dev *gspca_dev,
1004                   u16 value, int len)
1005 {
1006 #ifdef GSPCA_DEBUG
1007         if (len > USB_BUF_SZ) {
1008                 err("reg_r: buffer overflow");
1009                 return;
1010         }
1011 #endif
1012         usb_control_msg(gspca_dev->dev,
1013                         usb_rcvctrlpipe(gspca_dev->dev, 0),
1014                         0,
1015                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1016                         value, 0,
1017                         gspca_dev->usb_buf, len,
1018                         500);
1019         PDEBUG(D_USBI, "reg_r [%02x] -> %02x", value, gspca_dev->usb_buf[0]);
1020 }
1021
1022 static void reg_w1(struct gspca_dev *gspca_dev,
1023                    u16 value,
1024                    u8 data)
1025 {
1026         PDEBUG(D_USBO, "reg_w1 [%04x] = %02x", value, data);
1027         gspca_dev->usb_buf[0] = data;
1028         usb_control_msg(gspca_dev->dev,
1029                         usb_sndctrlpipe(gspca_dev->dev, 0),
1030                         0x08,
1031                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1032                         value,
1033                         0,
1034                         gspca_dev->usb_buf, 1,
1035                         500);
1036 }
1037 static void reg_w(struct gspca_dev *gspca_dev,
1038                           u16 value,
1039                           const u8 *buffer,
1040                           int len)
1041 {
1042         PDEBUG(D_USBO, "reg_w [%04x] = %02x %02x ..",
1043                 value, buffer[0], buffer[1]);
1044 #ifdef GSPCA_DEBUG
1045         if (len > USB_BUF_SZ) {
1046                 err("reg_w: buffer overflow");
1047                 return;
1048         }
1049 #endif
1050         memcpy(gspca_dev->usb_buf, buffer, len);
1051         usb_control_msg(gspca_dev->dev,
1052                         usb_sndctrlpipe(gspca_dev->dev, 0),
1053                         0x08,
1054                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1055                         value, 0,
1056                         gspca_dev->usb_buf, len,
1057                         500);
1058 }
1059
1060 /* I2C write 1 byte */
1061 static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
1062 {
1063         struct sd *sd = (struct sd *) gspca_dev;
1064
1065         PDEBUG(D_USBO, "i2c_w2 [%02x] = %02x", reg, val);
1066         switch (sd->sensor) {
1067         case SENSOR_OM6802:             /* i2c command = a0 (100 kHz) */
1068                 gspca_dev->usb_buf[0] = 0x80 | (2 << 4);
1069                 break;
1070         default:                        /* i2c command = a1 (400 kHz) */
1071                 gspca_dev->usb_buf[0] = 0x81 | (2 << 4);
1072                 break;
1073         }
1074         gspca_dev->usb_buf[1] = sd->i2c_addr;
1075         gspca_dev->usb_buf[2] = reg;
1076         gspca_dev->usb_buf[3] = val;
1077         gspca_dev->usb_buf[4] = 0;
1078         gspca_dev->usb_buf[5] = 0;
1079         gspca_dev->usb_buf[6] = 0;
1080         gspca_dev->usb_buf[7] = 0x10;
1081         usb_control_msg(gspca_dev->dev,
1082                         usb_sndctrlpipe(gspca_dev->dev, 0),
1083                         0x08,
1084                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1085                         0x08,                   /* value = i2c */
1086                         0,
1087                         gspca_dev->usb_buf, 8,
1088                         500);
1089 }
1090
1091 /* I2C write 8 bytes */
1092 static void i2c_w8(struct gspca_dev *gspca_dev,
1093                    const u8 *buffer)
1094 {
1095         memcpy(gspca_dev->usb_buf, buffer, 8);
1096         usb_control_msg(gspca_dev->dev,
1097                         usb_sndctrlpipe(gspca_dev->dev, 0),
1098                         0x08,
1099                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1100                         0x08, 0,                /* value, index */
1101                         gspca_dev->usb_buf, 8,
1102                         500);
1103         msleep(2);
1104 }
1105
1106 /* sensor read 'len' (1..5) bytes in gspca_dev->usb_buf */
1107 static void i2c_r(struct gspca_dev *gspca_dev, u8 reg, int len)
1108 {
1109         struct sd *sd = (struct sd *) gspca_dev;
1110         u8 mode[8];
1111
1112         switch (sd->sensor) {
1113         case SENSOR_OM6802:             /* i2c command = 90 (100 kHz) */
1114                 mode[0] = 0x80 | 0x10;
1115                 break;
1116         default:                        /* i2c command = 91 (400 kHz) */
1117                 mode[0] = 0x81 | 0x10;
1118                 break;
1119         }
1120         mode[1] = sd->i2c_addr;
1121         mode[2] = reg;
1122         mode[3] = 0;
1123         mode[4] = 0;
1124         mode[5] = 0;
1125         mode[6] = 0;
1126         mode[7] = 0x10;
1127         i2c_w8(gspca_dev, mode);
1128         msleep(2);
1129         mode[0] = (mode[0] & 0x81) | (len << 4) | 0x02;
1130         mode[2] = 0;
1131         i2c_w8(gspca_dev, mode);
1132         msleep(2);
1133         reg_r(gspca_dev, 0x0a, 5);
1134 }
1135
1136 static void i2c_w_seq(struct gspca_dev *gspca_dev,
1137                         const u8 (*data)[8])
1138 {
1139         while ((*data)[0] != 0) {
1140                 if ((*data)[0] != 0xdd)
1141                         i2c_w8(gspca_dev, *data);
1142                 else
1143                         msleep((*data)[1]);
1144                 data++;
1145         }
1146 }
1147
1148 static void hv7131r_probe(struct gspca_dev *gspca_dev)
1149 {
1150         i2c_w1(gspca_dev, 0x02, 0);                     /* sensor wakeup */
1151         msleep(10);
1152         reg_w1(gspca_dev, 0x02, 0x66);                  /* Gpio on */
1153         msleep(10);
1154         i2c_r(gspca_dev, 0, 5);                         /* read sensor id */
1155         if (gspca_dev->usb_buf[0] == 0x02
1156             && gspca_dev->usb_buf[1] == 0x09
1157             && gspca_dev->usb_buf[2] == 0x01
1158             && gspca_dev->usb_buf[3] == 0x00
1159             && gspca_dev->usb_buf[4] == 0x00) {
1160                 PDEBUG(D_PROBE, "Sensor sn9c102P HV7131R found");
1161                 return;
1162         }
1163         PDEBUG(D_PROBE, "Sensor 0x%02x 0x%02x 0x%02x - sn9c102P not found",
1164                 gspca_dev->usb_buf[0], gspca_dev->usb_buf[1],
1165                 gspca_dev->usb_buf[2]);
1166 }
1167
1168 static void mi0360_probe(struct gspca_dev *gspca_dev)
1169 {
1170         struct sd *sd = (struct sd *) gspca_dev;
1171         int i, j;
1172         u16 val = 0;
1173         static const u8 probe_tb[][4][8] = {
1174             {                                   /* mi0360 */
1175                 {0xb0, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
1176                 {0x90, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1177                 {0xa2, 0x5d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1178                 {0xb0, 0x5d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10}
1179             },
1180             {                                   /* mt9v111 */
1181                 {0xb0, 0x5c, 0x01, 0x00, 0x04, 0x00, 0x00, 0x10},
1182                 {0x90, 0x5c, 0x36, 0x00, 0x00, 0x00, 0x00, 0x10},
1183                 {0xa2, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10},
1184                 {}
1185             },
1186         };
1187
1188         for (i = 0; i < ARRAY_SIZE(probe_tb); i++) {
1189                 reg_w1(gspca_dev, 0x17, 0x62);
1190                 reg_w1(gspca_dev, 0x01, 0x08);
1191                 for (j = 0; j < 3; j++)
1192                         i2c_w8(gspca_dev, probe_tb[i][j]);
1193                 msleep(2);
1194                 reg_r(gspca_dev, 0x0a, 5);
1195                 val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
1196                 if (probe_tb[i][3][0] != 0)
1197                         i2c_w8(gspca_dev, probe_tb[i][3]);
1198                 reg_w1(gspca_dev, 0x01, 0x29);
1199                 reg_w1(gspca_dev, 0x17, 0x42);
1200                 if (val != 0xffff)
1201                         break;
1202         }
1203         switch (val) {
1204         case 0x823a:
1205                 PDEBUG(D_PROBE, "Sensor mt9v111");
1206                 sd->sensor = SENSOR_MT9V111;
1207                 break;
1208         case 0x8243:
1209                 PDEBUG(D_PROBE, "Sensor mi0360");
1210                 break;
1211         default:
1212                 PDEBUG(D_PROBE, "Unknown sensor %04x - forced to mi0360", val);
1213                 break;
1214         }
1215 }
1216
1217 static void ov7648_probe(struct gspca_dev *gspca_dev)
1218 {
1219         struct sd *sd = (struct sd *) gspca_dev;
1220
1221         /* check ov76xx */
1222         reg_w1(gspca_dev, 0x17, 0x62);
1223         reg_w1(gspca_dev, 0x01, 0x08);
1224         sd->i2c_addr = 0x21;
1225         i2c_r(gspca_dev, 0x0a, 2);
1226         if (gspca_dev->usb_buf[3] == 0x76) {    /* ov76xx */
1227                 PDEBUG(D_PROBE, "Sensor ov%02x%02x",
1228                         gspca_dev->usb_buf[3], gspca_dev->usb_buf[4]);
1229                 return;
1230         }
1231
1232         /* reset */
1233         reg_w1(gspca_dev, 0x01, 0x29);
1234         reg_w1(gspca_dev, 0x17, 0x42);
1235
1236         /* check po1030 */
1237         reg_w1(gspca_dev, 0x17, 0x62);
1238         reg_w1(gspca_dev, 0x01, 0x08);
1239         sd->i2c_addr = 0x6e;
1240         i2c_r(gspca_dev, 0x00, 2);
1241         if (gspca_dev->usb_buf[3] == 0x10       /* po1030 */
1242             && gspca_dev->usb_buf[4] == 0x30) {
1243                 PDEBUG(D_PROBE, "Sensor po1030");
1244                 sd->sensor = SENSOR_PO1030;
1245                 return;
1246         }
1247
1248         PDEBUG(D_PROBE, "Unknown sensor %02x%02x",
1249                 gspca_dev->usb_buf[3], gspca_dev->usb_buf[4]);
1250 }
1251
1252 static void bridge_init(struct gspca_dev *gspca_dev,
1253                           const u8 *sn9c1xx)
1254 {
1255         struct sd *sd = (struct sd *) gspca_dev;
1256         const u8 *reg9a;
1257         static const u8 reg9a_def[] =
1258                 {0x00, 0x40, 0x20, 0x00, 0x00, 0x00};
1259         static const u8 reg9a_spec[] =
1260                 {0x00, 0x40, 0x38, 0x30, 0x00, 0x20};
1261         static const u8 regd4[] = {0x60, 0x00, 0x00};
1262
1263         reg_w1(gspca_dev, 0xf1, 0x00);
1264         reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
1265
1266         /* configure gpio */
1267         reg_w(gspca_dev, 0x01, &sn9c1xx[1], 2);
1268         reg_w(gspca_dev, 0x08, &sn9c1xx[8], 2);
1269         reg_w(gspca_dev, 0x17, &sn9c1xx[0x17], 5);
1270         switch (sd->sensor) {
1271         case SENSOR_OV7660:
1272         case SENSOR_PO1030:
1273         case SENSOR_SP80708:
1274                 reg9a = reg9a_spec;
1275                 break;
1276         default:
1277                 reg9a = reg9a_def;
1278                 break;
1279         }
1280         reg_w(gspca_dev, 0x9a, reg9a, 6);
1281
1282         reg_w(gspca_dev, 0xd4, regd4, sizeof regd4);
1283
1284         reg_w(gspca_dev, 0x03, &sn9c1xx[3], 0x0f);
1285
1286         switch (sd->sensor) {
1287         case SENSOR_MT9V111:
1288                 reg_w1(gspca_dev, 0x01, 0x61);
1289                 reg_w1(gspca_dev, 0x17, 0x61);
1290                 reg_w1(gspca_dev, 0x01, 0x60);
1291                 reg_w1(gspca_dev, 0x01, 0x40);
1292                 break;
1293         case SENSOR_OM6802:
1294                 msleep(10);
1295                 reg_w1(gspca_dev, 0x02, 0x73);
1296                 reg_w1(gspca_dev, 0x17, 0x60);
1297                 reg_w1(gspca_dev, 0x01, 0x22);
1298                 msleep(100);
1299                 reg_w1(gspca_dev, 0x01, 0x62);
1300                 reg_w1(gspca_dev, 0x17, 0x64);
1301                 reg_w1(gspca_dev, 0x17, 0x64);
1302                 reg_w1(gspca_dev, 0x01, 0x42);
1303                 msleep(10);
1304                 reg_w1(gspca_dev, 0x01, 0x42);
1305                 i2c_w8(gspca_dev, om6802_init0[0]);
1306                 i2c_w8(gspca_dev, om6802_init0[1]);
1307                 msleep(15);
1308                 reg_w1(gspca_dev, 0x02, 0x71);
1309                 msleep(150);
1310                 break;
1311         case SENSOR_OV7630:
1312                 reg_w1(gspca_dev, 0x01, 0x61);
1313                 reg_w1(gspca_dev, 0x17, 0xe2);
1314                 reg_w1(gspca_dev, 0x01, 0x60);
1315                 reg_w1(gspca_dev, 0x01, 0x40);
1316                 break;
1317         case SENSOR_OV7648:
1318                 reg_w1(gspca_dev, 0x01, 0x63);
1319                 reg_w1(gspca_dev, 0x17, 0x20);
1320                 reg_w1(gspca_dev, 0x01, 0x62);
1321                 reg_w1(gspca_dev, 0x01, 0x42);
1322                 break;
1323         case SENSOR_PO1030:
1324                 reg_w1(gspca_dev, 0x01, 0x61);
1325                 reg_w1(gspca_dev, 0x17, 0x20);
1326                 reg_w1(gspca_dev, 0x01, 0x60);
1327                 reg_w1(gspca_dev, 0x01, 0x40);
1328                 break;
1329         case SENSOR_OV7660:
1330                 /* fall thru */
1331         case SENSOR_SP80708:
1332                 reg_w1(gspca_dev, 0x01, 0x63);
1333                 reg_w1(gspca_dev, 0x17, 0x20);
1334                 reg_w1(gspca_dev, 0x01, 0x62);
1335                 reg_w1(gspca_dev, 0x01, 0x42);
1336                 msleep(100);
1337                 reg_w1(gspca_dev, 0x02, 0x62);
1338                 break;
1339         default:
1340 /*      case SENSOR_HV7131R: */
1341 /*      case SENSOR_MI0360: */
1342 /*      case SENSOR_MO4000: */
1343                 reg_w1(gspca_dev, 0x01, 0x43);
1344                 reg_w1(gspca_dev, 0x17, 0x61);
1345                 reg_w1(gspca_dev, 0x01, 0x42);
1346                 if (sd->sensor == SENSOR_HV7131R
1347                     && sd->bridge == BRIDGE_SN9C102P)
1348                         hv7131r_probe(gspca_dev);
1349                 break;
1350         }
1351 }
1352
1353 /* this function is called at probe time */
1354 static int sd_config(struct gspca_dev *gspca_dev,
1355                         const struct usb_device_id *id)
1356 {
1357         struct sd *sd = (struct sd *) gspca_dev;
1358         struct cam *cam;
1359
1360         cam = &gspca_dev->cam;
1361         cam->cam_mode = vga_mode;
1362         cam->nmodes = ARRAY_SIZE(vga_mode);
1363         cam->npkt = 24;                 /* 24 packets per ISOC message */
1364
1365         sd->bridge = id->driver_info >> 16;
1366         sd->sensor = id->driver_info;
1367
1368         sd->brightness = BRIGHTNESS_DEF;
1369         sd->contrast = CONTRAST_DEF;
1370         sd->colors = COLOR_DEF;
1371         sd->blue = BLUE_BALANCE_DEF;
1372         sd->red = RED_BALANCE_DEF;
1373         sd->gamma = GAMMA_DEF;
1374         sd->autogain = AUTOGAIN_DEF;
1375         sd->ag_cnt = -1;
1376         sd->vflip = VFLIP_DEF;
1377         sd->infrared = INFRARED_DEF;
1378         sd->freq = FREQ_DEF;
1379         sd->quality = QUALITY_DEF;
1380         sd->jpegqual = 80;
1381
1382         return 0;
1383 }
1384
1385 /* this function is called at probe and resume time */
1386 static int sd_init(struct gspca_dev *gspca_dev)
1387 {
1388         struct sd *sd = (struct sd *) gspca_dev;
1389         const u8 *sn9c1xx;
1390         u8 regGpio[] = { 0x29, 0x74 };
1391         u8 regF1;
1392
1393         /* setup a selector by bridge */
1394         reg_w1(gspca_dev, 0xf1, 0x01);
1395         reg_r(gspca_dev, 0x00, 1);
1396         reg_w1(gspca_dev, 0xf1, gspca_dev->usb_buf[0]);
1397         reg_r(gspca_dev, 0x00, 1);              /* get sonix chip id */
1398         regF1 = gspca_dev->usb_buf[0];
1399         PDEBUG(D_PROBE, "Sonix chip id: %02x", regF1);
1400         switch (sd->bridge) {
1401         case BRIDGE_SN9C102P:
1402                 if (regF1 != 0x11)
1403                         return -ENODEV;
1404                 reg_w1(gspca_dev, 0x02, regGpio[1]);
1405                 break;
1406         case BRIDGE_SN9C105:
1407                 if (regF1 != 0x11)
1408                         return -ENODEV;
1409                 if (sd->sensor == SENSOR_MI0360)
1410                         mi0360_probe(gspca_dev);
1411                 reg_w(gspca_dev, 0x01, regGpio, 2);
1412                 break;
1413         case BRIDGE_SN9C120:
1414                 if (regF1 != 0x12)
1415                         return -ENODEV;
1416                 switch (sd->sensor) {
1417                 case SENSOR_MI0360:
1418                         mi0360_probe(gspca_dev);
1419                         break;
1420                 case SENSOR_OV7648:
1421                         ov7648_probe(gspca_dev);
1422                         break;
1423                 }
1424                 regGpio[1] = 0x70;
1425                 reg_w(gspca_dev, 0x01, regGpio, 2);
1426                 break;
1427         default:
1428 /*      case BRIDGE_SN9C110: */
1429 /*      case BRIDGE_SN9C325: */
1430                 if (regF1 != 0x12)
1431                         return -ENODEV;
1432                 reg_w1(gspca_dev, 0x02, 0x62);
1433                 break;
1434         }
1435
1436         reg_w1(gspca_dev, 0xf1, 0x01);
1437
1438         /* set the i2c address */
1439         sn9c1xx = sn_tb[sd->sensor];
1440         sd->i2c_addr = sn9c1xx[9];
1441
1442         gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
1443
1444         return 0;
1445 }
1446
1447 static u32 setexposure(struct gspca_dev *gspca_dev,
1448                         u32 expo)
1449 {
1450         struct sd *sd = (struct sd *) gspca_dev;
1451
1452         switch (sd->sensor) {
1453         case SENSOR_HV7131R: {
1454                 u8 Expodoit[] =
1455                         { 0xc1, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x16 };
1456
1457                 Expodoit[3] = expo >> 16;
1458                 Expodoit[4] = expo >> 8;
1459                 Expodoit[5] = expo;
1460                 i2c_w8(gspca_dev, Expodoit);
1461                 break;
1462             }
1463         case SENSOR_MI0360: {
1464                 u8 expoMi[] =           /* exposure 0x0635 -> 4 fp/s 0x10 */
1465                         { 0xb1, 0x5d, 0x09, 0x00, 0x00, 0x00, 0x00, 0x16 };
1466                 static const u8 doit[] =                /* update sensor */
1467                         { 0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10 };
1468                 static const u8 sensorgo[] =            /* sensor on */
1469                         { 0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10 };
1470
1471                 if (expo > 0x0635)
1472                         expo = 0x0635;
1473                 else if (expo < 0x0001)
1474                         expo = 0x0001;
1475                 expoMi[3] = expo >> 8;
1476                 expoMi[4] = expo;
1477                 i2c_w8(gspca_dev, expoMi);
1478                 i2c_w8(gspca_dev, doit);
1479                 i2c_w8(gspca_dev, sensorgo);
1480                 break;
1481             }
1482         case SENSOR_MO4000: {
1483                 u8 expoMof[] =
1484                         { 0xa1, 0x21, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x10 };
1485                 u8 expoMo10[] =
1486                         { 0xa1, 0x21, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10 };
1487                 static const u8 gainMo[] =
1488                         { 0xa1, 0x21, 0x00, 0x10, 0x00, 0x00, 0x00, 0x1d };
1489
1490                 if (expo > 0x1fff)
1491                         expo = 0x1fff;
1492                 else if (expo < 0x0001)
1493                         expo = 0x0001;
1494                 expoMof[3] = (expo & 0x03fc) >> 2;
1495                 i2c_w8(gspca_dev, expoMof);
1496                 expoMo10[3] = ((expo & 0x1c00) >> 10)
1497                                 | ((expo & 0x0003) << 4);
1498                 i2c_w8(gspca_dev, expoMo10);
1499                 i2c_w8(gspca_dev, gainMo);
1500                 PDEBUG(D_FRAM, "set exposure %d",
1501                         ((expoMo10[3] & 0x07) << 10)
1502                         | (expoMof[3] << 2)
1503                         | ((expoMo10[3] & 0x30) >> 4));
1504                 break;
1505             }
1506         case SENSOR_MT9V111: {
1507                 u8 expo_c1[] =
1508                         { 0xb1, 0x5c, 0x09, 0x00, 0x00, 0x00, 0x00, 0x10 };
1509
1510                 if (expo > 0x0280)
1511                         expo = 0x0280;
1512                 else if (expo < 0x0040)
1513                         expo = 0x0040;
1514                 expo_c1[3] = expo >> 8;
1515                 expo_c1[4] = expo;
1516                 i2c_w8(gspca_dev, expo_c1);
1517                 break;
1518             }
1519         case SENSOR_OM6802: {
1520                 u8 gainOm[] =
1521                         { 0xa0, 0x34, 0xe5, 0x00, 0x00, 0x00, 0x00, 0x10 };
1522                                 /* preset AGC - works when AutoExpo = off */
1523
1524                 if (expo > 0x03ff)
1525                         expo = 0x03ff;
1526                  if (expo < 0x0001)
1527                         expo = 0x0001;
1528                 gainOm[3] = expo >> 2;
1529                 i2c_w8(gspca_dev, gainOm);
1530                 reg_w1(gspca_dev, 0x96, expo >> 5);
1531                 PDEBUG(D_FRAM, "set exposure %d", gainOm[3]);
1532                 break;
1533             }
1534         }
1535         return expo;
1536 }
1537
1538 static void setbrightness(struct gspca_dev *gspca_dev)
1539 {
1540         struct sd *sd = (struct sd *) gspca_dev;
1541         unsigned int expo;
1542         u8 k2;
1543
1544         k2 = ((int) sd->brightness - 0x8000) >> 10;
1545         switch (sd->sensor) {
1546         case SENSOR_HV7131R:
1547                 expo = sd->brightness << 4;
1548                 if (expo > 0x002dc6c0)
1549                         expo = 0x002dc6c0;
1550                 else if (expo < 0x02a0)
1551                         expo = 0x02a0;
1552                 sd->exposure = setexposure(gspca_dev, expo);
1553                 break;
1554         case SENSOR_MI0360:
1555         case SENSOR_MO4000:
1556                 expo = sd->brightness >> 4;
1557                 sd->exposure = setexposure(gspca_dev, expo);
1558                 break;
1559         case SENSOR_MT9V111:
1560                 expo = sd->brightness >> 8;
1561                 sd->exposure = setexposure(gspca_dev, expo);
1562                 return;                 /* don't set the Y offset */
1563         case SENSOR_OM6802:
1564                 expo = sd->brightness >> 6;
1565                 sd->exposure = setexposure(gspca_dev, expo);
1566                 k2 = sd->brightness >> 11;
1567                 break;
1568         }
1569
1570         reg_w1(gspca_dev, 0x96, k2);    /* color matrix Y offset */
1571 }
1572
1573 static void setcontrast(struct gspca_dev *gspca_dev)
1574 {
1575         struct sd *sd = (struct sd *) gspca_dev;
1576         u8 k2;
1577         u8 contrast[6];
1578
1579         k2 = sd->contrast * 0x30 / (CONTRAST_MAX + 1) + 0x10;   /* 10..40 */
1580         contrast[0] = (k2 + 1) / 2;             /* red */
1581         contrast[1] = 0;
1582         contrast[2] = k2;                       /* green */
1583         contrast[3] = 0;
1584         contrast[4] = (k2 + 1) / 5;             /* blue */
1585         contrast[5] = 0;
1586         reg_w(gspca_dev, 0x84, contrast, sizeof contrast);
1587 }
1588
1589 static void setcolors(struct gspca_dev *gspca_dev)
1590 {
1591         struct sd *sd = (struct sd *) gspca_dev;
1592         int i, v;
1593         u8 reg8a[12];                   /* U & V gains */
1594         static s16 uv[6] = {            /* same as reg84 in signed decimal */
1595                 -24, -38, 64,           /* UR UG UB */
1596                  62, -51, -9            /* VR VG VB */
1597         };
1598
1599         for (i = 0; i < 6; i++) {
1600                 v = uv[i] * sd->colors / COLOR_DEF;
1601                 reg8a[i * 2] = v;
1602                 reg8a[i * 2 + 1] = (v >> 8) & 0x0f;
1603         }
1604         reg_w(gspca_dev, 0x8a, reg8a, sizeof reg8a);
1605 }
1606
1607 static void setredblue(struct gspca_dev *gspca_dev)
1608 {
1609         struct sd *sd = (struct sd *) gspca_dev;
1610
1611         reg_w1(gspca_dev, 0x05, sd->red);
1612 /*      reg_w1(gspca_dev, 0x07, 32); */
1613         reg_w1(gspca_dev, 0x06, sd->blue);
1614 }
1615
1616 static void setgamma(struct gspca_dev *gspca_dev)
1617 {
1618         struct sd *sd = (struct sd *) gspca_dev;
1619         int i;
1620         u8 gamma[17];
1621         const u8 *gamma_base;
1622         static const u8 delta[17] = {
1623                 0x00, 0x14, 0x1c, 0x1c, 0x1c, 0x1c, 0x1b, 0x1a,
1624                 0x18, 0x13, 0x10, 0x0e, 0x08, 0x07, 0x04, 0x02, 0x00
1625         };
1626
1627         switch (sd->sensor) {
1628         case SENSOR_HV7131R:
1629         case SENSOR_MT9V111:
1630                 gamma_base = gamma_spec_1;
1631                 break;
1632         case SENSOR_SP80708:
1633                 gamma_base = gamma_spec_2;
1634                 break;
1635         default:
1636                 gamma_base = gamma_def;
1637                 break;
1638         }
1639
1640         for (i = 0; i < sizeof gamma; i++)
1641                 gamma[i] = gamma_base[i]
1642                         + delta[i] * (sd->gamma - GAMMA_DEF) / 32;
1643         reg_w(gspca_dev, 0x20, gamma, sizeof gamma);
1644 }
1645
1646 static void setautogain(struct gspca_dev *gspca_dev)
1647 {
1648         struct sd *sd = (struct sd *) gspca_dev;
1649
1650         if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
1651                 return;
1652         switch (sd->sensor) {
1653         case SENSOR_OV7630:
1654         case SENSOR_OV7648: {
1655                 u8 comb;
1656
1657                 if (sd->sensor == SENSOR_OV7630)
1658                         comb = 0xc0;
1659                 else
1660                         comb = 0xa0;
1661                 if (sd->autogain)
1662                         comb |= 0x03;
1663                 i2c_w1(&sd->gspca_dev, 0x13, comb);
1664                 return;
1665             }
1666         }
1667         if (sd->autogain)
1668                 sd->ag_cnt = AG_CNT_START;
1669         else
1670                 sd->ag_cnt = -1;
1671 }
1672
1673 /* ov7630/ov7648 only */
1674 static void setvflip(struct sd *sd)
1675 {
1676         u8 comn;
1677
1678         if (sd->gspca_dev.ctrl_dis & (1 << VFLIP_IDX))
1679                 return;
1680         if (sd->sensor == SENSOR_OV7630) {
1681                 comn = 0x02;
1682                 if (!sd->vflip)
1683                         comn |= 0x80;
1684         } else {
1685                 comn = 0x06;
1686                 if (sd->vflip)
1687                         comn |= 0x80;
1688         }
1689         i2c_w1(&sd->gspca_dev, 0x75, comn);
1690 }
1691
1692 static void setinfrared(struct sd *sd)
1693 {
1694         if (sd->gspca_dev.ctrl_dis & (1 << INFRARED_IDX))
1695                 return;
1696 /*fixme: different sequence for StarCam Clip and StarCam 370i */
1697 /* Clip */
1698         i2c_w1(&sd->gspca_dev, 0x02,                    /* gpio */
1699                 sd->infrared ? 0x66 : 0x64);
1700 }
1701
1702 static void setfreq(struct gspca_dev *gspca_dev)
1703 {
1704         struct sd *sd = (struct sd *) gspca_dev;
1705
1706         if (gspca_dev->ctrl_dis & (1 << FREQ_IDX))
1707                 return;
1708         if (sd->sensor == SENSOR_OV7660) {
1709                 u8 com8;
1710
1711                 com8 = 0xdf;            /* auto gain/wb/expo */
1712                 switch (sd->freq) {
1713                 case 0: /* Banding filter disabled */
1714                         i2c_w1(gspca_dev, 0x13, com8 | 0x20);
1715                         break;
1716                 case 1: /* 50 hz */
1717                         i2c_w1(gspca_dev, 0x13, com8);
1718                         i2c_w1(gspca_dev, 0x3b, 0x0a);
1719                         break;
1720                 case 2: /* 60 hz */
1721                         i2c_w1(gspca_dev, 0x13, com8);
1722                         i2c_w1(gspca_dev, 0x3b, 0x02);
1723                         break;
1724                 }
1725         } else {
1726                 u8 reg2a = 0, reg2b = 0, reg2d = 0;
1727
1728                 /* Get reg2a / reg2d base values */
1729                 switch (sd->sensor) {
1730                 case SENSOR_OV7630:
1731                         reg2a = 0x08;
1732                         reg2d = 0x01;
1733                         break;
1734                 case SENSOR_OV7648:
1735                         reg2a = 0x11;
1736                         reg2d = 0x81;
1737                         break;
1738                 }
1739
1740                 switch (sd->freq) {
1741                 case 0: /* Banding filter disabled */
1742                         break;
1743                 case 1: /* 50 hz (filter on and framerate adj) */
1744                         reg2a |= 0x80;
1745                         reg2b = 0xac;
1746                         reg2d |= 0x04;
1747                         break;
1748                 case 2: /* 60 hz (filter on, no framerate adj) */
1749                         reg2a |= 0x80;
1750                         reg2d |= 0x04;
1751                         break;
1752                 }
1753                 i2c_w1(gspca_dev, 0x2a, reg2a);
1754                 i2c_w1(gspca_dev, 0x2b, reg2b);
1755                 i2c_w1(gspca_dev, 0x2d, reg2d);
1756         }
1757 }
1758
1759 static void setjpegqual(struct gspca_dev *gspca_dev)
1760 {
1761         struct sd *sd = (struct sd *) gspca_dev;
1762         int i, sc;
1763
1764         if (sd->jpegqual < 50)
1765                 sc = 5000 / sd->jpegqual;
1766         else
1767                 sc = 200 - sd->jpegqual * 2;
1768 #if USB_BUF_SZ < 64
1769 #error "No room enough in usb_buf for quantization table"
1770 #endif
1771         for (i = 0; i < 64; i++)
1772                 gspca_dev->usb_buf[i] =
1773                         (jpeg_head[JPEG_QT0_OFFSET + i] * sc + 50) / 100;
1774         usb_control_msg(gspca_dev->dev,
1775                         usb_sndctrlpipe(gspca_dev->dev, 0),
1776                         0x08,
1777                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1778                         0x0100, 0,
1779                         gspca_dev->usb_buf, 64,
1780                         500);
1781         for (i = 0; i < 64; i++)
1782                 gspca_dev->usb_buf[i] =
1783                         (jpeg_head[JPEG_QT1_OFFSET + i] * sc + 50) / 100;
1784         usb_control_msg(gspca_dev->dev,
1785                         usb_sndctrlpipe(gspca_dev->dev, 0),
1786                         0x08,
1787                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
1788                         0x0140, 0,
1789                         gspca_dev->usb_buf, 64,
1790                         500);
1791
1792         sd->reg18 ^= 0x40;
1793         reg_w1(gspca_dev, 0x18, sd->reg18);
1794 }
1795
1796 /* -- start the camera -- */
1797 static int sd_start(struct gspca_dev *gspca_dev)
1798 {
1799         struct sd *sd = (struct sd *) gspca_dev;
1800         int i;
1801         u8 reg1, reg2, reg17;
1802         const u8 *sn9c1xx;
1803         const u8 (*init)[8];
1804         int mode;
1805         static const u8 C0[] = { 0x2d, 0x2d, 0x3a, 0x05, 0x04, 0x3f };
1806         static const u8 CA[] = { 0x28, 0xd8, 0x14, 0xec };
1807         static const u8 CE[] = { 0x32, 0xdd, 0x2d, 0xdd };      /* MI0360 */
1808         static const u8 CE_ov76xx[] =
1809                                 { 0x32, 0xdd, 0x32, 0xdd };
1810
1811         /* create the JPEG header */
1812         sd->jpeg_hdr = kmalloc(JPEG_HDR_SZ, GFP_KERNEL);
1813         if (!sd->jpeg_hdr)
1814                 return -ENOMEM;
1815         jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
1816                         0x21);          /* JPEG 422 */
1817         jpeg_set_qual(sd->jpeg_hdr, sd->quality);
1818
1819         /* initialize the bridge */
1820         sn9c1xx = sn_tb[sd->sensor];
1821         bridge_init(gspca_dev, sn9c1xx);
1822
1823         /* initialize the sensor */
1824         i2c_w_seq(gspca_dev, sensor_init[sd->sensor]);
1825
1826         switch (sd->sensor) {
1827         case SENSOR_OM6802:
1828                 reg2 = 0x71;
1829                 break;
1830         case SENSOR_SP80708:
1831                 reg2 = 0x62;
1832                 break;
1833         default:
1834                 reg2 = 0x40;
1835                 break;
1836         }
1837         reg_w1(gspca_dev, 0x02, reg2);
1838         reg_w1(gspca_dev, 0x02, reg2);
1839
1840         reg_w1(gspca_dev, 0x15, sn9c1xx[0x15]);
1841         reg_w1(gspca_dev, 0x16, sn9c1xx[0x16]);
1842         reg_w1(gspca_dev, 0x12, sn9c1xx[0x12]);
1843         reg_w1(gspca_dev, 0x13, sn9c1xx[0x13]);
1844         reg_w1(gspca_dev, 0x18, sn9c1xx[0x18]);
1845         reg_w1(gspca_dev, 0xd2, 0x6a);          /* DC29 */
1846         reg_w1(gspca_dev, 0xd3, 0x50);
1847         reg_w1(gspca_dev, 0xc6, 0x00);
1848         reg_w1(gspca_dev, 0xc7, 0x00);
1849         reg_w1(gspca_dev, 0xc8, 0x50);
1850         reg_w1(gspca_dev, 0xc9, 0x3c);
1851         reg_w1(gspca_dev, 0x18, sn9c1xx[0x18]);
1852         switch (sd->sensor) {
1853         case SENSOR_MT9V111:
1854                 reg17 = 0xe0;
1855                 break;
1856         case SENSOR_OV7630:
1857                 reg17 = 0xe2;
1858                 break;
1859         case SENSOR_OV7648:
1860                 reg17 = 0x20;
1861                 break;
1862         case SENSOR_OV7660:
1863                 reg17 = 0xa0;
1864                 break;
1865         case SENSOR_PO1030:
1866                 reg17 = 0xa0;
1867                 break;
1868         default:
1869                 reg17 = 0x60;
1870                 break;
1871         }
1872         reg_w1(gspca_dev, 0x17, reg17);
1873 /* set reg1 was here */
1874         reg_w1(gspca_dev, 0x05, sn9c1xx[5]);    /* red */
1875         reg_w1(gspca_dev, 0x07, sn9c1xx[7]);    /* green */
1876         reg_w1(gspca_dev, 0x06, sn9c1xx[6]);    /* blue */
1877         reg_w1(gspca_dev, 0x14, sn9c1xx[0x14]);
1878
1879         setgamma(gspca_dev);
1880
1881         for (i = 0; i < 8; i++)
1882                 reg_w(gspca_dev, 0x84, reg84, sizeof reg84);
1883         switch (sd->sensor) {
1884         case SENSOR_MT9V111:
1885                 reg_w1(gspca_dev, 0x9a, 0x07);
1886                 reg_w1(gspca_dev, 0x99, 0x59);
1887                 break;
1888         case SENSOR_OM6802:
1889                 reg_w1(gspca_dev, 0x9a, 0x08);
1890                 reg_w1(gspca_dev, 0x99, 0x10);
1891                 break;
1892         case SENSOR_OV7648:
1893                 reg_w1(gspca_dev, 0x9a, 0x0a);
1894                 reg_w1(gspca_dev, 0x99, 0x60);
1895                 break;
1896         case SENSOR_OV7660:
1897         case SENSOR_SP80708:
1898                 reg_w1(gspca_dev, 0x9a, 0x05);
1899                 reg_w1(gspca_dev, 0x99, 0x59);
1900                 break;
1901         default:
1902                 reg_w1(gspca_dev, 0x9a, 0x08);
1903                 reg_w1(gspca_dev, 0x99, 0x59);
1904                 break;
1905         }
1906
1907         reg_w(gspca_dev, 0x84, reg84, sizeof reg84);
1908         reg_w1(gspca_dev, 0x05, sn9c1xx[5]);    /* red */
1909         reg_w1(gspca_dev, 0x07, sn9c1xx[7]);    /* green */
1910         reg_w1(gspca_dev, 0x06, sn9c1xx[6]);    /* blue */
1911
1912         init = NULL;
1913         mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
1914         if (mode)
1915                 reg1 = 0x46;    /* 320x240: clk 48Mhz, video trf enable */
1916         else
1917                 reg1 = 0x06;    /* 640x480: clk 24Mhz, video trf enable */
1918         reg17 = 0x61;           /* 0x:20: enable sensor clock */
1919         switch (sd->sensor) {
1920         case SENSOR_MO4000:
1921                 if (mode) {
1922 /*                      reg1 = 0x46;     * 320 clk 48Mhz 60fp/s */
1923                         reg1 = 0x06;    /* clk 24Mz */
1924                 } else {
1925                         reg17 = 0x22;   /* 640 MCKSIZE */
1926 /*                      reg1 = 0x06;     * 640 clk 24Mz (done) */
1927                 }
1928                 break;
1929         case SENSOR_MT9V111:
1930                 init = mt9v111_sensor_param1;
1931                 if (mode) {
1932                         reg1 = 0x04;    /* 320 clk 48Mhz */
1933                 } else {
1934 /*                      reg1 = 0x06;     * 640 clk 24Mz (done) */
1935                         reg17 = 0xc2;
1936                 }
1937                 break;
1938         case SENSOR_OM6802:
1939                 init = om6802_sensor_param1;
1940                 reg17 = 0x64;           /* 640 MCKSIZE */
1941                 break;
1942         case SENSOR_OV7630:
1943                 setvflip(sd);
1944                 reg17 = 0xe2;
1945                 reg1 = 0x44;
1946                 break;
1947         case SENSOR_OV7648:
1948                 init = ov7648_sensor_param1;
1949                 reg17 = 0x21;
1950 /*              reg1 = 0x42;             * 42 - 46? */
1951                 break;
1952         case SENSOR_OV7660:
1953                 init = ov7660_sensor_param1;
1954                 if (sd->bridge == BRIDGE_SN9C120) {
1955                         if (mode) {             /* 320x240 - 160x120 */
1956                                 reg17 = 0xa2;
1957                                 reg1 = 0x44;    /* 48 Mhz, video trf eneble */
1958                         }
1959                 } else {
1960                         reg17 = 0x22;
1961                         reg1 = 0x06;    /* 24 Mhz, video trf eneble
1962                                          * inverse power down */
1963                 }
1964                 break;
1965         case SENSOR_PO1030:
1966                 init = po1030_sensor_param1;
1967                 reg17 = 0xa2;
1968                 reg1 = 0x44;
1969                 break;
1970         default:
1971 /*      case SENSOR_SP80708: */
1972                 init = sp80708_sensor_param1;
1973                 if (mode) {
1974 /*??                    reg1 = 0x04;     * 320 clk 48Mhz */
1975                 } else {
1976                         reg1 = 0x46;     /* 640 clk 48Mz */
1977                         reg17 = 0xa2;
1978                 }
1979                 break;
1980         }
1981
1982         /* more sensor initialization - param1 */
1983         if (init != NULL) {
1984                 i2c_w_seq(gspca_dev, init);
1985 /*              init = NULL; */
1986         }
1987
1988         reg_w(gspca_dev, 0xc0, C0, 6);
1989         reg_w(gspca_dev, 0xca, CA, 4);
1990         switch (sd->sensor) {
1991         case SENSOR_OV7630:
1992         case SENSOR_OV7648:
1993         case SENSOR_OV7660:
1994                 reg_w(gspca_dev, 0xce, CE_ov76xx, 4);
1995                 break;
1996         default:
1997                 reg_w(gspca_dev, 0xce, CE, 4);
1998                                         /* ?? {0x1e, 0xdd, 0x2d, 0xe7} */
1999                 break;
2000         }
2001
2002
2003         /* here change size mode 0 -> VGA; 1 -> CIF */
2004         sd->reg18 = sn9c1xx[0x18] | (mode << 4) | 0x40;
2005         reg_w1(gspca_dev, 0x18, sd->reg18);
2006         setjpegqual(gspca_dev);
2007
2008         reg_w1(gspca_dev, 0x17, reg17);
2009         reg_w1(gspca_dev, 0x01, reg1);
2010
2011         switch (sd->sensor) {
2012         case SENSOR_OV7630:
2013                 setvflip(sd);
2014                 break;
2015         }
2016         setbrightness(gspca_dev);
2017         setcontrast(gspca_dev);
2018         setautogain(gspca_dev);
2019         setfreq(gspca_dev);
2020         return 0;
2021 }
2022
2023 static void sd_stopN(struct gspca_dev *gspca_dev)
2024 {
2025         struct sd *sd = (struct sd *) gspca_dev;
2026         static const u8 stophv7131[] =
2027                 { 0xa1, 0x11, 0x02, 0x09, 0x00, 0x00, 0x00, 0x10 };
2028         static const u8 stopmi0360[] =
2029                 { 0xb1, 0x5d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10 };
2030         static const u8 stopov7648[] =
2031                 { 0xa1, 0x21, 0x76, 0x20, 0x00, 0x00, 0x00, 0x10 };
2032         u8 data;
2033         const u8 *sn9c1xx;
2034
2035         data = 0x0b;
2036         switch (sd->sensor) {
2037         case SENSOR_HV7131R:
2038                 i2c_w8(gspca_dev, stophv7131);
2039                 data = 0x2b;
2040                 break;
2041         case SENSOR_MI0360:
2042                 i2c_w8(gspca_dev, stopmi0360);
2043                 data = 0x29;
2044                 break;
2045         case SENSOR_OV7648:
2046                 i2c_w8(gspca_dev, stopov7648);
2047                 /* fall thru */
2048         case SENSOR_MT9V111:
2049         case SENSOR_OV7630:
2050         case SENSOR_PO1030:
2051                 data = 0x29;
2052                 break;
2053         }
2054         sn9c1xx = sn_tb[sd->sensor];
2055         reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
2056         reg_w1(gspca_dev, 0x17, sn9c1xx[0x17]);
2057         reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
2058         reg_w1(gspca_dev, 0x01, data);
2059         reg_w1(gspca_dev, 0xf1, 0x00);
2060 }
2061
2062 static void sd_stop0(struct gspca_dev *gspca_dev)
2063 {
2064         struct sd *sd = (struct sd *) gspca_dev;
2065
2066         kfree(sd->jpeg_hdr);
2067 }
2068
2069 static void do_autogain(struct gspca_dev *gspca_dev)
2070 {
2071         struct sd *sd = (struct sd *) gspca_dev;
2072         int delta;
2073         int expotimes;
2074         u8 luma_mean = 130;
2075         u8 luma_delta = 20;
2076
2077         /* Thanks S., without your advice, autobright should not work :) */
2078         if (sd->ag_cnt < 0)
2079                 return;
2080         if (--sd->ag_cnt >= 0)
2081                 return;
2082         sd->ag_cnt = AG_CNT_START;
2083
2084         delta = atomic_read(&sd->avg_lum);
2085         PDEBUG(D_FRAM, "mean lum %d", delta);
2086         if (delta < luma_mean - luma_delta ||
2087             delta > luma_mean + luma_delta) {
2088                 switch (sd->sensor) {
2089                 case SENSOR_HV7131R:
2090                         expotimes = sd->exposure >> 8;
2091                         expotimes += (luma_mean - delta) >> 4;
2092                         if (expotimes < 0)
2093                                 expotimes = 0;
2094                         sd->exposure = setexposure(gspca_dev,
2095                                         (unsigned int) (expotimes << 8));
2096                         break;
2097                 case SENSOR_OM6802:
2098                         expotimes = sd->exposure;
2099                         expotimes += (luma_mean - delta) >> 2;
2100                         if (expotimes < 0)
2101                                 expotimes = 0;
2102                         sd->exposure = setexposure(gspca_dev,
2103                                                    (unsigned int) expotimes);
2104                         setredblue(gspca_dev);
2105                         break;
2106                 default:
2107 /*              case SENSOR_MO4000: */
2108 /*              case SENSOR_MI0360: */
2109 /*              case SENSOR_MT9V111: */
2110                         expotimes = sd->exposure;
2111                         expotimes += (luma_mean - delta) >> 6;
2112                         if (expotimes < 0)
2113                                 expotimes = 0;
2114                         sd->exposure = setexposure(gspca_dev,
2115                                                    (unsigned int) expotimes);
2116                         setredblue(gspca_dev);
2117                         break;
2118                 }
2119         }
2120 }
2121
2122 /* scan the URB packets */
2123 /* This function is run at interrupt level. */
2124 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2125                         u8 *data,                       /* isoc packet */
2126                         int len)                        /* iso packet length */
2127 {
2128         struct sd *sd = (struct sd *) gspca_dev;
2129         int sof, avg_lum;
2130
2131         sof = len - 64;
2132         if (sof >= 0 && data[sof] == 0xff && data[sof + 1] == 0xd9) {
2133
2134                 /* end of frame */
2135                 gspca_frame_add(gspca_dev, LAST_PACKET,
2136                                 data, sof + 2);
2137                 if (sd->ag_cnt < 0)
2138                         return;
2139 /* w1 w2 w3 */
2140 /* w4 w5 w6 */
2141 /* w7 w8 */
2142 /* w4 */
2143                 avg_lum = ((data[sof + 29] << 8) | data[sof + 30]) >> 6;
2144 /* w6 */
2145                 avg_lum += ((data[sof + 33] << 8) | data[sof + 34]) >> 6;
2146 /* w2 */
2147                 avg_lum += ((data[sof + 25] << 8) | data[sof + 26]) >> 6;
2148 /* w8 */
2149                 avg_lum += ((data[sof + 37] << 8) | data[sof + 38]) >> 6;
2150 /* w5 */
2151                 avg_lum += ((data[sof + 31] << 8) | data[sof + 32]) >> 4;
2152                 avg_lum >>= 4;
2153                 atomic_set(&sd->avg_lum, avg_lum);
2154                 return;
2155         }
2156         if (gspca_dev->last_packet_type == LAST_PACKET) {
2157
2158                 /* put the JPEG 422 header */
2159                 gspca_frame_add(gspca_dev, FIRST_PACKET,
2160                         sd->jpeg_hdr, JPEG_HDR_SZ);
2161         }
2162         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
2163 }
2164
2165 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2166 {
2167         struct sd *sd = (struct sd *) gspca_dev;
2168
2169         sd->brightness = val;
2170         if (gspca_dev->streaming)
2171                 setbrightness(gspca_dev);
2172         return 0;
2173 }
2174
2175 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2176 {
2177         struct sd *sd = (struct sd *) gspca_dev;
2178
2179         *val = sd->brightness;
2180         return 0;
2181 }
2182
2183 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2184 {
2185         struct sd *sd = (struct sd *) gspca_dev;
2186
2187         sd->contrast = val;
2188         if (gspca_dev->streaming)
2189                 setcontrast(gspca_dev);
2190         return 0;
2191 }
2192
2193 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2194 {
2195         struct sd *sd = (struct sd *) gspca_dev;
2196
2197         *val = sd->contrast;
2198         return 0;
2199 }
2200
2201 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2202 {
2203         struct sd *sd = (struct sd *) gspca_dev;
2204
2205         sd->colors = val;
2206         if (gspca_dev->streaming)
2207                 setcolors(gspca_dev);
2208         return 0;
2209 }
2210
2211 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2212 {
2213         struct sd *sd = (struct sd *) gspca_dev;
2214
2215         *val = sd->colors;
2216         return 0;
2217 }
2218
2219 static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val)
2220 {
2221         struct sd *sd = (struct sd *) gspca_dev;
2222
2223         sd->blue = val;
2224         if (gspca_dev->streaming)
2225                 setredblue(gspca_dev);
2226         return 0;
2227 }
2228
2229 static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val)
2230 {
2231         struct sd *sd = (struct sd *) gspca_dev;
2232
2233         *val = sd->blue;
2234         return 0;
2235 }
2236
2237 static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val)
2238 {
2239         struct sd *sd = (struct sd *) gspca_dev;
2240
2241         sd->red = val;
2242         if (gspca_dev->streaming)
2243                 setredblue(gspca_dev);
2244         return 0;
2245 }
2246
2247 static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val)
2248 {
2249         struct sd *sd = (struct sd *) gspca_dev;
2250
2251         *val = sd->red;
2252         return 0;
2253 }
2254
2255 static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
2256 {
2257         struct sd *sd = (struct sd *) gspca_dev;
2258
2259         sd->gamma = val;
2260         if (gspca_dev->streaming)
2261                 setgamma(gspca_dev);
2262         return 0;
2263 }
2264
2265 static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
2266 {
2267         struct sd *sd = (struct sd *) gspca_dev;
2268
2269         *val = sd->gamma;
2270         return 0;
2271 }
2272
2273 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
2274 {
2275         struct sd *sd = (struct sd *) gspca_dev;
2276
2277         sd->autogain = val;
2278         if (gspca_dev->streaming)
2279                 setautogain(gspca_dev);
2280         return 0;
2281 }
2282
2283 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
2284 {
2285         struct sd *sd = (struct sd *) gspca_dev;
2286
2287         *val = sd->autogain;
2288         return 0;
2289 }
2290
2291 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2292 {
2293         struct sd *sd = (struct sd *) gspca_dev;
2294
2295         sd->vflip = val;
2296         if (gspca_dev->streaming)
2297                 setvflip(sd);
2298         return 0;
2299 }
2300
2301 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2302 {
2303         struct sd *sd = (struct sd *) gspca_dev;
2304
2305         *val = sd->vflip;
2306         return 0;
2307 }
2308
2309 static int sd_setinfrared(struct gspca_dev *gspca_dev, __s32 val)
2310 {
2311         struct sd *sd = (struct sd *) gspca_dev;
2312
2313         sd->infrared = val;
2314         if (gspca_dev->streaming)
2315                 setinfrared(sd);
2316         return 0;
2317 }
2318
2319 static int sd_getinfrared(struct gspca_dev *gspca_dev, __s32 *val)
2320 {
2321         struct sd *sd = (struct sd *) gspca_dev;
2322
2323         *val = sd->infrared;
2324         return 0;
2325 }
2326
2327 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
2328 {
2329         struct sd *sd = (struct sd *) gspca_dev;
2330
2331         sd->freq = val;
2332         if (gspca_dev->streaming)
2333                 setfreq(gspca_dev);
2334         return 0;
2335 }
2336
2337 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
2338 {
2339         struct sd *sd = (struct sd *) gspca_dev;
2340
2341         *val = sd->freq;
2342         return 0;
2343 }
2344
2345 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
2346                         struct v4l2_jpegcompression *jcomp)
2347 {
2348         struct sd *sd = (struct sd *) gspca_dev;
2349
2350         if (jcomp->quality < QUALITY_MIN)
2351                 sd->quality = QUALITY_MIN;
2352         else if (jcomp->quality > QUALITY_MAX)
2353                 sd->quality = QUALITY_MAX;
2354         else
2355                 sd->quality = jcomp->quality;
2356         if (gspca_dev->streaming)
2357                 jpeg_set_qual(sd->jpeg_hdr, sd->quality);
2358         return 0;
2359 }
2360
2361 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
2362                         struct v4l2_jpegcompression *jcomp)
2363 {
2364         struct sd *sd = (struct sd *) gspca_dev;
2365
2366         memset(jcomp, 0, sizeof *jcomp);
2367         jcomp->quality = sd->quality;
2368         jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
2369                         | V4L2_JPEG_MARKER_DQT;
2370         return 0;
2371 }
2372
2373 static int sd_querymenu(struct gspca_dev *gspca_dev,
2374                         struct v4l2_querymenu *menu)
2375 {
2376         switch (menu->id) {
2377         case V4L2_CID_POWER_LINE_FREQUENCY:
2378                 switch (menu->index) {
2379                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2380                         strcpy((char *) menu->name, "NoFliker");
2381                         return 0;
2382                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2383                         strcpy((char *) menu->name, "50 Hz");
2384                         return 0;
2385                 case 2:         /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2386                         strcpy((char *) menu->name, "60 Hz");
2387                         return 0;
2388                 }
2389                 break;
2390         }
2391         return -EINVAL;
2392 }
2393
2394 /* sub-driver description */
2395 static const struct sd_desc sd_desc = {
2396         .name = MODULE_NAME,
2397         .ctrls = sd_ctrls,
2398         .nctrls = ARRAY_SIZE(sd_ctrls),
2399         .config = sd_config,
2400         .init = sd_init,
2401         .start = sd_start,
2402         .stopN = sd_stopN,
2403         .stop0 = sd_stop0,
2404         .pkt_scan = sd_pkt_scan,
2405         .dq_callback = do_autogain,
2406         .get_jcomp = sd_get_jcomp,
2407         .set_jcomp = sd_set_jcomp,
2408         .querymenu = sd_querymenu,
2409 };
2410
2411 /* -- module initialisation -- */
2412 #define BS(bridge, sensor) \
2413         .driver_info = (BRIDGE_ ## bridge << 16) \
2414                         | SENSOR_ ## sensor
2415 static const __devinitdata struct usb_device_id device_table[] = {
2416 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
2417         {USB_DEVICE(0x0458, 0x7025), BS(SN9C120, MI0360)},
2418         {USB_DEVICE(0x0458, 0x702e), BS(SN9C120, OV7660)},
2419 #endif
2420         {USB_DEVICE(0x045e, 0x00f5), BS(SN9C105, OV7660)},
2421         {USB_DEVICE(0x045e, 0x00f7), BS(SN9C105, OV7660)},
2422         {USB_DEVICE(0x0471, 0x0327), BS(SN9C105, MI0360)},
2423         {USB_DEVICE(0x0471, 0x0328), BS(SN9C105, MI0360)},
2424         {USB_DEVICE(0x0471, 0x0330), BS(SN9C105, MI0360)},
2425         {USB_DEVICE(0x06f8, 0x3004), BS(SN9C105, OV7660)},
2426         {USB_DEVICE(0x06f8, 0x3008), BS(SN9C105, OV7660)},
2427 /*      {USB_DEVICE(0x0c45, 0x603a), BS(SN9C102P, OV7648)}, */
2428         {USB_DEVICE(0x0c45, 0x6040), BS(SN9C102P, HV7131R)},
2429 /*      {USB_DEVICE(0x0c45, 0x607a), BS(SN9C102P, OV7648)}, */
2430 /*      {USB_DEVICE(0x0c45, 0x607b), BS(SN9C102P, OV7660)}, */
2431         {USB_DEVICE(0x0c45, 0x607c), BS(SN9C102P, HV7131R)},
2432 /*      {USB_DEVICE(0x0c45, 0x607e), BS(SN9C102P, OV7630)}, */
2433         {USB_DEVICE(0x0c45, 0x60c0), BS(SN9C105, MI0360)},
2434 /*      {USB_DEVICE(0x0c45, 0x60c2), BS(SN9C105, P1030xC)}, */
2435 /*      {USB_DEVICE(0x0c45, 0x60c8), BS(SN9C105, OM6802)}, */
2436 /*      {USB_DEVICE(0x0c45, 0x60cc), BS(SN9C105, HV7131GP)}, */
2437         {USB_DEVICE(0x0c45, 0x60ec), BS(SN9C105, MO4000)},
2438 /*      {USB_DEVICE(0x0c45, 0x60ef), BS(SN9C105, ICM105C)}, */
2439 /*      {USB_DEVICE(0x0c45, 0x60fa), BS(SN9C105, OV7648)}, */
2440         {USB_DEVICE(0x0c45, 0x60fb), BS(SN9C105, OV7660)},
2441 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
2442         {USB_DEVICE(0x0c45, 0x60fc), BS(SN9C105, HV7131R)},
2443         {USB_DEVICE(0x0c45, 0x60fe), BS(SN9C105, OV7630)},
2444 #endif
2445         {USB_DEVICE(0x0c45, 0x6100), BS(SN9C120, MI0360)},      /*sn9c128*/
2446 /*      {USB_DEVICE(0x0c45, 0x6102), BS(SN9C120, P1030xC)}, */
2447 /*      {USB_DEVICE(0x0c45, 0x6108), BS(SN9C120, OM6802)}, */
2448         {USB_DEVICE(0x0c45, 0x610a), BS(SN9C120, OV7648)},      /*sn9c128*/
2449         {USB_DEVICE(0x0c45, 0x610b), BS(SN9C120, OV7660)},      /*sn9c128*/
2450         {USB_DEVICE(0x0c45, 0x610c), BS(SN9C120, HV7131R)},     /*sn9c128*/
2451         {USB_DEVICE(0x0c45, 0x610e), BS(SN9C120, OV7630)},      /*sn9c128*/
2452 /*      {USB_DEVICE(0x0c45, 0x610f), BS(SN9C120, S5K53BEB)}, */
2453 /*      {USB_DEVICE(0x0c45, 0x6122), BS(SN9C110, ICM105C)}, */
2454 /*      {USB_DEVICE(0x0c45, 0x6123), BS(SN9C110, SanyoCCD)}, */
2455         {USB_DEVICE(0x0c45, 0x6128), BS(SN9C120, OM6802)},      /*sn9c325?*/
2456 /*bw600.inf:*/
2457         {USB_DEVICE(0x0c45, 0x612a), BS(SN9C120, OV7648)},      /*sn9c325?*/
2458         {USB_DEVICE(0x0c45, 0x612c), BS(SN9C110, MO4000)},
2459         {USB_DEVICE(0x0c45, 0x612e), BS(SN9C110, OV7630)},
2460 /*      {USB_DEVICE(0x0c45, 0x612f), BS(SN9C110, ICM105C)}, */
2461 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
2462         {USB_DEVICE(0x0c45, 0x6130), BS(SN9C120, MI0360)},
2463 #endif
2464 /*      {USB_DEVICE(0x0c45, 0x6132), BS(SN9C120, OV7670)}, */
2465         {USB_DEVICE(0x0c45, 0x6138), BS(SN9C120, MO4000)},
2466         {USB_DEVICE(0x0c45, 0x613a), BS(SN9C120, OV7648)},
2467 #if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
2468         {USB_DEVICE(0x0c45, 0x613b), BS(SN9C120, OV7660)},
2469 #endif
2470         {USB_DEVICE(0x0c45, 0x613c), BS(SN9C120, HV7131R)},
2471         {USB_DEVICE(0x0c45, 0x613e), BS(SN9C120, OV7630)},
2472 /*      {USB_DEVICE(0x0c45, 0x6142), BS(SN9C120, PO2030N)},      *sn9c120b*/
2473         {USB_DEVICE(0x0c45, 0x6143), BS(SN9C120, SP80708)},     /*sn9c120b*/
2474         {USB_DEVICE(0x0c45, 0x6148), BS(SN9C120, OM6802)},      /*sn9c120b*/
2475         {}
2476 };
2477 MODULE_DEVICE_TABLE(usb, device_table);
2478
2479 /* -- device connect -- */
2480 static int sd_probe(struct usb_interface *intf,
2481                     const struct usb_device_id *id)
2482 {
2483         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2484                                 THIS_MODULE);
2485 }
2486
2487 static struct usb_driver sd_driver = {
2488         .name = MODULE_NAME,
2489         .id_table = device_table,
2490         .probe = sd_probe,
2491         .disconnect = gspca_disconnect,
2492 #ifdef CONFIG_PM
2493         .suspend = gspca_suspend,
2494         .resume = gspca_resume,
2495 #endif
2496 };
2497
2498 /* -- module insert / remove -- */
2499 static int __init sd_mod_init(void)
2500 {
2501         int ret;
2502         ret = usb_register(&sd_driver);
2503         if (ret < 0)
2504                 return ret;
2505         info("registered");
2506         return 0;
2507 }
2508 static void __exit sd_mod_exit(void)
2509 {
2510         usb_deregister(&sd_driver);
2511         info("deregistered");
2512 }
2513
2514 module_init(sd_mod_init);
2515 module_exit(sd_mod_exit);