1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32#include <linux/input.h>
33#include "stv06xx_sensor.h"
34
35MODULE_AUTHOR("Erik Andrén");
36MODULE_DESCRIPTION("STV06XX USB Camera Driver");
37MODULE_LICENSE("GPL");
38
39static bool dump_bridge;
40static bool dump_sensor;
41
42int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data)
43{
44 int err;
45 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
46 struct usb_device *udev = sd->gspca_dev.dev;
47 __u8 *buf = sd->gspca_dev.usb_buf;
48
49 u8 len = (i2c_data > 0xff) ? 2 : 1;
50
51 buf[0] = i2c_data & 0xff;
52 buf[1] = (i2c_data >> 8) & 0xff;
53
54 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
55 0x04, 0x40, address, 0, buf, len,
56 STV06XX_URB_MSG_TIMEOUT);
57
58 PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d",
59 i2c_data, address, err);
60
61 return (err < 0) ? err : 0;
62}
63
64int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data)
65{
66 int err;
67 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
68 struct usb_device *udev = sd->gspca_dev.dev;
69 __u8 *buf = sd->gspca_dev.usb_buf;
70
71 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
72 0x04, 0xc0, address, 0, buf, 1,
73 STV06XX_URB_MSG_TIMEOUT);
74
75 *i2c_data = buf[0];
76
77 PDEBUG(D_CONF, "Reading 0x%x from address 0x%x, status %d",
78 *i2c_data, address, err);
79
80 return (err < 0) ? err : 0;
81}
82
83
84
85int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value)
86{
87 if (sd->sensor->i2c_len == 2) {
88 u16 data[2] = { address, value };
89 return stv06xx_write_sensor_words(sd, data, 1);
90 } else {
91 u8 data[2] = { address, value };
92 return stv06xx_write_sensor_bytes(sd, data, 1);
93 }
94}
95
96static int stv06xx_write_sensor_finish(struct sd *sd)
97{
98 int err = 0;
99
100 if (sd->bridge == BRIDGE_STV610) {
101 struct usb_device *udev = sd->gspca_dev.dev;
102 __u8 *buf = sd->gspca_dev.usb_buf;
103
104 buf[0] = 0;
105 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
106 0x04, 0x40, 0x1704, 0, buf, 1,
107 STV06XX_URB_MSG_TIMEOUT);
108 }
109
110 return (err < 0) ? err : 0;
111}
112
113int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
114{
115 int err, i, j;
116 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
117 struct usb_device *udev = sd->gspca_dev.dev;
118 __u8 *buf = sd->gspca_dev.usb_buf;
119
120 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
121 for (i = 0; i < len;) {
122
123 memset(buf, 0, I2C_BUFFER_LENGTH);
124 for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) {
125 buf[j] = data[2*i];
126 buf[0x10 + j] = data[2*i+1];
127 PDEBUG(D_CONF, "I2C: Writing 0x%02x to reg 0x%02x",
128 data[2*i+1], data[2*i]);
129 }
130 buf[0x20] = sd->sensor->i2c_addr;
131 buf[0x21] = j - 1;
132 buf[0x22] = I2C_WRITE_CMD;
133 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
134 0x04, 0x40, 0x0400, 0, buf,
135 I2C_BUFFER_LENGTH,
136 STV06XX_URB_MSG_TIMEOUT);
137 if (err < 0)
138 return err;
139 }
140 return stv06xx_write_sensor_finish(sd);
141}
142
143int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
144{
145 int err, i, j;
146 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
147 struct usb_device *udev = sd->gspca_dev.dev;
148 __u8 *buf = sd->gspca_dev.usb_buf;
149
150 PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
151
152 for (i = 0; i < len;) {
153
154 memset(buf, 0, I2C_BUFFER_LENGTH);
155 for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) {
156 buf[j] = data[2*i];
157 buf[0x10 + j * 2] = data[2*i+1];
158 buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8;
159 PDEBUG(D_CONF, "I2C: Writing 0x%04x to reg 0x%02x",
160 data[2*i+1], data[2*i]);
161 }
162 buf[0x20] = sd->sensor->i2c_addr;
163 buf[0x21] = j - 1;
164 buf[0x22] = I2C_WRITE_CMD;
165 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
166 0x04, 0x40, 0x0400, 0, buf,
167 I2C_BUFFER_LENGTH,
168 STV06XX_URB_MSG_TIMEOUT);
169 if (err < 0)
170 return err;
171 }
172 return stv06xx_write_sensor_finish(sd);
173}
174
175int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
176{
177 int err;
178 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
179 struct usb_device *udev = sd->gspca_dev.dev;
180 __u8 *buf = sd->gspca_dev.usb_buf;
181
182 err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
183 if (err < 0)
184 return err;
185
186
187 memset(buf, 0, I2C_BUFFER_LENGTH);
188
189 buf[0] = address;
190 buf[0x20] = sd->sensor->i2c_addr;
191 buf[0x21] = 0;
192
193
194 buf[0x22] = I2C_READ_CMD;
195
196 err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
197 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
198 STV06XX_URB_MSG_TIMEOUT);
199 if (err < 0) {
200 pr_err("I2C: Read error writing address: %d\n", err);
201 return err;
202 }
203
204 err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
205 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len,
206 STV06XX_URB_MSG_TIMEOUT);
207 if (sd->sensor->i2c_len == 2)
208 *value = buf[0] | (buf[1] << 8);
209 else
210 *value = buf[0];
211
212 PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d",
213 *value, address, err);
214
215 return (err < 0) ? err : 0;
216}
217
218
219static void stv06xx_dump_bridge(struct sd *sd)
220{
221 int i;
222 u8 data, buf;
223
224 pr_info("Dumping all stv06xx bridge registers\n");
225 for (i = 0x1400; i < 0x160f; i++) {
226 stv06xx_read_bridge(sd, i, &data);
227
228 pr_info("Read 0x%x from address 0x%x\n", data, i);
229 }
230
231 pr_info("Testing stv06xx bridge registers for writability\n");
232 for (i = 0x1400; i < 0x160f; i++) {
233 stv06xx_read_bridge(sd, i, &data);
234 buf = data;
235
236 stv06xx_write_bridge(sd, i, 0xff);
237 stv06xx_read_bridge(sd, i, &data);
238 if (data == 0xff)
239 pr_info("Register 0x%x is read/write\n", i);
240 else if (data != buf)
241 pr_info("Register 0x%x is read/write, but only partially\n",
242 i);
243 else
244 pr_info("Register 0x%x is read-only\n", i);
245
246 stv06xx_write_bridge(sd, i, buf);
247 }
248}
249
250
251static int stv06xx_init(struct gspca_dev *gspca_dev)
252{
253 struct sd *sd = (struct sd *) gspca_dev;
254 int err;
255
256 PDEBUG(D_PROBE, "Initializing camera");
257
258
259
260 msleep(250);
261
262 err = sd->sensor->init(sd);
263
264 if (dump_sensor && sd->sensor->dump)
265 sd->sensor->dump(sd);
266
267 return (err < 0) ? err : 0;
268}
269
270
271static int stv06xx_init_controls(struct gspca_dev *gspca_dev)
272{
273 struct sd *sd = (struct sd *) gspca_dev;
274
275 PDEBUG(D_PROBE, "Initializing controls");
276
277 gspca_dev->vdev.ctrl_handler = &gspca_dev->ctrl_handler;
278 return sd->sensor->init_controls(sd);
279}
280
281
282static int stv06xx_start(struct gspca_dev *gspca_dev)
283{
284 struct sd *sd = (struct sd *) gspca_dev;
285 struct usb_host_interface *alt;
286 struct usb_interface *intf;
287 int err, packet_size;
288
289 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
290 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
291 if (!alt) {
292 PERR("Couldn't get altsetting");
293 return -EIO;
294 }
295
296 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
297 err = stv06xx_write_bridge(sd, STV_ISO_SIZE_L, packet_size);
298 if (err < 0)
299 return err;
300
301
302 err = sd->sensor->start(sd);
303 if (err < 0)
304 goto out;
305
306
307 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
308
309out:
310 if (err < 0)
311 PDEBUG(D_STREAM, "Starting stream failed");
312 else
313 PDEBUG(D_STREAM, "Started streaming");
314
315 return (err < 0) ? err : 0;
316}
317
318static int stv06xx_isoc_init(struct gspca_dev *gspca_dev)
319{
320 struct usb_host_interface *alt;
321 struct sd *sd = (struct sd *) gspca_dev;
322
323
324 alt = &gspca_dev->dev->actconfig->intf_cache[0]->altsetting[1];
325 alt->endpoint[0].desc.wMaxPacketSize =
326 cpu_to_le16(sd->sensor->max_packet_size[gspca_dev->curr_mode]);
327
328 return 0;
329}
330
331static int stv06xx_isoc_nego(struct gspca_dev *gspca_dev)
332{
333 int ret, packet_size, min_packet_size;
334 struct usb_host_interface *alt;
335 struct sd *sd = (struct sd *) gspca_dev;
336
337 alt = &gspca_dev->dev->actconfig->intf_cache[0]->altsetting[1];
338 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
339 min_packet_size = sd->sensor->min_packet_size[gspca_dev->curr_mode];
340 if (packet_size <= min_packet_size)
341 return -EIO;
342
343 packet_size -= 100;
344 if (packet_size < min_packet_size)
345 packet_size = min_packet_size;
346 alt->endpoint[0].desc.wMaxPacketSize = cpu_to_le16(packet_size);
347
348 ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
349 if (ret < 0)
350 PERR("set alt 1 err %d", ret);
351
352 return ret;
353}
354
355static void stv06xx_stopN(struct gspca_dev *gspca_dev)
356{
357 int err;
358 struct sd *sd = (struct sd *) gspca_dev;
359
360
361 err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
362 if (err < 0)
363 goto out;
364
365 err = sd->sensor->stop(sd);
366
367out:
368 if (err < 0)
369 PDEBUG(D_STREAM, "Failed to stop stream");
370 else
371 PDEBUG(D_STREAM, "Stopped streaming");
372}
373
374
375
376
377
378
379
380
381
382
383
384
385
386static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
387 u8 *data,
388 int len)
389{
390 struct sd *sd = (struct sd *) gspca_dev;
391
392 PDEBUG(D_PACK, "Packet of length %d arrived", len);
393
394
395
396 while (len) {
397 int id, chunk_len;
398
399 if (len < 4) {
400 PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
401 return;
402 }
403
404
405 id = (data[0] << 8) | data[1];
406
407
408 chunk_len = (data[2] << 8) | data[3];
409 PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len);
410
411 data += 4;
412 len -= 4;
413
414 if (len < chunk_len) {
415 PERR("URB packet length is smaller"
416 " than the specified chunk length");
417 gspca_dev->last_packet_type = DISCARD_PACKET;
418 return;
419 }
420
421
422 if (sd->bridge == BRIDGE_ST6422 && (id & 0xff00) == 0x0200)
423 goto frame_data;
424
425 switch (id) {
426 case 0x0200:
427 case 0x4200:
428frame_data:
429 PDEBUG(D_PACK, "Frame data packet detected");
430
431 if (sd->to_skip) {
432 int skip = (sd->to_skip < chunk_len) ?
433 sd->to_skip : chunk_len;
434 data += skip;
435 len -= skip;
436 chunk_len -= skip;
437 sd->to_skip -= skip;
438 }
439
440 gspca_frame_add(gspca_dev, INTER_PACKET,
441 data, chunk_len);
442 break;
443
444 case 0x8001:
445 case 0x8005:
446 case 0xc001:
447 case 0xc005:
448 PDEBUG(D_PACK, "Starting new frame");
449
450
451 gspca_frame_add(gspca_dev, FIRST_PACKET,
452 NULL, 0);
453
454 if (sd->bridge == BRIDGE_ST6422)
455 sd->to_skip = gspca_dev->width * 4;
456
457 if (chunk_len)
458 PERR("Chunk length is "
459 "non-zero on a SOF");
460 break;
461
462 case 0x8002:
463 case 0x8006:
464 case 0xc002:
465 PDEBUG(D_PACK, "End of frame detected");
466
467
468 gspca_frame_add(gspca_dev, LAST_PACKET,
469 NULL, 0);
470
471 if (chunk_len)
472 PERR("Chunk length is "
473 "non-zero on a EOF");
474 break;
475
476 case 0x0005:
477 PDEBUG(D_PACK, "Chunk 0x005 detected");
478
479
480
481 break;
482
483 case 0x0100:
484 PDEBUG(D_PACK, "Chunk 0x0100 detected");
485
486
487 break;
488 case 0x42ff:
489 PDEBUG(D_PACK, "Chunk 0x42ff detected");
490
491 break;
492 default:
493 PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id);
494
495 }
496 data += chunk_len;
497 len -= chunk_len;
498 }
499}
500
501#if IS_ENABLED(CONFIG_INPUT)
502static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
503 u8 *data,
504 int len)
505{
506 int ret = -EINVAL;
507
508 if (len == 1 && data[0] == 0x80) {
509 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
510 input_sync(gspca_dev->input_dev);
511 ret = 0;
512 }
513
514 if (len == 1 && data[0] == 0x88) {
515 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
516 input_sync(gspca_dev->input_dev);
517 ret = 0;
518 }
519
520 return ret;
521}
522#endif
523
524static int stv06xx_config(struct gspca_dev *gspca_dev,
525 const struct usb_device_id *id);
526
527
528static const struct sd_desc sd_desc = {
529 .name = MODULE_NAME,
530 .config = stv06xx_config,
531 .init = stv06xx_init,
532 .init_controls = stv06xx_init_controls,
533 .start = stv06xx_start,
534 .stopN = stv06xx_stopN,
535 .pkt_scan = stv06xx_pkt_scan,
536 .isoc_init = stv06xx_isoc_init,
537 .isoc_nego = stv06xx_isoc_nego,
538#if IS_ENABLED(CONFIG_INPUT)
539 .int_pkt_scan = sd_int_pkt_scan,
540#endif
541};
542
543
544static int stv06xx_config(struct gspca_dev *gspca_dev,
545 const struct usb_device_id *id)
546{
547 struct sd *sd = (struct sd *) gspca_dev;
548
549 PDEBUG(D_PROBE, "Configuring camera");
550
551 sd->bridge = id->driver_info;
552 gspca_dev->sd_desc = &sd_desc;
553
554 if (dump_bridge)
555 stv06xx_dump_bridge(sd);
556
557 sd->sensor = &stv06xx_sensor_st6422;
558 if (!sd->sensor->probe(sd))
559 return 0;
560
561 sd->sensor = &stv06xx_sensor_vv6410;
562 if (!sd->sensor->probe(sd))
563 return 0;
564
565 sd->sensor = &stv06xx_sensor_hdcs1x00;
566 if (!sd->sensor->probe(sd))
567 return 0;
568
569 sd->sensor = &stv06xx_sensor_hdcs1020;
570 if (!sd->sensor->probe(sd))
571 return 0;
572
573 sd->sensor = &stv06xx_sensor_pb0100;
574 if (!sd->sensor->probe(sd))
575 return 0;
576
577 sd->sensor = NULL;
578 return -ENODEV;
579}
580
581
582
583
584static const struct usb_device_id device_table[] = {
585
586 {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 },
587
588 {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 },
589
590 {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 },
591
592 {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 },
593
594 {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 },
595
596 {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 },
597 {}
598};
599MODULE_DEVICE_TABLE(usb, device_table);
600
601
602static int sd_probe(struct usb_interface *intf,
603 const struct usb_device_id *id)
604{
605 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
606 THIS_MODULE);
607}
608
609static void sd_disconnect(struct usb_interface *intf)
610{
611 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
612 struct sd *sd = (struct sd *) gspca_dev;
613 void *priv = sd->sensor_priv;
614 PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
615
616 sd->sensor = NULL;
617 gspca_disconnect(intf);
618 kfree(priv);
619}
620
621static struct usb_driver sd_driver = {
622 .name = MODULE_NAME,
623 .id_table = device_table,
624 .probe = sd_probe,
625 .disconnect = sd_disconnect,
626#ifdef CONFIG_PM
627 .suspend = gspca_suspend,
628 .resume = gspca_resume,
629 .reset_resume = gspca_resume,
630#endif
631};
632
633module_usb_driver(sd_driver);
634
635module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
636MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
637
638module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
639MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");
640