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