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