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
/*	$NetBSD: umass.c,v 1.175.2.2 2021/06/21 17:27:57 martin Exp $	*/

/*
 * Copyright (c) 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Charles M. Hannum.
 *
 * 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.
 */

/*-
 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
 *		      Nick Hibma <n_hibma@freebsd.org>
 * 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 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 AUTHOR 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.
 *
 *     $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
 */

/*
 * Universal Serial Bus Mass Storage Class specs:
 * http://www.usb.org/developers/docs/devclass_docs/Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf
 * http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
 * http://www.usb.org/developers/docs/devclass_docs/usb_msc_cbi_1.1.pdf
 * http://www.usb.org/developers/docs/devclass_docs/usbmass-ufi10.pdf
 */

/*
 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
 */

/*
 * The driver handles 3 Wire Protocols
 * - Command/Bulk/Interrupt (CBI)
 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
 * - Mass Storage Bulk-Only (BBB)
 *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
 *
 * Over these wire protocols it handles the following command protocols
 * - SCSI
 * - 8070 (ATA/ATAPI for rewritable removable media)
 * - UFI (USB Floppy Interface)
 *
 * 8070i is a transformed version of the SCSI command set. UFI is a transformed
 * version of the 8070i command set.  The sc->transform method is used to
 * convert the commands into the appropriate format (if at all necessary).
 * For example, ATAPI requires all commands to be 12 bytes in length amongst
 * other things.
 *
 * The source code below is marked and can be split into a number of pieces
 * (in this order):
 *
 * - probe/attach/detach
 * - generic transfer routines
 * - BBB
 * - CBI
 * - CBI_I (in addition to functions from CBI)
 * - CAM (Common Access Method)
 * - SCSI
 * - UFI
 * - 8070i
 *
 * The protocols are implemented using a state machine, for the transfers as
 * well as for the resets. The state machine is contained in umass_*_state.
 * The state machine is started through either umass_*_transfer or
 * umass_*_reset.
 *
 * The reason for doing this is a) CAM performs a lot better this way and b) it
 * avoids sleeping in interrupt context which is prohibited (for example after a
 * failed transfer).
 */

/*
 * The SCSI related part of this driver has been derived from the
 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
 *
 * The CAM layer uses so called actions which are messages sent to the host
 * adapter for completion. The actions come in through umass_cam_action. The
 * appropriate block of routines is called depending on the transport protocol
 * in use. When the transfer has finished, these routines call
 * umass_cam_cb again to complete the CAM command.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.175.2.2 2021/06/21 17:27:57 martin Exp $");

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

#include "atapibus.h"
#include "scsibus.h"
#include "wd.h"

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/sysctl.h>
#include <sys/systm.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/usbhist.h>

#include <dev/usb/umassvar.h>
#include <dev/usb/umass_quirks.h>
#include <dev/usb/umass_scsipi.h>
#include <dev/usb/umass_isdata.h>

#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipiconf.h>

#ifdef USB_DEBUG
#ifdef UMASS_DEBUG
int umassdebug = 0;

SYSCTL_SETUP(sysctl_hw_umass_setup, "sysctl hw.umass setup")
{
	int err;
	const struct sysctlnode *rnode;
	const struct sysctlnode *cnode;

	err = sysctl_createv(clog, 0, NULL, &rnode,
	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "umass",
	    SYSCTL_DESCR("umass global controls"),
	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);

	if (err)
		goto fail;

	/* control debugging printfs */
	err = sysctl_createv(clog, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
	    "debug", SYSCTL_DESCR("Enable debugging output"),
	    NULL, 0, &umassdebug, sizeof(umassdebug), CTL_CREATE, CTL_EOL);
	if (err)
		goto fail;

	return;
fail:
	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
}

const char *states[TSTATE_STATES+1] = {
	/* should be kept in sync with the list at transfer_state */
	"Idle",
	"BBB CBW",
	"BBB Data",
	"BBB Data bulk-in/-out clear stall",
	"BBB CSW, 1st attempt",
	"BBB CSW bulk-in clear stall",
	"BBB CSW, 2nd attempt",
	"BBB Reset",
	"BBB bulk-in clear stall",
	"BBB bulk-out clear stall",
	"CBI Command",
	"CBI Data",
	"CBI Status",
	"CBI Data bulk-in/-out clear stall",
	"CBI Status intr-in clear stall",
	"CBI Reset",
	"CBI bulk-in clear stall",
	"CBI bulk-out clear stall",
	NULL
};
#endif
#endif

/* USB device probe/attach/detach functions */
int umass_match(device_t, cfdata_t, void *);
void umass_attach(device_t, device_t, void *);
int umass_detach(device_t, int);
static void umass_childdet(device_t, device_t);
int umass_activate(device_t, enum devact);

CFATTACH_DECL2_NEW(umass, sizeof(struct umass_softc), umass_match,
    umass_attach, umass_detach, umass_activate, NULL, umass_childdet);

Static void umass_disco(struct umass_softc *sc);

/* generic transfer functions */
Static usbd_status umass_setup_transfer(struct umass_softc *,
				struct usbd_pipe *,
				void *, int, int,
				struct usbd_xfer *);
Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *,
				usb_device_request_t *,
				void *, int, int,
				struct usbd_xfer *);
Static void umass_clear_endpoint_stall(struct umass_softc *, int,
				struct usbd_xfer *);
Static void umass_transfer_done(struct umass_softc *, int, int);
Static void umass_transfer_reset(struct umass_softc *);
#if 0
Static void umass_reset(struct umass_softc *, transfer_cb_f, void *);
#endif

/* Bulk-Only related functions */
Static void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *,
			       int, int, u_int, int, umass_callback, void *);
Static void umass_bbb_reset(struct umass_softc *, int);
Static void umass_bbb_state(struct usbd_xfer *, void *, usbd_status);

usbd_status umass_bbb_get_max_lun(struct umass_softc *, uint8_t *);

/* CBI related functions */
Static void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *,
			       int, int, u_int, int, umass_callback, void *);
Static void umass_cbi_reset(struct umass_softc *, int);
Static void umass_cbi_state(struct usbd_xfer *, void *, usbd_status);

Static int umass_cbi_adsc(struct umass_softc *, char *, int, int,
    struct usbd_xfer *);

const struct umass_wire_methods umass_bbb_methods = {
	.wire_xfer = umass_bbb_transfer,
	.wire_reset = umass_bbb_reset,
	.wire_state = umass_bbb_state
};

const struct umass_wire_methods umass_cbi_methods = {
	.wire_xfer = umass_cbi_transfer,
	.wire_reset = umass_cbi_reset,
	.wire_state = umass_cbi_state
};

#ifdef UMASS_DEBUG
/* General debugging functions */
Static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
Static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
Static void umass_dump_buffer(struct umass_softc *, uint8_t *, int, int);
#endif


/*
 * USB device probe/attach/detach
 */

int
umass_match(device_t parent, cfdata_t match, void *aux)
{
	struct usbif_attach_arg *uiaa = aux;
	const struct umass_quirk *quirk;

	quirk = umass_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
	if (quirk != NULL && quirk->uq_match != UMASS_QUIRK_USE_DEFAULTMATCH)
		return quirk->uq_match;

	if (uiaa->uiaa_class != UICLASS_MASS)
		return UMATCH_NONE;

	switch (uiaa->uiaa_subclass) {
	case UISUBCLASS_RBC:
	case UISUBCLASS_SFF8020I:
	case UISUBCLASS_QIC157:
	case UISUBCLASS_UFI:
	case UISUBCLASS_SFF8070I:
	case UISUBCLASS_SCSI:
		break;
	default:
		return UMATCH_IFACECLASS;
	}

	switch (uiaa->uiaa_proto) {
	case UIPROTO_MASS_CBI_I:
	case UIPROTO_MASS_CBI:
	case UIPROTO_MASS_BBB_OLD:
	case UIPROTO_MASS_BBB:
		break;
	default:
		return UMATCH_IFACECLASS_IFACESUBCLASS;
	}

	return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
}

