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