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 #include <sys/stdint.h> 36 #include <sys/param.h> 37 #include <sys/queue.h> 38 #include <sys/types.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/bus.h> 42 #include <sys/module.h> 43 #include <sys/lock.h> 44 #include <sys/mutex.h> 45 #include <sys/condvar.h> 46 #include <sys/sysctl.h> 47 #include <sys/unistd.h> 48 #include <sys/callout.h> 49 #include <sys/malloc.h> 50 #include <sys/priv.h> 51 52 #include <bus/u4b/usb.h> 53 #include <bus/u4b/usbdi.h> 54 #include <bus/u4b/usbdi_util.h> 55 56 #define USB_DEBUG_VAR usb_debug 57 58 #include <bus/u4b/usb_busdma.h> 59 #include <bus/u4b/usb_process.h> 60 #include <bus/u4b/usb_transfer.h> 61 #include <bus/u4b/usb_msctest.h> 62 #include <bus/u4b/usb_debug.h> 63 #include <bus/u4b/usb_device.h> 64 #include <bus/u4b/usb_request.h> 65 #include <bus/u4b/usb_util.h> 66 #include <bus/u4b/quirk/usb_quirk.h> 67 68 enum { 69 ST_COMMAND, 70 ST_DATA_RD, 71 ST_DATA_RD_CS, 72 ST_DATA_WR, 73 ST_DATA_WR_CS, 74 ST_STATUS, 75 ST_MAX, 76 }; 77 78 enum { 79 DIR_IN, 80 DIR_OUT, 81 DIR_NONE, 82 }; 83 84 #define SCSI_MAX_LEN 0x100 85 #define SCSI_INQ_LEN 0x24 86 #define SCSI_SENSE_LEN 0xFF 87 88 static uint8_t scsi_test_unit_ready[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 89 static uint8_t scsi_inquiry[] = { 0x12, 0x00, 0x00, 0x00, SCSI_INQ_LEN, 0x00 }; 90 static uint8_t scsi_rezero_init[] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 }; 91 static uint8_t scsi_start_stop_unit[] = { 0x1b, 0x00, 0x00, 0x00, 0x02, 0x00 }; 92 static uint8_t scsi_ztestor_eject[] = { 0x85, 0x01, 0x01, 0x01, 0x18, 0x01, 93 0x01, 0x01, 0x01, 0x01, 0x00, 0x00 }; 94 static uint8_t scsi_cmotech_eject[] = { 0xff, 0x52, 0x44, 0x45, 0x56, 0x43, 95 0x48, 0x47 }; 96 static uint8_t scsi_huawei_eject[] = { 0x11, 0x06, 0x00, 0x00, 0x00, 0x00, 97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 98 0x00, 0x00, 0x00, 0x00 }; 99 static uint8_t scsi_tct_eject[] = { 0x06, 0xf5, 0x04, 0x02, 0x52, 0x70 }; 100 static uint8_t scsi_sync_cache[] = { 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 101 0x00, 0x00, 0x00, 0x00 }; 102 static uint8_t scsi_request_sense[] = { 0x03, 0x00, 0x00, 0x00, 0x12, 0x00, 103 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 104 105 #define BULK_SIZE 64 /* dummy */ 106 #define ERR_CSW_FAILED -1 107 108 /* Command Block Wrapper */ 109 struct bbb_cbw { 110 uDWord dCBWSignature; 111 #define CBWSIGNATURE 0x43425355 112 uDWord dCBWTag; 113 uDWord dCBWDataTransferLength; 114 uByte bCBWFlags; 115 #define CBWFLAGS_OUT 0x00 116 #define CBWFLAGS_IN 0x80 117 uByte bCBWLUN; 118 uByte bCDBLength; 119 #define CBWCDBLENGTH 16 120 uByte CBWCDB[CBWCDBLENGTH]; 121 } __packed; 122 123 /* Command Status Wrapper */ 124 struct bbb_csw { 125 uDWord dCSWSignature; 126 #define CSWSIGNATURE 0x53425355 127 uDWord dCSWTag; 128 uDWord dCSWDataResidue; 129 uByte bCSWStatus; 130 #define CSWSTATUS_GOOD 0x0 131 #define CSWSTATUS_FAILED 0x1 132 #define CSWSTATUS_PHASE 0x2 133 } __packed; 134 135 struct bbb_transfer { 136 struct lock lock; 137 struct cv cv; 138 struct bbb_cbw cbw; 139 struct bbb_csw csw; 140 141 struct usb_xfer *xfer[ST_MAX]; 142 143 uint8_t *data_ptr; 144 145 usb_size_t data_len; /* bytes */ 146 usb_size_t data_rem; /* bytes */ 147 usb_timeout_t data_timeout; /* ms */ 148 usb_frlength_t actlen; /* bytes */ 149 150 uint8_t cmd_len; /* bytes */ 151 uint8_t dir; 152 uint8_t lun; 153 uint8_t state; 154 uint8_t status_try; 155 int error; 156 157 uint8_t buffer[SCSI_MAX_LEN] __aligned(4); 158 }; 159 160 static usb_callback_t bbb_command_callback; 161 static usb_callback_t bbb_data_read_callback; 162 static usb_callback_t bbb_data_rd_cs_callback; 163 static usb_callback_t bbb_data_write_callback; 164 static usb_callback_t bbb_data_wr_cs_callback; 165 static usb_callback_t bbb_status_callback; 166 167 static void bbb_done(struct bbb_transfer *, int); 168 static void bbb_transfer_start(struct bbb_transfer *, uint8_t); 169 static void bbb_data_clear_stall_callback(struct usb_xfer *, uint8_t, 170 uint8_t); 171 static int bbb_command_start(struct bbb_transfer *, uint8_t, uint8_t, 172 void *, size_t, void *, size_t, usb_timeout_t); 173 static struct bbb_transfer *bbb_attach(struct usb_device *, uint8_t); 174 static void bbb_detach(struct bbb_transfer *); 175 176 static const struct usb_config bbb_config[ST_MAX] = { 177 178 [ST_COMMAND] = { 179 .type = UE_BULK, 180 .endpoint = UE_ADDR_ANY, 181 .direction = UE_DIR_OUT, 182 .bufsize = sizeof(struct bbb_cbw), 183 .flags = {.ext_buffer = 1,}, 184 .callback = &bbb_command_callback, 185 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 186 }, 187 188 [ST_DATA_RD] = { 189 .type = UE_BULK, 190 .endpoint = UE_ADDR_ANY, 191 .direction = UE_DIR_IN, 192 .bufsize = BULK_SIZE, 193 .flags = {.ext_buffer = 1,.proxy_buffer = 1,.short_xfer_ok = 1,}, 194 .callback = &bbb_data_read_callback, 195 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 196 }, 197 198 [ST_DATA_RD_CS] = { 199 .type = UE_CONTROL, 200 .endpoint = 0x00, /* Control pipe */ 201 .direction = UE_DIR_ANY, 202 .bufsize = sizeof(struct usb_device_request), 203 .callback = &bbb_data_rd_cs_callback, 204 .timeout = 1 * USB_MS_HZ, /* 1 second */ 205 }, 206 207 [ST_DATA_WR] = { 208 .type = UE_BULK, 209 .endpoint = UE_ADDR_ANY, 210 .direction = UE_DIR_OUT, 211 .bufsize = BULK_SIZE, 212 .flags = {.ext_buffer = 1,.proxy_buffer = 1,}, 213 .callback = &bbb_data_write_callback, 214 .timeout = 4 * USB_MS_HZ, /* 4 seconds */ 215 }, 216 217 [ST_DATA_WR_CS] = { 218 .type = UE_CONTROL, 219 .endpoint = 0x00, /* Control pipe */ 220 .direction = UE_DIR_ANY, 221 .bufsize = sizeof(struct usb_device_request), 222 .callback = &bbb_data_wr_cs_callback, 223 .timeout = 1 * USB_MS_HZ, /* 1 second */ 224 }, 225 226 [ST_STATUS] = { 227 .type = UE_BULK, 228 .endpoint = UE_ADDR_ANY, 229 .direction = UE_DIR_IN, 230 .bufsize = sizeof(struct bbb_csw), 231 .flags = {.ext_buffer = 1,.short_xfer_ok = 1,}, 232 .callback = &bbb_status_callback, 233 .timeout = 1 * USB_MS_HZ, /* 1 second */ 234 }, 235 }; 236 237 static void 238 bbb_done(struct bbb_transfer *sc, int error) 239 { 240 241 sc->error = error; 242 sc->state = ST_COMMAND; 243 sc->status_try = 1; 244 cv_signal(&sc->cv); 245 } 246 247 static void 248 bbb_transfer_start(struct bbb_transfer *sc, uint8_t xfer_index) 249 { 250 sc->state = xfer_index; 251 usbd_transfer_start(sc->xfer[xfer_index]); 252 } 253 254 static void 255 bbb_data_clear_stall_callback(struct usb_xfer *xfer, 256 uint8_t next_xfer, uint8_t stall_xfer) 257 { 258 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 259 260 if (usbd_clear_stall_callback(xfer, sc->xfer[stall_xfer])) { 261 switch (USB_GET_STATE(xfer)) { 262 case USB_ST_SETUP: 263 case USB_ST_TRANSFERRED: 264 bbb_transfer_start(sc, next_xfer); 265 break; 266 default: 267 bbb_done(sc, USB_ERR_STALLED); 268 break; 269 } 270 } 271 } 272 273 static void 274 bbb_command_callback(struct usb_xfer *xfer, usb_error_t error) 275 { 276 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 277 uint32_t tag; 278 279 switch (USB_GET_STATE(xfer)) { 280 case USB_ST_TRANSFERRED: 281 bbb_transfer_start 282 (sc, ((sc->dir == DIR_IN) ? ST_DATA_RD : 283 (sc->dir == DIR_OUT) ? ST_DATA_WR : 284 ST_STATUS)); 285 break; 286 287 case USB_ST_SETUP: 288 sc->status_try = 0; 289 tag = UGETDW(sc->cbw.dCBWTag) + 1; 290 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE); 291 USETDW(sc->cbw.dCBWTag, tag); 292 USETDW(sc->cbw.dCBWDataTransferLength, (uint32_t)sc->data_len); 293 sc->cbw.bCBWFlags = ((sc->dir == DIR_IN) ? CBWFLAGS_IN : CBWFLAGS_OUT); 294 sc->cbw.bCBWLUN = sc->lun; 295 sc->cbw.bCDBLength = sc->cmd_len; 296 if (sc->cbw.bCDBLength > sizeof(sc->cbw.CBWCDB)) { 297 sc->cbw.bCDBLength = sizeof(sc->cbw.CBWCDB); 298 DPRINTFN(0, "Truncating long command\n"); 299 } 300 usbd_xfer_set_frame_data(xfer, 0, &sc->cbw, sizeof(sc->cbw)); 301 usbd_transfer_submit(xfer); 302 break; 303 304 default: /* Error */ 305 bbb_done(sc, error); 306 break; 307 } 308 } 309 310 static void 311 bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error) 312 { 313 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 314 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 315 int actlen, sumlen; 316 317 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 318 319 switch (USB_GET_STATE(xfer)) { 320 case USB_ST_TRANSFERRED: 321 sc->data_rem -= actlen; 322 sc->data_ptr += actlen; 323 sc->actlen += actlen; 324 325 if (actlen < sumlen) { 326 /* short transfer */ 327 sc->data_rem = 0; 328 } 329 case USB_ST_SETUP: 330 DPRINTF("max_bulk=%d, data_rem=%d\n", 331 max_bulk, sc->data_rem); 332 333 if (sc->data_rem == 0) { 334 bbb_transfer_start(sc, ST_STATUS); 335 break; 336 } 337 if (max_bulk > sc->data_rem) { 338 max_bulk = sc->data_rem; 339 } 340 usbd_xfer_set_timeout(xfer, sc->data_timeout); 341 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 342 usbd_transfer_submit(xfer); 343 break; 344 345 default: /* Error */ 346 if (error == USB_ERR_CANCELLED) { 347 bbb_done(sc, error); 348 } else { 349 bbb_transfer_start(sc, ST_DATA_RD_CS); 350 } 351 break; 352 } 353 } 354 355 static void 356 bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error) 357 { 358 bbb_data_clear_stall_callback(xfer, ST_STATUS, 359 ST_DATA_RD); 360 } 361 362 static void 363 bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error) 364 { 365 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 366 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 367 int actlen, sumlen; 368 369 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 370 371 switch (USB_GET_STATE(xfer)) { 372 case USB_ST_TRANSFERRED: 373 sc->data_rem -= actlen; 374 sc->data_ptr += actlen; 375 sc->actlen += actlen; 376 377 if (actlen < sumlen) { 378 /* short transfer */ 379 sc->data_rem = 0; 380 } 381 case USB_ST_SETUP: 382 DPRINTF("max_bulk=%d, data_rem=%d\n", 383 max_bulk, sc->data_rem); 384 385 if (sc->data_rem == 0) { 386 bbb_transfer_start(sc, ST_STATUS); 387 return; 388 } 389 if (max_bulk > sc->data_rem) { 390 max_bulk = sc->data_rem; 391 } 392 usbd_xfer_set_timeout(xfer, sc->data_timeout); 393 usbd_xfer_set_frame_data(xfer, 0, sc->data_ptr, max_bulk); 394 usbd_transfer_submit(xfer); 395 return; 396 397 default: /* Error */ 398 if (error == USB_ERR_CANCELLED) { 399 bbb_done(sc, error); 400 } else { 401 bbb_transfer_start(sc, ST_DATA_WR_CS); 402 } 403 return; 404 405 } 406 } 407 408 static void 409 bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error) 410 { 411 bbb_data_clear_stall_callback(xfer, ST_STATUS, 412 ST_DATA_WR); 413 } 414 415 static void 416 bbb_status_callback(struct usb_xfer *xfer, usb_error_t error) 417 { 418 struct bbb_transfer *sc = usbd_xfer_softc(xfer); 419 int actlen, sumlen; 420 421 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 422 423 switch (USB_GET_STATE(xfer)) { 424 case USB_ST_TRANSFERRED: 425 426 /* very simple status check */ 427 428 if (actlen < sizeof(sc->csw)) { 429 bbb_done(sc, USB_ERR_SHORT_XFER); 430 } else if (sc->csw.bCSWStatus == CSWSTATUS_GOOD) { 431 bbb_done(sc, 0); /* success */ 432 } else { 433 bbb_done(sc, ERR_CSW_FAILED); /* error */ 434 } 435 break; 436 437 case USB_ST_SETUP: 438 usbd_xfer_set_frame_data(xfer, 0, &sc->csw, sizeof(sc->csw)); 439 usbd_transfer_submit(xfer); 440 break; 441 442 default: 443 DPRINTF("Failed to read CSW: %s, try %d\n", 444 usbd_errstr(error), sc->status_try); 445 446 if (error == USB_ERR_CANCELLED || sc->status_try) { 447 bbb_done(sc, error); 448 } else { 449 sc->status_try = 1; 450 bbb_transfer_start(sc, ST_DATA_RD_CS); 451 } 452 break; 453 } 454 } 455 456 /*------------------------------------------------------------------------* 457 * bbb_command_start - execute a SCSI command synchronously 458 * 459 * Return values 460 * 0: Success 461 * Else: Failure 462 *------------------------------------------------------------------------*/ 463 static int 464 bbb_command_start(struct bbb_transfer *sc, uint8_t dir, uint8_t lun, 465 void *data_ptr, size_t data_len, void *cmd_ptr, size_t cmd_len, 466 usb_timeout_t data_timeout) 467 { 468 #ifdef USB_DEBUG 469 char hexstr[HEX_NCPYLEN(CBWCDBLENGTH)]; 470 #endif 471 472 sc->lun = lun; 473 sc->dir = data_len ? dir : DIR_NONE; 474 sc->data_ptr = data_ptr; 475 sc->data_len = data_len; 476 sc->data_rem = data_len; 477 sc->data_timeout = (data_timeout + USB_MS_HZ); 478 sc->actlen = 0; 479 sc->cmd_len = cmd_len; 480 memset(&sc->cbw.CBWCDB, 0, sizeof(sc->cbw.CBWCDB)); 481 memcpy(&sc->cbw.CBWCDB, cmd_ptr, cmd_len); 482 DPRINTFN(1, "SCSI cmd = %s\n", hexncpy(sc->cbw.CBWCDB, cmd_len, 483 hexstr, HEX_NCPYLEN(cmd_len), ":")); 484 lockmgr(&sc->lock, LK_EXCLUSIVE); 485 usbd_transfer_start(sc->xfer[sc->state]); 486 487 while (usbd_transfer_pending(sc->xfer[sc->state])) { 488 cv_wait(&sc->cv, &sc->lock); 489 } 490 lockmgr(&sc->lock, LK_RELEASE); 491 return (sc->error); 492 } 493 494 static struct bbb_transfer * 495 bbb_attach(struct usb_device *udev, uint8_t iface_index) 496 { 497 struct usb_interface *iface; 498 struct usb_interface_descriptor *id; 499 struct bbb_transfer *sc; 500 usb_error_t err; 501 uint8_t do_unlock; 502 503 /* automatic locking */ 504 if (usbd_enum_is_locked(udev)) { 505 do_unlock = 0; 506 } else { 507 do_unlock = 1; 508 usbd_enum_lock(udev); 509 } 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 = kmalloc(sizeof(*sc), M_USB, M_WAITOK | M_ZERO); 547 lockinit(&sc->lock, "USB autoinstall", 0, 0); 548 cv_init(&sc->cv, "WBBB"); 549 550 err = usbd_transfer_setup(udev, &iface_index, sc->xfer, bbb_config, 551 ST_MAX, sc, &sc->lock); 552 if (err) { 553 bbb_detach(sc); 554 return (NULL); 555 } 556 return (sc); 557 } 558 559 static void 560 bbb_detach(struct bbb_transfer *sc) 561 { 562 usbd_transfer_unsetup(sc->xfer, ST_MAX); 563 lockuninit(&sc->lock); 564 cv_destroy(&sc->cv); 565 kfree(sc, M_USB); 566 } 567 568 /*------------------------------------------------------------------------* 569 * usb_iface_is_cdrom 570 * 571 * Return values: 572 * 1: This interface is an auto install disk (CD-ROM) 573 * 0: Not an auto install disk. 574 *------------------------------------------------------------------------*/ 575 int 576 usb_iface_is_cdrom(struct usb_device *udev, uint8_t iface_index) 577 { 578 struct bbb_transfer *sc; 579 uint8_t timeout; 580 uint8_t is_cdrom; 581 uint8_t sid_type; 582 int err; 583 584 sc = bbb_attach(udev, iface_index); 585 if (sc == NULL) 586 return (0); 587 588 is_cdrom = 0; 589 timeout = 4; /* tries */ 590 while (--timeout) { 591 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 592 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 593 USB_MS_HZ); 594 595 if (err == 0 && sc->actlen > 0) { 596 sid_type = sc->buffer[0] & 0x1F; 597 if (sid_type == 0x05) 598 is_cdrom = 1; 599 break; 600 } else if (err != ERR_CSW_FAILED) 601 break; /* non retryable error */ 602 usb_pause_mtx(NULL, hz); 603 } 604 bbb_detach(sc); 605 return (is_cdrom); 606 } 607 608 static uint8_t 609 usb_msc_get_max_lun(struct usb_device *udev, uint8_t iface_index) 610 { 611 struct usb_device_request req; 612 usb_error_t err; 613 uint8_t buf = 0; 614 615 616 /* The Get Max Lun command is a class-specific request. */ 617 req.bmRequestType = UT_READ_CLASS_INTERFACE; 618 req.bRequest = 0xFE; /* GET_MAX_LUN */ 619 USETW(req.wValue, 0); 620 req.wIndex[0] = iface_index; 621 req.wIndex[1] = 0; 622 USETW(req.wLength, 1); 623 624 err = usbd_do_request(udev, NULL, &req, &buf); 625 if (err) 626 buf = 0; 627 628 return (buf); 629 } 630 631 usb_error_t 632 usb_msc_auto_quirk(struct usb_device *udev, uint8_t iface_index) 633 { 634 struct bbb_transfer *sc; 635 uint8_t timeout; 636 uint8_t is_no_direct; 637 uint8_t sid_type; 638 int err; 639 640 sc = bbb_attach(udev, iface_index); 641 if (sc == NULL) 642 return (0); 643 644 /* 645 * Some devices need a delay after that the configuration 646 * value is set to function properly: 647 */ 648 usb_pause_mtx(NULL, hz); 649 650 if (usb_msc_get_max_lun(udev, iface_index) == 0) { 651 DPRINTF("Device has only got one LUN.\n"); 652 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_GETMAXLUN); 653 } 654 655 is_no_direct = 1; 656 for (timeout = 4; timeout; timeout--) { 657 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 658 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 659 USB_MS_HZ); 660 661 if (err == 0 && sc->actlen > 0) { 662 sid_type = sc->buffer[0] & 0x1F; 663 if (sid_type == 0x00) 664 is_no_direct = 0; 665 break; 666 } else if (err != ERR_CSW_FAILED) 667 break; /* non retryable error */ 668 usb_pause_mtx(NULL, hz); 669 } 670 671 if (is_no_direct) { 672 DPRINTF("Device is not direct access.\n"); 673 goto done; 674 } 675 676 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 677 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 678 USB_MS_HZ); 679 680 if (err != 0) { 681 682 if (err != ERR_CSW_FAILED) 683 goto error; 684 } 685 686 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 687 &scsi_sync_cache, sizeof(scsi_sync_cache), 688 USB_MS_HZ); 689 690 if (err != 0) { 691 692 if (err != ERR_CSW_FAILED) 693 goto error; 694 695 DPRINTF("Device doesn't handle synchronize cache\n"); 696 697 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 698 } 699 700 /* clear sense status of any failed commands on the device */ 701 702 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 703 SCSI_INQ_LEN, &scsi_inquiry, sizeof(scsi_inquiry), 704 USB_MS_HZ); 705 706 DPRINTF("Inquiry = %d\n", err); 707 708 if (err != 0) { 709 710 if (err != ERR_CSW_FAILED) 711 goto error; 712 } 713 714 err = bbb_command_start(sc, DIR_IN, 0, sc->buffer, 715 SCSI_SENSE_LEN, &scsi_request_sense, 716 sizeof(scsi_request_sense), USB_MS_HZ); 717 718 DPRINTF("Request sense = %d\n", err); 719 720 if (err != 0) { 721 722 if (err != ERR_CSW_FAILED) 723 goto error; 724 } 725 726 done: 727 bbb_detach(sc); 728 return (0); 729 730 error: 731 bbb_detach(sc); 732 733 DPRINTF("Device did not respond, enabling all quirks\n"); 734 735 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_SYNC_CACHE); 736 usbd_add_dynamic_quirk(udev, UQ_MSC_NO_TEST_UNIT_READY); 737 738 /* Need to re-enumerate the device */ 739 usbd_req_re_enumerate(udev, NULL); 740 741 return (USB_ERR_STALLED); 742 } 743 744 usb_error_t 745 usb_msc_eject(struct usb_device *udev, uint8_t iface_index, int method) 746 { 747 struct bbb_transfer *sc; 748 usb_error_t err; 749 750 sc = bbb_attach(udev, iface_index); 751 if (sc == NULL) 752 return (USB_ERR_INVAL); 753 754 err = 0; 755 switch (method) { 756 case MSC_EJECT_STOPUNIT: 757 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 758 &scsi_test_unit_ready, sizeof(scsi_test_unit_ready), 759 USB_MS_HZ); 760 DPRINTF("Test unit ready status: %s\n", usbd_errstr(err)); 761 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 762 &scsi_start_stop_unit, sizeof(scsi_start_stop_unit), 763 USB_MS_HZ); 764 break; 765 case MSC_EJECT_REZERO: 766 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 767 &scsi_rezero_init, sizeof(scsi_rezero_init), 768 USB_MS_HZ); 769 break; 770 case MSC_EJECT_ZTESTOR: 771 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 772 &scsi_ztestor_eject, sizeof(scsi_ztestor_eject), 773 USB_MS_HZ); 774 break; 775 case MSC_EJECT_CMOTECH: 776 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 777 &scsi_cmotech_eject, sizeof(scsi_cmotech_eject), 778 USB_MS_HZ); 779 break; 780 case MSC_EJECT_HUAWEI: 781 err = bbb_command_start(sc, DIR_IN, 0, NULL, 0, 782 &scsi_huawei_eject, sizeof(scsi_huawei_eject), 783 USB_MS_HZ); 784 break; 785 case MSC_EJECT_TCT: 786 /* 787 * TCTMobile needs DIR_IN flag. To get it, we 788 * supply a dummy data with the command. 789 */ 790 err = bbb_command_start(sc, DIR_IN, 0, &sc->buffer, 791 sizeof(sc->buffer), &scsi_tct_eject, 792 sizeof(scsi_tct_eject), USB_MS_HZ); 793 break; 794 default: 795 kprintf("usb_msc_eject: unknown eject method (%d)\n", method); 796 break; 797 } 798 DPRINTF("Eject CD command status: %s\n", usbd_errstr(err)); 799 800 bbb_detach(sc); 801 return (0); 802 } 803