Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
/* $NetBSD: video.c,v 1.45 2022/03/03 06:23:25 riastradh Exp $ */

/*
 * Copyright (c) 2008 Patrick Mahoney <pat@polycrystal.org>
 * All rights reserved.
 *
 * This code was written by Patrick Mahoney (pat@polycrystal.org) as
 * part of Google Summer of Code 2008.
 *
 * 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.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*
 * This ia a Video4Linux 2 compatible /dev/video driver for NetBSD
 *
 * See http://v4l2spec.bytesex.org/ for Video4Linux 2 specifications
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: video.c,v 1.45 2022/03/03 06:23:25 riastradh Exp $");

#include "video.h"
#if NVIDEO > 0

#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/poll.h>
#include <sys/select.h>
#include <sys/kmem.h>
#include <sys/pool.h>
#include <sys/conf.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/condvar.h>
#include <sys/queue.h>
#include <sys/videoio.h>

#include <dev/video_if.h>

#include "ioconf.h"

/* #define VIDEO_DEBUG 1 */

#ifdef VIDEO_DEBUG
#define	DPRINTF(x)	do { if (videodebug) printf x; } while (0)
#define	DPRINTFN(n,x)	do { if (videodebug>(n)) printf x; } while (0)
int	videodebug = VIDEO_DEBUG;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif

#define PAGE_ALIGN(a)		(((a) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))

#define VIDEO_DRIVER_VERSION				\
	(((__NetBSD_Version__ / 100000000) << 16) |	\
	 ((__NetBSD_Version__ / 1000000 % 100) << 8) |	\
	 (__NetBSD_Version__ / 100 % 100))

/* TODO: move to sys/intr.h */
#define IPL_VIDEO	IPL_VM
#define splvideo()	splvm()

#define VIDEO_MIN_BUFS 2
#define VIDEO_MAX_BUFS 32
#define VIDEO_NUM_BUFS 4

/* Scatter Buffer - an array of fixed size (PAGE_SIZE) chunks
 * allocated non-contiguously and functions to get data into and out
 * of the scatter buffer. */
struct scatter_buf {
	pool_cache_t	sb_pool;
	size_t		sb_size;    /* size in bytes */
	size_t		sb_npages;  /* number of pages */
	uint8_t		**sb_page_ary; /* array of page pointers */
};

struct scatter_io {
	struct scatter_buf *sio_buf;
	off_t		sio_offset;
	size_t		sio_resid;
};

static void	scatter_buf_init(struct scatter_buf *);
static void	scatter_buf_destroy(struct scatter_buf *);
static int	scatter_buf_set_size(struct scatter_buf *, size_t);
static paddr_t	scatter_buf_map(struct scatter_buf *, off_t);

static bool	scatter_io_init(struct scatter_buf *, off_t, size_t, struct scatter_io *);
static bool	scatter_io_next(struct scatter_io *, void **, size_t *);
static void	scatter_io_undo(struct scatter_io *, size_t);
static void	scatter_io_copyin(struct scatter_io *, const void *);
/* static void	scatter_io_copyout(struct scatter_io *, void *); */
static int	scatter_io_uiomove(struct scatter_io *, struct uio *);


enum video_stream_method {
	VIDEO_STREAM_METHOD_NONE,
	VIDEO_STREAM_METHOD_READ,
	VIDEO_STREAM_METHOD_MMAP,
	VIDEO_STREAM_METHOD_USERPTR
};

struct video_buffer {
	struct v4l2_buffer		*vb_buf;
	SIMPLEQ_ENTRY(video_buffer)	entries;
};

SIMPLEQ_HEAD(sample_queue, video_buffer);

struct video_stream {
	int			vs_flags; /* flags given to open() */

	struct video_format	vs_format;

	int			vs_frameno; /* toggles between 0 and 1,
					     * or -1 if new */
	uint32_t		vs_sequence; /* absolute frame/sample number in
					      * sequence, wraps around */
	bool			vs_drop; /* drop payloads from current
					  * frameno? */

	enum v4l2_buf_type	vs_type;
	uint8_t			vs_nbufs;
	struct video_buffer	**vs_buf;

	struct scatter_buf	vs_data; /* stores video data for MMAP
					  * and READ */

	/* Video samples may exist in different locations.  Initially,
	 * samples are queued into the ingress queue.  The driver
	 * grabs these in turn and fills them with video data.  Once
	 * filled, they are moved to the egress queue.  Samples are
	 * dequeued either by user with MMAP method or, with READ
	 * method, videoread() works from the first sample in the
	 * ingress queue without dequeing.  In the first case, the
	 * user re-queues the buffer when finished, and videoread()
	 * does the same when all data has been read.  The sample now
	 * returns to the ingress queue. */
	struct sample_queue	vs_ingress; /* samples under driver control */
	struct sample_queue	vs_egress; /* samples headed for userspace */

	bool			vs_streaming;
	enum video_stream_method vs_method; /* method by which
					     * userspace will read
					     * samples */

	kmutex_t		vs_lock; /* Lock to manipulate queues.
					  * Should also be held when
					  * changing number of
					  * buffers. */
	kcondvar_t		vs_sample_cv; /* signaled on new
					       * ingress sample */
	struct selinfo		vs_sel;

	uint32_t		vs_bytesread; /* bytes read() from current
					       * sample thus far */
};

struct video_softc {
	device_t	sc_dev;
	device_t	hw_dev;	  	 /* Hardware (parent) device */
	void *		hw_softc;	 /* Hardware device private softc */
	const struct video_hw_if *hw_if; /* Hardware interface */

	u_int		sc_open;
	int		sc_refcnt;
	int		sc_opencnt;
	bool		sc_dying;

	struct video_stream sc_stream_in;
};
static int	video_print(void *, const char *);

static int	video_match(device_t, cfdata_t, void *);
static void	video_attach(device_t, device_t, void *);
static int	video_detach(device_t, int);
static int	video_activate(device_t, enum devact);

dev_type_open(videoopen);
dev_type_close(videoclose);
dev_type_read(videoread);
dev_type_write(videowrite);
dev_type_ioctl(videoioctl);
dev_type_poll(videopoll);
dev_type_mmap(videommap);

const struct cdevsw video_cdevsw = {
	.d_open = videoopen,
	.d_close = videoclose,
	.d_read = videoread,
	.d_write = videowrite,
	.d_ioctl = videoioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = videopoll,
	.d_mmap = videommap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = D_OTHER
};

#define VIDEOUNIT(n)	(minor(n))

CFATTACH_DECL_NEW(video, sizeof(struct video_softc),
		  video_match, video_attach, video_detach, video_activate);

static const char *	video_pixel_format_str(enum video_pixel_format);

/* convert various values from V4L2 to native values of this driver */
static uint16_t	v4l2id_to_control_id(uint32_t);
static uint32_t control_flags_to_v4l2flags(uint32_t);
static enum v4l2_ctrl_type control_type_to_v4l2type(enum video_control_type);

static void	v4l2_format_to_video_format(const struct v4l2_format *,
					    struct video_format *);
static void	video_format_to_v4l2_format(const struct video_format *,
					    struct v4l2_format *);
static void	v4l2_standard_to_video_standard(v4l2_std_id,
						enum video_standard *);
static void	video_standard_to_v4l2_standard(enum video_standard,
						struct v4l2_standard *);
static void	v4l2_input_to_video_input(const struct v4l2_input *,
					  struct video_input *);
static void	video_input_to_v4l2_input(const struct video_input *,
					  struct v4l2_input *);
static void	v4l2_audio_to_video_audio(const struct v4l2_audio *,
					  struct video_audio *);
static void	video_audio_to_v4l2_audio(const struct video_audio *,
					  struct v4l2_audio *);
static void	v4l2_tuner_to_video_tuner(const struct v4l2_tuner *,
					  struct video_tuner *);
static void	video_tuner_to_v4l2_tuner(const struct video_tuner *,
					  struct v4l2_tuner *);

/* V4L2 api functions, typically called from videoioctl() */
static int	video_enum_format(struct video_softc *, struct v4l2_fmtdesc *);
static int	video_get_format(struct video_softc *,
				 struct v4l2_format *);
static int	video_set_format(struct video_softc *,
				 struct v4l2_format *);
static int	video_try_format(struct video_softc *,
				 struct v4l2_format *);
static int	video_get_parm(struct video_softc *,
			       struct v4l2_streamparm *);
static int	video_set_parm(struct video_softc *,
			       struct v4l2_streamparm *);
static int	video_enum_standard(struct video_softc *,
				    struct v4l2_standard *);
static int	video_get_standard(struct video_softc *, v4l2_std_id *);
static int	video_set_standard(struct video_softc *, v4l2_std_id);
static int	video_enum_input(struct video_softc *, struct v4l2_input *);
static int	video_get_input(struct video_softc *, int *);
static int	video_set_input(struct video_softc *, int);
static int	video_enum_audio(struct video_softc *, struct v4l2_audio *);
static int	video_get_audio(struct video_softc *, struct v4l2_audio *);
static int	video_set_audio(struct video_softc *, struct v4l2_audio *);
static int	video_get_tuner(struct video_softc *, struct v4l2_tuner *);
static int	video_set_tuner(struct video_softc *, struct v4l2_tuner *);
static int	video_get_frequency(struct video_softc *,
				    struct v4l2_frequency *);
static int	video_set_frequency(struct video_softc *,
				    struct v4l2_frequency *);
static int	video_query_control(struct video_softc *,
				    struct v4l2_queryctrl *);
static int	video_get_control(struct video_softc *,
				  struct v4l2_control *);
static int	video_set_control(struct video_softc *,
				  const struct v4l2_control *);
static int	video_request_bufs(struct video_softc *,
				   struct v4l2_requestbuffers *);
static int	video_query_buf(struct video_softc *, struct v4l2_buffer *);
static int	video_queue_buf(struct video_softc *, struct v4l2_buffer *);
static int	video_dequeue_buf(struct video_softc *, struct v4l2_buffer *);
static int	video_stream_on(struct video_softc *, enum v4l2_buf_type);
static int	video_stream_off(struct video_softc *, enum v4l2_buf_type);

static struct video_buffer *	video_buffer_alloc(void);
static void			video_buffer_free(struct video_buffer *);


/* functions for video_stream */
static void	video_stream_init(struct video_stream *);
static void	video_stream_fini(struct video_stream *);

static int	video_stream_setup_bufs(struct video_stream *,
					enum video_stream_method,
					uint8_t);
static void	video_stream_teardown_bufs(struct video_stream *);

static int	video_stream_realloc_bufs(struct video_stream *, uint8_t);
#define		video_stream_free_bufs(vs) \
	video_stream_realloc_bufs((vs), 0)

static void	video_stream_enqueue(struct video_stream *,
				     struct video_buffer *);
static struct video_buffer * video_stream_dequeue(struct video_stream *);
static void	video_stream_write(struct video_stream *,
				   const struct video_payload *);
static void	video_stream_sample_done(struct video_stream *);

#ifdef VIDEO_DEBUG
/* debugging */
static const char *	video_ioctl_str(u_long);
#endif


