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 #include <linux/fixp-arith.h>
38 #include <media/v4l2-ctrls.h>
40 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
41 #define OV534_REG_SUBADDR 0xf2
42 #define OV534_REG_WRITE 0xf3
43 #define OV534_REG_READ 0xf4
44 #define OV534_REG_OPERATION 0xf5
45 #define OV534_REG_STATUS 0xf6
47 #define OV534_OP_WRITE_3 0x37
48 #define OV534_OP_WRITE_2 0x33
49 #define OV534_OP_READ_2 0xf9
51 #define CTRL_TIMEOUT 500
53 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
54 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
55 MODULE_LICENSE("GPL");
57 /* specific webcam descriptor */
59 struct gspca_dev gspca_dev
; /* !! must be the first item */
61 struct v4l2_ctrl_handler ctrl_handler
;
62 struct v4l2_ctrl
*hue
;
63 struct v4l2_ctrl
*saturation
;
64 struct v4l2_ctrl
*brightness
;
65 struct v4l2_ctrl
*contrast
;
66 struct { /* gain control cluster */
67 struct v4l2_ctrl
*autogain
;
68 struct v4l2_ctrl
*gain
;
70 struct v4l2_ctrl
*autowhitebalance
;
71 struct { /* exposure control cluster */
72 struct v4l2_ctrl
*autoexposure
;
73 struct v4l2_ctrl
*exposure
;
75 struct v4l2_ctrl
*sharpness
;
76 struct v4l2_ctrl
*hflip
;
77 struct v4l2_ctrl
*vflip
;
78 struct v4l2_ctrl
*plfreq
;
92 static int sd_start(struct gspca_dev
*gspca_dev
);
93 static void sd_stopN(struct gspca_dev
*gspca_dev
);
96 static const struct v4l2_pix_format ov772x_mode
[] = {
97 {320, 240, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
98 .bytesperline
= 320 * 2,
99 .sizeimage
= 320 * 240 * 2,
100 .colorspace
= V4L2_COLORSPACE_SRGB
,
102 {640, 480, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
103 .bytesperline
= 640 * 2,
104 .sizeimage
= 640 * 480 * 2,
105 .colorspace
= V4L2_COLORSPACE_SRGB
,
108 static const struct v4l2_pix_format ov767x_mode
[] = {
109 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
111 .sizeimage
= 320 * 240 * 3 / 8 + 590,
112 .colorspace
= V4L2_COLORSPACE_JPEG
},
113 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
115 .sizeimage
= 640 * 480 * 3 / 8 + 590,
116 .colorspace
= V4L2_COLORSPACE_JPEG
},
119 static const u8 qvga_rates
[] = {125, 100, 75, 60, 50, 40, 30};
120 static const u8 vga_rates
[] = {60, 50, 40, 30, 15};
122 static const struct framerates ov772x_framerates
[] = {
125 .nrates
= ARRAY_SIZE(qvga_rates
),
129 .nrates
= ARRAY_SIZE(vga_rates
),
138 static const u8 bridge_init_767x
[][2] = {
139 /* comments from the ms-win file apollo7670.set */
169 {0xc0, 0x50}, /* HSize 640 */
170 {0xc1, 0x3c}, /* VSize 480 */
171 {0x34, 0x05}, /* enable Audio Suspend mode */
172 {0xc2, 0x0c}, /* Input YUV */
173 {0xc3, 0xf9}, /* enable PRE */
174 {0x34, 0x05}, /* enable Audio Suspend mode */
175 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
176 {0x31, 0xf9}, /* enable 1.8V Suspend */
177 {0x35, 0x02}, /* turn on JPEG */
179 {0x25, 0x42}, /* GPIO[8]:Input */
180 {0x94, 0x11}, /* If the default setting is loaded when
181 * system boots up, this flag is closed here */
183 static const u8 sensor_init_767x
[][2] = {
201 {0x7a, 0x2a}, /* set Gamma=1.6 below */
221 {0x14, 0x38}, /* gain max 16x */
301 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
305 {0xa4, 0x8a}, /* Night mode trigger point */
338 static const u8 bridge_start_vga_767x
[][2] = {
346 {0x35, 0x02}, /* turn on JPEG */
348 {0xda, 0x00}, /* for higher clock rate(30fps) */
349 {0x34, 0x05}, /* enable Audio Suspend mode */
350 {0xc3, 0xf9}, /* enable PRE */
351 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
352 {0x8d, 0x1c}, /* output YUV */
353 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
354 {0x50, 0x00}, /* H/V divider=0 */
355 {0x51, 0xa0}, /* input H=640/4 */
356 {0x52, 0x3c}, /* input V=480/4 */
357 {0x53, 0x00}, /* offset X=0 */
358 {0x54, 0x00}, /* offset Y=0 */
359 {0x55, 0x00}, /* H/V size[8]=0 */
360 {0x57, 0x00}, /* H-size[9]=0 */
361 {0x5c, 0x00}, /* output size[9:8]=0 */
362 {0x5a, 0xa0}, /* output H=640/4 */
363 {0x5b, 0x78}, /* output V=480/4 */
368 static const u8 sensor_start_vga_767x
[][2] = {
374 static const u8 bridge_start_qvga_767x
[][2] = {
382 {0x35, 0x02}, /* turn on JPEG */
384 {0xc0, 0x50}, /* CIF HSize 640 */
385 {0xc1, 0x3c}, /* CIF VSize 480 */
386 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
387 {0x8d, 0x1c}, /* output YUV */
388 {0x34, 0x05}, /* enable Audio Suspend mode */
389 {0xc2, 0x4c}, /* output YUV and Enable DCW */
390 {0xc3, 0xf9}, /* enable PRE */
391 {0x1c, 0x00}, /* indirect addressing */
392 {0x1d, 0x48}, /* output YUV422 */
393 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
394 {0x51, 0xa0}, /* DCW input H=640/4 */
395 {0x52, 0x78}, /* DCW input V=480/4 */
396 {0x53, 0x00}, /* offset X=0 */
397 {0x54, 0x00}, /* offset Y=0 */
398 {0x55, 0x00}, /* H/V size[8]=0 */
399 {0x57, 0x00}, /* H-size[9]=0 */
400 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
401 {0x5a, 0x50}, /* DCW output H=320/4 */
402 {0x5b, 0x3c}, /* DCW output V=240/4 */
407 static const u8 sensor_start_qvga_767x
[][2] = {
414 static const u8 bridge_init_772x
[][2] = {
453 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
454 { 0x1d, 0x00 }, /* payload size */
456 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
457 { 0x1d, 0x58 }, /* frame size */
458 { 0x1d, 0x00 }, /* frame size */
461 { 0x1d, 0x08 }, /* turn on UVC header */
462 { 0x1d, 0x0e }, /* .. */
472 static const u8 sensor_init_772x
[][2] = {
475 /*fixme: better have a delay?*/
498 { 0x63, 0xaa }, /* AWB - was e0 */
501 { 0x13, 0xf0 }, /* com8 */
514 { 0x13, 0xff }, /* AWB */
562 { 0x8e, 0x00 }, /* De-noise threshold */
565 static const u8 bridge_start_vga_772x
[][2] = {
576 static const u8 sensor_start_vga_772x
[][2] = {
586 static const u8 bridge_start_qvga_772x
[][2] = {
597 static const u8 sensor_start_qvga_772x
[][2] = {
608 static void ov534_reg_write(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
610 struct usb_device
*udev
= gspca_dev
->dev
;
613 if (gspca_dev
->usb_err
< 0)
616 PDEBUG(D_USBO
, "SET 01 0000 %04x %02x", reg
, val
);
617 gspca_dev
->usb_buf
[0] = val
;
618 ret
= usb_control_msg(udev
,
619 usb_sndctrlpipe(udev
, 0),
621 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
622 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
624 pr_err("write failed %d\n", ret
);
625 gspca_dev
->usb_err
= ret
;
629 static u8
ov534_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
631 struct usb_device
*udev
= gspca_dev
->dev
;
634 if (gspca_dev
->usb_err
< 0)
636 ret
= usb_control_msg(udev
,
637 usb_rcvctrlpipe(udev
, 0),
639 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
640 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
641 PDEBUG(D_USBI
, "GET 01 0000 %04x %02x", reg
, gspca_dev
->usb_buf
[0]);
643 pr_err("read failed %d\n", ret
);
644 gspca_dev
->usb_err
= ret
;
646 return gspca_dev
->usb_buf
[0];
649 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
650 * (direction and output)? */
651 static void ov534_set_led(struct gspca_dev
*gspca_dev
, int status
)
655 PDEBUG(D_CONF
, "led status: %d", status
);
657 data
= ov534_reg_read(gspca_dev
, 0x21);
659 ov534_reg_write(gspca_dev
, 0x21, data
);
661 data
= ov534_reg_read(gspca_dev
, 0x23);
667 ov534_reg_write(gspca_dev
, 0x23, data
);
670 data
= ov534_reg_read(gspca_dev
, 0x21);
672 ov534_reg_write(gspca_dev
, 0x21, data
);
676 static int sccb_check_status(struct gspca_dev
*gspca_dev
)
681 for (i
= 0; i
< 5; i
++) {
683 data
= ov534_reg_read(gspca_dev
, OV534_REG_STATUS
);
693 PERR("sccb status 0x%02x, attempt %d/5",
700 static void sccb_reg_write(struct gspca_dev
*gspca_dev
, u8 reg
, u8 val
)
702 PDEBUG(D_USBO
, "sccb write: %02x %02x", reg
, val
);
703 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
704 ov534_reg_write(gspca_dev
, OV534_REG_WRITE
, val
);
705 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_3
);
707 if (!sccb_check_status(gspca_dev
)) {
708 pr_err("sccb_reg_write failed\n");
709 gspca_dev
->usb_err
= -EIO
;
713 static u8
sccb_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
715 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
716 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_2
);
717 if (!sccb_check_status(gspca_dev
))
718 pr_err("sccb_reg_read failed 1\n");
720 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_READ_2
);
721 if (!sccb_check_status(gspca_dev
))
722 pr_err("sccb_reg_read failed 2\n");
724 return ov534_reg_read(gspca_dev
, OV534_REG_READ
);
727 /* output a bridge sequence (reg - val) */
728 static void reg_w_array(struct gspca_dev
*gspca_dev
,
729 const u8 (*data
)[2], int len
)
732 ov534_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
737 /* output a sensor sequence (reg - val) */
738 static void sccb_w_array(struct gspca_dev
*gspca_dev
,
739 const u8 (*data
)[2], int len
)
742 if ((*data
)[0] != 0xff) {
743 sccb_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
745 sccb_reg_read(gspca_dev
, (*data
)[1]);
746 sccb_reg_write(gspca_dev
, 0xff, 0x00);
752 /* ov772x specific controls */
753 static void set_frame_rate(struct gspca_dev
*gspca_dev
)
755 struct sd
*sd
= (struct sd
*) gspca_dev
;
763 const struct rate_s
*r
;
764 static const struct rate_s rate_0
[] = { /* 640x480 */
765 {60, 0x01, 0xc1, 0x04},
766 {50, 0x01, 0x41, 0x02},
767 {40, 0x02, 0xc1, 0x04},
768 {30, 0x04, 0x81, 0x02},
769 {15, 0x03, 0x41, 0x04},
771 static const struct rate_s rate_1
[] = { /* 320x240 */
772 {125, 0x02, 0x81, 0x02},
773 {100, 0x02, 0xc1, 0x04},
774 {75, 0x03, 0xc1, 0x04},
775 {60, 0x04, 0xc1, 0x04},
776 {50, 0x02, 0x41, 0x04},
777 {40, 0x03, 0x41, 0x04},
778 {30, 0x04, 0x41, 0x04},
781 if (sd
->sensor
!= SENSOR_OV772x
)
783 if (gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
== 0) {
785 i
= ARRAY_SIZE(rate_0
);
788 i
= ARRAY_SIZE(rate_1
);
791 if (sd
->frame_rate
>= r
->fps
)
796 sccb_reg_write(gspca_dev
, 0x11, r
->r11
);
797 sccb_reg_write(gspca_dev
, 0x0d, r
->r0d
);
798 ov534_reg_write(gspca_dev
, 0xe5, r
->re5
);
800 PDEBUG(D_PROBE
, "frame_rate: %d", r
->fps
);
803 static void sethue(struct gspca_dev
*gspca_dev
, s32 val
)
805 struct sd
*sd
= (struct sd
*) gspca_dev
;
807 if (sd
->sensor
== SENSOR_OV767x
) {
813 /* fixp_sin and fixp_cos accept only positive values, while
814 * our val is between -90 and 90
818 /* According to the datasheet the registers expect HUESIN and
819 * HUECOS to be the result of the trigonometric functions,
822 * The 0x100 here represents the maximun absolute value
823 * returned byt fixp_sin and fixp_cos, so the scaling will
824 * consider the result like in the interval [-1.0, 1.0].
826 huesin
= fixp_sin(val
) * 0x80 / 0x100;
827 huecos
= fixp_cos(val
) * 0x80 / 0x100;
830 sccb_reg_write(gspca_dev
, 0xab,
831 sccb_reg_read(gspca_dev
, 0xab) | 0x2);
834 sccb_reg_write(gspca_dev
, 0xab,
835 sccb_reg_read(gspca_dev
, 0xab) & ~0x2);
838 sccb_reg_write(gspca_dev
, 0xa9, (u8
)huecos
);
839 sccb_reg_write(gspca_dev
, 0xaa, (u8
)huesin
);
843 static void setsaturation(struct gspca_dev
*gspca_dev
, s32 val
)
845 struct sd
*sd
= (struct sd
*) gspca_dev
;
847 if (sd
->sensor
== SENSOR_OV767x
) {
849 static u8 color_tb
[][6] = {
850 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
851 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
852 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
853 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
854 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
855 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
856 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
859 for (i
= 0; i
< ARRAY_SIZE(color_tb
[0]); i
++)
860 sccb_reg_write(gspca_dev
, 0x4f + i
, color_tb
[val
][i
]);
862 sccb_reg_write(gspca_dev
, 0xa7, val
); /* U saturation */
863 sccb_reg_write(gspca_dev
, 0xa8, val
); /* V saturation */
867 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
869 struct sd
*sd
= (struct sd
*) gspca_dev
;
871 if (sd
->sensor
== SENSOR_OV767x
) {
874 sccb_reg_write(gspca_dev
, 0x55, val
); /* bright */
876 sccb_reg_write(gspca_dev
, 0x9b, val
);
880 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
882 struct sd
*sd
= (struct sd
*) gspca_dev
;
884 if (sd
->sensor
== SENSOR_OV767x
)
885 sccb_reg_write(gspca_dev
, 0x56, val
); /* contras */
887 sccb_reg_write(gspca_dev
, 0x9c, val
);
890 static void setgain(struct gspca_dev
*gspca_dev
, s32 val
)
892 switch (val
& 0x30) {
910 sccb_reg_write(gspca_dev
, 0x00, val
);
913 static s32
getgain(struct gspca_dev
*gspca_dev
)
915 return sccb_reg_read(gspca_dev
, 0x00);
918 static void setexposure(struct gspca_dev
*gspca_dev
, s32 val
)
920 struct sd
*sd
= (struct sd
*) gspca_dev
;
922 if (sd
->sensor
== SENSOR_OV767x
) {
924 /* set only aec[9:2] */
925 sccb_reg_write(gspca_dev
, 0x10, val
); /* aech */
928 /* 'val' is one byte and represents half of the exposure value
929 * we are going to set into registers, a two bytes value:
931 * MSB: ((u16) val << 1) >> 8 == val >> 7
932 * LSB: ((u16) val << 1) & 0xff == val << 1
934 sccb_reg_write(gspca_dev
, 0x08, val
>> 7);
935 sccb_reg_write(gspca_dev
, 0x10, val
<< 1);
939 static s32
getexposure(struct gspca_dev
*gspca_dev
)
941 struct sd
*sd
= (struct sd
*) gspca_dev
;
943 if (sd
->sensor
== SENSOR_OV767x
) {
944 /* get only aec[9:2] */
945 return sccb_reg_read(gspca_dev
, 0x10); /* aech */
947 u8 hi
= sccb_reg_read(gspca_dev
, 0x08);
948 u8 lo
= sccb_reg_read(gspca_dev
, 0x10);
949 return (hi
<< 8 | lo
) >> 1;
953 static void setagc(struct gspca_dev
*gspca_dev
, s32 val
)
956 sccb_reg_write(gspca_dev
, 0x13,
957 sccb_reg_read(gspca_dev
, 0x13) | 0x04);
958 sccb_reg_write(gspca_dev
, 0x64,
959 sccb_reg_read(gspca_dev
, 0x64) | 0x03);
961 sccb_reg_write(gspca_dev
, 0x13,
962 sccb_reg_read(gspca_dev
, 0x13) & ~0x04);
963 sccb_reg_write(gspca_dev
, 0x64,
964 sccb_reg_read(gspca_dev
, 0x64) & ~0x03);
968 static void setawb(struct gspca_dev
*gspca_dev
, s32 val
)
970 struct sd
*sd
= (struct sd
*) gspca_dev
;
973 sccb_reg_write(gspca_dev
, 0x13,
974 sccb_reg_read(gspca_dev
, 0x13) | 0x02);
975 if (sd
->sensor
== SENSOR_OV772x
)
976 sccb_reg_write(gspca_dev
, 0x63,
977 sccb_reg_read(gspca_dev
, 0x63) | 0xc0);
979 sccb_reg_write(gspca_dev
, 0x13,
980 sccb_reg_read(gspca_dev
, 0x13) & ~0x02);
981 if (sd
->sensor
== SENSOR_OV772x
)
982 sccb_reg_write(gspca_dev
, 0x63,
983 sccb_reg_read(gspca_dev
, 0x63) & ~0xc0);
987 static void setaec(struct gspca_dev
*gspca_dev
, s32 val
)
989 struct sd
*sd
= (struct sd
*) gspca_dev
;
992 data
= sd
->sensor
== SENSOR_OV767x
?
993 0x05 : /* agc + aec */
996 case V4L2_EXPOSURE_AUTO
:
997 sccb_reg_write(gspca_dev
, 0x13,
998 sccb_reg_read(gspca_dev
, 0x13) | data
);
1000 case V4L2_EXPOSURE_MANUAL
:
1001 sccb_reg_write(gspca_dev
, 0x13,
1002 sccb_reg_read(gspca_dev
, 0x13) & ~data
);
1007 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
1009 sccb_reg_write(gspca_dev
, 0x91, val
); /* Auto de-noise threshold */
1010 sccb_reg_write(gspca_dev
, 0x8e, val
); /* De-noise threshold */
1013 static void sethvflip(struct gspca_dev
*gspca_dev
, s32 hflip
, s32 vflip
)
1015 struct sd
*sd
= (struct sd
*) gspca_dev
;
1018 if (sd
->sensor
== SENSOR_OV767x
) {
1019 val
= sccb_reg_read(gspca_dev
, 0x1e); /* mvfp */
1025 sccb_reg_write(gspca_dev
, 0x1e, val
);
1027 val
= sccb_reg_read(gspca_dev
, 0x0c);
1033 sccb_reg_write(gspca_dev
, 0x0c, val
);
1037 static void setlightfreq(struct gspca_dev
*gspca_dev
, s32 val
)
1039 struct sd
*sd
= (struct sd
*) gspca_dev
;
1041 val
= val
? 0x9e : 0x00;
1042 if (sd
->sensor
== SENSOR_OV767x
) {
1043 sccb_reg_write(gspca_dev
, 0x2a, 0x00);
1045 val
= 0x9d; /* insert dummy to 25fps for 50Hz */
1047 sccb_reg_write(gspca_dev
, 0x2b, val
);
1051 /* this function is called at probe time */
1052 static int sd_config(struct gspca_dev
*gspca_dev
,
1053 const struct usb_device_id
*id
)
1055 struct sd
*sd
= (struct sd
*) gspca_dev
;
1058 cam
= &gspca_dev
->cam
;
1060 cam
->cam_mode
= ov772x_mode
;
1061 cam
->nmodes
= ARRAY_SIZE(ov772x_mode
);
1063 sd
->frame_rate
= 30;
1068 static int ov534_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1070 struct sd
*sd
= container_of(ctrl
->handler
, struct sd
, ctrl_handler
);
1071 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
1074 case V4L2_CID_AUTOGAIN
:
1075 gspca_dev
->usb_err
= 0;
1076 if (ctrl
->val
&& sd
->gain
&& gspca_dev
->streaming
)
1077 sd
->gain
->val
= getgain(gspca_dev
);
1078 return gspca_dev
->usb_err
;
1080 case V4L2_CID_EXPOSURE_AUTO
:
1081 gspca_dev
->usb_err
= 0;
1082 if (ctrl
->val
== V4L2_EXPOSURE_AUTO
&& sd
->exposure
&&
1083 gspca_dev
->streaming
)
1084 sd
->exposure
->val
= getexposure(gspca_dev
);
1085 return gspca_dev
->usb_err
;
1090 static int ov534_s_ctrl(struct v4l2_ctrl
*ctrl
)
1092 struct sd
*sd
= container_of(ctrl
->handler
, struct sd
, ctrl_handler
);
1093 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
1095 gspca_dev
->usb_err
= 0;
1096 if (!gspca_dev
->streaming
)
1101 sethue(gspca_dev
, ctrl
->val
);
1103 case V4L2_CID_SATURATION
:
1104 setsaturation(gspca_dev
, ctrl
->val
);
1106 case V4L2_CID_BRIGHTNESS
:
1107 setbrightness(gspca_dev
, ctrl
->val
);
1109 case V4L2_CID_CONTRAST
:
1110 setcontrast(gspca_dev
, ctrl
->val
);
1112 case V4L2_CID_AUTOGAIN
:
1113 /* case V4L2_CID_GAIN: */
1114 setagc(gspca_dev
, ctrl
->val
);
1115 if (!gspca_dev
->usb_err
&& !ctrl
->val
&& sd
->gain
)
1116 setgain(gspca_dev
, sd
->gain
->val
);
1118 case V4L2_CID_AUTO_WHITE_BALANCE
:
1119 setawb(gspca_dev
, ctrl
->val
);
1121 case V4L2_CID_EXPOSURE_AUTO
:
1122 /* case V4L2_CID_EXPOSURE: */
1123 setaec(gspca_dev
, ctrl
->val
);
1124 if (!gspca_dev
->usb_err
&& ctrl
->val
== V4L2_EXPOSURE_MANUAL
&&
1126 setexposure(gspca_dev
, sd
->exposure
->val
);
1128 case V4L2_CID_SHARPNESS
:
1129 setsharpness(gspca_dev
, ctrl
->val
);
1131 case V4L2_CID_HFLIP
:
1132 sethvflip(gspca_dev
, ctrl
->val
, sd
->vflip
->val
);
1134 case V4L2_CID_VFLIP
:
1135 sethvflip(gspca_dev
, sd
->hflip
->val
, ctrl
->val
);
1137 case V4L2_CID_POWER_LINE_FREQUENCY
:
1138 setlightfreq(gspca_dev
, ctrl
->val
);
1141 return gspca_dev
->usb_err
;
1144 static const struct v4l2_ctrl_ops ov534_ctrl_ops
= {
1145 .g_volatile_ctrl
= ov534_g_volatile_ctrl
,
1146 .s_ctrl
= ov534_s_ctrl
,
1149 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
1151 struct sd
*sd
= (struct sd
*) gspca_dev
;
1152 struct v4l2_ctrl_handler
*hdl
= &sd
->ctrl_handler
;
1153 /* parameters with different values between the supported sensors */
1167 if (sd
->sensor
== SENSOR_OV767x
) {
1171 brightness_min
= -127;
1172 brightness_max
= 127;
1174 contrast_max
= 0x80;
1175 contrast_def
= 0x40;
1176 exposure_min
= 0x08;
1177 exposure_max
= 0x60;
1178 exposure_def
= 0x13;
1182 saturation_max
= 255,
1183 saturation_def
= 64,
1185 brightness_max
= 255;
1195 gspca_dev
->vdev
.ctrl_handler
= hdl
;
1197 v4l2_ctrl_handler_init(hdl
, 13);
1199 if (sd
->sensor
== SENSOR_OV772x
)
1200 sd
->hue
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1201 V4L2_CID_HUE
, -90, 90, 1, 0);
1203 sd
->saturation
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1204 V4L2_CID_SATURATION
, saturation_min
, saturation_max
, 1,
1206 sd
->brightness
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1207 V4L2_CID_BRIGHTNESS
, brightness_min
, brightness_max
, 1,
1209 sd
->contrast
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1210 V4L2_CID_CONTRAST
, 0, contrast_max
, 1, contrast_def
);
1212 if (sd
->sensor
== SENSOR_OV772x
) {
1213 sd
->autogain
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1214 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1215 sd
->gain
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1216 V4L2_CID_GAIN
, 0, 63, 1, 20);
1219 sd
->autoexposure
= v4l2_ctrl_new_std_menu(hdl
, &ov534_ctrl_ops
,
1220 V4L2_CID_EXPOSURE_AUTO
,
1221 V4L2_EXPOSURE_MANUAL
, 0,
1222 V4L2_EXPOSURE_AUTO
);
1223 sd
->exposure
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1224 V4L2_CID_EXPOSURE
, exposure_min
, exposure_max
, 1,
1227 sd
->autowhitebalance
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1228 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1230 if (sd
->sensor
== SENSOR_OV772x
)
1231 sd
->sharpness
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1232 V4L2_CID_SHARPNESS
, 0, 63, 1, 0);
1234 sd
->hflip
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1235 V4L2_CID_HFLIP
, 0, 1, 1, hflip_def
);
1236 sd
->vflip
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1237 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1238 sd
->plfreq
= v4l2_ctrl_new_std_menu(hdl
, &ov534_ctrl_ops
,
1239 V4L2_CID_POWER_LINE_FREQUENCY
,
1240 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
, 0,
1241 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
);
1244 pr_err("Could not initialize controls\n");
1248 if (sd
->sensor
== SENSOR_OV772x
)
1249 v4l2_ctrl_auto_cluster(2, &sd
->autogain
, 0, true);
1251 v4l2_ctrl_auto_cluster(2, &sd
->autoexposure
, V4L2_EXPOSURE_MANUAL
,
1257 /* this function is called at probe and resume time */
1258 static int sd_init(struct gspca_dev
*gspca_dev
)
1260 struct sd
*sd
= (struct sd
*) gspca_dev
;
1262 static const struct reg_array bridge_init
[NSENSORS
] = {
1263 [SENSOR_OV767x
] = {bridge_init_767x
, ARRAY_SIZE(bridge_init_767x
)},
1264 [SENSOR_OV772x
] = {bridge_init_772x
, ARRAY_SIZE(bridge_init_772x
)},
1266 static const struct reg_array sensor_init
[NSENSORS
] = {
1267 [SENSOR_OV767x
] = {sensor_init_767x
, ARRAY_SIZE(sensor_init_767x
)},
1268 [SENSOR_OV772x
] = {sensor_init_772x
, ARRAY_SIZE(sensor_init_772x
)},
1272 ov534_reg_write(gspca_dev
, 0xe7, 0x3a);
1273 ov534_reg_write(gspca_dev
, 0xe0, 0x08);
1276 /* initialize the sensor address */
1277 ov534_reg_write(gspca_dev
, OV534_REG_ADDRESS
, 0x42);
1280 sccb_reg_write(gspca_dev
, 0x12, 0x80);
1283 /* probe the sensor */
1284 sccb_reg_read(gspca_dev
, 0x0a);
1285 sensor_id
= sccb_reg_read(gspca_dev
, 0x0a) << 8;
1286 sccb_reg_read(gspca_dev
, 0x0b);
1287 sensor_id
|= sccb_reg_read(gspca_dev
, 0x0b);
1288 PDEBUG(D_PROBE
, "Sensor ID: %04x", sensor_id
);
1290 if ((sensor_id
& 0xfff0) == 0x7670) {
1291 sd
->sensor
= SENSOR_OV767x
;
1292 gspca_dev
->cam
.cam_mode
= ov767x_mode
;
1293 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(ov767x_mode
);
1295 sd
->sensor
= SENSOR_OV772x
;
1296 gspca_dev
->cam
.bulk
= 1;
1297 gspca_dev
->cam
.bulk_size
= 16384;
1298 gspca_dev
->cam
.bulk_nurbs
= 2;
1299 gspca_dev
->cam
.mode_framerates
= ov772x_framerates
;
1303 reg_w_array(gspca_dev
, bridge_init
[sd
->sensor
].val
,
1304 bridge_init
[sd
->sensor
].len
);
1305 ov534_set_led(gspca_dev
, 1);
1306 sccb_w_array(gspca_dev
, sensor_init
[sd
->sensor
].val
,
1307 sensor_init
[sd
->sensor
].len
);
1309 sd_stopN(gspca_dev
);
1310 /* set_frame_rate(gspca_dev); */
1312 return gspca_dev
->usb_err
;
1315 static int sd_start(struct gspca_dev
*gspca_dev
)
1317 struct sd
*sd
= (struct sd
*) gspca_dev
;
1319 static const struct reg_array bridge_start
[NSENSORS
][2] = {
1320 [SENSOR_OV767x
] = {{bridge_start_qvga_767x
,
1321 ARRAY_SIZE(bridge_start_qvga_767x
)},
1322 {bridge_start_vga_767x
,
1323 ARRAY_SIZE(bridge_start_vga_767x
)}},
1324 [SENSOR_OV772x
] = {{bridge_start_qvga_772x
,
1325 ARRAY_SIZE(bridge_start_qvga_772x
)},
1326 {bridge_start_vga_772x
,
1327 ARRAY_SIZE(bridge_start_vga_772x
)}},
1329 static const struct reg_array sensor_start
[NSENSORS
][2] = {
1330 [SENSOR_OV767x
] = {{sensor_start_qvga_767x
,
1331 ARRAY_SIZE(sensor_start_qvga_767x
)},
1332 {sensor_start_vga_767x
,
1333 ARRAY_SIZE(sensor_start_vga_767x
)}},
1334 [SENSOR_OV772x
] = {{sensor_start_qvga_772x
,
1335 ARRAY_SIZE(sensor_start_qvga_772x
)},
1336 {sensor_start_vga_772x
,
1337 ARRAY_SIZE(sensor_start_vga_772x
)}},
1340 /* (from ms-win trace) */
1341 if (sd
->sensor
== SENSOR_OV767x
)
1342 sccb_reg_write(gspca_dev
, 0x1e, 0x04);
1343 /* black sun enable ? */
1345 mode
= gspca_dev
->curr_mode
; /* 0: 320x240, 1: 640x480 */
1346 reg_w_array(gspca_dev
, bridge_start
[sd
->sensor
][mode
].val
,
1347 bridge_start
[sd
->sensor
][mode
].len
);
1348 sccb_w_array(gspca_dev
, sensor_start
[sd
->sensor
][mode
].val
,
1349 sensor_start
[sd
->sensor
][mode
].len
);
1351 set_frame_rate(gspca_dev
);
1354 sethue(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->hue
));
1355 setsaturation(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->saturation
));
1357 setagc(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autogain
));
1358 setawb(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autowhitebalance
));
1359 setaec(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autoexposure
));
1361 setgain(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->gain
));
1362 setexposure(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->exposure
));
1363 setbrightness(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->brightness
));
1364 setcontrast(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->contrast
));
1366 setsharpness(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->sharpness
));
1367 sethvflip(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->hflip
),
1368 v4l2_ctrl_g_ctrl(sd
->vflip
));
1369 setlightfreq(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->plfreq
));
1371 ov534_set_led(gspca_dev
, 1);
1372 ov534_reg_write(gspca_dev
, 0xe0, 0x00);
1373 return gspca_dev
->usb_err
;
1376 static void sd_stopN(struct gspca_dev
*gspca_dev
)
1378 ov534_reg_write(gspca_dev
, 0xe0, 0x09);
1379 ov534_set_led(gspca_dev
, 0);
1382 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1383 #define UVC_STREAM_EOH (1 << 7)
1384 #define UVC_STREAM_ERR (1 << 6)
1385 #define UVC_STREAM_STI (1 << 5)
1386 #define UVC_STREAM_RES (1 << 4)
1387 #define UVC_STREAM_SCR (1 << 3)
1388 #define UVC_STREAM_PTS (1 << 2)
1389 #define UVC_STREAM_EOF (1 << 1)
1390 #define UVC_STREAM_FID (1 << 0)
1392 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1395 struct sd
*sd
= (struct sd
*) gspca_dev
;
1398 int remaining_len
= len
;
1401 payload_len
= gspca_dev
->cam
.bulk
? 2048 : 2040;
1403 len
= min(remaining_len
, payload_len
);
1405 /* Payloads are prefixed with a UVC-style header. We
1406 consider a frame to start when the FID toggles, or the PTS
1407 changes. A frame ends when EOF is set, and we've received
1408 the correct number of bytes. */
1410 /* Verify UVC header. Header length is always 12 */
1411 if (data
[0] != 12 || len
< 12) {
1412 PDEBUG(D_PACK
, "bad header");
1417 if (data
[1] & UVC_STREAM_ERR
) {
1418 PDEBUG(D_PACK
, "payload error");
1422 /* Extract PTS and FID */
1423 if (!(data
[1] & UVC_STREAM_PTS
)) {
1424 PDEBUG(D_PACK
, "PTS not present");
1427 this_pts
= (data
[5] << 24) | (data
[4] << 16)
1428 | (data
[3] << 8) | data
[2];
1429 this_fid
= (data
[1] & UVC_STREAM_FID
) ? 1 : 0;
1431 /* If PTS or FID has changed, start a new frame. */
1432 if (this_pts
!= sd
->last_pts
|| this_fid
!= sd
->last_fid
) {
1433 if (gspca_dev
->last_packet_type
== INTER_PACKET
)
1434 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1436 sd
->last_pts
= this_pts
;
1437 sd
->last_fid
= this_fid
;
1438 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1439 data
+ 12, len
- 12);
1440 /* If this packet is marked as EOF, end the frame */
1441 } else if (data
[1] & UVC_STREAM_EOF
) {
1443 if (gspca_dev
->pixfmt
.pixelformat
== V4L2_PIX_FMT_YUYV
1444 && gspca_dev
->image_len
+ len
- 12 !=
1445 gspca_dev
->pixfmt
.width
*
1446 gspca_dev
->pixfmt
.height
* 2) {
1447 PDEBUG(D_PACK
, "wrong sized frame");
1450 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1451 data
+ 12, len
- 12);
1454 /* Add the data from this payload */
1455 gspca_frame_add(gspca_dev
, INTER_PACKET
,
1456 data
+ 12, len
- 12);
1459 /* Done this payload */
1463 /* Discard data until a new frame starts. */
1464 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1467 remaining_len
-= len
;
1469 } while (remaining_len
> 0);
1472 /* get stream parameters (framerate) */
1473 static void sd_get_streamparm(struct gspca_dev
*gspca_dev
,
1474 struct v4l2_streamparm
*parm
)
1476 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1477 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1478 struct sd
*sd
= (struct sd
*) gspca_dev
;
1480 cp
->capability
|= V4L2_CAP_TIMEPERFRAME
;
1482 tpf
->denominator
= sd
->frame_rate
;
1485 /* set stream parameters (framerate) */
1486 static void sd_set_streamparm(struct gspca_dev
*gspca_dev
,
1487 struct v4l2_streamparm
*parm
)
1489 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1490 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1491 struct sd
*sd
= (struct sd
*) gspca_dev
;
1493 /* Set requested framerate */
1494 sd
->frame_rate
= tpf
->denominator
/ tpf
->numerator
;
1495 if (gspca_dev
->streaming
)
1496 set_frame_rate(gspca_dev
);
1498 /* Return the actual framerate */
1500 tpf
->denominator
= sd
->frame_rate
;
1503 /* sub-driver description */
1504 static const struct sd_desc sd_desc
= {
1505 .name
= MODULE_NAME
,
1506 .config
= sd_config
,
1508 .init_controls
= sd_init_controls
,
1511 .pkt_scan
= sd_pkt_scan
,
1512 .get_streamparm
= sd_get_streamparm
,
1513 .set_streamparm
= sd_set_streamparm
,
1516 /* -- module initialisation -- */
1517 static const struct usb_device_id device_table
[] = {
1518 {USB_DEVICE(0x1415, 0x2000)},
1519 {USB_DEVICE(0x06f8, 0x3002)},
1523 MODULE_DEVICE_TABLE(usb
, device_table
);
1525 /* -- device connect -- */
1526 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1528 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1532 static struct usb_driver sd_driver
= {
1533 .name
= MODULE_NAME
,
1534 .id_table
= device_table
,
1536 .disconnect
= gspca_disconnect
,
1538 .suspend
= gspca_suspend
,
1539 .resume
= gspca_resume
,
1540 .reset_resume
= gspca_resume
,
1544 module_usb_driver(sd_driver
);