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/module.h>
114 #include <sys/lock.h>
115 #include <sys/mutex.h>
116 #include <sys/condvar.h>
117 #include <sys/sysctl.h>
119 #include <sys/unistd.h>
120 #include <sys/callout.h>
121 #include <sys/malloc.h>
122 #include <sys/priv.h>
124 #include <dev/usb/usb.h>
125 #include <dev/usb/usbdi.h>
126 #include <dev/usb/usbdi_util.h>
129 #include <dev/usb/quirk/usb_quirk.h>
132 #include <cam/cam_ccb.h>
133 #include <cam/cam_sim.h>
134 #include <cam/cam_xpt_sim.h>
135 #include <cam/scsi/scsi_all.h>
136 #include <cam/scsi/scsi_da.h>
138 #include <cam/cam_periph.h>
140 #define UMASS_EXT_BUFFER
141 #ifdef UMASS_EXT_BUFFER
142 /* this enables loading of virtual buffers into DMA */
143 #define UMASS_USB_FLAGS .ext_buffer=1,
145 #define UMASS_USB_FLAGS
151 if (umass_debug & (m)) { x ; } \
154 #define DPRINTF(sc, m, fmt, ...) \
156 if (umass_debug & (m)) { \
157 printf("%s:%s: " fmt, \
158 (sc) ? (const char *)(sc)->sc_name : \
159 (const char *)"umassX", \
160 __FUNCTION__ ,## __VA_ARGS__); \
164 #define UDMASS_GEN 0x00010000 /* general */
165 #define UDMASS_SCSI 0x00020000 /* scsi */
166 #define UDMASS_UFI 0x00040000 /* ufi command set */
167 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
168 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
169 #define UDMASS_USB 0x00100000 /* USB general */
170 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
171 #define UDMASS_CBI 0x00400000 /* CBI transfers */
172 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
173 #define UDMASS_ALL 0xffff0000 /* all of the above */
174 static int umass_debug = 0;
176 SYSCTL_NODE(_hw_usb, OID_AUTO, umass, CTLFLAG_RW, 0, "USB umass");
177 SYSCTL_INT(_hw_usb_umass, OID_AUTO, debug, CTLFLAG_RW,
178 &umass_debug, 0, "umass debug level");
180 TUNABLE_INT("hw.usb.umass.debug", &umass_debug);
182 #define DIF(...) do { } while (0)
183 #define DPRINTF(...) do { } while (0)
186 #define UMASS_GONE ((struct umass_softc *)1)
188 #define UMASS_BULK_SIZE (1 << 17)
189 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */
190 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */
192 /* USB transfer definitions */
194 #define UMASS_T_BBB_RESET1 0 /* Bulk-Only */
195 #define UMASS_T_BBB_RESET2 1
196 #define UMASS_T_BBB_RESET3 2
197 #define UMASS_T_BBB_COMMAND 3
198 #define UMASS_T_BBB_DATA_READ 4
199 #define UMASS_T_BBB_DATA_RD_CS 5
200 #define UMASS_T_BBB_DATA_WRITE 6
201 #define UMASS_T_BBB_DATA_WR_CS 7
202 #define UMASS_T_BBB_STATUS 8
203 #define UMASS_T_BBB_MAX 9
205 #define UMASS_T_CBI_RESET1 0 /* CBI */
206 #define UMASS_T_CBI_RESET2 1
207 #define UMASS_T_CBI_RESET3 2
208 #define UMASS_T_CBI_COMMAND 3
209 #define UMASS_T_CBI_DATA_READ 4
210 #define UMASS_T_CBI_DATA_RD_CS 5
211 #define UMASS_T_CBI_DATA_WRITE 6
212 #define UMASS_T_CBI_DATA_WR_CS 7
213 #define UMASS_T_CBI_STATUS 8
214 #define UMASS_T_CBI_RESET4 9
215 #define UMASS_T_CBI_MAX 10
217 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
219 /* Generic definitions */
221 /* Direction for transfer */
227 #define DEVNAME "umass"
228 #define DEVNAME_SIM "umass-sim"
230 /* Approximate maximum transfer speeds (assumes 33% overhead). */
231 #define UMASS_FULL_TRANSFER_SPEED 1000
232 #define UMASS_HIGH_TRANSFER_SPEED 40000
233 #define UMASS_SUPER_TRANSFER_SPEED 400000
234 #define UMASS_FLOPPY_TRANSFER_SPEED 20
236 #define UMASS_TIMEOUT 5000 /* ms */
238 /* CAM specific definitions */
240 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
241 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
243 /* Bulk-Only features */
245 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
246 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
248 /* Command Block Wrapper */
250 uDWord dCBWSignature;
251 #define CBWSIGNATURE 0x43425355
253 uDWord dCBWDataTransferLength;
255 #define CBWFLAGS_OUT 0x00
256 #define CBWFLAGS_IN 0x80
259 #define CBWCDBLENGTH 16
260 uByte CBWCDB[CBWCDBLENGTH];
261 } __packed umass_bbb_cbw_t;
263 #define UMASS_BBB_CBW_SIZE 31
265 /* Command Status Wrapper */
267 uDWord dCSWSignature;
268 #define CSWSIGNATURE 0x53425355
269 #define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
270 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
272 uDWord dCSWDataResidue;
274 #define CSWSTATUS_GOOD 0x0
275 #define CSWSTATUS_FAILED 0x1
276 #define CSWSTATUS_PHASE 0x2
277 } __packed umass_bbb_csw_t;
279 #define UMASS_BBB_CSW_SIZE 13
283 #define UR_CBI_ADSC 0x00
288 #define IDB_TYPE_CCI 0x00
290 #define IDB_VALUE_PASS 0x00
291 #define IDB_VALUE_FAIL 0x01
292 #define IDB_VALUE_PHASE 0x02
293 #define IDB_VALUE_PERSISTENT 0x03
294 #define IDB_VALUE_STATUS_MASK 0x03
301 } __packed umass_cbi_sbl_t;
303 struct umass_softc; /* see below */
305 typedef void (umass_callback_t)(struct umass_softc *sc, union ccb *ccb,
306 uint32_t residue, uint8_t status);
308 #define STATUS_CMD_OK 0 /* everything ok */
309 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
310 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
311 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
313 typedef uint8_t (umass_transform_t)(struct umass_softc *sc, uint8_t *cmd_ptr,
316 /* Wire and command protocol */
317 #define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
318 #define UMASS_PROTO_CBI 0x0002
319 #define UMASS_PROTO_CBI_I 0x0004
320 #define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
321 #define UMASS_PROTO_SCSI 0x0100 /* command protocol */
322 #define UMASS_PROTO_ATAPI 0x0200
323 #define UMASS_PROTO_UFI 0x0400
324 #define UMASS_PROTO_RBC 0x0800
325 #define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
327 /* Device specific quirks */
328 #define NO_QUIRKS 0x0000
330 * The drive does not support Test Unit Ready. Convert to Start Unit
332 #define NO_TEST_UNIT_READY 0x0001
334 * The drive does not reset the Unit Attention state after REQUEST
335 * SENSE has been sent. The INQUIRY command does not reset the UA
336 * either, and so CAM runs in circles trying to retrieve the initial
339 #define RS_NO_CLEAR_UA 0x0002
340 /* The drive does not support START STOP. */
341 #define NO_START_STOP 0x0004
342 /* Don't ask for full inquiry data (255b). */
343 #define FORCE_SHORT_INQUIRY 0x0008
344 /* Needs to be initialised the Shuttle way */
345 #define SHUTTLE_INIT 0x0010
346 /* Drive needs to be switched to alternate iface 1 */
347 #define ALT_IFACE_1 0x0020
348 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
349 #define FLOPPY_SPEED 0x0040
350 /* The device can't count and gets the residue of transfers wrong */
351 #define IGNORE_RESIDUE 0x0080
352 /* No GetMaxLun call */
353 #define NO_GETMAXLUN 0x0100
354 /* The device uses a weird CSWSIGNATURE. */
355 #define WRONG_CSWSIG 0x0200
356 /* Device cannot handle INQUIRY so fake a generic response */
357 #define NO_INQUIRY 0x0400
358 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
359 #define NO_INQUIRY_EVPD 0x0800
360 /* Pad all RBC requests to 12 bytes. */
361 #define RBC_PAD_TO_12 0x1000
363 * Device reports number of sectors from READ_CAPACITY, not max
366 #define READ_CAPACITY_OFFBY1 0x2000
368 * Device cannot handle a SCSI synchronize cache command. Normally
369 * this quirk would be handled in the cam layer, but for IDE bridges
370 * we need to associate the quirk with the bridge and not the
371 * underlying disk device. This is handled by faking a success
374 #define NO_SYNCHRONIZE_CACHE 0x4000
378 struct scsi_sense cam_scsi_sense;
379 struct scsi_test_unit_ready cam_scsi_test_unit_ready;
384 umass_callback_t *callback;
386 uint32_t data_len; /* bytes */
387 uint32_t data_rem; /* bytes */
388 uint32_t data_timeout; /* ms */
389 uint32_t actlen; /* bytes */
391 uint8_t cmd_data[UMASS_MAX_CMDLEN];
392 uint8_t cmd_len; /* bytes */
397 /* Bulk specific variables for transfers in progress */
398 umass_bbb_cbw_t cbw; /* command block wrapper */
399 umass_bbb_csw_t csw; /* command status wrapper */
401 /* CBI specific variables for transfers in progress */
402 umass_cbi_sbl_t sbl; /* status block */
405 struct usb_device *sc_udev;
406 struct cam_sim *sc_sim; /* SCSI Interface Module */
407 struct usb_xfer *sc_xfer[UMASS_T_MAX];
410 * The command transform function is used to convert the SCSI
411 * commands into their derivatives, like UFI, ATAPI, and friends.
413 umass_transform_t *sc_transform;
416 uint32_t sc_quirks; /* they got it almost right */
417 uint32_t sc_proto; /* wire and cmd protocol */
420 uint8_t sc_iface_no; /* interface number */
421 uint8_t sc_maxlun; /* maximum LUN number, inclusive */
422 uint8_t sc_last_xfer_index;
423 uint8_t sc_status_try;
426 struct umass_probe_proto {
435 static device_probe_t umass_probe;
436 static device_attach_t umass_attach;
437 static device_detach_t umass_detach;
439 static usb_callback_t umass_tr_error;
440 static usb_callback_t umass_t_bbb_reset1_callback;
441 static usb_callback_t umass_t_bbb_reset2_callback;
442 static usb_callback_t umass_t_bbb_reset3_callback;
443 static usb_callback_t umass_t_bbb_command_callback;
444 static usb_callback_t umass_t_bbb_data_read_callback;
445 static usb_callback_t umass_t_bbb_data_rd_cs_callback;
446 static usb_callback_t umass_t_bbb_data_write_callback;
447 static usb_callback_t umass_t_bbb_data_wr_cs_callback;
448 static usb_callback_t umass_t_bbb_status_callback;
449 static usb_callback_t umass_t_cbi_reset1_callback;
450 static usb_callback_t umass_t_cbi_reset2_callback;
451 static usb_callback_t umass_t_cbi_reset3_callback;
452 static usb_callback_t umass_t_cbi_reset4_callback;
453 static usb_callback_t umass_t_cbi_command_callback;
454 static usb_callback_t umass_t_cbi_data_read_callback;
455 static usb_callback_t umass_t_cbi_data_rd_cs_callback;
456 static usb_callback_t umass_t_cbi_data_write_callback;
457 static usb_callback_t umass_t_cbi_data_wr_cs_callback;
458 static usb_callback_t umass_t_cbi_status_callback;
460 static void umass_cancel_ccb(struct umass_softc *);
461 static void umass_init_shuttle(struct umass_softc *);
462 static void umass_reset(struct umass_softc *);
463 static void umass_t_bbb_data_clear_stall_callback(struct usb_xfer *,
464 uint8_t, uint8_t, usb_error_t);
465 static void umass_command_start(struct umass_softc *, uint8_t, void *,
466 uint32_t, uint32_t, umass_callback_t *, union ccb *);
467 static uint8_t umass_bbb_get_max_lun(struct umass_softc *);
468 static void umass_cbi_start_status(struct umass_softc *);
469 static void umass_t_cbi_data_clear_stall_callback(struct usb_xfer *,
470 uint8_t, uint8_t, usb_error_t);
471 static int umass_cam_attach_sim(struct umass_softc *);
472 static void umass_cam_attach(struct umass_softc *);
473 static void umass_cam_detach_sim(struct umass_softc *);
474 static void umass_cam_action(struct cam_sim *, union ccb *);
475 static void umass_cam_poll(struct cam_sim *);
476 static void umass_cam_cb(struct umass_softc *, union ccb *, uint32_t,
478 static void umass_cam_sense_cb(struct umass_softc *, union ccb *, uint32_t,
480 static void umass_cam_quirk_cb(struct umass_softc *, union ccb *, uint32_t,
482 static uint8_t umass_scsi_transform(struct umass_softc *, uint8_t *, uint8_t);
483 static uint8_t umass_rbc_transform(struct umass_softc *, uint8_t *, uint8_t);
484 static uint8_t umass_ufi_transform(struct umass_softc *, uint8_t *, uint8_t);
485 static uint8_t umass_atapi_transform(struct umass_softc *, uint8_t *,
487 static uint8_t umass_no_transform(struct umass_softc *, uint8_t *, uint8_t);
488 static uint8_t umass_std_transform(struct umass_softc *, union ccb *, uint8_t
492 static void umass_bbb_dump_cbw(struct umass_softc *, umass_bbb_cbw_t *);
493 static void umass_bbb_dump_csw(struct umass_softc *, umass_bbb_csw_t *);
494 static void umass_cbi_dump_cmd(struct umass_softc *, void *, uint8_t);
495 static void umass_dump_buffer(struct umass_softc *, uint8_t *, uint32_t,
499 static struct usb_config umass_bbb_config[UMASS_T_BBB_MAX] = {
501 [UMASS_T_BBB_RESET1] = {
503 .endpoint = 0x00, /* Control pipe */
504 .direction = UE_DIR_ANY,
505 .bufsize = sizeof(struct usb_device_request),
506 .callback = &umass_t_bbb_reset1_callback,
507 .timeout = 5000, /* 5 seconds */
508 .interval = 500, /* 500 milliseconds */
511 [UMASS_T_BBB_RESET2] = {
513 .endpoint = 0x00, /* Control pipe */
514 .direction = UE_DIR_ANY,
515 .bufsize = sizeof(struct usb_device_request),
516 .callback = &umass_t_bbb_reset2_callback,
517 .timeout = 5000, /* 5 seconds */
518 .interval = 50, /* 50 milliseconds */
521 [UMASS_T_BBB_RESET3] = {
523 .endpoint = 0x00, /* Control pipe */
524 .direction = UE_DIR_ANY,
525 .bufsize = sizeof(struct usb_device_request),
526 .callback = &umass_t_bbb_reset3_callback,
527 .timeout = 5000, /* 5 seconds */
528 .interval = 50, /* 50 milliseconds */
531 [UMASS_T_BBB_COMMAND] = {
533 .endpoint = UE_ADDR_ANY,
534 .direction = UE_DIR_OUT,
535 .bufsize = sizeof(umass_bbb_cbw_t),
536 .callback = &umass_t_bbb_command_callback,
537 .timeout = 5000, /* 5 seconds */
540 [UMASS_T_BBB_DATA_READ] = {
542 .endpoint = UE_ADDR_ANY,
543 .direction = UE_DIR_IN,
544 .bufsize = UMASS_BULK_SIZE,
545 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
546 .callback = &umass_t_bbb_data_read_callback,
547 .timeout = 0, /* overwritten later */
550 [UMASS_T_BBB_DATA_RD_CS] = {
552 .endpoint = 0x00, /* Control pipe */
553 .direction = UE_DIR_ANY,
554 .bufsize = sizeof(struct usb_device_request),
555 .callback = &umass_t_bbb_data_rd_cs_callback,
556 .timeout = 5000, /* 5 seconds */
559 [UMASS_T_BBB_DATA_WRITE] = {
561 .endpoint = UE_ADDR_ANY,
562 .direction = UE_DIR_OUT,
563 .bufsize = UMASS_BULK_SIZE,
564 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
565 .callback = &umass_t_bbb_data_write_callback,
566 .timeout = 0, /* overwritten later */
569 [UMASS_T_BBB_DATA_WR_CS] = {
571 .endpoint = 0x00, /* Control pipe */
572 .direction = UE_DIR_ANY,
573 .bufsize = sizeof(struct usb_device_request),
574 .callback = &umass_t_bbb_data_wr_cs_callback,
575 .timeout = 5000, /* 5 seconds */
578 [UMASS_T_BBB_STATUS] = {
580 .endpoint = UE_ADDR_ANY,
581 .direction = UE_DIR_IN,
582 .bufsize = sizeof(umass_bbb_csw_t),
583 .flags = {.short_xfer_ok = 1,},
584 .callback = &umass_t_bbb_status_callback,
585 .timeout = 5000, /* ms */
589 static struct usb_config umass_cbi_config[UMASS_T_CBI_MAX] = {
591 [UMASS_T_CBI_RESET1] = {
593 .endpoint = 0x00, /* Control pipe */
594 .direction = UE_DIR_ANY,
595 .bufsize = (sizeof(struct usb_device_request) +
596 UMASS_CBI_DIAGNOSTIC_CMDLEN),
597 .callback = &umass_t_cbi_reset1_callback,
598 .timeout = 5000, /* 5 seconds */
599 .interval = 500, /* 500 milliseconds */
602 [UMASS_T_CBI_RESET2] = {
604 .endpoint = 0x00, /* Control pipe */
605 .direction = UE_DIR_ANY,
606 .bufsize = sizeof(struct usb_device_request),
607 .callback = &umass_t_cbi_reset2_callback,
608 .timeout = 5000, /* 5 seconds */
609 .interval = 50, /* 50 milliseconds */
612 [UMASS_T_CBI_RESET3] = {
614 .endpoint = 0x00, /* Control pipe */
615 .direction = UE_DIR_ANY,
616 .bufsize = sizeof(struct usb_device_request),
617 .callback = &umass_t_cbi_reset3_callback,
618 .timeout = 5000, /* 5 seconds */
619 .interval = 50, /* 50 milliseconds */
622 [UMASS_T_CBI_COMMAND] = {
624 .endpoint = 0x00, /* Control pipe */
625 .direction = UE_DIR_ANY,
626 .bufsize = (sizeof(struct usb_device_request) +
628 .callback = &umass_t_cbi_command_callback,
629 .timeout = 5000, /* 5 seconds */
632 [UMASS_T_CBI_DATA_READ] = {
634 .endpoint = UE_ADDR_ANY,
635 .direction = UE_DIR_IN,
636 .bufsize = UMASS_BULK_SIZE,
637 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
638 .callback = &umass_t_cbi_data_read_callback,
639 .timeout = 0, /* overwritten later */
642 [UMASS_T_CBI_DATA_RD_CS] = {
644 .endpoint = 0x00, /* Control pipe */
645 .direction = UE_DIR_ANY,
646 .bufsize = sizeof(struct usb_device_request),
647 .callback = &umass_t_cbi_data_rd_cs_callback,
648 .timeout = 5000, /* 5 seconds */
651 [UMASS_T_CBI_DATA_WRITE] = {
653 .endpoint = UE_ADDR_ANY,
654 .direction = UE_DIR_OUT,
655 .bufsize = UMASS_BULK_SIZE,
656 .flags = {.proxy_buffer = 1,.short_xfer_ok = 1, UMASS_USB_FLAGS},
657 .callback = &umass_t_cbi_data_write_callback,
658 .timeout = 0, /* overwritten later */
661 [UMASS_T_CBI_DATA_WR_CS] = {
663 .endpoint = 0x00, /* Control pipe */
664 .direction = UE_DIR_ANY,
665 .bufsize = sizeof(struct usb_device_request),
666 .callback = &umass_t_cbi_data_wr_cs_callback,
667 .timeout = 5000, /* 5 seconds */
670 [UMASS_T_CBI_STATUS] = {
671 .type = UE_INTERRUPT,
672 .endpoint = UE_ADDR_ANY,
673 .direction = UE_DIR_IN,
674 .flags = {.short_xfer_ok = 1,.no_pipe_ok = 1,},
675 .bufsize = sizeof(umass_cbi_sbl_t),
676 .callback = &umass_t_cbi_status_callback,
677 .timeout = 5000, /* ms */
680 [UMASS_T_CBI_RESET4] = {
682 .endpoint = 0x00, /* Control pipe */
683 .direction = UE_DIR_ANY,
684 .bufsize = sizeof(struct usb_device_request),
685 .callback = &umass_t_cbi_reset4_callback,
686 .timeout = 5000, /* ms */
690 /* If device cannot return valid inquiry data, fake it */
691 static const uint8_t fake_inq_data[SHORT_INQUIRY_LENGTH] = {
692 0, /* removable */ 0x80, SCSI_REV_2, SCSI_REV_2,
693 /* additional_length */ 31, 0, 0, 0
696 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
697 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
699 static devclass_t umass_devclass;
701 static device_method_t umass_methods[] = {
702 /* Device interface */
703 DEVMETHOD(device_probe, umass_probe),
704 DEVMETHOD(device_attach, umass_attach),
705 DEVMETHOD(device_detach, umass_detach),
709 static driver_t umass_driver = {
711 .methods = umass_methods,
712 .size = sizeof(struct umass_softc),
715 DRIVER_MODULE(umass, uhub, umass_driver, umass_devclass, NULL, 0);
716 MODULE_DEPEND(umass, usb, 1, 1, 1);
717 MODULE_DEPEND(umass, cam, 1, 1, 1);
718 MODULE_VERSION(umass, 1);
721 * USB device probe/attach/detach
724 static const STRUCT_USB_HOST_ID __used umass_devs[] = {
725 /* generic mass storage class */
726 {USB_IFACE_CLASS(UICLASS_MASS),},
730 umass_get_proto(struct usb_interface *iface)
732 struct usb_interface_descriptor *id;
737 /* Check for a standards compliant device */
738 id = usbd_get_interface_descriptor(iface);
740 (id->bInterfaceClass != UICLASS_MASS)) {
743 switch (id->bInterfaceSubClass) {
744 case UISUBCLASS_SCSI:
745 retval |= UMASS_PROTO_SCSI;
748 retval |= UMASS_PROTO_UFI;
751 retval |= UMASS_PROTO_RBC;
753 case UISUBCLASS_SFF8020I:
754 case UISUBCLASS_SFF8070I:
755 retval |= UMASS_PROTO_ATAPI;
761 switch (id->bInterfaceProtocol) {
762 case UIPROTO_MASS_CBI:
763 retval |= UMASS_PROTO_CBI;
765 case UIPROTO_MASS_CBI_I:
766 retval |= UMASS_PROTO_CBI_I;
768 case UIPROTO_MASS_BBB_OLD:
769 case UIPROTO_MASS_BBB:
770 retval |= UMASS_PROTO_BBB;
780 * Match the device we are seeing with the devices supported.
782 static struct umass_probe_proto
783 umass_probe_proto(device_t dev, struct usb_attach_arg *uaa)
785 struct umass_probe_proto ret;
786 uint32_t quirks = NO_QUIRKS;
787 uint32_t proto = umass_get_proto(uaa->iface);
789 memset(&ret, 0, sizeof(ret));
790 ret.error = BUS_PROBE_GENERIC;
792 /* Search for protocol enforcement */
794 if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_BBB)) {
795 proto &= ~UMASS_PROTO_WIRE;
796 proto |= UMASS_PROTO_BBB;
797 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI)) {
798 proto &= ~UMASS_PROTO_WIRE;
799 proto |= UMASS_PROTO_CBI;
800 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_WIRE_CBI_I)) {
801 proto &= ~UMASS_PROTO_WIRE;
802 proto |= UMASS_PROTO_CBI_I;
805 if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_SCSI)) {
806 proto &= ~UMASS_PROTO_COMMAND;
807 proto |= UMASS_PROTO_SCSI;
808 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_ATAPI)) {
809 proto &= ~UMASS_PROTO_COMMAND;
810 proto |= UMASS_PROTO_ATAPI;
811 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_UFI)) {
812 proto &= ~UMASS_PROTO_COMMAND;
813 proto |= UMASS_PROTO_UFI;
814 } else if (usb_test_quirk(uaa, UQ_MSC_FORCE_PROTO_RBC)) {
815 proto &= ~UMASS_PROTO_COMMAND;
816 proto |= UMASS_PROTO_RBC;
819 /* Check if the protocol is invalid */
821 if ((proto & UMASS_PROTO_COMMAND) == 0) {
826 if ((proto & UMASS_PROTO_WIRE) == 0) {
831 /* Search for quirks */
833 if (usb_test_quirk(uaa, UQ_MSC_NO_TEST_UNIT_READY))
834 quirks |= NO_TEST_UNIT_READY;
835 if (usb_test_quirk(uaa, UQ_MSC_NO_RS_CLEAR_UA))
836 quirks |= RS_NO_CLEAR_UA;
837 if (usb_test_quirk(uaa, UQ_MSC_NO_START_STOP))
838 quirks |= NO_START_STOP;
839 if (usb_test_quirk(uaa, UQ_MSC_NO_GETMAXLUN))
840 quirks |= NO_GETMAXLUN;
841 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY))
842 quirks |= NO_INQUIRY;
843 if (usb_test_quirk(uaa, UQ_MSC_NO_INQUIRY_EVPD))
844 quirks |= NO_INQUIRY_EVPD;
845 if (usb_test_quirk(uaa, UQ_MSC_NO_SYNC_CACHE))
846 quirks |= NO_SYNCHRONIZE_CACHE;
847 if (usb_test_quirk(uaa, UQ_MSC_SHUTTLE_INIT))
848 quirks |= SHUTTLE_INIT;
849 if (usb_test_quirk(uaa, UQ_MSC_ALT_IFACE_1))
850 quirks |= ALT_IFACE_1;
851 if (usb_test_quirk(uaa, UQ_MSC_FLOPPY_SPEED))
852 quirks |= FLOPPY_SPEED;
853 if (usb_test_quirk(uaa, UQ_MSC_IGNORE_RESIDUE))
854 quirks |= IGNORE_RESIDUE;
855 if (usb_test_quirk(uaa, UQ_MSC_WRONG_CSWSIG))
856 quirks |= WRONG_CSWSIG;
857 if (usb_test_quirk(uaa, UQ_MSC_RBC_PAD_TO_12))
858 quirks |= RBC_PAD_TO_12;
859 if (usb_test_quirk(uaa, UQ_MSC_READ_CAP_OFFBY1))
860 quirks |= READ_CAPACITY_OFFBY1;
861 if (usb_test_quirk(uaa, UQ_MSC_FORCE_SHORT_INQ))
862 quirks |= FORCE_SHORT_INQUIRY;
871 umass_probe(device_t dev)
873 struct usb_attach_arg *uaa = device_get_ivars(dev);
874 struct umass_probe_proto temp;
876 if (uaa->usb_mode != USB_MODE_HOST) {
879 temp = umass_probe_proto(dev, uaa);
885 umass_attach(device_t dev)
887 struct umass_softc *sc = device_get_softc(dev);
888 struct usb_attach_arg *uaa = device_get_ivars(dev);
889 struct umass_probe_proto temp = umass_probe_proto(dev, uaa);
890 struct usb_interface_descriptor *id;
894 * NOTE: the softc struct is cleared in device_set_driver.
895 * We can safely call umass_detach without specifically
896 * initializing the struct.
900 sc->sc_udev = uaa->device;
901 sc->sc_proto = temp.proto;
902 sc->sc_quirks = temp.quirks;
903 sc->sc_unit = device_get_unit(dev);
905 snprintf(sc->sc_name, sizeof(sc->sc_name),
906 "%s", device_get_nameunit(dev));
908 device_set_usb_desc(dev);
910 mtx_init(&sc->sc_mtx, device_get_nameunit(dev),
911 NULL, MTX_DEF | MTX_RECURSE);
913 /* get interface index */
915 id = usbd_get_interface_descriptor(uaa->iface);
917 device_printf(dev, "failed to get "
918 "interface number\n");
921 sc->sc_iface_no = id->bInterfaceNumber;
924 device_printf(dev, " ");
926 switch (sc->sc_proto & UMASS_PROTO_COMMAND) {
927 case UMASS_PROTO_SCSI:
930 case UMASS_PROTO_ATAPI:
931 printf("8070i (ATAPI)");
933 case UMASS_PROTO_UFI:
936 case UMASS_PROTO_RBC:
940 printf("(unknown 0x%02x)",
941 sc->sc_proto & UMASS_PROTO_COMMAND);
947 switch (sc->sc_proto & UMASS_PROTO_WIRE) {
948 case UMASS_PROTO_BBB:
951 case UMASS_PROTO_CBI: /* uses Comand/Bulk pipes */
954 case UMASS_PROTO_CBI_I: /* uses Comand/Bulk/Interrupt pipes */
955 printf("CBI with CCI");
958 printf("(unknown 0x%02x)",
959 sc->sc_proto & UMASS_PROTO_WIRE);
962 printf("; quirks = 0x%04x\n", sc->sc_quirks);
965 if (sc->sc_quirks & ALT_IFACE_1) {
966 err = usbd_set_alt_interface_index
967 (uaa->device, uaa->info.bIfaceIndex, 1);
970 DPRINTF(sc, UDMASS_USB, "could not switch to "
971 "Alt Interface 1\n");
975 /* allocate all required USB transfers */
977 if (sc->sc_proto & UMASS_PROTO_BBB) {
979 err = usbd_transfer_setup(uaa->device,
980 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_bbb_config,
981 UMASS_T_BBB_MAX, sc, &sc->sc_mtx);
983 /* skip reset first time */
984 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
986 } else if (sc->sc_proto & (UMASS_PROTO_CBI | UMASS_PROTO_CBI_I)) {
988 err = usbd_transfer_setup(uaa->device,
989 &uaa->info.bIfaceIndex, sc->sc_xfer, umass_cbi_config,
990 UMASS_T_CBI_MAX, sc, &sc->sc_mtx);
992 /* skip reset first time */
993 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1000 device_printf(dev, "could not setup required "
1001 "transfers, %s\n", usbd_errstr(err));
1005 (sc->sc_proto & UMASS_PROTO_SCSI) ? &umass_scsi_transform :
1006 (sc->sc_proto & UMASS_PROTO_UFI) ? &umass_ufi_transform :
1007 (sc->sc_proto & UMASS_PROTO_ATAPI) ? &umass_atapi_transform :
1008 (sc->sc_proto & UMASS_PROTO_RBC) ? &umass_rbc_transform :
1009 &umass_no_transform;
1011 /* from here onwards the device can be used. */
1013 if (sc->sc_quirks & SHUTTLE_INIT) {
1014 umass_init_shuttle(sc);
1016 /* get the maximum LUN supported by the device */
1018 if (((sc->sc_proto & UMASS_PROTO_WIRE) == UMASS_PROTO_BBB) &&
1019 !(sc->sc_quirks & NO_GETMAXLUN))
1020 sc->sc_maxlun = umass_bbb_get_max_lun(sc);
1024 /* Prepare the SCSI command block */
1025 sc->cam_scsi_sense.opcode = REQUEST_SENSE;
1026 sc->cam_scsi_test_unit_ready.opcode = TEST_UNIT_READY;
1028 /* register the SIM */
1029 err = umass_cam_attach_sim(sc);
1034 umass_cam_attach(sc);
1036 DPRINTF(sc, UDMASS_GEN, "Attach finished\n");
1038 return (0); /* success */
1042 return (ENXIO); /* failure */
1046 umass_detach(device_t dev)
1048 struct umass_softc *sc = device_get_softc(dev);
1050 DPRINTF(sc, UDMASS_USB, "\n");
1052 /* teardown our statemachine */
1054 usbd_transfer_unsetup(sc->sc_xfer, UMASS_T_MAX);
1056 #if (__FreeBSD_version >= 700037)
1057 mtx_lock(&sc->sc_mtx);
1059 umass_cam_detach_sim(sc);
1061 #if (__FreeBSD_version >= 700037)
1062 mtx_unlock(&sc->sc_mtx);
1064 mtx_destroy(&sc->sc_mtx);
1066 return (0); /* success */
1070 umass_init_shuttle(struct umass_softc *sc)
1072 struct usb_device_request req;
1074 uint8_t status[2] = {0, 0};
1077 * The Linux driver does this, but no one can tell us what the
1080 req.bmRequestType = UT_READ_VENDOR_DEVICE;
1081 req.bRequest = 1; /* XXX unknown command */
1082 USETW(req.wValue, 0);
1083 req.wIndex[0] = sc->sc_iface_no;
1085 USETW(req.wLength, sizeof(status));
1086 err = usbd_do_request(sc->sc_udev, NULL, &req, &status);
1088 DPRINTF(sc, UDMASS_GEN, "Shuttle init returned 0x%02x%02x\n",
1089 status[0], status[1]);
1093 * Generic functions to handle transfers
1097 umass_transfer_start(struct umass_softc *sc, uint8_t xfer_index)
1099 DPRINTF(sc, UDMASS_GEN, "transfer index = "
1100 "%d\n", xfer_index);
1102 if (sc->sc_xfer[xfer_index]) {
1103 sc->sc_last_xfer_index = xfer_index;
1104 usbd_transfer_start(sc->sc_xfer[xfer_index]);
1106 umass_cancel_ccb(sc);
1111 umass_reset(struct umass_softc *sc)
1113 DPRINTF(sc, UDMASS_GEN, "resetting device\n");
1116 * stop the last transfer, if not already stopped:
1118 usbd_transfer_stop(sc->sc_xfer[sc->sc_last_xfer_index]);
1119 umass_transfer_start(sc, 0);
1123 umass_cancel_ccb(struct umass_softc *sc)
1127 mtx_assert(&sc->sc_mtx, MA_OWNED);
1129 ccb = sc->sc_transfer.ccb;
1130 sc->sc_transfer.ccb = NULL;
1131 sc->sc_last_xfer_index = 0;
1134 (sc->sc_transfer.callback)
1135 (sc, ccb, (sc->sc_transfer.data_len -
1136 sc->sc_transfer.actlen), STATUS_WIRE_FAILED);
1141 umass_tr_error(struct usb_xfer *xfer, usb_error_t error)
1143 struct umass_softc *sc = usbd_xfer_softc(xfer);
1145 if (error != USB_ERR_CANCELLED) {
1147 DPRINTF(sc, UDMASS_GEN, "transfer error, %s -> "
1148 "reset\n", usbd_errstr(error));
1150 umass_cancel_ccb(sc);
1154 * BBB protocol specific functions
1158 umass_t_bbb_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1160 struct umass_softc *sc = usbd_xfer_softc(xfer);
1161 struct usb_device_request req;
1162 struct usb_page_cache *pc;
1164 switch (USB_GET_STATE(xfer)) {
1165 case USB_ST_TRANSFERRED:
1166 umass_transfer_start(sc, UMASS_T_BBB_RESET2);
1171 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1173 * For Reset Recovery the host shall issue in the following order:
1174 * a) a Bulk-Only Mass Storage Reset
1175 * b) a Clear Feature HALT to the Bulk-In endpoint
1176 * c) a Clear Feature HALT to the Bulk-Out endpoint
1178 * This is done in 3 steps, using 3 transfers:
1179 * UMASS_T_BBB_RESET1
1180 * UMASS_T_BBB_RESET2
1181 * UMASS_T_BBB_RESET3
1184 DPRINTF(sc, UDMASS_BBB, "BBB reset!\n");
1186 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1187 req.bRequest = UR_BBB_RESET; /* bulk only reset */
1188 USETW(req.wValue, 0);
1189 req.wIndex[0] = sc->sc_iface_no;
1191 USETW(req.wLength, 0);
1193 pc = usbd_xfer_get_frame(xfer, 0);
1194 usbd_copy_in(pc, 0, &req, sizeof(req));
1196 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1197 usbd_xfer_set_frames(xfer, 1);
1198 usbd_transfer_submit(xfer);
1201 default: /* Error */
1202 umass_tr_error(xfer, error);
1209 umass_t_bbb_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1211 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_RESET3,
1212 UMASS_T_BBB_DATA_READ, error);
1216 umass_t_bbb_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1218 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_COMMAND,
1219 UMASS_T_BBB_DATA_WRITE, error);
1223 umass_t_bbb_data_clear_stall_callback(struct usb_xfer *xfer,
1224 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1226 struct umass_softc *sc = usbd_xfer_softc(xfer);
1228 switch (USB_GET_STATE(xfer)) {
1229 case USB_ST_TRANSFERRED:
1231 umass_transfer_start(sc, next_xfer);
1235 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1236 goto tr_transferred;
1240 default: /* Error */
1241 umass_tr_error(xfer, error);
1248 umass_t_bbb_command_callback(struct usb_xfer *xfer, usb_error_t error)
1250 struct umass_softc *sc = usbd_xfer_softc(xfer);
1251 union ccb *ccb = sc->sc_transfer.ccb;
1252 struct usb_page_cache *pc;
1255 switch (USB_GET_STATE(xfer)) {
1256 case USB_ST_TRANSFERRED:
1257 umass_transfer_start
1258 (sc, ((sc->sc_transfer.dir == DIR_IN) ? UMASS_T_BBB_DATA_READ :
1259 (sc->sc_transfer.dir == DIR_OUT) ? UMASS_T_BBB_DATA_WRITE :
1260 UMASS_T_BBB_STATUS));
1265 sc->sc_status_try = 0;
1270 * the initial value is not important,
1271 * as long as the values are unique:
1273 tag = UGETDW(sc->cbw.dCBWTag) + 1;
1275 USETDW(sc->cbw.dCBWSignature, CBWSIGNATURE);
1276 USETDW(sc->cbw.dCBWTag, tag);
1279 * dCBWDataTransferLength:
1280 * This field indicates the number of bytes of data that the host
1281 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1282 * the Direction bit) during the execution of this command. If this
1283 * field is set to 0, the device will expect that no data will be
1284 * transferred IN or OUT during this command, regardless of the value
1285 * of the Direction bit defined in dCBWFlags.
1287 USETDW(sc->cbw.dCBWDataTransferLength, sc->sc_transfer.data_len);
1291 * The bits of the Flags field are defined as follows:
1293 * Bit 7 Direction - this bit shall be ignored if the
1294 * dCBWDataTransferLength field is zero.
1295 * 0 = data Out from host to device
1296 * 1 = data In from device to host
1298 sc->cbw.bCBWFlags = ((sc->sc_transfer.dir == DIR_IN) ?
1299 CBWFLAGS_IN : CBWFLAGS_OUT);
1300 sc->cbw.bCBWLUN = sc->sc_transfer.lun;
1302 if (sc->sc_transfer.cmd_len > sizeof(sc->cbw.CBWCDB)) {
1303 sc->sc_transfer.cmd_len = sizeof(sc->cbw.CBWCDB);
1304 DPRINTF(sc, UDMASS_BBB, "Truncating long command!\n");
1306 sc->cbw.bCDBLength = sc->sc_transfer.cmd_len;
1308 memcpy(sc->cbw.CBWCDB, sc->sc_transfer.cmd_data,
1309 sc->sc_transfer.cmd_len);
1311 memset(sc->sc_transfer.cmd_data +
1312 sc->sc_transfer.cmd_len, 0,
1313 sizeof(sc->cbw.CBWCDB) -
1314 sc->sc_transfer.cmd_len);
1316 DIF(UDMASS_BBB, umass_bbb_dump_cbw(sc, &sc->cbw));
1318 pc = usbd_xfer_get_frame(xfer, 0);
1319 usbd_copy_in(pc, 0, &sc->cbw, sizeof(sc->cbw));
1320 usbd_xfer_set_frame_len(xfer, 0, sizeof(sc->cbw));
1322 usbd_transfer_submit(xfer);
1326 default: /* Error */
1327 umass_tr_error(xfer, error);
1334 umass_t_bbb_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1336 struct umass_softc *sc = usbd_xfer_softc(xfer);
1337 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1338 #ifndef UMASS_EXT_BUFFER
1339 struct usb_page_cache *pc;
1343 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1345 switch (USB_GET_STATE(xfer)) {
1346 case USB_ST_TRANSFERRED:
1347 #ifndef UMASS_EXT_BUFFER
1348 pc = usbd_xfer_get_frame(xfer, 0);
1349 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1351 sc->sc_transfer.data_rem -= actlen;
1352 sc->sc_transfer.data_ptr += actlen;
1353 sc->sc_transfer.actlen += actlen;
1355 if (actlen < sumlen) {
1356 /* short transfer */
1357 sc->sc_transfer.data_rem = 0;
1360 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1361 max_bulk, sc->sc_transfer.data_rem);
1363 if (sc->sc_transfer.data_rem == 0) {
1364 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1367 if (max_bulk > sc->sc_transfer.data_rem) {
1368 max_bulk = sc->sc_transfer.data_rem;
1370 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1372 #ifdef UMASS_EXT_BUFFER
1373 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1376 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1378 usbd_transfer_submit(xfer);
1381 default: /* Error */
1382 if (error == USB_ERR_CANCELLED) {
1383 umass_tr_error(xfer, error);
1385 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1393 umass_t_bbb_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1395 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1396 UMASS_T_BBB_DATA_READ, error);
1400 umass_t_bbb_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1402 struct umass_softc *sc = usbd_xfer_softc(xfer);
1403 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1404 #ifndef UMASS_EXT_BUFFER
1405 struct usb_page_cache *pc;
1409 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1411 switch (USB_GET_STATE(xfer)) {
1412 case USB_ST_TRANSFERRED:
1413 sc->sc_transfer.data_rem -= actlen;
1414 sc->sc_transfer.data_ptr += actlen;
1415 sc->sc_transfer.actlen += actlen;
1417 if (actlen < sumlen) {
1418 /* short transfer */
1419 sc->sc_transfer.data_rem = 0;
1422 DPRINTF(sc, UDMASS_BBB, "max_bulk=%d, data_rem=%d\n",
1423 max_bulk, sc->sc_transfer.data_rem);
1425 if (sc->sc_transfer.data_rem == 0) {
1426 umass_transfer_start(sc, UMASS_T_BBB_STATUS);
1429 if (max_bulk > sc->sc_transfer.data_rem) {
1430 max_bulk = sc->sc_transfer.data_rem;
1432 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1434 #ifdef UMASS_EXT_BUFFER
1435 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1438 pc = usbd_xfer_get_frame(xfer, 0);
1439 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
1440 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1443 usbd_transfer_submit(xfer);
1446 default: /* Error */
1447 if (error == USB_ERR_CANCELLED) {
1448 umass_tr_error(xfer, error);
1450 umass_transfer_start(sc, UMASS_T_BBB_DATA_WR_CS);
1458 umass_t_bbb_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1460 umass_t_bbb_data_clear_stall_callback(xfer, UMASS_T_BBB_STATUS,
1461 UMASS_T_BBB_DATA_WRITE, error);
1465 umass_t_bbb_status_callback(struct usb_xfer *xfer, usb_error_t error)
1467 struct umass_softc *sc = usbd_xfer_softc(xfer);
1468 union ccb *ccb = sc->sc_transfer.ccb;
1469 struct usb_page_cache *pc;
1473 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
1475 switch (USB_GET_STATE(xfer)) {
1476 case USB_ST_TRANSFERRED:
1479 * Do a full reset if there is something wrong with the CSW:
1481 sc->sc_status_try = 1;
1483 /* Zero missing parts of the CSW: */
1485 if (actlen < sizeof(sc->csw))
1486 memset(&sc->csw, 0, sizeof(sc->csw));
1488 pc = usbd_xfer_get_frame(xfer, 0);
1489 usbd_copy_out(pc, 0, &sc->csw, actlen);
1491 DIF(UDMASS_BBB, umass_bbb_dump_csw(sc, &sc->csw));
1493 residue = UGETDW(sc->csw.dCSWDataResidue);
1495 if ((!residue) || (sc->sc_quirks & IGNORE_RESIDUE)) {
1496 residue = (sc->sc_transfer.data_len -
1497 sc->sc_transfer.actlen);
1499 if (residue > sc->sc_transfer.data_len) {
1500 DPRINTF(sc, UDMASS_BBB, "truncating residue from %d "
1501 "to %d bytes\n", residue, sc->sc_transfer.data_len);
1502 residue = sc->sc_transfer.data_len;
1504 /* translate weird command-status signatures: */
1505 if (sc->sc_quirks & WRONG_CSWSIG) {
1507 uint32_t temp = UGETDW(sc->csw.dCSWSignature);
1509 if ((temp == CSWSIGNATURE_OLYMPUS_C1) ||
1510 (temp == CSWSIGNATURE_IMAGINATION_DBX1)) {
1511 USETDW(sc->csw.dCSWSignature, CSWSIGNATURE);
1514 /* check CSW and handle eventual error */
1515 if (UGETDW(sc->csw.dCSWSignature) != CSWSIGNATURE) {
1516 DPRINTF(sc, UDMASS_BBB, "bad CSW signature 0x%08x != 0x%08x\n",
1517 UGETDW(sc->csw.dCSWSignature), CSWSIGNATURE);
1519 * Invalid CSW: Wrong signature or wrong tag might
1520 * indicate that we lost synchronization. Reset the
1524 } else if (UGETDW(sc->csw.dCSWTag) != UGETDW(sc->cbw.dCBWTag)) {
1525 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: tag 0x%08x should be "
1526 "0x%08x\n", UGETDW(sc->csw.dCSWTag),
1527 UGETDW(sc->cbw.dCBWTag));
1529 } else if (sc->csw.bCSWStatus > CSWSTATUS_PHASE) {
1530 DPRINTF(sc, UDMASS_BBB, "Invalid CSW: status %d > %d\n",
1531 sc->csw.bCSWStatus, CSWSTATUS_PHASE);
1533 } else if (sc->csw.bCSWStatus == CSWSTATUS_PHASE) {
1534 DPRINTF(sc, UDMASS_BBB, "Phase error, residue = "
1537 } else if (sc->sc_transfer.actlen > sc->sc_transfer.data_len) {
1538 DPRINTF(sc, UDMASS_BBB, "Buffer overrun %d > %d\n",
1539 sc->sc_transfer.actlen, sc->sc_transfer.data_len);
1541 } else if (sc->csw.bCSWStatus == CSWSTATUS_FAILED) {
1542 DPRINTF(sc, UDMASS_BBB, "Command failed, residue = "
1545 sc->sc_transfer.ccb = NULL;
1547 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1549 (sc->sc_transfer.callback)
1550 (sc, ccb, residue, STATUS_CMD_FAILED);
1552 sc->sc_transfer.ccb = NULL;
1554 sc->sc_last_xfer_index = UMASS_T_BBB_COMMAND;
1556 (sc->sc_transfer.callback)
1557 (sc, ccb, residue, STATUS_CMD_OK);
1562 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
1563 usbd_transfer_submit(xfer);
1568 DPRINTF(sc, UDMASS_BBB, "Failed to read CSW: %s, try %d\n",
1569 usbd_errstr(error), sc->sc_status_try);
1571 if ((error == USB_ERR_CANCELLED) ||
1572 (sc->sc_status_try)) {
1573 umass_tr_error(xfer, error);
1575 sc->sc_status_try = 1;
1576 umass_transfer_start(sc, UMASS_T_BBB_DATA_RD_CS);
1584 umass_command_start(struct umass_softc *sc, uint8_t dir,
1585 void *data_ptr, uint32_t data_len,
1586 uint32_t data_timeout, umass_callback_t *callback,
1589 sc->sc_transfer.lun = ccb->ccb_h.target_lun;
1592 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1593 * "sc->sc_transfer.cmd_len" has been properly
1597 sc->sc_transfer.dir = data_len ? dir : DIR_NONE;
1598 sc->sc_transfer.data_ptr = data_ptr;
1599 sc->sc_transfer.data_len = data_len;
1600 sc->sc_transfer.data_rem = data_len;
1601 sc->sc_transfer.data_timeout = (data_timeout + UMASS_TIMEOUT);
1603 sc->sc_transfer.actlen = 0;
1604 sc->sc_transfer.callback = callback;
1605 sc->sc_transfer.ccb = ccb;
1607 if (sc->sc_xfer[sc->sc_last_xfer_index]) {
1608 usbd_transfer_start(sc->sc_xfer[sc->sc_last_xfer_index]);
1610 ccb->ccb_h.status = CAM_TID_INVALID;
1616 umass_bbb_get_max_lun(struct umass_softc *sc)
1618 struct usb_device_request req;
1622 /* The Get Max Lun command is a class-specific request. */
1623 req.bmRequestType = UT_READ_CLASS_INTERFACE;
1624 req.bRequest = UR_BBB_GET_MAX_LUN;
1625 USETW(req.wValue, 0);
1626 req.wIndex[0] = sc->sc_iface_no;
1628 USETW(req.wLength, 1);
1630 err = usbd_do_request(sc->sc_udev, NULL, &req, &buf);
1634 /* Device doesn't support Get Max Lun request. */
1635 printf("%s: Get Max Lun not supported (%s)\n",
1636 sc->sc_name, usbd_errstr(err));
1642 * Command/Bulk/Interrupt (CBI) specific functions
1646 umass_cbi_start_status(struct umass_softc *sc)
1648 if (sc->sc_xfer[UMASS_T_CBI_STATUS]) {
1649 umass_transfer_start(sc, UMASS_T_CBI_STATUS);
1651 union ccb *ccb = sc->sc_transfer.ccb;
1653 sc->sc_transfer.ccb = NULL;
1655 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1657 (sc->sc_transfer.callback)
1658 (sc, ccb, (sc->sc_transfer.data_len -
1659 sc->sc_transfer.actlen), STATUS_CMD_UNKNOWN);
1664 umass_t_cbi_reset1_callback(struct usb_xfer *xfer, usb_error_t error)
1666 struct umass_softc *sc = usbd_xfer_softc(xfer);
1667 struct usb_device_request req;
1668 struct usb_page_cache *pc;
1669 uint8_t buf[UMASS_CBI_DIAGNOSTIC_CMDLEN];
1673 switch (USB_GET_STATE(xfer)) {
1674 case USB_ST_TRANSFERRED:
1675 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1680 * Command Block Reset Protocol
1682 * First send a reset request to the device. Then clear
1683 * any possibly stalled bulk endpoints.
1685 * This is done in 3 steps, using 3 transfers:
1686 * UMASS_T_CBI_RESET1
1687 * UMASS_T_CBI_RESET2
1688 * UMASS_T_CBI_RESET3
1689 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1692 DPRINTF(sc, UDMASS_CBI, "CBI reset!\n");
1694 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1695 req.bRequest = UR_CBI_ADSC;
1696 USETW(req.wValue, 0);
1697 req.wIndex[0] = sc->sc_iface_no;
1699 USETW(req.wLength, UMASS_CBI_DIAGNOSTIC_CMDLEN);
1702 * The 0x1d code is the SEND DIAGNOSTIC command. To
1703 * distinguish between the two, the last 10 bytes of the CBL
1704 * is filled with 0xff (section 2.2 of the CBI
1707 buf[0] = 0x1d; /* Command Block Reset */
1710 for (i = 2; i < UMASS_CBI_DIAGNOSTIC_CMDLEN; i++) {
1714 pc = usbd_xfer_get_frame(xfer, 0);
1715 usbd_copy_in(pc, 0, &req, sizeof(req));
1716 pc = usbd_xfer_get_frame(xfer, 1);
1717 usbd_copy_in(pc, 0, buf, sizeof(buf));
1719 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1720 usbd_xfer_set_frame_len(xfer, 1, sizeof(buf));
1721 usbd_xfer_set_frames(xfer, 2);
1722 usbd_transfer_submit(xfer);
1725 default: /* Error */
1726 if (error == USB_ERR_CANCELLED)
1727 umass_tr_error(xfer, error);
1729 umass_transfer_start(sc, UMASS_T_CBI_RESET2);
1736 umass_t_cbi_reset2_callback(struct usb_xfer *xfer, usb_error_t error)
1738 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_RESET3,
1739 UMASS_T_CBI_DATA_READ, error);
1743 umass_t_cbi_reset3_callback(struct usb_xfer *xfer, usb_error_t error)
1745 struct umass_softc *sc = usbd_xfer_softc(xfer);
1747 umass_t_cbi_data_clear_stall_callback
1748 (xfer, (sc->sc_xfer[UMASS_T_CBI_RESET4] &&
1749 sc->sc_xfer[UMASS_T_CBI_STATUS]) ?
1750 UMASS_T_CBI_RESET4 : UMASS_T_CBI_COMMAND,
1751 UMASS_T_CBI_DATA_WRITE, error);
1755 umass_t_cbi_reset4_callback(struct usb_xfer *xfer, usb_error_t error)
1757 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_COMMAND,
1758 UMASS_T_CBI_STATUS, error);
1762 umass_t_cbi_data_clear_stall_callback(struct usb_xfer *xfer,
1763 uint8_t next_xfer, uint8_t stall_xfer, usb_error_t error)
1765 struct umass_softc *sc = usbd_xfer_softc(xfer);
1767 switch (USB_GET_STATE(xfer)) {
1768 case USB_ST_TRANSFERRED:
1770 if (next_xfer == UMASS_T_CBI_STATUS) {
1771 umass_cbi_start_status(sc);
1773 umass_transfer_start(sc, next_xfer);
1778 if (usbd_clear_stall_callback(xfer, sc->sc_xfer[stall_xfer])) {
1779 goto tr_transferred; /* should not happen */
1783 default: /* Error */
1784 umass_tr_error(xfer, error);
1791 umass_t_cbi_command_callback(struct usb_xfer *xfer, usb_error_t error)
1793 struct umass_softc *sc = usbd_xfer_softc(xfer);
1794 union ccb *ccb = sc->sc_transfer.ccb;
1795 struct usb_device_request req;
1796 struct usb_page_cache *pc;
1798 switch (USB_GET_STATE(xfer)) {
1799 case USB_ST_TRANSFERRED:
1801 if (sc->sc_transfer.dir == DIR_NONE) {
1802 umass_cbi_start_status(sc);
1804 umass_transfer_start
1805 (sc, (sc->sc_transfer.dir == DIR_IN) ?
1806 UMASS_T_CBI_DATA_READ : UMASS_T_CBI_DATA_WRITE);
1815 * do a CBI transfer with cmd_len bytes from
1816 * cmd_data, possibly a data phase of data_len
1817 * bytes from/to the device and finally a status
1821 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
1822 req.bRequest = UR_CBI_ADSC;
1823 USETW(req.wValue, 0);
1824 req.wIndex[0] = sc->sc_iface_no;
1826 req.wLength[0] = sc->sc_transfer.cmd_len;
1829 pc = usbd_xfer_get_frame(xfer, 0);
1830 usbd_copy_in(pc, 0, &req, sizeof(req));
1831 pc = usbd_xfer_get_frame(xfer, 1);
1832 usbd_copy_in(pc, 0, sc->sc_transfer.cmd_data,
1833 sc->sc_transfer.cmd_len);
1835 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
1836 usbd_xfer_set_frame_len(xfer, 1, sc->sc_transfer.cmd_len);
1837 usbd_xfer_set_frames(xfer,
1838 sc->sc_transfer.cmd_len ? 2 : 1);
1841 umass_cbi_dump_cmd(sc,
1842 sc->sc_transfer.cmd_data,
1843 sc->sc_transfer.cmd_len));
1845 usbd_transfer_submit(xfer);
1849 default: /* Error */
1851 * STALL on the control pipe can be result of the command error.
1852 * Attempt to clear this STALL same as for bulk pipe also
1853 * results in command completion interrupt, but ASC/ASCQ there
1854 * look like not always valid, so don't bother about it.
1856 if ((error == USB_ERR_STALLED) ||
1857 (sc->sc_transfer.callback == &umass_cam_cb)) {
1858 sc->sc_transfer.ccb = NULL;
1859 (sc->sc_transfer.callback)
1860 (sc, ccb, sc->sc_transfer.data_len,
1861 STATUS_CMD_UNKNOWN);
1863 umass_tr_error(xfer, error);
1865 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
1872 umass_t_cbi_data_read_callback(struct usb_xfer *xfer, usb_error_t error)
1874 struct umass_softc *sc = usbd_xfer_softc(xfer);
1875 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1876 #ifndef UMASS_EXT_BUFFER
1877 struct usb_page_cache *pc;
1881 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1883 switch (USB_GET_STATE(xfer)) {
1884 case USB_ST_TRANSFERRED:
1885 #ifndef UMASS_EXT_BUFFER
1886 pc = usbd_xfer_get_frame(xfer, 0);
1887 usbd_copy_out(pc, 0, sc->sc_transfer.data_ptr, actlen);
1889 sc->sc_transfer.data_rem -= actlen;
1890 sc->sc_transfer.data_ptr += actlen;
1891 sc->sc_transfer.actlen += actlen;
1893 if (actlen < sumlen) {
1894 /* short transfer */
1895 sc->sc_transfer.data_rem = 0;
1898 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1899 max_bulk, sc->sc_transfer.data_rem);
1901 if (sc->sc_transfer.data_rem == 0) {
1902 umass_cbi_start_status(sc);
1905 if (max_bulk > sc->sc_transfer.data_rem) {
1906 max_bulk = sc->sc_transfer.data_rem;
1908 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1910 #ifdef UMASS_EXT_BUFFER
1911 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1914 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1916 usbd_transfer_submit(xfer);
1919 default: /* Error */
1920 if ((error == USB_ERR_CANCELLED) ||
1921 (sc->sc_transfer.callback != &umass_cam_cb)) {
1922 umass_tr_error(xfer, error);
1924 umass_transfer_start(sc, UMASS_T_CBI_DATA_RD_CS);
1932 umass_t_cbi_data_rd_cs_callback(struct usb_xfer *xfer, usb_error_t error)
1934 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
1935 UMASS_T_CBI_DATA_READ, error);
1939 umass_t_cbi_data_write_callback(struct usb_xfer *xfer, usb_error_t error)
1941 struct umass_softc *sc = usbd_xfer_softc(xfer);
1942 uint32_t max_bulk = usbd_xfer_max_len(xfer);
1943 #ifndef UMASS_EXT_BUFFER
1944 struct usb_page_cache *pc;
1948 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1950 switch (USB_GET_STATE(xfer)) {
1951 case USB_ST_TRANSFERRED:
1952 sc->sc_transfer.data_rem -= actlen;
1953 sc->sc_transfer.data_ptr += actlen;
1954 sc->sc_transfer.actlen += actlen;
1956 if (actlen < sumlen) {
1957 /* short transfer */
1958 sc->sc_transfer.data_rem = 0;
1961 DPRINTF(sc, UDMASS_CBI, "max_bulk=%d, data_rem=%d\n",
1962 max_bulk, sc->sc_transfer.data_rem);
1964 if (sc->sc_transfer.data_rem == 0) {
1965 umass_cbi_start_status(sc);
1968 if (max_bulk > sc->sc_transfer.data_rem) {
1969 max_bulk = sc->sc_transfer.data_rem;
1971 usbd_xfer_set_timeout(xfer, sc->sc_transfer.data_timeout);
1973 #ifdef UMASS_EXT_BUFFER
1974 usbd_xfer_set_frame_data(xfer, 0, sc->sc_transfer.data_ptr,
1977 pc = usbd_xfer_get_frame(xfer, 0);
1978 usbd_copy_in(pc, 0, sc->sc_transfer.data_ptr, max_bulk);
1979 usbd_xfer_set_frame_len(xfer, 0, max_bulk);
1982 usbd_transfer_submit(xfer);
1985 default: /* Error */
1986 if ((error == USB_ERR_CANCELLED) ||
1987 (sc->sc_transfer.callback != &umass_cam_cb)) {
1988 umass_tr_error(xfer, error);
1990 umass_transfer_start(sc, UMASS_T_CBI_DATA_WR_CS);
1998 umass_t_cbi_data_wr_cs_callback(struct usb_xfer *xfer, usb_error_t error)
2000 umass_t_cbi_data_clear_stall_callback(xfer, UMASS_T_CBI_STATUS,
2001 UMASS_T_CBI_DATA_WRITE, error);
2005 umass_t_cbi_status_callback(struct usb_xfer *xfer, usb_error_t error)
2007 struct umass_softc *sc = usbd_xfer_softc(xfer);
2008 union ccb *ccb = sc->sc_transfer.ccb;
2009 struct usb_page_cache *pc;
2014 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
2016 switch (USB_GET_STATE(xfer)) {
2017 case USB_ST_TRANSFERRED:
2019 if (actlen < sizeof(sc->sbl)) {
2022 pc = usbd_xfer_get_frame(xfer, 0);
2023 usbd_copy_out(pc, 0, &sc->sbl, sizeof(sc->sbl));
2025 residue = (sc->sc_transfer.data_len -
2026 sc->sc_transfer.actlen);
2028 /* dissect the information in the buffer */
2030 if (sc->sc_proto & UMASS_PROTO_UFI) {
2033 * Section 3.4.3.1.3 specifies that the UFI command
2034 * protocol returns an ASC and ASCQ in the interrupt
2038 DPRINTF(sc, UDMASS_CBI, "UFI CCI, ASC = 0x%02x, "
2039 "ASCQ = 0x%02x\n", sc->sbl.ufi.asc,
2042 status = (((sc->sbl.ufi.asc == 0) &&
2043 (sc->sbl.ufi.ascq == 0)) ?
2044 STATUS_CMD_OK : STATUS_CMD_FAILED);
2046 sc->sc_transfer.ccb = NULL;
2048 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2050 (sc->sc_transfer.callback)
2051 (sc, ccb, residue, status);
2057 /* Command Interrupt Data Block */
2059 DPRINTF(sc, UDMASS_CBI, "type=0x%02x, value=0x%02x\n",
2060 sc->sbl.common.type, sc->sbl.common.value);
2062 if (sc->sbl.common.type == IDB_TYPE_CCI) {
2064 status = (sc->sbl.common.value & IDB_VALUE_STATUS_MASK);
2066 status = ((status == IDB_VALUE_PASS) ? STATUS_CMD_OK :
2067 (status == IDB_VALUE_FAIL) ? STATUS_CMD_FAILED :
2068 (status == IDB_VALUE_PERSISTENT) ? STATUS_CMD_FAILED :
2069 STATUS_WIRE_FAILED);
2071 sc->sc_transfer.ccb = NULL;
2073 sc->sc_last_xfer_index = UMASS_T_CBI_COMMAND;
2075 (sc->sc_transfer.callback)
2076 (sc, ccb, residue, status);
2086 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
2087 usbd_transfer_submit(xfer);
2090 default: /* Error */
2091 DPRINTF(sc, UDMASS_CBI, "Failed to read CSW: %s\n",
2092 usbd_errstr(error));
2093 umass_tr_error(xfer, error);
2100 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2104 umass_cam_attach_sim(struct umass_softc *sc)
2106 struct cam_devq *devq; /* Per device Queue */
2109 * A HBA is attached to the CAM layer.
2111 * The CAM layer will then after a while start probing for devices on
2112 * the bus. The number of SIMs is limited to one.
2115 devq = cam_simq_alloc(1 /* maximum openings */ );
2119 sc->sc_sim = cam_sim_alloc
2120 (&umass_cam_action, &umass_cam_poll,
2123 sc->sc_unit /* unit number */ ,
2124 #if (__FreeBSD_version >= 700037)
2125 &sc->sc_mtx /* mutex */ ,
2127 1 /* maximum device openings */ ,
2128 0 /* maximum tagged device openings */ ,
2131 if (sc->sc_sim == NULL) {
2132 cam_simq_free(devq);
2136 #if (__FreeBSD_version >= 700037)
2137 mtx_lock(&sc->sc_mtx);
2140 #if (__FreeBSD_version >= 700048)
2141 if (xpt_bus_register(sc->sc_sim, sc->sc_dev, sc->sc_unit) != CAM_SUCCESS) {
2142 mtx_unlock(&sc->sc_mtx);
2146 if (xpt_bus_register(sc->sc_sim, sc->sc_unit) != CAM_SUCCESS) {
2147 #if (__FreeBSD_version >= 700037)
2148 mtx_unlock(&sc->sc_mtx);
2154 #if (__FreeBSD_version >= 700037)
2155 mtx_unlock(&sc->sc_mtx);
2161 umass_cam_attach(struct umass_softc *sc)
2166 printf("%s:%d:%d:%d: Attached to scbus%d\n",
2167 sc->sc_name, cam_sim_path(sc->sc_sim),
2168 sc->sc_unit, CAM_LUN_WILDCARD,
2169 cam_sim_path(sc->sc_sim));
2173 * detach from the CAM layer
2177 umass_cam_detach_sim(struct umass_softc *sc)
2179 if (sc->sc_sim != NULL) {
2180 if (xpt_bus_deregister(cam_sim_path(sc->sc_sim))) {
2181 /* accessing the softc is not possible after this */
2182 sc->sc_sim->softc = UMASS_GONE;
2183 cam_sim_free(sc->sc_sim, /* free_devq */ TRUE);
2185 panic("%s: CAM layer is busy\n",
2193 * CAM requests for action come through here
2197 umass_cam_action(struct cam_sim *sim, union ccb *ccb)
2199 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2201 if (sc == UMASS_GONE ||
2202 (sc != NULL && !usbd_device_attached(sc->sc_udev))) {
2203 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
2208 #if (__FreeBSD_version < 700037)
2209 mtx_lock(&sc->sc_mtx);
2213 * Verify, depending on the operation to perform, that we either got
2214 * a valid sc, because an existing target was referenced, or
2215 * otherwise the SIM is addressed.
2217 * This avoids bombing out at a printf and does give the CAM layer some
2218 * sensible feedback on errors.
2220 switch (ccb->ccb_h.func_code) {
2223 case XPT_GET_TRAN_SETTINGS:
2224 case XPT_SET_TRAN_SETTINGS:
2225 case XPT_CALC_GEOMETRY:
2226 /* the opcodes requiring a target. These should never occur. */
2228 DPRINTF(sc, UDMASS_GEN, "%s:%d:%d:%d:func_code 0x%04x: "
2229 "Invalid target (target needed)\n",
2230 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2231 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2232 ccb->ccb_h.func_code);
2234 ccb->ccb_h.status = CAM_TID_INVALID;
2242 * The opcodes sometimes aimed at a target (sc is valid),
2243 * sometimes aimed at the SIM (sc is invalid and target is
2244 * CAM_TARGET_WILDCARD)
2247 (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD)) {
2248 DPRINTF(sc, UDMASS_SCSI, "%s:%d:%d:%d:func_code 0x%04x: "
2249 "Invalid target (no wildcard)\n",
2250 DEVNAME_SIM, cam_sim_path(sc->sc_sim),
2251 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
2252 ccb->ccb_h.func_code);
2254 ccb->ccb_h.status = CAM_TID_INVALID;
2260 /* XXX Hm, we should check the input parameters */
2264 /* Perform the requested action */
2265 switch (ccb->ccb_h.func_code) {
2271 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2272 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2274 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2277 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2278 "cmd: 0x%02x, flags: 0x%02x, "
2279 "%db cmd/%db data/%db sense\n",
2280 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2281 ccb->ccb_h.target_lun, cmd[0],
2282 ccb->ccb_h.flags & CAM_DIR_MASK, ccb->csio.cdb_len,
2283 ccb->csio.dxfer_len, ccb->csio.sense_len);
2285 if (sc->sc_transfer.ccb) {
2286 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SCSI_IO: "
2287 "I/O in progress, deferring\n",
2288 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2289 ccb->ccb_h.target_lun);
2290 ccb->ccb_h.status = CAM_SCSI_BUSY;
2294 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
2301 umass_dump_buffer(sc, ccb->csio.data_ptr,
2302 ccb->csio.dxfer_len, 48));
2308 ccb->ccb_h.status = CAM_REQ_INPROG | CAM_SIM_QUEUED;
2311 * sc->sc_transform will convert the command to the
2312 * command format needed by the specific command set
2313 * and return the converted command in
2314 * "sc->sc_transfer.cmd_data"
2316 if (umass_std_transform(sc, ccb, cmd, ccb->csio.cdb_len)) {
2318 if (sc->sc_transfer.cmd_data[0] == INQUIRY) {
2319 const char *pserial;
2321 pserial = usb_get_serial(sc->sc_udev);
2324 * Umass devices don't generally report their serial numbers
2325 * in the usual SCSI way. Emulate it here.
2327 if ((sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2328 (sc->sc_transfer.cmd_data[2] == SVPD_UNIT_SERIAL_NUMBER) &&
2329 (pserial[0] != '\0')) {
2330 struct scsi_vpd_unit_serial_number *vpd_serial;
2332 vpd_serial = (struct scsi_vpd_unit_serial_number *)ccb->csio.data_ptr;
2333 vpd_serial->length = strlen(pserial);
2334 if (vpd_serial->length > sizeof(vpd_serial->serial_num))
2335 vpd_serial->length = sizeof(vpd_serial->serial_num);
2336 memcpy(vpd_serial->serial_num, pserial, vpd_serial->length);
2337 ccb->csio.scsi_status = SCSI_STATUS_OK;
2338 ccb->ccb_h.status = CAM_REQ_CMP;
2344 * Handle EVPD inquiry for broken devices first
2345 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2347 if ((sc->sc_quirks & (NO_INQUIRY_EVPD | NO_INQUIRY)) &&
2348 (sc->sc_transfer.cmd_data[1] & SI_EVPD)) {
2350 scsi_set_sense_data(&ccb->csio.sense_data,
2351 /*sense_format*/ SSD_TYPE_NONE,
2352 /*current_error*/ 1,
2353 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
2356 /*extra args*/ SSD_ELEM_NONE);
2357 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2358 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR |
2364 * Return fake inquiry data for
2367 if (sc->sc_quirks & NO_INQUIRY) {
2368 memcpy(ccb->csio.data_ptr, &fake_inq_data,
2369 sizeof(fake_inq_data));
2370 ccb->csio.scsi_status = SCSI_STATUS_OK;
2371 ccb->ccb_h.status = CAM_REQ_CMP;
2375 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2376 ccb->csio.dxfer_len = SHORT_INQUIRY_LENGTH;
2378 } else if (sc->sc_transfer.cmd_data[0] == SYNCHRONIZE_CACHE) {
2379 if (sc->sc_quirks & NO_SYNCHRONIZE_CACHE) {
2380 ccb->csio.scsi_status = SCSI_STATUS_OK;
2381 ccb->ccb_h.status = CAM_REQ_CMP;
2386 umass_command_start(sc, dir, ccb->csio.data_ptr,
2387 ccb->csio.dxfer_len,
2389 &umass_cam_cb, ccb);
2395 struct ccb_pathinq *cpi = &ccb->cpi;
2397 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_PATH_INQ:.\n",
2398 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2399 ccb->ccb_h.target_lun);
2401 /* host specific information */
2402 cpi->version_num = 1;
2403 cpi->hba_inquiry = 0;
2404 cpi->target_sprt = 0;
2405 cpi->hba_misc = PIM_NO_6_BYTE;
2406 cpi->hba_eng_cnt = 0;
2407 cpi->max_target = UMASS_SCSIID_MAX; /* one target */
2408 cpi->initiator_id = UMASS_SCSIID_HOST;
2409 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2410 strlcpy(cpi->hba_vid, "USB SCSI", HBA_IDLEN);
2411 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2412 cpi->unit_number = cam_sim_unit(sim);
2413 cpi->bus_id = sc->sc_unit;
2414 #if (__FreeBSD_version >= 700025)
2415 cpi->protocol = PROTO_SCSI;
2416 cpi->protocol_version = SCSI_REV_2;
2417 cpi->transport = XPORT_USB;
2418 cpi->transport_version = 0;
2421 cpi->base_transfer_speed = 0;
2424 if (sc->sc_quirks & FLOPPY_SPEED) {
2425 cpi->base_transfer_speed =
2426 UMASS_FLOPPY_TRANSFER_SPEED;
2428 switch (usbd_get_speed(sc->sc_udev)) {
2429 case USB_SPEED_SUPER:
2430 cpi->base_transfer_speed =
2431 UMASS_SUPER_TRANSFER_SPEED;
2432 cpi->maxio = MAXPHYS;
2434 case USB_SPEED_HIGH:
2435 cpi->base_transfer_speed =
2436 UMASS_HIGH_TRANSFER_SPEED;
2439 cpi->base_transfer_speed =
2440 UMASS_FULL_TRANSFER_SPEED;
2444 cpi->max_lun = sc->sc_maxlun;
2447 cpi->ccb_h.status = CAM_REQ_CMP;
2453 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_RESET_DEV:.\n",
2454 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2455 ccb->ccb_h.target_lun);
2459 ccb->ccb_h.status = CAM_REQ_CMP;
2463 case XPT_GET_TRAN_SETTINGS:
2465 struct ccb_trans_settings *cts = &ccb->cts;
2467 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2468 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2469 ccb->ccb_h.target_lun);
2471 #if (__FreeBSD_version >= 700025)
2472 cts->protocol = PROTO_SCSI;
2473 cts->protocol_version = SCSI_REV_2;
2474 cts->transport = XPORT_USB;
2475 cts->transport_version = 0;
2476 cts->xport_specific.valid = 0;
2479 cts->flags = 0; /* no disconnection, tagging */
2481 ccb->ccb_h.status = CAM_REQ_CMP;
2485 case XPT_SET_TRAN_SETTINGS:
2487 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2488 cam_sim_path(sc->sc_sim), ccb->ccb_h.target_id,
2489 ccb->ccb_h.target_lun);
2491 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2495 case XPT_CALC_GEOMETRY:
2497 cam_calc_geometry(&ccb->ccg, /* extended */ 1);
2503 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:XPT_NOOP:.\n",
2504 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2505 ccb->ccb_h.target_lun);
2507 ccb->ccb_h.status = CAM_REQ_CMP;
2512 DPRINTF(sc, UDMASS_SCSI, "%d:%d:%d:func_code 0x%04x: "
2513 "Not implemented\n",
2514 sc ? cam_sim_path(sc->sc_sim) : -1, ccb->ccb_h.target_id,
2515 ccb->ccb_h.target_lun, ccb->ccb_h.func_code);
2517 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2523 #if (__FreeBSD_version < 700037)
2525 mtx_unlock(&sc->sc_mtx);
2532 umass_cam_poll(struct cam_sim *sim)
2534 struct umass_softc *sc = (struct umass_softc *)sim->softc;
2536 if (sc == UMASS_GONE)
2539 DPRINTF(sc, UDMASS_SCSI, "CAM poll\n");
2541 usbd_transfer_poll(sc->sc_xfer, UMASS_T_MAX);
2546 * finalise a completed CAM command
2550 umass_cam_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2553 ccb->csio.resid = residue;
2557 ccb->ccb_h.status = CAM_REQ_CMP;
2558 if ((sc->sc_quirks & READ_CAPACITY_OFFBY1) &&
2559 (ccb->ccb_h.func_code == XPT_SCSI_IO) &&
2560 (ccb->csio.cdb_io.cdb_bytes[0] == READ_CAPACITY)) {
2561 struct scsi_read_capacity_data *rcap;
2564 rcap = (void *)(ccb->csio.data_ptr);
2565 maxsector = scsi_4btoul(rcap->addr) - 1;
2566 scsi_ulto4b(maxsector, rcap->addr);
2569 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2570 * of pages supported by the device - otherwise, CAM
2571 * will never ask us for the serial number if the
2572 * device cannot handle that by itself.
2574 if (ccb->ccb_h.func_code == XPT_SCSI_IO &&
2575 sc->sc_transfer.cmd_data[0] == INQUIRY &&
2576 (sc->sc_transfer.cmd_data[1] & SI_EVPD) &&
2577 sc->sc_transfer.cmd_data[2] == SVPD_SUPPORTED_PAGE_LIST &&
2578 (usb_get_serial(sc->sc_udev)[0] != '\0')) {
2579 struct ccb_scsiio *csio;
2580 struct scsi_vpd_supported_page_list *page_list;
2583 page_list = (struct scsi_vpd_supported_page_list *)csio->data_ptr;
2584 if (page_list->length + 1 < SVPD_SUPPORTED_PAGES_SIZE) {
2585 page_list->list[page_list->length] = SVPD_UNIT_SERIAL_NUMBER;
2586 page_list->length++;
2592 case STATUS_CMD_UNKNOWN:
2593 case STATUS_CMD_FAILED:
2595 /* fetch sense data */
2597 /* the rest of the command was filled in at attach */
2598 sc->cam_scsi_sense.length = ccb->csio.sense_len;
2600 DPRINTF(sc, UDMASS_SCSI, "Fetching %d bytes of "
2601 "sense data\n", ccb->csio.sense_len);
2603 if (umass_std_transform(sc, ccb, &sc->cam_scsi_sense.opcode,
2604 sizeof(sc->cam_scsi_sense))) {
2606 if ((sc->sc_quirks & FORCE_SHORT_INQUIRY) &&
2607 (sc->sc_transfer.cmd_data[0] == INQUIRY)) {
2608 ccb->csio.sense_len = SHORT_INQUIRY_LENGTH;
2610 umass_command_start(sc, DIR_IN, &ccb->csio.sense_data.error_code,
2611 ccb->csio.sense_len, ccb->ccb_h.timeout,
2612 &umass_cam_sense_cb, ccb);
2618 * The wire protocol failed and will hopefully have
2619 * recovered. We return an error to CAM and let CAM
2620 * retry the command if necessary.
2622 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2629 * Finalise a completed autosense operation
2632 umass_cam_sense_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2639 case STATUS_CMD_UNKNOWN:
2640 case STATUS_CMD_FAILED: {
2643 ccb->csio.sense_resid = residue;
2644 sense_len = ccb->csio.sense_len - ccb->csio.sense_resid;
2645 key = scsi_get_sense_key(&ccb->csio.sense_data, sense_len,
2648 if (ccb->csio.ccb_h.flags & CAM_CDB_POINTER) {
2649 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_ptr);
2651 cmd = (uint8_t *)(ccb->csio.cdb_io.cdb_bytes);
2655 * Getting sense data always succeeds (apart from wire
2658 if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2659 (cmd[0] == INQUIRY) &&
2660 (key == SSD_KEY_UNIT_ATTENTION)) {
2662 * Ignore unit attention errors in the case where
2663 * the Unit Attention state is not cleared on
2664 * REQUEST SENSE. They will appear again at the next
2667 ccb->ccb_h.status = CAM_REQ_CMP;
2668 } else if (key == SSD_KEY_NO_SENSE) {
2670 * No problem after all (in the case of CBI without
2673 ccb->ccb_h.status = CAM_REQ_CMP;
2674 } else if ((sc->sc_quirks & RS_NO_CLEAR_UA) &&
2675 (cmd[0] == READ_CAPACITY) &&
2676 (key == SSD_KEY_UNIT_ATTENTION)) {
2678 * Some devices do not clear the unit attention error
2679 * on request sense. We insert a test unit ready
2680 * command to make sure we clear the unit attention
2681 * condition, then allow the retry to proceed as
2685 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2686 | CAM_AUTOSNS_VALID;
2687 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2692 DPRINTF(sc, UDMASS_SCSI, "Doing a sneaky"
2693 "TEST_UNIT_READY\n");
2695 /* the rest of the command was filled in at attach */
2697 if (umass_std_transform(sc, ccb,
2698 &sc->cam_scsi_test_unit_ready.opcode,
2699 sizeof(sc->cam_scsi_test_unit_ready))) {
2700 umass_command_start(sc, DIR_NONE, NULL, 0,
2702 &umass_cam_quirk_cb, ccb);
2706 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2707 | CAM_AUTOSNS_VALID;
2708 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2714 DPRINTF(sc, UDMASS_SCSI, "Autosense failed, "
2715 "status %d\n", status);
2716 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
2722 * This completion code just handles the fact that we sent a test-unit-ready
2723 * after having previously failed a READ CAPACITY with CHECK_COND. Even
2724 * though this command succeeded, we have to tell CAM to retry.
2727 umass_cam_quirk_cb(struct umass_softc *sc, union ccb *ccb, uint32_t residue,
2730 DPRINTF(sc, UDMASS_SCSI, "Test unit ready "
2731 "returned status %d\n", status);
2733 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR
2734 | CAM_AUTOSNS_VALID;
2735 ccb->csio.scsi_status = SCSI_STATUS_CHECK_COND;
2740 * SCSI specific functions
2744 umass_scsi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2747 if ((cmd_len == 0) ||
2748 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2749 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2750 "length: %d bytes\n", cmd_len);
2751 return (0); /* failure */
2753 sc->sc_transfer.cmd_len = cmd_len;
2755 switch (cmd_ptr[0]) {
2756 case TEST_UNIT_READY:
2757 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2758 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2760 memset(sc->sc_transfer.cmd_data, 0, cmd_len);
2761 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2762 sc->sc_transfer.cmd_data[4] = SSS_START;
2769 * some drives wedge when asked for full inquiry
2772 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2773 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2774 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2780 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2785 umass_rbc_transform(struct umass_softc *sc, uint8_t *cmd_ptr, uint8_t cmd_len)
2787 if ((cmd_len == 0) ||
2788 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2789 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2790 "length: %d bytes\n", cmd_len);
2791 return (0); /* failure */
2793 switch (cmd_ptr[0]) {
2794 /* these commands are defined in RBC: */
2797 case START_STOP_UNIT:
2798 case SYNCHRONIZE_CACHE:
2800 case 0x2f: /* VERIFY_10 is absent from
2803 case MODE_SELECT_10:
2805 case TEST_UNIT_READY:
2808 * The following commands are not listed in my copy of the
2809 * RBC specs. CAM however seems to want those, and at least
2810 * the Sony DSC device appears to support those as well
2815 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2817 if ((sc->sc_quirks & RBC_PAD_TO_12) && (cmd_len < 12)) {
2818 memset(sc->sc_transfer.cmd_data + cmd_len,
2822 sc->sc_transfer.cmd_len = cmd_len;
2823 return (1); /* sucess */
2825 /* All other commands are not legal in RBC */
2827 DPRINTF(sc, UDMASS_SCSI, "Unsupported RBC "
2828 "command 0x%02x\n", cmd_ptr[0]);
2829 return (0); /* failure */
2834 umass_ufi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2837 if ((cmd_len == 0) ||
2838 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2839 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2840 "length: %d bytes\n", cmd_len);
2841 return (0); /* failure */
2843 /* An UFI command is always 12 bytes in length */
2844 sc->sc_transfer.cmd_len = UFI_COMMAND_LENGTH;
2846 /* Zero the command data */
2847 memset(sc->sc_transfer.cmd_data, 0, UFI_COMMAND_LENGTH);
2849 switch (cmd_ptr[0]) {
2851 * Commands of which the format has been verified. They
2852 * should work. Copy the command into the (zeroed out)
2853 * destination buffer.
2855 case TEST_UNIT_READY:
2856 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2858 * Some devices do not support this command. Start
2859 * Stop Unit should give the same results
2861 DPRINTF(sc, UDMASS_UFI, "Converted TEST_UNIT_READY "
2864 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2865 sc->sc_transfer.cmd_data[4] = SSS_START;
2874 case START_STOP_UNIT:
2875 case SEND_DIAGNOSTIC:
2880 case POSITION_TO_ELEMENT: /* SEEK_10 */
2881 case WRITE_AND_VERIFY:
2883 case MODE_SELECT_10:
2887 case READ_FORMAT_CAPACITIES:
2891 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2892 * required for UFI devices, so it is appropriate to fake
2895 case SYNCHRONIZE_CACHE:
2899 DPRINTF(sc, UDMASS_SCSI, "Unsupported UFI "
2900 "command 0x%02x\n", cmd_ptr[0]);
2901 return (0); /* failure */
2904 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2905 return (1); /* success */
2909 * 8070i (ATAPI) specific functions
2912 umass_atapi_transform(struct umass_softc *sc, uint8_t *cmd_ptr,
2915 if ((cmd_len == 0) ||
2916 (cmd_len > sizeof(sc->sc_transfer.cmd_data))) {
2917 DPRINTF(sc, UDMASS_SCSI, "Invalid command "
2918 "length: %d bytes\n", cmd_len);
2919 return (0); /* failure */
2921 /* An ATAPI command is always 12 bytes in length. */
2922 sc->sc_transfer.cmd_len = ATAPI_COMMAND_LENGTH;
2924 /* Zero the command data */
2925 memset(sc->sc_transfer.cmd_data, 0, ATAPI_COMMAND_LENGTH);
2927 switch (cmd_ptr[0]) {
2929 * Commands of which the format has been verified. They
2930 * should work. Copy the command into the destination
2935 * some drives wedge when asked for full inquiry
2938 if (sc->sc_quirks & FORCE_SHORT_INQUIRY) {
2939 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
2941 sc->sc_transfer.cmd_data[4] = SHORT_INQUIRY_LENGTH;
2946 case TEST_UNIT_READY:
2947 if (sc->sc_quirks & NO_TEST_UNIT_READY) {
2948 DPRINTF(sc, UDMASS_SCSI, "Converted TEST_UNIT_READY "
2950 sc->sc_transfer.cmd_data[0] = START_STOP_UNIT;
2951 sc->sc_transfer.cmd_data[4] = SSS_START;
2958 case START_STOP_UNIT:
2959 case SEND_DIAGNOSTIC:
2964 case POSITION_TO_ELEMENT: /* SEEK_10 */
2965 case SYNCHRONIZE_CACHE:
2966 case MODE_SELECT_10:
2969 case 0x42: /* READ_SUBCHANNEL */
2970 case 0x43: /* READ_TOC */
2971 case 0x44: /* READ_HEADER */
2972 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
2973 case 0x48: /* PLAY_TRACK */
2974 case 0x49: /* PLAY_TRACK_REL */
2975 case 0x4b: /* PAUSE */
2976 case 0x51: /* READ_DISK_INFO */
2977 case 0x52: /* READ_TRACK_INFO */
2978 case 0x54: /* SEND_OPC */
2979 case 0x59: /* READ_MASTER_CUE */
2980 case 0x5b: /* CLOSE_TR_SESSION */
2981 case 0x5c: /* READ_BUFFER_CAP */
2982 case 0x5d: /* SEND_CUE_SHEET */
2983 case 0xa1: /* BLANK */
2984 case 0xa5: /* PLAY_12 */
2985 case 0xa6: /* EXCHANGE_MEDIUM */
2986 case 0xad: /* READ_DVD_STRUCTURE */
2987 case 0xbb: /* SET_CD_SPEED */
2988 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
2994 DPRINTF(sc, UDMASS_SCSI, "Unsupported ATAPI "
2995 "command 0x%02x - trying anyway\n",
3000 memcpy(sc->sc_transfer.cmd_data, cmd_ptr, cmd_len);
3001 return (1); /* success */
3005 umass_no_transform(struct umass_softc *sc, uint8_t *cmd,
3008 return (0); /* failure */
3012 umass_std_transform(struct umass_softc *sc, union ccb *ccb,
3013 uint8_t *cmd, uint8_t cmdlen)
3017 retval = (sc->sc_transform) (sc, cmd, cmdlen);
3020 ccb->ccb_h.status = CAM_REQ_CMP;
3023 } else if (retval == 0) {
3024 ccb->ccb_h.status = CAM_REQ_INVALID;
3028 /* Command should be executed */
3034 umass_bbb_dump_cbw(struct umass_softc *sc, umass_bbb_cbw_t *cbw)
3036 uint8_t *c = cbw->CBWCDB;
3038 uint32_t dlen = UGETDW(cbw->dCBWDataTransferLength);
3039 uint32_t tag = UGETDW(cbw->dCBWTag);
3041 uint8_t clen = cbw->bCDBLength;
3042 uint8_t flags = cbw->bCBWFlags;
3043 uint8_t lun = cbw->bCBWLUN;
3045 DPRINTF(sc, UDMASS_BBB, "CBW %d: cmd = %db "
3046 "(0x%02x%02x%02x%02x%02x%02x%s), "
3047 "data = %db, lun = %d, dir = %s\n",
3049 c[0], c[1], c[2], c[3], c[4], c[5], (clen > 6 ? "..." : ""),
3050 dlen, lun, (flags == CBWFLAGS_IN ? "in" :
3051 (flags == CBWFLAGS_OUT ? "out" : "<invalid>")));
3055 umass_bbb_dump_csw(struct umass_softc *sc, umass_bbb_csw_t *csw)
3057 uint32_t sig = UGETDW(csw->dCSWSignature);
3058 uint32_t tag = UGETDW(csw->dCSWTag);
3059 uint32_t res = UGETDW(csw->dCSWDataResidue);
3060 uint8_t status = csw->bCSWStatus;
3062 DPRINTF(sc, UDMASS_BBB, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3063 "res = %d, status = 0x%02x (%s)\n",
3064 tag, sig, (sig == CSWSIGNATURE ? "valid" : "invalid"),
3066 status, (status == CSWSTATUS_GOOD ? "good" :
3067 (status == CSWSTATUS_FAILED ? "failed" :
3068 (status == CSWSTATUS_PHASE ? "phase" : "<invalid>"))));
3072 umass_cbi_dump_cmd(struct umass_softc *sc, void *cmd, uint8_t cmdlen)
3075 uint8_t dir = sc->sc_transfer.dir;
3077 DPRINTF(sc, UDMASS_BBB, "cmd = %db "
3078 "(0x%02x%02x%02x%02x%02x%02x%s), "
3079 "data = %db, dir = %s\n",
3081 c[0], c[1], c[2], c[3], c[4], c[5], (cmdlen > 6 ? "..." : ""),
3082 sc->sc_transfer.data_len,
3083 (dir == DIR_IN ? "in" :
3084 (dir == DIR_OUT ? "out" :
3085 (dir == DIR_NONE ? "no data phase" : "<invalid>"))));
3089 umass_dump_buffer(struct umass_softc *sc, uint8_t *buffer, uint32_t buflen,
3100 sprintf(s2, " buffer=%p, buflen=%d", buffer, buflen);
3101 for (i = 0; (i < buflen) && (i < printlen); i++) {
3103 if (j == 0 && i != 0) {
3104 DPRINTF(sc, UDMASS_GEN, "0x %s%s\n",
3108 sprintf(&s1[j * 2], "%02x", buffer[i] & 0xff);
3110 if (buflen > printlen)
3111 sprintf(s3, " ...");
3112 DPRINTF(sc, UDMASS_GEN, "0x %s%s%s\n",