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