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