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 /* global parameters */
47 static int frame_rate;
49 /* specific webcam descriptor */
51 struct gspca_dev gspca_dev; /* !! must be the first item */
56 /* V4L2 controls supported by the driver */
57 static struct ctrl sd_ctrls[] = {
60 static struct v4l2_pix_format vga_mode[] = {
61 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
62 .bytesperline = 640 * 2,
63 .sizeimage = 640 * 480 * 2,
64 .colorspace = V4L2_COLORSPACE_JPEG,
68 static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
73 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
74 ret = usb_control_msg(udev,
75 usb_sndctrlpipe(udev, 0),
77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
78 0x0, reg, &data, 1, CTRL_TIMEOUT);
80 PDEBUG(D_ERR, "write failed");
83 static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
88 ret = usb_control_msg(udev,
89 usb_rcvctrlpipe(udev, 0),
91 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
92 0x0, reg, &data, 1, CTRL_TIMEOUT);
93 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
95 PDEBUG(D_ERR, "read failed");
99 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
100 * (direction and output)? */
101 static void ov534_set_led(struct usb_device *udev, int status)
105 PDEBUG(D_CONF, "led status: %d", status);
107 data = ov534_reg_read(udev, 0x21);
109 ov534_reg_write(udev, 0x21, data);
111 data = ov534_reg_read(udev, 0x23);
117 ov534_reg_write(udev, 0x23, data);
120 static int sccb_check_status(struct usb_device *udev)
125 for (i = 0; i < 5; i++) {
126 data = ov534_reg_read(udev, OV534_REG_STATUS);
136 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
143 static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
145 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
146 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
147 ov534_reg_write(udev, OV534_REG_WRITE, val);
148 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
150 if (!sccb_check_status(udev))
151 PDEBUG(D_ERR, "sccb_reg_write failed");
154 static const __u8 ov534_reg_initdata[][2] = {
157 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
197 { 0x1d, 0x02 }, /* payload size 0x0200 * 4 = 2048 bytes */
198 { 0x1d, 0x00 }, /* payload size */
199 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
200 { 0x1d, 0x58 }, /* frame size */
201 { 0x1d, 0x00 }, /* frame size */
204 { 0x1d, 0x08 }, /* turn on UVC header */
205 { 0x1d, 0x0e }, /* .. */
216 static const __u8 ov772x_reg_initdata[][2] = {
301 static void ov534_setup(struct usb_device *udev)
305 /* Initialize bridge chip */
306 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
307 ov534_reg_write(udev, ov534_reg_initdata[i][0],
308 ov534_reg_initdata[i][1]);
310 ov534_set_led(udev, 1);
312 /* Initialize sensor */
313 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
314 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
315 ov772x_reg_initdata[i][1]);
317 ov534_reg_write(udev, 0xe0, 0x09);
318 ov534_set_led(udev, 0);
321 /* this function is called at probe time */
322 static int sd_config(struct gspca_dev *gspca_dev,
323 const struct usb_device_id *id)
327 cam = &gspca_dev->cam;
330 cam->cam_mode = vga_mode;
331 cam->nmodes = ARRAY_SIZE(vga_mode);
333 cam->bulk_size = 16384;
339 /* this function is called at probe and resume time */
340 static int sd_init(struct gspca_dev *gspca_dev)
344 ov534_setup(gspca_dev->dev);
350 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
351 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
352 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
355 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
356 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
357 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
362 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
363 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
364 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
367 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
368 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
369 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
373 PDEBUG(D_PROBE, "frame_rate: %d", fr);
378 static int sd_start(struct gspca_dev *gspca_dev)
380 /* start streaming data */
381 ov534_set_led(gspca_dev->dev, 1);
382 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
387 static void sd_stopN(struct gspca_dev *gspca_dev)
389 /* stop streaming data */
390 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
391 ov534_set_led(gspca_dev->dev, 0);
394 /* Values for bmHeaderInfo (Video and Still Image Payload Headers, 2.4.3.3) */
395 #define UVC_STREAM_EOH (1 << 7)
396 #define UVC_STREAM_ERR (1 << 6)
397 #define UVC_STREAM_STI (1 << 5)
398 #define UVC_STREAM_RES (1 << 4)
399 #define UVC_STREAM_SCR (1 << 3)
400 #define UVC_STREAM_PTS (1 << 2)
401 #define UVC_STREAM_EOF (1 << 1)
402 #define UVC_STREAM_FID (1 << 0)
404 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
407 struct sd *sd = (struct sd *) gspca_dev;
410 int remaining_len = len;
411 __u8 *next_data = data;
414 if (remaining_len <= 0)
418 len = min(remaining_len, 2048);
419 remaining_len -= len;
422 /* Payloads are prefixed with a the UVC-style header. We
423 consider a frame to start when the FID toggles, or the PTS
424 changes. A frame ends when EOF is set, and we've received
425 the correct number of bytes. */
427 /* Verify UVC header. Header length is always 12 */
428 if (data[0] != 12 || len < 12) {
429 PDEBUG(D_PACK, "bad header");
434 if (data[1] & UVC_STREAM_ERR) {
435 PDEBUG(D_PACK, "payload error");
439 /* Extract PTS and FID */
440 if (!(data[1] & UVC_STREAM_PTS)) {
441 PDEBUG(D_PACK, "PTS not present");
444 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2];
445 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0;
447 /* If PTS or FID has changed, start a new frame. */
448 if (this_pts != sd->last_pts || this_fid != sd->last_fid) {
449 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, NULL, 0);
450 sd->last_pts = this_pts;
451 sd->last_fid = this_fid;
454 /* Add the data from this payload */
455 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
456 data + 12, len - 12);
458 /* If this packet is marked as EOF, end the frame */
459 if (data[1] & UVC_STREAM_EOF) {
462 if ((frame->data_end - frame->data) !=
463 (gspca_dev->width * gspca_dev->height * 2)) {
464 PDEBUG(D_PACK, "short frame");
468 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0);
471 /* Done this payload */
475 /* Discard data until a new frame starts. */
476 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0);
480 /* sub-driver description */
481 static const struct sd_desc sd_desc = {
484 .nctrls = ARRAY_SIZE(sd_ctrls),
489 .pkt_scan = sd_pkt_scan,
492 /* -- module initialisation -- */
493 static const __devinitdata struct usb_device_id device_table[] = {
494 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
495 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
496 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
500 MODULE_DEVICE_TABLE(usb, device_table);
502 /* -- device connect -- */
503 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
505 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
509 static struct usb_driver sd_driver = {
511 .id_table = device_table,
513 .disconnect = gspca_disconnect,
515 .suspend = gspca_suspend,
516 .resume = gspca_resume,
520 /* -- module insert / remove -- */
521 static int __init sd_mod_init(void)
523 if (usb_register(&sd_driver) < 0)
525 PDEBUG(D_PROBE, "registered");
529 static void __exit sd_mod_exit(void)
531 usb_deregister(&sd_driver);
532 PDEBUG(D_PROBE, "deregistered");
535 module_init(sd_mod_init);
536 module_exit(sd_mod_exit);
538 module_param(frame_rate, int, 0644);
539 MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");