Update TODO list
[trut64.git] / libusb / usb.h.in
blob665f7cf643c6e559c3aa6b88ceab5ab4901df182
1 /*
2 * Prototypes, structure definitions and macros.
4 * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is covered by the LGPL, read LICENSE for details.
8 * This file (and only this file) may alternatively be licensed under the
9 * BSD license as well, read LICENSE for details.
11 #ifndef __USB_H__
12 #define __USB_H__
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <stdint.h>
17 #include <limits.h>
18 #include <sys/param.h>
19 #include <dirent.h>
22 * USB spec information
24 * This is all stuff grabbed from various USB specs and is pretty much
25 * not subject to change
29 * Device and/or Interface Class codes
31 #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
32 #define USB_CLASS_AUDIO 1
33 #define USB_CLASS_COMM 2
34 #define USB_CLASS_HID 3
35 #define USB_CLASS_PRINTER 7
36 #define USB_CLASS_PTP 6
37 #define USB_CLASS_MASS_STORAGE 8
38 #define USB_CLASS_HUB 9
39 #define USB_CLASS_DATA 10
40 #define USB_CLASS_VENDOR_SPEC 0xff
43 * Descriptor types
45 #define USB_DT_DEVICE 0x01
46 #define USB_DT_CONFIG 0x02
47 #define USB_DT_STRING 0x03
48 #define USB_DT_INTERFACE 0x04
49 #define USB_DT_ENDPOINT 0x05
51 #define USB_DT_HID 0x21
52 #define USB_DT_REPORT 0x22
53 #define USB_DT_PHYSICAL 0x23
54 #define USB_DT_HUB 0x29
57 * Descriptor sizes per descriptor type
59 #define USB_DT_DEVICE_SIZE 18
60 #define USB_DT_CONFIG_SIZE 9
61 #define USB_DT_INTERFACE_SIZE 9
62 #define USB_DT_ENDPOINT_SIZE 7
63 #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
64 #define USB_DT_HUB_NONVAR_SIZE 7
66 /* All standard descriptors have these 2 fields in common */
67 struct usb_descriptor_header {
68 uint8_t bLength;
69 uint8_t bDescriptorType;
70 } __attribute__ ((packed));
72 /* String descriptor */
73 struct usb_string_descriptor {
74 uint8_t bLength;
75 uint8_t bDescriptorType;
76 uint16_t wData[1];
77 } __attribute__ ((packed));
79 /* HID descriptor */
80 struct usb_hid_descriptor {
81 uint8_t bLength;
82 uint8_t bDescriptorType;
83 uint16_t bcdHID;
84 uint8_t bCountryCode;
85 uint8_t bNumDescriptors;
86 /* uint8_t bReportDescriptorType; */
87 /* uint16_t wDescriptorLength; */
88 /* ... */
89 } __attribute__ ((packed));
91 /* Endpoint descriptor */
92 #define USB_MAXENDPOINTS 32
93 struct usb_endpoint_descriptor {
94 uint8_t bLength;
95 uint8_t bDescriptorType;
96 uint8_t bEndpointAddress;
97 uint8_t bmAttributes;
98 uint16_t wMaxPacketSize;
99 uint8_t bInterval;
100 uint8_t bRefresh;
101 uint8_t bSynchAddress;
103 unsigned char *extra; /* Extra descriptors */
104 int extralen;
107 #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
108 #define USB_ENDPOINT_DIR_MASK 0x80
110 #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */
111 #define USB_ENDPOINT_TYPE_CONTROL 0
112 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
113 #define USB_ENDPOINT_TYPE_BULK 2
114 #define USB_ENDPOINT_TYPE_INTERRUPT 3
116 /* Interface descriptor */
117 #define USB_MAXINTERFACES 32
118 struct usb_interface_descriptor {
119 uint8_t bLength;
120 uint8_t bDescriptorType;
121 uint8_t bInterfaceNumber;
122 uint8_t bAlternateSetting;
123 uint8_t bNumEndpoints;
124 uint8_t bInterfaceClass;
125 uint8_t bInterfaceSubClass;
126 uint8_t bInterfaceProtocol;
127 uint8_t iInterface;
129 struct usb_endpoint_descriptor *endpoint;
131 unsigned char *extra; /* Extra descriptors */
132 int extralen;
135 #define USB_MAXALTSETTING 128 /* Hard limit */
136 struct usb_interface {
137 struct usb_interface_descriptor *altsetting;
139 int num_altsetting;
142 /* Configuration descriptor information.. */
143 #define USB_MAXCONFIG 8
144 struct usb_config_descriptor {
145 uint8_t bLength;
146 uint8_t bDescriptorType;
147 uint16_t wTotalLength;
148 uint8_t bNumInterfaces;
149 uint8_t bConfigurationValue;
150 uint8_t iConfiguration;
151 uint8_t bmAttributes;
152 uint8_t MaxPower;
154 struct usb_interface *interface;
156 unsigned char *extra; /* Extra descriptors */
157 int extralen;
160 /* Device descriptor */
161 struct usb_device_descriptor {
162 uint8_t bLength;
163 uint8_t bDescriptorType;
164 uint16_t bcdUSB;
165 uint8_t bDeviceClass;
166 uint8_t bDeviceSubClass;
167 uint8_t bDeviceProtocol;
168 uint8_t bMaxPacketSize0;
169 uint16_t idVendor;
170 uint16_t idProduct;
171 uint16_t bcdDevice;
172 uint8_t iManufacturer;
173 uint8_t iProduct;
174 uint8_t iSerialNumber;
175 uint8_t bNumConfigurations;
176 } __attribute__ ((packed));
178 struct usb_ctrl_setup {
179 uint8_t bRequestType;
180 uint8_t bRequest;
181 uint16_t wValue;
182 uint16_t wIndex;
183 uint16_t wLength;
184 } __attribute__ ((packed));
187 * Standard requests
189 #define USB_REQ_GET_STATUS 0x00
190 #define USB_REQ_CLEAR_FEATURE 0x01
191 /* 0x02 is reserved */
192 #define USB_REQ_SET_FEATURE 0x03
193 /* 0x04 is reserved */
194 #define USB_REQ_SET_ADDRESS 0x05
195 #define USB_REQ_GET_DESCRIPTOR 0x06
196 #define USB_REQ_SET_DESCRIPTOR 0x07
197 #define USB_REQ_GET_CONFIGURATION 0x08
198 #define USB_REQ_SET_CONFIGURATION 0x09
199 #define USB_REQ_GET_INTERFACE 0x0A
200 #define USB_REQ_SET_INTERFACE 0x0B
201 #define USB_REQ_SYNCH_FRAME 0x0C
203 #define USB_TYPE_STANDARD (0x00 << 5)
204 #define USB_TYPE_CLASS (0x01 << 5)
205 #define USB_TYPE_VENDOR (0x02 << 5)
206 #define USB_TYPE_RESERVED (0x03 << 5)
208 #define USB_RECIP_DEVICE 0x00
209 #define USB_RECIP_INTERFACE 0x01
210 #define USB_RECIP_ENDPOINT 0x02
211 #define USB_RECIP_OTHER 0x03
214 * Various libusb API related stuff
217 #define USB_ENDPOINT_IN 0x80
218 #define USB_ENDPOINT_OUT 0x00
220 /* Error codes */
221 #define USB_ERROR_BEGIN 500000
224 * This is supposed to look weird. This file is generated from autoconf
225 * and I didn't want to make this too complicated.
227 #if @BIGENDIAN@
228 #define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0)
229 #else
230 #define USB_LE16_TO_CPU(x)
231 #endif
233 /* Data types */
234 struct usb_device;
235 struct usb_bus;
238 * To maintain compatibility with applications already built with libusb,
239 * we must only add entries to the end of this structure. NEVER delete or
240 * move members and only change types if you really know what you're doing.
242 struct usb_device {
243 struct usb_device *next, *prev;
245 char filename[PATH_MAX + 1];
247 struct usb_bus *bus;
249 struct usb_device_descriptor descriptor;
250 struct usb_config_descriptor *config;
252 void *dev; /* Darwin support */
254 uint8_t devnum;
256 unsigned char num_children;
257 struct usb_device **children;
260 struct usb_bus {
261 struct usb_bus *next, *prev;
263 char dirname[PATH_MAX + 1];
265 struct usb_device *devices;
266 uint32_t location;
268 struct usb_device *root_dev;
271 struct usb_dev_handle;
272 typedef struct usb_dev_handle usb_dev_handle;
274 /* Variables */
275 extern struct usb_bus *usb_busses;
277 #ifdef __cplusplus
278 extern "C" {
279 #endif
281 /* Function prototypes */
283 /* usb.c */
284 usb_dev_handle *usb_open(struct usb_device *dev);
285 int usb_close(usb_dev_handle *dev);
286 int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
287 size_t buflen);
288 int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
289 size_t buflen);
291 /* descriptors.c */
292 int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
293 unsigned char type, unsigned char index, void *buf, int size);
294 int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
295 unsigned char index, void *buf, int size);
297 /* <arch>.c */
298 int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
299 int timeout);
300 int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
301 int timeout);
302 int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
303 int timeout);
304 int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
305 int timeout);
306 int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
307 int value, int index, char *bytes, int size, int timeout);
308 int usb_set_configuration(usb_dev_handle *dev, int configuration);
309 int usb_claim_interface(usb_dev_handle *dev, int interface);
310 int usb_release_interface(usb_dev_handle *dev, int interface);
311 int usb_set_altinterface(usb_dev_handle *dev, int alternate);
312 int usb_resetep(usb_dev_handle *dev, unsigned int ep);
313 int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
314 int usb_reset(usb_dev_handle *dev);
316 #if @LINUX_API@
317 #define LIBUSB_HAS_GET_DRIVER_NP 1
318 int usb_get_driver_np(usb_dev_handle *dev, int interface, char *name,
319 unsigned int namelen);
320 #define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1
321 int usb_detach_kernel_driver_np(usb_dev_handle *dev, int interface);
323 int usb_bulk_write_low_latency(usb_dev_handle *dev, int ep, char *bytes, int size,
324 int timeout);
325 int usb_interrupt_write_low_latency(usb_dev_handle *dev, int ep, char *bytes, int size,
326 int timeout);
329 int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
330 unsigned char ep);
331 int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
332 unsigned char ep);
334 int usb_submit_async(void *context, char *bytes, int size);
335 int usb_reap_async(void *context, int timeout);
336 int usb_reap_async_nocancel(void *context, int timeout);
337 int usb_async_is_done(void *context);
338 int usb_cancel_async(void *context);
339 int usb_free_async(void *context);
340 int usb_async_get_fd(void *context);
341 int usb_max_read_write();
343 #endif
345 char *usb_strerror(void);
347 void usb_init(void);
348 void usb_set_debug(int level);
349 int usb_find_busses(void);
350 int usb_find_devices(void);
351 struct usb_device *usb_device(usb_dev_handle *dev);
352 struct usb_bus *usb_get_busses(void);
354 #ifdef __cplusplus
356 #endif
358 #endif /* __USB_H__ */