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