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  *
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 
21 #ifndef LIBUSB_H
22 #define LIBUSB_H
23 
24 #ifdef _MSC_VER
25 /* on MS environments, the inline keyword is available in C++ only */
26 #define inline __inline
27 /* ssize_t is also not available (copy/paste from MinGW) */
28 #ifndef _SSIZE_T_DEFINED
29 #define _SSIZE_T_DEFINED
30 #undef ssize_t
31 #ifdef _WIN64
32  typedef __int64 ssize_t;
33 #else
34  typedef int ssize_t;
35 #endif /* _WIN64 */
36 #endif /* _SSIZE_T_DEFINED */
37 #endif /* _MSC_VER */
38 
39 /* stdint.h is also not usually available on MS */
40 #if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H))
41 typedef unsigned __int8 uint8_t;
42 typedef unsigned __int16 uint16_t;
43 typedef unsigned __int32 uint32_t;
44 #else
45 #include <stdint.h>
46 #endif
47 
48 #include <sys/types.h>
49 #include <time.h>
50 #include <limits.h>
51 
52 #if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__)
53 #include <sys/time.h>
54 #endif
55 
56 /* 'interface' might be defined as a macro on Windows, so we need to
57  * undefine it so as not to break the current libusb API, because
58  * libusb_config_descriptor has an 'interface' member
59  * As this can be problematic if you include windows.h after libusb.h
60  * in your sources, we force windows.h to be included first. */
61 #if defined(_WIN32) || defined(__CYGWIN__)
62 #include <windows.h>
63 #if defined(interface)
64 #undef interface
65 #endif
66 #endif
67 
93 /* LIBUSB_CALL must be defined on both definition and declaration of libusb
94  * functions. You'd think that declaration would be enough, but cygwin will
95  * complain about conflicting types unless both are marked this way.
96  * The placement of this macro is important too; it must appear after the
97  * return type, before the function name. See internal documentation for
98  * API_EXPORTED.
99  */
100 #if defined(_WIN32) || defined(__CYGWIN__)
101 #define LIBUSB_CALL WINAPI
102 #else
103 #define LIBUSB_CALL
104 #endif
105 
106 #ifdef __cplusplus
107 extern "C" {
108 #endif
109 
118 static inline uint16_t libusb_cpu_to_le16(const uint16_t x)
119 {
120  union {
121  uint8_t b8[2];
122  uint16_t b16;
123  } _tmp;
124  _tmp.b8[1] = x >> 8;
125  _tmp.b8[0] = x & 0xff;
126  return _tmp.b16;
127 }
128 
137 #define libusb_le16_to_cpu libusb_cpu_to_le16
138 
139 /* standard USB stuff */
140 
149 
152 
155 
158 
161 
164 
166  LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */
167  LIBUSB_CLASS_IMAGE = 6,
168 
171 
174 
177 
180 
183 
186 
189 
192 
195 
198 
201 };
202 
208 
211 
214 
217 
220 
223 
226 
229 
232 };
233 
234 /* Descriptor sizes per descriptor type */
235 #define LIBUSB_DT_DEVICE_SIZE 18
236 #define LIBUSB_DT_CONFIG_SIZE 9
237 #define LIBUSB_DT_INTERFACE_SIZE 9
238 #define LIBUSB_DT_ENDPOINT_SIZE 7
239 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
240 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
241 
242 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
243 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
244 
252 
255 };
256 
257 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
258 
266 
269 
272 
275 };
276 
282 
285 
286  /* 0x02 is reserved */
287 
290 
291  /* 0x04 is reserved */
292 
295 
298 
301 
304 
307 
310 
313 
316 };
317 
325 
328 
331 
334 };
335 
343 
346 
349 
352 };
353 
354 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
355 
364 
367 
370 
373 };
374 
375 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
376 
385 
388 
391 };
392 
400  uint8_t bLength;
401 
406 
409  uint16_t bcdUSB;
410 
412  uint8_t bDeviceClass;
413 
417 
421 
424 
426  uint16_t idVendor;
427 
429  uint16_t idProduct;
430 
432  uint16_t bcdDevice;
433 
435  uint8_t iManufacturer;
436 
438  uint8_t iProduct;
439 
441  uint8_t iSerialNumber;
442 
445 };
446 
454  uint8_t bLength;
455 
460 
466 
474  uint8_t bmAttributes;
475 
477  uint16_t wMaxPacketSize;
478 
480  uint8_t bInterval;
481 
484  uint8_t bRefresh;
485 
487  uint8_t bSynchAddress;
488 
491  const unsigned char *extra;
492 
495 };
496 
504  uint8_t bLength;
505 
510 
513 
516 
519  uint8_t bNumEndpoints;
520 
523 
527 
531 
533  uint8_t iInterface;
534 
538 
541  const unsigned char *extra;
542 
545 };
546 
554 
557 };
558 
566  uint8_t bLength;
567 
572 
574  uint16_t wTotalLength;
575 
577  uint8_t bNumInterfaces;
578 
581 
583  uint8_t iConfiguration;
584 
586  uint8_t bmAttributes;
587 
591  uint8_t MaxPower;
592 
596 
599  const unsigned char *extra;
600 
603 };
604 
613  uint8_t bmRequestType;
614 
620  uint8_t bRequest;
621 
623  uint16_t wValue;
624 
627  uint16_t wIndex;
628 
630  uint16_t wLength;
631 };
632 
633 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
634 
635 /* libusb */
636 
637 struct libusb_context;
638 struct libusb_device;
639 struct libusb_device_handle;
640 
646  const uint16_t major;
647 
649  const uint16_t minor;
650 
652  const uint16_t micro;
653 
655  const uint16_t nano;
656 
658  const char *rc;
659 
661  const char *describe;
662 };
663 
682 
699 
700 
710 
717 
720 
723 
726 
729 };
730 
740 
743 
746 
749 
752 
755 
758 
761 
764 
767 
770 
773 
776 
777  /* NB! Remember to update libusb_error_name()
778  when adding new error codes here. */
779 
782 };
783 
790 
793 
796 
799 
803 
806 
809 };
810 
816 
819 
825 
850 };
851 
856  unsigned int length;
857 
859  unsigned int actual_length;
860 
863 };
864 
865 struct libusb_transfer;
866 
876 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
877 
887 
889  uint8_t flags;
890 
892  unsigned char endpoint;
893 
895  unsigned char type;
896 
899  unsigned int timeout;
900 
909 
911  int length;
912 
917 
921 
923  void *user_data;
924 
926  unsigned char *buffer;
927 
931 
934 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
935  [] /* valid C99 code */
936 #else
937  [0] /* non-standard, but usually working code */
938 #endif
939  ;
940 };
941 
950 };
951 
954 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
955 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
956 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
957 const char * LIBUSB_CALL libusb_error_name(int errcode);
958 
960  libusb_device ***list);
962  int unref_devices);
965 
967  int *config);
969  struct libusb_device_descriptor *desc);
971  struct libusb_config_descriptor **config);
973  uint8_t config_index, struct libusb_config_descriptor **config);
975  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
977  struct libusb_config_descriptor *config);
982  unsigned char endpoint);
984  unsigned char endpoint);
985 
989 
991  int configuration);
993  int interface_number);
995  int interface_number);
996 
998  libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
999 
1001  int interface_number, int alternate_setting);
1003  unsigned char endpoint);
1005 
1007  int interface_number);
1009  int interface_number);
1011  int interface_number);
1012 
1013 /* async I/O */
1014 
1027 static inline unsigned char *libusb_control_transfer_get_data(
1028  struct libusb_transfer *transfer)
1029 {
1030  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1031 }
1032 
1046  struct libusb_transfer *transfer)
1047 {
1048  return (struct libusb_control_setup *) transfer->buffer;
1049 }
1050 
1073 static inline void libusb_fill_control_setup(unsigned char *buffer,
1074  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1075  uint16_t wLength)
1076 {
1077  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1078  setup->bmRequestType = bmRequestType;
1079  setup->bRequest = bRequest;
1080  setup->wValue = libusb_cpu_to_le16(wValue);
1081  setup->wIndex = libusb_cpu_to_le16(wIndex);
1082  setup->wLength = libusb_cpu_to_le16(wLength);
1083 }
1084 
1085 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1086 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1087 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1088 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1089 
1117 static inline void libusb_fill_control_transfer(
1118  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1119  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1120  unsigned int timeout)
1121 {
1122  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1123  transfer->dev_handle = dev_handle;
1124  transfer->endpoint = 0;
1125  transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1126  transfer->timeout = timeout;
1127  transfer->buffer = buffer;
1128  if (setup)
1129  transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1130  + libusb_le16_to_cpu(setup->wLength);
1131  transfer->user_data = user_data;
1132  transfer->callback = callback;
1133 }
1134 
1148 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1149  libusb_device_handle *dev_handle, unsigned char endpoint,
1150  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1151  void *user_data, unsigned int timeout)
1152 {
1153  transfer->dev_handle = dev_handle;
1154  transfer->endpoint = endpoint;
1155  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1156  transfer->timeout = timeout;
1157  transfer->buffer = buffer;
1158  transfer->length = length;
1159  transfer->user_data = user_data;
1160  transfer->callback = callback;
1161 }
1162 
1177  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1178  unsigned char endpoint, unsigned char *buffer, int length,
1179  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1180 {
1181  transfer->dev_handle = dev_handle;
1182  transfer->endpoint = endpoint;
1184  transfer->timeout = timeout;
1185  transfer->buffer = buffer;
1186  transfer->length = length;
1187  transfer->user_data = user_data;
1188  transfer->callback = callback;
1189 }
1190 
1205 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1206  libusb_device_handle *dev_handle, unsigned char endpoint,
1207  unsigned char *buffer, int length, int num_iso_packets,
1208  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1209 {
1210  transfer->dev_handle = dev_handle;
1211  transfer->endpoint = endpoint;
1213  transfer->timeout = timeout;
1214  transfer->buffer = buffer;
1215  transfer->length = length;
1216  transfer->num_iso_packets = num_iso_packets;
1217  transfer->user_data = user_data;
1218  transfer->callback = callback;
1219 }
1220 
1230  struct libusb_transfer *transfer, unsigned int length)
1231 {
1232  int i;
1233  for (i = 0; i < transfer->num_iso_packets; i++)
1234  transfer->iso_packet_desc[i].length = length;
1235 }
1236 
1253 static inline unsigned char *libusb_get_iso_packet_buffer(
1254  struct libusb_transfer *transfer, unsigned int packet)
1255 {
1256  int i;
1257  size_t offset = 0;
1258  int _packet;
1259 
1260  /* oops..slight bug in the API. packet is an unsigned int, but we use
1261  * signed integers almost everywhere else. range-check and convert to
1262  * signed to avoid compiler warnings. FIXME for libusb-2. */
1263  if (packet > INT_MAX)
1264  return NULL;
1265  _packet = packet;
1266 
1267  if (_packet >= transfer->num_iso_packets)
1268  return NULL;
1269 
1270  for (i = 0; i < _packet; i++)
1271  offset += transfer->iso_packet_desc[i].length;
1272 
1273  return transfer->buffer + offset;
1274 }
1275 
1295 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1296  struct libusb_transfer *transfer, unsigned int packet)
1297 {
1298  int _packet;
1299 
1300  /* oops..slight bug in the API. packet is an unsigned int, but we use
1301  * signed integers almost everywhere else. range-check and convert to
1302  * signed to avoid compiler warnings. FIXME for libusb-2. */
1303  if (packet > INT_MAX)
1304  return NULL;
1305  _packet = packet;
1306 
1307  if (_packet >= transfer->num_iso_packets)
1308  return NULL;
1309 
1310  return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1311 }
1312 
1313 /* sync I/O */
1314 
1316  uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1317  unsigned char *data, uint16_t wLength, unsigned int timeout);
1318 
1320  unsigned char endpoint, unsigned char *data, int length,
1321  int *actual_length, unsigned int timeout);
1322 
1324  unsigned char endpoint, unsigned char *data, int length,
1325  int *actual_length, unsigned int timeout);
1326 
1340  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1341 {
1343  LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1344  (uint16_t) length, 1000);
1345 }
1346 
1362  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1363 {
1365  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1366  langid, data, (uint16_t) length, 1000);
1367 }
1368 
1370  uint8_t desc_index, unsigned char *data, int length);
1371 
1372 /* polling and timeouts */
1373 
1381 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1382 
1384  struct timeval *tv);
1386  struct timeval *tv, int *completed);
1390  struct timeval *tv);
1393  struct timeval *tv);
1394 
1400  int fd;
1401 
1406  short events;
1407 };
1408 
1419 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1420  void *user_data);
1421 
1431 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1432 
1434  libusb_context *ctx);
1436  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1437  void *user_data);
1438 
1439 #ifdef __cplusplus
1440 }
1441 #endif
1442 
1443 #endif