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