void
umass_attach(device_t parent, device_t self, void *aux)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	struct umass_softc *sc = device_private(self);
	struct usbif_attach_arg *uiaa = aux;
	const struct umass_quirk *quirk;
	usb_interface_descriptor_t *id;
	usb_endpoint_descriptor_t *ed;
	const char *sWire, *sCommand;
	char *devinfop;
	usbd_status err;
	int i, error;

	sc->sc_dev = self;

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

	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
	cv_init(&sc->sc_detach_cv, "umassdet");

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

	sc->sc_udev = uiaa->uiaa_device;
	sc->sc_iface = uiaa->uiaa_iface;
	sc->sc_ifaceno = uiaa->uiaa_ifaceno;

	quirk = umass_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
	if (quirk != NULL) {
		sc->sc_wire = quirk->uq_wire;
		sc->sc_cmd = quirk->uq_cmd;
		sc->sc_quirks = quirk->uq_flags;
		sc->sc_busquirks = quirk->uq_busquirks;

		if (quirk->uq_fixup != NULL)
			(*quirk->uq_fixup)(sc);
	} else {
		sc->sc_wire = UMASS_WPROTO_UNSPEC;
		sc->sc_cmd = UMASS_CPROTO_UNSPEC;
		sc->sc_quirks = 0;
		sc->sc_busquirks = 0;
	}

	if (sc->sc_wire == UMASS_WPROTO_UNSPEC) {
		switch (uiaa->uiaa_proto) {
		case UIPROTO_MASS_CBI:
			sc->sc_wire = UMASS_WPROTO_CBI;
			break;
		case UIPROTO_MASS_CBI_I:
			sc->sc_wire = UMASS_WPROTO_CBI_I;
			break;
		case UIPROTO_MASS_BBB:
		case UIPROTO_MASS_BBB_OLD:
			sc->sc_wire = UMASS_WPROTO_BBB;
			break;
		default:
			DPRINTFM(UDMASS_GEN, "Unsupported wire protocol %ju",
			    uiaa->uiaa_proto, 0, 0, 0);
			return;
		}
	}

	if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) {
		switch (uiaa->uiaa_subclass) {
		case UISUBCLASS_SCSI:
			sc->sc_cmd = UMASS_CPROTO_SCSI;
			break;
		case UISUBCLASS_UFI:
			sc->sc_cmd = UMASS_CPROTO_UFI;
			break;
		case UISUBCLASS_SFF8020I:
		case UISUBCLASS_SFF8070I:
		case UISUBCLASS_QIC157:
			sc->sc_cmd = UMASS_CPROTO_ATAPI;
			break;
		case UISUBCLASS_RBC:
			sc->sc_cmd = UMASS_CPROTO_RBC;
			break;
		default:
			DPRINTFM(UDMASS_GEN, "Unsupported command protocol %ju",
			    uiaa->uiaa_subclass, 0, 0, 0);
			return;
		}
	}

	switch (sc->sc_wire) {
	case UMASS_WPROTO_CBI:
		sWire = "CBI";
		break;
	case UMASS_WPROTO_CBI_I:
		sWire = "CBI with CCI";
		break;
	case UMASS_WPROTO_BBB:
		sWire = "Bulk-Only";
		break;
	default:
		sWire = "unknown";
		break;
	}

	switch (sc->sc_cmd) {
	case UMASS_CPROTO_RBC:
		sCommand = "RBC";
		break;
	case UMASS_CPROTO_SCSI:
		sCommand = "SCSI";
		break;
	case UMASS_CPROTO_UFI:
		sCommand = "UFI";
		break;
	case UMASS_CPROTO_ATAPI:
		sCommand = "ATAPI";
		break;
	case UMASS_CPROTO_ISD_ATA:
		sCommand = "ISD-ATA";
		break;
	default:
		sCommand = "unknown";
		break;
	}

	aprint_verbose_dev(self, "using %s over %s\n", sCommand, sWire);

	if (quirk != NULL && quirk->uq_init != NULL) {
		err = (*quirk->uq_init)(sc);
		if (err) {
			aprint_error_dev(self, "quirk init failed\n");
			umass_disco(sc);
			return;
		}
	}

	/*
	 * In addition to the Control endpoint the following endpoints
	 * are required:
	 * a) bulk-in endpoint.
	 * b) bulk-out endpoint.
	 * and for Control/Bulk/Interrupt with CCI (CBI_I)
	 * c) intr-in
	 *
	 * The endpoint addresses are not fixed, so we have to read them
	 * from the device descriptors of the current interface.
	 */
	id = usbd_get_interface_descriptor(sc->sc_iface);
	for (i = 0 ; i < id->bNumEndpoints ; i++) {
		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
		if (ed == NULL) {
			aprint_error_dev(self,
			    "could not read endpoint descriptor\n");
			return;
		}
		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
			sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
			sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
		} else if (sc->sc_wire == UMASS_WPROTO_CBI_I
		    && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
		    && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
			sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
#ifdef UMASS_DEBUG
			if (UGETW(ed->wMaxPacketSize) > 2) {
				DPRINTFM(UDMASS_CBI, "sc %#jx intr size is %jd",
				    (uintptr_t)sc, UGETW(ed->wMaxPacketSize),
				    0, 0);
			}
#endif
		}
	}

	/* check whether we found all the endpoints we need */
	if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
	    (sc->sc_wire == UMASS_WPROTO_CBI_I &&
	     !sc->sc_epaddr[UMASS_INTRIN])) {
		aprint_error_dev(self, "endpoint not found %u/%u/%u\n",
		       sc->sc_epaddr[UMASS_BULKIN],
		       sc->sc_epaddr[UMASS_BULKOUT],
		       sc->sc_epaddr[UMASS_INTRIN]);
		return;
	}

	/*
	 * Get the maximum LUN supported by the device.
	 */
	if (sc->sc_wire == UMASS_WPROTO_BBB &&
	    (sc->sc_quirks & UMASS_QUIRK_NOGETMAXLUN) == 0) {
		err = umass_bbb_get_max_lun(sc, &sc->maxlun);
		if (err) {
			aprint_error_dev(self, "unable to get Max Lun: %s\n",
			    usbd_errstr(err));
			return;
		}
		if (sc->maxlun > 0)
			sc->sc_busquirks |= PQUIRK_FORCELUNS;
	} else {
		sc->maxlun = 0;
	}

	/* Open the bulk-in and -out pipe */
	DPRINTFM(UDMASS_USB, "sc %#jx: opening iface %#jx epaddr %jd for "
	    "BULKOUT", (uintptr_t)sc, (uintptr_t)sc->sc_iface,
	    sc->sc_epaddr[UMASS_BULKOUT], 0);
	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_BULKOUT]);
	if (err) {
		aprint_error_dev(self, "cannot open %u-out pipe (bulk)\n",
		    sc->sc_epaddr[UMASS_BULKOUT]);
		umass_disco(sc);
		return;
	}
	DPRINTFM(UDMASS_USB, "sc %#jx: opening iface %#jx epaddr %jd for "
	    "BULKIN", (uintptr_t)sc, (uintptr_t)sc->sc_iface,
	    sc->sc_epaddr[UMASS_BULKIN], 0);
	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_BULKIN]);
	if (err) {
		aprint_error_dev(self, "could not open %u-in pipe (bulk)\n",
		    sc->sc_epaddr[UMASS_BULKIN]);
		umass_disco(sc);
		return;
	}
	/*
	 * Open the intr-in pipe if the protocol is CBI with CCI.
	 * Note: early versions of the Zip drive do have an interrupt pipe, but
	 * this pipe is unused
	 *
	 * We do not open the interrupt pipe as an interrupt pipe, but as a
	 * normal bulk endpoint. We send an IN transfer down the wire at the
	 * appropriate time, because we know exactly when to expect data on
	 * that endpoint. This saves bandwidth, but more important, makes the
	 * code for handling the data on that endpoint simpler. No data
	 * arriving concurrently.
	 */
	if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
		DPRINTFM(UDMASS_USB,
		    "sc %#jx: opening iface %#jx epaddr %jd for INTRIN",
		    (uintptr_t)sc, (uintptr_t)sc->sc_iface,
		    sc->sc_epaddr[UMASS_INTRIN], 0);
		err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
		    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_INTRIN]);
		if (err) {
			aprint_error_dev(self, "couldn't open %u-in (intr)\n",
			    sc->sc_epaddr[UMASS_INTRIN]);
			umass_disco(sc);
			return;
		}
	}

	/* initialisation of generic part */
	sc->transfer_state = TSTATE_IDLE;

	for (i = 0; i < XFER_NR; i++) {
		sc->transfer_xfer[i] = NULL;
	}

	/*
	 * Create the transfers
	 */
	struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->sc_udev);
	switch (sc->sc_wire) {
	case UMASS_WPROTO_BBB:
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_DATAIN]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_DATAOUT]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
		    UMASS_BBB_CBW_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_CBW]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
		    UMASS_BBB_CSW_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_CSW1]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
		    UMASS_BBB_CSW_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_CSW2]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_SCLEAR]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_DCLEAR]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_RESET1]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_RESET2]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_BBB_RESET3]);
		if (err)
			goto fail_create;
		break;
	case UMASS_WPROTO_CBI:
	case UMASS_WPROTO_CBI_I:
		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
		    &sc->transfer_xfer[XFER_CBI_CB]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_CBI_DATAIN]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
		    &sc->transfer_xfer[XFER_CBI_DATAOUT]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(sc->sc_pipe[UMASS_INTRIN],
		    sizeof(sc->sbl), 0, 0,
		    &sc->transfer_xfer[XFER_CBI_STATUS]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_CBI_DCLEAR]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, 0, 0, 0,
		    &sc->transfer_xfer[XFER_CBI_SCLEAR]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
		    &sc->transfer_xfer[XFER_CBI_RESET1]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
		    &sc->transfer_xfer[XFER_CBI_RESET2]);
		if (err)
			goto fail_create;
		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
		    &sc->transfer_xfer[XFER_CBI_RESET3]);
		if (err)
			goto fail_create;
		break;
	default:
	fail_create:
		aprint_error_dev(self, "failed to create xfers\n");
		umass_disco(sc);
		return;
	}

	/*
	 * Record buffer pointers for data transfer (it's huge), command and
	 * status data here
	 */
	switch (sc->sc_wire) {
	case UMASS_WPROTO_BBB:
		sc->datain_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_DATAIN]);
		sc->dataout_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_DATAOUT]);
		sc->cmd_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CBW]);
		sc->s1_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CSW1]);
		sc->s2_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CSW2]);
		break;
	case UMASS_WPROTO_CBI:
	case UMASS_WPROTO_CBI_I:
		sc->datain_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_DATAIN]);
		sc->dataout_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_DATAOUT]);
		sc->cmd_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_CB]);
		sc->s1_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_STATUS]);
		sc->s2_buffer =
		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_RESET1]);
		break;
	default:
		break;
	}

	/* Initialise the wire protocol specific methods */
	switch (sc->sc_wire) {
	case UMASS_WPROTO_BBB:
		sc->sc_methods = &umass_bbb_methods;
		break;
	case UMASS_WPROTO_CBI:
	case UMASS_WPROTO_CBI_I:
		sc->sc_methods = &umass_cbi_methods;
		break;
	default:
		umass_disco(sc);
		return;
	}

	error = 0;
	switch (sc->sc_cmd) {
	case UMASS_CPROTO_RBC:
	case UMASS_CPROTO_SCSI:
#if NSCSIBUS > 0
		error = umass_scsi_attach(sc);
#else
		aprint_error_dev(self, "scsibus not configured\n");
#endif
		break;

	case UMASS_CPROTO_UFI:
	case UMASS_CPROTO_ATAPI:
#if NATAPIBUS > 0
		error = umass_atapi_attach(sc);
#else
		aprint_error_dev(self, "atapibus not configured\n");
#endif
		break;

	case UMASS_CPROTO_ISD_ATA:
#if NWD > 0 && NATABUS > 0
		error = umass_isdata_attach(sc);
#else
		aprint_error_dev(self, "isdata not configured\n");
#endif
		break;

	default:
		aprint_error_dev(self, "command protocol=0x%x not supported\n",
		    sc->sc_cmd);
		umass_disco(sc);
		return;
	}
	if (error) {
		aprint_error_dev(self, "bus attach failed\n");
		umass_disco(sc);
		return;
	}

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

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

	DPRINTFM(UDMASS_GEN, "sc %#jx: Attach finished", (uintptr_t)sc,
	    0, 0, 0);

	return;
}

