GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / video / gspca / ov534.c
blob54e110a31409bca59f92acdf9cdd597955cd2dd8
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, awb, agc, aec controls
14 * added 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 agc;
64 u8 awb;
65 u8 aec;
66 s8 sharpness;
67 u8 hflip;
68 u8 vflip;
69 u8 freqfltr;
72 /* V4L2 controls supported by the driver */
73 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
74 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
75 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getagc(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
85 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val);
86 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val);
87 static int sd_setaec(struct gspca_dev *gspca_dev, __s32 val);
88 static int sd_getaec(struct gspca_dev *gspca_dev, __s32 *val);
89 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
93 static int sd_setfreqfltr(struct gspca_dev *gspca_dev, __s32 val);
94 static int sd_getfreqfltr(struct gspca_dev *gspca_dev, __s32 *val);
95 static int sd_querymenu(struct gspca_dev *gspca_dev,
96 struct v4l2_querymenu *menu);
98 static const struct ctrl sd_ctrls[] = {
99 { /* 0 */
101 .id = V4L2_CID_BRIGHTNESS,
102 .type = V4L2_CTRL_TYPE_INTEGER,
103 .name = "Brightness",
104 .minimum = 0,
105 .maximum = 255,
106 .step = 1,
107 #define BRIGHTNESS_DEF 0
108 .default_value = BRIGHTNESS_DEF,
110 .set = sd_setbrightness,
111 .get = sd_getbrightness,
113 { /* 1 */
115 .id = V4L2_CID_CONTRAST,
116 .type = V4L2_CTRL_TYPE_INTEGER,
117 .name = "Contrast",
118 .minimum = 0,
119 .maximum = 255,
120 .step = 1,
121 #define CONTRAST_DEF 32
122 .default_value = CONTRAST_DEF,
124 .set = sd_setcontrast,
125 .get = sd_getcontrast,
127 { /* 2 */
129 .id = V4L2_CID_GAIN,
130 .type = V4L2_CTRL_TYPE_INTEGER,
131 .name = "Main Gain",
132 .minimum = 0,
133 .maximum = 63,
134 .step = 1,
135 #define GAIN_DEF 20
136 .default_value = GAIN_DEF,
138 .set = sd_setgain,
139 .get = sd_getgain,
141 { /* 3 */
143 .id = V4L2_CID_EXPOSURE,
144 .type = V4L2_CTRL_TYPE_INTEGER,
145 .name = "Exposure",
146 .minimum = 0,
147 .maximum = 255,
148 .step = 1,
149 #define EXPO_DEF 120
150 .default_value = EXPO_DEF,
152 .set = sd_setexposure,
153 .get = sd_getexposure,
155 { /* 4 */
157 .id = V4L2_CID_AUTOGAIN,
158 .type = V4L2_CTRL_TYPE_BOOLEAN,
159 .name = "Auto Gain",
160 .minimum = 0,
161 .maximum = 1,
162 .step = 1,
163 #define AGC_DEF 1
164 .default_value = AGC_DEF,
166 .set = sd_setagc,
167 .get = sd_getagc,
169 #define AWB_IDX 5
170 { /* 5 */
172 .id = V4L2_CID_AUTO_WHITE_BALANCE,
173 .type = V4L2_CTRL_TYPE_BOOLEAN,
174 .name = "Auto White Balance",
175 .minimum = 0,
176 .maximum = 1,
177 .step = 1,
178 #define AWB_DEF 1
179 .default_value = AWB_DEF,
181 .set = sd_setawb,
182 .get = sd_getawb,
184 { /* 6 */
186 .id = V4L2_CID_EXPOSURE_AUTO,
187 .type = V4L2_CTRL_TYPE_BOOLEAN,
188 .name = "Auto Exposure",
189 .minimum = 0,
190 .maximum = 1,
191 .step = 1,
192 #define AEC_DEF 1
193 .default_value = AEC_DEF,
195 .set = sd_setaec,
196 .get = sd_getaec,
198 { /* 7 */
200 .id = V4L2_CID_SHARPNESS,
201 .type = V4L2_CTRL_TYPE_INTEGER,
202 .name = "Sharpness",
203 .minimum = 0,
204 .maximum = 63,
205 .step = 1,
206 #define SHARPNESS_DEF 0
207 .default_value = SHARPNESS_DEF,
209 .set = sd_setsharpness,
210 .get = sd_getsharpness,
212 { /* 8 */
214 .id = V4L2_CID_HFLIP,
215 .type = V4L2_CTRL_TYPE_BOOLEAN,
216 .name = "HFlip",
217 .minimum = 0,
218 .maximum = 1,
219 .step = 1,
220 #define HFLIP_DEF 0
221 .default_value = HFLIP_DEF,
223 .set = sd_sethflip,
224 .get = sd_gethflip,
226 { /* 9 */
228 .id = V4L2_CID_VFLIP,
229 .type = V4L2_CTRL_TYPE_BOOLEAN,
230 .name = "VFlip",
231 .minimum = 0,
232 .maximum = 1,
233 .step = 1,
234 #define VFLIP_DEF 0
235 .default_value = VFLIP_DEF,
237 .set = sd_setvflip,
238 .get = sd_getvflip,
240 { /* 10 */
242 .id = V4L2_CID_POWER_LINE_FREQUENCY,
243 .type = V4L2_CTRL_TYPE_MENU,
244 .name = "Light Frequency Filter",
245 .minimum = 0,
246 .maximum = 1,
247 .step = 1,
248 #define FREQFLTR_DEF 0
249 .default_value = FREQFLTR_DEF,
251 .set = sd_setfreqfltr,
252 .get = sd_getfreqfltr,
256 static const struct v4l2_pix_format ov772x_mode[] = {
257 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
258 .bytesperline = 320 * 2,
259 .sizeimage = 320 * 240 * 2,
260 .colorspace = V4L2_COLORSPACE_SRGB,
261 .priv = 1},
262 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
263 .bytesperline = 640 * 2,
264 .sizeimage = 640 * 480 * 2,
265 .colorspace = V4L2_COLORSPACE_SRGB,
266 .priv = 0},
269 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
270 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
272 static const struct framerates ov772x_framerates[] = {
273 { /* 320x240 */
274 .rates = qvga_rates,
275 .nrates = ARRAY_SIZE(qvga_rates),
277 { /* 640x480 */
278 .rates = vga_rates,
279 .nrates = ARRAY_SIZE(vga_rates),
283 static const u8 bridge_init[][2] = {
284 { 0xc2, 0x0c },
285 { 0x88, 0xf8 },
286 { 0xc3, 0x69 },
287 { 0x89, 0xff },
288 { 0x76, 0x03 },
289 { 0x92, 0x01 },
290 { 0x93, 0x18 },
291 { 0x94, 0x10 },
292 { 0x95, 0x10 },
293 { 0xe2, 0x00 },
294 { 0xe7, 0x3e },
296 { 0x96, 0x00 },
298 { 0x97, 0x20 },
299 { 0x97, 0x20 },
300 { 0x97, 0x20 },
301 { 0x97, 0x0a },
302 { 0x97, 0x3f },
303 { 0x97, 0x4a },
304 { 0x97, 0x20 },
305 { 0x97, 0x15 },
306 { 0x97, 0x0b },
308 { 0x8e, 0x40 },
309 { 0x1f, 0x81 },
310 { 0x34, 0x05 },
311 { 0xe3, 0x04 },
312 { 0x88, 0x00 },
313 { 0x89, 0x00 },
314 { 0x76, 0x00 },
315 { 0xe7, 0x2e },
316 { 0x31, 0xf9 },
317 { 0x25, 0x42 },
318 { 0x21, 0xf0 },
320 { 0x1c, 0x00 },
321 { 0x1d, 0x40 },
322 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
323 { 0x1d, 0x00 }, /* payload size */
325 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
326 { 0x1d, 0x58 }, /* frame size */
327 { 0x1d, 0x00 }, /* frame size */
329 { 0x1c, 0x0a },
330 { 0x1d, 0x08 }, /* turn on UVC header */
331 { 0x1d, 0x0e }, /* .. */
333 { 0x8d, 0x1c },
334 { 0x8e, 0x80 },
335 { 0xe5, 0x04 },
337 { 0xc0, 0x50 },
338 { 0xc1, 0x3c },
339 { 0xc2, 0x0c },
341 static const u8 sensor_init[][2] = {
342 { 0x12, 0x80 },
343 { 0x11, 0x01 },
344 { 0x11, 0x01 },
345 { 0x11, 0x01 },
346 { 0x11, 0x01 },
347 { 0x11, 0x01 },
348 { 0x11, 0x01 },
349 { 0x11, 0x01 },
350 { 0x11, 0x01 },
351 { 0x11, 0x01 },
352 { 0x11, 0x01 },
353 { 0x11, 0x01 },
355 { 0x3d, 0x03 },
356 { 0x17, 0x26 },
357 { 0x18, 0xa0 },
358 { 0x19, 0x07 },
359 { 0x1a, 0xf0 },
360 { 0x32, 0x00 },
361 { 0x29, 0xa0 },
362 { 0x2c, 0xf0 },
363 { 0x65, 0x20 },
364 { 0x11, 0x01 },
365 { 0x42, 0x7f },
366 { 0x63, 0xaa }, /* AWB - was e0 */
367 { 0x64, 0xff },
368 { 0x66, 0x00 },
369 { 0x13, 0xf0 }, /* com8 */
370 { 0x0d, 0x41 },
371 { 0x0f, 0xc5 },
372 { 0x14, 0x11 },
374 { 0x22, 0x7f },
375 { 0x23, 0x03 },
376 { 0x24, 0x40 },
377 { 0x25, 0x30 },
378 { 0x26, 0xa1 },
379 { 0x2a, 0x00 },
380 { 0x2b, 0x00 },
381 { 0x6b, 0xaa },
382 { 0x13, 0xff }, /* AWB */
384 { 0x90, 0x05 },
385 { 0x91, 0x01 },
386 { 0x92, 0x03 },
387 { 0x93, 0x00 },
388 { 0x94, 0x60 },
389 { 0x95, 0x3c },
390 { 0x96, 0x24 },
391 { 0x97, 0x1e },
392 { 0x98, 0x62 },
393 { 0x99, 0x80 },
394 { 0x9a, 0x1e },
395 { 0x9b, 0x08 },
396 { 0x9c, 0x20 },
397 { 0x9e, 0x81 },
399 { 0xa6, 0x04 },
400 { 0x7e, 0x0c },
401 { 0x7f, 0x16 },
402 { 0x80, 0x2a },
403 { 0x81, 0x4e },
404 { 0x82, 0x61 },
405 { 0x83, 0x6f },
406 { 0x84, 0x7b },
407 { 0x85, 0x86 },
408 { 0x86, 0x8e },
409 { 0x87, 0x97 },
410 { 0x88, 0xa4 },
411 { 0x89, 0xaf },
412 { 0x8a, 0xc5 },
413 { 0x8b, 0xd7 },
414 { 0x8c, 0xe8 },
415 { 0x8d, 0x20 },
417 { 0x0c, 0x90 },
419 { 0x2b, 0x00 },
420 { 0x22, 0x7f },
421 { 0x23, 0x03 },
422 { 0x11, 0x01 },
423 { 0x0c, 0xd0 },
424 { 0x64, 0xff },
425 { 0x0d, 0x41 },
427 { 0x14, 0x41 },
428 { 0x0e, 0xcd },
429 { 0xac, 0xbf },
430 { 0x8e, 0x00 }, /* De-noise threshold */
431 { 0x0c, 0xd0 }
433 static const u8 bridge_start_vga[][2] = {
434 {0x1c, 0x00},
435 {0x1d, 0x40},
436 {0x1d, 0x02},
437 {0x1d, 0x00},
438 {0x1d, 0x02},
439 {0x1d, 0x58},
440 {0x1d, 0x00},
441 {0xc0, 0x50},
442 {0xc1, 0x3c},
444 static const u8 sensor_start_vga[][2] = {
445 {0x12, 0x00},
446 {0x17, 0x26},
447 {0x18, 0xa0},
448 {0x19, 0x07},
449 {0x1a, 0xf0},
450 {0x29, 0xa0},
451 {0x2c, 0xf0},
452 {0x65, 0x20},
454 static const u8 bridge_start_qvga[][2] = {
455 {0x1c, 0x00},
456 {0x1d, 0x40},
457 {0x1d, 0x02},
458 {0x1d, 0x00},
459 {0x1d, 0x01},
460 {0x1d, 0x4b},
461 {0x1d, 0x00},
462 {0xc0, 0x28},
463 {0xc1, 0x1e},
465 static const u8 sensor_start_qvga[][2] = {
466 {0x12, 0x40},
467 {0x17, 0x3f},
468 {0x18, 0x50},
469 {0x19, 0x03},
470 {0x1a, 0x78},
471 {0x29, 0x50},
472 {0x2c, 0x78},
473 {0x65, 0x2f},
476 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
478 struct usb_device *udev = gspca_dev->dev;
479 int ret;
481 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
482 gspca_dev->usb_buf[0] = val;
483 ret = usb_control_msg(udev,
484 usb_sndctrlpipe(udev, 0),
485 0x01,
486 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
487 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
488 if (ret < 0)
489 PDEBUG(D_ERR, "write failed");
492 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
494 struct usb_device *udev = gspca_dev->dev;
495 int ret;
497 ret = usb_control_msg(udev,
498 usb_rcvctrlpipe(udev, 0),
499 0x01,
500 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
501 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
502 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
503 if (ret < 0)
504 PDEBUG(D_ERR, "read failed");
505 return gspca_dev->usb_buf[0];
508 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
509 * (direction and output)? */
510 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
512 u8 data;
514 PDEBUG(D_CONF, "led status: %d", status);
516 data = ov534_reg_read(gspca_dev, 0x21);
517 data |= 0x80;
518 ov534_reg_write(gspca_dev, 0x21, data);
520 data = ov534_reg_read(gspca_dev, 0x23);
521 if (status)
522 data |= 0x80;
523 else
524 data &= ~0x80;
526 ov534_reg_write(gspca_dev, 0x23, data);
528 if (!status) {
529 data = ov534_reg_read(gspca_dev, 0x21);
530 data &= ~0x80;
531 ov534_reg_write(gspca_dev, 0x21, data);
535 static int sccb_check_status(struct gspca_dev *gspca_dev)
537 u8 data;
538 int i;
540 for (i = 0; i < 5; i++) {
541 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
543 switch (data) {
544 case 0x00:
545 return 1;
546 case 0x04:
547 return 0;
548 case 0x03:
549 break;
550 default:
551 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
552 data, i + 1);
555 return 0;
558 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
560 PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
561 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
562 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
563 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
565 if (!sccb_check_status(gspca_dev))
566 PDEBUG(D_ERR, "sccb_reg_write failed");
569 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
571 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
572 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
573 if (!sccb_check_status(gspca_dev))
574 PDEBUG(D_ERR, "sccb_reg_read failed 1");
576 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
577 if (!sccb_check_status(gspca_dev))
578 PDEBUG(D_ERR, "sccb_reg_read failed 2");
580 return ov534_reg_read(gspca_dev, OV534_REG_READ);
583 /* output a bridge sequence (reg - val) */
584 static void reg_w_array(struct gspca_dev *gspca_dev,
585 const u8 (*data)[2], int len)
587 while (--len >= 0) {
588 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
589 data++;
593 /* output a sensor sequence (reg - val) */
594 static void sccb_w_array(struct gspca_dev *gspca_dev,
595 const u8 (*data)[2], int len)
597 while (--len >= 0) {
598 if ((*data)[0] != 0xff) {
599 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
600 } else {
601 sccb_reg_read(gspca_dev, (*data)[1]);
602 sccb_reg_write(gspca_dev, 0xff, 0x00);
604 data++;
608 /* ov772x specific controls */
609 static void set_frame_rate(struct gspca_dev *gspca_dev)
611 struct sd *sd = (struct sd *) gspca_dev;
612 int i;
613 struct rate_s {
614 u8 fps;
615 u8 r11;
616 u8 r0d;
617 u8 re5;
619 const struct rate_s *r;
620 static const struct rate_s rate_0[] = { /* 640x480 */
621 {60, 0x01, 0xc1, 0x04},
622 {50, 0x01, 0x41, 0x02},
623 {40, 0x02, 0xc1, 0x04},
624 {30, 0x04, 0x81, 0x02},
625 {15, 0x03, 0x41, 0x04},
627 static const struct rate_s rate_1[] = { /* 320x240 */
628 {125, 0x02, 0x81, 0x02},
629 {100, 0x02, 0xc1, 0x04},
630 {75, 0x03, 0xc1, 0x04},
631 {60, 0x04, 0xc1, 0x04},
632 {50, 0x02, 0x41, 0x04},
633 {40, 0x03, 0x41, 0x04},
634 {30, 0x04, 0x41, 0x04},
637 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
638 r = rate_0;
639 i = ARRAY_SIZE(rate_0);
640 } else {
641 r = rate_1;
642 i = ARRAY_SIZE(rate_1);
644 while (--i > 0) {
645 if (sd->frame_rate >= r->fps)
646 break;
647 r++;
650 sccb_reg_write(gspca_dev, 0x11, r->r11);
651 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
652 ov534_reg_write(gspca_dev, 0xe5, r->re5);
654 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
657 static void setbrightness(struct gspca_dev *gspca_dev)
659 struct sd *sd = (struct sd *) gspca_dev;
661 sccb_reg_write(gspca_dev, 0x9b, sd->brightness);
664 static void setcontrast(struct gspca_dev *gspca_dev)
666 struct sd *sd = (struct sd *) gspca_dev;
668 sccb_reg_write(gspca_dev, 0x9c, sd->contrast);
671 static void setgain(struct gspca_dev *gspca_dev)
673 struct sd *sd = (struct sd *) gspca_dev;
674 u8 val;
676 if (sd->agc)
677 return;
679 val = sd->gain;
680 switch (val & 0x30) {
681 case 0x00:
682 val &= 0x0f;
683 break;
684 case 0x10:
685 val &= 0x0f;
686 val |= 0x30;
687 break;
688 case 0x20:
689 val &= 0x0f;
690 val |= 0x70;
691 break;
692 default:
693 /* case 0x30: */
694 val &= 0x0f;
695 val |= 0xf0;
696 break;
698 sccb_reg_write(gspca_dev, 0x00, val);
701 static void setexposure(struct gspca_dev *gspca_dev)
703 struct sd *sd = (struct sd *) gspca_dev;
704 u8 val;
706 if (sd->aec)
707 return;
709 /* 'val' is one byte and represents half of the exposure value we are
710 * going to set into registers, a two bytes value:
712 * MSB: ((u16) val << 1) >> 8 == val >> 7
713 * LSB: ((u16) val << 1) & 0xff == val << 1
715 val = sd->exposure;
716 sccb_reg_write(gspca_dev, 0x08, val >> 7);
717 sccb_reg_write(gspca_dev, 0x10, val << 1);
720 static void setagc(struct gspca_dev *gspca_dev)
722 struct sd *sd = (struct sd *) gspca_dev;
724 if (sd->agc) {
725 sccb_reg_write(gspca_dev, 0x13,
726 sccb_reg_read(gspca_dev, 0x13) | 0x04);
727 sccb_reg_write(gspca_dev, 0x64,
728 sccb_reg_read(gspca_dev, 0x64) | 0x03);
729 } else {
730 sccb_reg_write(gspca_dev, 0x13,
731 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
732 sccb_reg_write(gspca_dev, 0x64,
733 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
735 setgain(gspca_dev);
739 static void setawb(struct gspca_dev *gspca_dev)
741 struct sd *sd = (struct sd *) gspca_dev;
743 if (sd->awb) {
744 sccb_reg_write(gspca_dev, 0x13,
745 sccb_reg_read(gspca_dev, 0x13) | 0x02);
746 sccb_reg_write(gspca_dev, 0x63,
747 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
748 } else {
749 sccb_reg_write(gspca_dev, 0x13,
750 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
751 sccb_reg_write(gspca_dev, 0x63,
752 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
756 static void setaec(struct gspca_dev *gspca_dev)
758 struct sd *sd = (struct sd *) gspca_dev;
760 if (sd->aec)
761 sccb_reg_write(gspca_dev, 0x13,
762 sccb_reg_read(gspca_dev, 0x13) | 0x01);
763 else {
764 sccb_reg_write(gspca_dev, 0x13,
765 sccb_reg_read(gspca_dev, 0x13) & ~0x01);
766 setexposure(gspca_dev);
770 static void setsharpness(struct gspca_dev *gspca_dev)
772 struct sd *sd = (struct sd *) gspca_dev;
773 u8 val;
775 val = sd->sharpness;
776 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
777 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
780 static void sethflip(struct gspca_dev *gspca_dev)
782 struct sd *sd = (struct sd *) gspca_dev;
784 if (sd->hflip == 0)
785 sccb_reg_write(gspca_dev, 0x0c,
786 sccb_reg_read(gspca_dev, 0x0c) | 0x40);
787 else
788 sccb_reg_write(gspca_dev, 0x0c,
789 sccb_reg_read(gspca_dev, 0x0c) & ~0x40);
792 static void setvflip(struct gspca_dev *gspca_dev)
794 struct sd *sd = (struct sd *) gspca_dev;
796 if (sd->vflip == 0)
797 sccb_reg_write(gspca_dev, 0x0c,
798 sccb_reg_read(gspca_dev, 0x0c) | 0x80);
799 else
800 sccb_reg_write(gspca_dev, 0x0c,
801 sccb_reg_read(gspca_dev, 0x0c) & ~0x80);
804 static void setfreqfltr(struct gspca_dev *gspca_dev)
806 struct sd *sd = (struct sd *) gspca_dev;
808 if (sd->freqfltr == 0)
809 sccb_reg_write(gspca_dev, 0x2b, 0x00);
810 else
811 sccb_reg_write(gspca_dev, 0x2b, 0x9e);
815 /* this function is called at probe time */
816 static int sd_config(struct gspca_dev *gspca_dev,
817 const struct usb_device_id *id)
819 struct sd *sd = (struct sd *) gspca_dev;
820 struct cam *cam;
822 cam = &gspca_dev->cam;
824 cam->cam_mode = ov772x_mode;
825 cam->nmodes = ARRAY_SIZE(ov772x_mode);
826 cam->mode_framerates = ov772x_framerates;
828 cam->bulk = 1;
829 cam->bulk_size = 16384;
830 cam->bulk_nurbs = 2;
832 sd->frame_rate = 30;
834 sd->brightness = BRIGHTNESS_DEF;
835 sd->contrast = CONTRAST_DEF;
836 sd->gain = GAIN_DEF;
837 sd->exposure = EXPO_DEF;
838 #if AGC_DEF != 0
839 sd->agc = AGC_DEF;
840 #else
841 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
842 #endif
843 sd->awb = AWB_DEF;
844 sd->aec = AEC_DEF;
845 sd->sharpness = SHARPNESS_DEF;
846 sd->hflip = HFLIP_DEF;
847 sd->vflip = VFLIP_DEF;
848 sd->freqfltr = FREQFLTR_DEF;
850 return 0;
853 /* this function is called at probe and resume time */
854 static int sd_init(struct gspca_dev *gspca_dev)
856 u16 sensor_id;
858 /* reset bridge */
859 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
860 ov534_reg_write(gspca_dev, 0xe0, 0x08);
861 msleep(100);
863 /* initialize the sensor address */
864 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
866 /* reset sensor */
867 sccb_reg_write(gspca_dev, 0x12, 0x80);
868 msleep(10);
870 /* probe the sensor */
871 sccb_reg_read(gspca_dev, 0x0a);
872 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
873 sccb_reg_read(gspca_dev, 0x0b);
874 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
875 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
877 /* initialize */
878 reg_w_array(gspca_dev, bridge_init,
879 ARRAY_SIZE(bridge_init));
880 ov534_set_led(gspca_dev, 1);
881 sccb_w_array(gspca_dev, sensor_init,
882 ARRAY_SIZE(sensor_init));
883 ov534_reg_write(gspca_dev, 0xe0, 0x09);
884 ov534_set_led(gspca_dev, 0);
885 set_frame_rate(gspca_dev);
887 return 0;
890 static int sd_start(struct gspca_dev *gspca_dev)
892 int mode;
894 mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
895 if (mode != 0) { /* 320x240 */
896 reg_w_array(gspca_dev, bridge_start_qvga,
897 ARRAY_SIZE(bridge_start_qvga));
898 sccb_w_array(gspca_dev, sensor_start_qvga,
899 ARRAY_SIZE(sensor_start_qvga));
900 } else { /* 640x480 */
901 reg_w_array(gspca_dev, bridge_start_vga,
902 ARRAY_SIZE(bridge_start_vga));
903 sccb_w_array(gspca_dev, sensor_start_vga,
904 ARRAY_SIZE(sensor_start_vga));
906 set_frame_rate(gspca_dev);
908 setagc(gspca_dev);
909 setawb(gspca_dev);
910 setaec(gspca_dev);
911 setgain(gspca_dev);
912 setexposure(gspca_dev);
913 setbrightness(gspca_dev);
914 setcontrast(gspca_dev);
915 setsharpness(gspca_dev);
916 setvflip(gspca_dev);
917 sethflip(gspca_dev);
918 setfreqfltr(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 sd->last_pts = 0;
990 if (gspca_dev->image_len + len - 12 !=
991 gspca_dev->width * gspca_dev->height * 2) {
992 PDEBUG(D_PACK, "wrong sized frame");
993 goto discard;
995 gspca_frame_add(gspca_dev, LAST_PACKET,
996 data + 12, len - 12);
997 } else {
999 /* Add the data from this payload */
1000 gspca_frame_add(gspca_dev, INTER_PACKET,
1001 data + 12, len - 12);
1004 /* Done this payload */
1005 goto scan_next;
1007 discard:
1008 /* Discard data until a new frame starts. */
1009 gspca_dev->last_packet_type = DISCARD_PACKET;
1011 scan_next:
1012 remaining_len -= len;
1013 data += len;
1014 } while (remaining_len > 0);
1017 /* controls */
1018 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1020 struct sd *sd = (struct sd *) gspca_dev;
1022 sd->gain = val;
1023 if (gspca_dev->streaming)
1024 setgain(gspca_dev);
1025 return 0;
1028 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1030 struct sd *sd = (struct sd *) gspca_dev;
1032 *val = sd->gain;
1033 return 0;
1036 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1038 struct sd *sd = (struct sd *) gspca_dev;
1040 sd->exposure = val;
1041 if (gspca_dev->streaming)
1042 setexposure(gspca_dev);
1043 return 0;
1046 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1048 struct sd *sd = (struct sd *) gspca_dev;
1050 *val = sd->exposure;
1051 return 0;
1054 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1056 struct sd *sd = (struct sd *) gspca_dev;
1058 sd->brightness = val;
1059 if (gspca_dev->streaming)
1060 setbrightness(gspca_dev);
1061 return 0;
1064 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1066 struct sd *sd = (struct sd *) gspca_dev;
1068 *val = sd->brightness;
1069 return 0;
1072 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
1074 struct sd *sd = (struct sd *) gspca_dev;
1076 sd->contrast = val;
1077 if (gspca_dev->streaming)
1078 setcontrast(gspca_dev);
1079 return 0;
1082 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1084 struct sd *sd = (struct sd *) gspca_dev;
1086 *val = sd->contrast;
1087 return 0;
1090 static int sd_setagc(struct gspca_dev *gspca_dev, __s32 val)
1092 struct sd *sd = (struct sd *) gspca_dev;
1094 sd->agc = val;
1096 if (gspca_dev->streaming) {
1098 /* the auto white balance control works only
1099 * when auto gain is set */
1100 if (val)
1101 gspca_dev->ctrl_inac &= ~(1 << AWB_IDX);
1102 else
1103 gspca_dev->ctrl_inac |= (1 << AWB_IDX);
1104 setagc(gspca_dev);
1106 return 0;
1109 static int sd_getagc(struct gspca_dev *gspca_dev, __s32 *val)
1111 struct sd *sd = (struct sd *) gspca_dev;
1113 *val = sd->agc;
1114 return 0;
1117 static int sd_setawb(struct gspca_dev *gspca_dev, __s32 val)
1119 struct sd *sd = (struct sd *) gspca_dev;
1121 sd->awb = val;
1122 if (gspca_dev->streaming)
1123 setawb(gspca_dev);
1124 return 0;
1127 static int sd_getawb(struct gspca_dev *gspca_dev, __s32 *val)
1129 struct sd *sd = (struct sd *) gspca_dev;
1131 *val = sd->awb;
1132 return 0;
1135 static int sd_setaec(struct gspca_dev *gspca_dev, __s32 val)
1137 struct sd *sd = (struct sd *) gspca_dev;
1139 sd->aec = val;
1140 if (gspca_dev->streaming)
1141 setaec(gspca_dev);
1142 return 0;
1145 static int sd_getaec(struct gspca_dev *gspca_dev, __s32 *val)
1147 struct sd *sd = (struct sd *) gspca_dev;
1149 *val = sd->aec;
1150 return 0;
1153 static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
1155 struct sd *sd = (struct sd *) gspca_dev;
1157 sd->sharpness = val;
1158 if (gspca_dev->streaming)
1159 setsharpness(gspca_dev);
1160 return 0;
1163 static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
1165 struct sd *sd = (struct sd *) gspca_dev;
1167 *val = sd->sharpness;
1168 return 0;
1171 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
1173 struct sd *sd = (struct sd *) gspca_dev;
1175 sd->hflip = val;
1176 if (gspca_dev->streaming)
1177 sethflip(gspca_dev);
1178 return 0;
1181 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
1183 struct sd *sd = (struct sd *) gspca_dev;
1185 *val = sd->hflip;
1186 return 0;
1189 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
1191 struct sd *sd = (struct sd *) gspca_dev;
1193 sd->vflip = val;
1194 if (gspca_dev->streaming)
1195 setvflip(gspca_dev);
1196 return 0;
1199 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
1201 struct sd *sd = (struct sd *) gspca_dev;
1203 *val = sd->vflip;
1204 return 0;
1207 static int sd_setfreqfltr(struct gspca_dev *gspca_dev, __s32 val)
1209 struct sd *sd = (struct sd *) gspca_dev;
1211 sd->freqfltr = val;
1212 if (gspca_dev->streaming)
1213 setfreqfltr(gspca_dev);
1214 return 0;
1217 static int sd_getfreqfltr(struct gspca_dev *gspca_dev, __s32 *val)
1219 struct sd *sd = (struct sd *) gspca_dev;
1221 *val = sd->freqfltr;
1222 return 0;
1225 static int sd_querymenu(struct gspca_dev *gspca_dev,
1226 struct v4l2_querymenu *menu)
1228 switch (menu->id) {
1229 case V4L2_CID_POWER_LINE_FREQUENCY:
1230 switch (menu->index) {
1231 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1232 strcpy((char *) menu->name, "Disabled");
1233 return 0;
1234 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1235 strcpy((char *) menu->name, "50 Hz");
1236 return 0;
1238 break;
1241 return -EINVAL;
1244 /* get stream parameters (framerate) */
1245 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
1246 struct v4l2_streamparm *parm)
1248 struct v4l2_captureparm *cp = &parm->parm.capture;
1249 struct v4l2_fract *tpf = &cp->timeperframe;
1250 struct sd *sd = (struct sd *) gspca_dev;
1252 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1253 return -EINVAL;
1255 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1256 tpf->numerator = 1;
1257 tpf->denominator = sd->frame_rate;
1259 return 0;
1262 /* set stream parameters (framerate) */
1263 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
1264 struct v4l2_streamparm *parm)
1266 struct v4l2_captureparm *cp = &parm->parm.capture;
1267 struct v4l2_fract *tpf = &cp->timeperframe;
1268 struct sd *sd = (struct sd *) gspca_dev;
1270 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1271 return -EINVAL;
1273 /* Set requested framerate */
1274 sd->frame_rate = tpf->denominator / tpf->numerator;
1275 if (gspca_dev->streaming)
1276 set_frame_rate(gspca_dev);
1278 /* Return the actual framerate */
1279 tpf->numerator = 1;
1280 tpf->denominator = sd->frame_rate;
1282 return 0;
1285 /* sub-driver description */
1286 static const struct sd_desc sd_desc = {
1287 .name = MODULE_NAME,
1288 .ctrls = sd_ctrls,
1289 .nctrls = ARRAY_SIZE(sd_ctrls),
1290 .config = sd_config,
1291 .init = sd_init,
1292 .start = sd_start,
1293 .stopN = sd_stopN,
1294 .pkt_scan = sd_pkt_scan,
1295 .querymenu = sd_querymenu,
1296 .get_streamparm = sd_get_streamparm,
1297 .set_streamparm = sd_set_streamparm,
1300 /* -- module initialisation -- */
1301 static const __devinitdata struct usb_device_id device_table[] = {
1302 {USB_DEVICE(0x1415, 0x2000)},
1306 MODULE_DEVICE_TABLE(usb, device_table);
1308 /* -- device connect -- */
1309 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1311 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1312 THIS_MODULE);
1315 static struct usb_driver sd_driver = {
1316 .name = MODULE_NAME,
1317 .id_table = device_table,
1318 .probe = sd_probe,
1319 .disconnect = gspca_disconnect,
1320 #ifdef CONFIG_PM
1321 .suspend = gspca_suspend,
1322 .resume = gspca_resume,
1323 #endif
1326 /* -- module insert / remove -- */
1327 static int __init sd_mod_init(void)
1329 int ret;
1331 ret = usb_register(&sd_driver);
1332 if (ret < 0)
1333 return ret;
1334 PDEBUG(D_PROBE, "registered");
1335 return 0;
1338 static void __exit sd_mod_exit(void)
1340 usb_deregister(&sd_driver);
1341 PDEBUG(D_PROBE, "deregistered");
1344 module_init(sd_mod_init);
1345 module_exit(sd_mod_exit);