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