]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/media/video/gspca/ov519.c
7ae304d0ebf6fc54c6cf604cdb01d094f33d7556
[linux-beck.git] / drivers / media / video / gspca / ov519.c
1 /**
2  * OV519 driver
3  *
4  * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
6  *
7  * This module is adapted from the ov51x-jpeg package, which itself
8  * was adapted from the ov511 driver.
9  *
10  * Original copyright for the ov511 driver is:
11  *
12  * Copyright (c) 1999-2006 Mark W. McClelland
13  * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
14  * Many improvements by Bret Wallach <bwallac1@san.rr.com>
15  * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
16  * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
17  * Changes by Claudio Matsuoka <claudio@conectiva.com>
18  *
19  * ov51x-jpeg original copyright is:
20  *
21  * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
22  * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37  *
38  */
39 #define MODULE_NAME "ov519"
40
41 #include <linux/input.h>
42 #include "gspca.h"
43
44 /* The jpeg_hdr is used by w996Xcf only */
45 /* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */
46 #define CONEX_CAM
47 #include "jpeg.h"
48
49 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
50 MODULE_DESCRIPTION("OV519 USB Camera Driver");
51 MODULE_LICENSE("GPL");
52
53 /* global parameters */
54 static int frame_rate;
55
56 /* Number of times to retry a failed I2C transaction. Increase this if you
57  * are getting "Failed to read sensor ID..." */
58 static int i2c_detect_tries = 10;
59
60 /* controls */
61 enum e_ctrl {
62         BRIGHTNESS,
63         CONTRAST,
64         COLORS,
65         HFLIP,
66         VFLIP,
67         AUTOBRIGHT,
68         FREQ,
69         NCTRL           /* number of controls */
70 };
71
72 /* ov519 device descriptor */
73 struct sd {
74         struct gspca_dev gspca_dev;             /* !! must be the first item */
75
76         struct gspca_ctrl ctrls[NCTRL];
77
78         u8 packet_nr;
79
80         char bridge;
81 #define BRIDGE_OV511            0
82 #define BRIDGE_OV511PLUS        1
83 #define BRIDGE_OV518            2
84 #define BRIDGE_OV518PLUS        3
85 #define BRIDGE_OV519            4
86 #define BRIDGE_OVFX2            5
87 #define BRIDGE_W9968CF          6
88 #define BRIDGE_MASK             7
89
90         char invert_led;
91 #define BRIDGE_INVERT_LED       8
92
93         char snapshot_pressed;
94         char snapshot_needs_reset;
95
96         /* Determined by sensor type */
97         u8 sif;
98
99         u8 quality;
100 #define QUALITY_MIN 50
101 #define QUALITY_MAX 70
102 #define QUALITY_DEF 50
103
104         u8 stopped;             /* Streaming is temporarily paused */
105         u8 first_frame;
106
107         u8 frame_rate;          /* current Framerate */
108         u8 clockdiv;            /* clockdiv override */
109
110         s8 sensor;              /* Type of image sensor chip (SEN_*) */
111
112         u8 sensor_addr;
113         u16 sensor_width;
114         u16 sensor_height;
115         s16 sensor_reg_cache[256];
116
117         u8 jpeg_hdr[JPEG_HDR_SZ];
118 };
119 enum sensors {
120         SEN_OV2610,
121         SEN_OV3610,
122         SEN_OV6620,
123         SEN_OV6630,
124         SEN_OV66308AF,
125         SEN_OV7610,
126         SEN_OV7620,
127         SEN_OV7620AE,
128         SEN_OV7640,
129         SEN_OV7648,
130         SEN_OV7670,
131         SEN_OV76BE,
132         SEN_OV8610,
133 };
134
135 /* Note this is a bit of a hack, but the w9968cf driver needs the code for all
136    the ov sensors which is already present here. When we have the time we
137    really should move the sensor drivers to v4l2 sub drivers. */
138 #include "w996Xcf.c"
139
140 /* V4L2 controls supported by the driver */
141 static void setbrightness(struct gspca_dev *gspca_dev);
142 static void setcontrast(struct gspca_dev *gspca_dev);
143 static void setcolors(struct gspca_dev *gspca_dev);
144 static void sethvflip(struct gspca_dev *gspca_dev);
145 static void setautobright(struct gspca_dev *gspca_dev);
146 static void setfreq(struct gspca_dev *gspca_dev);
147 static void setfreq_i(struct sd *sd);
148
149 static const struct ctrl sd_ctrls[] = {
150 [BRIGHTNESS] = {
151             {
152                 .id      = V4L2_CID_BRIGHTNESS,
153                 .type    = V4L2_CTRL_TYPE_INTEGER,
154                 .name    = "Brightness",
155                 .minimum = 0,
156                 .maximum = 255,
157                 .step    = 1,
158                 .default_value = 127,
159             },
160             .set_control = setbrightness,
161         },
162 [CONTRAST] = {
163             {
164                 .id      = V4L2_CID_CONTRAST,
165                 .type    = V4L2_CTRL_TYPE_INTEGER,
166                 .name    = "Contrast",
167                 .minimum = 0,
168                 .maximum = 255,
169                 .step    = 1,
170                 .default_value = 127,
171             },
172             .set_control = setcontrast,
173         },
174 [COLORS] = {
175             {
176                 .id      = V4L2_CID_SATURATION,
177                 .type    = V4L2_CTRL_TYPE_INTEGER,
178                 .name    = "Color",
179                 .minimum = 0,
180                 .maximum = 255,
181                 .step    = 1,
182                 .default_value = 127,
183             },
184             .set_control = setcolors,
185         },
186 /* The flip controls work with ov7670 only */
187 [HFLIP] = {
188             {
189                 .id      = V4L2_CID_HFLIP,
190                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
191                 .name    = "Mirror",
192                 .minimum = 0,
193                 .maximum = 1,
194                 .step    = 1,
195                 .default_value = 0,
196             },
197             .set_control = sethvflip,
198         },
199 [VFLIP] = {
200             {
201                 .id      = V4L2_CID_VFLIP,
202                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
203                 .name    = "Vflip",
204                 .minimum = 0,
205                 .maximum = 1,
206                 .step    = 1,
207                 .default_value = 0,
208             },
209             .set_control = sethvflip,
210         },
211 [AUTOBRIGHT] = {
212             {
213                 .id      = V4L2_CID_AUTOBRIGHTNESS,
214                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
215                 .name    = "Auto Brightness",
216                 .minimum = 0,
217                 .maximum = 1,
218                 .step    = 1,
219                 .default_value = 1,
220             },
221             .set_control = setautobright,
222         },
223 [FREQ] = {
224             {
225                 .id      = V4L2_CID_POWER_LINE_FREQUENCY,
226                 .type    = V4L2_CTRL_TYPE_MENU,
227                 .name    = "Light frequency filter",
228                 .minimum = 0,
229                 .maximum = 2,   /* 0: no flicker, 1: 50Hz, 2:60Hz, 3: auto */
230                 .step    = 1,
231                 .default_value = 0,
232             },
233             .set_control = setfreq,
234         },
235 };
236
237 /* table of the disabled controls */
238 static const unsigned ctrl_dis[] = {
239 [SEN_OV2610] =          (1 << NCTRL) - 1,       /* no control */
240
241 [SEN_OV3610] =          (1 << NCTRL) - 1,       /* no control */
242
243 [SEN_OV6620] =          (1 << HFLIP) |
244                         (1 << VFLIP),
245
246 [SEN_OV6630] =          (1 << HFLIP) |
247                         (1 << VFLIP),
248
249 [SEN_OV66308AF] =       (1 << HFLIP) |
250                         (1 << VFLIP),
251
252 [SEN_OV7610] =          (1 << HFLIP) |
253                         (1 << VFLIP),
254
255 [SEN_OV7620] =          (1 << HFLIP) |
256                         (1 << VFLIP),
257
258 [SEN_OV7620AE] =        (1 << HFLIP) |
259                         (1 << VFLIP),
260
261 [SEN_OV7640] =          (1 << HFLIP) |
262                         (1 << VFLIP) |
263                         (1 << AUTOBRIGHT) |
264                         (1 << CONTRAST),
265
266 [SEN_OV7648] =          (1 << HFLIP) |
267                         (1 << VFLIP) |
268                         (1 << AUTOBRIGHT) |
269                         (1 << CONTRAST),
270
271 [SEN_OV7670] =          (1 << COLORS) |
272                         (1 << AUTOBRIGHT),
273
274 [SEN_OV76BE] =          (1 << HFLIP) |
275                         (1 << VFLIP),
276
277 [SEN_OV8610] =          (1 << HFLIP) |
278                         (1 << VFLIP) |
279                         (1 << FREQ),
280 };
281
282 static const struct v4l2_pix_format ov519_vga_mode[] = {
283         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
284                 .bytesperline = 320,
285                 .sizeimage = 320 * 240 * 3 / 8 + 590,
286                 .colorspace = V4L2_COLORSPACE_JPEG,
287                 .priv = 1},
288         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
289                 .bytesperline = 640,
290                 .sizeimage = 640 * 480 * 3 / 8 + 590,
291                 .colorspace = V4L2_COLORSPACE_JPEG,
292                 .priv = 0},
293 };
294 static const struct v4l2_pix_format ov519_sif_mode[] = {
295         {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
296                 .bytesperline = 160,
297                 .sizeimage = 160 * 120 * 3 / 8 + 590,
298                 .colorspace = V4L2_COLORSPACE_JPEG,
299                 .priv = 3},
300         {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
301                 .bytesperline = 176,
302                 .sizeimage = 176 * 144 * 3 / 8 + 590,
303                 .colorspace = V4L2_COLORSPACE_JPEG,
304                 .priv = 1},
305         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
306                 .bytesperline = 320,
307                 .sizeimage = 320 * 240 * 3 / 8 + 590,
308                 .colorspace = V4L2_COLORSPACE_JPEG,
309                 .priv = 2},
310         {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
311                 .bytesperline = 352,
312                 .sizeimage = 352 * 288 * 3 / 8 + 590,
313                 .colorspace = V4L2_COLORSPACE_JPEG,
314                 .priv = 0},
315 };
316
317 /* Note some of the sizeimage values for the ov511 / ov518 may seem
318    larger then necessary, however they need to be this big as the ov511 /
319    ov518 always fills the entire isoc frame, using 0 padding bytes when
320    it doesn't have any data. So with low framerates the amount of data
321    transfered can become quite large (libv4l will remove all the 0 padding
322    in userspace). */
323 static const struct v4l2_pix_format ov518_vga_mode[] = {
324         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
325                 .bytesperline = 320,
326                 .sizeimage = 320 * 240 * 3,
327                 .colorspace = V4L2_COLORSPACE_JPEG,
328                 .priv = 1},
329         {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
330                 .bytesperline = 640,
331                 .sizeimage = 640 * 480 * 2,
332                 .colorspace = V4L2_COLORSPACE_JPEG,
333                 .priv = 0},
334 };
335 static const struct v4l2_pix_format ov518_sif_mode[] = {
336         {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
337                 .bytesperline = 160,
338                 .sizeimage = 70000,
339                 .colorspace = V4L2_COLORSPACE_JPEG,
340                 .priv = 3},
341         {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
342                 .bytesperline = 176,
343                 .sizeimage = 70000,
344                 .colorspace = V4L2_COLORSPACE_JPEG,
345                 .priv = 1},
346         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
347                 .bytesperline = 320,
348                 .sizeimage = 320 * 240 * 3,
349                 .colorspace = V4L2_COLORSPACE_JPEG,
350                 .priv = 2},
351         {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
352                 .bytesperline = 352,
353                 .sizeimage = 352 * 288 * 3,
354                 .colorspace = V4L2_COLORSPACE_JPEG,
355                 .priv = 0},
356 };
357
358 static const struct v4l2_pix_format ov511_vga_mode[] = {
359         {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
360                 .bytesperline = 320,
361                 .sizeimage = 320 * 240 * 3,
362                 .colorspace = V4L2_COLORSPACE_JPEG,
363                 .priv = 1},
364         {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
365                 .bytesperline = 640,
366                 .sizeimage = 640 * 480 * 2,
367                 .colorspace = V4L2_COLORSPACE_JPEG,
368                 .priv = 0},
369 };
370 static const struct v4l2_pix_format ov511_sif_mode[] = {
371         {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
372                 .bytesperline = 160,
373                 .sizeimage = 70000,
374                 .colorspace = V4L2_COLORSPACE_JPEG,
375                 .priv = 3},
376         {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
377                 .bytesperline = 176,
378                 .sizeimage = 70000,
379                 .colorspace = V4L2_COLORSPACE_JPEG,
380                 .priv = 1},
381         {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
382                 .bytesperline = 320,
383                 .sizeimage = 320 * 240 * 3,
384                 .colorspace = V4L2_COLORSPACE_JPEG,
385                 .priv = 2},
386         {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
387                 .bytesperline = 352,
388                 .sizeimage = 352 * 288 * 3,
389                 .colorspace = V4L2_COLORSPACE_JPEG,
390                 .priv = 0},
391 };
392
393 static const struct v4l2_pix_format ovfx2_vga_mode[] = {
394         {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
395                 .bytesperline = 320,
396                 .sizeimage = 320 * 240,
397                 .colorspace = V4L2_COLORSPACE_SRGB,
398                 .priv = 1},
399         {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
400                 .bytesperline = 640,
401                 .sizeimage = 640 * 480,
402                 .colorspace = V4L2_COLORSPACE_SRGB,
403                 .priv = 0},
404 };
405 static const struct v4l2_pix_format ovfx2_cif_mode[] = {
406         {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
407                 .bytesperline = 160,
408                 .sizeimage = 160 * 120,
409                 .colorspace = V4L2_COLORSPACE_SRGB,
410                 .priv = 3},
411         {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
412                 .bytesperline = 176,
413                 .sizeimage = 176 * 144,
414                 .colorspace = V4L2_COLORSPACE_SRGB,
415                 .priv = 1},
416         {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
417                 .bytesperline = 320,
418                 .sizeimage = 320 * 240,
419                 .colorspace = V4L2_COLORSPACE_SRGB,
420                 .priv = 2},
421         {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
422                 .bytesperline = 352,
423                 .sizeimage = 352 * 288,
424                 .colorspace = V4L2_COLORSPACE_SRGB,
425                 .priv = 0},
426 };
427 static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
428         {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
429                 .bytesperline = 1600,
430                 .sizeimage = 1600 * 1200,
431                 .colorspace = V4L2_COLORSPACE_SRGB},
432 };
433 static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
434         {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
435                 .bytesperline = 640,
436                 .sizeimage = 640 * 480,
437                 .colorspace = V4L2_COLORSPACE_SRGB,
438                 .priv = 1},
439         {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
440                 .bytesperline = 800,
441                 .sizeimage = 800 * 600,
442                 .colorspace = V4L2_COLORSPACE_SRGB,
443                 .priv = 1},
444         {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
445                 .bytesperline = 1024,
446                 .sizeimage = 1024 * 768,
447                 .colorspace = V4L2_COLORSPACE_SRGB,
448                 .priv = 1},
449         {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
450                 .bytesperline = 1600,
451                 .sizeimage = 1600 * 1200,
452                 .colorspace = V4L2_COLORSPACE_SRGB,
453                 .priv = 0},
454         {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
455                 .bytesperline = 2048,
456                 .sizeimage = 2048 * 1536,
457                 .colorspace = V4L2_COLORSPACE_SRGB,
458                 .priv = 0},
459 };
460
461 /* Registers common to OV511 / OV518 */
462 #define R51x_FIFO_PSIZE                 0x30    /* 2 bytes wide w/ OV518(+) */
463 #define R51x_SYS_RESET                  0x50
464         /* Reset type flags */
465         #define OV511_RESET_OMNICE      0x08
466 #define R51x_SYS_INIT                   0x53
467 #define R51x_SYS_SNAP                   0x52
468 #define R51x_SYS_CUST_ID                0x5f
469 #define R51x_COMP_LUT_BEGIN             0x80
470
471 /* OV511 Camera interface register numbers */
472 #define R511_CAM_DELAY                  0x10
473 #define R511_CAM_EDGE                   0x11
474 #define R511_CAM_PXCNT                  0x12
475 #define R511_CAM_LNCNT                  0x13
476 #define R511_CAM_PXDIV                  0x14
477 #define R511_CAM_LNDIV                  0x15
478 #define R511_CAM_UV_EN                  0x16
479 #define R511_CAM_LINE_MODE              0x17
480 #define R511_CAM_OPTS                   0x18
481
482 #define R511_SNAP_FRAME                 0x19
483 #define R511_SNAP_PXCNT                 0x1a
484 #define R511_SNAP_LNCNT                 0x1b
485 #define R511_SNAP_PXDIV                 0x1c
486 #define R511_SNAP_LNDIV                 0x1d
487 #define R511_SNAP_UV_EN                 0x1e
488 #define R511_SNAP_UV_EN                 0x1e
489 #define R511_SNAP_OPTS                  0x1f
490
491 #define R511_DRAM_FLOW_CTL              0x20
492 #define R511_FIFO_OPTS                  0x31
493 #define R511_I2C_CTL                    0x40
494 #define R511_SYS_LED_CTL                0x55    /* OV511+ only */
495 #define R511_COMP_EN                    0x78
496 #define R511_COMP_LUT_EN                0x79
497
498 /* OV518 Camera interface register numbers */
499 #define R518_GPIO_OUT                   0x56    /* OV518(+) only */
500 #define R518_GPIO_CTL                   0x57    /* OV518(+) only */
501
502 /* OV519 Camera interface register numbers */
503 #define OV519_R10_H_SIZE                0x10
504 #define OV519_R11_V_SIZE                0x11
505 #define OV519_R12_X_OFFSETL             0x12
506 #define OV519_R13_X_OFFSETH             0x13
507 #define OV519_R14_Y_OFFSETL             0x14
508 #define OV519_R15_Y_OFFSETH             0x15
509 #define OV519_R16_DIVIDER               0x16
510 #define OV519_R20_DFR                   0x20
511 #define OV519_R25_FORMAT                0x25
512
513 /* OV519 System Controller register numbers */
514 #define OV519_R51_RESET1                0x51
515 #define OV519_R54_EN_CLK1               0x54
516
517 #define OV519_GPIO_DATA_OUT0            0x71
518 #define OV519_GPIO_IO_CTRL0             0x72
519
520 /*#define OV511_ENDPOINT_ADDRESS 1       * Isoc endpoint number */
521
522 /*
523  * The FX2 chip does not give us a zero length read at end of frame.
524  * It does, however, give a short read at the end of a frame, if
525  * necessary, rather than run two frames together.
526  *
527  * By choosing the right bulk transfer size, we are guaranteed to always
528  * get a short read for the last read of each frame.  Frame sizes are
529  * always a composite number (width * height, or a multiple) so if we
530  * choose a prime number, we are guaranteed that the last read of a
531  * frame will be short.
532  *
533  * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
534  * otherwise EOVERFLOW "babbling" errors occur.  I have not been able
535  * to figure out why.  [PMiller]
536  *
537  * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
538  *
539  * It isn't enough to know the number of bytes per frame, in case we
540  * have data dropouts or buffer overruns (even though the FX2 double
541  * buffers, there are some pretty strict real time constraints for
542  * isochronous transfer for larger frame sizes).
543  */
544 #define OVFX2_BULK_SIZE (13 * 4096)
545
546 /* I2C registers */
547 #define R51x_I2C_W_SID          0x41
548 #define R51x_I2C_SADDR_3        0x42
549 #define R51x_I2C_SADDR_2        0x43
550 #define R51x_I2C_R_SID          0x44
551 #define R51x_I2C_DATA           0x45
552 #define R518_I2C_CTL            0x47    /* OV518(+) only */
553 #define OVFX2_I2C_ADDR          0x00
554
555 /* I2C ADDRESSES */
556 #define OV7xx0_SID   0x42
557 #define OV_HIRES_SID 0x60               /* OV9xxx / OV2xxx / OV3xxx */
558 #define OV8xx0_SID   0xa0
559 #define OV6xx0_SID   0xc0
560
561 /* OV7610 registers */
562 #define OV7610_REG_GAIN         0x00    /* gain setting (5:0) */
563 #define OV7610_REG_BLUE         0x01    /* blue channel balance */
564 #define OV7610_REG_RED          0x02    /* red channel balance */
565 #define OV7610_REG_SAT          0x03    /* saturation */
566 #define OV8610_REG_HUE          0x04    /* 04 reserved */
567 #define OV7610_REG_CNT          0x05    /* Y contrast */
568 #define OV7610_REG_BRT          0x06    /* Y brightness */
569 #define OV7610_REG_COM_C        0x14    /* misc common regs */
570 #define OV7610_REG_ID_HIGH      0x1c    /* manufacturer ID MSB */
571 #define OV7610_REG_ID_LOW       0x1d    /* manufacturer ID LSB */
572 #define OV7610_REG_COM_I        0x29    /* misc settings */
573
574 /* OV7670 registers */
575 #define OV7670_R00_GAIN         0x00    /* Gain lower 8 bits (rest in vref) */
576 #define OV7670_R01_BLUE         0x01    /* blue gain */
577 #define OV7670_R02_RED          0x02    /* red gain */
578 #define OV7670_R03_VREF         0x03    /* Pieces of GAIN, VSTART, VSTOP */
579 #define OV7670_R04_COM1         0x04    /* Control 1 */
580 /*#define OV7670_R07_AECHH      0x07     * AEC MS 5 bits */
581 #define OV7670_R0C_COM3         0x0c    /* Control 3 */
582 #define OV7670_R0D_COM4         0x0d    /* Control 4 */
583 #define OV7670_R0E_COM5         0x0e    /* All "reserved" */
584 #define OV7670_R0F_COM6         0x0f    /* Control 6 */
585 #define OV7670_R10_AECH         0x10    /* More bits of AEC value */
586 #define OV7670_R11_CLKRC        0x11    /* Clock control */
587 #define OV7670_R12_COM7         0x12    /* Control 7 */
588 #define   OV7670_COM7_FMT_VGA    0x00
589 /*#define   OV7670_COM7_YUV      0x00    * YUV */
590 #define   OV7670_COM7_FMT_QVGA   0x10   /* QVGA format */
591 #define   OV7670_COM7_FMT_MASK   0x38
592 #define   OV7670_COM7_RESET      0x80   /* Register reset */
593 #define OV7670_R13_COM8         0x13    /* Control 8 */
594 #define   OV7670_COM8_AEC        0x01   /* Auto exposure enable */
595 #define   OV7670_COM8_AWB        0x02   /* White balance enable */
596 #define   OV7670_COM8_AGC        0x04   /* Auto gain enable */
597 #define   OV7670_COM8_BFILT      0x20   /* Band filter enable */
598 #define   OV7670_COM8_AECSTEP    0x40   /* Unlimited AEC step size */
599 #define   OV7670_COM8_FASTAEC    0x80   /* Enable fast AGC/AEC */
600 #define OV7670_R14_COM9         0x14    /* Control 9 - gain ceiling */
601 #define OV7670_R15_COM10        0x15    /* Control 10 */
602 #define OV7670_R17_HSTART       0x17    /* Horiz start high bits */
603 #define OV7670_R18_HSTOP        0x18    /* Horiz stop high bits */
604 #define OV7670_R19_VSTART       0x19    /* Vert start high bits */
605 #define OV7670_R1A_VSTOP        0x1a    /* Vert stop high bits */
606 #define OV7670_R1E_MVFP         0x1e    /* Mirror / vflip */
607 #define   OV7670_MVFP_VFLIP      0x10   /* vertical flip */
608 #define   OV7670_MVFP_MIRROR     0x20   /* Mirror image */
609 #define OV7670_R24_AEW          0x24    /* AGC upper limit */
610 #define OV7670_R25_AEB          0x25    /* AGC lower limit */
611 #define OV7670_R26_VPT          0x26    /* AGC/AEC fast mode op region */
612 #define OV7670_R32_HREF         0x32    /* HREF pieces */
613 #define OV7670_R3A_TSLB         0x3a    /* lots of stuff */
614 #define OV7670_R3B_COM11        0x3b    /* Control 11 */
615 #define   OV7670_COM11_EXP       0x02
616 #define   OV7670_COM11_HZAUTO    0x10   /* Auto detect 50/60 Hz */
617 #define OV7670_R3C_COM12        0x3c    /* Control 12 */
618 #define OV7670_R3D_COM13        0x3d    /* Control 13 */
619 #define   OV7670_COM13_GAMMA     0x80   /* Gamma enable */
620 #define   OV7670_COM13_UVSAT     0x40   /* UV saturation auto adjustment */
621 #define OV7670_R3E_COM14        0x3e    /* Control 14 */
622 #define OV7670_R3F_EDGE         0x3f    /* Edge enhancement factor */
623 #define OV7670_R40_COM15        0x40    /* Control 15 */
624 /*#define   OV7670_COM15_R00FF   0xc0    *      00 to FF */
625 #define OV7670_R41_COM16        0x41    /* Control 16 */
626 #define   OV7670_COM16_AWBGAIN   0x08   /* AWB gain enable */
627 #define OV7670_R55_BRIGHT       0x55    /* Brightness */
628 #define OV7670_R56_CONTRAS      0x56    /* Contrast control */
629 #define OV7670_R69_GFIX         0x69    /* Fix gain control */
630 /*#define OV7670_R8C_RGB444     0x8c     * RGB 444 control */
631 #define OV7670_R9F_HAECC1       0x9f    /* Hist AEC/AGC control 1 */
632 #define OV7670_RA0_HAECC2       0xa0    /* Hist AEC/AGC control 2 */
633 #define OV7670_RA5_BD50MAX      0xa5    /* 50hz banding step limit */
634 #define OV7670_RA6_HAECC3       0xa6    /* Hist AEC/AGC control 3 */
635 #define OV7670_RA7_HAECC4       0xa7    /* Hist AEC/AGC control 4 */
636 #define OV7670_RA8_HAECC5       0xa8    /* Hist AEC/AGC control 5 */
637 #define OV7670_RA9_HAECC6       0xa9    /* Hist AEC/AGC control 6 */
638 #define OV7670_RAA_HAECC7       0xaa    /* Hist AEC/AGC control 7 */
639 #define OV7670_RAB_BD60MAX      0xab    /* 60hz banding step limit */
640
641 struct ov_regvals {
642         u8 reg;
643         u8 val;
644 };
645 struct ov_i2c_regvals {
646         u8 reg;
647         u8 val;
648 };
649
650 /* Settings for OV2610 camera chip */
651 static const struct ov_i2c_regvals norm_2610[] = {
652         { 0x12, 0x80 }, /* reset */
653 };
654
655 static const struct ov_i2c_regvals norm_3620b[] = {
656         /*
657          * From the datasheet: "Note that after writing to register COMH
658          * (0x12) to change the sensor mode, registers related to the
659          * sensor’s cropping window will be reset back to their default
660          * values."
661          *
662          * "wait 4096 external clock ... to make sure the sensor is
663          * stable and ready to access registers" i.e. 160us at 24MHz
664          */
665         { 0x12, 0x80 }, /* COMH reset */
666         { 0x12, 0x00 }, /* QXGA, master */
667
668         /*
669          * 11 CLKRC "Clock Rate Control"
670          * [7] internal frequency doublers: on
671          * [6] video port mode: master
672          * [5:0] clock divider: 1
673          */
674         { 0x11, 0x80 },
675
676         /*
677          * 13 COMI "Common Control I"
678          *                  = 192 (0xC0) 11000000
679          *    COMI[7] "AEC speed selection"
680          *                  =   1 (0x01) 1....... "Faster AEC correction"
681          *    COMI[6] "AEC speed step selection"
682          *                  =   1 (0x01) .1...... "Big steps, fast"
683          *    COMI[5] "Banding filter on off"
684          *                  =   0 (0x00) ..0..... "Off"
685          *    COMI[4] "Banding filter option"
686          *                  =   0 (0x00) ...0.... "Main clock is 48 MHz and
687          *                                         the PLL is ON"
688          *    COMI[3] "Reserved"
689          *                  =   0 (0x00) ....0...
690          *    COMI[2] "AGC auto manual control selection"
691          *                  =   0 (0x00) .....0.. "Manual"
692          *    COMI[1] "AWB auto manual control selection"
693          *                  =   0 (0x00) ......0. "Manual"
694          *    COMI[0] "Exposure control"
695          *                  =   0 (0x00) .......0 "Manual"
696          */
697         { 0x13, 0xc0 },
698
699         /*
700          * 09 COMC "Common Control C"
701          *                  =   8 (0x08) 00001000
702          *    COMC[7:5] "Reserved"
703          *                  =   0 (0x00) 000.....
704          *    COMC[4] "Sleep Mode Enable"
705          *                  =   0 (0x00) ...0.... "Normal mode"
706          *    COMC[3:2] "Sensor sampling reset timing selection"
707          *                  =   2 (0x02) ....10.. "Longer reset time"
708          *    COMC[1:0] "Output drive current select"
709          *                  =   0 (0x00) ......00 "Weakest"
710          */
711         { 0x09, 0x08 },
712
713         /*
714          * 0C COMD "Common Control D"
715          *                  =   8 (0x08) 00001000
716          *    COMD[7] "Reserved"
717          *                  =   0 (0x00) 0.......
718          *    COMD[6] "Swap MSB and LSB at the output port"
719          *                  =   0 (0x00) .0...... "False"
720          *    COMD[5:3] "Reserved"
721          *                  =   1 (0x01) ..001...
722          *    COMD[2] "Output Average On Off"
723          *                  =   0 (0x00) .....0.. "Output Normal"
724          *    COMD[1] "Sensor precharge voltage selection"
725          *                  =   0 (0x00) ......0. "Selects internal
726          *                                         reference precharge
727          *                                         voltage"
728          *    COMD[0] "Snapshot option"
729          *                  =   0 (0x00) .......0 "Enable live video output
730          *                                         after snapshot sequence"
731          */
732         { 0x0c, 0x08 },
733
734         /*
735          * 0D COME "Common Control E"
736          *                  = 161 (0xA1) 10100001
737          *    COME[7] "Output average option"
738          *                  =   1 (0x01) 1....... "Output average of 4 pixels"
739          *    COME[6] "Anti-blooming control"
740          *                  =   0 (0x00) .0...... "Off"
741          *    COME[5:3] "Reserved"
742          *                  =   4 (0x04) ..100...
743          *    COME[2] "Clock output power down pin status"
744          *                  =   0 (0x00) .....0.. "Tri-state data output pin
745          *                                         on power down"
746          *    COME[1] "Data output pin status selection at power down"
747          *                  =   0 (0x00) ......0. "Tri-state VSYNC, PCLK,
748          *                                         HREF, and CHSYNC pins on
749          *                                         power down"
750          *    COME[0] "Auto zero circuit select"
751          *                  =   1 (0x01) .......1 "On"
752          */
753         { 0x0d, 0xa1 },
754
755         /*
756          * 0E COMF "Common Control F"
757          *                  = 112 (0x70) 01110000
758          *    COMF[7] "System clock selection"
759          *                  =   0 (0x00) 0....... "Use 24 MHz system clock"
760          *    COMF[6:4] "Reserved"
761          *                  =   7 (0x07) .111....
762          *    COMF[3] "Manual auto negative offset canceling selection"
763          *                  =   0 (0x00) ....0... "Auto detect negative
764          *                                         offset and cancel it"
765          *    COMF[2:0] "Reserved"
766          *                  =   0 (0x00) .....000
767          */
768         { 0x0e, 0x70 },
769
770         /*
771          * 0F COMG "Common Control G"
772          *                  =  66 (0x42) 01000010
773          *    COMG[7] "Optical black output selection"
774          *                  =   0 (0x00) 0....... "Disable"
775          *    COMG[6] "Black level calibrate selection"
776          *                  =   1 (0x01) .1...... "Use optical black pixels
777          *                                         to calibrate"
778          *    COMG[5:4] "Reserved"
779          *                  =   0 (0x00) ..00....
780          *    COMG[3] "Channel offset adjustment"
781          *                  =   0 (0x00) ....0... "Disable offset adjustment"
782          *    COMG[2] "ADC black level calibration option"
783          *                  =   0 (0x00) .....0.. "Use B/G line and G/R
784          *                                         line to calibrate each
785          *                                         channel's black level"
786          *    COMG[1] "Reserved"
787          *                  =   1 (0x01) ......1.
788          *    COMG[0] "ADC black level calibration enable"
789          *                  =   0 (0x00) .......0 "Disable"
790          */
791         { 0x0f, 0x42 },
792
793         /*
794          * 14 COMJ "Common Control J"
795          *                  = 198 (0xC6) 11000110
796          *    COMJ[7:6] "AGC gain ceiling"
797          *                  =   3 (0x03) 11...... "8x"
798          *    COMJ[5:4] "Reserved"
799          *                  =   0 (0x00) ..00....
800          *    COMJ[3] "Auto banding filter"
801          *                  =   0 (0x00) ....0... "Banding filter is always
802          *                                         on off depending on
803          *                                         COMI[5] setting"
804          *    COMJ[2] "VSYNC drop option"
805          *                  =   1 (0x01) .....1.. "SYNC is dropped if frame
806          *                                         data is dropped"
807          *    COMJ[1] "Frame data drop"
808          *                  =   1 (0x01) ......1. "Drop frame data if
809          *                                         exposure is not within
810          *                                         tolerance.  In AEC mode,
811          *                                         data is normally dropped
812          *                                         when data is out of
813          *                                         range."
814          *    COMJ[0] "Reserved"
815          *                  =   0 (0x00) .......0
816          */
817         { 0x14, 0xc6 },
818
819         /*
820          * 15 COMK "Common Control K"
821          *                  =   2 (0x02) 00000010
822          *    COMK[7] "CHSYNC pin output swap"
823          *                  =   0 (0x00) 0....... "CHSYNC"
824          *    COMK[6] "HREF pin output swap"
825          *                  =   0 (0x00) .0...... "HREF"
826          *    COMK[5] "PCLK output selection"
827          *                  =   0 (0x00) ..0..... "PCLK always output"
828          *    COMK[4] "PCLK edge selection"
829          *                  =   0 (0x00) ...0.... "Data valid on falling edge"
830          *    COMK[3] "HREF output polarity"
831          *                  =   0 (0x00) ....0... "positive"
832          *    COMK[2] "Reserved"
833          *                  =   0 (0x00) .....0..
834          *    COMK[1] "VSYNC polarity"
835          *                  =   1 (0x01) ......1. "negative"
836          *    COMK[0] "HSYNC polarity"
837          *                  =   0 (0x00) .......0 "positive"
838          */
839         { 0x15, 0x02 },
840
841         /*
842          * 33 CHLF "Current Control"
843          *                  =   9 (0x09) 00001001
844          *    CHLF[7:6] "Sensor current control"
845          *                  =   0 (0x00) 00......
846          *    CHLF[5] "Sensor current range control"
847          *                  =   0 (0x00) ..0..... "normal range"
848          *    CHLF[4] "Sensor current"
849          *                  =   0 (0x00) ...0.... "normal current"
850          *    CHLF[3] "Sensor buffer current control"
851          *                  =   1 (0x01) ....1... "half current"
852          *    CHLF[2] "Column buffer current control"
853          *                  =   0 (0x00) .....0.. "normal current"
854          *    CHLF[1] "Analog DSP current control"
855          *                  =   0 (0x00) ......0. "normal current"
856          *    CHLF[1] "ADC current control"
857          *                  =   0 (0x00) ......0. "normal current"
858          */
859         { 0x33, 0x09 },
860
861         /*
862          * 34 VBLM "Blooming Control"
863          *                  =  80 (0x50) 01010000
864          *    VBLM[7] "Hard soft reset switch"
865          *                  =   0 (0x00) 0....... "Hard reset"
866          *    VBLM[6:4] "Blooming voltage selection"
867          *                  =   5 (0x05) .101....
868          *    VBLM[3:0] "Sensor current control"
869          *                  =   0 (0x00) ....0000
870          */
871         { 0x34, 0x50 },
872
873         /*
874          * 36 VCHG "Sensor Precharge Voltage Control"
875          *                  =   0 (0x00) 00000000
876          *    VCHG[7] "Reserved"
877          *                  =   0 (0x00) 0.......
878          *    VCHG[6:4] "Sensor precharge voltage control"
879          *                  =   0 (0x00) .000....
880          *    VCHG[3:0] "Sensor array common reference"
881          *                  =   0 (0x00) ....0000
882          */
883         { 0x36, 0x00 },
884
885         /*
886          * 37 ADC "ADC Reference Control"
887          *                  =   4 (0x04) 00000100
888          *    ADC[7:4] "Reserved"
889          *                  =   0 (0x00) 0000....
890          *    ADC[3] "ADC input signal range"
891          *                  =   0 (0x00) ....0... "Input signal 1.0x"
892          *    ADC[2:0] "ADC range control"
893          *                  =   4 (0x04) .....100
894          */
895         { 0x37, 0x04 },
896
897         /*
898          * 38 ACOM "Analog Common Ground"
899          *                  =  82 (0x52) 01010010
900          *    ACOM[7] "Analog gain control"
901          *                  =   0 (0x00) 0....... "Gain 1x"
902          *    ACOM[6] "Analog black level calibration"
903          *                  =   1 (0x01) .1...... "On"
904          *    ACOM[5:0] "Reserved"
905          *                  =  18 (0x12) ..010010
906          */
907         { 0x38, 0x52 },
908
909         /*
910          * 3A FREFA "Internal Reference Adjustment"
911          *                  =   0 (0x00) 00000000
912          *    FREFA[7:0] "Range"
913          *                  =   0 (0x00) 00000000
914          */
915         { 0x3a, 0x00 },
916
917         /*
918          * 3C FVOPT "Internal Reference Adjustment"
919          *                  =  31 (0x1F) 00011111
920          *    FVOPT[7:0] "Range"
921          *                  =  31 (0x1F) 00011111
922          */
923         { 0x3c, 0x1f },
924
925         /*
926          * 44 Undocumented  =   0 (0x00) 00000000
927          *    44[7:0] "It's a secret"
928          *                  =   0 (0x00) 00000000
929          */
930         { 0x44, 0x00 },
931
932         /*
933          * 40 Undocumented  =   0 (0x00) 00000000
934          *    40[7:0] "It's a secret"
935          *                  =   0 (0x00) 00000000
936          */
937         { 0x40, 0x00 },
938
939         /*
940          * 41 Undocumented  =   0 (0x00) 00000000
941          *    41[7:0] "It's a secret"
942          *                  =   0 (0x00) 00000000
943          */
944         { 0x41, 0x00 },
945
946         /*
947          * 42 Undocumented  =   0 (0x00) 00000000
948          *    42[7:0] "It's a secret"
949          *                  =   0 (0x00) 00000000
950          */
951         { 0x42, 0x00 },
952
953         /*
954          * 43 Undocumented  =   0 (0x00) 00000000
955          *    43[7:0] "It's a secret"
956          *                  =   0 (0x00) 00000000
957          */
958         { 0x43, 0x00 },
959
960         /*
961          * 45 Undocumented  = 128 (0x80) 10000000
962          *    45[7:0] "It's a secret"
963          *                  = 128 (0x80) 10000000
964          */
965         { 0x45, 0x80 },
966
967         /*
968          * 48 Undocumented  = 192 (0xC0) 11000000
969          *    48[7:0] "It's a secret"
970          *                  = 192 (0xC0) 11000000
971          */
972         { 0x48, 0xc0 },
973
974         /*
975          * 49 Undocumented  =  25 (0x19) 00011001
976          *    49[7:0] "It's a secret"
977          *                  =  25 (0x19) 00011001
978          */
979         { 0x49, 0x19 },
980
981         /*
982          * 4B Undocumented  = 128 (0x80) 10000000
983          *    4B[7:0] "It's a secret"
984          *                  = 128 (0x80) 10000000
985          */
986         { 0x4b, 0x80 },
987
988         /*
989          * 4D Undocumented  = 196 (0xC4) 11000100
990          *    4D[7:0] "It's a secret"
991          *                  = 196 (0xC4) 11000100
992          */
993         { 0x4d, 0xc4 },
994
995         /*
996          * 35 VREF "Reference Voltage Control"
997          *                  =  76 (0x4c) 01001100
998          *    VREF[7:5] "Column high reference control"
999          *                  =   2 (0x02) 010..... "higher voltage"
1000          *    VREF[4:2] "Column low reference control"
1001          *                  =   3 (0x03) ...011.. "Highest voltage"
1002          *    VREF[1:0] "Reserved"
1003          *                  =   0 (0x00) ......00
1004          */
1005         { 0x35, 0x4c },
1006
1007         /*
1008          * 3D Undocumented  =   0 (0x00) 00000000
1009          *    3D[7:0] "It's a secret"
1010          *                  =   0 (0x00) 00000000
1011          */
1012         { 0x3d, 0x00 },
1013
1014         /*
1015          * 3E Undocumented  =   0 (0x00) 00000000
1016          *    3E[7:0] "It's a secret"
1017          *                  =   0 (0x00) 00000000
1018          */
1019         { 0x3e, 0x00 },
1020
1021         /*
1022          * 3B FREFB "Internal Reference Adjustment"
1023          *                  =  24 (0x18) 00011000
1024          *    FREFB[7:0] "Range"
1025          *                  =  24 (0x18) 00011000
1026          */
1027         { 0x3b, 0x18 },
1028
1029         /*
1030          * 33 CHLF "Current Control"
1031          *                  =  25 (0x19) 00011001
1032          *    CHLF[7:6] "Sensor current control"
1033          *                  =   0 (0x00) 00......
1034          *    CHLF[5] "Sensor current range control"
1035          *                  =   0 (0x00) ..0..... "normal range"
1036          *    CHLF[4] "Sensor current"
1037          *                  =   1 (0x01) ...1.... "double current"
1038          *    CHLF[3] "Sensor buffer current control"
1039          *                  =   1 (0x01) ....1... "half current"
1040          *    CHLF[2] "Column buffer current control"
1041          *                  =   0 (0x00) .....0.. "normal current"
1042          *    CHLF[1] "Analog DSP current control"
1043          *                  =   0 (0x00) ......0. "normal current"
1044          *    CHLF[1] "ADC current control"
1045          *                  =   0 (0x00) ......0. "normal current"
1046          */
1047         { 0x33, 0x19 },
1048
1049         /*
1050          * 34 VBLM "Blooming Control"
1051          *                  =  90 (0x5A) 01011010
1052          *    VBLM[7] "Hard soft reset switch"
1053          *                  =   0 (0x00) 0....... "Hard reset"
1054          *    VBLM[6:4] "Blooming voltage selection"
1055          *                  =   5 (0x05) .101....
1056          *    VBLM[3:0] "Sensor current control"
1057          *                  =  10 (0x0A) ....1010
1058          */
1059         { 0x34, 0x5a },
1060
1061         /*
1062          * 3B FREFB "Internal Reference Adjustment"
1063          *                  =   0 (0x00) 00000000
1064          *    FREFB[7:0] "Range"
1065          *                  =   0 (0x00) 00000000
1066          */
1067         { 0x3b, 0x00 },
1068
1069         /*
1070          * 33 CHLF "Current Control"
1071          *                  =   9 (0x09) 00001001
1072          *    CHLF[7:6] "Sensor current control"
1073          *                  =   0 (0x00) 00......
1074          *    CHLF[5] "Sensor current range control"
1075          *                  =   0 (0x00) ..0..... "normal range"
1076          *    CHLF[4] "Sensor current"
1077          *                  =   0 (0x00) ...0.... "normal current"
1078          *    CHLF[3] "Sensor buffer current control"
1079          *                  =   1 (0x01) ....1... "half current"
1080          *    CHLF[2] "Column buffer current control"
1081          *                  =   0 (0x00) .....0.. "normal current"
1082          *    CHLF[1] "Analog DSP current control"
1083          *                  =   0 (0x00) ......0. "normal current"
1084          *    CHLF[1] "ADC current control"
1085          *                  =   0 (0x00) ......0. "normal current"
1086          */
1087         { 0x33, 0x09 },
1088
1089         /*
1090          * 34 VBLM "Blooming Control"
1091          *                  =  80 (0x50) 01010000
1092          *    VBLM[7] "Hard soft reset switch"
1093          *                  =   0 (0x00) 0....... "Hard reset"
1094          *    VBLM[6:4] "Blooming voltage selection"
1095          *                  =   5 (0x05) .101....
1096          *    VBLM[3:0] "Sensor current control"
1097          *                  =   0 (0x00) ....0000
1098          */
1099         { 0x34, 0x50 },
1100
1101         /*
1102          * 12 COMH "Common Control H"
1103          *                  =  64 (0x40) 01000000
1104          *    COMH[7] "SRST"
1105          *                  =   0 (0x00) 0....... "No-op"
1106          *    COMH[6:4] "Resolution selection"
1107          *                  =   4 (0x04) .100.... "XGA"
1108          *    COMH[3] "Master slave selection"
1109          *                  =   0 (0x00) ....0... "Master mode"
1110          *    COMH[2] "Internal B/R channel option"
1111          *                  =   0 (0x00) .....0.. "B/R use same channel"
1112          *    COMH[1] "Color bar test pattern"
1113          *                  =   0 (0x00) ......0. "Off"
1114          *    COMH[0] "Reserved"
1115          *                  =   0 (0x00) .......0
1116          */
1117         { 0x12, 0x40 },
1118
1119         /*
1120          * 17 HREFST "Horizontal window start"
1121          *                  =  31 (0x1F) 00011111
1122          *    HREFST[7:0] "Horizontal window start, 8 MSBs"
1123          *                  =  31 (0x1F) 00011111
1124          */
1125         { 0x17, 0x1f },
1126
1127         /*
1128          * 18 HREFEND "Horizontal window end"
1129          *                  =  95 (0x5F) 01011111
1130          *    HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1131          *                  =  95 (0x5F) 01011111
1132          */
1133         { 0x18, 0x5f },
1134
1135         /*
1136          * 19 VSTRT "Vertical window start"
1137          *                  =   0 (0x00) 00000000
1138          *    VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1139          *                  =   0 (0x00) 00000000
1140          */
1141         { 0x19, 0x00 },
1142
1143         /*
1144          * 1A VEND "Vertical window end"
1145          *                  =  96 (0x60) 01100000
1146          *    VEND[7:0] "Vertical Window End, 8 MSBs"
1147          *                  =  96 (0x60) 01100000
1148          */
1149         { 0x1a, 0x60 },
1150
1151         /*
1152          * 32 COMM "Common Control M"
1153          *                  =  18 (0x12) 00010010
1154          *    COMM[7:6] "Pixel clock divide option"
1155          *                  =   0 (0x00) 00...... "/1"
1156          *    COMM[5:3] "Horizontal window end position, 3 LSBs"
1157          *                  =   2 (0x02) ..010...
1158          *    COMM[2:0] "Horizontal window start position, 3 LSBs"
1159          *                  =   2 (0x02) .....010
1160          */
1161         { 0x32, 0x12 },
1162
1163         /*
1164          * 03 COMA "Common Control A"
1165          *                  =  74 (0x4A) 01001010
1166          *    COMA[7:4] "AWB Update Threshold"
1167          *                  =   4 (0x04) 0100....
1168          *    COMA[3:2] "Vertical window end line control 2 LSBs"
1169          *                  =   2 (0x02) ....10..
1170          *    COMA[1:0] "Vertical window start line control 2 LSBs"
1171          *                  =   2 (0x02) ......10
1172          */
1173         { 0x03, 0x4a },
1174
1175         /*
1176          * 11 CLKRC "Clock Rate Control"
1177          *                  = 128 (0x80) 10000000
1178          *    CLKRC[7] "Internal frequency doublers on off seclection"
1179          *                  =   1 (0x01) 1....... "On"
1180          *    CLKRC[6] "Digital video master slave selection"
1181          *                  =   0 (0x00) .0...... "Master mode, sensor
1182          *                                         provides PCLK"
1183          *    CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1184          *                  =   0 (0x00) ..000000
1185          */
1186         { 0x11, 0x80 },
1187
1188         /*
1189          * 12 COMH "Common Control H"
1190          *                  =   0 (0x00) 00000000
1191          *    COMH[7] "SRST"
1192          *                  =   0 (0x00) 0....... "No-op"
1193          *    COMH[6:4] "Resolution selection"
1194          *                  =   0 (0x00) .000.... "QXGA"
1195          *    COMH[3] "Master slave selection"
1196          *                  =   0 (0x00) ....0... "Master mode"
1197          *    COMH[2] "Internal B/R channel option"
1198          *                  =   0 (0x00) .....0.. "B/R use same channel"
1199          *    COMH[1] "Color bar test pattern"
1200          *                  =   0 (0x00) ......0. "Off"
1201          *    COMH[0] "Reserved"
1202          *                  =   0 (0x00) .......0
1203          */
1204         { 0x12, 0x00 },
1205
1206         /*
1207          * 12 COMH "Common Control H"
1208          *                  =  64 (0x40) 01000000
1209          *    COMH[7] "SRST"
1210          *                  =   0 (0x00) 0....... "No-op"
1211          *    COMH[6:4] "Resolution selection"
1212          *                  =   4 (0x04) .100.... "XGA"
1213          *    COMH[3] "Master slave selection"
1214          *                  =   0 (0x00) ....0... "Master mode"
1215          *    COMH[2] "Internal B/R channel option"
1216          *                  =   0 (0x00) .....0.. "B/R use same channel"
1217          *    COMH[1] "Color bar test pattern"
1218          *                  =   0 (0x00) ......0. "Off"
1219          *    COMH[0] "Reserved"
1220          *                  =   0 (0x00) .......0
1221          */
1222         { 0x12, 0x40 },
1223
1224         /*
1225          * 17 HREFST "Horizontal window start"
1226          *                  =  31 (0x1F) 00011111
1227          *    HREFST[7:0] "Horizontal window start, 8 MSBs"
1228          *                  =  31 (0x1F) 00011111
1229          */
1230         { 0x17, 0x1f },
1231
1232         /*
1233          * 18 HREFEND "Horizontal window end"
1234          *                  =  95 (0x5F) 01011111
1235          *    HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1236          *                  =  95 (0x5F) 01011111
1237          */
1238         { 0x18, 0x5f },
1239
1240         /*
1241          * 19 VSTRT "Vertical window start"
1242          *                  =   0 (0x00) 00000000
1243          *    VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1244          *                  =   0 (0x00) 00000000
1245          */
1246         { 0x19, 0x00 },
1247
1248         /*
1249          * 1A VEND "Vertical window end"
1250          *                  =  96 (0x60) 01100000
1251          *    VEND[7:0] "Vertical Window End, 8 MSBs"
1252          *                  =  96 (0x60) 01100000
1253          */
1254         { 0x1a, 0x60 },
1255
1256         /*
1257          * 32 COMM "Common Control M"
1258          *                  =  18 (0x12) 00010010
1259          *    COMM[7:6] "Pixel clock divide option"
1260          *                  =   0 (0x00) 00...... "/1"
1261          *    COMM[5:3] "Horizontal window end position, 3 LSBs"
1262          *                  =   2 (0x02) ..010...
1263          *    COMM[2:0] "Horizontal window start position, 3 LSBs"
1264          *                  =   2 (0x02) .....010
1265          */
1266         { 0x32, 0x12 },
1267
1268         /*
1269          * 03 COMA "Common Control A"
1270          *                  =  74 (0x4A) 01001010
1271          *    COMA[7:4] "AWB Update Threshold"
1272          *                  =   4 (0x04) 0100....
1273          *    COMA[3:2] "Vertical window end line control 2 LSBs"
1274          *                  =   2 (0x02) ....10..
1275          *    COMA[1:0] "Vertical window start line control 2 LSBs"
1276          *                  =   2 (0x02) ......10
1277          */
1278         { 0x03, 0x4a },
1279
1280         /*
1281          * 02 RED "Red Gain Control"
1282          *                  = 175 (0xAF) 10101111
1283          *    RED[7] "Action"
1284          *                  =   1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1285          *    RED[6:0] "Value"
1286          *                  =  47 (0x2F) .0101111
1287          */
1288         { 0x02, 0xaf },
1289
1290         /*
1291          * 2D ADDVSL "VSYNC Pulse Width"
1292          *                  = 210 (0xD2) 11010010
1293          *    ADDVSL[7:0] "VSYNC pulse width, LSB"
1294          *                  = 210 (0xD2) 11010010
1295          */
1296         { 0x2d, 0xd2 },
1297
1298         /*
1299          * 00 GAIN          =  24 (0x18) 00011000
1300          *    GAIN[7:6] "Reserved"
1301          *                  =   0 (0x00) 00......
1302          *    GAIN[5] "Double"
1303          *                  =   0 (0x00) ..0..... "False"
1304          *    GAIN[4] "Double"
1305          *                  =   1 (0x01) ...1.... "True"
1306          *    GAIN[3:0] "Range"
1307          *                  =   8 (0x08) ....1000
1308          */
1309         { 0x00, 0x18 },
1310
1311         /*
1312          * 01 BLUE "Blue Gain Control"
1313          *                  = 240 (0xF0) 11110000
1314          *    BLUE[7] "Action"
1315          *                  =   1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1316          *    BLUE[6:0] "Value"
1317          *                  = 112 (0x70) .1110000
1318          */
1319         { 0x01, 0xf0 },
1320
1321         /*
1322          * 10 AEC "Automatic Exposure Control"
1323          *                  =  10 (0x0A) 00001010
1324          *    AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1325          *                  =  10 (0x0A) 00001010
1326          */
1327         { 0x10, 0x0a },
1328
1329         { 0xe1, 0x67 },
1330         { 0xe3, 0x03 },
1331         { 0xe4, 0x26 },
1332         { 0xe5, 0x3e },
1333         { 0xf8, 0x01 },
1334         { 0xff, 0x01 },
1335 };
1336
1337 static const struct ov_i2c_regvals norm_6x20[] = {
1338         { 0x12, 0x80 }, /* reset */
1339         { 0x11, 0x01 },
1340         { 0x03, 0x60 },
1341         { 0x05, 0x7f }, /* For when autoadjust is off */
1342         { 0x07, 0xa8 },
1343         /* The ratio of 0x0c and 0x0d controls the white point */
1344         { 0x0c, 0x24 },
1345         { 0x0d, 0x24 },
1346         { 0x0f, 0x15 }, /* COMS */
1347         { 0x10, 0x75 }, /* AEC Exposure time */
1348         { 0x12, 0x24 }, /* Enable AGC */
1349         { 0x14, 0x04 },
1350         /* 0x16: 0x06 helps frame stability with moving objects */
1351         { 0x16, 0x06 },
1352 /*      { 0x20, 0x30 },  * Aperture correction enable */
1353         { 0x26, 0xb2 }, /* BLC enable */
1354         /* 0x28: 0x05 Selects RGB format if RGB on */
1355         { 0x28, 0x05 },
1356         { 0x2a, 0x04 }, /* Disable framerate adjust */
1357 /*      { 0x2b, 0xac },  * Framerate; Set 2a[7] first */
1358         { 0x2d, 0x85 },
1359         { 0x33, 0xa0 }, /* Color Processing Parameter */
1360         { 0x34, 0xd2 }, /* Max A/D range */
1361         { 0x38, 0x8b },
1362         { 0x39, 0x40 },
1363
1364         { 0x3c, 0x39 }, /* Enable AEC mode changing */
1365         { 0x3c, 0x3c }, /* Change AEC mode */
1366         { 0x3c, 0x24 }, /* Disable AEC mode changing */
1367
1368         { 0x3d, 0x80 },
1369         /* These next two registers (0x4a, 0x4b) are undocumented.
1370          * They control the color balance */
1371         { 0x4a, 0x80 },
1372         { 0x4b, 0x80 },
1373         { 0x4d, 0xd2 }, /* This reduces noise a bit */
1374         { 0x4e, 0xc1 },
1375         { 0x4f, 0x04 },
1376 /* Do 50-53 have any effect? */
1377 /* Toggle 0x12[2] off and on here? */
1378 };
1379
1380 static const struct ov_i2c_regvals norm_6x30[] = {
1381         { 0x12, 0x80 }, /* Reset */
1382         { 0x00, 0x1f }, /* Gain */
1383         { 0x01, 0x99 }, /* Blue gain */
1384         { 0x02, 0x7c }, /* Red gain */
1385         { 0x03, 0xc0 }, /* Saturation */
1386         { 0x05, 0x0a }, /* Contrast */
1387         { 0x06, 0x95 }, /* Brightness */
1388         { 0x07, 0x2d }, /* Sharpness */
1389         { 0x0c, 0x20 },
1390         { 0x0d, 0x20 },
1391         { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
1392         { 0x0f, 0x05 },
1393         { 0x10, 0x9a },
1394         { 0x11, 0x00 }, /* Pixel clock = fastest */
1395         { 0x12, 0x24 }, /* Enable AGC and AWB */
1396         { 0x13, 0x21 },
1397         { 0x14, 0x80 },
1398         { 0x15, 0x01 },
1399         { 0x16, 0x03 },
1400         { 0x17, 0x38 },
1401         { 0x18, 0xea },
1402         { 0x19, 0x04 },
1403         { 0x1a, 0x93 },
1404         { 0x1b, 0x00 },
1405         { 0x1e, 0xc4 },
1406         { 0x1f, 0x04 },
1407         { 0x20, 0x20 },
1408         { 0x21, 0x10 },
1409         { 0x22, 0x88 },
1410         { 0x23, 0xc0 }, /* Crystal circuit power level */
1411         { 0x25, 0x9a }, /* Increase AEC black ratio */
1412         { 0x26, 0xb2 }, /* BLC enable */
1413         { 0x27, 0xa2 },
1414         { 0x28, 0x00 },
1415         { 0x29, 0x00 },
1416         { 0x2a, 0x84 }, /* 60 Hz power */
1417         { 0x2b, 0xa8 }, /* 60 Hz power */
1418         { 0x2c, 0xa0 },
1419         { 0x2d, 0x95 }, /* Enable auto-brightness */
1420         { 0x2e, 0x88 },
1421         { 0x33, 0x26 },
1422         { 0x34, 0x03 },
1423         { 0x36, 0x8f },
1424         { 0x37, 0x80 },
1425         { 0x38, 0x83 },
1426         { 0x39, 0x80 },
1427         { 0x3a, 0x0f },
1428         { 0x3b, 0x3c },
1429         { 0x3c, 0x1a },
1430         { 0x3d, 0x80 },
1431         { 0x3e, 0x80 },
1432         { 0x3f, 0x0e },
1433         { 0x40, 0x00 }, /* White bal */
1434         { 0x41, 0x00 }, /* White bal */
1435         { 0x42, 0x80 },
1436         { 0x43, 0x3f }, /* White bal */
1437         { 0x44, 0x80 },
1438         { 0x45, 0x20 },
1439         { 0x46, 0x20 },
1440         { 0x47, 0x80 },
1441         { 0x48, 0x7f },
1442         { 0x49, 0x00 },
1443         { 0x4a, 0x00 },
1444         { 0x4b, 0x80 },
1445         { 0x4c, 0xd0 },
1446         { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1447         { 0x4e, 0x40 },
1448         { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1449         { 0x50, 0xff },
1450         { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1451         { 0x55, 0xff },
1452         { 0x56, 0x12 },
1453         { 0x57, 0x81 },
1454         { 0x58, 0x75 },
1455         { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1456         { 0x5a, 0x2c },
1457         { 0x5b, 0x0f }, /* AWB chrominance levels */
1458         { 0x5c, 0x10 },
1459         { 0x3d, 0x80 },
1460         { 0x27, 0xa6 },
1461         { 0x12, 0x20 }, /* Toggle AWB */
1462         { 0x12, 0x24 },
1463 };
1464
1465 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
1466  *
1467  * Register 0x0f in the 7610 has the following effects:
1468  *
1469  * 0x85 (AEC method 1): Best overall, good contrast range
1470  * 0x45 (AEC method 2): Very overexposed
1471  * 0xa5 (spec sheet default): Ok, but the black level is
1472  *      shifted resulting in loss of contrast
1473  * 0x05 (old driver setting): very overexposed, too much
1474  *      contrast
1475  */
1476 static const struct ov_i2c_regvals norm_7610[] = {
1477         { 0x10, 0xff },
1478         { 0x16, 0x06 },
1479         { 0x28, 0x24 },
1480         { 0x2b, 0xac },
1481         { 0x12, 0x00 },
1482         { 0x38, 0x81 },
1483         { 0x28, 0x24 }, /* 0c */
1484         { 0x0f, 0x85 }, /* lg's setting */
1485         { 0x15, 0x01 },
1486         { 0x20, 0x1c },
1487         { 0x23, 0x2a },
1488         { 0x24, 0x10 },
1489         { 0x25, 0x8a },
1490         { 0x26, 0xa2 },
1491         { 0x27, 0xc2 },
1492         { 0x2a, 0x04 },
1493         { 0x2c, 0xfe },
1494         { 0x2d, 0x93 },
1495         { 0x30, 0x71 },
1496         { 0x31, 0x60 },
1497         { 0x32, 0x26 },
1498         { 0x33, 0x20 },
1499         { 0x34, 0x48 },
1500         { 0x12, 0x24 },
1501         { 0x11, 0x01 },
1502         { 0x0c, 0x24 },
1503         { 0x0d, 0x24 },
1504 };
1505
1506 static const struct ov_i2c_regvals norm_7620[] = {
1507         { 0x12, 0x80 },         /* reset */
1508         { 0x00, 0x00 },         /* gain */
1509         { 0x01, 0x80 },         /* blue gain */
1510         { 0x02, 0x80 },         /* red gain */
1511         { 0x03, 0xc0 },         /* OV7670_R03_VREF */
1512         { 0x06, 0x60 },
1513         { 0x07, 0x00 },
1514         { 0x0c, 0x24 },
1515         { 0x0c, 0x24 },
1516         { 0x0d, 0x24 },
1517         { 0x11, 0x01 },
1518         { 0x12, 0x24 },
1519         { 0x13, 0x01 },
1520         { 0x14, 0x84 },
1521         { 0x15, 0x01 },
1522         { 0x16, 0x03 },
1523         { 0x17, 0x2f },
1524         { 0x18, 0xcf },
1525         { 0x19, 0x06 },
1526         { 0x1a, 0xf5 },
1527         { 0x1b, 0x00 },
1528         { 0x20, 0x18 },
1529         { 0x21, 0x80 },
1530         { 0x22, 0x80 },
1531         { 0x23, 0x00 },
1532         { 0x26, 0xa2 },
1533         { 0x27, 0xea },
1534         { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
1535         { 0x29, 0x00 },
1536         { 0x2a, 0x10 },
1537         { 0x2b, 0x00 },
1538         { 0x2c, 0x88 },
1539         { 0x2d, 0x91 },
1540         { 0x2e, 0x80 },
1541         { 0x2f, 0x44 },
1542         { 0x60, 0x27 },
1543         { 0x61, 0x02 },
1544         { 0x62, 0x5f },
1545         { 0x63, 0xd5 },
1546         { 0x64, 0x57 },
1547         { 0x65, 0x83 },
1548         { 0x66, 0x55 },
1549         { 0x67, 0x92 },
1550         { 0x68, 0xcf },
1551         { 0x69, 0x76 },
1552         { 0x6a, 0x22 },
1553         { 0x6b, 0x00 },
1554         { 0x6c, 0x02 },
1555         { 0x6d, 0x44 },
1556         { 0x6e, 0x80 },
1557         { 0x6f, 0x1d },
1558         { 0x70, 0x8b },
1559         { 0x71, 0x00 },
1560         { 0x72, 0x14 },
1561         { 0x73, 0x54 },
1562         { 0x74, 0x00 },
1563         { 0x75, 0x8e },
1564         { 0x76, 0x00 },
1565         { 0x77, 0xff },
1566         { 0x78, 0x80 },
1567         { 0x79, 0x80 },
1568         { 0x7a, 0x80 },
1569         { 0x7b, 0xe2 },
1570         { 0x7c, 0x00 },
1571 };
1572
1573 /* 7640 and 7648. The defaults should be OK for most registers. */
1574 static const struct ov_i2c_regvals norm_7640[] = {
1575         { 0x12, 0x80 },
1576         { 0x12, 0x14 },
1577 };
1578
1579 /* 7670. Defaults taken from OmniVision provided data,
1580 *  as provided by Jonathan Corbet of OLPC               */
1581 static const struct ov_i2c_regvals norm_7670[] = {
1582         { OV7670_R12_COM7, OV7670_COM7_RESET },
1583         { OV7670_R3A_TSLB, 0x04 },              /* OV */
1584         { OV7670_R12_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1585         { OV7670_R11_CLKRC, 0x01 },
1586 /*
1587  * Set the hardware window.  These values from OV don't entirely
1588  * make sense - hstop is less than hstart.  But they work...
1589  */
1590         { OV7670_R17_HSTART, 0x13 },
1591         { OV7670_R18_HSTOP, 0x01 },
1592         { OV7670_R32_HREF, 0xb6 },
1593         { OV7670_R19_VSTART, 0x02 },
1594         { OV7670_R1A_VSTOP, 0x7a },
1595         { OV7670_R03_VREF, 0x0a },
1596
1597         { OV7670_R0C_COM3, 0x00 },
1598         { OV7670_R3E_COM14, 0x00 },
1599 /* Mystery scaling numbers */
1600         { 0x70, 0x3a },
1601         { 0x71, 0x35 },
1602         { 0x72, 0x11 },
1603         { 0x73, 0xf0 },
1604         { 0xa2, 0x02 },
1605 /*      { OV7670_R15_COM10, 0x0 }, */
1606
1607 /* Gamma curve values */
1608         { 0x7a, 0x20 },
1609         { 0x7b, 0x10 },
1610         { 0x7c, 0x1e },
1611         { 0x7d, 0x35 },
1612         { 0x7e, 0x5a },
1613         { 0x7f, 0x69 },
1614         { 0x80, 0x76 },
1615         { 0x81, 0x80 },
1616         { 0x82, 0x88 },
1617         { 0x83, 0x8f },
1618         { 0x84, 0x96 },
1619         { 0x85, 0xa3 },
1620         { 0x86, 0xaf },
1621         { 0x87, 0xc4 },
1622         { 0x88, 0xd7 },
1623         { 0x89, 0xe8 },
1624
1625 /* AGC and AEC parameters.  Note we start by disabling those features,
1626    then turn them only after tweaking the values. */
1627         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1628                          | OV7670_COM8_AECSTEP
1629                          | OV7670_COM8_BFILT },
1630         { OV7670_R00_GAIN, 0x00 },
1631         { OV7670_R10_AECH, 0x00 },
1632         { OV7670_R0D_COM4, 0x40 }, /* magic reserved bit */
1633         { OV7670_R14_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1634         { OV7670_RA5_BD50MAX, 0x05 },
1635         { OV7670_RAB_BD60MAX, 0x07 },
1636         { OV7670_R24_AEW, 0x95 },
1637         { OV7670_R25_AEB, 0x33 },
1638         { OV7670_R26_VPT, 0xe3 },
1639         { OV7670_R9F_HAECC1, 0x78 },
1640         { OV7670_RA0_HAECC2, 0x68 },
1641         { 0xa1, 0x03 }, /* magic */
1642         { OV7670_RA6_HAECC3, 0xd8 },
1643         { OV7670_RA7_HAECC4, 0xd8 },
1644         { OV7670_RA8_HAECC5, 0xf0 },
1645         { OV7670_RA9_HAECC6, 0x90 },
1646         { OV7670_RAA_HAECC7, 0x94 },
1647         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1648                         | OV7670_COM8_AECSTEP
1649                         | OV7670_COM8_BFILT
1650                         | OV7670_COM8_AGC
1651                         | OV7670_COM8_AEC },
1652
1653 /* Almost all of these are magic "reserved" values.  */
1654         { OV7670_R0E_COM5, 0x61 },
1655         { OV7670_R0F_COM6, 0x4b },
1656         { 0x16, 0x02 },
1657         { OV7670_R1E_MVFP, 0x07 },
1658         { 0x21, 0x02 },
1659         { 0x22, 0x91 },
1660         { 0x29, 0x07 },
1661         { 0x33, 0x0b },
1662         { 0x35, 0x0b },
1663         { 0x37, 0x1d },
1664         { 0x38, 0x71 },
1665         { 0x39, 0x2a },
1666         { OV7670_R3C_COM12, 0x78 },
1667         { 0x4d, 0x40 },
1668         { 0x4e, 0x20 },
1669         { OV7670_R69_GFIX, 0x00 },
1670         { 0x6b, 0x4a },
1671         { 0x74, 0x10 },
1672         { 0x8d, 0x4f },
1673         { 0x8e, 0x00 },
1674         { 0x8f, 0x00 },
1675         { 0x90, 0x00 },
1676         { 0x91, 0x00 },
1677         { 0x96, 0x00 },
1678         { 0x9a, 0x00 },
1679         { 0xb0, 0x84 },
1680         { 0xb1, 0x0c },
1681         { 0xb2, 0x0e },
1682         { 0xb3, 0x82 },
1683         { 0xb8, 0x0a },
1684
1685 /* More reserved magic, some of which tweaks white balance */
1686         { 0x43, 0x0a },
1687         { 0x44, 0xf0 },
1688         { 0x45, 0x34 },
1689         { 0x46, 0x58 },
1690         { 0x47, 0x28 },
1691         { 0x48, 0x3a },
1692         { 0x59, 0x88 },
1693         { 0x5a, 0x88 },
1694         { 0x5b, 0x44 },
1695         { 0x5c, 0x67 },
1696         { 0x5d, 0x49 },
1697         { 0x5e, 0x0e },
1698         { 0x6c, 0x0a },
1699         { 0x6d, 0x55 },
1700         { 0x6e, 0x11 },
1701         { 0x6f, 0x9f },
1702                                         /* "9e for advance AWB" */
1703         { 0x6a, 0x40 },
1704         { OV7670_R01_BLUE, 0x40 },
1705         { OV7670_R02_RED, 0x60 },
1706         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1707                         | OV7670_COM8_AECSTEP
1708                         | OV7670_COM8_BFILT
1709                         | OV7670_COM8_AGC
1710                         | OV7670_COM8_AEC
1711                         | OV7670_COM8_AWB },
1712
1713 /* Matrix coefficients */
1714         { 0x4f, 0x80 },
1715         { 0x50, 0x80 },
1716         { 0x51, 0x00 },
1717         { 0x52, 0x22 },
1718         { 0x53, 0x5e },
1719         { 0x54, 0x80 },
1720         { 0x58, 0x9e },
1721
1722         { OV7670_R41_COM16, OV7670_COM16_AWBGAIN },
1723         { OV7670_R3F_EDGE, 0x00 },
1724         { 0x75, 0x05 },
1725         { 0x76, 0xe1 },
1726         { 0x4c, 0x00 },
1727         { 0x77, 0x01 },
1728         { OV7670_R3D_COM13, OV7670_COM13_GAMMA
1729                           | OV7670_COM13_UVSAT
1730                           | 2},         /* was 3 */
1731         { 0x4b, 0x09 },
1732         { 0xc9, 0x60 },
1733         { OV7670_R41_COM16, 0x38 },
1734         { 0x56, 0x40 },
1735
1736         { 0x34, 0x11 },
1737         { OV7670_R3B_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
1738         { 0xa4, 0x88 },
1739         { 0x96, 0x00 },
1740         { 0x97, 0x30 },
1741         { 0x98, 0x20 },
1742         { 0x99, 0x30 },
1743         { 0x9a, 0x84 },
1744         { 0x9b, 0x29 },
1745         { 0x9c, 0x03 },
1746         { 0x9d, 0x4c },
1747         { 0x9e, 0x3f },
1748         { 0x78, 0x04 },
1749
1750 /* Extra-weird stuff.  Some sort of multiplexor register */
1751         { 0x79, 0x01 },
1752         { 0xc8, 0xf0 },
1753         { 0x79, 0x0f },
1754         { 0xc8, 0x00 },
1755         { 0x79, 0x10 },
1756         { 0xc8, 0x7e },
1757         { 0x79, 0x0a },
1758         { 0xc8, 0x80 },
1759         { 0x79, 0x0b },
1760         { 0xc8, 0x01 },
1761         { 0x79, 0x0c },
1762         { 0xc8, 0x0f },
1763         { 0x79, 0x0d },
1764         { 0xc8, 0x20 },
1765         { 0x79, 0x09 },
1766         { 0xc8, 0x80 },
1767         { 0x79, 0x02 },
1768         { 0xc8, 0xc0 },
1769         { 0x79, 0x03 },
1770         { 0xc8, 0x40 },
1771         { 0x79, 0x05 },
1772         { 0xc8, 0x30 },
1773         { 0x79, 0x26 },
1774 };
1775
1776 static const struct ov_i2c_regvals norm_8610[] = {
1777         { 0x12, 0x80 },
1778         { 0x00, 0x00 },
1779         { 0x01, 0x80 },
1780         { 0x02, 0x80 },
1781         { 0x03, 0xc0 },
1782         { 0x04, 0x30 },
1783         { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
1784         { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
1785         { 0x0a, 0x86 },
1786         { 0x0b, 0xb0 },
1787         { 0x0c, 0x20 },
1788         { 0x0d, 0x20 },
1789         { 0x11, 0x01 },
1790         { 0x12, 0x25 },
1791         { 0x13, 0x01 },
1792         { 0x14, 0x04 },
1793         { 0x15, 0x01 }, /* Lin and Win think different about UV order */
1794         { 0x16, 0x03 },
1795         { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
1796         { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
1797         { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
1798         { 0x1a, 0xf5 },
1799         { 0x1b, 0x00 },
1800         { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
1801         { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
1802         { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
1803         { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
1804         { 0x26, 0xa2 },
1805         { 0x27, 0xea },
1806         { 0x28, 0x00 },
1807         { 0x29, 0x00 },
1808         { 0x2a, 0x80 },
1809         { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
1810         { 0x2c, 0xac },
1811         { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
1812         { 0x2e, 0x80 },
1813         { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
1814         { 0x4c, 0x00 },
1815         { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
1816         { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
1817         { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
1818         { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
1819         { 0x63, 0xff },
1820         { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
1821                          * maybe thats wrong */
1822         { 0x65, 0x00 },
1823         { 0x66, 0x55 },
1824         { 0x67, 0xb0 },
1825         { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
1826         { 0x69, 0x02 },
1827         { 0x6a, 0x22 },
1828         { 0x6b, 0x00 },
1829         { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
1830                          * deleting bit7 colors the first images red */
1831         { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
1832         { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
1833         { 0x6f, 0x01 },
1834         { 0x70, 0x8b },
1835         { 0x71, 0x00 },
1836         { 0x72, 0x14 },
1837         { 0x73, 0x54 },
1838         { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
1839         { 0x75, 0x0e },
1840         { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
1841         { 0x77, 0xff },
1842         { 0x78, 0x80 },
1843         { 0x79, 0x80 },
1844         { 0x7a, 0x80 },
1845         { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
1846         { 0x7c, 0x00 },
1847         { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
1848         { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
1849         { 0x7f, 0xfb },
1850         { 0x80, 0x28 },
1851         { 0x81, 0x00 },
1852         { 0x82, 0x23 },
1853         { 0x83, 0x0b },
1854         { 0x84, 0x00 },
1855         { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
1856         { 0x86, 0xc9 },
1857         { 0x87, 0x00 },
1858         { 0x88, 0x00 },
1859         { 0x89, 0x01 },
1860         { 0x12, 0x20 },
1861         { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
1862 };
1863
1864 static unsigned char ov7670_abs_to_sm(unsigned char v)
1865 {
1866         if (v > 127)
1867                 return v & 0x7f;
1868         return (128 - v) | 0x80;
1869 }
1870
1871 /* Write a OV519 register */
1872 static void reg_w(struct sd *sd, u16 index, u16 value)
1873 {
1874         int ret, req = 0;
1875
1876         if (sd->gspca_dev.usb_err < 0)
1877                 return;
1878
1879         switch (sd->bridge) {
1880         case BRIDGE_OV511:
1881         case BRIDGE_OV511PLUS:
1882                 req = 2;
1883                 break;
1884         case BRIDGE_OVFX2:
1885                 req = 0x0a;
1886                 /* fall through */
1887         case BRIDGE_W9968CF:
1888                 PDEBUG(D_USBO, "SET %02x %04x %04x",
1889                                 req, value, index);
1890                 ret = usb_control_msg(sd->gspca_dev.dev,
1891                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1892                         req,
1893                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1894                         value, index, NULL, 0, 500);
1895                 goto leave;
1896         default:
1897                 req = 1;
1898         }
1899
1900         PDEBUG(D_USBO, "SET %02x 0000 %04x %02x",
1901                         req, index, value);
1902         sd->gspca_dev.usb_buf[0] = value;
1903         ret = usb_control_msg(sd->gspca_dev.dev,
1904                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1905                         req,
1906                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1907                         0, index,
1908                         sd->gspca_dev.usb_buf, 1, 500);
1909 leave:
1910         if (ret < 0) {
1911                 err("reg_w %02x failed %d", index, ret);
1912                 sd->gspca_dev.usb_err = ret;
1913                 return;
1914         }
1915 }
1916
1917 /* Read from a OV519 register, note not valid for the w9968cf!! */
1918 /* returns: negative is error, pos or zero is data */
1919 static int reg_r(struct sd *sd, u16 index)
1920 {
1921         int ret;
1922         int req;
1923
1924         if (sd->gspca_dev.usb_err < 0)
1925                 return -1;
1926
1927         switch (sd->bridge) {
1928         case BRIDGE_OV511:
1929         case BRIDGE_OV511PLUS:
1930                 req = 3;
1931                 break;
1932         case BRIDGE_OVFX2:
1933                 req = 0x0b;
1934                 break;
1935         default:
1936                 req = 1;
1937         }
1938
1939         ret = usb_control_msg(sd->gspca_dev.dev,
1940                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1941                         req,
1942                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1943                         0, index, sd->gspca_dev.usb_buf, 1, 500);
1944
1945         if (ret >= 0) {
1946                 ret = sd->gspca_dev.usb_buf[0];
1947                 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x",
1948                         req, index, ret);
1949         } else {
1950                 err("reg_r %02x failed %d", index, ret);
1951                 sd->gspca_dev.usb_err = ret;
1952         }
1953
1954         return ret;
1955 }
1956
1957 /* Read 8 values from a OV519 register */
1958 static int reg_r8(struct sd *sd,
1959                   u16 index)
1960 {
1961         int ret;
1962
1963         if (sd->gspca_dev.usb_err < 0)
1964                 return -1;
1965
1966         ret = usb_control_msg(sd->gspca_dev.dev,
1967                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1968                         1,                      /* REQ_IO */
1969                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1970                         0, index, sd->gspca_dev.usb_buf, 8, 500);
1971
1972         if (ret >= 0) {
1973                 ret = sd->gspca_dev.usb_buf[0];
1974         } else {
1975                 err("reg_r8 %02x failed %d", index, ret);
1976                 sd->gspca_dev.usb_err = ret;
1977         }
1978
1979         return ret;
1980 }
1981
1982 /*
1983  * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1984  * the same position as 1's in "mask" are cleared and set to "value". Bits
1985  * that are in the same position as 0's in "mask" are preserved, regardless
1986  * of their respective state in "value".
1987  */
1988 static void reg_w_mask(struct sd *sd,
1989                         u16 index,
1990                         u8 value,
1991                         u8 mask)
1992 {
1993         int ret;
1994         u8 oldval;
1995
1996         if (mask != 0xff) {
1997                 value &= mask;                  /* Enforce mask on value */
1998                 ret = reg_r(sd, index);
1999                 if (ret < 0)
2000                         return;
2001
2002                 oldval = ret & ~mask;           /* Clear the masked bits */
2003                 value |= oldval;                /* Set the desired bits */
2004         }
2005         reg_w(sd, index, value);
2006 }
2007
2008 /*
2009  * Writes multiple (n) byte value to a single register. Only valid with certain
2010  * registers (0x30 and 0xc4 - 0xce).
2011  */
2012 static void ov518_reg_w32(struct sd *sd, u16 index, u32 value, int n)
2013 {
2014         int ret;
2015
2016         if (sd->gspca_dev.usb_err < 0)
2017                 return;
2018
2019         *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
2020
2021         ret = usb_control_msg(sd->gspca_dev.dev,
2022                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2023                         1 /* REG_IO */,
2024                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2025                         0, index,
2026                         sd->gspca_dev.usb_buf, n, 500);
2027         if (ret < 0) {
2028                 err("reg_w32 %02x failed %d", index, ret);
2029                 sd->gspca_dev.usb_err = ret;
2030         }
2031 }
2032
2033 static void ov511_i2c_w(struct sd *sd, u8 reg, u8 value)
2034 {
2035         int rc, retries;
2036
2037         PDEBUG(D_USBO, "ov511_i2c_w %02x %02x", reg, value);
2038
2039         /* Three byte write cycle */
2040         for (retries = 6; ; ) {
2041                 /* Select camera register */
2042                 reg_w(sd, R51x_I2C_SADDR_3, reg);
2043
2044                 /* Write "value" to I2C data port of OV511 */
2045                 reg_w(sd, R51x_I2C_DATA, value);
2046
2047                 /* Initiate 3-byte write cycle */
2048                 reg_w(sd, R511_I2C_CTL, 0x01);
2049
2050                 do {
2051                         rc = reg_r(sd, R511_I2C_CTL);
2052                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2053
2054                 if (rc < 0)
2055                         return;
2056
2057                 if ((rc & 2) == 0) /* Ack? */
2058                         break;
2059                 if (--retries < 0) {
2060                         PDEBUG(D_USBO, "i2c write retries exhausted");
2061                         return;
2062                 }
2063         }
2064 }
2065
2066 static int ov511_i2c_r(struct sd *sd, u8 reg)
2067 {
2068         int rc, value, retries;
2069
2070         /* Two byte write cycle */
2071         for (retries = 6; ; ) {
2072                 /* Select camera register */
2073                 reg_w(sd, R51x_I2C_SADDR_2, reg);
2074
2075                 /* Initiate 2-byte write cycle */
2076                 reg_w(sd, R511_I2C_CTL, 0x03);
2077
2078                 do {
2079                         rc = reg_r(sd, R511_I2C_CTL);
2080                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2081
2082                 if (rc < 0)
2083                         return rc;
2084
2085                 if ((rc & 2) == 0) /* Ack? */
2086                         break;
2087
2088                 /* I2C abort */
2089                 reg_w(sd, R511_I2C_CTL, 0x10);
2090
2091                 if (--retries < 0) {
2092                         PDEBUG(D_USBI, "i2c write retries exhausted");
2093                         return -1;
2094                 }
2095         }
2096
2097         /* Two byte read cycle */
2098         for (retries = 6; ; ) {
2099                 /* Initiate 2-byte read cycle */
2100                 reg_w(sd, R511_I2C_CTL, 0x05);
2101
2102                 do {
2103                         rc = reg_r(sd, R511_I2C_CTL);
2104                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2105
2106                 if (rc < 0)
2107                         return rc;
2108
2109                 if ((rc & 2) == 0) /* Ack? */
2110                         break;
2111
2112                 /* I2C abort */
2113                 reg_w(sd, R511_I2C_CTL, 0x10);
2114
2115                 if (--retries < 0) {
2116                         PDEBUG(D_USBI, "i2c read retries exhausted");
2117                         return -1;
2118                 }
2119         }
2120
2121         value = reg_r(sd, R51x_I2C_DATA);
2122
2123         PDEBUG(D_USBI, "ov511_i2c_r %02x %02x", reg, value);
2124
2125         /* This is needed to make i2c_w() work */
2126         reg_w(sd, R511_I2C_CTL, 0x05);
2127
2128         return value;
2129 }
2130
2131 /*
2132  * The OV518 I2C I/O procedure is different, hence, this function.
2133  * This is normally only called from i2c_w(). Note that this function
2134  * always succeeds regardless of whether the sensor is present and working.
2135  */
2136 static void ov518_i2c_w(struct sd *sd,
2137                 u8 reg,
2138                 u8 value)
2139 {
2140         PDEBUG(D_USBO, "ov518_i2c_w %02x %02x", reg, value);
2141
2142         /* Select camera register */
2143         reg_w(sd, R51x_I2C_SADDR_3, reg);
2144
2145         /* Write "value" to I2C data port of OV511 */
2146         reg_w(sd, R51x_I2C_DATA, value);
2147
2148         /* Initiate 3-byte write cycle */
2149         reg_w(sd, R518_I2C_CTL, 0x01);
2150
2151         /* wait for write complete */
2152         msleep(4);
2153         reg_r8(sd, R518_I2C_CTL);
2154 }
2155
2156 /*
2157  * returns: negative is error, pos or zero is data
2158  *
2159  * The OV518 I2C I/O procedure is different, hence, this function.
2160  * This is normally only called from i2c_r(). Note that this function
2161  * always succeeds regardless of whether the sensor is present and working.
2162  */
2163 static int ov518_i2c_r(struct sd *sd, u8 reg)
2164 {
2165         int value;
2166
2167         /* Select camera register */
2168         reg_w(sd, R51x_I2C_SADDR_2, reg);
2169
2170         /* Initiate 2-byte write cycle */
2171         reg_w(sd, R518_I2C_CTL, 0x03);
2172
2173         /* Initiate 2-byte read cycle */
2174         reg_w(sd, R518_I2C_CTL, 0x05);
2175         value = reg_r(sd, R51x_I2C_DATA);
2176         PDEBUG(D_USBI, "ov518_i2c_r %02x %02x", reg, value);
2177         return value;
2178 }
2179
2180 static void ovfx2_i2c_w(struct sd *sd, u8 reg, u8 value)
2181 {
2182         int ret;
2183
2184         if (sd->gspca_dev.usb_err < 0)
2185                 return;
2186
2187         ret = usb_control_msg(sd->gspca_dev.dev,
2188                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2189                         0x02,
2190                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2191                         (u16) value, (u16) reg, NULL, 0, 500);
2192
2193         if (ret < 0) {
2194                 err("ovfx2_i2c_w %02x failed %d", reg, ret);
2195                 sd->gspca_dev.usb_err = ret;
2196         }
2197
2198         PDEBUG(D_USBO, "ovfx2_i2c_w %02x %02x", reg, value);
2199 }
2200
2201 static int ovfx2_i2c_r(struct sd *sd, u8 reg)
2202 {
2203         int ret;
2204
2205         if (sd->gspca_dev.usb_err < 0)
2206                 return -1;
2207
2208         ret = usb_control_msg(sd->gspca_dev.dev,
2209                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2210                         0x03,
2211                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2212                         0, (u16) reg, sd->gspca_dev.usb_buf, 1, 500);
2213
2214         if (ret >= 0) {
2215                 ret = sd->gspca_dev.usb_buf[0];
2216                 PDEBUG(D_USBI, "ovfx2_i2c_r %02x %02x", reg, ret);
2217         } else {
2218                 err("ovfx2_i2c_r %02x failed %d", reg, ret);
2219                 sd->gspca_dev.usb_err = ret;
2220         }
2221
2222         return ret;
2223 }
2224
2225 static void i2c_w(struct sd *sd, u8 reg, u8 value)
2226 {
2227         if (sd->sensor_reg_cache[reg] == value)
2228                 return;
2229
2230         switch (sd->bridge) {
2231         case BRIDGE_OV511:
2232         case BRIDGE_OV511PLUS:
2233                 ov511_i2c_w(sd, reg, value);
2234                 break;
2235         case BRIDGE_OV518:
2236         case BRIDGE_OV518PLUS:
2237         case BRIDGE_OV519:
2238                 ov518_i2c_w(sd, reg, value);
2239                 break;
2240         case BRIDGE_OVFX2:
2241                 ovfx2_i2c_w(sd, reg, value);
2242                 break;
2243         case BRIDGE_W9968CF:
2244                 w9968cf_i2c_w(sd, reg, value);
2245                 break;
2246         }
2247
2248         if (sd->gspca_dev.usb_err >= 0) {
2249                 /* Up on sensor reset empty the register cache */
2250                 if (reg == 0x12 && (value & 0x80))
2251                         memset(sd->sensor_reg_cache, -1,
2252                                 sizeof(sd->sensor_reg_cache));
2253                 else
2254                         sd->sensor_reg_cache[reg] = value;
2255         }
2256 }
2257
2258 static int i2c_r(struct sd *sd, u8 reg)
2259 {
2260         int ret = -1;
2261
2262         if (sd->sensor_reg_cache[reg] != -1)
2263                 return sd->sensor_reg_cache[reg];
2264
2265         switch (sd->bridge) {
2266         case BRIDGE_OV511:
2267         case BRIDGE_OV511PLUS:
2268                 ret = ov511_i2c_r(sd, reg);
2269                 break;
2270         case BRIDGE_OV518:
2271         case BRIDGE_OV518PLUS:
2272         case BRIDGE_OV519:
2273                 ret = ov518_i2c_r(sd, reg);
2274                 break;
2275         case BRIDGE_OVFX2:
2276                 ret = ovfx2_i2c_r(sd, reg);
2277                 break;
2278         case BRIDGE_W9968CF:
2279                 ret = w9968cf_i2c_r(sd, reg);
2280                 break;
2281         }
2282
2283         if (ret >= 0)
2284                 sd->sensor_reg_cache[reg] = ret;
2285
2286         return ret;
2287 }
2288
2289 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2290  * the same position as 1's in "mask" are cleared and set to "value". Bits
2291  * that are in the same position as 0's in "mask" are preserved, regardless
2292  * of their respective state in "value".
2293  */
2294 static void i2c_w_mask(struct sd *sd,
2295                         u8 reg,
2296                         u8 value,
2297                         u8 mask)
2298 {
2299         int rc;
2300         u8 oldval;
2301
2302         value &= mask;                  /* Enforce mask on value */
2303         rc = i2c_r(sd, reg);
2304         if (rc < 0)
2305                 return;
2306         oldval = rc & ~mask;            /* Clear the masked bits */
2307         value |= oldval;                /* Set the desired bits */
2308         i2c_w(sd, reg, value);
2309 }
2310
2311 /* Temporarily stops OV511 from functioning. Must do this before changing
2312  * registers while the camera is streaming */
2313 static inline void ov51x_stop(struct sd *sd)
2314 {
2315         PDEBUG(D_STREAM, "stopping");
2316         sd->stopped = 1;
2317         switch (sd->bridge) {
2318         case BRIDGE_OV511:
2319         case BRIDGE_OV511PLUS:
2320                 reg_w(sd, R51x_SYS_RESET, 0x3d);
2321                 break;
2322         case BRIDGE_OV518:
2323         case BRIDGE_OV518PLUS:
2324                 reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2325                 break;
2326         case BRIDGE_OV519:
2327                 reg_w(sd, OV519_R51_RESET1, 0x0f);
2328                 break;
2329         case BRIDGE_OVFX2:
2330                 reg_w_mask(sd, 0x0f, 0x00, 0x02);
2331                 break;
2332         case BRIDGE_W9968CF:
2333                 reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
2334                 break;
2335         }
2336 }
2337
2338 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2339  * actually stopped (for performance). */
2340 static inline void ov51x_restart(struct sd *sd)
2341 {
2342         PDEBUG(D_STREAM, "restarting");
2343         if (!sd->stopped)
2344                 return;
2345         sd->stopped = 0;
2346
2347         /* Reinitialize the stream */
2348         switch (sd->bridge) {
2349         case BRIDGE_OV511:
2350         case BRIDGE_OV511PLUS:
2351                 reg_w(sd, R51x_SYS_RESET, 0x00);
2352                 break;
2353         case BRIDGE_OV518:
2354         case BRIDGE_OV518PLUS:
2355                 reg_w(sd, 0x2f, 0x80);
2356                 reg_w(sd, R51x_SYS_RESET, 0x00);
2357                 break;
2358         case BRIDGE_OV519:
2359                 reg_w(sd, OV519_R51_RESET1, 0x00);
2360                 break;
2361         case BRIDGE_OVFX2:
2362                 reg_w_mask(sd, 0x0f, 0x02, 0x02);
2363                 break;
2364         case BRIDGE_W9968CF:
2365                 reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
2366                 break;
2367         }
2368 }
2369
2370 static void ov51x_set_slave_ids(struct sd *sd, u8 slave);
2371
2372 /* This does an initial reset of an OmniVision sensor and ensures that I2C
2373  * is synchronized. Returns <0 on failure.
2374  */
2375 static int init_ov_sensor(struct sd *sd, u8 slave)
2376 {
2377         int i;
2378
2379         ov51x_set_slave_ids(sd, slave);
2380
2381         /* Reset the sensor */
2382         i2c_w(sd, 0x12, 0x80);
2383
2384         /* Wait for it to initialize */
2385         msleep(150);
2386
2387         for (i = 0; i < i2c_detect_tries; i++) {
2388                 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2389                     i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
2390                         PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2391                         return 0;
2392                 }
2393
2394                 /* Reset the sensor */
2395                 i2c_w(sd, 0x12, 0x80);
2396
2397                 /* Wait for it to initialize */
2398                 msleep(150);
2399
2400                 /* Dummy read to sync I2C */
2401                 if (i2c_r(sd, 0x00) < 0)
2402                         return -1;
2403         }
2404         return -1;
2405 }
2406
2407 /* Set the read and write slave IDs. The "slave" argument is the write slave,
2408  * and the read slave will be set to (slave + 1).
2409  * This should not be called from outside the i2c I/O functions.
2410  * Sets I2C read and write slave IDs. Returns <0 for error
2411  */
2412 static void ov51x_set_slave_ids(struct sd *sd,
2413                                 u8 slave)
2414 {
2415         switch (sd->bridge) {
2416         case BRIDGE_OVFX2:
2417                 reg_w(sd, OVFX2_I2C_ADDR, slave);
2418                 return;
2419         case BRIDGE_W9968CF:
2420                 sd->sensor_addr = slave;
2421                 return;
2422         }
2423
2424         reg_w(sd, R51x_I2C_W_SID, slave);
2425         reg_w(sd, R51x_I2C_R_SID, slave + 1);
2426 }
2427
2428 static void write_regvals(struct sd *sd,
2429                          const struct ov_regvals *regvals,
2430                          int n)
2431 {
2432         while (--n >= 0) {
2433                 reg_w(sd, regvals->reg, regvals->val);
2434                 regvals++;
2435         }
2436 }
2437
2438 static void write_i2c_regvals(struct sd *sd,
2439                         const struct ov_i2c_regvals *regvals,
2440                         int n)
2441 {
2442         while (--n >= 0) {
2443                 i2c_w(sd, regvals->reg, regvals->val);
2444                 regvals++;
2445         }
2446 }
2447
2448 /****************************************************************************
2449  *
2450  * OV511 and sensor configuration
2451  *
2452  ***************************************************************************/
2453
2454 /* This initializes the OV2x10 / OV3610 / OV3620 */
2455 static void ov_hires_configure(struct sd *sd)
2456 {
2457         int high, low;
2458
2459         if (sd->bridge != BRIDGE_OVFX2) {
2460                 err("error hires sensors only supported with ovfx2");
2461                 return;
2462         }
2463
2464         PDEBUG(D_PROBE, "starting ov hires configuration");
2465
2466         /* Detect sensor (sub)type */
2467         high = i2c_r(sd, 0x0a);
2468         low = i2c_r(sd, 0x0b);
2469         /* info("%x, %x", high, low); */
2470         if (high == 0x96 && low == 0x40) {
2471                 PDEBUG(D_PROBE, "Sensor is an OV2610");
2472                 sd->sensor = SEN_OV2610;
2473         } else if (high == 0x36 && (low & 0x0f) == 0x00) {
2474                 PDEBUG(D_PROBE, "Sensor is an OV3610");
2475                 sd->sensor = SEN_OV3610;
2476         } else {
2477                 err("Error unknown sensor type: %02x%02x",
2478                         high, low);
2479         }
2480 }
2481
2482 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2483  * the same register settings as the OV8610, since they are very similar.
2484  */
2485 static void ov8xx0_configure(struct sd *sd)
2486 {
2487         int rc;
2488
2489         PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2490
2491         /* Detect sensor (sub)type */
2492         rc = i2c_r(sd, OV7610_REG_COM_I);
2493         if (rc < 0) {
2494                 PDEBUG(D_ERR, "Error detecting sensor type");
2495                 return;
2496         }
2497         if ((rc & 3) == 1)
2498                 sd->sensor = SEN_OV8610;
2499         else
2500                 err("Unknown image sensor version: %d", rc & 3);
2501 }
2502
2503 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2504  * the same register settings as the OV7610, since they are very similar.
2505  */
2506 static void ov7xx0_configure(struct sd *sd)
2507 {
2508         int rc, high, low;
2509
2510         PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2511
2512         /* Detect sensor (sub)type */
2513         rc = i2c_r(sd, OV7610_REG_COM_I);
2514
2515         /* add OV7670 here
2516          * it appears to be wrongly detected as a 7610 by default */
2517         if (rc < 0) {
2518                 PDEBUG(D_ERR, "Error detecting sensor type");
2519                 return;
2520         }
2521         if ((rc & 3) == 3) {
2522                 /* quick hack to make OV7670s work */
2523                 high = i2c_r(sd, 0x0a);
2524                 low = i2c_r(sd, 0x0b);
2525                 /* info("%x, %x", high, low); */
2526                 if (high == 0x76 && low == 0x73) {
2527                         PDEBUG(D_PROBE, "Sensor is an OV7670");
2528                         sd->sensor = SEN_OV7670;
2529                 } else {
2530                         PDEBUG(D_PROBE, "Sensor is an OV7610");
2531                         sd->sensor = SEN_OV7610;
2532                 }
2533         } else if ((rc & 3) == 1) {
2534                 /* I don't know what's different about the 76BE yet. */
2535                 if (i2c_r(sd, 0x15) & 1) {
2536                         PDEBUG(D_PROBE, "Sensor is an OV7620AE");
2537                         sd->sensor = SEN_OV7620AE;
2538                 } else {
2539                         PDEBUG(D_PROBE, "Sensor is an OV76BE");
2540                         sd->sensor = SEN_OV76BE;
2541                 }
2542         } else if ((rc & 3) == 0) {
2543                 /* try to read product id registers */
2544                 high = i2c_r(sd, 0x0a);
2545                 if (high < 0) {
2546                         PDEBUG(D_ERR, "Error detecting camera chip PID");
2547                         return;
2548                 }
2549                 low = i2c_r(sd, 0x0b);
2550                 if (low < 0) {
2551                         PDEBUG(D_ERR, "Error detecting camera chip VER");
2552                         return;
2553                 }
2554                 if (high == 0x76) {
2555                         switch (low) {
2556                         case 0x30:
2557                                 err("Sensor is an OV7630/OV7635");
2558                                 err("7630 is not supported by this driver");
2559                                 return;
2560                         case 0x40:
2561                                 PDEBUG(D_PROBE, "Sensor is an OV7645");
2562                                 sd->sensor = SEN_OV7640; /* FIXME */
2563                                 break;
2564                         case 0x45:
2565                                 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2566                                 sd->sensor = SEN_OV7640; /* FIXME */
2567                                 break;
2568                         case 0x48:
2569                                 PDEBUG(D_PROBE, "Sensor is an OV7648");
2570                                 sd->sensor = SEN_OV7648;
2571                                 break;
2572                         default:
2573                                 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
2574                                 return;
2575                         }
2576                 } else {
2577                         PDEBUG(D_PROBE, "Sensor is an OV7620");
2578                         sd->sensor = SEN_OV7620;
2579                 }
2580         } else {
2581                 err("Unknown image sensor version: %d", rc & 3);
2582         }
2583 }
2584
2585 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
2586 static void ov6xx0_configure(struct sd *sd)
2587 {
2588         int rc;
2589         PDEBUG(D_PROBE, "starting OV6xx0 configuration");
2590
2591         /* Detect sensor (sub)type */
2592         rc = i2c_r(sd, OV7610_REG_COM_I);
2593         if (rc < 0) {
2594                 PDEBUG(D_ERR, "Error detecting sensor type");
2595                 return;
2596         }
2597
2598         /* Ugh. The first two bits are the version bits, but
2599          * the entire register value must be used. I guess OVT
2600          * underestimated how many variants they would make. */
2601         switch (rc) {
2602         case 0x00:
2603                 sd->sensor = SEN_OV6630;
2604                 warn("WARNING: Sensor is an OV66308. Your camera may have");
2605                 warn("been misdetected in previous driver versions.");
2606                 break;
2607         case 0x01:
2608                 sd->sensor = SEN_OV6620;
2609                 PDEBUG(D_PROBE, "Sensor is an OV6620");
2610                 break;
2611         case 0x02:
2612                 sd->sensor = SEN_OV6630;
2613                 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
2614                 break;
2615         case 0x03:
2616                 sd->sensor = SEN_OV66308AF;
2617                 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
2618                 break;
2619         case 0x90:
2620                 sd->sensor = SEN_OV6630;
2621                 warn("WARNING: Sensor is an OV66307. Your camera may have");
2622                 warn("been misdetected in previous driver versions.");
2623                 break;
2624         default:
2625                 err("FATAL: Unknown sensor version: 0x%02x", rc);
2626                 return;
2627         }
2628
2629         /* Set sensor-specific vars */
2630         sd->sif = 1;
2631 }
2632
2633 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2634 static void ov51x_led_control(struct sd *sd, int on)
2635 {
2636         if (sd->invert_led)
2637                 on = !on;
2638
2639         switch (sd->bridge) {
2640         /* OV511 has no LED control */
2641         case BRIDGE_OV511PLUS:
2642                 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
2643                 break;
2644         case BRIDGE_OV518:
2645         case BRIDGE_OV518PLUS:
2646                 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
2647                 break;
2648         case BRIDGE_OV519:
2649                 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1);   /* 0 / 1 */
2650                 break;
2651         }
2652 }
2653
2654 static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2655 {
2656         struct sd *sd = (struct sd *) gspca_dev;
2657
2658         if (!sd->snapshot_needs_reset)
2659                 return;
2660
2661         /* Note it is important that we clear sd->snapshot_needs_reset,
2662            before actually clearing the snapshot state in the bridge
2663            otherwise we might race with the pkt_scan interrupt handler */
2664         sd->snapshot_needs_reset = 0;
2665
2666         switch (sd->bridge) {
2667         case BRIDGE_OV511:
2668         case BRIDGE_OV511PLUS:
2669                 reg_w(sd, R51x_SYS_SNAP, 0x02);
2670                 reg_w(sd, R51x_SYS_SNAP, 0x00);
2671                 break;
2672         case BRIDGE_OV518:
2673         case BRIDGE_OV518PLUS:
2674                 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2675                 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2676                 break;
2677         case BRIDGE_OV519:
2678                 reg_w(sd, R51x_SYS_RESET, 0x40);
2679                 reg_w(sd, R51x_SYS_RESET, 0x00);
2680                 break;
2681         }
2682 }
2683
2684 static void ov51x_upload_quan_tables(struct sd *sd)
2685 {
2686         const unsigned char yQuanTable511[] = {
2687                 0, 1, 1, 2, 2, 3, 3, 4,
2688                 1, 1, 1, 2, 2, 3, 4, 4,
2689                 1, 1, 2, 2, 3, 4, 4, 4,
2690                 2, 2, 2, 3, 4, 4, 4, 4,
2691                 2, 2, 3, 4, 4, 5, 5, 5,
2692                 3, 3, 4, 4, 5, 5, 5, 5,
2693                 3, 4, 4, 4, 5, 5, 5, 5,
2694                 4, 4, 4, 4, 5, 5, 5, 5
2695         };
2696
2697         const unsigned char uvQuanTable511[] = {
2698                 0, 2, 2, 3, 4, 4, 4, 4,
2699                 2, 2, 2, 4, 4, 4, 4, 4,
2700                 2, 2, 3, 4, 4, 4, 4, 4,
2701                 3, 4, 4, 4, 4, 4, 4, 4,
2702                 4, 4, 4, 4, 4, 4, 4, 4,
2703                 4, 4, 4, 4, 4, 4, 4, 4,
2704                 4, 4, 4, 4, 4, 4, 4, 4,
2705                 4, 4, 4, 4, 4, 4, 4, 4
2706         };
2707
2708         /* OV518 quantization tables are 8x4 (instead of 8x8) */
2709         const unsigned char yQuanTable518[] = {
2710                 5, 4, 5, 6, 6, 7, 7, 7,
2711                 5, 5, 5, 5, 6, 7, 7, 7,
2712                 6, 6, 6, 6, 7, 7, 7, 8,
2713                 7, 7, 6, 7, 7, 7, 8, 8
2714         };
2715         const unsigned char uvQuanTable518[] = {
2716                 6, 6, 6, 7, 7, 7, 7, 7,
2717                 6, 6, 6, 7, 7, 7, 7, 7,
2718                 6, 6, 6, 7, 7, 7, 7, 8,
2719                 7, 7, 7, 7, 7, 7, 8, 8
2720         };
2721
2722         const unsigned char *pYTable, *pUVTable;
2723         unsigned char val0, val1;
2724         int i, size, reg = R51x_COMP_LUT_BEGIN;
2725
2726         PDEBUG(D_PROBE, "Uploading quantization tables");
2727
2728         if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
2729                 pYTable = yQuanTable511;
2730                 pUVTable = uvQuanTable511;
2731                 size = 32;
2732         } else {
2733                 pYTable = yQuanTable518;
2734                 pUVTable = uvQuanTable518;
2735                 size = 16;
2736         }
2737
2738         for (i = 0; i < size; i++) {
2739                 val0 = *pYTable++;
2740                 val1 = *pYTable++;
2741                 val0 &= 0x0f;
2742                 val1 &= 0x0f;
2743                 val0 |= val1 << 4;
2744                 reg_w(sd, reg, val0);
2745
2746                 val0 = *pUVTable++;
2747                 val1 = *pUVTable++;
2748                 val0 &= 0x0f;
2749                 val1 &= 0x0f;
2750                 val0 |= val1 << 4;
2751                 reg_w(sd, reg + size, val0);
2752
2753                 reg++;
2754         }
2755 }
2756
2757 /* This initializes the OV511/OV511+ and the sensor */
2758 static void ov511_configure(struct gspca_dev *gspca_dev)
2759 {
2760         struct sd *sd = (struct sd *) gspca_dev;
2761
2762         /* For 511 and 511+ */
2763         const struct ov_regvals init_511[] = {
2764                 { R51x_SYS_RESET,       0x7f },
2765                 { R51x_SYS_INIT,        0x01 },
2766                 { R51x_SYS_RESET,       0x7f },
2767                 { R51x_SYS_INIT,        0x01 },
2768                 { R51x_SYS_RESET,       0x3f },
2769                 { R51x_SYS_INIT,        0x01 },
2770                 { R51x_SYS_RESET,       0x3d },
2771         };
2772
2773         const struct ov_regvals norm_511[] = {
2774                 { R511_DRAM_FLOW_CTL,   0x01 },
2775                 { R51x_SYS_SNAP,        0x00 },
2776                 { R51x_SYS_SNAP,        0x02 },
2777                 { R51x_SYS_SNAP,        0x00 },
2778                 { R511_FIFO_OPTS,       0x1f },
2779                 { R511_COMP_EN,         0x00 },
2780                 { R511_COMP_LUT_EN,     0x03 },
2781         };
2782
2783         const struct ov_regvals norm_511_p[] = {
2784                 { R511_DRAM_FLOW_CTL,   0xff },
2785                 { R51x_SYS_SNAP,        0x00 },
2786                 { R51x_SYS_SNAP,        0x02 },
2787                 { R51x_SYS_SNAP,        0x00 },
2788                 { R511_FIFO_OPTS,       0xff },
2789                 { R511_COMP_EN,         0x00 },
2790                 { R511_COMP_LUT_EN,     0x03 },
2791         };
2792
2793         const struct ov_regvals compress_511[] = {
2794                 { 0x70, 0x1f },
2795                 { 0x71, 0x05 },
2796                 { 0x72, 0x06 },
2797                 { 0x73, 0x06 },
2798                 { 0x74, 0x14 },
2799                 { 0x75, 0x03 },
2800                 { 0x76, 0x04 },
2801                 { 0x77, 0x04 },
2802         };
2803
2804         PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
2805
2806         write_regvals(sd, init_511, ARRAY_SIZE(init_511));
2807
2808         switch (sd->bridge) {
2809         case BRIDGE_OV511:
2810                 write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
2811                 break;
2812         case BRIDGE_OV511PLUS:
2813                 write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
2814                 break;
2815         }
2816
2817         /* Init compression */
2818         write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
2819
2820         ov51x_upload_quan_tables(sd);
2821 }
2822
2823 /* This initializes the OV518/OV518+ and the sensor */
2824 static void ov518_configure(struct gspca_dev *gspca_dev)
2825 {
2826         struct sd *sd = (struct sd *) gspca_dev;
2827
2828         /* For 518 and 518+ */
2829         const struct ov_regvals init_518[] = {
2830                 { R51x_SYS_RESET,       0x40 },
2831                 { R51x_SYS_INIT,        0xe1 },
2832                 { R51x_SYS_RESET,       0x3e },
2833                 { R51x_SYS_INIT,        0xe1 },
2834                 { R51x_SYS_RESET,       0x00 },
2835                 { R51x_SYS_INIT,        0xe1 },
2836                 { 0x46,                 0x00 },
2837                 { 0x5d,                 0x03 },
2838         };
2839
2840         const struct ov_regvals norm_518[] = {
2841                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
2842                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
2843                 { 0x31,                 0x0f },
2844                 { 0x5d,                 0x03 },
2845                 { 0x24,                 0x9f },
2846                 { 0x25,                 0x90 },
2847                 { 0x20,                 0x00 },
2848                 { 0x51,                 0x04 },
2849                 { 0x71,                 0x19 },
2850                 { 0x2f,                 0x80 },
2851         };
2852
2853         const struct ov_regvals norm_518_p[] = {
2854                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
2855                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
2856                 { 0x31,                 0x0f },
2857                 { 0x5d,                 0x03 },
2858                 { 0x24,                 0x9f },
2859                 { 0x25,                 0x90 },
2860                 { 0x20,                 0x60 },
2861                 { 0x51,                 0x02 },
2862                 { 0x71,                 0x19 },
2863                 { 0x40,                 0xff },
2864                 { 0x41,                 0x42 },
2865                 { 0x46,                 0x00 },
2866                 { 0x33,                 0x04 },
2867                 { 0x21,                 0x19 },
2868                 { 0x3f,                 0x10 },
2869                 { 0x2f,                 0x80 },
2870         };
2871
2872         /* First 5 bits of custom ID reg are a revision ID on OV518 */
2873         PDEBUG(D_PROBE, "Device revision %d",
2874                 0x1f & reg_r(sd, R51x_SYS_CUST_ID));
2875
2876         write_regvals(sd, init_518, ARRAY_SIZE(init_518));
2877
2878         /* Set LED GPIO pin to output mode */
2879         reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
2880
2881         switch (sd->bridge) {
2882         case BRIDGE_OV518:
2883                 write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
2884                 break;
2885         case BRIDGE_OV518PLUS:
2886                 write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
2887                 break;
2888         }
2889
2890         ov51x_upload_quan_tables(sd);
2891
2892         reg_w(sd, 0x2f, 0x80);
2893 }
2894
2895 static void ov519_configure(struct sd *sd)
2896 {
2897         static const struct ov_regvals init_519[] = {
2898                 { 0x5a, 0x6d }, /* EnableSystem */
2899                 { 0x53, 0x9b },
2900                 { OV519_R54_EN_CLK1, 0xff }, /* set bit2 to enable jpeg */
2901                 { 0x5d, 0x03 },
2902                 { 0x49, 0x01 },
2903                 { 0x48, 0x00 },
2904                 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
2905                  * detection will fail. This deserves further investigation. */
2906                 { OV519_GPIO_IO_CTRL0,   0xee },
2907                 { OV519_R51_RESET1, 0x0f },
2908                 { OV519_R51_RESET1, 0x00 },
2909                 { 0x22, 0x00 },
2910                 /* windows reads 0x55 at this point*/
2911         };
2912
2913         write_regvals(sd, init_519, ARRAY_SIZE(init_519));
2914 }
2915
2916 static void ovfx2_configure(struct sd *sd)
2917 {
2918         static const struct ov_regvals init_fx2[] = {
2919                 { 0x00, 0x60 },
2920                 { 0x02, 0x01 },
2921                 { 0x0f, 0x1d },
2922                 { 0xe9, 0x82 },
2923                 { 0xea, 0xc7 },
2924                 { 0xeb, 0x10 },
2925                 { 0xec, 0xf6 },
2926         };
2927
2928         sd->stopped = 1;
2929
2930         write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
2931 }
2932
2933 /* this function is called at probe time */
2934 static int sd_config(struct gspca_dev *gspca_dev,
2935                         const struct usb_device_id *id)
2936 {
2937         struct sd *sd = (struct sd *) gspca_dev;
2938         struct cam *cam = &gspca_dev->cam;
2939
2940         sd->bridge = id->driver_info & BRIDGE_MASK;
2941         sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
2942
2943         switch (sd->bridge) {
2944         case BRIDGE_OV511:
2945         case BRIDGE_OV511PLUS:
2946                 ov511_configure(gspca_dev);
2947                 break;
2948         case BRIDGE_OV518:
2949         case BRIDGE_OV518PLUS:
2950                 ov518_configure(gspca_dev);
2951                 break;
2952         case BRIDGE_OV519:
2953                 ov519_configure(sd);
2954                 break;
2955         case BRIDGE_OVFX2:
2956                 ovfx2_configure(sd);
2957                 cam->bulk_size = OVFX2_BULK_SIZE;
2958                 cam->bulk_nurbs = MAX_NURBS;
2959                 cam->bulk = 1;
2960                 break;
2961         case BRIDGE_W9968CF:
2962                 w9968cf_configure(sd);
2963                 cam->reverse_alts = 1;
2964                 break;
2965         }
2966
2967         ov51x_led_control(sd, 0);       /* turn LED off */
2968
2969         /* The OV519 must be more aggressive about sensor detection since
2970          * I2C write will never fail if the sensor is not present. We have
2971          * to try to initialize the sensor to detect its presence */
2972         sd->sensor = -1;
2973
2974         /* Test for 76xx */
2975         if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
2976                 ov7xx0_configure(sd);
2977
2978         /* Test for 6xx0 */
2979         } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
2980                 ov6xx0_configure(sd);
2981
2982         /* Test for 8xx0 */
2983         } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
2984                 ov8xx0_configure(sd);
2985
2986         /* Test for 3xxx / 2xxx */
2987         } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
2988                 ov_hires_configure(sd);
2989         } else {
2990                 err("Can't determine sensor slave IDs");
2991                 goto error;
2992         }
2993
2994         if (sd->sensor < 0)
2995                 goto error;
2996
2997         switch (sd->bridge) {
2998         case BRIDGE_OV511:
2999         case BRIDGE_OV511PLUS:
3000                 if (!sd->sif) {
3001                         cam->cam_mode = ov511_vga_mode;
3002                         cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
3003                 } else {
3004                         cam->cam_mode = ov511_sif_mode;
3005                         cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3006                 }
3007                 break;
3008         case BRIDGE_OV518:
3009         case BRIDGE_OV518PLUS:
3010                 if (!sd->sif) {
3011                         cam->cam_mode = ov518_vga_mode;
3012                         cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
3013                 } else {
3014                         cam->cam_mode = ov518_sif_mode;
3015                         cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3016                 }
3017                 break;
3018         case BRIDGE_OV519:
3019                 if (!sd->sif) {
3020                         cam->cam_mode = ov519_vga_mode;
3021                         cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3022                 } else {
3023                         cam->cam_mode = ov519_sif_mode;
3024                         cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3025                 }
3026                 break;
3027         case BRIDGE_OVFX2:
3028                 if (sd->sensor == SEN_OV2610) {
3029                         cam->cam_mode = ovfx2_ov2610_mode;
3030                         cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
3031                 } else if (sd->sensor == SEN_OV3610) {
3032                         cam->cam_mode = ovfx2_ov3610_mode;
3033                         cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
3034                 } else if (!sd->sif) {
3035                         cam->cam_mode = ov519_vga_mode;
3036                         cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3037                 } else {
3038                         cam->cam_mode = ov519_sif_mode;
3039                         cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3040                 }
3041                 break;
3042         case BRIDGE_W9968CF:
3043                 cam->cam_mode = w9968cf_vga_mode;
3044                 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
3045                 if (sd->sif)
3046                         cam->nmodes--;
3047
3048                 /* w9968cf needs initialisation once the sensor is known */
3049                 w9968cf_init(sd);
3050                 break;
3051         }
3052         gspca_dev->cam.ctrls = sd->ctrls;
3053         sd->quality = QUALITY_DEF;
3054
3055         gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
3056
3057         return gspca_dev->usb_err;
3058 error:
3059         PDEBUG(D_ERR, "OV519 Config failed");
3060         return -EINVAL;
3061 }
3062
3063 /* this function is called at probe and resume time */
3064 static int sd_init(struct gspca_dev *gspca_dev)
3065 {
3066         struct sd *sd = (struct sd *) gspca_dev;
3067
3068         /* initialize the sensor */
3069         switch (sd->sensor) {
3070         case SEN_OV2610:
3071                 write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610));
3072
3073                 /* Enable autogain, autoexpo, awb, bandfilter */
3074                 i2c_w_mask(sd, 0x13, 0x27, 0x27);
3075                 break;
3076         case SEN_OV3610:
3077                 write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b));
3078
3079                 /* Enable autogain, autoexpo, awb, bandfilter */
3080                 i2c_w_mask(sd, 0x13, 0x27, 0x27);
3081                 break;
3082         case SEN_OV6620:
3083                 write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20));
3084                 break;
3085         case SEN_OV6630:
3086         case SEN_OV66308AF:
3087                 sd->ctrls[CONTRAST].def = 200;
3088                                  /* The default is too low for the ov6630 */
3089                 write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30));
3090                 break;
3091         default:
3092 /*      case SEN_OV7610: */
3093 /*      case SEN_OV76BE: */
3094                 write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610));
3095                 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
3096                 break;
3097         case SEN_OV7620:
3098         case SEN_OV7620AE:
3099                 write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620));
3100                 break;
3101         case SEN_OV7640:
3102         case SEN_OV7648:
3103                 write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640));
3104                 break;
3105         case SEN_OV7670:
3106                 sd->ctrls[FREQ].max = 3;        /* auto */
3107                 sd->ctrls[FREQ].def = 3;
3108                 write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670));
3109                 break;
3110         case SEN_OV8610:
3111                 write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610));
3112                 break;
3113         }
3114         return gspca_dev->usb_err;
3115 }
3116
3117 /* Set up the OV511/OV511+ with the given image parameters.
3118  *
3119  * Do not put any sensor-specific code in here (including I2C I/O functions)
3120  */
3121 static void ov511_mode_init_regs(struct sd *sd)
3122 {
3123         int hsegs, vsegs, packet_size, fps, needed;
3124         int interlaced = 0;
3125         struct usb_host_interface *alt;
3126         struct usb_interface *intf;
3127
3128         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3129         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3130         if (!alt) {
3131                 err("Couldn't get altsetting");
3132                 sd->gspca_dev.usb_err = -EIO;
3133                 return;
3134         }
3135
3136         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3137         reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3138
3139         reg_w(sd, R511_CAM_UV_EN, 0x01);
3140         reg_w(sd, R511_SNAP_UV_EN, 0x01);
3141         reg_w(sd, R511_SNAP_OPTS, 0x03);
3142
3143         /* Here I'm assuming that snapshot size == image size.
3144          * I hope that's always true. --claudio
3145          */
3146         hsegs = (sd->gspca_dev.width >> 3) - 1;
3147         vsegs = (sd->gspca_dev.height >> 3) - 1;
3148
3149         reg_w(sd, R511_CAM_PXCNT, hsegs);
3150         reg_w(sd, R511_CAM_LNCNT, vsegs);
3151         reg_w(sd, R511_CAM_PXDIV, 0x00);
3152         reg_w(sd, R511_CAM_LNDIV, 0x00);
3153
3154         /* YUV420, low pass filter on */
3155         reg_w(sd, R511_CAM_OPTS, 0x03);
3156
3157         /* Snapshot additions */
3158         reg_w(sd, R511_SNAP_PXCNT, hsegs);
3159         reg_w(sd, R511_SNAP_LNCNT, vsegs);
3160         reg_w(sd, R511_SNAP_PXDIV, 0x00);
3161         reg_w(sd, R511_SNAP_LNDIV, 0x00);
3162
3163         /******** Set the framerate ********/
3164         if (frame_rate > 0)
3165                 sd->frame_rate = frame_rate;
3166
3167         switch (sd->sensor) {
3168         case SEN_OV6620:
3169                 /* No framerate control, doesn't like higher rates yet */
3170                 sd->clockdiv = 3;
3171                 break;
3172
3173         /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3174            for more sensors we need to do this for them too */
3175         case SEN_OV7620:
3176         case SEN_OV7620AE:
3177         case SEN_OV7640:
3178         case SEN_OV7648:
3179         case SEN_OV76BE:
3180                 if (sd->gspca_dev.width == 320)
3181                         interlaced = 1;
3182                 /* Fall through */
3183         case SEN_OV6630:
3184         case SEN_OV7610:
3185         case SEN_OV7670:
3186                 switch (sd->frame_rate) {
3187                 case 30:
3188                 case 25:
3189                         /* Not enough bandwidth to do 640x480 @ 30 fps */
3190                         if (sd->gspca_dev.width != 640) {
3191                                 sd->clockdiv = 0;
3192                                 break;
3193                         }
3194                         /* Fall through for 640x480 case */
3195                 default:
3196 /*              case 20: */
3197 /*              case 15: */
3198                         sd->clockdiv = 1;
3199                         break;
3200                 case 10:
3201                         sd->clockdiv = 2;
3202                         break;
3203                 case 5:
3204                         sd->clockdiv = 5;
3205                         break;
3206                 }
3207                 if (interlaced) {
3208                         sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3209                         /* Higher then 10 does not work */
3210                         if (sd->clockdiv > 10)
3211                                 sd->clockdiv = 10;
3212                 }
3213                 break;
3214
3215         case SEN_OV8610:
3216                 /* No framerate control ?? */
3217                 sd->clockdiv = 0;
3218                 break;
3219         }
3220
3221         /* Check if we have enough bandwidth to disable compression */
3222         fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3223         needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3224         /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3225         if (needed > 1400 * packet_size) {
3226                 /* Enable Y and UV quantization and compression */
3227                 reg_w(sd, R511_COMP_EN, 0x07);
3228                 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3229         } else {
3230                 reg_w(sd, R511_COMP_EN, 0x06);
3231                 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3232         }
3233
3234         reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3235         reg_w(sd, R51x_SYS_RESET, 0);
3236 }
3237
3238 /* Sets up the OV518/OV518+ with the given image parameters
3239  *
3240  * OV518 needs a completely different approach, until we can figure out what
3241  * the individual registers do. Also, only 15 FPS is supported now.
3242  *
3243  * Do not put any sensor-specific code in here (including I2C I/O functions)
3244  */
3245 static void ov518_mode_init_regs(struct sd *sd)
3246 {
3247         int hsegs, vsegs, packet_size;
3248         struct usb_host_interface *alt;
3249         struct usb_interface *intf;
3250
3251         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3252         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3253         if (!alt) {
3254                 err("Couldn't get altsetting");
3255                 sd->gspca_dev.usb_err = -EIO;
3256                 return;
3257         }
3258
3259         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3260         ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
3261
3262         /******** Set the mode ********/
3263         reg_w(sd, 0x2b, 0);
3264         reg_w(sd, 0x2c, 0);
3265         reg_w(sd, 0x2d, 0);
3266         reg_w(sd, 0x2e, 0);
3267         reg_w(sd, 0x3b, 0);
3268         reg_w(sd, 0x3c, 0);
3269         reg_w(sd, 0x3d, 0);
3270         reg_w(sd, 0x3e, 0);
3271
3272         if (sd->bridge == BRIDGE_OV518) {
3273                 /* Set 8-bit (YVYU) input format */
3274                 reg_w_mask(sd, 0x20, 0x08, 0x08);
3275
3276                 /* Set 12-bit (4:2:0) output format */
3277                 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3278                 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3279         } else {
3280                 reg_w(sd, 0x28, 0x80);
3281                 reg_w(sd, 0x38, 0x80);
3282         }
3283
3284         hsegs = sd->gspca_dev.width / 16;
3285         vsegs = sd->gspca_dev.height / 4;
3286
3287         reg_w(sd, 0x29, hsegs);
3288         reg_w(sd, 0x2a, vsegs);
3289
3290         reg_w(sd, 0x39, hsegs);
3291         reg_w(sd, 0x3a, vsegs);
3292
3293         /* Windows driver does this here; who knows why */
3294         reg_w(sd, 0x2f, 0x80);
3295
3296         /******** Set the framerate ********/
3297         sd->clockdiv = 1;
3298
3299         /* Mode independent, but framerate dependent, regs */
3300         /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3301         reg_w(sd, 0x51, 0x04);
3302         reg_w(sd, 0x22, 0x18);
3303         reg_w(sd, 0x23, 0xff);
3304
3305         if (sd->bridge == BRIDGE_OV518PLUS) {
3306                 switch (sd->sensor) {
3307                 case SEN_OV7620AE:
3308                         if (sd->gspca_dev.width == 320) {
3309                                 reg_w(sd, 0x20, 0x00);
3310                                 reg_w(sd, 0x21, 0x19);
3311                         } else {
3312                                 reg_w(sd, 0x20, 0x60);
3313                                 reg_w(sd, 0x21, 0x1f);
3314                         }
3315                         break;
3316                 case SEN_OV7620:
3317                         reg_w(sd, 0x20, 0x00);
3318                         reg_w(sd, 0x21, 0x19);
3319                         break;
3320                 default:
3321                         reg_w(sd, 0x21, 0x19);
3322                 }
3323         } else
3324                 reg_w(sd, 0x71, 0x17);  /* Compression-related? */
3325
3326         /* FIXME: Sensor-specific */
3327         /* Bit 5 is what matters here. Of course, it is "reserved" */
3328         i2c_w(sd, 0x54, 0x23);
3329
3330         reg_w(sd, 0x2f, 0x80);
3331
3332         if (sd->bridge == BRIDGE_OV518PLUS) {
3333                 reg_w(sd, 0x24, 0x94);
3334                 reg_w(sd, 0x25, 0x90);
3335                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
3336                 ov518_reg_w32(sd, 0xc6,    540, 2);     /* 21ch   */
3337                 ov518_reg_w32(sd, 0xc7,    540, 2);     /* 21ch   */
3338                 ov518_reg_w32(sd, 0xc8,    108, 2);     /* 6ch    */
3339                 ov518_reg_w32(sd, 0xca, 131098, 3);     /* 2001ah */
3340                 ov518_reg_w32(sd, 0xcb,    532, 2);     /* 214h   */
3341                 ov518_reg_w32(sd, 0xcc,   2400, 2);     /* 960h   */
3342                 ov518_reg_w32(sd, 0xcd,     32, 2);     /* 20h    */
3343                 ov518_reg_w32(sd, 0xce,    608, 2);     /* 260h   */
3344         } else {
3345                 reg_w(sd, 0x24, 0x9f);
3346                 reg_w(sd, 0x25, 0x90);
3347                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
3348                 ov518_reg_w32(sd, 0xc6,    381, 2);     /* 17dh   */
3349                 ov518_reg_w32(sd, 0xc7,    381, 2);     /* 17dh   */
3350                 ov518_reg_w32(sd, 0xc8,    128, 2);     /* 80h    */
3351                 ov518_reg_w32(sd, 0xca, 183331, 3);     /* 2cc23h */
3352                 ov518_reg_w32(sd, 0xcb,    746, 2);     /* 2eah   */
3353                 ov518_reg_w32(sd, 0xcc,   1750, 2);     /* 6d6h   */
3354                 ov518_reg_w32(sd, 0xcd,     45, 2);     /* 2dh    */
3355                 ov518_reg_w32(sd, 0xce,    851, 2);     /* 353h   */
3356         }
3357
3358         reg_w(sd, 0x2f, 0x80);
3359 }
3360
3361 /* Sets up the OV519 with the given image parameters
3362  *
3363  * OV519 needs a completely different approach, until we can figure out what
3364  * the individual registers do.
3365  *
3366  * Do not put any sensor-specific code in here (including I2C I/O functions)
3367  */
3368 static void ov519_mode_init_regs(struct sd *sd)
3369 {
3370         static const struct ov_regvals mode_init_519_ov7670[] = {
3371                 { 0x5d, 0x03 }, /* Turn off suspend mode */
3372                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3373                 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
3374                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3375                 { 0xa3, 0x18 },
3376                 { 0xa4, 0x04 },
3377                 { 0xa5, 0x28 },
3378                 { 0x37, 0x00 }, /* SetUsbInit */
3379                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3380                 /* Enable both fields, YUV Input, disable defect comp (why?) */
3381                 { 0x20, 0x0c },
3382                 { 0x21, 0x38 },
3383                 { 0x22, 0x1d },
3384                 { 0x17, 0x50 }, /* undocumented */
3385                 { 0x37, 0x00 }, /* undocumented */
3386                 { 0x40, 0xff }, /* I2C timeout counter */
3387                 { 0x46, 0x00 }, /* I2C clock prescaler */
3388                 { 0x59, 0x04 }, /* new from windrv 090403 */
3389                 { 0xff, 0x00 }, /* undocumented */
3390                 /* windows reads 0x55 at this point, why? */
3391         };
3392
3393         static const struct ov_regvals mode_init_519[] = {
3394                 { 0x5d, 0x03 }, /* Turn off suspend mode */
3395                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3396                 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
3397                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3398                 { 0xa3, 0x18 },
3399                 { 0xa4, 0x04 },
3400                 { 0xa5, 0x28 },
3401                 { 0x37, 0x00 }, /* SetUsbInit */
3402                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3403                 /* Enable both fields, YUV Input, disable defect comp (why?) */
3404                 { 0x22, 0x1d },
3405                 { 0x17, 0x50 }, /* undocumented */
3406                 { 0x37, 0x00 }, /* undocumented */
3407                 { 0x40, 0xff }, /* I2C timeout counter */
3408                 { 0x46, 0x00 }, /* I2C clock prescaler */
3409                 { 0x59, 0x04 }, /* new from windrv 090403 */
3410                 { 0xff, 0x00 }, /* undocumented */
3411                 /* windows reads 0x55 at this point, why? */
3412         };
3413
3414         /******** Set the mode ********/
3415         if (sd->sensor != SEN_OV7670) {
3416                 write_regvals(sd, mode_init_519, ARRAY_SIZE(mode_init_519));
3417                 if (sd->sensor == SEN_OV7640 ||
3418                     sd->sensor == SEN_OV7648) {
3419                         /* Select 8-bit input mode */
3420                         reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
3421                 }
3422         } else {
3423                 write_regvals(sd, mode_init_519_ov7670,
3424                                 ARRAY_SIZE(mode_init_519_ov7670));
3425         }
3426
3427         reg_w(sd, OV519_R10_H_SIZE,     sd->gspca_dev.width >> 4);
3428         reg_w(sd, OV519_R11_V_SIZE,     sd->gspca_dev.height >> 3);
3429         if (sd->sensor == SEN_OV7670 &&
3430             sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3431                 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
3432         else if (sd->sensor == SEN_OV7648 &&
3433             sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3434                 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
3435         else
3436                 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
3437         reg_w(sd, OV519_R13_X_OFFSETH,  0x00);
3438         reg_w(sd, OV519_R14_Y_OFFSETL,  0x00);
3439         reg_w(sd, OV519_R15_Y_OFFSETH,  0x00);
3440         reg_w(sd, OV519_R16_DIVIDER,    0x00);
3441         reg_w(sd, OV519_R25_FORMAT,     0x03); /* YUV422 */
3442         reg_w(sd, 0x26,                 0x00); /* Undocumented */
3443
3444         /******** Set the framerate ********/
3445         if (frame_rate > 0)
3446                 sd->frame_rate = frame_rate;
3447
3448 /* FIXME: These are only valid at the max resolution. */
3449         sd->clockdiv = 0;
3450         switch (sd->sensor) {
3451         case SEN_OV7640:
3452         case SEN_OV7648:
3453                 switch (sd->frame_rate) {
3454                 default:
3455 /*              case 30: */
3456                         reg_w(sd, 0xa4, 0x0c);
3457                         reg_w(sd, 0x23, 0xff);
3458                         break;
3459                 case 25:
3460                         reg_w(sd, 0xa4, 0x0c);
3461                         reg_w(sd, 0x23, 0x1f);
3462                         break;
3463                 case 20:
3464                         reg_w(sd, 0xa4, 0x0c);
3465                         reg_w(sd, 0x23, 0x1b);
3466                         break;
3467                 case 15:
3468                         reg_w(sd, 0xa4, 0x04);
3469                         reg_w(sd, 0x23, 0xff);
3470                         sd->clockdiv = 1;
3471                         break;
3472                 case 10:
3473                         reg_w(sd, 0xa4, 0x04);
3474                         reg_w(sd, 0x23, 0x1f);
3475                         sd->clockdiv = 1;
3476                         break;
3477                 case 5:
3478                         reg_w(sd, 0xa4, 0x04);
3479                         reg_w(sd, 0x23, 0x1b);
3480                         sd->clockdiv = 1;
3481                         break;
3482                 }
3483                 break;
3484         case SEN_OV8610:
3485                 switch (sd->frame_rate) {
3486                 default:        /* 15 fps */
3487 /*              case 15: */
3488                         reg_w(sd, 0xa4, 0x06);
3489                         reg_w(sd, 0x23, 0xff);
3490                         break;
3491                 case 10:
3492                         reg_w(sd, 0xa4, 0x06);
3493                         reg_w(sd, 0x23, 0x1f);
3494                         break;
3495                 case 5:
3496                         reg_w(sd, 0xa4, 0x06);
3497                         reg_w(sd, 0x23, 0x1b);
3498                         break;
3499                 }
3500                 break;
3501         case SEN_OV7670:                /* guesses, based on 7640 */
3502                 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3503                                  (sd->frame_rate == 0) ? 15 : sd->frame_rate);
3504                 reg_w(sd, 0xa4, 0x10);
3505                 switch (sd->frame_rate) {
3506                 case 30:
3507                         reg_w(sd, 0x23, 0xff);
3508                         break;
3509                 case 20:
3510                         reg_w(sd, 0x23, 0x1b);
3511                         break;
3512                 default:
3513 /*              case 15: */
3514                         reg_w(sd, 0x23, 0xff);
3515                         sd->clockdiv = 1;
3516                         break;
3517                 }
3518                 break;
3519         }
3520 }
3521
3522 static void mode_init_ov_sensor_regs(struct sd *sd)
3523 {
3524         struct gspca_dev *gspca_dev;
3525         int qvga, xstart, xend, ystart, yend;
3526         u8 v;
3527
3528         gspca_dev = &sd->gspca_dev;
3529         qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
3530
3531         /******** Mode (VGA/QVGA) and sensor specific regs ********/
3532         switch (sd->sensor) {
3533         case SEN_OV2610:
3534                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3535                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3536                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3537                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3538                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3539                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3540                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3541                 return;
3542         case SEN_OV3610:
3543                 if (qvga) {
3544                         xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
3545                         ystart = (776 - gspca_dev->height) / 2;
3546                 } else {
3547                         xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
3548                         ystart = (1544 - gspca_dev->height) / 2;
3549                 }
3550                 xend = xstart + gspca_dev->width;
3551                 yend = ystart + gspca_dev->height;
3552                 /* Writing to the COMH register resets the other windowing regs
3553                    to their default values, so we must do this first. */
3554                 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
3555                 i2c_w_mask(sd, 0x32,
3556                            (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
3557                            0x3f);
3558                 i2c_w_mask(sd, 0x03,
3559                            (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
3560                            0x0f);
3561                 i2c_w(sd, 0x17, xstart >> 4);
3562                 i2c_w(sd, 0x18, xend >> 4);
3563                 i2c_w(sd, 0x19, ystart >> 3);
3564                 i2c_w(sd, 0x1a, yend >> 3);
3565                 return;
3566         case SEN_OV8610:
3567                 /* For OV8610 qvga means qsvga */
3568                 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
3569                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3570                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3571                 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
3572                 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
3573                 break;
3574         case SEN_OV7610:
3575                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3576                 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3577                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3578                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3579                 break;
3580         case SEN_OV7620:
3581         case SEN_OV7620AE:
3582         case SEN_OV76BE:
3583                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3584                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3585                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3586                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3587                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3588                 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
3589                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3590                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3591                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3592                 if (sd->sensor == SEN_OV76BE)
3593                         i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3594                 break;
3595         case SEN_OV7640:
3596         case SEN_OV7648:
3597                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3598                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3599                 /* Setting this undocumented bit in qvga mode removes a very
3600                    annoying vertical shaking of the image */
3601                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3602                 /* Unknown */
3603                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3604                 /* Allow higher automatic gain (to allow higher framerates) */
3605                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3606                 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
3607                 break;
3608         case SEN_OV7670:
3609                 /* set COM7_FMT_VGA or COM7_FMT_QVGA
3610                  * do we need to set anything else?
3611                  *      HSTART etc are set in set_ov_sensor_window itself */
3612                 i2c_w_mask(sd, OV7670_R12_COM7,
3613                          qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
3614                          OV7670_COM7_FMT_MASK);
3615                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3616                 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_AWB,
3617                                 OV7670_COM8_AWB);
3618                 if (qvga) {             /* QVGA from ov7670.c by
3619                                          * Jonathan Corbet */
3620                         xstart = 164;
3621                         xend = 28;
3622                         ystart = 14;
3623                         yend = 494;
3624                 } else {                /* VGA */
3625                         xstart = 158;
3626                         xend = 14;
3627                         ystart = 10;
3628                         yend = 490;
3629                 }
3630                 /* OV7670 hardware window registers are split across
3631                  * multiple locations */
3632                 i2c_w(sd, OV7670_R17_HSTART, xstart >> 3);
3633                 i2c_w(sd, OV7670_R18_HSTOP, xend >> 3);
3634                 v = i2c_r(sd, OV7670_R32_HREF);
3635                 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
3636                 msleep(10);     /* need to sleep between read and write to
3637                                  * same reg! */
3638                 i2c_w(sd, OV7670_R32_HREF, v);
3639
3640                 i2c_w(sd, OV7670_R19_VSTART, ystart >> 2);
3641                 i2c_w(sd, OV7670_R1A_VSTOP, yend >> 2);
3642                 v = i2c_r(sd, OV7670_R03_VREF);
3643                 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
3644                 msleep(10);     /* need to sleep between read and write to
3645                                  * same reg! */
3646                 i2c_w(sd, OV7670_R03_VREF, v);
3647                 break;
3648         case SEN_OV6620:
3649                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3650                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3651                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3652                 break;
3653         case SEN_OV6630:
3654         case SEN_OV66308AF:
3655                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3656                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3657                 break;
3658         default:
3659                 return;
3660         }
3661
3662         /******** Clock programming ********/
3663         i2c_w(sd, 0x11, sd->clockdiv);
3664 }
3665
3666 static void sethvflip(struct gspca_dev *gspca_dev)
3667 {
3668         struct sd *sd = (struct sd *) gspca_dev;
3669
3670         if (sd->sensor != SEN_OV7670)
3671                 return;
3672         if (sd->gspca_dev.streaming)
3673                 ov51x_stop(sd);
3674         i2c_w_mask(sd, OV7670_R1E_MVFP,
3675                 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val
3676                         | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
3677                 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
3678         if (sd->gspca_dev.streaming)
3679                 ov51x_restart(sd);
3680 }
3681
3682 static void set_ov_sensor_window(struct sd *sd)
3683 {
3684         struct gspca_dev *gspca_dev;
3685         int qvga, crop;
3686         int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
3687
3688         /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
3689         if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610 ||
3690             sd->sensor == SEN_OV7670) {
3691                 mode_init_ov_sensor_regs(sd);
3692                 return;
3693         }
3694         gspca_dev = &sd->gspca_dev;
3695         qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
3696         crop = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 2;
3697
3698         /* The different sensor ICs handle setting up of window differently.
3699          * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
3700         switch (sd->sensor) {
3701         case SEN_OV8610:
3702                 hwsbase = 0x1e;
3703                 hwebase = 0x1e;
3704                 vwsbase = 0x02;
3705                 vwebase = 0x02;
3706                 break;
3707         case SEN_OV7610:
3708         case SEN_OV76BE:
3709                 hwsbase = 0x38;
3710                 hwebase = 0x3a;
3711                 vwsbase = vwebase = 0x05;
3712                 break;
3713         case SEN_OV6620:
3714         case SEN_OV6630:
3715         case SEN_OV66308AF:
3716                 hwsbase = 0x38;
3717                 hwebase = 0x3a;
3718                 vwsbase = 0x05;
3719                 vwebase = 0x06;
3720                 if (sd->sensor == SEN_OV66308AF && qvga)
3721                         /* HDG: this fixes U and V getting swapped */
3722                         hwsbase++;
3723                 if (crop) {
3724                         hwsbase += 8;
3725                         hwebase += 8;
3726                         vwsbase += 11;
3727                         vwebase += 11;
3728                 }
3729                 break;
3730         case SEN_OV7620:
3731         case SEN_OV7620AE:
3732                 hwsbase = 0x2f;         /* From 7620.SET (spec is wrong) */
3733                 hwebase = 0x2f;
3734                 vwsbase = vwebase = 0x05;
3735                 break;
3736         case SEN_OV7640:
3737         case SEN_OV7648:
3738                 hwsbase = 0x1a;
3739                 hwebase = 0x1a;
3740                 vwsbase = vwebase = 0x03;
3741                 break;
3742         default:
3743                 return;
3744         }
3745
3746         switch (sd->sensor) {
3747         case SEN_OV6620:
3748         case SEN_OV6630:
3749         case SEN_OV66308AF:
3750                 if (qvga) {             /* QCIF */
3751                         hwscale = 0;
3752                         vwscale = 0;
3753                 } else {                /* CIF */
3754                         hwscale = 1;
3755                         vwscale = 1;    /* The datasheet says 0;
3756                                          * it's wrong */
3757                 }
3758                 break;
3759         case SEN_OV8610:
3760                 if (qvga) {             /* QSVGA */
3761                         hwscale = 1;
3762                         vwscale = 1;
3763                 } else {                /* SVGA */
3764                         hwscale = 2;
3765                         vwscale = 2;
3766                 }
3767                 break;
3768         default:                        /* SEN_OV7xx0 */
3769                 if (qvga) {             /* QVGA */
3770                         hwscale = 1;
3771                         vwscale = 0;
3772                 } else {                /* VGA */
3773                         hwscale = 2;
3774                         vwscale = 1;
3775                 }
3776         }
3777
3778         mode_init_ov_sensor_regs(sd);
3779
3780         i2c_w(sd, 0x17, hwsbase);
3781         i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
3782         i2c_w(sd, 0x19, vwsbase);
3783         i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
3784 }
3785
3786 /* -- start the camera -- */
3787 static int sd_start(struct gspca_dev *gspca_dev)
3788 {
3789         struct sd *sd = (struct sd *) gspca_dev;
3790
3791         /* Default for most bridges, allow bridge_mode_init_regs to override */
3792         sd->sensor_width = sd->gspca_dev.width;
3793         sd->sensor_height = sd->gspca_dev.height;
3794
3795         switch (sd->bridge) {
3796         case BRIDGE_OV511:
3797         case BRIDGE_OV511PLUS:
3798                 ov511_mode_init_regs(sd);
3799                 break;
3800         case BRIDGE_OV518:
3801         case BRIDGE_OV518PLUS:
3802                 ov518_mode_init_regs(sd);
3803                 break;
3804         case BRIDGE_OV519:
3805                 ov519_mode_init_regs(sd);
3806                 break;
3807         /* case BRIDGE_OVFX2: nothing to do */
3808         case BRIDGE_W9968CF:
3809                 w9968cf_mode_init_regs(sd);
3810                 break;
3811         }
3812
3813         set_ov_sensor_window(sd);
3814
3815         setcontrast(gspca_dev);
3816         setbrightness(gspca_dev);
3817         setcolors(gspca_dev);
3818         sethvflip(gspca_dev);
3819         setautobright(gspca_dev);
3820         setfreq_i(sd);
3821
3822         /* Force clear snapshot state in case the snapshot button was
3823            pressed while we weren't streaming */
3824         sd->snapshot_needs_reset = 1;
3825         sd_reset_snapshot(gspca_dev);
3826
3827         sd->first_frame = 3;
3828
3829         ov51x_restart(sd);
3830         ov51x_led_control(sd, 1);
3831         return gspca_dev->usb_err;
3832 }
3833
3834 static void sd_stopN(struct gspca_dev *gspca_dev)
3835 {
3836         struct sd *sd = (struct sd *) gspca_dev;
3837
3838         ov51x_stop(sd);
3839         ov51x_led_control(sd, 0);
3840 }
3841
3842 static void sd_stop0(struct gspca_dev *gspca_dev)
3843 {
3844         struct sd *sd = (struct sd *) gspca_dev;
3845
3846         if (!sd->gspca_dev.present)
3847                 return;
3848         if (sd->bridge == BRIDGE_W9968CF)
3849                 w9968cf_stop0(sd);
3850
3851 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3852         /* If the last button state is pressed, release it now! */
3853         if (sd->snapshot_pressed) {
3854                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
3855                 input_sync(gspca_dev->input_dev);
3856                 sd->snapshot_pressed = 0;
3857         }
3858 #endif
3859 }
3860
3861 static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
3862 {
3863         struct sd *sd = (struct sd *) gspca_dev;
3864
3865         if (sd->snapshot_pressed != state) {
3866 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3867                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
3868                 input_sync(gspca_dev->input_dev);
3869 #endif
3870                 if (state)
3871                         sd->snapshot_needs_reset = 1;
3872
3873                 sd->snapshot_pressed = state;
3874         } else {
3875                 /* On the ov511 / ov519 we need to reset the button state
3876                    multiple times, as resetting does not work as long as the
3877                    button stays pressed */
3878                 switch (sd->bridge) {
3879                 case BRIDGE_OV511:
3880                 case BRIDGE_OV511PLUS:
3881                 case BRIDGE_OV519:
3882                         if (state)
3883                                 sd->snapshot_needs_reset = 1;
3884                         break;
3885                 }
3886         }
3887 }
3888
3889 static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
3890                         u8 *in,                 /* isoc packet */
3891                         int len)                /* iso packet length */
3892 {
3893         struct sd *sd = (struct sd *) gspca_dev;
3894
3895         /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3896          * byte non-zero. The EOF packet has image width/height in the
3897          * 10th and 11th bytes. The 9th byte is given as follows:
3898          *
3899          * bit 7: EOF
3900          *     6: compression enabled
3901          *     5: 422/420/400 modes
3902          *     4: 422/420/400 modes
3903          *     3: 1
3904          *     2: snapshot button on
3905          *     1: snapshot frame
3906          *     0: even/odd field
3907          */
3908         if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
3909             (in[8] & 0x08)) {
3910                 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
3911                 if (in[8] & 0x80) {
3912                         /* Frame end */
3913                         if ((in[9] + 1) * 8 != gspca_dev->width ||
3914                             (in[10] + 1) * 8 != gspca_dev->height) {
3915                                 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
3916                                         " requested: %dx%d\n",
3917                                         (in[9] + 1) * 8, (in[10] + 1) * 8,
3918                                         gspca_dev->width, gspca_dev->height);
3919                                 gspca_dev->last_packet_type = DISCARD_PACKET;
3920                                 return;
3921                         }
3922                         /* Add 11 byte footer to frame, might be usefull */
3923                         gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
3924                         return;
3925                 } else {
3926                         /* Frame start */
3927                         gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
3928                         sd->packet_nr = 0;
3929                 }
3930         }
3931
3932         /* Ignore the packet number */
3933         len--;
3934
3935         /* intermediate packet */
3936         gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
3937 }
3938
3939 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
3940                         u8 *data,                       /* isoc packet */
3941                         int len)                        /* iso packet length */
3942 {
3943         struct sd *sd = (struct sd *) gspca_dev;
3944
3945         /* A false positive here is likely, until OVT gives me
3946          * the definitive SOF/EOF format */
3947         if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
3948                 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
3949                 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
3950                 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
3951                 sd->packet_nr = 0;
3952         }
3953
3954         if (gspca_dev->last_packet_type == DISCARD_PACKET)
3955                 return;
3956
3957         /* Does this device use packet numbers ? */
3958         if (len & 7) {
3959                 len--;
3960                 if (sd->packet_nr == data[len])
3961                         sd->packet_nr++;
3962                 /* The last few packets of the frame (which are all 0's
3963                    except that they may contain part of the footer), are
3964                    numbered 0 */
3965                 else if (sd->packet_nr == 0 || data[len]) {
3966                         PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
3967                                 (int)data[len], (int)sd->packet_nr);
3968                         gspca_dev->last_packet_type = DISCARD_PACKET;
3969                         return;
3970                 }
3971         }
3972
3973         /* intermediate packet */
3974         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
3975 }
3976
3977 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
3978                         u8 *data,                       /* isoc packet */
3979                         int len)                        /* iso packet length */
3980 {
3981         /* Header of ov519 is 16 bytes:
3982          *     Byte     Value      Description
3983          *      0       0xff    magic
3984          *      1       0xff    magic
3985          *      2       0xff    magic
3986          *      3       0xXX    0x50 = SOF, 0x51 = EOF
3987          *      9       0xXX    0x01 initial frame without data,
3988          *                      0x00 standard frame with image
3989          *      14      Lo      in EOF: length of image data / 8
3990          *      15      Hi
3991          */
3992
3993         if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
3994                 switch (data[3]) {
3995                 case 0x50:              /* start of frame */
3996                         /* Don't check the button state here, as the state
3997                            usually (always ?) changes at EOF and checking it
3998                            here leads to unnecessary snapshot state resets. */
3999 #define HDRSZ 16
4000                         data += HDRSZ;
4001                         len -= HDRSZ;
4002 #undef HDRSZ
4003                         if (data[0] == 0xff || data[1] == 0xd8)
4004                                 gspca_frame_add(gspca_dev, FIRST_PACKET,
4005                                                 data, len);
4006                         else
4007                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4008                         return;
4009                 case 0x51:              /* end of frame */
4010                         ov51x_handle_button(gspca_dev, data[11] & 1);
4011                         if (data[9] != 0)
4012                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4013                         gspca_frame_add(gspca_dev, LAST_PACKET,
4014                                         NULL, 0);
4015                         return;
4016                 }
4017         }
4018
4019         /* intermediate packet */
4020         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4021 }
4022
4023 static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
4024                         u8 *data,                       /* isoc packet */
4025                         int len)                        /* iso packet length */
4026 {
4027         struct sd *sd = (struct sd *) gspca_dev;
4028
4029         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4030
4031         /* A short read signals EOF */
4032         if (len < OVFX2_BULK_SIZE) {
4033                 /* If the frame is short, and it is one of the first ones
4034                    the sensor and bridge are still syncing, so drop it. */
4035                 if (sd->first_frame) {
4036                         sd->first_frame--;
4037                         if (gspca_dev->image_len <
4038                                   sd->gspca_dev.width * sd->gspca_dev.height)
4039                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4040                 }
4041                 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4042                 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4043         }
4044 }
4045
4046 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4047                         u8 *data,                       /* isoc packet */
4048                         int len)                        /* iso packet length */
4049 {
4050         struct sd *sd = (struct sd *) gspca_dev;
4051
4052         switch (sd->bridge) {
4053         case BRIDGE_OV511:
4054         case BRIDGE_OV511PLUS:
4055                 ov511_pkt_scan(gspca_dev, data, len);
4056                 break;
4057         case BRIDGE_OV518:
4058         case BRIDGE_OV518PLUS:
4059                 ov518_pkt_scan(gspca_dev, data, len);
4060                 break;
4061         case BRIDGE_OV519:
4062                 ov519_pkt_scan(gspca_dev, data, len);
4063                 break;
4064         case BRIDGE_OVFX2:
4065                 ovfx2_pkt_scan(gspca_dev, data, len);
4066                 break;
4067         case BRIDGE_W9968CF:
4068                 w9968cf_pkt_scan(gspca_dev, data, len);
4069                 break;
4070         }
4071 }
4072
4073 /* -- management routines -- */
4074
4075 static void setbrightness(struct gspca_dev *gspca_dev)
4076 {
4077         struct sd *sd = (struct sd *) gspca_dev;
4078         int val;
4079
4080         val = sd->ctrls[BRIGHTNESS].val;
4081         switch (sd->sensor) {
4082         case SEN_OV8610:
4083         case SEN_OV7610:
4084         case SEN_OV76BE:
4085         case SEN_OV6620:
4086         case SEN_OV6630:
4087         case SEN_OV66308AF:
4088         case SEN_OV7640:
4089         case SEN_OV7648:
4090                 i2c_w(sd, OV7610_REG_BRT, val);
4091                 break;
4092         case SEN_OV7620:
4093         case SEN_OV7620AE:
4094                 /* 7620 doesn't like manual changes when in auto mode */
4095                 if (!sd->ctrls[AUTOBRIGHT].val)
4096                         i2c_w(sd, OV7610_REG_BRT, val);
4097                 break;
4098         case SEN_OV7670:
4099 /*win trace
4100  *              i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_AEC); */
4101                 i2c_w(sd, OV7670_R55_BRIGHT, ov7670_abs_to_sm(val));
4102                 break;
4103         }
4104 }
4105
4106 static void setcontrast(struct gspca_dev *gspca_dev)
4107 {
4108         struct sd *sd = (struct sd *) gspca_dev;
4109         int val;
4110
4111         val = sd->ctrls[CONTRAST].val;
4112         switch (sd->sensor) {
4113         case SEN_OV7610:
4114         case SEN_OV6620:
4115                 i2c_w(sd, OV7610_REG_CNT, val);
4116                 break;
4117         case SEN_OV6630:
4118         case SEN_OV66308AF:
4119                 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
4120                 break;
4121         case SEN_OV8610: {
4122                 static const u8 ctab[] = {
4123                         0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4124                 };
4125
4126                 /* Use Y gamma control instead. Bit 0 enables it. */
4127                 i2c_w(sd, 0x64, ctab[val >> 5]);
4128                 break;
4129             }
4130         case SEN_OV7620:
4131         case SEN_OV7620AE: {
4132                 static const u8 ctab[] = {
4133                         0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4134                         0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4135                 };
4136
4137                 /* Use Y gamma control instead. Bit 0 enables it. */
4138                 i2c_w(sd, 0x64, ctab[val >> 4]);
4139                 break;
4140             }
4141         case SEN_OV7670:
4142                 /* check that this isn't just the same as ov7610 */
4143                 i2c_w(sd, OV7670_R56_CONTRAS, val >> 1);
4144                 break;
4145         }
4146 }
4147
4148 static void setcolors(struct gspca_dev *gspca_dev)
4149 {
4150         struct sd *sd = (struct sd *) gspca_dev;
4151         int val;
4152
4153         val = sd->ctrls[COLORS].val;
4154         switch (sd->sensor) {
4155         case SEN_OV8610:
4156         case SEN_OV7610:
4157         case SEN_OV76BE:
4158         case SEN_OV6620:
4159         case SEN_OV6630:
4160         case SEN_OV66308AF:
4161                 i2c_w(sd, OV7610_REG_SAT, val);
4162                 break;
4163         case SEN_OV7620:
4164         case SEN_OV7620AE:
4165                 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4166 /*              rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4167                 if (rc < 0)
4168                         goto out; */
4169                 i2c_w(sd, OV7610_REG_SAT, val);
4170                 break;
4171         case SEN_OV7640:
4172         case SEN_OV7648:
4173                 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4174                 break;
4175         case SEN_OV7670:
4176                 /* supported later once I work out how to do it
4177                  * transparently fail now! */
4178                 /* set REG_COM13 values for UV sat auto mode */
4179                 break;
4180         }
4181 }
4182
4183 static void setautobright(struct gspca_dev *gspca_dev)
4184 {
4185         struct sd *sd = (struct sd *) gspca_dev;
4186
4187         if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7648 ||
4188             sd->sensor == SEN_OV7670 ||
4189             sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4190                 return;
4191
4192         i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);
4193 }
4194
4195 static void setfreq_i(struct sd *sd)
4196 {
4197         if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4198                 return;
4199
4200         if (sd->sensor == SEN_OV7670) {
4201                 switch (sd->ctrls[FREQ].val) {
4202                 case 0: /* Banding filter disabled */
4203                         i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT);
4204                         break;
4205                 case 1: /* 50 hz */
4206                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4207                                    OV7670_COM8_BFILT);
4208                         i2c_w_mask(sd, OV7670_R3B_COM11, 0x08, 0x18);
4209                         break;
4210                 case 2: /* 60 hz */
4211                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4212                                    OV7670_COM8_BFILT);
4213                         i2c_w_mask(sd, OV7670_R3B_COM11, 0x00, 0x18);
4214                         break;
4215                 case 3: /* Auto hz - ov7670 only */
4216                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4217                                    OV7670_COM8_BFILT);
4218                         i2c_w_mask(sd, OV7670_R3B_COM11, OV7670_COM11_HZAUTO,
4219                                    0x18);
4220                         break;
4221                 }
4222         } else {
4223                 switch (sd->ctrls[FREQ].val) {
4224                 case 0: /* Banding filter disabled */
4225                         i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4226                         i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4227                         break;
4228                 case 1: /* 50 hz (filter on and framerate adj) */
4229                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4230                         i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4231                         /* 20 fps -> 16.667 fps */
4232                         if (sd->sensor == SEN_OV6620 ||
4233                             sd->sensor == SEN_OV6630 ||
4234                             sd->sensor == SEN_OV66308AF)
4235                                 i2c_w(sd, 0x2b, 0x5e);
4236                         else
4237                                 i2c_w(sd, 0x2b, 0xac);
4238                         break;
4239                 case 2: /* 60 hz (filter on, ...) */
4240                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4241                         if (sd->sensor == SEN_OV6620 ||
4242                             sd->sensor == SEN_OV6630 ||
4243                             sd->sensor == SEN_OV66308AF) {
4244                                 /* 20 fps -> 15 fps */
4245                                 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4246                                 i2c_w(sd, 0x2b, 0xa8);
4247                         } else {
4248                                 /* no framerate adj. */
4249                                 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4250                         }
4251                         break;
4252                 }
4253         }
4254 }
4255 static void setfreq(struct gspca_dev *gspca_dev)
4256 {
4257         struct sd *sd = (struct sd *) gspca_dev;
4258
4259         setfreq_i(sd);
4260
4261         /* Ugly but necessary */
4262         if (sd->bridge == BRIDGE_W9968CF)
4263                 w9968cf_set_crop_window(sd);
4264 }
4265
4266 static int sd_querymenu(struct gspca_dev *gspca_dev,
4267                         struct v4l2_querymenu *menu)
4268 {
4269         struct sd *sd = (struct sd *) gspca_dev;
4270
4271         switch (menu->id) {
4272         case V4L2_CID_POWER_LINE_FREQUENCY:
4273                 switch (menu->index) {
4274                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4275                         strcpy((char *) menu->name, "NoFliker");
4276                         return 0;
4277                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4278                         strcpy((char *) menu->name, "50 Hz");
4279                         return 0;
4280                 case 2:         /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4281                         strcpy((char *) menu->name, "60 Hz");
4282                         return 0;
4283                 case 3:
4284                         if (sd->sensor != SEN_OV7670)
4285                                 return -EINVAL;
4286
4287                         strcpy((char *) menu->name, "Automatic");
4288                         return 0;
4289                 }
4290                 break;
4291         }
4292         return -EINVAL;
4293 }
4294
4295 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4296                         struct v4l2_jpegcompression *jcomp)
4297 {
4298         struct sd *sd = (struct sd *) gspca_dev;
4299
4300         if (sd->bridge != BRIDGE_W9968CF)
4301                 return -EINVAL;
4302
4303         memset(jcomp, 0, sizeof *jcomp);
4304         jcomp->quality = sd->quality;
4305         jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4306                               V4L2_JPEG_MARKER_DRI;
4307         return 0;
4308 }
4309
4310 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4311                         struct v4l2_jpegcompression *jcomp)
4312 {
4313         struct sd *sd = (struct sd *) gspca_dev;
4314
4315         if (sd->bridge != BRIDGE_W9968CF)
4316                 return -EINVAL;
4317
4318         if (gspca_dev->streaming)
4319                 return -EBUSY;
4320
4321         if (jcomp->quality < QUALITY_MIN)
4322                 sd->quality = QUALITY_MIN;
4323         else if (jcomp->quality > QUALITY_MAX)
4324                 sd->quality = QUALITY_MAX;
4325         else
4326                 sd->quality = jcomp->quality;
4327
4328         /* Return resulting jcomp params to app */
4329         sd_get_jcomp(gspca_dev, jcomp);
4330
4331         return 0;
4332 }
4333
4334 /* sub-driver description */
4335 static const struct sd_desc sd_desc = {
4336         .name = MODULE_NAME,
4337         .ctrls = sd_ctrls,
4338         .nctrls = ARRAY_SIZE(sd_ctrls),
4339         .config = sd_config,
4340         .init = sd_init,
4341         .start = sd_start,
4342         .stopN = sd_stopN,
4343         .stop0 = sd_stop0,
4344         .pkt_scan = sd_pkt_scan,
4345         .dq_callback = sd_reset_snapshot,
4346         .querymenu = sd_querymenu,
4347         .get_jcomp = sd_get_jcomp,
4348         .set_jcomp = sd_set_jcomp,
4349 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
4350         .other_input = 1,
4351 #endif
4352 };
4353
4354 /* -- module initialisation -- */
4355 static const __devinitdata struct usb_device_id device_table[] = {
4356         {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
4357         {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
4358         {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
4359         {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
4360         {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
4361         {USB_DEVICE(0x041e, 0x4064),
4362                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4363         {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
4364         {USB_DEVICE(0x041e, 0x4068),
4365                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4366         {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
4367         {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
4368         {USB_DEVICE(0x054c, 0x0155),
4369                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4370         {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
4371         {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
4372         {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
4373         {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
4374         {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
4375         {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
4376         {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
4377         {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
4378         {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
4379         {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
4380         {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
4381         {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
4382         {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
4383         {USB_DEVICE(0x8020, 0xef04), .driver_info = BRIDGE_OVFX2 },
4384         {}
4385 };
4386
4387 MODULE_DEVICE_TABLE(usb, device_table);
4388
4389 /* -- device connect -- */
4390 static int sd_probe(struct usb_interface *intf,
4391                         const struct usb_device_id *id)
4392 {
4393         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4394                                 THIS_MODULE);
4395 }
4396
4397 static struct usb_driver sd_driver = {
4398         .name = MODULE_NAME,
4399         .id_table = device_table,
4400         .probe = sd_probe,
4401         .disconnect = gspca_disconnect,
4402 #ifdef CONFIG_PM
4403         .suspend = gspca_suspend,
4404         .resume = gspca_resume,
4405 #endif
4406 };
4407
4408 /* -- module insert / remove -- */
4409 static int __init sd_mod_init(void)
4410 {
4411         return usb_register(&sd_driver);
4412 }
4413 static void __exit sd_mod_exit(void)
4414 {
4415         usb_deregister(&sd_driver);
4416 }
4417
4418 module_init(sd_mod_init);
4419 module_exit(sd_mod_exit);
4420
4421 module_param(frame_rate, int, 0644);
4422 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");