1 /* $NetBSD: usscanner.c,v 1.35 2012/03/06 03:35:30 mrg 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 * 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.35 2012/03/06 03:35:30 mrg 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, "setting config no failed\n"); 200 return; 201 } 202 203 err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface); 204 if (err) { 205 aprint_error_dev(self, "getting interface handle failed\n"); 206 return; 207 } 208 209 sc->sc_udev = dev; 210 sc->sc_iface = iface; 211 212 epcount = 0; 213 (void)usbd_endpoint_count(iface, &epcount); 214 215 sc->sc_in_addr = -1; 216 sc->sc_intr_addr = -1; 217 sc->sc_out_addr = -1; 218 for (i = 0; i < epcount; i++) { 219 ed = usbd_interface2endpoint_descriptor(iface, i); 220 if (ed == NULL) { 221 aprint_error_dev(self, "couldn't get ep %d\n", i); 222 return; 223 } 224 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 225 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 226 sc->sc_in_addr = ed->bEndpointAddress; 227 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 228 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 229 sc->sc_intr_addr = ed->bEndpointAddress; 230 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 231 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 232 sc->sc_out_addr = ed->bEndpointAddress; 233 } 234 } 235 if (sc->sc_in_addr == -1 || sc->sc_intr_addr == -1 || 236 sc->sc_out_addr == -1) { 237 aprint_error_dev(self, "missing endpoint\n"); 238 return; 239 } 240 241 err = usbd_open_pipe(sc->sc_iface, sc->sc_in_addr, 242 USBD_EXCLUSIVE_USE, &sc->sc_in_pipe); 243 if (err) { 244 aprint_error_dev(self, "open in pipe failed, err=%d\n", err); 245 return; 246 } 247 248 /* The interrupt endpoint must be opened as a normal pipe. */ 249 err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr, 250 USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe); 251 252 if (err) { 253 aprint_error_dev(self, "open intr pipe failed, err=%d\n", err); 254 usscanner_cleanup(sc); 255 return; 256 } 257 err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr, 258 USBD_EXCLUSIVE_USE, &sc->sc_out_pipe); 259 if (err) { 260 aprint_error_dev(self, "open out pipe failed, err=%d\n", err); 261 usscanner_cleanup(sc); 262 return; 263 } 264 265 sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device); 266 if (sc->sc_cmd_xfer == NULL) { 267 aprint_error_dev(self, "alloc cmd xfer failed, err=%d\n", err); 268 usscanner_cleanup(sc); 269 return; 270 } 271 272 /* XXX too big */ 273 sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer, 274 USSCANNER_MAX_TRANSFER_SIZE); 275 if (sc->sc_cmd_buffer == NULL) { 276 aprint_error_dev(self, "alloc cmd buffer failed, err=%d\n", 277 err); 278 usscanner_cleanup(sc); 279 return; 280 } 281 282 sc->sc_intr_xfer = usbd_alloc_xfer (uaa->device); 283 if (sc->sc_intr_xfer == NULL) { 284 aprint_error_dev(self, "alloc intr xfer failed, err=%d\n", err); 285 usscanner_cleanup(sc); 286 return; 287 } 288 289 sc->sc_data_xfer = usbd_alloc_xfer(uaa->device); 290 if (sc->sc_data_xfer == NULL) { 291 aprint_error_dev(self, "alloc data xfer failed, err=%d\n", err); 292 usscanner_cleanup(sc); 293 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 aprint_error_dev(self, "alloc data buffer failed, err=%d\n", 299 err); 300 usscanner_cleanup(sc); 301 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 #if NSCSIBUS > 0 315 /* 316 * fill in the scsipi_channel. 317 */ 318 sc->sc_channel.chan_adapter = &sc->sc_adapter; 319 sc->sc_channel.chan_bustype = &scsi_bustype; 320 sc->sc_channel.chan_channel = 0; 321 sc->sc_channel.chan_ntargets = USSCANNER_SCSIID_DEVICE + 1; 322 sc->sc_channel.chan_nluns = 1; 323 sc->sc_channel.chan_id = USSCANNER_SCSIID_HOST; 324 325 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 326 sc->sc_dev); 327 328 sc->sc_child = config_found(sc->sc_dev, &sc->sc_channel, scsiprint); 329 330 DPRINTFN(10, ("usscanner_attach: %p\n", sc->sc_udev)); 331 332 return; 333 334 #else 335 /* No SCSI bus, just ignore it */ 336 usscanner_cleanup(sc); 337 338 aprint_error_dev(self, 339 "no scsibus configured, see usscanner(4) for details\n"); 340 341 return; 342 343 #endif 344 } 345 346 void 347 usscanner_childdet(device_t self, device_t child) 348 { 349 struct usscanner_softc *sc = device_private(self); 350 351 KASSERT(sc->sc_child == NULL); 352 sc->sc_child = NULL; 353 } 354 355 int 356 usscanner_detach(device_t self, int flags) 357 { 358 struct usscanner_softc *sc = device_private(self); 359 int rv, s; 360 361 DPRINTF(("usscanner_detach: sc=%p flags=%d\n", sc, flags)); 362 363 sc->sc_dying = 1; 364 /* Abort all pipes. Causes processes waiting for transfer to wake. */ 365 if (sc->sc_in_pipe != NULL) 366 usbd_abort_pipe(sc->sc_in_pipe); 367 if (sc->sc_intr_pipe != NULL) 368 usbd_abort_pipe(sc->sc_intr_pipe); 369 if (sc->sc_out_pipe != NULL) 370 usbd_abort_pipe(sc->sc_out_pipe); 371 372 s = splusb(); 373 if (--sc->sc_refcnt >= 0) { 374 /* Wait for processes to go away. */ 375 usb_detach_waitold(sc->sc_dev); 376 } 377 splx(s); 378 379 if (sc->sc_child != NULL) 380 rv = config_detach(sc->sc_child, flags); 381 else 382 rv = 0; 383 384 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 385 sc->sc_dev); 386 387 return (rv); 388 } 389 390 Static void 391 usscanner_cleanup(struct usscanner_softc *sc) 392 { 393 if (sc->sc_in_pipe != NULL) { 394 usbd_close_pipe(sc->sc_in_pipe); 395 sc->sc_in_pipe = NULL; 396 } 397 if (sc->sc_intr_pipe != NULL) { 398 usbd_close_pipe(sc->sc_intr_pipe); 399 sc->sc_intr_pipe = NULL; 400 } 401 if (sc->sc_out_pipe != NULL) { 402 usbd_close_pipe(sc->sc_out_pipe); 403 sc->sc_out_pipe = NULL; 404 } 405 if (sc->sc_cmd_xfer != NULL) { 406 usbd_free_xfer(sc->sc_cmd_xfer); 407 sc->sc_cmd_xfer = NULL; 408 } 409 if (sc->sc_data_xfer != NULL) { 410 usbd_free_xfer(sc->sc_data_xfer); 411 sc->sc_data_xfer = NULL; 412 } 413 } 414 415 int 416 usscanner_activate(device_t self, enum devact act) 417 { 418 struct usscanner_softc *sc = device_private(self); 419 420 switch (act) { 421 case DVACT_DEACTIVATE: 422 sc->sc_dying = 1; 423 return 0; 424 default: 425 return EOPNOTSUPP; 426 } 427 } 428 429 Static void 430 usscanner_scsipi_minphys(struct buf *bp) 431 { 432 if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE) 433 bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE; 434 minphys(bp); 435 } 436 437 Static void 438 usscanner_sense(struct usscanner_softc *sc) 439 { 440 struct scsipi_xfer *xs = sc->sc_xs; 441 struct scsipi_periph *periph = xs->xs_periph; 442 struct scsi_request_sense sense_cmd; 443 usbd_status err; 444 445 /* fetch sense data */ 446 memset(&sense_cmd, 0, sizeof(sense_cmd)); 447 sense_cmd.opcode = SCSI_REQUEST_SENSE; 448 sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT; 449 sense_cmd.length = sizeof xs->sense; 450 451 sc->sc_state = UAS_SENSECMD; 452 memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd); 453 usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer, 454 sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT, 455 usscanner_sensecmd_cb); 456 err = usbd_transfer(sc->sc_cmd_xfer); 457 if (err == USBD_IN_PROGRESS) 458 return; 459 460 xs->error = XS_DRIVER_STUFFUP; 461 usscanner_done(sc); 462 } 463 464 Static void 465 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 466 usbd_status status) 467 { 468 struct usscanner_softc *sc = priv; 469 int s; 470 471 DPRINTFN(10, ("usscanner_data_cb status=%d\n", status)); 472 473 #ifdef USSCANNER_DEBUG 474 if (sc->sc_state != UAS_STATUS) { 475 printf("%s: !UAS_STATUS\n", device_xname(sc->sc_dev)); 476 } 477 if (sc->sc_status != 0) { 478 printf("%s: status byte=0x%02x\n", device_xname(sc->sc_dev), sc->sc_status); 479 } 480 #endif 481 /* XXX what should we do on non-0 status */ 482 483 sc->sc_state = UAS_IDLE; 484 485 s = splbio(); 486 KERNEL_LOCK(1, curlwp); 487 scsipi_done(sc->sc_xs); 488 KERNEL_UNLOCK_ONE(curlwp); 489 splx(s); 490 } 491 492 Static void 493 usscanner_data_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 494 usbd_status status) 495 { 496 struct usscanner_softc *sc = priv; 497 struct scsipi_xfer *xs = sc->sc_xs; 498 u_int32_t len; 499 500 DPRINTFN(10, ("usscanner_data_cb status=%d\n", status)); 501 502 #ifdef USSCANNER_DEBUG 503 if (sc->sc_state != UAS_DATA) { 504 printf("%s: !UAS_DATA\n", device_xname(sc->sc_dev)); 505 } 506 #endif 507 508 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 509 510 xs->resid = xs->datalen - len; 511 512 switch (status) { 513 case USBD_NORMAL_COMPLETION: 514 if (xs->xs_control & XS_CTL_DATA_IN) 515 memcpy(xs->data, sc->sc_data_buffer, len); 516 xs->error = XS_NOERROR; 517 break; 518 case USBD_TIMEOUT: 519 xs->error = XS_TIMEOUT; 520 break; 521 case USBD_CANCELLED: 522 if (xs->error == XS_SENSE) { 523 usscanner_sense(sc); 524 return; 525 } 526 break; 527 default: 528 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */ 529 break; 530 } 531 usscanner_done(sc); 532 } 533 534 Static void 535 usscanner_sensedata_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 536 usbd_status status) 537 { 538 struct usscanner_softc *sc = priv; 539 struct scsipi_xfer *xs = sc->sc_xs; 540 u_int32_t len; 541 542 DPRINTFN(10, ("usscanner_sensedata_cb status=%d\n", status)); 543 544 #ifdef USSCANNER_DEBUG 545 if (sc->sc_state != UAS_SENSEDATA) { 546 printf("%s: !UAS_SENSEDATA\n", device_xname(sc->sc_dev)); 547 } 548 #endif 549 550 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 551 552 switch (status) { 553 case USBD_NORMAL_COMPLETION: 554 memcpy(&xs->sense, sc->sc_data_buffer, len); 555 if (len < sizeof xs->sense) 556 xs->error = XS_SHORTSENSE; 557 break; 558 case USBD_TIMEOUT: 559 xs->error = XS_TIMEOUT; 560 break; 561 case USBD_CANCELLED: 562 xs->error = XS_RESET; 563 break; 564 default: 565 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */ 566 break; 567 } 568 usscanner_done(sc); 569 } 570 571 Static void 572 usscanner_done(struct usscanner_softc *sc) 573 { 574 struct scsipi_xfer *xs = sc->sc_xs; 575 usbd_status err; 576 577 DPRINTFN(10,("usscanner_done: error=%d\n", sc->sc_xs->error)); 578 579 sc->sc_state = UAS_STATUS; 580 usbd_setup_xfer(sc->sc_intr_xfer, sc->sc_intr_pipe, sc, &sc->sc_status, 581 1, USBD_SHORT_XFER_OK | USBD_NO_COPY, 582 USSCANNER_TIMEOUT, usscanner_intr_cb); 583 err = usbd_transfer(sc->sc_intr_xfer); 584 if (err == USBD_IN_PROGRESS) 585 return; 586 xs->error = XS_DRIVER_STUFFUP; 587 } 588 589 Static void 590 usscanner_sensecmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 591 usbd_status status) 592 { 593 struct usscanner_softc *sc = priv; 594 struct scsipi_xfer *xs = sc->sc_xs; 595 usbd_status err; 596 597 DPRINTFN(10, ("usscanner_sensecmd_cb status=%d\n", status)); 598 599 #ifdef USSCANNER_DEBUG 600 if (usscannerdebug > 15) 601 xs->xs_periph->periph_flags |= 1; /* XXX 1 */ 602 603 if (sc->sc_state != UAS_SENSECMD) { 604 aprint_error_dev(sc->sc_dev, "!UAS_SENSECMD\n"); 605 xs->error = XS_DRIVER_STUFFUP; 606 goto done; 607 } 608 #endif 609 610 switch (status) { 611 case USBD_NORMAL_COMPLETION: 612 break; 613 case USBD_TIMEOUT: 614 xs->error = XS_TIMEOUT; 615 goto done; 616 default: 617 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */ 618 goto done; 619 } 620 621 sc->sc_state = UAS_SENSEDATA; 622 usbd_setup_xfer(sc->sc_data_xfer, sc->sc_in_pipe, sc, 623 sc->sc_data_buffer, 624 sizeof xs->sense, USBD_SHORT_XFER_OK | USBD_NO_COPY, 625 USSCANNER_TIMEOUT, usscanner_sensedata_cb); 626 err = usbd_transfer(sc->sc_data_xfer); 627 if (err == USBD_IN_PROGRESS) 628 return; 629 xs->error = XS_DRIVER_STUFFUP; 630 done: 631 usscanner_done(sc); 632 } 633 634 Static void 635 usscanner_cmd_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 636 usbd_status status) 637 { 638 struct usscanner_softc *sc = priv; 639 struct scsipi_xfer *xs = sc->sc_xs; 640 usbd_pipe_handle pipe; 641 usbd_status err; 642 643 DPRINTFN(10, ("usscanner_cmd_cb status=%d\n", status)); 644 645 #ifdef USSCANNER_DEBUG 646 if (usscannerdebug > 15) 647 xs->xs_periph->periph_flags |= 1; /* XXX 1 */ 648 649 if (sc->sc_state != UAS_CMD) { 650 aprint_error_dev(sc->sc_dev, "!UAS_CMD\n"); 651 xs->error = XS_DRIVER_STUFFUP; 652 goto done; 653 } 654 #endif 655 656 switch (status) { 657 case USBD_NORMAL_COMPLETION: 658 break; 659 case USBD_TIMEOUT: 660 xs->error = XS_TIMEOUT; 661 goto done; 662 case USBD_CANCELLED: 663 goto done; 664 default: 665 xs->error = XS_DRIVER_STUFFUP; /* XXX ? */ 666 goto done; 667 } 668 669 if (xs->datalen == 0) { 670 DPRINTFN(4, ("usscanner_cmd_cb: no data phase\n")); 671 xs->error = XS_NOERROR; 672 goto done; 673 } 674 675 if (xs->xs_control & XS_CTL_DATA_IN) { 676 DPRINTFN(4, ("usscanner_cmd_cb: data in len=%d\n", 677 xs->datalen)); 678 pipe = sc->sc_in_pipe; 679 } else { 680 DPRINTFN(4, ("usscanner_cmd_cb: data out len=%d\n", 681 xs->datalen)); 682 memcpy(sc->sc_data_buffer, xs->data, xs->datalen); 683 pipe = sc->sc_out_pipe; 684 } 685 sc->sc_state = UAS_DATA; 686 usbd_setup_xfer(sc->sc_data_xfer, pipe, sc, sc->sc_data_buffer, 687 xs->datalen, USBD_SHORT_XFER_OK | USBD_NO_COPY, 688 xs->timeout, usscanner_data_cb); 689 err = usbd_transfer(sc->sc_data_xfer); 690 if (err == USBD_IN_PROGRESS) 691 return; 692 xs->error = XS_DRIVER_STUFFUP; 693 694 done: 695 usscanner_done(sc); 696 } 697 698 Static void 699 usscanner_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg) 700 { 701 struct scsipi_xfer *xs; 702 struct scsipi_periph *periph; 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 periph = xs->xs_periph; 711 712 DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d " 713 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n", 714 device_xname(sc->sc_dev), 715 periph->periph_target, periph->periph_lun, 716 xs, xs->cmd->opcode, xs->datalen, 717 periph->periph_quirks, xs->xs_control & XS_CTL_POLL)); 718 719 if (sc->sc_dying) { 720 xs->error = XS_DRIVER_STUFFUP; 721 goto done; 722 } 723 724 #ifdef USSCANNER_DEBUG 725 if (periph->periph_target != USSCANNER_SCSIID_DEVICE) { 726 DPRINTF(("%s: wrong SCSI ID %d\n", 727 device_xname(sc->sc_dev), 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