static int
video_match(device_t parent, cfdata_t match, void *aux)
{
#ifdef VIDEO_DEBUG
	struct video_attach_args *args;

	args = aux;
	DPRINTF(("video_match: hw=%p\n", args->hw_if));
#endif
	return 1;
}


static void
video_attach(device_t parent, device_t self, void *aux)
{
	struct video_softc *sc;
	struct video_attach_args *args;

	sc = device_private(self);
	args = aux;

	sc->sc_dev = self;
	sc->hw_dev = parent;
	sc->hw_if = args->hw_if;
	sc->hw_softc = args->hw_softc;

	sc->sc_open = 0;
	sc->sc_refcnt = 0;
	sc->sc_opencnt = 0;
	sc->sc_dying = false;

	video_stream_init(&sc->sc_stream_in);

	aprint_naive("\n");
	aprint_normal(": %s\n", sc->hw_if->get_devname(sc->hw_softc));

	DPRINTF(("video_attach: sc=%p hwif=%p\n", sc, sc->hw_if));

	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");
}


static int
video_activate(device_t self, enum devact act)
{
	struct video_softc *sc = device_private(self);

	DPRINTF(("video_activate: sc=%p\n", sc));
	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = true;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}


static int
video_detach(device_t self, int flags)
{
	struct video_softc *sc;
	int maj, mn;

	sc = device_private(self);
	DPRINTF(("video_detach: sc=%p flags=%d\n", sc, flags));

	sc->sc_dying = true;

	pmf_device_deregister(self);

	maj = cdevsw_lookup_major(&video_cdevsw);
	mn = device_unit(self);
	/* close open instances */
	vdevgone(maj, mn, mn, VCHR);

	video_stream_fini(&sc->sc_stream_in);

	return 0;
}


static int
video_print(void *aux, const char *pnp)
{
	if (pnp != NULL) {
		DPRINTF(("video_print: have pnp\n"));
		aprint_normal("%s at %s\n", "video", pnp);
	} else {
		DPRINTF(("video_print: pnp is NULL\n"));
	}
	return UNCONF;
}


/*
 * Called from hardware driver.  This is where the MI audio driver
 * gets probed/attached to the hardware driver.
 */
device_t
video_attach_mi(const struct video_hw_if *hw_if, device_t parent, void *sc)
{
	struct video_attach_args args;

	args.hw_if = hw_if;
	args.hw_softc = sc;
	return config_found(parent, &args, video_print,
	    CFARGS(.iattr = "videobus"));
}

/* video_submit_payload - called by hardware driver to submit payload data */
void
video_submit_payload(device_t self, const struct video_payload *payload)
{
	struct video_softc *sc;

	sc = device_private(self);

	if (sc == NULL)
		return;

	video_stream_write(&sc->sc_stream_in, payload);
}

static const char *
video_pixel_format_str(enum video_pixel_format px)
{
	switch (px) {
	case VIDEO_FORMAT_UYVY:		return "UYVY";
	case VIDEO_FORMAT_YUV420:	return "YUV420";
	case VIDEO_FORMAT_YUY2: 	return "YUYV";
	case VIDEO_FORMAT_NV12:		return "NV12";
	case VIDEO_FORMAT_RGB24:	return "RGB24";
	case VIDEO_FORMAT_RGB555:	return "RGB555";
	case VIDEO_FORMAT_RGB565:	return "RGB565";
	case VIDEO_FORMAT_SBGGR8:	return "SBGGR8";
	case VIDEO_FORMAT_MJPEG:	return "MJPEG";
	case VIDEO_FORMAT_DV:		return "DV";
	case VIDEO_FORMAT_MPEG:		return "MPEG";
	default:			return "Unknown";
	}
}

/* Takes a V4L2 id and returns a "native" video driver control id.
 * TODO: is there a better way to do this?  some kind of array? */
static uint16_t
v4l2id_to_control_id(uint32_t v4l2id)
{
	/* mask includes class bits and control id bits */
	switch (v4l2id & 0xffffff) {
	case V4L2_CID_BRIGHTNESS:	return VIDEO_CONTROL_BRIGHTNESS;
	case V4L2_CID_CONTRAST:		return VIDEO_CONTROL_CONTRAST;
	case V4L2_CID_SATURATION:	return VIDEO_CONTROL_SATURATION;
	case V4L2_CID_HUE:		return VIDEO_CONTROL_HUE;
	case V4L2_CID_HUE_AUTO:		return VIDEO_CONTROL_HUE_AUTO;
	case V4L2_CID_SHARPNESS:	return VIDEO_CONTROL_SHARPNESS;
	case V4L2_CID_GAMMA:		return VIDEO_CONTROL_GAMMA;

	/* "black level" means the same as "brightness", but V4L2
	 * defines two separate controls that are not identical.
	 * V4L2_CID_BLACK_LEVEL is deprecated however in V4L2. */
	case V4L2_CID_BLACK_LEVEL:	return VIDEO_CONTROL_BRIGHTNESS;

	case V4L2_CID_AUDIO_VOLUME:	return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_AUDIO_BALANCE:	return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_AUDIO_BASS:	return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_AUDIO_TREBLE:	return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_AUDIO_MUTE:	return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_AUDIO_LOUDNESS:	return VIDEO_CONTROL_UNDEFINED;

	case V4L2_CID_AUTO_WHITE_BALANCE:
		return VIDEO_CONTROL_WHITE_BALANCE_AUTO;
	case V4L2_CID_DO_WHITE_BALANCE:
		return VIDEO_CONTROL_WHITE_BALANCE_ACTION;
	case V4L2_CID_RED_BALANCE:
	case V4L2_CID_BLUE_BALANCE:
		/* This might not fit in with the control_id/value_id scheme */
		return VIDEO_CONTROL_WHITE_BALANCE_COMPONENT;
	case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
		return VIDEO_CONTROL_WHITE_BALANCE_TEMPERATURE;
	case V4L2_CID_EXPOSURE:
		return VIDEO_CONTROL_EXPOSURE_TIME_ABSOLUTE;
	case V4L2_CID_GAIN:		return VIDEO_CONTROL_GAIN;
	case V4L2_CID_AUTOGAIN:		return VIDEO_CONTROL_GAIN_AUTO;
	case V4L2_CID_HFLIP:		return VIDEO_CONTROL_HFLIP;
	case V4L2_CID_VFLIP:		return VIDEO_CONTROL_VFLIP;
	case V4L2_CID_HCENTER_DEPRECATED:
	case V4L2_CID_VCENTER_DEPRECATED:
		return VIDEO_CONTROL_UNDEFINED;
	case V4L2_CID_POWER_LINE_FREQUENCY:
		return VIDEO_CONTROL_POWER_LINE_FREQUENCY;
	case V4L2_CID_BACKLIGHT_COMPENSATION:
		return VIDEO_CONTROL_BACKLIGHT_COMPENSATION;
	default:			return V4L2_CTRL_ID2CID(v4l2id);
	}
}


static uint32_t
control_flags_to_v4l2flags(uint32_t flags)
{
	uint32_t v4l2flags = 0;

	if (flags & VIDEO_CONTROL_FLAG_DISABLED)
		v4l2flags |= V4L2_CTRL_FLAG_INACTIVE;

	if (!(flags & VIDEO_CONTROL_FLAG_WRITE))
		v4l2flags |= V4L2_CTRL_FLAG_READ_ONLY;

	if (flags & VIDEO_CONTROL_FLAG_AUTOUPDATE)
		v4l2flags |= V4L2_CTRL_FLAG_GRABBED;

	return v4l2flags;
}


static enum v4l2_ctrl_type
control_type_to_v4l2type(enum video_control_type type) {
	switch (type) {
	case VIDEO_CONTROL_TYPE_INT:	return V4L2_CTRL_TYPE_INTEGER;
	case VIDEO_CONTROL_TYPE_BOOL:	return V4L2_CTRL_TYPE_BOOLEAN;
	case VIDEO_CONTROL_TYPE_LIST:	return V4L2_CTRL_TYPE_MENU;
	case VIDEO_CONTROL_TYPE_ACTION:	return V4L2_CTRL_TYPE_BUTTON;
	default:			return V4L2_CTRL_TYPE_INTEGER; /* err? */
	}
}


static int
video_query_control(struct video_softc *sc,
		    struct v4l2_queryctrl *query)
{
	const struct video_hw_if *hw;
	struct video_control_desc_group desc_group;
	struct video_control_desc desc;
	int err;

	hw = sc->hw_if;
	if (hw->get_control_desc_group) {
		desc.group_id = desc.control_id =
		    v4l2id_to_control_id(query->id);

		desc_group.group_id = desc.group_id;
		desc_group.length = 1;
		desc_group.desc = &desc;

		err = hw->get_control_desc_group(sc->hw_softc, &desc_group);
		if (err != 0)
			return err;

		query->type = control_type_to_v4l2type(desc.type);
		memcpy(query->name, desc.name, 32);
		query->minimum = desc.min;
		query->maximum = desc.max;
		query->step = desc.step;
		query->default_value = desc.def;
		query->flags = control_flags_to_v4l2flags(desc.flags);

		return 0;
	} else {
		return EINVAL;
	}
}


/* Takes a single Video4Linux2 control and queries the driver for the
 * current value. */
static int
video_get_control(struct video_softc *sc,
		  struct v4l2_control *vcontrol)
{
	const struct video_hw_if *hw;
	struct video_control_group group;
	struct video_control control;
	int err;

	hw = sc->hw_if;
	if (hw->get_control_group) {
		control.group_id = control.control_id =
		    v4l2id_to_control_id(vcontrol->id);
		/* ?? if "control_id" is arbitrarily defined by the
		 * driver, then we need some way to store it...  Maybe
		 * it doesn't matter for single value controls. */
		control.value = 0;

		group.group_id = control.group_id;
		group.length = 1;
		group.control = &control;

		err = hw->get_control_group(sc->hw_softc, &group);
		if (err != 0)
			return err;

		vcontrol->value = control.value;
		return 0;
	} else {
		return EINVAL;
	}
}

static void
video_format_to_v4l2_format(const struct video_format *src,
			    struct v4l2_format *dest)
{
	/* TODO: what about win and vbi formats? */
	dest->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	dest->fmt.pix.width = src->width;
	dest->fmt.pix.height = src->height;
	if (VIDEO_INTERLACED(src->interlace_flags))
		dest->fmt.pix.field = V4L2_FIELD_INTERLACED;
	else
		dest->fmt.pix.field = V4L2_FIELD_NONE;
	dest->fmt.pix.bytesperline = src->stride;
	dest->fmt.pix.sizeimage = src->sample_size;
	dest->fmt.pix.priv = src->priv;

	switch (src->color.primaries) {
	case VIDEO_COLOR_PRIMARIES_SMPTE_170M:
		dest->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
		break;
	/* XXX */
	case VIDEO_COLOR_PRIMARIES_UNSPECIFIED:
	default:
		dest->fmt.pix.colorspace = 0;
		break;
	}