static void
umass_childdet(device_t self, device_t child)
{
	struct umass_softc *sc = device_private(self);

	KASSERTMSG(child == sc->bus->sc_child,
		   "assertion child == sc->bus->sc_child failed\n");
	sc->bus->sc_child = NULL;
}

int
umass_detach(device_t self, int flags)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	struct umass_softc *sc = device_private(self);
	struct umassbus_softc *scbus;
	int rv = 0, i;

	DPRINTFM(UDMASS_USB, "sc %#jx detached", (uintptr_t)sc, 0, 0, 0);

	mutex_enter(&sc->sc_lock);
	sc->sc_dying = true;
	mutex_exit(&sc->sc_lock);

	pmf_device_deregister(self);

	/* Abort the pipes to wake up any waiting processes. */
	for (i = 0 ; i < UMASS_NEP ; i++) {
		if (sc->sc_pipe[i] != NULL)
			usbd_abort_pipe(sc->sc_pipe[i]);
	}
	usbd_abort_default_pipe(sc->sc_udev);

	/* Do we really need reference counting?  Perhaps in ioctl() */
	mutex_enter(&sc->sc_lock);
	if (--sc->sc_refcnt >= 0) {
#ifdef DIAGNOSTIC
		aprint_normal_dev(self, "waiting for refcnt\n");
#endif
		/* Wait for processes to go away. */
		if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60))
			aprint_error_dev(self, ": didn't detach\n");
	}
	mutex_exit(&sc->sc_lock);

	scbus = sc->bus;
	if (scbus != NULL) {
		if (scbus->sc_child != NULL)
			rv = config_detach(scbus->sc_child, flags);

		switch (sc->sc_cmd) {
		case UMASS_CPROTO_RBC:
		case UMASS_CPROTO_SCSI:
#if NSCSIBUS > 0
			umass_scsi_detach(sc);
#else
			aprint_error_dev(self, "scsibus not configured\n");
#endif
			break;

		case UMASS_CPROTO_UFI:
		case UMASS_CPROTO_ATAPI:
#if NATAPIBUS > 0
			umass_atapi_detach(sc);
#else
			aprint_error_dev(self, "atapibus not configured\n");
#endif
			break;

		case UMASS_CPROTO_ISD_ATA:
#if NWD > 0
			umass_isdata_detach(sc);
#else
			aprint_error_dev(self, "isdata not configured\n");
#endif
			break;

		default:
			/* nothing to do */
			break;
		}

		/* protocol detach is expected to free sc->bus */
		KASSERT(sc->bus == NULL);
	}

	if (rv != 0)
		return rv;

	umass_disco(sc);

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

	mutex_destroy(&sc->sc_lock);
	cv_destroy(&sc->sc_detach_cv);

	return rv;
}

