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