1 /* $NetBSD: umass.c,v 1.135 2009/10/30 16:22:32 is Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
34 * Nick Hibma <n_hibma@freebsd.org>
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
62 * Universal Serial Bus Mass Storage Class specs:
63 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
64 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
65 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
66 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
70 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
71 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
75 * The driver handles 3 Wire Protocols
76 * - Command/Bulk/Interrupt (CBI)
77 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
78 * - Mass Storage Bulk-Only (BBB)
79 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
81 * Over these wire protocols it handles the following command protocols
83 * - 8070 (ATA/ATAPI for rewritable removable media)
84 * - UFI (USB Floppy Interface)
86 * 8070i is a transformed version of the SCSI command set. UFI is a transformed
87 * version of the 8070i command set. The sc->transform method is used to
88 * convert the commands into the appropriate format (if at all necessary).
89 * For example, ATAPI requires all commands to be 12 bytes in length amongst
92 * The source code below is marked and can be split into a number of pieces
95 * - probe/attach/detach
96 * - generic transfer routines
99 * - CBI_I (in addition to functions from CBI)
100 * - CAM (Common Access Method)
105 * The protocols are implemented using a state machine, for the transfers as
106 * well as for the resets. The state machine is contained in umass_*_state.
107 * The state machine is started through either umass_*_transfer or
110 * The reason for doing this is a) CAM performs a lot better this way and b) it
111 * avoids using tsleep from interrupt context (for example after a failed
116 * The SCSI related part of this driver has been derived from the
117 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org).
119 * The CAM layer uses so called actions which are messages sent to the host
120 * adapter for completion. The actions come in through umass_cam_action. The
121 * appropriate block of routines is called depending on the transport protocol
122 * in use. When the transfer has finished, these routines call
123 * umass_cam_cb again to complete the CAM command.
126 #include <sys/cdefs.h>
127 __KERNEL_RCSID(0, "$NetBSD: umass.c,v 1.135 2009/10/30 16:22:32 is Exp $");
129 #include "atapibus.h"
133 #include <sys/param.h>
134 #include <sys/systm.h>
135 #include <sys/kernel.h>
136 #include <sys/conf.h>
137 #if defined(__NetBSD__) || defined(__OpenBSD__)
139 #include <sys/device.h>
140 #include <sys/malloc.h>
142 #define KASSERT(cond, msg)
143 #elif defined(__FreeBSD__)
144 #include <sys/module.h>
146 #include <machine/clock.h>
149 #include <dev/usb/usb.h>
150 #include <dev/usb/usbdi.h>
151 #include <dev/usb/usbdi_util.h>
152 #include <dev/usb/usbdevs.h>
154 #include <dev/usb/umassvar.h>
155 #include <dev/usb/umass_quirks.h>
156 #include <dev/usb/umass_scsipi.h>
157 #include <dev/usb/umass_isdata.h>
159 #include <dev/scsipi/scsipi_all.h>
160 #include <dev/scsipi/scsipiconf.h>
166 const char *states
[TSTATE_STATES
+1] = {
167 /* should be kept in sync with the list at transfer_state */
171 "BBB Data bulk-in/-out clear stall",
172 "BBB CSW, 1st attempt",
173 "BBB CSW bulk-in clear stall",
174 "BBB CSW, 2nd attempt",
176 "BBB bulk-in clear stall",
177 "BBB bulk-out clear stall",
181 "CBI Data bulk-in/-out clear stall",
182 "CBI Status intr-in clear stall",
184 "CBI bulk-in clear stall",
185 "CBI bulk-out clear stall",
190 /* USB device probe/attach/detach functions */
191 int umass_match(device_t
, cfdata_t
, void *);
192 void umass_attach(device_t
, device_t
, void *);
193 int umass_detach(device_t
, int);
194 static void umass_childdet(device_t
, device_t
);
195 int umass_activate(device_t
, enum devact
);
196 extern struct cfdriver umass_cd
;
197 CFATTACH_DECL2_NEW(umass
, sizeof(struct umass_softc
), umass_match
, umass_attach
,
198 umass_detach
, umass_activate
, NULL
, umass_childdet
);
200 Static
void umass_disco(struct umass_softc
*sc
);
202 /* generic transfer functions */
203 Static 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 Static 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 Static
void umass_clear_endpoint_stall(struct umass_softc
*sc
, int endpt
,
212 usbd_xfer_handle xfer
);
214 Static
void umass_reset(struct umass_softc
*sc
, transfer_cb_f cb
, void *priv
);
217 /* Bulk-Only related functions */
218 Static
void umass_bbb_transfer(struct umass_softc
*, int, void *, int, void *,
219 int, int, u_int
, umass_callback
, void *);
220 Static
void umass_bbb_reset(struct umass_softc
*, int);
221 Static
void umass_bbb_state(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
223 usbd_status
umass_bbb_get_max_lun(struct umass_softc
*, u_int8_t
*);
225 /* CBI related functions */
226 Static
void umass_cbi_transfer(struct umass_softc
*, int, void *, int, void *,
227 int, int, u_int
, umass_callback
, void *);
228 Static
void umass_cbi_reset(struct umass_softc
*, int);
229 Static
void umass_cbi_state(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
231 Static
int umass_cbi_adsc(struct umass_softc
*, char *, int, usbd_xfer_handle
);
233 const struct umass_wire_methods umass_bbb_methods
= {
239 const struct umass_wire_methods umass_cbi_methods
= {
246 /* General debugging functions */
247 Static
void umass_bbb_dump_cbw(struct umass_softc
*sc
,
248 umass_bbb_cbw_t
*cbw
);
249 Static
void umass_bbb_dump_csw(struct umass_softc
*sc
,
250 umass_bbb_csw_t
*csw
);
251 Static
void umass_dump_buffer(struct umass_softc
*sc
, u_int8_t
*buffer
,
252 int buflen
, int printlen
);
257 * USB device probe/attach/detach
261 umass_match(device_t parent
, cfdata_t match
, void *aux
)
263 struct usbif_attach_arg
*uaa
= aux
;
264 const struct umass_quirk
*quirk
;
266 quirk
= umass_lookup(uaa
->vendor
, uaa
->product
);
267 if (quirk
!= NULL
&& quirk
->uq_match
!= UMASS_QUIRK_USE_DEFAULTMATCH
)
268 return (quirk
->uq_match
);
270 if (uaa
->class != UICLASS_MASS
)
271 return (UMATCH_NONE
);
273 switch (uaa
->subclass
) {
275 case UISUBCLASS_SFF8020I
:
276 case UISUBCLASS_QIC157
:
278 case UISUBCLASS_SFF8070I
:
279 case UISUBCLASS_SCSI
:
282 return (UMATCH_IFACECLASS
);
285 switch (uaa
->proto
) {
286 case UIPROTO_MASS_CBI_I
:
287 case UIPROTO_MASS_CBI
:
288 case UIPROTO_MASS_BBB_OLD
:
289 case UIPROTO_MASS_BBB
:
292 return (UMATCH_IFACECLASS_IFACESUBCLASS
);
295 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO
);
299 umass_attach(device_t parent
, device_t self
, void *aux
)
301 struct umass_softc
*sc
= device_private(self
);
302 struct usbif_attach_arg
*uaa
= aux
;
303 const struct umass_quirk
*quirk
;
304 usb_interface_descriptor_t
*id
;
305 usb_endpoint_descriptor_t
*ed
;
306 const char *sWire
, *sCommand
;
316 devinfop
= usbd_devinfo_alloc(uaa
->device
, 0);
317 aprint_normal_dev(self
, "%s\n", devinfop
);
318 usbd_devinfo_free(devinfop
);
320 sc
->sc_udev
= uaa
->device
;
321 sc
->sc_iface
= uaa
->iface
;
322 sc
->sc_ifaceno
= uaa
->ifaceno
;
324 quirk
= umass_lookup(uaa
->vendor
, uaa
->product
);
326 sc
->sc_wire
= quirk
->uq_wire
;
327 sc
->sc_cmd
= quirk
->uq_cmd
;
328 sc
->sc_quirks
= quirk
->uq_flags
;
329 sc
->sc_busquirks
= quirk
->uq_busquirks
;
331 if (quirk
->uq_fixup
!= NULL
)
332 (*quirk
->uq_fixup
)(sc
);
334 sc
->sc_wire
= UMASS_WPROTO_UNSPEC
;
335 sc
->sc_cmd
= UMASS_CPROTO_UNSPEC
;
337 sc
->sc_busquirks
= 0;
340 if (sc
->sc_wire
== UMASS_WPROTO_UNSPEC
) {
341 switch (uaa
->proto
) {
342 case UIPROTO_MASS_CBI
:
343 sc
->sc_wire
= UMASS_WPROTO_CBI
;
345 case UIPROTO_MASS_CBI_I
:
346 sc
->sc_wire
= UMASS_WPROTO_CBI_I
;
348 case UIPROTO_MASS_BBB
:
349 case UIPROTO_MASS_BBB_OLD
:
350 sc
->sc_wire
= UMASS_WPROTO_BBB
;
354 ("%s: Unsupported wire protocol %u\n",
355 device_xname(sc
->sc_dev
),
361 if (sc
->sc_cmd
== UMASS_CPROTO_UNSPEC
) {
362 switch (uaa
->subclass
) {
363 case UISUBCLASS_SCSI
:
364 sc
->sc_cmd
= UMASS_CPROTO_SCSI
;
367 sc
->sc_cmd
= UMASS_CPROTO_UFI
;
369 case UISUBCLASS_SFF8020I
:
370 case UISUBCLASS_SFF8070I
:
371 case UISUBCLASS_QIC157
:
372 sc
->sc_cmd
= UMASS_CPROTO_ATAPI
;
375 sc
->sc_cmd
= UMASS_CPROTO_RBC
;
379 ("%s: Unsupported command protocol %u\n",
380 device_xname(sc
->sc_dev
),
386 switch (sc
->sc_wire
) {
387 case UMASS_WPROTO_CBI
:
390 case UMASS_WPROTO_CBI_I
:
391 sWire
= "CBI with CCI";
393 case UMASS_WPROTO_BBB
:
401 switch (sc
->sc_cmd
) {
402 case UMASS_CPROTO_RBC
:
405 case UMASS_CPROTO_SCSI
:
408 case UMASS_CPROTO_UFI
:
411 case UMASS_CPROTO_ATAPI
:
414 case UMASS_CPROTO_ISD_ATA
:
415 sCommand
= "ISD-ATA";
418 sCommand
= "unknown";
422 aprint_verbose_dev(self
, "using %s over %s\n", sCommand
, sWire
);
424 if (quirk
!= NULL
&& quirk
->uq_init
!= NULL
) {
425 err
= (*quirk
->uq_init
)(sc
);
427 aprint_error_dev(self
, "quirk init failed\n");
434 * In addition to the Control endpoint the following endpoints
436 * a) bulk-in endpoint.
437 * b) bulk-out endpoint.
438 * and for Control/Bulk/Interrupt with CCI (CBI_I)
441 * The endpoint addresses are not fixed, so we have to read them
442 * from the device descriptors of the current interface.
444 id
= usbd_get_interface_descriptor(sc
->sc_iface
);
445 for (i
= 0 ; i
< id
->bNumEndpoints
; i
++) {
446 ed
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
448 aprint_error_dev(self
,
449 "could not read endpoint descriptor\n");
452 if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
453 && (ed
->bmAttributes
& UE_XFERTYPE
) == UE_BULK
) {
454 sc
->sc_epaddr
[UMASS_BULKIN
] = ed
->bEndpointAddress
;
455 } else if (UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_OUT
456 && (ed
->bmAttributes
& UE_XFERTYPE
) == UE_BULK
) {
457 sc
->sc_epaddr
[UMASS_BULKOUT
] = ed
->bEndpointAddress
;
458 } else if (sc
->sc_wire
== UMASS_WPROTO_CBI_I
459 && UE_GET_DIR(ed
->bEndpointAddress
) == UE_DIR_IN
460 && (ed
->bmAttributes
& UE_XFERTYPE
) == UE_INTERRUPT
) {
461 sc
->sc_epaddr
[UMASS_INTRIN
] = ed
->bEndpointAddress
;
463 if (UGETW(ed
->wMaxPacketSize
) > 2) {
464 DPRINTF(UDMASS_CBI
, ("%s: intr size is %d\n",
465 device_xname(sc
->sc_dev
),
466 UGETW(ed
->wMaxPacketSize
)));
472 /* check whether we found all the endpoints we need */
473 if (!sc
->sc_epaddr
[UMASS_BULKIN
] || !sc
->sc_epaddr
[UMASS_BULKOUT
] ||
474 (sc
->sc_wire
== UMASS_WPROTO_CBI_I
&&
475 !sc
->sc_epaddr
[UMASS_INTRIN
])) {
476 aprint_error_dev(self
, "endpoint not found %u/%u/%u\n",
477 sc
->sc_epaddr
[UMASS_BULKIN
],
478 sc
->sc_epaddr
[UMASS_BULKOUT
],
479 sc
->sc_epaddr
[UMASS_INTRIN
]);
484 * Get the maximum LUN supported by the device.
486 if (sc
->sc_wire
== UMASS_WPROTO_BBB
&&
487 (sc
->sc_quirks
& UMASS_QUIRK_NOGETMAXLUN
) == 0) {
488 err
= umass_bbb_get_max_lun(sc
, &sc
->maxlun
);
490 aprint_error_dev(self
, "unable to get Max Lun: %s\n",
495 sc
->sc_busquirks
|= PQUIRK_FORCELUNS
;
500 /* Open the bulk-in and -out pipe */
501 DPRINTF(UDMASS_USB
, ("%s: opening iface %p epaddr %d for BULKOUT\n",
502 device_xname(sc
->sc_dev
), sc
->sc_iface
,
503 sc
->sc_epaddr
[UMASS_BULKOUT
]));
504 err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_epaddr
[UMASS_BULKOUT
],
506 &sc
->sc_pipe
[UMASS_BULKOUT
]);
508 aprint_error_dev(self
, "cannot open %u-out pipe (bulk)\n",
509 sc
->sc_epaddr
[UMASS_BULKOUT
]);
513 DPRINTF(UDMASS_USB
, ("%s: opening iface %p epaddr %d for BULKIN\n",
514 device_xname(sc
->sc_dev
), sc
->sc_iface
,
515 sc
->sc_epaddr
[UMASS_BULKIN
]));
516 err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_epaddr
[UMASS_BULKIN
],
517 USBD_EXCLUSIVE_USE
, &sc
->sc_pipe
[UMASS_BULKIN
]);
519 aprint_error_dev(self
, "could not open %u-in pipe (bulk)\n",
520 sc
->sc_epaddr
[UMASS_BULKIN
]);
525 * Open the intr-in pipe if the protocol is CBI with CCI.
526 * Note: early versions of the Zip drive do have an interrupt pipe, but
527 * this pipe is unused
529 * We do not open the interrupt pipe as an interrupt pipe, but as a
530 * normal bulk endpoint. We send an IN transfer down the wire at the
531 * appropriate time, because we know exactly when to expect data on
532 * that endpoint. This saves bandwidth, but more important, makes the
533 * code for handling the data on that endpoint simpler. No data
534 * arriving concurrently.
536 if (sc
->sc_wire
== UMASS_WPROTO_CBI_I
) {
537 DPRINTF(UDMASS_USB
, ("%s: opening iface %p epaddr %d for INTRIN\n",
538 device_xname(sc
->sc_dev
), sc
->sc_iface
,
539 sc
->sc_epaddr
[UMASS_INTRIN
]));
540 err
= usbd_open_pipe(sc
->sc_iface
, sc
->sc_epaddr
[UMASS_INTRIN
],
541 USBD_EXCLUSIVE_USE
, &sc
->sc_pipe
[UMASS_INTRIN
]);
543 aprint_error_dev(self
, "couldn't open %u-in (intr)\n",
544 sc
->sc_epaddr
[UMASS_INTRIN
]);
550 /* initialisation of generic part */
551 sc
->transfer_state
= TSTATE_IDLE
;
553 /* request a sufficient number of xfer handles */
554 for (i
= 0; i
< XFER_NR
; i
++) {
555 sc
->transfer_xfer
[i
] = usbd_alloc_xfer(uaa
->device
);
556 if (sc
->transfer_xfer
[i
] == NULL
) {
557 aprint_error_dev(self
, "Out of memory\n");
562 /* Allocate buffer for data transfer (it's huge). */
563 switch (sc
->sc_wire
) {
564 case UMASS_WPROTO_BBB
:
567 case UMASS_WPROTO_CBI
:
570 case UMASS_WPROTO_CBI_I
:
573 sc
->data_buffer
= usbd_alloc_buffer(sc
->transfer_xfer
[bno
],
574 UMASS_MAX_TRANSFER_SIZE
);
575 if (sc
->data_buffer
== NULL
) {
576 aprint_error_dev(self
, "no buffer memory\n");
585 /* Initialise the wire protocol specific methods */
586 switch (sc
->sc_wire
) {
587 case UMASS_WPROTO_BBB
:
588 sc
->sc_methods
= &umass_bbb_methods
;
590 case UMASS_WPROTO_CBI
:
591 case UMASS_WPROTO_CBI_I
:
592 sc
->sc_methods
= &umass_cbi_methods
;
600 switch (sc
->sc_cmd
) {
601 case UMASS_CPROTO_RBC
:
602 case UMASS_CPROTO_SCSI
:
604 error
= umass_scsi_attach(sc
);
606 aprint_error_dev(self
, "scsibus not configured\n");
610 case UMASS_CPROTO_UFI
:
611 case UMASS_CPROTO_ATAPI
:
613 error
= umass_atapi_attach(sc
);
615 aprint_error_dev(self
, "atapibus not configured\n");
619 case UMASS_CPROTO_ISD_ATA
:
621 error
= umass_isdata_attach(sc
);
623 aprint_error_dev(self
, "isdata not configured\n");
628 aprint_error_dev(self
, "command protocol=0x%x not supported\n",
634 aprint_error_dev(self
, "bus attach failed\n");
639 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
, sc
->sc_udev
,
642 if (!pmf_device_register(self
, NULL
, NULL
))
643 aprint_error_dev(self
, "couldn't establish power handler\n");
645 DPRINTF(UDMASS_GEN
, ("%s: Attach finished\n", device_xname(sc
->sc_dev
)));
651 umass_childdet(device_t self
, device_t child
)
653 struct umass_softc
*sc
= device_private(self
);
655 KASSERT(child
== sc
->bus
->sc_child
, ("assertion child == sc->bus->sc_child failed\n"));
656 sc
->bus
->sc_child
= NULL
;
660 umass_detach(device_t self
, int flags
)
662 struct umass_softc
*sc
= device_private(self
);
663 struct umassbus_softc
*scbus
;
666 DPRINTF(UDMASS_USB
, ("%s: detached\n", device_xname(sc
->sc_dev
)));
668 pmf_device_deregister(self
);
670 /* Abort the pipes to wake up any waiting processes. */
671 for (i
= 0 ; i
< UMASS_NEP
; i
++) {
672 if (sc
->sc_pipe
[i
] != NULL
)
673 usbd_abort_pipe(sc
->sc_pipe
[i
]);
676 /* Do we really need reference counting? Perhaps in ioctl() */
678 if (--sc
->sc_refcnt
>= 0) {
680 aprint_normal_dev(self
, "waiting for refcnt\n");
682 /* Wait for processes to go away. */
683 usb_detach_wait(sc
->sc_dev
);
689 if (scbus
->sc_child
!= NULL
)
690 rv
= config_detach(scbus
->sc_child
, flags
);
691 free(scbus
, M_DEVBUF
);
700 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->sc_udev
,
707 umass_activate(device_t dev
, enum devact act
)
709 struct umass_softc
*sc
= device_private(dev
);
711 DPRINTF(UDMASS_USB
, ("%s: umass_activate: %d\n",
712 device_xname(dev
), act
));
715 case DVACT_DEACTIVATE
:
724 umass_disco(struct umass_softc
*sc
)
728 DPRINTF(UDMASS_GEN
, ("umass_disco\n"));
730 /* Remove all the pipes. */
731 for (i
= 0 ; i
< UMASS_NEP
; i
++) {
732 if (sc
->sc_pipe
[i
] != NULL
) {
733 usbd_abort_pipe(sc
->sc_pipe
[i
]);
734 usbd_close_pipe(sc
->sc_pipe
[i
]);
735 sc
->sc_pipe
[i
] = NULL
;
739 /* Some xfers may be queued in the default pipe */
740 usbd_abort_default_pipe(sc
->sc_udev
);
742 /* Free the xfers. */
743 for (i
= 0; i
< XFER_NR
; i
++)
744 if (sc
->transfer_xfer
[i
] != NULL
) {
745 usbd_free_xfer(sc
->transfer_xfer
[i
]);
746 sc
->transfer_xfer
[i
] = NULL
;
751 * Generic functions to handle transfers
755 umass_setup_transfer(struct umass_softc
*sc
, usbd_pipe_handle pipe
,
756 void *buffer
, int buflen
, int flags
,
757 usbd_xfer_handle xfer
)
762 return (USBD_IOERROR
);
764 /* Initialiase a USB transfer and then schedule it */
766 usbd_setup_xfer(xfer
, pipe
, (void *)sc
, buffer
, buflen
,
767 flags
| sc
->sc_xfer_flags
, sc
->timeout
, sc
->sc_methods
->wire_state
);
769 err
= usbd_transfer(xfer
);
770 DPRINTF(UDMASS_XFER
,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
771 "timeout=%d\n", device_xname(sc
->sc_dev
),
772 buffer
, buflen
, flags
| sc
->sc_xfer_flags
, sc
->timeout
));
773 if (err
&& err
!= USBD_IN_PROGRESS
) {
774 DPRINTF(UDMASS_BBB
, ("%s: failed to setup transfer, %s\n",
775 device_xname(sc
->sc_dev
), usbd_errstr(err
)));
779 return (USBD_NORMAL_COMPLETION
);
784 umass_setup_ctrl_transfer(struct umass_softc
*sc
, usb_device_request_t
*req
,
785 void *buffer
, int buflen
, int flags
, usbd_xfer_handle xfer
)
790 return (USBD_IOERROR
);
792 /* Initialiase a USB control transfer and then schedule it */
794 usbd_setup_default_xfer(xfer
, sc
->sc_udev
, (void *) sc
, sc
->timeout
,
795 req
, buffer
, buflen
, flags
, sc
->sc_methods
->wire_state
);
797 err
= usbd_transfer(xfer
);
798 if (err
&& err
!= USBD_IN_PROGRESS
) {
799 DPRINTF(UDMASS_BBB
, ("%s: failed to setup ctrl transfer, %s\n",
800 device_xname(sc
->sc_dev
), usbd_errstr(err
)));
802 /* do not reset, as this would make us loop */
806 return (USBD_NORMAL_COMPLETION
);
810 umass_clear_endpoint_stall(struct umass_softc
*sc
, int endpt
,
811 usbd_xfer_handle xfer
)
816 DPRINTF(UDMASS_BBB
, ("%s: Clear endpoint 0x%02x stall\n",
817 device_xname(sc
->sc_dev
), sc
->sc_epaddr
[endpt
]));
819 usbd_clear_endpoint_toggle(sc
->sc_pipe
[endpt
]);
821 sc
->sc_req
.bmRequestType
= UT_WRITE_ENDPOINT
;
822 sc
->sc_req
.bRequest
= UR_CLEAR_FEATURE
;
823 USETW(sc
->sc_req
.wValue
, UF_ENDPOINT_HALT
);
824 USETW(sc
->sc_req
.wIndex
, sc
->sc_epaddr
[endpt
]);
825 USETW(sc
->sc_req
.wLength
, 0);
826 umass_setup_ctrl_transfer(sc
, &sc
->sc_req
, NULL
, 0, 0, xfer
);
831 umass_reset(struct umass_softc
*sc
, transfer_cb_f cb
, void *priv
)
833 sc
->transfer_cb
= cb
;
834 sc
->transfer_priv
= priv
;
836 /* The reset is a forced reset, so no error (yet) */
837 sc
->reset(sc
, STATUS_CMD_OK
);
842 * Bulk protocol specific functions
846 umass_bbb_reset(struct umass_softc
*sc
, int status
)
848 KASSERT(sc
->sc_wire
& UMASS_WPROTO_BBB
,
849 ("sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n",
856 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
858 * For Reset Recovery the host shall issue in the following order:
859 * a) a Bulk-Only Mass Storage Reset
860 * b) a Clear Feature HALT to the Bulk-In endpoint
861 * c) a Clear Feature HALT to the Bulk-Out endpoint
863 * This is done in 3 steps, states:
868 * If the reset doesn't succeed, the device should be port reset.
871 DPRINTF(UDMASS_BBB
, ("%s: Bulk Reset\n",
872 device_xname(sc
->sc_dev
)));
874 sc
->transfer_state
= TSTATE_BBB_RESET1
;
875 sc
->transfer_status
= status
;
877 /* reset is a class specific interface write */
878 sc
->sc_req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
879 sc
->sc_req
.bRequest
= UR_BBB_RESET
;
880 USETW(sc
->sc_req
.wValue
, 0);
881 USETW(sc
->sc_req
.wIndex
, sc
->sc_ifaceno
);
882 USETW(sc
->sc_req
.wLength
, 0);
883 umass_setup_ctrl_transfer(sc
, &sc
->sc_req
, NULL
, 0, 0,
884 sc
->transfer_xfer
[XFER_BBB_RESET1
]);
888 umass_bbb_transfer(struct umass_softc
*sc
, int lun
, void *cmd
, int cmdlen
,
889 void *data
, int datalen
, int dir
, u_int timeout
,
890 umass_callback cb
, void *priv
)
892 static int dCBWtag
= 42; /* unique for CBW of transfer */
894 DPRINTF(UDMASS_BBB
,("%s: umass_bbb_transfer cmd=0x%02x\n",
895 device_xname(sc
->sc_dev
), *(u_char
*)cmd
));
897 KASSERT(sc
->sc_wire
& UMASS_WPROTO_BBB
,
898 ("sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n",
904 /* Be a little generous. */
905 sc
->timeout
= timeout
+ USBD_DEFAULT_TIMEOUT
;
908 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
909 * a data phase of datalen bytes from/to the device and finally a
911 * If the data direction was inbound a maximum of datalen bytes
912 * is stored in the buffer pointed to by data.
914 * umass_bbb_transfer initialises the transfer and lets the state
915 * machine in umass_bbb_state handle the completion. It uses the
919 * -> TSTATE_BBB_STATUS
920 * -> TSTATE_BBB_STATUS2
923 * An error in any of those states will invoke
927 /* check the given arguments */
928 KASSERT(datalen
== 0 || data
!= NULL
,
929 ("%s: datalen > 0, but no buffer",device_xname(sc
->sc_dev
)));
930 KASSERT(cmdlen
<= CBWCDBLENGTH
,
931 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
932 device_xname(sc
->sc_dev
), cmdlen
, CBWCDBLENGTH
));
933 KASSERT(dir
== DIR_NONE
|| datalen
> 0,
934 ("%s: datalen == 0 while direction is not NONE\n",
935 device_xname(sc
->sc_dev
)));
936 KASSERT(datalen
== 0 || dir
!= DIR_NONE
,
937 ("%s: direction is NONE while datalen is not zero\n",
938 device_xname(sc
->sc_dev
)));
939 KASSERT(sizeof(umass_bbb_cbw_t
) == UMASS_BBB_CBW_SIZE
,
940 ("%s: CBW struct does not have the right size (%d vs. %d)\n",
941 device_xname(sc
->sc_dev
),
942 sizeof(umass_bbb_cbw_t
), UMASS_BBB_CBW_SIZE
));
943 KASSERT(sizeof(umass_bbb_csw_t
) == UMASS_BBB_CSW_SIZE
,
944 ("%s: CSW struct does not have the right size (%d vs. %d)\n",
945 device_xname(sc
->sc_dev
),
946 sizeof(umass_bbb_csw_t
), UMASS_BBB_CSW_SIZE
));
949 * Determine the direction of the data transfer and the length.
951 * dCBWDataTransferLength (datalen) :
952 * This field indicates the number of bytes of data that the host
953 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
954 * the Direction bit) during the execution of this command. If this
955 * field is set to 0, the device will expect that no data will be
956 * transferred IN or OUT during this command, regardless of the value
957 * of the Direction bit defined in dCBWFlags.
960 * The bits of the Flags field are defined as follows:
962 * Bit 7 Direction - this bit shall be ignored if the
963 * dCBWDataTransferLength field is zero.
964 * 0 = data Out from host to device
965 * 1 = data In from device to host
968 /* Fill in the Command Block Wrapper */
969 USETDW(sc
->cbw
.dCBWSignature
, CBWSIGNATURE
);
970 USETDW(sc
->cbw
.dCBWTag
, dCBWtag
);
971 dCBWtag
++; /* cannot be done in macro (it will be done 4 times) */
972 USETDW(sc
->cbw
.dCBWDataTransferLength
, datalen
);
973 /* DIR_NONE is treated as DIR_OUT (0x00) */
974 sc
->cbw
.bCBWFlags
= (dir
== DIR_IN
? CBWFLAGS_IN
:CBWFLAGS_OUT
);
975 sc
->cbw
.bCBWLUN
= lun
;
976 sc
->cbw
.bCDBLength
= cmdlen
;
977 memcpy(sc
->cbw
.CBWCDB
, cmd
, cmdlen
);
979 DIF(UDMASS_BBB
, umass_bbb_dump_cbw(sc
, &sc
->cbw
));
981 /* store the details for the data transfer phase */
982 sc
->transfer_dir
= dir
;
983 sc
->transfer_data
= data
;
984 sc
->transfer_datalen
= datalen
;
985 sc
->transfer_actlen
= 0;
986 sc
->transfer_cb
= cb
;
987 sc
->transfer_priv
= priv
;
988 sc
->transfer_status
= STATUS_CMD_OK
;
990 /* move from idle to the command state */
991 sc
->transfer_state
= TSTATE_BBB_COMMAND
;
993 /* Send the CBW from host to device via bulk-out endpoint. */
994 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKOUT
],
995 &sc
->cbw
, UMASS_BBB_CBW_SIZE
, 0,
996 sc
->transfer_xfer
[XFER_BBB_CBW
])) {
997 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1003 umass_bbb_state(usbd_xfer_handle xfer
, usbd_private_handle priv
,
1006 struct umass_softc
*sc
= (struct umass_softc
*) priv
;
1007 usbd_xfer_handle next_xfer
;
1010 KASSERT(sc
->sc_wire
& UMASS_WPROTO_BBB
,
1011 ("sc->sc_wire == 0x%02x wrong for umass_bbb_state\n",
1018 * State handling for BBB transfers.
1020 * The subroutine is rather long. It steps through the states given in
1021 * Annex A of the Bulk-Only specification.
1022 * Each state first does the error handling of the previous transfer
1023 * and then prepares the next transfer.
1024 * Each transfer is done asynchroneously so after the request/transfer
1025 * has been submitted you will find a 'return;'.
1028 DPRINTF(UDMASS_BBB
, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
1029 device_xname(sc
->sc_dev
), sc
->transfer_state
,
1030 states
[sc
->transfer_state
], xfer
, usbd_errstr(err
)));
1032 switch (sc
->transfer_state
) {
1034 /***** Bulk Transfer *****/
1035 case TSTATE_BBB_COMMAND
:
1036 /* Command transport phase, error handling */
1038 DPRINTF(UDMASS_BBB
, ("%s: failed to send CBW\n",
1039 device_xname(sc
->sc_dev
)));
1040 /* If the device detects that the CBW is invalid, then
1041 * the device may STALL both bulk endpoints and require
1044 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1048 /* Data transport phase, setup transfer */
1049 sc
->transfer_state
= TSTATE_BBB_DATA
;
1050 if (sc
->transfer_dir
== DIR_IN
) {
1051 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKIN
],
1052 sc
->data_buffer
, sc
->transfer_datalen
,
1053 USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1054 sc
->transfer_xfer
[XFER_BBB_DATA
]))
1055 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1058 } else if (sc
->transfer_dir
== DIR_OUT
) {
1059 memcpy(sc
->data_buffer
, sc
->transfer_data
,
1060 sc
->transfer_datalen
);
1061 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKOUT
],
1062 sc
->data_buffer
, sc
->transfer_datalen
,
1063 USBD_NO_COPY
,/* fixed length transfer */
1064 sc
->transfer_xfer
[XFER_BBB_DATA
]))
1065 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1069 DPRINTF(UDMASS_BBB
, ("%s: no data phase\n",
1070 device_xname(sc
->sc_dev
)));
1073 /* FALLTHROUGH if no data phase, err == 0 */
1074 case TSTATE_BBB_DATA
:
1075 /* Command transport phase error handling (ignored if no data
1076 * phase (fallthrough from previous state)) */
1077 if (sc
->transfer_dir
!= DIR_NONE
) {
1078 /* retrieve the length of the transfer that was done */
1079 usbd_get_xfer_status(xfer
, NULL
, NULL
,
1080 &sc
->transfer_actlen
, NULL
);
1081 DPRINTF(UDMASS_BBB
, ("%s: BBB_DATA actlen=%d\n",
1082 device_xname(sc
->sc_dev
), sc
->transfer_actlen
));
1085 DPRINTF(UDMASS_BBB
, ("%s: Data-%s %d failed, "
1086 "%s\n", device_xname(sc
->sc_dev
),
1087 (sc
->transfer_dir
== DIR_IN
?"in":"out"),
1088 sc
->transfer_datalen
,usbd_errstr(err
)));
1090 if (err
== USBD_STALLED
) {
1091 sc
->transfer_state
= TSTATE_BBB_DCLEAR
;
1092 umass_clear_endpoint_stall(sc
,
1093 (sc
->transfer_dir
== DIR_IN
?
1094 UMASS_BULKIN
:UMASS_BULKOUT
),
1095 sc
->transfer_xfer
[XFER_BBB_DCLEAR
]);
1097 /* Unless the error is a pipe stall the
1100 umass_bbb_reset(sc
,STATUS_WIRE_FAILED
);
1106 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1107 case TSTATE_BBB_DCLEAR
: /* stall clear after data phase */
1108 if (sc
->transfer_dir
== DIR_IN
)
1109 memcpy(sc
->transfer_data
, sc
->data_buffer
,
1110 sc
->transfer_actlen
);
1112 DIF(UDMASS_BBB
, if (sc
->transfer_dir
== DIR_IN
)
1113 umass_dump_buffer(sc
, sc
->transfer_data
,
1114 sc
->transfer_datalen
, 48));
1116 /* FALLTHROUGH, err == 0 (no data phase or successful) */
1117 case TSTATE_BBB_SCLEAR
: /* stall clear after status phase */
1118 /* Reading of CSW after bulk stall condition in data phase
1119 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
1120 * reading CSW (TSTATE_BBB_SCLEAR).
1121 * In the case of no data phase or successful data phase,
1122 * err == 0 and the following if block is passed.
1124 if (err
) { /* should not occur */
1125 printf("%s: BBB bulk-%s stall clear failed, %s\n",
1126 device_xname(sc
->sc_dev
),
1127 (sc
->transfer_dir
== DIR_IN
? "in":"out"),
1129 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1133 /* Status transport phase, setup transfer */
1134 if (sc
->transfer_state
== TSTATE_BBB_COMMAND
||
1135 sc
->transfer_state
== TSTATE_BBB_DATA
||
1136 sc
->transfer_state
== TSTATE_BBB_DCLEAR
) {
1137 /* After no data phase, successful data phase and
1138 * after clearing bulk-in/-out stall condition
1140 sc
->transfer_state
= TSTATE_BBB_STATUS1
;
1141 next_xfer
= sc
->transfer_xfer
[XFER_BBB_CSW1
];
1143 /* After first attempt of fetching CSW */
1144 sc
->transfer_state
= TSTATE_BBB_STATUS2
;
1145 next_xfer
= sc
->transfer_xfer
[XFER_BBB_CSW2
];
1148 /* Read the Command Status Wrapper via bulk-in endpoint. */
1149 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKIN
],
1150 &sc
->csw
, UMASS_BBB_CSW_SIZE
, 0, next_xfer
)) {
1151 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1156 case TSTATE_BBB_STATUS1
: /* first attempt */
1157 case TSTATE_BBB_STATUS2
: /* second attempt */
1158 /* Status transfer, error handling */
1160 DPRINTF(UDMASS_BBB
, ("%s: Failed to read CSW, %s%s\n",
1161 device_xname(sc
->sc_dev
), usbd_errstr(err
),
1162 (sc
->transfer_state
== TSTATE_BBB_STATUS1
?
1165 /* If this was the first attempt at fetching the CSW
1166 * retry it, otherwise fail.
1168 if (sc
->transfer_state
== TSTATE_BBB_STATUS1
) {
1169 sc
->transfer_state
= TSTATE_BBB_SCLEAR
;
1170 umass_clear_endpoint_stall(sc
, UMASS_BULKIN
,
1171 sc
->transfer_xfer
[XFER_BBB_SCLEAR
]);
1174 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1179 DIF(UDMASS_BBB
, umass_bbb_dump_csw(sc
, &sc
->csw
));
1181 if (sc
->sc_quirks
& UMASS_QUIRK_IGNORE_RESIDUE
)
1182 residue
= sc
->transfer_datalen
- sc
->transfer_actlen
;
1184 residue
= UGETDW(sc
->csw
.dCSWDataResidue
);
1186 /* Translate weird command-status signatures. */
1187 if ((sc
->sc_quirks
& UMASS_QUIRK_WRONG_CSWSIG
) &&
1188 UGETDW(sc
->csw
.dCSWSignature
) == CSWSIGNATURE_OLYMPUS_C1
)
1189 USETDW(sc
->csw
.dCSWSignature
, CSWSIGNATURE
);
1191 /* Translate invalid command-status tags */
1192 if (sc
->sc_quirks
& UMASS_QUIRK_WRONG_CSWTAG
)
1193 USETDW(sc
->csw
.dCSWTag
, UGETDW(sc
->cbw
.dCBWTag
));
1195 /* Check CSW and handle any error */
1196 if (UGETDW(sc
->csw
.dCSWSignature
) != CSWSIGNATURE
) {
1197 /* Invalid CSW: Wrong signature or wrong tag might
1198 * indicate that the device is confused -> reset it.
1200 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
1201 device_xname(sc
->sc_dev
),
1202 UGETDW(sc
->csw
.dCSWSignature
),
1205 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1207 } else if (UGETDW(sc
->csw
.dCSWTag
)
1208 != UGETDW(sc
->cbw
.dCBWTag
)) {
1209 printf("%s: Invalid CSW: tag %d should be %d\n",
1210 device_xname(sc
->sc_dev
),
1211 UGETDW(sc
->csw
.dCSWTag
),
1212 UGETDW(sc
->cbw
.dCBWTag
));
1214 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1217 /* CSW is valid here */
1218 } else if (sc
->csw
.bCSWStatus
> CSWSTATUS_PHASE
) {
1219 printf("%s: Invalid CSW: status %d > %d\n",
1220 device_xname(sc
->sc_dev
),
1224 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1226 } else if (sc
->csw
.bCSWStatus
== CSWSTATUS_PHASE
) {
1227 printf("%s: Phase Error, residue = %d\n",
1228 device_xname(sc
->sc_dev
), residue
);
1230 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1233 } else if (sc
->transfer_actlen
> sc
->transfer_datalen
) {
1234 /* Buffer overrun! Don't let this go by unnoticed */
1235 panic("%s: transferred %d bytes instead of %d bytes",
1236 device_xname(sc
->sc_dev
),
1237 sc
->transfer_actlen
, sc
->transfer_datalen
);
1239 } else if (sc
->transfer_datalen
- sc
->transfer_actlen
1241 DPRINTF(UDMASS_BBB
, ("%s: actlen=%d != residue=%d\n",
1242 device_xname(sc
->sc_dev
),
1243 sc
->transfer_datalen
- sc
->transfer_actlen
,
1246 umass_bbb_reset(sc
, STATUS_WIRE_FAILED
);
1249 } else if (sc
->csw
.bCSWStatus
== CSWSTATUS_FAILED
) {
1250 DPRINTF(UDMASS_BBB
, ("%s: Command Failed, res = %d\n",
1251 device_xname(sc
->sc_dev
), residue
));
1253 /* SCSI command failed but transfer was succesful */
1254 sc
->transfer_state
= TSTATE_IDLE
;
1255 sc
->transfer_cb(sc
, sc
->transfer_priv
, residue
,
1260 } else { /* success */
1261 sc
->transfer_state
= TSTATE_IDLE
;
1262 sc
->transfer_cb(sc
, sc
->transfer_priv
, residue
,
1268 /***** Bulk Reset *****/
1269 case TSTATE_BBB_RESET1
:
1271 printf("%s: BBB reset failed, %s\n",
1272 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1274 sc
->transfer_state
= TSTATE_BBB_RESET2
;
1275 umass_clear_endpoint_stall(sc
, UMASS_BULKIN
,
1276 sc
->transfer_xfer
[XFER_BBB_RESET2
]);
1279 case TSTATE_BBB_RESET2
:
1280 if (err
) /* should not occur */
1281 printf("%s: BBB bulk-in clear stall failed, %s\n",
1282 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1283 /* no error recovery, otherwise we end up in a loop */
1285 sc
->transfer_state
= TSTATE_BBB_RESET3
;
1286 umass_clear_endpoint_stall(sc
, UMASS_BULKOUT
,
1287 sc
->transfer_xfer
[XFER_BBB_RESET3
]);
1290 case TSTATE_BBB_RESET3
:
1291 if (err
) /* should not occur */
1292 printf("%s: BBB bulk-out clear stall failed, %s\n",
1293 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1294 /* no error recovery, otherwise we end up in a loop */
1296 sc
->transfer_state
= TSTATE_IDLE
;
1297 if (sc
->transfer_priv
) {
1298 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1299 sc
->transfer_datalen
,
1300 sc
->transfer_status
);
1305 /***** Default *****/
1307 panic("%s: Unknown state %d",
1308 device_xname(sc
->sc_dev
), sc
->transfer_state
);
1313 * Command/Bulk/Interrupt (CBI) specific functions
1317 umass_cbi_adsc(struct umass_softc
*sc
, char *buffer
, int buflen
,
1318 usbd_xfer_handle xfer
)
1320 KASSERT(sc
->sc_wire
& (UMASS_WPROTO_CBI
|UMASS_WPROTO_CBI_I
),
1321 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n",
1324 if ((sc
->sc_cmd
== UMASS_CPROTO_RBC
) &&
1325 (sc
->sc_quirks
& UMASS_QUIRK_RBC_PAD_TO_12
) != 0 && buflen
< 12) {
1326 (void)memset(buffer
+ buflen
, 0, 12 - buflen
);
1330 sc
->sc_req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
1331 sc
->sc_req
.bRequest
= UR_CBI_ADSC
;
1332 USETW(sc
->sc_req
.wValue
, 0);
1333 USETW(sc
->sc_req
.wIndex
, sc
->sc_ifaceno
);
1334 USETW(sc
->sc_req
.wLength
, buflen
);
1335 return umass_setup_ctrl_transfer(sc
, &sc
->sc_req
, buffer
,
1341 umass_cbi_reset(struct umass_softc
*sc
, int status
)
1344 # define SEND_DIAGNOSTIC_CMDLEN 12
1346 KASSERT(sc
->sc_wire
& (UMASS_WPROTO_CBI
|UMASS_WPROTO_CBI_I
),
1347 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n",
1354 * Command Block Reset Protocol
1356 * First send a reset request to the device. Then clear
1357 * any possibly stalled bulk endpoints.
1359 * This is done in 3 steps, states:
1364 * If the reset doesn't succeed, the device should be port reset.
1367 DPRINTF(UDMASS_CBI
, ("%s: CBI Reset\n",
1368 device_xname(sc
->sc_dev
)));
1370 KASSERT(sizeof(sc
->cbl
) >= SEND_DIAGNOSTIC_CMDLEN
,
1371 ("%s: CBL struct is too small (%d < %d)\n",
1372 device_xname(sc
->sc_dev
),
1373 sizeof(sc
->cbl
), SEND_DIAGNOSTIC_CMDLEN
));
1375 sc
->transfer_state
= TSTATE_CBI_RESET1
;
1376 sc
->transfer_status
= status
;
1378 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
1379 * the two the last 10 bytes of the cbl is filled with 0xff (section
1380 * 2.2 of the CBI spec).
1382 sc
->cbl
[0] = 0x1d; /* Command Block Reset */
1384 for (i
= 2; i
< SEND_DIAGNOSTIC_CMDLEN
; i
++)
1387 umass_cbi_adsc(sc
, sc
->cbl
, SEND_DIAGNOSTIC_CMDLEN
,
1388 sc
->transfer_xfer
[XFER_CBI_RESET1
]);
1389 /* XXX if the command fails we should reset the port on the bub */
1393 umass_cbi_transfer(struct umass_softc
*sc
, int lun
,
1394 void *cmd
, int cmdlen
, void *data
, int datalen
, int dir
,
1395 u_int timeout
, umass_callback cb
, void *priv
)
1397 DPRINTF(UDMASS_CBI
,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
1398 device_xname(sc
->sc_dev
), *(u_char
*)cmd
, datalen
));
1400 KASSERT(sc
->sc_wire
& (UMASS_WPROTO_CBI
|UMASS_WPROTO_CBI_I
),
1401 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n",
1407 /* Be a little generous. */
1408 sc
->timeout
= timeout
+ USBD_DEFAULT_TIMEOUT
;
1411 * Do a CBI transfer with cmdlen bytes from cmd, possibly
1412 * a data phase of datalen bytes from/to the device and finally a
1414 * If the data direction was inbound a maximum of datalen bytes
1415 * is stored in the buffer pointed to by data.
1417 * umass_cbi_transfer initialises the transfer and lets the state
1418 * machine in umass_cbi_state handle the completion. It uses the
1420 * TSTATE_CBI_COMMAND
1423 * An error in any of those states will invoke
1427 /* check the given arguments */
1428 KASSERT(datalen
== 0 || data
!= NULL
,
1429 ("%s: datalen > 0, but no buffer",device_xname(sc
->sc_dev
)));
1430 KASSERT(datalen
== 0 || dir
!= DIR_NONE
,
1431 ("%s: direction is NONE while datalen is not zero\n",
1432 device_xname(sc
->sc_dev
)));
1434 /* store the details for the data transfer phase */
1435 sc
->transfer_dir
= dir
;
1436 sc
->transfer_data
= data
;
1437 sc
->transfer_datalen
= datalen
;
1438 sc
->transfer_actlen
= 0;
1439 sc
->transfer_cb
= cb
;
1440 sc
->transfer_priv
= priv
;
1441 sc
->transfer_status
= STATUS_CMD_OK
;
1443 /* move from idle to the command state */
1444 sc
->transfer_state
= TSTATE_CBI_COMMAND
;
1446 /* Send the Command Block from host to device via control endpoint. */
1447 if (umass_cbi_adsc(sc
, cmd
, cmdlen
, sc
->transfer_xfer
[XFER_CBI_CB
]))
1448 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1452 umass_cbi_state(usbd_xfer_handle xfer
, usbd_private_handle priv
,
1455 struct umass_softc
*sc
= (struct umass_softc
*) priv
;
1457 KASSERT(sc
->sc_wire
& (UMASS_WPROTO_CBI
|UMASS_WPROTO_CBI_I
),
1458 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n",
1465 * State handling for CBI transfers.
1468 DPRINTF(UDMASS_CBI
, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
1469 device_xname(sc
->sc_dev
), sc
->transfer_state
,
1470 states
[sc
->transfer_state
], xfer
, usbd_errstr(err
)));
1472 switch (sc
->transfer_state
) {
1474 /***** CBI Transfer *****/
1475 case TSTATE_CBI_COMMAND
:
1476 if (err
== USBD_STALLED
) {
1477 DPRINTF(UDMASS_CBI
, ("%s: Command Transport failed\n",
1478 device_xname(sc
->sc_dev
)));
1479 /* Status transport by control pipe (section 2.3.2.1).
1480 * The command contained in the command block failed.
1482 * The control pipe has already been unstalled by the
1484 * Section 2.4.3.1.1 states that the bulk in endpoints
1485 * should not stalled at this point.
1488 sc
->transfer_state
= TSTATE_IDLE
;
1489 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1490 sc
->transfer_datalen
,
1495 DPRINTF(UDMASS_CBI
, ("%s: failed to send ADSC\n",
1496 device_xname(sc
->sc_dev
)));
1497 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1501 /* Data transport phase, setup transfer */
1502 sc
->transfer_state
= TSTATE_CBI_DATA
;
1503 if (sc
->transfer_dir
== DIR_IN
) {
1504 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKIN
],
1505 sc
->data_buffer
, sc
->transfer_datalen
,
1506 USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1507 sc
->transfer_xfer
[XFER_CBI_DATA
]))
1508 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1511 } else if (sc
->transfer_dir
== DIR_OUT
) {
1512 memcpy(sc
->data_buffer
, sc
->transfer_data
,
1513 sc
->transfer_datalen
);
1514 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_BULKOUT
],
1515 sc
->data_buffer
, sc
->transfer_datalen
,
1516 USBD_NO_COPY
,/* fixed length transfer */
1517 sc
->transfer_xfer
[XFER_CBI_DATA
]))
1518 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1522 DPRINTF(UDMASS_CBI
, ("%s: no data phase\n",
1523 device_xname(sc
->sc_dev
)));
1526 /* FALLTHROUGH if no data phase, err == 0 */
1527 case TSTATE_CBI_DATA
:
1528 /* Command transport phase error handling (ignored if no data
1529 * phase (fallthrough from previous state)) */
1530 if (sc
->transfer_dir
!= DIR_NONE
) {
1531 /* retrieve the length of the transfer that was done */
1532 usbd_get_xfer_status(xfer
, NULL
, NULL
,
1533 &sc
->transfer_actlen
, NULL
);
1534 DPRINTF(UDMASS_CBI
, ("%s: CBI_DATA actlen=%d\n",
1535 device_xname(sc
->sc_dev
), sc
->transfer_actlen
));
1538 DPRINTF(UDMASS_CBI
, ("%s: Data-%s %d failed, "
1539 "%s\n", device_xname(sc
->sc_dev
),
1540 (sc
->transfer_dir
== DIR_IN
?"in":"out"),
1541 sc
->transfer_datalen
,usbd_errstr(err
)));
1543 if (err
== USBD_STALLED
) {
1544 sc
->transfer_state
= TSTATE_CBI_DCLEAR
;
1545 umass_clear_endpoint_stall(sc
,
1546 (sc
->transfer_dir
== DIR_IN
?
1547 UMASS_BULKIN
:UMASS_BULKOUT
),
1548 sc
->transfer_xfer
[XFER_CBI_DCLEAR
]);
1550 /* Unless the error is a pipe stall the
1553 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1559 if (sc
->transfer_dir
== DIR_IN
)
1560 memcpy(sc
->transfer_data
, sc
->data_buffer
,
1561 sc
->transfer_actlen
);
1563 DIF(UDMASS_CBI
, if (sc
->transfer_dir
== DIR_IN
)
1564 umass_dump_buffer(sc
, sc
->transfer_data
,
1565 sc
->transfer_actlen
, 48));
1568 if (sc
->sc_wire
== UMASS_WPROTO_CBI_I
) {
1569 sc
->transfer_state
= TSTATE_CBI_STATUS
;
1570 memset(&sc
->sbl
, 0, sizeof(sc
->sbl
));
1571 if (umass_setup_transfer(sc
, sc
->sc_pipe
[UMASS_INTRIN
],
1572 &sc
->sbl
, sizeof(sc
->sbl
),
1573 0, /* fixed length transfer */
1574 sc
->transfer_xfer
[XFER_CBI_STATUS
]))
1575 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1577 /* No command completion interrupt. Request
1578 * sense to get status of command.
1580 sc
->transfer_state
= TSTATE_IDLE
;
1581 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1582 sc
->transfer_datalen
- sc
->transfer_actlen
,
1583 STATUS_CMD_UNKNOWN
);
1587 case TSTATE_CBI_STATUS
:
1589 DPRINTF(UDMASS_CBI
, ("%s: Status Transport failed\n",
1590 device_xname(sc
->sc_dev
)));
1591 /* Status transport by interrupt pipe (section 2.3.2.2).
1594 if (err
== USBD_STALLED
) {
1595 sc
->transfer_state
= TSTATE_CBI_SCLEAR
;
1596 umass_clear_endpoint_stall(sc
, UMASS_INTRIN
,
1597 sc
->transfer_xfer
[XFER_CBI_SCLEAR
]);
1599 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1604 /* Dissect the information in the buffer */
1608 usbd_get_xfer_status(xfer
,NULL
,NULL
,&actlen
,NULL
);
1609 DPRINTF(UDMASS_CBI
, ("%s: CBI_STATUS actlen=%d\n",
1610 device_xname(sc
->sc_dev
), actlen
));
1615 if (sc
->sc_cmd
== UMASS_CPROTO_UFI
) {
1618 /* Section 3.4.3.1.3 specifies that the UFI command
1619 * protocol returns an ASC and ASCQ in the interrupt
1623 DPRINTF(UDMASS_CBI
, ("%s: UFI CCI, ASC = 0x%02x, "
1625 device_xname(sc
->sc_dev
),
1626 sc
->sbl
.ufi
.asc
, sc
->sbl
.ufi
.ascq
));
1628 if ((sc
->sbl
.ufi
.asc
== 0 && sc
->sbl
.ufi
.ascq
== 0) ||
1630 status
= STATUS_CMD_OK
;
1632 status
= STATUS_CMD_FAILED
;
1634 /* No autosense, command successful */
1635 sc
->transfer_state
= TSTATE_IDLE
;
1636 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1637 sc
->transfer_datalen
- sc
->transfer_actlen
, status
);
1641 /* Command Interrupt Data Block */
1643 DPRINTF(UDMASS_CBI
, ("%s: type=0x%02x, value=0x%02x\n",
1644 device_xname(sc
->sc_dev
),
1645 sc
->sbl
.common
.type
, sc
->sbl
.common
.value
));
1647 if (sc
->sbl
.common
.type
== IDB_TYPE_CCI
) {
1648 switch (sc
->sbl
.common
.value
& IDB_VALUE_STATUS_MASK
) {
1649 case IDB_VALUE_PASS
:
1650 status
= STATUS_CMD_OK
;
1652 case IDB_VALUE_FAIL
:
1653 case IDB_VALUE_PERSISTENT
:
1654 status
= STATUS_CMD_FAILED
;
1656 case IDB_VALUE_PHASE
:
1657 default: /* XXX: gcc */
1658 status
= STATUS_WIRE_FAILED
;
1662 sc
->transfer_state
= TSTATE_IDLE
;
1663 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1664 sc
->transfer_datalen
- sc
->transfer_actlen
, status
);
1669 case TSTATE_CBI_DCLEAR
:
1670 if (err
) { /* should not occur */
1671 printf("%s: CBI bulk-%s stall clear failed, %s\n",
1672 device_xname(sc
->sc_dev
),
1673 (sc
->transfer_dir
== DIR_IN
? "in":"out"),
1675 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1677 sc
->transfer_state
= TSTATE_IDLE
;
1678 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1679 sc
->transfer_datalen
, STATUS_CMD_FAILED
);
1683 case TSTATE_CBI_SCLEAR
:
1684 if (err
) { /* should not occur */
1685 printf("%s: CBI intr-in stall clear failed, %s\n",
1686 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1687 umass_cbi_reset(sc
, STATUS_WIRE_FAILED
);
1689 sc
->transfer_state
= TSTATE_IDLE
;
1690 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1691 sc
->transfer_datalen
, STATUS_CMD_FAILED
);
1695 /***** CBI Reset *****/
1696 case TSTATE_CBI_RESET1
:
1698 printf("%s: CBI reset failed, %s\n",
1699 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1701 sc
->transfer_state
= TSTATE_CBI_RESET2
;
1702 umass_clear_endpoint_stall(sc
, UMASS_BULKIN
,
1703 sc
->transfer_xfer
[XFER_CBI_RESET2
]);
1706 case TSTATE_CBI_RESET2
:
1707 if (err
) /* should not occur */
1708 printf("%s: CBI bulk-in stall clear failed, %s\n",
1709 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1710 /* no error recovery, otherwise we end up in a loop */
1712 sc
->transfer_state
= TSTATE_CBI_RESET3
;
1713 umass_clear_endpoint_stall(sc
, UMASS_BULKOUT
,
1714 sc
->transfer_xfer
[XFER_CBI_RESET3
]);
1717 case TSTATE_CBI_RESET3
:
1718 if (err
) /* should not occur */
1719 printf("%s: CBI bulk-out stall clear failed, %s\n",
1720 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1721 /* no error recovery, otherwise we end up in a loop */
1723 sc
->transfer_state
= TSTATE_IDLE
;
1724 if (sc
->transfer_priv
) {
1725 sc
->transfer_cb(sc
, sc
->transfer_priv
,
1726 sc
->transfer_datalen
,
1727 sc
->transfer_status
);
1733 /***** Default *****/
1735 panic("%s: Unknown state %d",
1736 device_xname(sc
->sc_dev
), sc
->transfer_state
);
1741 umass_bbb_get_max_lun(struct umass_softc
*sc
, u_int8_t
*maxlun
)
1743 usb_device_request_t req
;
1746 *maxlun
= 0; /* Default to 0. */
1748 DPRINTF(UDMASS_BBB
, ("%s: Get Max Lun\n", device_xname(sc
->sc_dev
)));
1750 /* The Get Max Lun command is a class-specific request. */
1751 req
.bmRequestType
= UT_READ_CLASS_INTERFACE
;
1752 req
.bRequest
= UR_BBB_GET_MAX_LUN
;
1753 USETW(req
.wValue
, 0);
1754 USETW(req
.wIndex
, sc
->sc_ifaceno
);
1755 USETW(req
.wLength
, 1);
1757 err
= usbd_do_request_flags(sc
->sc_udev
, &req
, maxlun
,
1758 USBD_SHORT_XFER_OK
, 0, USBD_DEFAULT_TIMEOUT
);
1760 case USBD_NORMAL_COMPLETION
:
1761 DPRINTF(UDMASS_BBB
, ("%s: Max Lun %d\n",
1762 device_xname(sc
->sc_dev
), *maxlun
));
1767 * Device doesn't support Get Max Lun request.
1769 err
= USBD_NORMAL_COMPLETION
;
1770 DPRINTF(UDMASS_BBB
, ("%s: Get Max Lun not supported\n",
1771 device_xname(sc
->sc_dev
)));
1774 case USBD_SHORT_XFER
:
1776 * XXX This must mean Get Max Lun is not supported, too!
1778 err
= USBD_NORMAL_COMPLETION
;
1779 DPRINTF(UDMASS_BBB
, ("%s: Get Max Lun SHORT_XFER\n",
1780 device_xname(sc
->sc_dev
)));
1784 printf("%s: Get Max Lun failed: %s\n",
1785 device_xname(sc
->sc_dev
), usbd_errstr(err
));
1786 /* XXX Should we port_reset the device? */
1798 umass_bbb_dump_cbw(struct umass_softc
*sc
, umass_bbb_cbw_t
*cbw
)
1800 int clen
= cbw
->bCDBLength
;
1801 int dlen
= UGETDW(cbw
->dCBWDataTransferLength
);
1802 u_int8_t
*c
= cbw
->CBWCDB
;
1803 int tag
= UGETDW(cbw
->dCBWTag
);
1804 int flags
= cbw
->bCBWFlags
;
1806 DPRINTF(UDMASS_BBB
, ("%s: CBW %d: cmdlen=%d "
1807 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), "
1808 "data = %d bytes, dir = %s\n",
1809 device_xname(sc
->sc_dev
), tag
, clen
,
1810 c
[0], c
[1], c
[2], c
[3], c
[4], c
[5],
1811 c
[6], c
[7], c
[8], c
[9],
1812 (clen
> 10? "...":""),
1813 dlen
, (flags
== CBWFLAGS_IN
? "in":
1814 (flags
== CBWFLAGS_OUT
? "out":"<invalid>"))));
1818 umass_bbb_dump_csw(struct umass_softc
*sc
, umass_bbb_csw_t
*csw
)
1820 int sig
= UGETDW(csw
->dCSWSignature
);
1821 int tag
= UGETDW(csw
->dCSWTag
);
1822 int res
= UGETDW(csw
->dCSWDataResidue
);
1823 int status
= csw
->bCSWStatus
;
1825 DPRINTF(UDMASS_BBB
, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
1826 "res = %d, status = 0x%02x (%s)\n", device_xname(sc
->sc_dev
),
1827 tag
, sig
, (sig
== CSWSIGNATURE
? "valid":"invalid"),
1829 status
, (status
== CSWSTATUS_GOOD
? "good":
1830 (status
== CSWSTATUS_FAILED
? "failed":
1831 (status
== CSWSTATUS_PHASE
? "phase":"<invalid>")))));
1835 umass_dump_buffer(struct umass_softc
*sc
, u_int8_t
*buffer
, int buflen
,
1846 snprintf(s2
, sizeof(s2
), " buffer=%p, buflen=%d", buffer
, buflen
);
1847 for (i
= 0; i
< buflen
&& i
< printlen
; i
++) {
1849 if (j
== 0 && i
!= 0) {
1850 DPRINTF(UDMASS_GEN
, ("%s: 0x %s%s\n",
1851 device_xname(sc
->sc_dev
), s1
, s2
));
1854 snprintf(&s1
[j
* 2], sizeof(s1
) - j
* 2, "%02x",
1857 if (buflen
> printlen
)
1858 snprintf(s3
, sizeof(s3
), " ...");
1859 DPRINTF(UDMASS_GEN
, ("%s: 0x %s%s%s\n",
1860 device_xname(sc
->sc_dev
), s1
, s2
, s3
));