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