xref: /netbsd-src/sys/dev/usb/usbdi.h (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: usbdi.h,v 1.75 2007/08/15 04:00:34 kiyohara Exp $	*/
2 /*	$FreeBSD: src/sys/dev/usb/usbdi.h,v 1.18 1999/11/17 22:33:49 n_hibma Exp $	*/
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 #ifndef _USBDI_H_
42 #define _USBDI_H_
43 
44 typedef struct usbd_bus		*usbd_bus_handle;
45 typedef struct usbd_device	*usbd_device_handle;
46 typedef struct usbd_interface	*usbd_interface_handle;
47 typedef struct usbd_pipe	*usbd_pipe_handle;
48 typedef struct usbd_xfer	*usbd_xfer_handle;
49 typedef void			*usbd_private_handle;
50 
51 typedef enum {		/* keep in sync with usbd_status_msgs */
52 	USBD_NORMAL_COMPLETION = 0, /* must be 0 */
53 	USBD_IN_PROGRESS,	/* 1 */
54 	/* errors */
55 	USBD_PENDING_REQUESTS,	/* 2 */
56 	USBD_NOT_STARTED,	/* 3 */
57 	USBD_INVAL,		/* 4 */
58 	USBD_NOMEM,		/* 5 */
59 	USBD_CANCELLED,		/* 6 */
60 	USBD_BAD_ADDRESS,	/* 7 */
61 	USBD_IN_USE,		/* 8 */
62 	USBD_NO_ADDR,		/* 9 */
63 	USBD_SET_ADDR_FAILED,	/* 10 */
64 	USBD_NO_POWER,		/* 11 */
65 	USBD_TOO_DEEP,		/* 12 */
66 	USBD_IOERROR,		/* 13 */
67 	USBD_NOT_CONFIGURED,	/* 14 */
68 	USBD_TIMEOUT,		/* 15 */
69 	USBD_SHORT_XFER,	/* 16 */
70 	USBD_STALLED,		/* 17 */
71 	USBD_INTERRUPTED,	/* 18 */
72 
73 	USBD_ERROR_MAX		/* must be last */
74 } usbd_status;
75 
76 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
77 			      usbd_status);
78 
79 /* Open flags */
80 #define USBD_EXCLUSIVE_USE	0x01
81 
82 /* Use default (specified by ep. desc.) interval on interrupt pipe */
83 #define USBD_DEFAULT_INTERVAL	(-1)
84 
85 /* Request flags */
86 #define USBD_NO_COPY		0x01	/* do not copy data to DMA buffer */
87 #define USBD_SYNCHRONOUS	0x02	/* wait for completion */
88 /* in usb.h #define USBD_SHORT_XFER_OK	0x04*/	/* allow short reads */
89 #define USBD_FORCE_SHORT_XFER	0x08	/* force last short packet on write */
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 #define DEVINFOSIZE 1024
99 
100 usbd_status usbd_open_pipe(usbd_interface_handle, u_int8_t,
101 			   u_int8_t, usbd_pipe_handle *);
102 usbd_status usbd_close_pipe(usbd_pipe_handle);
103 usbd_status usbd_transfer(usbd_xfer_handle);
104 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
105 usbd_status usbd_free_xfer(usbd_xfer_handle);
106 void usbd_setup_xfer(usbd_xfer_handle, usbd_pipe_handle,
107 		     usbd_private_handle, void *,
108 		     u_int32_t, u_int16_t, u_int32_t,
109 		     usbd_callback);
110 void usbd_setup_default_xfer(usbd_xfer_handle, usbd_device_handle,
111 			     usbd_private_handle, u_int32_t,
112 			     usb_device_request_t *, void *,
113 			     u_int32_t, u_int16_t, usbd_callback);
114 void usbd_setup_isoc_xfer(usbd_xfer_handle, usbd_pipe_handle,
115 			  usbd_private_handle, u_int16_t *,
116 			  u_int32_t, u_int16_t, usbd_callback);
117 void usbd_get_xfer_status(usbd_xfer_handle, usbd_private_handle *,
118 			  void **, u_int32_t *, usbd_status *);
119 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
120 			(usbd_interface_handle, u_int8_t);
121 usbd_status usbd_abort_pipe(usbd_pipe_handle);
122 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle);
123 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle);
124 void usbd_clear_endpoint_toggle(usbd_pipe_handle);
125 usbd_status usbd_endpoint_count(usbd_interface_handle, u_int8_t *);
126 usbd_status usbd_interface_count(usbd_device_handle, u_int8_t *);
127 void usbd_interface2device_handle(usbd_interface_handle,
128 					 usbd_device_handle *);
129 usbd_status usbd_device2interface_handle(usbd_device_handle,
130 			      u_int8_t, usbd_interface_handle *);
131 
132 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
133 void *usbd_alloc_buffer(usbd_xfer_handle, u_int32_t);
134 void usbd_free_buffer(usbd_xfer_handle);
135 void *usbd_get_buffer(usbd_xfer_handle);
136 usbd_status usbd_sync_transfer(usbd_xfer_handle);
137 usbd_status usbd_open_pipe_intr(usbd_interface_handle, u_int8_t,
138 				u_int8_t, usbd_pipe_handle *,
139 				usbd_private_handle, void *,
140 				u_int32_t, usbd_callback, int);
141 usbd_status usbd_do_request(usbd_device_handle, usb_device_request_t *, void *);
142 usbd_status usbd_do_request_async(usbd_device_handle,
143 				  usb_device_request_t *, void *);
144 usbd_status usbd_do_request_flags(usbd_device_handle, usb_device_request_t *,
145 				  void *, u_int16_t, int*, u_int32_t);
146 usbd_status usbd_do_request_flags_pipe(usbd_device_handle, usbd_pipe_handle,
147 	usb_device_request_t *, void *, u_int16_t, int *, u_int32_t);
148 usb_interface_descriptor_t *usbd_get_interface_descriptor
149 				(usbd_interface_handle);
150 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle);
151 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle);
152 usbd_status usbd_set_interface(usbd_interface_handle, int);
153 int usbd_get_no_alts(usb_config_descriptor_t *, int);
154 usbd_status  usbd_get_interface(usbd_interface_handle, u_int8_t *);
155 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
156 #ifdef COMPAT_30
157 void usbd_fill_deviceinfo_old(usbd_device_handle, struct usb_device_info_old *,
158     int);
159 #endif
160 int usbd_get_interface_altindex(usbd_interface_handle);
161 
162 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
163 					    int, int);
164 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
165 					   int, int, int);
166 
167 void usbd_dopoll(usbd_interface_handle);
168 void usbd_set_polling(usbd_device_handle, int);
169 
170 const char *usbd_errstr(usbd_status);
171 
172 void usbd_add_dev_event(int, usbd_device_handle);
173 void usbd_add_drv_event(int, usbd_device_handle, device_ptr_t);
174 
175 char *usbd_devinfo_alloc(usbd_device_handle, int);
176 void usbd_devinfo_free(char *);
177 
178 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
179 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
180 			(usbd_interface_handle, u_int8_t);
181 
182 usbd_status usbd_reload_device_desc(usbd_device_handle);
183 
184 int usbd_ratecheck(struct timeval *);
185 
186 usbd_status usbd_get_string(usbd_device_handle, int, char *);
187 usbd_status usbd_get_string0(usbd_device_handle, int, char *, int);
188 
189 /* An iterator for descriptors. */
190 typedef struct {
191 	const uByte *cur;
192 	const uByte *end;
193 } usbd_desc_iter_t;
194 void usb_desc_iter_init(usbd_device_handle, usbd_desc_iter_t *);
195 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *);
196 
197 /*
198  * The usb_task structs form a queue of things to run in the USB event
199  * thread.  Normally this is just device discovery when a connect/disconnect
200  * has been detected.  But it may also be used by drivers that need to
201  * perform (short) tasks that must have a process context.
202  */
203 struct usb_task {
204 	TAILQ_ENTRY(usb_task) next;
205 	void (*fun)(void *);
206 	void *arg;
207 	int queue;
208 };
209 #define	USB_TASKQ_HC		0
210 #define	USB_TASKQ_DRIVER	1
211 #define	USB_NUM_TASKQS		2
212 #define	USB_TASKQ_NAMES		{"usbtask-hc", "usbtask-dr"}
213 
214 void usb_add_task(usbd_device_handle, struct usb_task *, int);
215 void usb_rem_task(usbd_device_handle, struct usb_task *);
216 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->queue = -1)
217 
218 struct usb_devno {
219 	u_int16_t ud_vendor;
220 	u_int16_t ud_product;
221 };
222 const struct usb_devno *usb_match_device(const struct usb_devno *,
223 	u_int, u_int, u_int16_t, u_int16_t);
224 #define usb_lookup(tbl, vendor, product) \
225 	usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
226 #define	USB_PRODUCT_ANY		0xffff
227 
228 /* NetBSD attachment information */
229 
230 /* Attach data */
231 struct usb_attach_arg {
232 	int			port;
233 	int			vendor;
234 	int			product;
235 	int			release;
236 	usbd_device_handle	device;	/* current device */
237 	int			class, subclass, proto;
238 	int			usegeneric;
239 };
240 
241 struct usbif_attach_arg {
242 	int			port;
243 	int			configno;
244 	int			ifaceno;
245 	int			vendor;
246 	int			product;
247 	int			release;
248 	usbd_device_handle	device;	/* current device */
249 
250 	usbd_interface_handle	iface; /* current interface */
251 	int			class, subclass, proto;
252 
253 	/* XXX need accounting for interfaces not matched to */
254 
255 	usbd_interface_handle  *ifaces;	/* all interfaces */
256 	int			nifaces; /* number of interfaces */
257 };
258 
259 /* Match codes. */
260 #define UMATCH_HIGHEST					15
261 /* First five codes is for a whole device. */
262 #define UMATCH_VENDOR_PRODUCT_REV			14
263 #define UMATCH_VENDOR_PRODUCT				13
264 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO			12
265 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO		11
266 #define UMATCH_DEVCLASS_DEVSUBCLASS			10
267 /* Next six codes are for interfaces. */
268 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE		 9
269 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE		 8
270 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO		 7
271 #define UMATCH_VENDOR_IFACESUBCLASS			 6
272 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO	 5
273 #define UMATCH_IFACECLASS_IFACESUBCLASS			 4
274 #define UMATCH_IFACECLASS				 3
275 #define UMATCH_IFACECLASS_GENERIC			 2
276 /* Generic driver */
277 #define UMATCH_GENERIC					 1
278 /* No match */
279 #define UMATCH_NONE					 0
280 
281 /* XXX Perhaps USB should have its own levels? */
282 #define splusb splsoftnet
283 #define splhardusb splbio
284 #define IPL_USB IPL_BIO
285 #define splsoftusb splusb
286 #define IPL_SOFTUSB IPL_SOFTNET
287 #define IPL_HARDUSB IPL_USB
288 
289 
290 #endif /* _USBDI_H_ */
291