1 /* 2 * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher 3 * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland 4 * Copyright (c) 2002, 2003 Tuukka Toivonen 5 * Copyright (c) 2008 Erik Andrén 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * P/N 861037: Sensor HDCS1000 ASIC STV0600 18 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600 19 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express 20 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam 21 * P/N 861075-0040: Sensor HDCS1000 ASIC 22 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB 23 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web 24 */ 25 26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 27 28 #include <linux/input.h> 29 #include "stv06xx_sensor.h" 30 31 MODULE_AUTHOR("Erik Andrén"); 32 MODULE_DESCRIPTION("STV06XX USB Camera Driver"); 33 MODULE_LICENSE("GPL"); 34 35 static bool dump_bridge; 36 static bool dump_sensor; 37 38 int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data) 39 { 40 int err; 41 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 42 struct usb_device *udev = sd->gspca_dev.dev; 43 __u8 *buf = sd->gspca_dev.usb_buf; 44 45 u8 len = (i2c_data > 0xff) ? 2 : 1; 46 47 buf[0] = i2c_data & 0xff; 48 buf[1] = (i2c_data >> 8) & 0xff; 49 50 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 51 0x04, 0x40, address, 0, buf, len, 52 STV06XX_URB_MSG_TIMEOUT); 53 54 PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d", 55 i2c_data, address, err); 56 57 return (err < 0) ? err : 0; 58 } 59 60 int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data) 61 { 62 int err; 63 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 64 struct usb_device *udev = sd->gspca_dev.dev; 65 __u8 *buf = sd->gspca_dev.usb_buf; 66 67 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 68 0x04, 0xc0, address, 0, buf, 1, 69 STV06XX_URB_MSG_TIMEOUT); 70 71 *i2c_data = buf[0]; 72 73 PDEBUG(D_CONF, "Reading 0x%x from address 0x%x, status %d", 74 *i2c_data, address, err); 75 76 return (err < 0) ? err : 0; 77 } 78 79 /* Wraps the normal write sensor bytes / words functions for writing a 80 single value */ 81 int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value) 82 { 83 if (sd->sensor->i2c_len == 2) { 84 u16 data[2] = { address, value }; 85 return stv06xx_write_sensor_words(sd, data, 1); 86 } else { 87 u8 data[2] = { address, value }; 88 return stv06xx_write_sensor_bytes(sd, data, 1); 89 } 90 } 91 92 static int stv06xx_write_sensor_finish(struct sd *sd) 93 { 94 int err = 0; 95 96 if (sd->bridge == BRIDGE_STV610) { 97 struct usb_device *udev = sd->gspca_dev.dev; 98 __u8 *buf = sd->gspca_dev.usb_buf; 99 100 buf[0] = 0; 101 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 102 0x04, 0x40, 0x1704, 0, buf, 1, 103 STV06XX_URB_MSG_TIMEOUT); 104 } 105 106 return (err < 0) ? err : 0; 107 } 108 109 int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len) 110 { 111 int err, i, j; 112 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 113 struct usb_device *udev = sd->gspca_dev.dev; 114 __u8 *buf = sd->gspca_dev.usb_buf; 115 116 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len); 117 for (i = 0; i < len;) { 118 /* Build the command buffer */ 119 memset(buf, 0, I2C_BUFFER_LENGTH); 120 for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) { 121 buf[j] = data[2*i]; 122 buf[0x10 + j] = data[2*i+1]; 123 PDEBUG(D_CONF, "I2C: Writing 0x%02x to reg 0x%02x", 124 data[2*i+1], data[2*i]); 125 } 126 buf[0x20] = sd->sensor->i2c_addr; 127 buf[0x21] = j - 1; /* Number of commands to send - 1 */ 128 buf[0x22] = I2C_WRITE_CMD; 129 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 130 0x04, 0x40, 0x0400, 0, buf, 131 I2C_BUFFER_LENGTH, 132 STV06XX_URB_MSG_TIMEOUT); 133 if (err < 0) 134 return err; 135 } 136 return stv06xx_write_sensor_finish(sd); 137 } 138 139 int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len) 140 { 141 int err, i, j; 142 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 143 struct usb_device *udev = sd->gspca_dev.dev; 144 __u8 *buf = sd->gspca_dev.usb_buf; 145 146 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len); 147 148 for (i = 0; i < len;) { 149 /* Build the command buffer */ 150 memset(buf, 0, I2C_BUFFER_LENGTH); 151 for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) { 152 buf[j] = data[2*i]; 153 buf[0x10 + j * 2] = data[2*i+1]; 154 buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8; 155 PDEBUG(D_CONF, "I2C: Writing 0x%04x to reg 0x%02x", 156 data[2*i+1], data[2*i]); 157 } 158 buf[0x20] = sd->sensor->i2c_addr; 159 buf[0x21] = j - 1; /* Number of commands to send - 1 */ 160 buf[0x22] = I2C_WRITE_CMD; 161 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 162 0x04, 0x40, 0x0400, 0, buf, 163 I2C_BUFFER_LENGTH, 164 STV06XX_URB_MSG_TIMEOUT); 165 if (err < 0) 166 return err; 167 } 168 return stv06xx_write_sensor_finish(sd); 169 } 170 171 int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value) 172 { 173 int err; 174 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd; 175 struct usb_device *udev = sd->gspca_dev.dev; 176 __u8 *buf = sd->gspca_dev.usb_buf; 177 178 err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush); 179 if (err < 0) 180 return err; 181 182 /* Clear mem */ 183 memset(buf, 0, I2C_BUFFER_LENGTH); 184 185 buf[0] = address; 186 buf[0x20] = sd->sensor->i2c_addr; 187 buf[0x21] = 0; 188 189 /* Read I2C register */ 190 buf[0x22] = I2C_READ_CMD; 191 192 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 193 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH, 194 STV06XX_URB_MSG_TIMEOUT); 195 if (err < 0) { 196 pr_err("I2C: Read error writing address: %d\n", err); 197 return err; 198 } 199 200 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 201 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len, 202 STV06XX_URB_MSG_TIMEOUT); 203 if (sd->sensor->i2c_len == 2) 204 *value = buf[0] | (buf[1] << 8); 205 else 206 *value = buf[0]; 207 208 PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d", 209 *value, address, err); 210 211 return (err < 0) ? err : 0; 212 } 213 214 /* Dumps all bridge registers */ 215 static void stv06xx_dump_bridge(struct sd *sd) 216 { 217 int i; 218 u8 data, buf; 219 220 pr_info("Dumping all stv06xx bridge registers\n"); 221 for (i = 0x1400; i < 0x160f; i++) { 222 stv06xx_read_bridge(sd, i, &data); 223 224 pr_info("Read 0x%x from address 0x%x\n", data, i); 225 } 226 227 pr_info("Testing stv06xx bridge registers for writability\n"); 228 for (i = 0x1400; i < 0x160f; i++) { 229 stv06xx_read_bridge(sd, i, &data); 230 buf = data; 231 232 stv06xx_write_bridge(sd, i, 0xff); 233 stv06xx_read_bridge(sd, i, &data); 234 if (data == 0xff) 235 pr_info("Register 0x%x is read/write\n", i); 236 else if (data != buf) 237 pr_info("Register 0x%x is read/write, but only partially\n", 238 i); 239 else 240 pr_info("Register 0x%x is read-only\n", i); 241 242 stv06xx_write_bridge(sd, i, buf); 243 } 244 } 245 246 /* this function is called at probe and resume time */ 247 static int stv06xx_init(struct gspca_dev *gspca_dev) 248 { 249 struct sd *sd = (struct sd *) gspca_dev; 250 int err; 251 252 PDEBUG(D_PROBE, "Initializing camera"); 253 254 /* Let the usb init settle for a bit 255 before performing the initialization */ 256 msleep(250); 257 258 err = sd->sensor->init(sd); 259 260 if (dump_sensor && sd->sensor->dump) 261 sd->sensor->dump(sd); 262 263 return (err < 0) ? err : 0; 264 } 265 266 /* this function is called at probe time */ 267 static int stv06xx_init_controls(struct gspca_dev *gspca_dev) 268 { 269 struct sd *sd = (struct sd *) gspca_dev; 270 271 PDEBUG(D_PROBE, "Initializing controls"); 272 273 gspca_dev->vdev.ctrl_handler = &gspca_dev->ctrl_handler; 274 return sd->sensor->init_controls(sd); 275 } 276 277 /* Start the camera */ 278 static int stv06xx_start(struct gspca_dev *gspca_dev) 279 { 280 struct sd *sd = (struct sd *) gspca_dev; 281 struct usb_host_interface *alt; 282 struct usb_interface *intf; 283 int err, packet_size; 284 285 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface); 286 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt); 287 if (!alt) { 288 PERR("Couldn't get altsetting"); 289 return -EIO; 290 } 291 292 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize); 293 err = stv06xx_write_bridge(sd, STV_ISO_SIZE_L, packet_size); 294 if (err < 0) 295 return err; 296 297 /* Prepare the sensor for start */ 298 err = sd->sensor->start(sd); 299 if (err < 0) 300 goto out; 301 302 /* Start isochronous streaming */ 303 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1); 304 305 out: 306 if (err < 0) 307 PDEBUG(D_STREAM, "Starting stream failed"); 308 else 309 PDEBUG(D_STREAM, "Started streaming"); 310 311 return (err < 0) ? err : 0; 312 } 313 314 static int stv06xx_isoc_init(struct gspca_dev *gspca_dev) 315 { 316 struct usb_host_interface *alt; 317 struct sd *sd = (struct sd *) gspca_dev; 318 319 /* Start isoc bandwidth "negotiation" at max isoc bandwidth */ 320 alt = &gspca_dev->dev->actconfig->intf_cache[0]->altsetting[1]; 321 alt->endpoint[0].desc.wMaxPacketSize = 322 cpu_to_le16(sd->sensor->max_packet_size[gspca_dev->curr_mode]); 323 324 return 0; 325 } 326 327 static int stv06xx_isoc_nego(struct gspca_dev *gspca_dev) 328 { 329 int ret, packet_size, min_packet_size; 330 struct usb_host_interface *alt; 331 struct sd *sd = (struct sd *) gspca_dev; 332 333 alt = &gspca_dev->dev->actconfig->intf_cache[0]->altsetting[1]; 334 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize); 335 min_packet_size = sd->sensor->min_packet_size[gspca_dev->curr_mode]; 336 if (packet_size <= min_packet_size) 337 return -EIO; 338 339 packet_size -= 100; 340 if (packet_size < min_packet_size) 341 packet_size = min_packet_size; 342 alt->endpoint[0].desc.wMaxPacketSize = cpu_to_le16(packet_size); 343 344 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1); 345 if (ret < 0) 346 PERR("set alt 1 err %d", ret); 347 348 return ret; 349 } 350 351 static void stv06xx_stopN(struct gspca_dev *gspca_dev) 352 { 353 int err; 354 struct sd *sd = (struct sd *) gspca_dev; 355 356 /* stop ISO-streaming */ 357 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0); 358 if (err < 0) 359 goto out; 360 361 err = sd->sensor->stop(sd); 362 363 out: 364 if (err < 0) 365 PDEBUG(D_STREAM, "Failed to stop stream"); 366 else 367 PDEBUG(D_STREAM, "Stopped streaming"); 368 } 369 370 /* 371 * Analyse an USB packet of the data stream and store it appropriately. 372 * Each packet contains an integral number of chunks. Each chunk has 373 * 2-bytes identification, followed by 2-bytes that describe the chunk 374 * length. Known/guessed chunk identifications are: 375 * 8001/8005/C001/C005 - Begin new frame 376 * 8002/8006/C002/C006 - End frame 377 * 0200/4200 - Contains actual image data, bayer or compressed 378 * 0005 - 11 bytes of unknown data 379 * 0100 - 2 bytes of unknown data 380 * The 0005 and 0100 chunks seem to appear only in compressed stream. 381 */ 382 static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev, 383 u8 *data, /* isoc packet */ 384 int len) /* iso packet length */ 385 { 386 struct sd *sd = (struct sd *) gspca_dev; 387 388 PDEBUG(D_PACK, "Packet of length %d arrived", len); 389 390 /* A packet may contain several frames 391 loop until the whole packet is reached */ 392 while (len) { 393 int id, chunk_len; 394 395 if (len < 4) { 396 PDEBUG(D_PACK, "Packet is smaller than 4 bytes"); 397 return; 398 } 399 400 /* Capture the id */ 401 id = (data[0] << 8) | data[1]; 402 403 /* Capture the chunk length */ 404 chunk_len = (data[2] << 8) | data[3]; 405 PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len); 406 407 data += 4; 408 len -= 4; 409 410 if (len < chunk_len) { 411 PERR("URB packet length is smaller than the specified chunk length"); 412 gspca_dev->last_packet_type = DISCARD_PACKET; 413 return; 414 } 415 416 /* First byte seem to be 02=data 2nd byte is unknown??? */ 417 if (sd->bridge == BRIDGE_ST6422 && (id & 0xff00) == 0x0200) 418 goto frame_data; 419 420 switch (id) { 421 case 0x0200: 422 case 0x4200: 423 frame_data: 424 PDEBUG(D_PACK, "Frame data packet detected"); 425 426 if (sd->to_skip) { 427 int skip = (sd->to_skip < chunk_len) ? 428 sd->to_skip : chunk_len; 429 data += skip; 430 len -= skip; 431 chunk_len -= skip; 432 sd->to_skip -= skip; 433 } 434 435 gspca_frame_add(gspca_dev, INTER_PACKET, 436 data, chunk_len); 437 break; 438 439 case 0x8001: 440 case 0x8005: 441 case 0xc001: 442 case 0xc005: 443 PDEBUG(D_PACK, "Starting new frame"); 444 445 /* Create a new frame, chunk length should be zero */ 446 gspca_frame_add(gspca_dev, FIRST_PACKET, 447 NULL, 0); 448 449 if (sd->bridge == BRIDGE_ST6422) 450 sd->to_skip = gspca_dev->pixfmt.width * 4; 451 452 if (chunk_len) 453 PERR("Chunk length is non-zero on a SOF"); 454 break; 455 456 case 0x8002: 457 case 0x8006: 458 case 0xc002: 459 PDEBUG(D_PACK, "End of frame detected"); 460 461 /* Complete the last frame (if any) */ 462 gspca_frame_add(gspca_dev, LAST_PACKET, 463 NULL, 0); 464 465 if (chunk_len) 466 PERR("Chunk length is non-zero on a EOF"); 467 break; 468 469 case 0x0005: 470 PDEBUG(D_PACK, "Chunk 0x005 detected"); 471 /* Unknown chunk with 11 bytes of data, 472 occurs just before end of each frame 473 in compressed mode */ 474 break; 475 476 case 0x0100: 477 PDEBUG(D_PACK, "Chunk 0x0100 detected"); 478 /* Unknown chunk with 2 bytes of data, 479 occurs 2-3 times per USB interrupt */ 480 break; 481 case 0x42ff: 482 PDEBUG(D_PACK, "Chunk 0x42ff detected"); 483 /* Special chunk seen sometimes on the ST6422 */ 484 break; 485 default: 486 PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id); 487 /* Unknown chunk */ 488 } 489 data += chunk_len; 490 len -= chunk_len; 491 } 492 } 493 494 #if IS_ENABLED(CONFIG_INPUT) 495 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev, 496 u8 *data, /* interrupt packet data */ 497 int len) /* interrupt packet length */ 498 { 499 int ret = -EINVAL; 500 501 if (len == 1 && (data[0] == 0x80 || data[0] == 0x10)) { 502 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1); 503 input_sync(gspca_dev->input_dev); 504 ret = 0; 505 } 506 507 if (len == 1 && (data[0] == 0x88 || data[0] == 0x11)) { 508 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0); 509 input_sync(gspca_dev->input_dev); 510 ret = 0; 511 } 512 513 return ret; 514 } 515 #endif 516 517 static int stv06xx_config(struct gspca_dev *gspca_dev, 518 const struct usb_device_id *id); 519 520 /* sub-driver description */ 521 static const struct sd_desc sd_desc = { 522 .name = MODULE_NAME, 523 .config = stv06xx_config, 524 .init = stv06xx_init, 525 .init_controls = stv06xx_init_controls, 526 .start = stv06xx_start, 527 .stopN = stv06xx_stopN, 528 .pkt_scan = stv06xx_pkt_scan, 529 .isoc_init = stv06xx_isoc_init, 530 .isoc_nego = stv06xx_isoc_nego, 531 #if IS_ENABLED(CONFIG_INPUT) 532 .int_pkt_scan = sd_int_pkt_scan, 533 #endif 534 }; 535 536 /* This function is called at probe time */ 537 static int stv06xx_config(struct gspca_dev *gspca_dev, 538 const struct usb_device_id *id) 539 { 540 struct sd *sd = (struct sd *) gspca_dev; 541 542 PDEBUG(D_PROBE, "Configuring camera"); 543 544 sd->bridge = id->driver_info; 545 gspca_dev->sd_desc = &sd_desc; 546 547 if (dump_bridge) 548 stv06xx_dump_bridge(sd); 549 550 sd->sensor = &stv06xx_sensor_st6422; 551 if (!sd->sensor->probe(sd)) 552 return 0; 553 554 sd->sensor = &stv06xx_sensor_vv6410; 555 if (!sd->sensor->probe(sd)) 556 return 0; 557 558 sd->sensor = &stv06xx_sensor_hdcs1x00; 559 if (!sd->sensor->probe(sd)) 560 return 0; 561 562 sd->sensor = &stv06xx_sensor_hdcs1020; 563 if (!sd->sensor->probe(sd)) 564 return 0; 565 566 sd->sensor = &stv06xx_sensor_pb0100; 567 if (!sd->sensor->probe(sd)) 568 return 0; 569 570 sd->sensor = NULL; 571 return -ENODEV; 572 } 573 574 575 576 /* -- module initialisation -- */ 577 static const struct usb_device_id device_table[] = { 578 {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 }, /* QuickCam Express */ 579 {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 }, /* LEGO cam / QuickCam Web */ 580 {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 }, /* Dexxa WebCam USB */ 581 {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 }, /* QuickCam Messenger */ 582 {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 }, /* QuickCam Communicate */ 583 {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 }, /* QuickCam Messenger (new) */ 584 {} 585 }; 586 MODULE_DEVICE_TABLE(usb, device_table); 587 588 /* -- device connect -- */ 589 static int sd_probe(struct usb_interface *intf, 590 const struct usb_device_id *id) 591 { 592 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), 593 THIS_MODULE); 594 } 595 596 static void sd_disconnect(struct usb_interface *intf) 597 { 598 struct gspca_dev *gspca_dev = usb_get_intfdata(intf); 599 struct sd *sd = (struct sd *) gspca_dev; 600 void *priv = sd->sensor_priv; 601 PDEBUG(D_PROBE, "Disconnecting the stv06xx device"); 602 603 sd->sensor = NULL; 604 gspca_disconnect(intf); 605 kfree(priv); 606 } 607 608 static struct usb_driver sd_driver = { 609 .name = MODULE_NAME, 610 .id_table = device_table, 611 .probe = sd_probe, 612 .disconnect = sd_disconnect, 613 #ifdef CONFIG_PM 614 .suspend = gspca_suspend, 615 .resume = gspca_resume, 616 .reset_resume = gspca_resume, 617 #endif 618 }; 619 620 module_usb_driver(sd_driver); 621 622 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR); 623 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup"); 624 625 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR); 626 MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup"); 627