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
/*	$NetBSD: uhso.c,v 1.30 2019/05/05 03:17:54 mrg Exp $	*/

/*-
 * Copyright (c) 2009 Iain Hibbert
 * Copyright (c) 2008 Fredrik Lindberg
 * 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.
 * 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 AUTHOR ``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 AUTHOR 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 driver originated as the hso module for FreeBSD written by
 * Fredrik Lindberg[1]. It has been rewritten almost completely for
 * NetBSD, and to support more devices with information extracted from
 * the Linux hso driver provided by Option N.V.[2]
 *
 *   [1] http://www.shapeshifter.se/code/hso
 *   [2] http://www.pharscape.org/hso.htm
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uhso.c,v 1.30 2019/05/05 03:17:54 mrg Exp $");

#ifdef _KERNEL_OPT
#include "opt_inet.h"
#include "opt_usb.h"
#endif

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/fcntl.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/mbuf.h>
#include <sys/poll.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/vnode.h>
#include <sys/lwp.h>

#include <net/bpf.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/netisr.h>

#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbcdc.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/umassvar.h>

#include <dev/scsipi/scsi_disk.h>

#include "usbdevs.h"
#include "ioconf.h"

#undef DPRINTF
#ifdef UHSO_DEBUG
/*
 * defined levels
 *	0	warnings only
 *	1	informational
 *	5	really chatty
 */
int uhso_debug = 0;

#define DPRINTF(n, ...)	do {			\
	if (uhso_debug >= (n)) {		\
		printf("%s: ", __func__);	\
		printf(__VA_ARGS__);		\
	}					\
} while (/* CONSTCOND */0)
#else
#define DPRINTF(...)	((void)0)
#endif

/*
 * When first attached, the device class will be 0 and the modem
 * will attach as UMASS until a SCSI REZERO_UNIT command is sent,
 * in which case it will detach and reattach with device class set
 * to UDCLASS_VENDOR (0xff) and provide the serial interfaces.
 *
 * If autoswitch is set (the default) this will happen automatically.
 */
Static int uhso_autoswitch = 1;

SYSCTL_SETUP(sysctl_hw_uhso_setup, "uhso sysctl setup")
{
	const struct sysctlnode *node = NULL;

	sysctl_createv(clog, 0, NULL, &node,
		CTLFLAG_PERMANENT,
		CTLTYPE_NODE, "uhso",
		NULL,
		NULL, 0,
		NULL, 0,
		CTL_HW, CTL_CREATE, CTL_EOL);

	if (node == NULL)
		return;

#ifdef UHSO_DEBUG
	sysctl_createv(clog, 0, &node, NULL,
		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
		CTLTYPE_INT, "debug",
		SYSCTL_DESCR("uhso debug level (0, 1, 5)"),
		NULL, 0,
		&uhso_debug, sizeof(uhso_debug),
		CTL_CREATE, CTL_EOL);
#endif

	sysctl_createv(clog, 0, &node, NULL,
		CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
		CTLTYPE_INT, "autoswitch",
		SYSCTL_DESCR("automatically switch device into modem mode"),
		NULL, 0,
		&uhso_autoswitch, sizeof(uhso_autoswitch),
		CTL_CREATE, CTL_EOL);
}

/*
 * The uhso modems have a number of interfaces providing a variety of
 * IO ports using the bulk endpoints, or multiplexed on the control
 * endpoints. We separate the ports by function and provide each with
 * a predictable index number used to construct the device minor number.
 *
 * The Network port is configured as a network interface rather than
 * a tty as it provides raw IPv4 packets.
 */

Static const char *uhso_port_name[] = {
	"Control",
	"Diagnostic",
	"Diagnostic2",
	"Application",
	"Application2",
	"GPS",
	"GPS Control",
	"PC Smartcard",
	"Modem",
	"MSD",			/* "Modem Sharing Device" ? */
	"Voice",
	"Network",
};

#define UHSO_PORT_CONTROL	0x00
#define UHSO_PORT_DIAG		0x01
#define UHSO_PORT_DIAG2		0x02
#define UHSO_PORT_APP		0x03
#define UHSO_PORT_APP2		0x04
#define UHSO_PORT_GPS		0x05
#define UHSO_PORT_GPS_CONTROL	0x06
#define UHSO_PORT_PCSC		0x07
#define UHSO_PORT_MODEM		0x08
#define UHSO_PORT_MSD		0x09
#define UHSO_PORT_VOICE		0x0a
#define UHSO_PORT_NETWORK	0x0b

#define UHSO_PORT_MAX		__arraycount(uhso_port_name)

#define UHSO_IFACE_MUX		0x20
#define UHSO_IFACE_BULK		0x40
#define UHSO_IFACE_IFNET	0x80

/*
 * The interface specification can sometimes be deduced from the device
 * type and interface number, or some modems support a vendor specific
 * way to read config info which we can translate to the port index.
 */
Static const uint8_t uhso_spec_default[] = {
	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK | UHSO_IFACE_MUX,
	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
};

Static const uint8_t uhso_spec_icon321[] = {
	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK | UHSO_IFACE_MUX,
	UHSO_IFACE_BULK | UHSO_PORT_DIAG2,
	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
};

Static const uint8_t uhso_spec_config[] = {
	0,
	UHSO_IFACE_BULK | UHSO_PORT_DIAG,
	UHSO_IFACE_BULK | UHSO_PORT_GPS,
	UHSO_IFACE_BULK | UHSO_PORT_GPS_CONTROL,
	UHSO_IFACE_BULK | UHSO_PORT_APP,
	UHSO_IFACE_BULK | UHSO_PORT_APP2,
	UHSO_IFACE_BULK | UHSO_PORT_CONTROL,
	UHSO_IFACE_IFNET | UHSO_PORT_NETWORK,
	UHSO_IFACE_BULK | UHSO_PORT_MODEM,
	UHSO_IFACE_BULK | UHSO_PORT_MSD,
	UHSO_IFACE_BULK | UHSO_PORT_PCSC,
	UHSO_IFACE_BULK | UHSO_PORT_VOICE,
};

struct uhso_dev {
	uint16_t vendor;
	uint16_t product;
	uint16_t type;
};

#define UHSOTYPE_DEFAULT	1
#define UHSOTYPE_ICON321	2
#define UHSOTYPE_CONFIG		3

Static const struct uhso_dev uhso_devs[] = {
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MAXHSDPA,    UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GSICON72,    UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON225,     UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GEHSUPA,     UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTHSUPA,     UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GSHSUPA,     UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X1,      UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X2,      UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X3,      UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON401,     UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTM382,	     UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GE40X4,      UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_GTHSUPAM,    UHSOTYPE_CONFIG },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICONEDGE,    UHSOTYPE_DEFAULT },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_MODHSXPA,    UHSOTYPE_ICON321 },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON321,     UHSOTYPE_ICON321 },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON322,     UHSOTYPE_ICON321 },
    { USB_VENDOR_OPTIONNV, USB_PRODUCT_OPTIONNV_ICON505,     UHSOTYPE_CONFIG },
};

#define uhso_lookup(p, v)  ((const struct uhso_dev *)usb_lookup(uhso_devs, (p), (v)))

/* IO buffer sizes */
#define UHSO_MUX_WSIZE		64
#define UHSO_MUX_RSIZE		1024
#define UHSO_BULK_WSIZE		8192
#define UHSO_BULK_RSIZE		4096
#define UHSO_IFNET_MTU		1500

/*
 * Each IO port provided by the modem can be mapped to a network
 * interface (when hp_ifp != NULL) or a tty (when hp_tp != NULL)
 * which may be multiplexed and sharing interrupt and control endpoints
 * from an interface, or using the dedicated bulk endpoints.
 */

struct uhso_port;
struct uhso_softc;

/* uhso callback functions return errno on failure */
typedef int (*uhso_callback)(struct uhso_port *);

struct uhso_port {
	struct uhso_softc      *hp_sc;		/* master softc */
	struct tty	       *hp_tp;		/* tty pointer */
	struct ifnet	       *hp_ifp;		/* ifnet pointer */
	unsigned int		hp_flags;	/* see below */
	int			hp_swflags;	/* persistent tty flags */
	int			hp_status;	/* modem status */

	/* port type specific handlers */
	uhso_callback		hp_abort;	/* abort any transfers */
	uhso_callback		hp_detach;	/* detach port completely */
	uhso_callback		hp_init;	/* init port (first open) */
	uhso_callback		hp_clean;	/* clean port (last close) */
	uhso_callback		hp_write;	/* write data */
	usbd_callback		hp_write_cb;	/* write callback */
	uhso_callback		hp_read;	/* read data */
	usbd_callback		hp_read_cb;	/* read callback */
	uhso_callback		hp_control;	/* set control lines */

