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(SCSI_FIXED_BLOCK_SIZE, USB_MSCTEST_BULK_SIZE) 91 #define SCSI_INQ_LEN 0x24 92 #define SCSI_SENSE_LEN 0xFF 93 #define SCSI_FIXED_BLOCK_SIZE 512 /* bytes */ 94 95 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 96 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 }; 97 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }; 98 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 }; 99 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01, 100 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 }; 101 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43, 102 0x48, 0x47 }; 103 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00, 104 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 105 0x00, 0x00, 0x00, 0x00 }; 106 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 }; 107 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 108 0x00, 0x00, 0x00, 0x00 }; 109 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00, 110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 111 static uint8_t scsi_read_capacity[] = { 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 112 0x00, 0x00, 0x00, 0x00 }; 113 114 #ifndef USB_MSCTEST_BULK_SIZE 115 #define USB_MSCTEST_BULK_SIZE 64 /* dummy */ 116 #endif 117 118 #define ERR_CSW_FAILED -1 119 120 /* Command Block Wrapper */ 121 struct bbb_cbw { 122 uDWord dCBWSignature; 123 #define CBWSIGNATURE 0x43425355 124 uDWord dCBWTag; 125 uDWord dCBWDataTransferLength; 126 uByte bCBWFlags; 127 #define CBWFLAGS_OUT 0x00 128 #define CBWFLAGS_IN 0x80 129 uByte bCBWLUN; 130 uByte bCDBLength; 131 #define CBWCDBLENGTH 16 132 uByte CBWCDB[CBWCDBLENGTH]; 133 } __packed; 134 135 /* Command Status Wrapper */ 136 struct bbb_csw { 137 uDWord dCSWSignature; 138 #define CSWSIGNATURE 0x53425355 139 uDWord dCSWTag; 140 uDWord dCSWDataResidue; 141 uByte bCSWStatus; 142 #define CSWSTATUS_GOOD 0x0 143 #define CSWSTATUS_FAILED 0x1 144 #define CSWSTATUS_PHASE 0x2 145 } __packed; 146 147 struct bbb_transfer { 148 struct mtx mtx; 149 struct cv cv; 150 struct bbb_cbw *cbw; 151 struct bbb_csw *csw; 152 153 struct usb_xfer *xfer[ST_MAX]; 154 155 uint8_t *data_ptr; 156 157 usb_size_t data_len; /* bytes */ 158 usb_size_t data_rem; /* bytes */ 159 usb_timeout_t data_timeout; /* ms */ 160 usb_frlength_t actlen; /* bytes */ 161 usb_frlength_t buffer_size; /* bytes */ 162 163 uint8_t cmd_len; /* bytes */ 164 uint8_t dir; 165 uint8_t lun; 166 uint8_t state; 167 uint8_t status_try; 168 int error; 169 170 uint8_t *buffer; 171 }; 172 173 static usb_callback_t bbb_command_callback; 174 static usb_callback_t bbb_data_read_callback; 175 static usb_callback_t bbb_data_rd_cs_callback; 176 static usb_callback_t bbb_data_write_callback; 177 static usb_callback_t bbb_data_wr_cs_callback; 178 static usb_callback_t bbb_status_callback; 179 180 static void bbb_done(struct bbb_transfer *, int); 181 static void bbb_transfer_start(struct bbb_transfer *, uint8_t); 182 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t, 183 uint8_t); 184 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t, 185 void *, size_t, void *, size_t, usb_timeout_t); 186 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t); 187 static void bbb_detach(struct bbb_transfer *); 188 189 static const struct usb_config bbb_config[ST_MAX] = { 190 191 [ST_COMMAND] = { 192 .type = UE_BULK, 193 .endpoint = UE_ADDR_ANY, 194 .direction = UE_DIR_OUT, 195 .bufsize = sizeof(struct bbb_cbw), 196 .callback = &bbb_command_callback, 197 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 198 }, 199 200 [ST_DATA_RD] = { 201 .type = UE_BULK, 202 .endpoint = UE_ADDR_ANY, 203 .direction = UE_DIR_IN, 204 .bufsize = SCSI_MAX_LEN, 205 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1,}, 206 .callback = &bbb_data_read_callback, 207 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 208 }, 209 210 [ST_DATA_RD_CS] = { 211 .type = UE_CONTROL, 212 .endpoint = 0x00, /* Control pipe */ 213 .direction = UE_DIR_ANY, 214 .bufsize = sizeof(struct usb_device_request), 215 .callback = &bbb_data_rd_cs_callback, 216 .timeout = 1 * USB_MS_HZ, /* 1 second */ 217 }, 218 219 [ST_DATA_WR] = { 220 .type = UE_BULK, 221 .endpoint = UE_ADDR_ANY, 222 .direction = UE_DIR_OUT, 223 .bufsize = SCSI_MAX_LEN, 224 .flags = {.ext_buffer = 1,.proxy_buffer = 1,}, 225 .callback = &bbb_data_write_callback, 226 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 227 }, 228 229 [ST_DATA_WR_CS] = { 230 .type = UE_CONTROL, 231 .endpoint = 0x00, /* Control pipe */ 232 .direction = UE_DIR_ANY, 233 .bufsize = sizeof(struct usb_device_request), 234 .callback = &bbb_data_wr_cs_callback, 235 .timeout = 1 * USB_MS_HZ, /* 1 second */ 236 }, 237 238 [ST_STATUS] = { 239 .type = UE_BULK, 240 .endpoint = UE_ADDR_ANY, 241 .direction = UE_DIR_IN, 242 .bufsize = sizeof(struct bbb_csw), 243 .flags = {.short_xfer_ok = 1,}, 244 .callback = &bbb_status_callback, 245 .timeout = 1 * USB_MS_HZ, /* 1 second */ 246 }, 247 }; 248 249 static void 250 bbb_done(struct bbb_transfer *sc, int error) 251 { 252 sc->error = error; 253 sc->state = ST_COMMAND; 254 sc->status_try = 1; 255 cv_signal(&sc->cv); 256 } 257 258 static void 259 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index) 260 { 261 sc->state = xfer_index; 262 usbd_transfer_start(sc->xfer[xfer_index]); 263 } 264 265 static void 266 bbb_data_clear_stall_callback(struct usb_xfer *xfer, 267 uint8_t next_xfer, uint8_t stall_xfer) 268 { 269 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 270 271 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) { 272 switch (USB_GET_STATE(xfer)) { 273 case USB_ST_SETUP: 274 case USB_ST_TRANSFERRED: 275 bbb_transfer_start(sc, next_xfer); 276 break; 277 default: 278 bbb_done(sc, USB_ERR_STALLED); 279 break; 280 } 281 } 282 } 283 284 static void 285 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error) 286 { 287 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 288 uint32_t tag; 289 290 switch (USB_GET_STATE(xfer)) { 291 case USB_ST_TRANSFERRED: 292 bbb_transfer_start 293 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD : 294 (sc->dir == DIR_OUT) ? ST_DATA_WR : 295 ST_STATUS)); 296 break; 297 298 case USB_ST_SETUP: 299 sc->status_try = 0; 300 tag = UGETDW(sc->cbw->dCBWTag) + 1; 301 USETDW(sc->cbw->dCBWSignature, CBWSIGNATURE); 302 USETDW(sc->cbw->dCBWTag, tag); 303 USETDW(sc->cbw->dCBWDataTransferLength, (uint32_t)sc->data_len); 304 sc->cbw->bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT); 305 sc->cbw->bCBWLUN = sc->lun; 306 sc->cbw->bCDBLength = sc->cmd_len; 307 if (sc->cbw->bCDBLength > sizeof(sc->cbw->CBWCDB)) { 308 sc->cbw->bCDBLength = sizeof(sc->cbw->CBWCDB); 309 DPRINTFN(0, "Truncating long command\n"); 310 } 311 usbd_xfer_set_frame_len(xfer, 0, 312 sizeof(struct bbb_cbw)); 313 usbd_transfer_submit(xfer); 314 break; 315 316 default: /* Error */ 317 bbb_done(sc, error); 318 break; 319 } 320 } 321 322 static void 323 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error) 324 { 325 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 326 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 327 int actlen, sumlen; 328 329 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 330 331 switch (USB_GET_STATE(xfer)) { 332 case USB_ST_TRANSFERRED: 333 sc->data_rem -= actlen; 334 sc->data_ptr += actlen; 335 sc->actlen += actlen; 336 337 if (actlen < sumlen) { 338 /* short transfer */ 339 sc->data_rem = 0; 340 } 341 case USB_ST_SETUP: 342 DPRINTF("max_bulk=%d, data_rem=%d\n", 343 max_bulk, sc->data_rem); 344 345 if (sc->data_rem == 0) { 346 bbb_transfer_start(sc, ST_STATUS); 347 break; 348 } 349 if (max_bulk > sc->data_rem) { 350 max_bulk = sc->data_rem; 351 } 352 usbd_xfer_set_timeout(xfer, sc->data_timeout); 353 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 354 usbd_transfer_submit(xfer); 355 break; 356 357 default: /* Error */ 358 if (error == USB_ERR_CANCELLED) { 359 bbb_done(sc, error); 360 } else { 361 bbb_transfer_start(sc, ST_DATA_RD_CS); 362 } 363 break; 364 } 365 } 366 367 static void 368 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error) 369 { 370 bbb_data_clear_stall_callback(xfer, ST_STATUS, 371 ST_DATA_RD); 372 } 373 374 static void 375 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error) 376 { 377 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 378 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 379 int actlen, sumlen; 380 381 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 382 383 switch (USB_GET_STATE(xfer)) { 384 case USB_ST_TRANSFERRED: 385 sc->data_rem -= actlen; 386 sc->data_ptr += actlen; 387 sc->actlen += actlen; 388 389 if (actlen < sumlen) { 390 /* short transfer */ 391 sc->data_rem = 0; 392 } 393 case USB_ST_SETUP: 394 DPRINTF("max_bulk=%d, data_rem=%d\n", 395 max_bulk, sc->data_rem); 396 397 if (sc->data_rem == 0) { 398 bbb_transfer_start(sc, ST_STATUS); 399 break; 400 } 401 if (max_bulk > sc->data_rem) { 402 max_bulk = sc->data_rem; 403 } 404 usbd_xfer_set_timeout(xfer, sc->data_timeout); 405 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 406 usbd_transfer_submit(xfer); 407 break; 408 409 default: /* Error */ 410 if (error == USB_ERR_CANCELLED) { 411 bbb_done(sc, error); 412 } else { 413 bbb_transfer_start(sc, ST_DATA_WR_CS); 414 } 415 break; 416 } 417 } 418 419 static void 420 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error) 421 { 422 bbb_data_clear_stall_callback(xfer, ST_STATUS, 423 ST_DATA_WR); 424 } 425 426 static void 427 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error) 428 { 429 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 430 int actlen; 431 int sumlen; 432 433 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 434 435 switch (USB_GET_STATE(xfer)) { 436 case USB_ST_TRANSFERRED: 437 438 /* very simple status check */ 439 440 if (actlen < (int)sizeof(struct bbb_csw)) { 441 bbb_done(sc, USB_ERR_SHORT_XFER); 442 } else if (sc->csw->bCSWStatus == CSWSTATUS_GOOD) { 443 bbb_done(sc, 0); /* success */ 444 } else { 445 bbb_done(sc, ERR_CSW_FAILED); /* error */ 446 } 447 break; 448 449 case USB_ST_SETUP: 450 usbd_xfer_set_frame_len(xfer, 0, 451 sizeof(struct bbb_csw)); 452 usbd_transfer_submit(xfer); 453 break; 454 455 default: 456 DPRINTF("Failed to read CSW: %s, try %d\n", 457 usbd_errstr(error), sc->status_try); 458 459 if (error == USB_ERR_CANCELLED || sc->status_try) { 460 bbb_done(sc, error); 461 } else { 462 sc->status_try = 1; 463 bbb_transfer_start(sc, ST_DATA_RD_CS); 464 } 465 break; 466 } 467 } 468 469 /*------------------------------------------------------------------------* 470 * bbb_command_start - execute a SCSI command synchronously 471 * 472 * Return values 473 * 0: Success 474 * Else: Failure 475 *------------------------------------------------------------------------*/ 476 static int 477 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun, 478 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len, 479 usb_timeout_t data_timeout) 480 { 481 sc->lun = lun; 482 sc->dir = data_len ? dir : DIR_NONE; 483 sc->data_ptr = data_ptr; 484 sc->data_len = data_len; 485 sc->data_rem = data_len; 486 sc->data_timeout = (data_timeout + USB_MS_HZ); 487 sc->actlen = 0; 488 sc->cmd_len = cmd_len; 489 memset(&sc->cbw->CBWCDB, 0, sizeof(sc->cbw->CBWCDB)); 490 memcpy(&sc->cbw->CBWCDB, cmd_ptr, cmd_len); 491 DPRINTFN(1, "SCSI cmd = %*D\n", (int)cmd_len, (char *)sc->cbw->CBWCDB, ":"); 492 493 mtx_lock(&sc->mtx); 494 usbd_transfer_start(sc->xfer[sc->state]); 495 496 while (usbd_transfer_pending(sc->xfer[sc->state])) { 497 cv_wait(&sc->cv, &sc->mtx); 498 } 499 mtx_unlock(&sc->mtx); 500 return (sc->error); 501 } 502 503 static struct bbb_transfer * 504 bbb_attach(struct usb_device *udev, uint8_t iface_index) 505 { 506 struct usb_interface *iface; 507 struct usb_interface_descriptor *id; 508 struct bbb_transfer *sc; 509 usb_error_t err; 510 511 #if USB_HAVE_MSCTEST_DETACH 512 uint8_t do_unlock; 513 514 /* Prevent re-enumeration */ 515 do_unlock = usbd_enum_lock(udev); 516 517 /* 518 * Make sure any driver which is hooked up to this interface, 519 * like umass is gone: 520 */ 521 usb_detach_device(udev, iface_index, 0); 522 523 if (do_unlock) 524 usbd_enum_unlock(udev); 525 #endif 526 527 iface = usbd_get_iface(udev, iface_index); 528 if (iface == NULL) 529 return (NULL); 530 531 id = iface->idesc; 532 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 533 return (NULL); 534 535 switch (id->bInterfaceSubClass) { 536 case UISUBCLASS_SCSI: 537 case UISUBCLASS_UFI: 538 case UISUBCLASS_SFF8020I: 539 case UISUBCLASS_SFF8070I: 540 break; 541 default: 542 return (NULL); 543 } 544 545 switch (id->bInterfaceProtocol) { 546 case UIPROTO_MASS_BBB_OLD: 547 case UIPROTO_MASS_BBB: 548 break; 549 default: 550 return (NULL); 551 } 552 553 sc = malloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO); 554 mtx_init(&sc->mtx, "USB autoinstall", NULL, MTX_DEF); 555 cv_init(&sc->cv, "WBBB"); 556 557 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config, 558 ST_MAX, sc, &sc->mtx); 559 if (err) { 560 bbb_detach(sc); 561 return (NULL); 562 } 563 /* store pointer to DMA buffers */ 564 sc->buffer = usbd_xfer_get_frame_buffer( 565 sc->xfer[ST_DATA_RD], 0); 566 sc->buffer_size = 567 usbd_xfer_max_len(sc->xfer[ST_DATA_RD]); 568 sc->cbw = usbd_xfer_get_frame_buffer( 569 sc->xfer[ST_COMMAND], 0); 570 sc->csw = usbd_xfer_get_frame_buffer( 571 sc->xfer[ST_STATUS], 0); 572 573 return (sc); 574 } 575 576 static void 577 bbb_detach(struct bbb_transfer *sc) 578 { 579 usbd_transfer_unsetup(sc->xfer, ST_MAX); 580 mtx_destroy(&sc->mtx); 581 cv_destroy(&sc->cv); 582 free(sc, M_USB); 583 } 584 585 /*------------------------------------------------------------------------* 586 * usb_iface_is_cdrom 587 * 588 * Return values: 589 * 1: This interface is an auto install disk (CD-ROM) 590 * 0: Not an auto install disk. 591 *------------------------------------------------------------------------*/ 592 int 593 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index) 594 { 595 struct bbb_transfer *sc; 596 uint8_t timeout; 597 uint8_t is_cdrom; 598 uint8_t sid_type; 599 int err; 600 601 sc = bbb_attach(udev, iface_index); 602 if (sc == NULL) 603 return (0); 604 605 is_cdrom = 0; 606 timeout = 4; /* tries */ 607 while (--timeout) { 608 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 609 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 610 USB_MS_HZ); 611 612 if (err == 0 && sc->actlen > 0) { 613 sid_type = sc->buffer[0] & 0x1F; 614 if (sid_type == 0x05) 615 is_cdrom = 1; 616 break; 617 } else if (err != ERR_CSW_FAILED) 618 break; /* non retryable error */ 619 usb_pause_mtx(NULL, hz); 620 } 621 bbb_detach(sc); 622 return (is_cdrom); 623 } 624 625 static uint8_t 626 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index) 627 { 628 struct usb_device_request req; 629 usb_error_t err; 630 uint8_t buf = 0; 631 632 633 /* The Get Max Lun command is a class-specific request. */ 634 req.bmRequestType = UT_READ_CLASS_INTERFACE; 635 req.bRequest = 0xFE; /* GET_MAX_LUN */ 636 USETW(req.wValue, 0); 637 req.wIndex[0] = iface_index; 638 req.wIndex[1] = 0; 639 USETW(req.wLength, 1); 640 641 err = usbd_do_request(udev, NULL, &req, &buf); 642 if (err) 643 buf = 0; 644 645 return (buf); 646 } 647 648 usb_error_t 649 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index) 650 { 651 struct bbb_transfer *sc; 652 uint8_t timeout; 653 uint8_t is_no_direct; 654 uint8_t sid_type; 655 int err; 656 657 sc = bbb_attach(udev, iface_index); 658 if (sc == NULL) 659 return (0); 660 661 /* 662 * Some devices need a delay after that the configuration 663 * value is set to function properly: 664 */ 665 usb_pause_mtx(NULL, hz); 666 667 if (usb_msc_get_max_lun(udev, iface_index) == 0) { 668 DPRINTF("Device has only got one LUN.\n"); 669 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN); 670 } 671 672 is_no_direct = 1; 673 for (timeout = 4; timeout != 0; timeout--) { 674 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 675 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 676 USB_MS_HZ); 677 678 if (err == 0 && sc->actlen > 0) { 679 sid_type = sc->buffer[0] & 0x1F; 680 if (sid_type == 0x00) 681 is_no_direct = 0; 682 break; 683 } else if (err != ERR_CSW_FAILED) { 684 DPRINTF("Device is not responding " 685 "properly to SCSI INQUIRY command.\n"); 686 goto error; /* non retryable error */ 687 } 688 usb_pause_mtx(NULL, hz); 689 } 690 691 if (is_no_direct) { 692 DPRINTF("Device is not direct access.\n"); 693 goto done; 694 } 695 696 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 697 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 698 USB_MS_HZ); 699 700 if (err != 0) { 701 702 if (err != ERR_CSW_FAILED) 703 goto error; 704 } 705 timeout = 1; 706 707 retry_sync_cache: 708 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 709 &scsi_sync_cache, sizeof(scsi_sync_cache), 710 USB_MS_HZ); 711 712 if (err != 0) { 713 714 if (err != ERR_CSW_FAILED) 715 goto error; 716 717 DPRINTF("Device doesn't handle synchronize cache\n"); 718 719 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 720 721 } else { 722 723 /* 724 * Certain Kingston memory sticks fail the first 725 * read capacity after a synchronize cache command 726 * has been issued. Disable the synchronize cache 727 * command for such devices. 728 */ 729 730 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8, 731 &scsi_read_capacity, sizeof(scsi_read_capacity), 732 USB_MS_HZ); 733 734 if (err != 0) { 735 if (err != ERR_CSW_FAILED) 736 goto error; 737 738 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8, 739 &scsi_read_capacity, sizeof(scsi_read_capacity), 740 USB_MS_HZ); 741 742 if (err == 0) { 743 if (timeout--) 744 goto retry_sync_cache; 745 746 DPRINTF("Device most likely doesn't " 747 "handle synchronize cache\n"); 748 749 usbd_add_dynamic_quirk(udev, 750 UQ_MSC_NO_SYNC_CACHE); 751 } else { 752 if (err != ERR_CSW_FAILED) 753 goto error; 754 } 755 } 756 } 757 758 /* clear sense status of any failed commands on the device */ 759 760 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 761 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 762 USB_MS_HZ); 763 764 DPRINTF("Inquiry = %d\n", err); 765 766 if (err != 0) { 767 768 if (err != ERR_CSW_FAILED) 769 goto error; 770 } 771 772 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 773 SCSI_SENSE_LEN, &scsi_request_sense, 774 sizeof(scsi_request_sense), USB_MS_HZ); 775 776 DPRINTF("Request sense = %d\n", err); 777 778 if (err != 0) { 779 780 if (err != ERR_CSW_FAILED) 781 goto error; 782 } 783 784 done: 785 bbb_detach(sc); 786 return (0); 787 788 error: 789 bbb_detach(sc); 790 791 DPRINTF("Device did not respond, enabling all quirks\n"); 792 793 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 794 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY); 795 796 /* Need to re-enumerate the device */ 797 usbd_req_re_enumerate(udev, NULL); 798 799 return (USB_ERR_STALLED); 800 } 801 802 usb_error_t 803 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method) 804 { 805 struct bbb_transfer *sc; 806 usb_error_t err; 807 808 sc = bbb_attach(udev, iface_index); 809 if (sc == NULL) 810 return (USB_ERR_INVAL); 811 812 switch (method) { 813 case MSC_EJECT_STOPUNIT: 814 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 815 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 816 USB_MS_HZ); 817 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err)); 818 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 819 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit), 820 USB_MS_HZ); 821 break; 822 case MSC_EJECT_REZERO: 823 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 824 &scsi_rezero_init, sizeof(scsi_rezero_init), 825 USB_MS_HZ); 826 break; 827 case MSC_EJECT_ZTESTOR: 828 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 829 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject), 830 USB_MS_HZ); 831 break; 832 case MSC_EJECT_CMOTECH: 833 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 834 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject), 835 USB_MS_HZ); 836 break; 837 case MSC_EJECT_HUAWEI: 838 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 839 &scsi_huawei_eject, sizeof(scsi_huawei_eject), 840 USB_MS_HZ); 841 break; 842 case MSC_EJECT_TCT: 843 /* 844 * TCTMobile needs DIR_IN flag. To get it, we 845 * supply a dummy data with the command. 846 */ 847 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 848 sc->buffer_size, &scsi_tct_eject, 849 sizeof(scsi_tct_eject), USB_MS_HZ); 850 break; 851 default: 852 DPRINTF("Unknown eject method (%d)\n", method); 853 err = 0; 854 break; 855 } 856 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err)); 857 858 bbb_detach(sc); 859 return (0); 860 } 861 862 usb_error_t 863 usb_msc_read_10(struct usb_device *udev, uint8_t iface_index, 864 uint32_t lba, uint32_t blocks, void *buffer) 865 { 866 struct bbb_transfer *sc; 867 uint8_t cmd[10]; 868 usb_error_t err; 869 870 cmd[0] = 0x28; /* READ_10 */ 871 cmd[1] = 0; 872 cmd[2] = lba >> 24; 873 cmd[3] = lba >> 16; 874 cmd[4] = lba >> 8; 875 cmd[5] = lba >> 0; 876 cmd[6] = 0; 877 cmd[7] = blocks >> 8; 878 cmd[8] = blocks; 879 cmd[9] = 0; 880 881 sc = bbb_attach(udev, iface_index); 882 if (sc == NULL) 883 return (USB_ERR_INVAL); 884 885 err = bbb_command_start(sc, DIR_IN, 0, buffer, 886 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ); 887 888 bbb_detach(sc); 889 890 return (err); 891 } 892 893 usb_error_t 894 usb_msc_write_10(struct usb_device *udev, uint8_t iface_index, 895 uint32_t lba, uint32_t blocks, void *buffer) 896 { 897 struct bbb_transfer *sc; 898 uint8_t cmd[10]; 899 usb_error_t err; 900 901 cmd[0] = 0x2a; /* WRITE_10 */ 902 cmd[1] = 0; 903 cmd[2] = lba >> 24; 904 cmd[3] = lba >> 16; 905 cmd[4] = lba >> 8; 906 cmd[5] = lba >> 0; 907 cmd[6] = 0; 908 cmd[7] = blocks >> 8; 909 cmd[8] = blocks; 910 cmd[9] = 0; 911 912 sc = bbb_attach(udev, iface_index); 913 if (sc == NULL) 914 return (USB_ERR_INVAL); 915 916 err = bbb_command_start(sc, DIR_OUT, 0, buffer, 917 blocks * SCSI_FIXED_BLOCK_SIZE, cmd, 10, USB_MS_HZ); 918 919 bbb_detach(sc); 920 921 return (err); 922 } 923 924 usb_error_t 925 usb_msc_read_capacity(struct usb_device *udev, uint8_t iface_index, 926 uint32_t *lba_last, uint32_t *block_size) 927 { 928 struct bbb_transfer *sc; 929 usb_error_t err; 930 931 sc = bbb_attach(udev, iface_index); 932 if (sc == NULL) 933 return (USB_ERR_INVAL); 934 935 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 8, 936 &scsi_read_capacity, sizeof(scsi_read_capacity), 937 USB_MS_HZ); 938 939 *lba_last = 940 (sc->buffer[0] << 24) | 941 (sc->buffer[1] << 16) | 942 (sc->buffer[2] << 8) | 943 (sc->buffer[3]); 944 945 *block_size = 946 (sc->buffer[4] << 24) | 947 (sc->buffer[5] << 16) | 948 (sc->buffer[6] << 8) | 949 (sc->buffer[7]); 950 951 /* we currently only support one block size */ 952 if (*block_size != SCSI_FIXED_BLOCK_SIZE) 953 err = USB_ERR_INVAL; 954 955 bbb_detach(sc); 956 957 return (err); 958 } 959