Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / gspca / stv06xx / stv06xx.c
blobb7ea4f982964805ea6234c5e477ddb2719d5e669
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
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)
40 int err;
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)
62 int err;
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);
71 *i2c_data = buf[0];
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
80 single value */
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);
86 } else {
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)
94 int err = 0;
96 if (sd->bridge == BRIDGE_STV610) {
97 struct usb_device *udev = sd->gspca_dev.dev;
98 __u8 *buf = sd->gspca_dev.usb_buf;
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);
106 return (err < 0) ? err : 0;
109 int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
111 int err, i, j;
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",
117 len);
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++) {
122 buf[j] = data[2*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,
132 I2C_BUFFER_LENGTH,
133 STV06XX_URB_MSG_TIMEOUT);
134 if (err < 0)
135 return err;
137 return stv06xx_write_sensor_finish(sd);
140 int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
142 int err, i, j;
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",
148 len);
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++) {
154 buf[j] = data[2*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,
165 I2C_BUFFER_LENGTH,
166 STV06XX_URB_MSG_TIMEOUT);
167 if (err < 0)
168 return err;
170 return stv06xx_write_sensor_finish(sd);
173 int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
175 int err;
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);
181 if (err < 0)
182 return err;
184 /* Clear mem */
185 memset(buf, 0, I2C_BUFFER_LENGTH);
187 buf[0] = address;
188 buf[0x20] = sd->sensor->i2c_addr;
189 buf[0x21] = 0;
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);
197 if (err < 0) {
198 pr_err("I2C: Read error writing address: %d\n", err);
199 return 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);
207 else
208 *value = buf[0];
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)
219 int i;
220 u8 data, buf;
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);
232 buf = data;
234 stv06xx_write_bridge(sd, i, 0xff);
235 stv06xx_read_bridge(sd, i, &data);
236 if (data == 0xff)
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",
241 else
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;
252 int err;
254 gspca_dbg(gspca_dev, D_PROBE, "Initializing camera\n");
256 /* Let the usb init settle for a bit
257 before performing the initialization */
258 msleep(250);
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);
289 if (!alt) {
290 gspca_err(gspca_dev, "Couldn't get altsetting\n");
291 return -EIO;
294 if (alt->desc.bNumEndpoints < 1)
295 return -ENODEV;
297 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
298 err = stv06xx_write_bridge(sd, STV_ISO_SIZE_L, packet_size);
299 if (err < 0)
300 return err;
302 /* Prepare the sensor for start */
303 err = sd->sensor->start(sd);
304 if (err < 0)
305 goto out;
307 /* Start isochronous streaming */
308 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
310 out:
311 if (err < 0)
312 gspca_dbg(gspca_dev, D_STREAM, "Starting stream failed\n");
313 else
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)
328 return -ENODEV;
330 alt = &intfc->altsetting[1];
332 if (alt->desc.bNumEndpoints < 1)
333 return -ENODEV;
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]);
339 return 0;
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)
356 return -EIO;
358 packet_size -= 100;
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);
364 if (ret < 0)
365 gspca_err(gspca_dev, "set alt 1 err %d\n", ret);
367 return ret;
370 static void stv06xx_stopN(struct gspca_dev *gspca_dev)
372 int err;
373 struct sd *sd = (struct sd *) gspca_dev;
375 /* stop ISO-streaming */
376 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
377 if (err < 0)
378 goto out;
380 err = sd->sensor->stop(sd);
382 out:
383 if (err < 0)
384 gspca_dbg(gspca_dev, D_STREAM, "Failed to stop stream\n");
385 else
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 */
411 while (len) {
412 int id, chunk_len;
414 if (len < 4) {
415 gspca_dbg(gspca_dev, D_PACK, "Packet is smaller than 4 bytes\n");
416 return;
419 /* Capture the id */
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",
425 id, chunk_len);
427 data += 4;
428 len -= 4;
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;
433 return;
436 /* First byte seem to be 02=data 2nd byte is unknown??? */
437 if (sd->bridge == BRIDGE_ST6422 && (id & 0xff00) == 0x0200)
438 goto frame_data;
440 switch (id) {
441 case 0x0200:
442 case 0x4200:
443 frame_data:
444 gspca_dbg(gspca_dev, D_PACK, "Frame data packet detected\n");
446 if (sd->to_skip) {
447 int skip = (sd->to_skip < chunk_len) ?
448 sd->to_skip : chunk_len;
449 data += skip;
450 len -= skip;
451 chunk_len -= skip;
452 sd->to_skip -= skip;
455 gspca_frame_add(gspca_dev, INTER_PACKET,
456 data, chunk_len);
457 break;
459 case 0x8001:
460 case 0x8005:
461 case 0xc001:
462 case 0xc005:
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,
467 NULL, 0);
469 if (sd->bridge == BRIDGE_ST6422)
470 sd->to_skip = gspca_dev->pixfmt.width * 4;
472 if (chunk_len)
473 gspca_err(gspca_dev, "Chunk length is non-zero on a SOF\n");
474 break;
476 case 0x8002:
477 case 0x8006:
478 case 0xc002:
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,
483 NULL, 0);
485 if (chunk_len)
486 gspca_err(gspca_dev, "Chunk length is non-zero on a EOF\n");
487 break;
489 case 0x0005:
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 */
494 break;
496 case 0x0100:
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 */
500 break;
501 case 0x42ff:
502 gspca_dbg(gspca_dev, D_PACK, "Chunk 0x42ff detected\n");
503 /* Special chunk seen sometimes on the ST6422 */
504 break;
505 default:
506 gspca_dbg(gspca_dev, D_PACK, "Unknown chunk 0x%04x detected\n",
507 id);
508 /* Unknown chunk */
510 data += chunk_len;
511 len -= chunk_len;
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 */
520 int ret = -EINVAL;
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);
525 ret = 0;
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);
531 ret = 0;
534 return ret;
536 #endif
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 = {
543 .name = MODULE_NAME,
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,
554 #endif
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;
568 if (dump_bridge)
569 stv06xx_dump_bridge(sd);
571 sd->sensor = &stv06xx_sensor_st6422;
572 if (!sd->sensor->probe(sd))
573 return 0;
575 sd->sensor = &stv06xx_sensor_vv6410;
576 if (!sd->sensor->probe(sd))
577 return 0;
579 sd->sensor = &stv06xx_sensor_hdcs1x00;
580 if (!sd->sensor->probe(sd))
581 return 0;
583 sd->sensor = &stv06xx_sensor_hdcs1020;
584 if (!sd->sensor->probe(sd))
585 return 0;
587 sd->sensor = &stv06xx_sensor_pb0100;
588 if (!sd->sensor->probe(sd))
589 return 0;
591 sd->sensor = NULL;
592 return -ENODEV;
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),
614 THIS_MODULE);
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");
624 sd->sensor = NULL;
625 gspca_disconnect(intf);
626 kfree(priv);
629 static struct usb_driver sd_driver = {
630 .name = MODULE_NAME,
631 .id_table = device_table,
632 .probe = sd_probe,
633 .disconnect = sd_disconnect,
634 #ifdef CONFIG_PM
635 .suspend = gspca_suspend,
636 .resume = gspca_resume,
637 .reset_resume = gspca_resume,
638 #endif
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");