5 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
6 * Nick Hibma <n_hibma@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
34 /* Also already merged from NetBSD:
35 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
36 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
37 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
38 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
42 * Universal Serial Bus Mass Storage Class specs:
43 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
44 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
45 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
46 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
50 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
51 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
55 * The driver handles 3 Wire Protocols
56 * - Command/Bulk/Interrupt (CBI)
57 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
58 * - Mass Storage Bulk-Only (BBB)
59 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
61 * Over these wire protocols it handles the following command protocols
63 * - UFI (floppy command set)
66 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
67 * sc->sc_transform method is used to convert the commands into the appropriate
68 * format (if at all necessary). For example, UFI requires all commands to be
69 * 12 bytes in length amongst other things.
71 * The source code below is marked and can be split into a number of pieces
74 * - probe/attach/detach
75 * - generic transfer routines
78 * - CBI_I (in addition to functions from CBI)
79 * - CAM (Common Access Method)
84 * The protocols are implemented using a state machine, for the transfers as
85 * well as for the resets. The state machine is contained in umass_t_*_callback.
86 * The state machine is started through either umass_command_start() or
89 * The reason for doing this is a) CAM performs a lot better this way and b) it
90 * avoids using tsleep from interrupt context (for example after a failed
95 * The SCSI related part of this driver has been derived from the
96 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
98 * The CAM layer uses so called actions which are messages sent to the host
99 * adapter for completion. The actions come in through umass_cam_action. The
100 * appropriate block of routines is called depending on the transport protocol
101 * in use. When the transfer has finished, these routines call
102 * umass_cam_cb again to complete the CAM command.
105 #include <sys/stdint.h>
106 #include <sys/stddef.h>
107 #include <sys/param.h>
108 #include <sys/queue.h>
109 #include <sys/types.h>
110 #include <sys/systm.h>
111 #include <sys/kernel.h>
113 #include <sys/linker_set.h>
114 #include <sys/module.h>
115 #include <sys/lock.h>
116 #include <sys/mutex.h>
117 #include <sys/condvar.h>
118 #include <sys/sysctl.h>
120 #include <sys/unistd.h>
121 #include <sys/callout.h>
122 #include <sys/malloc.h>
123 #include <sys/priv.h>
125 #include <dev/usb/usb.h>
126 #include <dev/usb/usbdi.h>
127 #include <dev/usb/usbdi_util.h>
130 #include <dev/usb/quirk/usb_quirk.h>
133 #include <cam/cam_ccb.h>
134 #include <cam/cam_sim.h>
135 #include <cam/cam_xpt_sim.h>
136 #include <cam/scsi/scsi_all.h>
137 #include <cam/scsi/scsi_da.h>
139 #include <cam/cam_periph.h>
141 #define UMASS_EXT_BUFFER
142 #ifdef UMASS_EXT_BUFFER
143 /* this enables loading of virtual buffers into DMA */
144 #define UMASS_USB_FLAGS .ext_buffer=1,
146 #define UMASS_USB_FLAGS
152 if (umass_debug & (m)) { x ; } \
155 #define DPRINTF(sc, m, fmt, ...) \
157 if (umass_debug & (m)) { \
158 printf("%s:%s: " fmt, \
159 (sc) ? (const char *)(sc)->sc_name : \
160 (const char *)"umassX", \
161 __FUNCTION__ ,## __VA_ARGS__); \
165 #define UDMASS_GEN 0x00010000 /* general */
166 #define UDMASS_SCSI 0x00020000 /* scsi */
167 #define UDMASS_UFI 0x00040000 /* ufi command set */
168 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
169 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
170 #define UDMASS_USB 0x00100000 /* USB general */
171 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
172 #define UDMASS_CBI 0x00400000 /* CBI transfers */
173 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
174 #define UDMASS_ALL 0xffff0000 /* all of the above */
175 static int umass_debug = 0;
177 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
178 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
179 &umass_debug, 0, "umass debug level");
181 TUNABLE_INT("hw.usb.umass.debug", &umass_debug);
183 #define DIF(...) do { } while (0)
184 #define DPRINTF(...) do { } while (0)
187 #define UMASS_GONE ((struct umass_softc *)1)
189 #define UMASS_BULK_SIZE (1 << 17)
190 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */
191 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */
193 /* USB transfer definitions */
195 #define UMASS_T_BBB_RESET1 0 /* Bulk-Only */
196 #define UMASS_T_BBB_RESET2 1
197 #define UMASS_T_BBB_RESET3 2
198 #define UMASS_T_BBB_COMMAND 3
199 #define UMASS_T_BBB_DATA_READ 4
200 #define UMASS_T_BBB_DATA_RD_CS 5
201 #define UMASS_T_BBB_DATA_WRITE 6
202 #define UMASS_T_BBB_DATA_WR_CS 7
203 #define UMASS_T_BBB_STATUS 8
204 #define UMASS_T_BBB_MAX 9
206 #define UMASS_T_CBI_RESET1 0 /* CBI */
207 #define UMASS_T_CBI_RESET2 1
208 #define UMASS_T_CBI_RESET3 2
209 #define UMASS_T_CBI_COMMAND 3
210 #define UMASS_T_CBI_DATA_READ 4
211 #define UMASS_T_CBI_DATA_RD_CS 5
212 #define UMASS_T_CBI_DATA_WRITE 6
213 #define UMASS_T_CBI_DATA_WR_CS 7
214 #define UMASS_T_CBI_STATUS 8
215 #define UMASS_T_CBI_RESET4 9
216 #define UMASS_T_CBI_MAX 10
218 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
220 /* Generic definitions */
222 /* Direction for transfer */
228 #define DEVNAME "umass"
229 #define DEVNAME_SIM "umass-sim"
231 /* Approximate maximum transfer speeds (assumes 33% overhead). */
232 #define UMASS_FULL_TRANSFER_SPEED 1000
233 #define UMASS_HIGH_TRANSFER_SPEED 40000
234 #define UMASS_SUPER_TRANSFER_SPEED 400000
235 #define UMASS_FLOPPY_TRANSFER_SPEED 20
237 #define UMASS_TIMEOUT 5000 /* ms */
239 /* CAM specific definitions */
241 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
242 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
244 /* Bulk-Only features */
246 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
247 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
249 /* Command Block Wrapper */
251 uDWord dCBWSignature;
252 #define CBWSIGNATURE 0x43425355
254 uDWord dCBWDataTransferLength;
256 #define CBWFLAGS_OUT 0x00
257 #define CBWFLAGS_IN 0x80
260 #define CBWCDBLENGTH 16
261 uByte CBWCDB[CBWCDBLENGTH];
262 } __packed umass_bbb_cbw_t;
264 #define UMASS_BBB_CBW_SIZE 31
266 /* Command Status Wrapper */
268 uDWord dCSWSignature;
269 #define CSWSIGNATURE 0x53425355
270 #define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
271 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
273 uDWord dCSWDataResidue;
275 #define CSWSTATUS_GOOD 0x0
276 #define CSWSTATUS_FAILED 0x1
277 #define CSWSTATUS_PHASE 0x2
278 } __packed umass_bbb_csw_t;
280 #define UMASS_BBB_CSW_SIZE 13
284 #define UR_CBI_ADSC 0x00
289 #define IDB_TYPE_CCI 0x00
291 #define IDB_VALUE_PASS 0x00
292 #define IDB_VALUE_FAIL 0x01
293 #define IDB_VALUE_PHASE 0x02
294 #define IDB_VALUE_PERSISTENT 0x03
295 #define IDB_VALUE_STATUS_MASK 0x03
302 } __packed umass_cbi_sbl_t;
304 struct umass_softc; /* see below */
306 typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
307 uint32_t residue, uint8_t status);
309 #define STATUS_CMD_OK 0 /* everything ok */
310 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
311 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
312 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
314 typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
317 /* Wire and command protocol */
318 #define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
319 #define UMASS_PROTO_CBI 0x0002
320 #define UMASS_PROTO_CBI_I 0x0004
321 #define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
322 #define UMASS_PROTO_SCSI 0x0100 /* command protocol */
323 #define UMASS_PROTO_ATAPI 0x0200
324 #define UMASS_PROTO_UFI 0x0400
325 #define UMASS_PROTO_RBC 0x0800
326 #define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
328 /* Device specific quirks */
329 #define NO_QUIRKS 0x0000
331 * The drive does not support Test Unit Ready. Convert to Start Unit
333 #define NO_TEST_UNIT_READY 0x0001
335 * The drive does not reset the Unit Attention state after REQUEST
336 * SENSE has been sent. The INQUIRY command does not reset the UA
337 * either, and so CAM runs in circles trying to retrieve the initial
340 #define RS_NO_CLEAR_UA 0x0002
341 /* The drive does not support START STOP. */
342 #define NO_START_STOP 0x0004
343 /* Don't ask for full inquiry data (255b). */
344 #define FORCE_SHORT_INQUIRY 0x0008
345 /* Needs to be initialised the Shuttle way */
346 #define SHUTTLE_INIT 0x0010
347 /* Drive needs to be switched to alternate iface 1 */
348 #define ALT_IFACE_1 0x0020
349 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
350 #define FLOPPY_SPEED 0x0040
351 /* The device can't count and gets the residue of transfers wrong */
352 #define IGNORE_RESIDUE 0x0080
353 /* No GetMaxLun call */
354 #define NO_GETMAXLUN 0x0100
355 /* The device uses a weird CSWSIGNATURE. */
356 #define WRONG_CSWSIG 0x0200
357 /* Device cannot handle INQUIRY so fake a generic response */
358 #define NO_INQUIRY 0x0400
359 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
360 #define NO_INQUIRY_EVPD 0x0800
361 /* Pad all RBC requests to 12 bytes. */
362 #define RBC_PAD_TO_12 0x1000
364 * Device reports number of sectors from READ_CAPACITY, not max
367 #define READ_CAPACITY_OFFBY1 0x2000
369 * Device cannot handle a SCSI synchronize cache command. Normally
370 * this quirk would be handled in the cam layer, but for IDE bridges
371 * we need to associate the quirk with the bridge and not the
372 * underlying disk device. This is handled by faking a success
375 #define NO_SYNCHRONIZE_CACHE 0x4000
379 struct scsi_sense cam_scsi_sense;
380 struct scsi_test_unit_ready cam_scsi_test_unit_ready;
385 umass_callback_t *callback;
387 uint32_t data_len; /* bytes */
388 uint32_t data_rem; /* bytes */
389 uint32_t data_timeout; /* ms */
390 uint32_t actlen; /* bytes */
392 uint8_t cmd_data[UMASS_MAX_CMDLEN];
393 uint8_t cmd_len; /* bytes */
398 /* Bulk specific variables for transfers in progress */
399 umass_bbb_cbw_t cbw; /* command block wrapper */
400 umass_bbb_csw_t csw; /* command status wrapper */
402 /* CBI specific variables for transfers in progress */
403 umass_cbi_sbl_t sbl; /* status block */
406 struct usb_device *sc_udev;
407 struct cam_sim *sc_sim; /* SCSI Interface Module */
408 struct usb_xfer *sc_xfer[UMASS_T_MAX];
411 * The command transform function is used to convert the SCSI
412 * commands into their derivatives, like UFI, ATAPI, and friends.
414 umass_transform_t *sc_transform;
417 uint32_t sc_quirks; /* they got it almost right */
418 uint32_t sc_proto; /* wire and cmd protocol */
421 uint8_t sc_iface_no; /* interface number */
422 uint8_t sc_maxlun; /* maximum LUN number, inclusive */
423 uint8_t sc_last_xfer_index;
424 uint8_t sc_status_try;
427 struct umass_probe_proto {
436 static device_probe_t umass_probe;
437 static device_attach_t umass_attach;
438 static device_detach_t umass_detach;
440 static usb_callback_t umass_tr_error;
441 static usb_callback_t umass_t_bbb_reset1_callback;
442 static usb_callback_t umass_t_bbb_reset2_callback;
443 static usb_callback_t umass_t_bbb_reset3_callback;
444 static usb_callback_t umass_t_bbb_command_callback;
445 static usb_callback_t umass_t_bbb_data_read_callback;
446 static usb_callback_t umass_t_bbb_data_rd_cs_callback;
447 static usb_callback_t umass_t_bbb_data_write_callback;
448 static usb_callback_t umass_t_bbb_data_wr_cs_callback;
449 static usb_callback_t umass_t_bbb_status_callback;
450 static usb_callback_t umass_t_cbi_reset1_callback;
451 static usb_callback_t umass_t_cbi_reset2_callback;
452 static usb_callback_t umass_t_cbi_reset3_callback;
453 static usb_callback_t umass_t_cbi_reset4_callback;
454 static usb_callback_t umass_t_cbi_command_callback;
455 static usb_callback_t umass_t_cbi_data_read_callback;
456 static usb_callback_t umass_t_cbi_data_rd_cs_callback;
457 static usb_callback_t umass_t_cbi_data_write_callback;
458 static usb_callback_t umass_t_cbi_data_wr_cs_callback;
459 static usb_callback_t umass_t_cbi_status_callback;
461 static void umass_cancel_ccb(struct umass_softc *);
462 static void umass_init_shuttle(struct umass_softc *);
463 static void umass_reset(struct umass_softc *);
464 static void umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
465 uint8_t, uint8_t, usb_error_t);
466 static void umass_command_start(struct umass_softc *, uint8_t, void *,
467 uint32_t, uint32_t, umass_callback_t *, union ccb *);
468 static uint8_t umass_bbb_get_max_lun(struct umass_softc *);
469 static void umass_cbi_start_status(struct umass_softc *);
470 static void umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
471 uint8_t, uint8_t, usb_error_t);
472 static int umass_cam_attach_sim(struct umass_softc *);
473 static void umass_cam_attach(struct umass_softc *);
474 static void umass_cam_detach_sim(struct umass_softc *);
475 static void umass_cam_action(struct cam_sim *, union ccb *);
476 static void umass_cam_poll(struct cam_sim *);
477 static void umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
479 static void umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
481 static void umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
483 static uint8_t umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
484 static uint8_t umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
485 static uint8_t umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
486 static uint8_t umass_atapi_transform(struct umass_softc *, uint8_t *,
488 static uint8_t umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
489 static uint8_t umass_std_transform(struct umass_softc *, union ccb *, uint8_t
493 static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
494 static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
495 static void umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
496 static void umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
500 static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
502 [UMASS_T_BBB_RESET1] = {
504 .endpoint = 0x00, /* Control pipe */
505 .direction = UE_DIR_ANY,
506 .bufsize = sizeof(struct usb_device_request),
507 .callback = &umass_t_bbb_reset1_callback,
508 .timeout = 5000, /* 5 seconds */
509 .interval = 500, /* 500 milliseconds */
512 [UMASS_T_BBB_RESET2] = {
514 .endpoint = 0x00, /* Control pipe */
515 .direction = UE_DIR_ANY,
516 .bufsize = sizeof(struct usb_device_request),
517 .callback = &umass_t_bbb_reset2_callback,
518 .timeout = 5000, /* 5 seconds */
519 .interval = 50, /* 50 milliseconds */
522 [UMASS_T_BBB_RESET3] = {
524 .endpoint = 0x00, /* Control pipe */
525 .direction = UE_DIR_ANY,
526 .bufsize = sizeof(struct usb_device_request),
527 .callback = &umass_t_bbb_reset3_callback,
528 .timeout = 5000, /* 5 seconds */
529 .interval = 50, /* 50 milliseconds */
532 [UMASS_T_BBB_COMMAND] = {
534 .endpoint = UE_ADDR_ANY,
535 .direction = UE_DIR_OUT,
536 .bufsize = sizeof(umass_bbb_cbw_t),
537 .callback = &umass_t_bbb_command_callback,
538 .timeout = 5000, /* 5 seconds */
541 [UMASS_T_BBB_DATA_READ] = {
543 .endpoint = UE_ADDR_ANY,
544 .direction = UE_DIR_IN,
545 .bufsize = UMASS_BULK_SIZE,
546 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
547 .callback = &umass_t_bbb_data_read_callback,
548 .timeout = 0, /* overwritten later */
551 [UMASS_T_BBB_DATA_RD_CS] = {
553 .endpoint = 0x00, /* Control pipe */
554 .direction = UE_DIR_ANY,
555 .bufsize = sizeof(struct usb_device_request),
556 .callback = &umass_t_bbb_data_rd_cs_callback,
557 .timeout = 5000, /* 5 seconds */
560 [UMASS_T_BBB_DATA_WRITE] = {
562 .endpoint = UE_ADDR_ANY,
563 .direction = UE_DIR_OUT,
564 .bufsize = UMASS_BULK_SIZE,
565 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
566 .callback = &umass_t_bbb_data_write_callback,
567 .timeout = 0, /* overwritten later */
570 [UMASS_T_BBB_DATA_WR_CS] = {
572 .endpoint = 0x00, /* Control pipe */
573 .direction = UE_DIR_ANY,
574 .bufsize = sizeof(struct usb_device_request),
575 .callback = &umass_t_bbb_data_wr_cs_callback,
576 .timeout = 5000, /* 5 seconds */
579 [UMASS_T_BBB_STATUS] = {
581 .endpoint = UE_ADDR_ANY,
582 .direction = UE_DIR_IN,
583 .bufsize = sizeof(umass_bbb_csw_t),
584 .flags = {.short_xfer_ok = 1,},
585 .callback = &umass_t_bbb_status_callback,
586 .timeout = 5000, /* ms */
590 static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
592 [UMASS_T_CBI_RESET1] = {
594 .endpoint = 0x00, /* Control pipe */
595 .direction = UE_DIR_ANY,
596 .bufsize = (sizeof(struct usb_device_request) +
597 UMASS_CBI_DIAGNOSTIC_CMDLEN),
598 .callback = &umass_t_cbi_reset1_callback,
599 .timeout = 5000, /* 5 seconds */
600 .interval = 500, /* 500 milliseconds */
603 [UMASS_T_CBI_RESET2] = {
605 .endpoint = 0x00, /* Control pipe */
606 .direction = UE_DIR_ANY,
607 .bufsize = sizeof(struct usb_device_request),
608 .callback = &umass_t_cbi_reset2_callback,
609 .timeout = 5000, /* 5 seconds */
610 .interval = 50, /* 50 milliseconds */
613 [UMASS_T_CBI_RESET3] = {
615 .endpoint = 0x00, /* Control pipe */
616 .direction = UE_DIR_ANY,
617 .bufsize = sizeof(struct usb_device_request),
618 .callback = &umass_t_cbi_reset3_callback,
619 .timeout = 5000, /* 5 seconds */
620 .interval = 50, /* 50 milliseconds */
623 [UMASS_T_CBI_COMMAND] = {
625 .endpoint = 0x00, /* Control pipe */
626 .direction = UE_DIR_ANY,
627 .bufsize = (sizeof(struct usb_device_request) +
629 .callback = &umass_t_cbi_command_callback,
630 .timeout = 5000, /* 5 seconds */
633 [UMASS_T_CBI_DATA_READ] = {
635 .endpoint = UE_ADDR_ANY,
636 .direction = UE_DIR_IN,
637 .bufsize = UMASS_BULK_SIZE,
638 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
639 .callback = &umass_t_cbi_data_read_callback,
640 .timeout = 0, /* overwritten later */
643 [UMASS_T_CBI_DATA_RD_CS] = {
645 .endpoint = 0x00, /* Control pipe */
646 .direction = UE_DIR_ANY,
647 .bufsize = sizeof(struct usb_device_request),
648 .callback = &umass_t_cbi_data_rd_cs_callback,
649 .timeout = 5000, /* 5 seconds */
652 [UMASS_T_CBI_DATA_WRITE] = {
654 .endpoint = UE_ADDR_ANY,
655 .direction = UE_DIR_OUT,
656 .bufsize = UMASS_BULK_SIZE,
657 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
658 .callback = &umass_t_cbi_data_write_callback,
659 .timeout = 0, /* overwritten later */
662 [UMASS_T_CBI_DATA_WR_CS] = {
664 .endpoint = 0x00, /* Control pipe */
665 .direction = UE_DIR_ANY,
666 .bufsize = sizeof(struct usb_device_request),
667 .callback = &umass_t_cbi_data_wr_cs_callback,
668 .timeout = 5000, /* 5 seconds */
671 [UMASS_T_CBI_STATUS] = {
672 .type = UE_INTERRUPT,
673 .endpoint = UE_ADDR_ANY,
674 .direction = UE_DIR_IN,
675 .flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,},
676 .bufsize = sizeof(umass_cbi_sbl_t),
677 .callback = &umass_t_cbi_status_callback,
678 .timeout = 5000, /* ms */
681 [UMASS_T_CBI_RESET4] = {
683 .endpoint = 0x00, /* Control pipe */
684 .direction = UE_DIR_ANY,
685 .bufsize = sizeof(struct usb_device_request),
686 .callback = &umass_t_cbi_reset4_callback,
687 .timeout = 5000, /* ms */
691 /* If device cannot return valid inquiry data, fake it */
692 static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
693 0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
694 /* additional_length */ 31, 0, 0, 0
697 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
698 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
700 static devclass_t umass_devclass;
702 static device_method_t umass_methods[] = {
703 /* Device interface */
704 DEVMETHOD(device_probe, umass_probe),
705 DEVMETHOD(device_attach, umass_attach),
706 DEVMETHOD(device_detach, umass_detach),
710 static driver_t umass_driver = {
712 .methods = umass_methods,
713 .size = sizeof(struct umass_softc),
716 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
717 MODULE_DEPEND(umass, usb, 1, 1, 1);
718 MODULE_DEPEND(umass, cam, 1, 1, 1);
719 MODULE_VERSION(umass, 1);
722 * USB device probe/attach/detach
726 umass_get_proto(struct usb_interface *iface)
728 struct usb_interface_descriptor *id;
733 /* Check for a standards compliant device */
734 id = usbd_get_interface_descriptor(iface);
736 (id->bInterfaceClass != UICLASS_MASS)) {
739 switch (id->bInterfaceSubClass) {
740 case UISUBCLASS_SCSI:
741 retval |= UMASS_PROTO_SCSI;
744 retval |= UMASS_PROTO_UFI;
747 retval |= UMASS_PROTO_RBC;
749 case UISUBCLASS_SFF8020I:
750 case UISUBCLASS_SFF8070I:
751 retval |= UMASS_PROTO_ATAPI;
757 switch (id->bInterfaceProtocol) {
758 case UIPROTO_MASS_CBI:
759 retval |= UMASS_PROTO_CBI;
761 case UIPROTO_MASS_CBI_I:
762 retval |= UMASS_PROTO_CBI_I;
764 case UIPROTO_MASS_BBB_OLD:
765 case UIPROTO_MASS_BBB:
766 retval |= UMASS_PROTO_BBB;
776 * Match the device we are seeing with the devices supported.
778 static struct umass_probe_proto
779 umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
781 struct umass_probe_proto ret;
782 uint32_t quirks = NO_QUIRKS;
783 uint32_t proto = umass_get_proto(uaa->iface);
785 memset(&ret, 0, sizeof(ret));
787 /* Search for protocol enforcement */
789 if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) {
790 proto &= ~UMASS_PROTO_WIRE;
791 proto |= UMASS_PROTO_BBB;
792 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) {
793 proto &= ~UMASS_PROTO_WIRE;
794 proto |= UMASS_PROTO_CBI;
795 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) {
796 proto &= ~UMASS_PROTO_WIRE;
797 proto |= UMASS_PROTO_CBI_I;
800 if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) {
801 proto &= ~UMASS_PROTO_COMMAND;
802 proto |= UMASS_PROTO_SCSI;
803 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) {
804 proto &= ~UMASS_PROTO_COMMAND;
805 proto |= UMASS_PROTO_ATAPI;
806 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) {
807 proto &= ~UMASS_PROTO_COMMAND;
808 proto |= UMASS_PROTO_UFI;
809 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) {
810 proto &= ~UMASS_PROTO_COMMAND;
811 proto |= UMASS_PROTO_RBC;
814 /* Check if the protocol is invalid */
816 if ((proto & UMASS_PROTO_COMMAND) == 0) {
821 if ((proto & UMASS_PROTO_WIRE) == 0) {
826 /* Search for quirks */
828 if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY))
829 quirks |= NO_TEST_UNIT_READY;
830 if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA))
831 quirks |= RS_NO_CLEAR_UA;
832 if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP))
833 quirks |= NO_START_STOP;
834 if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN))
835 quirks |= NO_GETMAXLUN;
836 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY))
837 quirks |= NO_INQUIRY;
838 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD))
839 quirks |= NO_INQUIRY_EVPD;
840 if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE))
841 quirks |= NO_SYNCHRONIZE_CACHE;
842 if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT))
843 quirks |= SHUTTLE_INIT;
844 if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1))
845 quirks |= ALT_IFACE_1;
846 if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED))
847 quirks |= FLOPPY_SPEED;
848 if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE))
849 quirks |= IGNORE_RESIDUE;
850 if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG))
851 quirks |= WRONG_CSWSIG;
852 if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12))
853 quirks |= RBC_PAD_TO_12;
854 if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1))
855 quirks |= READ_CAPACITY_OFFBY1;
856 if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ))
857 quirks |= FORCE_SHORT_INQUIRY;
866 umass_probe(device_t dev)
868 struct usb_attach_arg *uaa = device_get_ivars(dev);
869 struct umass_probe_proto temp;
871 if (uaa->usb_mode != USB_MODE_HOST) {
874 if (uaa->use_generic == 0) {
875 /* give other drivers a try first */
878 temp = umass_probe_proto(dev, uaa);
884 umass_attach(device_t dev)
886 struct umass_softc *sc = device_get_softc(dev);
887 struct usb_attach_arg *uaa = device_get_ivars(dev);
888 struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
889 struct usb_interface_descriptor *id;
893 * NOTE: the softc struct is bzero-ed in device_set_driver.
894 * We can safely call umass_detach without specifically
895 * initializing the struct.
899 sc->sc_udev = uaa->device;
900 sc->sc_proto = temp.proto;
901 sc->sc_quirks = temp.quirks;
902 sc->sc_unit = device_get_unit(dev);
904 snprintf(sc->sc_name, sizeof(sc->sc_name),
905 "%s", device_get_nameunit(dev));
907 device_set_usb_desc(dev);
909 mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
910 NULL, MTX_DEF | MTX_RECURSE);
912 /* get interface index */
914 id = usbd_get_interface_descriptor(uaa->iface);
916 device_printf(dev, "failed to get "
917 "interface number\n");
920 sc->sc_iface_no = id->bInterfaceNumber;
923 device_printf(dev, " ");
925 switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
926 case UMASS_PROTO_SCSI:
929 case UMASS_PROTO_ATAPI:
930 printf("8070i (ATAPI)");
932 case UMASS_PROTO_UFI:
935 case UMASS_PROTO_RBC:
939 printf("(unknown 0x%02x)",
940 sc->sc_proto & UMASS_PROTO_COMMAND);
946 switch (sc->sc_proto & UMASS_PROTO_WIRE) {
947 case UMASS_PROTO_BBB:
950 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
953 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
954 printf("CBI with CCI");
957 printf("(unknown 0x%02x)",
958 sc->sc_proto & UMASS_PROTO_WIRE);
961 printf("; quirks = 0x%04x\n", sc->sc_quirks);
964 if (sc->sc_quirks & ALT_IFACE_1) {
965 err = usbd_set_alt_interface_index
966 (uaa->device, uaa->info.bIfaceIndex, 1);
969 DPRINTF(sc, UDMASS_USB, "could not switch to "
970 "Alt Interface 1\n");
974 /* allocate all required USB transfers */
976 if (sc->sc_proto & UMASS_PROTO_BBB) {
978 err = usbd_transfer_setup(uaa->device,
979 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
980 UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
982 /* skip reset first time */
983 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
985 } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
987 err = usbd_transfer_setup(uaa->device,
988 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
989 UMASS_T_CBI_MAX, sc, &sc->sc_mtx);
991 /* skip reset first time */
992 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
999 device_printf(dev, "could not setup required "
1000 "transfers, %s\n", usbd_errstr(err));
1004 (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1005 (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1006 (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1007 (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1008 &umass_no_transform;
1010 /* from here onwards the device can be used. */
1012 if (sc->sc_quirks & SHUTTLE_INIT) {
1013 umass_init_shuttle(sc);
1015 /* get the maximum LUN supported by the device */
1017 if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1018 !(sc->sc_quirks & NO_GETMAXLUN))
1019 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1023 /* Prepare the SCSI command block */
1024 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1025 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1028 * some devices need a delay after that the configuration value is
1029 * set to function properly:
1031 usb_pause_mtx(NULL, hz);
1033 /* register the SIM */
1034 err = umass_cam_attach_sim(sc);
1039 umass_cam_attach(sc);
1041 DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1043 return (0); /* success */
1047 return (ENXIO); /* failure */
1051 umass_detach(device_t dev)
1053 struct umass_softc *sc = device_get_softc(dev);
1055 DPRINTF(sc, UDMASS_USB, "\n");
1057 /* teardown our statemachine */
1059 usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1061 #if (__FreeBSD_version >= 700037)
1062 mtx_lock(&sc->sc_mtx);
1064 umass_cam_detach_sim(sc);
1066 #if (__FreeBSD_version >= 700037)
1067 mtx_unlock(&sc->sc_mtx);
1069 mtx_destroy(&sc->sc_mtx);
1071 return (0); /* success */
1075 umass_init_shuttle(struct umass_softc *sc)
1077 struct usb_device_request req;
1079 uint8_t status[2] = {0, 0};
1082 * The Linux driver does this, but no one can tell us what the
1085 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1086 req.bRequest = 1; /* XXX unknown command */
1087 USETW(req.wValue, 0);
1088 req.wIndex[0] = sc->sc_iface_no;
1090 USETW(req.wLength, sizeof(status));
1091 err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1093 DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1094 status[0], status[1]);
1098 * Generic functions to handle transfers
1102 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1104 DPRINTF(sc, UDMASS_GEN, "transfer index = "
1105 "%d\n", xfer_index);
1107 if (sc->sc_xfer[xfer_index]) {
1108 sc->sc_last_xfer_index = xfer_index;
1109 usbd_transfer_start(sc->sc_xfer[xfer_index]);
1111 umass_cancel_ccb(sc);
1116 umass_reset(struct umass_softc *sc)
1118 DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1121 * stop the last transfer, if not already stopped:
1123 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1124 umass_transfer_start(sc, 0);
1128 umass_cancel_ccb(struct umass_softc *sc)
1132 mtx_assert(&sc->sc_mtx, MA_OWNED);
1134 ccb = sc->sc_transfer.ccb;
1135 sc->sc_transfer.ccb = NULL;
1136 sc->sc_last_xfer_index = 0;
1139 (sc->sc_transfer.callback)
1140 (sc, ccb, (sc->sc_transfer.data_len -
1141 sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1146 umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1148 struct umass_softc *sc = usbd_xfer_softc(xfer);
1150 if (error != USB_ERR_CANCELLED) {
1152 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1153 "reset\n", usbd_errstr(error));
1155 umass_cancel_ccb(sc);
1159 * BBB protocol specific functions
1163 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1165 struct umass_softc *sc = usbd_xfer_softc(xfer);
1166 struct usb_device_request req;
1167 struct usb_page_cache *pc;
1169 switch (USB_GET_STATE(xfer)) {
1170 case USB_ST_TRANSFERRED:
1171 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1176 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1178 * For Reset Recovery the host shall issue in the following order:
1179 * a) a Bulk-Only Mass Storage Reset
1180 * b) a Clear Feature HALT to the Bulk-In endpoint
1181 * c) a Clear Feature HALT to the Bulk-Out endpoint
1183 * This is done in 3 steps, using 3 transfers:
1184 * UMASS_T_BBB_RESET1
1185 * UMASS_T_BBB_RESET2
1186 * UMASS_T_BBB_RESET3
1189 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1191 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1192 req.bRequest = UR_BBB_RESET; /* bulk only reset */
1193 USETW(req.wValue, 0);
1194 req.wIndex[0] = sc->sc_iface_no;
1196 USETW(req.wLength, 0);
1198 pc = usbd_xfer_get_frame(xfer, 0);
1199 usbd_copy_in(pc, 0, &req, sizeof(req));
1201 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1202 usbd_xfer_set_frames(xfer, 1);
1203 usbd_transfer_submit(xfer);
1206 default: /* Error */
1207 umass_tr_error(xfer, error);
1214 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1216 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1217 UMASS_T_BBB_DATA_READ, error);
1221 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1223 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1224 UMASS_T_BBB_DATA_WRITE, error);
1228 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1229 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1231 struct umass_softc *sc = usbd_xfer_softc(xfer);
1233 switch (USB_GET_STATE(xfer)) {
1234 case USB_ST_TRANSFERRED:
1236 umass_transfer_start(sc, next_xfer);
1240 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1241 goto tr_transferred;
1245 default: /* Error */
1246 umass_tr_error(xfer, error);
1253 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1255 struct umass_softc *sc = usbd_xfer_softc(xfer);
1256 union ccb *ccb = sc->sc_transfer.ccb;
1257 struct usb_page_cache *pc;
1260 switch (USB_GET_STATE(xfer)) {
1261 case USB_ST_TRANSFERRED:
1262 umass_transfer_start
1263 (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1264 (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1265 UMASS_T_BBB_STATUS));
1270 sc->sc_status_try = 0;
1275 * the initial value is not important,
1276 * as long as the values are unique:
1278 tag = UGETDW(sc->cbw.dCBWTag) + 1;
1280 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1281 USETDW(sc->cbw.dCBWTag, tag);
1284 * dCBWDataTransferLength:
1285 * This field indicates the number of bytes of data that the host
1286 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1287 * the Direction bit) during the execution of this command. If this
1288 * field is set to 0, the device will expect that no data will be
1289 * transferred IN or OUT during this command, regardless of the value
1290 * of the Direction bit defined in dCBWFlags.
1292 USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1296 * The bits of the Flags field are defined as follows:
1298 * Bit 7 Direction - this bit shall be ignored if the
1299 * dCBWDataTransferLength field is zero.
1300 * 0 = data Out from host to device
1301 * 1 = data In from device to host
1303 sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1304 CBWFLAGS_IN : CBWFLAGS_OUT);
1305 sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1307 if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1308 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1309 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1311 sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1313 bcopy(sc->sc_transfer.cmd_data, sc->cbw.CBWCDB,
1314 sc->sc_transfer.cmd_len);
1316 bzero(sc->sc_transfer.cmd_data + sc->sc_transfer.cmd_len,
1317 sizeof(sc->cbw.CBWCDB) - sc->sc_transfer.cmd_len);
1319 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1321 pc = usbd_xfer_get_frame(xfer, 0);
1322 usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1323 usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1325 usbd_transfer_submit(xfer);
1329 default: /* Error */
1330 umass_tr_error(xfer, error);
1337 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1339 struct umass_softc *sc = usbd_xfer_softc(xfer);
1340 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1341 #ifndef UMASS_EXT_BUFFER
1342 struct usb_page_cache *pc;
1346 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1348 switch (USB_GET_STATE(xfer)) {
1349 case USB_ST_TRANSFERRED:
1350 #ifndef UMASS_EXT_BUFFER
1351 pc = usbd_xfer_get_frame(xfer, 0);
1352 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1354 sc->sc_transfer.data_rem -= actlen;
1355 sc->sc_transfer.data_ptr += actlen;
1356 sc->sc_transfer.actlen += actlen;
1358 if (actlen < sumlen) {
1359 /* short transfer */
1360 sc->sc_transfer.data_rem = 0;
1363 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1364 max_bulk, sc->sc_transfer.data_rem);
1366 if (sc->sc_transfer.data_rem == 0) {
1367 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1370 if (max_bulk > sc->sc_transfer.data_rem) {
1371 max_bulk = sc->sc_transfer.data_rem;
1373 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1375 #ifdef UMASS_EXT_BUFFER
1376 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1379 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1381 usbd_transfer_submit(xfer);
1384 default: /* Error */
1385 if (error == USB_ERR_CANCELLED) {
1386 umass_tr_error(xfer, error);
1388 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1396 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1398 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1399 UMASS_T_BBB_DATA_READ, error);
1403 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1405 struct umass_softc *sc = usbd_xfer_softc(xfer);
1406 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1407 #ifndef UMASS_EXT_BUFFER
1408 struct usb_page_cache *pc;
1412 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1414 switch (USB_GET_STATE(xfer)) {
1415 case USB_ST_TRANSFERRED:
1416 sc->sc_transfer.data_rem -= actlen;
1417 sc->sc_transfer.data_ptr += actlen;
1418 sc->sc_transfer.actlen += actlen;
1420 if (actlen < sumlen) {
1421 /* short transfer */
1422 sc->sc_transfer.data_rem = 0;
1425 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1426 max_bulk, sc->sc_transfer.data_rem);
1428 if (sc->sc_transfer.data_rem == 0) {
1429 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1432 if (max_bulk > sc->sc_transfer.data_rem) {
1433 max_bulk = sc->sc_transfer.data_rem;
1435 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1437 #ifdef UMASS_EXT_BUFFER
1438 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1441 pc = usbd_xfer_get_frame(xfer, 0);
1442 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
1443 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1446 usbd_transfer_submit(xfer);
1449 default: /* Error */
1450 if (error == USB_ERR_CANCELLED) {
1451 umass_tr_error(xfer, error);
1453 umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1461 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1463 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1464 UMASS_T_BBB_DATA_WRITE, error);
1468 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
1470 struct umass_softc *sc = usbd_xfer_softc(xfer);
1471 union ccb *ccb = sc->sc_transfer.ccb;
1472 struct usb_page_cache *pc;
1476 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1478 switch (USB_GET_STATE(xfer)) {
1479 case USB_ST_TRANSFERRED:
1482 * Do a full reset if there is something wrong with the CSW:
1484 sc->sc_status_try = 1;
1486 /* Zero missing parts of the CSW: */
1488 if (actlen < sizeof(sc->csw)) {
1489 bzero(&sc->csw, sizeof(sc->csw));
1491 pc = usbd_xfer_get_frame(xfer, 0);
1492 usbd_copy_out(pc, 0, &sc->csw, actlen);
1494 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1496 residue = UGETDW(sc->csw.dCSWDataResidue);
1498 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
1499 residue = (sc->sc_transfer.data_len -
1500 sc->sc_transfer.actlen);
1502 if (residue > sc->sc_transfer.data_len) {
1503 DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
1504 "to %d bytes\n", residue, sc->sc_transfer.data_len);
1505 residue = sc->sc_transfer.data_len;
1507 /* translate weird command-status signatures: */
1508 if (sc->sc_quirks & WRONG_CSWSIG) {
1510 uint32_t temp = UGETDW(sc->csw.dCSWSignature);
1512 if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
1513 (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
1514 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1517 /* check CSW and handle eventual error */
1518 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1519 DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
1520 UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
1522 * Invalid CSW: Wrong signature or wrong tag might
1523 * indicate that we lost synchronization. Reset the
1527 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
1528 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
1529 "0x%08x\n", UGETDW(sc->csw.dCSWTag),
1530 UGETDW(sc->cbw.dCBWTag));
1532 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1533 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
1534 sc->csw.bCSWStatus, CSWSTATUS_PHASE);
1536 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1537 DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
1540 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
1541 DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
1542 sc->sc_transfer.actlen, sc->sc_transfer.data_len);
1544 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1545 DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
1548 sc->sc_transfer.ccb = NULL;
1550 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1552 (sc->sc_transfer.callback)
1553 (sc, ccb, residue, STATUS_CMD_FAILED);
1555 sc->sc_transfer.ccb = NULL;
1557 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1559 (sc->sc_transfer.callback)
1560 (sc, ccb, residue, STATUS_CMD_OK);
1565 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1566 usbd_transfer_submit(xfer);
1571 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
1572 usbd_errstr(error), sc->sc_status_try);
1574 if ((error == USB_ERR_CANCELLED) ||
1575 (sc->sc_status_try)) {
1576 umass_tr_error(xfer, error);
1578 sc->sc_status_try = 1;
1579 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1587 umass_command_start(struct umass_softc *sc, uint8_t dir,
1588 void *data_ptr, uint32_t data_len,
1589 uint32_t data_timeout, umass_callback_t *callback,
1592 sc->sc_transfer.lun = ccb->ccb_h.target_lun;
1595 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1596 * "sc->sc_transfer.cmd_len" has been properly
1600 sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
1601 sc->sc_transfer.data_ptr = data_ptr;
1602 sc->sc_transfer.data_len = data_len;
1603 sc->sc_transfer.data_rem = data_len;
1604 sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
1606 sc->sc_transfer.actlen = 0;
1607 sc->sc_transfer.callback = callback;
1608 sc->sc_transfer.ccb = ccb;
1610 if (sc->sc_xfer[sc->sc_last_xfer_index]) {
1611 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
1613 ccb->ccb_h.status = CAM_TID_INVALID;
1619 umass_bbb_get_max_lun(struct umass_softc *sc)
1621 struct usb_device_request req;
1625 /* The Get Max Lun command is a class-specific request. */
1626 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1627 req.bRequest = UR_BBB_GET_MAX_LUN;
1628 USETW(req.wValue, 0);
1629 req.wIndex[0] = sc->sc_iface_no;
1631 USETW(req.wLength, 1);
1633 err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
1637 /* Device doesn't support Get Max Lun request. */
1638 printf("%s: Get Max Lun not supported (%s)\n",
1639 sc->sc_name, usbd_errstr(err));
1645 * Command/Bulk/Interrupt (CBI) specific functions
1649 umass_cbi_start_status(struct umass_softc *sc)
1651 if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
1652 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
1654 union ccb *ccb = sc->sc_transfer.ccb;
1656 sc->sc_transfer.ccb = NULL;
1658 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1660 (sc->sc_transfer.callback)
1661 (sc, ccb, (sc->sc_transfer.data_len -
1662 sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
1667 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1669 struct umass_softc *sc = usbd_xfer_softc(xfer);
1670 struct usb_device_request req;
1671 struct usb_page_cache *pc;
1672 uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
1676 switch (USB_GET_STATE(xfer)) {
1677 case USB_ST_TRANSFERRED:
1678 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1683 * Command Block Reset Protocol
1685 * First send a reset request to the device. Then clear
1686 * any possibly stalled bulk endpoints.
1688 * This is done in 3 steps, using 3 transfers:
1689 * UMASS_T_CBI_RESET1
1690 * UMASS_T_CBI_RESET2
1691 * UMASS_T_CBI_RESET3
1692 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1695 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
1697 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1698 req.bRequest = UR_CBI_ADSC;
1699 USETW(req.wValue, 0);
1700 req.wIndex[0] = sc->sc_iface_no;
1702 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
1705 * The 0x1d code is the SEND DIAGNOSTIC command. To
1706 * distinguish between the two, the last 10 bytes of the CBL
1707 * is filled with 0xff (section 2.2 of the CBI
1710 buf[0] = 0x1d; /* Command Block Reset */
1713 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
1717 pc = usbd_xfer_get_frame(xfer, 0);
1718 usbd_copy_in(pc, 0, &req, sizeof(req));
1719 pc = usbd_xfer_get_frame(xfer, 1);
1720 usbd_copy_in(pc, 0, buf, sizeof(buf));
1722 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1723 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
1724 usbd_xfer_set_frames(xfer, 2);
1725 usbd_transfer_submit(xfer);
1728 default: /* Error */
1729 if (error == USB_ERR_CANCELLED)
1730 umass_tr_error(xfer, error);
1732 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1739 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1741 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
1742 UMASS_T_CBI_DATA_READ, error);
1746 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1748 struct umass_softc *sc = usbd_xfer_softc(xfer);
1750 umass_t_cbi_data_clear_stall_callback
1751 (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
1752 sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
1753 UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
1754 UMASS_T_CBI_DATA_WRITE, error);
1758 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
1760 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
1761 UMASS_T_CBI_STATUS, error);
1765 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
1766 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1768 struct umass_softc *sc = usbd_xfer_softc(xfer);
1770 switch (USB_GET_STATE(xfer)) {
1771 case USB_ST_TRANSFERRED:
1773 if (next_xfer == UMASS_T_CBI_STATUS) {
1774 umass_cbi_start_status(sc);
1776 umass_transfer_start(sc, next_xfer);
1781 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1782 goto tr_transferred; /* should not happen */
1786 default: /* Error */
1787 umass_tr_error(xfer, error);
1794 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
1796 struct umass_softc *sc = usbd_xfer_softc(xfer);
1797 union ccb *ccb = sc->sc_transfer.ccb;
1798 struct usb_device_request req;
1799 struct usb_page_cache *pc;
1801 switch (USB_GET_STATE(xfer)) {
1802 case USB_ST_TRANSFERRED:
1804 if (sc->sc_transfer.dir == DIR_NONE) {
1805 umass_cbi_start_status(sc);
1807 umass_transfer_start
1808 (sc, (sc->sc_transfer.dir == DIR_IN) ?
1809 UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
1818 * do a CBI transfer with cmd_len bytes from
1819 * cmd_data, possibly a data phase of data_len
1820 * bytes from/to the device and finally a status
1824 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1825 req.bRequest = UR_CBI_ADSC;
1826 USETW(req.wValue, 0);
1827 req.wIndex[0] = sc->sc_iface_no;
1829 req.wLength[0] = sc->sc_transfer.cmd_len;
1832 pc = usbd_xfer_get_frame(xfer, 0);
1833 usbd_copy_in(pc, 0, &req, sizeof(req));
1834 pc = usbd_xfer_get_frame(xfer, 1);
1835 usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
1836 sc->sc_transfer.cmd_len);
1838 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1839 usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
1840 usbd_xfer_set_frames(xfer,
1841 sc->sc_transfer.cmd_len ? 2 : 1);
1844 umass_cbi_dump_cmd(sc,
1845 sc->sc_transfer.cmd_data,
1846 sc->sc_transfer.cmd_len));
1848 usbd_transfer_submit(xfer);
1852 default: /* Error */
1853 umass_tr_error(xfer, error);
1855 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1861 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1863 struct umass_softc *sc = usbd_xfer_softc(xfer);
1864 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1865 #ifndef UMASS_EXT_BUFFER
1866 struct usb_page_cache *pc;
1870 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1872 switch (USB_GET_STATE(xfer)) {
1873 case USB_ST_TRANSFERRED:
1874 #ifndef UMASS_EXT_BUFFER
1875 pc = usbd_xfer_get_frame(xfer, 0);
1876 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1878 sc->sc_transfer.data_rem -= actlen;
1879 sc->sc_transfer.data_ptr += actlen;
1880 sc->sc_transfer.actlen += actlen;
1882 if (actlen < sumlen) {
1883 /* short transfer */
1884 sc->sc_transfer.data_rem = 0;
1887 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1888 max_bulk, sc->sc_transfer.data_rem);
1890 if (sc->sc_transfer.data_rem == 0) {
1891 umass_cbi_start_status(sc);
1894 if (max_bulk > sc->sc_transfer.data_rem) {
1895 max_bulk = sc->sc_transfer.data_rem;
1897 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1899 #ifdef UMASS_EXT_BUFFER
1900 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1903 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1905 usbd_transfer_submit(xfer);
1908 default: /* Error */
1909 if ((error == USB_ERR_CANCELLED) ||
1910 (sc->sc_transfer.callback != &umass_cam_cb)) {
1911 umass_tr_error(xfer, error);
1913 umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
1921 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1923 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1924 UMASS_T_CBI_DATA_READ, error);
1928 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1930 struct umass_softc *sc = usbd_xfer_softc(xfer);
1931 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1932 #ifndef UMASS_EXT_BUFFER
1933 struct usb_page_cache *pc;
1937 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1939 switch (USB_GET_STATE(xfer)) {
1940 case USB_ST_TRANSFERRED:
1941 sc->sc_transfer.data_rem -= actlen;
1942 sc->sc_transfer.data_ptr += actlen;
1943 sc->sc_transfer.actlen += actlen;
1945 if (actlen < sumlen) {
1946 /* short transfer */
1947 sc->sc_transfer.data_rem = 0;
1950 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1951 max_bulk, sc->sc_transfer.data_rem);
1953 if (sc->sc_transfer.data_rem == 0) {
1954 umass_cbi_start_status(sc);
1957 if (max_bulk > sc->sc_transfer.data_rem) {
1958 max_bulk = sc->sc_transfer.data_rem;
1960 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1962 #ifdef UMASS_EXT_BUFFER
1963 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1966 pc = usbd_xfer_get_frame(xfer, 0);
1967 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
1968 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1971 usbd_transfer_submit(xfer);
1974 default: /* Error */
1975 if ((error == USB_ERR_CANCELLED) ||
1976 (sc->sc_transfer.callback != &umass_cam_cb)) {
1977 umass_tr_error(xfer, error);
1979 umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
1987 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1989 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1990 UMASS_T_CBI_DATA_WRITE, error);
1994 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
1996 struct umass_softc *sc = usbd_xfer_softc(xfer);
1997 union ccb *ccb = sc->sc_transfer.ccb;
1998 struct usb_page_cache *pc;
2003 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2005 switch (USB_GET_STATE(xfer)) {
2006 case USB_ST_TRANSFERRED:
2008 if (actlen < sizeof(sc->sbl)) {
2011 pc = usbd_xfer_get_frame(xfer, 0);
2012 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
2014 residue = (sc->sc_transfer.data_len -
2015 sc->sc_transfer.actlen);
2017 /* dissect the information in the buffer */
2019 if (sc->sc_proto & UMASS_PROTO_UFI) {
2022 * Section 3.4.3.1.3 specifies that the UFI command
2023 * protocol returns an ASC and ASCQ in the interrupt
2027 DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2028 "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2031 status = (((sc->sbl.ufi.asc == 0) &&
2032 (sc->sbl.ufi.ascq == 0)) ?
2033 STATUS_CMD_OK : STATUS_CMD_FAILED);
2035 sc->sc_transfer.ccb = NULL;
2037 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2039 (sc->sc_transfer.callback)
2040 (sc, ccb, residue, status);
2046 /* Command Interrupt Data Block */
2048 DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2049 sc->sbl.common.type, sc->sbl.common.value);
2051 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2053 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2055 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2056 (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2057 (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2058 STATUS_WIRE_FAILED);
2060 sc->sc_transfer.ccb = NULL;
2062 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2064 (sc->sc_transfer.callback)
2065 (sc, ccb, residue, status);
2075 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2076 usbd_transfer_submit(xfer);
2079 default: /* Error */
2080 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2081 usbd_errstr(error));
2082 umass_tr_error(xfer, error);
2089 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2093 umass_cam_attach_sim(struct umass_softc *sc)
2095 struct cam_devq *devq; /* Per device Queue */
2098 * A HBA is attached to the CAM layer.
2100 * The CAM layer will then after a while start probing for devices on
2101 * the bus. The number of SIMs is limited to one.
2104 devq = cam_simq_alloc(1 /* maximum openings */ );
2108 sc->sc_sim = cam_sim_alloc
2109 (&umass_cam_action, &umass_cam_poll,
2112 sc->sc_unit /* unit number */ ,
2113 #if (__FreeBSD_version >= 700037)
2114 &sc->sc_mtx /* mutex */ ,
2116 1 /* maximum device openings */ ,
2117 0 /* maximum tagged device openings */ ,
2120 if (sc->sc_sim == NULL) {
2121 cam_simq_free(devq);
2125 #if (__FreeBSD_version >= 700037)
2126 mtx_lock(&sc->sc_mtx);
2129 #if (__FreeBSD_version >= 700048)
2130 if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2131 mtx_unlock(&sc->sc_mtx);
2135 if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2136 #if (__FreeBSD_version >= 700037)
2137 mtx_unlock(&sc->sc_mtx);
2143 #if (__FreeBSD_version >= 700037)
2144 mtx_unlock(&sc->sc_mtx);
2150 umass_cam_attach(struct umass_softc *sc)
2155 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2156 sc->sc_name, cam_sim_path(sc->sc_sim),
2157 sc->sc_unit, CAM_LUN_WILDCARD,
2158 cam_sim_path(sc->sc_sim));
2162 * detach from the CAM layer
2166 umass_cam_detach_sim(struct umass_softc *sc)
2168 if (sc->sc_sim != NULL) {
2169 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2170 /* accessing the softc is not possible after this */
2171 sc->sc_sim->softc = UMASS_GONE;
2172 cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2174 panic("%s: CAM layer is busy\n",
2182 * CAM requests for action come through here
2186 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2188 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2190 if (sc == UMASS_GONE ||
2191 (sc != NULL && !usbd_device_attached(sc->sc_udev))) {
2192 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2197 #if (__FreeBSD_version < 700037)
2198 mtx_lock(&sc->sc_mtx);
2202 * Verify, depending on the operation to perform, that we either got
2203 * a valid sc, because an existing target was referenced, or
2204 * otherwise the SIM is addressed.
2206 * This avoids bombing out at a printf and does give the CAM layer some
2207 * sensible feedback on errors.
2209 switch (ccb->ccb_h.func_code) {
2212 case XPT_GET_TRAN_SETTINGS:
2213 case XPT_SET_TRAN_SETTINGS:
2214 case XPT_CALC_GEOMETRY:
2215 /* the opcodes requiring a target. These should never occur. */
2217 DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2218 "Invalid target (target needed)\n",
2219 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2220 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2221 ccb->ccb_h.func_code);
2223 ccb->ccb_h.status = CAM_TID_INVALID;
2231 * The opcodes sometimes aimed at a target (sc is valid),
2232 * sometimes aimed at the SIM (sc is invalid and target is
2233 * CAM_TARGET_WILDCARD)
2236 (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2237 DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2238 "Invalid target (no wildcard)\n",
2239 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2240 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2241 ccb->ccb_h.func_code);
2243 ccb->ccb_h.status = CAM_TID_INVALID;
2249 /* XXX Hm, we should check the input parameters */
2253 /* Perform the requested action */
2254 switch (ccb->ccb_h.func_code) {
2260 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2261 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2263 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2266 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2267 "cmd: 0x%02x, flags: 0x%02x, "
2268 "%db cmd/%db data/%db sense\n",
2269 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2270 ccb->ccb_h.target_lun, cmd[0],
2271 ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2272 ccb->csio.dxfer_len, ccb->csio.sense_len);
2274 if (sc->sc_transfer.ccb) {
2275 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2276 "I/O in progress, deferring\n",
2277 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2278 ccb->ccb_h.target_lun);
2279 ccb->ccb_h.status = CAM_SCSI_BUSY;
2283 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2290 umass_dump_buffer(sc, ccb->csio.data_ptr,
2291 ccb->csio.dxfer_len, 48));
2297 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2300 * sc->sc_transform will convert the command to the
2301 * command format needed by the specific command set
2302 * and return the converted command in
2303 * "sc->sc_transfer.cmd_data"
2305 if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2307 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2308 const char *pserial;
2310 pserial = usb_get_serial(sc->sc_udev);
2313 * Umass devices don't generally report their serial numbers
2314 * in the usual SCSI way. Emulate it here.
2316 if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2317 (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) &&
2318 (pserial[0] != '\0')) {
2319 struct scsi_vpd_unit_serial_number *vpd_serial;
2321 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2322 vpd_serial->length = strlen(pserial);
2323 if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2324 vpd_serial->length = sizeof(vpd_serial->serial_num);
2325 memcpy(vpd_serial->serial_num, pserial, vpd_serial->length);
2326 ccb->csio.scsi_status = SCSI_STATUS_OK;
2327 ccb->ccb_h.status = CAM_REQ_CMP;
2333 * Handle EVPD inquiry for broken devices first
2334 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2336 if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2337 (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2338 struct scsi_sense_data *sense;
2340 sense = &ccb->csio.sense_data;
2341 bzero(sense, sizeof(*sense));
2342 sense->error_code = SSD_CURRENT_ERROR;
2343 sense->flags = SSD_KEY_ILLEGAL_REQUEST;
2344 sense->add_sense_code = 0x24;
2345 sense->extra_len = 10;
2346 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2347 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2353 * Return fake inquiry data for
2356 if (sc->sc_quirks & NO_INQUIRY) {
2357 memcpy(ccb->csio.data_ptr, &fake_inq_data,
2358 sizeof(fake_inq_data));
2359 ccb->csio.scsi_status = SCSI_STATUS_OK;
2360 ccb->ccb_h.status = CAM_REQ_CMP;
2364 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2365 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2367 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2368 if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2369 ccb->csio.scsi_status = SCSI_STATUS_OK;
2370 ccb->ccb_h.status = CAM_REQ_CMP;
2375 umass_command_start(sc, dir, ccb->csio.data_ptr,
2376 ccb->csio.dxfer_len,
2378 &umass_cam_cb, ccb);
2384 struct ccb_pathinq *cpi = &ccb->cpi;
2386 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2387 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2388 ccb->ccb_h.target_lun);
2390 /* host specific information */
2391 cpi->version_num = 1;
2392 cpi->hba_inquiry = 0;
2393 cpi->target_sprt = 0;
2394 cpi->hba_misc = PIM_NO_6_BYTE;
2395 cpi->hba_eng_cnt = 0;
2396 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2397 cpi->initiator_id = UMASS_SCSIID_HOST;
2398 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2399 strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2400 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2401 cpi->unit_number = cam_sim_unit(sim);
2402 cpi->bus_id = sc->sc_unit;
2403 #if (__FreeBSD_version >= 700025)
2404 cpi->protocol = PROTO_SCSI;
2405 cpi->protocol_version = SCSI_REV_2;
2406 cpi->transport = XPORT_USB;
2407 cpi->transport_version = 0;
2410 cpi->base_transfer_speed = 0;
2413 if (sc->sc_quirks & FLOPPY_SPEED) {
2414 cpi->base_transfer_speed =
2415 UMASS_FLOPPY_TRANSFER_SPEED;
2417 switch (usbd_get_speed(sc->sc_udev)) {
2418 case USB_SPEED_SUPER:
2419 cpi->base_transfer_speed =
2420 UMASS_SUPER_TRANSFER_SPEED;
2421 cpi->maxio = MAXPHYS;
2423 case USB_SPEED_HIGH:
2424 cpi->base_transfer_speed =
2425 UMASS_HIGH_TRANSFER_SPEED;
2428 cpi->base_transfer_speed =
2429 UMASS_FULL_TRANSFER_SPEED;
2433 cpi->max_lun = sc->sc_maxlun;
2436 cpi->ccb_h.status = CAM_REQ_CMP;
2442 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2443 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2444 ccb->ccb_h.target_lun);
2448 ccb->ccb_h.status = CAM_REQ_CMP;
2452 case XPT_GET_TRAN_SETTINGS:
2454 struct ccb_trans_settings *cts = &ccb->cts;
2456 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2457 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2458 ccb->ccb_h.target_lun);
2460 #if (__FreeBSD_version >= 700025)
2461 cts->protocol = PROTO_SCSI;
2462 cts->protocol_version = SCSI_REV_2;
2463 cts->transport = XPORT_USB;
2464 cts->transport_version = 0;
2465 cts->xport_specific.valid = 0;
2468 cts->flags = 0; /* no disconnection, tagging */
2470 ccb->ccb_h.status = CAM_REQ_CMP;
2474 case XPT_SET_TRAN_SETTINGS:
2476 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2477 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2478 ccb->ccb_h.target_lun);
2480 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2484 case XPT_CALC_GEOMETRY:
2486 cam_calc_geometry(&ccb->ccg, /* extended */ 1);
2492 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
2493 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2494 ccb->ccb_h.target_lun);
2496 ccb->ccb_h.status = CAM_REQ_CMP;
2501 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
2502 "Not implemented\n",
2503 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2504 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2506 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2512 #if (__FreeBSD_version < 700037)
2514 mtx_unlock(&sc->sc_mtx);
2521 umass_cam_poll(struct cam_sim *sim)
2523 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2525 if (sc == UMASS_GONE)
2528 DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
2530 usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
2535 * finalise a completed CAM command
2539 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2542 ccb->csio.resid = residue;
2546 ccb->ccb_h.status = CAM_REQ_CMP;
2547 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
2548 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
2549 (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
2550 struct scsi_read_capacity_data *rcap;
2553 rcap = (void *)(ccb->csio.data_ptr);
2554 maxsector = scsi_4btoul(rcap->addr) - 1;
2555 scsi_ulto4b(maxsector, rcap->addr);
2558 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2559 * of pages supported by the device - otherwise, CAM
2560 * will never ask us for the serial number if the
2561 * device cannot handle that by itself.
2563 if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
2564 sc->sc_transfer.cmd_data[0] == INQUIRY &&
2565 (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2566 sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
2567 (usb_get_serial(sc->sc_udev)[0] != '\0')) {
2568 struct ccb_scsiio *csio;
2569 struct scsi_vpd_supported_page_list *page_list;
2572 page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
2573 if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
2574 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
2575 page_list->length++;
2581 case STATUS_CMD_UNKNOWN:
2582 case STATUS_CMD_FAILED:
2584 /* fetch sense data */
2586 /* the rest of the command was filled in at attach */
2587 sc->cam_scsi_sense.length = ccb->csio.sense_len;
2589 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
2590 "sense data\n", ccb->csio.sense_len);
2592 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
2593 sizeof(sc->cam_scsi_sense))) {
2595 if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
2596 (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
2597 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
2599 umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
2600 ccb->csio.sense_len, ccb->ccb_h.timeout,
2601 &umass_cam_sense_cb, ccb);
2607 * The wire protocol failed and will hopefully have
2608 * recovered. We return an error to CAM and let CAM
2609 * retry the command if necessary. In case of SCSI IO
2610 * commands we ask the CAM layer to check the
2611 * condition first. This is a quick hack to make
2612 * certain devices work.
2614 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
2615 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
2616 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2618 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2626 * Finalise a completed autosense operation
2629 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2637 case STATUS_CMD_UNKNOWN:
2638 case STATUS_CMD_FAILED:
2640 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2641 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2643 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2646 key = (ccb->csio.sense_data.flags & SSD_KEY);
2649 * Getting sense data always succeeds (apart from wire
2652 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2653 (cmd[0] == INQUIRY) &&
2654 (key == SSD_KEY_UNIT_ATTENTION)) {
2656 * Ignore unit attention errors in the case where
2657 * the Unit Attention state is not cleared on
2658 * REQUEST SENSE. They will appear again at the next
2661 ccb->ccb_h.status = CAM_REQ_CMP;
2662 } else if (key == SSD_KEY_NO_SENSE) {
2664 * No problem after all (in the case of CBI without
2667 ccb->ccb_h.status = CAM_REQ_CMP;
2668 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2669 (cmd[0] == READ_CAPACITY) &&
2670 (key == SSD_KEY_UNIT_ATTENTION)) {
2672 * Some devices do not clear the unit attention error
2673 * on request sense. We insert a test unit ready
2674 * command to make sure we clear the unit attention
2675 * condition, then allow the retry to proceed as
2679 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2680 | CAM_AUTOSNS_VALID;
2681 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2686 DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
2687 "TEST_UNIT_READY\n");
2689 /* the rest of the command was filled in at attach */
2691 if (umass_std_transform(sc, ccb,
2692 &sc->cam_scsi_test_unit_ready.opcode,
2693 sizeof(sc->cam_scsi_test_unit_ready))) {
2694 umass_command_start(sc, DIR_NONE, NULL, 0,
2696 &umass_cam_quirk_cb, ccb);
2700 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2701 | CAM_AUTOSNS_VALID;
2702 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2708 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
2709 "status %d\n", status);
2710 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2716 * This completion code just handles the fact that we sent a test-unit-ready
2717 * after having previously failed a READ CAPACITY with CHECK_COND. Even
2718 * though this command succeeded, we have to tell CAM to retry.
2721 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2724 DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
2725 "returned status %d\n", status);
2727 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2728 | CAM_AUTOSNS_VALID;
2729 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2734 * SCSI specific functions
2738 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2741 if ((cmd_len == 0) ||
2742 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2743 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2744 "length: %d bytes\n", cmd_len);
2745 return (0); /* failure */
2747 sc->sc_transfer.cmd_len = cmd_len;
2749 switch (cmd_ptr[0]) {
2750 case TEST_UNIT_READY:
2751 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2752 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2754 bzero(sc->sc_transfer.cmd_data, cmd_len);
2755 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2756 sc->sc_transfer.cmd_data[4] = SSS_START;
2763 * some drives wedge when asked for full inquiry
2766 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2767 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2768 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2774 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2779 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
2781 if ((cmd_len == 0) ||
2782 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2783 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2784 "length: %d bytes\n", cmd_len);
2785 return (0); /* failure */
2787 switch (cmd_ptr[0]) {
2788 /* these commands are defined in RBC: */
2791 case START_STOP_UNIT:
2792 case SYNCHRONIZE_CACHE:
2794 case 0x2f: /* VERIFY_10 is absent from
2797 case MODE_SELECT_10:
2799 case TEST_UNIT_READY:
2802 * The following commands are not listed in my copy of the
2803 * RBC specs. CAM however seems to want those, and at least
2804 * the Sony DSC device appears to support those as well
2809 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2811 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
2812 bzero(sc->sc_transfer.cmd_data + cmd_len, 12 - cmd_len);
2815 sc->sc_transfer.cmd_len = cmd_len;
2816 return (1); /* sucess */
2818 /* All other commands are not legal in RBC */
2820 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
2821 "command 0x%02x\n", cmd_ptr[0]);
2822 return (0); /* failure */
2827 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2830 if ((cmd_len == 0) ||
2831 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2832 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2833 "length: %d bytes\n", cmd_len);
2834 return (0); /* failure */
2836 /* An UFI command is always 12 bytes in length */
2837 sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
2839 /* Zero the command data */
2840 bzero(sc->sc_transfer.cmd_data, UFI_COMMAND_LENGTH);
2842 switch (cmd_ptr[0]) {
2844 * Commands of which the format has been verified. They
2845 * should work. Copy the command into the (zeroed out)
2846 * destination buffer.
2848 case TEST_UNIT_READY:
2849 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2851 * Some devices do not support this command. Start
2852 * Stop Unit should give the same results
2854 DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
2857 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2858 sc->sc_transfer.cmd_data[4] = SSS_START;
2867 case START_STOP_UNIT:
2868 case SEND_DIAGNOSTIC:
2873 case POSITION_TO_ELEMENT: /* SEEK_10 */
2874 case WRITE_AND_VERIFY:
2876 case MODE_SELECT_10:
2880 case READ_FORMAT_CAPACITIES:
2884 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2885 * required for UFI devices, so it is appropriate to fake
2888 case SYNCHRONIZE_CACHE:
2892 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
2893 "command 0x%02x\n", cmd_ptr[0]);
2894 return (0); /* failure */
2897 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2898 return (1); /* success */
2902 * 8070i (ATAPI) specific functions
2905 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2908 if ((cmd_len == 0) ||
2909 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2910 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2911 "length: %d bytes\n", cmd_len);
2912 return (0); /* failure */
2914 /* An ATAPI command is always 12 bytes in length. */
2915 sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
2917 /* Zero the command data */
2918 bzero(sc->sc_transfer.cmd_data, ATAPI_COMMAND_LENGTH);
2920 switch (cmd_ptr[0]) {
2922 * Commands of which the format has been verified. They
2923 * should work. Copy the command into the destination
2928 * some drives wedge when asked for full inquiry
2931 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2932 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2934 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2939 case TEST_UNIT_READY:
2940 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2941 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2943 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2944 sc->sc_transfer.cmd_data[4] = SSS_START;
2951 case START_STOP_UNIT:
2952 case SEND_DIAGNOSTIC:
2957 case POSITION_TO_ELEMENT: /* SEEK_10 */
2958 case SYNCHRONIZE_CACHE:
2959 case MODE_SELECT_10:
2962 case 0x42: /* READ_SUBCHANNEL */
2963 case 0x43: /* READ_TOC */
2964 case 0x44: /* READ_HEADER */
2965 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
2966 case 0x48: /* PLAY_TRACK */
2967 case 0x49: /* PLAY_TRACK_REL */
2968 case 0x4b: /* PAUSE */
2969 case 0x51: /* READ_DISK_INFO */
2970 case 0x52: /* READ_TRACK_INFO */
2971 case 0x54: /* SEND_OPC */
2972 case 0x59: /* READ_MASTER_CUE */
2973 case 0x5b: /* CLOSE_TR_SESSION */
2974 case 0x5c: /* READ_BUFFER_CAP */
2975 case 0x5d: /* SEND_CUE_SHEET */
2976 case 0xa1: /* BLANK */
2977 case 0xa5: /* PLAY_12 */
2978 case 0xa6: /* EXCHANGE_MEDIUM */
2979 case 0xad: /* READ_DVD_STRUCTURE */
2980 case 0xbb: /* SET_CD_SPEED */
2981 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
2987 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
2988 "command 0x%02x - trying anyway\n",
2993 bcopy(cmd_ptr, sc->sc_transfer.cmd_data, cmd_len);
2994 return (1); /* success */
2998 umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3001 return (0); /* failure */
3005 umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3006 uint8_t *cmd, uint8_t cmdlen)
3010 retval = (sc->sc_transform) (sc, cmd, cmdlen);
3013 ccb->ccb_h.status = CAM_REQ_CMP;
3016 } else if (retval == 0) {
3017 ccb->ccb_h.status = CAM_REQ_INVALID;
3021 /* Command should be executed */
3027 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3029 uint8_t *c = cbw->CBWCDB;
3031 uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3032 uint32_t tag = UGETDW(cbw->dCBWTag);
3034 uint8_t clen = cbw->bCDBLength;
3035 uint8_t flags = cbw->bCBWFlags;
3036 uint8_t lun = cbw->bCBWLUN;
3038 DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3039 "(0x%02x%02x%02x%02x%02x%02x%s), "
3040 "data = %db, lun = %d, dir = %s\n",
3042 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3043 dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3044 (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3048 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3050 uint32_t sig = UGETDW(csw->dCSWSignature);
3051 uint32_t tag = UGETDW(csw->dCSWTag);
3052 uint32_t res = UGETDW(csw->dCSWDataResidue);
3053 uint8_t status = csw->bCSWStatus;
3055 DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3056 "res = %d, status = 0x%02x (%s)\n",
3057 tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3059 status, (status == CSWSTATUS_GOOD ? "good" :
3060 (status == CSWSTATUS_FAILED ? "failed" :
3061 (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3065 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3068 uint8_t dir = sc->sc_transfer.dir;
3070 DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3071 "(0x%02x%02x%02x%02x%02x%02x%s), "
3072 "data = %db, dir = %s\n",
3074 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3075 sc->sc_transfer.data_len,
3076 (dir == DIR_IN ? "in" :
3077 (dir == DIR_OUT ? "out" :
3078 (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3082 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3093 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3094 for (i = 0; (i < buflen) && (i < printlen); i++) {
3096 if (j == 0 && i != 0) {
3097 DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3101 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3103 if (buflen > printlen)
3104 sprintf(s3, " ...");
3105 DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",