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