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