Add a function to retrieve the speed a USB device is running on.
[dragonfly.git] / sys / bus / usb / usbdi.h
1 /*
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.48 2003/07/14 20:31:03 joe Exp $
4  * $DragonFly: src/sys/bus/usb/usbdi.h,v 1.6 2006/12/06 20:14:47 tgen Exp $
5  */
6
7 /*
8  * Copyright (c) 1998 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Lennart Augustsson (lennart@augustsson.net) at
13  * Carlstedt Research & Technology.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *        This product includes software developed by the NetBSD
26  *        Foundation, Inc. and its contributors.
27  * 4. Neither the name of The NetBSD Foundation nor the names of its
28  *    contributors may be used to endorse or promote products derived
29  *    from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
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__) || defined(__DragonFly__)
95 #define USB_CDEV_MAJOR 108
96 #endif
97
98 usbd_status usbd_open_pipe(usbd_interface_handle iface, u_int8_t address,
99                            u_int8_t flags, usbd_pipe_handle *pipe);
100 usbd_status usbd_close_pipe(usbd_pipe_handle pipe);
101 usbd_status usbd_transfer(usbd_xfer_handle req);
102 usbd_xfer_handle usbd_alloc_xfer(usbd_device_handle);
103 usbd_status usbd_free_xfer(usbd_xfer_handle xfer);
104 void usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
105                      usbd_private_handle priv, void *buffer,
106                      u_int32_t length, u_int16_t flags, u_int32_t timeout,
107                      usbd_callback);
108 void usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev,
109                              usbd_private_handle priv, u_int32_t timeout,
110                              usb_device_request_t *req, void *buffer,
111                              u_int32_t length, u_int16_t flags, usbd_callback);
112 void usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
113                           usbd_private_handle priv, u_int16_t *frlengths,
114                           u_int32_t nframes, u_int16_t flags, usbd_callback);
115 void usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv,
116                           void **buffer, u_int32_t *count, usbd_status *status);
117 usb_endpoint_descriptor_t *usbd_interface2endpoint_descriptor
118                         (usbd_interface_handle iface, u_int8_t address);
119 usbd_status usbd_abort_pipe(usbd_pipe_handle pipe);
120 usbd_status usbd_clear_endpoint_stall(usbd_pipe_handle pipe);
121 usbd_status usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe);
122 void usbd_clear_endpoint_toggle(usbd_pipe_handle pipe);
123 usbd_status usbd_endpoint_count(usbd_interface_handle dev, u_int8_t *count);
124 usbd_status usbd_interface_count(usbd_device_handle dev, u_int8_t *count);
125 void usbd_interface2device_handle(usbd_interface_handle iface,
126                                          usbd_device_handle *dev);
127 usbd_status usbd_device2interface_handle(usbd_device_handle dev,
128                               u_int8_t ifaceno, usbd_interface_handle *iface);
129
130 usbd_device_handle usbd_pipe2device_handle(usbd_pipe_handle);
131 void *usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size);
132 void usbd_free_buffer(usbd_xfer_handle xfer);
133 void *usbd_get_buffer(usbd_xfer_handle xfer);
134 usbd_status usbd_sync_transfer(usbd_xfer_handle req);
135 usbd_status usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address,
136                                 u_int8_t flags, usbd_pipe_handle *pipe,
137                                 usbd_private_handle priv, void *buffer,
138                                 u_int32_t length, usbd_callback, int);
139 usbd_status usbd_do_request(usbd_device_handle pipe, usb_device_request_t *req,
140                             void *data);
141 usbd_status usbd_do_request_async(usbd_device_handle pipe,
142                                   usb_device_request_t *req, void *data);
143 usbd_status usbd_do_request_flags(usbd_device_handle pipe,
144                                   usb_device_request_t *req,
145                                   void *data, u_int16_t flags, int*, u_int32_t);
146 usbd_status usbd_do_request_flags_pipe(
147         usbd_device_handle dev, usbd_pipe_handle pipe,
148         usb_device_request_t *req, void *data, u_int16_t flags, int *actlen,
149         u_int32_t);
150 int usbd_get_speed(usbd_device_handle dev);
151 usb_interface_descriptor_t *usbd_get_interface_descriptor
152                                 (usbd_interface_handle iface);
153 usb_config_descriptor_t *usbd_get_config_descriptor(usbd_device_handle dev);
154 usb_device_descriptor_t *usbd_get_device_descriptor(usbd_device_handle dev);
155 usbd_status usbd_set_interface(usbd_interface_handle, int);
156 int usbd_get_no_alts(usb_config_descriptor_t *, int);
157 usbd_status  usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface);
158 void usbd_fill_deviceinfo(usbd_device_handle, struct usb_device_info *, int);
159 int usbd_get_interface_altindex(usbd_interface_handle iface);
160
161 usb_interface_descriptor_t *usbd_find_idesc(usb_config_descriptor_t *cd,
162                                             int iindex, int ano);
163 usb_endpoint_descriptor_t *usbd_find_edesc(usb_config_descriptor_t *cd,
164                                            int ifaceidx, int altidx,
165                                            int endptidx);
166
167 void usbd_dopoll(usbd_interface_handle);
168 void usbd_set_polling(usbd_device_handle dev, int on);
169
170 const char *usbd_errstr(usbd_status err);
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 void usbd_devinfo(usbd_device_handle, int, char *);
176 const struct usbd_quirks *usbd_get_quirks(usbd_device_handle);
177 usb_endpoint_descriptor_t *usbd_get_endpoint_descriptor
178                         (usbd_interface_handle iface, u_int8_t address);
179
180 usbd_status usbd_reload_device_desc(usbd_device_handle);
181
182 int usbd_ratecheck(struct timeval *last);
183
184 /*
185  * The usb_task structs form a queue of things to run in the USB event
186  * thread.  Normally this is just device discovery when a connect/disconnect
187  * has been detected.  But it may also be used by drivers that need to
188  * perform (short) tasks that must have a process context.
189  */
190 struct usb_task {
191         TAILQ_ENTRY(usb_task) next;
192         void (*fun)(void *);
193         void *arg;
194         char onqueue;
195 };
196
197 void usb_add_task(usbd_device_handle dev, struct usb_task *task);
198 void usb_rem_task(usbd_device_handle dev, struct usb_task *task);
199 #define usb_init_task(t, f, a) ((t)->fun = (f), (t)->arg = (a), (t)->onqueue = 0)
200
201 struct usb_devno {
202         u_int16_t ud_vendor;
203         u_int16_t ud_product;
204 };
205 const struct usb_devno *usb_match_device(const struct usb_devno *tbl,
206         u_int nentries, u_int sz, u_int16_t vendor, u_int16_t product);
207 #define usb_lookup(tbl, vendor, product) \
208         usb_match_device((const struct usb_devno *)(tbl), sizeof (tbl) / sizeof ((tbl)[0]), sizeof ((tbl)[0]), (vendor), (product))
209 #define USB_PRODUCT_ANY         0xffff
210
211 /* NetBSD attachment information */
212
213 /* Attach data */
214 struct usb_attach_arg {
215         int                     port;
216         int                     configno;
217         int                     ifaceno;
218         int                     vendor;
219         int                     product;
220         int                     release;
221         int                     matchlvl;
222         usbd_device_handle      device; /* current device */
223         usbd_interface_handle   iface; /* current interface */
224         int                     usegeneric;
225         usbd_interface_handle  *ifaces; /* all interfaces */
226         int                     nifaces; /* number of interfaces */
227 };
228
229 #if defined(__NetBSD__) || defined(__OpenBSD__)
230 /* Match codes. */
231 /* First five codes is for a whole device. */
232 #define UMATCH_VENDOR_PRODUCT_REV                       14
233 #define UMATCH_VENDOR_PRODUCT                           13
234 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO                 12
235 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO            11
236 #define UMATCH_DEVCLASS_DEVSUBCLASS                     10
237 /* Next six codes are for interfaces. */
238 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE             9
239 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE                 8
240 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO           7
241 #define UMATCH_VENDOR_IFACESUBCLASS                      6
242 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO       5
243 #define UMATCH_IFACECLASS_IFACESUBCLASS                  4
244 #define UMATCH_IFACECLASS                                3
245 #define UMATCH_IFACECLASS_GENERIC                        2
246 /* Generic driver */
247 #define UMATCH_GENERIC                                   1
248 /* No match */
249 #define UMATCH_NONE                                      0
250
251 #elif defined(__FreeBSD__) || defined(__DragonFly__)
252 /* FreeBSD needs values less than zero */
253 #define UMATCH_VENDOR_PRODUCT_REV                       (-10)
254 #define UMATCH_VENDOR_PRODUCT                           (-20)
255 #define UMATCH_VENDOR_DEVCLASS_DEVPROTO                 (-30)
256 #define UMATCH_DEVCLASS_DEVSUBCLASS_DEVPROTO            (-40)
257 #define UMATCH_DEVCLASS_DEVSUBCLASS                     (-50)
258 #define UMATCH_VENDOR_PRODUCT_REV_CONF_IFACE            (-60)
259 #define UMATCH_VENDOR_PRODUCT_CONF_IFACE                (-70)
260 #define UMATCH_VENDOR_IFACESUBCLASS_IFACEPROTO          (-80)
261 #define UMATCH_VENDOR_IFACESUBCLASS                     (-90)
262 #define UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO      (-100)
263 #define UMATCH_IFACECLASS_IFACESUBCLASS                 (-110)
264 #define UMATCH_IFACECLASS                               (-120)
265 #define UMATCH_IFACECLASS_GENERIC                       (-130)
266 #define UMATCH_GENERIC                                  (-140)
267 #define UMATCH_NONE                                     (ENXIO)
268
269 #endif
270
271 #if defined(__FreeBSD__) || defined(__DragonFly__)
272 int usbd_driver_load(module_t mod, int what, void *arg);
273 #endif
274