xref: /freebsd/sys/dev/usb/usbdi.h (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*	$NetBSD: usbdi.h,v 1.40 2000/02/02 07:34:00 augustss Exp $	*/
2 /*	$FreeBSD$	*/
3 
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *        This product includes software developed by the NetBSD
23  *        Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 typedef struct usbd_bus		*usbd_bus_handle;
42 typedef struct usbd_device	*usbd_device_handle;
43 typedef struct usbd_interface	*usbd_interface_handle;
44 typedef struct usbd_pipe	*usbd_pipe_handle;
45 typedef struct usbd_xfer	*usbd_xfer_handle;
46 typedef void			*usbd_private_handle;
47 
48 typedef enum {		/* keep in sync with usbd_status_msgs */
49 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
50 	USBD_IN_PROGRESS,
51 	/* errors */
52 	USBD_PENDING_REQUESTS,
53 	USBD_NOT_STARTED,
54 	USBD_INVAL,
55 	USBD_NOMEM,
56 	USBD_CANCELLED,
57 	USBD_BAD_ADDRESS,
58 	USBD_IN_USE,
59 	USBD_NO_ADDR,
60 	USBD_SET_ADDR_FAILED,
61 	USBD_NO_POWER,
62 	USBD_TOO_DEEP,
63 	USBD_IOERROR,
64 	USBD_NOT_CONFIGURED,
65 	USBD_TIMEOUT,
66 	USBD_SHORT_XFER,
67 	USBD_STALLED,
68 	USBD_INTERRUPTED,
69 
70 	USBD_ERROR_MAX,		/* must be last */
71 } usbd_status;
72 
73 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
74 				   usbd_status);
75 
76 /* Open flags */
77 #define USBD_EXCLUSIVE_USE	0x01
78 
79 /* Use default (specified by ep. desc.) interval on interrupt pipe */
80 #define USBD_DEFAULT_INTERVAL	(-1)
81 
82 /* Request flags */
83 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
84 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
85 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
86 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
87 
88 /* XXX Temporary hack XXX */
89 #define USBD_NO_TSLEEP		0x80	/* XXX use busy wait */
90 
91 #define USBD_NO_TIMEOUT 0
92 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
93 
94 #if defined(__FreeBSD__)
95 #define USB_CDEV_MAJOR 108
96 #endif
97 
98 usbd_status usbd_open_pipe
99 	(usbd_interface_handle iface, u_int8_t address,
100 	 u_int8_t flags, usbd_pipe_handle *pipe);
101 usbd_status usbd_close_pipe	(usbd_pipe_handle pipe);
102 usbd_status usbd_transfer	(usbd_xfer_handle req);
103 usbd_xfer_handle usbd_alloc_xfer (usbd_device_handle);
104 usbd_status usbd_free_xfer	(usbd_xfer_handle xfer);
105 void usbd_setup_xfer
106 	(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
107 	 usbd_private_handle priv, void *buffer,
108 	 u_int32_t length, u_int16_t flags, u_int32_t timeout,
109 	 usbd_callback);
110 void usbd_setup_default_xfer
111 	(usbd_xfer_handle xfer, usbd_device_handle dev,
112 	 usbd_private_handle priv, u_int32_t timeout,
113 	 usb_device_request_t *req,  void *buffer,
114 	 u_int32_t length, u_int16_t flags, usbd_callback);
115 void usbd_setup_isoc_xfer
116 	(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
117 	 usbd_private_handle priv, u_int16_t *frlengths,
118 	 u_int32_t nframes, u_int16_t flags, usbd_callback);
119 void usbd_get_xfer_status
120 	(usbd_xfer_handle xfer, usbd_private_handle *priv,
121 	 void **buffer, u_int32_t *count, usbd_status *status);
122 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
123 	(usbd_interface_handle iface, u_int8_t address);
124 usbd_status usbd_abort_pipe(usbd_pipe_handle pipe);
125 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle pipe);
126 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe);
127 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe);
128 usbd_status usbd_endpoint_count
129 	(usbd_interface_handle dev, u_int8_t *count);
130 usbd_status usbd_interface_count
131 	(usbd_device_handle dev, u_int8_t *count);
132 usbd_status usbd_interface2device_handle
133 	(usbd_interface_handle iface, usbd_device_handle *dev);
134 usbd_status usbd_device2interface_handle
135 	(usbd_device_handle dev, u_int8_t ifaceno, usbd_interface_handle *iface);
136 
137 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
138 void *usbd_alloc_buffer(usbd_xfer_handle req, u_int32_t size);
139 void usbd_free_buffer(usbd_xfer_handle req);
140 void *usbd_get_buffer(usbd_xfer_handle xfer);
141 usbd_status usbd_sync_transfer(usbd_xfer_handle req);
142 usbd_status usbd_open_pipe_intr
143 	(usbd_interface_handle iface, u_int8_t address,
144 	 u_int8_t flags, usbd_pipe_handle *pipe,
145 	 usbd_private_handle priv, void *buffer,
146 	 u_int32_t length, usbd_callback, int);
147 usbd_status usbd_do_request
148 	(usbd_device_handle pipe, usb_device_request_t *req, void *data);
149 usbd_status usbd_do_request_async
150 	(usbd_device_handle pipe, usb_device_request_t *req, void *data);
151 usbd_status usbd_do_request_flags
152 	(usbd_device_handle pipe, usb_device_request_t *req,
153 	 void *data, u_int16_t flags, int *);
154 usb_interface_descriptor_t *usbd_get_interface_descriptor
155 	(usbd_interface_handle iface);
156 usb_config_descriptor_t *usbd_get_config_descriptor
157 	(usbd_device_handle dev);
158 usb_device_descriptor_t *usbd_get_device_descriptor
159 	(usbd_device_handle dev);
160 usbd_status usbd_set_interface(usbd_interface_handle, int);
161 int usbd_get_no_alts(usb_config_descriptor_t *, int);
162 usbd_status	usbd_get_interface
163 	(usbd_interface_handle iface, u_int8_t *aiface);
164 void usbd_fill_deviceinfo
165 	(usbd_device_handle dev, struct usb_device_info *di, int usedev);
166 int usbd_get_interface_altindex(usbd_interface_handle iface);
167 
168 usb_interface_descriptor_t *usbd_find_idesc
169 	(usb_config_descriptor_t *cd, int iindex, int ano);
170 usb_endpoint_descriptor_t *usbd_find_edesc
171 	(usb_config_descriptor_t *cd, int ifaceidx, int altidx,
172 	     int endptidx);
173 
174 void usbd_dopoll(usbd_interface_handle);
175 void usbd_set_polling(usbd_interface_handle iface, int on);
176 
177 const char *usbd_errstr(usbd_status err);
178 
179 void usbd_add_dev_event (int, usbd_device_handle);
180 void usbd_add_drv_event (int, usbd_device_handle, device_ptr_t);
181 
182 void usbd_devinfo(usbd_device_handle, int, char *);
183 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
184 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
185 	(usbd_interface_handle iface, u_int8_t address);
186 
187 usbd_status usbd_reload_device_desc(usbd_device_handle);
188 
189 struct usb_devno {
190 	u_int16_t ud_vendor;
191 	u_int16_t ud_product;
192 };
193 const struct usb_devno *usb_match_device(const struct usb_devno *tbl,
194 	u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
195 #define usb_lookup(tbl, vendor, product) \
196 	usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
197 
198 /* NetBSD attachment information */
199 
200 /* Attach data */
201 struct usb_attach_arg {
202 	int			port;
203 	int			configno;
204 	int			ifaceno;
205 	int			vendor;
206 	int			product;
207 	int			release;
208 	usbd_device_handle	device;	/* current device */
209 	usbd_interface_handle	iface; /* current interface */
210 	int			usegeneric;
211 	usbd_interface_handle  *ifaces;	/* all interfaces */
212 	int			nifaces; /* number of interfaces */
213 };
214 
215 #if defined(__NetBSD__) || defined(__OpenBSD__)
216 /* Match codes. */
217 /* First five codes is for a whole device. */
218 #define UMATCH_VENDOR_PRODUCT_REV			14
219 #define UMATCH_VENDOR_PRODUCT				13
220 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
221 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
222 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
223 /* Next six codes are for interfaces. */
224 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
225 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
226 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
227 #define UMATCH_VENDOR_IFACESUBCLASS			 6
228 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
229 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
230 #define UMATCH_IFACECLASS				 3
231 #define UMATCH_IFACECLASS_GENERIC			 2
232 /* Generic driver */
233 #define UMATCH_GENERIC					 1
234 /* No match */
235 #define UMATCH_NONE					 0
236 
237 #elif defined(__FreeBSD__)
238 /* FreeBSD needs values less than zero */
239 #define UMATCH_VENDOR_PRODUCT_REV			(-10)
240 #define UMATCH_VENDOR_PRODUCT				(-20)
241 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			(-30)
242 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		(-40)
243 #define UMATCH_DEVCLASS_DEVSUBCLASS			(-50)
244 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		(-60)
245 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		(-70)
246 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		(-80)
247 #define UMATCH_VENDOR_IFACESUBCLASS			(-90)
248 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	(-100)
249 #define UMATCH_IFACECLASS_IFACESUBCLASS			(-110)
250 #define UMATCH_IFACECLASS				(-120)
251 #define UMATCH_IFACECLASS_GENERIC			(-130)
252 #define UMATCH_GENERIC					(-140)
253 #define UMATCH_NONE					(ENXIO)
254 
255 #endif
256 
257 #if defined(__FreeBSD__)
258 int usbd_driver_load(module_t mod, int what, void *arg);
259 #endif
260 
261 /*
262  * XXX
263  * splusb MUST be the lowest level interrupt so that within USB callbacks
264  * the level can be raised the appropriate level.
265  * XXX Should probably use a softsplusb.
266  */
267 /* XXX */
268 #define splusb splbio
269 #define splhardusb splbio
270 #define IPL_USB IPL_BIO
271 /* XXX */
272