Home | History | Annotate | Line # | Download | only in usb
umass.c revision 1.6
      1 /*	$NetBSD: umass.c,v 1.6 1999/08/29 18:58:03 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
      5  *		      Nick Hibma <hibma (at) skylink.it>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  *	FreeBSD: src/sys/dev/usb/umass.c,v 1.8 1999/06/20 15:46:13 n_hibma Exp
     30  */
     31 
     32 /*
     33  * Universal Serial Bus Mass Storage Class Control/Interrupt/Bulk (CBI)
     34  * Specification:
     35  *
     36  *	http://www.usb.org/developers/usbmass-cbi10.pdf
     37  *
     38  * Universal Serial Bus Mass Storage Bulk Only 1.0rc4 Specification:
     39  *
     40  *	http://www.usb.org/developers/usbmassbulk_10rc4.pdf
     41  *
     42  * Relevant parts of the old spec (Bulk-only 0.9) have been quoted
     43  * in the source.
     44  */
     45 
     46 /* To do:
     47  *	x The umass_usb_transfer routine uses synchroneous transfers. This
     48  *	  should be changed to async and state handling.
     49  *
     50  *	x Should handle more than just Iomega USB Zip drives.  There are
     51  *	  a fair number of USB->SCSI dongles out there.
     52  *
     53  *	x Need to implement SCSI command timeout/abort handling.
     54  *
     55  *	x Need to handle SCSI Sense handling.
     56  *
     57  *	x Need to handle hot-unplug.
     58  *
     59  *	x Add support for other than Bulk.
     60  *
     61  *	x Add support for other than SCSI.
     62  */
     63 
     64 /* Authors: (with short acronyms for comments)
     65  *   NWH - Nick Hibma <hibma (at) skylink.it>
     66  *   JRT - Jason R. Thorpe <thorpej (at) shagadelic.org>
     67  */
     68 
     69 #include <sys/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/kernel.h>
     72 #include <sys/malloc.h>
     73 #include <sys/device.h>
     74 #include <sys/buf.h>
     75 #include <sys/proc.h>
     76 
     77 #include <dev/usb/usb.h>
     78 #include <dev/usb/usbdi.h>
     79 #include <dev/usb/usbdi_util.h>
     80 
     81 #include <dev/scsipi/scsi_all.h>
     82 #include <dev/scsipi/scsipi_all.h>
     83 #include <dev/scsipi/scsiconf.h>
     84 
     85 #ifdef UMASS_DEBUG
     86 #define	DPRINTF(m, x)	if (umassdebug & (m)) logprintf x
     87 #define UDMASS_SCSI	0x00020000
     88 #define UDMASS_USB	0x00040000
     89 #define UDMASS_BULK	0x00080000
     90 #define UDMASS_ALL	0xffff0000
     91 int umassdebug = /* UDMASS_SCSI|UDMASS_BULK|UDMASS_USB */ 0;
     92 #else
     93 #define	DPRINTF(m, x)
     94 #endif
     95 
     96 typedef struct umass_softc {
     97 	bdevice			sc_dev;		/* base device */
     98 	usbd_interface_handle	sc_iface;	/* the interface we use */
     99 
    100 	u_int8_t		sc_bulkout;	/* bulk-out Endpoint Address */
    101 	usbd_pipe_handle	sc_bulkout_pipe;
    102 	u_int8_t		sc_bulkin;	/* bulk-in Endpoint Address */
    103 	usbd_pipe_handle	sc_bulkin_pipe;
    104 
    105 	struct scsipi_link	sc_link;	/* prototype for devs */
    106 	struct scsipi_adapter	sc_adapter;
    107 } umass_softc_t;
    108 
    109 #define USBD_COMMAND_FAILED	USBD_INVAL	/* redefine some errors for */
    110 
    111 #define UMASS_SCSIID_HOST	0x00
    112 #define UMASS_SCSIID_DEVICE	0x01
    113 
    114 #define DIR_OUT		0
    115 #define DIR_IN		1
    116 #define DIR_NONE	2
    117 
    118 /* Bulk-Only specific request */
    119 #define	UR_RESET	0xff
    120 
    121 /* Bulk-Only Mass Storage features */
    122 /* Command Block Wrapper */
    123 typedef struct {
    124 	uDWord		dCBWSignature;
    125 #define  CBWSIGNATURE		0x43425355
    126 	uDWord		dCBWTag;
    127 	uDWord		dCBWDataTransferLength;
    128 	uByte		bCBWFlags;
    129 #define	 CBWFLAGS_OUT	0x00
    130 #define	 CBWFLAGS_IN	0x80
    131 	uByte		bCBWLUN;
    132 	uByte		bCDBLength;
    133 	uByte		CBWCDB[16];
    134 } usb_bulk_cbw_t;
    135 #define	USB_BULK_CBW_SIZE	31
    136 
    137 /* Command Status Wrapper */
    138 typedef struct {
    139 	uDWord		dCSWSignature;
    140 #define	 CSWSIGNATURE		0x53425355
    141 	uDWord		dCSWTag;
    142 	uDWord		dCSWDataResidue;
    143 	uByte		bCSWStatus;
    144 #define  CSWSTATUS_GOOD		0x0
    145 #define  CSWSTATUS_FAILED	0x1
    146 #define  CSWSTATUS_PHASE	0x2
    147 } usb_bulk_csw_t;
    148 #define	USB_BULK_CSW_SIZE	13
    149 
    150 
    151 USB_DECLARE_DRIVER(umass);
    152 
    153 /* USB related functions */
    154 usbd_status umass_usb_transfer __P((usbd_interface_handle iface,
    155 				usbd_pipe_handle pipe,
    156 		                void *buf, int buflen,
    157 				int flags, int *xfer_size));
    158 
    159 /* Bulk-Only related functions */
    160 usbd_status umass_bulk_reset	__P((umass_softc_t *sc));
    161 usbd_status umass_bulk_transfer	__P((umass_softc_t *sc, int lun,
    162 				void *cmd, int cmdlen,
    163 		    		void *data, int datalen,
    164 				int dir, int *residue));
    165 
    166 /* SCSIPI related functions */
    167 struct scsipi_device umass_dev = {
    168 	NULL,			/* Use default error handler */
    169 	NULL,			/* have a queue, served by this */
    170 	NULL,			/* have no async handler */
    171 	NULL,			/* Use default `done' routine */
    172 };
    173 
    174 void	umass_minphys		__P((struct buf *));
    175 int	umass_scsi_cmd		__P((struct scsipi_xfer *));
    176 
    177 
    178 
    179 USB_MATCH(umass)
    180 {
    181 	USB_MATCH_START(umass, uaa);
    182 	usb_interface_descriptor_t *id;
    183 
    184 	if (!uaa->iface)
    185 		return(UMATCH_NONE);
    186 
    187 	id = usbd_get_interface_descriptor(uaa->iface);
    188 	if (id
    189 	    && id->bInterfaceClass == UCLASS_MASS
    190 	    && id->bInterfaceSubClass == USUBCLASS_SCSI
    191 	    && id->bInterfaceProtocol == UPROTO_MASS_BULK)
    192 		return(UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    193 
    194 	return(UMATCH_NONE);
    195 }
    196 
    197 USB_ATTACH(umass)
    198 {
    199 	USB_ATTACH_START(umass, sc, uaa);
    200 	usb_interface_descriptor_t *id;
    201 	usb_endpoint_descriptor_t *ed;
    202 	char devinfo[1024];
    203 	int i;
    204 	int err;
    205 
    206 	sc->sc_iface = uaa->iface;
    207 	sc->sc_bulkout_pipe = NULL;
    208 	sc->sc_bulkin_pipe = NULL;
    209 
    210 	usbd_devinfo(uaa->device, 0, devinfo);
    211 	USB_ATTACH_SETUP;
    212 
    213 	id = usbd_get_interface_descriptor(sc->sc_iface);
    214 	printf("%s: %s, iclass %d/%d/%d\n", USBDEVNAME(sc->sc_dev), devinfo,
    215 	       id->bInterfaceClass, id->bInterfaceSubClass,
    216 	       id->bInterfaceProtocol);
    217 
    218 	/*
    219 	 * A Bulk-Only Mass Storage device supports the following endpoints,
    220 	 * in addition to the Endpoint 0 for Control transfer that is required
    221 	 * of all USB devices:
    222 	 * (a) bulk-in endpoint.
    223 	 * (b) bulk-out endpoint.
    224 	 *
    225 	 * The endpoint addresses are not fixed, so we have to read them
    226 	 * from the device descriptors of the current interface.
    227 	 */
    228 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    229 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    230 		if (!ed) {
    231 			printf("%s: could not read endpoint descriptor\n",
    232 			       USBDEVNAME(sc->sc_dev));
    233 			USB_ATTACH_ERROR_RETURN;
    234 		}
    235 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_IN
    236 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    237 			sc->sc_bulkin = ed->bEndpointAddress;
    238 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_OUT
    239 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    240 			sc->sc_bulkout = ed->bEndpointAddress;
    241 		}
    242 	}
    243 
    244 	/* Open the bulk-in and -out pipe */
    245 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
    246 				USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    247 	if (err) {
    248 		DPRINTF(UDMASS_USB, ("cannot open bulk out pipe (address %d)\n",
    249 			sc->sc_bulkout));
    250 		USB_ATTACH_ERROR_RETURN;
    251 	}
    252 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
    253 				USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    254 	if (err) {
    255 		DPRINTF(UDMASS_USB, ("cannot open bulk in pipe (address %d)\n",
    256 			sc->sc_bulkin));
    257 		usbd_close_pipe(sc->sc_bulkout_pipe);
    258 		USB_ATTACH_ERROR_RETURN;
    259 	}
    260 
    261 	/* attach the device to the SCSI layer */
    262 	sc->sc_adapter.scsipi_cmd = umass_scsi_cmd;
    263 	sc->sc_adapter.scsipi_minphys = umass_minphys;
    264 
    265 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
    266 	sc->sc_link.adapter_softc = sc;
    267 	sc->sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
    268 	sc->sc_link.adapter = &sc->sc_adapter;
    269 	sc->sc_link.device = &umass_dev;
    270 	sc->sc_link.openings = 4;		/* XXX */
    271 	sc->sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE; /* XXX */
    272 	sc->sc_link.scsipi_scsi.max_lun = 0;	/* XXX */
    273 	sc->sc_link.type = BUS_SCSI;
    274 
    275 	if (config_found(&sc->sc_dev, &sc->sc_link, scsiprint) == NULL) {
    276 		usbd_close_pipe(sc->sc_bulkout_pipe);
    277 		usbd_close_pipe(sc->sc_bulkin_pipe);
    278 		/* XXX Not really an error. */
    279 		USB_ATTACH_ERROR_RETURN;
    280 	}
    281 
    282 	USB_ATTACH_SUCCESS_RETURN;
    283 }
    284 
    285 int
    286 umass_activate(self, act)
    287 	struct device *self;
    288 	enum devact act;
    289 {
    290 
    291 	switch (act) {
    292 	case DVACT_ACTIVATE:
    293 		return (EOPNOTSUPP);
    294 		break;
    295 
    296 	case DVACT_DEACTIVATE:
    297 		/* XXX Not supported yet. */
    298 		return (EOPNOTSUPP);
    299 		break;
    300 	}
    301 	return (0);
    302 }
    303 
    304 int
    305 umass_detach(self, flags)
    306 	struct device *self;
    307 	int flags;
    308 {
    309 
    310 	/* XXX Not supported yet. */
    311 	return (EOPNOTSUPP);
    312 }
    313 
    314 /* Performs a request over a pipe.
    315  *
    316  * flags: Can be set to USBD_SHORT_XFER_OK
    317  * xfer_size: if not null returns the nr. of bytes transferred
    318  *
    319  * If the returned error is USBD_STALLED the pipe stall has
    320  * been cleared again.
    321  */
    322 
    323 usbd_status
    324 umass_usb_transfer(usbd_interface_handle iface, usbd_pipe_handle pipe,
    325 		   void *buf, int buflen, int flags, int *xfer_size)
    326 {
    327 	usbd_request_handle reqh;
    328 	usbd_private_handle priv;
    329 	void *buffer;
    330 	int size;
    331 	usbd_status err;
    332 
    333 	/* A transfer is done synchronously. We create and schedule the
    334 	 * transfer and then wait for it to complete
    335 	 */
    336 
    337 	reqh = usbd_alloc_request();
    338 	if (!reqh) {
    339 		DPRINTF(UDMASS_USB, ("Not enough memory\n"));
    340 		return USBD_NOMEM;
    341 	}
    342 
    343 	(void) usbd_setup_request(reqh, pipe, 0, buf, buflen,
    344 				flags, 3000 /*ms*/, NULL);
    345 	err = usbd_sync_transfer(reqh);
    346 	if (err) {
    347 		DPRINTF(UDMASS_USB, ("transfer failed, %s\n",
    348 			usbd_errstr(err)));
    349 		usbd_free_request(reqh);
    350 		return(err);
    351 	}
    352 
    353 	usbd_get_request_status(reqh, &priv, &buffer, &size, &err);
    354 
    355 	if (xfer_size)
    356 		*xfer_size = size;
    357 
    358 	usbd_free_request(reqh);
    359 	return(USBD_NORMAL_COMPLETION);
    360 }
    361 
    362 
    363 
    364 
    365 usbd_status
    366 umass_bulk_reset(umass_softc_t *sc)
    367 {
    368 	usbd_device_handle dev;
    369         usb_device_request_t req;
    370 	usbd_status err;
    371 	usb_interface_descriptor_t *id;
    372 
    373 	/*
    374 	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
    375 	 *
    376 	 * For Reset Recovery the host shall issue in the following order:
    377 	 * a) a Bulk-Only Mass Storage Reset
    378 	 * b) a Clear Feature HALT to the Bulk-In endpoint
    379 	 * c) a Clear Feature HALT to the Bulk-Out endpoint
    380 	 */
    381 
    382 	DPRINTF(UDMASS_BULK, ("%s: Reset\n",
    383 		USBDEVNAME(sc->sc_dev)));
    384 
    385 	usbd_interface2device_handle(sc->sc_iface, &dev);
    386 	id = usbd_get_interface_descriptor(sc->sc_iface);
    387 
    388 	/* the reset command is a class specific interface request */
    389 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    390 	req.bRequest = UR_RESET;
    391 	USETW(req.wValue, 0);
    392 	USETW(req.wIndex, id->bInterfaceNumber);
    393 	USETW(req.wLength, 0);
    394 
    395 	err = usbd_do_request(dev, &req, 0);
    396 	if (err) {
    397 		printf("%s: Reset failed, %s\n",
    398 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    399 		/* XXX we should port_reset the device */
    400 		return(err);
    401 	}
    402 
    403 	usbd_clear_endpoint_stall(sc->sc_bulkout_pipe);
    404 	usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
    405 
    406 	/*
    407 	 * XXX we should convert this into a more friendly delay.
    408 	 * Perhaps a tsleep (or is this routine run from int context?)
    409 	 */
    410 
    411 	DELAY(2500000 /*us*/);
    412 
    413 	return(USBD_NORMAL_COMPLETION);
    414 }
    415 
    416 /*
    417  * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
    418  * a data phase of datalen bytes from/to data and finally a csw read
    419  * phase.
    420  *
    421  * If the data direction was inbound a maximum of datalen bytes
    422  * is stored in the buffer pointed to by data.
    423  * The status returned is USBD_NORMAL_COMPLETION,
    424  * USBD_IOERROR, USBD_COMMAND_FAILED.
    425  * In the last case *residue is set to the residue from the CSW,
    426  * otherwise to 0.
    427  *
    428  * For the functionality of this subroutine see the Mass Storage
    429  * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of
    430  * the spec).
    431  */
    432 
    433 usbd_status
    434 umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen,
    435 		    void *data, int datalen, int dir, int *residue)
    436 {
    437 	static int dCBWtag = 42;	/* tag to be used in transfers,
    438 					 * incremented at each transfer */
    439 	usb_bulk_cbw_t cbw;		/* command block wrapper struct */
    440 	usb_bulk_csw_t csw;		/* command status wrapper struct */
    441 	u_int32_t n = 0;		/* number of bytes transported */
    442 	usbd_status err;
    443 
    444 #ifdef UMASS_DEBUG
    445 	u_int8_t *c = cmd;
    446 
    447 	/* check the given arguments */
    448 	if (!data && datalen > 0) {	/* no buffer for transfer */
    449 		DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n",
    450 			USBDEVNAME(sc->sc_dev)));
    451 		return USBD_IOERROR;
    452 	}
    453 
    454 	DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)"
    455 		", data: %d bytes, dir: %s\n",
    456 		USBDEVNAME(sc->sc_dev),
    457 		cmdlen, c[0], c[1], c[2], c[3], c[4], c[5],
    458 		(cmdlen > 6? "...":""),
    459 		datalen, (dir == DIR_IN? "in":"out")));
    460 #endif
    461 
    462 	if (dir == DIR_NONE || datalen == 0) {		/* make sure they correspond */
    463 		datalen = 0;
    464 		dir = DIR_NONE;
    465 	}
    466 
    467 	*residue = 0;			/* reset residue */
    468 
    469 	/*
    470 	 * Determine the direction of transferring data and data length.
    471 	 *
    472 	 * dCBWDataTransferLength (datalen) :
    473 	 *   This field indicates the number of bytes of data that the host
    474 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
    475 	 *   the Direction bit) during the execution of this command. If this
    476 	 *   field is set to 0, the device will expect that no data will be
    477 	 *   transferred IN or OUT during this command, regardless of the value
    478 	 *   of the Direction bit defined in dCBWFlags.
    479 	 *
    480 	 * dCBWFlags (dir) :
    481 	 *   The bits of the Flags field are defined as follows:
    482 	 *     Bits 0-6  reserved
    483 	 *     Bit  7    Direction - this bit shall be ignored if the
    484 	 *                           dCBWDataTransferLength field is zero.
    485 	 *               0 = data Out from host to device
    486 	 *               1 = data In from device to host
    487 	 */
    488 
    489 
    490 	/*
    491 	 * Command transport phase
    492 	 */
    493 
    494 	/* Fill in the Command Block Wrapper */
    495 	USETDW(cbw.dCBWSignature, CBWSIGNATURE);
    496 	USETDW(cbw.dCBWTag, dCBWtag++);
    497 	USETDW(cbw.dCBWDataTransferLength, datalen);
    498 	/* we do not check for DIR_NONE below (see text on dCBWFlags above) */
    499 	cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
    500 	cbw.bCBWLUN = lun;
    501 	cbw.bCDBLength = cmdlen;
    502 	bcopy(cmd, cbw.CBWCDB, cmdlen);
    503 
    504 	/* Send the CBW from host to device via bulk-out endpoint. */
    505 	err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkout_pipe,
    506 				&cbw, USB_BULK_CBW_SIZE, 0, NULL);
    507 	if (err) {
    508 		DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n",
    509 		         USBDEVNAME(sc->sc_dev)));
    510 		/* If the device detects that the CBW is invalid, then the
    511 		 * device may STALL both bulk endpoints and require a
    512 		 * Bulk-Only MS Reset
    513 		 */
    514 		umass_bulk_reset(sc);
    515 		return(USBD_IOERROR);
    516 	}
    517 
    518 
    519 	/*
    520 	 * Data transport phase (only if there is data to be sent/received)
    521 	 */
    522 
    523 	if (dir == DIR_IN) {
    524 		/* we allow short transfers for bulk-in pipes */
    525 		err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
    526 					data, datalen,
    527 					USBD_SHORT_XFER_OK, &n);
    528 		if (err)
    529 			DPRINTF(UDMASS_BULK, ("%s: failed to receive data, "
    530 				"(%d bytes, n = %d), %s\n",
    531 				USBDEVNAME(sc->sc_dev),
    532 				datalen, n, usbd_errstr(err)));
    533 	} else if (dir == DIR_OUT) {
    534 		err = umass_usb_transfer(sc->sc_iface,
    535 					sc->sc_bulkout_pipe,
    536 					data, datalen, 0, &n);
    537 		if (err)
    538 			DPRINTF(UDMASS_BULK, ("%s: failed to send data, "
    539 				"(%d bytes, n = %d), %s\n",
    540 				USBDEVNAME(sc->sc_dev),
    541 				datalen, n, usbd_errstr(err)));
    542 	}
    543 	if (err && err != USBD_STALLED)
    544 		return(USBD_IOERROR);
    545 
    546 
    547 	/*
    548 	 * Status transport phase
    549 	 */
    550 
    551 	/* Read the Command Status Wrapper via bulk-in endpoint. */
    552 	err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
    553 				&csw, USB_BULK_CSW_SIZE, 0, NULL);
    554 	/* Try again if the bulk-in pipe was stalled */
    555 	if (err == USBD_STALLED) {
    556 		err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
    557 		if (!err) {
    558 			err = umass_usb_transfer(sc->sc_iface, sc->sc_bulkin_pipe,
    559 						&csw, USB_BULK_CSW_SIZE, 0, NULL);
    560 		}
    561 	}
    562 	if (err && err != USBD_STALLED)
    563 		return(USBD_IOERROR);
    564 
    565 	/*
    566 	 * Check the CSW for status and validity, and check for fatal errors
    567 	 */
    568 
    569 	/* Invalid CSW: Wrong signature or wrong tag might indicate
    570 	 * that the device is confused -> reset it.
    571 	 * Other fatal errors: STALL on read of CSW and Phase error
    572 	 * or unknown status.
    573 	 */
    574 	if (err == USBD_STALLED
    575 	    || UGETDW(csw.dCSWSignature) != CSWSIGNATURE
    576 	    || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag)
    577 	    || csw.bCSWStatus == CSWSTATUS_PHASE
    578 	    || csw.bCSWStatus > CSWSTATUS_PHASE) {
    579 		if (err) {
    580 			printf("%s: failed to read CSW, %s\n",
    581 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    582 		} else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
    583 			printf("%s: Phase Error, residue = %d, n = %d\n",
    584 				USBDEVNAME(sc->sc_dev),
    585 				UGETDW(csw.dCSWDataResidue), n);
    586 		} else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
    587 			printf("%s: Unknown status %d in CSW\n",
    588 				USBDEVNAME(sc->sc_dev), csw.bCSWStatus);
    589 		} else {
    590 			printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n",
    591 				USBDEVNAME(sc->sc_dev),
    592 				UGETDW(csw.dCSWSignature),
    593 				UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag));
    594 		}
    595 		umass_bulk_reset(sc);
    596 		return(USBD_IOERROR);
    597 	}
    598 
    599 	if (csw.bCSWStatus == CSWSTATUS_FAILED) {
    600 		DPRINTF(UDMASS_BULK, ("%s: Command Failed, "
    601 			"residue = %d, n = %d\n",
    602 			USBDEVNAME(sc->sc_dev),
    603 			UGETDW(csw.dCSWDataResidue), n));
    604 		*residue = UGETDW(csw.dCSWDataResidue);
    605 		return(USBD_COMMAND_FAILED);
    606 	}
    607 
    608 	/*
    609 	 * XXX a residue not equal to 0 might indicate that something
    610 	 * is wrong. Does CAM high level drivers check this for us?
    611 	 */
    612 
    613 	return(USBD_NORMAL_COMPLETION);
    614 }
    615 
    616 
    617 /*
    618  * SCSIPI specific functions
    619  */
    620 
    621 int
    622 umass_scsi_cmd(xs)
    623 	struct scsipi_xfer *xs;
    624 {
    625 	struct scsipi_link *sc_link = xs->sc_link;
    626 	struct umass_softc *sc = sc_link->adapter_softc;
    627 	int residue, dir;
    628 	usbd_status err;
    629 
    630 	DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd %d:%d\n",
    631 	    USBDEVNAME(sc->sc_dev),
    632 	    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun));
    633 
    634 #ifdef UMASS_DEBUG
    635 	if (sc_link->scsipi_scsi.target != UMASS_SCSIID_DEVICE ||
    636 	    sc_link->scsipi_scsi.lun != 0) {
    637 		DPRINTF(UDMASS_SCSI, ("%s: Wrong SCSI ID %d or LUN %d\n",
    638 		    USBDEVNAME(sc->sc_dev),
    639 		    sc_link->scsipi_scsi.target,
    640 		    sc_link->scsipi_scsi.lun));
    641 		xs->error = XS_DRIVER_STUFFUP;
    642 		return (COMPLETE);
    643 	}
    644 #endif
    645 
    646 	dir = DIR_NONE;
    647 	if (xs->datalen) {
    648 		switch (xs->flags & (SCSI_DATA_IN|SCSI_DATA_OUT)) {
    649 		case SCSI_DATA_IN:
    650 			dir = DIR_IN;
    651 			break;
    652 		case SCSI_DATA_OUT:
    653 			dir = DIR_OUT;
    654 			break;
    655 		}
    656 	}
    657 
    658 	err = umass_bulk_transfer(sc, sc_link->scsipi_scsi.lun,
    659 	    xs->cmd, xs->cmdlen, xs->data, xs->datalen, dir, &residue);
    660 
    661 	/*
    662 	 * FAILED commands are supposed to be SCSI failed commands
    663 	 * and are therefore considered to be successfull CDW/CSW
    664 	 * transfers.  PHASE errors are more serious and should return
    665 	 * an error to the SCSIPI system.
    666 	 *
    667 	 * XXX This is however more based on empirical evidence than on
    668 	 * hard proof from the Bulk-Only spec.
    669 	 */
    670 	if (err == USBD_NORMAL_COMPLETION)
    671 		xs->error = XS_NOERROR;
    672 	else
    673 		xs->error = XS_DRIVER_STUFFUP;	/* XXX */
    674 	xs->resid = residue;
    675 
    676 	DPRINTF(UDMASS_SCSI, ("%s: umass_scsi_cmd: error = %d, resid = 0x%x\n",
    677 	    USBDEVNAME(sc->sc_dev), xs->error, xs->resid));
    678 
    679 	xs->flags |= ITSDONE;
    680 	scsipi_done(xs);
    681 
    682 	/*
    683 	 * XXXJRT We must return successfully queued if we're an
    684 	 * XXXJRT `asynchronous' command, otherwise `xs' will be
    685 	 * XXXJRT freed twice: once in scsipi_done(), and once in
    686 	 * XXXJRT scsi_scsipi_cmd().
    687 	 */
    688 	if (SCSIPI_XFER_ASYNC(xs))
    689 		return (SUCCESSFULLY_QUEUED);
    690 
    691 	return (COMPLETE);
    692 }
    693 
    694 void
    695 umass_minphys(bp)
    696 	struct buf *bp;
    697 {
    698 
    699 	/* No limit here. */
    700 	minphys(bp);
    701 }
    702