xref: /netbsd-src/sys/dev/usb/usscanner.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /*	$NetBSD: usscanner.c,v 1.48 2020/03/14 02:35:33 christos 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@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.48 2020/03/14 02:35:33 christos 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 
336 	DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev));
337 
338 	return;
339 
340 #else
341 	/* No SCSI bus, just ignore it */
342 	usscanner_cleanup(sc);
343 
344 	aprint_error_dev(self,
345 	    "no scsibus configured, see usscanner(4) for details\n");
346 
347 	return;
348 
349 #endif
350 }
351 
352 static void
353 usscanner_childdet(device_t self, device_t child)
354 {
355 	struct usscanner_softc *sc = device_private(self);
356 
357 	KASSERT(sc->sc_child == NULL);
358 	sc->sc_child = NULL;
359 }
360 
361 static int
362 usscanner_detach(device_t self, int flags)
363 {
364 	struct usscanner_softc *sc = device_private(self);
365 	int rv, s;
366 
367 	DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
368 
369 	sc->sc_dying = 1;
370 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
371 	if (sc->sc_in_pipe != NULL)
372 		usbd_abort_pipe(sc->sc_in_pipe);
373 	if (sc->sc_intr_pipe != NULL)
374 		usbd_abort_pipe(sc->sc_intr_pipe);
375 	if (sc->sc_out_pipe != NULL)
376 		usbd_abort_pipe(sc->sc_out_pipe);
377 
378 	s = splusb();
379 	if (--sc->sc_refcnt >= 0) {
380 		/* Wait for processes to go away. */
381 		usb_detach_waitold(sc->sc_dev);
382 	}
383 	splx(s);
384 
385 	if (sc->sc_child != NULL)
386 		rv = config_detach(sc->sc_child, flags);
387 	else
388 		rv = 0;
389 
390 	if (sc->sc_udev != NULL)
391 		usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
392 		    sc->sc_dev);
393 
394 	return rv;
395 }
396 
397 Static void
398 usscanner_cleanup(struct usscanner_softc *sc)
399 {
400 	if (sc->sc_cmd_xfer != NULL) {
401 		usbd_destroy_xfer(sc->sc_cmd_xfer);
402 		sc->sc_cmd_xfer = NULL;
403 	}
404 	if (sc->sc_datain_xfer != NULL) {
405 		usbd_destroy_xfer(sc->sc_datain_xfer);
406 		sc->sc_datain_xfer = NULL;
407 	}
408 	if (sc->sc_dataout_xfer != NULL) {
409 		usbd_destroy_xfer(sc->sc_dataout_xfer);
410 		sc->sc_dataout_xfer = NULL;
411 	}
412 	if (sc->sc_in_pipe != NULL) {
413 		usbd_close_pipe(sc->sc_in_pipe);
414 		sc->sc_in_pipe = NULL;
415 	}
416 	if (sc->sc_intr_pipe != NULL) {
417 		usbd_close_pipe(sc->sc_intr_pipe);
418 		sc->sc_intr_pipe = NULL;
419 	}
420 	if (sc->sc_out_pipe != NULL) {
421 		usbd_close_pipe(sc->sc_out_pipe);
422 		sc->sc_out_pipe = NULL;
423 	}
424 }
425 
426 static int
427 usscanner_activate(device_t self, enum devact act)
428 {
429 	struct usscanner_softc *sc = device_private(self);
430 
431 	switch (act) {
432 	case DVACT_DEACTIVATE:
433 		sc->sc_dying = 1;
434 		return 0;
435 	default:
436 		return EOPNOTSUPP;
437 	}
438 }
439 
440 Static void
441 usscanner_scsipi_minphys(struct buf *bp)
442 {
443 	if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE)
444 		bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE;
445 	minphys(bp);
446 }
447 
448 Static void
449 usscanner_sense(struct usscanner_softc *sc)
450 {
451 	struct scsipi_xfer *xs = sc->sc_xs;
452 	struct scsipi_periph *periph = xs->xs_periph;
453 	struct scsi_request_sense sense_cmd;
454 	usbd_status err;
455 
456 	/* fetch sense data */
457 	memset(&sense_cmd, 0, sizeof(sense_cmd));
458 	sense_cmd.opcode = SCSI_REQUEST_SENSE;
459 	sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
460 	sense_cmd.length = sizeof(xs->sense);
461 
462 	sc->sc_state = UAS_SENSECMD;
463 	memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof(sense_cmd));
464 
465 	usbd_setup_xfer(sc->sc_cmd_xfer, sc, sc->sc_cmd_buffer,
466 	    sizeof(sense_cmd), 0, USSCANNER_TIMEOUT,
467 	    usscanner_sensecmd_cb);
468 	err = usbd_transfer(sc->sc_cmd_xfer);
469 	if (err == USBD_IN_PROGRESS)
470 		return;
471 
472 	xs->error = XS_DRIVER_STUFFUP;
473 	usscanner_done(sc);
474 }
475 
476 Static void
477 usscanner_intr_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
478 {
479 	struct usscanner_softc *sc = priv;
480 	int s;
481 
482 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
483 
484 #ifdef USSCANNER_DEBUG
485 	if (sc->sc_state != UAS_STATUS) {
486 		printf("%s: !UAS_STATUS\n", device_xname(sc->sc_dev));
487 	}
488 	if (sc->sc_status != 0) {
489 		printf("%s: status byte=0x%02x\n", device_xname(sc->sc_dev),
490 		    sc->sc_status);
491 	}
492 #endif
493 	/* XXX what should we do on non-0 status */
494 
495 	sc->sc_state = UAS_IDLE;
496 
497 	s = splbio();
498 	KERNEL_LOCK(1, curlwp);
499 	scsipi_done(sc->sc_xs);
500 	KERNEL_UNLOCK_ONE(curlwp);
501 	splx(s);
502 }
503 
504 Static void
505 usscanner_data_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
506 {
507 	struct usscanner_softc *sc = priv;
508 	struct scsipi_xfer *xs = sc->sc_xs;
509 	uint32_t len;
510 
511 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
512 
513 #ifdef USSCANNER_DEBUG
514 	if (sc->sc_state != UAS_DATA) {
515 		printf("%s: !UAS_DATA\n", device_xname(sc->sc_dev));
516 	}
517 #endif
518 
519 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
520 
521 	xs->resid = xs->datalen - len;
522 
523 	switch (status) {
524 	case USBD_NORMAL_COMPLETION:
525 		xs->error = XS_NOERROR;
526 		break;
527 	case USBD_TIMEOUT:
528 		xs->error = XS_TIMEOUT;
529 		break;
530 	case USBD_CANCELLED:
531 		if (xs->error == XS_SENSE) {
532 			usscanner_sense(sc);
533 			return;
534 		}
535 		break;
536 	default:
537 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
538 		break;
539 	}
540 	usscanner_done(sc);
541 }
542 
543 Static void
544 usscanner_sensedata_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
545 {
546 	struct usscanner_softc *sc = priv;
547 	struct scsipi_xfer *xs = sc->sc_xs;
548 	uint32_t len;
549 
550 	DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status));
551 
552 #ifdef USSCANNER_DEBUG
553 	if (sc->sc_state != UAS_SENSEDATA) {
554 		printf("%s: !UAS_SENSEDATA\n", device_xname(sc->sc_dev));
555 	}
556 #endif
557 
558 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
559 
560 	switch (status) {
561 	case USBD_NORMAL_COMPLETION:
562 		memcpy(&xs->sense, sc->sc_datain_buffer, len);
563 		if (len < sizeof(xs->sense))
564 			xs->error = XS_SHORTSENSE;
565 		break;
566 	case USBD_TIMEOUT:
567 		xs->error = XS_TIMEOUT;
568 		break;
569 	case USBD_CANCELLED:
570 		xs->error = XS_RESET;
571 		break;
572 	default:
573 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
574 		break;
575 	}
576 	usscanner_done(sc);
577 }
578 
579 Static void
580 usscanner_done(struct usscanner_softc *sc)
581 {
582 	struct scsipi_xfer *xs = sc->sc_xs;
583 	usbd_status err;
584 
585 	DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error));
586 
587 	sc->sc_state = UAS_STATUS;
588 	usbd_setup_xfer(sc->sc_intr_xfer, sc, &sc->sc_status, 1,
589 	    USBD_SHORT_XFER_OK, USSCANNER_TIMEOUT, usscanner_intr_cb);
590 	err = usbd_transfer(sc->sc_intr_xfer);
591 	if (err == USBD_IN_PROGRESS)
592 		return;
593 	xs->error = XS_DRIVER_STUFFUP;
594 }
595 
596 Static void
597 usscanner_sensecmd_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
598 {
599 	struct usscanner_softc *sc = priv;
600 	struct scsipi_xfer *xs = sc->sc_xs;
601 	usbd_status err;
602 
603 	DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status));
604 
605 #ifdef USSCANNER_DEBUG
606 	if (usscannerdebug > 15)
607 		xs->xs_periph->periph_flags |= 1; /* XXX 1 */
608 
609 	if (sc->sc_state != UAS_SENSECMD) {
610 		aprint_error_dev(sc->sc_dev, "!UAS_SENSECMD\n");
611 		xs->error = XS_DRIVER_STUFFUP;
612 		goto done;
613 	}
614 #endif
615 
616 	switch (status) {
617 	case USBD_NORMAL_COMPLETION:
618 		break;
619 	case USBD_TIMEOUT:
620 		xs->error = XS_TIMEOUT;
621 		goto done;
622 	default:
623 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
624 		goto done;
625 	}
626 
627 	sc->sc_state = UAS_SENSEDATA;
628 	usbd_setup_xfer(sc->sc_datain_xfer, sc, sc->sc_datain_buffer,
629 	    sizeof(xs->sense), USBD_SHORT_XFER_OK,
630 	    USSCANNER_TIMEOUT, usscanner_sensedata_cb);
631 	err = usbd_transfer(sc->sc_datain_xfer);
632 	if (err == USBD_IN_PROGRESS)
633 		return;
634 	xs->error = XS_DRIVER_STUFFUP;
635  done:
636 	usscanner_done(sc);
637 }
638 
639 Static void
640 usscanner_cmd_cb(struct usbd_xfer *xfer, void *priv, usbd_status status)
641 {
642 	struct usscanner_softc *sc = priv;
643 	struct scsipi_xfer *xs = sc->sc_xs;
644 	struct usbd_xfer *dxfer;
645 	usbd_status err;
646 
647 	DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status));
648 
649 #ifdef USSCANNER_DEBUG
650 	if (usscannerdebug > 15)
651 		xs->xs_periph->periph_flags |= 1;	/* XXX 1 */
652 
653 	if (sc->sc_state != UAS_CMD) {
654 		aprint_error_dev(sc->sc_dev, "!UAS_CMD\n");
655 		xs->error = XS_DRIVER_STUFFUP;
656 		goto done;
657 	}
658 #endif
659 
660 	switch (status) {
661 	case USBD_NORMAL_COMPLETION:
662 		break;
663 	case USBD_TIMEOUT:
664 		xs->error = XS_TIMEOUT;
665 		goto done;
666 	case USBD_CANCELLED:
667 		goto done;
668 	default:
669 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
670 		goto done;
671 	}
672 
673 	if (xs->datalen == 0) {
674 		DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n"));
675 		xs->error = XS_NOERROR;
676 		goto done;
677 	}
678 
679 	if (xs->xs_control & XS_CTL_DATA_IN) {
680 		DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n",
681 			     xs->datalen));
682 		dxfer = sc->sc_datain_xfer;
683 	} else {
684 		DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n",
685 			     xs->datalen));
686 		dxfer = sc->sc_dataout_xfer;
687 	}
688 	sc->sc_state = UAS_DATA;
689 	usbd_setup_xfer(dxfer, sc, xs->data, xs->datalen,
690 	    USBD_SHORT_XFER_OK, xs->timeout, usscanner_data_cb);
691 	err = usbd_transfer(dxfer);
692 	if (err == USBD_IN_PROGRESS)
693 		return;
694 	xs->error = XS_DRIVER_STUFFUP;
695 
696  done:
697 	usscanner_done(sc);
698 }
699 
700 Static void
701 usscanner_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
702     void *arg)
703 {
704 	struct scsipi_xfer *xs;
705 	struct usscanner_softc *sc =
706 	    device_private(chan->chan_adapter->adapt_dev);
707 	usbd_status err;
708 
709 	switch (req) {
710 	case ADAPTER_REQ_RUN_XFER:
711 		xs = arg;
712 
713 		DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d "
714 		    "xs=%p cmd=0x%02x datalen=%d (quirks=%#x, poll=%d)\n",
715 		    device_xname(sc->sc_dev),
716 		    xs->xs_periph->periph_target, xs->xs_periph->periph_lun,
717 		    xs, xs->cmd->opcode, xs->datalen,
718 		    xs->xs_periph->periph_quirks,
719 		    xs->xs_control & XS_CTL_POLL));
720 
721 		if (sc->sc_dying) {
722 			xs->error = XS_DRIVER_STUFFUP;
723 			goto done;
724 		}
725 
726 #ifdef USSCANNER_DEBUG
727 		if (xs->xs_periph->periph_target != USSCANNER_SCSIID_DEVICE) {
728 			DPRINTF(("%s: wrong SCSI ID %d\n",
729 			    device_xname(sc->sc_dev),
730 			    xs->xs_periph->periph_target));
731 			xs->error = XS_DRIVER_STUFFUP;
732 			goto done;
733 		}
734 		if (sc->sc_state != UAS_IDLE) {
735 			printf("%s: !UAS_IDLE\n", device_xname(sc->sc_dev));
736 			xs->error = XS_DRIVER_STUFFUP;
737 			goto done;
738 		}
739 #endif
740 
741 		if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) {
742 			aprint_normal_dev(sc->sc_dev,
743 			    "usscanner_scsipi_request: large datalen, %d\n",
744 			    xs->datalen);
745 			xs->error = XS_DRIVER_STUFFUP;
746 			goto done;
747 		}
748 
749 		DPRINTFN(4, ("%s: usscanner_scsipi_request: async cmdlen=%d"
750 		    " datalen=%d\n", device_xname(sc->sc_dev), xs->cmdlen,
751 		    xs->datalen));
752 		sc->sc_state = UAS_CMD;
753 		sc->sc_xs = xs;
754 		memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
755 		usbd_setup_xfer(sc->sc_cmd_xfer, sc, sc->sc_cmd_buffer,
756 		    xs->cmdlen, 0, USSCANNER_TIMEOUT, usscanner_cmd_cb);
757 		err = usbd_transfer(sc->sc_cmd_xfer);
758 		if (err != USBD_IN_PROGRESS) {
759 			xs->error = XS_DRIVER_STUFFUP;
760 			goto done;
761 		}
762 
763 		return;
764 
765 
766  done:
767 		sc->sc_state = UAS_IDLE;
768 		KERNEL_LOCK(1, curlwp);
769 		scsipi_done(xs);
770 		KERNEL_UNLOCK_ONE(curlwp);
771 		return;
772 
773 	case ADAPTER_REQ_GROW_RESOURCES:
774 		/* XXX Not supported. */
775 		return;
776 	case ADAPTER_REQ_SET_XFER_MODE:
777 		/* XXX Not supported. */
778 		return;
779 	}
780 
781 }
782