Commit | Line | Data |
---|---|---|
4c98834a EA |
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 | ||
32 | MODULE_AUTHOR("Erik Andrén"); | |
33 | MODULE_DESCRIPTION("STV06XX USB Camera Driver"); | |
34 | MODULE_LICENSE("GPL"); | |
35 | ||
8a787b40 MCC |
36 | static int dump_bridge; |
37 | static int dump_sensor; | |
4c98834a EA |
38 | |
39 | int 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 | ||
54 | PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d", | |
55 | i2c_data, address, err); | |
56 | ||
57 | return (err < 0) ? err : 0; | |
58 | } | |
59 | ||
60 | int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data) | |
61 | { | |
62 | int err; | |
63 | struct usb_device *udev = sd->gspca_dev.dev; | |
64 | __u8 *buf = sd->gspca_dev.usb_buf; | |
65 | ||
66 | err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
67 | 0x04, 0xc0, address, 0, buf, 1, | |
68 | STV06XX_URB_MSG_TIMEOUT); | |
69 | ||
70 | *i2c_data = buf[0]; | |
71 | ||
72 | PDEBUG(D_CONF, "Read 0x%x from address 0x%x, status %d", | |
73 | *i2c_data, address, err); | |
74 | ||
75 | return (err < 0) ? err : 0; | |
76 | } | |
77 | ||
78 | /* Wraps the normal write sensor bytes / words functions for writing a | |
79 | single value */ | |
80 | int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value) | |
81 | { | |
82 | if (sd->sensor->i2c_len == 2) { | |
83 | u16 data[2] = { address, value }; | |
84 | return stv06xx_write_sensor_words(sd, data, 1); | |
85 | } else { | |
86 | u8 data[2] = { address, value }; | |
87 | return stv06xx_write_sensor_bytes(sd, data, 1); | |
88 | } | |
89 | } | |
90 | ||
91 | static int stv06xx_write_sensor_finish(struct sd *sd) | |
92 | { | |
93 | int err = 0; | |
94 | ||
95 | if (IS_850(sd)) { | |
96 | struct usb_device *udev = sd->gspca_dev.dev; | |
97 | __u8 *buf = sd->gspca_dev.usb_buf; | |
98 | ||
99 | /* Quickam Web needs an extra packet */ | |
100 | buf[0] = 0; | |
101 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
102 | 0x04, 0x40, 0x1704, 0, buf, 1, | |
103 | STV06XX_URB_MSG_TIMEOUT); | |
104 | } | |
105 | ||
106 | return (err < 0) ? err : 0; | |
107 | } | |
108 | ||
109 | int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len) | |
110 | { | |
111 | int err, i, j; | |
112 | struct usb_device *udev = sd->gspca_dev.dev; | |
113 | __u8 *buf = sd->gspca_dev.usb_buf; | |
114 | ||
115 | PDEBUG(D_USBO, "I2C: Command buffer contains %d entries", len); | |
116 | for (i = 0; i < len;) { | |
117 | /* Build the command buffer */ | |
118 | memset(buf, 0, I2C_BUFFER_LENGTH); | |
119 | for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) { | |
120 | buf[j] = data[2*i]; | |
121 | buf[0x10 + j] = data[2*i+1]; | |
122 | PDEBUG(D_USBO, "I2C: Writing 0x%02x to reg 0x%02x", | |
123 | data[2*i+1], data[2*i]); | |
124 | } | |
125 | buf[0x20] = sd->sensor->i2c_addr; | |
126 | buf[0x21] = j - 1; /* Number of commands to send - 1 */ | |
127 | buf[0x22] = I2C_WRITE_CMD; | |
128 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
129 | 0x04, 0x40, 0x0400, 0, buf, | |
130 | I2C_BUFFER_LENGTH, | |
131 | STV06XX_URB_MSG_TIMEOUT); | |
132 | if (err < 0) | |
133 | return err; | |
134 | } | |
135 | return stv06xx_write_sensor_finish(sd); | |
136 | } | |
137 | ||
138 | int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len) | |
139 | { | |
140 | int err, i, j; | |
141 | struct usb_device *udev = sd->gspca_dev.dev; | |
142 | __u8 *buf = sd->gspca_dev.usb_buf; | |
143 | ||
144 | PDEBUG(D_USBO, "I2C: Command buffer contains %d entries", len); | |
145 | ||
146 | for (i = 0; i < len;) { | |
147 | /* Build the command buffer */ | |
148 | memset(buf, 0, I2C_BUFFER_LENGTH); | |
149 | for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) { | |
150 | buf[j] = data[2*i]; | |
151 | buf[0x10 + j * 2] = data[2*i+1]; | |
152 | buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8; | |
153 | PDEBUG(D_USBO, "I2C: Writing 0x%04x to reg 0x%02x", | |
154 | data[2*i+1], data[2*i]); | |
155 | } | |
156 | buf[0x20] = sd->sensor->i2c_addr; | |
157 | buf[0x21] = j - 1; /* Number of commands to send - 1 */ | |
158 | buf[0x22] = I2C_WRITE_CMD; | |
159 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
160 | 0x04, 0x40, 0x0400, 0, buf, | |
161 | I2C_BUFFER_LENGTH, | |
162 | STV06XX_URB_MSG_TIMEOUT); | |
163 | if (err < 0) | |
164 | return err; | |
165 | } | |
166 | return stv06xx_write_sensor_finish(sd); | |
167 | } | |
168 | ||
169 | int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value) | |
170 | { | |
171 | int err; | |
172 | struct usb_device *udev = sd->gspca_dev.dev; | |
173 | __u8 *buf = sd->gspca_dev.usb_buf; | |
174 | ||
175 | err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush); | |
176 | if (err < 0) | |
177 | return err; | |
178 | ||
179 | /* Clear mem */ | |
180 | memset(buf, 0, I2C_BUFFER_LENGTH); | |
181 | ||
182 | buf[0] = address; | |
183 | buf[0x20] = sd->sensor->i2c_addr; | |
184 | buf[0x21] = 0; | |
185 | ||
186 | /* Read I2C register */ | |
187 | buf[0x22] = I2C_READ_CMD; | |
188 | ||
189 | err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
190 | 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH, | |
191 | STV06XX_URB_MSG_TIMEOUT); | |
192 | if (err < 0) { | |
193 | PDEBUG(D_ERR, "I2C Read: error writing address: %d", err); | |
194 | return err; | |
195 | } | |
196 | ||
197 | err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), | |
198 | 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len, | |
199 | STV06XX_URB_MSG_TIMEOUT); | |
200 | if (sd->sensor->i2c_len == 2) | |
201 | *value = buf[0] | (buf[1] << 8); | |
202 | else | |
203 | *value = buf[0]; | |
204 | ||
205 | PDEBUG(D_USBO, "I2C: Read 0x%x from address 0x%x, status: %d", | |
206 | *value, address, err); | |
207 | ||
208 | return (err < 0) ? err : 0; | |
209 | } | |
210 | ||
211 | /* Dumps all bridge registers */ | |
212 | static void stv06xx_dump_bridge(struct sd *sd) | |
213 | { | |
214 | int i; | |
215 | u8 data, buf; | |
216 | ||
217 | info("Dumping all stv06xx bridge registers"); | |
218 | for (i = 0x1400; i < 0x160f; i++) { | |
219 | stv06xx_read_bridge(sd, i, &data); | |
220 | ||
221 | info("Read 0x%x from address 0x%x", data, i); | |
222 | } | |
223 | ||
224 | for (i = 0x1400; i < 0x160f; i++) { | |
225 | stv06xx_read_bridge(sd, i, &data); | |
226 | buf = data; | |
227 | ||
228 | stv06xx_write_bridge(sd, i, 0xff); | |
229 | stv06xx_read_bridge(sd, i, &data); | |
230 | if (data == 0xff) | |
231 | info("Register 0x%x is read/write", i); | |
232 | else if (data != buf) | |
233 | info("Register 0x%x is read/write," | |
234 | "but only partially", i); | |
235 | else | |
236 | info("Register 0x%x is read-only", i); | |
237 | ||
238 | stv06xx_write_bridge(sd, i, buf); | |
239 | } | |
240 | } | |
241 | ||
242 | /* this function is called at probe and resume time */ | |
243 | static int stv06xx_init(struct gspca_dev *gspca_dev) | |
244 | { | |
245 | struct sd *sd = (struct sd *) gspca_dev; | |
246 | int err; | |
247 | ||
248 | PDEBUG(D_PROBE, "Initializing camera"); | |
249 | ||
250 | /* Let the usb init settle for a bit | |
251 | before performing the initialization */ | |
252 | msleep(250); | |
253 | ||
254 | err = sd->sensor->init(sd); | |
255 | ||
256 | if (dump_sensor) | |
257 | sd->sensor->dump(sd); | |
258 | ||
259 | return (err < 0) ? err : 0; | |
260 | } | |
261 | ||
262 | /* Start the camera */ | |
263 | static int stv06xx_start(struct gspca_dev *gspca_dev) | |
264 | { | |
265 | struct sd *sd = (struct sd *) gspca_dev; | |
266 | int err; | |
267 | ||
268 | /* Prepare the sensor for start */ | |
269 | err = sd->sensor->start(sd); | |
270 | if (err < 0) | |
271 | goto out; | |
272 | ||
273 | /* Start isochronous streaming */ | |
274 | err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1); | |
275 | ||
276 | out: | |
277 | if (err < 0) | |
278 | PDEBUG(D_STREAM, "Starting stream failed"); | |
279 | else | |
280 | PDEBUG(D_STREAM, "Started streaming"); | |
281 | ||
282 | return (err < 0) ? err : 0; | |
283 | } | |
284 | ||
285 | static void stv06xx_stopN(struct gspca_dev *gspca_dev) | |
286 | { | |
287 | int err; | |
288 | struct sd *sd = (struct sd *) gspca_dev; | |
289 | ||
290 | /* stop ISO-streaming */ | |
291 | err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0); | |
292 | if (err < 0) | |
293 | goto out; | |
294 | ||
295 | err = sd->sensor->stop(sd); | |
296 | if (err < 0) | |
297 | goto out; | |
298 | ||
299 | out: | |
300 | if (err < 0) | |
301 | PDEBUG(D_STREAM, "Failed to stop stream"); | |
302 | else | |
303 | PDEBUG(D_STREAM, "Stopped streaming"); | |
304 | } | |
305 | ||
306 | /* | |
307 | * Analyse an USB packet of the data stream and store it appropriately. | |
308 | * Each packet contains an integral number of chunks. Each chunk has | |
309 | * 2-bytes identification, followed by 2-bytes that describe the chunk | |
310 | * length. Known/guessed chunk identifications are: | |
311 | * 8001/8005/C001/C005 - Begin new frame | |
312 | * 8002/8006/C002/C006 - End frame | |
313 | * 0200/4200 - Contains actual image data, bayer or compressed | |
314 | * 0005 - 11 bytes of unknown data | |
315 | * 0100 - 2 bytes of unknown data | |
316 | * The 0005 and 0100 chunks seem to appear only in compressed stream. | |
317 | */ | |
318 | static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev, | |
319 | struct gspca_frame *frame, /* target */ | |
320 | __u8 *data, /* isoc packet */ | |
321 | int len) /* iso packet length */ | |
322 | { | |
323 | PDEBUG(D_PACK, "Packet of length %d arrived", len); | |
324 | ||
325 | /* A packet may contain several frames | |
326 | loop until the whole packet is reached */ | |
327 | while (len) { | |
328 | int id, chunk_len; | |
329 | ||
330 | if (len < 4) { | |
331 | PDEBUG(D_PACK, "Packet is smaller than 4 bytes"); | |
332 | return; | |
333 | } | |
334 | ||
335 | /* Capture the id */ | |
336 | id = (data[0] << 8) | data[1]; | |
337 | ||
338 | /* Capture the chunk length */ | |
339 | chunk_len = (data[2] << 8) | data[3]; | |
340 | PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len); | |
341 | ||
342 | data += 4; | |
343 | len -= 4; | |
344 | ||
345 | if (len < chunk_len) { | |
346 | PDEBUG(D_ERR, "URB packet length is smaller" | |
347 | " than the specified chunk length"); | |
348 | return; | |
349 | } | |
350 | ||
351 | switch (id) { | |
352 | case 0x0200: | |
353 | case 0x4200: | |
354 | PDEBUG(D_PACK, "Frame data packet detected"); | |
355 | ||
356 | gspca_frame_add(gspca_dev, INTER_PACKET, frame, | |
357 | data, chunk_len); | |
358 | break; | |
359 | ||
360 | case 0x8001: | |
361 | case 0x8005: | |
362 | case 0xc001: | |
363 | case 0xc005: | |
364 | PDEBUG(D_PACK, "Starting new frame"); | |
365 | ||
366 | /* Create a new frame, chunk length should be zero */ | |
367 | gspca_frame_add(gspca_dev, FIRST_PACKET, | |
368 | frame, data, 0); | |
369 | ||
370 | if (chunk_len) | |
371 | PDEBUG(D_ERR, "Chunk length is " | |
372 | "non-zero on a SOF"); | |
373 | break; | |
374 | ||
375 | case 0x8002: | |
376 | case 0x8006: | |
377 | case 0xc002: | |
378 | PDEBUG(D_PACK, "End of frame detected"); | |
379 | ||
380 | /* Complete the last frame (if any) */ | |
381 | gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0); | |
382 | ||
383 | if (chunk_len) | |
384 | PDEBUG(D_ERR, "Chunk length is " | |
385 | "non-zero on a EOF"); | |
386 | break; | |
387 | ||
388 | case 0x0005: | |
389 | PDEBUG(D_PACK, "Chunk 0x005 detected"); | |
390 | /* Unknown chunk with 11 bytes of data, | |
391 | occurs just before end of each frame | |
392 | in compressed mode */ | |
393 | break; | |
394 | ||
395 | case 0x0100: | |
396 | PDEBUG(D_PACK, "Chunk 0x0100 detected"); | |
397 | /* Unknown chunk with 2 bytes of data, | |
398 | occurs 2-3 times per USB interrupt */ | |
399 | break; | |
400 | default: | |
401 | PDEBUG(D_PACK, "Unknown chunk %d detected", id); | |
402 | /* Unknown chunk */ | |
403 | } | |
404 | data += chunk_len; | |
405 | len -= chunk_len; | |
406 | } | |
407 | } | |
408 | ||
409 | static int stv06xx_config(struct gspca_dev *gspca_dev, | |
410 | const struct usb_device_id *id); | |
411 | ||
412 | /* sub-driver description */ | |
413 | static const struct sd_desc sd_desc = { | |
414 | .name = MODULE_NAME, | |
415 | .config = stv06xx_config, | |
416 | .init = stv06xx_init, | |
417 | .start = stv06xx_start, | |
418 | .stopN = stv06xx_stopN, | |
419 | .pkt_scan = stv06xx_pkt_scan | |
420 | }; | |
421 | ||
422 | /* This function is called at probe time */ | |
423 | static int stv06xx_config(struct gspca_dev *gspca_dev, | |
424 | const struct usb_device_id *id) | |
425 | { | |
426 | struct sd *sd = (struct sd *) gspca_dev; | |
427 | struct cam *cam; | |
428 | ||
429 | PDEBUG(D_PROBE, "Configuring camera"); | |
430 | ||
431 | cam = &gspca_dev->cam; | |
4c98834a EA |
432 | sd->desc = sd_desc; |
433 | gspca_dev->sd_desc = &sd->desc; | |
434 | ||
435 | if (dump_bridge) | |
436 | stv06xx_dump_bridge(sd); | |
437 | ||
438 | sd->sensor = &stv06xx_sensor_vv6410; | |
439 | if (!sd->sensor->probe(sd)) | |
440 | return 0; | |
441 | ||
442 | sd->sensor = &stv06xx_sensor_hdcs1x00; | |
443 | if (!sd->sensor->probe(sd)) | |
444 | return 0; | |
445 | ||
446 | sd->sensor = &stv06xx_sensor_hdcs1020; | |
447 | if (!sd->sensor->probe(sd)) | |
448 | return 0; | |
449 | ||
450 | sd->sensor = &stv06xx_sensor_pb0100; | |
451 | if (!sd->sensor->probe(sd)) | |
452 | return 0; | |
453 | ||
454 | sd->sensor = NULL; | |
455 | return -ENODEV; | |
456 | } | |
457 | ||
458 | ||
459 | ||
460 | /* -- module initialisation -- */ | |
461 | static const __devinitdata struct usb_device_id device_table[] = { | |
462 | {USB_DEVICE(0x046d, 0x0840)}, /* QuickCam Express */ | |
463 | {USB_DEVICE(0x046d, 0x0850)}, /* LEGO cam / QuickCam Web */ | |
464 | {USB_DEVICE(0x046d, 0x0870)}, /* Dexxa WebCam USB */ | |
465 | {} | |
466 | }; | |
467 | MODULE_DEVICE_TABLE(usb, device_table); | |
468 | ||
469 | /* -- device connect -- */ | |
470 | static int sd_probe(struct usb_interface *intf, | |
471 | const struct usb_device_id *id) | |
472 | { | |
473 | PDEBUG(D_PROBE, "Probing for a stv06xx device"); | |
474 | return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), | |
475 | THIS_MODULE); | |
476 | } | |
477 | ||
8a787b40 | 478 | static void sd_disconnect(struct usb_interface *intf) |
4c98834a EA |
479 | { |
480 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); | |
481 | struct sd *sd = (struct sd *) gspca_dev; | |
482 | PDEBUG(D_PROBE, "Disconnecting the stv06xx device"); | |
483 | ||
484 | if (sd->sensor->disconnect) | |
485 | sd->sensor->disconnect(sd); | |
486 | gspca_disconnect(intf); | |
487 | } | |
488 | ||
489 | static struct usb_driver sd_driver = { | |
490 | .name = MODULE_NAME, | |
491 | .id_table = device_table, | |
492 | .probe = sd_probe, | |
493 | .disconnect = sd_disconnect, | |
494 | #ifdef CONFIG_PM | |
495 | .suspend = gspca_suspend, | |
496 | .resume = gspca_resume, | |
497 | #endif | |
498 | }; | |
499 | ||
500 | /* -- module insert / remove -- */ | |
501 | static int __init sd_mod_init(void) | |
502 | { | |
f69e9529 AK |
503 | int ret; |
504 | ret = usb_register(&sd_driver); | |
505 | if (ret < 0) | |
e6b14849 | 506 | return ret; |
4c98834a EA |
507 | PDEBUG(D_PROBE, "registered"); |
508 | return 0; | |
509 | } | |
510 | static void __exit sd_mod_exit(void) | |
511 | { | |
512 | usb_deregister(&sd_driver); | |
513 | PDEBUG(D_PROBE, "deregistered"); | |
514 | } | |
515 | ||
516 | module_init(sd_mod_init); | |
517 | module_exit(sd_mod_exit); | |
518 | ||
519 | module_param(dump_bridge, bool, S_IRUGO | S_IWUSR); | |
520 | MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup"); | |
521 | ||
522 | module_param(dump_sensor, bool, S_IRUGO | S_IWUSR); | |
523 | MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup"); |