1 /* $OpenBSD: umass.c,v 1.62 2011/07/03 15:47:17 matthew Exp $ */ 2 /* $NetBSD: umass.c,v 1.116 2004/06/30 05:53:46 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 2003 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /*- 34 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>, 35 * Nick Hibma <n_hibma@freebsd.org> 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 50 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 57 * SUCH DAMAGE. 58 * 59 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $ 60 */ 61 62 /* 63 * Universal Serial Bus Mass Storage Class specs: 64 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf 65 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf 66 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf 67 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf 68 */ 69 70 /* 71 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>. 72 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>. 73 */ 74 75 /* 76 * The driver handles 3 Wire Protocols 77 * - Command/Bulk/Interrupt (CBI) 78 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI) 79 * - Mass Storage Bulk-Only (BBB) 80 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases) 81 * 82 * Over these wire protocols it handles the following command protocols 83 * - SCSI 84 * - 8070 (ATA/ATAPI for rewritable removable media) 85 * - UFI (USB Floppy Interface) 86 * 87 * 8070i is a transformed version of the SCSI command set. UFI is a transformed 88 * version of the 8070i command set. The sc->transform method is used to 89 * convert the commands into the appropriate format (if at all necessary). 90 * For example, ATAPI requires all commands to be 12 bytes in length amongst 91 * other things. 92 * 93 * The source code below is marked and can be split into a number of pieces 94 * (in this order): 95 * 96 * - probe/attach/detach 97 * - generic transfer routines 98 * - BBB 99 * - CBI 100 * - CBI_I (in addition to functions from CBI) 101 * - CAM (Common Access Method) 102 * - SCSI 103 * - UFI 104 * - 8070i 105 * 106 * The protocols are implemented using a state machine, for the transfers as 107 * well as for the resets. The state machine is contained in umass_*_state. 108 * The state machine is started through either umass_*_transfer or 109 * umass_*_reset. 110 * 111 * The reason for doing this is a) CAM performs a lot better this way and b) it 112 * avoids using tsleep from interrupt context (for example after a failed 113 * transfer). 114 */ 115 116 /* 117 * The SCSI related part of this driver has been derived from the 118 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org). 119 * 120 * The CAM layer uses so called actions which are messages sent to the host 121 * adapter for completion. The actions come in through umass_cam_action. The 122 * appropriate block of routines is called depending on the transport protocol 123 * in use. When the transfer has finished, these routines call 124 * umass_cam_cb again to complete the CAM command. 125 */ 126 127 #include <sys/param.h> 128 #include <sys/systm.h> 129 #include <sys/kernel.h> 130 #include <sys/conf.h> 131 #include <sys/buf.h> 132 #include <sys/device.h> 133 #include <sys/malloc.h> 134 #include <sys/timeout.h> 135 #undef KASSERT 136 #define KASSERT(cond, msg) 137 #include <machine/bus.h> 138 139 #include <scsi/scsi_all.h> 140 141 #include <dev/usb/usb.h> 142 #include <dev/usb/usbdi.h> 143 #include <dev/usb/usbdi_util.h> 144 #include <dev/usb/usbdivar.h> 145 #include <dev/usb/usbdevs.h> 146 147 #include <dev/usb/umassvar.h> 148 #include <dev/usb/umass_quirks.h> 149 #include <dev/usb/umass_scsi.h> 150 151 152 #ifdef UMASS_DEBUG 153 int umassdebug = 0; 154 155 char *states[TSTATE_STATES+1] = { 156 /* should be kept in sync with the list at transfer_state */ 157 "Idle", 158 "BBB CBW", 159 "BBB Data", 160 "BBB Data bulk-in/-out clear stall", 161 "BBB CSW, 1st attempt", 162 "BBB CSW bulk-in clear stall", 163 "BBB CSW, 2nd attempt", 164 "BBB Reset", 165 "BBB bulk-in clear stall", 166 "BBB bulk-out clear stall", 167 "CBI Command", 168 "CBI Data", 169 "CBI Status", 170 "CBI Data bulk-in/-out clear stall", 171 "CBI Status intr-in clear stall", 172 "CBI Reset", 173 "CBI bulk-in clear stall", 174 "CBI bulk-out clear stall", 175 NULL 176 }; 177 #endif 178 179 /* USB device probe/attach/detach functions */ 180 int umass_match(struct device *, void *, void *); 181 void umass_attach(struct device *, struct device *, void *); 182 int umass_detach(struct device *, int); 183 int umass_activate(struct device *, int); 184 185 struct cfdriver umass_cd = { 186 NULL, "umass", DV_DULL 187 }; 188 189 const struct cfattach umass_ca = { 190 sizeof(struct umass_softc), 191 umass_match, 192 umass_attach, 193 umass_detach, 194 umass_activate, 195 }; 196 void umass_disco(struct umass_softc *sc); 197 198 /* generic transfer functions */ 199 usbd_status umass_polled_transfer(struct umass_softc *sc, 200 usbd_xfer_handle xfer); 201 usbd_status umass_setup_transfer(struct umass_softc *sc, 202 usbd_pipe_handle pipe, 203 void *buffer, int buflen, int flags, 204 usbd_xfer_handle xfer); 205 usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc, 206 usb_device_request_t *req, 207 void *buffer, int buflen, int flags, 208 usbd_xfer_handle xfer); 209 void umass_clear_endpoint_stall(struct umass_softc *sc, int endpt, 210 usbd_xfer_handle xfer); 211 void umass_adjust_transfer(struct umass_softc *); 212 #if 0 213 void umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv); 214 #endif 215 216 /* Bulk-Only related functions */ 217 void umass_bbb_transfer(struct umass_softc *, int, void *, int, void *, 218 int, int, u_int, umass_callback, void *); 219 void umass_bbb_reset(struct umass_softc *, int); 220 void umass_bbb_state(usbd_xfer_handle, usbd_private_handle, usbd_status); 221 222 u_int8_t umass_bbb_get_max_lun(struct umass_softc *); 223 224 /* CBI related functions */ 225 void umass_cbi_transfer(struct umass_softc *, int, void *, int, void *, 226 int, int, u_int, umass_callback, void *); 227 void umass_cbi_reset(struct umass_softc *, int); 228 void umass_cbi_state(usbd_xfer_handle, usbd_private_handle, usbd_status); 229 230 int umass_cbi_adsc(struct umass_softc *, char *, int, usbd_xfer_handle); 231 232 const struct umass_wire_methods umass_bbb_methods = { 233 umass_bbb_transfer, 234 umass_bbb_reset, 235 umass_bbb_state 236 }; 237 238 const struct umass_wire_methods umass_cbi_methods = { 239 umass_cbi_transfer, 240 umass_cbi_reset, 241 umass_cbi_state 242 }; 243 244 #ifdef UMASS_DEBUG 245 /* General debugging functions */ 246 void umass_bbb_dump_cbw(struct umass_softc *sc, 247 umass_bbb_cbw_t *cbw); 248 void umass_bbb_dump_csw(struct umass_softc *sc, 249 umass_bbb_csw_t *csw); 250 void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, 251 int buflen, int printlen); 252 #endif 253 254 255 /* 256 * USB device probe/attach/detach 257 */ 258 259 int 260 umass_match(struct device *parent, void *match, void *aux) 261 { 262 struct usb_attach_arg *uaa = aux; 263 const struct umass_quirk *quirk; 264 usb_interface_descriptor_t *id; 265 266 if (uaa->iface == NULL) 267 return (UMATCH_NONE); 268 269 quirk = umass_lookup(uaa->vendor, uaa->product); 270 if (quirk != NULL) 271 return (quirk->uq_match); 272 273 id = usbd_get_interface_descriptor(uaa->iface); 274 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 275 return (UMATCH_NONE); 276 277 switch (id->bInterfaceSubClass) { 278 case UISUBCLASS_RBC: 279 case UISUBCLASS_SFF8020I: 280 case UISUBCLASS_QIC157: 281 case UISUBCLASS_UFI: 282 case UISUBCLASS_SFF8070I: 283 case UISUBCLASS_SCSI: 284 break; 285 default: 286 return (UMATCH_IFACECLASS); 287 } 288 289 switch (id->bInterfaceProtocol) { 290 case UIPROTO_MASS_CBI_I: 291 case UIPROTO_MASS_CBI: 292 case UIPROTO_MASS_BBB_OLD: 293 case UIPROTO_MASS_BBB: 294 break; 295 default: 296 return (UMATCH_IFACECLASS_IFACESUBCLASS); 297 } 298 299 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO); 300 } 301 302 void 303 umass_attach(struct device *parent, struct device *self, void *aux) 304 { 305 struct umass_softc *sc = (struct umass_softc *)self; 306 struct usb_attach_arg *uaa = aux; 307 const struct umass_quirk *quirk; 308 usb_interface_descriptor_t *id; 309 usb_endpoint_descriptor_t *ed; 310 const char *sWire, *sCommand; 311 usbd_status err; 312 int i, bno, error; 313 314 sc->sc_udev = uaa->device; 315 sc->sc_iface = uaa->iface; 316 sc->sc_ifaceno = uaa->ifaceno; 317 318 quirk = umass_lookup(uaa->vendor, uaa->product); 319 if (quirk != NULL) { 320 sc->sc_wire = quirk->uq_wire; 321 sc->sc_cmd = quirk->uq_cmd; 322 sc->sc_quirks = quirk->uq_flags; 323 sc->sc_busquirks = quirk->uq_busquirks; 324 325 if (quirk->uq_fixup != NULL) 326 (*quirk->uq_fixup)(sc); 327 } else { 328 sc->sc_wire = UMASS_WPROTO_UNSPEC; 329 sc->sc_cmd = UMASS_CPROTO_UNSPEC; 330 sc->sc_quirks = 0; 331 sc->sc_busquirks = 0; 332 } 333 334 id = usbd_get_interface_descriptor(sc->sc_iface); 335 if (id == NULL) 336 return; 337 338 if (sc->sc_wire == UMASS_WPROTO_UNSPEC) { 339 switch (id->bInterfaceProtocol) { 340 case UIPROTO_MASS_CBI: 341 sc->sc_wire = UMASS_WPROTO_CBI; 342 break; 343 case UIPROTO_MASS_CBI_I: 344 sc->sc_wire = UMASS_WPROTO_CBI_I; 345 break; 346 case UIPROTO_MASS_BBB: 347 case UIPROTO_MASS_BBB_OLD: 348 sc->sc_wire = UMASS_WPROTO_BBB; 349 break; 350 default: 351 DPRINTF(UDMASS_GEN, 352 ("%s: Unsupported wire protocol %u\n", 353 sc->sc_dev.dv_xname, 354 id->bInterfaceProtocol)); 355 return; 356 } 357 } 358 359 if (sc->sc_cmd == UMASS_CPROTO_UNSPEC) { 360 switch (id->bInterfaceSubClass) { 361 case UISUBCLASS_SCSI: 362 sc->sc_cmd = UMASS_CPROTO_SCSI; 363 break; 364 case UISUBCLASS_UFI: 365 sc->sc_cmd = UMASS_CPROTO_UFI; 366 break; 367 case UISUBCLASS_SFF8020I: 368 case UISUBCLASS_SFF8070I: 369 case UISUBCLASS_QIC157: 370 sc->sc_cmd = UMASS_CPROTO_ATAPI; 371 break; 372 case UISUBCLASS_RBC: 373 sc->sc_cmd = UMASS_CPROTO_RBC; 374 break; 375 default: 376 DPRINTF(UDMASS_GEN, 377 ("%s: Unsupported command protocol %u\n", 378 sc->sc_dev.dv_xname, 379 id->bInterfaceSubClass)); 380 return; 381 } 382 } 383 384 switch (sc->sc_wire) { 385 case UMASS_WPROTO_CBI: 386 sWire = "CBI"; 387 break; 388 case UMASS_WPROTO_CBI_I: 389 sWire = "CBI with CCI"; 390 break; 391 case UMASS_WPROTO_BBB: 392 sWire = "Bulk-Only"; 393 break; 394 default: 395 sWire = "unknown"; 396 break; 397 } 398 399 switch (sc->sc_cmd) { 400 case UMASS_CPROTO_RBC: 401 sCommand = "RBC"; 402 break; 403 case UMASS_CPROTO_SCSI: 404 sCommand = "SCSI"; 405 break; 406 case UMASS_CPROTO_UFI: 407 sCommand = "UFI"; 408 break; 409 case UMASS_CPROTO_ATAPI: 410 sCommand = "ATAPI"; 411 break; 412 case UMASS_CPROTO_ISD_ATA: 413 sCommand = "ISD-ATA"; 414 break; 415 default: 416 sCommand = "unknown"; 417 break; 418 } 419 420 printf("%s: using %s over %s\n", sc->sc_dev.dv_xname, sCommand, 421 sWire); 422 423 if (quirk != NULL && quirk->uq_init != NULL) { 424 err = (*quirk->uq_init)(sc); 425 if (err) { 426 umass_disco(sc); 427 return; 428 } 429 } 430 431 /* 432 * In addition to the Control endpoint the following endpoints 433 * are required: 434 * a) bulk-in endpoint. 435 * b) bulk-out endpoint. 436 * and for Control/Bulk/Interrupt with CCI (CBI_I) 437 * c) intr-in 438 * 439 * The endpoint addresses are not fixed, so we have to read them 440 * from the device descriptors of the current interface. 441 */ 442 for (i = 0 ; i < id->bNumEndpoints ; i++) { 443 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i); 444 if (ed == NULL) { 445 printf("%s: could not read endpoint descriptor\n", 446 sc->sc_dev.dv_xname); 447 return; 448 } 449 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN 450 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 451 sc->sc_epaddr[UMASS_BULKIN] = ed->bEndpointAddress; 452 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT 453 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 454 sc->sc_epaddr[UMASS_BULKOUT] = ed->bEndpointAddress; 455 } else if (sc->sc_wire == UMASS_WPROTO_CBI_I 456 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN 457 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { 458 sc->sc_epaddr[UMASS_INTRIN] = ed->bEndpointAddress; 459 #ifdef UMASS_DEBUG 460 if (UGETW(ed->wMaxPacketSize) > 2) { 461 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n", 462 sc->sc_dev.dv_xname, 463 UGETW(ed->wMaxPacketSize))); 464 } 465 #endif 466 } 467 } 468 469 /* check whether we found all the endpoints we need */ 470 if (!sc->sc_epaddr[UMASS_BULKIN] || !sc->sc_epaddr[UMASS_BULKOUT] || 471 (sc->sc_wire == UMASS_WPROTO_CBI_I && 472 !sc->sc_epaddr[UMASS_INTRIN])) { 473 DPRINTF(UDMASS_USB, ("%s: endpoint not found %u/%u/%u\n", 474 sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKIN], 475 sc->sc_epaddr[UMASS_BULKOUT], 476 sc->sc_epaddr[UMASS_INTRIN])); 477 return; 478 } 479 480 /* 481 * Get the maximum LUN supported by the device. 482 */ 483 if (sc->sc_wire == UMASS_WPROTO_BBB) { 484 sc->maxlun = umass_bbb_get_max_lun(sc); 485 } else { 486 sc->maxlun = 0; 487 } 488 489 /* Open the bulk-in and -out pipe */ 490 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKOUT\n", 491 sc->sc_dev.dv_xname, sc->sc_iface, 492 sc->sc_epaddr[UMASS_BULKOUT])); 493 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKOUT], 494 USBD_EXCLUSIVE_USE, 495 &sc->sc_pipe[UMASS_BULKOUT]); 496 if (err) { 497 DPRINTF(UDMASS_USB, ("%s: cannot open %u-out pipe (bulk)\n", 498 sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKOUT])); 499 umass_disco(sc); 500 return; 501 } 502 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for BULKIN\n", 503 sc->sc_dev.dv_xname, sc->sc_iface, 504 sc->sc_epaddr[UMASS_BULKIN])); 505 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_BULKIN], 506 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_BULKIN]); 507 if (err) { 508 DPRINTF(UDMASS_USB, ("%s: could not open %u-in pipe (bulk)\n", 509 sc->sc_dev.dv_xname, sc->sc_epaddr[UMASS_BULKIN])); 510 umass_disco(sc); 511 return; 512 } 513 /* 514 * Open the intr-in pipe if the protocol is CBI with CCI. 515 * Note: early versions of the Zip drive do have an interrupt pipe, but 516 * this pipe is unused 517 * 518 * We do not open the interrupt pipe as an interrupt pipe, but as a 519 * normal bulk endpoint. We send an IN transfer down the wire at the 520 * appropriate time, because we know exactly when to expect data on 521 * that endpoint. This saves bandwidth, but more important, makes the 522 * code for handling the data on that endpoint simpler. No data 523 * arriving concurrently. 524 */ 525 if (sc->sc_wire == UMASS_WPROTO_CBI_I) { 526 DPRINTF(UDMASS_USB, ("%s: opening iface %p epaddr %d for INTRIN\n", 527 sc->sc_dev.dv_xname, sc->sc_iface, 528 sc->sc_epaddr[UMASS_INTRIN])); 529 err = usbd_open_pipe(sc->sc_iface, sc->sc_epaddr[UMASS_INTRIN], 530 USBD_EXCLUSIVE_USE, &sc->sc_pipe[UMASS_INTRIN]); 531 if (err) { 532 DPRINTF(UDMASS_USB, ("%s: couldn't open %u-in (intr)\n", 533 sc->sc_dev.dv_xname, 534 sc->sc_epaddr[UMASS_INTRIN])); 535 umass_disco(sc); 536 return; 537 } 538 } 539 540 /* initialisation of generic part */ 541 sc->transfer_state = TSTATE_IDLE; 542 543 /* request a sufficient number of xfer handles */ 544 for (i = 0; i < XFER_NR; i++) { 545 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device); 546 if (sc->transfer_xfer[i] == NULL) { 547 DPRINTF(UDMASS_USB, ("%s: Out of memory\n", 548 sc->sc_dev.dv_xname)); 549 umass_disco(sc); 550 return; 551 } 552 } 553 /* Allocate buffer for data transfer (it's huge). */ 554 switch (sc->sc_wire) { 555 case UMASS_WPROTO_BBB: 556 bno = XFER_BBB_DATA; 557 goto dalloc; 558 case UMASS_WPROTO_CBI: 559 bno = XFER_CBI_DATA; 560 goto dalloc; 561 case UMASS_WPROTO_CBI_I: 562 bno = XFER_CBI_DATA; 563 dalloc: 564 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno], 565 UMASS_MAX_TRANSFER_SIZE); 566 if (sc->data_buffer == NULL) { 567 umass_disco(sc); 568 return; 569 } 570 break; 571 default: 572 break; 573 } 574 575 /* Initialise the wire protocol specific methods */ 576 switch (sc->sc_wire) { 577 case UMASS_WPROTO_BBB: 578 sc->sc_methods = &umass_bbb_methods; 579 break; 580 case UMASS_WPROTO_CBI: 581 case UMASS_WPROTO_CBI_I: 582 sc->sc_methods = &umass_cbi_methods; 583 break; 584 default: 585 umass_disco(sc); 586 return; 587 } 588 589 error = 0; 590 switch (sc->sc_cmd) { 591 case UMASS_CPROTO_RBC: 592 case UMASS_CPROTO_SCSI: 593 error = umass_scsi_attach(sc); 594 break; 595 596 case UMASS_CPROTO_UFI: 597 case UMASS_CPROTO_ATAPI: 598 error = umass_atapi_attach(sc); 599 break; 600 601 case UMASS_CPROTO_ISD_ATA: 602 printf("%s: isdata not configured\n", sc->sc_dev.dv_xname); 603 break; 604 605 default: 606 printf("%s: command protocol=0x%x not supported\n", 607 sc->sc_dev.dv_xname, sc->sc_cmd); 608 umass_disco(sc); 609 return; 610 } 611 if (error) { 612 printf("%s: bus attach failed\n", sc->sc_dev.dv_xname); 613 umass_disco(sc); 614 return; 615 } 616 617 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", sc->sc_dev.dv_xname)); 618 } 619 620 int 621 umass_detach(struct device *self, int flags) 622 { 623 struct umass_softc *sc = (struct umass_softc *)self; 624 struct umassbus_softc *scbus; 625 int rv = 0, i, s; 626 627 DPRINTF(UDMASS_USB, ("%s: detached\n", sc->sc_dev.dv_xname)); 628 629 /* Abort the pipes to wake up any waiting processes. */ 630 for (i = 0 ; i < UMASS_NEP ; i++) { 631 if (sc->sc_pipe[i] != NULL) 632 usbd_abort_pipe(sc->sc_pipe[i]); 633 } 634 635 /* Do we really need reference counting? Perhaps in ioctl() */ 636 s = splusb(); 637 if (--sc->sc_refcnt >= 0) { 638 #ifdef DIAGNOSTIC 639 printf("%s: waiting for refcnt\n", sc->sc_dev.dv_xname); 640 #endif 641 /* Wait for processes to go away. */ 642 usb_detach_wait(&sc->sc_dev); 643 } 644 645 /* Free the buffers via callback. */ 646 if (sc->transfer_state != TSTATE_IDLE && sc->transfer_priv) { 647 sc->transfer_state = TSTATE_IDLE; 648 sc->transfer_cb(sc, sc->transfer_priv, 649 sc->transfer_datalen, 650 STATUS_WIRE_FAILED); 651 sc->transfer_priv = NULL; 652 } 653 splx(s); 654 655 scbus = sc->bus; 656 if (scbus != NULL) { 657 if (scbus->sc_child != NULL) 658 rv = config_detach(scbus->sc_child, flags); 659 free(scbus, M_DEVBUF); 660 sc->bus = NULL; 661 } 662 663 if (rv != 0) 664 return (rv); 665 666 umass_disco(sc); 667 668 return (rv); 669 } 670 671 int 672 umass_activate(struct device *dev, int act) 673 { 674 struct umass_softc *sc = (struct umass_softc *)dev; 675 struct umassbus_softc *scbus = sc->bus; 676 int rv = 0; 677 678 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n", 679 sc->sc_dev.dv_xname, act)); 680 681 switch (act) { 682 case DVACT_DEACTIVATE: 683 sc->sc_dying = 1; 684 if (scbus == NULL || scbus->sc_child == NULL) 685 break; 686 rv = config_deactivate(scbus->sc_child); 687 DPRINTF(UDMASS_USB, ("%s: umass_activate: child " 688 "returned %d\n", sc->sc_dev.dv_xname, rv)); 689 break; 690 } 691 return (rv); 692 } 693 694 void 695 umass_disco(struct umass_softc *sc) 696 { 697 int i; 698 699 DPRINTF(UDMASS_GEN, ("umass_disco\n")); 700 701 /* Free the xfers. */ 702 for (i = 0; i < XFER_NR; i++) 703 if (sc->transfer_xfer[i] != NULL) { 704 usbd_free_xfer(sc->transfer_xfer[i]); 705 sc->transfer_xfer[i] = NULL; 706 } 707 708 /* Remove all the pipes. */ 709 for (i = 0 ; i < UMASS_NEP ; i++) { 710 if (sc->sc_pipe[i] != NULL) { 711 usbd_close_pipe(sc->sc_pipe[i]); 712 sc->sc_pipe[i] = NULL; 713 } 714 } 715 } 716 717 /* 718 * Generic functions to handle transfers 719 */ 720 721 usbd_status 722 umass_polled_transfer(struct umass_softc *sc, usbd_xfer_handle xfer) 723 { 724 usbd_status err; 725 726 if (sc->sc_dying) 727 return (USBD_IOERROR); 728 729 /* 730 * If a polled transfer is already in progress, preserve the new 731 * usbd_xfer_handle and run it after the running one completes. 732 * This converts the recursive calls into the umass_*_state callbacks 733 * into iteration, preventing us from running out of stack under 734 * error conditions. 735 */ 736 if (sc->polling_depth) { 737 if (sc->next_polled_xfer) 738 panic("%s: got polled xfer %p, but %p already " 739 "pending\n", sc->sc_dev.dv_xname, xfer, 740 sc->next_polled_xfer); 741 742 DPRINTF(UDMASS_XFER, ("%s: saving polled xfer %p\n", 743 sc->sc_dev.dv_xname, xfer)); 744 sc->next_polled_xfer = xfer; 745 746 return (USBD_IN_PROGRESS); 747 } 748 749 sc->polling_depth++; 750 751 start_next_xfer: 752 DPRINTF(UDMASS_XFER, ("%s: start polled xfer %p\n", 753 sc->sc_dev.dv_xname, xfer)); 754 err = usbd_transfer(xfer); 755 if (err && err != USBD_IN_PROGRESS && sc->next_polled_xfer == NULL) { 756 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n", 757 sc->sc_dev.dv_xname, usbd_errstr(err))); 758 sc->polling_depth--; 759 return (err); 760 } 761 762 if (err && err != USBD_IN_PROGRESS) { 763 DPRINTF(UDMASS_XFER, ("umass_polled_xfer %p has error %s\n", 764 xfer, usbd_errstr(err))); 765 } 766 767 if (sc->next_polled_xfer != NULL) { 768 DPRINTF(UDMASS_XFER, ("umass_polled_xfer running next " 769 "transaction %p\n", sc->next_polled_xfer)); 770 xfer = sc->next_polled_xfer; 771 sc->next_polled_xfer = NULL; 772 goto start_next_xfer; 773 } 774 775 sc->polling_depth--; 776 777 return (USBD_NORMAL_COMPLETION); 778 } 779 780 usbd_status 781 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe, 782 void *buffer, int buflen, int flags, 783 usbd_xfer_handle xfer) 784 { 785 usbd_status err; 786 787 if (sc->sc_dying) 788 return (USBD_IOERROR); 789 790 /* Initialise a USB transfer and then schedule it */ 791 792 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen, 793 flags | sc->sc_xfer_flags, sc->timeout, sc->sc_methods->wire_state); 794 795 if (sc->sc_udev->bus->use_polling) { 796 DPRINTF(UDMASS_XFER,("%s: start polled xfer buffer=%p " 797 "buflen=%d flags=0x%x timeout=%d\n", sc->sc_dev.dv_xname, 798 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout)); 799 err = umass_polled_transfer(sc, xfer); 800 } else { 801 err = usbd_transfer(xfer); 802 DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d " 803 "flags=0x%x timeout=%d\n", sc->sc_dev.dv_xname, 804 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout)); 805 } 806 if (err && err != USBD_IN_PROGRESS) { 807 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n", 808 sc->sc_dev.dv_xname, usbd_errstr(err))); 809 return (err); 810 } 811 812 return (USBD_NORMAL_COMPLETION); 813 } 814 815 816 usbd_status 817 umass_setup_ctrl_transfer(struct umass_softc *sc, usb_device_request_t *req, 818 void *buffer, int buflen, int flags, usbd_xfer_handle xfer) 819 { 820 usbd_status err; 821 822 if (sc->sc_dying) 823 return (USBD_IOERROR); 824 825 /* Initialise a USB control transfer and then schedule it */ 826 827 usbd_setup_default_xfer(xfer, sc->sc_udev, (void *) sc, 828 USBD_DEFAULT_TIMEOUT, req, buffer, buflen, flags, 829 sc->sc_methods->wire_state); 830 831 if (sc->sc_udev->bus->use_polling) { 832 DPRINTF(UDMASS_XFER,("%s: start polled ctrl xfer buffer=%p " 833 "buflen=%d flags=0x%x\n", sc->sc_dev.dv_xname, buffer, 834 buflen, flags)); 835 err = umass_polled_transfer(sc, xfer); 836 } else { 837 DPRINTF(UDMASS_XFER,("%s: start ctrl xfer buffer=%p buflen=%d " 838 "flags=0x%x\n", sc->sc_dev.dv_xname, buffer, buflen, 839 flags)); 840 err = usbd_transfer(xfer); 841 } 842 if (err && err != USBD_IN_PROGRESS) { 843 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n", 844 sc->sc_dev.dv_xname, usbd_errstr(err))); 845 846 /* do not reset, as this would make us loop */ 847 return (err); 848 } 849 850 return (USBD_NORMAL_COMPLETION); 851 } 852 853 void 854 umass_adjust_transfer(struct umass_softc *sc) 855 { 856 switch (sc->sc_cmd) { 857 case UMASS_CPROTO_UFI: 858 sc->cbw.bCDBLength = UFI_COMMAND_LENGTH; 859 /* Adjust the length field in certain scsi commands. */ 860 switch (sc->cbw.CBWCDB[0]) { 861 case INQUIRY: 862 if (sc->transfer_datalen > 36) { 863 sc->transfer_datalen = 36; 864 sc->cbw.CBWCDB[4] = 36; 865 } 866 break; 867 case MODE_SENSE_BIG: 868 if (sc->transfer_datalen > 8) { 869 sc->transfer_datalen = 8; 870 sc->cbw.CBWCDB[7] = 0; 871 sc->cbw.CBWCDB[8] = 8; 872 } 873 break; 874 case REQUEST_SENSE: 875 if (sc->transfer_datalen > 18) { 876 sc->transfer_datalen = 18; 877 sc->cbw.CBWCDB[4] = 18; 878 } 879 break; 880 } 881 break; 882 case UMASS_CPROTO_ATAPI: 883 sc->cbw.bCDBLength = UFI_COMMAND_LENGTH; 884 break; 885 } 886 } 887 888 void 889 umass_clear_endpoint_stall(struct umass_softc *sc, int endpt, 890 usbd_xfer_handle xfer) 891 { 892 if (sc->sc_dying) 893 return; 894 895 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n", 896 sc->sc_dev.dv_xname, sc->sc_epaddr[endpt])); 897 898 usbd_clear_endpoint_toggle(sc->sc_pipe[endpt]); 899 900 sc->sc_req.bmRequestType = UT_WRITE_ENDPOINT; 901 sc->sc_req.bRequest = UR_CLEAR_FEATURE; 902 USETW(sc->sc_req.wValue, UF_ENDPOINT_HALT); 903 USETW(sc->sc_req.wIndex, sc->sc_epaddr[endpt]); 904 USETW(sc->sc_req.wLength, 0); 905 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, xfer); 906 } 907 908 #if 0 909 void 910 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv) 911 { 912 sc->transfer_cb = cb; 913 sc->transfer_priv = priv; 914 915 /* The reset is a forced reset, so no error (yet) */ 916 sc->reset(sc, STATUS_CMD_OK); 917 } 918 #endif 919 920 /* 921 * Bulk protocol specific functions 922 */ 923 924 void 925 umass_bbb_reset(struct umass_softc *sc, int status) 926 { 927 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB, 928 ("sc->sc_wire == 0x%02x wrong for umass_bbb_reset\n", 929 sc->sc_wire)); 930 931 if (sc->sc_dying) 932 return; 933 934 /* 935 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class) 936 * 937 * For Reset Recovery the host shall issue in the following order: 938 * a) a Bulk-Only Mass Storage Reset 939 * b) a Clear Feature HALT to the Bulk-In endpoint 940 * c) a Clear Feature HALT to the Bulk-Out endpoint 941 * 942 * This is done in 3 steps, states: 943 * TSTATE_BBB_RESET1 944 * TSTATE_BBB_RESET2 945 * TSTATE_BBB_RESET3 946 * 947 * If the reset doesn't succeed, the device should be port reset. 948 */ 949 950 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n", 951 sc->sc_dev.dv_xname)); 952 953 sc->transfer_state = TSTATE_BBB_RESET1; 954 sc->transfer_status = status; 955 956 /* reset is a class specific interface write */ 957 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 958 sc->sc_req.bRequest = UR_BBB_RESET; 959 USETW(sc->sc_req.wValue, 0); 960 USETW(sc->sc_req.wIndex, sc->sc_ifaceno); 961 USETW(sc->sc_req.wLength, 0); 962 umass_setup_ctrl_transfer(sc, &sc->sc_req, NULL, 0, 0, 963 sc->transfer_xfer[XFER_BBB_RESET1]); 964 } 965 966 void 967 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen, 968 void *data, int datalen, int dir, u_int timeout, 969 umass_callback cb, void *priv) 970 { 971 static int dCBWtag = 42; /* unique for CBW of transfer */ 972 usbd_status err; 973 974 DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n", 975 sc->sc_dev.dv_xname, *(u_char *)cmd)); 976 977 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB, 978 ("sc->sc_wire == 0x%02x wrong for umass_bbb_transfer\n", 979 sc->sc_wire)); 980 981 if (sc->sc_dying) { 982 sc->polled_xfer_status = USBD_IOERROR; 983 return; 984 } 985 986 /* Be a little generous. */ 987 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT; 988 989 /* 990 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly 991 * a data phase of datalen bytes from/to the device and finally a 992 * csw read phase. 993 * If the data direction was inbound a maximum of datalen bytes 994 * is stored in the buffer pointed to by data. 995 * 996 * umass_bbb_transfer initialises the transfer and lets the state 997 * machine in umass_bbb_state handle the completion. It uses the 998 * following states: 999 * TSTATE_BBB_COMMAND 1000 * -> TSTATE_BBB_DATA 1001 * -> TSTATE_BBB_STATUS 1002 * -> TSTATE_BBB_STATUS2 1003 * -> TSTATE_BBB_IDLE 1004 * 1005 * An error in any of those states will invoke 1006 * umass_bbb_reset. 1007 */ 1008 1009 /* check the given arguments */ 1010 KASSERT(datalen == 0 || data != NULL, 1011 ("%s: datalen > 0, but no buffer",sc->sc_dev.dv_xname)); 1012 KASSERT(cmdlen <= CBWCDBLENGTH, 1013 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)", 1014 sc->sc_dev.dv_xname, cmdlen, CBWCDBLENGTH)); 1015 KASSERT(dir == DIR_NONE || datalen > 0, 1016 ("%s: datalen == 0 while direction is not NONE\n", 1017 sc->sc_dev.dv_xname)); 1018 KASSERT(datalen == 0 || dir != DIR_NONE, 1019 ("%s: direction is NONE while datalen is not zero\n", 1020 sc->sc_dev.dv_xname)); 1021 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE, 1022 ("%s: CBW struct does not have the right size (%d vs. %d)\n", 1023 sc->sc_dev.dv_xname, 1024 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE)); 1025 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE, 1026 ("%s: CSW struct does not have the right size (%d vs. %d)\n", 1027 sc->sc_dev.dv_xname, 1028 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE)); 1029 1030 /* 1031 * Determine the direction of the data transfer and the length. 1032 * 1033 * dCBWDataTransferLength (datalen) : 1034 * This field indicates the number of bytes of data that the host 1035 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by 1036 * the Direction bit) during the execution of this command. If this 1037 * field is set to 0, the device will expect that no data will be 1038 * transferred IN or OUT during this command, regardless of the value 1039 * of the Direction bit defined in dCBWFlags. 1040 * 1041 * dCBWFlags (dir) : 1042 * The bits of the Flags field are defined as follows: 1043 * Bits 0-6 reserved 1044 * Bit 7 Direction - this bit shall be ignored if the 1045 * dCBWDataTransferLength field is zero. 1046 * 0 = data Out from host to device 1047 * 1 = data In from device to host 1048 */ 1049 1050 /* Fill in the Command Block Wrapper */ 1051 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE); 1052 USETDW(sc->cbw.dCBWTag, dCBWtag); 1053 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */ 1054 USETDW(sc->cbw.dCBWDataTransferLength, datalen); 1055 /* DIR_NONE is treated as DIR_OUT (0x00) */ 1056 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT); 1057 sc->cbw.bCBWLUN = lun; 1058 sc->cbw.bCDBLength = cmdlen; 1059 bzero(sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB)); 1060 memcpy(sc->cbw.CBWCDB, cmd, cmdlen); 1061 1062 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw)); 1063 1064 /* store the details for the data transfer phase */ 1065 sc->transfer_dir = dir; 1066 sc->transfer_data = data; 1067 sc->transfer_datalen = datalen; 1068 sc->transfer_actlen = 0; 1069 sc->transfer_cb = cb; 1070 sc->transfer_priv = priv; 1071 sc->transfer_status = STATUS_CMD_OK; 1072 1073 /* move from idle to the command state */ 1074 sc->transfer_state = TSTATE_BBB_COMMAND; 1075 1076 /* Send the CBW from host to device via bulk-out endpoint. */ 1077 umass_adjust_transfer(sc); 1078 if ((err = umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT], 1079 &sc->cbw, UMASS_BBB_CBW_SIZE, 0, 1080 sc->transfer_xfer[XFER_BBB_CBW]))) 1081 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1082 1083 if (sc->sc_udev->bus->use_polling) 1084 sc->polled_xfer_status = err; 1085 } 1086 1087 void 1088 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv, 1089 usbd_status err) 1090 { 1091 struct umass_softc *sc = (struct umass_softc *) priv; 1092 usbd_xfer_handle next_xfer; 1093 1094 KASSERT(sc->sc_wire & UMASS_WPROTO_BBB, 1095 ("sc->sc_wire == 0x%02x wrong for umass_bbb_state\n", 1096 sc->sc_wire)); 1097 1098 if (sc->sc_dying) 1099 return; 1100 1101 /* 1102 * State handling for BBB transfers. 1103 * 1104 * The subroutine is rather long. It steps through the states given in 1105 * Annex A of the Bulk-Only specification. 1106 * Each state first does the error handling of the previous transfer 1107 * and then prepares the next transfer. 1108 * Each transfer is done asynchronously so after the request/transfer 1109 * has been submitted you will find a 'return;'. 1110 */ 1111 1112 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n", 1113 sc->sc_dev.dv_xname, sc->transfer_state, 1114 states[sc->transfer_state], xfer, usbd_errstr(err))); 1115 1116 switch (sc->transfer_state) { 1117 1118 /***** Bulk Transfer *****/ 1119 case TSTATE_BBB_COMMAND: 1120 /* Command transport phase, error handling */ 1121 if (err) { 1122 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n", 1123 sc->sc_dev.dv_xname)); 1124 /* If the device detects that the CBW is invalid, then 1125 * the device may STALL both bulk endpoints and require 1126 * a Bulk-Reset 1127 */ 1128 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1129 return; 1130 } 1131 1132 /* Data transport phase, setup transfer */ 1133 sc->transfer_state = TSTATE_BBB_DATA; 1134 if (sc->transfer_dir == DIR_IN) { 1135 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN], 1136 sc->data_buffer, sc->transfer_datalen, 1137 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1138 sc->transfer_xfer[XFER_BBB_DATA])) 1139 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1140 1141 return; 1142 } else if (sc->transfer_dir == DIR_OUT) { 1143 memcpy(sc->data_buffer, sc->transfer_data, 1144 sc->transfer_datalen); 1145 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT], 1146 sc->data_buffer, sc->transfer_datalen, 1147 USBD_NO_COPY,/* fixed length transfer */ 1148 sc->transfer_xfer[XFER_BBB_DATA])) 1149 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1150 1151 return; 1152 } else { 1153 DPRINTF(UDMASS_BBB, ("%s: no data phase\n", 1154 sc->sc_dev.dv_xname)); 1155 } 1156 1157 /* FALLTHROUGH if no data phase, err == 0 */ 1158 case TSTATE_BBB_DATA: 1159 /* Command transport phase error handling (ignored if no data 1160 * phase (fallthrough from previous state)) */ 1161 if (sc->transfer_dir != DIR_NONE) { 1162 /* retrieve the length of the transfer that was done */ 1163 usbd_get_xfer_status(xfer, NULL, NULL, 1164 &sc->transfer_actlen, NULL); 1165 DPRINTF(UDMASS_BBB, ("%s: BBB_DATA actlen=%d\n", 1166 sc->sc_dev.dv_xname, sc->transfer_actlen)); 1167 1168 if (err) { 1169 DPRINTF(UDMASS_BBB, ("%s: Data-%s %d failed, " 1170 "%s\n", sc->sc_dev.dv_xname, 1171 (sc->transfer_dir == DIR_IN?"in":"out"), 1172 sc->transfer_datalen,usbd_errstr(err))); 1173 1174 if (err == USBD_STALLED) { 1175 sc->transfer_state = TSTATE_BBB_DCLEAR; 1176 umass_clear_endpoint_stall(sc, 1177 (sc->transfer_dir == DIR_IN? 1178 UMASS_BULKIN:UMASS_BULKOUT), 1179 sc->transfer_xfer[XFER_BBB_DCLEAR]); 1180 } else { 1181 /* Unless the error is a pipe stall the 1182 * error is fatal. 1183 */ 1184 umass_bbb_reset(sc,STATUS_WIRE_FAILED); 1185 } 1186 return; 1187 } 1188 } 1189 1190 /* FALLTHROUGH, err == 0 (no data phase or successful) */ 1191 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */ 1192 if (sc->transfer_dir == DIR_IN) 1193 memcpy(sc->transfer_data, sc->data_buffer, 1194 sc->transfer_actlen); 1195 1196 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN) 1197 umass_dump_buffer(sc, sc->transfer_data, 1198 sc->transfer_datalen, 48)); 1199 1200 /* FALLTHROUGH, err == 0 (no data phase or successful) */ 1201 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */ 1202 /* Reading of CSW after bulk stall condition in data phase 1203 * (TSTATE_BBB_DATA2) or bulk-in stall condition after 1204 * reading CSW (TSTATE_BBB_SCLEAR). 1205 * In the case of no data phase or successful data phase, 1206 * err == 0 and the following if block is passed. 1207 */ 1208 if (err) { /* should not occur */ 1209 printf("%s: BBB bulk-%s stall clear failed, %s\n", 1210 sc->sc_dev.dv_xname, 1211 (sc->transfer_dir == DIR_IN? "in":"out"), 1212 usbd_errstr(err)); 1213 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1214 return; 1215 } 1216 1217 /* Status transport phase, setup transfer */ 1218 if (sc->transfer_state == TSTATE_BBB_COMMAND || 1219 sc->transfer_state == TSTATE_BBB_DATA || 1220 sc->transfer_state == TSTATE_BBB_DCLEAR) { 1221 /* After no data phase, successful data phase and 1222 * after clearing bulk-in/-out stall condition 1223 */ 1224 sc->transfer_state = TSTATE_BBB_STATUS1; 1225 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1]; 1226 } else { 1227 /* After first attempt of fetching CSW */ 1228 sc->transfer_state = TSTATE_BBB_STATUS2; 1229 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2]; 1230 } 1231 1232 /* Read the Command Status Wrapper via bulk-in endpoint. */ 1233 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN], 1234 &sc->csw, UMASS_BBB_CSW_SIZE, 0, next_xfer)) { 1235 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1236 return; 1237 } 1238 1239 return; 1240 case TSTATE_BBB_STATUS1: /* first attempt */ 1241 case TSTATE_BBB_STATUS2: /* second attempt */ 1242 /* Status transfer, error handling */ 1243 if (err) { 1244 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n", 1245 sc->sc_dev.dv_xname, usbd_errstr(err), 1246 (sc->transfer_state == TSTATE_BBB_STATUS1? 1247 ", retrying":""))); 1248 1249 /* If this was the first attempt at fetching the CSW 1250 * retry it, otherwise fail. 1251 */ 1252 if (sc->transfer_state == TSTATE_BBB_STATUS1) { 1253 sc->transfer_state = TSTATE_BBB_SCLEAR; 1254 umass_clear_endpoint_stall(sc, UMASS_BULKIN, 1255 sc->transfer_xfer[XFER_BBB_SCLEAR]); 1256 return; 1257 } else { 1258 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1259 return; 1260 } 1261 } 1262 1263 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw)); 1264 1265 /* Translate weird command-status signatures. */ 1266 if ((sc->sc_quirks & UMASS_QUIRK_WRONG_CSWSIG) && 1267 UGETDW(sc->csw.dCSWSignature) == CSWSIGNATURE_OLYMPUS_C1) 1268 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE); 1269 1270 /* Translate invalid command-status tags */ 1271 if (sc->sc_quirks & UMASS_QUIRK_WRONG_CSWTAG) 1272 USETDW(sc->csw.dCSWTag, UGETDW(sc->cbw.dCBWTag)); 1273 1274 /* Check CSW and handle any error */ 1275 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) { 1276 /* Invalid CSW: Wrong signature or wrong tag might 1277 * indicate that the device is confused -> reset it. 1278 */ 1279 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n", 1280 sc->sc_dev.dv_xname, 1281 UGETDW(sc->csw.dCSWSignature), 1282 CSWSIGNATURE); 1283 1284 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1285 return; 1286 } else if (UGETDW(sc->csw.dCSWTag) 1287 != UGETDW(sc->cbw.dCBWTag)) { 1288 printf("%s: Invalid CSW: tag %d should be %d\n", 1289 sc->sc_dev.dv_xname, 1290 UGETDW(sc->csw.dCSWTag), 1291 UGETDW(sc->cbw.dCBWTag)); 1292 1293 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1294 return; 1295 1296 /* CSW is valid here */ 1297 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) { 1298 printf("%s: Invalid CSW: status %d > %d\n", 1299 sc->sc_dev.dv_xname, 1300 sc->csw.bCSWStatus, 1301 CSWSTATUS_PHASE); 1302 1303 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1304 return; 1305 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) { 1306 printf("%s: Phase Error, residue = %d\n", 1307 sc->sc_dev.dv_xname, 1308 UGETDW(sc->csw.dCSWDataResidue)); 1309 1310 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1311 return; 1312 1313 } else if (sc->transfer_actlen > sc->transfer_datalen) { 1314 /* Buffer overrun! Don't let this go by unnoticed */ 1315 panic("%s: transferred %d bytes instead of %d bytes", 1316 sc->sc_dev.dv_xname, 1317 sc->transfer_actlen, sc->transfer_datalen); 1318 #if 0 1319 } else if (sc->transfer_datalen - sc->transfer_actlen 1320 != UGETDW(sc->csw.dCSWDataResidue)) { 1321 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n", 1322 sc->sc_dev.dv_xname, 1323 sc->transfer_datalen - sc->transfer_actlen, 1324 UGETDW(sc->csw.dCSWDataResidue))); 1325 1326 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1327 return; 1328 #endif 1329 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) { 1330 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n", 1331 sc->sc_dev.dv_xname, 1332 UGETDW(sc->csw.dCSWDataResidue))); 1333 1334 /* SCSI command failed but transfer was successful */ 1335 sc->transfer_state = TSTATE_IDLE; 1336 sc->transfer_cb(sc, sc->transfer_priv, 1337 UGETDW(sc->csw.dCSWDataResidue), 1338 STATUS_CMD_FAILED); 1339 1340 return; 1341 1342 } else { /* success */ 1343 sc->transfer_state = TSTATE_IDLE; 1344 sc->transfer_cb(sc, sc->transfer_priv, 1345 UGETDW(sc->csw.dCSWDataResidue), 1346 STATUS_CMD_OK); 1347 1348 return; 1349 } 1350 1351 /***** Bulk Reset *****/ 1352 case TSTATE_BBB_RESET1: 1353 if (err) 1354 printf("%s: BBB reset failed, %s\n", 1355 sc->sc_dev.dv_xname, usbd_errstr(err)); 1356 1357 sc->transfer_state = TSTATE_BBB_RESET2; 1358 umass_clear_endpoint_stall(sc, UMASS_BULKIN, 1359 sc->transfer_xfer[XFER_BBB_RESET2]); 1360 1361 return; 1362 case TSTATE_BBB_RESET2: 1363 if (err) /* should not occur */ 1364 printf("%s: BBB bulk-in clear stall failed, %s\n", 1365 sc->sc_dev.dv_xname, usbd_errstr(err)); 1366 /* no error recovery, otherwise we end up in a loop */ 1367 1368 sc->transfer_state = TSTATE_BBB_RESET3; 1369 umass_clear_endpoint_stall(sc, UMASS_BULKOUT, 1370 sc->transfer_xfer[XFER_BBB_RESET3]); 1371 1372 return; 1373 case TSTATE_BBB_RESET3: 1374 if (err) /* should not occur */ 1375 printf("%s: BBB bulk-out clear stall failed, %s\n", 1376 sc->sc_dev.dv_xname, usbd_errstr(err)); 1377 /* no error recovery, otherwise we end up in a loop */ 1378 1379 sc->transfer_state = TSTATE_IDLE; 1380 if (sc->transfer_priv) { 1381 sc->transfer_cb(sc, sc->transfer_priv, 1382 sc->transfer_datalen, 1383 sc->transfer_status); 1384 } 1385 1386 return; 1387 1388 /***** Default *****/ 1389 default: 1390 panic("%s: Unknown state %d", 1391 sc->sc_dev.dv_xname, sc->transfer_state); 1392 } 1393 } 1394 1395 /* 1396 * Command/Bulk/Interrupt (CBI) specific functions 1397 */ 1398 1399 int 1400 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, 1401 usbd_xfer_handle xfer) 1402 { 1403 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1404 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n", 1405 sc->sc_wire)); 1406 1407 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1408 sc->sc_req.bRequest = UR_CBI_ADSC; 1409 USETW(sc->sc_req.wValue, 0); 1410 USETW(sc->sc_req.wIndex, sc->sc_ifaceno); 1411 USETW(sc->sc_req.wLength, buflen); 1412 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer, 1413 buflen, 0, xfer); 1414 } 1415 1416 1417 void 1418 umass_cbi_reset(struct umass_softc *sc, int status) 1419 { 1420 int i; 1421 # define SEND_DIAGNOSTIC_CMDLEN 12 1422 1423 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1424 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n", 1425 sc->sc_wire)); 1426 1427 if (sc->sc_dying) 1428 return; 1429 1430 /* 1431 * Command Block Reset Protocol 1432 * 1433 * First send a reset request to the device. Then clear 1434 * any possibly stalled bulk endpoints. 1435 1436 * This is done in 3 steps, states: 1437 * TSTATE_CBI_RESET1 1438 * TSTATE_CBI_RESET2 1439 * TSTATE_CBI_RESET3 1440 * 1441 * If the reset doesn't succeed, the device should be port reset. 1442 */ 1443 1444 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n", 1445 sc->sc_dev.dv_xname)); 1446 1447 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN, 1448 ("%s: CBL struct is too small (%d < %d)\n", 1449 sc->sc_dev.dv_xname, 1450 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN)); 1451 1452 sc->transfer_state = TSTATE_CBI_RESET1; 1453 sc->transfer_status = status; 1454 1455 /* The 0x1d code is the SEND DIAGNOSTIC command. To distinguish between 1456 * the two the last 10 bytes of the cbl is filled with 0xff (section 1457 * 2.2 of the CBI spec). 1458 */ 1459 sc->cbl[0] = 0x1d; /* Command Block Reset */ 1460 sc->cbl[1] = 0x04; 1461 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++) 1462 sc->cbl[i] = 0xff; 1463 1464 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 1465 sc->transfer_xfer[XFER_CBI_RESET1]); 1466 /* XXX if the command fails we should reset the port on the bub */ 1467 } 1468 1469 void 1470 umass_cbi_transfer(struct umass_softc *sc, int lun, 1471 void *cmd, int cmdlen, void *data, int datalen, int dir, 1472 u_int timeout, umass_callback cb, void *priv) 1473 { 1474 usbd_status err; 1475 1476 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n", 1477 sc->sc_dev.dv_xname, *(u_char *)cmd, datalen)); 1478 1479 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1480 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n", 1481 sc->sc_wire)); 1482 1483 if (sc->sc_dying) { 1484 sc->polled_xfer_status = USBD_IOERROR; 1485 return; 1486 } 1487 1488 /* Be a little generous. */ 1489 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT; 1490 1491 /* 1492 * Do a CBI transfer with cmdlen bytes from cmd, possibly 1493 * a data phase of datalen bytes from/to the device and finally a 1494 * csw read phase. 1495 * If the data direction was inbound a maximum of datalen bytes 1496 * is stored in the buffer pointed to by data. 1497 * 1498 * umass_cbi_transfer initialises the transfer and lets the state 1499 * machine in umass_cbi_state handle the completion. It uses the 1500 * following states: 1501 * TSTATE_CBI_COMMAND 1502 * -> XXX fill in 1503 * 1504 * An error in any of those states will invoke 1505 * umass_cbi_reset. 1506 */ 1507 1508 /* check the given arguments */ 1509 KASSERT(datalen == 0 || data != NULL, 1510 ("%s: datalen > 0, but no buffer",sc->sc_dev.dv_xname)); 1511 KASSERT(datalen == 0 || dir != DIR_NONE, 1512 ("%s: direction is NONE while datalen is not zero\n", 1513 sc->sc_dev.dv_xname)); 1514 1515 /* store the details for the data transfer phase */ 1516 sc->transfer_dir = dir; 1517 sc->transfer_data = data; 1518 sc->transfer_datalen = datalen; 1519 sc->transfer_actlen = 0; 1520 sc->transfer_cb = cb; 1521 sc->transfer_priv = priv; 1522 sc->transfer_status = STATUS_CMD_OK; 1523 1524 /* move from idle to the command state */ 1525 sc->transfer_state = TSTATE_CBI_COMMAND; 1526 1527 /* Send the Command Block from host to device via control endpoint. */ 1528 sc->cbw.bCDBLength = cmdlen; 1529 bzero(sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB)); 1530 memcpy(sc->cbw.CBWCDB, cmd, cmdlen); 1531 umass_adjust_transfer(sc); 1532 if ((err = umass_cbi_adsc(sc, (void *)sc->cbw.CBWCDB, sc->cbw.bCDBLength, 1533 sc->transfer_xfer[XFER_CBI_CB]))) 1534 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1535 1536 if (sc->sc_udev->bus->use_polling) 1537 sc->polled_xfer_status = err; 1538 } 1539 1540 void 1541 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv, 1542 usbd_status err) 1543 { 1544 struct umass_softc *sc = (struct umass_softc *) priv; 1545 1546 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1547 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n", 1548 sc->sc_wire)); 1549 1550 if (sc->sc_dying) 1551 return; 1552 1553 /* 1554 * State handling for CBI transfers. 1555 */ 1556 1557 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n", 1558 sc->sc_dev.dv_xname, sc->transfer_state, 1559 states[sc->transfer_state], xfer, usbd_errstr(err))); 1560 1561 switch (sc->transfer_state) { 1562 1563 /***** CBI Transfer *****/ 1564 case TSTATE_CBI_COMMAND: 1565 if (err == USBD_STALLED) { 1566 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n", 1567 sc->sc_dev.dv_xname)); 1568 /* Status transport by control pipe (section 2.3.2.1). 1569 * The command contained in the command block failed. 1570 * 1571 * The control pipe has already been unstalled by the 1572 * USB stack. 1573 * Section 2.4.3.1.1 states that the bulk in endpoints 1574 * should not stalled at this point. 1575 */ 1576 1577 sc->transfer_state = TSTATE_IDLE; 1578 sc->transfer_cb(sc, sc->transfer_priv, 1579 sc->transfer_datalen, 1580 STATUS_CMD_FAILED); 1581 1582 return; 1583 } else if (err) { 1584 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n", 1585 sc->sc_dev.dv_xname)); 1586 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1587 return; 1588 } 1589 1590 /* Data transport phase, setup transfer */ 1591 sc->transfer_state = TSTATE_CBI_DATA; 1592 if (sc->transfer_dir == DIR_IN) { 1593 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN], 1594 sc->data_buffer, sc->transfer_datalen, 1595 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1596 sc->transfer_xfer[XFER_CBI_DATA])) 1597 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1598 1599 return; 1600 } else if (sc->transfer_dir == DIR_OUT) { 1601 memcpy(sc->data_buffer, sc->transfer_data, 1602 sc->transfer_datalen); 1603 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT], 1604 sc->data_buffer, sc->transfer_datalen, 1605 USBD_NO_COPY,/* fixed length transfer */ 1606 sc->transfer_xfer[XFER_CBI_DATA])) 1607 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1608 1609 return; 1610 } else { 1611 DPRINTF(UDMASS_CBI, ("%s: no data phase\n", 1612 sc->sc_dev.dv_xname)); 1613 } 1614 1615 /* FALLTHROUGH if no data phase, err == 0 */ 1616 case TSTATE_CBI_DATA: 1617 /* Command transport phase error handling (ignored if no data 1618 * phase (fallthrough from previous state)) */ 1619 if (sc->transfer_dir != DIR_NONE) { 1620 /* retrieve the length of the transfer that was done */ 1621 usbd_get_xfer_status(xfer, NULL, NULL, 1622 &sc->transfer_actlen, NULL); 1623 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n", 1624 sc->sc_dev.dv_xname, sc->transfer_actlen)); 1625 1626 if (err) { 1627 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, " 1628 "%s\n", sc->sc_dev.dv_xname, 1629 (sc->transfer_dir == DIR_IN?"in":"out"), 1630 sc->transfer_datalen,usbd_errstr(err))); 1631 1632 if (err == USBD_STALLED) { 1633 sc->transfer_state = TSTATE_CBI_DCLEAR; 1634 umass_clear_endpoint_stall(sc, 1635 (sc->transfer_dir == DIR_IN? 1636 UMASS_BULKIN:UMASS_BULKOUT), 1637 sc->transfer_xfer[XFER_CBI_DCLEAR]); 1638 } else { 1639 /* Unless the error is a pipe stall the 1640 * error is fatal. 1641 */ 1642 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1643 } 1644 return; 1645 } 1646 } 1647 1648 if (sc->transfer_dir == DIR_IN) 1649 memcpy(sc->transfer_data, sc->data_buffer, 1650 sc->transfer_actlen); 1651 1652 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN) 1653 umass_dump_buffer(sc, sc->transfer_data, 1654 sc->transfer_actlen, 48)); 1655 1656 /* Status phase */ 1657 if (sc->sc_wire == UMASS_WPROTO_CBI_I) { 1658 sc->transfer_state = TSTATE_CBI_STATUS; 1659 memset(&sc->sbl, 0, sizeof(sc->sbl)); 1660 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN], 1661 &sc->sbl, sizeof(sc->sbl), 1662 0, /* fixed length transfer */ 1663 sc->transfer_xfer[XFER_CBI_STATUS])) 1664 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1665 } else { 1666 /* No command completion interrupt. Request 1667 * sense to get status of command. 1668 */ 1669 sc->transfer_state = TSTATE_IDLE; 1670 sc->transfer_cb(sc, sc->transfer_priv, 1671 sc->transfer_datalen - sc->transfer_actlen, 1672 STATUS_CMD_UNKNOWN); 1673 } 1674 return; 1675 1676 case TSTATE_CBI_STATUS: 1677 if (err) { 1678 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n", 1679 sc->sc_dev.dv_xname)); 1680 /* Status transport by interrupt pipe (section 2.3.2.2). 1681 */ 1682 1683 if (err == USBD_STALLED) { 1684 sc->transfer_state = TSTATE_CBI_SCLEAR; 1685 umass_clear_endpoint_stall(sc, UMASS_INTRIN, 1686 sc->transfer_xfer[XFER_CBI_SCLEAR]); 1687 } else { 1688 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1689 } 1690 return; 1691 } 1692 1693 /* Dissect the information in the buffer */ 1694 1695 { 1696 u_int32_t actlen; 1697 usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL); 1698 DPRINTF(UDMASS_CBI, ("%s: CBI_STATUS actlen=%d\n", 1699 sc->sc_dev.dv_xname, actlen)); 1700 if (actlen != 2) 1701 break; 1702 } 1703 1704 if (sc->sc_cmd == UMASS_CPROTO_UFI) { 1705 int status; 1706 1707 /* Section 3.4.3.1.3 specifies that the UFI command 1708 * protocol returns an ASC and ASCQ in the interrupt 1709 * data block. 1710 */ 1711 1712 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, " 1713 "ASCQ = 0x%02x\n", 1714 sc->sc_dev.dv_xname, 1715 sc->sbl.ufi.asc, sc->sbl.ufi.ascq)); 1716 1717 if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) || 1718 sc->sc_sense) 1719 status = STATUS_CMD_OK; 1720 else 1721 status = STATUS_CMD_FAILED; 1722 1723 /* No autosense, command successful */ 1724 sc->transfer_state = TSTATE_IDLE; 1725 sc->transfer_cb(sc, sc->transfer_priv, 1726 sc->transfer_datalen - sc->transfer_actlen, status); 1727 } else { 1728 /* Command Interrupt Data Block */ 1729 1730 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n", 1731 sc->sc_dev.dv_xname, 1732 sc->sbl.common.type, sc->sbl.common.value)); 1733 1734 if (sc->sbl.common.type == IDB_TYPE_CCI) { 1735 int status; 1736 switch (sc->sbl.common.value & 1737 IDB_VALUE_STATUS_MASK) { 1738 case IDB_VALUE_PASS: 1739 status = STATUS_CMD_OK; 1740 break; 1741 case IDB_VALUE_FAIL: 1742 case IDB_VALUE_PERSISTENT: 1743 status = STATUS_CMD_FAILED; 1744 break; 1745 case IDB_VALUE_PHASE: 1746 default: 1747 status = STATUS_WIRE_FAILED; 1748 break; 1749 } 1750 1751 sc->transfer_state = TSTATE_IDLE; 1752 sc->transfer_cb(sc, sc->transfer_priv, 1753 sc->transfer_datalen - sc->transfer_actlen, 1754 status); 1755 } 1756 } 1757 return; 1758 1759 case TSTATE_CBI_DCLEAR: 1760 if (err) { /* should not occur */ 1761 printf("%s: CBI bulk-in/out stall clear failed, %s\n", 1762 sc->sc_dev.dv_xname, usbd_errstr(err)); 1763 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1764 } else { 1765 sc->transfer_state = TSTATE_IDLE; 1766 sc->transfer_cb(sc, sc->transfer_priv, 1767 sc->transfer_datalen, STATUS_CMD_FAILED); 1768 } 1769 return; 1770 1771 case TSTATE_CBI_SCLEAR: 1772 if (err) { /* should not occur */ 1773 printf("%s: CBI intr-in stall clear failed, %s\n", 1774 sc->sc_dev.dv_xname, usbd_errstr(err)); 1775 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1776 } else { 1777 sc->transfer_state = TSTATE_IDLE; 1778 sc->transfer_cb(sc, sc->transfer_priv, 1779 sc->transfer_datalen, STATUS_CMD_FAILED); 1780 } 1781 return; 1782 1783 /***** CBI Reset *****/ 1784 case TSTATE_CBI_RESET1: 1785 if (err) 1786 printf("%s: CBI reset failed, %s\n", 1787 sc->sc_dev.dv_xname, usbd_errstr(err)); 1788 1789 sc->transfer_state = TSTATE_CBI_RESET2; 1790 umass_clear_endpoint_stall(sc, UMASS_BULKIN, 1791 sc->transfer_xfer[XFER_CBI_RESET2]); 1792 1793 return; 1794 case TSTATE_CBI_RESET2: 1795 if (err) /* should not occur */ 1796 printf("%s: CBI bulk-in stall clear failed, %s\n", 1797 sc->sc_dev.dv_xname, usbd_errstr(err)); 1798 /* no error recovery, otherwise we end up in a loop */ 1799 1800 sc->transfer_state = TSTATE_CBI_RESET3; 1801 umass_clear_endpoint_stall(sc, UMASS_BULKOUT, 1802 sc->transfer_xfer[XFER_CBI_RESET3]); 1803 1804 return; 1805 case TSTATE_CBI_RESET3: 1806 if (err) /* should not occur */ 1807 printf("%s: CBI bulk-out stall clear failed, %s\n", 1808 sc->sc_dev.dv_xname, usbd_errstr(err)); 1809 /* no error recovery, otherwise we end up in a loop */ 1810 1811 sc->transfer_state = TSTATE_IDLE; 1812 if (sc->transfer_priv) { 1813 sc->transfer_cb(sc, sc->transfer_priv, 1814 sc->transfer_datalen, 1815 sc->transfer_status); 1816 } 1817 1818 return; 1819 1820 1821 /***** Default *****/ 1822 default: 1823 panic("%s: Unknown state %d", 1824 sc->sc_dev.dv_xname, sc->transfer_state); 1825 } 1826 } 1827 1828 u_int8_t 1829 umass_bbb_get_max_lun(struct umass_softc *sc) 1830 { 1831 usb_device_request_t req; 1832 usbd_status err; 1833 u_int8_t maxlun = 0; 1834 u_int8_t buf = 0; 1835 1836 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", sc->sc_dev.dv_xname)); 1837 1838 /* The Get Max Lun command is a class-specific request. */ 1839 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1840 req.bRequest = UR_BBB_GET_MAX_LUN; 1841 USETW(req.wValue, 0); 1842 USETW(req.wIndex, sc->sc_ifaceno); 1843 USETW(req.wLength, 1); 1844 1845 err = usbd_do_request_flags(sc->sc_udev, &req, &buf, 1846 USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT); 1847 1848 switch (err) { 1849 case USBD_NORMAL_COMPLETION: 1850 maxlun = buf; 1851 break; 1852 1853 default: 1854 /* XXX Should we port_reset the device? */ 1855 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported (%s)\n", 1856 sc->sc_dev.dv_xname, usbd_errstr(err))); 1857 break; 1858 } 1859 1860 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n", sc->sc_dev.dv_xname, maxlun)); 1861 return (maxlun); 1862 } 1863 1864 #ifdef UMASS_DEBUG 1865 void 1866 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw) 1867 { 1868 int clen = cbw->bCDBLength; 1869 int dlen = UGETDW(cbw->dCBWDataTransferLength); 1870 u_int8_t *c = cbw->CBWCDB; 1871 int tag = UGETDW(cbw->dCBWTag); 1872 int flags = cbw->bCBWFlags; 1873 1874 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d " 1875 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), " 1876 "data = %d bytes, dir = %s\n", 1877 sc->sc_dev.dv_xname, tag, clen, 1878 c[0], c[1], c[2], c[3], c[4], c[5], 1879 c[6], c[7], c[8], c[9], 1880 (clen > 10? "...":""), 1881 dlen, (flags == CBWFLAGS_IN? "in": 1882 (flags == CBWFLAGS_OUT? "out":"<invalid>")))); 1883 } 1884 1885 void 1886 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw) 1887 { 1888 int sig = UGETDW(csw->dCSWSignature); 1889 int tag = UGETDW(csw->dCSWTag); 1890 int res = UGETDW(csw->dCSWDataResidue); 1891 int status = csw->bCSWStatus; 1892 1893 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, " 1894 "res = %d, status = 0x%02x (%s)\n", sc->sc_dev.dv_xname, 1895 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"), 1896 tag, res, 1897 status, (status == CSWSTATUS_GOOD? "good": 1898 (status == CSWSTATUS_FAILED? "failed": 1899 (status == CSWSTATUS_PHASE? "phase":"<invalid>"))))); 1900 } 1901 1902 void 1903 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen, 1904 int printlen) 1905 { 1906 int i, j; 1907 char s1[40]; 1908 char s2[40]; 1909 char s3[5]; 1910 1911 s1[0] = '\0'; 1912 s3[0] = '\0'; 1913 1914 snprintf(s2, sizeof s2, " buffer=%p, buflen=%d", buffer, buflen); 1915 for (i = 0; i < buflen && i < printlen; i++) { 1916 j = i % 16; 1917 if (j == 0 && i != 0) { 1918 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n", 1919 sc->sc_dev.dv_xname, s1, s2)); 1920 s2[0] = '\0'; 1921 } 1922 snprintf(&s1[j*2], sizeof s1 - j*2, "%02x", buffer[i] & 0xff); 1923 } 1924 if (buflen > printlen) 1925 snprintf(s3, sizeof s3, " ..."); 1926 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n", 1927 sc->sc_dev.dv_xname, s1, s2, s3)); 1928 } 1929 #endif 1930