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