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
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
|
/* $OpenBSD: utvfu.c,v 1.3 2016/06/02 17:39:37 mglocker Exp $ */
/*
* Copyright (c) 2013 Lubomir Rintel
* Copyright (c) 2013 Federico Simoncelli
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* Alternatively, this software may be distributed under the terms of the
* GNU General Public License ("GPL").
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Fushicai USBTV007 Audio-Video Grabber Driver
*
* Product web site:
* http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
*
* Following LWN articles were very useful in construction of this driver:
* Video4Linux2 API series: http://lwn.net/Articles/203924/
* videobuf2 API explanation: http://lwn.net/Articles/447435/
* Thanks go to Jonathan Corbet for providing this quality documentation.
* He is awesome.
*
* No physical hardware was harmed running Windows during the
* reverse-engineering activity
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/fcntl.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/audioio.h>
#include <sys/videoio.h>
#include <uvm/uvm_extern.h>
#include <machine/bus.h>
#include <dev/audio_if.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/video_if.h>
#include "utvfu.h"
#ifdef UTVFU_DEBUG
int utvfu_debug = 1;
#define DPRINTF(l, x...) do { if ((l) <= utvfu_debug) printf(x); } while (0)
#else
#define DPRINTF(l, x...)
#endif
#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
struct utvfu_norm_params utvfu_norm_params[] = {
{
.norm = V4L2_STD_525_60,
.cap_width = 720,
.cap_height = 480,
/* 4 bytes/2 pixel YUYV/YUV 4:2:2 */
.frame_len = (720 * 480 * 2),
},
{
.norm = V4L2_STD_PAL,
.cap_width = 720,
.cap_height = 576,
/* 4 bytes/2 pixel YUYV/YUV 4:2:2 */
.frame_len = (720 * 576 * 2),
}
};
int
utvfu_set_regs(struct utvfu_softc *sc, const uint16_t regs[][2], int size)
{
int i;
usbd_status error;
usb_device_request_t req;
DPRINTF(1, "%s: %s: size=%d enter\n", DEVNAME(sc), __func__, size);
req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
req.bRequest = UTVFU_REQUEST_REG;
USETW(req.wLength, 0);
for (i = 0; i < size; i++) {
USETW(req.wIndex, regs[i][0]);
USETW(req.wValue, regs[i][1]);
error = usbd_do_request(sc->sc_udev, &req, NULL);
if (error != USBD_NORMAL_COMPLETION) {
DPRINTF(1, "%s: %s: exit EINVAL\n",
DEVNAME(sc), __func__);
return (EINVAL);
}
}
DPRINTF(1, "%s: %s: exit OK\n", DEVNAME(sc), __func__);
return (0);
}
int
utvfu_max_frame_size(void)
{
int i, sz = 0;
for (i = 0; i < nitems(utvfu_norm_params); i++) {
if (sz < utvfu_norm_params[i].frame_len)
sz = utvfu_norm_params[i].frame_len;
}
return (sz);
}
int
utvfu_configure_for_norm(struct utvfu_softc *sc, v4l2_std_id norm)
{
int i, ret = EINVAL;
struct utvfu_norm_params *params = NULL;
for (i = 0; i < nitems(utvfu_norm_params); i++) {
if (utvfu_norm_params[i].norm & norm) {
params = &utvfu_norm_params[i];
break;
}
}
if (params != NULL) {
sc->sc_normi = i;
sc->sc_nchunks = params->cap_width * params->cap_height
/ 4 / UTVFU_CHUNK;
ret = 0;
}
return (ret);
}
int
utvfu_select_input(struct utvfu_softc *sc, int input)
{
int ret;
static const uint16_t composite[][2] = {
{ UTVFU_BASE + 0x0105, 0x0060 },
{ UTVFU_BASE + 0x011f, 0x00f2 },
{ UTVFU_BASE + 0x0127, 0x0060 },
{ UTVFU_BASE + 0x00ae, 0x0010 },
{ UTVFU_BASE + 0x0239, 0x0060 },
};
static const uint16_t svideo[][2] = {
{ UTVFU_BASE + 0x0105, 0x0010 },
{ UTVFU_BASE + 0x011f, 0x00ff },
{ UTVFU_BASE + 0x0127, 0x0060 },
{ UTVFU_BASE + 0x00ae, 0x0030 },
{ UTVFU_BASE + 0x0239, 0x0060 },
};
switch (input) {
case UTVFU_COMPOSITE_INPUT:
ret = utvfu_set_regs(sc, composite, nitems(composite));
break;
case UTVFU_SVIDEO_INPUT:
ret = utvfu_set_regs(sc, svideo, nitems(svideo));
break;
default:
ret = EINVAL;
}
if (ret == 0)
sc->sc_input = input;
return (ret);
}
int
utvfu_select_norm(struct utvfu_softc *sc, v4l2_std_id norm)
{
int ret;
static const uint16_t pal[][2] = {
{ UTVFU_BASE + 0x001a, 0x0068 },
{ UTVFU_BASE + 0x010e, 0x0072 },
{ UTVFU_BASE + 0x010f, 0x00a2 },
{ UTVFU_BASE + 0x0112, 0x00b0 },
{ UTVFU_BASE + 0x0117, 0x0001 },
{ UTVFU_BASE + 0x0118, 0x002c },
{ UTVFU_BASE + 0x012d, 0x0010 },
{ UTVFU_BASE + 0x012f, 0x0020 },
{ UTVFU_BASE + 0x024f, 0x0002 },
{ UTVFU_BASE + 0x0254, 0x0059 },
{ UTVFU_BASE + 0x025a, 0x0016 },
{ UTVFU_BASE + 0x025b, 0x0035 },
{ UTVFU_BASE + 0x0263, 0x0017 },
{ UTVFU_BASE + 0x0266, 0x0016 },
{ UTVFU_BASE + 0x0267, 0x0036 }
};
static const uint16_t ntsc[][2] = {
{ UTVFU_BASE + 0x001a, 0x0079 },
{ UTVFU_BASE + 0x010e, 0x0068 },
{ UTVFU_BASE + 0x010f, 0x009c },
{ UTVFU_BASE + 0x0112, 0x00f0 },
{ UTVFU_BASE + 0x0117, 0x0000 },
{ UTVFU_BASE + 0x0118, 0x00fc },
{ UTVFU_BASE + 0x012d, 0x0004 },
{ UTVFU_BASE + 0x012f, 0x0008 },
{ UTVFU_BASE + 0x024f, 0x0001 },
{ UTVFU_BASE + 0x0254, 0x005f },
{ UTVFU_BASE + 0x025a, 0x0012 },
{ UTVFU_BASE + 0x025b, 0x0001 },
{ UTVFU_BASE + 0x0263, 0x001c },
{ UTVFU_BASE + 0x0266, 0x0011 },
{ UTVFU_BASE + 0x0267, 0x0005 }
};
ret = utvfu_configure_for_norm(sc, norm);
if (ret == 0) {
if (norm & V4L2_STD_525_60)
ret = utvfu_set_regs(sc, ntsc, nitems(ntsc));
else if (norm & V4L2_STD_PAL)
ret = utvfu_set_regs(sc, pal, nitems(pal));
}
return (ret);
}
int
utvfu_setup_capture(struct utvfu_softc *sc)
{
int ret;
static const uint16_t setup[][2] = {
/* These seem to enable the device. */
{ UTVFU_BASE + 0x0008, 0x0001 },
{ UTVFU_BASE + 0x01d0, 0x00ff },
{ UTVFU_BASE + 0x01d9, 0x0002 },
/*
* These seem to influence color parameters, such as
* brightness, etc.
*/
{ UTVFU_BASE + 0x0239, 0x0040 },
{ UTVFU_BASE + 0x0240, 0x0000 },
{ UTVFU_BASE + 0x0241, 0x0000 },
{ UTVFU_BASE + 0x0242, 0x0002 },
{ UTVFU_BASE + 0x0243, 0x0080 },
{ UTVFU_BASE + 0x0244, 0x0012 },
{ UTVFU_BASE + 0x0245, 0x0090 },
{ UTVFU_BASE + 0x0246, 0x0000 },
{ UTVFU_BASE + 0x0278, 0x002d },
{ UTVFU_BASE + 0x0279, 0x000a },
{ UTVFU_BASE + 0x027a, 0x0032 },
{ 0xf890, 0x000c },
{ 0xf894, 0x0086 },
{ UTVFU_BASE + 0x00ac, 0x00c0 },
{ UTVFU_BASE + 0x00ad, 0x0000 },
{ UTVFU_BASE + 0x00a2, 0x0012 },
{ UTVFU_BASE + 0x00a3, 0x00e0 },
{ UTVFU_BASE + 0x00a4, 0x0028 },
{ UTVFU_BASE + 0x00a5, 0x0082 },
{ UTVFU_BASE + 0x00a7, 0x0080 },
{ UTVFU_BASE + 0x0000, 0x0014 },
{ UTVFU_BASE + 0x0006, 0x0003 },
{ UTVFU_BASE + 0x0090, 0x0099 },
{ UTVFU_BASE + 0x0091, 0x0090 },
{ UTVFU_BASE + 0x0094, 0x0068 },
{ UTVFU_BASE + 0x0095, 0x0070 },
{ UTVFU_BASE + 0x009c, 0x0030 },
{ UTVFU_BASE + 0x009d, 0x00c0 },
{ UTVFU_BASE + 0x009e, 0x00e0 },
{ UTVFU_BASE + 0x0019, 0x0006 },
{ UTVFU_BASE + 0x008c, 0x00ba },
{ UTVFU_BASE + 0x0101, 0x00ff },
{ UTVFU_BASE + 0x010c, 0x00b3 },
{ UTVFU_BASE + 0x01b2, 0x0080 },
{ UTVFU_BASE + 0x01b4, 0x00a0 },
{ UTVFU_BASE + 0x014c, 0x00ff },
{ UTVFU_BASE + 0x014d, 0x00ca },
{ UTVFU_BASE + 0x0113, 0x0053 },
{ UTVFU_BASE + 0x0119, 0x008a },
{ UTVFU_BASE + 0x013c, 0x0003 },
{ UTVFU_BASE + 0x0150, 0x009c },
{ UTVFU_BASE + 0x0151, 0x0071 },
{ UTVFU_BASE + 0x0152, 0x00c6 },
{ UTVFU_BASE + 0x0153, 0x0084 },
{ UTVFU_BASE + 0x0154, 0x00bc },
{ UTVFU_BASE + 0x0155, 0x00a0 },
{ UTVFU_BASE + 0x0156, 0x00a0 },
{ UTVFU_BASE + 0x0157, 0x009c },
{ UTVFU_BASE + 0x0158, 0x001f },
{ UTVFU_BASE + 0x0159, 0x0006 },
{ UTVFU_BASE + 0x015d, 0x0000 },
{ UTVFU_BASE + 0x0003, 0x0004 },
{ UTVFU_BASE + 0x0100, 0x00d3 },
{ UTVFU_BASE + 0x0115, 0x0015 },
{ UTVFU_BASE + 0x0220, 0x002e },
{ UTVFU_BASE + 0x0225, 0x0008 },
{ UTVFU_BASE + 0x024e, 0x0002 },
{ UTVFU_BASE + 0x024e, 0x0002 },
{ UTVFU_BASE + 0x024f, 0x0002 },
};
ret = utvfu_set_regs(sc, setup, nitems(setup));
if (ret)
return (ret);
ret = utvfu_select_norm(sc, utvfu_norm_params[sc->sc_normi].norm);
if (ret)
return (ret);
ret = utvfu_select_input(sc, sc->sc_input);
if (ret)
return (ret);
return (0);
}
/*
* Copy data from chunk into a frame buffer, deinterlacing the data
* into every second line. Unfortunately, they don't align nicely into
* 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
* Therefore, we break down the chunk into two halves before copying,
* so that we can interleave a line if needed.
*
* Each "chunk" is 240 words; a word in this context equals 4 bytes.
* Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
* pixels, the Cr and Cb shared between the two pixels, but each having
* separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
* takes 1.5 chunks to make a 720 pixel-wide line for the frame.
* The image is interlaced, so there is a "scan" of odd lines, followed
* by "scan" of even numbered lines.
*
* Following code is writing the chunks in correct sequence, skipping
* the rows based on "odd" value.
* line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
* line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
* ...etc
*/
void
utvfu_chunk_to_vbuf(uint8_t *frame, uint8_t *src, int chunk_no, int odd)
{
uint8_t *dst;
int half, line, part_no, part_index;
#define UTVFU_STRIDE (UTVFU_CHUNK/2 * 4)
for (half = 0; half < 2; half++) {
part_no = chunk_no * 2 + half;
line = part_no / 3;
part_index = (line * 2 + !odd) * 3 + (part_no % 3);
dst = &frame[part_index * UTVFU_STRIDE];
memcpy(dst, src, UTVFU_STRIDE);
src += UTVFU_STRIDE;
}
#undef UTVFU_STRIDE
}
/*
* Called for each 256-byte image chunk.
* First word identifies the chunk, followed by 240 words of image
* data and padding.
*/
void
utvfu_image_chunk(struct utvfu_softc *sc, u_char *chunk)
{
int frame_id, odd, chunk_no, frame_len;
uint32_t hdr;
memcpy(&hdr, chunk, sizeof(hdr));
chunk += sizeof(hdr);
hdr = be32toh(hdr);
/* Ignore corrupted lines. */
if (!UTVFU_MAGIC_OK(hdr)) {
DPRINTF(2, "%s: bad magic=0x%08x\n",
DEVNAME(sc), UTVFU_MAGIC(hdr));
return;
}
frame_id = UTVFU_FRAME_ID(hdr);
odd = UTVFU_ODD(hdr);
chunk_no = UTVFU_CHUNK_NO(hdr);
if (chunk_no >= sc->sc_nchunks) {
DPRINTF(2, "%s: chunk_no=%d >= sc_nchunks=%d\n",
DEVNAME(sc), chunk_no, sc->sc_nchunks);
return;
}
/* Beginning of a frame. */
if (chunk_no == 0) {
sc->sc_fb.fid = frame_id;
sc->sc_fb.chunks_done = 0;
}
else if (sc->sc_fb.fid != frame_id) {
DPRINTF(2, "%s: frame id mismatch expecting=%d got=%d\n",
DEVNAME(sc), sc->sc_fb.fid, frame_id);
return;
}
frame_len = utvfu_norm_params[sc->sc_normi].frame_len;
/* Copy the chunk data. */
utvfu_chunk_to_vbuf(sc->sc_fb.buf, chunk, chunk_no, odd);
sc->sc_fb.chunks_done++;
/* Last chunk in a field */
if (chunk_no == sc->sc_nchunks-1) {
/* Last chunk in a frame, signalling an end */
if (odd && !sc->sc_fb.last_odd) {
if (sc->sc_fb.chunks_done != sc->sc_nchunks) {
DPRINTF(1, "%s: chunks_done=%d != nchunks=%d\n",
DEVNAME(sc),
sc->sc_fb.chunks_done, sc->sc_nchunks);
}
if (sc->sc_flags & UTVFU_FLAG_MMAP) {
utvfu_mmap_queue(sc, sc->sc_fb.buf, frame_len);
}
else {
utvfu_read(sc, sc->sc_fb.buf, frame_len);
}
}
sc->sc_fb.last_odd = odd;
}
}
int
utvfu_start_capture(struct utvfu_softc *sc)
{
usbd_status error;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
utvfu_audio_stop(sc);
/* default video stream interface */
error = usbd_set_interface(sc->sc_uifaceh, UTVFU_DFLT_IFACE_IDX);
if (error != USBD_NORMAL_COMPLETION)
return (EINVAL);
if (utvfu_setup_capture(sc) != 0)
return (EINVAL);
/* alt setting */
error = usbd_set_interface(sc->sc_uifaceh, UTVFU_ALT_IFACE_IDX);
if (error != USBD_NORMAL_COMPLETION)
return (EINVAL);
utvfu_audio_start(sc);
return (0);
}
int
utvfu_querycap(void *v, struct v4l2_capability *cap)
{
struct utvfu_softc *sc = v;
memset(cap, 0, sizeof(*cap));
strlcpy(cap->driver, DEVNAME(sc), sizeof(cap->driver));
strlcpy(cap->card, "utvfu", sizeof(cap->card));
strlcpy(cap->bus_info, "usb", sizeof(cap->bus_info));
cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
return (0);
}
int
utvfu_enum_input(void *v, struct v4l2_input *i)
{
struct utvfu_softc *sc = v;
switch (i->index) {
case UTVFU_COMPOSITE_INPUT:
strlcpy(i->name, "Composite", sizeof(i->name));
break;
case UTVFU_SVIDEO_INPUT:
strlcpy(i->name, "S-Video", sizeof(i->name));
break;
default:
return (EINVAL);
}
i->type = V4L2_INPUT_TYPE_CAMERA;
i->std = utvfu_norm_params[sc->sc_normi].norm;
return (0);
}
int
utvfu_enum_fmt_vid_cap(void *v, struct v4l2_fmtdesc *f)
{
if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || f->index != 0)
return (EINVAL);
strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
sizeof(f->description));
f->pixelformat = V4L2_PIX_FMT_YUYV;
return (0);
}
int
utvfu_enum_fsizes(void *v, struct v4l2_frmsizeenum *fsizes)
{
struct utvfu_softc *sc = v;
if (fsizes->pixel_format != V4L2_PIX_FMT_YUYV)
return (EINVAL);
fsizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
fsizes->discrete.width = utvfu_norm_params[sc->sc_normi].cap_width;
fsizes->discrete.height = utvfu_norm_params[sc->sc_normi].cap_height;
return (0);
}
int
utvfu_g_fmt(void *v, struct v4l2_format *f)
{
struct utvfu_softc *sc = v;
f->fmt.pix.width = utvfu_norm_params[sc->sc_normi].cap_width;
f->fmt.pix.height = utvfu_norm_params[sc->sc_normi].cap_height;
f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
f->fmt.pix.field = V4L2_FIELD_INTERLACED;
f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
return (0);
}
int
utvfu_s_fmt(void *v, struct v4l2_format *f)
{
if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_YUYV)
return (EINVAL);
return (0);
}
int
utvfu_g_std(void *v, v4l2_std_id *norm)
{
struct utvfu_softc *sc = v;
*norm = utvfu_norm_params[sc->sc_normi].norm;
return (0);
}
int
utvfu_s_std(void *v, v4l2_std_id norm)
{
int ret = EINVAL;
struct utvfu_softc *sc = v;
if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
ret = utvfu_select_norm(sc, norm);
return (ret);
}
int
utvfu_g_input(void *v, int *i)
{
struct utvfu_softc *sc = v;
*i = sc->sc_input;
return (0);
}
int
utvfu_s_input(void *v, int i)
{
return utvfu_select_input(v, i);
}
/* A U D I O */
void
utvfu_audio_decode(struct utvfu_softc *sc, int len)
{
uint8_t *dst, *src;
int n, chunk, ncopied;
if (sc->sc_audio.blksize == 0)
return;
src = KERNADDR(&sc->sc_audio.iface.xfer->dmabuf, 0);
dst = sc->sc_audio.cur;
ncopied = sc->sc_audio.cur - sc->sc_audio.start;
/* b/c region start->end is a multiple blksize chunks */
ncopied %= sc->sc_audio.blksize;
while (len >= UTVFU_CHUNK_SIZE) {
/*
* The header, skipped here, ranges from 0xdd000000 to
* 0xdd0003ff. The 0xdd seems to be the "magic" and
* 0x3ff masks the chunk number.
*/
src += UTVFU_AUDIO_HDRSIZE;
chunk = UTVFU_CHUNK;
while (chunk > 0) {
n = min(chunk, sc->sc_audio.blksize - ncopied);
memcpy(dst, src, n);
dst += n;
src += n;
chunk -= n;
ncopied += n;
if (ncopied >= sc->sc_audio.blksize) {
mtx_enter(&audio_lock);
(*sc->sc_audio.intr)(sc->sc_audio.intr_arg);
mtx_leave(&audio_lock);
ncopied -= sc->sc_audio.blksize;
}
if (dst > sc->sc_audio.end)
dst = sc->sc_audio.start;
}
len -= UTVFU_CHUNK_SIZE; /* _CHUNK + _AUDIO_HDRSIZE */
}
sc->sc_audio.cur = dst;
}
int
utvfu_audio_start_chip(struct utvfu_softc *sc)
{
static const uint16_t setup[][2] = {
/* These seem to enable the device. */
{ UTVFU_BASE + 0x0008, 0x0001 },
{ UTVFU_BASE + 0x01d0, 0x00ff },
{ UTVFU_BASE + 0x01d9, 0x0002 },
{ UTVFU_BASE + 0x01da, 0x0013 },
{ UTVFU_BASE + 0x01db, 0x0012 },
{ UTVFU_BASE + 0x01e9, 0x0002 },
{ UTVFU_BASE + 0x01ec, 0x006c },
{ UTVFU_BASE + 0x0294, 0x0020 },
{ UTVFU_BASE + 0x0255, 0x00cf },
{ UTVFU_BASE + 0x0256, 0x0020 },
{ UTVFU_BASE + 0x01eb, 0x0030 },
{ UTVFU_BASE + 0x027d, 0x00a6 },
{ UTVFU_BASE + 0x0280, 0x0011 },
{ UTVFU_BASE + 0x0281, 0x0040 },
{ UTVFU_BASE + 0x0282, 0x0011 },
{ UTVFU_BASE + 0x0283, 0x0040 },
{ 0xf891, 0x0010 },
/* this sets the input from composite */
{ UTVFU_BASE + 0x0284, 0x00aa },
};
/* starting the stream */
utvfu_set_regs(sc, setup, nitems(setup));
return (0);
}
int
utvfu_audio_stop_chip(struct utvfu_softc *sc)
{
static const uint16_t setup[][2] = {
/*
* The original windows driver sometimes sends also:
* { UTVFU_BASE + 0x00a2, 0x0013 }
* but it seems useless and its real effects are untested at
* the moment.
*/
{ UTVFU_BASE + 0x027d, 0x0000 },
{ UTVFU_BASE + 0x0280, 0x0010 },
{ UTVFU_BASE + 0x0282, 0x0010 },
};
utvfu_set_regs(sc, setup, nitems(setup));
return (0);
}
/*
* Copyright (c) 2008 Robert Nagy <robert@openbsd.org>
* Copyright (c) 2008 Marcus Glocker <mglocker@openbsd.org>
* Copyright (c) 2016 Patrick Keshishian <patrick@boxsoft.com>
*
* 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.
*/
/*
* Heavily based on uvideo.c source.
*/
int utvfu_match(struct device *, void *, void *);
void utvfu_attach(struct device *, struct device *, void *);
int utvfu_detach(struct device *, int);
usbd_status utvfu_parse_desc(struct utvfu_softc *);
void utvfu_vs_close(struct utvfu_softc *);
void utvfu_vs_free_frame(struct utvfu_softc *);
void utvfu_vs_free_isoc(struct utvfu_softc *);
void utvfu_vs_start_isoc_ixfer(struct utvfu_softc *,
struct utvfu_isoc_xfer *);
void utvfu_vs_cb(struct usbd_xfer *, void *, usbd_status);
void utvfu_vs_free(struct utvfu_softc *);
int utvfu_vs_init(struct utvfu_softc *);
int utvfu_vs_alloc_frame(struct utvfu_softc *);
usbd_status utvfu_vs_alloc_isoc(struct utvfu_softc *);
int utvfu_open(void *, int, int *, uint8_t *,
void (*)(void *), void *);
int utvfu_close(void *);
int utvfu_querycap(void *, struct v4l2_capability *);
int utvfu_enum_fmt_vid_cap(void *, struct v4l2_fmtdesc *);
int utvfu_enum_fsizes(void *, struct v4l2_frmsizeenum *);
int utvfu_g_fmt(void *, struct v4l2_format *);
int utvfu_s_fmt(void *, struct v4l2_format *);
int utvfu_g_parm(void *, struct v4l2_streamparm *);
int utvfu_s_parm(void *, struct v4l2_streamparm *);
int utvfu_enum_input(void *, struct v4l2_input *);
int utvfu_s_input(void *, int);
int utvfu_g_input(void *, int *);
int utvfu_reqbufs(void *, struct v4l2_requestbuffers *);
int utvfu_querybuf(void *, struct v4l2_buffer *);
int utvfu_qbuf(void *, struct v4l2_buffer *);
int utvfu_dqbuf(void *, struct v4l2_buffer *);
int utvfu_streamon(void *, int);
int utvfu_streamoff(void *, int);
int utvfu_queryctrl(void *, struct v4l2_queryctrl *);
caddr_t utvfu_mappage(void *, off_t, int);
int utvfu_get_bufsize(void *);
int utvfu_start_read(void *);
int utvfu_as_init(struct utvfu_softc *);
void utvfu_as_free(struct utvfu_softc *);
usbd_status utvfu_as_open(struct utvfu_softc *);
int utvfu_as_alloc_bulk(struct utvfu_softc *);
void utvfu_as_free_bulk(struct utvfu_softc *);
int utvfu_as_start_bulk(struct utvfu_softc *);
void utvfu_as_bulk_thread(void *);
int utvfu_audio_open(void *, int);
void utvfu_audio_close(void *);
int utvfu_audio_query_encoding(void *, struct audio_encoding *);
int utvfu_audio_set_params(void *, int, int,
struct audio_params *, struct audio_params *);
int utvfu_audio_halt_out(void *);
int utvfu_audio_halt_in(void *);
int utvfu_audio_getdev(void *, struct audio_device *);
int utvfu_audio_mixer_set_port(void *, struct mixer_ctrl *);
int utvfu_audio_mixer_get_port(void *, struct mixer_ctrl *);
int utvfu_audio_query_devinfo(void *, struct mixer_devinfo *);
int utvfu_audio_get_props(void *);
int utvfu_audio_trigger_output(void *, void *, void *, int,
void (*)(void *), void *, struct audio_params *);
int utvfu_audio_trigger_input(void *, void *, void *, int,
void (*)(void *), void *, struct audio_params *);
void utvfu_audio_get_default_params(void *, int,
struct audio_params *);
#define UTVFU_AUDIO_HAS_CLIENT(sc) ((sc)->sc_flags & UTVFU_FLAG_AUDIO_CLI)
#define UTVFU_VIDEO_HAS_CLIENT(sc) ((sc)->sc_flags & UTVFU_FLAG_VIDEO_CLI)
struct cfdriver utvfu_cd = {
NULL, "utvfu", DV_DULL
};
const struct cfattach utvfu_ca = {
sizeof(struct utvfu_softc),
utvfu_match,
utvfu_attach,
utvfu_detach,
NULL
};
struct video_hw_if utvfu_vid_hw_if = {
utvfu_open, /* open */
utvfu_close, /* close */
utvfu_querycap, /* VIDIOC_QUERYCAP */
utvfu_enum_fmt_vid_cap, /* VIDIOC_ENUM_FMT */
utvfu_enum_fsizes, /* VIDIOC_ENUM_FRAMESIZES */
NULL, /* VIDIOC_ENUM_FRAMEINTERVALS */
utvfu_s_fmt, /* VIDIOC_S_FMT */
utvfu_g_fmt, /* VIDIOC_G_FMT */
utvfu_s_parm, /* VIDIOC_S_PARM */
utvfu_g_parm, /* VIDIOC_G_PARM */
utvfu_enum_input, /* VIDIOC_ENUMINPUT */
utvfu_s_input, /* VIDIOC_S_INPUT */
utvfu_g_input, /* VIDIOC_G_INPUT */
utvfu_reqbufs, /* VIDIOC_REQBUFS */
utvfu_querybuf, /* VIDIOC_QUERYBUF */
utvfu_qbuf, /* VIDIOC_QBUF */
utvfu_dqbuf, /* VIDIOC_DQBUF */
utvfu_streamon, /* VIDIOC_STREAMON */
utvfu_streamoff, /* VIDIOC_STREAMOFF */
NULL, /* VIDIOC_TRY_FMT */
utvfu_queryctrl, /* VIDIOC_QUERYCTRL */
NULL, /* VIDIOC_G_CTRL */
NULL, /* VIDIOC_S_CTRL */
utvfu_mappage, /* mmap */
utvfu_get_bufsize, /* read */
utvfu_start_read /* start stream for read */
};
struct audio_device utvfu_audio_device = {
"UTVFU Audio", /* name */
"", /* version */
"utvfu" /* config */
};
struct audio_hw_if utvfu_au_hw_if = {
utvfu_audio_open, /* open hardware */
utvfu_audio_close, /* close hardware */
NULL, /* Optional: drain buffers */
utvfu_audio_query_encoding,
utvfu_audio_set_params,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
utvfu_audio_halt_out,
utvfu_audio_halt_in,
NULL,
utvfu_audio_getdev,
NULL,
utvfu_audio_mixer_set_port,
utvfu_audio_mixer_get_port,
utvfu_audio_query_devinfo,
NULL,
NULL,
NULL,
NULL,
utvfu_audio_get_props,
utvfu_audio_trigger_output,
utvfu_audio_trigger_input,
utvfu_audio_get_default_params
};
int
utvfu_match(struct device *parent, void *match, void *aux)
{
struct usb_attach_arg *uaa = aux;
struct usb_descriptor const *ud;
struct usbd_desc_iter iter;
struct usb_interface_descriptor *uid = NULL;
usb_device_descriptor_t *dd;
int ret = UMATCH_NONE;
int nep, nalt;
if (uaa->iface == NULL)
return ret;
dd = usbd_get_device_descriptor(uaa->device);
if (UGETW(dd->idVendor) == USB_VENDOR_FUSHICAI &&
UGETW(dd->idProduct) == USB_PRODUCT_FUSHICAI_USBTV007)
ret = UMATCH_VENDOR_PRODUCT;
/*
* This seems like a fragile check, but the original driver ensures
* there are two alternate settings for the interface, and alternate
* setting 1 has four endpoints.
*
* Comment says "Checks that the device is what we think it is."
*/
nep = nalt = 0;
usbd_desc_iter_init(uaa->device, &iter);
while ((ud = usbd_desc_iter_next(&iter)) != NULL) {
switch (ud->bDescriptorType) {
default:
break;
case UDESC_INTERFACE:
uid = (void *)ud;
if (uid->bInterfaceNumber == 0)
nalt++;
break;
case UDESC_ENDPOINT:
if (uid->bAlternateSetting == 1)
nep++;
break;
}
if (uid != NULL && uid->bInterfaceNumber > 0)
break;
}
if (nalt != 2 || nep != 4)
ret = UMATCH_NONE;
return (ret);
}
void
utvfu_attach(struct device *parent, struct device *self, void *aux)
{
int i;
struct utvfu_softc *sc = (struct utvfu_softc *)self;
struct usb_attach_arg *uaa = aux;
sc->sc_udev = uaa->device;
for (i = 0; i < uaa->nifaces; i++) {
if (usbd_iface_claimed(sc->sc_udev, i))
continue;
usbd_claim_iface(sc->sc_udev, i);
}
utvfu_parse_desc(sc);
/* init mmap queue */
SIMPLEQ_INIT(&sc->sc_mmap_q);
sc->sc_mmap_count = 0;
sc->sc_max_frame_sz = utvfu_max_frame_size();
/* calculate optimal isoc xfer size */
sc->sc_nframes = (sc->sc_max_frame_sz + sc->sc_iface.psize - 1)
/ sc->sc_iface.psize;
if (sc->sc_nframes > UTVFU_NFRAMES_MAX)
sc->sc_nframes = UTVFU_NFRAMES_MAX;
DPRINTF(1, "%s: nframes=%d\n", DEVNAME(sc), sc->sc_nframes);
rw_init(&sc->sc_audio.rwlock, "audiorwl");
sc->sc_audiodev = audio_attach_mi(&utvfu_au_hw_if, sc, &sc->sc_dev);
sc->sc_videodev = video_attach_mi(&utvfu_vid_hw_if, sc, &sc->sc_dev);
}
int
utvfu_detach(struct device *self, int flags)
{
struct utvfu_softc *sc = (struct utvfu_softc *)self;
int rv = 0;
/* Wait for outstanding requests to complete */
usbd_delay_ms(sc->sc_udev, UTVFU_NFRAMES_MAX); /* XXX meh? */
if (sc->sc_videodev != NULL)
rv = config_detach(sc->sc_videodev, flags);
if (sc->sc_audiodev != NULL)
rv += config_detach(sc->sc_audiodev, flags);
sc->sc_flags = 0;
utvfu_as_free(sc);
utvfu_vs_free(sc);
return (rv);
}
usbd_status
utvfu_parse_desc(struct utvfu_softc *sc)
{
int nif, nep;
uint32_t psize;
struct usbd_desc_iter iter;
const struct usb_descriptor *ud;
struct usb_endpoint_descriptor *ued;
struct usb_interface_descriptor *uid = NULL;
nif = nep = 0;
usbd_desc_iter_init(sc->sc_udev, &iter);
while ((ud = usbd_desc_iter_next(&iter)) != NULL) {
if (ud->bDescriptorType != UDESC_INTERFACE)
continue;
/* looking for interface 0, alt-setting 1 */
uid = (void *)ud;
if (uid->bInterfaceNumber > 0)
break;
if (uid->bAlternateSetting == 1)
break;
}
if (uid == NULL || uid->bInterfaceNumber != 0 ||
uid->bAlternateSetting != 1)
goto bad;
/* now looking for endpoint with maximum bandwidth */
while ((ud = usbd_desc_iter_next(&iter)) != NULL) {
if (ud->bDescriptorType != UDESC_ENDPOINT)
break;
ued = (void *)ud;
psize = UGETW(ued->wMaxPacketSize);
psize = UE_GET_SIZE(psize) * (1 + UE_GET_TRANS(psize));
if (psize > sc->sc_iface.psize) {
/* bInterfaceNumber = 0 */
sc->sc_uifaceh = &sc->sc_udev->ifaces[0];
sc->sc_iface.endpoint = ued->bEndpointAddress;
sc->sc_iface.psize = psize;
}
}
if (sc->sc_uifaceh != NULL)
return (USBD_NORMAL_COMPLETION);
bad:
printf("%s: no valid alternate interface found!\n", DEVNAME(sc));
return (USBD_INVAL);
}
int
utvfu_open(void *addr, int flags, int *size, uint8_t *buffer,
void (*intr)(void *), void *arg)
{
struct utvfu_softc *sc = addr;
int rv;
DPRINTF(1, "%s: utvfu_open: sc=%p\n", DEVNAME(sc), sc);
if (usbd_is_dying(sc->sc_udev))
return (EIO);
if ((rv = utvfu_vs_init(sc)) != 0 || (rv = utvfu_as_init(sc)) != 0)
return (rv);
/* pointers to upper video layer */
sc->sc_uplayer_arg = arg;
sc->sc_uplayer_fsize = size;
sc->sc_uplayer_fbuffer = buffer;
sc->sc_uplayer_intr = intr;
sc->sc_flags |= UTVFU_FLAG_VIDEO_CLI;
sc->sc_flags &= ~UTVFU_FLAG_MMAP;
return (0);
}
int
utvfu_close(void *addr)
{
struct utvfu_softc *sc = addr;
DPRINTF(1, "%s: utvfu_close: sc=%p\n", DEVNAME(sc), sc);
/* free & clean up video stream */
utvfu_vs_free(sc);
sc->sc_flags &= ~UTVFU_FLAG_VIDEO_CLI;
if (!UTVFU_AUDIO_HAS_CLIENT(sc))
utvfu_as_free(sc);
return (0);
}
usbd_status
utvfu_as_open(struct utvfu_softc *sc)
{
usb_endpoint_descriptor_t *ed;
usbd_status error;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
if (sc->sc_audio.iface.pipeh != NULL) {
printf("%s: %s called while sc_audio.iface.pipeh not NULL\n",
DEVNAME(sc), __func__);
return (USBD_INVAL);
}
ed = usbd_get_endpoint_descriptor(sc->sc_uifaceh,
UTVFU_AUDIO_ENDP);
if (ed == NULL) {
printf("%s: no endpoint descriptor for AS iface\n",
DEVNAME(sc));
return (USBD_INVAL);
}
DPRINTF(1, "%s: open pipe for ", DEVNAME(sc));
DPRINTF(1, "bEndpointAddress=0x%02x (0x%02x), wMaxPacketSize="
"0x%04x (%d)\n",
UE_GET_ADDR(ed->bEndpointAddress),
UTVFU_AUDIO_ENDP,
UGETW(ed->wMaxPacketSize),
sc->sc_iface.psize);
error = usbd_open_pipe(
sc->sc_uifaceh,
UTVFU_AUDIO_ENDP,
USBD_EXCLUSIVE_USE,
&sc->sc_audio.iface.pipeh);
if (error != USBD_NORMAL_COMPLETION) {
printf("%s: could not open AS pipe: %s\n",
DEVNAME(sc), usbd_errstr(error));
}
return (error);
}
usbd_status
utvfu_vs_open(struct utvfu_softc *sc)
{
usb_endpoint_descriptor_t *ed;
usbd_status error;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
if (sc->sc_iface.pipeh != NULL) {
printf("%s: %s called while sc_iface.pipeh not NULL\n",
DEVNAME(sc), __func__);
return (USBD_INVAL);
}
ed = usbd_get_endpoint_descriptor(sc->sc_uifaceh,
sc->sc_iface.endpoint);
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="
"0x%04x (%d)\n",
UE_GET_ADDR(ed->bEndpointAddress),
sc->sc_iface.endpoint,
UGETW(ed->wMaxPacketSize),
sc->sc_iface.psize);
error = usbd_open_pipe(
sc->sc_uifaceh,
sc->sc_iface.endpoint,
USBD_EXCLUSIVE_USE,
&sc->sc_iface.pipeh);
if (error != USBD_NORMAL_COMPLETION) {
printf("%s: could not open VS pipe: %s\n",
DEVNAME(sc), usbd_errstr(error));
}
return (error);
}
void
utvfu_as_close(struct utvfu_softc *sc)
{
if (sc->sc_audio.iface.pipeh != NULL) {
usbd_abort_pipe(sc->sc_audio.iface.pipeh);
usbd_close_pipe(sc->sc_audio.iface.pipeh);
sc->sc_audio.iface.pipeh = NULL;
}
}
void
utvfu_vs_close(struct utvfu_softc *sc)
{
if (sc->sc_iface.pipeh != NULL) {
usbd_abort_pipe(sc->sc_iface.pipeh);
usbd_close_pipe(sc->sc_iface.pipeh);
sc->sc_iface.pipeh = NULL;
}
/*
* Some devices need time to shutdown before we switch back to
* the default interface (0). Not doing so can leave the device
* back in a undefined condition.
*/
usbd_delay_ms(sc->sc_udev, 100);
/* switch back to default interface (turns off cam LED) */
(void)usbd_set_interface(sc->sc_uifaceh, UTVFU_DFLT_IFACE_IDX);
}
void
utvfu_read(struct utvfu_softc *sc, uint8_t *buf, int len)
{
/*
* Copy video frame to upper layer buffer and call
* upper layer interrupt.
*/
*sc->sc_uplayer_fsize = len;
memcpy(sc->sc_uplayer_fbuffer, buf, len);
(*sc->sc_uplayer_intr)(sc->sc_uplayer_arg);
}
int
utvfu_as_start_bulk(struct utvfu_softc *sc)
{
int error;
if (sc->sc_as_running == 1)
return (0);
if (sc->sc_audio.iface.pipeh == NULL)
return (ENXIO);
sc->sc_as_running = 1;
error = kthread_create(utvfu_as_bulk_thread, sc, NULL, DEVNAME(sc));
if (error) {
sc->sc_as_running = 0;
printf("%s: can't create kernel thread!", DEVNAME(sc));
}
return (error);
}
void
utvfu_as_bulk_thread(void *arg)
{
struct utvfu_softc *sc = arg;
struct utvfu_as_iface *iface;
usbd_status error;
uint32_t actlen;
iface = &sc->sc_audio.iface;
while (sc->sc_as_running) {
usbd_setup_xfer(
iface->xfer,
iface->pipeh,
0,
NULL,
UTVFU_AUDIO_URBSIZE,
USBD_NO_COPY | USBD_SHORT_XFER_OK | USBD_SYNCHRONOUS,
0,
NULL);
error = usbd_transfer(iface->xfer);
if (error != USBD_NORMAL_COMPLETION) {
DPRINTF(1, "%s: error in bulk xfer: %s!\n",
DEVNAME(sc), usbd_errstr(error));
break;
}
usbd_get_xfer_status(iface->xfer, NULL, NULL, &actlen,
NULL);
DPRINTF(2, "%s: *** buffer len = %d\n", DEVNAME(sc), actlen);
if (UTVFU_AUDIO_HAS_CLIENT(sc)) {
rw_enter_read(&sc->sc_audio.rwlock);
utvfu_audio_decode(sc, actlen);
rw_exit_read(&sc->sc_audio.rwlock);
}
}
sc->sc_as_running = 0;
DPRINTF(1, "%s %s: exiting\n", DEVNAME(sc), __func__);
kthread_exit(0);
}
void
utvfu_vs_start_isoc(struct utvfu_softc *sc)
{
int i;
for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++)
utvfu_vs_start_isoc_ixfer(sc, &sc->sc_iface.ixfer[i]);
}
void
utvfu_vs_start_isoc_ixfer(struct utvfu_softc *sc,
struct utvfu_isoc_xfer *ixfer)
{
int i;
usbd_status error;
DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
if (usbd_is_dying(sc->sc_udev))
return;
for (i = 0; i < sc->sc_nframes; i++)
ixfer->size[i] = sc->sc_iface.psize;
usbd_setup_isoc_xfer(
ixfer->xfer,
sc->sc_iface.pipeh,
ixfer,
ixfer->size,
sc->sc_nframes,
USBD_NO_COPY | USBD_SHORT_XFER_OK,
utvfu_vs_cb);
error = usbd_transfer(ixfer->xfer);
if (error && error != USBD_IN_PROGRESS) {
DPRINTF(1, "%s: usbd_transfer error=%s!\n",
DEVNAME(sc), usbd_errstr(error));
}
}
/*
* Each packet contains a number of 256-byte chunks composing the image frame.
*/
void
utvfu_vs_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
{
struct utvfu_isoc_xfer *ixfer = priv;
struct utvfu_softc *sc = ixfer->sc;
int i, off, frame_size;
uint32_t actlen;
uint8_t *frame;
DPRINTF(2, "%s: %s\n", DEVNAME(sc), __func__);
if (status != USBD_NORMAL_COMPLETION) {
DPRINTF(1, "%s: %s: %s\n", DEVNAME(sc), __func__,
usbd_errstr(status));
return;
}
usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
DPRINTF(2, "%s: *** buffer len = %d\n", DEVNAME(sc), actlen);
if (actlen == 0)
goto skip;
frame = KERNADDR(&xfer->dmabuf, 0);
for (i = 0; i < sc->sc_nframes; i++, frame += sc->sc_iface.psize) {
frame_size = ixfer->size[i];
if (frame_size == 0)
/* frame is empty */
continue;
#define CHUNK_STRIDE (UTVFU_CHUNK_SIZE*4)
for (off = 0; off + CHUNK_STRIDE <= frame_size;
off += CHUNK_STRIDE) {
utvfu_image_chunk(sc, frame + off);
}
#undef CHUNK_STRIDE
}
skip: /* setup new transfer */
utvfu_vs_start_isoc_ixfer(sc, ixfer);
}
int
utvfu_find_queued(struct utvfu_softc *sc)
{
int i;
/* find a buffer which is ready for queueing */
for (i = 0; i < sc->sc_mmap_count; i++) {
if (sc->sc_mmap[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE)
continue;
if (sc->sc_mmap[i].v4l2_buf.flags & V4L2_BUF_FLAG_QUEUED)
return (i);
}
return (-1);
}
int
utvfu_mmap_queue(struct utvfu_softc *sc, uint8_t *buf, int len)
{
int i;
if (sc->sc_mmap_count == 0 || sc->sc_mmap_buffer == NULL)
panic("%s: mmap buffers not allocated", __func__);
/* find a buffer which is ready for queueing */
if ((i = utvfu_find_queued(sc)) == -1) {
DPRINTF(2, "%s: mmap queue is full!\n", DEVNAME(sc));
return (ENOMEM);
}
/* copy frame to mmap buffer and report length */
memcpy(sc->sc_mmap[i].buf, buf, len);
sc->sc_mmap[i].v4l2_buf.bytesused = len;
/* timestamp it */
getmicrotime(&sc->sc_mmap[i].v4l2_buf.timestamp);
/* appropriately set/clear flags */
sc->sc_mmap[i].v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED;
sc->sc_mmap[i].v4l2_buf.flags |= V4L2_BUF_FLAG_DONE;
/* queue it */
SIMPLEQ_INSERT_TAIL(&sc->sc_mmap_q, &sc->sc_mmap[i], q_frames);
DPRINTF(2, "%s: %s: frame queued on index %d\n",
DEVNAME(sc), __func__, i);
wakeup(sc);
/*
* In case userland uses poll(2), signal that we have a frame
* ready to dequeue.
*/
(*sc->sc_uplayer_intr)(sc->sc_uplayer_arg);
return (0);
}
caddr_t
utvfu_mappage(void *v, off_t off, int prot)
{
struct utvfu_softc *sc = v;
caddr_t p = NULL;
if (off < sc->sc_mmap_bufsz) {
if ((sc->sc_flags & UTVFU_FLAG_MMAP) == 0)
sc->sc_flags |= UTVFU_FLAG_MMAP;
p = sc->sc_mmap_buffer + off;
}
return (p);
}
int
utvfu_get_bufsize(void *v)
{
struct utvfu_softc *sc = v;
/* YUYV/YUV-422: 4 bytes/2 pixel */
return (utvfu_norm_params[sc->sc_normi].cap_width *
utvfu_norm_params[sc->sc_normi].cap_height * 2);
}
int
utvfu_start_read(void *v)
{
struct utvfu_softc *sc = v;
usbd_status error;
if (sc->sc_flags & UTVFU_FLAG_MMAP)
sc->sc_flags &= ~UTVFU_FLAG_MMAP;
/* open video stream pipe */
error = utvfu_vs_open(sc);
if (error != USBD_NORMAL_COMPLETION)
return (EINVAL);
utvfu_vs_start_isoc(sc);
return (0);
}
void
utvfu_audio_clear_client(struct utvfu_softc *sc)
{
sc->sc_flags &= ~UTVFU_FLAG_AUDIO_CLI;
rw_enter_write(&sc->sc_audio.rwlock);
sc->sc_audio.intr = NULL;
sc->sc_audio.intr_arg = NULL;
sc->sc_audio.start = NULL;
sc->sc_audio.end = NULL;
sc->sc_audio.cur = NULL;
sc->sc_audio.blksize = 0;
rw_exit_write(&sc->sc_audio.rwlock);
}
void
utvfu_as_free(struct utvfu_softc *sc)
{
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
utvfu_as_close(sc);
utvfu_as_free_bulk(sc);
}
void
utvfu_vs_free(struct utvfu_softc *sc)
{
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
utvfu_vs_close(sc);
utvfu_vs_free_isoc(sc);
utvfu_vs_free_frame(sc);
}
int
utvfu_as_init(struct utvfu_softc *sc)
{
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
if (sc->sc_audio.iface.xfer != NULL)
return (0);
/* allocate audio and video stream xfer buffer */
return utvfu_as_alloc_bulk(sc);
}
int
utvfu_vs_init(struct utvfu_softc *sc)
{
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
if (utvfu_start_capture(sc) != 0)
return (EINVAL);
if (utvfu_vs_alloc_isoc(sc) != 0 || utvfu_vs_alloc_frame(sc) != 0)
return (ENOMEM);
return (0);
}
int
utvfu_vs_alloc_frame(struct utvfu_softc *sc)
{
struct utvfu_frame_buf *fb = &sc->sc_fb;
fb->size = sc->sc_max_frame_sz;
fb->buf = malloc(fb->size, M_DEVBUF, M_NOWAIT);
if (fb->buf == NULL) {
printf("%s: can't allocate frame buffer!\n", DEVNAME(sc));
return (ENOMEM);
}
DPRINTF(1, "%s: %s: allocated %d bytes frame buffer\n",
DEVNAME(sc), __func__, fb->size);
fb->chunks_done = 0;
fb->fid = 0;
fb->last_odd = 1;
return (0);
}
void
utvfu_vs_free_frame(struct utvfu_softc *sc)
{
struct utvfu_frame_buf *fb = &sc->sc_fb;
if (fb->buf != NULL) {
free(fb->buf, M_DEVBUF, fb->size);
fb->buf = NULL;
}
if (sc->sc_mmap_buffer != NULL) {
free(sc->sc_mmap_buffer, M_DEVBUF, sc->sc_mmap_bufsz);
sc->sc_mmap_buffer = NULL;
memset(sc->sc_mmap, 0, sizeof(sc->sc_mmap));
}
while (!SIMPLEQ_EMPTY(&sc->sc_mmap_q))
SIMPLEQ_REMOVE_HEAD(&sc->sc_mmap_q, q_frames);
sc->sc_mmap_count = 0;
}
usbd_status
utvfu_vs_alloc_isoc(struct utvfu_softc *sc)
{
int size, i;
void *buf;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++) {
sc->sc_iface.ixfer[i].sc = sc;
sc->sc_iface.ixfer[i].xfer = usbd_alloc_xfer(sc->sc_udev);
if (sc->sc_iface.ixfer[i].xfer == NULL) {
printf("%s: could not allocate isoc VS xfer!\n",
DEVNAME(sc));
return (USBD_NOMEM);
}
size = sc->sc_iface.psize * sc->sc_nframes;
buf = usbd_alloc_buffer(sc->sc_iface.ixfer[i].xfer, size);
if (buf == NULL) {
printf("%s: could not allocate isoc VS buffer!\n",
DEVNAME(sc));
return (USBD_NOMEM);
}
DPRINTF(1, "%s: allocated %d bytes isoc VS xfer buffer\n",
DEVNAME(sc), size);
}
return (USBD_NORMAL_COMPLETION);
}
int
utvfu_as_alloc_bulk(struct utvfu_softc *sc)
{
struct usbd_xfer *xfer;
xfer = usbd_alloc_xfer(sc->sc_udev);
if (xfer == NULL) {
printf("%s: could not allocate bulk AUDIO xfer!\n",
DEVNAME(sc));
return (ENOMEM);
}
if (usbd_alloc_buffer(xfer, UTVFU_AUDIO_URBSIZE) == NULL) {
usbd_free_xfer(xfer);
printf("%s: could not allocate bulk AUDIO buffer!\n",
DEVNAME(sc));
return (ENOMEM);
}
DPRINTF(1, "%s: allocated %d bytes bulk AUDIO xfer buffer\n",
DEVNAME(sc), UTVFU_AUDIO_URBSIZE);
sc->sc_audio.iface.xfer = xfer;
return (0);
}
void
utvfu_vs_free_isoc(struct utvfu_softc *sc)
{
int i;
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
for (i = 0; i < UTVFU_ISOC_TRANSFERS; i++) {
if (sc->sc_iface.ixfer[i].xfer != NULL) {
usbd_free_xfer(sc->sc_iface.ixfer[i].xfer);
sc->sc_iface.ixfer[i].xfer = NULL;
}
}
}
void
utvfu_as_free_bulk(struct utvfu_softc *sc)
{
if (sc->sc_audio.iface.xfer != NULL) {
usbd_free_xfer(sc->sc_audio.iface.xfer);
sc->sc_audio.iface.xfer = NULL;
}
}
int
utvfu_reqbufs(void *v, struct v4l2_requestbuffers *rb)
{
struct utvfu_softc *sc = v;
int i;
DPRINTF(1, "%s: %s: count=%d\n", DEVNAME(sc), __func__, rb->count);
/* We do not support freeing buffers via reqbufs(0) */
if (rb->count == 0)
return (EINVAL);
if (sc->sc_mmap_count > 0 || sc->sc_mmap_buffer != NULL) {
DPRINTF(1, "%s: %s: mmap buffers already allocated\n",
DEVNAME(sc), __func__);
return (EINVAL);
}
/* limit the buffers */
if (rb->count > UTVFU_MAX_BUFFERS)
sc->sc_mmap_count = UTVFU_MAX_BUFFERS;
else
sc->sc_mmap_count = rb->count;
/* allocate the total mmap buffer */
sc->sc_mmap_bufsz = sc->sc_max_frame_sz;
if (INT_MAX / sc->sc_mmap_count < sc->sc_max_frame_sz) /* overflow */
return (ENOMEM);
sc->sc_mmap_bufsz *= sc->sc_mmap_count;
sc->sc_mmap_bufsz = round_page(sc->sc_mmap_bufsz); /* page align */
sc->sc_mmap_buffer = malloc(sc->sc_mmap_bufsz, M_DEVBUF, M_NOWAIT);
if (sc->sc_mmap_buffer == NULL) {
printf("%s: can't allocate mmap buffer!\n", DEVNAME(sc));
return (ENOMEM);
}
DPRINTF(1, "%s: allocated %d bytes mmap buffer\n",
DEVNAME(sc), sc->sc_mmap_bufsz);
/* fill the v4l2_buffer structure */
for (i = 0; i < sc->sc_mmap_count; i++) {
sc->sc_mmap[i].buf = sc->sc_mmap_buffer
+ (i * sc->sc_max_frame_sz);
sc->sc_mmap[i].v4l2_buf.index = i;
sc->sc_mmap[i].v4l2_buf.m.offset = i * sc->sc_max_frame_sz;
sc->sc_mmap[i].v4l2_buf.length = sc->sc_max_frame_sz;
sc->sc_mmap[i].v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
sc->sc_mmap[i].v4l2_buf.sequence = 0;
sc->sc_mmap[i].v4l2_buf.field = V4L2_FIELD_NONE;
sc->sc_mmap[i].v4l2_buf.memory = V4L2_MEMORY_MMAP;
sc->sc_mmap[i].v4l2_buf.flags = V4L2_BUF_FLAG_MAPPED;
DPRINTF(1, "%s: %s: index=%d, offset=%d, length=%d\n",
DEVNAME(sc), __func__,
sc->sc_mmap[i].v4l2_buf.index,
sc->sc_mmap[i].v4l2_buf.m.offset,
sc->sc_mmap[i].v4l2_buf.length);
}
/* tell how many buffers we have really allocated */
rb->count = sc->sc_mmap_count;
return (0);
}
int
utvfu_querybuf(void *v, struct v4l2_buffer *qb)
{
struct utvfu_softc *sc = v;
if (qb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
qb->memory != V4L2_MEMORY_MMAP ||
qb->index >= sc->sc_mmap_count)
return (EINVAL);
memcpy(qb, &sc->sc_mmap[qb->index].v4l2_buf,
sizeof(struct v4l2_buffer));
DPRINTF(1, "%s: %s: index=%d, offset=%d, length=%d\n",
DEVNAME(sc), __func__, qb->index, qb->m.offset, qb->length);
return (0);
}
int
utvfu_qbuf(void *v, struct v4l2_buffer *qb)
{
struct utvfu_softc *sc = v;
if (qb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
qb->memory != V4L2_MEMORY_MMAP ||
qb->index >= sc->sc_mmap_count)
return (EINVAL);
sc->sc_mmap[qb->index].v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE;
sc->sc_mmap[qb->index].v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
sc->sc_mmap[qb->index].v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED;
DPRINTF(2, "%s: %s: buffer on index %d ready for queueing\n",
DEVNAME(sc), __func__, qb->index);
return (0);
}
int
utvfu_dqbuf(void *v, struct v4l2_buffer *dqb)
{
struct utvfu_softc *sc = v;
struct utvfu_mmap *mmap;
int error;
if (dqb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
dqb->memory != V4L2_MEMORY_MMAP)
return (EINVAL);
if (SIMPLEQ_EMPTY(&sc->sc_mmap_q)) {
/* mmap queue is empty, block until first frame is queued */
error = tsleep(sc, 0, "vid_mmap", 10 * hz);
if (error)
return (EINVAL);
}
mmap = SIMPLEQ_FIRST(&sc->sc_mmap_q);
if (mmap == NULL)
panic("utvfu_dqbuf: NULL pointer!");
memcpy(dqb, &mmap->v4l2_buf, sizeof(struct v4l2_buffer));
mmap->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_DONE|V4L2_BUF_FLAG_QUEUED);
mmap->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED;
DPRINTF(2, "%s: %s: frame dequeued from index %d\n",
DEVNAME(sc), __func__, mmap->v4l2_buf.index);
SIMPLEQ_REMOVE_HEAD(&sc->sc_mmap_q, q_frames);
return (0);
}
int
utvfu_streamon(void *v, int type)
{
struct utvfu_softc *sc = v;
usbd_status error;
/* open video stream pipe */
error = utvfu_vs_open(sc);
if (error != USBD_NORMAL_COMPLETION)
return (EINVAL);
utvfu_vs_start_isoc(sc);
return (0);
}
int
utvfu_streamoff(void *v, int type)
{
utvfu_vs_close(v);
return (0);
}
int
utvfu_queryctrl(void *v, struct v4l2_queryctrl *qctrl)
{
qctrl->flags = V4L2_CTRL_FLAG_DISABLED;
return (0);
}
int
utvfu_g_parm(void *v, struct v4l2_streamparm *parm)
{
struct utvfu_softc *sc = v;
if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return (EINVAL);
/*
* XXX Unsure whether there is a way to negotiate this with the
* device, but returning 0 will allow xenocara's video to run
*/
switch (utvfu_norm_params[sc->sc_normi].norm) {
default:
return (EINVAL);
case V4L2_STD_525_60:
parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
parm->parm.capture.capturemode = 0;
parm->parm.capture.timeperframe.numerator = 30;
parm->parm.capture.timeperframe.denominator = 1;
break;
case V4L2_STD_PAL:
parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
parm->parm.capture.capturemode = 0;
parm->parm.capture.timeperframe.numerator = 25;
parm->parm.capture.timeperframe.denominator = 1;
break;
}
return (0);
}
int
utvfu_s_parm(void *v, struct v4l2_streamparm *parm)
{
if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
return (EINVAL);
return (0);
}
/*
* A U D I O O P S
*/
int
utvfu_audio_open(void *v, int flags)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
if ((flags & FWRITE))
return (ENXIO);
if (UTVFU_AUDIO_HAS_CLIENT(sc))
return (EBUSY);
return utvfu_as_init(sc);
}
void
utvfu_audio_close(void *v)
{
struct utvfu_softc *sc = v;
/* Leave the audio thread running if video is streaming */
if (!UTVFU_VIDEO_HAS_CLIENT(sc))
utvfu_as_free(sc);
utvfu_audio_clear_client(sc);
}
int
utvfu_audio_query_encoding(void *v, struct audio_encoding *p)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
if (p->index != 0)
return (EINVAL);
strlcpy(p->name, AudioEslinear_le, sizeof(p->name));
p->encoding = AUDIO_ENCODING_SLINEAR_LE;
p->precision = 16;
p->bps = 2;
p->msb = 1;
p->flags = 0;
return (0);
}
int
utvfu_audio_set_params(void *v, int setmode, int usemode,
struct audio_params *play, struct audio_params *rec)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
/* XXX ? */
play->sample_rate = 0;
play->encoding = AUDIO_ENCODING_NONE;
rec->sample_rate = 48000;
rec->encoding = AUDIO_ENCODING_SLINEAR_LE;
rec->precision = 16;
rec->bps = 2;
rec->msb = 1;
rec->channels = 2;
return (0);
}
int
utvfu_audio_halt_out(void *v)
{
return (EIO);
}
int
utvfu_audio_halt_in(void *v)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s: %s\n", DEVNAME(sc), __func__);
if (!UTVFU_VIDEO_HAS_CLIENT(sc))
utvfu_audio_stop(sc);
utvfu_audio_clear_client(sc);
return (0);
}
int
utvfu_audio_getdev(void *v, struct audio_device *retp)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
*retp = utvfu_audio_device;
return (0);
}
int
utvfu_audio_mixer_set_port(void *v, struct mixer_ctrl *cp)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
if (cp->type != AUDIO_MIXER_ENUM ||
cp->un.ord < 0 || cp->un.ord > 1)
return (EINVAL);
/* XXX TODO */
DPRINTF(1, "%s %s: cp->un.ord=%d\n", DEVNAME(sc), __func__, cp->un.ord);
return (0);
}
int
utvfu_audio_mixer_get_port(void *v, struct mixer_ctrl *cp)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
if (cp->type != AUDIO_MIXER_ENUM ||
cp->un.ord < 0 || cp->un.ord > 1)
return (EINVAL);
/* XXX TODO */
DPRINTF(1, "%s %s: cp->un.ord=%d\n", DEVNAME(sc), __func__, cp->un.ord);
return (0);
}
int
utvfu_audio_query_devinfo(void *v, struct mixer_devinfo *mi)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
DPRINTF(1, "%s %s\n", DEVNAME(sc), __func__);
if (mi->index != 0)
return (EINVAL);
/* XXX SOMEONE WITH AUDIO EXPERTIZE NEEDS TO HELP HERE */
strlcpy(mi->label.name, "mix0-i0", sizeof(mi->label.name));
mi->type = AUDIO_MIXER_ENUM;
mi->un.e.num_mem = 2;
mi->un.e.member[0].ord = 0;
strlcpy(mi->un.e.member[0].label.name, AudioNoff,
sizeof(mi->un.e.member[0].label.name));
mi->un.e.member[1].ord = 1;
strlcpy(mi->un.e.member[1].label.name, AudioNon,
sizeof(mi->un.e.member[1].label.name));
return (0);
}
int
utvfu_audio_get_props(void *v)
{
return (0);
}
void
utvfu_audio_get_default_params(void *v, int mode, struct audio_params *p)
{
if (mode != AUMODE_RECORD)
return;
DPRINTF(1, "%s %s\n", DEVNAME((struct utvfu_softc *)v), __func__);
p->sample_rate = 48000;
p->encoding = AUDIO_ENCODING_SLINEAR_LE;
p->precision = 16;
p->bps = 2;
p->msb = 1;
p->channels = 2;
}
int
utvfu_audio_trigger_output(void *v, void *start, void *end, int blksize,
void (*intr)(void *), void *arg, struct audio_params *param)
{
return (EIO);
}
int
utvfu_audio_trigger_input(void *v, void *start, void *end, int blksize,
void (*intr)(void *), void *arg, struct audio_params *param)
{
struct utvfu_softc *sc = v;
if (usbd_is_dying(sc->sc_udev))
return (EIO);
rw_enter_write(&sc->sc_audio.rwlock);
sc->sc_audio.intr_arg = arg;
sc->sc_audio.intr = intr;
sc->sc_audio.start = start;
sc->sc_audio.end = end;
sc->sc_audio.cur = start;
sc->sc_audio.blksize = blksize;
rw_exit_write(&sc->sc_audio.rwlock);
sc->sc_flags |= UTVFU_FLAG_AUDIO_CLI;
DPRINTF(1, "%s %s: start=%p end=%p diff=%lu blksize=%d\n",
DEVNAME(sc), __func__, start, end,
((u_char *)end - (u_char *)start), blksize);
return utvfu_audio_start(sc);
}
int
utvfu_audio_start(struct utvfu_softc *sc)
{
if (sc->sc_as_running == 1)
return (0);
utvfu_audio_start_chip(sc);
if (sc->sc_audio.iface.pipeh == NULL) {
if (utvfu_as_open(sc) != USBD_NORMAL_COMPLETION)
return (ENOMEM);
}
return utvfu_as_start_bulk(sc);
}
int
utvfu_audio_stop(struct utvfu_softc *sc)
{
if (sc->sc_as_running == 1) {
utvfu_audio_stop_chip(sc);
utvfu_as_close(sc);
}
return (0);
}
|