	struct usbd_interface  *hp_ifh;		/* interface handle */
	unsigned int		hp_index;	/* usb request index */

	int			hp_iaddr;	/* interrupt endpoint */
	struct usbd_pipe       *hp_ipipe;	/* interrupt pipe */
	void		       *hp_ibuf;	/* interrupt buffer */
	size_t			hp_isize;	/* allocated size */

	int			hp_raddr;	/* bulk in endpoint */
	struct usbd_pipe       *hp_rpipe;	/* bulk in pipe */
	struct usbd_xfer       *hp_rxfer;	/* input xfer */
	void		       *hp_rbuf;	/* input buffer */
	size_t			hp_rlen;	/* fill length */
	size_t			hp_rsize;	/* allocated size */

	int			hp_waddr;	/* bulk out endpoint */
	struct usbd_pipe       *hp_wpipe;	/* bulk out pipe */
	struct usbd_xfer       *hp_wxfer;	/* output xfer */
	void		       *hp_wbuf;	/* output buffer */
	size_t			hp_wlen;	/* fill length */
	size_t			hp_wsize;	/* allocated size */

	struct mbuf	       *hp_mbuf;	/* partial packet */
};

/* hp_flags */
#define UHSO_PORT_MUXPIPE	__BIT(0)	/* duplicate ipipe/ibuf references */
#define UHSO_PORT_MUXREADY	__BIT(1)	/* input is ready */
#define UHSO_PORT_MUXBUSY	__BIT(2)	/* read in progress */

struct uhso_softc {
	device_t		sc_dev;		/* self */
	struct usbd_device     *sc_udev;
	int			sc_refcnt;
	struct uhso_port       *sc_port[UHSO_PORT_MAX];
};

#define UHSO_CONFIG_NO		1

int uhso_match(device_t, cfdata_t, void *);
void uhso_attach(device_t, device_t, void *);
int uhso_detach(device_t, int);



CFATTACH_DECL_NEW(uhso, sizeof(struct uhso_softc), uhso_match, uhso_attach,
    uhso_detach, NULL);

Static int uhso_switch_mode(struct usbd_device *);
Static int uhso_get_iface_spec(struct usb_attach_arg *, uint8_t, uint8_t *);
Static usb_endpoint_descriptor_t *uhso_get_endpoint(struct usbd_interface *,
    int, int);

Static void uhso_mux_attach(struct uhso_softc *, struct usbd_interface *, int);
Static int  uhso_mux_abort(struct uhso_port *);
Static int  uhso_mux_detach(struct uhso_port *);
Static int  uhso_mux_init(struct uhso_port *);
Static int  uhso_mux_clean(struct uhso_port *);
Static int  uhso_mux_write(struct uhso_port *);
Static int  uhso_mux_read(struct uhso_port *);
Static int  uhso_mux_control(struct uhso_port *);
Static void uhso_mux_intr(struct usbd_xfer *, void *, usbd_status);

Static void uhso_bulk_attach(struct uhso_softc *, struct usbd_interface *, int);
Static int  uhso_bulk_abort(struct uhso_port *);
Static int  uhso_bulk_detach(struct uhso_port *);
Static int  uhso_bulk_init(struct uhso_port *);
Static int  uhso_bulk_clean(struct uhso_port *);
Static int  uhso_bulk_write(struct uhso_port *);
Static int  uhso_bulk_read(struct uhso_port *);
Static int  uhso_bulk_control(struct uhso_port *);
Static void uhso_bulk_intr(struct usbd_xfer *, void *, usbd_status);

Static void uhso_tty_attach(struct uhso_port *);
Static void uhso_tty_detach(struct uhso_port *);
Static void uhso_tty_read_cb(struct usbd_xfer *, void *, usbd_status);
Static void uhso_tty_write_cb(struct usbd_xfer *, void *, usbd_status);

dev_type_open(uhso_tty_open);
dev_type_close(uhso_tty_close);
dev_type_read(uhso_tty_read);
dev_type_write(uhso_tty_write);
dev_type_ioctl(uhso_tty_ioctl);
dev_type_stop(uhso_tty_stop);
dev_type_tty(uhso_tty_tty);
dev_type_poll(uhso_tty_poll);

const struct cdevsw uhso_cdevsw = {
	.d_open = uhso_tty_open,
	.d_close = uhso_tty_close,
	.d_read = uhso_tty_read,
	.d_write = uhso_tty_write,
	.d_ioctl = uhso_tty_ioctl,
	.d_stop = uhso_tty_stop,
	.d_tty = uhso_tty_tty,
	.d_poll = uhso_tty_poll,
	.d_mmap = nommap,
	.d_kqfilter = ttykqfilter,
	.d_discard = nodiscard,
	.d_flag = D_TTY
};

Static int  uhso_tty_init(struct uhso_port *);
Static void uhso_tty_clean(struct uhso_port *);
Static int  uhso_tty_do_ioctl(struct uhso_port *, u_long, void *, int, struct lwp *);
Static void uhso_tty_start(struct tty *);
Static int  uhso_tty_param(struct tty *, struct termios *);
Static int  uhso_tty_control(struct uhso_port *, u_long, int);

#define UHSO_UNIT_MASK		TTUNIT_MASK
#define UHSO_PORT_MASK		0x0000f
#define UHSO_DIALOUT_MASK	TTDIALOUT_MASK
#define UHSO_CALLUNIT_MASK	TTCALLUNIT_MASK

#define UHSOUNIT(x)	(TTUNIT(x) >> 4)
#define UHSOPORT(x)	(TTUNIT(x) & UHSO_PORT_MASK)
#define UHSODIALOUT(x)	TTDIALOUT(x)
#define UHSOMINOR(u, p)	((((u) << 4) & UHSO_UNIT_MASK) | ((p) & UHSO_UNIT_MASK))

Static void uhso_ifnet_attach(struct uhso_softc *, struct usbd_interface *,
    int);
Static int  uhso_ifnet_abort(struct uhso_port *);
Static int  uhso_ifnet_detach(struct uhso_port *);
Static void uhso_ifnet_read_cb(struct usbd_xfer *, void *, usbd_status);
Static void uhso_ifnet_input(struct ifnet *, struct mbuf **, uint8_t *, size_t);
Static void uhso_ifnet_write_cb(struct usbd_xfer *, void *, usbd_status);

Static int  uhso_ifnet_ioctl(struct ifnet *, u_long, void *);
Static int  uhso_ifnet_init(struct uhso_port *);
Static void uhso_ifnet_clean(struct uhso_port *);
Static void uhso_ifnet_start(struct ifnet *);
Static int  uhso_ifnet_output(struct ifnet *, struct mbuf *,
    const struct sockaddr *, const struct rtentry *);


/*******************************************************************************
 *
 *	USB autoconfig
 *
 */

int
uhso_match(device_t parent, cfdata_t match, void *aux)
{
	struct usb_attach_arg *uaa = aux;

	/*
	 * don't claim this device if autoswitch is disabled
	 * and it is not in modem mode already
	 */
	if (!uhso_autoswitch && uaa->uaa_class != UDCLASS_VENDOR)
		return UMATCH_NONE;

	if (uhso_lookup(uaa->uaa_vendor, uaa->uaa_product))
		return UMATCH_VENDOR_PRODUCT;

	return UMATCH_NONE;
}

void
uhso_attach(device_t parent, device_t self, void *aux)
{
	struct uhso_softc *sc = device_private(self);
	struct usb_attach_arg *uaa = aux;
	struct usbd_interface *ifh;
	char *devinfop;
	uint8_t count, i, spec;
	usbd_status status;

	DPRINTF(1, ": sc = %p, self=%p", sc, self);

	sc->sc_dev = self;
	sc->sc_udev = uaa->uaa_device;

	aprint_naive("\n");
	aprint_normal("\n");

	devinfop = usbd_devinfo_alloc(uaa->uaa_device, 0);
	aprint_normal_dev(self, "%s\n", devinfop);
	usbd_devinfo_free(devinfop);

	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);

	status = usbd_set_config_no(sc->sc_udev, UHSO_CONFIG_NO, 1);
	if (status != USBD_NORMAL_COMPLETION) {
		aprint_error_dev(self, "failed to set configuration"
		    ", err=%s\n", usbd_errstr(status));
		return;
	}

	if (uaa->uaa_class != UDCLASS_VENDOR) {
		aprint_verbose_dev(self,
		    "Switching device into modem mode..\n");
		if (uhso_switch_mode(uaa->uaa_device) != 0)
			aprint_error_dev(self, "modem switch failed\n");

		return;
	}

	count = 0;
	(void)usbd_interface_count(sc->sc_udev, &count);
	DPRINTF(1, "interface count %d\n", count);

	for (i = 0; i < count; i++) {
		status = usbd_device2interface_handle(sc->sc_udev, i, &ifh);
		if (status != USBD_NORMAL_COMPLETION) {
			aprint_error_dev(self,
			    "could not get interface %d: %s\n",
			    i, usbd_errstr(status));

			return;
		}

		if (!uhso_get_iface_spec(uaa, i, &spec)) {
			aprint_error_dev(self,
			    "could not get interface %d specification\n", i);

			return;
		}

		if (ISSET(spec, UHSO_IFACE_MUX))
			uhso_mux_attach(sc, ifh, UHSOPORT(spec));

		if (ISSET(spec, UHSO_IFACE_BULK))
			uhso_bulk_attach(sc, ifh, UHSOPORT(spec));

		if (ISSET(spec, UHSO_IFACE_IFNET))
			uhso_ifnet_attach(sc, ifh, UHSOPORT(spec));
	}

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

