eXpand your USB potential
libusb.h
1 /*
2  * Public libusbx header file
3  * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
4  * Copyright © 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 libusbx 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 libusbx
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 /* libusbx */
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 
963  LOG_LEVEL_NONE = 0,
964  LOG_LEVEL_ERROR,
965  LOG_LEVEL_WARNING,
966  LOG_LEVEL_INFO,
967  LOG_LEVEL_DEBUG,
968 };
969 
972 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
973 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
974 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
975 const char * LIBUSB_CALL libusb_error_name(int errcode);
976 
978  libusb_device ***list);
980  int unref_devices);
983 
985  int *config);
987  struct libusb_device_descriptor *desc);
989  struct libusb_config_descriptor **config);
991  uint8_t config_index, struct libusb_config_descriptor **config);
993  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
995  struct libusb_config_descriptor *config);
999 int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length);
1003  unsigned char endpoint);
1005  unsigned char endpoint);
1006 
1010 
1012  int configuration);
1014  int interface_number);
1016  int interface_number);
1017 
1019  libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1020 
1022  int interface_number, int alternate_setting);
1024  unsigned char endpoint);
1026 
1028  int interface_number);
1030  int interface_number);
1032  int interface_number);
1033 
1034 /* async I/O */
1035 
1048 static inline unsigned char *libusb_control_transfer_get_data(
1049  struct libusb_transfer *transfer)
1050 {
1051  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1052 }
1053 
1067  struct libusb_transfer *transfer)
1068 {
1069  return (struct libusb_control_setup *) transfer->buffer;
1070 }
1071 
1094 static inline void libusb_fill_control_setup(unsigned char *buffer,
1095  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1096  uint16_t wLength)
1097 {
1098  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1099  setup->bmRequestType = bmRequestType;
1100  setup->bRequest = bRequest;
1101  setup->wValue = libusb_cpu_to_le16(wValue);
1102  setup->wIndex = libusb_cpu_to_le16(wIndex);
1103  setup->wLength = libusb_cpu_to_le16(wLength);
1104 }
1105 
1106 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1107 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1108 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1109 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1110 
1138 static inline void libusb_fill_control_transfer(
1139  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1140  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1141  unsigned int timeout)
1142 {
1143  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1144  transfer->dev_handle = dev_handle;
1145  transfer->endpoint = 0;
1146  transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1147  transfer->timeout = timeout;
1148  transfer->buffer = buffer;
1149  if (setup)
1150  transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1151  + libusb_le16_to_cpu(setup->wLength);
1152  transfer->user_data = user_data;
1153  transfer->callback = callback;
1154 }
1155 
1169 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1170  libusb_device_handle *dev_handle, unsigned char endpoint,
1171  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1172  void *user_data, unsigned int timeout)
1173 {
1174  transfer->dev_handle = dev_handle;
1175  transfer->endpoint = endpoint;
1176  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1177  transfer->timeout = timeout;
1178  transfer->buffer = buffer;
1179  transfer->length = length;
1180  transfer->user_data = user_data;
1181  transfer->callback = callback;
1182 }
1183 
1198  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1199  unsigned char endpoint, unsigned char *buffer, int length,
1200  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1201 {
1202  transfer->dev_handle = dev_handle;
1203  transfer->endpoint = endpoint;
1205  transfer->timeout = timeout;
1206  transfer->buffer = buffer;
1207  transfer->length = length;
1208  transfer->user_data = user_data;
1209  transfer->callback = callback;
1210 }
1211 
1226 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1227  libusb_device_handle *dev_handle, unsigned char endpoint,
1228  unsigned char *buffer, int length, int num_iso_packets,
1229  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1230 {
1231  transfer->dev_handle = dev_handle;
1232  transfer->endpoint = endpoint;
1234  transfer->timeout = timeout;
1235  transfer->buffer = buffer;
1236  transfer->length = length;
1237  transfer->num_iso_packets = num_iso_packets;
1238  transfer->user_data = user_data;
1239  transfer->callback = callback;
1240 }
1241 
1251  struct libusb_transfer *transfer, unsigned int length)
1252 {
1253  int i;
1254  for (i = 0; i < transfer->num_iso_packets; i++)
1255  transfer->iso_packet_desc[i].length = length;
1256 }
1257 
1274 static inline unsigned char *libusb_get_iso_packet_buffer(
1275  struct libusb_transfer *transfer, unsigned int packet)
1276 {
1277  int i;
1278  size_t offset = 0;
1279  int _packet;
1280 
1281  /* oops..slight bug in the API. packet is an unsigned int, but we use
1282  * signed integers almost everywhere else. range-check and convert to
1283  * signed to avoid compiler warnings. FIXME for libusb-2. */
1284  if (packet > INT_MAX)
1285  return NULL;
1286  _packet = packet;
1287 
1288  if (_packet >= transfer->num_iso_packets)
1289  return NULL;
1290 
1291  for (i = 0; i < _packet; i++)
1292  offset += transfer->iso_packet_desc[i].length;
1293 
1294  return transfer->buffer + offset;
1295 }
1296 
1316 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1317  struct libusb_transfer *transfer, unsigned int packet)
1318 {
1319  int _packet;
1320 
1321  /* oops..slight bug in the API. packet is an unsigned int, but we use
1322  * signed integers almost everywhere else. range-check and convert to
1323  * signed to avoid compiler warnings. FIXME for libusb-2. */
1324  if (packet > INT_MAX)
1325  return NULL;
1326  _packet = packet;
1327 
1328  if (_packet >= transfer->num_iso_packets)
1329  return NULL;
1330 
1331  return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1332 }
1333 
1334 /* sync I/O */
1335 
1337  uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1338  unsigned char *data, uint16_t wLength, unsigned int timeout);
1339 
1341  unsigned char endpoint, unsigned char *data, int length,
1342  int *actual_length, unsigned int timeout);
1343 
1345  unsigned char endpoint, unsigned char *data, int length,
1346  int *actual_length, unsigned int timeout);
1347 
1361  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1362 {
1364  LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1365  (uint16_t) length, 1000);
1366 }
1367 
1383  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1384 {
1386  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1387  langid, data, (uint16_t) length, 1000);
1388 }
1389 
1391  uint8_t desc_index, unsigned char *data, int length);
1392 
1393 /* polling and timeouts */
1394 
1402 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1403 
1405  struct timeval *tv);
1407  struct timeval *tv, int *completed);
1411  struct timeval *tv);
1414  struct timeval *tv);
1415 
1421  int fd;
1422 
1427  short events;
1428 };
1429 
1440 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1441  void *user_data);
1442 
1452 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1453 
1455  libusb_context *ctx);
1457  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1458  void *user_data);
1459 
1460 #ifdef __cplusplus
1461 }
1462 #endif
1463 
1464 #endif