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