int
uhso_detach(device_t self, int flags)
{
	struct uhso_softc *sc = device_private(self);
	struct uhso_port *hp;
	devmajor_t major;
	devminor_t minor;
	unsigned int i;
	int s;

	pmf_device_deregister(self);

	for (i = 0; i < UHSO_PORT_MAX; i++) {
		hp = sc->sc_port[i];
		if (hp != NULL)
			(*hp->hp_abort)(hp);
	}

	s = splusb();
	if (sc->sc_refcnt-- > 0) {
		DPRINTF(1, "waiting for refcnt (%d)..\n", sc->sc_refcnt);
		usb_detach_waitold(sc->sc_dev);
	}
	splx(s);

	/*
	 * XXX the tty close routine increases/decreases refcnt causing
	 * XXX another usb_detach_wakeupold() does it matter, should these
	 * XXX be before the detach_wait? or before the abort?
	 */

	/* Nuke the vnodes for any open instances (calls close). */
	major = cdevsw_lookup_major(&uhso_cdevsw);
	minor = UHSOMINOR(device_unit(sc->sc_dev), 0);
	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);
	minor = UHSOMINOR(device_unit(sc->sc_dev), 0) | UHSO_DIALOUT_MASK;
	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);
	minor = UHSOMINOR(device_unit(sc->sc_dev), 0) | UHSO_CALLUNIT_MASK;
	vdevgone(major, minor, minor + UHSO_PORT_MAX, VCHR);

	for (i = 0; i < UHSO_PORT_MAX; i++) {
		hp = sc->sc_port[i];
		if (hp != NULL)
			(*hp->hp_detach)(hp);
	}

	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);

	return 0;
}

/*
 * Send SCSI REZERO_UNIT command to switch device into modem mode
 */
Static int
uhso_switch_mode(struct usbd_device *udev)
{
	umass_bbb_cbw_t	cmd;
	usb_endpoint_descriptor_t *ed;
	struct usbd_interface *ifh;
	struct usbd_pipe *pipe;
	struct usbd_xfer *xfer;
	usbd_status status;

	status = usbd_device2interface_handle(udev, 0, &ifh);
	if (status != USBD_NORMAL_COMPLETION)
		return EIO;

	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
	if (ed == NULL)
		return ENODEV;

	status = usbd_open_pipe(ifh, ed->bEndpointAddress, 0, &pipe);
	if (status != USBD_NORMAL_COMPLETION)
		return EIO;

	int error = usbd_create_xfer(pipe, sizeof(cmd), 0, 0, &xfer);
	if (error)
		return error;

	USETDW(cmd.dCBWSignature, CBWSIGNATURE);
	USETDW(cmd.dCBWTag, 1);
	USETDW(cmd.dCBWDataTransferLength, 0);
	cmd.bCBWFlags = CBWFLAGS_OUT;
	cmd.bCBWLUN = 0;
	cmd.bCDBLength = 6;

	memset(&cmd.CBWCDB, 0, CBWCDBLENGTH);
	cmd.CBWCDB[0] = SCSI_REZERO_UNIT;

	usbd_setup_xfer(xfer, NULL, &cmd, sizeof(cmd),
		USBD_SYNCHRONOUS, USBD_DEFAULT_TIMEOUT, NULL);

	status = usbd_transfer(xfer);

	usbd_destroy_xfer(xfer);
	usbd_close_pipe(pipe);

	return status == USBD_NORMAL_COMPLETION ? 0 : EIO;
}

Static int
uhso_get_iface_spec(struct usb_attach_arg *uaa, uint8_t ifnum, uint8_t *spec)
{
	const struct uhso_dev *hd;
	uint8_t config[17];
	usb_device_request_t req;
	usbd_status status;

	hd = uhso_lookup(uaa->uaa_vendor, uaa->uaa_product);
	KASSERT(hd != NULL);

	switch (hd->type) {
	case UHSOTYPE_DEFAULT:
		if (ifnum >= __arraycount(uhso_spec_default))
			break;

		*spec = uhso_spec_default[ifnum];
		return 1;

	case UHSOTYPE_ICON321:
		if (ifnum >= __arraycount(uhso_spec_icon321))
			break;

		*spec = uhso_spec_icon321[ifnum];
		return 1;

	case UHSOTYPE_CONFIG:
		req.bmRequestType = UT_READ_VENDOR_DEVICE;
		req.bRequest = 0x86;	/* "Config Info" */
		USETW(req.wValue, 0);
		USETW(req.wIndex, 0);
		USETW(req.wLength, sizeof(config));

		status = usbd_do_request(uaa->uaa_device, &req, config);
		if (status != USBD_NORMAL_COMPLETION)
			break;

		if (ifnum >= __arraycount(config)
		    || config[ifnum] >= __arraycount(uhso_spec_config))
			break;

		*spec = uhso_spec_config[config[ifnum]];

		/*
		 * Apparently some modems also have a CRC bug that is
		 * indicated by ISSET(config[16], __BIT(0)) but we dont
		 * handle it at this time.
		 */
		return 1;

	default:
		DPRINTF(0, "unknown interface type\n");
		break;
	}

	return 0;
}

Static usb_endpoint_descriptor_t *
uhso_get_endpoint(struct usbd_interface *ifh, int type, int dir)
{
	usb_endpoint_descriptor_t *ed;
	uint8_t count, i;

	count = 0;
	(void)usbd_endpoint_count(ifh, &count);

	for (i = 0; i < count; i++) {
		ed = usbd_interface2endpoint_descriptor(ifh, i);
		if (ed != NULL
		    && UE_GET_XFERTYPE(ed->bmAttributes) == type
		    && UE_GET_DIR(ed->bEndpointAddress) == dir)
			return ed;
	}

	return NULL;
}


/******************************************************************************
 *
 *	Multiplexed ports signal with the interrupt endpoint to indicate
 *  when data is available for reading, and a separate request is made on
 *  the control endpoint to read or write on each port. The offsets in the
 *  table below relate to bit numbers in the mux mask, identifying each port.
 */

Static const int uhso_mux_port[] = {
	UHSO_PORT_CONTROL,
	UHSO_PORT_APP,
	UHSO_PORT_PCSC,
	UHSO_PORT_GPS,
	UHSO_PORT_APP2,
};

