blob: 6d9b102be70c9f256e7415726c6f4ec94f50721a [file] [log] [blame]
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03001/*
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03002 * ov534 gspca driver
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03003 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
Jim Paris0f7a50b2008-12-10 05:45:14 -03004 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -03005 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -03006 *
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
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -030030#define OV534_REG_ADDRESS 0xf1 /* sensor address */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030031#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
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030047/* specific webcam descriptor */
48struct sd {
49 struct gspca_dev gspca_dev; /* !! must be the first item */
Jean-Francois Moine8c252052008-12-04 05:06:08 -030050 __u32 last_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -030051 u16 last_fid;
52 u8 frame_rate;
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -030053
54 u8 sensor;
55#define SENSOR_OV772X 0
56#define SENSOR_OV965X 1
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030057};
58
59/* V4L2 controls supported by the driver */
60static struct ctrl sd_ctrls[] = {
61};
62
Jean-Francois Moinecc611b82008-12-29 07:49:41 -030063static const struct v4l2_pix_format vga_mode[] = {
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -030064 {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
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -030071static const u8 bridge_init_ov722x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -030072 { 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
Jim Paris47dfd212008-12-04 04:28:27 -030084 { 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 },
Jim Paris0f7a50b2008-12-10 05:45:14 -0300110 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
111 { 0x1d, 0x00 }, /* payload size */
Jim Paris5ea9c4d2008-12-04 04:36:14 -0300112 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
113 { 0x1d, 0x58 }, /* frame size */
114 { 0x1d, 0x00 }, /* frame size */
Jim Paris47dfd212008-12-04 04:28:27 -0300115
Jim Parisc06eb612008-12-10 05:47:44 -0300116 { 0x1c, 0x0a },
117 { 0x1d, 0x08 }, /* turn on UVC header */
118 { 0x1d, 0x0e }, /* .. */
119
Jim Paris47dfd212008-12-04 04:28:27 -0300120 { 0x8d, 0x1c },
121 { 0x8e, 0x80 },
122 { 0xe5, 0x04 },
123
124 { 0xc0, 0x50 },
125 { 0xc1, 0x3c },
126 { 0xc2, 0x0c },
127};
128
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300129static const u8 sensor_init_ov722x[][2] = {
Jim Paris47dfd212008-12-04 04:28:27 -0300130 { 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};
211
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300212static 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
Jim Paris11d9f252008-12-10 06:06:20 -0300662/* 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:
Jean-Francois Moine15f64862008-12-29 06:19:43 -0300670 sccb_reg_write(gspca_dev, 0x11, 0x01);
671 sccb_reg_write(gspca_dev, 0x0d, 0x41);
672 ov534_reg_write(gspca_dev, 0xe5, 0x02);
Jim Paris11d9f252008-12-10 06:06:20 -0300673 break;
674 case 40:
Jean-Francois Moine15f64862008-12-29 06:19:43 -0300675 sccb_reg_write(gspca_dev, 0x11, 0x02);
676 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
677 ov534_reg_write(gspca_dev, 0xe5, 0x04);
Jim Paris11d9f252008-12-10 06:06:20 -0300678 break;
679/* case 30: */
680 default:
681 fr = 30;
Jean-Francois Moine15f64862008-12-29 06:19:43 -0300682 sccb_reg_write(gspca_dev, 0x11, 0x04);
683 sccb_reg_write(gspca_dev, 0x0d, 0x81);
684 ov534_reg_write(gspca_dev, 0xe5, 0x02);
Jim Paris11d9f252008-12-10 06:06:20 -0300685 break;
686 case 15:
Jean-Francois Moine15f64862008-12-29 06:19:43 -0300687 sccb_reg_write(gspca_dev, 0x11, 0x03);
688 sccb_reg_write(gspca_dev, 0x0d, 0x41);
689 ov534_reg_write(gspca_dev, 0xe5, 0x04);
Jim Paris11d9f252008-12-10 06:06:20 -0300690 break;
691 }
692
693 sd->frame_rate = fr;
694 PDEBUG(D_PROBE, "frame_rate: %d", fr);
695}
Jim Paris47dfd212008-12-04 04:28:27 -0300696
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300697/* this function is called at probe time */
698static int sd_config(struct gspca_dev *gspca_dev,
699 const struct usb_device_id *id)
700{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300701 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300702 struct cam *cam;
703
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300704 sd->sensor = id->driver_info;
705
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300706 cam = &gspca_dev->cam;
707
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300708 cam->cam_mode = vga_mode;
709 cam->nmodes = ARRAY_SIZE(vga_mode);
710
Jean-Francois Moine6929dc62009-04-21 13:45:56 -0300711 if (sd->sensor == SENSOR_OV772X) {
712 cam->bulk = 1;
713 cam->bulk_size = 16384;
714 cam->bulk_nurbs = 2;
715 }
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300716
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300717 return 0;
718}
719
720/* this function is called at probe and resume time */
721static int sd_init(struct gspca_dev *gspca_dev)
722{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300723 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 }
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300777
778 return 0;
779}
780
781static int sd_start(struct gspca_dev *gspca_dev)
782{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300783 struct sd *sd = (struct sd *) gspca_dev;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300784
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300785 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 }
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300800 return 0;
801}
802
803static void sd_stopN(struct gspca_dev *gspca_dev)
804{
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300805 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 }
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300819}
820
Jim Parisfb139222008-12-04 04:52:40 -0300821/* 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
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300831static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
832 __u8 *data, int len)
833{
Jean-Francois Moine8c252052008-12-04 05:06:08 -0300834 struct sd *sd = (struct sd *) gspca_dev;
Jim Parisfb139222008-12-04 04:52:40 -0300835 __u32 this_pts;
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300836 u16 this_fid;
Jim Paris0f7a50b2008-12-10 05:45:14 -0300837 int remaining_len = len;
Jim Paris0f7a50b2008-12-10 05:45:14 -0300838
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300839 do {
840 len = min(remaining_len, 2040); /*fixme: was 2048*/
Jim Paris0f7a50b2008-12-10 05:45:14 -0300841
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300842 /* 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. */
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300846
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300847 /* Verify UVC header. Header length is always 12 */
848 if (data[0] != 12 || len < 12) {
849 PDEBUG(D_PACK, "bad header");
Jim Parisfb139222008-12-04 04:52:40 -0300850 goto discard;
851 }
852
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300853 /* Check errors */
854 if (data[1] & UVC_STREAM_ERR) {
855 PDEBUG(D_PACK, "payload error");
856 goto discard;
Jean-Francois Moine3481c192009-03-19 06:05:06 -0300857 }
Jim Parisfb139222008-12-04 04:52:40 -0300858
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300859 /* Extract PTS and FID */
860 if (!(data[1] & UVC_STREAM_PTS)) {
861 PDEBUG(D_PACK, "PTS not present");
862 goto discard;
863 }
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;
867
868 /* If PTS or FID has changed, start a new frame. */
869 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
Jean-Francois Moineed471192009-04-23 13:52:27 -0300870 if (gspca_dev->last_packet_type == INTER_PACKET)
871 frame = gspca_frame_add(gspca_dev,
872 LAST_PACKET, frame,
873 NULL, 0);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300874 sd->last_pts = this_pts;
875 sd->last_fid = this_fid;
Jean-Francois Moineed471192009-04-23 13:52:27 -0300876 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300877 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300878 /* If this packet is marked as EOF, end the frame */
Jean-Francois Moineed471192009-04-23 13:52:27 -0300879 } else if (data[1] & UVC_STREAM_EOF) {
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300880 sd->last_pts = 0;
881
882 if (frame->data_end - frame->data !=
883 gspca_dev->width * gspca_dev->height * 2) {
884 PDEBUG(D_PACK, "short frame");
885 goto discard;
886 }
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300887 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
Jean-Francois Moineed471192009-04-23 13:52:27 -0300888 data + 12, len - 12);
889 } else {
890
891 /* Add the data from this payload */
892 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
893 data + 12, len - 12);
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300894 }
895
Jean-Francois Moineed471192009-04-23 13:52:27 -0300896
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300897 /* Done this payload */
898 goto scan_next;
Jim Parisfb139222008-12-04 04:52:40 -0300899
900discard:
Jean-Francois Moine84fbdf82009-03-19 06:12:59 -0300901 /* Discard data until a new frame starts. */
902 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
903
904scan_next:
905 remaining_len -= len;
906 data += len;
907 } while (remaining_len > 0);
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300908}
909
Jim Paris11d9f252008-12-10 06:06:20 -0300910/* get stream parameters (framerate) */
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -0300911static int sd_get_streamparm(struct gspca_dev *gspca_dev,
912 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -0300913{
914 struct v4l2_captureparm *cp = &parm->parm.capture;
915 struct v4l2_fract *tpf = &cp->timeperframe;
916 struct sd *sd = (struct sd *) gspca_dev;
917
918 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
919 return -EINVAL;
920
921 cp->capability |= V4L2_CAP_TIMEPERFRAME;
922 tpf->numerator = 1;
923 tpf->denominator = sd->frame_rate;
924
925 return 0;
926}
927
928/* set stream parameters (framerate) */
Jean-Francois Moine5c1a15a2008-12-21 15:02:54 -0300929static int sd_set_streamparm(struct gspca_dev *gspca_dev,
930 struct v4l2_streamparm *parm)
Jim Paris11d9f252008-12-10 06:06:20 -0300931{
932 struct v4l2_captureparm *cp = &parm->parm.capture;
933 struct v4l2_fract *tpf = &cp->timeperframe;
934 struct sd *sd = (struct sd *) gspca_dev;
935
936 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
937 return -EINVAL;
938
939 /* Set requested framerate */
940 sd->frame_rate = tpf->denominator / tpf->numerator;
941 ov534_set_frame_rate(gspca_dev);
942
943 /* Return the actual framerate */
944 tpf->numerator = 1;
945 tpf->denominator = sd->frame_rate;
946
947 return 0;
948}
949
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300950/* sub-driver description */
951static const struct sd_desc sd_desc = {
952 .name = MODULE_NAME,
953 .ctrls = sd_ctrls,
954 .nctrls = ARRAY_SIZE(sd_ctrls),
955 .config = sd_config,
956 .init = sd_init,
957 .start = sd_start,
958 .stopN = sd_stopN,
959 .pkt_scan = sd_pkt_scan,
Jim Paris11d9f252008-12-10 06:06:20 -0300960 .get_streamparm = sd_get_streamparm,
961 .set_streamparm = sd_set_streamparm,
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300962};
963
964/* -- module initialisation -- */
965static const __devinitdata struct usb_device_id device_table[] = {
Jean-Francois Moine2ce949e2009-03-19 06:15:21 -0300966 {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
967 {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300968 {}
969};
970
971MODULE_DEVICE_TABLE(usb, device_table);
972
973/* -- device connect -- */
974static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
975{
976 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
977 THIS_MODULE);
978}
979
980static struct usb_driver sd_driver = {
981 .name = MODULE_NAME,
982 .id_table = device_table,
983 .probe = sd_probe,
984 .disconnect = gspca_disconnect,
985#ifdef CONFIG_PM
986 .suspend = gspca_suspend,
987 .resume = gspca_resume,
988#endif
989};
990
991/* -- module insert / remove -- */
992static int __init sd_mod_init(void)
993{
Alexey Klimovf69e9522009-01-01 13:02:07 -0300994 int ret;
995 ret = usb_register(&sd_driver);
996 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -0300997 return ret;
Antonio Ospitefbb4c6d2008-11-22 05:23:39 -0300998 PDEBUG(D_PROBE, "registered");
999 return 0;
1000}
1001
1002static void __exit sd_mod_exit(void)
1003{
1004 usb_deregister(&sd_driver);
1005 PDEBUG(D_PROBE, "deregistered");
1006}
1007
1008module_init(sd_mod_init);
1009module_exit(sd_mod_exit);