Sync USB support (host controller part) with FreeBSD6.
[dragonfly.git] / sys / bus / usb / usbdi.h
1 /*      $NetBSD: usbdi.h,v 1.64 2004/10/23 13:26:34 augustss Exp $      */
2 /*      $FreeBSD: src/sys/dev/usb/usbdi.h,v 1.59 2005/05/16 06:58:43 imp Exp $  */
3 /*      $DragonFly: src/sys/bus/usb/usbdi.h,v 1.7 2006/12/10 02:03:57 sephe 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  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #ifndef _USBDI_H_
43 #define _USBDI_H_
44
45 typedef struct usbd_bus         *usbd_bus_handle;
46 typedef struct usbd_device      *usbd_device_handle;
47 typedef struct usbd_interface   *usbd_interface_handle;
48 typedef struct usbd_pipe        *usbd_pipe_handle;
49 typedef struct usbd_xfer        *usbd_xfer_handle;
50 typedef void                    *usbd_private_handle;
51
52 typedef enum {          /* keep in sync with usbd_status_msgs */
53         USBD_NORMAL_COMPLETION = 0, /* must be 0 */
54         USBD_IN_PROGRESS,       /* 1 */
55         /* errors */
56         USBD_PENDING_REQUESTS,  /* 2 */
57         USBD_NOT_STARTED,       /* 3 */
58         USBD_INVAL,             /* 4 */
59         USBD_NOMEM,             /* 5 */
60         USBD_CANCELLED,         /* 6 */
61         USBD_BAD_ADDRESS,       /* 7 */
62         USBD_IN_USE,            /* 8 */
63         USBD_NO_ADDR,           /* 9 */
64         USBD_SET_ADDR_FAILED,   /* 10 */
65         USBD_NO_POWER,          /* 11 */
66         USBD_TOO_DEEP,          /* 12 */
67         USBD_IOERROR,           /* 13 */
68         USBD_NOT_CONFIGURED,    /* 14 */
69         USBD_TIMEOUT,           /* 15 */
70         USBD_SHORT_XFER,        /* 16 */
71         USBD_STALLED,           /* 17 */
72         USBD_INTERRUPTED,       /* 18 */
73
74         USBD_ERROR_MAX          /* must be last */
75 } usbd_status;
76
77 typedef void (*usbd_callback)(usbd_xfer_handle, usbd_private_handle,
78                               usbd_status);
79
80 /* Open flags */
81 #define USBD_EXCLUSIVE_USE      0x01
82
83 /* Use default (specified by ep. desc.) interval on interrupt pipe */
84 #define USBD_DEFAULT_INTERVAL   (-1)
85
86 /* Request flags */
87 #define USBD_NO_COPY            0x01    /* do not copy data to DMA buffer */
88 #define USBD_SYNCHRONOUS        0x02    /* wait for completion */
89 /* in usb.h #define USBD_SHORT_XFER_OK  0x04*/  /* allow short reads */
90 #define USBD_FORCE_SHORT_XFER   0x08    /* force last short packet on write */
91
92 #define USBD_NO_TIMEOUT 0
93 #define USBD_DEFAULT_TIMEOUT 5000 /* ms = 5 s */
94
95 #ifdef __DragonFly__
96 #define USB_CDEV_MAJOR 108
97 #endif
98
99 usbd_status usbd_open_pipe(usbd_interface_handle, u_int8_t,
100                            u_int8_t, usbd_pipe_handle *);
101 usbd_status usbd_close_pipe(usbd_pipe_handle);
102 usbd_status usbd_transfer(usbd_xfer_handle);
103 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
104 usbd_status usbd_free_xfer(usbd_xfer_handle);
105 void usbd_setup_xfer(usbd_xfer_handle, usbd_pipe_handle,
106                      usbd_private_handle, void *,
107                      u_int32_t, u_int16_t, u_int32_t,
108                      usbd_callback);
109 void usbd_setup_default_xfer(usbd_xfer_handle, usbd_device_handle,
110                              usbd_private_handle, u_int32_t,
111                              usb_device_request_t *, void *,
112                              u_int32_t, u_int16_t, usbd_callback);
113 void usbd_setup_isoc_xfer(usbd_xfer_handle, usbd_pipe_handle,
114                           usbd_private_handle, u_int16_t *,
115                           u_int32_t, u_int16_t, usbd_callback);
116 void usbd_get_xfer_status(usbd_xfer_handle, usbd_private_handle *,
117                           void **, u_int32_t *, usbd_status *);
118 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
119                         (usbd_interface_handle, u_int8_t);
120 usbd_status usbd_abort_pipe(usbd_pipe_handle);
121 usbd_status usbd_abort_default_pipe(usbd_device_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 int usbd_get_speed(usbd_device_handle);
153 usbd_status usbd_set_interface(usbd_interface_handle, int);
154 int usbd_get_no_alts(usb_config_descriptor_t *, int);
155 usbd_status  usbd_get_interface(usbd_interface_handle, u_int8_t *);
156 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
157 int usbd_get_interface_altindex(usbd_interface_handle);
158
159 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *,
160                                             int, int);
161 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *,
162                                            int, int, int);
163
164 void usbd_dopoll(usbd_interface_handle);
165 void usbd_set_polling(usbd_device_handle, int);
166
167 const char *usbd_errstr(usbd_status);
168
169 void usbd_add_dev_event(int, usbd_device_handle);
170 void usbd_add_drv_event(int, usbd_device_handle, device_ptr_t);
171
172 void usbd_devinfo(usbd_device_handle, int, char *);
173 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
174 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
175                         (usbd_interface_handle, u_int8_t);
176
177 usbd_status usbd_reload_device_desc(usbd_device_handle);
178
179 int usbd_ratecheck(struct timeval *last);
180
181 usbd_status usbd_get_string(usbd_device_handle dev, int si, char *buf);
182
183 /* An iterator for descriptors. */
184 typedef struct {
185         const uByte *cur;
186         const uByte *end;
187 } usbd_desc_iter_t;
188 void usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter);
189 const usb_descriptor_t *usb_desc_iter_next(usbd_desc_iter_t *iter);
190
191 /*
192  * The usb_task structs form a queue of things to run in the USB event
193  * thread.  Normally this is just device discovery when a connect/disconnect
194  * has been detected.  But it may also be used by drivers that need to
195  * perform (short) tasks that must have a process context.
196  */
197 struct usb_task {
198         TAILQ_ENTRY(usb_task) next;
199         void (*fun)(void *);
200         void *arg;
201         int queue;
202 };
203 #define USB_TASKQ_HC            0
204 #define USB_TASKQ_DRIVER        1
205 #define USB_NUM_TASKQS          2
206 #define USB_TASKQ_NAMES         {"usbtask-hc", "usbtask-dr"}
207
208 void usb_do_task(usbd_device_handle, struct usb_task *, int, int);
209 void usb_add_task(usbd_device_handle, struct usb_task *, int);
210 void usb_rem_task(usbd_device_handle, struct usb_task *);
211 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->queue = -1)
212
213 struct usb_devno {
214         u_int16_t ud_vendor;
215         u_int16_t ud_product;
216 };
217 const struct usb_devno *usb_match_device(const struct usb_devno *,
218         u_int, u_int, u_int16_t, u_int16_t);
219 #define usb_lookup(tbl, vendor, product) \
220         usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
221 #define USB_PRODUCT_ANY         0xffff
222
223 /* NetBSD attachment information */
224
225 /* Attach data */
226 struct usb_attach_arg {
227         int                     port;
228         int                     configno;
229         int                     ifaceno;
230         int                     vendor;
231         int                     product;
232         int                     release;
233         int                     matchlvl;
234         usbd_device_handle      device; /* current device */
235         usbd_interface_handle   iface; /* current interface */
236         int                     usegeneric;
237         usbd_interface_handle  *ifaces; /* all interfaces */
238         int                     nifaces; /* number of interfaces */
239 };
240
241 #if defined(__NetBSD__) || defined(__OpenBSD__)
242 /* Match codes. */
243 /* First five codes is for a whole device. */
244 #define UMATCH_VENDOR_PRODUCT_REV                       14
245 #define UMATCH_VENDOR_PRODUCT                           13
246 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO                 12
247 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO            11
248 #define UMATCH_DEVCLASS_DEVSUBCLASS                     10
249 /* Next six codes are for interfaces. */
250 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE             9
251 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE                 8
252 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO           7
253 #define UMATCH_VENDOR_IFACESUBCLASS                      6
254 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO       5
255 #define UMATCH_IFACECLASS_IFACESUBCLASS                  4
256 #define UMATCH_IFACECLASS                                3
257 #define UMATCH_IFACECLASS_GENERIC                        2
258 /* Generic driver */
259 #define UMATCH_GENERIC                                   1
260 /* No match */
261 #define UMATCH_NONE                                      0
262
263 #elif defined(__FreeBSD__) || defined(__DragonFly__)
264 /* FreeBSD needs values less than zero */
265 #define UMATCH_VENDOR_PRODUCT_REV                       (-10)
266 #define UMATCH_VENDOR_PRODUCT                           (-20)
267 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO                 (-30)
268 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO            (-40)
269 #define UMATCH_DEVCLASS_DEVSUBCLASS                     (-50)
270 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE            (-60)
271 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE                (-70)
272 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO          (-80)
273 #define UMATCH_VENDOR_IFACESUBCLASS                     (-90)
274 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO      (-100)
275 #define UMATCH_IFACECLASS_IFACESUBCLASS                 (-110)
276 #define UMATCH_IFACECLASS                               (-120)
277 #define UMATCH_IFACECLASS_GENERIC                       (-130)
278 #define UMATCH_GENERIC                                  (-140)
279 #define UMATCH_NONE                                     (ENXIO)
280
281 #endif
282
283 #define USBD_SHOW_DEVICE_CLASS          0x1
284 #define USBD_SHOW_INTERFACE_CLASS       0x2
285
286 #if defined(__FreeBSD__) || defined(__DragonFly__)
287 int usbd_driver_load(module_t mod, int what, void *arg);
288
289 static __inline int
290 usb_get_port(device_t dev)
291 {
292         struct usb_attach_arg *uap = device_get_ivars(dev);
293         return (uap->port);
294 }
295
296 static __inline struct usbd_interface *
297 usb_get_iface(device_t dev)
298 {
299         struct usb_attach_arg *uap = device_get_ivars(dev);
300         return (uap->iface);
301 }
302
303 #endif
304
305 #define IPL_USB IPL_BIO
306
307 #endif /* _USBDI_H_ */