	switch (src->pixel_format) {
	case VIDEO_FORMAT_UYVY:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
		break;
	case VIDEO_FORMAT_YUV420:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
		break;
	case VIDEO_FORMAT_YUY2:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
		break;
	case VIDEO_FORMAT_NV12:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
		break;
	case VIDEO_FORMAT_RGB24:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24;
		break;
	case VIDEO_FORMAT_RGB555:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB555;
		break;
	case VIDEO_FORMAT_RGB565:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
		break;
	case VIDEO_FORMAT_SBGGR8:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
		break;
	case VIDEO_FORMAT_MJPEG:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
		break;
	case VIDEO_FORMAT_DV:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_DV;
		break;
	case VIDEO_FORMAT_MPEG:
		dest->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
		break;
	case VIDEO_FORMAT_UNDEFINED:
	default:
		DPRINTF(("video_get_format: unknown pixel format %d\n",
			 src->pixel_format));
		dest->fmt.pix.pixelformat = 0; /* V4L2 doesn't define
					       * and "undefined"
					       * format? */
		break;
	}

}

static void
v4l2_format_to_video_format(const struct v4l2_format *src,
			    struct video_format *dest)
{
	switch (src->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		dest->width = src->fmt.pix.width;
		dest->height = src->fmt.pix.height;

		dest->stride = src->fmt.pix.bytesperline;
		dest->sample_size = src->fmt.pix.sizeimage;

		if (src->fmt.pix.field == V4L2_FIELD_INTERLACED)
			dest->interlace_flags = VIDEO_INTERLACE_ON;
		else
			dest->interlace_flags = VIDEO_INTERLACE_OFF;

		switch (src->fmt.pix.colorspace) {
		case V4L2_COLORSPACE_SMPTE170M:
			dest->color.primaries =
			    VIDEO_COLOR_PRIMARIES_SMPTE_170M;
			break;
		/* XXX */
		default:
			dest->color.primaries =
			    VIDEO_COLOR_PRIMARIES_UNSPECIFIED;
			break;
		}

		switch (src->fmt.pix.pixelformat) {
		case V4L2_PIX_FMT_UYVY:
			dest->pixel_format = VIDEO_FORMAT_UYVY;
			break;
		case V4L2_PIX_FMT_YUV420:
			dest->pixel_format = VIDEO_FORMAT_YUV420;
			break;
		case V4L2_PIX_FMT_YUYV:
			dest->pixel_format = VIDEO_FORMAT_YUY2;
			break;
		case V4L2_PIX_FMT_NV12:
			dest->pixel_format = VIDEO_FORMAT_NV12;
			break;
		case V4L2_PIX_FMT_RGB24:
			dest->pixel_format = VIDEO_FORMAT_RGB24;
			break;
		case V4L2_PIX_FMT_RGB555:
			dest->pixel_format = VIDEO_FORMAT_RGB555;
			break;
		case V4L2_PIX_FMT_RGB565:
			dest->pixel_format = VIDEO_FORMAT_RGB565;
			break;
		case V4L2_PIX_FMT_SBGGR8:
			dest->pixel_format = VIDEO_FORMAT_SBGGR8;
			break;
		case V4L2_PIX_FMT_MJPEG:
			dest->pixel_format = VIDEO_FORMAT_MJPEG;
			break;
		case V4L2_PIX_FMT_DV:
			dest->pixel_format = VIDEO_FORMAT_DV;
			break;
		case V4L2_PIX_FMT_MPEG:
			dest->pixel_format = VIDEO_FORMAT_MPEG;
			break;
		default:
			DPRINTF(("video: unknown v4l2 pixel format %d\n",
				 src->fmt.pix.pixelformat));
			dest->pixel_format = VIDEO_FORMAT_UNDEFINED;
			break;
		}
		break;
	default:
		/* TODO: other v4l2 format types */
		DPRINTF(("video: unsupported v4l2 format type %d\n",
			 src->type));
		break;
	}
}

static int
video_enum_format(struct video_softc *sc, struct v4l2_fmtdesc *fmtdesc)
{
	const struct video_hw_if *hw;
	struct video_format vfmt;
	struct v4l2_format fmt;
	int err;

	hw = sc->hw_if;
	if (hw->enum_format == NULL)
		return ENOTTY;

	err = hw->enum_format(sc->hw_softc, fmtdesc->index, &vfmt);
	if (err != 0)
		return err;

	video_format_to_v4l2_format(&vfmt, &fmt);

	fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; /* TODO: only one type for now */
	fmtdesc->flags = 0;
	if (vfmt.pixel_format >= VIDEO_FORMAT_MJPEG)
		fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED;
	strlcpy(fmtdesc->description,
		video_pixel_format_str(vfmt.pixel_format),
		sizeof(fmtdesc->description));
	fmtdesc->pixelformat = fmt.fmt.pix.pixelformat;

	return 0;
}

static int
video_enum_framesizes(struct video_softc *sc, struct v4l2_frmsizeenum *frmdesc)
{
	const struct video_hw_if *hw;
	struct video_format vfmt;
	struct v4l2_format fmt;
	int err;

	hw = sc->hw_if;
	if (hw->enum_format == NULL)
		return ENOTTY;

	err = hw->enum_format(sc->hw_softc, frmdesc->index, &vfmt);
	if (err != 0)
		return err;

	video_format_to_v4l2_format(&vfmt, &fmt);
	if (fmt.fmt.pix.pixelformat != frmdesc->pixel_format) {
		printf("video_enum_framesizes: type mismatch %x %x\n",
		    fmt.fmt.pix.pixelformat, frmdesc->pixel_format);
	}

	frmdesc->type = V4L2_FRMSIZE_TYPE_DISCRETE; /* TODO: only one type for now */
	frmdesc->discrete.width = vfmt.width;
	frmdesc->discrete.height = vfmt.height;
	return 0;
}

static int
video_enum_frameival(struct video_softc *sc, struct v4l2_frmivalenum *frmdesc)
{
	const struct video_hw_if *hw;

	hw = sc->hw_if;
	if (hw->enum_format == NULL)
		return ENOTTY;

	frmdesc->type = V4L2_FRMSIZE_TYPE_DISCRETE;
	frmdesc->discrete.numerator = 1;
	frmdesc->discrete.denominator = 15;
	return 0;
}

static int
video_get_format(struct video_softc *sc,
		      struct v4l2_format *format)
{
	const struct video_hw_if *hw;
	struct video_format vfmt;
	int err;

	hw = sc->hw_if;
	if (hw->get_format == NULL)
		return ENOTTY;

	err = hw->get_format(sc->hw_softc, &vfmt);
	if (err != 0)
		return err;

	video_format_to_v4l2_format(&vfmt, format);

	return 0;
}

static int
video_set_format(struct video_softc *sc, struct v4l2_format *fmt)
{
	const struct video_hw_if *hw;
	struct video_format vfmt;
	int err;

	hw = sc->hw_if;
	if (hw->set_format == NULL)
		return ENOTTY;

	v4l2_format_to_video_format(fmt, &vfmt);

	err = hw->set_format(sc->hw_softc, &vfmt);
	if (err != 0)
		return err;

	video_format_to_v4l2_format(&vfmt, fmt);
	sc->sc_stream_in.vs_format = vfmt;

	return 0;
}


static int
video_try_format(struct video_softc *sc,
		      struct v4l2_format *format)
{
	const struct video_hw_if *hw;
	struct video_format vfmt;
	int err;

	hw = sc->hw_if;
	if (hw->try_format == NULL)
		return ENOTTY;

	v4l2_format_to_video_format(format, &vfmt);

	err = hw->try_format(sc->hw_softc, &vfmt);
	if (err != 0)
		return err;

	video_format_to_v4l2_format(&vfmt, format);

	return 0;
}

static int
video_get_parm(struct video_softc *sc, struct v4l2_streamparm *parm)
{
	struct video_fract fract;
	const struct video_hw_if *hw;
	int error;

	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return EINVAL;

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;

	memset(&parm->parm, 0, sizeof(parm->parm));
	if (hw->get_framerate != NULL) {
		error = hw->get_framerate(sc->hw_softc, &fract);
		if (error != 0)
			return error;
		parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
		parm->parm.capture.timeperframe.numerator = fract.numerator;
		parm->parm.capture.timeperframe.denominator = fract.denominator;
	}

	return 0;
}

static int
video_set_parm(struct video_softc *sc, struct v4l2_streamparm *parm)
{
	struct video_fract fract;
	const struct video_hw_if *hw;
	int error;

	if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return EINVAL;

	hw = sc->hw_if;
	if (hw == NULL || hw->set_framerate == NULL)
		return ENXIO;

	error = hw->set_framerate(sc->hw_softc, &fract);
	if (error != 0)
		return error;

	parm->parm.capture.timeperframe.numerator = fract.numerator;
	parm->parm.capture.timeperframe.denominator = fract.denominator;

	return 0;
}

static void
v4l2_standard_to_video_standard(v4l2_std_id stdid,
    enum video_standard *vstd)
{
#define VSTD(id, vid)	case (id):	*vstd = (vid); break;
	switch (stdid) {
	VSTD(V4L2_STD_NTSC_M, VIDEO_STANDARD_NTSC_M)
	default:
		*vstd = VIDEO_STANDARD_UNKNOWN;
		break;
	}
#undef VSTD
}

static void
video_standard_to_v4l2_standard(enum video_standard vstd,
    struct v4l2_standard *std)
{
	switch (vstd) {
	case VIDEO_STANDARD_NTSC_M:
		std->id = V4L2_STD_NTSC_M;
		strlcpy(std->name, "NTSC-M", sizeof(std->name));
		std->frameperiod.numerator = 1001;
		std->frameperiod.denominator = 30000;
		std->framelines = 525;
		break;
	default:
		std->id = V4L2_STD_UNKNOWN;
		strlcpy(std->name, "Unknown", sizeof(std->name));
		break;
	}
}

static int
video_enum_standard(struct video_softc *sc, struct v4l2_standard *std)
{
	const struct video_hw_if *hw = sc->hw_if;
	enum video_standard vstd;
	int err;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->enum_standard == NULL) {
		if (std->index != 0)
			return EINVAL;
		std->id = V4L2_STD_UNKNOWN;
		strlcpy(std->name, "webcam", sizeof(std->name));
		return 0;
	}

	v4l2_standard_to_video_standard(std->id, &vstd);

	err = hw->enum_standard(sc->hw_softc, std->index, &vstd);
	if (err != 0)
		return err;

	video_standard_to_v4l2_standard(vstd, std);

	return 0;
}

static int
video_get_standard(struct video_softc *sc, v4l2_std_id *stdid)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct v4l2_standard std;
	enum video_standard vstd;
	int err;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->get_standard == NULL) {
		*stdid = V4L2_STD_UNKNOWN;
		return 0;
	}

	err = hw->get_standard(sc->hw_softc, &vstd);
	if (err != 0)
		return err;

	video_standard_to_v4l2_standard(vstd, &std);
	*stdid = std.id;

	return 0;
}

