Home | History | Annotate | Line # | Download | only in usb
umass.c revision 1.184
      1 /*	$NetBSD: umass.c,v 1.184 2020/04/13 09:26:43 jdolecek Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
     34  *		      Nick Hibma <n_hibma (at) freebsd.org>
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  *
     46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     56  * SUCH DAMAGE.
     57  *
     58  *     $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
     59  */
     60 
     61 /*
     62  * Universal Serial Bus Mass Storage Class specs:
     63  * http://www.usb.org/developers/docs/devclass_docs/Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf
     64  * http://www.usb.org/developers/docs/devclass_docs/usbmassbulk_10.pdf
     65  * http://www.usb.org/developers/docs/devclass_docs/usb_msc_cbi_1.1.pdf
     66  * http://www.usb.org/developers/docs/devclass_docs/usbmass-ufi10.pdf
     67  */
     68 
     69 /*
     70  * Ported to NetBSD by Lennart Augustsson <augustss (at) NetBSD.org>.
     71  * Parts of the code written by Jason R. Thorpe <thorpej (at) shagadelic.org>.
     72  */
     73 
     74 /*
     75  * The driver handles 3 Wire Protocols
     76  * - Command/Bulk/Interrupt (CBI)
     77  * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
     78  * - Mass Storage Bulk-Only (BBB)
     79  *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
     80  *
     81  * Over these wire protocols it handles the following command protocols
     82  * - SCSI
     83  * - 8070 (ATA/ATAPI for rewritable removable media)
     84  * - UFI (USB Floppy Interface)
     85  *
     86  * 8070i is a transformed version of the SCSI command set. UFI is a transformed
     87  * version of the 8070i command set.  The sc->transform method is used to
     88  * convert the commands into the appropriate format (if at all necessary).
     89  * For example, ATAPI requires all commands to be 12 bytes in length amongst
     90  * other things.
     91  *
     92  * The source code below is marked and can be split into a number of pieces
     93  * (in this order):
     94  *
     95  * - probe/attach/detach
     96  * - generic transfer routines
     97  * - BBB
     98  * - CBI
     99  * - CBI_I (in addition to functions from CBI)
    100  * - CAM (Common Access Method)
    101  * - SCSI
    102  * - UFI
    103  * - 8070i
    104  *
    105  * The protocols are implemented using a state machine, for the transfers as
    106  * well as for the resets. The state machine is contained in umass_*_state.
    107  * The state machine is started through either umass_*_transfer or
    108  * umass_*_reset.
    109  *
    110  * The reason for doing this is a) CAM performs a lot better this way and b) it
    111  * avoids sleeping in interrupt context which is prohibited (for example after a
    112  * failed transfer).
    113  */
    114 
    115 /*
    116  * The SCSI related part of this driver has been derived from the
    117  * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch (at) freebsd.org).
    118  *
    119  * The CAM layer uses so called actions which are messages sent to the host
    120  * adapter for completion. The actions come in through umass_cam_action. The
    121  * appropriate block of routines is called depending on the transport protocol
    122  * in use. When the transfer has finished, these routines call
    123  * umass_cam_cb again to complete the CAM command.
    124  */
    125 
    126 #include <sys/cdefs.h>
    127 __KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.184 2020/04/13 09:26:43 jdolecek Exp $");
    128 
    129 #ifdef _KERNEL_OPT
    130 #include "opt_usb.h"
    131 #endif
    132 
    133 #include "atapibus.h"
    134 #include "scsibus.h"
    135 
    136 #include <sys/param.h>
    137 #include <sys/buf.h>
    138 #include <sys/conf.h>
    139 #include <sys/device.h>
    140 #include <sys/kernel.h>
    141 #include <sys/kmem.h>
    142 #include <sys/sysctl.h>
    143 #include <sys/systm.h>
    144 
    145 #include <dev/usb/usb.h>
    146 #include <dev/usb/usb_sdt.h>
    147 #include <dev/usb/usbdi.h>
    148 #include <dev/usb/usbdi_util.h>
    149 #include <dev/usb/usbdevs.h>
    150 #include <dev/usb/usbhist.h>
    151 
    152 #include <dev/usb/umassvar.h>
    153 #include <dev/usb/umass_quirks.h>
    154 #include <dev/usb/umass_scsipi.h>
    155 
    156 #include <dev/scsipi/scsipi_all.h>
    157 #include <dev/scsipi/scsipiconf.h>
    158 
    159 SDT_PROBE_DEFINE1(usb, umass, device, attach__start,
    160     "struct umass_softc *"/*sc*/);
    161 SDT_PROBE_DEFINE2(usb, umass, device, attach__done,
    162     "struct umass_softc *"/*sc*/, "usbd_status"/*err*/);
    163 SDT_PROBE_DEFINE1(usb, umass, device, detach__start,
    164     "struct umass_softc *"/*sc*/);
    165 SDT_PROBE_DEFINE2(usb, umass, device, detach__done,
    166     "struct umass_softc *"/*sc*/, "int"/*error*/);
    167 
    168 SDT_PROBE_DEFINE7(usb, umass, transfer, start__bbb,
    169     "struct umass_softc *"/*sc*/,
    170     "transfer_cb_f"/*cb*/,
    171     "void *"/*priv*/,
    172     "void *"/*data*/,
    173     "int"/*datalen*/,
    174     "int"/*dir*/,
    175     "int"/*timeout*/);
    176 SDT_PROBE_DEFINE7(usb, umass, transfer, start__cbi,
    177     "struct umass_softc *"/*sc*/,
    178     "transfer_cb_f"/*cb*/,
    179     "void *"/*priv*/,
    180     "void *"/*data*/,
    181     "int"/*datalen*/,
    182     "int"/*dir*/,
    183     "int"/*timeout*/);
    184 SDT_PROBE_DEFINE7(usb, umass, transfer, done,
    185     "struct umass_softc *"/*sc*/,
    186     "transfer_cb_f"/*cb*/,
    187     "void *"/*priv*/,
    188     "void *"/*data*/,
    189     "int"/*datalen*/,
    190     "int"/*resid*/,
    191     "int"/*status*/);	/* STATUS_* */
    192 
    193 SDT_PROBE_DEFINE3(usb, umass, bbb, state,
    194     "struct umass_softc *"/*sc*/,
    195     "struct usbd_xfer *"/*xfer*/,
    196     "usbd_status"/*err*/);
    197 SDT_PROBE_DEFINE2(usb, umass, bbb, reset,
    198     "struct umass_softc *"/*sc*/,
    199     "int"/*status*/);
    200 
    201 SDT_PROBE_DEFINE3(usb, umass, cbi, state,
    202     "struct umass_softc *"/*sc*/,
    203     "struct usbd_xfer *"/*xfer*/,
    204     "usbd_status"/*err*/);
    205 SDT_PROBE_DEFINE2(usb, umass, cbi, reset,
    206     "struct umass_softc *"/*sc*/,
    207     "int"/*status*/);
    208 
    209 #ifdef USB_DEBUG
    210 #ifdef UMASS_DEBUG
    211 int umassdebug = 0;
    212 
    213 SYSCTL_SETUP(sysctl_hw_umass_setup, "sysctl hw.umass setup")
    214 {
    215 	int err;
    216 	const struct sysctlnode *rnode;
    217 	const struct sysctlnode *cnode;
    218 
    219 	err = sysctl_createv(clog, 0, NULL, &rnode,
    220 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "umass",
    221 	    SYSCTL_DESCR("umass global controls"),
    222 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    223 
    224 	if (err)
    225 		goto fail;
    226 
    227 	/* control debugging printfs */
    228 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    229 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    230 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    231 	    NULL, 0, &umassdebug, sizeof(umassdebug), CTL_CREATE, CTL_EOL);
    232 	if (err)
    233 		goto fail;
    234 
    235 	return;
    236 fail:
    237 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    238 }
    239 
    240 const char *states[TSTATE_STATES+1] = {
    241 	/* should be kept in sync with the list at transfer_state */
    242 	"Idle",
    243 	"BBB CBW",
    244 	"BBB Data",
    245 	"BBB Data bulk-in/-out clear stall",
    246 	"BBB CSW, 1st attempt",
    247 	"BBB CSW bulk-in clear stall",
    248 	"BBB CSW, 2nd attempt",
    249 	"BBB Reset",
    250 	"BBB bulk-in clear stall",
    251 	"BBB bulk-out clear stall",
    252 	"CBI Command",
    253 	"CBI Data",
    254 	"CBI Status",
    255 	"CBI Data bulk-in/-out clear stall",
    256 	"CBI Status intr-in clear stall",
    257 	"CBI Reset",
    258 	"CBI bulk-in clear stall",
    259 	"CBI bulk-out clear stall",
    260 	NULL
    261 };
    262 #endif
    263 #endif
    264 
    265 /* USB device probe/attach/detach functions */
    266 static int umass_match(device_t, cfdata_t, void *);
    267 static void umass_attach(device_t, device_t, void *);
    268 static int umass_detach(device_t, int);
    269 static void umass_childdet(device_t, device_t);
    270 static int umass_activate(device_t, enum devact);
    271 
    272 CFATTACH_DECL2_NEW(umass, sizeof(struct umass_softc), umass_match,
    273     umass_attach, umass_detach, umass_activate, NULL, umass_childdet);
    274 
    275 Static void umass_disco(struct umass_softc *sc);
    276 
    277 /* generic transfer functions */
    278 Static usbd_status umass_setup_transfer(struct umass_softc *,
    279 				struct usbd_pipe *,
    280 				void *, int, int,
    281 				struct usbd_xfer *);
    282 Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *,
    283 				usb_device_request_t *,
    284 				void *, int, int,
    285 				struct usbd_xfer *);
    286 Static void umass_clear_endpoint_stall(struct umass_softc *, int,
    287 				struct usbd_xfer *);
    288 Static void umass_transfer_done(struct umass_softc *, int, int);
    289 Static void umass_transfer_reset(struct umass_softc *);
    290 #if 0
    291 Static void umass_reset(struct umass_softc *, transfer_cb_f, void *);
    292 #endif
    293 
    294 /* Bulk-Only related functions */
    295 Static void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *,
    296 			       int, int, u_int, int, umass_callback, void *);
    297 Static void umass_bbb_reset(struct umass_softc *, int);
    298 Static void umass_bbb_state(struct usbd_xfer *, void *, usbd_status);
    299 
    300 static usbd_status umass_bbb_get_max_lun(struct umass_softc *, uint8_t *);
    301 
    302 /* CBI related functions */
    303 Static void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *,
    304 			       int, int, u_int, int, umass_callback, void *);
    305 Static void umass_cbi_reset(struct umass_softc *, int);
    306 Static void umass_cbi_state(struct usbd_xfer *, void *, usbd_status);
    307 
    308 Static int umass_cbi_adsc(struct umass_softc *, char *, int, int,
    309     struct usbd_xfer *);
    310 
    311 const struct umass_wire_methods umass_bbb_methods = {
    312 	.wire_xfer = umass_bbb_transfer,
    313 	.wire_reset = umass_bbb_reset,
    314 	.wire_state = umass_bbb_state
    315 };
    316 
    317 const struct umass_wire_methods umass_cbi_methods = {
    318 	.wire_xfer = umass_cbi_transfer,
    319 	.wire_reset = umass_cbi_reset,
    320 	.wire_state = umass_cbi_state
    321 };
    322 
    323 #ifdef UMASS_DEBUG
    324 /* General debugging functions */
    325 Static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
    326 Static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
    327 Static void umass_dump_buffer(struct umass_softc *, uint8_t *, int, int);
    328 #endif
    329 
    330 
    331 /*
    332  * USB device probe/attach/detach
    333  */
    334 
    335 static int
    336 umass_match(device_t parent, cfdata_t match, void *aux)
    337 {
    338 	struct usbif_attach_arg *uiaa = aux;
    339 	const struct umass_quirk *quirk;
    340 
    341 	quirk = umass_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
    342 	if (quirk != NULL && quirk->uq_match != UMASS_QUIRK_USE_DEFAULTMATCH)
    343 		return quirk->uq_match;
    344 
    345 	if (uiaa->uiaa_class != UICLASS_MASS)
    346 		return UMATCH_NONE;
    347 
    348 	switch (uiaa->uiaa_subclass) {
    349 	case UISUBCLASS_RBC:
    350 	case UISUBCLASS_SFF8020I:
    351 	case UISUBCLASS_QIC157:
    352 	case UISUBCLASS_UFI:
    353 	case UISUBCLASS_SFF8070I:
    354 	case UISUBCLASS_SCSI:
    355 		break;
    356 	default:
    357 		return UMATCH_IFACECLASS;
    358 	}
    359 
    360 	switch (uiaa->uiaa_proto) {
    361 	case UIPROTO_MASS_CBI_I:
    362 	case UIPROTO_MASS_CBI:
    363 	case UIPROTO_MASS_BBB_OLD:
    364 	case UIPROTO_MASS_BBB:
    365 		break;
    366 	default:
    367 		return UMATCH_IFACECLASS_IFACESUBCLASS;
    368 	}
    369 
    370 	return UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO;
    371 }
    372 
    373 static void
    374 umass_attach(device_t parent, device_t self, void *aux)
    375 {
    376 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    377 	struct umass_softc *sc = device_private(self);
    378 	struct usbif_attach_arg *uiaa = aux;
    379 	const struct umass_quirk *quirk;
    380 	usb_interface_descriptor_t *id;
    381 	usb_endpoint_descriptor_t *ed;
    382 	const char *sWire, *sCommand;
    383 	char *devinfop;
    384 	usbd_status err;
    385 	int i, error;
    386 
    387 	SDT_PROBE1(usb, umass, device, attach__start,  sc);
    388 
    389 	sc->sc_dev = self;
    390 
    391 	aprint_naive("\n");
    392 	aprint_normal("\n");
    393 
    394 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    395 	cv_init(&sc->sc_detach_cv, "umassdet");
    396 
    397 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    398 	aprint_normal_dev(self, "%s\n", devinfop);
    399 	usbd_devinfo_free(devinfop);
    400 
    401 	sc->sc_udev = uiaa->uiaa_device;
    402 	sc->sc_iface = uiaa->uiaa_iface;
    403 	sc->sc_ifaceno = uiaa->uiaa_ifaceno;
    404 
    405 	quirk = umass_lookup(uiaa->uiaa_vendor, uiaa->uiaa_product);
    406 	if (quirk != NULL) {
    407 		sc->sc_wire = quirk->uq_wire;
    408 		sc->sc_cmd = quirk->uq_cmd;
    409 		sc->sc_quirks = quirk->uq_flags;
    410 		sc->sc_busquirks = quirk->uq_busquirks;
    411 
    412 		if (quirk->uq_fixup != NULL)
    413 			(*quirk->uq_fixup)(sc);
    414 	} else {
    415 		sc->sc_wire = UMASS_WPROTO_UNSPEC;
    416 		sc->sc_cmd = UMASS_CPROTO_UNSPEC;
    417 		sc->sc_quirks = 0;
    418 		sc->sc_busquirks = 0;
    419 	}
    420 
    421 	if (sc->sc_wire == UMASS_WPROTO_UNSPEC) {
    422 		switch (uiaa->uiaa_proto) {
    423 		case UIPROTO_MASS_CBI:
    424 			sc->sc_wire = UMASS_WPROTO_CBI;
    425 			break;
    426 		case UIPROTO_MASS_CBI_I:
    427 			sc->sc_wire = UMASS_WPROTO_CBI_I;
    428 			break;
    429 		case UIPROTO_MASS_BBB:
    430 		case UIPROTO_MASS_BBB_OLD:
    431 			sc->sc_wire = UMASS_WPROTO_BBB;
    432 			break;
    433 		default:
    434 			DPRINTFM(UDMASS_GEN, "Unsupported wire protocol %ju",
    435 			    uiaa->uiaa_proto, 0, 0, 0);
    436 			SDT_PROBE2(usb, umass, device, attach__done,
    437 			    sc, USBD_IOERROR);
    438 			return;
    439 		}
    440 	}
    441 
    442 	if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) {
    443 		switch (uiaa->uiaa_subclass) {
    444 		case UISUBCLASS_SCSI:
    445 			sc->sc_cmd = UMASS_CPROTO_SCSI;
    446 			break;
    447 		case UISUBCLASS_UFI:
    448 			sc->sc_cmd = UMASS_CPROTO_UFI;
    449 			break;
    450 		case UISUBCLASS_SFF8020I:
    451 		case UISUBCLASS_SFF8070I:
    452 		case UISUBCLASS_QIC157:
    453 			sc->sc_cmd = UMASS_CPROTO_ATAPI;
    454 			break;
    455 		case UISUBCLASS_RBC:
    456 			sc->sc_cmd = UMASS_CPROTO_RBC;
    457 			break;
    458 		default:
    459 			DPRINTFM(UDMASS_GEN, "Unsupported command protocol %ju",
    460 			    uiaa->uiaa_subclass, 0, 0, 0);
    461 			SDT_PROBE2(usb, umass, device, attach__done,
    462 			    sc, USBD_IOERROR);
    463 			return;
    464 		}
    465 	}
    466 
    467 	switch (sc->sc_wire) {
    468 	case UMASS_WPROTO_CBI:
    469 		sWire = "CBI";
    470 		break;
    471 	case UMASS_WPROTO_CBI_I:
    472 		sWire = "CBI with CCI";
    473 		break;
    474 	case UMASS_WPROTO_BBB:
    475 		sWire = "Bulk-Only";
    476 		break;
    477 	default:
    478 		sWire = "unknown";
    479 		break;
    480 	}
    481 
    482 	switch (sc->sc_cmd) {
    483 	case UMASS_CPROTO_RBC:
    484 		sCommand = "RBC";
    485 		break;
    486 	case UMASS_CPROTO_SCSI:
    487 		sCommand = "SCSI";
    488 		break;
    489 	case UMASS_CPROTO_UFI:
    490 		sCommand = "UFI";
    491 		break;
    492 	case UMASS_CPROTO_ATAPI:
    493 		sCommand = "ATAPI";
    494 		break;
    495 	case UMASS_CPROTO_ISD_ATA:
    496 		sCommand = "ISD-ATA";
    497 		break;
    498 	default:
    499 		sCommand = "unknown";
    500 		break;
    501 	}
    502 
    503 	aprint_verbose_dev(self, "using %s over %s\n", sCommand, sWire);
    504 
    505 	if (quirk != NULL && quirk->uq_init != NULL) {
    506 		err = (*quirk->uq_init)(sc);
    507 		if (err) {
    508 			aprint_error_dev(self, "quirk init failed\n");
    509 			SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    510 			umass_disco(sc);
    511 			return;
    512 		}
    513 	}
    514 
    515 	/*
    516 	 * In addition to the Control endpoint the following endpoints
    517 	 * are required:
    518 	 * a) bulk-in endpoint.
    519 	 * b) bulk-out endpoint.
    520 	 * and for Control/Bulk/Interrupt with CCI (CBI_I)
    521 	 * c) intr-in
    522 	 *
    523 	 * The endpoint addresses are not fixed, so we have to read them
    524 	 * from the device descriptors of the current interface.
    525 	 */
    526 	id = usbd_get_interface_descriptor(sc->sc_iface);
    527 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    528 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    529 		if (ed == NULL) {
    530 			aprint_error_dev(self,
    531 			    "could not read endpoint descriptor\n");
    532 			SDT_PROBE2(usb, umass, device, attach__done,
    533 			    sc, USBD_IOERROR);
    534 			return;
    535 		}
    536 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
    537 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    538 			sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
    539 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
    540 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    541 			sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
    542 		} else if (sc->sc_wire == UMASS_WPROTO_CBI_I
    543 		    && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
    544 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    545 			sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
    546 #ifdef UMASS_DEBUG
    547 			if (UGETW(ed->wMaxPacketSize) > 2) {
    548 				DPRINTFM(UDMASS_CBI, "sc %#jx intr size is %jd",
    549 				    (uintptr_t)sc, UGETW(ed->wMaxPacketSize),
    550 				    0, 0);
    551 			}
    552 #endif
    553 		}
    554 	}
    555 
    556 	/* check whether we found all the endpoints we need */
    557 	if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
    558 	    (sc->sc_wire == UMASS_WPROTO_CBI_I &&
    559 	     !sc->sc_epaddr[UMASS_INTRIN])) {
    560 		aprint_error_dev(self, "endpoint not found %u/%u/%u\n",
    561 		       sc->sc_epaddr[UMASS_BULKIN],
    562 		       sc->sc_epaddr[UMASS_BULKOUT],
    563 		       sc->sc_epaddr[UMASS_INTRIN]);
    564 		return;
    565 	}
    566 
    567 	/*
    568 	 * Get the maximum LUN supported by the device.
    569 	 */
    570 	if (sc->sc_wire == UMASS_WPROTO_BBB &&
    571 	    (sc->sc_quirks & UMASS_QUIRK_NOGETMAXLUN) == 0) {
    572 		err = umass_bbb_get_max_lun(sc, &sc->maxlun);
    573 		if (err) {
    574 			aprint_error_dev(self, "unable to get Max Lun: %s\n",
    575 			    usbd_errstr(err));
    576 			SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    577 			return;
    578 		}
    579 		if (sc->maxlun > 0)
    580 			sc->sc_busquirks |= PQUIRK_FORCELUNS;
    581 	} else {
    582 		sc->maxlun = 0;
    583 	}
    584 
    585 	/* Open the bulk-in and -out pipe */
    586 	DPRINTFM(UDMASS_USB, "sc %#jx: opening iface %#jx epaddr %jd for "
    587 	    "BULKOUT", (uintptr_t)sc, (uintptr_t)sc->sc_iface,
    588 	    sc->sc_epaddr[UMASS_BULKOUT], 0);
    589 	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
    590 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_BULKOUT]);
    591 	if (err) {
    592 		aprint_error_dev(self, "cannot open %u-out pipe (bulk)\n",
    593 		    sc->sc_epaddr[UMASS_BULKOUT]);
    594 		SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    595 		umass_disco(sc);
    596 		return;
    597 	}
    598 	DPRINTFM(UDMASS_USB, "sc %#jx: opening iface %#jx epaddr %jd for "
    599 	    "BULKIN", (uintptr_t)sc, (uintptr_t)sc->sc_iface,
    600 	    sc->sc_epaddr[UMASS_BULKIN], 0);
    601 	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
    602 	    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_BULKIN]);
    603 	if (err) {
    604 		aprint_error_dev(self, "could not open %u-in pipe (bulk)\n",
    605 		    sc->sc_epaddr[UMASS_BULKIN]);
    606 		SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    607 		umass_disco(sc);
    608 		return;
    609 	}
    610 	/*
    611 	 * Open the intr-in pipe if the protocol is CBI with CCI.
    612 	 * Note: early versions of the Zip drive do have an interrupt pipe, but
    613 	 * this pipe is unused
    614 	 *
    615 	 * We do not open the interrupt pipe as an interrupt pipe, but as a
    616 	 * normal bulk endpoint. We send an IN transfer down the wire at the
    617 	 * appropriate time, because we know exactly when to expect data on
    618 	 * that endpoint. This saves bandwidth, but more important, makes the
    619 	 * code for handling the data on that endpoint simpler. No data
    620 	 * arriving concurrently.
    621 	 */
    622 	if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
    623 		DPRINTFM(UDMASS_USB,
    624 		    "sc %#jx: opening iface %#jx epaddr %jd for INTRIN",
    625 		    (uintptr_t)sc, (uintptr_t)sc->sc_iface,
    626 		    sc->sc_epaddr[UMASS_INTRIN], 0);
    627 		err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
    628 		    USBD_EXCLUSIVE_USE | USBD_MPSAFE, &sc->sc_pipe[UMASS_INTRIN]);
    629 		if (err) {
    630 			aprint_error_dev(self, "couldn't open %u-in (intr)\n",
    631 			    sc->sc_epaddr[UMASS_INTRIN]);
    632 			SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    633 			umass_disco(sc);
    634 			return;
    635 		}
    636 	}
    637 
    638 	/* initialisation of generic part */
    639 	sc->transfer_state = TSTATE_IDLE;
    640 
    641 	for (i = 0; i < XFER_NR; i++) {
    642 		sc->transfer_xfer[i] = NULL;
    643 	}
    644 
    645 	/*
    646 	 * Create the transfers
    647 	 */
    648 	struct usbd_pipe *pipe0 = usbd_get_pipe0(sc->sc_udev);
    649 	switch (sc->sc_wire) {
    650 	case UMASS_WPROTO_BBB:
    651 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
    652 		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
    653 		    &sc->transfer_xfer[XFER_BBB_DATAIN]);
    654 		if (err)
    655 			goto fail_create;
    656 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
    657 		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
    658 		    &sc->transfer_xfer[XFER_BBB_DATAOUT]);
    659 		if (err)
    660 			goto fail_create;
    661 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
    662 		    UMASS_BBB_CBW_SIZE, 0, 0,
    663 		    &sc->transfer_xfer[XFER_BBB_CBW]);
    664 		if (err)
    665 			goto fail_create;
    666 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
    667 		    UMASS_BBB_CSW_SIZE, 0, 0,
    668 		    &sc->transfer_xfer[XFER_BBB_CSW1]);
    669 		if (err)
    670 			goto fail_create;
    671 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
    672 		    UMASS_BBB_CSW_SIZE, 0, 0,
    673 		    &sc->transfer_xfer[XFER_BBB_CSW2]);
    674 		if (err)
    675 			goto fail_create;
    676 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    677 		    &sc->transfer_xfer[XFER_BBB_SCLEAR]);
    678 		if (err)
    679 			goto fail_create;
    680 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    681 		    &sc->transfer_xfer[XFER_BBB_DCLEAR]);
    682 		if (err)
    683 			goto fail_create;
    684 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    685 		    &sc->transfer_xfer[XFER_BBB_RESET1]);
    686 		if (err)
    687 			goto fail_create;
    688 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    689 		    &sc->transfer_xfer[XFER_BBB_RESET2]);
    690 		if (err)
    691 			goto fail_create;
    692 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    693 		    &sc->transfer_xfer[XFER_BBB_RESET3]);
    694 		if (err)
    695 			goto fail_create;
    696 		break;
    697 	case UMASS_WPROTO_CBI:
    698 	case UMASS_WPROTO_CBI_I:
    699 		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
    700 		    &sc->transfer_xfer[XFER_CBI_CB]);
    701 		if (err)
    702 			goto fail_create;
    703 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKIN],
    704 		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
    705 		    &sc->transfer_xfer[XFER_CBI_DATAIN]);
    706 		if (err)
    707 			goto fail_create;
    708 		err = usbd_create_xfer(sc->sc_pipe[UMASS_BULKOUT],
    709 		    UMASS_MAX_TRANSFER_SIZE, 0, 0,
    710 		    &sc->transfer_xfer[XFER_CBI_DATAOUT]);
    711 		if (err)
    712 			goto fail_create;
    713 		err = usbd_create_xfer(sc->sc_pipe[UMASS_INTRIN],
    714 		    sizeof(sc->sbl), 0, 0,
    715 		    &sc->transfer_xfer[XFER_CBI_STATUS]);
    716 		if (err)
    717 			goto fail_create;
    718 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    719 		    &sc->transfer_xfer[XFER_CBI_DCLEAR]);
    720 		if (err)
    721 			goto fail_create;
    722 		err = usbd_create_xfer(pipe0, 0, 0, 0,
    723 		    &sc->transfer_xfer[XFER_CBI_SCLEAR]);
    724 		if (err)
    725 			goto fail_create;
    726 		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
    727 		    &sc->transfer_xfer[XFER_CBI_RESET1]);
    728 		if (err)
    729 			goto fail_create;
    730 		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
    731 		    &sc->transfer_xfer[XFER_CBI_RESET2]);
    732 		if (err)
    733 			goto fail_create;
    734 		err = usbd_create_xfer(pipe0, sizeof(sc->cbl), 0, 0,
    735 		    &sc->transfer_xfer[XFER_CBI_RESET3]);
    736 		if (err)
    737 			goto fail_create;
    738 		break;
    739 	default:
    740 	fail_create:
    741 		aprint_error_dev(self, "failed to create xfers\n");
    742 		SDT_PROBE2(usb, umass, device, attach__done,  sc, err);
    743 		umass_disco(sc);
    744 		return;
    745 	}
    746 
    747 	/*
    748 	 * Record buffer pointers for data transfer (it's huge), command and
    749 	 * status data here
    750 	 */
    751 	switch (sc->sc_wire) {
    752 	case UMASS_WPROTO_BBB:
    753 		sc->datain_buffer =
    754 		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_DATAIN]);
    755 		sc->dataout_buffer =
    756 		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_DATAOUT]);
    757 		sc->cmd_buffer =
    758 		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CBW]);
    759 		sc->s1_buffer =
    760 		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CSW1]);
    761 		sc->s2_buffer =
    762 		    usbd_get_buffer(sc->transfer_xfer[XFER_BBB_CSW2]);
    763 		break;
    764 	case UMASS_WPROTO_CBI:
    765 	case UMASS_WPROTO_CBI_I:
    766 		sc->datain_buffer =
    767 		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_DATAIN]);
    768 		sc->dataout_buffer =
    769 		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_DATAOUT]);
    770 		sc->cmd_buffer =
    771 		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_CB]);
    772 		sc->s1_buffer =
    773 		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_STATUS]);
    774 		sc->s2_buffer =
    775 		    usbd_get_buffer(sc->transfer_xfer[XFER_CBI_RESET1]);
    776 		break;
    777 	default:
    778 		break;
    779 	}
    780 
    781 	/* Initialise the wire protocol specific methods */
    782 	switch (sc->sc_wire) {
    783 	case UMASS_WPROTO_BBB:
    784 		sc->sc_methods = &umass_bbb_methods;
    785 		break;
    786 	case UMASS_WPROTO_CBI:
    787 	case UMASS_WPROTO_CBI_I:
    788 		sc->sc_methods = &umass_cbi_methods;
    789 		break;
    790 	default:
    791 		umass_disco(sc);
    792 		return;
    793 	}
    794 
    795 	error = 0;
    796 	switch (sc->sc_cmd) {
    797 	case UMASS_CPROTO_RBC:
    798 	case UMASS_CPROTO_SCSI:
    799 #if NSCSIBUS > 0
    800 		error = umass_scsi_attach(sc);
    801 #else
    802 		aprint_error_dev(self, "scsibus not configured\n");
    803 #endif
    804 		break;
    805 
    806 	case UMASS_CPROTO_UFI:
    807 	case UMASS_CPROTO_ATAPI:
    808 #if NATAPIBUS > 0
    809 		error = umass_atapi_attach(sc);
    810 #else
    811 		aprint_error_dev(self, "atapibus not configured\n");
    812 #endif
    813 		break;
    814 
    815 	default:
    816 		aprint_error_dev(self, "command protocol=%#x not supported\n",
    817 		    sc->sc_cmd);
    818 		umass_disco(sc);
    819 		return;
    820 	}
    821 	if (error) {
    822 		aprint_error_dev(self, "bus attach failed\n");
    823 		SDT_PROBE2(usb, umass, device, attach__done,
    824 		    sc, USBD_IOERROR);
    825 		umass_disco(sc);
    826 		return;
    827 	}
    828 
    829 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    830 
    831 	if (!pmf_device_register(self, NULL, NULL))
    832 		aprint_error_dev(self, "couldn't establish power handler\n");
    833 
    834 	DPRINTFM(UDMASS_GEN, "sc %#jx: Attach finished", (uintptr_t)sc,
    835 	    0, 0, 0);
    836 
    837 	SDT_PROBE2(usb, umass, device, attach__done,  sc, 0);
    838 	return;
    839 }
    840 
    841 static void
    842 umass_childdet(device_t self, device_t child)
    843 {
    844 	struct umass_softc *sc = device_private(self);
    845 
    846 	KASSERTMSG(child == sc->bus->sc_child,
    847 		   "assertion child == sc->bus->sc_child failed\n");
    848 	sc->bus->sc_child = NULL;
    849 }
    850 
    851 static int
    852 umass_detach(device_t self, int flags)
    853 {
    854 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    855 	struct umass_softc *sc = device_private(self);
    856 	struct umassbus_softc *scbus;
    857 	int rv = 0, i;
    858 
    859 	DPRINTFM(UDMASS_USB, "sc %#jx detached", (uintptr_t)sc, 0, 0, 0);
    860 	SDT_PROBE1(usb, umass, device, detach__start,  sc);
    861 
    862 	mutex_enter(&sc->sc_lock);
    863 	sc->sc_dying = true;
    864 	mutex_exit(&sc->sc_lock);
    865 
    866 	pmf_device_deregister(self);
    867 
    868 	/* Abort the pipes to wake up any waiting processes. */
    869 	for (i = 0 ; i < UMASS_NEP ; i++) {
    870 		if (sc->sc_pipe[i] != NULL)
    871 			usbd_abort_pipe(sc->sc_pipe[i]);
    872 	}
    873 	usbd_abort_default_pipe(sc->sc_udev);
    874 
    875 	/* Do we really need reference counting?  Perhaps in ioctl() */
    876 	mutex_enter(&sc->sc_lock);
    877 	if (--sc->sc_refcnt >= 0) {
    878 #ifdef DIAGNOSTIC
    879 		aprint_normal_dev(self, "waiting for refcnt\n");
    880 #endif
    881 		/* Wait for processes to go away. */
    882 		if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60))
    883 			aprint_error_dev(self, ": didn't detach\n");
    884 	}
    885 	mutex_exit(&sc->sc_lock);
    886 
    887 	scbus = sc->bus;
    888 	if (scbus != NULL) {
    889 		if (scbus->sc_child != NULL)
    890 			rv = config_detach(scbus->sc_child, flags);
    891 
    892 		switch (sc->sc_cmd) {
    893 		case UMASS_CPROTO_RBC:
    894 		case UMASS_CPROTO_SCSI:
    895 #if NSCSIBUS > 0
    896 			umass_scsi_detach(sc);
    897 #else
    898 			aprint_error_dev(self, "scsibus not configured\n");
    899 #endif
    900 			break;
    901 
    902 		case UMASS_CPROTO_UFI:
    903 		case UMASS_CPROTO_ATAPI:
    904 #if NATAPIBUS > 0
    905 			umass_atapi_detach(sc);
    906 #else
    907 			aprint_error_dev(self, "atapibus not configured\n");
    908 #endif
    909 			break;
    910 
    911 		default:
    912 			/* nothing to do */
    913 			break;
    914 		}
    915 
    916 		/* protocol detach is expected to free sc->bus */
    917 		KASSERT(sc->bus == NULL);
    918 	}
    919 
    920 	if (rv)
    921 		goto out;
    922 
    923 	umass_disco(sc);
    924 
    925 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    926 
    927 	mutex_destroy(&sc->sc_lock);
    928 	cv_destroy(&sc->sc_detach_cv);
    929 
    930 out:	SDT_PROBE2(usb, umass, device, detach__done,  sc, rv);
    931 	return rv;
    932 }
    933 
    934 static int
    935 umass_activate(device_t dev, enum devact act)
    936 {
    937 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    938 	struct umass_softc *sc = device_private(dev);
    939 
    940 	DPRINTFM(UDMASS_USB, "sc %#jx act %jd", (uintptr_t)sc, act, 0, 0);
    941 
    942 	switch (act) {
    943 	case DVACT_DEACTIVATE:
    944 		sc->sc_dying = 1;
    945 		return 0;
    946 	default:
    947 		return EOPNOTSUPP;
    948 	}
    949 }
    950 
    951 Static void
    952 umass_disco(struct umass_softc *sc)
    953 {
    954 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    955 	int i;
    956 
    957 	/* Remove all the pipes. */
    958 	for (i = 0 ; i < UMASS_NEP ; i++) {
    959 		if (sc->sc_pipe[i] != NULL) {
    960 			usbd_abort_pipe(sc->sc_pipe[i]);
    961 		}
    962 	}
    963 
    964 	/* Some xfers may be queued in the default pipe */
    965 	usbd_abort_default_pipe(sc->sc_udev);
    966 
    967 	/* Free the xfers. */
    968 	for (i = 0; i < XFER_NR; i++) {
    969 		if (sc->transfer_xfer[i] != NULL) {
    970 			usbd_destroy_xfer(sc->transfer_xfer[i]);
    971 			sc->transfer_xfer[i] = NULL;
    972 		}
    973 	}
    974 
    975 	for (i = 0 ; i < UMASS_NEP ; i++) {
    976 		if (sc->sc_pipe[i] != NULL) {
    977 			usbd_close_pipe(sc->sc_pipe[i]);
    978 			sc->sc_pipe[i] = NULL;
    979 		}
    980 	}
    981 
    982 }
    983 
    984 /*
    985  * Generic functions to handle transfers
    986  */
    987 
    988 Static usbd_status
    989 umass_setup_transfer(struct umass_softc *sc, struct usbd_pipe *pipe,
    990 			void *buffer, int buflen, int flags,
    991 			struct usbd_xfer *xfer)
    992 {
    993 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
    994 	usbd_status err;
    995 
    996 	if (sc->sc_dying)
    997 		return USBD_IOERROR;
    998 
    999 	/* Initialiase a USB transfer and then schedule it */
   1000 
   1001 	usbd_setup_xfer(xfer, sc, buffer, buflen, flags, sc->timeout,
   1002 	    sc->sc_methods->wire_state);
   1003 
   1004 	err = usbd_transfer(xfer);
   1005 	DPRINTFM(UDMASS_XFER, "start xfer buffer=%#jx buflen=%jd flags=%#jx "
   1006 	    "timeout=%jd", (uintptr_t)buffer, buflen, flags, sc->timeout);
   1007 	if (err && err != USBD_IN_PROGRESS) {
   1008 		DPRINTFM(UDMASS_BBB, "failed to setup transfer... err=%jd",
   1009 		    err, 0, 0, 0);
   1010 		return err;
   1011 	}
   1012 
   1013 	return USBD_NORMAL_COMPLETION;
   1014 }
   1015 
   1016 
   1017 Static usbd_status
   1018 umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req,
   1019 	 void *buffer, int buflen, int flags, struct usbd_xfer *xfer)
   1020 {
   1021 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1022 	usbd_status err;
   1023 
   1024 	if (sc->sc_dying)
   1025 		return USBD_IOERROR;
   1026 
   1027 	/* Initialiase a USB control transfer and then schedule it */
   1028 
   1029 	usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc, sc->timeout,
   1030 		req, buffer, buflen, flags, sc->sc_methods->wire_state);
   1031 
   1032 	err = usbd_transfer(xfer);
   1033 	if (err && err != USBD_IN_PROGRESS) {
   1034 		DPRINTFM(UDMASS_BBB, "failed to setup ctrl transfer... err=%jd",
   1035 		    err, 0, 0, 0);
   1036 
   1037 		/* do not reset, as this would make us loop */
   1038 		return err;
   1039 	}
   1040 
   1041 	return USBD_NORMAL_COMPLETION;
   1042 }
   1043 
   1044 Static void
   1045 umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
   1046 	struct usbd_xfer *xfer)
   1047 {
   1048 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1049 
   1050 	if (sc->sc_dying) {
   1051 		umass_transfer_done(sc, sc->transfer_datalen,
   1052 		    STATUS_WIRE_FAILED);
   1053 		return;
   1054 	}
   1055 
   1056 	DPRINTFM(UDMASS_BBB, "Clear endpoint 0x%02jx stall",
   1057 	    sc->sc_epaddr[endpt], 0, 0, 0);
   1058 
   1059 	usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]);
   1060 
   1061 	sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT;
   1062 	sc->sc_req.bRequest = UR_CLEAR_FEATURE;
   1063 	USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT);
   1064 	USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]);
   1065 	USETW(sc->sc_req.wLength, 0);
   1066 	if (umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer))
   1067 		umass_transfer_done(sc, sc->transfer_datalen,
   1068 		    STATUS_WIRE_FAILED);
   1069 }
   1070 
   1071 Static void
   1072 umass_transfer_done(struct umass_softc *sc, int residue, int status)
   1073 {
   1074 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1075 
   1076 	sc->transfer_state = TSTATE_IDLE;
   1077 	SDT_PROBE7(usb, umass, transfer, done,
   1078 	    sc,
   1079 	    sc->transfer_cb,
   1080 	    sc->transfer_priv,
   1081 	    sc->transfer_data,
   1082 	    sc->transfer_datalen,
   1083 	    residue,
   1084 	    status);
   1085 	sc->transfer_cb(sc, sc->transfer_priv, residue, status);
   1086 }
   1087 
   1088 Static void
   1089 umass_transfer_reset(struct umass_softc *sc)
   1090 {
   1091 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1092 
   1093 	sc->transfer_state = TSTATE_IDLE;
   1094 	if (sc->transfer_priv) {
   1095 		SDT_PROBE7(usb, umass, transfer, done,
   1096 		    sc,
   1097 		    sc->transfer_cb,
   1098 		    sc->transfer_priv,
   1099 		    sc->transfer_data,
   1100 		    sc->transfer_datalen,
   1101 		    sc->transfer_datalen,
   1102 		    sc->transfer_status);
   1103 		sc->transfer_cb(sc, sc->transfer_priv, sc->transfer_datalen,
   1104 		    sc->transfer_status);
   1105 	}
   1106 }
   1107 
   1108 #if 0
   1109 Static void
   1110 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
   1111 {
   1112 	sc->transfer_cb = cb;
   1113 	sc->transfer_priv = priv;
   1114 
   1115 	/* The reset is a forced reset, so no error (yet) */
   1116 	sc->reset(sc, STATUS_CMD_OK);
   1117 }
   1118 #endif
   1119 
   1120 /*
   1121  * Bulk protocol specific functions
   1122  */
   1123 
   1124 Static void
   1125 umass_bbb_reset(struct umass_softc *sc, int status)
   1126 {
   1127 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1128 	SDT_PROBE2(usb, umass, bbb, reset,  sc, status);
   1129 	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
   1130 		   "sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
   1131 		   sc->sc_wire);
   1132 
   1133 	if (sc->sc_dying) {
   1134 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1135 		return;
   1136 	}
   1137 
   1138 	/*
   1139 	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
   1140 	 *
   1141 	 * For Reset Recovery the host shall issue in the following order:
   1142 	 * a) a Bulk-Only Mass Storage Reset
   1143 	 * b) a Clear Feature HALT to the Bulk-In endpoint
   1144 	 * c) a Clear Feature HALT to the Bulk-Out endpoint
   1145 	 *
   1146 	 * This is done in 3 steps, states:
   1147 	 * TSTATE_BBB_RESET1
   1148 	 * TSTATE_BBB_RESET2
   1149 	 * TSTATE_BBB_RESET3
   1150 	 *
   1151 	 * If the reset doesn't succeed, the device should be port reset.
   1152 	 */
   1153 
   1154 	DPRINTFM(UDMASS_BBB, "Bulk Reset", 0, 0, 0, 0);
   1155 
   1156 	sc->transfer_state = TSTATE_BBB_RESET1;
   1157 	sc->transfer_status = status;
   1158 
   1159 	/* reset is a class specific interface write */
   1160 	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1161 	sc->sc_req.bRequest = UR_BBB_RESET;
   1162 	USETW(sc->sc_req.wValue, 0);
   1163 	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
   1164 	USETW(sc->sc_req.wLength, 0);
   1165 	if (umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0,
   1166 		sc->transfer_xfer[XFER_BBB_RESET1]))
   1167 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1168 }
   1169 
   1170 Static void
   1171 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
   1172 		   void *data, int datalen, int dir, u_int timeout,
   1173 		   int flags, umass_callback cb, void *priv)
   1174 {
   1175 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1176 	SDT_PROBE7(usb, umass, transfer, start__bbb,
   1177 	    sc, cb, priv, data, datalen, dir, timeout);
   1178 	static int dCBWtag = 42;	/* unique for CBW of transfer */
   1179 
   1180 	DPRINTFM(UDMASS_BBB, "sc %#jx cmd=0x%02jx", (uintptr_t)sc,
   1181 	    *(u_char *)cmd, 0, 0);
   1182 
   1183 	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
   1184 		   "sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
   1185 		   sc->sc_wire);
   1186 
   1187 	if (sc->sc_dying) {
   1188 		SDT_PROBE7(usb, umass, transfer, done,
   1189 		    sc, cb, priv, data, datalen, datalen, STATUS_WIRE_FAILED);
   1190 		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
   1191 		return;
   1192 	}
   1193 
   1194 	/* Be a little generous. */
   1195 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
   1196 
   1197 	/*
   1198 	 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
   1199 	 * a data phase of datalen bytes from/to the device and finally a
   1200 	 * csw read phase.
   1201 	 * If the data direction was inbound a maximum of datalen bytes
   1202 	 * is stored in the buffer pointed to by data.
   1203 	 *
   1204 	 * umass_bbb_transfer initialises the transfer and lets the state
   1205 	 * machine in umass_bbb_state handle the completion. It uses the
   1206 	 * following states:
   1207 	 * TSTATE_BBB_COMMAND
   1208 	 *   -> TSTATE_BBB_DATA
   1209 	 *   -> TSTATE_BBB_STATUS
   1210 	 *   -> TSTATE_BBB_STATUS2
   1211 	 *   -> TSTATE_BBB_IDLE
   1212 	 *
   1213 	 * An error in any of those states will invoke
   1214 	 * umass_bbb_reset.
   1215 	 */
   1216 
   1217 	/* check the given arguments */
   1218 	KASSERTMSG(datalen == 0 || data != NULL,
   1219 		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
   1220 	KASSERTMSG(cmdlen <= CBWCDBLENGTH,
   1221 		   "%s: cmdlen exceeds CDB length in CBW (%d > %d)",
   1222 			device_xname(sc->sc_dev), cmdlen, CBWCDBLENGTH);
   1223 	KASSERTMSG(dir == DIR_NONE || datalen > 0,
   1224 		   "%s: datalen == 0 while direction is not NONE\n",
   1225 			device_xname(sc->sc_dev));
   1226 	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
   1227 		   "%s: direction is NONE while datalen is not zero\n",
   1228 			device_xname(sc->sc_dev));
   1229 	/* CTASSERT */
   1230 	KASSERTMSG(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
   1231 		   "%s: CBW struct does not have the right size (%zu vs. %u)\n",
   1232 			device_xname(sc->sc_dev),
   1233 			sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE);
   1234 	/* CTASSERT */
   1235 	KASSERTMSG(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
   1236 		   "%s: CSW struct does not have the right size (%zu vs. %u)\n",
   1237 			device_xname(sc->sc_dev),
   1238 			sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE);
   1239 
   1240 	/*
   1241 	 * Determine the direction of the data transfer and the length.
   1242 	 *
   1243 	 * dCBWDataTransferLength (datalen) :
   1244 	 *   This field indicates the number of bytes of data that the host
   1245 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
   1246 	 *   the Direction bit) during the execution of this command. If this
   1247 	 *   field is set to 0, the device will expect that no data will be
   1248 	 *   transferred IN or OUT during this command, regardless of the value
   1249 	 *   of the Direction bit defined in dCBWFlags.
   1250 	 *
   1251 	 * dCBWFlags (dir) :
   1252 	 *   The bits of the Flags field are defined as follows:
   1253 	 *     Bits 0-6	 reserved
   1254 	 *     Bit  7	 Direction - this bit shall be ignored if the
   1255 	 *			     dCBWDataTransferLength field is zero.
   1256 	 *		 0 = data Out from host to device
   1257 	 *		 1 = data In from device to host
   1258 	 */
   1259 
   1260 	/* Fill in the Command Block Wrapper */
   1261 	USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
   1262 	USETDW(sc->cbw.dCBWTag, dCBWtag);
   1263 	dCBWtag++;	/* cannot be done in macro (it will be done 4 times) */
   1264 	USETDW(sc->cbw.dCBWDataTransferLength, datalen);
   1265 	/* DIR_NONE is treated as DIR_OUT (0x00) */
   1266 	sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
   1267 	sc->cbw.bCBWLUN = lun;
   1268 	sc->cbw.bCDBLength = cmdlen;
   1269 	memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
   1270 
   1271 	DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
   1272 
   1273 	/* store the details for the data transfer phase */
   1274 	sc->transfer_dir = dir;
   1275 	sc->transfer_data = data;
   1276 	sc->transfer_datalen = datalen;
   1277 	sc->transfer_actlen = 0;
   1278 	sc->transfer_cb = cb;
   1279 	sc->transfer_priv = priv;
   1280 	sc->transfer_status = STATUS_CMD_OK;
   1281 
   1282 	/* move from idle to the command state */
   1283 	sc->transfer_state = TSTATE_BBB_COMMAND;
   1284 
   1285 	/* Send the CBW from host to device via bulk-out endpoint. */
   1286 	if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
   1287 			&sc->cbw, UMASS_BBB_CBW_SIZE, flags,
   1288 			sc->transfer_xfer[XFER_BBB_CBW])) {
   1289 		umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1290 	}
   1291 }
   1292 
   1293 
   1294 Static void
   1295 umass_bbb_state(struct usbd_xfer *xfer, void *priv,
   1296 		usbd_status err)
   1297 {
   1298 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1299 	struct umass_softc *sc = (struct umass_softc *) priv;
   1300 	struct usbd_xfer *next_xfer;
   1301 	int residue;
   1302 
   1303 	SDT_PROBE3(usb, umass, bbb, state,  sc, xfer, err);
   1304 
   1305 	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
   1306 		   "sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
   1307 		   sc->sc_wire);
   1308 
   1309 	/*
   1310 	 * State handling for BBB transfers.
   1311 	 *
   1312 	 * The subroutine is rather long. It steps through the states given in
   1313 	 * Annex A of the Bulk-Only specification.
   1314 	 * Each state first does the error handling of the previous transfer
   1315 	 * and then prepares the next transfer.
   1316 	 * Each transfer is done asynchroneously so after the request/transfer
   1317 	 * has been submitted you will find a 'return;'.
   1318 	 */
   1319 
   1320 	DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx, transfer_state %jd dir %jd",
   1321 	    (uintptr_t)sc, (uintptr_t)xfer, sc->transfer_state,
   1322 	    sc->transfer_dir);
   1323 
   1324 	if (err == USBD_CANCELLED) {
   1325 		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
   1326 		    (uintptr_t)sc, (uintptr_t)xfer, 0, 0);
   1327 
   1328 		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
   1329 		return;
   1330 	}
   1331 
   1332 	if (sc->sc_dying) {
   1333 		umass_transfer_done(sc, sc->transfer_datalen,
   1334 		    STATUS_WIRE_FAILED);
   1335 		return;
   1336 	}
   1337 
   1338 	switch (sc->transfer_state) {
   1339 
   1340 	/***** Bulk Transfer *****/
   1341 	case TSTATE_BBB_COMMAND:
   1342 		/* Command transport phase, error handling */
   1343 		if (err) {
   1344 			DPRINTFM(UDMASS_BBB, "sc %#jx failed to send CBW",
   1345 			    (uintptr_t)sc, 0, 0, 0);
   1346 			/* If the device detects that the CBW is invalid, then
   1347 			 * the device may STALL both bulk endpoints and require
   1348 			 * a Bulk-Reset
   1349 			 */
   1350 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1351 			return;
   1352 		}
   1353 
   1354 		/* Data transport phase, setup transfer */
   1355 		sc->transfer_state = TSTATE_BBB_DATA;
   1356 		if (sc->transfer_dir == DIR_IN) {
   1357 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1358 					sc->datain_buffer, sc->transfer_datalen,
   1359 					USBD_SHORT_XFER_OK,
   1360 					sc->transfer_xfer[XFER_BBB_DATAIN]))
   1361 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1362 
   1363 			return;
   1364 		} else if (sc->transfer_dir == DIR_OUT) {
   1365 			memcpy(sc->dataout_buffer, sc->transfer_data,
   1366 			       sc->transfer_datalen);
   1367 			if (umass_setup_transfer(sc,
   1368 			    sc->sc_pipe[UMASS_BULKOUT], sc->dataout_buffer,
   1369 			    sc->transfer_datalen, 0,/* fixed length transfer */
   1370 			    sc->transfer_xfer[XFER_BBB_DATAOUT]))
   1371 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1372 
   1373 			return;
   1374 		} else {
   1375 			DPRINTFM(UDMASS_BBB, "sc %#jx: no data phase",
   1376 			    (uintptr_t)sc, 0, 0, 0);
   1377 		}
   1378 
   1379 		/* if no data phase, err == 0 */
   1380 		/* FALLTHROUGH */
   1381 	case TSTATE_BBB_DATA:
   1382 		/* Command transport phase error handling (ignored if no data
   1383 		 * phase (fallthrough from previous state)) */
   1384 		if (sc->transfer_dir != DIR_NONE) {
   1385 			/* retrieve the length of the transfer that was done */
   1386 			usbd_get_xfer_status(xfer, NULL, NULL,
   1387 			     &sc->transfer_actlen, NULL);
   1388 			DPRINTFM(UDMASS_BBB, "sc %#jx: BBB_DATA actlen=%jd",
   1389 			    (uintptr_t)sc, sc->transfer_actlen, 0, 0);
   1390 
   1391 			if (err) {
   1392 				DPRINTFM(UDMASS_BBB, "sc %#jx Data dir %jd "
   1393 				    "err %jd failed, err %jd",
   1394 				    (uintptr_t)sc, sc->transfer_dir,
   1395 				    sc->transfer_datalen, err);
   1396 
   1397 				if (err == USBD_STALLED) {
   1398 					sc->transfer_state = TSTATE_BBB_DCLEAR;
   1399 					umass_clear_endpoint_stall(sc,
   1400 					  (sc->transfer_dir == DIR_IN?
   1401 					    UMASS_BULKIN:UMASS_BULKOUT),
   1402 					  sc->transfer_xfer[XFER_BBB_DCLEAR]);
   1403 				} else {
   1404 					/* Unless the error is a pipe stall the
   1405 					 * error is fatal.
   1406 					 */
   1407 					umass_bbb_reset(sc,STATUS_WIRE_FAILED);
   1408 				}
   1409 				return;
   1410 			}
   1411 		}
   1412 
   1413 		/* err == 0 (no data phase or successful) */
   1414 		/* FALLTHROUGH */
   1415 	case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
   1416 		if (sc->transfer_dir == DIR_IN)
   1417 			memcpy(sc->transfer_data, sc->datain_buffer,
   1418 			       sc->transfer_actlen);
   1419 
   1420 		DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
   1421 					umass_dump_buffer(sc, sc->transfer_data,
   1422 						sc->transfer_datalen, 48));
   1423 
   1424 		/* err == 0 (no data phase or successful) */
   1425 		/* FALLTHROUGH */
   1426 	case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
   1427 		/* Reading of CSW after bulk stall condition in data phase
   1428 		 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
   1429 		 * reading CSW (TSTATE_BBB_SCLEAR).
   1430 		 * In the case of no data phase or successful data phase,
   1431 		 * err == 0 and the following if block is passed.
   1432 		 */
   1433 		if (err) {	/* should not occur */
   1434 			printf("%s: BBB bulk-%s stall clear failed, %s\n",
   1435 			    device_xname(sc->sc_dev),
   1436 			    (sc->transfer_dir == DIR_IN? "in":"out"),
   1437 			    usbd_errstr(err));
   1438 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1439 			return;
   1440 		}
   1441 
   1442 		/* Status transport phase, setup transfer */
   1443 		if (sc->transfer_state == TSTATE_BBB_COMMAND ||
   1444 		    sc->transfer_state == TSTATE_BBB_DATA ||
   1445 		    sc->transfer_state == TSTATE_BBB_DCLEAR) {
   1446 			/* After no data phase, successful data phase and
   1447 			 * after clearing bulk-in/-out stall condition
   1448 			 */
   1449 			sc->transfer_state = TSTATE_BBB_STATUS1;
   1450 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
   1451 		} else {
   1452 			/* After first attempt of fetching CSW */
   1453 			sc->transfer_state = TSTATE_BBB_STATUS2;
   1454 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
   1455 		}
   1456 
   1457 		/* Read the Command Status Wrapper via bulk-in endpoint. */
   1458 		if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1459 			&sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
   1460 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1461 			return;
   1462 		}
   1463 
   1464 		return;
   1465 	case TSTATE_BBB_STATUS1:	/* first attempt */
   1466 	case TSTATE_BBB_STATUS2:	/* second attempt */
   1467 		/* Status transfer, error handling */
   1468 		if (err) {
   1469 			DPRINTFM(UDMASS_BBB, "sc %#jx Failed to read CSW "
   1470 			    "err %jd (state %jd)", (uintptr_t)sc, err,
   1471 			    sc->transfer_state, 0);
   1472 
   1473 			/* If this was the first attempt at fetching the CSW
   1474 			 * retry it, otherwise fail.
   1475 			 */
   1476 			if (sc->transfer_state == TSTATE_BBB_STATUS1) {
   1477 				sc->transfer_state = TSTATE_BBB_SCLEAR;
   1478 				umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   1479 				    sc->transfer_xfer[XFER_BBB_SCLEAR]);
   1480 				return;
   1481 			} else {
   1482 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1483 				return;
   1484 			}
   1485 		}
   1486 
   1487 		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
   1488 
   1489 #ifdef UMASS_DEBUG
   1490 		residue = UGETDW(sc->csw.dCSWDataResidue);
   1491 		if (residue != sc->transfer_datalen - sc->transfer_actlen)
   1492 			printf("%s: dCSWDataResidue=%d req=%d act=%d\n",
   1493 			       device_xname(sc->sc_dev), residue,
   1494 			       sc->transfer_datalen, sc->transfer_actlen);
   1495 #endif
   1496 		residue = sc->transfer_datalen - sc->transfer_actlen;
   1497 
   1498 		/* Translate weird command-status signatures. */
   1499 		if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
   1500 		    UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
   1501 			USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
   1502 
   1503 		/* Translate invalid command-status tags */
   1504 		if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
   1505 			USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
   1506 
   1507 		/* Check CSW and handle any error */
   1508 		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
   1509 			/* Invalid CSW: Wrong signature or wrong tag might
   1510 			 * indicate that the device is confused -> reset it.
   1511 			 */
   1512 			printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
   1513 				device_xname(sc->sc_dev),
   1514 				UGETDW(sc->csw.dCSWSignature),
   1515 				CSWSIGNATURE);
   1516 
   1517 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1518 			return;
   1519 		} else if (UGETDW(sc->csw.dCSWTag)
   1520 				!= UGETDW(sc->cbw.dCBWTag)) {
   1521 			printf("%s: Invalid CSW: tag %d should be %d\n",
   1522 				device_xname(sc->sc_dev),
   1523 				UGETDW(sc->csw.dCSWTag),
   1524 				UGETDW(sc->cbw.dCBWTag));
   1525 
   1526 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1527 			return;
   1528 
   1529 		/* CSW is valid here */
   1530 		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
   1531 			printf("%s: Invalid CSW: status %d > %d\n",
   1532 				device_xname(sc->sc_dev),
   1533 				sc->csw.bCSWStatus,
   1534 				CSWSTATUS_PHASE);
   1535 
   1536 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1537 			return;
   1538 		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
   1539 			printf("%s: Phase Error, residue = %d\n",
   1540 				device_xname(sc->sc_dev), residue);
   1541 
   1542 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1543 			return;
   1544 
   1545 		} else if (sc->transfer_actlen > sc->transfer_datalen) {
   1546 			/* Buffer overrun! Don't let this go by unnoticed */
   1547 			panic("%s: transferred %s %d bytes instead of %d bytes",
   1548 			    device_xname(sc->sc_dev),
   1549 			    sc->transfer_dir == DIR_IN ? "IN" : "OUT",
   1550 			    sc->transfer_actlen, sc->transfer_datalen);
   1551 #if 0
   1552 		} else if (sc->transfer_datalen - sc->transfer_actlen
   1553 			   != residue) {
   1554 			DPRINTFM(UDMASS_BBB, "sc %#jx: actlen=%jd != "
   1555 			    "residue=%jd\n", (uintptr_t)sc,
   1556 			    sc->transfer_datalen - sc->transfer_actlen,
   1557 			    residue, 0);
   1558 
   1559 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1560 			return;
   1561 #endif
   1562 		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
   1563 			DPRINTFM(UDMASS_BBB, "sc %#jx: Command Failed, "
   1564 			    "res = %jd", (uintptr_t)sc, residue, 0, 0);
   1565 
   1566 			/* SCSI command failed but transfer was succesful */
   1567 			umass_transfer_done(sc, residue, STATUS_CMD_FAILED);
   1568 			return;
   1569 
   1570 		} else {	/* success */
   1571 			umass_transfer_done(sc, residue, STATUS_CMD_OK);
   1572 			return;
   1573 		}
   1574 
   1575 	/***** Bulk Reset *****/
   1576 	case TSTATE_BBB_RESET1:
   1577 		if (err)
   1578 			printf("%s: BBB reset failed, %s\n",
   1579 				device_xname(sc->sc_dev), usbd_errstr(err));
   1580 
   1581 		sc->transfer_state = TSTATE_BBB_RESET2;
   1582 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   1583 			sc->transfer_xfer[XFER_BBB_RESET2]);
   1584 
   1585 		return;
   1586 	case TSTATE_BBB_RESET2:
   1587 		if (err)	/* should not occur */
   1588 			printf("%s: BBB bulk-in clear stall failed, %s\n",
   1589 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1590 			/* no error recovery, otherwise we end up in a loop */
   1591 
   1592 		sc->transfer_state = TSTATE_BBB_RESET3;
   1593 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
   1594 			sc->transfer_xfer[XFER_BBB_RESET3]);
   1595 
   1596 		return;
   1597 	case TSTATE_BBB_RESET3:
   1598 		if (err)	/* should not occur */
   1599 			printf("%s: BBB bulk-out clear stall failed, %s\n",
   1600 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1601 			/* no error recovery, otherwise we end up in a loop */
   1602 
   1603 		umass_transfer_reset(sc);
   1604 
   1605 		return;
   1606 
   1607 	/***** Default *****/
   1608 	default:
   1609 		panic("%s: Unknown state %d",
   1610 		      device_xname(sc->sc_dev), sc->transfer_state);
   1611 	}
   1612 }
   1613 
   1614 /*
   1615  * Command/Bulk/Interrupt (CBI) specific functions
   1616  */
   1617 
   1618 Static int
   1619 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, int flags,
   1620 	       struct usbd_xfer *xfer)
   1621 {
   1622 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1623 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
   1624 		   sc->sc_wire);
   1625 
   1626 	if ((sc->sc_cmd == UMASS_CPROTO_RBC) &&
   1627 	    (sc->sc_quirks & UMASS_QUIRK_RBC_PAD_TO_12) != 0 && buflen < 12) {
   1628 		(void)memset(buffer + buflen, 0, 12 - buflen);
   1629 		buflen = 12;
   1630 	}
   1631 
   1632 	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1633 	sc->sc_req.bRequest = UR_CBI_ADSC;
   1634 	USETW(sc->sc_req.wValue, 0);
   1635 	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
   1636 	USETW(sc->sc_req.wLength, buflen);
   1637 	return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
   1638 					 buflen, flags, xfer);
   1639 }
   1640 
   1641 
   1642 Static void
   1643 umass_cbi_reset(struct umass_softc *sc, int status)
   1644 {
   1645 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1646 	SDT_PROBE2(usb, umass, bbb, reset,  sc, status);
   1647 	int i;
   1648 #	define SEND_DIAGNOSTIC_CMDLEN	12
   1649 
   1650 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1651 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
   1652 		   sc->sc_wire);
   1653 
   1654 	if (sc->sc_dying) {
   1655 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1656 		return;
   1657 	}
   1658 
   1659 	/*
   1660 	 * Command Block Reset Protocol
   1661 	 *
   1662 	 * First send a reset request to the device. Then clear
   1663 	 * any possibly stalled bulk endpoints.
   1664 
   1665 	 * This is done in 3 steps, states:
   1666 	 * TSTATE_CBI_RESET1
   1667 	 * TSTATE_CBI_RESET2
   1668 	 * TSTATE_CBI_RESET3
   1669 	 *
   1670 	 * If the reset doesn't succeed, the device should be port reset.
   1671 	 */
   1672 
   1673 	DPRINTFM(UDMASS_CBI, "sc %#jx: CBI Reset", (uintptr_t)sc, 0, 0, 0);
   1674 
   1675 	/* CTASSERT */
   1676 	KASSERTMSG(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
   1677 		   "%s: CBL struct is too small (%zu < %u)\n",
   1678 			device_xname(sc->sc_dev),
   1679 			sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN);
   1680 
   1681 	sc->transfer_state = TSTATE_CBI_RESET1;
   1682 	sc->transfer_status = status;
   1683 
   1684 	/* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
   1685 	 * the two the last 10 bytes of the cbl is filled with 0xff (section
   1686 	 * 2.2 of the CBI spec).
   1687 	 */
   1688 	sc->cbl[0] = 0x1d;	/* Command Block Reset */
   1689 	sc->cbl[1] = 0x04;
   1690 	for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
   1691 		sc->cbl[i] = 0xff;
   1692 
   1693 	if (umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 0,
   1694 		sc->transfer_xfer[XFER_CBI_RESET1]))
   1695 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1696 	/* XXX if the command fails we should reset the port on the bub */
   1697 }
   1698 
   1699 Static void
   1700 umass_cbi_transfer(struct umass_softc *sc, int lun,
   1701 		   void *cmd, int cmdlen, void *data, int datalen, int dir,
   1702 		   u_int timeout, int flags, umass_callback cb, void *priv)
   1703 {
   1704 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1705 	SDT_PROBE7(usb, umass, transfer, start__cbi,
   1706 	    sc, cb, priv, data, datalen, dir, timeout);
   1707 
   1708 	DPRINTFM(UDMASS_CBI, "sc %#jx: cmd=0x%02jx, len=%jd",
   1709 	     (uintptr_t)sc, *(u_char *)cmd, datalen, 0);
   1710 
   1711 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1712 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
   1713 		   sc->sc_wire);
   1714 
   1715 	if (sc->sc_dying) {
   1716 		SDT_PROBE7(usb, umass, transfer, done,
   1717 		    sc, cb, priv, data, datalen, datalen, STATUS_WIRE_FAILED);
   1718 		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
   1719 		return;
   1720 	}
   1721 
   1722 	/* Be a little generous. */
   1723 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
   1724 
   1725 	/*
   1726 	 * Do a CBI transfer with cmdlen bytes from cmd, possibly
   1727 	 * a data phase of datalen bytes from/to the device and finally a
   1728 	 * csw read phase.
   1729 	 * If the data direction was inbound a maximum of datalen bytes
   1730 	 * is stored in the buffer pointed to by data.
   1731 	 *
   1732 	 * umass_cbi_transfer initialises the transfer and lets the state
   1733 	 * machine in umass_cbi_state handle the completion. It uses the
   1734 	 * following states:
   1735 	 * TSTATE_CBI_COMMAND
   1736 	 *   -> XXX fill in
   1737 	 *
   1738 	 * An error in any of those states will invoke
   1739 	 * umass_cbi_reset.
   1740 	 */
   1741 
   1742 	/* check the given arguments */
   1743 	KASSERTMSG(datalen == 0 || data != NULL,
   1744 		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
   1745 	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
   1746 		   "%s: direction is NONE while datalen is not zero\n",
   1747 			device_xname(sc->sc_dev));
   1748 
   1749 	/* store the details for the data transfer phase */
   1750 	sc->transfer_dir = dir;
   1751 	sc->transfer_data = data;
   1752 	sc->transfer_datalen = datalen;
   1753 	sc->transfer_actlen = 0;
   1754 	sc->transfer_cb = cb;
   1755 	sc->transfer_priv = priv;
   1756 	sc->transfer_status = STATUS_CMD_OK;
   1757 
   1758 	/* move from idle to the command state */
   1759 	sc->transfer_state = TSTATE_CBI_COMMAND;
   1760 
   1761 	/* Send the Command Block from host to device via control endpoint. */
   1762 	if (umass_cbi_adsc(sc, cmd, cmdlen, flags,
   1763 	    sc->transfer_xfer[XFER_CBI_CB]))
   1764 		umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1765 }
   1766 
   1767 Static void
   1768 umass_cbi_state(struct usbd_xfer *xfer, void *priv,
   1769 		usbd_status err)
   1770 {
   1771 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1772 	struct umass_softc *sc = (struct umass_softc *) priv;
   1773 
   1774 	SDT_PROBE3(usb, umass, bbb, state,  sc, xfer, err);
   1775 
   1776 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1777 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
   1778 		   sc->sc_wire);
   1779 
   1780 	if (err == USBD_CANCELLED) {
   1781 		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
   1782 			(uintptr_t)sc, (uintptr_t)xfer, 0, 0);
   1783 		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
   1784 		return;
   1785 	}
   1786 
   1787 	if (sc->sc_dying) {
   1788 		umass_transfer_done(sc, sc->transfer_datalen,
   1789 		    STATUS_WIRE_FAILED);
   1790 		return;
   1791 	}
   1792 
   1793 	/*
   1794 	 * State handling for CBI transfers.
   1795 	 */
   1796 
   1797 	DPRINTFM(UDMASS_CBI, "sc %#jx: Handling CBI state %jd, xfer=%#jx, ...",
   1798 	    (uintptr_t)sc, sc->transfer_state, (uintptr_t)xfer, 0);
   1799 	DPRINTFM(UDMASS_CBI, "... err %jd", err, 0, 0, 0);
   1800 
   1801 	switch (sc->transfer_state) {
   1802 
   1803 	/***** CBI Transfer *****/
   1804 	case TSTATE_CBI_COMMAND:
   1805 		if (err == USBD_STALLED) {
   1806 			DPRINTFM(UDMASS_CBI, "sc %#jx: Command Transport "
   1807 			    "failed", (uintptr_t)sc, 0, 0, 0);
   1808 			/* Status transport by control pipe (section 2.3.2.1).
   1809 			 * The command contained in the command block failed.
   1810 			 *
   1811 			 * The control pipe has already been unstalled by the
   1812 			 * USB stack.
   1813 			 * Section 2.4.3.1.1 states that the bulk in endpoints
   1814 			 * should not stalled at this point.
   1815 			 */
   1816 			umass_transfer_done(sc, sc->transfer_datalen,
   1817 			    STATUS_CMD_FAILED);
   1818 			return;
   1819 		} else if (err) {
   1820 			DPRINTFM(UDMASS_CBI, "sc %#jx: failed to send ADSC",
   1821 			    (uintptr_t)sc, 0, 0, 0);
   1822 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1823 			return;
   1824 		}
   1825 
   1826 		/* Data transport phase, setup transfer */
   1827 		sc->transfer_state = TSTATE_CBI_DATA;
   1828 		if (sc->transfer_dir == DIR_IN) {
   1829 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1830 			    sc->datain_buffer, sc->transfer_datalen,
   1831 			    USBD_SHORT_XFER_OK,
   1832 			    sc->transfer_xfer[XFER_CBI_DATAIN]))
   1833 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1834 
   1835 			return;
   1836 		} else if (sc->transfer_dir == DIR_OUT) {
   1837 			memcpy(sc->dataout_buffer, sc->transfer_data,
   1838 			       sc->transfer_datalen);
   1839 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
   1840 			    sc->dataout_buffer, sc->transfer_datalen,
   1841 			    0, /* fixed length transfer */
   1842 			    sc->transfer_xfer[XFER_CBI_DATAOUT]))
   1843 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1844 
   1845 			return;
   1846 		} else {
   1847 			DPRINTFM(UDMASS_CBI, "sc %#jx: no data phase",
   1848 			    (uintptr_t)sc, 0, 0, 0);
   1849 		}
   1850 
   1851 		/* if no data phase, err == 0 */
   1852 		/* FALLTHROUGH */
   1853 	case TSTATE_CBI_DATA:
   1854 		/* Command transport phase error handling (ignored if no data
   1855 		 * phase (fallthrough from previous state)) */
   1856 		if (sc->transfer_dir != DIR_NONE) {
   1857 			/* retrieve the length of the transfer that was done */
   1858 			usbd_get_xfer_status(xfer, NULL, NULL,
   1859 			    &sc->transfer_actlen, NULL);
   1860 			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_DATA actlen=%jd",
   1861 				(uintptr_t)sc, sc->transfer_actlen, 0, 0);
   1862 
   1863 			if (err) {
   1864 				DPRINTFM(UDMASS_CBI, "sc %#jx: Data dir %jd "
   1865 				    "err %jd failed",
   1866 				    (uintptr_t)sc, sc->transfer_dir,
   1867 				    sc->transfer_datalen, err);
   1868 
   1869 				if (err == USBD_STALLED) {
   1870 					sc->transfer_state = TSTATE_CBI_DCLEAR;
   1871 					umass_clear_endpoint_stall(sc,
   1872 					  (sc->transfer_dir == DIR_IN?
   1873 					    UMASS_BULKIN:UMASS_BULKOUT),
   1874 					sc->transfer_xfer[XFER_CBI_DCLEAR]);
   1875 				} else {
   1876 					/* Unless the error is a pipe stall the
   1877 					 * error is fatal.
   1878 					 */
   1879 					umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1880 				}
   1881 				return;
   1882 			}
   1883 		}
   1884 
   1885 		if (sc->transfer_dir == DIR_IN)
   1886 			memcpy(sc->transfer_data, sc->datain_buffer,
   1887 			       sc->transfer_actlen);
   1888 
   1889 		DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
   1890 					umass_dump_buffer(sc, sc->transfer_data,
   1891 						sc->transfer_actlen, 48));
   1892 
   1893 		/* Status phase */
   1894 		if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
   1895 			sc->transfer_state = TSTATE_CBI_STATUS;
   1896 			memset(&sc->sbl, 0, sizeof(sc->sbl));
   1897 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
   1898 				    &sc->sbl, sizeof(sc->sbl),
   1899 				    0,	/* fixed length transfer */
   1900 				    sc->transfer_xfer[XFER_CBI_STATUS]))
   1901 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1902 		} else {
   1903 			/* No command completion interrupt. Request
   1904 			 * sense to get status of command.
   1905 			 */
   1906 			umass_transfer_done(sc,
   1907 			    sc->transfer_datalen - sc->transfer_actlen,
   1908 			    STATUS_CMD_UNKNOWN);
   1909 		}
   1910 		return;
   1911 
   1912 	case TSTATE_CBI_STATUS:
   1913 		if (err) {
   1914 			DPRINTFM(UDMASS_CBI, "sc %#jx: Status Transport failed",
   1915 			    (uintptr_t)sc, 0, 0, 0);
   1916 			/* Status transport by interrupt pipe (section 2.3.2.2).
   1917 			 */
   1918 
   1919 			if (err == USBD_STALLED) {
   1920 				sc->transfer_state = TSTATE_CBI_SCLEAR;
   1921 				umass_clear_endpoint_stall(sc, UMASS_INTRIN,
   1922 					sc->transfer_xfer[XFER_CBI_SCLEAR]);
   1923 			} else {
   1924 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1925 			}
   1926 			return;
   1927 		}
   1928 
   1929 		/* Dissect the information in the buffer */
   1930 
   1931 		{
   1932 			uint32_t actlen;
   1933 			usbd_get_xfer_status(xfer,NULL,NULL,&actlen,NULL);
   1934 			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_STATUS actlen=%jd",
   1935 			    (uintptr_t)sc, actlen, 0, 0);
   1936 			if (actlen != 2)
   1937 				break;
   1938 		}
   1939 
   1940 		if (sc->sc_cmd == UMASS_CPROTO_UFI) {
   1941 			int status;
   1942 
   1943 			/* Section 3.4.3.1.3 specifies that the UFI command
   1944 			 * protocol returns an ASC and ASCQ in the interrupt
   1945 			 * data block.
   1946 			 */
   1947 
   1948 			DPRINTFM(UDMASS_CBI, "sc %#jx: UFI CCI, ASC = 0x%02jx, "
   1949 			    "ASCQ = 0x%02jx", (uintptr_t)sc, sc->sbl.ufi.asc,
   1950 			    sc->sbl.ufi.ascq, 0);
   1951 
   1952 			if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
   1953 			    sc->sc_sense)
   1954 				status = STATUS_CMD_OK;
   1955 			else
   1956 				status = STATUS_CMD_FAILED;
   1957 
   1958 			/* No autosense, command successful */
   1959 			umass_transfer_done(sc,
   1960 			    sc->transfer_datalen - sc->transfer_actlen,
   1961 			    status);
   1962 		} else {
   1963 			int status;
   1964 
   1965 			/* Command Interrupt Data Block */
   1966 
   1967 			DPRINTFM(UDMASS_CBI, "sc %#jx: type=0x%02jx, "
   1968 			    "value=0x%02jx", (uintptr_t)sc,
   1969 			    sc->sbl.common.type, sc->sbl.common.value, 0);
   1970 
   1971 			if (sc->sbl.common.type == IDB_TYPE_CCI) {
   1972 				switch (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) {
   1973 				case IDB_VALUE_PASS:
   1974 					status = STATUS_CMD_OK;
   1975 					break;
   1976 				case IDB_VALUE_FAIL:
   1977 				case IDB_VALUE_PERSISTENT:
   1978 					status = STATUS_CMD_FAILED;
   1979 					break;
   1980 				case IDB_VALUE_PHASE:
   1981 				default: /* XXX: gcc */
   1982 					status = STATUS_WIRE_FAILED;
   1983 					break;
   1984 				}
   1985 
   1986 				umass_transfer_done(sc,
   1987 				    sc->transfer_datalen - sc->transfer_actlen,
   1988 				    status);
   1989 			} else {
   1990 				/* XXX What to do?  */
   1991 				umass_transfer_done(sc, sc->transfer_datalen,
   1992 				    STATUS_WIRE_FAILED);
   1993 			}
   1994 		}
   1995 		return;
   1996 
   1997 	case TSTATE_CBI_DCLEAR:
   1998 		if (err) {	/* should not occur */
   1999 			printf("%s: CBI bulk-%s stall clear failed, %s\n",
   2000 			    device_xname(sc->sc_dev),
   2001 			    (sc->transfer_dir == DIR_IN? "in":"out"),
   2002 			    usbd_errstr(err));
   2003 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2004 		} else {
   2005 			umass_transfer_done(sc,
   2006 			    sc->transfer_datalen, STATUS_CMD_FAILED);
   2007 		}
   2008 		return;
   2009 
   2010 	case TSTATE_CBI_SCLEAR:
   2011 		if (err) {	/* should not occur */
   2012 			printf("%s: CBI intr-in stall clear failed, %s\n",
   2013 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2014 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2015 		} else {
   2016 			umass_transfer_done(sc,
   2017 			    sc->transfer_datalen, STATUS_CMD_FAILED);
   2018 		}
   2019 		return;
   2020 
   2021 	/***** CBI Reset *****/
   2022 	case TSTATE_CBI_RESET1:
   2023 		if (err)
   2024 			printf("%s: CBI reset failed, %s\n",
   2025 				device_xname(sc->sc_dev), usbd_errstr(err));
   2026 
   2027 		sc->transfer_state = TSTATE_CBI_RESET2;
   2028 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   2029 			sc->transfer_xfer[XFER_CBI_RESET2]);
   2030 
   2031 		return;
   2032 	case TSTATE_CBI_RESET2:
   2033 		if (err)	/* should not occur */
   2034 			printf("%s: CBI bulk-in stall clear failed, %s\n",
   2035 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2036 			/* no error recovery, otherwise we end up in a loop */
   2037 
   2038 		sc->transfer_state = TSTATE_CBI_RESET3;
   2039 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
   2040 			sc->transfer_xfer[XFER_CBI_RESET3]);
   2041 
   2042 		return;
   2043 	case TSTATE_CBI_RESET3:
   2044 		if (err)	/* should not occur */
   2045 			printf("%s: CBI bulk-out stall clear failed, %s\n",
   2046 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2047 			/* no error recovery, otherwise we end up in a loop */
   2048 
   2049 		umass_transfer_reset(sc);
   2050 		return;
   2051 
   2052 
   2053 	/***** Default *****/
   2054 	default:
   2055 		panic("%s: Unknown state %d",
   2056 		      device_xname(sc->sc_dev), sc->transfer_state);
   2057 	}
   2058 }
   2059 
   2060 static usbd_status
   2061 umass_bbb_get_max_lun(struct umass_softc *sc, uint8_t *maxlun)
   2062 {
   2063 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2064 	usb_device_request_t req;
   2065 	usbd_status err;
   2066 
   2067 	*maxlun = 0;		/* Default to 0. */
   2068 
   2069 	DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun", (uintptr_t)sc, 0, 0, 0);
   2070 
   2071 	/* The Get Max Lun command is a class-specific request. */
   2072 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
   2073 	req.bRequest = UR_BBB_GET_MAX_LUN;
   2074 	USETW(req.wValue, 0);
   2075 	USETW(req.wIndex, sc->sc_ifaceno);
   2076 	USETW(req.wLength, 1);
   2077 
   2078 	err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
   2079 	    USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
   2080 	switch (err) {
   2081 	case USBD_NORMAL_COMPLETION:
   2082 		DPRINTFM(UDMASS_BBB, "sc %#jx: Max Lun %jd",
   2083 		    (uintptr_t)sc, *maxlun , 0, 0);
   2084 		break;
   2085 
   2086 	case USBD_STALLED:
   2087 		/*
   2088 		 * Device doesn't support Get Max Lun request.
   2089 		 */
   2090 		err = USBD_NORMAL_COMPLETION;
   2091 		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun not supported",
   2092 		    (uintptr_t)sc, 0, 0, 0);
   2093 		break;
   2094 
   2095 	case USBD_SHORT_XFER:
   2096 		/*
   2097 		 * XXX This must mean Get Max Lun is not supported, too!
   2098 		 */
   2099 		err = USBD_NORMAL_COMPLETION;
   2100 		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun SHORT_XFER",
   2101 		    (uintptr_t)sc, 0, 0, 0);
   2102 		break;
   2103 
   2104 	default:
   2105 		printf("%s: Get Max Lun failed: %s\n",
   2106 		    device_xname(sc->sc_dev), usbd_errstr(err));
   2107 		/* XXX Should we port_reset the device? */
   2108 		break;
   2109 	}
   2110 
   2111 	return err;
   2112 }
   2113 
   2114 
   2115 
   2116 
   2117 #ifdef UMASS_DEBUG
   2118 Static void
   2119 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
   2120 {
   2121 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2122 	int clen = cbw->bCDBLength;
   2123 	int dlen = UGETDW(cbw->dCBWDataTransferLength);
   2124 	uint8_t *c = cbw->CBWCDB;
   2125 	int tag = UGETDW(cbw->dCBWTag);
   2126 	int flags = cbw->bCBWFlags;
   2127 
   2128 	DPRINTFM(UDMASS_BBB, "sc %#jx: CBW %jd: cmdlen=%jd",
   2129 	    (uintptr_t)sc, tag, clen, 0);
   2130 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
   2131 	    c[0], c[1], c[2], c[3]);
   2132 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
   2133 	    c[4], c[5], c[6], c[7]);
   2134 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx...", c[8], c[9], 0, 0);
   2135 	DPRINTFM(UDMASS_BBB, "  data = %jd bytes, flags = %jx", dlen, flags, 0,
   2136 	    0);
   2137 }
   2138 
   2139 Static void
   2140 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
   2141 {
   2142 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2143 	int sig = UGETDW(csw->dCSWSignature);
   2144 	int tag = UGETDW(csw->dCSWTag);
   2145 	int res = UGETDW(csw->dCSWDataResidue);
   2146 	int status = csw->bCSWStatus;
   2147 
   2148 	DPRINTFM(UDMASS_BBB, "sc %#jx: CSW %jd: sig = 0x%08jx, tag = %jd",
   2149 	    (uintptr_t)sc, (uintptr_t)csw, sig, tag);
   2150 	DPRINTFM(UDMASS_BBB, "  res = %jd, status = 0x%02jx",
   2151 	    res, status, 0, 0);
   2152 }
   2153 
   2154 Static void
   2155 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, int buflen,
   2156 		  int printlen)
   2157 {
   2158 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2159 	int i;
   2160 
   2161 	DPRINTFM(UDMASS_GEN, "sc %#jx: buffer %#jx", (uintptr_t)sc,
   2162 	    (uintptr_t)buffer, 0, 0);
   2163 	for (i = 0; i < buflen && i < printlen;) {
   2164 		if (i + 3 < buflen && i + 3 < printlen) {
   2165 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx%02jx",
   2166 			    buffer[i], buffer[i + 1],
   2167 			    buffer[i + 2], buffer[i + 3]);
   2168 			i += 4;
   2169 		} else if (i + 2 < buflen && i + 2 < printlen) {
   2170 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx",
   2171 			    buffer[i], buffer[i + 1], buffer[i + 2], 0);
   2172 			i += 3;
   2173 		} else if (i + 1 < buflen && i + 2 < printlen) {
   2174 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx",
   2175 			    buffer[i], buffer[i + 1], 0, 0);
   2176 			i += 2;
   2177 		} else {
   2178 			DPRINTFM(UDMASS_GEN, "   0x%02jx", buffer[i], 0, 0, 0);
   2179 			i += 1;
   2180 		}
   2181 	}
   2182 }
   2183 #endif
   2184