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