Static void
uhso_mux_attach(struct uhso_softc *sc, struct usbd_interface *ifh, int index)
{
	usbd_desc_iter_t iter;
	const usb_descriptor_t *desc;
	usb_endpoint_descriptor_t *ed;
	struct usbd_pipe *pipe;
	struct uhso_port *hp;
	uint8_t *buf;
	size_t size;
	unsigned int i, mux, flags;
	int addr;
	usbd_status status;

	ed = uhso_get_endpoint(ifh, UE_INTERRUPT, UE_DIR_IN);
	if (ed == NULL) {
		aprint_error_dev(sc->sc_dev, "no interrupt endpoint\n");
		return;
	}
	addr = ed->bEndpointAddress;
	size = UGETW(ed->wMaxPacketSize);

	/*
	 * There should be an additional "Class Specific" descriptor on
	 * the mux interface containing a single byte with a bitmask of
	 * enabled ports. We need to look through the device descriptor
	 * to find it and the port index is found from the uhso_mux_port
	 * array, above.
	 */
	usb_desc_iter_init(sc->sc_udev, &iter);

	/* skip past the current interface descriptor */
	iter.cur = (const uByte *)usbd_get_interface_descriptor(ifh);
	desc = usb_desc_iter_next(&iter);

	for (;;) {
		desc = usb_desc_iter_next(&iter);
		if (desc == NULL
		    || desc->bDescriptorType == UDESC_INTERFACE) {
			mux = 0;
			break;	/* not found */
		}

		if (desc->bDescriptorType == UDESC_CS_INTERFACE
		    && desc->bLength == 3) {
			mux = ((const uint8_t *)desc)[2];
			break;
		}
	}

	DPRINTF(1, "addr=%d, size=%zd, mux=0x%02x\n", addr, size, mux);

	buf = kmem_alloc(size, KM_SLEEP);
	status = usbd_open_pipe_intr(ifh, addr, USBD_SHORT_XFER_OK, &pipe,
	    sc, buf, size, uhso_mux_intr, USBD_DEFAULT_INTERVAL);

	if (status != USBD_NORMAL_COMPLETION) {
		aprint_error_dev(sc->sc_dev,
		    "failed to open interrupt pipe: %s", usbd_errstr(status));

		kmem_free(buf, size);
		return;
	}

	flags = 0;
	for (i = 0; i < __arraycount(uhso_mux_port); i++) {
		if (ISSET(mux, __BIT(i))) {
			if (sc->sc_port[uhso_mux_port[i]] != NULL) {
				aprint_error_dev(sc->sc_dev,
				    "mux port %d is duplicate!\n", i);

				continue;
			}

			hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
			sc->sc_port[uhso_mux_port[i]] = hp;

			hp->hp_sc = sc;
			hp->hp_index = i;
			hp->hp_ipipe = pipe;
			hp->hp_ibuf = buf;
			hp->hp_isize = size;
			hp->hp_flags = flags;
			hp->hp_abort = uhso_mux_abort;
			hp->hp_detach = uhso_mux_detach;
			hp->hp_init = uhso_mux_init;
			hp->hp_clean = uhso_mux_clean;
			hp->hp_write = uhso_mux_write;
			hp->hp_write_cb = uhso_tty_write_cb;
			hp->hp_read = uhso_mux_read;
			hp->hp_read_cb = uhso_tty_read_cb;
			hp->hp_control = uhso_mux_control;
			hp->hp_wsize = UHSO_MUX_WSIZE;
			hp->hp_rsize = UHSO_MUX_RSIZE;

			uhso_tty_attach(hp);

			aprint_normal_dev(sc->sc_dev,
			    "%s (port %d) attached as mux tty\n",
			    uhso_port_name[uhso_mux_port[i]], uhso_mux_port[i]);

			/*
			 * As the pipe handle is stored in each mux, mark
			 * secondary references so they don't get released
			 */
			flags = UHSO_PORT_MUXPIPE;
		}
	}

	if (flags == 0) {
		/* for whatever reasons, nothing was attached */
		usbd_abort_pipe(pipe);
		usbd_close_pipe(pipe);
		kmem_free(buf, size);
	}
}

Static int
uhso_mux_abort(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;

	DPRINTF(1, "hp=%p\n", hp);

	if (!ISSET(hp->hp_flags, UHSO_PORT_MUXPIPE))
		usbd_abort_pipe(hp->hp_ipipe);

	usbd_abort_default_pipe(sc->sc_udev);

	return (*hp->hp_clean)(hp);
}

Static int
uhso_mux_detach(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	if (!ISSET(hp->hp_flags, UHSO_PORT_MUXPIPE)) {
		DPRINTF(1, "interrupt pipe closed\n");
		usbd_abort_pipe(hp->hp_ipipe);
		usbd_close_pipe(hp->hp_ipipe);
		kmem_free(hp->hp_ibuf, hp->hp_isize);
	}

	uhso_tty_detach(hp);
	kmem_free(hp, sizeof(struct uhso_port));
	return 0;
}

Static int
uhso_mux_init(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	CLR(hp->hp_flags, UHSO_PORT_MUXBUSY | UHSO_PORT_MUXREADY);
	SET(hp->hp_status, TIOCM_DSR | TIOCM_CAR);

	struct uhso_softc *sc = hp->hp_sc;
	struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->sc_udev);
	int error;

	error = usbd_create_xfer(pipe0, hp->hp_rsize, 0, 0, &hp->hp_rxfer);
	if (error)
		return error;

	hp->hp_rbuf = usbd_get_buffer(hp->hp_rxfer);

	error = usbd_create_xfer(pipe0, hp->hp_wsize, 0, 0, &hp->hp_wxfer);
	if (error)
		return error;

	hp->hp_wbuf = usbd_get_buffer(hp->hp_wxfer);

	return 0;
}

Static int
uhso_mux_clean(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	CLR(hp->hp_flags, UHSO_PORT_MUXREADY);
	CLR(hp->hp_status, TIOCM_DTR | TIOCM_DSR | TIOCM_CAR);
	return 0;
}

Static int
uhso_mux_write(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	usb_device_request_t req;
	usbd_status status;

	DPRINTF(5, "hp=%p, index=%d, wlen=%zd\n", hp, hp->hp_index,
	    hp->hp_wlen);

	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	req.bRequest = UCDC_SEND_ENCAPSULATED_COMMAND;
	USETW(req.wValue, 0);
	USETW(req.wIndex, hp->hp_index);
	USETW(req.wLength, hp->hp_wlen);

	usbd_setup_default_xfer(hp->hp_wxfer, sc->sc_udev, hp, USBD_NO_TIMEOUT,
	    &req, hp->hp_wbuf, hp->hp_wlen, 0, hp->hp_write_cb);

	status = usbd_transfer(hp->hp_wxfer);
	if (status != USBD_IN_PROGRESS) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return EIO;
	}

	sc->sc_refcnt++;
	return 0;
}

Static int
uhso_mux_read(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	usb_device_request_t req;
	usbd_status status;

	CLR(hp->hp_flags, UHSO_PORT_MUXBUSY);

	if (hp->hp_rlen == 0 && !ISSET(hp->hp_flags, UHSO_PORT_MUXREADY))
		return 0;

	SET(hp->hp_flags, UHSO_PORT_MUXBUSY);
	CLR(hp->hp_flags, UHSO_PORT_MUXREADY);

	DPRINTF(5, "hp=%p, index=%d\n", hp, hp->hp_index);

	req.bmRequestType = UT_READ_CLASS_INTERFACE;
	req.bRequest = UCDC_GET_ENCAPSULATED_RESPONSE;
	USETW(req.wValue, 0);
	USETW(req.wIndex, hp->hp_index);
	USETW(req.wLength, hp->hp_rsize);

	usbd_setup_default_xfer(hp->hp_rxfer, sc->sc_udev, hp, USBD_NO_TIMEOUT,
	    &req, hp->hp_rbuf, hp->hp_rsize, USBD_SHORT_XFER_OK,
	    hp->hp_read_cb);

	status = usbd_transfer(hp->hp_rxfer);
	if (status != USBD_IN_PROGRESS) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		CLR(hp->hp_flags, UHSO_PORT_MUXBUSY);
		return EIO;
	}

	sc->sc_refcnt++;
	return 0;
}

Static int
uhso_mux_control(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	return 0;
}

Static void
uhso_mux_intr(struct usbd_xfer *xfer, void * p, usbd_status status)
{
	struct uhso_softc *sc = p;
	struct uhso_port *hp;
	uint32_t cc;
	uint8_t *buf;
	unsigned int i;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return;
	}

	usbd_get_xfer_status(xfer, NULL, (void **)&buf, &cc, NULL);
	if (cc == 0)
		return;

	DPRINTF(5, "mux mask 0x%02x, cc=%u\n", buf[0], cc);

	for (i = 0; i < __arraycount(uhso_mux_port); i++) {
		if (!ISSET(buf[0], __BIT(i)))
			continue;

		DPRINTF(5, "mux %d port %d\n", i, uhso_mux_port[i]);
		hp = sc->sc_port[uhso_mux_port[i]];
		if (hp == NULL
		    || hp->hp_tp == NULL
		    || !ISSET(hp->hp_status, TIOCM_DTR))
			continue;

		SET(hp->hp_flags, UHSO_PORT_MUXREADY);
		if (ISSET(hp->hp_flags, UHSO_PORT_MUXBUSY))
			continue;

		uhso_mux_read(hp);
	}
}


/******************************************************************************
 *
 *	Bulk ports operate using the bulk endpoints on an interface, though
 *   the Modem port (at least) may have an interrupt endpoint that will pass
 *   CDC Notification messages with the modem status.
 */

