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