This source file includes following definitions.
- stv06xx_write_bridge
- stv06xx_read_bridge
- stv06xx_write_sensor
- stv06xx_write_sensor_finish
- stv06xx_write_sensor_bytes
- stv06xx_write_sensor_words
- stv06xx_read_sensor
- stv06xx_dump_bridge
- stv06xx_init
- stv06xx_init_controls
- stv06xx_start
- stv06xx_isoc_init
- stv06xx_isoc_nego
- stv06xx_stopN
- stv06xx_pkt_scan
- sd_int_pkt_scan
- stv06xx_config
- sd_probe
- sd_disconnect
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
22 MODULE_AUTHOR("Erik Andrén");
23 MODULE_DESCRIPTION("STV06XX USB Camera Driver");
24 MODULE_LICENSE("GPL");
25
26 static bool dump_bridge;
27 static bool dump_sensor;
28
29 int 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
51 int 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
72 int 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
83 static 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
100 int 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
131 int 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
164 int 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
208 static 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
240 static 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
260 static 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
271 static 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
301 out:
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
310 static 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
333 static 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
361 static 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
373 out:
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
392 static 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:
434 frame_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)
507 static 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
529 static int stv06xx_config(struct gspca_dev *gspca_dev,
530 const struct usb_device_id *id);
531
532
533 static const struct sd_desc sd_desc = {
534 .name = MODULE_NAME,
535 .config = stv06xx_config,
536 .init = stv06xx_init,
537 .init_controls = stv06xx_init_controls,
538 .start = stv06xx_start,
539 .stopN = stv06xx_stopN,
540 .pkt_scan = stv06xx_pkt_scan,
541 .isoc_init = stv06xx_isoc_init,
542 .isoc_nego = stv06xx_isoc_nego,
543 #if IS_ENABLED(CONFIG_INPUT)
544 .int_pkt_scan = sd_int_pkt_scan,
545 #endif
546 };
547
548
549 static int stv06xx_config(struct gspca_dev *gspca_dev,
550 const struct usb_device_id *id)
551 {
552 struct sd *sd = (struct sd *) gspca_dev;
553
554 gspca_dbg(gspca_dev, D_PROBE, "Configuring camera\n");
555
556 sd->bridge = id->driver_info;
557 gspca_dev->sd_desc = &sd_desc;
558
559 if (dump_bridge)
560 stv06xx_dump_bridge(sd);
561
562 sd->sensor = &stv06xx_sensor_st6422;
563 if (!sd->sensor->probe(sd))
564 return 0;
565
566 sd->sensor = &stv06xx_sensor_vv6410;
567 if (!sd->sensor->probe(sd))
568 return 0;
569
570 sd->sensor = &stv06xx_sensor_hdcs1x00;
571 if (!sd->sensor->probe(sd))
572 return 0;
573
574 sd->sensor = &stv06xx_sensor_hdcs1020;
575 if (!sd->sensor->probe(sd))
576 return 0;
577
578 sd->sensor = &stv06xx_sensor_pb0100;
579 if (!sd->sensor->probe(sd))
580 return 0;
581
582 sd->sensor = NULL;
583 return -ENODEV;
584 }
585
586
587
588
589 static const struct usb_device_id device_table[] = {
590 {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 },
591 {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 },
592 {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 },
593 {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 },
594 {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 },
595 {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 },
596 {}
597 };
598 MODULE_DEVICE_TABLE(usb, device_table);
599
600
601 static int sd_probe(struct usb_interface *intf,
602 const struct usb_device_id *id)
603 {
604 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
605 THIS_MODULE);
606 }
607
608 static void sd_disconnect(struct usb_interface *intf)
609 {
610 struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
611 struct sd *sd = (struct sd *) gspca_dev;
612 void *priv = sd->sensor_priv;
613 gspca_dbg(gspca_dev, D_PROBE, "Disconnecting the stv06xx device\n");
614
615 sd->sensor = NULL;
616 gspca_disconnect(intf);
617 kfree(priv);
618 }
619
620 static struct usb_driver sd_driver = {
621 .name = MODULE_NAME,
622 .id_table = device_table,
623 .probe = sd_probe,
624 .disconnect = sd_disconnect,
625 #ifdef CONFIG_PM
626 .suspend = gspca_suspend,
627 .resume = gspca_resume,
628 .reset_resume = gspca_resume,
629 #endif
630 };
631
632 module_usb_driver(sd_driver);
633
634 module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
635 MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
636
637 module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
638 MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");