[media] gspca - ov534: Add Hue control
[linux-drm-fsl-dcu.git] / 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 int sd_setagc(struct gspca_dev *gspca_dev, __s32 val);
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 = sd_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                 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
855
856                 switch (data) {
857                 case 0x00:
858                         return 1;
859                 case 0x04:
860                         return 0;
861                 case 0x03:
862                         break;
863                 default:
864                         PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
865                                data, i + 1);
866                 }
867         }
868         return 0;
869 }
870
871 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
872 {
873         PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
874         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
875         ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
876         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
877
878         if (!sccb_check_status(gspca_dev)) {
879                 pr_err("sccb_reg_write failed\n");
880                 gspca_dev->usb_err = -EIO;
881         }
882 }
883
884 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
885 {
886         ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
887         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
888         if (!sccb_check_status(gspca_dev))
889                 pr_err("sccb_reg_read failed 1\n");
890
891         ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
892         if (!sccb_check_status(gspca_dev))
893                 pr_err("sccb_reg_read failed 2\n");
894
895         return ov534_reg_read(gspca_dev, OV534_REG_READ);
896 }
897
898 /* output a bridge sequence (reg - val) */
899 static void reg_w_array(struct gspca_dev *gspca_dev,
900                         const u8 (*data)[2], int len)
901 {
902         while (--len >= 0) {
903                 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
904                 data++;
905         }
906 }
907
908 /* output a sensor sequence (reg - val) */
909 static void sccb_w_array(struct gspca_dev *gspca_dev,
910                         const u8 (*data)[2], int len)
911 {
912         while (--len >= 0) {
913                 if ((*data)[0] != 0xff) {
914                         sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
915                 } else {
916                         sccb_reg_read(gspca_dev, (*data)[1]);
917                         sccb_reg_write(gspca_dev, 0xff, 0x00);
918                 }
919                 data++;
920         }
921 }
922
923 /* ov772x specific controls */
924 static void set_frame_rate(struct gspca_dev *gspca_dev)
925 {
926         struct sd *sd = (struct sd *) gspca_dev;
927         int i;
928         struct rate_s {
929                 u8 fps;
930                 u8 r11;
931                 u8 r0d;
932                 u8 re5;
933         };
934         const struct rate_s *r;
935         static const struct rate_s rate_0[] = { /* 640x480 */
936                 {60, 0x01, 0xc1, 0x04},
937                 {50, 0x01, 0x41, 0x02},
938                 {40, 0x02, 0xc1, 0x04},
939                 {30, 0x04, 0x81, 0x02},
940                 {15, 0x03, 0x41, 0x04},
941         };
942         static const struct rate_s rate_1[] = { /* 320x240 */
943                 {125, 0x02, 0x81, 0x02},
944                 {100, 0x02, 0xc1, 0x04},
945                 {75, 0x03, 0xc1, 0x04},
946                 {60, 0x04, 0xc1, 0x04},
947                 {50, 0x02, 0x41, 0x04},
948                 {40, 0x03, 0x41, 0x04},
949                 {30, 0x04, 0x41, 0x04},
950         };
951
952         if (sd->sensor != SENSOR_OV772x)
953                 return;
954         if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
955                 r = rate_0;
956                 i = ARRAY_SIZE(rate_0);
957         } else {
958                 r = rate_1;
959                 i = ARRAY_SIZE(rate_1);
960         }
961         while (--i > 0) {
962                 if (sd->frame_rate >= r->fps)
963                         break;
964                 r++;
965         }
966
967         sccb_reg_write(gspca_dev, 0x11, r->r11);
968         sccb_reg_write(gspca_dev, 0x0d, r->r0d);
969         ov534_reg_write(gspca_dev, 0xe5, r->re5);
970
971         PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
972 }
973
974 static void sethue(struct gspca_dev *gspca_dev)
975 {
976         struct sd *sd = (struct sd *) gspca_dev;
977         int val;
978
979         val = sd->ctrls[HUE].val;
980         if (sd->sensor == SENSOR_OV767x) {
981                 /* TBD */
982         } else {
983                 s16 huesin;
984                 s16 huecos;
985
986                 /* fixp_sin and fixp_cos accept only positive values, while
987                  * our val is between -90 and 90
988                  */
989                 val += 360;
990
991                 /* According to the datasheet the registers expect HUESIN and
992                  * HUECOS to be the result of the trigonometric functions,
993                  * scaled by 0x80.
994                  *
995                  * The 0x100 here represents the maximun absolute value
996                  * returned byt fixp_sin and fixp_cos, so the scaling will
997                  * consider the result like in the interval [-1.0, 1.0].
998                  */
999                 huesin = fixp_sin(val) * 0x80 / 0x100;
1000                 huecos = fixp_cos(val) * 0x80 / 0x100;
1001
1002                 if (huesin < 0) {
1003                         sccb_reg_write(gspca_dev, 0xab,
1004                                 sccb_reg_read(gspca_dev, 0xab) | 0x2);
1005                         huesin = -huesin;
1006                 } else {
1007                         sccb_reg_write(gspca_dev, 0xab,
1008                                 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
1009
1010                 }
1011                 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
1012                 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
1013         }
1014 }
1015
1016 static void setsaturation(struct gspca_dev *gspca_dev)
1017 {
1018         struct sd *sd = (struct sd *) gspca_dev;
1019         int val;
1020
1021         val = sd->ctrls[SATURATION].val;
1022         if (sd->sensor == SENSOR_OV767x) {
1023                 int i;
1024                 static u8 color_tb[][6] = {
1025                         {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1026                         {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1027                         {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1028                         {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1029                         {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1030                         {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1031                         {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1032                 };
1033
1034                 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
1035                         sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
1036         } else {
1037                 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
1038                 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
1039         }
1040 }
1041
1042 static void setbrightness(struct gspca_dev *gspca_dev)
1043 {
1044         struct sd *sd = (struct sd *) gspca_dev;
1045         int val;
1046
1047         val = sd->ctrls[BRIGHTNESS].val;
1048         if (sd->sensor == SENSOR_OV767x) {
1049                 if (val < 0)
1050                         val = 0x80 - val;
1051                 sccb_reg_write(gspca_dev, 0x55, val);   /* bright */
1052         } else {
1053                 sccb_reg_write(gspca_dev, 0x9b, val);
1054         }
1055 }
1056
1057 static void setcontrast(struct gspca_dev *gspca_dev)
1058 {
1059         struct sd *sd = (struct sd *) gspca_dev;
1060         u8 val;
1061
1062         val = sd->ctrls[CONTRAST].val;
1063         if (sd->sensor == SENSOR_OV767x)
1064                 sccb_reg_write(gspca_dev, 0x56, val);   /* contras */
1065         else
1066                 sccb_reg_write(gspca_dev, 0x9c, val);
1067 }
1068
1069 static void setgain(struct gspca_dev *gspca_dev)
1070 {
1071         struct sd *sd = (struct sd *) gspca_dev;
1072         u8 val;
1073
1074         if (sd->ctrls[AGC].val)
1075                 return;
1076
1077         val = sd->ctrls[GAIN].val;
1078         switch (val & 0x30) {
1079         case 0x00:
1080                 val &= 0x0f;
1081                 break;
1082         case 0x10:
1083                 val &= 0x0f;
1084                 val |= 0x30;
1085                 break;
1086         case 0x20:
1087                 val &= 0x0f;
1088                 val |= 0x70;
1089                 break;
1090         default:
1091 /*      case 0x30: */
1092                 val &= 0x0f;
1093                 val |= 0xf0;
1094                 break;
1095         }
1096         sccb_reg_write(gspca_dev, 0x00, val);
1097 }
1098
1099 static void setexposure(struct gspca_dev *gspca_dev)
1100 {
1101         struct sd *sd = (struct sd *) gspca_dev;
1102         u8 val;
1103
1104         if (sd->ctrls[AEC].val)
1105                 return;
1106
1107         val = sd->ctrls[EXPOSURE].val;
1108         if (sd->sensor == SENSOR_OV767x) {
1109
1110                 /* set only aec[9:2] */
1111                 sccb_reg_write(gspca_dev, 0x10, val);   /* aech */
1112         } else {
1113
1114                 /* 'val' is one byte and represents half of the exposure value
1115                  * we are going to set into registers, a two bytes value:
1116                  *
1117                  *    MSB: ((u16) val << 1) >> 8   == val >> 7
1118                  *    LSB: ((u16) val << 1) & 0xff == val << 1
1119                  */
1120                 sccb_reg_write(gspca_dev, 0x08, val >> 7);
1121                 sccb_reg_write(gspca_dev, 0x10, val << 1);
1122         }
1123 }
1124
1125 static void setagc(struct gspca_dev *gspca_dev)
1126 {
1127         struct sd *sd = (struct sd *) gspca_dev;
1128
1129         if (sd->ctrls[AGC].val) {
1130                 sccb_reg_write(gspca_dev, 0x13,
1131                                 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1132                 sccb_reg_write(gspca_dev, 0x64,
1133                                 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1134         } else {
1135                 sccb_reg_write(gspca_dev, 0x13,
1136                                 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1137                 sccb_reg_write(gspca_dev, 0x64,
1138                                 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1139
1140                 setgain(gspca_dev);
1141         }
1142 }
1143
1144 static void setawb(struct gspca_dev *gspca_dev)
1145 {
1146         struct sd *sd = (struct sd *) gspca_dev;
1147
1148         if (sd->ctrls[AWB].val) {
1149                 sccb_reg_write(gspca_dev, 0x13,
1150                                 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1151                 if (sd->sensor == SENSOR_OV772x)
1152                         sccb_reg_write(gspca_dev, 0x63,
1153                                 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1154         } else {
1155                 sccb_reg_write(gspca_dev, 0x13,
1156                                 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1157                 if (sd->sensor == SENSOR_OV772x)
1158                         sccb_reg_write(gspca_dev, 0x63,
1159                                 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1160         }
1161 }
1162
1163 static void setaec(struct gspca_dev *gspca_dev)
1164 {
1165         struct sd *sd = (struct sd *) gspca_dev;
1166         u8 data;
1167
1168         data = sd->sensor == SENSOR_OV767x ?
1169                         0x05 :          /* agc + aec */
1170                         0x01;           /* agc */
1171         if (sd->ctrls[AEC].val)
1172                 sccb_reg_write(gspca_dev, 0x13,
1173                                 sccb_reg_read(gspca_dev, 0x13) | data);
1174         else {
1175                 sccb_reg_write(gspca_dev, 0x13,
1176                                 sccb_reg_read(gspca_dev, 0x13) & ~data);
1177                 if (sd->sensor == SENSOR_OV767x)
1178                         sd->ctrls[EXPOSURE].val =
1179                                 sccb_reg_read(gspca_dev, 10);   /* aech */
1180                 else
1181                         setexposure(gspca_dev);
1182         }
1183 }
1184
1185 static void setsharpness(struct gspca_dev *gspca_dev)
1186 {
1187         struct sd *sd = (struct sd *) gspca_dev;
1188         u8 val;
1189
1190         val = sd->ctrls[SHARPNESS].val;
1191         sccb_reg_write(gspca_dev, 0x91, val);   /* Auto de-noise threshold */
1192         sccb_reg_write(gspca_dev, 0x8e, val);   /* De-noise threshold */
1193 }
1194
1195 static void sethvflip(struct gspca_dev *gspca_dev)
1196 {
1197         struct sd *sd = (struct sd *) gspca_dev;
1198         u8 val;
1199
1200         if (sd->sensor == SENSOR_OV767x) {
1201                 val = sccb_reg_read(gspca_dev, 0x1e);   /* mvfp */
1202                 val &= ~0x30;
1203                 if (sd->ctrls[HFLIP].val)
1204                         val |= 0x20;
1205                 if (sd->ctrls[VFLIP].val)
1206                         val |= 0x10;
1207                 sccb_reg_write(gspca_dev, 0x1e, val);
1208         } else {
1209                 val = sccb_reg_read(gspca_dev, 0x0c);
1210                 val &= ~0xc0;
1211                 if (sd->ctrls[HFLIP].val == 0)
1212                         val |= 0x40;
1213                 if (sd->ctrls[VFLIP].val == 0)
1214                         val |= 0x80;
1215                 sccb_reg_write(gspca_dev, 0x0c, val);
1216         }
1217 }
1218
1219 static void setlightfreq(struct gspca_dev *gspca_dev)
1220 {
1221         struct sd *sd = (struct sd *) gspca_dev;
1222         u8 val;
1223
1224         val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
1225         if (sd->sensor == SENSOR_OV767x) {
1226                 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1227                 if (val)
1228                         val = 0x9d;     /* insert dummy to 25fps for 50Hz */
1229         }
1230         sccb_reg_write(gspca_dev, 0x2b, val);
1231 }
1232
1233
1234 /* this function is called at probe time */
1235 static int sd_config(struct gspca_dev *gspca_dev,
1236                      const struct usb_device_id *id)
1237 {
1238         struct sd *sd = (struct sd *) gspca_dev;
1239         struct cam *cam;
1240
1241         cam = &gspca_dev->cam;
1242
1243         cam->ctrls = sd->ctrls;
1244
1245         /* the auto white balance control works only when auto gain is set */
1246         if (sd_ctrls[AGC].qctrl.default_value == 0)
1247                 gspca_dev->ctrl_inac |= (1 << AWB);
1248
1249         cam->cam_mode = ov772x_mode;
1250         cam->nmodes = ARRAY_SIZE(ov772x_mode);
1251
1252         sd->frame_rate = 30;
1253
1254         return 0;
1255 }
1256
1257 /* this function is called at probe and resume time */
1258 static int sd_init(struct gspca_dev *gspca_dev)
1259 {
1260         struct sd *sd = (struct sd *) gspca_dev;
1261         u16 sensor_id;
1262         static const struct reg_array bridge_init[NSENSORS] = {
1263         [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1264         [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1265         };
1266         static const struct reg_array sensor_init[NSENSORS] = {
1267         [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1268         [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1269         };
1270
1271         /* reset bridge */
1272         ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1273         ov534_reg_write(gspca_dev, 0xe0, 0x08);
1274         msleep(100);
1275
1276         /* initialize the sensor address */
1277         ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1278
1279         /* reset sensor */
1280         sccb_reg_write(gspca_dev, 0x12, 0x80);
1281         msleep(10);
1282
1283         /* probe the sensor */
1284         sccb_reg_read(gspca_dev, 0x0a);
1285         sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1286         sccb_reg_read(gspca_dev, 0x0b);
1287         sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1288         PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1289
1290         if ((sensor_id & 0xfff0) == 0x7670) {
1291                 sd->sensor = SENSOR_OV767x;
1292                 gspca_dev->ctrl_dis = (1 << HUE) |
1293                                         (1 << GAIN) |
1294                                         (1 << AGC) |
1295                                         (1 << SHARPNESS);       /* auto */
1296                 sd->ctrls[SATURATION].min = 0,
1297                 sd->ctrls[SATURATION].max = 6,
1298                 sd->ctrls[SATURATION].def = 3,
1299                 sd->ctrls[BRIGHTNESS].min = -127;
1300                 sd->ctrls[BRIGHTNESS].max = 127;
1301                 sd->ctrls[BRIGHTNESS].def = 0;
1302                 sd->ctrls[CONTRAST].max = 0x80;
1303                 sd->ctrls[CONTRAST].def = 0x40;
1304                 sd->ctrls[EXPOSURE].min = 0x08;
1305                 sd->ctrls[EXPOSURE].max = 0x60;
1306                 sd->ctrls[EXPOSURE].def = 0x13;
1307                 sd->ctrls[SHARPNESS].max = 9;
1308                 sd->ctrls[SHARPNESS].def = 4;
1309                 sd->ctrls[HFLIP].def = 1;
1310                 gspca_dev->cam.cam_mode = ov767x_mode;
1311                 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1312         } else {
1313                 sd->sensor = SENSOR_OV772x;
1314                 gspca_dev->cam.bulk = 1;
1315                 gspca_dev->cam.bulk_size = 16384;
1316                 gspca_dev->cam.bulk_nurbs = 2;
1317                 gspca_dev->cam.mode_framerates = ov772x_framerates;
1318         }
1319
1320         /* initialize */
1321         reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1322                         bridge_init[sd->sensor].len);
1323         ov534_set_led(gspca_dev, 1);
1324         sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1325                         sensor_init[sd->sensor].len);
1326         if (sd->sensor == SENSOR_OV767x)
1327                 sd_start(gspca_dev);
1328         sd_stopN(gspca_dev);
1329 /*      set_frame_rate(gspca_dev);      */
1330
1331         return gspca_dev->usb_err;
1332 }
1333
1334 static int sd_start(struct gspca_dev *gspca_dev)
1335 {
1336         struct sd *sd = (struct sd *) gspca_dev;
1337         int mode;
1338         static const struct reg_array bridge_start[NSENSORS][2] = {
1339         [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1340                                         ARRAY_SIZE(bridge_start_qvga_767x)},
1341                         {bridge_start_vga_767x,
1342                                         ARRAY_SIZE(bridge_start_vga_767x)}},
1343         [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1344                                         ARRAY_SIZE(bridge_start_qvga_772x)},
1345                         {bridge_start_vga_772x,
1346                                         ARRAY_SIZE(bridge_start_vga_772x)}},
1347         };
1348         static const struct reg_array sensor_start[NSENSORS][2] = {
1349         [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1350                                         ARRAY_SIZE(sensor_start_qvga_767x)},
1351                         {sensor_start_vga_767x,
1352                                         ARRAY_SIZE(sensor_start_vga_767x)}},
1353         [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1354                                         ARRAY_SIZE(sensor_start_qvga_772x)},
1355                         {sensor_start_vga_772x,
1356                                         ARRAY_SIZE(sensor_start_vga_772x)}},
1357         };
1358
1359         /* (from ms-win trace) */
1360         if (sd->sensor == SENSOR_OV767x)
1361                 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1362                                         /* black sun enable ? */
1363
1364         mode = gspca_dev->curr_mode;    /* 0: 320x240, 1: 640x480 */
1365         reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1366                                 bridge_start[sd->sensor][mode].len);
1367         sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1368                                 sensor_start[sd->sensor][mode].len);
1369
1370         set_frame_rate(gspca_dev);
1371
1372         if (!(gspca_dev->ctrl_dis & (1 << HUE)))
1373                 sethue(gspca_dev);
1374         setsaturation(gspca_dev);
1375         if (!(gspca_dev->ctrl_dis & (1 << AGC)))
1376                 setagc(gspca_dev);
1377         setawb(gspca_dev);
1378         setaec(gspca_dev);
1379         if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
1380                 setgain(gspca_dev);
1381         setexposure(gspca_dev);
1382         setbrightness(gspca_dev);
1383         setcontrast(gspca_dev);
1384         if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
1385                 setsharpness(gspca_dev);
1386         sethvflip(gspca_dev);
1387         setlightfreq(gspca_dev);
1388
1389         ov534_set_led(gspca_dev, 1);
1390         ov534_reg_write(gspca_dev, 0xe0, 0x00);
1391         return gspca_dev->usb_err;
1392 }
1393
1394 static void sd_stopN(struct gspca_dev *gspca_dev)
1395 {
1396         ov534_reg_write(gspca_dev, 0xe0, 0x09);
1397         ov534_set_led(gspca_dev, 0);
1398 }
1399
1400 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1401 #define UVC_STREAM_EOH  (1 << 7)
1402 #define UVC_STREAM_ERR  (1 << 6)
1403 #define UVC_STREAM_STI  (1 << 5)
1404 #define UVC_STREAM_RES  (1 << 4)
1405 #define UVC_STREAM_SCR  (1 << 3)
1406 #define UVC_STREAM_PTS  (1 << 2)
1407 #define UVC_STREAM_EOF  (1 << 1)
1408 #define UVC_STREAM_FID  (1 << 0)
1409
1410 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1411                         u8 *data, int len)
1412 {
1413         struct sd *sd = (struct sd *) gspca_dev;
1414         __u32 this_pts;
1415         u16 this_fid;
1416         int remaining_len = len;
1417         int payload_len;
1418
1419         payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1420         do {
1421                 len = min(remaining_len, payload_len);
1422
1423                 /* Payloads are prefixed with a UVC-style header.  We
1424                    consider a frame to start when the FID toggles, or the PTS
1425                    changes.  A frame ends when EOF is set, and we've received
1426                    the correct number of bytes. */
1427
1428                 /* Verify UVC header.  Header length is always 12 */
1429                 if (data[0] != 12 || len < 12) {
1430                         PDEBUG(D_PACK, "bad header");
1431                         goto discard;
1432                 }
1433
1434                 /* Check errors */
1435                 if (data[1] & UVC_STREAM_ERR) {
1436                         PDEBUG(D_PACK, "payload error");
1437                         goto discard;
1438                 }
1439
1440                 /* Extract PTS and FID */
1441                 if (!(data[1] & UVC_STREAM_PTS)) {
1442                         PDEBUG(D_PACK, "PTS not present");
1443                         goto discard;
1444                 }
1445                 this_pts = (data[5] << 24) | (data[4] << 16)
1446                                                 | (data[3] << 8) | data[2];
1447                 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1448
1449                 /* If PTS or FID has changed, start a new frame. */
1450                 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1451                         if (gspca_dev->last_packet_type == INTER_PACKET)
1452                                 gspca_frame_add(gspca_dev, LAST_PACKET,
1453                                                 NULL, 0);
1454                         sd->last_pts = this_pts;
1455                         sd->last_fid = this_fid;
1456                         gspca_frame_add(gspca_dev, FIRST_PACKET,
1457                                         data + 12, len - 12);
1458                 /* If this packet is marked as EOF, end the frame */
1459                 } else if (data[1] & UVC_STREAM_EOF) {
1460                         sd->last_pts = 0;
1461                         if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
1462                          && gspca_dev->image_len + len - 12 !=
1463                                    gspca_dev->width * gspca_dev->height * 2) {
1464                                 PDEBUG(D_PACK, "wrong sized frame");
1465                                 goto discard;
1466                         }
1467                         gspca_frame_add(gspca_dev, LAST_PACKET,
1468                                         data + 12, len - 12);
1469                 } else {
1470
1471                         /* Add the data from this payload */
1472                         gspca_frame_add(gspca_dev, INTER_PACKET,
1473                                         data + 12, len - 12);
1474                 }
1475
1476                 /* Done this payload */
1477                 goto scan_next;
1478
1479 discard:
1480                 /* Discard data until a new frame starts. */
1481                 gspca_dev->last_packet_type = DISCARD_PACKET;
1482
1483 scan_next:
1484                 remaining_len -= len;
1485                 data += len;
1486         } while (remaining_len > 0);
1487 }
1488
1489 static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val)
1490 {
1491         struct sd *sd = (struct sd *) gspca_dev;
1492
1493         sd->ctrls[AGC].val = val;
1494
1495         /* the auto white balance control works only
1496          * when auto gain is set */
1497         if (val) {
1498                 gspca_dev->ctrl_inac &= ~(1 << AWB);
1499         } else {
1500                 gspca_dev->ctrl_inac |= (1 << AWB);
1501                 if (sd->ctrls[AWB].val) {
1502                         sd->ctrls[AWB].val = 0;
1503                         if (gspca_dev->streaming)
1504                                 setawb(gspca_dev);
1505                 }
1506         }
1507         if (gspca_dev->streaming)
1508                 setagc(gspca_dev);
1509         return gspca_dev->usb_err;
1510 }
1511
1512 static int sd_querymenu(struct gspca_dev *gspca_dev,
1513                 struct v4l2_querymenu *menu)
1514 {
1515         switch (menu->id) {
1516         case V4L2_CID_POWER_LINE_FREQUENCY:
1517                 switch (menu->index) {
1518                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1519                         strcpy((char *) menu->name, "Disabled");
1520                         return 0;
1521                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1522                         strcpy((char *) menu->name, "50 Hz");
1523                         return 0;
1524                 }
1525                 break;
1526         }
1527
1528         return -EINVAL;
1529 }
1530
1531 /* get stream parameters (framerate) */
1532 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1533                              struct v4l2_streamparm *parm)
1534 {
1535         struct v4l2_captureparm *cp = &parm->parm.capture;
1536         struct v4l2_fract *tpf = &cp->timeperframe;
1537         struct sd *sd = (struct sd *) gspca_dev;
1538
1539         cp->capability |= V4L2_CAP_TIMEPERFRAME;
1540         tpf->numerator = 1;
1541         tpf->denominator = sd->frame_rate;
1542 }
1543
1544 /* set stream parameters (framerate) */
1545 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1546                              struct v4l2_streamparm *parm)
1547 {
1548         struct v4l2_captureparm *cp = &parm->parm.capture;
1549         struct v4l2_fract *tpf = &cp->timeperframe;
1550         struct sd *sd = (struct sd *) gspca_dev;
1551
1552         /* Set requested framerate */
1553         sd->frame_rate = tpf->denominator / tpf->numerator;
1554         if (gspca_dev->streaming)
1555                 set_frame_rate(gspca_dev);
1556
1557         /* Return the actual framerate */
1558         tpf->numerator = 1;
1559         tpf->denominator = sd->frame_rate;
1560 }
1561
1562 /* sub-driver description */
1563 static const struct sd_desc sd_desc = {
1564         .name     = MODULE_NAME,
1565         .ctrls    = sd_ctrls,
1566         .nctrls   = ARRAY_SIZE(sd_ctrls),
1567         .config   = sd_config,
1568         .init     = sd_init,
1569         .start    = sd_start,
1570         .stopN    = sd_stopN,
1571         .pkt_scan = sd_pkt_scan,
1572         .querymenu = sd_querymenu,
1573         .get_streamparm = sd_get_streamparm,
1574         .set_streamparm = sd_set_streamparm,
1575 };
1576
1577 /* -- module initialisation -- */
1578 static const struct usb_device_id device_table[] = {
1579         {USB_DEVICE(0x1415, 0x2000)},
1580         {USB_DEVICE(0x06f8, 0x3002)},
1581         {}
1582 };
1583
1584 MODULE_DEVICE_TABLE(usb, device_table);
1585
1586 /* -- device connect -- */
1587 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1588 {
1589         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1590                                 THIS_MODULE);
1591 }
1592
1593 static struct usb_driver sd_driver = {
1594         .name       = MODULE_NAME,
1595         .id_table   = device_table,
1596         .probe      = sd_probe,
1597         .disconnect = gspca_disconnect,
1598 #ifdef CONFIG_PM
1599         .suspend    = gspca_suspend,
1600         .resume     = gspca_resume,
1601 #endif
1602 };
1603
1604 module_usb_driver(sd_driver);