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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #include <linux/input.h>
33 #include "stv06xx_sensor.h"
35 MODULE_AUTHOR("Erik Andrén");
36 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
37 MODULE_LICENSE("GPL");
39 static bool dump_bridge
;
40 static bool dump_sensor
;
42 int stv06xx_write_bridge(struct sd
*sd
, u16 address
, u16 i2c_data
)
45 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
46 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
47 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
49 u8 len
= (i2c_data
> 0xff) ? 2 : 1;
51 buf
[0] = i2c_data
& 0xff;
52 buf
[1] = (i2c_data
>> 8) & 0xff;
54 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
55 0x04, 0x40, address
, 0, buf
, len
,
56 STV06XX_URB_MSG_TIMEOUT
);
58 PDEBUG(D_CONF
, "Written 0x%x to address 0x%x, status: %d",
59 i2c_data
, address
, err
);
61 return (err
< 0) ? err
: 0;
64 int stv06xx_read_bridge(struct sd
*sd
, u16 address
, u8
*i2c_data
)
67 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
68 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
69 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
71 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
72 0x04, 0xc0, address
, 0, buf
, 1,
73 STV06XX_URB_MSG_TIMEOUT
);
77 PDEBUG(D_CONF
, "Reading 0x%x from address 0x%x, status %d",
78 *i2c_data
, address
, err
);
80 return (err
< 0) ? err
: 0;
83 /* Wraps the normal write sensor bytes / words functions for writing a
85 int stv06xx_write_sensor(struct sd
*sd
, u8 address
, u16 value
)
87 if (sd
->sensor
->i2c_len
== 2) {
88 u16 data
[2] = { address
, value
};
89 return stv06xx_write_sensor_words(sd
, data
, 1);
91 u8 data
[2] = { address
, value
};
92 return stv06xx_write_sensor_bytes(sd
, data
, 1);
96 static int stv06xx_write_sensor_finish(struct sd
*sd
)
100 if (sd
->bridge
== BRIDGE_STV610
) {
101 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
102 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
105 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
106 0x04, 0x40, 0x1704, 0, buf
, 1,
107 STV06XX_URB_MSG_TIMEOUT
);
110 return (err
< 0) ? err
: 0;
113 int stv06xx_write_sensor_bytes(struct sd
*sd
, const u8
*data
, u8 len
)
116 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
117 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
118 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
120 PDEBUG(D_CONF
, "I2C: Command buffer contains %d entries", len
);
121 for (i
= 0; i
< len
;) {
122 /* Build the command buffer */
123 memset(buf
, 0, I2C_BUFFER_LENGTH
);
124 for (j
= 0; j
< I2C_MAX_BYTES
&& i
< len
; j
++, i
++) {
126 buf
[0x10 + j
] = data
[2*i
+1];
127 PDEBUG(D_CONF
, "I2C: Writing 0x%02x to reg 0x%02x",
128 data
[2*i
+1], data
[2*i
]);
130 buf
[0x20] = sd
->sensor
->i2c_addr
;
131 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
132 buf
[0x22] = I2C_WRITE_CMD
;
133 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
134 0x04, 0x40, 0x0400, 0, buf
,
136 STV06XX_URB_MSG_TIMEOUT
);
140 return stv06xx_write_sensor_finish(sd
);
143 int stv06xx_write_sensor_words(struct sd
*sd
, const u16
*data
, u8 len
)
146 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
147 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
148 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
150 PDEBUG(D_CONF
, "I2C: Command buffer contains %d entries", len
);
152 for (i
= 0; i
< len
;) {
153 /* Build the command buffer */
154 memset(buf
, 0, I2C_BUFFER_LENGTH
);
155 for (j
= 0; j
< I2C_MAX_WORDS
&& i
< len
; j
++, i
++) {
157 buf
[0x10 + j
* 2] = data
[2*i
+1];
158 buf
[0x10 + j
* 2 + 1] = data
[2*i
+1] >> 8;
159 PDEBUG(D_CONF
, "I2C: Writing 0x%04x to reg 0x%02x",
160 data
[2*i
+1], data
[2*i
]);
162 buf
[0x20] = sd
->sensor
->i2c_addr
;
163 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
164 buf
[0x22] = I2C_WRITE_CMD
;
165 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
166 0x04, 0x40, 0x0400, 0, buf
,
168 STV06XX_URB_MSG_TIMEOUT
);
172 return stv06xx_write_sensor_finish(sd
);
175 int stv06xx_read_sensor(struct sd
*sd
, const u8 address
, u16
*value
)
178 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
179 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
180 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
182 err
= stv06xx_write_bridge(sd
, STV_I2C_FLUSH
, sd
->sensor
->i2c_flush
);
187 memset(buf
, 0, I2C_BUFFER_LENGTH
);
190 buf
[0x20] = sd
->sensor
->i2c_addr
;
193 /* Read I2C register */
194 buf
[0x22] = I2C_READ_CMD
;
196 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
197 0x04, 0x40, 0x1400, 0, buf
, I2C_BUFFER_LENGTH
,
198 STV06XX_URB_MSG_TIMEOUT
);
200 pr_err("I2C: Read error writing address: %d\n", err
);
204 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
205 0x04, 0xc0, 0x1410, 0, buf
, sd
->sensor
->i2c_len
,
206 STV06XX_URB_MSG_TIMEOUT
);
207 if (sd
->sensor
->i2c_len
== 2)
208 *value
= buf
[0] | (buf
[1] << 8);
212 PDEBUG(D_CONF
, "I2C: Read 0x%x from address 0x%x, status: %d",
213 *value
, address
, err
);
215 return (err
< 0) ? err
: 0;
218 /* Dumps all bridge registers */
219 static void stv06xx_dump_bridge(struct sd
*sd
)
224 pr_info("Dumping all stv06xx bridge registers\n");
225 for (i
= 0x1400; i
< 0x160f; i
++) {
226 stv06xx_read_bridge(sd
, i
, &data
);
228 pr_info("Read 0x%x from address 0x%x\n", data
, i
);
231 pr_info("Testing stv06xx bridge registers for writability\n");
232 for (i
= 0x1400; i
< 0x160f; i
++) {
233 stv06xx_read_bridge(sd
, i
, &data
);
236 stv06xx_write_bridge(sd
, i
, 0xff);
237 stv06xx_read_bridge(sd
, i
, &data
);
239 pr_info("Register 0x%x is read/write\n", i
);
240 else if (data
!= buf
)
241 pr_info("Register 0x%x is read/write, but only partially\n",
244 pr_info("Register 0x%x is read-only\n", i
);
246 stv06xx_write_bridge(sd
, i
, buf
);
250 /* this function is called at probe and resume time */
251 static int stv06xx_init(struct gspca_dev
*gspca_dev
)
253 struct sd
*sd
= (struct sd
*) gspca_dev
;
256 PDEBUG(D_PROBE
, "Initializing camera");
258 /* Let the usb init settle for a bit
259 before performing the initialization */
262 err
= sd
->sensor
->init(sd
);
264 if (dump_sensor
&& sd
->sensor
->dump
)
265 sd
->sensor
->dump(sd
);
267 return (err
< 0) ? err
: 0;
270 /* this function is called at probe time */
271 static int stv06xx_init_controls(struct gspca_dev
*gspca_dev
)
273 struct sd
*sd
= (struct sd
*) gspca_dev
;
275 PDEBUG(D_PROBE
, "Initializing controls");
277 gspca_dev
->vdev
.ctrl_handler
= &gspca_dev
->ctrl_handler
;
278 return sd
->sensor
->init_controls(sd
);
281 /* Start the camera */
282 static int stv06xx_start(struct gspca_dev
*gspca_dev
)
284 struct sd
*sd
= (struct sd
*) gspca_dev
;
285 struct usb_host_interface
*alt
;
286 struct usb_interface
*intf
;
287 int err
, packet_size
;
289 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
290 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
292 PERR("Couldn't get altsetting");
296 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
297 err
= stv06xx_write_bridge(sd
, STV_ISO_SIZE_L
, packet_size
);
301 /* Prepare the sensor for start */
302 err
= sd
->sensor
->start(sd
);
306 /* Start isochronous streaming */
307 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 1);
311 PDEBUG(D_STREAM
, "Starting stream failed");
313 PDEBUG(D_STREAM
, "Started streaming");
315 return (err
< 0) ? err
: 0;
318 static int stv06xx_isoc_init(struct gspca_dev
*gspca_dev
)
320 struct usb_host_interface
*alt
;
321 struct sd
*sd
= (struct sd
*) gspca_dev
;
323 /* Start isoc bandwidth "negotiation" at max isoc bandwidth */
324 alt
= &gspca_dev
->dev
->actconfig
->intf_cache
[0]->altsetting
[1];
325 alt
->endpoint
[0].desc
.wMaxPacketSize
=
326 cpu_to_le16(sd
->sensor
->max_packet_size
[gspca_dev
->curr_mode
]);
331 static int stv06xx_isoc_nego(struct gspca_dev
*gspca_dev
)
333 int ret
, packet_size
, min_packet_size
;
334 struct usb_host_interface
*alt
;
335 struct sd
*sd
= (struct sd
*) gspca_dev
;
337 alt
= &gspca_dev
->dev
->actconfig
->intf_cache
[0]->altsetting
[1];
338 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
339 min_packet_size
= sd
->sensor
->min_packet_size
[gspca_dev
->curr_mode
];
340 if (packet_size
<= min_packet_size
)
344 if (packet_size
< min_packet_size
)
345 packet_size
= min_packet_size
;
346 alt
->endpoint
[0].desc
.wMaxPacketSize
= cpu_to_le16(packet_size
);
348 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
350 PERR("set alt 1 err %d", ret
);
355 static void stv06xx_stopN(struct gspca_dev
*gspca_dev
)
358 struct sd
*sd
= (struct sd
*) gspca_dev
;
360 /* stop ISO-streaming */
361 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 0);
365 err
= sd
->sensor
->stop(sd
);
369 PDEBUG(D_STREAM
, "Failed to stop stream");
371 PDEBUG(D_STREAM
, "Stopped streaming");
375 * Analyse an USB packet of the data stream and store it appropriately.
376 * Each packet contains an integral number of chunks. Each chunk has
377 * 2-bytes identification, followed by 2-bytes that describe the chunk
378 * length. Known/guessed chunk identifications are:
379 * 8001/8005/C001/C005 - Begin new frame
380 * 8002/8006/C002/C006 - End frame
381 * 0200/4200 - Contains actual image data, bayer or compressed
382 * 0005 - 11 bytes of unknown data
383 * 0100 - 2 bytes of unknown data
384 * The 0005 and 0100 chunks seem to appear only in compressed stream.
386 static void stv06xx_pkt_scan(struct gspca_dev
*gspca_dev
,
387 u8
*data
, /* isoc packet */
388 int len
) /* iso packet length */
390 struct sd
*sd
= (struct sd
*) gspca_dev
;
392 PDEBUG(D_PACK
, "Packet of length %d arrived", len
);
394 /* A packet may contain several frames
395 loop until the whole packet is reached */
400 PDEBUG(D_PACK
, "Packet is smaller than 4 bytes");
405 id
= (data
[0] << 8) | data
[1];
407 /* Capture the chunk length */
408 chunk_len
= (data
[2] << 8) | data
[3];
409 PDEBUG(D_PACK
, "Chunk id: %x, length: %d", id
, chunk_len
);
414 if (len
< chunk_len
) {
415 PERR("URB packet length is smaller"
416 " than the specified chunk length");
417 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
421 /* First byte seem to be 02=data 2nd byte is unknown??? */
422 if (sd
->bridge
== BRIDGE_ST6422
&& (id
& 0xff00) == 0x0200)
429 PDEBUG(D_PACK
, "Frame data packet detected");
432 int skip
= (sd
->to_skip
< chunk_len
) ?
433 sd
->to_skip
: chunk_len
;
440 gspca_frame_add(gspca_dev
, INTER_PACKET
,
448 PDEBUG(D_PACK
, "Starting new frame");
450 /* Create a new frame, chunk length should be zero */
451 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
454 if (sd
->bridge
== BRIDGE_ST6422
)
455 sd
->to_skip
= gspca_dev
->pixfmt
.width
* 4;
458 PERR("Chunk length is "
459 "non-zero on a SOF");
465 PDEBUG(D_PACK
, "End of frame detected");
467 /* Complete the last frame (if any) */
468 gspca_frame_add(gspca_dev
, LAST_PACKET
,
472 PERR("Chunk length is "
473 "non-zero on a EOF");
477 PDEBUG(D_PACK
, "Chunk 0x005 detected");
478 /* Unknown chunk with 11 bytes of data,
479 occurs just before end of each frame
480 in compressed mode */
484 PDEBUG(D_PACK
, "Chunk 0x0100 detected");
485 /* Unknown chunk with 2 bytes of data,
486 occurs 2-3 times per USB interrupt */
489 PDEBUG(D_PACK
, "Chunk 0x42ff detected");
490 /* Special chunk seen sometimes on the ST6422 */
493 PDEBUG(D_PACK
, "Unknown chunk 0x%04x detected", id
);
501 #if IS_ENABLED(CONFIG_INPUT)
502 static int sd_int_pkt_scan(struct gspca_dev
*gspca_dev
,
503 u8
*data
, /* interrupt packet data */
504 int len
) /* interrupt packet length */
508 if (len
== 1 && data
[0] == 0x80) {
509 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 1);
510 input_sync(gspca_dev
->input_dev
);
514 if (len
== 1 && data
[0] == 0x88) {
515 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
516 input_sync(gspca_dev
->input_dev
);
524 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
525 const struct usb_device_id
*id
);
527 /* sub-driver description */
528 static const struct sd_desc sd_desc
= {
530 .config
= stv06xx_config
,
531 .init
= stv06xx_init
,
532 .init_controls
= stv06xx_init_controls
,
533 .start
= stv06xx_start
,
534 .stopN
= stv06xx_stopN
,
535 .pkt_scan
= stv06xx_pkt_scan
,
536 .isoc_init
= stv06xx_isoc_init
,
537 .isoc_nego
= stv06xx_isoc_nego
,
538 #if IS_ENABLED(CONFIG_INPUT)
539 .int_pkt_scan
= sd_int_pkt_scan
,
543 /* This function is called at probe time */
544 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
545 const struct usb_device_id
*id
)
547 struct sd
*sd
= (struct sd
*) gspca_dev
;
549 PDEBUG(D_PROBE
, "Configuring camera");
551 sd
->bridge
= id
->driver_info
;
552 gspca_dev
->sd_desc
= &sd_desc
;
555 stv06xx_dump_bridge(sd
);
557 sd
->sensor
= &stv06xx_sensor_st6422
;
558 if (!sd
->sensor
->probe(sd
))
561 sd
->sensor
= &stv06xx_sensor_vv6410
;
562 if (!sd
->sensor
->probe(sd
))
565 sd
->sensor
= &stv06xx_sensor_hdcs1x00
;
566 if (!sd
->sensor
->probe(sd
))
569 sd
->sensor
= &stv06xx_sensor_hdcs1020
;
570 if (!sd
->sensor
->probe(sd
))
573 sd
->sensor
= &stv06xx_sensor_pb0100
;
574 if (!sd
->sensor
->probe(sd
))
583 /* -- module initialisation -- */
584 static const struct usb_device_id device_table
[] = {
585 /* QuickCam Express */
586 {USB_DEVICE(0x046d, 0x0840), .driver_info
= BRIDGE_STV600
},
587 /* LEGO cam / QuickCam Web */
588 {USB_DEVICE(0x046d, 0x0850), .driver_info
= BRIDGE_STV610
},
589 /* Dexxa WebCam USB */
590 {USB_DEVICE(0x046d, 0x0870), .driver_info
= BRIDGE_STV602
},
591 /* QuickCam Messenger */
592 {USB_DEVICE(0x046D, 0x08F0), .driver_info
= BRIDGE_ST6422
},
593 /* QuickCam Communicate */
594 {USB_DEVICE(0x046D, 0x08F5), .driver_info
= BRIDGE_ST6422
},
595 /* QuickCam Messenger (new) */
596 {USB_DEVICE(0x046D, 0x08F6), .driver_info
= BRIDGE_ST6422
},
599 MODULE_DEVICE_TABLE(usb
, device_table
);
601 /* -- device connect -- */
602 static int sd_probe(struct usb_interface
*intf
,
603 const struct usb_device_id
*id
)
605 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
609 static void sd_disconnect(struct usb_interface
*intf
)
611 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
612 struct sd
*sd
= (struct sd
*) gspca_dev
;
613 void *priv
= sd
->sensor_priv
;
614 PDEBUG(D_PROBE
, "Disconnecting the stv06xx device");
617 gspca_disconnect(intf
);
621 static struct usb_driver sd_driver
= {
623 .id_table
= device_table
,
625 .disconnect
= sd_disconnect
,
627 .suspend
= gspca_suspend
,
628 .resume
= gspca_resume
,
629 .reset_resume
= gspca_resume
,
633 module_usb_driver(sd_driver
);
635 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
636 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
638 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
639 MODULE_PARM_DESC(dump_sensor
, "Dumps all sensor registers at startup");