1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 /*- 5 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>, 6 * Nick Hibma <n_hibma@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * $FreeBSD$ 31 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $ 32 */ 33 34 /* Also already merged from NetBSD: 35 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $ 36 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $ 37 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $ 38 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $ 39 */ 40 41 /* 42 * Universal Serial Bus Mass Storage Class specs: 43 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf 44 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf 45 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf 46 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf 47 */ 48 49 /* 50 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>. 51 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>. 52 */ 53 54 /* 55 * The driver handles 3 Wire Protocols 56 * - Command/Bulk/Interrupt (CBI) 57 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI) 58 * - Mass Storage Bulk-Only (BBB) 59 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases) 60 * 61 * Over these wire protocols it handles the following command protocols 62 * - SCSI 63 * - UFI (floppy command set) 64 * - 8070i (ATAPI) 65 * 66 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The 67 * sc->sc_transform method is used to convert the commands into the appropriate 68 * format (if at all necessary). For example, UFI requires all commands to be 69 * 12 bytes in length amongst other things. 70 * 71 * The source code below is marked and can be split into a number of pieces 72 * (in this order): 73 * 74 * - probe/attach/detach 75 * - generic transfer routines 76 * - BBB 77 * - CBI 78 * - CBI_I (in addition to functions from CBI) 79 * - CAM (Common Access Method) 80 * - SCSI 81 * - UFI 82 * - 8070i (ATAPI) 83 * 84 * The protocols are implemented using a state machine, for the transfers as 85 * well as for the resets. The state machine is contained in umass_t_*_callback. 86 * The state machine is started through either umass_command_start() or 87 * umass_reset(). 88 * 89 * The reason for doing this is a) CAM performs a lot better this way and b) it 90 * avoids using tsleep from interrupt context (for example after a failed 91 * transfer). 92 */ 93 94 /* 95 * The SCSI related part of this driver has been derived from the 96 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org). 97 * 98 * The CAM layer uses so called actions which are messages sent to the host 99 * adapter for completion. The actions come in through umass_cam_action. The 100 * appropriate block of routines is called depending on the transport protocol 101 * in use. When the transfer has finished, these routines call 102 * umass_cam_cb again to complete the CAM command. 103 */ 104 105 #include <sys/stdint.h> 106 #include <sys/stddef.h> 107 #include <sys/param.h> 108 #include <sys/queue.h> 109 #include <sys/types.h> 110 #include <sys/systm.h> 111 #include <sys/kernel.h> 112 #include <sys/bus.h> 113 #include <sys/module.h> 114 #include <sys/lock.h> 115 #include <sys/mutex.h> 116 #include <sys/condvar.h> 117 #include <sys/sysctl.h> 118 #include <sys/sx.h> 119 #include <sys/unistd.h> 120 #include <sys/callout.h> 121 #include <sys/malloc.h> 122 #include <sys/priv.h> 123 124 #include <dev/usb/usb.h> 125 #include <dev/usb/usbdi.h> 126 #include <dev/usb/usbdi_util.h> 127 #include "usbdevs.h" 128 129 #include <dev/usb/quirk/usb_quirk.h> 130 131 #include <cam/cam.h> 132 #include <cam/cam_ccb.h> 133 #include <cam/cam_sim.h> 134 #include <cam/cam_xpt_sim.h> 135 #include <cam/scsi/scsi_all.h> 136 #include <cam/scsi/scsi_da.h> 137 138 #include <cam/cam_periph.h> 139 140 #define UMASS_EXT_BUFFER 141 #ifdef UMASS_EXT_BUFFER 142 /* this enables loading of virtual buffers into DMA */ 143 #define UMASS_USB_FLAGS .ext_buffer=1, 144 #else 145 #define UMASS_USB_FLAGS 146 #endif 147 148 #ifdef USB_DEBUG 149 #define DIF(m, x) \ 150 do { \ 151 if (umass_debug & (m)) { x ; } \ 152 } while (0) 153 154 #define DPRINTF(sc, m, fmt, ...) \ 155 do { \ 156 if (umass_debug & (m)) { \ 157 printf("%s:%s: " fmt, \ 158 (sc) ? (const char *)(sc)->sc_name : \ 159 (const char *)"umassX", \ 160 __FUNCTION__ ,## __VA_ARGS__); \ 161 } \ 162 } while (0) 163 164 #define UDMASS_GEN 0x00010000 /* general */ 165 #define UDMASS_SCSI 0x00020000 /* scsi */ 166 #define UDMASS_UFI 0x00040000 /* ufi command set */ 167 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */ 168 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI) 169 #define UDMASS_USB 0x00100000 /* USB general */ 170 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */ 171 #define UDMASS_CBI 0x00400000 /* CBI transfers */ 172 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI) 173 #define UDMASS_ALL 0xffff0000 /* all of the above */ 174 static int umass_debug = 0; 175 176 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass"); 177 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW, 178 &umass_debug, 0, "umass debug level"); 179 180 TUNABLE_INT("hw.usb.umass.debug", &umass_debug); 181 #else 182 #define DIF(...) do { } while (0) 183 #define DPRINTF(...) do { } while (0) 184 #endif 185 186 #define UMASS_GONE ((struct umass_softc *)1) 187 188 #define UMASS_BULK_SIZE (1 << 17) 189 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */ 190 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */ 191 192 /* USB transfer definitions */ 193 194 #define UMASS_T_BBB_RESET1 0 /* Bulk-Only */ 195 #define UMASS_T_BBB_RESET2 1 196 #define UMASS_T_BBB_RESET3 2 197 #define UMASS_T_BBB_COMMAND 3 198 #define UMASS_T_BBB_DATA_READ 4 199 #define UMASS_T_BBB_DATA_RD_CS 5 200 #define UMASS_T_BBB_DATA_WRITE 6 201 #define UMASS_T_BBB_DATA_WR_CS 7 202 #define UMASS_T_BBB_STATUS 8 203 #define UMASS_T_BBB_MAX 9 204 205 #define UMASS_T_CBI_RESET1 0 /* CBI */ 206 #define UMASS_T_CBI_RESET2 1 207 #define UMASS_T_CBI_RESET3 2 208 #define UMASS_T_CBI_COMMAND 3 209 #define UMASS_T_CBI_DATA_READ 4 210 #define UMASS_T_CBI_DATA_RD_CS 5 211 #define UMASS_T_CBI_DATA_WRITE 6 212 #define UMASS_T_CBI_DATA_WR_CS 7 213 #define UMASS_T_CBI_STATUS 8 214 #define UMASS_T_CBI_RESET4 9 215 #define UMASS_T_CBI_MAX 10 216 217 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX) 218 219 /* Generic definitions */ 220 221 /* Direction for transfer */ 222 #define DIR_NONE 0 223 #define DIR_IN 1 224 #define DIR_OUT 2 225 226 /* device name */ 227 #define DEVNAME "umass" 228 #define DEVNAME_SIM "umass-sim" 229 230 /* Approximate maximum transfer speeds (assumes 33% overhead). */ 231 #define UMASS_FULL_TRANSFER_SPEED 1000 232 #define UMASS_HIGH_TRANSFER_SPEED 40000 233 #define UMASS_SUPER_TRANSFER_SPEED 400000 234 #define UMASS_FLOPPY_TRANSFER_SPEED 20 235 236 #define UMASS_TIMEOUT 5000 /* ms */ 237 238 /* CAM specific definitions */ 239 240 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */ 241 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX 242 243 /* Bulk-Only features */ 244 245 #define UR_BBB_RESET 0xff /* Bulk-Only reset */ 246 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */ 247 248 /* Command Block Wrapper */ 249 typedef struct { 250 uDWord dCBWSignature; 251 #define CBWSIGNATURE 0x43425355 252 uDWord dCBWTag; 253 uDWord dCBWDataTransferLength; 254 uByte bCBWFlags; 255 #define CBWFLAGS_OUT 0x00 256 #define CBWFLAGS_IN 0x80 257 uByte bCBWLUN; 258 uByte bCDBLength; 259 #define CBWCDBLENGTH 16 260 uByte CBWCDB[CBWCDBLENGTH]; 261 } __packed umass_bbb_cbw_t; 262 263 #define UMASS_BBB_CBW_SIZE 31 264 265 /* Command Status Wrapper */ 266 typedef struct { 267 uDWord dCSWSignature; 268 #define CSWSIGNATURE 0x53425355 269 #define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355 270 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355 271 uDWord dCSWTag; 272 uDWord dCSWDataResidue; 273 uByte bCSWStatus; 274 #define CSWSTATUS_GOOD 0x0 275 #define CSWSTATUS_FAILED 0x1 276 #define CSWSTATUS_PHASE 0x2 277 } __packed umass_bbb_csw_t; 278 279 #define UMASS_BBB_CSW_SIZE 13 280 281 /* CBI features */ 282 283 #define UR_CBI_ADSC 0x00 284 285 typedef union { 286 struct { 287 uint8_t type; 288 #define IDB_TYPE_CCI 0x00 289 uint8_t value; 290 #define IDB_VALUE_PASS 0x00 291 #define IDB_VALUE_FAIL 0x01 292 #define IDB_VALUE_PHASE 0x02 293 #define IDB_VALUE_PERSISTENT 0x03 294 #define IDB_VALUE_STATUS_MASK 0x03 295 } __packed common; 296 297 struct { 298 uint8_t asc; 299 uint8_t ascq; 300 } __packed ufi; 301 } __packed umass_cbi_sbl_t; 302 303 struct umass_softc; /* see below */ 304 305 typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb, 306 uint32_t residue, uint8_t status); 307 308 #define STATUS_CMD_OK 0 /* everything ok */ 309 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */ 310 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */ 311 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */ 312 313 typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr, 314 uint8_t cmd_len); 315 316 /* Wire and command protocol */ 317 #define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */ 318 #define UMASS_PROTO_CBI 0x0002 319 #define UMASS_PROTO_CBI_I 0x0004 320 #define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */ 321 #define UMASS_PROTO_SCSI 0x0100 /* command protocol */ 322 #define UMASS_PROTO_ATAPI 0x0200 323 #define UMASS_PROTO_UFI 0x0400 324 #define UMASS_PROTO_RBC 0x0800 325 #define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */ 326 327 /* Device specific quirks */ 328 #define NO_QUIRKS 0x0000 329 /* 330 * The drive does not support Test Unit Ready. Convert to Start Unit 331 */ 332 #define NO_TEST_UNIT_READY 0x0001 333 /* 334 * The drive does not reset the Unit Attention state after REQUEST 335 * SENSE has been sent. The INQUIRY command does not reset the UA 336 * either, and so CAM runs in circles trying to retrieve the initial 337 * INQUIRY data. 338 */ 339 #define RS_NO_CLEAR_UA 0x0002 340 /* The drive does not support START STOP. */ 341 #define NO_START_STOP 0x0004 342 /* Don't ask for full inquiry data (255b). */ 343 #define FORCE_SHORT_INQUIRY 0x0008 344 /* Needs to be initialised the Shuttle way */ 345 #define SHUTTLE_INIT 0x0010 346 /* Drive needs to be switched to alternate iface 1 */ 347 #define ALT_IFACE_1 0x0020 348 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */ 349 #define FLOPPY_SPEED 0x0040 350 /* The device can't count and gets the residue of transfers wrong */ 351 #define IGNORE_RESIDUE 0x0080 352 /* No GetMaxLun call */ 353 #define NO_GETMAXLUN 0x0100 354 /* The device uses a weird CSWSIGNATURE. */ 355 #define WRONG_CSWSIG 0x0200 356 /* Device cannot handle INQUIRY so fake a generic response */ 357 #define NO_INQUIRY 0x0400 358 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */ 359 #define NO_INQUIRY_EVPD 0x0800 360 /* Pad all RBC requests to 12 bytes. */ 361 #define RBC_PAD_TO_12 0x1000 362 /* 363 * Device reports number of sectors from READ_CAPACITY, not max 364 * sector number. 365 */ 366 #define READ_CAPACITY_OFFBY1 0x2000 367 /* 368 * Device cannot handle a SCSI synchronize cache command. Normally 369 * this quirk would be handled in the cam layer, but for IDE bridges 370 * we need to associate the quirk with the bridge and not the 371 * underlying disk device. This is handled by faking a success 372 * result. 373 */ 374 #define NO_SYNCHRONIZE_CACHE 0x4000 375 376 struct umass_softc { 377 378 struct scsi_sense cam_scsi_sense; 379 struct scsi_test_unit_ready cam_scsi_test_unit_ready; 380 struct mtx sc_mtx; 381 struct { 382 uint8_t *data_ptr; 383 union ccb *ccb; 384 umass_callback_t *callback; 385 386 uint32_t data_len; /* bytes */ 387 uint32_t data_rem; /* bytes */ 388 uint32_t data_timeout; /* ms */ 389 uint32_t actlen; /* bytes */ 390 391 uint8_t cmd_data[UMASS_MAX_CMDLEN]; 392 uint8_t cmd_len; /* bytes */ 393 uint8_t dir; 394 uint8_t lun; 395 } sc_transfer; 396 397 /* Bulk specific variables for transfers in progress */ 398 umass_bbb_cbw_t cbw; /* command block wrapper */ 399 umass_bbb_csw_t csw; /* command status wrapper */ 400 401 /* CBI specific variables for transfers in progress */ 402 umass_cbi_sbl_t sbl; /* status block */ 403 404 device_t sc_dev; 405 struct usb_device *sc_udev; 406 struct cam_sim *sc_sim; /* SCSI Interface Module */ 407 struct usb_xfer *sc_xfer[UMASS_T_MAX]; 408 409 /* 410 * The command transform function is used to convert the SCSI 411 * commands into their derivatives, like UFI, ATAPI, and friends. 412 */ 413 umass_transform_t *sc_transform; 414 415 uint32_t sc_unit; 416 uint32_t sc_quirks; /* they got it almost right */ 417 uint32_t sc_proto; /* wire and cmd protocol */ 418 419 uint8_t sc_name[16]; 420 uint8_t sc_iface_no; /* interface number */ 421 uint8_t sc_maxlun; /* maximum LUN number, inclusive */ 422 uint8_t sc_last_xfer_index; 423 uint8_t sc_status_try; 424 }; 425 426 struct umass_probe_proto { 427 uint32_t quirks; 428 uint32_t proto; 429 430 int error; 431 }; 432 433 /* prototypes */ 434 435 static device_probe_t umass_probe; 436 static device_attach_t umass_attach; 437 static device_detach_t umass_detach; 438 439 static usb_callback_t umass_tr_error; 440 static usb_callback_t umass_t_bbb_reset1_callback; 441 static usb_callback_t umass_t_bbb_reset2_callback; 442 static usb_callback_t umass_t_bbb_reset3_callback; 443 static usb_callback_t umass_t_bbb_command_callback; 444 static usb_callback_t umass_t_bbb_data_read_callback; 445 static usb_callback_t umass_t_bbb_data_rd_cs_callback; 446 static usb_callback_t umass_t_bbb_data_write_callback; 447 static usb_callback_t umass_t_bbb_data_wr_cs_callback; 448 static usb_callback_t umass_t_bbb_status_callback; 449 static usb_callback_t umass_t_cbi_reset1_callback; 450 static usb_callback_t umass_t_cbi_reset2_callback; 451 static usb_callback_t umass_t_cbi_reset3_callback; 452 static usb_callback_t umass_t_cbi_reset4_callback; 453 static usb_callback_t umass_t_cbi_command_callback; 454 static usb_callback_t umass_t_cbi_data_read_callback; 455 static usb_callback_t umass_t_cbi_data_rd_cs_callback; 456 static usb_callback_t umass_t_cbi_data_write_callback; 457 static usb_callback_t umass_t_cbi_data_wr_cs_callback; 458 static usb_callback_t umass_t_cbi_status_callback; 459 460 static void umass_cancel_ccb(struct umass_softc *); 461 static void umass_init_shuttle(struct umass_softc *); 462 static void umass_reset(struct umass_softc *); 463 static void umass_t_bbb_data_clear_stall_callback(struct usb_xfer *, 464 uint8_t, uint8_t, usb_error_t); 465 static void umass_command_start(struct umass_softc *, uint8_t, void *, 466 uint32_t, uint32_t, umass_callback_t *, union ccb *); 467 static uint8_t umass_bbb_get_max_lun(struct umass_softc *); 468 static void umass_cbi_start_status(struct umass_softc *); 469 static void umass_t_cbi_data_clear_stall_callback(struct usb_xfer *, 470 uint8_t, uint8_t, usb_error_t); 471 static int umass_cam_attach_sim(struct umass_softc *); 472 static void umass_cam_attach(struct umass_softc *); 473 static void umass_cam_detach_sim(struct umass_softc *); 474 static void umass_cam_action(struct cam_sim *, union ccb *); 475 static void umass_cam_poll(struct cam_sim *); 476 static void umass_cam_cb(struct umass_softc *, union ccb *, uint32_t, 477 uint8_t); 478 static void umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t, 479 uint8_t); 480 static void umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t, 481 uint8_t); 482 static uint8_t umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t); 483 static uint8_t umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t); 484 static uint8_t umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t); 485 static uint8_t umass_atapi_transform(struct umass_softc *, uint8_t *, 486 uint8_t); 487 static uint8_t umass_no_transform(struct umass_softc *, uint8_t *, uint8_t); 488 static uint8_t umass_std_transform(struct umass_softc *, union ccb *, uint8_t 489 *, uint8_t); 490 491 #ifdef USB_DEBUG 492 static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *); 493 static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *); 494 static void umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t); 495 static void umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t, 496 uint32_t); 497 #endif 498 499 static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = { 500 501 [UMASS_T_BBB_RESET1] = { 502 .type = UE_CONTROL, 503 .endpoint = 0x00, /* Control pipe */ 504 .direction = UE_DIR_ANY, 505 .bufsize = sizeof(struct usb_device_request), 506 .callback = &umass_t_bbb_reset1_callback, 507 .timeout = 5000, /* 5 seconds */ 508 .interval = 500, /* 500 milliseconds */ 509 }, 510 511 [UMASS_T_BBB_RESET2] = { 512 .type = UE_CONTROL, 513 .endpoint = 0x00, /* Control pipe */ 514 .direction = UE_DIR_ANY, 515 .bufsize = sizeof(struct usb_device_request), 516 .callback = &umass_t_bbb_reset2_callback, 517 .timeout = 5000, /* 5 seconds */ 518 .interval = 50, /* 50 milliseconds */ 519 }, 520 521 [UMASS_T_BBB_RESET3] = { 522 .type = UE_CONTROL, 523 .endpoint = 0x00, /* Control pipe */ 524 .direction = UE_DIR_ANY, 525 .bufsize = sizeof(struct usb_device_request), 526 .callback = &umass_t_bbb_reset3_callback, 527 .timeout = 5000, /* 5 seconds */ 528 .interval = 50, /* 50 milliseconds */ 529 }, 530 531 [UMASS_T_BBB_COMMAND] = { 532 .type = UE_BULK, 533 .endpoint = UE_ADDR_ANY, 534 .direction = UE_DIR_OUT, 535 .bufsize = sizeof(umass_bbb_cbw_t), 536 .callback = &umass_t_bbb_command_callback, 537 .timeout = 5000, /* 5 seconds */ 538 }, 539 540 [UMASS_T_BBB_DATA_READ] = { 541 .type = UE_BULK, 542 .endpoint = UE_ADDR_ANY, 543 .direction = UE_DIR_IN, 544 .bufsize = UMASS_BULK_SIZE, 545 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS}, 546 .callback = &umass_t_bbb_data_read_callback, 547 .timeout = 0, /* overwritten later */ 548 }, 549 550 [UMASS_T_BBB_DATA_RD_CS] = { 551 .type = UE_CONTROL, 552 .endpoint = 0x00, /* Control pipe */ 553 .direction = UE_DIR_ANY, 554 .bufsize = sizeof(struct usb_device_request), 555 .callback = &umass_t_bbb_data_rd_cs_callback, 556 .timeout = 5000, /* 5 seconds */ 557 }, 558 559 [UMASS_T_BBB_DATA_WRITE] = { 560 .type = UE_BULK, 561 .endpoint = UE_ADDR_ANY, 562 .direction = UE_DIR_OUT, 563 .bufsize = UMASS_BULK_SIZE, 564 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS}, 565 .callback = &umass_t_bbb_data_write_callback, 566 .timeout = 0, /* overwritten later */ 567 }, 568 569 [UMASS_T_BBB_DATA_WR_CS] = { 570 .type = UE_CONTROL, 571 .endpoint = 0x00, /* Control pipe */ 572 .direction = UE_DIR_ANY, 573 .bufsize = sizeof(struct usb_device_request), 574 .callback = &umass_t_bbb_data_wr_cs_callback, 575 .timeout = 5000, /* 5 seconds */ 576 }, 577 578 [UMASS_T_BBB_STATUS] = { 579 .type = UE_BULK, 580 .endpoint = UE_ADDR_ANY, 581 .direction = UE_DIR_IN, 582 .bufsize = sizeof(umass_bbb_csw_t), 583 .flags = {.short_xfer_ok = 1,}, 584 .callback = &umass_t_bbb_status_callback, 585 .timeout = 5000, /* ms */ 586 }, 587 }; 588 589 static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = { 590 591 [UMASS_T_CBI_RESET1] = { 592 .type = UE_CONTROL, 593 .endpoint = 0x00, /* Control pipe */ 594 .direction = UE_DIR_ANY, 595 .bufsize = (sizeof(struct usb_device_request) + 596 UMASS_CBI_DIAGNOSTIC_CMDLEN), 597 .callback = &umass_t_cbi_reset1_callback, 598 .timeout = 5000, /* 5 seconds */ 599 .interval = 500, /* 500 milliseconds */ 600 }, 601 602 [UMASS_T_CBI_RESET2] = { 603 .type = UE_CONTROL, 604 .endpoint = 0x00, /* Control pipe */ 605 .direction = UE_DIR_ANY, 606 .bufsize = sizeof(struct usb_device_request), 607 .callback = &umass_t_cbi_reset2_callback, 608 .timeout = 5000, /* 5 seconds */ 609 .interval = 50, /* 50 milliseconds */ 610 }, 611 612 [UMASS_T_CBI_RESET3] = { 613 .type = UE_CONTROL, 614 .endpoint = 0x00, /* Control pipe */ 615 .direction = UE_DIR_ANY, 616 .bufsize = sizeof(struct usb_device_request), 617 .callback = &umass_t_cbi_reset3_callback, 618 .timeout = 5000, /* 5 seconds */ 619 .interval = 50, /* 50 milliseconds */ 620 }, 621 622 [UMASS_T_CBI_COMMAND] = { 623 .type = UE_CONTROL, 624 .endpoint = 0x00, /* Control pipe */ 625 .direction = UE_DIR_ANY, 626 .bufsize = (sizeof(struct usb_device_request) + 627 UMASS_MAX_CMDLEN), 628 .callback = &umass_t_cbi_command_callback, 629 .timeout = 5000, /* 5 seconds */ 630 }, 631 632 [UMASS_T_CBI_DATA_READ] = { 633 .type = UE_BULK, 634 .endpoint = UE_ADDR_ANY, 635 .direction = UE_DIR_IN, 636 .bufsize = UMASS_BULK_SIZE, 637 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS}, 638 .callback = &umass_t_cbi_data_read_callback, 639 .timeout = 0, /* overwritten later */ 640 }, 641 642 [UMASS_T_CBI_DATA_RD_CS] = { 643 .type = UE_CONTROL, 644 .endpoint = 0x00, /* Control pipe */ 645 .direction = UE_DIR_ANY, 646 .bufsize = sizeof(struct usb_device_request), 647 .callback = &umass_t_cbi_data_rd_cs_callback, 648 .timeout = 5000, /* 5 seconds */ 649 }, 650 651 [UMASS_T_CBI_DATA_WRITE] = { 652 .type = UE_BULK, 653 .endpoint = UE_ADDR_ANY, 654 .direction = UE_DIR_OUT, 655 .bufsize = UMASS_BULK_SIZE, 656 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS}, 657 .callback = &umass_t_cbi_data_write_callback, 658 .timeout = 0, /* overwritten later */ 659 }, 660 661 [UMASS_T_CBI_DATA_WR_CS] = { 662 .type = UE_CONTROL, 663 .endpoint = 0x00, /* Control pipe */ 664 .direction = UE_DIR_ANY, 665 .bufsize = sizeof(struct usb_device_request), 666 .callback = &umass_t_cbi_data_wr_cs_callback, 667 .timeout = 5000, /* 5 seconds */ 668 }, 669 670 [UMASS_T_CBI_STATUS] = { 671 .type = UE_INTERRUPT, 672 .endpoint = UE_ADDR_ANY, 673 .direction = UE_DIR_IN, 674 .flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,}, 675 .bufsize = sizeof(umass_cbi_sbl_t), 676 .callback = &umass_t_cbi_status_callback, 677 .timeout = 5000, /* ms */ 678 }, 679 680 [UMASS_T_CBI_RESET4] = { 681 .type = UE_CONTROL, 682 .endpoint = 0x00, /* Control pipe */ 683 .direction = UE_DIR_ANY, 684 .bufsize = sizeof(struct usb_device_request), 685 .callback = &umass_t_cbi_reset4_callback, 686 .timeout = 5000, /* ms */ 687 }, 688 }; 689 690 /* If device cannot return valid inquiry data, fake it */ 691 static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = { 692 0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2, 693 /* additional_length */ 31, 0, 0, 0 694 }; 695 696 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */ 697 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */ 698 699 static devclass_t umass_devclass; 700 701 static device_method_t umass_methods[] = { 702 /* Device interface */ 703 DEVMETHOD(device_probe, umass_probe), 704 DEVMETHOD(device_attach, umass_attach), 705 DEVMETHOD(device_detach, umass_detach), 706 {0, 0} 707 }; 708 709 static driver_t umass_driver = { 710 .name = "umass", 711 .methods = umass_methods, 712 .size = sizeof(struct umass_softc), 713 }; 714 715 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0); 716 MODULE_DEPEND(umass, usb, 1, 1, 1); 717 MODULE_DEPEND(umass, cam, 1, 1, 1); 718 MODULE_VERSION(umass, 1); 719 720 /* 721 * USB device probe/attach/detach 722 */ 723 724 static uint16_t 725 umass_get_proto(struct usb_interface *iface) 726 { 727 struct usb_interface_descriptor *id; 728 uint16_t retval; 729 730 retval = 0; 731 732 /* Check for a standards compliant device */ 733 id = usbd_get_interface_descriptor(iface); 734 if ((id == NULL) || 735 (id->bInterfaceClass != UICLASS_MASS)) { 736 goto done; 737 } 738 switch (id->bInterfaceSubClass) { 739 case UISUBCLASS_SCSI: 740 retval |= UMASS_PROTO_SCSI; 741 break; 742 case UISUBCLASS_UFI: 743 retval |= UMASS_PROTO_UFI; 744 break; 745 case UISUBCLASS_RBC: 746 retval |= UMASS_PROTO_RBC; 747 break; 748 case UISUBCLASS_SFF8020I: 749 case UISUBCLASS_SFF8070I: 750 retval |= UMASS_PROTO_ATAPI; 751 break; 752 default: 753 goto done; 754 } 755 756 switch (id->bInterfaceProtocol) { 757 case UIPROTO_MASS_CBI: 758 retval |= UMASS_PROTO_CBI; 759 break; 760 case UIPROTO_MASS_CBI_I: 761 retval |= UMASS_PROTO_CBI_I; 762 break; 763 case UIPROTO_MASS_BBB_OLD: 764 case UIPROTO_MASS_BBB: 765 retval |= UMASS_PROTO_BBB; 766 break; 767 default: 768 goto done; 769 } 770 done: 771 return (retval); 772 } 773 774 /* 775 * Match the device we are seeing with the devices supported. 776 */ 777 static struct umass_probe_proto 778 umass_probe_proto(device_t dev, struct usb_attach_arg *uaa) 779 { 780 struct umass_probe_proto ret; 781 uint32_t quirks = NO_QUIRKS; 782 uint32_t proto = umass_get_proto(uaa->iface); 783 784 memset(&ret, 0, sizeof(ret)); 785 ret.error = BUS_PROBE_GENERIC; 786 787 /* Search for protocol enforcement */ 788 789 if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) { 790 proto &= ~UMASS_PROTO_WIRE; 791 proto |= UMASS_PROTO_BBB; 792 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) { 793 proto &= ~UMASS_PROTO_WIRE; 794 proto |= UMASS_PROTO_CBI; 795 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) { 796 proto &= ~UMASS_PROTO_WIRE; 797 proto |= UMASS_PROTO_CBI_I; 798 } 799 800 if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) { 801 proto &= ~UMASS_PROTO_COMMAND; 802 proto |= UMASS_PROTO_SCSI; 803 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) { 804 proto &= ~UMASS_PROTO_COMMAND; 805 proto |= UMASS_PROTO_ATAPI; 806 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) { 807 proto &= ~UMASS_PROTO_COMMAND; 808 proto |= UMASS_PROTO_UFI; 809 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) { 810 proto &= ~UMASS_PROTO_COMMAND; 811 proto |= UMASS_PROTO_RBC; 812 } 813 814 /* Check if the protocol is invalid */ 815 816 if ((proto & UMASS_PROTO_COMMAND) == 0) { 817 ret.error = ENXIO; 818 goto done; 819 } 820 821 if ((proto & UMASS_PROTO_WIRE) == 0) { 822 ret.error = ENXIO; 823 goto done; 824 } 825 826 /* Search for quirks */ 827 828 if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY)) 829 quirks |= NO_TEST_UNIT_READY; 830 if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA)) 831 quirks |= RS_NO_CLEAR_UA; 832 if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP)) 833 quirks |= NO_START_STOP; 834 if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN)) 835 quirks |= NO_GETMAXLUN; 836 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY)) 837 quirks |= NO_INQUIRY; 838 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD)) 839 quirks |= NO_INQUIRY_EVPD; 840 if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE)) 841 quirks |= NO_SYNCHRONIZE_CACHE; 842 if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT)) 843 quirks |= SHUTTLE_INIT; 844 if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1)) 845 quirks |= ALT_IFACE_1; 846 if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED)) 847 quirks |= FLOPPY_SPEED; 848 if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE)) 849 quirks |= IGNORE_RESIDUE; 850 if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG)) 851 quirks |= WRONG_CSWSIG; 852 if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12)) 853 quirks |= RBC_PAD_TO_12; 854 if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1)) 855 quirks |= READ_CAPACITY_OFFBY1; 856 if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ)) 857 quirks |= FORCE_SHORT_INQUIRY; 858 859 done: 860 ret.quirks = quirks; 861 ret.proto = proto; 862 return (ret); 863 } 864 865 static int 866 umass_probe(device_t dev) 867 { 868 struct usb_attach_arg *uaa = device_get_ivars(dev); 869 struct umass_probe_proto temp; 870 871 if (uaa->usb_mode != USB_MODE_HOST) { 872 return (ENXIO); 873 } 874 temp = umass_probe_proto(dev, uaa); 875 876 return (temp.error); 877 } 878 879 static int 880 umass_attach(device_t dev) 881 { 882 struct umass_softc *sc = device_get_softc(dev); 883 struct usb_attach_arg *uaa = device_get_ivars(dev); 884 struct umass_probe_proto temp = umass_probe_proto(dev, uaa); 885 struct usb_interface_descriptor *id; 886 int32_t err; 887 888 /* 889 * NOTE: the softc struct is bzero-ed in device_set_driver. 890 * We can safely call umass_detach without specifically 891 * initializing the struct. 892 */ 893 894 sc->sc_dev = dev; 895 sc->sc_udev = uaa->device; 896 sc->sc_proto = temp.proto; 897 sc->sc_quirks = temp.quirks; 898 sc->sc_unit = device_get_unit(dev); 899 900 snprintf(sc->sc_name, sizeof(sc->sc_name), 901 "%s", device_get_nameunit(dev)); 902 903 device_set_usb_desc(dev); 904 905 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), 906 NULL, MTX_DEF | MTX_RECURSE); 907 908 /* get interface index */ 909 910 id = usbd_get_interface_descriptor(uaa->iface); 911 if (id == NULL) { 912 device_printf(dev, "failed to get " 913 "interface number\n"); 914 goto detach; 915 } 916 sc->sc_iface_no = id->bInterfaceNumber; 917 918 #ifdef USB_DEBUG 919 device_printf(dev, " "); 920 921 switch (sc->sc_proto & UMASS_PROTO_COMMAND) { 922 case UMASS_PROTO_SCSI: 923 printf("SCSI"); 924 break; 925 case UMASS_PROTO_ATAPI: 926 printf("8070i (ATAPI)"); 927 break; 928 case UMASS_PROTO_UFI: 929 printf("UFI"); 930 break; 931 case UMASS_PROTO_RBC: 932 printf("RBC"); 933 break; 934 default: 935 printf("(unknown 0x%02x)", 936 sc->sc_proto & UMASS_PROTO_COMMAND); 937 break; 938 } 939 940 printf(" over "); 941 942 switch (sc->sc_proto & UMASS_PROTO_WIRE) { 943 case UMASS_PROTO_BBB: 944 printf("Bulk-Only"); 945 break; 946 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */ 947 printf("CBI"); 948 break; 949 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */ 950 printf("CBI with CCI"); 951 break; 952 default: 953 printf("(unknown 0x%02x)", 954 sc->sc_proto & UMASS_PROTO_WIRE); 955 } 956 957 printf("; quirks = 0x%04x\n", sc->sc_quirks); 958 #endif 959 960 if (sc->sc_quirks & ALT_IFACE_1) { 961 err = usbd_set_alt_interface_index 962 (uaa->device, uaa->info.bIfaceIndex, 1); 963 964 if (err) { 965 DPRINTF(sc, UDMASS_USB, "could not switch to " 966 "Alt Interface 1\n"); 967 goto detach; 968 } 969 } 970 /* allocate all required USB transfers */ 971 972 if (sc->sc_proto & UMASS_PROTO_BBB) { 973 974 err = usbd_transfer_setup(uaa->device, 975 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config, 976 UMASS_T_BBB_MAX, sc, &sc->sc_mtx); 977 978 /* skip reset first time */ 979 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND; 980 981 } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) { 982 983 err = usbd_transfer_setup(uaa->device, 984 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config, 985 UMASS_T_CBI_MAX, sc, &sc->sc_mtx); 986 987 /* skip reset first time */ 988 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND; 989 990 } else { 991 err = USB_ERR_INVAL; 992 } 993 994 if (err) { 995 device_printf(dev, "could not setup required " 996 "transfers, %s\n", usbd_errstr(err)); 997 goto detach; 998 } 999 sc->sc_transform = 1000 (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform : 1001 (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform : 1002 (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform : 1003 (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform : 1004 &umass_no_transform; 1005 1006 /* from here onwards the device can be used. */ 1007 1008 if (sc->sc_quirks & SHUTTLE_INIT) { 1009 umass_init_shuttle(sc); 1010 } 1011 /* get the maximum LUN supported by the device */ 1012 1013 if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) && 1014 !(sc->sc_quirks & NO_GETMAXLUN)) 1015 sc->sc_maxlun = umass_bbb_get_max_lun(sc); 1016 else 1017 sc->sc_maxlun = 0; 1018 1019 /* Prepare the SCSI command block */ 1020 sc->cam_scsi_sense.opcode = REQUEST_SENSE; 1021 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY; 1022 1023 /* 1024 * some devices need a delay after that the configuration value is 1025 * set to function properly: 1026 */ 1027 usb_pause_mtx(NULL, hz); 1028 1029 /* register the SIM */ 1030 err = umass_cam_attach_sim(sc); 1031 if (err) { 1032 goto detach; 1033 } 1034 /* scan the SIM */ 1035 umass_cam_attach(sc); 1036 1037 DPRINTF(sc, UDMASS_GEN, "Attach finished\n"); 1038 1039 return (0); /* success */ 1040 1041 detach: 1042 umass_detach(dev); 1043 return (ENXIO); /* failure */ 1044 } 1045 1046 static int 1047 umass_detach(device_t dev) 1048 { 1049 struct umass_softc *sc = device_get_softc(dev); 1050 1051 DPRINTF(sc, UDMASS_USB, "\n"); 1052 1053 /* teardown our statemachine */ 1054 1055 usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX); 1056 1057 #if (__FreeBSD_version >= 700037) 1058 mtx_lock(&sc->sc_mtx); 1059 #endif 1060 umass_cam_detach_sim(sc); 1061 1062 #if (__FreeBSD_version >= 700037) 1063 mtx_unlock(&sc->sc_mtx); 1064 #endif 1065 mtx_destroy(&sc->sc_mtx); 1066 1067 return (0); /* success */ 1068 } 1069 1070 static void 1071 umass_init_shuttle(struct umass_softc *sc) 1072 { 1073 struct usb_device_request req; 1074 usb_error_t err; 1075 uint8_t status[2] = {0, 0}; 1076 1077 /* 1078 * The Linux driver does this, but no one can tell us what the 1079 * command does. 1080 */ 1081 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1082 req.bRequest = 1; /* XXX unknown command */ 1083 USETW(req.wValue, 0); 1084 req.wIndex[0] = sc->sc_iface_no; 1085 req.wIndex[1] = 0; 1086 USETW(req.wLength, sizeof(status)); 1087 err = usbd_do_request(sc->sc_udev, NULL, &req, &status); 1088 1089 DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n", 1090 status[0], status[1]); 1091 } 1092 1093 /* 1094 * Generic functions to handle transfers 1095 */ 1096 1097 static void 1098 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index) 1099 { 1100 DPRINTF(sc, UDMASS_GEN, "transfer index = " 1101 "%d\n", xfer_index); 1102 1103 if (sc->sc_xfer[xfer_index]) { 1104 sc->sc_last_xfer_index = xfer_index; 1105 usbd_transfer_start(sc->sc_xfer[xfer_index]); 1106 } else { 1107 umass_cancel_ccb(sc); 1108 } 1109 } 1110 1111 static void 1112 umass_reset(struct umass_softc *sc) 1113 { 1114 DPRINTF(sc, UDMASS_GEN, "resetting device\n"); 1115 1116 /* 1117 * stop the last transfer, if not already stopped: 1118 */ 1119 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]); 1120 umass_transfer_start(sc, 0); 1121 } 1122 1123 static void 1124 umass_cancel_ccb(struct umass_softc *sc) 1125 { 1126 union ccb *ccb; 1127 1128 mtx_assert(&sc->sc_mtx, MA_OWNED); 1129 1130 ccb = sc->sc_transfer.ccb; 1131 sc->sc_transfer.ccb = NULL; 1132 sc->sc_last_xfer_index = 0; 1133 1134 if (ccb) { 1135 (sc->sc_transfer.callback) 1136 (sc, ccb, (sc->sc_transfer.data_len - 1137 sc->sc_transfer.actlen), STATUS_WIRE_FAILED); 1138 } 1139 } 1140 1141 static void 1142 umass_tr_error(struct usb_xfer *xfer, usb_error_t error) 1143 { 1144 struct umass_softc *sc = usbd_xfer_softc(xfer); 1145 1146 if (error != USB_ERR_CANCELLED) { 1147 1148 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> " 1149 "reset\n", usbd_errstr(error)); 1150 } 1151 umass_cancel_ccb(sc); 1152 } 1153 1154 /* 1155 * BBB protocol specific functions 1156 */ 1157 1158 static void 1159 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error) 1160 { 1161 struct umass_softc *sc = usbd_xfer_softc(xfer); 1162 struct usb_device_request req; 1163 struct usb_page_cache *pc; 1164 1165 switch (USB_GET_STATE(xfer)) { 1166 case USB_ST_TRANSFERRED: 1167 umass_transfer_start(sc, UMASS_T_BBB_RESET2); 1168 return; 1169 1170 case USB_ST_SETUP: 1171 /* 1172 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class) 1173 * 1174 * For Reset Recovery the host shall issue in the following order: 1175 * a) a Bulk-Only Mass Storage Reset 1176 * b) a Clear Feature HALT to the Bulk-In endpoint 1177 * c) a Clear Feature HALT to the Bulk-Out endpoint 1178 * 1179 * This is done in 3 steps, using 3 transfers: 1180 * UMASS_T_BBB_RESET1 1181 * UMASS_T_BBB_RESET2 1182 * UMASS_T_BBB_RESET3 1183 */ 1184 1185 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n"); 1186 1187 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1188 req.bRequest = UR_BBB_RESET; /* bulk only reset */ 1189 USETW(req.wValue, 0); 1190 req.wIndex[0] = sc->sc_iface_no; 1191 req.wIndex[1] = 0; 1192 USETW(req.wLength, 0); 1193 1194 pc = usbd_xfer_get_frame(xfer, 0); 1195 usbd_copy_in(pc, 0, &req, sizeof(req)); 1196 1197 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1198 usbd_xfer_set_frames(xfer, 1); 1199 usbd_transfer_submit(xfer); 1200 return; 1201 1202 default: /* Error */ 1203 umass_tr_error(xfer, error); 1204 return; 1205 1206 } 1207 } 1208 1209 static void 1210 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error) 1211 { 1212 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3, 1213 UMASS_T_BBB_DATA_READ, error); 1214 } 1215 1216 static void 1217 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error) 1218 { 1219 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND, 1220 UMASS_T_BBB_DATA_WRITE, error); 1221 } 1222 1223 static void 1224 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer, 1225 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error) 1226 { 1227 struct umass_softc *sc = usbd_xfer_softc(xfer); 1228 1229 switch (USB_GET_STATE(xfer)) { 1230 case USB_ST_TRANSFERRED: 1231 tr_transferred: 1232 umass_transfer_start(sc, next_xfer); 1233 return; 1234 1235 case USB_ST_SETUP: 1236 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) { 1237 goto tr_transferred; 1238 } 1239 return; 1240 1241 default: /* Error */ 1242 umass_tr_error(xfer, error); 1243 return; 1244 1245 } 1246 } 1247 1248 static void 1249 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error) 1250 { 1251 struct umass_softc *sc = usbd_xfer_softc(xfer); 1252 union ccb *ccb = sc->sc_transfer.ccb; 1253 struct usb_page_cache *pc; 1254 uint32_t tag; 1255 1256 switch (USB_GET_STATE(xfer)) { 1257 case USB_ST_TRANSFERRED: 1258 umass_transfer_start 1259 (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ : 1260 (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE : 1261 UMASS_T_BBB_STATUS)); 1262 return; 1263 1264 case USB_ST_SETUP: 1265 1266 sc->sc_status_try = 0; 1267 1268 if (ccb) { 1269 1270 /* 1271 * the initial value is not important, 1272 * as long as the values are unique: 1273 */ 1274 tag = UGETDW(sc->cbw.dCBWTag) + 1; 1275 1276 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE); 1277 USETDW(sc->cbw.dCBWTag, tag); 1278 1279 /* 1280 * dCBWDataTransferLength: 1281 * This field indicates the number of bytes of data that the host 1282 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by 1283 * the Direction bit) during the execution of this command. If this 1284 * field is set to 0, the device will expect that no data will be 1285 * transferred IN or OUT during this command, regardless of the value 1286 * of the Direction bit defined in dCBWFlags. 1287 */ 1288 USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len); 1289 1290 /* 1291 * dCBWFlags: 1292 * The bits of the Flags field are defined as follows: 1293 * Bits 0-6 reserved 1294 * Bit 7 Direction - this bit shall be ignored if the 1295 * dCBWDataTransferLength field is zero. 1296 * 0 = data Out from host to device 1297 * 1 = data In from device to host 1298 */ 1299 sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ? 1300 CBWFLAGS_IN : CBWFLAGS_OUT); 1301 sc->cbw.bCBWLUN = sc->sc_transfer.lun; 1302 1303 if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) { 1304 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB); 1305 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n"); 1306 } 1307 sc->cbw.bCDBLength = sc->sc_transfer.cmd_len; 1308 1309 bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB, 1310 sc->sc_transfer.cmd_len); 1311 1312 bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len, 1313 sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len); 1314 1315 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw)); 1316 1317 pc = usbd_xfer_get_frame(xfer, 0); 1318 usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw)); 1319 usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw)); 1320 1321 usbd_transfer_submit(xfer); 1322 } 1323 return; 1324 1325 default: /* Error */ 1326 umass_tr_error(xfer, error); 1327 return; 1328 1329 } 1330 } 1331 1332 static void 1333 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error) 1334 { 1335 struct umass_softc *sc = usbd_xfer_softc(xfer); 1336 uint32_t max_bulk = usbd_xfer_max_len(xfer); 1337 #ifndef UMASS_EXT_BUFFER 1338 struct usb_page_cache *pc; 1339 #endif 1340 int actlen, sumlen; 1341 1342 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1343 1344 switch (USB_GET_STATE(xfer)) { 1345 case USB_ST_TRANSFERRED: 1346 #ifndef UMASS_EXT_BUFFER 1347 pc = usbd_xfer_get_frame(xfer, 0); 1348 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen); 1349 #endif 1350 sc->sc_transfer.data_rem -= actlen; 1351 sc->sc_transfer.data_ptr += actlen; 1352 sc->sc_transfer.actlen += actlen; 1353 1354 if (actlen < sumlen) { 1355 /* short transfer */ 1356 sc->sc_transfer.data_rem = 0; 1357 } 1358 case USB_ST_SETUP: 1359 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n", 1360 max_bulk, sc->sc_transfer.data_rem); 1361 1362 if (sc->sc_transfer.data_rem == 0) { 1363 umass_transfer_start(sc, UMASS_T_BBB_STATUS); 1364 return; 1365 } 1366 if (max_bulk > sc->sc_transfer.data_rem) { 1367 max_bulk = sc->sc_transfer.data_rem; 1368 } 1369 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout); 1370 1371 #ifdef UMASS_EXT_BUFFER 1372 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr, 1373 max_bulk); 1374 #else 1375 usbd_xfer_set_frame_len(xfer, 0, max_bulk); 1376 #endif 1377 usbd_transfer_submit(xfer); 1378 return; 1379 1380 default: /* Error */ 1381 if (error == USB_ERR_CANCELLED) { 1382 umass_tr_error(xfer, error); 1383 } else { 1384 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS); 1385 } 1386 return; 1387 1388 } 1389 } 1390 1391 static void 1392 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error) 1393 { 1394 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS, 1395 UMASS_T_BBB_DATA_READ, error); 1396 } 1397 1398 static void 1399 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error) 1400 { 1401 struct umass_softc *sc = usbd_xfer_softc(xfer); 1402 uint32_t max_bulk = usbd_xfer_max_len(xfer); 1403 #ifndef UMASS_EXT_BUFFER 1404 struct usb_page_cache *pc; 1405 #endif 1406 int actlen, sumlen; 1407 1408 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1409 1410 switch (USB_GET_STATE(xfer)) { 1411 case USB_ST_TRANSFERRED: 1412 sc->sc_transfer.data_rem -= actlen; 1413 sc->sc_transfer.data_ptr += actlen; 1414 sc->sc_transfer.actlen += actlen; 1415 1416 if (actlen < sumlen) { 1417 /* short transfer */ 1418 sc->sc_transfer.data_rem = 0; 1419 } 1420 case USB_ST_SETUP: 1421 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n", 1422 max_bulk, sc->sc_transfer.data_rem); 1423 1424 if (sc->sc_transfer.data_rem == 0) { 1425 umass_transfer_start(sc, UMASS_T_BBB_STATUS); 1426 return; 1427 } 1428 if (max_bulk > sc->sc_transfer.data_rem) { 1429 max_bulk = sc->sc_transfer.data_rem; 1430 } 1431 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout); 1432 1433 #ifdef UMASS_EXT_BUFFER 1434 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr, 1435 max_bulk); 1436 #else 1437 pc = usbd_xfer_get_frame(xfer, 0); 1438 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk); 1439 usbd_xfer_set_frame_len(xfer, 0, max_bulk); 1440 #endif 1441 1442 usbd_transfer_submit(xfer); 1443 return; 1444 1445 default: /* Error */ 1446 if (error == USB_ERR_CANCELLED) { 1447 umass_tr_error(xfer, error); 1448 } else { 1449 umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS); 1450 } 1451 return; 1452 1453 } 1454 } 1455 1456 static void 1457 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error) 1458 { 1459 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS, 1460 UMASS_T_BBB_DATA_WRITE, error); 1461 } 1462 1463 static void 1464 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error) 1465 { 1466 struct umass_softc *sc = usbd_xfer_softc(xfer); 1467 union ccb *ccb = sc->sc_transfer.ccb; 1468 struct usb_page_cache *pc; 1469 uint32_t residue; 1470 int actlen; 1471 1472 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 1473 1474 switch (USB_GET_STATE(xfer)) { 1475 case USB_ST_TRANSFERRED: 1476 1477 /* 1478 * Do a full reset if there is something wrong with the CSW: 1479 */ 1480 sc->sc_status_try = 1; 1481 1482 /* Zero missing parts of the CSW: */ 1483 1484 if (actlen < sizeof(sc->csw)) { 1485 bzero(&sc->csw, sizeof(sc->csw)); 1486 } 1487 pc = usbd_xfer_get_frame(xfer, 0); 1488 usbd_copy_out(pc, 0, &sc->csw, actlen); 1489 1490 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw)); 1491 1492 residue = UGETDW(sc->csw.dCSWDataResidue); 1493 1494 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) { 1495 residue = (sc->sc_transfer.data_len - 1496 sc->sc_transfer.actlen); 1497 } 1498 if (residue > sc->sc_transfer.data_len) { 1499 DPRINTF(sc, UDMASS_BBB, "truncating residue from %d " 1500 "to %d bytes\n", residue, sc->sc_transfer.data_len); 1501 residue = sc->sc_transfer.data_len; 1502 } 1503 /* translate weird command-status signatures: */ 1504 if (sc->sc_quirks & WRONG_CSWSIG) { 1505 1506 uint32_t temp = UGETDW(sc->csw.dCSWSignature); 1507 1508 if ((temp == CSWSIGNATURE_OLYMPUS_C1) || 1509 (temp == CSWSIGNATURE_IMAGINATION_DBX1)) { 1510 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE); 1511 } 1512 } 1513 /* check CSW and handle eventual error */ 1514 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) { 1515 DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n", 1516 UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE); 1517 /* 1518 * Invalid CSW: Wrong signature or wrong tag might 1519 * indicate that we lost synchronization. Reset the 1520 * device. 1521 */ 1522 goto tr_error; 1523 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) { 1524 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be " 1525 "0x%08x\n", UGETDW(sc->csw.dCSWTag), 1526 UGETDW(sc->cbw.dCBWTag)); 1527 goto tr_error; 1528 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) { 1529 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n", 1530 sc->csw.bCSWStatus, CSWSTATUS_PHASE); 1531 goto tr_error; 1532 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) { 1533 DPRINTF(sc, UDMASS_BBB, "Phase error, residue = " 1534 "%d\n", residue); 1535 goto tr_error; 1536 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) { 1537 DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n", 1538 sc->sc_transfer.actlen, sc->sc_transfer.data_len); 1539 goto tr_error; 1540 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) { 1541 DPRINTF(sc, UDMASS_BBB, "Command failed, residue = " 1542 "%d\n", residue); 1543 1544 sc->sc_transfer.ccb = NULL; 1545 1546 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND; 1547 1548 (sc->sc_transfer.callback) 1549 (sc, ccb, residue, STATUS_CMD_FAILED); 1550 } else { 1551 sc->sc_transfer.ccb = NULL; 1552 1553 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND; 1554 1555 (sc->sc_transfer.callback) 1556 (sc, ccb, residue, STATUS_CMD_OK); 1557 } 1558 return; 1559 1560 case USB_ST_SETUP: 1561 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 1562 usbd_transfer_submit(xfer); 1563 return; 1564 1565 default: 1566 tr_error: 1567 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n", 1568 usbd_errstr(error), sc->sc_status_try); 1569 1570 if ((error == USB_ERR_CANCELLED) || 1571 (sc->sc_status_try)) { 1572 umass_tr_error(xfer, error); 1573 } else { 1574 sc->sc_status_try = 1; 1575 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS); 1576 } 1577 return; 1578 1579 } 1580 } 1581 1582 static void 1583 umass_command_start(struct umass_softc *sc, uint8_t dir, 1584 void *data_ptr, uint32_t data_len, 1585 uint32_t data_timeout, umass_callback_t *callback, 1586 union ccb *ccb) 1587 { 1588 sc->sc_transfer.lun = ccb->ccb_h.target_lun; 1589 1590 /* 1591 * NOTE: assumes that "sc->sc_transfer.cmd_data" and 1592 * "sc->sc_transfer.cmd_len" has been properly 1593 * initialized. 1594 */ 1595 1596 sc->sc_transfer.dir = data_len ? dir : DIR_NONE; 1597 sc->sc_transfer.data_ptr = data_ptr; 1598 sc->sc_transfer.data_len = data_len; 1599 sc->sc_transfer.data_rem = data_len; 1600 sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT); 1601 1602 sc->sc_transfer.actlen = 0; 1603 sc->sc_transfer.callback = callback; 1604 sc->sc_transfer.ccb = ccb; 1605 1606 if (sc->sc_xfer[sc->sc_last_xfer_index]) { 1607 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]); 1608 } else { 1609 ccb->ccb_h.status = CAM_TID_INVALID; 1610 xpt_done(ccb); 1611 } 1612 } 1613 1614 static uint8_t 1615 umass_bbb_get_max_lun(struct umass_softc *sc) 1616 { 1617 struct usb_device_request req; 1618 usb_error_t err; 1619 uint8_t buf = 0; 1620 1621 /* The Get Max Lun command is a class-specific request. */ 1622 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1623 req.bRequest = UR_BBB_GET_MAX_LUN; 1624 USETW(req.wValue, 0); 1625 req.wIndex[0] = sc->sc_iface_no; 1626 req.wIndex[1] = 0; 1627 USETW(req.wLength, 1); 1628 1629 err = usbd_do_request(sc->sc_udev, NULL, &req, &buf); 1630 if (err) { 1631 buf = 0; 1632 1633 /* Device doesn't support Get Max Lun request. */ 1634 printf("%s: Get Max Lun not supported (%s)\n", 1635 sc->sc_name, usbd_errstr(err)); 1636 } 1637 return (buf); 1638 } 1639 1640 /* 1641 * Command/Bulk/Interrupt (CBI) specific functions 1642 */ 1643 1644 static void 1645 umass_cbi_start_status(struct umass_softc *sc) 1646 { 1647 if (sc->sc_xfer[UMASS_T_CBI_STATUS]) { 1648 umass_transfer_start(sc, UMASS_T_CBI_STATUS); 1649 } else { 1650 union ccb *ccb = sc->sc_transfer.ccb; 1651 1652 sc->sc_transfer.ccb = NULL; 1653 1654 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND; 1655 1656 (sc->sc_transfer.callback) 1657 (sc, ccb, (sc->sc_transfer.data_len - 1658 sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN); 1659 } 1660 } 1661 1662 static void 1663 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error) 1664 { 1665 struct umass_softc *sc = usbd_xfer_softc(xfer); 1666 struct usb_device_request req; 1667 struct usb_page_cache *pc; 1668 uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN]; 1669 1670 uint8_t i; 1671 1672 switch (USB_GET_STATE(xfer)) { 1673 case USB_ST_TRANSFERRED: 1674 umass_transfer_start(sc, UMASS_T_CBI_RESET2); 1675 break; 1676 1677 case USB_ST_SETUP: 1678 /* 1679 * Command Block Reset Protocol 1680 * 1681 * First send a reset request to the device. Then clear 1682 * any possibly stalled bulk endpoints. 1683 * 1684 * This is done in 3 steps, using 3 transfers: 1685 * UMASS_T_CBI_RESET1 1686 * UMASS_T_CBI_RESET2 1687 * UMASS_T_CBI_RESET3 1688 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint) 1689 */ 1690 1691 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n"); 1692 1693 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1694 req.bRequest = UR_CBI_ADSC; 1695 USETW(req.wValue, 0); 1696 req.wIndex[0] = sc->sc_iface_no; 1697 req.wIndex[1] = 0; 1698 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN); 1699 1700 /* 1701 * The 0x1d code is the SEND DIAGNOSTIC command. To 1702 * distinguish between the two, the last 10 bytes of the CBL 1703 * is filled with 0xff (section 2.2 of the CBI 1704 * specification) 1705 */ 1706 buf[0] = 0x1d; /* Command Block Reset */ 1707 buf[1] = 0x04; 1708 1709 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) { 1710 buf[i] = 0xff; 1711 } 1712 1713 pc = usbd_xfer_get_frame(xfer, 0); 1714 usbd_copy_in(pc, 0, &req, sizeof(req)); 1715 pc = usbd_xfer_get_frame(xfer, 1); 1716 usbd_copy_in(pc, 0, buf, sizeof(buf)); 1717 1718 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1719 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf)); 1720 usbd_xfer_set_frames(xfer, 2); 1721 usbd_transfer_submit(xfer); 1722 break; 1723 1724 default: /* Error */ 1725 if (error == USB_ERR_CANCELLED) 1726 umass_tr_error(xfer, error); 1727 else 1728 umass_transfer_start(sc, UMASS_T_CBI_RESET2); 1729 break; 1730 1731 } 1732 } 1733 1734 static void 1735 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error) 1736 { 1737 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3, 1738 UMASS_T_CBI_DATA_READ, error); 1739 } 1740 1741 static void 1742 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error) 1743 { 1744 struct umass_softc *sc = usbd_xfer_softc(xfer); 1745 1746 umass_t_cbi_data_clear_stall_callback 1747 (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] && 1748 sc->sc_xfer[UMASS_T_CBI_STATUS]) ? 1749 UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND, 1750 UMASS_T_CBI_DATA_WRITE, error); 1751 } 1752 1753 static void 1754 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error) 1755 { 1756 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND, 1757 UMASS_T_CBI_STATUS, error); 1758 } 1759 1760 static void 1761 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer, 1762 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error) 1763 { 1764 struct umass_softc *sc = usbd_xfer_softc(xfer); 1765 1766 switch (USB_GET_STATE(xfer)) { 1767 case USB_ST_TRANSFERRED: 1768 tr_transferred: 1769 if (next_xfer == UMASS_T_CBI_STATUS) { 1770 umass_cbi_start_status(sc); 1771 } else { 1772 umass_transfer_start(sc, next_xfer); 1773 } 1774 break; 1775 1776 case USB_ST_SETUP: 1777 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) { 1778 goto tr_transferred; /* should not happen */ 1779 } 1780 break; 1781 1782 default: /* Error */ 1783 umass_tr_error(xfer, error); 1784 break; 1785 1786 } 1787 } 1788 1789 static void 1790 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error) 1791 { 1792 struct umass_softc *sc = usbd_xfer_softc(xfer); 1793 union ccb *ccb = sc->sc_transfer.ccb; 1794 struct usb_device_request req; 1795 struct usb_page_cache *pc; 1796 1797 switch (USB_GET_STATE(xfer)) { 1798 case USB_ST_TRANSFERRED: 1799 1800 if (sc->sc_transfer.dir == DIR_NONE) { 1801 umass_cbi_start_status(sc); 1802 } else { 1803 umass_transfer_start 1804 (sc, (sc->sc_transfer.dir == DIR_IN) ? 1805 UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE); 1806 } 1807 break; 1808 1809 case USB_ST_SETUP: 1810 1811 if (ccb) { 1812 1813 /* 1814 * do a CBI transfer with cmd_len bytes from 1815 * cmd_data, possibly a data phase of data_len 1816 * bytes from/to the device and finally a status 1817 * read phase. 1818 */ 1819 1820 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1821 req.bRequest = UR_CBI_ADSC; 1822 USETW(req.wValue, 0); 1823 req.wIndex[0] = sc->sc_iface_no; 1824 req.wIndex[1] = 0; 1825 req.wLength[0] = sc->sc_transfer.cmd_len; 1826 req.wLength[1] = 0; 1827 1828 pc = usbd_xfer_get_frame(xfer, 0); 1829 usbd_copy_in(pc, 0, &req, sizeof(req)); 1830 pc = usbd_xfer_get_frame(xfer, 1); 1831 usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data, 1832 sc->sc_transfer.cmd_len); 1833 1834 usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 1835 usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len); 1836 usbd_xfer_set_frames(xfer, 1837 sc->sc_transfer.cmd_len ? 2 : 1); 1838 1839 DIF(UDMASS_CBI, 1840 umass_cbi_dump_cmd(sc, 1841 sc->sc_transfer.cmd_data, 1842 sc->sc_transfer.cmd_len)); 1843 1844 usbd_transfer_submit(xfer); 1845 } 1846 break; 1847 1848 default: /* Error */ 1849 /* 1850 * STALL on the control pipe can be result of the command error. 1851 * Attempt to clear this STALL same as for bulk pipe also 1852 * results in command completion interrupt, but ASC/ASCQ there 1853 * look like not always valid, so don't bother about it. 1854 */ 1855 if ((error == USB_ERR_STALLED) || 1856 (sc->sc_transfer.callback == &umass_cam_cb)) { 1857 sc->sc_transfer.ccb = NULL; 1858 (sc->sc_transfer.callback) 1859 (sc, ccb, sc->sc_transfer.data_len, 1860 STATUS_CMD_UNKNOWN); 1861 } else { 1862 umass_tr_error(xfer, error); 1863 /* skip reset */ 1864 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND; 1865 } 1866 break; 1867 } 1868 } 1869 1870 static void 1871 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error) 1872 { 1873 struct umass_softc *sc = usbd_xfer_softc(xfer); 1874 uint32_t max_bulk = usbd_xfer_max_len(xfer); 1875 #ifndef UMASS_EXT_BUFFER 1876 struct usb_page_cache *pc; 1877 #endif 1878 int actlen, sumlen; 1879 1880 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1881 1882 switch (USB_GET_STATE(xfer)) { 1883 case USB_ST_TRANSFERRED: 1884 #ifndef UMASS_EXT_BUFFER 1885 pc = usbd_xfer_get_frame(xfer, 0); 1886 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen); 1887 #endif 1888 sc->sc_transfer.data_rem -= actlen; 1889 sc->sc_transfer.data_ptr += actlen; 1890 sc->sc_transfer.actlen += actlen; 1891 1892 if (actlen < sumlen) { 1893 /* short transfer */ 1894 sc->sc_transfer.data_rem = 0; 1895 } 1896 case USB_ST_SETUP: 1897 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n", 1898 max_bulk, sc->sc_transfer.data_rem); 1899 1900 if (sc->sc_transfer.data_rem == 0) { 1901 umass_cbi_start_status(sc); 1902 break; 1903 } 1904 if (max_bulk > sc->sc_transfer.data_rem) { 1905 max_bulk = sc->sc_transfer.data_rem; 1906 } 1907 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout); 1908 1909 #ifdef UMASS_EXT_BUFFER 1910 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr, 1911 max_bulk); 1912 #else 1913 usbd_xfer_set_frame_len(xfer, 0, max_bulk); 1914 #endif 1915 usbd_transfer_submit(xfer); 1916 break; 1917 1918 default: /* Error */ 1919 if ((error == USB_ERR_CANCELLED) || 1920 (sc->sc_transfer.callback != &umass_cam_cb)) { 1921 umass_tr_error(xfer, error); 1922 } else { 1923 umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS); 1924 } 1925 break; 1926 1927 } 1928 } 1929 1930 static void 1931 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error) 1932 { 1933 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS, 1934 UMASS_T_CBI_DATA_READ, error); 1935 } 1936 1937 static void 1938 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error) 1939 { 1940 struct umass_softc *sc = usbd_xfer_softc(xfer); 1941 uint32_t max_bulk = usbd_xfer_max_len(xfer); 1942 #ifndef UMASS_EXT_BUFFER 1943 struct usb_page_cache *pc; 1944 #endif 1945 int actlen, sumlen; 1946 1947 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL); 1948 1949 switch (USB_GET_STATE(xfer)) { 1950 case USB_ST_TRANSFERRED: 1951 sc->sc_transfer.data_rem -= actlen; 1952 sc->sc_transfer.data_ptr += actlen; 1953 sc->sc_transfer.actlen += actlen; 1954 1955 if (actlen < sumlen) { 1956 /* short transfer */ 1957 sc->sc_transfer.data_rem = 0; 1958 } 1959 case USB_ST_SETUP: 1960 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n", 1961 max_bulk, sc->sc_transfer.data_rem); 1962 1963 if (sc->sc_transfer.data_rem == 0) { 1964 umass_cbi_start_status(sc); 1965 break; 1966 } 1967 if (max_bulk > sc->sc_transfer.data_rem) { 1968 max_bulk = sc->sc_transfer.data_rem; 1969 } 1970 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout); 1971 1972 #ifdef UMASS_EXT_BUFFER 1973 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr, 1974 max_bulk); 1975 #else 1976 pc = usbd_xfer_get_frame(xfer, 0); 1977 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk); 1978 usbd_xfer_set_frame_len(xfer, 0, max_bulk); 1979 #endif 1980 1981 usbd_transfer_submit(xfer); 1982 break; 1983 1984 default: /* Error */ 1985 if ((error == USB_ERR_CANCELLED) || 1986 (sc->sc_transfer.callback != &umass_cam_cb)) { 1987 umass_tr_error(xfer, error); 1988 } else { 1989 umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS); 1990 } 1991 break; 1992 1993 } 1994 } 1995 1996 static void 1997 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error) 1998 { 1999 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS, 2000 UMASS_T_CBI_DATA_WRITE, error); 2001 } 2002 2003 static void 2004 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error) 2005 { 2006 struct umass_softc *sc = usbd_xfer_softc(xfer); 2007 union ccb *ccb = sc->sc_transfer.ccb; 2008 struct usb_page_cache *pc; 2009 uint32_t residue; 2010 uint8_t status; 2011 int actlen; 2012 2013 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 2014 2015 switch (USB_GET_STATE(xfer)) { 2016 case USB_ST_TRANSFERRED: 2017 2018 if (actlen < sizeof(sc->sbl)) { 2019 goto tr_setup; 2020 } 2021 pc = usbd_xfer_get_frame(xfer, 0); 2022 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl)); 2023 2024 residue = (sc->sc_transfer.data_len - 2025 sc->sc_transfer.actlen); 2026 2027 /* dissect the information in the buffer */ 2028 2029 if (sc->sc_proto & UMASS_PROTO_UFI) { 2030 2031 /* 2032 * Section 3.4.3.1.3 specifies that the UFI command 2033 * protocol returns an ASC and ASCQ in the interrupt 2034 * data block. 2035 */ 2036 2037 DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, " 2038 "ASCQ = 0x%02x\n", sc->sbl.ufi.asc, 2039 sc->sbl.ufi.ascq); 2040 2041 status = (((sc->sbl.ufi.asc == 0) && 2042 (sc->sbl.ufi.ascq == 0)) ? 2043 STATUS_CMD_OK : STATUS_CMD_FAILED); 2044 2045 sc->sc_transfer.ccb = NULL; 2046 2047 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND; 2048 2049 (sc->sc_transfer.callback) 2050 (sc, ccb, residue, status); 2051 2052 break; 2053 2054 } else { 2055 2056 /* Command Interrupt Data Block */ 2057 2058 DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n", 2059 sc->sbl.common.type, sc->sbl.common.value); 2060 2061 if (sc->sbl.common.type == IDB_TYPE_CCI) { 2062 2063 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK); 2064 2065 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK : 2066 (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED : 2067 (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED : 2068 STATUS_WIRE_FAILED); 2069 2070 sc->sc_transfer.ccb = NULL; 2071 2072 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND; 2073 2074 (sc->sc_transfer.callback) 2075 (sc, ccb, residue, status); 2076 2077 break; 2078 } 2079 } 2080 2081 /* fallthrough */ 2082 2083 case USB_ST_SETUP: 2084 tr_setup: 2085 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 2086 usbd_transfer_submit(xfer); 2087 break; 2088 2089 default: /* Error */ 2090 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n", 2091 usbd_errstr(error)); 2092 umass_tr_error(xfer, error); 2093 break; 2094 2095 } 2096 } 2097 2098 /* 2099 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI)) 2100 */ 2101 2102 static int 2103 umass_cam_attach_sim(struct umass_softc *sc) 2104 { 2105 struct cam_devq *devq; /* Per device Queue */ 2106 2107 /* 2108 * A HBA is attached to the CAM layer. 2109 * 2110 * The CAM layer will then after a while start probing for devices on 2111 * the bus. The number of SIMs is limited to one. 2112 */ 2113 2114 devq = cam_simq_alloc(1 /* maximum openings */ ); 2115 if (devq == NULL) { 2116 return (ENOMEM); 2117 } 2118 sc->sc_sim = cam_sim_alloc 2119 (&umass_cam_action, &umass_cam_poll, 2120 DEVNAME_SIM, 2121 sc /* priv */ , 2122 sc->sc_unit /* unit number */ , 2123 #if (__FreeBSD_version >= 700037) 2124 &sc->sc_mtx /* mutex */ , 2125 #endif 2126 1 /* maximum device openings */ , 2127 0 /* maximum tagged device openings */ , 2128 devq); 2129 2130 if (sc->sc_sim == NULL) { 2131 cam_simq_free(devq); 2132 return (ENOMEM); 2133 } 2134 2135 #if (__FreeBSD_version >= 700037) 2136 mtx_lock(&sc->sc_mtx); 2137 #endif 2138 2139 #if (__FreeBSD_version >= 700048) 2140 if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) { 2141 mtx_unlock(&sc->sc_mtx); 2142 return (ENOMEM); 2143 } 2144 #else 2145 if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) { 2146 #if (__FreeBSD_version >= 700037) 2147 mtx_unlock(&sc->sc_mtx); 2148 #endif 2149 return (ENOMEM); 2150 } 2151 #endif 2152 2153 #if (__FreeBSD_version >= 700037) 2154 mtx_unlock(&sc->sc_mtx); 2155 #endif 2156 return (0); 2157 } 2158 2159 static void 2160 umass_cam_attach(struct umass_softc *sc) 2161 { 2162 #ifndef USB_DEBUG 2163 if (bootverbose) 2164 #endif 2165 printf("%s:%d:%d:%d: Attached to scbus%d\n", 2166 sc->sc_name, cam_sim_path(sc->sc_sim), 2167 sc->sc_unit, CAM_LUN_WILDCARD, 2168 cam_sim_path(sc->sc_sim)); 2169 } 2170 2171 /* umass_cam_detach 2172 * detach from the CAM layer 2173 */ 2174 2175 static void 2176 umass_cam_detach_sim(struct umass_softc *sc) 2177 { 2178 if (sc->sc_sim != NULL) { 2179 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) { 2180 /* accessing the softc is not possible after this */ 2181 sc->sc_sim->softc = UMASS_GONE; 2182 cam_sim_free(sc->sc_sim, /* free_devq */ TRUE); 2183 } else { 2184 panic("%s: CAM layer is busy\n", 2185 sc->sc_name); 2186 } 2187 sc->sc_sim = NULL; 2188 } 2189 } 2190 2191 /* umass_cam_action 2192 * CAM requests for action come through here 2193 */ 2194 2195 static void 2196 umass_cam_action(struct cam_sim *sim, union ccb *ccb) 2197 { 2198 struct umass_softc *sc = (struct umass_softc *)sim->softc; 2199 2200 if (sc == UMASS_GONE || 2201 (sc != NULL && !usbd_device_attached(sc->sc_udev))) { 2202 ccb->ccb_h.status = CAM_SEL_TIMEOUT; 2203 xpt_done(ccb); 2204 return; 2205 } 2206 if (sc) { 2207 #if (__FreeBSD_version < 700037) 2208 mtx_lock(&sc->sc_mtx); 2209 #endif 2210 } 2211 /* 2212 * Verify, depending on the operation to perform, that we either got 2213 * a valid sc, because an existing target was referenced, or 2214 * otherwise the SIM is addressed. 2215 * 2216 * This avoids bombing out at a printf and does give the CAM layer some 2217 * sensible feedback on errors. 2218 */ 2219 switch (ccb->ccb_h.func_code) { 2220 case XPT_SCSI_IO: 2221 case XPT_RESET_DEV: 2222 case XPT_GET_TRAN_SETTINGS: 2223 case XPT_SET_TRAN_SETTINGS: 2224 case XPT_CALC_GEOMETRY: 2225 /* the opcodes requiring a target. These should never occur. */ 2226 if (sc == NULL) { 2227 DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: " 2228 "Invalid target (target needed)\n", 2229 DEVNAME_SIM, cam_sim_path(sc->sc_sim), 2230 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2231 ccb->ccb_h.func_code); 2232 2233 ccb->ccb_h.status = CAM_TID_INVALID; 2234 xpt_done(ccb); 2235 goto done; 2236 } 2237 break; 2238 case XPT_PATH_INQ: 2239 case XPT_NOOP: 2240 /* 2241 * The opcodes sometimes aimed at a target (sc is valid), 2242 * sometimes aimed at the SIM (sc is invalid and target is 2243 * CAM_TARGET_WILDCARD) 2244 */ 2245 if ((sc == NULL) && 2246 (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) { 2247 DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: " 2248 "Invalid target (no wildcard)\n", 2249 DEVNAME_SIM, cam_sim_path(sc->sc_sim), 2250 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2251 ccb->ccb_h.func_code); 2252 2253 ccb->ccb_h.status = CAM_TID_INVALID; 2254 xpt_done(ccb); 2255 goto done; 2256 } 2257 break; 2258 default: 2259 /* XXX Hm, we should check the input parameters */ 2260 break; 2261 } 2262 2263 /* Perform the requested action */ 2264 switch (ccb->ccb_h.func_code) { 2265 case XPT_SCSI_IO: 2266 { 2267 uint8_t *cmd; 2268 uint8_t dir; 2269 2270 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) { 2271 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr); 2272 } else { 2273 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes); 2274 } 2275 2276 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: " 2277 "cmd: 0x%02x, flags: 0x%02x, " 2278 "%db cmd/%db data/%db sense\n", 2279 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id, 2280 ccb->ccb_h.target_lun, cmd[0], 2281 ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len, 2282 ccb->csio.dxfer_len, ccb->csio.sense_len); 2283 2284 if (sc->sc_transfer.ccb) { 2285 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: " 2286 "I/O in progress, deferring\n", 2287 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id, 2288 ccb->ccb_h.target_lun); 2289 ccb->ccb_h.status = CAM_SCSI_BUSY; 2290 xpt_done(ccb); 2291 goto done; 2292 } 2293 switch (ccb->ccb_h.flags & CAM_DIR_MASK) { 2294 case CAM_DIR_IN: 2295 dir = DIR_IN; 2296 break; 2297 case CAM_DIR_OUT: 2298 dir = DIR_OUT; 2299 DIF(UDMASS_SCSI, 2300 umass_dump_buffer(sc, ccb->csio.data_ptr, 2301 ccb->csio.dxfer_len, 48)); 2302 break; 2303 default: 2304 dir = DIR_NONE; 2305 } 2306 2307 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED; 2308 2309 /* 2310 * sc->sc_transform will convert the command to the 2311 * command format needed by the specific command set 2312 * and return the converted command in 2313 * "sc->sc_transfer.cmd_data" 2314 */ 2315 if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) { 2316 2317 if (sc->sc_transfer.cmd_data[0] == INQUIRY) { 2318 const char *pserial; 2319 2320 pserial = usb_get_serial(sc->sc_udev); 2321 2322 /* 2323 * Umass devices don't generally report their serial numbers 2324 * in the usual SCSI way. Emulate it here. 2325 */ 2326 if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) && 2327 (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) && 2328 (pserial[0] != '\0')) { 2329 struct scsi_vpd_unit_serial_number *vpd_serial; 2330 2331 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr; 2332 vpd_serial->length = strlen(pserial); 2333 if (vpd_serial->length > sizeof(vpd_serial->serial_num)) 2334 vpd_serial->length = sizeof(vpd_serial->serial_num); 2335 memcpy(vpd_serial->serial_num, pserial, vpd_serial->length); 2336 ccb->csio.scsi_status = SCSI_STATUS_OK; 2337 ccb->ccb_h.status = CAM_REQ_CMP; 2338 xpt_done(ccb); 2339 goto done; 2340 } 2341 2342 /* 2343 * Handle EVPD inquiry for broken devices first 2344 * NO_INQUIRY also implies NO_INQUIRY_EVPD 2345 */ 2346 if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) && 2347 (sc->sc_transfer.cmd_data[1] & SI_EVPD)) { 2348 struct scsi_sense_data *sense; 2349 2350 sense = &ccb->csio.sense_data; 2351 bzero(sense, sizeof(*sense)); 2352 sense->error_code = SSD_CURRENT_ERROR; 2353 sense->flags = SSD_KEY_ILLEGAL_REQUEST; 2354 sense->add_sense_code = 0x24; 2355 sense->extra_len = 10; 2356 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 2357 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR | 2358 CAM_AUTOSNS_VALID; 2359 xpt_done(ccb); 2360 goto done; 2361 } 2362 /* 2363 * Return fake inquiry data for 2364 * broken devices 2365 */ 2366 if (sc->sc_quirks & NO_INQUIRY) { 2367 memcpy(ccb->csio.data_ptr, &fake_inq_data, 2368 sizeof(fake_inq_data)); 2369 ccb->csio.scsi_status = SCSI_STATUS_OK; 2370 ccb->ccb_h.status = CAM_REQ_CMP; 2371 xpt_done(ccb); 2372 goto done; 2373 } 2374 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) { 2375 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH; 2376 } 2377 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) { 2378 if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) { 2379 ccb->csio.scsi_status = SCSI_STATUS_OK; 2380 ccb->ccb_h.status = CAM_REQ_CMP; 2381 xpt_done(ccb); 2382 goto done; 2383 } 2384 } 2385 umass_command_start(sc, dir, ccb->csio.data_ptr, 2386 ccb->csio.dxfer_len, 2387 ccb->ccb_h.timeout, 2388 &umass_cam_cb, ccb); 2389 } 2390 break; 2391 } 2392 case XPT_PATH_INQ: 2393 { 2394 struct ccb_pathinq *cpi = &ccb->cpi; 2395 2396 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n", 2397 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id, 2398 ccb->ccb_h.target_lun); 2399 2400 /* host specific information */ 2401 cpi->version_num = 1; 2402 cpi->hba_inquiry = 0; 2403 cpi->target_sprt = 0; 2404 cpi->hba_misc = PIM_NO_6_BYTE; 2405 cpi->hba_eng_cnt = 0; 2406 cpi->max_target = UMASS_SCSIID_MAX; /* one target */ 2407 cpi->initiator_id = UMASS_SCSIID_HOST; 2408 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2409 strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN); 2410 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2411 cpi->unit_number = cam_sim_unit(sim); 2412 cpi->bus_id = sc->sc_unit; 2413 #if (__FreeBSD_version >= 700025) 2414 cpi->protocol = PROTO_SCSI; 2415 cpi->protocol_version = SCSI_REV_2; 2416 cpi->transport = XPORT_USB; 2417 cpi->transport_version = 0; 2418 #endif 2419 if (sc == NULL) { 2420 cpi->base_transfer_speed = 0; 2421 cpi->max_lun = 0; 2422 } else { 2423 if (sc->sc_quirks & FLOPPY_SPEED) { 2424 cpi->base_transfer_speed = 2425 UMASS_FLOPPY_TRANSFER_SPEED; 2426 } else { 2427 switch (usbd_get_speed(sc->sc_udev)) { 2428 case USB_SPEED_SUPER: 2429 cpi->base_transfer_speed = 2430 UMASS_SUPER_TRANSFER_SPEED; 2431 cpi->maxio = MAXPHYS; 2432 break; 2433 case USB_SPEED_HIGH: 2434 cpi->base_transfer_speed = 2435 UMASS_HIGH_TRANSFER_SPEED; 2436 break; 2437 default: 2438 cpi->base_transfer_speed = 2439 UMASS_FULL_TRANSFER_SPEED; 2440 break; 2441 } 2442 } 2443 cpi->max_lun = sc->sc_maxlun; 2444 } 2445 2446 cpi->ccb_h.status = CAM_REQ_CMP; 2447 xpt_done(ccb); 2448 break; 2449 } 2450 case XPT_RESET_DEV: 2451 { 2452 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n", 2453 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id, 2454 ccb->ccb_h.target_lun); 2455 2456 umass_reset(sc); 2457 2458 ccb->ccb_h.status = CAM_REQ_CMP; 2459 xpt_done(ccb); 2460 break; 2461 } 2462 case XPT_GET_TRAN_SETTINGS: 2463 { 2464 struct ccb_trans_settings *cts = &ccb->cts; 2465 2466 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n", 2467 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id, 2468 ccb->ccb_h.target_lun); 2469 2470 #if (__FreeBSD_version >= 700025) 2471 cts->protocol = PROTO_SCSI; 2472 cts->protocol_version = SCSI_REV_2; 2473 cts->transport = XPORT_USB; 2474 cts->transport_version = 0; 2475 cts->xport_specific.valid = 0; 2476 #else 2477 cts->valid = 0; 2478 cts->flags = 0; /* no disconnection, tagging */ 2479 #endif 2480 ccb->ccb_h.status = CAM_REQ_CMP; 2481 xpt_done(ccb); 2482 break; 2483 } 2484 case XPT_SET_TRAN_SETTINGS: 2485 { 2486 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n", 2487 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id, 2488 ccb->ccb_h.target_lun); 2489 2490 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2491 xpt_done(ccb); 2492 break; 2493 } 2494 case XPT_CALC_GEOMETRY: 2495 { 2496 cam_calc_geometry(&ccb->ccg, /* extended */ 1); 2497 xpt_done(ccb); 2498 break; 2499 } 2500 case XPT_NOOP: 2501 { 2502 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n", 2503 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id, 2504 ccb->ccb_h.target_lun); 2505 2506 ccb->ccb_h.status = CAM_REQ_CMP; 2507 xpt_done(ccb); 2508 break; 2509 } 2510 default: 2511 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: " 2512 "Not implemented\n", 2513 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id, 2514 ccb->ccb_h.target_lun, ccb->ccb_h.func_code); 2515 2516 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2517 xpt_done(ccb); 2518 break; 2519 } 2520 2521 done: 2522 #if (__FreeBSD_version < 700037) 2523 if (sc) { 2524 mtx_unlock(&sc->sc_mtx); 2525 } 2526 #endif 2527 return; 2528 } 2529 2530 static void 2531 umass_cam_poll(struct cam_sim *sim) 2532 { 2533 struct umass_softc *sc = (struct umass_softc *)sim->softc; 2534 2535 if (sc == UMASS_GONE) 2536 return; 2537 2538 DPRINTF(sc, UDMASS_SCSI, "CAM poll\n"); 2539 2540 usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX); 2541 } 2542 2543 2544 /* umass_cam_cb 2545 * finalise a completed CAM command 2546 */ 2547 2548 static void 2549 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue, 2550 uint8_t status) 2551 { 2552 ccb->csio.resid = residue; 2553 2554 switch (status) { 2555 case STATUS_CMD_OK: 2556 ccb->ccb_h.status = CAM_REQ_CMP; 2557 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) && 2558 (ccb->ccb_h.func_code == XPT_SCSI_IO) && 2559 (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) { 2560 struct scsi_read_capacity_data *rcap; 2561 uint32_t maxsector; 2562 2563 rcap = (void *)(ccb->csio.data_ptr); 2564 maxsector = scsi_4btoul(rcap->addr) - 1; 2565 scsi_ulto4b(maxsector, rcap->addr); 2566 } 2567 /* 2568 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list 2569 * of pages supported by the device - otherwise, CAM 2570 * will never ask us for the serial number if the 2571 * device cannot handle that by itself. 2572 */ 2573 if (ccb->ccb_h.func_code == XPT_SCSI_IO && 2574 sc->sc_transfer.cmd_data[0] == INQUIRY && 2575 (sc->sc_transfer.cmd_data[1] & SI_EVPD) && 2576 sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST && 2577 (usb_get_serial(sc->sc_udev)[0] != '\0')) { 2578 struct ccb_scsiio *csio; 2579 struct scsi_vpd_supported_page_list *page_list; 2580 2581 csio = &ccb->csio; 2582 page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr; 2583 if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) { 2584 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER; 2585 page_list->length++; 2586 } 2587 } 2588 xpt_done(ccb); 2589 break; 2590 2591 case STATUS_CMD_UNKNOWN: 2592 case STATUS_CMD_FAILED: 2593 2594 /* fetch sense data */ 2595 2596 /* the rest of the command was filled in at attach */ 2597 sc->cam_scsi_sense.length = ccb->csio.sense_len; 2598 2599 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of " 2600 "sense data\n", ccb->csio.sense_len); 2601 2602 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode, 2603 sizeof(sc->cam_scsi_sense))) { 2604 2605 if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) && 2606 (sc->sc_transfer.cmd_data[0] == INQUIRY)) { 2607 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH; 2608 } 2609 umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code, 2610 ccb->csio.sense_len, ccb->ccb_h.timeout, 2611 &umass_cam_sense_cb, ccb); 2612 } 2613 break; 2614 2615 default: 2616 /* 2617 * The wire protocol failed and will hopefully have 2618 * recovered. We return an error to CAM and let CAM 2619 * retry the command if necessary. 2620 */ 2621 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2622 xpt_done(ccb); 2623 break; 2624 } 2625 } 2626 2627 /* 2628 * Finalise a completed autosense operation 2629 */ 2630 static void 2631 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue, 2632 uint8_t status) 2633 { 2634 uint8_t *cmd; 2635 uint8_t key; 2636 2637 switch (status) { 2638 case STATUS_CMD_OK: 2639 case STATUS_CMD_UNKNOWN: 2640 case STATUS_CMD_FAILED: 2641 2642 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) { 2643 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr); 2644 } else { 2645 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes); 2646 } 2647 2648 key = (ccb->csio.sense_data.flags & SSD_KEY); 2649 2650 /* 2651 * Getting sense data always succeeds (apart from wire 2652 * failures): 2653 */ 2654 if ((sc->sc_quirks & RS_NO_CLEAR_UA) && 2655 (cmd[0] == INQUIRY) && 2656 (key == SSD_KEY_UNIT_ATTENTION)) { 2657 /* 2658 * Ignore unit attention errors in the case where 2659 * the Unit Attention state is not cleared on 2660 * REQUEST SENSE. They will appear again at the next 2661 * command. 2662 */ 2663 ccb->ccb_h.status = CAM_REQ_CMP; 2664 } else if (key == SSD_KEY_NO_SENSE) { 2665 /* 2666 * No problem after all (in the case of CBI without 2667 * CCI) 2668 */ 2669 ccb->ccb_h.status = CAM_REQ_CMP; 2670 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) && 2671 (cmd[0] == READ_CAPACITY) && 2672 (key == SSD_KEY_UNIT_ATTENTION)) { 2673 /* 2674 * Some devices do not clear the unit attention error 2675 * on request sense. We insert a test unit ready 2676 * command to make sure we clear the unit attention 2677 * condition, then allow the retry to proceed as 2678 * usual. 2679 */ 2680 2681 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 2682 | CAM_AUTOSNS_VALID; 2683 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 2684 2685 #if 0 2686 DELAY(300000); 2687 #endif 2688 DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky" 2689 "TEST_UNIT_READY\n"); 2690 2691 /* the rest of the command was filled in at attach */ 2692 2693 if (umass_std_transform(sc, ccb, 2694 &sc->cam_scsi_test_unit_ready.opcode, 2695 sizeof(sc->cam_scsi_test_unit_ready))) { 2696 umass_command_start(sc, DIR_NONE, NULL, 0, 2697 ccb->ccb_h.timeout, 2698 &umass_cam_quirk_cb, ccb); 2699 } 2700 break; 2701 } else { 2702 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 2703 | CAM_AUTOSNS_VALID; 2704 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 2705 } 2706 xpt_done(ccb); 2707 break; 2708 2709 default: 2710 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, " 2711 "status %d\n", status); 2712 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL; 2713 xpt_done(ccb); 2714 } 2715 } 2716 2717 /* 2718 * This completion code just handles the fact that we sent a test-unit-ready 2719 * after having previously failed a READ CAPACITY with CHECK_COND. Even 2720 * though this command succeeded, we have to tell CAM to retry. 2721 */ 2722 static void 2723 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue, 2724 uint8_t status) 2725 { 2726 DPRINTF(sc, UDMASS_SCSI, "Test unit ready " 2727 "returned status %d\n", status); 2728 2729 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 2730 | CAM_AUTOSNS_VALID; 2731 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND; 2732 xpt_done(ccb); 2733 } 2734 2735 /* 2736 * SCSI specific functions 2737 */ 2738 2739 static uint8_t 2740 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr, 2741 uint8_t cmd_len) 2742 { 2743 if ((cmd_len == 0) || 2744 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) { 2745 DPRINTF(sc, UDMASS_SCSI, "Invalid command " 2746 "length: %d bytes\n", cmd_len); 2747 return (0); /* failure */ 2748 } 2749 sc->sc_transfer.cmd_len = cmd_len; 2750 2751 switch (cmd_ptr[0]) { 2752 case TEST_UNIT_READY: 2753 if (sc->sc_quirks & NO_TEST_UNIT_READY) { 2754 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY " 2755 "to START_UNIT\n"); 2756 bzero(sc->sc_transfer.cmd_data, cmd_len); 2757 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT; 2758 sc->sc_transfer.cmd_data[4] = SSS_START; 2759 return (1); 2760 } 2761 break; 2762 2763 case INQUIRY: 2764 /* 2765 * some drives wedge when asked for full inquiry 2766 * information. 2767 */ 2768 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) { 2769 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2770 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH; 2771 return (1); 2772 } 2773 break; 2774 } 2775 2776 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2777 return (1); 2778 } 2779 2780 static uint8_t 2781 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len) 2782 { 2783 if ((cmd_len == 0) || 2784 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) { 2785 DPRINTF(sc, UDMASS_SCSI, "Invalid command " 2786 "length: %d bytes\n", cmd_len); 2787 return (0); /* failure */ 2788 } 2789 switch (cmd_ptr[0]) { 2790 /* these commands are defined in RBC: */ 2791 case READ_10: 2792 case READ_CAPACITY: 2793 case START_STOP_UNIT: 2794 case SYNCHRONIZE_CACHE: 2795 case WRITE_10: 2796 case 0x2f: /* VERIFY_10 is absent from 2797 * scsi_all.h??? */ 2798 case INQUIRY: 2799 case MODE_SELECT_10: 2800 case MODE_SENSE_10: 2801 case TEST_UNIT_READY: 2802 case WRITE_BUFFER: 2803 /* 2804 * The following commands are not listed in my copy of the 2805 * RBC specs. CAM however seems to want those, and at least 2806 * the Sony DSC device appears to support those as well 2807 */ 2808 case REQUEST_SENSE: 2809 case PREVENT_ALLOW: 2810 2811 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2812 2813 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) { 2814 bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len); 2815 cmd_len = 12; 2816 } 2817 sc->sc_transfer.cmd_len = cmd_len; 2818 return (1); /* sucess */ 2819 2820 /* All other commands are not legal in RBC */ 2821 default: 2822 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC " 2823 "command 0x%02x\n", cmd_ptr[0]); 2824 return (0); /* failure */ 2825 } 2826 } 2827 2828 static uint8_t 2829 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr, 2830 uint8_t cmd_len) 2831 { 2832 if ((cmd_len == 0) || 2833 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) { 2834 DPRINTF(sc, UDMASS_SCSI, "Invalid command " 2835 "length: %d bytes\n", cmd_len); 2836 return (0); /* failure */ 2837 } 2838 /* An UFI command is always 12 bytes in length */ 2839 sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH; 2840 2841 /* Zero the command data */ 2842 bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH); 2843 2844 switch (cmd_ptr[0]) { 2845 /* 2846 * Commands of which the format has been verified. They 2847 * should work. Copy the command into the (zeroed out) 2848 * destination buffer. 2849 */ 2850 case TEST_UNIT_READY: 2851 if (sc->sc_quirks & NO_TEST_UNIT_READY) { 2852 /* 2853 * Some devices do not support this command. Start 2854 * Stop Unit should give the same results 2855 */ 2856 DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY " 2857 "to START_UNIT\n"); 2858 2859 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT; 2860 sc->sc_transfer.cmd_data[4] = SSS_START; 2861 return (1); 2862 } 2863 break; 2864 2865 case REZERO_UNIT: 2866 case REQUEST_SENSE: 2867 case FORMAT_UNIT: 2868 case INQUIRY: 2869 case START_STOP_UNIT: 2870 case SEND_DIAGNOSTIC: 2871 case PREVENT_ALLOW: 2872 case READ_CAPACITY: 2873 case READ_10: 2874 case WRITE_10: 2875 case POSITION_TO_ELEMENT: /* SEEK_10 */ 2876 case WRITE_AND_VERIFY: 2877 case VERIFY: 2878 case MODE_SELECT_10: 2879 case MODE_SENSE_10: 2880 case READ_12: 2881 case WRITE_12: 2882 case READ_FORMAT_CAPACITIES: 2883 break; 2884 2885 /* 2886 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be 2887 * required for UFI devices, so it is appropriate to fake 2888 * success. 2889 */ 2890 case SYNCHRONIZE_CACHE: 2891 return (2); 2892 2893 default: 2894 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI " 2895 "command 0x%02x\n", cmd_ptr[0]); 2896 return (0); /* failure */ 2897 } 2898 2899 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2900 return (1); /* success */ 2901 } 2902 2903 /* 2904 * 8070i (ATAPI) specific functions 2905 */ 2906 static uint8_t 2907 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr, 2908 uint8_t cmd_len) 2909 { 2910 if ((cmd_len == 0) || 2911 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) { 2912 DPRINTF(sc, UDMASS_SCSI, "Invalid command " 2913 "length: %d bytes\n", cmd_len); 2914 return (0); /* failure */ 2915 } 2916 /* An ATAPI command is always 12 bytes in length. */ 2917 sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH; 2918 2919 /* Zero the command data */ 2920 bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH); 2921 2922 switch (cmd_ptr[0]) { 2923 /* 2924 * Commands of which the format has been verified. They 2925 * should work. Copy the command into the destination 2926 * buffer. 2927 */ 2928 case INQUIRY: 2929 /* 2930 * some drives wedge when asked for full inquiry 2931 * information. 2932 */ 2933 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) { 2934 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2935 2936 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH; 2937 return (1); 2938 } 2939 break; 2940 2941 case TEST_UNIT_READY: 2942 if (sc->sc_quirks & NO_TEST_UNIT_READY) { 2943 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY " 2944 "to START_UNIT\n"); 2945 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT; 2946 sc->sc_transfer.cmd_data[4] = SSS_START; 2947 return (1); 2948 } 2949 break; 2950 2951 case REZERO_UNIT: 2952 case REQUEST_SENSE: 2953 case START_STOP_UNIT: 2954 case SEND_DIAGNOSTIC: 2955 case PREVENT_ALLOW: 2956 case READ_CAPACITY: 2957 case READ_10: 2958 case WRITE_10: 2959 case POSITION_TO_ELEMENT: /* SEEK_10 */ 2960 case SYNCHRONIZE_CACHE: 2961 case MODE_SELECT_10: 2962 case MODE_SENSE_10: 2963 case READ_BUFFER: 2964 case 0x42: /* READ_SUBCHANNEL */ 2965 case 0x43: /* READ_TOC */ 2966 case 0x44: /* READ_HEADER */ 2967 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */ 2968 case 0x48: /* PLAY_TRACK */ 2969 case 0x49: /* PLAY_TRACK_REL */ 2970 case 0x4b: /* PAUSE */ 2971 case 0x51: /* READ_DISK_INFO */ 2972 case 0x52: /* READ_TRACK_INFO */ 2973 case 0x54: /* SEND_OPC */ 2974 case 0x59: /* READ_MASTER_CUE */ 2975 case 0x5b: /* CLOSE_TR_SESSION */ 2976 case 0x5c: /* READ_BUFFER_CAP */ 2977 case 0x5d: /* SEND_CUE_SHEET */ 2978 case 0xa1: /* BLANK */ 2979 case 0xa5: /* PLAY_12 */ 2980 case 0xa6: /* EXCHANGE_MEDIUM */ 2981 case 0xad: /* READ_DVD_STRUCTURE */ 2982 case 0xbb: /* SET_CD_SPEED */ 2983 case 0xe5: /* READ_TRACK_INFO_PHILIPS */ 2984 break; 2985 2986 case READ_12: 2987 case WRITE_12: 2988 default: 2989 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI " 2990 "command 0x%02x - trying anyway\n", 2991 cmd_ptr[0]); 2992 break; 2993 } 2994 2995 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len); 2996 return (1); /* success */ 2997 } 2998 2999 static uint8_t 3000 umass_no_transform(struct umass_softc *sc, uint8_t *cmd, 3001 uint8_t cmdlen) 3002 { 3003 return (0); /* failure */ 3004 } 3005 3006 static uint8_t 3007 umass_std_transform(struct umass_softc *sc, union ccb *ccb, 3008 uint8_t *cmd, uint8_t cmdlen) 3009 { 3010 uint8_t retval; 3011 3012 retval = (sc->sc_transform) (sc, cmd, cmdlen); 3013 3014 if (retval == 2) { 3015 ccb->ccb_h.status = CAM_REQ_CMP; 3016 xpt_done(ccb); 3017 return (0); 3018 } else if (retval == 0) { 3019 ccb->ccb_h.status = CAM_REQ_INVALID; 3020 xpt_done(ccb); 3021 return (0); 3022 } 3023 /* Command should be executed */ 3024 return (1); 3025 } 3026 3027 #ifdef USB_DEBUG 3028 static void 3029 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw) 3030 { 3031 uint8_t *c = cbw->CBWCDB; 3032 3033 uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength); 3034 uint32_t tag = UGETDW(cbw->dCBWTag); 3035 3036 uint8_t clen = cbw->bCDBLength; 3037 uint8_t flags = cbw->bCBWFlags; 3038 uint8_t lun = cbw->bCBWLUN; 3039 3040 DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db " 3041 "(0x%02x%02x%02x%02x%02x%02x%s), " 3042 "data = %db, lun = %d, dir = %s\n", 3043 tag, clen, 3044 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""), 3045 dlen, lun, (flags == CBWFLAGS_IN ? "in" : 3046 (flags == CBWFLAGS_OUT ? "out" : "<invalid>"))); 3047 } 3048 3049 static void 3050 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw) 3051 { 3052 uint32_t sig = UGETDW(csw->dCSWSignature); 3053 uint32_t tag = UGETDW(csw->dCSWTag); 3054 uint32_t res = UGETDW(csw->dCSWDataResidue); 3055 uint8_t status = csw->bCSWStatus; 3056 3057 DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, " 3058 "res = %d, status = 0x%02x (%s)\n", 3059 tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"), 3060 tag, res, 3061 status, (status == CSWSTATUS_GOOD ? "good" : 3062 (status == CSWSTATUS_FAILED ? "failed" : 3063 (status == CSWSTATUS_PHASE ? "phase" : "<invalid>")))); 3064 } 3065 3066 static void 3067 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen) 3068 { 3069 uint8_t *c = cmd; 3070 uint8_t dir = sc->sc_transfer.dir; 3071 3072 DPRINTF(sc, UDMASS_BBB, "cmd = %db " 3073 "(0x%02x%02x%02x%02x%02x%02x%s), " 3074 "data = %db, dir = %s\n", 3075 cmdlen, 3076 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""), 3077 sc->sc_transfer.data_len, 3078 (dir == DIR_IN ? "in" : 3079 (dir == DIR_OUT ? "out" : 3080 (dir == DIR_NONE ? "no data phase" : "<invalid>")))); 3081 } 3082 3083 static void 3084 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen, 3085 uint32_t printlen) 3086 { 3087 uint32_t i, j; 3088 char s1[40]; 3089 char s2[40]; 3090 char s3[5]; 3091 3092 s1[0] = '\0'; 3093 s3[0] = '\0'; 3094 3095 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen); 3096 for (i = 0; (i < buflen) && (i < printlen); i++) { 3097 j = i % 16; 3098 if (j == 0 && i != 0) { 3099 DPRINTF(sc, UDMASS_GEN, "0x %s%s\n", 3100 s1, s2); 3101 s2[0] = '\0'; 3102 } 3103 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff); 3104 } 3105 if (buflen > printlen) 3106 sprintf(s3, " ..."); 3107 DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n", 3108 s1, s2, s3); 3109 } 3110 3111 #endif 3112