gspca - ov534: Fix ov772x
[linux-2.6/mini2440.git] / drivers / media / video / gspca / ov534.c
blob504f849fb31f78cb51a5128eefba7afde9d7a74a
1 /*
2 * ov534 gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
5 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
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/
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.
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.
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
26 #define MODULE_NAME "ov534"
28 #include "gspca.h"
30 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
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
37 #define OV534_OP_WRITE_3 0x37
38 #define OV534_OP_WRITE_2 0x33
39 #define OV534_OP_READ_2 0xf9
41 #define CTRL_TIMEOUT 500
43 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
44 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
45 MODULE_LICENSE("GPL");
47 /* specific webcam descriptor */
48 struct sd {
49 struct gspca_dev gspca_dev; /* !! must be the first item */
50 __u32 last_pts;
51 u16 last_fid;
52 u8 frame_rate;
54 u8 sensor;
55 #define SENSOR_OV772X 0
56 #define SENSOR_OV965X 1
59 /* V4L2 controls supported by the driver */
60 static struct ctrl sd_ctrls[] = {
63 static const struct v4l2_pix_format vga_mode[] = {
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},
71 static const u8 bridge_init_ov722x[][2] = {
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 },
84 { 0x96, 0x00 },
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 },
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 },
108 { 0x1c, 0x00 },
109 { 0x1d, 0x40 },
110 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
111 { 0x1d, 0x00 }, /* payload size */
112 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
113 { 0x1d, 0x58 }, /* frame size */
114 { 0x1d, 0x00 }, /* frame size */
116 { 0x1c, 0x0a },
117 { 0x1d, 0x08 }, /* turn on UVC header */
118 { 0x1d, 0x0e }, /* .. */
120 { 0x8d, 0x1c },
121 { 0x8e, 0x80 },
122 { 0xe5, 0x04 },
124 { 0xc0, 0x50 },
125 { 0xc1, 0x3c },
126 { 0xc2, 0x0c },
129 static const u8 sensor_init_ov722x[][2] = {
130 { 0x12, 0x80 },
131 { 0x11, 0x01 },
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 },
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 },
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 },
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 },
195 { 0x0c, 0x90 },
197 { 0x2b, 0x00 },
198 { 0x22, 0x7f },
199 { 0x23, 0x03 },
200 { 0x11, 0x01 },
201 { 0x0c, 0xd0 },
202 { 0x64, 0xff },
203 { 0x0d, 0x41 },
205 { 0x14, 0x41 },
206 { 0x0e, 0xcd },
207 { 0xac, 0xbf },
208 { 0x8e, 0x00 },
209 { 0x0c, 0xd0 }
212 static 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},
246 static 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},
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},
427 static 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},
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},
455 static 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) */
492 static 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},
510 static 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},
530 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
532 struct usb_device *udev = gspca_dev->dev;
533 int ret;
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");
546 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
548 struct usb_device *udev = gspca_dev->dev;
549 int ret;
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];
562 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
563 * (direction and output)? */
564 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
566 u8 data;
568 PDEBUG(D_CONF, "led status: %d", status);
570 data = ov534_reg_read(gspca_dev, 0x21);
571 data |= 0x80;
572 ov534_reg_write(gspca_dev, 0x21, data);
574 data = ov534_reg_read(gspca_dev, 0x23);
575 if (status)
576 data |= 0x80;
577 else
578 data &= ~0x80;
580 ov534_reg_write(gspca_dev, 0x23, data);
582 if (!status) {
583 data = ov534_reg_read(gspca_dev, 0x21);
584 data &= ~0x80;
585 ov534_reg_write(gspca_dev, 0x21, data);
589 static int sccb_check_status(struct gspca_dev *gspca_dev)
591 u8 data;
592 int i;
594 for (i = 0; i < 5; i++) {
595 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
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);
609 return 0;
612 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
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);
619 if (!sccb_check_status(gspca_dev))
620 PDEBUG(D_ERR, "sccb_reg_write failed");
623 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
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");
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");
634 return ov534_reg_read(gspca_dev, OV534_REG_READ);
637 /* output a bridge sequence (reg - val) */
638 static void reg_w_array(struct gspca_dev *gspca_dev,
639 const u8 (*data)[2], int len)
641 while (--len >= 0) {
642 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
643 data++;
647 /* output a sensor sequence (reg - val) */
648 static void sccb_w_array(struct gspca_dev *gspca_dev,
649 const u8 (*data)[2], int len)
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);
658 data++;
662 /* set framerate */
663 static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
665 struct sd *sd = (struct sd *) gspca_dev;
666 int fr = sd->frame_rate;
668 switch (fr) {
669 case 50:
670 sccb_reg_write(gspca_dev, 0x11, 0x01);
671 sccb_reg_write(gspca_dev, 0x0d, 0x41);
672 ov534_reg_write(gspca_dev, 0xe5, 0x02);
673 break;
674 case 40:
675 sccb_reg_write(gspca_dev, 0x11, 0x02);
676 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
677 ov534_reg_write(gspca_dev, 0xe5, 0x04);
678 break;
679 /* case 30: */
680 default:
681 fr = 30;
682 sccb_reg_write(gspca_dev, 0x11, 0x04);
683 sccb_reg_write(gspca_dev, 0x0d, 0x81);
684 ov534_reg_write(gspca_dev, 0xe5, 0x02);
685 break;
686 case 15:
687 sccb_reg_write(gspca_dev, 0x11, 0x03);
688 sccb_reg_write(gspca_dev, 0x0d, 0x41);
689 ov534_reg_write(gspca_dev, 0xe5, 0x04);
690 break;
693 sd->frame_rate = fr;
694 PDEBUG(D_PROBE, "frame_rate: %d", fr);
697 /* this function is called at probe time */
698 static int sd_config(struct gspca_dev *gspca_dev,
699 const struct usb_device_id *id)
701 struct sd *sd = (struct sd *) gspca_dev;
702 struct cam *cam;
704 sd->sensor = id->driver_info;
706 cam = &gspca_dev->cam;
708 cam->cam_mode = vga_mode;
709 cam->nmodes = ARRAY_SIZE(vga_mode);
711 cam->bulk_size = 16384;
712 cam->bulk_nurbs = 2;
714 return 0;
717 /* this function is called at probe and resume time */
718 static int sd_init(struct gspca_dev *gspca_dev)
720 struct sd *sd = (struct sd *) gspca_dev;
721 u16 sensor_id;
722 static const u8 sensor_addr[2] = {
723 0x42, /* 0 SENSOR_OV772X */
724 0x60, /* 1 SENSOR_OV965X */
727 /* reset bridge */
728 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
729 ov534_reg_write(gspca_dev, 0xe0, 0x08);
730 msleep(100);
732 /* initialize the sensor address */
733 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
734 sensor_addr[sd->sensor]);
736 /* reset sensor */
737 sccb_reg_write(gspca_dev, 0x12, 0x80);
738 msleep(10);
740 /* probe the sensor */
741 sccb_reg_read(gspca_dev, 0x0a);
742 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
743 sccb_reg_read(gspca_dev, 0x0b);
744 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
745 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
747 /* initialize */
748 switch (sd->sensor) {
749 case SENSOR_OV772X:
750 reg_w_array(gspca_dev, bridge_init_ov722x,
751 ARRAY_SIZE(bridge_init_ov722x));
752 ov534_set_led(gspca_dev, 1);
753 sccb_w_array(gspca_dev, sensor_init_ov722x,
754 ARRAY_SIZE(sensor_init_ov722x));
755 ov534_reg_write(gspca_dev, 0xe0, 0x09);
756 ov534_set_led(gspca_dev, 0);
757 ov534_set_frame_rate(gspca_dev);
758 break;
759 default:
760 /* case SENSOR_OV965X: */
761 reg_w_array(gspca_dev, bridge_init_ov965x,
762 ARRAY_SIZE(bridge_init_ov965x));
763 sccb_w_array(gspca_dev, sensor_init_ov965x,
764 ARRAY_SIZE(sensor_init_ov965x));
765 reg_w_array(gspca_dev, bridge_init_ov965x_2,
766 ARRAY_SIZE(bridge_init_ov965x_2));
767 sccb_w_array(gspca_dev, sensor_init_ov965x_2,
768 ARRAY_SIZE(sensor_init_ov965x_2));
769 ov534_reg_write(gspca_dev, 0xe0, 0x00);
770 ov534_reg_write(gspca_dev, 0xe0, 0x01);
771 ov534_set_led(gspca_dev, 0);
772 ov534_reg_write(gspca_dev, 0xe0, 0x00);
775 return 0;
778 static int sd_start(struct gspca_dev *gspca_dev)
780 struct sd *sd = (struct sd *) gspca_dev;
782 switch (sd->sensor) {
783 case SENSOR_OV772X:
784 ov534_set_led(gspca_dev, 1);
785 ov534_reg_write(gspca_dev, 0xe0, 0x00);
786 break;
787 default:
788 /* case SENSOR_OV965X: */
789 reg_w_array(gspca_dev, bridge_start_ov965x,
790 ARRAY_SIZE(bridge_start_ov965x));
791 sccb_w_array(gspca_dev, sensor_start_ov965x,
792 ARRAY_SIZE(sensor_start_ov965x));
793 ov534_reg_write(gspca_dev, 0xe0, 0x00);
794 ov534_set_led(gspca_dev, 1);
795 /*fixme: other sensor start omitted*/
797 return 0;
800 static void sd_stopN(struct gspca_dev *gspca_dev)
802 struct sd *sd = (struct sd *) gspca_dev;
804 switch (sd->sensor) {
805 case SENSOR_OV772X:
806 ov534_reg_write(gspca_dev, 0xe0, 0x09);
807 ov534_set_led(gspca_dev, 0);
808 break;
809 default:
810 /* case SENSOR_OV965X: */
811 ov534_reg_write(gspca_dev, 0xe0, 0x01);
812 ov534_set_led(gspca_dev, 0);
813 ov534_reg_write(gspca_dev, 0xe0, 0x00);
814 break;
818 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
819 #define UVC_STREAM_EOH (1 << 7)
820 #define UVC_STREAM_ERR (1 << 6)
821 #define UVC_STREAM_STI (1 << 5)
822 #define UVC_STREAM_RES (1 << 4)
823 #define UVC_STREAM_SCR (1 << 3)
824 #define UVC_STREAM_PTS (1 << 2)
825 #define UVC_STREAM_EOF (1 << 1)
826 #define UVC_STREAM_FID (1 << 0)
828 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
829 __u8 *data, int len)
831 struct sd *sd = (struct sd *) gspca_dev;
832 __u32 this_pts;
833 u16 this_fid;
834 int remaining_len = len;
835 int payload_len;
837 payload_len = (sd->sensor == SENSOR_OV772X) ? 2048 : 2040;
838 do {
839 len = min(remaining_len, payload_len);
841 /* Payloads are prefixed with a UVC-style header. We
842 consider a frame to start when the FID toggles, or the PTS
843 changes. A frame ends when EOF is set, and we've received
844 the correct number of bytes. */
846 /* Verify UVC header. Header length is always 12 */
847 if (data[0] != 12 || len < 12) {
848 PDEBUG(D_PACK, "bad header");
849 goto discard;
852 /* Check errors */
853 if (data[1] & UVC_STREAM_ERR) {
854 PDEBUG(D_PACK, "payload error");
855 goto discard;
858 /* Extract PTS and FID */
859 if (!(data[1] & UVC_STREAM_PTS)) {
860 PDEBUG(D_PACK, "PTS not present");
861 goto discard;
863 this_pts = (data[5] << 24) | (data[4] << 16)
864 | (data[3] << 8) | data[2];
865 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
867 /* If PTS or FID has changed, start a new frame. */
868 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
869 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
870 NULL, 0);
871 sd->last_pts = this_pts;
872 sd->last_fid = this_fid;
875 /* Add the data from this payload */
876 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
877 data + 12, len - 12);
879 /* If this packet is marked as EOF, end the frame */
880 if (data[1] & UVC_STREAM_EOF) {
881 sd->last_pts = 0;
883 if (frame->data_end - frame->data !=
884 gspca_dev->width * gspca_dev->height * 2) {
885 PDEBUG(D_PACK, "short frame");
886 goto discard;
889 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
890 NULL, 0);
893 /* Done this payload */
894 goto scan_next;
896 discard:
897 /* Discard data until a new frame starts. */
898 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
900 scan_next:
901 remaining_len -= len;
902 data += len;
903 } while (remaining_len > 0);
906 /* get stream parameters (framerate) */
907 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
908 struct v4l2_streamparm *parm)
910 struct v4l2_captureparm *cp = &parm->parm.capture;
911 struct v4l2_fract *tpf = &cp->timeperframe;
912 struct sd *sd = (struct sd *) gspca_dev;
914 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
915 return -EINVAL;
917 cp->capability |= V4L2_CAP_TIMEPERFRAME;
918 tpf->numerator = 1;
919 tpf->denominator = sd->frame_rate;
921 return 0;
924 /* set stream parameters (framerate) */
925 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
926 struct v4l2_streamparm *parm)
928 struct v4l2_captureparm *cp = &parm->parm.capture;
929 struct v4l2_fract *tpf = &cp->timeperframe;
930 struct sd *sd = (struct sd *) gspca_dev;
932 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
933 return -EINVAL;
935 /* Set requested framerate */
936 sd->frame_rate = tpf->denominator / tpf->numerator;
937 ov534_set_frame_rate(gspca_dev);
939 /* Return the actual framerate */
940 tpf->numerator = 1;
941 tpf->denominator = sd->frame_rate;
943 return 0;
946 /* sub-driver description */
947 static const struct sd_desc sd_desc = {
948 .name = MODULE_NAME,
949 .ctrls = sd_ctrls,
950 .nctrls = ARRAY_SIZE(sd_ctrls),
951 .config = sd_config,
952 .init = sd_init,
953 .start = sd_start,
954 .stopN = sd_stopN,
955 .pkt_scan = sd_pkt_scan,
956 .get_streamparm = sd_get_streamparm,
957 .set_streamparm = sd_set_streamparm,
960 /* -- module initialisation -- */
961 static const __devinitdata struct usb_device_id device_table[] = {
962 {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
963 {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
967 MODULE_DEVICE_TABLE(usb, device_table);
969 /* -- device connect -- */
970 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
972 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
973 THIS_MODULE);
976 static struct usb_driver sd_driver = {
977 .name = MODULE_NAME,
978 .id_table = device_table,
979 .probe = sd_probe,
980 .disconnect = gspca_disconnect,
981 #ifdef CONFIG_PM
982 .suspend = gspca_suspend,
983 .resume = gspca_resume,
984 #endif
987 /* -- module insert / remove -- */
988 static int __init sd_mod_init(void)
990 int ret;
991 ret = usb_register(&sd_driver);
992 if (ret < 0)
993 return ret;
994 PDEBUG(D_PROBE, "registered");
995 return 0;
998 static void __exit sd_mod_exit(void)
1000 usb_deregister(&sd_driver);
1001 PDEBUG(D_PROBE, "deregistered");
1004 module_init(sd_mod_init);
1005 module_exit(sd_mod_exit);