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
55 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
56 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
57 MODULE_LICENSE("GPL");
59 /* specific webcam descriptor */
61 struct gspca_dev gspca_dev; /* !! must be the first item */
63 struct v4l2_ctrl_handler ctrl_handler;
64 struct v4l2_ctrl *hue;
65 struct v4l2_ctrl *saturation;
66 struct v4l2_ctrl *brightness;
67 struct v4l2_ctrl *contrast;
68 struct { /* gain control cluster */
69 struct v4l2_ctrl *autogain;
70 struct v4l2_ctrl *gain;
72 struct v4l2_ctrl *autowhitebalance;
73 struct { /* exposure control cluster */
74 struct v4l2_ctrl *autoexposure;
75 struct v4l2_ctrl *exposure;
77 struct v4l2_ctrl *sharpness;
78 struct v4l2_ctrl *hflip;
79 struct v4l2_ctrl *vflip;
80 struct v4l2_ctrl *plfreq;
94 static int sd_start(struct gspca_dev *gspca_dev);
95 static void sd_stopN(struct gspca_dev *gspca_dev);
98 static const struct v4l2_pix_format ov772x_mode[] = {
99 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
100 .bytesperline = 320 * 2,
101 .sizeimage = 320 * 240 * 2,
102 .colorspace = V4L2_COLORSPACE_SRGB,
104 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
105 .bytesperline = 640 * 2,
106 .sizeimage = 640 * 480 * 2,
107 .colorspace = V4L2_COLORSPACE_SRGB,
110 static const struct v4l2_pix_format ov767x_mode[] = {
111 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
113 .sizeimage = 320 * 240 * 3 / 8 + 590,
114 .colorspace = V4L2_COLORSPACE_JPEG},
115 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
117 .sizeimage = 640 * 480 * 3 / 8 + 590,
118 .colorspace = V4L2_COLORSPACE_JPEG},
121 static const u8 qvga_rates[] = {187, 150, 137, 125, 100, 75, 60, 50, 37, 30};
122 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
124 static const struct framerates ov772x_framerates[] = {
127 .nrates = ARRAY_SIZE(qvga_rates),
131 .nrates = ARRAY_SIZE(vga_rates),
140 static const u8 bridge_init_767x[][2] = {
141 /* comments from the ms-win file apollo7670.set */
171 {0xc0, 0x50}, /* HSize 640 */
172 {0xc1, 0x3c}, /* VSize 480 */
173 {0x34, 0x05}, /* enable Audio Suspend mode */
174 {0xc2, 0x0c}, /* Input YUV */
175 {0xc3, 0xf9}, /* enable PRE */
176 {0x34, 0x05}, /* enable Audio Suspend mode */
177 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
178 {0x31, 0xf9}, /* enable 1.8V Suspend */
179 {0x35, 0x02}, /* turn on JPEG */
181 {0x25, 0x42}, /* GPIO[8]:Input */
182 {0x94, 0x11}, /* If the default setting is loaded when
183 * system boots up, this flag is closed here */
185 static const u8 sensor_init_767x[][2] = {
203 {0x7a, 0x2a}, /* set Gamma=1.6 below */
223 {0x14, 0x38}, /* gain max 16x */
303 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
307 {0xa4, 0x8a}, /* Night mode trigger point */
340 static const u8 bridge_start_vga_767x[][2] = {
348 {0x35, 0x02}, /* turn on JPEG */
350 {0xda, 0x00}, /* for higher clock rate(30fps) */
351 {0x34, 0x05}, /* enable Audio Suspend mode */
352 {0xc3, 0xf9}, /* enable PRE */
353 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
354 {0x8d, 0x1c}, /* output YUV */
355 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
356 {0x50, 0x00}, /* H/V divider=0 */
357 {0x51, 0xa0}, /* input H=640/4 */
358 {0x52, 0x3c}, /* input V=480/4 */
359 {0x53, 0x00}, /* offset X=0 */
360 {0x54, 0x00}, /* offset Y=0 */
361 {0x55, 0x00}, /* H/V size[8]=0 */
362 {0x57, 0x00}, /* H-size[9]=0 */
363 {0x5c, 0x00}, /* output size[9:8]=0 */
364 {0x5a, 0xa0}, /* output H=640/4 */
365 {0x5b, 0x78}, /* output V=480/4 */
370 static const u8 sensor_start_vga_767x[][2] = {
376 static const u8 bridge_start_qvga_767x[][2] = {
384 {0x35, 0x02}, /* turn on JPEG */
386 {0xc0, 0x50}, /* CIF HSize 640 */
387 {0xc1, 0x3c}, /* CIF VSize 480 */
388 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
389 {0x8d, 0x1c}, /* output YUV */
390 {0x34, 0x05}, /* enable Audio Suspend mode */
391 {0xc2, 0x4c}, /* output YUV and Enable DCW */
392 {0xc3, 0xf9}, /* enable PRE */
393 {0x1c, 0x00}, /* indirect addressing */
394 {0x1d, 0x48}, /* output YUV422 */
395 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
396 {0x51, 0xa0}, /* DCW input H=640/4 */
397 {0x52, 0x78}, /* DCW input V=480/4 */
398 {0x53, 0x00}, /* offset X=0 */
399 {0x54, 0x00}, /* offset Y=0 */
400 {0x55, 0x00}, /* H/V size[8]=0 */
401 {0x57, 0x00}, /* H-size[9]=0 */
402 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
403 {0x5a, 0x50}, /* DCW output H=320/4 */
404 {0x5b, 0x3c}, /* DCW output V=240/4 */
409 static const u8 sensor_start_qvga_767x[][2] = {
416 static const u8 bridge_init_772x[][2] = {
455 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
456 { 0x1d, 0x00 }, /* payload size */
458 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
459 { 0x1d, 0x58 }, /* frame size */
460 { 0x1d, 0x00 }, /* frame size */
463 { 0x1d, 0x08 }, /* turn on UVC header */
464 { 0x1d, 0x0e }, /* .. */
474 static const u8 sensor_init_772x[][2] = {
477 /*fixme: better have a delay?*/
500 { 0x63, 0xaa }, /* AWB - was e0 */
503 { 0x13, 0xf0 }, /* com8 */
516 { 0x13, 0xff }, /* AWB */
564 { 0x8e, 0x00 }, /* De-noise threshold */
567 static const u8 bridge_start_vga_772x[][2] = {
578 static const u8 sensor_start_vga_772x[][2] = {
588 static const u8 bridge_start_qvga_772x[][2] = {
599 static const u8 sensor_start_qvga_772x[][2] = {
610 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
612 struct usb_device *udev = gspca_dev->dev;
615 if (gspca_dev->usb_err < 0)
618 PDEBUG(D_USBO, "SET 01 0000 %04x %02x", reg, val);
619 gspca_dev->usb_buf[0] = val;
620 ret = usb_control_msg(udev,
621 usb_sndctrlpipe(udev, 0),
623 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
624 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
626 pr_err("write failed %d\n", ret);
627 gspca_dev->usb_err = ret;
631 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
633 struct usb_device *udev = gspca_dev->dev;
636 if (gspca_dev->usb_err < 0)
638 ret = usb_control_msg(udev,
639 usb_rcvctrlpipe(udev, 0),
641 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
642 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
643 PDEBUG(D_USBI, "GET 01 0000 %04x %02x", reg, gspca_dev->usb_buf[0]);
645 pr_err("read failed %d\n", ret);
646 gspca_dev->usb_err = ret;
648 return gspca_dev->usb_buf[0];
651 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
652 * (direction and output)? */
653 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
657 PDEBUG(D_CONF, "led status: %d", status);
659 data = ov534_reg_read(gspca_dev, 0x21);
661 ov534_reg_write(gspca_dev, 0x21, data);
663 data = ov534_reg_read(gspca_dev, 0x23);
669 ov534_reg_write(gspca_dev, 0x23, data);
672 data = ov534_reg_read(gspca_dev, 0x21);
674 ov534_reg_write(gspca_dev, 0x21, data);
678 static int sccb_check_status(struct gspca_dev *gspca_dev)
683 for (i = 0; i < 5; i++) {
685 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
695 PERR("sccb status 0x%02x, attempt %d/5",
702 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
704 PDEBUG(D_USBO, "sccb write: %02x %02x", reg, val);
705 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
706 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
707 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
709 if (!sccb_check_status(gspca_dev)) {
710 pr_err("sccb_reg_write failed\n");
711 gspca_dev->usb_err = -EIO;
715 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
717 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
718 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
719 if (!sccb_check_status(gspca_dev))
720 pr_err("sccb_reg_read failed 1\n");
722 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
723 if (!sccb_check_status(gspca_dev))
724 pr_err("sccb_reg_read failed 2\n");
726 return ov534_reg_read(gspca_dev, OV534_REG_READ);
729 /* output a bridge sequence (reg - val) */
730 static void reg_w_array(struct gspca_dev *gspca_dev,
731 const u8 (*data)[2], int len)
734 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
739 /* output a sensor sequence (reg - val) */
740 static void sccb_w_array(struct gspca_dev *gspca_dev,
741 const u8 (*data)[2], int len)
744 if ((*data)[0] != 0xff) {
745 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
747 sccb_reg_read(gspca_dev, (*data)[1]);
748 sccb_reg_write(gspca_dev, 0xff, 0x00);
754 /* ov772x specific controls */
755 static void set_frame_rate(struct gspca_dev *gspca_dev)
757 struct sd *sd = (struct sd *) gspca_dev;
765 const struct rate_s *r;
766 static const struct rate_s rate_0[] = { /* 640x480 */
767 {60, 0x01, 0xc1, 0x04},
768 {50, 0x01, 0x41, 0x02},
769 {40, 0x02, 0xc1, 0x04},
770 {30, 0x04, 0x81, 0x02},
771 {15, 0x03, 0x41, 0x04},
773 static const struct rate_s rate_1[] = { /* 320x240 */
774 /* {205, 0x01, 0xc1, 0x02}, * 205 FPS: video is partly corrupt */
775 {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
776 {150, 0x01, 0xc1, 0x04},
777 {137, 0x02, 0xc1, 0x02},
778 {125, 0x02, 0x81, 0x02},
779 {100, 0x02, 0xc1, 0x04},
780 {75, 0x03, 0xc1, 0x04},
781 {60, 0x04, 0xc1, 0x04},
782 {50, 0x02, 0x41, 0x04},
783 {37, 0x03, 0x41, 0x04},
784 {30, 0x04, 0x41, 0x04},
787 if (sd->sensor != SENSOR_OV772x)
789 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
791 i = ARRAY_SIZE(rate_0);
794 i = ARRAY_SIZE(rate_1);
797 if (sd->frame_rate >= r->fps)
802 sccb_reg_write(gspca_dev, 0x11, r->r11);
803 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
804 ov534_reg_write(gspca_dev, 0xe5, r->re5);
806 PDEBUG(D_PROBE, "frame_rate: %d", r->fps);
809 static void sethue(struct gspca_dev *gspca_dev, s32 val)
811 struct sd *sd = (struct sd *) gspca_dev;
813 if (sd->sensor == SENSOR_OV767x) {
819 /* According to the datasheet the registers expect HUESIN and
820 * HUECOS to be the result of the trigonometric functions,
823 * The 0x7fff here represents the maximum absolute value
824 * returned byt fixp_sin and fixp_cos, so the scaling will
825 * consider the result like in the interval [-1.0, 1.0].
827 huesin = fixp_sin16(val) * 0x80 / 0x7fff;
828 huecos = fixp_cos16(val) * 0x80 / 0x7fff;
831 sccb_reg_write(gspca_dev, 0xab,
832 sccb_reg_read(gspca_dev, 0xab) | 0x2);
835 sccb_reg_write(gspca_dev, 0xab,
836 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
839 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
840 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
844 static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
846 struct sd *sd = (struct sd *) gspca_dev;
848 if (sd->sensor == SENSOR_OV767x) {
850 static u8 color_tb[][6] = {
851 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
852 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
853 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
854 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
855 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
856 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
857 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
860 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
861 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
863 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
864 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
868 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
870 struct sd *sd = (struct sd *) gspca_dev;
872 if (sd->sensor == SENSOR_OV767x) {
875 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
877 sccb_reg_write(gspca_dev, 0x9b, val);
881 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
883 struct sd *sd = (struct sd *) gspca_dev;
885 if (sd->sensor == SENSOR_OV767x)
886 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
888 sccb_reg_write(gspca_dev, 0x9c, val);
891 static void setgain(struct gspca_dev *gspca_dev, s32 val)
893 switch (val & 0x30) {
911 sccb_reg_write(gspca_dev, 0x00, val);
914 static s32 getgain(struct gspca_dev *gspca_dev)
916 return sccb_reg_read(gspca_dev, 0x00);
919 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
921 struct sd *sd = (struct sd *) gspca_dev;
923 if (sd->sensor == SENSOR_OV767x) {
925 /* set only aec[9:2] */
926 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
929 /* 'val' is one byte and represents half of the exposure value
930 * we are going to set into registers, a two bytes value:
932 * MSB: ((u16) val << 1) >> 8 == val >> 7
933 * LSB: ((u16) val << 1) & 0xff == val << 1
935 sccb_reg_write(gspca_dev, 0x08, val >> 7);
936 sccb_reg_write(gspca_dev, 0x10, val << 1);
940 static s32 getexposure(struct gspca_dev *gspca_dev)
942 struct sd *sd = (struct sd *) gspca_dev;
944 if (sd->sensor == SENSOR_OV767x) {
945 /* get only aec[9:2] */
946 return sccb_reg_read(gspca_dev, 0x10); /* aech */
948 u8 hi = sccb_reg_read(gspca_dev, 0x08);
949 u8 lo = sccb_reg_read(gspca_dev, 0x10);
950 return (hi << 8 | lo) >> 1;
954 static void setagc(struct gspca_dev *gspca_dev, s32 val)
957 sccb_reg_write(gspca_dev, 0x13,
958 sccb_reg_read(gspca_dev, 0x13) | 0x04);
959 sccb_reg_write(gspca_dev, 0x64,
960 sccb_reg_read(gspca_dev, 0x64) | 0x03);
962 sccb_reg_write(gspca_dev, 0x13,
963 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
964 sccb_reg_write(gspca_dev, 0x64,
965 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
969 static void setawb(struct gspca_dev *gspca_dev, s32 val)
971 struct sd *sd = (struct sd *) gspca_dev;
974 sccb_reg_write(gspca_dev, 0x13,
975 sccb_reg_read(gspca_dev, 0x13) | 0x02);
976 if (sd->sensor == SENSOR_OV772x)
977 sccb_reg_write(gspca_dev, 0x63,
978 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
980 sccb_reg_write(gspca_dev, 0x13,
981 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
982 if (sd->sensor == SENSOR_OV772x)
983 sccb_reg_write(gspca_dev, 0x63,
984 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
988 static void setaec(struct gspca_dev *gspca_dev, s32 val)
990 struct sd *sd = (struct sd *) gspca_dev;
993 data = sd->sensor == SENSOR_OV767x ?
994 0x05 : /* agc + aec */
997 case V4L2_EXPOSURE_AUTO:
998 sccb_reg_write(gspca_dev, 0x13,
999 sccb_reg_read(gspca_dev, 0x13) | data);
1001 case V4L2_EXPOSURE_MANUAL:
1002 sccb_reg_write(gspca_dev, 0x13,
1003 sccb_reg_read(gspca_dev, 0x13) & ~data);
1008 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1010 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1011 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1014 static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
1016 struct sd *sd = (struct sd *) gspca_dev;
1019 if (sd->sensor == SENSOR_OV767x) {
1020 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1026 sccb_reg_write(gspca_dev, 0x1e, val);
1028 val = sccb_reg_read(gspca_dev, 0x0c);
1034 sccb_reg_write(gspca_dev, 0x0c, val);
1038 static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
1040 struct sd *sd = (struct sd *) gspca_dev;
1042 val = val ? 0x9e : 0x00;
1043 if (sd->sensor == SENSOR_OV767x) {
1044 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1046 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1048 sccb_reg_write(gspca_dev, 0x2b, val);
1052 /* this function is called at probe time */
1053 static int sd_config(struct gspca_dev *gspca_dev,
1054 const struct usb_device_id *id)
1056 struct sd *sd = (struct sd *) gspca_dev;
1059 cam = &gspca_dev->cam;
1061 cam->cam_mode = ov772x_mode;
1062 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1064 sd->frame_rate = 30;
1069 static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1071 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1072 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1075 case V4L2_CID_AUTOGAIN:
1076 gspca_dev->usb_err = 0;
1077 if (ctrl->val && sd->gain && gspca_dev->streaming)
1078 sd->gain->val = getgain(gspca_dev);
1079 return gspca_dev->usb_err;
1081 case V4L2_CID_EXPOSURE_AUTO:
1082 gspca_dev->usb_err = 0;
1083 if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1084 gspca_dev->streaming)
1085 sd->exposure->val = getexposure(gspca_dev);
1086 return gspca_dev->usb_err;
1091 static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1093 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1094 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1096 gspca_dev->usb_err = 0;
1097 if (!gspca_dev->streaming)
1102 sethue(gspca_dev, ctrl->val);
1104 case V4L2_CID_SATURATION:
1105 setsaturation(gspca_dev, ctrl->val);
1107 case V4L2_CID_BRIGHTNESS:
1108 setbrightness(gspca_dev, ctrl->val);
1110 case V4L2_CID_CONTRAST:
1111 setcontrast(gspca_dev, ctrl->val);
1113 case V4L2_CID_AUTOGAIN:
1114 /* case V4L2_CID_GAIN: */
1115 setagc(gspca_dev, ctrl->val);
1116 if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1117 setgain(gspca_dev, sd->gain->val);
1119 case V4L2_CID_AUTO_WHITE_BALANCE:
1120 setawb(gspca_dev, ctrl->val);
1122 case V4L2_CID_EXPOSURE_AUTO:
1123 /* case V4L2_CID_EXPOSURE: */
1124 setaec(gspca_dev, ctrl->val);
1125 if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1127 setexposure(gspca_dev, sd->exposure->val);
1129 case V4L2_CID_SHARPNESS:
1130 setsharpness(gspca_dev, ctrl->val);
1132 case V4L2_CID_HFLIP:
1133 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1135 case V4L2_CID_VFLIP:
1136 sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1138 case V4L2_CID_POWER_LINE_FREQUENCY:
1139 setlightfreq(gspca_dev, ctrl->val);
1142 return gspca_dev->usb_err;
1145 static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1146 .g_volatile_ctrl = ov534_g_volatile_ctrl,
1147 .s_ctrl = ov534_s_ctrl,
1150 static int sd_init_controls(struct gspca_dev *gspca_dev)
1152 struct sd *sd = (struct sd *) gspca_dev;
1153 struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1154 /* parameters with different values between the supported sensors */
1168 if (sd->sensor == SENSOR_OV767x) {
1172 brightness_min = -127;
1173 brightness_max = 127;
1175 contrast_max = 0x80;
1176 contrast_def = 0x40;
1177 exposure_min = 0x08;
1178 exposure_max = 0x60;
1179 exposure_def = 0x13;
1183 saturation_max = 255,
1184 saturation_def = 64,
1186 brightness_max = 255;
1196 gspca_dev->vdev.ctrl_handler = hdl;
1198 v4l2_ctrl_handler_init(hdl, 13);
1200 if (sd->sensor == SENSOR_OV772x)
1201 sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1202 V4L2_CID_HUE, -90, 90, 1, 0);
1204 sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1205 V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1207 sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1208 V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1210 sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1211 V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1213 if (sd->sensor == SENSOR_OV772x) {
1214 sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1215 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1216 sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1217 V4L2_CID_GAIN, 0, 63, 1, 20);
1220 sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1221 V4L2_CID_EXPOSURE_AUTO,
1222 V4L2_EXPOSURE_MANUAL, 0,
1223 V4L2_EXPOSURE_AUTO);
1224 sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1225 V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1228 sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1229 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1231 if (sd->sensor == SENSOR_OV772x)
1232 sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1233 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1235 sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1236 V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1237 sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1238 V4L2_CID_VFLIP, 0, 1, 1, 0);
1239 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1240 V4L2_CID_POWER_LINE_FREQUENCY,
1241 V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1242 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1245 pr_err("Could not initialize controls\n");
1249 if (sd->sensor == SENSOR_OV772x)
1250 v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1252 v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1258 /* this function is called at probe and resume time */
1259 static int sd_init(struct gspca_dev *gspca_dev)
1261 struct sd *sd = (struct sd *) gspca_dev;
1263 static const struct reg_array bridge_init[NSENSORS] = {
1264 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1265 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1267 static const struct reg_array sensor_init[NSENSORS] = {
1268 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1269 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1273 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1274 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1277 /* initialize the sensor address */
1278 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1281 sccb_reg_write(gspca_dev, 0x12, 0x80);
1284 /* probe the sensor */
1285 sccb_reg_read(gspca_dev, 0x0a);
1286 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1287 sccb_reg_read(gspca_dev, 0x0b);
1288 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1289 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
1291 if ((sensor_id & 0xfff0) == 0x7670) {
1292 sd->sensor = SENSOR_OV767x;
1293 gspca_dev->cam.cam_mode = ov767x_mode;
1294 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1296 sd->sensor = SENSOR_OV772x;
1297 gspca_dev->cam.bulk = 1;
1298 gspca_dev->cam.bulk_size = 16384;
1299 gspca_dev->cam.bulk_nurbs = 2;
1300 gspca_dev->cam.mode_framerates = ov772x_framerates;
1304 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1305 bridge_init[sd->sensor].len);
1306 ov534_set_led(gspca_dev, 1);
1307 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1308 sensor_init[sd->sensor].len);
1310 sd_stopN(gspca_dev);
1311 /* set_frame_rate(gspca_dev); */
1313 return gspca_dev->usb_err;
1316 static int sd_start(struct gspca_dev *gspca_dev)
1318 struct sd *sd = (struct sd *) gspca_dev;
1320 static const struct reg_array bridge_start[NSENSORS][2] = {
1321 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1322 ARRAY_SIZE(bridge_start_qvga_767x)},
1323 {bridge_start_vga_767x,
1324 ARRAY_SIZE(bridge_start_vga_767x)}},
1325 [SENSOR_OV772x] = {{bridge_start_qvga_772x,
1326 ARRAY_SIZE(bridge_start_qvga_772x)},
1327 {bridge_start_vga_772x,
1328 ARRAY_SIZE(bridge_start_vga_772x)}},
1330 static const struct reg_array sensor_start[NSENSORS][2] = {
1331 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1332 ARRAY_SIZE(sensor_start_qvga_767x)},
1333 {sensor_start_vga_767x,
1334 ARRAY_SIZE(sensor_start_vga_767x)}},
1335 [SENSOR_OV772x] = {{sensor_start_qvga_772x,
1336 ARRAY_SIZE(sensor_start_qvga_772x)},
1337 {sensor_start_vga_772x,
1338 ARRAY_SIZE(sensor_start_vga_772x)}},
1341 /* (from ms-win trace) */
1342 if (sd->sensor == SENSOR_OV767x)
1343 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1344 /* black sun enable ? */
1346 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1347 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1348 bridge_start[sd->sensor][mode].len);
1349 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1350 sensor_start[sd->sensor][mode].len);
1352 set_frame_rate(gspca_dev);
1355 sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1356 setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1358 setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1359 setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1360 setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1362 setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1363 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1364 setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1365 setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1367 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1368 sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1369 v4l2_ctrl_g_ctrl(sd->vflip));
1370 setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
1372 ov534_set_led(gspca_dev, 1);
1373 ov534_reg_write(gspca_dev, 0xe0, 0x00);
1374 return gspca_dev->usb_err;
1377 static void sd_stopN(struct gspca_dev *gspca_dev)
1379 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1380 ov534_set_led(gspca_dev, 0);
1383 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1384 #define UVC_STREAM_EOH (1 << 7)
1385 #define UVC_STREAM_ERR (1 << 6)
1386 #define UVC_STREAM_STI (1 << 5)
1387 #define UVC_STREAM_RES (1 << 4)
1388 #define UVC_STREAM_SCR (1 << 3)
1389 #define UVC_STREAM_PTS (1 << 2)
1390 #define UVC_STREAM_EOF (1 << 1)
1391 #define UVC_STREAM_FID (1 << 0)
1393 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1396 struct sd *sd = (struct sd *) gspca_dev;
1399 int remaining_len = len;
1402 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1404 len = min(remaining_len, payload_len);
1406 /* Payloads are prefixed with a UVC-style header. We
1407 consider a frame to start when the FID toggles, or the PTS
1408 changes. A frame ends when EOF is set, and we've received
1409 the correct number of bytes. */
1411 /* Verify UVC header. Header length is always 12 */
1412 if (data[0] != 12 || len < 12) {
1413 PDEBUG(D_PACK, "bad header");
1418 if (data[1] & UVC_STREAM_ERR) {
1419 PDEBUG(D_PACK, "payload error");
1423 /* Extract PTS and FID */
1424 if (!(data[1] & UVC_STREAM_PTS)) {
1425 PDEBUG(D_PACK, "PTS not present");
1428 this_pts = (data[5] << 24) | (data[4] << 16)
1429 | (data[3] << 8) | data[2];
1430 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1432 /* If PTS or FID has changed, start a new frame. */
1433 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1434 if (gspca_dev->last_packet_type == INTER_PACKET)
1435 gspca_frame_add(gspca_dev, LAST_PACKET,
1437 sd->last_pts = this_pts;
1438 sd->last_fid = this_fid;
1439 gspca_frame_add(gspca_dev, FIRST_PACKET,
1440 data + 12, len - 12);
1441 /* If this packet is marked as EOF, end the frame */
1442 } else if (data[1] & UVC_STREAM_EOF) {
1444 if (gspca_dev->pixfmt.pixelformat == V4L2_PIX_FMT_YUYV
1445 && gspca_dev->image_len + len - 12 !=
1446 gspca_dev->pixfmt.width *
1447 gspca_dev->pixfmt.height * 2) {
1448 PDEBUG(D_PACK, "wrong sized frame");
1451 gspca_frame_add(gspca_dev, LAST_PACKET,
1452 data + 12, len - 12);
1455 /* Add the data from this payload */
1456 gspca_frame_add(gspca_dev, INTER_PACKET,
1457 data + 12, len - 12);
1460 /* Done this payload */
1464 /* Discard data until a new frame starts. */
1465 gspca_dev->last_packet_type = DISCARD_PACKET;
1468 remaining_len -= len;
1470 } while (remaining_len > 0);
1473 /* get stream parameters (framerate) */
1474 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1475 struct v4l2_streamparm *parm)
1477 struct v4l2_captureparm *cp = &parm->parm.capture;
1478 struct v4l2_fract *tpf = &cp->timeperframe;
1479 struct sd *sd = (struct sd *) gspca_dev;
1481 cp->capability |= V4L2_CAP_TIMEPERFRAME;
1483 tpf->denominator = sd->frame_rate;
1486 /* set stream parameters (framerate) */
1487 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1488 struct v4l2_streamparm *parm)
1490 struct v4l2_captureparm *cp = &parm->parm.capture;
1491 struct v4l2_fract *tpf = &cp->timeperframe;
1492 struct sd *sd = (struct sd *) gspca_dev;
1494 if (tpf->numerator == 0 || tpf->denominator == 0)
1495 /* Set default framerate */
1496 sd->frame_rate = 30;
1498 /* Set requested framerate */
1499 sd->frame_rate = tpf->denominator / tpf->numerator;
1501 if (gspca_dev->streaming)
1502 set_frame_rate(gspca_dev);
1504 /* Return the actual framerate */
1506 tpf->denominator = sd->frame_rate;
1509 /* sub-driver description */
1510 static const struct sd_desc sd_desc = {
1511 .name = MODULE_NAME,
1512 .config = sd_config,
1514 .init_controls = sd_init_controls,
1517 .pkt_scan = sd_pkt_scan,
1518 .get_streamparm = sd_get_streamparm,
1519 .set_streamparm = sd_set_streamparm,
1522 /* -- module initialisation -- */
1523 static const struct usb_device_id device_table[] = {
1524 {USB_DEVICE(0x1415, 0x2000)},
1525 {USB_DEVICE(0x06f8, 0x3002)},
1529 MODULE_DEVICE_TABLE(usb, device_table);
1531 /* -- device connect -- */
1532 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1534 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1538 static struct usb_driver sd_driver = {
1539 .name = MODULE_NAME,
1540 .id_table = device_table,
1542 .disconnect = gspca_disconnect,
1544 .suspend = gspca_suspend,
1545 .resume = gspca_resume,
1546 .reset_resume = gspca_resume,
1550 module_usb_driver(sd_driver);