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 MODULE_NAME "ov534"
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");
66 NCTRLS
/* number of controls */
69 /* specific webcam descriptor */
71 struct gspca_dev gspca_dev
; /* !! must be the first item */
73 struct gspca_ctrl ctrls
[NCTRLS
];
87 /* V4L2 controls supported by the driver */
88 static void setbrightness(struct gspca_dev
*gspca_dev
);
89 static void setcontrast(struct gspca_dev
*gspca_dev
);
90 static void setgain(struct gspca_dev
*gspca_dev
);
91 static void setexposure(struct gspca_dev
*gspca_dev
);
92 static int sd_setagc(struct gspca_dev
*gspca_dev
, __s32 val
);
93 static void setawb(struct gspca_dev
*gspca_dev
);
94 static void setaec(struct gspca_dev
*gspca_dev
);
95 static void setsharpness(struct gspca_dev
*gspca_dev
);
96 static void sethvflip(struct gspca_dev
*gspca_dev
);
97 static void setcolors(struct gspca_dev
*gspca_dev
);
98 static void setlightfreq(struct gspca_dev
*gspca_dev
);
100 static int sd_start(struct gspca_dev
*gspca_dev
);
101 static void sd_stopN(struct gspca_dev
*gspca_dev
);
103 static const struct ctrl sd_ctrls
[] = {
106 .id
= V4L2_CID_BRIGHTNESS
,
107 .type
= V4L2_CTRL_TYPE_INTEGER
,
108 .name
= "Brightness",
114 .set_control
= setbrightness
118 .id
= V4L2_CID_CONTRAST
,
119 .type
= V4L2_CTRL_TYPE_INTEGER
,
126 .set_control
= setcontrast
131 .type
= V4L2_CTRL_TYPE_INTEGER
,
138 .set_control
= setgain
142 .id
= V4L2_CID_EXPOSURE
,
143 .type
= V4L2_CTRL_TYPE_INTEGER
,
148 .default_value
= 120,
150 .set_control
= setexposure
154 .id
= V4L2_CID_AUTOGAIN
,
155 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
166 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
167 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
168 .name
= "Auto White Balance",
174 .set_control
= setawb
178 .id
= V4L2_CID_EXPOSURE_AUTO
,
179 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
180 .name
= "Auto Exposure",
186 .set_control
= setaec
190 .id
= V4L2_CID_SHARPNESS
,
191 .type
= V4L2_CTRL_TYPE_INTEGER
,
198 .set_control
= setsharpness
202 .id
= V4L2_CID_HFLIP
,
203 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
210 .set_control
= sethvflip
214 .id
= V4L2_CID_VFLIP
,
215 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
222 .set_control
= sethvflip
226 .id
= V4L2_CID_SATURATION
,
227 .type
= V4L2_CTRL_TYPE_INTEGER
,
228 .name
= "Saturation",
234 .set_control
= setcolors
238 .id
= V4L2_CID_POWER_LINE_FREQUENCY
,
239 .type
= V4L2_CTRL_TYPE_MENU
,
240 .name
= "Light Frequency Filter",
246 .set_control
= setlightfreq
250 static const struct v4l2_pix_format ov772x_mode
[] = {
251 {320, 240, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
252 .bytesperline
= 320 * 2,
253 .sizeimage
= 320 * 240 * 2,
254 .colorspace
= V4L2_COLORSPACE_SRGB
,
256 {640, 480, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
257 .bytesperline
= 640 * 2,
258 .sizeimage
= 640 * 480 * 2,
259 .colorspace
= V4L2_COLORSPACE_SRGB
,
262 static const struct v4l2_pix_format ov767x_mode
[] = {
263 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
265 .sizeimage
= 320 * 240 * 3 / 8 + 590,
266 .colorspace
= V4L2_COLORSPACE_JPEG
},
267 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
269 .sizeimage
= 640 * 480 * 3 / 8 + 590,
270 .colorspace
= V4L2_COLORSPACE_JPEG
},
273 static const u8 qvga_rates
[] = {125, 100, 75, 60, 50, 40, 30};
274 static const u8 vga_rates
[] = {60, 50, 40, 30, 15};
276 static const struct framerates ov772x_framerates
[] = {
279 .nrates
= ARRAY_SIZE(qvga_rates
),
283 .nrates
= ARRAY_SIZE(vga_rates
),
292 static const u8 bridge_init_767x
[][2] = {
293 /* comments from the ms-win file apollo7670.set */
323 {0xc0, 0x50}, /* HSize 640 */
324 {0xc1, 0x3c}, /* VSize 480 */
325 {0x34, 0x05}, /* enable Audio Suspend mode */
326 {0xc2, 0x0c}, /* Input YUV */
327 {0xc3, 0xf9}, /* enable PRE */
328 {0x34, 0x05}, /* enable Audio Suspend mode */
329 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
330 {0x31, 0xf9}, /* enable 1.8V Suspend */
331 {0x35, 0x02}, /* turn on JPEG */
333 {0x25, 0x42}, /* GPIO[8]:Input */
334 {0x94, 0x11}, /* If the default setting is loaded when
335 * system boots up, this flag is closed here */
337 static const u8 sensor_init_767x
[][2] = {
355 {0x7a, 0x2a}, /* set Gamma=1.6 below */
375 {0x14, 0x38}, /* gain max 16x */
455 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
459 {0xa4, 0x8a}, /* Night mode trigger point */
492 static const u8 bridge_start_vga_767x
[][2] = {
500 {0x35, 0x02}, /* turn on JPEG */
502 {0xda, 0x00}, /* for higher clock rate(30fps) */
503 {0x34, 0x05}, /* enable Audio Suspend mode */
504 {0xc3, 0xf9}, /* enable PRE */
505 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
506 {0x8d, 0x1c}, /* output YUV */
507 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
508 {0x50, 0x00}, /* H/V divider=0 */
509 {0x51, 0xa0}, /* input H=640/4 */
510 {0x52, 0x3c}, /* input V=480/4 */
511 {0x53, 0x00}, /* offset X=0 */
512 {0x54, 0x00}, /* offset Y=0 */
513 {0x55, 0x00}, /* H/V size[8]=0 */
514 {0x57, 0x00}, /* H-size[9]=0 */
515 {0x5c, 0x00}, /* output size[9:8]=0 */
516 {0x5a, 0xa0}, /* output H=640/4 */
517 {0x5b, 0x78}, /* output V=480/4 */
522 static const u8 sensor_start_vga_767x
[][2] = {
528 static const u8 bridge_start_qvga_767x
[][2] = {
536 {0x35, 0x02}, /* turn on JPEG */
538 {0xc0, 0x50}, /* CIF HSize 640 */
539 {0xc1, 0x3c}, /* CIF VSize 480 */
540 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
541 {0x8d, 0x1c}, /* output YUV */
542 {0x34, 0x05}, /* enable Audio Suspend mode */
543 {0xc2, 0x4c}, /* output YUV and Enable DCW */
544 {0xc3, 0xf9}, /* enable PRE */
545 {0x1c, 0x00}, /* indirect addressing */
546 {0x1d, 0x48}, /* output YUV422 */
547 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
548 {0x51, 0xa0}, /* DCW input H=640/4 */
549 {0x52, 0x78}, /* DCW input V=480/4 */
550 {0x53, 0x00}, /* offset X=0 */
551 {0x54, 0x00}, /* offset Y=0 */
552 {0x55, 0x00}, /* H/V size[8]=0 */
553 {0x57, 0x00}, /* H-size[9]=0 */
554 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
555 {0x5a, 0x50}, /* DCW output H=320/4 */
556 {0x5b, 0x3c}, /* DCW output V=240/4 */
561 static const u8 sensor_start_qvga_767x
[][2] = {
568 static const u8 bridge_init_772x
[][2] = {
607 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
608 { 0x1d, 0x00 }, /* payload size */
610 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
611 { 0x1d, 0x58 }, /* frame size */
612 { 0x1d, 0x00 }, /* frame size */
615 { 0x1d, 0x08 }, /* turn on UVC header */
616 { 0x1d, 0x0e }, /* .. */
626 static const u8 sensor_init_772x
[][2] = {
629 /*fixme: better have a delay?*/
652 { 0x63, 0xaa }, /* AWB - was e0 */
655 { 0x13, 0xf0 }, /* com8 */
668 { 0x13, 0xff }, /* AWB */
716 { 0x8e, 0x00 }, /* De-noise threshold */
719 static const u8 bridge_start_vga_772x
[][2] = {
730 static const u8 sensor_start_vga_772x
[][2] = {
740 static const u8 bridge_start_qvga_772x
[][2] = {
751 static const u8 sensor_start_qvga_772x
[][2] = {
762 static void ov534_reg_write(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
764 struct usb_device
*udev
= gspca_dev
->dev
;
767 if (gspca_dev
->usb_err
< 0)
770 PDEBUG(D_USBO
, "SET 01 0000 %04x %02x", reg
, val
);
771 gspca_dev
->usb_buf
[0] = val
;
772 ret
= usb_control_msg(udev
,
773 usb_sndctrlpipe(udev
, 0),
775 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
776 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
778 err("write failed %d", ret
);
779 gspca_dev
->usb_err
= ret
;
783 static u8
ov534_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
785 struct usb_device
*udev
= gspca_dev
->dev
;
788 if (gspca_dev
->usb_err
< 0)
790 ret
= usb_control_msg(udev
,
791 usb_rcvctrlpipe(udev
, 0),
793 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
794 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
795 PDEBUG(D_USBI
, "GET 01 0000 %04x %02x", reg
, gspca_dev
->usb_buf
[0]);
797 err("read failed %d", ret
);
798 gspca_dev
->usb_err
= ret
;
800 return gspca_dev
->usb_buf
[0];
803 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
804 * (direction and output)? */
805 static void ov534_set_led(struct gspca_dev
*gspca_dev
, int status
)
809 PDEBUG(D_CONF
, "led status: %d", status
);
811 data
= ov534_reg_read(gspca_dev
, 0x21);
813 ov534_reg_write(gspca_dev
, 0x21, data
);
815 data
= ov534_reg_read(gspca_dev
, 0x23);
821 ov534_reg_write(gspca_dev
, 0x23, data
);
824 data
= ov534_reg_read(gspca_dev
, 0x21);
826 ov534_reg_write(gspca_dev
, 0x21, data
);
830 static int sccb_check_status(struct gspca_dev
*gspca_dev
)
835 for (i
= 0; i
< 5; i
++) {
836 data
= ov534_reg_read(gspca_dev
, OV534_REG_STATUS
);
846 PDEBUG(D_ERR
, "sccb status 0x%02x, attempt %d/5",
853 static void sccb_reg_write(struct gspca_dev
*gspca_dev
, u8 reg
, u8 val
)
855 PDEBUG(D_USBO
, "sccb write: %02x %02x", reg
, val
);
856 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
857 ov534_reg_write(gspca_dev
, OV534_REG_WRITE
, val
);
858 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_3
);
860 if (!sccb_check_status(gspca_dev
)) {
861 err("sccb_reg_write failed");
862 gspca_dev
->usb_err
= -EIO
;
866 static u8
sccb_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
868 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
869 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_2
);
870 if (!sccb_check_status(gspca_dev
))
871 err("sccb_reg_read failed 1");
873 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_READ_2
);
874 if (!sccb_check_status(gspca_dev
))
875 err("sccb_reg_read failed 2");
877 return ov534_reg_read(gspca_dev
, OV534_REG_READ
);
880 /* output a bridge sequence (reg - val) */
881 static void reg_w_array(struct gspca_dev
*gspca_dev
,
882 const u8 (*data
)[2], int len
)
885 ov534_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
890 /* output a sensor sequence (reg - val) */
891 static void sccb_w_array(struct gspca_dev
*gspca_dev
,
892 const u8 (*data
)[2], int len
)
895 if ((*data
)[0] != 0xff) {
896 sccb_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
898 sccb_reg_read(gspca_dev
, (*data
)[1]);
899 sccb_reg_write(gspca_dev
, 0xff, 0x00);
905 /* ov772x specific controls */
906 static void set_frame_rate(struct gspca_dev
*gspca_dev
)
908 struct sd
*sd
= (struct sd
*) gspca_dev
;
916 const struct rate_s
*r
;
917 static const struct rate_s rate_0
[] = { /* 640x480 */
918 {60, 0x01, 0xc1, 0x04},
919 {50, 0x01, 0x41, 0x02},
920 {40, 0x02, 0xc1, 0x04},
921 {30, 0x04, 0x81, 0x02},
922 {15, 0x03, 0x41, 0x04},
924 static const struct rate_s rate_1
[] = { /* 320x240 */
925 {125, 0x02, 0x81, 0x02},
926 {100, 0x02, 0xc1, 0x04},
927 {75, 0x03, 0xc1, 0x04},
928 {60, 0x04, 0xc1, 0x04},
929 {50, 0x02, 0x41, 0x04},
930 {40, 0x03, 0x41, 0x04},
931 {30, 0x04, 0x41, 0x04},
934 if (sd
->sensor
!= SENSOR_OV772x
)
936 if (gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
== 0) {
938 i
= ARRAY_SIZE(rate_0
);
941 i
= ARRAY_SIZE(rate_1
);
944 if (sd
->frame_rate
>= r
->fps
)
949 sccb_reg_write(gspca_dev
, 0x11, r
->r11
);
950 sccb_reg_write(gspca_dev
, 0x0d, r
->r0d
);
951 ov534_reg_write(gspca_dev
, 0xe5, r
->re5
);
953 PDEBUG(D_PROBE
, "frame_rate: %d", r
->fps
);
956 static void setbrightness(struct gspca_dev
*gspca_dev
)
958 struct sd
*sd
= (struct sd
*) gspca_dev
;
961 val
= sd
->ctrls
[BRIGHTNESS
].val
;
962 if (sd
->sensor
== SENSOR_OV767x
) {
965 sccb_reg_write(gspca_dev
, 0x55, val
); /* bright */
967 sccb_reg_write(gspca_dev
, 0x9b, val
);
971 static void setcontrast(struct gspca_dev
*gspca_dev
)
973 struct sd
*sd
= (struct sd
*) gspca_dev
;
976 val
= sd
->ctrls
[CONTRAST
].val
;
977 if (sd
->sensor
== SENSOR_OV767x
)
978 sccb_reg_write(gspca_dev
, 0x56, val
); /* contras */
980 sccb_reg_write(gspca_dev
, 0x9c, val
);
983 static void setgain(struct gspca_dev
*gspca_dev
)
985 struct sd
*sd
= (struct sd
*) gspca_dev
;
988 if (sd
->ctrls
[AGC
].val
)
991 val
= sd
->ctrls
[GAIN
].val
;
992 switch (val
& 0x30) {
1010 sccb_reg_write(gspca_dev
, 0x00, val
);
1013 static void setexposure(struct gspca_dev
*gspca_dev
)
1015 struct sd
*sd
= (struct sd
*) gspca_dev
;
1018 if (sd
->ctrls
[AEC
].val
)
1021 val
= sd
->ctrls
[EXPOSURE
].val
;
1022 if (sd
->sensor
== SENSOR_OV767x
) {
1024 /* set only aec[9:2] */
1025 sccb_reg_write(gspca_dev
, 0x10, val
); /* aech */
1028 /* 'val' is one byte and represents half of the exposure value
1029 * we are going to set into registers, a two bytes value:
1031 * MSB: ((u16) val << 1) >> 8 == val >> 7
1032 * LSB: ((u16) val << 1) & 0xff == val << 1
1034 sccb_reg_write(gspca_dev
, 0x08, val
>> 7);
1035 sccb_reg_write(gspca_dev
, 0x10, val
<< 1);
1039 static void setagc(struct gspca_dev
*gspca_dev
)
1041 struct sd
*sd
= (struct sd
*) gspca_dev
;
1043 if (sd
->ctrls
[AGC
].val
) {
1044 sccb_reg_write(gspca_dev
, 0x13,
1045 sccb_reg_read(gspca_dev
, 0x13) | 0x04);
1046 sccb_reg_write(gspca_dev
, 0x64,
1047 sccb_reg_read(gspca_dev
, 0x64) | 0x03);
1049 sccb_reg_write(gspca_dev
, 0x13,
1050 sccb_reg_read(gspca_dev
, 0x13) & ~0x04);
1051 sccb_reg_write(gspca_dev
, 0x64,
1052 sccb_reg_read(gspca_dev
, 0x64) & ~0x03);
1058 static void setawb(struct gspca_dev
*gspca_dev
)
1060 struct sd
*sd
= (struct sd
*) gspca_dev
;
1062 if (sd
->ctrls
[AWB
].val
) {
1063 sccb_reg_write(gspca_dev
, 0x13,
1064 sccb_reg_read(gspca_dev
, 0x13) | 0x02);
1065 if (sd
->sensor
== SENSOR_OV772x
)
1066 sccb_reg_write(gspca_dev
, 0x63,
1067 sccb_reg_read(gspca_dev
, 0x63) | 0xc0);
1069 sccb_reg_write(gspca_dev
, 0x13,
1070 sccb_reg_read(gspca_dev
, 0x13) & ~0x02);
1071 if (sd
->sensor
== SENSOR_OV772x
)
1072 sccb_reg_write(gspca_dev
, 0x63,
1073 sccb_reg_read(gspca_dev
, 0x63) & ~0xc0);
1077 static void setaec(struct gspca_dev
*gspca_dev
)
1079 struct sd
*sd
= (struct sd
*) gspca_dev
;
1082 data
= sd
->sensor
== SENSOR_OV767x
?
1083 0x05 : /* agc + aec */
1085 if (sd
->ctrls
[AEC
].val
)
1086 sccb_reg_write(gspca_dev
, 0x13,
1087 sccb_reg_read(gspca_dev
, 0x13) | data
);
1089 sccb_reg_write(gspca_dev
, 0x13,
1090 sccb_reg_read(gspca_dev
, 0x13) & ~data
);
1091 if (sd
->sensor
== SENSOR_OV767x
)
1092 sd
->ctrls
[EXPOSURE
].val
=
1093 sccb_reg_read(gspca_dev
, 10); /* aech */
1095 setexposure(gspca_dev
);
1099 static void setsharpness(struct gspca_dev
*gspca_dev
)
1101 struct sd
*sd
= (struct sd
*) gspca_dev
;
1104 val
= sd
->ctrls
[SHARPNESS
].val
;
1105 sccb_reg_write(gspca_dev
, 0x91, val
); /* Auto de-noise threshold */
1106 sccb_reg_write(gspca_dev
, 0x8e, val
); /* De-noise threshold */
1109 static void sethvflip(struct gspca_dev
*gspca_dev
)
1111 struct sd
*sd
= (struct sd
*) gspca_dev
;
1114 if (sd
->sensor
== SENSOR_OV767x
) {
1115 val
= sccb_reg_read(gspca_dev
, 0x1e); /* mvfp */
1117 if (sd
->ctrls
[HFLIP
].val
)
1119 if (sd
->ctrls
[VFLIP
].val
)
1121 sccb_reg_write(gspca_dev
, 0x1e, val
);
1123 val
= sccb_reg_read(gspca_dev
, 0x0c);
1125 if (sd
->ctrls
[HFLIP
].val
== 0)
1127 if (sd
->ctrls
[VFLIP
].val
== 0)
1129 sccb_reg_write(gspca_dev
, 0x0c, val
);
1133 static void setcolors(struct gspca_dev
*gspca_dev
)
1135 struct sd
*sd
= (struct sd
*) gspca_dev
;
1138 static u8 color_tb
[][6] = {
1139 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1140 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1141 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1142 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1143 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1144 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1145 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1148 val
= sd
->ctrls
[COLORS
].val
;
1149 for (i
= 0; i
< ARRAY_SIZE(color_tb
[0]); i
++)
1150 sccb_reg_write(gspca_dev
, 0x4f + i
, color_tb
[val
][i
]);
1153 static void setlightfreq(struct gspca_dev
*gspca_dev
)
1155 struct sd
*sd
= (struct sd
*) gspca_dev
;
1158 val
= sd
->ctrls
[LIGHTFREQ
].val
? 0x9e : 0x00;
1159 if (sd
->sensor
== SENSOR_OV767x
) {
1160 sccb_reg_write(gspca_dev
, 0x2a, 0x00);
1162 val
= 0x9d; /* insert dummy to 25fps for 50Hz */
1164 sccb_reg_write(gspca_dev
, 0x2b, val
);
1168 /* this function is called at probe time */
1169 static int sd_config(struct gspca_dev
*gspca_dev
,
1170 const struct usb_device_id
*id
)
1172 struct sd
*sd
= (struct sd
*) gspca_dev
;
1175 cam
= &gspca_dev
->cam
;
1177 cam
->ctrls
= sd
->ctrls
;
1179 /* the auto white balance control works only when auto gain is set */
1180 if (sd_ctrls
[AGC
].qctrl
.default_value
== 0)
1181 gspca_dev
->ctrl_inac
|= (1 << AWB
);
1183 cam
->cam_mode
= ov772x_mode
;
1184 cam
->nmodes
= ARRAY_SIZE(ov772x_mode
);
1186 sd
->frame_rate
= 30;
1191 /* this function is called at probe and resume time */
1192 static int sd_init(struct gspca_dev
*gspca_dev
)
1194 struct sd
*sd
= (struct sd
*) gspca_dev
;
1196 static const struct reg_array bridge_init
[NSENSORS
] = {
1197 [SENSOR_OV767x
] = {bridge_init_767x
, ARRAY_SIZE(bridge_init_767x
)},
1198 [SENSOR_OV772x
] = {bridge_init_772x
, ARRAY_SIZE(bridge_init_772x
)},
1200 static const struct reg_array sensor_init
[NSENSORS
] = {
1201 [SENSOR_OV767x
] = {sensor_init_767x
, ARRAY_SIZE(sensor_init_767x
)},
1202 [SENSOR_OV772x
] = {sensor_init_772x
, ARRAY_SIZE(sensor_init_772x
)},
1206 ov534_reg_write(gspca_dev
, 0xe7, 0x3a);
1207 ov534_reg_write(gspca_dev
, 0xe0, 0x08);
1210 /* initialize the sensor address */
1211 ov534_reg_write(gspca_dev
, OV534_REG_ADDRESS
, 0x42);
1214 sccb_reg_write(gspca_dev
, 0x12, 0x80);
1217 /* probe the sensor */
1218 sccb_reg_read(gspca_dev
, 0x0a);
1219 sensor_id
= sccb_reg_read(gspca_dev
, 0x0a) << 8;
1220 sccb_reg_read(gspca_dev
, 0x0b);
1221 sensor_id
|= sccb_reg_read(gspca_dev
, 0x0b);
1222 PDEBUG(D_PROBE
, "Sensor ID: %04x", sensor_id
);
1224 if ((sensor_id
& 0xfff0) == 0x7670) {
1225 sd
->sensor
= SENSOR_OV767x
;
1226 gspca_dev
->ctrl_dis
= (1 << GAIN
) |
1228 (1 << SHARPNESS
); /* auto */
1229 sd
->ctrls
[BRIGHTNESS
].min
= -127;
1230 sd
->ctrls
[BRIGHTNESS
].max
= 127;
1231 sd
->ctrls
[BRIGHTNESS
].def
= 0;
1232 sd
->ctrls
[CONTRAST
].max
= 0x80;
1233 sd
->ctrls
[CONTRAST
].def
= 0x40;
1234 sd
->ctrls
[EXPOSURE
].min
= 0x08;
1235 sd
->ctrls
[EXPOSURE
].max
= 0x60;
1236 sd
->ctrls
[EXPOSURE
].def
= 0x13;
1237 sd
->ctrls
[SHARPNESS
].max
= 9;
1238 sd
->ctrls
[SHARPNESS
].def
= 4;
1239 sd
->ctrls
[HFLIP
].def
= 1;
1240 gspca_dev
->cam
.cam_mode
= ov767x_mode
;
1241 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(ov767x_mode
);
1243 sd
->sensor
= SENSOR_OV772x
;
1244 gspca_dev
->ctrl_dis
= (1 << COLORS
);
1245 gspca_dev
->cam
.bulk
= 1;
1246 gspca_dev
->cam
.bulk_size
= 16384;
1247 gspca_dev
->cam
.bulk_nurbs
= 2;
1248 gspca_dev
->cam
.mode_framerates
= ov772x_framerates
;
1252 reg_w_array(gspca_dev
, bridge_init
[sd
->sensor
].val
,
1253 bridge_init
[sd
->sensor
].len
);
1254 ov534_set_led(gspca_dev
, 1);
1255 sccb_w_array(gspca_dev
, sensor_init
[sd
->sensor
].val
,
1256 sensor_init
[sd
->sensor
].len
);
1257 if (sd
->sensor
== SENSOR_OV767x
)
1258 sd_start(gspca_dev
);
1259 sd_stopN(gspca_dev
);
1260 /* set_frame_rate(gspca_dev); */
1262 return gspca_dev
->usb_err
;
1265 static int sd_start(struct gspca_dev
*gspca_dev
)
1267 struct sd
*sd
= (struct sd
*) gspca_dev
;
1269 static const struct reg_array bridge_start
[NSENSORS
][2] = {
1270 [SENSOR_OV767x
] = {{bridge_start_qvga_767x
,
1271 ARRAY_SIZE(bridge_start_qvga_767x
)},
1272 {bridge_start_vga_767x
,
1273 ARRAY_SIZE(bridge_start_vga_767x
)}},
1274 [SENSOR_OV772x
] = {{bridge_start_qvga_772x
,
1275 ARRAY_SIZE(bridge_start_qvga_772x
)},
1276 {bridge_start_vga_772x
,
1277 ARRAY_SIZE(bridge_start_vga_772x
)}},
1279 static const struct reg_array sensor_start
[NSENSORS
][2] = {
1280 [SENSOR_OV767x
] = {{sensor_start_qvga_767x
,
1281 ARRAY_SIZE(sensor_start_qvga_767x
)},
1282 {sensor_start_vga_767x
,
1283 ARRAY_SIZE(sensor_start_vga_767x
)}},
1284 [SENSOR_OV772x
] = {{sensor_start_qvga_772x
,
1285 ARRAY_SIZE(sensor_start_qvga_772x
)},
1286 {sensor_start_vga_772x
,
1287 ARRAY_SIZE(sensor_start_vga_772x
)}},
1290 /* (from ms-win trace) */
1291 if (sd
->sensor
== SENSOR_OV767x
)
1292 sccb_reg_write(gspca_dev
, 0x1e, 0x04);
1293 /* black sun enable ? */
1295 mode
= gspca_dev
->curr_mode
; /* 0: 320x240, 1: 640x480 */
1296 reg_w_array(gspca_dev
, bridge_start
[sd
->sensor
][mode
].val
,
1297 bridge_start
[sd
->sensor
][mode
].len
);
1298 sccb_w_array(gspca_dev
, sensor_start
[sd
->sensor
][mode
].val
,
1299 sensor_start
[sd
->sensor
][mode
].len
);
1301 set_frame_rate(gspca_dev
);
1303 if (!(gspca_dev
->ctrl_dis
& (1 << AGC
)))
1307 if (!(gspca_dev
->ctrl_dis
& (1 << GAIN
)))
1309 setexposure(gspca_dev
);
1310 setbrightness(gspca_dev
);
1311 setcontrast(gspca_dev
);
1312 if (!(gspca_dev
->ctrl_dis
& (1 << SHARPNESS
)))
1313 setsharpness(gspca_dev
);
1314 sethvflip(gspca_dev
);
1315 if (!(gspca_dev
->ctrl_dis
& (1 << COLORS
)))
1316 setcolors(gspca_dev
);
1317 setlightfreq(gspca_dev
);
1319 ov534_set_led(gspca_dev
, 1);
1320 ov534_reg_write(gspca_dev
, 0xe0, 0x00);
1321 return gspca_dev
->usb_err
;
1324 static void sd_stopN(struct gspca_dev
*gspca_dev
)
1326 ov534_reg_write(gspca_dev
, 0xe0, 0x09);
1327 ov534_set_led(gspca_dev
, 0);
1330 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1331 #define UVC_STREAM_EOH (1 << 7)
1332 #define UVC_STREAM_ERR (1 << 6)
1333 #define UVC_STREAM_STI (1 << 5)
1334 #define UVC_STREAM_RES (1 << 4)
1335 #define UVC_STREAM_SCR (1 << 3)
1336 #define UVC_STREAM_PTS (1 << 2)
1337 #define UVC_STREAM_EOF (1 << 1)
1338 #define UVC_STREAM_FID (1 << 0)
1340 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1343 struct sd
*sd
= (struct sd
*) gspca_dev
;
1346 int remaining_len
= len
;
1349 payload_len
= gspca_dev
->cam
.bulk
? 2048 : 2040;
1351 len
= min(remaining_len
, payload_len
);
1353 /* Payloads are prefixed with a UVC-style header. We
1354 consider a frame to start when the FID toggles, or the PTS
1355 changes. A frame ends when EOF is set, and we've received
1356 the correct number of bytes. */
1358 /* Verify UVC header. Header length is always 12 */
1359 if (data
[0] != 12 || len
< 12) {
1360 PDEBUG(D_PACK
, "bad header");
1365 if (data
[1] & UVC_STREAM_ERR
) {
1366 PDEBUG(D_PACK
, "payload error");
1370 /* Extract PTS and FID */
1371 if (!(data
[1] & UVC_STREAM_PTS
)) {
1372 PDEBUG(D_PACK
, "PTS not present");
1375 this_pts
= (data
[5] << 24) | (data
[4] << 16)
1376 | (data
[3] << 8) | data
[2];
1377 this_fid
= (data
[1] & UVC_STREAM_FID
) ? 1 : 0;
1379 /* If PTS or FID has changed, start a new frame. */
1380 if (this_pts
!= sd
->last_pts
|| this_fid
!= sd
->last_fid
) {
1381 if (gspca_dev
->last_packet_type
== INTER_PACKET
)
1382 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1384 sd
->last_pts
= this_pts
;
1385 sd
->last_fid
= this_fid
;
1386 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1387 data
+ 12, len
- 12);
1388 /* If this packet is marked as EOF, end the frame */
1389 } else if (data
[1] & UVC_STREAM_EOF
) {
1391 if (gspca_dev
->pixfmt
== V4L2_PIX_FMT_YUYV
1392 && gspca_dev
->image_len
+ len
- 12 !=
1393 gspca_dev
->width
* gspca_dev
->height
* 2) {
1394 PDEBUG(D_PACK
, "wrong sized frame");
1397 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1398 data
+ 12, len
- 12);
1401 /* Add the data from this payload */
1402 gspca_frame_add(gspca_dev
, INTER_PACKET
,
1403 data
+ 12, len
- 12);
1406 /* Done this payload */
1410 /* Discard data until a new frame starts. */
1411 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1414 remaining_len
-= len
;
1416 } while (remaining_len
> 0);
1419 static int sd_setagc(struct gspca_dev
*gspca_dev
, __s32 val
)
1421 struct sd
*sd
= (struct sd
*) gspca_dev
;
1423 sd
->ctrls
[AGC
].val
= val
;
1425 /* the auto white balance control works only
1426 * when auto gain is set */
1428 gspca_dev
->ctrl_inac
&= ~(1 << AWB
);
1430 gspca_dev
->ctrl_inac
|= (1 << AWB
);
1431 if (sd
->ctrls
[AWB
].val
) {
1432 sd
->ctrls
[AWB
].val
= 0;
1433 if (gspca_dev
->streaming
)
1437 if (gspca_dev
->streaming
)
1439 return gspca_dev
->usb_err
;
1442 static int sd_querymenu(struct gspca_dev
*gspca_dev
,
1443 struct v4l2_querymenu
*menu
)
1446 case V4L2_CID_POWER_LINE_FREQUENCY
:
1447 switch (menu
->index
) {
1448 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1449 strcpy((char *) menu
->name
, "Disabled");
1451 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1452 strcpy((char *) menu
->name
, "50 Hz");
1461 /* get stream parameters (framerate) */
1462 static void sd_get_streamparm(struct gspca_dev
*gspca_dev
,
1463 struct v4l2_streamparm
*parm
)
1465 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1466 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1467 struct sd
*sd
= (struct sd
*) gspca_dev
;
1469 cp
->capability
|= V4L2_CAP_TIMEPERFRAME
;
1471 tpf
->denominator
= sd
->frame_rate
;
1474 /* set stream parameters (framerate) */
1475 static void sd_set_streamparm(struct gspca_dev
*gspca_dev
,
1476 struct v4l2_streamparm
*parm
)
1478 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1479 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1480 struct sd
*sd
= (struct sd
*) gspca_dev
;
1482 /* Set requested framerate */
1483 sd
->frame_rate
= tpf
->denominator
/ tpf
->numerator
;
1484 if (gspca_dev
->streaming
)
1485 set_frame_rate(gspca_dev
);
1487 /* Return the actual framerate */
1489 tpf
->denominator
= sd
->frame_rate
;
1492 /* sub-driver description */
1493 static const struct sd_desc sd_desc
= {
1494 .name
= MODULE_NAME
,
1496 .nctrls
= ARRAY_SIZE(sd_ctrls
),
1497 .config
= sd_config
,
1501 .pkt_scan
= sd_pkt_scan
,
1502 .querymenu
= sd_querymenu
,
1503 .get_streamparm
= sd_get_streamparm
,
1504 .set_streamparm
= sd_set_streamparm
,
1507 /* -- module initialisation -- */
1508 static const struct usb_device_id device_table
[] = {
1509 {USB_DEVICE(0x1415, 0x2000)},
1510 {USB_DEVICE(0x06f8, 0x3002)},
1514 MODULE_DEVICE_TABLE(usb
, device_table
);
1516 /* -- device connect -- */
1517 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1519 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1523 static struct usb_driver sd_driver
= {
1524 .name
= MODULE_NAME
,
1525 .id_table
= device_table
,
1527 .disconnect
= gspca_disconnect
,
1529 .suspend
= gspca_suspend
,
1530 .resume
= gspca_resume
,
1534 /* -- module insert / remove -- */
1535 static int __init
sd_mod_init(void)
1537 return usb_register(&sd_driver
);
1540 static void __exit
sd_mod_exit(void)
1542 usb_deregister(&sd_driver
);
1545 module_init(sd_mod_init
);
1546 module_exit(sd_mod_exit
);