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++) {
854 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
864 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
871 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
873 PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
874 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
875 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
876 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
878 if (!sccb_check_status(gspca_dev)) {
879 pr_err("sccb_reg_write failed\n");
880 gspca_dev->usb_err = -EIO;
884 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
886 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
887 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
888 if (!sccb_check_status(gspca_dev))
889 pr_err("sccb_reg_read failed 1\n");
891 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
892 if (!sccb_check_status(gspca_dev))
893 pr_err("sccb_reg_read failed 2\n");
895 return ov534_reg_read(gspca_dev, OV534_REG_READ);
898 /* output a bridge sequence (reg - val) */
899 static void reg_w_array(struct gspca_dev *gspca_dev,
900 const u8 (*data)[2], int len)
903 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
908 /* output a sensor sequence (reg - val) */
909 static void sccb_w_array(struct gspca_dev *gspca_dev,
910 const u8 (*data)[2], int len)
913 if ((*data)[0] != 0xff) {
914 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
916 sccb_reg_read(gspca_dev, (*data)[1]);
917 sccb_reg_write(gspca_dev, 0xff, 0x00);
923 /* ov772x specific controls */
924 static void set_frame_rate(struct gspca_dev *gspca_dev)
926 struct sd *sd = (struct sd *) gspca_dev;
934 const struct rate_s *r;
935 static const struct rate_s rate_0[] = { /* 640x480 */
936 {60, 0x01, 0xc1, 0x04},
937 {50, 0x01, 0x41, 0x02},
938 {40, 0x02, 0xc1, 0x04},
939 {30, 0x04, 0x81, 0x02},
940 {15, 0x03, 0x41, 0x04},
942 static const struct rate_s rate_1[] = { /* 320x240 */
943 {125, 0x02, 0x81, 0x02},
944 {100, 0x02, 0xc1, 0x04},
945 {75, 0x03, 0xc1, 0x04},
946 {60, 0x04, 0xc1, 0x04},
947 {50, 0x02, 0x41, 0x04},
948 {40, 0x03, 0x41, 0x04},
949 {30, 0x04, 0x41, 0x04},
952 if (sd->sensor != SENSOR_OV772x)
954 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
956 i = ARRAY_SIZE(rate_0);
959 i = ARRAY_SIZE(rate_1);
962 if (sd->frame_rate >= r->fps)
967 sccb_reg_write(gspca_dev, 0x11, r->r11);
968 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
969 ov534_reg_write(gspca_dev, 0xe5, r->re5);
971 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
974 static void sethue(struct gspca_dev *gspca_dev)
976 struct sd *sd = (struct sd *) gspca_dev;
979 val = sd->ctrls[HUE].val;
980 if (sd->sensor == SENSOR_OV767x) {
986 /* fixp_sin and fixp_cos accept only positive values, while
987 * our val is between -90 and 90
991 /* According to the datasheet the registers expect HUESIN and
992 * HUECOS to be the result of the trigonometric functions,
995 * The 0x100 here represents the maximun absolute value
996 * returned byt fixp_sin and fixp_cos, so the scaling will
997 * consider the result like in the interval [-1.0, 1.0].
999 huesin = fixp_sin(val) * 0x80 / 0x100;
1000 huecos = fixp_cos(val) * 0x80 / 0x100;
1003 sccb_reg_write(gspca_dev, 0xab,
1004 sccb_reg_read(gspca_dev, 0xab) | 0x2);
1007 sccb_reg_write(gspca_dev, 0xab,
1008 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
1011 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
1012 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
1016 static void setsaturation(struct gspca_dev *gspca_dev)
1018 struct sd *sd = (struct sd *) gspca_dev;
1021 val = sd->ctrls[SATURATION].val;
1022 if (sd->sensor == SENSOR_OV767x) {
1024 static u8 color_tb[][6] = {
1025 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
1026 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
1027 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
1028 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
1029 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
1030 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
1031 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
1034 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
1035 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
1037 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
1038 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
1042 static void setbrightness(struct gspca_dev *gspca_dev)
1044 struct sd *sd = (struct sd *) gspca_dev;
1047 val = sd->ctrls[BRIGHTNESS].val;
1048 if (sd->sensor == SENSOR_OV767x) {
1051 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
1053 sccb_reg_write(gspca_dev, 0x9b, val);
1057 static void setcontrast(struct gspca_dev *gspca_dev)
1059 struct sd *sd = (struct sd *) gspca_dev;
1062 val = sd->ctrls[CONTRAST].val;
1063 if (sd->sensor == SENSOR_OV767x)
1064 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
1066 sccb_reg_write(gspca_dev, 0x9c, val);
1069 static void setgain(struct gspca_dev *gspca_dev)
1071 struct sd *sd = (struct sd *) gspca_dev;
1074 if (sd->ctrls[AGC].val)
1077 val = sd->ctrls[GAIN].val;
1078 switch (val & 0x30) {
1096 sccb_reg_write(gspca_dev, 0x00, val);
1099 static void setexposure(struct gspca_dev *gspca_dev)
1101 struct sd *sd = (struct sd *) gspca_dev;
1104 if (sd->ctrls[AEC].val)
1107 val = sd->ctrls[EXPOSURE].val;
1108 if (sd->sensor == SENSOR_OV767x) {
1110 /* set only aec[9:2] */
1111 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
1114 /* 'val' is one byte and represents half of the exposure value
1115 * we are going to set into registers, a two bytes value:
1117 * MSB: ((u16) val << 1) >> 8 == val >> 7
1118 * LSB: ((u16) val << 1) & 0xff == val << 1
1120 sccb_reg_write(gspca_dev, 0x08, val >> 7);
1121 sccb_reg_write(gspca_dev, 0x10, val << 1);
1125 static void setagc(struct gspca_dev *gspca_dev)
1127 struct sd *sd = (struct sd *) gspca_dev;
1129 if (sd->ctrls[AGC].val) {
1130 sccb_reg_write(gspca_dev, 0x13,
1131 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1132 sccb_reg_write(gspca_dev, 0x64,
1133 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1135 sccb_reg_write(gspca_dev, 0x13,
1136 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1137 sccb_reg_write(gspca_dev, 0x64,
1138 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1144 static void setawb(struct gspca_dev *gspca_dev)
1146 struct sd *sd = (struct sd *) gspca_dev;
1148 if (sd->ctrls[AWB].val) {
1149 sccb_reg_write(gspca_dev, 0x13,
1150 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1151 if (sd->sensor == SENSOR_OV772x)
1152 sccb_reg_write(gspca_dev, 0x63,
1153 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1155 sccb_reg_write(gspca_dev, 0x13,
1156 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1157 if (sd->sensor == SENSOR_OV772x)
1158 sccb_reg_write(gspca_dev, 0x63,
1159 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1163 static void setaec(struct gspca_dev *gspca_dev)
1165 struct sd *sd = (struct sd *) gspca_dev;
1168 data = sd->sensor == SENSOR_OV767x ?
1169 0x05 : /* agc + aec */
1171 if (sd->ctrls[AEC].val)
1172 sccb_reg_write(gspca_dev, 0x13,
1173 sccb_reg_read(gspca_dev, 0x13) | data);
1175 sccb_reg_write(gspca_dev, 0x13,
1176 sccb_reg_read(gspca_dev, 0x13) & ~data);
1177 if (sd->sensor == SENSOR_OV767x)
1178 sd->ctrls[EXPOSURE].val =
1179 sccb_reg_read(gspca_dev, 10); /* aech */
1181 setexposure(gspca_dev);
1185 static void setsharpness(struct gspca_dev *gspca_dev)
1187 struct sd *sd = (struct sd *) gspca_dev;
1190 val = sd->ctrls[SHARPNESS].val;
1191 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1192 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1195 static void sethvflip(struct gspca_dev *gspca_dev)
1197 struct sd *sd = (struct sd *) gspca_dev;
1200 if (sd->sensor == SENSOR_OV767x) {
1201 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1203 if (sd->ctrls[HFLIP].val)
1205 if (sd->ctrls[VFLIP].val)
1207 sccb_reg_write(gspca_dev, 0x1e, val);
1209 val = sccb_reg_read(gspca_dev, 0x0c);
1211 if (sd->ctrls[HFLIP].val == 0)
1213 if (sd->ctrls[VFLIP].val == 0)
1215 sccb_reg_write(gspca_dev, 0x0c, val);
1219 static void setlightfreq(struct gspca_dev *gspca_dev)
1221 struct sd *sd = (struct sd *) gspca_dev;
1224 val = sd->ctrls[LIGHTFREQ].val ? 0x9e : 0x00;
1225 if (sd->sensor == SENSOR_OV767x) {
1226 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1228 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1230 sccb_reg_write(gspca_dev, 0x2b, val);
1234 /* this function is called at probe time */
1235 static int sd_config(struct gspca_dev *gspca_dev,
1236 const struct usb_device_id *id)
1238 struct sd *sd = (struct sd *) gspca_dev;
1241 cam = &gspca_dev->cam;
1243 cam->ctrls = sd->ctrls;
1245 cam->cam_mode = ov772x_mode;
1246 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1248 sd->frame_rate = 30;
1253 /* this function is called at probe and resume time */
1254 static int sd_init(struct gspca_dev *gspca_dev)
1256 struct sd *sd = (struct sd *) gspca_dev;
1258 static const struct reg_array bridge_init[NSENSORS] = {
1259 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1260 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1262 static const struct reg_array sensor_init[NSENSORS] = {
1263 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1264 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1268 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1269 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1272 /* initialize the sensor address */
1273 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1276 sccb_reg_write(gspca_dev, 0x12, 0x80);
1279 /* probe the sensor */
1280 sccb_reg_read(gspca_dev, 0x0a);
1281 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1282 sccb_reg_read(gspca_dev, 0x0b);
1283 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1284 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1286 if ((sensor_id & 0xfff0) == 0x7670) {
1287 sd->sensor = SENSOR_OV767x;
1288 gspca_dev->ctrl_dis = (1 << HUE) |
1291 (1 << SHARPNESS); /* auto */
1292 sd->ctrls[SATURATION].min = 0,
1293 sd->ctrls[SATURATION].max = 6,
1294 sd->ctrls[SATURATION].def = 3,
1295 sd->ctrls[BRIGHTNESS].min = -127;
1296 sd->ctrls[BRIGHTNESS].max = 127;
1297 sd->ctrls[BRIGHTNESS].def = 0;
1298 sd->ctrls[CONTRAST].max = 0x80;
1299 sd->ctrls[CONTRAST].def = 0x40;
1300 sd->ctrls[EXPOSURE].min = 0x08;
1301 sd->ctrls[EXPOSURE].max = 0x60;
1302 sd->ctrls[EXPOSURE].def = 0x13;
1303 sd->ctrls[SHARPNESS].max = 9;
1304 sd->ctrls[SHARPNESS].def = 4;
1305 sd->ctrls[HFLIP].def = 1;
1306 gspca_dev->cam.cam_mode = ov767x_mode;
1307 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1309 sd->sensor = SENSOR_OV772x;
1310 gspca_dev->cam.bulk = 1;
1311 gspca_dev->cam.bulk_size = 16384;
1312 gspca_dev->cam.bulk_nurbs = 2;
1313 gspca_dev->cam.mode_framerates = ov772x_framerates;
1317 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1318 bridge_init[sd->sensor].len);
1319 ov534_set_led(gspca_dev, 1);
1320 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1321 sensor_init[sd->sensor].len);
1322 if (sd->sensor == SENSOR_OV767x)
1323 sd_start(gspca_dev);
1324 sd_stopN(gspca_dev);
1325 /* set_frame_rate(gspca_dev); */
1327 return gspca_dev->usb_err;
1330 static int sd_start(struct gspca_dev *gspca_dev)
1332 struct sd *sd = (struct sd *) gspca_dev;
1334 static const struct reg_array bridge_start[NSENSORS][2] = {
1335 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1336 ARRAY_SIZE(bridge_start_qvga_767x)},
1337 {bridge_start_vga_767x,
1338 ARRAY_SIZE(bridge_start_vga_767x)}},
1339 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1340 ARRAY_SIZE(bridge_start_qvga_772x)},
1341 {bridge_start_vga_772x,
1342 ARRAY_SIZE(bridge_start_vga_772x)}},
1344 static const struct reg_array sensor_start[NSENSORS][2] = {
1345 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1346 ARRAY_SIZE(sensor_start_qvga_767x)},
1347 {sensor_start_vga_767x,
1348 ARRAY_SIZE(sensor_start_vga_767x)}},
1349 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1350 ARRAY_SIZE(sensor_start_qvga_772x)},
1351 {sensor_start_vga_772x,
1352 ARRAY_SIZE(sensor_start_vga_772x)}},
1355 /* (from ms-win trace) */
1356 if (sd->sensor == SENSOR_OV767x)
1357 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1358 /* black sun enable ? */
1360 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1361 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1362 bridge_start[sd->sensor][mode].len);
1363 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1364 sensor_start[sd->sensor][mode].len);
1366 set_frame_rate(gspca_dev);
1368 if (!(gspca_dev->ctrl_dis & (1 << HUE)))
1370 setsaturation(gspca_dev);
1371 if (!(gspca_dev->ctrl_dis & (1 << AGC)))
1375 if (!(gspca_dev->ctrl_dis & (1 << GAIN)))
1377 setexposure(gspca_dev);
1378 setbrightness(gspca_dev);
1379 setcontrast(gspca_dev);
1380 if (!(gspca_dev->ctrl_dis & (1 << SHARPNESS)))
1381 setsharpness(gspca_dev);
1382 sethvflip(gspca_dev);
1383 setlightfreq(gspca_dev);
1385 ov534_set_led(gspca_dev, 1);
1386 ov534_reg_write(gspca_dev, 0xe0, 0x00);
1387 return gspca_dev->usb_err;
1390 static void sd_stopN(struct gspca_dev *gspca_dev)
1392 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1393 ov534_set_led(gspca_dev, 0);
1396 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1397 #define UVC_STREAM_EOH (1 << 7)
1398 #define UVC_STREAM_ERR (1 << 6)
1399 #define UVC_STREAM_STI (1 << 5)
1400 #define UVC_STREAM_RES (1 << 4)
1401 #define UVC_STREAM_SCR (1 << 3)
1402 #define UVC_STREAM_PTS (1 << 2)
1403 #define UVC_STREAM_EOF (1 << 1)
1404 #define UVC_STREAM_FID (1 << 0)
1406 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1409 struct sd *sd = (struct sd *) gspca_dev;
1412 int remaining_len = len;
1415 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1417 len = min(remaining_len, payload_len);
1419 /* Payloads are prefixed with a UVC-style header. We
1420 consider a frame to start when the FID toggles, or the PTS
1421 changes. A frame ends when EOF is set, and we've received
1422 the correct number of bytes. */
1424 /* Verify UVC header. Header length is always 12 */
1425 if (data[0] != 12 || len < 12) {
1426 PDEBUG(D_PACK, "bad header");
1431 if (data[1] & UVC_STREAM_ERR) {
1432 PDEBUG(D_PACK, "payload error");
1436 /* Extract PTS and FID */
1437 if (!(data[1] & UVC_STREAM_PTS)) {
1438 PDEBUG(D_PACK, "PTS not present");
1441 this_pts = (data[5] << 24) | (data[4] << 16)
1442 | (data[3] << 8) | data[2];
1443 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1445 /* If PTS or FID has changed, start a new frame. */
1446 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1447 if (gspca_dev->last_packet_type == INTER_PACKET)
1448 gspca_frame_add(gspca_dev, LAST_PACKET,
1450 sd->last_pts = this_pts;
1451 sd->last_fid = this_fid;
1452 gspca_frame_add(gspca_dev, FIRST_PACKET,
1453 data + 12, len - 12);
1454 /* If this packet is marked as EOF, end the frame */
1455 } else if (data[1] & UVC_STREAM_EOF) {
1457 if (gspca_dev->pixfmt == V4L2_PIX_FMT_YUYV
1458 && gspca_dev->image_len + len - 12 !=
1459 gspca_dev->width * gspca_dev->height * 2) {
1460 PDEBUG(D_PACK, "wrong sized frame");
1463 gspca_frame_add(gspca_dev, LAST_PACKET,
1464 data + 12, len - 12);
1467 /* Add the data from this payload */
1468 gspca_frame_add(gspca_dev, INTER_PACKET,
1469 data + 12, len - 12);
1472 /* Done this payload */
1476 /* Discard data until a new frame starts. */
1477 gspca_dev->last_packet_type = DISCARD_PACKET;
1480 remaining_len -= len;
1482 } while (remaining_len > 0);
1485 static int sd_querymenu(struct gspca_dev *gspca_dev,
1486 struct v4l2_querymenu *menu)
1489 case V4L2_CID_POWER_LINE_FREQUENCY:
1490 switch (menu->index) {
1491 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1492 strcpy((char *) menu->name, "Disabled");
1494 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1495 strcpy((char *) menu->name, "50 Hz");
1504 /* get stream parameters (framerate) */
1505 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1506 struct v4l2_streamparm *parm)
1508 struct v4l2_captureparm *cp = &parm->parm.capture;
1509 struct v4l2_fract *tpf = &cp->timeperframe;
1510 struct sd *sd = (struct sd *) gspca_dev;
1512 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1514 tpf->denominator = sd->frame_rate;
1517 /* set stream parameters (framerate) */
1518 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1519 struct v4l2_streamparm *parm)
1521 struct v4l2_captureparm *cp = &parm->parm.capture;
1522 struct v4l2_fract *tpf = &cp->timeperframe;
1523 struct sd *sd = (struct sd *) gspca_dev;
1525 /* Set requested framerate */
1526 sd->frame_rate = tpf->denominator / tpf->numerator;
1527 if (gspca_dev->streaming)
1528 set_frame_rate(gspca_dev);
1530 /* Return the actual framerate */
1532 tpf->denominator = sd->frame_rate;
1535 /* sub-driver description */
1536 static const struct sd_desc sd_desc = {
1537 .name = MODULE_NAME,
1539 .nctrls = ARRAY_SIZE(sd_ctrls),
1540 .config = sd_config,
1544 .pkt_scan = sd_pkt_scan,
1545 .querymenu = sd_querymenu,
1546 .get_streamparm = sd_get_streamparm,
1547 .set_streamparm = sd_set_streamparm,
1550 /* -- module initialisation -- */
1551 static const struct usb_device_id device_table[] = {
1552 {USB_DEVICE(0x1415, 0x2000)},
1553 {USB_DEVICE(0x06f8, 0x3002)},
1557 MODULE_DEVICE_TABLE(usb, device_table);
1559 /* -- device connect -- */
1560 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1562 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1566 static struct usb_driver sd_driver = {
1567 .name = MODULE_NAME,
1568 .id_table = device_table,
1570 .disconnect = gspca_disconnect,
1572 .suspend = gspca_suspend,
1573 .resume = gspca_resume,
1577 module_usb_driver(sd_driver);