eXpand your USB potential
libusb.h
1 /*
2  * Public libusbx header file
3  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
4  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
5  * Copyright © 2012 Pete Batard <pete@akeo.ie>
6  * Copyright © 2012 Nathan Hjelm <hjelmn@cs.unm.edu>
7  * For more information, please visit: http://libusbx.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #ifndef LIBUSB_H
25 #define LIBUSB_H
26 
27 #ifdef _MSC_VER
28 /* on MS environments, the inline keyword is available in C++ only */
29 #if !defined(__cplusplus)
30 #define inline __inline
31 #endif
32 /* ssize_t is also not available (copy/paste from MinGW) */
33 #ifndef _SSIZE_T_DEFINED
34 #define _SSIZE_T_DEFINED
35 #undef ssize_t
36 #ifdef _WIN64
37  typedef __int64 ssize_t;
38 #else
39  typedef int ssize_t;
40 #endif /* _WIN64 */
41 #endif /* _SSIZE_T_DEFINED */
42 #endif /* _MSC_VER */
43 
44 /* stdint.h is not available on older MSVC */
45 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
46 typedef unsigned __int8 uint8_t;
47 typedef unsigned __int16 uint16_t;
48 typedef unsigned __int32 uint32_t;
49 #else
50 #include <stdint.h>
51 #endif
52 
53 #if !defined(_WIN32_WCE)
54 #include <sys/types.h>
55 #endif
56 
57 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
58 #include <sys/time.h>
59 #endif
60 
61 #include <time.h>
62 #include <limits.h>
63 
64 /* 'interface' might be defined as a macro on Windows, so we need to
65  * undefine it so as not to break the current libusbx API, because
66  * libusb_config_descriptor has an 'interface' member
67  * As this can be problematic if you include windows.h after libusb.h
68  * in your sources, we force windows.h to be included first. */
69 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
70 #include <windows.h>
71 #if defined(interface)
72 #undef interface
73 #endif
74 #if !defined(__CYGWIN__)
75 #include <winsock.h>
76 #endif
77 #endif
78 
79 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
80 #define LIBUSB_DEPRECATED_FOR(f) \
81  __attribute__((deprecated("Use " #f " instead")))
82 #else
83 #define LIBUSB_DEPRECATED_FOR(f)
84 #endif /* __GNUC__ */
85 
111 /* LIBUSB_CALL must be defined on both definition and declaration of libusbx
112  * functions. You'd think that declaration would be enough, but cygwin will
113  * complain about conflicting types unless both are marked this way.
114  * The placement of this macro is important too; it must appear after the
115  * return type, before the function name. See internal documentation for
116  * API_EXPORTED.
117  */
118 #if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE)
119 #define LIBUSB_CALL WINAPI
120 #else
121 #define LIBUSB_CALL
122 #endif
123 
147 #define LIBUSBX_API_VERSION 0x01000102
148 
149 #ifdef __cplusplus
150 extern "C" {
151 #endif
152 
161 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
162 {
163  union {
164  uint8_t b8[2];
165  uint16_t b16;
166  } _tmp;
167  _tmp.b8[1] = (uint8_t) (x >> 8);
168  _tmp.b8[0] = (uint8_t) (x & 0xff);
169  return _tmp.b16;
170 }
171 
180 #define libusb_le16_to_cpu libusb_cpu_to_le16
181 
182 /* standard USB stuff */
183 
192 
195 
198 
201 
204 
207 
209  LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
210  LIBUSB_CLASS_IMAGE = 6,
211 
214 
217 
220 
223 
226 
229 
232 
235 
238 
241 
244 };
245 
251 
254 
257 
260 
263 
266 
269 
272 
275 
278 
281 
284 
287 };
288 
289 /* Descriptor sizes per descriptor type */
290 #define LIBUSB_DT_DEVICE_SIZE 18
291 #define LIBUSB_DT_CONFIG_SIZE 9
292 #define LIBUSB_DT_INTERFACE_SIZE 9
293 #define LIBUSB_DT_ENDPOINT_SIZE 7
294 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
295 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
296 #define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6
297 #define LIBUSB_DT_BOS_SIZE 5
298 #define LIBUSB_DT_DEVICE_CAPABILITY_SIZE 3
299 
300 /* BOS descriptor sizes */
301 #define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7
302 #define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10
303 #define LIBUSB_BT_CONTAINER_ID_SIZE 20
304 
305 /* We unwrap the BOS => define its max size */
306 #define LIBUSB_DT_BOS_MAX_SIZE ((LIBUSB_DT_BOS_SIZE) +\
307  (LIBUSB_BT_USB_2_0_EXTENSION_SIZE) +\
308  (LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) +\
309  (LIBUSB_BT_CONTAINER_ID_SIZE))
310 
311 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
312 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
313 
321 
324 };
325 
326 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
327 
335 
338 
341 
344 };
345 
351 
354 
355  /* 0x02 is reserved */
356 
359 
360  /* 0x04 is reserved */
361 
364 
367 
370 
373 
376 
379 
382 
385 
388 
392 };
393 
401 
404 
407 
410 };
411 
419 
422 
425 
428 };
429 
430 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
431 
440 
443 
446 
449 };
450 
451 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
452 
461 
464 
467 };
468 
476  uint8_t bLength;
477 
482 
485  uint16_t bcdUSB;
486 
488  uint8_t bDeviceClass;
489 
493 
497 
500 
502  uint16_t idVendor;
503 
505  uint16_t idProduct;
506 
508  uint16_t bcdDevice;
509 
511  uint8_t iManufacturer;
512 
514  uint8_t iProduct;
515 
517  uint8_t iSerialNumber;
518 
521 };
522 
530  uint8_t bLength;
531 
536 
542 
550  uint8_t bmAttributes;
551 
553  uint16_t wMaxPacketSize;
554 
556  uint8_t bInterval;
557 
560  uint8_t bRefresh;
561 
563  uint8_t bSynchAddress;
564 
567  const unsigned char *extra;
568 
571 };
572 
580  uint8_t bLength;
581 
586 
589 
592 
595  uint8_t bNumEndpoints;
596 
599 
603 
607 
609  uint8_t iInterface;
610 
614 
617  const unsigned char *extra;
618 
621 };
622 
630 
633 };
634 
642  uint8_t bLength;
643 
648 
650  uint16_t wTotalLength;
651 
653  uint8_t bNumInterfaces;
654 
657 
659  uint8_t iConfiguration;
660 
662  uint8_t bmAttributes;
663 
667  uint8_t MaxPower;
668 
672 
675  const unsigned char *extra;
676 
679 };
680 
688 
690  uint8_t bLength;
691 
696 
697 
700  uint8_t bMaxBurst;
701 
706  uint8_t bmAttributes;
707 
711 };
712 
720  uint8_t bLength;
728  uint8_t dev_capability_data
729 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
730  [] /* valid C99 code */
731 #else
732  [0] /* non-standard, but usually working code */
733 #endif
734  ;
735 };
736 
744  uint8_t bLength;
745 
750 
752  uint16_t wTotalLength;
753 
756  uint8_t bNumDeviceCaps;
757 
760 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
761  [] /* valid C99 code */
762 #else
763  [0] /* non-standard, but usually working code */
764 #endif
765  ;
766 };
767 
775  uint8_t bLength;
776 
781 
786 
791  uint32_t bmAttributes;
792 };
793 
801  uint8_t bLength;
802 
807 
812 
817  uint8_t bmAttributes;
818 
821  uint16_t wSpeedSupported;
822 
828 
830  uint8_t bU1DevExitLat;
831 
833  uint16_t bU2DevExitLat;
834 };
835 
843  uint8_t bLength;
844 
849 
854 
856  uint8_t bReserved;
857 
859  uint8_t ContainerID[16];
860 };
861 
870  uint8_t bmRequestType;
871 
877  uint8_t bRequest;
878 
880  uint16_t wValue;
881 
884  uint16_t wIndex;
885 
887  uint16_t wLength;
888 };
889 
890 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
891 
892 /* libusbx */
893 
894 struct libusb_context;
895 struct libusb_device;
896 struct libusb_device_handle;
897 struct libusb_hotplug_callback;
898 
904  const uint16_t major;
905 
907  const uint16_t minor;
908 
910  const uint16_t micro;
911 
913  const uint16_t nano;
914 
916  const char *rc;
917 
919  const char* describe;
920 };
921 
940 
957 
958 
968 
975 
978 
981 
984 
987 };
988 
996 
999 
1002 
1005 };
1006 
1015 };
1016 
1025 };
1026 
1033 
1036 
1039 
1042 };
1043 
1054 
1057 
1060 
1063 
1066 
1069 
1072 
1075 
1078 
1081 
1084 
1087 
1090 
1091  /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the
1092  message strings in strerror.c when adding new error codes here. */
1093 
1096 };
1097 
1098 /* Total number of error codes in enum libusb_error */
1099 #define LIBUSB_ERROR_COUNT 14
1100 
1107 
1110 
1113 
1116 
1120 
1123 
1126 
1127  /* NB! Remember to update libusb_error_name()
1128  when adding new status codes here. */
1129 };
1130 
1136 
1139 
1145 
1170 };
1171 
1176  unsigned int length;
1177 
1179  unsigned int actual_length;
1180 
1183 };
1184 
1185 struct libusb_transfer;
1186 
1196 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
1197 
1207 
1209  uint8_t flags;
1210 
1212  unsigned char endpoint;
1213 
1215  unsigned char type;
1216 
1219  unsigned int timeout;
1220 
1229 
1231  int length;
1232 
1237 
1241 
1243  void *user_data;
1244 
1246  unsigned char *buffer;
1247 
1251 
1254 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
1255  [] /* valid C99 code */
1256 #else
1257  [0] /* non-standard, but usually working code */
1258 #endif
1259  ;
1260 };
1261 
1280 };
1281 
1293  LIBUSB_LOG_LEVEL_NONE = 0,
1294  LIBUSB_LOG_LEVEL_ERROR,
1295  LIBUSB_LOG_LEVEL_WARNING,
1296  LIBUSB_LOG_LEVEL_INFO,
1297  LIBUSB_LOG_LEVEL_DEBUG,
1298 };
1299 
1302 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
1303 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
1304 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
1305 const char * LIBUSB_CALL libusb_error_name(int errcode);
1306 int LIBUSB_CALL libusb_setlocale(const char *locale);
1307 const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode);
1308 
1310  libusb_device ***list);
1312  int unref_devices);
1315 
1317  int *config);
1319  struct libusb_device_descriptor *desc);
1321  struct libusb_config_descriptor **config);
1323  uint8_t config_index, struct libusb_config_descriptor **config);
1325  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
1327  struct libusb_config_descriptor *config);
1329  struct libusb_context *ctx,
1330  const struct libusb_endpoint_descriptor *endpoint,
1331  struct libusb_ss_endpoint_companion_descriptor **ep_comp);
1333  struct libusb_ss_endpoint_companion_descriptor *ep_comp);
1335  struct libusb_bos_descriptor **bos);
1338  struct libusb_context *ctx,
1339  struct libusb_bos_dev_capability_descriptor *dev_cap,
1340  struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension);
1342  struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension);
1344  struct libusb_context *ctx,
1345  struct libusb_bos_dev_capability_descriptor *dev_cap,
1346  struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap);
1348  struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap);
1350  struct libusb_bos_dev_capability_descriptor *dev_cap,
1351  struct libusb_container_id_descriptor **container_id);
1353  struct libusb_container_id_descriptor *container_id);
1356 int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len);
1357 LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers)
1358 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1363  unsigned char endpoint);
1365  unsigned char endpoint);
1366 
1370 
1372  int configuration);
1374  int interface_number);
1376  int interface_number);
1377 
1379  libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1380 
1382  int interface_number, int alternate_setting);
1384  unsigned char endpoint);
1386 
1388  int interface_number);
1390  int interface_number);
1392  int interface_number);
1394  libusb_device_handle *dev, int enable);
1395 
1396 /* async I/O */
1397 
1410 static inline unsigned char *libusb_control_transfer_get_data(
1411  struct libusb_transfer *transfer)
1412 {
1413  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1414 }
1415 
1429  struct libusb_transfer *transfer)
1430 {
1431  return (struct libusb_control_setup *)(void *) transfer->buffer;
1432 }
1433 
1457 static inline void libusb_fill_control_setup(unsigned char *buffer,
1458  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1459  uint16_t wLength)
1460 {
1461  struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
1462  setup->bmRequestType = bmRequestType;
1463  setup->bRequest = bRequest;
1464  setup->wValue = libusb_cpu_to_le16(wValue);
1465  setup->wIndex = libusb_cpu_to_le16(wIndex);
1466  setup->wLength = libusb_cpu_to_le16(wLength);
1467 }
1468 
1469 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1470 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1471 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1472 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1473 
1502 static inline void libusb_fill_control_transfer(
1503  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1504  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1505  unsigned int timeout)
1506 {
1507  struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer;
1508  transfer->dev_handle = dev_handle;
1509  transfer->endpoint = 0;
1510  transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1511  transfer->timeout = timeout;
1512  transfer->buffer = buffer;
1513  if (setup)
1514  transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE
1515  + libusb_le16_to_cpu(setup->wLength));
1516  transfer->user_data = user_data;
1517  transfer->callback = callback;
1518 }
1519 
1533 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1534  libusb_device_handle *dev_handle, unsigned char endpoint,
1535  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1536  void *user_data, unsigned int timeout)
1537 {
1538  transfer->dev_handle = dev_handle;
1539  transfer->endpoint = endpoint;
1540  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1541  transfer->timeout = timeout;
1542  transfer->buffer = buffer;
1543  transfer->length = length;
1544  transfer->user_data = user_data;
1545  transfer->callback = callback;
1546 }
1547 
1562  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1563  unsigned char endpoint, unsigned char *buffer, int length,
1564  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1565 {
1566  transfer->dev_handle = dev_handle;
1567  transfer->endpoint = endpoint;
1569  transfer->timeout = timeout;
1570  transfer->buffer = buffer;
1571  transfer->length = length;
1572  transfer->user_data = user_data;
1573  transfer->callback = callback;
1574 }
1575 
1590 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1591  libusb_device_handle *dev_handle, unsigned char endpoint,
1592  unsigned char *buffer, int length, int num_iso_packets,
1593  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1594 {
1595  transfer->dev_handle = dev_handle;
1596  transfer->endpoint = endpoint;
1598  transfer->timeout = timeout;
1599  transfer->buffer = buffer;
1600  transfer->length = length;
1601  transfer->num_iso_packets = num_iso_packets;
1602  transfer->user_data = user_data;
1603  transfer->callback = callback;
1604 }
1605 
1615  struct libusb_transfer *transfer, unsigned int length)
1616 {
1617  int i;
1618  for (i = 0; i < transfer->num_iso_packets; i++)
1619  transfer->iso_packet_desc[i].length = length;
1620 }
1621 
1638 static inline unsigned char *libusb_get_iso_packet_buffer(
1639  struct libusb_transfer *transfer, unsigned int packet)
1640 {
1641  int i;
1642  size_t offset = 0;
1643  int _packet;
1644 
1645  /* oops..slight bug in the API. packet is an unsigned int, but we use
1646  * signed integers almost everywhere else. range-check and convert to
1647  * signed to avoid compiler warnings. FIXME for libusb-2. */
1648  if (packet > INT_MAX)
1649  return NULL;
1650  _packet = (int) packet;
1651 
1652  if (_packet >= transfer->num_iso_packets)
1653  return NULL;
1654 
1655  for (i = 0; i < _packet; i++)
1656  offset += transfer->iso_packet_desc[i].length;
1657 
1658  return transfer->buffer + offset;
1659 }
1660 
1680 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1681  struct libusb_transfer *transfer, unsigned int packet)
1682 {
1683  int _packet;
1684 
1685  /* oops..slight bug in the API. packet is an unsigned int, but we use
1686  * signed integers almost everywhere else. range-check and convert to
1687  * signed to avoid compiler warnings. FIXME for libusb-2. */
1688  if (packet > INT_MAX)
1689  return NULL;
1690  _packet = (int) packet;
1691 
1692  if (_packet >= transfer->num_iso_packets)
1693  return NULL;
1694 
1695  return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet);
1696 }
1697 
1698 /* sync I/O */
1699 
1701  uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1702  unsigned char *data, uint16_t wLength, unsigned int timeout);
1703 
1705  unsigned char endpoint, unsigned char *data, int length,
1706  int *actual_length, unsigned int timeout);
1707 
1709  unsigned char endpoint, unsigned char *data, int length,
1710  int *actual_length, unsigned int timeout);
1711 
1725  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1726 {
1728  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index),
1729  0, data, (uint16_t) length, 1000);
1730 }
1731 
1747  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1748 {
1750  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1751  langid, data, (uint16_t) length, 1000);
1752 }
1753 
1755  uint8_t desc_index, unsigned char *data, int length);
1756 
1757 /* polling and timeouts */
1758 
1766 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1767 
1769  struct timeval *tv);
1771  struct timeval *tv, int *completed);
1775  struct timeval *tv);
1778  struct timeval *tv);
1779 
1785  int fd;
1786 
1791  short events;
1792 };
1793 
1804 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1805  void *user_data);
1806 
1816 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1817 
1819  libusb_context *ctx);
1821  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1822  void *user_data);
1823 
1837 
1843 typedef enum {
1847 
1853 typedef enum {
1856 
1862 
1865 #define LIBUSB_HOTPLUG_MATCH_ANY -1
1866 
1890  libusb_device *device,
1891  libusb_hotplug_event event,
1892  void *user_data);
1893 
1929  libusb_hotplug_event events,
1930  libusb_hotplug_flag flags,
1931  int vendor_id, int product_id,
1932  int dev_class,
1934  void *user_data,
1935  libusb_hotplug_callback_handle *handle);
1936 
1949  libusb_hotplug_callback_handle handle);
1950 
1951 #ifdef __cplusplus
1952 }
1953 #endif
1954 
1955 #endif