Home | History | Annotate | Line # | Download | only in usb
usscanner.c revision 1.16
      1 /*	$NetBSD: usscanner.c,v 1.16 2005/04/29 17:52:46 augustss Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) and LLoyd Parkes.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This driver is partly based on information taken from the Linux driver
     41  * by John Fremlin, Oliver Neukum, and Jeremy Hall.
     42  */
     43 /*
     44  * Protocol:
     45  * Send raw SCSI command on the bulk-out pipe.
     46  * If output command then
     47  *     send further data on the bulk-out pipe
     48  * else if input command then
     49  *     read data on the bulk-in pipe
     50  * else
     51  *     don't do anything.
     52  * Read status byte on the interrupt pipe (which doesn't seem to be
     53  * an interrupt pipe at all).  This operation sometimes times out.
     54  */
     55 
     56 #include <sys/cdefs.h>
     57 __KERNEL_RCSID(0, "$NetBSD: usscanner.c,v 1.16 2005/04/29 17:52:46 augustss Exp $");
     58 
     59 #include "scsibus.h"
     60 #include <sys/param.h>
     61 #include <sys/systm.h>
     62 #include <sys/kernel.h>
     63 #include <sys/malloc.h>
     64 #include <sys/device.h>
     65 #include <sys/conf.h>
     66 #include <sys/buf.h>
     67 
     68 #include <dev/usb/usb.h>
     69 #include <dev/usb/usbdi.h>
     70 #include <dev/usb/usbdi_util.h>
     71 
     72 #include <dev/usb/usbdevs.h>
     73 
     74 #include <sys/scsiio.h>
     75 #include <dev/scsipi/scsi_spc.h>
     76 #include <dev/scsipi/scsi_all.h>
     77 #include <dev/scsipi/scsipi_all.h>
     78 #include <dev/scsipi/scsiconf.h>
     79 #include <dev/scsipi/atapiconf.h>
     80 
     81 #ifdef USSCANNER_DEBUG
     82 #define DPRINTF(x)	if (usscannerdebug) logprintf x
     83 #define DPRINTFN(n,x)	if (usscannerdebug>(n)) logprintf x
     84 int	usscannerdebug = 0;
     85 #else
     86 #define DPRINTF(x)
     87 #define DPRINTFN(n,x)
     88 #endif
     89 
     90 
     91 #define USSCANNER_CONFIG_NO		1
     92 #define USSCANNER_IFACE_IDX		0
     93 
     94 #define USSCANNER_SCSIID_HOST	0x00
     95 #define USSCANNER_SCSIID_DEVICE	0x01
     96 
     97 #define USSCANNER_MAX_TRANSFER_SIZE	MAXPHYS
     98 
     99 #define USSCANNER_TIMEOUT 2000
    100 
    101 struct usscanner_softc {
    102  	USBBASEDEVICE		sc_dev;
    103 	usbd_device_handle	sc_udev;
    104 	usbd_interface_handle	sc_iface;
    105 
    106 	int			sc_in_addr;
    107 	usbd_pipe_handle	sc_in_pipe;
    108 
    109 	int			sc_intr_addr;
    110 	usbd_pipe_handle	sc_intr_pipe;
    111 	usbd_xfer_handle	sc_intr_xfer;
    112 	u_char			sc_status;
    113 
    114 	int			sc_out_addr;
    115 	usbd_pipe_handle	sc_out_pipe;
    116 
    117 	usbd_xfer_handle	sc_cmd_xfer;
    118 	void			*sc_cmd_buffer;
    119 	usbd_xfer_handle	sc_data_xfer;
    120 	void			*sc_data_buffer;
    121 
    122 	int			sc_state;
    123 #define UAS_IDLE	0
    124 #define UAS_CMD		1
    125 #define UAS_DATA	2
    126 #define UAS_SENSECMD	3
    127 #define UAS_SENSEDATA	4
    128 #define UAS_STATUS	5
    129 
    130 	struct scsipi_xfer	*sc_xs;
    131 
    132 	device_ptr_t		sc_child;	/* child device, for detach */
    133 
    134 	struct scsipi_adapter	sc_adapter;
    135 	struct scsipi_channel	sc_channel;
    136 
    137 	int			sc_refcnt;
    138 	char			sc_dying;
    139 };
    140 
    141 
    142 Static void usscanner_cleanup(struct usscanner_softc *sc);
    143 Static void usscanner_scsipi_request(struct scsipi_channel *chan,
    144 				scsipi_adapter_req_t req, void *arg);
    145 Static void usscanner_scsipi_minphys(struct buf *bp);
    146 Static void usscanner_done(struct usscanner_softc *sc);
    147 Static void usscanner_sense(struct usscanner_softc *sc);
    148 typedef void callback(usbd_xfer_handle, usbd_private_handle, usbd_status);
    149 Static callback usscanner_intr_cb;
    150 Static callback usscanner_cmd_cb;
    151 Static callback usscanner_data_cb;
    152 Static callback usscanner_sensecmd_cb;
    153 Static callback usscanner_sensedata_cb;
    154 
    155 USB_DECLARE_DRIVER(usscanner);
    156 
    157 USB_MATCH(usscanner)
    158 {
    159 	USB_MATCH_START(usscanner, uaa);
    160 
    161 	DPRINTFN(50,("usscanner_match\n"));
    162 
    163 	if (uaa->iface != NULL)
    164 		return (UMATCH_NONE);
    165 
    166 	if (uaa->vendor == USB_VENDOR_HP &&
    167 	    uaa->product == USB_PRODUCT_HP_5300C)
    168 		return (UMATCH_VENDOR_PRODUCT);
    169 	else
    170 		return (UMATCH_NONE);
    171 }
    172 
    173 USB_ATTACH(usscanner)
    174 {
    175 	USB_ATTACH_START(usscanner, sc, uaa);
    176 	usbd_device_handle	dev = uaa->device;
    177 	usbd_interface_handle	iface;
    178 	char			devinfo[1024];
    179 	usbd_status		err;
    180 	usb_endpoint_descriptor_t *ed;
    181 	u_int8_t		epcount;
    182 	int			i;
    183 
    184 	DPRINTFN(10,("usscanner_attach: sc=%p\n", sc));
    185 
    186 	usbd_devinfo(dev, 0, devinfo, sizeof(devinfo));
    187 	USB_ATTACH_SETUP;
    188 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
    189 
    190 	err = usbd_set_config_no(dev, USSCANNER_CONFIG_NO, 1);
    191 	if (err) {
    192 		printf("%s: setting config no failed\n",
    193 		    USBDEVNAME(sc->sc_dev));
    194 		USB_ATTACH_ERROR_RETURN;
    195 	}
    196 
    197 	err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface);
    198 	if (err) {
    199 		printf("%s: getting interface handle failed\n",
    200 		    USBDEVNAME(sc->sc_dev));
    201 		USB_ATTACH_ERROR_RETURN;
    202 	}
    203 
    204 	sc->sc_udev = dev;
    205 	sc->sc_iface = iface;
    206 
    207 	epcount = 0;
    208 	(void)usbd_endpoint_count(iface, &epcount);
    209 
    210 	sc->sc_in_addr = -1;
    211 	sc->sc_intr_addr = -1;
    212 	sc->sc_out_addr = -1;
    213 	for (i = 0; i < epcount; i++) {
    214 		ed = usbd_interface2endpoint_descriptor(iface, i);
    215 		if (ed == NULL) {
    216 			printf("%s: couldn't get ep %d\n",
    217 			    USBDEVNAME(sc->sc_dev), i);
    218 			USB_ATTACH_ERROR_RETURN;
    219 		}
    220 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    221 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    222 			sc->sc_in_addr = ed->bEndpointAddress;
    223 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    224 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    225 			sc->sc_intr_addr = ed->bEndpointAddress;
    226 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    227 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    228 			sc->sc_out_addr = ed->bEndpointAddress;
    229 		}
    230 	}
    231 	if (sc->sc_in_addr == -1 || sc->sc_intr_addr == -1 ||
    232 	    sc->sc_out_addr == -1) {
    233 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    234 		USB_ATTACH_ERROR_RETURN;
    235 	}
    236 
    237 	err = usbd_open_pipe(sc->sc_iface, sc->sc_in_addr,
    238 			     USBD_EXCLUSIVE_USE, &sc->sc_in_pipe);
    239 	if (err) {
    240 		printf("%s: open in pipe failed, err=%d\n",
    241 		       USBDEVNAME(sc->sc_dev), err);
    242 		USB_ATTACH_ERROR_RETURN;
    243 	}
    244 
    245 	/* The interrupt endpoint must be opened as a normal pipe. */
    246 	err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr,
    247 			     USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe);
    248 
    249 	if (err) {
    250 		printf("%s: open intr pipe failed, err=%d\n",
    251 		       USBDEVNAME(sc->sc_dev), err);
    252 		usscanner_cleanup(sc);
    253 		USB_ATTACH_ERROR_RETURN;
    254 	}
    255 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr,
    256 			     USBD_EXCLUSIVE_USE, &sc->sc_out_pipe);
    257 	if (err) {
    258 		printf("%s: open out pipe failed, err=%d\n",
    259 		       USBDEVNAME(sc->sc_dev), err);
    260 		usscanner_cleanup(sc);
    261 		USB_ATTACH_ERROR_RETURN;
    262 	}
    263 
    264 	sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device);
    265 	if (sc->sc_cmd_xfer == NULL) {
    266 		printf("%s: alloc cmd xfer failed, err=%d\n",
    267 		       USBDEVNAME(sc->sc_dev), err);
    268 		usscanner_cleanup(sc);
    269 		USB_ATTACH_ERROR_RETURN;
    270 	}
    271 
    272 	/* XXX too big */
    273 	sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer,
    274 					     USSCANNER_MAX_TRANSFER_SIZE);
    275 	if (sc->sc_cmd_buffer == NULL) {
    276 		printf("%s: alloc cmd buffer failed, err=%d\n",
    277 		       USBDEVNAME(sc->sc_dev), err);
    278 		usscanner_cleanup(sc);
    279 		USB_ATTACH_ERROR_RETURN;
    280 	}
    281 
    282 	sc->sc_intr_xfer = usbd_alloc_xfer (uaa->device);
    283 	if (sc->sc_intr_xfer == NULL) {
    284 	  printf("%s: alloc intr xfer failed, err=%d\n",
    285 		 USBDEVNAME(sc->sc_dev), err);
    286 	  usscanner_cleanup(sc);
    287 	  USB_ATTACH_ERROR_RETURN;
    288         }
    289 
    290 	sc->sc_data_xfer = usbd_alloc_xfer(uaa->device);
    291 	if (sc->sc_data_xfer == NULL) {
    292 		printf("%s: alloc data xfer failed, err=%d\n",
    293 		       USBDEVNAME(sc->sc_dev), err);
    294 		usscanner_cleanup(sc);
    295 		USB_ATTACH_ERROR_RETURN;
    296 	}
    297 	sc->sc_data_buffer = usbd_alloc_buffer(sc->sc_data_xfer,
    298 					      USSCANNER_MAX_TRANSFER_SIZE);
    299 	if (sc->sc_data_buffer == NULL) {
    300 		printf("%s: alloc data buffer failed, err=%d\n",
    301 		       USBDEVNAME(sc->sc_dev), err);
    302 		usscanner_cleanup(sc);
    303 		USB_ATTACH_ERROR_RETURN;
    304 	}
    305 
    306 	/*
    307 	 * Fill in the adapter.
    308 	 */
    309 	sc->sc_adapter.adapt_request = usscanner_scsipi_request;
    310 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    311 	sc->sc_adapter.adapt_nchannels = 1;
    312 	sc->sc_adapter.adapt_openings = 1;
    313 	sc->sc_adapter.adapt_max_periph = 1;
    314 	sc->sc_adapter.adapt_minphys = usscanner_scsipi_minphys;
    315 
    316 #if NSCSIBUS > 0
    317 	/*
    318 	 * fill in the scsipi_channel.
    319 	 */
    320 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    321 	sc->sc_channel.chan_bustype = &scsi_bustype;
    322 	sc->sc_channel.chan_channel = 0;
    323 	sc->sc_channel.chan_ntargets = USSCANNER_SCSIID_DEVICE + 1;
    324 	sc->sc_channel.chan_nluns = 1;
    325 	sc->sc_channel.chan_id = USSCANNER_SCSIID_HOST;
    326 
    327 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    328 			   USBDEV(sc->sc_dev));
    329 
    330 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    331 
    332 	DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev));
    333 
    334 	USB_ATTACH_SUCCESS_RETURN;
    335 
    336 #else
    337 	/* No SCSI bus, just ignore it */
    338 	usscanner_cleanup(sc);
    339 	USB_ATTACH_ERROR_RETURN;
    340 
    341 #endif
    342 }
    343 
    344 USB_DETACH(usscanner)
    345 {
    346 	USB_DETACH_START(usscanner, sc);
    347 	int rv, s;
    348 
    349 	DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
    350 
    351 	sc->sc_dying = 1;
    352 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    353 	if (sc->sc_in_pipe != NULL)
    354 		usbd_abort_pipe(sc->sc_in_pipe);
    355 	if (sc->sc_intr_pipe != NULL)
    356 		usbd_abort_pipe(sc->sc_intr_pipe);
    357 	if (sc->sc_out_pipe != NULL)
    358 		usbd_abort_pipe(sc->sc_out_pipe);
    359 
    360 	s = splusb();
    361 	if (--sc->sc_refcnt >= 0) {
    362 		/* Wait for processes to go away. */
    363 		usb_detach_wait(USBDEV(sc->sc_dev));
    364 	}
    365 	splx(s);
    366 
    367 	if (sc->sc_child != NULL)
    368 		rv = config_detach(sc->sc_child, flags);
    369 	else
    370 		rv = 0;
    371 
    372 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    373 			   USBDEV(sc->sc_dev));
    374 
    375 	return (rv);
    376 }
    377 
    378 Static void
    379 usscanner_cleanup(struct usscanner_softc *sc)
    380 {
    381 	if (sc->sc_in_pipe != NULL) {
    382 		usbd_close_pipe(sc->sc_in_pipe);
    383 		sc->sc_in_pipe = NULL;
    384 	}
    385 	if (sc->sc_intr_pipe != NULL) {
    386 		usbd_close_pipe(sc->sc_intr_pipe);
    387 		sc->sc_intr_pipe = NULL;
    388 	}
    389 	if (sc->sc_out_pipe != NULL) {
    390 		usbd_close_pipe(sc->sc_out_pipe);
    391 		sc->sc_out_pipe = NULL;
    392 	}
    393 	if (sc->sc_cmd_xfer != NULL) {
    394 		usbd_free_xfer(sc->sc_cmd_xfer);
    395 		sc->sc_cmd_xfer = NULL;
    396 	}
    397 	if (sc->sc_data_xfer != NULL) {
    398 		usbd_free_xfer(sc->sc_data_xfer);
    399 		sc->sc_data_xfer = NULL;
    400 	}
    401 }
    402 
    403 int
    404 usscanner_activate(device_ptr_t self, enum devact act)
    405 {
    406 	struct usscanner_softc *sc = (struct usscanner_softc *)self;
    407 
    408 	switch (act) {
    409 	case DVACT_ACTIVATE:
    410 		return (EOPNOTSUPP);
    411 
    412 	case DVACT_DEACTIVATE:
    413 		sc->sc_dying = 1;
    414 		break;
    415 	}
    416 	return (0);
    417 }
    418 
    419 Static void
    420 usscanner_scsipi_minphys(struct buf *bp)
    421 {
    422 	if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE)
    423 		bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE;
    424 	minphys(bp);
    425 }
    426 
    427 Static void
    428 usscanner_sense(struct usscanner_softc *sc)
    429 {
    430 	struct scsipi_xfer *xs = sc->sc_xs;
    431 	struct scsipi_periph *periph = xs->xs_periph;
    432 	struct scsi_request_sense sense_cmd;
    433 	usbd_status err;
    434 
    435 	/* fetch sense data */
    436 	memset(&sense_cmd, 0, sizeof(sense_cmd));
    437 	sense_cmd.opcode = SCSI_REQUEST_SENSE;
    438 	sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
    439 	sense_cmd.length = sizeof xs->sense;
    440 
    441 	sc->sc_state = UAS_SENSECMD;
    442 	memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd);
    443 	usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer,
    444 	    sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT,
    445 	    usscanner_sensecmd_cb);
    446 	err = usbd_transfer(sc->sc_cmd_xfer);
    447 	if (err == USBD_IN_PROGRESS)
    448 		return;
    449 
    450 	xs->error = XS_DRIVER_STUFFUP;
    451 	usscanner_done(sc);
    452 }
    453 
    454 Static void
    455 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    456 		 usbd_status status)
    457 {
    458 	struct usscanner_softc *sc = priv;
    459 	int s;
    460 
    461 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
    462 
    463 #ifdef USSCANNER_DEBUG
    464 	if (sc->sc_state != UAS_STATUS) {
    465 		printf("%s: !UAS_STATUS\n", USBDEVNAME(sc->sc_dev));
    466 	}
    467 	if (sc->sc_status != 0) {
    468 		printf("%s: status byte=0x%02x\n", USBDEVNAME(sc->sc_dev), sc->sc_status);
    469 	}
    470 #endif
    471 	/* XXX what should we do on non-0 status */
    472 
    473 	sc->sc_state = UAS_IDLE;
    474 
    475 	s = splbio();
    476 	scsipi_done(sc->sc_xs);
    477 	splx(s);
    478 }
    479 
    480 Static void
    481 usscanner_data_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    482 		 usbd_status status)
    483 {
    484 	struct usscanner_softc *sc = priv;
    485 	struct scsipi_xfer *xs = sc->sc_xs;
    486 	u_int32_t len;
    487 
    488 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
    489 
    490 #ifdef USSCANNER_DEBUG
    491 	if (sc->sc_state != UAS_DATA) {
    492 		printf("%s: !UAS_DATA\n", USBDEVNAME(sc->sc_dev));
    493 	}
    494 #endif
    495 
    496 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    497 
    498 	xs->resid = xs->datalen - len;
    499 
    500 	switch (status) {
    501 	case USBD_NORMAL_COMPLETION:
    502 		if (xs->xs_control & XS_CTL_DATA_IN)
    503 			memcpy(xs->data, sc->sc_data_buffer, len);
    504 		xs->error = XS_NOERROR;
    505 		break;
    506 	case USBD_TIMEOUT:
    507 		xs->error = XS_TIMEOUT;
    508 		break;
    509 	case USBD_CANCELLED:
    510 		if (xs->error == XS_SENSE) {
    511 			usscanner_sense(sc);
    512 			return;
    513 		}
    514 		break;
    515 	default:
    516 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    517 		break;
    518 	}
    519 	usscanner_done(sc);
    520 }
    521 
    522 Static void
    523 usscanner_sensedata_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    524 		       usbd_status status)
    525 {
    526 	struct usscanner_softc *sc = priv;
    527 	struct scsipi_xfer *xs = sc->sc_xs;
    528 	u_int32_t len;
    529 
    530 	DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status));
    531 
    532 #ifdef USSCANNER_DEBUG
    533 	if (sc->sc_state != UAS_SENSEDATA) {
    534 		printf("%s: !UAS_SENSEDATA\n", USBDEVNAME(sc->sc_dev));
    535 	}
    536 #endif
    537 
    538 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    539 
    540 	switch (status) {
    541 	case USBD_NORMAL_COMPLETION:
    542 		memcpy(&xs->sense, sc->sc_data_buffer, len);
    543 		if (len < sizeof xs->sense)
    544 			xs->error = XS_SHORTSENSE;
    545 		break;
    546 	case USBD_TIMEOUT:
    547 		xs->error = XS_TIMEOUT;
    548 		break;
    549 	case USBD_CANCELLED:
    550 		xs->error = XS_RESET;
    551 		break;
    552 	default:
    553 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    554 		break;
    555 	}
    556 	usscanner_done(sc);
    557 }
    558 
    559 Static void
    560 usscanner_done(struct usscanner_softc *sc)
    561 {
    562 	struct scsipi_xfer *xs = sc->sc_xs;
    563 	usbd_status err;
    564 
    565 	DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error));
    566 
    567 	sc->sc_state = UAS_STATUS;
    568 	usbd_setup_xfer(sc->sc_intr_xfer, sc->sc_intr_pipe, sc, &sc->sc_status,
    569 	    1, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    570 	    USSCANNER_TIMEOUT, usscanner_intr_cb);
    571 	err = usbd_transfer(sc->sc_intr_xfer);
    572 	if (err == USBD_IN_PROGRESS)
    573 		return;
    574 	xs->error = XS_DRIVER_STUFFUP;
    575 }
    576 
    577 Static void
    578 usscanner_sensecmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    579 		      usbd_status status)
    580 {
    581 	struct usscanner_softc *sc = priv;
    582 	struct scsipi_xfer *xs = sc->sc_xs;
    583 	usbd_status err;
    584 
    585 	DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status));
    586 
    587 #ifdef USSCANNER_DEBUG
    588 	if (usscannerdebug > 15)
    589 		xs->xs_periph->periph_flags |= 1; /* XXX 1 */
    590 
    591 	if (sc->sc_state != UAS_SENSECMD) {
    592 		printf("%s: !UAS_SENSECMD\n", USBDEVNAME(sc->sc_dev));
    593 		xs->error = XS_DRIVER_STUFFUP;
    594 		goto done;
    595 	}
    596 #endif
    597 
    598 	switch (status) {
    599 	case USBD_NORMAL_COMPLETION:
    600 		break;
    601 	case USBD_TIMEOUT:
    602 		xs->error = XS_TIMEOUT;
    603 		goto done;
    604 	default:
    605 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    606 		goto done;
    607 	}
    608 
    609 	sc->sc_state = UAS_SENSEDATA;
    610 	usbd_setup_xfer(sc->sc_data_xfer, sc->sc_in_pipe, sc,
    611 	    sc->sc_data_buffer,
    612 	    sizeof xs->sense, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    613 	    USSCANNER_TIMEOUT, usscanner_sensedata_cb);
    614 	err = usbd_transfer(sc->sc_data_xfer);
    615 	if (err == USBD_IN_PROGRESS)
    616 		return;
    617 	xs->error = XS_DRIVER_STUFFUP;
    618  done:
    619 	usscanner_done(sc);
    620 }
    621 
    622 Static void
    623 usscanner_cmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    624 		 usbd_status status)
    625 {
    626 	struct usscanner_softc *sc = priv;
    627 	struct scsipi_xfer *xs = sc->sc_xs;
    628 	usbd_pipe_handle pipe;
    629 	usbd_status err;
    630 
    631 	DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status));
    632 
    633 #ifdef USSCANNER_DEBUG
    634 	if (usscannerdebug > 15)
    635 		xs->xs_periph->periph_flags |= 1;	/* XXX 1 */
    636 
    637 	if (sc->sc_state != UAS_CMD) {
    638 		printf("%s: !UAS_CMD\n", USBDEVNAME(sc->sc_dev));
    639 		xs->error = XS_DRIVER_STUFFUP;
    640 		goto done;
    641 	}
    642 #endif
    643 
    644 	switch (status) {
    645 	case USBD_NORMAL_COMPLETION:
    646 		break;
    647 	case USBD_TIMEOUT:
    648 		xs->error = XS_TIMEOUT;
    649 		goto done;
    650 	case USBD_CANCELLED:
    651 		goto done;
    652 	default:
    653 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    654 		goto done;
    655 	}
    656 
    657 	if (xs->datalen == 0) {
    658 		DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n"));
    659 		xs->error = XS_NOERROR;
    660 		goto done;
    661 	}
    662 
    663 	if (xs->xs_control & XS_CTL_DATA_IN) {
    664 		DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n",
    665 			     xs->datalen));
    666 		pipe = sc->sc_in_pipe;
    667 	} else {
    668 		DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n",
    669 			     xs->datalen));
    670 		memcpy(sc->sc_data_buffer, xs->data, xs->datalen);
    671 		pipe = sc->sc_out_pipe;
    672 	}
    673 	sc->sc_state = UAS_DATA;
    674 	usbd_setup_xfer(sc->sc_data_xfer, pipe, sc, sc->sc_data_buffer,
    675 	    xs->datalen, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    676 	    xs->timeout, usscanner_data_cb);
    677 	err = usbd_transfer(sc->sc_data_xfer);
    678 	if (err == USBD_IN_PROGRESS)
    679 		return;
    680 	xs->error = XS_DRIVER_STUFFUP;
    681 
    682  done:
    683 	usscanner_done(sc);
    684 }
    685 
    686 Static void
    687 usscanner_scsipi_request(chan, req, arg)
    688 	struct scsipi_channel *chan;
    689 	scsipi_adapter_req_t req;
    690 	void *arg;
    691 {
    692 	struct scsipi_xfer *xs;
    693 	struct scsipi_periph *periph;
    694 	struct usscanner_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    695 	usbd_status err;
    696 
    697 	switch (req) {
    698 	case ADAPTER_REQ_RUN_XFER:
    699 		xs = arg;
    700 		periph = xs->xs_periph;
    701 
    702 		DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d "
    703 		    "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
    704 		    USBDEVNAME(sc->sc_dev),
    705 		    periph->periph_target, periph->periph_lun,
    706 		    xs, xs->cmd->opcode, xs->datalen,
    707 		    periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
    708 
    709 		if (sc->sc_dying) {
    710 			xs->error = XS_DRIVER_STUFFUP;
    711 			goto done;
    712 		}
    713 
    714 #ifdef USSCANNER_DEBUG
    715 		if (periph->periph_target != USSCANNER_SCSIID_DEVICE) {
    716 			DPRINTF(("%s: wrong SCSI ID %d\n",
    717 			    USBDEVNAME(sc->sc_dev), periph->periph_target));
    718 			xs->error = XS_DRIVER_STUFFUP;
    719 			goto done;
    720 		}
    721 		if (sc->sc_state != UAS_IDLE) {
    722 			printf("%s: !UAS_IDLE\n", USBDEVNAME(sc->sc_dev));
    723 			xs->error = XS_DRIVER_STUFFUP;
    724 			goto done;
    725 		}
    726 #endif
    727 
    728 		if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) {
    729 			printf("%s: usscanner_scsipi_request: large datalen,"
    730 			    " %d\n", USBDEVNAME(sc->sc_dev), xs->datalen);
    731 			xs->error = XS_DRIVER_STUFFUP;
    732 			goto done;
    733 		}
    734 
    735 		DPRINTFN(4, ("%s: usscanner_scsipi_request: async cmdlen=%d"
    736 		    " datalen=%d\n", USBDEVNAME(sc->sc_dev), xs->cmdlen,
    737 		    xs->datalen));
    738 		sc->sc_state = UAS_CMD;
    739 		sc->sc_xs = xs;
    740 		memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
    741 		usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc,
    742 		    sc->sc_cmd_buffer, xs->cmdlen, USBD_NO_COPY,
    743 		    USSCANNER_TIMEOUT, usscanner_cmd_cb);
    744 		err = usbd_transfer(sc->sc_cmd_xfer);
    745 		if (err != USBD_IN_PROGRESS) {
    746 			xs->error = XS_DRIVER_STUFFUP;
    747 			goto done;
    748 		}
    749 
    750 		return;
    751 
    752 
    753  done:
    754 		sc->sc_state = UAS_IDLE;
    755 		scsipi_done(xs);
    756 		return;
    757 
    758 	case ADAPTER_REQ_GROW_RESOURCES:
    759 		/* XXX Not supported. */
    760 		return;
    761 	case ADAPTER_REQ_SET_XFER_MODE:
    762 		/* XXX Not supported. */
    763 		return;
    764 	}
    765 
    766 }
    767