1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008,2011 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * The following file contains code that will detect USB autoinstall 29 * disks. 30 * 31 * TODO: Potentially we could add code to automatically detect USB 32 * mass storage quirks for not supported SCSI commands! 33 */ 34 35 #ifdef USB_GLOBAL_INCLUDE_FILE 36 #include USB_GLOBAL_INCLUDE_FILE 37 #else 38 #include <sys/stdint.h> 39 #include <sys/stddef.h> 40 #include <sys/param.h> 41 #include <sys/queue.h> 42 #include <sys/types.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/bus.h> 46 #include <sys/module.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/condvar.h> 50 #include <sys/sysctl.h> 51 #include <sys/sx.h> 52 #include <sys/unistd.h> 53 #include <sys/callout.h> 54 #include <sys/malloc.h> 55 #include <sys/priv.h> 56 57 #include <dev/usb/usb.h> 58 #include <dev/usb/usbdi.h> 59 #include <dev/usb/usbdi_util.h> 60 61 #define USB_DEBUG_VAR usb_debug 62 63 #include <dev/usb/usb_busdma.h> 64 #include <dev/usb/usb_process.h> 65 #include <dev/usb/usb_transfer.h> 66 #include <dev/usb/usb_msctest.h> 67 #include <dev/usb/usb_debug.h> 68 #include <dev/usb/usb_device.h> 69 #include <dev/usb/usb_request.h> 70 #include <dev/usb/usb_util.h> 71 #include <dev/usb/quirk/usb_quirk.h> 72 #endif /* USB_GLOBAL_INCLUDE_FILE */ 73 74 enum { 75 ST_COMMAND, 76 ST_DATA_RD, 77 ST_DATA_RD_CS, 78 ST_DATA_WR, 79 ST_DATA_WR_CS, 80 ST_STATUS, 81 ST_MAX, 82 }; 83 84 enum { 85 DIR_IN, 86 DIR_OUT, 87 DIR_NONE, 88 }; 89 90 #define SCSI_MAX_LEN MAX(0x100, BULK_SIZE) 91 #define SCSI_INQ_LEN 0x24 92 #define SCSI_SENSE_LEN 0xFF 93 94 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 95 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 }; 96 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }; 97 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 }; 98 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01, 99 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 }; 100 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43, 101 0x48, 0x47 }; 102 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00, 103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 104 0x00, 0x00, 0x00, 0x00 }; 105 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 }; 106 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 107 0x00, 0x00, 0x00, 0x00 }; 108 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00, 109 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 110 static uint8_t scsi_read_capacity[] = { 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 111 0x00, 0x00, 0x00, 0x00 }; 112 113 #define BULK_SIZE 64 /* dummy */ 114 #define ERR_CSW_FAILED -1 115 116 /* Command Block Wrapper */ 117 struct bbb_cbw { 118 uDWord dCBWSignature; 119 #define CBWSIGNATURE 0x43425355 120 uDWord dCBWTag; 121 uDWord dCBWDataTransferLength; 122 uByte bCBWFlags; 123 #define CBWFLAGS_OUT 0x00 124 #define CBWFLAGS_IN 0x80 125 uByte bCBWLUN; 126 uByte bCDBLength; 127 #define CBWCDBLENGTH 16 128 uByte CBWCDB[CBWCDBLENGTH]; 129 } __packed; 130 131 /* Command Status Wrapper */ 132 struct bbb_csw { 133 uDWord dCSWSignature; 134 #define CSWSIGNATURE 0x53425355 135 uDWord dCSWTag; 136 uDWord dCSWDataResidue; 137 uByte bCSWStatus; 138 #define CSWSTATUS_GOOD 0x0 139 #define CSWSTATUS_FAILED 0x1 140 #define CSWSTATUS_PHASE 0x2 141 } __packed; 142 143 struct bbb_transfer { 144 struct mtx mtx; 145 struct cv cv; 146 struct bbb_cbw *cbw; 147 struct bbb_csw *csw; 148 149 struct usb_xfer *xfer[ST_MAX]; 150 151 uint8_t *data_ptr; 152 153 usb_size_t data_len; /* bytes */ 154 usb_size_t data_rem; /* bytes */ 155 usb_timeout_t data_timeout; /* ms */ 156 usb_frlength_t actlen; /* bytes */ 157 usb_frlength_t buffer_size; /* bytes */ 158 159 uint8_t cmd_len; /* bytes */ 160 uint8_t dir; 161 uint8_t lun; 162 uint8_t state; 163 uint8_t status_try; 164 int error; 165 166 uint8_t *buffer; 167 }; 168 169 static usb_callback_t bbb_command_callback; 170 static usb_callback_t bbb_data_read_callback; 171 static usb_callback_t bbb_data_rd_cs_callback; 172 static usb_callback_t bbb_data_write_callback; 173 static usb_callback_t bbb_data_wr_cs_callback; 174 static usb_callback_t bbb_status_callback; 175 176 static void bbb_done(struct bbb_transfer *, int); 177 static void bbb_transfer_start(struct bbb_transfer *, uint8_t); 178 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t, 179 uint8_t); 180 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t, 181 void *, size_t, void *, size_t, usb_timeout_t); 182 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t); 183 static void bbb_detach(struct bbb_transfer *); 184 185 static const struct usb_config bbb_config[ST_MAX] = { 186 187 [ST_COMMAND] = { 188 .type = UE_BULK, 189 .endpoint = UE_ADDR_ANY, 190 .direction = UE_DIR_OUT, 191 .bufsize = sizeof(struct bbb_cbw), 192 .callback = &bbb_command_callback, 193 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 194 }, 195 196 [ST_DATA_RD] = { 197 .type = UE_BULK, 198 .endpoint = UE_ADDR_ANY, 199 .direction = UE_DIR_IN, 200 .bufsize = SCSI_MAX_LEN, 201 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,}, 202 .callback = &bbb_data_read_callback, 203 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 204 }, 205 206 [ST_DATA_RD_CS] = { 207 .type = UE_CONTROL, 208 .endpoint = 0x00, /* Control pipe */ 209 .direction = UE_DIR_ANY, 210 .bufsize = sizeof(struct usb_device_request), 211 .callback = &bbb_data_rd_cs_callback, 212 .timeout = 1 * USB_MS_HZ, /* 1 second */ 213 }, 214 215 [ST_DATA_WR] = { 216 .type = UE_BULK, 217 .endpoint = UE_ADDR_ANY, 218 .direction = UE_DIR_OUT, 219 .bufsize = SCSI_MAX_LEN, 220 .flags = {.ext_buffer = 1,.proxy_buffer = 1,}, 221 .callback = &bbb_data_write_callback, 222 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 223 }, 224 225 [ST_DATA_WR_CS] = { 226 .type = UE_CONTROL, 227 .endpoint = 0x00, /* Control pipe */ 228 .direction = UE_DIR_ANY, 229 .bufsize = sizeof(struct usb_device_request), 230 .callback = &bbb_data_wr_cs_callback, 231 .timeout = 1 * USB_MS_HZ, /* 1 second */ 232 }, 233 234 [ST_STATUS] = { 235 .type = UE_BULK, 236 .endpoint = UE_ADDR_ANY, 237 .direction = UE_DIR_IN, 238 .bufsize = sizeof(struct bbb_csw), 239 .flags = {.short_xfer_ok = 1,}, 240 .callback = &bbb_status_callback, 241 .timeout = 1 * USB_MS_HZ, /* 1 second */ 242 }, 243 }; 244 245 static void 246 bbb_done(struct bbb_transfer *sc, int error) 247 { 248 sc->error = error; 249 sc->state = ST_COMMAND; 250 sc->status_try = 1; 251 cv_signal(&sc->cv); 252 } 253 254 static void 255 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index) 256 { 257 sc->state = xfer_index; 258 usbd_transfer_start(sc->xfer[xfer_index]); 259 } 260 261 static void 262 bbb_data_clear_stall_callback(struct usb_xfer *xfer, 263 uint8_t next_xfer, uint8_t stall_xfer) 264 { 265 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 266 267 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) { 268 switch (USB_GET_STATE(xfer)) { 269 case USB_ST_SETUP: 270 case USB_ST_TRANSFERRED: 271 bbb_transfer_start(sc, next_xfer); 272 break; 273 default: 274 bbb_done(sc, USB_ERR_STALLED); 275 break; 276 } 277 } 278 } 279 280 static void 281 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error) 282 { 283 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 284 uint32_t tag; 285 286 switch (USB_GET_STATE(xfer)) { 287 case USB_ST_TRANSFERRED: 288 bbb_transfer_start 289 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD : 290 (sc->dir == DIR_OUT) ? ST_DATA_WR : 291 ST_STATUS)); 292 break; 293 294 case USB_ST_SETUP: 295 sc->status_try = 0; 296 tag = UGETDW(sc->cbw->dCBWTag) + 1; 297 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE); 298 USETDW(sc->cbw->dCBWTag, tag); 299 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len); 300 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT); 301 sc->cbw->bCBWLUN = sc->lun; 302 sc->cbw->bCDBLength = sc->cmd_len; 303 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) { 304 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB); 305 DPRINTFN(0, "Truncating long command\n"); 306 } 307 usbd_xfer_set_frame_len(xfer, 0, 308 sizeof(struct bbb_cbw)); 309 usbd_transfer_submit(xfer); 310 break; 311 312 default: /* Error */ 313 bbb_done(sc, error); 314 break; 315 } 316 } 317 318 static void 319 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error) 320 { 321 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 322 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 323 int actlen, sumlen; 324 325 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 326 327 switch (USB_GET_STATE(xfer)) { 328 case USB_ST_TRANSFERRED: 329 sc->data_rem -= actlen; 330 sc->data_ptr += actlen; 331 sc->actlen += actlen; 332 333 if (actlen < sumlen) { 334 /* short transfer */ 335 sc->data_rem = 0; 336 } 337 case USB_ST_SETUP: 338 DPRINTF("max_bulk=%d, data_rem=%d\n", 339 max_bulk, sc->data_rem); 340 341 if (sc->data_rem == 0) { 342 bbb_transfer_start(sc, ST_STATUS); 343 break; 344 } 345 if (max_bulk > sc->data_rem) { 346 max_bulk = sc->data_rem; 347 } 348 usbd_xfer_set_timeout(xfer, sc->data_timeout); 349 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 350 usbd_transfer_submit(xfer); 351 break; 352 353 default: /* Error */ 354 if (error == USB_ERR_CANCELLED) { 355 bbb_done(sc, error); 356 } else { 357 bbb_transfer_start(sc, ST_DATA_RD_CS); 358 } 359 break; 360 } 361 } 362 363 static void 364 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error) 365 { 366 bbb_data_clear_stall_callback(xfer, ST_STATUS, 367 ST_DATA_RD); 368 } 369 370 static void 371 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error) 372 { 373 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 374 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 375 int actlen, sumlen; 376 377 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 378 379 switch (USB_GET_STATE(xfer)) { 380 case USB_ST_TRANSFERRED: 381 sc->data_rem -= actlen; 382 sc->data_ptr += actlen; 383 sc->actlen += actlen; 384 385 if (actlen < sumlen) { 386 /* short transfer */ 387 sc->data_rem = 0; 388 } 389 case USB_ST_SETUP: 390 DPRINTF("max_bulk=%d, data_rem=%d\n", 391 max_bulk, sc->data_rem); 392 393 if (sc->data_rem == 0) { 394 bbb_transfer_start(sc, ST_STATUS); 395 break; 396 } 397 if (max_bulk > sc->data_rem) { 398 max_bulk = sc->data_rem; 399 } 400 usbd_xfer_set_timeout(xfer, sc->data_timeout); 401 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 402 usbd_transfer_submit(xfer); 403 break; 404 405 default: /* Error */ 406 if (error == USB_ERR_CANCELLED) { 407 bbb_done(sc, error); 408 } else { 409 bbb_transfer_start(sc, ST_DATA_WR_CS); 410 } 411 break; 412 } 413 } 414 415 static void 416 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error) 417 { 418 bbb_data_clear_stall_callback(xfer, ST_STATUS, 419 ST_DATA_WR); 420 } 421 422 static void 423 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error) 424 { 425 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 426 int actlen; 427 int sumlen; 428 429 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 430 431 switch (USB_GET_STATE(xfer)) { 432 case USB_ST_TRANSFERRED: 433 434 /* very simple status check */ 435 436 if (actlen < (int)sizeof(struct bbb_csw)) { 437 bbb_done(sc, USB_ERR_SHORT_XFER); 438 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) { 439 bbb_done(sc, 0); /* success */ 440 } else { 441 bbb_done(sc, ERR_CSW_FAILED); /* error */ 442 } 443 break; 444 445 case USB_ST_SETUP: 446 usbd_xfer_set_frame_len(xfer, 0, 447 sizeof(struct bbb_csw)); 448 usbd_transfer_submit(xfer); 449 break; 450 451 default: 452 DPRINTF("Failed to read CSW: %s, try %d\n", 453 usbd_errstr(error), sc->status_try); 454 455 if (error == USB_ERR_CANCELLED || sc->status_try) { 456 bbb_done(sc, error); 457 } else { 458 sc->status_try = 1; 459 bbb_transfer_start(sc, ST_DATA_RD_CS); 460 } 461 break; 462 } 463 } 464 465 /*------------------------------------------------------------------------* 466 * bbb_command_start - execute a SCSI command synchronously 467 * 468 * Return values 469 * 0: Success 470 * Else: Failure 471 *------------------------------------------------------------------------*/ 472 static int 473 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun, 474 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len, 475 usb_timeout_t data_timeout) 476 { 477 sc->lun = lun; 478 sc->dir = data_len ? dir : DIR_NONE; 479 sc->data_ptr = data_ptr; 480 sc->data_len = data_len; 481 sc->data_rem = data_len; 482 sc->data_timeout = (data_timeout + USB_MS_HZ); 483 sc->actlen = 0; 484 sc->cmd_len = cmd_len; 485 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB)); 486 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len); 487 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":"); 488 489 mtx_lock(&sc->mtx); 490 usbd_transfer_start(sc->xfer[sc->state]); 491 492 while (usbd_transfer_pending(sc->xfer[sc->state])) { 493 cv_wait(&sc->cv, &sc->mtx); 494 } 495 mtx_unlock(&sc->mtx); 496 return (sc->error); 497 } 498 499 static struct bbb_transfer * 500 bbb_attach(struct usb_device *udev, uint8_t iface_index) 501 { 502 struct usb_interface *iface; 503 struct usb_interface_descriptor *id; 504 struct bbb_transfer *sc; 505 usb_error_t err; 506 uint8_t do_unlock; 507 508 /* Prevent re-enumeration */ 509 do_unlock = usbd_enum_lock(udev); 510 511 /* 512 * Make sure any driver which is hooked up to this interface, 513 * like umass is gone: 514 */ 515 usb_detach_device(udev, iface_index, 0); 516 517 if (do_unlock) 518 usbd_enum_unlock(udev); 519 520 iface = usbd_get_iface(udev, iface_index); 521 if (iface == NULL) 522 return (NULL); 523 524 id = iface->idesc; 525 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 526 return (NULL); 527 528 switch (id->bInterfaceSubClass) { 529 case UISUBCLASS_SCSI: 530 case UISUBCLASS_UFI: 531 case UISUBCLASS_SFF8020I: 532 case UISUBCLASS_SFF8070I: 533 break; 534 default: 535 return (NULL); 536 } 537 538 switch (id->bInterfaceProtocol) { 539 case UIPROTO_MASS_BBB_OLD: 540 case UIPROTO_MASS_BBB: 541 break; 542 default: 543 return (NULL); 544 } 545 546 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO); 547 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF); 548 cv_init(&sc->cv, "WBBB"); 549 550 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config, 551 ST_MAX, sc, &sc->mtx); 552 if (err) { 553 bbb_detach(sc); 554 return (NULL); 555 } 556 /* store pointer to DMA buffers */ 557 sc->buffer = usbd_xfer_get_frame_buffer( 558 sc->xfer[ST_DATA_RD], 0); 559 sc->buffer_size = 560 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]); 561 sc->cbw = usbd_xfer_get_frame_buffer( 562 sc->xfer[ST_COMMAND], 0); 563 sc->csw = usbd_xfer_get_frame_buffer( 564 sc->xfer[ST_STATUS], 0); 565 566 return (sc); 567 } 568 569 static void 570 bbb_detach(struct bbb_transfer *sc) 571 { 572 usbd_transfer_unsetup(sc->xfer, ST_MAX); 573 mtx_destroy(&sc->mtx); 574 cv_destroy(&sc->cv); 575 free(sc, M_USB); 576 } 577 578 /*------------------------------------------------------------------------* 579 * usb_iface_is_cdrom 580 * 581 * Return values: 582 * 1: This interface is an auto install disk (CD-ROM) 583 * 0: Not an auto install disk. 584 *------------------------------------------------------------------------*/ 585 int 586 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index) 587 { 588 struct bbb_transfer *sc; 589 uint8_t timeout; 590 uint8_t is_cdrom; 591 uint8_t sid_type; 592 int err; 593 594 sc = bbb_attach(udev, iface_index); 595 if (sc == NULL) 596 return (0); 597 598 is_cdrom = 0; 599 timeout = 4; /* tries */ 600 while (--timeout) { 601 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 602 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 603 USB_MS_HZ); 604 605 if (err == 0 && sc->actlen > 0) { 606 sid_type = sc->buffer[0] & 0x1F; 607 if (sid_type == 0x05) 608 is_cdrom = 1; 609 break; 610 } else if (err != ERR_CSW_FAILED) 611 break; /* non retryable error */ 612 usb_pause_mtx(NULL, hz); 613 } 614 bbb_detach(sc); 615 return (is_cdrom); 616 } 617 618 static uint8_t 619 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index) 620 { 621 struct usb_device_request req; 622 usb_error_t err; 623 uint8_t buf = 0; 624 625 626 /* The Get Max Lun command is a class-specific request. */ 627 req.bmRequestType = UT_READ_CLASS_INTERFACE; 628 req.bRequest = 0xFE; /* GET_MAX_LUN */ 629 USETW(req.wValue, 0); 630 req.wIndex[0] = iface_index; 631 req.wIndex[1] = 0; 632 USETW(req.wLength, 1); 633 634 err = usbd_do_request(udev, NULL, &req, &buf); 635 if (err) 636 buf = 0; 637 638 return (buf); 639 } 640 641 usb_error_t 642 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index) 643 { 644 struct bbb_transfer *sc; 645 uint8_t timeout; 646 uint8_t is_no_direct; 647 uint8_t sid_type; 648 int err; 649 650 sc = bbb_attach(udev, iface_index); 651 if (sc == NULL) 652 return (0); 653 654 /* 655 * Some devices need a delay after that the configuration 656 * value is set to function properly: 657 */ 658 usb_pause_mtx(NULL, hz); 659 660 if (usb_msc_get_max_lun(udev, iface_index) == 0) { 661 DPRINTF("Device has only got one LUN.\n"); 662 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN); 663 } 664 665 is_no_direct = 1; 666 for (timeout = 4; timeout != 0; timeout--) { 667 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 668 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 669 USB_MS_HZ); 670 671 if (err == 0 && sc->actlen > 0) { 672 sid_type = sc->buffer[0] & 0x1F; 673 if (sid_type == 0x00) 674 is_no_direct = 0; 675 break; 676 } else if (err != ERR_CSW_FAILED) { 677 DPRINTF("Device is not responding " 678 "properly to SCSI INQUIRY command.\n"); 679 goto error; /* non retryable error */ 680 } 681 usb_pause_mtx(NULL, hz); 682 } 683 684 if (is_no_direct) { 685 DPRINTF("Device is not direct access.\n"); 686 goto done; 687 } 688 689 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 690 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 691 USB_MS_HZ); 692 693 if (err != 0) { 694 695 if (err != ERR_CSW_FAILED) 696 goto error; 697 } 698 timeout = 1; 699 700 retry_sync_cache: 701 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 702 &scsi_sync_cache, sizeof(scsi_sync_cache), 703 USB_MS_HZ); 704 705 if (err != 0) { 706 707 if (err != ERR_CSW_FAILED) 708 goto error; 709 710 DPRINTF("Device doesn't handle synchronize cache\n"); 711 712 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 713 714 } else { 715 716 /* 717 * Certain Kingston memory sticks fail the first 718 * read capacity after a synchronize cache command 719 * has been issued. Disable the synchronize cache 720 * command for such devices. 721 */ 722 723 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8, 724 &scsi_read_capacity, sizeof(scsi_read_capacity), 725 USB_MS_HZ); 726 727 if (err != 0) { 728 if (err != ERR_CSW_FAILED) 729 goto error; 730 731 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8, 732 &scsi_read_capacity, sizeof(scsi_read_capacity), 733 USB_MS_HZ); 734 735 if (err == 0) { 736 if (timeout--) 737 goto retry_sync_cache; 738 739 DPRINTF("Device most likely doesn't " 740 "handle synchronize cache\n"); 741 742 usbd_add_dynamic_quirk(udev, 743 UQ_MSC_NO_SYNC_CACHE); 744 } else { 745 if (err != ERR_CSW_FAILED) 746 goto error; 747 } 748 } 749 } 750 751 /* clear sense status of any failed commands on the device */ 752 753 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 754 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 755 USB_MS_HZ); 756 757 DPRINTF("Inquiry = %d\n", err); 758 759 if (err != 0) { 760 761 if (err != ERR_CSW_FAILED) 762 goto error; 763 } 764 765 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 766 SCSI_SENSE_LEN, &scsi_request_sense, 767 sizeof(scsi_request_sense), USB_MS_HZ); 768 769 DPRINTF("Request sense = %d\n", err); 770 771 if (err != 0) { 772 773 if (err != ERR_CSW_FAILED) 774 goto error; 775 } 776 777 done: 778 bbb_detach(sc); 779 return (0); 780 781 error: 782 bbb_detach(sc); 783 784 DPRINTF("Device did not respond, enabling all quirks\n"); 785 786 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 787 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY); 788 789 /* Need to re-enumerate the device */ 790 usbd_req_re_enumerate(udev, NULL); 791 792 return (USB_ERR_STALLED); 793 } 794 795 usb_error_t 796 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method) 797 { 798 struct bbb_transfer *sc; 799 usb_error_t err; 800 801 sc = bbb_attach(udev, iface_index); 802 if (sc == NULL) 803 return (USB_ERR_INVAL); 804 805 switch (method) { 806 case MSC_EJECT_STOPUNIT: 807 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 808 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 809 USB_MS_HZ); 810 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err)); 811 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 812 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit), 813 USB_MS_HZ); 814 break; 815 case MSC_EJECT_REZERO: 816 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 817 &scsi_rezero_init, sizeof(scsi_rezero_init), 818 USB_MS_HZ); 819 break; 820 case MSC_EJECT_ZTESTOR: 821 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 822 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject), 823 USB_MS_HZ); 824 break; 825 case MSC_EJECT_CMOTECH: 826 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 827 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject), 828 USB_MS_HZ); 829 break; 830 case MSC_EJECT_HUAWEI: 831 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 832 &scsi_huawei_eject, sizeof(scsi_huawei_eject), 833 USB_MS_HZ); 834 break; 835 case MSC_EJECT_TCT: 836 /* 837 * TCTMobile needs DIR_IN flag. To get it, we 838 * supply a dummy data with the command. 839 */ 840 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 841 sc->buffer_size, &scsi_tct_eject, 842 sizeof(scsi_tct_eject), USB_MS_HZ); 843 break; 844 default: 845 DPRINTF("Unknown eject method (%d)\n", method); 846 err = 0; 847 break; 848 } 849 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err)); 850 851 bbb_detach(sc); 852 return (0); 853 } 854