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