xref: /openbsd-src/sys/dev/usb/usbdi.h (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: usbdi.h,v 1.62 2014/03/07 09:38:14 mpi 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 #ifndef _USBDI_H_
36 #define _USBDI_H_
37 
38 struct usbd_bus;
39 struct usbd_device;
40 struct usbd_interface;
41 struct usbd_pipe;
42 struct usbd_xfer;
43 
44 typedef enum {
45 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
46 	USBD_IN_PROGRESS,	/* 1 */
47 	/* errors */
48 	USBD_PENDING_REQUESTS,	/* 2 */
49 	USBD_NOT_STARTED,	/* 3 */
50 	USBD_INVAL,		/* 4 */
51 	USBD_NOMEM,		/* 5 */
52 	USBD_CANCELLED,		/* 6 */
53 	USBD_BAD_ADDRESS,	/* 7 */
54 	USBD_IN_USE,		/* 8 */
55 	USBD_NO_ADDR,		/* 9 */
56 	USBD_SET_ADDR_FAILED,	/* 10 */
57 	USBD_NO_POWER,		/* 11 */
58 	USBD_TOO_DEEP,		/* 12 */
59 	USBD_IOERROR,		/* 13 */
60 	USBD_NOT_CONFIGURED,	/* 14 */
61 	USBD_TIMEOUT,		/* 15 */
62 	USBD_SHORT_XFER,	/* 16 */
63 	USBD_STALLED,		/* 17 */
64 	USBD_INTERRUPTED,	/* 18 */
65 
66 	USBD_ERROR_MAX		/* must be last */
67 } usbd_status;
68 
69 typedef void (*usbd_callback)(struct usbd_xfer *, void *, usbd_status);
70 
71 /* Open flags */
72 #define USBD_EXCLUSIVE_USE	0x01
73 
74 /* Use default (specified by ep. desc.) interval on interrupt pipe */
75 #define USBD_DEFAULT_INTERVAL	(-1)
76 
77 /* Request flags */
78 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
79 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
80 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
81 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
82 #define USBD_CATCH		0x10	/* catch signals while sleeping */
83 
84 #define USBD_NO_TIMEOUT 0
85 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
86 
87 #define DEVINFOSIZE 1024
88 
89 usbd_status usbd_open_pipe(struct usbd_interface *iface, u_int8_t address,
90     u_int8_t flags, struct usbd_pipe **pipe);
91 usbd_status usbd_close_pipe(struct usbd_pipe *pipe);
92 usbd_status usbd_transfer(struct usbd_xfer *req);
93 struct usbd_xfer *usbd_alloc_xfer(struct usbd_device *);
94 void usbd_free_xfer(struct usbd_xfer *xfer);
95 void usbd_setup_xfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
96     void *priv, void *buffer, u_int32_t length, u_int16_t flags,
97     u_int32_t timeout, usbd_callback);
98 void usbd_setup_default_xfer(struct usbd_xfer *xfer, struct usbd_device *dev,
99     void *priv, u_int32_t timeout, usb_device_request_t *req,
100     void *buffer, u_int32_t length, u_int16_t flags, usbd_callback);
101 void usbd_setup_isoc_xfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
102     void *priv, u_int16_t *frlengths, u_int32_t nframes,
103     u_int16_t flags, usbd_callback);
104 void usbd_get_xfer_status(struct usbd_xfer *xfer, void **priv,
105     void **buffer, u_int32_t *count, usbd_status *status);
106 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor(
107     struct usbd_interface *iface, u_int8_t address);
108 void usbd_abort_pipe(struct usbd_pipe *pipe);
109 usbd_status usbd_clear_endpoint_stall(struct usbd_pipe *pipe);
110 usbd_status usbd_clear_endpoint_stall_async(struct usbd_pipe *pipe);
111 void usbd_clear_endpoint_toggle(struct usbd_pipe *pipe);
112 int usbd_endpoint_count(struct usbd_interface *dev, u_int8_t *count);
113 int usbd_interface_count(struct usbd_device *dev, u_int8_t *count);
114 usbd_status usbd_device2interface_handle(struct usbd_device *dev,
115     u_int8_t ifaceno, struct usbd_interface **iface);
116 
117 void *usbd_alloc_buffer(struct usbd_xfer *xfer, u_int32_t size);
118 void usbd_free_buffer(struct usbd_xfer *xfer);
119 usbd_status usbd_open_pipe_intr(struct usbd_interface *iface, u_int8_t address,
120     u_int8_t flags, struct usbd_pipe **pipe, void *priv,
121     void *buffer, u_int32_t length, usbd_callback, int);
122 usbd_status usbd_do_request(struct usbd_device *pipe, usb_device_request_t *req,
123     void *data);
124 usbd_status usbd_do_request_async(struct usbd_device *pipe,
125     usb_device_request_t *req, void *data);
126 usbd_status usbd_do_request_flags(struct usbd_device *pipe,
127     usb_device_request_t *req, void *data, u_int16_t flags, int*, u_int32_t);
128 usb_interface_descriptor_t *usbd_get_interface_descriptor(
129     struct usbd_interface *iface);
130 usb_config_descriptor_t *usbd_get_config_descriptor(struct usbd_device *dev);
131 usb_device_descriptor_t *usbd_get_device_descriptor(struct usbd_device *dev);
132 usbd_status usbd_set_interface(struct usbd_interface *, int);
133 int usbd_get_no_alts(usb_config_descriptor_t *, int);
134 void usbd_fill_deviceinfo(struct usbd_device *, struct usb_device_info *, int);
135 usb_config_descriptor_t *usbd_get_cdesc(struct usbd_device *, int, int *);
136 int usbd_get_interface_altindex(struct usbd_interface *iface);
137 
138 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
139     int iindex, int ano);
140 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd,
141     int ifaceidx, int altidx, int endptidx);
142 
143 void usbd_dopoll(struct usbd_device *);
144 void usbd_set_polling(struct usbd_device *iface, int on);
145 
146 const char *usbd_errstr(usbd_status err);
147 
148 const struct usbd_quirks *usbd_get_quirks(struct usbd_device *);
149 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor(
150     struct usbd_interface *iface, u_int8_t address);
151 
152 usbd_status usbd_reload_device_desc(struct usbd_device *);
153 
154 int usbd_ratecheck(struct timeval *last);
155 
156 int usbd_get_devcnt(struct usbd_device *);
157 void usbd_claim_iface(struct usbd_device *, int);
158 int usbd_iface_claimed(struct usbd_device *, int);
159 
160 int usbd_is_dying(struct usbd_device *);
161 void usbd_deactivate(struct usbd_device *);
162 
163 void usbd_ref_incr(struct usbd_device *);
164 void usbd_ref_decr(struct usbd_device *);
165 void usbd_ref_wait(struct usbd_device *);
166 
167 /* An iterator for descriptors. */
168 struct usbd_desc_iter {
169 	const uByte *cur;
170 	const uByte *end;
171 };
172 void usbd_desc_iter_init(struct usbd_device *, struct usbd_desc_iter *);
173 const usb_descriptor_t *usbd_desc_iter_next(struct usbd_desc_iter *);
174 
175 int usbd_str(usb_string_descriptor_t *, int, const char *);
176 
177 /*
178  * The usb_task structs form a queue of things to run in the USB task
179  * threads.  Normally this is just device discovery when a connect/disconnect
180  * has been detected.  But it may also be used by drivers that need to
181  * perform (short) tasks that must have a process context.
182  */
183 struct usb_task {
184 	TAILQ_ENTRY(usb_task) next;
185 	struct usbd_device *dev;
186 	void (*fun)(void *);
187 	void *arg;
188 	char type;
189 #define	USB_TASK_TYPE_GENERIC	0
190 #define USB_TASK_TYPE_EXPLORE	1
191 #define USB_TASK_TYPE_ABORT	2
192 	u_int state;
193 #define	USB_TASK_STATE_NONE	0x0
194 #define	USB_TASK_STATE_ONQ	0x1
195 #define	USB_TASK_STATE_RUN	0x2
196 
197 };
198 
199 void usb_add_task(struct usbd_device *, struct usb_task *);
200 void usb_rem_task(struct usbd_device *, struct usb_task *);
201 void usb_wait_task(struct usbd_device *, struct usb_task *);
202 void usb_rem_wait_task(struct usbd_device *, struct usb_task *);
203 #define usb_init_task(t, f, a, y) \
204 	((t)->fun = (f),	\
205 	(t)->arg = (a),		\
206 	(t)->type = (y),	\
207 	(t)->state = USB_TASK_STATE_NONE)
208 
209 struct usb_devno {
210 	u_int16_t ud_vendor;
211 	u_int16_t ud_product;
212 };
213 const struct usb_devno *usbd_match_device(const struct usb_devno *tbl,
214     u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
215 #define usb_lookup(tbl, vendor, product) \
216 	usbd_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
217 #define	USB_PRODUCT_ANY		0xffff
218 
219 /* Attach data */
220 struct usb_attach_arg {
221 	int			port;
222 	int			configno;
223 	int			ifaceno;
224 	int			vendor;
225 	int			product;
226 	int			release;
227 	struct usbd_device	*device;	/* current device */
228 	struct usbd_interface	*iface; /* current interface */
229 	int			usegeneric;
230 	struct usbd_interface	**ifaces;/* all interfaces */
231 	int			nifaces; /* number of interfaces */
232 };
233 
234 /* Match codes. */
235 /* First five codes is for a whole device. */
236 #define UMATCH_VENDOR_PRODUCT_REV			14
237 #define UMATCH_VENDOR_PRODUCT				13
238 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
239 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
240 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
241 /* Next six codes are for interfaces. */
242 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
243 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
244 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
245 #define UMATCH_VENDOR_IFACESUBCLASS			 6
246 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
247 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
248 #define UMATCH_IFACECLASS				 3
249 #define UMATCH_IFACECLASS_GENERIC			 2
250 /* Generic driver */
251 #define UMATCH_GENERIC					 1
252 /* No match */
253 #define UMATCH_NONE					 0
254 
255 /* XXX Perhaps USB should have its own levels? */
256 #define splusb splsoftnet
257 #if 0
258 #define	SPLUSBCHECK	splsoftassert(IPL_SOFTNET)
259 #else
260 #define	SPLUSBCHECK	do { /* nothing */ } while (0)
261 #endif
262 #define splhardusb splbio
263 #define IPL_USB IPL_BIO
264 
265 #endif /* _USBDI_H_ */
266