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