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
);
53 PDEBUG(D_CONF
, "Written 0x%x to address 0x%x, status: %d",
54 i2c_data
, address
, err
);
56 return (err
< 0) ? err
: 0;
59 int stv06xx_read_bridge(struct sd
*sd
, u16 address
, u8
*i2c_data
)
62 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
63 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
65 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
66 0x04, 0xc0, address
, 0, buf
, 1,
67 STV06XX_URB_MSG_TIMEOUT
);
71 PDEBUG(D_CONF
, "Reading 0x%x from address 0x%x, status %d",
72 *i2c_data
, address
, err
);
74 return (err
< 0) ? err
: 0;
77 /* Wraps the normal write sensor bytes / words functions for writing a
79 int stv06xx_write_sensor(struct sd
*sd
, u8 address
, u16 value
)
81 if (sd
->sensor
->i2c_len
== 2) {
82 u16 data
[2] = { address
, value
};
83 return stv06xx_write_sensor_words(sd
, data
, 1);
85 u8 data
[2] = { address
, value
};
86 return stv06xx_write_sensor_bytes(sd
, data
, 1);
90 static int stv06xx_write_sensor_finish(struct sd
*sd
)
94 if (sd
->bridge
== BRIDGE_STV610
) {
95 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
96 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
99 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
100 0x04, 0x40, 0x1704, 0, buf
, 1,
101 STV06XX_URB_MSG_TIMEOUT
);
104 return (err
< 0) ? err
: 0;
107 int stv06xx_write_sensor_bytes(struct sd
*sd
, const u8
*data
, u8 len
)
110 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
111 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
113 PDEBUG(D_CONF
, "I2C: Command buffer contains %d entries", len
);
114 for (i
= 0; i
< len
;) {
115 /* Build the command buffer */
116 memset(buf
, 0, I2C_BUFFER_LENGTH
);
117 for (j
= 0; j
< I2C_MAX_BYTES
&& i
< len
; j
++, i
++) {
119 buf
[0x10 + j
] = data
[2*i
+1];
120 PDEBUG(D_CONF
, "I2C: Writing 0x%02x to reg 0x%02x",
121 data
[2*i
+1], data
[2*i
]);
123 buf
[0x20] = sd
->sensor
->i2c_addr
;
124 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
125 buf
[0x22] = I2C_WRITE_CMD
;
126 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
127 0x04, 0x40, 0x0400, 0, buf
,
129 STV06XX_URB_MSG_TIMEOUT
);
133 return stv06xx_write_sensor_finish(sd
);
136 int stv06xx_write_sensor_words(struct sd
*sd
, const u16
*data
, u8 len
)
139 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
140 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
142 PDEBUG(D_CONF
, "I2C: Command buffer contains %d entries", len
);
144 for (i
= 0; i
< len
;) {
145 /* Build the command buffer */
146 memset(buf
, 0, I2C_BUFFER_LENGTH
);
147 for (j
= 0; j
< I2C_MAX_WORDS
&& i
< len
; j
++, i
++) {
149 buf
[0x10 + j
* 2] = data
[2*i
+1];
150 buf
[0x10 + j
* 2 + 1] = data
[2*i
+1] >> 8;
151 PDEBUG(D_CONF
, "I2C: Writing 0x%04x to reg 0x%02x",
152 data
[2*i
+1], data
[2*i
]);
154 buf
[0x20] = sd
->sensor
->i2c_addr
;
155 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
156 buf
[0x22] = I2C_WRITE_CMD
;
157 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
158 0x04, 0x40, 0x0400, 0, buf
,
160 STV06XX_URB_MSG_TIMEOUT
);
164 return stv06xx_write_sensor_finish(sd
);
167 int stv06xx_read_sensor(struct sd
*sd
, const u8 address
, u16
*value
)
170 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
171 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
173 err
= stv06xx_write_bridge(sd
, STV_I2C_FLUSH
, sd
->sensor
->i2c_flush
);
178 memset(buf
, 0, I2C_BUFFER_LENGTH
);
181 buf
[0x20] = sd
->sensor
->i2c_addr
;
184 /* Read I2C register */
185 buf
[0x22] = I2C_READ_CMD
;
187 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
188 0x04, 0x40, 0x1400, 0, buf
, I2C_BUFFER_LENGTH
,
189 STV06XX_URB_MSG_TIMEOUT
);
191 PDEBUG(D_ERR
, "I2C: Read error writing address: %d", err
);
195 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
196 0x04, 0xc0, 0x1410, 0, buf
, sd
->sensor
->i2c_len
,
197 STV06XX_URB_MSG_TIMEOUT
);
198 if (sd
->sensor
->i2c_len
== 2)
199 *value
= buf
[0] | (buf
[1] << 8);
203 PDEBUG(D_CONF
, "I2C: Read 0x%x from address 0x%x, status: %d",
204 *value
, address
, err
);
206 return (err
< 0) ? err
: 0;
209 /* Dumps all bridge registers */
210 static void stv06xx_dump_bridge(struct sd
*sd
)
215 info("Dumping all stv06xx bridge registers");
216 for (i
= 0x1400; i
< 0x160f; i
++) {
217 stv06xx_read_bridge(sd
, i
, &data
);
219 info("Read 0x%x from address 0x%x", data
, i
);
222 for (i
= 0x1400; i
< 0x160f; i
++) {
223 stv06xx_read_bridge(sd
, i
, &data
);
226 stv06xx_write_bridge(sd
, i
, 0xff);
227 stv06xx_read_bridge(sd
, i
, &data
);
229 info("Register 0x%x is read/write", i
);
230 else if (data
!= buf
)
231 info("Register 0x%x is read/write,"
232 "but only partially", i
);
234 info("Register 0x%x is read-only", i
);
236 stv06xx_write_bridge(sd
, i
, buf
);
240 /* this function is called at probe and resume time */
241 static int stv06xx_init(struct gspca_dev
*gspca_dev
)
243 struct sd
*sd
= (struct sd
*) gspca_dev
;
246 PDEBUG(D_PROBE
, "Initializing camera");
248 /* Let the usb init settle for a bit
249 before performing the initialization */
252 err
= sd
->sensor
->init(sd
);
254 if (dump_sensor
&& sd
->sensor
->dump
)
255 sd
->sensor
->dump(sd
);
257 return (err
< 0) ? err
: 0;
260 /* Start the camera */
261 static int stv06xx_start(struct gspca_dev
*gspca_dev
)
263 struct sd
*sd
= (struct sd
*) gspca_dev
;
266 /* Prepare the sensor for start */
267 err
= sd
->sensor
->start(sd
);
271 /* Start isochronous streaming */
272 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 1);
276 PDEBUG(D_STREAM
, "Starting stream failed");
278 PDEBUG(D_STREAM
, "Started streaming");
280 return (err
< 0) ? err
: 0;
283 static void stv06xx_stopN(struct gspca_dev
*gspca_dev
)
286 struct sd
*sd
= (struct sd
*) gspca_dev
;
288 /* stop ISO-streaming */
289 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 0);
293 err
= sd
->sensor
->stop(sd
);
297 PDEBUG(D_STREAM
, "Failed to stop stream");
299 PDEBUG(D_STREAM
, "Stopped streaming");
303 * Analyse an USB packet of the data stream and store it appropriately.
304 * Each packet contains an integral number of chunks. Each chunk has
305 * 2-bytes identification, followed by 2-bytes that describe the chunk
306 * length. Known/guessed chunk identifications are:
307 * 8001/8005/C001/C005 - Begin new frame
308 * 8002/8006/C002/C006 - End frame
309 * 0200/4200 - Contains actual image data, bayer or compressed
310 * 0005 - 11 bytes of unknown data
311 * 0100 - 2 bytes of unknown data
312 * The 0005 and 0100 chunks seem to appear only in compressed stream.
314 static void stv06xx_pkt_scan(struct gspca_dev
*gspca_dev
,
315 struct gspca_frame
*frame
, /* target */
316 __u8
*data
, /* isoc packet */
317 int len
) /* iso packet length */
319 struct sd
*sd
= (struct sd
*) gspca_dev
;
321 PDEBUG(D_PACK
, "Packet of length %d arrived", len
);
323 /* A packet may contain several frames
324 loop until the whole packet is reached */
329 PDEBUG(D_PACK
, "Packet is smaller than 4 bytes");
334 id
= (data
[0] << 8) | data
[1];
336 /* Capture the chunk length */
337 chunk_len
= (data
[2] << 8) | data
[3];
338 PDEBUG(D_PACK
, "Chunk id: %x, length: %d", id
, chunk_len
);
343 if (len
< chunk_len
) {
344 PDEBUG(D_ERR
, "URB packet length is smaller"
345 " than the specified chunk length");
346 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
350 /* First byte seem to be 02=data 2nd byte is unknown??? */
351 if (sd
->bridge
== BRIDGE_ST6422
&& (id
& 0xFF00) == 0x0200)
358 PDEBUG(D_PACK
, "Frame data packet detected");
361 int skip
= (sd
->to_skip
< chunk_len
) ?
362 sd
->to_skip
: chunk_len
;
369 gspca_frame_add(gspca_dev
, INTER_PACKET
, frame
,
377 PDEBUG(D_PACK
, "Starting new frame");
379 /* Create a new frame, chunk length should be zero */
380 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
383 if (sd
->bridge
== BRIDGE_ST6422
)
384 sd
->to_skip
= gspca_dev
->width
* 4;
387 PDEBUG(D_ERR
, "Chunk length is "
388 "non-zero on a SOF");
394 PDEBUG(D_PACK
, "End of frame detected");
396 /* Complete the last frame (if any) */
397 gspca_frame_add(gspca_dev
, LAST_PACKET
, frame
, data
, 0);
400 PDEBUG(D_ERR
, "Chunk length is "
401 "non-zero on a EOF");
405 PDEBUG(D_PACK
, "Chunk 0x005 detected");
406 /* Unknown chunk with 11 bytes of data,
407 occurs just before end of each frame
408 in compressed mode */
412 PDEBUG(D_PACK
, "Chunk 0x0100 detected");
413 /* Unknown chunk with 2 bytes of data,
414 occurs 2-3 times per USB interrupt */
417 PDEBUG(D_PACK
, "Chunk 0x42ff detected");
418 /* Special chunk seen sometimes on the ST6422 */
421 PDEBUG(D_PACK
, "Unknown chunk 0x%04x detected", id
);
429 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
430 const struct usb_device_id
*id
);
432 /* sub-driver description */
433 static const struct sd_desc sd_desc
= {
435 .config
= stv06xx_config
,
436 .init
= stv06xx_init
,
437 .start
= stv06xx_start
,
438 .stopN
= stv06xx_stopN
,
439 .pkt_scan
= stv06xx_pkt_scan
442 /* This function is called at probe time */
443 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
444 const struct usb_device_id
*id
)
446 struct sd
*sd
= (struct sd
*) gspca_dev
;
449 PDEBUG(D_PROBE
, "Configuring camera");
451 cam
= &gspca_dev
->cam
;
453 sd
->bridge
= id
->driver_info
;
454 gspca_dev
->sd_desc
= &sd
->desc
;
457 stv06xx_dump_bridge(sd
);
459 sd
->sensor
= &stv06xx_sensor_st6422
;
460 if (!sd
->sensor
->probe(sd
))
463 sd
->sensor
= &stv06xx_sensor_vv6410
;
464 if (!sd
->sensor
->probe(sd
))
467 sd
->sensor
= &stv06xx_sensor_hdcs1x00
;
468 if (!sd
->sensor
->probe(sd
))
471 sd
->sensor
= &stv06xx_sensor_hdcs1020
;
472 if (!sd
->sensor
->probe(sd
))
475 sd
->sensor
= &stv06xx_sensor_pb0100
;
476 if (!sd
->sensor
->probe(sd
))
485 /* -- module initialisation -- */
486 static const __devinitdata
struct usb_device_id device_table
[] = {
487 /* QuickCam Express */
488 {USB_DEVICE(0x046d, 0x0840), .driver_info
= BRIDGE_STV600
},
489 /* LEGO cam / QuickCam Web */
490 {USB_DEVICE(0x046d, 0x0850), .driver_info
= BRIDGE_STV610
},
491 /* Dexxa WebCam USB */
492 {USB_DEVICE(0x046d, 0x0870), .driver_info
= BRIDGE_STV602
},
493 /* QuickCam Messenger */
494 {USB_DEVICE(0x046D, 0x08F0), .driver_info
= BRIDGE_ST6422
},
495 /* QuickCam Communicate */
496 {USB_DEVICE(0x046D, 0x08F5), .driver_info
= BRIDGE_ST6422
},
497 /* QuickCam Messenger (new) */
498 {USB_DEVICE(0x046D, 0x08F6), .driver_info
= BRIDGE_ST6422
},
499 /* QuickCam Messenger (new) */
500 {USB_DEVICE(0x046D, 0x08DA), .driver_info
= BRIDGE_ST6422
},
503 MODULE_DEVICE_TABLE(usb
, device_table
);
505 /* -- device connect -- */
506 static int sd_probe(struct usb_interface
*intf
,
507 const struct usb_device_id
*id
)
509 PDEBUG(D_PROBE
, "Probing for a stv06xx device");
510 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
514 static void sd_disconnect(struct usb_interface
*intf
)
516 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
517 struct sd
*sd
= (struct sd
*) gspca_dev
;
518 PDEBUG(D_PROBE
, "Disconnecting the stv06xx device");
520 if (sd
->sensor
->disconnect
)
521 sd
->sensor
->disconnect(sd
);
522 gspca_disconnect(intf
);
525 static struct usb_driver sd_driver
= {
527 .id_table
= device_table
,
529 .disconnect
= sd_disconnect
,
531 .suspend
= gspca_suspend
,
532 .resume
= gspca_resume
,
536 /* -- module insert / remove -- */
537 static int __init
sd_mod_init(void)
540 ret
= usb_register(&sd_driver
);
543 PDEBUG(D_PROBE
, "registered");
546 static void __exit
sd_mod_exit(void)
548 usb_deregister(&sd_driver
);
549 PDEBUG(D_PROBE
, "deregistered");
552 module_init(sd_mod_init
);
553 module_exit(sd_mod_exit
);
555 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
556 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
558 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
559 MODULE_PARM_DESC(dump_sensor
, "Dumps all sensor registers at startup");