xref: /openbsd/sys/dev/usb/usbdi.h (revision 3d8817e4)
1 /*	$OpenBSD: usbdi.h,v 1.42 2011/02/09 20:24:39 jakemsr Exp $ */
2 /*	$NetBSD: usbdi.h,v 1.62 2002/07/11 21:14:35 augustss Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 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  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 typedef struct usbd_bus		*usbd_bus_handle;
36 typedef struct usbd_device	*usbd_device_handle;
37 typedef struct usbd_interface	*usbd_interface_handle;
38 typedef struct usbd_pipe	*usbd_pipe_handle;
39 typedef struct usbd_xfer	*usbd_xfer_handle;
40 typedef void			*usbd_private_handle;
41 
42 typedef enum {		/* keep in sync with usbd_status_msgs */
43 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
44 	USBD_IN_PROGRESS,	/* 1 */
45 	/* errors */
46 	USBD_PENDING_REQUESTS,	/* 2 */
47 	USBD_NOT_STARTED,	/* 3 */
48 	USBD_INVAL,		/* 4 */
49 	USBD_NOMEM,		/* 5 */
50 	USBD_CANCELLED,		/* 6 */
51 	USBD_BAD_ADDRESS,	/* 7 */
52 	USBD_IN_USE,		/* 8 */
53 	USBD_NO_ADDR,		/* 9 */
54 	USBD_SET_ADDR_FAILED,	/* 10 */
55 	USBD_NO_POWER,		/* 11 */
56 	USBD_TOO_DEEP,		/* 12 */
57 	USBD_IOERROR,		/* 13 */
58 	USBD_NOT_CONFIGURED,	/* 14 */
59 	USBD_TIMEOUT,		/* 15 */
60 	USBD_SHORT_XFER,	/* 16 */
61 	USBD_STALLED,		/* 17 */
62 	USBD_INTERRUPTED,	/* 18 */
63 
64 	USBD_ERROR_MAX		/* must be last */
65 } usbd_status;
66 
67 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
68 			      usbd_status);
69 
70 /* Open flags */
71 #define USBD_EXCLUSIVE_USE	0x01
72 
73 /* Use default (specified by ep. desc.) interval on interrupt pipe */
74 #define USBD_DEFAULT_INTERVAL	(-1)
75 
76 /* Request flags */
77 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
78 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
79 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
80 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
81 
82 #define USBD_NO_TIMEOUT 0
83 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
84 
85 #define DEVINFOSIZE 1024
86 
87 usbd_status usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
88     u_int8_t flags, usbd_pipe_handle *pipe);
89 usbd_status usbd_close_pipe(usbd_pipe_handle pipe);
90 usbd_status usbd_transfer(usbd_xfer_handle req);
91 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
92 usbd_status usbd_free_xfer(usbd_xfer_handle xfer);
93 void usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
94     usbd_private_handle priv, void *buffer, u_int32_t length, u_int16_t flags,
95     u_int32_t timeout, usbd_callback);
96 void usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
97     usbd_private_handle priv, u_int32_t timeout, usb_device_request_t *req,
98     void *buffer, u_int32_t length, u_int16_t flags, usbd_callback);
99 void usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
100     usbd_private_handle priv, u_int16_t *frlengths, u_int32_t nframes,
101     u_int16_t flags, usbd_callback);
102 void usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
103     void **buffer, u_int32_t *count, usbd_status *status);
104 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor(
105     usbd_interface_handle iface, u_int8_t address);
106 usbd_status usbd_abort_pipe(usbd_pipe_handle pipe);
107 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle pipe);
108 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe);
109 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe);
110 usbd_status usbd_endpoint_count(usbd_interface_handle dev, u_int8_t *count);
111 usbd_status usbd_interface_count(usbd_device_handle dev, u_int8_t *count);
112 void usbd_interface2device_handle(usbd_interface_handle iface,
113     usbd_device_handle *dev);
114 usbd_status usbd_device2interface_handle(usbd_device_handle dev,
115     u_int8_t ifaceno, usbd_interface_handle *iface);
116 
117 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
118 void *usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size);
119 void usbd_free_buffer(usbd_xfer_handle xfer);
120 void *usbd_get_buffer(usbd_xfer_handle xfer);
121 usbd_status usbd_sync_transfer(usbd_xfer_handle req);
122 usbd_status usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
123     u_int8_t flags, usbd_pipe_handle *pipe, usbd_private_handle priv,
124     void *buffer, u_int32_t length, usbd_callback, int);
125 usbd_status usbd_do_request(usbd_device_handle pipe, usb_device_request_t *req,
126     void *data);
127 usbd_status usbd_do_request_async(usbd_device_handle pipe,
128     usb_device_request_t *req, void *data);
129 usbd_status usbd_do_request_flags(usbd_device_handle pipe,
130     usb_device_request_t *req, void *data, u_int16_t flags, int*, u_int32_t);
131 usbd_status usbd_do_request_flags_pipe( usbd_device_handle dev,
132     usbd_pipe_handle pipe, usb_device_request_t *req, void *data,
133     u_int16_t flags, int *actlen, u_int32_t);
134 usb_interface_descriptor_t *usbd_get_interface_descriptor(
135     usbd_interface_handle iface);
136 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle dev);
137 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle dev);
138 usbd_status usbd_set_interface(usbd_interface_handle, int);
139 int usbd_get_no_alts(usb_config_descriptor_t *, int);
140 usbd_status  usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface);
141 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
142 void usbd_fill_di_task(void *);
143 int usbd_get_interface_altindex(usbd_interface_handle iface);
144 
145 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
146     int iindex, int ano);
147 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd,
148     int ifaceidx, int altidx, int endptidx);
149 
150 void usbd_dopoll(usbd_interface_handle);
151 void usbd_set_polling(usbd_device_handle iface, int on);
152 
153 const char *usbd_errstr(usbd_status err);
154 
155 char *usbd_devinfo_alloc(usbd_device_handle dev, int showclass);
156 void usbd_devinfo_free(char *devinfop);
157 
158 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
159 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor(
160     usbd_interface_handle iface, u_int8_t address);
161 
162 usbd_status usbd_reload_device_desc(usbd_device_handle);
163 
164 int usbd_ratecheck(struct timeval *last);
165 
166 int usbd_get_devcnt(usbd_device_handle);
167 void usbd_claim_iface(usbd_device_handle, int);
168 int usbd_iface_claimed(usbd_device_handle, int);
169 
170 int usbd_is_dying(usbd_device_handle);
171 void usbd_deactivate(usbd_device_handle);
172 
173 void usbd_ref_incr(usbd_device_handle);
174 void usbd_ref_decr(usbd_device_handle);
175 void usbd_ref_wait(usbd_device_handle);
176 
177 /* An iterator for descriptors. */
178 typedef struct {
179 	const uByte *cur;
180 	const uByte *end;
181 } usbd_desc_iter_t;
182 void usb_desc_iter_init(usbd_device_handle, usbd_desc_iter_t *);
183 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
184 
185 /*
186  * The usb_task structs form a queue of things to run in the USB task
187  * threads.  Normally this is just device discovery when a connect/disconnect
188  * has been detected.  But it may also be used by drivers that need to
189  * perform (short) tasks that must have a process context.
190  */
191 struct usb_task {
192 	TAILQ_ENTRY(usb_task) next;
193 	usbd_device_handle dev;
194 	void (*fun)(void *);
195 	void *arg;
196 	char type;
197 #define	USB_TASK_TYPE_GENERIC	0
198 #define USB_TASK_TYPE_EXPLORE	1
199 #define USB_TASK_TYPE_ABORT	2
200 	u_int state;
201 #define	USB_TASK_STATE_NONE	0x0
202 #define	USB_TASK_STATE_ONQ	0x1
203 #define	USB_TASK_STATE_RUN	0x2
204 
205 };
206 
207 void usb_add_task(usbd_device_handle, struct usb_task *);
208 void usb_rem_task(usbd_device_handle, struct usb_task *);
209 void usb_wait_task(usbd_device_handle, struct usb_task *);
210 void usb_rem_wait_task(usbd_device_handle, struct usb_task *);
211 #define usb_init_task(t, f, a, y) \
212 	((t)->fun = (f),	\
213 	(t)->arg = (a),		\
214 	(t)->type = (y),	\
215 	(t)->state = USB_TASK_STATE_NONE)
216 
217 struct usb_devno {
218 	u_int16_t ud_vendor;
219 	u_int16_t ud_product;
220 };
221 const struct usb_devno *usb_match_device(const struct usb_devno *tbl,
222     u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
223 #define usb_lookup(tbl, vendor, product) \
224 	usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
225 #define	USB_PRODUCT_ANY		0xffff
226 
227 /* NetBSD attachment information */
228 
229 /* Attach data */
230 struct usb_attach_arg {
231 	int			port;
232 	int			configno;
233 	int			ifaceno;
234 	int			vendor;
235 	int			product;
236 	int			release;
237 	int			matchlvl;
238 	usbd_device_handle	device;	/* current device */
239 	usbd_interface_handle	iface; /* current interface */
240 	int			usegeneric;
241 	usbd_interface_handle  *ifaces;	/* all interfaces */
242 	int			nifaces; /* number of interfaces */
243 };
244 
245 /* Match codes. */
246 /* First five codes is for a whole device. */
247 #define UMATCH_VENDOR_PRODUCT_REV			14
248 #define UMATCH_VENDOR_PRODUCT				13
249 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
250 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
251 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
252 /* Next six codes are for interfaces. */
253 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
254 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
255 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
256 #define UMATCH_VENDOR_IFACESUBCLASS			 6
257 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
258 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
259 #define UMATCH_IFACECLASS				 3
260 #define UMATCH_IFACECLASS_GENERIC			 2
261 /* Generic driver */
262 #define UMATCH_GENERIC					 1
263 /* No match */
264 #define UMATCH_NONE					 0
265 
266 /* XXX Perhaps USB should have its own levels? */
267 #define splusb splsoftnet
268 #if 0
269 #define	SPLUSBCHECK	splsoftassert(IPL_SOFTNET)
270 #else
271 #define	SPLUSBCHECK	do { /* nothing */ } while (0)
272 #endif
273 #define splhardusb splbio
274 #define IPL_USB IPL_BIO
275