]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - lib/libusb/libusb.h
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / lib / libusb / libusb.h
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #ifndef __LIBUSB_H__
28 #define __LIBUSB_H__
29
30 #include <sys/time.h>
31 #include <sys/types.h>
32
33 #ifdef __cplusplus
34 extern  "C" {
35 #endif
36 #if 0
37 }                                       /* indent fix */
38
39 #endif
40
41 /* libusb enums */
42
43 enum libusb_class_code {
44         LIBUSB_CLASS_PER_INTERFACE = 0,
45         LIBUSB_CLASS_AUDIO = 1,
46         LIBUSB_CLASS_COMM = 2,
47         LIBUSB_CLASS_HID = 3,
48         LIBUSB_CLASS_PTP = 6,
49         LIBUSB_CLASS_PRINTER = 7,
50         LIBUSB_CLASS_MASS_STORAGE = 8,
51         LIBUSB_CLASS_HUB = 9,
52         LIBUSB_CLASS_DATA = 10,
53         LIBUSB_CLASS_VENDOR_SPEC = 0xff,
54 };
55
56 enum libusb_descriptor_type {
57         LIBUSB_DT_DEVICE = 0x01,
58         LIBUSB_DT_CONFIG = 0x02,
59         LIBUSB_DT_STRING = 0x03,
60         LIBUSB_DT_INTERFACE = 0x04,
61         LIBUSB_DT_ENDPOINT = 0x05,
62         LIBUSB_DT_HID = 0x21,
63         LIBUSB_DT_REPORT = 0x22,
64         LIBUSB_DT_PHYSICAL = 0x23,
65         LIBUSB_DT_HUB = 0x29,
66 };
67
68 #define LIBUSB_DT_DEVICE_SIZE           18
69 #define LIBUSB_DT_CONFIG_SIZE           9
70 #define LIBUSB_DT_INTERFACE_SIZE        9
71 #define LIBUSB_DT_ENDPOINT_SIZE         7
72 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE   9
73 #define LIBUSB_DT_HUB_NONVAR_SIZE       7
74
75 #define LIBUSB_ENDPOINT_ADDRESS_MASK    0x0f
76 #define LIBUSB_ENDPOINT_DIR_MASK        0x80
77
78 enum libusb_endpoint_direction {
79         LIBUSB_ENDPOINT_IN = 0x80,
80         LIBUSB_ENDPOINT_OUT = 0x00,
81 };
82
83 #define LIBUSB_TRANSFER_TYPE_MASK       0x03
84
85 enum libusb_transfer_type {
86         LIBUSB_TRANSFER_TYPE_CONTROL = 0,
87         LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
88         LIBUSB_TRANSFER_TYPE_BULK = 2,
89         LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
90 };
91
92 enum libusb_standard_request {
93         LIBUSB_REQUEST_GET_STATUS = 0x00,
94         LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
95         LIBUSB_REQUEST_SET_FEATURE = 0x03,
96         LIBUSB_REQUEST_SET_ADDRESS = 0x05,
97         LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
98         LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
99         LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
100         LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
101         LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
102         LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
103         LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
104 };
105
106 enum libusb_request_type {
107         LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
108         LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
109         LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
110         LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
111 };
112
113 enum libusb_request_recipient {
114         LIBUSB_RECIPIENT_DEVICE = 0x00,
115         LIBUSB_RECIPIENT_INTERFACE = 0x01,
116         LIBUSB_RECIPIENT_ENDPOINT = 0x02,
117         LIBUSB_RECIPIENT_OTHER = 0x03,
118 };
119
120 #define LIBUSB_ISO_SYNC_TYPE_MASK       0x0C
121
122 enum libusb_iso_sync_type {
123         LIBUSB_ISO_SYNC_TYPE_NONE = 0,
124         LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
125         LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
126         LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
127 };
128
129 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
130
131 enum libusb_iso_usage_type {
132         LIBUSB_ISO_USAGE_TYPE_DATA = 0,
133         LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
134         LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
135 };
136
137 enum libusb_error {
138         LIBUSB_SUCCESS = 0,
139         LIBUSB_ERROR_IO = -1,
140         LIBUSB_ERROR_INVALID_PARAM = -2,
141         LIBUSB_ERROR_ACCESS = -3,
142         LIBUSB_ERROR_NO_DEVICE = -4,
143         LIBUSB_ERROR_NOT_FOUND = -5,
144         LIBUSB_ERROR_BUSY = -6,
145         LIBUSB_ERROR_TIMEOUT = -7,
146         LIBUSB_ERROR_OVERFLOW = -8,
147         LIBUSB_ERROR_PIPE = -9,
148         LIBUSB_ERROR_INTERRUPTED = -10,
149         LIBUSB_ERROR_NO_MEM = -11,
150         LIBUSB_ERROR_NOT_SUPPORTED = -12,
151         LIBUSB_ERROR_OTHER = -99,
152 };
153
154 enum libusb_speed {
155         LIBUSB_SPEED_UNKNOWN = 0,
156         LIBUSB_SPEED_LOW = 1,
157         LIBUSB_SPEED_FULL = 2,
158         LIBUSB_SPEED_HIGH = 3,
159         LIBUSB_SPEED_SUPER = 4,
160 };
161
162 enum libusb_transfer_status {
163         LIBUSB_TRANSFER_COMPLETED,
164         LIBUSB_TRANSFER_ERROR,
165         LIBUSB_TRANSFER_TIMED_OUT,
166         LIBUSB_TRANSFER_CANCELLED,
167         LIBUSB_TRANSFER_STALL,
168         LIBUSB_TRANSFER_NO_DEVICE,
169         LIBUSB_TRANSFER_OVERFLOW,
170 };
171
172 enum libusb_transfer_flags {
173         LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0,
174         LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1,
175         LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2,
176 };
177
178 enum libusb_debug_level {
179         LIBUSB_DEBUG_NO=0,
180         LIBUSB_DEBUG_FUNCTION=1,
181         LIBUSB_DEBUG_TRANSFER=2,
182 };
183
184 /* libusb structures */
185
186 struct libusb_context;
187 struct libusb_device;
188 struct libusb_transfer;
189 struct libusb_device_handle;
190
191 struct libusb_pollfd {
192         int     fd;
193         short   events;
194 };
195
196 typedef struct libusb_context libusb_context;
197 typedef struct libusb_device libusb_device;
198 typedef struct libusb_device_handle libusb_device_handle;
199 typedef struct libusb_pollfd libusb_pollfd;
200 typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data);
201 typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data);
202
203 typedef struct libusb_device_descriptor {
204         uint8_t bLength;
205         uint8_t bDescriptorType;
206         uint16_t bcdUSB;
207         uint8_t bDeviceClass;
208         uint8_t bDeviceSubClass;
209         uint8_t bDeviceProtocol;
210         uint8_t bMaxPacketSize0;
211         uint16_t idVendor;
212         uint16_t idProduct;
213         uint16_t bcdDevice;
214         uint8_t iManufacturer;
215         uint8_t iProduct;
216         uint8_t iSerialNumber;
217         uint8_t bNumConfigurations;
218 }       libusb_device_descriptor;
219
220 typedef struct libusb_endpoint_descriptor {
221         uint8_t bLength;
222         uint8_t bDescriptorType;
223         uint8_t bEndpointAddress;
224         uint8_t bmAttributes;
225         uint16_t wMaxPacketSize;
226         uint8_t bInterval;
227         uint8_t bRefresh;
228         uint8_t bSynchAddress;
229         uint8_t *extra;
230         int     extra_length;
231 }       libusb_endpoint_descriptor __aligned(sizeof(void *));
232
233 typedef struct libusb_interface_descriptor {
234         uint8_t bLength;
235         uint8_t bDescriptorType;
236         uint8_t bInterfaceNumber;
237         uint8_t bAlternateSetting;
238         uint8_t bNumEndpoints;
239         uint8_t bInterfaceClass;
240         uint8_t bInterfaceSubClass;
241         uint8_t bInterfaceProtocol;
242         uint8_t iInterface;
243         struct libusb_endpoint_descriptor *endpoint;
244         uint8_t *extra;
245         int     extra_length;
246 }       libusb_interface_descriptor __aligned(sizeof(void *));
247
248 typedef struct libusb_interface {
249         struct libusb_interface_descriptor *altsetting;
250         int     num_altsetting;
251 }       libusb_interface __aligned(sizeof(void *));
252
253 typedef struct libusb_config_descriptor {
254         uint8_t bLength;
255         uint8_t bDescriptorType;
256         uint16_t wTotalLength;
257         uint8_t bNumInterfaces;
258         uint8_t bConfigurationValue;
259         uint8_t iConfiguration;
260         uint8_t bmAttributes;
261         uint8_t MaxPower;
262         struct libusb_interface *interface;
263         uint8_t *extra;
264         int     extra_length;
265 }       libusb_config_descriptor __aligned(sizeof(void *));
266
267 typedef struct libusb_control_setup {
268         uint8_t bmRequestType;
269         uint8_t bRequest;
270         uint16_t wValue;
271         uint16_t wIndex;
272         uint16_t wLength;
273 }       libusb_control_setup;
274
275 #define LIBUSB_CONTROL_SETUP_SIZE       8       /* bytes */
276
277 typedef struct libusb_iso_packet_descriptor {
278         uint32_t length;
279         uint32_t actual_length;
280         enum libusb_transfer_status status;
281 }       libusb_iso_packet_descriptor __aligned(sizeof(void *));
282
283 typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer);
284
285 typedef struct libusb_transfer {
286         libusb_device_handle *dev_handle;
287         uint8_t flags;
288         uint32_t endpoint;
289         uint8_t type;
290         uint32_t timeout;
291         enum libusb_transfer_status status;
292         int     length;
293         int     actual_length;
294         libusb_transfer_cb_fn callback;
295         void   *user_data;
296         uint8_t *buffer;
297         void *os_priv;
298         int     num_iso_packets;
299         struct libusb_iso_packet_descriptor iso_packet_desc[0];
300 }       libusb_transfer __aligned(sizeof(void *));
301
302 /* Library initialisation */
303
304 void    libusb_set_debug(libusb_context * ctx, int level);
305 const char *libusb_strerror(int code);
306 const char *libusb_error_name(int code);
307 int     libusb_init(libusb_context ** context);
308 void    libusb_exit(struct libusb_context *ctx);
309
310 /* Device handling and enumeration */
311
312 ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list);
313 void    libusb_free_device_list(libusb_device ** list, int unref_devices);
314 uint8_t libusb_get_bus_number(libusb_device * dev);
315 uint8_t libusb_get_device_address(libusb_device * dev);
316 enum libusb_speed libusb_get_device_speed(libusb_device * dev);
317 int     libusb_clear_halt(libusb_device_handle *devh, uint8_t endpoint);
318 int     libusb_get_max_packet_size(libusb_device * dev, uint8_t endpoint);
319 libusb_device *libusb_ref_device(libusb_device * dev);
320 void    libusb_unref_device(libusb_device * dev);
321 int     libusb_open(libusb_device * dev, libusb_device_handle ** devh);
322 libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id);
323 void    libusb_close(libusb_device_handle * devh);
324 libusb_device *libusb_get_device(libusb_device_handle * devh);
325 int     libusb_get_configuration(libusb_device_handle * devh, int *config);
326 int     libusb_set_configuration(libusb_device_handle * devh, int configuration);
327 int     libusb_claim_interface(libusb_device_handle * devh, int interface_number);
328 int     libusb_release_interface(libusb_device_handle * devh, int interface_number);
329 int     libusb_reset_device(libusb_device_handle * devh);
330 int     libusb_check_connected(libusb_device_handle * devh);
331 int     libusb_kernel_driver_active(libusb_device_handle * devh, int interface);
332 int     libusb_get_driver_np(libusb_device_handle * devh, int interface, char *name, int namelen);
333 int     libusb_get_driver(libusb_device_handle * devh, int interface, char *name, int namelen);
334 int     libusb_detach_kernel_driver_np(libusb_device_handle * devh, int interface);
335 int     libusb_detach_kernel_driver(libusb_device_handle * devh, int interface);
336 int     libusb_attach_kernel_driver(libusb_device_handle * devh, int interface);
337 int     libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting);
338
339 /* USB Descriptors */
340
341 int     libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc);
342 int     libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config);
343 int     libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config);
344 int     libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
345 void    libusb_free_config_descriptor(struct libusb_config_descriptor *config);
346 int     libusb_get_string_descriptor_ascii(libusb_device_handle * devh, uint8_t desc_index, uint8_t *data, int length);
347 int     libusb_get_descriptor(libusb_device_handle * devh, uint8_t desc_type, uint8_t desc_index, uint8_t *data, int length);
348
349 /* Asynchronous device I/O */
350
351 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
352 void    libusb_free_transfer(struct libusb_transfer *transfer);
353 int     libusb_submit_transfer(struct libusb_transfer *transfer);
354 int     libusb_cancel_transfer(struct libusb_transfer *transfer);
355 uint8_t *libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index);
356 uint8_t *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index);
357 void    libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length);
358 uint8_t *libusb_control_transfer_get_data(struct libusb_transfer *transfer);
359 struct libusb_control_setup *libusb_control_transfer_get_setup(struct libusb_transfer *transfer);
360 void    libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength);
361 void    libusb_fill_control_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t *buf, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout);
362 void    libusb_fill_bulk_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout);
363 void    libusb_fill_interrupt_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout);
364 void    libusb_fill_iso_transfer(struct libusb_transfer *transfer, libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf, int length, int npacket, libusb_transfer_cb_fn callback, void *user_data, uint32_t timeout);
365
366 /* Polling and timing */
367
368 int     libusb_try_lock_events(libusb_context * ctx);
369 void    libusb_lock_events(libusb_context * ctx);
370 void    libusb_unlock_events(libusb_context * ctx);
371 int     libusb_event_handling_ok(libusb_context * ctx);
372 int     libusb_event_handler_active(libusb_context * ctx);
373 void    libusb_lock_event_waiters(libusb_context * ctx);
374 void    libusb_unlock_event_waiters(libusb_context * ctx);
375 int     libusb_wait_for_event(libusb_context * ctx, struct timeval *tv);
376 int     libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv);
377 int     libusb_handle_events(libusb_context * ctx);
378 int     libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv);
379 int     libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv);
380 void    libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data);
381 struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx);
382
383 /* Synchronous device I/O */
384
385 int     libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint8_t *data, uint16_t wLength, uint32_t timeout);
386 int     libusb_bulk_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout);
387 int     libusb_interrupt_transfer(libusb_device_handle * devh, uint8_t endpoint, uint8_t *data, int length, int *transferred, uint32_t timeout);
388
389 /* Byte-order */
390
391 uint16_t libusb_cpu_to_le16(uint16_t x);
392 uint16_t libusb_le16_to_cpu(uint16_t x);
393
394 #if 0
395 {                                       /* indent fix */
396 #endif
397 #ifdef __cplusplus
398 }
399
400 #endif
401
402 #endif                                  /* __LIBUSB_H__ */