Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / media / video / gspca / stv06xx / stv06xx.c
blobabf1658fa33e9e4ca175e2f69a56ed9651d3e1ec
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
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * P/N 861037: Sensor HDCS1000 ASIC STV0600
22 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
23 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
24 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
25 * P/N 861075-0040: Sensor HDCS1000 ASIC
26 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
27 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
30 #include <linux/input.h>
31 #include "stv06xx_sensor.h"
33 MODULE_AUTHOR("Erik Andrén");
34 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
35 MODULE_LICENSE("GPL");
37 static int dump_bridge;
38 static int dump_sensor;
40 int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data)
42 int err;
43 struct usb_device *udev = sd->gspca_dev.dev;
44 __u8 *buf = sd->gspca_dev.usb_buf;
45 u8 len = (i2c_data > 0xff) ? 2 : 1;
47 buf[0] = i2c_data & 0xff;
48 buf[1] = (i2c_data >> 8) & 0xff;
50 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
51 0x04, 0x40, address, 0, buf, len,
52 STV06XX_URB_MSG_TIMEOUT);
54 PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d",
55 i2c_data, address, err);
57 return (err < 0) ? err : 0;
60 int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data)
62 int err;
63 struct usb_device *udev = sd->gspca_dev.dev;
64 __u8 *buf = sd->gspca_dev.usb_buf;
66 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
67 0x04, 0xc0, address, 0, buf, 1,
68 STV06XX_URB_MSG_TIMEOUT);
70 *i2c_data = buf[0];
72 PDEBUG(D_CONF, "Reading 0x%x from address 0x%x, status %d",
73 *i2c_data, address, err);
75 return (err < 0) ? err : 0;
78 /* Wraps the normal write sensor bytes / words functions for writing a
79 single value */
80 int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value)
82 if (sd->sensor->i2c_len == 2) {
83 u16 data[2] = { address, value };
84 return stv06xx_write_sensor_words(sd, data, 1);
85 } else {
86 u8 data[2] = { address, value };
87 return stv06xx_write_sensor_bytes(sd, data, 1);
91 static int stv06xx_write_sensor_finish(struct sd *sd)
93 int err = 0;
95 if (sd->bridge == BRIDGE_STV610) {
96 struct usb_device *udev = sd->gspca_dev.dev;
97 __u8 *buf = sd->gspca_dev.usb_buf;
99 buf[0] = 0;
100 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
101 0x04, 0x40, 0x1704, 0, buf, 1,
102 STV06XX_URB_MSG_TIMEOUT);
105 return (err < 0) ? err : 0;
108 int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
110 int err, i, j;
111 struct usb_device *udev = sd->gspca_dev.dev;
112 __u8 *buf = sd->gspca_dev.usb_buf;
114 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
115 for (i = 0; i < len;) {
116 /* Build the command buffer */
117 memset(buf, 0, I2C_BUFFER_LENGTH);
118 for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) {
119 buf[j] = data[2*i];
120 buf[0x10 + j] = data[2*i+1];
121 PDEBUG(D_CONF, "I2C: Writing 0x%02x to reg 0x%02x",
122 data[2*i+1], data[2*i]);
124 buf[0x20] = sd->sensor->i2c_addr;
125 buf[0x21] = j - 1; /* Number of commands to send - 1 */
126 buf[0x22] = I2C_WRITE_CMD;
127 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
128 0x04, 0x40, 0x0400, 0, buf,
129 I2C_BUFFER_LENGTH,
130 STV06XX_URB_MSG_TIMEOUT);
131 if (err < 0)
132 return err;
134 return stv06xx_write_sensor_finish(sd);
137 int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
139 int err, i, j;
140 struct usb_device *udev = sd->gspca_dev.dev;
141 __u8 *buf = sd->gspca_dev.usb_buf;
143 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
145 for (i = 0; i < len;) {
146 /* Build the command buffer */
147 memset(buf, 0, I2C_BUFFER_LENGTH);
148 for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) {
149 buf[j] = data[2*i];
150 buf[0x10 + j * 2] = data[2*i+1];
151 buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8;
152 PDEBUG(D_CONF, "I2C: Writing 0x%04x to reg 0x%02x",
153 data[2*i+1], data[2*i]);
155 buf[0x20] = sd->sensor->i2c_addr;
156 buf[0x21] = j - 1; /* Number of commands to send - 1 */
157 buf[0x22] = I2C_WRITE_CMD;
158 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
159 0x04, 0x40, 0x0400, 0, buf,
160 I2C_BUFFER_LENGTH,
161 STV06XX_URB_MSG_TIMEOUT);
162 if (err < 0)
163 return err;
165 return stv06xx_write_sensor_finish(sd);
168 int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
170 int err;
171 struct usb_device *udev = sd->gspca_dev.dev;
172 __u8 *buf = sd->gspca_dev.usb_buf;
174 err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
175 if (err < 0)
176 return err;
178 /* Clear mem */
179 memset(buf, 0, I2C_BUFFER_LENGTH);
181 buf[0] = address;
182 buf[0x20] = sd->sensor->i2c_addr;
183 buf[0x21] = 0;
185 /* Read I2C register */
186 buf[0x22] = I2C_READ_CMD;
188 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
189 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
190 STV06XX_URB_MSG_TIMEOUT);
191 if (err < 0) {
192 err("I2C: Read error writing address: %d", err);
193 return err;
196 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
197 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len,
198 STV06XX_URB_MSG_TIMEOUT);
199 if (sd->sensor->i2c_len == 2)
200 *value = buf[0] | (buf[1] << 8);
201 else
202 *value = buf[0];
204 PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d",
205 *value, address, err);
207 return (err < 0) ? err : 0;
210 /* Dumps all bridge registers */
211 static void stv06xx_dump_bridge(struct sd *sd)
213 int i;
214 u8 data, buf;
216 info("Dumping all stv06xx bridge registers");
217 for (i = 0x1400; i < 0x160f; i++) {
218 stv06xx_read_bridge(sd, i, &data);
220 info("Read 0x%x from address 0x%x", data, i);
223 info("Testing stv06xx bridge registers for writability");
224 for (i = 0x1400; i < 0x160f; i++) {
225 stv06xx_read_bridge(sd, i, &data);
226 buf = data;
228 stv06xx_write_bridge(sd, i, 0xff);
229 stv06xx_read_bridge(sd, i, &data);
230 if (data == 0xff)
231 info("Register 0x%x is read/write", i);
232 else if (data != buf)
233 info("Register 0x%x is read/write,"
234 " but only partially", i);
235 else
236 info("Register 0x%x is read-only", i);
238 stv06xx_write_bridge(sd, i, buf);
242 /* this function is called at probe and resume time */
243 static int stv06xx_init(struct gspca_dev *gspca_dev)
245 struct sd *sd = (struct sd *) gspca_dev;
246 int err;
248 PDEBUG(D_PROBE, "Initializing camera");
250 /* Let the usb init settle for a bit
251 before performing the initialization */
252 msleep(250);
254 err = sd->sensor->init(sd);
256 if (dump_sensor && sd->sensor->dump)
257 sd->sensor->dump(sd);
259 return (err < 0) ? err : 0;
262 /* Start the camera */
263 static int stv06xx_start(struct gspca_dev *gspca_dev)
265 struct sd *sd = (struct sd *) gspca_dev;
266 struct usb_host_interface *alt;
267 struct usb_interface *intf;
268 int err, packet_size;
270 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
271 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
272 if (!alt) {
273 PDEBUG(D_ERR, "Couldn't get altsetting");
274 return -EIO;
277 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
278 err = stv06xx_write_bridge(sd, STV_ISO_SIZE_L, packet_size);
279 if (err < 0)
280 return err;
282 /* Prepare the sensor for start */
283 err = sd->sensor->start(sd);
284 if (err < 0)
285 goto out;
287 /* Start isochronous streaming */
288 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
290 out:
291 if (err < 0)
292 PDEBUG(D_STREAM, "Starting stream failed");
293 else
294 PDEBUG(D_STREAM, "Started streaming");
296 return (err < 0) ? err : 0;
299 static int stv06xx_isoc_init(struct gspca_dev *gspca_dev)
301 struct usb_host_interface *alt;
302 struct sd *sd = (struct sd *) gspca_dev;
304 /* Start isoc bandwidth "negotiation" at max isoc bandwidth */
305 alt = &gspca_dev->dev->config->intf_cache[0]->altsetting[1];
306 alt->endpoint[0].desc.wMaxPacketSize =
307 cpu_to_le16(sd->sensor->max_packet_size[gspca_dev->curr_mode]);
309 return 0;
312 static int stv06xx_isoc_nego(struct gspca_dev *gspca_dev)
314 int ret, packet_size, min_packet_size;
315 struct usb_host_interface *alt;
316 struct sd *sd = (struct sd *) gspca_dev;
318 alt = &gspca_dev->dev->config->intf_cache[0]->altsetting[1];
319 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
320 min_packet_size = sd->sensor->min_packet_size[gspca_dev->curr_mode];
321 if (packet_size <= min_packet_size)
322 return -EIO;
324 packet_size -= 100;
325 if (packet_size < min_packet_size)
326 packet_size = min_packet_size;
327 alt->endpoint[0].desc.wMaxPacketSize = cpu_to_le16(packet_size);
329 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
330 if (ret < 0)
331 PDEBUG(D_ERR|D_STREAM, "set alt 1 err %d", ret);
333 return ret;
336 static void stv06xx_stopN(struct gspca_dev *gspca_dev)
338 int err;
339 struct sd *sd = (struct sd *) gspca_dev;
341 /* stop ISO-streaming */
342 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
343 if (err < 0)
344 goto out;
346 err = sd->sensor->stop(sd);
348 out:
349 if (err < 0)
350 PDEBUG(D_STREAM, "Failed to stop stream");
351 else
352 PDEBUG(D_STREAM, "Stopped streaming");
356 * Analyse an USB packet of the data stream and store it appropriately.
357 * Each packet contains an integral number of chunks. Each chunk has
358 * 2-bytes identification, followed by 2-bytes that describe the chunk
359 * length. Known/guessed chunk identifications are:
360 * 8001/8005/C001/C005 - Begin new frame
361 * 8002/8006/C002/C006 - End frame
362 * 0200/4200 - Contains actual image data, bayer or compressed
363 * 0005 - 11 bytes of unknown data
364 * 0100 - 2 bytes of unknown data
365 * The 0005 and 0100 chunks seem to appear only in compressed stream.
367 static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
368 u8 *data, /* isoc packet */
369 int len) /* iso packet length */
371 struct sd *sd = (struct sd *) gspca_dev;
373 PDEBUG(D_PACK, "Packet of length %d arrived", len);
375 /* A packet may contain several frames
376 loop until the whole packet is reached */
377 while (len) {
378 int id, chunk_len;
380 if (len < 4) {
381 PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
382 return;
385 /* Capture the id */
386 id = (data[0] << 8) | data[1];
388 /* Capture the chunk length */
389 chunk_len = (data[2] << 8) | data[3];
390 PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len);
392 data += 4;
393 len -= 4;
395 if (len < chunk_len) {
396 PDEBUG(D_ERR, "URB packet length is smaller"
397 " than the specified chunk length");
398 gspca_dev->last_packet_type = DISCARD_PACKET;
399 return;
402 /* First byte seem to be 02=data 2nd byte is unknown??? */
403 if (sd->bridge == BRIDGE_ST6422 && (id & 0xff00) == 0x0200)
404 goto frame_data;
406 switch (id) {
407 case 0x0200:
408 case 0x4200:
409 frame_data:
410 PDEBUG(D_PACK, "Frame data packet detected");
412 if (sd->to_skip) {
413 int skip = (sd->to_skip < chunk_len) ?
414 sd->to_skip : chunk_len;
415 data += skip;
416 len -= skip;
417 chunk_len -= skip;
418 sd->to_skip -= skip;
421 gspca_frame_add(gspca_dev, INTER_PACKET,
422 data, chunk_len);
423 break;
425 case 0x8001:
426 case 0x8005:
427 case 0xc001:
428 case 0xc005:
429 PDEBUG(D_PACK, "Starting new frame");
431 /* Create a new frame, chunk length should be zero */
432 gspca_frame_add(gspca_dev, FIRST_PACKET,
433 NULL, 0);
435 if (sd->bridge == BRIDGE_ST6422)
436 sd->to_skip = gspca_dev->width * 4;
438 if (chunk_len)
439 PDEBUG(D_ERR, "Chunk length is "
440 "non-zero on a SOF");
441 break;
443 case 0x8002:
444 case 0x8006:
445 case 0xc002:
446 PDEBUG(D_PACK, "End of frame detected");
448 /* Complete the last frame (if any) */
449 gspca_frame_add(gspca_dev, LAST_PACKET,
450 NULL, 0);
452 if (chunk_len)
453 PDEBUG(D_ERR, "Chunk length is "
454 "non-zero on a EOF");
455 break;
457 case 0x0005:
458 PDEBUG(D_PACK, "Chunk 0x005 detected");
459 /* Unknown chunk with 11 bytes of data,
460 occurs just before end of each frame
461 in compressed mode */
462 break;
464 case 0x0100:
465 PDEBUG(D_PACK, "Chunk 0x0100 detected");
466 /* Unknown chunk with 2 bytes of data,
467 occurs 2-3 times per USB interrupt */
468 break;
469 case 0x42ff:
470 PDEBUG(D_PACK, "Chunk 0x42ff detected");
471 /* Special chunk seen sometimes on the ST6422 */
472 break;
473 default:
474 PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id);
475 /* Unknown chunk */
477 data += chunk_len;
478 len -= chunk_len;
482 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
483 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
484 u8 *data, /* interrupt packet data */
485 int len) /* interrupt packet length */
487 int ret = -EINVAL;
489 if (len == 1 && data[0] == 0x80) {
490 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
491 input_sync(gspca_dev->input_dev);
492 ret = 0;
495 if (len == 1 && data[0] == 0x88) {
496 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
497 input_sync(gspca_dev->input_dev);
498 ret = 0;
501 return ret;
503 #endif
505 static int stv06xx_config(struct gspca_dev *gspca_dev,
506 const struct usb_device_id *id);
508 /* sub-driver description */
509 static const struct sd_desc sd_desc = {
510 .name = MODULE_NAME,
511 .config = stv06xx_config,
512 .init = stv06xx_init,
513 .start = stv06xx_start,
514 .stopN = stv06xx_stopN,
515 .pkt_scan = stv06xx_pkt_scan,
516 .isoc_init = stv06xx_isoc_init,
517 .isoc_nego = stv06xx_isoc_nego,
518 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
519 .int_pkt_scan = sd_int_pkt_scan,
520 #endif
523 /* This function is called at probe time */
524 static int stv06xx_config(struct gspca_dev *gspca_dev,
525 const struct usb_device_id *id)
527 struct sd *sd = (struct sd *) gspca_dev;
529 PDEBUG(D_PROBE, "Configuring camera");
531 sd->desc = sd_desc;
532 sd->bridge = id->driver_info;
533 gspca_dev->sd_desc = &sd->desc;
535 if (dump_bridge)
536 stv06xx_dump_bridge(sd);
538 sd->sensor = &stv06xx_sensor_st6422;
539 if (!sd->sensor->probe(sd))
540 return 0;
542 sd->sensor = &stv06xx_sensor_vv6410;
543 if (!sd->sensor->probe(sd))
544 return 0;
546 sd->sensor = &stv06xx_sensor_hdcs1x00;
547 if (!sd->sensor->probe(sd))
548 return 0;
550 sd->sensor = &stv06xx_sensor_hdcs1020;
551 if (!sd->sensor->probe(sd))
552 return 0;
554 sd->sensor = &stv06xx_sensor_pb0100;
555 if (!sd->sensor->probe(sd))
556 return 0;
558 sd->sensor = NULL;
559 return -ENODEV;
564 /* -- module initialisation -- */
565 static const struct usb_device_id device_table[] = {
566 /* QuickCam Express */
567 {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 },
568 /* LEGO cam / QuickCam Web */
569 {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 },
570 /* Dexxa WebCam USB */
571 {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 },
572 /* QuickCam Messenger */
573 {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 },
574 /* QuickCam Communicate */
575 {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 },
576 /* QuickCam Messenger (new) */
577 {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 },
580 MODULE_DEVICE_TABLE(usb, device_table);
582 /* -- device connect -- */
583 static int sd_probe(struct usb_interface *intf,
584 const struct usb_device_id *id)
586 PDEBUG(D_PROBE, "Probing for a stv06xx device");
587 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
588 THIS_MODULE);
591 static void sd_disconnect(struct usb_interface *intf)
593 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
594 struct sd *sd = (struct sd *) gspca_dev;
595 PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
597 if (sd->sensor->disconnect)
598 sd->sensor->disconnect(sd);
599 gspca_disconnect(intf);
602 static struct usb_driver sd_driver = {
603 .name = MODULE_NAME,
604 .id_table = device_table,
605 .probe = sd_probe,
606 .disconnect = sd_disconnect,
607 #ifdef CONFIG_PM
608 .suspend = gspca_suspend,
609 .resume = gspca_resume,
610 #endif
613 /* -- module insert / remove -- */
614 static int __init sd_mod_init(void)
616 return usb_register(&sd_driver);
618 static void __exit sd_mod_exit(void)
620 usb_deregister(&sd_driver);
623 module_init(sd_mod_init);
624 module_exit(sd_mod_exit);
626 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
627 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
629 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
630 MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");