3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
5 * Copyright (C) 2009 Jean-Francois Moine http://moinejf.free.fr
7 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
8 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
9 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define MODULE_NAME "ov534"
30 #define OV534_REG_ADDRESS 0xf1 /* sensor address */
31 #define OV534_REG_SUBADDR 0xf2
32 #define OV534_REG_WRITE 0xf3
33 #define OV534_REG_READ 0xf4
34 #define OV534_REG_OPERATION 0xf5
35 #define OV534_REG_STATUS 0xf6
37 #define OV534_OP_WRITE_3 0x37
38 #define OV534_OP_WRITE_2 0x33
39 #define OV534_OP_READ_2 0xf9
41 #define CTRL_TIMEOUT 500
43 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
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 */
55 #define SENSOR_OV772X 0
56 #define SENSOR_OV965X 1
59 /* V4L2 controls supported by the driver */
60 static struct ctrl sd_ctrls[] = {
63 static const struct v4l2_pix_format vga_mode[] = {
64 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
65 .bytesperline = 640 * 2,
66 .sizeimage = 640 * 480 * 2,
67 .colorspace = V4L2_COLORSPACE_JPEG,
71 static const u8 bridge_init_ov722x[][2] = {
110 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
111 { 0x1d, 0x00 }, /* payload size */
112 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
113 { 0x1d, 0x58 }, /* frame size */
114 { 0x1d, 0x00 }, /* frame size */
117 { 0x1d, 0x08 }, /* turn on UVC header */
118 { 0x1d, 0x0e }, /* .. */
129 static const u8 sensor_init_ov722x[][2] = {
212 static const u8 bridge_init_ov965x[][2] = {
246 static const u8 sensor_init_ov965x[][2] = {
247 {0x12, 0x80}, /* com7 - reset */
248 {0x00, 0x00}, /* gain */
249 {0x01, 0x80}, /* blue */
250 {0x02, 0x80}, /* red */
251 {0x03, 0x1b}, /* vref */
252 {0x04, 0x03}, /* com1 - exposure low bits */
253 {0x0b, 0x57}, /* ver */
254 {0x0e, 0x61}, /* com5 */
255 {0x0f, 0x42}, /* com6 */
256 {0x11, 0x00}, /* clkrc */
257 {0x12, 0x02}, /* com7 */
258 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
259 {0x14, 0x28}, /* com9 */
260 {0x16, 0x24}, /* rsvd16 */
261 {0x17, 0x1d}, /* hstart*/
262 {0x18, 0xbd}, /* hstop */
263 {0x19, 0x01}, /* vstrt */
264 {0x1a, 0x81}, /* vstop*/
265 {0x1e, 0x04}, /* mvfp */
266 {0x24, 0x3c}, /* aew */
267 {0x25, 0x36}, /* aeb */
268 {0x26, 0x71}, /* vpt */
269 {0x27, 0x08}, /* bbias */
270 {0x28, 0x08}, /* gbbias */
271 {0x29, 0x15}, /* gr com */
274 {0x2c, 0x08}, /* rbias */
275 {0x32, 0xff}, /* href */
276 {0x33, 0x00}, /* chlf */
277 {0x34, 0x3f}, /* arblm */
278 {0x35, 0x00}, /* rsvd35 */
279 {0x36, 0xf8}, /* rsvd36 */
280 {0x38, 0x72}, /* acom38 */
281 {0x39, 0x57}, /* ofon */
282 {0x3a, 0x80}, /* tslb */
284 {0x3d, 0x99}, /* com13 */
286 {0x40, 0xc0}, /* com15 */
287 {0x41, 0x40}, /* com16 */
316 {0x62, 0x00}, /* lcc1 */
317 {0x63, 0x00}, /* lcc2 */
318 {0x64, 0x02}, /* lcc3 */
319 {0x65, 0x16}, /* lcc4 */
320 {0x66, 0x01}, /* lcc5 */
321 {0x69, 0x02}, /* hv */
322 {0x6b, 0x5a}, /* dbvl */
353 {0x14, 0x28}, /* com9 */
360 {0xa1, 0x40}, /* aechm */
362 {0xa5, 0x68}, /* com26 */
364 {0xa8, 0xc1}, /* acoma8 */
365 {0xa9, 0xef}, /* acoma9 */
400 {0xff, 0x41}, /* read 41, write ff 00 */
401 {0x41, 0x40}, /* com16 */
405 {0x12, 0x62}, /* com7 - VGA + CIF */
406 {0x36, 0xfa}, /* rsvd36 */
407 {0x69, 0x0a}, /* hv */
408 {0x8c, 0x89}, /* com22 */
409 {0x14, 0x28}, /* com9 */
411 {0x41, 0x40}, /* com16 */
418 {0x03, 0x12}, /* vref */
419 {0x17, 0x16}, /* hstart */
420 {0x18, 0x02}, /* hstop */
421 {0x19, 0x01}, /* vstrt */
422 {0x1a, 0x3d}, /* vstop */
423 {0x32, 0xff}, /* href */
427 static const u8 bridge_init_ov965x_2[][2] = {
455 static const u8 sensor_init_ov965x_2[][2] = {
457 {0x1e, 0x04}, /* mvfp */
458 {0x13, 0xe0}, /* com8 */
459 {0x00, 0x00}, /* gain */
460 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
461 {0x11, 0x03}, /* clkrc */
462 {0x6b, 0x5a}, /* dblv */
468 {0xff, 0x42}, /* read 42, write ff 00 */
471 {0xff, 0x42}, /* read 42, write ff 00 */
474 {0xff, 0x42}, /* read 42, write ff 00 */
483 {0xff, 0x41}, /* read 41, write ff 00 */
484 {0x41, 0x40}, /* com16 */
487 {0x10, 0x25}, /* aech - exposure high bits */
488 {0xff, 0x13}, /* read 13, write ff 00 */
489 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
492 static const u8 bridge_start_ov965x[][2] = {
510 static const u8 sensor_start_ov965x[][2] = {
512 {0x1e, 0x04}, /* mvfp */
513 {0x13, 0xe0}, /* com8 */
515 {0x13, 0xe7}, /* com8 - everything (AGC, AWB and AEC) */
516 {0x11, 0x01}, /* clkrc */
517 {0x6b, 0x5a}, /* dblv */
522 {0xff, 0x13}, /* read 13, write ff 00 */
525 {0xff, 0x42}, /* read 42, write ff 00 */
530 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
532 struct usb_device *udev = gspca_dev->dev;
535 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
536 gspca_dev->usb_buf[0] = val;
537 ret = usb_control_msg(udev,
538 usb_sndctrlpipe(udev, 0),
540 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
541 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
543 PDEBUG(D_ERR, "write failed");
546 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
548 struct usb_device *udev = gspca_dev->dev;
551 ret = usb_control_msg(udev,
552 usb_rcvctrlpipe(udev, 0),
554 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
555 0x00, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
556 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
558 PDEBUG(D_ERR, "read failed");
559 return gspca_dev->usb_buf[0];
562 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
563 * (direction and output)? */
564 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
568 PDEBUG(D_CONF, "led status: %d", status);
570 data = ov534_reg_read(gspca_dev, 0x21);
572 ov534_reg_write(gspca_dev, 0x21, data);
574 data = ov534_reg_read(gspca_dev, 0x23);
580 ov534_reg_write(gspca_dev, 0x23, data);
583 data = ov534_reg_read(gspca_dev, 0x21);
585 ov534_reg_write(gspca_dev, 0x21, data);
589 static int sccb_check_status(struct gspca_dev *gspca_dev)
594 for (i = 0; i < 5; i++) {
595 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
605 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
612 static void sccb_reg_write(struct gspca_dev *gspca_dev, u8 reg, u8 val)
614 PDEBUG(D_USBO, "reg: 0x%02x, val: 0x%02x", reg, val);
615 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
616 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
617 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
619 if (!sccb_check_status(gspca_dev))
620 PDEBUG(D_ERR, "sccb_reg_write failed");
623 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
625 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
626 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
627 if (!sccb_check_status(gspca_dev))
628 PDEBUG(D_ERR, "sccb_reg_read failed 1");
630 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
631 if (!sccb_check_status(gspca_dev))
632 PDEBUG(D_ERR, "sccb_reg_read failed 2");
634 return ov534_reg_read(gspca_dev, OV534_REG_READ);
637 /* output a bridge sequence (reg - val) */
638 static void reg_w_array(struct gspca_dev *gspca_dev,
639 const u8 (*data)[2], int len)
642 ov534_reg_write(gspca_dev, (*data)[0], (*data)[1]);
647 /* output a sensor sequence (reg - val) */
648 static void sccb_w_array(struct gspca_dev *gspca_dev,
649 const u8 (*data)[2], int len)
652 if ((*data)[0] != 0xff) {
653 sccb_reg_write(gspca_dev, (*data)[0], (*data)[1]);
655 sccb_reg_read(gspca_dev, (*data)[1]);
656 sccb_reg_write(gspca_dev, 0xff, 0x00);
663 static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
665 struct sd *sd = (struct sd *) gspca_dev;
666 int fr = sd->frame_rate;
670 sccb_reg_write(gspca_dev, 0x11, 0x01);
671 sccb_reg_write(gspca_dev, 0x0d, 0x41);
672 ov534_reg_write(gspca_dev, 0xe5, 0x02);
675 sccb_reg_write(gspca_dev, 0x11, 0x02);
676 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
677 ov534_reg_write(gspca_dev, 0xe5, 0x04);
682 sccb_reg_write(gspca_dev, 0x11, 0x04);
683 sccb_reg_write(gspca_dev, 0x0d, 0x81);
684 ov534_reg_write(gspca_dev, 0xe5, 0x02);
687 sccb_reg_write(gspca_dev, 0x11, 0x03);
688 sccb_reg_write(gspca_dev, 0x0d, 0x41);
689 ov534_reg_write(gspca_dev, 0xe5, 0x04);
694 PDEBUG(D_PROBE, "frame_rate: %d", fr);
697 /* this function is called at probe time */
698 static int sd_config(struct gspca_dev *gspca_dev,
699 const struct usb_device_id *id)
701 struct sd *sd = (struct sd *) gspca_dev;
704 sd->sensor = id->driver_info;
706 cam = &gspca_dev->cam;
708 cam->cam_mode = vga_mode;
709 cam->nmodes = ARRAY_SIZE(vga_mode);
711 cam->bulk_size = 16384;
717 /* this function is called at probe and resume time */
718 static int sd_init(struct gspca_dev *gspca_dev)
720 struct sd *sd = (struct sd *) gspca_dev;
722 static const u8 sensor_addr[2] = {
723 0x42, /* 0 SENSOR_OV772X */
724 0x60, /* 1 SENSOR_OV965X */
728 ov534_reg_write(gspca_dev, 0xe7, 0x3a);
729 ov534_reg_write(gspca_dev, 0xe0, 0x08);
732 /* initialize the sensor address */
733 ov534_reg_write(gspca_dev, OV534_REG_ADDRESS,
734 sensor_addr[sd->sensor]);
737 sccb_reg_write(gspca_dev, 0x12, 0x80);
740 /* probe the sensor */
741 sccb_reg_read(gspca_dev, 0x0a);
742 sensor_id = sccb_reg_read(gspca_dev, 0x0a) << 8;
743 sccb_reg_read(gspca_dev, 0x0b);
744 sensor_id |= sccb_reg_read(gspca_dev, 0x0b);
745 PDEBUG(D_PROBE, "Sensor ID: %04x", sensor_id);
748 switch (sd->sensor) {
750 reg_w_array(gspca_dev, bridge_init_ov722x,
751 ARRAY_SIZE(bridge_init_ov722x));
752 ov534_set_led(gspca_dev, 1);
753 sccb_w_array(gspca_dev, sensor_init_ov722x,
754 ARRAY_SIZE(sensor_init_ov722x));
755 ov534_reg_write(gspca_dev, 0xe0, 0x09);
756 ov534_set_led(gspca_dev, 0);
757 ov534_set_frame_rate(gspca_dev);
760 /* case SENSOR_OV965X: */
761 reg_w_array(gspca_dev, bridge_init_ov965x,
762 ARRAY_SIZE(bridge_init_ov965x));
763 sccb_w_array(gspca_dev, sensor_init_ov965x,
764 ARRAY_SIZE(sensor_init_ov965x));
765 reg_w_array(gspca_dev, bridge_init_ov965x_2,
766 ARRAY_SIZE(bridge_init_ov965x_2));
767 sccb_w_array(gspca_dev, sensor_init_ov965x_2,
768 ARRAY_SIZE(sensor_init_ov965x_2));
769 ov534_reg_write(gspca_dev, 0xe0, 0x00);
770 ov534_reg_write(gspca_dev, 0xe0, 0x01);
771 ov534_set_led(gspca_dev, 0);
772 ov534_reg_write(gspca_dev, 0xe0, 0x00);
778 static int sd_start(struct gspca_dev *gspca_dev)
780 struct sd *sd = (struct sd *) gspca_dev;
782 switch (sd->sensor) {
784 ov534_set_led(gspca_dev, 1);
785 ov534_reg_write(gspca_dev, 0xe0, 0x00);
788 /* case SENSOR_OV965X: */
789 reg_w_array(gspca_dev, bridge_start_ov965x,
790 ARRAY_SIZE(bridge_start_ov965x));
791 sccb_w_array(gspca_dev, sensor_start_ov965x,
792 ARRAY_SIZE(sensor_start_ov965x));
793 ov534_reg_write(gspca_dev, 0xe0, 0x00);
794 ov534_set_led(gspca_dev, 1);
795 /*fixme: other sensor start omitted*/
800 static void sd_stopN(struct gspca_dev *gspca_dev)
802 struct sd *sd = (struct sd *) gspca_dev;
804 switch (sd->sensor) {
806 ov534_reg_write(gspca_dev, 0xe0, 0x09);
807 ov534_set_led(gspca_dev, 0);
810 /* case SENSOR_OV965X: */
811 ov534_reg_write(gspca_dev, 0xe0, 0x01);
812 ov534_set_led(gspca_dev, 0);
813 ov534_reg_write(gspca_dev, 0xe0, 0x00);
818 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
819 #define UVC_STREAM_EOH (1 << 7)
820 #define UVC_STREAM_ERR (1 << 6)
821 #define UVC_STREAM_STI (1 << 5)
822 #define UVC_STREAM_RES (1 << 4)
823 #define UVC_STREAM_SCR (1 << 3)
824 #define UVC_STREAM_PTS (1 << 2)
825 #define UVC_STREAM_EOF (1 << 1)
826 #define UVC_STREAM_FID (1 << 0)
828 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
831 struct sd *sd = (struct sd *) gspca_dev;
834 int remaining_len = len;
837 len = min(remaining_len, 2040); /*fixme: was 2048*/
839 /* Payloads are prefixed with a UVC-style header. We
840 consider a frame to start when the FID toggles, or the PTS
841 changes. A frame ends when EOF is set, and we've received
842 the correct number of bytes. */
844 /* Verify UVC header. Header length is always 12 */
845 if (data[0] != 12 || len < 12) {
846 PDEBUG(D_PACK, "bad header");
851 if (data[1] & UVC_STREAM_ERR) {
852 PDEBUG(D_PACK, "payload error");
856 /* Extract PTS and FID */
857 if (!(data[1] & UVC_STREAM_PTS)) {
858 PDEBUG(D_PACK, "PTS not present");
861 this_pts = (data[5] << 24) | (data[4] << 16)
862 | (data[3] << 8) | data[2];
863 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
865 /* If PTS or FID has changed, start a new frame. */
866 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
867 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
869 sd->last_pts = this_pts;
870 sd->last_fid = this_fid;
873 /* Add the data from this payload */
874 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
875 data + 12, len - 12);
877 /* If this packet is marked as EOF, end the frame */
878 if (data[1] & UVC_STREAM_EOF) {
881 if (frame->data_end - frame->data !=
882 gspca_dev->width * gspca_dev->height * 2) {
883 PDEBUG(D_PACK, "short frame");
887 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
891 /* Done this payload */
895 /* Discard data until a new frame starts. */
896 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
899 remaining_len -= len;
901 } while (remaining_len > 0);
904 /* get stream parameters (framerate) */
905 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
906 struct v4l2_streamparm *parm)
908 struct v4l2_captureparm *cp = &parm->parm.capture;
909 struct v4l2_fract *tpf = &cp->timeperframe;
910 struct sd *sd = (struct sd *) gspca_dev;
912 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
915 cp->capability |= V4L2_CAP_TIMEPERFRAME;
917 tpf->denominator = sd->frame_rate;
922 /* set stream parameters (framerate) */
923 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
924 struct v4l2_streamparm *parm)
926 struct v4l2_captureparm *cp = &parm->parm.capture;
927 struct v4l2_fract *tpf = &cp->timeperframe;
928 struct sd *sd = (struct sd *) gspca_dev;
930 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
933 /* Set requested framerate */
934 sd->frame_rate = tpf->denominator / tpf->numerator;
935 ov534_set_frame_rate(gspca_dev);
937 /* Return the actual framerate */
939 tpf->denominator = sd->frame_rate;
944 /* sub-driver description */
945 static const struct sd_desc sd_desc = {
948 .nctrls = ARRAY_SIZE(sd_ctrls),
953 .pkt_scan = sd_pkt_scan,
954 .get_streamparm = sd_get_streamparm,
955 .set_streamparm = sd_set_streamparm,
958 /* -- module initialisation -- */
959 static const __devinitdata struct usb_device_id device_table[] = {
960 {USB_DEVICE(0x06f8, 0x3003), .driver_info = SENSOR_OV965X},
961 {USB_DEVICE(0x1415, 0x2000), .driver_info = SENSOR_OV772X},
965 MODULE_DEVICE_TABLE(usb, device_table);
967 /* -- device connect -- */
968 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
970 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
974 static struct usb_driver sd_driver = {
976 .id_table = device_table,
978 .disconnect = gspca_disconnect,
980 .suspend = gspca_suspend,
981 .resume = gspca_resume,
985 /* -- module insert / remove -- */
986 static int __init sd_mod_init(void)
989 ret = usb_register(&sd_driver);
992 PDEBUG(D_PROBE, "registered");
996 static void __exit sd_mod_exit(void)
998 usb_deregister(&sd_driver);
999 PDEBUG(D_PROBE, "deregistered");
1002 module_init(sd_mod_init);
1003 module_exit(sd_mod_exit);