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