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