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