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>
15 * PS3 Eye camera - FPS range extended by Joseph Howse
16 * <josephhowse@nummist.com> http://nummist.com
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #define MODULE_NAME "ov534"
39 #include <linux/fixp-arith.h>
40 #include <media/v4l2-ctrls.h>
42 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
43 #define OV534_REG_SUBADDR 0xf2
44 #define OV534_REG_WRITE 0xf3
45 #define OV534_REG_READ 0xf4
46 #define OV534_REG_OPERATION 0xf5
47 #define OV534_REG_STATUS 0xf6
49 #define OV534_OP_WRITE_3 0x37
50 #define OV534_OP_WRITE_2 0x33
51 #define OV534_OP_READ_2 0xf9
53 #define CTRL_TIMEOUT 500
54 #define DEFAULT_FRAME_RATE 30
56 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
57 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
58 MODULE_LICENSE("GPL");
60 /* specific webcam descriptor */
62 struct gspca_dev gspca_dev
; /* !! must be the first item */
64 struct v4l2_ctrl_handler ctrl_handler
;
65 struct v4l2_ctrl
*hue
;
66 struct v4l2_ctrl
*saturation
;
67 struct v4l2_ctrl
*brightness
;
68 struct v4l2_ctrl
*contrast
;
69 struct { /* gain control cluster */
70 struct v4l2_ctrl
*autogain
;
71 struct v4l2_ctrl
*gain
;
73 struct v4l2_ctrl
*autowhitebalance
;
74 struct { /* exposure control cluster */
75 struct v4l2_ctrl
*autoexposure
;
76 struct v4l2_ctrl
*exposure
;
78 struct v4l2_ctrl
*sharpness
;
79 struct v4l2_ctrl
*hflip
;
80 struct v4l2_ctrl
*vflip
;
81 struct v4l2_ctrl
*plfreq
;
95 static int sd_start(struct gspca_dev
*gspca_dev
);
96 static void sd_stopN(struct gspca_dev
*gspca_dev
);
99 static const struct v4l2_pix_format ov772x_mode
[] = {
100 {320, 240, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
101 .bytesperline
= 320 * 2,
102 .sizeimage
= 320 * 240 * 2,
103 .colorspace
= V4L2_COLORSPACE_SRGB
,
105 {640, 480, V4L2_PIX_FMT_YUYV
, V4L2_FIELD_NONE
,
106 .bytesperline
= 640 * 2,
107 .sizeimage
= 640 * 480 * 2,
108 .colorspace
= V4L2_COLORSPACE_SRGB
,
111 static const struct v4l2_pix_format ov767x_mode
[] = {
112 {320, 240, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
114 .sizeimage
= 320 * 240 * 3 / 8 + 590,
115 .colorspace
= V4L2_COLORSPACE_JPEG
},
116 {640, 480, V4L2_PIX_FMT_JPEG
, V4L2_FIELD_NONE
,
118 .sizeimage
= 640 * 480 * 3 / 8 + 590,
119 .colorspace
= V4L2_COLORSPACE_JPEG
},
122 static const u8 qvga_rates
[] = {187, 150, 137, 125, 100, 75, 60, 50, 37, 30};
123 static const u8 vga_rates
[] = {60, 50, 40, 30, 15};
125 static const struct framerates ov772x_framerates
[] = {
128 .nrates
= ARRAY_SIZE(qvga_rates
),
132 .nrates
= ARRAY_SIZE(vga_rates
),
141 static const u8 bridge_init_767x
[][2] = {
142 /* comments from the ms-win file apollo7670.set */
172 {0xc0, 0x50}, /* HSize 640 */
173 {0xc1, 0x3c}, /* VSize 480 */
174 {0x34, 0x05}, /* enable Audio Suspend mode */
175 {0xc2, 0x0c}, /* Input YUV */
176 {0xc3, 0xf9}, /* enable PRE */
177 {0x34, 0x05}, /* enable Audio Suspend mode */
178 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
179 {0x31, 0xf9}, /* enable 1.8V Suspend */
180 {0x35, 0x02}, /* turn on JPEG */
182 {0x25, 0x42}, /* GPIO[8]:Input */
183 {0x94, 0x11}, /* If the default setting is loaded when
184 * system boots up, this flag is closed here */
186 static const u8 sensor_init_767x
[][2] = {
204 {0x7a, 0x2a}, /* set Gamma=1.6 below */
224 {0x14, 0x38}, /* gain max 16x */
304 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
308 {0xa4, 0x8a}, /* Night mode trigger point */
341 static const u8 bridge_start_vga_767x
[][2] = {
349 {0x35, 0x02}, /* turn on JPEG */
351 {0xda, 0x00}, /* for higher clock rate(30fps) */
352 {0x34, 0x05}, /* enable Audio Suspend mode */
353 {0xc3, 0xf9}, /* enable PRE */
354 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
355 {0x8d, 0x1c}, /* output YUV */
356 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
357 {0x50, 0x00}, /* H/V divider=0 */
358 {0x51, 0xa0}, /* input H=640/4 */
359 {0x52, 0x3c}, /* input V=480/4 */
360 {0x53, 0x00}, /* offset X=0 */
361 {0x54, 0x00}, /* offset Y=0 */
362 {0x55, 0x00}, /* H/V size[8]=0 */
363 {0x57, 0x00}, /* H-size[9]=0 */
364 {0x5c, 0x00}, /* output size[9:8]=0 */
365 {0x5a, 0xa0}, /* output H=640/4 */
366 {0x5b, 0x78}, /* output V=480/4 */
371 static const u8 sensor_start_vga_767x
[][2] = {
377 static const u8 bridge_start_qvga_767x
[][2] = {
385 {0x35, 0x02}, /* turn on JPEG */
387 {0xc0, 0x50}, /* CIF HSize 640 */
388 {0xc1, 0x3c}, /* CIF VSize 480 */
389 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
390 {0x8d, 0x1c}, /* output YUV */
391 {0x34, 0x05}, /* enable Audio Suspend mode */
392 {0xc2, 0x4c}, /* output YUV and Enable DCW */
393 {0xc3, 0xf9}, /* enable PRE */
394 {0x1c, 0x00}, /* indirect addressing */
395 {0x1d, 0x48}, /* output YUV422 */
396 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
397 {0x51, 0xa0}, /* DCW input H=640/4 */
398 {0x52, 0x78}, /* DCW input V=480/4 */
399 {0x53, 0x00}, /* offset X=0 */
400 {0x54, 0x00}, /* offset Y=0 */
401 {0x55, 0x00}, /* H/V size[8]=0 */
402 {0x57, 0x00}, /* H-size[9]=0 */
403 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
404 {0x5a, 0x50}, /* DCW output H=320/4 */
405 {0x5b, 0x3c}, /* DCW output V=240/4 */
410 static const u8 sensor_start_qvga_767x
[][2] = {
417 static const u8 bridge_init_772x
[][2] = {
456 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
457 { 0x1d, 0x00 }, /* payload size */
459 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
460 { 0x1d, 0x58 }, /* frame size */
461 { 0x1d, 0x00 }, /* frame size */
464 { 0x1d, 0x08 }, /* turn on UVC header */
465 { 0x1d, 0x0e }, /* .. */
475 static const u8 sensor_init_772x
[][2] = {
478 /*fixme: better have a delay?*/
501 { 0x63, 0xaa }, /* AWB - was e0 */
504 { 0x13, 0xf0 }, /* com8 */
517 { 0x13, 0xff }, /* AWB */
565 { 0x8e, 0x00 }, /* De-noise threshold */
568 static const u8 bridge_start_vga_772x
[][2] = {
579 static const u8 sensor_start_vga_772x
[][2] = {
589 static const u8 bridge_start_qvga_772x
[][2] = {
600 static const u8 sensor_start_qvga_772x
[][2] = {
611 static void ov534_reg_write(struct gspca_dev
*gspca_dev
, u16 reg
, u8 val
)
613 struct usb_device
*udev
= gspca_dev
->dev
;
616 if (gspca_dev
->usb_err
< 0)
619 PDEBUG(D_USBO
, "SET 01 0000 %04x %02x", reg
, val
);
620 gspca_dev
->usb_buf
[0] = val
;
621 ret
= usb_control_msg(udev
,
622 usb_sndctrlpipe(udev
, 0),
624 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
625 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
627 pr_err("write failed %d\n", ret
);
628 gspca_dev
->usb_err
= ret
;
632 static u8
ov534_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
634 struct usb_device
*udev
= gspca_dev
->dev
;
637 if (gspca_dev
->usb_err
< 0)
639 ret
= usb_control_msg(udev
,
640 usb_rcvctrlpipe(udev
, 0),
642 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
643 0x00, reg
, gspca_dev
->usb_buf
, 1, CTRL_TIMEOUT
);
644 PDEBUG(D_USBI
, "GET 01 0000 %04x %02x", reg
, gspca_dev
->usb_buf
[0]);
646 pr_err("read failed %d\n", ret
);
647 gspca_dev
->usb_err
= ret
;
649 return gspca_dev
->usb_buf
[0];
652 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
653 * (direction and output)? */
654 static void ov534_set_led(struct gspca_dev
*gspca_dev
, int status
)
658 PDEBUG(D_CONF
, "led status: %d", status
);
660 data
= ov534_reg_read(gspca_dev
, 0x21);
662 ov534_reg_write(gspca_dev
, 0x21, data
);
664 data
= ov534_reg_read(gspca_dev
, 0x23);
670 ov534_reg_write(gspca_dev
, 0x23, data
);
673 data
= ov534_reg_read(gspca_dev
, 0x21);
675 ov534_reg_write(gspca_dev
, 0x21, data
);
679 static int sccb_check_status(struct gspca_dev
*gspca_dev
)
684 for (i
= 0; i
< 5; i
++) {
686 data
= ov534_reg_read(gspca_dev
, OV534_REG_STATUS
);
696 PERR("sccb status 0x%02x, attempt %d/5",
703 static void sccb_reg_write(struct gspca_dev
*gspca_dev
, u8 reg
, u8 val
)
705 PDEBUG(D_USBO
, "sccb write: %02x %02x", reg
, val
);
706 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
707 ov534_reg_write(gspca_dev
, OV534_REG_WRITE
, val
);
708 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_3
);
710 if (!sccb_check_status(gspca_dev
)) {
711 pr_err("sccb_reg_write failed\n");
712 gspca_dev
->usb_err
= -EIO
;
716 static u8
sccb_reg_read(struct gspca_dev
*gspca_dev
, u16 reg
)
718 ov534_reg_write(gspca_dev
, OV534_REG_SUBADDR
, reg
);
719 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_WRITE_2
);
720 if (!sccb_check_status(gspca_dev
))
721 pr_err("sccb_reg_read failed 1\n");
723 ov534_reg_write(gspca_dev
, OV534_REG_OPERATION
, OV534_OP_READ_2
);
724 if (!sccb_check_status(gspca_dev
))
725 pr_err("sccb_reg_read failed 2\n");
727 return ov534_reg_read(gspca_dev
, OV534_REG_READ
);
730 /* output a bridge sequence (reg - val) */
731 static void reg_w_array(struct gspca_dev
*gspca_dev
,
732 const u8 (*data
)[2], int len
)
735 ov534_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
740 /* output a sensor sequence (reg - val) */
741 static void sccb_w_array(struct gspca_dev
*gspca_dev
,
742 const u8 (*data
)[2], int len
)
745 if ((*data
)[0] != 0xff) {
746 sccb_reg_write(gspca_dev
, (*data
)[0], (*data
)[1]);
748 sccb_reg_read(gspca_dev
, (*data
)[1]);
749 sccb_reg_write(gspca_dev
, 0xff, 0x00);
755 /* ov772x specific controls */
756 static void set_frame_rate(struct gspca_dev
*gspca_dev
)
758 struct sd
*sd
= (struct sd
*) gspca_dev
;
766 const struct rate_s
*r
;
767 static const struct rate_s rate_0
[] = { /* 640x480 */
768 {60, 0x01, 0xc1, 0x04},
769 {50, 0x01, 0x41, 0x02},
770 {40, 0x02, 0xc1, 0x04},
771 {30, 0x04, 0x81, 0x02},
772 {15, 0x03, 0x41, 0x04},
774 static const struct rate_s rate_1
[] = { /* 320x240 */
775 /* {205, 0x01, 0xc1, 0x02}, * 205 FPS: video is partly corrupt */
776 {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
777 {150, 0x01, 0xc1, 0x04},
778 {137, 0x02, 0xc1, 0x02},
779 {125, 0x02, 0x81, 0x02},
780 {100, 0x02, 0xc1, 0x04},
781 {75, 0x03, 0xc1, 0x04},
782 {60, 0x04, 0xc1, 0x04},
783 {50, 0x02, 0x41, 0x04},
784 {37, 0x03, 0x41, 0x04},
785 {30, 0x04, 0x41, 0x04},
788 if (sd
->sensor
!= SENSOR_OV772x
)
790 if (gspca_dev
->cam
.cam_mode
[gspca_dev
->curr_mode
].priv
== 0) {
792 i
= ARRAY_SIZE(rate_0
);
795 i
= ARRAY_SIZE(rate_1
);
798 if (sd
->frame_rate
>= r
->fps
)
803 sccb_reg_write(gspca_dev
, 0x11, r
->r11
);
804 sccb_reg_write(gspca_dev
, 0x0d, r
->r0d
);
805 ov534_reg_write(gspca_dev
, 0xe5, r
->re5
);
807 PDEBUG(D_PROBE
, "frame_rate: %d", r
->fps
);
810 static void sethue(struct gspca_dev
*gspca_dev
, s32 val
)
812 struct sd
*sd
= (struct sd
*) gspca_dev
;
814 if (sd
->sensor
== SENSOR_OV767x
) {
820 /* According to the datasheet the registers expect HUESIN and
821 * HUECOS to be the result of the trigonometric functions,
824 * The 0x7fff here represents the maximum absolute value
825 * returned byt fixp_sin and fixp_cos, so the scaling will
826 * consider the result like in the interval [-1.0, 1.0].
828 huesin
= fixp_sin16(val
) * 0x80 / 0x7fff;
829 huecos
= fixp_cos16(val
) * 0x80 / 0x7fff;
832 sccb_reg_write(gspca_dev
, 0xab,
833 sccb_reg_read(gspca_dev
, 0xab) | 0x2);
836 sccb_reg_write(gspca_dev
, 0xab,
837 sccb_reg_read(gspca_dev
, 0xab) & ~0x2);
840 sccb_reg_write(gspca_dev
, 0xa9, (u8
)huecos
);
841 sccb_reg_write(gspca_dev
, 0xaa, (u8
)huesin
);
845 static void setsaturation(struct gspca_dev
*gspca_dev
, s32 val
)
847 struct sd
*sd
= (struct sd
*) gspca_dev
;
849 if (sd
->sensor
== SENSOR_OV767x
) {
851 static u8 color_tb
[][6] = {
852 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
853 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
854 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
855 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
856 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
857 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
858 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
861 for (i
= 0; i
< ARRAY_SIZE(color_tb
[0]); i
++)
862 sccb_reg_write(gspca_dev
, 0x4f + i
, color_tb
[val
][i
]);
864 sccb_reg_write(gspca_dev
, 0xa7, val
); /* U saturation */
865 sccb_reg_write(gspca_dev
, 0xa8, val
); /* V saturation */
869 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
871 struct sd
*sd
= (struct sd
*) gspca_dev
;
873 if (sd
->sensor
== SENSOR_OV767x
) {
876 sccb_reg_write(gspca_dev
, 0x55, val
); /* bright */
878 sccb_reg_write(gspca_dev
, 0x9b, val
);
882 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
884 struct sd
*sd
= (struct sd
*) gspca_dev
;
886 if (sd
->sensor
== SENSOR_OV767x
)
887 sccb_reg_write(gspca_dev
, 0x56, val
); /* contras */
889 sccb_reg_write(gspca_dev
, 0x9c, val
);
892 static void setgain(struct gspca_dev
*gspca_dev
, s32 val
)
894 switch (val
& 0x30) {
912 sccb_reg_write(gspca_dev
, 0x00, val
);
915 static s32
getgain(struct gspca_dev
*gspca_dev
)
917 return sccb_reg_read(gspca_dev
, 0x00);
920 static void setexposure(struct gspca_dev
*gspca_dev
, s32 val
)
922 struct sd
*sd
= (struct sd
*) gspca_dev
;
924 if (sd
->sensor
== SENSOR_OV767x
) {
926 /* set only aec[9:2] */
927 sccb_reg_write(gspca_dev
, 0x10, val
); /* aech */
930 /* 'val' is one byte and represents half of the exposure value
931 * we are going to set into registers, a two bytes value:
933 * MSB: ((u16) val << 1) >> 8 == val >> 7
934 * LSB: ((u16) val << 1) & 0xff == val << 1
936 sccb_reg_write(gspca_dev
, 0x08, val
>> 7);
937 sccb_reg_write(gspca_dev
, 0x10, val
<< 1);
941 static s32
getexposure(struct gspca_dev
*gspca_dev
)
943 struct sd
*sd
= (struct sd
*) gspca_dev
;
945 if (sd
->sensor
== SENSOR_OV767x
) {
946 /* get only aec[9:2] */
947 return sccb_reg_read(gspca_dev
, 0x10); /* aech */
949 u8 hi
= sccb_reg_read(gspca_dev
, 0x08);
950 u8 lo
= sccb_reg_read(gspca_dev
, 0x10);
951 return (hi
<< 8 | lo
) >> 1;
955 static void setagc(struct gspca_dev
*gspca_dev
, s32 val
)
958 sccb_reg_write(gspca_dev
, 0x13,
959 sccb_reg_read(gspca_dev
, 0x13) | 0x04);
960 sccb_reg_write(gspca_dev
, 0x64,
961 sccb_reg_read(gspca_dev
, 0x64) | 0x03);
963 sccb_reg_write(gspca_dev
, 0x13,
964 sccb_reg_read(gspca_dev
, 0x13) & ~0x04);
965 sccb_reg_write(gspca_dev
, 0x64,
966 sccb_reg_read(gspca_dev
, 0x64) & ~0x03);
970 static void setawb(struct gspca_dev
*gspca_dev
, s32 val
)
972 struct sd
*sd
= (struct sd
*) gspca_dev
;
975 sccb_reg_write(gspca_dev
, 0x13,
976 sccb_reg_read(gspca_dev
, 0x13) | 0x02);
977 if (sd
->sensor
== SENSOR_OV772x
)
978 sccb_reg_write(gspca_dev
, 0x63,
979 sccb_reg_read(gspca_dev
, 0x63) | 0xc0);
981 sccb_reg_write(gspca_dev
, 0x13,
982 sccb_reg_read(gspca_dev
, 0x13) & ~0x02);
983 if (sd
->sensor
== SENSOR_OV772x
)
984 sccb_reg_write(gspca_dev
, 0x63,
985 sccb_reg_read(gspca_dev
, 0x63) & ~0xc0);
989 static void setaec(struct gspca_dev
*gspca_dev
, s32 val
)
991 struct sd
*sd
= (struct sd
*) gspca_dev
;
994 data
= sd
->sensor
== SENSOR_OV767x
?
995 0x05 : /* agc + aec */
998 case V4L2_EXPOSURE_AUTO
:
999 sccb_reg_write(gspca_dev
, 0x13,
1000 sccb_reg_read(gspca_dev
, 0x13) | data
);
1002 case V4L2_EXPOSURE_MANUAL
:
1003 sccb_reg_write(gspca_dev
, 0x13,
1004 sccb_reg_read(gspca_dev
, 0x13) & ~data
);
1009 static void setsharpness(struct gspca_dev
*gspca_dev
, s32 val
)
1011 sccb_reg_write(gspca_dev
, 0x91, val
); /* Auto de-noise threshold */
1012 sccb_reg_write(gspca_dev
, 0x8e, val
); /* De-noise threshold */
1015 static void sethvflip(struct gspca_dev
*gspca_dev
, s32 hflip
, s32 vflip
)
1017 struct sd
*sd
= (struct sd
*) gspca_dev
;
1020 if (sd
->sensor
== SENSOR_OV767x
) {
1021 val
= sccb_reg_read(gspca_dev
, 0x1e); /* mvfp */
1027 sccb_reg_write(gspca_dev
, 0x1e, val
);
1029 val
= sccb_reg_read(gspca_dev
, 0x0c);
1035 sccb_reg_write(gspca_dev
, 0x0c, val
);
1039 static void setlightfreq(struct gspca_dev
*gspca_dev
, s32 val
)
1041 struct sd
*sd
= (struct sd
*) gspca_dev
;
1043 val
= val
? 0x9e : 0x00;
1044 if (sd
->sensor
== SENSOR_OV767x
) {
1045 sccb_reg_write(gspca_dev
, 0x2a, 0x00);
1047 val
= 0x9d; /* insert dummy to 25fps for 50Hz */
1049 sccb_reg_write(gspca_dev
, 0x2b, val
);
1053 /* this function is called at probe time */
1054 static int sd_config(struct gspca_dev
*gspca_dev
,
1055 const struct usb_device_id
*id
)
1057 struct sd
*sd
= (struct sd
*) gspca_dev
;
1060 cam
= &gspca_dev
->cam
;
1062 cam
->cam_mode
= ov772x_mode
;
1063 cam
->nmodes
= ARRAY_SIZE(ov772x_mode
);
1065 sd
->frame_rate
= DEFAULT_FRAME_RATE
;
1070 static int ov534_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1072 struct sd
*sd
= container_of(ctrl
->handler
, struct sd
, ctrl_handler
);
1073 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
1076 case V4L2_CID_AUTOGAIN
:
1077 gspca_dev
->usb_err
= 0;
1078 if (ctrl
->val
&& sd
->gain
&& gspca_dev
->streaming
)
1079 sd
->gain
->val
= getgain(gspca_dev
);
1080 return gspca_dev
->usb_err
;
1082 case V4L2_CID_EXPOSURE_AUTO
:
1083 gspca_dev
->usb_err
= 0;
1084 if (ctrl
->val
== V4L2_EXPOSURE_AUTO
&& sd
->exposure
&&
1085 gspca_dev
->streaming
)
1086 sd
->exposure
->val
= getexposure(gspca_dev
);
1087 return gspca_dev
->usb_err
;
1092 static int ov534_s_ctrl(struct v4l2_ctrl
*ctrl
)
1094 struct sd
*sd
= container_of(ctrl
->handler
, struct sd
, ctrl_handler
);
1095 struct gspca_dev
*gspca_dev
= &sd
->gspca_dev
;
1097 gspca_dev
->usb_err
= 0;
1098 if (!gspca_dev
->streaming
)
1103 sethue(gspca_dev
, ctrl
->val
);
1105 case V4L2_CID_SATURATION
:
1106 setsaturation(gspca_dev
, ctrl
->val
);
1108 case V4L2_CID_BRIGHTNESS
:
1109 setbrightness(gspca_dev
, ctrl
->val
);
1111 case V4L2_CID_CONTRAST
:
1112 setcontrast(gspca_dev
, ctrl
->val
);
1114 case V4L2_CID_AUTOGAIN
:
1115 /* case V4L2_CID_GAIN: */
1116 setagc(gspca_dev
, ctrl
->val
);
1117 if (!gspca_dev
->usb_err
&& !ctrl
->val
&& sd
->gain
)
1118 setgain(gspca_dev
, sd
->gain
->val
);
1120 case V4L2_CID_AUTO_WHITE_BALANCE
:
1121 setawb(gspca_dev
, ctrl
->val
);
1123 case V4L2_CID_EXPOSURE_AUTO
:
1124 /* case V4L2_CID_EXPOSURE: */
1125 setaec(gspca_dev
, ctrl
->val
);
1126 if (!gspca_dev
->usb_err
&& ctrl
->val
== V4L2_EXPOSURE_MANUAL
&&
1128 setexposure(gspca_dev
, sd
->exposure
->val
);
1130 case V4L2_CID_SHARPNESS
:
1131 setsharpness(gspca_dev
, ctrl
->val
);
1133 case V4L2_CID_HFLIP
:
1134 sethvflip(gspca_dev
, ctrl
->val
, sd
->vflip
->val
);
1136 case V4L2_CID_VFLIP
:
1137 sethvflip(gspca_dev
, sd
->hflip
->val
, ctrl
->val
);
1139 case V4L2_CID_POWER_LINE_FREQUENCY
:
1140 setlightfreq(gspca_dev
, ctrl
->val
);
1143 return gspca_dev
->usb_err
;
1146 static const struct v4l2_ctrl_ops ov534_ctrl_ops
= {
1147 .g_volatile_ctrl
= ov534_g_volatile_ctrl
,
1148 .s_ctrl
= ov534_s_ctrl
,
1151 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
1153 struct sd
*sd
= (struct sd
*) gspca_dev
;
1154 struct v4l2_ctrl_handler
*hdl
= &sd
->ctrl_handler
;
1155 /* parameters with different values between the supported sensors */
1169 if (sd
->sensor
== SENSOR_OV767x
) {
1173 brightness_min
= -127;
1174 brightness_max
= 127;
1176 contrast_max
= 0x80;
1177 contrast_def
= 0x40;
1178 exposure_min
= 0x08;
1179 exposure_max
= 0x60;
1180 exposure_def
= 0x13;
1184 saturation_max
= 255,
1185 saturation_def
= 64,
1187 brightness_max
= 255;
1197 gspca_dev
->vdev
.ctrl_handler
= hdl
;
1199 v4l2_ctrl_handler_init(hdl
, 13);
1201 if (sd
->sensor
== SENSOR_OV772x
)
1202 sd
->hue
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1203 V4L2_CID_HUE
, -90, 90, 1, 0);
1205 sd
->saturation
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1206 V4L2_CID_SATURATION
, saturation_min
, saturation_max
, 1,
1208 sd
->brightness
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1209 V4L2_CID_BRIGHTNESS
, brightness_min
, brightness_max
, 1,
1211 sd
->contrast
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1212 V4L2_CID_CONTRAST
, 0, contrast_max
, 1, contrast_def
);
1214 if (sd
->sensor
== SENSOR_OV772x
) {
1215 sd
->autogain
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1216 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
1217 sd
->gain
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1218 V4L2_CID_GAIN
, 0, 63, 1, 20);
1221 sd
->autoexposure
= v4l2_ctrl_new_std_menu(hdl
, &ov534_ctrl_ops
,
1222 V4L2_CID_EXPOSURE_AUTO
,
1223 V4L2_EXPOSURE_MANUAL
, 0,
1224 V4L2_EXPOSURE_AUTO
);
1225 sd
->exposure
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1226 V4L2_CID_EXPOSURE
, exposure_min
, exposure_max
, 1,
1229 sd
->autowhitebalance
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1230 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1232 if (sd
->sensor
== SENSOR_OV772x
)
1233 sd
->sharpness
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1234 V4L2_CID_SHARPNESS
, 0, 63, 1, 0);
1236 sd
->hflip
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1237 V4L2_CID_HFLIP
, 0, 1, 1, hflip_def
);
1238 sd
->vflip
= v4l2_ctrl_new_std(hdl
, &ov534_ctrl_ops
,
1239 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1240 sd
->plfreq
= v4l2_ctrl_new_std_menu(hdl
, &ov534_ctrl_ops
,
1241 V4L2_CID_POWER_LINE_FREQUENCY
,
1242 V4L2_CID_POWER_LINE_FREQUENCY_50HZ
, 0,
1243 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED
);
1246 pr_err("Could not initialize controls\n");
1250 if (sd
->sensor
== SENSOR_OV772x
)
1251 v4l2_ctrl_auto_cluster(2, &sd
->autogain
, 0, true);
1253 v4l2_ctrl_auto_cluster(2, &sd
->autoexposure
, V4L2_EXPOSURE_MANUAL
,
1259 /* this function is called at probe and resume time */
1260 static int sd_init(struct gspca_dev
*gspca_dev
)
1262 struct sd
*sd
= (struct sd
*) gspca_dev
;
1264 static const struct reg_array bridge_init
[NSENSORS
] = {
1265 [SENSOR_OV767x
] = {bridge_init_767x
, ARRAY_SIZE(bridge_init_767x
)},
1266 [SENSOR_OV772x
] = {bridge_init_772x
, ARRAY_SIZE(bridge_init_772x
)},
1268 static const struct reg_array sensor_init
[NSENSORS
] = {
1269 [SENSOR_OV767x
] = {sensor_init_767x
, ARRAY_SIZE(sensor_init_767x
)},
1270 [SENSOR_OV772x
] = {sensor_init_772x
, ARRAY_SIZE(sensor_init_772x
)},
1274 ov534_reg_write(gspca_dev
, 0xe7, 0x3a);
1275 ov534_reg_write(gspca_dev
, 0xe0, 0x08);
1278 /* initialize the sensor address */
1279 ov534_reg_write(gspca_dev
, OV534_REG_ADDRESS
, 0x42);
1282 sccb_reg_write(gspca_dev
, 0x12, 0x80);
1285 /* probe the sensor */
1286 sccb_reg_read(gspca_dev
, 0x0a);
1287 sensor_id
= sccb_reg_read(gspca_dev
, 0x0a) << 8;
1288 sccb_reg_read(gspca_dev
, 0x0b);
1289 sensor_id
|= sccb_reg_read(gspca_dev
, 0x0b);
1290 PDEBUG(D_PROBE
, "Sensor ID: %04x", sensor_id
);
1292 if ((sensor_id
& 0xfff0) == 0x7670) {
1293 sd
->sensor
= SENSOR_OV767x
;
1294 gspca_dev
->cam
.cam_mode
= ov767x_mode
;
1295 gspca_dev
->cam
.nmodes
= ARRAY_SIZE(ov767x_mode
);
1297 sd
->sensor
= SENSOR_OV772x
;
1298 gspca_dev
->cam
.bulk
= 1;
1299 gspca_dev
->cam
.bulk_size
= 16384;
1300 gspca_dev
->cam
.bulk_nurbs
= 2;
1301 gspca_dev
->cam
.mode_framerates
= ov772x_framerates
;
1305 reg_w_array(gspca_dev
, bridge_init
[sd
->sensor
].val
,
1306 bridge_init
[sd
->sensor
].len
);
1307 ov534_set_led(gspca_dev
, 1);
1308 sccb_w_array(gspca_dev
, sensor_init
[sd
->sensor
].val
,
1309 sensor_init
[sd
->sensor
].len
);
1311 sd_stopN(gspca_dev
);
1312 /* set_frame_rate(gspca_dev); */
1314 return gspca_dev
->usb_err
;
1317 static int sd_start(struct gspca_dev
*gspca_dev
)
1319 struct sd
*sd
= (struct sd
*) gspca_dev
;
1321 static const struct reg_array bridge_start
[NSENSORS
][2] = {
1322 [SENSOR_OV767x
] = {{bridge_start_qvga_767x
,
1323 ARRAY_SIZE(bridge_start_qvga_767x
)},
1324 {bridge_start_vga_767x
,
1325 ARRAY_SIZE(bridge_start_vga_767x
)}},
1326 [SENSOR_OV772x
] = {{bridge_start_qvga_772x
,
1327 ARRAY_SIZE(bridge_start_qvga_772x
)},
1328 {bridge_start_vga_772x
,
1329 ARRAY_SIZE(bridge_start_vga_772x
)}},
1331 static const struct reg_array sensor_start
[NSENSORS
][2] = {
1332 [SENSOR_OV767x
] = {{sensor_start_qvga_767x
,
1333 ARRAY_SIZE(sensor_start_qvga_767x
)},
1334 {sensor_start_vga_767x
,
1335 ARRAY_SIZE(sensor_start_vga_767x
)}},
1336 [SENSOR_OV772x
] = {{sensor_start_qvga_772x
,
1337 ARRAY_SIZE(sensor_start_qvga_772x
)},
1338 {sensor_start_vga_772x
,
1339 ARRAY_SIZE(sensor_start_vga_772x
)}},
1342 /* (from ms-win trace) */
1343 if (sd
->sensor
== SENSOR_OV767x
)
1344 sccb_reg_write(gspca_dev
, 0x1e, 0x04);
1345 /* black sun enable ? */
1347 mode
= gspca_dev
->curr_mode
; /* 0: 320x240, 1: 640x480 */
1348 reg_w_array(gspca_dev
, bridge_start
[sd
->sensor
][mode
].val
,
1349 bridge_start
[sd
->sensor
][mode
].len
);
1350 sccb_w_array(gspca_dev
, sensor_start
[sd
->sensor
][mode
].val
,
1351 sensor_start
[sd
->sensor
][mode
].len
);
1353 set_frame_rate(gspca_dev
);
1356 sethue(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->hue
));
1357 setsaturation(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->saturation
));
1359 setagc(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autogain
));
1360 setawb(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autowhitebalance
));
1361 setaec(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autoexposure
));
1363 setgain(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->gain
));
1364 setexposure(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->exposure
));
1365 setbrightness(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->brightness
));
1366 setcontrast(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->contrast
));
1368 setsharpness(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->sharpness
));
1369 sethvflip(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->hflip
),
1370 v4l2_ctrl_g_ctrl(sd
->vflip
));
1371 setlightfreq(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->plfreq
));
1373 ov534_set_led(gspca_dev
, 1);
1374 ov534_reg_write(gspca_dev
, 0xe0, 0x00);
1375 return gspca_dev
->usb_err
;
1378 static void sd_stopN(struct gspca_dev
*gspca_dev
)
1380 ov534_reg_write(gspca_dev
, 0xe0, 0x09);
1381 ov534_set_led(gspca_dev
, 0);
1384 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1385 #define UVC_STREAM_EOH (1 << 7)
1386 #define UVC_STREAM_ERR (1 << 6)
1387 #define UVC_STREAM_STI (1 << 5)
1388 #define UVC_STREAM_RES (1 << 4)
1389 #define UVC_STREAM_SCR (1 << 3)
1390 #define UVC_STREAM_PTS (1 << 2)
1391 #define UVC_STREAM_EOF (1 << 1)
1392 #define UVC_STREAM_FID (1 << 0)
1394 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1397 struct sd
*sd
= (struct sd
*) gspca_dev
;
1400 int remaining_len
= len
;
1403 payload_len
= gspca_dev
->cam
.bulk
? 2048 : 2040;
1405 len
= min(remaining_len
, payload_len
);
1407 /* Payloads are prefixed with a UVC-style header. We
1408 consider a frame to start when the FID toggles, or the PTS
1409 changes. A frame ends when EOF is set, and we've received
1410 the correct number of bytes. */
1412 /* Verify UVC header. Header length is always 12 */
1413 if (data
[0] != 12 || len
< 12) {
1414 PDEBUG(D_PACK
, "bad header");
1419 if (data
[1] & UVC_STREAM_ERR
) {
1420 PDEBUG(D_PACK
, "payload error");
1424 /* Extract PTS and FID */
1425 if (!(data
[1] & UVC_STREAM_PTS
)) {
1426 PDEBUG(D_PACK
, "PTS not present");
1429 this_pts
= (data
[5] << 24) | (data
[4] << 16)
1430 | (data
[3] << 8) | data
[2];
1431 this_fid
= (data
[1] & UVC_STREAM_FID
) ? 1 : 0;
1433 /* If PTS or FID has changed, start a new frame. */
1434 if (this_pts
!= sd
->last_pts
|| this_fid
!= sd
->last_fid
) {
1435 if (gspca_dev
->last_packet_type
== INTER_PACKET
)
1436 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1438 sd
->last_pts
= this_pts
;
1439 sd
->last_fid
= this_fid
;
1440 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1441 data
+ 12, len
- 12);
1442 /* If this packet is marked as EOF, end the frame */
1443 } else if (data
[1] & UVC_STREAM_EOF
) {
1445 if (gspca_dev
->pixfmt
.pixelformat
== V4L2_PIX_FMT_YUYV
1446 && gspca_dev
->image_len
+ len
- 12 !=
1447 gspca_dev
->pixfmt
.width
*
1448 gspca_dev
->pixfmt
.height
* 2) {
1449 PDEBUG(D_PACK
, "wrong sized frame");
1452 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1453 data
+ 12, len
- 12);
1456 /* Add the data from this payload */
1457 gspca_frame_add(gspca_dev
, INTER_PACKET
,
1458 data
+ 12, len
- 12);
1461 /* Done this payload */
1465 /* Discard data until a new frame starts. */
1466 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
1469 remaining_len
-= len
;
1471 } while (remaining_len
> 0);
1474 /* get stream parameters (framerate) */
1475 static void sd_get_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 cp
->capability
|= V4L2_CAP_TIMEPERFRAME
;
1484 tpf
->denominator
= sd
->frame_rate
;
1487 /* set stream parameters (framerate) */
1488 static void sd_set_streamparm(struct gspca_dev
*gspca_dev
,
1489 struct v4l2_streamparm
*parm
)
1491 struct v4l2_captureparm
*cp
= &parm
->parm
.capture
;
1492 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1493 struct sd
*sd
= (struct sd
*) gspca_dev
;
1495 if (tpf
->numerator
== 0 || tpf
->denominator
== 0)
1496 sd
->frame_rate
= DEFAULT_FRAME_RATE
;
1498 sd
->frame_rate
= tpf
->denominator
/ tpf
->numerator
;
1500 if (gspca_dev
->streaming
)
1501 set_frame_rate(gspca_dev
);
1503 /* Return the actual framerate */
1505 tpf
->denominator
= sd
->frame_rate
;
1508 /* sub-driver description */
1509 static const struct sd_desc sd_desc
= {
1510 .name
= MODULE_NAME
,
1511 .config
= sd_config
,
1513 .init_controls
= sd_init_controls
,
1516 .pkt_scan
= sd_pkt_scan
,
1517 .get_streamparm
= sd_get_streamparm
,
1518 .set_streamparm
= sd_set_streamparm
,
1521 /* -- module initialisation -- */
1522 static const struct usb_device_id device_table
[] = {
1523 {USB_DEVICE(0x1415, 0x2000)},
1524 {USB_DEVICE(0x06f8, 0x3002)},
1528 MODULE_DEVICE_TABLE(usb
, device_table
);
1530 /* -- device connect -- */
1531 static int sd_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
1533 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1537 static struct usb_driver sd_driver
= {
1538 .name
= MODULE_NAME
,
1539 .id_table
= device_table
,
1541 .disconnect
= gspca_disconnect
,
1543 .suspend
= gspca_suspend
,
1544 .resume
= gspca_resume
,
1545 .reset_resume
= gspca_resume
,
1549 module_usb_driver(sd_driver
);