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