int
umass_activate(device_t dev, enum devact act)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	struct umass_softc *sc = device_private(dev);

	DPRINTFM(UDMASS_USB, "sc %#jx act %jd", (uintptr_t)sc, act, 0, 0);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

Static void
umass_disco(struct umass_softc *sc)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	int i;

	/* Remove all the pipes. */
	for (i = 0 ; i < UMASS_NEP ; i++) {
		if (sc->sc_pipe[i] != NULL) {
			usbd_abort_pipe(sc->sc_pipe[i]);
		}
	}

	/* Some xfers may be queued in the default pipe */
	usbd_abort_default_pipe(sc->sc_udev);

	/* Free the xfers. */
	for (i = 0; i < XFER_NR; i++) {
		if (sc->transfer_xfer[i] != NULL) {
			usbd_destroy_xfer(sc->transfer_xfer[i]);
			sc->transfer_xfer[i] = NULL;
		}
	}

	for (i = 0 ; i < UMASS_NEP ; i++) {
		if (sc->sc_pipe[i] != NULL) {
			usbd_close_pipe(sc->sc_pipe[i]);
			sc->sc_pipe[i] = NULL;
		}
	}

}

/*
 * Generic functions to handle transfers
 */

Static usbd_status
umass_setup_transfer(struct umass_softc *sc, struct usbd_pipe *pipe,
			void *buffer, int buflen, int flags,
			struct usbd_xfer *xfer)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	usbd_status err;

	if (sc->sc_dying)
		return USBD_IOERROR;

	/* Initialiase a USB transfer and then schedule it */

	usbd_setup_xfer(xfer, sc, buffer, buflen, flags, sc->timeout,
	    sc->sc_methods->wire_state);

	err = usbd_transfer(xfer);
	DPRINTFM(UDMASS_XFER, "start xfer buffer=%#jx buflen=%jd flags=0x%jx "
	    "timeout=%d", (uintptr_t)buffer, buflen, flags, sc->timeout);
	if (err && err != USBD_IN_PROGRESS) {
		DPRINTFM(UDMASS_BBB, "failed to setup transfer... err=%jd",
		    err, 0, 0, 0);
		return err;
	}

	return USBD_NORMAL_COMPLETION;
}


Static usbd_status
umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req,
	 void *buffer, int buflen, int flags, struct usbd_xfer *xfer)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	usbd_status err;

	if (sc->sc_dying)
		return USBD_IOERROR;

	/* Initialiase a USB control transfer and then schedule it */

	usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc, sc->timeout,
		req, buffer, buflen, flags, sc->sc_methods->wire_state);

	err = usbd_transfer(xfer);
	if (err && err != USBD_IN_PROGRESS) {
		DPRINTFM(UDMASS_BBB, "failed to setup ctrl transfer... err=%jd",
		    err, 0, 0, 0);

		/* do not reset, as this would make us loop */
		return err;
	}

	return USBD_NORMAL_COMPLETION;
}

Static void
umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
	struct usbd_xfer *xfer)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();

	if (sc->sc_dying) {
		umass_transfer_done(sc, sc->transfer_datalen,
		    STATUS_WIRE_FAILED);
		return;
	}

	DPRINTFM(UDMASS_BBB, "Clear endpoint 0x%02jx stall",
	    sc->sc_epaddr[endpt], 0, 0, 0);

	usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]);

	sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT;
	sc->sc_req.bRequest = UR_CLEAR_FEATURE;
	USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT);
	USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]);
	USETW(sc->sc_req.wLength, 0);
	if (umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer))
		umass_transfer_done(sc, sc->transfer_datalen,
		    STATUS_WIRE_FAILED);
}

Static void
umass_transfer_done(struct umass_softc *sc, int residue, int status)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();

	sc->transfer_state = TSTATE_IDLE;
	sc->transfer_cb(sc, sc->transfer_priv, residue, status);
}

Static void
umass_transfer_reset(struct umass_softc *sc)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();

	sc->transfer_state = TSTATE_IDLE;
	if (sc->transfer_priv)
		sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
		    sc->transfer_status);
}

#if 0
Static void
umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
{
	sc->transfer_cb = cb;
	sc->transfer_priv = priv;

	/* The reset is a forced reset, so no error (yet) */
	sc->reset(sc, STATUS_CMD_OK);
}
#endif

/*
 * Bulk protocol specific functions
 */

Static void
umass_bbb_reset(struct umass_softc *sc, int status)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
		   "sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
		   sc->sc_wire);

	if (sc->sc_dying) {
		umass_transfer_done(sc, sc->transfer_datalen, status);
		return;
	}

	/*
	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
	 *
	 * For Reset Recovery the host shall issue in the following order:
	 * a) a Bulk-Only Mass Storage Reset
	 * b) a Clear Feature HALT to the Bulk-In endpoint
	 * c) a Clear Feature HALT to the Bulk-Out endpoint
	 *
	 * This is done in 3 steps, states:
	 * TSTATE_BBB_RESET1
	 * TSTATE_BBB_RESET2
	 * TSTATE_BBB_RESET3
	 *
	 * If the reset doesn't succeed, the device should be port reset.
	 */

	DPRINTFM(UDMASS_BBB, "Bulk Reset", 0, 0, 0, 0);

	sc->transfer_state = TSTATE_BBB_RESET1;
	sc->transfer_status = status;

	/* reset is a class specific interface write */
	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	sc->sc_req.bRequest = UR_BBB_RESET;
	USETW(sc->sc_req.wValue, 0);
	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
	USETW(sc->sc_req.wLength, 0);
	if (umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0,
		sc->transfer_xfer[XFER_BBB_RESET1]))
		umass_transfer_done(sc, sc->transfer_datalen, status);
}

