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