static int
video_set_standard(struct video_softc *sc, v4l2_std_id stdid)
{
	const struct video_hw_if *hw = sc->hw_if;
	enum video_standard vstd;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->set_standard == NULL) {
		if (stdid != V4L2_STD_UNKNOWN)
			return EINVAL;
		return 0;
	}

	v4l2_standard_to_video_standard(stdid, &vstd);

	return hw->set_standard(sc->hw_softc, vstd);
}

static void
v4l2_input_to_video_input(const struct v4l2_input *input,
    struct video_input *vi)
{
	vi->index = input->index;
	strlcpy(vi->name, input->name, sizeof(vi->name));
	switch (input->type) {
	case V4L2_INPUT_TYPE_TUNER:
		vi->type = VIDEO_INPUT_TYPE_TUNER;
		break;
	case V4L2_INPUT_TYPE_CAMERA:
		vi->type = VIDEO_INPUT_TYPE_CAMERA;
		break;
	}
	vi->audiomask = input->audioset;
	vi->tuner_index = input->tuner;
	vi->standards = input->std;	/* ... values are the same */
	vi->status = 0;
	if (input->status & V4L2_IN_ST_NO_POWER)
		vi->status |= VIDEO_STATUS_NO_POWER;
	if (input->status & V4L2_IN_ST_NO_SIGNAL)
		vi->status |= VIDEO_STATUS_NO_SIGNAL;
	if (input->status & V4L2_IN_ST_NO_COLOR)
		vi->status |= VIDEO_STATUS_NO_COLOR;
	if (input->status & V4L2_IN_ST_NO_H_LOCK)
		vi->status |= VIDEO_STATUS_NO_HLOCK;
	if (input->status & V4L2_IN_ST_MACROVISION)
		vi->status |= VIDEO_STATUS_MACROVISION;
}

static void
video_input_to_v4l2_input(const struct video_input *vi,
    struct v4l2_input *input)
{
	input->index = vi->index;
	strlcpy(input->name, vi->name, sizeof(input->name));
	switch (vi->type) {
	case VIDEO_INPUT_TYPE_TUNER:
		input->type = V4L2_INPUT_TYPE_TUNER;
		break;
	case VIDEO_INPUT_TYPE_CAMERA:
		input->type = V4L2_INPUT_TYPE_CAMERA;
		break;
	}
	input->audioset = vi->audiomask;
	input->tuner = vi->tuner_index;
	input->std = vi->standards;	/* ... values are the same */
	input->status = 0;
	if (vi->status & VIDEO_STATUS_NO_POWER)
		input->status |= V4L2_IN_ST_NO_POWER;
	if (vi->status & VIDEO_STATUS_NO_SIGNAL)
		input->status |= V4L2_IN_ST_NO_SIGNAL;
	if (vi->status & VIDEO_STATUS_NO_COLOR)
		input->status |= V4L2_IN_ST_NO_COLOR;
	if (vi->status & VIDEO_STATUS_NO_HLOCK)
		input->status |= V4L2_IN_ST_NO_H_LOCK;
	if (vi->status & VIDEO_STATUS_MACROVISION)
		input->status |= V4L2_IN_ST_MACROVISION;
}

static int
video_enum_input(struct video_softc *sc, struct v4l2_input *input)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_input vi;
	int err;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->enum_input == NULL) {
		if (input->index != 0)
			return EINVAL;
		memset(input, 0, sizeof(*input));
		input->index = 0;
		strlcpy(input->name, "Camera", sizeof(input->name));
		input->type = V4L2_INPUT_TYPE_CAMERA;
		return 0;
	}

	v4l2_input_to_video_input(input, &vi);

	err = hw->enum_input(sc->hw_softc, input->index, &vi);
	if (err != 0)
		return err;

	video_input_to_v4l2_input(&vi, input);

	return 0;
}

static int
video_get_input(struct video_softc *sc, int *index)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_input vi;
	struct v4l2_input input;
	int err;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->get_input == NULL) {
		*index = 0;
		return 0;
	}

	input.index = *index;
	v4l2_input_to_video_input(&input, &vi);

	err = hw->get_input(sc->hw_softc, &vi);
	if (err != 0)
		return err;

	video_input_to_v4l2_input(&vi, &input);
	*index = input.index;

	return 0;
}

static int
video_set_input(struct video_softc *sc, int index)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_input vi;
	struct v4l2_input input;

	/* simple webcam drivers don't need to implement this callback */
	if (hw->set_input == NULL) {
		if (index != 0)
			return EINVAL;
		return 0;
	}

	input.index = index;
	v4l2_input_to_video_input(&input, &vi);

	return hw->set_input(sc->hw_softc, &vi);
}

static void
v4l2_audio_to_video_audio(const struct v4l2_audio *audio,
    struct video_audio *va)
{
	va->index = audio->index;
	strlcpy(va->name, audio->name, sizeof(va->name));
	va->caps = va->mode = 0;
	if (audio->capability & V4L2_AUDCAP_STEREO)
		va->caps |= VIDEO_AUDIO_F_STEREO;
	if (audio->capability & V4L2_AUDCAP_AVL)
		va->caps |= VIDEO_AUDIO_F_AVL;
	if (audio->mode & V4L2_AUDMODE_AVL)
		va->mode |= VIDEO_AUDIO_F_AVL;
}

static void
video_audio_to_v4l2_audio(const struct video_audio *va,
    struct v4l2_audio *audio)
{
	audio->index = va->index;
	strlcpy(audio->name, va->name, sizeof(audio->name));
	audio->capability = audio->mode = 0;
	if (va->caps & VIDEO_AUDIO_F_STEREO)
		audio->capability |= V4L2_AUDCAP_STEREO;
	if (va->caps & VIDEO_AUDIO_F_AVL)
		audio->capability |= V4L2_AUDCAP_AVL;
	if (va->mode & VIDEO_AUDIO_F_AVL)
		audio->mode |= V4L2_AUDMODE_AVL;
}

static int
video_enum_audio(struct video_softc *sc, struct v4l2_audio *audio)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_audio va;
	int err;

	if (hw->enum_audio == NULL)
		return ENOTTY;

	v4l2_audio_to_video_audio(audio, &va);

	err = hw->enum_audio(sc->hw_softc, audio->index, &va);
	if (err != 0)
		return err;

	video_audio_to_v4l2_audio(&va, audio);

	return 0;
}

static int
video_get_audio(struct video_softc *sc, struct v4l2_audio *audio)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_audio va;
	int err;

	if (hw->get_audio == NULL)
		return ENOTTY;

	v4l2_audio_to_video_audio(audio, &va);

	err = hw->get_audio(sc->hw_softc, &va);
	if (err != 0)
		return err;

	video_audio_to_v4l2_audio(&va, audio);

	return 0;
}

static int
video_set_audio(struct video_softc *sc, struct v4l2_audio *audio)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_audio va;

	if (hw->set_audio == NULL)
		return ENOTTY;

	v4l2_audio_to_video_audio(audio, &va);

	return hw->set_audio(sc->hw_softc, &va);
}

static void
v4l2_tuner_to_video_tuner(const struct v4l2_tuner *tuner,
    struct video_tuner *vt)
{
	vt->index = tuner->index;
	strlcpy(vt->name, tuner->name, sizeof(vt->name));
	vt->freq_lo = tuner->rangelow;
	vt->freq_hi = tuner->rangehigh;
	vt->signal = tuner->signal;
	vt->afc = tuner->afc;
	vt->caps = 0;
	if (tuner->capability & V4L2_TUNER_CAP_STEREO)
		vt->caps |= VIDEO_TUNER_F_STEREO;
	if (tuner->capability & V4L2_TUNER_CAP_LANG1)
		vt->caps |= VIDEO_TUNER_F_LANG1;
	if (tuner->capability & V4L2_TUNER_CAP_LANG2)
		vt->caps |= VIDEO_TUNER_F_LANG2;
	switch (tuner->audmode) {
	case V4L2_TUNER_MODE_MONO:
		vt->mode = VIDEO_TUNER_F_MONO;
		break;
	case V4L2_TUNER_MODE_STEREO:
		vt->mode = VIDEO_TUNER_F_STEREO;
		break;
	case V4L2_TUNER_MODE_LANG1:
		vt->mode = VIDEO_TUNER_F_LANG1;
		break;
	case V4L2_TUNER_MODE_LANG2:
		vt->mode = VIDEO_TUNER_F_LANG2;
		break;
	case V4L2_TUNER_MODE_LANG1_LANG2:
		vt->mode = VIDEO_TUNER_F_LANG1 | VIDEO_TUNER_F_LANG2;
		break;
	}
}

static void
video_tuner_to_v4l2_tuner(const struct video_tuner *vt,
    struct v4l2_tuner *tuner)
{
	tuner->index = vt->index;
	strlcpy(tuner->name, vt->name, sizeof(tuner->name));
	tuner->rangelow = vt->freq_lo;
	tuner->rangehigh = vt->freq_hi;
	tuner->signal = vt->signal;
	tuner->afc = vt->afc;
	tuner->capability = 0;
	if (vt->caps & VIDEO_TUNER_F_STEREO)
		tuner->capability |= V4L2_TUNER_CAP_STEREO;
	if (vt->caps & VIDEO_TUNER_F_LANG1)
		tuner->capability |= V4L2_TUNER_CAP_LANG1;
	if (vt->caps & VIDEO_TUNER_F_LANG2)
		tuner->capability |= V4L2_TUNER_CAP_LANG2;
	switch (vt->mode) {
	case VIDEO_TUNER_F_MONO:
		tuner->audmode = V4L2_TUNER_MODE_MONO;
		break;
	case VIDEO_TUNER_F_STEREO:
		tuner->audmode = V4L2_TUNER_MODE_STEREO;
		break;
	case VIDEO_TUNER_F_LANG1:
		tuner->audmode = V4L2_TUNER_MODE_LANG1;
		break;
	case VIDEO_TUNER_F_LANG2:
		tuner->audmode = V4L2_TUNER_MODE_LANG2;
		break;
	case VIDEO_TUNER_F_LANG1|VIDEO_TUNER_F_LANG2:
		tuner->audmode = V4L2_TUNER_MODE_LANG1_LANG2;
		break;
	}
}

static int
video_get_tuner(struct video_softc *sc, struct v4l2_tuner *tuner)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_tuner vt;
	int err;

	if (hw->get_tuner == NULL)
		return ENOTTY;

	v4l2_tuner_to_video_tuner(tuner, &vt);

	err = hw->get_tuner(sc->hw_softc, &vt);
	if (err != 0)
		return err;

	video_tuner_to_v4l2_tuner(&vt, tuner);

	return 0;
}

static int
video_set_tuner(struct video_softc *sc, struct v4l2_tuner *tuner)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_tuner vt;

	if (hw->set_tuner == NULL)
		return ENOTTY;

	v4l2_tuner_to_video_tuner(tuner, &vt);

	return hw->set_tuner(sc->hw_softc, &vt);
}

static int
video_get_frequency(struct video_softc *sc, struct v4l2_frequency *freq)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_frequency vfreq;
	int err;

	if (hw->get_frequency == NULL)
		return ENOTTY;

	err = hw->get_frequency(sc->hw_softc, &vfreq);
	if (err)
		return err;

	freq->tuner = vfreq.tuner_index;
	freq->type = V4L2_TUNER_ANALOG_TV;
	freq->frequency = vfreq.frequency;

	return 0;
}

