xref: /netbsd/sys/dev/usb/usscanner.c (revision bf9ec67e)
1 /*	$NetBSD: usscanner.c,v 1.9 2001/11/13 06:24:57 lukem 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.9 2001/11/13 06:24:57 lukem 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_all.h>
75 #include <dev/scsipi/scsipi_all.h>
76 #include <dev/scsipi/scsiconf.h>
77 #include <dev/scsipi/atapiconf.h>
78 
79 #ifdef USSCANNER_DEBUG
80 #define DPRINTF(x)	if (usscannerdebug) logprintf x
81 #define DPRINTFN(n,x)	if (usscannerdebug>(n)) logprintf x
82 int	usscannerdebug = 0;
83 #else
84 #define DPRINTF(x)
85 #define DPRINTFN(n,x)
86 #endif
87 
88 
89 #define USSCANNER_CONFIG_NO		1
90 #define USSCANNER_IFACE_IDX		0
91 
92 #define USSCANNER_SCSIID_HOST	0x00
93 #define USSCANNER_SCSIID_DEVICE	0x01
94 
95 #define USSCANNER_MAX_TRANSFER_SIZE	MAXBSIZE
96 
97 #define USSCANNER_TIMEOUT 2000
98 
99 struct usscanner_softc {
100  	USBBASEDEVICE		sc_dev;
101 	usbd_device_handle	sc_udev;
102 	usbd_interface_handle	sc_iface;
103 
104 	int			sc_in_addr;
105 	usbd_pipe_handle	sc_in_pipe;
106 
107 	int			sc_intr_addr;
108 	usbd_pipe_handle	sc_intr_pipe;
109 	usbd_xfer_handle	sc_intr_xfer;
110 	u_char			sc_status;
111 
112 	int			sc_out_addr;
113 	usbd_pipe_handle	sc_out_pipe;
114 
115 	usbd_xfer_handle	sc_cmd_xfer;
116 	void			*sc_cmd_buffer;
117 	usbd_xfer_handle	sc_data_xfer;
118 	void			*sc_data_buffer;
119 
120 	int			sc_state;
121 #define UAS_IDLE	0
122 #define UAS_CMD		1
123 #define UAS_DATA	2
124 #define UAS_SENSECMD	3
125 #define UAS_SENSEDATA	4
126 #define UAS_STATUS	5
127 
128 	struct scsipi_xfer	*sc_xs;
129 
130 	device_ptr_t		sc_child;	/* child device, for detach */
131 
132 	struct scsipi_adapter	sc_adapter;
133 	struct scsipi_channel	sc_channel;
134 
135 	int			sc_refcnt;
136 	char			sc_dying;
137 };
138 
139 
140 Static void usscanner_cleanup(struct usscanner_softc *sc);
141 Static void usscanner_scsipi_request(struct scsipi_channel *chan,
142 				scsipi_adapter_req_t req, void *arg);
143 Static void usscanner_scsipi_minphys(struct buf *bp);
144 Static void usscanner_done(struct usscanner_softc *sc);
145 Static void usscanner_sense(struct usscanner_softc *sc);
146 typedef void callback(usbd_xfer_handle, usbd_private_handle, usbd_status);
147 Static callback usscanner_intr_cb;
148 Static callback usscanner_cmd_cb;
149 Static callback usscanner_data_cb;
150 Static callback usscanner_sensecmd_cb;
151 Static callback usscanner_sensedata_cb;
152 
153 USB_DECLARE_DRIVER(usscanner);
154 
155 USB_MATCH(usscanner)
156 {
157 	USB_MATCH_START(usscanner, uaa);
158 
159 	DPRINTFN(50,("usscanner_match\n"));
160 
161 	if (uaa->iface != NULL)
162 		return (UMATCH_NONE);
163 
164 	if (uaa->vendor == USB_VENDOR_HP &&
165 	    uaa->product == USB_PRODUCT_HP_5300C)
166 		return (UMATCH_VENDOR_PRODUCT);
167 	else
168 		return (UMATCH_NONE);
169 }
170 
171 USB_ATTACH(usscanner)
172 {
173 	USB_ATTACH_START(usscanner, sc, uaa);
174 	usbd_device_handle	dev = uaa->device;
175 	usbd_interface_handle	iface;
176 	char			devinfo[1024];
177 	usbd_status		err;
178 	usb_endpoint_descriptor_t *ed;
179 	u_int8_t		epcount;
180 	int			i;
181 
182 	DPRINTFN(10,("usscanner_attach: sc=%p\n", sc));
183 
184 	usbd_devinfo(dev, 0, devinfo);
185 	USB_ATTACH_SETUP;
186 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
187 
188 	err = usbd_set_config_no(dev, USSCANNER_CONFIG_NO, 1);
189 	if (err) {
190 		printf("%s: setting config no failed\n",
191 		    USBDEVNAME(sc->sc_dev));
192 		USB_ATTACH_ERROR_RETURN;
193 	}
194 
195 	err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface);
196 	if (err) {
197 		printf("%s: getting interface handle failed\n",
198 		    USBDEVNAME(sc->sc_dev));
199 		USB_ATTACH_ERROR_RETURN;
200 	}
201 
202 	sc->sc_udev = dev;
203 	sc->sc_iface = iface;
204 
205 	epcount = 0;
206 	(void)usbd_endpoint_count(iface, &epcount);
207 
208 	sc->sc_in_addr = -1;
209 	sc->sc_intr_addr = -1;
210 	sc->sc_out_addr = -1;
211 	for (i = 0; i < epcount; i++) {
212 		ed = usbd_interface2endpoint_descriptor(iface, i);
213 		if (ed == NULL) {
214 			printf("%s: couldn't get ep %d\n",
215 			    USBDEVNAME(sc->sc_dev), i);
216 			USB_ATTACH_ERROR_RETURN;
217 		}
218 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
219 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
220 			sc->sc_in_addr = ed->bEndpointAddress;
221 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
222 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
223 			sc->sc_intr_addr = ed->bEndpointAddress;
224 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
225 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
226 			sc->sc_out_addr = ed->bEndpointAddress;
227 		}
228 	}
229 	if (sc->sc_in_addr == -1 || sc->sc_intr_addr == -1 ||
230 	    sc->sc_out_addr == -1) {
231 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
232 		USB_ATTACH_ERROR_RETURN;
233 	}
234 
235 	err = usbd_open_pipe(sc->sc_iface, sc->sc_in_addr,
236 			     USBD_EXCLUSIVE_USE, &sc->sc_in_pipe);
237 	if (err) {
238 		printf("%s: open in pipe failed, err=%d\n",
239 		       USBDEVNAME(sc->sc_dev), err);
240 		USB_ATTACH_ERROR_RETURN;
241 	}
242 
243 	/* The interrupt endpoint must be opened as a normal pipe. */
244 	err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr,
245 			     USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe);
246 
247 	if (err) {
248 		printf("%s: open intr pipe failed, err=%d\n",
249 		       USBDEVNAME(sc->sc_dev), err);
250 		usscanner_cleanup(sc);
251 		USB_ATTACH_ERROR_RETURN;
252 	}
253 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr,
254 			     USBD_EXCLUSIVE_USE, &sc->sc_out_pipe);
255 	if (err) {
256 		printf("%s: open out pipe failed, err=%d\n",
257 		       USBDEVNAME(sc->sc_dev), err);
258 		usscanner_cleanup(sc);
259 		USB_ATTACH_ERROR_RETURN;
260 	}
261 
262 	sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device);
263 	if (sc->sc_cmd_xfer == NULL) {
264 		printf("%s: alloc cmd xfer failed, err=%d\n",
265 		       USBDEVNAME(sc->sc_dev), err);
266 		usscanner_cleanup(sc);
267 		USB_ATTACH_ERROR_RETURN;
268 	}
269 
270 	/* XXX too big */
271 	sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer,
272 					     USSCANNER_MAX_TRANSFER_SIZE);
273 	if (sc->sc_cmd_buffer == NULL) {
274 		printf("%s: alloc cmd buffer failed, err=%d\n",
275 		       USBDEVNAME(sc->sc_dev), err);
276 		usscanner_cleanup(sc);
277 		USB_ATTACH_ERROR_RETURN;
278 	}
279 
280 	sc->sc_intr_xfer = usbd_alloc_xfer (uaa->device);
281 	if (sc->sc_intr_xfer == NULL) {
282 	  printf("%s: alloc intr xfer failed, err=%d\n",
283 		 USBDEVNAME(sc->sc_dev), err);
284 	  usscanner_cleanup(sc);
285 	  USB_ATTACH_ERROR_RETURN;
286         }
287 
288 	sc->sc_data_xfer = usbd_alloc_xfer(uaa->device);
289 	if (sc->sc_data_xfer == NULL) {
290 		printf("%s: alloc data xfer failed, err=%d\n",
291 		       USBDEVNAME(sc->sc_dev), err);
292 		usscanner_cleanup(sc);
293 		USB_ATTACH_ERROR_RETURN;
294 	}
295 	sc->sc_data_buffer = usbd_alloc_buffer(sc->sc_data_xfer,
296 					      USSCANNER_MAX_TRANSFER_SIZE);
297 	if (sc->sc_data_buffer == NULL) {
298 		printf("%s: alloc data buffer failed, err=%d\n",
299 		       USBDEVNAME(sc->sc_dev), err);
300 		usscanner_cleanup(sc);
301 		USB_ATTACH_ERROR_RETURN;
302 	}
303 
304 	/*
305 	 * Fill in the adapter.
306 	 */
307 	sc->sc_adapter.adapt_request = usscanner_scsipi_request;
308 	sc->sc_adapter.adapt_dev = &sc->sc_dev;
309 	sc->sc_adapter.adapt_nchannels = 1;
310 	sc->sc_adapter.adapt_openings = 1;
311 	sc->sc_adapter.adapt_max_periph = 1;
312 	sc->sc_adapter.adapt_minphys = usscanner_scsipi_minphys;
313 
314 	/*
315 	 * fill in the scsipi_channel.
316 	 */
317 	sc->sc_channel.chan_adapter = &sc->sc_adapter;
318 	sc->sc_channel.chan_bustype = &scsi_bustype;
319 	sc->sc_channel.chan_channel = 0;
320 	sc->sc_channel.chan_ntargets = USSCANNER_SCSIID_DEVICE + 1;
321 	sc->sc_channel.chan_nluns = 1;
322 	sc->sc_channel.chan_id = USSCANNER_SCSIID_HOST;
323 
324 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
325 			   USBDEV(sc->sc_dev));
326 
327 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_channel, scsiprint);
328 
329 	DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev));
330 
331 	USB_ATTACH_SUCCESS_RETURN;
332 }
333 
334 USB_DETACH(usscanner)
335 {
336 	USB_DETACH_START(usscanner, sc);
337 	int rv, s;
338 
339 	DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
340 
341 	sc->sc_dying = 1;
342 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
343 	if (sc->sc_in_pipe != NULL)
344 		usbd_abort_pipe(sc->sc_in_pipe);
345 	if (sc->sc_intr_pipe != NULL)
346 		usbd_abort_pipe(sc->sc_intr_pipe);
347 	if (sc->sc_out_pipe != NULL)
348 		usbd_abort_pipe(sc->sc_out_pipe);
349 
350 	s = splusb();
351 	if (--sc->sc_refcnt >= 0) {
352 		/* Wait for processes to go away. */
353 		usb_detach_wait(USBDEV(sc->sc_dev));
354 	}
355 	splx(s);
356 
357 	if (sc->sc_child != NULL)
358 		rv = config_detach(sc->sc_child, flags);
359 	else
360 		rv = 0;
361 
362 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
363 			   USBDEV(sc->sc_dev));
364 
365 	return (rv);
366 }
367 
368 Static void
369 usscanner_cleanup(struct usscanner_softc *sc)
370 {
371 	if (sc->sc_in_pipe != NULL) {
372 		usbd_close_pipe(sc->sc_in_pipe);
373 		sc->sc_in_pipe = NULL;
374 	}
375 	if (sc->sc_intr_pipe != NULL) {
376 		usbd_close_pipe(sc->sc_intr_pipe);
377 		sc->sc_intr_pipe = NULL;
378 	}
379 	if (sc->sc_out_pipe != NULL) {
380 		usbd_close_pipe(sc->sc_out_pipe);
381 		sc->sc_out_pipe = NULL;
382 	}
383 	if (sc->sc_cmd_xfer != NULL) {
384 		usbd_free_xfer(sc->sc_cmd_xfer);
385 		sc->sc_cmd_xfer = NULL;
386 	}
387 	if (sc->sc_data_xfer != NULL) {
388 		usbd_free_xfer(sc->sc_data_xfer);
389 		sc->sc_data_xfer = NULL;
390 	}
391 }
392 
393 int
394 usscanner_activate(device_ptr_t self, enum devact act)
395 {
396 	struct usscanner_softc *sc = (struct usscanner_softc *)self;
397 
398 	switch (act) {
399 	case DVACT_ACTIVATE:
400 		return (EOPNOTSUPP);
401 		break;
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 scsipi_sense sense_cmd;
424 	usbd_status err;
425 
426 	/* fetch sense data */
427 	memset(&sense_cmd, 0, sizeof(sense_cmd));
428 	sense_cmd.opcode = 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_scsi_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("umass_cmd: large datalen, %d\n", xs->datalen);
721 			xs->error = XS_DRIVER_STUFFUP;
722 			goto done;
723 		}
724 
725 		DPRINTFN(4, ("usscanner_scsi_cmd: async cmdlen=%d datalen=%d\n",
726 			    xs->cmdlen, xs->datalen));
727 		sc->sc_state = UAS_CMD;
728 		sc->sc_xs = xs;
729 		memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
730 		usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc,
731 		    sc->sc_cmd_buffer, xs->cmdlen, USBD_NO_COPY,
732 		    USSCANNER_TIMEOUT, usscanner_cmd_cb);
733 		err = usbd_transfer(sc->sc_cmd_xfer);
734 		if (err != USBD_IN_PROGRESS) {
735 			xs->error = XS_DRIVER_STUFFUP;
736 			goto done;
737 		}
738 
739 		return;
740 
741 
742  done:
743 		sc->sc_state = UAS_IDLE;
744 		scsipi_done(xs);
745 		return;
746 
747 	case ADAPTER_REQ_GROW_RESOURCES:
748 		/* XXX Not supported. */
749 		return;
750 	case ADAPTER_REQ_SET_XFER_MODE:
751 		/* XXX Not supported. */
752 		return;
753 	}
754 
755 }
756