Home | History | Annotate | Line # | Download | only in usb
umass.c revision 1.21.2.2
      1  1.21.2.2   thorpej /*	$NetBSD: umass.c,v 1.21.2.2 1999/10/26 23:10:19 thorpej Exp $	*/
      2       1.7   thorpej 
      3       1.7   thorpej /*-
      4       1.7   thorpej  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5       1.7   thorpej  * All rights reserved.
      6       1.7   thorpej  *
      7       1.7   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8       1.7   thorpej  * by Jason R. Thorpe.
      9       1.7   thorpej  *
     10       1.7   thorpej  * Redistribution and use in source and binary forms, with or without
     11       1.7   thorpej  * modification, are permitted provided that the following conditions
     12       1.7   thorpej  * are met:
     13       1.7   thorpej  * 1. Redistributions of source code must retain the above copyright
     14       1.7   thorpej  *    notice, this list of conditions and the following disclaimer.
     15       1.7   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.7   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17       1.7   thorpej  *    documentation and/or other materials provided with the distribution.
     18       1.7   thorpej  * 3. All advertising materials mentioning features or use of this software
     19       1.7   thorpej  *    must display the following acknowledgement:
     20       1.7   thorpej  *        This product includes software developed by the NetBSD
     21       1.7   thorpej  *        Foundation, Inc. and its contributors.
     22       1.7   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.7   thorpej  *    contributors may be used to endorse or promote products derived
     24       1.7   thorpej  *    from this software without specific prior written permission.
     25       1.7   thorpej  *
     26       1.7   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.7   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.7   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.7   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.7   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.7   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.7   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.7   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.7   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.7   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.7   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37       1.7   thorpej  */
     38       1.1   thorpej 
     39       1.1   thorpej /*-
     40       1.1   thorpej  * Copyright (c) 1999 MAEKAWA Masahide <bishop (at) rr.iij4u.or.jp>,
     41       1.1   thorpej  *		      Nick Hibma <hibma (at) skylink.it>
     42       1.1   thorpej  * All rights reserved.
     43       1.1   thorpej  *
     44       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     45       1.1   thorpej  * modification, are permitted provided that the following conditions
     46       1.1   thorpej  * are met:
     47       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     48       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     49       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     50       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     51       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     52       1.1   thorpej  *
     53       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     54       1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55       1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56       1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     57       1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58       1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59       1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60       1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61       1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62       1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63       1.1   thorpej  * SUCH DAMAGE.
     64       1.1   thorpej  *
     65       1.1   thorpej  *	FreeBSD: src/sys/dev/usb/umass.c,v 1.8 1999/06/20 15:46:13 n_hibma Exp
     66       1.1   thorpej  */
     67       1.1   thorpej 
     68       1.1   thorpej /*
     69       1.3   thorpej  * Universal Serial Bus Mass Storage Class Control/Interrupt/Bulk (CBI)
     70       1.3   thorpej  * Specification:
     71       1.1   thorpej  *
     72      1.14  augustss  *	http://www.usb.org/developers/data/usbmass-cbi10.pdf
     73       1.3   thorpej  *
     74       1.3   thorpej  * Universal Serial Bus Mass Storage Bulk Only 1.0rc4 Specification:
     75       1.3   thorpej  *
     76      1.14  augustss  *	http://www.usb.org/developers/data/usbmassbulk_10rc4.pdf
     77       1.3   thorpej  *
     78       1.3   thorpej  * Relevant parts of the old spec (Bulk-only 0.9) have been quoted
     79       1.3   thorpej  * in the source.
     80       1.1   thorpej  */
     81       1.1   thorpej 
     82       1.1   thorpej /* To do:
     83       1.1   thorpej  *	x The umass_usb_transfer routine uses synchroneous transfers. This
     84       1.1   thorpej  *	  should be changed to async and state handling.
     85       1.1   thorpej  *
     86       1.1   thorpej  *	x Should handle more than just Iomega USB Zip drives.  There are
     87       1.1   thorpej  *	  a fair number of USB->SCSI dongles out there.
     88       1.2   thorpej  *
     89       1.2   thorpej  *	x Need to implement SCSI command timeout/abort handling.
     90       1.3   thorpej  *
     91       1.3   thorpej  *	x Add support for other than Bulk.
     92       1.6   thorpej  *
     93       1.6   thorpej  *	x Add support for other than SCSI.
     94       1.1   thorpej  */
     95       1.1   thorpej 
     96       1.1   thorpej /* Authors: (with short acronyms for comments)
     97       1.1   thorpej  *   NWH - Nick Hibma <hibma (at) skylink.it>
     98       1.1   thorpej  *   JRT - Jason R. Thorpe <thorpej (at) shagadelic.org>
     99       1.1   thorpej  */
    100       1.1   thorpej 
    101       1.1   thorpej #include <sys/param.h>
    102       1.1   thorpej #include <sys/systm.h>
    103       1.1   thorpej #include <sys/kernel.h>
    104       1.1   thorpej #include <sys/malloc.h>
    105       1.1   thorpej #include <sys/device.h>
    106       1.1   thorpej #include <sys/buf.h>
    107       1.1   thorpej #include <sys/proc.h>
    108       1.1   thorpej 
    109       1.1   thorpej #include <dev/usb/usb.h>
    110       1.1   thorpej #include <dev/usb/usbdi.h>
    111       1.1   thorpej #include <dev/usb/usbdi_util.h>
    112       1.1   thorpej 
    113       1.1   thorpej #include <dev/scsipi/scsi_all.h>
    114       1.1   thorpej #include <dev/scsipi/scsipi_all.h>
    115       1.1   thorpej #include <dev/scsipi/scsiconf.h>
    116       1.1   thorpej 
    117      1.18  augustss #if defined(USB_DEBUG) && !defined(UMASS_DEBUG)
    118      1.18  augustss #define UMASS_DEBUG 1
    119      1.18  augustss #endif
    120      1.18  augustss 
    121       1.1   thorpej #ifdef UMASS_DEBUG
    122       1.1   thorpej #define	DPRINTF(m, x)	if (umassdebug & (m)) logprintf x
    123       1.1   thorpej #define UDMASS_SCSI	0x00020000
    124       1.1   thorpej #define UDMASS_USB	0x00040000
    125       1.1   thorpej #define UDMASS_BULK	0x00080000
    126       1.1   thorpej #define UDMASS_ALL	0xffff0000
    127      1.10   thorpej int umassdebug = /* UDMASS_SCSI|UDMASS_BULK|UDMASS_USB */ 0;
    128       1.1   thorpej #else
    129       1.1   thorpej #define	DPRINTF(m, x)
    130       1.1   thorpej #endif
    131       1.1   thorpej 
    132       1.1   thorpej typedef struct umass_softc {
    133      1.12  augustss 	USBBASEDEVICE		sc_dev;		/* base device */
    134       1.1   thorpej 	usbd_interface_handle	sc_iface;	/* the interface we use */
    135       1.1   thorpej 
    136      1.17   thorpej 	u_int8_t		sc_subclass;	/* our USB subclass */
    137      1.17   thorpej 	u_int8_t		sc_protocol;	/* our USB protocol */
    138      1.17   thorpej 
    139       1.1   thorpej 	u_int8_t		sc_bulkout;	/* bulk-out Endpoint Address */
    140       1.1   thorpej 	usbd_pipe_handle	sc_bulkout_pipe;
    141       1.1   thorpej 	u_int8_t		sc_bulkin;	/* bulk-in Endpoint Address */
    142       1.1   thorpej 	usbd_pipe_handle	sc_bulkin_pipe;
    143       1.1   thorpej 
    144       1.1   thorpej 	struct scsipi_adapter	sc_adapter;
    145  1.21.2.1   thorpej 	struct scsipi_channel	sc_channel;
    146       1.9   thorpej 
    147      1.12  augustss 	device_ptr_t		sc_child;	/* child device, for detach */
    148       1.9   thorpej 
    149      1.18  augustss 	int			sc_refcnt;
    150       1.9   thorpej 	char			sc_dying;
    151       1.1   thorpej } umass_softc_t;
    152       1.1   thorpej 
    153       1.1   thorpej #define USBD_COMMAND_FAILED	USBD_INVAL	/* redefine some errors for */
    154       1.1   thorpej 
    155       1.1   thorpej #define UMASS_SCSIID_HOST	0x00
    156       1.1   thorpej #define UMASS_SCSIID_DEVICE	0x01
    157       1.1   thorpej 
    158       1.1   thorpej #define DIR_OUT		0
    159       1.1   thorpej #define DIR_IN		1
    160       1.1   thorpej #define DIR_NONE	2
    161       1.1   thorpej 
    162       1.1   thorpej /* Bulk-Only specific request */
    163       1.1   thorpej #define	UR_RESET	0xff
    164       1.7   thorpej #define	UR_GET_MAX_LUN	0xfe
    165       1.1   thorpej 
    166       1.1   thorpej /* Bulk-Only Mass Storage features */
    167       1.1   thorpej /* Command Block Wrapper */
    168       1.1   thorpej typedef struct {
    169       1.1   thorpej 	uDWord		dCBWSignature;
    170       1.1   thorpej #define  CBWSIGNATURE		0x43425355
    171       1.1   thorpej 	uDWord		dCBWTag;
    172       1.1   thorpej 	uDWord		dCBWDataTransferLength;
    173       1.1   thorpej 	uByte		bCBWFlags;
    174       1.1   thorpej #define	 CBWFLAGS_OUT	0x00
    175       1.1   thorpej #define	 CBWFLAGS_IN	0x80
    176       1.1   thorpej 	uByte		bCBWLUN;
    177       1.1   thorpej 	uByte		bCDBLength;
    178       1.1   thorpej 	uByte		CBWCDB[16];
    179       1.1   thorpej } usb_bulk_cbw_t;
    180       1.1   thorpej #define	USB_BULK_CBW_SIZE	31
    181       1.1   thorpej 
    182       1.1   thorpej /* Command Status Wrapper */
    183       1.1   thorpej typedef struct {
    184       1.1   thorpej 	uDWord		dCSWSignature;
    185       1.1   thorpej #define	 CSWSIGNATURE		0x53425355
    186       1.1   thorpej 	uDWord		dCSWTag;
    187       1.1   thorpej 	uDWord		dCSWDataResidue;
    188       1.1   thorpej 	uByte		bCSWStatus;
    189       1.1   thorpej #define  CSWSTATUS_GOOD		0x0
    190       1.1   thorpej #define  CSWSTATUS_FAILED	0x1
    191       1.1   thorpej #define  CSWSTATUS_PHASE	0x2
    192       1.1   thorpej } usb_bulk_csw_t;
    193       1.1   thorpej #define	USB_BULK_CSW_SIZE	13
    194       1.1   thorpej 
    195       1.1   thorpej 
    196       1.1   thorpej USB_DECLARE_DRIVER(umass);
    197       1.1   thorpej 
    198       1.1   thorpej /* USB related functions */
    199      1.15   thorpej usbd_status umass_usb_transfer __P((umass_softc_t *,
    200       1.1   thorpej 				usbd_pipe_handle pipe,
    201       1.1   thorpej 		                void *buf, int buflen,
    202       1.1   thorpej 				int flags, int *xfer_size));
    203       1.1   thorpej 
    204       1.1   thorpej /* Bulk-Only related functions */
    205       1.1   thorpej usbd_status umass_bulk_reset	__P((umass_softc_t *sc));
    206       1.7   thorpej usbd_status umass_bulk_get_max_lun __P((umass_softc_t *sc, u_int8_t *maxlun));
    207       1.1   thorpej usbd_status umass_bulk_transfer	__P((umass_softc_t *sc, int lun,
    208       1.1   thorpej 				void *cmd, int cmdlen,
    209       1.1   thorpej 		    		void *data, int datalen,
    210       1.1   thorpej 				int dir, int *residue));
    211       1.1   thorpej 
    212       1.9   thorpej void	umass_scsipi_minphys	__P((struct buf *));
    213  1.21.2.1   thorpej void	umass_scsipi_request	__P((struct scsipi_channel *,
    214  1.21.2.1   thorpej 				scsipi_adapter_req_t, void *));
    215       1.1   thorpej 
    216       1.1   thorpej 
    217       1.1   thorpej 
    218       1.1   thorpej USB_MATCH(umass)
    219       1.1   thorpej {
    220       1.1   thorpej 	USB_MATCH_START(umass, uaa);
    221       1.1   thorpej 	usb_interface_descriptor_t *id;
    222       1.1   thorpej 
    223       1.1   thorpej 	if (!uaa->iface)
    224       1.1   thorpej 		return(UMATCH_NONE);
    225       1.1   thorpej 
    226       1.1   thorpej 	id = usbd_get_interface_descriptor(uaa->iface);
    227       1.1   thorpej 	if (id
    228       1.1   thorpej 	    && id->bInterfaceClass == UCLASS_MASS
    229       1.1   thorpej 	    && id->bInterfaceSubClass == USUBCLASS_SCSI
    230      1.19  augustss 	    && id->bInterfaceProtocol == UPROTO_MASS_BULK_P)
    231       1.4   thorpej 		return(UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
    232       1.1   thorpej 
    233       1.1   thorpej 	return(UMATCH_NONE);
    234       1.1   thorpej }
    235       1.1   thorpej 
    236       1.1   thorpej USB_ATTACH(umass)
    237       1.1   thorpej {
    238       1.1   thorpej 	USB_ATTACH_START(umass, sc, uaa);
    239       1.1   thorpej 	usb_interface_descriptor_t *id;
    240       1.1   thorpej 	usb_endpoint_descriptor_t *ed;
    241       1.1   thorpej 	char devinfo[1024];
    242       1.7   thorpej 	usbd_status err;
    243       1.1   thorpej 	int i;
    244       1.7   thorpej 	u_int8_t maxlun;
    245      1.17   thorpej 	const char *subclass, *protocol;
    246       1.1   thorpej 
    247       1.1   thorpej 	sc->sc_iface = uaa->iface;
    248       1.1   thorpej 	sc->sc_bulkout_pipe = NULL;
    249       1.1   thorpej 	sc->sc_bulkin_pipe = NULL;
    250       1.1   thorpej 
    251       1.1   thorpej 	usbd_devinfo(uaa->device, 0, devinfo);
    252       1.1   thorpej 	USB_ATTACH_SETUP;
    253       1.1   thorpej 
    254       1.1   thorpej 	id = usbd_get_interface_descriptor(sc->sc_iface);
    255      1.17   thorpej 
    256      1.17   thorpej 	sc->sc_subclass = id->bInterfaceSubClass;
    257      1.17   thorpej 	sc->sc_protocol = id->bInterfaceProtocol;
    258      1.17   thorpej 
    259      1.17   thorpej 	switch (sc->sc_subclass) {
    260      1.17   thorpej #if 0
    261      1.17   thorpej 	case USUBCLASS_RBC:		subclass = "RBC";	break;
    262      1.17   thorpej 	case USUBCLASS_SFF8020I:	subclass = "8020i";	break;
    263      1.17   thorpej 	case USUBCLASS_QIC157:		subclass = "QIC157";	break;
    264      1.17   thorpej 	case USUBCLASS_UFI:		subclass = "UFI";	break;
    265      1.17   thorpej 	case USUBCLASS_SFF8070I:	subclass = "8070i";	break;
    266      1.17   thorpej #endif
    267      1.17   thorpej 	case USUBCLASS_SCSI:		subclass = "SCSI";	break;
    268      1.17   thorpej 	default:
    269      1.17   thorpej 		panic("umass_attach: impossible subclass");
    270      1.17   thorpej 	}
    271      1.17   thorpej 
    272      1.17   thorpej 	switch (sc->sc_protocol) {
    273      1.17   thorpej #if 0
    274      1.17   thorpej 	case UPROTO_MASS_CBI_I:		protocol = "CBI with CCI"; break;
    275      1.17   thorpej 	case UPROTO_MASS_CBI:		protocol = "CBI";	break;
    276      1.17   thorpej #endif
    277      1.19  augustss 	case UPROTO_MASS_BULK:		/* XXX Is this really right? */
    278      1.19  augustss 	case UPROTO_MASS_BULK_P:	protocol = "Bulk-Only";	break;
    279      1.17   thorpej 	default:
    280      1.17   thorpej 		panic("umass_attach: impossible protocol");
    281      1.17   thorpej 	}
    282      1.17   thorpej 
    283      1.17   thorpej 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    284      1.17   thorpej 	printf("%s: %s over %s (iclass %d/%d/%d)\n", USBDEVNAME(sc->sc_dev),
    285      1.17   thorpej 	    subclass, protocol, id->bInterfaceClass, id->bInterfaceSubClass,
    286      1.17   thorpej 	    id->bInterfaceProtocol);
    287       1.1   thorpej 
    288       1.1   thorpej 	/*
    289       1.1   thorpej 	 * A Bulk-Only Mass Storage device supports the following endpoints,
    290       1.1   thorpej 	 * in addition to the Endpoint 0 for Control transfer that is required
    291       1.1   thorpej 	 * of all USB devices:
    292       1.1   thorpej 	 * (a) bulk-in endpoint.
    293       1.1   thorpej 	 * (b) bulk-out endpoint.
    294       1.1   thorpej 	 *
    295       1.1   thorpej 	 * The endpoint addresses are not fixed, so we have to read them
    296       1.1   thorpej 	 * from the device descriptors of the current interface.
    297       1.1   thorpej 	 */
    298       1.1   thorpej 	for (i = 0 ; i < id->bNumEndpoints ; i++) {
    299       1.1   thorpej 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    300       1.1   thorpej 		if (!ed) {
    301       1.1   thorpej 			printf("%s: could not read endpoint descriptor\n",
    302       1.1   thorpej 			       USBDEVNAME(sc->sc_dev));
    303       1.1   thorpej 			USB_ATTACH_ERROR_RETURN;
    304       1.1   thorpej 		}
    305      1.11  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN
    306       1.1   thorpej 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    307       1.1   thorpej 			sc->sc_bulkin = ed->bEndpointAddress;
    308      1.11  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT
    309       1.1   thorpej 		    && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
    310       1.1   thorpej 			sc->sc_bulkout = ed->bEndpointAddress;
    311       1.1   thorpej 		}
    312       1.1   thorpej 	}
    313       1.1   thorpej 
    314       1.7   thorpej 	/*
    315       1.7   thorpej 	 * Get the maximum LUN supported by the device.
    316       1.7   thorpej 	 */
    317       1.7   thorpej 	err = umass_bulk_get_max_lun(sc, &maxlun);
    318       1.7   thorpej 	if (err != USBD_NORMAL_COMPLETION) {
    319       1.7   thorpej 		printf("%s: unable to get Max Lun: %s\n",
    320       1.7   thorpej 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    321       1.7   thorpej 		USB_ATTACH_ERROR_RETURN;
    322       1.7   thorpej 	}
    323       1.7   thorpej 
    324       1.1   thorpej 	/* Open the bulk-in and -out pipe */
    325       1.1   thorpej 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout,
    326       1.1   thorpej 				USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe);
    327       1.1   thorpej 	if (err) {
    328      1.14  augustss 		DPRINTF(UDMASS_USB,("cannot open bulk out pipe (address %d)\n",
    329       1.1   thorpej 			sc->sc_bulkout));
    330       1.1   thorpej 		USB_ATTACH_ERROR_RETURN;
    331       1.1   thorpej 	}
    332       1.1   thorpej 	err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin,
    333       1.1   thorpej 				USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe);
    334       1.1   thorpej 	if (err) {
    335      1.14  augustss 		DPRINTF(UDMASS_USB,("cannot open bulk in pipe (address %d)\n",
    336       1.1   thorpej 			sc->sc_bulkin));
    337       1.1   thorpej 		usbd_close_pipe(sc->sc_bulkout_pipe);
    338       1.1   thorpej 		USB_ATTACH_ERROR_RETURN;
    339       1.1   thorpej 	}
    340       1.1   thorpej 
    341       1.9   thorpej 	/* attach the device to the SCSIPI layer */
    342  1.21.2.1   thorpej 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    343  1.21.2.1   thorpej 	sc->sc_adapter.adapt_nchannels = 1;
    344  1.21.2.1   thorpej 	sc->sc_adapter.adapt_openings = 1;
    345  1.21.2.1   thorpej 	sc->sc_adapter.adapt_max_periph = 1;
    346  1.21.2.1   thorpej 	sc->sc_adapter.adapt_request = umass_scsipi_request;
    347  1.21.2.1   thorpej 	sc->sc_adapter.adapt_minphys = umass_scsipi_minphys;
    348  1.21.2.1   thorpej 
    349  1.21.2.1   thorpej 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    350  1.21.2.1   thorpej 	sc->sc_channel.chan_bustype = &scsi_bustype;
    351  1.21.2.1   thorpej 	sc->sc_channel.chan_ntargets = 2;
    352  1.21.2.1   thorpej 	sc->sc_channel.chan_nluns = maxlun + 1;
    353  1.21.2.1   thorpej 	sc->sc_channel.chan_id = UMASS_SCSIID_HOST;
    354       1.1   thorpej 
    355  1.21.2.1   thorpej 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    356       1.9   thorpej 	if (sc->sc_child == NULL) {
    357       1.1   thorpej 		usbd_close_pipe(sc->sc_bulkout_pipe);
    358       1.1   thorpej 		usbd_close_pipe(sc->sc_bulkin_pipe);
    359       1.1   thorpej 		/* XXX Not really an error. */
    360       1.1   thorpej 		USB_ATTACH_ERROR_RETURN;
    361       1.1   thorpej 	}
    362       1.1   thorpej 
    363       1.1   thorpej 	USB_ATTACH_SUCCESS_RETURN;
    364       1.1   thorpej }
    365       1.1   thorpej 
    366       1.1   thorpej int
    367       1.1   thorpej umass_activate(self, act)
    368       1.1   thorpej 	struct device *self;
    369       1.1   thorpej 	enum devact act;
    370       1.1   thorpej {
    371       1.9   thorpej 	struct umass_softc *sc = (struct umass_softc *) self;
    372      1.15   thorpej 	int s, rv = 0;
    373       1.9   thorpej 
    374       1.9   thorpej 	DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n",
    375       1.9   thorpej 	    USBDEVNAME(sc->sc_dev), act));
    376       1.1   thorpej 
    377      1.15   thorpej 	s = splhigh();
    378       1.1   thorpej 	switch (act) {
    379       1.1   thorpej 	case DVACT_ACTIVATE:
    380       1.9   thorpej 		rv = EOPNOTSUPP;
    381       1.1   thorpej 		break;
    382       1.1   thorpej 
    383       1.1   thorpej 	case DVACT_DEACTIVATE:
    384      1.15   thorpej 		if (sc->sc_child == NULL || sc->sc_dying)
    385       1.9   thorpej 			break;
    386       1.9   thorpej 		rv = config_deactivate(sc->sc_child);
    387       1.9   thorpej 		DPRINTF(UDMASS_USB, ("%s: umass_activate: child "
    388       1.9   thorpej 		    "returned %d\n", USBDEVNAME(sc->sc_dev), rv));
    389       1.9   thorpej 		if (rv == 0)
    390       1.9   thorpej 			sc->sc_dying = 1;
    391       1.1   thorpej 		break;
    392       1.1   thorpej 	}
    393      1.15   thorpej 	splx(s);
    394       1.9   thorpej 	return (rv);
    395       1.1   thorpej }
    396       1.1   thorpej 
    397      1.21  augustss USB_DETACH(umass)
    398       1.1   thorpej {
    399      1.21  augustss 	USB_DETACH_START(umass, sc);
    400      1.18  augustss 	int s, rv = 0;
    401       1.9   thorpej 
    402       1.9   thorpej 	DPRINTF(UDMASS_USB, ("%s: umass_detach: flags 0x%x\n",
    403       1.9   thorpej 	    USBDEVNAME(sc->sc_dev), flags));
    404       1.9   thorpej 
    405       1.9   thorpej 	if (sc->sc_child != NULL)
    406       1.9   thorpej 		rv = config_detach(sc->sc_child, flags);
    407       1.9   thorpej 
    408      1.18  augustss 	if (rv != 0)
    409      1.18  augustss 		return (rv);
    410      1.18  augustss 
    411      1.18  augustss 	/* Abort the pipes to wake up any waiting processes. */
    412      1.18  augustss 	if (sc->sc_bulkin_pipe != NULL)
    413      1.18  augustss 		usbd_abort_pipe(sc->sc_bulkin_pipe);
    414      1.18  augustss 	if (sc->sc_bulkout_pipe != NULL)
    415      1.18  augustss 		usbd_abort_pipe(sc->sc_bulkout_pipe);
    416      1.18  augustss 
    417      1.18  augustss 	s = splusb();
    418      1.18  augustss 	if (--sc->sc_refcnt >= 0) {
    419      1.18  augustss 		/* Wait for processes to go away. */
    420      1.18  augustss 		usb_detach_wait(USBDEV(sc->sc_dev));
    421      1.18  augustss 	}
    422      1.18  augustss 	splx(s);
    423      1.18  augustss 
    424      1.18  augustss 	if (sc->sc_bulkin_pipe != NULL) {
    425      1.18  augustss 		usbd_close_pipe(sc->sc_bulkin_pipe);
    426      1.18  augustss 		sc->sc_bulkin_pipe = NULL;
    427      1.18  augustss 	}
    428      1.18  augustss 	if (sc->sc_bulkout_pipe != NULL) {
    429      1.18  augustss 		usbd_close_pipe(sc->sc_bulkout_pipe);
    430      1.18  augustss 		sc->sc_bulkout_pipe = NULL;
    431       1.9   thorpej 	}
    432       1.1   thorpej 
    433       1.9   thorpej 	return (rv);
    434       1.1   thorpej }
    435       1.1   thorpej 
    436       1.1   thorpej /* Performs a request over a pipe.
    437       1.1   thorpej  *
    438       1.1   thorpej  * flags: Can be set to USBD_SHORT_XFER_OK
    439       1.1   thorpej  * xfer_size: if not null returns the nr. of bytes transferred
    440       1.1   thorpej  *
    441       1.1   thorpej  * If the returned error is USBD_STALLED the pipe stall has
    442       1.1   thorpej  * been cleared again.
    443       1.1   thorpej  */
    444       1.1   thorpej 
    445       1.1   thorpej usbd_status
    446      1.15   thorpej umass_usb_transfer(umass_softc_t *sc, usbd_pipe_handle pipe,
    447       1.1   thorpej 		   void *buf, int buflen, int flags, int *xfer_size)
    448       1.1   thorpej {
    449       1.1   thorpej 	usbd_request_handle reqh;
    450       1.1   thorpej 	usbd_private_handle priv;
    451       1.1   thorpej 	void *buffer;
    452       1.1   thorpej 	int size;
    453       1.1   thorpej 	usbd_status err;
    454       1.1   thorpej 
    455       1.1   thorpej 	/* A transfer is done synchronously. We create and schedule the
    456       1.1   thorpej 	 * transfer and then wait for it to complete
    457       1.1   thorpej 	 */
    458       1.1   thorpej 
    459      1.13  augustss 	reqh = usbd_alloc_request(usbd_pipe2device_handle(pipe));
    460       1.1   thorpej 	if (!reqh) {
    461      1.15   thorpej 		DPRINTF(UDMASS_USB, ("%s: not enough memory\n",
    462      1.15   thorpej 		    USBDEVNAME(sc->sc_dev)));
    463       1.1   thorpej 		return USBD_NOMEM;
    464       1.1   thorpej 	}
    465       1.1   thorpej 
    466      1.18  augustss 	usbd_setup_request(reqh, pipe, 0, buf, buflen,flags, 3000/*ms*/, NULL);
    467       1.1   thorpej 	err = usbd_sync_transfer(reqh);
    468       1.1   thorpej 	if (err) {
    469      1.15   thorpej 		DPRINTF(UDMASS_USB, ("%s: transfer failed: %s\n",
    470      1.15   thorpej 			USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    471       1.1   thorpej 		usbd_free_request(reqh);
    472       1.1   thorpej 		return(err);
    473       1.1   thorpej 	}
    474       1.1   thorpej 
    475       1.1   thorpej 	usbd_get_request_status(reqh, &priv, &buffer, &size, &err);
    476       1.1   thorpej 
    477       1.1   thorpej 	if (xfer_size)
    478       1.1   thorpej 		*xfer_size = size;
    479       1.1   thorpej 
    480       1.1   thorpej 	usbd_free_request(reqh);
    481       1.1   thorpej 	return(USBD_NORMAL_COMPLETION);
    482       1.1   thorpej }
    483       1.1   thorpej 
    484       1.7   thorpej usbd_status
    485       1.7   thorpej umass_bulk_get_max_lun(umass_softc_t *sc, u_int8_t *maxlun)
    486       1.7   thorpej {
    487       1.7   thorpej 	usbd_device_handle dev;
    488       1.7   thorpej 	usb_device_request_t req;
    489       1.7   thorpej 	usbd_status err;
    490       1.7   thorpej 	usb_interface_descriptor_t *id;
    491       1.7   thorpej 
    492      1.15   thorpej 	*maxlun = 0;		/* Default to 0. */
    493      1.15   thorpej 
    494       1.7   thorpej 	DPRINTF(UDMASS_BULK, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev)));
    495       1.1   thorpej 
    496       1.7   thorpej 	usbd_interface2device_handle(sc->sc_iface, &dev);
    497       1.7   thorpej 	id = usbd_get_interface_descriptor(sc->sc_iface);
    498       1.7   thorpej 
    499       1.7   thorpej 	/* The Get Max Lun command is a class-specific request. */
    500      1.14  augustss 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    501       1.7   thorpej 	req.bRequest = UR_GET_MAX_LUN;
    502       1.7   thorpej 	USETW(req.wValue, 0);
    503       1.7   thorpej 	USETW(req.wIndex, id->bInterfaceNumber);
    504       1.7   thorpej 	USETW(req.wLength, 1);
    505       1.7   thorpej 
    506       1.7   thorpej 	err = usbd_do_request(dev, &req, maxlun);
    507       1.7   thorpej 	switch (err) {
    508       1.7   thorpej 	case USBD_NORMAL_COMPLETION:
    509       1.7   thorpej 		DPRINTF(UDMASS_BULK, ("%s: Max Lun %d\n",
    510       1.7   thorpej 		    USBDEVNAME(sc->sc_dev), *maxlun));
    511       1.7   thorpej 		break;
    512       1.7   thorpej 
    513       1.7   thorpej 	case USBD_STALLED:
    514       1.7   thorpej 		/*
    515      1.15   thorpej 		 * Device doesn't support Get Max Lun request.
    516       1.7   thorpej 		 */
    517       1.7   thorpej 		err = USBD_NORMAL_COMPLETION;
    518       1.7   thorpej 		DPRINTF(UDMASS_BULK, ("%s: Get Max Lun not supported\n",
    519       1.7   thorpej 		    USBDEVNAME(sc->sc_dev)));
    520       1.7   thorpej 		break;
    521       1.7   thorpej 
    522      1.15   thorpej 	case USBD_SHORT_XFER:
    523      1.15   thorpej 		/*
    524      1.15   thorpej 		 * XXX This must mean Get Max Lun is not supported, too!
    525      1.15   thorpej 		 */
    526      1.15   thorpej 		err = USBD_NORMAL_COMPLETION;
    527      1.15   thorpej 		DPRINTF(UDMASS_BULK, ("%s: Get Max Lun SHORT_XFER\n",
    528      1.15   thorpej 		    USBDEVNAME(sc->sc_dev)));
    529      1.15   thorpej 		break;
    530      1.15   thorpej 
    531       1.7   thorpej 	default:
    532       1.7   thorpej 		printf("%s: Get Max Lun failed: %s\n",
    533       1.7   thorpej 		    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    534       1.7   thorpej 		/* XXX Should we port_reset the device? */
    535      1.14  augustss 		break;
    536       1.7   thorpej 	}
    537       1.1   thorpej 
    538       1.7   thorpej 	return (err);
    539       1.7   thorpej }
    540       1.1   thorpej 
    541       1.1   thorpej usbd_status
    542       1.1   thorpej umass_bulk_reset(umass_softc_t *sc)
    543       1.1   thorpej {
    544       1.1   thorpej 	usbd_device_handle dev;
    545       1.1   thorpej         usb_device_request_t req;
    546       1.1   thorpej 	usbd_status err;
    547       1.1   thorpej 	usb_interface_descriptor_t *id;
    548       1.1   thorpej 
    549       1.1   thorpej 	/*
    550       1.1   thorpej 	 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
    551       1.1   thorpej 	 *
    552       1.1   thorpej 	 * For Reset Recovery the host shall issue in the following order:
    553       1.1   thorpej 	 * a) a Bulk-Only Mass Storage Reset
    554       1.1   thorpej 	 * b) a Clear Feature HALT to the Bulk-In endpoint
    555       1.1   thorpej 	 * c) a Clear Feature HALT to the Bulk-Out endpoint
    556       1.1   thorpej 	 */
    557       1.1   thorpej 
    558       1.1   thorpej 	DPRINTF(UDMASS_BULK, ("%s: Reset\n",
    559       1.1   thorpej 		USBDEVNAME(sc->sc_dev)));
    560       1.1   thorpej 
    561       1.1   thorpej 	usbd_interface2device_handle(sc->sc_iface, &dev);
    562       1.1   thorpej 	id = usbd_get_interface_descriptor(sc->sc_iface);
    563       1.1   thorpej 
    564       1.1   thorpej 	/* the reset command is a class specific interface request */
    565       1.1   thorpej 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    566       1.1   thorpej 	req.bRequest = UR_RESET;
    567       1.1   thorpej 	USETW(req.wValue, 0);
    568       1.1   thorpej 	USETW(req.wIndex, id->bInterfaceNumber);
    569       1.1   thorpej 	USETW(req.wLength, 0);
    570       1.1   thorpej 
    571       1.1   thorpej 	err = usbd_do_request(dev, &req, 0);
    572       1.1   thorpej 	if (err) {
    573       1.1   thorpej 		printf("%s: Reset failed, %s\n",
    574       1.1   thorpej 			USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    575       1.1   thorpej 		/* XXX we should port_reset the device */
    576       1.1   thorpej 		return(err);
    577       1.1   thorpej 	}
    578       1.1   thorpej 
    579       1.1   thorpej 	usbd_clear_endpoint_stall(sc->sc_bulkout_pipe);
    580       1.1   thorpej 	usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
    581       1.1   thorpej 
    582      1.18  augustss #if 0
    583       1.1   thorpej 	/*
    584       1.1   thorpej 	 * XXX we should convert this into a more friendly delay.
    585       1.1   thorpej 	 * Perhaps a tsleep (or is this routine run from int context?)
    586       1.1   thorpej 	 */
    587       1.1   thorpej 
    588       1.1   thorpej 	DELAY(2500000 /*us*/);
    589      1.18  augustss #else
    590      1.18  augustss 	usbd_delay_ms(dev, 2500);
    591      1.18  augustss #endif
    592       1.1   thorpej 
    593       1.1   thorpej 	return(USBD_NORMAL_COMPLETION);
    594       1.1   thorpej }
    595       1.1   thorpej 
    596       1.1   thorpej /*
    597       1.1   thorpej  * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly
    598       1.1   thorpej  * a data phase of datalen bytes from/to data and finally a csw read
    599       1.1   thorpej  * phase.
    600       1.1   thorpej  *
    601       1.1   thorpej  * If the data direction was inbound a maximum of datalen bytes
    602       1.1   thorpej  * is stored in the buffer pointed to by data.
    603       1.1   thorpej  * The status returned is USBD_NORMAL_COMPLETION,
    604       1.1   thorpej  * USBD_IOERROR, USBD_COMMAND_FAILED.
    605       1.1   thorpej  * In the last case *residue is set to the residue from the CSW,
    606       1.1   thorpej  * otherwise to 0.
    607       1.1   thorpej  *
    608       1.1   thorpej  * For the functionality of this subroutine see the Mass Storage
    609       1.1   thorpej  * Spec., the graphs on page 14 and page 19 and beyong (v0.9 of
    610       1.1   thorpej  * the spec).
    611       1.1   thorpej  */
    612       1.1   thorpej 
    613       1.1   thorpej usbd_status
    614       1.1   thorpej umass_bulk_transfer(umass_softc_t *sc, int lun, void *cmd, int cmdlen,
    615       1.1   thorpej 		    void *data, int datalen, int dir, int *residue)
    616       1.1   thorpej {
    617       1.1   thorpej 	static int dCBWtag = 42;	/* tag to be used in transfers,
    618       1.1   thorpej 					 * incremented at each transfer */
    619       1.1   thorpej 	usb_bulk_cbw_t cbw;		/* command block wrapper struct */
    620       1.1   thorpej 	usb_bulk_csw_t csw;		/* command status wrapper struct */
    621       1.1   thorpej 	u_int32_t n = 0;		/* number of bytes transported */
    622       1.1   thorpej 	usbd_status err;
    623       1.1   thorpej 
    624       1.1   thorpej #ifdef UMASS_DEBUG
    625       1.1   thorpej 	u_int8_t *c = cmd;
    626       1.1   thorpej 
    627       1.1   thorpej 	/* check the given arguments */
    628       1.1   thorpej 	if (!data && datalen > 0) {	/* no buffer for transfer */
    629       1.1   thorpej 		DPRINTF(UDMASS_BULK, ("%s: no buffer, but datalen > 0 !\n",
    630       1.1   thorpej 			USBDEVNAME(sc->sc_dev)));
    631       1.1   thorpej 		return USBD_IOERROR;
    632       1.1   thorpej 	}
    633       1.1   thorpej 
    634       1.1   thorpej 	DPRINTF(UDMASS_BULK, ("%s: cmd: %d bytes (0x%02x%02x%02x%02x%02x%02x%s)"
    635       1.1   thorpej 		", data: %d bytes, dir: %s\n",
    636       1.1   thorpej 		USBDEVNAME(sc->sc_dev),
    637       1.1   thorpej 		cmdlen, c[0], c[1], c[2], c[3], c[4], c[5],
    638       1.1   thorpej 		(cmdlen > 6? "...":""),
    639       1.1   thorpej 		datalen, (dir == DIR_IN? "in":"out")));
    640       1.1   thorpej #endif
    641       1.1   thorpej 
    642       1.1   thorpej 	if (dir == DIR_NONE || datalen == 0) {		/* make sure they correspond */
    643       1.1   thorpej 		datalen = 0;
    644       1.1   thorpej 		dir = DIR_NONE;
    645       1.1   thorpej 	}
    646       1.1   thorpej 
    647      1.15   thorpej 	if (residue != NULL)
    648      1.15   thorpej 		*residue = 0;			/* reset residue */
    649       1.1   thorpej 
    650       1.1   thorpej 	/*
    651       1.1   thorpej 	 * Determine the direction of transferring data and data length.
    652       1.1   thorpej 	 *
    653       1.1   thorpej 	 * dCBWDataTransferLength (datalen) :
    654       1.1   thorpej 	 *   This field indicates the number of bytes of data that the host
    655       1.1   thorpej 	 *   intends to transfer on the IN or OUT Bulk endpoint(as indicated by
    656       1.1   thorpej 	 *   the Direction bit) during the execution of this command. If this
    657       1.1   thorpej 	 *   field is set to 0, the device will expect that no data will be
    658       1.1   thorpej 	 *   transferred IN or OUT during this command, regardless of the value
    659       1.1   thorpej 	 *   of the Direction bit defined in dCBWFlags.
    660       1.1   thorpej 	 *
    661       1.1   thorpej 	 * dCBWFlags (dir) :
    662       1.1   thorpej 	 *   The bits of the Flags field are defined as follows:
    663       1.1   thorpej 	 *     Bits 0-6  reserved
    664       1.1   thorpej 	 *     Bit  7    Direction - this bit shall be ignored if the
    665       1.1   thorpej 	 *                           dCBWDataTransferLength field is zero.
    666       1.1   thorpej 	 *               0 = data Out from host to device
    667       1.1   thorpej 	 *               1 = data In from device to host
    668       1.1   thorpej 	 */
    669       1.1   thorpej 
    670       1.1   thorpej 
    671       1.1   thorpej 	/*
    672       1.1   thorpej 	 * Command transport phase
    673       1.1   thorpej 	 */
    674       1.1   thorpej 
    675       1.1   thorpej 	/* Fill in the Command Block Wrapper */
    676       1.1   thorpej 	USETDW(cbw.dCBWSignature, CBWSIGNATURE);
    677       1.1   thorpej 	USETDW(cbw.dCBWTag, dCBWtag++);
    678       1.1   thorpej 	USETDW(cbw.dCBWDataTransferLength, datalen);
    679       1.1   thorpej 	/* we do not check for DIR_NONE below (see text on dCBWFlags above) */
    680       1.1   thorpej 	cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT);
    681       1.1   thorpej 	cbw.bCBWLUN = lun;
    682       1.1   thorpej 	cbw.bCDBLength = cmdlen;
    683       1.1   thorpej 	bcopy(cmd, cbw.CBWCDB, cmdlen);
    684       1.1   thorpej 
    685       1.1   thorpej 	/* Send the CBW from host to device via bulk-out endpoint. */
    686      1.15   thorpej 	err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
    687       1.1   thorpej 				&cbw, USB_BULK_CBW_SIZE, 0, NULL);
    688       1.1   thorpej 	if (err) {
    689       1.1   thorpej 		DPRINTF(UDMASS_BULK, ("%s: failed to send CBW\n",
    690       1.1   thorpej 		         USBDEVNAME(sc->sc_dev)));
    691       1.1   thorpej 		/* If the device detects that the CBW is invalid, then the
    692       1.1   thorpej 		 * device may STALL both bulk endpoints and require a
    693       1.1   thorpej 		 * Bulk-Only MS Reset
    694       1.1   thorpej 		 */
    695      1.18  augustss 		if (!sc->sc_dying)
    696      1.18  augustss 			umass_bulk_reset(sc);
    697       1.1   thorpej 		return(USBD_IOERROR);
    698       1.1   thorpej 	}
    699       1.1   thorpej 
    700       1.1   thorpej 
    701       1.1   thorpej 	/*
    702       1.1   thorpej 	 * Data transport phase (only if there is data to be sent/received)
    703       1.1   thorpej 	 */
    704       1.1   thorpej 
    705       1.1   thorpej 	if (dir == DIR_IN) {
    706       1.1   thorpej 		/* we allow short transfers for bulk-in pipes */
    707      1.15   thorpej 		err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
    708       1.1   thorpej 					data, datalen,
    709       1.1   thorpej 					USBD_SHORT_XFER_OK, &n);
    710       1.1   thorpej 		if (err)
    711       1.1   thorpej 			DPRINTF(UDMASS_BULK, ("%s: failed to receive data, "
    712       1.1   thorpej 				"(%d bytes, n = %d), %s\n",
    713       1.1   thorpej 				USBDEVNAME(sc->sc_dev),
    714       1.1   thorpej 				datalen, n, usbd_errstr(err)));
    715       1.1   thorpej 	} else if (dir == DIR_OUT) {
    716      1.15   thorpej 		err = umass_usb_transfer(sc, sc->sc_bulkout_pipe,
    717       1.1   thorpej 					data, datalen, 0, &n);
    718       1.1   thorpej 		if (err)
    719       1.1   thorpej 			DPRINTF(UDMASS_BULK, ("%s: failed to send data, "
    720       1.1   thorpej 				"(%d bytes, n = %d), %s\n",
    721       1.1   thorpej 				USBDEVNAME(sc->sc_dev),
    722       1.1   thorpej 				datalen, n, usbd_errstr(err)));
    723       1.1   thorpej 	}
    724       1.1   thorpej 	if (err && err != USBD_STALLED)
    725       1.1   thorpej 		return(USBD_IOERROR);
    726       1.1   thorpej 
    727       1.1   thorpej 
    728       1.1   thorpej 	/*
    729       1.1   thorpej 	 * Status transport phase
    730       1.1   thorpej 	 */
    731       1.1   thorpej 
    732       1.1   thorpej 	/* Read the Command Status Wrapper via bulk-in endpoint. */
    733      1.15   thorpej 	err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
    734       1.1   thorpej 				&csw, USB_BULK_CSW_SIZE, 0, NULL);
    735       1.1   thorpej 	/* Try again if the bulk-in pipe was stalled */
    736       1.1   thorpej 	if (err == USBD_STALLED) {
    737       1.1   thorpej 		err = usbd_clear_endpoint_stall(sc->sc_bulkin_pipe);
    738       1.1   thorpej 		if (!err) {
    739      1.15   thorpej 			err = umass_usb_transfer(sc, sc->sc_bulkin_pipe,
    740      1.15   thorpej 						&csw, USB_BULK_CSW_SIZE, 0,
    741      1.15   thorpej 						NULL);
    742       1.1   thorpej 		}
    743       1.1   thorpej 	}
    744       1.1   thorpej 	if (err && err != USBD_STALLED)
    745       1.1   thorpej 		return(USBD_IOERROR);
    746       1.1   thorpej 
    747       1.1   thorpej 	/*
    748       1.1   thorpej 	 * Check the CSW for status and validity, and check for fatal errors
    749       1.1   thorpej 	 */
    750       1.1   thorpej 
    751       1.1   thorpej 	/* Invalid CSW: Wrong signature or wrong tag might indicate
    752       1.1   thorpej 	 * that the device is confused -> reset it.
    753       1.1   thorpej 	 * Other fatal errors: STALL on read of CSW and Phase error
    754       1.1   thorpej 	 * or unknown status.
    755       1.1   thorpej 	 */
    756       1.1   thorpej 	if (err == USBD_STALLED
    757       1.1   thorpej 	    || UGETDW(csw.dCSWSignature) != CSWSIGNATURE
    758       1.1   thorpej 	    || UGETDW(csw.dCSWTag) != UGETDW(cbw.dCBWTag)
    759       1.1   thorpej 	    || csw.bCSWStatus == CSWSTATUS_PHASE
    760       1.1   thorpej 	    || csw.bCSWStatus > CSWSTATUS_PHASE) {
    761       1.1   thorpej 		if (err) {
    762       1.1   thorpej 			printf("%s: failed to read CSW, %s\n",
    763       1.1   thorpej 			       USBDEVNAME(sc->sc_dev), usbd_errstr(err));
    764       1.1   thorpej 		} else if (csw.bCSWStatus == CSWSTATUS_PHASE) {
    765       1.1   thorpej 			printf("%s: Phase Error, residue = %d, n = %d\n",
    766       1.1   thorpej 				USBDEVNAME(sc->sc_dev),
    767       1.1   thorpej 				UGETDW(csw.dCSWDataResidue), n);
    768       1.1   thorpej 		} else if (csw.bCSWStatus > CSWSTATUS_PHASE) {
    769       1.1   thorpej 			printf("%s: Unknown status %d in CSW\n",
    770       1.1   thorpej 				USBDEVNAME(sc->sc_dev), csw.bCSWStatus);
    771       1.1   thorpej 		} else {
    772       1.1   thorpej 			printf("%s: invalid CSW, sig = 0x%08x, tag = %d (!= %d)\n",
    773       1.1   thorpej 				USBDEVNAME(sc->sc_dev),
    774       1.1   thorpej 				UGETDW(csw.dCSWSignature),
    775       1.1   thorpej 				UGETDW(csw.dCSWTag), UGETDW(cbw.dCBWTag));
    776       1.1   thorpej 		}
    777       1.1   thorpej 		umass_bulk_reset(sc);
    778       1.1   thorpej 		return(USBD_IOERROR);
    779       1.1   thorpej 	}
    780       1.1   thorpej 
    781       1.1   thorpej 	if (csw.bCSWStatus == CSWSTATUS_FAILED) {
    782       1.1   thorpej 		DPRINTF(UDMASS_BULK, ("%s: Command Failed, "
    783       1.1   thorpej 			"residue = %d, n = %d\n",
    784       1.1   thorpej 			USBDEVNAME(sc->sc_dev),
    785       1.1   thorpej 			UGETDW(csw.dCSWDataResidue), n));
    786      1.15   thorpej 		if (residue != NULL)
    787      1.15   thorpej 			*residue = UGETDW(csw.dCSWDataResidue);
    788       1.1   thorpej 		return(USBD_COMMAND_FAILED);
    789       1.1   thorpej 	}
    790       1.1   thorpej 
    791       1.1   thorpej 	/*
    792       1.1   thorpej 	 * XXX a residue not equal to 0 might indicate that something
    793       1.1   thorpej 	 * is wrong. Does CAM high level drivers check this for us?
    794       1.1   thorpej 	 */
    795       1.1   thorpej 
    796       1.1   thorpej 	return(USBD_NORMAL_COMPLETION);
    797       1.1   thorpej }
    798       1.1   thorpej 
    799       1.1   thorpej 
    800       1.1   thorpej /*
    801       1.1   thorpej  * SCSIPI specific functions
    802       1.1   thorpej  */
    803       1.1   thorpej 
    804  1.21.2.1   thorpej void
    805  1.21.2.1   thorpej umass_scsipi_request(chan, req, arg)
    806  1.21.2.1   thorpej 	struct scsipi_channel *chan;
    807  1.21.2.1   thorpej 	scsipi_adapter_req_t req;
    808  1.21.2.1   thorpej 	void *arg;
    809       1.1   thorpej {
    810  1.21.2.1   thorpej 	struct scsipi_xfer *xs;
    811  1.21.2.1   thorpej 	struct scsipi_periph *periph;
    812  1.21.2.1   thorpej 	struct umass_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    813       1.1   thorpej 	int residue, dir;
    814       1.1   thorpej 	usbd_status err;
    815      1.15   thorpej 	struct scsipi_sense sense_cmd;
    816       1.1   thorpej 
    817  1.21.2.1   thorpej 	switch (req) {
    818  1.21.2.1   thorpej 	case ADAPTER_REQ_RUN_XFER:
    819  1.21.2.1   thorpej 		xs = arg;
    820  1.21.2.1   thorpej 		periph = xs->xs_periph;
    821      1.15   thorpej 
    822  1.21.2.1   thorpej 		DPRINTF(UDMASS_SCSI, ("%s: umass_scsipi_request %d:%d\n",
    823       1.1   thorpej 		    USBDEVNAME(sc->sc_dev),
    824  1.21.2.1   thorpej 		    periph->periph_target, periph->periph_lun));
    825       1.1   thorpej 
    826  1.21.2.1   thorpej 		if (sc->sc_dying) {
    827  1.21.2.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    828  1.21.2.1   thorpej 			scsipi_done(xs);
    829  1.21.2.1   thorpej 			return;
    830       1.1   thorpej 		}
    831       1.1   thorpej 
    832  1.21.2.1   thorpej #ifdef UMASS_DEBUG
    833  1.21.2.1   thorpej 		if (periph->periph_target != UMASS_SCSIID_DEVICE) {
    834  1.21.2.1   thorpej 			DPRINTF(UDMASS_SCSI, ("%s: Wrong SCSI ID %d\n",
    835  1.21.2.1   thorpej 			    USBDEVNAME(sc->sc_dev),
    836  1.21.2.1   thorpej 			    periph->periph_target));
    837  1.21.2.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    838  1.21.2.1   thorpej 			scsipi_done(xs);
    839  1.21.2.1   thorpej 		}
    840  1.21.2.1   thorpej #endif
    841  1.21.2.1   thorpej 
    842  1.21.2.1   thorpej 		dir = DIR_NONE;
    843  1.21.2.1   thorpej 		if (xs->datalen) {
    844  1.21.2.1   thorpej 			switch (xs->xs_control &
    845  1.21.2.1   thorpej 			    (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)) {
    846  1.21.2.1   thorpej 			case XS_CTL_DATA_IN:
    847  1.21.2.1   thorpej 				dir = DIR_IN;
    848  1.21.2.1   thorpej 				break;
    849  1.21.2.1   thorpej 			case XS_CTL_DATA_OUT:
    850  1.21.2.1   thorpej 				dir = DIR_OUT;
    851  1.21.2.1   thorpej 				break;
    852  1.21.2.1   thorpej 			}
    853  1.21.2.1   thorpej 		}
    854      1.18  augustss 
    855  1.21.2.1   thorpej 		/* Make sure we don't lose our softc. */
    856  1.21.2.1   thorpej 		sc->sc_refcnt++;
    857       1.1   thorpej 
    858  1.21.2.1   thorpej 		err = umass_bulk_transfer(sc, periph->periph_lun,
    859  1.21.2.1   thorpej 		    xs->cmd, xs->cmdlen, xs->data, xs->datalen, dir, &residue);
    860      1.15   thorpej 
    861      1.15   thorpej 		/*
    862  1.21.2.1   thorpej 		 * FAILED commands are supposed to be SCSI failed commands
    863  1.21.2.1   thorpej 		 * and are therefore considered to be successfull CDW/CSW
    864  1.21.2.1   thorpej 		 * transfers.  PHASE errors are more serious and should return
    865  1.21.2.1   thorpej 		 * an error to the SCSIPI system.
    866  1.21.2.1   thorpej 		 *
    867  1.21.2.1   thorpej 		 * XXX This is however more based on empirical evidence than on
    868  1.21.2.1   thorpej 		 * hard proof from the Bulk-Only spec.
    869      1.15   thorpej 		 */
    870  1.21.2.1   thorpej 		if (err == USBD_NORMAL_COMPLETION) {
    871  1.21.2.1   thorpej 			xs->error = XS_NOERROR;
    872  1.21.2.1   thorpej 		} else if (sc->sc_dying) {
    873  1.21.2.1   thorpej 			/*
    874  1.21.2.1   thorpej 			 * We are being detached, no use talking to the
    875  1.21.2.1   thorpej 			 * device.
    876  1.21.2.1   thorpej 			 */
    877  1.21.2.1   thorpej 			xs->error = XS_DRIVER_STUFFUP;
    878  1.21.2.1   thorpej 		} else {
    879  1.21.2.1   thorpej 			DPRINTF(UDMASS_USB|UDMASS_SCSI,
    880  1.21.2.1   thorpej 			    ("%s: bulk transfer completed "
    881  1.21.2.1   thorpej 			    "with error %s\n", USBDEVNAME(sc->sc_dev),
    882  1.21.2.1   thorpej 			    usbd_errstr(err)));
    883  1.21.2.1   thorpej 
    884  1.21.2.1   thorpej 			/*
    885  1.21.2.1   thorpej 			 * Probably have a CHECK CONDITION here.  Issue a
    886  1.21.2.1   thorpej 			 * REQUEST SENSE.
    887  1.21.2.1   thorpej 			 */
    888  1.21.2.1   thorpej 			memset(&sense_cmd, 0, sizeof(sense_cmd));
    889  1.21.2.1   thorpej 			sense_cmd.opcode = REQUEST_SENSE;
    890  1.21.2.1   thorpej 			sense_cmd.byte2 = periph->periph_lun <<
    891  1.21.2.1   thorpej 			    SCSI_CMD_LUN_SHIFT;
    892  1.21.2.1   thorpej 			sense_cmd.length = sizeof(xs->sense);
    893  1.21.2.1   thorpej 
    894  1.21.2.1   thorpej 			if ((err = umass_bulk_transfer(sc,
    895  1.21.2.1   thorpej 			    periph->periph_lun,
    896  1.21.2.1   thorpej 			    (struct scsipi_generic *)&sense_cmd,
    897  1.21.2.1   thorpej 			    sizeof(sense_cmd), &xs->sense, sizeof(xs->sense),
    898  1.21.2.1   thorpej 			    DIR_IN, NULL)) != USBD_NORMAL_COMPLETION) {
    899  1.21.2.1   thorpej 				DPRINTF(UDMASS_SCSI,
    900  1.21.2.1   thorpej 				    ("%s: REQUEST SENSE failed: %s\n",
    901  1.21.2.1   thorpej 				    USBDEVNAME(sc->sc_dev), usbd_errstr(err)));
    902  1.21.2.1   thorpej 				xs->error = XS_DRIVER_STUFFUP;	/* XXX */
    903  1.21.2.1   thorpej 			} else
    904  1.21.2.1   thorpej 				xs->error = XS_SENSE;
    905  1.21.2.1   thorpej 		}
    906  1.21.2.1   thorpej 		xs->resid = residue;
    907       1.1   thorpej 
    908  1.21.2.1   thorpej 		DPRINTF(UDMASS_SCSI,
    909  1.21.2.1   thorpej 		    ("%s: umass_scsi_cmd: error = %d, resid = 0x%x\n",
    910  1.21.2.1   thorpej 		    USBDEVNAME(sc->sc_dev), xs->error, xs->resid));
    911  1.21.2.1   thorpej 
    912  1.21.2.1   thorpej 		scsipi_done(xs);
    913  1.21.2.1   thorpej 
    914  1.21.2.1   thorpej 		/* We are done with the softc for now. */
    915  1.21.2.1   thorpej 		if (--sc->sc_refcnt < 0)
    916  1.21.2.1   thorpej 			usb_detach_wakeup(USBDEV(sc->sc_dev));
    917       1.1   thorpej 
    918  1.21.2.1   thorpej 		return;
    919  1.21.2.1   thorpej 
    920  1.21.2.1   thorpej 	case ADAPTER_REQ_GROW_RESOURCES:
    921  1.21.2.1   thorpej 		/*
    922  1.21.2.1   thorpej 		 * We're only allowed one command, so don't do this.
    923  1.21.2.1   thorpej 		 */
    924  1.21.2.1   thorpej 		return;
    925  1.21.2.1   thorpej 
    926  1.21.2.1   thorpej 	case ADAPTER_REQ_SET_XFER_MODE:
    927  1.21.2.1   thorpej 		/*
    928  1.21.2.1   thorpej 		 * Nothing in the proctol for this.
    929  1.21.2.1   thorpej 		 */
    930  1.21.2.1   thorpej 		return;
    931  1.21.2.1   thorpej 	}
    932       1.1   thorpej }
    933       1.1   thorpej 
    934       1.1   thorpej void
    935       1.9   thorpej umass_scsipi_minphys(bp)
    936       1.1   thorpej 	struct buf *bp;
    937       1.1   thorpej {
    938       1.1   thorpej 
    939       1.1   thorpej 	/* No limit here. */
    940       1.1   thorpej 	minphys(bp);
    941       1.1   thorpej }
    942