2 * ov534/ov772x gspca driver
3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it>
5 * Based on a prototype written by Mark Ferrell <majortrips@gmail.com>
6 * USB protocol reverse engineered by Jim Paris <jim@jtan.com>
7 * https://jim.sh/svn/jim/devl/playstation/ps3/eye/test/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define MODULE_NAME "ov534"
28 #define OV534_REG_ADDRESS 0xf1 /* ? */
29 #define OV534_REG_SUBADDR 0xf2
30 #define OV534_REG_WRITE 0xf3
31 #define OV534_REG_READ 0xf4
32 #define OV534_REG_OPERATION 0xf5
33 #define OV534_REG_STATUS 0xf6
35 #define OV534_OP_WRITE_3 0x37
36 #define OV534_OP_WRITE_2 0x33
37 #define OV534_OP_READ_2 0xf9
39 #define CTRL_TIMEOUT 500
41 MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>");
42 MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver");
43 MODULE_LICENSE("GPL");
45 /* global parameters */
46 static int frame_rate;
48 /* specific webcam descriptor */
50 struct gspca_dev gspca_dev; /* !! must be the first item */
55 /* V4L2 controls supported by the driver */
56 static struct ctrl sd_ctrls[] = {
59 static struct v4l2_pix_format vga_mode[] = {
60 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
61 .bytesperline = 640 * 2,
62 .sizeimage = 640 * 480 * 2,
63 .colorspace = V4L2_COLORSPACE_JPEG,
67 static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
72 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, 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, &data, 1, CTRL_TIMEOUT);
79 PDEBUG(D_ERR, "write failed");
82 static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
87 ret = usb_control_msg(udev,
88 usb_rcvctrlpipe(udev, 0),
90 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
91 0x0, reg, &data, 1, CTRL_TIMEOUT);
92 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
94 PDEBUG(D_ERR, "read failed");
98 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
99 * (direction and output)? */
100 static void ov534_set_led(struct usb_device *udev, int status)
104 PDEBUG(D_CONF, "led status: %d", status);
106 data = ov534_reg_read(udev, 0x21);
108 ov534_reg_write(udev, 0x21, data);
110 data = ov534_reg_read(udev, 0x23);
116 ov534_reg_write(udev, 0x23, data);
119 static int sccb_check_status(struct usb_device *udev)
124 for (i = 0; i < 5; i++) {
125 data = ov534_reg_read(udev, OV534_REG_STATUS);
135 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
142 static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
144 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
145 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
146 ov534_reg_write(udev, OV534_REG_WRITE, val);
147 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
149 if (!sccb_check_status(udev))
150 PDEBUG(D_ERR, "sccb_reg_write failed");
153 static const __u8 ov534_reg_initdata[][2] = {
156 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
198 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
199 { 0x1d, 0x58 }, /* frame size */
200 { 0x1d, 0x00 }, /* frame size */
211 static const __u8 ov772x_reg_initdata[][2] = {
296 static void ov534_setup(struct usb_device *udev)
300 /* Initialize bridge chip */
301 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
302 ov534_reg_write(udev, ov534_reg_initdata[i][0],
303 ov534_reg_initdata[i][1]);
305 ov534_set_led(udev, 1);
307 /* Initialize sensor */
308 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
309 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
310 ov772x_reg_initdata[i][1]);
312 ov534_reg_write(udev, 0xe0, 0x09);
313 ov534_set_led(udev, 0);
316 /* this function is called at probe time */
317 static int sd_config(struct gspca_dev *gspca_dev,
318 const struct usb_device_id *id)
322 cam = &gspca_dev->cam;
325 cam->cam_mode = vga_mode;
326 cam->nmodes = ARRAY_SIZE(vga_mode);
328 cam->bulk_size = 2048;
334 /* this function is called at probe and resume time */
335 static int sd_init(struct gspca_dev *gspca_dev)
339 ov534_setup(gspca_dev->dev);
345 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
346 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
347 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
350 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
351 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
352 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
357 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
358 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
359 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
362 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
363 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
364 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
368 PDEBUG(D_PROBE, "frame_rate: %d", fr);
373 static int sd_start(struct gspca_dev *gspca_dev)
375 /* start streaming data */
376 ov534_set_led(gspca_dev->dev, 1);
377 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
382 static void sd_stopN(struct gspca_dev *gspca_dev)
384 /* stop streaming data */
385 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
386 ov534_set_led(gspca_dev->dev, 0);
389 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
390 #define UVC_STREAM_EOH (1 << 7)
391 #define UVC_STREAM_ERR (1 << 6)
392 #define UVC_STREAM_STI (1 << 5)
393 #define UVC_STREAM_RES (1 << 4)
394 #define UVC_STREAM_SCR (1 << 3)
395 #define UVC_STREAM_PTS (1 << 2)
396 #define UVC_STREAM_EOF (1 << 1)
397 #define UVC_STREAM_FID (1 << 0)
399 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
402 struct sd *sd = (struct sd *) gspca_dev;
406 /* Payloads are prefixed with a the UVC-style header. We
407 consider a frame to start when the FID toggles, or the PTS
408 changes. A frame ends when EOF is set, and we've received
409 the correct number of bytes. */
411 /* Verify UVC header. Header length is always 12 */
412 if (data[0] != 12 || len < 12) {
413 PDEBUG(D_PACK, "bad header");
418 if (data[1] & UVC_STREAM_ERR) {
419 PDEBUG(D_PACK, "payload error");
423 /* Extract PTS and FID */
424 if (!(data[1] & UVC_STREAM_PTS)) {
425 PDEBUG(D_PACK, "PTS not present");
428 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
429 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
431 /* If PTS or FID has changed, start a new frame. */
432 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
433 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
434 sd->last_pts = this_pts;
435 sd->last_fid = this_fid;
438 /* Add the data from this payload */
439 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
440 data + 12, len - 12);
442 /* If this packet is marked as EOF, end the frame */
443 if (data[1] & UVC_STREAM_EOF) {
446 if ((frame->data_end - frame->data) !=
447 (gspca_dev->width * gspca_dev->height * 2)) {
448 PDEBUG(D_PACK, "short frame");
452 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
459 /* Discard data until a new frame starts. */
460 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
463 /* sub-driver description */
464 static const struct sd_desc sd_desc = {
467 .nctrls = ARRAY_SIZE(sd_ctrls),
472 .pkt_scan = sd_pkt_scan,
475 /* -- module initialisation -- */
476 static const __devinitdata struct usb_device_id device_table[] = {
477 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
478 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
479 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
483 MODULE_DEVICE_TABLE(usb, device_table);
485 /* -- device connect -- */
486 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
488 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
492 static struct usb_driver sd_driver = {
494 .id_table = device_table,
496 .disconnect = gspca_disconnect,
498 .suspend = gspca_suspend,
499 .resume = gspca_resume,
503 /* -- module insert / remove -- */
504 static int __init sd_mod_init(void)
506 if (usb_register(&sd_driver) < 0)
508 PDEBUG(D_PROBE, "registered");
512 static void __exit sd_mod_exit(void)
514 usb_deregister(&sd_driver);
515 PDEBUG(D_PROBE, "deregistered");
518 module_init(sd_mod_init);
519 module_exit(sd_mod_exit);
521 module_param(frame_rate, int, 0644);
522 MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");