2 * ov534/ov772x gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
4 * Copyright (C) 2008 Jim Paris <jim@jtan.com>
6 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
7 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
8 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define MODULE_NAME "ov534"
29 #define OV534_REG_ADDRESS 0xf1 /* ? */
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
42 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
43 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
44 MODULE_LICENSE("GPL");
46 /* specific webcam descriptor */
48 struct gspca_dev gspca_dev; /* !! must be the first item */
54 /* V4L2 controls supported by the driver */
55 static struct ctrl sd_ctrls[] = {
58 static const struct v4l2_pix_format vga_mode[] = {
59 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
60 .bytesperline = 640 * 2,
61 .sizeimage = 640 * 480 * 2,
62 .colorspace = V4L2_COLORSPACE_JPEG,
66 static void ov534_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
68 struct usb_device *udev = gspca_dev->dev;
71 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
72 gspca_dev->usb_buf[0] = val;
73 ret = usb_control_msg(udev,
74 usb_sndctrlpipe(udev, 0),
76 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
77 0x0, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
79 PDEBUG(D_ERR, "write failed");
82 static u8 ov534_reg_read(struct gspca_dev *gspca_dev, u16 reg)
84 struct usb_device *udev = gspca_dev->dev;
87 ret = usb_control_msg(udev,
88 usb_rcvctrlpipe(udev, 0),
90 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 0x0, reg, gspca_dev->usb_buf, 1, CTRL_TIMEOUT);
92 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, gspca_dev->usb_buf[0]);
94 PDEBUG(D_ERR, "read failed");
95 return gspca_dev->usb_buf[0];
98 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
99 * (direction and output)? */
100 static void ov534_set_led(struct gspca_dev *gspca_dev, int status)
104 PDEBUG(D_CONF, "led status: %d", status);
106 data = ov534_reg_read(gspca_dev, 0x21);
108 ov534_reg_write(gspca_dev, 0x21, data);
110 data = ov534_reg_read(gspca_dev, 0x23);
116 ov534_reg_write(gspca_dev, 0x23, data);
119 static int sccb_check_status(struct gspca_dev *gspca_dev)
124 for (i = 0; i < 5; i++) {
125 data = ov534_reg_read(gspca_dev, OV534_REG_STATUS);
135 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5",
142 static void sccb_reg_write(struct gspca_dev *gspca_dev, u16 reg, u8 val)
144 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
145 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
146 ov534_reg_write(gspca_dev, OV534_REG_WRITE, val);
147 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
149 if (!sccb_check_status(gspca_dev))
150 PDEBUG(D_ERR, "sccb_reg_write failed");
154 static u8 sccb_reg_read(struct gspca_dev *gspca_dev, u16 reg)
156 ov534_reg_write(gspca_dev, OV534_REG_SUBADDR, reg);
157 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_WRITE_2);
158 if (!sccb_check_status(gspca_dev))
159 PDEBUG(D_ERR, "sccb_reg_read failed 1");
161 ov534_reg_write(gspca_dev, OV534_REG_OPERATION, OV534_OP_READ_2);
162 if (!sccb_check_status(gspca_dev))
163 PDEBUG(D_ERR, "sccb_reg_read failed 2");
165 return ov534_reg_read(gspca_dev, OV534_REG_READ);
169 static const __u8 ov534_reg_initdata[][2] = {
172 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
212 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
213 { 0x1d, 0x00 }, /* payload size */
214 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
215 { 0x1d, 0x58 }, /* frame size */
216 { 0x1d, 0x00 }, /* frame size */
219 { 0x1d, 0x08 }, /* turn on UVC header */
220 { 0x1d, 0x0e }, /* .. */
231 static const __u8 ov772x_reg_initdata[][2] = {
315 static void ov534_set_frame_rate(struct gspca_dev *gspca_dev)
317 struct sd *sd = (struct sd *) gspca_dev;
318 int fr = sd->frame_rate;
322 sccb_reg_write(gspca_dev, 0x11, 0x01);
323 sccb_reg_write(gspca_dev, 0x0d, 0x41);
324 ov534_reg_write(gspca_dev, 0xe5, 0x02);
327 sccb_reg_write(gspca_dev, 0x11, 0x02);
328 sccb_reg_write(gspca_dev, 0x0d, 0xc1);
329 ov534_reg_write(gspca_dev, 0xe5, 0x04);
334 sccb_reg_write(gspca_dev, 0x11, 0x04);
335 sccb_reg_write(gspca_dev, 0x0d, 0x81);
336 ov534_reg_write(gspca_dev, 0xe5, 0x02);
339 sccb_reg_write(gspca_dev, 0x11, 0x03);
340 sccb_reg_write(gspca_dev, 0x0d, 0x41);
341 ov534_reg_write(gspca_dev, 0xe5, 0x04);
346 PDEBUG(D_PROBE, "frame_rate: %d", fr);
350 static void ov534_setup(struct gspca_dev *gspca_dev)
354 /* Initialize bridge chip */
355 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
356 ov534_reg_write(gspca_dev, ov534_reg_initdata[i][0],
357 ov534_reg_initdata[i][1]);
359 PDEBUG(D_PROBE, "sensor is ov%02x%02x",
360 sccb_reg_read(gspca_dev, 0x0a),
361 sccb_reg_read(gspca_dev, 0x0b));
363 ov534_set_led(gspca_dev, 1);
365 /* Initialize sensor */
366 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
367 sccb_reg_write(gspca_dev, ov772x_reg_initdata[i][0],
368 ov772x_reg_initdata[i][1]);
370 ov534_reg_write(gspca_dev, 0xe0, 0x09);
371 ov534_set_led(gspca_dev, 0);
374 /* this function is called at probe time */
375 static int sd_config(struct gspca_dev *gspca_dev,
376 const struct usb_device_id *id)
380 cam = &gspca_dev->cam;
383 cam->cam_mode = vga_mode;
384 cam->nmodes = ARRAY_SIZE(vga_mode);
386 cam->bulk_size = 16384;
392 /* this function is called at probe and resume time */
393 static int sd_init(struct gspca_dev *gspca_dev)
395 ov534_setup(gspca_dev);
396 ov534_set_frame_rate(gspca_dev);
401 static int sd_start(struct gspca_dev *gspca_dev)
403 /* start streaming data */
404 ov534_set_led(gspca_dev, 1);
405 ov534_reg_write(gspca_dev, 0xe0, 0x00);
410 static void sd_stopN(struct gspca_dev *gspca_dev)
412 /* stop streaming data */
413 ov534_reg_write(gspca_dev, 0xe0, 0x09);
414 ov534_set_led(gspca_dev, 0);
417 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
418 #define UVC_STREAM_EOH (1 << 7)
419 #define UVC_STREAM_ERR (1 << 6)
420 #define UVC_STREAM_STI (1 << 5)
421 #define UVC_STREAM_RES (1 << 4)
422 #define UVC_STREAM_SCR (1 << 3)
423 #define UVC_STREAM_PTS (1 << 2)
424 #define UVC_STREAM_EOF (1 << 1)
425 #define UVC_STREAM_FID (1 << 0)
427 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
430 struct sd *sd = (struct sd *) gspca_dev;
433 int remaining_len = len;
434 __u8 *next_data = data;
437 if (remaining_len <= 0)
441 len = min(remaining_len, 2048);
442 remaining_len -= len;
445 /* Payloads are prefixed with a UVC-style header. We
446 consider a frame to start when the FID toggles, or the PTS
447 changes. A frame ends when EOF is set, and we've received
448 the correct number of bytes. */
450 /* Verify UVC header. Header length is always 12 */
451 if (data[0] != 12 || len < 12) {
452 PDEBUG(D_PACK, "bad header");
457 if (data[1] & UVC_STREAM_ERR) {
458 PDEBUG(D_PACK, "payload error");
462 /* Extract PTS and FID */
463 if (!(data[1] & UVC_STREAM_PTS)) {
464 PDEBUG(D_PACK, "PTS not present");
467 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
468 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
470 /* If PTS or FID has changed, start a new frame. */
471 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
472 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
473 sd->last_pts = this_pts;
474 sd->last_fid = this_fid;
477 /* Add the data from this payload */
478 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
479 data + 12, len - 12);
481 /* If this packet is marked as EOF, end the frame */
482 if (data[1] & UVC_STREAM_EOF) {
485 if ((frame->data_end - frame->data) !=
486 (gspca_dev->width * gspca_dev->height * 2)) {
487 PDEBUG(D_PACK, "short frame");
491 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
494 /* Done this payload */
498 /* Discard data until a new frame starts. */
499 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
503 /* get stream parameters (framerate) */
504 static int sd_get_streamparm(struct gspca_dev *gspca_dev,
505 struct v4l2_streamparm *parm)
507 struct v4l2_captureparm *cp = &parm->parm.capture;
508 struct v4l2_fract *tpf = &cp->timeperframe;
509 struct sd *sd = (struct sd *) gspca_dev;
511 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
514 cp->capability |= V4L2_CAP_TIMEPERFRAME;
516 tpf->denominator = sd->frame_rate;
521 /* set stream parameters (framerate) */
522 static int sd_set_streamparm(struct gspca_dev *gspca_dev,
523 struct v4l2_streamparm *parm)
525 struct v4l2_captureparm *cp = &parm->parm.capture;
526 struct v4l2_fract *tpf = &cp->timeperframe;
527 struct sd *sd = (struct sd *) gspca_dev;
529 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
532 /* Set requested framerate */
533 sd->frame_rate = tpf->denominator / tpf->numerator;
534 ov534_set_frame_rate(gspca_dev);
536 /* Return the actual framerate */
538 tpf->denominator = sd->frame_rate;
543 /* sub-driver description */
544 static const struct sd_desc sd_desc = {
547 .nctrls = ARRAY_SIZE(sd_ctrls),
552 .pkt_scan = sd_pkt_scan,
553 .get_streamparm = sd_get_streamparm,
554 .set_streamparm = sd_set_streamparm,
557 /* -- module initialisation -- */
558 static const __devinitdata struct usb_device_id device_table[] = {
559 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
560 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
561 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
565 MODULE_DEVICE_TABLE(usb, device_table);
567 /* -- device connect -- */
568 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
570 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
574 static struct usb_driver sd_driver = {
576 .id_table = device_table,
578 .disconnect = gspca_disconnect,
580 .suspend = gspca_suspend,
581 .resume = gspca_resume,
585 /* -- module insert / remove -- */
586 static int __init sd_mod_init(void)
588 if (usb_register(&sd_driver) < 0)
590 PDEBUG(D_PROBE, "registered");
594 static void __exit sd_mod_exit(void)
596 usb_deregister(&sd_driver);
597 PDEBUG(D_PROBE, "deregistered");
600 module_init(sd_mod_init);
601 module_exit(sd_mod_exit);