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 * 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
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/input.h>
29 #include "stv06xx_sensor.h"
31 MODULE_AUTHOR("Erik Andrén");
32 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
33 MODULE_LICENSE("GPL");
35 static bool dump_bridge
;
36 static bool dump_sensor
;
38 int stv06xx_write_bridge(struct sd
*sd
, u16 address
, u16 i2c_data
)
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
;
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 gspca_dbg(gspca_dev
, D_CONF
, "Written 0x%x to address 0x%x, status: %d\n",
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 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
;
67 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
68 0x04, 0xc0, address
, 0, buf
, 1,
69 STV06XX_URB_MSG_TIMEOUT
);
73 gspca_dbg(gspca_dev
, D_CONF
, "Reading 0x%x from address 0x%x, status %d\n",
74 *i2c_data
, address
, err
);
76 return (err
< 0) ? err
: 0;
79 /* Wraps the normal write sensor bytes / words functions for writing a
81 int stv06xx_write_sensor(struct sd
*sd
, u8 address
, u16 value
)
83 if (sd
->sensor
->i2c_len
== 2) {
84 u16 data
[2] = { address
, value
};
85 return stv06xx_write_sensor_words(sd
, data
, 1);
87 u8 data
[2] = { address
, value
};
88 return stv06xx_write_sensor_bytes(sd
, data
, 1);
92 static int stv06xx_write_sensor_finish(struct sd
*sd
)
96 if (sd
->bridge
== BRIDGE_STV610
) {
97 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
98 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
101 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
102 0x04, 0x40, 0x1704, 0, buf
, 1,
103 STV06XX_URB_MSG_TIMEOUT
);
106 return (err
< 0) ? err
: 0;
109 int stv06xx_write_sensor_bytes(struct sd
*sd
, const u8
*data
, u8 len
)
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
;
116 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Command buffer contains %d entries\n",
118 for (i
= 0; i
< len
;) {
119 /* Build the command buffer */
120 memset(buf
, 0, I2C_BUFFER_LENGTH
);
121 for (j
= 0; j
< I2C_MAX_BYTES
&& i
< len
; j
++, i
++) {
123 buf
[0x10 + j
] = data
[2*i
+1];
124 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Writing 0x%02x to reg 0x%02x\n",
125 data
[2*i
+1], data
[2*i
]);
127 buf
[0x20] = sd
->sensor
->i2c_addr
;
128 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
129 buf
[0x22] = I2C_WRITE_CMD
;
130 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
131 0x04, 0x40, 0x0400, 0, buf
,
133 STV06XX_URB_MSG_TIMEOUT
);
137 return stv06xx_write_sensor_finish(sd
);
140 int stv06xx_write_sensor_words(struct sd
*sd
, const u16
*data
, u8 len
)
143 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
144 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
145 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
147 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Command buffer contains %d entries\n",
150 for (i
= 0; i
< len
;) {
151 /* Build the command buffer */
152 memset(buf
, 0, I2C_BUFFER_LENGTH
);
153 for (j
= 0; j
< I2C_MAX_WORDS
&& i
< len
; j
++, i
++) {
155 buf
[0x10 + j
* 2] = data
[2*i
+1];
156 buf
[0x10 + j
* 2 + 1] = data
[2*i
+1] >> 8;
157 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Writing 0x%04x to reg 0x%02x\n",
158 data
[2*i
+1], data
[2*i
]);
160 buf
[0x20] = sd
->sensor
->i2c_addr
;
161 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
162 buf
[0x22] = I2C_WRITE_CMD
;
163 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
164 0x04, 0x40, 0x0400, 0, buf
,
166 STV06XX_URB_MSG_TIMEOUT
);
170 return stv06xx_write_sensor_finish(sd
);
173 int stv06xx_read_sensor(struct sd
*sd
, const u8 address
, u16
*value
)
176 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
177 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
178 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
180 err
= stv06xx_write_bridge(sd
, STV_I2C_FLUSH
, sd
->sensor
->i2c_flush
);
185 memset(buf
, 0, I2C_BUFFER_LENGTH
);
188 buf
[0x20] = sd
->sensor
->i2c_addr
;
191 /* Read I2C register */
192 buf
[0x22] = I2C_READ_CMD
;
194 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
195 0x04, 0x40, 0x1400, 0, buf
, I2C_BUFFER_LENGTH
,
196 STV06XX_URB_MSG_TIMEOUT
);
198 pr_err("I2C: Read error writing address: %d\n", err
);
202 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
203 0x04, 0xc0, 0x1410, 0, buf
, sd
->sensor
->i2c_len
,
204 STV06XX_URB_MSG_TIMEOUT
);
205 if (sd
->sensor
->i2c_len
== 2)
206 *value
= buf
[0] | (buf
[1] << 8);
210 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Read 0x%x from address 0x%x, status: %d\n",
211 *value
, address
, err
);
213 return (err
< 0) ? err
: 0;
216 /* Dumps all bridge registers */
217 static void stv06xx_dump_bridge(struct sd
*sd
)
222 pr_info("Dumping all stv06xx bridge registers\n");
223 for (i
= 0x1400; i
< 0x160f; i
++) {
224 stv06xx_read_bridge(sd
, i
, &data
);
226 pr_info("Read 0x%x from address 0x%x\n", data
, i
);
229 pr_info("Testing stv06xx bridge registers for writability\n");
230 for (i
= 0x1400; i
< 0x160f; i
++) {
231 stv06xx_read_bridge(sd
, i
, &data
);
234 stv06xx_write_bridge(sd
, i
, 0xff);
235 stv06xx_read_bridge(sd
, i
, &data
);
237 pr_info("Register 0x%x is read/write\n", i
);
238 else if (data
!= buf
)
239 pr_info("Register 0x%x is read/write, but only partially\n",
242 pr_info("Register 0x%x is read-only\n", i
);
244 stv06xx_write_bridge(sd
, i
, buf
);
248 /* this function is called at probe and resume time */
249 static int stv06xx_init(struct gspca_dev
*gspca_dev
)
251 struct sd
*sd
= (struct sd
*) gspca_dev
;
254 gspca_dbg(gspca_dev
, D_PROBE
, "Initializing camera\n");
256 /* Let the usb init settle for a bit
257 before performing the initialization */
260 err
= sd
->sensor
->init(sd
);
262 if (dump_sensor
&& sd
->sensor
->dump
)
263 sd
->sensor
->dump(sd
);
265 return (err
< 0) ? err
: 0;
268 /* this function is called at probe time */
269 static int stv06xx_init_controls(struct gspca_dev
*gspca_dev
)
271 struct sd
*sd
= (struct sd
*) gspca_dev
;
273 gspca_dbg(gspca_dev
, D_PROBE
, "Initializing controls\n");
275 gspca_dev
->vdev
.ctrl_handler
= &gspca_dev
->ctrl_handler
;
276 return sd
->sensor
->init_controls(sd
);
279 /* Start the camera */
280 static int stv06xx_start(struct gspca_dev
*gspca_dev
)
282 struct sd
*sd
= (struct sd
*) gspca_dev
;
283 struct usb_host_interface
*alt
;
284 struct usb_interface
*intf
;
285 int err
, packet_size
;
287 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
288 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
290 gspca_err(gspca_dev
, "Couldn't get altsetting\n");
294 if (alt
->desc
.bNumEndpoints
< 1)
297 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
298 err
= stv06xx_write_bridge(sd
, STV_ISO_SIZE_L
, packet_size
);
302 /* Prepare the sensor for start */
303 err
= sd
->sensor
->start(sd
);
307 /* Start isochronous streaming */
308 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 1);
312 gspca_dbg(gspca_dev
, D_STREAM
, "Starting stream failed\n");
314 gspca_dbg(gspca_dev
, D_STREAM
, "Started streaming\n");
316 return (err
< 0) ? err
: 0;
319 static int stv06xx_isoc_init(struct gspca_dev
*gspca_dev
)
321 struct usb_interface_cache
*intfc
;
322 struct usb_host_interface
*alt
;
323 struct sd
*sd
= (struct sd
*) gspca_dev
;
325 intfc
= gspca_dev
->dev
->actconfig
->intf_cache
[0];
327 if (intfc
->num_altsetting
< 2)
330 alt
= &intfc
->altsetting
[1];
332 if (alt
->desc
.bNumEndpoints
< 1)
335 /* Start isoc bandwidth "negotiation" at max isoc bandwidth */
336 alt
->endpoint
[0].desc
.wMaxPacketSize
=
337 cpu_to_le16(sd
->sensor
->max_packet_size
[gspca_dev
->curr_mode
]);
342 static int stv06xx_isoc_nego(struct gspca_dev
*gspca_dev
)
344 int ret
, packet_size
, min_packet_size
;
345 struct usb_host_interface
*alt
;
346 struct sd
*sd
= (struct sd
*) gspca_dev
;
349 * Existence of altsetting and endpoint was verified in
350 * stv06xx_isoc_init()
352 alt
= &gspca_dev
->dev
->actconfig
->intf_cache
[0]->altsetting
[1];
353 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
354 min_packet_size
= sd
->sensor
->min_packet_size
[gspca_dev
->curr_mode
];
355 if (packet_size
<= min_packet_size
)
359 if (packet_size
< min_packet_size
)
360 packet_size
= min_packet_size
;
361 alt
->endpoint
[0].desc
.wMaxPacketSize
= cpu_to_le16(packet_size
);
363 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
365 gspca_err(gspca_dev
, "set alt 1 err %d\n", ret
);
370 static void stv06xx_stopN(struct gspca_dev
*gspca_dev
)
373 struct sd
*sd
= (struct sd
*) gspca_dev
;
375 /* stop ISO-streaming */
376 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 0);
380 err
= sd
->sensor
->stop(sd
);
384 gspca_dbg(gspca_dev
, D_STREAM
, "Failed to stop stream\n");
386 gspca_dbg(gspca_dev
, D_STREAM
, "Stopped streaming\n");
390 * Analyse an USB packet of the data stream and store it appropriately.
391 * Each packet contains an integral number of chunks. Each chunk has
392 * 2-bytes identification, followed by 2-bytes that describe the chunk
393 * length. Known/guessed chunk identifications are:
394 * 8001/8005/C001/C005 - Begin new frame
395 * 8002/8006/C002/C006 - End frame
396 * 0200/4200 - Contains actual image data, bayer or compressed
397 * 0005 - 11 bytes of unknown data
398 * 0100 - 2 bytes of unknown data
399 * The 0005 and 0100 chunks seem to appear only in compressed stream.
401 static void stv06xx_pkt_scan(struct gspca_dev
*gspca_dev
,
402 u8
*data
, /* isoc packet */
403 int len
) /* iso packet length */
405 struct sd
*sd
= (struct sd
*) gspca_dev
;
407 gspca_dbg(gspca_dev
, D_PACK
, "Packet of length %d arrived\n", len
);
409 /* A packet may contain several frames
410 loop until the whole packet is reached */
415 gspca_dbg(gspca_dev
, D_PACK
, "Packet is smaller than 4 bytes\n");
420 id
= (data
[0] << 8) | data
[1];
422 /* Capture the chunk length */
423 chunk_len
= (data
[2] << 8) | data
[3];
424 gspca_dbg(gspca_dev
, D_PACK
, "Chunk id: %x, length: %d\n",
430 if (len
< chunk_len
) {
431 gspca_err(gspca_dev
, "URB packet length is smaller than the specified chunk length\n");
432 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
436 /* First byte seem to be 02=data 2nd byte is unknown??? */
437 if (sd
->bridge
== BRIDGE_ST6422
&& (id
& 0xff00) == 0x0200)
444 gspca_dbg(gspca_dev
, D_PACK
, "Frame data packet detected\n");
447 int skip
= (sd
->to_skip
< chunk_len
) ?
448 sd
->to_skip
: chunk_len
;
455 gspca_frame_add(gspca_dev
, INTER_PACKET
,
463 gspca_dbg(gspca_dev
, D_PACK
, "Starting new frame\n");
465 /* Create a new frame, chunk length should be zero */
466 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
469 if (sd
->bridge
== BRIDGE_ST6422
)
470 sd
->to_skip
= gspca_dev
->pixfmt
.width
* 4;
473 gspca_err(gspca_dev
, "Chunk length is non-zero on a SOF\n");
479 gspca_dbg(gspca_dev
, D_PACK
, "End of frame detected\n");
481 /* Complete the last frame (if any) */
482 gspca_frame_add(gspca_dev
, LAST_PACKET
,
486 gspca_err(gspca_dev
, "Chunk length is non-zero on a EOF\n");
490 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x005 detected\n");
491 /* Unknown chunk with 11 bytes of data,
492 occurs just before end of each frame
493 in compressed mode */
497 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x0100 detected\n");
498 /* Unknown chunk with 2 bytes of data,
499 occurs 2-3 times per USB interrupt */
502 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x42ff detected\n");
503 /* Special chunk seen sometimes on the ST6422 */
506 gspca_dbg(gspca_dev
, D_PACK
, "Unknown chunk 0x%04x detected\n",
515 #if IS_ENABLED(CONFIG_INPUT)
516 static int sd_int_pkt_scan(struct gspca_dev
*gspca_dev
,
517 u8
*data
, /* interrupt packet data */
518 int len
) /* interrupt packet length */
522 if (len
== 1 && (data
[0] == 0x80 || data
[0] == 0x10)) {
523 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 1);
524 input_sync(gspca_dev
->input_dev
);
528 if (len
== 1 && (data
[0] == 0x88 || data
[0] == 0x11)) {
529 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
530 input_sync(gspca_dev
->input_dev
);
538 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
539 const struct usb_device_id
*id
);
541 /* sub-driver description */
542 static const struct sd_desc sd_desc
= {
544 .config
= stv06xx_config
,
545 .init
= stv06xx_init
,
546 .init_controls
= stv06xx_init_controls
,
547 .start
= stv06xx_start
,
548 .stopN
= stv06xx_stopN
,
549 .pkt_scan
= stv06xx_pkt_scan
,
550 .isoc_init
= stv06xx_isoc_init
,
551 .isoc_nego
= stv06xx_isoc_nego
,
552 #if IS_ENABLED(CONFIG_INPUT)
553 .int_pkt_scan
= sd_int_pkt_scan
,
557 /* This function is called at probe time */
558 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
559 const struct usb_device_id
*id
)
561 struct sd
*sd
= (struct sd
*) gspca_dev
;
563 gspca_dbg(gspca_dev
, D_PROBE
, "Configuring camera\n");
565 sd
->bridge
= id
->driver_info
;
566 gspca_dev
->sd_desc
= &sd_desc
;
569 stv06xx_dump_bridge(sd
);
571 sd
->sensor
= &stv06xx_sensor_st6422
;
572 if (!sd
->sensor
->probe(sd
))
575 sd
->sensor
= &stv06xx_sensor_vv6410
;
576 if (!sd
->sensor
->probe(sd
))
579 sd
->sensor
= &stv06xx_sensor_hdcs1x00
;
580 if (!sd
->sensor
->probe(sd
))
583 sd
->sensor
= &stv06xx_sensor_hdcs1020
;
584 if (!sd
->sensor
->probe(sd
))
587 sd
->sensor
= &stv06xx_sensor_pb0100
;
588 if (!sd
->sensor
->probe(sd
))
597 /* -- module initialisation -- */
598 static const struct usb_device_id device_table
[] = {
599 {USB_DEVICE(0x046d, 0x0840), .driver_info
= BRIDGE_STV600
}, /* QuickCam Express */
600 {USB_DEVICE(0x046d, 0x0850), .driver_info
= BRIDGE_STV610
}, /* LEGO cam / QuickCam Web */
601 {USB_DEVICE(0x046d, 0x0870), .driver_info
= BRIDGE_STV602
}, /* Dexxa WebCam USB */
602 {USB_DEVICE(0x046D, 0x08F0), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Messenger */
603 {USB_DEVICE(0x046D, 0x08F5), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Communicate */
604 {USB_DEVICE(0x046D, 0x08F6), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Messenger (new) */
607 MODULE_DEVICE_TABLE(usb
, device_table
);
609 /* -- device connect -- */
610 static int sd_probe(struct usb_interface
*intf
,
611 const struct usb_device_id
*id
)
613 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
617 static void sd_disconnect(struct usb_interface
*intf
)
619 struct gspca_dev
*gspca_dev
= usb_get_intfdata(intf
);
620 struct sd
*sd
= (struct sd
*) gspca_dev
;
621 void *priv
= sd
->sensor_priv
;
622 gspca_dbg(gspca_dev
, D_PROBE
, "Disconnecting the stv06xx device\n");
625 gspca_disconnect(intf
);
629 static struct usb_driver sd_driver
= {
631 .id_table
= device_table
,
633 .disconnect
= sd_disconnect
,
635 .suspend
= gspca_suspend
,
636 .resume
= gspca_resume
,
637 .reset_resume
= gspca_resume
,
641 module_usb_driver(sd_driver
);
643 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
644 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
646 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
647 MODULE_PARM_DESC(dump_sensor
, "Dumps all sensor registers at startup");