static int
video_set_frequency(struct video_softc *sc, struct v4l2_frequency *freq)
{
	const struct video_hw_if *hw = sc->hw_if;
	struct video_frequency vfreq;
	struct video_tuner vt;
	int error;

	if (hw->set_frequency == NULL || hw->get_tuner == NULL)
		return ENOTTY;
	if (freq->type != V4L2_TUNER_ANALOG_TV)
		return EINVAL;

	vt.index = freq->tuner;
	error = hw->get_tuner(sc->hw_softc, &vt);
	if (error)
		return error;

	if (freq->frequency < vt.freq_lo)
		freq->frequency = vt.freq_lo;
	else if (freq->frequency > vt.freq_hi)
		freq->frequency = vt.freq_hi;

	vfreq.tuner_index = freq->tuner;
	vfreq.frequency = freq->frequency;

	return hw->set_frequency(sc->hw_softc, &vfreq);
}

/* Takes a single Video4Linux2 control, converts it to a struct
 * video_control, and calls the hardware driver. */
static int
video_set_control(struct video_softc *sc,
		       const struct v4l2_control *vcontrol)
{
	const struct video_hw_if *hw;
	struct video_control_group group;
	struct video_control control;

	hw = sc->hw_if;
	if (hw->set_control_group) {
		control.group_id = control.control_id =
		    v4l2id_to_control_id(vcontrol->id);
		/* ?? if "control_id" is arbitrarily defined by the
		 * driver, then we need some way to store it...  Maybe
		 * it doesn't matter for single value controls. */
		control.value = vcontrol->value;

		group.group_id = control.group_id;
		group.length = 1;
		group.control = &control;

		return (hw->set_control_group(sc->hw_softc, &group));
	} else {
		return EINVAL;
	}
}

static int
video_request_bufs(struct video_softc *sc,
		   struct v4l2_requestbuffers *req)
{
	struct video_stream *vs = &sc->sc_stream_in;
	struct v4l2_buffer *buf;
	int i, err;

	if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return EINVAL;

	vs->vs_type = req->type;

	switch (req->memory) {
	case V4L2_MEMORY_MMAP:
		if (req->count < VIDEO_MIN_BUFS)
			req->count = VIDEO_MIN_BUFS;
		else if (req->count > VIDEO_MAX_BUFS)
			req->count = VIDEO_MAX_BUFS;

		err = video_stream_setup_bufs(vs,
					      VIDEO_STREAM_METHOD_MMAP,
					      req->count);
		if (err != 0)
			return err;

		for (i = 0; i < req->count; ++i) {
			buf = vs->vs_buf[i]->vb_buf;
			buf->memory = V4L2_MEMORY_MMAP;
			buf->flags |= V4L2_BUF_FLAG_MAPPED;
		}
		break;
	case V4L2_MEMORY_USERPTR:
	default:
		return EINVAL;
	}

	return 0;
}

static int
video_query_buf(struct video_softc *sc,
		struct v4l2_buffer *buf)
{
	struct video_stream *vs = &sc->sc_stream_in;

	if (buf->type != vs->vs_type)
		return EINVAL;
	if (buf->index >= vs->vs_nbufs)
		return EINVAL;

	memcpy(buf, vs->vs_buf[buf->index]->vb_buf, sizeof(*buf));

	return 0;
}

/* Accept a buffer descriptor from userspace and return the indicated
 * buffer to the driver's queue. */
static int
video_queue_buf(struct video_softc *sc, struct v4l2_buffer *userbuf)
{
	struct video_stream *vs = &sc->sc_stream_in;
	struct video_buffer *vb;
	struct v4l2_buffer *driverbuf;

	if (userbuf->type != vs->vs_type) {
		DPRINTF(("video_queue_buf: expected type=%d got type=%d\n",
			 userbuf->type, vs->vs_type));
		return EINVAL;
	}
	if (userbuf->index >= vs->vs_nbufs) {
		DPRINTF(("video_queue_buf: invalid index %d >= %d\n",
			 userbuf->index, vs->vs_nbufs));
		return EINVAL;
	}

	switch (vs->vs_method) {
	case VIDEO_STREAM_METHOD_MMAP:
		if (userbuf->memory != V4L2_MEMORY_MMAP) {
			DPRINTF(("video_queue_buf: invalid memory=%d\n",
				 userbuf->memory));
			return EINVAL;
		}

		mutex_enter(&vs->vs_lock);

		vb = vs->vs_buf[userbuf->index];
		driverbuf = vb->vb_buf;
		if (driverbuf->flags & V4L2_BUF_FLAG_QUEUED) {
			DPRINTF(("video_queue_buf: buf already queued; "
				 "flags=0x%x\n", driverbuf->flags));
			mutex_exit(&vs->vs_lock);
			return EINVAL;
		}
		video_stream_enqueue(vs, vb);
		memcpy(userbuf, driverbuf, sizeof(*driverbuf));

		mutex_exit(&vs->vs_lock);
		break;
	default:
		return EINVAL;
	}

	return 0;
}

/* Dequeue the described buffer from the driver queue, making it
 * available for reading via mmap. */
static int
video_dequeue_buf(struct video_softc *sc, struct v4l2_buffer *buf)
{
	struct video_stream *vs = &sc->sc_stream_in;
	struct video_buffer *vb;
	int err;

	if (buf->type != vs->vs_type) {
		aprint_debug_dev(sc->sc_dev,
		    "requested type %d (expected %d)\n",
		    buf->type, vs->vs_type);
		return EINVAL;
	}

	switch (vs->vs_method) {
	case VIDEO_STREAM_METHOD_MMAP:
		if (buf->memory != V4L2_MEMORY_MMAP) {
			aprint_debug_dev(sc->sc_dev,
			    "requested memory %d (expected %d)\n",
			    buf->memory, V4L2_MEMORY_MMAP);
			return EINVAL;
		}

		mutex_enter(&vs->vs_lock);

		if (vs->vs_flags & O_NONBLOCK) {
			vb = video_stream_dequeue(vs);
			if (vb == NULL) {
				mutex_exit(&vs->vs_lock);
				return EAGAIN;
			}
		} else {
			/* Block until we have sample */
			while ((vb = video_stream_dequeue(vs)) == NULL) {
				if (!vs->vs_streaming) {
					mutex_exit(&vs->vs_lock);
					return EINVAL;
				}
				err = cv_wait_sig(&vs->vs_sample_cv,
						  &vs->vs_lock);
				if (err != 0) {
					mutex_exit(&vs->vs_lock);
					return EINTR;
				}
			}
		}

		memcpy(buf, vb->vb_buf, sizeof(*buf));

		mutex_exit(&vs->vs_lock);
		break;
	default:
		aprint_debug_dev(sc->sc_dev, "unknown vs_method %d\n",
		    vs->vs_method);
		return EINVAL;
	}

	return 0;
}

static int
video_stream_on(struct video_softc *sc, enum v4l2_buf_type type)
{
	int err;
	struct video_stream *vs = &sc->sc_stream_in;
	const struct video_hw_if *hw;

	if (vs->vs_streaming)
		return 0;
	if (type != vs->vs_type)
		return EINVAL;

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;


	err = hw->start_transfer(sc->hw_softc);
	if (err != 0)
		return err;

	vs->vs_streaming = true;
	return 0;
}

static int
video_stream_off(struct video_softc *sc, enum v4l2_buf_type type)
{
	int err;
	struct video_stream *vs = &sc->sc_stream_in;
	const struct video_hw_if *hw;

	if (!vs->vs_streaming)
		return 0;
	if (type != vs->vs_type)
		return EINVAL;

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;

	err = hw->stop_transfer(sc->hw_softc);
	if (err != 0)
		return err;

	vs->vs_frameno = -1;
	vs->vs_sequence = 0;
	vs->vs_streaming = false;

	return 0;
}

int
videoopen(dev_t dev, int flags, int ifmt, struct lwp *l)
{
	struct video_softc *sc;
	const struct video_hw_if *hw;
	struct video_stream *vs;
	int err;

	DPRINTF(("videoopen\n"));

	sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
	if (sc == NULL) {
		DPRINTF(("videoopen: failed to get softc for unit %d\n",
			VIDEOUNIT(dev)));
		return ENXIO;
	}

	if (sc->sc_dying) {
		DPRINTF(("videoopen: dying\n"));
		return EIO;
	}

	sc->sc_stream_in.vs_flags = flags;

	DPRINTF(("videoopen: flags=0x%x sc=%p parent=%p\n",
		 flags, sc, sc->hw_dev));

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;

	device_active(sc->sc_dev, DVA_SYSTEM);

	sc->sc_opencnt++;

	if (hw->open != NULL) {
		err = hw->open(sc->hw_softc, flags);
		if (err)
			return err;
	}

	/* set up input stream.  TODO: check flags to determine if
	 * "read" is desired? */
	vs = &sc->sc_stream_in;

	if (hw->get_format != NULL) {
		err = hw->get_format(sc->hw_softc, &vs->vs_format);
		if (err != 0)
			return err;
	}
	return 0;
}


int
videoclose(dev_t dev, int flags, int ifmt, struct lwp *l)
{
	struct video_softc *sc;
	const struct video_hw_if *hw;

	sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
	if (sc == NULL)
		return ENXIO;

	DPRINTF(("videoclose: sc=%p\n", sc));

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;

	device_active(sc->sc_dev, DVA_SYSTEM);

	video_stream_off(sc, sc->sc_stream_in.vs_type);

	/* ignore error */
	if (hw->close != NULL)
		hw->close(sc->hw_softc);

	video_stream_teardown_bufs(&sc->sc_stream_in);

	sc->sc_open = 0;
	sc->sc_opencnt--;

	return 0;
}


int
videoread(dev_t dev, struct uio *uio, int ioflag)
{
	struct video_softc *sc;
	struct video_stream *vs;
	struct video_buffer *vb;
	struct scatter_io sio;
	int err;
	size_t len;
	off_t offset;

	sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
	if (sc == NULL)
		return ENXIO;

	if (sc->sc_dying)
		return EIO;

	vs = &sc->sc_stream_in;

	/* userspace has chosen read() method */
	if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) {
		err = video_stream_setup_bufs(vs,
					      VIDEO_STREAM_METHOD_READ,
					      VIDEO_NUM_BUFS);
		if (err != 0)
			return err;

		err = video_stream_on(sc, vs->vs_type);
		if (err != 0)
			return err;
	} else if (vs->vs_method != VIDEO_STREAM_METHOD_READ) {
		return EBUSY;
	}

	mutex_enter(&vs->vs_lock);

