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 */
53 /* V4L2 controls supported by the driver */
54 static struct ctrl sd_ctrls[] = {
57 static struct v4l2_pix_format vga_mode[] = {
58 {640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
59 .bytesperline = 640 * 2,
60 .sizeimage = 640 * 480 * 2,
61 .colorspace = V4L2_COLORSPACE_JPEG,
65 static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val)
70 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val);
71 ret = usb_control_msg(udev,
72 usb_sndctrlpipe(udev, 0),
74 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
75 0x0, reg, &data, 1, CTRL_TIMEOUT);
77 PDEBUG(D_ERR, "write failed");
80 static u8 ov534_reg_read(struct usb_device *udev, u16 reg)
85 ret = usb_control_msg(udev,
86 usb_rcvctrlpipe(udev, 0),
88 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
89 0x0, reg, &data, 1, CTRL_TIMEOUT);
90 PDEBUG(D_USBI, "reg=0x%04x, data=0x%02x", reg, data);
92 PDEBUG(D_ERR, "read failed");
96 /* Two bits control LED: 0x21 bit 7 and 0x23 bit 7.
97 * (direction and output)? */
98 static void ov534_set_led(struct usb_device *udev, int status)
102 PDEBUG(D_CONF, "led status: %d", status);
104 data = ov534_reg_read(udev, 0x21);
106 ov534_reg_write(udev, 0x21, data);
108 data = ov534_reg_read(udev, 0x23);
114 ov534_reg_write(udev, 0x23, data);
117 static int sccb_check_status(struct usb_device *udev)
122 for (i = 0; i < 5; i++) {
123 data = ov534_reg_read(udev, OV534_REG_STATUS);
133 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n",
140 static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val)
142 PDEBUG(D_USBO, "reg: 0x%04x, val: 0x%02x", reg, val);
143 ov534_reg_write(udev, OV534_REG_SUBADDR, reg);
144 ov534_reg_write(udev, OV534_REG_WRITE, val);
145 ov534_reg_write(udev, OV534_REG_OPERATION, OV534_OP_WRITE_3);
147 if (!sccb_check_status(udev))
148 PDEBUG(D_ERR, "sccb_reg_write failed");
151 static const __u8 ov534_reg_initdata[][2] = {
154 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */
200 { 0x1d, 0x02 }, /* frame size 0x025800 * 4 = 614400 */
201 { 0x1d, 0x58 }, /* frame size */
202 { 0x1d, 0x00 }, /* frame size */
213 static const __u8 ov772x_reg_initdata[][2] = {
298 static void ov534_setup(struct usb_device *udev)
302 /* Initialize bridge chip */
303 for (i = 0; i < ARRAY_SIZE(ov534_reg_initdata); i++)
304 ov534_reg_write(udev, ov534_reg_initdata[i][0],
305 ov534_reg_initdata[i][1]);
307 ov534_set_led(udev, 1);
309 /* Initialize sensor */
310 for (i = 0; i < ARRAY_SIZE(ov772x_reg_initdata); i++)
311 sccb_reg_write(udev, ov772x_reg_initdata[i][0],
312 ov772x_reg_initdata[i][1]);
314 ov534_reg_write(udev, 0xe0, 0x09);
315 ov534_set_led(udev, 0);
318 /* this function is called at probe time */
319 static int sd_config(struct gspca_dev *gspca_dev,
320 const struct usb_device_id *id)
324 cam = &gspca_dev->cam;
327 cam->cam_mode = vga_mode;
328 cam->nmodes = ARRAY_SIZE(vga_mode);
331 * On some architectures we need contiguous memory for urb buffers, and
332 * in low memory situation 'sizeimage' can be too much.
333 * 16kiB chunks should be available even when we are low in memory.
334 * TODO: CHECK this description: is the problem arch-dependent or more
337 cam->bulk_size = 16 * 1024;
340 PDEBUG(D_PROBE, "bulk_size = %d", cam->bulk_size);
345 /* this function is called at probe and resume time */
346 static int sd_init(struct gspca_dev *gspca_dev)
350 ov534_setup(gspca_dev->dev);
356 sccb_reg_write(gspca_dev->dev, 0x11, 0x01);
357 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
358 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
361 sccb_reg_write(gspca_dev->dev, 0x11, 0x02);
362 sccb_reg_write(gspca_dev->dev, 0x0d, 0xc1);
363 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
368 sccb_reg_write(gspca_dev->dev, 0x11, 0x04);
369 sccb_reg_write(gspca_dev->dev, 0x0d, 0x81);
370 ov534_reg_write(gspca_dev->dev, 0xe5, 0x02);
373 sccb_reg_write(gspca_dev->dev, 0x11, 0x03);
374 sccb_reg_write(gspca_dev->dev, 0x0d, 0x41);
375 ov534_reg_write(gspca_dev->dev, 0xe5, 0x04);
379 PDEBUG(D_PROBE, "frame_rate: %d", fr);
384 static int sd_start(struct gspca_dev *gspca_dev)
386 struct gspca_frame *frame;
388 /* start streaming data */
389 ov534_set_led(gspca_dev->dev, 1);
390 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00);
392 frame = gspca_get_i_frame(gspca_dev);
394 PDEBUG(D_ERR, "NULL frame!");
397 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, gspca_dev->usb_buf, 0);
402 static void sd_stopN(struct gspca_dev *gspca_dev)
404 /* stop streaming data */
405 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09);
406 ov534_set_led(gspca_dev->dev, 0);
409 static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame,
412 int framesize = frame->v4l2_buf.length;
414 if (len == framesize) {
415 frame = gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
417 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
419 PDEBUG(D_PACK, "packet len = %d, framesize = %d", len,
423 /* sub-driver description */
424 static const struct sd_desc sd_desc = {
427 .nctrls = ARRAY_SIZE(sd_ctrls),
432 .pkt_scan = sd_pkt_scan,
435 /* -- module initialisation -- */
436 static const __devinitdata struct usb_device_id device_table[] = {
437 {USB_DEVICE(0x06f8, 0x3002)}, /* Hercules Blog Webcam */
438 {USB_DEVICE(0x06f8, 0x3003)}, /* Hercules Dualpix HD Weblog */
439 {USB_DEVICE(0x1415, 0x2000)}, /* Sony HD Eye for PS3 (SLEH 00201) */
443 MODULE_DEVICE_TABLE(usb, device_table);
445 /* -- device connect -- */
446 static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id)
448 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
452 static struct usb_driver sd_driver = {
454 .id_table = device_table,
456 .disconnect = gspca_disconnect,
458 .suspend = gspca_suspend,
459 .resume = gspca_resume,
463 /* -- module insert / remove -- */
464 static int __init sd_mod_init(void)
466 if (usb_register(&sd_driver) < 0)
468 PDEBUG(D_PROBE, "registered");
472 static void __exit sd_mod_exit(void)
474 usb_deregister(&sd_driver);
475 PDEBUG(D_PROBE, "deregistered");
478 module_init(sd_mod_init);
479 module_exit(sd_mod_exit);
481 module_param(frame_rate, int, 0644);
482 MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)");