Static void
umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
		   void *data, int datalen, int dir, u_int timeout,
		   int flags, umass_callback cb, void *priv)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	static int dCBWtag = 42;	/* unique for CBW of transfer */

	KASSERT(cb);
	DPRINTFM(UDMASS_BBB, "sc %#jx cmd=0x%02jx", (uintptr_t)sc,
	    *(u_char *)cmd, 0, 0);

	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
		   "sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
		   sc->sc_wire);

	if (sc->sc_dying) {
		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
		return;
	}

	/* Be a little generous. */
	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;

	/*
	 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
	 * a data phase of datalen bytes from/to the device and finally a
	 * csw read phase.
	 * If the data direction was inbound a maximum of datalen bytes
	 * is stored in the buffer pointed to by data.
	 *
	 * umass_bbb_transfer initialises the transfer and lets the state
	 * machine in umass_bbb_state handle the completion. It uses the
	 * following states:
	 * TSTATE_BBB_COMMAND
	 *   -> TSTATE_BBB_DATA
	 *   -> TSTATE_BBB_STATUS
	 *   -> TSTATE_BBB_STATUS2
	 *   -> TSTATE_BBB_IDLE
	 *
	 * An error in any of those states will invoke
	 * umass_bbb_reset.
	 */

	/* check the given arguments */
	KASSERTMSG(datalen == 0 || data != NULL,
		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
	KASSERTMSG(cmdlen <= CBWCDBLENGTH,
		   "%s: cmdlen exceeds CDB length in CBW (%d > %d)",
			device_xname(sc->sc_dev), cmdlen, CBWCDBLENGTH);
	KASSERTMSG(dir == DIR_NONE || datalen > 0,
		   "%s: datalen == 0 while direction is not NONE\n",
			device_xname(sc->sc_dev));
	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
		   "%s: direction is NONE while datalen is not zero\n",
			device_xname(sc->sc_dev));
	/* CTASSERT */
	KASSERTMSG(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
		   "%s: CBW struct does not have the right size (%zu vs. %u)\n",
			device_xname(sc->sc_dev),
			sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE);
	/* CTASSERT */
	KASSERTMSG(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
		   "%s: CSW struct does not have the right size (%zu vs. %u)\n",
			device_xname(sc->sc_dev),
			sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE);

	/*
	 * Determine the direction of the data transfer and the length.
	 *
	 * dCBWDataTransferLength (datalen) :
	 *   This field indicates the number of bytes of data that the host
	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
	 *   the Direction bit) during the execution of this command. If this
	 *   field is set to 0, the device will expect that no data will be
	 *   transferred IN or OUT during this command, regardless of the value
	 *   of the Direction bit defined in dCBWFlags.
	 *
	 * dCBWFlags (dir) :
	 *   The bits of the Flags field are defined as follows:
	 *     Bits 0-6	 reserved
	 *     Bit  7	 Direction - this bit shall be ignored if the
	 *			     dCBWDataTransferLength field is zero.
	 *		 0 = data Out from host to device
	 *		 1 = data In from device to host
	 */

	/* Fill in the Command Block Wrapper */
	USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
	USETDW(sc->cbw.dCBWTag, dCBWtag);
	dCBWtag++;	/* cannot be done in macro (it will be done 4 times) */
	USETDW(sc->cbw.dCBWDataTransferLength, datalen);
	/* DIR_NONE is treated as DIR_OUT (0x00) */
	sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
	sc->cbw.bCBWLUN = lun;
	sc->cbw.bCDBLength = cmdlen;
	memcpy(sc->cbw.CBWCDB, cmd, cmdlen);

	DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));

	/* store the details for the data transfer phase */
	sc->transfer_dir = dir;
	sc->transfer_data = data;
	sc->transfer_datalen = datalen;
	sc->transfer_actlen = 0;
	sc->transfer_cb = cb;
	sc->transfer_priv = priv;
	sc->transfer_status = STATUS_CMD_OK;

	/* move from idle to the command state */
	sc->transfer_state = TSTATE_BBB_COMMAND;

	/* Send the CBW from host to device via bulk-out endpoint. */
	if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
			&sc->cbw, UMASS_BBB_CBW_SIZE, flags,
			sc->transfer_xfer[XFER_BBB_CBW])) {
		umass_bbb_reset(sc, STATUS_WIRE_FAILED);
	}
}


Static void
umass_bbb_state(struct usbd_xfer *xfer, void *priv,
		usbd_status err)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	struct umass_softc *sc = (struct umass_softc *) priv;
	struct usbd_xfer *next_xfer;
	int residue;

	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
		   "sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
		   sc->sc_wire);

	/*
	 * State handling for BBB transfers.
	 *
	 * The subroutine is rather long. It steps through the states given in
	 * Annex A of the Bulk-Only specification.
	 * Each state first does the error handling of the previous transfer
	 * and then prepares the next transfer.
	 * Each transfer is done asynchroneously so after the request/transfer
	 * has been submitted you will find a 'return;'.
	 */

	DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx, transfer_state %jd dir %jd",
	    (uintptr_t)sc, (uintptr_t)xfer, sc->transfer_state,
	    sc->transfer_dir);

	if (err == USBD_CANCELLED) {
		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
		    (uintptr_t)sc, (uintptr_t)xfer, 0, 0);

		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
		return;
	}

	if (sc->sc_dying) {
		umass_transfer_done(sc, sc->transfer_datalen,
		    STATUS_WIRE_FAILED);
		return;
	}

	switch (sc->transfer_state) {

	/***** Bulk Transfer *****/
	case TSTATE_BBB_COMMAND:
		/* Command transport phase, error handling */
		if (err) {
			DPRINTFM(UDMASS_BBB, "sc %#jx failed to send CBW",
			    (uintptr_t)sc, 0, 0, 0);
			/* If the device detects that the CBW is invalid, then
			 * the device may STALL both bulk endpoints and require
			 * a Bulk-Reset
			 */
			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
		}

		/* Data transport phase, setup transfer */
		sc->transfer_state = TSTATE_BBB_DATA;
		if (sc->transfer_dir == DIR_IN) {
			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
					sc->datain_buffer, sc->transfer_datalen,
					USBD_SHORT_XFER_OK,
					sc->transfer_xfer[XFER_BBB_DATAIN]))
				umass_bbb_reset(sc, STATUS_WIRE_FAILED);

			return;
		} else if (sc->transfer_dir == DIR_OUT) {
			memcpy(sc->dataout_buffer, sc->transfer_data,
			       sc->transfer_datalen);
			if (umass_setup_transfer(sc,
			    sc->sc_pipe[UMASS_BULKOUT], sc->dataout_buffer,
			    sc->transfer_datalen, 0,/* fixed length transfer */
			    sc->transfer_xfer[XFER_BBB_DATAOUT]))
				umass_bbb_reset(sc, STATUS_WIRE_FAILED);

			return;
		} else {
			DPRINTFM(UDMASS_BBB, "sc %#jx: no data phase",
			    (uintptr_t)sc, 0, 0, 0);
		}

		/* if no data phase, err == 0 */
		/* FALLTHROUGH */
	case TSTATE_BBB_DATA:
		/* Command transport phase error handling (ignored if no data
		 * phase (fallthrough from previous state)) */
		if (sc->transfer_dir != DIR_NONE) {
			/* retrieve the length of the transfer that was done */
			usbd_get_xfer_status(xfer, NULL, NULL,
			     &sc->transfer_actlen, NULL);
			DPRINTFM(UDMASS_BBB, "sc %#jx: BBB_DATA actlen=%jd",
			    (uintptr_t)sc, sc->transfer_actlen, 0, 0);

			if (err) {
				DPRINTFM(UDMASS_BBB, "sc %#jx Data dir %jd "
				    "err %jd failed, err %jd",
				    (uintptr_t)sc, sc->transfer_dir,
				    sc->transfer_datalen, err);

				if (err == USBD_STALLED) {
					sc->transfer_state = TSTATE_BBB_DCLEAR;
					umass_clear_endpoint_stall(sc,
					  (sc->transfer_dir == DIR_IN?
					    UMASS_BULKIN:UMASS_BULKOUT),
					  sc->transfer_xfer[XFER_BBB_DCLEAR]);
				} else {
					/* Unless the error is a pipe stall the
					 * error is fatal.
					 */
					umass_bbb_reset(sc,STATUS_WIRE_FAILED);
				}
				return;
			}
		}

		/* err == 0 (no data phase or successful) */
		/* FALLTHROUGH */
	case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
		if (sc->transfer_dir == DIR_IN)
			memcpy(sc->transfer_data, sc->datain_buffer,
			       sc->transfer_actlen);

		DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
					umass_dump_buffer(sc, sc->transfer_data,
						sc->transfer_datalen, 48));

		/* err == 0 (no data phase or successful) */
		/* FALLTHROUGH */
	case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
		/* Reading of CSW after bulk stall condition in data phase
		 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
		 * reading CSW (TSTATE_BBB_SCLEAR).
		 * In the case of no data phase or successful data phase,
		 * err == 0 and the following if block is passed.
		 */
		if (err) {	/* should not occur */
			printf("%s: BBB bulk-%s stall clear failed, %s\n",
			    device_xname(sc->sc_dev),
			    (sc->transfer_dir == DIR_IN? "in":"out"),
			    usbd_errstr(err));
			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
		}

		/* Status transport phase, setup transfer */
		if (sc->transfer_state == TSTATE_BBB_COMMAND ||
		    sc->transfer_state == TSTATE_BBB_DATA ||
		    sc->transfer_state == TSTATE_BBB_DCLEAR) {
			/* After no data phase, successful data phase and
			 * after clearing bulk-in/-out stall condition
			 */
			sc->transfer_state = TSTATE_BBB_STATUS1;
			next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
		} else {
			/* After first attempt of fetching CSW */
			sc->transfer_state = TSTATE_BBB_STATUS2;
			next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
		}

		/* Read the Command Status Wrapper via bulk-in endpoint. */
		if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
			&sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
		}

		return;
	case TSTATE_BBB_STATUS1:	/* first attempt */
	case TSTATE_BBB_STATUS2:	/* second attempt */
		/* Status transfer, error handling */
		if (err) {
			DPRINTFM(UDMASS_BBB, "sc %#jx Failed to read CSW "
			    "err %jd (state %jd)", (uintptr_t)sc, err,
			    sc->transfer_state, 0);

			/* If this was the first attempt at fetching the CSW
			 * retry it, otherwise fail.
			 */
			if (sc->transfer_state == TSTATE_BBB_STATUS1) {
				sc->transfer_state = TSTATE_BBB_SCLEAR;
				umass_clear_endpoint_stall(sc, UMASS_BULKIN,
				    sc->transfer_xfer[XFER_BBB_SCLEAR]);
				return;
			} else {
				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
				return;
			}
		}

		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));

