1 /* $NetBSD: umass.c,v 1.38 2000/06/01 14:29:00 augustss Exp $ */ 2 /*- 3 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>, 4 * Nick Hibma <n_hibma@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $ 29 */ 30 31 /* 32 * Universal Serial Bus Mass Storage Class Bulk-Only Transport 33 * http://www.usb.org/developers/usbmassbulk_09.pdf 34 * XXX Add URL to CBI spec in www.usb.org 35 */ 36 37 /* 38 * Ported to NetBSD by Lennart Augustsson <augustss@netbsd.org>. 39 * Parts of the code written my Jason R. Thorpe <thorpej@shagadelic.org>. 40 */ 41 42 /* 43 * The driver handles 3 Wire Protocols 44 * - Command/Bulk/Interrupt (CBI) 45 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI) 46 * - Mass Storage Bulk-Only (BBB) 47 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases) 48 * 49 * Over these wire protocols it handles the following command protocols 50 * - SCSI 51 * - UFI (floppy command set) 52 * - 8070 (ATA/ATAPI) 53 * 54 * UFI and 8070i are transformed versions of the SCSI command set. The 55 * sc->transform method is used to convert the commands into the appropriate 56 * format (if at all necessary). For example, UFI requires all commands to be 57 * 12 bytes in length amongst other things. 58 * 59 * The source code below is marked and can be split into a number of pieces 60 * (in this order): 61 * 62 * - probe/attach/detach 63 * - generic transfer routines 64 * - BBB 65 * - CBI 66 * - CBI_I (in addition to functions from CBI) 67 * - CAM (Common Access Method) 68 * - SCSI 69 * - UFI 70 * - 8070i 71 * 72 * The protocols are implemented using a state machine, for the transfers as 73 * well as for the resets. The state machine is contained in umass_*_state. 74 * The state machine is started through either umass_*_transfer or 75 * umass_*_reset. 76 * 77 * The reason for doing this is a) CAM performs a lot better this way and b) it 78 * avoids using tsleep from interrupt context (for example after a failed 79 * transfer). 80 */ 81 82 /* 83 * The SCSI related part of this driver has been derived from the 84 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@freebsd.org). 85 * 86 * The CAM layer uses so called actions which are messages sent to the host 87 * adapter for completion. The actions come in through umass_cam_action. The 88 * appropriate block of routines is called depending on the transport protocol 89 * in use. When the transfer has finished, these routines call 90 * umass_cam_cb again to complete the CAM command. 91 */ 92 93 /* XXX Should we split the driver into a number of files? umass.c, 94 * umass_scsi.c, umass_8070.c, umass_ufi.c, umass_bbb.c, umass_cbi.c or 95 * something similar? 96 */ 97 98 #include "atapibus.h" 99 100 #include <sys/param.h> 101 #include <sys/systm.h> 102 #include <sys/kernel.h> 103 #include <sys/conf.h> 104 #if defined(__NetBSD__) || defined(__OpenBSD__) 105 #include <sys/buf.h> 106 #include <sys/device.h> 107 #include <sys/ioctl.h> 108 #include <sys/malloc.h> 109 #undef KASSERT 110 #define KASSERT(cond, msg) 111 #elif defined(__FreeBSD__) 112 #include <sys/module.h> 113 #include <sys/bus.h> 114 #include <machine/clock.h> 115 #endif 116 117 #include <dev/usb/usb.h> 118 #include <dev/usb/usbdi.h> 119 #include <dev/usb/usbdi_util.h> 120 #include <dev/usb/usbdevs.h> 121 122 #if defined(__FreeBSD__) 123 #include <cam/cam.h> 124 #include <cam/cam_ccb.h> 125 #include <cam/cam_sim.h> 126 #include <cam/cam_xpt_sim.h> 127 #include <cam/scsi/scsi_all.h> 128 #include <cam/scsi/scsi_da.h> 129 130 #ifdef UMASS_DO_CAM_RESCAN 131 #include <sys/devicestat.h> 132 #include <cam/cam_periph.h> 133 #endif 134 135 #elif defined(__NetBSD__) || defined(__OpenBSD__) 136 #include <sys/scsiio.h> 137 #include <dev/scsipi/scsi_all.h> 138 #include <dev/scsipi/scsipi_all.h> 139 #include <dev/scsipi/scsiconf.h> 140 141 #include <dev/scsipi/atapiconf.h> 142 143 #include <dev/scsipi/scsipi_disk.h> 144 #include <dev/scsipi/scsi_disk.h> 145 #include <dev/scsipi/scsi_changer.h> 146 147 #include <dev/ata/atavar.h> /* XXX */ 148 #include <sys/disk.h> /* XXX */ 149 #include <dev/scsipi/sdvar.h> /* XXX */ 150 #endif 151 152 #ifdef UMASS_DEBUG 153 #define DIF(m, x) if (umassdebug & (m)) do { x ; } while (0) 154 #define DPRINTF(m, x) if (umassdebug & (m)) logprintf x 155 #define UDMASS_UPPER 0x00008000 /* upper layer */ 156 #define UDMASS_GEN 0x00010000 /* general */ 157 #define UDMASS_SCSI 0x00020000 /* scsi */ 158 #define UDMASS_UFI 0x00040000 /* ufi command set */ 159 #define UDMASS_8070 0x00080000 /* 8070i command set */ 160 #define UDMASS_USB 0x00100000 /* USB general */ 161 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */ 162 #define UDMASS_CBI 0x00400000 /* CBI transfers */ 163 #define UDMASS_ALL 0xffff0000 /* all of the above */ 164 165 #define UDMASS_XFER 0x40000000 /* all transfers */ 166 #define UDMASS_CMD 0x80000000 167 168 int umassdebug = 0; //UDMASS_ALL; 169 #else 170 #define DIF(m, x) /* nop */ 171 #define DPRINTF(m, x) /* nop */ 172 #endif 173 174 175 /* Generic definitions */ 176 177 #define UFI_COMMAND_LENGTH 12 178 179 /* Direction for umass_*_transfer */ 180 #define DIR_NONE 0 181 #define DIR_IN 1 182 #define DIR_OUT 2 183 184 /* The transfer speed determines the timeout value */ 185 #define UMASS_DEFAULT_TRANSFER_SPEED 150 /* in kb/s, conservative est. */ 186 #define UMASS_FLOPPY_TRANSFER_SPEED 20 187 #define UMASS_ZIP100_TRANSFER_SPEED 650 188 189 #define UMASS_SPINUP_TIME 10000 /* ms */ 190 191 #ifdef __FreeBSD__ 192 /* device name */ 193 #define DEVNAME "umass" 194 #define DEVNAME_SIM "umass-" 195 196 #define UMASS_MAX_TRANSFER_SIZE 65536 197 198 /* CAM specific definitions */ 199 200 /* The bus id, whatever that is */ 201 #define UMASS_SCSI_BUS 0 202 203 /* All USB drives are 'connected' to one SIM (SCSI controller). umass3 204 * ends up being target 3 on that SIM. When a request for target 3 205 * comes in we fetch the softc with devclass_get_softc(target_id). 206 * 207 * The SIM is the highest target number. This makes sure that umass0 corresponds 208 * to target 0 on the USB SCSI bus. 209 */ 210 #ifndef UMASS_DEBUG 211 #define UMASS_SCSIID_MAX 32 /* maximum number of drives expected */ 212 #else 213 /* while debugging avoid unnecessary clutter in the output at umass_cam_rescan 214 * (XPT_PATH_INQ) 215 */ 216 #define UMASS_SCSIID_MAX 3 /* maximum number of drives expected */ 217 #endif 218 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX 219 #endif 220 221 #define MS_TO_TICKS(ms) ((ms) * hz / 1000) 222 223 224 /* Bulk-Only features */ 225 226 #define UR_BBB_RESET 0xff /* Bulk-Only reset */ 227 #define UR_BBB_GET_MAX_LUN 0xfe 228 229 /* Command Block Wrapper */ 230 typedef struct { 231 uDWord dCBWSignature; 232 # define CBWSIGNATURE 0x43425355 233 uDWord dCBWTag; 234 uDWord dCBWDataTransferLength; 235 uByte bCBWFlags; 236 # define CBWFLAGS_OUT 0x00 237 # define CBWFLAGS_IN 0x80 238 uByte bCBWLUN; 239 uByte bCDBLength; 240 # define CBWCDBLENGTH 16 241 uByte CBWCDB[CBWCDBLENGTH]; 242 } umass_bbb_cbw_t; 243 #define UMASS_BBB_CBW_SIZE 31 244 245 /* Command Status Wrapper */ 246 typedef struct { 247 uDWord dCSWSignature; 248 # define CSWSIGNATURE 0x53425355 249 uDWord dCSWTag; 250 uDWord dCSWDataResidue; 251 uByte bCSWStatus; 252 # define CSWSTATUS_GOOD 0x0 253 # define CSWSTATUS_FAILED 0x1 254 # define CSWSTATUS_PHASE 0x2 255 } umass_bbb_csw_t; 256 #define UMASS_BBB_CSW_SIZE 13 257 258 /* CBI features */ 259 260 #define UR_CBI_ADSC 0x00 261 262 typedef unsigned char umass_cbi_cbl_t[16]; /* Command block */ 263 264 typedef union { 265 struct { 266 unsigned char type; 267 #define IDB_TYPE_CCI 0x00 268 unsigned char value; 269 #define IDB_VALUE_PASS 0x00 270 #define IDB_VALUE_FAIL 0x01 271 #define IDB_VALUE_PHASE 0x02 272 #define IDB_VALUE_PERSISTENT 0x03 273 #define IDB_VALUE_STATUS_MASK 0x03 274 } common; 275 276 struct { 277 unsigned char asc; 278 unsigned char ascq; 279 } ufi; 280 } umass_cbi_sbl_t; 281 282 283 284 struct umass_softc; /* see below */ 285 286 typedef void (*transfer_cb_f)(struct umass_softc *sc, void *priv, 287 int residue, int status); 288 #define STATUS_CMD_OK 0 /* everything ok */ 289 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */ 290 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */ 291 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */ 292 293 typedef void (*wire_reset_f)(struct umass_softc *sc, int status); 294 typedef void (*wire_transfer_f)(struct umass_softc *sc, int lun, 295 void *cmd, int cmdlen, void *data, int datalen, 296 int dir, transfer_cb_f cb, void *priv); 297 typedef void (*wire_state_f)(usbd_xfer_handle xfer, 298 usbd_private_handle priv, usbd_status err); 299 300 #if defined(__FreeBSD__) 301 typedef int (*command_transform_f)(struct umass_softc *sc, 302 u_char *cmd, int cmdlen, 303 u_char **rcmd, int *rcmdlen)); 304 #endif 305 306 307 /* the per device structure */ 308 struct umass_softc { 309 USBBASEDEVICE sc_dev; /* base device */ 310 usbd_device_handle sc_udev; /* device */ 311 312 unsigned char drive; 313 # define DRIVE_GENERIC 0 /* use defaults for this one */ 314 # define ZIP_100 1 /* to be used for quirks */ 315 # define SHUTTLE_EUSB 2 316 317 unsigned char quirks; 318 /* The drive does not support Test Unit Ready. Convert to 319 * Start Unit. 320 * Y-E Data 321 * ZIP 100 322 */ 323 # define NO_TEST_UNIT_READY 0x01 324 /* The drive does not reset the Unit Attention state after 325 * REQUEST SENSE has been sent. The INQUIRY command does not reset 326 * the UA either, and so CAM runs in circles trying to retrieve the 327 * initial INQUIRY data. 328 * Y-E Data 329 */ 330 # define RS_NO_CLEAR_UA 0x02 /* no REQUEST SENSE on INQUIRY*/ 331 /* The drive does not support START_STOP. 332 * Shuttle E-USB 333 */ 334 # define NO_START_STOP 0x04 335 336 unsigned int proto; 337 # define PROTO_UNKNOWN 0x0000 /* unknown protocol */ 338 # define PROTO_BBB 0x0001 /* USB wire protocol */ 339 # define PROTO_CBI 0x0002 340 # define PROTO_CBI_I 0x0004 341 # define PROTO_WIRE 0x00ff /* USB wire protocol mask */ 342 # define PROTO_SCSI 0x0100 /* command protocol */ 343 # define PROTO_ATAPI 0x0200 344 # define PROTO_UFI 0x0400 345 # define PROTO_COMMAND 0xff00 /* command protocol mask */ 346 347 usbd_interface_handle iface; /* Mass Storage interface */ 348 int ifaceno; /* MS iface number */ 349 350 u_int8_t bulkin; /* bulk-in Endpoint Address */ 351 u_int8_t bulkout; /* bulk-out Endpoint Address */ 352 u_int8_t intrin; /* intr-in Endp. (CBI) */ 353 usbd_pipe_handle bulkin_pipe; 354 usbd_pipe_handle bulkout_pipe; 355 usbd_pipe_handle intrin_pipe; 356 357 /* Reset the device in a wire protocol specific way */ 358 wire_reset_f reset; 359 360 /* The start of a wire transfer. It prepares the whole transfer (cmd, 361 * data, and status stage) and initiates it. It is up to the state 362 * machine (below) to handle the various stages and errors in these 363 */ 364 wire_transfer_f transfer; 365 366 /* The state machine, handling the various states during a transfer */ 367 wire_state_f state; 368 369 #if defined(__FreeBSD__) 370 /* The command transform function is used to conver the SCSI commands 371 * into their derivatives, like UFI, ATAPI, and friends. 372 */ 373 command_transform_f transform; /* command transform */ 374 #endif 375 376 /* Bulk specific variables for transfers in progress */ 377 umass_bbb_cbw_t cbw; /* command block wrapper */ 378 umass_bbb_csw_t csw; /* command status wrapper*/ 379 /* CBI specific variables for transfers in progress */ 380 umass_cbi_cbl_t cbl; /* command block */ 381 umass_cbi_sbl_t sbl; /* status block */ 382 383 /* generic variables for transfers in progress */ 384 /* ctrl transfer requests */ 385 usb_device_request_t request; 386 387 /* xfer handles 388 * Most of our operations are initiated from interrupt context, so 389 * we need to avoid using the one that is in use. We want to avoid 390 * allocating them in the interrupt context as well. 391 */ 392 /* indices into array below */ 393 # define XFER_BBB_CBW 0 /* Bulk-Only */ 394 # define XFER_BBB_DATA 1 395 # define XFER_BBB_DCLEAR 2 396 # define XFER_BBB_CSW1 3 397 # define XFER_BBB_CSW2 4 398 # define XFER_BBB_SCLEAR 5 399 # define XFER_BBB_RESET1 6 400 # define XFER_BBB_RESET2 7 401 # define XFER_BBB_RESET3 8 402 403 # define XFER_CBI_CB 0 /* CBI */ 404 # define XFER_CBI_DATA 1 405 # define XFER_CBI_STATUS 2 406 # define XFER_CBI_DCLEAR 3 407 # define XFER_CBI_SCLEAR 4 408 # define XFER_CBI_RESET1 5 409 # define XFER_CBI_RESET2 6 410 # define XFER_CBI_RESET3 7 411 412 # define XFER_NR 9 /* maximum number */ 413 414 usbd_xfer_handle transfer_xfer[XFER_NR]; /* for ctrl xfers */ 415 416 void *data_buffer; 417 418 int transfer_dir; /* data direction */ 419 void *transfer_data; /* data buffer */ 420 int transfer_datalen; /* (maximum) length */ 421 int transfer_actlen; /* actual length */ 422 transfer_cb_f transfer_cb; /* callback */ 423 void *transfer_priv; /* for callback */ 424 int transfer_status; 425 426 int transfer_state; 427 # define TSTATE_IDLE 0 428 # define TSTATE_BBB_COMMAND 1 /* CBW transfer */ 429 # define TSTATE_BBB_DATA 2 /* Data transfer */ 430 # define TSTATE_BBB_DCLEAR 3 /* clear endpt stall */ 431 # define TSTATE_BBB_STATUS1 4 /* clear endpt stall */ 432 # define TSTATE_BBB_SCLEAR 5 /* clear endpt stall */ 433 # define TSTATE_BBB_STATUS2 6 /* CSW transfer */ 434 # define TSTATE_BBB_RESET1 7 /* reset command */ 435 # define TSTATE_BBB_RESET2 8 /* in clear stall */ 436 # define TSTATE_BBB_RESET3 9 /* out clear stall */ 437 # define TSTATE_CBI_COMMAND 10 /* command transfer */ 438 # define TSTATE_CBI_DATA 11 /* data transfer */ 439 # define TSTATE_CBI_STATUS 12 /* status transfer */ 440 # define TSTATE_CBI_DCLEAR 13 /* clear ep stall */ 441 # define TSTATE_CBI_SCLEAR 14 /* clear ep stall */ 442 # define TSTATE_CBI_RESET1 15 /* reset command */ 443 # define TSTATE_CBI_RESET2 16 /* in clear stall */ 444 # define TSTATE_CBI_RESET3 17 /* out clear stall */ 445 # define TSTATE_STATES 18 /* # of states above */ 446 447 448 int transfer_speed; /* in kb/s */ 449 int timeout; /* in msecs */ 450 451 u_int8_t maxlun; /* max lun supported */ 452 453 #if defined(__FreeBSD__) 454 /* SCSI/CAM specific variables */ 455 struct scsi_sense cam_scsi_sense; 456 457 #elif defined(__NetBSD__) || defined(__OpenBSD__) 458 union { 459 struct scsipi_link sc_link; 460 struct { 461 struct ata_atapi_attach sc_aa; 462 struct ata_drive_datas sc_aa_drive; 463 } aa; 464 } u; 465 struct atapi_adapter sc_atapi_adapter; 466 #define sc_adapter sc_atapi_adapter._generic 467 int sc_xfer_flags; 468 usbd_status sc_sync_status; 469 struct scsipi_sense sc_sense_cmd; 470 471 device_ptr_t sc_child; /* child device, for detach */ 472 char sc_dying; 473 474 #endif 475 }; 476 477 #ifdef UMASS_DEBUG 478 char *states[TSTATE_STATES+1] = { 479 /* should be kept in sync with the list at transfer_state */ 480 "Idle", 481 "BBB CBW", 482 "BBB Data", 483 "BBB Data bulk-in/-out clear stall", 484 "BBB CSW, 1st attempt", 485 "BBB CSW bulk-in clear stall", 486 "BBB CSW, 2nd attempt", 487 "BBB Reset", 488 "BBB bulk-in clear stall", 489 "BBB bulk-out clear stall", 490 "CBI Command", 491 "CBI Data", 492 "CBI Status", 493 "CBI Data bulk-in/-out clear stall", 494 "CBI Status intr-in clear stall", 495 "CBI Reset", 496 "CBI bulk-in clear stall", 497 "CBI bulk-out clear stall", 498 NULL 499 }; 500 #endif 501 502 struct cam_sim *umass_sim; /* SCSI Interface Module */ 503 struct cam_path *umass_path; /* and its path */ 504 505 506 /* USB device probe/attach/detach functions */ 507 USB_DECLARE_DRIVER(umass); 508 Static void umass_disco(struct umass_softc *sc); 509 Static int umass_match_proto(struct umass_softc *sc, 510 usbd_interface_handle iface, 511 usbd_device_handle dev); 512 Static void umass_init_shuttle(struct umass_softc *sc); 513 514 /* generic transfer functions */ 515 Static usbd_status umass_setup_transfer(struct umass_softc *sc, 516 usbd_pipe_handle pipe, 517 void *buffer, int buflen, int flags, 518 usbd_xfer_handle xfer); 519 Static usbd_status umass_setup_ctrl_transfer(struct umass_softc *sc, 520 usbd_device_handle dev, 521 usb_device_request_t *req, 522 void *buffer, int buflen, int flags, 523 usbd_xfer_handle xfer); 524 Static void umass_clear_endpoint_stall(struct umass_softc *sc, 525 u_int8_t endpt, usbd_pipe_handle pipe, 526 int state, usbd_xfer_handle xfer); 527 #if 0 528 Static void umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv); 529 #endif 530 531 /* Bulk-Only related functions */ 532 Static void umass_bbb_reset(struct umass_softc *sc, int status); 533 Static void umass_bbb_transfer(struct umass_softc *sc, int lun, 534 void *cmd, int cmdlen, 535 void *data, int datalen, int dir, 536 transfer_cb_f cb, void *priv); 537 Static void umass_bbb_state(usbd_xfer_handle xfer, 538 usbd_private_handle priv, 539 usbd_status err); 540 usbd_status umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun); 541 542 543 /* CBI related functions */ 544 Static int umass_cbi_adsc(struct umass_softc *sc, char *buffer,int buflen, 545 usbd_xfer_handle xfer); 546 Static void umass_cbi_reset(struct umass_softc *sc, int status); 547 Static void umass_cbi_transfer(struct umass_softc *sc, int lun, 548 void *cmd, int cmdlen, 549 void *data, int datalen, int dir, 550 transfer_cb_f cb, void *priv); 551 Static void umass_cbi_state(usbd_xfer_handle xfer, 552 usbd_private_handle priv, usbd_status err); 553 554 #if defined(__FreeBSD__) 555 /* CAM related functions */ 556 Static void umass_cam_action(struct cam_sim *sim, union ccb *ccb); 557 Static void umass_cam_poll(struct cam_sim *sim); 558 559 Static void umass_cam_cb(struct umass_softc *sc, void *priv, 560 int residue, int status); 561 Static void umass_cam_sense_cb(struct umass_softc *sc, void *priv, 562 int residue, int status); 563 564 #ifdef UMASS_DO_CAM_RESCAN 565 Static void umass_cam_rescan(struct umass_softc *sc); 566 #endif 567 568 Static int umass_cam_attach_sim(void); 569 Static int umass_cam_attach(struct umass_softc *sc); 570 Static int umass_cam_detach_sim(void); 571 Static int umass_cam_detach(struct umass_softc *sc); 572 573 #elif defined(__NetBSD__) || defined(__OpenBSD__) 574 575 #define UMASS_SCSIID_HOST 0x00 576 #define UMASS_SCSIID_DEVICE 0x01 577 578 #define UMASS_MAX_TRANSFER_SIZE MAXBSIZE 579 580 struct scsipi_device umass_dev = 581 { 582 NULL, /* Use default error handler */ 583 NULL, /* have a queue, served by this */ 584 NULL, /* have no async handler */ 585 NULL, /* Use default 'done' routine */ 586 }; 587 588 Static int umass_scsipi_cmd(struct scsipi_xfer *xs); 589 Static void umass_scsipi_minphys(struct buf *bp); 590 Static int umass_scsipi_ioctl(struct scsipi_link *, u_long, 591 caddr_t, int, struct proc *); 592 Static int umass_scsipi_getgeom(struct scsipi_link *link, 593 struct disk_parms *, u_long sectors); 594 595 Static void umass_scsipi_cb(struct umass_softc *sc, void *priv, 596 int residue, int status); 597 Static void umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, 598 int residue, int status); 599 600 Static int scsipiprint(void *aux, const char *pnp); 601 Static int umass_ufi_transform(struct umass_softc *sc, 602 struct scsipi_generic *cmd, int cmdlen, 603 struct scsipi_generic *rcmd, int *rcmdlen); 604 #if NATAPIBUS > 0 605 Static void umass_atapi_probedev(struct atapibus_softc *, int); 606 #endif 607 #endif 608 609 #if defined(__FreeBSD__) 610 /* SCSI specific functions */ 611 Static int umass_scsi_transform(struct umass_softc *sc, 612 unsigned char *cmd, int cmdlen, 613 unsigned char **rcmd, int *rcmdlen); 614 615 /* UFI specific functions */ 616 Static int umass_ufi_transform(struct umass_softc *sc, 617 unsigned char *cmd, int cmdlen, 618 unsigned char **rcmd, int *rcmdlen); 619 620 /* 8070 specific functions */ 621 Static int umass_8070_transform(struct umass_softc *sc, 622 unsigned char *cmd, int cmdlen, 623 unsigned char **rcmd, int *rcmdlen); 624 #endif 625 626 #ifdef UMASS_DEBUG 627 /* General debugging functions */ 628 Static void umass_bbb_dump_cbw(struct umass_softc *sc, 629 umass_bbb_cbw_t *cbw); 630 Static void umass_bbb_dump_csw(struct umass_softc *sc, 631 umass_bbb_csw_t *csw); 632 Static void umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, 633 int buflen, int printlen); 634 #endif 635 636 637 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe); /* XXXXX */ 638 639 /* 640 * USB device probe/attach/detach 641 */ 642 643 /* 644 * Match the device we are seeing with the devices supported. Fill in the 645 * proto and drive fields in the softc accordingly. 646 * This function is called from both probe and attach. 647 */ 648 649 Static int 650 umass_match_proto(struct umass_softc *sc, usbd_interface_handle iface, 651 usbd_device_handle dev) 652 { 653 usb_device_descriptor_t *dd; 654 usb_interface_descriptor_t *id; 655 656 /* 657 * Fill in sc->drive and sc->proto and return a match 658 * value if both are determined and 0 otherwise. 659 */ 660 661 sc->drive = DRIVE_GENERIC; 662 sc->proto = PROTO_UNKNOWN; 663 sc->transfer_speed = UMASS_DEFAULT_TRANSFER_SPEED; 664 665 sc->sc_udev = dev; 666 dd = usbd_get_device_descriptor(dev); 667 668 if (UGETW(dd->idVendor) == USB_VENDOR_SHUTTLE 669 && UGETW(dd->idProduct) == USB_PRODUCT_SHUTTLE_EUSB) { 670 sc->drive = SHUTTLE_EUSB; 671 #if CBI_I 672 sc->proto = PROTO_ATAPI | PROTO_CBI_I; 673 #else 674 sc->proto = PROTO_ATAPI | PROTO_CBI; 675 #endif 676 sc->quirks |= NO_TEST_UNIT_READY | NO_START_STOP; 677 return (UMATCH_VENDOR_PRODUCT); 678 } 679 680 if (UGETW(dd->idVendor) == USB_VENDOR_YEDATA 681 && UGETW(dd->idProduct) == USB_PRODUCT_YEDATA_FLASHBUSTERU) { 682 683 /* Revisions < 1.28 do not handle the interrupt endpoint 684 * very well. 685 */ 686 if (UGETW(dd->bcdDevice) < 0x128) 687 sc->proto = PROTO_UFI | PROTO_CBI; 688 else 689 #if CBI_I 690 sc->proto = PROTO_UFI | PROTO_CBI_I; 691 #else 692 sc->proto = PROTO_UFI | PROTO_CBI; 693 #endif 694 /* 695 * Revisions < 1.28 do not have the TEST UNIT READY command 696 * Revisions == 1.28 have a broken TEST UNIT READY 697 */ 698 if (UGETW(dd->bcdDevice) <= 0x128) 699 sc->quirks |= NO_TEST_UNIT_READY; 700 701 sc->quirks |= RS_NO_CLEAR_UA; 702 sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED; 703 return (UMATCH_VENDOR_PRODUCT_REV); 704 } 705 706 707 id = usbd_get_interface_descriptor(iface); 708 if (id == NULL || id->bInterfaceClass != UICLASS_MASS) 709 return (UMATCH_NONE); 710 711 switch (id->bInterfaceSubClass) { 712 case UISUBCLASS_SCSI: 713 sc->proto |= PROTO_SCSI; 714 break; 715 case UISUBCLASS_UFI: 716 sc->transfer_speed = UMASS_FLOPPY_TRANSFER_SPEED; 717 sc->proto |= PROTO_UFI; 718 break; 719 case UISUBCLASS_SFF8020I: 720 case UISUBCLASS_SFF8070I: 721 case UISUBCLASS_QIC157: 722 sc->proto |= PROTO_ATAPI; 723 break; 724 default: 725 DPRINTF(UDMASS_GEN, ("%s: Unsupported command protocol %d\n", 726 USBDEVNAME(sc->sc_dev), id->bInterfaceSubClass)); 727 return (UMATCH_NONE); 728 } 729 730 switch (id->bInterfaceProtocol) { 731 case UIPROTO_MASS_CBI: 732 sc->proto |= PROTO_CBI; 733 break; 734 case UIPROTO_MASS_CBI_I: 735 #if CBI_I 736 sc->proto |= PROTO_CBI_I; 737 #else 738 sc->proto |= PROTO_CBI; 739 #endif 740 break; 741 case UIPROTO_MASS_BBB: 742 sc->proto |= PROTO_BBB; 743 break; 744 case UIPROTO_MASS_BBB_P: 745 sc->drive = ZIP_100; 746 sc->proto |= PROTO_BBB; 747 sc->transfer_speed = UMASS_ZIP100_TRANSFER_SPEED; 748 sc->quirks |= NO_TEST_UNIT_READY; 749 break; 750 default: 751 DPRINTF(UDMASS_GEN, ("%s: Unsupported wire protocol %d\n", 752 USBDEVNAME(sc->sc_dev), id->bInterfaceProtocol)); 753 return (UMATCH_NONE); 754 } 755 756 return (UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO); 757 } 758 759 USB_MATCH(umass) 760 { 761 USB_MATCH_START(umass, uaa); 762 #if defined(__FreeBSD__) 763 struct umass_softc *sc = device_get_softc(self); 764 #else if defined(__NetBSD__) || defined(__OpenBSD__) 765 struct umass_softc scs, *sc = &scs; 766 memset(sc, 0, sizeof *sc); 767 #endif 768 769 if (uaa->iface == NULL) 770 return(UMATCH_NONE); 771 772 return (umass_match_proto(sc, uaa->iface, uaa->device)); 773 } 774 775 USB_ATTACH(umass) 776 { 777 USB_ATTACH_START(umass, sc, uaa); 778 usb_interface_descriptor_t *id; 779 usb_endpoint_descriptor_t *ed; 780 const char *sSubclass, *sProto; 781 char devinfo[1024]; 782 int i, bno; 783 int err; 784 785 /* 786 * the softc struct is bzero-ed in device_set_driver. We can safely 787 * call umass_detach without specifically initialising the struct. 788 */ 789 790 usbd_devinfo(uaa->device, 0, devinfo); 791 USB_ATTACH_SETUP; 792 793 sc->iface = uaa->iface; 794 sc->ifaceno = uaa->ifaceno; 795 796 /* initialise the proto and drive values in the umass_softc (again) */ 797 (void) umass_match_proto(sc, sc->iface, uaa->device); 798 799 /* 800 * The timeout is based on the maximum expected transfer size 801 * divided by the expected transfer speed. 802 * We multiply by 4 to make sure a busy system doesn't make things 803 * fail. 804 */ 805 sc->timeout = 4 * UMASS_MAX_TRANSFER_SIZE / sc->transfer_speed; 806 sc->timeout += UMASS_SPINUP_TIME; /* allow for spinning up */ 807 808 id = usbd_get_interface_descriptor(sc->iface); 809 printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo); 810 811 switch (id->bInterfaceSubClass) { 812 case UISUBCLASS_SCSI: 813 sSubclass = "SCSI"; 814 break; 815 case UISUBCLASS_UFI: 816 sSubclass = "UFI"; 817 break; 818 case UISUBCLASS_SFF8020I: 819 sSubclass = "SFF8020i"; 820 break; 821 case UISUBCLASS_SFF8070I: 822 sSubclass = "SFF8070i"; 823 break; 824 case UISUBCLASS_QIC157: 825 sSubclass = "QIC157"; 826 break; 827 default: 828 sSubclass = "unknown"; 829 break; 830 } 831 switch (id->bInterfaceProtocol) { 832 case UIPROTO_MASS_CBI: 833 sProto = "CBI"; 834 break; 835 case UIPROTO_MASS_CBI_I: 836 sProto = "CBI-I"; 837 break; 838 case UIPROTO_MASS_BBB: 839 sProto = "BBB"; 840 break; 841 case UIPROTO_MASS_BBB_P: 842 sProto = "BBB-P"; 843 break; 844 default: 845 sProto = "unknown"; 846 break; 847 } 848 printf("%s: using %s over %s\n", USBDEVNAME(sc->sc_dev), sSubclass, 849 sProto); 850 851 /* 852 * In addition to the Control endpoint the following endpoints 853 * are required: 854 * a) bulk-in endpoint. 855 * b) bulk-out endpoint. 856 * and for Control/Bulk/Interrupt with CCI (CBI_I) 857 * c) intr-in 858 * 859 * The endpoint addresses are not fixed, so we have to read them 860 * from the device descriptors of the current interface. 861 */ 862 for (i = 0 ; i < id->bNumEndpoints ; i++) { 863 ed = usbd_interface2endpoint_descriptor(sc->iface, i); 864 if (!ed) { 865 printf("%s: could not read endpoint descriptor\n", 866 USBDEVNAME(sc->sc_dev)); 867 USB_ATTACH_ERROR_RETURN; 868 } 869 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN 870 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 871 sc->bulkin = ed->bEndpointAddress; 872 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT 873 && (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) { 874 sc->bulkout = ed->bEndpointAddress; 875 } else if (sc->proto & PROTO_CBI_I 876 && UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN 877 && (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) { 878 sc->intrin = ed->bEndpointAddress; 879 #ifdef UMASS_DEBUG 880 if (UGETW(ed->wMaxPacketSize) > 2) { 881 DPRINTF(UDMASS_CBI, ("%s: intr size is %d\n", 882 USBDEVNAME(sc->sc_dev), 883 UGETW(ed->wMaxPacketSize))); 884 } 885 #endif 886 } 887 } 888 889 /* check whether we found all the endpoints we need */ 890 if (!sc->bulkin || !sc->bulkout 891 || (sc->proto & PROTO_CBI_I && !sc->intrin) ) { 892 DPRINTF(UDMASS_USB, ("%s: endpoint not found %d/%d/%d\n", 893 USBDEVNAME(sc->sc_dev), 894 sc->bulkin, sc->bulkout, sc->intrin)); 895 umass_disco(sc); 896 USB_ATTACH_ERROR_RETURN; 897 } 898 899 /* 900 * Get the maximum LUN supported by the device. 901 */ 902 if ((sc->proto & PROTO_WIRE) == PROTO_BBB) { 903 err = umass_bbb_get_max_lun(sc, &sc->maxlun); 904 if (err) { 905 printf("%s: unable to get Max Lun: %s\n", 906 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 907 USB_ATTACH_ERROR_RETURN; 908 } 909 } else { 910 sc->maxlun = 0; 911 } 912 913 /* Open the bulk-in and -out pipe */ 914 err = usbd_open_pipe(sc->iface, sc->bulkout, 915 USBD_EXCLUSIVE_USE, &sc->bulkout_pipe); 916 if (err) { 917 DPRINTF(UDMASS_USB, ("%s: cannot open %d-out pipe (bulk)\n", 918 USBDEVNAME(sc->sc_dev), sc->bulkout)); 919 umass_disco(sc); 920 USB_ATTACH_ERROR_RETURN; 921 } 922 err = usbd_open_pipe(sc->iface, sc->bulkin, 923 USBD_EXCLUSIVE_USE, &sc->bulkin_pipe); 924 if (err) { 925 DPRINTF(UDMASS_USB, ("%s: could not open %d-in pipe (bulk)\n", 926 USBDEVNAME(sc->sc_dev), sc->bulkin)); 927 umass_disco(sc); 928 USB_ATTACH_ERROR_RETURN; 929 } 930 /* 931 * Open the intr-in pipe if the protocol is CBI with CCI. 932 * Note: early versions of the Zip drive do have an interrupt pipe, but 933 * this pipe is unused 934 * 935 * We do not open the interrupt pipe as an interrupt pipe, but as a 936 * normal bulk endpoint. We send an IN transfer down the wire at the 937 * appropriate time, because we know exactly when to expect data on 938 * that endpoint. This saves bandwidth, but more important, makes the 939 * code for handling the data on that endpoint simpler. No data 940 * arriving concurrently. 941 */ 942 if (sc->proto & PROTO_CBI_I) { 943 err = usbd_open_pipe(sc->iface, sc->intrin, 944 USBD_EXCLUSIVE_USE, &sc->intrin_pipe); 945 if (err) { 946 DPRINTF(UDMASS_USB, ("%s: couldn't open %d-in (intr)\n", 947 USBDEVNAME(sc->sc_dev), sc->intrin)); 948 umass_disco(sc); 949 USB_ATTACH_ERROR_RETURN; 950 } 951 } 952 953 /* initialisation of generic part */ 954 sc->transfer_state = TSTATE_IDLE; 955 956 /* request a sufficient number of xfer handles */ 957 for (i = 0; i < XFER_NR; i++) { 958 sc->transfer_xfer[i] = usbd_alloc_xfer(uaa->device); 959 if (sc->transfer_xfer[i] == 0) { 960 DPRINTF(UDMASS_USB, ("%s: Out of memory\n", 961 USBDEVNAME(sc->sc_dev))); 962 umass_disco(sc); 963 USB_ATTACH_ERROR_RETURN; 964 } 965 } 966 /* Allocate buffer for data transfer (it's huge). */ 967 switch (sc->proto & PROTO_WIRE) { 968 case PROTO_BBB: 969 bno = XFER_BBB_DATA; 970 goto dalloc; 971 case PROTO_CBI: 972 bno = XFER_CBI_DATA; 973 goto dalloc; 974 case PROTO_CBI_I: 975 bno = XFER_CBI_DATA; 976 dalloc: 977 sc->data_buffer = usbd_alloc_buffer(sc->transfer_xfer[bno], 978 UMASS_MAX_TRANSFER_SIZE); 979 if (sc->data_buffer == NULL) { 980 umass_disco(sc); 981 USB_ATTACH_ERROR_RETURN; 982 } 983 break; 984 default: 985 break; 986 } 987 988 /* Initialise the wire protocol specific methods */ 989 if (sc->proto & PROTO_BBB) { 990 sc->reset = umass_bbb_reset; 991 sc->transfer = umass_bbb_transfer; 992 sc->state = umass_bbb_state; 993 } else if ((sc->proto & PROTO_CBI) || (sc->proto & PROTO_CBI_I)) { 994 sc->reset = umass_cbi_reset; 995 sc->transfer = umass_cbi_transfer; 996 sc->state = umass_cbi_state; 997 #ifdef UMASS_DEBUG 998 } else { 999 panic("%s:%d: Unknown proto 0x%02x\n", 1000 __FILE__, __LINE__, sc->proto); 1001 #endif 1002 } 1003 1004 if (sc->drive == SHUTTLE_EUSB) 1005 umass_init_shuttle(sc); 1006 1007 #if defined(__FreeBSD__) 1008 if (sc->proto & PROTO_SCSI) 1009 sc->transform = umass_scsi_transform; 1010 else if (sc->proto & PROTO_UFI) 1011 sc->transform = umass_ufi_transform; 1012 else if (sc->proto & PROTO_ATAPI) 1013 sc->transform = umass_8070_transform; 1014 #ifdef UMASS_DEBUG 1015 else 1016 panic("No transformation defined for command proto 0x%02x\n", 1017 sc->proto & PROTO_COMMAND); 1018 #endif 1019 1020 /* From here onwards the device can be used. */ 1021 1022 if ((sc->proto & PROTO_SCSI) || 1023 (sc->proto & PROTO_ATAPI) || 1024 (sc->proto & PROTO_UFI)) { 1025 /* Prepare the SCSI command block */ 1026 sc->cam_scsi_sense.opcode = REQUEST_SENSE; 1027 1028 /* If this is the first device register the SIM */ 1029 if (umass_sim == NULL) { 1030 err = umass_cam_attach_sim(); 1031 if (err) { 1032 umass_disco(self); 1033 USB_ATTACH_ERROR_RETURN; 1034 } 1035 } 1036 1037 /* Attach the new device to our SCSI host controller (SIM) */ 1038 err = umass_cam_attach(sc); 1039 if (err) { 1040 umass_disco(self); 1041 USB_ATTACH_ERROR_RETURN; 1042 } 1043 } else { 1044 panic("%s:%d: Unknown proto 0x%02x\n", 1045 __FILE__, __LINE__, sc->proto); 1046 } 1047 #elif defined(__NetBSD__) || defined(__OpenBSD__) 1048 /* 1049 * Fill in the adapter. 1050 */ 1051 sc->sc_adapter.scsipi_cmd = umass_scsipi_cmd; 1052 sc->sc_adapter.scsipi_minphys = umass_scsipi_minphys; 1053 sc->sc_adapter.scsipi_ioctl = umass_scsipi_ioctl; 1054 sc->sc_adapter.scsipi_getgeom = umass_scsipi_getgeom; 1055 1056 /* 1057 * fill in the prototype scsipi_link. 1058 */ 1059 switch (sc->proto & PROTO_COMMAND) { 1060 case PROTO_UFI: 1061 sc->u.sc_link.quirks |= SDEV_ONLYBIG; 1062 /* fall into */ 1063 case PROTO_SCSI: 1064 sc->u.sc_link.type = BUS_SCSI; 1065 sc->u.sc_link.scsipi_scsi.channel = SCSI_CHANNEL_ONLY_ONE; 1066 sc->u.sc_link.adapter_softc = sc; 1067 sc->u.sc_link.scsipi_scsi.adapter_target = UMASS_SCSIID_HOST; 1068 sc->u.sc_link.adapter = &sc->sc_adapter; 1069 sc->u.sc_link.device = &umass_dev; 1070 sc->u.sc_link.openings = 1; 1071 sc->u.sc_link.scsipi_scsi.max_target = UMASS_SCSIID_DEVICE; 1072 sc->u.sc_link.scsipi_scsi.max_lun = sc->maxlun; 1073 1074 if (sc->quirks & NO_TEST_UNIT_READY) 1075 sc->u.sc_link.quirks |= ADEV_NOTUR; 1076 break; 1077 1078 #if NATAPIBUS > 0 1079 case PROTO_ATAPI: 1080 sc->u.aa.sc_aa.aa_type = T_ATAPI; 1081 sc->u.aa.sc_aa.aa_channel = 0; 1082 sc->u.aa.sc_aa.aa_openings = 1; 1083 sc->u.aa.sc_aa.aa_drv_data = &sc->u.aa.sc_aa_drive; 1084 sc->u.aa.sc_aa.aa_bus_private = &sc->sc_atapi_adapter; 1085 sc->sc_atapi_adapter.atapi_probedev = umass_atapi_probedev; 1086 sc->sc_atapi_adapter.atapi_kill_pending = scsi_kill_pending; 1087 break; 1088 #endif 1089 1090 default: 1091 printf("%s: proto=0x%x not supported yet\n", 1092 USBDEVNAME(sc->sc_dev), sc->proto); 1093 umass_disco(sc); 1094 USB_ATTACH_ERROR_RETURN; 1095 } 1096 1097 sc->sc_child = config_found(&sc->sc_dev, &sc->u, scsipiprint); 1098 if (sc->sc_child == NULL) { 1099 umass_disco(sc); 1100 /* Not an error, just not a complete success. */ 1101 USB_ATTACH_SUCCESS_RETURN; 1102 } 1103 #endif 1104 1105 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 1106 USBDEV(sc->sc_dev)); 1107 1108 DPRINTF(UDMASS_GEN, ("%s: Attach finished\n", USBDEVNAME(sc->sc_dev))); 1109 1110 USB_ATTACH_SUCCESS_RETURN; 1111 } 1112 1113 Static int 1114 scsipiprint(void *aux, const char *pnp) 1115 { 1116 struct scsipi_link *l = aux; 1117 1118 if (l->type == BUS_SCSI) 1119 return (scsiprint(aux, pnp)); 1120 else { 1121 #if NATAPIBUS > 0 1122 extern int atapi_print(void *aux, const char *pnp); 1123 return (atapi_print(aux, pnp)); 1124 #else 1125 if (pnp) 1126 printf("atapibus at %s", pnp); 1127 return (UNCONF); 1128 #endif 1129 } 1130 } 1131 1132 USB_DETACH(umass) 1133 { 1134 USB_DETACH_START(umass, sc); 1135 int rv = 0; 1136 1137 DPRINTF(UDMASS_USB, ("%s: detached\n", USBDEVNAME(sc->sc_dev))); 1138 1139 /* Abort the pipes to wake up any waiting processes. */ 1140 if (sc->bulkout_pipe != NULL) 1141 usbd_abort_pipe(sc->bulkout_pipe); 1142 if (sc->bulkin_pipe != NULL) 1143 usbd_abort_pipe(sc->bulkin_pipe); 1144 if (sc->intrin_pipe != NULL) 1145 usbd_abort_pipe(sc->intrin_pipe); 1146 1147 #if 0 1148 /* Do we really need referebce counting? Perhaps in ioctl() */ 1149 s = splusb(); 1150 if (--sc->sc_refcnt >= 0) { 1151 /* Wait for processes to go away. */ 1152 usb_detach_wait(USBDEV(sc->sc_dev)); 1153 } 1154 splx(s); 1155 #endif 1156 1157 #if defined(__FreeBSD__) 1158 if ((sc->proto & PROTO_SCSI) || 1159 (sc->proto & PROTO_ATAPI) || 1160 (sc->proto & PROTO_UFI)) 1161 /* detach the device from the SCSI host controller (SIM) */ 1162 rv = umass_cam_detach(sc); 1163 #elif defined(__NetBSD__) || defined(__OpenBSD__) 1164 if (sc->sc_child != NULL) 1165 rv = config_detach(sc->sc_child, flags); 1166 #endif 1167 if (rv != 0) 1168 return (rv); 1169 1170 umass_disco(sc); 1171 1172 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, 1173 USBDEV(sc->sc_dev)); 1174 1175 return (0); 1176 } 1177 1178 #if defined(__NetBSD__) || defined(__OpenBSD__) 1179 int 1180 umass_activate(struct device *self, enum devact act) 1181 { 1182 struct umass_softc *sc = (struct umass_softc *) self; 1183 int rv = 0; 1184 1185 DPRINTF(UDMASS_USB, ("%s: umass_activate: %d\n", 1186 USBDEVNAME(sc->sc_dev), act)); 1187 1188 switch (act) { 1189 case DVACT_ACTIVATE: 1190 rv = EOPNOTSUPP; 1191 break; 1192 1193 case DVACT_DEACTIVATE: 1194 if (sc->sc_child != NULL) 1195 break; 1196 rv = config_deactivate(sc->sc_child); 1197 DPRINTF(UDMASS_USB, ("%s: umass_activate: child " 1198 "returned %d\n", USBDEVNAME(sc->sc_dev), rv)); 1199 if (rv == 0) 1200 sc->sc_dying = 1; 1201 break; 1202 } 1203 return (rv); 1204 } 1205 #endif 1206 1207 Static void 1208 umass_disco(struct umass_softc *sc) 1209 { 1210 int i; 1211 1212 DPRINTF(UDMASS_GEN, ("umass_disco\n")); 1213 1214 /* Free the xfers. */ 1215 for (i = 0; i < XFER_NR; i++) 1216 if (sc->transfer_xfer[i] != NULL) { 1217 usbd_free_xfer(sc->transfer_xfer[i]); 1218 sc->transfer_xfer[i] = NULL; 1219 } 1220 1221 /* Remove all the pipes. */ 1222 if (sc->bulkout_pipe != NULL) 1223 usbd_close_pipe(sc->bulkout_pipe); 1224 if (sc->bulkin_pipe != NULL) 1225 usbd_close_pipe(sc->bulkin_pipe); 1226 if (sc->intrin_pipe != NULL) 1227 usbd_close_pipe(sc->intrin_pipe); 1228 } 1229 1230 Static void 1231 umass_init_shuttle(struct umass_softc *sc) 1232 { 1233 usb_device_request_t req; 1234 u_char status[2]; 1235 1236 /* The Linux driver does this */ 1237 req.bmRequestType = UT_READ_VENDOR_DEVICE; 1238 req.bRequest = 1; 1239 USETW(req.wValue, 0); 1240 USETW(req.wIndex, sc->ifaceno); 1241 USETW(req.wLength, sizeof status); 1242 (void)usbd_do_request(sc->sc_udev, &req, &status); 1243 } 1244 1245 /* 1246 * Generic functions to handle transfers 1247 */ 1248 1249 Static usbd_status 1250 umass_setup_transfer(struct umass_softc *sc, usbd_pipe_handle pipe, 1251 void *buffer, int buflen, int flags, 1252 usbd_xfer_handle xfer) 1253 { 1254 usbd_status err; 1255 1256 if (sc->sc_dying) 1257 return (USBD_IOERROR); 1258 1259 /* Initialiase a USB transfer and then schedule it */ 1260 1261 usbd_setup_xfer(xfer, pipe, (void *)sc, buffer, buflen, 1262 flags | sc->sc_xfer_flags, sc->timeout, sc->state); 1263 1264 err = usbd_transfer(xfer); 1265 DPRINTF(UDMASS_XFER,("%s: start xfer buffer=%p buflen=%d flags=0x%x " 1266 "timeout=%d\n", USBDEVNAME(sc->sc_dev), 1267 buffer, buflen, flags | sc->sc_xfer_flags, sc->timeout)); 1268 if (err && err != USBD_IN_PROGRESS) { 1269 DPRINTF(UDMASS_BBB, ("%s: failed to setup transfer, %s\n", 1270 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 1271 return (err); 1272 } 1273 1274 return (USBD_NORMAL_COMPLETION); 1275 } 1276 1277 1278 Static usbd_status 1279 umass_setup_ctrl_transfer(struct umass_softc *sc, usbd_device_handle dev, 1280 usb_device_request_t *req, 1281 void *buffer, int buflen, int flags, 1282 usbd_xfer_handle xfer) 1283 { 1284 usbd_status err; 1285 1286 if (sc->sc_dying) 1287 return (USBD_IOERROR); 1288 1289 /* Initialiase a USB control transfer and then schedule it */ 1290 1291 usbd_setup_default_xfer(xfer, dev, (void *) sc, 1292 sc->timeout, req, buffer, buflen, flags, sc->state); 1293 1294 err = usbd_transfer(xfer); 1295 if (err && err != USBD_IN_PROGRESS) { 1296 DPRINTF(UDMASS_BBB, ("%s: failed to setup ctrl transfer, %s\n", 1297 USBDEVNAME(sc->sc_dev), usbd_errstr(err))); 1298 1299 /* do not reset, as this would make us loop */ 1300 return (err); 1301 } 1302 1303 return (USBD_NORMAL_COMPLETION); 1304 } 1305 1306 Static void 1307 umass_clear_endpoint_stall(struct umass_softc *sc, 1308 u_int8_t endpt, usbd_pipe_handle pipe, 1309 int state, usbd_xfer_handle xfer) 1310 { 1311 usbd_device_handle dev; 1312 1313 if (sc->sc_dying) 1314 return; 1315 1316 DPRINTF(UDMASS_BBB, ("%s: Clear endpoint 0x%02x stall\n", 1317 USBDEVNAME(sc->sc_dev), endpt)); 1318 1319 usbd_interface2device_handle(sc->iface, &dev); 1320 1321 sc->transfer_state = state; 1322 1323 usbd_clear_endpoint_toggle(pipe); 1324 1325 sc->request.bmRequestType = UT_WRITE_ENDPOINT; 1326 sc->request.bRequest = UR_CLEAR_FEATURE; 1327 USETW(sc->request.wValue, UF_ENDPOINT_HALT); 1328 USETW(sc->request.wIndex, endpt); 1329 USETW(sc->request.wLength, 0); 1330 umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, xfer); 1331 } 1332 1333 #if 0 1334 Static void 1335 umass_reset(struct umass_softc *sc, transfer_cb_f cb, void *priv) 1336 { 1337 sc->transfer_cb = cb; 1338 sc->transfer_priv = priv; 1339 1340 /* The reset is a forced reset, so no error (yet) */ 1341 sc->reset(sc, STATUS_CMD_OK); 1342 } 1343 #endif 1344 1345 /* 1346 * Bulk protocol specific functions 1347 */ 1348 1349 Static void 1350 umass_bbb_reset(struct umass_softc *sc, int status) 1351 { 1352 usbd_device_handle dev; 1353 1354 KASSERT(sc->proto & PROTO_BBB, 1355 ("sc->proto == 0x%02x wrong for umass_bbb_reset\n", sc->proto)); 1356 1357 if (sc->sc_dying) 1358 return; 1359 1360 /* 1361 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class) 1362 * 1363 * For Reset Recovery the host shall issue in the following order: 1364 * a) a Bulk-Only Mass Storage Reset 1365 * b) a Clear Feature HALT to the Bulk-In endpoint 1366 * c) a Clear Feature HALT to the Bulk-Out endpoint 1367 * 1368 * This is done in 3 steps, states: 1369 * TSTATE_BBB_RESET1 1370 * TSTATE_BBB_RESET2 1371 * TSTATE_BBB_RESET3 1372 * 1373 * If the reset doesn't succeed, the device should be port reset. 1374 */ 1375 1376 DPRINTF(UDMASS_BBB, ("%s: Bulk Reset\n", 1377 USBDEVNAME(sc->sc_dev))); 1378 1379 sc->transfer_state = TSTATE_BBB_RESET1; 1380 sc->transfer_status = status; 1381 1382 usbd_interface2device_handle(sc->iface, &dev); 1383 1384 /* reset is a class specific interface write */ 1385 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1386 sc->request.bRequest = UR_BBB_RESET; 1387 USETW(sc->request.wValue, 0); 1388 USETW(sc->request.wIndex, sc->ifaceno); 1389 USETW(sc->request.wLength, 0); 1390 umass_setup_ctrl_transfer(sc, dev, &sc->request, NULL, 0, 0, 1391 sc->transfer_xfer[XFER_BBB_RESET1]); 1392 } 1393 1394 Static void 1395 umass_bbb_transfer(struct umass_softc *sc, int lun, void *cmd, int cmdlen, 1396 void *data, int datalen, int dir, 1397 transfer_cb_f cb, void *priv) 1398 { 1399 static int dCBWtag = 42; /* unique for CBW of transfer */ 1400 1401 DPRINTF(UDMASS_BBB,("%s: umass_bbb_transfer cmd=0x%02x\n", 1402 USBDEVNAME(sc->sc_dev), *(u_char*)cmd)); 1403 1404 KASSERT(sc->proto & PROTO_BBB, 1405 ("sc->proto == 0x%02x wrong for umass_bbb_transfer\n", 1406 sc->proto)); 1407 1408 /* 1409 * Do a Bulk-Only transfer with cmdlen bytes from cmd, possibly 1410 * a data phase of datalen bytes from/to the device and finally a 1411 * csw read phase. 1412 * If the data direction was inbound a maximum of datalen bytes 1413 * is stored in the buffer pointed to by data. 1414 * 1415 * umass_bbb_transfer initialises the transfer and lets the state 1416 * machine in umass_bbb_state handle the completion. It uses the 1417 * following states: 1418 * TSTATE_BBB_COMMAND 1419 * -> TSTATE_BBB_DATA 1420 * -> TSTATE_BBB_STATUS 1421 * -> TSTATE_BBB_STATUS2 1422 * -> TSTATE_BBB_IDLE 1423 * 1424 * An error in any of those states will invoke 1425 * umass_bbb_reset. 1426 */ 1427 1428 /* check the given arguments */ 1429 KASSERT(datalen == 0 || data != NULL, 1430 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev))); 1431 KASSERT(cmdlen <= CBWCDBLENGTH, 1432 ("%s: cmdlen exceeds CDB length in CBW (%d > %d)", 1433 USBDEVNAME(sc->sc_dev), cmdlen, CBWCDBLENGTH)); 1434 KASSERT(dir == DIR_NONE || datalen > 0, 1435 ("%s: datalen == 0 while direction is not NONE\n", 1436 USBDEVNAME(sc->sc_dev))); 1437 KASSERT(datalen == 0 || dir != DIR_NONE, 1438 ("%s: direction is NONE while datalen is not zero\n", 1439 USBDEVNAME(sc->sc_dev))); 1440 KASSERT(sizeof(umass_bbb_cbw_t) == UMASS_BBB_CBW_SIZE, 1441 ("%s: CBW struct does not have the right size (%d vs. %d)\n", 1442 USBDEVNAME(sc->sc_dev), 1443 sizeof(umass_bbb_cbw_t), UMASS_BBB_CBW_SIZE)); 1444 KASSERT(sizeof(umass_bbb_csw_t) == UMASS_BBB_CSW_SIZE, 1445 ("%s: CSW struct does not have the right size (%d vs. %d)\n", 1446 USBDEVNAME(sc->sc_dev), 1447 sizeof(umass_bbb_csw_t), UMASS_BBB_CSW_SIZE)); 1448 1449 /* 1450 * Determine the direction of the data transfer and the length. 1451 * 1452 * dCBWDataTransferLength (datalen) : 1453 * This field indicates the number of bytes of data that the host 1454 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by 1455 * the Direction bit) during the execution of this command. If this 1456 * field is set to 0, the device will expect that no data will be 1457 * transferred IN or OUT during this command, regardless of the value 1458 * of the Direction bit defined in dCBWFlags. 1459 * 1460 * dCBWFlags (dir) : 1461 * The bits of the Flags field are defined as follows: 1462 * Bits 0-6 reserved 1463 * Bit 7 Direction - this bit shall be ignored if the 1464 * dCBWDataTransferLength field is zero. 1465 * 0 = data Out from host to device 1466 * 1 = data In from device to host 1467 */ 1468 1469 /* Fill in the Command Block Wrapper */ 1470 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE); 1471 USETDW(sc->cbw.dCBWTag, dCBWtag); 1472 dCBWtag++; /* cannot be done in macro (it will be done 4 times) */ 1473 USETDW(sc->cbw.dCBWDataTransferLength, datalen); 1474 /* DIR_NONE is treated as DIR_OUT (0x00) */ 1475 sc->cbw.bCBWFlags = (dir == DIR_IN? CBWFLAGS_IN:CBWFLAGS_OUT); 1476 sc->cbw.bCBWLUN = lun; 1477 sc->cbw.bCDBLength = cmdlen; 1478 bcopy(cmd, sc->cbw.CBWCDB, cmdlen); 1479 1480 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw)); 1481 1482 /* store the details for the data transfer phase */ 1483 sc->transfer_dir = dir; 1484 sc->transfer_data = data; 1485 sc->transfer_datalen = datalen; 1486 sc->transfer_actlen = 0; 1487 sc->transfer_cb = cb; 1488 sc->transfer_priv = priv; 1489 sc->transfer_status = STATUS_CMD_OK; 1490 1491 /* move from idle to the command state */ 1492 sc->transfer_state = TSTATE_BBB_COMMAND; 1493 1494 /* Send the CBW from host to device via bulk-out endpoint. */ 1495 if (umass_setup_transfer(sc, sc->bulkout_pipe, 1496 &sc->cbw, UMASS_BBB_CBW_SIZE, 0, 1497 sc->transfer_xfer[XFER_BBB_CBW])) { 1498 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1499 } 1500 } 1501 1502 1503 Static void 1504 umass_bbb_state(usbd_xfer_handle xfer, usbd_private_handle priv, 1505 usbd_status err) 1506 { 1507 struct umass_softc *sc = (struct umass_softc *) priv; 1508 usbd_xfer_handle next_xfer; 1509 1510 KASSERT(sc->proto & PROTO_BBB, 1511 ("sc->proto == 0x%02x wrong for umass_bbb_state\n",sc->proto)); 1512 1513 if (sc->sc_dying) 1514 return; 1515 1516 /* 1517 * State handling for BBB transfers. 1518 * 1519 * The subroutine is rather long. It steps through the states given in 1520 * Annex A of the Bulk-Only specification. 1521 * Each state first does the error handling of the previous transfer 1522 * and then prepares the next transfer. 1523 * Each transfer is done asynchroneously so after the request/transfer 1524 * has been submitted you will find a 'return;'. 1525 */ 1526 1527 DPRINTF(UDMASS_BBB, ("%s: Handling BBB state %d (%s), xfer=%p, %s\n", 1528 USBDEVNAME(sc->sc_dev), sc->transfer_state, 1529 states[sc->transfer_state], xfer, usbd_errstr(err))); 1530 1531 switch (sc->transfer_state) { 1532 1533 /***** Bulk Transfer *****/ 1534 case TSTATE_BBB_COMMAND: 1535 /* Command transport phase, error handling */ 1536 if (err) { 1537 DPRINTF(UDMASS_BBB, ("%s: failed to send CBW\n", 1538 USBDEVNAME(sc->sc_dev))); 1539 /* If the device detects that the CBW is invalid, then 1540 * the device may STALL both bulk endpoints and require 1541 * a Bulk-Reset 1542 */ 1543 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1544 return; 1545 } 1546 1547 /* Data transport phase, setup transfer */ 1548 sc->transfer_state = TSTATE_BBB_DATA; 1549 if (sc->transfer_dir == DIR_IN) { 1550 if (umass_setup_transfer(sc, sc->bulkin_pipe, 1551 sc->data_buffer, sc->transfer_datalen, 1552 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1553 sc->transfer_xfer[XFER_BBB_DATA])) 1554 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1555 1556 return; 1557 } else if (sc->transfer_dir == DIR_OUT) { 1558 memcpy(sc->data_buffer, sc->transfer_data, 1559 sc->transfer_datalen); 1560 if (umass_setup_transfer(sc, sc->bulkout_pipe, 1561 sc->data_buffer, sc->transfer_datalen, 1562 USBD_NO_COPY,/* fixed length transfer */ 1563 sc->transfer_xfer[XFER_BBB_DATA])) 1564 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1565 1566 return; 1567 } else { 1568 DPRINTF(UDMASS_BBB, ("%s: no data phase\n", 1569 USBDEVNAME(sc->sc_dev))); 1570 } 1571 1572 /* FALLTHROUGH if no data phase, err == 0 */ 1573 case TSTATE_BBB_DATA: 1574 /* Command transport phase, error handling (ignored if no data 1575 * phase (fallthrough from previous state)) */ 1576 if (sc->transfer_dir != DIR_NONE) { 1577 /* retrieve the length of the transfer that was done */ 1578 usbd_get_xfer_status(xfer, NULL, NULL, 1579 &sc->transfer_actlen, NULL); 1580 1581 if (err) { 1582 DPRINTF(UDMASS_BBB, ("%s: Data-%s %db failed, " 1583 "%s\n", USBDEVNAME(sc->sc_dev), 1584 (sc->transfer_dir == DIR_IN?"in":"out"), 1585 sc->transfer_datalen,usbd_errstr(err))); 1586 1587 if (err == USBD_STALLED) { 1588 umass_clear_endpoint_stall(sc, 1589 (sc->transfer_dir == DIR_IN? 1590 sc->bulkin:sc->bulkout), 1591 (sc->transfer_dir == DIR_IN? 1592 sc->bulkin_pipe:sc->bulkout_pipe), 1593 TSTATE_BBB_DCLEAR, 1594 sc->transfer_xfer[XFER_BBB_DCLEAR]); 1595 return; 1596 } else { 1597 /* Unless the error is a pipe stall the 1598 * error is fatal. 1599 */ 1600 umass_bbb_reset(sc,STATUS_WIRE_FAILED); 1601 return; 1602 } 1603 } 1604 } 1605 1606 if (sc->transfer_dir == DIR_IN) 1607 memcpy(sc->transfer_data, sc->data_buffer, 1608 sc->transfer_actlen); 1609 1610 DIF(UDMASS_BBB, if (sc->transfer_dir == DIR_IN) 1611 umass_dump_buffer(sc, sc->transfer_data, 1612 sc->transfer_datalen, 48)); 1613 1614 /* FALLTHROUGH, err == 0 (no data phase or successfull) */ 1615 case TSTATE_BBB_DCLEAR: /* stall clear after data phase */ 1616 case TSTATE_BBB_SCLEAR: /* stall clear after status phase */ 1617 /* Reading of CSW after bulk stall condition in data phase 1618 * (TSTATE_BBB_DATA2) or bulk-in stall condition after 1619 * reading CSW (TSTATE_BBB_SCLEAR). 1620 * In the case of no data phase or successfull data phase, 1621 * err == 0 and the following if block is passed. 1622 */ 1623 if (err) { /* should not occur */ 1624 /* try the transfer below, even if clear stall failed */ 1625 DPRINTF(UDMASS_BBB, ("%s: bulk-%s stall clear failed" 1626 ", %s\n", USBDEVNAME(sc->sc_dev), 1627 (sc->transfer_dir == DIR_IN? "in":"out"), 1628 usbd_errstr(err))); 1629 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1630 return; 1631 } 1632 1633 /* Status transport phase, setup transfer */ 1634 if (sc->transfer_state == TSTATE_BBB_COMMAND || 1635 sc->transfer_state == TSTATE_BBB_DATA || 1636 sc->transfer_state == TSTATE_BBB_DCLEAR) { 1637 /* After no data phase, successfull data phase and 1638 * after clearing bulk-in/-out stall condition 1639 */ 1640 sc->transfer_state = TSTATE_BBB_STATUS1; 1641 next_xfer = sc->transfer_xfer[XFER_BBB_CSW1]; 1642 } else { 1643 /* After first attempt of fetching CSW */ 1644 sc->transfer_state = TSTATE_BBB_STATUS2; 1645 next_xfer = sc->transfer_xfer[XFER_BBB_CSW2]; 1646 } 1647 1648 /* Read the Command Status Wrapper via bulk-in endpoint. */ 1649 if (umass_setup_transfer(sc, sc->bulkin_pipe, 1650 &sc->csw, UMASS_BBB_CSW_SIZE, 0, 1651 next_xfer)) { 1652 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1653 return; 1654 } 1655 1656 return; 1657 case TSTATE_BBB_STATUS1: /* first attempt */ 1658 case TSTATE_BBB_STATUS2: /* second attempt */ 1659 /* Status transfer, error handling */ 1660 if (err) { 1661 DPRINTF(UDMASS_BBB, ("%s: Failed to read CSW, %s%s\n", 1662 USBDEVNAME(sc->sc_dev), usbd_errstr(err), 1663 (sc->transfer_state == TSTATE_BBB_STATUS1? 1664 ", retrying":""))); 1665 1666 /* If this was the first attempt at fetching the CSW 1667 * retry it, otherwise fail. 1668 */ 1669 if (sc->transfer_state == TSTATE_BBB_STATUS1) { 1670 umass_clear_endpoint_stall(sc, 1671 sc->bulkin, sc->bulkin_pipe, 1672 TSTATE_BBB_SCLEAR, 1673 sc->transfer_xfer[XFER_BBB_SCLEAR]); 1674 return; 1675 } else { 1676 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1677 return; 1678 } 1679 } 1680 1681 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw)); 1682 1683 /* Check CSW and handle any error */ 1684 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) { 1685 /* Invalid CSW: Wrong signature or wrong tag might 1686 * indicate that the device is confused -> reset it. 1687 */ 1688 printf("%s: Invalid CSW: sig 0x%08x should be 0x%08x\n", 1689 USBDEVNAME(sc->sc_dev), 1690 UGETDW(sc->csw.dCSWSignature), 1691 CSWSIGNATURE); 1692 1693 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1694 return; 1695 } else if (UGETDW(sc->csw.dCSWTag) 1696 != UGETDW(sc->cbw.dCBWTag)) { 1697 printf("%s: Invalid CSW: tag %d should be %d\n", 1698 USBDEVNAME(sc->sc_dev), 1699 UGETDW(sc->csw.dCSWTag), 1700 UGETDW(sc->cbw.dCBWTag)); 1701 1702 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1703 return; 1704 1705 /* CSW is valid here */ 1706 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) { 1707 printf("%s: Invalid CSW: status %d > %d\n", 1708 USBDEVNAME(sc->sc_dev), 1709 sc->csw.bCSWStatus, 1710 CSWSTATUS_PHASE); 1711 1712 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1713 return; 1714 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) { 1715 printf("%s: Phase Error, residue = %d\n", 1716 USBDEVNAME(sc->sc_dev), 1717 UGETDW(sc->csw.dCSWDataResidue)); 1718 1719 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1720 return; 1721 1722 } else if (sc->transfer_actlen > sc->transfer_datalen) { 1723 /* Buffer overrun! Don't let this go by unnoticed */ 1724 panic("%s: transferred %d bytes instead of %d bytes\n", 1725 USBDEVNAME(sc->sc_dev), 1726 sc->transfer_actlen, sc->transfer_datalen); 1727 } else if (sc->transfer_datalen - sc->transfer_actlen 1728 != UGETDW(sc->csw.dCSWDataResidue)) { 1729 DPRINTF(UDMASS_BBB, ("%s: actlen=%d != residue=%d\n", 1730 USBDEVNAME(sc->sc_dev), 1731 sc->transfer_datalen - sc->transfer_actlen, 1732 UGETDW(sc->csw.dCSWDataResidue))); 1733 1734 umass_bbb_reset(sc, STATUS_WIRE_FAILED); 1735 return; 1736 1737 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) { 1738 DPRINTF(UDMASS_BBB, ("%s: Command Failed, res = %d\n", 1739 USBDEVNAME(sc->sc_dev), 1740 UGETDW(sc->csw.dCSWDataResidue))); 1741 1742 /* SCSI command failed but transfer was succesful */ 1743 sc->transfer_state = TSTATE_IDLE; 1744 sc->transfer_cb(sc, sc->transfer_priv, 1745 UGETDW(sc->csw.dCSWDataResidue), 1746 STATUS_CMD_FAILED); 1747 1748 return; 1749 1750 } else { /* success */ 1751 sc->transfer_state = TSTATE_IDLE; 1752 sc->transfer_cb(sc, sc->transfer_priv, 1753 UGETDW(sc->csw.dCSWDataResidue), 1754 STATUS_CMD_OK); 1755 1756 return; 1757 } 1758 1759 /***** Bulk Reset *****/ 1760 case TSTATE_BBB_RESET1: 1761 if (err) 1762 printf("%s: BBB reset failed, %s\n", 1763 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1764 1765 umass_clear_endpoint_stall(sc, 1766 sc->bulkin, sc->bulkin_pipe, TSTATE_BBB_RESET2, 1767 sc->transfer_xfer[XFER_BBB_RESET2]); 1768 1769 return; 1770 case TSTATE_BBB_RESET2: 1771 if (err) /* should not occur */ 1772 printf("%s: BBB bulk-in clear stall failed, %s\n", 1773 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1774 /* no error recovery, otherwise we end up in a loop */ 1775 1776 umass_clear_endpoint_stall(sc, 1777 sc->bulkout, sc->bulkout_pipe, TSTATE_BBB_RESET3, 1778 sc->transfer_xfer[XFER_BBB_RESET3]); 1779 1780 return; 1781 case TSTATE_BBB_RESET3: 1782 if (err) /* should not occur */ 1783 printf("%s: BBB bulk-out clear stall failed, %s\n", 1784 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 1785 /* no error recovery, otherwise we end up in a loop */ 1786 1787 sc->transfer_state = TSTATE_IDLE; 1788 if (sc->transfer_priv) { 1789 sc->transfer_cb(sc, sc->transfer_priv, 1790 sc->transfer_datalen, 1791 sc->transfer_status); 1792 } 1793 1794 return; 1795 1796 /***** Default *****/ 1797 default: 1798 panic("%s: Unknown state %d\n", 1799 USBDEVNAME(sc->sc_dev), sc->transfer_state); 1800 } 1801 } 1802 1803 /* 1804 * Command/Bulk/Interrupt (CBI) specific functions 1805 */ 1806 1807 Static int 1808 umass_cbi_adsc(struct umass_softc *sc, char *buffer, int buflen, 1809 usbd_xfer_handle xfer) 1810 { 1811 usbd_device_handle dev; 1812 1813 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), 1814 ("sc->proto == 0x%02x wrong for umass_cbi_adsc\n",sc->proto)); 1815 1816 usbd_interface2device_handle(sc->iface, &dev); 1817 1818 sc->request.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1819 sc->request.bRequest = UR_CBI_ADSC; 1820 USETW(sc->request.wValue, 0); 1821 USETW(sc->request.wIndex, sc->ifaceno); 1822 USETW(sc->request.wLength, buflen); 1823 return umass_setup_ctrl_transfer(sc, dev, &sc->request, buffer, 1824 buflen, 0, xfer); 1825 } 1826 1827 1828 Static void 1829 umass_cbi_reset(struct umass_softc *sc, int status) 1830 { 1831 int i; 1832 # define SEND_DIAGNOSTIC_CMDLEN 12 1833 1834 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), 1835 ("sc->proto == 0x%02x wrong for umass_cbi_reset\n",sc->proto)); 1836 1837 if (sc->sc_dying) 1838 return; 1839 1840 /* 1841 * Command Block Reset Protocol 1842 * 1843 * First send a reset request to the device. Then clear 1844 * any possibly stalled bulk endpoints. 1845 1846 * This is done in 3 steps, states: 1847 * TSTATE_CBI_RESET1 1848 * TSTATE_CBI_RESET2 1849 * TSTATE_CBI_RESET3 1850 * 1851 * If the reset doesn't succeed, the device should be port reset. 1852 */ 1853 1854 DPRINTF(UDMASS_CBI, ("%s: CBI Reset\n", 1855 USBDEVNAME(sc->sc_dev))); 1856 1857 KASSERT(sizeof(sc->cbl) >= SEND_DIAGNOSTIC_CMDLEN, 1858 ("%s: CBL struct is too small (%d < %d)\n", 1859 USBDEVNAME(sc->sc_dev), 1860 sizeof(sc->cbl), SEND_DIAGNOSTIC_CMDLEN)); 1861 1862 sc->transfer_state = TSTATE_CBI_RESET1; 1863 sc->transfer_status = status; 1864 1865 /* The 0x1d code is the SEND DIAGNOSTIC command. To distingiush between 1866 * the two the last 10 bytes of the cbl is filled with 0xff (section 1867 * 2.2 of the CBI spec). 1868 */ 1869 sc->cbl[0] = 0x1d; /* Command Block Reset */ 1870 sc->cbl[1] = 0x04; 1871 for (i = 2; i < SEND_DIAGNOSTIC_CMDLEN; i++) 1872 sc->cbl[i] = 0xff; 1873 1874 umass_cbi_adsc(sc, sc->cbl, SEND_DIAGNOSTIC_CMDLEN, 1875 sc->transfer_xfer[XFER_CBI_RESET1]); 1876 /* XXX if the command fails we should reset the port on the bub */ 1877 } 1878 1879 Static void 1880 umass_cbi_transfer(struct umass_softc *sc, int lun, 1881 void *cmd, int cmdlen, void *data, int datalen, int dir, 1882 transfer_cb_f cb, void *priv) 1883 { 1884 DPRINTF(UDMASS_CBI,("%s: umass_cbi_transfer cmd=0x%02x, len=%d\n", 1885 USBDEVNAME(sc->sc_dev), *(u_char*)cmd, datalen)); 1886 1887 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), 1888 ("sc->proto == 0x%02x wrong for umass_cbi_transfer\n", 1889 sc->proto)); 1890 1891 if (sc->sc_dying) 1892 return; 1893 1894 /* 1895 * Do a CBI transfer with cmdlen bytes from cmd, possibly 1896 * a data phase of datalen bytes from/to the device and finally a 1897 * csw read phase. 1898 * If the data direction was inbound a maximum of datalen bytes 1899 * is stored in the buffer pointed to by data. 1900 * 1901 * umass_cbi_transfer initialises the transfer and lets the state 1902 * machine in umass_cbi_state handle the completion. It uses the 1903 * following states: 1904 * TSTATE_CBI_COMMAND 1905 * -> XXX fill in 1906 * 1907 * An error in any of those states will invoke 1908 * umass_cbi_reset. 1909 */ 1910 1911 /* check the given arguments */ 1912 KASSERT(datalen == 0 || data != NULL, 1913 ("%s: datalen > 0, but no buffer",USBDEVNAME(sc->sc_dev))); 1914 KASSERT(datalen == 0 || dir != DIR_NONE, 1915 ("%s: direction is NONE while datalen is not zero\n", 1916 USBDEVNAME(sc->sc_dev))); 1917 1918 /* store the details for the data transfer phase */ 1919 sc->transfer_dir = dir; 1920 sc->transfer_data = data; 1921 sc->transfer_datalen = datalen; 1922 sc->transfer_actlen = 0; 1923 sc->transfer_cb = cb; 1924 sc->transfer_priv = priv; 1925 sc->transfer_status = STATUS_CMD_OK; 1926 1927 /* move from idle to the command state */ 1928 sc->transfer_state = TSTATE_CBI_COMMAND; 1929 1930 /* Send the Command Block from host to device via control endpoint. */ 1931 if (umass_cbi_adsc(sc, cmd, cmdlen, sc->transfer_xfer[XFER_CBI_CB])) 1932 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1933 } 1934 1935 Static void 1936 umass_cbi_state(usbd_xfer_handle xfer, usbd_private_handle priv, 1937 usbd_status err) 1938 { 1939 struct umass_softc *sc = (struct umass_softc *) priv; 1940 1941 KASSERT(sc->proto & (PROTO_CBI|PROTO_CBI_I), 1942 ("sc->proto == 0x%02x wrong for umass_cbi_state\n", sc->proto)); 1943 1944 if (sc->sc_dying) 1945 return; 1946 1947 /* 1948 * State handling for CBI transfers. 1949 */ 1950 1951 DPRINTF(UDMASS_CBI, ("%s: Handling CBI state %d (%s), xfer=%p, %s\n", 1952 USBDEVNAME(sc->sc_dev), sc->transfer_state, 1953 states[sc->transfer_state], xfer, usbd_errstr(err))); 1954 1955 switch (sc->transfer_state) { 1956 1957 /***** CBI Transfer *****/ 1958 case TSTATE_CBI_COMMAND: 1959 if (err == USBD_STALLED) { 1960 DPRINTF(UDMASS_CBI, ("%s: Command Transport failed\n", 1961 USBDEVNAME(sc->sc_dev))); 1962 /* Status transport by control pipe (section 2.3.2.1). 1963 * The command contained in the command block failed. 1964 * 1965 * The control pipe has already been unstalled by the 1966 * USB stack. 1967 * Section 2.4.3.1.1 states that the bulk in endpoints 1968 * should not stalled at this point. 1969 */ 1970 1971 sc->transfer_state = TSTATE_IDLE; 1972 sc->transfer_cb(sc, sc->transfer_priv, 1973 sc->transfer_datalen, 1974 STATUS_CMD_FAILED); 1975 1976 return; 1977 } else if (err) { 1978 DPRINTF(UDMASS_CBI, ("%s: failed to send ADSC\n", 1979 USBDEVNAME(sc->sc_dev))); 1980 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1981 1982 return; 1983 } 1984 1985 sc->transfer_state = TSTATE_CBI_DATA; 1986 if (sc->transfer_dir == DIR_IN) { 1987 if (umass_setup_transfer(sc, sc->bulkin_pipe, 1988 sc->transfer_data, sc->transfer_datalen, 1989 USBD_SHORT_XFER_OK | USBD_NO_COPY, 1990 sc->transfer_xfer[XFER_CBI_DATA])) 1991 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 1992 1993 } else if (sc->transfer_dir == DIR_OUT) { 1994 memcpy(sc->data_buffer, sc->transfer_data, 1995 sc->transfer_datalen); 1996 if (umass_setup_transfer(sc, sc->bulkout_pipe, 1997 sc->transfer_data, sc->transfer_datalen, 1998 USBD_NO_COPY,/* fixed length transfer */ 1999 sc->transfer_xfer[XFER_CBI_DATA])) 2000 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2001 2002 } else if (sc->proto & PROTO_CBI_I) { 2003 DPRINTF(UDMASS_CBI, ("%s: no data phase\n", 2004 USBDEVNAME(sc->sc_dev))); 2005 sc->transfer_state = TSTATE_CBI_STATUS; 2006 if (umass_setup_transfer(sc, sc->intrin_pipe, 2007 &sc->sbl, sizeof(sc->sbl), 2008 0, /* fixed length transfer */ 2009 sc->transfer_xfer[XFER_CBI_STATUS])){ 2010 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2011 } 2012 } else { 2013 DPRINTF(UDMASS_CBI, ("%s: no data phase\n", 2014 USBDEVNAME(sc->sc_dev))); 2015 /* No command completion interrupt. Request 2016 * sense data. 2017 */ 2018 sc->transfer_state = TSTATE_IDLE; 2019 sc->transfer_cb(sc, sc->transfer_priv, 2020 0, STATUS_CMD_UNKNOWN); 2021 } 2022 2023 return; 2024 2025 case TSTATE_CBI_DATA: 2026 /* retrieve the length of the transfer that was done */ 2027 usbd_get_xfer_status(xfer,NULL,NULL,&sc->transfer_actlen,NULL); 2028 DPRINTF(UDMASS_CBI, ("%s: CBI_DATA actlen=%d\n", 2029 USBDEVNAME(sc->sc_dev), sc->transfer_actlen)); 2030 2031 if (err) { 2032 DPRINTF(UDMASS_CBI, ("%s: Data-%s %db failed, " 2033 "%s\n", USBDEVNAME(sc->sc_dev), 2034 (sc->transfer_dir == DIR_IN?"in":"out"), 2035 sc->transfer_datalen,usbd_errstr(err))); 2036 2037 if (err == USBD_STALLED) { 2038 umass_clear_endpoint_stall(sc, 2039 sc->bulkin, sc->bulkin_pipe, 2040 TSTATE_CBI_DCLEAR, 2041 sc->transfer_xfer[XFER_CBI_DCLEAR]); 2042 } else { 2043 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2044 } 2045 return; 2046 } 2047 2048 if (sc->transfer_dir == DIR_IN) 2049 memcpy(sc->transfer_data, sc->data_buffer, 2050 sc->transfer_actlen); 2051 2052 DIF(UDMASS_CBI, if (sc->transfer_dir == DIR_IN) 2053 umass_dump_buffer(sc, sc->transfer_data, 2054 sc->transfer_actlen, 48)); 2055 2056 if (sc->proto & PROTO_CBI_I) { 2057 sc->transfer_state = TSTATE_CBI_STATUS; 2058 memset(&sc->sbl, 0, sizeof(sc->sbl)); 2059 if (umass_setup_transfer(sc, sc->intrin_pipe, 2060 &sc->sbl, sizeof(sc->sbl), 2061 0, /* fixed length transfer */ 2062 sc->transfer_xfer[XFER_CBI_STATUS])){ 2063 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2064 } 2065 } else { 2066 /* No command completion interrupt. Request 2067 * sense to get status of command. 2068 */ 2069 sc->transfer_state = TSTATE_IDLE; 2070 sc->transfer_cb(sc, sc->transfer_priv, 2071 sc->transfer_datalen - sc->transfer_actlen, 2072 STATUS_CMD_UNKNOWN); 2073 } 2074 return; 2075 2076 case TSTATE_CBI_STATUS: 2077 if (err) { 2078 DPRINTF(UDMASS_CBI, ("%s: Status Transport failed\n", 2079 USBDEVNAME(sc->sc_dev))); 2080 /* Status transport by interrupt pipe (section 2.3.2.2). 2081 */ 2082 2083 if (err == USBD_STALLED) { 2084 umass_clear_endpoint_stall(sc, 2085 sc->intrin, sc->intrin_pipe, 2086 TSTATE_CBI_SCLEAR, 2087 sc->transfer_xfer[XFER_CBI_SCLEAR]); 2088 } else { 2089 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2090 } 2091 return; 2092 } 2093 2094 /* Dissect the information in the buffer */ 2095 2096 if (sc->proto & PROTO_UFI) { 2097 int status; 2098 2099 /* Section 3.4.3.1.3 specifies that the UFI command 2100 * protocol returns an ASC and ASCQ in the interrupt 2101 * data block. 2102 */ 2103 2104 DPRINTF(UDMASS_CBI, ("%s: UFI CCI, ASC = 0x%02x, " 2105 "ASCQ = 0x%02x\n", 2106 USBDEVNAME(sc->sc_dev), 2107 sc->sbl.ufi.asc, sc->sbl.ufi.ascq)); 2108 2109 if (sc->sbl.ufi.asc == 0 && sc->sbl.ufi.ascq == 0) 2110 status = STATUS_CMD_OK; 2111 else 2112 status = STATUS_CMD_FAILED; 2113 2114 /* No sense, command successfull */ 2115 } else { 2116 /* Command Interrupt Data Block */ 2117 DPRINTF(UDMASS_CBI, ("%s: type=0x%02x, value=0x%02x\n", 2118 USBDEVNAME(sc->sc_dev), 2119 sc->sbl.common.type, sc->sbl.common.value)); 2120 2121 if (sc->sbl.common.type == IDB_TYPE_CCI) { 2122 int err; 2123 2124 if ((sc->sbl.common.value&IDB_VALUE_STATUS_MASK) 2125 == IDB_VALUE_PASS) { 2126 err = STATUS_CMD_OK; 2127 } else if ((sc->sbl.common.value & IDB_VALUE_STATUS_MASK) 2128 == IDB_VALUE_FAIL || 2129 (sc->sbl.common.value & IDB_VALUE_STATUS_MASK) 2130 == IDB_VALUE_PERSISTENT) { 2131 err = STATUS_CMD_FAILED; 2132 } else { 2133 err = STATUS_WIRE_FAILED; 2134 } 2135 2136 sc->transfer_state = TSTATE_IDLE; 2137 sc->transfer_cb(sc, sc->transfer_priv, 2138 sc->transfer_datalen, 2139 err); 2140 } 2141 } 2142 return; 2143 2144 case TSTATE_CBI_DCLEAR: 2145 if (err) { /* should not occur */ 2146 printf("%s: CBI bulk-in/out stall clear failed, %s\n", 2147 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2148 umass_cbi_reset(sc, STATUS_WIRE_FAILED); 2149 } 2150 2151 sc->transfer_state = TSTATE_IDLE; 2152 sc->transfer_cb(sc, sc->transfer_priv, 2153 sc->transfer_datalen, 2154 STATUS_CMD_FAILED); 2155 return; 2156 2157 case TSTATE_CBI_SCLEAR: 2158 if (err) /* should not occur */ 2159 printf("%s: CBI intr-in stall clear failed, %s\n", 2160 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2161 2162 /* Something really bad is going on. Reset the device */ 2163 umass_cbi_reset(sc, STATUS_CMD_FAILED); 2164 return; 2165 2166 /***** CBI Reset *****/ 2167 case TSTATE_CBI_RESET1: 2168 if (err) 2169 printf("%s: CBI reset failed, %s\n", 2170 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2171 2172 umass_clear_endpoint_stall(sc, 2173 sc->bulkin, sc->bulkin_pipe, TSTATE_CBI_RESET2, 2174 sc->transfer_xfer[XFER_CBI_RESET2]); 2175 2176 return; 2177 case TSTATE_CBI_RESET2: 2178 if (err) /* should not occur */ 2179 printf("%s: CBI bulk-in stall clear failed, %s\n", 2180 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2181 /* no error recovery, otherwise we end up in a loop */ 2182 2183 umass_clear_endpoint_stall(sc, 2184 sc->bulkout, sc->bulkout_pipe, TSTATE_CBI_RESET3, 2185 sc->transfer_xfer[XFER_CBI_RESET3]); 2186 2187 return; 2188 case TSTATE_CBI_RESET3: 2189 if (err) /* should not occur */ 2190 printf("%s: CBI bulk-out stall clear failed, %s\n", 2191 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2192 /* no error recovery, otherwise we end up in a loop */ 2193 2194 sc->transfer_state = TSTATE_IDLE; 2195 if (sc->transfer_priv) { 2196 sc->transfer_cb(sc, sc->transfer_priv, 2197 sc->transfer_datalen, 2198 sc->transfer_status); 2199 } 2200 2201 return; 2202 2203 2204 /***** Default *****/ 2205 default: 2206 panic("%s: Unknown state %d\n", 2207 USBDEVNAME(sc->sc_dev), sc->transfer_state); 2208 } 2209 } 2210 2211 usbd_status 2212 umass_bbb_get_max_lun(struct umass_softc *sc, u_int8_t *maxlun) 2213 { 2214 usbd_device_handle dev; 2215 usb_device_request_t req; 2216 usbd_status err; 2217 usb_interface_descriptor_t *id; 2218 2219 *maxlun = 0; /* Default to 0. */ 2220 2221 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun\n", USBDEVNAME(sc->sc_dev))); 2222 2223 usbd_interface2device_handle(sc->iface, &dev); 2224 id = usbd_get_interface_descriptor(sc->iface); 2225 2226 /* The Get Max Lun command is a class-specific request. */ 2227 req.bmRequestType = UT_READ_CLASS_INTERFACE; 2228 req.bRequest = UR_BBB_GET_MAX_LUN; 2229 USETW(req.wValue, 0); 2230 USETW(req.wIndex, id->bInterfaceNumber); 2231 USETW(req.wLength, 1); 2232 2233 err = usbd_do_request(dev, &req, maxlun); 2234 switch (err) { 2235 case USBD_NORMAL_COMPLETION: 2236 DPRINTF(UDMASS_BBB, ("%s: Max Lun %d\n", 2237 USBDEVNAME(sc->sc_dev), *maxlun)); 2238 break; 2239 2240 case USBD_STALLED: 2241 /* 2242 * Device doesn't support Get Max Lun request. 2243 */ 2244 err = USBD_NORMAL_COMPLETION; 2245 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun not supported\n", 2246 USBDEVNAME(sc->sc_dev))); 2247 break; 2248 2249 case USBD_SHORT_XFER: 2250 /* 2251 * XXX This must mean Get Max Lun is not supported, too! 2252 */ 2253 err = USBD_NORMAL_COMPLETION; 2254 DPRINTF(UDMASS_BBB, ("%s: Get Max Lun SHORT_XFER\n", 2255 USBDEVNAME(sc->sc_dev))); 2256 break; 2257 2258 default: 2259 printf("%s: Get Max Lun failed: %s\n", 2260 USBDEVNAME(sc->sc_dev), usbd_errstr(err)); 2261 /* XXX Should we port_reset the device? */ 2262 break; 2263 } 2264 2265 return (err); 2266 } 2267 2268 2269 2270 #if defined(__FreeBSD__) 2271 /* 2272 * CAM specific functions (used by SCSI, UFI, 8070) 2273 */ 2274 2275 Static int 2276 umass_cam_attach_sim(void) 2277 { 2278 struct cam_devq *devq; /* Per device Queue */ 2279 2280 /* A HBA is attached to the CAM layer. 2281 * 2282 * The CAM layer will then after a while start probing for 2283 * devices on the bus. The number of devices is limitted to one. 2284 */ 2285 2286 /* SCSI transparent command set */ 2287 2288 devq = cam_simq_alloc(1 /*maximum openings*/); 2289 if (devq == NULL) 2290 return(ENOMEM); 2291 2292 umass_sim = cam_sim_alloc(umass_cam_action, umass_cam_poll, DEVNAME, 2293 NULL /*priv*/, 0 /*unit number*/, 2294 1 /*maximum device openings*/, 2295 0 /*maximum tagged device openings*/, 2296 devq); 2297 if (umass_sim == NULL) { 2298 cam_simq_free(devq); 2299 return(ENOMEM); 2300 } 2301 2302 if(xpt_bus_register(umass_sim, 0) != CAM_SUCCESS) 2303 return(ENOMEM); 2304 2305 if (xpt_create_path(&umass_path, NULL, cam_sim_path(umass_sim), 2306 UMASS_SCSIID_HOST, 0) 2307 != CAM_REQ_CMP) 2308 return(ENOMEM); 2309 2310 return(0); 2311 } 2312 2313 #ifdef UMASS_DO_CAM_RESCAN 2314 /* this function is only used from umass_cam_rescan, so mention 2315 * prototype down here. 2316 */ 2317 Static void umass_cam_rescan_callback(struct cam_periph *periph,union ccb *ccb); 2318 2319 Static void 2320 umass_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb) 2321 { 2322 #ifdef UMASS_DEBUG 2323 struct umass_softc *sc = devclass_get_softc(umass_devclass, 2324 ccb->ccb_h.target_id); 2325 2326 if (ccb->ccb_h.status != CAM_REQ_CMP) { 2327 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan failed, 0x%04x\n", 2328 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2329 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2330 ccb->ccb_h.status)); 2331 } else { 2332 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: Rescan succeeded, freeing resources.\n", 2333 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2334 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2335 } 2336 #endif 2337 2338 xpt_free_path(ccb->ccb_h.path); 2339 free(ccb, M_USBDEV); 2340 } 2341 2342 Static void 2343 umass_cam_rescan(struct umass_softc *sc) 2344 { 2345 struct cam_path *path; 2346 union ccb *ccb = malloc(sizeof(union ccb), M_USBDEV, M_WAITOK); 2347 2348 memset(ccb, 0, sizeof(union ccb)); 2349 2350 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d: scanning bus for new device %d\n", 2351 USBDEVNAME(sc->sc_dev), cam_sim_path(umass_sim), 2352 device_get_unit(sc->sc_dev), 0, 2353 device_get_unit(sc->sc_dev))); 2354 2355 if (xpt_create_path(&path, xpt_periph, cam_sim_path(umass_sim), 2356 device_get_unit(sc->sc_dev), 0) 2357 != CAM_REQ_CMP) 2358 return; 2359 2360 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/); 2361 ccb->ccb_h.func_code = XPT_SCAN_BUS; 2362 ccb->ccb_h.cbfcnp = umass_cam_rescan_callback; 2363 ccb->crcn.flags = CAM_FLAG_NONE; 2364 xpt_action(ccb); 2365 2366 /* The scan is in progress now. */ 2367 } 2368 #endif 2369 2370 Static int 2371 umass_cam_attach(struct umass_softc *sc) 2372 { 2373 /* SIM already attached at module load. The device is a target on the 2374 * one SIM we registered: target device_get_unit(self). 2375 */ 2376 2377 /* The artificial limit UMASS_SCSIID_MAX is there because CAM expects 2378 * a limit to the number of targets that are present on a SIM. 2379 */ 2380 if (device_get_unit(sc->sc_dev) > UMASS_SCSIID_MAX) { 2381 printf("%s: Increase UMASS_SCSIID_MAX (currently %d) in %s " 2382 "and try again.\n", USBDEVNAME(sc->sc_dev), 2383 UMASS_SCSIID_MAX, __FILE__); 2384 return(1); 2385 } 2386 2387 #ifdef UMASS_DO_CAM_RESCAN 2388 if (!cold) { 2389 /* Notify CAM of the new device. Any failure is benign, as the 2390 * user can still do it by hand (camcontrol rescan <busno>). 2391 * Only do this if we are not booting, because CAM does a scan 2392 * after booting has completed, when interrupts have been 2393 * enabled. 2394 */ 2395 umass_cam_rescan(sc); 2396 } 2397 #endif 2398 2399 return(0); /* always succesful */ 2400 } 2401 2402 /* umass_cam_detach 2403 * detach from the CAM layer 2404 */ 2405 2406 Static int 2407 umass_cam_detach_sim(void) 2408 { 2409 if (umass_sim) 2410 return(EBUSY); /* XXX CAM can't handle disappearing SIMs yet */ 2411 2412 if (umass_path) { 2413 /* XXX do we need to send an asynchroneous event for the SIM? 2414 xpt_async(AC_LOST_DEVICE, umass_path, NULL); 2415 */ 2416 xpt_free_path(umass_path); 2417 umass_path = NULL; 2418 } 2419 2420 if (umass_sim) { 2421 if (xpt_bus_deregister(cam_sim_path(umass_sim))) 2422 cam_sim_free(umass_sim, /*free_devq*/TRUE); 2423 else 2424 return(EBUSY); 2425 2426 umass_sim = NULL; 2427 } 2428 2429 return(0); 2430 } 2431 2432 Static int 2433 umass_cam_detach(struct umass_softc *sc) 2434 { 2435 struct cam_path *path; 2436 2437 /* detach of sim not done until module unload */ 2438 DPRINTF(UDMASS_SCSI, ("%s: losing CAM device entry\n", 2439 USBDEVNAME(sc->sc_dev))); 2440 2441 if (xpt_create_path(&path, NULL, cam_sim_path(umass_sim), 2442 device_get_unit(sc->sc_dev), CAM_LUN_WILDCARD) 2443 != CAM_REQ_CMP) 2444 return(ENOMEM); 2445 xpt_async(AC_LOST_DEVICE, path, NULL); 2446 xpt_free_path(path); 2447 2448 return(0); 2449 } 2450 2451 2452 2453 /* umass_cam_action 2454 * CAM requests for action come through here 2455 */ 2456 2457 Static void 2458 umass_cam_action(struct cam_sim *sim, union ccb *ccb) 2459 { 2460 struct umass_softc *sc = devclass_get_softc(umass_devclass, 2461 ccb->ccb_h.target_id); 2462 2463 /* The softc is still there, but marked as going away. umass_cam_detach 2464 * has not yet notified CAM of the lost device however. 2465 */ 2466 if (sc && sc->sc_dying) { 2467 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " 2468 "Invalid target (gone)\n", 2469 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2470 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2471 ccb->ccb_h.func_code)); 2472 ccb->ccb_h.status = CAM_TID_INVALID; 2473 xpt_done(ccb); 2474 return; 2475 } 2476 2477 /* Verify, depending on the operation to perform, that we either got a 2478 * valid sc, because an existing target was referenced, or otherwise 2479 * the SIM is addressed. 2480 * 2481 * This avoids bombing out at a printf and does give the CAM layer some 2482 * sensible feedback on errors. 2483 */ 2484 switch (ccb->ccb_h.func_code) { 2485 case XPT_SCSI_IO: 2486 case XPT_RESET_DEV: 2487 case XPT_GET_TRAN_SETTINGS: 2488 case XPT_SET_TRAN_SETTINGS: 2489 case XPT_CALC_GEOMETRY: 2490 /* the opcodes requiring a target. These should never occur. */ 2491 if (sc == NULL) { 2492 printf("%s:%d:%d:%d:func_code 0x%04x: " 2493 "Invalid target\n", 2494 DEVNAME_SIM, UMASS_SCSI_BUS, 2495 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2496 ccb->ccb_h.func_code); 2497 2498 ccb->ccb_h.status = CAM_TID_INVALID; 2499 xpt_done(ccb); 2500 return; 2501 } 2502 break; 2503 case XPT_PATH_INQ: 2504 case XPT_NOOP: 2505 /* The opcodes sometimes aimed at a target (sc is valid), 2506 * sometimes aimed at the SIM (sc is invalid and target is 2507 * CAM_TARGET_WILDCARD) 2508 */ 2509 if (sc == NULL && ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) { 2510 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " 2511 "Invalid target\n", 2512 DEVNAME_SIM, UMASS_SCSI_BUS, 2513 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2514 ccb->ccb_h.func_code)); 2515 2516 ccb->ccb_h.status = CAM_TID_INVALID; 2517 xpt_done(ccb); 2518 return; 2519 } 2520 break; 2521 default: 2522 /* XXX Hm, we should check the input parameters */ 2523 } 2524 2525 /* Perform the requested action */ 2526 switch (ccb->ccb_h.func_code) { 2527 case XPT_SCSI_IO: 2528 { 2529 struct ccb_scsiio *csio = &ccb->csio; /* deref union */ 2530 int dir; 2531 unsigned char *cmd; 2532 int cmdlen; 2533 2534 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: " 2535 "cmd: 0x%02x, flags: 0x%02x, " 2536 "%db cmd/%db data/%db sense\n", 2537 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2538 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2539 csio->cdb_io.cdb_bytes[0], 2540 ccb->ccb_h.flags & CAM_DIR_MASK, 2541 csio->cdb_len, csio->dxfer_len, 2542 csio->sense_len)); 2543 2544 /* clear the end of the buffer to make sure we don't send out 2545 * garbage. 2546 */ 2547 DIF(UDMASS_SCSI, if ((ccb->ccb_h.flags & CAM_DIR_MASK) 2548 == CAM_DIR_OUT) 2549 umass_dump_buffer(sc, csio->data_ptr, 2550 csio->dxfer_len, 48)); 2551 2552 if (sc->transfer_state != TSTATE_IDLE) { 2553 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SCSI_IO: " 2554 "I/O requested while busy (state %d, %s)\n", 2555 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2556 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2557 sc->transfer_state,states[sc->transfer_state])); 2558 ccb->ccb_h.status = CAM_SCSI_BUSY; 2559 xpt_done(ccb); 2560 return; 2561 } 2562 2563 switch(ccb->ccb_h.flags&CAM_DIR_MASK) { 2564 case CAM_DIR_IN: 2565 dir = DIR_IN; 2566 break; 2567 case CAM_DIR_OUT: 2568 dir = DIR_OUT; 2569 break; 2570 default: 2571 dir = DIR_NONE; 2572 } 2573 2574 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED; 2575 if (sc->transform(sc, csio->cdb_io.cdb_bytes, csio->cdb_len, 2576 &cmd, &cmdlen)) { 2577 sc->transfer(sc, ccb->ccb_h.target_lun, cmd, cmdlen, 2578 csio->data_ptr, 2579 csio->dxfer_len, dir, 2580 umass_cam_cb, (void *) ccb); 2581 } else { 2582 ccb->ccb_h.status = CAM_REQ_INVALID; 2583 xpt_done(ccb); 2584 } 2585 2586 break; 2587 } 2588 case XPT_PATH_INQ: 2589 { 2590 struct ccb_pathinq *cpi = &ccb->cpi; 2591 2592 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_PATH_INQ:.\n", 2593 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), 2594 UMASS_SCSI_BUS, 2595 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2596 2597 /* host specific information */ 2598 cpi->version_num = 1; 2599 cpi->hba_inquiry = 0; 2600 cpi->target_sprt = 0; 2601 cpi->hba_misc = 0; 2602 cpi->hba_eng_cnt = 0; 2603 cpi->max_target = UMASS_SCSIID_MAX; /* one target */ 2604 cpi->max_lun = 0; /* no LUN's supported */ 2605 cpi->initiator_id = UMASS_SCSIID_HOST; 2606 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN); 2607 strncpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN); 2608 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN); 2609 cpi->unit_number = cam_sim_unit(sim); 2610 cpi->bus_id = UMASS_SCSI_BUS; 2611 if (sc) { 2612 cpi->base_transfer_speed = sc->transfer_speed; 2613 cpi->max_lun = sc->maxlun; 2614 } 2615 2616 cpi->ccb_h.status = CAM_REQ_CMP; 2617 xpt_done(ccb); 2618 break; 2619 } 2620 case XPT_RESET_DEV: 2621 { 2622 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_RESET_DEV:.\n", 2623 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2624 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2625 2626 ccb->ccb_h.status = CAM_REQ_INPROG; 2627 umass_reset(sc, umass_cam_cb, (void *) ccb); 2628 break; 2629 } 2630 case XPT_GET_TRAN_SETTINGS: 2631 { 2632 struct ccb_trans_settings *cts = &ccb->cts; 2633 2634 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n", 2635 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2636 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2637 2638 cts->valid = 0; 2639 cts->flags = 0; /* no disconnection, tagging */ 2640 2641 ccb->ccb_h.status = CAM_REQ_CMP; 2642 xpt_done(ccb); 2643 break; 2644 } 2645 case XPT_SET_TRAN_SETTINGS: 2646 { 2647 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n", 2648 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2649 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2650 2651 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2652 xpt_done(ccb); 2653 break; 2654 } 2655 case XPT_CALC_GEOMETRY: 2656 { 2657 struct ccb_calc_geometry *ccg = &ccb->ccg; 2658 2659 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_CALC_GEOMETRY: " 2660 "Volume size = %d\n", 2661 USBDEVNAME(sc->sc_dev), UMASS_SCSI_BUS, 2662 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2663 ccg->volume_size)); 2664 2665 /* XXX We should probably ask the drive for the details 2666 * instead of cluching them up ourselves 2667 */ 2668 if (sc->drive == ZIP_100) { 2669 ccg->heads = 64; 2670 ccg->secs_per_track = 32; 2671 ccg->cylinders = ccg->volume_size / ccg->heads 2672 / ccg->secs_per_track; 2673 ccb->ccb_h.status = CAM_REQ_CMP; 2674 break; 2675 } else if (sc->proto & PROTO_UFI) { 2676 ccg->heads = 2; 2677 if (ccg->volume_size == 2880) 2678 ccg->secs_per_track = 18; 2679 else 2680 ccg->secs_per_track = 9; 2681 ccg->cylinders = 80; 2682 break; 2683 } else { 2684 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2685 } 2686 2687 xpt_done(ccb); 2688 break; 2689 } 2690 case XPT_NOOP: 2691 { 2692 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:XPT_NOOP:.\n", 2693 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), 2694 UMASS_SCSI_BUS, 2695 ccb->ccb_h.target_id, ccb->ccb_h.target_lun)); 2696 2697 ccb->ccb_h.status = CAM_REQ_CMP; 2698 xpt_done(ccb); 2699 break; 2700 } 2701 default: 2702 DPRINTF(UDMASS_SCSI, ("%s:%d:%d:%d:func_code 0x%04x: " 2703 "Not implemented\n", 2704 (sc == NULL? DEVNAME_SIM:USBDEVNAME(sc->sc_dev)), 2705 UMASS_SCSI_BUS, 2706 ccb->ccb_h.target_id, ccb->ccb_h.target_lun, 2707 ccb->ccb_h.func_code)); 2708 2709 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL; 2710 xpt_done(ccb); 2711 break; 2712 } 2713 } 2714 2715 /* umass_cam_poll 2716 * all requests are handled through umass_cam_action, requests 2717 * are never pending. So, nothing to do here. 2718 */ 2719 Static void 2720 umass_cam_poll(struct cam_sim *sim) 2721 { 2722 #ifdef UMASS_DEBUG 2723 struct umass_softc *sc = (struct umass_softc *) sim->softc; 2724 2725 DPRINTF(UDMASS_SCSI, ("%s: CAM poll\n", 2726 USBDEVNAME(sc->sc_dev))); 2727 #endif 2728 2729 /* nop */ 2730 } 2731 2732 2733 /* umass_cam_cb 2734 * finalise a completed CAM command 2735 */ 2736 2737 Static void 2738 umass_cam_cb(struct umass_softc *sc, void *priv, int residue, int status) 2739 { 2740 union ccb *ccb = (union ccb *) priv; 2741 struct ccb_scsiio *csio = &ccb->csio; /* deref union */ 2742 2743 csio->resid = residue; 2744 2745 switch (status) { 2746 case STATUS_CMD_OK: 2747 ccb->ccb_h.status = CAM_REQ_CMP; 2748 xpt_done(ccb); 2749 break; 2750 2751 case STATUS_CMD_UNKNOWN: 2752 case STATUS_CMD_FAILED: 2753 switch (ccb->ccb_h.func_code) { 2754 case XPT_SCSI_IO: 2755 { 2756 unsigned char *cmd; 2757 int cmdlen; 2758 2759 /* fetch sense data */ 2760 DPRINTF(UDMASS_SCSI,("%s: Fetching %db sense data\n", 2761 USBDEVNAME(sc->sc_dev), 2762 sc->cam_scsi_sense.length)); 2763 2764 sc->cam_scsi_sense.length = csio->sense_len; 2765 2766 if (sc->transform(sc, (char *) &sc->cam_scsi_sense, 2767 sizeof(sc->cam_scsi_sense), 2768 &cmd, &cmdlen)) { 2769 sc->transfer(sc, ccb->ccb_h.target_lun, 2770 cmd, cmdlen, 2771 &csio->sense_data, 2772 csio->sense_len, DIR_IN, 2773 umass_cam_sense_cb, (void *) ccb); 2774 } else { 2775 #ifdef UMASS_DEBUG 2776 panic("transform(REQUEST_SENSE) failed\n"); 2777 #else 2778 csio->resid = sc->transfer_datalen; 2779 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2780 xpt_done(ccb); 2781 #endif 2782 } 2783 break; 2784 } 2785 case XPT_RESET_DEV: /* Reset failed */ 2786 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2787 xpt_done(ccb); 2788 break; 2789 default: 2790 panic("umass_cam_cb called for func_code %d\n", 2791 ccb->ccb_h.func_code); 2792 } 2793 break; 2794 2795 case STATUS_WIRE_FAILED: 2796 /* the wire protocol failed and will have recovered 2797 * (hopefully). We return an error to CAM and let CAM retry 2798 * the command if necessary. 2799 */ 2800 ccb->ccb_h.status = CAM_REQ_CMP_ERR; 2801 xpt_done(ccb); 2802 break; 2803 2804 default: 2805 panic("%s: Unknown status %d in umass_cam_cb\n", 2806 USBDEVNAME(sc->sc_dev), status); 2807 } 2808 } 2809 2810 /* Finalise a completed autosense operation 2811 */ 2812 Static void 2813 umass_cam_sense_cb(struct umass_softc *sc, void *priv, int residue, int status) 2814 { 2815 union ccb *ccb = (union ccb *) priv; 2816 struct ccb_scsiio *csio = &ccb->csio; /* deref union */ 2817 2818 switch (status) { 2819 case STATUS_CMD_OK: 2820 case STATUS_CMD_UNKNOWN: 2821 /* Getting sense data succeeded. The length of the sense data 2822 * is not returned in any way. The sense data itself contains 2823 * the length of the sense data that is valid. 2824 */ 2825 if (sc->quirks & RS_NO_CLEAR_UA 2826 && csio->cdb_io.cdb_bytes[0] == INQUIRY 2827 && (csio->sense_data.flags & SSD_KEY) 2828 == SSD_KEY_UNIT_ATTENTION) { 2829 /* Ignore unit attention errors in the case where 2830 * the Unit Attention state is not cleared on 2831 * REQUEST SENSE. They will appear again at the next 2832 * command. 2833 */ 2834 ccb->ccb_h.status = CAM_REQ_CMP; 2835 } else if ((csio->sense_data.flags & SSD_KEY) 2836 == SSD_KEY_NO_SENSE) { 2837 /* No problem after all (in the case of CBI without 2838 * CCI) 2839 */ 2840 ccb->ccb_h.status = CAM_REQ_CMP; 2841 } else { 2842 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR 2843 | CAM_AUTOSNS_VALID; 2844 csio->scsi_status = SCSI_STATUS_CHECK_COND; 2845 } 2846 xpt_done(ccb); 2847 break; 2848 2849 default: 2850 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n", 2851 USBDEVNAME(sc->sc_dev), status)); 2852 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL; 2853 xpt_done(ccb); 2854 } 2855 } 2856 2857 2858 Static int 2859 umass_driver_load(module_t mod, int what, void *arg) 2860 { 2861 int err; 2862 2863 switch (what) { 2864 case MOD_UNLOAD: 2865 err = umass_cam_detach_sim(); 2866 if (err) 2867 return(err); 2868 return(usbd_driver_load(mod, what, arg)); 2869 case MOD_LOAD: 2870 /* We don't attach to CAM at this point, because it will try 2871 * and malloc memory for it. This is not possible when the 2872 * boot loader loads umass as a module before the kernel 2873 * has been bootstrapped. 2874 */ 2875 default: 2876 return(usbd_driver_load(mod, what, arg)); 2877 } 2878 } 2879 2880 2881 2882 /* (even the comment is missing) */ 2883 2884 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, umass_driver_load, 0); 2885 2886 2887 /* 2888 * SCSI specific functions 2889 */ 2890 2891 Static int 2892 umass_scsi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, 2893 unsigned char **rcmd, int *rcmdlen) 2894 { 2895 *rcmd = cmd; /* trivial copy */ 2896 *rcmdlen = cmdlen; 2897 2898 switch (cmd[0]) { 2899 case TEST_UNIT_READY: 2900 if (sc->quirks & NO_TEST_UNIT_READY) { 2901 DPRINTF(UDMASS_SCSI, ("%s: Converted TEST_UNIT_READY " 2902 "to START_UNIT\n", USBDEVNAME(sc->sc_dev))); 2903 cmd[0] = START_STOP_UNIT; 2904 cmd[4] = SSS_START; 2905 } 2906 break; 2907 } 2908 2909 return 1; /* success */ 2910 } 2911 2912 /* 2913 * UFI specific functions 2914 */ 2915 2916 Static int 2917 umass_ufi_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, 2918 unsigned char **rcmd, int *rcmdlen) 2919 { 2920 *rcmd = cmd; 2921 /* A UFI command is always 12 bytes in length */ 2922 /* XXX cmd[(cmdlen+1)..12] contains garbage */ 2923 *rcmdlen = 12; 2924 2925 switch (cmd[0]) { 2926 case TEST_UNIT_READY: 2927 if (sc->quirks & NO_TEST_UNIT_READY) { 2928 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY " 2929 "to START_UNIT\n", USBDEVNAME(sc->sc_dev))); 2930 cmd[0] = START_STOP_UNIT; 2931 cmd[4] = SSS_START; 2932 } 2933 return 1; 2934 case INQUIRY: 2935 case START_STOP_UNIT: 2936 case MODE_SENSE: 2937 case PREVENT_ALLOW: 2938 case READ_10: 2939 case READ_12: 2940 case READ_CAPACITY: 2941 case REQUEST_SENSE: 2942 case REZERO_UNIT: 2943 case POSITION_TO_ELEMENT: /* SEEK_10 */ 2944 case SEND_DIAGNOSTIC: 2945 case WRITE_10: 2946 case WRITE_12: 2947 /* FORMAT_UNIT */ 2948 /* MODE_SELECT */ 2949 /* READ_FORMAT_CAPACITY */ 2950 /* VERIFY */ 2951 /* WRITE_AND_VERIFY */ 2952 return 1; /* success */ 2953 default: 2954 return 0; /* success */ 2955 } 2956 } 2957 2958 /* 2959 * 8070 specific functions 2960 */ 2961 Static int 2962 umass_8070_transform(struct umass_softc *sc, unsigned char *cmd, int cmdlen, 2963 unsigned char **rcmd, int *rcmdlen) 2964 { 2965 return 0; /* failure */ 2966 } 2967 2968 #endif /* __FreeBSD__ */ 2969 2970 2971 #ifdef UMASS_DEBUG 2972 Static void 2973 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw) 2974 { 2975 int clen = cbw->bCDBLength; 2976 int dlen = UGETDW(cbw->dCBWDataTransferLength); 2977 u_int8_t *c = cbw->CBWCDB; 2978 int tag = UGETDW(cbw->dCBWTag); 2979 int flags = cbw->bCBWFlags; 2980 2981 DPRINTF(UDMASS_BBB, ("%s: CBW %d: cmd = %db " 2982 "(0x%02x%02x%02x%02x%02x%02x%s), " 2983 "data = %d bytes, dir = %s\n", 2984 USBDEVNAME(sc->sc_dev), tag, clen, 2985 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6? "...":""), 2986 dlen, (flags == CBWFLAGS_IN? "in": 2987 (flags == CBWFLAGS_OUT? "out":"<invalid>")))); 2988 } 2989 2990 Static void 2991 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw) 2992 { 2993 int sig = UGETDW(csw->dCSWSignature); 2994 int tag = UGETW(csw->dCSWTag); 2995 int res = UGETDW(csw->dCSWDataResidue); 2996 int status = csw->bCSWStatus; 2997 2998 DPRINTF(UDMASS_BBB, ("%s: CSW %d: sig = 0x%08x (%s), tag = %d, " 2999 "res = %d, status = 0x%02x (%s)\n", USBDEVNAME(sc->sc_dev), 3000 tag, sig, (sig == CSWSIGNATURE? "valid":"invalid"), 3001 tag, res, 3002 status, (status == CSWSTATUS_GOOD? "good": 3003 (status == CSWSTATUS_FAILED? "failed": 3004 (status == CSWSTATUS_PHASE? "phase":"<invalid>"))))); 3005 } 3006 3007 Static void 3008 umass_dump_buffer(struct umass_softc *sc, u_int8_t *buffer, int buflen, 3009 int printlen) 3010 { 3011 int i, j; 3012 char s1[40]; 3013 char s2[40]; 3014 char s3[5]; 3015 3016 s1[0] = '\0'; 3017 s3[0] = '\0'; 3018 3019 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen); 3020 for (i = 0; i < buflen && i < printlen; i++) { 3021 j = i % 16; 3022 if (j == 0 && i != 0) { 3023 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s\n", 3024 USBDEVNAME(sc->sc_dev), s1, s2)); 3025 s2[0] = '\0'; 3026 } 3027 sprintf(&s1[j*2], "%02x", buffer[i] & 0xff); 3028 } 3029 if (buflen > printlen) 3030 sprintf(s3, " ..."); 3031 DPRINTF(UDMASS_GEN, ("%s: 0x %s%s%s\n", 3032 USBDEVNAME(sc->sc_dev), s1, s2, s3)); 3033 } 3034 #endif 3035 3036 3037 3038 3039 3040 3041 3042 3043 #if defined(__NetBSD__) || defined(__OpenBSD__) 3044 Static int 3045 umass_scsipi_cmd(struct scsipi_xfer *xs) 3046 { 3047 struct scsipi_link *sc_link = xs->sc_link; 3048 struct umass_softc *sc = sc_link->adapter_softc; 3049 struct scsipi_generic *cmd, trcmd; 3050 int cmdlen; 3051 int dir; 3052 3053 DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL); 3054 3055 DPRINTF(UDMASS_CMD, ("%s: umass_scsi_cmd: %d:%d xs=%p cmd=0x%02x " 3056 "(quirks=0x%x, poll=%d)\n", USBDEVNAME(sc->sc_dev), 3057 sc_link->scsipi_scsi.target, sc_link->scsipi_scsi.lun, 3058 xs, xs->cmd->opcode, sc_link->quirks, 3059 xs->xs_control & XS_CTL_POLL)); 3060 #if defined(USB_DEBUG) && defined(SCSIDEBUG) 3061 if (umassdebug & UDMASS_SCSI) 3062 show_scsipi_xs(xs); 3063 else if (umassdebug & ~UDMASS_CMD) 3064 show_scsipi_cmd(xs); 3065 #endif 3066 3067 if (sc->sc_dying) { 3068 xs->error = XS_DRIVER_STUFFUP; 3069 goto done; 3070 } 3071 3072 #ifdef UMASS_DEBUG 3073 if ((sc_link->type == BUS_ATAPI ? 3074 sc_link->scsipi_atapi.drive : sc_link->scsipi_scsi.target) 3075 != UMASS_SCSIID_DEVICE) { 3076 DPRINTF(UDMASS_SCSI, ("%s: wrong SCSI ID %d\n", 3077 USBDEVNAME(sc->sc_dev), 3078 sc_link->scsipi_scsi.target)); 3079 xs->error = XS_DRIVER_STUFFUP; 3080 goto done; 3081 } 3082 #endif 3083 3084 if (xs->cmd->opcode == SCSI_MODE_SENSE && 3085 (sc_link->quirks & SDEV_NOMODESENSE)) { 3086 /*printf("%s: SCSI_MODE_SENSE\n", USBDEVNAME(sc->sc_dev));*/ 3087 xs->error = XS_TIMEOUT; 3088 goto done; 3089 } 3090 3091 if (xs->cmd->opcode == START_STOP && 3092 (sc->quirks & NO_START_STOP)) { 3093 /*printf("%s: START_STOP\n", USBDEVNAME(sc->sc_dev));*/ 3094 xs->error = XS_NOERROR; 3095 goto done; 3096 } 3097 3098 dir = DIR_NONE; 3099 if (xs->datalen) { 3100 switch (xs->xs_control & (XS_CTL_DATA_IN | XS_CTL_DATA_OUT)) { 3101 case XS_CTL_DATA_IN: 3102 dir = DIR_IN; 3103 break; 3104 case XS_CTL_DATA_OUT: 3105 dir = DIR_OUT; 3106 break; 3107 } 3108 } 3109 3110 if (xs->datalen > UMASS_MAX_TRANSFER_SIZE) { 3111 printf("umass_cmd: large datalen, %d\n", xs->datalen); 3112 xs->error = XS_DRIVER_STUFFUP; 3113 goto done; 3114 } 3115 3116 cmd = xs->cmd; 3117 cmdlen = xs->cmdlen; 3118 if (sc->proto & PROTO_UFI) { 3119 if (!umass_ufi_transform(sc, cmd, cmdlen, &trcmd, &cmdlen)) { 3120 xs->error = XS_DRIVER_STUFFUP; 3121 goto done; 3122 } 3123 cmd = &trcmd; 3124 3125 } 3126 3127 if (xs->xs_control & XS_CTL_POLL) { 3128 /* Use sync transfer. XXX Broken! */ 3129 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: sync dir=%d\n", dir)); 3130 sc->sc_xfer_flags = USBD_SYNCHRONOUS; 3131 sc->sc_sync_status = USBD_INVAL; 3132 sc->transfer(sc, sc_link->scsipi_scsi.lun, cmd, cmdlen, 3133 xs->data, xs->datalen, dir, 0, xs); 3134 sc->sc_xfer_flags = 0; 3135 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: done err=%d\n", 3136 sc->sc_sync_status)); 3137 switch (sc->sc_sync_status) { 3138 case USBD_NORMAL_COMPLETION: 3139 xs->error = XS_NOERROR; 3140 break; 3141 case USBD_TIMEOUT: 3142 xs->error = XS_TIMEOUT; 3143 break; 3144 default: 3145 xs->error = XS_DRIVER_STUFFUP; 3146 break; 3147 } 3148 goto done; 3149 } else { 3150 DPRINTF(UDMASS_SCSI, ("umass_scsi_cmd: async dir=%d, cmdlen=%d" 3151 " datalen=%d\n", 3152 dir, cmdlen, xs->datalen)); 3153 sc->transfer(sc, sc_link->scsipi_scsi.lun, cmd, cmdlen, 3154 xs->data, xs->datalen, dir, umass_scsipi_cb, xs); 3155 return (SUCCESSFULLY_QUEUED); 3156 } 3157 3158 /* Return if command finishes early. */ 3159 done: 3160 xs->xs_status |= XS_STS_DONE; 3161 scsipi_done(xs); 3162 if (xs->xs_control & XS_CTL_POLL) 3163 return (COMPLETE); 3164 else 3165 return (SUCCESSFULLY_QUEUED); 3166 } 3167 3168 Static void 3169 umass_scsipi_minphys(struct buf *bp) 3170 { 3171 if (bp->b_bcount > UMASS_MAX_TRANSFER_SIZE) 3172 bp->b_bcount = UMASS_MAX_TRANSFER_SIZE; 3173 minphys(bp); 3174 } 3175 3176 int 3177 umass_scsipi_ioctl(struct scsipi_link *link, u_long cmd, caddr_t arg, 3178 int flag, struct proc *p) 3179 { 3180 /*struct umass_softc *sc = link->adapter_softc;*/ 3181 3182 switch (cmd) { 3183 #if 0 3184 case SCBUSIORESET: 3185 ccb->ccb_h.status = CAM_REQ_INPROG; 3186 umass_reset(sc, umass_cam_cb, (void *) ccb); 3187 return (0); 3188 #endif 3189 default: 3190 return (ENOTTY); 3191 } 3192 } 3193 3194 Static int 3195 umass_scsipi_getgeom(struct scsipi_link *sc_link, struct disk_parms *dp, 3196 u_long sectors) 3197 { 3198 struct umass_softc *sc = sc_link->adapter_softc; 3199 3200 /* If it's not a floppy, we don't know what to do. */ 3201 if (!(sc->proto & PROTO_UFI)) 3202 return (0); 3203 3204 switch (sectors) { 3205 case 1440: 3206 /* Most likely a single density 3.5" floppy. */ 3207 dp->heads = 2; 3208 dp->sectors = 9; 3209 dp->cyls = 80; 3210 return (1); 3211 case 2880: 3212 /* Most likely a double density 3.5" floppy. */ 3213 dp->heads = 2; 3214 dp->sectors = 18; 3215 dp->cyls = 80; 3216 return (1); 3217 default: 3218 return (0); 3219 } 3220 } 3221 3222 Static void 3223 umass_scsipi_cb(struct umass_softc *sc, void *priv, int residue, int status) 3224 { 3225 struct scsipi_xfer *xs = priv; 3226 struct scsipi_link *sc_link = xs->sc_link; 3227 int cmdlen; 3228 int s; 3229 3230 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: xs=%p residue=%d status=%d\n", 3231 xs, residue, status)); 3232 3233 xs->resid = residue; 3234 3235 switch (status) { 3236 case STATUS_CMD_OK: 3237 xs->error = XS_NOERROR; 3238 break; 3239 3240 case STATUS_CMD_UNKNOWN: 3241 case STATUS_CMD_FAILED: 3242 /* fetch sense data */ 3243 memset(&sc->sc_sense_cmd, 0, sizeof(sc->sc_sense_cmd)); 3244 sc->sc_sense_cmd.opcode = REQUEST_SENSE; 3245 sc->sc_sense_cmd.byte2 = sc_link->scsipi_scsi.lun << 3246 SCSI_CMD_LUN_SHIFT; 3247 sc->sc_sense_cmd.length = sizeof(xs->sense); 3248 3249 cmdlen = sizeof(sc->sc_sense_cmd); 3250 if (sc->proto & PROTO_UFI) /* XXX */ 3251 cmdlen = UFI_COMMAND_LENGTH; 3252 sc->transfer(sc, sc_link->scsipi_scsi.lun, 3253 &sc->sc_sense_cmd, cmdlen, 3254 &xs->sense, sizeof(xs->sense), DIR_IN, 3255 umass_scsipi_sense_cb, xs); 3256 return; 3257 3258 case STATUS_WIRE_FAILED: 3259 xs->error = XS_RESET; 3260 break; 3261 3262 default: 3263 panic("%s: Unknown status %d in umass_scsipi_cb\n", 3264 USBDEVNAME(sc->sc_dev), status); 3265 } 3266 3267 xs->xs_status |= XS_STS_DONE; 3268 3269 DPRINTF(UDMASS_CMD,("umass_scsipi_cb: return xs->error=%d, " 3270 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status, 3271 xs->resid)); 3272 3273 s = splbio(); 3274 scsipi_done(xs); 3275 splx(s); 3276 } 3277 3278 /* 3279 * Finalise a completed autosense operation 3280 */ 3281 Static void 3282 umass_scsipi_sense_cb(struct umass_softc *sc, void *priv, int residue, 3283 int status) 3284 { 3285 struct scsipi_xfer *xs = priv; 3286 int s; 3287 3288 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: xs=%p residue=%d " 3289 "status=%d\n", xs, residue, status)); 3290 3291 switch (status) { 3292 case STATUS_CMD_OK: 3293 case STATUS_CMD_UNKNOWN: 3294 /* getting sense data succeeded */ 3295 if (xs->cmd->opcode == INQUIRY && (xs->resid < xs->datalen 3296 || ((sc->quirks & RS_NO_CLEAR_UA) /* XXX */) )) { 3297 /* 3298 * Some drivers return SENSE errors even after INQUIRY. 3299 * The upper layer doesn't like that. 3300 */ 3301 xs->error = XS_NOERROR; 3302 break; 3303 } 3304 /* XXX look at residue */ 3305 if (residue == 0 || residue == 14)/* XXX */ 3306 xs->error = XS_SENSE; 3307 else 3308 xs->error = XS_SHORTSENSE; 3309 break; 3310 default: 3311 DPRINTF(UDMASS_SCSI, ("%s: Autosense failed, status %d\n", 3312 USBDEVNAME(sc->sc_dev), status)); 3313 xs->error = XS_DRIVER_STUFFUP; 3314 break; 3315 } 3316 3317 xs->xs_status |= XS_STS_DONE; 3318 3319 DPRINTF(UDMASS_CMD,("umass_scsipi_sense_cb: return xs->error=%d, " 3320 "xs->xs_status=0x%x xs->resid=%d\n", xs->error, xs->xs_status, 3321 xs->resid)); 3322 3323 s = splbio(); 3324 scsipi_done(xs); 3325 splx(s); 3326 } 3327 3328 /* 3329 * UFI specific functions 3330 */ 3331 3332 Static int 3333 umass_ufi_transform(struct umass_softc *sc, struct scsipi_generic *cmd, 3334 int cmdlen, struct scsipi_generic *rcmd, int *rcmdlen) 3335 { 3336 *rcmdlen = UFI_COMMAND_LENGTH; 3337 memset(rcmd, 0, sizeof *rcmd); 3338 3339 /* Handle any quirks */ 3340 if (cmd->opcode == TEST_UNIT_READY 3341 && (sc->quirks & NO_TEST_UNIT_READY)) { 3342 /* 3343 * Some devices do not support this command. 3344 * Start Stop Unit should give the same results 3345 */ 3346 DPRINTF(UDMASS_UFI, ("%s: Converted TEST_UNIT_READY " 3347 "to START_UNIT\n", USBDEVNAME(sc->sc_dev))); 3348 cmd->opcode = START_STOP; 3349 cmd->bytes[3] = SSS_START; 3350 return 1; 3351 } 3352 3353 switch (cmd->opcode) { 3354 /* Commands of which the format has been verified. They should work. */ 3355 case TEST_UNIT_READY: 3356 case SCSI_REZERO_UNIT: 3357 case REQUEST_SENSE: 3358 case INQUIRY: 3359 case START_STOP: 3360 /*case SEND_DIAGNOSTIC: ??*/ 3361 case PREVENT_ALLOW: 3362 case READ_CAPACITY: 3363 case READ_BIG: 3364 case WRITE_BIG: 3365 case POSITION_TO_ELEMENT: /* SEEK_10 */ 3366 case SCSI_MODE_SELECT_BIG: 3367 case SCSI_MODE_SENSE_BIG: 3368 /* Copy the command into the (zeroed out) destination buffer */ 3369 memcpy(rcmd, cmd, cmdlen); 3370 return (1); /* success */ 3371 3372 /* 3373 * Other UFI commands: FORMAT_UNIT, MODE_SELECT, READ_FORMAT_CAPACITY, 3374 * VERIFY, WRITE_AND_VERIFY. 3375 * These should be checked whether they somehow can be made to fit. 3376 */ 3377 3378 /* These commands are known _not_ to work. They should be converted. */ 3379 case SCSI_READ_COMMAND: 3380 case SCSI_WRITE_COMMAND: 3381 case SCSI_MODE_SENSE: 3382 case SCSI_MODE_SELECT: 3383 default: 3384 printf("%s: Unsupported UFI command 0x%02x", 3385 USBDEVNAME(sc->sc_dev), cmd->opcode); 3386 if (cmdlen == 6) 3387 printf(", 6 byte command should have been converted"); 3388 printf("\n"); 3389 return (0); /* failure */ 3390 } 3391 } 3392 3393 #if NATAPIBUS > 0 3394 Static void 3395 umass_atapi_probedev(struct atapibus_softc *atapi, int target) 3396 { 3397 struct scsipi_link *sc_link; 3398 struct scsipibus_attach_args sa; 3399 struct ata_drive_datas *drvp = &atapi->sc_drvs[target]; 3400 char vendor[33], product[65], revision[17]; 3401 struct scsipi_inquiry_data inqbuf; 3402 3403 DPRINTF(UDMASS_SCSI,("umass_atapi_probedev: atapi=%p target=%d\n", 3404 atapi, target)); 3405 3406 if (atapi->sc_link[target]) 3407 return; 3408 3409 sc_link = malloc(sizeof(*sc_link), M_DEVBUF, M_NOWAIT); 3410 if (sc_link == NULL) { 3411 printf("%s: can't allocate link for drive %d\n", 3412 atapi->sc_dev.dv_xname, target); 3413 return; 3414 } 3415 *sc_link = *atapi->adapter_link; 3416 3417 DIF(UDMASS_UPPER, sc_link->flags |= DEBUGLEVEL); 3418 3419 /* Fill generic parts of the link. */ 3420 sc_link->active = 0; 3421 sc_link->scsipi_atapi.drive = target; 3422 sc_link->device = &umass_dev; 3423 TAILQ_INIT(&sc_link->pending_xfers); 3424 3425 DPRINTF(UDMASS_SCSI, ("umass_atapi_probedev: doing inquiry\n")); 3426 /* Now go ask the device all about itself. */ 3427 memset(&inqbuf, 0, sizeof(inqbuf)); 3428 if (scsipi_inquire(sc_link, &inqbuf, XS_CTL_DISCOVERY) != 0) 3429 goto bad; 3430 3431 scsipi_strvis(vendor, 33, inqbuf.vendor, 8); 3432 scsipi_strvis(product, 65, inqbuf.product, 16); 3433 scsipi_strvis(revision, 17, inqbuf.revision, 4); 3434 3435 sa.sa_sc_link = sc_link; 3436 sa.sa_inqbuf.type = inqbuf.device; 3437 sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ? 3438 T_REMOV : T_FIXED; 3439 if (sa.sa_inqbuf.removable) 3440 sc_link->flags |= SDEV_REMOVABLE; 3441 /* XXX how? sc_link->scsipi_atapi.cap |= ACAP_LEN;*/ 3442 sa.sa_inqbuf.vendor = vendor; 3443 sa.sa_inqbuf.product = product; 3444 sa.sa_inqbuf.revision = revision; 3445 sa.sa_inqptr = NULL; 3446 3447 drvp->drv_softc = atapi_probedev(atapi, target, sc_link, &sa); 3448 /* atapi_probedev() frees the scsipi_link when there is no device. */ 3449 return; 3450 3451 bad: 3452 free(sc_link, M_DEVBUF); 3453 return; 3454 } 3455 #endif 3456 #endif 3457