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