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>
39 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
40 #define OV534_REG_SUBADDR 0xf2
41 #define OV534_REG_WRITE 0xf3
42 #define OV534_REG_READ 0xf4
43 #define OV534_REG_OPERATION 0xf5
44 #define OV534_REG_STATUS 0xf6
46 #define OV534_OP_WRITE_3 0x37
47 #define OV534_OP_WRITE_2 0x33
48 #define OV534_OP_READ_2 0xf9
50 #define CTRL_TIMEOUT 500
52 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
53 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
54 MODULE_LICENSE("GPL");
71 NCTRLS /* number of controls */
74 /* specific webcam descriptor */
76 struct gspca_dev gspca_dev; /* !! must be the first item */
78 struct gspca_ctrl ctrls[NCTRLS];
92 /* V4L2 controls supported by the driver */
93 static void sethue(struct gspca_dev *gspca_dev);
94 static void setsaturation(struct gspca_dev *gspca_dev);
95 static void setbrightness(struct gspca_dev *gspca_dev);
96 static void setcontrast(struct gspca_dev *gspca_dev);
97 static void setgain(struct gspca_dev *gspca_dev);
98 static void setexposure(struct gspca_dev *gspca_dev);
99 static void setagc(struct gspca_dev *gspca_dev);
100 static void setawb(struct gspca_dev *gspca_dev);
101 static void setaec(struct gspca_dev *gspca_dev);
102 static void setsharpness(struct gspca_dev *gspca_dev);
103 static void sethvflip(struct gspca_dev *gspca_dev);
104 static void setlightfreq(struct gspca_dev *gspca_dev);
106 static int sd_start(struct gspca_dev *gspca_dev);
107 static void sd_stopN(struct gspca_dev *gspca_dev);
109 static const struct ctrl sd_ctrls[] = {
113 .type = V4L2_CTRL_TYPE_INTEGER,
120 .set_control = sethue
124 .id = V4L2_CID_SATURATION,
125 .type = V4L2_CTRL_TYPE_INTEGER,
126 .name = "Saturation",
132 .set_control = setsaturation
136 .id = V4L2_CID_BRIGHTNESS,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Brightness",
144 .set_control = setbrightness
148 .id = V4L2_CID_CONTRAST,
149 .type = V4L2_CTRL_TYPE_INTEGER,
156 .set_control = setcontrast
161 .type = V4L2_CTRL_TYPE_INTEGER,
168 .set_control = setgain
172 .id = V4L2_CID_EXPOSURE,
173 .type = V4L2_CTRL_TYPE_INTEGER,
178 .default_value = 120,
180 .set_control = setexposure
184 .id = V4L2_CID_AUTOGAIN,
185 .type = V4L2_CTRL_TYPE_BOOLEAN,
192 .set_control = setagc
196 .id = V4L2_CID_AUTO_WHITE_BALANCE,
197 .type = V4L2_CTRL_TYPE_BOOLEAN,
198 .name = "Auto White Balance",
204 .set_control = setawb
208 .id = V4L2_CID_EXPOSURE_AUTO,
209 .type = V4L2_CTRL_TYPE_BOOLEAN,
210 .name = "Auto Exposure",
216 .set_control = setaec
220 .id = V4L2_CID_SHARPNESS,
221 .type = V4L2_CTRL_TYPE_INTEGER,
228 .set_control = setsharpness
232 .id = V4L2_CID_HFLIP,
233 .type = V4L2_CTRL_TYPE_BOOLEAN,
240 .set_control = sethvflip
244 .id = V4L2_CID_VFLIP,
245 .type = V4L2_CTRL_TYPE_BOOLEAN,
252 .set_control = sethvflip
256 .id = V4L2_CID_POWER_LINE_FREQUENCY,
257 .type = V4L2_CTRL_TYPE_MENU,
258 .name = "Light Frequency Filter",
264 .set_control = setlightfreq
268 static const struct v4l2_pix_format ov772x_mode[] = {
269 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
270 .bytesperline = 320 * 2,
271 .sizeimage = 320 * 240 * 2,
272 .colorspace = V4L2_COLORSPACE_SRGB,
274 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
275 .bytesperline = 640 * 2,
276 .sizeimage = 640 * 480 * 2,
277 .colorspace = V4L2_COLORSPACE_SRGB,
280 static const struct v4l2_pix_format ov767x_mode[] = {
281 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
283 .sizeimage = 320 * 240 * 3 / 8 + 590,
284 .colorspace = V4L2_COLORSPACE_JPEG},
285 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
287 .sizeimage = 640 * 480 * 3 / 8 + 590,
288 .colorspace = V4L2_COLORSPACE_JPEG},
291 static const u8 qvga_rates[] = {125, 100, 75, 60, 50, 40, 30};
292 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
294 static const struct framerates ov772x_framerates[] = {
297 .nrates = ARRAY_SIZE(qvga_rates),
301 .nrates = ARRAY_SIZE(vga_rates),
310 static const u8 bridge_init_767x[][2] = {
311 /* comments from the ms-win file apollo7670.set */
341 {0xc0, 0x50}, /* HSize 640 */
342 {0xc1, 0x3c}, /* VSize 480 */
343 {0x34, 0x05}, /* enable Audio Suspend mode */
344 {0xc2, 0x0c}, /* Input YUV */
345 {0xc3, 0xf9}, /* enable PRE */
346 {0x34, 0x05}, /* enable Audio Suspend mode */
347 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
348 {0x31, 0xf9}, /* enable 1.8V Suspend */
349 {0x35, 0x02}, /* turn on JPEG */
351 {0x25, 0x42}, /* GPIO[8]:Input */
352 {0x94, 0x11}, /* If the default setting is loaded when
353 * system boots up, this flag is closed here */
355 static const u8 sensor_init_767x[][2] = {
373 {0x7a, 0x2a}, /* set Gamma=1.6 below */
393 {0x14, 0x38}, /* gain max 16x */
473 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
477 {0xa4, 0x8a}, /* Night mode trigger point */
510 static const u8 bridge_start_vga_767x[][2] = {
518 {0x35, 0x02}, /* turn on JPEG */
520 {0xda, 0x00}, /* for higher clock rate(30fps) */
521 {0x34, 0x05}, /* enable Audio Suspend mode */
522 {0xc3, 0xf9}, /* enable PRE */
523 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
524 {0x8d, 0x1c}, /* output YUV */
525 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
526 {0x50, 0x00}, /* H/V divider=0 */
527 {0x51, 0xa0}, /* input H=640/4 */
528 {0x52, 0x3c}, /* input V=480/4 */
529 {0x53, 0x00}, /* offset X=0 */
530 {0x54, 0x00}, /* offset Y=0 */
531 {0x55, 0x00}, /* H/V size[8]=0 */
532 {0x57, 0x00}, /* H-size[9]=0 */
533 {0x5c, 0x00}, /* output size[9:8]=0 */
534 {0x5a, 0xa0}, /* output H=640/4 */
535 {0x5b, 0x78}, /* output V=480/4 */
540 static const u8 sensor_start_vga_767x[][2] = {
546 static const u8 bridge_start_qvga_767x[][2] = {
554 {0x35, 0x02}, /* turn on JPEG */
556 {0xc0, 0x50}, /* CIF HSize 640 */
557 {0xc1, 0x3c}, /* CIF VSize 480 */
558 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
559 {0x8d, 0x1c}, /* output YUV */
560 {0x34, 0x05}, /* enable Audio Suspend mode */
561 {0xc2, 0x4c}, /* output YUV and Enable DCW */
562 {0xc3, 0xf9}, /* enable PRE */
563 {0x1c, 0x00}, /* indirect addressing */
564 {0x1d, 0x48}, /* output YUV422 */
565 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
566 {0x51, 0xa0}, /* DCW input H=640/4 */
567 {0x52, 0x78}, /* DCW input V=480/4 */
568 {0x53, 0x00}, /* offset X=0 */
569 {0x54, 0x00}, /* offset Y=0 */
570 {0x55, 0x00}, /* H/V size[8]=0 */
571 {0x57, 0x00}, /* H-size[9]=0 */
572 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
573 {0x5a, 0x50}, /* DCW output H=320/4 */
574 {0x5b, 0x3c}, /* DCW output V=240/4 */
579 static const u8 sensor_start_qvga_767x[][2] = {
586 static const u8 bridge_init_772x[][2] = {
625 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
626 { 0x1d, 0x00 }, /* payload size */
628 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
629 { 0x1d, 0x58 }, /* frame size */
630 { 0x1d, 0x00 }, /* frame size */
633 { 0x1d, 0x08 }, /* turn on UVC header */
634 { 0x1d, 0x0e }, /* .. */
644 static const u8 sensor_init_772x[][2] = {
647 /*fixme: better have a delay?*/
670 { 0x63, 0xaa }, /* AWB - was e0 */
673 { 0x13, 0xf0 }, /* com8 */
686 { 0x13, 0xff }, /* AWB */
734 { 0x8e, 0x00 }, /* De-noise threshold */
737 static const u8 bridge_start_vga_772x[][2] = {
748 static const u8 sensor_start_vga_772x[][2] = {
758 static const u8 bridge_start_qvga_772x[][2] = {
769 static const u8 sensor_start_qvga_772x[][2] = {
780 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
782 struct usb_device *udev = gspca_dev->dev;
785 if (gspca_dev->usb_err < 0)
788 PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
789 gspca_dev->usb_buf[0] = val;
790 ret = usb_control_msg(udev,
791 usb_sndctrlpipe(udev, 0),
793 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
794 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
796 pr_err("write failed %d\n", ret);
797 gspca_dev->usb_err = ret;
801 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
803 struct usb_device *udev = gspca_dev->dev;
806 if (gspca_dev->usb_err < 0)
808 ret = usb_control_msg(udev,
809 usb_rcvctrlpipe(udev, 0),
811 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
812 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
813 PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
815 pr_err("read failed %d\n", ret);
816 gspca_dev->usb_err = ret;
818 return gspca_dev->usb_buf[0];
821 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
822 * (direction and output)? */
823 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
827 PDEBUG(D_CONF, "led status: %d", status);
829 data = ov534_reg_read(gspca_dev, 0x21);
831 ov534_reg_write(gspca_dev, 0x21, data);
833 data = ov534_reg_read(gspca_dev, 0x23);
839 ov534_reg_write(gspca_dev, 0x23, data);
842 data = ov534_reg_read(gspca_dev, 0x21);
844 ov534_reg_write(gspca_dev, 0x21, data);
848 static int sccb_check_status(struct gspca_dev *gspca_dev)
853 for (i = 0; i < 5; i++) {
855 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
865 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
872 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
874 PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
875 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
876 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
877 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
879 if (!sccb_check_status(gspca_dev)) {
880 pr_err("sccb_reg_write failed\n");
881 gspca_dev->usb_err = -EIO;
885 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
887 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
888 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
889 if (!sccb_check_status(gspca_dev))
890 pr_err("sccb_reg_read failed 1\n");
892 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
893 if (!sccb_check_status(gspca_dev))
894 pr_err("sccb_reg_read failed 2\n");
896 return ov534_reg_read(gspca_dev, OV534_REG_READ);
899 /* output a bridge sequence (reg - val) */
900 static void reg_w_array(struct gspca_dev *gspca_dev,
901 const u8 (*data)[2], int len)
904 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
909 /* output a sensor sequence (reg - val) */
910 static void sccb_w_array(struct gspca_dev *gspca_dev,
911 const u8 (*data)[2], int len)
914 if ((*data)[0] != 0xff) {
915 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
917 sccb_reg_read(gspca_dev, (*data)[1]);
918 sccb_reg_write(gspca_dev, 0xff, 0x00);
924 /* ov772x specific controls */
925 static void set_frame_rate(struct gspca_dev *gspca_dev)
927 struct sd *sd = (struct sd *) gspca_dev;
935 const struct rate_s *r;
936 static const struct rate_s rate_0[] = { /* 640x480 */
937 {60, 0x01, 0xc1, 0x04},
938 {50, 0x01, 0x41, 0x02},
939 {40, 0x02, 0xc1, 0x04},
940 {30, 0x04, 0x81, 0x02},
941 {15, 0x03, 0x41, 0x04},
943 static const struct rate_s rate_1[] = { /* 320x240 */
944 {125, 0x02, 0x81, 0x02},
945 {100, 0x02, 0xc1, 0x04},
946 {75, 0x03, 0xc1, 0x04},
947 {60, 0x04, 0xc1, 0x04},
948 {50, 0x02, 0x41, 0x04},
949 {40, 0x03, 0x41, 0x04},
950 {30, 0x04, 0x41, 0x04},
953 if (sd->sensor != SENSOR_OV772x)
955 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
957 i = ARRAY_SIZE(rate_0);
960 i = ARRAY_SIZE(rate_1);
963 if (sd->frame_rate >= r->fps)
968 sccb_reg_write(gspca_dev, 0x11, r->r11);
969 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
970 ov534_reg_write(gspca_dev, 0xe5, r->re5);
972 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
975 static void sethue(struct gspca_dev *gspca_dev)
977 struct sd *sd = (struct sd *) gspca_dev;
980 val = sd->ctrls[HUE].val;
981 if (sd->sensor == SENSOR_OV767x) {
987 /* fixp_sin and fixp_cos accept only positive values, while
988 * our val is between -90 and 90
992 /* According to the datasheet the registers expect HUESIN and
993 * HUECOS to be the result of the trigonometric functions,
996 * The 0x100 here represents the maximun absolute value
997 * returned byt fixp_sin and fixp_cos, so the scaling will
998 * consider the result like in the interval [-1.0, 1.0].
1000 huesin = fixp_sin(val) * 0x80 / 0x100;
1001 huecos = fixp_cos(val) * 0x80 / 0x100;
1004 sccb_reg_write(gspca_dev, 0xab,
1005 sccb_reg_read(gspca_dev, 0xab) | 0x2);
1008 sccb_reg_write(gspca_dev, 0xab,
1009 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
1012 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
1013 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
1017 static void setsaturation(struct gspca_dev *gspca_dev)
1019 struct sd *sd = (struct sd *) gspca_dev;
1022 val = sd->ctrls[SATURATION].val;
1023 if (sd->sensor == SENSOR_OV767x) {
1025 static u8 color_tb[][6] = {
1026 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1027 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1028 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1029 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1030 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1031 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1032 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1035 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
1036 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
1038 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
1039 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
1043 static void setbrightness(struct gspca_dev *gspca_dev)
1045 struct sd *sd = (struct sd *) gspca_dev;
1048 val = sd->ctrls[BRIGHTNESS].val;
1049 if (sd->sensor == SENSOR_OV767x) {
1052 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
1054 sccb_reg_write(gspca_dev, 0x9b, val);
1058 static void setcontrast(struct gspca_dev *gspca_dev)
1060 struct sd *sd = (struct sd *) gspca_dev;
1063 val = sd->ctrls[CONTRAST].val;
1064 if (sd->sensor == SENSOR_OV767x)
1065 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
1067 sccb_reg_write(gspca_dev, 0x9c, val);
1070 static void setgain(struct gspca_dev *gspca_dev)
1072 struct sd *sd = (struct sd *) gspca_dev;
1075 if (sd->ctrls[AGC].val)
1078 val = sd->ctrls[GAIN].val;
1079 switch (val & 0x30) {
1097 sccb_reg_write(gspca_dev, 0x00, val);
1100 static void setexposure(struct gspca_dev *gspca_dev)
1102 struct sd *sd = (struct sd *) gspca_dev;
1105 if (sd->ctrls[AEC].val)
1108 val = sd->ctrls[EXPOSURE].val;
1109 if (sd->sensor == SENSOR_OV767x) {
1111 /* set only aec[9:2] */
1112 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
1115 /* 'val' is one byte and represents half of the exposure value
1116 * we are going to set into registers, a two bytes value:
1118 * MSB: ((u16) val << 1) >> 8 == val >> 7
1119 * LSB: ((u16) val << 1) & 0xff == val << 1
1121 sccb_reg_write(gspca_dev, 0x08, val >> 7);
1122 sccb_reg_write(gspca_dev, 0x10, val << 1);
1126 static void setagc(struct gspca_dev *gspca_dev)
1128 struct sd *sd = (struct sd *) gspca_dev;
1130 if (sd->ctrls[AGC].val) {
1131 sccb_reg_write(gspca_dev, 0x13,
1132 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1133 sccb_reg_write(gspca_dev, 0x64,
1134 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1136 sccb_reg_write(gspca_dev, 0x13,
1137 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1138 sccb_reg_write(gspca_dev, 0x64,
1139 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1145 static void setawb(struct gspca_dev *gspca_dev)
1147 struct sd *sd = (struct sd *) gspca_dev;
1149 if (sd->ctrls[AWB].val) {
1150 sccb_reg_write(gspca_dev, 0x13,
1151 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1152 if (sd->sensor == SENSOR_OV772x)
1153 sccb_reg_write(gspca_dev, 0x63,
1154 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1156 sccb_reg_write(gspca_dev, 0x13,
1157 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1158 if (sd->sensor == SENSOR_OV772x)
1159 sccb_reg_write(gspca_dev, 0x63,
1160 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1164 static void setaec(struct gspca_dev *gspca_dev)
1166 struct sd *sd = (struct sd *) gspca_dev;
1169 data = sd->sensor == SENSOR_OV767x ?
1170 0x05 : /* agc + aec */
1172 if (sd->ctrls[AEC].val)
1173 sccb_reg_write(gspca_dev, 0x13,
1174 sccb_reg_read(gspca_dev, 0x13) | data);
1176 sccb_reg_write(gspca_dev, 0x13,
1177 sccb_reg_read(gspca_dev, 0x13) & ~data);
1178 if (sd->sensor == SENSOR_OV767x)
1179 sd->ctrls[EXPOSURE].val =
1180 sccb_reg_read(gspca_dev, 10); /* aech */
1182 setexposure(gspca_dev);
1186 static void setsharpness(struct gspca_dev *gspca_dev)
1188 struct sd *sd = (struct sd *) gspca_dev;
1191 val = sd->ctrls[SHARPNESS].val;
1192 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1193 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1196 static void sethvflip(struct gspca_dev *gspca_dev)
1198 struct sd *sd = (struct sd *) gspca_dev;
1201 if (sd->sensor == SENSOR_OV767x) {
1202 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1204 if (sd->ctrls[HFLIP].val)
1206 if (sd->ctrls[VFLIP].val)
1208 sccb_reg_write(gspca_dev, 0x1e, val);
1210 val = sccb_reg_read(gspca_dev, 0x0c);
1212 if (sd->ctrls[HFLIP].val == 0)
1214 if (sd->ctrls[VFLIP].val == 0)
1216 sccb_reg_write(gspca_dev, 0x0c, val);
1220 static void setlightfreq(struct gspca_dev *gspca_dev)
1222 struct sd *sd = (struct sd *) gspca_dev;
1225 val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
1226 if (sd->sensor == SENSOR_OV767x) {
1227 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1229 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1231 sccb_reg_write(gspca_dev, 0x2b, val);
1235 /* this function is called at probe time */
1236 static int sd_config(struct gspca_dev *gspca_dev,
1237 const struct usb_device_id *id)
1239 struct sd *sd = (struct sd *) gspca_dev;
1242 cam = &gspca_dev->cam;
1244 cam->ctrls = sd->ctrls;
1246 cam->cam_mode = ov772x_mode;
1247 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1249 sd->frame_rate = 30;
1254 /* this function is called at probe and resume time */
1255 static int sd_init(struct gspca_dev *gspca_dev)
1257 struct sd *sd = (struct sd *) gspca_dev;
1259 static const struct reg_array bridge_init[NSENSORS] = {
1260 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1261 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1263 static const struct reg_array sensor_init[NSENSORS] = {
1264 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1265 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1269 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1270 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1273 /* initialize the sensor address */
1274 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1277 sccb_reg_write(gspca_dev, 0x12, 0x80);
1280 /* probe the sensor */
1281 sccb_reg_read(gspca_dev, 0x0a);
1282 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1283 sccb_reg_read(gspca_dev, 0x0b);
1284 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1285 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1287 if ((sensor_id & 0xfff0) == 0x7670) {
1288 sd->sensor = SENSOR_OV767x;
1289 gspca_dev->ctrl_dis = (1 << HUE) |
1292 (1 << SHARPNESS); /* auto */
1293 sd->ctrls[SATURATION].min = 0,
1294 sd->ctrls[SATURATION].max = 6,
1295 sd->ctrls[SATURATION].def = 3,
1296 sd->ctrls[BRIGHTNESS].min = -127;
1297 sd->ctrls[BRIGHTNESS].max = 127;
1298 sd->ctrls[BRIGHTNESS].def = 0;
1299 sd->ctrls[CONTRAST].max = 0x80;
1300 sd->ctrls[CONTRAST].def = 0x40;
1301 sd->ctrls[EXPOSURE].min = 0x08;
1302 sd->ctrls[EXPOSURE].max = 0x60;
1303 sd->ctrls[EXPOSURE].def = 0x13;
1304 sd->ctrls[SHARPNESS].max = 9;
1305 sd->ctrls[SHARPNESS].def = 4;
1306 sd->ctrls[HFLIP].def = 1;
1307 gspca_dev->cam.cam_mode = ov767x_mode;
1308 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1310 sd->sensor = SENSOR_OV772x;
1311 gspca_dev->cam.bulk = 1;
1312 gspca_dev->cam.bulk_size = 16384;
1313 gspca_dev->cam.bulk_nurbs = 2;
1314 gspca_dev->cam.mode_framerates = ov772x_framerates;
1318 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1319 bridge_init[sd->sensor].len);
1320 ov534_set_led(gspca_dev, 1);
1321 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1322 sensor_init[sd->sensor].len);
1323 if (sd->sensor == SENSOR_OV767x)
1324 sd_start(gspca_dev);
1325 sd_stopN(gspca_dev);
1326 /* set_frame_rate(gspca_dev); */
1328 return gspca_dev->usb_err;
1331 static int sd_start(struct gspca_dev *gspca_dev)
1333 struct sd *sd = (struct sd *) gspca_dev;
1335 static const struct reg_array bridge_start[NSENSORS][2] = {
1336 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1337 ARRAY_SIZE(bridge_start_qvga_767x)},
1338 {bridge_start_vga_767x,
1339 ARRAY_SIZE(bridge_start_vga_767x)}},
1340 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1341 ARRAY_SIZE(bridge_start_qvga_772x)},
1342 {bridge_start_vga_772x,
1343 ARRAY_SIZE(bridge_start_vga_772x)}},
1345 static const struct reg_array sensor_start[NSENSORS][2] = {
1346 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1347 ARRAY_SIZE(sensor_start_qvga_767x)},
1348 {sensor_start_vga_767x,
1349 ARRAY_SIZE(sensor_start_vga_767x)}},
1350 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1351 ARRAY_SIZE(sensor_start_qvga_772x)},
1352 {sensor_start_vga_772x,
1353 ARRAY_SIZE(sensor_start_vga_772x)}},
1356 /* (from ms-win trace) */
1357 if (sd->sensor == SENSOR_OV767x)
1358 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1359 /* black sun enable ? */
1361 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1362 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1363 bridge_start[sd->sensor][mode].len);
1364 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1365 sensor_start[sd->sensor][mode].len);
1367 set_frame_rate(gspca_dev);
1369 if (!(gspca_dev->ctrl_dis & (1 << HUE)))
1371 setsaturation(gspca_dev);
1372 if (!(gspca_dev->ctrl_dis & (1 << AGC)))
1376 if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
1378 setexposure(gspca_dev);
1379 setbrightness(gspca_dev);
1380 setcontrast(gspca_dev);
1381 if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
1382 setsharpness(gspca_dev);
1383 sethvflip(gspca_dev);
1384 setlightfreq(gspca_dev);
1386 ov534_set_led(gspca_dev, 1);
1387 ov534_reg_write(gspca_dev, 0xe0, 0x00);
1388 return gspca_dev->usb_err;
1391 static void sd_stopN(struct gspca_dev *gspca_dev)
1393 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1394 ov534_set_led(gspca_dev, 0);
1397 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1398 #define UVC_STREAM_EOH (1 << 7)
1399 #define UVC_STREAM_ERR (1 << 6)
1400 #define UVC_STREAM_STI (1 << 5)
1401 #define UVC_STREAM_RES (1 << 4)
1402 #define UVC_STREAM_SCR (1 << 3)
1403 #define UVC_STREAM_PTS (1 << 2)
1404 #define UVC_STREAM_EOF (1 << 1)
1405 #define UVC_STREAM_FID (1 << 0)
1407 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1410 struct sd *sd = (struct sd *) gspca_dev;
1413 int remaining_len = len;
1416 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1418 len = min(remaining_len, payload_len);
1420 /* Payloads are prefixed with a UVC-style header. We
1421 consider a frame to start when the FID toggles, or the PTS
1422 changes. A frame ends when EOF is set, and we've received
1423 the correct number of bytes. */
1425 /* Verify UVC header. Header length is always 12 */
1426 if (data[0] != 12 || len < 12) {
1427 PDEBUG(D_PACK, "bad header");
1432 if (data[1] & UVC_STREAM_ERR) {
1433 PDEBUG(D_PACK, "payload error");
1437 /* Extract PTS and FID */
1438 if (!(data[1] & UVC_STREAM_PTS)) {
1439 PDEBUG(D_PACK, "PTS not present");
1442 this_pts = (data[5] << 24) | (data[4] << 16)
1443 | (data[3] << 8) | data[2];
1444 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1446 /* If PTS or FID has changed, start a new frame. */
1447 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1448 if (gspca_dev->last_packet_type == INTER_PACKET)
1449 gspca_frame_add(gspca_dev, LAST_PACKET,
1451 sd->last_pts = this_pts;
1452 sd->last_fid = this_fid;
1453 gspca_frame_add(gspca_dev, FIRST_PACKET,
1454 data + 12, len - 12);
1455 /* If this packet is marked as EOF, end the frame */
1456 } else if (data[1] & UVC_STREAM_EOF) {
1458 if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
1459 && gspca_dev->image_len + len - 12 !=
1460 gspca_dev->width * gspca_dev->height * 2) {
1461 PDEBUG(D_PACK, "wrong sized frame");
1464 gspca_frame_add(gspca_dev, LAST_PACKET,
1465 data + 12, len - 12);
1468 /* Add the data from this payload */
1469 gspca_frame_add(gspca_dev, INTER_PACKET,
1470 data + 12, len - 12);
1473 /* Done this payload */
1477 /* Discard data until a new frame starts. */
1478 gspca_dev->last_packet_type = DISCARD_PACKET;
1481 remaining_len -= len;
1483 } while (remaining_len > 0);
1486 static int sd_querymenu(struct gspca_dev *gspca_dev,
1487 struct v4l2_querymenu *menu)
1490 case V4L2_CID_POWER_LINE_FREQUENCY:
1491 switch (menu->index) {
1492 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1493 strcpy((char *) menu->name, "Disabled");
1495 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1496 strcpy((char *) menu->name, "50 Hz");
1505 /* get stream parameters (framerate) */
1506 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1507 struct v4l2_streamparm *parm)
1509 struct v4l2_captureparm *cp = &parm->parm.capture;
1510 struct v4l2_fract *tpf = &cp->timeperframe;
1511 struct sd *sd = (struct sd *) gspca_dev;
1513 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1515 tpf->denominator = sd->frame_rate;
1518 /* set stream parameters (framerate) */
1519 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1520 struct v4l2_streamparm *parm)
1522 struct v4l2_captureparm *cp = &parm->parm.capture;
1523 struct v4l2_fract *tpf = &cp->timeperframe;
1524 struct sd *sd = (struct sd *) gspca_dev;
1526 /* Set requested framerate */
1527 sd->frame_rate = tpf->denominator / tpf->numerator;
1528 if (gspca_dev->streaming)
1529 set_frame_rate(gspca_dev);
1531 /* Return the actual framerate */
1533 tpf->denominator = sd->frame_rate;
1536 /* sub-driver description */
1537 static const struct sd_desc sd_desc = {
1538 .name = MODULE_NAME,
1540 .nctrls = ARRAY_SIZE(sd_ctrls),
1541 .config = sd_config,
1545 .pkt_scan = sd_pkt_scan,
1546 .querymenu = sd_querymenu,
1547 .get_streamparm = sd_get_streamparm,
1548 .set_streamparm = sd_set_streamparm,
1551 /* -- module initialisation -- */
1552 static const struct usb_device_id device_table[] = {
1553 {USB_DEVICE(0x1415, 0x2000)},
1554 {USB_DEVICE(0x06f8, 0x3002)},
1558 MODULE_DEVICE_TABLE(usb, device_table);
1560 /* -- device connect -- */
1561 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1563 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1567 static struct usb_driver sd_driver = {
1568 .name = MODULE_NAME,
1569 .id_table = device_table,
1571 .disconnect = gspca_disconnect,
1573 .suspend = gspca_suspend,
1574 .resume = gspca_resume,
1578 module_usb_driver(sd_driver);