Home | History | Annotate | Line # | Download | only in usb
umass.c revision 1.185
      1 /*	$NetBSD: umass.c,v 1.185 2021/05/23 08:42:47 riastradh 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.185 2021/05/23 08:42:47 riastradh 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 	KASSERT(cb);
   1181 	DPRINTFM(UDMASS_BBB, "sc %#jx cmd=0x%02jx", (uintptr_t)sc,
   1182 	    *(u_char *)cmd, 0, 0);
   1183 
   1184 	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
   1185 		   "sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
   1186 		   sc->sc_wire);
   1187 
   1188 	if (sc->sc_dying) {
   1189 		SDT_PROBE7(usb, umass, transfer, done,
   1190 		    sc, cb, priv, data, datalen, datalen, STATUS_WIRE_FAILED);
   1191 		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
   1192 		return;
   1193 	}
   1194 
   1195 	/* Be a little generous. */
   1196 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
   1197 
   1198 	/*
   1199 	 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
   1200 	 * a data phase of datalen bytes from/to the device and finally a
   1201 	 * csw read phase.
   1202 	 * If the data direction was inbound a maximum of datalen bytes
   1203 	 * is stored in the buffer pointed to by data.
   1204 	 *
   1205 	 * umass_bbb_transfer initialises the transfer and lets the state
   1206 	 * machine in umass_bbb_state handle the completion. It uses the
   1207 	 * following states:
   1208 	 * TSTATE_BBB_COMMAND
   1209 	 *   -> TSTATE_BBB_DATA
   1210 	 *   -> TSTATE_BBB_STATUS
   1211 	 *   -> TSTATE_BBB_STATUS2
   1212 	 *   -> TSTATE_BBB_IDLE
   1213 	 *
   1214 	 * An error in any of those states will invoke
   1215 	 * umass_bbb_reset.
   1216 	 */
   1217 
   1218 	/* check the given arguments */
   1219 	KASSERTMSG(datalen == 0 || data != NULL,
   1220 		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
   1221 	KASSERTMSG(cmdlen <= CBWCDBLENGTH,
   1222 		   "%s: cmdlen exceeds CDB length in CBW (%d > %d)",
   1223 			device_xname(sc->sc_dev), cmdlen, CBWCDBLENGTH);
   1224 	KASSERTMSG(dir == DIR_NONE || datalen > 0,
   1225 		   "%s: datalen == 0 while direction is not NONE\n",
   1226 			device_xname(sc->sc_dev));
   1227 	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
   1228 		   "%s: direction is NONE while datalen is not zero\n",
   1229 			device_xname(sc->sc_dev));
   1230 	/* CTASSERT */
   1231 	KASSERTMSG(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
   1232 		   "%s: CBW struct does not have the right size (%zu vs. %u)\n",
   1233 			device_xname(sc->sc_dev),
   1234 			sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE);
   1235 	/* CTASSERT */
   1236 	KASSERTMSG(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
   1237 		   "%s: CSW struct does not have the right size (%zu vs. %u)\n",
   1238 			device_xname(sc->sc_dev),
   1239 			sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE);
   1240 
   1241 	/*
   1242 	 * Determine the direction of the data transfer and the length.
   1243 	 *
   1244 	 * dCBWDataTransferLength (datalen) :
   1245 	 *   This field indicates the number of bytes of data that the host
   1246 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
   1247 	 *   the Direction bit) during the execution of this command. If this
   1248 	 *   field is set to 0, the device will expect that no data will be
   1249 	 *   transferred IN or OUT during this command, regardless of the value
   1250 	 *   of the Direction bit defined in dCBWFlags.
   1251 	 *
   1252 	 * dCBWFlags (dir) :
   1253 	 *   The bits of the Flags field are defined as follows:
   1254 	 *     Bits 0-6	 reserved
   1255 	 *     Bit  7	 Direction - this bit shall be ignored if the
   1256 	 *			     dCBWDataTransferLength field is zero.
   1257 	 *		 0 = data Out from host to device
   1258 	 *		 1 = data In from device to host
   1259 	 */
   1260 
   1261 	/* Fill in the Command Block Wrapper */
   1262 	USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
   1263 	USETDW(sc->cbw.dCBWTag, dCBWtag);
   1264 	dCBWtag++;	/* cannot be done in macro (it will be done 4 times) */
   1265 	USETDW(sc->cbw.dCBWDataTransferLength, datalen);
   1266 	/* DIR_NONE is treated as DIR_OUT (0x00) */
   1267 	sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
   1268 	sc->cbw.bCBWLUN = lun;
   1269 	sc->cbw.bCDBLength = cmdlen;
   1270 	memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
   1271 
   1272 	DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
   1273 
   1274 	/* store the details for the data transfer phase */
   1275 	sc->transfer_dir = dir;
   1276 	sc->transfer_data = data;
   1277 	sc->transfer_datalen = datalen;
   1278 	sc->transfer_actlen = 0;
   1279 	sc->transfer_cb = cb;
   1280 	sc->transfer_priv = priv;
   1281 	sc->transfer_status = STATUS_CMD_OK;
   1282 
   1283 	/* move from idle to the command state */
   1284 	sc->transfer_state = TSTATE_BBB_COMMAND;
   1285 
   1286 	/* Send the CBW from host to device via bulk-out endpoint. */
   1287 	if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
   1288 			&sc->cbw, UMASS_BBB_CBW_SIZE, flags,
   1289 			sc->transfer_xfer[XFER_BBB_CBW])) {
   1290 		umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1291 	}
   1292 }
   1293 
   1294 
   1295 Static void
   1296 umass_bbb_state(struct usbd_xfer *xfer, void *priv,
   1297 		usbd_status err)
   1298 {
   1299 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1300 	struct umass_softc *sc = (struct umass_softc *) priv;
   1301 	struct usbd_xfer *next_xfer;
   1302 	int residue;
   1303 
   1304 	SDT_PROBE3(usb, umass, bbb, state,  sc, xfer, err);
   1305 
   1306 	KASSERTMSG(sc->sc_wire & UMASS_WPROTO_BBB,
   1307 		   "sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
   1308 		   sc->sc_wire);
   1309 
   1310 	/*
   1311 	 * State handling for BBB transfers.
   1312 	 *
   1313 	 * The subroutine is rather long. It steps through the states given in
   1314 	 * Annex A of the Bulk-Only specification.
   1315 	 * Each state first does the error handling of the previous transfer
   1316 	 * and then prepares the next transfer.
   1317 	 * Each transfer is done asynchroneously so after the request/transfer
   1318 	 * has been submitted you will find a 'return;'.
   1319 	 */
   1320 
   1321 	DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx, transfer_state %jd dir %jd",
   1322 	    (uintptr_t)sc, (uintptr_t)xfer, sc->transfer_state,
   1323 	    sc->transfer_dir);
   1324 
   1325 	if (err == USBD_CANCELLED) {
   1326 		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
   1327 		    (uintptr_t)sc, (uintptr_t)xfer, 0, 0);
   1328 
   1329 		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
   1330 		return;
   1331 	}
   1332 
   1333 	if (sc->sc_dying) {
   1334 		umass_transfer_done(sc, sc->transfer_datalen,
   1335 		    STATUS_WIRE_FAILED);
   1336 		return;
   1337 	}
   1338 
   1339 	switch (sc->transfer_state) {
   1340 
   1341 	/***** Bulk Transfer *****/
   1342 	case TSTATE_BBB_COMMAND:
   1343 		/* Command transport phase, error handling */
   1344 		if (err) {
   1345 			DPRINTFM(UDMASS_BBB, "sc %#jx failed to send CBW",
   1346 			    (uintptr_t)sc, 0, 0, 0);
   1347 			/* If the device detects that the CBW is invalid, then
   1348 			 * the device may STALL both bulk endpoints and require
   1349 			 * a Bulk-Reset
   1350 			 */
   1351 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1352 			return;
   1353 		}
   1354 
   1355 		/* Data transport phase, setup transfer */
   1356 		sc->transfer_state = TSTATE_BBB_DATA;
   1357 		if (sc->transfer_dir == DIR_IN) {
   1358 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1359 					sc->datain_buffer, sc->transfer_datalen,
   1360 					USBD_SHORT_XFER_OK,
   1361 					sc->transfer_xfer[XFER_BBB_DATAIN]))
   1362 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1363 
   1364 			return;
   1365 		} else if (sc->transfer_dir == DIR_OUT) {
   1366 			memcpy(sc->dataout_buffer, sc->transfer_data,
   1367 			       sc->transfer_datalen);
   1368 			if (umass_setup_transfer(sc,
   1369 			    sc->sc_pipe[UMASS_BULKOUT], sc->dataout_buffer,
   1370 			    sc->transfer_datalen, 0,/* fixed length transfer */
   1371 			    sc->transfer_xfer[XFER_BBB_DATAOUT]))
   1372 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1373 
   1374 			return;
   1375 		} else {
   1376 			DPRINTFM(UDMASS_BBB, "sc %#jx: no data phase",
   1377 			    (uintptr_t)sc, 0, 0, 0);
   1378 		}
   1379 
   1380 		/* if no data phase, err == 0 */
   1381 		/* FALLTHROUGH */
   1382 	case TSTATE_BBB_DATA:
   1383 		/* Command transport phase error handling (ignored if no data
   1384 		 * phase (fallthrough from previous state)) */
   1385 		if (sc->transfer_dir != DIR_NONE) {
   1386 			/* retrieve the length of the transfer that was done */
   1387 			usbd_get_xfer_status(xfer, NULL, NULL,
   1388 			     &sc->transfer_actlen, NULL);
   1389 			DPRINTFM(UDMASS_BBB, "sc %#jx: BBB_DATA actlen=%jd",
   1390 			    (uintptr_t)sc, sc->transfer_actlen, 0, 0);
   1391 
   1392 			if (err) {
   1393 				DPRINTFM(UDMASS_BBB, "sc %#jx Data dir %jd "
   1394 				    "err %jd failed, err %jd",
   1395 				    (uintptr_t)sc, sc->transfer_dir,
   1396 				    sc->transfer_datalen, err);
   1397 
   1398 				if (err == USBD_STALLED) {
   1399 					sc->transfer_state = TSTATE_BBB_DCLEAR;
   1400 					umass_clear_endpoint_stall(sc,
   1401 					  (sc->transfer_dir == DIR_IN?
   1402 					    UMASS_BULKIN:UMASS_BULKOUT),
   1403 					  sc->transfer_xfer[XFER_BBB_DCLEAR]);
   1404 				} else {
   1405 					/* Unless the error is a pipe stall the
   1406 					 * error is fatal.
   1407 					 */
   1408 					umass_bbb_reset(sc,STATUS_WIRE_FAILED);
   1409 				}
   1410 				return;
   1411 			}
   1412 		}
   1413 
   1414 		/* err == 0 (no data phase or successful) */
   1415 		/* FALLTHROUGH */
   1416 	case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
   1417 		if (sc->transfer_dir == DIR_IN)
   1418 			memcpy(sc->transfer_data, sc->datain_buffer,
   1419 			       sc->transfer_actlen);
   1420 
   1421 		DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
   1422 					umass_dump_buffer(sc, sc->transfer_data,
   1423 						sc->transfer_datalen, 48));
   1424 
   1425 		/* err == 0 (no data phase or successful) */
   1426 		/* FALLTHROUGH */
   1427 	case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
   1428 		/* Reading of CSW after bulk stall condition in data phase
   1429 		 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
   1430 		 * reading CSW (TSTATE_BBB_SCLEAR).
   1431 		 * In the case of no data phase or successful data phase,
   1432 		 * err == 0 and the following if block is passed.
   1433 		 */
   1434 		if (err) {	/* should not occur */
   1435 			printf("%s: BBB bulk-%s stall clear failed, %s\n",
   1436 			    device_xname(sc->sc_dev),
   1437 			    (sc->transfer_dir == DIR_IN? "in":"out"),
   1438 			    usbd_errstr(err));
   1439 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1440 			return;
   1441 		}
   1442 
   1443 		/* Status transport phase, setup transfer */
   1444 		if (sc->transfer_state == TSTATE_BBB_COMMAND ||
   1445 		    sc->transfer_state == TSTATE_BBB_DATA ||
   1446 		    sc->transfer_state == TSTATE_BBB_DCLEAR) {
   1447 			/* After no data phase, successful data phase and
   1448 			 * after clearing bulk-in/-out stall condition
   1449 			 */
   1450 			sc->transfer_state = TSTATE_BBB_STATUS1;
   1451 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
   1452 		} else {
   1453 			/* After first attempt of fetching CSW */
   1454 			sc->transfer_state = TSTATE_BBB_STATUS2;
   1455 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
   1456 		}
   1457 
   1458 		/* Read the Command Status Wrapper via bulk-in endpoint. */
   1459 		if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1460 			&sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
   1461 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1462 			return;
   1463 		}
   1464 
   1465 		return;
   1466 	case TSTATE_BBB_STATUS1:	/* first attempt */
   1467 	case TSTATE_BBB_STATUS2:	/* second attempt */
   1468 		/* Status transfer, error handling */
   1469 		if (err) {
   1470 			DPRINTFM(UDMASS_BBB, "sc %#jx Failed to read CSW "
   1471 			    "err %jd (state %jd)", (uintptr_t)sc, err,
   1472 			    sc->transfer_state, 0);
   1473 
   1474 			/* If this was the first attempt at fetching the CSW
   1475 			 * retry it, otherwise fail.
   1476 			 */
   1477 			if (sc->transfer_state == TSTATE_BBB_STATUS1) {
   1478 				sc->transfer_state = TSTATE_BBB_SCLEAR;
   1479 				umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   1480 				    sc->transfer_xfer[XFER_BBB_SCLEAR]);
   1481 				return;
   1482 			} else {
   1483 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1484 				return;
   1485 			}
   1486 		}
   1487 
   1488 		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
   1489 
   1490 #ifdef UMASS_DEBUG
   1491 		residue = UGETDW(sc->csw.dCSWDataResidue);
   1492 		if (residue != sc->transfer_datalen - sc->transfer_actlen)
   1493 			printf("%s: dCSWDataResidue=%d req=%d act=%d\n",
   1494 			       device_xname(sc->sc_dev), residue,
   1495 			       sc->transfer_datalen, sc->transfer_actlen);
   1496 #endif
   1497 		residue = sc->transfer_datalen - sc->transfer_actlen;
   1498 
   1499 		/* Translate weird command-status signatures. */
   1500 		if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
   1501 		    UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
   1502 			USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
   1503 
   1504 		/* Translate invalid command-status tags */
   1505 		if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
   1506 			USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
   1507 
   1508 		/* Check CSW and handle any error */
   1509 		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
   1510 			/* Invalid CSW: Wrong signature or wrong tag might
   1511 			 * indicate that the device is confused -> reset it.
   1512 			 */
   1513 			printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
   1514 				device_xname(sc->sc_dev),
   1515 				UGETDW(sc->csw.dCSWSignature),
   1516 				CSWSIGNATURE);
   1517 
   1518 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1519 			return;
   1520 		} else if (UGETDW(sc->csw.dCSWTag)
   1521 				!= UGETDW(sc->cbw.dCBWTag)) {
   1522 			printf("%s: Invalid CSW: tag %d should be %d\n",
   1523 				device_xname(sc->sc_dev),
   1524 				UGETDW(sc->csw.dCSWTag),
   1525 				UGETDW(sc->cbw.dCBWTag));
   1526 
   1527 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1528 			return;
   1529 
   1530 		/* CSW is valid here */
   1531 		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
   1532 			printf("%s: Invalid CSW: status %d > %d\n",
   1533 				device_xname(sc->sc_dev),
   1534 				sc->csw.bCSWStatus,
   1535 				CSWSTATUS_PHASE);
   1536 
   1537 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1538 			return;
   1539 		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
   1540 			printf("%s: Phase Error, residue = %d\n",
   1541 				device_xname(sc->sc_dev), residue);
   1542 
   1543 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1544 			return;
   1545 
   1546 		} else if (sc->transfer_actlen > sc->transfer_datalen) {
   1547 			/* Buffer overrun! Don't let this go by unnoticed */
   1548 			panic("%s: transferred %s %d bytes instead of %d bytes",
   1549 			    device_xname(sc->sc_dev),
   1550 			    sc->transfer_dir == DIR_IN ? "IN" : "OUT",
   1551 			    sc->transfer_actlen, sc->transfer_datalen);
   1552 #if 0
   1553 		} else if (sc->transfer_datalen - sc->transfer_actlen
   1554 			   != residue) {
   1555 			DPRINTFM(UDMASS_BBB, "sc %#jx: actlen=%jd != "
   1556 			    "residue=%jd\n", (uintptr_t)sc,
   1557 			    sc->transfer_datalen - sc->transfer_actlen,
   1558 			    residue, 0);
   1559 
   1560 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1561 			return;
   1562 #endif
   1563 		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
   1564 			DPRINTFM(UDMASS_BBB, "sc %#jx: Command Failed, "
   1565 			    "res = %jd", (uintptr_t)sc, residue, 0, 0);
   1566 
   1567 			/* SCSI command failed but transfer was succesful */
   1568 			umass_transfer_done(sc, residue, STATUS_CMD_FAILED);
   1569 			return;
   1570 
   1571 		} else {	/* success */
   1572 			umass_transfer_done(sc, residue, STATUS_CMD_OK);
   1573 			return;
   1574 		}
   1575 
   1576 	/***** Bulk Reset *****/
   1577 	case TSTATE_BBB_RESET1:
   1578 		if (err)
   1579 			printf("%s: BBB reset failed, %s\n",
   1580 				device_xname(sc->sc_dev), usbd_errstr(err));
   1581 
   1582 		sc->transfer_state = TSTATE_BBB_RESET2;
   1583 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   1584 			sc->transfer_xfer[XFER_BBB_RESET2]);
   1585 
   1586 		return;
   1587 	case TSTATE_BBB_RESET2:
   1588 		if (err)	/* should not occur */
   1589 			printf("%s: BBB bulk-in clear stall failed, %s\n",
   1590 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1591 			/* no error recovery, otherwise we end up in a loop */
   1592 
   1593 		sc->transfer_state = TSTATE_BBB_RESET3;
   1594 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
   1595 			sc->transfer_xfer[XFER_BBB_RESET3]);
   1596 
   1597 		return;
   1598 	case TSTATE_BBB_RESET3:
   1599 		if (err)	/* should not occur */
   1600 			printf("%s: BBB bulk-out clear stall failed, %s\n",
   1601 			       device_xname(sc->sc_dev), usbd_errstr(err));
   1602 			/* no error recovery, otherwise we end up in a loop */
   1603 
   1604 		umass_transfer_reset(sc);
   1605 
   1606 		return;
   1607 
   1608 	/***** Default *****/
   1609 	default:
   1610 		panic("%s: Unknown state %d",
   1611 		      device_xname(sc->sc_dev), sc->transfer_state);
   1612 	}
   1613 }
   1614 
   1615 /*
   1616  * Command/Bulk/Interrupt (CBI) specific functions
   1617  */
   1618 
   1619 Static int
   1620 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, int flags,
   1621 	       struct usbd_xfer *xfer)
   1622 {
   1623 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1624 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
   1625 		   sc->sc_wire);
   1626 
   1627 	if ((sc->sc_cmd == UMASS_CPROTO_RBC) &&
   1628 	    (sc->sc_quirks & UMASS_QUIRK_RBC_PAD_TO_12) != 0 && buflen < 12) {
   1629 		(void)memset(buffer + buflen, 0, 12 - buflen);
   1630 		buflen = 12;
   1631 	}
   1632 
   1633 	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1634 	sc->sc_req.bRequest = UR_CBI_ADSC;
   1635 	USETW(sc->sc_req.wValue, 0);
   1636 	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
   1637 	USETW(sc->sc_req.wLength, buflen);
   1638 	return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
   1639 					 buflen, flags, xfer);
   1640 }
   1641 
   1642 
   1643 Static void
   1644 umass_cbi_reset(struct umass_softc *sc, int status)
   1645 {
   1646 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1647 	SDT_PROBE2(usb, umass, bbb, reset,  sc, status);
   1648 	int i;
   1649 #	define SEND_DIAGNOSTIC_CMDLEN	12
   1650 
   1651 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1652 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
   1653 		   sc->sc_wire);
   1654 
   1655 	if (sc->sc_dying) {
   1656 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1657 		return;
   1658 	}
   1659 
   1660 	/*
   1661 	 * Command Block Reset Protocol
   1662 	 *
   1663 	 * First send a reset request to the device. Then clear
   1664 	 * any possibly stalled bulk endpoints.
   1665 
   1666 	 * This is done in 3 steps, states:
   1667 	 * TSTATE_CBI_RESET1
   1668 	 * TSTATE_CBI_RESET2
   1669 	 * TSTATE_CBI_RESET3
   1670 	 *
   1671 	 * If the reset doesn't succeed, the device should be port reset.
   1672 	 */
   1673 
   1674 	DPRINTFM(UDMASS_CBI, "sc %#jx: CBI Reset", (uintptr_t)sc, 0, 0, 0);
   1675 
   1676 	/* CTASSERT */
   1677 	KASSERTMSG(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
   1678 		   "%s: CBL struct is too small (%zu < %u)\n",
   1679 			device_xname(sc->sc_dev),
   1680 			sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN);
   1681 
   1682 	sc->transfer_state = TSTATE_CBI_RESET1;
   1683 	sc->transfer_status = status;
   1684 
   1685 	/* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
   1686 	 * the two the last 10 bytes of the cbl is filled with 0xff (section
   1687 	 * 2.2 of the CBI spec).
   1688 	 */
   1689 	sc->cbl[0] = 0x1d;	/* Command Block Reset */
   1690 	sc->cbl[1] = 0x04;
   1691 	for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
   1692 		sc->cbl[i] = 0xff;
   1693 
   1694 	if (umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 0,
   1695 		sc->transfer_xfer[XFER_CBI_RESET1]))
   1696 		umass_transfer_done(sc, sc->transfer_datalen, status);
   1697 	/* XXX if the command fails we should reset the port on the bub */
   1698 }
   1699 
   1700 Static void
   1701 umass_cbi_transfer(struct umass_softc *sc, int lun,
   1702 		   void *cmd, int cmdlen, void *data, int datalen, int dir,
   1703 		   u_int timeout, int flags, umass_callback cb, void *priv)
   1704 {
   1705 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1706 	SDT_PROBE7(usb, umass, transfer, start__cbi,
   1707 	    sc, cb, priv, data, datalen, dir, timeout);
   1708 
   1709 	DPRINTFM(UDMASS_CBI, "sc %#jx: cmd=0x%02jx, len=%jd",
   1710 	     (uintptr_t)sc, *(u_char *)cmd, datalen, 0);
   1711 
   1712 	KASSERT(cb);
   1713 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1714 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
   1715 		   sc->sc_wire);
   1716 
   1717 	if (sc->sc_dying) {
   1718 		SDT_PROBE7(usb, umass, transfer, done,
   1719 		    sc, cb, priv, data, datalen, datalen, STATUS_WIRE_FAILED);
   1720 		cb(sc, priv, datalen, STATUS_WIRE_FAILED);
   1721 		return;
   1722 	}
   1723 
   1724 	/* Be a little generous. */
   1725 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
   1726 
   1727 	/*
   1728 	 * Do a CBI transfer with cmdlen bytes from cmd, possibly
   1729 	 * a data phase of datalen bytes from/to the device and finally a
   1730 	 * csw read phase.
   1731 	 * If the data direction was inbound a maximum of datalen bytes
   1732 	 * is stored in the buffer pointed to by data.
   1733 	 *
   1734 	 * umass_cbi_transfer initialises the transfer and lets the state
   1735 	 * machine in umass_cbi_state handle the completion. It uses the
   1736 	 * following states:
   1737 	 * TSTATE_CBI_COMMAND
   1738 	 *   -> XXX fill in
   1739 	 *
   1740 	 * An error in any of those states will invoke
   1741 	 * umass_cbi_reset.
   1742 	 */
   1743 
   1744 	/* check the given arguments */
   1745 	KASSERTMSG(datalen == 0 || data != NULL,
   1746 		   "%s: datalen > 0, but no buffer",device_xname(sc->sc_dev));
   1747 	KASSERTMSG(datalen == 0 || dir != DIR_NONE,
   1748 		   "%s: direction is NONE while datalen is not zero\n",
   1749 			device_xname(sc->sc_dev));
   1750 
   1751 	/* store the details for the data transfer phase */
   1752 	sc->transfer_dir = dir;
   1753 	sc->transfer_data = data;
   1754 	sc->transfer_datalen = datalen;
   1755 	sc->transfer_actlen = 0;
   1756 	sc->transfer_cb = cb;
   1757 	sc->transfer_priv = priv;
   1758 	sc->transfer_status = STATUS_CMD_OK;
   1759 
   1760 	/* move from idle to the command state */
   1761 	sc->transfer_state = TSTATE_CBI_COMMAND;
   1762 
   1763 	/* Send the Command Block from host to device via control endpoint. */
   1764 	if (umass_cbi_adsc(sc, cmd, cmdlen, flags,
   1765 	    sc->transfer_xfer[XFER_CBI_CB]))
   1766 		umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1767 }
   1768 
   1769 Static void
   1770 umass_cbi_state(struct usbd_xfer *xfer, void *priv,
   1771 		usbd_status err)
   1772 {
   1773 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   1774 	struct umass_softc *sc = (struct umass_softc *) priv;
   1775 
   1776 	SDT_PROBE3(usb, umass, bbb, state,  sc, xfer, err);
   1777 
   1778 	KASSERTMSG(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
   1779 		   "sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
   1780 		   sc->sc_wire);
   1781 
   1782 	if (err == USBD_CANCELLED) {
   1783 		DPRINTFM(UDMASS_BBB, "sc %#jx xfer %#jx cancelled",
   1784 			(uintptr_t)sc, (uintptr_t)xfer, 0, 0);
   1785 		umass_transfer_done(sc, 0, STATUS_TIMEOUT);
   1786 		return;
   1787 	}
   1788 
   1789 	if (sc->sc_dying) {
   1790 		umass_transfer_done(sc, sc->transfer_datalen,
   1791 		    STATUS_WIRE_FAILED);
   1792 		return;
   1793 	}
   1794 
   1795 	/*
   1796 	 * State handling for CBI transfers.
   1797 	 */
   1798 
   1799 	DPRINTFM(UDMASS_CBI, "sc %#jx: Handling CBI state %jd, xfer=%#jx, ...",
   1800 	    (uintptr_t)sc, sc->transfer_state, (uintptr_t)xfer, 0);
   1801 	DPRINTFM(UDMASS_CBI, "... err %jd", err, 0, 0, 0);
   1802 
   1803 	switch (sc->transfer_state) {
   1804 
   1805 	/***** CBI Transfer *****/
   1806 	case TSTATE_CBI_COMMAND:
   1807 		if (err == USBD_STALLED) {
   1808 			DPRINTFM(UDMASS_CBI, "sc %#jx: Command Transport "
   1809 			    "failed", (uintptr_t)sc, 0, 0, 0);
   1810 			/* Status transport by control pipe (section 2.3.2.1).
   1811 			 * The command contained in the command block failed.
   1812 			 *
   1813 			 * The control pipe has already been unstalled by the
   1814 			 * USB stack.
   1815 			 * Section 2.4.3.1.1 states that the bulk in endpoints
   1816 			 * should not stalled at this point.
   1817 			 */
   1818 			umass_transfer_done(sc, sc->transfer_datalen,
   1819 			    STATUS_CMD_FAILED);
   1820 			return;
   1821 		} else if (err) {
   1822 			DPRINTFM(UDMASS_CBI, "sc %#jx: failed to send ADSC",
   1823 			    (uintptr_t)sc, 0, 0, 0);
   1824 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1825 			return;
   1826 		}
   1827 
   1828 		/* Data transport phase, setup transfer */
   1829 		sc->transfer_state = TSTATE_CBI_DATA;
   1830 		if (sc->transfer_dir == DIR_IN) {
   1831 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
   1832 			    sc->datain_buffer, sc->transfer_datalen,
   1833 			    USBD_SHORT_XFER_OK,
   1834 			    sc->transfer_xfer[XFER_CBI_DATAIN]))
   1835 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1836 
   1837 			return;
   1838 		} else if (sc->transfer_dir == DIR_OUT) {
   1839 			memcpy(sc->dataout_buffer, sc->transfer_data,
   1840 			       sc->transfer_datalen);
   1841 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
   1842 			    sc->dataout_buffer, sc->transfer_datalen,
   1843 			    0, /* fixed length transfer */
   1844 			    sc->transfer_xfer[XFER_CBI_DATAOUT]))
   1845 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1846 
   1847 			return;
   1848 		} else {
   1849 			DPRINTFM(UDMASS_CBI, "sc %#jx: no data phase",
   1850 			    (uintptr_t)sc, 0, 0, 0);
   1851 		}
   1852 
   1853 		/* if no data phase, err == 0 */
   1854 		/* FALLTHROUGH */
   1855 	case TSTATE_CBI_DATA:
   1856 		/* Command transport phase error handling (ignored if no data
   1857 		 * phase (fallthrough from previous state)) */
   1858 		if (sc->transfer_dir != DIR_NONE) {
   1859 			/* retrieve the length of the transfer that was done */
   1860 			usbd_get_xfer_status(xfer, NULL, NULL,
   1861 			    &sc->transfer_actlen, NULL);
   1862 			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_DATA actlen=%jd",
   1863 				(uintptr_t)sc, sc->transfer_actlen, 0, 0);
   1864 
   1865 			if (err) {
   1866 				DPRINTFM(UDMASS_CBI, "sc %#jx: Data dir %jd "
   1867 				    "err %jd failed",
   1868 				    (uintptr_t)sc, sc->transfer_dir,
   1869 				    sc->transfer_datalen, err);
   1870 
   1871 				if (err == USBD_STALLED) {
   1872 					sc->transfer_state = TSTATE_CBI_DCLEAR;
   1873 					umass_clear_endpoint_stall(sc,
   1874 					  (sc->transfer_dir == DIR_IN?
   1875 					    UMASS_BULKIN:UMASS_BULKOUT),
   1876 					sc->transfer_xfer[XFER_CBI_DCLEAR]);
   1877 				} else {
   1878 					/* Unless the error is a pipe stall the
   1879 					 * error is fatal.
   1880 					 */
   1881 					umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1882 				}
   1883 				return;
   1884 			}
   1885 		}
   1886 
   1887 		if (sc->transfer_dir == DIR_IN)
   1888 			memcpy(sc->transfer_data, sc->datain_buffer,
   1889 			       sc->transfer_actlen);
   1890 
   1891 		DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
   1892 					umass_dump_buffer(sc, sc->transfer_data,
   1893 						sc->transfer_actlen, 48));
   1894 
   1895 		/* Status phase */
   1896 		if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
   1897 			sc->transfer_state = TSTATE_CBI_STATUS;
   1898 			memset(&sc->sbl, 0, sizeof(sc->sbl));
   1899 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
   1900 				    &sc->sbl, sizeof(sc->sbl),
   1901 				    0,	/* fixed length transfer */
   1902 				    sc->transfer_xfer[XFER_CBI_STATUS]))
   1903 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1904 		} else {
   1905 			/* No command completion interrupt. Request
   1906 			 * sense to get status of command.
   1907 			 */
   1908 			umass_transfer_done(sc,
   1909 			    sc->transfer_datalen - sc->transfer_actlen,
   1910 			    STATUS_CMD_UNKNOWN);
   1911 		}
   1912 		return;
   1913 
   1914 	case TSTATE_CBI_STATUS:
   1915 		if (err) {
   1916 			DPRINTFM(UDMASS_CBI, "sc %#jx: Status Transport failed",
   1917 			    (uintptr_t)sc, 0, 0, 0);
   1918 			/* Status transport by interrupt pipe (section 2.3.2.2).
   1919 			 */
   1920 
   1921 			if (err == USBD_STALLED) {
   1922 				sc->transfer_state = TSTATE_CBI_SCLEAR;
   1923 				umass_clear_endpoint_stall(sc, UMASS_INTRIN,
   1924 					sc->transfer_xfer[XFER_CBI_SCLEAR]);
   1925 			} else {
   1926 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1927 			}
   1928 			return;
   1929 		}
   1930 
   1931 		/* Dissect the information in the buffer */
   1932 
   1933 		{
   1934 			uint32_t actlen;
   1935 			usbd_get_xfer_status(xfer,NULL,NULL,&actlen,NULL);
   1936 			DPRINTFM(UDMASS_CBI, "sc %#jx: CBI_STATUS actlen=%jd",
   1937 			    (uintptr_t)sc, actlen, 0, 0);
   1938 			if (actlen != 2)
   1939 				break;
   1940 		}
   1941 
   1942 		if (sc->sc_cmd == UMASS_CPROTO_UFI) {
   1943 			int status;
   1944 
   1945 			/* Section 3.4.3.1.3 specifies that the UFI command
   1946 			 * protocol returns an ASC and ASCQ in the interrupt
   1947 			 * data block.
   1948 			 */
   1949 
   1950 			DPRINTFM(UDMASS_CBI, "sc %#jx: UFI CCI, ASC = 0x%02jx, "
   1951 			    "ASCQ = 0x%02jx", (uintptr_t)sc, sc->sbl.ufi.asc,
   1952 			    sc->sbl.ufi.ascq, 0);
   1953 
   1954 			if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
   1955 			    sc->sc_sense)
   1956 				status = STATUS_CMD_OK;
   1957 			else
   1958 				status = STATUS_CMD_FAILED;
   1959 
   1960 			/* No autosense, command successful */
   1961 			umass_transfer_done(sc,
   1962 			    sc->transfer_datalen - sc->transfer_actlen,
   1963 			    status);
   1964 		} else {
   1965 			int status;
   1966 
   1967 			/* Command Interrupt Data Block */
   1968 
   1969 			DPRINTFM(UDMASS_CBI, "sc %#jx: type=0x%02jx, "
   1970 			    "value=0x%02jx", (uintptr_t)sc,
   1971 			    sc->sbl.common.type, sc->sbl.common.value, 0);
   1972 
   1973 			if (sc->sbl.common.type == IDB_TYPE_CCI) {
   1974 				switch (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) {
   1975 				case IDB_VALUE_PASS:
   1976 					status = STATUS_CMD_OK;
   1977 					break;
   1978 				case IDB_VALUE_FAIL:
   1979 				case IDB_VALUE_PERSISTENT:
   1980 					status = STATUS_CMD_FAILED;
   1981 					break;
   1982 				case IDB_VALUE_PHASE:
   1983 				default: /* XXX: gcc */
   1984 					status = STATUS_WIRE_FAILED;
   1985 					break;
   1986 				}
   1987 
   1988 				umass_transfer_done(sc,
   1989 				    sc->transfer_datalen - sc->transfer_actlen,
   1990 				    status);
   1991 			} else {
   1992 				/* XXX What to do?  */
   1993 				umass_transfer_done(sc, sc->transfer_datalen,
   1994 				    STATUS_WIRE_FAILED);
   1995 			}
   1996 		}
   1997 		return;
   1998 
   1999 	case TSTATE_CBI_DCLEAR:
   2000 		if (err) {	/* should not occur */
   2001 			printf("%s: CBI bulk-%s stall clear failed, %s\n",
   2002 			    device_xname(sc->sc_dev),
   2003 			    (sc->transfer_dir == DIR_IN? "in":"out"),
   2004 			    usbd_errstr(err));
   2005 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2006 		} else {
   2007 			umass_transfer_done(sc,
   2008 			    sc->transfer_datalen, STATUS_CMD_FAILED);
   2009 		}
   2010 		return;
   2011 
   2012 	case TSTATE_CBI_SCLEAR:
   2013 		if (err) {	/* should not occur */
   2014 			printf("%s: CBI intr-in stall clear failed, %s\n",
   2015 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2016 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2017 		} else {
   2018 			umass_transfer_done(sc,
   2019 			    sc->transfer_datalen, STATUS_CMD_FAILED);
   2020 		}
   2021 		return;
   2022 
   2023 	/***** CBI Reset *****/
   2024 	case TSTATE_CBI_RESET1:
   2025 		if (err)
   2026 			printf("%s: CBI reset failed, %s\n",
   2027 				device_xname(sc->sc_dev), usbd_errstr(err));
   2028 
   2029 		sc->transfer_state = TSTATE_CBI_RESET2;
   2030 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
   2031 			sc->transfer_xfer[XFER_CBI_RESET2]);
   2032 
   2033 		return;
   2034 	case TSTATE_CBI_RESET2:
   2035 		if (err)	/* should not occur */
   2036 			printf("%s: CBI bulk-in stall clear failed, %s\n",
   2037 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2038 			/* no error recovery, otherwise we end up in a loop */
   2039 
   2040 		sc->transfer_state = TSTATE_CBI_RESET3;
   2041 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
   2042 			sc->transfer_xfer[XFER_CBI_RESET3]);
   2043 
   2044 		return;
   2045 	case TSTATE_CBI_RESET3:
   2046 		if (err)	/* should not occur */
   2047 			printf("%s: CBI bulk-out stall clear failed, %s\n",
   2048 			       device_xname(sc->sc_dev), usbd_errstr(err));
   2049 			/* no error recovery, otherwise we end up in a loop */
   2050 
   2051 		umass_transfer_reset(sc);
   2052 		return;
   2053 
   2054 
   2055 	/***** Default *****/
   2056 	default:
   2057 		panic("%s: Unknown state %d",
   2058 		      device_xname(sc->sc_dev), sc->transfer_state);
   2059 	}
   2060 }
   2061 
   2062 static usbd_status
   2063 umass_bbb_get_max_lun(struct umass_softc *sc, uint8_t *maxlun)
   2064 {
   2065 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2066 	usb_device_request_t req;
   2067 	usbd_status err;
   2068 
   2069 	*maxlun = 0;		/* Default to 0. */
   2070 
   2071 	DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun", (uintptr_t)sc, 0, 0, 0);
   2072 
   2073 	/* The Get Max Lun command is a class-specific request. */
   2074 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
   2075 	req.bRequest = UR_BBB_GET_MAX_LUN;
   2076 	USETW(req.wValue, 0);
   2077 	USETW(req.wIndex, sc->sc_ifaceno);
   2078 	USETW(req.wLength, 1);
   2079 
   2080 	err = usbd_do_request_flags(sc->sc_udev, &req, maxlun,
   2081 	    USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
   2082 	switch (err) {
   2083 	case USBD_NORMAL_COMPLETION:
   2084 		DPRINTFM(UDMASS_BBB, "sc %#jx: Max Lun %jd",
   2085 		    (uintptr_t)sc, *maxlun , 0, 0);
   2086 		break;
   2087 
   2088 	case USBD_STALLED:
   2089 		/*
   2090 		 * Device doesn't support Get Max Lun request.
   2091 		 */
   2092 		err = USBD_NORMAL_COMPLETION;
   2093 		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun not supported",
   2094 		    (uintptr_t)sc, 0, 0, 0);
   2095 		break;
   2096 
   2097 	case USBD_SHORT_XFER:
   2098 		/*
   2099 		 * XXX This must mean Get Max Lun is not supported, too!
   2100 		 */
   2101 		err = USBD_NORMAL_COMPLETION;
   2102 		DPRINTFM(UDMASS_BBB, "sc %#jx: Get Max Lun SHORT_XFER",
   2103 		    (uintptr_t)sc, 0, 0, 0);
   2104 		break;
   2105 
   2106 	default:
   2107 		printf("%s: Get Max Lun failed: %s\n",
   2108 		    device_xname(sc->sc_dev), usbd_errstr(err));
   2109 		/* XXX Should we port_reset the device? */
   2110 		break;
   2111 	}
   2112 
   2113 	return err;
   2114 }
   2115 
   2116 
   2117 
   2118 
   2119 #ifdef UMASS_DEBUG
   2120 Static void
   2121 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
   2122 {
   2123 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2124 	int clen = cbw->bCDBLength;
   2125 	int dlen = UGETDW(cbw->dCBWDataTransferLength);
   2126 	uint8_t *c = cbw->CBWCDB;
   2127 	int tag = UGETDW(cbw->dCBWTag);
   2128 	int flags = cbw->bCBWFlags;
   2129 
   2130 	DPRINTFM(UDMASS_BBB, "sc %#jx: CBW %jd: cmdlen=%jd",
   2131 	    (uintptr_t)sc, tag, clen, 0);
   2132 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
   2133 	    c[0], c[1], c[2], c[3]);
   2134 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx%02jx%02jx...",
   2135 	    c[4], c[5], c[6], c[7]);
   2136 	DPRINTFM(UDMASS_BBB, "  0x%02jx%02jx...", c[8], c[9], 0, 0);
   2137 	DPRINTFM(UDMASS_BBB, "  data = %jd bytes, flags = %jx", dlen, flags, 0,
   2138 	    0);
   2139 }
   2140 
   2141 Static void
   2142 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
   2143 {
   2144 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2145 	int sig = UGETDW(csw->dCSWSignature);
   2146 	int tag = UGETDW(csw->dCSWTag);
   2147 	int res = UGETDW(csw->dCSWDataResidue);
   2148 	int status = csw->bCSWStatus;
   2149 
   2150 	DPRINTFM(UDMASS_BBB, "sc %#jx: CSW %jd: sig = 0x%08jx, tag = %jd",
   2151 	    (uintptr_t)sc, (uintptr_t)csw, sig, tag);
   2152 	DPRINTFM(UDMASS_BBB, "  res = %jd, status = 0x%02jx",
   2153 	    res, status, 0, 0);
   2154 }
   2155 
   2156 Static void
   2157 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, int buflen,
   2158 		  int printlen)
   2159 {
   2160 	UMASSHIST_FUNC(); UMASSHIST_CALLED();
   2161 	int i;
   2162 
   2163 	DPRINTFM(UDMASS_GEN, "sc %#jx: buffer %#jx", (uintptr_t)sc,
   2164 	    (uintptr_t)buffer, 0, 0);
   2165 	for (i = 0; i < buflen && i < printlen;) {
   2166 		if (i + 3 < buflen && i + 3 < printlen) {
   2167 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx%02jx",
   2168 			    buffer[i], buffer[i + 1],
   2169 			    buffer[i + 2], buffer[i + 3]);
   2170 			i += 4;
   2171 		} else if (i + 2 < buflen && i + 2 < printlen) {
   2172 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx%02jx",
   2173 			    buffer[i], buffer[i + 1], buffer[i + 2], 0);
   2174 			i += 3;
   2175 		} else if (i + 1 < buflen && i + 2 < printlen) {
   2176 			DPRINTFM(UDMASS_GEN, "   0x%02jx%02jx",
   2177 			    buffer[i], buffer[i + 1], 0, 0);
   2178 			i += 2;
   2179 		} else {
   2180 			DPRINTFM(UDMASS_GEN, "   0x%02jx", buffer[i], 0, 0, 0);
   2181 			i += 1;
   2182 		}
   2183 	}
   2184 }
   2185 #endif
   2186