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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
/* $OpenBSD: uvideo.c,v 1.13 2008/05/16 08:01:39 mglocker Exp $ */
/*
* Copyright (c) 2008 Robert Nagy <robert@openbsd.org>
* Copyright (c) 2008 Marcus Glocker <mglocker@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#include <sys/file.h>
#include <sys/reboot.h>
#include <sys/selinfo.h>
#include <sys/proc.h>
#include <sys/namei.h>
#include <sys/vnode.h>
#include <sys/lock.h>
#include <sys/stat.h>
#include <sys/device.h>
#include <sys/poll.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uvideo.h>
#include <dev/video_if.h>
#define UVIDEO_DEBUG
#ifdef UVIDEO_DEBUG
int uvideo_debug = 1;
#define DPRINTF(l, x...) do { if ((l) <= uvideo_debug) printf(x); } while (0)
#else
#define DPRINTF(l, x...)
#endif
int uvideo_enable(void *);
void uvideo_disable(void *);
int uvideo_open(void *, int);
int uvideo_close(void *);
int uvideo_match(struct device *, void *, void *);
void uvideo_attach(struct device *, struct device *, void *);
int uvideo_detach(struct device *, int);
int uvideo_activate(struct device *, enum devact);
int uvideo_vc_parse_desc(struct uvideo_softc *);
int uvideo_vc_parse_desc_header(struct uvideo_softc *,
const usb_descriptor_t *);
int uvideo_vs_parse_desc(struct uvideo_softc *,
struct usb_attach_arg *, usb_config_descriptor_t *);
int uvideo_vs_parse_desc_input_header(struct uvideo_softc *,
const usb_descriptor_t *);
int uvideo_vs_parse_desc_format(struct uvideo_softc *);
int uvideo_vs_parse_desc_format_mjpeg(struct uvideo_softc *,
const usb_descriptor_t *);
int uvideo_vs_parse_desc_frame(struct uvideo_softc *);
int uvideo_vs_parse_desc_frame_mjpeg(struct uvideo_softc *,
const usb_descriptor_t *);
int uvideo_vs_parse_desc_alt(struct uvideo_softc *,
struct usb_attach_arg *uaa, int, int, int);
int uvideo_vs_set_alt(struct uvideo_softc *,
struct usb_attach_arg *, int, int);
int uvideo_desc_len(const usb_descriptor_t *, int, int, int, int);
usbd_status uvideo_vs_negotation(struct uvideo_softc *);
usbd_status uvideo_vs_set_probe(struct uvideo_softc *, uint8_t *);
usbd_status uvideo_vs_get_probe(struct uvideo_softc *, uint8_t *);
usbd_status uvideo_vs_set_commit(struct uvideo_softc *, uint8_t *);
usbd_status uvideo_vs_alloc_sample(struct uvideo_softc *);
usbd_status uvideo_vs_alloc(struct uvideo_softc *);
usbd_status uvideo_vs_open(struct uvideo_softc *, struct usb_attach_arg *);
void uvideo_vs_start(struct uvideo_softc *);
void uvideo_vs_cb(usbd_xfer_handle, usbd_private_handle,
usbd_status);
int uvideo_vs_decode_stream_header(struct uvideo_softc *,
uint8_t *, int);
#ifdef UVIDEO_DEBUG
void uvideo_dump_desc_all(struct uvideo_softc *);
void uvideo_dump_desc_vc_header(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_input_header(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_input(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_output(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_endpoint(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_interface(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_config(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_cs_endpoint(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_colorformat(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_frame_mjpeg(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_dump_desc_format_mjpeg(struct uvideo_softc *,
const usb_descriptor_t *);
void uvideo_hexdump(void *, int);
int uvideo_debug_file_open(struct uvideo_softc *);
void uvideo_debug_file_write_sample(void *);
#endif
int uvideo_querycap(void *, struct v4l2_capability *);
int uvideo_s_fmt(void *, struct v4l2_format *);
int uvideo_g_fmt(void *, struct v4l2_format *);
int uvideo_reqbufs(void *, struct v4l2_requestbuffers *);
#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
const struct cfattach uvideo_ca = {
sizeof(struct uvideo_softc),
uvideo_match,
uvideo_attach,
uvideo_detach,
uvideo_activate,
};
struct cfdriver uvideo_cd = {
NULL,
"uvideo",
DV_DULL
};
usbd_status
uvideo_usb_request(struct uvideo_softc * sc, u_int8_t type, u_int8_t request,
u_int16_t value, u_int16_t index, u_int16_t length, u_int8_t * data);
struct video_hw_if uvideo_hw_if = {
uvideo_open, /* open */
uvideo_close, /* close */
uvideo_querycap, /* VIDIOC_QUERYCAP */
uvideo_s_fmt, /* VIDIOC_S_FMT */
uvideo_g_fmt, /* VIDIOC_G_FMT */
uvideo_reqbufs, /* VIDIOC_REQBUFS */
NULL, /* VIDIOC_QBUF */
NULL /* VIDIOC_DQBUF */
};
int
uvideo_enable(void *v)
{
struct uvideo_softc *sc = v;
DPRINTF(1, "%s: uvideo_enable sc=%p\n", DEVNAME(sc), sc);
if (sc->sc_dying)
return (EIO);
if (sc->sc_enabled)
return (EBUSY);
sc->sc_enabled = 1;
return (0);
}
void
uvideo_disable(void *v)
{
struct uvideo_softc *sc = v;
DPRINTF(1, "%s: uvideo_disable sc=%p\n", DEVNAME(sc), sc);
if (!sc->sc_enabled) {
printf("uvideo_disable: already disabled!\n");
return;
}
sc->sc_enabled = 0;
}
int
uvideo_open(void *addr, int flags)
{
struct uvideo_softc *sc = addr;
DPRINTF(1, "uvideo_open: sc=%p\n", sc);
if (sc->sc_dying)
return (EIO);
return (0);
}
int
uvideo_close(void *addr)
{
#if 0
struct uvideo_softc *sc = addr;
DPRINTF(1, "uvideo_close: sc=%p\n", sc);
#endif
return (0);
}
int
uvideo_match(struct device * parent, void *match, void *aux)
{
struct usb_attach_arg *uaa = aux;
usb_interface_descriptor_t *id;
if (uaa->iface == NULL)
return (UMATCH_NONE);
id = usbd_get_interface_descriptor(uaa->iface);
if (id == NULL)
return (UMATCH_NONE);
if (id->bInterfaceClass == UICLASS_VIDEO &&
id->bInterfaceSubClass == UISUBCLASS_VIDEOCONTROL)
return (UMATCH_VENDOR_PRODUCT_CONF_IFACE);
return (UMATCH_NONE);
}
void
uvideo_attach(struct device * parent, struct device * self, void *aux)
{
struct uvideo_softc *sc = (struct uvideo_softc *) self;
struct usb_attach_arg *uaa = aux;
usb_config_descriptor_t *cdesc;
usbd_status error;
sc->sc_udev = uaa->device;
/* get the config descriptor */
cdesc = usbd_get_config_descriptor(sc->sc_udev);
if (cdesc == NULL) {
printf("%s: failed to get configuration descriptor\n",
DEVNAME(sc));
return;
}
#ifdef UVIDEO_DEBUG
uvideo_dump_desc_all(sc);
#endif
/* parse video control descriptors */
error = uvideo_vc_parse_desc(sc);
if (error != USBD_NORMAL_COMPLETION)
return;
/* parse video stream descriptors */
error = uvideo_vs_parse_desc(sc, uaa, cdesc);
if (error != USBD_NORMAL_COMPLETION)
return;
/* do device negotation */
error = uvideo_vs_negotation(sc);
if (error != USBD_NORMAL_COMPLETION)
return;
/* open video stream pipe */
error = uvideo_vs_open(sc, uaa);
if (error != USBD_NORMAL_COMPLETION)
return;
/* allocate video stream xfer buffer */
error = uvideo_vs_alloc(sc);
if (error != USBD_NORMAL_COMPLETION)
return;
/* allocate video stream sample buffer */
error = uvideo_vs_alloc_sample(sc);
if (error != USBD_NORMAL_COMPLETION)
return;
#ifdef UVIDEO_DEBUG
uvideo_debug_file_open(sc);
usb_init_task(&sc->sc_task_write, uvideo_debug_file_write_sample, sc);
#endif
uvideo_vs_start(sc);
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, &sc->sc_dev);
DPRINTF(1, "uvideo_attach: doing video_attach_mi\n");
sc->sc_videodev = video_attach_mi(&uvideo_hw_if, sc, &sc->sc_dev);
}
int
uvideo_detach(struct device * self, int flags)
{
struct uvideo_softc *sc = (struct uvideo_softc *)self;
int rv = 0;
/* Wait for outstanding requests to complete */
usbd_delay_ms(sc->sc_udev, UVIDEO_NFRAMES_MAX);
if (sc->sc_videodev != NULL)
rv = config_detach(sc->sc_videodev, flags);
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev);
return (rv);
}
int
uvideo_activate(struct device * self, enum devact act)
{
struct uvideo_softc *sc = (struct uvideo_softc *) self;
int rv = 0;
DPRINTF(1, "uvideo_activate: sc=%p\n", sc);
switch (act) {
case DVACT_ACTIVATE:
break;
case DVACT_DEACTIVATE:
if (sc->sc_videodev != NULL)
config_deactivate(sc->sc_videodev);
sc->sc_dying = 1;
break;
}
return (rv);
}
int
uvideo_vc_parse_desc(struct uvideo_softc *sc)
{
usbd_desc_iter_t iter;
const usb_descriptor_t *desc;
int vc_header_found;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
vc_header_found = 0;
usb_desc_iter_init(sc->sc_udev, &iter);
desc = usb_desc_iter_next(&iter);
while (desc) {
if (desc->bDescriptorType != UDESC_CS_INTERFACE) {
desc = usb_desc_iter_next(&iter);
continue;
}
switch (desc->bDescriptorSubtype) {
case UDESCSUB_VC_HEADER:
if (!uvideo_desc_len(desc, 12, 11, 1, 0))
break;
if (vc_header_found) {
printf("%s: too many VC_HEADERs!\n",
DEVNAME(sc));
return (-1);
}
if (uvideo_vc_parse_desc_header(sc, desc) != 0)
return (-1);
vc_header_found = 1;
break;
/* TODO: which VC descriptors do we need else? */
}
desc = usb_desc_iter_next(&iter);
}
if (vc_header_found == 0) {
printf("%s: no VC_HEADER found!\n", DEVNAME(sc));
return (-1);
}
return (0);
}
int
uvideo_vc_parse_desc_header(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_header_desc *d;
d = (struct usb_video_header_desc *)(uint8_t *)desc;
if (d->bInCollection == 0) {
printf("%s: no VS interface found!\n",
DEVNAME(sc));
return (-1);
}
sc->sc_desc_vc_header.fix = d;
sc->sc_desc_vc_header.baInterfaceNr = (uByte *)(d + 1);
return (0);
}
int
uvideo_vs_parse_desc(struct uvideo_softc *sc, struct usb_attach_arg *uaa,
usb_config_descriptor_t *cdesc)
{
usbd_desc_iter_t iter;
const usb_descriptor_t *desc;
usb_interface_descriptor_t *id;
int i, iface, numalts;
usbd_status error;
DPRINTF(1, "%s: number of total interfaces=%d\n",
DEVNAME(sc), uaa->nifaces);
DPRINTF(1, "%s: number of VS interfaces=%d\n",
DEVNAME(sc), sc->sc_desc_vc_header.fix->bInCollection);
usb_desc_iter_init(sc->sc_udev, &iter);
desc = usb_desc_iter_next(&iter);
while (desc) {
if (desc->bDescriptorType != UDESC_CS_INTERFACE) {
desc = usb_desc_iter_next(&iter);
continue;
}
switch (desc->bDescriptorSubtype) {
case UDESCSUB_VS_INPUT_HEADER:
if (!uvideo_desc_len(desc, 13, 3, 0, 12))
break;
if (uvideo_vs_parse_desc_input_header(sc, desc) != 0)
return (-1);
break;
/* TODO: which VS descriptors do we need else? */
}
desc = usb_desc_iter_next(&iter);
}
/* parse video stream format descriptors */
error = uvideo_vs_parse_desc_format(sc);
if (error != USBD_NORMAL_COMPLETION)
return (error);
/* parse video stream frame descriptors */
error = uvideo_vs_parse_desc_frame(sc);
if (error != USBD_NORMAL_COMPLETION)
return (error);
/* parse interface collection */
for (i = 0; i < sc->sc_desc_vc_header.fix->bInCollection; i++) {
iface = sc->sc_desc_vc_header.baInterfaceNr[i];
id = usbd_get_interface_descriptor(uaa->ifaces[iface]);
if (id == NULL) {
printf("%s: can't get VS interface %d!\n",
DEVNAME(sc), iface);
return (USBD_INVAL);
}
numalts = usbd_get_no_alts(cdesc, id->bInterfaceNumber);
DPRINTF(1, "%s: VS interface %d, ", DEVNAME(sc), i);
DPRINTF(1, "bInterfaceNumber=0x%02x, numalts=%d\n",
id->bInterfaceNumber, numalts);
uvideo_vs_parse_desc_alt(sc, uaa, i, iface, numalts);
}
/* XXX for now always use the first video stream */
sc->sc_vs_curr = &sc->sc_vs_coll[0];
return (USBD_NORMAL_COMPLETION);
}
int
uvideo_vs_parse_desc_input_header(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_input_header_desc *d;
d = (struct usb_video_input_header_desc *)(uint8_t *)desc;
/* on some devices bNumFormats is larger than the truth */
if (d->bNumFormats == 0) {
printf("%s: no INPUT FORMAT descriptors found!\n", DEVNAME(sc));
return (-1);
}
sc->sc_desc_vs_input_header.fix = d;
sc->sc_desc_vs_input_header.bmaControls = (uByte *)(d + 1);
return (0);
}
int
uvideo_vs_parse_desc_format(struct uvideo_softc *sc)
{
usbd_desc_iter_t iter;
const usb_descriptor_t *desc;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
usb_desc_iter_init(sc->sc_udev, &iter);
desc = usb_desc_iter_next(&iter);
while (desc) {
if (desc->bDescriptorType != UDESC_CS_INTERFACE) {
desc = usb_desc_iter_next(&iter);
continue;
}
switch (desc->bDescriptorSubtype) {
case UDESCSUB_VS_FORMAT_MJPEG:
if (desc->bLength == 11) {
uvideo_vs_parse_desc_format_mjpeg(sc, desc);
}
break;
}
desc = usb_desc_iter_next(&iter);
}
return (0);
}
int
uvideo_vs_parse_desc_format_mjpeg(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_format_mjpeg_desc *d;
d = (struct usb_video_format_mjpeg_desc *)(uint8_t *)desc;
if (d->bNumFrameDescriptors == 0) {
printf("%s: no MJPEG frame descriptors found!\n",
DEVNAME(sc));
return (-1);
}
sc->sc_desc_format_mjpeg = d;
return (0);
}
int
uvideo_vs_parse_desc_frame(struct uvideo_softc *sc)
{
usbd_desc_iter_t iter;
const usb_descriptor_t *desc;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
usb_desc_iter_init(sc->sc_udev, &iter);
desc = usb_desc_iter_next(&iter);
while (desc) {
if (desc->bDescriptorType != UDESC_CS_INTERFACE) {
desc = usb_desc_iter_next(&iter);
continue;
}
switch (desc->bDescriptorSubtype) {
case UDESCSUB_VS_FRAME_MJPEG:
if (uvideo_vs_parse_desc_frame_mjpeg(sc, desc) == 0)
return (0);
break;
}
desc = usb_desc_iter_next(&iter);
}
printf("%s: no default frame descriptor found!\n", DEVNAME(sc));
return (1);
}
int
uvideo_vs_parse_desc_frame_mjpeg(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_frame_mjpeg_desc *d;
d = (struct usb_video_frame_mjpeg_desc *)(uint8_t *)desc;
/* choose default frame index */
if (d->bFrameIndex != sc->sc_desc_format_mjpeg->bDefaultFrameIndex)
return (1);
sc->sc_desc_frame_mjpeg = d;
return (0);
}
int
uvideo_vs_parse_desc_alt(struct uvideo_softc *sc, struct usb_attach_arg *uaa,
int vs_nr, int iface, int numalts)
{
struct uvideo_vs_iface *vs;
usb_interface_descriptor_t *id;
usb_endpoint_descriptor_t *ed;
int i;
usbd_status error;
vs = &sc->sc_vs_coll[vs_nr];
for (i = 0; i < numalts; i++) {
error = usbd_set_interface(uaa->ifaces[iface], i);
if (error) {
printf("%s: could not set alternate interface %d!\n",
DEVNAME(sc), i);
return (USBD_INVAL);
}
id = usbd_get_interface_descriptor(uaa->ifaces[iface]);
if (id == NULL)
continue;
DPRINTF(1, "%s: bAlternateSetting=0x%02x, ",
DEVNAME(sc), id->bAlternateSetting);
ed = usbd_interface2endpoint_descriptor(uaa->ifaces[iface], 0);
if (ed == NULL) {
DPRINTF(1, "no endpoint descriptor\n");
continue;
}
DPRINTF(1, "bEndpointAddress=0x%02x, ", ed->bEndpointAddress);
DPRINTF(1, "wMaxPacketSize=%d\n", UGETW(ed->wMaxPacketSize));
if (UGETW(ed->wMaxPacketSize) > vs->max_packet_size) {
vs->ifaceh = uaa->ifaces[iface];
vs->endpoint = ed->bEndpointAddress;
vs->numalts = numalts;
vs->curalt = id->bAlternateSetting;
vs->max_packet_size = UGETW(ed->wMaxPacketSize);
vs->iface = iface;
}
}
/* set back first alternate, otherwise negotation can fail */
error = usbd_set_interface(uaa->ifaces[iface], 0);
if (error) {
printf("%s: could not set alternate interface 0!\n",
DEVNAME(sc));
return (USBD_INVAL);
}
return (USBD_NORMAL_COMPLETION);
}
int
uvideo_vs_set_alt(struct uvideo_softc *sc, struct usb_attach_arg *uaa,
int iface, int max_packet_size)
{
usb_interface_descriptor_t *id;
usb_endpoint_descriptor_t *ed;
int i;
usbd_status error;
for (i = 0; i < sc->sc_vs_curr->numalts; i++) {
error = usbd_set_interface(uaa->ifaces[iface], i);
if (error) {
printf("%s: could not set alternate interface %d!\n",
DEVNAME(sc), i);
return (USBD_INVAL);
}
id = usbd_get_interface_descriptor(uaa->ifaces[iface]);
if (id == NULL)
continue;
ed = usbd_interface2endpoint_descriptor(uaa->ifaces[iface], 0);
if (ed == NULL)
continue;
if (UGETW(ed->wMaxPacketSize) == max_packet_size) {
sc->sc_vs_curr->endpoint = ed->bEndpointAddress;
sc->sc_vs_curr->curalt = id->bAlternateSetting;
sc->sc_vs_curr->max_packet_size =
UGETW(ed->wMaxPacketSize);
DPRINTF(1, "%s: set alternate iface to ", DEVNAME(sc));
DPRINTF(1, "bAlternateSetting=0x%02x\n",
id->bAlternateSetting);
return (USBD_NORMAL_COMPLETION);
}
}
return (USBD_INVAL);
}
/*
* Thanks to the retarded USB Video Class specs there are different
* descriptors types with the same bDescriptorSubtype which makes
* it necessary to differ between those types by doing descriptor
* size dances :-(
*
* size_fix: total size of the fixed structure part
* off_num_elements: offset which tells the number of following elements
* size_element: size of a single element
* off_size_element: if size_element is 0 the element size is taken from
* this offset in the descriptor
*/
int
uvideo_desc_len(const usb_descriptor_t *desc,
int size_fix, int off_num_elements, int size_element, int off_size_element)
{
uint8_t *buf;
int size_elements, size_total;
if (desc->bLength < size_fix)
return (0);
buf = (uint8_t *)desc;
if (size_element == 0)
size_element = buf[off_size_element];
size_elements = buf[off_num_elements] * size_element;
size_total = size_fix + size_elements;
if (desc->bLength == size_total && size_elements != 0)
return (1);
return (0);
}
usbd_status
uvideo_vs_negotation(struct uvideo_softc *sc)
{
struct usb_video_probe_commit *pc;
uint8_t probe_data[34];
usbd_status error;
/* set probe */
bzero(probe_data, sizeof(probe_data));
pc = (struct usb_video_probe_commit *)probe_data;
#if 0
pc->bFormatIndex = 1;
pc->bFrameIndex = 1;
#endif
pc->bFormatIndex = sc->sc_desc_format_mjpeg->bFormatIndex;
pc->bFrameIndex = sc->sc_desc_format_mjpeg->bDefaultFrameIndex;
error = uvideo_vs_set_probe(sc, probe_data);
if (error != USBD_NORMAL_COMPLETION)
return (error);
/* get probe */
bzero(probe_data, sizeof(probe_data));
error = uvideo_vs_get_probe(sc, probe_data);
if (error != USBD_NORMAL_COMPLETION)
return (error);
/* commit */
error = uvideo_vs_set_commit(sc, probe_data);
if (error != USBD_NORMAL_COMPLETION)
return (error);
/* save a copy of probe commit */
bcopy(pc, &sc->sc_desc_probe, sizeof(sc->sc_desc_probe));
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_set_probe(struct uvideo_softc *sc, uint8_t *probe_data)
{
usb_device_request_t req;
usbd_status err;
uint16_t tmp;
struct usb_video_probe_commit *pc;
req.bmRequestType = UVIDEO_SET_IF;
req.bRequest = SET_CUR;
tmp = VS_PROBE_CONTROL;
tmp = tmp << 8;
USETW(req.wValue, tmp);
USETW(req.wIndex, sc->sc_vs_curr->iface);
USETW(req.wLength, 26);
pc = (struct usb_video_probe_commit *)probe_data;
err = usbd_do_request(sc->sc_udev, &req, probe_data);
if (err) {
printf("%s: could not SET probe request: %s\n",
DEVNAME(sc), usbd_errstr(err));
return (USBD_INVAL);
}
DPRINTF(1, "%s: SET probe request successfully\n", DEVNAME(sc));
DPRINTF(1, "bmHint=0x%02x\n", UGETW(pc->bmHint));
DPRINTF(1, "bFormatIndex=0x%02x\n", pc->bFormatIndex);
DPRINTF(1, "bFrameIndex=0x%02x\n", pc->bFrameIndex);
DPRINTF(1, "dwFrameInterval=%d (ns)\n", UGETDW(pc->dwFrameInterval));
DPRINTF(1, "wKeyFrameRate=%d\n", UGETW(pc->wKeyFrameRate));
DPRINTF(1, "wPFrameRate=%d\n", UGETW(pc->wPFrameRate));
DPRINTF(1, "wCompQuality=%d\n", UGETW(pc->wCompQuality));
DPRINTF(1, "wCompWindowSize=0x%04x\n", UGETW(pc->wCompWindowSize));
DPRINTF(1, "wDelay=%d (ms)\n", UGETW(pc->wDelay));
DPRINTF(1, "dwMaxVideoFrameSize=%d (bytes)\n",
UGETDW(pc->dwMaxVideoFrameSize));
DPRINTF(1, "dwMaxPayloadTransferSize=%d (bytes)\n",
UGETDW(pc->dwMaxPayloadTransferSize));
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_get_probe(struct uvideo_softc *sc, uint8_t *probe_data)
{
usb_device_request_t req;
usbd_status err;
uint16_t tmp;
struct usb_video_probe_commit *pc;
req.bmRequestType = UVIDEO_GET_IF;
req.bRequest = GET_CUR;
tmp = VS_PROBE_CONTROL;
tmp = tmp << 8;
USETW(req.wValue, tmp);
USETW(req.wIndex, sc->sc_vs_curr->iface);
USETW(req.wLength, 26);
pc = (struct usb_video_probe_commit *)probe_data;
err = usbd_do_request(sc->sc_udev, &req, probe_data);
if (err) {
printf("%s: could not GET probe request: %s\n",
DEVNAME(sc), usbd_errstr(err));
return (USBD_INVAL);
}
DPRINTF(1, "%s: GET probe request successfully\n", DEVNAME(sc));
DPRINTF(1, "bmHint=0x%02x\n", UGETW(pc->bmHint));
DPRINTF(1, "bFormatIndex=0x%02x\n", pc->bFormatIndex);
DPRINTF(1, "bFrameIndex=0x%02x\n", pc->bFrameIndex);
DPRINTF(1, "dwFrameInterval=%d (ns)\n", UGETDW(pc->dwFrameInterval));
DPRINTF(1, "wKeyFrameRate=%d\n", UGETW(pc->wKeyFrameRate));
DPRINTF(1, "wPFrameRate=%d\n", UGETW(pc->wPFrameRate));
DPRINTF(1, "wCompQuality=%d\n", UGETW(pc->wCompQuality));
DPRINTF(1, "wCompWindowSize=0x%04x\n", UGETW(pc->wCompWindowSize));
DPRINTF(1, "wDelay=%d (ms)\n", UGETW(pc->wDelay));
DPRINTF(1, "dwMaxVideoFrameSize=%d (bytes)\n",
UGETDW(pc->dwMaxVideoFrameSize));
DPRINTF(1, "dwMaxPayloadTransferSize=%d (bytes)\n",
UGETDW(pc->dwMaxPayloadTransferSize));
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_set_commit(struct uvideo_softc *sc, uint8_t *probe_data)
{
usb_device_request_t req;
usbd_status err;
uint16_t tmp;
req.bmRequestType = UVIDEO_SET_IF;
req.bRequest = SET_CUR;
tmp = VS_COMMIT_CONTROL;
tmp = tmp << 8;
USETW(req.wValue, tmp);
USETW(req.wIndex, sc->sc_vs_curr->iface);
USETW(req.wLength, 26);
err = usbd_do_request(sc->sc_udev, &req, probe_data);
if (err) {
printf("%s: could not SET commit request: %s\n",
DEVNAME(sc), usbd_errstr(err));
return (USBD_INVAL);
}
DPRINTF(1, "%s: SET commit request successfully\n", DEVNAME(sc));
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_alloc_sample(struct uvideo_softc *sc)
{
struct uvideo_sample_buffer *fb = &sc->sc_sample_buffer;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
fb->buf = malloc(32000, M_TEMP, M_NOWAIT); /* XXX find proper size */
if (fb->buf == NULL) {
printf("%s: can't allocate sample buffer!\n", DEVNAME(sc));
return (USBD_NOMEM);
}
fb->fragment = 0;
fb->fid = 0;
fb->offset = 0;
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_alloc(struct uvideo_softc *sc)
{
int size;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
sc->sc_vs_curr->sc = sc;
sc->sc_vs_curr->xfer = usbd_alloc_xfer(sc->sc_udev);
if (sc->sc_vs_curr->xfer == NULL) {
printf("%s: could not allocate VS xfer!\n", DEVNAME(sc));
return (USBD_NOMEM);
}
size = sc->sc_vs_curr->max_packet_size * sc->sc_nframes;
sc->sc_vs_curr->buf = usbd_alloc_buffer(sc->sc_vs_curr->xfer, size);
if (sc->sc_vs_curr->buf == NULL) {
printf("%s: could not allocate VS buffer!\n", DEVNAME(sc));
return (USBD_NOMEM);
}
DPRINTF(1, "%s: allocated %d bytes VS xfer buffer\n",
DEVNAME(sc), size);
return (USBD_NORMAL_COMPLETION);
}
usbd_status
uvideo_vs_open(struct uvideo_softc *sc, struct usb_attach_arg *uaa)
{
usb_endpoint_descriptor_t *ed;
usbd_status error;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
error = uvideo_vs_set_alt(sc, uaa, sc->sc_vs_curr->iface,
UGETDW(sc->sc_desc_probe.dwMaxPayloadTransferSize));
if (error != USBD_NORMAL_COMPLETION) {
printf("%s: could not set alternate interface!\n",
DEVNAME(sc));
return (error);
}
ed = usbd_interface2endpoint_descriptor(sc->sc_vs_curr->ifaceh, 0);
if (ed == NULL) {
printf("%s: no endpoint descriptor for VS iface\n",
DEVNAME(sc));
return (USBD_INVAL);
}
DPRINTF(1, "%s: open pipe for ", DEVNAME(sc));
DPRINTF(1, "bEndpointAddress=0x%02x (0x%02x), wMaxPacketSize=%d (%d)\n",
ed->bEndpointAddress,
sc->sc_vs_curr->endpoint,
UGETW(ed->wMaxPacketSize),
sc->sc_vs_curr->max_packet_size);
error = usbd_open_pipe(
sc->sc_vs_curr->ifaceh,
sc->sc_vs_curr->endpoint,
USBD_EXCLUSIVE_USE,
&sc->sc_vs_curr->pipeh);
if (error != USBD_NORMAL_COMPLETION) {
printf("%s: could not open VS pipe: %s\n",
DEVNAME(sc), usbd_errstr(error));
return (error);
}
/* calculate optimal isoc xfer size */
sc->sc_nframes = UVIDEO_SFRAMES_MAX / sc->sc_vs_curr->max_packet_size;
if (sc->sc_nframes > UVIDEO_NFRAMES_MAX)
sc->sc_nframes = UVIDEO_NFRAMES_MAX;
DPRINTF(1, "%s: nframes=%d\n", DEVNAME(sc), sc->sc_nframes);
return (USBD_NORMAL_COMPLETION);
}
void
uvideo_vs_start(struct uvideo_softc *sc)
{
int i;
DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
if (sc->sc_dying)
return;
for (i = 0; i < sc->sc_nframes; i++)
sc->sc_vs_curr->size[i] = sc->sc_vs_curr->max_packet_size;
bzero(sc->sc_vs_curr->buf,
sc->sc_vs_curr->max_packet_size * sc->sc_nframes);
usbd_setup_isoc_xfer(
sc->sc_vs_curr->xfer,
sc->sc_vs_curr->pipeh,
sc->sc_vs_curr,
sc->sc_vs_curr->size,
sc->sc_nframes,
USBD_NO_COPY | USBD_SHORT_XFER_OK,
uvideo_vs_cb);
(void)usbd_transfer(sc->sc_vs_curr->xfer);
}
void
uvideo_vs_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
usbd_status status)
{
struct uvideo_vs_iface *vs = priv;
struct uvideo_softc *sc = vs->sc;
int len, i, frame_size;
uint8_t *frame;
DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
if (status != USBD_NORMAL_COMPLETION) {
printf("%s: %s: %s\n", DEVNAME(sc), __func__,
usbd_errstr(status));
return;
}
usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
DPRINTF(2, "%s: *** buffer len = %d\n", DEVNAME(sc), len);
if (len == 0)
goto skip;
for (i = 0; i < sc->sc_nframes; i++) {
frame = vs->buf + (i * vs->max_packet_size);
frame_size = vs->size[i];
if (frame_size == 0)
/* frame is empty */
continue;
uvideo_vs_decode_stream_header(sc, frame, frame_size);
}
skip: /* setup new transfer */
uvideo_vs_start(sc);
}
int
uvideo_vs_decode_stream_header(struct uvideo_softc *sc, uint8_t *frame,
int frame_size)
{
struct uvideo_sample_buffer *fb = &sc->sc_sample_buffer;
int header_len, header_flags, fragment_len;
if (frame_size < 2)
/* frame too small to contain a valid stream header */
return (-1);
header_len = frame[0];
header_flags = frame[1];
DPRINTF(2, "%s: header_len = %d\n", DEVNAME(sc), header_len);
if (header_len != 12)
/* frame header is 12 bytes long */
return (-1);
if (header_len == frame_size && !(header_flags & UVIDEO_STREAM_EOF)) {
/* stream header without payload and no EOF */
return (-1);
}
DPRINTF(2, "%s: frame_size = %d\n", DEVNAME(sc), frame_size);
if (header_flags & UVIDEO_STREAM_FID) {
DPRINTF(2, "%s: %s: FID ON (0x%02x)\n",
DEVNAME(sc), __func__,
header_flags & UVIDEO_STREAM_FID);
} else {
DPRINTF(2, "%s: %s: FID OFF (0x%02x)\n",
DEVNAME(sc), __func__,
header_flags & UVIDEO_STREAM_FID);
}
if (fb->fragment == 0) {
/* first fragment for a sample */
fb->fragment = 1;
fb->fid = header_flags & UVIDEO_STREAM_FID;
fb->offset = 0;
} else {
/* continues fragment for a sample, check consistency */
if (fb->fid != (header_flags & UVIDEO_STREAM_FID)) {
DPRINTF(1, "%s: %s: wrong FID, ignore last sample!\n",
DEVNAME(sc), __func__);
fb->fragment = 1;
fb->fid = header_flags & UVIDEO_STREAM_FID;
fb->offset = 0;
}
}
/* save sample fragment */
fragment_len = frame_size - header_len;
bcopy(frame + header_len, fb->buf + fb->offset, fragment_len);
fb->offset += fragment_len;
if (header_flags & UVIDEO_STREAM_EOF) {
/* got a full sample */
DPRINTF(2, "%s: %s: EOF (sample size = %d bytes)\n",
DEVNAME(sc), __func__, fb->offset);
#ifdef UVIDEO_DEBUG
/* do the file write in process context */
usb_rem_task(sc->sc_udev, &sc->sc_task_write);
usb_add_task(sc->sc_udev, &sc->sc_task_write);
#endif
fb->fragment = 0;
fb->fid = 0;
// fb->offset = 0;
}
return (0);
}
#ifdef UVIDEO_DEBUG
void
uvideo_dump_desc_all(struct uvideo_softc *sc)
{
usbd_desc_iter_t iter;
const usb_descriptor_t *desc;
usb_desc_iter_init(sc->sc_udev, &iter);
desc = usb_desc_iter_next(&iter);
while (desc) {
printf("bLength=%d\n", desc->bLength);
printf("bDescriptorType=0x%02x", desc->bDescriptorType);
switch (desc->bDescriptorType) {
case UDESC_CS_INTERFACE:
printf(" (CS_INTERFACE)\n");
switch (desc->bDescriptorSubtype) {
case UDESCSUB_VC_HEADER:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
if (uvideo_desc_len(desc, 12, 11, 1, 0)) {
printf(" (UDESCSUB_VC_HEADER)\n");
printf("|\n");
uvideo_dump_desc_vc_header(sc, desc);
break;
}
if (uvideo_desc_len(desc, 13, 3, 0, 12)) {
printf(" (UDESCSUB_VS_INPUT_HEADER)\n");
printf("|\n");
uvideo_dump_desc_input_header(sc, desc);
break;
}
printf(" (unknown)\n");
break;
case UDESCSUB_VC_INPUT_TERMINAL:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VC_INPUT_TERMINAL)\n");
printf("|\n");
uvideo_dump_desc_input(sc, desc);
break;
case UDESCSUB_VC_OUTPUT_TERMINAL:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VC_OUTPUT)\n");
printf("|\n");
uvideo_dump_desc_output(sc, desc);
break;
case UDESCSUB_VC_SELECTOR_UNIT:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VC_SELECTOR_UNIT)\n");
/* TODO */
break;
case UDESCSUB_VC_PROCESSING_UNIT:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VC_PROCESSING_UNIT)\n");
/* TODO */
break;
case UDESCSUB_VC_EXTENSION_UNIT:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
if (desc->bLength == 11) {
printf(" (UDESCSUB_VS_FORMAT_MJPEG)\n");
printf("|\n");
uvideo_dump_desc_format_mjpeg(sc, desc);
} else {
printf(" (UDESCSUB_VC_EXTENSION_UNIT)\n");
/* TODO */
}
break;
case UDESCSUB_VS_FRAME_MJPEG:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VS_FRAME_MJPEG)\n");
if (desc->bLength > 26) {
printf("|\n");
uvideo_dump_desc_frame_mjpeg(sc, desc);
}
break;
case UDESCSUB_VS_COLORFORMAT:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (UDESCSUB_VS_COLORFORMAT)\n");
printf("|\n");
uvideo_dump_desc_colorformat(sc, desc);
break;
}
break;
case UDESC_CS_ENDPOINT:
printf(" (UDESC_CS_ENDPOINT)\n");
switch (desc->bDescriptorSubtype) {
case EP_INTERRUPT:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (EP_INTERRUPT)\n");
printf("|\n");
uvideo_dump_desc_cs_endpoint(sc, desc);
break;
case EP_GENERAL:
printf("bDescriptorSubtype=0x%02x",
desc->bDescriptorSubtype);
printf(" (EP_GENERAL)\n");
printf("|\n");
uvideo_dump_desc_cs_endpoint(sc, desc);
break;
}
break;
case UDESC_CONFIG:
printf(" (UDESC_CONFIG)\n");
printf("|\n");
uvideo_dump_desc_config(sc, desc);
break;
case UDESC_ENDPOINT:
printf(" (UDESC_ENDPOINT)\n");
printf("|\n");
uvideo_dump_desc_endpoint(sc, desc);
break;
case UDESC_INTERFACE:
printf(" (UDESC_INTERFACE)\n");
printf("|\n");
uvideo_dump_desc_interface(sc, desc);
break;
default:
printf(" (unknown)\n");
break;
}
printf("\n");
desc = usb_desc_iter_next(&iter);
}
}
void
uvideo_dump_desc_vc_header(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_header_desc *d;
d = (struct usb_video_header_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bcdUVC=0x%04x\n", UGETW(d->bcdUVC));
printf("wTotalLength=%d\n", UGETW(d->wTotalLength));
printf("dwClockFrequency=%d\n", UGETDW(d->dwClockFrequency));
printf("bInCollection=0x%02x\n", d->bInCollection);
}
void
uvideo_dump_desc_input_header(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_input_header_desc *d;
d = (struct usb_video_input_header_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bNumFormats=%d\n", d->bNumFormats);
printf("wTotalLength=%d\n", UGETW(d->wTotalLength));
printf("bEndpointAddress=0x%02x\n", d->bEndpointAddress);
printf("bmInfo=0x%02x\n", d->bmInfo);
printf("bTerminalLink=0x%02x\n", d->bTerminalLink);
printf("bStillCaptureMethod=0x%02x\n", d->bStillCaptureMethod);
printf("bTriggerSupport=0x%02x\n", d->bTriggerSupport);
printf("bTriggerUsage=0x%02x\n", d->bTriggerUsage);
printf("bControlSize=%d\n", d->bControlSize);
}
void
uvideo_dump_desc_input(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_input_terminal_desc *d;
d = (struct usb_video_input_terminal_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bTerminalID=0x%02x\n", d->bTerminalID);
printf("wTerminalType=0x%04x\n", UGETW(d->wTerminalType));
printf("bAssocTerminal=0x%02x\n", d->bAssocTerminal);
printf("iTerminal=0x%02x\n", d->iTerminal);
}
void
uvideo_dump_desc_output(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_output_terminal_desc *d;
d = (struct usb_video_output_terminal_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bTerminalID=0x%02x\n", d->bTerminalID);
printf("bAssocTerminal=0x%02x\n", d->bAssocTerminal);
printf("bSourceID=0x%02x\n", d->bSourceID);
printf("iTerminal=0x%02x\n", d->iTerminal);
}
void
uvideo_dump_desc_endpoint(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
usb_endpoint_descriptor_t *d;
d = (usb_endpoint_descriptor_t *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bEndpointAddress=0x%02x", d->bEndpointAddress);
if (UE_GET_DIR(d->bEndpointAddress) == UE_DIR_IN)
printf(" (IN)\n");
if (UE_GET_DIR(d->bEndpointAddress) == UE_DIR_OUT)
printf(" (OUT)\n");
printf("bmAttributes=0x%02x", d->bmAttributes);
if (UE_GET_XFERTYPE(d->bmAttributes) == UE_ISOCHRONOUS) {
printf(" (UE_ISOCHRONOUS,");
if (UE_GET_ISO_TYPE(d->bmAttributes) == UE_ISO_ASYNC)
printf(" UE_ISO_ASYNC)\n");
if (UE_GET_ISO_TYPE(d->bmAttributes) == UE_ISO_ADAPT)
printf(" UE_ISO_ADAPT)\n");
if (UE_GET_ISO_TYPE(d->bmAttributes) == UE_ISO_SYNC)
printf(" UE_ISO_SYNC)\n");
}
if (UE_GET_XFERTYPE(d->bmAttributes) == UE_CONTROL)
printf(" (UE_CONTROL)\n");
if (UE_GET_XFERTYPE(d->bmAttributes) == UE_BULK)
printf(" (UE_BULK)\n");
if (UE_GET_XFERTYPE(d->bmAttributes) == UE_INTERRUPT)
printf(" (UE_INTERRUPT)\n");
printf("wMaxPacketSize=%d\n", UGETW(d->wMaxPacketSize));
printf("bInterval=0x%02x\n", d->bInterval);
}
void
uvideo_dump_desc_interface(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
usb_interface_descriptor_t *d;
d = (usb_interface_descriptor_t *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bInterfaceNumber=0x%02x\n", d->bInterfaceNumber);
printf("bAlternateSetting=0x%02x\n", d->bAlternateSetting);
printf("bNumEndpoints=%d\n", d->bNumEndpoints);
printf("bInterfaceClass=0x%02x\n", d->bInterfaceClass);
printf("bInterfaceSubClass=0x%02x\n", d->bInterfaceSubClass);
printf("bInterfaceProtocol=0x%02x\n", d->bInterfaceProtocol);
printf("iInterface=0x%02x\n", d->iInterface);
}
void
uvideo_dump_desc_config(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
usb_config_descriptor_t *d;
d = (usb_config_descriptor_t *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("wTotalLength=%d\n", UGETW(d->wTotalLength));
printf("bNumInterface=0x%02x\n", d->bNumInterface);
printf("bConfigurationValue=0x%02x\n", d->bConfigurationValue);
printf("iConfiguration=0x%02x\n", d->iConfiguration);
printf("bmAttributes=0x%02x\n", d->bmAttributes);
printf("bMaxPower=0x%02x\n", d->bMaxPower);
}
void
uvideo_dump_desc_cs_endpoint(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_vc_endpoint_desc *d;
d = (struct usb_video_vc_endpoint_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("wMaxTransferSize=%d\n", UGETW(d->wMaxTransferSize));
}
void
uvideo_dump_desc_colorformat(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_color_matching_descr *d;
d = (struct usb_video_color_matching_descr *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bColorPrimaries=0x%02x\n", d->bColorPrimaries);
printf("bTransferCharacteristics=0x%02x\n",
d->bTransferCharacteristics);
printf("bMatrixCoefficients=0x%02x\n", d->bMatrixCoefficients);
}
void
uvideo_dump_desc_frame_mjpeg(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_frame_mjpeg_desc *d;
d = (struct usb_video_frame_mjpeg_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bFrameIndex=0x%02x\n", d->bFrameIndex);
printf("bmCapabilities=0x%02x\n", d->bmCapabilities);
printf("wWidth=%d\n", UGETW(d->wWidth));
printf("wHeight=%d\n", UGETW(d->wHeight));
printf("dwMinBitRate=%d\n", UGETDW(d->dwMinBitRate));
printf("dwMaxBitRate=%d\n", UGETDW(d->dwMaxBitRate));
printf("dwMaxVideoFrameBufferSize=%d\n",
UGETDW(d->dwMaxVideoFrameBufferSize));
printf("dwDefaultFrameInterval=%d\n",
UGETDW(d->dwDefaultFrameInterval));
printf("bFrameIntervalType=0x%02x\n", d->bFrameIntervalType);
}
void
uvideo_dump_desc_format_mjpeg(struct uvideo_softc *sc,
const usb_descriptor_t *desc)
{
struct usb_video_format_mjpeg_desc *d;
d = (struct usb_video_format_mjpeg_desc *)(uint8_t *)desc;
printf("bLength=%d\n", d->bLength);
printf("bDescriptorType=0x%02x\n", d->bDescriptorType);
printf("bDescriptorSubtype=0x%02x\n", d->bDescriptorSubtype);
printf("bFormatIndex=0x%02x\n", d->bFormatIndex);
printf("bNumFrameDescriptors=0x%02x\n", d->bNumFrameDescriptors);
printf("bmFlags=0x%02x\n", d->bmFlags);
printf("bDefaultFrameIndex=0x%02x\n", d->bDefaultFrameIndex);
printf("bAspectRatioX=0x%02x\n", d->bAspectRatioX);
printf("bAspectRatioY=0x%02x\n", d->bAspectRatioY);
printf("bmInterlaceFlags=0x%02x\n", d->bmInterlaceFlags);
printf("bCopyProtect=0x%02x\n", d->bCopyProtect);
}
void
uvideo_hexdump(void *buf, int len)
{
int i;
for (i = 0; i < len; i++) {
if (i % 16 == 0)
printf("%s%5i:", i ? "\n" : "", i);
if (i % 4 == 0)
printf(" ");
printf("%02x", (int)*((u_char *)buf + i));
}
printf("\n");
}
int
uvideo_debug_file_open(struct uvideo_softc *sc)
{
struct proc *p = curproc;
struct nameidata nd;
char name[] = "/uvideo.mjpeg";
int error;
NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, name, p);
error = vn_open(&nd, O_CREAT | FWRITE | O_NOFOLLOW, S_IRUSR | S_IWUSR);
if (error) {
return (-1);
}
sc->sc_vp = nd.ni_vp;
VOP_UNLOCK(sc->sc_vp, 0, p);
if (nd.ni_vp->v_type != VREG) {
vn_close(nd.ni_vp, FWRITE, p->p_ucred, p);
return (-1);
}
DPRINTF(1, "%s: %s: created debug file %s\n",
DEVNAME(sc), __func__, name);
return (0);
}
void
uvideo_debug_file_write_sample(void *arg)
{
struct uvideo_softc *sc = arg;
struct uvideo_sample_buffer *sb = &sc->sc_sample_buffer;
struct proc *p = curproc;
int error;
if (sc->sc_vp == NULL) {
printf("%s: %s: no file open!\n", DEVNAME(sc));
return;
}
error = vn_rdwr(UIO_WRITE, sc->sc_vp, sb->buf, sb->offset, (off_t)0,
UIO_SYSSPACE, IO_APPEND|IO_UNIT, p->p_ucred, NULL, p);
if (error)
DPRINTF(1, "vn_rdwr error!\n");
}
#endif
int
uvideo_querycap(void *v, struct v4l2_capability * caps)
{
struct uvideo_softc *sc = v;
bzero(caps, sizeof(caps));
strlcpy(caps->driver, DEVNAME(sc),
sizeof(caps->driver));
strncpy(caps->card, "Generic USB video class device",
sizeof(caps->card));
strncpy(caps->bus_info, "usb", sizeof(caps->bus_info));
caps->version = 1;
caps->capabilities = V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
| V4L2_CAP_READWRITE;
return (0);
}
int
uvideo_s_fmt(void *v, struct v4l2_format * fmt)
{
if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return (EINVAL);
return (0);
}
int
uvideo_g_fmt(void *v, struct v4l2_format * fmt)
{
if (fmt->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return (EINVAL);
return (0);
}
int
uvideo_reqbufs(void *v, struct v4l2_requestbuffers * rb)
{
return (0);
}
|