Home | History | Annotate | Line # | Download | only in usb
umass.c revision 1.28
      1  1.28  augustss /*	$NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss 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.28  augustss  * Universal Serial Bus Mass Storage Class Bulk-Only Transport
     33  1.28  augustss  * http://www.usb.org/developers/usbmassbulk_09.pdf
     34  1.28  augustss  * XXX Add URL to CBI spec in www.usb.org
     35  1.28  augustss  */
     36  1.28  augustss 
     37  1.28  augustss /*
     38  1.28  augustss  * Ported to NetBSD by Lennart Augustsson <augustss (at) netbsd.org>.
     39  1.28  augustss  * Parts of the code written my Jason R. Thorpe <thorpej (at) shagadelic.org>.
     40   1.1   thorpej  */
     41   1.1   thorpej 
     42   1.1   thorpej /*
     43  1.28  augustss  * The driver handles 3 Wire Protocols
     44  1.28  augustss  * - Command/Bulk/Interrupt (CBI)
     45  1.28  augustss  * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
     46  1.28  augustss  * - Mass Storage Bulk-Only (BBB)
     47  1.28  augustss  *   (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
     48  1.28  augustss  *
     49  1.28  augustss  * Over these wire protocols it handles the following command protocols
     50  1.28  augustss  * - SCSI
     51  1.28  augustss  * - UFI (floppy command set)
     52  1.28  augustss  * - 8070 (ATA/ATAPI)
     53   1.1   thorpej  *
     54  1.28  augustss  * UFI and 8070i are transformed versions of the SCSI command set. The
     55  1.28  augustss  * sc->transform method is used to convert the commands into the appropriate
     56  1.28  augustss  * format (if at all necessary). For example, UFI requires all commands to be
     57  1.28  augustss  * 12 bytes in length amongst other things.
     58   1.3   thorpej  *
     59  1.28  augustss  * The source code below is marked and can be split into a number of pieces
     60  1.28  augustss  * (in this order):
     61   1.3   thorpej  *
     62  1.28  augustss  * - probe/attach/detach
     63  1.28  augustss  * - generic transfer routines
     64  1.28  augustss  * - BBB
     65  1.28  augustss  * - CBI
     66  1.28  augustss  * - CBI_I (in addition to functions from CBI)
     67  1.28  augustss  * - CAM (Common Access Method)
     68  1.28  augustss  * - SCSI
     69  1.28  augustss  * - UFI
     70  1.28  augustss  * - 8070i
     71   1.3   thorpej  *
     72  1.28  augustss  * The protocols are implemented using a state machine, for the transfers as
     73  1.28  augustss  * well as for the resets. The state machine is contained in umass_*_state.
     74  1.28  augustss  * The state machine is started through either umass_*_transfer or
     75  1.28  augustss  * umass_*_reset.
     76  1.28  augustss  *
     77  1.28  augustss  * The reason for doing this is a) CAM performs a lot better this way and b) it
     78  1.28  augustss  * avoids using tsleep from interrupt context (for example after a failed
     79  1.28  augustss  * transfer).
     80   1.1   thorpej  */
     81   1.1   thorpej 
     82  1.28  augustss /*
     83  1.28  augustss  * The SCSI related part of this driver has been derived from the
     84  1.28  augustss  * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch (at) freebsd.org).
     85   1.1   thorpej  *
     86  1.28  augustss  * The CAM layer uses so called actions which are messages sent to the host
     87  1.28  augustss  * adapter for completion. The actions come in through umass_cam_action. The
     88  1.28  augustss  * appropriate block of routines is called depending on the transport protocol
     89  1.28  augustss  * in use. When the transfer has finished, these routines call
     90  1.28  augustss  * umass_cam_cb again to complete the CAM command.
     91   1.1   thorpej  */
     92   1.1   thorpej 
     93  1.28  augustss /* XXX Should we split the driver into a number of files?  umass.c,
     94  1.28  augustss  *     umass_scsi.c, umass_8070.c, umass_ufi.c, umass_bbb.c, umass_cbi.c or
     95  1.28  augustss  *     something similar?
     96   1.1   thorpej  */
     97   1.1   thorpej 
     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.28  augustss #if defined(__FreeBSD__)
    121  1.28  augustss #include <cam/cam.h>
    122  1.28  augustss #include <cam/cam_ccb.h>
    123  1.28  augustss #include <cam/cam_sim.h>
    124  1.28  augustss #include <cam/cam_xpt_sim.h>
    125  1.28  augustss #include <cam/scsi/scsi_all.h>
    126  1.28  augustss #include <cam/scsi/scsi_da.h>
    127  1.28  augustss 
    128  1.28  augustss #ifdef UMASS_DO_CAM_RESCAN
    129  1.28  augustss #include <sys/devicestat.h>
    130  1.28  augustss #include <cam/cam_periph.h>
    131  1.28  augustss #endif
    132  1.28  augustss 
    133  1.28  augustss #elif defined(__NetBSD__) || defined(__OpenBSD__)
    134  1.28  augustss #include <sys/scsiio.h>
    135   1.1   thorpej #include <dev/scsipi/scsi_all.h>
    136   1.1   thorpej #include <dev/scsipi/scsipi_all.h>
    137  1.28  augustss #include <dev/scsipi/scsiconf.h>
    138  1.28  augustss 
    139  1.28  augustss #include <dev/scsipi/atapiconf.h>
    140  1.28  augustss 
    141  1.28  augustss #include <dev/scsipi/scsipi_disk.h>
    142  1.28  augustss #include <dev/scsipi/scsi_disk.h>
    143  1.28  augustss #include <dev/scsipi/scsi_changer.h>
    144   1.1   thorpej 
    145  1.28  augustss #include <dev/ata/atavar.h>	/* XXX */
    146  1.18  augustss #endif
    147  1.18  augustss 
    148   1.1   thorpej #ifdef UMASS_DEBUG
    149  1.28  augustss #define DIF(m, x)	if (umassdebug & (m)) do { x ; } while (0)
    150  1.28  augustss #define DPRINTF(m, x)	if (umassdebug & (m)) logprintf x
    151  1.28  augustss #define UDMASS_UPPER	0x00008000	/* upper layer */
    152  1.28  augustss #define UDMASS_GEN	0x00010000	/* general */
    153  1.28  augustss #define UDMASS_SCSI	0x00020000	/* scsi */
    154  1.28  augustss #define UDMASS_UFI	0x00040000	/* ufi command set */
    155  1.28  augustss #define UDMASS_8070	0x00080000	/* 8070i command set */
    156  1.28  augustss #define UDMASS_USB	0x00100000	/* USB general */
    157  1.28  augustss #define UDMASS_BBB	0x00200000	/* Bulk-Only transfers */
    158  1.28  augustss #define UDMASS_CBI	0x00400000	/* CBI transfers */
    159  1.28  augustss #define UDMASS_ALL	0xffff0000	/* all of the above */
    160  1.28  augustss 
    161  1.28  augustss #define UDMASS_XFER	0x40000000	/* all transfers */
    162  1.28  augustss #define UDMASS_CMD	0x80000000
    163  1.28  augustss 
    164  1.28  augustss int umassdebug = 0; //UDMASS_ALL;
    165   1.1   thorpej #else
    166  1.28  augustss #define DIF(m, x)	/* nop */
    167  1.28  augustss #define DPRINTF(m, x)	/* nop */
    168   1.1   thorpej #endif
    169   1.1   thorpej 
    170   1.1   thorpej 
    171  1.28  augustss /* Generic definitions */
    172  1.28  augustss 
    173  1.28  augustss /* Direction for umass_*_transfer */
    174  1.28  augustss #define DIR_NONE	0
    175  1.28  augustss #define DIR_IN		1
    176  1.28  augustss #define DIR_OUT		2
    177  1.28  augustss 
    178  1.28  augustss /* The transfer speed determines the timeout value */
    179  1.28  augustss #define UMASS_DEFAULT_TRANSFER_SPEED	150	/* in kb/s, conservative est. */
    180  1.28  augustss #define UMASS_FLOPPY_TRANSFER_SPEED	20
    181  1.28  augustss #define UMASS_ZIP100_TRANSFER_SPEED	650
    182  1.28  augustss 
    183  1.28  augustss #define UMASS_SPINUP_TIME 10000	/* ms */
    184  1.28  augustss 
    185  1.28  augustss #ifdef __FreeBSD__
    186  1.28  augustss /* device name */
    187  1.28  augustss #define DEVNAME		"umass"
    188  1.28  augustss #define DEVNAME_SIM	"umass-"
    189  1.17   thorpej 
    190  1.28  augustss #define UMASS_MAX_TRANSFER_SIZE		65536
    191   1.1   thorpej 
    192  1.28  augustss /* CAM specific definitions */
    193   1.9   thorpej 
    194  1.28  augustss /* The bus id, whatever that is */
    195  1.28  augustss #define UMASS_SCSI_BUS		0
    196   1.9   thorpej 
    197  1.28  augustss /* All USB drives are 'connected' to one SIM (SCSI controller). umass3
    198  1.28  augustss  * ends up being target 3 on that SIM. When a request for target 3
    199  1.28  augustss  * comes in we fetch the softc with devclass_get_softc(target_id).
    200  1.28  augustss  *
    201  1.28  augustss  * The SIM is the highest target number. This makes sure that umass0 corresponds
    202  1.28  augustss  * to target 0 on the USB SCSI bus.
    203  1.28  augustss  */
    204  1.28  augustss #ifndef UMASS_DEBUG
    205  1.28  augustss #define UMASS_SCSIID_MAX	32	/* maximum number of drives expected */
    206  1.28  augustss #else
    207  1.28  augustss /* while debugging avoid unnecessary clutter in the output at umass_cam_rescan
    208  1.28  augustss  * (XPT_PATH_INQ)
    209  1.28  augustss  */
    210  1.28  augustss #define UMASS_SCSIID_MAX	3	/* maximum number of drives expected */
    211  1.28  augustss #endif
    212  1.28  augustss #define UMASS_SCSIID_HOST	UMASS_SCSIID_MAX
    213  1.28  augustss #endif
    214   1.1   thorpej 
    215  1.28  augustss #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
    216   1.1   thorpej 
    217   1.1   thorpej 
    218  1.28  augustss /* Bulk-Only features */
    219   1.1   thorpej 
    220  1.28  augustss #define UR_BBB_RESET	0xff		/* Bulk-Only reset */
    221  1.28  augustss #define	UR_BBB_GET_MAX_LUN	0xfe
    222   1.1   thorpej 
    223   1.1   thorpej /* Command Block Wrapper */
    224   1.1   thorpej typedef struct {
    225   1.1   thorpej 	uDWord		dCBWSignature;
    226  1.28  augustss #	define CBWSIGNATURE	0x43425355
    227   1.1   thorpej 	uDWord		dCBWTag;
    228   1.1   thorpej 	uDWord		dCBWDataTransferLength;
    229   1.1   thorpej 	uByte		bCBWFlags;
    230  1.28  augustss #	define CBWFLAGS_OUT	0x00
    231  1.28  augustss #	define CBWFLAGS_IN	0x80
    232   1.1   thorpej 	uByte		bCBWLUN;
    233   1.1   thorpej 	uByte		bCDBLength;
    234  1.28  augustss #	define CBWCDBLENGTH	16
    235  1.28  augustss 	uByte		CBWCDB[CBWCDBLENGTH];
    236  1.28  augustss } umass_bbb_cbw_t;
    237  1.28  augustss #define UMASS_BBB_CBW_SIZE	31
    238   1.1   thorpej 
    239   1.1   thorpej /* Command Status Wrapper */
    240   1.1   thorpej typedef struct {
    241   1.1   thorpej 	uDWord		dCSWSignature;
    242  1.28  augustss #	define CSWSIGNATURE	0x53425355
    243   1.1   thorpej 	uDWord		dCSWTag;
    244   1.1   thorpej 	uDWord		dCSWDataResidue;
    245   1.1   thorpej 	uByte		bCSWStatus;
    246  1.28  augustss #	define CSWSTATUS_GOOD	0x0
    247  1.28  augustss #	define CSWSTATUS_FAILED 0x1
    248  1.28  augustss #	define CSWSTATUS_PHASE	0x2
    249  1.28  augustss } umass_bbb_csw_t;
    250  1.28  augustss #define UMASS_BBB_CSW_SIZE	13
    251  1.28  augustss 
    252  1.28  augustss /* CBI features */
    253  1.28  augustss 
    254  1.28  augustss #define UR_CBI_ADSC	0x00
    255  1.28  augustss 
    256  1.28  augustss typedef unsigned char umass_cbi_cbl_t[16];	/* Command block */
    257  1.28  augustss 
    258  1.28  augustss typedef union {
    259  1.28  augustss 	struct {
    260  1.28  augustss 		unsigned char	type;
    261  1.28  augustss 		#define IDB_TYPE_CCI		0x00
    262  1.28  augustss 		unsigned char	value;
    263  1.28  augustss 		#define IDB_VALUE_PASS		0x00
    264  1.28  augustss 		#define IDB_VALUE_FAIL		0x01
    265  1.28  augustss 		#define IDB_VALUE_PHASE		0x02
    266  1.28  augustss 		#define IDB_VALUE_PERSISTENT	0x03
    267  1.28  augustss 		#define IDB_VALUE_STATUS_MASK	0x03
    268  1.28  augustss 	} common;
    269  1.28  augustss 
    270  1.28  augustss 	struct {
    271  1.28  augustss 		unsigned char	asc;
    272  1.28  augustss 		unsigned char	ascq;
    273  1.28  augustss 	} ufi;
    274  1.28  augustss } umass_cbi_sbl_t;
    275  1.28  augustss 
    276  1.28  augustss 
    277  1.28  augustss 
    278  1.28  augustss struct umass_softc;		/* see below */
    279  1.28  augustss 
    280  1.28  augustss typedef void (*transfer_cb_f)	__P((struct umass_softc *sc, void *priv,
    281  1.28  augustss 				int residue, int status));
    282  1.28  augustss #define STATUS_CMD_OK		0	/* everything ok */
    283  1.28  augustss #define STATUS_CMD_UNKNOWN	1	/* will have to fetch sense */
    284  1.28  augustss #define STATUS_CMD_FAILED	2	/* transfer was ok, command failed */
    285  1.28  augustss #define STATUS_WIRE_FAILED	3	/* couldn't even get command across */
    286  1.28  augustss 
    287  1.28  augustss typedef void (*wire_reset_f)	__P((struct umass_softc *sc, int status));
    288  1.28  augustss typedef void (*wire_transfer_f) __P((struct umass_softc *sc, int lun,
    289  1.28  augustss 				void *cmd, int cmdlen, void *data, int datalen,
    290  1.28  augustss 				int dir, transfer_cb_f cb, void *priv));
    291  1.28  augustss typedef void (*wire_state_f)	__P((usbd_xfer_handle xfer,
    292  1.28  augustss 				usbd_private_handle priv, usbd_status err));
    293  1.28  augustss 
    294  1.28  augustss #if defined(__FreeBSD__)
    295  1.28  augustss typedef int (*command_transform_f)	__P((struct umass_softc *sc,
    296  1.28  augustss 				unsigned char *cmd, int cmdlen,
    297  1.28  augustss 				unsigned char **rcmd, int *rcmdlen));
    298  1.28  augustss #endif
    299  1.28  augustss 
    300  1.28  augustss 
    301  1.28  augustss /* the per device structure */
    302  1.28  augustss struct umass_softc {
    303  1.28  augustss 	USBBASEDEVICE		sc_dev;		/* base device */
    304  1.28  augustss 	usbd_device_handle	sc_udev;	/* device */
    305  1.28  augustss 
    306  1.28  augustss 	unsigned char		drive;
    307  1.28  augustss #	define DRIVE_GENERIC		0	/* use defaults for this one */
    308  1.28  augustss #	define ZIP_100			1	/* to be used for quirks */
    309  1.28  augustss #	define SHUTTLE_EUSB		2
    310  1.28  augustss 
    311  1.28  augustss 	unsigned char		quirks;
    312  1.28  augustss 	/* The drive does not support Test Unit Ready. Convert to
    313  1.28  augustss 	 * Start Unit.
    314  1.28  augustss 	 * Y-E Data
    315  1.28  augustss 	 * ZIP 100
    316  1.28  augustss 	 */
    317  1.28  augustss #	define NO_TEST_UNIT_READY	0x01
    318  1.28  augustss 	/* The drive does not reset the Unit Attention state after
    319  1.28  augustss 	 * REQUEST SENSE has been sent. The INQUIRY command does not reset
    320  1.28  augustss 	 * the UA either, and so CAM runs in circles trying to retrieve the
    321  1.28  augustss 	 * initial INQUIRY data.
    322  1.28  augustss 	 * Y-E Data
    323  1.28  augustss 	 */
    324  1.28  augustss #	define RS_NO_CLEAR_UA		0x02	/* no REQUEST SENSE on INQUIRY*/
    325  1.28  augustss 	/* The drive does not support START_STOP.
    326  1.28  augustss 	 * Shuttle E-USB
    327  1.28  augustss 	 */
    328  1.28  augustss #	define NO_START_STOP		0x04
    329  1.28  augustss 
    330  1.28  augustss 	unsigned int		proto;
    331  1.28  augustss #	define PROTO_UNKNOWN	0x0000		/* unknown protocol */
    332  1.28  augustss #	define PROTO_BBB	0x0001		/* USB wire protocol */
    333  1.28  augustss #	define PROTO_CBI	0x0002
    334  1.28  augustss #	define PROTO_CBI_I	0x0004
    335  1.28  augustss #	define PROTO_WIRE	0x00ff		/* USB wire protocol mask */
    336  1.28  augustss #	define PROTO_SCSI	0x0100		/* command protocol */
    337  1.28  augustss #	define PROTO_8070	0x0200
    338  1.28  augustss #	define PROTO_UFI	0x0400
    339  1.28  augustss #	define PROTO_COMMAND	0xff00		/* command protocol mask */
    340  1.28  augustss 
    341  1.28  augustss 	usbd_interface_handle	iface;		/* Mass Storage interface */
    342  1.28  augustss 	int			ifaceno;	/* MS iface number */
    343  1.28  augustss 
    344  1.28  augustss 	u_int8_t		bulkin;		/* bulk-in Endpoint Address */
    345  1.28  augustss 	u_int8_t		bulkout;	/* bulk-out Endpoint Address */
    346  1.28  augustss 	u_int8_t		intrin;		/* intr-in Endp. (CBI) */
    347  1.28  augustss 	usbd_pipe_handle	bulkin_pipe;
    348  1.28  augustss 	usbd_pipe_handle	bulkout_pipe;
    349  1.28  augustss 	usbd_pipe_handle	intrin_pipe;
    350  1.28  augustss 
    351  1.28  augustss 	/* Reset the device in a wire protocol specific way */
    352  1.28  augustss 	wire_reset_f		reset;
    353  1.28  augustss 
    354  1.28  augustss 	/* The start of a wire transfer. It prepares the whole transfer (cmd,
    355  1.28  augustss 	 * data, and status stage) and initiates it. It is up to the state
    356  1.28  augustss 	 * machine (below) to handle the various stages and errors in these
    357  1.28  augustss 	 */
    358  1.28  augustss 	wire_transfer_f		transfer;
    359  1.28  augustss 
    360  1.28  augustss 	/* The state machine, handling the various states during a transfer */
    361  1.28  augustss 	wire_state_f		state;
    362  1.28  augustss 
    363  1.28  augustss #if defined(__FreeBSD__)
    364  1.28  augustss 	/* The command transform function is used to conver the SCSI commands
    365  1.28  augustss 	 * into their derivatives, like UFI, ATAPI, and friends.
    366  1.28  augustss 	 */
    367  1.28  augustss 	command_transform_f	transform;	/* command transform */
    368  1.28  augustss #endif
    369  1.28  augustss 
    370  1.28  augustss 	/* Bulk specific variables for transfers in progress */
    371  1.28  augustss 	umass_bbb_cbw_t		cbw;	/* command block wrapper */
    372  1.28  augustss 	umass_bbb_csw_t		csw;	/* command status wrapper*/
    373  1.28  augustss 	/* CBI specific variables for transfers in progress */
    374  1.28  augustss 	umass_cbi_cbl_t		cbl;	/* command block */
    375  1.28  augustss 	umass_cbi_sbl_t		sbl;	/* status block */
    376  1.28  augustss 
    377  1.28  augustss 	/* generic variables for transfers in progress */
    378  1.28  augustss 	/* ctrl transfer requests */
    379  1.28  augustss 	usb_device_request_t	request;
    380  1.28  augustss 
    381  1.28  augustss 	/* xfer handles
    382  1.28  augustss 	 * Most of our operations are initiated from interrupt context, so
    383  1.28  augustss 	 * we need to avoid using the one that is in use. We want to avoid
    384  1.28  augustss 	 * allocating them in the interrupt context as well.
    385  1.28  augustss 	 */
    386  1.28  augustss 	/* indices into array below */
    387  1.28  augustss #	define XFER_BBB_CBW		0	/* Bulk-Only */
    388  1.28  augustss #	define XFER_BBB_DATA		1
    389  1.28  augustss #	define XFER_BBB_DCLEAR		2
    390  1.28  augustss #	define XFER_BBB_CSW1		3
    391  1.28  augustss #	define XFER_BBB_CSW2		4
    392  1.28  augustss #	define XFER_BBB_SCLEAR		5
    393  1.28  augustss #	define XFER_BBB_RESET1		6
    394  1.28  augustss #	define XFER_BBB_RESET2		7
    395  1.28  augustss #	define XFER_BBB_RESET3		8
    396  1.28  augustss 
    397  1.28  augustss #	define XFER_CBI_CB		0	/* CBI */
    398  1.28  augustss #	define XFER_CBI_DATA		1
    399  1.28  augustss #	define XFER_CBI_STATUS		2
    400  1.28  augustss #	define XFER_CBI_DCLEAR		3
    401  1.28  augustss #	define XFER_CBI_SCLEAR		4
    402  1.28  augustss #	define XFER_CBI_RESET1		5
    403  1.28  augustss #	define XFER_CBI_RESET2		6
    404  1.28  augustss #	define XFER_CBI_RESET3		7
    405  1.28  augustss 
    406  1.28  augustss #	define XFER_NR			9	/* maximum number */
    407  1.28  augustss 
    408  1.28  augustss 	usbd_xfer_handle	transfer_xfer[XFER_NR]; /* for ctrl xfers */
    409  1.28  augustss 
    410  1.28  augustss 	void			*data_buffer;
    411  1.28  augustss 
    412  1.28  augustss 	int			transfer_dir;		/* data direction */
    413  1.28  augustss 	void			*transfer_data;		/* data buffer */
    414  1.28  augustss 	int			transfer_datalen;	/* (maximum) length */
    415  1.28  augustss 	int			transfer_actlen;	/* actual length */
    416  1.28  augustss 	transfer_cb_f		transfer_cb;		/* callback */
    417  1.28  augustss 	void			*transfer_priv;		/* for callback */
    418  1.28  augustss 	int			transfer_status;
    419  1.28  augustss 
    420  1.28  augustss 	int			transfer_state;
    421  1.28  augustss #	define TSTATE_IDLE			0
    422  1.28  augustss #	define TSTATE_BBB_COMMAND		1	/* CBW transfer */
    423  1.28  augustss #	define TSTATE_BBB_DATA			2	/* Data transfer */
    424  1.28  augustss #	define TSTATE_BBB_DCLEAR		3	/* clear endpt stall */
    425  1.28  augustss #	define TSTATE_BBB_STATUS1		4	/* clear endpt stall */
    426  1.28  augustss #	define TSTATE_BBB_SCLEAR		5	/* clear endpt stall */
    427  1.28  augustss #	define TSTATE_BBB_STATUS2		6	/* CSW transfer */
    428  1.28  augustss #	define TSTATE_BBB_RESET1		7	/* reset command */
    429  1.28  augustss #	define TSTATE_BBB_RESET2		8	/* in clear stall */
    430  1.28  augustss #	define TSTATE_BBB_RESET3		9	/* out clear stall */
    431  1.28  augustss #	define TSTATE_CBI_COMMAND		10	/* command transfer */
    432  1.28  augustss #	define TSTATE_CBI_DATA			11	/* data transfer */
    433  1.28  augustss #	define TSTATE_CBI_STATUS		12	/* status transfer */
    434  1.28  augustss #	define TSTATE_CBI_DCLEAR		13	/* clear ep stall */
    435  1.28  augustss #	define TSTATE_CBI_SCLEAR		14	/* clear ep stall */
    436  1.28  augustss #	define TSTATE_CBI_RESET1		15	/* reset command */
    437  1.28  augustss #	define TSTATE_CBI_RESET2		16	/* in clear stall */
    438  1.28  augustss #	define TSTATE_CBI_RESET3		17	/* out clear stall */
    439  1.28  augustss #	define TSTATE_STATES			18	/* # of states above */
    440  1.28  augustss 
    441  1.28  augustss 
    442  1.28  augustss 	int			transfer_speed;		/* in kb/s */
    443  1.28  augustss 	int			timeout;		/* in msecs */
    444  1.28  augustss 
    445  1.28  augustss 	u_int8_t		maxlun;			/* max lun supported */
    446  1.28  augustss 
    447  1.28  augustss #if defined(__FreeBSD__)
    448  1.28  augustss 	/* SCSI/CAM specific variables */
    449  1.28  augustss 	struct scsi_sense	cam_scsi_sense;
    450  1.28  augustss 
    451  1.28  augustss #elif defined(__NetBSD__) || defined(__OpenBSD__)
    452  1.28  augustss 	union {
    453  1.28  augustss 		struct scsipi_link	sc_link;
    454  1.28  augustss 		struct {
    455  1.28  augustss 			struct ata_atapi_attach	sc_aa;
    456  1.28  augustss 			struct ata_drive_datas	sc_aa_drive;
    457  1.28  augustss 		} aa;
    458  1.28  augustss 	} u;
    459  1.28  augustss 	struct atapi_adapter	sc_atapi_adapter;
    460  1.28  augustss #define sc_adapter sc_atapi_adapter._generic
    461  1.28  augustss 	int			sc_xfer_flags;
    462  1.28  augustss 	usbd_status		sc_sync_status;
    463  1.28  augustss 	struct scsipi_sense	sc_sense_cmd;
    464  1.28  augustss 
    465  1.28  augustss 	device_ptr_t		sc_child;	/* child device, for detach */
    466  1.28  augustss 	char			sc_dying;
    467  1.28  augustss 
    468  1.28  augustss #endif
    469  1.28  augustss };
    470  1.28  augustss 
    471  1.28  augustss #ifdef UMASS_DEBUG
    472  1.28  augustss char *states[TSTATE_STATES+1] = {
    473  1.28  augustss 	/* should be kept in sync with the list at transfer_state */
    474  1.28  augustss 	"Idle",
    475  1.28  augustss 	"BBB CBW",
    476  1.28  augustss 	"BBB Data",
    477  1.28  augustss 	"BBB Data bulk-in/-out clear stall",
    478  1.28  augustss 	"BBB CSW, 1st attempt",
    479  1.28  augustss 	"BBB CSW bulk-in clear stall",
    480  1.28  augustss 	"BBB CSW, 2nd attempt",
    481  1.28  augustss 	"BBB Reset",
    482  1.28  augustss 	"BBB bulk-in clear stall",
    483  1.28  augustss 	"BBB bulk-out clear stall",
    484  1.28  augustss 	"CBI Command",
    485  1.28  augustss 	"CBI Data",
    486  1.28  augustss 	"CBI Status",
    487  1.28  augustss 	"CBI Data bulk-in/-out clear stall",
    488  1.28  augustss 	"CBI Status intr-in clear stall",
    489  1.28  augustss 	"CBI Reset",
    490  1.28  augustss 	"CBI bulk-in clear stall",
    491  1.28  augustss 	"CBI bulk-out clear stall",
    492  1.28  augustss 	NULL
    493  1.28  augustss };
    494  1.28  augustss #endif
    495  1.28  augustss 
    496  1.28  augustss struct cam_sim *umass_sim;	/* SCSI Interface Module */
    497  1.28  augustss struct cam_path *umass_path;	/*   and its path */
    498   1.1   thorpej 
    499   1.1   thorpej 
    500  1.28  augustss /* USB device probe/attach/detach functions */
    501   1.1   thorpej USB_DECLARE_DRIVER(umass);
    502  1.28  augustss Static void umass_disco		__P((struct umass_softc *sc));
    503  1.28  augustss Static int umass_match_proto	__P((struct umass_softc *sc,
    504  1.28  augustss 				usbd_interface_handle iface,
    505  1.28  augustss 				usbd_device_handle dev));
    506  1.28  augustss Static void umass_init_shuttle	__P((struct umass_softc *sc));
    507   1.1   thorpej 
    508  1.28  augustss /* generic transfer functions */
    509  1.28  augustss Static usbd_status umass_setup_transfer __P((struct umass_softc *sc,
    510   1.1   thorpej 				usbd_pipe_handle pipe,
    511  1.28  augustss 				void *buffer, int buflen, int flags,
    512  1.28  augustss 				usbd_xfer_handle xfer));
    513  1.28  augustss Static usbd_status umass_setup_ctrl_transfer	__P((struct umass_softc *sc,
    514  1.28  augustss 				usbd_device_handle dev,
    515  1.28  augustss 				usb_device_request_t *req,
    516  1.28  augustss 				void *buffer, int buflen, int flags,
    517  1.28  augustss 				usbd_xfer_handle xfer));
    518  1.28  augustss Static void umass_clear_endpoint_stall	__P((struct umass_softc *sc,
    519  1.28  augustss 				u_int8_t endpt, usbd_pipe_handle pipe,
    520  1.28  augustss 				int state, usbd_xfer_handle xfer));
    521  1.28  augustss Static void umass_reset		__P((struct umass_softc *sc,
    522  1.28  augustss 				transfer_cb_f cb, void *priv));
    523   1.1   thorpej 
    524   1.1   thorpej /* Bulk-Only related functions */
    525  1.28  augustss Static void umass_bbb_reset	__P((struct umass_softc *sc, int status));
    526  1.28  augustss Static void umass_bbb_transfer	__P((struct umass_softc *sc, int lun,
    527  1.28  augustss 				void *cmd, int cmdlen,
    528  1.28  augustss 				void *data, int datalen, int dir,
    529  1.28  augustss 				transfer_cb_f cb, void *priv));
    530  1.28  augustss Static void umass_bbb_state	__P((usbd_xfer_handle xfer,
    531  1.28  augustss 				usbd_private_handle priv,
    532  1.28  augustss 				usbd_status err));
    533  1.28  augustss usbd_status umass_bbb_get_max_lun __P((struct umass_softc *sc,
    534  1.28  augustss 				       u_int8_t *maxlun));
    535  1.28  augustss 
    536  1.28  augustss 
    537  1.28  augustss /* CBI related functions */
    538  1.28  augustss Static int umass_cbi_adsc	__P((struct umass_softc *sc, char *buffer,int buflen,
    539  1.28  augustss 				usbd_xfer_handle xfer));
    540  1.28  augustss Static void umass_cbi_reset	__P((struct umass_softc *sc, int status));
    541  1.28  augustss Static void umass_cbi_transfer	__P((struct umass_softc *sc, int lun,
    542   1.1   thorpej 				void *cmd, int cmdlen,
    543  1.28  augustss 				void *data, int datalen, int dir,
    544  1.28  augustss 				transfer_cb_f cb, void *priv));
    545  1.28  augustss Static void umass_cbi_state	__P((usbd_xfer_handle xfer,
    546  1.28  augustss 				usbd_private_handle priv, usbd_status err));
    547  1.28  augustss 
    548  1.28  augustss #if defined(__FreeBSD__)
    549  1.28  augustss /* CAM related functions */
    550  1.28  augustss Static void umass_cam_action	__P((struct cam_sim *sim, union ccb *ccb));
    551  1.28  augustss Static void umass_cam_poll	__P((struct cam_sim *sim));
    552  1.28  augustss 
    553  1.28  augustss Static void umass_cam_cb	__P((struct umass_softc *sc, void *priv,
    554  1.28  augustss 				int residue, int status));
    555  1.28  augustss Static void umass_cam_sense_cb	__P((struct umass_softc *sc, void *priv,
    556  1.28  augustss 				int residue, int status));
    557  1.28  augustss 
    558  1.28  augustss #ifdef UMASS_DO_CAM_RESCAN
    559  1.28  augustss Static void umass_cam_rescan	__P((struct umass_softc *sc));
    560  1.28  augustss #endif
    561  1.28  augustss 
    562  1.28  augustss Static int umass_cam_attach_sim __P((void));
    563  1.28  augustss Static int umass_cam_attach	__P((struct umass_softc *sc));
    564  1.28  augustss Static int umass_cam_detach_sim __P((void));
    565  1.28  augustss Static int umass_cam_detach	__P((struct umass_softc *sc));
    566  1.28  augustss 
    567  1.28  augustss #elif defined(__NetBSD__) || defined(__OpenBSD__)
    568  1.28  augustss 
    569  1.28  augustss #define UMASS_SCSIID_HOST	0x00
    570  1.28  augustss #define UMASS_SCSIID_DEVICE	0x01
    571   1.1   thorpej 
    572  1.28  augustss #define UMASS_MAX_TRANSFER_SIZE	MAXBSIZE
    573  1.28  augustss 
    574  1.28  augustss struct scsipi_device umass_dev =
    575  1.28  augustss {
    576   1.1   thorpej 	NULL,			/* Use default error handler */
    577   1.1   thorpej 	NULL,			/* have a queue, served by this */
    578   1.1   thorpej 	NULL,			/* have no async handler */
    579  1.28  augustss 	NULL,			/* Use default 'done' routine */
    580   1.1   thorpej };
    581   1.1   thorpej 
    582  1.28  augustss Static int umass_scsipi_cmd __P((struct scsipi_xfer *xs));
    583  1.28  augustss Static void umass_scsipi_minphys __P((struct buf *bp));
    584  1.28  augustss Static int umass_scsipi_ioctl __P((struct scsipi_link *, u_long,
    585  1.28  augustss 				   caddr_t, int, struct proc *));
    586  1.28  augustss Static void umass_scsipi_cb	__P((struct umass_softc *sc, void *priv,
    587  1.28  augustss 				     int residue, int status));
    588  1.28  augustss Static void umass_scsipi_sense_cb __P((struct umass_softc *sc, void *priv,
    589  1.28  augustss 				       int residue, int status));
    590  1.28  augustss 
    591  1.28  augustss Static int scsipiprint __P((void *aux, const char *pnp));
    592  1.28  augustss Static void umass_atapi_probedev __P((struct atapibus_softc *, int));
    593  1.28  augustss #endif
    594  1.28  augustss 
    595  1.28  augustss #if defined(__FreeBSD__)
    596  1.28  augustss /* SCSI specific functions */
    597  1.28  augustss Static int umass_scsi_transform __P((struct umass_softc *sc,
    598  1.28  augustss 				unsigned char *cmd, int cmdlen,
    599  1.28  augustss 				unsigned char **rcmd, int *rcmdlen));
    600  1.28  augustss 
    601  1.28  augustss /* UFI specific functions */
    602  1.28  augustss Static int umass_ufi_transform	__P((struct umass_softc *sc,
    603  1.28  augustss 				unsigned char *cmd, int cmdlen,
    604  1.28  augustss 				unsigned char **rcmd, int *rcmdlen));
    605  1.28  augustss 
    606  1.28  augustss /* 8070 specific functions */
    607  1.28  augustss Static int umass_8070_transform __P((struct umass_softc *sc,
    608  1.28  augustss 				unsigned char *cmd, int cmdlen,
    609  1.28  augustss 				unsigned char **rcmd, int *rcmdlen));
    610  1.28  augustss #endif
    611  1.28  augustss 
    612  1.28  augustss #ifdef UMASS_DEBUG
    613  1.28  augustss /* General debugging functions */
    614  1.28  augustss Static void umass_bbb_dump_cbw	__P((struct umass_softc *sc,
    615  1.28  augustss 				umass_bbb_cbw_t *cbw));
    616  1.28  augustss Static void umass_bbb_dump_csw	__P((struct umass_softc *sc,
    617  1.28  augustss 				umass_bbb_csw_t *csw));
    618  1.28  augustss Static void umass_dump_buffer	__P((struct umass_softc *sc, u_int8_t *buffer,
    619  1.28  augustss 				int buflen, int printlen));
    620  1.28  augustss #endif
    621  1.28  augustss 
    622  1.28  augustss 
    623  1.28  augustss void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe);	/* XXXXX */
    624  1.28  augustss 
    625  1.28  augustss /*
    626  1.28  augustss  * USB device probe/attach/detach
    627  1.28  augustss  */
    628  1.28  augustss 
    629  1.28  augustss /*
    630  1.28  augustss  * Match the device we are seeing with the devices supported. Fill in the
    631  1.28  augustss  * proto and drive fields in the softc accordingly.
    632  1.28  augustss  * This function is called from both probe and attach.
    633  1.28  augustss  */
    634  1.28  augustss 
    635  1.28  augustss Static int
    636  1.28  augustss umass_match_proto(sc, iface, dev)
    637  1.28  augustss 	struct umass_softc *sc;
    638  1.28  augustss 	usbd_interface_handle iface;
    639  1.28  augustss 	usbd_device_handle dev;
    640  1.28  augustss {
    641  1.28  augustss 	usb_device_descriptor_t *dd;
    642  1.28  augustss 	usb_interface_descriptor_t *id;
    643  1.28  augustss 
    644  1.28  augustss 	/*
    645  1.28  augustss 	 * Fill in sc->drive and sc->proto and return a match
    646  1.28  augustss 	 * value if both are determined and 0 otherwise.
    647  1.28  augustss 	 */
    648  1.28  augustss 
    649  1.28  augustss 	sc->drive = DRIVE_GENERIC;
    650  1.28  augustss 	sc->proto = PROTO_UNKNOWN;
    651  1.28  augustss 	sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED;
    652  1.28  augustss 
    653  1.28  augustss 	sc->sc_udev = dev;
    654  1.28  augustss 	dd = usbd_get_device_descriptor(dev);
    655  1.28  augustss 
    656  1.28  augustss 	if (UGETW(dd->idVendor) == USB_VENDOR_SHUTTLE
    657  1.28  augustss 	    && UGETW(dd->idProduct) == USB_PRODUCT_SHUTTLE_EUSB) {
    658  1.28  augustss 		sc->drive = SHUTTLE_EUSB;
    659  1.28  augustss #if CBI_I
    660  1.28  augustss 		sc->proto = PROTO_8070 | PROTO_CBI_I;
    661  1.28  augustss #else
    662  1.28  augustss 		sc->proto = PROTO_8070 | PROTO_CBI;
    663  1.28  augustss #endif
    664  1.28  augustss 		sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP;
    665  1.28  augustss 		return (UMATCH_VENDOR_PRODUCT);
    666  1.28  augustss 	}
    667  1.28  augustss 
    668  1.28  augustss 	if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA
    669  1.28  augustss 	    && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) {
    670  1.28  augustss 
    671  1.28  augustss 		/* Revisions < 1.28 do not handle the interrupt endpoint
    672  1.28  augustss 		 * very well.
    673  1.28  augustss 		 */
    674  1.28  augustss 		if (UGETW(dd->bcdDevice) < 0x128)
    675  1.28  augustss 			sc->proto = PROTO_UFI | PROTO_CBI;
    676  1.28  augustss 		else
    677  1.28  augustss #if CBI_I
    678  1.28  augustss 			sc->proto = PROTO_UFI | PROTO_CBI_I;
    679  1.28  augustss #else
    680  1.28  augustss 			sc->proto = PROTO_UFI | PROTO_CBI;
    681  1.28  augustss #endif
    682  1.28  augustss 		/*
    683  1.28  augustss 		 * Revisions < 1.28 do not have the TEST UNIT READY command
    684  1.28  augustss 		 * Revisions == 1.28 have a broken TEST UNIT READY
    685  1.28  augustss 		 */
    686  1.28  augustss 		if (UGETW(dd->bcdDevice) <= 0x128)
    687  1.28  augustss 			sc->quirks |= NO_TEST_UNIT_READY;
    688  1.28  augustss 
    689  1.28  augustss 		sc->quirks |= RS_NO_CLEAR_UA;
    690  1.28  augustss 		sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
    691  1.28  augustss 		return (UMATCH_VENDOR_PRODUCT_REV);
    692  1.28  augustss 	}
    693  1.28  augustss 
    694  1.28  augustss 
    695  1.28  augustss 	id = usbd_get_interface_descriptor(iface);
    696  1.28  augustss 	if (id == NULL || id->bInterfaceClass != UICLASS_MASS)
    697  1.28  augustss 		return (UMATCH_NONE);
    698   1.1   thorpej 
    699  1.28  augustss 	switch (id->bInterfaceSubClass) {
    700  1.28  augustss 	case UISUBCLASS_SCSI:
    701  1.28  augustss 		sc->proto |= PROTO_SCSI;
    702  1.28  augustss 		break;
    703  1.28  augustss 	case UISUBCLASS_UFI:
    704  1.28  augustss 		sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED;
    705  1.28  augustss 		sc->proto |= PROTO_UFI;
    706  1.28  augustss 		break;
    707  1.28  augustss 	case UISUBCLASS_SFF8020I:
    708  1.28  augustss 	case UISUBCLASS_SFF8070I:
    709  1.28  augustss 		sc->proto |= PROTO_8070;
    710  1.28  augustss 		break;
    711  1.28  augustss 	default:
    712  1.28  augustss 		DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n",
    713  1.28  augustss 			USBDEVNAME(sc->sc_dev), id->bInterfaceSubClass));
    714  1.28  augustss 		return (UMATCH_NONE);
    715  1.28  augustss 	}
    716  1.28  augustss 
    717  1.28  augustss 	switch (id->bInterfaceProtocol) {
    718  1.28  augustss 	case UIPROTO_MASS_CBI:
    719  1.28  augustss 		sc->proto |= PROTO_CBI;
    720  1.28  augustss 		break;
    721  1.28  augustss 	case UIPROTO_MASS_CBI_I:
    722  1.28  augustss #if CBI_I
    723  1.28  augustss 		sc->proto |= PROTO_CBI_I;
    724  1.28  augustss #else
    725  1.28  augustss 		sc->proto |= PROTO_CBI;
    726  1.28  augustss #endif
    727  1.28  augustss 		break;
    728  1.28  augustss 	case UIPROTO_MASS_BBB:
    729  1.28  augustss 		sc->proto |= PROTO_BBB;
    730  1.28  augustss 		break;
    731  1.28  augustss 	case UIPROTO_MASS_BBB_P:
    732  1.28  augustss 		sc->drive = ZIP_100;
    733  1.28  augustss 		sc->proto |= PROTO_BBB;
    734  1.28  augustss 		sc->transfer_speed = UMASS_ZIP100_TRANSFER_SPEED;
    735  1.28  augustss 		sc->quirks |= NO_TEST_UNIT_READY;
    736  1.28  augustss 		break;
    737  1.28  augustss 	default:
    738  1.28  augustss 		DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n",
    739  1.28  augustss 			USBDEVNAME(sc->sc_dev), id->bInterfaceProtocol));
    740  1.28  augustss 		return (UMATCH_NONE);
    741  1.28  augustss 	}
    742   1.1   thorpej 
    743  1.28  augustss 	return (UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO);
    744  1.28  augustss }
    745   1.1   thorpej 
    746   1.1   thorpej USB_MATCH(umass)
    747   1.1   thorpej {
    748   1.1   thorpej 	USB_MATCH_START(umass, uaa);
    749  1.28  augustss #if defined(__FreeBSD__)
    750  1.28  augustss 	struct umass_softc *sc = device_get_softc(self);
    751  1.28  augustss #else if defined(__NetBSD__) || defined(__OpenBSD__)
    752  1.28  augustss 	struct umass_softc scs, *sc = &scs;
    753  1.28  augustss 	memset(sc, 0, sizeof *sc);
    754  1.28  augustss #endif
    755   1.1   thorpej 
    756  1.22  augustss 	if (uaa->iface == NULL)
    757   1.1   thorpej 		return(UMATCH_NONE);
    758   1.1   thorpej 
    759  1.28  augustss 	return (umass_match_proto(sc, uaa->iface, uaa->device));
    760   1.1   thorpej }
    761   1.1   thorpej 
    762   1.1   thorpej USB_ATTACH(umass)
    763   1.1   thorpej {
    764   1.1   thorpej 	USB_ATTACH_START(umass, sc, uaa);
    765   1.1   thorpej 	usb_interface_descriptor_t *id;
    766   1.1   thorpej 	usb_endpoint_descriptor_t *ed;
    767   1.1   thorpej 	char devinfo[1024];
    768  1.28  augustss 	int i, bno;
    769  1.28  augustss 	int err;
    770   1.1   thorpej 
    771  1.28  augustss 	/*
    772  1.28  augustss 	 * the softc struct is bzero-ed in device_set_driver. We can safely
    773  1.28  augustss 	 * call umass_detach without specifically initialising the struct.
    774  1.28  augustss 	 */
    775   1.1   thorpej 
    776   1.1   thorpej 	usbd_devinfo(uaa->device, 0, devinfo);
    777   1.1   thorpej 	USB_ATTACH_SETUP;
    778   1.1   thorpej 
    779  1.28  augustss 	sc->iface = uaa->iface;
    780  1.28  augustss 	sc->ifaceno = uaa->ifaceno;
    781  1.28  augustss 
    782  1.28  augustss 	/* initialise the proto and drive values in the umass_softc (again) */
    783  1.28  augustss 	(void) umass_match_proto(sc, sc->iface, uaa->device);
    784  1.28  augustss 
    785  1.28  augustss 	/*
    786  1.28  augustss 	 * The timeout is based on the maximum expected transfer size
    787  1.28  augustss 	 * divided by the expected transfer speed.
    788  1.28  augustss 	 * We multiply by 4 to make sure a busy system doesn't make things
    789  1.28  augustss 	 * fail.
    790  1.28  augustss 	 */
    791  1.28  augustss 	sc->timeout = 4 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed;
    792  1.28  augustss 	sc->timeout += UMASS_SPINUP_TIME;	/* allow for spinning up */
    793  1.17   thorpej 
    794  1.28  augustss 	id = usbd_get_interface_descriptor(sc->iface);
    795  1.28  augustss 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    796  1.17   thorpej 
    797  1.28  augustss 	printf("%s: using ", USBDEVNAME(sc->sc_dev));
    798  1.28  augustss 	switch (sc->proto & PROTO_COMMAND) {
    799  1.28  augustss 	case PROTO_SCSI:
    800  1.28  augustss 		printf("SCSI");
    801  1.28  augustss 		break;
    802  1.28  augustss 	case PROTO_8070:
    803  1.28  augustss 		printf("8070i");
    804  1.28  augustss 		break;
    805  1.28  augustss 	case PROTO_UFI:
    806  1.28  augustss 		printf("UFI");
    807  1.28  augustss 		break;
    808  1.17   thorpej 	default:
    809  1.28  augustss 		printf("(unknown 0x%02x)", sc->proto&PROTO_COMMAND);
    810  1.28  augustss 		break;
    811  1.17   thorpej 	}
    812  1.28  augustss 	printf(" over ");
    813  1.28  augustss 	switch (sc->proto & PROTO_WIRE) {
    814  1.28  augustss 	case PROTO_BBB:
    815  1.28  augustss 		printf("Bulk-Only");
    816  1.28  augustss 		break;
    817  1.28  augustss 	case PROTO_CBI:			/* uses Command/Bulk pipes */
    818  1.28  augustss 		printf("CBI");
    819  1.28  augustss 		break;
    820  1.28  augustss 	case PROTO_CBI_I:		/* uses Command/Bulk/Interrupt pipes */
    821  1.28  augustss 		printf("CBI with CCI");
    822  1.28  augustss 		break;
    823  1.17   thorpej 	default:
    824  1.28  augustss 		printf("(unknown 0x%02x)", sc->proto&PROTO_WIRE);
    825  1.17   thorpej 	}
    826  1.28  augustss 	printf("\n");
    827   1.1   thorpej 
    828   1.1   thorpej 	/*
    829  1.28  augustss 	 * In addition to the Control endpoint the following endpoints
    830  1.28  augustss 	 * are required:
    831  1.28  augustss 	 * a) bulk-in endpoint.
    832  1.28  augustss 	 * b) bulk-out endpoint.
    833  1.28  augustss 	 * and for Control/Bulk/Interrupt with CCI (CBI_I)
    834  1.28  augustss 	 * c) intr-in
    835   1.1   thorpej 	 *
    836   1.1   thorpej 	 * The endpoint addresses are not fixed, so we have to read them
    837   1.1   thorpej 	 * from the device descriptors of the current interface.
    838   1.1   thorpej 	 */
    839   1.1   thorpej 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    840  1.28  augustss 		ed = usbd_interface2endpoint_descriptor(sc->iface, i);
    841  1.28  augustss 		if (!ed) {
    842   1.1   thorpej 			printf("%s: could not read endpoint descriptor\n",
    843   1.1   thorpej 			       USBDEVNAME(sc->sc_dev));
    844   1.1   thorpej 			USB_ATTACH_ERROR_RETURN;
    845   1.1   thorpej 		}
    846  1.11  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
    847   1.1   thorpej 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    848  1.28  augustss 			sc->bulkin = ed->bEndpointAddress;
    849  1.11  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
    850   1.1   thorpej 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    851  1.28  augustss 			sc->bulkout = ed->bEndpointAddress;
    852  1.28  augustss 		} else if (sc->proto & PROTO_CBI_I
    853  1.28  augustss 		    && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
    854  1.28  augustss 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
    855  1.28  augustss 			sc->intrin = ed->bEndpointAddress;
    856  1.28  augustss #ifdef UMASS_DEBUG
    857  1.28  augustss 			if (UGETW(ed->wMaxPacketSize) > 2) {
    858  1.28  augustss 				DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n",
    859  1.28  augustss 					USBDEVNAME(sc->sc_dev),
    860  1.28  augustss 					UGETW(ed->wMaxPacketSize)));
    861  1.28  augustss 			}
    862  1.28  augustss #endif
    863   1.1   thorpej 		}
    864   1.1   thorpej 	}
    865   1.1   thorpej 
    866  1.28  augustss 	/* check whether we found all the endpoints we need */
    867  1.28  augustss 	if (!sc->bulkin || !sc->bulkout
    868  1.28  augustss 	    || (sc->proto & PROTO_CBI_I && !sc->intrin) ) {
    869  1.28  augustss 		DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n",
    870  1.28  augustss 			USBDEVNAME(sc->sc_dev),
    871  1.28  augustss 			sc->bulkin, sc->bulkout, sc->intrin));
    872  1.28  augustss 		umass_disco(sc);
    873  1.28  augustss 		USB_ATTACH_ERROR_RETURN;
    874  1.28  augustss 	}
    875  1.28  augustss 
    876   1.7   thorpej 	/*
    877   1.7   thorpej 	 * Get the maximum LUN supported by the device.
    878   1.7   thorpej 	 */
    879  1.28  augustss 	if ((sc->proto & PROTO_WIRE) == PROTO_BBB) {
    880  1.28  augustss 		err = umass_bbb_get_max_lun(sc, &sc->maxlun);
    881  1.28  augustss 		if (err) {
    882  1.28  augustss 			printf("%s: unable to get Max Lun: %s\n",
    883  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    884  1.28  augustss 			USB_ATTACH_ERROR_RETURN;
    885  1.28  augustss 		}
    886  1.28  augustss 	} else {
    887  1.28  augustss 		sc->maxlun = 0;
    888   1.7   thorpej 	}
    889   1.7   thorpej 
    890   1.1   thorpej 	/* Open the bulk-in and -out pipe */
    891  1.28  augustss 	err = usbd_open_pipe(sc->iface, sc->bulkout,
    892  1.28  augustss 				USBD_EXCLUSIVE_USE, &sc->bulkout_pipe);
    893   1.1   thorpej 	if (err) {
    894  1.28  augustss 		DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n",
    895  1.28  augustss 			USBDEVNAME(sc->sc_dev), sc->bulkout));
    896  1.28  augustss 		umass_disco(sc);
    897   1.1   thorpej 		USB_ATTACH_ERROR_RETURN;
    898   1.1   thorpej 	}
    899  1.28  augustss 	err = usbd_open_pipe(sc->iface, sc->bulkin,
    900  1.28  augustss 				USBD_EXCLUSIVE_USE, &sc->bulkin_pipe);
    901   1.1   thorpej 	if (err) {
    902  1.28  augustss 		DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n",
    903  1.28  augustss 			USBDEVNAME(sc->sc_dev), sc->bulkin));
    904  1.28  augustss 		umass_disco(sc);
    905   1.1   thorpej 		USB_ATTACH_ERROR_RETURN;
    906   1.1   thorpej 	}
    907  1.28  augustss 	/*
    908  1.28  augustss 	 * Open the intr-in pipe if the protocol is CBI with CCI.
    909  1.28  augustss 	 * Note: early versions of the Zip drive do have an interrupt pipe, but
    910  1.28  augustss 	 * this pipe is unused
    911  1.28  augustss 	 *
    912  1.28  augustss 	 * We do not open the interrupt pipe as an interrupt pipe, but as a
    913  1.28  augustss 	 * normal bulk endpoint. We send an IN transfer down the wire at the
    914  1.28  augustss 	 * appropriate time, because we know exactly when to expect data on
    915  1.28  augustss 	 * that endpoint. This saves bandwidth, but more important, makes the
    916  1.28  augustss 	 * code for handling the data on that endpoint simpler. No data
    917  1.28  augustss 	 * arriving concurrently.
    918  1.28  augustss 	 */
    919  1.28  augustss 	if (sc->proto & PROTO_CBI_I) {
    920  1.28  augustss 		err = usbd_open_pipe(sc->iface, sc->intrin,
    921  1.28  augustss 				USBD_EXCLUSIVE_USE, &sc->intrin_pipe);
    922  1.28  augustss 		if (err) {
    923  1.28  augustss 			DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n",
    924  1.28  augustss 				USBDEVNAME(sc->sc_dev), sc->intrin));
    925  1.28  augustss 			umass_disco(sc);
    926  1.28  augustss 			USB_ATTACH_ERROR_RETURN;
    927  1.28  augustss 		}
    928  1.28  augustss 	}
    929  1.28  augustss 
    930  1.28  augustss 	/* initialisation of generic part */
    931  1.28  augustss 	sc->transfer_state = TSTATE_IDLE;
    932  1.28  augustss 
    933  1.28  augustss 	/* request a sufficient number of xfer handles */
    934  1.28  augustss 	for (i = 0; i < XFER_NR; i++) {
    935  1.28  augustss 		sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device);
    936  1.28  augustss 		if (sc->transfer_xfer[i] == 0) {
    937  1.28  augustss 			DPRINTF(UDMASS_USB, ("%s: Out of memory\n",
    938  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
    939  1.28  augustss 			umass_disco(sc);
    940  1.28  augustss 			USB_ATTACH_ERROR_RETURN;
    941  1.28  augustss 		}
    942  1.28  augustss 	}
    943  1.28  augustss 	/* Allocate buffer for data transfer (it's huge). */
    944  1.28  augustss 	switch (sc->proto & PROTO_WIRE) {
    945  1.28  augustss 	case PROTO_BBB:
    946  1.28  augustss 		bno = XFER_BBB_DATA;
    947  1.28  augustss 		goto dalloc;
    948  1.28  augustss 	case PROTO_CBI:
    949  1.28  augustss 		bno = XFER_CBI_DATA;
    950  1.28  augustss 		goto dalloc;
    951  1.28  augustss 	case PROTO_CBI_I:
    952  1.28  augustss 		bno = XFER_CBI_DATA;
    953  1.28  augustss 	dalloc:
    954  1.28  augustss 		sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno],
    955  1.28  augustss 						    UMASS_MAX_TRANSFER_SIZE);
    956  1.28  augustss 		if (sc->data_buffer == NULL) {
    957  1.28  augustss 			umass_disco(sc);
    958  1.28  augustss 			USB_ATTACH_ERROR_RETURN;
    959  1.28  augustss 		}
    960  1.28  augustss 		break;
    961  1.28  augustss 	default:
    962  1.28  augustss 		break;
    963  1.28  augustss 	}
    964   1.1   thorpej 
    965  1.28  augustss 	/* Initialise the wire protocol specific methods */
    966  1.28  augustss 	if (sc->proto & PROTO_BBB) {
    967  1.28  augustss 		sc->reset = umass_bbb_reset;
    968  1.28  augustss 		sc->transfer = umass_bbb_transfer;
    969  1.28  augustss 		sc->state = umass_bbb_state;
    970  1.28  augustss 	} else if ((sc->proto & PROTO_CBI) || (sc->proto & PROTO_CBI_I)) {
    971  1.28  augustss 		sc->reset = umass_cbi_reset;
    972  1.28  augustss 		sc->transfer = umass_cbi_transfer;
    973  1.28  augustss 		sc->state = umass_cbi_state;
    974  1.28  augustss #ifdef UMASS_DEBUG
    975  1.28  augustss 	} else {
    976  1.28  augustss 		panic("%s:%d: Unknown proto 0x%02x\n",
    977  1.28  augustss 		      __FILE__, __LINE__, sc->proto);
    978  1.28  augustss #endif
    979  1.28  augustss 	}
    980  1.28  augustss 
    981  1.28  augustss 	if (sc->drive == SHUTTLE_EUSB)
    982  1.28  augustss 		umass_init_shuttle(sc);
    983  1.28  augustss 
    984  1.28  augustss #if defined(__FreeBSD__)
    985  1.28  augustss 	if (sc->proto & PROTO_SCSI)
    986  1.28  augustss 		sc->transform = umass_scsi_transform;
    987  1.28  augustss 	else if (sc->proto & PROTO_UFI)
    988  1.28  augustss 		sc->transform = umass_ufi_transform;
    989  1.28  augustss 	else if (sc->proto & PROTO_8070)
    990  1.28  augustss 		sc->transform = umass_8070_transform;
    991  1.28  augustss #ifdef UMASS_DEBUG
    992  1.28  augustss 	else
    993  1.28  augustss 		panic("No transformation defined for command proto 0x%02x\n",
    994  1.28  augustss 		      sc->proto & PROTO_COMMAND);
    995  1.28  augustss #endif
    996  1.28  augustss 
    997  1.28  augustss 	/* From here onwards the device can be used. */
    998  1.28  augustss 
    999  1.28  augustss 	if ((sc->proto & PROTO_SCSI) ||
   1000  1.28  augustss 	    (sc->proto & PROTO_8070) ||
   1001  1.28  augustss 	    (sc->proto & PROTO_UFI)) {
   1002  1.28  augustss 		/* Prepare the SCSI command block */
   1003  1.28  augustss 		sc->cam_scsi_sense.opcode = REQUEST_SENSE;
   1004  1.28  augustss 
   1005  1.28  augustss 		/* If this is the first device register the SIM */
   1006  1.28  augustss 		if (umass_sim == NULL) {
   1007  1.28  augustss 			err = umass_cam_attach_sim();
   1008  1.28  augustss 			if (err) {
   1009  1.28  augustss 				umass_disco(self);
   1010  1.28  augustss 				USB_ATTACH_ERROR_RETURN;
   1011  1.28  augustss 			}
   1012  1.28  augustss 		}
   1013  1.28  augustss 
   1014  1.28  augustss 		/* Attach the new device to our SCSI host controller (SIM) */
   1015  1.28  augustss 		err = umass_cam_attach(sc);
   1016  1.28  augustss 		if (err) {
   1017  1.28  augustss 			umass_disco(self);
   1018  1.28  augustss 			USB_ATTACH_ERROR_RETURN;
   1019  1.28  augustss 		}
   1020  1.28  augustss 	} else {
   1021  1.28  augustss 		panic("%s:%d: Unknown proto 0x%02x\n",
   1022  1.28  augustss 		      __FILE__, __LINE__, sc->proto);
   1023  1.28  augustss 	}
   1024  1.28  augustss #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1025  1.28  augustss 	/*
   1026  1.28  augustss 	 * Fill in the adapter.
   1027  1.28  augustss 	 */
   1028  1.28  augustss 	sc->sc_adapter.scsipi_cmd = umass_scsipi_cmd;
   1029   1.9   thorpej 	sc->sc_adapter.scsipi_minphys = umass_scsipi_minphys;
   1030  1.28  augustss 	sc->sc_adapter.scsipi_ioctl = umass_scsipi_ioctl;
   1031  1.28  augustss 
   1032  1.28  augustss 	/*
   1033  1.28  augustss 	 * fill in the prototype scsipi_link.
   1034  1.28  augustss 	 */
   1035  1.28  augustss 	switch (sc->proto & PROTO_COMMAND) {
   1036  1.28  augustss 	case PROTO_SCSI:
   1037  1.28  augustss 	/*case PROTO_UFI:*/
   1038  1.28  augustss 		sc->u.sc_link.type = BUS_SCSI;
   1039  1.28  augustss 		sc->u.sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
   1040  1.28  augustss 		sc->u.sc_link.adapter_softc = sc;
   1041  1.28  augustss 		sc->u.sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST;
   1042  1.28  augustss 		sc->u.sc_link.adapter = &sc->sc_adapter;
   1043  1.28  augustss 		sc->u.sc_link.device = &umass_dev;
   1044  1.28  augustss 		sc->u.sc_link.openings = 1;
   1045  1.28  augustss 		sc->u.sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE;
   1046  1.28  augustss 		sc->u.sc_link.scsipi_scsi.max_lun = sc->maxlun;
   1047  1.28  augustss 
   1048  1.28  augustss 		if(sc->quirks & NO_TEST_UNIT_READY)
   1049  1.28  augustss 			sc->u.sc_link.quirks |= ADEV_NOTUR;
   1050  1.28  augustss 		break;
   1051   1.1   thorpej 
   1052  1.28  augustss 	case PROTO_8070:
   1053  1.28  augustss 		sc->u.aa.sc_aa.aa_type = T_ATAPI;
   1054  1.28  augustss 		sc->u.aa.sc_aa.aa_channel = 0;
   1055  1.28  augustss 		sc->u.aa.sc_aa.aa_openings = 1;
   1056  1.28  augustss 		sc->u.aa.sc_aa.aa_drv_data = &sc->u.aa.sc_aa_drive;
   1057  1.28  augustss 		sc->u.aa.sc_aa.aa_bus_private = &sc->sc_atapi_adapter;
   1058  1.28  augustss 		sc->sc_atapi_adapter.atapi_probedev = umass_atapi_probedev;
   1059  1.28  augustss 		sc->sc_atapi_adapter.atapi_kill_pending = scsi_kill_pending;
   1060  1.28  augustss 		break;
   1061  1.28  augustss 
   1062  1.28  augustss 	default:
   1063  1.28  augustss 		printf("%s: proto=0x%x not supported yet\n",
   1064  1.28  augustss 		       USBDEVNAME(sc->sc_dev), sc->proto);
   1065  1.28  augustss 		umass_disco(sc);
   1066  1.28  augustss 		USB_ATTACH_ERROR_RETURN;
   1067  1.28  augustss 	}
   1068   1.1   thorpej 
   1069  1.28  augustss 	sc->sc_child = config_found(&sc->sc_dev, &sc->u, scsipiprint);
   1070   1.9   thorpej 	if (sc->sc_child == NULL) {
   1071  1.28  augustss 		umass_disco(sc);
   1072  1.28  augustss 		/* Not an error, just not a complete success. */
   1073  1.28  augustss 		USB_ATTACH_SUCCESS_RETURN;
   1074   1.1   thorpej 	}
   1075  1.28  augustss #endif
   1076   1.1   thorpej 
   1077  1.24  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
   1078  1.24  augustss 			   USBDEV(sc->sc_dev));
   1079  1.24  augustss 
   1080  1.28  augustss 	DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev)));
   1081  1.28  augustss 
   1082   1.1   thorpej 	USB_ATTACH_SUCCESS_RETURN;
   1083   1.1   thorpej }
   1084   1.1   thorpej 
   1085  1.28  augustss Static int
   1086  1.28  augustss scsipiprint(aux, pnp)
   1087  1.28  augustss 	void *aux;
   1088  1.28  augustss 	const char *pnp;
   1089   1.1   thorpej {
   1090  1.28  augustss 	extern int atapi_print __P((void *aux, const char *pnp));
   1091  1.28  augustss 	struct scsipi_link *l = aux;
   1092  1.28  augustss 
   1093  1.28  augustss 	if (l->type == BUS_SCSI)
   1094  1.28  augustss 		return (scsiprint(aux, pnp));
   1095  1.28  augustss 	else
   1096  1.28  augustss 		return (atapi_print(aux, pnp));
   1097  1.28  augustss }
   1098   1.9   thorpej 
   1099  1.28  augustss USB_DETACH(umass)
   1100  1.28  augustss {
   1101  1.28  augustss 	USB_DETACH_START(umass, sc);
   1102  1.28  augustss 	int rv = 0;
   1103   1.1   thorpej 
   1104  1.28  augustss 	DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev)));
   1105  1.28  augustss 
   1106  1.28  augustss 	/* Abort the pipes to wake up any waiting processes. */
   1107  1.28  augustss 	if (sc->bulkout_pipe != NULL)
   1108  1.28  augustss 		usbd_abort_pipe(sc->bulkout_pipe);
   1109  1.28  augustss 	if (sc->bulkin_pipe != NULL)
   1110  1.28  augustss 		usbd_abort_pipe(sc->bulkin_pipe);
   1111  1.28  augustss 	if (sc->intrin_pipe != NULL)
   1112  1.28  augustss 		usbd_abort_pipe(sc->intrin_pipe);
   1113  1.28  augustss 
   1114  1.28  augustss #if 0
   1115  1.28  augustss 	/* Do we really need referebce counting?  Perhaps in ioctl() */
   1116  1.28  augustss 	s = splusb();
   1117  1.28  augustss 	if (--sc->sc_refcnt >= 0) {
   1118  1.28  augustss 		/* Wait for processes to go away. */
   1119  1.28  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
   1120  1.28  augustss 	}
   1121  1.28  augustss 	splx(s);
   1122  1.28  augustss #endif
   1123  1.28  augustss 
   1124  1.28  augustss #if defined(__FreeBSD__)
   1125  1.28  augustss 	if ((sc->proto & PROTO_SCSI) ||
   1126  1.28  augustss 	    (sc->proto & PROTO_8070) ||
   1127  1.28  augustss 	    (sc->proto & PROTO_UFI))
   1128  1.28  augustss 		/* detach the device from the SCSI host controller (SIM) */
   1129  1.28  augustss 		rv = umass_cam_detach(sc);
   1130  1.28  augustss #elif defined(__NetBSD__) || defined(__OpenBSD__)
   1131  1.28  augustss 	if (sc->sc_child != NULL)
   1132  1.28  augustss 		rv = config_detach(sc->sc_child, flags);
   1133  1.28  augustss #endif
   1134  1.28  augustss 	if (rv != 0)
   1135  1.28  augustss 		return (rv);
   1136  1.28  augustss 
   1137  1.28  augustss 	umass_disco(sc);
   1138  1.28  augustss 
   1139  1.28  augustss 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
   1140  1.28  augustss 			   USBDEV(sc->sc_dev));
   1141  1.28  augustss 
   1142  1.28  augustss 	return (0);
   1143  1.28  augustss }
   1144  1.28  augustss 
   1145  1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
   1146  1.28  augustss int
   1147  1.28  augustss umass_activate(self, act)
   1148  1.28  augustss 	struct device *self;
   1149  1.28  augustss 	enum devact act;
   1150  1.28  augustss {
   1151  1.28  augustss 	struct umass_softc *sc = (struct umass_softc *) self;
   1152  1.28  augustss 	int rv = 0;
   1153  1.28  augustss 
   1154  1.28  augustss 	DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
   1155  1.28  augustss 	    USBDEVNAME(sc->sc_dev), act));
   1156  1.28  augustss 
   1157  1.28  augustss 	switch (act) {
   1158  1.28  augustss 	case DVACT_ACTIVATE:
   1159  1.28  augustss 		rv = EOPNOTSUPP;
   1160  1.28  augustss 		break;
   1161   1.1   thorpej 
   1162   1.1   thorpej 	case DVACT_DEACTIVATE:
   1163  1.28  augustss 		if (sc->sc_child != NULL)
   1164   1.9   thorpej 			break;
   1165   1.9   thorpej 		rv = config_deactivate(sc->sc_child);
   1166   1.9   thorpej 		DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
   1167   1.9   thorpej 		    "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
   1168   1.9   thorpej 		if (rv == 0)
   1169   1.9   thorpej 			sc->sc_dying = 1;
   1170   1.1   thorpej 		break;
   1171   1.1   thorpej 	}
   1172   1.9   thorpej 	return (rv);
   1173   1.1   thorpej }
   1174  1.28  augustss #endif
   1175  1.28  augustss 
   1176  1.28  augustss Static void
   1177  1.28  augustss umass_disco(sc)
   1178  1.28  augustss 	struct umass_softc *sc;
   1179  1.28  augustss {
   1180  1.28  augustss 	int i;
   1181  1.28  augustss 
   1182  1.28  augustss 	DPRINTF(UDMASS_GEN, ("umass_disco\n"));
   1183  1.28  augustss 
   1184  1.28  augustss 	/* Free the xfers. */
   1185  1.28  augustss 	for (i = 0; i < XFER_NR; i++)
   1186  1.28  augustss 		if (sc->transfer_xfer[i] != NULL) {
   1187  1.28  augustss 			usbd_free_xfer(sc->transfer_xfer[i]);
   1188  1.28  augustss 			sc->transfer_xfer[i] = NULL;
   1189  1.28  augustss 		}
   1190  1.28  augustss 
   1191  1.28  augustss 	/* Remove all the pipes. */
   1192  1.28  augustss 	if (sc->bulkout_pipe != NULL)
   1193  1.28  augustss 		usbd_close_pipe(sc->bulkout_pipe);
   1194  1.28  augustss 	if (sc->bulkin_pipe != NULL)
   1195  1.28  augustss 		usbd_close_pipe(sc->bulkin_pipe);
   1196  1.28  augustss 	if (sc->intrin_pipe != NULL)
   1197  1.28  augustss 		usbd_close_pipe(sc->intrin_pipe);
   1198  1.28  augustss }
   1199   1.1   thorpej 
   1200  1.28  augustss Static void
   1201  1.28  augustss umass_init_shuttle(struct umass_softc *sc)
   1202   1.1   thorpej {
   1203  1.28  augustss 	usb_device_request_t req;
   1204  1.28  augustss 	u_char status[2];
   1205  1.28  augustss 
   1206  1.28  augustss 	/* The Linux driver does this */
   1207  1.28  augustss 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
   1208  1.28  augustss 	req.bRequest = 1;
   1209  1.28  augustss 	USETW(req.wValue, 0);
   1210  1.28  augustss 	USETW(req.wIndex, sc->ifaceno);
   1211  1.28  augustss 	USETW(req.wLength, sizeof status);
   1212  1.28  augustss 	(void)usbd_do_request(sc->sc_udev, &req, &status);
   1213  1.28  augustss }
   1214  1.28  augustss 
   1215  1.28  augustss /*
   1216  1.28  augustss  * Generic functions to handle transfers
   1217  1.28  augustss  */
   1218   1.9   thorpej 
   1219  1.28  augustss Static usbd_status
   1220  1.28  augustss umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe,
   1221  1.28  augustss 			void *buffer, int buflen, int flags,
   1222  1.28  augustss 			usbd_xfer_handle xfer)
   1223  1.28  augustss {
   1224  1.28  augustss 	usbd_status err;
   1225   1.9   thorpej 
   1226  1.28  augustss 	if (sc->sc_dying)
   1227  1.28  augustss 		return (USBD_IOERROR);
   1228  1.18  augustss 
   1229  1.28  augustss 	/* Initialiase a USB transfer and then schedule it */
   1230  1.18  augustss 
   1231  1.28  augustss 	usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen,
   1232  1.28  augustss 	    flags | sc->sc_xfer_flags, sc->timeout, sc->state);
   1233  1.18  augustss 
   1234  1.28  augustss 	err = usbd_transfer(xfer);
   1235  1.28  augustss 	DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x "
   1236  1.28  augustss 	    "timeout=%d\n", USBDEVNAME(sc->sc_dev),
   1237  1.28  augustss 	    buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout));
   1238  1.28  augustss 	if (err && err != USBD_IN_PROGRESS) {
   1239  1.28  augustss 		DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n",
   1240  1.28  augustss 			USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
   1241  1.28  augustss 		return (err);
   1242   1.9   thorpej 	}
   1243  1.24  augustss 
   1244  1.28  augustss 	return (USBD_NORMAL_COMPLETION);
   1245   1.1   thorpej }
   1246   1.1   thorpej 
   1247   1.1   thorpej 
   1248  1.28  augustss Static usbd_status
   1249  1.28  augustss umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle dev,
   1250  1.28  augustss 	 usb_device_request_t *req,
   1251  1.28  augustss 	 void *buffer, int buflen, int flags,
   1252  1.28  augustss 	 usbd_xfer_handle xfer)
   1253   1.1   thorpej {
   1254   1.1   thorpej 	usbd_status err;
   1255   1.1   thorpej 
   1256  1.28  augustss 	if (sc->sc_dying)
   1257  1.28  augustss 		return (USBD_IOERROR);
   1258   1.1   thorpej 
   1259  1.28  augustss 	/* Initialiase a USB control transfer and then schedule it */
   1260   1.1   thorpej 
   1261  1.28  augustss 	usbd_setup_default_xfer(xfer, dev, (void *) sc,
   1262  1.28  augustss 	    sc->timeout, req, buffer, buflen, flags, sc->state);
   1263   1.1   thorpej 
   1264  1.28  augustss 	err = usbd_transfer(xfer);
   1265  1.28  augustss 	if (err && err != USBD_IN_PROGRESS) {
   1266  1.28  augustss 		DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n",
   1267  1.28  augustss 			 USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
   1268   1.1   thorpej 
   1269  1.28  augustss 		/* do not reset, as this would make us loop */
   1270  1.28  augustss 		return (err);
   1271  1.28  augustss 	}
   1272   1.1   thorpej 
   1273  1.28  augustss 	return (USBD_NORMAL_COMPLETION);
   1274   1.1   thorpej }
   1275   1.1   thorpej 
   1276  1.28  augustss Static void
   1277  1.28  augustss umass_clear_endpoint_stall(struct umass_softc *sc,
   1278  1.28  augustss 	u_int8_t endpt, usbd_pipe_handle pipe,
   1279  1.28  augustss 	int state, usbd_xfer_handle xfer)
   1280   1.7   thorpej {
   1281   1.7   thorpej 	usbd_device_handle dev;
   1282  1.15   thorpej 
   1283  1.28  augustss 	if (sc->sc_dying)
   1284  1.28  augustss 		return;
   1285   1.1   thorpej 
   1286  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n",
   1287  1.28  augustss 		USBDEVNAME(sc->sc_dev), endpt));
   1288   1.7   thorpej 
   1289  1.28  augustss 	usbd_interface2device_handle(sc->iface, &dev);
   1290   1.7   thorpej 
   1291  1.28  augustss 	sc->transfer_state = state;
   1292   1.7   thorpej 
   1293  1.28  augustss 	usbd_clear_endpoint_toggle(pipe);
   1294   1.7   thorpej 
   1295  1.28  augustss 	sc->request.bmRequestType = UT_WRITE_ENDPOINT;
   1296  1.28  augustss 	sc->request.bRequest = UR_CLEAR_FEATURE;
   1297  1.28  augustss 	USETW(sc->request.wValue, UF_ENDPOINT_HALT);
   1298  1.28  augustss 	USETW(sc->request.wIndex, endpt);
   1299  1.28  augustss 	USETW(sc->request.wLength, 0);
   1300  1.28  augustss 	umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, xfer);
   1301  1.28  augustss }
   1302  1.15   thorpej 
   1303  1.28  augustss Static void
   1304  1.28  augustss umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv)
   1305  1.28  augustss {
   1306  1.28  augustss 	sc->transfer_cb = cb;
   1307  1.28  augustss 	sc->transfer_priv = priv;
   1308   1.1   thorpej 
   1309  1.28  augustss 	/* The reset is a forced reset, so no error (yet) */
   1310  1.28  augustss 	sc->reset(sc, STATUS_CMD_OK);
   1311   1.7   thorpej }
   1312   1.1   thorpej 
   1313  1.28  augustss /*
   1314  1.28  augustss  * Bulk protocol specific functions
   1315  1.28  augustss  */
   1316  1.28  augustss 
   1317  1.28  augustss Static void
   1318  1.28  augustss umass_bbb_reset(struct umass_softc *sc, int status)
   1319   1.1   thorpej {
   1320   1.1   thorpej 	usbd_device_handle dev;
   1321  1.28  augustss 
   1322  1.28  augustss 	KASSERT(sc->proto & PROTO_BBB,
   1323  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_bbb_reset\n", sc->proto));
   1324  1.28  augustss 
   1325  1.28  augustss 	if (sc->sc_dying)
   1326  1.28  augustss 		return;
   1327   1.1   thorpej 
   1328   1.1   thorpej 	/*
   1329   1.1   thorpej 	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
   1330   1.1   thorpej 	 *
   1331   1.1   thorpej 	 * For Reset Recovery the host shall issue in the following order:
   1332   1.1   thorpej 	 * a) a Bulk-Only Mass Storage Reset
   1333   1.1   thorpej 	 * b) a Clear Feature HALT to the Bulk-In endpoint
   1334   1.1   thorpej 	 * c) a Clear Feature HALT to the Bulk-Out endpoint
   1335  1.28  augustss 	 *
   1336  1.28  augustss 	 * This is done in 3 steps, states:
   1337  1.28  augustss 	 * TSTATE_BBB_RESET1
   1338  1.28  augustss 	 * TSTATE_BBB_RESET2
   1339  1.28  augustss 	 * TSTATE_BBB_RESET3
   1340  1.28  augustss 	 *
   1341  1.28  augustss 	 * If the reset doesn't succeed, the device should be port reset.
   1342   1.1   thorpej 	 */
   1343   1.1   thorpej 
   1344  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n",
   1345   1.1   thorpej 		USBDEVNAME(sc->sc_dev)));
   1346  1.28  augustss 
   1347  1.28  augustss 	sc->transfer_state = TSTATE_BBB_RESET1;
   1348  1.28  augustss 	sc->transfer_status = status;
   1349   1.1   thorpej 
   1350  1.28  augustss 	usbd_interface2device_handle(sc->iface, &dev);
   1351   1.1   thorpej 
   1352  1.28  augustss 	/* reset is a class specific interface write */
   1353  1.28  augustss 	sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1354  1.28  augustss 	sc->request.bRequest = UR_BBB_RESET;
   1355  1.28  augustss 	USETW(sc->request.wValue, 0);
   1356  1.28  augustss 	USETW(sc->request.wIndex, sc->ifaceno);
   1357  1.28  augustss 	USETW(sc->request.wLength, 0);
   1358  1.28  augustss 	umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0,
   1359  1.28  augustss 				  sc->transfer_xfer[XFER_BBB_RESET1]);
   1360  1.28  augustss }
   1361  1.28  augustss 
   1362  1.28  augustss Static void
   1363  1.28  augustss umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen,
   1364  1.28  augustss 		    void *data, int datalen, int dir,
   1365  1.28  augustss 		    transfer_cb_f cb, void *priv)
   1366  1.28  augustss {
   1367  1.28  augustss 	static int dCBWtag = 42;	/* unique for CBW of transfer */
   1368   1.1   thorpej 
   1369  1.28  augustss 	DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n",
   1370  1.28  augustss 		USBDEVNAME(sc->sc_dev), *(u_char*)cmd));
   1371   1.1   thorpej 
   1372  1.28  augustss 	KASSERT(sc->proto & PROTO_BBB,
   1373  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_bbb_transfer\n",
   1374  1.28  augustss 		sc->proto));
   1375   1.1   thorpej 
   1376   1.1   thorpej 	/*
   1377  1.28  augustss 	 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
   1378  1.28  augustss 	 * a data phase of datalen bytes from/to the device and finally a
   1379  1.28  augustss 	 * csw read phase.
   1380  1.28  augustss 	 * If the data direction was inbound a maximum of datalen bytes
   1381  1.28  augustss 	 * is stored in the buffer pointed to by data.
   1382  1.28  augustss 	 *
   1383  1.28  augustss 	 * umass_bbb_transfer initialises the transfer and lets the state
   1384  1.28  augustss 	 * machine in umass_bbb_state handle the completion. It uses the
   1385  1.28  augustss 	 * following states:
   1386  1.28  augustss 	 * TSTATE_BBB_COMMAND
   1387  1.28  augustss 	 *   -> TSTATE_BBB_DATA
   1388  1.28  augustss 	 *   -> TSTATE_BBB_STATUS
   1389  1.28  augustss 	 *   -> TSTATE_BBB_STATUS2
   1390  1.28  augustss 	 *   -> TSTATE_BBB_IDLE
   1391  1.28  augustss 	 *
   1392  1.28  augustss 	 * An error in any of those states will invoke
   1393  1.28  augustss 	 * umass_bbb_reset.
   1394   1.1   thorpej 	 */
   1395   1.1   thorpej 
   1396   1.1   thorpej 	/* check the given arguments */
   1397  1.28  augustss 	KASSERT(datalen == 0 || data != NULL,
   1398  1.28  augustss 		("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
   1399  1.28  augustss 	KASSERT(cmdlen <= CBWCDBLENGTH,
   1400  1.28  augustss 		("%s: cmdlen exceeds CDB length in CBW (%d > %d)",
   1401  1.28  augustss 			USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH));
   1402  1.28  augustss 	KASSERT(dir == DIR_NONE || datalen > 0,
   1403  1.28  augustss 		("%s: datalen == 0 while direction is not NONE\n",
   1404   1.1   thorpej 			USBDEVNAME(sc->sc_dev)));
   1405  1.28  augustss 	KASSERT(datalen == 0 || dir != DIR_NONE,
   1406  1.28  augustss 		("%s: direction is NONE while datalen is not zero\n",
   1407  1.28  augustss 			USBDEVNAME(sc->sc_dev)));
   1408  1.28  augustss 	KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE,
   1409  1.28  augustss 		("%s: CBW struct does not have the right size (%d vs. %d)\n",
   1410  1.28  augustss 			USBDEVNAME(sc->sc_dev),
   1411  1.28  augustss 			sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE));
   1412  1.28  augustss 	KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE,
   1413  1.28  augustss 		("%s: CSW struct does not have the right size (%d vs. %d)\n",
   1414  1.28  augustss 			USBDEVNAME(sc->sc_dev),
   1415  1.28  augustss 			sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE));
   1416   1.1   thorpej 
   1417   1.1   thorpej 	/*
   1418  1.28  augustss 	 * Determine the direction of the data transfer and the length.
   1419   1.1   thorpej 	 *
   1420   1.1   thorpej 	 * dCBWDataTransferLength (datalen) :
   1421   1.1   thorpej 	 *   This field indicates the number of bytes of data that the host
   1422   1.1   thorpej 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
   1423   1.1   thorpej 	 *   the Direction bit) during the execution of this command. If this
   1424   1.1   thorpej 	 *   field is set to 0, the device will expect that no data will be
   1425   1.1   thorpej 	 *   transferred IN or OUT during this command, regardless of the value
   1426   1.1   thorpej 	 *   of the Direction bit defined in dCBWFlags.
   1427   1.1   thorpej 	 *
   1428   1.1   thorpej 	 * dCBWFlags (dir) :
   1429   1.1   thorpej 	 *   The bits of the Flags field are defined as follows:
   1430  1.28  augustss 	 *     Bits 0-6	 reserved
   1431  1.28  augustss 	 *     Bit  7	 Direction - this bit shall be ignored if the
   1432  1.28  augustss 	 *			     dCBWDataTransferLength field is zero.
   1433  1.28  augustss 	 *		 0 = data Out from host to device
   1434  1.28  augustss 	 *		 1 = data In from device to host
   1435   1.1   thorpej 	 */
   1436   1.1   thorpej 
   1437  1.28  augustss 	/* Fill in the Command Block Wrapper */
   1438  1.28  augustss 	USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
   1439  1.28  augustss 	USETDW(sc->cbw.dCBWTag, dCBWtag);
   1440  1.28  augustss 	dCBWtag++;	/* cannot be done in macro (it will be done 4 times) */
   1441  1.28  augustss 	USETDW(sc->cbw.dCBWDataTransferLength, datalen);
   1442  1.28  augustss 	/* DIR_NONE is treated as DIR_OUT (0x00) */
   1443  1.28  augustss 	sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
   1444  1.28  augustss 	sc->cbw.bCBWLUN = lun;
   1445  1.28  augustss 	sc->cbw.bCDBLength = cmdlen;
   1446  1.28  augustss 	bcopy(cmd, sc->cbw.CBWCDB, cmdlen);
   1447  1.28  augustss 
   1448  1.28  augustss 	DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
   1449  1.28  augustss 
   1450  1.28  augustss 	/* store the details for the data transfer phase */
   1451  1.28  augustss 	sc->transfer_dir = dir;
   1452  1.28  augustss 	sc->transfer_data = data;
   1453  1.28  augustss 	sc->transfer_datalen = datalen;
   1454  1.28  augustss 	sc->transfer_actlen = 0;
   1455  1.28  augustss 	sc->transfer_cb = cb;
   1456  1.28  augustss 	sc->transfer_priv = priv;
   1457  1.28  augustss 	sc->transfer_status = STATUS_CMD_OK;
   1458   1.1   thorpej 
   1459  1.28  augustss 	/* move from idle to the command state */
   1460  1.28  augustss 	sc->transfer_state = TSTATE_BBB_COMMAND;
   1461   1.1   thorpej 
   1462   1.1   thorpej 	/* Send the CBW from host to device via bulk-out endpoint. */
   1463  1.28  augustss 	if (umass_setup_transfer(sc, sc->bulkout_pipe,
   1464  1.28  augustss 			&sc->cbw, UMASS_BBB_CBW_SIZE, 0,
   1465  1.28  augustss 			sc->transfer_xfer[XFER_BBB_CBW])) {
   1466  1.28  augustss 		umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1467   1.1   thorpej 	}
   1468  1.28  augustss }
   1469  1.28  augustss 
   1470   1.1   thorpej 
   1471  1.28  augustss Static void
   1472  1.28  augustss umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv,
   1473  1.28  augustss 		usbd_status err)
   1474  1.28  augustss {
   1475  1.28  augustss 	struct umass_softc *sc = (struct umass_softc *) priv;
   1476  1.28  augustss 	usbd_xfer_handle next_xfer;
   1477  1.28  augustss 
   1478  1.28  augustss 	KASSERT(sc->proto & PROTO_BBB,
   1479  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto));
   1480  1.28  augustss 
   1481  1.28  augustss 	if (sc->sc_dying)
   1482  1.28  augustss 		return;
   1483   1.1   thorpej 
   1484   1.1   thorpej 	/*
   1485  1.28  augustss 	 * State handling for BBB transfers.
   1486  1.28  augustss 	 *
   1487  1.28  augustss 	 * The subroutine is rather long. It steps through the states given in
   1488  1.28  augustss 	 * Annex A of the Bulk-Only specification.
   1489  1.28  augustss 	 * Each state first does the error handling of the previous transfer
   1490  1.28  augustss 	 * and then prepares the next transfer.
   1491  1.28  augustss 	 * Each transfer is done asynchroneously so after the request/transfer
   1492  1.28  augustss 	 * has been submitted you will find a 'return;'.
   1493   1.1   thorpej 	 */
   1494   1.1   thorpej 
   1495  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n",
   1496  1.28  augustss 		USBDEVNAME(sc->sc_dev), sc->transfer_state,
   1497  1.28  augustss 		states[sc->transfer_state], xfer, usbd_errstr(err)));
   1498  1.28  augustss 
   1499  1.28  augustss 	switch (sc->transfer_state) {
   1500  1.28  augustss 
   1501  1.28  augustss 	/***** Bulk Transfer *****/
   1502  1.28  augustss 	case TSTATE_BBB_COMMAND:
   1503  1.28  augustss 		/* Command transport phase, error handling */
   1504  1.28  augustss 		if (err) {
   1505  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n",
   1506  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1507  1.28  augustss 			/* If the device detects that the CBW is invalid, then
   1508  1.28  augustss 			 * the device may STALL both bulk endpoints and require
   1509  1.28  augustss 			 * a Bulk-Reset
   1510  1.28  augustss 			 */
   1511  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1512  1.28  augustss 			return;
   1513  1.28  augustss 		}
   1514  1.28  augustss 
   1515  1.28  augustss 		/* Data transport phase, setup transfer */
   1516  1.28  augustss 		sc->transfer_state = TSTATE_BBB_DATA;
   1517  1.28  augustss 		if (sc->transfer_dir == DIR_IN) {
   1518  1.28  augustss 			if (umass_setup_transfer(sc, sc->bulkin_pipe,
   1519  1.28  augustss 					sc->data_buffer, sc->transfer_datalen,
   1520  1.28  augustss 					USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1521  1.28  augustss 					sc->transfer_xfer[XFER_BBB_DATA]))
   1522  1.28  augustss 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1523  1.28  augustss 
   1524  1.28  augustss 			return;
   1525  1.28  augustss 		} else if (sc->transfer_dir == DIR_OUT) {
   1526  1.28  augustss 			memcpy(sc->data_buffer, sc->transfer_data,
   1527  1.28  augustss 			       sc->transfer_datalen);
   1528  1.28  augustss 			if (umass_setup_transfer(sc, sc->bulkout_pipe,
   1529  1.28  augustss 					sc->data_buffer, sc->transfer_datalen,
   1530  1.28  augustss 					USBD_NO_COPY,/* fixed length transfer */
   1531  1.28  augustss 					sc->transfer_xfer[XFER_BBB_DATA]))
   1532  1.28  augustss 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1533   1.1   thorpej 
   1534  1.28  augustss 			return;
   1535  1.28  augustss 		} else {
   1536  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: no data phase\n",
   1537  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1538  1.28  augustss 		}
   1539   1.1   thorpej 
   1540  1.28  augustss 		/* FALLTHROUGH if no data phase, err == 0 */
   1541  1.28  augustss 	case TSTATE_BBB_DATA:
   1542  1.28  augustss 		/* Command transport phase, error handling (ignored if no data
   1543  1.28  augustss 		 * phase (fallthrough from previous state)) */
   1544  1.28  augustss 		if (sc->transfer_dir != DIR_NONE) {
   1545  1.28  augustss 			/* retrieve the length of the transfer that was done */
   1546  1.28  augustss 			usbd_get_xfer_status(xfer, NULL, NULL,
   1547  1.28  augustss 					     &sc->transfer_actlen, NULL);
   1548  1.28  augustss 
   1549  1.28  augustss 			if (err) {
   1550  1.28  augustss 				DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, "
   1551  1.28  augustss 					"%s\n", USBDEVNAME(sc->sc_dev),
   1552  1.28  augustss 					(sc->transfer_dir == DIR_IN?"in":"out"),
   1553  1.28  augustss 					sc->transfer_datalen,usbd_errstr(err)));
   1554  1.28  augustss 
   1555  1.28  augustss 				if (err == USBD_STALLED) {
   1556  1.28  augustss 					umass_clear_endpoint_stall(sc,
   1557  1.28  augustss 					  (sc->transfer_dir == DIR_IN?
   1558  1.28  augustss 					    sc->bulkin:sc->bulkout),
   1559  1.28  augustss 					  (sc->transfer_dir == DIR_IN?
   1560  1.28  augustss 					    sc->bulkin_pipe:sc->bulkout_pipe),
   1561  1.28  augustss 					  TSTATE_BBB_DCLEAR,
   1562  1.28  augustss 					  sc->transfer_xfer[XFER_BBB_DCLEAR]);
   1563  1.28  augustss 					return;
   1564  1.28  augustss 				} else {
   1565  1.28  augustss 					/* Unless the error is a pipe stall the
   1566  1.28  augustss 					 * error is fatal.
   1567  1.28  augustss 					 */
   1568  1.28  augustss 					umass_bbb_reset(sc,STATUS_WIRE_FAILED);
   1569  1.28  augustss 					return;
   1570  1.28  augustss 				}
   1571  1.28  augustss 			}
   1572  1.28  augustss 		}
   1573   1.1   thorpej 
   1574  1.28  augustss 		if (sc->transfer_dir == DIR_IN)
   1575  1.28  augustss 			memcpy(sc->transfer_data, sc->data_buffer,
   1576  1.28  augustss 			       sc->transfer_actlen);
   1577  1.28  augustss 
   1578  1.28  augustss 		DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN)
   1579  1.28  augustss 					umass_dump_buffer(sc, sc->transfer_data,
   1580  1.28  augustss 						sc->transfer_datalen, 48));
   1581  1.28  augustss 
   1582  1.28  augustss 		/* FALLTHROUGH, err == 0 (no data phase or successfull) */
   1583  1.28  augustss 	case TSTATE_BBB_DCLEAR: /* stall clear after data phase */
   1584  1.28  augustss 	case TSTATE_BBB_SCLEAR: /* stall clear after status phase */
   1585  1.28  augustss 		/* Reading of CSW after bulk stall condition in data phase
   1586  1.28  augustss 		 * (TSTATE_BBB_DATA2) or bulk-in stall condition after
   1587  1.28  augustss 		 * reading CSW (TSTATE_BBB_SCLEAR).
   1588  1.28  augustss 		 * In the case of no data phase or successfull data phase,
   1589  1.28  augustss 		 * err == 0 and the following if block is passed.
   1590  1.28  augustss 		 */
   1591  1.28  augustss 		if (err) {	/* should not occur */
   1592  1.28  augustss 			/* try the transfer below, even if clear stall failed */
   1593  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed"
   1594  1.28  augustss 				", %s\n", USBDEVNAME(sc->sc_dev),
   1595  1.28  augustss 				(sc->transfer_dir == DIR_IN? "in":"out"),
   1596  1.28  augustss 				usbd_errstr(err)));
   1597  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1598  1.28  augustss 			return;
   1599  1.28  augustss 		}
   1600  1.28  augustss 
   1601  1.28  augustss 		/* Status transport phase, setup transfer */
   1602  1.28  augustss 		if (sc->transfer_state == TSTATE_BBB_COMMAND ||
   1603  1.28  augustss 		    sc->transfer_state == TSTATE_BBB_DATA ||
   1604  1.28  augustss 		    sc->transfer_state == TSTATE_BBB_DCLEAR) {
   1605  1.28  augustss 			/* After no data phase, successfull data phase and
   1606  1.28  augustss 			 * after clearing bulk-in/-out stall condition
   1607  1.28  augustss 			 */
   1608  1.28  augustss 			sc->transfer_state = TSTATE_BBB_STATUS1;
   1609  1.28  augustss 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW1];
   1610  1.28  augustss 		} else {
   1611  1.28  augustss 			/* After first attempt of fetching CSW */
   1612  1.28  augustss 			sc->transfer_state = TSTATE_BBB_STATUS2;
   1613  1.28  augustss 			next_xfer = sc->transfer_xfer[XFER_BBB_CSW2];
   1614   1.1   thorpej 		}
   1615   1.1   thorpej 
   1616  1.28  augustss 		/* Read the Command Status Wrapper via bulk-in endpoint. */
   1617  1.28  augustss 		if (umass_setup_transfer(sc, sc->bulkin_pipe,
   1618  1.28  augustss 				&sc->csw, UMASS_BBB_CSW_SIZE, 0,
   1619  1.28  augustss 				next_xfer)) {
   1620  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1621  1.28  augustss 			return;
   1622  1.28  augustss 		}
   1623   1.1   thorpej 
   1624  1.28  augustss 		return;
   1625  1.28  augustss 	case TSTATE_BBB_STATUS1:	/* first attempt */
   1626  1.28  augustss 	case TSTATE_BBB_STATUS2:	/* second attempt */
   1627  1.28  augustss 		/* Status transfer, error handling */
   1628   1.1   thorpej 		if (err) {
   1629  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n",
   1630  1.28  augustss 				USBDEVNAME(sc->sc_dev), usbd_errstr(err),
   1631  1.28  augustss 				(sc->transfer_state == TSTATE_BBB_STATUS1?
   1632  1.28  augustss 					", retrying":"")));
   1633  1.28  augustss 
   1634  1.28  augustss 			/* If this was the first attempt at fetching the CSW
   1635  1.28  augustss 			 * retry it, otherwise fail.
   1636  1.28  augustss 			 */
   1637  1.28  augustss 			if (sc->transfer_state == TSTATE_BBB_STATUS1) {
   1638  1.28  augustss 				umass_clear_endpoint_stall(sc,
   1639  1.28  augustss 						sc->bulkin, sc->bulkin_pipe,
   1640  1.28  augustss 						TSTATE_BBB_SCLEAR,
   1641  1.28  augustss 						sc->transfer_xfer[XFER_BBB_SCLEAR]);
   1642  1.28  augustss 				return;
   1643  1.28  augustss 			} else {
   1644  1.28  augustss 				umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1645  1.28  augustss 				return;
   1646  1.28  augustss 			}
   1647  1.28  augustss 		}
   1648  1.28  augustss 
   1649  1.28  augustss 		DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
   1650  1.28  augustss 
   1651  1.28  augustss 		/* Check CSW and handle any error */
   1652  1.28  augustss 		if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
   1653  1.28  augustss 			/* Invalid CSW: Wrong signature or wrong tag might
   1654  1.28  augustss 			 * indicate that the device is confused -> reset it.
   1655  1.28  augustss 			 */
   1656  1.28  augustss 			printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n",
   1657  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   1658  1.28  augustss 				UGETDW(sc->csw.dCSWSignature),
   1659  1.28  augustss 				CSWSIGNATURE);
   1660  1.28  augustss 
   1661  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1662  1.28  augustss 			return;
   1663  1.28  augustss 		} else if (UGETDW(sc->csw.dCSWTag)
   1664  1.28  augustss 				!= UGETDW(sc->cbw.dCBWTag)) {
   1665  1.28  augustss 			printf("%s: Invalid CSW: tag %d should be %d\n",
   1666  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   1667  1.28  augustss 				UGETDW(sc->csw.dCSWTag),
   1668  1.28  augustss 				UGETDW(sc->cbw.dCBWTag));
   1669  1.28  augustss 
   1670  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1671  1.28  augustss 			return;
   1672  1.28  augustss 
   1673  1.28  augustss 		/* CSW is valid here */
   1674  1.28  augustss 		} else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
   1675  1.28  augustss 			printf("%s: Invalid CSW: status %d > %d\n",
   1676  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   1677  1.28  augustss 				sc->csw.bCSWStatus,
   1678  1.28  augustss 				CSWSTATUS_PHASE);
   1679  1.28  augustss 
   1680  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1681  1.28  augustss 			return;
   1682  1.28  augustss 		} else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
   1683  1.28  augustss 			printf("%s: Phase Error, residue = %d\n",
   1684  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   1685  1.28  augustss 				UGETDW(sc->csw.dCSWDataResidue));
   1686  1.28  augustss 
   1687  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1688  1.28  augustss 			return;
   1689  1.28  augustss 
   1690  1.28  augustss 		} else if (sc->transfer_actlen > sc->transfer_datalen) {
   1691  1.28  augustss 			/* Buffer overrun! Don't let this go by unnoticed */
   1692  1.28  augustss 			panic("%s: transferred %d bytes instead of %d bytes\n",
   1693  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   1694  1.28  augustss 				sc->transfer_actlen, sc->transfer_datalen);
   1695  1.28  augustss 		} else if (sc->transfer_datalen - sc->transfer_actlen
   1696  1.28  augustss 			   != UGETDW(sc->csw.dCSWDataResidue)) {
   1697  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n",
   1698   1.1   thorpej 				USBDEVNAME(sc->sc_dev),
   1699  1.28  augustss 				sc->transfer_datalen - sc->transfer_actlen,
   1700  1.28  augustss 				UGETDW(sc->csw.dCSWDataResidue)));
   1701  1.28  augustss 
   1702  1.28  augustss 			umass_bbb_reset(sc, STATUS_WIRE_FAILED);
   1703  1.28  augustss 			return;
   1704  1.28  augustss 
   1705  1.28  augustss 		} else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
   1706  1.28  augustss 			DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n",
   1707   1.1   thorpej 				USBDEVNAME(sc->sc_dev),
   1708  1.28  augustss 				UGETDW(sc->csw.dCSWDataResidue)));
   1709  1.28  augustss 
   1710  1.28  augustss 			/* SCSI command failed but transfer was succesful */
   1711  1.28  augustss 			sc->transfer_state = TSTATE_IDLE;
   1712  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   1713  1.28  augustss 					UGETDW(sc->csw.dCSWDataResidue),
   1714  1.28  augustss 					STATUS_CMD_FAILED);
   1715  1.28  augustss 
   1716  1.28  augustss 			return;
   1717  1.28  augustss 
   1718  1.28  augustss 		} else {	/* success */
   1719  1.28  augustss 			sc->transfer_state = TSTATE_IDLE;
   1720  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   1721  1.28  augustss 					UGETDW(sc->csw.dCSWDataResidue),
   1722  1.28  augustss 					STATUS_CMD_OK);
   1723  1.28  augustss 
   1724  1.28  augustss 			return;
   1725   1.1   thorpej 		}
   1726   1.1   thorpej 
   1727  1.28  augustss 	/***** Bulk Reset *****/
   1728  1.28  augustss 	case TSTATE_BBB_RESET1:
   1729  1.28  augustss 		if (err)
   1730  1.28  augustss 			printf("%s: BBB reset failed, %s\n",
   1731  1.28  augustss 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1732  1.28  augustss 
   1733  1.28  augustss 		umass_clear_endpoint_stall(sc,
   1734  1.28  augustss 			sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2,
   1735  1.28  augustss 			sc->transfer_xfer[XFER_BBB_RESET2]);
   1736  1.28  augustss 
   1737  1.28  augustss 		return;
   1738  1.28  augustss 	case TSTATE_BBB_RESET2:
   1739  1.28  augustss 		if (err)	/* should not occur */
   1740  1.28  augustss 			printf("%s: BBB bulk-in clear stall failed, %s\n",
   1741  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1742  1.28  augustss 			/* no error recovery, otherwise we end up in a loop */
   1743  1.28  augustss 
   1744  1.28  augustss 		umass_clear_endpoint_stall(sc,
   1745  1.28  augustss 			sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3,
   1746  1.28  augustss 			sc->transfer_xfer[XFER_BBB_RESET3]);
   1747  1.28  augustss 
   1748  1.28  augustss 		return;
   1749  1.28  augustss 	case TSTATE_BBB_RESET3:
   1750  1.28  augustss 		if (err)	/* should not occur */
   1751  1.28  augustss 			printf("%s: BBB bulk-out clear stall failed, %s\n",
   1752  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   1753  1.28  augustss 			/* no error recovery, otherwise we end up in a loop */
   1754  1.28  augustss 
   1755  1.28  augustss 		sc->transfer_state = TSTATE_IDLE;
   1756  1.28  augustss 		if (sc->transfer_priv) {
   1757  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   1758  1.28  augustss 					sc->transfer_datalen,
   1759  1.28  augustss 					sc->transfer_status);
   1760  1.28  augustss 		}
   1761   1.1   thorpej 
   1762  1.28  augustss 		return;
   1763   1.1   thorpej 
   1764  1.28  augustss 	/***** Default *****/
   1765  1.28  augustss 	default:
   1766  1.28  augustss 		panic("%s: Unknown state %d\n",
   1767  1.28  augustss 		      USBDEVNAME(sc->sc_dev), sc->transfer_state);
   1768  1.28  augustss 	}
   1769   1.1   thorpej }
   1770   1.1   thorpej 
   1771   1.1   thorpej /*
   1772  1.28  augustss  * Command/Bulk/Interrupt (CBI) specific functions
   1773   1.1   thorpej  */
   1774   1.1   thorpej 
   1775  1.28  augustss Static int
   1776  1.28  augustss umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen,
   1777  1.28  augustss 	       usbd_xfer_handle xfer)
   1778   1.1   thorpej {
   1779  1.28  augustss 	usbd_device_handle dev;
   1780  1.28  augustss 
   1781  1.28  augustss 	KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
   1782  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto));
   1783  1.28  augustss 
   1784  1.28  augustss 	usbd_interface2device_handle(sc->iface, &dev);
   1785  1.28  augustss 
   1786  1.28  augustss 	sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE;
   1787  1.28  augustss 	sc->request.bRequest = UR_CBI_ADSC;
   1788  1.28  augustss 	USETW(sc->request.wValue, 0);
   1789  1.28  augustss 	USETW(sc->request.wIndex, sc->ifaceno);
   1790  1.28  augustss 	USETW(sc->request.wLength, buflen);
   1791  1.28  augustss 	return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer,
   1792  1.28  augustss 					 buflen, 0, xfer);
   1793  1.28  augustss }
   1794  1.28  augustss 
   1795   1.1   thorpej 
   1796  1.28  augustss Static void
   1797  1.28  augustss umass_cbi_reset(struct umass_softc *sc, int status)
   1798  1.28  augustss {
   1799  1.28  augustss 	int i;
   1800  1.28  augustss #	define SEND_DIAGNOSTIC_CMDLEN	12
   1801   1.1   thorpej 
   1802  1.28  augustss 	KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
   1803  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto));
   1804  1.15   thorpej 
   1805  1.28  augustss 	if (sc->sc_dying)
   1806  1.28  augustss 		return;
   1807  1.28  augustss 
   1808  1.28  augustss 	/*
   1809  1.28  augustss 	 * Command Block Reset Protocol
   1810  1.28  augustss 	 *
   1811  1.28  augustss 	 * First send a reset request to the device. Then clear
   1812  1.28  augustss 	 * any possibly stalled bulk endpoints.
   1813  1.28  augustss 
   1814  1.28  augustss 	 * This is done in 3 steps, states:
   1815  1.28  augustss 	 * TSTATE_CBI_RESET1
   1816  1.28  augustss 	 * TSTATE_CBI_RESET2
   1817  1.28  augustss 	 * TSTATE_CBI_RESET3
   1818  1.28  augustss 	 *
   1819  1.28  augustss 	 * If the reset doesn't succeed, the device should be port reset.
   1820  1.28  augustss 	 */
   1821  1.28  augustss 
   1822  1.28  augustss 	DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n",
   1823  1.28  augustss 		USBDEVNAME(sc->sc_dev)));
   1824  1.28  augustss 
   1825  1.28  augustss 	KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN,
   1826  1.28  augustss 		("%s: CBL struct is too small (%d < %d)\n",
   1827  1.28  augustss 			USBDEVNAME(sc->sc_dev),
   1828  1.28  augustss 			sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN));
   1829  1.28  augustss 
   1830  1.28  augustss 	sc->transfer_state = TSTATE_CBI_RESET1;
   1831  1.28  augustss 	sc->transfer_status = status;
   1832  1.28  augustss 
   1833  1.28  augustss 	/* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between
   1834  1.28  augustss 	 * the two the last 10 bytes of the cbl is filled with 0xff (section
   1835  1.28  augustss 	 * 2.2 of the CBI spec).
   1836  1.28  augustss 	 */
   1837  1.28  augustss 	sc->cbl[0] = 0x1d;	/* Command Block Reset */
   1838  1.28  augustss 	sc->cbl[1] = 0x04;
   1839  1.28  augustss 	for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++)
   1840  1.28  augustss 		sc->cbl[i] = 0xff;
   1841  1.28  augustss 
   1842  1.28  augustss 	umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN,
   1843  1.28  augustss 		       sc->transfer_xfer[XFER_CBI_RESET1]);
   1844  1.28  augustss 	/* XXX if the command fails we should reset the port on the bub */
   1845  1.28  augustss }
   1846  1.28  augustss 
   1847  1.28  augustss Static void
   1848  1.28  augustss umass_cbi_transfer(struct umass_softc *sc, int lun,
   1849  1.28  augustss 		void *cmd, int cmdlen, void *data, int datalen, int dir,
   1850  1.28  augustss 		transfer_cb_f cb, void *priv)
   1851  1.28  augustss {
   1852  1.28  augustss 	DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n",
   1853  1.28  augustss 		USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen));
   1854  1.28  augustss 
   1855  1.28  augustss 	KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
   1856  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_cbi_transfer\n",
   1857  1.28  augustss 		sc->proto));
   1858  1.28  augustss 
   1859  1.28  augustss 	if (sc->sc_dying)
   1860  1.28  augustss 		return;
   1861  1.28  augustss 
   1862  1.28  augustss 	/*
   1863  1.28  augustss 	 * Do a CBI transfer with cmdlen bytes from cmd, possibly
   1864  1.28  augustss 	 * a data phase of datalen bytes from/to the device and finally a
   1865  1.28  augustss 	 * csw read phase.
   1866  1.28  augustss 	 * If the data direction was inbound a maximum of datalen bytes
   1867  1.28  augustss 	 * is stored in the buffer pointed to by data.
   1868  1.28  augustss 	 *
   1869  1.28  augustss 	 * umass_cbi_transfer initialises the transfer and lets the state
   1870  1.28  augustss 	 * machine in umass_cbi_state handle the completion. It uses the
   1871  1.28  augustss 	 * following states:
   1872  1.28  augustss 	 * TSTATE_CBI_COMMAND
   1873  1.28  augustss 	 *   -> XXX fill in
   1874  1.28  augustss 	 *
   1875  1.28  augustss 	 * An error in any of those states will invoke
   1876  1.28  augustss 	 * umass_cbi_reset.
   1877  1.28  augustss 	 */
   1878  1.28  augustss 
   1879  1.28  augustss 	/* check the given arguments */
   1880  1.28  augustss 	KASSERT(datalen == 0 || data != NULL,
   1881  1.28  augustss 		("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev)));
   1882  1.28  augustss 	KASSERT(datalen == 0 || dir != DIR_NONE,
   1883  1.28  augustss 		("%s: direction is NONE while datalen is not zero\n",
   1884  1.28  augustss 			USBDEVNAME(sc->sc_dev)));
   1885  1.28  augustss 
   1886  1.28  augustss 	/* store the details for the data transfer phase */
   1887  1.28  augustss 	sc->transfer_dir = dir;
   1888  1.28  augustss 	sc->transfer_data = data;
   1889  1.28  augustss 	sc->transfer_datalen = datalen;
   1890  1.28  augustss 	sc->transfer_actlen = 0;
   1891  1.28  augustss 	sc->transfer_cb = cb;
   1892  1.28  augustss 	sc->transfer_priv = priv;
   1893  1.28  augustss 	sc->transfer_status = STATUS_CMD_OK;
   1894  1.28  augustss 
   1895  1.28  augustss 	/* move from idle to the command state */
   1896  1.28  augustss 	sc->transfer_state = TSTATE_CBI_COMMAND;
   1897  1.28  augustss 
   1898  1.28  augustss 	/* Send the Command Block from host to device via control endpoint. */
   1899  1.28  augustss 	if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB]))
   1900  1.28  augustss 		umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1901  1.28  augustss }
   1902  1.28  augustss 
   1903  1.28  augustss Static void
   1904  1.28  augustss umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv,
   1905  1.28  augustss 		usbd_status err)
   1906  1.28  augustss {
   1907  1.28  augustss 	struct umass_softc *sc = (struct umass_softc *) priv;
   1908  1.28  augustss 
   1909  1.28  augustss 	KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I),
   1910  1.28  augustss 		("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto));
   1911  1.28  augustss 
   1912  1.28  augustss 	if (sc->sc_dying)
   1913  1.28  augustss 		return;
   1914  1.28  augustss 
   1915  1.28  augustss 	/*
   1916  1.28  augustss 	 * State handling for CBI transfers.
   1917  1.28  augustss 	 */
   1918  1.28  augustss 
   1919  1.28  augustss 	DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n",
   1920  1.28  augustss 		USBDEVNAME(sc->sc_dev), sc->transfer_state,
   1921  1.28  augustss 		states[sc->transfer_state], xfer, usbd_errstr(err)));
   1922  1.28  augustss 
   1923  1.28  augustss 	switch (sc->transfer_state) {
   1924  1.28  augustss 
   1925  1.28  augustss 	/***** CBI Transfer *****/
   1926  1.28  augustss 	case TSTATE_CBI_COMMAND:
   1927  1.28  augustss 		if (err == USBD_STALLED) {
   1928  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n",
   1929  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1930  1.28  augustss 			/* Status transport by control pipe (section 2.3.2.1).
   1931  1.28  augustss 			 * The command contained in the command block failed.
   1932  1.28  augustss 			 *
   1933  1.28  augustss 			 * The control pipe has already been unstalled by the
   1934  1.28  augustss 			 * USB stack.
   1935  1.28  augustss 			 * Section 2.4.3.1.1 states that the bulk in endpoints
   1936  1.28  augustss 			 * should not stalled at this point.
   1937  1.28  augustss 			 */
   1938  1.28  augustss 
   1939  1.28  augustss 			sc->transfer_state = TSTATE_IDLE;
   1940  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   1941  1.28  augustss 					sc->transfer_datalen,
   1942  1.28  augustss 					STATUS_CMD_FAILED);
   1943  1.28  augustss 
   1944  1.28  augustss 			return;
   1945  1.28  augustss 		} else if (err) {
   1946  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n",
   1947  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1948  1.28  augustss 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1949  1.28  augustss 
   1950  1.28  augustss 			return;
   1951  1.28  augustss 		}
   1952  1.28  augustss 
   1953  1.28  augustss 		sc->transfer_state = TSTATE_CBI_DATA;
   1954  1.28  augustss 		if (sc->transfer_dir == DIR_IN) {
   1955  1.28  augustss 			if (umass_setup_transfer(sc, sc->bulkin_pipe,
   1956  1.28  augustss 					sc->transfer_data, sc->transfer_datalen,
   1957  1.28  augustss 					USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1958  1.28  augustss 					sc->transfer_xfer[XFER_CBI_DATA]))
   1959  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1960  1.28  augustss 
   1961  1.28  augustss 		} else if (sc->transfer_dir == DIR_OUT) {
   1962  1.28  augustss 			memcpy(sc->data_buffer, sc->transfer_data,
   1963  1.28  augustss 			       sc->transfer_datalen);
   1964  1.28  augustss 			if (umass_setup_transfer(sc, sc->bulkout_pipe,
   1965  1.28  augustss 					sc->transfer_data, sc->transfer_datalen,
   1966  1.28  augustss 					USBD_NO_COPY,/* fixed length transfer */
   1967  1.28  augustss 					sc->transfer_xfer[XFER_CBI_DATA]))
   1968  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1969  1.28  augustss 
   1970  1.28  augustss 		} else if (sc->proto & PROTO_CBI_I) {
   1971  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
   1972  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1973  1.28  augustss 			sc->transfer_state = TSTATE_CBI_STATUS;
   1974  1.28  augustss 			if (umass_setup_transfer(sc, sc->intrin_pipe,
   1975  1.28  augustss 					&sc->sbl, sizeof(sc->sbl),
   1976  1.28  augustss 					0,	/* fixed length transfer */
   1977  1.28  augustss 					sc->transfer_xfer[XFER_CBI_STATUS])){
   1978  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   1979  1.28  augustss 			}
   1980  1.28  augustss 		} else {
   1981  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: no data phase\n",
   1982  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   1983  1.28  augustss 			/* No command completion interrupt. Request
   1984  1.28  augustss 			 * sense data.
   1985  1.28  augustss 			 */
   1986  1.28  augustss 			sc->transfer_state = TSTATE_IDLE;
   1987  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   1988  1.28  augustss 			       0, STATUS_CMD_UNKNOWN);
   1989  1.28  augustss 		}
   1990  1.28  augustss 
   1991  1.28  augustss 		return;
   1992  1.28  augustss 
   1993  1.28  augustss 	case TSTATE_CBI_DATA:
   1994  1.28  augustss 		/* retrieve the length of the transfer that was done */
   1995  1.28  augustss 		usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL);
   1996  1.28  augustss 		DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n",
   1997  1.28  augustss 			USBDEVNAME(sc->sc_dev), sc->transfer_actlen));
   1998  1.28  augustss 
   1999  1.28  augustss 		if (err) {
   2000  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, "
   2001  1.28  augustss 				"%s\n", USBDEVNAME(sc->sc_dev),
   2002  1.28  augustss 				(sc->transfer_dir == DIR_IN?"in":"out"),
   2003  1.28  augustss 				sc->transfer_datalen,usbd_errstr(err)));
   2004  1.28  augustss 
   2005  1.28  augustss 			if (err == USBD_STALLED) {
   2006  1.28  augustss 				umass_clear_endpoint_stall(sc,
   2007  1.28  augustss 					sc->bulkin, sc->bulkin_pipe,
   2008  1.28  augustss 					TSTATE_CBI_DCLEAR,
   2009  1.28  augustss 					sc->transfer_xfer[XFER_CBI_DCLEAR]);
   2010  1.28  augustss 			} else {
   2011  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2012  1.28  augustss 			}
   2013  1.28  augustss 			return;
   2014  1.28  augustss 		}
   2015  1.28  augustss 
   2016  1.28  augustss 		if (sc->transfer_dir == DIR_IN)
   2017  1.28  augustss 			memcpy(sc->transfer_data, sc->data_buffer,
   2018  1.28  augustss 			       sc->transfer_actlen);
   2019  1.28  augustss 
   2020  1.28  augustss 		DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN)
   2021  1.28  augustss 					umass_dump_buffer(sc, sc->transfer_data,
   2022  1.28  augustss 						sc->transfer_actlen, 48));
   2023  1.28  augustss 
   2024  1.28  augustss 		if (sc->proto & PROTO_CBI_I) {
   2025  1.28  augustss 			sc->transfer_state = TSTATE_CBI_STATUS;
   2026  1.28  augustss 			memset(&sc->sbl, 0, sizeof(sc->sbl));
   2027  1.28  augustss 			if (umass_setup_transfer(sc, sc->intrin_pipe,
   2028  1.28  augustss 				    &sc->sbl, sizeof(sc->sbl),
   2029  1.28  augustss 				    0,	/* fixed length transfer */
   2030  1.28  augustss 				    sc->transfer_xfer[XFER_CBI_STATUS])){
   2031  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2032  1.28  augustss 			}
   2033  1.28  augustss 		} else {
   2034  1.28  augustss 			/* No command completion interrupt. Request
   2035  1.28  augustss 			 * sense to get status of command.
   2036  1.28  augustss 			 */
   2037  1.28  augustss 			sc->transfer_state = TSTATE_IDLE;
   2038  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   2039  1.28  augustss 				sc->transfer_datalen - sc->transfer_actlen,
   2040  1.28  augustss 				STATUS_CMD_UNKNOWN);
   2041  1.28  augustss 		}
   2042  1.28  augustss 		return;
   2043  1.28  augustss 
   2044  1.28  augustss 	case TSTATE_CBI_STATUS:
   2045  1.28  augustss 		if (err) {
   2046  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n",
   2047  1.28  augustss 				USBDEVNAME(sc->sc_dev)));
   2048  1.28  augustss 			/* Status transport by interrupt pipe (section 2.3.2.2).
   2049  1.28  augustss 			 */
   2050  1.28  augustss 
   2051  1.28  augustss 			if (err == USBD_STALLED) {
   2052  1.28  augustss 				umass_clear_endpoint_stall(sc,
   2053  1.28  augustss 					sc->intrin, sc->intrin_pipe,
   2054  1.28  augustss 					TSTATE_CBI_SCLEAR,
   2055  1.28  augustss 					sc->transfer_xfer[XFER_CBI_SCLEAR]);
   2056  1.28  augustss 			} else {
   2057  1.28  augustss 				umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2058  1.28  augustss 			}
   2059  1.28  augustss 			return;
   2060  1.28  augustss 		}
   2061  1.28  augustss 
   2062  1.28  augustss 		/* Dissect the information in the buffer */
   2063  1.28  augustss 
   2064  1.28  augustss 		if (sc->proto & PROTO_UFI) {
   2065  1.28  augustss 			int status;
   2066  1.28  augustss 
   2067  1.28  augustss 			/* Section 3.4.3.1.3 specifies that the UFI command
   2068  1.28  augustss 			 * protocol returns an ASC and ASCQ in the interrupt
   2069  1.28  augustss 			 * data block.
   2070  1.28  augustss 			 */
   2071  1.28  augustss 
   2072  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, "
   2073  1.28  augustss 				"ASCQ = 0x%02x\n",
   2074  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   2075  1.28  augustss 				sc->sbl.ufi.asc, sc->sbl.ufi.ascq));
   2076  1.28  augustss 
   2077  1.28  augustss 			if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0)
   2078  1.28  augustss 				status = STATUS_CMD_OK;
   2079  1.28  augustss 			else
   2080  1.28  augustss 				status = STATUS_CMD_FAILED;
   2081  1.28  augustss 
   2082  1.28  augustss 			/* No sense, command successfull */
   2083  1.28  augustss 		} else {
   2084  1.28  augustss 			/* Command Interrupt Data Block */
   2085  1.28  augustss 			DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n",
   2086  1.28  augustss 				USBDEVNAME(sc->sc_dev),
   2087  1.28  augustss 				sc->sbl.common.type, sc->sbl.common.value));
   2088  1.28  augustss 
   2089  1.28  augustss 			if (sc->sbl.common.type == IDB_TYPE_CCI) {
   2090  1.28  augustss 				int err;
   2091  1.28  augustss 
   2092  1.28  augustss 				if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK)
   2093  1.28  augustss 							== IDB_VALUE_PASS) {
   2094  1.28  augustss 					err = STATUS_CMD_OK;
   2095  1.28  augustss 				} else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
   2096  1.28  augustss 							== IDB_VALUE_FAIL ||
   2097  1.28  augustss 					   (sc->sbl.common.value & IDB_VALUE_STATUS_MASK)
   2098  1.28  augustss 						== IDB_VALUE_PERSISTENT) {
   2099  1.28  augustss 					err = STATUS_CMD_FAILED;
   2100  1.28  augustss 				} else {
   2101  1.28  augustss 					err = STATUS_WIRE_FAILED;
   2102  1.28  augustss 				}
   2103  1.28  augustss 
   2104  1.28  augustss 				sc->transfer_state = TSTATE_IDLE;
   2105  1.28  augustss 				sc->transfer_cb(sc, sc->transfer_priv,
   2106  1.28  augustss 						sc->transfer_datalen,
   2107  1.28  augustss 						err);
   2108  1.28  augustss 			}
   2109  1.28  augustss 		}
   2110  1.28  augustss 		return;
   2111  1.28  augustss 
   2112  1.28  augustss 	case TSTATE_CBI_DCLEAR:
   2113  1.28  augustss 		if (err) {	/* should not occur */
   2114  1.28  augustss 			printf("%s: CBI bulk-in/out stall clear failed, %s\n",
   2115  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2116  1.28  augustss 			umass_cbi_reset(sc, STATUS_WIRE_FAILED);
   2117  1.28  augustss 		}
   2118  1.28  augustss 
   2119  1.28  augustss 		sc->transfer_state = TSTATE_IDLE;
   2120  1.28  augustss 		sc->transfer_cb(sc, sc->transfer_priv,
   2121  1.28  augustss 				sc->transfer_datalen,
   2122  1.28  augustss 				STATUS_CMD_FAILED);
   2123  1.28  augustss 		return;
   2124  1.28  augustss 
   2125  1.28  augustss 	case TSTATE_CBI_SCLEAR:
   2126  1.28  augustss 		if (err)	/* should not occur */
   2127  1.28  augustss 			printf("%s: CBI intr-in stall clear failed, %s\n",
   2128  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2129  1.28  augustss 
   2130  1.28  augustss 		/* Something really bad is going on. Reset the device */
   2131  1.28  augustss 		umass_cbi_reset(sc, STATUS_CMD_FAILED);
   2132  1.28  augustss 		return;
   2133  1.28  augustss 
   2134  1.28  augustss 	/***** CBI Reset *****/
   2135  1.28  augustss 	case TSTATE_CBI_RESET1:
   2136  1.28  augustss 		if (err)
   2137  1.28  augustss 			printf("%s: CBI reset failed, %s\n",
   2138  1.28  augustss 				USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2139  1.28  augustss 
   2140  1.28  augustss 		umass_clear_endpoint_stall(sc,
   2141  1.28  augustss 			sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2,
   2142  1.28  augustss 			sc->transfer_xfer[XFER_CBI_RESET2]);
   2143  1.28  augustss 
   2144  1.28  augustss 		return;
   2145  1.28  augustss 	case TSTATE_CBI_RESET2:
   2146  1.28  augustss 		if (err)	/* should not occur */
   2147  1.28  augustss 			printf("%s: CBI bulk-in stall clear failed, %s\n",
   2148  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2149  1.28  augustss 			/* no error recovery, otherwise we end up in a loop */
   2150  1.28  augustss 
   2151  1.28  augustss 		umass_clear_endpoint_stall(sc,
   2152  1.28  augustss 			sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3,
   2153  1.28  augustss 			sc->transfer_xfer[XFER_CBI_RESET3]);
   2154  1.28  augustss 
   2155  1.28  augustss 		return;
   2156  1.28  augustss 	case TSTATE_CBI_RESET3:
   2157  1.28  augustss 		if (err)	/* should not occur */
   2158  1.28  augustss 			printf("%s: CBI bulk-out stall clear failed, %s\n",
   2159  1.28  augustss 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2160  1.28  augustss 			/* no error recovery, otherwise we end up in a loop */
   2161  1.28  augustss 
   2162  1.28  augustss 		sc->transfer_state = TSTATE_IDLE;
   2163  1.28  augustss 		if (sc->transfer_priv) {
   2164  1.28  augustss 			sc->transfer_cb(sc, sc->transfer_priv,
   2165  1.28  augustss 					sc->transfer_datalen,
   2166  1.28  augustss 					sc->transfer_status);
   2167  1.28  augustss 		}
   2168  1.28  augustss 
   2169  1.28  augustss 		return;
   2170  1.28  augustss 
   2171  1.28  augustss 
   2172  1.28  augustss 	/***** Default *****/
   2173  1.28  augustss 	default:
   2174  1.28  augustss 		panic("%s: Unknown state %d\n",
   2175  1.28  augustss 		      USBDEVNAME(sc->sc_dev), sc->transfer_state);
   2176  1.28  augustss 	}
   2177  1.28  augustss }
   2178  1.28  augustss 
   2179  1.28  augustss usbd_status
   2180  1.28  augustss umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun)
   2181  1.28  augustss {
   2182  1.28  augustss 	usbd_device_handle dev;
   2183  1.28  augustss 	usb_device_request_t req;
   2184  1.28  augustss 	usbd_status err;
   2185  1.28  augustss 	usb_interface_descriptor_t *id;
   2186  1.28  augustss 
   2187  1.28  augustss 	*maxlun = 0;		/* Default to 0. */
   2188  1.28  augustss 
   2189  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
   2190  1.28  augustss 
   2191  1.28  augustss 	usbd_interface2device_handle(sc->iface, &dev);
   2192  1.28  augustss 	id = usbd_get_interface_descriptor(sc->iface);
   2193  1.28  augustss 
   2194  1.28  augustss 	/* The Get Max Lun command is a class-specific request. */
   2195  1.28  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
   2196  1.28  augustss 	req.bRequest = UR_BBB_GET_MAX_LUN;
   2197  1.28  augustss 	USETW(req.wValue, 0);
   2198  1.28  augustss 	USETW(req.wIndex, id->bInterfaceNumber);
   2199  1.28  augustss 	USETW(req.wLength, 1);
   2200  1.28  augustss 
   2201  1.28  augustss 	err = usbd_do_request(dev, &req, maxlun);
   2202  1.28  augustss 	switch (err) {
   2203  1.28  augustss 	case USBD_NORMAL_COMPLETION:
   2204  1.28  augustss 		DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n",
   2205  1.28  augustss 		    USBDEVNAME(sc->sc_dev), *maxlun));
   2206  1.28  augustss 		break;
   2207  1.28  augustss 
   2208  1.28  augustss 	case USBD_STALLED:
   2209  1.28  augustss 		/*
   2210  1.28  augustss 		 * Device doesn't support Get Max Lun request.
   2211  1.28  augustss 		 */
   2212  1.28  augustss 		err = USBD_NORMAL_COMPLETION;
   2213  1.28  augustss 		DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n",
   2214  1.28  augustss 		    USBDEVNAME(sc->sc_dev)));
   2215  1.28  augustss 		break;
   2216  1.28  augustss 
   2217  1.28  augustss 	case USBD_SHORT_XFER:
   2218  1.28  augustss 		/*
   2219  1.28  augustss 		 * XXX This must mean Get Max Lun is not supported, too!
   2220  1.28  augustss 		 */
   2221  1.28  augustss 		err = USBD_NORMAL_COMPLETION;
   2222  1.28  augustss 		DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n",
   2223  1.28  augustss 		    USBDEVNAME(sc->sc_dev)));
   2224  1.28  augustss 		break;
   2225  1.28  augustss 
   2226  1.28  augustss 	default:
   2227  1.28  augustss 		printf("%s: Get Max Lun failed: %s\n",
   2228  1.28  augustss 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
   2229  1.28  augustss 		/* XXX Should we port_reset the device? */
   2230  1.28  augustss 		break;
   2231  1.28  augustss 	}
   2232  1.28  augustss 
   2233  1.28  augustss 	return (err);
   2234  1.28  augustss }
   2235  1.28  augustss 
   2236  1.28  augustss 
   2237  1.28  augustss 
   2238  1.28  augustss #if defined(__FreeBSD__)
   2239  1.28  augustss /*
   2240  1.28  augustss  * CAM specific functions (used by SCSI, UFI, 8070)
   2241  1.28  augustss  */
   2242  1.28  augustss 
   2243  1.28  augustss Static int
   2244  1.28  augustss umass_cam_attach_sim()
   2245  1.28  augustss {
   2246  1.28  augustss 	struct cam_devq *devq;		/* Per device Queue */
   2247  1.28  augustss 
   2248  1.28  augustss 	/* A HBA is attached to the CAM layer.
   2249  1.28  augustss 	 *
   2250  1.28  augustss 	 * The CAM layer will then after a while start probing for
   2251  1.28  augustss 	 * devices on the bus. The number of devices is limitted to one.
   2252  1.28  augustss 	 */
   2253  1.28  augustss 
   2254  1.28  augustss 	/* SCSI transparent command set */
   2255  1.28  augustss 
   2256  1.28  augustss 	devq = cam_simq_alloc(1 /*maximum openings*/);
   2257  1.28  augustss 	if (devq == NULL)
   2258  1.28  augustss 		return(ENOMEM);
   2259  1.28  augustss 
   2260  1.28  augustss 	umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, DEVNAME,
   2261  1.28  augustss 				NULL /*priv*/, 0 /*unit number*/,
   2262  1.28  augustss 				1 /*maximum device openings*/,
   2263  1.28  augustss 				0 /*maximum tagged device openings*/,
   2264  1.28  augustss 				devq);
   2265  1.28  augustss 	if (umass_sim == NULL) {
   2266  1.28  augustss 		cam_simq_free(devq);
   2267  1.28  augustss 		return(ENOMEM);
   2268  1.28  augustss 	}
   2269  1.28  augustss 
   2270  1.28  augustss 	if(xpt_bus_register(umass_sim, 0) != CAM_SUCCESS)
   2271  1.28  augustss 		return(ENOMEM);
   2272  1.28  augustss 
   2273  1.28  augustss 	if (xpt_create_path(&umass_path, NULL, cam_sim_path(umass_sim),
   2274  1.28  augustss 			    UMASS_SCSIID_HOST, 0)
   2275  1.28  augustss 	    != CAM_REQ_CMP)
   2276  1.28  augustss 		return(ENOMEM);
   2277  1.28  augustss 
   2278  1.28  augustss 	return(0);
   2279  1.28  augustss }
   2280  1.28  augustss 
   2281  1.28  augustss #ifdef UMASS_DO_CAM_RESCAN
   2282  1.28  augustss /* this function is only used from umass_cam_rescan, so mention
   2283  1.28  augustss  * prototype down here.
   2284  1.28  augustss  */
   2285  1.28  augustss Static void umass_cam_rescan_callback(struct cam_periph *periph,union ccb *ccb);
   2286  1.28  augustss 
   2287  1.28  augustss Static void
   2288  1.28  augustss umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
   2289  1.28  augustss {
   2290  1.28  augustss #ifdef UMASS_DEBUG
   2291  1.28  augustss 	struct umass_softc *sc = devclass_get_softc(umass_devclass,
   2292  1.28  augustss 					       ccb->ccb_h.target_id);
   2293  1.28  augustss 
   2294  1.28  augustss 	if (ccb->ccb_h.status != CAM_REQ_CMP) {
   2295  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan failed, 0x%04x\n",
   2296  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2297  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2298  1.28  augustss 			ccb->ccb_h.status));
   2299  1.28  augustss 	} else {
   2300  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan succeeded, freeing resources.\n",
   2301  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2302  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2303  1.28  augustss 	}
   2304  1.28  augustss #endif
   2305  1.28  augustss 
   2306  1.28  augustss 	xpt_free_path(ccb->ccb_h.path);
   2307  1.28  augustss 	free(ccb, M_USBDEV);
   2308  1.28  augustss }
   2309  1.28  augustss 
   2310  1.28  augustss Static void
   2311  1.28  augustss umass_cam_rescan(struct umass_softc *sc)
   2312  1.28  augustss {
   2313  1.28  augustss 	struct cam_path *path;
   2314  1.28  augustss 	union ccb *ccb = malloc(sizeof(union ccb), M_USBDEV, M_WAITOK);
   2315  1.28  augustss 
   2316  1.28  augustss 	memset(ccb, 0, sizeof(union ccb));
   2317  1.28  augustss 
   2318  1.28  augustss 	DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: scanning bus for new device %d\n",
   2319  1.28  augustss 		USBDEVNAME(sc->sc_dev),	 cam_sim_path(umass_sim),
   2320  1.28  augustss 		device_get_unit(sc->sc_dev), 0,
   2321  1.28  augustss 		device_get_unit(sc->sc_dev)));
   2322  1.28  augustss 
   2323  1.28  augustss 	if (xpt_create_path(&path, xpt_periph, cam_sim_path(umass_sim),
   2324  1.28  augustss 		    device_get_unit(sc->sc_dev), 0)
   2325  1.28  augustss 	    != CAM_REQ_CMP)
   2326  1.28  augustss 		return;
   2327  1.28  augustss 
   2328  1.28  augustss 	xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
   2329  1.28  augustss 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
   2330  1.28  augustss 	ccb->ccb_h.cbfcnp = umass_cam_rescan_callback;
   2331  1.28  augustss 	ccb->crcn.flags = CAM_FLAG_NONE;
   2332  1.28  augustss 	xpt_action(ccb);
   2333  1.28  augustss 
   2334  1.28  augustss 	/* The scan is in progress now. */
   2335  1.28  augustss }
   2336  1.28  augustss #endif
   2337  1.28  augustss 
   2338  1.28  augustss Static int
   2339  1.28  augustss umass_cam_attach(struct umass_softc *sc)
   2340  1.28  augustss {
   2341  1.28  augustss 	/* SIM already attached at module load. The device is a target on the
   2342  1.28  augustss 	 * one SIM we registered: target device_get_unit(self).
   2343  1.28  augustss 	 */
   2344  1.28  augustss 
   2345  1.28  augustss 	/* The artificial limit UMASS_SCSIID_MAX is there because CAM expects
   2346  1.28  augustss 	 * a limit to the number of targets that are present on a SIM.
   2347  1.28  augustss 	 */
   2348  1.28  augustss 	if (device_get_unit(sc->sc_dev) > UMASS_SCSIID_MAX) {
   2349  1.28  augustss 		printf("%s: Increase UMASS_SCSIID_MAX (currently %d) in %s "
   2350  1.28  augustss 			"and try again.\n", USBDEVNAME(sc->sc_dev),
   2351  1.28  augustss 			UMASS_SCSIID_MAX, __FILE__);
   2352  1.28  augustss 		return(1);
   2353  1.28  augustss 	}
   2354  1.28  augustss 
   2355  1.28  augustss #ifdef UMASS_DO_CAM_RESCAN
   2356  1.28  augustss 	if (!cold) {
   2357  1.28  augustss 		/* Notify CAM of the new device. Any failure is benign, as the
   2358  1.28  augustss 		 * user can still do it by hand (camcontrol rescan <busno>).
   2359  1.28  augustss 		 * Only do this if we are not booting, because CAM does a scan
   2360  1.28  augustss 		 * after booting has completed, when interrupts have been
   2361  1.28  augustss 		 * enabled.
   2362  1.28  augustss 		 */
   2363  1.28  augustss 		umass_cam_rescan(sc);
   2364  1.28  augustss 	}
   2365  1.28  augustss #endif
   2366  1.28  augustss 
   2367  1.28  augustss 	return(0);	/* always succesful */
   2368  1.28  augustss }
   2369  1.28  augustss 
   2370  1.28  augustss /* umass_cam_detach
   2371  1.28  augustss  *	detach from the CAM layer
   2372  1.28  augustss  */
   2373  1.28  augustss 
   2374  1.28  augustss Static int
   2375  1.28  augustss umass_cam_detach_sim()
   2376  1.28  augustss {
   2377  1.28  augustss 	if (umass_sim)
   2378  1.28  augustss 		return(EBUSY);	/* XXX CAM can't handle disappearing SIMs yet */
   2379  1.28  augustss 
   2380  1.28  augustss 	if (umass_path) {
   2381  1.28  augustss 		/* XXX do we need to send an asynchroneous event for the SIM?
   2382  1.28  augustss 		xpt_async(AC_LOST_DEVICE, umass_path, NULL);
   2383  1.28  augustss 		 */
   2384  1.28  augustss 		xpt_free_path(umass_path);
   2385  1.28  augustss 		umass_path = NULL;
   2386  1.28  augustss 	}
   2387  1.28  augustss 
   2388  1.28  augustss 	if (umass_sim) {
   2389  1.28  augustss 		if (xpt_bus_deregister(cam_sim_path(umass_sim)))
   2390  1.28  augustss 			cam_sim_free(umass_sim, /*free_devq*/TRUE);
   2391  1.28  augustss 		else
   2392  1.28  augustss 			return(EBUSY);
   2393  1.28  augustss 
   2394  1.28  augustss 		umass_sim = NULL;
   2395  1.28  augustss 	}
   2396  1.28  augustss 
   2397  1.28  augustss 	return(0);
   2398  1.28  augustss }
   2399  1.28  augustss 
   2400  1.28  augustss Static int
   2401  1.28  augustss umass_cam_detach(struct umass_softc *sc)
   2402  1.28  augustss {
   2403  1.28  augustss 	struct cam_path *path;
   2404  1.28  augustss 
   2405  1.28  augustss 	/* detach of sim not done until module unload */
   2406  1.28  augustss 	DPRINTF(UDMASS_SCSI, ("%s: losing CAM device entry\n",
   2407  1.28  augustss 		USBDEVNAME(sc->sc_dev)));
   2408  1.28  augustss 
   2409  1.28  augustss 	if (xpt_create_path(&path, NULL, cam_sim_path(umass_sim),
   2410  1.28  augustss 		    device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD)
   2411  1.28  augustss 	    != CAM_REQ_CMP)
   2412  1.28  augustss 		return(ENOMEM);
   2413  1.28  augustss 	xpt_async(AC_LOST_DEVICE, path, NULL);
   2414  1.28  augustss 	xpt_free_path(path);
   2415  1.28  augustss 
   2416  1.28  augustss 	return(0);
   2417  1.28  augustss }
   2418  1.28  augustss 
   2419  1.28  augustss 
   2420  1.28  augustss 
   2421  1.28  augustss /* umass_cam_action
   2422  1.28  augustss  *	CAM requests for action come through here
   2423  1.28  augustss  */
   2424  1.28  augustss 
   2425  1.28  augustss Static void
   2426  1.28  augustss umass_cam_action(struct cam_sim *sim, union ccb *ccb)
   2427  1.28  augustss {
   2428  1.28  augustss 	struct umass_softc *sc = devclass_get_softc(umass_devclass,
   2429  1.28  augustss 					       ccb->ccb_h.target_id);
   2430  1.28  augustss 
   2431  1.28  augustss 	/* The softc is still there, but marked as going away. umass_cam_detach
   2432  1.28  augustss 	 * has not yet notified CAM of the lost device however.
   2433  1.28  augustss 	 */
   2434  1.28  augustss 	if (sc && sc->sc_dying) {
   2435  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
   2436  1.28  augustss 			"Invalid target (gone)\n",
   2437  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2438  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2439  1.28  augustss 			ccb->ccb_h.func_code));
   2440  1.28  augustss 		ccb->ccb_h.status = CAM_TID_INVALID;
   2441  1.28  augustss 		xpt_done(ccb);
   2442  1.28  augustss 		return;
   2443  1.28  augustss 	}
   2444  1.28  augustss 
   2445  1.28  augustss 	/* Verify, depending on the operation to perform, that we either got a
   2446  1.28  augustss 	 * valid sc, because an existing target was referenced, or otherwise
   2447  1.28  augustss 	 * the SIM is addressed.
   2448  1.28  augustss 	 *
   2449  1.28  augustss 	 * This avoids bombing out at a printf and does give the CAM layer some
   2450  1.28  augustss 	 * sensible feedback on errors.
   2451  1.28  augustss 	 */
   2452  1.28  augustss 	switch (ccb->ccb_h.func_code) {
   2453  1.28  augustss 	case XPT_SCSI_IO:
   2454  1.28  augustss 	case XPT_RESET_DEV:
   2455  1.28  augustss 	case XPT_GET_TRAN_SETTINGS:
   2456  1.28  augustss 	case XPT_SET_TRAN_SETTINGS:
   2457  1.28  augustss 	case XPT_CALC_GEOMETRY:
   2458  1.28  augustss 		/* the opcodes requiring a target. These should never occur. */
   2459  1.28  augustss 		if (sc == NULL) {
   2460  1.28  augustss 			printf("%s:%d:%d:%d:func_code 0x%04x: "
   2461  1.28  augustss 				"Invalid target\n",
   2462  1.28  augustss 				DEVNAME_SIM, UMASS_SCSI_BUS,
   2463  1.28  augustss 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2464  1.28  augustss 				ccb->ccb_h.func_code);
   2465  1.28  augustss 
   2466  1.28  augustss 			ccb->ccb_h.status = CAM_TID_INVALID;
   2467  1.28  augustss 			xpt_done(ccb);
   2468  1.28  augustss 			return;
   2469  1.28  augustss 		}
   2470  1.28  augustss 		break;
   2471  1.28  augustss 	case XPT_PATH_INQ:
   2472  1.28  augustss 	case XPT_NOOP:
   2473  1.28  augustss 		/* The opcodes sometimes aimed at a target (sc is valid),
   2474  1.28  augustss 		 * sometimes aimed at the SIM (sc is invalid and target is
   2475  1.28  augustss 		 * CAM_TARGET_WILDCARD)
   2476  1.28  augustss 		 */
   2477  1.28  augustss 		if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
   2478  1.28  augustss 			DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
   2479  1.28  augustss 				"Invalid target\n",
   2480  1.28  augustss 				DEVNAME_SIM, UMASS_SCSI_BUS,
   2481  1.28  augustss 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2482  1.28  augustss 				ccb->ccb_h.func_code));
   2483  1.28  augustss 
   2484  1.28  augustss 			ccb->ccb_h.status = CAM_TID_INVALID;
   2485  1.28  augustss 			xpt_done(ccb);
   2486  1.28  augustss 			return;
   2487  1.28  augustss 		}
   2488  1.28  augustss 		break;
   2489  1.28  augustss 	default:
   2490  1.28  augustss 		/* XXX Hm, we should check the input parameters */
   2491  1.28  augustss 	}
   2492  1.28  augustss 
   2493  1.28  augustss 	/* Perform the requested action */
   2494  1.28  augustss 	switch (ccb->ccb_h.func_code) {
   2495  1.28  augustss 	case XPT_SCSI_IO:
   2496  1.28  augustss 	{
   2497  1.28  augustss 		struct ccb_scsiio *csio = &ccb->csio;	/* deref union */
   2498  1.28  augustss 		int dir;
   2499  1.28  augustss 		unsigned char *cmd;
   2500  1.28  augustss 		int cmdlen;
   2501  1.28  augustss 
   2502  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
   2503  1.28  augustss 			"cmd: 0x%02x, flags: 0x%02x, "
   2504  1.28  augustss 			"%db cmd/%db data/%db sense\n",
   2505  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2506  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2507  1.28  augustss 			csio->cdb_io.cdb_bytes[0],
   2508  1.28  augustss 			ccb->ccb_h.flags & CAM_DIR_MASK,
   2509  1.28  augustss 			csio->cdb_len, csio->dxfer_len,
   2510  1.28  augustss 			csio->sense_len));
   2511  1.28  augustss 
   2512  1.28  augustss 		/* clear the end of the buffer to make sure we don't send out
   2513  1.28  augustss 		 * garbage.
   2514  1.28  augustss 		 */
   2515  1.28  augustss 		DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK)
   2516  1.28  augustss 				     == CAM_DIR_OUT)
   2517  1.28  augustss 					umass_dump_buffer(sc, csio->data_ptr,
   2518  1.28  augustss 						csio->dxfer_len, 48));
   2519  1.28  augustss 
   2520  1.28  augustss 		if (sc->transfer_state != TSTATE_IDLE) {
   2521  1.28  augustss 			DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: "
   2522  1.28  augustss 				"I/O requested while busy (state %d, %s)\n",
   2523  1.28  augustss 				USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2524  1.28  augustss 				ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2525  1.28  augustss 				sc->transfer_state,states[sc->transfer_state]));
   2526  1.28  augustss 			ccb->ccb_h.status = CAM_SCSI_BUSY;
   2527  1.28  augustss 			xpt_done(ccb);
   2528  1.28  augustss 			return;
   2529  1.28  augustss 		}
   2530  1.28  augustss 
   2531  1.28  augustss 		switch(ccb->ccb_h.flags&CAM_DIR_MASK) {
   2532  1.28  augustss 		case CAM_DIR_IN:
   2533  1.28  augustss 			dir = DIR_IN;
   2534  1.28  augustss 			break;
   2535  1.28  augustss 		case CAM_DIR_OUT:
   2536  1.28  augustss 			dir = DIR_OUT;
   2537  1.28  augustss 			break;
   2538  1.28  augustss 		default:
   2539  1.28  augustss 			dir = DIR_NONE;
   2540  1.28  augustss 		}
   2541  1.28  augustss 
   2542  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
   2543  1.28  augustss 		if (sc->transform(sc, csio->cdb_io.cdb_bytes, csio->cdb_len,
   2544  1.28  augustss 				  &cmd, &cmdlen)) {
   2545  1.28  augustss 			sc->transfer(sc, ccb->ccb_h.target_lun, cmd, cmdlen,
   2546  1.28  augustss 				     csio->data_ptr,
   2547  1.28  augustss 				     csio->dxfer_len, dir,
   2548  1.28  augustss 				     umass_cam_cb, (void *) ccb);
   2549  1.28  augustss 		} else {
   2550  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_INVALID;
   2551  1.28  augustss 			xpt_done(ccb);
   2552  1.28  augustss 		}
   2553  1.28  augustss 
   2554  1.28  augustss 		break;
   2555  1.28  augustss 	}
   2556  1.28  augustss 	case XPT_PATH_INQ:
   2557  1.28  augustss 	{
   2558  1.28  augustss 		struct ccb_pathinq *cpi = &ccb->cpi;
   2559  1.28  augustss 
   2560  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n",
   2561  1.28  augustss 			(sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
   2562  1.28  augustss 			UMASS_SCSI_BUS,
   2563  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2564  1.28  augustss 
   2565  1.28  augustss 		/* host specific information */
   2566  1.28  augustss 		cpi->version_num = 1;
   2567  1.28  augustss 		cpi->hba_inquiry = 0;
   2568  1.28  augustss 		cpi->target_sprt = 0;
   2569  1.28  augustss 		cpi->hba_misc = 0;
   2570  1.28  augustss 		cpi->hba_eng_cnt = 0;
   2571  1.28  augustss 		cpi->max_target = UMASS_SCSIID_MAX;	/* one target */
   2572  1.28  augustss 		cpi->max_lun = 0;	/* no LUN's supported */
   2573  1.28  augustss 		cpi->initiator_id = UMASS_SCSIID_HOST;
   2574  1.28  augustss 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
   2575  1.28  augustss 		strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
   2576  1.28  augustss 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
   2577  1.28  augustss 		cpi->unit_number = cam_sim_unit(sim);
   2578  1.28  augustss 		cpi->bus_id = UMASS_SCSI_BUS;
   2579  1.28  augustss 		if (sc) {
   2580  1.28  augustss 			cpi->base_transfer_speed = sc->transfer_speed;
   2581  1.28  augustss 			cpi->max_lun = sc->maxlun;
   2582  1.28  augustss 		}
   2583  1.28  augustss 
   2584  1.28  augustss 		cpi->ccb_h.status = CAM_REQ_CMP;
   2585  1.28  augustss 		xpt_done(ccb);
   2586  1.28  augustss 		break;
   2587  1.28  augustss 	}
   2588  1.28  augustss 	case XPT_RESET_DEV:
   2589  1.28  augustss 	{
   2590  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n",
   2591  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2592  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2593  1.28  augustss 
   2594  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_INPROG;
   2595  1.28  augustss 		umass_reset(sc, umass_cam_cb, (void *) ccb);
   2596  1.28  augustss 		break;
   2597  1.28  augustss 	}
   2598  1.28  augustss 	case XPT_GET_TRAN_SETTINGS:
   2599  1.28  augustss 	{
   2600  1.28  augustss 		struct ccb_trans_settings *cts = &ccb->cts;
   2601  1.28  augustss 
   2602  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
   2603  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2604  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2605  1.28  augustss 
   2606  1.28  augustss 		cts->valid = 0;
   2607  1.28  augustss 		cts->flags = 0;		/* no disconnection, tagging */
   2608  1.28  augustss 
   2609  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_CMP;
   2610  1.28  augustss 		xpt_done(ccb);
   2611  1.28  augustss 		break;
   2612  1.28  augustss 	}
   2613  1.28  augustss 	case XPT_SET_TRAN_SETTINGS:
   2614  1.28  augustss 	{
   2615  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
   2616  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2617  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2618  1.28  augustss 
   2619  1.28  augustss 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
   2620  1.28  augustss 		xpt_done(ccb);
   2621  1.28  augustss 		break;
   2622  1.28  augustss 	}
   2623  1.28  augustss 	case XPT_CALC_GEOMETRY:
   2624  1.28  augustss 	{
   2625  1.28  augustss 		struct ccb_calc_geometry *ccg = &ccb->ccg;
   2626  1.28  augustss 
   2627  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_CALC_GEOMETRY: "
   2628  1.28  augustss 			"Volume size = %d\n",
   2629  1.28  augustss 			USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS,
   2630  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2631  1.28  augustss 			ccg->volume_size));
   2632  1.28  augustss 
   2633  1.28  augustss 		/* XXX We should probably ask the drive for the details
   2634  1.28  augustss 		 *     instead of cluching them up ourselves
   2635  1.28  augustss 		 */
   2636  1.28  augustss 		if (sc->drive == ZIP_100) {
   2637  1.28  augustss 			ccg->heads = 64;
   2638  1.28  augustss 			ccg->secs_per_track = 32;
   2639  1.28  augustss 			ccg->cylinders = ccg->volume_size / ccg->heads
   2640  1.28  augustss 					  / ccg->secs_per_track;
   2641  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_CMP;
   2642  1.28  augustss 			break;
   2643  1.28  augustss 		} else if (sc->proto & PROTO_UFI) {
   2644  1.28  augustss 			ccg->heads = 2;
   2645  1.28  augustss 			if (ccg->volume_size == 2880)
   2646  1.28  augustss 				ccg->secs_per_track = 18;
   2647  1.28  augustss 			else
   2648  1.28  augustss 				ccg->secs_per_track = 9;
   2649  1.28  augustss 			ccg->cylinders = 80;
   2650  1.28  augustss 			break;
   2651  1.28  augustss 		} else {
   2652  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
   2653  1.28  augustss 		}
   2654  1.28  augustss 
   2655  1.28  augustss 		xpt_done(ccb);
   2656  1.28  augustss 		break;
   2657  1.28  augustss 	}
   2658  1.28  augustss 	case XPT_NOOP:
   2659  1.28  augustss 	{
   2660  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n",
   2661  1.28  augustss 			(sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
   2662  1.28  augustss 			UMASS_SCSI_BUS,
   2663  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun));
   2664  1.28  augustss 
   2665  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_CMP;
   2666  1.28  augustss 		xpt_done(ccb);
   2667  1.28  augustss 		break;
   2668  1.28  augustss 	}
   2669  1.28  augustss 	default:
   2670  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: "
   2671  1.28  augustss 			"Not implemented\n",
   2672  1.28  augustss 			(sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)),
   2673  1.28  augustss 			UMASS_SCSI_BUS,
   2674  1.28  augustss 			ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
   2675  1.28  augustss 			ccb->ccb_h.func_code));
   2676  1.28  augustss 
   2677  1.28  augustss 		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
   2678  1.28  augustss 		xpt_done(ccb);
   2679  1.28  augustss 		break;
   2680  1.28  augustss 	}
   2681  1.28  augustss }
   2682  1.28  augustss 
   2683  1.28  augustss /* umass_cam_poll
   2684  1.28  augustss  *	all requests are handled through umass_cam_action, requests
   2685  1.28  augustss  *	are never pending. So, nothing to do here.
   2686  1.28  augustss  */
   2687  1.28  augustss Static void
   2688  1.28  augustss umass_cam_poll(struct cam_sim *sim)
   2689  1.28  augustss {
   2690   1.1   thorpej #ifdef UMASS_DEBUG
   2691  1.28  augustss 	struct umass_softc *sc = (struct umass_softc *) sim->softc;
   2692  1.28  augustss 
   2693  1.28  augustss 	DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n",
   2694  1.28  augustss 		USBDEVNAME(sc->sc_dev)));
   2695  1.28  augustss #endif
   2696  1.28  augustss 
   2697  1.28  augustss 	/* nop */
   2698  1.28  augustss }
   2699  1.28  augustss 
   2700  1.28  augustss 
   2701  1.28  augustss /* umass_cam_cb
   2702  1.28  augustss  *	finalise a completed CAM command
   2703  1.28  augustss  */
   2704  1.28  augustss 
   2705  1.28  augustss Static void
   2706  1.28  augustss umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status)
   2707  1.28  augustss {
   2708  1.28  augustss 	union ccb *ccb = (union ccb *) priv;
   2709  1.28  augustss 	struct ccb_scsiio *csio = &ccb->csio;		/* deref union */
   2710  1.28  augustss 
   2711  1.28  augustss 	csio->resid = residue;
   2712  1.28  augustss 
   2713  1.28  augustss 	switch (status) {
   2714  1.28  augustss 	case STATUS_CMD_OK:
   2715  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_CMP;
   2716  1.28  augustss 		xpt_done(ccb);
   2717  1.28  augustss 		break;
   2718  1.28  augustss 
   2719  1.28  augustss 	case STATUS_CMD_UNKNOWN:
   2720  1.28  augustss 	case STATUS_CMD_FAILED:
   2721  1.28  augustss 		switch (ccb->ccb_h.func_code) {
   2722  1.28  augustss 		case XPT_SCSI_IO:
   2723  1.28  augustss 		{
   2724  1.28  augustss 			unsigned char *cmd;
   2725  1.28  augustss 			int cmdlen;
   2726  1.28  augustss 
   2727  1.28  augustss 			/* fetch sense data */
   2728  1.28  augustss 			DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n",
   2729  1.28  augustss 			        USBDEVNAME(sc->sc_dev),
   2730  1.28  augustss 			        sc->cam_scsi_sense.length));
   2731  1.28  augustss 
   2732  1.28  augustss 			sc->cam_scsi_sense.length = csio->sense_len;
   2733  1.28  augustss 
   2734  1.28  augustss 			if (sc->transform(sc, (char *) &sc->cam_scsi_sense,
   2735  1.28  augustss 				      sizeof(sc->cam_scsi_sense),
   2736  1.28  augustss 				      &cmd, &cmdlen)) {
   2737  1.28  augustss 				sc->transfer(sc, ccb->ccb_h.target_lun,
   2738  1.28  augustss 					     cmd, cmdlen,
   2739  1.28  augustss 					     &csio->sense_data,
   2740  1.28  augustss 					     csio->sense_len, DIR_IN,
   2741  1.28  augustss 					     umass_cam_sense_cb, (void *) ccb);
   2742  1.28  augustss 			} else {
   2743  1.28  augustss #ifdef UMASS_DEBUG
   2744  1.28  augustss 				panic("transform(REQUEST_SENSE) failed\n");
   2745  1.28  augustss #else
   2746  1.28  augustss 				csio->resid = sc->transfer_datalen;
   2747  1.28  augustss 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
   2748  1.28  augustss 				xpt_done(ccb);
   2749  1.28  augustss #endif
   2750  1.28  augustss 			}
   2751  1.28  augustss 			break;
   2752  1.28  augustss 		}
   2753  1.28  augustss 		case XPT_RESET_DEV: /* Reset failed */
   2754  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
   2755  1.28  augustss 			xpt_done(ccb);
   2756  1.28  augustss 			break;
   2757  1.28  augustss 		default:
   2758  1.28  augustss 			panic("umass_cam_cb called for func_code %d\n",
   2759  1.28  augustss 			      ccb->ccb_h.func_code);
   2760  1.28  augustss 		}
   2761  1.28  augustss 		break;
   2762  1.28  augustss 
   2763  1.28  augustss 	case STATUS_WIRE_FAILED:
   2764  1.28  augustss 		/* the wire protocol failed and will have recovered
   2765  1.28  augustss 		 * (hopefully).	 We return an error to CAM and let CAM retry
   2766  1.28  augustss 		 * the command if necessary.
   2767  1.28  augustss 		 */
   2768  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_CMP_ERR;
   2769  1.28  augustss 		xpt_done(ccb);
   2770  1.28  augustss 		break;
   2771  1.28  augustss 
   2772  1.28  augustss 	default:
   2773  1.28  augustss 		panic("%s: Unknown status %d in umass_cam_cb\n",
   2774  1.28  augustss 			USBDEVNAME(sc->sc_dev), status);
   2775  1.28  augustss 	}
   2776  1.28  augustss }
   2777  1.28  augustss 
   2778  1.28  augustss /* Finalise a completed autosense operation
   2779  1.28  augustss  */
   2780  1.28  augustss Static void
   2781  1.28  augustss umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status)
   2782  1.28  augustss {
   2783  1.28  augustss 	union ccb *ccb = (union ccb *) priv;
   2784  1.28  augustss 	struct ccb_scsiio *csio = &ccb->csio;		/* deref union */
   2785  1.28  augustss 
   2786  1.28  augustss 	switch (status) {
   2787  1.28  augustss 	case STATUS_CMD_OK:
   2788  1.28  augustss 	case STATUS_CMD_UNKNOWN:
   2789  1.28  augustss 		/* Getting sense data succeeded. The length of the sense data
   2790  1.28  augustss 		 * is not returned in any way. The sense data itself contains
   2791  1.28  augustss 		 * the length of the sense data that is valid.
   2792  1.28  augustss 		 */
   2793  1.28  augustss 		if (sc->quirks & RS_NO_CLEAR_UA
   2794  1.28  augustss 		    && csio->cdb_io.cdb_bytes[0] == INQUIRY
   2795  1.28  augustss 		    && (csio->sense_data.flags & SSD_KEY)
   2796  1.28  augustss 						== SSD_KEY_UNIT_ATTENTION) {
   2797  1.28  augustss 			/* Ignore unit attention errors in the case where
   2798  1.28  augustss 			 * the Unit Attention state is not cleared on
   2799  1.28  augustss 			 * REQUEST SENSE. They will appear again at the next
   2800  1.28  augustss 			 * command.
   2801  1.28  augustss 			 */
   2802  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_CMP;
   2803  1.28  augustss 		} else if ((csio->sense_data.flags & SSD_KEY)
   2804  1.28  augustss 						== SSD_KEY_NO_SENSE) {
   2805  1.28  augustss 			/* No problem after all (in the case of CBI without
   2806  1.28  augustss 			 * CCI)
   2807  1.28  augustss 			 */
   2808  1.28  augustss 			ccb->ccb_h.status = CAM_REQ_CMP;
   2809  1.28  augustss 		} else {
   2810  1.28  augustss 			ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
   2811  1.28  augustss 					    | CAM_AUTOSNS_VALID;
   2812  1.28  augustss 			csio->scsi_status = SCSI_STATUS_CHECK_COND;
   2813  1.28  augustss 		}
   2814  1.28  augustss 		xpt_done(ccb);
   2815  1.28  augustss 		break;
   2816  1.28  augustss 
   2817  1.28  augustss 	default:
   2818  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
   2819  1.28  augustss 			USBDEVNAME(sc->sc_dev), status));
   2820  1.28  augustss 		ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
   2821  1.28  augustss 		xpt_done(ccb);
   2822  1.28  augustss 	}
   2823  1.28  augustss }
   2824  1.28  augustss 
   2825  1.28  augustss 
   2826  1.28  augustss Static int
   2827  1.28  augustss umass_driver_load(module_t mod, int what, void *arg)
   2828  1.28  augustss {
   2829  1.28  augustss 	int err;
   2830  1.28  augustss 
   2831  1.28  augustss 	switch (what) {
   2832  1.28  augustss 	case MOD_UNLOAD:
   2833  1.28  augustss 		err = umass_cam_detach_sim();
   2834  1.28  augustss 		if (err)
   2835  1.28  augustss 			return(err);
   2836  1.28  augustss 		return(usbd_driver_load(mod, what, arg));
   2837  1.28  augustss 	case MOD_LOAD:
   2838  1.28  augustss 		/* We don't attach to CAM at this point, because it will try
   2839  1.28  augustss 		 * and malloc memory for it. This is not possible when the
   2840  1.28  augustss 		 * boot loader loads umass as a module before the kernel
   2841  1.28  augustss 		 * has been bootstrapped.
   2842  1.28  augustss 		 */
   2843  1.28  augustss 	default:
   2844  1.28  augustss 		return(usbd_driver_load(mod, what, arg));
   2845  1.28  augustss 	}
   2846  1.28  augustss }
   2847  1.28  augustss 
   2848  1.28  augustss 
   2849  1.28  augustss 
   2850  1.28  augustss /* (even the comment is missing) */
   2851  1.28  augustss 
   2852  1.28  augustss DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0);
   2853  1.28  augustss 
   2854  1.28  augustss 
   2855  1.28  augustss /*
   2856  1.28  augustss  * SCSI specific functions
   2857  1.28  augustss  */
   2858  1.28  augustss 
   2859  1.28  augustss Static int
   2860  1.28  augustss umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
   2861  1.28  augustss 		     unsigned char **rcmd, int *rcmdlen)
   2862  1.28  augustss {
   2863  1.28  augustss 	*rcmd = cmd;		/* trivial copy */
   2864  1.28  augustss 	*rcmdlen = cmdlen;
   2865  1.28  augustss 
   2866  1.28  augustss 	switch (cmd[0]) {
   2867  1.28  augustss 	case TEST_UNIT_READY:
   2868  1.28  augustss 		if (sc->quirks & NO_TEST_UNIT_READY) {
   2869  1.28  augustss 			DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY "
   2870  1.28  augustss 				"to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
   2871  1.28  augustss 			cmd[0] = START_STOP_UNIT;
   2872  1.28  augustss 			cmd[4] = SSS_START;
   2873  1.28  augustss 		}
   2874  1.28  augustss 		break;
   2875  1.28  augustss 	}
   2876  1.28  augustss 
   2877  1.28  augustss 	return 1;		/* success */
   2878  1.28  augustss }
   2879  1.28  augustss 
   2880  1.28  augustss /*
   2881  1.28  augustss  * UFI specific functions
   2882  1.28  augustss  */
   2883  1.28  augustss 
   2884  1.28  augustss Static int
   2885  1.28  augustss umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
   2886  1.28  augustss 		    unsigned char **rcmd, int *rcmdlen)
   2887  1.28  augustss {
   2888  1.28  augustss 	*rcmd = cmd;
   2889  1.28  augustss 	/* A UFI command is always 12 bytes in length */
   2890  1.28  augustss 	/* XXX cmd[(cmdlen+1)..12] contains garbage */
   2891  1.28  augustss 	*rcmdlen = 12;
   2892  1.28  augustss 
   2893  1.28  augustss 	switch (cmd[0]) {
   2894  1.28  augustss 	case TEST_UNIT_READY:
   2895  1.28  augustss 		if (sc->quirks &  NO_TEST_UNIT_READY) {
   2896  1.28  augustss 			DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY "
   2897  1.28  augustss 				"to START_UNIT\n", USBDEVNAME(sc->sc_dev)));
   2898  1.28  augustss 			cmd[0] = START_STOP_UNIT;
   2899  1.28  augustss 			cmd[4] = SSS_START;
   2900  1.28  augustss 		}
   2901  1.28  augustss 		return 1;
   2902  1.28  augustss 	case INQUIRY:
   2903  1.28  augustss 	case START_STOP_UNIT:
   2904  1.28  augustss 	case MODE_SENSE:
   2905  1.28  augustss 	case PREVENT_ALLOW:
   2906  1.28  augustss 	case READ_10:
   2907  1.28  augustss 	case READ_12:
   2908  1.28  augustss 	case READ_CAPACITY:
   2909  1.28  augustss 	case REQUEST_SENSE:
   2910  1.28  augustss 	case REZERO_UNIT:
   2911  1.28  augustss 	case POSITION_TO_ELEMENT:	/* SEEK_10 */
   2912  1.28  augustss 	case SEND_DIAGNOSTIC:
   2913  1.28  augustss 	case WRITE_10:
   2914  1.28  augustss 	case WRITE_12:
   2915  1.28  augustss 	/* FORMAT_UNIT */
   2916  1.28  augustss 	/* MODE_SELECT */
   2917  1.28  augustss 	/* READ_FORMAT_CAPACITY */
   2918  1.28  augustss 	/* VERIFY */
   2919  1.28  augustss 	/* WRITE_AND_VERIFY */
   2920  1.28  augustss 		return 1;	/* success */
   2921  1.28  augustss 	default:
   2922  1.28  augustss 		return 0;	/* success */
   2923  1.28  augustss 	}
   2924  1.28  augustss }
   2925  1.28  augustss 
   2926  1.28  augustss /*
   2927  1.28  augustss  * 8070 specific functions
   2928  1.28  augustss  */
   2929  1.28  augustss Static int
   2930  1.28  augustss umass_8070_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen,
   2931  1.28  augustss 		     unsigned char **rcmd, int *rcmdlen)
   2932  1.28  augustss {
   2933  1.28  augustss 	return 0;	/* failure */
   2934  1.28  augustss }
   2935  1.28  augustss 
   2936  1.28  augustss #endif /* __FreeBSD__ */
   2937  1.28  augustss 
   2938  1.28  augustss 
   2939  1.28  augustss #ifdef UMASS_DEBUG
   2940  1.28  augustss Static void
   2941  1.28  augustss umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
   2942  1.28  augustss {
   2943  1.28  augustss 	int clen = cbw->bCDBLength;
   2944  1.28  augustss 	int dlen = UGETDW(cbw->dCBWDataTransferLength);
   2945  1.28  augustss 	u_int8_t *c = cbw->CBWCDB;
   2946  1.28  augustss 	int tag = UGETDW(cbw->dCBWTag);
   2947  1.28  augustss 	int flags = cbw->bCBWFlags;
   2948  1.28  augustss 
   2949  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db "
   2950  1.28  augustss 		"(0x%02x%02x%02x%02x%02x%02x%s), "
   2951  1.28  augustss 		"data = %d bytes, dir = %s\n",
   2952  1.28  augustss 		USBDEVNAME(sc->sc_dev), tag, clen,
   2953  1.28  augustss 		c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""),
   2954  1.28  augustss 		dlen, (flags == CBWFLAGS_IN? "in":
   2955  1.28  augustss 		       (flags == CBWFLAGS_OUT? "out":"<invalid>"))));
   2956  1.28  augustss }
   2957  1.28  augustss 
   2958  1.28  augustss Static void
   2959  1.28  augustss umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
   2960  1.28  augustss {
   2961  1.28  augustss 	int sig = UGETDW(csw->dCSWSignature);
   2962  1.28  augustss 	int tag = UGETW(csw->dCSWTag);
   2963  1.28  augustss 	int res = UGETDW(csw->dCSWDataResidue);
   2964  1.28  augustss 	int status = csw->bCSWStatus;
   2965  1.28  augustss 
   2966  1.28  augustss 	DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, "
   2967  1.28  augustss 		"res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev),
   2968  1.28  augustss 		tag, sig, (sig == CSWSIGNATURE?	 "valid":"invalid"),
   2969  1.28  augustss 		tag, res,
   2970  1.28  augustss 		status, (status == CSWSTATUS_GOOD? "good":
   2971  1.28  augustss 			 (status == CSWSTATUS_FAILED? "failed":
   2972  1.28  augustss 			  (status == CSWSTATUS_PHASE? "phase":"<invalid>")))));
   2973  1.28  augustss }
   2974  1.28  augustss 
   2975  1.28  augustss Static void
   2976  1.28  augustss umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen,
   2977  1.28  augustss 		  int printlen)
   2978  1.28  augustss {
   2979  1.28  augustss 	int i, j;
   2980  1.28  augustss 	char s1[40];
   2981  1.28  augustss 	char s2[40];
   2982  1.28  augustss 	char s3[5];
   2983  1.28  augustss 
   2984  1.28  augustss 	s1[0] = '\0';
   2985  1.28  augustss 	s3[0] = '\0';
   2986  1.28  augustss 
   2987  1.28  augustss 	sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
   2988  1.28  augustss 	for (i = 0; i < buflen && i < printlen; i++) {
   2989  1.28  augustss 		j = i % 16;
   2990  1.28  augustss 		if (j == 0 && i != 0) {
   2991  1.28  augustss 			DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n",
   2992  1.28  augustss 				USBDEVNAME(sc->sc_dev), s1, s2));
   2993  1.28  augustss 			s2[0] = '\0';
   2994  1.28  augustss 		}
   2995  1.28  augustss 		sprintf(&s1[j*2], "%02x", buffer[i] & 0xff);
   2996  1.28  augustss 	}
   2997  1.28  augustss 	if (buflen > printlen)
   2998  1.28  augustss 		sprintf(s3, " ...");
   2999  1.28  augustss 	DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n",
   3000  1.28  augustss 		USBDEVNAME(sc->sc_dev), s1, s2, s3));
   3001  1.28  augustss }
   3002  1.28  augustss #endif
   3003  1.28  augustss 
   3004  1.28  augustss 
   3005  1.28  augustss 
   3006  1.28  augustss 
   3007  1.28  augustss 
   3008  1.28  augustss 
   3009  1.28  augustss 
   3010  1.28  augustss 
   3011  1.28  augustss #if defined(__NetBSD__) || defined(__OpenBSD__)
   3012  1.28  augustss Static int
   3013  1.28  augustss umass_scsipi_cmd(xs)
   3014  1.28  augustss 	struct scsipi_xfer *xs;
   3015  1.28  augustss {
   3016  1.28  augustss 	struct scsipi_link *sc_link = xs->sc_link;
   3017  1.28  augustss 	struct umass_softc *sc = sc_link->adapter_softc;
   3018  1.28  augustss 	int dir;
   3019  1.28  augustss 
   3020  1.28  augustss 	DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL);
   3021  1.28  augustss 
   3022  1.28  augustss 	DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd:  %d:%d xs=%p cmd=0x%02x "
   3023  1.28  augustss 	    "(quirks=0x%x, poll=%d)\n", USBDEVNAME(sc->sc_dev),
   3024  1.28  augustss 	    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun,
   3025  1.28  augustss 	    xs, xs->cmd->opcode, sc_link->quirks,
   3026  1.28  augustss 	    xs->xs_control & XS_CTL_POLL));
   3027  1.28  augustss #if defined(USB_DEBUG) && defined(SCSIDEBUG)
   3028  1.28  augustss 	if (umassdebug & UDMASS_SCSI)
   3029  1.28  augustss 		show_scsipi_xs(xs);
   3030  1.28  augustss 	else if (umassdebug & ~UDMASS_CMD)
   3031  1.28  augustss 		show_scsipi_cmd(xs);
   3032  1.28  augustss #endif
   3033  1.28  augustss 
   3034  1.28  augustss 	if (sc->sc_dying) {
   3035  1.28  augustss 		xs->error = XS_DRIVER_STUFFUP;
   3036  1.28  augustss 		goto done;
   3037  1.28  augustss 	}
   3038  1.28  augustss 
   3039  1.28  augustss #ifdef UMASS_DEBUG
   3040  1.28  augustss 	if ((sc_link->type == BUS_ATAPI ?
   3041  1.28  augustss 	     sc_link->scsipi_atapi.drive : sc_link->scsipi_scsi.target)
   3042  1.28  augustss 	    != UMASS_SCSIID_DEVICE) {
   3043  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n",
   3044   1.1   thorpej 		    USBDEVNAME(sc->sc_dev),
   3045   1.9   thorpej 		    sc_link->scsipi_scsi.target));
   3046   1.1   thorpej 		xs->error = XS_DRIVER_STUFFUP;
   3047  1.28  augustss 		goto done;
   3048   1.1   thorpej 	}
   3049   1.1   thorpej #endif
   3050   1.1   thorpej 
   3051  1.28  augustss 	if (xs->cmd->opcode == SCSI_MODE_SENSE &&
   3052  1.28  augustss 	    (sc_link->quirks & SDEV_NOMODESENSE)) {
   3053  1.28  augustss 		/*printf("%s: SCSI_MODE_SENSE\n", USBDEVNAME(sc->sc_dev));*/
   3054  1.28  augustss 		xs->error = XS_TIMEOUT;
   3055  1.28  augustss 		goto done;
   3056  1.28  augustss 	}
   3057  1.28  augustss 
   3058  1.28  augustss 	if (xs->cmd->opcode == START_STOP &&
   3059  1.28  augustss 	    (sc->quirks & NO_START_STOP)) {
   3060  1.28  augustss 		/*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/
   3061  1.28  augustss 		xs->error = XS_NOERROR;
   3062  1.28  augustss 		goto done;
   3063  1.28  augustss 	}
   3064  1.28  augustss 
   3065   1.1   thorpej 	dir = DIR_NONE;
   3066   1.1   thorpej 	if (xs->datalen) {
   3067  1.28  augustss 		switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) {
   3068  1.20   thorpej 		case XS_CTL_DATA_IN:
   3069   1.1   thorpej 			dir = DIR_IN;
   3070   1.1   thorpej 			break;
   3071  1.20   thorpej 		case XS_CTL_DATA_OUT:
   3072   1.1   thorpej 			dir = DIR_OUT;
   3073   1.1   thorpej 			break;
   3074   1.1   thorpej 		}
   3075   1.1   thorpej 	}
   3076   1.1   thorpej 
   3077  1.28  augustss 	if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) {
   3078  1.28  augustss 		printf("umass_cmd: large datalen, %d\n", xs->datalen);
   3079  1.28  augustss 		xs->error = XS_DRIVER_STUFFUP;
   3080  1.28  augustss 		goto done;
   3081  1.28  augustss 	}
   3082  1.28  augustss 
   3083  1.28  augustss 	if (xs->xs_control & XS_CTL_POLL) {
   3084  1.28  augustss 		/* Use sync transfer. XXX Broken! */
   3085  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: sync dir=%d\n", dir));
   3086  1.28  augustss 		sc->sc_xfer_flags = USBD_SYNCHRONOUS;
   3087  1.28  augustss 		sc->sc_sync_status = USBD_INVAL;
   3088  1.28  augustss 		sc->transfer(sc, sc_link->scsipi_scsi.lun, xs->cmd, xs->cmdlen,
   3089  1.28  augustss 			     xs->data, xs->datalen, dir, 0, xs);
   3090  1.28  augustss 		sc->sc_xfer_flags = 0;
   3091  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n",
   3092  1.28  augustss 				      sc->sc_sync_status));
   3093  1.28  augustss 		switch (sc->sc_sync_status) {
   3094  1.28  augustss 		case USBD_NORMAL_COMPLETION:
   3095  1.28  augustss 			xs->error = XS_NOERROR;
   3096  1.28  augustss 			break;
   3097  1.28  augustss 		case USBD_TIMEOUT:
   3098  1.28  augustss 			xs->error = XS_TIMEOUT;
   3099  1.28  augustss 			break;
   3100  1.28  augustss 		default:
   3101  1.28  augustss 			xs->error = XS_DRIVER_STUFFUP;
   3102  1.28  augustss 			break;
   3103  1.28  augustss 		}
   3104  1.28  augustss 		goto done;
   3105  1.28  augustss 	} else {
   3106  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: async dir=%d, cmdlen=%d"
   3107  1.28  augustss 				      " datalen=%d\n",
   3108  1.28  augustss 				      dir, xs->cmdlen, xs->datalen));
   3109  1.28  augustss 		sc->transfer(sc, sc_link->scsipi_scsi.lun, xs->cmd, xs->cmdlen,
   3110  1.28  augustss 		    xs->data, xs->datalen, dir, umass_scsipi_cb, xs);
   3111  1.28  augustss 		return (SUCCESSFULLY_QUEUED);
   3112  1.28  augustss 	}
   3113  1.28  augustss 
   3114  1.28  augustss 	/* Return if command finishes early. */
   3115  1.28  augustss  done:
   3116  1.28  augustss 	xs->xs_status |= XS_STS_DONE;
   3117  1.28  augustss 	scsipi_done(xs);
   3118  1.28  augustss 	if (xs->xs_control & XS_CTL_POLL)
   3119  1.28  augustss 		return (COMPLETE);
   3120  1.28  augustss 	else
   3121  1.28  augustss 		return (SUCCESSFULLY_QUEUED);
   3122  1.28  augustss }
   3123  1.28  augustss 
   3124  1.28  augustss Static void
   3125  1.28  augustss umass_scsipi_minphys(bp)
   3126  1.28  augustss 	struct buf *bp;
   3127  1.28  augustss {
   3128  1.28  augustss 	if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE)
   3129  1.28  augustss 		bp->b_bcount = UMASS_MAX_TRANSFER_SIZE;
   3130  1.28  augustss 	minphys(bp);
   3131  1.28  augustss }
   3132  1.28  augustss 
   3133  1.28  augustss int
   3134  1.28  augustss umass_scsipi_ioctl(link, cmd, arg, flag, p)
   3135  1.28  augustss 	struct scsipi_link *link;
   3136  1.28  augustss 	u_long cmd;
   3137  1.28  augustss 	caddr_t arg;
   3138  1.28  augustss 	int flag;
   3139  1.28  augustss 	struct proc *p;
   3140  1.28  augustss {
   3141  1.28  augustss 	/*struct umass_softc *sc = link->adapter_softc;*/
   3142  1.28  augustss 
   3143  1.28  augustss 	switch (cmd) {
   3144  1.28  augustss #if 0
   3145  1.28  augustss 	case SCBUSIORESET:
   3146  1.28  augustss 		ccb->ccb_h.status = CAM_REQ_INPROG;
   3147  1.28  augustss 		umass_reset(sc, umass_cam_cb, (void *) ccb);
   3148  1.28  augustss 		return (0);
   3149  1.28  augustss #endif
   3150  1.28  augustss 	default:
   3151  1.28  augustss 		return (ENOTTY);
   3152  1.28  augustss 	}
   3153  1.28  augustss }
   3154  1.28  augustss 
   3155  1.28  augustss Static void
   3156  1.28  augustss umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status)
   3157  1.28  augustss {
   3158  1.28  augustss 	struct scsipi_xfer *xs = priv;
   3159  1.28  augustss 	struct scsipi_link *sc_link = xs->sc_link;
   3160  1.28  augustss 	int s;
   3161  1.28  augustss 
   3162  1.28  augustss 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: xs=%p residue=%d status=%d\n",
   3163  1.28  augustss 		xs, residue, status));
   3164  1.18  augustss 
   3165  1.28  augustss 	xs->resid = residue;
   3166   1.1   thorpej 
   3167  1.28  augustss 	switch (status) {
   3168  1.28  augustss 	case STATUS_CMD_OK:
   3169   1.1   thorpej 		xs->error = XS_NOERROR;
   3170  1.28  augustss 		break;
   3171  1.15   thorpej 
   3172  1.28  augustss 	case STATUS_CMD_UNKNOWN:
   3173  1.28  augustss 	case STATUS_CMD_FAILED:
   3174  1.28  augustss 		/* fetch sense data */
   3175  1.28  augustss 		memset(&sc->sc_sense_cmd, 0, sizeof(sc->sc_sense_cmd));
   3176  1.28  augustss 		sc->sc_sense_cmd.opcode = REQUEST_SENSE;
   3177  1.28  augustss 		sc->sc_sense_cmd.byte2 = sc_link->scsipi_scsi.lun <<
   3178  1.15   thorpej 		    SCSI_CMD_LUN_SHIFT;
   3179  1.28  augustss 		sc->sc_sense_cmd.length = sizeof(xs->sense);
   3180  1.28  augustss 
   3181  1.28  augustss 		sc->transfer(sc, sc_link->scsipi_scsi.lun,
   3182  1.28  augustss 			     &sc->sc_sense_cmd, sizeof(sc->sc_sense_cmd),
   3183  1.28  augustss 			     &xs->sense, sizeof(xs->sense), DIR_IN,
   3184  1.28  augustss 			     umass_scsipi_sense_cb, xs);
   3185  1.28  augustss 		return;
   3186  1.28  augustss 
   3187  1.28  augustss 	case STATUS_WIRE_FAILED:
   3188  1.28  augustss 		xs->error = XS_RESET;
   3189  1.28  augustss 		break;
   3190  1.15   thorpej 
   3191  1.28  augustss 	default:
   3192  1.28  augustss 		panic("%s: Unknown status %d in umass_scsipi_cb\n",
   3193  1.28  augustss 			USBDEVNAME(sc->sc_dev), status);
   3194  1.15   thorpej 	}
   3195   1.1   thorpej 
   3196  1.28  augustss 	xs->xs_status |= XS_STS_DONE;
   3197  1.28  augustss 
   3198  1.28  augustss 	DPRINTF(UDMASS_CMD,("umass_scsipi_cb: return xs->error=%d, "
   3199  1.28  augustss 		"xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
   3200  1.28  augustss 		xs->resid));
   3201   1.1   thorpej 
   3202  1.28  augustss 	s = splbio();
   3203   1.1   thorpej 	scsipi_done(xs);
   3204  1.28  augustss 	splx(s);
   3205  1.28  augustss }
   3206  1.28  augustss 
   3207  1.28  augustss /*
   3208  1.28  augustss  * Finalise a completed autosense operation
   3209  1.28  augustss  */
   3210  1.28  augustss Static void
   3211  1.28  augustss umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue,
   3212  1.28  augustss 		      int status)
   3213  1.28  augustss {
   3214  1.28  augustss 	struct scsipi_xfer *xs = priv;
   3215  1.28  augustss 	int s;
   3216  1.28  augustss 
   3217  1.28  augustss 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d "
   3218  1.28  augustss 		"status=%d\n", xs, residue, status));
   3219  1.28  augustss 
   3220  1.28  augustss 	switch (status) {
   3221  1.28  augustss 	case STATUS_CMD_OK:
   3222  1.28  augustss 	case STATUS_CMD_UNKNOWN:
   3223  1.28  augustss 		/* getting sense data succeeded */
   3224  1.28  augustss 		/* XXX look at residue */
   3225  1.28  augustss 		if (residue == 0 || residue == 14)/* XXX */
   3226  1.28  augustss 			xs->error = XS_SENSE;
   3227  1.28  augustss 		else
   3228  1.28  augustss 			xs->error = XS_SHORTSENSE;
   3229  1.28  augustss 		break;
   3230  1.28  augustss 	default:
   3231  1.28  augustss 		DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n",
   3232  1.28  augustss 			USBDEVNAME(sc->sc_dev), status));
   3233  1.28  augustss 		xs->error = XS_DRIVER_STUFFUP;
   3234  1.28  augustss 		break;
   3235  1.28  augustss 	}
   3236  1.18  augustss 
   3237  1.28  augustss 	xs->xs_status |= XS_STS_DONE;
   3238   1.1   thorpej 
   3239  1.28  augustss 	DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, "
   3240  1.28  augustss 		"xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status,
   3241  1.28  augustss 		xs->resid));
   3242   1.1   thorpej 
   3243  1.28  augustss 	s = splbio();
   3244  1.28  augustss 	scsipi_done(xs);
   3245  1.28  augustss 	splx(s);
   3246   1.1   thorpej }
   3247   1.1   thorpej 
   3248  1.28  augustss Static void
   3249  1.28  augustss umass_atapi_probedev(atapi, target)
   3250  1.28  augustss 	struct atapibus_softc *atapi;
   3251  1.28  augustss 	int target;
   3252   1.1   thorpej {
   3253  1.28  augustss 	struct scsipi_link *sc_link;
   3254  1.28  augustss 	struct scsipibus_attach_args sa;
   3255  1.28  augustss 	struct ata_drive_datas *drvp = &atapi->sc_drvs[target];
   3256  1.28  augustss 	char vendor[33], product[65], revision[17];
   3257  1.28  augustss 	struct scsipi_inquiry_data inqbuf;
   3258  1.28  augustss 
   3259  1.28  augustss 	DPRINTF(UDMASS_SCSI,("umass_atapi_probedev: atapi=%p target=%d\n",
   3260  1.28  augustss 			     atapi, target));
   3261  1.28  augustss 
   3262  1.28  augustss 	if (atapi->sc_link[target])
   3263  1.28  augustss 		return;
   3264  1.28  augustss 
   3265  1.28  augustss 	sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT);
   3266  1.28  augustss 	if (sc_link == NULL) {
   3267  1.28  augustss 		printf("%s: can't allocate link for drive %d\n",
   3268  1.28  augustss 		       atapi->sc_dev.dv_xname, target);
   3269  1.28  augustss 		return;
   3270  1.28  augustss 	}
   3271  1.28  augustss 	*sc_link = *atapi->adapter_link;
   3272  1.28  augustss 
   3273  1.28  augustss 	DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL);
   3274  1.28  augustss 
   3275  1.28  augustss 	/* Fill generic parts of the link. */
   3276  1.28  augustss 	sc_link->active = 0;
   3277  1.28  augustss 	sc_link->scsipi_atapi.drive = target;
   3278  1.28  augustss 	sc_link->device = &umass_dev;
   3279  1.28  augustss 	TAILQ_INIT(&sc_link->pending_xfers);
   3280  1.28  augustss 
   3281  1.28  augustss 	DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing inquiry\n"));
   3282  1.28  augustss 	/* Now go ask the device all about itself. */
   3283  1.28  augustss 	memset(&inqbuf, 0, sizeof(inqbuf));
   3284  1.28  augustss 	if (scsipi_inquire(sc_link, &inqbuf, XS_CTL_DISCOVERY) != 0)
   3285  1.28  augustss 		goto bad;
   3286  1.28  augustss 
   3287  1.28  augustss 	scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
   3288  1.28  augustss 	scsipi_strvis(product, 65, inqbuf.product, 16);
   3289  1.28  augustss 	scsipi_strvis(revision, 17, inqbuf.revision, 4);
   3290  1.28  augustss 
   3291  1.28  augustss 	sa.sa_sc_link = sc_link;
   3292  1.28  augustss 	sa.sa_inqbuf.type = inqbuf.device;
   3293  1.28  augustss 	sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
   3294  1.28  augustss 	    T_REMOV : T_FIXED;
   3295  1.28  augustss 	if (sa.sa_inqbuf.removable)
   3296  1.28  augustss 		sc_link->flags |= SDEV_REMOVABLE;
   3297  1.28  augustss 	/* XXX how? sc_link->scsipi_atapi.cap |= ACAP_LEN;*/
   3298  1.28  augustss 	sa.sa_inqbuf.vendor = vendor;
   3299  1.28  augustss 	sa.sa_inqbuf.product = product;
   3300  1.28  augustss 	sa.sa_inqbuf.revision = revision;
   3301  1.28  augustss 	sa.sa_inqptr = NULL;
   3302  1.28  augustss 
   3303  1.28  augustss 	drvp->drv_softc = atapi_probedev(atapi, target, sc_link, &sa);
   3304  1.28  augustss 	/* atapi_probedev() frees the scsipi_link when there is no device. */
   3305  1.28  augustss 	return;
   3306  1.28  augustss 
   3307  1.28  augustss bad:
   3308  1.28  augustss 	free(sc_link, M_DEVBUF);
   3309  1.28  augustss 	return;
   3310   1.1   thorpej }
   3311  1.28  augustss #endif
   3312