xref: /openbsd-src/sys/dev/usb/umass.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: umass.c,v 1.58 2008/09/25 11:07:12 krw Exp $ */
2 /*	$NetBSD: umass.c,v 1.116 2004/06/30 05:53:46 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 2003 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Charles M. Hannum.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
35  *		      Nick Hibma <n_hibma@freebsd.org>
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *     $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
60  */
61 
62 /*
63  * Universal Serial Bus Mass Storage Class specs:
64  * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
65  * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
66  * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
67  * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
68  */
69 
70 /*
71  * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
72  * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
73  */
74 
75 /*
76  * The driver handles 3 Wire Protocols
77  * - Command/Bulk/Interrupt (CBI)
78  * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
79  * - Mass Storage Bulk-Only (BBB)
80  *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
81  *
82  * Over these wire protocols it handles the following command protocols
83  * - SCSI
84  * - 8070 (ATA/ATAPI for rewritable removable media)
85  * - UFI (USB Floppy Interface)
86  *
87  * 8070i is a transformed version of the SCSI command set. UFI is a transformed
88  * version of the 8070i command set.  The sc->transform method is used to
89  * convert the commands into the appropriate format (if at all necessary).
90  * For example, ATAPI requires all commands to be 12 bytes in length amongst
91  * other things.
92  *
93  * The source code below is marked and can be split into a number of pieces
94  * (in this order):
95  *
96  * - probe/attach/detach
97  * - generic transfer routines
98  * - BBB
99  * - CBI
100  * - CBI_I (in addition to functions from CBI)
101  * - CAM (Common Access Method)
102  * - SCSI
103  * - UFI
104  * - 8070i
105  *
106  * The protocols are implemented using a state machine, for the transfers as
107  * well as for the resets. The state machine is contained in umass_*_state.
108  * The state machine is started through either umass_*_transfer or
109  * umass_*_reset.
110  *
111  * The reason for doing this is a) CAM performs a lot better this way and b) it
112  * avoids using tsleep from interrupt context (for example after a failed
113  * transfer).
114  */
115 
116 /*
117  * The SCSI related part of this driver has been derived from the
118  * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
119  *
120  * The CAM layer uses so called actions which are messages sent to the host
121  * adapter for completion. The actions come in through umass_cam_action. The
122  * appropriate block of routines is called depending on the transport protocol
123  * in use. When the transfer has finished, these routines call
124  * umass_cam_cb again to complete the CAM command.
125  */
126 
127 #include "atapiscsi.h"
128 
129 #include <sys/param.h>
130 #include <sys/systm.h>
131 #include <sys/kernel.h>
132 #include <sys/conf.h>
133 #include <sys/buf.h>
134 #include <sys/device.h>
135 #include <sys/malloc.h>
136 #include <sys/timeout.h>
137 #undef KASSERT
138 #define KASSERT(cond, msg)
139 #include <machine/bus.h>
140 
141 #include <scsi/scsi_all.h>
142 
143 #include <dev/usb/usb.h>
144 #include <dev/usb/usbdi.h>
145 #include <dev/usb/usbdi_util.h>
146 #include <dev/usb/usbdivar.h>
147 #include <dev/usb/usbdevs.h>
148 
149 #include <dev/usb/umassvar.h>
150 #include <dev/usb/umass_quirks.h>
151 #include <dev/usb/umass_scsi.h>
152 
153 
154 #ifdef UMASS_DEBUG
155 int umassdebug = 0;
156 
157 char *states[TSTATE_STATES+1] = {
158 	/* should be kept in sync with the list at transfer_state */
159 	"Idle",
160 	"BBB CBW",
161 	"BBB Data",
162 	"BBB Data bulk-in/-out clear stall",
163 	"BBB CSW, 1st attempt",
164 	"BBB CSW bulk-in clear stall",
165 	"BBB CSW, 2nd attempt",
166 	"BBB Reset",
167 	"BBB bulk-in clear stall",
168 	"BBB bulk-out clear stall",
169 	"CBI Command",
170 	"CBI Data",
171 	"CBI Status",
172 	"CBI Data bulk-in/-out clear stall",
173 	"CBI Status intr-in clear stall",
174 	"CBI Reset",
175 	"CBI bulk-in clear stall",
176 	"CBI bulk-out clear stall",
177 	NULL
178 };
179 #endif
180 
181 /* USB device probe/attach/detach functions */
182 int umass_match(struct device *, void *, void *);
183 void umass_attach(struct device *, struct device *, void *);
184 int umass_detach(struct device *, int);
185 int umass_activate(struct device *, enum devact);
186 
187 struct cfdriver umass_cd = {
188 	NULL, "umass", DV_DULL
189 };
190 
191 const struct cfattach umass_ca = {
192 	sizeof(struct umass_softc),
193 	umass_match,
194 	umass_attach,
195 	umass_detach,
196 	umass_activate,
197 };
198 void umass_disco(struct umass_softc *sc);
199 
200 /* generic transfer functions */
201 usbd_status umass_polled_transfer(struct umass_softc *sc,
202 				usbd_xfer_handle xfer);
203 usbd_status umass_setup_transfer(struct umass_softc *sc,
204 				usbd_pipe_handle pipe,
205 				void *buffer, int buflen, int flags,
206 				usbd_xfer_handle xfer);
207 usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc,
208 				usb_device_request_t *req,
209 				void *buffer, int buflen, int flags,
210 				usbd_xfer_handle xfer);
211 void umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
212 				usbd_xfer_handle xfer);
213 void umass_adjust_transfer(struct umass_softc *);
214 #if 0
215 void umass_reset(struct umass_softc *sc,	transfer_cb_f cb, void *priv);
216 #endif
217 
218 /* Bulk-Only related functions */
219 void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *,
220 			       int, int, u_int, umass_callback, void *);
221 void umass_bbb_reset(struct umass_softc *, int);
222 void umass_bbb_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
223 
224 u_int8_t umass_bbb_get_max_lun(struct umass_softc *);
225 
226 /* CBI related functions */
227 void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *,
228 			       int, int, u_int, umass_callback, void *);
229 void umass_cbi_reset(struct umass_softc *, int);
230 void umass_cbi_state(usbd_xfer_handle, usbd_private_handle, usbd_status);
231 
232 int umass_cbi_adsc(struct umass_softc *, char *, int, usbd_xfer_handle);
233 
234 const struct umass_wire_methods umass_bbb_methods = {
235 	umass_bbb_transfer,
236 	umass_bbb_reset,
237 	umass_bbb_state
238 };
239 
240 const struct umass_wire_methods umass_cbi_methods = {
241 	umass_cbi_transfer,
242 	umass_cbi_reset,
243 	umass_cbi_state
244 };
245 
246 #ifdef UMASS_DEBUG
247 /* General debugging functions */
248 void umass_bbb_dump_cbw(struct umass_softc *sc,
249 				umass_bbb_cbw_t *cbw);
250 void umass_bbb_dump_csw(struct umass_softc *sc,
251 				umass_bbb_csw_t *csw);
252 void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer,
253 				int buflen, int printlen);
254 #endif
255 
256 
257 /*
258  * USB device probe/attach/detach
259  */
260 
261 int
262 umass_match(struct device *parent, void *match, void *aux)
263 {
264 	struct usb_attach_arg *uaa = aux;
265 	const struct umass_quirk *quirk;
266 	usb_interface_descriptor_t *id;
267 
268 	if (uaa->iface == NULL)
269 		return (UMATCH_NONE);
270 
271 	quirk = umass_lookup(uaa->vendor, uaa->product);
272 	if (quirk != NULL)
273 		return (quirk->uq_match);
274 
275 	id = usbd_get_interface_descriptor(uaa->iface);
276 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
277 		return (UMATCH_NONE);
278 
279 	switch (id->bInterfaceSubClass) {
280 	case UISUBCLASS_RBC:
281 	case UISUBCLASS_SFF8020I:
282 	case UISUBCLASS_QIC157:
283 	case UISUBCLASS_UFI:
284 	case UISUBCLASS_SFF8070I:
285 	case UISUBCLASS_SCSI:
286 		break;
287 	default:
288 		return (UMATCH_IFACECLASS);
289 	}
290 
291 	switch (id->bInterfaceProtocol) {
292 	case UIPROTO_MASS_CBI_I:
293 	case UIPROTO_MASS_CBI:
294 	case UIPROTO_MASS_BBB_OLD:
295 	case UIPROTO_MASS_BBB:
296 		break;
297 	default:
298 		return (UMATCH_IFACECLASS_IFACESUBCLASS);
299 	}
300 
301 	return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
302 }
303 
304 void
305 umass_attach(struct device *parent, struct device *self, void *aux)
306 {
307 	struct umass_softc *sc = (struct umass_softc *)self;
308 	struct usb_attach_arg *uaa = aux;
309 	const struct umass_quirk *quirk;
310 	usb_interface_descriptor_t *id;
311 	usb_endpoint_descriptor_t *ed;
312 	const char *sWire, *sCommand;
313 	usbd_status err;
314 	int i, bno, error;
315 
316 	sc->sc_udev = uaa->device;
317 	sc->sc_iface = uaa->iface;
318 	sc->sc_ifaceno = uaa->ifaceno;
319 
320 	quirk = umass_lookup(uaa->vendor, uaa->product);
321 	if (quirk != NULL) {
322 		sc->sc_wire = quirk->uq_wire;
323 		sc->sc_cmd = quirk->uq_cmd;
324 		sc->sc_quirks = quirk->uq_flags;
325 		sc->sc_busquirks = quirk->uq_busquirks;
326 
327 		if (quirk->uq_fixup != NULL)
328 			(*quirk->uq_fixup)(sc);
329 	} else {
330 		sc->sc_wire = UMASS_WPROTO_UNSPEC;
331 		sc->sc_cmd = UMASS_CPROTO_UNSPEC;
332 		sc->sc_quirks = 0;
333 		sc->sc_busquirks = 0;
334 	}
335 
336 	id = usbd_get_interface_descriptor(sc->sc_iface);
337 	if (id == NULL)
338 		return;
339 
340 	if (sc->sc_wire == UMASS_WPROTO_UNSPEC) {
341 		switch (id->bInterfaceProtocol) {
342 		case UIPROTO_MASS_CBI:
343 			sc->sc_wire = UMASS_WPROTO_CBI;
344 			break;
345 		case UIPROTO_MASS_CBI_I:
346 			sc->sc_wire = UMASS_WPROTO_CBI_I;
347 			break;
348 		case UIPROTO_MASS_BBB:
349 		case UIPROTO_MASS_BBB_OLD:
350 			sc->sc_wire = UMASS_WPROTO_BBB;
351 			break;
352 		default:
353 			DPRINTF(UDMASS_GEN,
354 				("%s: Unsupported wire protocol %u\n",
355 				sc->sc_dev.dv_xname,
356 				id->bInterfaceProtocol));
357 			return;
358 		}
359 	}
360 
361 	if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) {
362 		switch (id->bInterfaceSubClass) {
363 		case UISUBCLASS_SCSI:
364 			sc->sc_cmd = UMASS_CPROTO_SCSI;
365 			break;
366 		case UISUBCLASS_UFI:
367 			sc->sc_cmd = UMASS_CPROTO_UFI;
368 			break;
369 		case UISUBCLASS_SFF8020I:
370 		case UISUBCLASS_SFF8070I:
371 		case UISUBCLASS_QIC157:
372 			sc->sc_cmd = UMASS_CPROTO_ATAPI;
373 			break;
374 		case UISUBCLASS_RBC:
375 			sc->sc_cmd = UMASS_CPROTO_RBC;
376 			break;
377 		default:
378 			DPRINTF(UDMASS_GEN,
379 				("%s: Unsupported command protocol %u\n",
380 				sc->sc_dev.dv_xname,
381 				id->bInterfaceSubClass));
382 			return;
383 		}
384 	}
385 
386 	switch (sc->sc_wire) {
387 	case UMASS_WPROTO_CBI:
388 		sWire = "CBI";
389 		break;
390 	case UMASS_WPROTO_CBI_I:
391 		sWire = "CBI with CCI";
392 		break;
393 	case UMASS_WPROTO_BBB:
394 		sWire = "Bulk-Only";
395 		break;
396 	default:
397 		sWire = "unknown";
398 		break;
399 	}
400 
401 	switch (sc->sc_cmd) {
402 	case UMASS_CPROTO_RBC:
403 		sCommand = "RBC";
404 		break;
405 	case UMASS_CPROTO_SCSI:
406 		sCommand = "SCSI";
407 		break;
408 	case UMASS_CPROTO_UFI:
409 		sCommand = "UFI";
410 		break;
411 	case UMASS_CPROTO_ATAPI:
412 		sCommand = "ATAPI";
413 		break;
414 	case UMASS_CPROTO_ISD_ATA:
415 		sCommand = "ISD-ATA";
416 		break;
417 	default:
418 		sCommand = "unknown";
419 		break;
420 	}
421 
422 	printf("%s: using %s over %s\n", sc->sc_dev.dv_xname, sCommand,
423 	       sWire);
424 
425 	if (quirk != NULL && quirk->uq_init != NULL) {
426 		err = (*quirk->uq_init)(sc);
427 		if (err) {
428 			umass_disco(sc);
429 			return;
430 		}
431 	}
432 
433 	/*
434 	 * In addition to the Control endpoint the following endpoints
435 	 * are required:
436 	 * a) bulk-in endpoint.
437 	 * b) bulk-out endpoint.
438 	 * and for Control/Bulk/Interrupt with CCI (CBI_I)
439 	 * c) intr-in
440 	 *
441 	 * The endpoint addresses are not fixed, so we have to read them
442 	 * from the device descriptors of the current interface.
443 	 */
444 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
445 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
446 		if (ed == NULL) {
447 			printf("%s: could not read endpoint descriptor\n",
448 			       sc->sc_dev.dv_xname);
449 			return;
450 		}
451 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
452 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
453 			sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress;
454 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
455 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
456 			sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress;
457 		} else if (sc->sc_wire == UMASS_WPROTO_CBI_I
458 		    && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
459 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
460 			sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress;
461 #ifdef UMASS_DEBUG
462 			if (UGETW(ed->wMaxPacketSize) > 2) {
463 				DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
464 					sc->sc_dev.dv_xname,
465 					UGETW(ed->wMaxPacketSize)));
466 			}
467 #endif
468 		}
469 	}
470 
471 	/* check whether we found all the endpoints we need */
472 	if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] ||
473 	    (sc->sc_wire == UMASS_WPROTO_CBI_I &&
474 	     !sc->sc_epaddr[UMASS_INTRIN])) {
475 		DPRINTF(UDMASS_USB, ("%s: endpoint not found %u/%u/%u\n",
476 			sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKIN],
477 			sc->sc_epaddr[UMASS_BULKOUT],
478 			sc->sc_epaddr[UMASS_INTRIN]));
479 		return;
480 	}
481 
482 	/*
483 	 * Get the maximum LUN supported by the device.
484 	 */
485 	if (sc->sc_wire == UMASS_WPROTO_BBB) {
486 		sc->maxlun = umass_bbb_get_max_lun(sc);
487 	} else {
488 		sc->maxlun = 0;
489 	}
490 
491 	/* Open the bulk-in and -out pipe */
492 	DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKOUT\n",
493 	    sc->sc_dev.dv_xname, sc->sc_iface,
494 	    sc->sc_epaddr[UMASS_BULKOUT]));
495 	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT],
496 				USBD_EXCLUSIVE_USE,
497 				&sc->sc_pipe[UMASS_BULKOUT]);
498 	if (err) {
499 		DPRINTF(UDMASS_USB, ("%s: cannot open %u-out pipe (bulk)\n",
500 			sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKOUT]));
501 		umass_disco(sc);
502 		return;
503 	}
504 	DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKIN\n",
505 	    sc->sc_dev.dv_xname, sc->sc_iface,
506 	    sc->sc_epaddr[UMASS_BULKIN]));
507 	err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN],
508 				USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_BULKIN]);
509 	if (err) {
510 		DPRINTF(UDMASS_USB, ("%s: could not open %u-in pipe (bulk)\n",
511 			sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKIN]));
512 		umass_disco(sc);
513 		return;
514 	}
515 	/*
516 	 * Open the intr-in pipe if the protocol is CBI with CCI.
517 	 * Note: early versions of the Zip drive do have an interrupt pipe, but
518 	 * this pipe is unused
519 	 *
520 	 * We do not open the interrupt pipe as an interrupt pipe, but as a
521 	 * normal bulk endpoint. We send an IN transfer down the wire at the
522 	 * appropriate time, because we know exactly when to expect data on
523 	 * that endpoint. This saves bandwidth, but more important, makes the
524 	 * code for handling the data on that endpoint simpler. No data
525 	 * arriving concurrently.
526 	 */
527 	if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
528 		DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for INTRIN\n",
529 		    sc->sc_dev.dv_xname, sc->sc_iface,
530 		    sc->sc_epaddr[UMASS_INTRIN]));
531 		err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN],
532 				USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_INTRIN]);
533 		if (err) {
534 			DPRINTF(UDMASS_USB, ("%s: couldn't open %u-in (intr)\n",
535 				sc->sc_dev.dv_xname,
536 				sc->sc_epaddr[UMASS_INTRIN]));
537 			umass_disco(sc);
538 			return;
539 		}
540 	}
541 
542 	/* initialisation of generic part */
543 	sc->transfer_state = TSTATE_IDLE;
544 
545 	/* request a sufficient number of xfer handles */
546 	for (i = 0; i < XFER_NR; i++) {
547 		sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
548 		if (sc->transfer_xfer[i] == NULL) {
549 			DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
550 				sc->sc_dev.dv_xname));
551 			umass_disco(sc);
552 			return;
553 		}
554 	}
555 	/* Allocate buffer for data transfer (it's huge). */
556 	switch (sc->sc_wire) {
557 	case UMASS_WPROTO_BBB:
558 		bno = XFER_BBB_DATA;
559 		goto dalloc;
560 	case UMASS_WPROTO_CBI:
561 		bno = XFER_CBI_DATA;
562 		goto dalloc;
563 	case UMASS_WPROTO_CBI_I:
564 		bno = XFER_CBI_DATA;
565 	dalloc:
566 		sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
567 						    UMASS_MAX_TRANSFER_SIZE);
568 		if (sc->data_buffer == NULL) {
569 			umass_disco(sc);
570 			return;
571 		}
572 		break;
573 	default:
574 		break;
575 	}
576 
577 	/* Initialise the wire protocol specific methods */
578 	switch (sc->sc_wire) {
579 	case UMASS_WPROTO_BBB:
580 		sc->sc_methods = &umass_bbb_methods;
581 		break;
582 	case UMASS_WPROTO_CBI:
583 	case UMASS_WPROTO_CBI_I:
584 		sc->sc_methods = &umass_cbi_methods;
585 		break;
586 	default:
587 		umass_disco(sc);
588 		return;
589 	}
590 
591 	error = 0;
592 	switch (sc->sc_cmd) {
593 	case UMASS_CPROTO_RBC:
594 	case UMASS_CPROTO_SCSI:
595 		error = umass_scsi_attach(sc);
596 		break;
597 
598 	case UMASS_CPROTO_UFI:
599 	case UMASS_CPROTO_ATAPI:
600 #if (NATAPIBUS > 0) || (NATAPISCSI > 0)
601 		error = umass_atapi_attach(sc);
602 #else
603 		printf("%s: atapiscsi not configured\n", sc->sc_dev.dv_xname);
604 #endif
605 		break;
606 
607 	case UMASS_CPROTO_ISD_ATA:
608 		printf("%s: isdata not configured\n", sc->sc_dev.dv_xname);
609 		break;
610 
611 	default:
612 		printf("%s: command protocol=0x%x not supported\n",
613 		       sc->sc_dev.dv_xname, sc->sc_cmd);
614 		umass_disco(sc);
615 		return;
616 	}
617 	if (error) {
618 		printf("%s: bus attach failed\n", sc->sc_dev.dv_xname);
619 		umass_disco(sc);
620 		return;
621 	}
622 
623 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
624 			   &sc->sc_dev);
625 
626 	DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", sc->sc_dev.dv_xname));
627 }
628 
629 int
630 umass_detach(struct device *self, int flags)
631 {
632 	struct umass_softc *sc = (struct umass_softc *)self;
633 	struct umassbus_softc *scbus;
634 	int rv = 0, i, s;
635 
636 	DPRINTF(UDMASS_USB, ("%s: detached\n", sc->sc_dev.dv_xname));
637 
638 	/* Abort the pipes to wake up any waiting processes. */
639 	for (i = 0 ; i < UMASS_NEP ; i++) {
640 		if (sc->sc_pipe[i] != NULL)
641 			usbd_abort_pipe(sc->sc_pipe[i]);
642 	}
643 
644 	/* Do we really need reference counting?  Perhaps in ioctl() */
645 	s = splusb();
646 	if (--sc->sc_refcnt >= 0) {
647 #ifdef DIAGNOSTIC
648 		printf("%s: waiting for refcnt\n", sc->sc_dev.dv_xname);
649 #endif
650 		/* Wait for processes to go away. */
651 		usb_detach_wait(&sc->sc_dev);
652 	}
653 
654 	/* Free the buffers via callback. */
655 	if (sc->transfer_state != TSTATE_IDLE && sc->transfer_priv) {
656 		sc->transfer_state = TSTATE_IDLE;
657 		sc->transfer_cb(sc, sc->transfer_priv,
658 				sc->transfer_datalen,
659 				STATUS_WIRE_FAILED);
660 		sc->transfer_priv = NULL;
661 	}
662 	splx(s);
663 
664 	scbus = sc->bus;
665 	if (scbus != NULL) {
666 		if (scbus->sc_child != NULL)
667 			rv = config_detach(scbus->sc_child, flags);
668 		free(scbus, M_DEVBUF);
669 		sc->bus = NULL;
670 	}
671 
672 	if (rv != 0)
673 		return (rv);
674 
675 	umass_disco(sc);
676 
677 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
678 			   &sc->sc_dev);
679 
680 	return (rv);
681 }
682 
683 int
684 umass_activate(struct device *dev, enum devact act)
685 {
686 	struct umass_softc *sc = (struct umass_softc *)dev;
687 	struct umassbus_softc *scbus = sc->bus;
688 	int rv = 0;
689 
690 	DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
691 	    sc->sc_dev.dv_xname, act));
692 
693 	switch (act) {
694 	case DVACT_ACTIVATE:
695 		break;
696 
697 	case DVACT_DEACTIVATE:
698 		sc->sc_dying = 1;
699 		if (scbus == NULL || scbus->sc_child == NULL)
700 			break;
701 		rv = config_deactivate(scbus->sc_child);
702 		DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
703 		    "returned %d\n", sc->sc_dev.dv_xname, rv));
704 		break;
705 	}
706 	return (rv);
707 }
708 
709 void
710 umass_disco(struct umass_softc *sc)
711 {
712 	int i;
713 
714 	DPRINTF(UDMASS_GEN, ("umass_disco\n"));
715 
716 	/* Free the xfers. */
717 	for (i = 0; i < XFER_NR; i++)
718 		if (sc->transfer_xfer[i] != NULL) {
719 			usbd_free_xfer(sc->transfer_xfer[i]);
720 			sc->transfer_xfer[i] = NULL;
721 		}
722 
723 	/* Remove all the pipes. */
724 	for (i = 0 ; i < UMASS_NEP ; i++) {
725 		if (sc->sc_pipe[i] != NULL) {
726 			usbd_close_pipe(sc->sc_pipe[i]);
727 			sc->sc_pipe[i] = NULL;
728 		}
729 	}
730 }
731 
732 /*
733  * Generic functions to handle transfers
734  */
735 
736 usbd_status
737 umass_polled_transfer(struct umass_softc *sc, usbd_xfer_handle xfer)
738 {
739 	usbd_status err;
740 
741 	if (sc->sc_dying)
742 		return (USBD_IOERROR);
743 
744 	/*
745 	 * If a polled transfer is already in progress, preserve the new
746 	 * usbd_xfer_handle and run it after the running one completes.
747 	 * This converts the recursive calls into the umass_*_state callbacks
748 	 * into iteration, preventing us from running out of stack under
749 	 * error conditions.
750 	 */
751 	if (sc->polling_depth) {
752 		if (sc->next_polled_xfer)
753 			panic("%s: got polled xfer %p, but %p already "
754 			    "pending\n", sc->sc_dev.dv_xname, xfer,
755 			    sc->next_polled_xfer);
756 
757 		DPRINTF(UDMASS_XFER, ("%s: saving polled xfer %p\n",
758 		    sc->sc_dev.dv_xname, xfer));
759 		sc->next_polled_xfer = xfer;
760 
761 		return (USBD_IN_PROGRESS);
762 	}
763 
764 	sc->polling_depth++;
765 
766 start_next_xfer:
767 	DPRINTF(UDMASS_XFER, ("%s: start polled xfer %p\n",
768 	    sc->sc_dev.dv_xname, xfer));
769 	err = usbd_transfer(xfer);
770 	if (err && err != USBD_IN_PROGRESS && sc->next_polled_xfer == NULL) {
771 		DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
772 		    sc->sc_dev.dv_xname, usbd_errstr(err)));
773 		sc->polling_depth--;
774 		return (err);
775 	}
776 
777 	if (err && err != USBD_IN_PROGRESS) {
778 		DPRINTF(UDMASS_XFER, ("umass_polled_xfer %p has error %s\n",
779 		    xfer, usbd_errstr(err)));
780 	}
781 
782 	if (sc->next_polled_xfer != NULL) {
783 		DPRINTF(UDMASS_XFER, ("umass_polled_xfer running next "
784 		    "transaction %p\n", sc->next_polled_xfer));
785 		xfer = sc->next_polled_xfer;
786 		sc->next_polled_xfer = NULL;
787 		goto start_next_xfer;
788 	}
789 
790 	sc->polling_depth--;
791 
792 	return (USBD_NORMAL_COMPLETION);
793 }
794 
795 usbd_status
796 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
797 			void *buffer, int buflen, int flags,
798 			usbd_xfer_handle xfer)
799 {
800 	usbd_status err;
801 
802 	if (sc->sc_dying)
803 		return (USBD_IOERROR);
804 
805 	/* Initialise a USB transfer and then schedule it */
806 
807 	usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
808 	    flags | sc->sc_xfer_flags, sc->timeout, sc->sc_methods->wire_state);
809 
810 	if (sc->sc_udev->bus->use_polling) {
811 		DPRINTF(UDMASS_XFER,("%s: start polled xfer buffer=%p "
812 		    "buflen=%d flags=0x%x timeout=%d\n", sc->sc_dev.dv_xname,
813 		    buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
814 		err = umass_polled_transfer(sc, xfer);
815 	} else {
816 		err = usbd_transfer(xfer);
817 		DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d "
818 		    "flags=0x%x timeout=%d\n", sc->sc_dev.dv_xname,
819 		    buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
820 	}
821 	if (err && err != USBD_IN_PROGRESS) {
822 		DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
823 			sc->sc_dev.dv_xname, usbd_errstr(err)));
824 		return (err);
825 	}
826 
827 	return (USBD_NORMAL_COMPLETION);
828 }
829 
830 
831 usbd_status
832 umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req,
833 	 void *buffer, int buflen, int flags, usbd_xfer_handle xfer)
834 {
835 	usbd_status err;
836 
837 	if (sc->sc_dying)
838 		return (USBD_IOERROR);
839 
840 	/* Initialise a USB control transfer and then schedule it */
841 
842 	usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc,
843 	    USBD_DEFAULT_TIMEOUT, req, buffer, buflen, flags,
844 	    sc->sc_methods->wire_state);
845 
846 	if (sc->sc_udev->bus->use_polling) {
847 		DPRINTF(UDMASS_XFER,("%s: start polled ctrl xfer buffer=%p "
848 		    "buflen=%d flags=0x%x\n", sc->sc_dev.dv_xname, buffer,
849 		    buflen, flags));
850 		err = umass_polled_transfer(sc, xfer);
851 	} else {
852 		DPRINTF(UDMASS_XFER,("%s: start ctrl xfer buffer=%p buflen=%d "
853 		    "flags=0x%x\n", sc->sc_dev.dv_xname, buffer, buflen,
854 		    flags));
855 		err = usbd_transfer(xfer);
856 	}
857 	if (err && err != USBD_IN_PROGRESS) {
858 		DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
859 			 sc->sc_dev.dv_xname, usbd_errstr(err)));
860 
861 		/* do not reset, as this would make us loop */
862 		return (err);
863 	}
864 
865 	return (USBD_NORMAL_COMPLETION);
866 }
867 
868 void
869 umass_adjust_transfer(struct umass_softc *sc)
870 {
871 	switch (sc->sc_cmd) {
872 	case UMASS_CPROTO_UFI:
873 		sc->cbw.bCDBLength = UFI_COMMAND_LENGTH;
874 		/* Adjust the length field in certain scsi commands. */
875 		switch (sc->cbw.CBWCDB[0]) {
876 		case INQUIRY:
877 			if (sc->transfer_datalen > 36) {
878 				sc->transfer_datalen = 36;
879 				sc->cbw.CBWCDB[4] = 36;
880 			}
881 			break;
882 		case MODE_SENSE_BIG:
883 			if (sc->transfer_datalen > 8) {
884 				sc->transfer_datalen = 8;
885 				sc->cbw.CBWCDB[7] = 0;
886 				sc->cbw.CBWCDB[8] = 8;
887 			}
888 			break;
889 		case REQUEST_SENSE:
890 			if (sc->transfer_datalen > 18) {
891 				sc->transfer_datalen = 18;
892 				sc->cbw.CBWCDB[4] = 18;
893 			}
894 			break;
895 		}
896 		break;
897 	case UMASS_CPROTO_ATAPI:
898 		sc->cbw.bCDBLength = UFI_COMMAND_LENGTH;
899 		break;
900 	}
901 }
902 
903 void
904 umass_clear_endpoint_stall(struct umass_softc *sc, int endpt,
905 	usbd_xfer_handle xfer)
906 {
907 	if (sc->sc_dying)
908 		return;
909 
910 	DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
911 		sc->sc_dev.dv_xname, sc->sc_epaddr[endpt]));
912 
913 	usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]);
914 
915 	sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT;
916 	sc->sc_req.bRequest = UR_CLEAR_FEATURE;
917 	USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT);
918 	USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]);
919 	USETW(sc->sc_req.wLength, 0);
920 	umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer);
921 }
922 
923 #if 0
924 void
925 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
926 {
927 	sc->transfer_cb = cb;
928 	sc->transfer_priv = priv;
929 
930 	/* The reset is a forced reset, so no error (yet) */
931 	sc->reset(sc, STATUS_CMD_OK);
932 }
933 #endif
934 
935 /*
936  * Bulk protocol specific functions
937  */
938 
939 void
940 umass_bbb_reset(struct umass_softc *sc, int status)
941 {
942 	KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
943 		("sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
944 		sc->sc_wire));
945 
946 	if (sc->sc_dying)
947 		return;
948 
949 	/*
950 	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
951 	 *
952 	 * For Reset Recovery the host shall issue in the following order:
953 	 * a) a Bulk-Only Mass Storage Reset
954 	 * b) a Clear Feature HALT to the Bulk-In endpoint
955 	 * c) a Clear Feature HALT to the Bulk-Out endpoint
956 	 *
957 	 * This is done in 3 steps, states:
958 	 * TSTATE_BBB_RESET1
959 	 * TSTATE_BBB_RESET2
960 	 * TSTATE_BBB_RESET3
961 	 *
962 	 * If the reset doesn't succeed, the device should be port reset.
963 	 */
964 
965 	DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
966 		sc->sc_dev.dv_xname));
967 
968 	sc->transfer_state = TSTATE_BBB_RESET1;
969 	sc->transfer_status = status;
970 
971 	/* reset is a class specific interface write */
972 	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
973 	sc->sc_req.bRequest = UR_BBB_RESET;
974 	USETW(sc->sc_req.wValue, 0);
975 	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
976 	USETW(sc->sc_req.wLength, 0);
977 	umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0,
978 				  sc->transfer_xfer[XFER_BBB_RESET1]);
979 }
980 
981 void
982 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
983 		   void *data, int datalen, int dir, u_int timeout,
984 		   umass_callback cb, void *priv)
985 {
986 	static int dCBWtag = 42;	/* unique for CBW of transfer */
987 	usbd_status err;
988 
989 	DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
990 		sc->sc_dev.dv_xname, *(u_char *)cmd));
991 
992 	KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
993 		("sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
994 		sc->sc_wire));
995 
996 	if (sc->sc_dying) {
997 		sc->polled_xfer_status = USBD_IOERROR;
998 		return;
999 	}
1000 
1001 	/* Be a little generous. */
1002 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1003 
1004 	/*
1005 	 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
1006 	 * a data phase of datalen bytes from/to the device and finally a
1007 	 * csw read phase.
1008 	 * If the data direction was inbound a maximum of datalen bytes
1009 	 * is stored in the buffer pointed to by data.
1010 	 *
1011 	 * umass_bbb_transfer initialises the transfer and lets the state
1012 	 * machine in umass_bbb_state handle the completion. It uses the
1013 	 * following states:
1014 	 * TSTATE_BBB_COMMAND
1015 	 *   -> TSTATE_BBB_DATA
1016 	 *   -> TSTATE_BBB_STATUS
1017 	 *   -> TSTATE_BBB_STATUS2
1018 	 *   -> TSTATE_BBB_IDLE
1019 	 *
1020 	 * An error in any of those states will invoke
1021 	 * umass_bbb_reset.
1022 	 */
1023 
1024 	/* check the given arguments */
1025 	KASSERT(datalen == 0 || data != NULL,
1026 		("%s: datalen > 0, but no buffer",sc->sc_dev.dv_xname));
1027 	KASSERT(cmdlen <= CBWCDBLENGTH,
1028 		("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
1029 			sc->sc_dev.dv_xname, cmdlen, CBWCDBLENGTH));
1030 	KASSERT(dir == DIR_NONE || datalen > 0,
1031 		("%s: datalen == 0 while direction is not NONE\n",
1032 			sc->sc_dev.dv_xname));
1033 	KASSERT(datalen == 0 || dir != DIR_NONE,
1034 		("%s: direction is NONE while datalen is not zero\n",
1035 			sc->sc_dev.dv_xname));
1036 	KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
1037 		("%s: CBW struct does not have the right size (%d vs. %d)\n",
1038 			sc->sc_dev.dv_xname,
1039 			sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
1040 	KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
1041 		("%s: CSW struct does not have the right size (%d vs. %d)\n",
1042 			sc->sc_dev.dv_xname,
1043 			sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
1044 
1045 	/*
1046 	 * Determine the direction of the data transfer and the length.
1047 	 *
1048 	 * dCBWDataTransferLength (datalen) :
1049 	 *   This field indicates the number of bytes of data that the host
1050 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1051 	 *   the Direction bit) during the execution of this command. If this
1052 	 *   field is set to 0, the device will expect that no data will be
1053 	 *   transferred IN or OUT during this command, regardless of the value
1054 	 *   of the Direction bit defined in dCBWFlags.
1055 	 *
1056 	 * dCBWFlags (dir) :
1057 	 *   The bits of the Flags field are defined as follows:
1058 	 *     Bits 0-6	 reserved
1059 	 *     Bit  7	 Direction - this bit shall be ignored if the
1060 	 *			     dCBWDataTransferLength field is zero.
1061 	 *		 0 = data Out from host to device
1062 	 *		 1 = data In from device to host
1063 	 */
1064 
1065 	/* Fill in the Command Block Wrapper */
1066 	USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1067 	USETDW(sc->cbw.dCBWTag, dCBWtag);
1068 	dCBWtag++;	/* cannot be done in macro (it will be done 4 times) */
1069 	USETDW(sc->cbw.dCBWDataTransferLength, datalen);
1070 	/* DIR_NONE is treated as DIR_OUT (0x00) */
1071 	sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
1072 	sc->cbw.bCBWLUN = lun;
1073 	sc->cbw.bCDBLength = cmdlen;
1074 	bzero(sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB));
1075 	memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
1076 
1077 	DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1078 
1079 	/* store the details for the data transfer phase */
1080 	sc->transfer_dir = dir;
1081 	sc->transfer_data = data;
1082 	sc->transfer_datalen = datalen;
1083 	sc->transfer_actlen = 0;
1084 	sc->transfer_cb = cb;
1085 	sc->transfer_priv = priv;
1086 	sc->transfer_status = STATUS_CMD_OK;
1087 
1088 	/* move from idle to the command state */
1089 	sc->transfer_state = TSTATE_BBB_COMMAND;
1090 
1091 	/* Send the CBW from host to device via bulk-out endpoint. */
1092 	umass_adjust_transfer(sc);
1093 	if ((err = umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1094 			&sc->cbw, UMASS_BBB_CBW_SIZE, 0,
1095 			sc->transfer_xfer[XFER_BBB_CBW])))
1096 		umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1097 
1098 	if (sc->sc_udev->bus->use_polling)
1099 		sc->polled_xfer_status = err;
1100 }
1101 
1102 void
1103 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1104 		usbd_status err)
1105 {
1106 	struct umass_softc *sc = (struct umass_softc *) priv;
1107 	usbd_xfer_handle next_xfer;
1108 
1109 	KASSERT(sc->sc_wire & UMASS_WPROTO_BBB,
1110 		("sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
1111 		sc->sc_wire));
1112 
1113 	if (sc->sc_dying)
1114 		return;
1115 
1116 	/*
1117 	 * State handling for BBB transfers.
1118 	 *
1119 	 * The subroutine is rather long. It steps through the states given in
1120 	 * Annex A of the Bulk-Only specification.
1121 	 * Each state first does the error handling of the previous transfer
1122 	 * and then prepares the next transfer.
1123 	 * Each transfer is done asynchronously so after the request/transfer
1124 	 * has been submitted you will find a 'return;'.
1125 	 */
1126 
1127 	DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1128 		sc->sc_dev.dv_xname, sc->transfer_state,
1129 		states[sc->transfer_state], xfer, usbd_errstr(err)));
1130 
1131 	switch (sc->transfer_state) {
1132 
1133 	/***** Bulk Transfer *****/
1134 	case TSTATE_BBB_COMMAND:
1135 		/* Command transport phase, error handling */
1136 		if (err) {
1137 			DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
1138 				sc->sc_dev.dv_xname));
1139 			/* If the device detects that the CBW is invalid, then
1140 			 * the device may STALL both bulk endpoints and require
1141 			 * a Bulk-Reset
1142 			 */
1143 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1144 			return;
1145 		}
1146 
1147 		/* Data transport phase, setup transfer */
1148 		sc->transfer_state = TSTATE_BBB_DATA;
1149 		if (sc->transfer_dir == DIR_IN) {
1150 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1151 					sc->data_buffer, sc->transfer_datalen,
1152 					USBD_SHORT_XFER_OK | USBD_NO_COPY,
1153 					sc->transfer_xfer[XFER_BBB_DATA]))
1154 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1155 
1156 			return;
1157 		} else if (sc->transfer_dir == DIR_OUT) {
1158 			memcpy(sc->data_buffer, sc->transfer_data,
1159 			       sc->transfer_datalen);
1160 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1161 					sc->data_buffer, sc->transfer_datalen,
1162 					USBD_NO_COPY,/* fixed length transfer */
1163 					sc->transfer_xfer[XFER_BBB_DATA]))
1164 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1165 
1166 			return;
1167 		} else {
1168 			DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
1169 				sc->sc_dev.dv_xname));
1170 		}
1171 
1172 		/* FALLTHROUGH if no data phase, err == 0 */
1173 	case TSTATE_BBB_DATA:
1174 		/* Command transport phase error handling (ignored if no data
1175 		 * phase (fallthrough from previous state)) */
1176 		if (sc->transfer_dir != DIR_NONE) {
1177 			/* retrieve the length of the transfer that was done */
1178 			usbd_get_xfer_status(xfer, NULL, NULL,
1179 			     &sc->transfer_actlen, NULL);
1180 			DPRINTF(UDMASS_BBB, ("%s: BBB_DATA actlen=%d\n",
1181 				sc->sc_dev.dv_xname, sc->transfer_actlen));
1182 
1183 			if (err) {
1184 				DPRINTF(UDMASS_BBB, ("%s: Data-%s %d failed, "
1185 					"%s\n", sc->sc_dev.dv_xname,
1186 					(sc->transfer_dir == DIR_IN?"in":"out"),
1187 					sc->transfer_datalen,usbd_errstr(err)));
1188 
1189 				if (err == USBD_STALLED) {
1190 					sc->transfer_state = TSTATE_BBB_DCLEAR;
1191 					umass_clear_endpoint_stall(sc,
1192 					  (sc->transfer_dir == DIR_IN?
1193 					    UMASS_BULKIN:UMASS_BULKOUT),
1194 					  sc->transfer_xfer[XFER_BBB_DCLEAR]);
1195 				} else {
1196 					/* Unless the error is a pipe stall the
1197 					 * error is fatal.
1198 					 */
1199 					umass_bbb_reset(sc,STATUS_WIRE_FAILED);
1200 				}
1201 				return;
1202 			}
1203 		}
1204 
1205 		/* FALLTHROUGH, err == 0 (no data phase or successful) */
1206 	case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
1207 		if (sc->transfer_dir == DIR_IN)
1208 			memcpy(sc->transfer_data, sc->data_buffer,
1209 			       sc->transfer_actlen);
1210 
1211 		DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
1212 					umass_dump_buffer(sc, sc->transfer_data,
1213 						sc->transfer_datalen, 48));
1214 
1215 		/* FALLTHROUGH, err == 0 (no data phase or successful) */
1216 	case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
1217 		/* Reading of CSW after bulk stall condition in data phase
1218 		 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1219 		 * reading CSW (TSTATE_BBB_SCLEAR).
1220 		 * In the case of no data phase or successful data phase,
1221 		 * err == 0 and the following if block is passed.
1222 		 */
1223 		if (err) {	/* should not occur */
1224 			printf("%s: BBB bulk-%s stall clear failed, %s\n",
1225 			    sc->sc_dev.dv_xname,
1226 			    (sc->transfer_dir == DIR_IN? "in":"out"),
1227 			    usbd_errstr(err));
1228 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1229 			return;
1230 		}
1231 
1232 		/* Status transport phase, setup transfer */
1233 		if (sc->transfer_state == TSTATE_BBB_COMMAND ||
1234 		    sc->transfer_state == TSTATE_BBB_DATA ||
1235 		    sc->transfer_state == TSTATE_BBB_DCLEAR) {
1236 			/* After no data phase, successful data phase and
1237 			 * after clearing bulk-in/-out stall condition
1238 			 */
1239 			sc->transfer_state = TSTATE_BBB_STATUS1;
1240 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
1241 		} else {
1242 			/* After first attempt of fetching CSW */
1243 			sc->transfer_state = TSTATE_BBB_STATUS2;
1244 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
1245 		}
1246 
1247 		/* Read the Command Status Wrapper via bulk-in endpoint. */
1248 		if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1249 			&sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) {
1250 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1251 			return;
1252 		}
1253 
1254 		return;
1255 	case TSTATE_BBB_STATUS1:	/* first attempt */
1256 	case TSTATE_BBB_STATUS2:	/* second attempt */
1257 		/* Status transfer, error handling */
1258 		if (err) {
1259 			DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
1260 				sc->sc_dev.dv_xname, usbd_errstr(err),
1261 				(sc->transfer_state == TSTATE_BBB_STATUS1?
1262 					", retrying":"")));
1263 
1264 			/* If this was the first attempt at fetching the CSW
1265 			 * retry it, otherwise fail.
1266 			 */
1267 			if (sc->transfer_state == TSTATE_BBB_STATUS1) {
1268 				sc->transfer_state = TSTATE_BBB_SCLEAR;
1269 				umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1270 				    sc->transfer_xfer[XFER_BBB_SCLEAR]);
1271 				return;
1272 			} else {
1273 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1274 				return;
1275 			}
1276 		}
1277 
1278 		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1279 
1280 		/* Translate weird command-status signatures. */
1281 		if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) &&
1282 		    UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1)
1283 			USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1284 
1285 		/* Translate invalid command-status tags */
1286 		if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG)
1287 			USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag));
1288 
1289 		/* Check CSW and handle any error */
1290 		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1291 			/* Invalid CSW: Wrong signature or wrong tag might
1292 			 * indicate that the device is confused -> reset it.
1293 			 */
1294 			printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1295 				sc->sc_dev.dv_xname,
1296 				UGETDW(sc->csw.dCSWSignature),
1297 				CSWSIGNATURE);
1298 
1299 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1300 			return;
1301 		} else if (UGETDW(sc->csw.dCSWTag)
1302 				!= UGETDW(sc->cbw.dCBWTag)) {
1303 			printf("%s: Invalid CSW: tag %d should be %d\n",
1304 				sc->sc_dev.dv_xname,
1305 				UGETDW(sc->csw.dCSWTag),
1306 				UGETDW(sc->cbw.dCBWTag));
1307 
1308 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1309 			return;
1310 
1311 		/* CSW is valid here */
1312 		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1313 			printf("%s: Invalid CSW: status %d > %d\n",
1314 				sc->sc_dev.dv_xname,
1315 				sc->csw.bCSWStatus,
1316 				CSWSTATUS_PHASE);
1317 
1318 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1319 			return;
1320 		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1321 			printf("%s: Phase Error, residue = %d\n",
1322 				sc->sc_dev.dv_xname,
1323 				UGETDW(sc->csw.dCSWDataResidue));
1324 
1325 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1326 			return;
1327 
1328 		} else if (sc->transfer_actlen > sc->transfer_datalen) {
1329 			/* Buffer overrun! Don't let this go by unnoticed */
1330 			panic("%s: transferred %d bytes instead of %d bytes",
1331 				sc->sc_dev.dv_xname,
1332 				sc->transfer_actlen, sc->transfer_datalen);
1333 #if 0
1334 		} else if (sc->transfer_datalen - sc->transfer_actlen
1335 			   != UGETDW(sc->csw.dCSWDataResidue)) {
1336 			DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
1337 				sc->sc_dev.dv_xname,
1338 				sc->transfer_datalen - sc->transfer_actlen,
1339 				UGETDW(sc->csw.dCSWDataResidue)));
1340 
1341 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
1342 			return;
1343 #endif
1344 		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1345 			DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
1346 				sc->sc_dev.dv_xname,
1347 				UGETDW(sc->csw.dCSWDataResidue)));
1348 
1349 			/* SCSI command failed but transfer was successful */
1350 			sc->transfer_state = TSTATE_IDLE;
1351 			sc->transfer_cb(sc, sc->transfer_priv,
1352 					UGETDW(sc->csw.dCSWDataResidue),
1353 					STATUS_CMD_FAILED);
1354 
1355 			return;
1356 
1357 		} else {	/* success */
1358 			sc->transfer_state = TSTATE_IDLE;
1359 			sc->transfer_cb(sc, sc->transfer_priv,
1360 					UGETDW(sc->csw.dCSWDataResidue),
1361 					STATUS_CMD_OK);
1362 
1363 			return;
1364 		}
1365 
1366 	/***** Bulk Reset *****/
1367 	case TSTATE_BBB_RESET1:
1368 		if (err)
1369 			printf("%s: BBB reset failed, %s\n",
1370 				sc->sc_dev.dv_xname, usbd_errstr(err));
1371 
1372 		sc->transfer_state = TSTATE_BBB_RESET2;
1373 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1374 			sc->transfer_xfer[XFER_BBB_RESET2]);
1375 
1376 		return;
1377 	case TSTATE_BBB_RESET2:
1378 		if (err)	/* should not occur */
1379 			printf("%s: BBB bulk-in clear stall failed, %s\n",
1380 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1381 			/* no error recovery, otherwise we end up in a loop */
1382 
1383 		sc->transfer_state = TSTATE_BBB_RESET3;
1384 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1385 			sc->transfer_xfer[XFER_BBB_RESET3]);
1386 
1387 		return;
1388 	case TSTATE_BBB_RESET3:
1389 		if (err)	/* should not occur */
1390 			printf("%s: BBB bulk-out clear stall failed, %s\n",
1391 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1392 			/* no error recovery, otherwise we end up in a loop */
1393 
1394 		sc->transfer_state = TSTATE_IDLE;
1395 		if (sc->transfer_priv) {
1396 			sc->transfer_cb(sc, sc->transfer_priv,
1397 					sc->transfer_datalen,
1398 					sc->transfer_status);
1399 		}
1400 
1401 		return;
1402 
1403 	/***** Default *****/
1404 	default:
1405 		panic("%s: Unknown state %d",
1406 		      sc->sc_dev.dv_xname, sc->transfer_state);
1407 	}
1408 }
1409 
1410 /*
1411  * Command/Bulk/Interrupt (CBI) specific functions
1412  */
1413 
1414 int
1415 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
1416 	       usbd_xfer_handle xfer)
1417 {
1418 	KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1419 		("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1420 		sc->sc_wire));
1421 
1422 	sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1423 	sc->sc_req.bRequest = UR_CBI_ADSC;
1424 	USETW(sc->sc_req.wValue, 0);
1425 	USETW(sc->sc_req.wIndex, sc->sc_ifaceno);
1426 	USETW(sc->sc_req.wLength, buflen);
1427 	return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer,
1428 					 buflen, 0, xfer);
1429 }
1430 
1431 
1432 void
1433 umass_cbi_reset(struct umass_softc *sc, int status)
1434 {
1435 	int i;
1436 #	define SEND_DIAGNOSTIC_CMDLEN	12
1437 
1438 	KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1439 		("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1440 		sc->sc_wire));
1441 
1442 	if (sc->sc_dying)
1443 		return;
1444 
1445 	/*
1446 	 * Command Block Reset Protocol
1447 	 *
1448 	 * First send a reset request to the device. Then clear
1449 	 * any possibly stalled bulk endpoints.
1450 
1451 	 * This is done in 3 steps, states:
1452 	 * TSTATE_CBI_RESET1
1453 	 * TSTATE_CBI_RESET2
1454 	 * TSTATE_CBI_RESET3
1455 	 *
1456 	 * If the reset doesn't succeed, the device should be port reset.
1457 	 */
1458 
1459 	DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
1460 		sc->sc_dev.dv_xname));
1461 
1462 	KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
1463 		("%s: CBL struct is too small (%d < %d)\n",
1464 			sc->sc_dev.dv_xname,
1465 			sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
1466 
1467 	sc->transfer_state = TSTATE_CBI_RESET1;
1468 	sc->transfer_status = status;
1469 
1470 	/* The 0x1d code is the SEND DIAGNOSTIC command. To distinguish between
1471 	 * the two the last 10 bytes of the cbl is filled with 0xff (section
1472 	 * 2.2 of the CBI spec).
1473 	 */
1474 	sc->cbl[0] = 0x1d;	/* Command Block Reset */
1475 	sc->cbl[1] = 0x04;
1476 	for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
1477 		sc->cbl[i] = 0xff;
1478 
1479 	umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
1480 		       sc->transfer_xfer[XFER_CBI_RESET1]);
1481 	/* XXX if the command fails we should reset the port on the bub */
1482 }
1483 
1484 void
1485 umass_cbi_transfer(struct umass_softc *sc, int lun,
1486 		   void *cmd, int cmdlen, void *data, int datalen, int dir,
1487 		   u_int timeout, umass_callback cb, void *priv)
1488 {
1489 	usbd_status err;
1490 
1491 	DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1492 		sc->sc_dev.dv_xname, *(u_char *)cmd, datalen));
1493 
1494 	KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1495 		("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1496 		sc->sc_wire));
1497 
1498 	if (sc->sc_dying) {
1499 		sc->polled_xfer_status = USBD_IOERROR;
1500 		return;
1501 	}
1502 
1503 	/* Be a little generous. */
1504 	sc->timeout = timeout + USBD_DEFAULT_TIMEOUT;
1505 
1506 	/*
1507 	 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1508 	 * a data phase of datalen bytes from/to the device and finally a
1509 	 * csw read phase.
1510 	 * If the data direction was inbound a maximum of datalen bytes
1511 	 * is stored in the buffer pointed to by data.
1512 	 *
1513 	 * umass_cbi_transfer initialises the transfer and lets the state
1514 	 * machine in umass_cbi_state handle the completion. It uses the
1515 	 * following states:
1516 	 * TSTATE_CBI_COMMAND
1517 	 *   -> XXX fill in
1518 	 *
1519 	 * An error in any of those states will invoke
1520 	 * umass_cbi_reset.
1521 	 */
1522 
1523 	/* check the given arguments */
1524 	KASSERT(datalen == 0 || data != NULL,
1525 		("%s: datalen > 0, but no buffer",sc->sc_dev.dv_xname));
1526 	KASSERT(datalen == 0 || dir != DIR_NONE,
1527 		("%s: direction is NONE while datalen is not zero\n",
1528 			sc->sc_dev.dv_xname));
1529 
1530 	/* store the details for the data transfer phase */
1531 	sc->transfer_dir = dir;
1532 	sc->transfer_data = data;
1533 	sc->transfer_datalen = datalen;
1534 	sc->transfer_actlen = 0;
1535 	sc->transfer_cb = cb;
1536 	sc->transfer_priv = priv;
1537 	sc->transfer_status = STATUS_CMD_OK;
1538 
1539 	/* move from idle to the command state */
1540 	sc->transfer_state = TSTATE_CBI_COMMAND;
1541 
1542 	/* Send the Command Block from host to device via control endpoint. */
1543 	sc->cbw.bCDBLength = cmdlen;
1544 	bzero(sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB));
1545 	memcpy(sc->cbw.CBWCDB, cmd, cmdlen);
1546 	umass_adjust_transfer(sc);
1547 	if ((err = umass_cbi_adsc(sc, (void *)sc->cbw.CBWCDB, sc->cbw.bCDBLength,
1548 	    sc->transfer_xfer[XFER_CBI_CB])))
1549 		umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1550 
1551 	if (sc->sc_udev->bus->use_polling)
1552 		sc->polled_xfer_status = err;
1553 }
1554 
1555 void
1556 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
1557 		usbd_status err)
1558 {
1559 	struct umass_softc *sc = (struct umass_softc *) priv;
1560 
1561 	KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I),
1562 		("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1563 		sc->sc_wire));
1564 
1565 	if (sc->sc_dying)
1566 		return;
1567 
1568 	/*
1569 	 * State handling for CBI transfers.
1570 	 */
1571 
1572 	DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1573 		sc->sc_dev.dv_xname, sc->transfer_state,
1574 		states[sc->transfer_state], xfer, usbd_errstr(err)));
1575 
1576 	switch (sc->transfer_state) {
1577 
1578 	/***** CBI Transfer *****/
1579 	case TSTATE_CBI_COMMAND:
1580 		if (err == USBD_STALLED) {
1581 			DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
1582 				sc->sc_dev.dv_xname));
1583 			/* Status transport by control pipe (section 2.3.2.1).
1584 			 * The command contained in the command block failed.
1585 			 *
1586 			 * The control pipe has already been unstalled by the
1587 			 * USB stack.
1588 			 * Section 2.4.3.1.1 states that the bulk in endpoints
1589 			 * should not stalled at this point.
1590 			 */
1591 
1592 			sc->transfer_state = TSTATE_IDLE;
1593 			sc->transfer_cb(sc, sc->transfer_priv,
1594 					sc->transfer_datalen,
1595 					STATUS_CMD_FAILED);
1596 
1597 			return;
1598 		} else if (err) {
1599 			DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
1600 				sc->sc_dev.dv_xname));
1601 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1602 			return;
1603 		}
1604 
1605 		/* Data transport phase, setup transfer */
1606 		sc->transfer_state = TSTATE_CBI_DATA;
1607 		if (sc->transfer_dir == DIR_IN) {
1608 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN],
1609 					sc->data_buffer, sc->transfer_datalen,
1610 					USBD_SHORT_XFER_OK | USBD_NO_COPY,
1611 					sc->transfer_xfer[XFER_CBI_DATA]))
1612 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1613 
1614 			return;
1615 		} else if (sc->transfer_dir == DIR_OUT) {
1616 			memcpy(sc->data_buffer, sc->transfer_data,
1617 			       sc->transfer_datalen);
1618 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT],
1619 					sc->data_buffer, sc->transfer_datalen,
1620 					USBD_NO_COPY,/* fixed length transfer */
1621 					sc->transfer_xfer[XFER_CBI_DATA]))
1622 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1623 
1624 			return;
1625 		} else {
1626 			DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
1627 				sc->sc_dev.dv_xname));
1628 		}
1629 
1630 		/* FALLTHROUGH if no data phase, err == 0 */
1631 	case TSTATE_CBI_DATA:
1632 		/* Command transport phase error handling (ignored if no data
1633 		 * phase (fallthrough from previous state)) */
1634 		if (sc->transfer_dir != DIR_NONE) {
1635 			/* retrieve the length of the transfer that was done */
1636 			usbd_get_xfer_status(xfer, NULL, NULL,
1637 			    &sc->transfer_actlen, NULL);
1638 			DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
1639 				sc->sc_dev.dv_xname, sc->transfer_actlen));
1640 
1641 			if (err) {
1642 				DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, "
1643 					"%s\n", sc->sc_dev.dv_xname,
1644 					(sc->transfer_dir == DIR_IN?"in":"out"),
1645 					sc->transfer_datalen,usbd_errstr(err)));
1646 
1647 				if (err == USBD_STALLED) {
1648 					sc->transfer_state = TSTATE_CBI_DCLEAR;
1649 					umass_clear_endpoint_stall(sc,
1650 					  (sc->transfer_dir == DIR_IN?
1651 					    UMASS_BULKIN:UMASS_BULKOUT),
1652 					sc->transfer_xfer[XFER_CBI_DCLEAR]);
1653 				} else {
1654 					/* Unless the error is a pipe stall the
1655 					 * error is fatal.
1656 					 */
1657 					umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1658 				}
1659 				return;
1660 			}
1661 		}
1662 
1663 		if (sc->transfer_dir == DIR_IN)
1664 			memcpy(sc->transfer_data, sc->data_buffer,
1665 			       sc->transfer_actlen);
1666 
1667 		DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
1668 					umass_dump_buffer(sc, sc->transfer_data,
1669 						sc->transfer_actlen, 48));
1670 
1671 		/* Status phase */
1672 		if (sc->sc_wire == UMASS_WPROTO_CBI_I) {
1673 			sc->transfer_state = TSTATE_CBI_STATUS;
1674 			memset(&sc->sbl, 0, sizeof(sc->sbl));
1675 			if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN],
1676 				    &sc->sbl, sizeof(sc->sbl),
1677 				    0,	/* fixed length transfer */
1678 				    sc->transfer_xfer[XFER_CBI_STATUS]))
1679 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1680 		} else {
1681 			/* No command completion interrupt. Request
1682 			 * sense to get status of command.
1683 			 */
1684 			sc->transfer_state = TSTATE_IDLE;
1685 			sc->transfer_cb(sc, sc->transfer_priv,
1686 				sc->transfer_datalen - sc->transfer_actlen,
1687 				STATUS_CMD_UNKNOWN);
1688 		}
1689 		return;
1690 
1691 	case TSTATE_CBI_STATUS:
1692 		if (err) {
1693 			DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
1694 				sc->sc_dev.dv_xname));
1695 			/* Status transport by interrupt pipe (section 2.3.2.2).
1696 			 */
1697 
1698 			if (err == USBD_STALLED) {
1699 				sc->transfer_state = TSTATE_CBI_SCLEAR;
1700 				umass_clear_endpoint_stall(sc, UMASS_INTRIN,
1701 					sc->transfer_xfer[XFER_CBI_SCLEAR]);
1702 			} else {
1703 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1704 			}
1705 			return;
1706 		}
1707 
1708 		/* Dissect the information in the buffer */
1709 
1710 		{
1711 			u_int32_t actlen;
1712 			usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL);
1713 			DPRINTF(UDMASS_CBI, ("%s: CBI_STATUS actlen=%d\n",
1714 			    sc->sc_dev.dv_xname, actlen));
1715 			if (actlen != 2)
1716 				break;
1717 		}
1718 
1719 		if (sc->sc_cmd == UMASS_CPROTO_UFI) {
1720 			int status;
1721 
1722 			/* Section 3.4.3.1.3 specifies that the UFI command
1723 			 * protocol returns an ASC and ASCQ in the interrupt
1724 			 * data block.
1725 			 */
1726 
1727 			DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
1728 				"ASCQ = 0x%02x\n",
1729 				sc->sc_dev.dv_xname,
1730 				sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
1731 
1732 			if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) ||
1733 			    sc->sc_sense)
1734 				status = STATUS_CMD_OK;
1735 			else
1736 				status = STATUS_CMD_FAILED;
1737 
1738 			/* No autosense, command successful */
1739 			sc->transfer_state = TSTATE_IDLE;
1740 			sc->transfer_cb(sc, sc->transfer_priv,
1741 			    sc->transfer_datalen - sc->transfer_actlen, status);
1742 		} else {
1743 			/* Command Interrupt Data Block */
1744 
1745 			DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
1746 				sc->sc_dev.dv_xname,
1747 				sc->sbl.common.type, sc->sbl.common.value));
1748 
1749 			if (sc->sbl.common.type == IDB_TYPE_CCI) {
1750 				int status;
1751 				switch (sc->sbl.common.value &
1752 				    IDB_VALUE_STATUS_MASK) {
1753 				case IDB_VALUE_PASS:
1754 					status = STATUS_CMD_OK;
1755 					break;
1756 				case IDB_VALUE_FAIL:
1757 				case IDB_VALUE_PERSISTENT:
1758 					status = STATUS_CMD_FAILED;
1759 					break;
1760 				case IDB_VALUE_PHASE:
1761 				default:
1762 					status = STATUS_WIRE_FAILED;
1763 					break;
1764  				}
1765 
1766 				sc->transfer_state = TSTATE_IDLE;
1767 				sc->transfer_cb(sc, sc->transfer_priv,
1768 				    sc->transfer_datalen - sc->transfer_actlen,
1769 				    status);
1770 			}
1771 		}
1772 		return;
1773 
1774 	case TSTATE_CBI_DCLEAR:
1775 		if (err) {	/* should not occur */
1776 			printf("%s: CBI bulk-in/out stall clear failed, %s\n",
1777 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1778 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1779 		} else {
1780 			sc->transfer_state = TSTATE_IDLE;
1781 			sc->transfer_cb(sc, sc->transfer_priv,
1782 			    sc->transfer_datalen, STATUS_CMD_FAILED);
1783 		}
1784 		return;
1785 
1786 	case TSTATE_CBI_SCLEAR:
1787 		if (err) {	/* should not occur */
1788 			printf("%s: CBI intr-in stall clear failed, %s\n",
1789 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1790 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
1791 		} else {
1792 			sc->transfer_state = TSTATE_IDLE;
1793 			sc->transfer_cb(sc, sc->transfer_priv,
1794 			    sc->transfer_datalen, STATUS_CMD_FAILED);
1795 		}
1796 		return;
1797 
1798 	/***** CBI Reset *****/
1799 	case TSTATE_CBI_RESET1:
1800 		if (err)
1801 			printf("%s: CBI reset failed, %s\n",
1802 				sc->sc_dev.dv_xname, usbd_errstr(err));
1803 
1804 		sc->transfer_state = TSTATE_CBI_RESET2;
1805 		umass_clear_endpoint_stall(sc, UMASS_BULKIN,
1806 			sc->transfer_xfer[XFER_CBI_RESET2]);
1807 
1808 		return;
1809 	case TSTATE_CBI_RESET2:
1810 		if (err)	/* should not occur */
1811 			printf("%s: CBI bulk-in stall clear failed, %s\n",
1812 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1813 			/* no error recovery, otherwise we end up in a loop */
1814 
1815 		sc->transfer_state = TSTATE_CBI_RESET3;
1816 		umass_clear_endpoint_stall(sc, UMASS_BULKOUT,
1817 			sc->transfer_xfer[XFER_CBI_RESET3]);
1818 
1819 		return;
1820 	case TSTATE_CBI_RESET3:
1821 		if (err)	/* should not occur */
1822 			printf("%s: CBI bulk-out stall clear failed, %s\n",
1823 			       sc->sc_dev.dv_xname, usbd_errstr(err));
1824 			/* no error recovery, otherwise we end up in a loop */
1825 
1826 		sc->transfer_state = TSTATE_IDLE;
1827 		if (sc->transfer_priv) {
1828 			sc->transfer_cb(sc, sc->transfer_priv,
1829 					sc->transfer_datalen,
1830 					sc->transfer_status);
1831 		}
1832 
1833 		return;
1834 
1835 
1836 	/***** Default *****/
1837 	default:
1838 		panic("%s: Unknown state %d",
1839 		      sc->sc_dev.dv_xname, sc->transfer_state);
1840 	}
1841 }
1842 
1843 u_int8_t
1844 umass_bbb_get_max_lun(struct umass_softc *sc)
1845 {
1846 	usb_device_request_t req;
1847 	usbd_status err;
1848 	u_int8_t maxlun = 0;
1849 	u_int8_t buf = 0;
1850 
1851 	DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", sc->sc_dev.dv_xname));
1852 
1853 	/* The Get Max Lun command is a class-specific request. */
1854 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
1855 	req.bRequest = UR_BBB_GET_MAX_LUN;
1856 	USETW(req.wValue, 0);
1857 	USETW(req.wIndex, sc->sc_ifaceno);
1858 	USETW(req.wLength, 1);
1859 
1860 	err = usbd_do_request_flags(sc->sc_udev, &req, &buf,
1861 	    USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT);
1862 
1863 	switch (err) {
1864 	case USBD_NORMAL_COMPLETION:
1865 		maxlun = buf;
1866 		break;
1867 
1868 	default:
1869 		/* XXX Should we port_reset the device? */
1870 		DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported (%s)\n",
1871 		    sc->sc_dev.dv_xname, usbd_errstr(err)));
1872 		break;
1873 	}
1874 
1875 	DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n", sc->sc_dev.dv_xname, maxlun));
1876 	return (maxlun);
1877 }
1878 
1879 #ifdef UMASS_DEBUG
1880 void
1881 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
1882 {
1883 	int clen = cbw->bCDBLength;
1884 	int dlen = UGETDW(cbw->dCBWDataTransferLength);
1885 	u_int8_t *c = cbw->CBWCDB;
1886 	int tag = UGETDW(cbw->dCBWTag);
1887 	int flags = cbw->bCBWFlags;
1888 
1889 	DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d "
1890 		"(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), "
1891 		"data = %d bytes, dir = %s\n",
1892 		sc->sc_dev.dv_xname, tag, clen,
1893 		c[0], c[1], c[2], c[3], c[4], c[5],
1894 		c[6], c[7], c[8], c[9],
1895 		(clen > 10? "...":""),
1896 		dlen, (flags == CBWFLAGS_IN? "in":
1897 		       (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
1898 }
1899 
1900 void
1901 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
1902 {
1903 	int sig = UGETDW(csw->dCSWSignature);
1904 	int tag = UGETDW(csw->dCSWTag);
1905 	int res = UGETDW(csw->dCSWDataResidue);
1906 	int status = csw->bCSWStatus;
1907 
1908 	DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1909 		"res = %d, status = 0x%02x (%s)\n", sc->sc_dev.dv_xname,
1910 		tag, sig, (sig == CSWSIGNATURE?	 "valid":"invalid"),
1911 		tag, res,
1912 		status, (status == CSWSTATUS_GOOD? "good":
1913 			 (status == CSWSTATUS_FAILED? "failed":
1914 			  (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
1915 }
1916 
1917 void
1918 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
1919 		  int printlen)
1920 {
1921 	int i, j;
1922 	char s1[40];
1923 	char s2[40];
1924 	char s3[5];
1925 
1926 	s1[0] = '\0';
1927 	s3[0] = '\0';
1928 
1929 	snprintf(s2, sizeof s2, " buffer=%p, buflen=%d", buffer, buflen);
1930 	for (i = 0; i < buflen && i < printlen; i++) {
1931 		j = i % 16;
1932 		if (j == 0 && i != 0) {
1933 			DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
1934 				sc->sc_dev.dv_xname, s1, s2));
1935 			s2[0] = '\0';
1936 		}
1937 		snprintf(&s1[j*2], sizeof s1 - j*2, "%02x", buffer[i] & 0xff);
1938 	}
1939 	if (buflen > printlen)
1940 		snprintf(s3, sizeof s3, " ...");
1941 	DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
1942 		sc->sc_dev.dv_xname, s1, s2, s3));
1943 }
1944 #endif
1945