Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
57 | f9daq | 1 | #ifndef __USB_H__ |
2 | #define __USB_H__ |
||
3 | |||
4 | #include <stdlib.h> |
||
5 | |||
6 | /* |
||
7 | * 'interface' is defined somewhere in the Windows header files. This macro |
||
8 | * is deleted here to avoid conflicts and compile errors. |
||
9 | */ |
||
10 | |||
11 | #ifdef interface |
||
12 | #undef interface |
||
13 | #endif |
||
14 | |||
15 | /* |
||
16 | * PATH_MAX from limits.h can't be used on Windows if the dll and |
||
17 | * import libraries are build/used by different compilers |
||
18 | */ |
||
19 | |||
20 | #define LIBUSB_PATH_MAX 512 |
||
21 | |||
22 | |||
23 | /* |
||
24 | * USB spec information |
||
25 | * |
||
26 | * This is all stuff grabbed from various USB specs and is pretty much |
||
27 | * not subject to change |
||
28 | */ |
||
29 | |||
30 | /* |
||
31 | * Device and/or Interface Class codes |
||
32 | */ |
||
33 | #define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */ |
||
34 | #define USB_CLASS_AUDIO 1 |
||
35 | #define USB_CLASS_COMM 2 |
||
36 | #define USB_CLASS_HID 3 |
||
37 | #define USB_CLASS_PRINTER 7 |
||
38 | #define USB_CLASS_MASS_STORAGE 8 |
||
39 | #define USB_CLASS_HUB 9 |
||
40 | #define USB_CLASS_DATA 10 |
||
41 | #define USB_CLASS_VENDOR_SPEC 0xff |
||
42 | |||
43 | /* |
||
44 | * Descriptor types |
||
45 | */ |
||
46 | #define USB_DT_DEVICE 0x01 |
||
47 | #define USB_DT_CONFIG 0x02 |
||
48 | #define USB_DT_STRING 0x03 |
||
49 | #define USB_DT_INTERFACE 0x04 |
||
50 | #define USB_DT_ENDPOINT 0x05 |
||
51 | |||
52 | #define USB_DT_HID 0x21 |
||
53 | #define USB_DT_REPORT 0x22 |
||
54 | #define USB_DT_PHYSICAL 0x23 |
||
55 | #define USB_DT_HUB 0x29 |
||
56 | |||
57 | /* |
||
58 | * Descriptor sizes per descriptor type |
||
59 | */ |
||
60 | #define USB_DT_DEVICE_SIZE 18 |
||
61 | #define USB_DT_CONFIG_SIZE 9 |
||
62 | #define USB_DT_INTERFACE_SIZE 9 |
||
63 | #define USB_DT_ENDPOINT_SIZE 7 |
||
64 | #define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ |
||
65 | #define USB_DT_HUB_NONVAR_SIZE 7 |
||
66 | |||
67 | |||
68 | /* ensure byte-packed structures */ |
||
69 | #include <pshpack1.h> |
||
70 | |||
71 | |||
72 | /* All standard descriptors have these 2 fields in common */ |
||
73 | struct usb_descriptor_header { |
||
74 | unsigned char bLength; |
||
75 | unsigned char bDescriptorType; |
||
76 | }; |
||
77 | |||
78 | /* String descriptor */ |
||
79 | struct usb_string_descriptor { |
||
80 | unsigned char bLength; |
||
81 | unsigned char bDescriptorType; |
||
82 | unsigned short wData[1]; |
||
83 | }; |
||
84 | |||
85 | /* HID descriptor */ |
||
86 | struct usb_hid_descriptor { |
||
87 | unsigned char bLength; |
||
88 | unsigned char bDescriptorType; |
||
89 | unsigned short bcdHID; |
||
90 | unsigned char bCountryCode; |
||
91 | unsigned char bNumDescriptors; |
||
92 | }; |
||
93 | |||
94 | /* Endpoint descriptor */ |
||
95 | #define USB_MAXENDPOINTS 32 |
||
96 | struct usb_endpoint_descriptor { |
||
97 | unsigned char bLength; |
||
98 | unsigned char bDescriptorType; |
||
99 | unsigned char bEndpointAddress; |
||
100 | unsigned char bmAttributes; |
||
101 | unsigned short wMaxPacketSize; |
||
102 | unsigned char bInterval; |
||
103 | unsigned char bRefresh; |
||
104 | unsigned char bSynchAddress; |
||
105 | |||
106 | unsigned char *extra; /* Extra descriptors */ |
||
107 | int extralen; |
||
108 | }; |
||
109 | |||
110 | #define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ |
||
111 | #define USB_ENDPOINT_DIR_MASK 0x80 |
||
112 | |||
113 | #define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */ |
||
114 | #define USB_ENDPOINT_TYPE_CONTROL 0 |
||
115 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 1 |
||
116 | #define USB_ENDPOINT_TYPE_BULK 2 |
||
117 | #define USB_ENDPOINT_TYPE_INTERRUPT 3 |
||
118 | |||
119 | /* Interface descriptor */ |
||
120 | #define USB_MAXINTERFACES 32 |
||
121 | struct usb_interface_descriptor { |
||
122 | unsigned char bLength; |
||
123 | unsigned char bDescriptorType; |
||
124 | unsigned char bInterfaceNumber; |
||
125 | unsigned char bAlternateSetting; |
||
126 | unsigned char bNumEndpoints; |
||
127 | unsigned char bInterfaceClass; |
||
128 | unsigned char bInterfaceSubClass; |
||
129 | unsigned char bInterfaceProtocol; |
||
130 | unsigned char iInterface; |
||
131 | |||
132 | struct usb_endpoint_descriptor *endpoint; |
||
133 | |||
134 | unsigned char *extra; /* Extra descriptors */ |
||
135 | int extralen; |
||
136 | }; |
||
137 | |||
138 | #define USB_MAXALTSETTING 128 /* Hard limit */ |
||
139 | |||
140 | struct usb_interface { |
||
141 | struct usb_interface_descriptor *altsetting; |
||
142 | |||
143 | int num_altsetting; |
||
144 | }; |
||
145 | |||
146 | /* Configuration descriptor information.. */ |
||
147 | #define USB_MAXCONFIG 8 |
||
148 | struct usb_config_descriptor { |
||
149 | unsigned char bLength; |
||
150 | unsigned char bDescriptorType; |
||
151 | unsigned short wTotalLength; |
||
152 | unsigned char bNumInterfaces; |
||
153 | unsigned char bConfigurationValue; |
||
154 | unsigned char iConfiguration; |
||
155 | unsigned char bmAttributes; |
||
156 | unsigned char MaxPower; |
||
157 | |||
158 | struct usb_interface *interface; |
||
159 | |||
160 | unsigned char *extra; /* Extra descriptors */ |
||
161 | int extralen; |
||
162 | }; |
||
163 | |||
164 | /* Device descriptor */ |
||
165 | struct usb_device_descriptor { |
||
166 | unsigned char bLength; |
||
167 | unsigned char bDescriptorType; |
||
168 | unsigned short bcdUSB; |
||
169 | unsigned char bDeviceClass; |
||
170 | unsigned char bDeviceSubClass; |
||
171 | unsigned char bDeviceProtocol; |
||
172 | unsigned char bMaxPacketSize0; |
||
173 | unsigned short idVendor; |
||
174 | unsigned short idProduct; |
||
175 | unsigned short bcdDevice; |
||
176 | unsigned char iManufacturer; |
||
177 | unsigned char iProduct; |
||
178 | unsigned char iSerialNumber; |
||
179 | unsigned char bNumConfigurations; |
||
180 | }; |
||
181 | |||
182 | struct usb_ctrl_setup { |
||
183 | unsigned char bRequestType; |
||
184 | unsigned char bRequest; |
||
185 | unsigned short wValue; |
||
186 | unsigned short wIndex; |
||
187 | unsigned short wLength; |
||
188 | }; |
||
189 | |||
190 | /* |
||
191 | * Standard requests |
||
192 | */ |
||
193 | #define USB_REQ_GET_STATUS 0x00 |
||
194 | #define USB_REQ_CLEAR_FEATURE 0x01 |
||
195 | /* 0x02 is reserved */ |
||
196 | #define USB_REQ_SET_FEATURE 0x03 |
||
197 | /* 0x04 is reserved */ |
||
198 | #define USB_REQ_SET_ADDRESS 0x05 |
||
199 | #define USB_REQ_GET_DESCRIPTOR 0x06 |
||
200 | #define USB_REQ_SET_DESCRIPTOR 0x07 |
||
201 | #define USB_REQ_GET_CONFIGURATION 0x08 |
||
202 | #define USB_REQ_SET_CONFIGURATION 0x09 |
||
203 | #define USB_REQ_GET_INTERFACE 0x0A |
||
204 | #define USB_REQ_SET_INTERFACE 0x0B |
||
205 | #define USB_REQ_SYNCH_FRAME 0x0C |
||
206 | |||
207 | #define USB_TYPE_STANDARD (0x00 << 5) |
||
208 | #define USB_TYPE_CLASS (0x01 << 5) |
||
209 | #define USB_TYPE_VENDOR (0x02 << 5) |
||
210 | #define USB_TYPE_RESERVED (0x03 << 5) |
||
211 | |||
212 | #define USB_RECIP_DEVICE 0x00 |
||
213 | #define USB_RECIP_INTERFACE 0x01 |
||
214 | #define USB_RECIP_ENDPOINT 0x02 |
||
215 | #define USB_RECIP_OTHER 0x03 |
||
216 | |||
217 | /* |
||
218 | * Various libusb API related stuff |
||
219 | */ |
||
220 | |||
221 | #define USB_ENDPOINT_IN 0x80 |
||
222 | #define USB_ENDPOINT_OUT 0x00 |
||
223 | |||
224 | /* Error codes */ |
||
225 | #define USB_ERROR_BEGIN 500000 |
||
226 | |||
227 | /* |
||
228 | * This is supposed to look weird. This file is generated from autoconf |
||
229 | * and I didn't want to make this too complicated. |
||
230 | */ |
||
231 | #define USB_LE16_TO_CPU(x) |
||
232 | |||
233 | /* Data types */ |
||
234 | /* struct usb_device; */ |
||
235 | /* struct usb_bus; */ |
||
236 | |||
237 | struct usb_device { |
||
238 | struct usb_device *next, *prev; |
||
239 | |||
240 | char filename[LIBUSB_PATH_MAX]; |
||
241 | |||
242 | struct usb_bus *bus; |
||
243 | |||
244 | struct usb_device_descriptor descriptor; |
||
245 | struct usb_config_descriptor *config; |
||
246 | |||
247 | void *dev; /* Darwin support */ |
||
248 | |||
249 | unsigned char devnum; |
||
250 | |||
251 | unsigned char num_children; |
||
252 | struct usb_device **children; |
||
253 | }; |
||
254 | |||
255 | struct usb_bus { |
||
256 | struct usb_bus *next, *prev; |
||
257 | |||
258 | char dirname[LIBUSB_PATH_MAX]; |
||
259 | |||
260 | struct usb_device *devices; |
||
261 | unsigned long location; |
||
262 | |||
263 | struct usb_device *root_dev; |
||
264 | }; |
||
265 | |||
266 | /* Version information, Windows specific */ |
||
267 | struct usb_version { |
||
268 | struct { |
||
269 | int major; |
||
270 | int minor; |
||
271 | int micro; |
||
272 | int nano; |
||
273 | } dll; |
||
274 | struct { |
||
275 | int major; |
||
276 | int minor; |
||
277 | int micro; |
||
278 | int nano; |
||
279 | } driver; |
||
280 | }; |
||
281 | |||
282 | |||
283 | struct usb_dev_handle; |
||
284 | typedef struct usb_dev_handle usb_dev_handle; |
||
285 | |||
286 | /* Variables */ |
||
287 | struct usb_bus *usb_busses; |
||
288 | |||
289 | #include <poppack.h> |
||
290 | |||
291 | |||
292 | #ifdef __cplusplus |
||
293 | extern "C" { |
||
294 | #endif |
||
295 | |||
296 | /* Function prototypes */ |
||
297 | |||
298 | /* usb.c */ |
||
299 | usb_dev_handle *usb_open(struct usb_device *dev); |
||
300 | int usb_close(usb_dev_handle *dev); |
||
301 | int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf, |
||
302 | size_t buflen); |
||
303 | int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf, |
||
304 | size_t buflen); |
||
305 | |||
306 | /* descriptors.c */ |
||
307 | int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep, |
||
308 | unsigned char type, unsigned char index, |
||
309 | void *buf, int size); |
||
310 | int usb_get_descriptor(usb_dev_handle *udev, unsigned char type, |
||
311 | unsigned char index, void *buf, int size); |
||
312 | |||
313 | /* <arch>.c */ |
||
314 | int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, |
||
315 | int timeout); |
||
316 | int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, |
||
317 | int timeout); |
||
318 | int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, |
||
319 | int timeout); |
||
320 | int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size, |
||
321 | int timeout); |
||
322 | int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, |
||
323 | int value, int index, char *bytes, int size, |
||
324 | int timeout); |
||
325 | int usb_set_configuration(usb_dev_handle *dev, int configuration); |
||
326 | int usb_claim_interface(usb_dev_handle *dev, int interface); |
||
327 | int usb_release_interface(usb_dev_handle *dev, int interface); |
||
328 | int usb_set_altinterface(usb_dev_handle *dev, int alternate); |
||
329 | int usb_resetep(usb_dev_handle *dev, unsigned int ep); |
||
330 | int usb_clear_halt(usb_dev_handle *dev, unsigned int ep); |
||
331 | int usb_reset(usb_dev_handle *dev); |
||
332 | |||
333 | char *usb_strerror(void); |
||
334 | |||
335 | void usb_init(void); |
||
336 | void usb_set_debug(int level); |
||
337 | int usb_find_busses(void); |
||
338 | int usb_find_devices(void); |
||
339 | struct usb_device *usb_device(usb_dev_handle *dev); |
||
340 | struct usb_bus *usb_get_busses(void); |
||
341 | |||
342 | |||
343 | /* Windows specific functions */ |
||
344 | |||
345 | #define LIBUSB_HAS_INSTALL_SERVICE_NP 1 |
||
346 | int usb_install_service_np(void); |
||
347 | |||
348 | #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1 |
||
349 | int usb_uninstall_service_np(void); |
||
350 | |||
351 | #define LIBUSB_HAS_INSTALL_DRIVER_NP 1 |
||
352 | int usb_install_driver_np(const char *inf_file); |
||
353 | |||
354 | const struct usb_version *usb_get_version(void); |
||
355 | |||
356 | int usb_isochronous_setup_async(usb_dev_handle *dev, void **context, |
||
357 | unsigned char ep, int pktsize); |
||
358 | int usb_bulk_setup_async(usb_dev_handle *dev, void **context, |
||
359 | unsigned char ep); |
||
360 | int usb_interrupt_setup_async(usb_dev_handle *dev, void **context, |
||
361 | unsigned char ep); |
||
362 | |||
363 | int usb_submit_async(void *context, char *bytes, int size); |
||
364 | int usb_reap_async(void *context, int timeout); |
||
365 | int usb_free_async(void **context); |
||
366 | |||
367 | |||
368 | #ifdef __cplusplus |
||
369 | } |
||
370 | #endif |
||
371 | |||
372 | #endif /* __USB_H__ */ |
||
373 |