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