1 /*	$OpenBSD: usbdivar.h,v 1.23 2005/03/13 02:54:04 pascoe Exp $ */
2 /*	$NetBSD: usbdivar.h,v 1.70 2002/07/11 21:14:36 augustss Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/usbdivar.h,v 1.11 1999/11/17 22:33:51 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #if defined(__NetBSD__)
43 #include <sys/callout.h>
44 #endif
45 
46 /* From usb_mem.h */
47 DECLARE_USB_DMA_T;
48 
49 struct usbd_xfer;
50 struct usbd_pipe;
51 
52 struct usbd_endpoint {
53 	usb_endpoint_descriptor_t *edesc;
54 	int			refcnt;
55 };
56 
57 struct usbd_bus_methods {
58 	usbd_status	      (*open_pipe)(struct usbd_pipe *pipe);
59 	void		      (*soft_intr)(void *);
60 	void		      (*do_poll)(struct usbd_bus *);
61 	usbd_status	      (*allocm)(struct usbd_bus *, usb_dma_t *,
62 					u_int32_t bufsize);
63 	void		      (*freem)(struct usbd_bus *, usb_dma_t *);
64 	struct usbd_xfer *    (*allocx)(struct usbd_bus *);
65 	void		      (*freex)(struct usbd_bus *, struct usbd_xfer *);
66 };
67 
68 struct usbd_pipe_methods {
69 	usbd_status	      (*transfer)(usbd_xfer_handle xfer);
70 	usbd_status	      (*start)(usbd_xfer_handle xfer);
71 	void		      (*abort)(usbd_xfer_handle xfer);
72 	void		      (*close)(usbd_pipe_handle pipe);
73 	void		      (*cleartoggle)(usbd_pipe_handle pipe);
74 	void		      (*done)(usbd_xfer_handle xfer);
75 };
76 
77 struct usbd_tt {
78 	struct usbd_hub	       *hub;
79 };
80 
81 struct usbd_port {
82 	usb_port_status_t	status;
83 	u_int16_t		power;	/* mA of current on port */
84 	u_int8_t		portno;
85 	u_int8_t		restartcnt;
86 #define USBD_RESTART_MAX 5
87 	u_int8_t		reattach;
88 	struct usbd_device     *device;	/* Connected device */
89 	struct usbd_device     *parent;	/* The ports hub */
90 	struct usbd_tt	       *tt; /* Transaction translator (if any) */
91 };
92 
93 struct usbd_hub {
94 	usbd_status	      (*explore)(usbd_device_handle hub);
95 	void		       *hubsoftc;
96 	usb_hub_descriptor_t	hubdesc;
97 	struct usbd_port        ports[1];
98 };
99 
100 struct usb_softc;
101 
102 /*****/
103 
104 struct usbd_bus {
105 	/* Filled by HC driver */
106 	USBBASEDEVICE		bdev; /* base device, host adapter */
107 	struct usbd_bus_methods	*methods;
108 	u_int32_t		pipe_size; /* size of a pipe struct */
109 	/* Filled by usb driver */
110 	struct usbd_device     *root_hub;
111 	usbd_device_handle	devices[USB_MAX_DEVICES];
112 	char			needs_explore;/* a hub a signalled a change */
113 	char			use_polling;
114 	struct usb_softc       *usbctl;
115 	struct usb_device_stats	stats;
116 	int 			intr_context;
117 	u_int			no_intrs;
118 	int			usbrev;	/* USB revision */
119 #define USBREV_UNKNOWN	0
120 #define USBREV_PRE_1_0	1
121 #define USBREV_1_0	2
122 #define USBREV_1_1	3
123 #define USBREV_2_0	4
124 #define USBREV_STR { "unknown", "pre 1.0", "1.0", "1.1", "2.0" }
125 
126 #ifdef USB_USE_SOFTINTR
127 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
128 	void		       *soft; /* soft interrupt cookie */
129 #else
130 	usb_callout_t		softi;
131 #endif
132 #endif
133 
134 #if defined(__NetBSD__) || defined(__OpenBSD__)
135 	bus_dma_tag_t		dmatag;	/* DMA tag */
136 #endif
137 };
138 
139 struct usbd_device {
140 	struct usbd_bus	       *bus;           /* our controller */
141 	struct usbd_pipe       *default_pipe;  /* pipe 0 */
142 	u_int8_t		address;       /* device addess */
143 	u_int8_t		config;	       /* current configuration # */
144 	u_int8_t		depth;         /* distance from root hub */
145 	u_int8_t		speed;         /* low/full/high speed */
146 	u_int8_t		self_powered;  /* flag for self powered */
147 	u_int16_t		power;         /* mA the device uses */
148 	int16_t			langid;	       /* language for strings */
149 #define USBD_NOLANG (-1)
150 	usb_event_cookie_t	cookie;	       /* unique connection id */
151 	struct usbd_port       *powersrc;      /* upstream hub port, or 0 */
152 	struct usbd_device     *myhub; 	       /* upstream hub */
153 	struct usbd_port       *myhsport;      /* closest high speed port */
154 	struct usbd_endpoint	def_ep;	       /* for pipe 0 */
155 	usb_endpoint_descriptor_t def_ep_desc; /* for pipe 0 */
156 	struct usbd_interface  *ifaces;        /* array of all interfaces */
157 	usb_device_descriptor_t ddesc;         /* device descriptor */
158 	usb_config_descriptor_t *cdesc;	       /* full config descr */
159 	const struct usbd_quirks     *quirks;  /* device quirks, always set */
160 	struct usbd_hub	       *hub;           /* only if this is a hub */
161 	device_ptr_t	       *subdevs;       /* sub-devices, 0 terminated */
162 };
163 
164 struct usbd_interface {
165 	struct usbd_device     *device;
166 	usb_interface_descriptor_t *idesc;
167 	int			index;
168 	int			altindex;
169 	struct usbd_endpoint   *endpoints;
170 	void		       *priv;
171 	LIST_HEAD(, usbd_pipe)	pipes;
172 };
173 
174 struct usbd_pipe {
175 	struct usbd_interface  *iface;
176 	struct usbd_device     *device;
177 	struct usbd_endpoint   *endpoint;
178 	int			refcnt;
179 	char			running;
180 	char			aborting;
181 	SIMPLEQ_HEAD(, usbd_xfer) queue;
182 	LIST_ENTRY(usbd_pipe)	next;
183 
184 	usbd_xfer_handle	intrxfer; /* used for repeating requests */
185 	char			repeat;
186 	int			interval;
187 
188 	/* Filled by HC driver. */
189 	struct usbd_pipe_methods *methods;
190 };
191 
192 struct usbd_xfer {
193 	struct usbd_pipe       *pipe;
194 	void		       *priv;
195 	void		       *buffer;
196 	u_int32_t		length;
197 	u_int32_t		actlen;
198 	u_int16_t		flags;
199 	u_int32_t		timeout;
200 	usbd_status		status;
201 	usbd_callback		callback;
202 	__volatile char		done;
203 #ifdef DIAGNOSTIC
204 	u_int32_t		busy_free;
205 #define XFER_FREE 0x46524545
206 #define XFER_BUSY 0x42555359
207 #define XFER_ONQU 0x4f4e5155
208 #endif
209 
210 	/* For control pipe */
211 	usb_device_request_t	request;
212 
213 	/* For isoc */
214 	u_int16_t		*frlengths;
215 	int			nframes;
216 
217 	/* For memory allocation */
218 	struct usbd_device     *device;
219 	usb_dma_t		dmabuf;
220 
221 	int			rqflags;
222 #define URQ_REQUEST	0x01
223 #define URQ_AUTO_DMABUF	0x10
224 #define URQ_DEV_DMABUF	0x20
225 
226 	SIMPLEQ_ENTRY(usbd_xfer) next;
227 
228 	void		       *hcpriv; /* private use by the HC driver */
229 
230 	usb_callout_t		timeout_handle;
231 };
232 
233 void usbd_init(void);
234 void usbd_finish(void);
235 
236 #ifdef USB_DEBUG
237 void usbd_dump_iface(struct usbd_interface *iface);
238 void usbd_dump_device(struct usbd_device *dev);
239 void usbd_dump_endpoint(struct usbd_endpoint *endp);
240 void usbd_dump_queue(usbd_pipe_handle pipe);
241 void usbd_dump_pipe(usbd_pipe_handle pipe);
242 #endif
243 
244 /* Routines from usb_subr.c */
245 int		usbctlprint(void *, const char *);
246 void		usb_delay_ms(usbd_bus_handle, u_int);
247 usbd_status	usbd_reset_port(usbd_device_handle dev,
248 				int port, usb_port_status_t *ps);
249 usbd_status	usbd_setup_pipe(usbd_device_handle dev,
250 				usbd_interface_handle iface,
251 				struct usbd_endpoint *, int,
252 				usbd_pipe_handle *pipe);
253 usbd_status	usbd_new_device(device_ptr_t parent,
254 				usbd_bus_handle bus, int depth,
255 				int lowspeed, int port,
256 				struct usbd_port *);
257 void		usbd_remove_device(usbd_device_handle, struct usbd_port *);
258 int		usbd_printBCD(char *cp, size_t len, int bcd);
259 usbd_status	usbd_fill_iface_data(usbd_device_handle dev, int i, int a);
260 void		usb_free_device(usbd_device_handle);
261 
262 usbd_status	usb_insert_transfer(usbd_xfer_handle xfer);
263 void		usb_transfer_complete(usbd_xfer_handle xfer);
264 void		usb_disconnect_port(struct usbd_port *up, device_ptr_t);
265 
266 /* Routines from usb.c */
267 void		usb_needs_explore(usbd_device_handle);
268 void		usb_needs_reattach(usbd_device_handle);
269 void		usb_schedsoftintr(struct usbd_bus *);
270 
271 /*
272  * XXX This check is extremely bogus. Bad Bad Bad.
273  */
274 #if defined(DIAGNOSTIC) && 0
275 #define SPLUSBCHECK \
276 	do { int _s = splusb(), _su = splusb(); \
277              if (!cold && _s != _su) printf("SPLUSBCHECK failed 0x%x!=0x%x, %s:%d\n", \
278 				   _s, _su, __FILE__, __LINE__); \
279 	     splx(_s); \
280         } while (0)
281 #else
282 #define SPLUSBCHECK
283 #endif
284 
285 /* Locator stuff. */
286 
287 #if defined(__NetBSD__)
288 #include "locators.h"
289 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
290 /* XXX these values are used to statically bind some elements in the USB tree
291  * to specific driver instances. This should be somehow emulated in FreeBSD
292  * but can be done later on.
293  * The values are copied from the files.usb file in the NetBSD sources.
294  */
295 #define UHUBCF_PORT_DEFAULT -1
296 #define UHUBCF_CONFIGURATION_DEFAULT -1
297 #define UHUBCF_INTERFACE_DEFAULT -1
298 #define UHUBCF_VENDOR_DEFAULT -1
299 #define UHUBCF_PRODUCT_DEFAULT -1
300 #define UHUBCF_RELEASE_DEFAULT -1
301 #endif
302 
303 #if defined (__OpenBSD__)
304 #define	UHUBCF_PORT		0
305 #define	UHUBCF_CONFIGURATION	1
306 #define	UHUBCF_INTERFACE	2
307 #define	UHUBCF_VENDOR		3
308 #define	UHUBCF_PRODUCT		4
309 #define	UHUBCF_RELEASE		5
310 #endif
311 
312 #define	uhubcf_port		cf_loc[UHUBCF_PORT]
313 #define	uhubcf_configuration	cf_loc[UHUBCF_CONFIGURATION]
314 #define	uhubcf_interface	cf_loc[UHUBCF_INTERFACE]
315 #define	uhubcf_vendor		cf_loc[UHUBCF_VENDOR]
316 #define	uhubcf_product		cf_loc[UHUBCF_PRODUCT]
317 #define	uhubcf_release		cf_loc[UHUBCF_RELEASE]
318 #define	UHUB_UNK_PORT		UHUBCF_PORT_DEFAULT /* wildcarded 'port' */
319 #define	UHUB_UNK_CONFIGURATION	UHUBCF_CONFIGURATION_DEFAULT /* wildcarded 'configuration' */
320 #define	UHUB_UNK_INTERFACE	UHUBCF_INTERFACE_DEFAULT /* wildcarded 'interface' */
321 #define	UHUB_UNK_VENDOR		UHUBCF_VENDOR_DEFAULT /* wildcarded 'vendor' */
322 #define	UHUB_UNK_PRODUCT	UHUBCF_PRODUCT_DEFAULT /* wildcarded 'product' */
323 #define	UHUB_UNK_RELEASE	UHUBCF_RELEASE_DEFAULT /* wildcarded 'release' */
324 
325