2 * ov534-ov7xxx 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
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #define MODULE_NAME "ov534"
37 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
38 #define OV534_REG_SUBADDR 0xf2
39 #define OV534_REG_WRITE 0xf3
40 #define OV534_REG_READ 0xf4
41 #define OV534_REG_OPERATION 0xf5
42 #define OV534_REG_STATUS 0xf6
44 #define OV534_OP_WRITE_3 0x37
45 #define OV534_OP_WRITE_2 0x33
46 #define OV534_OP_READ_2 0xf9
48 #define CTRL_TIMEOUT 500
50 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
51 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
52 MODULE_LICENSE("GPL");
68 NCTRLS
/* number of controls */
71 /* specific webcam descriptor */
73 struct gspca_dev gspca_dev
; /* !! must be the first item */
75 struct gspca_ctrl ctrls
[NCTRLS
];
89 /* V4L2 controls supported by the driver */
90 static void setbrightness(struct gspca_dev
*gspca_dev
);
91 static void setcontrast(struct gspca_dev
*gspca_dev
);
92 static void setgain(struct gspca_dev
*gspca_dev
);
93 static void setexposure(struct gspca_dev
*gspca_dev
);
94 static int sd_setagc(struct gspca_dev
*gspca_dev
, __s32 val
);
95 static void setawb(struct gspca_dev
*gspca_dev
);
96 static void setaec(struct gspca_dev
*gspca_dev
);
97 static void setsharpness(struct gspca_dev
*gspca_dev
);
98 static void sethvflip(struct gspca_dev
*gspca_dev
);
99 static void setcolors(struct gspca_dev
*gspca_dev
);
100 static void setlightfreq(struct gspca_dev
*gspca_dev
);
102 static int sd_start(struct gspca_dev
*gspca_dev
);
103 static void sd_stopN(struct gspca_dev
*gspca_dev
);
105 static const struct ctrl sd_ctrls
[] = {
108 .id
= V4L2_CID_BRIGHTNESS
,
109 .type
= V4L2_CTRL_TYPE_INTEGER
,
110 .name
= "Brightness",
116 .set_control
= setbrightness
120 .id
= V4L2_CID_CONTRAST
,
121 .type
= V4L2_CTRL_TYPE_INTEGER
,
128 .set_control
= setcontrast
133 .type
= V4L2_CTRL_TYPE_INTEGER
,
140 .set_control
= setgain
144 .id
= V4L2_CID_EXPOSURE
,
145 .type
= V4L2_CTRL_TYPE_INTEGER
,
150 .default_value
= 120,
152 .set_control
= setexposure
156 .id
= V4L2_CID_AUTOGAIN
,
157 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
168 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
169 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
170 .name
= "Auto White Balance",
176 .set_control
= setawb
180 .id
= V4L2_CID_EXPOSURE_AUTO
,
181 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
182 .name
= "Auto Exposure",
188 .set_control
= setaec
192 .id
= V4L2_CID_SHARPNESS
,
193 .type
= V4L2_CTRL_TYPE_INTEGER
,
200 .set_control
= setsharpness
204 .id
= V4L2_CID_HFLIP
,
205 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
212 .set_control
= sethvflip
216 .id
= V4L2_CID_VFLIP
,
217 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
224 .set_control
= sethvflip
228 .id
= V4L2_CID_SATURATION
,
229 .type
= V4L2_CTRL_TYPE_INTEGER
,
230 .name
= "Saturation",
236 .set_control
= setcolors
240 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
241 .type
= V4L2_CTRL_TYPE_MENU
,
242 .name
= "Light Frequency Filter",
248 .set_control
= setlightfreq
252 static const struct v4l2_pix_format ov772x_mode
[] = {
253 {320, 240, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
254 .bytesperline
= 320 * 2,
255 .sizeimage
= 320 * 240 * 2,
256 .colorspace
= V4L2_COLORSPACE_SRGB
,
258 {640, 480, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
259 .bytesperline
= 640 * 2,
260 .sizeimage
= 640 * 480 * 2,
261 .colorspace
= V4L2_COLORSPACE_SRGB
,
264 static const struct v4l2_pix_format ov767x_mode
[] = {
265 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
267 .sizeimage
= 320 * 240 * 3 / 8 + 590,
268 .colorspace
= V4L2_COLORSPACE_JPEG
},
269 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
271 .sizeimage
= 640 * 480 * 3 / 8 + 590,
272 .colorspace
= V4L2_COLORSPACE_JPEG
},
275 static const u8 qvga_rates
[] = {125, 100, 75, 60, 50, 40, 30};
276 static const u8 vga_rates
[] = {60, 50, 40, 30, 15};
278 static const struct framerates ov772x_framerates
[] = {
281 .nrates
= ARRAY_SIZE(qvga_rates
),
285 .nrates
= ARRAY_SIZE(vga_rates
),
294 static const u8 bridge_init_767x
[][2] = {
295 /* comments from the ms-win file apollo7670.set */
325 {0xc0, 0x50}, /* HSize 640 */
326 {0xc1, 0x3c}, /* VSize 480 */
327 {0x34, 0x05}, /* enable Audio Suspend mode */
328 {0xc2, 0x0c}, /* Input YUV */
329 {0xc3, 0xf9}, /* enable PRE */
330 {0x34, 0x05}, /* enable Audio Suspend mode */
331 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
332 {0x31, 0xf9}, /* enable 1.8V Suspend */
333 {0x35, 0x02}, /* turn on JPEG */
335 {0x25, 0x42}, /* GPIO[8]:Input */
336 {0x94, 0x11}, /* If the default setting is loaded when
337 * system boots up, this flag is closed here */
339 static const u8 sensor_init_767x
[][2] = {
357 {0x7a, 0x2a}, /* set Gamma=1.6 below */
377 {0x14, 0x38}, /* gain max 16x */
457 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
461 {0xa4, 0x8a}, /* Night mode trigger point */
494 static const u8 bridge_start_vga_767x
[][2] = {
502 {0x35, 0x02}, /* turn on JPEG */
504 {0xda, 0x00}, /* for higher clock rate(30fps) */
505 {0x34, 0x05}, /* enable Audio Suspend mode */
506 {0xc3, 0xf9}, /* enable PRE */
507 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
508 {0x8d, 0x1c}, /* output YUV */
509 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
510 {0x50, 0x00}, /* H/V divider=0 */
511 {0x51, 0xa0}, /* input H=640/4 */
512 {0x52, 0x3c}, /* input V=480/4 */
513 {0x53, 0x00}, /* offset X=0 */
514 {0x54, 0x00}, /* offset Y=0 */
515 {0x55, 0x00}, /* H/V size[8]=0 */
516 {0x57, 0x00}, /* H-size[9]=0 */
517 {0x5c, 0x00}, /* output size[9:8]=0 */
518 {0x5a, 0xa0}, /* output H=640/4 */
519 {0x5b, 0x78}, /* output V=480/4 */
524 static const u8 sensor_start_vga_767x
[][2] = {
530 static const u8 bridge_start_qvga_767x
[][2] = {
538 {0x35, 0x02}, /* turn on JPEG */
540 {0xc0, 0x50}, /* CIF HSize 640 */
541 {0xc1, 0x3c}, /* CIF VSize 480 */
542 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
543 {0x8d, 0x1c}, /* output YUV */
544 {0x34, 0x05}, /* enable Audio Suspend mode */
545 {0xc2, 0x4c}, /* output YUV and Enable DCW */
546 {0xc3, 0xf9}, /* enable PRE */
547 {0x1c, 0x00}, /* indirect addressing */
548 {0x1d, 0x48}, /* output YUV422 */
549 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
550 {0x51, 0xa0}, /* DCW input H=640/4 */
551 {0x52, 0x78}, /* DCW input V=480/4 */
552 {0x53, 0x00}, /* offset X=0 */
553 {0x54, 0x00}, /* offset Y=0 */
554 {0x55, 0x00}, /* H/V size[8]=0 */
555 {0x57, 0x00}, /* H-size[9]=0 */
556 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
557 {0x5a, 0x50}, /* DCW output H=320/4 */
558 {0x5b, 0x3c}, /* DCW output V=240/4 */
563 static const u8 sensor_start_qvga_767x
[][2] = {
570 static const u8 bridge_init_772x
[][2] = {
609 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
610 { 0x1d, 0x00 }, /* payload size */
612 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
613 { 0x1d, 0x58 }, /* frame size */
614 { 0x1d, 0x00 }, /* frame size */
617 { 0x1d, 0x08 }, /* turn on UVC header */
618 { 0x1d, 0x0e }, /* .. */
628 static const u8 sensor_init_772x
[][2] = {
631 /*fixme: better have a delay?*/
654 { 0x63, 0xaa }, /* AWB - was e0 */
657 { 0x13, 0xf0 }, /* com8 */
670 { 0x13, 0xff }, /* AWB */
718 { 0x8e, 0x00 }, /* De-noise threshold */
721 static const u8 bridge_start_vga_772x
[][2] = {
732 static const u8 sensor_start_vga_772x
[][2] = {
742 static const u8 bridge_start_qvga_772x
[][2] = {
753 static const u8 sensor_start_qvga_772x
[][2] = {
764 static void ov534_reg_write(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
766 struct usb_device
*udev
= gspca_dev
->dev
;
769 if (gspca_dev
->usb_err
< 0)
772 PDEBUG(D_USBO
, "SET 01 0000 %04x %02x", reg
, val
);
773 gspca_dev
->usb_buf
[0] = val
;
774 ret
= usb_control_msg(udev
,
775 usb_sndctrlpipe(udev
, 0),
777 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
778 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
780 pr_err("write failed %d\n", ret
);
781 gspca_dev
->usb_err
= ret
;
785 static u8
ov534_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
787 struct usb_device
*udev
= gspca_dev
->dev
;
790 if (gspca_dev
->usb_err
< 0)
792 ret
= usb_control_msg(udev
,
793 usb_rcvctrlpipe(udev
, 0),
795 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
796 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
797 PDEBUG(D_USBI
, "GET 01 0000 %04x %02x", reg
, gspca_dev
->usb_buf
[0]);
799 pr_err("read failed %d\n", ret
);
800 gspca_dev
->usb_err
= ret
;
802 return gspca_dev
->usb_buf
[0];
805 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
806 * (direction and output)? */
807 static void ov534_set_led(struct gspca_dev
*gspca_dev
, int status
)
811 PDEBUG(D_CONF
, "led status: %d", status
);
813 data
= ov534_reg_read(gspca_dev
, 0x21);
815 ov534_reg_write(gspca_dev
, 0x21, data
);
817 data
= ov534_reg_read(gspca_dev
, 0x23);
823 ov534_reg_write(gspca_dev
, 0x23, data
);
826 data
= ov534_reg_read(gspca_dev
, 0x21);
828 ov534_reg_write(gspca_dev
, 0x21, data
);
832 static int sccb_check_status(struct gspca_dev
*gspca_dev
)
837 for (i
= 0; i
< 5; i
++) {
838 data
= ov534_reg_read(gspca_dev
, OV534_REG_STATUS
);
848 PDEBUG(D_ERR
, "sccb status 0x%02x, attempt %d/5",
855 static void sccb_reg_write(struct gspca_dev
*gspca_dev
, u8 reg
, u8 val
)
857 PDEBUG(D_USBO
, "sccb write: %02x %02x", reg
, val
);
858 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
859 ov534_reg_write(gspca_dev
, OV534_REG_WRITE
, val
);
860 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_3
);
862 if (!sccb_check_status(gspca_dev
)) {
863 pr_err("sccb_reg_write failed\n");
864 gspca_dev
->usb_err
= -EIO
;
868 static u8
sccb_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
870 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
871 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_2
);
872 if (!sccb_check_status(gspca_dev
))
873 pr_err("sccb_reg_read failed 1\n");
875 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_READ_2
);
876 if (!sccb_check_status(gspca_dev
))
877 pr_err("sccb_reg_read failed 2\n");
879 return ov534_reg_read(gspca_dev
, OV534_REG_READ
);
882 /* output a bridge sequence (reg - val) */
883 static void reg_w_array(struct gspca_dev
*gspca_dev
,
884 const u8 (*data
)[2], int len
)
887 ov534_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
892 /* output a sensor sequence (reg - val) */
893 static void sccb_w_array(struct gspca_dev
*gspca_dev
,
894 const u8 (*data
)[2], int len
)
897 if ((*data
)[0] != 0xff) {
898 sccb_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
900 sccb_reg_read(gspca_dev
, (*data
)[1]);
901 sccb_reg_write(gspca_dev
, 0xff, 0x00);
907 /* ov772x specific controls */
908 static void set_frame_rate(struct gspca_dev
*gspca_dev
)
910 struct sd
*sd
= (struct sd
*) gspca_dev
;
918 const struct rate_s
*r
;
919 static const struct rate_s rate_0
[] = { /* 640x480 */
920 {60, 0x01, 0xc1, 0x04},
921 {50, 0x01, 0x41, 0x02},
922 {40, 0x02, 0xc1, 0x04},
923 {30, 0x04, 0x81, 0x02},
924 {15, 0x03, 0x41, 0x04},
926 static const struct rate_s rate_1
[] = { /* 320x240 */
927 {125, 0x02, 0x81, 0x02},
928 {100, 0x02, 0xc1, 0x04},
929 {75, 0x03, 0xc1, 0x04},
930 {60, 0x04, 0xc1, 0x04},
931 {50, 0x02, 0x41, 0x04},
932 {40, 0x03, 0x41, 0x04},
933 {30, 0x04, 0x41, 0x04},
936 if (sd
->sensor
!= SENSOR_OV772x
)
938 if (gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
== 0) {
940 i
= ARRAY_SIZE(rate_0
);
943 i
= ARRAY_SIZE(rate_1
);
946 if (sd
->frame_rate
>= r
->fps
)
951 sccb_reg_write(gspca_dev
, 0x11, r
->r11
);
952 sccb_reg_write(gspca_dev
, 0x0d, r
->r0d
);
953 ov534_reg_write(gspca_dev
, 0xe5, r
->re5
);
955 PDEBUG(D_PROBE
, "frame_rate: %d", r
->fps
);
958 static void setbrightness(struct gspca_dev
*gspca_dev
)
960 struct sd
*sd
= (struct sd
*) gspca_dev
;
963 val
= sd
->ctrls
[BRIGHTNESS
].val
;
964 if (sd
->sensor
== SENSOR_OV767x
) {
967 sccb_reg_write(gspca_dev
, 0x55, val
); /* bright */
969 sccb_reg_write(gspca_dev
, 0x9b, val
);
973 static void setcontrast(struct gspca_dev
*gspca_dev
)
975 struct sd
*sd
= (struct sd
*) gspca_dev
;
978 val
= sd
->ctrls
[CONTRAST
].val
;
979 if (sd
->sensor
== SENSOR_OV767x
)
980 sccb_reg_write(gspca_dev
, 0x56, val
); /* contras */
982 sccb_reg_write(gspca_dev
, 0x9c, val
);
985 static void setgain(struct gspca_dev
*gspca_dev
)
987 struct sd
*sd
= (struct sd
*) gspca_dev
;
990 if (sd
->ctrls
[AGC
].val
)
993 val
= sd
->ctrls
[GAIN
].val
;
994 switch (val
& 0x30) {
1012 sccb_reg_write(gspca_dev
, 0x00, val
);
1015 static void setexposure(struct gspca_dev
*gspca_dev
)
1017 struct sd
*sd
= (struct sd
*) gspca_dev
;
1020 if (sd
->ctrls
[AEC
].val
)
1023 val
= sd
->ctrls
[EXPOSURE
].val
;
1024 if (sd
->sensor
== SENSOR_OV767x
) {
1026 /* set only aec[9:2] */
1027 sccb_reg_write(gspca_dev
, 0x10, val
); /* aech */
1030 /* 'val' is one byte and represents half of the exposure value
1031 * we are going to set into registers, a two bytes value:
1033 * MSB: ((u16) val << 1) >> 8 == val >> 7
1034 * LSB: ((u16) val << 1) & 0xff == val << 1
1036 sccb_reg_write(gspca_dev
, 0x08, val
>> 7);
1037 sccb_reg_write(gspca_dev
, 0x10, val
<< 1);
1041 static void setagc(struct gspca_dev
*gspca_dev
)
1043 struct sd
*sd
= (struct sd
*) gspca_dev
;
1045 if (sd
->ctrls
[AGC
].val
) {
1046 sccb_reg_write(gspca_dev
, 0x13,
1047 sccb_reg_read(gspca_dev
, 0x13) | 0x04);
1048 sccb_reg_write(gspca_dev
, 0x64,
1049 sccb_reg_read(gspca_dev
, 0x64) | 0x03);
1051 sccb_reg_write(gspca_dev
, 0x13,
1052 sccb_reg_read(gspca_dev
, 0x13) & ~0x04);
1053 sccb_reg_write(gspca_dev
, 0x64,
1054 sccb_reg_read(gspca_dev
, 0x64) & ~0x03);
1060 static void setawb(struct gspca_dev
*gspca_dev
)
1062 struct sd
*sd
= (struct sd
*) gspca_dev
;
1064 if (sd
->ctrls
[AWB
].val
) {
1065 sccb_reg_write(gspca_dev
, 0x13,
1066 sccb_reg_read(gspca_dev
, 0x13) | 0x02);
1067 if (sd
->sensor
== SENSOR_OV772x
)
1068 sccb_reg_write(gspca_dev
, 0x63,
1069 sccb_reg_read(gspca_dev
, 0x63) | 0xc0);
1071 sccb_reg_write(gspca_dev
, 0x13,
1072 sccb_reg_read(gspca_dev
, 0x13) & ~0x02);
1073 if (sd
->sensor
== SENSOR_OV772x
)
1074 sccb_reg_write(gspca_dev
, 0x63,
1075 sccb_reg_read(gspca_dev
, 0x63) & ~0xc0);
1079 static void setaec(struct gspca_dev
*gspca_dev
)
1081 struct sd
*sd
= (struct sd
*) gspca_dev
;
1084 data
= sd
->sensor
== SENSOR_OV767x
?
1085 0x05 : /* agc + aec */
1087 if (sd
->ctrls
[AEC
].val
)
1088 sccb_reg_write(gspca_dev
, 0x13,
1089 sccb_reg_read(gspca_dev
, 0x13) | data
);
1091 sccb_reg_write(gspca_dev
, 0x13,
1092 sccb_reg_read(gspca_dev
, 0x13) & ~data
);
1093 if (sd
->sensor
== SENSOR_OV767x
)
1094 sd
->ctrls
[EXPOSURE
].val
=
1095 sccb_reg_read(gspca_dev
, 10); /* aech */
1097 setexposure(gspca_dev
);
1101 static void setsharpness(struct gspca_dev
*gspca_dev
)
1103 struct sd
*sd
= (struct sd
*) gspca_dev
;
1106 val
= sd
->ctrls
[SHARPNESS
].val
;
1107 sccb_reg_write(gspca_dev
, 0x91, val
); /* Auto de-noise threshold */
1108 sccb_reg_write(gspca_dev
, 0x8e, val
); /* De-noise threshold */
1111 static void sethvflip(struct gspca_dev
*gspca_dev
)
1113 struct sd
*sd
= (struct sd
*) gspca_dev
;
1116 if (sd
->sensor
== SENSOR_OV767x
) {
1117 val
= sccb_reg_read(gspca_dev
, 0x1e); /* mvfp */
1119 if (sd
->ctrls
[HFLIP
].val
)
1121 if (sd
->ctrls
[VFLIP
].val
)
1123 sccb_reg_write(gspca_dev
, 0x1e, val
);
1125 val
= sccb_reg_read(gspca_dev
, 0x0c);
1127 if (sd
->ctrls
[HFLIP
].val
== 0)
1129 if (sd
->ctrls
[VFLIP
].val
== 0)
1131 sccb_reg_write(gspca_dev
, 0x0c, val
);
1135 static void setcolors(struct gspca_dev
*gspca_dev
)
1137 struct sd
*sd
= (struct sd
*) gspca_dev
;
1140 static u8 color_tb
[][6] = {
1141 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1142 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1143 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1144 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1145 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1146 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1147 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1150 val
= sd
->ctrls
[COLORS
].val
;
1151 for (i
= 0; i
< ARRAY_SIZE(color_tb
[0]); i
++)
1152 sccb_reg_write(gspca_dev
, 0x4f + i
, color_tb
[val
][i
]);
1155 static void setlightfreq(struct gspca_dev
*gspca_dev
)
1157 struct sd
*sd
= (struct sd
*) gspca_dev
;
1160 val
= sd
->ctrls
[LIGHTFREQ
].val
? 0x9e : 0x00;
1161 if (sd
->sensor
== SENSOR_OV767x
) {
1162 sccb_reg_write(gspca_dev
, 0x2a, 0x00);
1164 val
= 0x9d; /* insert dummy to 25fps for 50Hz */
1166 sccb_reg_write(gspca_dev
, 0x2b, val
);
1170 /* this function is called at probe time */
1171 static int sd_config(struct gspca_dev
*gspca_dev
,
1172 const struct usb_device_id
*id
)
1174 struct sd
*sd
= (struct sd
*) gspca_dev
;
1177 cam
= &gspca_dev
->cam
;
1179 cam
->ctrls
= sd
->ctrls
;
1181 /* the auto white balance control works only when auto gain is set */
1182 if (sd_ctrls
[AGC
].qctrl
.default_value
== 0)
1183 gspca_dev
->ctrl_inac
|= (1 << AWB
);
1185 cam
->cam_mode
= ov772x_mode
;
1186 cam
->nmodes
= ARRAY_SIZE(ov772x_mode
);
1188 sd
->frame_rate
= 30;
1193 /* this function is called at probe and resume time */
1194 static int sd_init(struct gspca_dev
*gspca_dev
)
1196 struct sd
*sd
= (struct sd
*) gspca_dev
;
1198 static const struct reg_array bridge_init
[NSENSORS
] = {
1199 [SENSOR_OV767x
] = {bridge_init_767x
, ARRAY_SIZE(bridge_init_767x
)},
1200 [SENSOR_OV772x
] = {bridge_init_772x
, ARRAY_SIZE(bridge_init_772x
)},
1202 static const struct reg_array sensor_init
[NSENSORS
] = {
1203 [SENSOR_OV767x
] = {sensor_init_767x
, ARRAY_SIZE(sensor_init_767x
)},
1204 [SENSOR_OV772x
] = {sensor_init_772x
, ARRAY_SIZE(sensor_init_772x
)},
1208 ov534_reg_write(gspca_dev
, 0xe7, 0x3a);
1209 ov534_reg_write(gspca_dev
, 0xe0, 0x08);
1212 /* initialize the sensor address */
1213 ov534_reg_write(gspca_dev
, OV534_REG_ADDRESS
, 0x42);
1216 sccb_reg_write(gspca_dev
, 0x12, 0x80);
1219 /* probe the sensor */
1220 sccb_reg_read(gspca_dev
, 0x0a);
1221 sensor_id
= sccb_reg_read(gspca_dev
, 0x0a) << 8;
1222 sccb_reg_read(gspca_dev
, 0x0b);
1223 sensor_id
|= sccb_reg_read(gspca_dev
, 0x0b);
1224 PDEBUG(D_PROBE
, "Sensor ID: %04x", sensor_id
);
1226 if ((sensor_id
& 0xfff0) == 0x7670) {
1227 sd
->sensor
= SENSOR_OV767x
;
1228 gspca_dev
->ctrl_dis
= (1 << GAIN
) |
1230 (1 << SHARPNESS
); /* auto */
1231 sd
->ctrls
[BRIGHTNESS
].min
= -127;
1232 sd
->ctrls
[BRIGHTNESS
].max
= 127;
1233 sd
->ctrls
[BRIGHTNESS
].def
= 0;
1234 sd
->ctrls
[CONTRAST
].max
= 0x80;
1235 sd
->ctrls
[CONTRAST
].def
= 0x40;
1236 sd
->ctrls
[EXPOSURE
].min
= 0x08;
1237 sd
->ctrls
[EXPOSURE
].max
= 0x60;
1238 sd
->ctrls
[EXPOSURE
].def
= 0x13;
1239 sd
->ctrls
[SHARPNESS
].max
= 9;
1240 sd
->ctrls
[SHARPNESS
].def
= 4;
1241 sd
->ctrls
[HFLIP
].def
= 1;
1242 gspca_dev
->cam
.cam_mode
= ov767x_mode
;
1243 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(ov767x_mode
);
1245 sd
->sensor
= SENSOR_OV772x
;
1246 gspca_dev
->ctrl_dis
= (1 << COLORS
);
1247 gspca_dev
->cam
.bulk
= 1;
1248 gspca_dev
->cam
.bulk_size
= 16384;
1249 gspca_dev
->cam
.bulk_nurbs
= 2;
1250 gspca_dev
->cam
.mode_framerates
= ov772x_framerates
;
1254 reg_w_array(gspca_dev
, bridge_init
[sd
->sensor
].val
,
1255 bridge_init
[sd
->sensor
].len
);
1256 ov534_set_led(gspca_dev
, 1);
1257 sccb_w_array(gspca_dev
, sensor_init
[sd
->sensor
].val
,
1258 sensor_init
[sd
->sensor
].len
);
1259 if (sd
->sensor
== SENSOR_OV767x
)
1260 sd_start(gspca_dev
);
1261 sd_stopN(gspca_dev
);
1262 /* set_frame_rate(gspca_dev); */
1264 return gspca_dev
->usb_err
;
1267 static int sd_start(struct gspca_dev
*gspca_dev
)
1269 struct sd
*sd
= (struct sd
*) gspca_dev
;
1271 static const struct reg_array bridge_start
[NSENSORS
][2] = {
1272 [SENSOR_OV767x
] = {{bridge_start_qvga_767x
,
1273 ARRAY_SIZE(bridge_start_qvga_767x
)},
1274 {bridge_start_vga_767x
,
1275 ARRAY_SIZE(bridge_start_vga_767x
)}},
1276 [SENSOR_OV772x
] = {{bridge_start_qvga_772x
,
1277 ARRAY_SIZE(bridge_start_qvga_772x
)},
1278 {bridge_start_vga_772x
,
1279 ARRAY_SIZE(bridge_start_vga_772x
)}},
1281 static const struct reg_array sensor_start
[NSENSORS
][2] = {
1282 [SENSOR_OV767x
] = {{sensor_start_qvga_767x
,
1283 ARRAY_SIZE(sensor_start_qvga_767x
)},
1284 {sensor_start_vga_767x
,
1285 ARRAY_SIZE(sensor_start_vga_767x
)}},
1286 [SENSOR_OV772x
] = {{sensor_start_qvga_772x
,
1287 ARRAY_SIZE(sensor_start_qvga_772x
)},
1288 {sensor_start_vga_772x
,
1289 ARRAY_SIZE(sensor_start_vga_772x
)}},
1292 /* (from ms-win trace) */
1293 if (sd
->sensor
== SENSOR_OV767x
)
1294 sccb_reg_write(gspca_dev
, 0x1e, 0x04);
1295 /* black sun enable ? */
1297 mode
= gspca_dev
->curr_mode
; /* 0: 320x240, 1: 640x480 */
1298 reg_w_array(gspca_dev
, bridge_start
[sd
->sensor
][mode
].val
,
1299 bridge_start
[sd
->sensor
][mode
].len
);
1300 sccb_w_array(gspca_dev
, sensor_start
[sd
->sensor
][mode
].val
,
1301 sensor_start
[sd
->sensor
][mode
].len
);
1303 set_frame_rate(gspca_dev
);
1305 if (!(gspca_dev
->ctrl_dis
& (1 << AGC
)))
1309 if (!(gspca_dev
->ctrl_dis
& (1 << GAIN
)))
1311 setexposure(gspca_dev
);
1312 setbrightness(gspca_dev
);
1313 setcontrast(gspca_dev
);
1314 if (!(gspca_dev
->ctrl_dis
& (1 << SHARPNESS
)))
1315 setsharpness(gspca_dev
);
1316 sethvflip(gspca_dev
);
1317 if (!(gspca_dev
->ctrl_dis
& (1 << COLORS
)))
1318 setcolors(gspca_dev
);
1319 setlightfreq(gspca_dev
);
1321 ov534_set_led(gspca_dev
, 1);
1322 ov534_reg_write(gspca_dev
, 0xe0, 0x00);
1323 return gspca_dev
->usb_err
;
1326 static void sd_stopN(struct gspca_dev
*gspca_dev
)
1328 ov534_reg_write(gspca_dev
, 0xe0, 0x09);
1329 ov534_set_led(gspca_dev
, 0);
1332 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1333 #define UVC_STREAM_EOH (1 << 7)
1334 #define UVC_STREAM_ERR (1 << 6)
1335 #define UVC_STREAM_STI (1 << 5)
1336 #define UVC_STREAM_RES (1 << 4)
1337 #define UVC_STREAM_SCR (1 << 3)
1338 #define UVC_STREAM_PTS (1 << 2)
1339 #define UVC_STREAM_EOF (1 << 1)
1340 #define UVC_STREAM_FID (1 << 0)
1342 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1345 struct sd
*sd
= (struct sd
*) gspca_dev
;
1348 int remaining_len
= len
;
1351 payload_len
= gspca_dev
->cam
.bulk
? 2048 : 2040;
1353 len
= min(remaining_len
, payload_len
);
1355 /* Payloads are prefixed with a UVC-style header. We
1356 consider a frame to start when the FID toggles, or the PTS
1357 changes. A frame ends when EOF is set, and we've received
1358 the correct number of bytes. */
1360 /* Verify UVC header. Header length is always 12 */
1361 if (data
[0] != 12 || len
< 12) {
1362 PDEBUG(D_PACK
, "bad header");
1367 if (data
[1] & UVC_STREAM_ERR
) {
1368 PDEBUG(D_PACK
, "payload error");
1372 /* Extract PTS and FID */
1373 if (!(data
[1] & UVC_STREAM_PTS
)) {
1374 PDEBUG(D_PACK
, "PTS not present");
1377 this_pts
= (data
[5] << 24) | (data
[4] << 16)
1378 | (data
[3] << 8) | data
[2];
1379 this_fid
= (data
[1] & UVC_STREAM_FID
) ? 1 : 0;
1381 /* If PTS or FID has changed, start a new frame. */
1382 if (this_pts
!= sd
->last_pts
|| this_fid
!= sd
->last_fid
) {
1383 if (gspca_dev
->last_packet_type
== INTER_PACKET
)
1384 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1386 sd
->last_pts
= this_pts
;
1387 sd
->last_fid
= this_fid
;
1388 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1389 data
+ 12, len
- 12);
1390 /* If this packet is marked as EOF, end the frame */
1391 } else if (data
[1] & UVC_STREAM_EOF
) {
1393 if (gspca_dev
->pixfmt
== V4L2_PIX_FMT_YUYV
1394 && gspca_dev
->image_len
+ len
- 12 !=
1395 gspca_dev
->width
* gspca_dev
->height
* 2) {
1396 PDEBUG(D_PACK
, "wrong sized frame");
1399 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1400 data
+ 12, len
- 12);
1403 /* Add the data from this payload */
1404 gspca_frame_add(gspca_dev
, INTER_PACKET
,
1405 data
+ 12, len
- 12);
1408 /* Done this payload */
1412 /* Discard data until a new frame starts. */
1413 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1416 remaining_len
-= len
;
1418 } while (remaining_len
> 0);
1421 static int sd_setagc(struct gspca_dev
*gspca_dev
, __s32 val
)
1423 struct sd
*sd
= (struct sd
*) gspca_dev
;
1425 sd
->ctrls
[AGC
].val
= val
;
1427 /* the auto white balance control works only
1428 * when auto gain is set */
1430 gspca_dev
->ctrl_inac
&= ~(1 << AWB
);
1432 gspca_dev
->ctrl_inac
|= (1 << AWB
);
1433 if (sd
->ctrls
[AWB
].val
) {
1434 sd
->ctrls
[AWB
].val
= 0;
1435 if (gspca_dev
->streaming
)
1439 if (gspca_dev
->streaming
)
1441 return gspca_dev
->usb_err
;
1444 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
1445 struct v4l2_querymenu
*menu
)
1448 case V4L2_CID_POWER_LINE_FREQUENCY
:
1449 switch (menu
->index
) {
1450 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1451 strcpy((char *) menu
->name
, "Disabled");
1453 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1454 strcpy((char *) menu
->name
, "50 Hz");
1463 /* get stream parameters (framerate) */
1464 static void sd_get_streamparm(struct gspca_dev
*gspca_dev
,
1465 struct v4l2_streamparm
*parm
)
1467 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1468 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1469 struct sd
*sd
= (struct sd
*) gspca_dev
;
1471 cp
->capability
|= V4L2_CAP_TIMEPERFRAME
;
1473 tpf
->denominator
= sd
->frame_rate
;
1476 /* set stream parameters (framerate) */
1477 static void sd_set_streamparm(struct gspca_dev
*gspca_dev
,
1478 struct v4l2_streamparm
*parm
)
1480 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1481 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1482 struct sd
*sd
= (struct sd
*) gspca_dev
;
1484 /* Set requested framerate */
1485 sd
->frame_rate
= tpf
->denominator
/ tpf
->numerator
;
1486 if (gspca_dev
->streaming
)
1487 set_frame_rate(gspca_dev
);
1489 /* Return the actual framerate */
1491 tpf
->denominator
= sd
->frame_rate
;
1494 /* sub-driver description */
1495 static const struct sd_desc sd_desc
= {
1496 .name
= MODULE_NAME
,
1498 .nctrls
= ARRAY_SIZE(sd_ctrls
),
1499 .config
= sd_config
,
1503 .pkt_scan
= sd_pkt_scan
,
1504 .querymenu
= sd_querymenu
,
1505 .get_streamparm
= sd_get_streamparm
,
1506 .set_streamparm
= sd_set_streamparm
,
1509 /* -- module initialisation -- */
1510 static const struct usb_device_id device_table
[] = {
1511 {USB_DEVICE(0x1415, 0x2000)},
1512 {USB_DEVICE(0x06f8, 0x3002)},
1516 MODULE_DEVICE_TABLE(usb
, device_table
);
1518 /* -- device connect -- */
1519 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1521 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1525 static struct usb_driver sd_driver
= {
1526 .name
= MODULE_NAME
,
1527 .id_table
= device_table
,
1529 .disconnect
= gspca_disconnect
,
1531 .suspend
= gspca_suspend
,
1532 .resume
= gspca_resume
,
1536 module_usb_driver(sd_driver
);