1 /*	$OpenBSD: usscanner.c,v 1.9 2004/07/08 22:18:45 deraadt Exp $	*/
2 /*	$NetBSD: usscanner.c,v 1.6 2001/01/23 14:04:14 augustss Exp $	*/
3 
4 /*
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) and LLoyd Parkes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * This driver is partly based on information taken from the Linux driver
42  * by John Fremlin, Oliver Neukum, and Jeremy Hall.
43  */
44 /*
45  * Protocol:
46  * Send raw SCSI command on the bulk-out pipe.
47  * If output command then
48  *     send further data on the bulk-out pipe
49  * else if input command then
50  *     read data on the bulk-in pipe
51  * else
52  *     don't do anything.
53  * Read status byte on the interrupt pipe (which doesn't seem to be
54  * an interrupt pipe at all).  This operation sometimes times out.
55  */
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.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 #if defined(__NetBSD__)
72 #include <sys/scsiio.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 #elif defined(__OpenBSD__)
78 #include <scsi/scsi_all.h>
79 #include <scsi/scsiconf.h>
80 #include <machine/bus.h>
81 #endif
82 
83 #ifdef USSCANNER_DEBUG
84 #define DPRINTF(x)	do { if (usscannerdebug) logprintf x; } while (0)
85 #define DPRINTFN(n,x)	do { if (usscannerdebug>(n)) logprintf x; } while (0)
86 int	usscannerdebug = 0;
87 #else
88 #define DPRINTF(x)
89 #define DPRINTFN(n,x)
90 #endif
91 
92 #define XS_CTL_DATA_IN		SCSI_DATA_IN
93 #define XS_CTL_DATA_OUT		SCSI_DATA_OUT
94 #define scsipi_adapter		scsi_adapter
95 #define scsipi_cmd		scsi_cmd
96 #define scsipi_device		scsi_device
97 #define scsipi_done		scsi_done
98 #define scsipi_link		scsi_link
99 #define scsipi_minphys		scsi_minphys
100 #define scsipi_sense		scsi_sense
101 #define scsipi_xfer		scsi_xfer
102 #define show_scsipi_xs          show_scsi_xs
103 #define show_scsipi_cmd         show_scsi_cmd
104 #define xs_control		flags
105 #define xs_status		status
106 #define XS_STS_DONE		ITSDONE
107 #define XS_CTL_POLL		SCSI_POLL
108 
109 #define USSCANNER_CONFIG_NO		1
110 #define USSCANNER_IFACE_IDX		0
111 
112 #define USSCANNER_SCSIID_HOST	0x00
113 #define USSCANNER_SCSIID_DEVICE	0x01
114 
115 #define USSCANNER_MAX_TRANSFER_SIZE	MAXBSIZE
116 
117 #define USSCANNER_TIMEOUT 2000
118 
119 Static struct scsipi_device usscanner_dev =
120 {
121 	NULL,			/* Use default error handler */
122 	NULL,			/* have a queue, served by this */
123 	NULL,			/* have no async handler */
124 	NULL,			/* Use default 'done' routine */
125 };
126 
127 struct usscanner_softc {
128  	USBBASEDEVICE		sc_dev;
129 	usbd_device_handle	sc_udev;
130 	usbd_interface_handle	sc_iface;
131 
132 	int			sc_in_addr;
133 	usbd_pipe_handle	sc_in_pipe;
134 
135 	int			sc_intr_addr;
136 	usbd_pipe_handle	sc_intr_pipe;
137 	usbd_xfer_handle	sc_intr_xfer;
138 	u_char			sc_status;
139 
140 	int			sc_out_addr;
141 	usbd_pipe_handle	sc_out_pipe;
142 
143 	usbd_xfer_handle	sc_cmd_xfer;
144 	void			*sc_cmd_buffer;
145 	usbd_xfer_handle	sc_data_xfer;
146 	void			*sc_data_buffer;
147 
148 	int			sc_state;
149 #define UAS_IDLE	0
150 #define UAS_CMD		1
151 #define UAS_DATA	2
152 #define UAS_SENSECMD	3
153 #define UAS_SENSEDATA	4
154 #define UAS_STATUS	5
155 
156 	struct scsipi_xfer	*sc_xs;
157 
158 	device_ptr_t		sc_child;	/* child device, for detach */
159 
160 	struct scsipi_link	sc_link;
161 #if defined(__NetBSD__)
162 	struct atapi_adapter	sc_atapi_adapter;
163 #define sc_adapter sc_atapi_adapter._generic
164 #else
165 	struct scsi_adapter	sc_atapi_adapter;
166 #define sc_adapter sc_atapi_adapter
167 #endif
168 
169 	int			sc_refcnt;
170 	char			sc_dying;
171 };
172 
173 
174 Static void usscanner_cleanup(struct usscanner_softc *sc);
175 Static int usscanner_scsipi_cmd(struct scsipi_xfer *xs);
176 Static void usscanner_scsipi_minphys(struct buf *bp);
177 Static void usscanner_done(struct usscanner_softc *sc);
178 Static void usscanner_sense(struct usscanner_softc *sc);
179 typedef void callback(usbd_xfer_handle, usbd_private_handle, usbd_status);
180 Static callback usscanner_intr_cb;
181 Static callback usscanner_cmd_cb;
182 Static callback usscanner_data_cb;
183 Static callback usscanner_sensecmd_cb;
184 Static callback usscanner_sensedata_cb;
185 
186 USB_DECLARE_DRIVER(usscanner);
187 
USB_MATCH(usscanner)188 USB_MATCH(usscanner)
189 {
190 	USB_MATCH_START(usscanner, uaa);
191 
192 	DPRINTFN(50,("usscanner_match\n"));
193 
194 	if (uaa->iface != NULL)
195 		return (UMATCH_NONE);
196 
197 	if (uaa->vendor == USB_VENDOR_HP &&
198 	    uaa->product == USB_PRODUCT_HP_5300C)
199 		return (UMATCH_VENDOR_PRODUCT);
200 	else
201 		return (UMATCH_NONE);
202 }
203 
USB_ATTACH(usscanner)204 USB_ATTACH(usscanner)
205 {
206 	USB_ATTACH_START(usscanner, sc, uaa);
207 	usbd_device_handle	dev = uaa->device;
208 	usbd_interface_handle	iface;
209 	char			devinfo[1024];
210 	usbd_status		err;
211 	usb_endpoint_descriptor_t *ed;
212 	u_int8_t		epcount;
213 	int			i;
214 
215 	DPRINTFN(10,("usscanner_attach: sc=%p\n", sc));
216 
217 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
218 	USB_ATTACH_SETUP;
219 	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
220 
221 	err = usbd_set_config_no(dev, USSCANNER_CONFIG_NO, 1);
222 	if (err) {
223 		printf("%s: setting config no failed\n",
224 		    USBDEVNAME(sc->sc_dev));
225 		USB_ATTACH_ERROR_RETURN;
226 	}
227 
228 	err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface);
229 	if (err) {
230 		printf("%s: getting interface handle failed\n",
231 		    USBDEVNAME(sc->sc_dev));
232 		USB_ATTACH_ERROR_RETURN;
233 	}
234 
235 	sc->sc_udev = dev;
236 	sc->sc_iface = iface;
237 
238 	epcount = 0;
239 	(void)usbd_endpoint_count(iface, &epcount);
240 
241 	sc->sc_in_addr = -1;
242 	sc->sc_intr_addr = -1;
243 	sc->sc_out_addr = -1;
244 	for (i = 0; i < epcount; i++) {
245 		ed = usbd_interface2endpoint_descriptor(iface, i);
246 		if (ed == NULL) {
247 			printf("%s: couldn't get ep %d\n",
248 			    USBDEVNAME(sc->sc_dev), i);
249 			USB_ATTACH_ERROR_RETURN;
250 		}
251 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
252 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
253 			sc->sc_in_addr = ed->bEndpointAddress;
254 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
255 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
256 			sc->sc_intr_addr = ed->bEndpointAddress;
257 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
258 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
259 			sc->sc_out_addr = ed->bEndpointAddress;
260 		}
261 	}
262 	if (sc->sc_in_addr == -1 || sc->sc_intr_addr == -1 ||
263 	    sc->sc_out_addr == -1) {
264 		printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
265 		USB_ATTACH_ERROR_RETURN;
266 	}
267 
268 	err = usbd_open_pipe(sc->sc_iface, sc->sc_in_addr,
269 			     USBD_EXCLUSIVE_USE, &sc->sc_in_pipe);
270 	if (err) {
271 		printf("%s: open in pipe failed, err=%d\n",
272 		       USBDEVNAME(sc->sc_dev), err);
273 		USB_ATTACH_ERROR_RETURN;
274 	}
275 
276 	/* The interrupt endpoint must be opened as a normal pipe. */
277 	err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr,
278 			     USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe);
279 
280 	if (err) {
281 		printf("%s: open intr pipe failed, err=%d\n",
282 		       USBDEVNAME(sc->sc_dev), err);
283 		usscanner_cleanup(sc);
284 		USB_ATTACH_ERROR_RETURN;
285 	}
286 	err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr,
287 			     USBD_EXCLUSIVE_USE, &sc->sc_out_pipe);
288 	if (err) {
289 		printf("%s: open out pipe failed, err=%d\n",
290 		       USBDEVNAME(sc->sc_dev), err);
291 		usscanner_cleanup(sc);
292 		USB_ATTACH_ERROR_RETURN;
293 	}
294 
295 	sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device);
296 	if (sc->sc_cmd_xfer == NULL) {
297 		printf("%s: alloc cmd xfer failed, err=%d\n",
298 		       USBDEVNAME(sc->sc_dev), err);
299 		usscanner_cleanup(sc);
300 		USB_ATTACH_ERROR_RETURN;
301 	}
302 
303 	/* XXX too big */
304 	sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer,
305 					     USSCANNER_MAX_TRANSFER_SIZE);
306 	if (sc->sc_cmd_buffer == NULL) {
307 		printf("%s: alloc cmd buffer failed, err=%d\n",
308 		       USBDEVNAME(sc->sc_dev), err);
309 		usscanner_cleanup(sc);
310 		USB_ATTACH_ERROR_RETURN;
311 	}
312 
313 	sc->sc_intr_xfer = usbd_alloc_xfer (uaa->device);
314 	if (sc->sc_intr_xfer == NULL) {
315 	  printf("%s: alloc intr xfer failed, err=%d\n",
316 		 USBDEVNAME(sc->sc_dev), err);
317 	  usscanner_cleanup(sc);
318 	  USB_ATTACH_ERROR_RETURN;
319         }
320 
321 	sc->sc_data_xfer = usbd_alloc_xfer(uaa->device);
322 	if (sc->sc_data_xfer == NULL) {
323 		printf("%s: alloc data xfer failed, err=%d\n",
324 		       USBDEVNAME(sc->sc_dev), err);
325 		usscanner_cleanup(sc);
326 		USB_ATTACH_ERROR_RETURN;
327 	}
328 	sc->sc_data_buffer = usbd_alloc_buffer(sc->sc_data_xfer,
329 					      USSCANNER_MAX_TRANSFER_SIZE);
330 	if (sc->sc_data_buffer == NULL) {
331 		printf("%s: alloc data buffer failed, err=%d\n",
332 		       USBDEVNAME(sc->sc_dev), err);
333 		usscanner_cleanup(sc);
334 		USB_ATTACH_ERROR_RETURN;
335 	}
336 
337 	/*
338 	 * Fill in the adapter.
339 	 */
340 	sc->sc_adapter.scsipi_cmd = usscanner_scsipi_cmd;
341 	sc->sc_adapter.scsipi_minphys = usscanner_scsipi_minphys;
342 
343 	/*
344 	 * fill in the prototype scsipi_link.
345 	 */
346 #if defined(__NetBSD__)
347 	sc->sc_link.type = BUS_SCSI;
348 #endif
349 #if defined(__OpenBSD__)
350 	sc->sc_link.flags &= ~SDEV_ATAPI;
351 	sc->sc_link.adapter_buswidth = 2;
352 	sc->sc_link.adapter_target = USSCANNER_SCSIID_HOST;
353 #endif
354 
355 	sc->sc_link.adapter_softc = sc;
356 	sc->sc_link.adapter = &sc->sc_adapter;
357 	sc->sc_link.device = &usscanner_dev;
358 	sc->sc_link.openings = 1;
359 #if defined(__NetBSD__)
360 	sc->sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE;
361 	sc->sc_link.scsipi_scsi.adapter_target = USSCANNER_SCSIID_HOST;
362 	sc->sc_link.scsipi_scsi.max_target = USSCANNER_SCSIID_DEVICE;
363 	sc->sc_link.scsipi_scsi.max_lun = 0;
364 #endif
365 
366 	sc->sc_child = config_found(&sc->sc_dev, &sc->sc_link, scsiprint);
367 
368 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
369 			   USBDEV(sc->sc_dev));
370 
371 	DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev));
372 
373 	USB_ATTACH_SUCCESS_RETURN;
374 }
375 
USB_DETACH(usscanner)376 USB_DETACH(usscanner)
377 {
378 	USB_DETACH_START(usscanner, sc);
379 	int rv, s;
380 
381 	DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
382 
383 	sc->sc_dying = 1;
384 	/* Abort all pipes.  Causes processes waiting for transfer to wake. */
385 	if (sc->sc_in_pipe != NULL)
386 		usbd_abort_pipe(sc->sc_in_pipe);
387 	if (sc->sc_intr_pipe != NULL)
388 		usbd_abort_pipe(sc->sc_intr_pipe);
389 	if (sc->sc_out_pipe != NULL)
390 		usbd_abort_pipe(sc->sc_out_pipe);
391 
392 	s = splusb();
393 	if (--sc->sc_refcnt >= 0) {
394 		/* Wait for processes to go away. */
395 		usb_detach_wait(USBDEV(sc->sc_dev));
396 	}
397 	splx(s);
398 
399 	if (sc->sc_child != NULL)
400 		rv = config_detach(sc->sc_child, flags);
401 	else
402 		rv = 0;
403 
404 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
405 			   USBDEV(sc->sc_dev));
406 
407 	return (rv);
408 }
409 
410 Static void
usscanner_cleanup(struct usscanner_softc * sc)411 usscanner_cleanup(struct usscanner_softc *sc)
412 {
413 	if (sc->sc_in_pipe != NULL) {
414 		usbd_close_pipe(sc->sc_in_pipe);
415 		sc->sc_in_pipe = NULL;
416 	}
417 	if (sc->sc_intr_pipe != NULL) {
418 		usbd_close_pipe(sc->sc_intr_pipe);
419 		sc->sc_intr_pipe = NULL;
420 	}
421 	if (sc->sc_out_pipe != NULL) {
422 		usbd_close_pipe(sc->sc_out_pipe);
423 		sc->sc_out_pipe = NULL;
424 	}
425 	if (sc->sc_cmd_xfer != NULL) {
426 		usbd_free_xfer(sc->sc_cmd_xfer);
427 		sc->sc_cmd_xfer = NULL;
428 	}
429 	if (sc->sc_data_xfer != NULL) {
430 		usbd_free_xfer(sc->sc_data_xfer);
431 		sc->sc_data_xfer = NULL;
432 	}
433 }
434 
435 int
usscanner_activate(device_ptr_t self,enum devact act)436 usscanner_activate(device_ptr_t self, enum devact act)
437 {
438 	struct usscanner_softc *sc = (struct usscanner_softc *)self;
439 
440 	switch (act) {
441 	case DVACT_ACTIVATE:
442 		return (EOPNOTSUPP);
443 		break;
444 
445 	case DVACT_DEACTIVATE:
446 		sc->sc_dying = 1;
447 		break;
448 	}
449 	return (0);
450 }
451 
452 Static void
usscanner_scsipi_minphys(struct buf * bp)453 usscanner_scsipi_minphys(struct buf *bp)
454 {
455 	if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE)
456 		bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE;
457 	minphys(bp);
458 }
459 
460 Static void
usscanner_sense(struct usscanner_softc * sc)461 usscanner_sense(struct usscanner_softc *sc)
462 {
463 	struct scsipi_xfer *xs = sc->sc_xs;
464 	struct scsipi_link *sc_link = xs->sc_link;
465 	struct scsipi_sense sense_cmd;
466 	usbd_status err;
467 
468 	/* fetch sense data */
469 	memset(&sense_cmd, 0, sizeof(sense_cmd));
470 	sense_cmd.opcode = REQUEST_SENSE;
471 #if defined(__NetBSD__)
472 	sense_cmd.byte2 = sc_link->scsipi_scsi.lun << SCSI_CMD_LUN_SHIFT;
473 #else
474 	sense_cmd.byte2 = sc_link->lun << SCSI_CMD_LUN_SHIFT;
475 #endif
476 	sense_cmd.length = sizeof xs->sense;
477 
478 	sc->sc_state = UAS_SENSECMD;
479 	memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd);
480 	usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer,
481 	    sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT,
482 	    usscanner_sensecmd_cb);
483 	err = usbd_transfer(sc->sc_cmd_xfer);
484 	if (err == USBD_IN_PROGRESS)
485 		return;
486 
487 	xs->error = XS_DRIVER_STUFFUP;
488 	usscanner_done(sc);
489 }
490 
491 Static void
usscanner_intr_cb(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)492 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
493 		 usbd_status status)
494 {
495 	struct usscanner_softc *sc = priv;
496 	int s;
497 
498 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
499 
500 #ifdef USSCANNER_DEBUG
501 	if (sc->sc_state != UAS_STATUS) {
502 		printf("%s: !UAS_STATUS\n", USBDEVNAME(sc->sc_dev));
503 	}
504 	if (sc->sc_status != 0) {
505 		printf("%s: status byte=0x%02x\n", USBDEVNAME(sc->sc_dev), sc->sc_status);
506 	}
507 #endif
508 	/* XXX what should we do on non-0 status */
509 
510 	sc->sc_state = UAS_IDLE;
511 
512 	sc->sc_xs->xs_status |= XS_STS_DONE;
513 	s = splbio();
514 	scsipi_done(sc->sc_xs);
515 	splx(s);
516 }
517 
518 Static void
usscanner_data_cb(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)519 usscanner_data_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
520 		 usbd_status status)
521 {
522 	struct usscanner_softc *sc = priv;
523 	struct scsipi_xfer *xs = sc->sc_xs;
524 	u_int32_t len;
525 
526 	DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
527 
528 #ifdef USSCANNER_DEBUG
529 	if (sc->sc_state != UAS_DATA) {
530 		printf("%s: !UAS_DATA\n", USBDEVNAME(sc->sc_dev));
531 	}
532 #endif
533 
534 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
535 
536 	xs->resid = xs->datalen - len;
537 
538 	switch (status) {
539 	case USBD_NORMAL_COMPLETION:
540 		if (xs->xs_control & XS_CTL_DATA_IN)
541 			memcpy(xs->data, sc->sc_data_buffer, len);
542 		xs->error = XS_NOERROR;
543 		break;
544 	case USBD_TIMEOUT:
545 		xs->error = XS_TIMEOUT;
546 		break;
547 	case USBD_CANCELLED:
548 		if (xs->error == XS_SENSE) {
549 			usscanner_sense(sc);
550 			return;
551 		}
552 		break;
553 	default:
554 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
555 		break;
556 	}
557 	usscanner_done(sc);
558 }
559 
560 Static void
usscanner_sensedata_cb(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)561 usscanner_sensedata_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
562 		       usbd_status status)
563 {
564 	struct usscanner_softc *sc = priv;
565 	struct scsipi_xfer *xs = sc->sc_xs;
566 	u_int32_t len;
567 
568 	DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status));
569 
570 #ifdef USSCANNER_DEBUG
571 	if (sc->sc_state != UAS_SENSEDATA) {
572 		printf("%s: !UAS_SENSEDATA\n", USBDEVNAME(sc->sc_dev));
573 	}
574 #endif
575 
576 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
577 
578 	switch (status) {
579 	case USBD_NORMAL_COMPLETION:
580 		memcpy(&xs->sense, sc->sc_data_buffer, len);
581 		if (len < sizeof xs->sense)
582 			xs->error = XS_SHORTSENSE;
583 		break;
584 	case USBD_TIMEOUT:
585 		xs->error = XS_TIMEOUT;
586 		break;
587 	case USBD_CANCELLED:
588 		xs->error = XS_RESET;
589 		break;
590 	default:
591 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
592 		break;
593 	}
594 	usscanner_done(sc);
595 }
596 
597 Static void
usscanner_done(struct usscanner_softc * sc)598 usscanner_done(struct usscanner_softc *sc)
599 {
600 	struct scsipi_xfer *xs = sc->sc_xs;
601 	usbd_status err;
602 
603 	DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error));
604 
605 	sc->sc_state = UAS_STATUS;
606 	usbd_setup_xfer(sc->sc_intr_xfer, sc->sc_intr_pipe, sc, &sc->sc_status,
607 	    1, USBD_SHORT_XFER_OK | USBD_NO_COPY,
608 	    USSCANNER_TIMEOUT, usscanner_intr_cb);
609 	err = usbd_transfer(sc->sc_intr_xfer);
610 	if (err == USBD_IN_PROGRESS)
611 		return;
612 	xs->error = XS_DRIVER_STUFFUP;
613 }
614 
615 Static void
usscanner_sensecmd_cb(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)616 usscanner_sensecmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
617 		      usbd_status status)
618 {
619 	struct usscanner_softc *sc = priv;
620 	struct scsipi_xfer *xs = sc->sc_xs;
621 	usbd_status err;
622 
623 	DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status));
624 
625 #ifdef USSCANNER_DEBUG
626 #ifdef notyet
627 	if (usscannerdebug > 15)
628 		xs->sc_link->flags |= DEBUGLEVEL;
629 #endif
630 
631 	if (sc->sc_state != UAS_SENSECMD) {
632 		printf("%s: !UAS_SENSECMD\n", USBDEVNAME(sc->sc_dev));
633 		xs->error = XS_DRIVER_STUFFUP;
634 		goto done;
635 	}
636 #endif
637 
638 	switch (status) {
639 	case USBD_NORMAL_COMPLETION:
640 		break;
641 	case USBD_TIMEOUT:
642 		xs->error = XS_TIMEOUT;
643 		goto done;
644 	default:
645 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
646 		goto done;
647 	}
648 
649 	sc->sc_state = UAS_SENSEDATA;
650 	usbd_setup_xfer(sc->sc_data_xfer, sc->sc_in_pipe, sc,
651 	    sc->sc_data_buffer,
652 	    sizeof xs->sense, USBD_SHORT_XFER_OK | USBD_NO_COPY,
653 	    USSCANNER_TIMEOUT, usscanner_sensedata_cb);
654 	err = usbd_transfer(sc->sc_data_xfer);
655 	if (err == USBD_IN_PROGRESS)
656 		return;
657 	xs->error = XS_DRIVER_STUFFUP;
658  done:
659 	usscanner_done(sc);
660 }
661 
662 Static void
usscanner_cmd_cb(usbd_xfer_handle xfer,usbd_private_handle priv,usbd_status status)663 usscanner_cmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
664 		 usbd_status status)
665 {
666 	struct usscanner_softc *sc = priv;
667 	struct scsipi_xfer *xs = sc->sc_xs;
668 	usbd_pipe_handle pipe;
669 	usbd_status err;
670 
671 	DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status));
672 
673 #ifdef USSCANNER_DEBUG
674 #ifdef notyet
675 	if (usscannerdebug > 15)
676 		xs->sc_link->flags |= DEBUGLEVEL;
677 #endif
678 
679 	if (sc->sc_state != UAS_CMD) {
680 		printf("%s: !UAS_CMD\n", USBDEVNAME(sc->sc_dev));
681 		xs->error = XS_DRIVER_STUFFUP;
682 		goto done;
683 	}
684 #endif
685 
686 	switch (status) {
687 	case USBD_NORMAL_COMPLETION:
688 		break;
689 	case USBD_TIMEOUT:
690 		xs->error = XS_TIMEOUT;
691 		goto done;
692 	case USBD_CANCELLED:
693 		goto done;
694 	default:
695 		xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
696 		goto done;
697 	}
698 
699 	if (xs->datalen == 0) {
700 		DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n"));
701 		xs->error = XS_NOERROR;
702 		goto done;
703 	}
704 
705 	if (xs->xs_control & XS_CTL_DATA_IN) {
706 		DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n",
707 			     xs->datalen));
708 		pipe = sc->sc_in_pipe;
709 	} else {
710 		DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n",
711 			     xs->datalen));
712 		memcpy(sc->sc_data_buffer, xs->data, xs->datalen);
713 		pipe = sc->sc_out_pipe;
714 	}
715 	sc->sc_state = UAS_DATA;
716 	usbd_setup_xfer(sc->sc_data_xfer, pipe, sc, sc->sc_data_buffer,
717 	    xs->datalen, USBD_SHORT_XFER_OK | USBD_NO_COPY,
718 	    xs->timeout, usscanner_data_cb);
719 	err = usbd_transfer(sc->sc_data_xfer);
720 	if (err == USBD_IN_PROGRESS)
721 		return;
722 	xs->error = XS_DRIVER_STUFFUP;
723 
724  done:
725 	usscanner_done(sc);
726 }
727 
728 Static int
usscanner_scsipi_cmd(struct scsipi_xfer * xs)729 usscanner_scsipi_cmd(struct scsipi_xfer *xs)
730 {
731 	struct scsipi_link *sc_link = xs->sc_link;
732 	struct usscanner_softc *sc = sc_link->adapter_softc;
733 	usbd_status err;
734 
735 #ifdef notyet
736 	DPRINTFN(8, ("%s: usscanner_scsi_cmd: %d:%d "
737 	    "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
738 	    USBDEVNAME(sc->sc_dev),
739 	    sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun,
740 	    xs, xs->cmd->opcode, xs->datalen,
741 	    sc_link->quirks, xs->xs_control & XS_CTL_POLL));
742 #endif
743 
744 	if (sc->sc_dying) {
745 		xs->error = XS_DRIVER_STUFFUP;
746 		goto done;
747 	}
748 
749 #ifdef USSCANNER_DEBUG
750 #ifdef notyet
751 	if (sc_link->scsipi_scsi.target != USSCANNER_SCSIID_DEVICE) {
752 		DPRINTF(("%s: wrong SCSI ID %d\n", USBDEVNAME(sc->sc_dev),
753 		    sc_link->scsipi_scsi.target));
754 		xs->error = XS_DRIVER_STUFFUP;
755 		goto done;
756 	}
757 #endif
758 	if (sc->sc_state != UAS_IDLE) {
759 		printf("%s: !UAS_IDLE\n", USBDEVNAME(sc->sc_dev));
760 		xs->error = XS_DRIVER_STUFFUP;
761 		goto done;
762 	}
763 #endif
764 
765 	if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) {
766 		printf("umass_cmd: large datalen, %d\n", xs->datalen);
767 		xs->error = XS_DRIVER_STUFFUP;
768 		goto done;
769 	}
770 
771 	DPRINTFN(4, ("usscanner_scsi_cmd: async cmdlen=%d datalen=%d\n",
772 		    xs->cmdlen, xs->datalen));
773 	sc->sc_state = UAS_CMD;
774 	sc->sc_xs = xs;
775 	memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
776 	usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer,
777 	    xs->cmdlen, USBD_NO_COPY, USSCANNER_TIMEOUT, usscanner_cmd_cb);
778 	err = usbd_transfer(sc->sc_cmd_xfer);
779 	if (err != USBD_IN_PROGRESS) {
780 		xs->error = XS_DRIVER_STUFFUP;
781 		goto done;
782 	}
783 
784 	return (SUCCESSFULLY_QUEUED);
785 
786 
787  done:
788 	sc->sc_state = UAS_IDLE;
789 	xs->xs_status |= XS_STS_DONE;
790 	scsipi_done(xs);
791 	if (xs->xs_control & XS_CTL_POLL)
792 		return (COMPLETE);
793 	else
794 		return (SUCCESSFULLY_QUEUED);
795 }
796