retry:
	if (SIMPLEQ_EMPTY(&vs->vs_egress)) {
		if (vs->vs_flags & O_NONBLOCK) {
			mutex_exit(&vs->vs_lock);
			return EAGAIN;
		}

		/* Block until we have a sample */
		while (SIMPLEQ_EMPTY(&vs->vs_egress)) {
			err = cv_wait_sig(&vs->vs_sample_cv,
					  &vs->vs_lock);
			if (err != 0) {
				mutex_exit(&vs->vs_lock);
				return EINTR;
			}
		}

		vb = SIMPLEQ_FIRST(&vs->vs_egress);
	} else {
	        vb = SIMPLEQ_FIRST(&vs->vs_egress);
	}

	/* Oops, empty sample buffer. */
	if (vb->vb_buf->bytesused == 0) {
		vb = video_stream_dequeue(vs);
		video_stream_enqueue(vs, vb);
		vs->vs_bytesread = 0;
		goto retry;
	}

	mutex_exit(&vs->vs_lock);

	len = uimin(uio->uio_resid, vb->vb_buf->bytesused - vs->vs_bytesread);
	offset = vb->vb_buf->m.offset + vs->vs_bytesread;

	if (scatter_io_init(&vs->vs_data, offset, len, &sio)) {
		err = scatter_io_uiomove(&sio, uio);
		if (err == EFAULT)
			return EFAULT;
		vs->vs_bytesread += (len - sio.sio_resid);
	} else {
		DPRINTF(("video: invalid read\n"));
	}

	/* Move the sample to the ingress queue if everything has
	 * been read */
	if (vs->vs_bytesread >= vb->vb_buf->bytesused) {
		mutex_enter(&vs->vs_lock);
		vb = video_stream_dequeue(vs);
		video_stream_enqueue(vs, vb);
		mutex_exit(&vs->vs_lock);

		vs->vs_bytesread = 0;
	}

	return 0;
}


int
videowrite(dev_t dev, struct uio *uio, int ioflag)
{
	return ENXIO;
}


/*
 * Before 64-bit time_t, timeval's tv_sec was 'long'.  Thus on LP64 ports
 * v4l2_buffer is the same size and layout as before.  However it did change
 * on LP32 ports, and we thus handle this difference here for "COMPAT_50".
 */

#ifndef _LP64
static void
buf50tobuf(const void *data, struct v4l2_buffer *buf)
{
	const struct v4l2_buffer50 *b50 = data;

	buf->index = b50->index;
	buf->type = b50->type;
	buf->bytesused = b50->bytesused;
	buf->flags = b50->flags;
	buf->field = b50->field;
	timeval50_to_timeval(&b50->timestamp, &buf->timestamp);
	buf->timecode = b50->timecode;
	buf->sequence = b50->sequence;
	buf->memory = b50->memory;
	buf->m.offset = b50->m.offset;
	/* XXX: Handle userptr */
	buf->length = b50->length;
	buf->reserved2 = b50->reserved2;
	buf->reserved = b50->reserved;
}

static void
buftobuf50(void *data, const struct v4l2_buffer *buf)
{
	struct v4l2_buffer50 *b50 = data;

	b50->index = buf->index;
	b50->type = buf->type;
	b50->bytesused = buf->bytesused;
	b50->flags = buf->flags;
	b50->field = buf->field;
	timeval_to_timeval50(&buf->timestamp, &b50->timestamp);
	b50->timecode = buf->timecode;
	b50->sequence = buf->sequence;
	b50->memory = buf->memory;
	b50->m.offset = buf->m.offset;
	/* XXX: Handle userptr */
	b50->length = buf->length;
	b50->reserved2 = buf->reserved2;
	b50->reserved = buf->reserved;
}
#endif

int
videoioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct video_softc *sc;
	const struct video_hw_if *hw;
	struct v4l2_capability *cap;
	struct v4l2_fmtdesc *fmtdesc;
	struct v4l2_format *fmt;
	struct v4l2_standard *std;
	struct v4l2_input *input;
	struct v4l2_audio *audio;
	struct v4l2_tuner *tuner;
	struct v4l2_frequency *freq;
	struct v4l2_control *control;
	struct v4l2_queryctrl *query;
	struct v4l2_requestbuffers *reqbufs;
	struct v4l2_buffer *buf;
	struct v4l2_streamparm *parm;
	struct v4l2_frmsizeenum *size;
	struct v4l2_frmivalenum *ival;
	v4l2_std_id *stdid;
	enum v4l2_buf_type *typep;
	int *ip;
#ifndef _LP64
	struct v4l2_buffer bufspace;
	int error;
#endif

	sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));

	if (sc->sc_dying)
		return EIO;

	hw = sc->hw_if;
	if (hw == NULL)
		return ENXIO;

	switch (cmd) {
	case VIDIOC_QUERYCAP:
		cap = data;
		memset(cap, 0, sizeof(*cap));
		strlcpy(cap->driver,
			device_cfdriver(sc->hw_dev)->cd_name,
			sizeof(cap->driver));
		strlcpy(cap->card, hw->get_devname(sc->hw_softc),
			sizeof(cap->card));
		strlcpy(cap->bus_info, hw->get_businfo(sc->hw_softc),
			sizeof(cap->bus_info));
		cap->version = VIDEO_DRIVER_VERSION;
		cap->capabilities = 0;
		if (hw->start_transfer != NULL && hw->stop_transfer != NULL)
			cap->capabilities |= V4L2_CAP_VIDEO_CAPTURE |
			    V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
		if (hw->set_tuner != NULL && hw->get_tuner != NULL)
			cap->capabilities |= V4L2_CAP_TUNER;
		if (hw->set_audio != NULL && hw->get_audio != NULL &&
		    hw->enum_audio != NULL)
			cap->capabilities |= V4L2_CAP_AUDIO;
		return 0;
	case VIDIOC_ENUM_FMT:
		/* TODO: for now, just enumerate one default format */
		fmtdesc = data;
		if (fmtdesc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
			return EINVAL;
		return video_enum_format(sc, fmtdesc);
	case VIDIOC_G_FMT:
		fmt = data;
		return video_get_format(sc, fmt);
	case VIDIOC_S_FMT:
		fmt = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_format(sc, fmt);
	case VIDIOC_TRY_FMT:
		fmt = data;
		return video_try_format(sc, fmt);
	case VIDIOC_G_PARM:
		parm = data;
		return video_get_parm(sc, parm);
	case VIDIOC_S_PARM:
		parm = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_parm(sc, parm);
	case VIDIOC_ENUMSTD:
		std = data;
		return video_enum_standard(sc, std);
	case VIDIOC_G_STD:
		stdid = data;
		return video_get_standard(sc, stdid);
	case VIDIOC_S_STD:
		stdid = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_standard(sc, *stdid);
	case VIDIOC_ENUMINPUT:
		input = data;
		return video_enum_input(sc, input);
	case VIDIOC_G_INPUT:
		ip = data;
		return video_get_input(sc, ip);
	case VIDIOC_S_INPUT:
		ip = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_input(sc, *ip);
	case VIDIOC_ENUMAUDIO:
		audio = data;
		return video_enum_audio(sc, audio);
	case VIDIOC_G_AUDIO:
		audio = data;
		return video_get_audio(sc, audio);
	case VIDIOC_S_AUDIO:
		audio = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_audio(sc, audio);
	case VIDIOC_G_TUNER:
		tuner = data;
		return video_get_tuner(sc, tuner);
	case VIDIOC_S_TUNER:
		tuner = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_tuner(sc, tuner);
	case VIDIOC_G_FREQUENCY:
		freq = data;
		return video_get_frequency(sc, freq);
	case VIDIOC_S_FREQUENCY:
		freq = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return video_set_frequency(sc, freq);
	case VIDIOC_QUERYCTRL:
		query = data;
		return (video_query_control(sc, query));
	case VIDIOC_G_CTRL:
		control = data;
		return (video_get_control(sc, control));
	case VIDIOC_S_CTRL:
		control = data;
		if ((flag & FWRITE) == 0)
			return EPERM;
		return (video_set_control(sc, control));
	case VIDIOC_REQBUFS:
		reqbufs = data;
		return (video_request_bufs(sc, reqbufs));
	case VIDIOC_QUERYBUF:
		buf = data;
		return video_query_buf(sc, buf);
#ifndef _LP64
	case VIDIOC_QUERYBUF50:
		buf50tobuf(data, buf = &bufspace);
		if ((error = video_query_buf(sc, buf)) != 0)
			return error;
		buftobuf50(data, buf);
		return 0;
#endif
	case VIDIOC_QBUF:
		buf = data;
		return video_queue_buf(sc, buf);
#ifndef _LP64
	case VIDIOC_QBUF50:
		buf50tobuf(data, buf = &bufspace);
		return video_queue_buf(sc, buf);
#endif
	case VIDIOC_DQBUF:
		buf = data;
		return video_dequeue_buf(sc, buf);
#ifndef _LP64
	case VIDIOC_DQBUF50:
		buf50tobuf(data, buf = &bufspace);
		if ((error = video_dequeue_buf(sc, buf)) != 0)
			return error;
		buftobuf50(data, buf);
		return 0;
#endif
	case VIDIOC_STREAMON:
		typep = data;
		return video_stream_on(sc, *typep);
	case VIDIOC_STREAMOFF:
		typep = data;
		return video_stream_off(sc, *typep);
	case VIDIOC_ENUM_FRAMESIZES:
		size = data;
		return video_enum_framesizes(sc, size);
	case VIDIOC_ENUM_FRAMEINTERVALS:
		ival = data;
		return video_enum_frameival(sc, ival);
	default:
		DPRINTF(("videoioctl: invalid cmd %s (%lx)\n",
			 video_ioctl_str(cmd), cmd));
		return EINVAL;
	}
}

