stv06xx.c revision 1381dfd5150cfbdb5fa55ce7ab5ab56812111909
1/*
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
6 *
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.
11 *
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.
16 *
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
20 *
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
28 */
29
30#include "stv06xx_sensor.h"
31
32MODULE_AUTHOR("Erik Andrén");
33MODULE_DESCRIPTION("STV06XX USB Camera Driver");
34MODULE_LICENSE("GPL");
35
36static int dump_bridge;
37static int dump_sensor;
38
39int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data)
40{
41	int err;
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;
45
46	buf[0] = i2c_data & 0xff;
47	buf[1] = (i2c_data >> 8) & 0xff;
48
49	err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
50			      0x04, 0x40, address, 0, buf, len,
51			      STV06XX_URB_MSG_TIMEOUT);
52
53	PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d",
54	       i2c_data, address, err);
55
56	return (err < 0) ? err : 0;
57}
58
59int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data)
60{
61	int err;
62	struct usb_device *udev = sd->gspca_dev.dev;
63	__u8 *buf = sd->gspca_dev.usb_buf;
64
65	err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
66			      0x04, 0xc0, address, 0, buf, 1,
67			      STV06XX_URB_MSG_TIMEOUT);
68
69	*i2c_data = buf[0];
70
71	PDEBUG(D_CONF, "Reading 0x%x from address 0x%x, status %d",
72	       *i2c_data, address, err);
73
74	return (err < 0) ? err : 0;
75}
76
77/* Wraps the normal write sensor bytes / words functions for writing a
78   single value */
79int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value)
80{
81	if (sd->sensor->i2c_len == 2) {
82		u16 data[2] = { address, value };
83		return stv06xx_write_sensor_words(sd, data, 1);
84	} else {
85		u8 data[2] = { address, value };
86		return stv06xx_write_sensor_bytes(sd, data, 1);
87	}
88}
89
90static int stv06xx_write_sensor_finish(struct sd *sd)
91{
92	int err = 0;
93
94	if (sd->bridge == BRIDGE_STV610) {
95		struct usb_device *udev = sd->gspca_dev.dev;
96		__u8 *buf = sd->gspca_dev.usb_buf;
97
98		buf[0] = 0;
99		err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
100				      0x04, 0x40, 0x1704, 0, buf, 1,
101				      STV06XX_URB_MSG_TIMEOUT);
102	}
103
104	return (err < 0) ? err : 0;
105}
106
107int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
108{
109	int err, i, j;
110	struct usb_device *udev = sd->gspca_dev.dev;
111	__u8 *buf = sd->gspca_dev.usb_buf;
112
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++) {
118			buf[j] = data[2*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]);
122		}
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,
128				      I2C_BUFFER_LENGTH,
129				      STV06XX_URB_MSG_TIMEOUT);
130		if (err < 0)
131			return err;
132	}
133	return stv06xx_write_sensor_finish(sd);
134}
135
136int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
137{
138	int err, i, j;
139	struct usb_device *udev = sd->gspca_dev.dev;
140	__u8 *buf = sd->gspca_dev.usb_buf;
141
142	PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
143
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++) {
148			buf[j] = data[2*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]);
153		}
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,
159				I2C_BUFFER_LENGTH,
160				STV06XX_URB_MSG_TIMEOUT);
161		if (err < 0)
162			return err;
163	}
164	return stv06xx_write_sensor_finish(sd);
165}
166
167int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
168{
169	int err;
170	struct usb_device *udev = sd->gspca_dev.dev;
171	__u8 *buf = sd->gspca_dev.usb_buf;
172
173	err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
174	if (err < 0)
175		return err;
176
177	/* Clear mem */
178	memset(buf, 0, I2C_BUFFER_LENGTH);
179
180	buf[0] = address;
181	buf[0x20] = sd->sensor->i2c_addr;
182	buf[0x21] = 0;
183
184	/* Read I2C register */
185	buf[0x22] = I2C_READ_CMD;
186
187	err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
188			      0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
189			      STV06XX_URB_MSG_TIMEOUT);
190	if (err < 0) {
191		PDEBUG(D_ERR, "I2C: Read error writing address: %d", err);
192		return err;
193	}
194
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);
200	else
201		*value = buf[0];
202
203	PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d",
204	       *value, address, err);
205
206	return (err < 0) ? err : 0;
207}
208
209/* Dumps all bridge registers */
210static void stv06xx_dump_bridge(struct sd *sd)
211{
212	int i;
213	u8 data, buf;
214
215	info("Dumping all stv06xx bridge registers");
216	for (i = 0x1400; i < 0x160f; i++) {
217		stv06xx_read_bridge(sd, i, &data);
218
219		info("Read 0x%x from address 0x%x", data, i);
220	}
221
222	for (i = 0x1400; i < 0x160f; i++) {
223		stv06xx_read_bridge(sd, i, &data);
224		buf = data;
225
226		stv06xx_write_bridge(sd, i, 0xff);
227		stv06xx_read_bridge(sd, i, &data);
228		if (data == 0xff)
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);
233		else
234			info("Register 0x%x is read-only", i);
235
236		stv06xx_write_bridge(sd, i, buf);
237	}
238}
239
240/* this function is called at probe and resume time */
241static int stv06xx_init(struct gspca_dev *gspca_dev)
242{
243	struct sd *sd = (struct sd *) gspca_dev;
244	int err;
245
246	PDEBUG(D_PROBE, "Initializing camera");
247
248	/* Let the usb init settle for a bit
249	   before performing the initialization */
250	msleep(250);
251
252	err = sd->sensor->init(sd);
253
254	if (dump_sensor && sd->sensor->dump)
255		sd->sensor->dump(sd);
256
257	return (err < 0) ? err : 0;
258}
259
260/* Start the camera */
261static int stv06xx_start(struct gspca_dev *gspca_dev)
262{
263	struct sd *sd = (struct sd *) gspca_dev;
264	int err;
265
266	/* Prepare the sensor for start */
267	err = sd->sensor->start(sd);
268	if (err < 0)
269		goto out;
270
271	/* Start isochronous streaming */
272	err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
273
274out:
275	if (err < 0)
276		PDEBUG(D_STREAM, "Starting stream failed");
277	else
278		PDEBUG(D_STREAM, "Started streaming");
279
280	return (err < 0) ? err : 0;
281}
282
283static void stv06xx_stopN(struct gspca_dev *gspca_dev)
284{
285	int err;
286	struct sd *sd = (struct sd *) gspca_dev;
287
288	/* stop ISO-streaming */
289	err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
290	if (err < 0)
291		goto out;
292
293	err = sd->sensor->stop(sd);
294
295out:
296	if (err < 0)
297		PDEBUG(D_STREAM, "Failed to stop stream");
298	else
299		PDEBUG(D_STREAM, "Stopped streaming");
300}
301
302/*
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.
313 */
314static 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 */
318{
319	struct sd *sd = (struct sd *) gspca_dev;
320
321	PDEBUG(D_PACK, "Packet of length %d arrived", len);
322
323	/* A packet may contain several frames
324	   loop until the whole packet is reached */
325	while (len) {
326		int id, chunk_len;
327
328		if (len < 4) {
329			PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
330			return;
331		}
332
333		/* Capture the id */
334		id = (data[0] << 8) | data[1];
335
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);
339
340		data += 4;
341		len -= 4;
342
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;
347			return;
348		}
349
350		/* First byte seem to be 02=data 2nd byte is unknown??? */
351		if (sd->bridge == BRIDGE_ST6422 && (id & 0xFF00) == 0x0200)
352			goto frame_data;
353
354		switch (id) {
355		case 0x0200:
356		case 0x4200:
357frame_data:
358			PDEBUG(D_PACK, "Frame data packet detected");
359
360			if (sd->to_skip) {
361				int skip = (sd->to_skip < chunk_len) ?
362					    sd->to_skip : chunk_len;
363				data += skip;
364				len -= skip;
365				chunk_len -= skip;
366				sd->to_skip -= skip;
367			}
368
369			gspca_frame_add(gspca_dev, INTER_PACKET, frame,
370					data, chunk_len);
371			break;
372
373		case 0x8001:
374		case 0x8005:
375		case 0xc001:
376		case 0xc005:
377			PDEBUG(D_PACK, "Starting new frame");
378
379			/* Create a new frame, chunk length should be zero */
380			gspca_frame_add(gspca_dev, FIRST_PACKET,
381					frame, data, 0);
382
383			if (sd->bridge == BRIDGE_ST6422)
384				sd->to_skip = gspca_dev->width * 4;
385
386			if (chunk_len)
387				PDEBUG(D_ERR, "Chunk length is "
388					      "non-zero on a SOF");
389			break;
390
391		case 0x8002:
392		case 0x8006:
393		case 0xc002:
394			PDEBUG(D_PACK, "End of frame detected");
395
396			/* Complete the last frame (if any) */
397			frame = gspca_frame_add(gspca_dev, LAST_PACKET,
398						frame, data, 0);
399
400			if (chunk_len)
401				PDEBUG(D_ERR, "Chunk length is "
402					      "non-zero on a EOF");
403			break;
404
405		case 0x0005:
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 */
410			break;
411
412		case 0x0100:
413			PDEBUG(D_PACK, "Chunk 0x0100 detected");
414			/* Unknown chunk with 2 bytes of data,
415			   occurs 2-3 times per USB interrupt */
416			break;
417		case 0x42ff:
418			PDEBUG(D_PACK, "Chunk 0x42ff detected");
419			/* Special chunk seen sometimes on the ST6422 */
420			break;
421		default:
422			PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id);
423			/* Unknown chunk */
424		}
425		data    += chunk_len;
426		len     -= chunk_len;
427	}
428}
429
430static int stv06xx_config(struct gspca_dev *gspca_dev,
431			  const struct usb_device_id *id);
432
433/* sub-driver description */
434static const struct sd_desc sd_desc = {
435	.name = MODULE_NAME,
436	.config = stv06xx_config,
437	.init = stv06xx_init,
438	.start = stv06xx_start,
439	.stopN = stv06xx_stopN,
440	.pkt_scan = stv06xx_pkt_scan
441};
442
443/* This function is called at probe time */
444static int stv06xx_config(struct gspca_dev *gspca_dev,
445			  const struct usb_device_id *id)
446{
447	struct sd *sd = (struct sd *) gspca_dev;
448	struct cam *cam;
449
450	PDEBUG(D_PROBE, "Configuring camera");
451
452	cam = &gspca_dev->cam;
453	sd->desc = sd_desc;
454	sd->bridge = id->driver_info;
455	gspca_dev->sd_desc = &sd->desc;
456
457	if (dump_bridge)
458		stv06xx_dump_bridge(sd);
459
460	sd->sensor = &stv06xx_sensor_st6422;
461	if (!sd->sensor->probe(sd))
462		return 0;
463
464	sd->sensor = &stv06xx_sensor_vv6410;
465	if (!sd->sensor->probe(sd))
466		return 0;
467
468	sd->sensor = &stv06xx_sensor_hdcs1x00;
469	if (!sd->sensor->probe(sd))
470		return 0;
471
472	sd->sensor = &stv06xx_sensor_hdcs1020;
473	if (!sd->sensor->probe(sd))
474		return 0;
475
476	sd->sensor = &stv06xx_sensor_pb0100;
477	if (!sd->sensor->probe(sd))
478		return 0;
479
480	sd->sensor = NULL;
481	return -ENODEV;
482}
483
484
485
486/* -- module initialisation -- */
487static 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 },
502	{}
503};
504MODULE_DEVICE_TABLE(usb, device_table);
505
506/* -- device connect -- */
507static int sd_probe(struct usb_interface *intf,
508			const struct usb_device_id *id)
509{
510	PDEBUG(D_PROBE, "Probing for a stv06xx device");
511	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
512			       THIS_MODULE);
513}
514
515static void sd_disconnect(struct usb_interface *intf)
516{
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");
520
521	if (sd->sensor->disconnect)
522		sd->sensor->disconnect(sd);
523	gspca_disconnect(intf);
524}
525
526static struct usb_driver sd_driver = {
527	.name = MODULE_NAME,
528	.id_table = device_table,
529	.probe = sd_probe,
530	.disconnect = sd_disconnect,
531#ifdef CONFIG_PM
532	.suspend = gspca_suspend,
533	.resume = gspca_resume,
534#endif
535};
536
537/* -- module insert / remove -- */
538static int __init sd_mod_init(void)
539{
540	int ret;
541	ret = usb_register(&sd_driver);
542	if (ret < 0)
543		return ret;
544	PDEBUG(D_PROBE, "registered");
545	return 0;
546}
547static void __exit sd_mod_exit(void)
548{
549	usb_deregister(&sd_driver);
550	PDEBUG(D_PROBE, "deregistered");
551}
552
553module_init(sd_mod_init);
554module_exit(sd_mod_exit);
555
556module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
557MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
558
559module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
560MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");
561