1 /* $OpenBSD: umass.c,v 1.76 2020/02/22 14:01:34 jasper 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, flags, 793 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)); 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)); 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 > 36) { 827 sc->transfer_datalen = 36; 828 sc->cbw.CBWCDB[4] = 36; 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 sc->transfer_state = TSTATE_IDLE; 1303 if (sc->transfer_dir == DIR_IN) { 1304 sc->transfer_actlen = sc->transfer_datalen - 1305 UGETDW(sc->csw.dCSWDataResidue); 1306 memcpy(sc->transfer_data, sc->data_buffer, 1307 sc->transfer_actlen); 1308 } 1309 sc->transfer_cb(sc, sc->transfer_priv, 1310 UGETDW(sc->csw.dCSWDataResidue), 1311 STATUS_CMD_OK); 1312 1313 return; 1314 } 1315 1316 /***** Bulk Reset *****/ 1317 case TSTATE_BBB_RESET1: 1318 if (err) 1319 DPRINTF(UDMASS_BBB, ("%s: BBB reset failed, %s\n", 1320 sc->sc_dev.dv_xname, usbd_errstr(err))); 1321 1322 sc->transfer_state = TSTATE_BBB_RESET2; 1323 umass_clear_endpoint_stall(sc, UMASS_BULKIN, 1324 sc->transfer_xfer[XFER_BBB_RESET2]); 1325 1326 return; 1327 case TSTATE_BBB_RESET2: 1328 if (err) /* should not occur */ 1329 DPRINTF(UDMASS_BBB, ("%s: BBB bulk-in clear stall" 1330 " failed, %s\n", sc->sc_dev.dv_xname, 1331 usbd_errstr(err))); 1332 /* no error recovery, otherwise we end up in a loop */ 1333 1334 sc->transfer_state = TSTATE_BBB_RESET3; 1335 umass_clear_endpoint_stall(sc, UMASS_BULKOUT, 1336 sc->transfer_xfer[XFER_BBB_RESET3]); 1337 1338 return; 1339 case TSTATE_BBB_RESET3: 1340 if (err) /* should not occur */ 1341 DPRINTF(UDMASS_BBB,("%s: BBB bulk-out clear stall" 1342 " failed, %s\n", sc->sc_dev.dv_xname, 1343 usbd_errstr(err))); 1344 /* no error recovery, otherwise we end up in a loop */ 1345 1346 sc->transfer_state = TSTATE_IDLE; 1347 if (sc->transfer_priv) { 1348 sc->transfer_cb(sc, sc->transfer_priv, 1349 sc->transfer_datalen, 1350 sc->transfer_status); 1351 } 1352 1353 return; 1354 1355 /***** Default *****/ 1356 default: 1357 panic("%s: Unknown state %d", 1358 sc->sc_dev.dv_xname, sc->transfer_state); 1359 } 1360 } 1361 1362 /* 1363 * Command/Bulk/Interrupt (CBI) specific functions 1364 */ 1365 1366 int 1367 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, 1368 struct usbd_xfer *xfer) 1369 { 1370 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1371 ("sc->sc_wire == 0x%02x wrong for umass_cbi_adsc\n", 1372 sc->sc_wire)); 1373 1374 sc->sc_req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1375 sc->sc_req.bRequest = UR_CBI_ADSC; 1376 USETW(sc->sc_req.wValue, 0); 1377 USETW(sc->sc_req.wIndex, sc->sc_ifaceno); 1378 USETW(sc->sc_req.wLength, buflen); 1379 return umass_setup_ctrl_transfer(sc, &sc->sc_req, buffer, 1380 buflen, 0, xfer); 1381 } 1382 1383 1384 void 1385 umass_cbi_reset(struct umass_softc *sc, int status) 1386 { 1387 int i; 1388 # define SEND_DIAGNOSTIC_CMDLEN 12 1389 1390 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1391 ("sc->sc_wire == 0x%02x wrong for umass_cbi_reset\n", 1392 sc->sc_wire)); 1393 1394 if (usbd_is_dying(sc->sc_udev)) 1395 return; 1396 1397 /* 1398 * Command Block Reset Protocol 1399 * 1400 * First send a reset request to the device. Then clear 1401 * any possibly stalled bulk endpoints. 1402 1403 * This is done in 3 steps, states: 1404 * TSTATE_CBI_RESET1 1405 * TSTATE_CBI_RESET2 1406 * TSTATE_CBI_RESET3 1407 * 1408 * If the reset doesn't succeed, the device should be port reset. 1409 */ 1410 1411 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n", 1412 sc->sc_dev.dv_xname)); 1413 1414 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN, 1415 ("%s: CBL struct is too small (%d < %d)\n", 1416 sc->sc_dev.dv_xname, 1417 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN)); 1418 1419 sc->transfer_state = TSTATE_CBI_RESET1; 1420 sc->transfer_status = status; 1421 1422 /* The 0x1d code is the SEND DIAGNOSTIC command. To distinguish between 1423 * the two the last 10 bytes of the cbl is filled with 0xff (section 1424 * 2.2 of the CBI spec). 1425 */ 1426 sc->cbl[0] = 0x1d; /* Command Block Reset */ 1427 sc->cbl[1] = 0x04; 1428 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++) 1429 sc->cbl[i] = 0xff; 1430 1431 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 1432 sc->transfer_xfer[XFER_CBI_RESET1]); 1433 /* XXX if the command fails we should reset the port on the bub */ 1434 } 1435 1436 void 1437 umass_cbi_transfer(struct umass_softc *sc, int lun, 1438 void *cmd, int cmdlen, void *data, int datalen, int dir, 1439 u_int timeout, umass_callback cb, void *priv) 1440 { 1441 usbd_status err; 1442 1443 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n", 1444 sc->sc_dev.dv_xname, *(u_char *)cmd, datalen)); 1445 1446 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1447 ("sc->sc_wire == 0x%02x wrong for umass_cbi_transfer\n", 1448 sc->sc_wire)); 1449 1450 if (usbd_is_dying(sc->sc_udev)) { 1451 sc->polled_xfer_status = USBD_IOERROR; 1452 return; 1453 } 1454 1455 /* Be a little generous. */ 1456 sc->timeout = timeout + USBD_DEFAULT_TIMEOUT; 1457 1458 /* 1459 * Do a CBI transfer with cmdlen bytes from cmd, possibly 1460 * a data phase of datalen bytes from/to the device and finally a 1461 * csw read phase. 1462 * If the data direction was inbound a maximum of datalen bytes 1463 * is stored in the buffer pointed to by data. 1464 * 1465 * umass_cbi_transfer initialises the transfer and lets the state 1466 * machine in umass_cbi_state handle the completion. It uses the 1467 * following states: 1468 * TSTATE_CBI_COMMAND 1469 * -> XXX fill in 1470 * 1471 * An error in any of those states will invoke 1472 * umass_cbi_reset. 1473 */ 1474 1475 /* check the given arguments */ 1476 KASSERT(datalen == 0 || data != NULL, 1477 ("%s: datalen > 0, but no buffer",sc->sc_dev.dv_xname)); 1478 KASSERT(datalen == 0 || dir != DIR_NONE, 1479 ("%s: direction is NONE while datalen is not zero\n", 1480 sc->sc_dev.dv_xname)); 1481 1482 /* store the details for the data transfer phase */ 1483 sc->transfer_dir = dir; 1484 sc->transfer_data = data; 1485 sc->transfer_datalen = datalen; 1486 sc->transfer_actlen = 0; 1487 sc->transfer_cb = cb; 1488 sc->transfer_priv = priv; 1489 sc->transfer_status = STATUS_CMD_OK; 1490 1491 /* move from idle to the command state */ 1492 sc->transfer_state = TSTATE_CBI_COMMAND; 1493 1494 /* Send the Command Block from host to device via control endpoint. */ 1495 sc->cbw.bCDBLength = cmdlen; 1496 bzero(sc->cbw.CBWCDB, sizeof(sc->cbw.CBWCDB)); 1497 memcpy(sc->cbw.CBWCDB, cmd, cmdlen); 1498 umass_adjust_transfer(sc); 1499 if ((err = umass_cbi_adsc(sc, (void *)sc->cbw.CBWCDB, sc->cbw.bCDBLength, 1500 sc->transfer_xfer[XFER_CBI_CB]))) 1501 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1502 1503 if (sc->sc_udev->bus->use_polling) 1504 sc->polled_xfer_status = err; 1505 } 1506 1507 void 1508 umass_cbi_state(struct usbd_xfer *xfer, void *priv, usbd_status err) 1509 { 1510 struct umass_softc *sc = (struct umass_softc *) priv; 1511 1512 KASSERT(sc->sc_wire & (UMASS_WPROTO_CBI|UMASS_WPROTO_CBI_I), 1513 ("sc->sc_wire == 0x%02x wrong for umass_cbi_state\n", 1514 sc->sc_wire)); 1515 1516 if (usbd_is_dying(sc->sc_udev)) 1517 return; 1518 1519 /* 1520 * State handling for CBI transfers. 1521 */ 1522 1523 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n", 1524 sc->sc_dev.dv_xname, sc->transfer_state, 1525 states[sc->transfer_state], xfer, usbd_errstr(err))); 1526 1527 switch (sc->transfer_state) { 1528 1529 /***** CBI Transfer *****/ 1530 case TSTATE_CBI_COMMAND: 1531 if (err == USBD_STALLED) { 1532 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n", 1533 sc->sc_dev.dv_xname)); 1534 /* Status transport by control pipe (section 2.3.2.1). 1535 * The command contained in the command block failed. 1536 * 1537 * The control pipe has already been unstalled by the 1538 * USB stack. 1539 * Section 2.4.3.1.1 states that the bulk in endpoints 1540 * should not stalled at this point. 1541 */ 1542 1543 sc->transfer_state = TSTATE_IDLE; 1544 sc->transfer_cb(sc, sc->transfer_priv, 1545 sc->transfer_datalen, 1546 STATUS_CMD_FAILED); 1547 1548 return; 1549 } else if (err) { 1550 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n", 1551 sc->sc_dev.dv_xname)); 1552 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1553 return; 1554 } 1555 1556 /* Data transport phase, setup transfer */ 1557 sc->transfer_state = TSTATE_CBI_DATA; 1558 if (sc->transfer_dir == DIR_IN) { 1559 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKIN], 1560 sc->data_buffer, sc->transfer_datalen, 1561 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1562 sc->transfer_xfer[XFER_CBI_DATA])) 1563 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1564 1565 return; 1566 } else if (sc->transfer_dir == DIR_OUT) { 1567 memcpy(sc->data_buffer, sc->transfer_data, 1568 sc->transfer_datalen); 1569 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_BULKOUT], 1570 sc->data_buffer, sc->transfer_datalen, 1571 USBD_NO_COPY,/* fixed length transfer */ 1572 sc->transfer_xfer[XFER_CBI_DATA])) 1573 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1574 1575 return; 1576 } else { 1577 DPRINTF(UDMASS_CBI, ("%s: no data phase\n", 1578 sc->sc_dev.dv_xname)); 1579 } 1580 1581 /* FALLTHROUGH if no data phase, err == 0 */ 1582 case TSTATE_CBI_DATA: 1583 /* Command transport phase error handling (ignored if no data 1584 * phase (fallthrough from previous state)) */ 1585 if (sc->transfer_dir != DIR_NONE) { 1586 /* retrieve the length of the transfer that was done */ 1587 usbd_get_xfer_status(xfer, NULL, NULL, 1588 &sc->transfer_actlen, NULL); 1589 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n", 1590 sc->sc_dev.dv_xname, sc->transfer_actlen)); 1591 1592 if (err) { 1593 DPRINTF(UDMASS_CBI, ("%s: Data-%s %d failed, " 1594 "%s\n", sc->sc_dev.dv_xname, 1595 (sc->transfer_dir == DIR_IN?"in":"out"), 1596 sc->transfer_datalen,usbd_errstr(err))); 1597 1598 if (err == USBD_STALLED) { 1599 sc->transfer_state = TSTATE_CBI_DCLEAR; 1600 umass_clear_endpoint_stall(sc, 1601 (sc->transfer_dir == DIR_IN? 1602 UMASS_BULKIN:UMASS_BULKOUT), 1603 sc->transfer_xfer[XFER_CBI_DCLEAR]); 1604 } else { 1605 /* Unless the error is a pipe stall the 1606 * error is fatal. 1607 */ 1608 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1609 } 1610 return; 1611 } 1612 } 1613 1614 if (sc->transfer_dir == DIR_IN) 1615 memcpy(sc->transfer_data, sc->data_buffer, 1616 sc->transfer_actlen); 1617 1618 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN) 1619 umass_dump_buffer(sc, sc->transfer_data, 1620 sc->transfer_actlen, 48)); 1621 1622 /* Status phase */ 1623 if (sc->sc_wire == UMASS_WPROTO_CBI_I) { 1624 sc->transfer_state = TSTATE_CBI_STATUS; 1625 memset(&sc->sbl, 0, sizeof(sc->sbl)); 1626 if (umass_setup_transfer(sc, sc->sc_pipe[UMASS_INTRIN], 1627 &sc->sbl, sizeof(sc->sbl), 1628 0, /* fixed length transfer */ 1629 sc->transfer_xfer[XFER_CBI_STATUS])) 1630 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1631 } else { 1632 /* No command completion interrupt. Request 1633 * sense to get status of command. 1634 */ 1635 sc->transfer_state = TSTATE_IDLE; 1636 sc->transfer_cb(sc, sc->transfer_priv, 1637 sc->transfer_datalen - sc->transfer_actlen, 1638 STATUS_CMD_UNKNOWN); 1639 } 1640 return; 1641 1642 case TSTATE_CBI_STATUS: 1643 if (err) { 1644 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n", 1645 sc->sc_dev.dv_xname)); 1646 /* Status transport by interrupt pipe (section 2.3.2.2). 1647 */ 1648 1649 if (err == USBD_STALLED) { 1650 sc->transfer_state = TSTATE_CBI_SCLEAR; 1651 umass_clear_endpoint_stall(sc, UMASS_INTRIN, 1652 sc->transfer_xfer[XFER_CBI_SCLEAR]); 1653 } else { 1654 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1655 } 1656 return; 1657 } 1658 1659 /* Dissect the information in the buffer */ 1660 1661 { 1662 u_int32_t actlen; 1663 usbd_get_xfer_status(xfer, NULL, NULL, &actlen, NULL); 1664 DPRINTF(UDMASS_CBI, ("%s: CBI_STATUS actlen=%d\n", 1665 sc->sc_dev.dv_xname, actlen)); 1666 if (actlen != 2) 1667 break; 1668 } 1669 1670 if (sc->sc_cmd == UMASS_CPROTO_UFI) { 1671 int status; 1672 1673 /* Section 3.4.3.1.3 specifies that the UFI command 1674 * protocol returns an ASC and ASCQ in the interrupt 1675 * data block. 1676 */ 1677 1678 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, " 1679 "ASCQ = 0x%02x\n", 1680 sc->sc_dev.dv_xname, 1681 sc->sbl.ufi.asc, sc->sbl.ufi.ascq)); 1682 1683 if ((sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) || 1684 sc->sc_sense) 1685 status = STATUS_CMD_OK; 1686 else 1687 status = STATUS_CMD_FAILED; 1688 1689 /* No autosense, command successful */ 1690 sc->transfer_state = TSTATE_IDLE; 1691 sc->transfer_cb(sc, sc->transfer_priv, 1692 sc->transfer_datalen - sc->transfer_actlen, status); 1693 } else { 1694 /* Command Interrupt Data Block */ 1695 1696 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n", 1697 sc->sc_dev.dv_xname, 1698 sc->sbl.common.type, sc->sbl.common.value)); 1699 1700 if (sc->sbl.common.type == IDB_TYPE_CCI) { 1701 int status; 1702 switch (sc->sbl.common.value & 1703 IDB_VALUE_STATUS_MASK) { 1704 case IDB_VALUE_PASS: 1705 status = STATUS_CMD_OK; 1706 break; 1707 case IDB_VALUE_FAIL: 1708 case IDB_VALUE_PERSISTENT: 1709 status = STATUS_CMD_FAILED; 1710 break; 1711 case IDB_VALUE_PHASE: 1712 default: 1713 status = STATUS_WIRE_FAILED; 1714 break; 1715 } 1716 1717 sc->transfer_state = TSTATE_IDLE; 1718 sc->transfer_cb(sc, sc->transfer_priv, 1719 sc->transfer_datalen - sc->transfer_actlen, 1720 status); 1721 } 1722 } 1723 return; 1724 1725 case TSTATE_CBI_DCLEAR: 1726 if (err) { /* should not occur */ 1727 printf("%s: CBI bulk-in/out stall clear failed, %s\n", 1728 sc->sc_dev.dv_xname, usbd_errstr(err)); 1729 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1730 } else { 1731 sc->transfer_state = TSTATE_IDLE; 1732 sc->transfer_cb(sc, sc->transfer_priv, 1733 sc->transfer_datalen, STATUS_CMD_FAILED); 1734 } 1735 return; 1736 1737 case TSTATE_CBI_SCLEAR: 1738 if (err) { /* should not occur */ 1739 printf("%s: CBI intr-in stall clear failed, %s\n", 1740 sc->sc_dev.dv_xname, usbd_errstr(err)); 1741 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1742 } else { 1743 sc->transfer_state = TSTATE_IDLE; 1744 sc->transfer_cb(sc, sc->transfer_priv, 1745 sc->transfer_datalen, STATUS_CMD_FAILED); 1746 } 1747 return; 1748 1749 /***** CBI Reset *****/ 1750 case TSTATE_CBI_RESET1: 1751 if (err) 1752 printf("%s: CBI reset failed, %s\n", 1753 sc->sc_dev.dv_xname, usbd_errstr(err)); 1754 1755 sc->transfer_state = TSTATE_CBI_RESET2; 1756 umass_clear_endpoint_stall(sc, UMASS_BULKIN, 1757 sc->transfer_xfer[XFER_CBI_RESET2]); 1758 1759 return; 1760 case TSTATE_CBI_RESET2: 1761 if (err) /* should not occur */ 1762 printf("%s: CBI bulk-in stall clear failed, %s\n", 1763 sc->sc_dev.dv_xname, usbd_errstr(err)); 1764 /* no error recovery, otherwise we end up in a loop */ 1765 1766 sc->transfer_state = TSTATE_CBI_RESET3; 1767 umass_clear_endpoint_stall(sc, UMASS_BULKOUT, 1768 sc->transfer_xfer[XFER_CBI_RESET3]); 1769 1770 return; 1771 case TSTATE_CBI_RESET3: 1772 if (err) /* should not occur */ 1773 printf("%s: CBI bulk-out stall clear failed, %s\n", 1774 sc->sc_dev.dv_xname, usbd_errstr(err)); 1775 /* no error recovery, otherwise we end up in a loop */ 1776 1777 sc->transfer_state = TSTATE_IDLE; 1778 if (sc->transfer_priv) { 1779 sc->transfer_cb(sc, sc->transfer_priv, 1780 sc->transfer_datalen, 1781 sc->transfer_status); 1782 } 1783 1784 return; 1785 1786 1787 /***** Default *****/ 1788 default: 1789 panic("%s: Unknown state %d", 1790 sc->sc_dev.dv_xname, sc->transfer_state); 1791 } 1792 } 1793 1794 u_int8_t 1795 umass_bbb_get_max_lun(struct umass_softc *sc) 1796 { 1797 usb_device_request_t req; 1798 usbd_status err; 1799 u_int8_t maxlun = 0; 1800 u_int8_t buf = 0; 1801 1802 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", sc->sc_dev.dv_xname)); 1803 1804 /* The Get Max Lun command is a class-specific request. */ 1805 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1806 req.bRequest = UR_BBB_GET_MAX_LUN; 1807 USETW(req.wValue, 0); 1808 USETW(req.wIndex, sc->sc_ifaceno); 1809 USETW(req.wLength, 1); 1810 1811 err = usbd_do_request_flags(sc->sc_udev, &req, &buf, 1812 USBD_SHORT_XFER_OK, 0, USBD_DEFAULT_TIMEOUT); 1813 1814 switch (err) { 1815 case USBD_NORMAL_COMPLETION: 1816 maxlun = buf; 1817 break; 1818 1819 default: 1820 /* XXX Should we port_reset the device? */ 1821 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported (%s)\n", 1822 sc->sc_dev.dv_xname, usbd_errstr(err))); 1823 break; 1824 } 1825 1826 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n", sc->sc_dev.dv_xname, maxlun)); 1827 return (maxlun); 1828 } 1829 1830 #ifdef UMASS_DEBUG 1831 void 1832 umass_bbb_dump_cbw(struct umass_softc *sc, struct umass_bbb_cbw *cbw) 1833 { 1834 int clen = cbw->bCDBLength; 1835 int dlen = UGETDW(cbw->dCBWDataTransferLength); 1836 u_int8_t *c = cbw->CBWCDB; 1837 int tag = UGETDW(cbw->dCBWTag); 1838 int flags = cbw->bCBWFlags; 1839 1840 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmdlen=%d " 1841 "(0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%s), " 1842 "data = %d bytes, dir = %s\n", 1843 sc->sc_dev.dv_xname, tag, clen, 1844 c[0], c[1], c[2], c[3], c[4], c[5], 1845 c[6], c[7], c[8], c[9], 1846 (clen > 10? "...":""), 1847 dlen, (flags == CBWFLAGS_IN? "in": 1848 (flags == CBWFLAGS_OUT? "out":"<invalid>")))); 1849 } 1850 1851 void 1852 umass_bbb_dump_csw(struct umass_softc *sc, struct umass_bbb_csw *csw) 1853 { 1854 int sig = UGETDW(csw->dCSWSignature); 1855 int tag = UGETDW(csw->dCSWTag); 1856 int res = UGETDW(csw->dCSWDataResidue); 1857 int status = csw->bCSWStatus; 1858 1859 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, " 1860 "res = %d, status = 0x%02x (%s)\n", sc->sc_dev.dv_xname, 1861 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"), 1862 tag, res, 1863 status, (status == CSWSTATUS_GOOD? "good": 1864 (status == CSWSTATUS_FAILED? "failed": 1865 (status == CSWSTATUS_PHASE? "phase":"<invalid>"))))); 1866 } 1867 1868 void 1869 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen, 1870 int printlen) 1871 { 1872 int i, j; 1873 char s1[40]; 1874 char s2[40]; 1875 char s3[5]; 1876 1877 s1[0] = '\0'; 1878 s3[0] = '\0'; 1879 1880 snprintf(s2, sizeof s2, " buffer=%p, buflen=%d", buffer, buflen); 1881 for (i = 0; i < buflen && i < printlen; i++) { 1882 j = i % 16; 1883 if (j == 0 && i != 0) { 1884 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n", 1885 sc->sc_dev.dv_xname, s1, s2)); 1886 s2[0] = '\0'; 1887 } 1888 snprintf(&s1[j*2], sizeof s1 - j*2, "%02x", buffer[i] & 0xff); 1889 } 1890 if (buflen > printlen) 1891 snprintf(s3, sizeof s3, " ..."); 1892 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n", 1893 sc->sc_dev.dv_xname, s1, s2, s3)); 1894 } 1895 #endif 1896