#ifdef UMASS_DEBUG
		residue = UGETDW(sc->csw.dCSWDataResidue);
		if (residue != sc->transfer_datalen - sc->transfer_actlen)
			printf("%s: dCSWDataResidue=%d req=%d act=%d\n",
			       device_xname(sc->sc_dev), residue,
			       sc->transfer_datalen, sc->transfer_actlen);
#endif
		residue = sc->transfer_datalen - sc->transfer_actlen;

		/* Translate weird command-status signatures. */
		if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
		    UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
			USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);

		/* Translate invalid command-status tags */
		if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
			USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));

		/* Check CSW and handle any error */
		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
			/* Invalid CSW: Wrong signature or wrong tag might
			 * indicate that the device is confused -> reset it.
			 */
			printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
				device_xname(sc->sc_dev),
				UGETDW(sc->csw.dCSWSignature),
				CSWSIGNATURE);

			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
		} else if (UGETDW(sc->csw.dCSWTag)
				!= UGETDW(sc->cbw.dCBWTag)) {
			printf("%s: Invalid CSW: tag %d should be %d\n",
				device_xname(sc->sc_dev),
				UGETDW(sc->csw.dCSWTag),
				UGETDW(sc->cbw.dCBWTag));

			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;

		/* CSW is valid here */
		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
			printf("%s: Invalid CSW: status %d > %d\n",
				device_xname(sc->sc_dev),
				sc->csw.bCSWStatus,
				CSWSTATUS_PHASE);

			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
			printf("%s: Phase Error, residue = %d\n",
				device_xname(sc->sc_dev), residue);

			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;

		} else if (sc->transfer_actlen > sc->transfer_datalen) {
			/* Buffer overrun! Don't let this go by unnoticed */
			panic("%s: transferred %s %d bytes instead of %d bytes",
			    device_xname(sc->sc_dev),
			    sc->transfer_dir == DIR_IN ? "IN" : "OUT",
			    sc->transfer_actlen, sc->transfer_datalen);
#if 0
		} else if (sc->transfer_datalen - sc->transfer_actlen
			   != residue) {
			DPRINTFM(UDMASS_BBB, "sc %#jx: actlen=%jd != "
			    "residue=%jd\n", (uintptr_t)sc,
			    sc->transfer_datalen - sc->transfer_actlen,
			    residue, 0);

			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
			return;
#endif
		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
			DPRINTFM(UDMASS_BBB, "sc %#jx: Command Failed, "
			    "res = %jd", (uintptr_t)sc, residue, 0, 0);

			/* SCSI command failed but transfer was succesful */
			umass_transfer_done(sc, residue, STATUS_CMD_FAILED);
			return;

		} else {	/* success */
			umass_transfer_done(sc, residue, STATUS_CMD_OK);
			return;
		}

	/***** Bulk Reset *****/
	case TSTATE_BBB_RESET1:
		if (err)
			printf("%s: BBB reset failed, %s\n",
				device_xname(sc->sc_dev), usbd_errstr(err));

		sc->transfer_state = TSTATE_BBB_RESET2;
		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
			sc->transfer_xfer[XFER_BBB_RESET2]);

		return;
	case TSTATE_BBB_RESET2:
		if (err)	/* should not occur */
			printf("%s: BBB bulk-in clear stall failed, %s\n",
			       device_xname(sc->sc_dev), usbd_errstr(err));
			/* no error recovery, otherwise we end up in a loop */

		sc->transfer_state = TSTATE_BBB_RESET3;
		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
			sc->transfer_xfer[XFER_BBB_RESET3]);

		return;
	case TSTATE_BBB_RESET3:
		if (err)	/* should not occur */
			printf("%s: BBB bulk-out clear stall failed, %s\n",
			       device_xname(sc->sc_dev), usbd_errstr(err));
			/* no error recovery, otherwise we end up in a loop */

		umass_transfer_reset(sc);

		return;

	/***** Default *****/
	default:
		panic("%s: Unknown state %d",
		      device_xname(sc->sc_dev), sc->transfer_state);
	}
}

/*
 * Command/Bulk/Interrupt (CBI) specific functions
 */

Static int
umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, int flags,
	       struct usbd_xfer *xfer)
{
	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
		   "sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
		   sc->sc_wire);

	if ((sc->sc_cmd == UMASS_CPROTO_RBC) &&
	    (sc->sc_quirks & UMASS_QUIRK_RBC_PAD_TO_12) != 0 && buflen < 12) {
		(void)memset(buffer + buflen, 0, 12 - buflen);
		buflen = 12;
	}

	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
	sc->sc_req.bRequest = UR_CBI_ADSC;
	USETW(sc->sc_req.wValue, 0);
	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
	USETW(sc->sc_req.wLength, buflen);
	return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
					 buflen, flags, xfer);
}


Static void
umass_cbi_reset(struct umass_softc *sc, int status)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	int i;
#	define SEND_DIAGNOSTIC_CMDLEN	12

	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
		   "sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
		   sc->sc_wire);

	if (sc->sc_dying) {
		umass_transfer_done(sc, sc->transfer_datalen, status);
		return;
	}

	/*
	 * Command Block Reset Protocol
	 *
	 * First send a reset request to the device. Then clear
	 * any possibly stalled bulk endpoints.

	 * This is done in 3 steps, states:
	 * TSTATE_CBI_RESET1
	 * TSTATE_CBI_RESET2
	 * TSTATE_CBI_RESET3
	 *
	 * If the reset doesn't succeed, the device should be port reset.
	 */

	DPRINTFM(UDMASS_CBI, "sc %#jx: CBI Reset", (uintptr_t)sc, 0, 0, 0);

	/* CTASSERT */
	KASSERTMSG(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
		   "%s: CBL struct is too small (%zu < %u)\n",
			device_xname(sc->sc_dev),
			sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN);

	sc->transfer_state = TSTATE_CBI_RESET1;
	sc->transfer_status = status;

	/* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
	 * the two the last 10 bytes of the cbl is filled with 0xff (section
	 * 2.2 of the CBI spec).
	 */
	sc->cbl[0] = 0x1d;	/* Command Block Reset */
	sc->cbl[1] = 0x04;
	for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
		sc->cbl[i] = 0xff;

	if (umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 0,
		sc->transfer_xfer[XFER_CBI_RESET1]))
		umass_transfer_done(sc, sc->transfer_datalen, status);
	/* XXX if the command fails we should reset the port on the bub */
}

