]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/usb/gspca/ov534.c
mac80211: fix throughput LED trigger
[mirror_ubuntu-artful-kernel.git] / drivers / media / usb / gspca / ov534.c
1 /*
2 * ov534-ov7xxx gspca driver
3 *
4 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
5 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
6 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
7 *
8 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
9 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
10 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
11 *
12 * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
13 * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
14 * added by Max Thrun <bear24rw@gmail.com>
15 * PS3 Eye camera - FPS range extended by Joseph Howse
16 * <josephhowse@nummist.com> http://nummist.com
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 */
32
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
35 #define MODULE_NAME "ov534"
36
37 #include "gspca.h"
38
39 #include <linux/fixp-arith.h>
40 #include <media/v4l2-ctrls.h>
41
42 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
43 #define OV534_REG_SUBADDR 0xf2
44 #define OV534_REG_WRITE 0xf3
45 #define OV534_REG_READ 0xf4
46 #define OV534_REG_OPERATION 0xf5
47 #define OV534_REG_STATUS 0xf6
48
49 #define OV534_OP_WRITE_3 0x37
50 #define OV534_OP_WRITE_2 0x33
51 #define OV534_OP_READ_2 0xf9
52
53 #define CTRL_TIMEOUT 500
54
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 /* fixp_sin and fixp_cos accept only positive values, while
820 * our val is between -90 and 90
821 */
822 val += 360;
823
824 /* According to the datasheet the registers expect HUESIN and
825 * HUECOS to be the result of the trigonometric functions,
826 * scaled by 0x80.
827 *
828 * The 0x100 here represents the maximun absolute value
829 * returned byt fixp_sin and fixp_cos, so the scaling will
830 * consider the result like in the interval [-1.0, 1.0].
831 */
832 huesin = fixp_sin(val) * 0x80 / 0x100;
833 huecos = fixp_cos(val) * 0x80 / 0x100;
834
835 if (huesin < 0) {
836 sccb_reg_write(gspca_dev, 0xab,
837 sccb_reg_read(gspca_dev, 0xab) | 0x2);
838 huesin = -huesin;
839 } else {
840 sccb_reg_write(gspca_dev, 0xab,
841 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
842
843 }
844 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
845 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
846 }
847 }
848
849 static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
850 {
851 struct sd *sd = (struct sd *) gspca_dev;
852
853 if (sd->sensor == SENSOR_OV767x) {
854 int i;
855 static u8 color_tb[][6] = {
856 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
857 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
858 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
859 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
860 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
861 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
862 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
863 };
864
865 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
866 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
867 } else {
868 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
869 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
870 }
871 }
872
873 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
874 {
875 struct sd *sd = (struct sd *) gspca_dev;
876
877 if (sd->sensor == SENSOR_OV767x) {
878 if (val < 0)
879 val = 0x80 - val;
880 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
881 } else {
882 sccb_reg_write(gspca_dev, 0x9b, val);
883 }
884 }
885
886 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
887 {
888 struct sd *sd = (struct sd *) gspca_dev;
889
890 if (sd->sensor == SENSOR_OV767x)
891 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
892 else
893 sccb_reg_write(gspca_dev, 0x9c, val);
894 }
895
896 static void setgain(struct gspca_dev *gspca_dev, s32 val)
897 {
898 switch (val & 0x30) {
899 case 0x00:
900 val &= 0x0f;
901 break;
902 case 0x10:
903 val &= 0x0f;
904 val |= 0x30;
905 break;
906 case 0x20:
907 val &= 0x0f;
908 val |= 0x70;
909 break;
910 default:
911 /* case 0x30: */
912 val &= 0x0f;
913 val |= 0xf0;
914 break;
915 }
916 sccb_reg_write(gspca_dev, 0x00, val);
917 }
918
919 static s32 getgain(struct gspca_dev *gspca_dev)
920 {
921 return sccb_reg_read(gspca_dev, 0x00);
922 }
923
924 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
925 {
926 struct sd *sd = (struct sd *) gspca_dev;
927
928 if (sd->sensor == SENSOR_OV767x) {
929
930 /* set only aec[9:2] */
931 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
932 } else {
933
934 /* 'val' is one byte and represents half of the exposure value
935 * we are going to set into registers, a two bytes value:
936 *
937 * MSB: ((u16) val << 1) >> 8 == val >> 7
938 * LSB: ((u16) val << 1) & 0xff == val << 1
939 */
940 sccb_reg_write(gspca_dev, 0x08, val >> 7);
941 sccb_reg_write(gspca_dev, 0x10, val << 1);
942 }
943 }
944
945 static s32 getexposure(struct gspca_dev *gspca_dev)
946 {
947 struct sd *sd = (struct sd *) gspca_dev;
948
949 if (sd->sensor == SENSOR_OV767x) {
950 /* get only aec[9:2] */
951 return sccb_reg_read(gspca_dev, 0x10); /* aech */
952 } else {
953 u8 hi = sccb_reg_read(gspca_dev, 0x08);
954 u8 lo = sccb_reg_read(gspca_dev, 0x10);
955 return (hi << 8 | lo) >> 1;
956 }
957 }
958
959 static void setagc(struct gspca_dev *gspca_dev, s32 val)
960 {
961 if (val) {
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 } else {
967 sccb_reg_write(gspca_dev, 0x13,
968 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
969 sccb_reg_write(gspca_dev, 0x64,
970 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
971 }
972 }
973
974 static void setawb(struct gspca_dev *gspca_dev, s32 val)
975 {
976 struct sd *sd = (struct sd *) gspca_dev;
977
978 if (val) {
979 sccb_reg_write(gspca_dev, 0x13,
980 sccb_reg_read(gspca_dev, 0x13) | 0x02);
981 if (sd->sensor == SENSOR_OV772x)
982 sccb_reg_write(gspca_dev, 0x63,
983 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
984 } else {
985 sccb_reg_write(gspca_dev, 0x13,
986 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
987 if (sd->sensor == SENSOR_OV772x)
988 sccb_reg_write(gspca_dev, 0x63,
989 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
990 }
991 }
992
993 static void setaec(struct gspca_dev *gspca_dev, s32 val)
994 {
995 struct sd *sd = (struct sd *) gspca_dev;
996 u8 data;
997
998 data = sd->sensor == SENSOR_OV767x ?
999 0x05 : /* agc + aec */
1000 0x01; /* agc */
1001 switch (val) {
1002 case V4L2_EXPOSURE_AUTO:
1003 sccb_reg_write(gspca_dev, 0x13,
1004 sccb_reg_read(gspca_dev, 0x13) | data);
1005 break;
1006 case V4L2_EXPOSURE_MANUAL:
1007 sccb_reg_write(gspca_dev, 0x13,
1008 sccb_reg_read(gspca_dev, 0x13) & ~data);
1009 break;
1010 }
1011 }
1012
1013 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1014 {
1015 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1016 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1017 }
1018
1019 static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
1020 {
1021 struct sd *sd = (struct sd *) gspca_dev;
1022 u8 val;
1023
1024 if (sd->sensor == SENSOR_OV767x) {
1025 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1026 val &= ~0x30;
1027 if (hflip)
1028 val |= 0x20;
1029 if (vflip)
1030 val |= 0x10;
1031 sccb_reg_write(gspca_dev, 0x1e, val);
1032 } else {
1033 val = sccb_reg_read(gspca_dev, 0x0c);
1034 val &= ~0xc0;
1035 if (hflip == 0)
1036 val |= 0x40;
1037 if (vflip == 0)
1038 val |= 0x80;
1039 sccb_reg_write(gspca_dev, 0x0c, val);
1040 }
1041 }
1042
1043 static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
1044 {
1045 struct sd *sd = (struct sd *) gspca_dev;
1046
1047 val = val ? 0x9e : 0x00;
1048 if (sd->sensor == SENSOR_OV767x) {
1049 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1050 if (val)
1051 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1052 }
1053 sccb_reg_write(gspca_dev, 0x2b, val);
1054 }
1055
1056
1057 /* this function is called at probe time */
1058 static int sd_config(struct gspca_dev *gspca_dev,
1059 const struct usb_device_id *id)
1060 {
1061 struct sd *sd = (struct sd *) gspca_dev;
1062 struct cam *cam;
1063
1064 cam = &gspca_dev->cam;
1065
1066 cam->cam_mode = ov772x_mode;
1067 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1068
1069 sd->frame_rate = 30;
1070
1071 return 0;
1072 }
1073
1074 static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1075 {
1076 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1077 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1078
1079 switch (ctrl->id) {
1080 case V4L2_CID_AUTOGAIN:
1081 gspca_dev->usb_err = 0;
1082 if (ctrl->val && sd->gain && gspca_dev->streaming)
1083 sd->gain->val = getgain(gspca_dev);
1084 return gspca_dev->usb_err;
1085
1086 case V4L2_CID_EXPOSURE_AUTO:
1087 gspca_dev->usb_err = 0;
1088 if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1089 gspca_dev->streaming)
1090 sd->exposure->val = getexposure(gspca_dev);
1091 return gspca_dev->usb_err;
1092 }
1093 return -EINVAL;
1094 }
1095
1096 static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1097 {
1098 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1099 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1100
1101 gspca_dev->usb_err = 0;
1102 if (!gspca_dev->streaming)
1103 return 0;
1104
1105 switch (ctrl->id) {
1106 case V4L2_CID_HUE:
1107 sethue(gspca_dev, ctrl->val);
1108 break;
1109 case V4L2_CID_SATURATION:
1110 setsaturation(gspca_dev, ctrl->val);
1111 break;
1112 case V4L2_CID_BRIGHTNESS:
1113 setbrightness(gspca_dev, ctrl->val);
1114 break;
1115 case V4L2_CID_CONTRAST:
1116 setcontrast(gspca_dev, ctrl->val);
1117 break;
1118 case V4L2_CID_AUTOGAIN:
1119 /* case V4L2_CID_GAIN: */
1120 setagc(gspca_dev, ctrl->val);
1121 if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1122 setgain(gspca_dev, sd->gain->val);
1123 break;
1124 case V4L2_CID_AUTO_WHITE_BALANCE:
1125 setawb(gspca_dev, ctrl->val);
1126 break;
1127 case V4L2_CID_EXPOSURE_AUTO:
1128 /* case V4L2_CID_EXPOSURE: */
1129 setaec(gspca_dev, ctrl->val);
1130 if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1131 sd->exposure)
1132 setexposure(gspca_dev, sd->exposure->val);
1133 break;
1134 case V4L2_CID_SHARPNESS:
1135 setsharpness(gspca_dev, ctrl->val);
1136 break;
1137 case V4L2_CID_HFLIP:
1138 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1139 break;
1140 case V4L2_CID_VFLIP:
1141 sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1142 break;
1143 case V4L2_CID_POWER_LINE_FREQUENCY:
1144 setlightfreq(gspca_dev, ctrl->val);
1145 break;
1146 }
1147 return gspca_dev->usb_err;
1148 }
1149
1150 static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1151 .g_volatile_ctrl = ov534_g_volatile_ctrl,
1152 .s_ctrl = ov534_s_ctrl,
1153 };
1154
1155 static int sd_init_controls(struct gspca_dev *gspca_dev)
1156 {
1157 struct sd *sd = (struct sd *) gspca_dev;
1158 struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1159 /* parameters with different values between the supported sensors */
1160 int saturation_min;
1161 int saturation_max;
1162 int saturation_def;
1163 int brightness_min;
1164 int brightness_max;
1165 int brightness_def;
1166 int contrast_max;
1167 int contrast_def;
1168 int exposure_min;
1169 int exposure_max;
1170 int exposure_def;
1171 int hflip_def;
1172
1173 if (sd->sensor == SENSOR_OV767x) {
1174 saturation_min = 0,
1175 saturation_max = 6,
1176 saturation_def = 3,
1177 brightness_min = -127;
1178 brightness_max = 127;
1179 brightness_def = 0;
1180 contrast_max = 0x80;
1181 contrast_def = 0x40;
1182 exposure_min = 0x08;
1183 exposure_max = 0x60;
1184 exposure_def = 0x13;
1185 hflip_def = 1;
1186 } else {
1187 saturation_min = 0,
1188 saturation_max = 255,
1189 saturation_def = 64,
1190 brightness_min = 0;
1191 brightness_max = 255;
1192 brightness_def = 0;
1193 contrast_max = 255;
1194 contrast_def = 32;
1195 exposure_min = 0;
1196 exposure_max = 255;
1197 exposure_def = 120;
1198 hflip_def = 0;
1199 }
1200
1201 gspca_dev->vdev.ctrl_handler = hdl;
1202
1203 v4l2_ctrl_handler_init(hdl, 13);
1204
1205 if (sd->sensor == SENSOR_OV772x)
1206 sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1207 V4L2_CID_HUE, -90, 90, 1, 0);
1208
1209 sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1210 V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1211 saturation_def);
1212 sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1213 V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1214 brightness_def);
1215 sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1216 V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1217
1218 if (sd->sensor == SENSOR_OV772x) {
1219 sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1220 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1221 sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1222 V4L2_CID_GAIN, 0, 63, 1, 20);
1223 }
1224
1225 sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1226 V4L2_CID_EXPOSURE_AUTO,
1227 V4L2_EXPOSURE_MANUAL, 0,
1228 V4L2_EXPOSURE_AUTO);
1229 sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1230 V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1231 exposure_def);
1232
1233 sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1234 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1235
1236 if (sd->sensor == SENSOR_OV772x)
1237 sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1238 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1239
1240 sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1241 V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1242 sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1243 V4L2_CID_VFLIP, 0, 1, 1, 0);
1244 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1245 V4L2_CID_POWER_LINE_FREQUENCY,
1246 V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1247 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1248
1249 if (hdl->error) {
1250 pr_err("Could not initialize controls\n");
1251 return hdl->error;
1252 }
1253
1254 if (sd->sensor == SENSOR_OV772x)
1255 v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1256
1257 v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1258 true);
1259
1260 return 0;
1261 }
1262
1263 /* this function is called at probe and resume time */
1264 static int sd_init(struct gspca_dev *gspca_dev)
1265 {
1266 struct sd *sd = (struct sd *) gspca_dev;
1267 u16 sensor_id;
1268 static const struct reg_array bridge_init[NSENSORS] = {
1269 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1270 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1271 };
1272 static const struct reg_array sensor_init[NSENSORS] = {
1273 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1274 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1275 };
1276
1277 /* reset bridge */
1278 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1279 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1280 msleep(100);
1281
1282 /* initialize the sensor address */
1283 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1284
1285 /* reset sensor */
1286 sccb_reg_write(gspca_dev, 0x12, 0x80);
1287 msleep(10);
1288
1289 /* probe the sensor */
1290 sccb_reg_read(gspca_dev, 0x0a);
1291 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1292 sccb_reg_read(gspca_dev, 0x0b);
1293 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1294 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1295
1296 if ((sensor_id & 0xfff0) == 0x7670) {
1297 sd->sensor = SENSOR_OV767x;
1298 gspca_dev->cam.cam_mode = ov767x_mode;
1299 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1300 } else {
1301 sd->sensor = SENSOR_OV772x;
1302 gspca_dev->cam.bulk = 1;
1303 gspca_dev->cam.bulk_size = 16384;
1304 gspca_dev->cam.bulk_nurbs = 2;
1305 gspca_dev->cam.mode_framerates = ov772x_framerates;
1306 }
1307
1308 /* initialize */
1309 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1310 bridge_init[sd->sensor].len);
1311 ov534_set_led(gspca_dev, 1);
1312 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1313 sensor_init[sd->sensor].len);
1314
1315 sd_stopN(gspca_dev);
1316 /* set_frame_rate(gspca_dev); */
1317
1318 return gspca_dev->usb_err;
1319 }
1320
1321 static int sd_start(struct gspca_dev *gspca_dev)
1322 {
1323 struct sd *sd = (struct sd *) gspca_dev;
1324 int mode;
1325 static const struct reg_array bridge_start[NSENSORS][2] = {
1326 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1327 ARRAY_SIZE(bridge_start_qvga_767x)},
1328 {bridge_start_vga_767x,
1329 ARRAY_SIZE(bridge_start_vga_767x)}},
1330 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1331 ARRAY_SIZE(bridge_start_qvga_772x)},
1332 {bridge_start_vga_772x,
1333 ARRAY_SIZE(bridge_start_vga_772x)}},
1334 };
1335 static const struct reg_array sensor_start[NSENSORS][2] = {
1336 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1337 ARRAY_SIZE(sensor_start_qvga_767x)},
1338 {sensor_start_vga_767x,
1339 ARRAY_SIZE(sensor_start_vga_767x)}},
1340 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1341 ARRAY_SIZE(sensor_start_qvga_772x)},
1342 {sensor_start_vga_772x,
1343 ARRAY_SIZE(sensor_start_vga_772x)}},
1344 };
1345
1346 /* (from ms-win trace) */
1347 if (sd->sensor == SENSOR_OV767x)
1348 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1349 /* black sun enable ? */
1350
1351 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1352 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1353 bridge_start[sd->sensor][mode].len);
1354 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1355 sensor_start[sd->sensor][mode].len);
1356
1357 set_frame_rate(gspca_dev);
1358
1359 if (sd->hue)
1360 sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1361 setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1362 if (sd->autogain)
1363 setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1364 setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1365 setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1366 if (sd->gain)
1367 setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1368 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1369 setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1370 setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1371 if (sd->sharpness)
1372 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1373 sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1374 v4l2_ctrl_g_ctrl(sd->vflip));
1375 setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
1376
1377 ov534_set_led(gspca_dev, 1);
1378 ov534_reg_write(gspca_dev, 0xe0, 0x00);
1379 return gspca_dev->usb_err;
1380 }
1381
1382 static void sd_stopN(struct gspca_dev *gspca_dev)
1383 {
1384 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1385 ov534_set_led(gspca_dev, 0);
1386 }
1387
1388 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1389 #define UVC_STREAM_EOH (1 << 7)
1390 #define UVC_STREAM_ERR (1 << 6)
1391 #define UVC_STREAM_STI (1 << 5)
1392 #define UVC_STREAM_RES (1 << 4)
1393 #define UVC_STREAM_SCR (1 << 3)
1394 #define UVC_STREAM_PTS (1 << 2)
1395 #define UVC_STREAM_EOF (1 << 1)
1396 #define UVC_STREAM_FID (1 << 0)
1397
1398 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1399 u8 *data, int len)
1400 {
1401 struct sd *sd = (struct sd *) gspca_dev;
1402 __u32 this_pts;
1403 u16 this_fid;
1404 int remaining_len = len;
1405 int payload_len;
1406
1407 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1408 do {
1409 len = min(remaining_len, payload_len);
1410
1411 /* Payloads are prefixed with a UVC-style header. We
1412 consider a frame to start when the FID toggles, or the PTS
1413 changes. A frame ends when EOF is set, and we've received
1414 the correct number of bytes. */
1415
1416 /* Verify UVC header. Header length is always 12 */
1417 if (data[0] != 12 || len < 12) {
1418 PDEBUG(D_PACK, "bad header");
1419 goto discard;
1420 }
1421
1422 /* Check errors */
1423 if (data[1] & UVC_STREAM_ERR) {
1424 PDEBUG(D_PACK, "payload error");
1425 goto discard;
1426 }
1427
1428 /* Extract PTS and FID */
1429 if (!(data[1] & UVC_STREAM_PTS)) {
1430 PDEBUG(D_PACK, "PTS not present");
1431 goto discard;
1432 }
1433 this_pts = (data[5] << 24) | (data[4] << 16)
1434 | (data[3] << 8) | data[2];
1435 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1436
1437 /* If PTS or FID has changed, start a new frame. */
1438 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1439 if (gspca_dev->last_packet_type == INTER_PACKET)
1440 gspca_frame_add(gspca_dev, LAST_PACKET,
1441 NULL, 0);
1442 sd->last_pts = this_pts;
1443 sd->last_fid = this_fid;
1444 gspca_frame_add(gspca_dev, FIRST_PACKET,
1445 data + 12, len - 12);
1446 /* If this packet is marked as EOF, end the frame */
1447 } else if (data[1] & UVC_STREAM_EOF) {
1448 sd->last_pts = 0;
1449 if (gspca_dev->pixfmt.pixelformat == V4L2_PIX_FMT_YUYV
1450 && gspca_dev->image_len + len - 12 !=
1451 gspca_dev->pixfmt.width *
1452 gspca_dev->pixfmt.height * 2) {
1453 PDEBUG(D_PACK, "wrong sized frame");
1454 goto discard;
1455 }
1456 gspca_frame_add(gspca_dev, LAST_PACKET,
1457 data + 12, len - 12);
1458 } else {
1459
1460 /* Add the data from this payload */
1461 gspca_frame_add(gspca_dev, INTER_PACKET,
1462 data + 12, len - 12);
1463 }
1464
1465 /* Done this payload */
1466 goto scan_next;
1467
1468 discard:
1469 /* Discard data until a new frame starts. */
1470 gspca_dev->last_packet_type = DISCARD_PACKET;
1471
1472 scan_next:
1473 remaining_len -= len;
1474 data += len;
1475 } while (remaining_len > 0);
1476 }
1477
1478 /* get stream parameters (framerate) */
1479 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1480 struct v4l2_streamparm *parm)
1481 {
1482 struct v4l2_captureparm *cp = &parm->parm.capture;
1483 struct v4l2_fract *tpf = &cp->timeperframe;
1484 struct sd *sd = (struct sd *) gspca_dev;
1485
1486 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1487 tpf->numerator = 1;
1488 tpf->denominator = sd->frame_rate;
1489 }
1490
1491 /* set stream parameters (framerate) */
1492 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1493 struct v4l2_streamparm *parm)
1494 {
1495 struct v4l2_captureparm *cp = &parm->parm.capture;
1496 struct v4l2_fract *tpf = &cp->timeperframe;
1497 struct sd *sd = (struct sd *) gspca_dev;
1498
1499 /* Set requested framerate */
1500 sd->frame_rate = tpf->denominator / tpf->numerator;
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 -- */
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);