libusb
libusb.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  * Copyright (C) 2012 Nathan Hjelm <hjelmn@cs.unm.edu>
6  * Copyright (C) 2012 Peter Stuge <peter@stuge.se>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifndef LIBUSB_H
24 #define LIBUSB_H
25 
26 #ifdef _MSC_VER
27 /* on MS environments, the inline keyword is available in C++ only */
28 #define inline __inline
29 /* ssize_t is also not available (copy/paste from MinGW) */
30 #ifndef _SSIZE_T_DEFINED
31 #define _SSIZE_T_DEFINED
32 #undef ssize_t
33 #ifdef _WIN64
34  typedef __int64 ssize_t;
35 #else
36  typedef int ssize_t;
37 #endif /* _WIN64 */
38 #endif /* _SSIZE_T_DEFINED */
39 #endif /* _MSC_VER */
40 
41 /* stdint.h is also not usually available on MS */
42 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
43 typedef unsigned __int8 uint8_t;
44 typedef unsigned __int16 uint16_t;
45 typedef unsigned __int32 uint32_t;
46 #else
47 #include <stdint.h>
48 #endif
49 
50 #include <sys/types.h>
51 #include <time.h>
52 #include <limits.h>
53 
54 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
55 #include <sys/time.h>
56 #endif
57 
58 /* 'interface' might be defined as a macro on Windows, so we need to
59  * undefine it so as not to break the current libusb API, because
60  * libusb_config_descriptor has an 'interface' member
61  * As this can be problematic if you include windows.h after libusb.h
62  * in your sources, we force windows.h to be included first. */
63 #if defined(_WIN32) || defined(__CYGWIN__)
64 #include <windows.h>
65 #if defined(interface)
66 #undef interface
67 #endif
68 #endif
69 
95 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
96  * functions. You'd think that declaration would be enough, but cygwin will
97  * complain about conflicting types unless both are marked this way.
98  * The placement of this macro is important too; it must appear after the
99  * return type, before the function name. See internal documentation for
100  * API_EXPORTED.
101  */
102 #if defined(_WIN32) || defined(__CYGWIN__)
103 #define LIBUSB_CALL WINAPI
104 #else
105 #define LIBUSB_CALL
106 #endif
107 
108 #ifdef __cplusplus
109 extern "C" {
110 #endif
111 
120 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
121 {
122  union {
123  uint8_t b8[2];
124  uint16_t b16;
125  } _tmp;
126  _tmp.b8[1] = x >> 8;
127  _tmp.b8[0] = x & 0xff;
128  return _tmp.b16;
129 }
130 
139 #define libusb_le16_to_cpu libusb_cpu_to_le16
140 
141 /* standard USB stuff */
142 
151 
154 
157 
160 
163 
166 
168  LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
169  LIBUSB_CLASS_IMAGE = 6,
170 
173 
176 
179 
182 
185 
188 
191 
194 
197 
200 
203 };
204 
210 
213 
216 
219 
222 
225 
228 
231 
234 };
235 
236 /* Descriptor sizes per descriptor type */
237 #define LIBUSB_DT_DEVICE_SIZE 18
238 #define LIBUSB_DT_CONFIG_SIZE 9
239 #define LIBUSB_DT_INTERFACE_SIZE 9
240 #define LIBUSB_DT_ENDPOINT_SIZE 7
241 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
242 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
243 
244 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
245 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
246 
254 
257 };
258 
259 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
260 
268 
271 
274 
277 };
278 
284 
287 
288  /* 0x02 is reserved */
289 
292 
293  /* 0x04 is reserved */
294 
297 
300 
303 
306 
309 
312 
315 
318 };
319 
327 
330 
333 
336 };
337 
345 
348 
351 
354 };
355 
356 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
357 
366 
369 
372 
375 };
376 
377 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
378 
387 
390 
393 };
394 
402  uint8_t bLength;
403 
408 
411  uint16_t bcdUSB;
412 
414  uint8_t bDeviceClass;
415 
419 
423 
426 
428  uint16_t idVendor;
429 
431  uint16_t idProduct;
432 
434  uint16_t bcdDevice;
435 
437  uint8_t iManufacturer;
438 
440  uint8_t iProduct;
441 
443  uint8_t iSerialNumber;
444 
447 };
448 
456  uint8_t bLength;
457 
462 
468 
476  uint8_t bmAttributes;
477 
479  uint16_t wMaxPacketSize;
480 
482  uint8_t bInterval;
483 
486  uint8_t bRefresh;
487 
489  uint8_t bSynchAddress;
490 
493  const unsigned char *extra;
494 
497 };
498 
506  uint8_t bLength;
507 
512 
515 
518 
521  uint8_t bNumEndpoints;
522 
525 
529 
533 
535  uint8_t iInterface;
536 
540 
543  const unsigned char *extra;
544 
547 };
548 
556 
559 };
560 
568  uint8_t bLength;
569 
574 
576  uint16_t wTotalLength;
577 
579  uint8_t bNumInterfaces;
580 
583 
585  uint8_t iConfiguration;
586 
588  uint8_t bmAttributes;
589 
593  uint8_t MaxPower;
594 
598 
601  const unsigned char *extra;
602 
605 };
606 
615  uint8_t bmRequestType;
616 
622  uint8_t bRequest;
623 
625  uint16_t wValue;
626 
629  uint16_t wIndex;
630 
632  uint16_t wLength;
633 };
634 
635 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
636 
637 /* libusb */
638 
639 struct libusb_context;
640 struct libusb_device;
641 struct libusb_device_handle;
643 
649  const uint16_t major;
650 
652  const uint16_t minor;
653 
655  const uint16_t micro;
656 
658  const uint16_t nano;
659 
661  const char *rc;
662 
664  const char *describe;
665 };
666 
685 
702 
703 
713 
720 
723 
726 
729 
732 };
733 
743 
746 
749 
752 
755 
758 
761 
764 
767 
770 
773 
776 
779 
780  /* NB! Remember to update libusb_error_name()
781  when adding new error codes here. */
782 
785 };
786 
793 
796 
799 
802 
806 
809 
812 };
813 
819 
822 
828 
853 };
854 
859  unsigned int length;
860 
862  unsigned int actual_length;
863 
866 };
867 
868 struct libusb_transfer;
869 
879 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
880 
890 
892  uint8_t flags;
893 
895  unsigned char endpoint;
896 
898  unsigned char type;
899 
902  unsigned int timeout;
903 
912 
914  int length;
915 
920 
924 
926  void *user_data;
927 
929  unsigned char *buffer;
930 
934 
937 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
938  [] /* valid C99 code */
939 #else
940  [0] /* non-standard, but usually working code */
941 #endif
942  ;
943 };
944 
955 };
956 
959 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
960 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
961 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
962 const char * LIBUSB_CALL libusb_error_name(int errcode);
963 
965  libusb_device ***list);
967  int unref_devices);
970 
972  int *config);
974  struct libusb_device_descriptor *desc);
976  struct libusb_config_descriptor **config);
978  uint8_t config_index, struct libusb_config_descriptor **config);
980  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
982  struct libusb_config_descriptor *config);
987  unsigned char endpoint);
989  unsigned char endpoint);
990 
994 
996  int configuration);
998  int interface_number);
1000  int interface_number);
1001 
1003  libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1004 
1006  int interface_number, int alternate_setting);
1008  unsigned char endpoint);
1010 
1012  int interface_number);
1014  int interface_number);
1016  int interface_number);
1017 
1018 /* async I/O */
1019 
1032 static inline unsigned char *libusb_control_transfer_get_data(
1033  struct libusb_transfer *transfer)
1034 {
1035  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1036 }
1037 
1051  struct libusb_transfer *transfer)
1052 {
1053  return (struct libusb_control_setup *) transfer->buffer;
1054 }
1055 
1078 static inline void libusb_fill_control_setup(unsigned char *buffer,
1079  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1080  uint16_t wLength)
1081 {
1082  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1083  setup->bmRequestType = bmRequestType;
1084  setup->bRequest = bRequest;
1085  setup->wValue = libusb_cpu_to_le16(wValue);
1086  setup->wIndex = libusb_cpu_to_le16(wIndex);
1087  setup->wLength = libusb_cpu_to_le16(wLength);
1088 }
1089 
1090 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1091 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1092 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1093 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1094 
1122 static inline void libusb_fill_control_transfer(
1123  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1124  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1125  unsigned int timeout)
1126 {
1127  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1128  transfer->dev_handle = dev_handle;
1129  transfer->endpoint = 0;
1130  transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1131  transfer->timeout = timeout;
1132  transfer->buffer = buffer;
1133  if (setup)
1134  transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1135  + libusb_le16_to_cpu(setup->wLength);
1136  transfer->user_data = user_data;
1137  transfer->callback = callback;
1138 }
1139 
1153 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1154  libusb_device_handle *dev_handle, unsigned char endpoint,
1155  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1156  void *user_data, unsigned int timeout)
1157 {
1158  transfer->dev_handle = dev_handle;
1159  transfer->endpoint = endpoint;
1160  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1161  transfer->timeout = timeout;
1162  transfer->buffer = buffer;
1163  transfer->length = length;
1164  transfer->user_data = user_data;
1165  transfer->callback = callback;
1166 }
1167 
1182  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1183  unsigned char endpoint, unsigned char *buffer, int length,
1184  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1185 {
1186  transfer->dev_handle = dev_handle;
1187  transfer->endpoint = endpoint;
1189  transfer->timeout = timeout;
1190  transfer->buffer = buffer;
1191  transfer->length = length;
1192  transfer->user_data = user_data;
1193  transfer->callback = callback;
1194 }
1195 
1210 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1211  libusb_device_handle *dev_handle, unsigned char endpoint,
1212  unsigned char *buffer, int length, int num_iso_packets,
1213  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1214 {
1215  transfer->dev_handle = dev_handle;
1216  transfer->endpoint = endpoint;
1218  transfer->timeout = timeout;
1219  transfer->buffer = buffer;
1220  transfer->length = length;
1221  transfer->num_iso_packets = num_iso_packets;
1222  transfer->user_data = user_data;
1223  transfer->callback = callback;
1224 }
1225 
1235  struct libusb_transfer *transfer, unsigned int length)
1236 {
1237  int i;
1238  for (i = 0; i < transfer->num_iso_packets; i++)
1239  transfer->iso_packet_desc[i].length = length;
1240 }
1241 
1258 static inline unsigned char *libusb_get_iso_packet_buffer(
1259  struct libusb_transfer *transfer, unsigned int packet)
1260 {
1261  int i;
1262  size_t offset = 0;
1263  int _packet;
1264 
1265  /* oops..slight bug in the API. packet is an unsigned int, but we use
1266  * signed integers almost everywhere else. range-check and convert to
1267  * signed to avoid compiler warnings. FIXME for libusb-2. */
1268  if (packet > INT_MAX)
1269  return NULL;
1270  _packet = packet;
1271 
1272  if (_packet >= transfer->num_iso_packets)
1273  return NULL;
1274 
1275  for (i = 0; i < _packet; i++)
1276  offset += transfer->iso_packet_desc[i].length;
1277 
1278  return transfer->buffer + offset;
1279 }
1280 
1300 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1301  struct libusb_transfer *transfer, unsigned int packet)
1302 {
1303  int _packet;
1304 
1305  /* oops..slight bug in the API. packet is an unsigned int, but we use
1306  * signed integers almost everywhere else. range-check and convert to
1307  * signed to avoid compiler warnings. FIXME for libusb-2. */
1308  if (packet > INT_MAX)
1309  return NULL;
1310  _packet = packet;
1311 
1312  if (_packet >= transfer->num_iso_packets)
1313  return NULL;
1314 
1315  return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1316 }
1317 
1318 /* sync I/O */
1319 
1321  uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1322  unsigned char *data, uint16_t wLength, unsigned int timeout);
1323 
1325  unsigned char endpoint, unsigned char *data, int length,
1326  int *actual_length, unsigned int timeout);
1327 
1329  unsigned char endpoint, unsigned char *data, int length,
1330  int *actual_length, unsigned int timeout);
1331 
1345  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1346 {
1348  LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1349  (uint16_t) length, 1000);
1350 }
1351 
1367  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1368 {
1370  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1371  langid, data, (uint16_t) length, 1000);
1372 }
1373 
1375  uint8_t desc_index, unsigned char *data, int length);
1376 
1377 /* polling and timeouts */
1378 
1386 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1387 
1389  struct timeval *tv);
1391  struct timeval *tv, int *completed);
1395  struct timeval *tv);
1398  struct timeval *tv);
1399 
1405  int fd;
1406 
1411  short events;
1412 };
1413 
1424 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1425  void *user_data);
1426 
1436 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1437 
1439  libusb_context *ctx);
1441  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1442  void *user_data);
1443 
1453 
1456 typedef enum {
1460 
1464 
1468 
1471 typedef enum {
1474 
1480 
1483 enum {
1486 
1489 
1492 };
1493 
1505  libusb_device *device,
1506  libusb_hotplug_event event,
1507  void *user_data);
1508 
1523  libusb_hotplug_flag flags,
1524  int vendor_id, int product_id,
1525  int dev_class,
1527  void *user_data, libusb_hotplug_callback **cb);
1528 
1540 
1550 
1559 
1560 #ifdef __cplusplus
1561 }
1562 #endif
1563 
1564 #endif