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