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