usscanner.c revision 1.21 1 /* $NetBSD: usscanner.c,v 1.21 2006/11/26 20:14:41 martin 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.21 2006/11/26 20:14:41 martin 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
341 printf("%s: no scsibus configured, see usscanner(4) for details\n",
342 USBDEVNAME(sc->sc_dev));
343
344 USB_ATTACH_ERROR_RETURN;
345
346 #endif
347 }
348
349 USB_DETACH(usscanner)
350 {
351 USB_DETACH_START(usscanner, sc);
352 int rv, s;
353
354 DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags));
355
356 sc->sc_dying = 1;
357 /* Abort all pipes. Causes processes waiting for transfer to wake. */
358 if (sc->sc_in_pipe != NULL)
359 usbd_abort_pipe(sc->sc_in_pipe);
360 if (sc->sc_intr_pipe != NULL)
361 usbd_abort_pipe(sc->sc_intr_pipe);
362 if (sc->sc_out_pipe != NULL)
363 usbd_abort_pipe(sc->sc_out_pipe);
364
365 s = splusb();
366 if (--sc->sc_refcnt >= 0) {
367 /* Wait for processes to go away. */
368 usb_detach_wait(USBDEV(sc->sc_dev));
369 }
370 splx(s);
371
372 if (sc->sc_child != NULL)
373 rv = config_detach(sc->sc_child, flags);
374 else
375 rv = 0;
376
377 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
378 USBDEV(sc->sc_dev));
379
380 return (rv);
381 }
382
383 Static void
384 usscanner_cleanup(struct usscanner_softc *sc)
385 {
386 if (sc->sc_in_pipe != NULL) {
387 usbd_close_pipe(sc->sc_in_pipe);
388 sc->sc_in_pipe = NULL;
389 }
390 if (sc->sc_intr_pipe != NULL) {
391 usbd_close_pipe(sc->sc_intr_pipe);
392 sc->sc_intr_pipe = NULL;
393 }
394 if (sc->sc_out_pipe != NULL) {
395 usbd_close_pipe(sc->sc_out_pipe);
396 sc->sc_out_pipe = NULL;
397 }
398 if (sc->sc_cmd_xfer != NULL) {
399 usbd_free_xfer(sc->sc_cmd_xfer);
400 sc->sc_cmd_xfer = NULL;
401 }
402 if (sc->sc_data_xfer != NULL) {
403 usbd_free_xfer(sc->sc_data_xfer);
404 sc->sc_data_xfer = NULL;
405 }
406 }
407
408 int
409 usscanner_activate(device_ptr_t self, enum devact act)
410 {
411 struct usscanner_softc *sc = (struct usscanner_softc *)self;
412
413 switch (act) {
414 case DVACT_ACTIVATE:
415 return (EOPNOTSUPP);
416
417 case DVACT_DEACTIVATE:
418 sc->sc_dying = 1;
419 break;
420 }
421 return (0);
422 }
423
424 Static void
425 usscanner_scsipi_minphys(struct buf *bp)
426 {
427 if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE)
428 bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE;
429 minphys(bp);
430 }
431
432 Static void
433 usscanner_sense(struct usscanner_softc *sc)
434 {
435 struct scsipi_xfer *xs = sc->sc_xs;
436 struct scsipi_periph *periph = xs->xs_periph;
437 struct scsi_request_sense sense_cmd;
438 usbd_status err;
439
440 /* fetch sense data */
441 memset(&sense_cmd, 0, sizeof(sense_cmd));
442 sense_cmd.opcode = SCSI_REQUEST_SENSE;
443 sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT;
444 sense_cmd.length = sizeof xs->sense;
445
446 sc->sc_state = UAS_SENSECMD;
447 memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd);
448 usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer,
449 sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT,
450 usscanner_sensecmd_cb);
451 err = usbd_transfer(sc->sc_cmd_xfer);
452 if (err == USBD_IN_PROGRESS)
453 return;
454
455 xs->error = XS_DRIVER_STUFFUP;
456 usscanner_done(sc);
457 }
458
459 Static void
460 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
461 usbd_status status)
462 {
463 struct usscanner_softc *sc = priv;
464 int s;
465
466 DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
467
468 #ifdef USSCANNER_DEBUG
469 if (sc->sc_state != UAS_STATUS) {
470 printf("%s: !UAS_STATUS\n", USBDEVNAME(sc->sc_dev));
471 }
472 if (sc->sc_status != 0) {
473 printf("%s: status byte=0x%02x\n", USBDEVNAME(sc->sc_dev), sc->sc_status);
474 }
475 #endif
476 /* XXX what should we do on non-0 status */
477
478 sc->sc_state = UAS_IDLE;
479
480 s = splbio();
481 scsipi_done(sc->sc_xs);
482 splx(s);
483 }
484
485 Static void
486 usscanner_data_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
487 usbd_status status)
488 {
489 struct usscanner_softc *sc = priv;
490 struct scsipi_xfer *xs = sc->sc_xs;
491 u_int32_t len;
492
493 DPRINTFN(10, ("usscanner_data_cb status=%d\n", status));
494
495 #ifdef USSCANNER_DEBUG
496 if (sc->sc_state != UAS_DATA) {
497 printf("%s: !UAS_DATA\n", USBDEVNAME(sc->sc_dev));
498 }
499 #endif
500
501 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
502
503 xs->resid = xs->datalen - len;
504
505 switch (status) {
506 case USBD_NORMAL_COMPLETION:
507 if (xs->xs_control & XS_CTL_DATA_IN)
508 memcpy(xs->data, sc->sc_data_buffer, len);
509 xs->error = XS_NOERROR;
510 break;
511 case USBD_TIMEOUT:
512 xs->error = XS_TIMEOUT;
513 break;
514 case USBD_CANCELLED:
515 if (xs->error == XS_SENSE) {
516 usscanner_sense(sc);
517 return;
518 }
519 break;
520 default:
521 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
522 break;
523 }
524 usscanner_done(sc);
525 }
526
527 Static void
528 usscanner_sensedata_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
529 usbd_status status)
530 {
531 struct usscanner_softc *sc = priv;
532 struct scsipi_xfer *xs = sc->sc_xs;
533 u_int32_t len;
534
535 DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status));
536
537 #ifdef USSCANNER_DEBUG
538 if (sc->sc_state != UAS_SENSEDATA) {
539 printf("%s: !UAS_SENSEDATA\n", USBDEVNAME(sc->sc_dev));
540 }
541 #endif
542
543 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
544
545 switch (status) {
546 case USBD_NORMAL_COMPLETION:
547 memcpy(&xs->sense, sc->sc_data_buffer, len);
548 if (len < sizeof xs->sense)
549 xs->error = XS_SHORTSENSE;
550 break;
551 case USBD_TIMEOUT:
552 xs->error = XS_TIMEOUT;
553 break;
554 case USBD_CANCELLED:
555 xs->error = XS_RESET;
556 break;
557 default:
558 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
559 break;
560 }
561 usscanner_done(sc);
562 }
563
564 Static void
565 usscanner_done(struct usscanner_softc *sc)
566 {
567 struct scsipi_xfer *xs = sc->sc_xs;
568 usbd_status err;
569
570 DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error));
571
572 sc->sc_state = UAS_STATUS;
573 usbd_setup_xfer(sc->sc_intr_xfer, sc->sc_intr_pipe, sc, &sc->sc_status,
574 1, USBD_SHORT_XFER_OK | USBD_NO_COPY,
575 USSCANNER_TIMEOUT, usscanner_intr_cb);
576 err = usbd_transfer(sc->sc_intr_xfer);
577 if (err == USBD_IN_PROGRESS)
578 return;
579 xs->error = XS_DRIVER_STUFFUP;
580 }
581
582 Static void
583 usscanner_sensecmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
584 usbd_status status)
585 {
586 struct usscanner_softc *sc = priv;
587 struct scsipi_xfer *xs = sc->sc_xs;
588 usbd_status err;
589
590 DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status));
591
592 #ifdef USSCANNER_DEBUG
593 if (usscannerdebug > 15)
594 xs->xs_periph->periph_flags |= 1; /* XXX 1 */
595
596 if (sc->sc_state != UAS_SENSECMD) {
597 printf("%s: !UAS_SENSECMD\n", USBDEVNAME(sc->sc_dev));
598 xs->error = XS_DRIVER_STUFFUP;
599 goto done;
600 }
601 #endif
602
603 switch (status) {
604 case USBD_NORMAL_COMPLETION:
605 break;
606 case USBD_TIMEOUT:
607 xs->error = XS_TIMEOUT;
608 goto done;
609 default:
610 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
611 goto done;
612 }
613
614 sc->sc_state = UAS_SENSEDATA;
615 usbd_setup_xfer(sc->sc_data_xfer, sc->sc_in_pipe, sc,
616 sc->sc_data_buffer,
617 sizeof xs->sense, USBD_SHORT_XFER_OK | USBD_NO_COPY,
618 USSCANNER_TIMEOUT, usscanner_sensedata_cb);
619 err = usbd_transfer(sc->sc_data_xfer);
620 if (err == USBD_IN_PROGRESS)
621 return;
622 xs->error = XS_DRIVER_STUFFUP;
623 done:
624 usscanner_done(sc);
625 }
626
627 Static void
628 usscanner_cmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
629 usbd_status status)
630 {
631 struct usscanner_softc *sc = priv;
632 struct scsipi_xfer *xs = sc->sc_xs;
633 usbd_pipe_handle pipe;
634 usbd_status err;
635
636 DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status));
637
638 #ifdef USSCANNER_DEBUG
639 if (usscannerdebug > 15)
640 xs->xs_periph->periph_flags |= 1; /* XXX 1 */
641
642 if (sc->sc_state != UAS_CMD) {
643 printf("%s: !UAS_CMD\n", USBDEVNAME(sc->sc_dev));
644 xs->error = XS_DRIVER_STUFFUP;
645 goto done;
646 }
647 #endif
648
649 switch (status) {
650 case USBD_NORMAL_COMPLETION:
651 break;
652 case USBD_TIMEOUT:
653 xs->error = XS_TIMEOUT;
654 goto done;
655 case USBD_CANCELLED:
656 goto done;
657 default:
658 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */
659 goto done;
660 }
661
662 if (xs->datalen == 0) {
663 DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n"));
664 xs->error = XS_NOERROR;
665 goto done;
666 }
667
668 if (xs->xs_control & XS_CTL_DATA_IN) {
669 DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n",
670 xs->datalen));
671 pipe = sc->sc_in_pipe;
672 } else {
673 DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n",
674 xs->datalen));
675 memcpy(sc->sc_data_buffer, xs->data, xs->datalen);
676 pipe = sc->sc_out_pipe;
677 }
678 sc->sc_state = UAS_DATA;
679 usbd_setup_xfer(sc->sc_data_xfer, pipe, sc, sc->sc_data_buffer,
680 xs->datalen, USBD_SHORT_XFER_OK | USBD_NO_COPY,
681 xs->timeout, usscanner_data_cb);
682 err = usbd_transfer(sc->sc_data_xfer);
683 if (err == USBD_IN_PROGRESS)
684 return;
685 xs->error = XS_DRIVER_STUFFUP;
686
687 done:
688 usscanner_done(sc);
689 }
690
691 Static void
692 usscanner_scsipi_request(chan, req, arg)
693 struct scsipi_channel *chan;
694 scsipi_adapter_req_t req;
695 void *arg;
696 {
697 struct scsipi_xfer *xs;
698 struct scsipi_periph *periph;
699 struct usscanner_softc *sc = (void *)chan->chan_adapter->adapt_dev;
700 usbd_status err;
701
702 switch (req) {
703 case ADAPTER_REQ_RUN_XFER:
704 xs = arg;
705 periph = xs->xs_periph;
706
707 DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d "
708 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n",
709 USBDEVNAME(sc->sc_dev),
710 periph->periph_target, periph->periph_lun,
711 xs, xs->cmd->opcode, xs->datalen,
712 periph->periph_quirks, xs->xs_control & XS_CTL_POLL));
713
714 if (sc->sc_dying) {
715 xs->error = XS_DRIVER_STUFFUP;
716 goto done;
717 }
718
719 #ifdef USSCANNER_DEBUG
720 if (periph->periph_target != USSCANNER_SCSIID_DEVICE) {
721 DPRINTF(("%s: wrong SCSI ID %d\n",
722 USBDEVNAME(sc->sc_dev), periph->periph_target));
723 xs->error = XS_DRIVER_STUFFUP;
724 goto done;
725 }
726 if (sc->sc_state != UAS_IDLE) {
727 printf("%s: !UAS_IDLE\n", USBDEVNAME(sc->sc_dev));
728 xs->error = XS_DRIVER_STUFFUP;
729 goto done;
730 }
731 #endif
732
733 if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) {
734 printf("%s: usscanner_scsipi_request: large datalen,"
735 " %d\n", USBDEVNAME(sc->sc_dev), xs->datalen);
736 xs->error = XS_DRIVER_STUFFUP;
737 goto done;
738 }
739
740 DPRINTFN(4, ("%s: usscanner_scsipi_request: async cmdlen=%d"
741 " datalen=%d\n", USBDEVNAME(sc->sc_dev), xs->cmdlen,
742 xs->datalen));
743 sc->sc_state = UAS_CMD;
744 sc->sc_xs = xs;
745 memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen);
746 usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc,
747 sc->sc_cmd_buffer, xs->cmdlen, USBD_NO_COPY,
748 USSCANNER_TIMEOUT, usscanner_cmd_cb);
749 err = usbd_transfer(sc->sc_cmd_xfer);
750 if (err != USBD_IN_PROGRESS) {
751 xs->error = XS_DRIVER_STUFFUP;
752 goto done;
753 }
754
755 return;
756
757
758 done:
759 sc->sc_state = UAS_IDLE;
760 scsipi_done(xs);
761 return;
762
763 case ADAPTER_REQ_GROW_RESOURCES:
764 /* XXX Not supported. */
765 return;
766 case ADAPTER_REQ_SET_XFER_MODE:
767 /* XXX Not supported. */
768 return;
769 }
770
771 }
772