Static void
uhso_bulk_attach(struct uhso_softc *sc, struct usbd_interface *ifh, int index)
{
	usb_endpoint_descriptor_t *ed;
	usb_interface_descriptor_t *id;
	struct uhso_port *hp;
	int in, out;

	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_IN);
	if (ed == NULL) {
		aprint_error_dev(sc->sc_dev, "bulk-in endpoint not found\n");
		return;
	}
	in = ed->bEndpointAddress;

	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
	if (ed == NULL) {
		aprint_error_dev(sc->sc_dev, "bulk-out endpoint not found\n");
		return;
	}
	out = ed->bEndpointAddress;

	id = usbd_get_interface_descriptor(ifh);
	if (id == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "interface descriptor not found\n");
		return;
	}

	DPRINTF(1, "bulk endpoints in=%x, out=%x\n", in, out);

	if (sc->sc_port[index] != NULL) {
		aprint_error_dev(sc->sc_dev, "bulk port %d is duplicate!\n",
		    index);

		return;
	}

	hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
	sc->sc_port[index] = hp;

	hp->hp_sc = sc;
	hp->hp_ifh = ifh;
	hp->hp_index = id->bInterfaceNumber;
	hp->hp_raddr = in;
	hp->hp_waddr = out;
	hp->hp_abort = uhso_bulk_abort;
	hp->hp_detach = uhso_bulk_detach;
	hp->hp_init = uhso_bulk_init;
	hp->hp_clean = uhso_bulk_clean;
	hp->hp_write = uhso_bulk_write;
	hp->hp_write_cb = uhso_tty_write_cb;
	hp->hp_read = uhso_bulk_read;
	hp->hp_read_cb = uhso_tty_read_cb;
	hp->hp_control = uhso_bulk_control;
	hp->hp_wsize = UHSO_BULK_WSIZE;
	hp->hp_rsize = UHSO_BULK_RSIZE;

	if (index == UHSO_PORT_MODEM) {
		ed = uhso_get_endpoint(ifh, UE_INTERRUPT, UE_DIR_IN);
		if (ed != NULL) {
			hp->hp_iaddr = ed->bEndpointAddress;
			hp->hp_isize = UGETW(ed->wMaxPacketSize);
		}
	}

	uhso_tty_attach(hp);

	aprint_normal_dev(sc->sc_dev,
	    "%s (port %d) attached as bulk tty\n",
	    uhso_port_name[index], index);
}

Static int
uhso_bulk_abort(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	return (*hp->hp_clean)(hp);
}

Static int
uhso_bulk_detach(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	uhso_tty_detach(hp);
	kmem_free(hp, sizeof(struct uhso_port));
	return 0;
}

Static int
uhso_bulk_init(struct uhso_port *hp)
{
	usbd_status status;

	DPRINTF(1, "hp=%p\n", hp);

	if (hp->hp_isize > 0) {
		hp->hp_ibuf = kmem_alloc(hp->hp_isize, KM_SLEEP);

		status = usbd_open_pipe_intr(hp->hp_ifh, hp->hp_iaddr,
		    USBD_SHORT_XFER_OK, &hp->hp_ipipe, hp, hp->hp_ibuf,
		    hp->hp_isize, uhso_bulk_intr, USBD_DEFAULT_INTERVAL);

		if (status != USBD_NORMAL_COMPLETION) {
			DPRINTF(0, "interrupt pipe open failed: %s\n",
			    usbd_errstr(status));

			return EIO;
		}
	}

	status = usbd_open_pipe(hp->hp_ifh, hp->hp_raddr, 0, &hp->hp_rpipe);
	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "read pipe open failed: %s\n", usbd_errstr(status));
		return EIO;
	}

	status = usbd_open_pipe(hp->hp_ifh, hp->hp_waddr, 0, &hp->hp_wpipe);
	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "write pipe open failed: %s\n", usbd_errstr(status));
		return EIO;
	}

	int error = usbd_create_xfer(hp->hp_rpipe, hp->hp_rsize,
	    0, 0, &hp->hp_rxfer);
	if (error)
		return error;

	hp->hp_rbuf = usbd_get_buffer(hp->hp_rxfer);

	error = usbd_create_xfer(hp->hp_wpipe, hp->hp_wsize, 0, 0,
	    &hp->hp_wxfer);
	if (error)
		return error;
	hp->hp_wbuf = usbd_get_buffer(hp->hp_wxfer);

	return 0;
}

Static int
uhso_bulk_clean(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	if (hp->hp_ipipe != NULL) {
		usbd_abort_pipe(hp->hp_ipipe);
		usbd_close_pipe(hp->hp_ipipe);
		hp->hp_ipipe = NULL;
	}

	if (hp->hp_ibuf != NULL) {
		kmem_free(hp->hp_ibuf, hp->hp_isize);
		hp->hp_ibuf = NULL;
	}

	if (hp->hp_rpipe != NULL) {
		usbd_abort_pipe(hp->hp_rpipe);
	}

	if (hp->hp_wpipe != NULL) {
		usbd_abort_pipe(hp->hp_wpipe);
	}

	if (hp->hp_rxfer != NULL) {
		usbd_destroy_xfer(hp->hp_rxfer);
		hp->hp_rxfer = NULL;
		hp->hp_rbuf = NULL;
	}

	if (hp->hp_wxfer != NULL) {
		usbd_destroy_xfer(hp->hp_wxfer);
		hp->hp_wxfer = NULL;
		hp->hp_wbuf = NULL;
	}

	if (hp->hp_rpipe != NULL) {
		usbd_close_pipe(hp->hp_rpipe);
		hp->hp_rpipe = NULL;
	}

	if (hp->hp_wpipe != NULL) {
		usbd_close_pipe(hp->hp_wpipe);
		hp->hp_wpipe = NULL;
	}

	return 0;
}

Static int
uhso_bulk_write(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	usbd_status status;

	DPRINTF(5, "hp=%p, wlen=%zd\n", hp, hp->hp_wlen);

	usbd_setup_xfer(hp->hp_wxfer, hp, hp->hp_wbuf, hp->hp_wlen, 0,
	     USBD_NO_TIMEOUT, hp->hp_write_cb);

	status = usbd_transfer(hp->hp_wxfer);
	if (status != USBD_IN_PROGRESS) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return EIO;
	}

	sc->sc_refcnt++;
	return 0;
}

Static int
uhso_bulk_read(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	usbd_status status;

	DPRINTF(5, "hp=%p\n", hp);

	usbd_setup_xfer(hp->hp_rxfer, hp, hp->hp_rbuf, hp->hp_rsize,
	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, hp->hp_read_cb);

	status = usbd_transfer(hp->hp_rxfer);
	if (status != USBD_IN_PROGRESS) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return EIO;
	}

	sc->sc_refcnt++;
	return 0;
}

Static int
uhso_bulk_control(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	usb_device_request_t req;
	usbd_status status;
	int val;

	DPRINTF(1, "hp=%p\n", hp);

	if (hp->hp_isize == 0)
		return 0;

	val = 0;
	if (ISSET(hp->hp_status, TIOCM_DTR))
		SET(val, UCDC_LINE_DTR);
	if (ISSET(hp->hp_status, TIOCM_RTS))
		SET(val, UCDC_LINE_RTS);

	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
	USETW(req.wValue, val);
	USETW(req.wIndex, hp->hp_index);
	USETW(req.wLength, 0);

	sc->sc_refcnt++;

	status = usbd_do_request(sc->sc_udev, &req, NULL);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return EIO;
	}

	return 0;
}

Static void
uhso_bulk_intr(struct usbd_xfer *xfer, void * p, usbd_status status)
{
	struct uhso_port *hp = p;
	struct tty *tp = hp->hp_tp;
	usb_cdc_notification_t *msg;
	uint32_t cc;
	int s, old;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));
		return;
	}

	usbd_get_xfer_status(xfer, NULL, (void **)&msg, &cc, NULL);

	if (cc < UCDC_NOTIFICATION_LENGTH
	    || msg->bmRequestType != UCDC_NOTIFICATION
	    || msg->bNotification != UCDC_N_SERIAL_STATE
	    || UGETW(msg->wValue) != 0
	    || UGETW(msg->wIndex) != hp->hp_index
	    || UGETW(msg->wLength) < 1)
		return;

	DPRINTF(5, "state=%02x\n", msg->data[0]);

	old = hp->hp_status;
	CLR(hp->hp_status, TIOCM_RNG | TIOCM_DSR | TIOCM_CAR);
	if (ISSET(msg->data[0], UCDC_N_SERIAL_RI))
		SET(hp->hp_status, TIOCM_RNG);
	if (ISSET(msg->data[0], UCDC_N_SERIAL_DSR))
		SET(hp->hp_status, TIOCM_DSR);
	if (ISSET(msg->data[0], UCDC_N_SERIAL_DCD))
		SET(hp->hp_status, TIOCM_CAR);

	if (ISSET(hp->hp_status ^ old, TIOCM_CAR)) {
		s = spltty();
		tp->t_linesw->l_modem(tp, ISSET(hp->hp_status, TIOCM_CAR));
		splx(s);
	}

	if (ISSET((hp->hp_status ^ old), TIOCM_RNG | TIOCM_DSR | TIOCM_CAR))
		DPRINTF(1, "RNG %s, DSR %s, DCD %s\n",
		    (ISSET(hp->hp_status, TIOCM_RNG) ? "on" : "off"),
		    (ISSET(hp->hp_status, TIOCM_DSR) ? "on" : "off"),
		    (ISSET(hp->hp_status, TIOCM_CAR) ? "on" : "off"));
}


