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