Home | History | Annotate | Line # | Download | only in usb
usscanner.c revision 1.21.14.3
      1 /*	$NetBSD: usscanner.c,v 1.21.14.3 2007/06/18 14:16:11 itohy 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.21.14.3 2007/06/18 14:16:11 itohy 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_datain_xfer;
    120 	usbd_xfer_handle	sc_dataout_xfer;
    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 #ifndef USB_USE_IFATTACH
    164 	if (uaa->iface != NULL)
    165 		return (UMATCH_NONE);
    166 #endif /* USB_USE_IFATTACH */
    167 
    168 	if (uaa->vendor == USB_VENDOR_HP &&
    169 	    uaa->product == USB_PRODUCT_HP_5300C)
    170 		return (UMATCH_VENDOR_PRODUCT);
    171 	else
    172 		return (UMATCH_NONE);
    173 }
    174 
    175 USB_ATTACH(usscanner)
    176 {
    177 	USB_ATTACH_START(usscanner, sc, uaa);
    178 	usbd_device_handle	dev = uaa->device;
    179 	usbd_interface_handle	iface;
    180 	char			*devinfop;
    181 	usbd_status		err;
    182 	usb_endpoint_descriptor_t *ed;
    183 	u_int8_t		epcount;
    184 	int			i;
    185 
    186 	DPRINTFN(10,("usscanner_attach: sc=%p\n", sc));
    187 
    188 	devinfop = usbd_devinfo_alloc(dev, 0);
    189 	USB_ATTACH_SETUP;
    190 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    191 	usbd_devinfo_free(devinfop);
    192 
    193 	err = usbd_set_config_no(dev, USSCANNER_CONFIG_NO, 1);
    194 	if (err) {
    195 		printf("%s: setting config no failed\n",
    196 		    USBDEVNAME(sc->sc_dev));
    197 		USB_ATTACH_ERROR_RETURN;
    198 	}
    199 
    200 	err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface);
    201 	if (err) {
    202 		printf("%s: getting interface handle failed\n",
    203 		    USBDEVNAME(sc->sc_dev));
    204 		USB_ATTACH_ERROR_RETURN;
    205 	}
    206 
    207 	sc->sc_udev = dev;
    208 	sc->sc_iface = iface;
    209 
    210 	epcount = 0;
    211 	(void)usbd_endpoint_count(iface, &epcount);
    212 
    213 	sc->sc_in_addr = -1;
    214 	sc->sc_intr_addr = -1;
    215 	sc->sc_out_addr = -1;
    216 	for (i = 0; i < epcount; i++) {
    217 		ed = usbd_interface2endpoint_descriptor(iface, i);
    218 		if (ed == NULL) {
    219 			printf("%s: couldn't get ep %d\n",
    220 			    USBDEVNAME(sc->sc_dev), i);
    221 			USB_ATTACH_ERROR_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 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
    237 		USB_ATTACH_ERROR_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 		printf("%s: open in pipe failed, err=%d\n",
    244 		       USBDEVNAME(sc->sc_dev), err);
    245 		USB_ATTACH_ERROR_RETURN;
    246 	}
    247 
    248 	/* The interrupt endpoint must be opened as a normal pipe. */
    249 	err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr,
    250 			     USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe);
    251 
    252 	if (err) {
    253 		printf("%s: open intr pipe failed, err=%d\n",
    254 		       USBDEVNAME(sc->sc_dev), err);
    255 		usscanner_cleanup(sc);
    256 		USB_ATTACH_ERROR_RETURN;
    257 	}
    258 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr,
    259 			     USBD_EXCLUSIVE_USE, &sc->sc_out_pipe);
    260 	if (err) {
    261 		printf("%s: open out pipe failed, err=%d\n",
    262 		       USBDEVNAME(sc->sc_dev), err);
    263 		usscanner_cleanup(sc);
    264 		USB_ATTACH_ERROR_RETURN;
    265 	}
    266 
    267 	sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device, sc->sc_out_pipe);
    268 	if (sc->sc_cmd_xfer == NULL) {
    269 		printf("%s: alloc cmd xfer failed, err=%d\n",
    270 		       USBDEVNAME(sc->sc_dev), err);
    271 		usscanner_cleanup(sc);
    272 		USB_ATTACH_ERROR_RETURN;
    273 	}
    274 
    275 	/* XXX too big */
    276 	sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer,
    277 					     USSCANNER_MAX_TRANSFER_SIZE);
    278 	if (sc->sc_cmd_buffer == NULL) {
    279 		printf("%s: alloc cmd buffer failed, err=%d\n",
    280 		       USBDEVNAME(sc->sc_dev), err);
    281 		usscanner_cleanup(sc);
    282 		USB_ATTACH_ERROR_RETURN;
    283 	}
    284 
    285 	sc->sc_intr_xfer = usbd_alloc_xfer(uaa->device, sc->sc_intr_pipe);
    286 	if (sc->sc_intr_xfer == NULL) {
    287 	  printf("%s: alloc intr xfer failed, err=%d\n",
    288 		 USBDEVNAME(sc->sc_dev), err);
    289 	  usscanner_cleanup(sc);
    290 	  USB_ATTACH_ERROR_RETURN;
    291         }
    292 
    293 	sc->sc_datain_xfer = usbd_alloc_xfer(uaa->device, sc->sc_in_pipe);
    294 	if (sc->sc_datain_xfer == NULL) {
    295 		printf("%s: alloc data xfer failed, err=%d\n",
    296 		       USBDEVNAME(sc->sc_dev), err);
    297 		usscanner_cleanup(sc);
    298 		USB_ATTACH_ERROR_RETURN;
    299 	}
    300 	err = usbd_map_alloc(sc->sc_datain_xfer);
    301 	if (err) {
    302 		printf("%s: alloc map failed, err=%d\n",
    303 		       USBDEVNAME(sc->sc_dev), err);
    304 		usscanner_cleanup(sc);
    305 		USB_ATTACH_ERROR_RETURN;
    306 	}
    307 
    308 	sc->sc_dataout_xfer = usbd_alloc_xfer(uaa->device, sc->sc_out_pipe);
    309 	if (sc->sc_dataout_xfer == NULL) {
    310 		printf("%s: alloc data xfer failed, err=%d\n",
    311 		       USBDEVNAME(sc->sc_dev), err);
    312 		usscanner_cleanup(sc);
    313 		USB_ATTACH_ERROR_RETURN;
    314 	}
    315 	err = usbd_map_alloc(sc->sc_dataout_xfer);
    316 	if (err) {
    317 		printf("%s: alloc map failed, err=%d\n",
    318 		       USBDEVNAME(sc->sc_dev), err);
    319 		usscanner_cleanup(sc);
    320 		USB_ATTACH_ERROR_RETURN;
    321 	}
    322 
    323 	/*
    324 	 * Fill in the adapter.
    325 	 */
    326 	sc->sc_adapter.adapt_request = usscanner_scsipi_request;
    327 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
    328 	sc->sc_adapter.adapt_nchannels = 1;
    329 	sc->sc_adapter.adapt_openings = 1;
    330 	sc->sc_adapter.adapt_max_periph = 1;
    331 	sc->sc_adapter.adapt_minphys = usscanner_scsipi_minphys;
    332 
    333 #if NSCSIBUS > 0
    334 	/*
    335 	 * fill in the scsipi_channel.
    336 	 */
    337 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
    338 	sc->sc_channel.chan_bustype = &scsi_bustype;
    339 	sc->sc_channel.chan_channel = 0;
    340 	sc->sc_channel.chan_ntargets = USSCANNER_SCSIID_DEVICE + 1;
    341 	sc->sc_channel.chan_nluns = 1;
    342 	sc->sc_channel.chan_id = USSCANNER_SCSIID_HOST;
    343 
    344 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    345 			   USBDEV(sc->sc_dev));
    346 
    347 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
    348 
    349 	DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev));
    350 
    351 	USB_ATTACH_SUCCESS_RETURN;
    352 
    353 #else
    354 	/* No SCSI bus, just ignore it */
    355 	usscanner_cleanup(sc);
    356 
    357 	printf("%s: no scsibus configured, see usscanner(4) for details\n",
    358 	    USBDEVNAME(sc->sc_dev));
    359 
    360 	USB_ATTACH_ERROR_RETURN;
    361 
    362 #endif
    363 }
    364 
    365 USB_DETACH(usscanner)
    366 {
    367 	USB_DETACH_START(usscanner, sc);
    368 	int rv, s;
    369 
    370 	DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
    371 
    372 	sc->sc_dying = 1;
    373 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
    374 	if (sc->sc_in_pipe != NULL)
    375 		usbd_abort_pipe(sc->sc_in_pipe);
    376 	if (sc->sc_intr_pipe != NULL)
    377 		usbd_abort_pipe(sc->sc_intr_pipe);
    378 	if (sc->sc_out_pipe != NULL)
    379 		usbd_abort_pipe(sc->sc_out_pipe);
    380 
    381 	s = splusb();
    382 	if (--sc->sc_refcnt >= 0) {
    383 		/* Wait for processes to go away. */
    384 		usb_detach_wait(USBDEV(sc->sc_dev));
    385 	}
    386 	splx(s);
    387 
    388 	if (sc->sc_child != NULL)
    389 		rv = config_detach(sc->sc_child, flags);
    390 	else
    391 		rv = 0;
    392 
    393 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    394 			   USBDEV(sc->sc_dev));
    395 
    396 	return (rv);
    397 }
    398 
    399 Static void
    400 usscanner_cleanup(struct usscanner_softc *sc)
    401 {
    402 	if (sc->sc_cmd_xfer != NULL) {
    403 		usbd_free_xfer(sc->sc_cmd_xfer);
    404 		sc->sc_cmd_xfer = NULL;
    405 	}
    406 	if (sc->sc_datain_xfer != NULL) {
    407 		usbd_free_xfer(sc->sc_datain_xfer);
    408 		sc->sc_datain_xfer = NULL;
    409 	}
    410 	if (sc->sc_dataout_xfer != NULL) {
    411 		usbd_free_xfer(sc->sc_dataout_xfer);
    412 		sc->sc_dataout_xfer = NULL;
    413 	}
    414 	if (sc->sc_in_pipe != NULL) {
    415 		usbd_close_pipe(sc->sc_in_pipe);
    416 		sc->sc_in_pipe = NULL;
    417 	}
    418 	if (sc->sc_intr_pipe != NULL) {
    419 		usbd_close_pipe(sc->sc_intr_pipe);
    420 		sc->sc_intr_pipe = NULL;
    421 	}
    422 	if (sc->sc_out_pipe != NULL) {
    423 		usbd_close_pipe(sc->sc_out_pipe);
    424 		sc->sc_out_pipe = NULL;
    425 	}
    426 }
    427 
    428 int
    429 usscanner_activate(device_ptr_t self, enum devact act)
    430 {
    431 	struct usscanner_softc *sc = (struct usscanner_softc *)self;
    432 
    433 	switch (act) {
    434 	case DVACT_ACTIVATE:
    435 		return (EOPNOTSUPP);
    436 
    437 	case DVACT_DEACTIVATE:
    438 		sc->sc_dying = 1;
    439 		break;
    440 	}
    441 	return (0);
    442 }
    443 
    444 Static void
    445 usscanner_scsipi_minphys(struct buf *bp)
    446 {
    447 	if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE)
    448 		bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE;
    449 	minphys(bp);
    450 }
    451 
    452 Static void
    453 usscanner_sense(struct usscanner_softc *sc)
    454 {
    455 	struct scsipi_xfer *xs = sc->sc_xs;
    456 	struct scsipi_periph *periph = xs->xs_periph;
    457 	struct scsi_request_sense sense_cmd;
    458 	usbd_status err;
    459 
    460 	/* fetch sense data */
    461 	memset(&sense_cmd, 0, sizeof(sense_cmd));
    462 	sense_cmd.opcode = SCSI_REQUEST_SENSE;
    463 	sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
    464 	sense_cmd.length = sizeof xs->sense;
    465 
    466 	sc->sc_state = UAS_SENSECMD;
    467 	memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd);
    468 	usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer,
    469 	    sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT,
    470 	    usscanner_sensecmd_cb);
    471 	err = usbd_transfer(sc->sc_cmd_xfer);
    472 	if (err == USBD_IN_PROGRESS)
    473 		return;
    474 
    475 	xs->error = XS_DRIVER_STUFFUP;
    476 	usscanner_done(sc);
    477 }
    478 
    479 Static void
    480 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    481 		 usbd_status status)
    482 {
    483 	struct usscanner_softc *sc = priv;
    484 	int s;
    485 
    486 	DPRINTFN(10, ("usscanner_intr_cb status=%d\n", status));
    487 
    488 #ifdef USSCANNER_DEBUG
    489 	if (sc->sc_state != UAS_STATUS) {
    490 		printf("%s: !UAS_STATUS\n", USBDEVNAME(sc->sc_dev));
    491 	}
    492 	if (sc->sc_status != 0) {
    493 		printf("%s: status byte=0x%02x\n", USBDEVNAME(sc->sc_dev), sc->sc_status);
    494 	}
    495 #endif
    496 	/* XXX what should we do on non-0 status */
    497 
    498 	sc->sc_state = UAS_IDLE;
    499 
    500 	s = splbio();
    501 	scsipi_done(sc->sc_xs);
    502 	splx(s);
    503 }
    504 
    505 Static void
    506 usscanner_data_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    507 		 usbd_status status)
    508 {
    509 	struct usscanner_softc *sc = priv;
    510 	struct scsipi_xfer *xs = sc->sc_xs;
    511 	u_int32_t len;
    512 
    513 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
    514 
    515 #ifdef USSCANNER_DEBUG
    516 	if (sc->sc_state != UAS_DATA) {
    517 		printf("%s: !UAS_DATA\n", USBDEVNAME(sc->sc_dev));
    518 	}
    519 #endif
    520 
    521 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    522 
    523 	xs->resid = xs->datalen - len;
    524 
    525 	usbd_unmap_buffer(xfer);
    526 
    527 	switch (status) {
    528 	case USBD_NORMAL_COMPLETION:
    529 		xs->error = XS_NOERROR;
    530 		break;
    531 	case USBD_TIMEOUT:
    532 		xs->error = XS_TIMEOUT;
    533 		break;
    534 	case USBD_CANCELLED:
    535 		if (xs->error == XS_SENSE) {
    536 			usscanner_sense(sc);
    537 			return;
    538 		}
    539 		break;
    540 	default:
    541 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    542 		break;
    543 	}
    544 	usscanner_done(sc);
    545 }
    546 
    547 Static void
    548 usscanner_sensedata_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    549 		       usbd_status status)
    550 {
    551 	struct usscanner_softc *sc = priv;
    552 	struct scsipi_xfer *xs = sc->sc_xs;
    553 	u_int32_t len;
    554 
    555 	DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status));
    556 
    557 #ifdef USSCANNER_DEBUG
    558 	if (sc->sc_state != UAS_SENSEDATA) {
    559 		printf("%s: !UAS_SENSEDATA\n", USBDEVNAME(sc->sc_dev));
    560 	}
    561 #endif
    562 
    563 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
    564 	usbd_unmap_buffer(xfer);
    565 
    566 	switch (status) {
    567 	case USBD_NORMAL_COMPLETION:
    568 		if (len < sizeof xs->sense)
    569 			xs->error = XS_SHORTSENSE;
    570 		break;
    571 	case USBD_TIMEOUT:
    572 		xs->error = XS_TIMEOUT;
    573 		break;
    574 	case USBD_CANCELLED:
    575 		xs->error = XS_RESET;
    576 		break;
    577 	default:
    578 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    579 		break;
    580 	}
    581 	usscanner_done(sc);
    582 }
    583 
    584 Static void
    585 usscanner_done(struct usscanner_softc *sc)
    586 {
    587 	struct scsipi_xfer *xs = sc->sc_xs;
    588 	usbd_status err;
    589 
    590 	DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error));
    591 
    592 	sc->sc_state = UAS_STATUS;
    593 	usbd_setup_xfer(sc->sc_intr_xfer, sc->sc_intr_pipe, sc, &sc->sc_status,
    594 	    1, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    595 	    USSCANNER_TIMEOUT, usscanner_intr_cb);
    596 	err = usbd_transfer(sc->sc_intr_xfer);
    597 	if (err == USBD_IN_PROGRESS)
    598 		return;
    599 	xs->error = XS_DRIVER_STUFFUP;
    600 }
    601 
    602 Static void
    603 usscanner_sensecmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    604 		      usbd_status status)
    605 {
    606 	struct usscanner_softc *sc = priv;
    607 	struct scsipi_xfer *xs = sc->sc_xs;
    608 	usbd_status err;
    609 
    610 	DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status));
    611 
    612 #ifdef USSCANNER_DEBUG
    613 	if (usscannerdebug > 15)
    614 		xs->xs_periph->periph_flags |= 1; /* XXX 1 */
    615 
    616 	if (sc->sc_state != UAS_SENSECMD) {
    617 		printf("%s: !UAS_SENSECMD\n", USBDEVNAME(sc->sc_dev));
    618 		xs->error = XS_DRIVER_STUFFUP;
    619 		goto done;
    620 	}
    621 #endif
    622 
    623 	switch (status) {
    624 	case USBD_NORMAL_COMPLETION:
    625 		break;
    626 	case USBD_TIMEOUT:
    627 		xs->error = XS_TIMEOUT;
    628 		goto done;
    629 	default:
    630 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    631 		goto done;
    632 	}
    633 
    634 	sc->sc_state = UAS_SENSEDATA;
    635 	usbd_map_buffer(xfer, &xs->sense, sizeof xs->sense);
    636 	usbd_setup_xfer(sc->sc_datain_xfer, sc->sc_in_pipe, sc,
    637 	    &xs->sense,
    638 	    sizeof xs->sense, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    639 	    USSCANNER_TIMEOUT, usscanner_sensedata_cb);
    640 	err = usbd_transfer(sc->sc_datain_xfer);
    641 	if (err == USBD_IN_PROGRESS)
    642 		return;
    643 	xs->error = XS_DRIVER_STUFFUP;
    644  done:
    645 	usscanner_done(sc);
    646 }
    647 
    648 Static void
    649 usscanner_cmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
    650 		 usbd_status status)
    651 {
    652 	struct usscanner_softc *sc = priv;
    653 	struct scsipi_xfer *xs = sc->sc_xs;
    654 	usbd_pipe_handle pipe;
    655 	usbd_status err;
    656 
    657 	DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status));
    658 
    659 #ifdef USSCANNER_DEBUG
    660 	if (usscannerdebug > 15)
    661 		xs->xs_periph->periph_flags |= 1;	/* XXX 1 */
    662 
    663 	if (sc->sc_state != UAS_CMD) {
    664 		printf("%s: !UAS_CMD\n", USBDEVNAME(sc->sc_dev));
    665 		xs->error = XS_DRIVER_STUFFUP;
    666 		goto done;
    667 	}
    668 #endif
    669 
    670 	switch (status) {
    671 	case USBD_NORMAL_COMPLETION:
    672 		break;
    673 	case USBD_TIMEOUT:
    674 		xs->error = XS_TIMEOUT;
    675 		goto done;
    676 	case USBD_CANCELLED:
    677 		goto done;
    678 	default:
    679 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
    680 		goto done;
    681 	}
    682 
    683 	if (xs->datalen == 0) {
    684 		DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n"));
    685 		xs->error = XS_NOERROR;
    686 		goto done;
    687 	}
    688 
    689 	if (xs->xs_control & XS_CTL_DATA_IN) {
    690 		DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n",
    691 			     xs->datalen));
    692 		xfer = sc->sc_datain_xfer;
    693 		pipe = sc->sc_in_pipe;
    694 	} else {
    695 		DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n",
    696 			     xs->datalen));
    697 		xfer = sc->sc_dataout_xfer;
    698 		pipe = sc->sc_out_pipe;
    699 	}
    700 	sc->sc_state = UAS_DATA;
    701 	usbd_map_buffer(xfer, xs->data, xs->datalen);
    702 	usbd_setup_xfer(xfer, pipe, sc, xs->data,
    703 	    xs->datalen, USBD_SHORT_XFER_OK | USBD_NO_COPY,
    704 	    xs->timeout, usscanner_data_cb);
    705 	err = usbd_transfer(xfer);
    706 	if (err == USBD_IN_PROGRESS)
    707 		return;
    708 	xs->error = XS_DRIVER_STUFFUP;
    709 
    710  done:
    711 	usscanner_done(sc);
    712 }
    713 
    714 Static void
    715 usscanner_scsipi_request(chan, req, arg)
    716 	struct scsipi_channel *chan;
    717 	scsipi_adapter_req_t req;
    718 	void *arg;
    719 {
    720 	struct scsipi_xfer *xs;
    721 	struct scsipi_periph *periph;
    722 	struct usscanner_softc *sc = (void *)chan->chan_adapter->adapt_dev;
    723 	usbd_status err;
    724 
    725 	switch (req) {
    726 	case ADAPTER_REQ_RUN_XFER:
    727 		xs = arg;
    728 		periph = xs->xs_periph;
    729 
    730 		DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d "
    731 		    "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
    732 		    USBDEVNAME(sc->sc_dev),
    733 		    periph->periph_target, periph->periph_lun,
    734 		    xs, xs->cmd->opcode, xs->datalen,
    735 		    periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
    736 
    737 		if (sc->sc_dying) {
    738 			xs->error = XS_DRIVER_STUFFUP;
    739 			goto done;
    740 		}
    741 
    742 #ifdef USSCANNER_DEBUG
    743 		if (periph->periph_target != USSCANNER_SCSIID_DEVICE) {
    744 			DPRINTF(("%s: wrong SCSI ID %d\n",
    745 			    USBDEVNAME(sc->sc_dev), periph->periph_target));
    746 			xs->error = XS_DRIVER_STUFFUP;
    747 			goto done;
    748 		}
    749 		if (sc->sc_state != UAS_IDLE) {
    750 			printf("%s: !UAS_IDLE\n", USBDEVNAME(sc->sc_dev));
    751 			xs->error = XS_DRIVER_STUFFUP;
    752 			goto done;
    753 		}
    754 #endif
    755 
    756 		if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) {
    757 			printf("%s: usscanner_scsipi_request: large datalen,"
    758 			    " %d\n", USBDEVNAME(sc->sc_dev), xs->datalen);
    759 			xs->error = XS_DRIVER_STUFFUP;
    760 			goto done;
    761 		}
    762 
    763 		DPRINTFN(4, ("%s: usscanner_scsipi_request: async cmdlen=%d"
    764 		    " datalen=%d\n", USBDEVNAME(sc->sc_dev), xs->cmdlen,
    765 		    xs->datalen));
    766 		sc->sc_state = UAS_CMD;
    767 		sc->sc_xs = xs;
    768 		memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
    769 		usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc,
    770 		    sc->sc_cmd_buffer, xs->cmdlen, USBD_NO_COPY,
    771 		    USSCANNER_TIMEOUT, usscanner_cmd_cb);
    772 		err = usbd_transfer(sc->sc_cmd_xfer);
    773 		if (err != USBD_IN_PROGRESS) {
    774 			xs->error = XS_DRIVER_STUFFUP;
    775 			goto done;
    776 		}
    777 
    778 		return;
    779 
    780 
    781  done:
    782 		sc->sc_state = UAS_IDLE;
    783 		scsipi_done(xs);
    784 		return;
    785 
    786 	case ADAPTER_REQ_GROW_RESOURCES:
    787 		/* XXX Not supported. */
    788 		return;
    789 	case ADAPTER_REQ_SET_XFER_MODE:
    790 		/* XXX Not supported. */
    791 		return;
    792 	}
    793 
    794 }
    795