]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/gspca/ov519.c
7d6237f18ba06950edc315383ae80074b1c3fc6c
[linux-2.6-omap-h63xx.git] / drivers / media / video / gspca / ov519.c
1 /**
2  * OV519 driver
3  *
4  * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5  *
6  * (This module is adapted from the ov51x-jpeg package)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23 #define MODULE_NAME "ov519"
24
25 #include "gspca.h"
26
27 #define DRIVER_VERSION_NUMBER   KERNEL_VERSION(2, 1, 0)
28 static const char version[] = "2.1.0";
29
30 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
31 MODULE_DESCRIPTION("OV519 USB Camera Driver");
32 MODULE_LICENSE("GPL");
33
34 /* global parameters */
35 static int frame_rate;
36
37 /* Number of times to retry a failed I2C transaction. Increase this if you
38  * are getting "Failed to read sensor ID..." */
39 static int i2c_detect_tries = 10;
40
41 /* ov519 device descriptor */
42 struct sd {
43         struct gspca_dev gspca_dev;             /* !! must be the first item */
44
45         /* Determined by sensor type */
46         short maxwidth;
47         short maxheight;
48
49         unsigned char primary_i2c_slave;        /* I2C write id of sensor */
50
51         unsigned char brightness;
52         unsigned char contrast;
53         unsigned char colors;
54
55         char compress;          /* Should the next frame be compressed? */
56         char compress_inited;   /* Are compression params uploaded? */
57         char stopped;           /* Streaming is temporarily paused */
58
59         char frame_rate;        /* current Framerate (OV519 only) */
60         char clockdiv;          /* clockdiv override for OV519 only */
61
62         char sensor;            /* Type of image sensor chip (SEN_*) */
63 #define SEN_UNKNOWN 0
64 #define SEN_OV6620 1
65 #define SEN_OV6630 2
66 #define SEN_OV7610 3
67 #define SEN_OV7620 4
68 #define SEN_OV7630 5
69 #define SEN_OV7640 6
70 #define SEN_OV7670 7
71 #define SEN_OV76BE 8
72 #define SEN_OV8610 9
73
74 };
75
76 /* V4L2 controls supported by the driver */
77 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
83
84 static struct ctrl sd_ctrls[] = {
85 #define SD_BRIGHTNESS 0
86         {
87             {
88                 .id      = V4L2_CID_BRIGHTNESS,
89                 .type    = V4L2_CTRL_TYPE_INTEGER,
90                 .name    = "Brightness",
91                 .minimum = 0,
92                 .maximum = 255,
93                 .step    = 1,
94                 .default_value = 127,
95             },
96             .set = sd_setbrightness,
97             .get = sd_getbrightness,
98         },
99 #define SD_CONTRAST 1
100         {
101             {
102                 .id      = V4L2_CID_CONTRAST,
103                 .type    = V4L2_CTRL_TYPE_INTEGER,
104                 .name    = "Contrast",
105                 .minimum = 0,
106                 .maximum = 255,
107                 .step    = 1,
108                 .default_value = 127,
109             },
110             .set = sd_setcontrast,
111             .get = sd_getcontrast,
112         },
113 #define SD_COLOR 2
114         {
115             {
116                 .id      = V4L2_CID_SATURATION,
117                 .type    = V4L2_CTRL_TYPE_INTEGER,
118                 .name    = "Saturation",
119                 .minimum = 0,
120                 .maximum = 255,
121                 .step    = 1,
122                 .default_value = 127,
123             },
124             .set = sd_setcolors,
125             .get = sd_getcolors,
126         },
127 };
128
129 static struct cam_mode vga_mode[] = {
130         {V4L2_PIX_FMT_JPEG, 320, 240},
131         {V4L2_PIX_FMT_JPEG, 640, 480},
132 };
133 static struct cam_mode sif_mode[] = {
134         {V4L2_PIX_FMT_JPEG, 176, 144},
135         {V4L2_PIX_FMT_JPEG, 352, 288},
136 };
137
138 /* OV519 Camera interface register numbers */
139 #define OV519_CAM_H_SIZE                0x10
140 #define OV519_CAM_V_SIZE                0x11
141 #define OV519_CAM_X_OFFSETL             0x12
142 #define OV519_CAM_X_OFFSETH             0x13
143 #define OV519_CAM_Y_OFFSETL             0x14
144 #define OV519_CAM_Y_OFFSETH             0x15
145 #define OV519_CAM_DIVIDER               0x16
146 #define OV519_CAM_DFR                   0x20
147 #define OV519_CAM_FORMAT                0x25
148
149 /* OV519 System Controller register numbers */
150 #define OV519_SYS_RESET1 0x51
151 #define OV519_SYS_EN_CLK1 0x54
152
153 #define OV519_GPIO_DATA_OUT0            0x71
154 #define OV519_GPIO_IO_CTRL0             0x72
155
156 #define OV511_ENDPOINT_ADDRESS  1       /* Isoc endpoint number */
157
158 /* I2C registers */
159 #define R51x_I2C_W_SID          0x41
160 #define R51x_I2C_SADDR_3        0x42
161 #define R51x_I2C_SADDR_2        0x43
162 #define R51x_I2C_R_SID          0x44
163 #define R51x_I2C_DATA           0x45
164 #define R518_I2C_CTL            0x47    /* OV518(+) only */
165
166 /* I2C ADDRESSES */
167 #define OV7xx0_SID   0x42
168 #define OV8xx0_SID   0xa0
169 #define OV6xx0_SID   0xc0
170
171 /* OV7610 registers */
172 #define OV7610_REG_GAIN         0x00    /* gain setting (5:0) */
173 #define OV7610_REG_SAT          0x03    /* saturation */
174 #define OV8610_REG_HUE          0x04    /* 04 reserved */
175 #define OV7610_REG_CNT          0x05    /* Y contrast */
176 #define OV7610_REG_BRT          0x06    /* Y brightness */
177 #define OV7610_REG_COM_C        0x14    /* misc common regs */
178 #define OV7610_REG_ID_HIGH      0x1c    /* manufacturer ID MSB */
179 #define OV7610_REG_ID_LOW       0x1d    /* manufacturer ID LSB */
180 #define OV7610_REG_COM_I        0x29    /* misc settings */
181
182 /* OV7670 registers */
183 #define OV7670_REG_GAIN        0x00    /* Gain lower 8 bits (rest in vref) */
184 #define OV7670_REG_BLUE        0x01    /* blue gain */
185 #define OV7670_REG_RED         0x02    /* red gain */
186 #define OV7670_REG_VREF        0x03    /* Pieces of GAIN, VSTART, VSTOP */
187 #define OV7670_REG_COM1        0x04    /* Control 1 */
188 #define OV7670_REG_AECHH       0x07    /* AEC MS 5 bits */
189 #define OV7670_REG_COM3        0x0c    /* Control 3 */
190 #define OV7670_REG_COM4        0x0d    /* Control 4 */
191 #define OV7670_REG_COM5        0x0e    /* All "reserved" */
192 #define OV7670_REG_COM6        0x0f    /* Control 6 */
193 #define OV7670_REG_AECH        0x10    /* More bits of AEC value */
194 #define OV7670_REG_CLKRC       0x11    /* Clock control */
195 #define OV7670_REG_COM7        0x12    /* Control 7 */
196 #define   OV7670_COM7_FMT_VGA    0x00
197 #define   OV7670_COM7_YUV        0x00    /* YUV */
198 #define   OV7670_COM7_FMT_QVGA   0x10    /* QVGA format */
199 #define   OV7670_COM7_FMT_MASK   0x38
200 #define   OV7670_COM7_RESET      0x80    /* Register reset */
201 #define OV7670_REG_COM8        0x13    /* Control 8 */
202 #define   OV7670_COM8_AEC        0x01    /* Auto exposure enable */
203 #define   OV7670_COM8_AWB        0x02    /* White balance enable */
204 #define   OV7670_COM8_AGC        0x04    /* Auto gain enable */
205 #define   OV7670_COM8_BFILT      0x20    /* Band filter enable */
206 #define   OV7670_COM8_AECSTEP    0x40    /* Unlimited AEC step size */
207 #define   OV7670_COM8_FASTAEC    0x80    /* Enable fast AGC/AEC */
208 #define OV7670_REG_COM9        0x14    /* Control 9  - gain ceiling */
209 #define OV7670_REG_COM10       0x15    /* Control 10 */
210 #define OV7670_REG_HSTART      0x17    /* Horiz start high bits */
211 #define OV7670_REG_HSTOP       0x18    /* Horiz stop high bits */
212 #define OV7670_REG_VSTART      0x19    /* Vert start high bits */
213 #define OV7670_REG_VSTOP       0x1a    /* Vert stop high bits */
214 #define OV7670_REG_MVFP        0x1e    /* Mirror / vflip */
215 #define   OV7670_MVFP_MIRROR     0x20    /* Mirror image */
216 #define OV7670_REG_AEW         0x24    /* AGC upper limit */
217 #define OV7670_REG_AEB         0x25    /* AGC lower limit */
218 #define OV7670_REG_VPT         0x26    /* AGC/AEC fast mode op region */
219 #define OV7670_REG_HREF        0x32    /* HREF pieces */
220 #define OV7670_REG_TSLB        0x3a    /* lots of stuff */
221 #define OV7670_REG_COM11       0x3b    /* Control 11 */
222 #define   OV7670_COM11_EXP       0x02
223 #define   OV7670_COM11_HZAUTO    0x10    /* Auto detect 50/60 Hz */
224 #define OV7670_REG_COM12       0x3c    /* Control 12 */
225 #define OV7670_REG_COM13       0x3d    /* Control 13 */
226 #define   OV7670_COM13_GAMMA     0x80    /* Gamma enable */
227 #define   OV7670_COM13_UVSAT     0x40    /* UV saturation auto adjustment */
228 #define OV7670_REG_COM14       0x3e    /* Control 14 */
229 #define OV7670_REG_EDGE        0x3f    /* Edge enhancement factor */
230 #define OV7670_REG_COM15       0x40    /* Control 15 */
231 #define   OV7670_COM15_R00FF     0xc0    /*            00 to FF */
232 #define OV7670_REG_COM16       0x41    /* Control 16 */
233 #define   OV7670_COM16_AWBGAIN   0x08    /* AWB gain enable */
234 #define OV7670_REG_BRIGHT      0x55    /* Brightness */
235 #define OV7670_REG_CONTRAS     0x56    /* Contrast control */
236 #define OV7670_REG_GFIX        0x69    /* Fix gain control */
237 #define OV7670_REG_RGB444      0x8c    /* RGB 444 control */
238 #define OV7670_REG_HAECC1      0x9f    /* Hist AEC/AGC control 1 */
239 #define OV7670_REG_HAECC2      0xa0    /* Hist AEC/AGC control 2 */
240 #define OV7670_REG_BD50MAX     0xa5    /* 50hz banding step limit */
241 #define OV7670_REG_HAECC3      0xa6    /* Hist AEC/AGC control 3 */
242 #define OV7670_REG_HAECC4      0xa7    /* Hist AEC/AGC control 4 */
243 #define OV7670_REG_HAECC5      0xa8    /* Hist AEC/AGC control 5 */
244 #define OV7670_REG_HAECC6      0xa9    /* Hist AEC/AGC control 6 */
245 #define OV7670_REG_HAECC7      0xaa    /* Hist AEC/AGC control 7 */
246 #define OV7670_REG_BD60MAX     0xab    /* 60hz banding step limit */
247
248 struct ovsensor_window {
249         short x;
250         short y;
251         short width;
252         short height;
253 /*      int format; */
254         short quarter;          /* Scale width and height down 2x */
255         short clockdiv;         /* Clock divisor setting */
256 };
257
258 static unsigned char ov7670_abs_to_sm(unsigned char v)
259 {
260         if (v > 127)
261                 return v & 0x7f;
262         return (128 - v) | 0x80;
263 }
264
265 /* Write a OV519 register */
266 static int reg_w(struct sd *sd, __u16 index, __u8 value)
267 {
268         int ret;
269         __u8 buf[4];
270
271         buf[0] = value;
272         ret = usb_control_msg(sd->gspca_dev.dev,
273                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
274                         1,                      /* REQ_IO (ov518/519) */
275                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
276                         0, index,
277                         &buf[0], 1, 500);
278         if (ret < 0)
279                 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
280         return ret;
281 }
282
283 /* Read from a OV519 register */
284 /* returns: negative is error, pos or zero is data */
285 static int reg_r(struct sd *sd, __u16 index)
286 {
287         int ret;
288         __u8 buf[4];
289
290         ret = usb_control_msg(sd->gspca_dev.dev,
291                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
292                         1,                      /* REQ_IO */
293                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
294                         0, index, &buf[0], 1, 500);
295
296         if (ret >= 0)
297                 ret = buf[0];
298         else
299                 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
300         return ret;
301 }
302
303 /* Read 8 values from a OV519 register */
304 static int reg_r8(struct sd *sd,
305                 __u16 index)
306 {
307         int ret;
308         __u8 buf[8];
309
310         ret = usb_control_msg(sd->gspca_dev.dev,
311                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
312                         1,                      /* REQ_IO */
313                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
314                         0, index, &buf[0], 8, 500);
315
316         if (ret >= 0)
317                 ret = buf[0];
318         else
319                 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
320         return ret;
321 }
322
323 /*
324  * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
325  * the same position as 1's in "mask" are cleared and set to "value". Bits
326  * that are in the same position as 0's in "mask" are preserved, regardless
327  * of their respective state in "value".
328  */
329 static int reg_w_mask(struct sd *sd,
330                         __u16 index,
331                         __u8 value,
332                         __u8 mask)
333 {
334         int ret;
335         __u8 oldval;
336
337         if (mask != 0xff) {
338                 value &= mask;                  /* Enforce mask on value */
339                 ret = reg_r(sd, index);
340                 if (ret < 0)
341                         return ret;
342
343                 oldval = ret & ~mask;           /* Clear the masked bits */
344                 value |= oldval;                /* Set the desired bits */
345         }
346         return reg_w(sd, index, value);
347 }
348
349 /*
350  * The OV518 I2C I/O procedure is different, hence, this function.
351  * This is normally only called from i2c_w(). Note that this function
352  * always succeeds regardless of whether the sensor is present and working.
353  */
354 static int i2c_w(struct sd *sd,
355                 __u8 reg,
356                 __u8 value)
357 {
358         int rc;
359
360         PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
361
362         /* Select camera register */
363         rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
364         if (rc < 0)
365                 return rc;
366
367         /* Write "value" to I2C data port of OV511 */
368         rc = reg_w(sd, R51x_I2C_DATA, value);
369         if (rc < 0)
370                 return rc;
371
372         /* Initiate 3-byte write cycle */
373         rc = reg_w(sd, R518_I2C_CTL, 0x01);
374
375         /* wait for write complete */
376         msleep(4);
377         if (rc < 0)
378                 return rc;
379         return reg_r8(sd, R518_I2C_CTL);
380 }
381
382 /*
383  * returns: negative is error, pos or zero is data
384  *
385  * The OV518 I2C I/O procedure is different, hence, this function.
386  * This is normally only called from i2c_r(). Note that this function
387  * always succeeds regardless of whether the sensor is present and working.
388  */
389 static int i2c_r(struct sd *sd, __u8 reg)
390 {
391         int rc, value;
392
393         /* Select camera register */
394         rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
395         if (rc < 0)
396                 return rc;
397
398         /* Initiate 2-byte write cycle */
399         rc = reg_w(sd, R518_I2C_CTL, 0x03);
400         if (rc < 0)
401                 return rc;
402
403         /* Initiate 2-byte read cycle */
404         rc = reg_w(sd, R518_I2C_CTL, 0x05);
405         if (rc < 0)
406                 return rc;
407         value = reg_r(sd, R51x_I2C_DATA);
408         PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
409         return value;
410 }
411
412 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
413  * the same position as 1's in "mask" are cleared and set to "value". Bits
414  * that are in the same position as 0's in "mask" are preserved, regardless
415  * of their respective state in "value".
416  */
417 static int i2c_w_mask(struct sd *sd,
418                    __u8 reg,
419                    __u8 value,
420                    __u8 mask)
421 {
422         int rc;
423         __u8 oldval;
424
425         value &= mask;                  /* Enforce mask on value */
426         rc = i2c_r(sd, reg);
427         if (rc < 0)
428                 return rc;
429         oldval = rc & ~mask;            /* Clear the masked bits */
430         value |= oldval;                /* Set the desired bits */
431         return i2c_w(sd, reg, value);
432 }
433
434 /* Temporarily stops OV511 from functioning. Must do this before changing
435  * registers while the camera is streaming */
436 static inline int ov51x_stop(struct sd *sd)
437 {
438         PDEBUG(D_STREAM, "stopping");
439         sd->stopped = 1;
440         return reg_w(sd, OV519_SYS_RESET1, 0x0f);
441 }
442
443 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
444  * actually stopped (for performance). */
445 static inline int ov51x_restart(struct sd *sd)
446 {
447         PDEBUG(D_STREAM, "restarting");
448         if (!sd->stopped)
449                 return 0;
450         sd->stopped = 0;
451
452         /* Reinitialize the stream */
453         return reg_w(sd, OV519_SYS_RESET1, 0x00);
454 }
455
456 /* This does an initial reset of an OmniVision sensor and ensures that I2C
457  * is synchronized. Returns <0 on failure.
458  */
459 static int init_ov_sensor(struct sd *sd)
460 {
461         int i, success;
462
463         /* Reset the sensor */
464         if (i2c_w(sd, 0x12, 0x80) < 0)
465                 return -EIO;
466
467         /* Wait for it to initialize */
468         msleep(150);
469
470         for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
471                 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
472                     i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
473                         success = 1;
474                         continue;
475                 }
476
477                 /* Reset the sensor */
478                 if (i2c_w(sd, 0x12, 0x80) < 0)
479                         return -EIO;
480                 /* Wait for it to initialize */
481                 msleep(150);
482                 /* Dummy read to sync I2C */
483                 if (i2c_r(sd, 0x00) < 0)
484                         return -EIO;
485         }
486         if (!success)
487                 return -EIO;
488         PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
489         return 0;
490 }
491
492 /* Switch on standard JPEG compression. Returns 0 for success. */
493 static int ov519_init_compression(struct sd *sd)
494 {
495         if (!sd->compress_inited) {
496                 if (reg_w_mask(sd, OV519_SYS_EN_CLK1, 1 << 2, 1 << 2) < 0) {
497                         PDEBUG(D_ERR, "Error switching to compressed mode");
498                         return -EIO;
499                 }
500                 sd->compress_inited = 1;
501         }
502         return 0;
503 }
504
505 /* Set the read and write slave IDs. The "slave" argument is the write slave,
506  * and the read slave will be set to (slave + 1).
507  * This should not be called from outside the i2c I/O functions.
508  * Sets I2C read and write slave IDs. Returns <0 for error
509  */
510 static int ov51x_set_slave_ids(struct sd *sd,
511                                 __u8 slave)
512 {
513         int rc;
514
515         rc = reg_w(sd, R51x_I2C_W_SID, slave);
516         if (rc < 0)
517                 return rc;
518         return reg_w(sd, R51x_I2C_R_SID, slave + 1);
519 }
520
521 struct ov_regvals {
522         __u8 reg;
523         __u8 val;
524 };
525 struct ov_i2c_regvals {
526         __u8 reg;
527         __u8 val;
528 };
529
530 static int write_regvals(struct sd *sd,
531                          struct ov_regvals *regvals,
532                          int n)
533 {
534         int rc;
535
536         while (--n >= 0) {
537                 rc = reg_w(sd, regvals->reg, regvals->val);
538                 if (rc < 0)
539                         return rc;
540                 regvals++;
541         }
542         return 0;
543 }
544
545 static int write_i2c_regvals(struct sd *sd,
546                              struct ov_i2c_regvals *regvals,
547                              int n)
548 {
549         int rc;
550
551         while (--n >= 0) {
552                 rc = i2c_w(sd, regvals->reg, regvals->val);
553                 if (rc < 0)
554                         return rc;
555                 regvals++;
556         }
557         return 0;
558 }
559
560 /****************************************************************************
561  *
562  * OV511 and sensor configuration
563  *
564  ***************************************************************************/
565
566 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
567  * the same register settings as the OV8610, since they are very similar.
568  */
569 static int ov8xx0_configure(struct sd *sd)
570 {
571         int rc;
572         static struct ov_i2c_regvals norm_8610[] = {
573                 { 0x12, 0x80 },
574                 { 0x00, 0x00 },
575                 { 0x01, 0x80 },
576                 { 0x02, 0x80 },
577                 { 0x03, 0xc0 },
578                 { 0x04, 0x30 },
579                 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
580                 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
581                 { 0x0a, 0x86 },
582                 { 0x0b, 0xb0 },
583                 { 0x0c, 0x20 },
584                 { 0x0d, 0x20 },
585                 { 0x11, 0x01 },
586                 { 0x12, 0x25 },
587                 { 0x13, 0x01 },
588                 { 0x14, 0x04 },
589                 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
590                 { 0x16, 0x03 },
591                 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
592                 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
593                 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
594                 { 0x1a, 0xf5 },
595                 { 0x1b, 0x00 },
596                 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
597                 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
598                 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
599                 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
600                 { 0x26, 0xa2 },
601                 { 0x27, 0xea },
602                 { 0x28, 0x00 },
603                 { 0x29, 0x00 },
604                 { 0x2a, 0x80 },
605                 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
606                 { 0x2c, 0xac },
607                 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
608                 { 0x2e, 0x80 },
609                 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
610                 { 0x4c, 0x00 },
611                 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
612                 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
613                 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
614                 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
615                 { 0x63, 0xff },
616                 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
617                                  * maybe thats wrong */
618                 { 0x65, 0x00 },
619                 { 0x66, 0x55 },
620                 { 0x67, 0xb0 },
621                 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
622                 { 0x69, 0x02 },
623                 { 0x6a, 0x22 },
624                 { 0x6b, 0x00 },
625                 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
626                                    deleting bit7 colors the first images red */
627                 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
628                 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
629                 { 0x6f, 0x01 },
630                 { 0x70, 0x8b },
631                 { 0x71, 0x00 },
632                 { 0x72, 0x14 },
633                 { 0x73, 0x54 },
634                 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
635                 { 0x75, 0x0e },
636                 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
637                 { 0x77, 0xff },
638                 { 0x78, 0x80 },
639                 { 0x79, 0x80 },
640                 { 0x7a, 0x80 },
641                 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
642                 { 0x7c, 0x00 },
643                 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
644                 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
645                 { 0x7f, 0xfb },
646                 { 0x80, 0x28 },
647                 { 0x81, 0x00 },
648                 { 0x82, 0x23 },
649                 { 0x83, 0x0b },
650                 { 0x84, 0x00 },
651                 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
652                 { 0x86, 0xc9 },
653                 { 0x87, 0x00 },
654                 { 0x88, 0x00 },
655                 { 0x89, 0x01 },
656                 { 0x12, 0x20 },
657                 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
658         };
659
660         PDEBUG(D_PROBE, "starting ov8xx0 configuration");
661
662         if (init_ov_sensor(sd) < 0)
663                 PDEBUG(D_ERR|D_PROBE, "Failed to read sensor ID");
664         else
665                 PDEBUG(D_PROBE, "OV86x0 initialized");
666
667         /* Detect sensor (sub)type */
668         rc = i2c_r(sd, OV7610_REG_COM_I);
669         if (rc < 0) {
670                 PDEBUG(D_ERR, "Error detecting sensor type");
671                 return -1;
672         }
673         if ((rc & 3) == 1) {
674                 PDEBUG(D_PROBE, "Sensor is an OV8610");
675                 sd->sensor = SEN_OV8610;
676         } else {
677                 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
678                 return -1;
679         }
680         PDEBUG(D_PROBE, "Writing 8610 registers");
681         if (write_i2c_regvals(sd,
682                         norm_8610,
683                         sizeof norm_8610 / sizeof norm_8610[0]))
684                 return -1;
685
686         /* Set sensor-specific vars */
687         sd->maxwidth = 640;
688         sd->maxheight = 480;
689         return 0;
690 }
691
692 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
693  * the same register settings as the OV7610, since they are very similar.
694  */
695 static int ov7xx0_configure(struct sd *sd)
696 {
697         int rc, high, low;
698
699         /* Lawrence Glaister <lg@jfm.bc.ca> reports:
700          *
701          * Register 0x0f in the 7610 has the following effects:
702          *
703          * 0x85 (AEC method 1): Best overall, good contrast range
704          * 0x45 (AEC method 2): Very overexposed
705          * 0xa5 (spec sheet default): Ok, but the black level is
706          *      shifted resulting in loss of contrast
707          * 0x05 (old driver setting): very overexposed, too much
708          *      contrast
709          */
710         static struct ov_i2c_regvals norm_7610[] = {
711                 { 0x10, 0xff },
712                 { 0x16, 0x06 },
713                 { 0x28, 0x24 },
714                 { 0x2b, 0xac },
715                 { 0x12, 0x00 },
716                 { 0x38, 0x81 },
717                 { 0x28, 0x24 }, /* 0c */
718                 { 0x0f, 0x85 }, /* lg's setting */
719                 { 0x15, 0x01 },
720                 { 0x20, 0x1c },
721                 { 0x23, 0x2a },
722                 { 0x24, 0x10 },
723                 { 0x25, 0x8a },
724                 { 0x26, 0xa2 },
725                 { 0x27, 0xc2 },
726                 { 0x2a, 0x04 },
727                 { 0x2c, 0xfe },
728                 { 0x2d, 0x93 },
729                 { 0x30, 0x71 },
730                 { 0x31, 0x60 },
731                 { 0x32, 0x26 },
732                 { 0x33, 0x20 },
733                 { 0x34, 0x48 },
734                 { 0x12, 0x24 },
735                 { 0x11, 0x01 },
736                 { 0x0c, 0x24 },
737                 { 0x0d, 0x24 },
738         };
739
740         static struct ov_i2c_regvals norm_7620[] = {
741                 { 0x00, 0x00 },         /* gain */
742                 { 0x01, 0x80 },         /* blue gain */
743                 { 0x02, 0x80 },         /* red gain */
744                 { 0x03, 0xc0 },         /* OV7670_REG_VREF */
745                 { 0x06, 0x60 },
746                 { 0x07, 0x00 },
747                 { 0x0c, 0x24 },
748                 { 0x0c, 0x24 },
749                 { 0x0d, 0x24 },
750                 { 0x11, 0x01 },
751                 { 0x12, 0x24 },
752                 { 0x13, 0x01 },
753                 { 0x14, 0x84 },
754                 { 0x15, 0x01 },
755                 { 0x16, 0x03 },
756                 { 0x17, 0x2f },
757                 { 0x18, 0xcf },
758                 { 0x19, 0x06 },
759                 { 0x1a, 0xf5 },
760                 { 0x1b, 0x00 },
761                 { 0x20, 0x18 },
762                 { 0x21, 0x80 },
763                 { 0x22, 0x80 },
764                 { 0x23, 0x00 },
765                 { 0x26, 0xa2 },
766                 { 0x27, 0xea },
767                 { 0x28, 0x20 },
768                 { 0x29, 0x00 },
769                 { 0x2a, 0x10 },
770                 { 0x2b, 0x00 },
771                 { 0x2c, 0x88 },
772                 { 0x2d, 0x91 },
773                 { 0x2e, 0x80 },
774                 { 0x2f, 0x44 },
775                 { 0x60, 0x27 },
776                 { 0x61, 0x02 },
777                 { 0x62, 0x5f },
778                 { 0x63, 0xd5 },
779                 { 0x64, 0x57 },
780                 { 0x65, 0x83 },
781                 { 0x66, 0x55 },
782                 { 0x67, 0x92 },
783                 { 0x68, 0xcf },
784                 { 0x69, 0x76 },
785                 { 0x6a, 0x22 },
786                 { 0x6b, 0x00 },
787                 { 0x6c, 0x02 },
788                 { 0x6d, 0x44 },
789                 { 0x6e, 0x80 },
790                 { 0x6f, 0x1d },
791                 { 0x70, 0x8b },
792                 { 0x71, 0x00 },
793                 { 0x72, 0x14 },
794                 { 0x73, 0x54 },
795                 { 0x74, 0x00 },
796                 { 0x75, 0x8e },
797                 { 0x76, 0x00 },
798                 { 0x77, 0xff },
799                 { 0x78, 0x80 },
800                 { 0x79, 0x80 },
801                 { 0x7a, 0x80 },
802                 { 0x7b, 0xe2 },
803                 { 0x7c, 0x00 },
804         };
805
806         /* 7640 and 7648. The defaults should be OK for most registers. */
807         static struct ov_i2c_regvals norm_7640[] = {
808                 { 0x12, 0x80 },
809                 { 0x12, 0x14 },
810         };
811
812         /* 7670. Defaults taken from OmniVision provided data,
813         *  as provided by Jonathan Corbet of OLPC               */
814         static struct ov_i2c_regvals norm_7670[] = {
815                 { OV7670_REG_COM7, OV7670_COM7_RESET },
816                 { OV7670_REG_TSLB, 0x04 },              /* OV */
817                 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
818                 { OV7670_REG_CLKRC, 0x1 },
819         /*
820          * Set the hardware window.  These values from OV don't entirely
821          * make sense - hstop is less than hstart.  But they work...
822          */
823                 { OV7670_REG_HSTART, 0x13 },    { OV7670_REG_HSTOP, 0x01 },
824                 { OV7670_REG_HREF, 0xb6 },      { OV7670_REG_VSTART, 0x02 },
825                 { OV7670_REG_VSTOP, 0x7a },     { OV7670_REG_VREF, 0x0a },
826
827                 { OV7670_REG_COM3, 0 }, { OV7670_REG_COM14, 0 },
828         /* Mystery scaling numbers */
829                 { 0x70, 0x3a },         { 0x71, 0x35 },
830                 { 0x72, 0x11 },         { 0x73, 0xf0 },
831                 { 0xa2, 0x02 },
832 /* jfm */
833 /* { OV7670_REG_COM10, 0x0 }, */
834
835         /* Gamma curve values */
836                 { 0x7a, 0x20 },
837 /* jfm:win 7b=1c */
838                 { 0x7b, 0x10 },
839 /* jfm:win 7c=28 */
840                 { 0x7c, 0x1e },
841 /* jfm:win 7d=3c */
842                 { 0x7d, 0x35 },
843                 { 0x7e, 0x5a },         { 0x7f, 0x69 },
844                 { 0x80, 0x76 },         { 0x81, 0x80 },
845                 { 0x82, 0x88 },         { 0x83, 0x8f },
846                 { 0x84, 0x96 },         { 0x85, 0xa3 },
847                 { 0x86, 0xaf },         { 0x87, 0xc4 },
848                 { 0x88, 0xd7 },         { 0x89, 0xe8 },
849
850         /* AGC and AEC parameters.  Note we start by disabling those features,
851            then turn them only after tweaking the values. */
852                 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
853                                  | OV7670_COM8_AECSTEP
854                                  | OV7670_COM8_BFILT },
855                 { OV7670_REG_GAIN, 0 }, { OV7670_REG_AECH, 0 },
856                 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
857 /* jfm:win 14=38 */
858                 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
859                 { OV7670_REG_BD50MAX, 0x05 },   { OV7670_REG_BD60MAX, 0x07 },
860                 { OV7670_REG_AEW, 0x95 },       { OV7670_REG_AEB, 0x33 },
861                 { OV7670_REG_VPT, 0xe3 },       { OV7670_REG_HAECC1, 0x78 },
862                 { OV7670_REG_HAECC2, 0x68 },
863 /* jfm:win a1=0b */
864                 { 0xa1, 0x03 }, /* magic */
865                 { OV7670_REG_HAECC3, 0xd8 },    { OV7670_REG_HAECC4, 0xd8 },
866                 { OV7670_REG_HAECC5, 0xf0 },    { OV7670_REG_HAECC6, 0x90 },
867                 { OV7670_REG_HAECC7, 0x94 },
868                 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
869                                 | OV7670_COM8_AECSTEP
870                                 | OV7670_COM8_BFILT
871                                 | OV7670_COM8_AGC
872                                 | OV7670_COM8_AEC },
873
874         /* Almost all of these are magic "reserved" values.  */
875                 { OV7670_REG_COM5, 0x61 },      { OV7670_REG_COM6, 0x4b },
876                 { 0x16, 0x02 },
877 /* jfm */
878 /*              { OV7670_REG_MVFP, 0x07|OV7670_MVFP_MIRROR }, */
879                 { OV7670_REG_MVFP, 0x07 },
880                 { 0x21, 0x02 },         { 0x22, 0x91 },
881                 { 0x29, 0x07 },         { 0x33, 0x0b },
882                 { 0x35, 0x0b },         { 0x37, 0x1d },
883                 { 0x38, 0x71 },         { 0x39, 0x2a },
884                 { OV7670_REG_COM12, 0x78 },     { 0x4d, 0x40 },
885                 { 0x4e, 0x20 },         { OV7670_REG_GFIX, 0 },
886                 { 0x6b, 0x4a },         { 0x74, 0x10 },
887                 { 0x8d, 0x4f },         { 0x8e, 0 },
888                 { 0x8f, 0 },            { 0x90, 0 },
889                 { 0x91, 0 },            { 0x96, 0 },
890                 { 0x9a, 0 },            { 0xb0, 0x84 },
891                 { 0xb1, 0x0c },         { 0xb2, 0x0e },
892                 { 0xb3, 0x82 },         { 0xb8, 0x0a },
893
894         /* More reserved magic, some of which tweaks white balance */
895                 { 0x43, 0x0a },         { 0x44, 0xf0 },
896                 { 0x45, 0x34 },         { 0x46, 0x58 },
897                 { 0x47, 0x28 },         { 0x48, 0x3a },
898                 { 0x59, 0x88 },         { 0x5a, 0x88 },
899                 { 0x5b, 0x44 },         { 0x5c, 0x67 },
900                 { 0x5d, 0x49 },         { 0x5e, 0x0e },
901                 { 0x6c, 0x0a },         { 0x6d, 0x55 },
902                 { 0x6e, 0x11 },         { 0x6f, 0x9f },
903                                                 /* "9e for advance AWB" */
904                 { 0x6a, 0x40 },         { OV7670_REG_BLUE, 0x40 },
905                 { OV7670_REG_RED, 0x60 },
906                 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
907                                 | OV7670_COM8_AECSTEP
908                                 | OV7670_COM8_BFILT
909                                 | OV7670_COM8_AGC
910                                 | OV7670_COM8_AEC
911                                 | OV7670_COM8_AWB },
912
913         /* Matrix coefficients */
914                 { 0x4f, 0x80 },         { 0x50, 0x80 },
915                 { 0x51, 0 },            { 0x52, 0x22 },
916                 { 0x53, 0x5e },         { 0x54, 0x80 },
917                 { 0x58, 0x9e },
918
919                 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
920                 { OV7670_REG_EDGE, 0 },
921                 { 0x75, 0x05 },         { 0x76, 0xe1 },
922                 { 0x4c, 0 },            { 0x77, 0x01 },
923                 { OV7670_REG_COM13, 0xc3 },     { 0x4b, 0x09 },
924                 { 0xc9, 0x60 },         { OV7670_REG_COM16, 0x38 },
925                 { 0x56, 0x40 },
926
927                 { 0x34, 0x11 },
928                 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
929                 { 0xa4, 0x88 },         { 0x96, 0 },
930                 { 0x97, 0x30 },         { 0x98, 0x20 },
931                 { 0x99, 0x30 },         { 0x9a, 0x84 },
932                 { 0x9b, 0x29 },         { 0x9c, 0x03 },
933                 { 0x9d, 0x4c },         { 0x9e, 0x3f },
934                 { 0x78, 0x04 },
935
936         /* Extra-weird stuff.  Some sort of multiplexor register */
937                 { 0x79, 0x01 },         { 0xc8, 0xf0 },
938                 { 0x79, 0x0f },         { 0xc8, 0x00 },
939                 { 0x79, 0x10 },         { 0xc8, 0x7e },
940                 { 0x79, 0x0a },         { 0xc8, 0x80 },
941                 { 0x79, 0x0b },         { 0xc8, 0x01 },
942                 { 0x79, 0x0c },         { 0xc8, 0x0f },
943                 { 0x79, 0x0d },         { 0xc8, 0x20 },
944                 { 0x79, 0x09 },         { 0xc8, 0x80 },
945                 { 0x79, 0x02 },         { 0xc8, 0xc0 },
946                 { 0x79, 0x03 },         { 0xc8, 0x40 },
947                 { 0x79, 0x05 },         { 0xc8, 0x30 },
948                 { 0x79, 0x26 },
949
950         /* Format YUV422 */
951                 { OV7670_REG_COM7, OV7670_COM7_YUV },  /* Selects YUV mode */
952                 { OV7670_REG_RGB444, 0 },       /* No RGB444 please */
953                 { OV7670_REG_COM1, 0 },
954                 { OV7670_REG_COM15, OV7670_COM15_R00FF },
955                 { OV7670_REG_COM9, 0x18 },
956                                 /* 4x gain ceiling; 0x8 is reserved bit */
957                 { 0x4f, 0x80 },         /* "matrix coefficient 1" */
958                 { 0x50, 0x80 },         /* "matrix coefficient 2" */
959                 { 0x52, 0x22 },         /* "matrix coefficient 4" */
960                 { 0x53, 0x5e },         /* "matrix coefficient 5" */
961                 { 0x54, 0x80 },         /* "matrix coefficient 6" */
962                 { OV7670_REG_COM13, OV7670_COM13_GAMMA|OV7670_COM13_UVSAT },
963 };
964
965         PDEBUG(D_PROBE, "starting OV7xx0 configuration");
966
967 /* jfm:already done? */
968         if (init_ov_sensor(sd) < 0)
969                 PDEBUG(D_ERR, "Failed to read sensor ID");
970         else
971                 PDEBUG(D_PROBE, "OV7xx0 initialized");
972
973         /* Detect sensor (sub)type */
974         rc = i2c_r(sd, OV7610_REG_COM_I);
975
976         /* add OV7670 here
977          * it appears to be wrongly detected as a 7610 by default */
978         if (rc < 0) {
979                 PDEBUG(D_ERR, "Error detecting sensor type");
980                 return -1;
981         }
982         if ((rc & 3) == 3) {
983                 /* quick hack to make OV7670s work */
984                 high = i2c_r(sd, 0x0a);
985                 low = i2c_r(sd, 0x0b);
986                 /* info("%x, %x", high, low); */
987                 if (high == 0x76 && low == 0x73) {
988                         PDEBUG(D_PROBE, "Sensor is an OV7670");
989                         sd->sensor = SEN_OV7670;
990                 } else {
991                         PDEBUG(D_PROBE, "Sensor is an OV7610");
992                         sd->sensor = SEN_OV7610;
993                 }
994         } else if ((rc & 3) == 1) {
995                 /* I don't know what's different about the 76BE yet. */
996                 if (i2c_r(sd, 0x15) & 1)
997                         PDEBUG(D_PROBE, "Sensor is an OV7620AE");
998                 else
999                         PDEBUG(D_PROBE, "Sensor is an OV76BE");
1000
1001                 /* OV511+ will return all zero isoc data unless we
1002                  * configure the sensor as a 7620. Someone needs to
1003                  * find the exact reg. setting that causes this. */
1004                 sd->sensor = SEN_OV76BE;
1005         } else if ((rc & 3) == 0) {
1006                 /* try to read product id registers */
1007                 high = i2c_r(sd, 0x0a);
1008                 if (high < 0) {
1009                         PDEBUG(D_ERR, "Error detecting camera chip PID");
1010                         return high;
1011                 }
1012                 low = i2c_r(sd, 0x0b);
1013                 if (low < 0) {
1014                         PDEBUG(D_ERR, "Error detecting camera chip VER");
1015                         return low;
1016                 }
1017                 if (high == 0x76) {
1018                         if (low == 0x30) {
1019                                 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1020                                 sd->sensor = SEN_OV7630;
1021                         } else if (low == 0x40) {
1022                                 PDEBUG(D_PROBE, "Sensor is an OV7645");
1023                                 sd->sensor = SEN_OV7640; /* FIXME */
1024                         } else if (low == 0x45) {
1025                                 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1026                                 sd->sensor = SEN_OV7640; /* FIXME */
1027                         } else if (low == 0x48) {
1028                                 PDEBUG(D_PROBE, "Sensor is an OV7648");
1029                                 sd->sensor = SEN_OV7640; /* FIXME */
1030                         } else {
1031                                 PDEBUG(D_PROBE, "Unknown sensor: 0x76%X", low);
1032                                 return -1;
1033                         }
1034                 } else {
1035                         PDEBUG(D_PROBE, "Sensor is an OV7620");
1036                         sd->sensor = SEN_OV7620;
1037                 }
1038         } else {
1039                 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1040                 return -1;
1041         }
1042
1043         if (sd->sensor == SEN_OV7620) {
1044                 PDEBUG(D_PROBE, "Writing 7620 registers");
1045                 if (write_i2c_regvals(sd, norm_7620,
1046                                 sizeof norm_7620 / sizeof norm_7620[0]))
1047                         return -1;
1048         } else if (sd->sensor == SEN_OV7630) {
1049                 PDEBUG(D_ERR, "7630 is not supported by this driver version");
1050                 return -1;
1051         } else if (sd->sensor == SEN_OV7640) {
1052                 PDEBUG(D_PROBE, "Writing 7640 registers");
1053                 if (write_i2c_regvals(sd, norm_7640,
1054                                 sizeof norm_7640 / sizeof norm_7640[0]))
1055                         return -1;
1056         } else if (sd->sensor == SEN_OV7670) {
1057                 PDEBUG(D_PROBE, "Writing 7670 registers");
1058                 if (write_i2c_regvals(sd, norm_7670,
1059                                 sizeof norm_7670 / sizeof norm_7670[0]))
1060                         return -1;
1061         } else {
1062                 PDEBUG(D_PROBE, "Writing 7610 registers");
1063                 if (write_i2c_regvals(sd, norm_7610,
1064                                 sizeof norm_7610 / sizeof norm_7610[0]))
1065                         return -1;
1066         }
1067
1068         /* Set sensor-specific vars */
1069         sd->maxwidth = 640;
1070         sd->maxheight = 480;
1071         return 0;
1072 }
1073
1074 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1075 static int ov6xx0_configure(struct sd *sd)
1076 {
1077         int rc;
1078         static struct ov_i2c_regvals norm_6x20[] = {
1079                 { 0x12, 0x80 }, /* reset */
1080                 { 0x11, 0x01 },
1081                 { 0x03, 0x60 },
1082                 { 0x05, 0x7f }, /* For when autoadjust is off */
1083                 { 0x07, 0xa8 },
1084                 /* The ratio of 0x0c and 0x0d  controls the white point */
1085                 { 0x0c, 0x24 },
1086                 { 0x0d, 0x24 },
1087                 { 0x0f, 0x15 }, /* COMS */
1088                 { 0x10, 0x75 }, /* AEC Exposure time */
1089                 { 0x12, 0x24 }, /* Enable AGC */
1090                 { 0x14, 0x04 },
1091                 /* 0x16: 0x06 helps frame stability with moving objects */
1092                 { 0x16, 0x06 },
1093 /*              { 0x20, 0x30 },  * Aperture correction enable */
1094                 { 0x26, 0xb2 }, /* BLC enable */
1095                 /* 0x28: 0x05 Selects RGB format if RGB on */
1096                 { 0x28, 0x05 },
1097                 { 0x2a, 0x04 }, /* Disable framerate adjust */
1098 /*              { 0x2b, 0xac },  * Framerate; Set 2a[7] first */
1099                 { 0x2d, 0x99 },
1100                 { 0x33, 0xa0 }, /* Color Processing Parameter */
1101                 { 0x34, 0xd2 }, /* Max A/D range */
1102                 { 0x38, 0x8b },
1103                 { 0x39, 0x40 },
1104
1105                 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1106                 { 0x3c, 0x3c }, /* Change AEC mode */
1107                 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1108
1109                 { 0x3d, 0x80 },
1110                 /* These next two registers (0x4a, 0x4b) are undocumented.
1111                  * They control the color balance */
1112                 { 0x4a, 0x80 },
1113                 { 0x4b, 0x80 },
1114                 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1115                 { 0x4e, 0xc1 },
1116                 { 0x4f, 0x04 },
1117 /* Do 50-53 have any effect? */
1118 /* Toggle 0x12[2] off and on here? */
1119         };
1120
1121         static struct ov_i2c_regvals norm_6x30[] = {
1122                 { 0x12, 0x80 }, /* Reset */
1123                 { 0x00, 0x1f }, /* Gain */
1124                 { 0x01, 0x99 }, /* Blue gain */
1125                 { 0x02, 0x7c }, /* Red gain */
1126                 { 0x03, 0xc0 }, /* Saturation */
1127                 { 0x05, 0x0a }, /* Contrast */
1128                 { 0x06, 0x95 }, /* Brightness */
1129                 { 0x07, 0x2d }, /* Sharpness */
1130                 { 0x0c, 0x20 },
1131                 { 0x0d, 0x20 },
1132                 { 0x0e, 0x20 },
1133                 { 0x0f, 0x05 },
1134                 { 0x10, 0x9a },
1135                 { 0x11, 0x00 }, /* Pixel clock = fastest */
1136                 { 0x12, 0x24 }, /* Enable AGC and AWB */
1137                 { 0x13, 0x21 },
1138                 { 0x14, 0x80 },
1139                 { 0x15, 0x01 },
1140                 { 0x16, 0x03 },
1141                 { 0x17, 0x38 },
1142                 { 0x18, 0xea },
1143                 { 0x19, 0x04 },
1144                 { 0x1a, 0x93 },
1145                 { 0x1b, 0x00 },
1146                 { 0x1e, 0xc4 },
1147                 { 0x1f, 0x04 },
1148                 { 0x20, 0x20 },
1149                 { 0x21, 0x10 },
1150                 { 0x22, 0x88 },
1151                 { 0x23, 0xc0 }, /* Crystal circuit power level */
1152                 { 0x25, 0x9a }, /* Increase AEC black ratio */
1153                 { 0x26, 0xb2 }, /* BLC enable */
1154                 { 0x27, 0xa2 },
1155                 { 0x28, 0x00 },
1156                 { 0x29, 0x00 },
1157                 { 0x2a, 0x84 }, /* 60 Hz power */
1158                 { 0x2b, 0xa8 }, /* 60 Hz power */
1159                 { 0x2c, 0xa0 },
1160                 { 0x2d, 0x95 }, /* Enable auto-brightness */
1161                 { 0x2e, 0x88 },
1162                 { 0x33, 0x26 },
1163                 { 0x34, 0x03 },
1164                 { 0x36, 0x8f },
1165                 { 0x37, 0x80 },
1166                 { 0x38, 0x83 },
1167                 { 0x39, 0x80 },
1168                 { 0x3a, 0x0f },
1169                 { 0x3b, 0x3c },
1170                 { 0x3c, 0x1a },
1171                 { 0x3d, 0x80 },
1172                 { 0x3e, 0x80 },
1173                 { 0x3f, 0x0e },
1174                 { 0x40, 0x00 }, /* White bal */
1175                 { 0x41, 0x00 }, /* White bal */
1176                 { 0x42, 0x80 },
1177                 { 0x43, 0x3f }, /* White bal */
1178                 { 0x44, 0x80 },
1179                 { 0x45, 0x20 },
1180                 { 0x46, 0x20 },
1181                 { 0x47, 0x80 },
1182                 { 0x48, 0x7f },
1183                 { 0x49, 0x00 },
1184                 { 0x4a, 0x00 },
1185                 { 0x4b, 0x80 },
1186                 { 0x4c, 0xd0 },
1187                 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1188                 { 0x4e, 0x40 },
1189                 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1190                 { 0x50, 0xff },
1191                 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1192                 { 0x55, 0xff },
1193                 { 0x56, 0x12 },
1194                 { 0x57, 0x81 },
1195                 { 0x58, 0x75 },
1196                 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1197                 { 0x5a, 0x2c },
1198                 { 0x5b, 0x0f }, /* AWB chrominance levels */
1199                 { 0x5c, 0x10 },
1200                 { 0x3d, 0x80 },
1201                 { 0x27, 0xa6 },
1202                 { 0x12, 0x20 }, /* Toggle AWB */
1203                 { 0x12, 0x24 },
1204         };
1205
1206         PDEBUG(D_PROBE, "starting sensor configuration");
1207
1208         if (init_ov_sensor(sd) < 0) {
1209                 PDEBUG(D_ERR, "Failed to read sensor ID.");
1210                 return -1;
1211         }
1212         PDEBUG(D_PROBE, "OV6xx0 sensor detected");
1213
1214         /* Detect sensor (sub)type */
1215         rc = i2c_r(sd, OV7610_REG_COM_I);
1216         if (rc < 0) {
1217                 PDEBUG(D_ERR, "Error detecting sensor type");
1218                 return -1;
1219         }
1220
1221         /* Ugh. The first two bits are the version bits, but
1222          * the entire register value must be used. I guess OVT
1223          * underestimated how many variants they would make. */
1224         if (rc == 0x00) {
1225                 sd->sensor = SEN_OV6630;
1226                 PDEBUG(D_ERR,
1227                         "WARNING: Sensor is an OV66308. Your camera may have");
1228                 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1229         } else if (rc == 0x01) {
1230                 sd->sensor = SEN_OV6620;
1231                 PDEBUG(D_PROBE, "Sensor is an OV6620");
1232         } else if (rc == 0x02) {
1233                 sd->sensor = SEN_OV6630;
1234                 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1235         } else if (rc == 0x03) {
1236                 sd->sensor = SEN_OV6630;
1237                 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1238         } else if (rc == 0x90) {
1239                 sd->sensor = SEN_OV6630;
1240                 PDEBUG(D_ERR,
1241                         "WARNING: Sensor is an OV66307. Your camera may have");
1242                 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1243         } else {
1244                 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1245                 return -1;
1246         }
1247
1248         /* Set sensor-specific vars */
1249         sd->maxwidth = 352;
1250         sd->maxheight = 288;
1251
1252         if (sd->sensor == SEN_OV6620) {
1253                 PDEBUG(D_PROBE, "Writing 6x20 registers");
1254                 if (write_i2c_regvals(sd, norm_6x20,
1255                                 sizeof norm_6x20 / sizeof norm_6x20[0]))
1256                         return -1;
1257         } else {
1258                 PDEBUG(D_PROBE, "Writing 6x30 registers");
1259                 if (write_i2c_regvals(sd, norm_6x30,
1260                                 sizeof norm_6x30 / sizeof norm_6x30[0]))
1261                         return -1;
1262         }
1263         return 0;
1264 }
1265
1266 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1267 static void ov51x_led_control(struct sd *sd, int on)
1268 {
1269         PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off");
1270
1271 /*      if (sd->bridge == BRG_OV511PLUS) */
1272 /*              reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0); */
1273 /*      else if (sd->bridge == BRG_OV519) */
1274                 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1);   /* 0 / 1 */
1275 /*      else if (sd->bclass == BCL_OV518) */
1276 /*              reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02); */
1277 }
1278
1279 /* this function is called at probe time */
1280 static int sd_config(struct gspca_dev *gspca_dev,
1281                         const struct usb_device_id *id)
1282 {
1283         struct sd *sd = (struct sd *) gspca_dev;
1284         struct cam *cam;
1285
1286 /* (from ov519_configure) */
1287         static struct ov_regvals init_519[] = {
1288                 { 0x5a,  0x6d }, /* EnableSystem */
1289 /* jfm trace usbsnoop3-1.txt */
1290 /* jfm 53 = fb */
1291                 { 0x53,  0x9b },
1292                 { 0x54,  0xff }, /* set bit2 to enable jpeg */
1293                 { 0x5d,  0x03 },
1294                 { 0x49,  0x01 },
1295                 { 0x48,  0x00 },
1296                 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1297                  * detection will fail. This deserves further investigation. */
1298                 { OV519_GPIO_IO_CTRL0,   0xee },
1299                 { 0x51,  0x0f }, /* SetUsbInit */
1300                 { 0x51,  0x00 },
1301                 { 0x22,  0x00 },
1302                 /* windows reads 0x55 at this point*/
1303         };
1304
1305         if (write_regvals(sd, init_519,
1306                         sizeof init_519 / sizeof init_519[0]))
1307                 goto error;
1308 /* jfm: not seen in windows trace */
1309         if (ov519_init_compression(sd))
1310                 goto error;
1311         ov51x_led_control(sd, 0);       /* turn LED off */
1312
1313         /* Test for 76xx */
1314         sd->primary_i2c_slave = OV7xx0_SID;
1315         if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1316                 goto error;
1317
1318         /* The OV519 must be more aggressive about sensor detection since
1319          * I2C write will never fail if the sensor is not present. We have
1320          * to try to initialize the sensor to detect its presence */
1321         if (init_ov_sensor(sd) < 0) {
1322                 /* Test for 6xx0 */
1323                 sd->primary_i2c_slave = OV6xx0_SID;
1324                 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1325                         goto error;
1326
1327                 if (init_ov_sensor(sd) < 0) {
1328                         /* Test for 8xx0 */
1329                         sd->primary_i2c_slave = OV8xx0_SID;
1330                         if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1331                                 goto error;
1332
1333                         if (init_ov_sensor(sd) < 0) {
1334                                 PDEBUG(D_ERR,
1335                                         "Can't determine sensor slave IDs");
1336                                 goto error;
1337                         } else {
1338                                 if (ov8xx0_configure(sd) < 0) {
1339                                         PDEBUG(D_ERR,
1340                                            "Failed to configure OV8xx0 sensor");
1341                                         goto error;
1342                                 }
1343                         }
1344                 } else {
1345                         if (ov6xx0_configure(sd) < 0) {
1346                                 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1347                                 goto error;
1348                         }
1349                 }
1350         } else {
1351                 if (ov7xx0_configure(sd) < 0) {
1352                         PDEBUG(D_ERR, "Failed to configure OV7xx0");
1353                         goto error;
1354                 }
1355         }
1356
1357         cam = &gspca_dev->cam;
1358         cam->epaddr = OV511_ENDPOINT_ADDRESS;
1359         if (sd->maxwidth == 640) {
1360                 cam->cam_mode = vga_mode;
1361                 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
1362         } else {
1363                 cam->cam_mode = sif_mode;
1364                 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
1365         }
1366         cam->dev_name = (char *) id->driver_info;
1367         sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
1368         sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
1369         sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
1370         return 0;
1371 error:
1372         PDEBUG(D_ERR, "OV519 Config failed");
1373         return -EBUSY;
1374 }
1375
1376 /* this function is called at open time */
1377 static int sd_open(struct gspca_dev *gspca_dev)
1378 {
1379         return 0;
1380 }
1381
1382 /* Sets up the OV519 with the given image parameters
1383  *
1384  * OV519 needs a completely different approach, until we can figure out what
1385  * the individual registers do.
1386  *
1387  * Do not put any sensor-specific code in here (including I2C I/O functions)
1388  */
1389 static int ov519_mode_init_regs(struct sd *sd,
1390                                 int width, int height)
1391 {
1392         static struct ov_regvals mode_init_519_ov7670[] = {
1393                 { 0x5d, 0x03 }, /* Turn off suspend mode */
1394                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1395                 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1396                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1397                 { 0xa3, 0x18 },
1398                 { 0xa4, 0x04 },
1399                 { 0xa5, 0x28 },
1400                 { 0x37, 0x00 }, /* SetUsbInit */
1401                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1402                 /* Enable both fields, YUV Input, disable defect comp (why?) */
1403                 { 0x20, 0x0c },
1404                 { 0x21, 0x38 },
1405                 { 0x22, 0x1d },
1406                 { 0x17, 0x50 }, /* undocumented */
1407                 { 0x37, 0x00 }, /* undocumented */
1408                 { 0x40, 0xff }, /* I2C timeout counter */
1409                 { 0x46, 0x00 }, /* I2C clock prescaler */
1410                 { 0x59, 0x04 }, /* new from windrv 090403 */
1411                 { 0xff, 0x00 }, /* undocumented */
1412                 /* windows reads 0x55 at this point, why? */
1413         };
1414
1415         static struct ov_regvals mode_init_519[] = {
1416                 { 0x5d, 0x03 }, /* Turn off suspend mode */
1417                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1418                 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1419                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1420                 { 0xa3, 0x18 },
1421                 { 0xa4, 0x04 },
1422                 { 0xa5, 0x28 },
1423                 { 0x37, 0x00 }, /* SetUsbInit */
1424                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1425                 /* Enable both fields, YUV Input, disable defect comp (why?) */
1426                 { 0x22, 0x1d },
1427                 { 0x17, 0x50 }, /* undocumented */
1428                 { 0x37, 0x00 }, /* undocumented */
1429                 { 0x40, 0xff }, /* I2C timeout counter */
1430                 { 0x46, 0x00 }, /* I2C clock prescaler */
1431                 { 0x59, 0x04 }, /* new from windrv 090403 */
1432                 { 0xff, 0x00 }, /* undocumented */
1433                 /* windows reads 0x55 at this point, why? */
1434         };
1435
1436 /* int hi_res; */
1437
1438         PDEBUG(D_CONF, "mode init %dx%d", width, height);
1439
1440 /*      if (width >= 800 && height >= 600)
1441                 hi_res = 1;
1442         else
1443                 hi_res = 0; */
1444
1445 /*      if (ov51x_stop(sd) < 0)
1446                 return -EIO; */
1447
1448         /******** Set the mode ********/
1449         if (sd->sensor != SEN_OV7670) {
1450                 if (write_regvals(sd, mode_init_519,
1451                             sizeof mode_init_519 / sizeof mode_init_519[0]))
1452                         return -EIO;
1453         } else {
1454                 if (write_regvals(sd, mode_init_519_ov7670,
1455                                 sizeof mode_init_519_ov7670
1456                                         / sizeof mode_init_519_ov7670[0]))
1457                         return -EIO;
1458         }
1459
1460         if (sd->sensor == SEN_OV7640) {
1461                 /* Select 8-bit input mode */
1462                 reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
1463         }
1464
1465         reg_w(sd, OV519_CAM_H_SIZE,     width >> 4);
1466         reg_w(sd, OV519_CAM_V_SIZE,     height >> 3);
1467         reg_w(sd, OV519_CAM_X_OFFSETL,  0x00);
1468         reg_w(sd, OV519_CAM_X_OFFSETH,  0x00);
1469         reg_w(sd, OV519_CAM_Y_OFFSETL,  0x00);
1470         reg_w(sd, OV519_CAM_Y_OFFSETH,  0x00);
1471         reg_w(sd, OV519_CAM_DIVIDER,    0x00);
1472         reg_w(sd, OV519_CAM_FORMAT,     0x03); /* YUV422 */
1473         reg_w(sd, 0x26,                 0x00); /* Undocumented */
1474
1475         /******** Set the framerate ********/
1476         if (frame_rate > 0)
1477                 sd->frame_rate = frame_rate;
1478
1479 /* FIXME: These are only valid at the max resolution. */
1480         sd->clockdiv = 0;
1481         if (sd->sensor == SEN_OV7640) {
1482                 switch (sd->frame_rate) {
1483 /*jfm: default was 30 fps */
1484                 case 30:
1485                         reg_w(sd, 0xa4, 0x0c);
1486                         reg_w(sd, 0x23, 0xff);
1487                         break;
1488                 case 25:
1489                         reg_w(sd, 0xa4, 0x0c);
1490                         reg_w(sd, 0x23, 0x1f);
1491                         break;
1492                 case 20:
1493                         reg_w(sd, 0xa4, 0x0c);
1494                         reg_w(sd, 0x23, 0x1b);
1495                         break;
1496                 default:
1497 /*              case 15: */
1498                         reg_w(sd, 0xa4, 0x04);
1499                         reg_w(sd, 0x23, 0xff);
1500                         sd->clockdiv = 1;
1501                         break;
1502                 case 10:
1503                         reg_w(sd, 0xa4, 0x04);
1504                         reg_w(sd, 0x23, 0x1f);
1505                         sd->clockdiv = 1;
1506                         break;
1507                 case 5:
1508                         reg_w(sd, 0xa4, 0x04);
1509                         reg_w(sd, 0x23, 0x1b);
1510                         sd->clockdiv = 1;
1511                         break;
1512                 }
1513         } else if (sd->sensor == SEN_OV8610) {
1514                 switch (sd->frame_rate) {
1515                 default:        /* 15 fps */
1516 /*              case 15: */
1517                         reg_w(sd, 0xa4, 0x06);
1518                         reg_w(sd, 0x23, 0xff);
1519                         break;
1520                 case 10:
1521                         reg_w(sd, 0xa4, 0x06);
1522                         reg_w(sd, 0x23, 0x1f);
1523                         break;
1524                 case 5:
1525                         reg_w(sd, 0xa4, 0x06);
1526                         reg_w(sd, 0x23, 0x1b);
1527                         break;
1528                 }
1529                 sd->clockdiv = 0;
1530         } else if (sd->sensor == SEN_OV7670) { /* guesses, based on 7640 */
1531                 PDEBUG(D_STREAM, "Setting framerate to %d fps",
1532                                  (sd->frame_rate == 0) ? 15 : sd->frame_rate);
1533                 switch (sd->frame_rate) {
1534                 case 30:
1535                         reg_w(sd, 0xa4, 0x10);
1536                         reg_w(sd, 0x23, 0xff);
1537                         break;
1538                 case 20:
1539                         reg_w(sd, 0xa4, 0x10);
1540                         reg_w(sd, 0x23, 0x1b);
1541                         break;
1542                 default: /* 15 fps */
1543 /*                      case 15: */
1544                         reg_w(sd, 0xa4, 0x10);
1545                         reg_w(sd, 0x23, 0xff);
1546                         sd->clockdiv = 1;
1547                         break;
1548                 }
1549         }
1550
1551 /*      if (ov51x_restart(sd) < 0)
1552                 return -EIO; */
1553
1554         /* Reset it just for good measure */
1555 /*      if (ov51x_reset(sd, OV511_RESET_NOREGS) < 0)
1556                 return -EIO; */
1557         return 0;
1558 }
1559
1560 static int mode_init_ov_sensor_regs(struct sd *sd,
1561                                 struct ovsensor_window *win)
1562 {
1563         int qvga = win->quarter;
1564
1565         /******** Mode (VGA/QVGA) and sensor specific regs ********/
1566         switch (sd->sensor) {
1567         case SEN_OV8610:
1568                 /* For OV8610 qvga means qsvga */
1569                 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
1570                 break;
1571         case SEN_OV7610:
1572                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1573                 break;
1574         case SEN_OV7620:
1575 /*              i2c_w(sd, 0x2b, 0x00); */
1576                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1577                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1578                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
1579                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
1580                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
1581                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
1582                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
1583                 break;
1584         case SEN_OV76BE:
1585 /*              i2c_w(sd, 0x2b, 0x00); */
1586                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1587                 break;
1588         case SEN_OV7640:
1589 /*              i2c_w(sd, 0x2b, 0x00); */
1590                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1591                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1592 /*              i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
1593 /*              i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
1594 /*              i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
1595 /*              i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
1596 /*              i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
1597                 break;
1598         case SEN_OV7670:
1599                 /* set COM7_FMT_VGA or COM7_FMT_QVGA
1600                  * do we need to set anything else?
1601                  *      HSTART etc are set in set_ov_sensor_window itself */
1602                 i2c_w_mask(sd, OV7670_REG_COM7,
1603                          qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
1604                          OV7670_COM7_FMT_MASK);
1605                 break;
1606         case SEN_OV6620:
1607                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1608                 break;
1609         case SEN_OV6630:
1610                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1611                 break;
1612         default:
1613                 return -EINVAL;
1614         }
1615
1616         /******** Palette-specific regs ********/
1617 /* Need to do work here for the OV7670 */
1618
1619                 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1620                         /* not valid on the OV6620/OV7620/6630? */
1621                         i2c_w_mask(sd, 0x0e, 0x00, 0x40);
1622                 }
1623
1624                 /* The OV518 needs special treatment. Although both the OV518
1625                  * and the OV6630 support a 16-bit video bus, only the 8 bit Y
1626                  * bus is actually used. The UV bus is tied to ground.
1627                  * Therefore, the OV6630 needs to be in 8-bit multiplexed
1628                  * output mode */
1629
1630                 /* OV7640 is 8-bit only */
1631
1632                 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
1633                         i2c_w_mask(sd, 0x13, 0x00, 0x20);
1634 /*      } */
1635
1636         /******** Clock programming ********/
1637         /* The OV6620 needs special handling. This prevents the
1638          * severe banding that normally occurs */
1639         if (sd->sensor == SEN_OV6620) {
1640
1641                 /* Clock down */
1642                 i2c_w(sd, 0x2a, 0x04);
1643                 i2c_w(sd, 0x11, win->clockdiv);
1644                 i2c_w(sd, 0x2a, 0x84);
1645                 /* This next setting is critical. It seems to improve
1646                  * the gain or the contrast. The "reserved" bits seem
1647                  * to have some effect in this case. */
1648                 i2c_w(sd, 0x2d, 0x85);
1649         } else if (win->clockdiv >= 0) {
1650                 i2c_w(sd, 0x11, win->clockdiv);
1651         }
1652
1653         /******** Special Features ********/
1654 /* no evidence this is possible with OV7670, either */
1655         /* Test Pattern */
1656         if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
1657                 i2c_w_mask(sd, 0x12, 0x00, 0x02);
1658
1659         /* Enable auto white balance */
1660         if (sd->sensor == SEN_OV7670)
1661                 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
1662                                 OV7670_COM8_AWB);
1663         else
1664                 i2c_w_mask(sd, 0x12, 0x04, 0x04);
1665
1666         /* This will go away as soon as ov51x_mode_init_sensor_regs() */
1667         /* is fully tested. */
1668         /* 7620/6620/6630? don't have register 0x35, so play it safe */
1669         if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1670                 if (win->width == 640 /*&& win->height == 480*/)
1671                         i2c_w(sd, 0x35, 0x9e);
1672                 else
1673                         i2c_w(sd, 0x35, 0x1e);
1674         }
1675         return 0;
1676 }
1677
1678 static int set_ov_sensor_window(struct sd *sd,
1679                                 struct ovsensor_window *win)
1680 {
1681         int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
1682         int ret, hstart, hstop, vstop, vstart;
1683         __u8 v;
1684
1685         /* The different sensor ICs handle setting up of window differently.
1686          * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
1687         switch (sd->sensor) {
1688         case SEN_OV8610:
1689                 hwsbase = 0x1e;
1690                 hwebase = 0x1e;
1691                 vwsbase = 0x02;
1692                 vwebase = 0x02;
1693                 break;
1694         case SEN_OV7610:
1695         case SEN_OV76BE:
1696                 hwsbase = 0x38;
1697                 hwebase = 0x3a;
1698                 vwsbase = vwebase = 0x05;
1699                 break;
1700         case SEN_OV6620:
1701         case SEN_OV6630:
1702                 hwsbase = 0x38;
1703                 hwebase = 0x3a;
1704                 vwsbase = 0x05;
1705                 vwebase = 0x06;
1706                 break;
1707         case SEN_OV7620:
1708                 hwsbase = 0x2f;         /* From 7620.SET (spec is wrong) */
1709                 hwebase = 0x2f;
1710                 vwsbase = vwebase = 0x05;
1711                 break;
1712         case SEN_OV7640:
1713                 hwsbase = 0x1a;
1714                 hwebase = 0x1a;
1715                 vwsbase = vwebase = 0x03;
1716                 break;
1717         case SEN_OV7670:
1718                 /*handling of OV7670 hardware sensor start and stop values
1719                  * is very odd, compared to the other OV sensors */
1720                 vwsbase = vwebase = hwebase = hwsbase = 0x00;
1721                 break;
1722         default:
1723                 return -EINVAL;
1724         }
1725
1726         switch (sd->sensor) {
1727         case SEN_OV6620:
1728         case SEN_OV6630:
1729                 if (win->quarter) {     /* QCIF */
1730                         hwscale = 0;
1731                         vwscale = 0;
1732                 } else {                /* CIF */
1733                         hwscale = 1;
1734                         vwscale = 1;    /* The datasheet says 0;
1735                                          * it's wrong */
1736                 }
1737                 break;
1738         case SEN_OV8610:
1739                 if (win->quarter) {     /* QSVGA */
1740                         hwscale = 1;
1741                         vwscale = 1;
1742                 } else {                /* SVGA */
1743                         hwscale = 2;
1744                         vwscale = 2;
1745                 }
1746                 break;
1747         default:                        /* SEN_OV7xx0 */
1748                 if (win->quarter) {     /* QVGA */
1749                         hwscale = 1;
1750                         vwscale = 0;
1751                 } else {                /* VGA */
1752                         hwscale = 2;
1753                         vwscale = 1;
1754                 }
1755         }
1756
1757         ret = mode_init_ov_sensor_regs(sd, win);
1758         if (ret < 0)
1759                 return ret;
1760
1761         if (sd->sensor == SEN_OV8610) {
1762                 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
1763                                 /* old 0x95, new 0x05 from windrv 090403 */
1764                                                 /* bits 5-7: reserved */
1765                 i2c_w_mask(sd, 0x28, 0x20, 0x20);
1766                                         /* bit 5: progressive mode on */
1767         }
1768
1769         /* The below is wrong for OV7670s because their window registers
1770          * only store the high bits in 0x17 to 0x1a */
1771
1772         /* SRH Use sd->max values instead of requested win values */
1773         /* SCS Since we're sticking with only the max hardware widths
1774          * for a given mode */
1775         /* I can hard code this for OV7670s */
1776         /* Yes, these numbers do look odd, but they're tested and work! */
1777         if (sd->sensor == SEN_OV7670) {
1778                 if (win->quarter) {     /* QVGA from ov7670.c by
1779                                          * Jonathan Corbet */
1780                         hstart = 164;
1781                         hstop = 20;
1782                         vstart = 14;
1783                         vstop = 494;
1784                 } else {                /* VGA */
1785                         hstart = 158;
1786                         hstop = 14;
1787                         vstart = 10;
1788                         vstop = 490;
1789                 }
1790                 /* OV7670 hardware window registers are split across
1791                  * multiple locations */
1792                 i2c_w(sd, OV7670_REG_HSTART, (hstart >> 3) & 0xff);
1793                 i2c_w(sd, OV7670_REG_HSTOP, (hstop >> 3) & 0xff);
1794                 v = i2c_r(sd, OV7670_REG_HREF);
1795                 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
1796                 msleep(10);     /* need to sleep between read and write to
1797                                  * same reg! */
1798                 i2c_w(sd, OV7670_REG_HREF, v);
1799
1800                 i2c_w(sd, OV7670_REG_VSTART, (vstart >> 2) & 0xff);
1801                 i2c_w(sd, OV7670_REG_VSTOP, (vstop >> 2) & 0xff);
1802                 v = i2c_r(sd, OV7670_REG_VREF);
1803                 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
1804                 msleep(10);     /* need to sleep between read and write to
1805                                  * same reg! */
1806                 i2c_w(sd, OV7670_REG_VREF, v);
1807
1808         } else {
1809                 i2c_w(sd, 0x17, hwsbase + (win->x >> hwscale));
1810                 i2c_w(sd, 0x18, hwebase + ((win->x + win->width) >> hwscale));
1811                 i2c_w(sd, 0x19, vwsbase + (win->y >> vwscale));
1812                 i2c_w(sd, 0x1a, vwebase + ((win->y + win->height) >> vwscale));
1813         }
1814         return 0;
1815 }
1816
1817 static int ov_sensor_mode_setup(struct sd *sd,
1818                                 int width, int height)
1819 {
1820         struct ovsensor_window win;
1821
1822 /*      win.format = mode; */
1823
1824         /* Unless subcapture is enabled,
1825          * center the image window and downsample
1826          * if possible to increase the field of view */
1827         /* NOTE: OV518(+) and OV519 does downsampling on its own */
1828         win.width = width;
1829         win.height = height;
1830         if (width == sd->maxwidth)
1831                 win.quarter = 0;
1832         else
1833                 win.quarter = 1;
1834
1835         /* Center it */
1836         win.x = (win.width - width) / 2;
1837         win.y = (win.height - height) / 2;
1838
1839         /* Clock is determined by OV519 frame rate code */
1840         win.clockdiv = sd->clockdiv;
1841
1842         PDEBUG(D_CONF, "Setting clock divider to %d", win.clockdiv);
1843         return set_ov_sensor_window(sd, &win);
1844 }
1845
1846 /* -- start the camera -- */
1847 static void sd_start(struct gspca_dev *gspca_dev)
1848 {
1849         struct sd *sd = (struct sd *) gspca_dev;
1850         int ret;
1851
1852
1853         ret = ov519_mode_init_regs(sd, gspca_dev->width, gspca_dev->height);
1854         if (ret < 0)
1855                 goto out;
1856         ret = ov_sensor_mode_setup(sd, gspca_dev->width, gspca_dev->height);
1857         if (ret < 0)
1858                 goto out;
1859
1860         ret = ov51x_restart((struct sd *) gspca_dev);
1861         if (ret < 0)
1862                 goto out;
1863         PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
1864         ov51x_led_control(sd, 1);
1865         return;
1866 out:
1867         PDEBUG(D_ERR, "camera start error:%d", ret);
1868 }
1869
1870 static void sd_stopN(struct gspca_dev *gspca_dev)
1871 {
1872         ov51x_stop((struct sd *) gspca_dev);
1873         ov51x_led_control((struct sd *) gspca_dev, 0);
1874 }
1875
1876 static void sd_stop0(struct gspca_dev *gspca_dev)
1877 {
1878 }
1879
1880 static void sd_close(struct gspca_dev *gspca_dev)
1881 {
1882 }
1883
1884 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1885                         struct gspca_frame *frame,      /* target */
1886                         unsigned char *data,            /* isoc packet */
1887                         int len)                        /* iso packet length */
1888 {
1889         /* Header of ov519 is 16 bytes:
1890          *     Byte     Value      Description
1891          *      0       0xff    magic
1892          *      1       0xff    magic
1893          *      2       0xff    magic
1894          *      3       0xXX    0x50 = SOF, 0x51 = EOF
1895          *      9       0xXX    0x01 initial frame without data,
1896          *                      0x00 standard frame with image
1897          *      14      Lo      in EOF: length of image data / 8
1898          *      15      Hi
1899          */
1900
1901         if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
1902                 switch (data[3]) {
1903                 case 0x50:              /* start of frame */
1904 #define HDRSZ 16
1905                         data += HDRSZ;
1906                         len -= HDRSZ;
1907 #undef HDRSZ
1908                         if (data[0] == 0xff || data[1] == 0xd8)
1909                                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1910                                                 data, len);
1911                         else
1912                                 gspca_dev->last_packet_type = DISCARD_PACKET;
1913                         return;
1914                 case 0x51:              /* end of frame */
1915                         if (data[9] != 0)
1916                                 gspca_dev->last_packet_type = DISCARD_PACKET;
1917                         gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1918                                         data, 0);
1919                         return;
1920                 }
1921         }
1922
1923         /* intermediate packet */
1924         gspca_frame_add(gspca_dev, INTER_PACKET, frame,
1925                         data, len);
1926 }
1927
1928 /* -- management routines -- */
1929
1930 static void setbrightness(struct gspca_dev *gspca_dev)
1931 {
1932         struct sd *sd = (struct sd *) gspca_dev;
1933         int val;
1934 /*      int was_streaming; */
1935
1936         val = sd->brightness;
1937         PDEBUG(D_CONF, "brightness:%d", val);
1938 /*      was_streaming = gspca_dev->streaming;
1939  *      if (was_streaming)
1940  *              ov51x_stop(sd); */
1941         switch (sd->sensor) {
1942         case SEN_OV8610:
1943         case SEN_OV7610:
1944         case SEN_OV76BE:
1945         case SEN_OV6620:
1946         case SEN_OV6630:
1947         case SEN_OV7640:
1948                 i2c_w(sd, OV7610_REG_BRT, val);
1949                 break;
1950         case SEN_OV7620:
1951                 /* 7620 doesn't like manual changes when in auto mode */
1952 /*fixme
1953  *              if (!sd->auto_brt) */
1954                         i2c_w(sd, OV7610_REG_BRT, val);
1955                 break;
1956         case SEN_OV7670:
1957 /*jfm - from windblows
1958  *              i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
1959                 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
1960                 break;
1961         }
1962 /*      if (was_streaming)
1963  *              ov51x_restart(sd); */
1964 }
1965
1966 static void setcontrast(struct gspca_dev *gspca_dev)
1967 {
1968         struct sd *sd = (struct sd *) gspca_dev;
1969         int val;
1970 /*      int was_streaming; */
1971
1972         val = sd->contrast;
1973         PDEBUG(D_CONF, "contrast:%d", val);
1974 /*      was_streaming = gspca_dev->streaming;
1975         if (was_streaming)
1976                 ov51x_stop(sd); */
1977         switch (sd->sensor) {
1978         case SEN_OV7610:
1979         case SEN_OV6620:
1980                 i2c_w(sd, OV7610_REG_CNT, val);
1981                 break;
1982         case SEN_OV6630:
1983                 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
1984         case SEN_OV8610: {
1985                 static __u8 ctab[] = {
1986                         0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
1987                 };
1988
1989                 /* Use Y gamma control instead. Bit 0 enables it. */
1990                 i2c_w(sd, 0x64, ctab[val >> 5]);
1991                 break;
1992             }
1993         case SEN_OV7620: {
1994                 static __u8 ctab[] = {
1995                         0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1996                         0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1997                 };
1998
1999                 /* Use Y gamma control instead. Bit 0 enables it. */
2000                 i2c_w(sd, 0x64, ctab[val >> 4]);
2001                 break;
2002             }
2003         case SEN_OV7640:
2004                 /* Use gain control instead. */
2005                 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2006                 break;
2007         case SEN_OV7670:
2008                 /* check that this isn't just the same as ov7610 */
2009                 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2010                 break;
2011         }
2012 /*      if (was_streaming)
2013                 ov51x_restart(sd); */
2014 }
2015
2016 static void setcolors(struct gspca_dev *gspca_dev)
2017 {
2018         struct sd *sd = (struct sd *) gspca_dev;
2019         int val;
2020 /*      int was_streaming; */
2021
2022         val = sd->colors;
2023         PDEBUG(D_CONF, "saturation:%d", val);
2024 /*      was_streaming = gspca_dev->streaming;
2025         if (was_streaming)
2026                 ov51x_stop(sd); */
2027         switch (sd->sensor) {
2028         case SEN_OV8610:
2029         case SEN_OV7610:
2030         case SEN_OV76BE:
2031         case SEN_OV6620:
2032         case SEN_OV6630:
2033                 i2c_w(sd, OV7610_REG_SAT, val);
2034                 break;
2035         case SEN_OV7620:
2036                 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2037 /*              rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2038                 if (rc < 0)
2039                         goto out; */
2040                 i2c_w(sd, OV7610_REG_SAT, val);
2041                 break;
2042         case SEN_OV7640:
2043                 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2044                 break;
2045         case SEN_OV7670:
2046                 /* supported later once I work out how to do it
2047                  * transparently fail now! */
2048                 /* set REG_COM13 values for UV sat auto mode */
2049                 break;
2050         }
2051 /*      if (was_streaming)
2052                 ov51x_restart(sd); */
2053 }
2054
2055 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2056 {
2057         struct sd *sd = (struct sd *) gspca_dev;
2058
2059         sd->brightness = val;
2060         setbrightness(gspca_dev);
2061         return 0;
2062 }
2063
2064 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2065 {
2066         struct sd *sd = (struct sd *) gspca_dev;
2067
2068         *val = sd->brightness;
2069         return 0;
2070 }
2071
2072 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2073 {
2074         struct sd *sd = (struct sd *) gspca_dev;
2075
2076         sd->contrast = val;
2077         setcontrast(gspca_dev);
2078         return 0;
2079 }
2080
2081 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2082 {
2083         struct sd *sd = (struct sd *) gspca_dev;
2084
2085         *val = sd->contrast;
2086         return 0;
2087 }
2088
2089 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2090 {
2091         struct sd *sd = (struct sd *) gspca_dev;
2092
2093         sd->colors = val;
2094         setcolors(gspca_dev);
2095         return 0;
2096 }
2097
2098 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2099 {
2100         struct sd *sd = (struct sd *) gspca_dev;
2101
2102         *val = sd->colors;
2103         return 0;
2104 }
2105
2106 /* sub-driver description */
2107 static struct sd_desc sd_desc = {
2108         .name = MODULE_NAME,
2109         .ctrls = sd_ctrls,
2110         .nctrls = ARRAY_SIZE(sd_ctrls),
2111         .config = sd_config,
2112         .open = sd_open,
2113         .start = sd_start,
2114         .stopN = sd_stopN,
2115         .stop0 = sd_stop0,
2116         .close = sd_close,
2117         .pkt_scan = sd_pkt_scan,
2118 };
2119
2120 /* -- module initialisation -- */
2121 #define DVNM(name) .driver_info = (kernel_ulong_t) name
2122 static __devinitdata struct usb_device_id device_table[] = {
2123         {USB_DEVICE(0x041e, 0x4052), DVNM("Creative Live! VISTA IM")},
2124         {USB_DEVICE(0x041e, 0x405f), DVNM("Creative Live! VISTA VF0330")},
2125         {USB_DEVICE(0x041e, 0x4060), DVNM("Creative Live! VISTA VF0350")},
2126         {USB_DEVICE(0x041e, 0x4061), DVNM("Creative Live! VISTA VF0400")},
2127         {USB_DEVICE(0x041e, 0x4064), DVNM("Creative Live! VISTA VF0420")},
2128         {USB_DEVICE(0x041e, 0x4068), DVNM("Creative Live! VISTA VF0470")},
2129         {USB_DEVICE(0x045e, 0x028c), DVNM("Microsoft xbox cam")},
2130         {USB_DEVICE(0x054c, 0x0154), DVNM("Sonny toy4")},
2131         {USB_DEVICE(0x054c, 0x0155), DVNM("Sonny toy5")},
2132         {USB_DEVICE(0x05a9, 0x0519), DVNM("OmniVision")},
2133         {USB_DEVICE(0x05a9, 0x0530), DVNM("OmniVision")},
2134         {USB_DEVICE(0x05a9, 0x4519), DVNM("OmniVision")},
2135         {USB_DEVICE(0x05a9, 0x8519), DVNM("OmniVision")},
2136         {}
2137 };
2138 #undef DVNAME
2139 MODULE_DEVICE_TABLE(usb, device_table);
2140
2141 /* -- device connect -- */
2142 static int sd_probe(struct usb_interface *intf,
2143                         const struct usb_device_id *id)
2144 {
2145         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2146                                 THIS_MODULE);
2147 }
2148
2149 static struct usb_driver sd_driver = {
2150         .name = MODULE_NAME,
2151         .id_table = device_table,
2152         .probe = sd_probe,
2153         .disconnect = gspca_disconnect,
2154 };
2155
2156 /* -- module insert / remove -- */
2157 static int __init sd_mod_init(void)
2158 {
2159         if (usb_register(&sd_driver) < 0)
2160                 return -1;
2161         PDEBUG(D_PROBE, "v%s registered", version);
2162         return 0;
2163 }
2164 static void __exit sd_mod_exit(void)
2165 {
2166         usb_deregister(&sd_driver);
2167         PDEBUG(D_PROBE, "deregistered");
2168 }
2169
2170 module_init(sd_mod_init);
2171 module_exit(sd_mod_exit);
2172
2173 module_param(frame_rate, int, 0644);
2174 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");