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