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