]> Pileus Git - ~andy/linux/blob - drivers/media/video/gspca/ov534.c
Merge branches 'acpi_pad', 'acpica', 'apei-bugzilla-43282', 'battery', 'cpuidle-coupl...
[~andy/linux] / drivers / media / video / gspca / ov534.c
1 /*
2  * ov534-ov7xxx gspca driver
3  *
4  * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
5  * Copyright (C) 2008 Jim Paris <jim@jtan.com>
6  * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
7  *
8  * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
9  * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
10  * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
11  *
12  * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
13  * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
14  *                  added by Max Thrun <bear24rw@gmail.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33 #define MODULE_NAME "ov534"
34
35 #include "gspca.h"
36
37 #include <linux/fixp-arith.h>
38
39 #define OV534_REG_ADDRESS       0xf1    /* sensor address */
40 #define OV534_REG_SUBADDR       0xf2
41 #define OV534_REG_WRITE         0xf3
42 #define OV534_REG_READ          0xf4
43 #define OV534_REG_OPERATION     0xf5
44 #define OV534_REG_STATUS        0xf6
45
46 #define OV534_OP_WRITE_3        0x37
47 #define OV534_OP_WRITE_2        0x33
48 #define OV534_OP_READ_2         0xf9
49
50 #define CTRL_TIMEOUT 500
51
52 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
53 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
54 MODULE_LICENSE("GPL");
55
56 /* controls */
57 enum e_ctrl {
58         HUE,
59         SATURATION,
60         BRIGHTNESS,
61         CONTRAST,
62         GAIN,
63         EXPOSURE,
64         AGC,
65         AWB,
66         AEC,
67         SHARPNESS,
68         HFLIP,
69         VFLIP,
70         LIGHTFREQ,
71         NCTRLS          /* number of controls */
72 };
73
74 /* specific webcam descriptor */
75 struct sd {
76         struct gspca_dev gspca_dev;     /* !! must be the first item */
77
78         struct gspca_ctrl ctrls[NCTRLS];
79
80         __u32 last_pts;
81         u16 last_fid;
82         u8 frame_rate;
83
84         u8 sensor;
85 };
86 enum sensors {
87         SENSOR_OV767x,
88         SENSOR_OV772x,
89         NSENSORS
90 };
91
92 /* V4L2 controls supported by the driver */
93 static void sethue(struct gspca_dev *gspca_dev);
94 static void setsaturation(struct gspca_dev *gspca_dev);
95 static void setbrightness(struct gspca_dev *gspca_dev);
96 static void setcontrast(struct gspca_dev *gspca_dev);
97 static void setgain(struct gspca_dev *gspca_dev);
98 static void setexposure(struct gspca_dev *gspca_dev);
99 static void setagc(struct gspca_dev *gspca_dev);
100 static void setawb(struct gspca_dev *gspca_dev);
101 static void setaec(struct gspca_dev *gspca_dev);
102 static void setsharpness(struct gspca_dev *gspca_dev);
103 static void sethvflip(struct gspca_dev *gspca_dev);
104 static void setlightfreq(struct gspca_dev *gspca_dev);
105
106 static int sd_start(struct gspca_dev *gspca_dev);
107 static void sd_stopN(struct gspca_dev *gspca_dev);
108
109 static const struct ctrl sd_ctrls[] = {
110 [HUE] = {
111                 {
112                         .id      = V4L2_CID_HUE,
113                         .type    = V4L2_CTRL_TYPE_INTEGER,
114                         .name    = "Hue",
115                         .minimum = -90,
116                         .maximum = 90,
117                         .step    = 1,
118                         .default_value = 0,
119                 },
120                 .set_control = sethue
121         },
122 [SATURATION] = {
123                 {
124                         .id      = V4L2_CID_SATURATION,
125                         .type    = V4L2_CTRL_TYPE_INTEGER,
126                         .name    = "Saturation",
127                         .minimum = 0,
128                         .maximum = 255,
129                         .step    = 1,
130                         .default_value = 64,
131                 },
132                 .set_control = setsaturation
133         },
134 [BRIGHTNESS] = {
135                 {
136                         .id      = V4L2_CID_BRIGHTNESS,
137                         .type    = V4L2_CTRL_TYPE_INTEGER,
138                         .name    = "Brightness",
139                         .minimum = 0,
140                         .maximum = 255,
141                         .step    = 1,
142                         .default_value = 0,
143                 },
144                 .set_control = setbrightness
145         },
146 [CONTRAST] = {
147                 {
148                         .id      = V4L2_CID_CONTRAST,
149                         .type    = V4L2_CTRL_TYPE_INTEGER,
150                         .name    = "Contrast",
151                         .minimum = 0,
152                         .maximum = 255,
153                         .step    = 1,
154                         .default_value = 32,
155                 },
156                 .set_control = setcontrast
157         },
158 [GAIN] = {
159                 {
160                         .id      = V4L2_CID_GAIN,
161                         .type    = V4L2_CTRL_TYPE_INTEGER,
162                         .name    = "Main Gain",
163                         .minimum = 0,
164                         .maximum = 63,
165                         .step    = 1,
166                         .default_value = 20,
167                 },
168                 .set_control = setgain
169         },
170 [EXPOSURE] = {
171                 {
172                         .id      = V4L2_CID_EXPOSURE,
173                         .type    = V4L2_CTRL_TYPE_INTEGER,
174                         .name    = "Exposure",
175                         .minimum = 0,
176                         .maximum = 255,
177                         .step    = 1,
178                         .default_value = 120,
179                 },
180                 .set_control = setexposure
181         },
182 [AGC] = {
183                 {
184                         .id      = V4L2_CID_AUTOGAIN,
185                         .type    = V4L2_CTRL_TYPE_BOOLEAN,
186                         .name    = "Auto Gain",
187                         .minimum = 0,
188                         .maximum = 1,
189                         .step    = 1,
190                         .default_value = 1,
191                 },
192                 .set_control = setagc
193         },
194 [AWB] = {
195                 {
196                         .id      = V4L2_CID_AUTO_WHITE_BALANCE,
197                         .type    = V4L2_CTRL_TYPE_BOOLEAN,
198                         .name    = "Auto White Balance",
199                         .minimum = 0,
200                         .maximum = 1,
201                         .step    = 1,
202                         .default_value = 1,
203                 },
204                 .set_control = setawb
205         },
206 [AEC] = {
207                 {
208                         .id      = V4L2_CID_EXPOSURE_AUTO,
209                         .type    = V4L2_CTRL_TYPE_BOOLEAN,
210                         .name    = "Auto Exposure",
211                         .minimum = 0,
212                         .maximum = 1,
213                         .step    = 1,
214                         .default_value = 1,
215                 },
216                 .set_control = setaec
217         },
218 [SHARPNESS] = {
219                 {
220                         .id      = V4L2_CID_SHARPNESS,
221                         .type    = V4L2_CTRL_TYPE_INTEGER,
222                         .name    = "Sharpness",
223                         .minimum = 0,
224                         .maximum = 63,
225                         .step    = 1,
226                         .default_value = 0,
227                 },
228                 .set_control = setsharpness
229         },
230 [HFLIP] = {
231                 {
232                         .id      = V4L2_CID_HFLIP,
233                         .type    = V4L2_CTRL_TYPE_BOOLEAN,
234                         .name    = "HFlip",
235                         .minimum = 0,
236                         .maximum = 1,
237                         .step    = 1,
238                         .default_value = 0,
239                 },
240                 .set_control = sethvflip
241         },
242 [VFLIP] = {
243                 {
244                         .id      = V4L2_CID_VFLIP,
245                         .type    = V4L2_CTRL_TYPE_BOOLEAN,
246                         .name    = "VFlip",
247                         .minimum = 0,
248                         .maximum = 1,
249                         .step    = 1,
250                         .default_value = 0,
251                 },
252                 .set_control = sethvflip
253         },
254 [LIGHTFREQ] = {
255                 {
256                         .id      = V4L2_CID_POWER_LINE_FREQUENCY,
257                         .type    = V4L2_CTRL_TYPE_MENU,
258                         .name    = "Light Frequency Filter",
259                         .minimum = 0,
260                         .maximum = 1,
261                         .step    = 1,
262                         .default_value = 0,
263                 },
264                 .set_control = setlightfreq
265         },
266 };
267
268 static const struct v4l2_pix_format ov772x_mode[] = {
269         {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
270          .bytesperline = 320 * 2,
271          .sizeimage = 320 * 240 * 2,
272          .colorspace = V4L2_COLORSPACE_SRGB,
273          .priv = 1},
274         {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
275          .bytesperline = 640 * 2,
276          .sizeimage = 640 * 480 * 2,
277          .colorspace = V4L2_COLORSPACE_SRGB,
278          .priv = 0},
279 };
280 static const struct v4l2_pix_format ov767x_mode[] = {
281         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
282                 .bytesperline = 320,
283                 .sizeimage = 320 * 240 * 3 / 8 + 590,
284                 .colorspace = V4L2_COLORSPACE_JPEG},
285         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
286                 .bytesperline = 640,
287                 .sizeimage = 640 * 480 * 3 / 8 + 590,
288                 .colorspace = V4L2_COLORSPACE_JPEG},
289 };
290
291 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
292 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
293
294 static const struct framerates ov772x_framerates[] = {
295         { /* 320x240 */
296                 .rates = qvga_rates,
297                 .nrates = ARRAY_SIZE(qvga_rates),
298         },
299         { /* 640x480 */
300                 .rates = vga_rates,
301                 .nrates = ARRAY_SIZE(vga_rates),
302         },
303 };
304
305 struct reg_array {
306         const u8 (*val)[2];
307         int len;
308 };
309
310 static const u8 bridge_init_767x[][2] = {
311 /* comments from the ms-win file apollo7670.set */
312 /* str1 */
313         {0xf1, 0x42},
314         {0x88, 0xf8},
315         {0x89, 0xff},
316         {0x76, 0x03},
317         {0x92, 0x03},
318         {0x95, 0x10},
319         {0xe2, 0x00},
320         {0xe7, 0x3e},
321         {0x8d, 0x1c},
322         {0x8e, 0x00},
323         {0x8f, 0x00},
324         {0x1f, 0x00},
325         {0xc3, 0xf9},
326         {0x89, 0xff},
327         {0x88, 0xf8},
328         {0x76, 0x03},
329         {0x92, 0x01},
330         {0x93, 0x18},
331         {0x1c, 0x00},
332         {0x1d, 0x48},
333         {0x1d, 0x00},
334         {0x1d, 0xff},
335         {0x1d, 0x02},
336         {0x1d, 0x58},
337         {0x1d, 0x00},
338         {0x1c, 0x0a},
339         {0x1d, 0x0a},
340         {0x1d, 0x0e},
341         {0xc0, 0x50},   /* HSize 640 */
342         {0xc1, 0x3c},   /* VSize 480 */
343         {0x34, 0x05},   /* enable Audio Suspend mode */
344         {0xc2, 0x0c},   /* Input YUV */
345         {0xc3, 0xf9},   /* enable PRE */
346         {0x34, 0x05},   /* enable Audio Suspend mode */
347         {0xe7, 0x2e},   /* this solves failure of "SuspendResumeTest" */
348         {0x31, 0xf9},   /* enable 1.8V Suspend */
349         {0x35, 0x02},   /* turn on JPEG */
350         {0xd9, 0x10},
351         {0x25, 0x42},   /* GPIO[8]:Input */
352         {0x94, 0x11},   /* If the default setting is loaded when
353                          * system boots up, this flag is closed here */
354 };
355 static const u8 sensor_init_767x[][2] = {
356         {0x12, 0x80},
357         {0x11, 0x03},
358         {0x3a, 0x04},
359         {0x12, 0x00},
360         {0x17, 0x13},
361         {0x18, 0x01},
362         {0x32, 0xb6},
363         {0x19, 0x02},
364         {0x1a, 0x7a},
365         {0x03, 0x0a},
366         {0x0c, 0x00},
367         {0x3e, 0x00},
368         {0x70, 0x3a},
369         {0x71, 0x35},
370         {0x72, 0x11},
371         {0x73, 0xf0},
372         {0xa2, 0x02},
373         {0x7a, 0x2a},   /* set Gamma=1.6 below */
374         {0x7b, 0x12},
375         {0x7c, 0x1d},
376         {0x7d, 0x2d},
377         {0x7e, 0x45},
378         {0x7f, 0x50},
379         {0x80, 0x59},
380         {0x81, 0x62},
381         {0x82, 0x6b},
382         {0x83, 0x73},
383         {0x84, 0x7b},
384         {0x85, 0x8a},
385         {0x86, 0x98},
386         {0x87, 0xb2},
387         {0x88, 0xca},
388         {0x89, 0xe0},
389         {0x13, 0xe0},
390         {0x00, 0x00},
391         {0x10, 0x00},
392         {0x0d, 0x40},
393         {0x14, 0x38},   /* gain max 16x */
394         {0xa5, 0x05},
395         {0xab, 0x07},
396         {0x24, 0x95},
397         {0x25, 0x33},
398         {0x26, 0xe3},
399         {0x9f, 0x78},
400         {0xa0, 0x68},
401         {0xa1, 0x03},
402         {0xa6, 0xd8},
403         {0xa7, 0xd8},
404         {0xa8, 0xf0},
405         {0xa9, 0x90},
406         {0xaa, 0x94},
407         {0x13, 0xe5},
408         {0x0e, 0x61},
409         {0x0f, 0x4b},
410         {0x16, 0x02},
411         {0x21, 0x02},
412         {0x22, 0x91},
413         {0x29, 0x07},
414         {0x33, 0x0b},
415         {0x35, 0x0b},
416         {0x37, 0x1d},
417         {0x38, 0x71},
418         {0x39, 0x2a},
419         {0x3c, 0x78},
420         {0x4d, 0x40},
421         {0x4e, 0x20},
422         {0x69, 0x00},
423         {0x6b, 0x4a},
424         {0x74, 0x10},
425         {0x8d, 0x4f},
426         {0x8e, 0x00},
427         {0x8f, 0x00},
428         {0x90, 0x00},
429         {0x91, 0x00},
430         {0x96, 0x00},
431         {0x9a, 0x80},
432         {0xb0, 0x84},
433         {0xb1, 0x0c},
434         {0xb2, 0x0e},
435         {0xb3, 0x82},
436         {0xb8, 0x0a},
437         {0x43, 0x0a},
438         {0x44, 0xf0},
439         {0x45, 0x34},
440         {0x46, 0x58},
441         {0x47, 0x28},
442         {0x48, 0x3a},
443         {0x59, 0x88},
444         {0x5a, 0x88},
445         {0x5b, 0x44},
446         {0x5c, 0x67},
447         {0x5d, 0x49},
448         {0x5e, 0x0e},
449         {0x6c, 0x0a},
450         {0x6d, 0x55},
451         {0x6e, 0x11},
452         {0x6f, 0x9f},
453         {0x6a, 0x40},
454         {0x01, 0x40},
455         {0x02, 0x40},
456         {0x13, 0xe7},
457         {0x4f, 0x80},
458         {0x50, 0x80},
459         {0x51, 0x00},
460         {0x52, 0x22},
461         {0x53, 0x5e},
462         {0x54, 0x80},
463         {0x58, 0x9e},
464         {0x41, 0x08},
465         {0x3f, 0x00},
466         {0x75, 0x04},
467         {0x76, 0xe1},
468         {0x4c, 0x00},
469         {0x77, 0x01},
470         {0x3d, 0xc2},
471         {0x4b, 0x09},
472         {0xc9, 0x60},
473         {0x41, 0x38},   /* jfm: auto sharpness + auto de-noise  */
474         {0x56, 0x40},
475         {0x34, 0x11},
476         {0x3b, 0xc2},
477         {0xa4, 0x8a},   /* Night mode trigger point */
478         {0x96, 0x00},
479         {0x97, 0x30},
480         {0x98, 0x20},
481         {0x99, 0x20},
482         {0x9a, 0x84},
483         {0x9b, 0x29},
484         {0x9c, 0x03},
485         {0x9d, 0x4c},
486         {0x9e, 0x3f},
487         {0x78, 0x04},
488         {0x79, 0x01},
489         {0xc8, 0xf0},
490         {0x79, 0x0f},
491         {0xc8, 0x00},
492         {0x79, 0x10},
493         {0xc8, 0x7e},
494         {0x79, 0x0a},
495         {0xc8, 0x80},
496         {0x79, 0x0b},
497         {0xc8, 0x01},
498         {0x79, 0x0c},
499         {0xc8, 0x0f},
500         {0x79, 0x0d},
501         {0xc8, 0x20},
502         {0x79, 0x09},
503         {0xc8, 0x80},
504         {0x79, 0x02},
505         {0xc8, 0xc0},
506         {0x79, 0x03},
507         {0xc8, 0x20},
508         {0x79, 0x26},
509 };
510 static const u8 bridge_start_vga_767x[][2] = {
511 /* str59 JPG */
512         {0x94, 0xaa},
513         {0xf1, 0x42},
514         {0xe5, 0x04},
515         {0xc0, 0x50},
516         {0xc1, 0x3c},
517         {0xc2, 0x0c},
518         {0x35, 0x02},   /* turn on JPEG */
519         {0xd9, 0x10},
520         {0xda, 0x00},   /* for higher clock rate(30fps) */
521         {0x34, 0x05},   /* enable Audio Suspend mode */
522         {0xc3, 0xf9},   /* enable PRE */
523         {0x8c, 0x00},   /* CIF VSize LSB[2:0] */
524         {0x8d, 0x1c},   /* output YUV */
525 /*      {0x34, 0x05},    * enable Audio Suspend mode (?) */
526         {0x50, 0x00},   /* H/V divider=0 */
527         {0x51, 0xa0},   /* input H=640/4 */
528         {0x52, 0x3c},   /* input V=480/4 */
529         {0x53, 0x00},   /* offset X=0 */
530         {0x54, 0x00},   /* offset Y=0 */
531         {0x55, 0x00},   /* H/V size[8]=0 */
532         {0x57, 0x00},   /* H-size[9]=0 */
533         {0x5c, 0x00},   /* output size[9:8]=0 */
534         {0x5a, 0xa0},   /* output H=640/4 */
535         {0x5b, 0x78},   /* output V=480/4 */
536         {0x1c, 0x0a},
537         {0x1d, 0x0a},
538         {0x94, 0x11},
539 };
540 static const u8 sensor_start_vga_767x[][2] = {
541         {0x11, 0x01},
542         {0x1e, 0x04},
543         {0x19, 0x02},
544         {0x1a, 0x7a},
545 };
546 static const u8 bridge_start_qvga_767x[][2] = {
547 /* str86 JPG */
548         {0x94, 0xaa},
549         {0xf1, 0x42},
550         {0xe5, 0x04},
551         {0xc0, 0x80},
552         {0xc1, 0x60},
553         {0xc2, 0x0c},
554         {0x35, 0x02},   /* turn on JPEG */
555         {0xd9, 0x10},
556         {0xc0, 0x50},   /* CIF HSize 640 */
557         {0xc1, 0x3c},   /* CIF VSize 480 */
558         {0x8c, 0x00},   /* CIF VSize LSB[2:0] */
559         {0x8d, 0x1c},   /* output YUV */
560         {0x34, 0x05},   /* enable Audio Suspend mode */
561         {0xc2, 0x4c},   /* output YUV and Enable DCW */
562         {0xc3, 0xf9},   /* enable PRE */
563         {0x1c, 0x00},   /* indirect addressing */
564         {0x1d, 0x48},   /* output YUV422 */
565         {0x50, 0x89},   /* H/V divider=/2; plus DCW AVG */
566         {0x51, 0xa0},   /* DCW input H=640/4 */
567         {0x52, 0x78},   /* DCW input V=480/4 */
568         {0x53, 0x00},   /* offset X=0 */
569         {0x54, 0x00},   /* offset Y=0 */
570         {0x55, 0x00},   /* H/V size[8]=0 */
571         {0x57, 0x00},   /* H-size[9]=0 */
572         {0x5c, 0x00},   /* DCW output size[9:8]=0 */
573         {0x5a, 0x50},   /* DCW output H=320/4 */
574         {0x5b, 0x3c},   /* DCW output V=240/4 */
575         {0x1c, 0x0a},
576         {0x1d, 0x0a},
577         {0x94, 0x11},
578 };
579 static const u8 sensor_start_qvga_767x[][2] = {
580         {0x11, 0x01},
581         {0x1e, 0x04},
582         {0x19, 0x02},
583         {0x1a, 0x7a},
584 };
585
586 static const u8 bridge_init_772x[][2] = {
587         { 0xc2, 0x0c },
588         { 0x88, 0xf8 },
589         { 0xc3, 0x69 },
590         { 0x89, 0xff },
591         { 0x76, 0x03 },
592         { 0x92, 0x01 },
593         { 0x93, 0x18 },
594         { 0x94, 0x10 },
595         { 0x95, 0x10 },
596         { 0xe2, 0x00 },
597         { 0xe7, 0x3e },
598
599         { 0x96, 0x00 },
600
601         { 0x97, 0x20 },
602         { 0x97, 0x20 },
603         { 0x97, 0x20 },
604         { 0x97, 0x0a },
605         { 0x97, 0x3f },
606         { 0x97, 0x4a },
607         { 0x97, 0x20 },
608         { 0x97, 0x15 },
609         { 0x97, 0x0b },
610
611         { 0x8e, 0x40 },
612         { 0x1f, 0x81 },
613         { 0x34, 0x05 },
614         { 0xe3, 0x04 },
615         { 0x88, 0x00 },
616         { 0x89, 0x00 },
617         { 0x76, 0x00 },
618         { 0xe7, 0x2e },
619         { 0x31, 0xf9 },
620         { 0x25, 0x42 },
621         { 0x21, 0xf0 },
622
623         { 0x1c, 0x00 },
624         { 0x1d, 0x40 },
625         { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
626         { 0x1d, 0x00 }, /* payload size */
627
628         { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
629         { 0x1d, 0x58 }, /* frame size */
630         { 0x1d, 0x00 }, /* frame size */
631
632         { 0x1c, 0x0a },
633         { 0x1d, 0x08 }, /* turn on UVC header */
634         { 0x1d, 0x0e }, /* .. */
635
636         { 0x8d, 0x1c },
637         { 0x8e, 0x80 },
638         { 0xe5, 0x04 },
639
640         { 0xc0, 0x50 },
641         { 0xc1, 0x3c },
642         { 0xc2, 0x0c },
643 };
644 static const u8 sensor_init_772x[][2] = {
645         { 0x12, 0x80 },
646         { 0x11, 0x01 },
647 /*fixme: better have a delay?*/
648         { 0x11, 0x01 },
649         { 0x11, 0x01 },
650         { 0x11, 0x01 },
651         { 0x11, 0x01 },
652         { 0x11, 0x01 },
653         { 0x11, 0x01 },
654         { 0x11, 0x01 },
655         { 0x11, 0x01 },
656         { 0x11, 0x01 },
657         { 0x11, 0x01 },
658
659         { 0x3d, 0x03 },
660         { 0x17, 0x26 },
661         { 0x18, 0xa0 },
662         { 0x19, 0x07 },
663         { 0x1a, 0xf0 },
664         { 0x32, 0x00 },
665         { 0x29, 0xa0 },
666         { 0x2c, 0xf0 },
667         { 0x65, 0x20 },
668         { 0x11, 0x01 },
669         { 0x42, 0x7f },
670         { 0x63, 0xaa },         /* AWB - was e0 */
671         { 0x64, 0xff },
672         { 0x66, 0x00 },
673         { 0x13, 0xf0 },         /* com8 */
674         { 0x0d, 0x41 },
675         { 0x0f, 0xc5 },
676         { 0x14, 0x11 },
677
678         { 0x22, 0x7f },
679         { 0x23, 0x03 },
680         { 0x24, 0x40 },
681         { 0x25, 0x30 },
682         { 0x26, 0xa1 },
683         { 0x2a, 0x00 },
684         { 0x2b, 0x00 },
685         { 0x6b, 0xaa },
686         { 0x13, 0xff },         /* AWB */
687
688         { 0x90, 0x05 },
689         { 0x91, 0x01 },
690         { 0x92, 0x03 },
691         { 0x93, 0x00 },
692         { 0x94, 0x60 },
693         { 0x95, 0x3c },
694         { 0x96, 0x24 },
695         { 0x97, 0x1e },
696         { 0x98, 0x62 },
697         { 0x99, 0x80 },
698         { 0x9a, 0x1e },
699         { 0x9b, 0x08 },
700         { 0x9c, 0x20 },
701         { 0x9e, 0x81 },
702
703         { 0xa6, 0x07 },
704         { 0x7e, 0x0c },
705         { 0x7f, 0x16 },
706         { 0x80, 0x2a },
707         { 0x81, 0x4e },
708         { 0x82, 0x61 },
709         { 0x83, 0x6f },
710         { 0x84, 0x7b },
711         { 0x85, 0x86 },
712         { 0x86, 0x8e },
713         { 0x87, 0x97 },
714         { 0x88, 0xa4 },
715         { 0x89, 0xaf },
716         { 0x8a, 0xc5 },
717         { 0x8b, 0xd7 },
718         { 0x8c, 0xe8 },
719         { 0x8d, 0x20 },
720
721         { 0x0c, 0x90 },
722
723         { 0x2b, 0x00 },
724         { 0x22, 0x7f },
725         { 0x23, 0x03 },
726         { 0x11, 0x01 },
727         { 0x0c, 0xd0 },
728         { 0x64, 0xff },
729         { 0x0d, 0x41 },
730
731         { 0x14, 0x41 },
732         { 0x0e, 0xcd },
733         { 0xac, 0xbf },
734         { 0x8e, 0x00 },         /* De-noise threshold */
735         { 0x0c, 0xd0 }
736 };
737 static const u8 bridge_start_vga_772x[][2] = {
738         {0x1c, 0x00},
739         {0x1d, 0x40},
740         {0x1d, 0x02},
741         {0x1d, 0x00},
742         {0x1d, 0x02},
743         {0x1d, 0x58},
744         {0x1d, 0x00},
745         {0xc0, 0x50},
746         {0xc1, 0x3c},
747 };
748 static const u8 sensor_start_vga_772x[][2] = {
749         {0x12, 0x00},
750         {0x17, 0x26},
751         {0x18, 0xa0},
752         {0x19, 0x07},
753         {0x1a, 0xf0},
754         {0x29, 0xa0},
755         {0x2c, 0xf0},
756         {0x65, 0x20},
757 };
758 static const u8 bridge_start_qvga_772x[][2] = {
759         {0x1c, 0x00},
760         {0x1d, 0x40},
761         {0x1d, 0x02},
762         {0x1d, 0x00},
763         {0x1d, 0x01},
764         {0x1d, 0x4b},
765         {0x1d, 0x00},
766         {0xc0, 0x28},
767         {0xc1, 0x1e},
768 };
769 static const u8 sensor_start_qvga_772x[][2] = {
770         {0x12, 0x40},
771         {0x17, 0x3f},
772         {0x18, 0x50},
773         {0x19, 0x03},
774         {0x1a, 0x78},
775         {0x29, 0x50},
776         {0x2c, 0x78},
777         {0x65, 0x2f},
778 };
779
780 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
781 {
782         struct usb_device *udev = gspca_dev->dev;
783         int ret;
784
785         if (gspca_dev->usb_err < 0)
786                 return;
787
788         PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
789         gspca_dev->usb_buf[0] = val;
790         ret = usb_control_msg(udev,
791                               usb_sndctrlpipe(udev, 0),
792                               0x01,
793                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
794                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
795         if (ret < 0) {
796                 pr_err("write failed %d\n", ret);
797                 gspca_dev->usb_err = ret;
798         }
799 }
800
801 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
802 {
803         struct usb_device *udev = gspca_dev->dev;
804         int ret;
805
806         if (gspca_dev->usb_err < 0)
807                 return 0;
808         ret = usb_control_msg(udev,
809                               usb_rcvctrlpipe(udev, 0),
810                               0x01,
811                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
812                               0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
813         PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
814         if (ret < 0) {
815                 pr_err("read failed %d\n", ret);
816                 gspca_dev->usb_err = ret;
817         }
818         return gspca_dev->usb_buf[0];
819 }
820
821 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
822  * (direction and output)? */
823 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
824 {
825         u8 data;
826
827         PDEBUG(D_CONF, "led status: %d", status);
828
829         data = ov534_reg_read(gspca_dev, 0x21);
830         data |= 0x80;
831         ov534_reg_write(gspca_dev, 0x21, data);
832
833         data = ov534_reg_read(gspca_dev, 0x23);
834         if (status)
835                 data |= 0x80;
836         else
837                 data &= ~0x80;
838
839         ov534_reg_write(gspca_dev, 0x23, data);
840
841         if (!status) {
842                 data = ov534_reg_read(gspca_dev, 0x21);
843                 data &= ~0x80;
844                 ov534_reg_write(gspca_dev, 0x21, data);
845         }
846 }
847
848 static int sccb_check_status(struct gspca_dev *gspca_dev)
849 {
850         u8 data;
851         int i;
852
853         for (i = 0; i < 5; i++) {
854                 msleep(10);
855                 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
856
857                 switch (data) {
858                 case 0x00:
859                         return 1;
860                 case 0x04:
861                         return 0;
862                 case 0x03:
863                         break;
864                 default:
865                         PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
866                                data, i + 1);
867                 }
868         }
869         return 0;
870 }
871
872 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
873 {
874         PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
875         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
876         ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
877         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
878
879         if (!sccb_check_status(gspca_dev)) {
880                 pr_err("sccb_reg_write failed\n");
881                 gspca_dev->usb_err = -EIO;
882         }
883 }
884
885 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
886 {
887         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
888         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
889         if (!sccb_check_status(gspca_dev))
890                 pr_err("sccb_reg_read failed 1\n");
891
892         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
893         if (!sccb_check_status(gspca_dev))
894                 pr_err("sccb_reg_read failed 2\n");
895
896         return ov534_reg_read(gspca_dev, OV534_REG_READ);
897 }
898
899 /* output a bridge sequence (reg - val) */
900 static void reg_w_array(struct gspca_dev *gspca_dev,
901                         const u8 (*data)[2], int len)
902 {
903         while (--len >= 0) {
904                 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
905                 data++;
906         }
907 }
908
909 /* output a sensor sequence (reg - val) */
910 static void sccb_w_array(struct gspca_dev *gspca_dev,
911                         const u8 (*data)[2], int len)
912 {
913         while (--len >= 0) {
914                 if ((*data)[0] != 0xff) {
915                         sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
916                 } else {
917                         sccb_reg_read(gspca_dev, (*data)[1]);
918                         sccb_reg_write(gspca_dev, 0xff, 0x00);
919                 }
920                 data++;
921         }
922 }
923
924 /* ov772x specific controls */
925 static void set_frame_rate(struct gspca_dev *gspca_dev)
926 {
927         struct sd *sd = (struct sd *) gspca_dev;
928         int i;
929         struct rate_s {
930                 u8 fps;
931                 u8 r11;
932                 u8 r0d;
933                 u8 re5;
934         };
935         const struct rate_s *r;
936         static const struct rate_s rate_0[] = { /* 640x480 */
937                 {60, 0x01, 0xc1, 0x04},
938                 {50, 0x01, 0x41, 0x02},
939                 {40, 0x02, 0xc1, 0x04},
940                 {30, 0x04, 0x81, 0x02},
941                 {15, 0x03, 0x41, 0x04},
942         };
943         static const struct rate_s rate_1[] = { /* 320x240 */
944                 {125, 0x02, 0x81, 0x02},
945                 {100, 0x02, 0xc1, 0x04},
946                 {75, 0x03, 0xc1, 0x04},
947                 {60, 0x04, 0xc1, 0x04},
948                 {50, 0x02, 0x41, 0x04},
949                 {40, 0x03, 0x41, 0x04},
950                 {30, 0x04, 0x41, 0x04},
951         };
952
953         if (sd->sensor != SENSOR_OV772x)
954                 return;
955         if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
956                 r = rate_0;
957                 i = ARRAY_SIZE(rate_0);
958         } else {
959                 r = rate_1;
960                 i = ARRAY_SIZE(rate_1);
961         }
962         while (--i > 0) {
963                 if (sd->frame_rate >= r->fps)
964                         break;
965                 r++;
966         }
967
968         sccb_reg_write(gspca_dev, 0x11, r->r11);
969         sccb_reg_write(gspca_dev, 0x0d, r->r0d);
970         ov534_reg_write(gspca_dev, 0xe5, r->re5);
971
972         PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
973 }
974
975 static void sethue(struct gspca_dev *gspca_dev)
976 {
977         struct sd *sd = (struct sd *) gspca_dev;
978         int val;
979
980         val = sd->ctrls[HUE].val;
981         if (sd->sensor == SENSOR_OV767x) {
982                 /* TBD */
983         } else {
984                 s16 huesin;
985                 s16 huecos;
986
987                 /* fixp_sin and fixp_cos accept only positive values, while
988                  * our val is between -90 and 90
989                  */
990                 val += 360;
991
992                 /* According to the datasheet the registers expect HUESIN and
993                  * HUECOS to be the result of the trigonometric functions,
994                  * scaled by 0x80.
995                  *
996                  * The 0x100 here represents the maximun absolute value
997                  * returned byt fixp_sin and fixp_cos, so the scaling will
998                  * consider the result like in the interval [-1.0, 1.0].
999                  */
1000                 huesin = fixp_sin(val) * 0x80 / 0x100;
1001                 huecos = fixp_cos(val) * 0x80 / 0x100;
1002
1003                 if (huesin < 0) {
1004                         sccb_reg_write(gspca_dev, 0xab,
1005                                 sccb_reg_read(gspca_dev, 0xab) | 0x2);
1006                         huesin = -huesin;
1007                 } else {
1008                         sccb_reg_write(gspca_dev, 0xab,
1009                                 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
1010
1011                 }
1012                 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
1013                 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
1014         }
1015 }
1016
1017 static void setsaturation(struct gspca_dev *gspca_dev)
1018 {
1019         struct sd *sd = (struct sd *) gspca_dev;
1020         int val;
1021
1022         val = sd->ctrls[SATURATION].val;
1023         if (sd->sensor == SENSOR_OV767x) {
1024                 int i;
1025                 static u8 color_tb[][6] = {
1026                         {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1027                         {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1028                         {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1029                         {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1030                         {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1031                         {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1032                         {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1033                 };
1034
1035                 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
1036                         sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
1037         } else {
1038                 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
1039                 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
1040         }
1041 }
1042
1043 static void setbrightness(struct gspca_dev *gspca_dev)
1044 {
1045         struct sd *sd = (struct sd *) gspca_dev;
1046         int val;
1047
1048         val = sd->ctrls[BRIGHTNESS].val;
1049         if (sd->sensor == SENSOR_OV767x) {
1050                 if (val < 0)
1051                         val = 0x80 - val;
1052                 sccb_reg_write(gspca_dev, 0x55, val);   /* bright */
1053         } else {
1054                 sccb_reg_write(gspca_dev, 0x9b, val);
1055         }
1056 }
1057
1058 static void setcontrast(struct gspca_dev *gspca_dev)
1059 {
1060         struct sd *sd = (struct sd *) gspca_dev;
1061         u8 val;
1062
1063         val = sd->ctrls[CONTRAST].val;
1064         if (sd->sensor == SENSOR_OV767x)
1065                 sccb_reg_write(gspca_dev, 0x56, val);   /* contras */
1066         else
1067                 sccb_reg_write(gspca_dev, 0x9c, val);
1068 }
1069
1070 static void setgain(struct gspca_dev *gspca_dev)
1071 {
1072         struct sd *sd = (struct sd *) gspca_dev;
1073         u8 val;
1074
1075         if (sd->ctrls[AGC].val)
1076                 return;
1077
1078         val = sd->ctrls[GAIN].val;
1079         switch (val & 0x30) {
1080         case 0x00:
1081                 val &= 0x0f;
1082                 break;
1083         case 0x10:
1084                 val &= 0x0f;
1085                 val |= 0x30;
1086                 break;
1087         case 0x20:
1088                 val &= 0x0f;
1089                 val |= 0x70;
1090                 break;
1091         default:
1092 /*      case 0x30: */
1093                 val &= 0x0f;
1094                 val |= 0xf0;
1095                 break;
1096         }
1097         sccb_reg_write(gspca_dev, 0x00, val);
1098 }
1099
1100 static void setexposure(struct gspca_dev *gspca_dev)
1101 {
1102         struct sd *sd = (struct sd *) gspca_dev;
1103         u8 val;
1104
1105         if (sd->ctrls[AEC].val)
1106                 return;
1107
1108         val = sd->ctrls[EXPOSURE].val;
1109         if (sd->sensor == SENSOR_OV767x) {
1110
1111                 /* set only aec[9:2] */
1112                 sccb_reg_write(gspca_dev, 0x10, val);   /* aech */
1113         } else {
1114
1115                 /* 'val' is one byte and represents half of the exposure value
1116                  * we are going to set into registers, a two bytes value:
1117                  *
1118                  *    MSB: ((u16) val << 1) >> 8   == val >> 7
1119                  *    LSB: ((u16) val << 1) & 0xff == val << 1
1120                  */
1121                 sccb_reg_write(gspca_dev, 0x08, val >> 7);
1122                 sccb_reg_write(gspca_dev, 0x10, val << 1);
1123         }
1124 }
1125
1126 static void setagc(struct gspca_dev *gspca_dev)
1127 {
1128         struct sd *sd = (struct sd *) gspca_dev;
1129
1130         if (sd->ctrls[AGC].val) {
1131                 sccb_reg_write(gspca_dev, 0x13,
1132                                 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1133                 sccb_reg_write(gspca_dev, 0x64,
1134                                 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1135         } else {
1136                 sccb_reg_write(gspca_dev, 0x13,
1137                                 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1138                 sccb_reg_write(gspca_dev, 0x64,
1139                                 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1140
1141                 setgain(gspca_dev);
1142         }
1143 }
1144
1145 static void setawb(struct gspca_dev *gspca_dev)
1146 {
1147         struct sd *sd = (struct sd *) gspca_dev;
1148
1149         if (sd->ctrls[AWB].val) {
1150                 sccb_reg_write(gspca_dev, 0x13,
1151                                 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1152                 if (sd->sensor == SENSOR_OV772x)
1153                         sccb_reg_write(gspca_dev, 0x63,
1154                                 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1155         } else {
1156                 sccb_reg_write(gspca_dev, 0x13,
1157                                 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1158                 if (sd->sensor == SENSOR_OV772x)
1159                         sccb_reg_write(gspca_dev, 0x63,
1160                                 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1161         }
1162 }
1163
1164 static void setaec(struct gspca_dev *gspca_dev)
1165 {
1166         struct sd *sd = (struct sd *) gspca_dev;
1167         u8 data;
1168
1169         data = sd->sensor == SENSOR_OV767x ?
1170                         0x05 :          /* agc + aec */
1171                         0x01;           /* agc */
1172         if (sd->ctrls[AEC].val)
1173                 sccb_reg_write(gspca_dev, 0x13,
1174                                 sccb_reg_read(gspca_dev, 0x13) | data);
1175         else {
1176                 sccb_reg_write(gspca_dev, 0x13,
1177                                 sccb_reg_read(gspca_dev, 0x13) & ~data);
1178                 if (sd->sensor == SENSOR_OV767x)
1179                         sd->ctrls[EXPOSURE].val =
1180                                 sccb_reg_read(gspca_dev, 10);   /* aech */
1181                 else
1182                         setexposure(gspca_dev);
1183         }
1184 }
1185
1186 static void setsharpness(struct gspca_dev *gspca_dev)
1187 {
1188         struct sd *sd = (struct sd *) gspca_dev;
1189         u8 val;
1190
1191         val = sd->ctrls[SHARPNESS].val;
1192         sccb_reg_write(gspca_dev, 0x91, val);   /* Auto de-noise threshold */
1193         sccb_reg_write(gspca_dev, 0x8e, val);   /* De-noise threshold */
1194 }
1195
1196 static void sethvflip(struct gspca_dev *gspca_dev)
1197 {
1198         struct sd *sd = (struct sd *) gspca_dev;
1199         u8 val;
1200
1201         if (sd->sensor == SENSOR_OV767x) {
1202                 val = sccb_reg_read(gspca_dev, 0x1e);   /* mvfp */
1203                 val &= ~0x30;
1204                 if (sd->ctrls[HFLIP].val)
1205                         val |= 0x20;
1206                 if (sd->ctrls[VFLIP].val)
1207                         val |= 0x10;
1208                 sccb_reg_write(gspca_dev, 0x1e, val);
1209         } else {
1210                 val = sccb_reg_read(gspca_dev, 0x0c);
1211                 val &= ~0xc0;
1212                 if (sd->ctrls[HFLIP].val == 0)
1213                         val |= 0x40;
1214                 if (sd->ctrls[VFLIP].val == 0)
1215                         val |= 0x80;
1216                 sccb_reg_write(gspca_dev, 0x0c, val);
1217         }
1218 }
1219
1220 static void setlightfreq(struct gspca_dev *gspca_dev)
1221 {
1222         struct sd *sd = (struct sd *) gspca_dev;
1223         u8 val;
1224
1225         val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
1226         if (sd->sensor == SENSOR_OV767x) {
1227                 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1228                 if (val)
1229                         val = 0x9d;     /* insert dummy to 25fps for 50Hz */
1230         }
1231         sccb_reg_write(gspca_dev, 0x2b, val);
1232 }
1233
1234
1235 /* this function is called at probe time */
1236 static int sd_config(struct gspca_dev *gspca_dev,
1237                      const struct usb_device_id *id)
1238 {
1239         struct sd *sd = (struct sd *) gspca_dev;
1240         struct cam *cam;
1241
1242         cam = &gspca_dev->cam;
1243
1244         cam->ctrls = sd->ctrls;
1245
1246         cam->cam_mode = ov772x_mode;
1247         cam->nmodes = ARRAY_SIZE(ov772x_mode);
1248
1249         sd->frame_rate = 30;
1250
1251         return 0;
1252 }
1253
1254 /* this function is called at probe and resume time */
1255 static int sd_init(struct gspca_dev *gspca_dev)
1256 {
1257         struct sd *sd = (struct sd *) gspca_dev;
1258         u16 sensor_id;
1259         static const struct reg_array bridge_init[NSENSORS] = {
1260         [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1261         [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1262         };
1263         static const struct reg_array sensor_init[NSENSORS] = {
1264         [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1265         [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1266         };
1267
1268         /* reset bridge */
1269         ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1270         ov534_reg_write(gspca_dev, 0xe0, 0x08);
1271         msleep(100);
1272
1273         /* initialize the sensor address */
1274         ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1275
1276         /* reset sensor */
1277         sccb_reg_write(gspca_dev, 0x12, 0x80);
1278         msleep(10);
1279
1280         /* probe the sensor */
1281         sccb_reg_read(gspca_dev, 0x0a);
1282         sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1283         sccb_reg_read(gspca_dev, 0x0b);
1284         sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1285         PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1286
1287         if ((sensor_id & 0xfff0) == 0x7670) {
1288                 sd->sensor = SENSOR_OV767x;
1289                 gspca_dev->ctrl_dis = (1 << HUE) |
1290                                         (1 << GAIN) |
1291                                         (1 << AGC) |
1292                                         (1 << SHARPNESS);       /* auto */
1293                 sd->ctrls[SATURATION].min = 0,
1294                 sd->ctrls[SATURATION].max = 6,
1295                 sd->ctrls[SATURATION].def = 3,
1296                 sd->ctrls[BRIGHTNESS].min = -127;
1297                 sd->ctrls[BRIGHTNESS].max = 127;
1298                 sd->ctrls[BRIGHTNESS].def = 0;
1299                 sd->ctrls[CONTRAST].max = 0x80;
1300                 sd->ctrls[CONTRAST].def = 0x40;
1301                 sd->ctrls[EXPOSURE].min = 0x08;
1302                 sd->ctrls[EXPOSURE].max = 0x60;
1303                 sd->ctrls[EXPOSURE].def = 0x13;
1304                 sd->ctrls[SHARPNESS].max = 9;
1305                 sd->ctrls[SHARPNESS].def = 4;
1306                 sd->ctrls[HFLIP].def = 1;
1307                 gspca_dev->cam.cam_mode = ov767x_mode;
1308                 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1309         } else {
1310                 sd->sensor = SENSOR_OV772x;
1311                 gspca_dev->cam.bulk = 1;
1312                 gspca_dev->cam.bulk_size = 16384;
1313                 gspca_dev->cam.bulk_nurbs = 2;
1314                 gspca_dev->cam.mode_framerates = ov772x_framerates;
1315         }
1316
1317         /* initialize */
1318         reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1319                         bridge_init[sd->sensor].len);
1320         ov534_set_led(gspca_dev, 1);
1321         sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1322                         sensor_init[sd->sensor].len);
1323         if (sd->sensor == SENSOR_OV767x)
1324                 sd_start(gspca_dev);
1325         sd_stopN(gspca_dev);
1326 /*      set_frame_rate(gspca_dev);      */
1327
1328         return gspca_dev->usb_err;
1329 }
1330
1331 static int sd_start(struct gspca_dev *gspca_dev)
1332 {
1333         struct sd *sd = (struct sd *) gspca_dev;
1334         int mode;
1335         static const struct reg_array bridge_start[NSENSORS][2] = {
1336         [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1337                                         ARRAY_SIZE(bridge_start_qvga_767x)},
1338                         {bridge_start_vga_767x,
1339                                         ARRAY_SIZE(bridge_start_vga_767x)}},
1340         [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1341                                         ARRAY_SIZE(bridge_start_qvga_772x)},
1342                         {bridge_start_vga_772x,
1343                                         ARRAY_SIZE(bridge_start_vga_772x)}},
1344         };
1345         static const struct reg_array sensor_start[NSENSORS][2] = {
1346         [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1347                                         ARRAY_SIZE(sensor_start_qvga_767x)},
1348                         {sensor_start_vga_767x,
1349                                         ARRAY_SIZE(sensor_start_vga_767x)}},
1350         [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1351                                         ARRAY_SIZE(sensor_start_qvga_772x)},
1352                         {sensor_start_vga_772x,
1353                                         ARRAY_SIZE(sensor_start_vga_772x)}},
1354         };
1355
1356         /* (from ms-win trace) */
1357         if (sd->sensor == SENSOR_OV767x)
1358                 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1359                                         /* black sun enable ? */
1360
1361         mode = gspca_dev->curr_mode;    /* 0: 320x240, 1: 640x480 */
1362         reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1363                                 bridge_start[sd->sensor][mode].len);
1364         sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1365                                 sensor_start[sd->sensor][mode].len);
1366
1367         set_frame_rate(gspca_dev);
1368
1369         if (!(gspca_dev->ctrl_dis & (1 << HUE)))
1370                 sethue(gspca_dev);
1371         setsaturation(gspca_dev);
1372         if (!(gspca_dev->ctrl_dis & (1 << AGC)))
1373                 setagc(gspca_dev);
1374         setawb(gspca_dev);
1375         setaec(gspca_dev);
1376         if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
1377                 setgain(gspca_dev);
1378         setexposure(gspca_dev);
1379         setbrightness(gspca_dev);
1380         setcontrast(gspca_dev);
1381         if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
1382                 setsharpness(gspca_dev);
1383         sethvflip(gspca_dev);
1384         setlightfreq(gspca_dev);
1385
1386         ov534_set_led(gspca_dev, 1);
1387         ov534_reg_write(gspca_dev, 0xe0, 0x00);
1388         return gspca_dev->usb_err;
1389 }
1390
1391 static void sd_stopN(struct gspca_dev *gspca_dev)
1392 {
1393         ov534_reg_write(gspca_dev, 0xe0, 0x09);
1394         ov534_set_led(gspca_dev, 0);
1395 }
1396
1397 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1398 #define UVC_STREAM_EOH  (1 << 7)
1399 #define UVC_STREAM_ERR  (1 << 6)
1400 #define UVC_STREAM_STI  (1 << 5)
1401 #define UVC_STREAM_RES  (1 << 4)
1402 #define UVC_STREAM_SCR  (1 << 3)
1403 #define UVC_STREAM_PTS  (1 << 2)
1404 #define UVC_STREAM_EOF  (1 << 1)
1405 #define UVC_STREAM_FID  (1 << 0)
1406
1407 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1408                         u8 *data, int len)
1409 {
1410         struct sd *sd = (struct sd *) gspca_dev;
1411         __u32 this_pts;
1412         u16 this_fid;
1413         int remaining_len = len;
1414         int payload_len;
1415
1416         payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1417         do {
1418                 len = min(remaining_len, payload_len);
1419
1420                 /* Payloads are prefixed with a UVC-style header.  We
1421                    consider a frame to start when the FID toggles, or the PTS
1422                    changes.  A frame ends when EOF is set, and we've received
1423                    the correct number of bytes. */
1424
1425                 /* Verify UVC header.  Header length is always 12 */
1426                 if (data[0] != 12 || len < 12) {
1427                         PDEBUG(D_PACK, "bad header");
1428                         goto discard;
1429                 }
1430
1431                 /* Check errors */
1432                 if (data[1] & UVC_STREAM_ERR) {
1433                         PDEBUG(D_PACK, "payload error");
1434                         goto discard;
1435                 }
1436
1437                 /* Extract PTS and FID */
1438                 if (!(data[1] & UVC_STREAM_PTS)) {
1439                         PDEBUG(D_PACK, "PTS not present");
1440                         goto discard;
1441                 }
1442                 this_pts = (data[5] << 24) | (data[4] << 16)
1443                                                 | (data[3] << 8) | data[2];
1444                 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1445
1446                 /* If PTS or FID has changed, start a new frame. */
1447                 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1448                         if (gspca_dev->last_packet_type == INTER_PACKET)
1449                                 gspca_frame_add(gspca_dev, LAST_PACKET,
1450                                                 NULL, 0);
1451                         sd->last_pts = this_pts;
1452                         sd->last_fid = this_fid;
1453                         gspca_frame_add(gspca_dev, FIRST_PACKET,
1454                                         data + 12, len - 12);
1455                 /* If this packet is marked as EOF, end the frame */
1456                 } else if (data[1] & UVC_STREAM_EOF) {
1457                         sd->last_pts = 0;
1458                         if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
1459                          && gspca_dev->image_len + len - 12 !=
1460                                    gspca_dev->width * gspca_dev->height * 2) {
1461                                 PDEBUG(D_PACK, "wrong sized frame");
1462                                 goto discard;
1463                         }
1464                         gspca_frame_add(gspca_dev, LAST_PACKET,
1465                                         data + 12, len - 12);
1466                 } else {
1467
1468                         /* Add the data from this payload */
1469                         gspca_frame_add(gspca_dev, INTER_PACKET,
1470                                         data + 12, len - 12);
1471                 }
1472
1473                 /* Done this payload */
1474                 goto scan_next;
1475
1476 discard:
1477                 /* Discard data until a new frame starts. */
1478                 gspca_dev->last_packet_type = DISCARD_PACKET;
1479
1480 scan_next:
1481                 remaining_len -= len;
1482                 data += len;
1483         } while (remaining_len > 0);
1484 }
1485
1486 static int sd_querymenu(struct gspca_dev *gspca_dev,
1487                 struct v4l2_querymenu *menu)
1488 {
1489         switch (menu->id) {
1490         case V4L2_CID_POWER_LINE_FREQUENCY:
1491                 switch (menu->index) {
1492                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1493                         strcpy((char *) menu->name, "Disabled");
1494                         return 0;
1495                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1496                         strcpy((char *) menu->name, "50 Hz");
1497                         return 0;
1498                 }
1499                 break;
1500         }
1501
1502         return -EINVAL;
1503 }
1504
1505 /* get stream parameters (framerate) */
1506 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1507                              struct v4l2_streamparm *parm)
1508 {
1509         struct v4l2_captureparm *cp = &parm->parm.capture;
1510         struct v4l2_fract *tpf = &cp->timeperframe;
1511         struct sd *sd = (struct sd *) gspca_dev;
1512
1513         cp->capability |= V4L2_CAP_TIMEPERFRAME;
1514         tpf->numerator = 1;
1515         tpf->denominator = sd->frame_rate;
1516 }
1517
1518 /* set stream parameters (framerate) */
1519 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1520                              struct v4l2_streamparm *parm)
1521 {
1522         struct v4l2_captureparm *cp = &parm->parm.capture;
1523         struct v4l2_fract *tpf = &cp->timeperframe;
1524         struct sd *sd = (struct sd *) gspca_dev;
1525
1526         /* Set requested framerate */
1527         sd->frame_rate = tpf->denominator / tpf->numerator;
1528         if (gspca_dev->streaming)
1529                 set_frame_rate(gspca_dev);
1530
1531         /* Return the actual framerate */
1532         tpf->numerator = 1;
1533         tpf->denominator = sd->frame_rate;
1534 }
1535
1536 /* sub-driver description */
1537 static const struct sd_desc sd_desc = {
1538         .name     = MODULE_NAME,
1539         .ctrls    = sd_ctrls,
1540         .nctrls   = ARRAY_SIZE(sd_ctrls),
1541         .config   = sd_config,
1542         .init     = sd_init,
1543         .start    = sd_start,
1544         .stopN    = sd_stopN,
1545         .pkt_scan = sd_pkt_scan,
1546         .querymenu = sd_querymenu,
1547         .get_streamparm = sd_get_streamparm,
1548         .set_streamparm = sd_set_streamparm,
1549 };
1550
1551 /* -- module initialisation -- */
1552 static const struct usb_device_id device_table[] = {
1553         {USB_DEVICE(0x1415, 0x2000)},
1554         {USB_DEVICE(0x06f8, 0x3002)},
1555         {}
1556 };
1557
1558 MODULE_DEVICE_TABLE(usb, device_table);
1559
1560 /* -- device connect -- */
1561 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1562 {
1563         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1564                                 THIS_MODULE);
1565 }
1566
1567 static struct usb_driver sd_driver = {
1568         .name       = MODULE_NAME,
1569         .id_table   = device_table,
1570         .probe      = sd_probe,
1571         .disconnect = gspca_disconnect,
1572 #ifdef CONFIG_PM
1573         .suspend    = gspca_suspend,
1574         .resume     = gspca_resume,
1575 #endif
1576 };
1577
1578 module_usb_driver(sd_driver);