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