xref: /openbsd-src/sys/dev/usb/usbdi.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: usbdi.h,v 1.30 2008/06/26 05:42:19 ray 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 int usbd_get_interface_altindex(usbd_interface_handle iface);
143 
144 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
145     int iindex, int ano);
146 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd,
147     int ifaceidx, int altidx, int endptidx);
148 
149 void usbd_dopoll(usbd_interface_handle);
150 void usbd_set_polling(usbd_device_handle iface, int on);
151 
152 const char *usbd_errstr(usbd_status err);
153 
154 void usbd_add_dev_event(int, usbd_device_handle);
155 void usbd_add_drv_event(int, usbd_device_handle, struct device *);
156 
157 char *usbd_devinfo_alloc(usbd_device_handle dev, int showclass);
158 void usbd_devinfo_free(char *devinfop);
159 
160 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
161 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor(
162     usbd_interface_handle iface, u_int8_t address);
163 
164 usbd_status usbd_reload_device_desc(usbd_device_handle);
165 
166 int usbd_ratecheck(struct timeval *last);
167 
168 /* An iterator for descriptors. */
169 typedef struct {
170 	const uByte *cur;
171 	const uByte *end;
172 } usbd_desc_iter_t;
173 void usb_desc_iter_init(usbd_device_handle, usbd_desc_iter_t *);
174 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
175 
176 /*
177  * The usb_task structs form a queue of things to run in the USB event
178  * thread.  Normally this is just device discovery when a connect/disconnect
179  * has been detected.  But it may also be used by drivers that need to
180  * perform (short) tasks that must have a process context.
181  */
182 struct usb_task {
183 	TAILQ_ENTRY(usb_task) next;
184 	void (*fun)(void *);
185 	void *arg;
186 	char onqueue;
187 };
188 
189 void usb_add_task(usbd_device_handle dev, struct usb_task *task);
190 void usb_rem_task(usbd_device_handle dev, struct usb_task *task);
191 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->onqueue = 0)
192 
193 struct usb_devno {
194 	u_int16_t ud_vendor;
195 	u_int16_t ud_product;
196 };
197 const struct usb_devno *usb_match_device(const struct usb_devno *tbl,
198     u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
199 #define usb_lookup(tbl, vendor, product) \
200 	usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
201 #define	USB_PRODUCT_ANY		0xffff
202 
203 /* NetBSD attachment information */
204 
205 /* Attach data */
206 struct usb_attach_arg {
207 	int			port;
208 	int			configno;
209 	int			ifaceno;
210 	int			vendor;
211 	int			product;
212 	int			release;
213 	int			matchlvl;
214 	usbd_device_handle	device;	/* current device */
215 	usbd_interface_handle	iface; /* current interface */
216 	int			usegeneric;
217 	usbd_interface_handle  *ifaces;	/* all interfaces */
218 	int			nifaces; /* number of interfaces */
219 };
220 
221 /* Match codes. */
222 /* First five codes is for a whole device. */
223 #define UMATCH_VENDOR_PRODUCT_REV			14
224 #define UMATCH_VENDOR_PRODUCT				13
225 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
226 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
227 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
228 /* Next six codes are for interfaces. */
229 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
230 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
231 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
232 #define UMATCH_VENDOR_IFACESUBCLASS			 6
233 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
234 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
235 #define UMATCH_IFACECLASS				 3
236 #define UMATCH_IFACECLASS_GENERIC			 2
237 /* Generic driver */
238 #define UMATCH_GENERIC					 1
239 /* No match */
240 #define UMATCH_NONE					 0
241 
242 /* XXX Perhaps USB should have its own levels? */
243 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
244 #define splusb splsoftnet
245 #else
246 #define splusb splbio
247 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
248 #define splhardusb splbio
249 #define IPL_USB IPL_BIO
250