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