/******************************************************************************
 *
 *	TTY management
 *
 */

Static void
uhso_tty_attach(struct uhso_port *hp)
{
	struct tty *tp;

	tp = tty_alloc();
	tp->t_oproc = uhso_tty_start;
	tp->t_param = uhso_tty_param;

	hp->hp_tp = tp;
	tty_attach(tp);

	DPRINTF(1, "hp=%p, tp=%p\n", hp, tp);
}

Static void
uhso_tty_detach(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	uhso_tty_clean(hp);

	tty_detach(hp->hp_tp);
	tty_free(hp->hp_tp);
	hp->hp_tp = NULL;
}

Static void
uhso_tty_write_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
{
	struct uhso_port *hp = p;
	struct uhso_softc *sc = hp->hp_sc;
	struct tty *tp = hp->hp_tp;
	uint32_t cc;
	int s;

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));

		if (status == USBD_STALLED && hp->hp_wpipe != NULL)
			usbd_clear_endpoint_stall_async(hp->hp_wpipe);
		else
			return;
	} else {
		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);

		DPRINTF(5, "wrote %d bytes (of %zd)\n", cc, hp->hp_wlen);
		if (cc != hp->hp_wlen)
			DPRINTF(0, "cc=%u, wlen=%zd\n", cc, hp->hp_wlen);
	}

	s = spltty();
	CLR(tp->t_state, TS_BUSY);
	tp->t_linesw->l_start(tp);
	splx(s);
}

Static void
uhso_tty_read_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
{
	struct uhso_port *hp = p;
	struct uhso_softc *sc = hp->hp_sc;
	struct tty *tp = hp->hp_tp;
	uint8_t *cp;
	uint32_t cc;
	int s;

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status: %s\n", usbd_errstr(status));

		if (status == USBD_STALLED && hp->hp_rpipe != NULL)
			usbd_clear_endpoint_stall_async(hp->hp_rpipe);
		else
			return;

		hp->hp_rlen = 0;
	} else {
		usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);

		hp->hp_rlen = cc;
		DPRINTF(5, "read %d bytes\n", cc);

		s = spltty();
		while (cc > 0) {
			if (tp->t_linesw->l_rint(*cp++, tp) == -1) {
				DPRINTF(0, "lost %d bytes\n", cc);
				break;
			}

			cc--;
		}
		splx(s);
	}

	(*hp->hp_read)(hp);
}


/******************************************************************************
 *
 *	TTY subsystem
 *
 */

int
uhso_tty_open(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct uhso_softc *sc;
	struct uhso_port *hp;
	struct tty *tp;
	int error, s;

	DPRINTF(1, "unit %d port %d\n", UHSOUNIT(dev), UHSOPORT(dev));

	sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	if (sc == NULL
	    || !device_is_active(sc->sc_dev)
	    || UHSOPORT(dev) >= UHSO_PORT_MAX)
		return ENXIO;

	hp = sc->sc_port[UHSOPORT(dev)];
	if (hp == NULL || hp->hp_tp == NULL)
		return ENXIO;

	tp = hp->hp_tp;
	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
		return EBUSY;

	error = 0;
	s = spltty();
	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
		tp->t_dev = dev;
		error = uhso_tty_init(hp);
	}
	splx(s);

	if (error == 0) {
		error = ttyopen(tp, UHSODIALOUT(dev), ISSET(flag, O_NONBLOCK));
		if (error == 0) {
			error = tp->t_linesw->l_open(dev, tp);
		}
	}

	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
		uhso_tty_clean(hp);

	DPRINTF(1, "sc=%p, hp=%p, tp=%p, error=%d\n", sc, hp, tp, error);

	return error;
}

Static int
uhso_tty_init(struct uhso_port *hp)
{
	struct tty *tp = hp->hp_tp;
	struct termios t;
	int error;

	DPRINTF(1, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);

	/*
	 * Initialize the termios status to the defaults.  Add in the
	 * sticky bits from TIOCSFLAGS.
	 */
	t.c_ispeed = 0;
	t.c_ospeed = TTYDEF_SPEED;
	t.c_cflag = TTYDEF_CFLAG;
	if (ISSET(hp->hp_swflags, TIOCFLAG_CLOCAL))
		SET(t.c_cflag, CLOCAL);
	if (ISSET(hp->hp_swflags, TIOCFLAG_CRTSCTS))
		SET(t.c_cflag, CRTSCTS);
	if (ISSET(hp->hp_swflags, TIOCFLAG_MDMBUF))
		SET(t.c_cflag, MDMBUF);

	/* Ensure uhso_tty_param() will do something. */
	tp->t_ospeed = 0;
	(void)uhso_tty_param(tp, &t);

	tp->t_iflag = TTYDEF_IFLAG;
	tp->t_oflag = TTYDEF_OFLAG;
	tp->t_lflag = TTYDEF_LFLAG;
	ttychars(tp);
	ttsetwater(tp);

	hp->hp_status = 0;
	error = (*hp->hp_init)(hp);
	if (error != 0)
		return error;

	/*
	 * Turn on DTR.  We must always do this, even if carrier is not
	 * present, because otherwise we'd have to use TIOCSDTR
	 * immediately after setting CLOCAL, which applications do not
	 * expect.  We always assert DTR while the port is open
	 * unless explicitly requested to deassert it.  Ditto RTS.
	 */
	uhso_tty_control(hp, TIOCMBIS, TIOCM_DTR | TIOCM_RTS);

	/* and start reading */
	error = (*hp->hp_read)(hp);
	if (error != 0)
		return error;

	return 0;
}

int
uhso_tty_close(dev_t dev, int flag, int mode, struct lwp *l)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
	struct tty *tp = hp->hp_tp;

	if (!ISSET(tp->t_state, TS_ISOPEN))
		return 0;

	DPRINTF(1, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);

	sc->sc_refcnt++;

	tp->t_linesw->l_close(tp, flag);
	ttyclose(tp);

	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
		uhso_tty_clean(hp);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	return 0;
}

Static void
uhso_tty_clean(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	if (ISSET(hp->hp_status, TIOCM_DTR)
	    && ISSET(hp->hp_tp->t_cflag, HUPCL))
		uhso_tty_control(hp, TIOCMBIC, TIOCM_DTR);

	(*hp->hp_clean)(hp);

	if (hp->hp_rxfer != NULL) {
		usbd_destroy_xfer(hp->hp_rxfer);
		hp->hp_rxfer = NULL;
		hp->hp_rbuf = NULL;
	}

	if (hp->hp_wxfer != NULL) {
		usbd_destroy_xfer(hp->hp_wxfer);
		hp->hp_wxfer = NULL;
		hp->hp_wbuf = NULL;
	}
}

int
uhso_tty_read(dev_t dev, struct uio *uio, int flag)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
	struct tty *tp = hp->hp_tp;
	int error;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	DPRINTF(5, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);

	sc->sc_refcnt++;

	error = tp->t_linesw->l_read(tp, uio, flag);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	return error;
}

int
uhso_tty_write(dev_t dev, struct uio *uio, int flag)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
	struct tty *tp = hp->hp_tp;
	int error;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	DPRINTF(5, "sc=%p, hp=%p, tp=%p\n", sc, hp, tp);

	sc->sc_refcnt++;

	error = tp->t_linesw->l_write(tp, uio, flag);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	return error;
}

int
uhso_tty_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
	int error;

	if (!device_is_active(sc->sc_dev))
		return EIO;

	DPRINTF(1, "sc=%p, hp=%p\n", sc, hp);

	sc->sc_refcnt++;

	error = uhso_tty_do_ioctl(hp, cmd, data, flag, l);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	return error;
}

Static int
uhso_tty_do_ioctl(struct uhso_port *hp, u_long cmd, void *data, int flag,
    struct lwp *l)
{
	struct tty *tp = hp->hp_tp;
	int error, s;

