1 /* $NetBSD: usscanner.c,v 1.24 2008/04/28 20:24:01 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@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.24 2008/04/28 20:24:01 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/malloc.h> 57 #include <sys/device.h> 58 #include <sys/conf.h> 59 #include <sys/buf.h> 60 61 #include <dev/usb/usb.h> 62 #include <dev/usb/usbdi.h> 63 #include <dev/usb/usbdi_util.h> 64 65 #include <dev/usb/usbdevs.h> 66 67 #include <sys/scsiio.h> 68 #include <dev/scsipi/scsi_spc.h> 69 #include <dev/scsipi/scsi_all.h> 70 #include <dev/scsipi/scsipi_all.h> 71 #include <dev/scsipi/scsiconf.h> 72 #include <dev/scsipi/atapiconf.h> 73 74 #ifdef USSCANNER_DEBUG 75 #define DPRINTF(x) if (usscannerdebug) logprintf x 76 #define DPRINTFN(n,x) if (usscannerdebug>(n)) logprintf x 77 int usscannerdebug = 0; 78 #else 79 #define DPRINTF(x) 80 #define DPRINTFN(n,x) 81 #endif 82 83 84 #define USSCANNER_CONFIG_NO 1 85 #define USSCANNER_IFACE_IDX 0 86 87 #define USSCANNER_SCSIID_HOST 0x00 88 #define USSCANNER_SCSIID_DEVICE 0x01 89 90 #define USSCANNER_MAX_TRANSFER_SIZE MAXPHYS 91 92 #define USSCANNER_TIMEOUT 2000 93 94 struct usscanner_softc { 95 USBBASEDEVICE sc_dev; 96 usbd_device_handle sc_udev; 97 usbd_interface_handle sc_iface; 98 99 int sc_in_addr; 100 usbd_pipe_handle sc_in_pipe; 101 102 int sc_intr_addr; 103 usbd_pipe_handle sc_intr_pipe; 104 usbd_xfer_handle sc_intr_xfer; 105 u_char sc_status; 106 107 int sc_out_addr; 108 usbd_pipe_handle sc_out_pipe; 109 110 usbd_xfer_handle sc_cmd_xfer; 111 void *sc_cmd_buffer; 112 usbd_xfer_handle sc_data_xfer; 113 void *sc_data_buffer; 114 115 int sc_state; 116 #define UAS_IDLE 0 117 #define UAS_CMD 1 118 #define UAS_DATA 2 119 #define UAS_SENSECMD 3 120 #define UAS_SENSEDATA 4 121 #define UAS_STATUS 5 122 123 struct scsipi_xfer *sc_xs; 124 125 device_ptr_t sc_child; /* child device, for detach */ 126 127 struct scsipi_adapter sc_adapter; 128 struct scsipi_channel sc_channel; 129 130 int sc_refcnt; 131 char sc_dying; 132 }; 133 134 135 Static void usscanner_cleanup(struct usscanner_softc *sc); 136 Static void usscanner_scsipi_request(struct scsipi_channel *chan, 137 scsipi_adapter_req_t req, void *arg); 138 Static void usscanner_scsipi_minphys(struct buf *bp); 139 Static void usscanner_done(struct usscanner_softc *sc); 140 Static void usscanner_sense(struct usscanner_softc *sc); 141 typedef void callback(usbd_xfer_handle, usbd_private_handle, usbd_status); 142 Static callback usscanner_intr_cb; 143 Static callback usscanner_cmd_cb; 144 Static callback usscanner_data_cb; 145 Static callback usscanner_sensecmd_cb; 146 Static callback usscanner_sensedata_cb; 147 148 int usscanner_match(device_t, struct cfdata *, void *); 149 void usscanner_attach(device_t, device_t, void *); 150 void usscanner_childdet(device_t, device_t); 151 int usscanner_detach(device_t, int); 152 int usscanner_activate(device_t, enum devact); 153 extern struct cfdriver usscanner_cd; 154 CFATTACH_DECL2(usscanner, sizeof(struct usscanner_softc), 155 usscanner_match, usscanner_attach, usscanner_detach, usscanner_activate, 156 NULL, usscanner_childdet); 157 158 USB_MATCH(usscanner) 159 { 160 USB_MATCH_START(usscanner, uaa); 161 162 DPRINTFN(50,("usscanner_match\n")); 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 *devinfop; 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 devinfop = usbd_devinfo_alloc(dev, 0); 185 USB_ATTACH_SETUP; 186 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop); 187 usbd_devinfo_free(devinfop); 188 189 err = usbd_set_config_no(dev, USSCANNER_CONFIG_NO, 1); 190 if (err) { 191 printf("%s: setting config no failed\n", 192 USBDEVNAME(sc->sc_dev)); 193 USB_ATTACH_ERROR_RETURN; 194 } 195 196 err = usbd_device2interface_handle(dev, USSCANNER_IFACE_IDX, &iface); 197 if (err) { 198 printf("%s: getting interface handle failed\n", 199 USBDEVNAME(sc->sc_dev)); 200 USB_ATTACH_ERROR_RETURN; 201 } 202 203 sc->sc_udev = dev; 204 sc->sc_iface = iface; 205 206 epcount = 0; 207 (void)usbd_endpoint_count(iface, &epcount); 208 209 sc->sc_in_addr = -1; 210 sc->sc_intr_addr = -1; 211 sc->sc_out_addr = -1; 212 for (i = 0; i < epcount; i++) { 213 ed = usbd_interface2endpoint_descriptor(iface, i); 214 if (ed == NULL) { 215 printf("%s: couldn't get ep %d\n", 216 USBDEVNAME(sc->sc_dev), i); 217 USB_ATTACH_ERROR_RETURN; 218 } 219 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 220 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 221 sc->sc_in_addr = ed->bEndpointAddress; 222 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 223 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 224 sc->sc_intr_addr = ed->bEndpointAddress; 225 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 226 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 227 sc->sc_out_addr = ed->bEndpointAddress; 228 } 229 } 230 if (sc->sc_in_addr == -1 || sc->sc_intr_addr == -1 || 231 sc->sc_out_addr == -1) { 232 printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev)); 233 USB_ATTACH_ERROR_RETURN; 234 } 235 236 err = usbd_open_pipe(sc->sc_iface, sc->sc_in_addr, 237 USBD_EXCLUSIVE_USE, &sc->sc_in_pipe); 238 if (err) { 239 printf("%s: open in pipe failed, err=%d\n", 240 USBDEVNAME(sc->sc_dev), err); 241 USB_ATTACH_ERROR_RETURN; 242 } 243 244 /* The interrupt endpoint must be opened as a normal pipe. */ 245 err = usbd_open_pipe(sc->sc_iface, sc->sc_intr_addr, 246 USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe); 247 248 if (err) { 249 printf("%s: open intr pipe failed, err=%d\n", 250 USBDEVNAME(sc->sc_dev), err); 251 usscanner_cleanup(sc); 252 USB_ATTACH_ERROR_RETURN; 253 } 254 err = usbd_open_pipe(sc->sc_iface, sc->sc_out_addr, 255 USBD_EXCLUSIVE_USE, &sc->sc_out_pipe); 256 if (err) { 257 printf("%s: open out pipe failed, err=%d\n", 258 USBDEVNAME(sc->sc_dev), err); 259 usscanner_cleanup(sc); 260 USB_ATTACH_ERROR_RETURN; 261 } 262 263 sc->sc_cmd_xfer = usbd_alloc_xfer(uaa->device); 264 if (sc->sc_cmd_xfer == NULL) { 265 printf("%s: alloc cmd xfer failed, err=%d\n", 266 USBDEVNAME(sc->sc_dev), err); 267 usscanner_cleanup(sc); 268 USB_ATTACH_ERROR_RETURN; 269 } 270 271 /* XXX too big */ 272 sc->sc_cmd_buffer = usbd_alloc_buffer(sc->sc_cmd_xfer, 273 USSCANNER_MAX_TRANSFER_SIZE); 274 if (sc->sc_cmd_buffer == NULL) { 275 printf("%s: alloc cmd buffer failed, err=%d\n", 276 USBDEVNAME(sc->sc_dev), err); 277 usscanner_cleanup(sc); 278 USB_ATTACH_ERROR_RETURN; 279 } 280 281 sc->sc_intr_xfer = usbd_alloc_xfer (uaa->device); 282 if (sc->sc_intr_xfer == NULL) { 283 printf("%s: alloc intr xfer failed, err=%d\n", 284 USBDEVNAME(sc->sc_dev), err); 285 usscanner_cleanup(sc); 286 USB_ATTACH_ERROR_RETURN; 287 } 288 289 sc->sc_data_xfer = usbd_alloc_xfer(uaa->device); 290 if (sc->sc_data_xfer == NULL) { 291 printf("%s: alloc data xfer failed, err=%d\n", 292 USBDEVNAME(sc->sc_dev), err); 293 usscanner_cleanup(sc); 294 USB_ATTACH_ERROR_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 printf("%s: alloc data buffer failed, err=%d\n", 300 USBDEVNAME(sc->sc_dev), err); 301 usscanner_cleanup(sc); 302 USB_ATTACH_ERROR_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 USBDEV(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 USB_ATTACH_SUCCESS_RETURN; 334 335 #else 336 /* No SCSI bus, just ignore it */ 337 usscanner_cleanup(sc); 338 339 printf("%s: no scsibus configured, see usscanner(4) for details\n", 340 USBDEVNAME(sc->sc_dev)); 341 342 USB_ATTACH_ERROR_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 USB_DETACH(usscanner) 357 { 358 USB_DETACH_START(usscanner, sc); 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_wait(USBDEV(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 USBDEV(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_ACTIVATE: 422 return (EOPNOTSUPP); 423 424 case DVACT_DEACTIVATE: 425 sc->sc_dying = 1; 426 break; 427 } 428 return (0); 429 } 430 431 Static void 432 usscanner_scsipi_minphys(struct buf *bp) 433 { 434 if (bp->b_bcount > USSCANNER_MAX_TRANSFER_SIZE) 435 bp->b_bcount = USSCANNER_MAX_TRANSFER_SIZE; 436 minphys(bp); 437 } 438 439 Static void 440 usscanner_sense(struct usscanner_softc *sc) 441 { 442 struct scsipi_xfer *xs = sc->sc_xs; 443 struct scsipi_periph *periph = xs->xs_periph; 444 struct scsi_request_sense sense_cmd; 445 usbd_status err; 446 447 /* fetch sense data */ 448 memset(&sense_cmd, 0, sizeof(sense_cmd)); 449 sense_cmd.opcode = SCSI_REQUEST_SENSE; 450 sense_cmd.byte2 = periph->periph_lun << SCSI_CMD_LUN_SHIFT; 451 sense_cmd.length = sizeof xs->sense; 452 453 sc->sc_state = UAS_SENSECMD; 454 memcpy(sc->sc_cmd_buffer, &sense_cmd, sizeof sense_cmd); 455 usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, sc->sc_cmd_buffer, 456 sizeof sense_cmd, USBD_NO_COPY, USSCANNER_TIMEOUT, 457 usscanner_sensecmd_cb); 458 err = usbd_transfer(sc->sc_cmd_xfer); 459 if (err == USBD_IN_PROGRESS) 460 return; 461 462 xs->error = XS_DRIVER_STUFFUP; 463 usscanner_done(sc); 464 } 465 466 Static void 467 usscanner_intr_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 468 usbd_status status) 469 { 470 struct usscanner_softc *sc = priv; 471 int s; 472 473 DPRINTFN(10, ("usscanner_data_cb status=%d\n", status)); 474 475 #ifdef USSCANNER_DEBUG 476 if (sc->sc_state != UAS_STATUS) { 477 printf("%s: !UAS_STATUS\n", USBDEVNAME(sc->sc_dev)); 478 } 479 if (sc->sc_status != 0) { 480 printf("%s: status byte=0x%02x\n", USBDEVNAME(sc->sc_dev), sc->sc_status); 481 } 482 #endif 483 /* XXX what should we do on non-0 status */ 484 485 sc->sc_state = UAS_IDLE; 486 487 s = splbio(); 488 scsipi_done(sc->sc_xs); 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", USBDEVNAME(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", USBDEVNAME(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 printf("%s: !UAS_SENSECMD\n", USBDEVNAME(sc->sc_dev)); 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 printf("%s: !UAS_CMD\n", USBDEVNAME(sc->sc_dev)); 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(chan, req, arg) 700 struct scsipi_channel *chan; 701 scsipi_adapter_req_t req; 702 void *arg; 703 { 704 struct scsipi_xfer *xs; 705 struct scsipi_periph *periph; 706 struct usscanner_softc *sc = (void *)chan->chan_adapter->adapt_dev; 707 usbd_status err; 708 709 switch (req) { 710 case ADAPTER_REQ_RUN_XFER: 711 xs = arg; 712 periph = xs->xs_periph; 713 714 DPRINTFN(8, ("%s: usscanner_scsipi_request: %d:%d " 715 "xs=%p cmd=0x%02x datalen=%d (quirks=0x%x, poll=%d)\n", 716 USBDEVNAME(sc->sc_dev), 717 periph->periph_target, periph->periph_lun, 718 xs, xs->cmd->opcode, xs->datalen, 719 periph->periph_quirks, xs->xs_control & XS_CTL_POLL)); 720 721 if (sc->sc_dying) { 722 xs->error = XS_DRIVER_STUFFUP; 723 goto done; 724 } 725 726 #ifdef USSCANNER_DEBUG 727 if (periph->periph_target != USSCANNER_SCSIID_DEVICE) { 728 DPRINTF(("%s: wrong SCSI ID %d\n", 729 USBDEVNAME(sc->sc_dev), periph->periph_target)); 730 xs->error = XS_DRIVER_STUFFUP; 731 goto done; 732 } 733 if (sc->sc_state != UAS_IDLE) { 734 printf("%s: !UAS_IDLE\n", USBDEVNAME(sc->sc_dev)); 735 xs->error = XS_DRIVER_STUFFUP; 736 goto done; 737 } 738 #endif 739 740 if (xs->datalen > USSCANNER_MAX_TRANSFER_SIZE) { 741 printf("%s: usscanner_scsipi_request: large datalen," 742 " %d\n", USBDEVNAME(sc->sc_dev), xs->datalen); 743 xs->error = XS_DRIVER_STUFFUP; 744 goto done; 745 } 746 747 DPRINTFN(4, ("%s: usscanner_scsipi_request: async cmdlen=%d" 748 " datalen=%d\n", USBDEVNAME(sc->sc_dev), xs->cmdlen, 749 xs->datalen)); 750 sc->sc_state = UAS_CMD; 751 sc->sc_xs = xs; 752 memcpy(sc->sc_cmd_buffer, xs->cmd, xs->cmdlen); 753 usbd_setup_xfer(sc->sc_cmd_xfer, sc->sc_out_pipe, sc, 754 sc->sc_cmd_buffer, xs->cmdlen, USBD_NO_COPY, 755 USSCANNER_TIMEOUT, usscanner_cmd_cb); 756 err = usbd_transfer(sc->sc_cmd_xfer); 757 if (err != USBD_IN_PROGRESS) { 758 xs->error = XS_DRIVER_STUFFUP; 759 goto done; 760 } 761 762 return; 763 764 765 done: 766 sc->sc_state = UAS_IDLE; 767 scsipi_done(xs); 768 return; 769 770 case ADAPTER_REQ_GROW_RESOURCES: 771 /* XXX Not supported. */ 772 return; 773 case ADAPTER_REQ_SET_XFER_MODE: 774 /* XXX Not supported. */ 775 return; 776 } 777 778 } 779