xref: /netbsd-src/sys/dev/usb/umassvar.h (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: umassvar.h,v 1.3 2001/04/17 00:50:13 augustss Exp $	*/
2 /*-
3  * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
4  *		      Nick Hibma <n_hibma@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *     $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
29  */
30 
31 #ifdef UMASS_DEBUG
32 #define DIF(m, x)	if (umassdebug & (m)) do { x ; } while (0)
33 #define DPRINTF(m, x)	if (umassdebug & (m)) logprintf x
34 #define UDMASS_UPPER	0x00008000	/* upper layer */
35 #define UDMASS_GEN	0x00010000	/* general */
36 #define UDMASS_SCSI	0x00020000	/* scsi */
37 #define UDMASS_UFI	0x00040000	/* ufi command set */
38 #define UDMASS_8070	0x00080000	/* 8070i command set */
39 #define UDMASS_USB	0x00100000	/* USB general */
40 #define UDMASS_BBB	0x00200000	/* Bulk-Only transfers */
41 #define UDMASS_CBI	0x00400000	/* CBI transfers */
42 #define UDMASS_ALL	0xffff0000	/* all of the above */
43 
44 #define UDMASS_XFER	0x40000000	/* all transfers */
45 #define UDMASS_CMD	0x80000000
46 
47 extern int umassdebug;
48 #else
49 #define DIF(m, x)	/* nop */
50 #define DPRINTF(m, x)	/* nop */
51 #endif
52 
53 /* Generic definitions */
54 
55 #define UFI_COMMAND_LENGTH 12
56 
57 /* Direction for umass_*_transfer */
58 #define DIR_NONE	0
59 #define DIR_IN		1
60 #define DIR_OUT		2
61 
62 /* The transfer speed determines the timeout value */
63 #define UMASS_DEFAULT_TRANSFER_SPEED	150	/* in kb/s, conservative est. */
64 #define UMASS_FLOPPY_TRANSFER_SPEED	20
65 #define UMASS_ZIP100_TRANSFER_SPEED	650
66 
67 #define UMASS_SPINUP_TIME 10000	/* ms */
68 
69 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
70 
71 
72 /* Bulk-Only features */
73 
74 #define UR_BBB_RESET	0xff		/* Bulk-Only reset */
75 #define	UR_BBB_GET_MAX_LUN	0xfe
76 
77 /* Command Block Wrapper */
78 typedef struct {
79 	uDWord		dCBWSignature;
80 #define CBWSIGNATURE	0x43425355
81 	uDWord		dCBWTag;
82 	uDWord		dCBWDataTransferLength;
83 	uByte		bCBWFlags;
84 #define CBWFLAGS_OUT	0x00
85 #define CBWFLAGS_IN	0x80
86 	uByte		bCBWLUN;
87 	uByte		bCDBLength;
88 #define CBWCDBLENGTH	16
89 	uByte		CBWCDB[CBWCDBLENGTH];
90 } umass_bbb_cbw_t;
91 #define UMASS_BBB_CBW_SIZE	31
92 
93 /* Command Status Wrapper */
94 typedef struct {
95 	uDWord		dCSWSignature;
96 #define CSWSIGNATURE	0x53425355
97 	uDWord		dCSWTag;
98 	uDWord		dCSWDataResidue;
99 	uByte		bCSWStatus;
100 #define CSWSTATUS_GOOD	0x0
101 #define CSWSTATUS_FAILED 0x1
102 #define CSWSTATUS_PHASE	0x2
103 } umass_bbb_csw_t;
104 #define UMASS_BBB_CSW_SIZE	13
105 
106 /* CBI features */
107 
108 #define UR_CBI_ADSC	0x00
109 
110 typedef unsigned char umass_cbi_cbl_t[16];	/* Command block */
111 
112 typedef union {
113 	struct {
114 		uByte	type;
115 #define IDB_TYPE_CCI		0x00
116 		uByte	value;
117 #define IDB_VALUE_PASS		0x00
118 #define IDB_VALUE_FAIL		0x01
119 #define IDB_VALUE_PHASE		0x02
120 #define IDB_VALUE_PERSISTENT	0x03
121 #define IDB_VALUE_STATUS_MASK	0x03
122 	} common;
123 
124 	struct {
125 		uByte	asc;
126 		uByte	ascq;
127 	} ufi;
128 } umass_cbi_sbl_t;
129 
130 struct umass_softc;		/* see below */
131 
132 typedef void (*transfer_cb_f)(struct umass_softc *sc, void *priv,
133 			      int residue, int status);
134 #define STATUS_CMD_OK		0	/* everything ok */
135 #define STATUS_CMD_UNKNOWN	1	/* will have to fetch sense */
136 #define STATUS_CMD_FAILED	2	/* transfer was ok, command failed */
137 #define STATUS_WIRE_FAILED	3	/* couldn't even get command across */
138 
139 typedef void (*wire_reset_f)(struct umass_softc *sc, int status);
140 typedef void (*wire_transfer_f)(struct umass_softc *sc, int lun,
141 				void *cmd, int cmdlen, void *data, int datalen,
142 				int dir, transfer_cb_f cb, void *priv);
143 typedef void (*wire_state_f)(usbd_xfer_handle xfer,
144 			     usbd_private_handle priv, usbd_status err);
145 
146 
147 /* the per device structure */
148 struct umass_softc {
149 	USBBASEDEVICE		sc_dev;		/* base device */
150 	usbd_device_handle	sc_udev;	/* device */
151 
152 	unsigned char		drive;
153 #define DRIVE_GENERIC		0	/* use defaults for this one */
154 #define ZIP_100			1	/* to be used for quirks */
155 #define ZIP_250			2
156 #define SHUTTLE_EUSB		3
157 #define INSYSTEM_USBCABLE	4
158 	unsigned char		quirks;
159 	/* The drive does not support Test Unit Ready. Convert to
160 	 * Start Unit.
161 	 * Y-E Data
162 	 * ZIP 100
163 	 */
164 #define NO_TEST_UNIT_READY	0x01
165 	/* The drive does not reset the Unit Attention state after
166 	 * REQUEST SENSE has been sent. The INQUIRY command does not reset
167 	 * the UA either, and so CAM runs in circles trying to retrieve the
168 	 * initial INQUIRY data.
169 	 * Y-E Data
170 	 */
171 #define RS_NO_CLEAR_UA		0x02	/* no REQUEST SENSE on INQUIRY*/
172 	/* The drive does not support START_STOP.
173 	 * Shuttle E-USB
174 	 */
175 #define NO_START_STOP		0x04
176 	/* Don't ask for full inquiry data (255 bytes).
177 	 * Yano ATAPI-USB
178 	 */
179 #define FORCE_SHORT_INQUIRY      0x08
180 
181 	u_int8_t	wire_proto;		/* USB wire protocol */
182 #define WPROTO_BBB	1
183 #define WPROTO_CBI	2
184 #define WPROTO_CBI_I	3
185 	u_int8_t	cmd_proto;		/* command protocol */
186 #define CPROTO_SCSI	1
187 #define CPROTO_ATAPI	2
188 #define CPROTO_UFI	3
189 #define CPROTO_RBC	4
190 
191 	u_char			subclass;	/* interface subclass */
192 	u_char			protocol;	/* interface protocol */
193 
194 	usbd_interface_handle	iface;		/* Mass Storage interface */
195 	int			ifaceno;	/* MS iface number */
196 
197 	u_int8_t		bulkin;		/* bulk-in Endpoint Address */
198 	u_int8_t		bulkout;	/* bulk-out Endpoint Address */
199 	u_int8_t		intrin;		/* intr-in Endp. (CBI) */
200 	usbd_pipe_handle	bulkin_pipe;
201 	usbd_pipe_handle	bulkout_pipe;
202 	usbd_pipe_handle	intrin_pipe;
203 
204 	/* Reset the device in a wire protocol specific way */
205 	wire_reset_f		reset;
206 
207 	/* The start of a wire transfer. It prepares the whole transfer (cmd,
208 	 * data, and status stage) and initiates it. It is up to the state
209 	 * machine (below) to handle the various stages and errors in these
210 	 */
211 	wire_transfer_f		transfer;
212 
213 	/* The state machine, handling the various states during a transfer */
214 	wire_state_f		state;
215 
216 	/* Bulk specific variables for transfers in progress */
217 	umass_bbb_cbw_t		cbw;	/* command block wrapper */
218 	umass_bbb_csw_t		csw;	/* command status wrapper*/
219 	/* CBI specific variables for transfers in progress */
220 	umass_cbi_cbl_t		cbl;	/* command block */
221 	umass_cbi_sbl_t		sbl;	/* status block */
222 
223 	/* generic variables for transfers in progress */
224 	/* ctrl transfer requests */
225 	usb_device_request_t	request;
226 
227 	/* xfer handles
228 	 * Most of our operations are initiated from interrupt context, so
229 	 * we need to avoid using the one that is in use. We want to avoid
230 	 * allocating them in the interrupt context as well.
231 	 */
232 	/* indices into array below */
233 #define XFER_BBB_CBW		0	/* Bulk-Only */
234 #define XFER_BBB_DATA		1
235 #define XFER_BBB_DCLEAR		2
236 #define XFER_BBB_CSW1		3
237 #define XFER_BBB_CSW2		4
238 #define XFER_BBB_SCLEAR		5
239 #define XFER_BBB_RESET1		6
240 #define XFER_BBB_RESET2		7
241 #define XFER_BBB_RESET3		8
242 
243 #define XFER_CBI_CB		0	/* CBI */
244 #define XFER_CBI_DATA		1
245 #define XFER_CBI_STATUS		2
246 #define XFER_CBI_DCLEAR		3
247 #define XFER_CBI_SCLEAR		4
248 #define XFER_CBI_RESET1		5
249 #define XFER_CBI_RESET2		6
250 #define XFER_CBI_RESET3		7
251 
252 #define XFER_NR			9	/* maximum number */
253 
254 	usbd_xfer_handle	transfer_xfer[XFER_NR]; /* for ctrl xfers */
255 
256 	void			*data_buffer;
257 
258 	int			transfer_dir;		/* data direction */
259 	void			*transfer_data;		/* data buffer */
260 	int			transfer_datalen;	/* (maximum) length */
261 	int			transfer_actlen;	/* actual length */
262 	transfer_cb_f		transfer_cb;		/* callback */
263 	void			*transfer_priv;		/* for callback */
264 	int			transfer_status;
265 
266 	int			transfer_state;
267 #define TSTATE_IDLE			0
268 #define TSTATE_BBB_COMMAND		1	/* CBW transfer */
269 #define TSTATE_BBB_DATA			2	/* Data transfer */
270 #define TSTATE_BBB_DCLEAR		3	/* clear endpt stall */
271 #define TSTATE_BBB_STATUS1		4	/* clear endpt stall */
272 #define TSTATE_BBB_SCLEAR		5	/* clear endpt stall */
273 #define TSTATE_BBB_STATUS2		6	/* CSW transfer */
274 #define TSTATE_BBB_RESET1		7	/* reset command */
275 #define TSTATE_BBB_RESET2		8	/* in clear stall */
276 #define TSTATE_BBB_RESET3		9	/* out clear stall */
277 #define TSTATE_CBI_COMMAND		10	/* command transfer */
278 #define TSTATE_CBI_DATA			11	/* data transfer */
279 #define TSTATE_CBI_STATUS		12	/* status transfer */
280 #define TSTATE_CBI_DCLEAR		13	/* clear ep stall */
281 #define TSTATE_CBI_SCLEAR		14	/* clear ep stall */
282 #define TSTATE_CBI_RESET1		15	/* reset command */
283 #define TSTATE_CBI_RESET2		16	/* in clear stall */
284 #define TSTATE_CBI_RESET3		17	/* out clear stall */
285 #define TSTATE_STATES			18	/* # of states above */
286 
287 
288 	int			transfer_speed;		/* in kb/s */
289 	int			timeout;		/* in msecs */
290 
291 	u_int8_t		maxlun;			/* max lun supported */
292 
293 #ifdef UMASS_DEBUG
294 	struct timeval tv;
295 #endif
296 
297 	int			sc_xfer_flags;
298 	char			sc_dying;
299 
300 	struct umassbus_softc	bus;
301 };
302 
303