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;
642 
648  const uint16_t major;
649 
651  const uint16_t minor;
652 
654  const uint16_t micro;
655 
657  const uint16_t nano;
658 
660  const char *rc;
661 
663  const char *describe;
664 };
665 
684 
701 
702 
712 
719 
722 
725 
728 
731 };
732 
742 
745 
748 
751 
754 
757 
760 
763 
766 
769 
772 
775 
778 
779  /* NB! Remember to update libusb_error_name()
780  when adding new error codes here. */
781 
784 };
785 
792 
795 
798 
801 
805 
808 
811 };
812 
818 
821 
827 
852 };
853 
858  unsigned int length;
859 
861  unsigned int actual_length;
862 
865 };
866 
867 struct libusb_transfer;
868 
878 typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
879 
889 
891  uint8_t flags;
892 
894  unsigned char endpoint;
895 
897  unsigned char type;
898 
901  unsigned int timeout;
902 
911 
913  int length;
914 
919 
923 
925  void *user_data;
926 
928  unsigned char *buffer;
929 
933 
936 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
937  [] /* valid C99 code */
938 #else
939  [0] /* non-standard, but usually working code */
940 #endif
941  ;
942 };
943 
954 };
955 
958 void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level);
959 const struct libusb_version * LIBUSB_CALL libusb_get_version(void);
960 int LIBUSB_CALL libusb_has_capability(uint32_t capability);
961 const char * LIBUSB_CALL libusb_error_name(int errcode);
962 
964  libusb_device ***list);
966  int unref_devices);
969 
971  int *config);
973  struct libusb_device_descriptor *desc);
975  struct libusb_config_descriptor **config);
977  uint8_t config_index, struct libusb_config_descriptor **config);
979  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
981  struct libusb_config_descriptor *config);
986  unsigned char endpoint);
988  unsigned char endpoint);
989 
993 
995  int configuration);
997  int interface_number);
999  int interface_number);
1000 
1002  libusb_context *ctx, uint16_t vendor_id, uint16_t product_id);
1003 
1005  int interface_number, int alternate_setting);
1007  unsigned char endpoint);
1009 
1011  int interface_number);
1013  int interface_number);
1015  int interface_number);
1016 
1017 /* async I/O */
1018 
1031 static inline unsigned char *libusb_control_transfer_get_data(
1032  struct libusb_transfer *transfer)
1033 {
1034  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
1035 }
1036 
1050  struct libusb_transfer *transfer)
1051 {
1052  return (struct libusb_control_setup *) transfer->buffer;
1053 }
1054 
1077 static inline void libusb_fill_control_setup(unsigned char *buffer,
1078  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1079  uint16_t wLength)
1080 {
1081  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1082  setup->bmRequestType = bmRequestType;
1083  setup->bRequest = bRequest;
1084  setup->wValue = libusb_cpu_to_le16(wValue);
1085  setup->wIndex = libusb_cpu_to_le16(wIndex);
1086  setup->wLength = libusb_cpu_to_le16(wLength);
1087 }
1088 
1089 struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets);
1090 int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer);
1091 int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer);
1092 void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer);
1093 
1121 static inline void libusb_fill_control_transfer(
1122  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1123  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
1124  unsigned int timeout)
1125 {
1126  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
1127  transfer->dev_handle = dev_handle;
1128  transfer->endpoint = 0;
1129  transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
1130  transfer->timeout = timeout;
1131  transfer->buffer = buffer;
1132  if (setup)
1133  transfer->length = LIBUSB_CONTROL_SETUP_SIZE
1134  + libusb_le16_to_cpu(setup->wLength);
1135  transfer->user_data = user_data;
1136  transfer->callback = callback;
1137 }
1138 
1152 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
1153  libusb_device_handle *dev_handle, unsigned char endpoint,
1154  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
1155  void *user_data, unsigned int timeout)
1156 {
1157  transfer->dev_handle = dev_handle;
1158  transfer->endpoint = endpoint;
1159  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1160  transfer->timeout = timeout;
1161  transfer->buffer = buffer;
1162  transfer->length = length;
1163  transfer->user_data = user_data;
1164  transfer->callback = callback;
1165 }
1166 
1181  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
1182  unsigned char endpoint, unsigned char *buffer, int length,
1183  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1184 {
1185  transfer->dev_handle = dev_handle;
1186  transfer->endpoint = endpoint;
1188  transfer->timeout = timeout;
1189  transfer->buffer = buffer;
1190  transfer->length = length;
1191  transfer->user_data = user_data;
1192  transfer->callback = callback;
1193 }
1194 
1209 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1210  libusb_device_handle *dev_handle, unsigned char endpoint,
1211  unsigned char *buffer, int length, int num_iso_packets,
1212  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1213 {
1214  transfer->dev_handle = dev_handle;
1215  transfer->endpoint = endpoint;
1217  transfer->timeout = timeout;
1218  transfer->buffer = buffer;
1219  transfer->length = length;
1220  transfer->num_iso_packets = num_iso_packets;
1221  transfer->user_data = user_data;
1222  transfer->callback = callback;
1223 }
1224 
1234  struct libusb_transfer *transfer, unsigned int length)
1235 {
1236  int i;
1237  for (i = 0; i < transfer->num_iso_packets; i++)
1238  transfer->iso_packet_desc[i].length = length;
1239 }
1240 
1257 static inline unsigned char *libusb_get_iso_packet_buffer(
1258  struct libusb_transfer *transfer, unsigned int packet)
1259 {
1260  int i;
1261  size_t offset = 0;
1262  int _packet;
1263 
1264  /* oops..slight bug in the API. packet is an unsigned int, but we use
1265  * signed integers almost everywhere else. range-check and convert to
1266  * signed to avoid compiler warnings. FIXME for libusb-2. */
1267  if (packet > INT_MAX)
1268  return NULL;
1269  _packet = packet;
1270 
1271  if (_packet >= transfer->num_iso_packets)
1272  return NULL;
1273 
1274  for (i = 0; i < _packet; i++)
1275  offset += transfer->iso_packet_desc[i].length;
1276 
1277  return transfer->buffer + offset;
1278 }
1279 
1299 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1300  struct libusb_transfer *transfer, unsigned int packet)
1301 {
1302  int _packet;
1303 
1304  /* oops..slight bug in the API. packet is an unsigned int, but we use
1305  * signed integers almost everywhere else. range-check and convert to
1306  * signed to avoid compiler warnings. FIXME for libusb-2. */
1307  if (packet > INT_MAX)
1308  return NULL;
1309  _packet = packet;
1310 
1311  if (_packet >= transfer->num_iso_packets)
1312  return NULL;
1313 
1314  return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1315 }
1316 
1317 /* sync I/O */
1318 
1320  uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
1321  unsigned char *data, uint16_t wLength, unsigned int timeout);
1322 
1324  unsigned char endpoint, unsigned char *data, int length,
1325  int *actual_length, unsigned int timeout);
1326 
1328  unsigned char endpoint, unsigned char *data, int length,
1329  int *actual_length, unsigned int timeout);
1330 
1344  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1345 {
1347  LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1348  (uint16_t) length, 1000);
1349 }
1350 
1366  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1367 {
1369  LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index),
1370  langid, data, (uint16_t) length, 1000);
1371 }
1372 
1374  uint8_t desc_index, unsigned char *data, int length);
1375 
1376 /* polling and timeouts */
1377 
1385 int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1386 
1388  struct timeval *tv);
1390  struct timeval *tv, int *completed);
1394  struct timeval *tv);
1397  struct timeval *tv);
1398 
1404  int fd;
1405 
1410  short events;
1411 };
1412 
1423 typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events,
1424  void *user_data);
1425 
1435 typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data);
1436 
1438  libusb_context *ctx);
1440  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1441  void *user_data);
1442 
1445 typedef enum {
1449 
1454 
1457 typedef enum {
1460 
1466 
1469 enum {
1472 
1475 
1478 };
1479 
1491  libusb_device *device,
1492  libusb_hotplug_event event,
1493  void *user_data);
1494 
1503 
1506 
1509 
1512 
1515 
1518 
1521 
1523  void *user_data;
1524 
1526  char reserved[32];
1527 };
1528 
1530 
1546  libusb_context *ctx,
1547  libusb_hotplug_event event,
1549  int vendor_id, int product_id,
1550  int dev_class,
1552  void *user_data)
1553 {
1554  cb->ctx = ctx;
1555  cb->vendor_id = vendor_id;
1556  cb->product_id = product_id;
1557  cb->dev_class = dev_class;
1558  cb->flags = flags;
1559  cb->cb = cb_fn;
1560  cb->user_data = user_data;
1561  cb->event = event;
1562 }
1563 
1570 
1578 
1579 #ifdef __cplusplus
1580 }
1581 #endif
1582 
1583 #endif