	error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	error = ttioctl(tp, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	error = 0;

	s = spltty();

	switch (cmd) {
	case TIOCSDTR:
		error = uhso_tty_control(hp, TIOCMBIS, TIOCM_DTR);
		break;

	case TIOCCDTR:
		error = uhso_tty_control(hp, TIOCMBIC, TIOCM_DTR);
		break;

	case TIOCGFLAGS:
		*(int *)data = hp->hp_swflags;
		break;

	case TIOCSFLAGS:
		error = kauth_authorize_device_tty(l->l_cred,
		    KAUTH_DEVICE_TTY_PRIVSET, tp);

		if (error)
			break;

		hp->hp_swflags = *(int *)data;
		break;

	case TIOCMSET:
	case TIOCMBIS:
	case TIOCMBIC:
		error = uhso_tty_control(hp, cmd, *(int *)data);
		break;

	case TIOCMGET:
		*(int *)data = hp->hp_status;
		break;

	default:
		error = EPASSTHROUGH;
		break;
	}

	splx(s);

	return error;
}

/* this is called with tty_lock held */
void
uhso_tty_stop(struct tty *tp, int flag)
{
#if 0
	struct uhso_softc *sc = device_lookup_private(&uhso_cd,
	    UHSOUNIT(tp->t_dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];
#endif
}

struct tty *
uhso_tty_tty(dev_t dev)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];

	return hp->hp_tp;
}

int
uhso_tty_poll(dev_t dev, int events, struct lwp *l)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd, UHSOUNIT(dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(dev)];
	struct tty *tp = hp->hp_tp;
        int revents;

	if (!device_is_active(sc->sc_dev))
                return POLLHUP;

	sc->sc_refcnt++;

        revents = tp->t_linesw->l_poll(tp, events, l);

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

        return revents;
}

Static int
uhso_tty_param(struct tty *tp, struct termios *t)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd,
	    UHSOUNIT(tp->t_dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];

	if (!device_is_active(sc->sc_dev))
		return EIO;

	DPRINTF(1, "hp=%p, tp=%p, termios iflag=%x, oflag=%x, cflag=%x\n",
	    hp, tp, t->c_iflag, t->c_oflag, t->c_cflag);

	/* Check requested parameters. */
	if (t->c_ispeed != 0
	    && t->c_ispeed != t->c_ospeed)
		return EINVAL;

	/* force CLOCAL and !HUPCL for console */
	if (ISSET(hp->hp_swflags, TIOCFLAG_SOFTCAR)) {
		SET(t->c_cflag, CLOCAL);
		CLR(t->c_cflag, HUPCL);
	}

	/* If there were no changes, don't do anything.  */
	if (tp->t_ospeed == t->c_ospeed
	    && tp->t_cflag == t->c_cflag)
		return 0;

	tp->t_ispeed = 0;
	tp->t_ospeed = t->c_ospeed;
	tp->t_cflag = t->c_cflag;

	/* update tty layers idea of carrier bit */
	tp->t_linesw->l_modem(tp, ISSET(hp->hp_status, TIOCM_CAR));
	return 0;
}

/* this is called with tty_lock held */
Static void
uhso_tty_start(struct tty *tp)
{
	struct uhso_softc *sc = device_lookup_private(&uhso_cd,
	    UHSOUNIT(tp->t_dev));
	struct uhso_port *hp = sc->sc_port[UHSOPORT(tp->t_dev)];
	int s;

	if (!device_is_active(sc->sc_dev))
		return;

	s = spltty();

	if (!ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)
	    && ttypull(tp) != 0) {
		hp->hp_wlen = q_to_b(&tp->t_outq, hp->hp_wbuf, hp->hp_wsize);
		if (hp->hp_wlen > 0) {
			SET(tp->t_state, TS_BUSY);
			(*hp->hp_write)(hp);
		}
	}

	splx(s);
}

Static int
uhso_tty_control(struct uhso_port *hp, u_long cmd, int bits)
{

	bits &= (TIOCM_DTR | TIOCM_RTS);
	DPRINTF(1, "cmd %s, DTR=%d, RTS=%d\n",
	    (cmd == TIOCMBIC ? "BIC" : (cmd == TIOCMBIS ? "BIS" : "SET")),
	    (bits & TIOCM_DTR) ? 1 : 0,
	    (bits & TIOCM_RTS) ? 1 : 0);

	switch (cmd) {
	case TIOCMBIC:
		CLR(hp->hp_status, bits);
		break;

	case TIOCMBIS:
		SET(hp->hp_status, bits);
		break;

	case TIOCMSET:
		CLR(hp->hp_status, TIOCM_DTR | TIOCM_RTS);
		SET(hp->hp_status, bits);
		break;
	}

	return (*hp->hp_control)(hp);
}


/******************************************************************************
 *
 *	Network Interface
 *
 */

Static void
uhso_ifnet_attach(struct uhso_softc *sc, struct usbd_interface *ifh, int index)
{
	usb_endpoint_descriptor_t *ed;
	struct uhso_port *hp;
	struct ifnet *ifp;
	int in, out;

	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_IN);
	if (ed == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "could not find bulk-in endpoint\n");

		return;
	}
	in = ed->bEndpointAddress;

	ed = uhso_get_endpoint(ifh, UE_BULK, UE_DIR_OUT);
	if (ed == NULL) {
		aprint_error_dev(sc->sc_dev,
		    "could not find bulk-out endpoint\n");

		return;
	}
	out = ed->bEndpointAddress;

	DPRINTF(1, "in=%d, out=%d\n", in, out);

	if (sc->sc_port[index] != NULL) {
		aprint_error_dev(sc->sc_dev,
		    "ifnet port %d is duplicate!\n", index);

		return;
	}

	hp = kmem_zalloc(sizeof(struct uhso_port), KM_SLEEP);
	sc->sc_port[index] = hp;

	ifp = if_alloc(IFT_IP);
	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
	ifp->if_softc = hp;
	ifp->if_mtu = UHSO_IFNET_MTU;
	ifp->if_dlt = DLT_RAW;
	ifp->if_type = IFT_IP;
	ifp->if_flags = IFF_NOARP | IFF_SIMPLEX;
	ifp->if_ioctl = uhso_ifnet_ioctl;
	ifp->if_start = uhso_ifnet_start;
	ifp->if_output = uhso_ifnet_output;
	IFQ_SET_READY(&ifp->if_snd);

	hp->hp_sc = sc;
	hp->hp_ifp = ifp;
	hp->hp_ifh = ifh;
	hp->hp_raddr = in;
	hp->hp_waddr = out;
	hp->hp_abort = uhso_ifnet_abort;
	hp->hp_detach = uhso_ifnet_detach;
	hp->hp_init = uhso_bulk_init;
	hp->hp_clean = uhso_bulk_clean;
	hp->hp_write = uhso_bulk_write;
	hp->hp_write_cb = uhso_ifnet_write_cb;
	hp->hp_read = uhso_bulk_read;
	hp->hp_read_cb = uhso_ifnet_read_cb;
	hp->hp_wsize = MCLBYTES;
	hp->hp_rsize = MCLBYTES;

	if_attach(ifp);
	if_alloc_sadl(ifp);
	bpf_attach(ifp, DLT_RAW, 0);

	aprint_normal_dev(sc->sc_dev, "%s (port %d) attached as ifnet\n",
	    uhso_port_name[index], index);
}

Static int
uhso_ifnet_abort(struct uhso_port *hp)
{
	struct ifnet *ifp = hp->hp_ifp;

	/* All ifnet IO will abort when IFF_RUNNING is not set */
	CLR(ifp->if_flags, IFF_RUNNING);

	return (*hp->hp_clean)(hp);
}

Static int
uhso_ifnet_detach(struct uhso_port *hp)
{
	struct ifnet *ifp = hp->hp_ifp;
	int s;

	s = splnet();
	bpf_detach(ifp);
	if_detach(ifp);
	if_free(ifp);
	splx(s);

	kmem_free(hp, sizeof(struct uhso_port));
	return 0;
}

Static void
uhso_ifnet_write_cb(struct usbd_xfer *xfer, void * p, usbd_status status)
{
	struct uhso_port *hp = p;
	struct uhso_softc *sc= hp->hp_sc;
	struct ifnet *ifp = hp->hp_ifp;
	uint32_t cc;
	int s;

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	if (!ISSET(ifp->if_flags, IFF_RUNNING))
		return;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status %s\n", usbd_errstr(status));

		if (status == USBD_STALLED && hp->hp_wpipe != NULL)
			usbd_clear_endpoint_stall_async(hp->hp_wpipe);
		else
			return;

		ifp->if_oerrors++;
	} else {
		usbd_get_xfer_status(xfer, NULL, NULL, &cc, NULL);
		DPRINTF(5, "wrote %d bytes (of %zd)\n", cc, hp->hp_wlen);

		if (cc != hp->hp_wlen)
			DPRINTF(0, "cc=%u, wlen=%zd\n", cc, hp->hp_wlen);

		ifp->if_opackets++;
	}

	s = splnet();
	CLR(ifp->if_flags, IFF_OACTIVE);
	ifp->if_start(ifp);
	splx(s);
}

