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