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