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 "stv06xx_sensor.h"
32 MODULE_AUTHOR("Erik Andrén");
33 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
34 MODULE_LICENSE("GPL");
36 static int dump_bridge
;
37 static int dump_sensor
;
39 int stv06xx_write_bridge(struct sd
*sd
, u16 address
, u16 i2c_data
)
42 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
43 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
44 u8 len
= (i2c_data
> 0xff) ? 2 : 1;
46 buf
[0] = i2c_data
& 0xff;
47 buf
[1] = (i2c_data
>> 8) & 0xff;
49 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
50 0x04, 0x40, address
, 0, buf
, len
,
51 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
)
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
);
72 PDEBUG(D_CONF
, "Read 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
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);
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
)
95 if (sd
->bridge
== BRIDGE_STV610
) {
96 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
97 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
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
)
111 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
112 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
114 PDEBUG(D_USBO
, "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
++) {
120 buf
[0x10 + j
] = data
[2*i
+1];
121 PDEBUG(D_USBO
, "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
,
130 STV06XX_URB_MSG_TIMEOUT
);
134 return stv06xx_write_sensor_finish(sd
);
137 int stv06xx_write_sensor_words(struct sd
*sd
, const u16
*data
, u8 len
)
140 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
141 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
143 PDEBUG(D_USBO
, "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
++) {
150 buf
[0x10 + j
* 2] = data
[2*i
+1];
151 buf
[0x10 + j
* 2 + 1] = data
[2*i
+1] >> 8;
152 PDEBUG(D_USBO
, "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
,
161 STV06XX_URB_MSG_TIMEOUT
);
165 return stv06xx_write_sensor_finish(sd
);
168 int stv06xx_read_sensor(struct sd
*sd
, const u8 address
, u16
*value
)
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
);
179 memset(buf
, 0, I2C_BUFFER_LENGTH
);
182 buf
[0x20] = sd
->sensor
->i2c_addr
;
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
);
192 PDEBUG(D_ERR
, "I2C Read: error writing address: %d", 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);
204 PDEBUG(D_USBO
, "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
)
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 for (i
= 0x1400; i
< 0x160f; i
++) {
224 stv06xx_read_bridge(sd
, i
, &data
);
227 stv06xx_write_bridge(sd
, i
, 0xff);
228 stv06xx_read_bridge(sd
, i
, &data
);
230 info("Register 0x%x is read/write", i
);
231 else if (data
!= buf
)
232 info("Register 0x%x is read/write,"
233 "but only partially", i
);
235 info("Register 0x%x is read-only", i
);
237 stv06xx_write_bridge(sd
, i
, buf
);
241 /* this function is called at probe and resume time */
242 static int stv06xx_init(struct gspca_dev
*gspca_dev
)
244 struct sd
*sd
= (struct sd
*) gspca_dev
;
247 PDEBUG(D_PROBE
, "Initializing camera");
249 /* Let the usb init settle for a bit
250 before performing the initialization */
253 err
= sd
->sensor
->init(sd
);
255 if (dump_sensor
&& sd
->sensor
->dump
)
256 sd
->sensor
->dump(sd
);
258 return (err
< 0) ? err
: 0;
261 /* Start the camera */
262 static int stv06xx_start(struct gspca_dev
*gspca_dev
)
264 struct sd
*sd
= (struct sd
*) gspca_dev
;
267 /* Prepare the sensor for start */
268 err
= sd
->sensor
->start(sd
);
272 /* Start isochronous streaming */
273 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 1);
277 PDEBUG(D_STREAM
, "Starting stream failed");
279 PDEBUG(D_STREAM
, "Started streaming");
281 return (err
< 0) ? err
: 0;
284 static void stv06xx_stopN(struct gspca_dev
*gspca_dev
)
287 struct sd
*sd
= (struct sd
*) gspca_dev
;
289 /* stop ISO-streaming */
290 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 0);
294 err
= sd
->sensor
->stop(sd
);
298 PDEBUG(D_STREAM
, "Failed to stop stream");
300 PDEBUG(D_STREAM
, "Stopped streaming");
304 * Analyse an USB packet of the data stream and store it appropriately.
305 * Each packet contains an integral number of chunks. Each chunk has
306 * 2-bytes identification, followed by 2-bytes that describe the chunk
307 * length. Known/guessed chunk identifications are:
308 * 8001/8005/C001/C005 - Begin new frame
309 * 8002/8006/C002/C006 - End frame
310 * 0200/4200 - Contains actual image data, bayer or compressed
311 * 0005 - 11 bytes of unknown data
312 * 0100 - 2 bytes of unknown data
313 * The 0005 and 0100 chunks seem to appear only in compressed stream.
315 static void stv06xx_pkt_scan(struct gspca_dev
*gspca_dev
,
316 struct gspca_frame
*frame
, /* target */
317 __u8
*data
, /* isoc packet */
318 int len
) /* iso packet length */
320 struct sd
*sd
= (struct sd
*) gspca_dev
;
322 PDEBUG(D_PACK
, "Packet of length %d arrived", len
);
324 /* A packet may contain several frames
325 loop until the whole packet is reached */
330 PDEBUG(D_PACK
, "Packet is smaller than 4 bytes");
335 id
= (data
[0] << 8) | data
[1];
337 /* Capture the chunk length */
338 chunk_len
= (data
[2] << 8) | data
[3];
339 PDEBUG(D_PACK
, "Chunk id: %x, length: %d", id
, chunk_len
);
344 if (len
< chunk_len
) {
345 PDEBUG(D_ERR
, "URB packet length is smaller"
346 " than the specified chunk length");
347 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
351 /* First byte seem to be 02=data 2nd byte is unknown??? */
352 if (sd
->bridge
== BRIDGE_ST6422
&& (id
& 0xFF00) == 0x0200)
359 PDEBUG(D_PACK
, "Frame data packet detected");
362 int skip
= (sd
->to_skip
< chunk_len
) ?
363 sd
->to_skip
: chunk_len
;
370 gspca_frame_add(gspca_dev
, INTER_PACKET
, frame
,
378 PDEBUG(D_PACK
, "Starting new frame");
380 /* Create a new frame, chunk length should be zero */
381 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
384 if (sd
->bridge
== BRIDGE_ST6422
)
385 sd
->to_skip
= gspca_dev
->width
* 4;
388 PDEBUG(D_ERR
, "Chunk length is "
389 "non-zero on a SOF");
395 PDEBUG(D_PACK
, "End of frame detected");
397 /* Complete the last frame (if any) */
398 gspca_frame_add(gspca_dev
, LAST_PACKET
, frame
, data
, 0);
401 PDEBUG(D_ERR
, "Chunk length is "
402 "non-zero on a EOF");
406 PDEBUG(D_PACK
, "Chunk 0x005 detected");
407 /* Unknown chunk with 11 bytes of data,
408 occurs just before end of each frame
409 in compressed mode */
413 PDEBUG(D_PACK
, "Chunk 0x0100 detected");
414 /* Unknown chunk with 2 bytes of data,
415 occurs 2-3 times per USB interrupt */
418 PDEBUG(D_PACK
, "Chunk 0x42ff detected");
419 /* Special chunk seen sometimes on the ST6422 */
422 PDEBUG(D_PACK
, "Unknown chunk 0x%04x detected", id
);
430 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
431 const struct usb_device_id
*id
);
433 /* sub-driver description */
434 static const struct sd_desc sd_desc
= {
436 .config
= stv06xx_config
,
437 .init
= stv06xx_init
,
438 .start
= stv06xx_start
,
439 .stopN
= stv06xx_stopN
,
440 .pkt_scan
= stv06xx_pkt_scan
443 /* This function is called at probe time */
444 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
445 const struct usb_device_id
*id
)
447 struct sd
*sd
= (struct sd
*) gspca_dev
;
450 PDEBUG(D_PROBE
, "Configuring camera");
452 cam
= &gspca_dev
->cam
;
454 sd
->bridge
= id
->driver_info
;
455 gspca_dev
->sd_desc
= &sd
->desc
;
458 stv06xx_dump_bridge(sd
);
460 sd
->sensor
= &stv06xx_sensor_st6422
;
461 if (!sd
->sensor
->probe(sd
))
464 sd
->sensor
= &stv06xx_sensor_vv6410
;
465 if (!sd
->sensor
->probe(sd
))
468 sd
->sensor
= &stv06xx_sensor_hdcs1x00
;
469 if (!sd
->sensor
->probe(sd
))
472 sd
->sensor
= &stv06xx_sensor_hdcs1020
;
473 if (!sd
->sensor
->probe(sd
))
476 sd
->sensor
= &stv06xx_sensor_pb0100
;
477 if (!sd
->sensor
->probe(sd
))
486 /* -- module initialisation -- */
487 static const __devinitdata
struct usb_device_id device_table
[] = {
488 /* QuickCam Express */
489 {USB_DEVICE(0x046d, 0x0840), .driver_info
= BRIDGE_STV600
},
490 /* LEGO cam / QuickCam Web */
491 {USB_DEVICE(0x046d, 0x0850), .driver_info
= BRIDGE_STV610
},
492 /* Dexxa WebCam USB */
493 {USB_DEVICE(0x046d, 0x0870), .driver_info
= BRIDGE_STV602
},
494 /* QuickCam Messenger */
495 {USB_DEVICE(0x046D, 0x08F0), .driver_info
= BRIDGE_ST6422
},
496 /* QuickCam Communicate */
497 {USB_DEVICE(0x046D, 0x08F5), .driver_info
= BRIDGE_ST6422
},
498 /* QuickCam Messenger (new) */
499 {USB_DEVICE(0x046D, 0x08F6), .driver_info
= BRIDGE_ST6422
},
500 /* QuickCam Messenger (new) */
501 {USB_DEVICE(0x046D, 0x08DA), .driver_info
= BRIDGE_ST6422
},
504 MODULE_DEVICE_TABLE(usb
, device_table
);
506 /* -- device connect -- */
507 static int sd_probe(struct usb_interface
*intf
,
508 const struct usb_device_id
*id
)
510 PDEBUG(D_PROBE
, "Probing for a stv06xx device");
511 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
515 static void sd_disconnect(struct usb_interface
*intf
)
517 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
518 struct sd
*sd
= (struct sd
*) gspca_dev
;
519 PDEBUG(D_PROBE
, "Disconnecting the stv06xx device");
521 if (sd
->sensor
->disconnect
)
522 sd
->sensor
->disconnect(sd
);
523 gspca_disconnect(intf
);
526 static struct usb_driver sd_driver
= {
528 .id_table
= device_table
,
530 .disconnect
= sd_disconnect
,
532 .suspend
= gspca_suspend
,
533 .resume
= gspca_resume
,
537 /* -- module insert / remove -- */
538 static int __init
sd_mod_init(void)
541 ret
= usb_register(&sd_driver
);
544 PDEBUG(D_PROBE
, "registered");
547 static void __exit
sd_mod_exit(void)
549 usb_deregister(&sd_driver
);
550 PDEBUG(D_PROBE
, "deregistered");
553 module_init(sd_mod_init
);
554 module_exit(sd_mod_exit
);
556 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
557 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
559 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
560 MODULE_PARM_DESC(dump_sensor
, "Dumps all sensor registers at startup");