#ifdef VIDEO_DEBUG
static const char *
video_ioctl_str(u_long cmd)
{
	const char *str;

	switch (cmd) {
	case VIDIOC_QUERYCAP:
		str = "VIDIOC_QUERYCAP";
		break;
	case VIDIOC_RESERVED:
		str = "VIDIOC_RESERVED";
		break;
	case VIDIOC_ENUM_FMT:
		str = "VIDIOC_ENUM_FMT";
		break;
	case VIDIOC_G_FMT:
		str = "VIDIOC_G_FMT";
		break;
	case VIDIOC_S_FMT:
		str = "VIDIOC_S_FMT";
		break;
/* 6 and 7 are VIDIOC_[SG]_COMP, which are unsupported */
	case VIDIOC_REQBUFS:
		str = "VIDIOC_REQBUFS";
		break;
	case VIDIOC_QUERYBUF:
		str = "VIDIOC_QUERYBUF";
		break;
#ifndef _LP64
	case VIDIOC_QUERYBUF50:
		str = "VIDIOC_QUERYBUF50";
		break;
#endif
	case VIDIOC_G_FBUF:
		str = "VIDIOC_G_FBUF";
		break;
	case VIDIOC_S_FBUF:
		str = "VIDIOC_S_FBUF";
		break;
	case VIDIOC_OVERLAY:
		str = "VIDIOC_OVERLAY";
		break;
	case VIDIOC_QBUF:
		str = "VIDIOC_QBUF";
		break;
#ifndef _LP64
	case VIDIOC_QBUF50:
		str = "VIDIOC_QBUF50";
		break;
#endif
	case VIDIOC_DQBUF:
		str = "VIDIOC_DQBUF";
		break;
#ifndef _LP64
	case VIDIOC_DQBUF50:
		str = "VIDIOC_DQBUF50";
		break;
#endif
	case VIDIOC_STREAMON:
		str = "VIDIOC_STREAMON";
		break;
	case VIDIOC_STREAMOFF:
		str = "VIDIOC_STREAMOFF";
		break;
	case VIDIOC_G_PARM:
		str = "VIDIOC_G_PARM";
		break;
	case VIDIOC_S_PARM:
		str = "VIDIOC_S_PARM";
		break;
	case VIDIOC_G_STD:
		str = "VIDIOC_G_STD";
		break;
	case VIDIOC_S_STD:
		str = "VIDIOC_S_STD";
		break;
	case VIDIOC_ENUMSTD:
		str = "VIDIOC_ENUMSTD";
		break;
	case VIDIOC_ENUMINPUT:
		str = "VIDIOC_ENUMINPUT";
		break;
	case VIDIOC_G_CTRL:
		str = "VIDIOC_G_CTRL";
		break;
	case VIDIOC_S_CTRL:
		str = "VIDIOC_S_CTRL";
		break;
	case VIDIOC_G_TUNER:
		str = "VIDIOC_G_TUNER";
		break;
	case VIDIOC_S_TUNER:
		str = "VIDIOC_S_TUNER";
		break;
	case VIDIOC_G_AUDIO:
		str = "VIDIOC_G_AUDIO";
		break;
	case VIDIOC_S_AUDIO:
		str = "VIDIOC_S_AUDIO";
		break;
	case VIDIOC_QUERYCTRL:
		str = "VIDIOC_QUERYCTRL";
		break;
	case VIDIOC_QUERYMENU:
		str = "VIDIOC_QUERYMENU";
		break;
	case VIDIOC_G_INPUT:
		str = "VIDIOC_G_INPUT";
		break;
	case VIDIOC_S_INPUT:
		str = "VIDIOC_S_INPUT";
		break;
	case VIDIOC_G_OUTPUT:
		str = "VIDIOC_G_OUTPUT";
		break;
	case VIDIOC_S_OUTPUT:
		str = "VIDIOC_S_OUTPUT";
		break;
	case VIDIOC_ENUMOUTPUT:
		str = "VIDIOC_ENUMOUTPUT";
		break;
	case VIDIOC_G_AUDOUT:
		str = "VIDIOC_G_AUDOUT";
		break;
	case VIDIOC_S_AUDOUT:
		str = "VIDIOC_S_AUDOUT";
		break;
	case VIDIOC_G_MODULATOR:
		str = "VIDIOC_G_MODULATOR";
		break;
	case VIDIOC_S_MODULATOR:
		str = "VIDIOC_S_MODULATOR";
		break;
	case VIDIOC_G_FREQUENCY:
		str = "VIDIOC_G_FREQUENCY";
		break;
	case VIDIOC_S_FREQUENCY:
		str = "VIDIOC_S_FREQUENCY";
		break;
	case VIDIOC_CROPCAP:
		str = "VIDIOC_CROPCAP";
		break;
	case VIDIOC_G_CROP:
		str = "VIDIOC_G_CROP";
		break;
	case VIDIOC_S_CROP:
		str = "VIDIOC_S_CROP";
		break;
	case VIDIOC_G_JPEGCOMP:
		str = "VIDIOC_G_JPEGCOMP";
		break;
	case VIDIOC_S_JPEGCOMP:
		str = "VIDIOC_S_JPEGCOMP";
		break;
	case VIDIOC_QUERYSTD:
		str = "VIDIOC_QUERYSTD";
		break;
	case VIDIOC_TRY_FMT:
		str = "VIDIOC_TRY_FMT";
		break;
	case VIDIOC_ENUMAUDIO:
		str = "VIDIOC_ENUMAUDIO";
		break;
	case VIDIOC_ENUMAUDOUT:
		str = "VIDIOC_ENUMAUDOUT";
		break;
	case VIDIOC_G_PRIORITY:
		str = "VIDIOC_G_PRIORITY";
		break;
	case VIDIOC_S_PRIORITY:
		str = "VIDIOC_S_PRIORITY";
		break;
	case VIDIOC_ENUM_FRAMESIZES:
		str = "VIDIOC_ENUM_FRAMESIZES";
		break;
	case VIDIOC_ENUM_FRAMEINTERVALS:
		str = "VIDIOC_FRAMEINTERVALS";
		break;
	default:
		str = "unknown";
		break;
	}
	return str;
}
#endif


int
videopoll(dev_t dev, int events, struct lwp *l)
{
	struct video_softc *sc;
	struct video_stream *vs;
	int err, revents = 0;

	sc = device_private(device_lookup(&video_cd, VIDEOUNIT(dev)));
	vs = &sc->sc_stream_in;

	if (sc->sc_dying)
		return (POLLHUP);

	/* userspace has chosen read() method */
	if (vs->vs_method == VIDEO_STREAM_METHOD_NONE) {
		err = video_stream_setup_bufs(vs,
					      VIDEO_STREAM_METHOD_READ,
					      VIDEO_NUM_BUFS);
		if (err != 0)
			return POLLERR;

		err = video_stream_on(sc, vs->vs_type);
		if (err != 0)
			return POLLERR;
	}

	mutex_enter(&vs->vs_lock);
	if (!SIMPLEQ_EMPTY(&sc->sc_stream_in.vs_egress))
		revents |= events & (POLLIN | POLLRDNORM);
	else
		selrecord(l, &vs->vs_sel);
	mutex_exit(&vs->vs_lock);

	return (revents);
}


paddr_t
videommap(dev_t dev, off_t off, int prot)
{
	struct video_softc *sc;
	struct video_stream *vs;
	/* paddr_t pa; */

	sc = device_lookup_private(&video_cd, VIDEOUNIT(dev));
	if (sc->sc_dying)
		return -1;

	vs = &sc->sc_stream_in;

	return scatter_buf_map(&vs->vs_data, off);
}


/* Allocates buffers and initializes some fields.  The format field
 * must already have been initialized. */
void
video_stream_init(struct video_stream *vs)
{
	vs->vs_method = VIDEO_STREAM_METHOD_NONE;
	vs->vs_flags = 0;
	vs->vs_frameno = -1;
	vs->vs_sequence = 0;
	vs->vs_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	vs->vs_nbufs = 0;
	vs->vs_buf = NULL;
	vs->vs_streaming = false;

	memset(&vs->vs_format, 0, sizeof(vs->vs_format));

	SIMPLEQ_INIT(&vs->vs_ingress);
	SIMPLEQ_INIT(&vs->vs_egress);

	mutex_init(&vs->vs_lock, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&vs->vs_sample_cv, "video");
	selinit(&vs->vs_sel);

	scatter_buf_init(&vs->vs_data);
}

void
video_stream_fini(struct video_stream *vs)
{
	/* Sample data in queues has already been freed */
	/* while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL)
		SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);
	while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL)
	SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries); */

	mutex_destroy(&vs->vs_lock);
	cv_destroy(&vs->vs_sample_cv);
	seldestroy(&vs->vs_sel);

	scatter_buf_destroy(&vs->vs_data);
}

static int
video_stream_setup_bufs(struct video_stream *vs,
			enum video_stream_method method,
			uint8_t nbufs)
{
	int i, err;

	mutex_enter(&vs->vs_lock);

	/* Ensure that all allocated buffers are queued and not under
	 * userspace control. */
	for (i = 0; i < vs->vs_nbufs; ++i) {
		if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED)) {
			mutex_exit(&vs->vs_lock);
			return EBUSY;
		}
	}

	/* Allocate the buffers */
	err = video_stream_realloc_bufs(vs, nbufs);
	if (err != 0) {
		mutex_exit(&vs->vs_lock);
		return err;
	}

	/* Queue up buffers for read method.  Other methods are queued
	 * by VIDIOC_QBUF ioctl. */
	if (method == VIDEO_STREAM_METHOD_READ) {
		for (i = 0; i < nbufs; ++i)
			if (!(vs->vs_buf[i]->vb_buf->flags & V4L2_BUF_FLAG_QUEUED))
				video_stream_enqueue(vs, vs->vs_buf[i]);
	}

	vs->vs_method = method;
	mutex_exit(&vs->vs_lock);

	return 0;
}

/* Free all buffer memory in preparation for close().  This should
 * free buffers regardless of errors.  Use video_stream_setup_bufs if
 * you need to check for errors. Streaming should be off before
 * calling this function. */
static void
video_stream_teardown_bufs(struct video_stream *vs)
{
	int err;

	mutex_enter(&vs->vs_lock);

	if (vs->vs_streaming) {
		DPRINTF(("video_stream_teardown_bufs: "
			 "tearing down bufs while streaming\n"));
	}

	/* dequeue all buffers */
	while (SIMPLEQ_FIRST(&vs->vs_ingress) != NULL)
		SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);
	while (SIMPLEQ_FIRST(&vs->vs_egress) != NULL)
		SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries);

	err = video_stream_free_bufs(vs);
	if (err != 0) {
		DPRINTF(("video_stream_teardown_bufs: "
			 "error releasing buffers: %d\n",
			 err));
	}
	vs->vs_method = VIDEO_STREAM_METHOD_NONE;

	mutex_exit(&vs->vs_lock);
}

static struct video_buffer *
video_buffer_alloc(void)
{
	struct video_buffer *vb;

	vb = kmem_alloc(sizeof(*vb), KM_SLEEP);
	vb->vb_buf = kmem_alloc(sizeof(*vb->vb_buf), KM_SLEEP);
	return vb;
}

static void
video_buffer_free(struct video_buffer *vb)
{
	kmem_free(vb->vb_buf, sizeof(*vb->vb_buf));
	vb->vb_buf = NULL;
	kmem_free(vb, sizeof(*vb));
}

/* TODO: for userptr method
struct video_buffer *
video_buf_alloc_with_ubuf(struct v4l2_buffer *buf)
{
}

void
video_buffer_free_with_ubuf(struct video_buffer *vb)
{
}
*/

