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