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