Static void
umass_cbi_transfer(struct umass_softc *sc, int lun,
		   void *cmd, int cmdlen, void *data, int datalen, int dir,
		   u_int timeout, int flags, umass_callback cb, void *priv)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();

	DPRINTFM(UDMASS_CBI, "sc %#jx: cmd=0x%02jx, len=%jd",
	     (uintptr_t)sc, *(u_char *)cmd, datalen, 0);

	KASSERT(cb);
	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
		   "sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
		   sc->sc_wire);

	if (sc->sc_dying) {
		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
		return;
	}

	/* Be a little generous. */
	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;

	/*
	 * Do a CBI transfer with cmdlen bytes from cmd, possibly
	 * a data phase of datalen bytes from/to the device and finally a
	 * csw read phase.
	 * If the data direction was inbound a maximum of datalen bytes
	 * is stored in the buffer pointed to by data.
	 *
	 * umass_cbi_transfer initialises the transfer and lets the state
	 * machine in umass_cbi_state handle the completion. It uses the
	 * following states:
	 * TSTATE_CBI_COMMAND
	 *   -> XXX fill in
	 *
	 * An error in any of those states will invoke
	 * umass_cbi_reset.
	 */

	/* check the given arguments */
	KASSERTMSG(datalen == 0 || data != NULL,
		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
		   "%s: direction is NONE while datalen is not zero\n",
			device_xname(sc->sc_dev));

	/* store the details for the data transfer phase */
	sc->transfer_dir = dir;
	sc->transfer_data = data;
	sc->transfer_datalen = datalen;
	sc->transfer_actlen = 0;
	sc->transfer_cb = cb;
	sc->transfer_priv = priv;
	sc->transfer_status = STATUS_CMD_OK;

	/* move from idle to the command state */
	sc->transfer_state = TSTATE_CBI_COMMAND;

	/* Send the Command Block from host to device via control endpoint. */
	if (umass_cbi_adsc(sc, cmd, cmdlen, flags,
	    sc->transfer_xfer[XFER_CBI_CB]))
		umass_cbi_reset(sc, STATUS_WIRE_FAILED);
}

Static void
umass_cbi_state(struct usbd_xfer *xfer, void *priv,
		usbd_status err)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	struct umass_softc *sc = (struct umass_softc *) priv;

	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
		   "sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
		   sc->sc_wire);

	if (err == USBD_CANCELLED) {
		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
			(uintptr_t)sc, (uintptr_t)xfer, 0, 0);
		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
		return;
	}

	if (sc->sc_dying) {
		umass_transfer_done(sc, sc->transfer_datalen,
		    STATUS_WIRE_FAILED);
		return;
	}

	/*
	 * State handling for CBI transfers.
	 */

	DPRINTFM(UDMASS_CBI, "sc %#jx: Handling CBI state %jd, xfer=%#jx, ...",
	    (uintptr_t)sc, sc->transfer_state, (uintptr_t)xfer, 0);
	DPRINTFM(UDMASS_CBI, "... err %jd", err, 0, 0, 0);

	switch (sc->transfer_state) {

	/***** CBI Transfer *****/
	case TSTATE_CBI_COMMAND:
		if (err == USBD_STALLED) {
			DPRINTFM(UDMASS_CBI, "sc %#jx: Command Transport "
			    "failed", (uintptr_t)sc, 0, 0, 0);
			/* Status transport by control pipe (section 2.3.2.1).
			 * The command contained in the command block failed.
			 *
			 * The control pipe has already been unstalled by the
			 * USB stack.
			 * Section 2.4.3.1.1 states that the bulk in endpoints
			 * should not stalled at this point.
			 */
			umass_transfer_done(sc, sc->transfer_datalen,
			    STATUS_CMD_FAILED);
			return;
		} else if (err) {
			DPRINTFM(UDMASS_CBI, "sc %#jx: failed to send ADSC",
			    (uintptr_t)sc, 0, 0, 0);
			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
			return;
		}

		/* Data transport phase, setup transfer */
		sc->transfer_state = TSTATE_CBI_DATA;
		if (sc->transfer_dir == DIR_IN) {
			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
			    sc->datain_buffer, sc->transfer_datalen,
			    USBD_SHORT_XFER_OK,
			    sc->transfer_xfer[XFER_CBI_DATAIN]))
				umass_cbi_reset(sc, STATUS_WIRE_FAILED);

			return;
		} else if (sc->transfer_dir == DIR_OUT) {
			memcpy(sc->dataout_buffer, sc->transfer_data,
			       sc->transfer_datalen);
			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
			    sc->dataout_buffer, sc->transfer_datalen,
			    0, /* fixed length transfer */
			    sc->transfer_xfer[XFER_CBI_DATAOUT]))
				umass_cbi_reset(sc, STATUS_WIRE_FAILED);

			return;
		} else {
			DPRINTFM(UDMASS_CBI, "sc %#jx: no data phase",
			    (uintptr_t)sc, 0, 0, 0);
		}

		/* if no data phase, err == 0 */
		/* FALLTHROUGH */
	case TSTATE_CBI_DATA:
		/* Command transport phase error handling (ignored if no data
		 * phase (fallthrough from previous state)) */
		if (sc->transfer_dir != DIR_NONE) {
			/* retrieve the length of the transfer that was done */
			usbd_get_xfer_status(xfer, NULL, NULL,
			    &sc->transfer_actlen, NULL);
			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_DATA actlen=%jd",
				(uintptr_t)sc, sc->transfer_actlen, 0, 0);

			if (err) {
				DPRINTFM(UDMASS_CBI, "sc %#jx: Data dir %jd "
				    "err %d failed",
				    (uintptr_t)sc, sc->transfer_dir,
				    sc->transfer_datalen, err);

				if (err == USBD_STALLED) {
					sc->transfer_state = TSTATE_CBI_DCLEAR;
					umass_clear_endpoint_stall(sc,
					  (sc->transfer_dir == DIR_IN?
					    UMASS_BULKIN:UMASS_BULKOUT),
					sc->transfer_xfer[XFER_CBI_DCLEAR]);
				} else {
					/* Unless the error is a pipe stall the
					 * error is fatal.
					 */
					umass_cbi_reset(sc, STATUS_WIRE_FAILED);
				}
				return;
			}
		}

		if (sc->transfer_dir == DIR_IN)
			memcpy(sc->transfer_data, sc->datain_buffer,
			       sc->transfer_actlen);

		DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
					umass_dump_buffer(sc, sc->transfer_data,
						sc->transfer_actlen, 48));

		/* Status phase */
		if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
			sc->transfer_state = TSTATE_CBI_STATUS;
			memset(&sc->sbl, 0, sizeof(sc->sbl));
			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
				    &sc->sbl, sizeof(sc->sbl),
				    0,	/* fixed length transfer */
				    sc->transfer_xfer[XFER_CBI_STATUS]))
				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
		} else {
			/* No command completion interrupt. Request
			 * sense to get status of command.
			 */
			umass_transfer_done(sc,
			    sc->transfer_datalen - sc->transfer_actlen,
			    STATUS_CMD_UNKNOWN);
		}
		return;

	case TSTATE_CBI_STATUS:
		if (err) {
			DPRINTFM(UDMASS_CBI, "sc %#jx: Status Transport failed",
			    (uintptr_t)sc, 0, 0, 0);
			/* Status transport by interrupt pipe (section 2.3.2.2).
			 */

			if (err == USBD_STALLED) {
				sc->transfer_state = TSTATE_CBI_SCLEAR;
				umass_clear_endpoint_stall(sc, UMASS_INTRIN,
					sc->transfer_xfer[XFER_CBI_SCLEAR]);
			} else {
				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
			}
			return;
		}

		/* Dissect the information in the buffer */

		{
			uint32_t actlen;
			usbd_get_xfer_status(xfer,NULL,NULL,&actlen,NULL);
			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_STATUS actlen=%jd",
			    (uintptr_t)sc, actlen, 0, 0);
			if (actlen != 2)
				break;
		}

		if (sc->sc_cmd == UMASS_CPROTO_UFI) {
			int status;

			/* Section 3.4.3.1.3 specifies that the UFI command
			 * protocol returns an ASC and ASCQ in the interrupt
			 * data block.
			 */

			DPRINTFM(UDMASS_CBI, "sc %#jx: UFI CCI, ASC = 0x%02jx, "
			    "ASCQ = 0x%02jx", (uintptr_t)sc, sc->sbl.ufi.asc,
			    sc->sbl.ufi.ascq, 0);

			if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
			    sc->sc_sense)
				status = STATUS_CMD_OK;
			else
				status = STATUS_CMD_FAILED;

			/* No autosense, command successful */
			umass_transfer_done(sc,
			    sc->transfer_datalen - sc->transfer_actlen,
			    status);
		} else {
			int status;

			/* Command Interrupt Data Block */

			DPRINTFM(UDMASS_CBI, "sc %#jx: type=0x%02jx, "
			    "value=0x%02jx", (uintptr_t)sc,
			    sc->sbl.common.type, sc->sbl.common.value, 0);

			if (sc->sbl.common.type == IDB_TYPE_CCI) {
				switch (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) {
				case IDB_VALUE_PASS:
					status = STATUS_CMD_OK;
					break;
				case IDB_VALUE_FAIL:
				case IDB_VALUE_PERSISTENT:
					status = STATUS_CMD_FAILED;
					break;
				case IDB_VALUE_PHASE:
				default: /* XXX: gcc */
					status = STATUS_WIRE_FAILED;
					break;
				}

				umass_transfer_done(sc,
				    sc->transfer_datalen - sc->transfer_actlen,
				    status);
			} else {
				/* XXX What to do?  */
				umass_transfer_done(sc, sc->transfer_datalen,
				    STATUS_WIRE_FAILED);
			}
		}
		return;

	case TSTATE_CBI_DCLEAR:
		if (err) {	/* should not occur */
			printf("%s: CBI bulk-%s stall clear failed, %s\n",
			    device_xname(sc->sc_dev),
			    (sc->transfer_dir == DIR_IN? "in":"out"),
			    usbd_errstr(err));
			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
		} else {
			umass_transfer_done(sc,
			    sc->transfer_datalen, STATUS_CMD_FAILED);
		}
		return;

	case TSTATE_CBI_SCLEAR:
		if (err) {	/* should not occur */
			printf("%s: CBI intr-in stall clear failed, %s\n",
			       device_xname(sc->sc_dev), usbd_errstr(err));
			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
		} else {
			umass_transfer_done(sc,
			    sc->transfer_datalen, STATUS_CMD_FAILED);
		}
		return;

	/***** CBI Reset *****/
	case TSTATE_CBI_RESET1:
		if (err)
			printf("%s: CBI reset failed, %s\n",
				device_xname(sc->sc_dev), usbd_errstr(err));

		sc->transfer_state = TSTATE_CBI_RESET2;
		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
			sc->transfer_xfer[XFER_CBI_RESET2]);

		return;
	case TSTATE_CBI_RESET2:
		if (err)	/* should not occur */
			printf("%s: CBI bulk-in stall clear failed, %s\n",
			       device_xname(sc->sc_dev), usbd_errstr(err));
			/* no error recovery, otherwise we end up in a loop */

		sc->transfer_state = TSTATE_CBI_RESET3;
		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
			sc->transfer_xfer[XFER_CBI_RESET3]);

		return;
	case TSTATE_CBI_RESET3:
		if (err)	/* should not occur */
			printf("%s: CBI bulk-out stall clear failed, %s\n",
			       device_xname(sc->sc_dev), usbd_errstr(err));
			/* no error recovery, otherwise we end up in a loop */

		umass_transfer_reset(sc);
		return;


	/***** Default *****/
	default:
		panic("%s: Unknown state %d",
		      device_xname(sc->sc_dev), sc->transfer_state);
	}
}

