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