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