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