]> cvs.zerfleddert.de Git - proxmark3-svn/blob - include/usb.h
04f5afa65c93e3d61eb62fe9e141bb2943dccdec
[proxmark3-svn] / include / usb.h
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 extern struct usb_bus *usb_busses;
288
289
290 #include <poppack.h>
291
292
293 #ifdef __cplusplus
294 extern "C" {
295 #endif
296
297 /* Function prototypes */
298
299 /* usb.c */
300 usb_dev_handle *usb_open(struct usb_device *dev);
301 int usb_close(usb_dev_handle *dev);
302 int usb_get_string(usb_dev_handle *dev, int index, int langid, char *buf,
303 size_t buflen);
304 int usb_get_string_simple(usb_dev_handle *dev, int index, char *buf,
305 size_t buflen);
306
307 /* descriptors.c */
308 int usb_get_descriptor_by_endpoint(usb_dev_handle *udev, int ep,
309 unsigned char type, unsigned char index,
310 void *buf, int size);
311 int usb_get_descriptor(usb_dev_handle *udev, unsigned char type,
312 unsigned char index, void *buf, int size);
313
314 /* <arch>.c */
315 int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size,
316 int timeout);
317 int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
318 int timeout);
319 int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size,
320 int timeout);
321 int usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes, int size,
322 int timeout);
323 int usb_control_msg(usb_dev_handle *dev, int requesttype, int request,
324 int value, int index, char *bytes, int size,
325 int timeout);
326 int usb_set_configuration(usb_dev_handle *dev, int configuration);
327 int usb_claim_interface(usb_dev_handle *dev, int interface);
328 int usb_release_interface(usb_dev_handle *dev, int interface);
329 int usb_set_altinterface(usb_dev_handle *dev, int alternate);
330 int usb_resetep(usb_dev_handle *dev, unsigned int ep);
331 int usb_clear_halt(usb_dev_handle *dev, unsigned int ep);
332 int usb_reset(usb_dev_handle *dev);
333
334 char *usb_strerror(void);
335
336 void usb_init(void);
337 void usb_set_debug(int level);
338 int usb_find_busses(void);
339 int usb_find_devices(void);
340 struct usb_device *usb_device(usb_dev_handle *dev);
341 struct usb_bus *usb_get_busses(void);
342
343
344 /* Windows specific functions */
345
346 #define LIBUSB_HAS_INSTALL_SERVICE_NP 1
347 int usb_install_service_np(void);
348
349 #define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1
350 int usb_uninstall_service_np(void);
351
352 #define LIBUSB_HAS_INSTALL_DRIVER_NP 1
353 int usb_install_driver_np(const char *inf_file);
354
355 const struct usb_version *usb_get_version(void);
356
357 int usb_isochronous_setup_async(usb_dev_handle *dev, void **context,
358 unsigned char ep, int pktsize);
359 int usb_bulk_setup_async(usb_dev_handle *dev, void **context,
360 unsigned char ep);
361 int usb_interrupt_setup_async(usb_dev_handle *dev, void **context,
362 unsigned char ep);
363
364 int usb_submit_async(void *context, char *bytes, int size);
365 int usb_reap_async(void *context, int timeout);
366 int usb_free_async(void **context);
367
368
369 #ifdef __cplusplus
370 }
371 #endif
372
373 #endif /* __USB_H__ */
374
Impressum, Datenschutz