static int
video_stream_realloc_bufs(struct video_stream *vs, uint8_t nbufs)
{
	int i, err;
	uint8_t minnbufs, oldnbufs;
	size_t size;
	off_t offset;
	struct video_buffer **oldbuf;
	struct v4l2_buffer *buf;

	size = PAGE_ALIGN(vs->vs_format.sample_size) * nbufs;
	err = scatter_buf_set_size(&vs->vs_data, size);
	if (err != 0)
		return err;

	oldnbufs = vs->vs_nbufs;
	oldbuf = vs->vs_buf;

	vs->vs_nbufs = nbufs;
	if (nbufs > 0) {
		vs->vs_buf =
		    kmem_alloc(sizeof(struct video_buffer *) * nbufs, KM_SLEEP);
	} else {
		vs->vs_buf = NULL;
	}

	minnbufs = uimin(vs->vs_nbufs, oldnbufs);
	/* copy any bufs that will be reused */
	for (i = 0; i < minnbufs; ++i)
		vs->vs_buf[i] = oldbuf[i];
	/* allocate any necessary new bufs */
	for (; i < vs->vs_nbufs; ++i)
		vs->vs_buf[i] = video_buffer_alloc();
	/* free any bufs no longer used */
	for (; i < oldnbufs; ++i) {
		video_buffer_free(oldbuf[i]);
		oldbuf[i] = NULL;
	}

	/* Free old buffer metadata */
	if (oldbuf != NULL)
		kmem_free(oldbuf, sizeof(struct video_buffer *) * oldnbufs);

	/* initialize bufs */
	offset = 0;
	for (i = 0; i < vs->vs_nbufs; ++i) {
		buf = vs->vs_buf[i]->vb_buf;
		buf->index = i;
		buf->type = vs->vs_type;
		buf->bytesused = 0;
		buf->flags = 0;
		buf->field = 0;
		buf->sequence = 0;
		buf->memory = V4L2_MEMORY_MMAP;
		buf->m.offset = offset;
		buf->length = PAGE_ALIGN(vs->vs_format.sample_size);
		buf->reserved2 = 0;
		buf->reserved = 0;

		offset += buf->length;
	}

	return 0;
}

/* Accepts a video_sample into the ingress queue.  Caller must hold
 * the stream lock. */
void
video_stream_enqueue(struct video_stream *vs, struct video_buffer *vb)
{
	if (vb->vb_buf->flags & V4L2_BUF_FLAG_QUEUED) {
		DPRINTF(("video_stream_enqueue: sample already queued\n"));
		return;
	}

	vb->vb_buf->flags |= V4L2_BUF_FLAG_QUEUED;
	vb->vb_buf->flags &= ~V4L2_BUF_FLAG_DONE;

	vb->vb_buf->bytesused = 0;

	SIMPLEQ_INSERT_TAIL(&vs->vs_ingress, vb, entries);
}


/* Removes the head of the egress queue for use by userspace.  Caller
 * must hold the stream lock. */
struct video_buffer *
video_stream_dequeue(struct video_stream *vs)
{
	struct video_buffer *vb;

	if (!SIMPLEQ_EMPTY(&vs->vs_egress)) {
		vb = SIMPLEQ_FIRST(&vs->vs_egress);
		SIMPLEQ_REMOVE_HEAD(&vs->vs_egress, entries);
		vb->vb_buf->flags &= ~V4L2_BUF_FLAG_QUEUED;
		vb->vb_buf->flags |= V4L2_BUF_FLAG_DONE;
		return vb;
	} else {
		return NULL;
	}
}

static void
v4l2buf_set_timestamp(struct v4l2_buffer *buf)
{

	getmicrotime(&buf->timestamp);
}

/*
 * write payload data to the appropriate video sample, possibly moving
 * the sample from ingress to egress queues
 */
void
video_stream_write(struct video_stream *vs,
		   const struct video_payload *payload)
{
	struct video_buffer *vb;
	struct v4l2_buffer *buf;
	struct scatter_io sio;

	mutex_enter(&vs->vs_lock);

	/* change of frameno implies end of current frame */
	if (vs->vs_frameno >= 0 && vs->vs_frameno != payload->frameno)
		video_stream_sample_done(vs);

	vs->vs_frameno = payload->frameno;

	if (vs->vs_drop || SIMPLEQ_EMPTY(&vs->vs_ingress)) {
		/* DPRINTF(("video_stream_write: dropping sample %d\n",
		   vs->vs_sequence)); */
		vs->vs_drop = true;
	} else if (payload->size > 0) {
		vb = SIMPLEQ_FIRST(&vs->vs_ingress);
		buf = vb->vb_buf;
		if (!buf->bytesused)
			v4l2buf_set_timestamp(buf);
		if (payload->size > buf->length - buf->bytesused) {
			DPRINTF(("video_stream_write: "
				 "payload would overflow\n"));
		} else if (scatter_io_init(&vs->vs_data,
					   buf->m.offset + buf->bytesused,
					   payload->size,
					   &sio))
		{
			scatter_io_copyin(&sio, payload->data);
			buf->bytesused += (payload->size - sio.sio_resid);
		} else {
			DPRINTF(("video_stream_write: failed to init scatter io "
				 "vb=%p buf=%p "
				 "buf->m.offset=%d buf->bytesused=%u "
				 "payload->size=%zu\n",
				 vb, buf,
				 buf->m.offset, buf->bytesused, payload->size));
		}
	}

	/* if the payload marks it, we can do sample_done() early */
	if (payload->end_of_frame)
		video_stream_sample_done(vs);

	mutex_exit(&vs->vs_lock);
}


/* Moves the head of the ingress queue to the tail of the egress
 * queue, or resets drop status if we were dropping this sample.
 * Caller should hold the stream queue lock. */
void
video_stream_sample_done(struct video_stream *vs)
{
	struct video_buffer *vb;

	if (vs->vs_drop) {
		vs->vs_drop = false;
	} else if (!SIMPLEQ_EMPTY(&vs->vs_ingress)) {
		vb = SIMPLEQ_FIRST(&vs->vs_ingress);
		vb->vb_buf->sequence = vs->vs_sequence;
		SIMPLEQ_REMOVE_HEAD(&vs->vs_ingress, entries);

		SIMPLEQ_INSERT_TAIL(&vs->vs_egress, vb, entries);
		cv_signal(&vs->vs_sample_cv);
		selnotify(&vs->vs_sel, 0, 0);
	} else {
		DPRINTF(("video_stream_sample_done: no sample\n"));
	}

	vs->vs_frameno ^= 1;
	vs->vs_sequence++;
}

/* Check if all buffers are queued, i.e. none are under control of
 * userspace. */
/*
static bool
video_stream_all_queued(struct video_stream *vs)
{
}
*/


static void
scatter_buf_init(struct scatter_buf *sb)
{
	sb->sb_pool = pool_cache_init(PAGE_SIZE, 0, 0, 0,
				      "video", NULL, IPL_VIDEO,
				      NULL, NULL, NULL);
	sb->sb_size = 0;
	sb->sb_npages = 0;
	sb->sb_page_ary = NULL;
}

static void
scatter_buf_destroy(struct scatter_buf *sb)
{
	/* Do we need to return everything to the pool first? */
	scatter_buf_set_size(sb, 0);
	pool_cache_destroy(sb->sb_pool);
	sb->sb_pool = 0;
	sb->sb_npages = 0;
	sb->sb_page_ary = NULL;
}

/* Increase or decrease the size of the buffer */
static int
scatter_buf_set_size(struct scatter_buf *sb, size_t sz)
{
	int i;
	size_t npages, minpages, oldnpages;
	uint8_t **old_ary;

	npages = (sz >> PAGE_SHIFT) + ((sz & PAGE_MASK) > 0);

	if (sb->sb_npages == npages) {
		return 0;
	}

	oldnpages = sb->sb_npages;
	old_ary = sb->sb_page_ary;

	sb->sb_npages = npages;
	if (npages > 0) {
		sb->sb_page_ary =
		    kmem_alloc(sizeof(uint8_t *) * npages, KM_SLEEP);
	} else {
		sb->sb_page_ary = NULL;
	}

	minpages = uimin(npages, oldnpages);
	/* copy any pages that will be reused */
	for (i = 0; i < minpages; ++i)
		sb->sb_page_ary[i] = old_ary[i];
	/* allocate any new pages */
	for (; i < npages; ++i)
		sb->sb_page_ary[i] = pool_cache_get(sb->sb_pool, PR_WAITOK);
	/* return any pages no longer needed */
	for (; i < oldnpages; ++i)
		pool_cache_put(sb->sb_pool, old_ary[i]);

	if (old_ary != NULL)
		kmem_free(old_ary, sizeof(uint8_t *) * oldnpages);

	sb->sb_size = sb->sb_npages << PAGE_SHIFT;

	return 0;
}


static paddr_t
scatter_buf_map(struct scatter_buf *sb, off_t off)
{
	size_t pg;
	paddr_t pa;

	pg = off >> PAGE_SHIFT;

	if (pg >= sb->sb_npages)
		return -1;
	else if (!pmap_extract(pmap_kernel(), (vaddr_t)sb->sb_page_ary[pg], &pa))
		return -1;

	return atop(pa);
}

/* Initialize data for an io operation on a scatter buffer. Returns
 * true if the transfer is valid, or false if out of range. */
static bool
scatter_io_init(struct scatter_buf *sb,
		    off_t off, size_t len,
		    struct scatter_io *sio)
{
	if ((off + len) > sb->sb_size) {
		DPRINTF(("video: scatter_io_init failed: off=%" PRId64
			 " len=%zu sb->sb_size=%zu\n",
			 off, len, sb->sb_size));
		return false;
	}

	sio->sio_buf = sb;
	sio->sio_offset = off;
	sio->sio_resid = len;

	return true;
}

/* Store the pointer and size of the next contiguous segment.  Returns
 * true if the segment is valid, or false if all has been transferred.
 * Does not check for overflow. */
static bool
scatter_io_next(struct scatter_io *sio, void **p, size_t *sz)
{
	size_t pg, pgo;

	if (sio->sio_resid == 0)
		return false;

	pg = sio->sio_offset >> PAGE_SHIFT;
	pgo = sio->sio_offset & PAGE_MASK;

	*sz = uimin(PAGE_SIZE - pgo, sio->sio_resid);
	*p = sio->sio_buf->sb_page_ary[pg] + pgo;

	sio->sio_offset += *sz;
	sio->sio_resid -= *sz;

	return true;
}

/* Semi-undo of a failed segment copy.  Updates the scatter_io
 * struct to the previous values prior to a failed segment copy. */
static void
scatter_io_undo(struct scatter_io *sio, size_t sz)
{
	sio->sio_offset -= sz;
	sio->sio_resid += sz;
}

/* Copy data from src into the scatter_buf as described by io. */
static void
scatter_io_copyin(struct scatter_io *sio, const void *p)
{
	void *dst;
	const uint8_t *src = p;
	size_t sz;

	while(scatter_io_next(sio, &dst, &sz)) {
		memcpy(dst, src, sz);
		src += sz;
	}
}

/* --not used; commented to avoid compiler warnings--
static void
scatter_io_copyout(struct scatter_io *sio, void *p)
{
	void *src;
	uint8_t *dst = p;
	size_t sz;

	while(scatter_io_next(sio, &src, &sz)) {
		memcpy(dst, src, sz);
		dst += sz;
	}
}
*/

/* Performat a series of uiomove calls on a scatter buf.  Returns
 * EFAULT if uiomove EFAULTs on the first segment.  Otherwise, returns
 * an incomplete transfer but with no error. */
static int
scatter_io_uiomove(struct scatter_io *sio, struct uio *uio)
{
	void *p;
	size_t sz;
	bool first = true;
	int err;

	while(scatter_io_next(sio, &p, &sz)) {
		err = uiomove(p, sz, uio);
		if (err == EFAULT) {
			scatter_io_undo(sio, sz);
			if (first)
				return EFAULT;
			else
				return 0;
		}
		first = false;
	}

	return 0;
}

#endif /* NVIDEO > 0 */