xref: /openbsd/sys/dev/usb/umassvar.h (revision 17df1aa7)
1 /*	$OpenBSD: umassvar.h,v 1.12 2007/06/13 06:25:03 mbalmer Exp $ */
2 /*	$NetBSD: umassvar.h,v 1.20 2003/09/08 19:31:01 mycroft Exp $	*/
3 /*-
4  * Copyright (c) 1999 MAEKAWA Masahide <bishop@rr.iij4u.or.jp>,
5  *		      Nick Hibma <n_hibma@freebsd.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *     $FreeBSD: src/sys/dev/usb/umass.c,v 1.13 2000/03/26 01:39:12 n_hibma Exp $
30  */
31 
32 #ifdef UMASS_DEBUG
33 #define DIF(m, x)	if (umassdebug & (m)) do { x ; } while (0)
34 #define DPRINTF(m, x)	do { if (umassdebug & (m)) printf x; } while (0)
35 #define UDMASS_UPPER	0x00008000	/* upper layer */
36 #define UDMASS_GEN	0x00010000	/* general */
37 #define UDMASS_SCSI	0x00020000	/* scsi */
38 #define UDMASS_UFI	0x00040000	/* ufi command set */
39 #define UDMASS_8070	0x00080000	/* 8070i command set */
40 #define UDMASS_USB	0x00100000	/* USB general */
41 #define UDMASS_BBB	0x00200000	/* Bulk-Only transfers */
42 #define UDMASS_CBI	0x00400000	/* CBI transfers */
43 #define UDMASS_ALL	0xffff0000	/* all of the above */
44 
45 #define UDMASS_XFER	0x40000000	/* all transfers */
46 #define UDMASS_CMD	0x80000000
47 
48 extern int umassdebug;
49 #else
50 #define DIF(m, x)	/* nop */
51 #define DPRINTF(m, x)	/* nop */
52 #endif
53 
54 /* Generic definitions */
55 
56 #define UFI_COMMAND_LENGTH 12
57 
58 /* Direction for umass_*_transfer */
59 #define DIR_NONE	0
60 #define DIR_IN		1
61 #define DIR_OUT		2
62 
63 /* Endpoints for umass */
64 #define	UMASS_BULKIN	0
65 #define	UMASS_BULKOUT	1
66 #define	UMASS_INTRIN	2
67 #define	UMASS_NEP	3
68 
69 /* Bulk-Only features */
70 
71 #define UR_BBB_RESET	0xff		/* Bulk-Only reset */
72 #define	UR_BBB_GET_MAX_LUN	0xfe
73 
74 /* Command Block Wrapper */
75 typedef struct {
76 	uDWord		dCBWSignature;
77 #define CBWSIGNATURE	0x43425355
78 	uDWord		dCBWTag;
79 	uDWord		dCBWDataTransferLength;
80 	uByte		bCBWFlags;
81 #define CBWFLAGS_OUT	0x00
82 #define CBWFLAGS_IN	0x80
83 	uByte		bCBWLUN;
84 	uByte		bCDBLength;
85 #define CBWCDBLENGTH	16
86 	uByte		CBWCDB[CBWCDBLENGTH];
87 } umass_bbb_cbw_t;
88 #define UMASS_BBB_CBW_SIZE	31
89 
90 /* Command Status Wrapper */
91 typedef struct {
92 	uDWord		dCSWSignature;
93 #define CSWSIGNATURE		0x53425355
94 #define CSWSIGNATURE_OLYMPUS_C1	0x55425355
95 	uDWord		dCSWTag;
96 	uDWord		dCSWDataResidue;
97 	uByte		bCSWStatus;
98 #define CSWSTATUS_GOOD	0x0
99 #define CSWSTATUS_FAILED 0x1
100 #define CSWSTATUS_PHASE	0x2
101 } umass_bbb_csw_t;
102 #define UMASS_BBB_CSW_SIZE	13
103 
104 /* CBI features */
105 
106 #define UR_CBI_ADSC	0x00
107 
108 typedef unsigned char umass_cbi_cbl_t[16];	/* Command block */
109 
110 typedef union {
111 	struct {
112 		uByte	type;
113 #define IDB_TYPE_CCI		0x00
114 		uByte	value;
115 #define IDB_VALUE_PASS		0x00
116 #define IDB_VALUE_FAIL		0x01
117 #define IDB_VALUE_PHASE		0x02
118 #define IDB_VALUE_PERSISTENT	0x03
119 #define IDB_VALUE_STATUS_MASK	0x03
120 	} common;
121 
122 	struct {
123 		uByte	asc;
124 		uByte	ascq;
125 	} ufi;
126 } umass_cbi_sbl_t;
127 
128 struct umass_softc;		/* see below */
129 
130 typedef void (*umass_callback)(struct umass_softc *, void *, int, int);
131 #define STATUS_CMD_OK		0	/* everything ok */
132 #define STATUS_CMD_UNKNOWN	1	/* will have to fetch sense */
133 #define STATUS_CMD_FAILED	2	/* transfer was ok, command failed */
134 #define STATUS_WIRE_FAILED	3	/* couldn't even get command across */
135 
136 typedef void (*umass_wire_xfer)(struct umass_softc *, int, void *, int, void *,
137 				int, int, u_int, umass_callback, void *);
138 typedef void (*umass_wire_reset)(struct umass_softc *, int);
139 typedef void (*umass_wire_state)(usbd_xfer_handle, usbd_private_handle,
140 				 usbd_status);
141 
142 struct umass_wire_methods {
143 	umass_wire_xfer		wire_xfer;
144 	umass_wire_reset	wire_reset;
145 	umass_wire_state	wire_state;
146 };
147 
148 struct umassbus_softc {
149 	struct device		*sc_child;	/* child device, for detach */
150 };
151 
152 /* the per device structure */
153 struct umass_softc {
154 	struct device		sc_dev;		/* base device */
155 	usbd_device_handle	sc_udev;	/* device */
156 	usbd_interface_handle	sc_iface;	/* interface */
157 	int			sc_ifaceno;	/* interface number */
158 
159 	u_int8_t		sc_epaddr[UMASS_NEP];
160 	usbd_pipe_handle	sc_pipe[UMASS_NEP];
161 	usb_device_request_t	sc_req;
162 
163 	const struct umass_wire_methods *sc_methods;
164 
165 	u_int8_t		sc_wire;	/* wire protocol */
166 #define	UMASS_WPROTO_UNSPEC	0
167 #define	UMASS_WPROTO_BBB	1
168 #define	UMASS_WPROTO_CBI	2
169 #define	UMASS_WPROTO_CBI_I	3
170 
171 	u_int8_t		sc_cmd;		/* command protocol */
172 #define	UMASS_CPROTO_UNSPEC	0
173 #define	UMASS_CPROTO_SCSI	1
174 #define	UMASS_CPROTO_ATAPI	2
175 #define	UMASS_CPROTO_UFI	3
176 #define	UMASS_CPROTO_RBC	4
177 #define UMASS_CPROTO_ISD_ATA	5
178 
179 	u_int32_t		sc_quirks;
180 #define UMASS_QUIRK_WRONG_CSWSIG	0x00000001
181 #define UMASS_QUIRK_WRONG_CSWTAG	0x00000002
182 
183 	u_int32_t		sc_busquirks;
184 
185 	/* Bulk specific variables for transfers in progress */
186 	umass_bbb_cbw_t		cbw;	/* command block wrapper */
187 	umass_bbb_csw_t		csw;	/* command status wrapper*/
188 	/* CBI specific variables for transfers in progress */
189 	umass_cbi_cbl_t		cbl;	/* command block */
190 	umass_cbi_sbl_t		sbl;	/* status block */
191 
192 	/* xfer handles
193 	 * Most of our operations are initiated from interrupt context, so
194 	 * we need to avoid using the one that is in use. We want to avoid
195 	 * allocating them in the interrupt context as well.
196 	 */
197 	/* indices into array below */
198 #define XFER_BBB_CBW		0	/* Bulk-Only */
199 #define XFER_BBB_DATA		1
200 #define XFER_BBB_DCLEAR		2
201 #define XFER_BBB_CSW1		3
202 #define XFER_BBB_CSW2		4
203 #define XFER_BBB_SCLEAR		5
204 #define XFER_BBB_RESET1		6
205 #define XFER_BBB_RESET2		7
206 #define XFER_BBB_RESET3		8
207 
208 #define XFER_CBI_CB		0	/* CBI */
209 #define XFER_CBI_DATA		1
210 #define XFER_CBI_STATUS		2
211 #define XFER_CBI_DCLEAR		3
212 #define XFER_CBI_SCLEAR		4
213 #define XFER_CBI_RESET1		5
214 #define XFER_CBI_RESET2		6
215 #define XFER_CBI_RESET3		7
216 
217 #define XFER_NR			9	/* maximum number */
218 
219 	usbd_xfer_handle	transfer_xfer[XFER_NR]; /* for ctrl xfers */
220 
221 	void			*data_buffer;
222 
223 	int			transfer_dir;		/* data direction */
224 	void			*transfer_data;		/* data buffer */
225 	int			transfer_datalen;	/* (maximum) length */
226 	int			transfer_actlen;	/* actual length */
227 	umass_callback		transfer_cb;		/* callback */
228 	void			*transfer_priv;		/* for callback */
229 	int			transfer_status;
230 
231 	int			transfer_state;
232 #define TSTATE_IDLE			0
233 #define TSTATE_BBB_COMMAND		1	/* CBW transfer */
234 #define TSTATE_BBB_DATA			2	/* Data transfer */
235 #define TSTATE_BBB_DCLEAR		3	/* clear endpt stall */
236 #define TSTATE_BBB_STATUS1		4	/* clear endpt stall */
237 #define TSTATE_BBB_SCLEAR		5	/* clear endpt stall */
238 #define TSTATE_BBB_STATUS2		6	/* CSW transfer */
239 #define TSTATE_BBB_RESET1		7	/* reset command */
240 #define TSTATE_BBB_RESET2		8	/* in clear stall */
241 #define TSTATE_BBB_RESET3		9	/* out clear stall */
242 #define TSTATE_CBI_COMMAND		10	/* command transfer */
243 #define TSTATE_CBI_DATA			11	/* data transfer */
244 #define TSTATE_CBI_STATUS		12	/* status transfer */
245 #define TSTATE_CBI_DCLEAR		13	/* clear ep stall */
246 #define TSTATE_CBI_SCLEAR		14	/* clear ep stall */
247 #define TSTATE_CBI_RESET1		15	/* reset command */
248 #define TSTATE_CBI_RESET2		16	/* in clear stall */
249 #define TSTATE_CBI_RESET3		17	/* out clear stall */
250 #define TSTATE_STATES			18	/* # of states above */
251 
252 
253 	int			timeout;		/* in msecs */
254 
255 	u_int8_t		maxlun;			/* max lun supported */
256 
257 #ifdef UMASS_DEBUG
258 	struct timeval tv;
259 #endif
260 
261 	int			sc_xfer_flags;
262 	char			sc_dying;
263 	int			sc_refcnt;
264 	int			sc_sense;
265 
266 	struct umassbus_softc	*bus;		 /* bus dependent data */
267 
268 	/* For polled transfers */
269 	int			polling_depth;
270 	usbd_status		polled_xfer_status;
271 	usbd_xfer_handle	next_polled_xfer;
272 };
273 
274 #define UMASS_MAX_TRANSFER_SIZE	MAXBSIZE
275