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