usbd_status
umass_bbb_get_max_lun(struct umass_softc *sc, uint8_t *maxlun)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	usb_device_request_t req;
	usbd_status err;

	*maxlun = 0;		/* Default to 0. */

	DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun", (uintptr_t)sc, 0, 0, 0);

	/* The Get Max Lun command is a class-specific request. */
	req.bmRequestType = UT_READ_CLASS_INTERFACE;
	req.bRequest = UR_BBB_GET_MAX_LUN;
	USETW(req.wValue, 0);
	USETW(req.wIndex, sc->sc_ifaceno);
	USETW(req.wLength, 1);

	err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
	    USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
	switch (err) {
	case USBD_NORMAL_COMPLETION:
		DPRINTFM(UDMASS_BBB, "sc %#jx: Max Lun %jd",
		    (uintptr_t)sc, *maxlun , 0, 0);
		break;

	case USBD_STALLED:
		/*
		 * Device doesn't support Get Max Lun request.
		 */
		err = USBD_NORMAL_COMPLETION;
		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun not supported",
		    (uintptr_t)sc, 0, 0, 0);
		break;

	case USBD_SHORT_XFER:
		/*
		 * XXX This must mean Get Max Lun is not supported, too!
		 */
		err = USBD_NORMAL_COMPLETION;
		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun SHORT_XFER",
		    (uintptr_t)sc, 0, 0, 0);
		break;

	default:
		printf("%s: Get Max Lun failed: %s\n",
		    device_xname(sc->sc_dev), usbd_errstr(err));
		/* XXX Should we port_reset the device? */
		break;
	}

	return err;
}




#ifdef UMASS_DEBUG
Static void
umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	int clen = cbw->bCDBLength;
	int dlen = UGETDW(cbw->dCBWDataTransferLength);
	uint8_t *c = cbw->CBWCDB;
	int tag = UGETDW(cbw->dCBWTag);
	int flags = cbw->bCBWFlags;

	DPRINTFM(UDMASS_BBB, "sc %#jx: CBW %jd: cmdlen=%jd",
	    (uintptr_t)sc, tag, clen, 0);
	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
	    c[0], c[1], c[2], c[3]);
	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
	    c[4], c[5], c[6], c[7]);
	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx...", c[8], c[9], 0, 0);
	DPRINTFM(UDMASS_BBB, "  data = %jd bytes, flags = %jx", dlen, flags, 0,
	    0);
}

Static void
umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	int sig = UGETDW(csw->dCSWSignature);
	int tag = UGETDW(csw->dCSWTag);
	int res = UGETDW(csw->dCSWDataResidue);
	int status = csw->bCSWStatus;

	DPRINTFM(UDMASS_BBB, "sc %#jx: CSW %jd: sig = 0x%08jx, tag = %jd",
	    (uintptr_t)sc, (uintptr_t)csw, sig, tag);
	DPRINTFM(UDMASS_BBB, "  res = %jd, status = 0x%02jx",
	    res, status, 0, 0);
}

Static void
umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, int buflen,
		  int printlen)
{
	UMASSHIST_FUNC(); UMASSHIST_CALLED();
	int i;

	DPRINTFM(UDMASS_GEN, "sc %#jx: buffer %#jx", (uintptr_t)sc,
	    (uintptr_t)buffer, 0, 0);
	for (i = 0; i < buflen && i < printlen;) {
		if (i + 3 < buflen && i + 3 < printlen) {
			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx%02jx",
			    buffer[i], buffer[i + 1],
			    buffer[i + 2], buffer[i + 3]);
			i += 4;
		} else if (i + 2 < buflen && i + 2 < printlen) {
			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx",
			    buffer[i], buffer[i + 1], buffer[i + 2], 0);
			i += 3;
		} else if (i + 1 < buflen && i + 2 < printlen) {
			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx",
			    buffer[i], buffer[i + 1], 0, 0);
			i += 2;
		} else {
			DPRINTFM(UDMASS_GEN, "   0x%02jx", buffer[i], 0, 0, 0);
			i += 1;
		}
	}
}
#endif