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