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