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