Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / gspca / ov534.c
blob957e05e2d08fbbf3944beec9c2fd76cc18d88808
1 /*
2 * ov534-ov772x gspca driver
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
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/
12 * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
13 * PS3 Eye camera, brightness, contrast, hue, AWB control added
14 * by Max Thrun <bear24rw@gmail.com>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #define MODULE_NAME "ov534"
33 #include "gspca.h"
35 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
36 #define OV534_REG_SUBADDR 0xf2
37 #define OV534_REG_WRITE 0xf3
38 #define OV534_REG_READ 0xf4
39 #define OV534_REG_OPERATION 0xf5
40 #define OV534_REG_STATUS 0xf6
42 #define OV534_OP_WRITE_3 0x37
43 #define OV534_OP_WRITE_2 0x33
44 #define OV534_OP_READ_2 0xf9
46 #define CTRL_TIMEOUT 500
48 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
49 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
50 MODULE_LICENSE("GPL");
52 /* specific webcam descriptor */
53 struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
55 __u32 last_pts;
56 u16 last_fid;
57 u8 frame_rate;
59 u8 brightness;
60 u8 contrast;
61 u8 gain;
62 u8 exposure;
63 u8 redblc;
64 u8 blueblc;
65 u8 hue;
66 u8 autogain;
67 u8 awb;
68 s8 sharpness;
69 u8 hflip;
70 u8 vflip;
74 /* V4L2 controls supported by the driver */
75 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
87 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
88 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
89 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);
93 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
94 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
95 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
96 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
97 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
98 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
100 static const struct ctrl sd_ctrls[] = {
101 { /* 0 */
103 .id = V4L2_CID_BRIGHTNESS,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Brightness",
106 .minimum = 0,
107 .maximum = 255,
108 .step = 1,
109 #define BRIGHTNESS_DEF 20
110 .default_value = BRIGHTNESS_DEF,
112 .set = sd_setbrightness,
113 .get = sd_getbrightness,
115 { /* 1 */
117 .id = V4L2_CID_CONTRAST,
118 .type = V4L2_CTRL_TYPE_INTEGER,
119 .name = "Contrast",
120 .minimum = 0,
121 .maximum = 255,
122 .step = 1,
123 #define CONTRAST_DEF 37
124 .default_value = CONTRAST_DEF,
126 .set = sd_setcontrast,
127 .get = sd_getcontrast,
129 { /* 2 */
131 .id = V4L2_CID_GAIN,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Main Gain",
134 .minimum = 0,
135 .maximum = 63,
136 .step = 1,
137 #define GAIN_DEF 20
138 .default_value = GAIN_DEF,
140 .set = sd_setgain,
141 .get = sd_getgain,
143 { /* 3 */
145 .id = V4L2_CID_EXPOSURE,
146 .type = V4L2_CTRL_TYPE_INTEGER,
147 .name = "Exposure",
148 .minimum = 0,
149 .maximum = 255,
150 .step = 1,
151 #define EXPO_DEF 120
152 .default_value = EXPO_DEF,
154 .set = sd_setexposure,
155 .get = sd_getexposure,
157 { /* 4 */
159 .id = V4L2_CID_RED_BALANCE,
160 .type = V4L2_CTRL_TYPE_INTEGER,
161 .name = "Red Balance",
162 .minimum = 0,
163 .maximum = 255,
164 .step = 1,
165 #define RED_BALANCE_DEF 128
166 .default_value = RED_BALANCE_DEF,
168 .set = sd_setredblc,
169 .get = sd_getredblc,
171 { /* 5 */
173 .id = V4L2_CID_BLUE_BALANCE,
174 .type = V4L2_CTRL_TYPE_INTEGER,
175 .name = "Blue Balance",
176 .minimum = 0,
177 .maximum = 255,
178 .step = 1,
179 #define BLUE_BALANCE_DEF 128
180 .default_value = BLUE_BALANCE_DEF,
182 .set = sd_setblueblc,
183 .get = sd_getblueblc,
185 { /* 6 */
187 .id = V4L2_CID_HUE,
188 .type = V4L2_CTRL_TYPE_INTEGER,
189 .name = "Hue",
190 .minimum = 0,
191 .maximum = 255,
192 .step = 1,
193 #define HUE_DEF 143
194 .default_value = HUE_DEF,
196 .set = sd_sethue,
197 .get = sd_gethue,
199 { /* 7 */
201 .id = V4L2_CID_AUTOGAIN,
202 .type = V4L2_CTRL_TYPE_BOOLEAN,
203 .name = "Autogain",
204 .minimum = 0,
205 .maximum = 1,
206 .step = 1,
207 #define AUTOGAIN_DEF 0
208 .default_value = AUTOGAIN_DEF,
210 .set = sd_setautogain,
211 .get = sd_getautogain,
213 #define AWB_IDX 8
214 { /* 8 */
216 .id = V4L2_CID_AUTO_WHITE_BALANCE,
217 .type = V4L2_CTRL_TYPE_BOOLEAN,
218 .name = "Auto White Balance",
219 .minimum = 0,
220 .maximum = 1,
221 .step = 1,
222 #define AWB_DEF 0
223 .default_value = AWB_DEF,
225 .set = sd_setawb,
226 .get = sd_getawb,
228 { /* 9 */
230 .id = V4L2_CID_SHARPNESS,
231 .type = V4L2_CTRL_TYPE_INTEGER,
232 .name = "Sharpness",
233 .minimum = 0,
234 .maximum = 63,
235 .step = 1,
236 #define SHARPNESS_DEF 0
237 .default_value = SHARPNESS_DEF,
239 .set = sd_setsharpness,
240 .get = sd_getsharpness,
242 { /* 10 */
244 .id = V4L2_CID_HFLIP,
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
246 .name = "HFlip",
247 .minimum = 0,
248 .maximum = 1,
249 .step = 1,
250 #define HFLIP_DEF 0
251 .default_value = HFLIP_DEF,
253 .set = sd_sethflip,
254 .get = sd_gethflip,
256 { /* 11 */
258 .id = V4L2_CID_VFLIP,
259 .type = V4L2_CTRL_TYPE_BOOLEAN,
260 .name = "VFlip",
261 .minimum = 0,
262 .maximum = 1,
263 .step = 1,
264 #define VFLIP_DEF 0
265 .default_value = VFLIP_DEF,
267 .set = sd_setvflip,
268 .get = sd_getvflip,
272 static const struct v4l2_pix_format ov772x_mode[] = {
273 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
274 .bytesperline = 320 * 2,
275 .sizeimage = 320 * 240 * 2,
276 .colorspace = V4L2_COLORSPACE_SRGB,
277 .priv = 1},
278 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
279 .bytesperline = 640 * 2,
280 .sizeimage = 640 * 480 * 2,
281 .colorspace = V4L2_COLORSPACE_SRGB,
282 .priv = 0},
285 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
286 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
288 static const struct framerates ov772x_framerates[] = {
289 { /* 320x240 */
290 .rates = qvga_rates,
291 .nrates = ARRAY_SIZE(qvga_rates),
293 { /* 640x480 */
294 .rates = vga_rates,
295 .nrates = ARRAY_SIZE(vga_rates),
299 static const u8 bridge_init[][2] = {
300 { 0xc2, 0x0c },
301 { 0x88, 0xf8 },
302 { 0xc3, 0x69 },
303 { 0x89, 0xff },
304 { 0x76, 0x03 },
305 { 0x92, 0x01 },
306 { 0x93, 0x18 },
307 { 0x94, 0x10 },
308 { 0x95, 0x10 },
309 { 0xe2, 0x00 },
310 { 0xe7, 0x3e },
312 { 0x96, 0x00 },
314 { 0x97, 0x20 },
315 { 0x97, 0x20 },
316 { 0x97, 0x20 },
317 { 0x97, 0x0a },
318 { 0x97, 0x3f },
319 { 0x97, 0x4a },
320 { 0x97, 0x20 },
321 { 0x97, 0x15 },
322 { 0x97, 0x0b },
324 { 0x8e, 0x40 },
325 { 0x1f, 0x81 },
326 { 0x34, 0x05 },
327 { 0xe3, 0x04 },
328 { 0x88, 0x00 },
329 { 0x89, 0x00 },
330 { 0x76, 0x00 },
331 { 0xe7, 0x2e },
332 { 0x31, 0xf9 },
333 { 0x25, 0x42 },
334 { 0x21, 0xf0 },
336 { 0x1c, 0x00 },
337 { 0x1d, 0x40 },
338 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
339 { 0x1d, 0x00 }, /* payload size */
341 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
342 { 0x1d, 0x58 }, /* frame size */
343 { 0x1d, 0x00 }, /* frame size */
345 { 0x1c, 0x0a },
346 { 0x1d, 0x08 }, /* turn on UVC header */
347 { 0x1d, 0x0e }, /* .. */
349 { 0x8d, 0x1c },
350 { 0x8e, 0x80 },
351 { 0xe5, 0x04 },
353 { 0xc0, 0x50 },
354 { 0xc1, 0x3c },
355 { 0xc2, 0x0c },
357 static const u8 sensor_init[][2] = {
358 { 0x12, 0x80 },
359 { 0x11, 0x01 },
360 /*fixme: better have a delay?*/
361 { 0x11, 0x01 },
362 { 0x11, 0x01 },
363 { 0x11, 0x01 },
364 { 0x11, 0x01 },
365 { 0x11, 0x01 },
366 { 0x11, 0x01 },
367 { 0x11, 0x01 },
368 { 0x11, 0x01 },
369 { 0x11, 0x01 },
370 { 0x11, 0x01 },
372 { 0x3d, 0x03 },
373 { 0x17, 0x26 },
374 { 0x18, 0xa0 },
375 { 0x19, 0x07 },
376 { 0x1a, 0xf0 },
377 { 0x32, 0x00 },
378 { 0x29, 0xa0 },
379 { 0x2c, 0xf0 },
380 { 0x65, 0x20 },
381 { 0x11, 0x01 },
382 { 0x42, 0x7f },
383 { 0x63, 0xaa }, /* AWB - was e0 */
384 { 0x64, 0xff },
385 { 0x66, 0x00 },
386 { 0x13, 0xf0 }, /* com8 */
387 { 0x0d, 0x41 },
388 { 0x0f, 0xc5 },
389 { 0x14, 0x11 },
391 { 0x22, 0x7f },
392 { 0x23, 0x03 },
393 { 0x24, 0x40 },
394 { 0x25, 0x30 },
395 { 0x26, 0xa1 },
396 { 0x2a, 0x00 },
397 { 0x2b, 0x00 },
398 { 0x6b, 0xaa },
399 { 0x13, 0xff }, /* AWB */
401 { 0x90, 0x05 },
402 { 0x91, 0x01 },
403 { 0x92, 0x03 },
404 { 0x93, 0x00 },
405 { 0x94, 0x60 },
406 { 0x95, 0x3c },
407 { 0x96, 0x24 },
408 { 0x97, 0x1e },
409 { 0x98, 0x62 },
410 { 0x99, 0x80 },
411 { 0x9a, 0x1e },
412 { 0x9b, 0x08 },
413 { 0x9c, 0x20 },
414 { 0x9e, 0x81 },
416 { 0xa6, 0x04 },
417 { 0x7e, 0x0c },
418 { 0x7f, 0x16 },
419 { 0x80, 0x2a },
420 { 0x81, 0x4e },
421 { 0x82, 0x61 },
422 { 0x83, 0x6f },
423 { 0x84, 0x7b },
424 { 0x85, 0x86 },
425 { 0x86, 0x8e },
426 { 0x87, 0x97 },
427 { 0x88, 0xa4 },
428 { 0x89, 0xaf },
429 { 0x8a, 0xc5 },
430 { 0x8b, 0xd7 },
431 { 0x8c, 0xe8 },
432 { 0x8d, 0x20 },
434 { 0x0c, 0x90 },
436 { 0x2b, 0x00 },
437 { 0x22, 0x7f },
438 { 0x23, 0x03 },
439 { 0x11, 0x01 },
440 { 0x0c, 0xd0 },
441 { 0x64, 0xff },
442 { 0x0d, 0x41 },
444 { 0x14, 0x41 },
445 { 0x0e, 0xcd },
446 { 0xac, 0xbf },
447 { 0x8e, 0x00 }, /* De-noise threshold */
448 { 0x0c, 0xd0 }
450 static const u8 bridge_start_vga[][2] = {
451 {0x1c, 0x00},
452 {0x1d, 0x40},
453 {0x1d, 0x02},
454 {0x1d, 0x00},
455 {0x1d, 0x02},
456 {0x1d, 0x58},
457 {0x1d, 0x00},
458 {0xc0, 0x50},
459 {0xc1, 0x3c},
461 static const u8 sensor_start_vga[][2] = {
462 {0x12, 0x00},
463 {0x17, 0x26},
464 {0x18, 0xa0},
465 {0x19, 0x07},
466 {0x1a, 0xf0},
467 {0x29, 0xa0},
468 {0x2c, 0xf0},
469 {0x65, 0x20},
471 static const u8 bridge_start_qvga[][2] = {
472 {0x1c, 0x00},
473 {0x1d, 0x40},
474 {0x1d, 0x02},
475 {0x1d, 0x00},
476 {0x1d, 0x01},
477 {0x1d, 0x4b},
478 {0x1d, 0x00},
479 {0xc0, 0x28},
480 {0xc1, 0x1e},
482 static const u8 sensor_start_qvga[][2] = {
483 {0x12, 0x40},
484 {0x17, 0x3f},
485 {0x18, 0x50},
486 {0x19, 0x03},
487 {0x1a, 0x78},
488 {0x29, 0x50},
489 {0x2c, 0x78},
490 {0x65, 0x2f},
493 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
495 struct usb_device *udev = gspca_dev->dev;
496 int ret;
498 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
499 gspca_dev->usb_buf[0] = val;
500 ret = usb_control_msg(udev,
501 usb_sndctrlpipe(udev, 0),
502 0x01,
503 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
504 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
505 if (ret < 0)
506 PDEBUG(D_ERR, "write failed");
509 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
511 struct usb_device *udev = gspca_dev->dev;
512 int ret;
514 ret = usb_control_msg(udev,
515 usb_rcvctrlpipe(udev, 0),
516 0x01,
517 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
518 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
519 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
520 if (ret < 0)
521 PDEBUG(D_ERR, "read failed");
522 return gspca_dev->usb_buf[0];
525 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
526 * (direction and output)? */
527 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
529 u8 data;
531 PDEBUG(D_CONF, "led status: %d", status);
533 data = ov534_reg_read(gspca_dev, 0x21);
534 data |= 0x80;
535 ov534_reg_write(gspca_dev, 0x21, data);
537 data = ov534_reg_read(gspca_dev, 0x23);
538 if (status)
539 data |= 0x80;
540 else
541 data &= ~0x80;
543 ov534_reg_write(gspca_dev, 0x23, data);
545 if (!status) {
546 data = ov534_reg_read(gspca_dev, 0x21);
547 data &= ~0x80;
548 ov534_reg_write(gspca_dev, 0x21, data);
552 static int sccb_check_status(struct gspca_dev *gspca_dev)
554 u8 data;
555 int i;
557 for (i = 0; i < 5; i++) {
558 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
560 switch (data) {
561 case 0x00:
562 return 1;
563 case 0x04:
564 return 0;
565 case 0x03:
566 break;
567 default:
568 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
569 data, i + 1);
572 return 0;
575 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
577 PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
578 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
579 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
580 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
582 if (!sccb_check_status(gspca_dev))
583 PDEBUG(D_ERR, "sccb_reg_write failed");
586 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
588 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
589 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
590 if (!sccb_check_status(gspca_dev))
591 PDEBUG(D_ERR, "sccb_reg_read failed 1");
593 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
594 if (!sccb_check_status(gspca_dev))
595 PDEBUG(D_ERR, "sccb_reg_read failed 2");
597 return ov534_reg_read(gspca_dev, OV534_REG_READ);
600 /* output a bridge sequence (reg - val) */
601 static void reg_w_array(struct gspca_dev *gspca_dev,
602 const u8 (*data)[2], int len)
604 while (--len >= 0) {
605 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
606 data++;
610 /* output a sensor sequence (reg - val) */
611 static void sccb_w_array(struct gspca_dev *gspca_dev,
612 const u8 (*data)[2], int len)
614 while (--len >= 0) {
615 if ((*data)[0] != 0xff) {
616 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
617 } else {
618 sccb_reg_read(gspca_dev, (*data)[1]);
619 sccb_reg_write(gspca_dev, 0xff, 0x00);
621 data++;
625 /* ov772x specific controls */
626 static void set_frame_rate(struct gspca_dev *gspca_dev)
628 struct sd *sd = (struct sd *) gspca_dev;
629 int i;
630 struct rate_s {
631 u8 fps;
632 u8 r11;
633 u8 r0d;
634 u8 re5;
636 const struct rate_s *r;
637 static const struct rate_s rate_0[] = { /* 640x480 */
638 {60, 0x01, 0xc1, 0x04},
639 {50, 0x01, 0x41, 0x02},
640 {40, 0x02, 0xc1, 0x04},
641 {30, 0x04, 0x81, 0x02},
642 {15, 0x03, 0x41, 0x04},
644 static const struct rate_s rate_1[] = { /* 320x240 */
645 {125, 0x02, 0x81, 0x02},
646 {100, 0x02, 0xc1, 0x04},
647 {75, 0x03, 0xc1, 0x04},
648 {60, 0x04, 0xc1, 0x04},
649 {50, 0x02, 0x41, 0x04},
650 {40, 0x03, 0x41, 0x04},
651 {30, 0x04, 0x41, 0x04},
654 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
655 r = rate_0;
656 i = ARRAY_SIZE(rate_0);
657 } else {
658 r = rate_1;
659 i = ARRAY_SIZE(rate_1);
661 while (--i > 0) {
662 if (sd->frame_rate >= r->fps)
663 break;
664 r++;
667 sccb_reg_write(gspca_dev, 0x11, r->r11);
668 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
669 ov534_reg_write(gspca_dev, 0xe5, r->re5);
671 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
674 static void setbrightness(struct gspca_dev *gspca_dev)
676 struct sd *sd = (struct sd *) gspca_dev;
678 sccb_reg_write(gspca_dev, 0x9B, sd->brightness);
681 static void setcontrast(struct gspca_dev *gspca_dev)
683 struct sd *sd = (struct sd *) gspca_dev;
685 sccb_reg_write(gspca_dev, 0x9C, sd->contrast);
688 static void setgain(struct gspca_dev *gspca_dev)
690 struct sd *sd = (struct sd *) gspca_dev;
691 u8 val;
693 val = sd->gain;
694 switch (val & 0x30) {
695 case 0x00:
696 val &= 0x0f;
697 break;
698 case 0x10:
699 val &= 0x0f;
700 val |= 0x30;
701 break;
702 case 0x20:
703 val &= 0x0f;
704 val |= 0x70;
705 break;
706 default:
707 /* case 0x30: */
708 val &= 0x0f;
709 val |= 0xf0;
710 break;
712 sccb_reg_write(gspca_dev, 0x00, val);
715 static void setexposure(struct gspca_dev *gspca_dev)
717 struct sd *sd = (struct sd *) gspca_dev;
718 u8 val;
720 val = sd->exposure;
721 sccb_reg_write(gspca_dev, 0x08, val >> 7);
722 sccb_reg_write(gspca_dev, 0x10, val << 1);
725 static void setredblc(struct gspca_dev *gspca_dev)
727 struct sd *sd = (struct sd *) gspca_dev;
729 sccb_reg_write(gspca_dev, 0x43, sd->redblc);
732 static void setblueblc(struct gspca_dev *gspca_dev)
734 struct sd *sd = (struct sd *) gspca_dev;
736 sccb_reg_write(gspca_dev, 0x42, sd->blueblc);
739 static void sethue(struct gspca_dev *gspca_dev)
741 struct sd *sd = (struct sd *) gspca_dev;
743 sccb_reg_write(gspca_dev, 0x01, sd->hue);
746 static void setautogain(struct gspca_dev *gspca_dev)
748 struct sd *sd = (struct sd *) gspca_dev;
750 if (sd->autogain) {
751 sccb_reg_write(gspca_dev, 0x13, 0xf7); /* AGC,AEC,AWB ON */
752 sccb_reg_write(gspca_dev, 0x64,
753 sccb_reg_read(gspca_dev, 0x64) | 0x03);
754 } else {
755 sccb_reg_write(gspca_dev, 0x13, 0xf0); /* AGC,AEC,AWB OFF */
756 sccb_reg_write(gspca_dev, 0x64,
757 sccb_reg_read(gspca_dev, 0x64) & 0xfc);
761 static void setawb(struct gspca_dev *gspca_dev)
763 struct sd *sd = (struct sd *) gspca_dev;
765 if (sd->awb)
766 sccb_reg_write(gspca_dev, 0x63, 0xe0); /* AWB on */
767 else
768 sccb_reg_write(gspca_dev, 0x63, 0xaa); /* AWB off */
771 static void setsharpness(struct gspca_dev *gspca_dev)
773 struct sd *sd = (struct sd *) gspca_dev;
774 u8 val;
776 val = sd->sharpness;
777 sccb_reg_write(gspca_dev, 0x91, val); /* vga noise */
778 sccb_reg_write(gspca_dev, 0x8e, val); /* qvga noise */
781 static void sethflip(struct gspca_dev *gspca_dev)
783 struct sd *sd = (struct sd *) gspca_dev;
785 if (sd->hflip == 0)
786 sccb_reg_write(gspca_dev, 0x0c,
787 sccb_reg_read(gspca_dev, 0x0c) | 0x40);
788 else
789 sccb_reg_write(gspca_dev, 0x0c,
790 sccb_reg_read(gspca_dev, 0x0c) & 0xbf);
793 static void setvflip(struct gspca_dev *gspca_dev)
795 struct sd *sd = (struct sd *) gspca_dev;
797 if (sd->vflip == 0)
798 sccb_reg_write(gspca_dev, 0x0c,
799 sccb_reg_read(gspca_dev, 0x0c) | 0x80);
800 else
801 sccb_reg_write(gspca_dev, 0x0c,
802 sccb_reg_read(gspca_dev, 0x0c) & 0x7f);
805 /* this function is called at probe time */
806 static int sd_config(struct gspca_dev *gspca_dev,
807 const struct usb_device_id *id)
809 struct sd *sd = (struct sd *) gspca_dev;
810 struct cam *cam;
812 cam = &gspca_dev->cam;
814 cam->cam_mode = ov772x_mode;
815 cam->nmodes = ARRAY_SIZE(ov772x_mode);
816 cam->mode_framerates = ov772x_framerates;
818 cam->bulk = 1;
819 cam->bulk_size = 16384;
820 cam->bulk_nurbs = 2;
822 sd->frame_rate = 30;
824 sd->brightness = BRIGHTNESS_DEF;
825 sd->contrast = CONTRAST_DEF;
826 sd->gain = GAIN_DEF;
827 sd->exposure = EXPO_DEF;
828 sd->redblc = RED_BALANCE_DEF;
829 sd->blueblc = BLUE_BALANCE_DEF;
830 sd->hue = HUE_DEF;
831 #if AUTOGAIN_DEF != 0
832 sd->autogain = AUTOGAIN_DEF;
833 #else
834 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
835 #endif
836 #if AWB_DEF != 0
837 sd->awb = AWB_DEF
838 #endif
839 #if SHARPNESS_DEF != 0
840 sd->sharpness = SHARPNESS_DEF;
841 #endif
842 #if HFLIP_DEF != 0
843 sd->hflip = HFLIP_DEF;
844 #endif
845 #if VFLIP_DEF != 0
846 sd->vflip = VFLIP_DEF;
847 #endif
849 return 0;
852 /* this function is called at probe and resume time */
853 static int sd_init(struct gspca_dev *gspca_dev)
855 u16 sensor_id;
857 /* reset bridge */
858 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
859 ov534_reg_write(gspca_dev, 0xe0, 0x08);
860 msleep(100);
862 /* initialize the sensor address */
863 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
865 /* reset sensor */
866 sccb_reg_write(gspca_dev, 0x12, 0x80);
867 msleep(10);
869 /* probe the sensor */
870 sccb_reg_read(gspca_dev, 0x0a);
871 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
872 sccb_reg_read(gspca_dev, 0x0b);
873 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
874 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
876 /* initialize */
877 reg_w_array(gspca_dev, bridge_init,
878 ARRAY_SIZE(bridge_init));
879 ov534_set_led(gspca_dev, 1);
880 sccb_w_array(gspca_dev, sensor_init,
881 ARRAY_SIZE(sensor_init));
882 ov534_reg_write(gspca_dev, 0xe0, 0x09);
883 ov534_set_led(gspca_dev, 0);
884 set_frame_rate(gspca_dev);
886 return 0;
889 static int sd_start(struct gspca_dev *gspca_dev)
891 int mode;
893 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
894 if (mode != 0) { /* 320x240 */
895 reg_w_array(gspca_dev, bridge_start_qvga,
896 ARRAY_SIZE(bridge_start_qvga));
897 sccb_w_array(gspca_dev, sensor_start_qvga,
898 ARRAY_SIZE(sensor_start_qvga));
899 } else { /* 640x480 */
900 reg_w_array(gspca_dev, bridge_start_vga,
901 ARRAY_SIZE(bridge_start_vga));
902 sccb_w_array(gspca_dev, sensor_start_vga,
903 ARRAY_SIZE(sensor_start_vga));
905 set_frame_rate(gspca_dev);
907 setautogain(gspca_dev);
908 setawb(gspca_dev);
909 setgain(gspca_dev);
910 setredblc(gspca_dev);
911 setblueblc(gspca_dev);
912 sethue(gspca_dev);
913 setexposure(gspca_dev);
914 setbrightness(gspca_dev);
915 setcontrast(gspca_dev);
916 setsharpness(gspca_dev);
917 setvflip(gspca_dev);
918 sethflip(gspca_dev);
920 ov534_set_led(gspca_dev, 1);
921 ov534_reg_write(gspca_dev, 0xe0, 0x00);
922 return 0;
925 static void sd_stopN(struct gspca_dev *gspca_dev)
927 ov534_reg_write(gspca_dev, 0xe0, 0x09);
928 ov534_set_led(gspca_dev, 0);
931 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
932 #define UVC_STREAM_EOH (1 << 7)
933 #define UVC_STREAM_ERR (1 << 6)
934 #define UVC_STREAM_STI (1 << 5)
935 #define UVC_STREAM_RES (1 << 4)
936 #define UVC_STREAM_SCR (1 << 3)
937 #define UVC_STREAM_PTS (1 << 2)
938 #define UVC_STREAM_EOF (1 << 1)
939 #define UVC_STREAM_FID (1 << 0)
941 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
942 u8 *data, int len)
944 struct sd *sd = (struct sd *) gspca_dev;
945 __u32 this_pts;
946 u16 this_fid;
947 int remaining_len = len;
949 do {
950 len = min(remaining_len, 2048);
952 /* Payloads are prefixed with a UVC-style header. We
953 consider a frame to start when the FID toggles, or the PTS
954 changes. A frame ends when EOF is set, and we've received
955 the correct number of bytes. */
957 /* Verify UVC header. Header length is always 12 */
958 if (data[0] != 12 || len < 12) {
959 PDEBUG(D_PACK, "bad header");
960 goto discard;
963 /* Check errors */
964 if (data[1] & UVC_STREAM_ERR) {
965 PDEBUG(D_PACK, "payload error");
966 goto discard;
969 /* Extract PTS and FID */
970 if (!(data[1] & UVC_STREAM_PTS)) {
971 PDEBUG(D_PACK, "PTS not present");
972 goto discard;
974 this_pts = (data[5] << 24) | (data[4] << 16)
975 | (data[3] << 8) | data[2];
976 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
978 /* If PTS or FID has changed, start a new frame. */
979 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
980 if (gspca_dev->last_packet_type == INTER_PACKET)
981 gspca_frame_add(gspca_dev, LAST_PACKET,
982 NULL, 0);
983 sd->last_pts = this_pts;
984 sd->last_fid = this_fid;
985 gspca_frame_add(gspca_dev, FIRST_PACKET,
986 data + 12, len - 12);
987 /* If this packet is marked as EOF, end the frame */
988 } else if (data[1] & UVC_STREAM_EOF) {
989 struct gspca_frame *frame;
991 sd->last_pts = 0;
992 frame = gspca_get_i_frame(gspca_dev);
993 if (frame == NULL)
994 goto discard;
995 if (frame->data_end - frame->data + (len - 12) !=
996 gspca_dev->width * gspca_dev->height * 2) {
997 PDEBUG(D_PACK, "wrong sized frame");
998 goto discard;
1000 gspca_frame_add(gspca_dev, LAST_PACKET,
1001 data + 12, len - 12);
1002 } else {
1004 /* Add the data from this payload */
1005 gspca_frame_add(gspca_dev, INTER_PACKET,
1006 data + 12, len - 12);
1009 /* Done this payload */
1010 goto scan_next;
1012 discard:
1013 /* Discard data until a new frame starts. */
1014 gspca_dev->last_packet_type = DISCARD_PACKET;
1016 scan_next:
1017 remaining_len -= len;
1018 data += len;
1019 } while (remaining_len > 0);
1022 /* controls */
1023 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1025 struct sd *sd = (struct sd *) gspca_dev;
1027 sd->gain = val;
1028 if (gspca_dev->streaming)
1029 setgain(gspca_dev);
1030 return 0;
1033 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1035 struct sd *sd = (struct sd *) gspca_dev;
1037 *val = sd->gain;
1038 return 0;
1041 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1043 struct sd *sd = (struct sd *) gspca_dev;
1045 sd->exposure = val;
1046 if (gspca_dev->streaming)
1047 setexposure(gspca_dev);
1048 return 0;
1051 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1053 struct sd *sd = (struct sd *) gspca_dev;
1055 *val = sd->exposure;
1056 return 0;
1059 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1061 struct sd *sd = (struct sd *) gspca_dev;
1063 sd->brightness = val;
1064 if (gspca_dev->streaming)
1065 setbrightness(gspca_dev);
1066 return 0;
1069 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1071 struct sd *sd = (struct sd *) gspca_dev;
1073 *val = sd->brightness;
1074 return 0;
1077 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1079 struct sd *sd = (struct sd *) gspca_dev;
1081 sd->contrast = val;
1082 if (gspca_dev->streaming)
1083 setcontrast(gspca_dev);
1084 return 0;
1087 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1089 struct sd *sd = (struct sd *) gspca_dev;
1091 *val = sd->contrast;
1092 return 0;
1095 static int sd_setredblc(struct gspca_dev *gspca_dev, __s32 val)
1097 struct sd *sd = (struct sd *) gspca_dev;
1099 sd->redblc = val;
1100 if (gspca_dev->streaming)
1101 setredblc(gspca_dev);
1102 return 0;
1105 static int sd_getredblc(struct gspca_dev *gspca_dev, __s32 *val)
1107 struct sd *sd = (struct sd *) gspca_dev;
1109 *val = sd->redblc;
1110 return 0;
1113 static int sd_setblueblc(struct gspca_dev *gspca_dev, __s32 val)
1115 struct sd *sd = (struct sd *) gspca_dev;
1117 sd->blueblc = val;
1118 if (gspca_dev->streaming)
1119 setblueblc(gspca_dev);
1120 return 0;
1123 static int sd_getblueblc(struct gspca_dev *gspca_dev, __s32 *val)
1125 struct sd *sd = (struct sd *) gspca_dev;
1127 *val = sd->blueblc;
1128 return 0;
1131 static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
1133 struct sd *sd = (struct sd *) gspca_dev;
1135 sd->hue = val;
1136 if (gspca_dev->streaming)
1137 sethue(gspca_dev);
1138 return 0;
1141 static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
1143 struct sd *sd = (struct sd *) gspca_dev;
1145 *val = sd->hue;
1146 return 0;
1149 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1151 struct sd *sd = (struct sd *) gspca_dev;
1153 sd->autogain = val;
1155 if (gspca_dev->streaming) {
1157 /* the auto white balance control works only
1158 * when auto gain is set */
1159 if (val)
1160 gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
1161 else
1162 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
1163 setautogain(gspca_dev);
1165 return 0;
1168 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1170 struct sd *sd = (struct sd *) gspca_dev;
1172 *val = sd->autogain;
1173 return 0;
1176 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
1178 struct sd *sd = (struct sd *) gspca_dev;
1180 sd->awb = val;
1181 if (gspca_dev->streaming)
1182 setawb(gspca_dev);
1183 return 0;
1186 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
1188 struct sd *sd = (struct sd *) gspca_dev;
1190 *val = sd->awb;
1191 return 0;
1194 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
1196 struct sd *sd = (struct sd *) gspca_dev;
1198 sd->sharpness = val;
1199 if (gspca_dev->streaming)
1200 setsharpness(gspca_dev);
1201 return 0;
1204 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
1206 struct sd *sd = (struct sd *) gspca_dev;
1208 *val = sd->sharpness;
1209 return 0;
1212 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1214 struct sd *sd = (struct sd *) gspca_dev;
1216 sd->hflip = val;
1217 if (gspca_dev->streaming)
1218 sethflip(gspca_dev);
1219 return 0;
1222 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1224 struct sd *sd = (struct sd *) gspca_dev;
1226 *val = sd->hflip;
1227 return 0;
1230 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1232 struct sd *sd = (struct sd *) gspca_dev;
1234 sd->vflip = val;
1235 if (gspca_dev->streaming)
1236 setvflip(gspca_dev);
1237 return 0;
1240 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1242 struct sd *sd = (struct sd *) gspca_dev;
1244 *val = sd->vflip;
1245 return 0;
1248 /* get stream parameters (framerate) */
1249 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
1250 struct v4l2_streamparm *parm)
1252 struct v4l2_captureparm *cp = &parm->parm.capture;
1253 struct v4l2_fract *tpf = &cp->timeperframe;
1254 struct sd *sd = (struct sd *) gspca_dev;
1256 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1257 return -EINVAL;
1259 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1260 tpf->numerator = 1;
1261 tpf->denominator = sd->frame_rate;
1263 return 0;
1266 /* set stream parameters (framerate) */
1267 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
1268 struct v4l2_streamparm *parm)
1270 struct v4l2_captureparm *cp = &parm->parm.capture;
1271 struct v4l2_fract *tpf = &cp->timeperframe;
1272 struct sd *sd = (struct sd *) gspca_dev;
1274 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1275 return -EINVAL;
1277 /* Set requested framerate */
1278 sd->frame_rate = tpf->denominator / tpf->numerator;
1279 if (gspca_dev->streaming)
1280 set_frame_rate(gspca_dev);
1282 /* Return the actual framerate */
1283 tpf->numerator = 1;
1284 tpf->denominator = sd->frame_rate;
1286 return 0;
1289 /* sub-driver description */
1290 static const struct sd_desc sd_desc = {
1291 .name = MODULE_NAME,
1292 .ctrls = sd_ctrls,
1293 .nctrls = ARRAY_SIZE(sd_ctrls),
1294 .config = sd_config,
1295 .init = sd_init,
1296 .start = sd_start,
1297 .stopN = sd_stopN,
1298 .pkt_scan = sd_pkt_scan,
1299 .get_streamparm = sd_get_streamparm,
1300 .set_streamparm = sd_set_streamparm,
1303 /* -- module initialisation -- */
1304 static const __devinitdata struct usb_device_id device_table[] = {
1305 {USB_DEVICE(0x1415, 0x2000)},
1309 MODULE_DEVICE_TABLE(usb, device_table);
1311 /* -- device connect -- */
1312 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1314 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1315 THIS_MODULE);
1318 static struct usb_driver sd_driver = {
1319 .name = MODULE_NAME,
1320 .id_table = device_table,
1321 .probe = sd_probe,
1322 .disconnect = gspca_disconnect,
1323 #ifdef CONFIG_PM
1324 .suspend = gspca_suspend,
1325 .resume = gspca_resume,
1326 #endif
1329 /* -- module insert / remove -- */
1330 static int __init sd_mod_init(void)
1332 int ret;
1334 ret = usb_register(&sd_driver);
1335 if (ret < 0)
1336 return ret;
1337 PDEBUG(D_PROBE, "registered");
1338 return 0;
1341 static void __exit sd_mod_exit(void)
1343 usb_deregister(&sd_driver);
1344 PDEBUG(D_PROBE, "deregistered");
1347 module_init(sd_mod_init);
1348 module_exit(sd_mod_exit);