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