2 * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
3 * Nick Hibma <n_hibma@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $NetBSD: umass.c,v 1.28 2000/04/02 23:46:53 augustss Exp $
31 /* Also already merged from NetBSD:
32 * $NetBSD: umass.c,v 1.67 2001/11/25 19:05:22 augustss Exp $
33 * $NetBSD: umass.c,v 1.90 2002/11/04 19:17:33 pooka Exp $
34 * $NetBSD: umass.c,v 1.108 2003/11/07 17:03:25 wiz Exp $
35 * $NetBSD: umass.c,v 1.109 2003/12/04 13:57:31 keihan Exp $
39 * Universal Serial Bus Mass Storage Class specs:
40 * http://www.usb.org/developers/devclass_docs/usb_msc_overview_1.2.pdf
41 * http://www.usb.org/developers/devclass_docs/usbmassbulk_10.pdf
42 * http://www.usb.org/developers/devclass_docs/usb_msc_cbi_1.1.pdf
43 * http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf
47 * Ported to NetBSD by Lennart Augustsson <augustss@NetBSD.org>.
48 * Parts of the code written by Jason R. Thorpe <thorpej@shagadelic.org>.
52 * The driver handles 3 Wire Protocols
53 * - Command/Bulk/Interrupt (CBI)
54 * - Command/Bulk/Interrupt with Command Completion Interrupt (CBI with CCI)
55 * - Mass Storage Bulk-Only (BBB)
56 * (BBB refers Bulk/Bulk/Bulk for Command/Data/Status phases)
58 * Over these wire protocols it handles the following command protocols
60 * - UFI (floppy command set)
63 * UFI and 8070i (ATAPI) are transformed versions of the SCSI command set. The
64 * sc->sc_transform method is used to convert the commands into the appropriate
65 * format (if at all necessary). For example, UFI requires all commands to be
66 * 12 bytes in length amongst other things.
68 * The source code below is marked and can be split into a number of pieces
71 * - probe/attach/detach
72 * - generic transfer routines
75 * - CBI_I (in addition to functions from CBI)
76 * - CAM (Common Access Method)
81 * The protocols are implemented using a state machine, for the transfers as
82 * well as for the resets. The state machine is contained in umass_t_*_callback.
83 * The state machine is started through either umass_command_start() or
86 * The reason for doing this is a) CAM performs a lot better this way and b) it
87 * avoids using tsleep from interrupt context (for example after a failed
92 * The SCSI related part of this driver has been derived from the
93 * dev/ppbus/vpo.c driver, by Nicolas Souchu (nsouch@FreeBSD.org).
95 * The CAM layer uses so called actions which are messages sent to the host
96 * adapter for completion. The actions come in through umass_cam_action. The
97 * appropriate block of routines is called depending on the transport protocol
98 * in use. When the transfer has finished, these routines call
99 * umass_cam_cb again to complete the CAM command.
102 #include <sys/stdint.h>
103 #include <sys/param.h>
104 #include <sys/queue.h>
105 #include <sys/types.h>
106 #include <sys/systm.h>
107 #include <sys/kernel.h>
109 #include <sys/module.h>
110 #include <sys/lock.h>
111 #include <sys/condvar.h>
112 #include <sys/sysctl.h>
113 #include <sys/unistd.h>
114 #include <sys/callout.h>
115 #include <sys/malloc.h>
116 #include <sys/priv.h>
118 #include <bus/u4b/usb.h>
119 #include <bus/u4b/usbdi.h>
120 #include <bus/u4b/usbdi_util.h>
121 #include <bus/u4b/usbdevs.h>
123 #include <bus/u4b/quirk/usb_quirk.h>
125 #include <bus/cam/cam.h>
126 #include <bus/cam/cam_ccb.h>
127 #include <bus/cam/cam_sim.h>
128 #include <bus/cam/cam_xpt_sim.h>
129 #include <bus/cam/scsi/scsi_all.h>
130 #include <bus/cam/scsi/scsi_da.h>
132 #include <bus/cam/cam_periph.h>
134 #define UMASS_EXT_BUFFER
135 #ifdef UMASS_EXT_BUFFER
136 /* this enables loading of virtual buffers into DMA */
137 #define UMASS_USB_FLAGS .ext_buffer=1,
139 #define UMASS_USB_FLAGS
145 if (umass_debug & (m)) { x ; } \
148 #define DPRINTF(sc, m, fmt, ...) \
150 if (umass_debug & (m)) { \
151 kprintf("%s:%s: " fmt, \
152 (sc) ? (const char *)(sc)->sc_name : \
153 (const char *)"umassX", \
154 __func__ ,## __VA_ARGS__); \
158 #define UDMASS_GEN 0x00010000 /* general */
159 #define UDMASS_SCSI 0x00020000 /* scsi */
160 #define UDMASS_UFI 0x00040000 /* ufi command set */
161 #define UDMASS_ATAPI 0x00080000 /* 8070i command set */
162 #define UDMASS_CMD (UDMASS_SCSI|UDMASS_UFI|UDMASS_ATAPI)
163 #define UDMASS_USB 0x00100000 /* USB general */
164 #define UDMASS_BBB 0x00200000 /* Bulk-Only transfers */
165 #define UDMASS_CBI 0x00400000 /* CBI transfers */
166 #define UDMASS_WIRE (UDMASS_BBB|UDMASS_CBI)
167 #define UDMASS_ALL 0xffff0000 /* all of the above */
168 static int umass_debug
= 0;
170 static SYSCTL_NODE(_hw_usb
, OID_AUTO
, umass
, CTLFLAG_RW
, 0, "USB umass");
171 SYSCTL_INT(_hw_usb_umass
, OID_AUTO
, debug
, CTLFLAG_RW
,
172 &umass_debug
, 0, "umass debug level");
174 TUNABLE_INT("hw.usb.umass.debug", &umass_debug
);
176 #define DIF(...) do { } while (0)
177 #define DPRINTF(...) do { } while (0)
180 #define UMASS_GONE ((struct umass_softc *)1)
182 #define UMASS_BULK_SIZE (1 << 17)
183 #define UMASS_CBI_DIAGNOSTIC_CMDLEN 12 /* bytes */
184 #define UMASS_MAX_CMDLEN MAX(12, CAM_MAX_CDBLEN) /* bytes */
186 /* USB transfer definitions */
188 #define UMASS_T_BBB_RESET1 0 /* Bulk-Only */
189 #define UMASS_T_BBB_RESET2 1
190 #define UMASS_T_BBB_RESET3 2
191 #define UMASS_T_BBB_COMMAND 3
192 #define UMASS_T_BBB_DATA_READ 4
193 #define UMASS_T_BBB_DATA_RD_CS 5
194 #define UMASS_T_BBB_DATA_WRITE 6
195 #define UMASS_T_BBB_DATA_WR_CS 7
196 #define UMASS_T_BBB_STATUS 8
197 #define UMASS_T_BBB_MAX 9
199 #define UMASS_T_CBI_RESET1 0 /* CBI */
200 #define UMASS_T_CBI_RESET2 1
201 #define UMASS_T_CBI_RESET3 2
202 #define UMASS_T_CBI_COMMAND 3
203 #define UMASS_T_CBI_DATA_READ 4
204 #define UMASS_T_CBI_DATA_RD_CS 5
205 #define UMASS_T_CBI_DATA_WRITE 6
206 #define UMASS_T_CBI_DATA_WR_CS 7
207 #define UMASS_T_CBI_STATUS 8
208 #define UMASS_T_CBI_RESET4 9
209 #define UMASS_T_CBI_MAX 10
211 #define UMASS_T_MAX MAX(UMASS_T_CBI_MAX, UMASS_T_BBB_MAX)
213 /* Generic definitions */
215 /* Direction for transfer */
221 #define DEVNAME "umass"
222 #define DEVNAME_SIM "umass-sim"
224 /* Approximate maximum transfer speeds (assumes 33% overhead). */
225 #define UMASS_FULL_TRANSFER_SPEED 1000
226 #define UMASS_HIGH_TRANSFER_SPEED 40000
227 #define UMASS_SUPER_TRANSFER_SPEED 400000
228 #define UMASS_FLOPPY_TRANSFER_SPEED 20
230 #define UMASS_TIMEOUT 5000 /* ms */
232 /* CAM specific definitions */
234 #define UMASS_SCSIID_MAX 1 /* maximum number of drives expected */
235 #define UMASS_SCSIID_HOST UMASS_SCSIID_MAX
237 /* Bulk-Only features */
239 #define UR_BBB_RESET 0xff /* Bulk-Only reset */
240 #define UR_BBB_GET_MAX_LUN 0xfe /* Get maximum lun */
242 /* Command Block Wrapper */
244 uDWord dCBWSignature
;
245 #define CBWSIGNATURE 0x43425355
247 uDWord dCBWDataTransferLength
;
249 #define CBWFLAGS_OUT 0x00
250 #define CBWFLAGS_IN 0x80
253 #define CBWCDBLENGTH 16
254 uByte CBWCDB
[CBWCDBLENGTH
];
255 } __packed umass_bbb_cbw_t
;
257 #define UMASS_BBB_CBW_SIZE 31
259 /* Command Status Wrapper */
261 uDWord dCSWSignature
;
262 #define CSWSIGNATURE 0x53425355
263 #define CSWSIGNATURE_IMAGINATION_DBX1 0x43425355
264 #define CSWSIGNATURE_OLYMPUS_C1 0x55425355
266 uDWord dCSWDataResidue
;
268 #define CSWSTATUS_GOOD 0x0
269 #define CSWSTATUS_FAILED 0x1
270 #define CSWSTATUS_PHASE 0x2
271 } __packed umass_bbb_csw_t
;
273 #define UMASS_BBB_CSW_SIZE 13
277 #define UR_CBI_ADSC 0x00
282 #define IDB_TYPE_CCI 0x00
284 #define IDB_VALUE_PASS 0x00
285 #define IDB_VALUE_FAIL 0x01
286 #define IDB_VALUE_PHASE 0x02
287 #define IDB_VALUE_PERSISTENT 0x03
288 #define IDB_VALUE_STATUS_MASK 0x03
295 } __packed umass_cbi_sbl_t
;
297 struct umass_softc
; /* see below */
299 typedef void (umass_callback_t
)(struct umass_softc
*sc
, union ccb
*ccb
,
300 uint32_t residue
, uint8_t status
);
302 #define STATUS_CMD_OK 0 /* everything ok */
303 #define STATUS_CMD_UNKNOWN 1 /* will have to fetch sense */
304 #define STATUS_CMD_FAILED 2 /* transfer was ok, command failed */
305 #define STATUS_WIRE_FAILED 3 /* couldn't even get command across */
307 typedef uint8_t (umass_transform_t
)(struct umass_softc
*sc
, uint8_t *cmd_ptr
,
310 /* Wire and command protocol */
311 #define UMASS_PROTO_BBB 0x0001 /* USB wire protocol */
312 #define UMASS_PROTO_CBI 0x0002
313 #define UMASS_PROTO_CBI_I 0x0004
314 #define UMASS_PROTO_WIRE 0x00ff /* USB wire protocol mask */
315 #define UMASS_PROTO_SCSI 0x0100 /* command protocol */
316 #define UMASS_PROTO_ATAPI 0x0200
317 #define UMASS_PROTO_UFI 0x0400
318 #define UMASS_PROTO_RBC 0x0800
319 #define UMASS_PROTO_COMMAND 0xff00 /* command protocol mask */
321 /* Device specific quirks */
322 #define NO_QUIRKS 0x0000
324 * The drive does not support Test Unit Ready. Convert to Start Unit
326 #define NO_TEST_UNIT_READY 0x0001
328 * The drive does not reset the Unit Attention state after REQUEST
329 * SENSE has been sent. The INQUIRY command does not reset the UA
330 * either, and so CAM runs in circles trying to retrieve the initial
333 #define RS_NO_CLEAR_UA 0x0002
334 /* The drive does not support START STOP. */
335 #define NO_START_STOP 0x0004
336 /* Don't ask for full inquiry data (255b). */
337 #define FORCE_SHORT_INQUIRY 0x0008
338 /* Needs to be initialised the Shuttle way */
339 #define SHUTTLE_INIT 0x0010
340 /* Drive needs to be switched to alternate iface 1 */
341 #define ALT_IFACE_1 0x0020
342 /* Drive does not do 1Mb/s, but just floppy speeds (20kb/s) */
343 #define FLOPPY_SPEED 0x0040
344 /* The device can't count and gets the residue of transfers wrong */
345 #define IGNORE_RESIDUE 0x0080
346 /* No GetMaxLun call */
347 #define NO_GETMAXLUN 0x0100
348 /* The device uses a weird CSWSIGNATURE. */
349 #define WRONG_CSWSIG 0x0200
350 /* Device cannot handle INQUIRY so fake a generic response */
351 #define NO_INQUIRY 0x0400
352 /* Device cannot handle INQUIRY EVPD, return CHECK CONDITION */
353 #define NO_INQUIRY_EVPD 0x0800
354 /* Pad all RBC requests to 12 bytes. */
355 #define RBC_PAD_TO_12 0x1000
357 * Device reports number of sectors from READ_CAPACITY, not max
360 #define READ_CAPACITY_OFFBY1 0x2000
362 * Device cannot handle a SCSI synchronize cache command. Normally
363 * this quirk would be handled in the cam layer, but for IDE bridges
364 * we need to associate the quirk with the bridge and not the
365 * underlying disk device. This is handled by faking a success
368 #define NO_SYNCHRONIZE_CACHE 0x4000
372 struct scsi_sense cam_scsi_sense
;
373 struct scsi_test_unit_ready cam_scsi_test_unit_ready
;
378 umass_callback_t
*callback
;
380 uint32_t data_len
; /* bytes */
381 uint32_t data_rem
; /* bytes */
382 uint32_t data_timeout
; /* ms */
383 uint32_t actlen
; /* bytes */
385 uint8_t cmd_data
[UMASS_MAX_CMDLEN
];
386 uint8_t cmd_len
; /* bytes */
391 /* Bulk specific variables for transfers in progress */
392 umass_bbb_cbw_t cbw
; /* command block wrapper */
393 umass_bbb_csw_t csw
; /* command status wrapper */
395 /* CBI specific variables for transfers in progress */
396 umass_cbi_sbl_t sbl
; /* status block */
399 struct usb_device
*sc_udev
;
400 struct cam_sim
*sc_sim
; /* SCSI Interface Module */
401 struct usb_xfer
*sc_xfer
[UMASS_T_MAX
];
404 * The command transform function is used to convert the SCSI
405 * commands into their derivatives, like UFI, ATAPI, and friends.
407 umass_transform_t
*sc_transform
;
410 uint32_t sc_quirks
; /* they got it almost right */
411 uint32_t sc_proto
; /* wire and cmd protocol */
414 uint8_t sc_iface_no
; /* interface number */
415 uint8_t sc_maxlun
; /* maximum LUN number, inclusive */
416 uint8_t sc_last_xfer_index
;
417 uint8_t sc_status_try
;
420 struct usb_callout sc_rescan_timeout
;
423 struct umass_probe_proto
{
432 static device_probe_t umass_probe
;
433 static device_attach_t umass_attach
;
434 static device_detach_t umass_detach
;
436 static usb_callback_t umass_tr_error
;
437 static usb_callback_t umass_t_bbb_reset1_callback
;
438 static usb_callback_t umass_t_bbb_reset2_callback
;
439 static usb_callback_t umass_t_bbb_reset3_callback
;
440 static usb_callback_t umass_t_bbb_command_callback
;
441 static usb_callback_t umass_t_bbb_data_read_callback
;
442 static usb_callback_t umass_t_bbb_data_rd_cs_callback
;
443 static usb_callback_t umass_t_bbb_data_write_callback
;
444 static usb_callback_t umass_t_bbb_data_wr_cs_callback
;
445 static usb_callback_t umass_t_bbb_status_callback
;
446 static usb_callback_t umass_t_cbi_reset1_callback
;
447 static usb_callback_t umass_t_cbi_reset2_callback
;
448 static usb_callback_t umass_t_cbi_reset3_callback
;
449 static usb_callback_t umass_t_cbi_reset4_callback
;
450 static usb_callback_t umass_t_cbi_command_callback
;
451 static usb_callback_t umass_t_cbi_data_read_callback
;
452 static usb_callback_t umass_t_cbi_data_rd_cs_callback
;
453 static usb_callback_t umass_t_cbi_data_write_callback
;
454 static usb_callback_t umass_t_cbi_data_wr_cs_callback
;
455 static usb_callback_t umass_t_cbi_status_callback
;
457 static void umass_cancel_ccb(struct umass_softc
*);
458 static void umass_init_shuttle(struct umass_softc
*);
459 static void umass_reset(struct umass_softc
*);
460 static void umass_t_bbb_data_clear_stall_callback(struct usb_xfer
*,
461 uint8_t, uint8_t, usb_error_t
);
462 static void umass_command_start(struct umass_softc
*, uint8_t, void *,
463 uint32_t, uint32_t, umass_callback_t
*, union ccb
*);
464 static uint8_t umass_bbb_get_max_lun(struct umass_softc
*);
465 static void umass_cbi_start_status(struct umass_softc
*);
466 static void umass_t_cbi_data_clear_stall_callback(struct usb_xfer
*,
467 uint8_t, uint8_t, usb_error_t
);
468 static int umass_cam_attach_sim(struct umass_softc
*);
469 static void umass_cam_attach(struct umass_softc
*);
470 static void umass_cam_detach_sim(struct umass_softc
*);
471 static void umass_cam_action(struct cam_sim
*, union ccb
*);
472 static void umass_cam_poll(struct cam_sim
*);
473 static void umass_cam_cb(struct umass_softc
*, union ccb
*, uint32_t,
475 static void umass_cam_sense_cb(struct umass_softc
*, union ccb
*, uint32_t,
477 static void umass_cam_quirk_cb(struct umass_softc
*, union ccb
*, uint32_t,
479 static uint8_t umass_scsi_transform(struct umass_softc
*, uint8_t *, uint8_t);
480 static uint8_t umass_rbc_transform(struct umass_softc
*, uint8_t *, uint8_t);
481 static uint8_t umass_ufi_transform(struct umass_softc
*, uint8_t *, uint8_t);
482 static uint8_t umass_atapi_transform(struct umass_softc
*, uint8_t *,
484 static uint8_t umass_no_transform(struct umass_softc
*, uint8_t *, uint8_t);
485 static uint8_t umass_std_transform(struct umass_softc
*, union ccb
*, uint8_t
489 static void umass_bbb_dump_cbw(struct umass_softc
*, umass_bbb_cbw_t
*);
490 static void umass_bbb_dump_csw(struct umass_softc
*, umass_bbb_csw_t
*);
491 static void umass_cbi_dump_cmd(struct umass_softc
*, void *, uint8_t);
492 static void umass_dump_buffer(struct umass_softc
*, uint8_t *, uint32_t,
496 static struct usb_config umass_bbb_config
[UMASS_T_BBB_MAX
] = {
498 [UMASS_T_BBB_RESET1
] = {
500 .endpoint
= 0x00, /* Control pipe */
501 .direction
= UE_DIR_ANY
,
502 .bufsize
= sizeof(struct usb_device_request
),
503 .callback
= &umass_t_bbb_reset1_callback
,
504 .timeout
= 5000, /* 5 seconds */
505 .interval
= 500, /* 500 milliseconds */
508 [UMASS_T_BBB_RESET2
] = {
510 .endpoint
= 0x00, /* Control pipe */
511 .direction
= UE_DIR_ANY
,
512 .bufsize
= sizeof(struct usb_device_request
),
513 .callback
= &umass_t_bbb_reset2_callback
,
514 .timeout
= 5000, /* 5 seconds */
515 .interval
= 50, /* 50 milliseconds */
518 [UMASS_T_BBB_RESET3
] = {
520 .endpoint
= 0x00, /* Control pipe */
521 .direction
= UE_DIR_ANY
,
522 .bufsize
= sizeof(struct usb_device_request
),
523 .callback
= &umass_t_bbb_reset3_callback
,
524 .timeout
= 5000, /* 5 seconds */
525 .interval
= 50, /* 50 milliseconds */
528 [UMASS_T_BBB_COMMAND
] = {
530 .endpoint
= UE_ADDR_ANY
,
531 .direction
= UE_DIR_OUT
,
532 .bufsize
= sizeof(umass_bbb_cbw_t
),
533 .callback
= &umass_t_bbb_command_callback
,
534 .timeout
= 5000, /* 5 seconds */
537 [UMASS_T_BBB_DATA_READ
] = {
539 .endpoint
= UE_ADDR_ANY
,
540 .direction
= UE_DIR_IN
,
541 .bufsize
= UMASS_BULK_SIZE
,
542 .flags
= {.proxy_buffer
= 1,.short_xfer_ok
= 1, UMASS_USB_FLAGS
},
543 .callback
= &umass_t_bbb_data_read_callback
,
544 .timeout
= 0, /* overwritten later */
547 [UMASS_T_BBB_DATA_RD_CS
] = {
549 .endpoint
= 0x00, /* Control pipe */
550 .direction
= UE_DIR_ANY
,
551 .bufsize
= sizeof(struct usb_device_request
),
552 .callback
= &umass_t_bbb_data_rd_cs_callback
,
553 .timeout
= 5000, /* 5 seconds */
556 [UMASS_T_BBB_DATA_WRITE
] = {
558 .endpoint
= UE_ADDR_ANY
,
559 .direction
= UE_DIR_OUT
,
560 .bufsize
= UMASS_BULK_SIZE
,
561 .flags
= {.proxy_buffer
= 1,.short_xfer_ok
= 1, UMASS_USB_FLAGS
},
562 .callback
= &umass_t_bbb_data_write_callback
,
563 .timeout
= 0, /* overwritten later */
566 [UMASS_T_BBB_DATA_WR_CS
] = {
568 .endpoint
= 0x00, /* Control pipe */
569 .direction
= UE_DIR_ANY
,
570 .bufsize
= sizeof(struct usb_device_request
),
571 .callback
= &umass_t_bbb_data_wr_cs_callback
,
572 .timeout
= 5000, /* 5 seconds */
575 [UMASS_T_BBB_STATUS
] = {
577 .endpoint
= UE_ADDR_ANY
,
578 .direction
= UE_DIR_IN
,
579 .bufsize
= sizeof(umass_bbb_csw_t
),
580 .flags
= {.short_xfer_ok
= 1,},
581 .callback
= &umass_t_bbb_status_callback
,
582 .timeout
= 5000, /* ms */
586 static struct usb_config umass_cbi_config
[UMASS_T_CBI_MAX
] = {
588 [UMASS_T_CBI_RESET1
] = {
590 .endpoint
= 0x00, /* Control pipe */
591 .direction
= UE_DIR_ANY
,
592 .bufsize
= (sizeof(struct usb_device_request
) +
593 UMASS_CBI_DIAGNOSTIC_CMDLEN
),
594 .callback
= &umass_t_cbi_reset1_callback
,
595 .timeout
= 5000, /* 5 seconds */
596 .interval
= 500, /* 500 milliseconds */
599 [UMASS_T_CBI_RESET2
] = {
601 .endpoint
= 0x00, /* Control pipe */
602 .direction
= UE_DIR_ANY
,
603 .bufsize
= sizeof(struct usb_device_request
),
604 .callback
= &umass_t_cbi_reset2_callback
,
605 .timeout
= 5000, /* 5 seconds */
606 .interval
= 50, /* 50 milliseconds */
609 [UMASS_T_CBI_RESET3
] = {
611 .endpoint
= 0x00, /* Control pipe */
612 .direction
= UE_DIR_ANY
,
613 .bufsize
= sizeof(struct usb_device_request
),
614 .callback
= &umass_t_cbi_reset3_callback
,
615 .timeout
= 5000, /* 5 seconds */
616 .interval
= 50, /* 50 milliseconds */
619 [UMASS_T_CBI_COMMAND
] = {
621 .endpoint
= 0x00, /* Control pipe */
622 .direction
= UE_DIR_ANY
,
623 .bufsize
= (sizeof(struct usb_device_request
) +
625 .callback
= &umass_t_cbi_command_callback
,
626 .timeout
= 5000, /* 5 seconds */
629 [UMASS_T_CBI_DATA_READ
] = {
631 .endpoint
= UE_ADDR_ANY
,
632 .direction
= UE_DIR_IN
,
633 .bufsize
= UMASS_BULK_SIZE
,
634 .flags
= {.proxy_buffer
= 1,.short_xfer_ok
= 1, UMASS_USB_FLAGS
},
635 .callback
= &umass_t_cbi_data_read_callback
,
636 .timeout
= 0, /* overwritten later */
639 [UMASS_T_CBI_DATA_RD_CS
] = {
641 .endpoint
= 0x00, /* Control pipe */
642 .direction
= UE_DIR_ANY
,
643 .bufsize
= sizeof(struct usb_device_request
),
644 .callback
= &umass_t_cbi_data_rd_cs_callback
,
645 .timeout
= 5000, /* 5 seconds */
648 [UMASS_T_CBI_DATA_WRITE
] = {
650 .endpoint
= UE_ADDR_ANY
,
651 .direction
= UE_DIR_OUT
,
652 .bufsize
= UMASS_BULK_SIZE
,
653 .flags
= {.proxy_buffer
= 1,.short_xfer_ok
= 1, UMASS_USB_FLAGS
},
654 .callback
= &umass_t_cbi_data_write_callback
,
655 .timeout
= 0, /* overwritten later */
658 [UMASS_T_CBI_DATA_WR_CS
] = {
660 .endpoint
= 0x00, /* Control pipe */
661 .direction
= UE_DIR_ANY
,
662 .bufsize
= sizeof(struct usb_device_request
),
663 .callback
= &umass_t_cbi_data_wr_cs_callback
,
664 .timeout
= 5000, /* 5 seconds */
667 [UMASS_T_CBI_STATUS
] = {
668 .type
= UE_INTERRUPT
,
669 .endpoint
= UE_ADDR_ANY
,
670 .direction
= UE_DIR_IN
,
671 .flags
= {.short_xfer_ok
= 1,.no_pipe_ok
= 1,},
672 .bufsize
= sizeof(umass_cbi_sbl_t
),
673 .callback
= &umass_t_cbi_status_callback
,
674 .timeout
= 5000, /* ms */
677 [UMASS_T_CBI_RESET4
] = {
679 .endpoint
= 0x00, /* Control pipe */
680 .direction
= UE_DIR_ANY
,
681 .bufsize
= sizeof(struct usb_device_request
),
682 .callback
= &umass_t_cbi_reset4_callback
,
683 .timeout
= 5000, /* ms */
687 /* If device cannot return valid inquiry data, fake it */
688 static const uint8_t fake_inq_data
[SHORT_INQUIRY_LENGTH
] = {
689 0, /* removable */ 0x80, SCSI_REV_2
, SCSI_REV_2
,
690 /* additional_length */ 31, 0, 0, 0
693 #define UFI_COMMAND_LENGTH 12 /* UFI commands are always 12 bytes */
694 #define ATAPI_COMMAND_LENGTH 12 /* ATAPI commands are always 12 bytes */
696 static devclass_t umass_devclass
;
698 static device_method_t umass_methods
[] = {
699 /* Device interface */
700 DEVMETHOD(device_probe
, umass_probe
),
701 DEVMETHOD(device_attach
, umass_attach
),
702 DEVMETHOD(device_detach
, umass_detach
),
706 static driver_t umass_driver
= {
708 .methods
= umass_methods
,
709 .size
= sizeof(struct umass_softc
),
712 DRIVER_MODULE(umass
, uhub
, umass_driver
, umass_devclass
, NULL
, NULL
);
713 MODULE_DEPEND(umass
, usb
, 1, 1, 1);
714 MODULE_DEPEND(umass
, cam
, 1, 1, 1);
715 MODULE_VERSION(umass
, 1);
718 * USB device probe/attach/detach
721 static const STRUCT_USB_HOST_ID __used umass_devs
[] = {
722 /* generic mass storage class */
723 {USB_IFACE_CLASS(UICLASS_MASS
),},
727 umass_get_proto(struct usb_interface
*iface
)
729 struct usb_interface_descriptor
*id
;
734 /* Check for a standards compliant device */
735 id
= usbd_get_interface_descriptor(iface
);
737 (id
->bInterfaceClass
!= UICLASS_MASS
)) {
740 switch (id
->bInterfaceSubClass
) {
741 case UISUBCLASS_SCSI
:
742 retval
|= UMASS_PROTO_SCSI
;
745 retval
|= UMASS_PROTO_UFI
;
748 retval
|= UMASS_PROTO_RBC
;
750 case UISUBCLASS_SFF8020I
:
751 case UISUBCLASS_SFF8070I
:
752 retval
|= UMASS_PROTO_ATAPI
;
758 switch (id
->bInterfaceProtocol
) {
759 case UIPROTO_MASS_CBI
:
760 retval
|= UMASS_PROTO_CBI
;
762 case UIPROTO_MASS_CBI_I
:
763 retval
|= UMASS_PROTO_CBI_I
;
765 case UIPROTO_MASS_BBB_OLD
:
766 case UIPROTO_MASS_BBB
:
767 retval
|= UMASS_PROTO_BBB
;
777 * Match the device we are seeing with the devices supported.
779 static struct umass_probe_proto
780 umass_probe_proto(device_t dev
, struct usb_attach_arg
*uaa
)
782 struct umass_probe_proto ret
;
783 uint32_t quirks
= NO_QUIRKS
;
784 uint32_t proto
= umass_get_proto(uaa
->iface
);
786 memset(&ret
, 0, sizeof(ret
));
787 ret
.error
= BUS_PROBE_GENERIC
;
789 /* Search for protocol enforcement */
791 if (usb_test_quirk(uaa
, UQ_MSC_FORCE_WIRE_BBB
)) {
792 proto
&= ~UMASS_PROTO_WIRE
;
793 proto
|= UMASS_PROTO_BBB
;
794 } else if (usb_test_quirk(uaa
, UQ_MSC_FORCE_WIRE_CBI
)) {
795 proto
&= ~UMASS_PROTO_WIRE
;
796 proto
|= UMASS_PROTO_CBI
;
797 } else if (usb_test_quirk(uaa
, UQ_MSC_FORCE_WIRE_CBI_I
)) {
798 proto
&= ~UMASS_PROTO_WIRE
;
799 proto
|= UMASS_PROTO_CBI_I
;
802 if (usb_test_quirk(uaa
, UQ_MSC_FORCE_PROTO_SCSI
)) {
803 proto
&= ~UMASS_PROTO_COMMAND
;
804 proto
|= UMASS_PROTO_SCSI
;
805 } else if (usb_test_quirk(uaa
, UQ_MSC_FORCE_PROTO_ATAPI
)) {
806 proto
&= ~UMASS_PROTO_COMMAND
;
807 proto
|= UMASS_PROTO_ATAPI
;
808 } else if (usb_test_quirk(uaa
, UQ_MSC_FORCE_PROTO_UFI
)) {
809 proto
&= ~UMASS_PROTO_COMMAND
;
810 proto
|= UMASS_PROTO_UFI
;
811 } else if (usb_test_quirk(uaa
, UQ_MSC_FORCE_PROTO_RBC
)) {
812 proto
&= ~UMASS_PROTO_COMMAND
;
813 proto
|= UMASS_PROTO_RBC
;
816 /* Check if the protocol is invalid */
818 if ((proto
& UMASS_PROTO_COMMAND
) == 0) {
823 if ((proto
& UMASS_PROTO_WIRE
) == 0) {
828 /* Search for quirks */
830 if (usb_test_quirk(uaa
, UQ_MSC_NO_TEST_UNIT_READY
))
831 quirks
|= NO_TEST_UNIT_READY
;
832 if (usb_test_quirk(uaa
, UQ_MSC_NO_RS_CLEAR_UA
))
833 quirks
|= RS_NO_CLEAR_UA
;
834 if (usb_test_quirk(uaa
, UQ_MSC_NO_START_STOP
))
835 quirks
|= NO_START_STOP
;
836 if (usb_test_quirk(uaa
, UQ_MSC_NO_GETMAXLUN
))
837 quirks
|= NO_GETMAXLUN
;
838 if (usb_test_quirk(uaa
, UQ_MSC_NO_INQUIRY
))
839 quirks
|= NO_INQUIRY
;
840 if (usb_test_quirk(uaa
, UQ_MSC_NO_INQUIRY_EVPD
))
841 quirks
|= NO_INQUIRY_EVPD
;
842 if (usb_test_quirk(uaa
, UQ_MSC_NO_SYNC_CACHE
))
843 quirks
|= NO_SYNCHRONIZE_CACHE
;
844 if (usb_test_quirk(uaa
, UQ_MSC_SHUTTLE_INIT
))
845 quirks
|= SHUTTLE_INIT
;
846 if (usb_test_quirk(uaa
, UQ_MSC_ALT_IFACE_1
))
847 quirks
|= ALT_IFACE_1
;
848 if (usb_test_quirk(uaa
, UQ_MSC_FLOPPY_SPEED
))
849 quirks
|= FLOPPY_SPEED
;
850 if (usb_test_quirk(uaa
, UQ_MSC_IGNORE_RESIDUE
))
851 quirks
|= IGNORE_RESIDUE
;
852 if (usb_test_quirk(uaa
, UQ_MSC_WRONG_CSWSIG
))
853 quirks
|= WRONG_CSWSIG
;
854 if (usb_test_quirk(uaa
, UQ_MSC_RBC_PAD_TO_12
))
855 quirks
|= RBC_PAD_TO_12
;
856 if (usb_test_quirk(uaa
, UQ_MSC_READ_CAP_OFFBY1
))
857 quirks
|= READ_CAPACITY_OFFBY1
;
858 if (usb_test_quirk(uaa
, UQ_MSC_FORCE_SHORT_INQ
))
859 quirks
|= FORCE_SHORT_INQUIRY
;
868 umass_probe(device_t dev
)
870 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
871 struct umass_probe_proto temp
;
873 if (uaa
->usb_mode
!= USB_MODE_HOST
) {
876 temp
= umass_probe_proto(dev
, uaa
);
882 umass_attach(device_t dev
)
884 struct umass_softc
*sc
= device_get_softc(dev
);
885 struct usb_attach_arg
*uaa
= device_get_ivars(dev
);
886 struct umass_probe_proto temp
= umass_probe_proto(dev
, uaa
);
887 struct usb_interface_descriptor
*id
;
891 * NOTE: the softc struct is cleared in device_set_driver.
892 * We can safely call umass_detach without specifically
893 * initializing the struct.
897 sc
->sc_udev
= uaa
->device
;
898 sc
->sc_proto
= temp
.proto
;
899 sc
->sc_quirks
= temp
.quirks
;
900 sc
->sc_unit
= device_get_unit(dev
);
902 ksnprintf(sc
->sc_name
, sizeof(sc
->sc_name
),
903 "%s", device_get_nameunit(dev
));
905 device_set_usb_desc(dev
);
907 lockinit(&sc
->sc_lock
, device_get_nameunit(dev
), 0, LK_CANRECURSE
);
909 /* get interface index */
911 id
= usbd_get_interface_descriptor(uaa
->iface
);
913 device_printf(dev
, "failed to get "
914 "interface number\n");
917 sc
->sc_iface_no
= id
->bInterfaceNumber
;
920 device_printf(dev
, " ");
922 switch (sc
->sc_proto
& UMASS_PROTO_COMMAND
) {
923 case UMASS_PROTO_SCSI
:
926 case UMASS_PROTO_ATAPI
:
927 kprintf("8070i (ATAPI)");
929 case UMASS_PROTO_UFI
:
932 case UMASS_PROTO_RBC
:
936 kprintf("(unknown 0x%02x)",
937 sc
->sc_proto
& UMASS_PROTO_COMMAND
);
943 switch (sc
->sc_proto
& UMASS_PROTO_WIRE
) {
944 case UMASS_PROTO_BBB
:
945 kprintf("Bulk-Only");
947 case UMASS_PROTO_CBI
: /* uses Comand/Bulk pipes */
950 case UMASS_PROTO_CBI_I
: /* uses Comand/Bulk/Interrupt pipes */
951 kprintf("CBI with CCI");
954 kprintf("(unknown 0x%02x)",
955 sc
->sc_proto
& UMASS_PROTO_WIRE
);
958 kprintf("; quirks = 0x%04x\n", sc
->sc_quirks
);
961 if (sc
->sc_quirks
& ALT_IFACE_1
) {
962 err
= usbd_set_alt_interface_index
963 (uaa
->device
, uaa
->info
.bIfaceIndex
, 1);
966 DPRINTF(sc
, UDMASS_USB
, "could not switch to "
967 "Alt Interface 1\n");
971 /* allocate all required USB transfers */
973 if (sc
->sc_proto
& UMASS_PROTO_BBB
) {
975 err
= usbd_transfer_setup(uaa
->device
,
976 &uaa
->info
.bIfaceIndex
, sc
->sc_xfer
, umass_bbb_config
,
977 UMASS_T_BBB_MAX
, sc
, &sc
->sc_lock
);
979 /* skip reset first time */
980 sc
->sc_last_xfer_index
= UMASS_T_BBB_COMMAND
;
982 } else if (sc
->sc_proto
& (UMASS_PROTO_CBI
| UMASS_PROTO_CBI_I
)) {
984 err
= usbd_transfer_setup(uaa
->device
,
985 &uaa
->info
.bIfaceIndex
, sc
->sc_xfer
, umass_cbi_config
,
986 UMASS_T_CBI_MAX
, sc
, &sc
->sc_lock
);
988 /* skip reset first time */
989 sc
->sc_last_xfer_index
= UMASS_T_CBI_COMMAND
;
996 device_printf(dev
, "could not setup required "
997 "transfers, %s\n", usbd_errstr(err
));
1001 (sc
->sc_proto
& UMASS_PROTO_SCSI
) ? &umass_scsi_transform
:
1002 (sc
->sc_proto
& UMASS_PROTO_UFI
) ? &umass_ufi_transform
:
1003 (sc
->sc_proto
& UMASS_PROTO_ATAPI
) ? &umass_atapi_transform
:
1004 (sc
->sc_proto
& UMASS_PROTO_RBC
) ? &umass_rbc_transform
:
1005 &umass_no_transform
;
1007 /* from here onwards the device can be used. */
1009 if (sc
->sc_quirks
& SHUTTLE_INIT
) {
1010 umass_init_shuttle(sc
);
1012 /* get the maximum LUN supported by the device */
1014 if (((sc
->sc_proto
& UMASS_PROTO_WIRE
) == UMASS_PROTO_BBB
) &&
1015 !(sc
->sc_quirks
& NO_GETMAXLUN
))
1016 sc
->sc_maxlun
= umass_bbb_get_max_lun(sc
);
1020 /* Prepare the SCSI command block */
1021 sc
->cam_scsi_sense
.opcode
= REQUEST_SENSE
;
1022 sc
->cam_scsi_test_unit_ready
.opcode
= TEST_UNIT_READY
;
1024 /* register the SIM */
1025 err
= umass_cam_attach_sim(sc
);
1030 umass_cam_attach(sc
);
1032 DPRINTF(sc
, UDMASS_GEN
, "Attach finished\n");
1034 return (0); /* success */
1038 return (ENXIO
); /* failure */
1042 umass_detach(device_t dev
)
1044 struct umass_softc
*sc
= device_get_softc(dev
);
1046 DPRINTF(sc
, UDMASS_USB
, "\n");
1048 /* teardown our statemachine */
1050 usbd_transfer_unsetup(sc
->sc_xfer
, UMASS_T_MAX
);
1052 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
1053 umass_cam_detach_sim(sc
);
1055 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
1056 lockuninit(&sc
->sc_lock
);
1058 return (0); /* success */
1062 umass_init_shuttle(struct umass_softc
*sc
)
1064 struct usb_device_request req
;
1066 uint8_t status
[2] = {0, 0};
1069 * The Linux driver does this, but no one can tell us what the
1072 req
.bmRequestType
= UT_READ_VENDOR_DEVICE
;
1073 req
.bRequest
= 1; /* XXX unknown command */
1074 USETW(req
.wValue
, 0);
1075 req
.wIndex
[0] = sc
->sc_iface_no
;
1077 USETW(req
.wLength
, sizeof(status
));
1078 err
= usbd_do_request(sc
->sc_udev
, NULL
, &req
, &status
);
1080 DPRINTF(sc
, UDMASS_GEN
, "Shuttle init returned 0x%02x%02x\n",
1081 status
[0], status
[1]);
1085 * Generic functions to handle transfers
1089 umass_transfer_start(struct umass_softc
*sc
, uint8_t xfer_index
)
1091 DPRINTF(sc
, UDMASS_GEN
, "transfer index = "
1092 "%d\n", xfer_index
);
1094 if (sc
->sc_xfer
[xfer_index
]) {
1095 sc
->sc_last_xfer_index
= xfer_index
;
1096 usbd_transfer_start(sc
->sc_xfer
[xfer_index
]);
1098 umass_cancel_ccb(sc
);
1103 umass_reset(struct umass_softc
*sc
)
1105 DPRINTF(sc
, UDMASS_GEN
, "resetting device\n");
1108 * stop the last transfer, if not already stopped:
1110 usbd_transfer_stop(sc
->sc_xfer
[sc
->sc_last_xfer_index
]);
1111 umass_transfer_start(sc
, 0);
1115 umass_cancel_ccb(struct umass_softc
*sc
)
1120 KKASSERT(lockstatus(&sc
->sc_lock
, curthread
) != 0);
1123 ccb
= sc
->sc_transfer
.ccb
;
1124 sc
->sc_transfer
.ccb
= NULL
;
1125 sc
->sc_last_xfer_index
= 0;
1128 (sc
->sc_transfer
.callback
)
1129 (sc
, ccb
, (sc
->sc_transfer
.data_len
-
1130 sc
->sc_transfer
.actlen
), STATUS_WIRE_FAILED
);
1135 umass_tr_error(struct usb_xfer
*xfer
, usb_error_t error
)
1137 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1139 if (error
!= USB_ERR_CANCELLED
) {
1141 DPRINTF(sc
, UDMASS_GEN
, "transfer error, %s -> "
1142 "reset\n", usbd_errstr(error
));
1144 umass_cancel_ccb(sc
);
1148 * BBB protocol specific functions
1152 umass_t_bbb_reset1_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1154 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1155 struct usb_device_request req
;
1156 struct usb_page_cache
*pc
;
1158 switch (USB_GET_STATE(xfer
)) {
1159 case USB_ST_TRANSFERRED
:
1160 umass_transfer_start(sc
, UMASS_T_BBB_RESET2
);
1165 * Reset recovery (5.3.4 in Universal Serial Bus Mass Storage Class)
1167 * For Reset Recovery the host shall issue in the following order:
1168 * a) a Bulk-Only Mass Storage Reset
1169 * b) a Clear Feature HALT to the Bulk-In endpoint
1170 * c) a Clear Feature HALT to the Bulk-Out endpoint
1172 * This is done in 3 steps, using 3 transfers:
1173 * UMASS_T_BBB_RESET1
1174 * UMASS_T_BBB_RESET2
1175 * UMASS_T_BBB_RESET3
1178 DPRINTF(sc
, UDMASS_BBB
, "BBB reset!\n");
1180 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
1181 req
.bRequest
= UR_BBB_RESET
; /* bulk only reset */
1182 USETW(req
.wValue
, 0);
1183 req
.wIndex
[0] = sc
->sc_iface_no
;
1185 USETW(req
.wLength
, 0);
1187 pc
= usbd_xfer_get_frame(xfer
, 0);
1188 usbd_copy_in(pc
, 0, &req
, sizeof(req
));
1190 usbd_xfer_set_frame_len(xfer
, 0, sizeof(req
));
1191 usbd_xfer_set_frames(xfer
, 1);
1192 usbd_transfer_submit(xfer
);
1195 default: /* Error */
1196 umass_tr_error(xfer
, error
);
1203 umass_t_bbb_reset2_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1205 umass_t_bbb_data_clear_stall_callback(xfer
, UMASS_T_BBB_RESET3
,
1206 UMASS_T_BBB_DATA_READ
, error
);
1210 umass_t_bbb_reset3_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1212 umass_t_bbb_data_clear_stall_callback(xfer
, UMASS_T_BBB_COMMAND
,
1213 UMASS_T_BBB_DATA_WRITE
, error
);
1217 umass_t_bbb_data_clear_stall_callback(struct usb_xfer
*xfer
,
1218 uint8_t next_xfer
, uint8_t stall_xfer
, usb_error_t error
)
1220 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1222 switch (USB_GET_STATE(xfer
)) {
1223 case USB_ST_TRANSFERRED
:
1225 umass_transfer_start(sc
, next_xfer
);
1229 if (usbd_clear_stall_callback(xfer
, sc
->sc_xfer
[stall_xfer
])) {
1230 goto tr_transferred
;
1234 default: /* Error */
1235 umass_tr_error(xfer
, error
);
1242 umass_t_bbb_command_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1244 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1245 union ccb
*ccb
= sc
->sc_transfer
.ccb
;
1246 struct usb_page_cache
*pc
;
1249 switch (USB_GET_STATE(xfer
)) {
1250 case USB_ST_TRANSFERRED
:
1251 umass_transfer_start
1252 (sc
, ((sc
->sc_transfer
.dir
== DIR_IN
) ? UMASS_T_BBB_DATA_READ
:
1253 (sc
->sc_transfer
.dir
== DIR_OUT
) ? UMASS_T_BBB_DATA_WRITE
:
1254 UMASS_T_BBB_STATUS
));
1259 sc
->sc_status_try
= 0;
1264 * the initial value is not important,
1265 * as long as the values are unique:
1267 tag
= UGETDW(sc
->cbw
.dCBWTag
) + 1;
1269 USETDW(sc
->cbw
.dCBWSignature
, CBWSIGNATURE
);
1270 USETDW(sc
->cbw
.dCBWTag
, tag
);
1273 * dCBWDataTransferLength:
1274 * This field indicates the number of bytes of data that the host
1275 * intends to transfer on the IN or OUT Bulk endpoint(as indicated by
1276 * the Direction bit) during the execution of this command. If this
1277 * field is set to 0, the device will expect that no data will be
1278 * transferred IN or OUT during this command, regardless of the value
1279 * of the Direction bit defined in dCBWFlags.
1281 USETDW(sc
->cbw
.dCBWDataTransferLength
, sc
->sc_transfer
.data_len
);
1285 * The bits of the Flags field are defined as follows:
1287 * Bit 7 Direction - this bit shall be ignored if the
1288 * dCBWDataTransferLength field is zero.
1289 * 0 = data Out from host to device
1290 * 1 = data In from device to host
1292 sc
->cbw
.bCBWFlags
= ((sc
->sc_transfer
.dir
== DIR_IN
) ?
1293 CBWFLAGS_IN
: CBWFLAGS_OUT
);
1294 sc
->cbw
.bCBWLUN
= sc
->sc_transfer
.lun
;
1296 if (sc
->sc_transfer
.cmd_len
> sizeof(sc
->cbw
.CBWCDB
)) {
1297 sc
->sc_transfer
.cmd_len
= sizeof(sc
->cbw
.CBWCDB
);
1298 DPRINTF(sc
, UDMASS_BBB
, "Truncating long command!\n");
1300 sc
->cbw
.bCDBLength
= sc
->sc_transfer
.cmd_len
;
1302 memcpy(sc
->cbw
.CBWCDB
, sc
->sc_transfer
.cmd_data
,
1303 sc
->sc_transfer
.cmd_len
);
1305 memset(sc
->sc_transfer
.cmd_data
+
1306 sc
->sc_transfer
.cmd_len
, 0,
1307 sizeof(sc
->cbw
.CBWCDB
) -
1308 sc
->sc_transfer
.cmd_len
);
1310 DIF(UDMASS_BBB
, umass_bbb_dump_cbw(sc
, &sc
->cbw
));
1312 pc
= usbd_xfer_get_frame(xfer
, 0);
1313 usbd_copy_in(pc
, 0, &sc
->cbw
, sizeof(sc
->cbw
));
1314 usbd_xfer_set_frame_len(xfer
, 0, sizeof(sc
->cbw
));
1316 usbd_transfer_submit(xfer
);
1320 default: /* Error */
1321 umass_tr_error(xfer
, error
);
1328 umass_t_bbb_data_read_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1330 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1331 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
1332 #ifndef UMASS_EXT_BUFFER
1333 struct usb_page_cache
*pc
;
1337 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
1339 switch (USB_GET_STATE(xfer
)) {
1340 case USB_ST_TRANSFERRED
:
1341 #ifndef UMASS_EXT_BUFFER
1342 pc
= usbd_xfer_get_frame(xfer
, 0);
1343 usbd_copy_out(pc
, 0, sc
->sc_transfer
.data_ptr
, actlen
);
1345 sc
->sc_transfer
.data_rem
-= actlen
;
1346 sc
->sc_transfer
.data_ptr
+= actlen
;
1347 sc
->sc_transfer
.actlen
+= actlen
;
1349 if (actlen
< sumlen
) {
1350 /* short transfer */
1351 sc
->sc_transfer
.data_rem
= 0;
1354 DPRINTF(sc
, UDMASS_BBB
, "max_bulk=%d, data_rem=%d\n",
1355 max_bulk
, sc
->sc_transfer
.data_rem
);
1357 if (sc
->sc_transfer
.data_rem
== 0) {
1358 umass_transfer_start(sc
, UMASS_T_BBB_STATUS
);
1361 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
1362 max_bulk
= sc
->sc_transfer
.data_rem
;
1364 usbd_xfer_set_timeout(xfer
, sc
->sc_transfer
.data_timeout
);
1366 #ifdef UMASS_EXT_BUFFER
1367 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_transfer
.data_ptr
,
1370 usbd_xfer_set_frame_len(xfer
, 0, max_bulk
);
1372 usbd_transfer_submit(xfer
);
1375 default: /* Error */
1376 if (error
== USB_ERR_CANCELLED
) {
1377 umass_tr_error(xfer
, error
);
1379 umass_transfer_start(sc
, UMASS_T_BBB_DATA_RD_CS
);
1387 umass_t_bbb_data_rd_cs_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1389 umass_t_bbb_data_clear_stall_callback(xfer
, UMASS_T_BBB_STATUS
,
1390 UMASS_T_BBB_DATA_READ
, error
);
1394 umass_t_bbb_data_write_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1396 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1397 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
1398 #ifndef UMASS_EXT_BUFFER
1399 struct usb_page_cache
*pc
;
1403 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
1405 switch (USB_GET_STATE(xfer
)) {
1406 case USB_ST_TRANSFERRED
:
1407 sc
->sc_transfer
.data_rem
-= actlen
;
1408 sc
->sc_transfer
.data_ptr
+= actlen
;
1409 sc
->sc_transfer
.actlen
+= actlen
;
1411 if (actlen
< sumlen
) {
1412 /* short transfer */
1413 sc
->sc_transfer
.data_rem
= 0;
1416 DPRINTF(sc
, UDMASS_BBB
, "max_bulk=%d, data_rem=%d\n",
1417 max_bulk
, sc
->sc_transfer
.data_rem
);
1419 if (sc
->sc_transfer
.data_rem
== 0) {
1420 umass_transfer_start(sc
, UMASS_T_BBB_STATUS
);
1423 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
1424 max_bulk
= sc
->sc_transfer
.data_rem
;
1426 usbd_xfer_set_timeout(xfer
, sc
->sc_transfer
.data_timeout
);
1428 #ifdef UMASS_EXT_BUFFER
1429 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_transfer
.data_ptr
,
1432 pc
= usbd_xfer_get_frame(xfer
, 0);
1433 usbd_copy_in(pc
, 0, sc
->sc_transfer
.data_ptr
, max_bulk
);
1434 usbd_xfer_set_frame_len(xfer
, 0, max_bulk
);
1437 usbd_transfer_submit(xfer
);
1440 default: /* Error */
1441 if (error
== USB_ERR_CANCELLED
) {
1442 umass_tr_error(xfer
, error
);
1444 umass_transfer_start(sc
, UMASS_T_BBB_DATA_WR_CS
);
1452 umass_t_bbb_data_wr_cs_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1454 umass_t_bbb_data_clear_stall_callback(xfer
, UMASS_T_BBB_STATUS
,
1455 UMASS_T_BBB_DATA_WRITE
, error
);
1459 umass_t_bbb_status_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1461 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1462 union ccb
*ccb
= sc
->sc_transfer
.ccb
;
1463 struct usb_page_cache
*pc
;
1467 usbd_xfer_status(xfer
, &actlen
, NULL
, NULL
, NULL
);
1469 switch (USB_GET_STATE(xfer
)) {
1470 case USB_ST_TRANSFERRED
:
1473 * Do a full reset if there is something wrong with the CSW:
1475 sc
->sc_status_try
= 1;
1477 /* Zero missing parts of the CSW: */
1479 if (actlen
< sizeof(sc
->csw
))
1480 memset(&sc
->csw
, 0, sizeof(sc
->csw
));
1482 pc
= usbd_xfer_get_frame(xfer
, 0);
1483 usbd_copy_out(pc
, 0, &sc
->csw
, actlen
);
1485 DIF(UDMASS_BBB
, umass_bbb_dump_csw(sc
, &sc
->csw
));
1487 residue
= UGETDW(sc
->csw
.dCSWDataResidue
);
1489 if ((!residue
) || (sc
->sc_quirks
& IGNORE_RESIDUE
)) {
1490 residue
= (sc
->sc_transfer
.data_len
-
1491 sc
->sc_transfer
.actlen
);
1493 if (residue
> sc
->sc_transfer
.data_len
) {
1494 DPRINTF(sc
, UDMASS_BBB
, "truncating residue from %d "
1495 "to %d bytes\n", residue
, sc
->sc_transfer
.data_len
);
1496 residue
= sc
->sc_transfer
.data_len
;
1498 /* translate weird command-status signatures: */
1499 if (sc
->sc_quirks
& WRONG_CSWSIG
) {
1501 uint32_t temp
= UGETDW(sc
->csw
.dCSWSignature
);
1503 if ((temp
== CSWSIGNATURE_OLYMPUS_C1
) ||
1504 (temp
== CSWSIGNATURE_IMAGINATION_DBX1
)) {
1505 USETDW(sc
->csw
.dCSWSignature
, CSWSIGNATURE
);
1508 /* check CSW and handle eventual error */
1509 if (UGETDW(sc
->csw
.dCSWSignature
) != CSWSIGNATURE
) {
1510 DPRINTF(sc
, UDMASS_BBB
, "bad CSW signature 0x%08x != 0x%08x\n",
1511 UGETDW(sc
->csw
.dCSWSignature
), CSWSIGNATURE
);
1513 * Invalid CSW: Wrong signature or wrong tag might
1514 * indicate that we lost synchronization. Reset the
1518 } else if (UGETDW(sc
->csw
.dCSWTag
) != UGETDW(sc
->cbw
.dCBWTag
)) {
1519 DPRINTF(sc
, UDMASS_BBB
, "Invalid CSW: tag 0x%08x should be "
1520 "0x%08x\n", UGETDW(sc
->csw
.dCSWTag
),
1521 UGETDW(sc
->cbw
.dCBWTag
));
1523 } else if (sc
->csw
.bCSWStatus
> CSWSTATUS_PHASE
) {
1524 DPRINTF(sc
, UDMASS_BBB
, "Invalid CSW: status %d > %d\n",
1525 sc
->csw
.bCSWStatus
, CSWSTATUS_PHASE
);
1527 } else if (sc
->csw
.bCSWStatus
== CSWSTATUS_PHASE
) {
1528 DPRINTF(sc
, UDMASS_BBB
, "Phase error, residue = "
1531 } else if (sc
->sc_transfer
.actlen
> sc
->sc_transfer
.data_len
) {
1532 DPRINTF(sc
, UDMASS_BBB
, "Buffer overrun %d > %d\n",
1533 sc
->sc_transfer
.actlen
, sc
->sc_transfer
.data_len
);
1535 } else if (sc
->csw
.bCSWStatus
== CSWSTATUS_FAILED
) {
1536 DPRINTF(sc
, UDMASS_BBB
, "Command failed, residue = "
1539 sc
->sc_transfer
.ccb
= NULL
;
1541 sc
->sc_last_xfer_index
= UMASS_T_BBB_COMMAND
;
1543 (sc
->sc_transfer
.callback
)
1544 (sc
, ccb
, residue
, STATUS_CMD_FAILED
);
1546 sc
->sc_transfer
.ccb
= NULL
;
1548 sc
->sc_last_xfer_index
= UMASS_T_BBB_COMMAND
;
1550 (sc
->sc_transfer
.callback
)
1551 (sc
, ccb
, residue
, STATUS_CMD_OK
);
1556 usbd_xfer_set_frame_len(xfer
, 0, usbd_xfer_max_len(xfer
));
1557 usbd_transfer_submit(xfer
);
1562 DPRINTF(sc
, UDMASS_BBB
, "Failed to read CSW: %s, try %d\n",
1563 usbd_errstr(error
), sc
->sc_status_try
);
1565 if ((error
== USB_ERR_CANCELLED
) ||
1566 (sc
->sc_status_try
)) {
1567 umass_tr_error(xfer
, error
);
1569 sc
->sc_status_try
= 1;
1570 umass_transfer_start(sc
, UMASS_T_BBB_DATA_RD_CS
);
1578 umass_command_start(struct umass_softc
*sc
, uint8_t dir
,
1579 void *data_ptr
, uint32_t data_len
,
1580 uint32_t data_timeout
, umass_callback_t
*callback
,
1583 sc
->sc_transfer
.lun
= ccb
->ccb_h
.target_lun
;
1586 * NOTE: assumes that "sc->sc_transfer.cmd_data" and
1587 * "sc->sc_transfer.cmd_len" has been properly
1591 sc
->sc_transfer
.dir
= data_len
? dir
: DIR_NONE
;
1592 sc
->sc_transfer
.data_ptr
= data_ptr
;
1593 sc
->sc_transfer
.data_len
= data_len
;
1594 sc
->sc_transfer
.data_rem
= data_len
;
1595 sc
->sc_transfer
.data_timeout
= (data_timeout
+ UMASS_TIMEOUT
);
1597 sc
->sc_transfer
.actlen
= 0;
1598 sc
->sc_transfer
.callback
= callback
;
1599 sc
->sc_transfer
.ccb
= ccb
;
1601 if (sc
->sc_xfer
[sc
->sc_last_xfer_index
]) {
1602 usbd_transfer_start(sc
->sc_xfer
[sc
->sc_last_xfer_index
]);
1604 ccb
->ccb_h
.status
= CAM_TID_INVALID
;
1610 umass_bbb_get_max_lun(struct umass_softc
*sc
)
1612 struct usb_device_request req
;
1616 /* The Get Max Lun command is a class-specific request. */
1617 req
.bmRequestType
= UT_READ_CLASS_INTERFACE
;
1618 req
.bRequest
= UR_BBB_GET_MAX_LUN
;
1619 USETW(req
.wValue
, 0);
1620 req
.wIndex
[0] = sc
->sc_iface_no
;
1622 USETW(req
.wLength
, 1);
1624 err
= usbd_do_request(sc
->sc_udev
, NULL
, &req
, &buf
);
1628 /* Device doesn't support Get Max Lun request. */
1629 kprintf("%s: Get Max Lun not supported (%s)\n",
1630 sc
->sc_name
, usbd_errstr(err
));
1636 * Command/Bulk/Interrupt (CBI) specific functions
1640 umass_cbi_start_status(struct umass_softc
*sc
)
1642 if (sc
->sc_xfer
[UMASS_T_CBI_STATUS
]) {
1643 umass_transfer_start(sc
, UMASS_T_CBI_STATUS
);
1645 union ccb
*ccb
= sc
->sc_transfer
.ccb
;
1647 sc
->sc_transfer
.ccb
= NULL
;
1649 sc
->sc_last_xfer_index
= UMASS_T_CBI_COMMAND
;
1651 (sc
->sc_transfer
.callback
)
1652 (sc
, ccb
, (sc
->sc_transfer
.data_len
-
1653 sc
->sc_transfer
.actlen
), STATUS_CMD_UNKNOWN
);
1658 umass_t_cbi_reset1_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1660 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1661 struct usb_device_request req
;
1662 struct usb_page_cache
*pc
;
1663 uint8_t buf
[UMASS_CBI_DIAGNOSTIC_CMDLEN
];
1667 switch (USB_GET_STATE(xfer
)) {
1668 case USB_ST_TRANSFERRED
:
1669 umass_transfer_start(sc
, UMASS_T_CBI_RESET2
);
1674 * Command Block Reset Protocol
1676 * First send a reset request to the device. Then clear
1677 * any possibly stalled bulk endpoints.
1679 * This is done in 3 steps, using 3 transfers:
1680 * UMASS_T_CBI_RESET1
1681 * UMASS_T_CBI_RESET2
1682 * UMASS_T_CBI_RESET3
1683 * UMASS_T_CBI_RESET4 (only if there is an interrupt endpoint)
1686 DPRINTF(sc
, UDMASS_CBI
, "CBI reset!\n");
1688 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
1689 req
.bRequest
= UR_CBI_ADSC
;
1690 USETW(req
.wValue
, 0);
1691 req
.wIndex
[0] = sc
->sc_iface_no
;
1693 USETW(req
.wLength
, UMASS_CBI_DIAGNOSTIC_CMDLEN
);
1696 * The 0x1d code is the SEND DIAGNOSTIC command. To
1697 * distinguish between the two, the last 10 bytes of the CBL
1698 * is filled with 0xff (section 2.2 of the CBI
1701 buf
[0] = 0x1d; /* Command Block Reset */
1704 for (i
= 2; i
< UMASS_CBI_DIAGNOSTIC_CMDLEN
; i
++) {
1708 pc
= usbd_xfer_get_frame(xfer
, 0);
1709 usbd_copy_in(pc
, 0, &req
, sizeof(req
));
1710 pc
= usbd_xfer_get_frame(xfer
, 1);
1711 usbd_copy_in(pc
, 0, buf
, sizeof(buf
));
1713 usbd_xfer_set_frame_len(xfer
, 0, sizeof(req
));
1714 usbd_xfer_set_frame_len(xfer
, 1, sizeof(buf
));
1715 usbd_xfer_set_frames(xfer
, 2);
1716 usbd_transfer_submit(xfer
);
1719 default: /* Error */
1720 if (error
== USB_ERR_CANCELLED
)
1721 umass_tr_error(xfer
, error
);
1723 umass_transfer_start(sc
, UMASS_T_CBI_RESET2
);
1730 umass_t_cbi_reset2_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1732 umass_t_cbi_data_clear_stall_callback(xfer
, UMASS_T_CBI_RESET3
,
1733 UMASS_T_CBI_DATA_READ
, error
);
1737 umass_t_cbi_reset3_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1739 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1741 umass_t_cbi_data_clear_stall_callback
1742 (xfer
, (sc
->sc_xfer
[UMASS_T_CBI_RESET4
] &&
1743 sc
->sc_xfer
[UMASS_T_CBI_STATUS
]) ?
1744 UMASS_T_CBI_RESET4
: UMASS_T_CBI_COMMAND
,
1745 UMASS_T_CBI_DATA_WRITE
, error
);
1749 umass_t_cbi_reset4_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1751 umass_t_cbi_data_clear_stall_callback(xfer
, UMASS_T_CBI_COMMAND
,
1752 UMASS_T_CBI_STATUS
, error
);
1756 umass_t_cbi_data_clear_stall_callback(struct usb_xfer
*xfer
,
1757 uint8_t next_xfer
, uint8_t stall_xfer
, usb_error_t error
)
1759 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1761 switch (USB_GET_STATE(xfer
)) {
1762 case USB_ST_TRANSFERRED
:
1764 if (next_xfer
== UMASS_T_CBI_STATUS
) {
1765 umass_cbi_start_status(sc
);
1767 umass_transfer_start(sc
, next_xfer
);
1772 if (usbd_clear_stall_callback(xfer
, sc
->sc_xfer
[stall_xfer
])) {
1773 goto tr_transferred
; /* should not happen */
1777 default: /* Error */
1778 umass_tr_error(xfer
, error
);
1785 umass_t_cbi_command_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1787 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1788 union ccb
*ccb
= sc
->sc_transfer
.ccb
;
1789 struct usb_device_request req
;
1790 struct usb_page_cache
*pc
;
1792 switch (USB_GET_STATE(xfer
)) {
1793 case USB_ST_TRANSFERRED
:
1795 if (sc
->sc_transfer
.dir
== DIR_NONE
) {
1796 umass_cbi_start_status(sc
);
1798 umass_transfer_start
1799 (sc
, (sc
->sc_transfer
.dir
== DIR_IN
) ?
1800 UMASS_T_CBI_DATA_READ
: UMASS_T_CBI_DATA_WRITE
);
1809 * do a CBI transfer with cmd_len bytes from
1810 * cmd_data, possibly a data phase of data_len
1811 * bytes from/to the device and finally a status
1815 req
.bmRequestType
= UT_WRITE_CLASS_INTERFACE
;
1816 req
.bRequest
= UR_CBI_ADSC
;
1817 USETW(req
.wValue
, 0);
1818 req
.wIndex
[0] = sc
->sc_iface_no
;
1820 req
.wLength
[0] = sc
->sc_transfer
.cmd_len
;
1823 pc
= usbd_xfer_get_frame(xfer
, 0);
1824 usbd_copy_in(pc
, 0, &req
, sizeof(req
));
1825 pc
= usbd_xfer_get_frame(xfer
, 1);
1826 usbd_copy_in(pc
, 0, sc
->sc_transfer
.cmd_data
,
1827 sc
->sc_transfer
.cmd_len
);
1829 usbd_xfer_set_frame_len(xfer
, 0, sizeof(req
));
1830 usbd_xfer_set_frame_len(xfer
, 1, sc
->sc_transfer
.cmd_len
);
1831 usbd_xfer_set_frames(xfer
,
1832 sc
->sc_transfer
.cmd_len
? 2 : 1);
1835 umass_cbi_dump_cmd(sc
,
1836 sc
->sc_transfer
.cmd_data
,
1837 sc
->sc_transfer
.cmd_len
));
1839 usbd_transfer_submit(xfer
);
1843 default: /* Error */
1845 * STALL on the control pipe can be result of the command error.
1846 * Attempt to clear this STALL same as for bulk pipe also
1847 * results in command completion interrupt, but ASC/ASCQ there
1848 * look like not always valid, so don't bother about it.
1850 if ((error
== USB_ERR_STALLED
) ||
1851 (sc
->sc_transfer
.callback
== &umass_cam_cb
)) {
1852 sc
->sc_transfer
.ccb
= NULL
;
1853 (sc
->sc_transfer
.callback
)
1854 (sc
, ccb
, sc
->sc_transfer
.data_len
,
1855 STATUS_CMD_UNKNOWN
);
1857 umass_tr_error(xfer
, error
);
1859 sc
->sc_last_xfer_index
= UMASS_T_CBI_COMMAND
;
1866 umass_t_cbi_data_read_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1868 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1869 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
1870 #ifndef UMASS_EXT_BUFFER
1871 struct usb_page_cache
*pc
;
1875 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
1877 switch (USB_GET_STATE(xfer
)) {
1878 case USB_ST_TRANSFERRED
:
1879 #ifndef UMASS_EXT_BUFFER
1880 pc
= usbd_xfer_get_frame(xfer
, 0);
1881 usbd_copy_out(pc
, 0, sc
->sc_transfer
.data_ptr
, actlen
);
1883 sc
->sc_transfer
.data_rem
-= actlen
;
1884 sc
->sc_transfer
.data_ptr
+= actlen
;
1885 sc
->sc_transfer
.actlen
+= actlen
;
1887 if (actlen
< sumlen
) {
1888 /* short transfer */
1889 sc
->sc_transfer
.data_rem
= 0;
1892 DPRINTF(sc
, UDMASS_CBI
, "max_bulk=%d, data_rem=%d\n",
1893 max_bulk
, sc
->sc_transfer
.data_rem
);
1895 if (sc
->sc_transfer
.data_rem
== 0) {
1896 umass_cbi_start_status(sc
);
1899 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
1900 max_bulk
= sc
->sc_transfer
.data_rem
;
1902 usbd_xfer_set_timeout(xfer
, sc
->sc_transfer
.data_timeout
);
1904 #ifdef UMASS_EXT_BUFFER
1905 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_transfer
.data_ptr
,
1908 usbd_xfer_set_frame_len(xfer
, 0, max_bulk
);
1910 usbd_transfer_submit(xfer
);
1913 default: /* Error */
1914 if ((error
== USB_ERR_CANCELLED
) ||
1915 (sc
->sc_transfer
.callback
!= &umass_cam_cb
)) {
1916 umass_tr_error(xfer
, error
);
1918 umass_transfer_start(sc
, UMASS_T_CBI_DATA_RD_CS
);
1926 umass_t_cbi_data_rd_cs_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1928 umass_t_cbi_data_clear_stall_callback(xfer
, UMASS_T_CBI_STATUS
,
1929 UMASS_T_CBI_DATA_READ
, error
);
1933 umass_t_cbi_data_write_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1935 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
1936 uint32_t max_bulk
= usbd_xfer_max_len(xfer
);
1937 #ifndef UMASS_EXT_BUFFER
1938 struct usb_page_cache
*pc
;
1942 usbd_xfer_status(xfer
, &actlen
, &sumlen
, NULL
, NULL
);
1944 switch (USB_GET_STATE(xfer
)) {
1945 case USB_ST_TRANSFERRED
:
1946 sc
->sc_transfer
.data_rem
-= actlen
;
1947 sc
->sc_transfer
.data_ptr
+= actlen
;
1948 sc
->sc_transfer
.actlen
+= actlen
;
1950 if (actlen
< sumlen
) {
1951 /* short transfer */
1952 sc
->sc_transfer
.data_rem
= 0;
1955 DPRINTF(sc
, UDMASS_CBI
, "max_bulk=%d, data_rem=%d\n",
1956 max_bulk
, sc
->sc_transfer
.data_rem
);
1958 if (sc
->sc_transfer
.data_rem
== 0) {
1959 umass_cbi_start_status(sc
);
1962 if (max_bulk
> sc
->sc_transfer
.data_rem
) {
1963 max_bulk
= sc
->sc_transfer
.data_rem
;
1965 usbd_xfer_set_timeout(xfer
, sc
->sc_transfer
.data_timeout
);
1967 #ifdef UMASS_EXT_BUFFER
1968 usbd_xfer_set_frame_data(xfer
, 0, sc
->sc_transfer
.data_ptr
,
1971 pc
= usbd_xfer_get_frame(xfer
, 0);
1972 usbd_copy_in(pc
, 0, sc
->sc_transfer
.data_ptr
, max_bulk
);
1973 usbd_xfer_set_frame_len(xfer
, 0, max_bulk
);
1976 usbd_transfer_submit(xfer
);
1979 default: /* Error */
1980 if ((error
== USB_ERR_CANCELLED
) ||
1981 (sc
->sc_transfer
.callback
!= &umass_cam_cb
)) {
1982 umass_tr_error(xfer
, error
);
1984 umass_transfer_start(sc
, UMASS_T_CBI_DATA_WR_CS
);
1992 umass_t_cbi_data_wr_cs_callback(struct usb_xfer
*xfer
, usb_error_t error
)
1994 umass_t_cbi_data_clear_stall_callback(xfer
, UMASS_T_CBI_STATUS
,
1995 UMASS_T_CBI_DATA_WRITE
, error
);
1999 umass_t_cbi_status_callback(struct usb_xfer
*xfer
, usb_error_t error
)
2001 struct umass_softc
*sc
= usbd_xfer_softc(xfer
);
2002 union ccb
*ccb
= sc
->sc_transfer
.ccb
;
2003 struct usb_page_cache
*pc
;
2008 usbd_xfer_status(xfer
, &actlen
, NULL
, NULL
, NULL
);
2010 switch (USB_GET_STATE(xfer
)) {
2011 case USB_ST_TRANSFERRED
:
2013 if (actlen
< sizeof(sc
->sbl
)) {
2016 pc
= usbd_xfer_get_frame(xfer
, 0);
2017 usbd_copy_out(pc
, 0, &sc
->sbl
, sizeof(sc
->sbl
));
2019 residue
= (sc
->sc_transfer
.data_len
-
2020 sc
->sc_transfer
.actlen
);
2022 /* dissect the information in the buffer */
2024 if (sc
->sc_proto
& UMASS_PROTO_UFI
) {
2027 * Section 3.4.3.1.3 specifies that the UFI command
2028 * protocol returns an ASC and ASCQ in the interrupt
2032 DPRINTF(sc
, UDMASS_CBI
, "UFI CCI, ASC = 0x%02x, "
2033 "ASCQ = 0x%02x\n", sc
->sbl
.ufi
.asc
,
2036 status
= (((sc
->sbl
.ufi
.asc
== 0) &&
2037 (sc
->sbl
.ufi
.ascq
== 0)) ?
2038 STATUS_CMD_OK
: STATUS_CMD_FAILED
);
2040 sc
->sc_transfer
.ccb
= NULL
;
2042 sc
->sc_last_xfer_index
= UMASS_T_CBI_COMMAND
;
2044 (sc
->sc_transfer
.callback
)
2045 (sc
, ccb
, residue
, status
);
2051 /* Command Interrupt Data Block */
2053 DPRINTF(sc
, UDMASS_CBI
, "type=0x%02x, value=0x%02x\n",
2054 sc
->sbl
.common
.type
, sc
->sbl
.common
.value
);
2056 if (sc
->sbl
.common
.type
== IDB_TYPE_CCI
) {
2058 status
= (sc
->sbl
.common
.value
& IDB_VALUE_STATUS_MASK
);
2060 status
= ((status
== IDB_VALUE_PASS
) ? STATUS_CMD_OK
:
2061 (status
== IDB_VALUE_FAIL
) ? STATUS_CMD_FAILED
:
2062 (status
== IDB_VALUE_PERSISTENT
) ? STATUS_CMD_FAILED
:
2063 STATUS_WIRE_FAILED
);
2065 sc
->sc_transfer
.ccb
= NULL
;
2067 sc
->sc_last_xfer_index
= UMASS_T_CBI_COMMAND
;
2069 (sc
->sc_transfer
.callback
)
2070 (sc
, ccb
, residue
, status
);
2080 usbd_xfer_set_frame_len(xfer
, 0, usbd_xfer_max_len(xfer
));
2081 usbd_transfer_submit(xfer
);
2084 default: /* Error */
2085 DPRINTF(sc
, UDMASS_CBI
, "Failed to read CSW: %s\n",
2086 usbd_errstr(error
));
2087 umass_tr_error(xfer
, error
);
2094 * CAM specific functions (used by SCSI, UFI, 8070i (ATAPI))
2098 umass_cam_attach_sim(struct umass_softc
*sc
)
2100 struct cam_devq
*devq
; /* Per device Queue */
2103 * A HBA is attached to the CAM layer.
2105 * The CAM layer will then after a while start probing for devices on
2106 * the bus. The number of SIMs is limited to one.
2109 usb_callout_init_mtx(&sc
->sc_rescan_timeout
, &sc
->sc_lock
, 0);
2110 devq
= cam_simq_alloc(1 /* maximum openings */ );
2114 sc
->sc_sim
= cam_sim_alloc
2115 (umass_cam_action
, umass_cam_poll
,
2118 sc
->sc_unit
/* unit number */ ,
2119 &sc
->sc_lock
/* mutex */ ,
2120 1 /* maximum device openings */ ,
2121 0 /* maximum tagged device openings */ ,
2124 cam_simq_release(devq
);
2125 if (sc
->sc_sim
== NULL
) {
2129 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
2131 if (xpt_bus_register(sc
->sc_sim
, sc
->sc_unit
) != CAM_SUCCESS
) {
2132 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
2133 cam_sim_free(sc
->sc_sim
);
2138 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
2143 umass_cam_rescan_callback(struct cam_periph
*periph
, union ccb
*ccb
)
2146 if (ccb
->ccb_h
.status
!= CAM_REQ_CMP
) {
2147 kprintf("%s:%d Rescan failed, 0x%04x\n",
2148 periph
->periph_name
, periph
->unit_number
,
2151 kprintf("%s%d: Rescan succeeded\n",
2152 periph
->periph_name
, periph
->unit_number
);
2156 xpt_free_path(ccb
->ccb_h
.path
);
2157 kfree(ccb
, M_USBDEV
);
2161 * Rescan the SCSI bus to detect newly added devices. We use
2162 * an async rescan to avoid reentrancy issues.
2165 umass_cam_rescan(void *addr
)
2167 struct umass_softc
*sc
= (struct umass_softc
*) addr
;
2168 struct cam_path
*path
;
2171 ccb
= kmalloc(sizeof(union ccb
), M_USBDEV
, M_INTWAIT
|M_ZERO
);
2173 DPRINTF(sc
, UDMASS_SCSI
, "scbus%d: scanning for %s:%d:%d:%d\n",
2174 cam_sim_path(sc
->sc_sim
),
2175 device_get_nameunit(sc
->sc_dev
), cam_sim_path(sc
->sc_sim
),
2176 device_get_unit(sc
->sc_dev
), CAM_LUN_WILDCARD
);
2178 if (xpt_create_path(&path
, xpt_periph
, cam_sim_path(sc
->sc_sim
),
2179 CAM_TARGET_WILDCARD
, CAM_LUN_WILDCARD
) != CAM_REQ_CMP
)
2181 kfree(ccb
, M_USBDEV
);
2185 xpt_setup_ccb(&ccb
->ccb_h
, path
, 5/*priority (low)*/);
2186 ccb
->ccb_h
.func_code
= XPT_SCAN_BUS
;
2187 ccb
->ccb_h
.cbfcnp
= umass_cam_rescan_callback
;
2188 ccb
->crcn
.flags
= CAM_FLAG_NONE
;
2189 xpt_action_async(ccb
);
2191 /* The scan is in progress now. */
2196 umass_cam_attach(struct umass_softc
*sc
)
2201 kprintf("%s:%d:%d:%d: Attached to scbus%d\n",
2202 sc
->sc_name
, cam_sim_path(sc
->sc_sim
),
2203 sc
->sc_unit
, CAM_LUN_WILDCARD
,
2204 cam_sim_path(sc
->sc_sim
));
2208 * failure is benign, as the user can still do it by hand
2209 * (camcontrol rescan <busno>). Only do this if we are not
2210 * booting, because CAM does a scan after booting has
2211 * completed, when interrupts have been enabled.
2213 usb_callout_reset(&sc
->sc_rescan_timeout
, USB_MS_TO_TICKS(200),
2214 umass_cam_rescan
, sc
);
2219 * detach from the CAM layer
2223 umass_cam_detach_sim(struct umass_softc
*sc
)
2225 if (sc
->sc_sim
!= NULL
) {
2226 if (xpt_bus_deregister(cam_sim_path(sc
->sc_sim
))) {
2227 /* accessing the softc is not possible after this */
2228 sc
->sc_sim
->softc
= UMASS_GONE
;
2229 cam_sim_free(sc
->sc_sim
);
2231 panic("%s: CAM layer is busy\n",
2239 * CAM requests for action come through here
2243 umass_cam_action(struct cam_sim
*sim
, union ccb
*ccb
)
2245 struct umass_softc
*sc
= (struct umass_softc
*)sim
->softc
;
2247 if (sc
== UMASS_GONE
||
2248 (sc
!= NULL
&& !usbd_device_attached(sc
->sc_udev
))) {
2249 ccb
->ccb_h
.status
= CAM_SEL_TIMEOUT
;
2254 lockmgr(&sc
->sc_lock
, LK_EXCLUSIVE
);
2257 * Verify, depending on the operation to perform, that we either got
2258 * a valid sc, because an existing target was referenced, or
2259 * otherwise the SIM is addressed.
2261 * This avoids bombing out at a printf and does give the CAM layer some
2262 * sensible feedback on errors.
2264 switch (ccb
->ccb_h
.func_code
) {
2267 case XPT_GET_TRAN_SETTINGS
:
2268 case XPT_SET_TRAN_SETTINGS
:
2269 case XPT_CALC_GEOMETRY
:
2270 /* the opcodes requiring a target. These should never occur. */
2272 DPRINTF(sc
, UDMASS_GEN
, "%s:xx:%d:%d:func_code 0x%04x: "
2273 "Invalid target (target needed)\n",
2275 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2276 ccb
->ccb_h
.func_code
);
2278 ccb
->ccb_h
.status
= CAM_TID_INVALID
;
2286 * The opcodes sometimes aimed at a target (sc is valid),
2287 * sometimes aimed at the SIM (sc is invalid and target is
2288 * CAM_TARGET_WILDCARD)
2291 (ccb
->ccb_h
.target_id
!= CAM_TARGET_WILDCARD
)) {
2292 DPRINTF(sc
, UDMASS_SCSI
, "%s:%d:%d:%d:func_code 0x%04x: "
2293 "Invalid target (no wildcard)\n",
2294 DEVNAME_SIM
, cam_sim_path(sc
->sc_sim
),
2295 ccb
->ccb_h
.target_id
, ccb
->ccb_h
.target_lun
,
2296 ccb
->ccb_h
.func_code
);
2298 ccb
->ccb_h
.status
= CAM_TID_INVALID
;
2304 /* XXX Hm, we should check the input parameters */
2308 /* Perform the requested action */
2309 switch (ccb
->ccb_h
.func_code
) {
2315 if (ccb
->csio
.ccb_h
.flags
& CAM_CDB_POINTER
) {
2316 cmd
= (uint8_t *)(ccb
->csio
.cdb_io
.cdb_ptr
);
2318 cmd
= (uint8_t *)(ccb
->csio
.cdb_io
.cdb_bytes
);
2321 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_SCSI_IO: "
2322 "cmd: 0x%02x, flags: 0x%02x, "
2323 "%db cmd/%db data/%db sense\n",
2324 cam_sim_path(sc
->sc_sim
), ccb
->ccb_h
.target_id
,
2325 ccb
->ccb_h
.target_lun
, cmd
[0],
2326 ccb
->ccb_h
.flags
& CAM_DIR_MASK
, ccb
->csio
.cdb_len
,
2327 ccb
->csio
.dxfer_len
, ccb
->csio
.sense_len
);
2329 if (sc
->sc_transfer
.ccb
) {
2330 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_SCSI_IO: "
2331 "I/O in progress, deferring\n",
2332 cam_sim_path(sc
->sc_sim
), ccb
->ccb_h
.target_id
,
2333 ccb
->ccb_h
.target_lun
);
2334 ccb
->ccb_h
.status
= CAM_SCSI_BUSY
;
2338 switch (ccb
->ccb_h
.flags
& CAM_DIR_MASK
) {
2345 umass_dump_buffer(sc
, ccb
->csio
.data_ptr
,
2346 ccb
->csio
.dxfer_len
, 48));
2352 ccb
->ccb_h
.status
= CAM_REQ_INPROG
| CAM_SIM_QUEUED
;
2355 * sc->sc_transform will convert the command to the
2356 * command format needed by the specific command set
2357 * and return the converted command in
2358 * "sc->sc_transfer.cmd_data"
2360 if (umass_std_transform(sc
, ccb
, cmd
, ccb
->csio
.cdb_len
)) {
2362 if (sc
->sc_transfer
.cmd_data
[0] == INQUIRY
) {
2363 const char *pserial
;
2365 pserial
= usb_get_serial(sc
->sc_udev
);
2368 * Umass devices don't generally report their serial numbers
2369 * in the usual SCSI way. Emulate it here.
2371 if ((sc
->sc_transfer
.cmd_data
[1] & SI_EVPD
) &&
2372 (sc
->sc_transfer
.cmd_data
[2] == SVPD_UNIT_SERIAL_NUMBER
) &&
2373 (pserial
[0] != '\0')) {
2374 struct scsi_vpd_unit_serial_number
*vpd_serial
;
2376 vpd_serial
= (struct scsi_vpd_unit_serial_number
*)ccb
->csio
.data_ptr
;
2377 vpd_serial
->length
= strlen(pserial
);
2378 if (vpd_serial
->length
> sizeof(vpd_serial
->serial_num
))
2379 vpd_serial
->length
= sizeof(vpd_serial
->serial_num
);
2380 memcpy(vpd_serial
->serial_num
, pserial
, vpd_serial
->length
);
2381 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
2382 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2388 * Handle EVPD inquiry for broken devices first
2389 * NO_INQUIRY also implies NO_INQUIRY_EVPD
2391 if ((sc
->sc_quirks
& (NO_INQUIRY_EVPD
| NO_INQUIRY
)) &&
2392 (sc
->sc_transfer
.cmd_data
[1] & SI_EVPD
)) {
2395 scsi_set_sense_data(&ccb
->csio
.sense_data
,
2396 /*sense_format*/ SSD_TYPE_NONE
,
2397 /*current_error*/ 1,
2398 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST
,
2401 /*extra args*/ SSD_ELEM_NONE
);
2403 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
2404 ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
|
2410 * Return fake inquiry data for
2413 if (sc
->sc_quirks
& NO_INQUIRY
) {
2414 memcpy(ccb
->csio
.data_ptr
, &fake_inq_data
,
2415 sizeof(fake_inq_data
));
2416 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
2417 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2421 if (sc
->sc_quirks
& FORCE_SHORT_INQUIRY
) {
2422 ccb
->csio
.dxfer_len
= SHORT_INQUIRY_LENGTH
;
2424 } else if (sc
->sc_transfer
.cmd_data
[0] == SYNCHRONIZE_CACHE
) {
2425 if (sc
->sc_quirks
& NO_SYNCHRONIZE_CACHE
) {
2426 ccb
->csio
.scsi_status
= SCSI_STATUS_OK
;
2427 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2432 umass_command_start(sc
, dir
, ccb
->csio
.data_ptr
,
2433 ccb
->csio
.dxfer_len
,
2435 &umass_cam_cb
, ccb
);
2441 struct ccb_pathinq
*cpi
= &ccb
->cpi
;
2443 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_PATH_INQ:.\n",
2444 sc
? cam_sim_path(sc
->sc_sim
) : -1, ccb
->ccb_h
.target_id
,
2445 ccb
->ccb_h
.target_lun
);
2447 /* host specific information */
2448 cpi
->version_num
= 1;
2449 cpi
->hba_inquiry
= 0;
2450 cpi
->target_sprt
= 0;
2451 cpi
->hba_misc
= PIM_NO_6_BYTE
;
2452 cpi
->hba_eng_cnt
= 0;
2453 cpi
->max_target
= UMASS_SCSIID_MAX
; /* one target */
2454 cpi
->initiator_id
= UMASS_SCSIID_HOST
;
2455 strlcpy(cpi
->sim_vid
, "FreeBSD", SIM_IDLEN
);
2456 strlcpy(cpi
->hba_vid
, "USB SCSI", HBA_IDLEN
);
2457 strlcpy(cpi
->dev_name
, cam_sim_name(sim
), DEV_IDLEN
);
2458 cpi
->unit_number
= cam_sim_unit(sim
);
2459 cpi
->bus_id
= sc
->sc_unit
;
2460 cpi
->protocol
= PROTO_SCSI
;
2461 cpi
->protocol_version
= SCSI_REV_2
;
2462 cpi
->transport
= XPORT_USB
;
2463 cpi
->transport_version
= 0;
2465 cpi
->base_transfer_speed
= 0;
2468 if (sc
->sc_quirks
& FLOPPY_SPEED
) {
2469 cpi
->base_transfer_speed
=
2470 UMASS_FLOPPY_TRANSFER_SPEED
;
2472 switch (usbd_get_speed(sc
->sc_udev
)) {
2473 case USB_SPEED_SUPER
:
2474 cpi
->base_transfer_speed
=
2475 UMASS_SUPER_TRANSFER_SPEED
;
2477 cpi
->maxio
= MAXPHYS
;
2480 case USB_SPEED_HIGH
:
2481 cpi
->base_transfer_speed
=
2482 UMASS_HIGH_TRANSFER_SPEED
;
2485 cpi
->base_transfer_speed
=
2486 UMASS_FULL_TRANSFER_SPEED
;
2490 cpi
->max_lun
= sc
->sc_maxlun
;
2493 cpi
->ccb_h
.status
= CAM_REQ_CMP
;
2499 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_RESET_DEV:.\n",
2500 cam_sim_path(sc
->sc_sim
), ccb
->ccb_h
.target_id
,
2501 ccb
->ccb_h
.target_lun
);
2505 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2509 case XPT_GET_TRAN_SETTINGS
:
2511 struct ccb_trans_settings
*cts
= &ccb
->cts
;
2513 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_GET_TRAN_SETTINGS:.\n",
2514 cam_sim_path(sc
->sc_sim
), ccb
->ccb_h
.target_id
,
2515 ccb
->ccb_h
.target_lun
);
2517 cts
->protocol
= PROTO_SCSI
;
2518 cts
->protocol_version
= SCSI_REV_2
;
2519 cts
->transport
= XPORT_USB
;
2520 cts
->transport_version
= 0;
2521 cts
->xport_specific
.valid
= 0;
2522 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2526 case XPT_SET_TRAN_SETTINGS
:
2528 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_SET_TRAN_SETTINGS:.\n",
2529 cam_sim_path(sc
->sc_sim
), ccb
->ccb_h
.target_id
,
2530 ccb
->ccb_h
.target_lun
);
2532 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
2536 case XPT_CALC_GEOMETRY
:
2538 cam_calc_geometry(&ccb
->ccg
, /* extended */ 1);
2544 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:XPT_NOOP:.\n",
2545 sc
? cam_sim_path(sc
->sc_sim
) : -1, ccb
->ccb_h
.target_id
,
2546 ccb
->ccb_h
.target_lun
);
2548 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2553 DPRINTF(sc
, UDMASS_SCSI
, "%d:%d:%d:func_code 0x%04x: "
2554 "Not implemented\n",
2555 sc
? cam_sim_path(sc
->sc_sim
) : -1, ccb
->ccb_h
.target_id
,
2556 ccb
->ccb_h
.target_lun
, ccb
->ccb_h
.func_code
);
2558 ccb
->ccb_h
.status
= CAM_FUNC_NOTAVAIL
;
2565 lockmgr(&sc
->sc_lock
, LK_RELEASE
);
2571 umass_cam_poll(struct cam_sim
*sim
)
2573 struct umass_softc
*sc
= (struct umass_softc
*)sim
->softc
;
2575 if (sc
== UMASS_GONE
)
2578 DPRINTF(sc
, UDMASS_SCSI
, "CAM poll\n");
2580 usbd_transfer_poll(sc
->sc_xfer
, UMASS_T_MAX
);
2585 * finalise a completed CAM command
2589 umass_cam_cb(struct umass_softc
*sc
, union ccb
*ccb
, uint32_t residue
,
2592 ccb
->csio
.resid
= residue
;
2596 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2597 if ((sc
->sc_quirks
& READ_CAPACITY_OFFBY1
) &&
2598 (ccb
->ccb_h
.func_code
== XPT_SCSI_IO
) &&
2599 (ccb
->csio
.cdb_io
.cdb_bytes
[0] == READ_CAPACITY
)) {
2600 struct scsi_read_capacity_data
*rcap
;
2603 rcap
= (void *)(ccb
->csio
.data_ptr
);
2604 maxsector
= scsi_4btoul(rcap
->addr
) - 1;
2605 scsi_ulto4b(maxsector
, rcap
->addr
);
2608 * We have to add SVPD_UNIT_SERIAL_NUMBER to the list
2609 * of pages supported by the device - otherwise, CAM
2610 * will never ask us for the serial number if the
2611 * device cannot handle that by itself.
2613 if (ccb
->ccb_h
.func_code
== XPT_SCSI_IO
&&
2614 sc
->sc_transfer
.cmd_data
[0] == INQUIRY
&&
2615 (sc
->sc_transfer
.cmd_data
[1] & SI_EVPD
) &&
2616 sc
->sc_transfer
.cmd_data
[2] == SVPD_SUPPORTED_PAGE_LIST
&&
2617 (usb_get_serial(sc
->sc_udev
)[0] != '\0')) {
2618 struct ccb_scsiio
*csio
;
2619 struct scsi_vpd_supported_page_list
*page_list
;
2622 page_list
= (struct scsi_vpd_supported_page_list
*)csio
->data_ptr
;
2623 if (page_list
->length
+ 1 < SVPD_SUPPORTED_PAGES_SIZE
) {
2624 page_list
->list
[page_list
->length
] = SVPD_UNIT_SERIAL_NUMBER
;
2625 page_list
->length
++;
2631 case STATUS_CMD_UNKNOWN
:
2632 case STATUS_CMD_FAILED
:
2634 /* fetch sense data */
2636 /* the rest of the command was filled in at attach */
2637 sc
->cam_scsi_sense
.length
= ccb
->csio
.sense_len
;
2639 DPRINTF(sc
, UDMASS_SCSI
, "Fetching %d bytes of "
2640 "sense data\n", ccb
->csio
.sense_len
);
2642 if (umass_std_transform(sc
, ccb
, &sc
->cam_scsi_sense
.opcode
,
2643 sizeof(sc
->cam_scsi_sense
))) {
2645 if ((sc
->sc_quirks
& FORCE_SHORT_INQUIRY
) &&
2646 (sc
->sc_transfer
.cmd_data
[0] == INQUIRY
)) {
2647 ccb
->csio
.sense_len
= SHORT_INQUIRY_LENGTH
;
2649 umass_command_start(sc
, DIR_IN
, &ccb
->csio
.sense_data
.error_code
,
2650 ccb
->csio
.sense_len
, ccb
->ccb_h
.timeout
,
2651 &umass_cam_sense_cb
, ccb
);
2657 * The wire protocol failed and will hopefully have
2658 * recovered. We return an error to CAM and let CAM
2659 * retry the command if necessary.
2661 ccb
->ccb_h
.status
= CAM_REQ_CMP_ERR
;
2668 * Finalise a completed autosense operation
2671 umass_cam_sense_cb(struct umass_softc
*sc
, union ccb
*ccb
, uint32_t residue
,
2678 case STATUS_CMD_UNKNOWN
:
2679 case STATUS_CMD_FAILED
:
2681 int error
, key
, asc
, ascq
;
2684 ccb
->csio
.sense_resid
= residue
;
2685 sense_len
= ccb
->csio
.sense_len
- ccb
->csio
.sense_resid
;
2686 scsi_extract_sense(&ccb
->csio
.sense_data
,
2690 if (ccb
->csio
.ccb_h
.flags
& CAM_CDB_POINTER
) {
2691 cmd
= (uint8_t *)(ccb
->csio
.cdb_io
.cdb_ptr
);
2693 cmd
= (uint8_t *)(ccb
->csio
.cdb_io
.cdb_bytes
);
2697 * Getting sense data always succeeds (apart from wire
2700 if ((sc
->sc_quirks
& RS_NO_CLEAR_UA
) &&
2701 (cmd
[0] == INQUIRY
) &&
2702 (key
== SSD_KEY_UNIT_ATTENTION
)) {
2704 * Ignore unit attention errors in the case where
2705 * the Unit Attention state is not cleared on
2706 * REQUEST SENSE. They will appear again at the next
2709 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2710 } else if (key
== SSD_KEY_NO_SENSE
) {
2712 * No problem after all (in the case of CBI without
2715 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
2716 } else if ((sc
->sc_quirks
& RS_NO_CLEAR_UA
) &&
2717 (cmd
[0] == READ_CAPACITY
) &&
2718 (key
== SSD_KEY_UNIT_ATTENTION
)) {
2720 * Some devices do not clear the unit attention error
2721 * on request sense. We insert a test unit ready
2722 * command to make sure we clear the unit attention
2723 * condition, then allow the retry to proceed as
2727 ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
2728 | CAM_AUTOSNS_VALID
;
2729 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
2734 DPRINTF(sc
, UDMASS_SCSI
, "Doing a sneaky"
2735 "TEST_UNIT_READY\n");
2737 /* the rest of the command was filled in at attach */
2739 if (umass_std_transform(sc
, ccb
,
2740 &sc
->cam_scsi_test_unit_ready
.opcode
,
2741 sizeof(sc
->cam_scsi_test_unit_ready
))) {
2742 umass_command_start(sc
, DIR_NONE
, NULL
, 0,
2744 &umass_cam_quirk_cb
, ccb
);
2748 ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
2749 | CAM_AUTOSNS_VALID
;
2750 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
2756 DPRINTF(sc
, UDMASS_SCSI
, "Autosense failed, "
2757 "status %d\n", status
);
2758 ccb
->ccb_h
.status
= CAM_AUTOSENSE_FAIL
;
2764 * This completion code just handles the fact that we sent a test-unit-ready
2765 * after having previously failed a READ CAPACITY with CHECK_COND. Even
2766 * though this command succeeded, we have to tell CAM to retry.
2769 umass_cam_quirk_cb(struct umass_softc
*sc
, union ccb
*ccb
, uint32_t residue
,
2772 DPRINTF(sc
, UDMASS_SCSI
, "Test unit ready "
2773 "returned status %d\n", status
);
2775 ccb
->ccb_h
.status
= CAM_SCSI_STATUS_ERROR
2776 | CAM_AUTOSNS_VALID
;
2777 ccb
->csio
.scsi_status
= SCSI_STATUS_CHECK_COND
;
2782 * SCSI specific functions
2786 umass_scsi_transform(struct umass_softc
*sc
, uint8_t *cmd_ptr
,
2789 if ((cmd_len
== 0) ||
2790 (cmd_len
> sizeof(sc
->sc_transfer
.cmd_data
))) {
2791 DPRINTF(sc
, UDMASS_SCSI
, "Invalid command "
2792 "length: %d bytes\n", cmd_len
);
2793 return (0); /* failure */
2795 sc
->sc_transfer
.cmd_len
= cmd_len
;
2797 switch (cmd_ptr
[0]) {
2798 case TEST_UNIT_READY
:
2799 if (sc
->sc_quirks
& NO_TEST_UNIT_READY
) {
2800 DPRINTF(sc
, UDMASS_SCSI
, "Converted TEST_UNIT_READY "
2802 memset(sc
->sc_transfer
.cmd_data
, 0, cmd_len
);
2803 sc
->sc_transfer
.cmd_data
[0] = START_STOP_UNIT
;
2804 sc
->sc_transfer
.cmd_data
[4] = SSS_START
;
2811 * some drives wedge when asked for full inquiry
2814 if (sc
->sc_quirks
& FORCE_SHORT_INQUIRY
) {
2815 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
2816 sc
->sc_transfer
.cmd_data
[4] = SHORT_INQUIRY_LENGTH
;
2822 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
2827 umass_rbc_transform(struct umass_softc
*sc
, uint8_t *cmd_ptr
, uint8_t cmd_len
)
2829 if ((cmd_len
== 0) ||
2830 (cmd_len
> sizeof(sc
->sc_transfer
.cmd_data
))) {
2831 DPRINTF(sc
, UDMASS_SCSI
, "Invalid command "
2832 "length: %d bytes\n", cmd_len
);
2833 return (0); /* failure */
2835 switch (cmd_ptr
[0]) {
2836 /* these commands are defined in RBC: */
2839 case START_STOP_UNIT
:
2840 case SYNCHRONIZE_CACHE
:
2842 case 0x2f: /* VERIFY_10 is absent from
2845 case MODE_SELECT_10
:
2847 case TEST_UNIT_READY
:
2850 * The following commands are not listed in my copy of the
2851 * RBC specs. CAM however seems to want those, and at least
2852 * the Sony DSC device appears to support those as well
2857 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
2859 if ((sc
->sc_quirks
& RBC_PAD_TO_12
) && (cmd_len
< 12)) {
2860 memset(sc
->sc_transfer
.cmd_data
+ cmd_len
,
2864 sc
->sc_transfer
.cmd_len
= cmd_len
;
2865 return (1); /* sucess */
2867 /* All other commands are not legal in RBC */
2869 DPRINTF(sc
, UDMASS_SCSI
, "Unsupported RBC "
2870 "command 0x%02x\n", cmd_ptr
[0]);
2871 return (0); /* failure */
2876 umass_ufi_transform(struct umass_softc
*sc
, uint8_t *cmd_ptr
,
2879 if ((cmd_len
== 0) ||
2880 (cmd_len
> sizeof(sc
->sc_transfer
.cmd_data
))) {
2881 DPRINTF(sc
, UDMASS_SCSI
, "Invalid command "
2882 "length: %d bytes\n", cmd_len
);
2883 return (0); /* failure */
2885 /* An UFI command is always 12 bytes in length */
2886 sc
->sc_transfer
.cmd_len
= UFI_COMMAND_LENGTH
;
2888 /* Zero the command data */
2889 memset(sc
->sc_transfer
.cmd_data
, 0, UFI_COMMAND_LENGTH
);
2891 switch (cmd_ptr
[0]) {
2893 * Commands of which the format has been verified. They
2894 * should work. Copy the command into the (zeroed out)
2895 * destination buffer.
2897 case TEST_UNIT_READY
:
2898 if (sc
->sc_quirks
& NO_TEST_UNIT_READY
) {
2900 * Some devices do not support this command. Start
2901 * Stop Unit should give the same results
2903 DPRINTF(sc
, UDMASS_UFI
, "Converted TEST_UNIT_READY "
2906 sc
->sc_transfer
.cmd_data
[0] = START_STOP_UNIT
;
2907 sc
->sc_transfer
.cmd_data
[4] = SSS_START
;
2916 case START_STOP_UNIT
:
2917 case SEND_DIAGNOSTIC
:
2922 case POSITION_TO_ELEMENT
: /* SEEK_10 */
2923 case WRITE_AND_VERIFY
:
2925 case MODE_SELECT_10
:
2929 case READ_FORMAT_CAPACITIES
:
2933 * SYNCHRONIZE_CACHE isn't supported by UFI, nor should it be
2934 * required for UFI devices, so it is appropriate to fake
2937 case SYNCHRONIZE_CACHE
:
2941 DPRINTF(sc
, UDMASS_SCSI
, "Unsupported UFI "
2942 "command 0x%02x\n", cmd_ptr
[0]);
2943 return (0); /* failure */
2946 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
2947 return (1); /* success */
2951 * 8070i (ATAPI) specific functions
2954 umass_atapi_transform(struct umass_softc
*sc
, uint8_t *cmd_ptr
,
2957 if ((cmd_len
== 0) ||
2958 (cmd_len
> sizeof(sc
->sc_transfer
.cmd_data
))) {
2959 DPRINTF(sc
, UDMASS_SCSI
, "Invalid command "
2960 "length: %d bytes\n", cmd_len
);
2961 return (0); /* failure */
2963 /* An ATAPI command is always 12 bytes in length. */
2964 sc
->sc_transfer
.cmd_len
= ATAPI_COMMAND_LENGTH
;
2966 /* Zero the command data */
2967 memset(sc
->sc_transfer
.cmd_data
, 0, ATAPI_COMMAND_LENGTH
);
2969 switch (cmd_ptr
[0]) {
2971 * Commands of which the format has been verified. They
2972 * should work. Copy the command into the destination
2977 * some drives wedge when asked for full inquiry
2980 if (sc
->sc_quirks
& FORCE_SHORT_INQUIRY
) {
2981 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
2983 sc
->sc_transfer
.cmd_data
[4] = SHORT_INQUIRY_LENGTH
;
2988 case TEST_UNIT_READY
:
2989 if (sc
->sc_quirks
& NO_TEST_UNIT_READY
) {
2990 DPRINTF(sc
, UDMASS_SCSI
, "Converted TEST_UNIT_READY "
2992 sc
->sc_transfer
.cmd_data
[0] = START_STOP_UNIT
;
2993 sc
->sc_transfer
.cmd_data
[4] = SSS_START
;
3000 case START_STOP_UNIT
:
3001 case SEND_DIAGNOSTIC
:
3006 case POSITION_TO_ELEMENT
: /* SEEK_10 */
3007 case SYNCHRONIZE_CACHE
:
3008 case MODE_SELECT_10
:
3011 case 0x42: /* READ_SUBCHANNEL */
3012 case 0x43: /* READ_TOC */
3013 case 0x44: /* READ_HEADER */
3014 case 0x47: /* PLAY_MSF (Play Minute/Second/Frame) */
3015 case 0x48: /* PLAY_TRACK */
3016 case 0x49: /* PLAY_TRACK_REL */
3017 case 0x4b: /* PAUSE */
3018 case 0x51: /* READ_DISK_INFO */
3019 case 0x52: /* READ_TRACK_INFO */
3020 case 0x54: /* SEND_OPC */
3021 case 0x59: /* READ_MASTER_CUE */
3022 case 0x5b: /* CLOSE_TR_SESSION */
3023 case 0x5c: /* READ_BUFFER_CAP */
3024 case 0x5d: /* SEND_CUE_SHEET */
3025 case 0xa1: /* BLANK */
3026 case 0xa5: /* PLAY_12 */
3027 case 0xa6: /* EXCHANGE_MEDIUM */
3028 case 0xad: /* READ_DVD_STRUCTURE */
3029 case 0xbb: /* SET_CD_SPEED */
3030 case 0xe5: /* READ_TRACK_INFO_PHILIPS */
3036 DPRINTF(sc
, UDMASS_SCSI
, "Unsupported ATAPI "
3037 "command 0x%02x - trying anyway\n",
3042 memcpy(sc
->sc_transfer
.cmd_data
, cmd_ptr
, cmd_len
);
3043 return (1); /* success */
3047 umass_no_transform(struct umass_softc
*sc
, uint8_t *cmd
,
3050 return (0); /* failure */
3054 umass_std_transform(struct umass_softc
*sc
, union ccb
*ccb
,
3055 uint8_t *cmd
, uint8_t cmdlen
)
3059 retval
= (sc
->sc_transform
) (sc
, cmd
, cmdlen
);
3062 ccb
->ccb_h
.status
= CAM_REQ_CMP
;
3065 } else if (retval
== 0) {
3066 ccb
->ccb_h
.status
= CAM_REQ_INVALID
;
3070 /* Command should be executed */
3076 umass_bbb_dump_cbw(struct umass_softc
*sc
, umass_bbb_cbw_t
*cbw
)
3078 uint8_t *c
= cbw
->CBWCDB
;
3080 uint32_t dlen
= UGETDW(cbw
->dCBWDataTransferLength
);
3081 uint32_t tag
= UGETDW(cbw
->dCBWTag
);
3083 uint8_t clen
= cbw
->bCDBLength
;
3084 uint8_t flags
= cbw
->bCBWFlags
;
3085 uint8_t lun
= cbw
->bCBWLUN
;
3087 DPRINTF(sc
, UDMASS_BBB
, "CBW %d: cmd = %db "
3088 "(0x%02x%02x%02x%02x%02x%02x%s), "
3089 "data = %db, lun = %d, dir = %s\n",
3091 c
[0], c
[1], c
[2], c
[3], c
[4], c
[5], (clen
> 6 ? "..." : ""),
3092 dlen
, lun
, (flags
== CBWFLAGS_IN
? "in" :
3093 (flags
== CBWFLAGS_OUT
? "out" : "<invalid>")));
3097 umass_bbb_dump_csw(struct umass_softc
*sc
, umass_bbb_csw_t
*csw
)
3099 uint32_t sig
= UGETDW(csw
->dCSWSignature
);
3100 uint32_t tag
= UGETDW(csw
->dCSWTag
);
3101 uint32_t res
= UGETDW(csw
->dCSWDataResidue
);
3102 uint8_t status
= csw
->bCSWStatus
;
3104 DPRINTF(sc
, UDMASS_BBB
, "CSW %d: sig = 0x%08x (%s), tag = 0x%08x, "
3105 "res = %d, status = 0x%02x (%s)\n",
3106 tag
, sig
, (sig
== CSWSIGNATURE
? "valid" : "invalid"),
3108 status
, (status
== CSWSTATUS_GOOD
? "good" :
3109 (status
== CSWSTATUS_FAILED
? "failed" :
3110 (status
== CSWSTATUS_PHASE
? "phase" : "<invalid>"))));
3114 umass_cbi_dump_cmd(struct umass_softc
*sc
, void *cmd
, uint8_t cmdlen
)
3117 uint8_t dir
= sc
->sc_transfer
.dir
;
3119 DPRINTF(sc
, UDMASS_BBB
, "cmd = %db "
3120 "(0x%02x%02x%02x%02x%02x%02x%s), "
3121 "data = %db, dir = %s\n",
3123 c
[0], c
[1], c
[2], c
[3], c
[4], c
[5], (cmdlen
> 6 ? "..." : ""),
3124 sc
->sc_transfer
.data_len
,
3125 (dir
== DIR_IN
? "in" :
3126 (dir
== DIR_OUT
? "out" :
3127 (dir
== DIR_NONE
? "no data phase" : "<invalid>"))));
3131 umass_dump_buffer(struct umass_softc
*sc
, uint8_t *buffer
, uint32_t buflen
,
3142 ksprintf(s2
, " buffer=%p, buflen=%d", buffer
, buflen
);
3143 for (i
= 0; (i
< buflen
) && (i
< printlen
); i
++) {
3145 if (j
== 0 && i
!= 0) {
3146 DPRINTF(sc
, UDMASS_GEN
, "0x %s%s\n",
3150 ksprintf(&s1
[j
* 2], "%02x", buffer
[i
] & 0xff);
3152 if (buflen
> printlen
)
3153 ksprintf(s3
, " ...");
3154 DPRINTF(sc
, UDMASS_GEN
, "0x %s%s%s\n",