ov534.c revision c06eb61941459b1981817fb675d0104ac0cd6df8
1/* 2 * ov534/ov772x gspca driver 3 * Copyright (C) 2008 Antonio Ospite <ospite@studenti.unina.it> 4 * Copyright (C) 2008 Jim Paris <jim@jtan.com> 5 * 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/ 9 * 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 13 * any later version. 14 * 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. 19 * 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 23 */ 24 25#define MODULE_NAME "ov534" 26 27#include "gspca.h" 28 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 35 36#define OV534_OP_WRITE_3 0x37 37#define OV534_OP_WRITE_2 0x33 38#define OV534_OP_READ_2 0xf9 39 40#define CTRL_TIMEOUT 500 41 42MODULE_AUTHOR("Antonio Ospite <ospite@studenti.unina.it>"); 43MODULE_DESCRIPTION("GSPCA/OV534 USB Camera Driver"); 44MODULE_LICENSE("GPL"); 45 46/* global parameters */ 47static int frame_rate; 48 49/* specific webcam descriptor */ 50struct sd { 51 struct gspca_dev gspca_dev; /* !! must be the first item */ 52 __u32 last_fid; 53 __u32 last_pts; 54}; 55 56/* V4L2 controls supported by the driver */ 57static struct ctrl sd_ctrls[] = { 58}; 59 60static 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, 65 .priv = 0}, 66}; 67 68static void ov534_reg_write(struct usb_device *udev, u16 reg, u8 val) 69{ 70 u8 data = val; 71 int ret; 72 73 PDEBUG(D_USBO, "reg=0x%04x, val=0%02x", reg, val); 74 ret = usb_control_msg(udev, 75 usb_sndctrlpipe(udev, 0), 76 0x1, 77 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 78 0x0, reg, &data, 1, CTRL_TIMEOUT); 79 if (ret < 0) 80 PDEBUG(D_ERR, "write failed"); 81} 82 83static u8 ov534_reg_read(struct usb_device *udev, u16 reg) 84{ 85 u8 data; 86 int ret; 87 88 ret = usb_control_msg(udev, 89 usb_rcvctrlpipe(udev, 0), 90 0x1, 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); 94 if (ret < 0) 95 PDEBUG(D_ERR, "read failed"); 96 return data; 97} 98 99/* Two bits control LED: 0x21 bit 7 and 0x23 bit 7. 100 * (direction and output)? */ 101static void ov534_set_led(struct usb_device *udev, int status) 102{ 103 u8 data; 104 105 PDEBUG(D_CONF, "led status: %d", status); 106 107 data = ov534_reg_read(udev, 0x21); 108 data |= 0x80; 109 ov534_reg_write(udev, 0x21, data); 110 111 data = ov534_reg_read(udev, 0x23); 112 if (status) 113 data |= 0x80; 114 else 115 data &= ~(0x80); 116 117 ov534_reg_write(udev, 0x23, data); 118} 119 120static int sccb_check_status(struct usb_device *udev) 121{ 122 u8 data; 123 int i; 124 125 for (i = 0; i < 5; i++) { 126 data = ov534_reg_read(udev, OV534_REG_STATUS); 127 128 switch (data) { 129 case 0x00: 130 return 1; 131 case 0x04: 132 return 0; 133 case 0x03: 134 break; 135 default: 136 PDEBUG(D_ERR, "sccb status 0x%02x, attempt %d/5\n", 137 data, i + 1); 138 } 139 } 140 return 0; 141} 142 143static void sccb_reg_write(struct usb_device *udev, u16 reg, u8 val) 144{ 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); 149 150 if (!sccb_check_status(udev)) 151 PDEBUG(D_ERR, "sccb_reg_write failed"); 152} 153 154static const __u8 ov534_reg_initdata[][2] = { 155 { 0xe7, 0x3a }, 156 157 { OV534_REG_ADDRESS, 0x42 }, /* select OV772x sensor */ 158 159 { 0xc2, 0x0c }, 160 { 0x88, 0xf8 }, 161 { 0xc3, 0x69 }, 162 { 0x89, 0xff }, 163 { 0x76, 0x03 }, 164 { 0x92, 0x01 }, 165 { 0x93, 0x18 }, 166 { 0x94, 0x10 }, 167 { 0x95, 0x10 }, 168 { 0xe2, 0x00 }, 169 { 0xe7, 0x3e }, 170 171 { 0x96, 0x00 }, 172 173 { 0x97, 0x20 }, 174 { 0x97, 0x20 }, 175 { 0x97, 0x20 }, 176 { 0x97, 0x0a }, 177 { 0x97, 0x3f }, 178 { 0x97, 0x4a }, 179 { 0x97, 0x20 }, 180 { 0x97, 0x15 }, 181 { 0x97, 0x0b }, 182 183 { 0x8e, 0x40 }, 184 { 0x1f, 0x81 }, 185 { 0x34, 0x05 }, 186 { 0xe3, 0x04 }, 187 { 0x88, 0x00 }, 188 { 0x89, 0x00 }, 189 { 0x76, 0x00 }, 190 { 0xe7, 0x2e }, 191 { 0x31, 0xf9 }, 192 { 0x25, 0x42 }, 193 { 0x21, 0xf0 }, 194 195 { 0x1c, 0x00 }, 196 { 0x1d, 0x40 }, 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 */ 202 203 { 0x1c, 0x0a }, 204 { 0x1d, 0x08 }, /* turn on UVC header */ 205 { 0x1d, 0x0e }, /* .. */ 206 207 { 0x8d, 0x1c }, 208 { 0x8e, 0x80 }, 209 { 0xe5, 0x04 }, 210 211 { 0xc0, 0x50 }, 212 { 0xc1, 0x3c }, 213 { 0xc2, 0x0c }, 214}; 215 216static const __u8 ov772x_reg_initdata[][2] = { 217 { 0x12, 0x80 }, 218 { 0x11, 0x01 }, 219 220 { 0x3d, 0x03 }, 221 { 0x17, 0x26 }, 222 { 0x18, 0xa0 }, 223 { 0x19, 0x07 }, 224 { 0x1a, 0xf0 }, 225 { 0x32, 0x00 }, 226 { 0x29, 0xa0 }, 227 { 0x2c, 0xf0 }, 228 { 0x65, 0x20 }, 229 { 0x11, 0x01 }, 230 { 0x42, 0x7f }, 231 { 0x63, 0xe0 }, 232 { 0x64, 0xff }, 233 { 0x66, 0x00 }, 234 { 0x13, 0xf0 }, 235 { 0x0d, 0x41 }, 236 { 0x0f, 0xc5 }, 237 { 0x14, 0x11 }, 238 239 { 0x22, 0x7f }, 240 { 0x23, 0x03 }, 241 { 0x24, 0x40 }, 242 { 0x25, 0x30 }, 243 { 0x26, 0xa1 }, 244 { 0x2a, 0x00 }, 245 { 0x2b, 0x00 }, 246 { 0x6b, 0xaa }, 247 { 0x13, 0xff }, 248 249 { 0x90, 0x05 }, 250 { 0x91, 0x01 }, 251 { 0x92, 0x03 }, 252 { 0x93, 0x00 }, 253 { 0x94, 0x60 }, 254 { 0x95, 0x3c }, 255 { 0x96, 0x24 }, 256 { 0x97, 0x1e }, 257 { 0x98, 0x62 }, 258 { 0x99, 0x80 }, 259 { 0x9a, 0x1e }, 260 { 0x9b, 0x08 }, 261 { 0x9c, 0x20 }, 262 { 0x9e, 0x81 }, 263 264 { 0xa6, 0x04 }, 265 { 0x7e, 0x0c }, 266 { 0x7f, 0x16 }, 267 { 0x80, 0x2a }, 268 { 0x81, 0x4e }, 269 { 0x82, 0x61 }, 270 { 0x83, 0x6f }, 271 { 0x84, 0x7b }, 272 { 0x85, 0x86 }, 273 { 0x86, 0x8e }, 274 { 0x87, 0x97 }, 275 { 0x88, 0xa4 }, 276 { 0x89, 0xaf }, 277 { 0x8a, 0xc5 }, 278 { 0x8b, 0xd7 }, 279 { 0x8c, 0xe8 }, 280 { 0x8d, 0x20 }, 281 282 { 0x0c, 0x90 }, 283 284 { 0x2b, 0x00 }, 285 { 0x22, 0x7f }, 286 { 0x23, 0x03 }, 287 { 0x11, 0x01 }, 288 { 0x0c, 0xd0 }, 289 { 0x64, 0xff }, 290 { 0x0d, 0x41 }, 291 292 { 0x14, 0x41 }, 293 { 0x0e, 0xcd }, 294 { 0xac, 0xbf }, 295 { 0x8e, 0x00 }, 296 { 0x0c, 0xd0 } 297}; 298 299 300/* setup method */ 301static void ov534_setup(struct usb_device *udev) 302{ 303 int i; 304 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]); 309 310 ov534_set_led(udev, 1); 311 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]); 316 317 ov534_reg_write(udev, 0xe0, 0x09); 318 ov534_set_led(udev, 0); 319} 320 321/* this function is called at probe time */ 322static int sd_config(struct gspca_dev *gspca_dev, 323 const struct usb_device_id *id) 324{ 325 struct cam *cam; 326 327 cam = &gspca_dev->cam; 328 329 cam->epaddr = 0x01; 330 cam->cam_mode = vga_mode; 331 cam->nmodes = ARRAY_SIZE(vga_mode); 332 333 cam->bulk_size = 16384; 334 cam->bulk_nurbs = 2; 335 336 return 0; 337} 338 339/* this function is called at probe and resume time */ 340static int sd_init(struct gspca_dev *gspca_dev) 341{ 342 int fr; 343 344 ov534_setup(gspca_dev->dev); 345 346 fr = frame_rate; 347 348 switch (fr) { 349 case 50: 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); 353 break; 354 case 40: 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); 358 break; 359/* case 30: */ 360 default: 361 fr = 30; 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); 365 break; 366 case 15: 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); 370 break; 371 } 372 373 PDEBUG(D_PROBE, "frame_rate: %d", fr); 374 375 return 0; 376} 377 378static int sd_start(struct gspca_dev *gspca_dev) 379{ 380 /* start streaming data */ 381 ov534_set_led(gspca_dev->dev, 1); 382 ov534_reg_write(gspca_dev->dev, 0xe0, 0x00); 383 384 return 0; 385} 386 387static void sd_stopN(struct gspca_dev *gspca_dev) 388{ 389 /* stop streaming data */ 390 ov534_reg_write(gspca_dev->dev, 0xe0, 0x09); 391 ov534_set_led(gspca_dev->dev, 0); 392} 393 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) 403 404static void sd_pkt_scan(struct gspca_dev *gspca_dev, struct gspca_frame *frame, 405 __u8 *data, int len) 406{ 407 struct sd *sd = (struct sd *) gspca_dev; 408 __u32 this_pts; 409 int this_fid; 410 int remaining_len = len; 411 __u8 *next_data = data; 412 413scan_next: 414 if (remaining_len <= 0) 415 return; 416 417 data = next_data; 418 len = min(remaining_len, 2048); 419 remaining_len -= len; 420 next_data += len; 421 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. */ 426 427 /* Verify UVC header. Header length is always 12 */ 428 if (data[0] != 12 || len < 12) { 429 PDEBUG(D_PACK, "bad header"); 430 goto discard; 431 } 432 433 /* Check errors */ 434 if (data[1] & UVC_STREAM_ERR) { 435 PDEBUG(D_PACK, "payload error"); 436 goto discard; 437 } 438 439 /* Extract PTS and FID */ 440 if (!(data[1] & UVC_STREAM_PTS)) { 441 PDEBUG(D_PACK, "PTS not present"); 442 goto discard; 443 } 444 this_pts = (data[5] << 24) | (data[4] << 16) | (data[3] << 8) | data[2]; 445 this_fid = (data[1] & UVC_STREAM_FID) ? 1 : 0; 446 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; 452 } 453 454 /* Add the data from this payload */ 455 gspca_frame_add(gspca_dev, INTER_PACKET, frame, 456 data + 12, len - 12); 457 458 /* If this packet is marked as EOF, end the frame */ 459 if (data[1] & UVC_STREAM_EOF) { 460 sd->last_pts = 0; 461 462 if ((frame->data_end - frame->data) != 463 (gspca_dev->width * gspca_dev->height * 2)) { 464 PDEBUG(D_PACK, "short frame"); 465 goto discard; 466 } 467 468 gspca_frame_add(gspca_dev, LAST_PACKET, frame, NULL, 0); 469 } 470 471 /* Done this payload */ 472 goto scan_next; 473 474discard: 475 /* Discard data until a new frame starts. */ 476 gspca_frame_add(gspca_dev, DISCARD_PACKET, frame, NULL, 0); 477 goto scan_next; 478} 479 480/* sub-driver description */ 481static const struct sd_desc sd_desc = { 482 .name = MODULE_NAME, 483 .ctrls = sd_ctrls, 484 .nctrls = ARRAY_SIZE(sd_ctrls), 485 .config = sd_config, 486 .init = sd_init, 487 .start = sd_start, 488 .stopN = sd_stopN, 489 .pkt_scan = sd_pkt_scan, 490}; 491 492/* -- module initialisation -- */ 493static 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) */ 497 {} 498}; 499 500MODULE_DEVICE_TABLE(usb, device_table); 501 502/* -- device connect -- */ 503static int sd_probe(struct usb_interface *intf, const struct usb_device_id *id) 504{ 505 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), 506 THIS_MODULE); 507} 508 509static struct usb_driver sd_driver = { 510 .name = MODULE_NAME, 511 .id_table = device_table, 512 .probe = sd_probe, 513 .disconnect = gspca_disconnect, 514#ifdef CONFIG_PM 515 .suspend = gspca_suspend, 516 .resume = gspca_resume, 517#endif 518}; 519 520/* -- module insert / remove -- */ 521static int __init sd_mod_init(void) 522{ 523 if (usb_register(&sd_driver) < 0) 524 return -1; 525 PDEBUG(D_PROBE, "registered"); 526 return 0; 527} 528 529static void __exit sd_mod_exit(void) 530{ 531 usb_deregister(&sd_driver); 532 PDEBUG(D_PROBE, "deregistered"); 533} 534 535module_init(sd_mod_init); 536module_exit(sd_mod_exit); 537 538module_param(frame_rate, int, 0644); 539MODULE_PARM_DESC(frame_rate, "Frame rate (15, 30, 40, 50)"); 540