1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ov534-ov7xxx gspca driver
7 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
11 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
13 * PS3 Eye camera enhanced by Richard Kaswy http://kaswy.free.fr
14 * PS3 Eye camera - brightness, contrast, awb, agc, aec controls
16 * PS3 Eye camera - FPS range extended by Joseph Howse
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 #define MODULE_NAME "ov534"
26 #include <linux/fixp-arith.h>
27 #include <media/v4l2-ctrls.h>
29 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
30 #define OV534_REG_SUBADDR 0xf2
31 #define OV534_REG_WRITE 0xf3
32 #define OV534_REG_READ 0xf4
33 #define OV534_REG_OPERATION 0xf5
34 #define OV534_REG_STATUS 0xf6
36 #define OV534_OP_WRITE_3 0x37
37 #define OV534_OP_WRITE_2 0x33
38 #define OV534_OP_READ_2 0xf9
40 #define CTRL_TIMEOUT 500
41 #define DEFAULT_FRAME_RATE 30
44 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
45 MODULE_LICENSE("GPL");
47 /* specific webcam descriptor */
49 struct gspca_dev gspca_dev; /* !! must be the first item */
51 struct v4l2_ctrl_handler ctrl_handler;
52 struct v4l2_ctrl *hue;
53 struct v4l2_ctrl *saturation;
54 struct v4l2_ctrl *brightness;
55 struct v4l2_ctrl *contrast;
56 struct { /* gain control cluster */
57 struct v4l2_ctrl *autogain;
58 struct v4l2_ctrl *gain;
60 struct v4l2_ctrl *autowhitebalance;
61 struct { /* exposure control cluster */
62 struct v4l2_ctrl *autoexposure;
63 struct v4l2_ctrl *exposure;
65 struct v4l2_ctrl *sharpness;
66 struct v4l2_ctrl *hflip;
67 struct v4l2_ctrl *vflip;
68 struct v4l2_ctrl *plfreq;
82 static int sd_start(struct gspca_dev *gspca_dev);
83 static void sd_stopN(struct gspca_dev *gspca_dev);
86 static const struct v4l2_pix_format ov772x_mode[] = {
87 {320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
88 .bytesperline = 320 * 2,
89 .sizeimage = 320 * 240 * 2,
90 .colorspace = V4L2_COLORSPACE_SRGB,
92 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
93 .bytesperline = 640 * 2,
94 .sizeimage = 640 * 480 * 2,
95 .colorspace = V4L2_COLORSPACE_SRGB,
97 {320, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
99 .sizeimage = 320 * 240,
100 .colorspace = V4L2_COLORSPACE_SRGB,
102 {640, 480, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
104 .sizeimage = 640 * 480,
105 .colorspace = V4L2_COLORSPACE_SRGB,
108 static const struct v4l2_pix_format ov767x_mode[] = {
109 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
111 .sizeimage = 320 * 240 * 3 / 8 + 590,
112 .colorspace = V4L2_COLORSPACE_JPEG},
113 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
115 .sizeimage = 640 * 480 * 3 / 8 + 590,
116 .colorspace = V4L2_COLORSPACE_JPEG},
119 static const u8 qvga_rates[] = {187, 150, 137, 125, 100, 75, 60, 50, 37, 30};
120 static const u8 vga_rates[] = {60, 50, 40, 30, 15};
122 static const struct framerates ov772x_framerates[] = {
125 .nrates = ARRAY_SIZE(qvga_rates),
129 .nrates = ARRAY_SIZE(vga_rates),
131 { /* 320x240 SGBRG8 */
133 .nrates = ARRAY_SIZE(qvga_rates),
135 { /* 640x480 SGBRG8 */
137 .nrates = ARRAY_SIZE(vga_rates),
146 static const u8 bridge_init_767x[][2] = {
147 /* comments from the ms-win file apollo7670.set */
177 {0xc0, 0x50}, /* HSize 640 */
178 {0xc1, 0x3c}, /* VSize 480 */
179 {0x34, 0x05}, /* enable Audio Suspend mode */
180 {0xc2, 0x0c}, /* Input YUV */
181 {0xc3, 0xf9}, /* enable PRE */
182 {0x34, 0x05}, /* enable Audio Suspend mode */
183 {0xe7, 0x2e}, /* this solves failure of "SuspendResumeTest" */
184 {0x31, 0xf9}, /* enable 1.8V Suspend */
185 {0x35, 0x02}, /* turn on JPEG */
187 {0x25, 0x42}, /* GPIO[8]:Input */
188 {0x94, 0x11}, /* If the default setting is loaded when
189 * system boots up, this flag is closed here */
191 static const u8 sensor_init_767x[][2] = {
209 {0x7a, 0x2a}, /* set Gamma=1.6 below */
229 {0x14, 0x38}, /* gain max 16x */
309 {0x41, 0x38}, /* jfm: auto sharpness + auto de-noise */
313 {0xa4, 0x8a}, /* Night mode trigger point */
346 static const u8 bridge_start_vga_767x[][2] = {
354 {0x35, 0x02}, /* turn on JPEG */
356 {0xda, 0x00}, /* for higher clock rate(30fps) */
357 {0x34, 0x05}, /* enable Audio Suspend mode */
358 {0xc3, 0xf9}, /* enable PRE */
359 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
360 {0x8d, 0x1c}, /* output YUV */
361 /* {0x34, 0x05}, * enable Audio Suspend mode (?) */
362 {0x50, 0x00}, /* H/V divider=0 */
363 {0x51, 0xa0}, /* input H=640/4 */
364 {0x52, 0x3c}, /* input V=480/4 */
365 {0x53, 0x00}, /* offset X=0 */
366 {0x54, 0x00}, /* offset Y=0 */
367 {0x55, 0x00}, /* H/V size[8]=0 */
368 {0x57, 0x00}, /* H-size[9]=0 */
369 {0x5c, 0x00}, /* output size[9:8]=0 */
370 {0x5a, 0xa0}, /* output H=640/4 */
371 {0x5b, 0x78}, /* output V=480/4 */
376 static const u8 sensor_start_vga_767x[][2] = {
382 static const u8 bridge_start_qvga_767x[][2] = {
390 {0x35, 0x02}, /* turn on JPEG */
392 {0xc0, 0x50}, /* CIF HSize 640 */
393 {0xc1, 0x3c}, /* CIF VSize 480 */
394 {0x8c, 0x00}, /* CIF VSize LSB[2:0] */
395 {0x8d, 0x1c}, /* output YUV */
396 {0x34, 0x05}, /* enable Audio Suspend mode */
397 {0xc2, 0x4c}, /* output YUV and Enable DCW */
398 {0xc3, 0xf9}, /* enable PRE */
399 {0x1c, 0x00}, /* indirect addressing */
400 {0x1d, 0x48}, /* output YUV422 */
401 {0x50, 0x89}, /* H/V divider=/2; plus DCW AVG */
402 {0x51, 0xa0}, /* DCW input H=640/4 */
403 {0x52, 0x78}, /* DCW input V=480/4 */
404 {0x53, 0x00}, /* offset X=0 */
405 {0x54, 0x00}, /* offset Y=0 */
406 {0x55, 0x00}, /* H/V size[8]=0 */
407 {0x57, 0x00}, /* H-size[9]=0 */
408 {0x5c, 0x00}, /* DCW output size[9:8]=0 */
409 {0x5a, 0x50}, /* DCW output H=320/4 */
410 {0x5b, 0x3c}, /* DCW output V=240/4 */
415 static const u8 sensor_start_qvga_767x[][2] = {
422 static const u8 bridge_init_772x[][2] = {
457 { 0x1d, 0x08 }, /* turn on UVC header */
458 { 0x1d, 0x0e }, /* .. */
460 static const u8 sensor_init_772x[][2] = {
463 /*fixme: better have a delay?*/
486 { 0x63, 0xaa }, /* AWB - was e0 */
489 { 0x13, 0xf0 }, /* com8 */
502 { 0x13, 0xff }, /* AWB */
547 { 0x8e, 0x00 }, /* De-noise threshold */
549 static const u8 bridge_start_vga_yuyv_772x[][2] = {
565 static const u8 sensor_start_vga_yuyv_772x[][2] = {
576 static const u8 bridge_start_qvga_yuyv_772x[][2] = {
592 static const u8 sensor_start_qvga_yuyv_772x[][2] = {
603 static const u8 bridge_start_vga_gbrg_772x[][2] = {
619 static const u8 sensor_start_vga_gbrg_772x[][2] = {
630 static const u8 bridge_start_qvga_gbrg_772x[][2] = {
646 static const u8 sensor_start_qvga_gbrg_772x[][2] = {
658 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
660 struct usb_device *udev = gspca_dev->dev;
663 if (gspca_dev->usb_err < 0)
666 gspca_dbg(gspca_dev, D_USBO, "SET 01 0000 %04x %02x\n", reg, val);
667 gspca_dev->usb_buf[0] = val;
668 ret = usb_control_msg(udev,
669 usb_sndctrlpipe(udev, 0),
671 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
672 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
674 pr_err("write failed %d\n", ret);
675 gspca_dev->usb_err = ret;
679 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
681 struct usb_device *udev = gspca_dev->dev;
684 if (gspca_dev->usb_err < 0)
686 ret = usb_control_msg(udev,
687 usb_rcvctrlpipe(udev, 0),
689 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
690 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
691 gspca_dbg(gspca_dev, D_USBI, "GET 01 0000 %04x %02x\n",
692 reg, gspca_dev->usb_buf[0]);
694 pr_err("read failed %d\n", ret);
695 gspca_dev->usb_err = ret;
697 * Make sure the result is zeroed to avoid uninitialized
700 gspca_dev->usb_buf[0] = 0;
702 return gspca_dev->usb_buf[0];
705 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
706 * (direction and output)? */
707 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
711 gspca_dbg(gspca_dev, D_CONF, "led status: %d\n", status);
713 data = ov534_reg_read(gspca_dev, 0x21);
715 ov534_reg_write(gspca_dev, 0x21, data);
717 data = ov534_reg_read(gspca_dev, 0x23);
723 ov534_reg_write(gspca_dev, 0x23, data);
726 data = ov534_reg_read(gspca_dev, 0x21);
728 ov534_reg_write(gspca_dev, 0x21, data);
732 static int sccb_check_status(struct gspca_dev *gspca_dev)
737 for (i = 0; i < 5; i++) {
738 usleep_range(10000, 20000);
739 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
749 gspca_err(gspca_dev, "sccb status 0x%02x, attempt %d/5\n",
756 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
758 gspca_dbg(gspca_dev, D_USBO, "sccb write: %02x %02x\n", reg, val);
759 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
760 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
761 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
763 if (!sccb_check_status(gspca_dev)) {
764 pr_err("sccb_reg_write failed\n");
765 gspca_dev->usb_err = -EIO;
769 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
771 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
772 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
773 if (!sccb_check_status(gspca_dev))
774 pr_err("sccb_reg_read failed 1\n");
776 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
777 if (!sccb_check_status(gspca_dev))
778 pr_err("sccb_reg_read failed 2\n");
780 return ov534_reg_read(gspca_dev, OV534_REG_READ);
783 /* output a bridge sequence (reg - val) */
784 static void reg_w_array(struct gspca_dev *gspca_dev,
785 const u8 (*data)[2], int len)
788 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
793 /* output a sensor sequence (reg - val) */
794 static void sccb_w_array(struct gspca_dev *gspca_dev,
795 const u8 (*data)[2], int len)
798 if ((*data)[0] != 0xff) {
799 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
801 sccb_reg_read(gspca_dev, (*data)[1]);
802 sccb_reg_write(gspca_dev, 0xff, 0x00);
808 /* ov772x specific controls */
809 static void set_frame_rate(struct gspca_dev *gspca_dev)
811 struct sd *sd = (struct sd *) gspca_dev;
819 const struct rate_s *r;
820 static const struct rate_s rate_0[] = { /* 640x480 */
821 {60, 0x01, 0xc1, 0x04},
822 {50, 0x01, 0x41, 0x02},
823 {40, 0x02, 0xc1, 0x04},
824 {30, 0x04, 0x81, 0x02},
825 {15, 0x03, 0x41, 0x04},
827 static const struct rate_s rate_1[] = { /* 320x240 */
828 /* {205, 0x01, 0xc1, 0x02}, * 205 FPS: video is partly corrupt */
829 {187, 0x01, 0x81, 0x02}, /* 187 FPS or below: video is valid */
830 {150, 0x01, 0xc1, 0x04},
831 {137, 0x02, 0xc1, 0x02},
832 {125, 0x02, 0x81, 0x02},
833 {100, 0x02, 0xc1, 0x04},
834 {75, 0x03, 0xc1, 0x04},
835 {60, 0x04, 0xc1, 0x04},
836 {50, 0x02, 0x41, 0x04},
837 {37, 0x03, 0x41, 0x04},
838 {30, 0x04, 0x41, 0x04},
841 if (sd->sensor != SENSOR_OV772x)
843 if (gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv == 0) {
845 i = ARRAY_SIZE(rate_0);
848 i = ARRAY_SIZE(rate_1);
851 if (sd->frame_rate >= r->fps)
856 sccb_reg_write(gspca_dev, 0x11, r->r11);
857 sccb_reg_write(gspca_dev, 0x0d, r->r0d);
858 ov534_reg_write(gspca_dev, 0xe5, r->re5);
860 gspca_dbg(gspca_dev, D_PROBE, "frame_rate: %d\n", r->fps);
863 static void sethue(struct gspca_dev *gspca_dev, s32 val)
865 struct sd *sd = (struct sd *) gspca_dev;
867 if (sd->sensor == SENSOR_OV767x) {
873 /* According to the datasheet the registers expect HUESIN and
874 * HUECOS to be the result of the trigonometric functions,
877 * The 0x7fff here represents the maximum absolute value
878 * returned byt fixp_sin and fixp_cos, so the scaling will
879 * consider the result like in the interval [-1.0, 1.0].
881 huesin = fixp_sin16(val) * 0x80 / 0x7fff;
882 huecos = fixp_cos16(val) * 0x80 / 0x7fff;
885 sccb_reg_write(gspca_dev, 0xab,
886 sccb_reg_read(gspca_dev, 0xab) | 0x2);
889 sccb_reg_write(gspca_dev, 0xab,
890 sccb_reg_read(gspca_dev, 0xab) & ~0x2);
893 sccb_reg_write(gspca_dev, 0xa9, (u8)huecos);
894 sccb_reg_write(gspca_dev, 0xaa, (u8)huesin);
898 static void setsaturation(struct gspca_dev *gspca_dev, s32 val)
900 struct sd *sd = (struct sd *) gspca_dev;
902 if (sd->sensor == SENSOR_OV767x) {
904 static u8 color_tb[][6] = {
905 {0x42, 0x42, 0x00, 0x11, 0x30, 0x41},
906 {0x52, 0x52, 0x00, 0x16, 0x3c, 0x52},
907 {0x66, 0x66, 0x00, 0x1b, 0x4b, 0x66},
908 {0x80, 0x80, 0x00, 0x22, 0x5e, 0x80},
909 {0x9a, 0x9a, 0x00, 0x29, 0x71, 0x9a},
910 {0xb8, 0xb8, 0x00, 0x31, 0x87, 0xb8},
911 {0xdd, 0xdd, 0x00, 0x3b, 0xa2, 0xdd},
914 for (i = 0; i < ARRAY_SIZE(color_tb[0]); i++)
915 sccb_reg_write(gspca_dev, 0x4f + i, color_tb[val][i]);
917 sccb_reg_write(gspca_dev, 0xa7, val); /* U saturation */
918 sccb_reg_write(gspca_dev, 0xa8, val); /* V saturation */
922 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
924 struct sd *sd = (struct sd *) gspca_dev;
926 if (sd->sensor == SENSOR_OV767x) {
929 sccb_reg_write(gspca_dev, 0x55, val); /* bright */
931 sccb_reg_write(gspca_dev, 0x9b, val);
935 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
937 struct sd *sd = (struct sd *) gspca_dev;
939 if (sd->sensor == SENSOR_OV767x)
940 sccb_reg_write(gspca_dev, 0x56, val); /* contras */
942 sccb_reg_write(gspca_dev, 0x9c, val);
945 static void setgain(struct gspca_dev *gspca_dev, s32 val)
947 switch (val & 0x30) {
965 sccb_reg_write(gspca_dev, 0x00, val);
968 static s32 getgain(struct gspca_dev *gspca_dev)
970 return sccb_reg_read(gspca_dev, 0x00);
973 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
975 struct sd *sd = (struct sd *) gspca_dev;
977 if (sd->sensor == SENSOR_OV767x) {
979 /* set only aec[9:2] */
980 sccb_reg_write(gspca_dev, 0x10, val); /* aech */
983 /* 'val' is one byte and represents half of the exposure value
984 * we are going to set into registers, a two bytes value:
986 * MSB: ((u16) val << 1) >> 8 == val >> 7
987 * LSB: ((u16) val << 1) & 0xff == val << 1
989 sccb_reg_write(gspca_dev, 0x08, val >> 7);
990 sccb_reg_write(gspca_dev, 0x10, val << 1);
994 static s32 getexposure(struct gspca_dev *gspca_dev)
996 struct sd *sd = (struct sd *) gspca_dev;
998 if (sd->sensor == SENSOR_OV767x) {
999 /* get only aec[9:2] */
1000 return sccb_reg_read(gspca_dev, 0x10); /* aech */
1002 u8 hi = sccb_reg_read(gspca_dev, 0x08);
1003 u8 lo = sccb_reg_read(gspca_dev, 0x10);
1004 return (hi << 8 | lo) >> 1;
1008 static void setagc(struct gspca_dev *gspca_dev, s32 val)
1011 sccb_reg_write(gspca_dev, 0x13,
1012 sccb_reg_read(gspca_dev, 0x13) | 0x04);
1013 sccb_reg_write(gspca_dev, 0x64,
1014 sccb_reg_read(gspca_dev, 0x64) | 0x03);
1016 sccb_reg_write(gspca_dev, 0x13,
1017 sccb_reg_read(gspca_dev, 0x13) & ~0x04);
1018 sccb_reg_write(gspca_dev, 0x64,
1019 sccb_reg_read(gspca_dev, 0x64) & ~0x03);
1023 static void setawb(struct gspca_dev *gspca_dev, s32 val)
1025 struct sd *sd = (struct sd *) gspca_dev;
1028 sccb_reg_write(gspca_dev, 0x13,
1029 sccb_reg_read(gspca_dev, 0x13) | 0x02);
1030 if (sd->sensor == SENSOR_OV772x)
1031 sccb_reg_write(gspca_dev, 0x63,
1032 sccb_reg_read(gspca_dev, 0x63) | 0xc0);
1034 sccb_reg_write(gspca_dev, 0x13,
1035 sccb_reg_read(gspca_dev, 0x13) & ~0x02);
1036 if (sd->sensor == SENSOR_OV772x)
1037 sccb_reg_write(gspca_dev, 0x63,
1038 sccb_reg_read(gspca_dev, 0x63) & ~0xc0);
1042 static void setaec(struct gspca_dev *gspca_dev, s32 val)
1044 struct sd *sd = (struct sd *) gspca_dev;
1047 data = sd->sensor == SENSOR_OV767x ?
1048 0x05 : /* agc + aec */
1051 case V4L2_EXPOSURE_AUTO:
1052 sccb_reg_write(gspca_dev, 0x13,
1053 sccb_reg_read(gspca_dev, 0x13) | data);
1055 case V4L2_EXPOSURE_MANUAL:
1056 sccb_reg_write(gspca_dev, 0x13,
1057 sccb_reg_read(gspca_dev, 0x13) & ~data);
1062 static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
1064 sccb_reg_write(gspca_dev, 0x91, val); /* Auto de-noise threshold */
1065 sccb_reg_write(gspca_dev, 0x8e, val); /* De-noise threshold */
1068 static void sethvflip(struct gspca_dev *gspca_dev, s32 hflip, s32 vflip)
1070 struct sd *sd = (struct sd *) gspca_dev;
1073 if (sd->sensor == SENSOR_OV767x) {
1074 val = sccb_reg_read(gspca_dev, 0x1e); /* mvfp */
1080 sccb_reg_write(gspca_dev, 0x1e, val);
1082 val = sccb_reg_read(gspca_dev, 0x0c);
1088 sccb_reg_write(gspca_dev, 0x0c, val);
1092 static void setlightfreq(struct gspca_dev *gspca_dev, s32 val)
1094 struct sd *sd = (struct sd *) gspca_dev;
1096 val = val ? 0x9e : 0x00;
1097 if (sd->sensor == SENSOR_OV767x) {
1098 sccb_reg_write(gspca_dev, 0x2a, 0x00);
1100 val = 0x9d; /* insert dummy to 25fps for 50Hz */
1102 sccb_reg_write(gspca_dev, 0x2b, val);
1106 /* this function is called at probe time */
1107 static int sd_config(struct gspca_dev *gspca_dev,
1108 const struct usb_device_id *id)
1110 struct sd *sd = (struct sd *) gspca_dev;
1113 cam = &gspca_dev->cam;
1115 cam->cam_mode = ov772x_mode;
1116 cam->nmodes = ARRAY_SIZE(ov772x_mode);
1118 sd->frame_rate = DEFAULT_FRAME_RATE;
1123 static int ov534_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1125 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1126 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1129 case V4L2_CID_AUTOGAIN:
1130 gspca_dev->usb_err = 0;
1131 if (ctrl->val && sd->gain && gspca_dev->streaming)
1132 sd->gain->val = getgain(gspca_dev);
1133 return gspca_dev->usb_err;
1135 case V4L2_CID_EXPOSURE_AUTO:
1136 gspca_dev->usb_err = 0;
1137 if (ctrl->val == V4L2_EXPOSURE_AUTO && sd->exposure &&
1138 gspca_dev->streaming)
1139 sd->exposure->val = getexposure(gspca_dev);
1140 return gspca_dev->usb_err;
1145 static int ov534_s_ctrl(struct v4l2_ctrl *ctrl)
1147 struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
1148 struct gspca_dev *gspca_dev = &sd->gspca_dev;
1150 gspca_dev->usb_err = 0;
1151 if (!gspca_dev->streaming)
1156 sethue(gspca_dev, ctrl->val);
1158 case V4L2_CID_SATURATION:
1159 setsaturation(gspca_dev, ctrl->val);
1161 case V4L2_CID_BRIGHTNESS:
1162 setbrightness(gspca_dev, ctrl->val);
1164 case V4L2_CID_CONTRAST:
1165 setcontrast(gspca_dev, ctrl->val);
1167 case V4L2_CID_AUTOGAIN:
1168 /* case V4L2_CID_GAIN: */
1169 setagc(gspca_dev, ctrl->val);
1170 if (!gspca_dev->usb_err && !ctrl->val && sd->gain)
1171 setgain(gspca_dev, sd->gain->val);
1173 case V4L2_CID_AUTO_WHITE_BALANCE:
1174 setawb(gspca_dev, ctrl->val);
1176 case V4L2_CID_EXPOSURE_AUTO:
1177 /* case V4L2_CID_EXPOSURE: */
1178 setaec(gspca_dev, ctrl->val);
1179 if (!gspca_dev->usb_err && ctrl->val == V4L2_EXPOSURE_MANUAL &&
1181 setexposure(gspca_dev, sd->exposure->val);
1183 case V4L2_CID_SHARPNESS:
1184 setsharpness(gspca_dev, ctrl->val);
1186 case V4L2_CID_HFLIP:
1187 sethvflip(gspca_dev, ctrl->val, sd->vflip->val);
1189 case V4L2_CID_VFLIP:
1190 sethvflip(gspca_dev, sd->hflip->val, ctrl->val);
1192 case V4L2_CID_POWER_LINE_FREQUENCY:
1193 setlightfreq(gspca_dev, ctrl->val);
1196 return gspca_dev->usb_err;
1199 static const struct v4l2_ctrl_ops ov534_ctrl_ops = {
1200 .g_volatile_ctrl = ov534_g_volatile_ctrl,
1201 .s_ctrl = ov534_s_ctrl,
1204 static int sd_init_controls(struct gspca_dev *gspca_dev)
1206 struct sd *sd = (struct sd *) gspca_dev;
1207 struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;
1208 /* parameters with different values between the supported sensors */
1222 if (sd->sensor == SENSOR_OV767x) {
1226 brightness_min = -127;
1227 brightness_max = 127;
1229 contrast_max = 0x80;
1230 contrast_def = 0x40;
1231 exposure_min = 0x08;
1232 exposure_max = 0x60;
1233 exposure_def = 0x13;
1237 saturation_max = 255;
1238 saturation_def = 64;
1240 brightness_max = 255;
1250 gspca_dev->vdev.ctrl_handler = hdl;
1252 v4l2_ctrl_handler_init(hdl, 13);
1254 if (sd->sensor == SENSOR_OV772x)
1255 sd->hue = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1256 V4L2_CID_HUE, -90, 90, 1, 0);
1258 sd->saturation = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1259 V4L2_CID_SATURATION, saturation_min, saturation_max, 1,
1261 sd->brightness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1262 V4L2_CID_BRIGHTNESS, brightness_min, brightness_max, 1,
1264 sd->contrast = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1265 V4L2_CID_CONTRAST, 0, contrast_max, 1, contrast_def);
1267 if (sd->sensor == SENSOR_OV772x) {
1268 sd->autogain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1269 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1270 sd->gain = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1271 V4L2_CID_GAIN, 0, 63, 1, 20);
1274 sd->autoexposure = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1275 V4L2_CID_EXPOSURE_AUTO,
1276 V4L2_EXPOSURE_MANUAL, 0,
1277 V4L2_EXPOSURE_AUTO);
1278 sd->exposure = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1279 V4L2_CID_EXPOSURE, exposure_min, exposure_max, 1,
1282 sd->autowhitebalance = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1283 V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1285 if (sd->sensor == SENSOR_OV772x)
1286 sd->sharpness = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1287 V4L2_CID_SHARPNESS, 0, 63, 1, 0);
1289 sd->hflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1290 V4L2_CID_HFLIP, 0, 1, 1, hflip_def);
1291 sd->vflip = v4l2_ctrl_new_std(hdl, &ov534_ctrl_ops,
1292 V4L2_CID_VFLIP, 0, 1, 1, 0);
1293 sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &ov534_ctrl_ops,
1294 V4L2_CID_POWER_LINE_FREQUENCY,
1295 V4L2_CID_POWER_LINE_FREQUENCY_50HZ, 0,
1296 V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1299 pr_err("Could not initialize controls\n");
1303 if (sd->sensor == SENSOR_OV772x)
1304 v4l2_ctrl_auto_cluster(2, &sd->autogain, 0, true);
1306 v4l2_ctrl_auto_cluster(2, &sd->autoexposure, V4L2_EXPOSURE_MANUAL,
1312 /* this function is called at probe and resume time */
1313 static int sd_init(struct gspca_dev *gspca_dev)
1315 struct sd *sd = (struct sd *) gspca_dev;
1317 static const struct reg_array bridge_init[NSENSORS] = {
1318 [SENSOR_OV767x] = {bridge_init_767x, ARRAY_SIZE(bridge_init_767x)},
1319 [SENSOR_OV772x] = {bridge_init_772x, ARRAY_SIZE(bridge_init_772x)},
1321 static const struct reg_array sensor_init[NSENSORS] = {
1322 [SENSOR_OV767x] = {sensor_init_767x, ARRAY_SIZE(sensor_init_767x)},
1323 [SENSOR_OV772x] = {sensor_init_772x, ARRAY_SIZE(sensor_init_772x)},
1327 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
1328 ov534_reg_write(gspca_dev, 0xe0, 0x08);
1331 /* initialize the sensor address */
1332 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS, 0x42);
1335 sccb_reg_write(gspca_dev, 0x12, 0x80);
1336 usleep_range(10000, 20000);
1338 /* probe the sensor */
1339 sccb_reg_read(gspca_dev, 0x0a);
1340 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
1341 sccb_reg_read(gspca_dev, 0x0b);
1342 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
1343 gspca_dbg(gspca_dev, D_PROBE, "Sensor ID: %04x\n", sensor_id);
1345 if ((sensor_id & 0xfff0) == 0x7670) {
1346 sd->sensor = SENSOR_OV767x;
1347 gspca_dev->cam.cam_mode = ov767x_mode;
1348 gspca_dev->cam.nmodes = ARRAY_SIZE(ov767x_mode);
1350 sd->sensor = SENSOR_OV772x;
1351 gspca_dev->cam.bulk = 1;
1352 gspca_dev->cam.bulk_size = 16384;
1353 gspca_dev->cam.bulk_nurbs = 2;
1354 gspca_dev->cam.mode_framerates = ov772x_framerates;
1358 reg_w_array(gspca_dev, bridge_init[sd->sensor].val,
1359 bridge_init[sd->sensor].len);
1360 ov534_set_led(gspca_dev, 1);
1361 sccb_w_array(gspca_dev, sensor_init[sd->sensor].val,
1362 sensor_init[sd->sensor].len);
1364 sd_stopN(gspca_dev);
1365 /* set_frame_rate(gspca_dev); */
1367 return gspca_dev->usb_err;
1370 static int sd_start(struct gspca_dev *gspca_dev)
1372 struct sd *sd = (struct sd *) gspca_dev;
1374 static const struct reg_array bridge_start[NSENSORS][4] = {
1375 [SENSOR_OV767x] = {{bridge_start_qvga_767x,
1376 ARRAY_SIZE(bridge_start_qvga_767x)},
1377 {bridge_start_vga_767x,
1378 ARRAY_SIZE(bridge_start_vga_767x)}},
1379 [SENSOR_OV772x] = {{bridge_start_qvga_yuyv_772x,
1380 ARRAY_SIZE(bridge_start_qvga_yuyv_772x)},
1381 {bridge_start_vga_yuyv_772x,
1382 ARRAY_SIZE(bridge_start_vga_yuyv_772x)},
1383 {bridge_start_qvga_gbrg_772x,
1384 ARRAY_SIZE(bridge_start_qvga_gbrg_772x)},
1385 {bridge_start_vga_gbrg_772x,
1386 ARRAY_SIZE(bridge_start_vga_gbrg_772x)} },
1388 static const struct reg_array sensor_start[NSENSORS][4] = {
1389 [SENSOR_OV767x] = {{sensor_start_qvga_767x,
1390 ARRAY_SIZE(sensor_start_qvga_767x)},
1391 {sensor_start_vga_767x,
1392 ARRAY_SIZE(sensor_start_vga_767x)}},
1393 [SENSOR_OV772x] = {{sensor_start_qvga_yuyv_772x,
1394 ARRAY_SIZE(sensor_start_qvga_yuyv_772x)},
1395 {sensor_start_vga_yuyv_772x,
1396 ARRAY_SIZE(sensor_start_vga_yuyv_772x)},
1397 {sensor_start_qvga_gbrg_772x,
1398 ARRAY_SIZE(sensor_start_qvga_gbrg_772x)},
1399 {sensor_start_vga_gbrg_772x,
1400 ARRAY_SIZE(sensor_start_vga_gbrg_772x)} },
1403 /* (from ms-win trace) */
1404 if (sd->sensor == SENSOR_OV767x)
1405 sccb_reg_write(gspca_dev, 0x1e, 0x04);
1406 /* black sun enable ? */
1408 mode = gspca_dev->curr_mode; /* 0: 320x240, 1: 640x480 */
1409 reg_w_array(gspca_dev, bridge_start[sd->sensor][mode].val,
1410 bridge_start[sd->sensor][mode].len);
1411 sccb_w_array(gspca_dev, sensor_start[sd->sensor][mode].val,
1412 sensor_start[sd->sensor][mode].len);
1414 set_frame_rate(gspca_dev);
1417 sethue(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue));
1418 setsaturation(gspca_dev, v4l2_ctrl_g_ctrl(sd->saturation));
1420 setagc(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
1421 setawb(gspca_dev, v4l2_ctrl_g_ctrl(sd->autowhitebalance));
1422 setaec(gspca_dev, v4l2_ctrl_g_ctrl(sd->autoexposure));
1424 setgain(gspca_dev, v4l2_ctrl_g_ctrl(sd->gain));
1425 setexposure(gspca_dev, v4l2_ctrl_g_ctrl(sd->exposure));
1426 setbrightness(gspca_dev, v4l2_ctrl_g_ctrl(sd->brightness));
1427 setcontrast(gspca_dev, v4l2_ctrl_g_ctrl(sd->contrast));
1429 setsharpness(gspca_dev, v4l2_ctrl_g_ctrl(sd->sharpness));
1430 sethvflip(gspca_dev, v4l2_ctrl_g_ctrl(sd->hflip),
1431 v4l2_ctrl_g_ctrl(sd->vflip));
1432 setlightfreq(gspca_dev, v4l2_ctrl_g_ctrl(sd->plfreq));
1434 ov534_set_led(gspca_dev, 1);
1435 ov534_reg_write(gspca_dev, 0xe0, 0x00);
1436 return gspca_dev->usb_err;
1439 static void sd_stopN(struct gspca_dev *gspca_dev)
1441 ov534_reg_write(gspca_dev, 0xe0, 0x09);
1442 ov534_set_led(gspca_dev, 0);
1445 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
1446 #define UVC_STREAM_EOH (1 << 7)
1447 #define UVC_STREAM_ERR (1 << 6)
1448 #define UVC_STREAM_STI (1 << 5)
1449 #define UVC_STREAM_RES (1 << 4)
1450 #define UVC_STREAM_SCR (1 << 3)
1451 #define UVC_STREAM_PTS (1 << 2)
1452 #define UVC_STREAM_EOF (1 << 1)
1453 #define UVC_STREAM_FID (1 << 0)
1455 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1458 struct sd *sd = (struct sd *) gspca_dev;
1461 int remaining_len = len;
1464 payload_len = gspca_dev->cam.bulk ? 2048 : 2040;
1466 len = min(remaining_len, payload_len);
1468 /* Payloads are prefixed with a UVC-style header. We
1469 consider a frame to start when the FID toggles, or the PTS
1470 changes. A frame ends when EOF is set, and we've received
1471 the correct number of bytes. */
1473 /* Verify UVC header. Header length is always 12 */
1474 if (data[0] != 12 || len < 12) {
1475 gspca_dbg(gspca_dev, D_PACK, "bad header\n");
1480 if (data[1] & UVC_STREAM_ERR) {
1481 gspca_dbg(gspca_dev, D_PACK, "payload error\n");
1485 /* Extract PTS and FID */
1486 if (!(data[1] & UVC_STREAM_PTS)) {
1487 gspca_dbg(gspca_dev, D_PACK, "PTS not present\n");
1490 this_pts = (data[5] << 24) | (data[4] << 16)
1491 | (data[3] << 8) | data[2];
1492 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
1494 /* If PTS or FID has changed, start a new frame. */
1495 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
1496 if (gspca_dev->last_packet_type == INTER_PACKET)
1497 gspca_frame_add(gspca_dev, LAST_PACKET,
1499 sd->last_pts = this_pts;
1500 sd->last_fid = this_fid;
1501 gspca_frame_add(gspca_dev, FIRST_PACKET,
1502 data + 12, len - 12);
1503 /* If this packet is marked as EOF, end the frame */
1504 } else if (data[1] & UVC_STREAM_EOF) {
1506 if (gspca_dev->pixfmt.pixelformat != V4L2_PIX_FMT_JPEG
1507 && gspca_dev->image_len + len - 12 !=
1508 gspca_dev->pixfmt.sizeimage) {
1509 gspca_dbg(gspca_dev, D_PACK, "wrong sized frame\n");
1512 gspca_frame_add(gspca_dev, LAST_PACKET,
1513 data + 12, len - 12);
1516 /* Add the data from this payload */
1517 gspca_frame_add(gspca_dev, INTER_PACKET,
1518 data + 12, len - 12);
1521 /* Done this payload */
1525 /* Discard data until a new frame starts. */
1526 gspca_dev->last_packet_type = DISCARD_PACKET;
1529 remaining_len -= len;
1531 } while (remaining_len > 0);
1534 /* get stream parameters (framerate) */
1535 static void sd_get_streamparm(struct gspca_dev *gspca_dev,
1536 struct v4l2_streamparm *parm)
1538 struct v4l2_captureparm *cp = &parm->parm.capture;
1539 struct v4l2_fract *tpf = &cp->timeperframe;
1540 struct sd *sd = (struct sd *) gspca_dev;
1543 tpf->denominator = sd->frame_rate;
1546 /* set stream parameters (framerate) */
1547 static void sd_set_streamparm(struct gspca_dev *gspca_dev,
1548 struct v4l2_streamparm *parm)
1550 struct v4l2_captureparm *cp = &parm->parm.capture;
1551 struct v4l2_fract *tpf = &cp->timeperframe;
1552 struct sd *sd = (struct sd *) gspca_dev;
1554 if (tpf->numerator == 0 || tpf->denominator == 0)
1555 sd->frame_rate = DEFAULT_FRAME_RATE;
1557 sd->frame_rate = tpf->denominator / tpf->numerator;
1559 if (gspca_dev->streaming)
1560 set_frame_rate(gspca_dev);
1562 /* Return the actual framerate */
1564 tpf->denominator = sd->frame_rate;
1567 /* sub-driver description */
1568 static const struct sd_desc sd_desc = {
1569 .name = MODULE_NAME,
1570 .config = sd_config,
1572 .init_controls = sd_init_controls,
1575 .pkt_scan = sd_pkt_scan,
1576 .get_streamparm = sd_get_streamparm,
1577 .set_streamparm = sd_set_streamparm,
1580 /* -- module initialisation -- */
1581 static const struct usb_device_id device_table[] = {
1582 {USB_DEVICE(0x1415, 0x2000)},
1583 {USB_DEVICE(0x06f8, 0x3002)},
1587 MODULE_DEVICE_TABLE(usb, device_table);
1589 /* -- device connect -- */
1590 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
1592 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1596 static struct usb_driver sd_driver = {
1597 .name = MODULE_NAME,
1598 .id_table = device_table,
1600 .disconnect = gspca_disconnect,
1602 .suspend = gspca_suspend,
1603 .resume = gspca_resume,
1604 .reset_resume = gspca_resume,
1608 module_usb_driver(sd_driver);