1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
4 * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
5 * Copyright (c) 2002, 2003 Tuukka Toivonen
6 * Copyright (c) 2008 Erik Andrén
8 * P/N 861037: Sensor HDCS1000 ASIC STV0600
9 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
10 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
11 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
12 * P/N 861075-0040: Sensor HDCS1000 ASIC
13 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
14 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/input.h>
20 #include "stv06xx_sensor.h"
22 MODULE_AUTHOR("Erik Andrén");
23 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
24 MODULE_LICENSE("GPL");
26 static bool dump_bridge
;
27 static bool dump_sensor
;
29 int stv06xx_write_bridge(struct sd
*sd
, u16 address
, u16 i2c_data
)
32 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
33 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
34 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
36 u8 len
= (i2c_data
> 0xff) ? 2 : 1;
38 buf
[0] = i2c_data
& 0xff;
39 buf
[1] = (i2c_data
>> 8) & 0xff;
41 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
42 0x04, 0x40, address
, 0, buf
, len
,
43 STV06XX_URB_MSG_TIMEOUT
);
45 gspca_dbg(gspca_dev
, D_CONF
, "Written 0x%x to address 0x%x, status: %d\n",
46 i2c_data
, address
, err
);
48 return (err
< 0) ? err
: 0;
51 int stv06xx_read_bridge(struct sd
*sd
, u16 address
, u8
*i2c_data
)
54 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
55 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
56 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
58 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
59 0x04, 0xc0, address
, 0, buf
, 1,
60 STV06XX_URB_MSG_TIMEOUT
);
64 gspca_dbg(gspca_dev
, D_CONF
, "Reading 0x%x from address 0x%x, status %d\n",
65 *i2c_data
, address
, err
);
67 return (err
< 0) ? err
: 0;
70 /* Wraps the normal write sensor bytes / words functions for writing a
72 int stv06xx_write_sensor(struct sd
*sd
, u8 address
, u16 value
)
74 if (sd
->sensor
->i2c_len
== 2) {
75 u16 data
[2] = { address
, value
};
76 return stv06xx_write_sensor_words(sd
, data
, 1);
78 u8 data
[2] = { address
, value
};
79 return stv06xx_write_sensor_bytes(sd
, data
, 1);
83 static int stv06xx_write_sensor_finish(struct sd
*sd
)
87 if (sd
->bridge
== BRIDGE_STV610
) {
88 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
89 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
92 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
93 0x04, 0x40, 0x1704, 0, buf
, 1,
94 STV06XX_URB_MSG_TIMEOUT
);
97 return (err
< 0) ? err
: 0;
100 int stv06xx_write_sensor_bytes(struct sd
*sd
, const u8
*data
, u8 len
)
103 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
104 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
105 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
107 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Command buffer contains %d entries\n",
109 for (i
= 0; i
< len
;) {
110 /* Build the command buffer */
111 memset(buf
, 0, I2C_BUFFER_LENGTH
);
112 for (j
= 0; j
< I2C_MAX_BYTES
&& i
< len
; j
++, i
++) {
114 buf
[0x10 + j
] = data
[2*i
+1];
115 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Writing 0x%02x to reg 0x%02x\n",
116 data
[2*i
+1], data
[2*i
]);
118 buf
[0x20] = sd
->sensor
->i2c_addr
;
119 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
120 buf
[0x22] = I2C_WRITE_CMD
;
121 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
122 0x04, 0x40, 0x0400, 0, buf
,
124 STV06XX_URB_MSG_TIMEOUT
);
128 return stv06xx_write_sensor_finish(sd
);
131 int stv06xx_write_sensor_words(struct sd
*sd
, const u16
*data
, u8 len
)
134 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
135 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
136 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
138 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Command buffer contains %d entries\n",
141 for (i
= 0; i
< len
;) {
142 /* Build the command buffer */
143 memset(buf
, 0, I2C_BUFFER_LENGTH
);
144 for (j
= 0; j
< I2C_MAX_WORDS
&& i
< len
; j
++, i
++) {
146 buf
[0x10 + j
* 2] = data
[2*i
+1];
147 buf
[0x10 + j
* 2 + 1] = data
[2*i
+1] >> 8;
148 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Writing 0x%04x to reg 0x%02x\n",
149 data
[2*i
+1], data
[2*i
]);
151 buf
[0x20] = sd
->sensor
->i2c_addr
;
152 buf
[0x21] = j
- 1; /* Number of commands to send - 1 */
153 buf
[0x22] = I2C_WRITE_CMD
;
154 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
155 0x04, 0x40, 0x0400, 0, buf
,
157 STV06XX_URB_MSG_TIMEOUT
);
161 return stv06xx_write_sensor_finish(sd
);
164 int stv06xx_read_sensor(struct sd
*sd
, const u8 address
, u16
*value
)
167 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
168 struct usb_device
*udev
= sd
->gspca_dev
.dev
;
169 __u8
*buf
= sd
->gspca_dev
.usb_buf
;
171 err
= stv06xx_write_bridge(sd
, STV_I2C_FLUSH
, sd
->sensor
->i2c_flush
);
176 memset(buf
, 0, I2C_BUFFER_LENGTH
);
179 buf
[0x20] = sd
->sensor
->i2c_addr
;
182 /* Read I2C register */
183 buf
[0x22] = I2C_READ_CMD
;
185 err
= usb_control_msg(udev
, usb_sndctrlpipe(udev
, 0),
186 0x04, 0x40, 0x1400, 0, buf
, I2C_BUFFER_LENGTH
,
187 STV06XX_URB_MSG_TIMEOUT
);
189 pr_err("I2C: Read error writing address: %d\n", err
);
193 err
= usb_control_msg(udev
, usb_rcvctrlpipe(udev
, 0),
194 0x04, 0xc0, 0x1410, 0, buf
, sd
->sensor
->i2c_len
,
195 STV06XX_URB_MSG_TIMEOUT
);
196 if (sd
->sensor
->i2c_len
== 2)
197 *value
= buf
[0] | (buf
[1] << 8);
201 gspca_dbg(gspca_dev
, D_CONF
, "I2C: Read 0x%x from address 0x%x, status: %d\n",
202 *value
, address
, err
);
204 return (err
< 0) ? err
: 0;
207 /* Dumps all bridge registers */
208 static void stv06xx_dump_bridge(struct sd
*sd
)
213 pr_info("Dumping all stv06xx bridge registers\n");
214 for (i
= 0x1400; i
< 0x160f; i
++) {
215 stv06xx_read_bridge(sd
, i
, &data
);
217 pr_info("Read 0x%x from address 0x%x\n", data
, i
);
220 pr_info("Testing stv06xx bridge registers for writability\n");
221 for (i
= 0x1400; i
< 0x160f; i
++) {
222 stv06xx_read_bridge(sd
, i
, &data
);
225 stv06xx_write_bridge(sd
, i
, 0xff);
226 stv06xx_read_bridge(sd
, i
, &data
);
228 pr_info("Register 0x%x is read/write\n", i
);
229 else if (data
!= buf
)
230 pr_info("Register 0x%x is read/write, but only partially\n",
233 pr_info("Register 0x%x is read-only\n", i
);
235 stv06xx_write_bridge(sd
, i
, buf
);
239 /* this function is called at probe and resume time */
240 static int stv06xx_init(struct gspca_dev
*gspca_dev
)
242 struct sd
*sd
= (struct sd
*) gspca_dev
;
245 gspca_dbg(gspca_dev
, D_PROBE
, "Initializing camera\n");
247 /* Let the usb init settle for a bit
248 before performing the initialization */
251 err
= sd
->sensor
->init(sd
);
253 if (dump_sensor
&& sd
->sensor
->dump
)
254 sd
->sensor
->dump(sd
);
256 return (err
< 0) ? err
: 0;
259 /* this function is called at probe time */
260 static int stv06xx_init_controls(struct gspca_dev
*gspca_dev
)
262 struct sd
*sd
= (struct sd
*) gspca_dev
;
264 gspca_dbg(gspca_dev
, D_PROBE
, "Initializing controls\n");
266 gspca_dev
->vdev
.ctrl_handler
= &gspca_dev
->ctrl_handler
;
267 return sd
->sensor
->init_controls(sd
);
270 /* Start the camera */
271 static int stv06xx_start(struct gspca_dev
*gspca_dev
)
273 struct sd
*sd
= (struct sd
*) gspca_dev
;
274 struct usb_host_interface
*alt
;
275 struct usb_interface
*intf
;
276 int err
, packet_size
;
278 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
279 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
281 gspca_err(gspca_dev
, "Couldn't get altsetting\n");
285 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
286 err
= stv06xx_write_bridge(sd
, STV_ISO_SIZE_L
, packet_size
);
290 /* Prepare the sensor for start */
291 err
= sd
->sensor
->start(sd
);
295 /* Start isochronous streaming */
296 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 1);
300 gspca_dbg(gspca_dev
, D_STREAM
, "Starting stream failed\n");
302 gspca_dbg(gspca_dev
, D_STREAM
, "Started streaming\n");
304 return (err
< 0) ? err
: 0;
307 static int stv06xx_isoc_init(struct gspca_dev
*gspca_dev
)
309 struct usb_host_interface
*alt
;
310 struct sd
*sd
= (struct sd
*) gspca_dev
;
312 /* Start isoc bandwidth "negotiation" at max isoc bandwidth */
313 alt
= &gspca_dev
->dev
->actconfig
->intf_cache
[0]->altsetting
[1];
314 alt
->endpoint
[0].desc
.wMaxPacketSize
=
315 cpu_to_le16(sd
->sensor
->max_packet_size
[gspca_dev
->curr_mode
]);
320 static int stv06xx_isoc_nego(struct gspca_dev
*gspca_dev
)
322 int ret
, packet_size
, min_packet_size
;
323 struct usb_host_interface
*alt
;
324 struct sd
*sd
= (struct sd
*) gspca_dev
;
326 alt
= &gspca_dev
->dev
->actconfig
->intf_cache
[0]->altsetting
[1];
327 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
328 min_packet_size
= sd
->sensor
->min_packet_size
[gspca_dev
->curr_mode
];
329 if (packet_size
<= min_packet_size
)
333 if (packet_size
< min_packet_size
)
334 packet_size
= min_packet_size
;
335 alt
->endpoint
[0].desc
.wMaxPacketSize
= cpu_to_le16(packet_size
);
337 ret
= usb_set_interface(gspca_dev
->dev
, gspca_dev
->iface
, 1);
339 gspca_err(gspca_dev
, "set alt 1 err %d\n", ret
);
344 static void stv06xx_stopN(struct gspca_dev
*gspca_dev
)
347 struct sd
*sd
= (struct sd
*) gspca_dev
;
349 /* stop ISO-streaming */
350 err
= stv06xx_write_bridge(sd
, STV_ISO_ENABLE
, 0);
354 err
= sd
->sensor
->stop(sd
);
358 gspca_dbg(gspca_dev
, D_STREAM
, "Failed to stop stream\n");
360 gspca_dbg(gspca_dev
, D_STREAM
, "Stopped streaming\n");
364 * Analyse an USB packet of the data stream and store it appropriately.
365 * Each packet contains an integral number of chunks. Each chunk has
366 * 2-bytes identification, followed by 2-bytes that describe the chunk
367 * length. Known/guessed chunk identifications are:
368 * 8001/8005/C001/C005 - Begin new frame
369 * 8002/8006/C002/C006 - End frame
370 * 0200/4200 - Contains actual image data, bayer or compressed
371 * 0005 - 11 bytes of unknown data
372 * 0100 - 2 bytes of unknown data
373 * The 0005 and 0100 chunks seem to appear only in compressed stream.
375 static void stv06xx_pkt_scan(struct gspca_dev
*gspca_dev
,
376 u8
*data
, /* isoc packet */
377 int len
) /* iso packet length */
379 struct sd
*sd
= (struct sd
*) gspca_dev
;
381 gspca_dbg(gspca_dev
, D_PACK
, "Packet of length %d arrived\n", len
);
383 /* A packet may contain several frames
384 loop until the whole packet is reached */
389 gspca_dbg(gspca_dev
, D_PACK
, "Packet is smaller than 4 bytes\n");
394 id
= (data
[0] << 8) | data
[1];
396 /* Capture the chunk length */
397 chunk_len
= (data
[2] << 8) | data
[3];
398 gspca_dbg(gspca_dev
, D_PACK
, "Chunk id: %x, length: %d\n",
404 if (len
< chunk_len
) {
405 gspca_err(gspca_dev
, "URB packet length is smaller than the specified chunk length\n");
406 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
410 /* First byte seem to be 02=data 2nd byte is unknown??? */
411 if (sd
->bridge
== BRIDGE_ST6422
&& (id
& 0xff00) == 0x0200)
418 gspca_dbg(gspca_dev
, D_PACK
, "Frame data packet detected\n");
421 int skip
= (sd
->to_skip
< chunk_len
) ?
422 sd
->to_skip
: chunk_len
;
429 gspca_frame_add(gspca_dev
, INTER_PACKET
,
437 gspca_dbg(gspca_dev
, D_PACK
, "Starting new frame\n");
439 /* Create a new frame, chunk length should be zero */
440 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
443 if (sd
->bridge
== BRIDGE_ST6422
)
444 sd
->to_skip
= gspca_dev
->pixfmt
.width
* 4;
447 gspca_err(gspca_dev
, "Chunk length is non-zero on a SOF\n");
453 gspca_dbg(gspca_dev
, D_PACK
, "End of frame detected\n");
455 /* Complete the last frame (if any) */
456 gspca_frame_add(gspca_dev
, LAST_PACKET
,
460 gspca_err(gspca_dev
, "Chunk length is non-zero on a EOF\n");
464 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x005 detected\n");
465 /* Unknown chunk with 11 bytes of data,
466 occurs just before end of each frame
467 in compressed mode */
471 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x0100 detected\n");
472 /* Unknown chunk with 2 bytes of data,
473 occurs 2-3 times per USB interrupt */
476 gspca_dbg(gspca_dev
, D_PACK
, "Chunk 0x42ff detected\n");
477 /* Special chunk seen sometimes on the ST6422 */
480 gspca_dbg(gspca_dev
, D_PACK
, "Unknown chunk 0x%04x detected\n",
489 #if IS_ENABLED(CONFIG_INPUT)
490 static int sd_int_pkt_scan(struct gspca_dev
*gspca_dev
,
491 u8
*data
, /* interrupt packet data */
492 int len
) /* interrupt packet length */
496 if (len
== 1 && (data
[0] == 0x80 || data
[0] == 0x10)) {
497 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 1);
498 input_sync(gspca_dev
->input_dev
);
502 if (len
== 1 && (data
[0] == 0x88 || data
[0] == 0x11)) {
503 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
504 input_sync(gspca_dev
->input_dev
);
512 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
513 const struct usb_device_id
*id
);
515 /* sub-driver description */
516 static const struct sd_desc sd_desc
= {
518 .config
= stv06xx_config
,
519 .init
= stv06xx_init
,
520 .init_controls
= stv06xx_init_controls
,
521 .start
= stv06xx_start
,
522 .stopN
= stv06xx_stopN
,
523 .pkt_scan
= stv06xx_pkt_scan
,
524 .isoc_init
= stv06xx_isoc_init
,
525 .isoc_nego
= stv06xx_isoc_nego
,
526 #if IS_ENABLED(CONFIG_INPUT)
527 .int_pkt_scan
= sd_int_pkt_scan
,
531 /* This function is called at probe time */
532 static int stv06xx_config(struct gspca_dev
*gspca_dev
,
533 const struct usb_device_id
*id
)
535 struct sd
*sd
= (struct sd
*) gspca_dev
;
537 gspca_dbg(gspca_dev
, D_PROBE
, "Configuring camera\n");
539 sd
->bridge
= id
->driver_info
;
540 gspca_dev
->sd_desc
= &sd_desc
;
543 stv06xx_dump_bridge(sd
);
545 sd
->sensor
= &stv06xx_sensor_st6422
;
546 if (!sd
->sensor
->probe(sd
))
549 sd
->sensor
= &stv06xx_sensor_vv6410
;
550 if (!sd
->sensor
->probe(sd
))
553 sd
->sensor
= &stv06xx_sensor_hdcs1x00
;
554 if (!sd
->sensor
->probe(sd
))
557 sd
->sensor
= &stv06xx_sensor_hdcs1020
;
558 if (!sd
->sensor
->probe(sd
))
561 sd
->sensor
= &stv06xx_sensor_pb0100
;
562 if (!sd
->sensor
->probe(sd
))
571 /* -- module initialisation -- */
572 static const struct usb_device_id device_table
[] = {
573 {USB_DEVICE(0x046d, 0x0840), .driver_info
= BRIDGE_STV600
}, /* QuickCam Express */
574 {USB_DEVICE(0x046d, 0x0850), .driver_info
= BRIDGE_STV610
}, /* LEGO cam / QuickCam Web */
575 {USB_DEVICE(0x046d, 0x0870), .driver_info
= BRIDGE_STV602
}, /* Dexxa WebCam USB */
576 {USB_DEVICE(0x046D, 0x08F0), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Messenger */
577 {USB_DEVICE(0x046D, 0x08F5), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Communicate */
578 {USB_DEVICE(0x046D, 0x08F6), .driver_info
= BRIDGE_ST6422
}, /* QuickCam Messenger (new) */
581 MODULE_DEVICE_TABLE(usb
, device_table
);
583 /* -- device connect -- */
584 static int sd_probe(struct usb_interface
*intf
,
585 const struct usb_device_id
*id
)
587 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
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 void *priv
= sd
->sensor_priv
;
596 gspca_dbg(gspca_dev
, D_PROBE
, "Disconnecting the stv06xx device\n");
599 gspca_disconnect(intf
);
603 static struct usb_driver sd_driver
= {
605 .id_table
= device_table
,
607 .disconnect
= sd_disconnect
,
609 .suspend
= gspca_suspend
,
610 .resume
= gspca_resume
,
611 .reset_resume
= gspca_resume
,
615 module_usb_driver(sd_driver
);
617 module_param(dump_bridge
, bool, S_IRUGO
| S_IWUSR
);
618 MODULE_PARM_DESC(dump_bridge
, "Dumps all usb bridge registers at startup");
620 module_param(dump_sensor
, bool, S_IRUGO
| S_IWUSR
);
621 MODULE_PARM_DESC(dump_sensor
, "Dumps all sensor registers at startup");