]> Pileus Git - ~andy/linux/blob - drivers/media/video/gspca/ov519.c
V4L/DVB (12077): gspca_ov519: Fix 320x240 with ov7660 sensor
[~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, which itself
7  * was adapted from the ov511 driver.
8  *
9  * Original copyright for the ov511 driver is:
10  *
11  * Copyright (c) 1999-2004 Mark W. McClelland
12  * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
13  *
14  * ov51x-jpeg original copyright is:
15  *
16  * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
17  * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or
22  * any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32  *
33  */
34 #define MODULE_NAME "ov519"
35
36 #include "gspca.h"
37
38 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
39 MODULE_DESCRIPTION("OV519 USB Camera Driver");
40 MODULE_LICENSE("GPL");
41
42 /* global parameters */
43 static int frame_rate;
44
45 /* Number of times to retry a failed I2C transaction. Increase this if you
46  * are getting "Failed to read sensor ID..." */
47 static int i2c_detect_tries = 10;
48
49 /* ov519 device descriptor */
50 struct sd {
51         struct gspca_dev gspca_dev;             /* !! must be the first item */
52
53         __u8 packet_nr;
54
55         char bridge;
56 #define BRIDGE_OV511            0
57 #define BRIDGE_OV511PLUS        1
58 #define BRIDGE_OV518            2
59 #define BRIDGE_OV518PLUS        3
60 #define BRIDGE_OV519            4
61 #define BRIDGE_MASK             7
62
63         char invert_led;
64 #define BRIDGE_INVERT_LED       8
65
66         /* Determined by sensor type */
67         __u8 sif;
68
69         __u8 brightness;
70         __u8 contrast;
71         __u8 colors;
72         __u8 hflip;
73         __u8 vflip;
74         __u8 autobrightness;
75         __u8 freq;
76
77         __u8 stopped;           /* Streaming is temporarily paused */
78
79         __u8 frame_rate;        /* current Framerate (OV519 only) */
80         __u8 clockdiv;          /* clockdiv override for OV519 only */
81
82         char sensor;            /* Type of image sensor chip (SEN_*) */
83 #define SEN_UNKNOWN 0
84 #define SEN_OV6620 1
85 #define SEN_OV6630 2
86 #define SEN_OV66308AF 3
87 #define SEN_OV7610 4
88 #define SEN_OV7620 5
89 #define SEN_OV7640 6
90 #define SEN_OV7670 7
91 #define SEN_OV76BE 8
92 #define SEN_OV8610 9
93 };
94
95 /* V4L2 controls supported by the driver */
96 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
97 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
98 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
99 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
100 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
101 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
102 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
103 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
104 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
105 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
106 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val);
107 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val);
108 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
109 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
110 static void setbrightness(struct gspca_dev *gspca_dev);
111 static void setcontrast(struct gspca_dev *gspca_dev);
112 static void setcolors(struct gspca_dev *gspca_dev);
113 static void setautobrightness(struct sd *sd);
114 static void setfreq(struct sd *sd);
115
116 static const struct ctrl sd_ctrls[] = {
117         {
118             {
119                 .id      = V4L2_CID_BRIGHTNESS,
120                 .type    = V4L2_CTRL_TYPE_INTEGER,
121                 .name    = "Brightness",
122                 .minimum = 0,
123                 .maximum = 255,
124                 .step    = 1,
125 #define BRIGHTNESS_DEF 127
126                 .default_value = BRIGHTNESS_DEF,
127             },
128             .set = sd_setbrightness,
129             .get = sd_getbrightness,
130         },
131         {
132             {
133                 .id      = V4L2_CID_CONTRAST,
134                 .type    = V4L2_CTRL_TYPE_INTEGER,
135                 .name    = "Contrast",
136                 .minimum = 0,
137                 .maximum = 255,
138                 .step    = 1,
139 #define CONTRAST_DEF 127
140                 .default_value = CONTRAST_DEF,
141             },
142             .set = sd_setcontrast,
143             .get = sd_getcontrast,
144         },
145         {
146             {
147                 .id      = V4L2_CID_SATURATION,
148                 .type    = V4L2_CTRL_TYPE_INTEGER,
149                 .name    = "Color",
150                 .minimum = 0,
151                 .maximum = 255,
152                 .step    = 1,
153 #define COLOR_DEF 127
154                 .default_value = COLOR_DEF,
155             },
156             .set = sd_setcolors,
157             .get = sd_getcolors,
158         },
159 /* The flip controls work with ov7670 only */
160 #define HFLIP_IDX 3
161         {
162             {
163                 .id      = V4L2_CID_HFLIP,
164                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
165                 .name    = "Mirror",
166                 .minimum = 0,
167                 .maximum = 1,
168                 .step    = 1,
169 #define HFLIP_DEF 0
170                 .default_value = HFLIP_DEF,
171             },
172             .set = sd_sethflip,
173             .get = sd_gethflip,
174         },
175 #define VFLIP_IDX 4
176         {
177             {
178                 .id      = V4L2_CID_VFLIP,
179                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
180                 .name    = "Vflip",
181                 .minimum = 0,
182                 .maximum = 1,
183                 .step    = 1,
184 #define VFLIP_DEF 0
185                 .default_value = VFLIP_DEF,
186             },
187             .set = sd_setvflip,
188             .get = sd_getvflip,
189         },
190 #define AUTOBRIGHT_IDX 5
191         {
192             {
193                 .id      = V4L2_CID_AUTOBRIGHTNESS,
194                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
195                 .name    = "Auto Brightness",
196                 .minimum = 0,
197                 .maximum = 1,
198                 .step    = 1,
199 #define AUTOBRIGHT_DEF 1
200                 .default_value = AUTOBRIGHT_DEF,
201             },
202             .set = sd_setautobrightness,
203             .get = sd_getautobrightness,
204         },
205 #define FREQ_IDX 6
206         {
207             {
208                 .id      = V4L2_CID_POWER_LINE_FREQUENCY,
209                 .type    = V4L2_CTRL_TYPE_MENU,
210                 .name    = "Light frequency filter",
211                 .minimum = 0,
212                 .maximum = 2,   /* 0: 0, 1: 50Hz, 2:60Hz */
213                 .step    = 1,
214 #define FREQ_DEF 0
215                 .default_value = FREQ_DEF,
216             },
217             .set = sd_setfreq,
218             .get = sd_getfreq,
219         },
220 #define OV7670_FREQ_IDX 7
221         {
222             {
223                 .id      = V4L2_CID_POWER_LINE_FREQUENCY,
224                 .type    = V4L2_CTRL_TYPE_MENU,
225                 .name    = "Light frequency filter",
226                 .minimum = 0,
227                 .maximum = 3,   /* 0: 0, 1: 50Hz, 2:60Hz 3: Auto Hz */
228                 .step    = 1,
229 #define OV7670_FREQ_DEF 3
230                 .default_value = OV7670_FREQ_DEF,
231             },
232             .set = sd_setfreq,
233             .get = sd_getfreq,
234         },
235 };
236
237 static const struct v4l2_pix_format ov519_vga_mode[] = {
238         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
239                 .bytesperline = 320,
240                 .sizeimage = 320 * 240 * 3 / 8 + 590,
241                 .colorspace = V4L2_COLORSPACE_JPEG,
242                 .priv = 1},
243         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
244                 .bytesperline = 640,
245                 .sizeimage = 640 * 480 * 3 / 8 + 590,
246                 .colorspace = V4L2_COLORSPACE_JPEG,
247                 .priv = 0},
248 };
249 static const struct v4l2_pix_format ov519_sif_mode[] = {
250         {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
251                 .bytesperline = 160,
252                 .sizeimage = 160 * 120 * 3 / 8 + 590,
253                 .colorspace = V4L2_COLORSPACE_JPEG,
254                 .priv = 3},
255         {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
256                 .bytesperline = 176,
257                 .sizeimage = 176 * 144 * 3 / 8 + 590,
258                 .colorspace = V4L2_COLORSPACE_JPEG,
259                 .priv = 1},
260         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
261                 .bytesperline = 320,
262                 .sizeimage = 320 * 240 * 3 / 8 + 590,
263                 .colorspace = V4L2_COLORSPACE_JPEG,
264                 .priv = 2},
265         {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
266                 .bytesperline = 352,
267                 .sizeimage = 352 * 288 * 3 / 8 + 590,
268                 .colorspace = V4L2_COLORSPACE_JPEG,
269                 .priv = 0},
270 };
271
272 static const struct v4l2_pix_format ov518_vga_mode[] = {
273         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
274                 .bytesperline = 320,
275                 .sizeimage = 320 * 240 * 3 / 8 + 590,
276                 .colorspace = V4L2_COLORSPACE_JPEG,
277                 .priv = 1},
278         {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
279                 .bytesperline = 640,
280                 .sizeimage = 640 * 480 * 3 / 8 + 590,
281                 .colorspace = V4L2_COLORSPACE_JPEG,
282                 .priv = 0},
283 };
284 static const struct v4l2_pix_format ov518_sif_mode[] = {
285         {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
286                 .bytesperline = 160,
287                 .sizeimage = 40000,
288                 .colorspace = V4L2_COLORSPACE_JPEG,
289                 .priv = 3},
290         {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
291                 .bytesperline = 176,
292                 .sizeimage = 40000,
293                 .colorspace = V4L2_COLORSPACE_JPEG,
294                 .priv = 1},
295         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
296                 .bytesperline = 320,
297                 .sizeimage = 320 * 240 * 3 / 8 + 590,
298                 .colorspace = V4L2_COLORSPACE_JPEG,
299                 .priv = 2},
300         {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
301                 .bytesperline = 352,
302                 .sizeimage = 352 * 288 * 3 / 8 + 590,
303                 .colorspace = V4L2_COLORSPACE_JPEG,
304                 .priv = 0},
305 };
306
307
308 /* Registers common to OV511 / OV518 */
309 #define R51x_SYS_RESET                  0x50
310 #define R51x_SYS_INIT                   0x53
311 #define R51x_SYS_SNAP                   0x52
312 #define R51x_SYS_CUST_ID                0x5F
313 #define R51x_COMP_LUT_BEGIN             0x80
314
315 /* OV511 Camera interface register numbers */
316 #define R511_SYS_LED_CTL                0x55    /* OV511+ only */
317 #define OV511_RESET_NOREGS              0x3F    /* All but OV511 & regs */
318
319 /* OV518 Camera interface register numbers */
320 #define R518_GPIO_OUT                   0x56    /* OV518(+) only */
321 #define R518_GPIO_CTL                   0x57    /* OV518(+) only */
322
323 /* OV519 Camera interface register numbers */
324 #define OV519_R10_H_SIZE                0x10
325 #define OV519_R11_V_SIZE                0x11
326 #define OV519_R12_X_OFFSETL             0x12
327 #define OV519_R13_X_OFFSETH             0x13
328 #define OV519_R14_Y_OFFSETL             0x14
329 #define OV519_R15_Y_OFFSETH             0x15
330 #define OV519_R16_DIVIDER               0x16
331 #define OV519_R20_DFR                   0x20
332 #define OV519_R25_FORMAT                0x25
333
334 /* OV519 System Controller register numbers */
335 #define OV519_SYS_RESET1 0x51
336 #define OV519_SYS_EN_CLK1 0x54
337
338 #define OV519_GPIO_DATA_OUT0            0x71
339 #define OV519_GPIO_IO_CTRL0             0x72
340
341 #define OV511_ENDPOINT_ADDRESS  1       /* Isoc endpoint number */
342
343 /* I2C registers */
344 #define R51x_I2C_W_SID          0x41
345 #define R51x_I2C_SADDR_3        0x42
346 #define R51x_I2C_SADDR_2        0x43
347 #define R51x_I2C_R_SID          0x44
348 #define R51x_I2C_DATA           0x45
349 #define R518_I2C_CTL            0x47    /* OV518(+) only */
350
351 /* I2C ADDRESSES */
352 #define OV7xx0_SID   0x42
353 #define OV8xx0_SID   0xa0
354 #define OV6xx0_SID   0xc0
355
356 /* OV7610 registers */
357 #define OV7610_REG_GAIN         0x00    /* gain setting (5:0) */
358 #define OV7610_REG_BLUE         0x01    /* blue channel balance */
359 #define OV7610_REG_RED          0x02    /* red channel balance */
360 #define OV7610_REG_SAT          0x03    /* saturation */
361 #define OV8610_REG_HUE          0x04    /* 04 reserved */
362 #define OV7610_REG_CNT          0x05    /* Y contrast */
363 #define OV7610_REG_BRT          0x06    /* Y brightness */
364 #define OV7610_REG_COM_C        0x14    /* misc common regs */
365 #define OV7610_REG_ID_HIGH      0x1c    /* manufacturer ID MSB */
366 #define OV7610_REG_ID_LOW       0x1d    /* manufacturer ID LSB */
367 #define OV7610_REG_COM_I        0x29    /* misc settings */
368
369 /* OV7670 registers */
370 #define OV7670_REG_GAIN        0x00    /* Gain lower 8 bits (rest in vref) */
371 #define OV7670_REG_BLUE        0x01    /* blue gain */
372 #define OV7670_REG_RED         0x02    /* red gain */
373 #define OV7670_REG_VREF        0x03    /* Pieces of GAIN, VSTART, VSTOP */
374 #define OV7670_REG_COM1        0x04    /* Control 1 */
375 #define OV7670_REG_AECHH       0x07    /* AEC MS 5 bits */
376 #define OV7670_REG_COM3        0x0c    /* Control 3 */
377 #define OV7670_REG_COM4        0x0d    /* Control 4 */
378 #define OV7670_REG_COM5        0x0e    /* All "reserved" */
379 #define OV7670_REG_COM6        0x0f    /* Control 6 */
380 #define OV7670_REG_AECH        0x10    /* More bits of AEC value */
381 #define OV7670_REG_CLKRC       0x11    /* Clock control */
382 #define OV7670_REG_COM7        0x12    /* Control 7 */
383 #define   OV7670_COM7_FMT_VGA    0x00
384 #define   OV7670_COM7_YUV        0x00    /* YUV */
385 #define   OV7670_COM7_FMT_QVGA   0x10    /* QVGA format */
386 #define   OV7670_COM7_FMT_MASK   0x38
387 #define   OV7670_COM7_RESET      0x80    /* Register reset */
388 #define OV7670_REG_COM8        0x13    /* Control 8 */
389 #define   OV7670_COM8_AEC        0x01    /* Auto exposure enable */
390 #define   OV7670_COM8_AWB        0x02    /* White balance enable */
391 #define   OV7670_COM8_AGC        0x04    /* Auto gain enable */
392 #define   OV7670_COM8_BFILT      0x20    /* Band filter enable */
393 #define   OV7670_COM8_AECSTEP    0x40    /* Unlimited AEC step size */
394 #define   OV7670_COM8_FASTAEC    0x80    /* Enable fast AGC/AEC */
395 #define OV7670_REG_COM9        0x14    /* Control 9  - gain ceiling */
396 #define OV7670_REG_COM10       0x15    /* Control 10 */
397 #define OV7670_REG_HSTART      0x17    /* Horiz start high bits */
398 #define OV7670_REG_HSTOP       0x18    /* Horiz stop high bits */
399 #define OV7670_REG_VSTART      0x19    /* Vert start high bits */
400 #define OV7670_REG_VSTOP       0x1a    /* Vert stop high bits */
401 #define OV7670_REG_MVFP        0x1e    /* Mirror / vflip */
402 #define   OV7670_MVFP_VFLIP      0x10    /* vertical flip */
403 #define   OV7670_MVFP_MIRROR     0x20    /* Mirror image */
404 #define OV7670_REG_AEW         0x24    /* AGC upper limit */
405 #define OV7670_REG_AEB         0x25    /* AGC lower limit */
406 #define OV7670_REG_VPT         0x26    /* AGC/AEC fast mode op region */
407 #define OV7670_REG_HREF        0x32    /* HREF pieces */
408 #define OV7670_REG_TSLB        0x3a    /* lots of stuff */
409 #define OV7670_REG_COM11       0x3b    /* Control 11 */
410 #define   OV7670_COM11_EXP       0x02
411 #define   OV7670_COM11_HZAUTO    0x10    /* Auto detect 50/60 Hz */
412 #define OV7670_REG_COM12       0x3c    /* Control 12 */
413 #define OV7670_REG_COM13       0x3d    /* Control 13 */
414 #define   OV7670_COM13_GAMMA     0x80    /* Gamma enable */
415 #define   OV7670_COM13_UVSAT     0x40    /* UV saturation auto adjustment */
416 #define OV7670_REG_COM14       0x3e    /* Control 14 */
417 #define OV7670_REG_EDGE        0x3f    /* Edge enhancement factor */
418 #define OV7670_REG_COM15       0x40    /* Control 15 */
419 #define   OV7670_COM15_R00FF     0xc0    /*            00 to FF */
420 #define OV7670_REG_COM16       0x41    /* Control 16 */
421 #define   OV7670_COM16_AWBGAIN   0x08    /* AWB gain enable */
422 #define OV7670_REG_BRIGHT      0x55    /* Brightness */
423 #define OV7670_REG_CONTRAS     0x56    /* Contrast control */
424 #define OV7670_REG_GFIX        0x69    /* Fix gain control */
425 #define OV7670_REG_RGB444      0x8c    /* RGB 444 control */
426 #define OV7670_REG_HAECC1      0x9f    /* Hist AEC/AGC control 1 */
427 #define OV7670_REG_HAECC2      0xa0    /* Hist AEC/AGC control 2 */
428 #define OV7670_REG_BD50MAX     0xa5    /* 50hz banding step limit */
429 #define OV7670_REG_HAECC3      0xa6    /* Hist AEC/AGC control 3 */
430 #define OV7670_REG_HAECC4      0xa7    /* Hist AEC/AGC control 4 */
431 #define OV7670_REG_HAECC5      0xa8    /* Hist AEC/AGC control 5 */
432 #define OV7670_REG_HAECC6      0xa9    /* Hist AEC/AGC control 6 */
433 #define OV7670_REG_HAECC7      0xaa    /* Hist AEC/AGC control 7 */
434 #define OV7670_REG_BD60MAX     0xab    /* 60hz banding step limit */
435
436 struct ov_regvals {
437         __u8 reg;
438         __u8 val;
439 };
440 struct ov_i2c_regvals {
441         __u8 reg;
442         __u8 val;
443 };
444
445 static const struct ov_i2c_regvals norm_6x20[] = {
446         { 0x12, 0x80 }, /* reset */
447         { 0x11, 0x01 },
448         { 0x03, 0x60 },
449         { 0x05, 0x7f }, /* For when autoadjust is off */
450         { 0x07, 0xa8 },
451         /* The ratio of 0x0c and 0x0d  controls the white point */
452         { 0x0c, 0x24 },
453         { 0x0d, 0x24 },
454         { 0x0f, 0x15 }, /* COMS */
455         { 0x10, 0x75 }, /* AEC Exposure time */
456         { 0x12, 0x24 }, /* Enable AGC */
457         { 0x14, 0x04 },
458         /* 0x16: 0x06 helps frame stability with moving objects */
459         { 0x16, 0x06 },
460 /*      { 0x20, 0x30 },  * Aperture correction enable */
461         { 0x26, 0xb2 }, /* BLC enable */
462         /* 0x28: 0x05 Selects RGB format if RGB on */
463         { 0x28, 0x05 },
464         { 0x2a, 0x04 }, /* Disable framerate adjust */
465 /*      { 0x2b, 0xac },  * Framerate; Set 2a[7] first */
466         { 0x2d, 0x99 },
467         { 0x33, 0xa0 }, /* Color Processing Parameter */
468         { 0x34, 0xd2 }, /* Max A/D range */
469         { 0x38, 0x8b },
470         { 0x39, 0x40 },
471
472         { 0x3c, 0x39 }, /* Enable AEC mode changing */
473         { 0x3c, 0x3c }, /* Change AEC mode */
474         { 0x3c, 0x24 }, /* Disable AEC mode changing */
475
476         { 0x3d, 0x80 },
477         /* These next two registers (0x4a, 0x4b) are undocumented.
478          * They control the color balance */
479         { 0x4a, 0x80 },
480         { 0x4b, 0x80 },
481         { 0x4d, 0xd2 }, /* This reduces noise a bit */
482         { 0x4e, 0xc1 },
483         { 0x4f, 0x04 },
484 /* Do 50-53 have any effect? */
485 /* Toggle 0x12[2] off and on here? */
486 };
487
488 static const struct ov_i2c_regvals norm_6x30[] = {
489         { 0x12, 0x80 }, /* Reset */
490         { 0x00, 0x1f }, /* Gain */
491         { 0x01, 0x99 }, /* Blue gain */
492         { 0x02, 0x7c }, /* Red gain */
493         { 0x03, 0xc0 }, /* Saturation */
494         { 0x05, 0x0a }, /* Contrast */
495         { 0x06, 0x95 }, /* Brightness */
496         { 0x07, 0x2d }, /* Sharpness */
497         { 0x0c, 0x20 },
498         { 0x0d, 0x20 },
499         { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
500         { 0x0f, 0x05 },
501         { 0x10, 0x9a },
502         { 0x11, 0x00 }, /* Pixel clock = fastest */
503         { 0x12, 0x24 }, /* Enable AGC and AWB */
504         { 0x13, 0x21 },
505         { 0x14, 0x80 },
506         { 0x15, 0x01 },
507         { 0x16, 0x03 },
508         { 0x17, 0x38 },
509         { 0x18, 0xea },
510         { 0x19, 0x04 },
511         { 0x1a, 0x93 },
512         { 0x1b, 0x00 },
513         { 0x1e, 0xc4 },
514         { 0x1f, 0x04 },
515         { 0x20, 0x20 },
516         { 0x21, 0x10 },
517         { 0x22, 0x88 },
518         { 0x23, 0xc0 }, /* Crystal circuit power level */
519         { 0x25, 0x9a }, /* Increase AEC black ratio */
520         { 0x26, 0xb2 }, /* BLC enable */
521         { 0x27, 0xa2 },
522         { 0x28, 0x00 },
523         { 0x29, 0x00 },
524         { 0x2a, 0x84 }, /* 60 Hz power */
525         { 0x2b, 0xa8 }, /* 60 Hz power */
526         { 0x2c, 0xa0 },
527         { 0x2d, 0x95 }, /* Enable auto-brightness */
528         { 0x2e, 0x88 },
529         { 0x33, 0x26 },
530         { 0x34, 0x03 },
531         { 0x36, 0x8f },
532         { 0x37, 0x80 },
533         { 0x38, 0x83 },
534         { 0x39, 0x80 },
535         { 0x3a, 0x0f },
536         { 0x3b, 0x3c },
537         { 0x3c, 0x1a },
538         { 0x3d, 0x80 },
539         { 0x3e, 0x80 },
540         { 0x3f, 0x0e },
541         { 0x40, 0x00 }, /* White bal */
542         { 0x41, 0x00 }, /* White bal */
543         { 0x42, 0x80 },
544         { 0x43, 0x3f }, /* White bal */
545         { 0x44, 0x80 },
546         { 0x45, 0x20 },
547         { 0x46, 0x20 },
548         { 0x47, 0x80 },
549         { 0x48, 0x7f },
550         { 0x49, 0x00 },
551         { 0x4a, 0x00 },
552         { 0x4b, 0x80 },
553         { 0x4c, 0xd0 },
554         { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
555         { 0x4e, 0x40 },
556         { 0x4f, 0x07 }, /* UV avg., col. killer: max */
557         { 0x50, 0xff },
558         { 0x54, 0x23 }, /* Max AGC gain: 18dB */
559         { 0x55, 0xff },
560         { 0x56, 0x12 },
561         { 0x57, 0x81 },
562         { 0x58, 0x75 },
563         { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
564         { 0x5a, 0x2c },
565         { 0x5b, 0x0f }, /* AWB chrominance levels */
566         { 0x5c, 0x10 },
567         { 0x3d, 0x80 },
568         { 0x27, 0xa6 },
569         { 0x12, 0x20 }, /* Toggle AWB */
570         { 0x12, 0x24 },
571 };
572
573 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
574  *
575  * Register 0x0f in the 7610 has the following effects:
576  *
577  * 0x85 (AEC method 1): Best overall, good contrast range
578  * 0x45 (AEC method 2): Very overexposed
579  * 0xa5 (spec sheet default): Ok, but the black level is
580  *      shifted resulting in loss of contrast
581  * 0x05 (old driver setting): very overexposed, too much
582  *      contrast
583  */
584 static const struct ov_i2c_regvals norm_7610[] = {
585         { 0x10, 0xff },
586         { 0x16, 0x06 },
587         { 0x28, 0x24 },
588         { 0x2b, 0xac },
589         { 0x12, 0x00 },
590         { 0x38, 0x81 },
591         { 0x28, 0x24 }, /* 0c */
592         { 0x0f, 0x85 }, /* lg's setting */
593         { 0x15, 0x01 },
594         { 0x20, 0x1c },
595         { 0x23, 0x2a },
596         { 0x24, 0x10 },
597         { 0x25, 0x8a },
598         { 0x26, 0xa2 },
599         { 0x27, 0xc2 },
600         { 0x2a, 0x04 },
601         { 0x2c, 0xfe },
602         { 0x2d, 0x93 },
603         { 0x30, 0x71 },
604         { 0x31, 0x60 },
605         { 0x32, 0x26 },
606         { 0x33, 0x20 },
607         { 0x34, 0x48 },
608         { 0x12, 0x24 },
609         { 0x11, 0x01 },
610         { 0x0c, 0x24 },
611         { 0x0d, 0x24 },
612 };
613
614 static const struct ov_i2c_regvals norm_7620[] = {
615         { 0x00, 0x00 },         /* gain */
616         { 0x01, 0x80 },         /* blue gain */
617         { 0x02, 0x80 },         /* red gain */
618         { 0x03, 0xc0 },         /* OV7670_REG_VREF */
619         { 0x06, 0x60 },
620         { 0x07, 0x00 },
621         { 0x0c, 0x24 },
622         { 0x0c, 0x24 },
623         { 0x0d, 0x24 },
624         { 0x11, 0x01 },
625         { 0x12, 0x24 },
626         { 0x13, 0x01 },
627         { 0x14, 0x84 },
628         { 0x15, 0x01 },
629         { 0x16, 0x03 },
630         { 0x17, 0x2f },
631         { 0x18, 0xcf },
632         { 0x19, 0x06 },
633         { 0x1a, 0xf5 },
634         { 0x1b, 0x00 },
635         { 0x20, 0x18 },
636         { 0x21, 0x80 },
637         { 0x22, 0x80 },
638         { 0x23, 0x00 },
639         { 0x26, 0xa2 },
640         { 0x27, 0xea },
641         { 0x28, 0x20 },
642         { 0x29, 0x00 },
643         { 0x2a, 0x10 },
644         { 0x2b, 0x00 },
645         { 0x2c, 0x88 },
646         { 0x2d, 0x91 },
647         { 0x2e, 0x80 },
648         { 0x2f, 0x44 },
649         { 0x60, 0x27 },
650         { 0x61, 0x02 },
651         { 0x62, 0x5f },
652         { 0x63, 0xd5 },
653         { 0x64, 0x57 },
654         { 0x65, 0x83 },
655         { 0x66, 0x55 },
656         { 0x67, 0x92 },
657         { 0x68, 0xcf },
658         { 0x69, 0x76 },
659         { 0x6a, 0x22 },
660         { 0x6b, 0x00 },
661         { 0x6c, 0x02 },
662         { 0x6d, 0x44 },
663         { 0x6e, 0x80 },
664         { 0x6f, 0x1d },
665         { 0x70, 0x8b },
666         { 0x71, 0x00 },
667         { 0x72, 0x14 },
668         { 0x73, 0x54 },
669         { 0x74, 0x00 },
670         { 0x75, 0x8e },
671         { 0x76, 0x00 },
672         { 0x77, 0xff },
673         { 0x78, 0x80 },
674         { 0x79, 0x80 },
675         { 0x7a, 0x80 },
676         { 0x7b, 0xe2 },
677         { 0x7c, 0x00 },
678 };
679
680 /* 7640 and 7648. The defaults should be OK for most registers. */
681 static const struct ov_i2c_regvals norm_7640[] = {
682         { 0x12, 0x80 },
683         { 0x12, 0x14 },
684 };
685
686 /* 7670. Defaults taken from OmniVision provided data,
687 *  as provided by Jonathan Corbet of OLPC               */
688 static const struct ov_i2c_regvals norm_7670[] = {
689         { OV7670_REG_COM7, OV7670_COM7_RESET },
690         { OV7670_REG_TSLB, 0x04 },              /* OV */
691         { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
692         { OV7670_REG_CLKRC, 0x01 },
693 /*
694  * Set the hardware window.  These values from OV don't entirely
695  * make sense - hstop is less than hstart.  But they work...
696  */
697         { OV7670_REG_HSTART, 0x13 },
698         { OV7670_REG_HSTOP, 0x01 },
699         { OV7670_REG_HREF, 0xb6 },
700         { OV7670_REG_VSTART, 0x02 },
701         { OV7670_REG_VSTOP, 0x7a },
702         { OV7670_REG_VREF, 0x0a },
703
704         { OV7670_REG_COM3, 0x00 },
705         { OV7670_REG_COM14, 0x00 },
706 /* Mystery scaling numbers */
707         { 0x70, 0x3a },
708         { 0x71, 0x35 },
709         { 0x72, 0x11 },
710         { 0x73, 0xf0 },
711         { 0xa2, 0x02 },
712 /*      { OV7670_REG_COM10, 0x0 }, */
713
714 /* Gamma curve values */
715         { 0x7a, 0x20 },
716         { 0x7b, 0x10 },
717         { 0x7c, 0x1e },
718         { 0x7d, 0x35 },
719         { 0x7e, 0x5a },
720         { 0x7f, 0x69 },
721         { 0x80, 0x76 },
722         { 0x81, 0x80 },
723         { 0x82, 0x88 },
724         { 0x83, 0x8f },
725         { 0x84, 0x96 },
726         { 0x85, 0xa3 },
727         { 0x86, 0xaf },
728         { 0x87, 0xc4 },
729         { 0x88, 0xd7 },
730         { 0x89, 0xe8 },
731
732 /* AGC and AEC parameters.  Note we start by disabling those features,
733    then turn them only after tweaking the values. */
734         { OV7670_REG_COM8, OV7670_COM8_FASTAEC
735                          | OV7670_COM8_AECSTEP
736                          | OV7670_COM8_BFILT },
737         { OV7670_REG_GAIN, 0x00 },
738         { OV7670_REG_AECH, 0x00 },
739         { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
740         { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
741         { OV7670_REG_BD50MAX, 0x05 },
742         { OV7670_REG_BD60MAX, 0x07 },
743         { OV7670_REG_AEW, 0x95 },
744         { OV7670_REG_AEB, 0x33 },
745         { OV7670_REG_VPT, 0xe3 },
746         { OV7670_REG_HAECC1, 0x78 },
747         { OV7670_REG_HAECC2, 0x68 },
748         { 0xa1, 0x03 }, /* magic */
749         { OV7670_REG_HAECC3, 0xd8 },
750         { OV7670_REG_HAECC4, 0xd8 },
751         { OV7670_REG_HAECC5, 0xf0 },
752         { OV7670_REG_HAECC6, 0x90 },
753         { OV7670_REG_HAECC7, 0x94 },
754         { OV7670_REG_COM8, OV7670_COM8_FASTAEC
755                         | OV7670_COM8_AECSTEP
756                         | OV7670_COM8_BFILT
757                         | OV7670_COM8_AGC
758                         | OV7670_COM8_AEC },
759
760 /* Almost all of these are magic "reserved" values.  */
761         { OV7670_REG_COM5, 0x61 },
762         { OV7670_REG_COM6, 0x4b },
763         { 0x16, 0x02 },
764         { OV7670_REG_MVFP, 0x07 },
765         { 0x21, 0x02 },
766         { 0x22, 0x91 },
767         { 0x29, 0x07 },
768         { 0x33, 0x0b },
769         { 0x35, 0x0b },
770         { 0x37, 0x1d },
771         { 0x38, 0x71 },
772         { 0x39, 0x2a },
773         { OV7670_REG_COM12, 0x78 },
774         { 0x4d, 0x40 },
775         { 0x4e, 0x20 },
776         { OV7670_REG_GFIX, 0x00 },
777         { 0x6b, 0x4a },
778         { 0x74, 0x10 },
779         { 0x8d, 0x4f },
780         { 0x8e, 0x00 },
781         { 0x8f, 0x00 },
782         { 0x90, 0x00 },
783         { 0x91, 0x00 },
784         { 0x96, 0x00 },
785         { 0x9a, 0x00 },
786         { 0xb0, 0x84 },
787         { 0xb1, 0x0c },
788         { 0xb2, 0x0e },
789         { 0xb3, 0x82 },
790         { 0xb8, 0x0a },
791
792 /* More reserved magic, some of which tweaks white balance */
793         { 0x43, 0x0a },
794         { 0x44, 0xf0 },
795         { 0x45, 0x34 },
796         { 0x46, 0x58 },
797         { 0x47, 0x28 },
798         { 0x48, 0x3a },
799         { 0x59, 0x88 },
800         { 0x5a, 0x88 },
801         { 0x5b, 0x44 },
802         { 0x5c, 0x67 },
803         { 0x5d, 0x49 },
804         { 0x5e, 0x0e },
805         { 0x6c, 0x0a },
806         { 0x6d, 0x55 },
807         { 0x6e, 0x11 },
808         { 0x6f, 0x9f },
809                                         /* "9e for advance AWB" */
810         { 0x6a, 0x40 },
811         { OV7670_REG_BLUE, 0x40 },
812         { OV7670_REG_RED, 0x60 },
813         { OV7670_REG_COM8, OV7670_COM8_FASTAEC
814                         | OV7670_COM8_AECSTEP
815                         | OV7670_COM8_BFILT
816                         | OV7670_COM8_AGC
817                         | OV7670_COM8_AEC
818                         | OV7670_COM8_AWB },
819
820 /* Matrix coefficients */
821         { 0x4f, 0x80 },
822         { 0x50, 0x80 },
823         { 0x51, 0x00 },
824         { 0x52, 0x22 },
825         { 0x53, 0x5e },
826         { 0x54, 0x80 },
827         { 0x58, 0x9e },
828
829         { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
830         { OV7670_REG_EDGE, 0x00 },
831         { 0x75, 0x05 },
832         { 0x76, 0xe1 },
833         { 0x4c, 0x00 },
834         { 0x77, 0x01 },
835         { OV7670_REG_COM13, OV7670_COM13_GAMMA
836                           | OV7670_COM13_UVSAT
837                           | 2},         /* was 3 */
838         { 0x4b, 0x09 },
839         { 0xc9, 0x60 },
840         { OV7670_REG_COM16, 0x38 },
841         { 0x56, 0x40 },
842
843         { 0x34, 0x11 },
844         { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
845         { 0xa4, 0x88 },
846         { 0x96, 0x00 },
847         { 0x97, 0x30 },
848         { 0x98, 0x20 },
849         { 0x99, 0x30 },
850         { 0x9a, 0x84 },
851         { 0x9b, 0x29 },
852         { 0x9c, 0x03 },
853         { 0x9d, 0x4c },
854         { 0x9e, 0x3f },
855         { 0x78, 0x04 },
856
857 /* Extra-weird stuff.  Some sort of multiplexor register */
858         { 0x79, 0x01 },
859         { 0xc8, 0xf0 },
860         { 0x79, 0x0f },
861         { 0xc8, 0x00 },
862         { 0x79, 0x10 },
863         { 0xc8, 0x7e },
864         { 0x79, 0x0a },
865         { 0xc8, 0x80 },
866         { 0x79, 0x0b },
867         { 0xc8, 0x01 },
868         { 0x79, 0x0c },
869         { 0xc8, 0x0f },
870         { 0x79, 0x0d },
871         { 0xc8, 0x20 },
872         { 0x79, 0x09 },
873         { 0xc8, 0x80 },
874         { 0x79, 0x02 },
875         { 0xc8, 0xc0 },
876         { 0x79, 0x03 },
877         { 0xc8, 0x40 },
878         { 0x79, 0x05 },
879         { 0xc8, 0x30 },
880         { 0x79, 0x26 },
881 };
882
883 static const struct ov_i2c_regvals norm_8610[] = {
884         { 0x12, 0x80 },
885         { 0x00, 0x00 },
886         { 0x01, 0x80 },
887         { 0x02, 0x80 },
888         { 0x03, 0xc0 },
889         { 0x04, 0x30 },
890         { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
891         { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
892         { 0x0a, 0x86 },
893         { 0x0b, 0xb0 },
894         { 0x0c, 0x20 },
895         { 0x0d, 0x20 },
896         { 0x11, 0x01 },
897         { 0x12, 0x25 },
898         { 0x13, 0x01 },
899         { 0x14, 0x04 },
900         { 0x15, 0x01 }, /* Lin and Win think different about UV order */
901         { 0x16, 0x03 },
902         { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
903         { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
904         { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
905         { 0x1a, 0xf5 },
906         { 0x1b, 0x00 },
907         { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
908         { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
909         { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
910         { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
911         { 0x26, 0xa2 },
912         { 0x27, 0xea },
913         { 0x28, 0x00 },
914         { 0x29, 0x00 },
915         { 0x2a, 0x80 },
916         { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
917         { 0x2c, 0xac },
918         { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
919         { 0x2e, 0x80 },
920         { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
921         { 0x4c, 0x00 },
922         { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
923         { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
924         { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
925         { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
926         { 0x63, 0xff },
927         { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
928                          * maybe thats wrong */
929         { 0x65, 0x00 },
930         { 0x66, 0x55 },
931         { 0x67, 0xb0 },
932         { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
933         { 0x69, 0x02 },
934         { 0x6a, 0x22 },
935         { 0x6b, 0x00 },
936         { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
937                          * deleting bit7 colors the first images red */
938         { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
939         { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
940         { 0x6f, 0x01 },
941         { 0x70, 0x8b },
942         { 0x71, 0x00 },
943         { 0x72, 0x14 },
944         { 0x73, 0x54 },
945         { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
946         { 0x75, 0x0e },
947         { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
948         { 0x77, 0xff },
949         { 0x78, 0x80 },
950         { 0x79, 0x80 },
951         { 0x7a, 0x80 },
952         { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
953         { 0x7c, 0x00 },
954         { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
955         { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
956         { 0x7f, 0xfb },
957         { 0x80, 0x28 },
958         { 0x81, 0x00 },
959         { 0x82, 0x23 },
960         { 0x83, 0x0b },
961         { 0x84, 0x00 },
962         { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
963         { 0x86, 0xc9 },
964         { 0x87, 0x00 },
965         { 0x88, 0x00 },
966         { 0x89, 0x01 },
967         { 0x12, 0x20 },
968         { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
969 };
970
971 static unsigned char ov7670_abs_to_sm(unsigned char v)
972 {
973         if (v > 127)
974                 return v & 0x7f;
975         return (128 - v) | 0x80;
976 }
977
978 /* Write a OV519 register */
979 static int reg_w(struct sd *sd, __u16 index, __u8 value)
980 {
981         int ret;
982         int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 2 : 1;
983
984         sd->gspca_dev.usb_buf[0] = value;
985         ret = usb_control_msg(sd->gspca_dev.dev,
986                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
987                         req,
988                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
989                         0, index,
990                         sd->gspca_dev.usb_buf, 1, 500);
991         if (ret < 0)
992                 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
993         return ret;
994 }
995
996 /* Read from a OV519 register */
997 /* returns: negative is error, pos or zero is data */
998 static int reg_r(struct sd *sd, __u16 index)
999 {
1000         int ret;
1001         int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 3 : 1;
1002
1003         ret = usb_control_msg(sd->gspca_dev.dev,
1004                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1005                         req,
1006                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1007                         0, index, sd->gspca_dev.usb_buf, 1, 500);
1008
1009         if (ret >= 0)
1010                 ret = sd->gspca_dev.usb_buf[0];
1011         else
1012                 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
1013         return ret;
1014 }
1015
1016 /* Read 8 values from a OV519 register */
1017 static int reg_r8(struct sd *sd,
1018                   __u16 index)
1019 {
1020         int ret;
1021
1022         ret = usb_control_msg(sd->gspca_dev.dev,
1023                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1024                         1,                      /* REQ_IO */
1025                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1026                         0, index, sd->gspca_dev.usb_buf, 8, 500);
1027
1028         if (ret >= 0)
1029                 ret = sd->gspca_dev.usb_buf[0];
1030         else
1031                 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
1032         return ret;
1033 }
1034
1035 /*
1036  * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1037  * the same position as 1's in "mask" are cleared and set to "value". Bits
1038  * that are in the same position as 0's in "mask" are preserved, regardless
1039  * of their respective state in "value".
1040  */
1041 static int reg_w_mask(struct sd *sd,
1042                         __u16 index,
1043                         __u8 value,
1044                         __u8 mask)
1045 {
1046         int ret;
1047         __u8 oldval;
1048
1049         if (mask != 0xff) {
1050                 value &= mask;                  /* Enforce mask on value */
1051                 ret = reg_r(sd, index);
1052                 if (ret < 0)
1053                         return ret;
1054
1055                 oldval = ret & ~mask;           /* Clear the masked bits */
1056                 value |= oldval;                /* Set the desired bits */
1057         }
1058         return reg_w(sd, index, value);
1059 }
1060
1061 /*
1062  * Writes multiple (n) byte value to a single register. Only valid with certain
1063  * registers (0x30 and 0xc4 - 0xce).
1064  */
1065 static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1066 {
1067         int ret;
1068
1069         *((u32 *)sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
1070
1071         ret = usb_control_msg(sd->gspca_dev.dev,
1072                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1073                         1 /* REG_IO */,
1074                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1075                         0, index,
1076                         sd->gspca_dev.usb_buf, n, 500);
1077         if (ret < 0)
1078                 PDEBUG(D_ERR, "Write reg32 [%02x] %08x failed", index, value);
1079         return ret;
1080 }
1081
1082
1083 /*
1084  * The OV518 I2C I/O procedure is different, hence, this function.
1085  * This is normally only called from i2c_w(). Note that this function
1086  * always succeeds regardless of whether the sensor is present and working.
1087  */
1088 static int i2c_w(struct sd *sd,
1089                 __u8 reg,
1090                 __u8 value)
1091 {
1092         int rc;
1093
1094         PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
1095
1096         /* Select camera register */
1097         rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
1098         if (rc < 0)
1099                 return rc;
1100
1101         /* Write "value" to I2C data port of OV511 */
1102         rc = reg_w(sd, R51x_I2C_DATA, value);
1103         if (rc < 0)
1104                 return rc;
1105
1106         /* Initiate 3-byte write cycle */
1107         rc = reg_w(sd, R518_I2C_CTL, 0x01);
1108         if (rc < 0)
1109                 return rc;
1110
1111         /* wait for write complete */
1112         msleep(4);
1113         return reg_r8(sd, R518_I2C_CTL);
1114 }
1115
1116 /*
1117  * returns: negative is error, pos or zero is data
1118  *
1119  * The OV518 I2C I/O procedure is different, hence, this function.
1120  * This is normally only called from i2c_r(). Note that this function
1121  * always succeeds regardless of whether the sensor is present and working.
1122  */
1123 static int i2c_r(struct sd *sd, __u8 reg)
1124 {
1125         int rc, value;
1126
1127         /* Select camera register */
1128         rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
1129         if (rc < 0)
1130                 return rc;
1131
1132         /* Initiate 2-byte write cycle */
1133         rc = reg_w(sd, R518_I2C_CTL, 0x03);
1134         if (rc < 0)
1135                 return rc;
1136
1137         /* Initiate 2-byte read cycle */
1138         rc = reg_w(sd, R518_I2C_CTL, 0x05);
1139         if (rc < 0)
1140                 return rc;
1141         value = reg_r(sd, R51x_I2C_DATA);
1142         PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
1143         return value;
1144 }
1145
1146 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
1147  * the same position as 1's in "mask" are cleared and set to "value". Bits
1148  * that are in the same position as 0's in "mask" are preserved, regardless
1149  * of their respective state in "value".
1150  */
1151 static int i2c_w_mask(struct sd *sd,
1152                    __u8 reg,
1153                    __u8 value,
1154                    __u8 mask)
1155 {
1156         int rc;
1157         __u8 oldval;
1158
1159         value &= mask;                  /* Enforce mask on value */
1160         rc = i2c_r(sd, reg);
1161         if (rc < 0)
1162                 return rc;
1163         oldval = rc & ~mask;            /* Clear the masked bits */
1164         value |= oldval;                /* Set the desired bits */
1165         return i2c_w(sd, reg, value);
1166 }
1167
1168 /* Temporarily stops OV511 from functioning. Must do this before changing
1169  * registers while the camera is streaming */
1170 static inline int ov51x_stop(struct sd *sd)
1171 {
1172         PDEBUG(D_STREAM, "stopping");
1173         sd->stopped = 1;
1174         switch (sd->bridge) {
1175         case BRIDGE_OV511:
1176         case BRIDGE_OV511PLUS:
1177                 return reg_w(sd, R51x_SYS_RESET, 0x3d);
1178         case BRIDGE_OV518:
1179         case BRIDGE_OV518PLUS:
1180                 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
1181         case BRIDGE_OV519:
1182                 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
1183         }
1184
1185         return 0;
1186 }
1187
1188 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1189  * actually stopped (for performance). */
1190 static inline int ov51x_restart(struct sd *sd)
1191 {
1192         int rc;
1193
1194         PDEBUG(D_STREAM, "restarting");
1195         if (!sd->stopped)
1196                 return 0;
1197         sd->stopped = 0;
1198
1199         /* Reinitialize the stream */
1200         switch (sd->bridge) {
1201         case BRIDGE_OV511:
1202         case BRIDGE_OV511PLUS:
1203                 return reg_w(sd, R51x_SYS_RESET, 0x00);
1204         case BRIDGE_OV518:
1205         case BRIDGE_OV518PLUS:
1206                 rc = reg_w(sd, 0x2f, 0x80);
1207                 if (rc < 0)
1208                         return rc;
1209                 return reg_w(sd, R51x_SYS_RESET, 0x00);
1210         case BRIDGE_OV519:
1211                 return reg_w(sd, OV519_SYS_RESET1, 0x00);
1212         }
1213
1214         return 0;
1215 }
1216
1217 /* This does an initial reset of an OmniVision sensor and ensures that I2C
1218  * is synchronized. Returns <0 on failure.
1219  */
1220 static int init_ov_sensor(struct sd *sd)
1221 {
1222         int i;
1223
1224         /* Reset the sensor */
1225         if (i2c_w(sd, 0x12, 0x80) < 0)
1226                 return -EIO;
1227
1228         /* Wait for it to initialize */
1229         msleep(150);
1230
1231         for (i = 0; i < i2c_detect_tries; i++) {
1232                 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
1233                     i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
1234                         PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
1235                         return 0;
1236                 }
1237
1238                 /* Reset the sensor */
1239                 if (i2c_w(sd, 0x12, 0x80) < 0)
1240                         return -EIO;
1241                 /* Wait for it to initialize */
1242                 msleep(150);
1243                 /* Dummy read to sync I2C */
1244                 if (i2c_r(sd, 0x00) < 0)
1245                         return -EIO;
1246         }
1247         return -EIO;
1248 }
1249
1250 /* Set the read and write slave IDs. The "slave" argument is the write slave,
1251  * and the read slave will be set to (slave + 1).
1252  * This should not be called from outside the i2c I/O functions.
1253  * Sets I2C read and write slave IDs. Returns <0 for error
1254  */
1255 static int ov51x_set_slave_ids(struct sd *sd,
1256                                 __u8 slave)
1257 {
1258         int rc;
1259
1260         rc = reg_w(sd, R51x_I2C_W_SID, slave);
1261         if (rc < 0)
1262                 return rc;
1263         return reg_w(sd, R51x_I2C_R_SID, slave + 1);
1264 }
1265
1266 static int write_regvals(struct sd *sd,
1267                          const struct ov_regvals *regvals,
1268                          int n)
1269 {
1270         int rc;
1271
1272         while (--n >= 0) {
1273                 rc = reg_w(sd, regvals->reg, regvals->val);
1274                 if (rc < 0)
1275                         return rc;
1276                 regvals++;
1277         }
1278         return 0;
1279 }
1280
1281 static int write_i2c_regvals(struct sd *sd,
1282                              const struct ov_i2c_regvals *regvals,
1283                              int n)
1284 {
1285         int rc;
1286
1287         while (--n >= 0) {
1288                 rc = i2c_w(sd, regvals->reg, regvals->val);
1289                 if (rc < 0)
1290                         return rc;
1291                 regvals++;
1292         }
1293         return 0;
1294 }
1295
1296 /****************************************************************************
1297  *
1298  * OV511 and sensor configuration
1299  *
1300  ***************************************************************************/
1301
1302 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
1303  * the same register settings as the OV8610, since they are very similar.
1304  */
1305 static int ov8xx0_configure(struct sd *sd)
1306 {
1307         int rc;
1308
1309         PDEBUG(D_PROBE, "starting ov8xx0 configuration");
1310
1311         /* Detect sensor (sub)type */
1312         rc = i2c_r(sd, OV7610_REG_COM_I);
1313         if (rc < 0) {
1314                 PDEBUG(D_ERR, "Error detecting sensor type");
1315                 return -1;
1316         }
1317         if ((rc & 3) == 1) {
1318                 sd->sensor = SEN_OV8610;
1319         } else {
1320                 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1321                 return -1;
1322         }
1323
1324         /* Set sensor-specific vars */
1325 /*      sd->sif = 0;            already done */
1326         return 0;
1327 }
1328
1329 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
1330  * the same register settings as the OV7610, since they are very similar.
1331  */
1332 static int ov7xx0_configure(struct sd *sd)
1333 {
1334         int rc, high, low;
1335
1336
1337         PDEBUG(D_PROBE, "starting OV7xx0 configuration");
1338
1339         /* Detect sensor (sub)type */
1340         rc = i2c_r(sd, OV7610_REG_COM_I);
1341
1342         /* add OV7670 here
1343          * it appears to be wrongly detected as a 7610 by default */
1344         if (rc < 0) {
1345                 PDEBUG(D_ERR, "Error detecting sensor type");
1346                 return -1;
1347         }
1348         if ((rc & 3) == 3) {
1349                 /* quick hack to make OV7670s work */
1350                 high = i2c_r(sd, 0x0a);
1351                 low = i2c_r(sd, 0x0b);
1352                 /* info("%x, %x", high, low); */
1353                 if (high == 0x76 && low == 0x73) {
1354                         PDEBUG(D_PROBE, "Sensor is an OV7670");
1355                         sd->sensor = SEN_OV7670;
1356                 } else {
1357                         PDEBUG(D_PROBE, "Sensor is an OV7610");
1358                         sd->sensor = SEN_OV7610;
1359                 }
1360         } else if ((rc & 3) == 1) {
1361                 /* I don't know what's different about the 76BE yet. */
1362                 if (i2c_r(sd, 0x15) & 1)
1363                         PDEBUG(D_PROBE, "Sensor is an OV7620AE");
1364                 else
1365                         PDEBUG(D_PROBE, "Sensor is an OV76BE");
1366
1367                 /* OV511+ will return all zero isoc data unless we
1368                  * configure the sensor as a 7620. Someone needs to
1369                  * find the exact reg. setting that causes this. */
1370                 sd->sensor = SEN_OV76BE;
1371         } else if ((rc & 3) == 0) {
1372                 /* try to read product id registers */
1373                 high = i2c_r(sd, 0x0a);
1374                 if (high < 0) {
1375                         PDEBUG(D_ERR, "Error detecting camera chip PID");
1376                         return high;
1377                 }
1378                 low = i2c_r(sd, 0x0b);
1379                 if (low < 0) {
1380                         PDEBUG(D_ERR, "Error detecting camera chip VER");
1381                         return low;
1382                 }
1383                 if (high == 0x76) {
1384                         switch (low) {
1385                         case 0x30:
1386                                 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1387                                 PDEBUG(D_ERR,
1388                                       "7630 is not supported by this driver");
1389                                 return -1;
1390                         case 0x40:
1391                                 PDEBUG(D_PROBE, "Sensor is an OV7645");
1392                                 sd->sensor = SEN_OV7640; /* FIXME */
1393                                 break;
1394                         case 0x45:
1395                                 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1396                                 sd->sensor = SEN_OV7640; /* FIXME */
1397                                 break;
1398                         case 0x48:
1399                                 PDEBUG(D_PROBE, "Sensor is an OV7648");
1400                                 sd->sensor = SEN_OV7640; /* FIXME */
1401                                 break;
1402                         default:
1403                                 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
1404                                 return -1;
1405                         }
1406                 } else {
1407                         PDEBUG(D_PROBE, "Sensor is an OV7620");
1408                         sd->sensor = SEN_OV7620;
1409                 }
1410         } else {
1411                 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1412                 return -1;
1413         }
1414
1415         /* Set sensor-specific vars */
1416 /*      sd->sif = 0;            already done */
1417         return 0;
1418 }
1419
1420 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1421 static int ov6xx0_configure(struct sd *sd)
1422 {
1423         int rc;
1424         PDEBUG(D_PROBE, "starting OV6xx0 configuration");
1425
1426         /* Detect sensor (sub)type */
1427         rc = i2c_r(sd, OV7610_REG_COM_I);
1428         if (rc < 0) {
1429                 PDEBUG(D_ERR, "Error detecting sensor type");
1430                 return -1;
1431         }
1432
1433         /* Ugh. The first two bits are the version bits, but
1434          * the entire register value must be used. I guess OVT
1435          * underestimated how many variants they would make. */
1436         switch (rc) {
1437         case 0x00:
1438                 sd->sensor = SEN_OV6630;
1439                 PDEBUG(D_ERR,
1440                         "WARNING: Sensor is an OV66308. Your camera may have");
1441                 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1442                 break;
1443         case 0x01:
1444                 sd->sensor = SEN_OV6620;
1445                 PDEBUG(D_PROBE, "Sensor is an OV6620");
1446                 break;
1447         case 0x02:
1448                 sd->sensor = SEN_OV6630;
1449                 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1450                 break;
1451         case 0x03:
1452                 sd->sensor = SEN_OV66308AF;
1453                 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1454                 break;
1455         case 0x90:
1456                 sd->sensor = SEN_OV6630;
1457                 PDEBUG(D_ERR,
1458                         "WARNING: Sensor is an OV66307. Your camera may have");
1459                 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1460                 break;
1461         default:
1462                 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1463                 return -1;
1464         }
1465
1466         /* Set sensor-specific vars */
1467         sd->sif = 1;
1468
1469         return 0;
1470 }
1471
1472 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1473 static void ov51x_led_control(struct sd *sd, int on)
1474 {
1475         if (sd->invert_led)
1476                 on = !on;
1477
1478         switch (sd->bridge) {
1479         /* OV511 has no LED control */
1480         case BRIDGE_OV511PLUS:
1481                 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
1482                 break;
1483         case BRIDGE_OV518:
1484         case BRIDGE_OV518PLUS:
1485                 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
1486                 break;
1487         case BRIDGE_OV519:
1488                 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1);   /* 0 / 1 */
1489                 break;
1490         }
1491 }
1492
1493 /* OV518 quantization tables are 8x4 (instead of 8x8) */
1494 static int ov518_upload_quan_tables(struct sd *sd)
1495 {
1496         const unsigned char yQuanTable518[] = {
1497                 5, 4, 5, 6, 6, 7, 7, 7,
1498                 5, 5, 5, 5, 6, 7, 7, 7,
1499                 6, 6, 6, 6, 7, 7, 7, 8,
1500                 7, 7, 6, 7, 7, 7, 8, 8
1501         };
1502
1503         const unsigned char uvQuanTable518[] = {
1504                 6, 6, 6, 7, 7, 7, 7, 7,
1505                 6, 6, 6, 7, 7, 7, 7, 7,
1506                 6, 6, 6, 7, 7, 7, 7, 8,
1507                 7, 7, 7, 7, 7, 7, 8, 8
1508         };
1509
1510         const unsigned char *pYTable = yQuanTable518;
1511         const unsigned char *pUVTable = uvQuanTable518;
1512         unsigned char val0, val1;
1513         int i, rc, reg = R51x_COMP_LUT_BEGIN;
1514
1515         PDEBUG(D_PROBE, "Uploading quantization tables");
1516
1517         for (i = 0; i < 16; i++) {
1518                 val0 = *pYTable++;
1519                 val1 = *pYTable++;
1520                 val0 &= 0x0f;
1521                 val1 &= 0x0f;
1522                 val0 |= val1 << 4;
1523                 rc = reg_w(sd, reg, val0);
1524                 if (rc < 0)
1525                         return rc;
1526
1527                 val0 = *pUVTable++;
1528                 val1 = *pUVTable++;
1529                 val0 &= 0x0f;
1530                 val1 &= 0x0f;
1531                 val0 |= val1 << 4;
1532                 rc = reg_w(sd, reg + 16, val0);
1533                 if (rc < 0)
1534                         return rc;
1535
1536                 reg++;
1537         }
1538
1539         return 0;
1540 }
1541
1542 /* This initializes the OV518/OV518+ and the sensor */
1543 static int ov518_configure(struct gspca_dev *gspca_dev)
1544 {
1545         struct sd *sd = (struct sd *) gspca_dev;
1546         int rc;
1547
1548         /* For 518 and 518+ */
1549         static struct ov_regvals init_518[] = {
1550                 { R51x_SYS_RESET,       0x40 },
1551                 { R51x_SYS_INIT,        0xe1 },
1552                 { R51x_SYS_RESET,       0x3e },
1553                 { R51x_SYS_INIT,        0xe1 },
1554                 { R51x_SYS_RESET,       0x00 },
1555                 { R51x_SYS_INIT,        0xe1 },
1556                 { 0x46,                 0x00 },
1557                 { 0x5d,                 0x03 },
1558         };
1559
1560         static struct ov_regvals norm_518[] = {
1561                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
1562                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
1563                 { 0x31,                 0x0f },
1564                 { 0x5d,                 0x03 },
1565                 { 0x24,                 0x9f },
1566                 { 0x25,                 0x90 },
1567                 { 0x20,                 0x00 },
1568                 { 0x51,                 0x04 },
1569                 { 0x71,                 0x19 },
1570                 { 0x2f,                 0x80 },
1571         };
1572
1573         static struct ov_regvals norm_518_p[] = {
1574                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
1575                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
1576                 { 0x31,                 0x0f },
1577                 { 0x5d,                 0x03 },
1578                 { 0x24,                 0x9f },
1579                 { 0x25,                 0x90 },
1580                 { 0x20,                 0x60 },
1581                 { 0x51,                 0x02 },
1582                 { 0x71,                 0x19 },
1583                 { 0x40,                 0xff },
1584                 { 0x41,                 0x42 },
1585                 { 0x46,                 0x00 },
1586                 { 0x33,                 0x04 },
1587                 { 0x21,                 0x19 },
1588                 { 0x3f,                 0x10 },
1589                 { 0x2f,                 0x80 },
1590         };
1591
1592         /* First 5 bits of custom ID reg are a revision ID on OV518 */
1593         PDEBUG(D_PROBE, "Device revision %d",
1594                0x1F & reg_r(sd, R51x_SYS_CUST_ID));
1595
1596         rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
1597         if (rc < 0)
1598                 return rc;
1599
1600         /* Set LED GPIO pin to output mode */
1601         rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
1602         if (rc < 0)
1603                 return rc;
1604
1605         switch (sd->bridge) {
1606         case BRIDGE_OV518:
1607                 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
1608                 if (rc < 0)
1609                         return rc;
1610                 break;
1611         case BRIDGE_OV518PLUS:
1612                 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
1613                 if (rc < 0)
1614                         return rc;
1615                 break;
1616         }
1617
1618         rc = ov518_upload_quan_tables(sd);
1619         if (rc < 0) {
1620                 PDEBUG(D_ERR, "Error uploading quantization tables");
1621                 return rc;
1622         }
1623
1624         rc = reg_w(sd, 0x2f, 0x80);
1625         if (rc < 0)
1626                 return rc;
1627
1628         return 0;
1629 }
1630
1631 static int ov519_configure(struct sd *sd)
1632 {
1633         static const struct ov_regvals init_519[] = {
1634                 { 0x5a,  0x6d }, /* EnableSystem */
1635                 { 0x53,  0x9b },
1636                 { 0x54,  0xff }, /* set bit2 to enable jpeg */
1637                 { 0x5d,  0x03 },
1638                 { 0x49,  0x01 },
1639                 { 0x48,  0x00 },
1640                 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1641                  * detection will fail. This deserves further investigation. */
1642                 { OV519_GPIO_IO_CTRL0,   0xee },
1643                 { 0x51,  0x0f }, /* SetUsbInit */
1644                 { 0x51,  0x00 },
1645                 { 0x22,  0x00 },
1646                 /* windows reads 0x55 at this point*/
1647         };
1648
1649         return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
1650 }
1651
1652 /* this function is called at probe time */
1653 static int sd_config(struct gspca_dev *gspca_dev,
1654                         const struct usb_device_id *id)
1655 {
1656         struct sd *sd = (struct sd *) gspca_dev;
1657         struct cam *cam;
1658         int ret = 0;
1659
1660         sd->bridge = id->driver_info & BRIDGE_MASK;
1661         sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
1662
1663         switch (sd->bridge) {
1664         case BRIDGE_OV518:
1665         case BRIDGE_OV518PLUS:
1666                 ret = ov518_configure(gspca_dev);
1667                 break;
1668         case BRIDGE_OV519:
1669                 ret = ov519_configure(sd);
1670                 break;
1671         }
1672
1673         if (ret)
1674                 goto error;
1675
1676         ov51x_led_control(sd, 0);       /* turn LED off */
1677
1678         /* Test for 76xx */
1679         if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1680                 goto error;
1681
1682         /* The OV519 must be more aggressive about sensor detection since
1683          * I2C write will never fail if the sensor is not present. We have
1684          * to try to initialize the sensor to detect its presence */
1685         if (init_ov_sensor(sd) >= 0) {
1686                 if (ov7xx0_configure(sd) < 0) {
1687                         PDEBUG(D_ERR, "Failed to configure OV7xx0");
1688                         goto error;
1689                 }
1690         } else {
1691
1692                 /* Test for 6xx0 */
1693                 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1694                         goto error;
1695
1696                 if (init_ov_sensor(sd) >= 0) {
1697                         if (ov6xx0_configure(sd) < 0) {
1698                                 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1699                                 goto error;
1700                         }
1701                 } else {
1702
1703                         /* Test for 8xx0 */
1704                         if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1705                                 goto error;
1706
1707                         if (init_ov_sensor(sd) < 0) {
1708                                 PDEBUG(D_ERR,
1709                                         "Can't determine sensor slave IDs");
1710                                 goto error;
1711                         }
1712                         if (ov8xx0_configure(sd) < 0) {
1713                                 PDEBUG(D_ERR,
1714                                         "Failed to configure OV8xx0 sensor");
1715                                 goto error;
1716                         }
1717                 }
1718         }
1719
1720         cam = &gspca_dev->cam;
1721         switch (sd->bridge) {
1722         case BRIDGE_OV518:
1723         case BRIDGE_OV518PLUS:
1724                 if (!sd->sif) {
1725                         cam->cam_mode = ov518_vga_mode;
1726                         cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
1727                 } else {
1728                         cam->cam_mode = ov518_sif_mode;
1729                         cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
1730                 }
1731                 break;
1732         case BRIDGE_OV519:
1733                 if (!sd->sif) {
1734                         cam->cam_mode = ov519_vga_mode;
1735                         cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
1736                 } else {
1737                         cam->cam_mode = ov519_sif_mode;
1738                         cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
1739                 }
1740                 break;
1741         }
1742         sd->brightness = BRIGHTNESS_DEF;
1743         sd->contrast = CONTRAST_DEF;
1744         sd->colors = COLOR_DEF;
1745         sd->hflip = HFLIP_DEF;
1746         sd->vflip = VFLIP_DEF;
1747         sd->autobrightness = AUTOBRIGHT_DEF;
1748         if (sd->sensor == SEN_OV7670) {
1749                 sd->freq = OV7670_FREQ_DEF;
1750                 gspca_dev->ctrl_dis = 1 << FREQ_IDX;
1751         } else {
1752                 sd->freq = FREQ_DEF;
1753                 gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX) |
1754                                       (1 << OV7670_FREQ_IDX);
1755         }
1756         if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
1757                 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT_IDX;
1758         /* OV8610 Frequency filter control should work but needs testing */
1759         if (sd->sensor == SEN_OV8610)
1760                 gspca_dev->ctrl_dis |= 1 << FREQ_IDX;
1761
1762         return 0;
1763 error:
1764         PDEBUG(D_ERR, "OV519 Config failed");
1765         return -EBUSY;
1766 }
1767
1768 /* this function is called at probe and resume time */
1769 static int sd_init(struct gspca_dev *gspca_dev)
1770 {
1771         struct sd *sd = (struct sd *) gspca_dev;
1772
1773         /* initialize the sensor */
1774         switch (sd->sensor) {
1775         case SEN_OV6620:
1776                 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
1777                         return -EIO;
1778                 break;
1779         case SEN_OV6630:
1780         case SEN_OV66308AF:
1781                 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
1782                         return -EIO;
1783                 break;
1784         default:
1785 /*      case SEN_OV7610: */
1786 /*      case SEN_OV76BE: */
1787                 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
1788                         return -EIO;
1789                 break;
1790         case SEN_OV7620:
1791                 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
1792                         return -EIO;
1793                 break;
1794         case SEN_OV7640:
1795                 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
1796                         return -EIO;
1797                 break;
1798         case SEN_OV7670:
1799                 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
1800                         return -EIO;
1801                 break;
1802         case SEN_OV8610:
1803                 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
1804                         return -EIO;
1805                 break;
1806         }
1807         return 0;
1808 }
1809
1810 /* Sets up the OV518/OV518+ with the given image parameters
1811  *
1812  * OV518 needs a completely different approach, until we can figure out what
1813  * the individual registers do. Also, only 15 FPS is supported now.
1814  *
1815  * Do not put any sensor-specific code in here (including I2C I/O functions)
1816  */
1817 static int ov518_mode_init_regs(struct sd *sd)
1818 {
1819         int hsegs, vsegs;
1820
1821         /******** Set the mode ********/
1822
1823         reg_w(sd, 0x2b, 0);
1824         reg_w(sd, 0x2c, 0);
1825         reg_w(sd, 0x2d, 0);
1826         reg_w(sd, 0x2e, 0);
1827         reg_w(sd, 0x3b, 0);
1828         reg_w(sd, 0x3c, 0);
1829         reg_w(sd, 0x3d, 0);
1830         reg_w(sd, 0x3e, 0);
1831
1832         if (sd->bridge == BRIDGE_OV518) {
1833                 /* Set 8-bit (YVYU) input format */
1834                 reg_w_mask(sd, 0x20, 0x08, 0x08);
1835
1836                 /* Set 12-bit (4:2:0) output format */
1837                 reg_w_mask(sd, 0x28, 0x80, 0xf0);
1838                 reg_w_mask(sd, 0x38, 0x80, 0xf0);
1839         } else {
1840                 reg_w(sd, 0x28, 0x80);
1841                 reg_w(sd, 0x38, 0x80);
1842         }
1843
1844         hsegs = sd->gspca_dev.width / 16;
1845         vsegs = sd->gspca_dev.height / 4;
1846
1847         reg_w(sd, 0x29, hsegs);
1848         reg_w(sd, 0x2a, vsegs);
1849
1850         reg_w(sd, 0x39, hsegs);
1851         reg_w(sd, 0x3a, vsegs);
1852
1853         /* Windows driver does this here; who knows why */
1854         reg_w(sd, 0x2f, 0x80);
1855
1856         /******** Set the framerate (to 30 FPS) ********/
1857         if (sd->bridge == BRIDGE_OV518PLUS)
1858                 sd->clockdiv = 1;
1859         else
1860                 sd->clockdiv = 0;
1861
1862         /* Mode independent, but framerate dependent, regs */
1863         reg_w(sd, 0x51, 0x04);  /* Clock divider; lower==faster */
1864         reg_w(sd, 0x22, 0x18);
1865         reg_w(sd, 0x23, 0xff);
1866
1867         if (sd->bridge == BRIDGE_OV518PLUS)
1868                 reg_w(sd, 0x21, 0x19);
1869         else
1870                 reg_w(sd, 0x71, 0x17);  /* Compression-related? */
1871
1872         /* FIXME: Sensor-specific */
1873         /* Bit 5 is what matters here. Of course, it is "reserved" */
1874         i2c_w(sd, 0x54, 0x23);
1875
1876         reg_w(sd, 0x2f, 0x80);
1877
1878         if (sd->bridge == BRIDGE_OV518PLUS) {
1879                 reg_w(sd, 0x24, 0x94);
1880                 reg_w(sd, 0x25, 0x90);
1881                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
1882                 ov518_reg_w32(sd, 0xc6,    540, 2);     /* 21ch   */
1883                 ov518_reg_w32(sd, 0xc7,    540, 2);     /* 21ch   */
1884                 ov518_reg_w32(sd, 0xc8,    108, 2);     /* 6ch    */
1885                 ov518_reg_w32(sd, 0xca, 131098, 3);     /* 2001ah */
1886                 ov518_reg_w32(sd, 0xcb,    532, 2);     /* 214h   */
1887                 ov518_reg_w32(sd, 0xcc,   2400, 2);     /* 960h   */
1888                 ov518_reg_w32(sd, 0xcd,     32, 2);     /* 20h    */
1889                 ov518_reg_w32(sd, 0xce,    608, 2);     /* 260h   */
1890         } else {
1891                 reg_w(sd, 0x24, 0x9f);
1892                 reg_w(sd, 0x25, 0x90);
1893                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
1894                 ov518_reg_w32(sd, 0xc6,    381, 2);     /* 17dh   */
1895                 ov518_reg_w32(sd, 0xc7,    381, 2);     /* 17dh   */
1896                 ov518_reg_w32(sd, 0xc8,    128, 2);     /* 80h    */
1897                 ov518_reg_w32(sd, 0xca, 183331, 3);     /* 2cc23h */
1898                 ov518_reg_w32(sd, 0xcb,    746, 2);     /* 2eah   */
1899                 ov518_reg_w32(sd, 0xcc,   1750, 2);     /* 6d6h   */
1900                 ov518_reg_w32(sd, 0xcd,     45, 2);     /* 2dh    */
1901                 ov518_reg_w32(sd, 0xce,    851, 2);     /* 353h   */
1902         }
1903
1904         reg_w(sd, 0x2f, 0x80);
1905
1906         return 0;
1907 }
1908
1909
1910 /* Sets up the OV519 with the given image parameters
1911  *
1912  * OV519 needs a completely different approach, until we can figure out what
1913  * the individual registers do.
1914  *
1915  * Do not put any sensor-specific code in here (including I2C I/O functions)
1916  */
1917 static int ov519_mode_init_regs(struct sd *sd)
1918 {
1919         static const struct ov_regvals mode_init_519_ov7670[] = {
1920                 { 0x5d, 0x03 }, /* Turn off suspend mode */
1921                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1922                 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1923                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1924                 { 0xa3, 0x18 },
1925                 { 0xa4, 0x04 },
1926                 { 0xa5, 0x28 },
1927                 { 0x37, 0x00 }, /* SetUsbInit */
1928                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1929                 /* Enable both fields, YUV Input, disable defect comp (why?) */
1930                 { 0x20, 0x0c },
1931                 { 0x21, 0x38 },
1932                 { 0x22, 0x1d },
1933                 { 0x17, 0x50 }, /* undocumented */
1934                 { 0x37, 0x00 }, /* undocumented */
1935                 { 0x40, 0xff }, /* I2C timeout counter */
1936                 { 0x46, 0x00 }, /* I2C clock prescaler */
1937                 { 0x59, 0x04 }, /* new from windrv 090403 */
1938                 { 0xff, 0x00 }, /* undocumented */
1939                 /* windows reads 0x55 at this point, why? */
1940         };
1941
1942         static const struct ov_regvals mode_init_519[] = {
1943                 { 0x5d, 0x03 }, /* Turn off suspend mode */
1944                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1945                 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1946                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1947                 { 0xa3, 0x18 },
1948                 { 0xa4, 0x04 },
1949                 { 0xa5, 0x28 },
1950                 { 0x37, 0x00 }, /* SetUsbInit */
1951                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1952                 /* Enable both fields, YUV Input, disable defect comp (why?) */
1953                 { 0x22, 0x1d },
1954                 { 0x17, 0x50 }, /* undocumented */
1955                 { 0x37, 0x00 }, /* undocumented */
1956                 { 0x40, 0xff }, /* I2C timeout counter */
1957                 { 0x46, 0x00 }, /* I2C clock prescaler */
1958                 { 0x59, 0x04 }, /* new from windrv 090403 */
1959                 { 0xff, 0x00 }, /* undocumented */
1960                 /* windows reads 0x55 at this point, why? */
1961         };
1962
1963         /******** Set the mode ********/
1964         if (sd->sensor != SEN_OV7670) {
1965                 if (write_regvals(sd, mode_init_519,
1966                                   ARRAY_SIZE(mode_init_519)))
1967                         return -EIO;
1968                 if (sd->sensor == SEN_OV7640) {
1969                         /* Select 8-bit input mode */
1970                         reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
1971                 }
1972         } else {
1973                 if (write_regvals(sd, mode_init_519_ov7670,
1974                                   ARRAY_SIZE(mode_init_519_ov7670)))
1975                         return -EIO;
1976         }
1977
1978         reg_w(sd, OV519_R10_H_SIZE,     sd->gspca_dev.width >> 4);
1979         reg_w(sd, OV519_R11_V_SIZE,     sd->gspca_dev.height >> 3);
1980         if (sd->sensor == SEN_OV7670 &&
1981             sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
1982                 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
1983         else
1984                 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
1985         reg_w(sd, OV519_R13_X_OFFSETH,  0x00);
1986         reg_w(sd, OV519_R14_Y_OFFSETL,  0x00);
1987         reg_w(sd, OV519_R15_Y_OFFSETH,  0x00);
1988         reg_w(sd, OV519_R16_DIVIDER,    0x00);
1989         reg_w(sd, OV519_R25_FORMAT,     0x03); /* YUV422 */
1990         reg_w(sd, 0x26,                 0x00); /* Undocumented */
1991
1992         /******** Set the framerate ********/
1993         if (frame_rate > 0)
1994                 sd->frame_rate = frame_rate;
1995
1996 /* FIXME: These are only valid at the max resolution. */
1997         sd->clockdiv = 0;
1998         switch (sd->sensor) {
1999         case SEN_OV7640:
2000                 switch (sd->frame_rate) {
2001                 default:
2002 /*              case 30: */
2003                         reg_w(sd, 0xa4, 0x0c);
2004                         reg_w(sd, 0x23, 0xff);
2005                         break;
2006                 case 25:
2007                         reg_w(sd, 0xa4, 0x0c);
2008                         reg_w(sd, 0x23, 0x1f);
2009                         break;
2010                 case 20:
2011                         reg_w(sd, 0xa4, 0x0c);
2012                         reg_w(sd, 0x23, 0x1b);
2013                         break;
2014                 case 15:
2015                         reg_w(sd, 0xa4, 0x04);
2016                         reg_w(sd, 0x23, 0xff);
2017                         sd->clockdiv = 1;
2018                         break;
2019                 case 10:
2020                         reg_w(sd, 0xa4, 0x04);
2021                         reg_w(sd, 0x23, 0x1f);
2022                         sd->clockdiv = 1;
2023                         break;
2024                 case 5:
2025                         reg_w(sd, 0xa4, 0x04);
2026                         reg_w(sd, 0x23, 0x1b);
2027                         sd->clockdiv = 1;
2028                         break;
2029                 }
2030                 break;
2031         case SEN_OV8610:
2032                 switch (sd->frame_rate) {
2033                 default:        /* 15 fps */
2034 /*              case 15: */
2035                         reg_w(sd, 0xa4, 0x06);
2036                         reg_w(sd, 0x23, 0xff);
2037                         break;
2038                 case 10:
2039                         reg_w(sd, 0xa4, 0x06);
2040                         reg_w(sd, 0x23, 0x1f);
2041                         break;
2042                 case 5:
2043                         reg_w(sd, 0xa4, 0x06);
2044                         reg_w(sd, 0x23, 0x1b);
2045                         break;
2046                 }
2047                 break;
2048         case SEN_OV7670:                /* guesses, based on 7640 */
2049                 PDEBUG(D_STREAM, "Setting framerate to %d fps",
2050                                  (sd->frame_rate == 0) ? 15 : sd->frame_rate);
2051                 reg_w(sd, 0xa4, 0x10);
2052                 switch (sd->frame_rate) {
2053                 case 30:
2054                         reg_w(sd, 0x23, 0xff);
2055                         break;
2056                 case 20:
2057                         reg_w(sd, 0x23, 0x1b);
2058                         break;
2059                 default:
2060 /*              case 15: */
2061                         reg_w(sd, 0x23, 0xff);
2062                         sd->clockdiv = 1;
2063                         break;
2064                 }
2065                 break;
2066         }
2067         return 0;
2068 }
2069
2070 static int mode_init_ov_sensor_regs(struct sd *sd)
2071 {
2072         struct gspca_dev *gspca_dev;
2073         int qvga;
2074
2075         gspca_dev = &sd->gspca_dev;
2076         qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
2077
2078         /******** Mode (VGA/QVGA) and sensor specific regs ********/
2079         switch (sd->sensor) {
2080         case SEN_OV8610:
2081                 /* For OV8610 qvga means qsvga */
2082                 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
2083                 break;
2084         case SEN_OV7610:
2085                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2086                 break;
2087         case SEN_OV7620:
2088 /*              i2c_w(sd, 0x2b, 0x00); */
2089                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2090                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2091                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
2092                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
2093                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
2094                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
2095                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
2096                 break;
2097         case SEN_OV76BE:
2098 /*              i2c_w(sd, 0x2b, 0x00); */
2099                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2100                 break;
2101         case SEN_OV7640:
2102 /*              i2c_w(sd, 0x2b, 0x00); */
2103                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2104                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2105 /*              i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
2106 /*              i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
2107 /*              i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
2108 /*              i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
2109 /*              i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
2110                 break;
2111         case SEN_OV7670:
2112                 /* set COM7_FMT_VGA or COM7_FMT_QVGA
2113                  * do we need to set anything else?
2114                  *      HSTART etc are set in set_ov_sensor_window itself */
2115                 i2c_w_mask(sd, OV7670_REG_COM7,
2116                          qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
2117                          OV7670_COM7_FMT_MASK);
2118                 break;
2119         case SEN_OV6620:
2120         case SEN_OV6630:
2121         case SEN_OV66308AF:
2122                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2123                 break;
2124         default:
2125                 return -EINVAL;
2126         }
2127
2128         /******** Palette-specific regs ********/
2129         if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
2130                 /* not valid on the OV6620/OV7620/6630? */
2131                 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
2132         }
2133
2134         /* The OV518 needs special treatment. Although both the OV518
2135          * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2136          * bus is actually used. The UV bus is tied to ground.
2137          * Therefore, the OV6630 needs to be in 8-bit multiplexed
2138          * output mode */
2139
2140         /* OV7640 is 8-bit only */
2141
2142         if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV66308AF &&
2143                                         sd->sensor != SEN_OV7640)
2144                 i2c_w_mask(sd, 0x13, 0x00, 0x20);
2145
2146         /******** Clock programming ********/
2147         /* The OV6620 needs special handling. This prevents the
2148          * severe banding that normally occurs */
2149         if (sd->sensor == SEN_OV6620) {
2150
2151                 /* Clock down */
2152                 i2c_w(sd, 0x2a, 0x04);
2153                 i2c_w(sd, 0x11, sd->clockdiv);
2154                 i2c_w(sd, 0x2a, 0x84);
2155                 /* This next setting is critical. It seems to improve
2156                  * the gain or the contrast. The "reserved" bits seem
2157                  * to have some effect in this case. */
2158                 i2c_w(sd, 0x2d, 0x85);
2159         } else {
2160                 i2c_w(sd, 0x11, sd->clockdiv);
2161         }
2162
2163         /******** Special Features ********/
2164 /* no evidence this is possible with OV7670, either */
2165         /* Test Pattern */
2166         if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
2167                 i2c_w_mask(sd, 0x12, 0x00, 0x02);
2168
2169         /* Enable auto white balance */
2170         if (sd->sensor == SEN_OV7670)
2171                 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
2172                                 OV7670_COM8_AWB);
2173         else
2174                 i2c_w_mask(sd, 0x12, 0x04, 0x04);
2175
2176         /* This will go away as soon as ov51x_mode_init_sensor_regs() */
2177         /* is fully tested. */
2178         /* 7620/6620/6630? don't have register 0x35, so play it safe */
2179         if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
2180                 if (!qvga)
2181                         i2c_w(sd, 0x35, 0x9e);
2182                 else
2183                         i2c_w(sd, 0x35, 0x1e);
2184         }
2185         return 0;
2186 }
2187
2188 static void sethvflip(struct sd *sd)
2189 {
2190         if (sd->sensor != SEN_OV7670)
2191                 return;
2192         if (sd->gspca_dev.streaming)
2193                 ov51x_stop(sd);
2194         i2c_w_mask(sd, OV7670_REG_MVFP,
2195                 OV7670_MVFP_MIRROR * sd->hflip
2196                         | OV7670_MVFP_VFLIP * sd->vflip,
2197                 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
2198         if (sd->gspca_dev.streaming)
2199                 ov51x_restart(sd);
2200 }
2201
2202 static int set_ov_sensor_window(struct sd *sd)
2203 {
2204         struct gspca_dev *gspca_dev;
2205         int qvga, crop;
2206         int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
2207         int ret, hstart, hstop, vstop, vstart;
2208         __u8 v;
2209
2210         gspca_dev = &sd->gspca_dev;
2211         qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
2212         crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
2213
2214         /* The different sensor ICs handle setting up of window differently.
2215          * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
2216         switch (sd->sensor) {
2217         case SEN_OV8610:
2218                 hwsbase = 0x1e;
2219                 hwebase = 0x1e;
2220                 vwsbase = 0x02;
2221                 vwebase = 0x02;
2222                 break;
2223         case SEN_OV7610:
2224         case SEN_OV76BE:
2225                 hwsbase = 0x38;
2226                 hwebase = 0x3a;
2227                 vwsbase = vwebase = 0x05;
2228                 break;
2229         case SEN_OV6620:
2230         case SEN_OV6630:
2231         case SEN_OV66308AF:
2232                 hwsbase = 0x38;
2233                 hwebase = 0x3a;
2234                 vwsbase = 0x05;
2235                 vwebase = 0x06;
2236                 if (sd->sensor == SEN_OV66308AF && qvga)
2237                         /* HDG: this fixes U and V getting swapped */
2238                         hwsbase++;
2239                 if (crop) {
2240                         hwsbase += 8;
2241                         hwebase += 8;
2242                         vwsbase += 11;
2243                         vwebase += 11;
2244                 }
2245                 break;
2246         case SEN_OV7620:
2247                 hwsbase = 0x2f;         /* From 7620.SET (spec is wrong) */
2248                 hwebase = 0x2f;
2249                 vwsbase = vwebase = 0x05;
2250                 break;
2251         case SEN_OV7640:
2252                 hwsbase = 0x1a;
2253                 hwebase = 0x1a;
2254                 vwsbase = vwebase = 0x03;
2255                 break;
2256         case SEN_OV7670:
2257                 /*handling of OV7670 hardware sensor start and stop values
2258                  * is very odd, compared to the other OV sensors */
2259                 vwsbase = vwebase = hwebase = hwsbase = 0x00;
2260                 break;
2261         default:
2262                 return -EINVAL;
2263         }
2264
2265         switch (sd->sensor) {
2266         case SEN_OV6620:
2267         case SEN_OV6630:
2268         case SEN_OV66308AF:
2269                 if (qvga) {             /* QCIF */
2270                         hwscale = 0;
2271                         vwscale = 0;
2272                 } else {                /* CIF */
2273                         hwscale = 1;
2274                         vwscale = 1;    /* The datasheet says 0;
2275                                          * it's wrong */
2276                 }
2277                 break;
2278         case SEN_OV8610:
2279                 if (qvga) {             /* QSVGA */
2280                         hwscale = 1;
2281                         vwscale = 1;
2282                 } else {                /* SVGA */
2283                         hwscale = 2;
2284                         vwscale = 2;
2285                 }
2286                 break;
2287         default:                        /* SEN_OV7xx0 */
2288                 if (qvga) {             /* QVGA */
2289                         hwscale = 1;
2290                         vwscale = 0;
2291                 } else {                /* VGA */
2292                         hwscale = 2;
2293                         vwscale = 1;
2294                 }
2295         }
2296
2297         ret = mode_init_ov_sensor_regs(sd);
2298         if (ret < 0)
2299                 return ret;
2300
2301         if (sd->sensor == SEN_OV8610) {
2302                 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
2303                                 /* old 0x95, new 0x05 from windrv 090403 */
2304                                                 /* bits 5-7: reserved */
2305                 i2c_w_mask(sd, 0x28, 0x20, 0x20);
2306                                         /* bit 5: progressive mode on */
2307         }
2308
2309         /* The below is wrong for OV7670s because their window registers
2310          * only store the high bits in 0x17 to 0x1a */
2311
2312         /* SRH Use sd->max values instead of requested win values */
2313         /* SCS Since we're sticking with only the max hardware widths
2314          * for a given mode */
2315         /* I can hard code this for OV7670s */
2316         /* Yes, these numbers do look odd, but they're tested and work! */
2317         if (sd->sensor == SEN_OV7670) {
2318                 if (qvga) {             /* QVGA from ov7670.c by
2319                                          * Jonathan Corbet */
2320                         hstart = 164;
2321                         hstop = 28;
2322                         vstart = 14;
2323                         vstop = 494;
2324                 } else {                /* VGA */
2325                         hstart = 158;
2326                         hstop = 14;
2327                         vstart = 10;
2328                         vstop = 490;
2329                 }
2330                 /* OV7670 hardware window registers are split across
2331                  * multiple locations */
2332                 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
2333                 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
2334                 v = i2c_r(sd, OV7670_REG_HREF);
2335                 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
2336                 msleep(10);     /* need to sleep between read and write to
2337                                  * same reg! */
2338                 i2c_w(sd, OV7670_REG_HREF, v);
2339
2340                 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
2341                 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
2342                 v = i2c_r(sd, OV7670_REG_VREF);
2343                 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
2344                 msleep(10);     /* need to sleep between read and write to
2345                                  * same reg! */
2346                 i2c_w(sd, OV7670_REG_VREF, v);
2347         } else {
2348                 i2c_w(sd, 0x17, hwsbase);
2349                 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
2350                 i2c_w(sd, 0x19, vwsbase);
2351                 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
2352         }
2353         return 0;
2354 }
2355
2356 /* -- start the camera -- */
2357 static int sd_start(struct gspca_dev *gspca_dev)
2358 {
2359         struct sd *sd = (struct sd *) gspca_dev;
2360         int ret = 0;
2361
2362         switch (sd->bridge) {
2363         case BRIDGE_OV518:
2364         case BRIDGE_OV518PLUS:
2365                 ret = ov518_mode_init_regs(sd);
2366                 break;
2367         case BRIDGE_OV519:
2368                 ret = ov519_mode_init_regs(sd);
2369                 break;
2370         }
2371         if (ret < 0)
2372                 goto out;
2373
2374         ret = set_ov_sensor_window(sd);
2375         if (ret < 0)
2376                 goto out;
2377
2378         setcontrast(gspca_dev);
2379         setbrightness(gspca_dev);
2380         setcolors(gspca_dev);
2381         sethvflip(sd);
2382         setautobrightness(sd);
2383         setfreq(sd);
2384
2385         ret = ov51x_restart(sd);
2386         if (ret < 0)
2387                 goto out;
2388         ov51x_led_control(sd, 1);
2389         return 0;
2390 out:
2391         PDEBUG(D_ERR, "camera start error:%d", ret);
2392         return ret;
2393 }
2394
2395 static void sd_stopN(struct gspca_dev *gspca_dev)
2396 {
2397         struct sd *sd = (struct sd *) gspca_dev;
2398
2399         ov51x_stop(sd);
2400         ov51x_led_control(sd, 0);
2401 }
2402
2403 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
2404                         struct gspca_frame *frame,      /* target */
2405                         __u8 *data,                     /* isoc packet */
2406                         int len)                        /* iso packet length */
2407 {
2408         struct sd *sd = (struct sd *) gspca_dev;
2409
2410         /* A false positive here is likely, until OVT gives me
2411          * the definitive SOF/EOF format */
2412         if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
2413                 gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
2414                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, 0);
2415                 sd->packet_nr = 0;
2416         }
2417
2418         if (gspca_dev->last_packet_type == DISCARD_PACKET)
2419                 return;
2420
2421         /* Does this device use packet numbers ? */
2422         if (len & 7) {
2423                 len--;
2424                 if (sd->packet_nr == data[len])
2425                         sd->packet_nr++;
2426                 /* The last few packets of the frame (which are all 0's
2427                    except that they may contain part of the footer), are
2428                    numbered 0 */
2429                 else if (sd->packet_nr == 0 || data[len]) {
2430                         PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
2431                                 (int)data[len], (int)sd->packet_nr);
2432                         gspca_dev->last_packet_type = DISCARD_PACKET;
2433                         return;
2434                 }
2435         }
2436
2437         /* intermediate packet */
2438         gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
2439 }
2440
2441 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
2442                         struct gspca_frame *frame,      /* target */
2443                         __u8 *data,                     /* isoc packet */
2444                         int len)                        /* iso packet length */
2445 {
2446         /* Header of ov519 is 16 bytes:
2447          *     Byte     Value      Description
2448          *      0       0xff    magic
2449          *      1       0xff    magic
2450          *      2       0xff    magic
2451          *      3       0xXX    0x50 = SOF, 0x51 = EOF
2452          *      9       0xXX    0x01 initial frame without data,
2453          *                      0x00 standard frame with image
2454          *      14      Lo      in EOF: length of image data / 8
2455          *      15      Hi
2456          */
2457
2458         if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
2459                 switch (data[3]) {
2460                 case 0x50:              /* start of frame */
2461 #define HDRSZ 16
2462                         data += HDRSZ;
2463                         len -= HDRSZ;
2464 #undef HDRSZ
2465                         if (data[0] == 0xff || data[1] == 0xd8)
2466                                 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
2467                                                 data, len);
2468                         else
2469                                 gspca_dev->last_packet_type = DISCARD_PACKET;
2470                         return;
2471                 case 0x51:              /* end of frame */
2472                         if (data[9] != 0)
2473                                 gspca_dev->last_packet_type = DISCARD_PACKET;
2474                         gspca_frame_add(gspca_dev, LAST_PACKET, frame,
2475                                         data, 0);
2476                         return;
2477                 }
2478         }
2479
2480         /* intermediate packet */
2481         gspca_frame_add(gspca_dev, INTER_PACKET, frame,
2482                         data, len);
2483 }
2484
2485 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2486                         struct gspca_frame *frame,      /* target */
2487                         __u8 *data,                     /* isoc packet */
2488                         int len)                        /* iso packet length */
2489 {
2490         struct sd *sd = (struct sd *) gspca_dev;
2491
2492         switch (sd->bridge) {
2493         case BRIDGE_OV511:
2494         case BRIDGE_OV511PLUS:
2495                 break;
2496         case BRIDGE_OV518:
2497         case BRIDGE_OV518PLUS:
2498                 ov518_pkt_scan(gspca_dev, frame, data, len);
2499                 break;
2500         case BRIDGE_OV519:
2501                 ov519_pkt_scan(gspca_dev, frame, data, len);
2502                 break;
2503         }
2504 }
2505
2506 /* -- management routines -- */
2507
2508 static void setbrightness(struct gspca_dev *gspca_dev)
2509 {
2510         struct sd *sd = (struct sd *) gspca_dev;
2511         int val;
2512
2513         val = sd->brightness;
2514         switch (sd->sensor) {
2515         case SEN_OV8610:
2516         case SEN_OV7610:
2517         case SEN_OV76BE:
2518         case SEN_OV6620:
2519         case SEN_OV6630:
2520         case SEN_OV66308AF:
2521         case SEN_OV7640:
2522                 i2c_w(sd, OV7610_REG_BRT, val);
2523                 break;
2524         case SEN_OV7620:
2525                 /* 7620 doesn't like manual changes when in auto mode */
2526                 if (!sd->autobrightness)
2527                         i2c_w(sd, OV7610_REG_BRT, val);
2528                 break;
2529         case SEN_OV7670:
2530 /*win trace
2531  *              i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
2532                 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
2533                 break;
2534         }
2535 }
2536
2537 static void setcontrast(struct gspca_dev *gspca_dev)
2538 {
2539         struct sd *sd = (struct sd *) gspca_dev;
2540         int val;
2541
2542         val = sd->contrast;
2543         switch (sd->sensor) {
2544         case SEN_OV7610:
2545         case SEN_OV6620:
2546                 i2c_w(sd, OV7610_REG_CNT, val);
2547                 break;
2548         case SEN_OV6630:
2549         case SEN_OV66308AF:
2550                 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
2551                 break;
2552         case SEN_OV8610: {
2553                 static const __u8 ctab[] = {
2554                         0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
2555                 };
2556
2557                 /* Use Y gamma control instead. Bit 0 enables it. */
2558                 i2c_w(sd, 0x64, ctab[val >> 5]);
2559                 break;
2560             }
2561         case SEN_OV7620: {
2562                 static const __u8 ctab[] = {
2563                         0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
2564                         0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
2565                 };
2566
2567                 /* Use Y gamma control instead. Bit 0 enables it. */
2568                 i2c_w(sd, 0x64, ctab[val >> 4]);
2569                 break;
2570             }
2571         case SEN_OV7640:
2572                 /* Use gain control instead. */
2573                 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2574                 break;
2575         case SEN_OV7670:
2576                 /* check that this isn't just the same as ov7610 */
2577                 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2578                 break;
2579         }
2580 }
2581
2582 static void setcolors(struct gspca_dev *gspca_dev)
2583 {
2584         struct sd *sd = (struct sd *) gspca_dev;
2585         int val;
2586
2587         val = sd->colors;
2588         switch (sd->sensor) {
2589         case SEN_OV8610:
2590         case SEN_OV7610:
2591         case SEN_OV76BE:
2592         case SEN_OV6620:
2593         case SEN_OV6630:
2594         case SEN_OV66308AF:
2595                 i2c_w(sd, OV7610_REG_SAT, val);
2596                 break;
2597         case SEN_OV7620:
2598                 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2599 /*              rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2600                 if (rc < 0)
2601                         goto out; */
2602                 i2c_w(sd, OV7610_REG_SAT, val);
2603                 break;
2604         case SEN_OV7640:
2605                 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2606                 break;
2607         case SEN_OV7670:
2608                 /* supported later once I work out how to do it
2609                  * transparently fail now! */
2610                 /* set REG_COM13 values for UV sat auto mode */
2611                 break;
2612         }
2613 }
2614
2615 static void setautobrightness(struct sd *sd)
2616 {
2617         if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
2618                 return;
2619
2620         i2c_w_mask(sd, 0x2d, sd->autobrightness ? 0x10 : 0x00, 0x10);
2621 }
2622
2623 static void setfreq(struct sd *sd)
2624 {
2625         if (sd->sensor == SEN_OV7670) {
2626                 switch (sd->freq) {
2627                 case 0: /* Banding filter disabled */
2628                         i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
2629                         break;
2630                 case 1: /* 50 hz */
2631                         i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2632                                    OV7670_COM8_BFILT);
2633                         i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
2634                         break;
2635                 case 2: /* 60 hz */
2636                         i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2637                                    OV7670_COM8_BFILT);
2638                         i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
2639                         break;
2640                 case 3: /* Auto hz */
2641                         i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2642                                    OV7670_COM8_BFILT);
2643                         i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
2644                                    0x18);
2645                         break;
2646                 }
2647         } else {
2648                 switch (sd->freq) {
2649                 case 0: /* Banding filter disabled */
2650                         i2c_w_mask(sd, 0x2d, 0x00, 0x04);
2651                         i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2652                         break;
2653                 case 1: /* 50 hz (filter on and framerate adj) */
2654                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2655                         i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2656                         /* 20 fps -> 16.667 fps */
2657                         if (sd->sensor == SEN_OV6620 ||
2658                             sd->sensor == SEN_OV6630 ||
2659                             sd->sensor == SEN_OV66308AF)
2660                                 i2c_w(sd, 0x2b, 0x5e);
2661                         else
2662                                 i2c_w(sd, 0x2b, 0xac);
2663                         break;
2664                 case 2: /* 60 hz (filter on, ...) */
2665                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2666                         if (sd->sensor == SEN_OV6620 ||
2667                             sd->sensor == SEN_OV6630 ||
2668                             sd->sensor == SEN_OV66308AF) {
2669                                 /* 20 fps -> 15 fps */
2670                                 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2671                                 i2c_w(sd, 0x2b, 0xa8);
2672                         } else {
2673                                 /* no framerate adj. */
2674                                 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2675                         }
2676                         break;
2677                 }
2678         }
2679 }
2680
2681 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2682 {
2683         struct sd *sd = (struct sd *) gspca_dev;
2684
2685         sd->brightness = val;
2686         if (gspca_dev->streaming)
2687                 setbrightness(gspca_dev);
2688         return 0;
2689 }
2690
2691 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2692 {
2693         struct sd *sd = (struct sd *) gspca_dev;
2694
2695         *val = sd->brightness;
2696         return 0;
2697 }
2698
2699 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2700 {
2701         struct sd *sd = (struct sd *) gspca_dev;
2702
2703         sd->contrast = val;
2704         if (gspca_dev->streaming)
2705                 setcontrast(gspca_dev);
2706         return 0;
2707 }
2708
2709 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2710 {
2711         struct sd *sd = (struct sd *) gspca_dev;
2712
2713         *val = sd->contrast;
2714         return 0;
2715 }
2716
2717 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2718 {
2719         struct sd *sd = (struct sd *) gspca_dev;
2720
2721         sd->colors = val;
2722         if (gspca_dev->streaming)
2723                 setcolors(gspca_dev);
2724         return 0;
2725 }
2726
2727 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2728 {
2729         struct sd *sd = (struct sd *) gspca_dev;
2730
2731         *val = sd->colors;
2732         return 0;
2733 }
2734
2735 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2736 {
2737         struct sd *sd = (struct sd *) gspca_dev;
2738
2739         sd->hflip = val;
2740         if (gspca_dev->streaming)
2741                 sethvflip(sd);
2742         return 0;
2743 }
2744
2745 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2746 {
2747         struct sd *sd = (struct sd *) gspca_dev;
2748
2749         *val = sd->hflip;
2750         return 0;
2751 }
2752
2753 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2754 {
2755         struct sd *sd = (struct sd *) gspca_dev;
2756
2757         sd->vflip = val;
2758         if (gspca_dev->streaming)
2759                 sethvflip(sd);
2760         return 0;
2761 }
2762
2763 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2764 {
2765         struct sd *sd = (struct sd *) gspca_dev;
2766
2767         *val = sd->vflip;
2768         return 0;
2769 }
2770
2771 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val)
2772 {
2773         struct sd *sd = (struct sd *) gspca_dev;
2774
2775         sd->autobrightness = val;
2776         if (gspca_dev->streaming)
2777                 setautobrightness(sd);
2778         return 0;
2779 }
2780
2781 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val)
2782 {
2783         struct sd *sd = (struct sd *) gspca_dev;
2784
2785         *val = sd->autobrightness;
2786         return 0;
2787 }
2788
2789 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
2790 {
2791         struct sd *sd = (struct sd *) gspca_dev;
2792
2793         sd->freq = val;
2794         if (gspca_dev->streaming)
2795                 setfreq(sd);
2796         return 0;
2797 }
2798
2799 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
2800 {
2801         struct sd *sd = (struct sd *) gspca_dev;
2802
2803         *val = sd->freq;
2804         return 0;
2805 }
2806
2807 static int sd_querymenu(struct gspca_dev *gspca_dev,
2808                         struct v4l2_querymenu *menu)
2809 {
2810         struct sd *sd = (struct sd *) gspca_dev;
2811
2812         switch (menu->id) {
2813         case V4L2_CID_POWER_LINE_FREQUENCY:
2814                 switch (menu->index) {
2815                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2816                         strcpy((char *) menu->name, "NoFliker");
2817                         return 0;
2818                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2819                         strcpy((char *) menu->name, "50 Hz");
2820                         return 0;
2821                 case 2:         /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2822                         strcpy((char *) menu->name, "60 Hz");
2823                         return 0;
2824                 case 3:
2825                         if (sd->sensor != SEN_OV7670)
2826                                 return -EINVAL;
2827
2828                         strcpy((char *) menu->name, "Automatic");
2829                         return 0;
2830                 }
2831                 break;
2832         }
2833         return -EINVAL;
2834 }
2835
2836 /* sub-driver description */
2837 static const struct sd_desc sd_desc = {
2838         .name = MODULE_NAME,
2839         .ctrls = sd_ctrls,
2840         .nctrls = ARRAY_SIZE(sd_ctrls),
2841         .config = sd_config,
2842         .init = sd_init,
2843         .start = sd_start,
2844         .stopN = sd_stopN,
2845         .pkt_scan = sd_pkt_scan,
2846         .querymenu = sd_querymenu,
2847 };
2848
2849 /* -- module initialisation -- */
2850 static const __devinitdata struct usb_device_id device_table[] = {
2851         {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
2852         {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
2853         {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
2854         {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
2855         {USB_DEVICE(0x041e, 0x4064),
2856          .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
2857         {USB_DEVICE(0x041e, 0x4068),
2858          .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
2859         {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
2860         {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
2861         {USB_DEVICE(0x054c, 0x0155), .driver_info = BRIDGE_OV519 },
2862         {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
2863         {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
2864         {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
2865         {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
2866         {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
2867         {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
2868         {}
2869 };
2870
2871 MODULE_DEVICE_TABLE(usb, device_table);
2872
2873 /* -- device connect -- */
2874 static int sd_probe(struct usb_interface *intf,
2875                         const struct usb_device_id *id)
2876 {
2877         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2878                                 THIS_MODULE);
2879 }
2880
2881 static struct usb_driver sd_driver = {
2882         .name = MODULE_NAME,
2883         .id_table = device_table,
2884         .probe = sd_probe,
2885         .disconnect = gspca_disconnect,
2886 #ifdef CONFIG_PM
2887         .suspend = gspca_suspend,
2888         .resume = gspca_resume,
2889 #endif
2890 };
2891
2892 /* -- module insert / remove -- */
2893 static int __init sd_mod_init(void)
2894 {
2895         int ret;
2896         ret = usb_register(&sd_driver);
2897         if (ret < 0)
2898                 return ret;
2899         PDEBUG(D_PROBE, "registered");
2900         return 0;
2901 }
2902 static void __exit sd_mod_exit(void)
2903 {
2904         usb_deregister(&sd_driver);
2905         PDEBUG(D_PROBE, "deregistered");
2906 }
2907
2908 module_init(sd_mod_init);
2909 module_exit(sd_mod_exit);
2910
2911 module_param(frame_rate, int, 0644);
2912 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");