V4L/DVB (11712): gspca - main: Set the current alternate setting only when needed
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / video / gspca / ov534.c
CommitLineData
fbb4c6d2 1/*
2ce949ec 2 * ov534 gspca driver
fbb4c6d2 3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
0f7a50b2 4 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
2ce949ec 5 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
fbb4c6d2
AO
6 *
7 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
8 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
9 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26#define MODULE_NAME "ov534"
27
28#include "gspca.h"
29
2ce949ec 30#define OV534_REG_ADDRESS 0xf1 /* sensor address */
fbb4c6d2
AO
31#define OV534_REG_SUBADDR 0xf2
32#define OV534_REG_WRITE 0xf3
33#define OV534_REG_READ 0xf4
34#define OV534_REG_OPERATION 0xf5
35#define OV534_REG_STATUS 0xf6
36
37#define OV534_OP_WRITE_3 0x37
38#define OV534_OP_WRITE_2 0x33
39#define OV534_OP_READ_2 0xf9
40
41#define CTRL_TIMEOUT 500
42
43MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
44MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
45MODULE_LICENSE("GPL");
46
fbb4c6d2
AO
47/* specific webcam descriptor */
48struct sd {
49 struct gspca_dev gspca_dev; /* !! must be the first item */
8c252050 50 __u32 last_pts;
84fbdf87
JFM
51 u16 last_fid;
52 u8 frame_rate;
2ce949ec
JFM
53
54 u8 sensor;
55#define SENSOR_OV772X 0
56#define SENSOR_OV965X 1
fbb4c6d2
AO
57};
58
59/* V4L2 controls supported by the driver */
60static struct ctrl sd_ctrls[] = {
61};
62
cc611b8a 63static const struct v4l2_pix_format vga_mode[] = {
fbb4c6d2
AO
64 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
65 .bytesperline = 640 * 2,
66 .sizeimage = 640 * 480 * 2,
67 .colorspace = V4L2_COLORSPACE_JPEG,
68 .priv = 0},
69};
70
2ce949ec 71static const u8 bridge_init_ov722x[][2] = {
47dfd21f
JP
72 { 0xc2, 0x0c },
73 { 0x88, 0xf8 },
74 { 0xc3, 0x69 },
75 { 0x89, 0xff },
76 { 0x76, 0x03 },
77 { 0x92, 0x01 },
78 { 0x93, 0x18 },
79 { 0x94, 0x10 },
80 { 0x95, 0x10 },
81 { 0xe2, 0x00 },
82 { 0xe7, 0x3e },
83
47dfd21f
JP
84 { 0x96, 0x00 },
85
86 { 0x97, 0x20 },
87 { 0x97, 0x20 },
88 { 0x97, 0x20 },
89 { 0x97, 0x0a },
90 { 0x97, 0x3f },
91 { 0x97, 0x4a },
92 { 0x97, 0x20 },
93 { 0x97, 0x15 },
94 { 0x97, 0x0b },
95
96 { 0x8e, 0x40 },
97 { 0x1f, 0x81 },
98 { 0x34, 0x05 },
99 { 0xe3, 0x04 },
100 { 0x88, 0x00 },
101 { 0x89, 0x00 },
102 { 0x76, 0x00 },
103 { 0xe7, 0x2e },
104 { 0x31, 0xf9 },
105 { 0x25, 0x42 },
106 { 0x21, 0xf0 },
107
108 { 0x1c, 0x00 },
109 { 0x1d, 0x40 },
0f7a50b2
JP
110 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
111 { 0x1d, 0x00 }, /* payload size */
5ea9c4de
JP
112 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
113 { 0x1d, 0x58 }, /* frame size */
114 { 0x1d, 0x00 }, /* frame size */
47dfd21f 115
c06eb619
JP
116 { 0x1c, 0x0a },
117 { 0x1d, 0x08 }, /* turn on UVC header */
118 { 0x1d, 0x0e }, /* .. */
119
47dfd21f
JP
120 { 0x8d, 0x1c },
121 { 0x8e, 0x80 },
122 { 0xe5, 0x04 },
123
124 { 0xc0, 0x50 },
125 { 0xc1, 0x3c },
126 { 0xc2, 0x0c },
127};
fbb4c6d2 128
2ce949ec 129static const u8 sensor_init_ov722x[][2] = {
47dfd21f
JP
130 { 0x12, 0x80 },
131 { 0x11, 0x01 },
132
133 { 0x3d, 0x03 },
134 { 0x17, 0x26 },
135 { 0x18, 0xa0 },
136 { 0x19, 0x07 },
137 { 0x1a, 0xf0 },
138 { 0x32, 0x00 },
139 { 0x29, 0xa0 },
140 { 0x2c, 0xf0 },
141 { 0x65, 0x20 },
142 { 0x11, 0x01 },
143 { 0x42, 0x7f },
144 { 0x63, 0xe0 },
145 { 0x64, 0xff },
146 { 0x66, 0x00 },
147 { 0x13, 0xf0 },
148 { 0x0d, 0x41 },
149 { 0x0f, 0xc5 },
150 { 0x14, 0x11 },
151
152 { 0x22, 0x7f },
153 { 0x23, 0x03 },
154 { 0x24, 0x40 },
155 { 0x25, 0x30 },
156 { 0x26, 0xa1 },
157 { 0x2a, 0x00 },
158 { 0x2b, 0x00 },
159 { 0x6b, 0xaa },
160 { 0x13, 0xff },
161
162 { 0x90, 0x05 },
163 { 0x91, 0x01 },
164 { 0x92, 0x03 },
165 { 0x93, 0x00 },
166 { 0x94, 0x60 },
167 { 0x95, 0x3c },
168 { 0x96, 0x24 },
169 { 0x97, 0x1e },
170 { 0x98, 0x62 },
171 { 0x99, 0x80 },
172 { 0x9a, 0x1e },
173 { 0x9b, 0x08 },
174 { 0x9c, 0x20 },
175 { 0x9e, 0x81 },
176
177 { 0xa6, 0x04 },
178 { 0x7e, 0x0c },
179 { 0x7f, 0x16 },
180 { 0x80, 0x2a },
181 { 0x81, 0x4e },
182 { 0x82, 0x61 },
183 { 0x83, 0x6f },
184 { 0x84, 0x7b },
185 { 0x85, 0x86 },
186 { 0x86, 0x8e },
187 { 0x87, 0x97 },
188 { 0x88, 0xa4 },
189 { 0x89, 0xaf },
190 { 0x8a, 0xc5 },
191 { 0x8b, 0xd7 },
192 { 0x8c, 0xe8 },
193 { 0x8d, 0x20 },
194
195 { 0x0c, 0x90 },
196
197 { 0x2b, 0x00 },
198 { 0x22, 0x7f },
199 { 0x23, 0x03 },
200 { 0x11, 0x01 },
201 { 0x0c, 0xd0 },
202 { 0x64, 0xff },
203 { 0x0d, 0x41 },
204
205 { 0x14, 0x41 },
206 { 0x0e, 0xcd },
207 { 0xac, 0xbf },
208 { 0x8e, 0x00 },
209 { 0x0c, 0xd0 }
210};
fbb4c6d2 211
2ce949ec
JFM
212static const u8 bridge_init_ov965x[][2] = {
213 {0x88, 0xf8},
214 {0x89, 0xff},
215 {0x76, 0x03},
216 {0x92, 0x03},
217 {0x95, 0x10},
218 {0xe2, 0x00},
219 {0xe7, 0x3e},
220 {0x8d, 0x1c},
221 {0x8e, 0x00},
222 {0x8f, 0x00},
223 {0x1f, 0x00},
224 {0xc3, 0xf9},
225 {0x89, 0xff},
226 {0x88, 0xf8},
227 {0x76, 0x03},
228 {0x92, 0x01},
229 {0x93, 0x18},
230 {0x1c, 0x0a},
231 {0x1d, 0x48},
232 {0xc0, 0x50},
233 {0xc1, 0x3c},
234 {0x34, 0x05},
235 {0xc2, 0x0c},
236 {0xc3, 0xf9},
237 {0x34, 0x05},
238 {0xe7, 0x2e},
239 {0x31, 0xf9},
240 {0x35, 0x02},
241 {0xd9, 0x10},
242 {0x25, 0x42},
243 {0x94, 0x11},
244};
245
246static const u8 sensor_init_ov965x[][2] = {
247 {0x12, 0x80}, /* com7 - reset */
248 {0x00, 0x00}, /* gain */
249 {0x01, 0x80}, /* blue */
250 {0x02, 0x80}, /* red */
251 {0x03, 0x1b}, /* vref */
252 {0x04, 0x03}, /* com1 - exposure low bits */
253 {0x0b, 0x57}, /* ver */
254 {0x0e, 0x61}, /* com5 */
255 {0x0f, 0x42}, /* com6 */
256 {0x11, 0x00}, /* clkrc */
257 {0x12, 0x02}, /* com7 */
258 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
259 {0x14, 0x28}, /* com9 */
260 {0x16, 0x24}, /* rsvd16 */
261 {0x17, 0x1d}, /* hstart*/
262 {0x18, 0xbd}, /* hstop */
263 {0x19, 0x01}, /* vstrt */
264 {0x1a, 0x81}, /* vstop*/
265 {0x1e, 0x04}, /* mvfp */
266 {0x24, 0x3c}, /* aew */
267 {0x25, 0x36}, /* aeb */
268 {0x26, 0x71}, /* vpt */
269 {0x27, 0x08}, /* bbias */
270 {0x28, 0x08}, /* gbbias */
271 {0x29, 0x15}, /* gr com */
272 {0x2a, 0x00},
273 {0x2b, 0x00},
274 {0x2c, 0x08}, /* rbias */
275 {0x32, 0xff}, /* href */
276 {0x33, 0x00}, /* chlf */
277 {0x34, 0x3f}, /* arblm */
278 {0x35, 0x00}, /* rsvd35 */
279 {0x36, 0xf8}, /* rsvd36 */
280 {0x38, 0x72}, /* acom38 */
281 {0x39, 0x57}, /* ofon */
282 {0x3a, 0x80}, /* tslb */
283 {0x3b, 0xc4},
284 {0x3d, 0x99}, /* com13 */
285 {0x3f, 0xc1},
286 {0x40, 0xc0}, /* com15 */
287 {0x41, 0x40}, /* com16 */
288 {0x42, 0xc0},
289 {0x43, 0x0a},
290 {0x44, 0xf0},
291 {0x45, 0x46},
292 {0x46, 0x62},
293 {0x47, 0x2a},
294 {0x48, 0x3c},
295 {0x4a, 0xfc},
296 {0x4b, 0xfc},
297 {0x4c, 0x7f},
298 {0x4d, 0x7f},
299 {0x4e, 0x7f},
300 {0x4f, 0x98},
301 {0x50, 0x98},
302 {0x51, 0x00},
303 {0x52, 0x28},
304 {0x53, 0x70},
305 {0x54, 0x98},
306 {0x58, 0x1a},
307 {0x59, 0x85},
308 {0x5a, 0xa9},
309 {0x5b, 0x64},
310 {0x5c, 0x84},
311 {0x5d, 0x53},
312 {0x5e, 0x0e},
313 {0x5f, 0xf0},
314 {0x60, 0xf0},
315 {0x61, 0xf0},
316 {0x62, 0x00}, /* lcc1 */
317 {0x63, 0x00}, /* lcc2 */
318 {0x64, 0x02}, /* lcc3 */
319 {0x65, 0x16}, /* lcc4 */
320 {0x66, 0x01}, /* lcc5 */
321 {0x69, 0x02}, /* hv */
322 {0x6b, 0x5a}, /* dbvl */
323 {0x6c, 0x04},
324 {0x6d, 0x55},
325 {0x6e, 0x00},
326 {0x6f, 0x9d},
327 {0x70, 0x21},
328 {0x71, 0x78},
329 {0x72, 0x00},
330 {0x73, 0x01},
331 {0x74, 0x3a},
332 {0x75, 0x35},
333 {0x76, 0x01},
334 {0x77, 0x02},
335 {0x7a, 0x12},
336 {0x7b, 0x08},
337 {0x7c, 0x16},
338 {0x7d, 0x30},
339 {0x7e, 0x5e},
340 {0x7f, 0x72},
341 {0x80, 0x82},
342 {0x81, 0x8e},
343 {0x82, 0x9a},
344 {0x83, 0xa4},
345 {0x84, 0xac},
346 {0x85, 0xb8},
347 {0x86, 0xc3},
348 {0x87, 0xd6},
349 {0x88, 0xe6},
350 {0x89, 0xf2},
351 {0x8a, 0x03},
352 {0x8c, 0x89},
353 {0x14, 0x28}, /* com9 */
354 {0x90, 0x7d},
355 {0x91, 0x7b},
356 {0x9d, 0x03},
357 {0x9e, 0x04},
358 {0x9f, 0x7a},
359 {0xa0, 0x79},
360 {0xa1, 0x40}, /* aechm */
361 {0xa4, 0x50},
362 {0xa5, 0x68}, /* com26 */
363 {0xa6, 0x4a},
364 {0xa8, 0xc1}, /* acoma8 */
365 {0xa9, 0xef}, /* acoma9 */
366 {0xaa, 0x92},
367 {0xab, 0x04},
368 {0xac, 0x80},
369 {0xad, 0x80},
370 {0xae, 0x80},
371 {0xaf, 0x80},
372 {0xb2, 0xf2},
373 {0xb3, 0x20},
374 {0xb4, 0x20},
375 {0xb5, 0x00},
376 {0xb6, 0xaf},
377 {0xbb, 0xae},
378 {0xbc, 0x7f},
379 {0xdb, 0x7f},
380 {0xbe, 0x7f},
381 {0xbf, 0x7f},
382 {0xc0, 0xe2},
383 {0xc1, 0xc0},
384 {0xc2, 0x01},
385 {0xc3, 0x4e},
386 {0xc6, 0x85},
387 {0xc7, 0x80},
388 {0xc9, 0xe0},
389 {0xca, 0xe8},
390 {0xcb, 0xf0},
391 {0xcc, 0xd8},
392 {0xcd, 0xf1},
393 {0x4f, 0x98},
394 {0x50, 0x98},
395 {0x51, 0x00},
396 {0x52, 0x28},
397 {0x53, 0x70},
398 {0x54, 0x98},
399 {0x58, 0x1a},
400 {0xff, 0x41}, /* read 41, write ff 00 */
401 {0x41, 0x40}, /* com16 */
402 {0xc5, 0x03},
403 {0x6a, 0x02},
404
405 {0x12, 0x62}, /* com7 - VGA + CIF */
406 {0x36, 0xfa}, /* rsvd36 */
407 {0x69, 0x0a}, /* hv */
408 {0x8c, 0x89}, /* com22 */
409 {0x14, 0x28}, /* com9 */
410 {0x3e, 0x0c},
411 {0x41, 0x40}, /* com16 */
412 {0x72, 0x00},
413 {0x73, 0x00},
414 {0x74, 0x3a},
415 {0x75, 0x35},
416 {0x76, 0x01},
417 {0xc7, 0x80},
418 {0x03, 0x12}, /* vref */
419 {0x17, 0x16}, /* hstart */
420 {0x18, 0x02}, /* hstop */
421 {0x19, 0x01}, /* vstrt */
422 {0x1a, 0x3d}, /* vstop */
423 {0x32, 0xff}, /* href */
424 {0xc0, 0xaa},
425};
426
427static const u8 bridge_init_ov965x_2[][2] = {
428 {0x94, 0xaa},
429 {0xf1, 0x60},
430 {0xe5, 0x04},
431 {0xc0, 0x50},
432 {0xc1, 0x3c},
433 {0x8c, 0x00},
434 {0x8d, 0x1c},
435 {0x34, 0x05},
436
437 {0xc2, 0x0c},
438 {0xc3, 0xf9},
439 {0xda, 0x01},
440 {0x50, 0x00},
441 {0x51, 0xa0},
442 {0x52, 0x3c},
443 {0x53, 0x00},
444 {0x54, 0x00},
445 {0x55, 0x00},
446 {0x57, 0x00},
447 {0x5c, 0x00},
448 {0x5a, 0xa0},
449 {0x5b, 0x78},
450 {0x35, 0x02},
451 {0xd9, 0x10},
452 {0x94, 0x11},
453};
454
455static const u8 sensor_init_ov965x_2[][2] = {
456 {0x3b, 0xc4},
457 {0x1e, 0x04}, /* mvfp */
458 {0x13, 0xe0}, /* com8 */
459 {0x00, 0x00}, /* gain */
460 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
461 {0x11, 0x03}, /* clkrc */
462 {0x6b, 0x5a}, /* dblv */
463 {0x6a, 0x05},
464 {0xc5, 0x07},
465 {0xa2, 0x4b},
466 {0xa3, 0x3e},
467 {0x2d, 0x00},
468 {0xff, 0x42}, /* read 42, write ff 00 */
469 {0x42, 0xc0},
470 {0x2d, 0x00},
471 {0xff, 0x42}, /* read 42, write ff 00 */
472 {0x42, 0xc1},
473 {0x3f, 0x01},
474 {0xff, 0x42}, /* read 42, write ff 00 */
475 {0x42, 0xc1},
476 {0x4f, 0x98},
477 {0x50, 0x98},
478 {0x51, 0x00},
479 {0x52, 0x28},
480 {0x53, 0x70},
481 {0x54, 0x98},
482 {0x58, 0x1a},
483 {0xff, 0x41}, /* read 41, write ff 00 */
484 {0x41, 0x40}, /* com16 */
485 {0x56, 0x40},
486 {0x55, 0x8f},
487 {0x10, 0x25}, /* aech - exposure high bits */
488 {0xff, 0x13}, /* read 13, write ff 00 */
489 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
490};
491
492static const u8 bridge_start_ov965x[][2] = {
493 {0xc2, 0x4c},
494 {0xc3, 0xf9},
495 {0x50, 0x00},
496 {0x51, 0xa0},
497 {0x52, 0x78},
498 {0x53, 0x00},
499 {0x54, 0x00},
500 {0x55, 0x00},
501 {0x57, 0x00},
502 {0x5c, 0x00},
503 {0x5a, 0x28},
504 {0x5b, 0x1e},
505 {0x35, 0x00},
506 {0xd9, 0x21},
507 {0x94, 0x11},
508};
509
510static const u8 sensor_start_ov965x[][2] = {
511 {0x3b, 0xe4},
512 {0x1e, 0x04}, /* mvfp */
513 {0x13, 0xe0}, /* com8 */
514 {0x00, 0x00},
515 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
516 {0x11, 0x01}, /* clkrc */
517 {0x6b, 0x5a}, /* dblv */
518 {0x6a, 0x02},
519 {0xc5, 0x03},
520 {0xa2, 0x96},
521 {0xa3, 0x7d},
522 {0xff, 0x13}, /* read 13, write ff 00 */
523 {0x13, 0xe7},
524 {0x3a, 0x80},
525 {0xff, 0x42}, /* read 42, write ff 00 */
526 {0x42, 0xc1},
527};
528
529
530static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
531{
532 struct usb_device *udev = gspca_dev->dev;
533 int ret;
534
535 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
536 gspca_dev->usb_buf[0] = val;
537 ret = usb_control_msg(udev,
538 usb_sndctrlpipe(udev, 0),
539 0x01,
540 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
541 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
542 if (ret < 0)
543 PDEBUG(D_ERR, "write failed");
544}
545
546static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
547{
548 struct usb_device *udev = gspca_dev->dev;
549 int ret;
550
551 ret = usb_control_msg(udev,
552 usb_rcvctrlpipe(udev, 0),
553 0x01,
554 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
555 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
556 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
557 if (ret < 0)
558 PDEBUG(D_ERR, "read failed");
559 return gspca_dev->usb_buf[0];
560}
561
562/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
563 * (direction and output)? */
564static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
565{
566 u8 data;
567
568 PDEBUG(D_CONF, "led status: %d", status);
569
570 data = ov534_reg_read(gspca_dev, 0x21);
571 data |= 0x80;
572 ov534_reg_write(gspca_dev, 0x21, data);
573
574 data = ov534_reg_read(gspca_dev, 0x23);
575 if (status)
576 data |= 0x80;
577 else
578 data &= ~0x80;
579
580 ov534_reg_write(gspca_dev, 0x23, data);
581
582 if (!status) {
583 data = ov534_reg_read(gspca_dev, 0x21);
584 data &= ~0x80;
585 ov534_reg_write(gspca_dev, 0x21, data);
586 }
587}
588
589static int sccb_check_status(struct gspca_dev *gspca_dev)
590{
591 u8 data;
592 int i;
593
594 for (i = 0; i < 5; i++) {
595 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
596
597 switch (data) {
598 case 0x00:
599 return 1;
600 case 0x04:
601 return 0;
602 case 0x03:
603 break;
604 default:
605 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
606 data, i + 1);
607 }
608 }
609 return 0;
610}
611
612static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
613{
614 PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
615 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
616 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
617 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
618
619 if (!sccb_check_status(gspca_dev))
620 PDEBUG(D_ERR, "sccb_reg_write failed");
621}
622
623static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
624{
625 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
626 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
627 if (!sccb_check_status(gspca_dev))
628 PDEBUG(D_ERR, "sccb_reg_read failed 1");
629
630 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
631 if (!sccb_check_status(gspca_dev))
632 PDEBUG(D_ERR, "sccb_reg_read failed 2");
633
634 return ov534_reg_read(gspca_dev, OV534_REG_READ);
635}
636
637/* output a bridge sequence (reg - val) */
638static void reg_w_array(struct gspca_dev *gspca_dev,
639 const u8 (*data)[2], int len)
640{
641 while (--len >= 0) {
642 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
643 data++;
644 }
645}
646
647/* output a sensor sequence (reg - val) */
648static void sccb_w_array(struct gspca_dev *gspca_dev,
649 const u8 (*data)[2], int len)
650{
651 while (--len >= 0) {
652 if ((*data)[0] != 0xff) {
653 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
654 } else {
655 sccb_reg_read(gspca_dev, (*data)[1]);
656 sccb_reg_write(gspca_dev, 0xff, 0x00);
657 }
658 data++;
659 }
660}
661
11d9f25d
JP
662/* set framerate */
663static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
664{
665 struct sd *sd = (struct sd *) gspca_dev;
666 int fr = sd->frame_rate;
667
668 switch (fr) {
669 case 50:
15f64864
JFM
670 sccb_reg_write(gspca_dev, 0x11, 0x01);
671 sccb_reg_write(gspca_dev, 0x0d, 0x41);
672 ov534_reg_write(gspca_dev, 0xe5, 0x02);
11d9f25d
JP
673 break;
674 case 40:
15f64864
JFM
675 sccb_reg_write(gspca_dev, 0x11, 0x02);
676 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
677 ov534_reg_write(gspca_dev, 0xe5, 0x04);
11d9f25d
JP
678 break;
679/* case 30: */
680 default:
681 fr = 30;
15f64864
JFM
682 sccb_reg_write(gspca_dev, 0x11, 0x04);
683 sccb_reg_write(gspca_dev, 0x0d, 0x81);
684 ov534_reg_write(gspca_dev, 0xe5, 0x02);
11d9f25d
JP
685 break;
686 case 15:
15f64864
JFM
687 sccb_reg_write(gspca_dev, 0x11, 0x03);
688 sccb_reg_write(gspca_dev, 0x0d, 0x41);
689 ov534_reg_write(gspca_dev, 0xe5, 0x04);
11d9f25d
JP
690 break;
691 }
692
693 sd->frame_rate = fr;
694 PDEBUG(D_PROBE, "frame_rate: %d", fr);
695}
fbb4c6d2 696
fbb4c6d2
AO
697/* this function is called at probe time */
698static int sd_config(struct gspca_dev *gspca_dev,
699 const struct usb_device_id *id)
700{
2ce949ec 701 struct sd *sd = (struct sd *) gspca_dev;
fbb4c6d2
AO
702 struct cam *cam;
703
2ce949ec
JFM
704 sd->sensor = id->driver_info;
705
fbb4c6d2
AO
706 cam = &gspca_dev->cam;
707
fbb4c6d2
AO
708 cam->cam_mode = vga_mode;
709 cam->nmodes = ARRAY_SIZE(vga_mode);
710
6929dc6b
JFM
711 if (sd->sensor == SENSOR_OV772X) {
712 cam->bulk = 1;
713 cam->bulk_size = 16384;
714 cam->bulk_nurbs = 2;
715 }
fbb4c6d2 716
fbb4c6d2
AO
717 return 0;
718}
719
720/* this function is called at probe and resume time */
721static int sd_init(struct gspca_dev *gspca_dev)
722{
2ce949ec
JFM
723 struct sd *sd = (struct sd *) gspca_dev;
724 u16 sensor_id;
725 static const u8 sensor_addr[2] = {
726 0x42, /* 0 SENSOR_OV772X */
727 0x60, /* 1 SENSOR_OV965X */
728 };
729
730 /* reset bridge */
731 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
732 ov534_reg_write(gspca_dev, 0xe0, 0x08);
733 msleep(100);
734
735 /* initialize the sensor address */
736 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
737 sensor_addr[sd->sensor]);
738
739 /* reset sensor */
740 sccb_reg_write(gspca_dev, 0x12, 0x80);
741 msleep(10);
742
743 /* probe the sensor */
744 sccb_reg_read(gspca_dev, 0x0a);
745 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
746 sccb_reg_read(gspca_dev, 0x0b);
747 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
748 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
749
750 /* initialize */
751 switch (sd->sensor) {
752 case SENSOR_OV772X:
753 reg_w_array(gspca_dev, bridge_init_ov722x,
754 ARRAY_SIZE(bridge_init_ov722x));
755 ov534_set_led(gspca_dev, 1);
756 sccb_w_array(gspca_dev, sensor_init_ov722x,
757 ARRAY_SIZE(sensor_init_ov722x));
758 ov534_reg_write(gspca_dev, 0xe0, 0x09);
759 ov534_set_led(gspca_dev, 0);
760 ov534_set_frame_rate(gspca_dev);
761 break;
762 default:
763/* case SENSOR_OV965X: */
764 reg_w_array(gspca_dev, bridge_init_ov965x,
765 ARRAY_SIZE(bridge_init_ov965x));
766 sccb_w_array(gspca_dev, sensor_init_ov965x,
767 ARRAY_SIZE(sensor_init_ov965x));
768 reg_w_array(gspca_dev, bridge_init_ov965x_2,
769 ARRAY_SIZE(bridge_init_ov965x_2));
770 sccb_w_array(gspca_dev, sensor_init_ov965x_2,
771 ARRAY_SIZE(sensor_init_ov965x_2));
772 ov534_reg_write(gspca_dev, 0xe0, 0x00);
773 ov534_reg_write(gspca_dev, 0xe0, 0x01);
774 ov534_set_led(gspca_dev, 0);
775 ov534_reg_write(gspca_dev, 0xe0, 0x00);
776 }
fbb4c6d2
AO
777
778 return 0;
779}
780
781static int sd_start(struct gspca_dev *gspca_dev)
782{
2ce949ec 783 struct sd *sd = (struct sd *) gspca_dev;
fbb4c6d2 784
2ce949ec
JFM
785 switch (sd->sensor) {
786 case SENSOR_OV772X:
787 ov534_set_led(gspca_dev, 1);
788 ov534_reg_write(gspca_dev, 0xe0, 0x00);
789 break;
790 default:
791/* case SENSOR_OV965X: */
792 reg_w_array(gspca_dev, bridge_start_ov965x,
793 ARRAY_SIZE(bridge_start_ov965x));
794 sccb_w_array(gspca_dev, sensor_start_ov965x,
795 ARRAY_SIZE(sensor_start_ov965x));
796 ov534_reg_write(gspca_dev, 0xe0, 0x00);
797 ov534_set_led(gspca_dev, 1);
798/*fixme: other sensor start omitted*/
799 }
fbb4c6d2
AO
800 return 0;
801}
802
803static void sd_stopN(struct gspca_dev *gspca_dev)
804{
2ce949ec
JFM
805 struct sd *sd = (struct sd *) gspca_dev;
806
807 switch (sd->sensor) {
808 case SENSOR_OV772X:
809 ov534_reg_write(gspca_dev, 0xe0, 0x09);
810 ov534_set_led(gspca_dev, 0);
811 break;
812 default:
813/* case SENSOR_OV965X: */
814 ov534_reg_write(gspca_dev, 0xe0, 0x01);
815 ov534_set_led(gspca_dev, 0);
816 ov534_reg_write(gspca_dev, 0xe0, 0x00);
817 break;
818 }
fbb4c6d2
AO
819}
820
fb139224
JP
821/* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
822#define UVC_STREAM_EOH (1 << 7)
823#define UVC_STREAM_ERR (1 << 6)
824#define UVC_STREAM_STI (1 << 5)
825#define UVC_STREAM_RES (1 << 4)
826#define UVC_STREAM_SCR (1 << 3)
827#define UVC_STREAM_PTS (1 << 2)
828#define UVC_STREAM_EOF (1 << 1)
829#define UVC_STREAM_FID (1 << 0)
830
fbb4c6d2
AO
831static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
832 __u8 *data, int len)
833{
8c252050 834 struct sd *sd = (struct sd *) gspca_dev;
fb139224 835 __u32 this_pts;
84fbdf87 836 u16 this_fid;
0f7a50b2 837 int remaining_len = len;
0f7a50b2 838
84fbdf87
JFM
839 do {
840 len = min(remaining_len, 2040); /*fixme: was 2048*/
fb139224 841
84fbdf87
JFM
842 /* Payloads are prefixed with a UVC-style header. We
843 consider a frame to start when the FID toggles, or the PTS
844 changes. A frame ends when EOF is set, and we've received
845 the correct number of bytes. */
fb139224 846
84fbdf87
JFM
847 /* Verify UVC header. Header length is always 12 */
848 if (data[0] != 12 || len < 12) {
849 PDEBUG(D_PACK, "bad header");
850 goto discard;
851 }
fb139224 852
84fbdf87
JFM
853 /* Check errors */
854 if (data[1] & UVC_STREAM_ERR) {
855 PDEBUG(D_PACK, "payload error");
856 goto discard;
857 }
fb139224 858
84fbdf87
JFM
859 /* Extract PTS and FID */
860 if (!(data[1] & UVC_STREAM_PTS)) {
861 PDEBUG(D_PACK, "PTS not present");
fb139224
JP
862 goto discard;
863 }
84fbdf87
JFM
864 this_pts = (data[5] << 24) | (data[4] << 16)
865 | (data[3] << 8) | data[2];
866 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
fb139224 867
84fbdf87
JFM
868 /* If PTS or FID has changed, start a new frame. */
869 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
870 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
3481c198 871 NULL, 0);
84fbdf87
JFM
872 sd->last_pts = this_pts;
873 sd->last_fid = this_fid;
874 }
875
876 /* Add the data from this payload */
877 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
878 data + 12, len - 12);
879
880 /* If this packet is marked as EOF, end the frame */
881 if (data[1] & UVC_STREAM_EOF) {
882 sd->last_pts = 0;
883
884 if (frame->data_end - frame->data !=
885 gspca_dev->width * gspca_dev->height * 2) {
886 PDEBUG(D_PACK, "short frame");
887 goto discard;
888 }
889
890 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
891 NULL, 0);
3481c198 892 }
fb139224 893
84fbdf87
JFM
894 /* Done this payload */
895 goto scan_next;
fb139224
JP
896
897discard:
84fbdf87
JFM
898 /* Discard data until a new frame starts. */
899 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
900
901scan_next:
902 remaining_len -= len;
903 data += len;
904 } while (remaining_len > 0);
fbb4c6d2
AO
905}
906
11d9f25d 907/* get stream parameters (framerate) */
5c1a15a1
JFM
908static int sd_get_streamparm(struct gspca_dev *gspca_dev,
909 struct v4l2_streamparm *parm)
11d9f25d
JP
910{
911 struct v4l2_captureparm *cp = &parm->parm.capture;
912 struct v4l2_fract *tpf = &cp->timeperframe;
913 struct sd *sd = (struct sd *) gspca_dev;
914
915 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
916 return -EINVAL;
917
918 cp->capability |= V4L2_CAP_TIMEPERFRAME;
919 tpf->numerator = 1;
920 tpf->denominator = sd->frame_rate;
921
922 return 0;
923}
924
925/* set stream parameters (framerate) */
5c1a15a1
JFM
926static int sd_set_streamparm(struct gspca_dev *gspca_dev,
927 struct v4l2_streamparm *parm)
11d9f25d
JP
928{
929 struct v4l2_captureparm *cp = &parm->parm.capture;
930 struct v4l2_fract *tpf = &cp->timeperframe;
931 struct sd *sd = (struct sd *) gspca_dev;
932
933 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
934 return -EINVAL;
935
936 /* Set requested framerate */
937 sd->frame_rate = tpf->denominator / tpf->numerator;
938 ov534_set_frame_rate(gspca_dev);
939
940 /* Return the actual framerate */
941 tpf->numerator = 1;
942 tpf->denominator = sd->frame_rate;
943
944 return 0;
945}
946
fbb4c6d2
AO
947/* sub-driver description */
948static const struct sd_desc sd_desc = {
949 .name = MODULE_NAME,
950 .ctrls = sd_ctrls,
951 .nctrls = ARRAY_SIZE(sd_ctrls),
952 .config = sd_config,
953 .init = sd_init,
954 .start = sd_start,
955 .stopN = sd_stopN,
956 .pkt_scan = sd_pkt_scan,
11d9f25d
JP
957 .get_streamparm = sd_get_streamparm,
958 .set_streamparm = sd_set_streamparm,
fbb4c6d2
AO
959};
960
961/* -- module initialisation -- */
962static const __devinitdata struct usb_device_id device_table[] = {
2ce949ec
JFM
963 {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
964 {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
fbb4c6d2
AO
965 {}
966};
967
968MODULE_DEVICE_TABLE(usb, device_table);
969
970/* -- device connect -- */
971static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
972{
973 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
974 THIS_MODULE);
975}
976
977static struct usb_driver sd_driver = {
978 .name = MODULE_NAME,
979 .id_table = device_table,
980 .probe = sd_probe,
981 .disconnect = gspca_disconnect,
982#ifdef CONFIG_PM
983 .suspend = gspca_suspend,
984 .resume = gspca_resume,
985#endif
986};
987
988/* -- module insert / remove -- */
989static int __init sd_mod_init(void)
990{
f69e9529
AK
991 int ret;
992 ret = usb_register(&sd_driver);
993 if (ret < 0)
e6b14849 994 return ret;
fbb4c6d2
AO
995 PDEBUG(D_PROBE, "registered");
996 return 0;
997}
998
999static void __exit sd_mod_exit(void)
1000{
1001 usb_deregister(&sd_driver);
1002 PDEBUG(D_PROBE, "deregistered");
1003}
1004
1005module_init(sd_mod_init);
1006module_exit(sd_mod_exit);