Static void
uhso_ifnet_read_cb(struct usbd_xfer *xfer, void * p,
    usbd_status status)
{
	struct uhso_port *hp = p;
	struct uhso_softc *sc= hp->hp_sc;
	struct ifnet *ifp = hp->hp_ifp;
	void *cp;
	uint32_t cc;

	if (--sc->sc_refcnt < 0)
		usb_detach_wakeupold(sc->sc_dev);

	if (!ISSET(ifp->if_flags, IFF_RUNNING))
		return;

	if (status != USBD_NORMAL_COMPLETION) {
		DPRINTF(0, "non-normal status: %s\n", usbd_errstr(status));

		if (status == USBD_STALLED && hp->hp_rpipe != NULL)
			usbd_clear_endpoint_stall_async(hp->hp_rpipe);
		else
			return;

		ifp->if_ierrors++;
		hp->hp_rlen = 0;
	} else {
		usbd_get_xfer_status(xfer, NULL, (void **)&cp, &cc, NULL);

		hp->hp_rlen = cc;
		DPRINTF(5, "read %d bytes\n", cc);

		uhso_ifnet_input(ifp, &hp->hp_mbuf, cp, cc);
	}

	(*hp->hp_read)(hp);
}

Static void
uhso_ifnet_input(struct ifnet *ifp, struct mbuf **mb, uint8_t *cp, size_t cc)
{
	struct mbuf *m;
	size_t got, len, want;
	int s;

	/*
	 * Several IP packets might be in the same buffer, we need to
	 * separate them before handing it to the ip-stack.  We might
	 * also receive partial packets which we need to defer until
	 * we get more data.
	 */
	while (cc > 0) {
		if (*mb == NULL) {
			MGETHDR(m, M_DONTWAIT, MT_DATA);
			if (m == NULL) {
				aprint_error_ifnet(ifp, "no mbufs\n");
				ifp->if_ierrors++;
				break;
			}

			MCLGET(m, M_DONTWAIT);
			if (!ISSET(m->m_flags, M_EXT)) {
				aprint_error_ifnet(ifp, "no mbuf clusters\n");
				ifp->if_ierrors++;
				m_freem(m);
				break;
			}

			got = 0;
		} else {
			m = *mb;
			*mb = NULL;
			got = m->m_pkthdr.len;
		}

		/* make sure that the incoming packet is ok */
		if (got == 0)
			mtod(m, uint8_t *)[0] = cp[0];

		want = mtod(m, struct ip *)->ip_hl << 2;
		if (mtod(m, struct ip *)->ip_v != 4
		    || want != sizeof(struct ip)) {
			aprint_error_ifnet(ifp,
			    "bad IP header (v=%d, hl=%zd)\n",
			    mtod(m, struct ip *)->ip_v, want);

			ifp->if_ierrors++;
			m_freem(m);
			break;
		}

		/* ensure we have the IP header.. */
		if (got < want) {
			len = MIN(want - got, cc);
			memcpy(mtod(m, uint8_t *) + got, cp, len);
			got += len;
			cc -= len;
			cp += len;

			if (got < want) {
				DPRINTF(5, "waiting for IP header "
					   "(got %zd want %zd)\n", got, want);

				m->m_pkthdr.len = got;
				*mb = m;
				break;
			}
		}

		/* ..and the packet body */
		want = ntohs(mtod(m, struct ip *)->ip_len);
		if (got < want) {
			len = MIN(want - got, cc);
			memcpy(mtod(m, uint8_t *) + got, cp, len);
			got += len;
			cc -= len;
			cp += len;

			if (got < want) {
				DPRINTF(5, "waiting for IP packet "
					   "(got %zd want %zd)\n", got, want);

				m->m_pkthdr.len = got;
				*mb = m;
				break;
			}
		}

		m_set_rcvif(m, ifp);
		m->m_pkthdr.len = m->m_len = got;

		s = splnet();

		bpf_mtap(ifp, m, BPF_D_IN);

		if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
			m_freem(m);
		} else {
			ifp->if_ipackets++;
			ifp->if_ibytes += got;
		}
		splx(s);
	}
}

Static int
uhso_ifnet_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct uhso_port *hp = ifp->if_softc;
	int error, s;

	s = splnet();

	switch (cmd) {
	case SIOCINITIFADDR:
		switch (((struct ifaddr *)data)->ifa_addr->sa_family) {
#ifdef INET
		case AF_INET:
			if (!ISSET(ifp->if_flags, IFF_RUNNING)) {
				SET(ifp->if_flags, IFF_UP);
				error = uhso_ifnet_init(hp);
				if (error != 0) {
					uhso_ifnet_clean(hp);
					break;
				}

				SET(ifp->if_flags, IFF_RUNNING);
				DPRINTF(1, "hp=%p, ifp=%p INITIFADDR\n", hp,
				    ifp);
				break;
			}

			error = 0;
			break;
#endif

		default:
			error = EAFNOSUPPORT;
			break;
		}
		break;

	case SIOCSIFMTU:
		if (((struct ifreq *)data)->ifr_mtu > hp->hp_wsize) {
			error = EINVAL;
			break;
		}

		error = ifioctl_common(ifp, cmd, data);
		if (error == ENETRESET)
			error = 0;

		break;

	case SIOCSIFFLAGS:
		error = ifioctl_common(ifp, cmd, data);
		if (error != 0)
			break;

		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
		case IFF_UP:
			error = uhso_ifnet_init(hp);
			if (error != 0) {
				uhso_ifnet_clean(hp);
				break;
			}

			SET(ifp->if_flags, IFF_RUNNING);
			DPRINTF(1, "hp=%p, ifp=%p RUNNING\n", hp, ifp);
			break;

		case IFF_RUNNING:
			uhso_ifnet_clean(hp);
			CLR(ifp->if_flags, IFF_RUNNING);
			DPRINTF(1, "hp=%p, ifp=%p STOPPED\n", hp, ifp);
			break;

		default:
			break;
		}
		break;

	default:
		error = ifioctl_common(ifp, cmd, data);
		break;
	}

	splx(s);

	return error;
}

/* is only called if IFF_RUNNING not set */
Static int
uhso_ifnet_init(struct uhso_port *hp)
{
	struct uhso_softc *sc = hp->hp_sc;
	int error;

	DPRINTF(1, "sc=%p, hp=%p\n", sc, hp);

	if (!device_is_active(sc->sc_dev))
		return EIO;

	error = (*hp->hp_init)(hp);
	if (error != 0)
		return error;

	error = (*hp->hp_read)(hp);
	if (error != 0)
		return error;

	return 0;
}

Static void
uhso_ifnet_clean(struct uhso_port *hp)
{

	DPRINTF(1, "hp=%p\n", hp);

	(*hp->hp_clean)(hp);
}

/* called at splnet() with IFF_OACTIVE not set */
Static void
uhso_ifnet_start(struct ifnet *ifp)
{
	struct uhso_port *hp = ifp->if_softc;
	struct mbuf *m;

	KASSERT(!ISSET(ifp->if_flags, IFF_OACTIVE));

	if (!ISSET(ifp->if_flags, IFF_RUNNING))
		return;

	if (IFQ_IS_EMPTY(&ifp->if_snd)) {
		DPRINTF(5, "finished sending\n");
		return;
	}

	SET(ifp->if_flags, IFF_OACTIVE);
	IFQ_DEQUEUE(&ifp->if_snd, m);
	hp->hp_wlen = m->m_pkthdr.len;
	if (hp->hp_wlen > hp->hp_wsize) {
		aprint_error_ifnet(ifp,
		    "packet too long (%zd > %zd), truncating\n",
		    hp->hp_wlen, hp->hp_wsize);

		hp->hp_wlen = hp->hp_wsize;
	}

	bpf_mtap(ifp, m, BPF_D_OUT);

	m_copydata(m, 0, hp->hp_wlen, hp->hp_wbuf);
	m_freem(m);

	if ((*hp->hp_write)(hp) != 0) {
		ifp->if_oerrors++;
		CLR(ifp->if_flags, IFF_OACTIVE);
	}
}

Static int
uhso_ifnet_output(struct ifnet *ifp, struct mbuf *m,
    const struct sockaddr *dst, const struct rtentry *rt0)
{
	int error;

	if (!ISSET(ifp->if_flags, IFF_RUNNING))
		return EIO;

	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);

	switch (dst->sa_family) {
#ifdef INET
	case AF_INET:
		error = ifq_enqueue(ifp, m);
		break;
#endif

	default:
		DPRINTF(0, "unsupported address family %d\n", dst->sa_family);
		error = EAFNOSUPPORT;
		m_freem(m);
		break;
	}

	return error;
}