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