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