LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
libusb.h
Go to the documentation of this file.
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 #include <stdint.h>
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <time.h>
28 #include <limits.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
42 #define libusb_cpu_to_le16(x) ({ \
43  union { \
44  uint8_t b8[2]; \
45  uint16_t b16; \
46  } _tmp; \
47  uint16_t _tmp2 = (uint16_t)(x); \
48  _tmp.b8[1] = _tmp2 >> 8; \
49  _tmp.b8[0] = _tmp2 & 0xff; \
50  _tmp.b16; \
51 })
52 
61 #define libusb_le16_to_cpu libusb_cpu_to_le16
62 
63 /* standard USB stuff */
64 
73 
76 
79 
82 
85 
88 
91 
94 
97 
100 
103 
106 };
107 
113 
116 
119 
122 
125 
128 
131 
134 
137 };
138 
139 /* Descriptor sizes per descriptor type */
140 #define LIBUSB_DT_DEVICE_SIZE 18
141 #define LIBUSB_DT_CONFIG_SIZE 9
142 #define LIBUSB_DT_INTERFACE_SIZE 9
143 #define LIBUSB_DT_ENDPOINT_SIZE 7
144 #define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
145 #define LIBUSB_DT_HUB_NONVAR_SIZE 7
146 
147 #define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
148 #define LIBUSB_ENDPOINT_DIR_MASK 0x80
149 
157 
160 };
161 
162 #define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */
163 
171 
174 
177 
180 };
181 
187 
190 
191  /* 0x02 is reserved */
192 
195 
196  /* 0x04 is reserved */
197 
200 
203 
206 
209 
212 
215 
218 
221 };
222 
230 
233 
236 
239 };
240 
248 
251 
254 
257 };
258 
259 #define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C
260 
269 
272 
275 
278 };
279 
280 #define LIBUSB_ISO_USAGE_TYPE_MASK 0x30
281 
290 
293 
296 };
297 
305  uint8_t bLength;
306 
311 
314  uint16_t bcdUSB;
315 
317  uint8_t bDeviceClass;
318 
322 
326 
329 
331  uint16_t idVendor;
332 
334  uint16_t idProduct;
335 
337  uint16_t bcdDevice;
338 
340  uint8_t iManufacturer;
341 
343  uint8_t iProduct;
344 
346  uint8_t iSerialNumber;
347 
350 };
351 
359  uint8_t bLength;
360 
365 
371 
379  uint8_t bmAttributes;
380 
382  uint16_t wMaxPacketSize;
383 
385  uint8_t bInterval;
386 
389  uint8_t bRefresh;
390 
392  uint8_t bSynchAddress;
393 
396  const unsigned char *extra;
397 
400 };
401 
409  uint8_t bLength;
410 
415 
418 
421 
424  uint8_t bNumEndpoints;
425 
428 
432 
436 
438  uint8_t iInterface;
439 
443 
446  const unsigned char *extra;
447 
450 };
451 
459 
462 };
463 
471  uint8_t bLength;
472 
477 
479  uint16_t wTotalLength;
480 
482  uint8_t bNumInterfaces;
483 
486 
488  uint8_t iConfiguration;
489 
491  uint8_t bmAttributes;
492 
496  uint8_t MaxPower;
497 
501 
504  const unsigned char *extra;
505 
508 };
509 
518  uint8_t bmRequestType;
519 
525  uint8_t bRequest;
526 
528  uint16_t wValue;
529 
532  uint16_t wIndex;
533 
535  uint16_t wLength;
536 };
537 
538 #define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup))
539 
540 /* libusb */
541 
542 struct libusb_context;
543 struct libusb_device;
544 struct libusb_device_handle;
545 
564 
581 
582 
592 
600 
603 
606 
609 
612 
615 
618 
621 
624 
627 
630 
633 
636 
639 };
640 
647 
650 
653 
656 
660 
663 
666 };
667 
673 
676 
682 };
683 
688  unsigned int length;
689 
691  unsigned int actual_length;
692 
695 };
696 
697 struct libusb_transfer;
698 
708 typedef void (*libusb_transfer_cb_fn)(struct libusb_transfer *transfer);
709 
719 
721  uint8_t flags;
722 
724  unsigned char endpoint;
725 
727  unsigned char type;
728 
731  unsigned int timeout;
732 
741 
743  int length;
744 
749 
753 
755  void *user_data;
756 
758  unsigned char *buffer;
759 
763 
766 #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
767  [] /* valid C99 code */
768 #else
769  [0] /* non-standard, but usually working code */
770 #endif
771  ;
772 };
773 
774 int libusb_init(libusb_context **ctx);
775 void libusb_exit(libusb_context *ctx);
776 void libusb_set_debug(libusb_context *ctx, int level);
777 
779  libusb_device ***list);
780 void libusb_free_device_list(libusb_device **list, int unref_devices);
783 
784 int libusb_get_configuration(libusb_device_handle *dev, int *config);
786  struct libusb_device_descriptor *desc);
788  struct libusb_config_descriptor **config);
789 int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index,
790  struct libusb_config_descriptor **config);
792  uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
796 int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint);
797 int libusb_get_max_iso_packet_size(libusb_device *dev, unsigned char endpoint);
798 
800 void libusb_close(libusb_device_handle *dev_handle);
802 
803 int libusb_set_configuration(libusb_device_handle *dev, int configuration);
804 int libusb_claim_interface(libusb_device_handle *dev, int iface);
806 
808  uint16_t vendor_id, uint16_t product_id);
809 
811  int interface_number, int alternate_setting);
812 int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint);
814 
815 int libusb_kernel_driver_active(libusb_device_handle *dev, int interface);
816 int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface);
817 int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface);
818 
819 /* async I/O */
820 
833 static inline unsigned char *libusb_control_transfer_get_data(
834  struct libusb_transfer *transfer)
835 {
836  return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
837 }
838 
851 static inline struct libusb_control_setup *libusb_control_transfer_get_setup(
852  struct libusb_transfer *transfer)
853 {
854  return (struct libusb_control_setup *) transfer->buffer;
855 }
856 
879 static inline void libusb_fill_control_setup(unsigned char *buffer,
880  uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
881  uint16_t wLength)
882 {
883  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
884  setup->bmRequestType = bmRequestType;
885  setup->bRequest = bRequest;
886  setup->wValue = libusb_cpu_to_le16(wValue);
887  setup->wIndex = libusb_cpu_to_le16(wIndex);
888  setup->wLength = libusb_cpu_to_le16(wLength);
889 }
890 
891 struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
892 int libusb_submit_transfer(struct libusb_transfer *transfer);
893 int libusb_cancel_transfer(struct libusb_transfer *transfer);
894 void libusb_free_transfer(struct libusb_transfer *transfer);
895 
923 static inline void libusb_fill_control_transfer(
925  unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data,
926  unsigned int timeout)
927 {
928  struct libusb_control_setup *setup = (struct libusb_control_setup *) buffer;
929  transfer->dev_handle = dev_handle;
930  transfer->endpoint = 0;
932  transfer->timeout = timeout;
933  transfer->buffer = buffer;
934  if (setup)
936  + libusb_le16_to_cpu(setup->wLength);
937  transfer->user_data = user_data;
938  transfer->callback = callback;
939 }
940 
954 static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
955  libusb_device_handle *dev_handle, unsigned char endpoint,
956  unsigned char *buffer, int length, libusb_transfer_cb_fn callback,
957  void *user_data, unsigned int timeout)
958 {
959  transfer->dev_handle = dev_handle;
960  transfer->endpoint = endpoint;
961  transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
962  transfer->timeout = timeout;
963  transfer->buffer = buffer;
964  transfer->length = length;
965  transfer->user_data = user_data;
966  transfer->callback = callback;
967 }
968 
982 static inline void libusb_fill_interrupt_transfer(
983  struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
984  unsigned char endpoint, unsigned char *buffer, int length,
985  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
986 {
987  transfer->dev_handle = dev_handle;
988  transfer->endpoint = endpoint;
990  transfer->timeout = timeout;
991  transfer->buffer = buffer;
992  transfer->length = length;
993  transfer->user_data = user_data;
994  transfer->callback = callback;
995 }
996 
1011 static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer,
1012  libusb_device_handle *dev_handle, unsigned char endpoint,
1013  unsigned char *buffer, int length, int num_iso_packets,
1014  libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
1015 {
1016  transfer->dev_handle = dev_handle;
1017  transfer->endpoint = endpoint;
1019  transfer->timeout = timeout;
1020  transfer->buffer = buffer;
1021  transfer->length = length;
1022  transfer->num_iso_packets = num_iso_packets;
1023  transfer->user_data = user_data;
1024  transfer->callback = callback;
1025 }
1026 
1035 static inline void libusb_set_iso_packet_lengths(
1036  struct libusb_transfer *transfer, unsigned int length)
1037 {
1038  int i;
1039  for (i = 0; i < transfer->num_iso_packets; i++)
1040  transfer->iso_packet_desc[i].length = length;
1041 }
1042 
1059 static inline unsigned char *libusb_get_iso_packet_buffer(
1060  struct libusb_transfer *transfer, unsigned int packet)
1061 {
1062  int i;
1063  size_t offset = 0;
1064  int _packet;
1065 
1066  /* oops..slight bug in the API. packet is an unsigned int, but we use
1067  * signed integers almost everywhere else. range-check and convert to
1068  * signed to avoid compiler warnings. FIXME for libusb-2. */
1069  if (packet > INT_MAX)
1070  return NULL;
1071  _packet = packet;
1072 
1073  if (_packet >= transfer->num_iso_packets)
1074  return NULL;
1075 
1076  for (i = 0; i < _packet; i++)
1077  offset += transfer->iso_packet_desc[i].length;
1078 
1079  return transfer->buffer + offset;
1080 }
1081 
1101 static inline unsigned char *libusb_get_iso_packet_buffer_simple(
1102  struct libusb_transfer *transfer, unsigned int packet)
1103 {
1104  int _packet;
1105 
1106  /* oops..slight bug in the API. packet is an unsigned int, but we use
1107  * signed integers almost everywhere else. range-check and convert to
1108  * signed to avoid compiler warnings. FIXME for libusb-2. */
1109  if (packet > INT_MAX)
1110  return NULL;
1111  _packet = packet;
1112 
1113  if (_packet >= transfer->num_iso_packets)
1114  return NULL;
1115 
1116  return transfer->buffer + (transfer->iso_packet_desc[0].length * _packet);
1117 }
1118 
1119 /* sync I/O */
1120 
1122  uint8_t request_type, uint8_t request, uint16_t value, uint16_t index,
1123  unsigned char *data, uint16_t length, unsigned int timeout);
1124 
1126  unsigned char endpoint, unsigned char *data, int length,
1127  int *actual_length, unsigned int timeout);
1128 
1130  unsigned char endpoint, unsigned char *data, int length,
1131  int *actual_length, unsigned int timeout);
1132 
1145 static inline int libusb_get_descriptor(libusb_device_handle *dev,
1146  uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length)
1147 {
1149  LIBUSB_REQUEST_GET_DESCRIPTOR, (desc_type << 8) | desc_index, 0, data,
1150  length, 1000);
1151 }
1152 
1167 static inline int libusb_get_string_descriptor(libusb_device_handle *dev,
1168  uint8_t desc_index, uint16_t langid, unsigned char *data, int length)
1169 {
1171  LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index,
1172  langid, data, length, 1000);
1173 }
1174 
1176  uint8_t index, unsigned char *data, int length);
1177 
1178 /* polling and timeouts */
1179 
1187 int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv);
1188 
1189 int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv);
1191 int libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv);
1193 int libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv);
1194 
1200  int fd;
1201 
1206  short events;
1207 };
1208 
1219 typedef void (*libusb_pollfd_added_cb)(int fd, short events, void *user_data);
1220 
1230 typedef void (*libusb_pollfd_removed_cb)(int fd, void *user_data);
1231 
1232 const struct libusb_pollfd **libusb_get_pollfds(libusb_context *ctx);
1234  libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
1235  void *user_data);
1236 
1237 #ifdef __cplusplus
1238 }
1239 #endif
1240 
1241 #endif
int libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
int libusb_clear_halt(libusb_device_handle *dev, unsigned char endpoint)
uint16_t wIndex
Definition: libusb.h:532
uint8_t bConfigurationValue
Definition: libusb.h:485
void(* libusb_pollfd_removed_cb)(int fd, void *user_data)
Definition: libusb.h:1230
int libusb_handle_events(libusb_context *ctx)
int num_altsetting
Definition: libusb.h:461
struct libusb_device libusb_device
Definition: libusb.h:580
void(* libusb_pollfd_added_cb)(int fd, short events, void *user_data)
Definition: libusb.h:1219
uint16_t wValue
Definition: libusb.h:528
int libusb_get_device_descriptor(libusb_device *dev, struct libusb_device_descriptor *desc)
int libusb_set_configuration(libusb_device_handle *dev, int configuration)
void(* libusb_transfer_cb_fn)(struct libusb_transfer *transfer)
Definition: libusb.h:708
int num_iso_packets
Definition: libusb.h:762
struct libusb_device_handle libusb_device_handle
Definition: libusb.h:591
int libusb_get_max_iso_packet_size(libusb_device *dev, unsigned char endpoint)
int libusb_reset_device(libusb_device_handle *dev)
int libusb_detach_kernel_driver(libusb_device_handle *dev, int interface)
libusb_transfer_type
Definition: libusb.h:168
uint8_t bNumConfigurations
Definition: libusb.h:349
int libusb_release_interface(libusb_device_handle *dev, int iface)
libusb_transfer_cb_fn callback
Definition: libusb.h:752
unsigned int timeout
Definition: libusb.h:731
int actual_length
Definition: libusb.h:748
libusb_standard_request
Definition: libusb.h:184
libusb_device * libusb_get_device(libusb_device_handle *dev_handle)
#define libusb_le16_to_cpu
Definition: libusb.h:61
int libusb_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t index, unsigned char *data, int length)
int libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
int libusb_get_active_config_descriptor(libusb_device *dev, struct libusb_config_descriptor **config)
libusb_error
Definition: libusb.h:597
void libusb_free_device_list(libusb_device **list, int unref_devices)
libusb_transfer_flags
Definition: libusb.h:670
libusb_request_recipient
Definition: libusb.h:245
libusb_iso_usage_type
Definition: libusb.h:287
int libusb_kernel_driver_active(libusb_device_handle *dev, int interface)
enum libusb_transfer_status status
Definition: libusb.h:740
int libusb_get_config_descriptor_by_value(libusb_device *dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
const struct libusb_interface * interface
Definition: libusb.h:500
libusb_transfer_status
Definition: libusb.h:643
int libusb_event_handler_active(libusb_context *ctx)
libusb_class_code
Definition: libusb.h:67
int libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint)
void libusb_lock_events(libusb_context *ctx)
int libusb_open(libusb_device *dev, libusb_device_handle **handle)
int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index, struct libusb_config_descriptor **config)
uint8_t bRequest
Definition: libusb.h:525
struct libusb_transfer * libusb_alloc_transfer(int iso_packets)
void libusb_unref_device(libusb_device *dev)
int libusb_get_configuration(libusb_device_handle *dev, int *config)
void libusb_close(libusb_device_handle *dev_handle)
void libusb_unlock_event_waiters(libusb_context *ctx)
int libusb_pollfds_handle_timeouts(libusb_context *ctx)
uint8_t bmRequestType
Definition: libusb.h:518
const unsigned char * extra
Definition: libusb.h:504
libusb_request_type
Definition: libusb.h:227
struct libusb_context libusb_context
Definition: libusb.h:563
unsigned char * buffer
Definition: libusb.h:758
#define libusb_cpu_to_le16(x)
Definition: libusb.h:42
uint8_t libusb_get_device_address(libusb_device *dev)
libusb_device * libusb_ref_device(libusb_device *dev)
void libusb_set_debug(libusb_context *ctx, int level)
libusb_descriptor_type
Definition: libusb.h:110
void libusb_free_transfer(struct libusb_transfer *transfer)
uint8_t libusb_get_bus_number(libusb_device *dev)
void libusb_free_config_descriptor(struct libusb_config_descriptor *config)
void libusb_set_pollfd_notifiers(libusb_context *ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data)
unsigned char type
Definition: libusb.h:727
int libusb_control_transfer(libusb_device_handle *dev_handle, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, unsigned char *data, uint16_t length, unsigned int timeout)
void libusb_lock_event_waiters(libusb_context *ctx)
int libusb_interrupt_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout)
uint16_t wLength
Definition: libusb.h:535
void libusb_unlock_events(libusb_context *ctx)
int libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
enum libusb_transfer_status status
Definition: libusb.h:694
libusb_device_handle * dev_handle
Definition: libusb.h:718
ssize_t libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
const unsigned char * extra
Definition: libusb.h:446
const struct libusb_endpoint_descriptor * endpoint
Definition: libusb.h:442
const struct libusb_interface_descriptor * altsetting
Definition: libusb.h:458
struct libusb_iso_packet_descriptor iso_packet_desc[0]
Definition: libusb.h:765
libusb_device_handle * libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
void * user_data
Definition: libusb.h:755
libusb_iso_sync_type
Definition: libusb.h:266
int libusb_submit_transfer(struct libusb_transfer *transfer)
const struct libusb_pollfd ** libusb_get_pollfds(libusb_context *ctx)
unsigned int actual_length
Definition: libusb.h:691
const unsigned char * extra
Definition: libusb.h:396
int libusb_attach_kernel_driver(libusb_device_handle *dev, int interface)
int libusb_cancel_transfer(struct libusb_transfer *transfer)
libusb_endpoint_direction
Definition: libusb.h:154
int libusb_bulk_transfer(libusb_device_handle *dev_handle, unsigned char endpoint, unsigned char *data, int length, int *actual_length, unsigned int timeout)
int libusb_try_lock_events(libusb_context *ctx)
int libusb_init(libusb_context **ctx)
int libusb_claim_interface(libusb_device_handle *dev, int iface)
int libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
int libusb_event_handling_ok(libusb_context *ctx)
#define LIBUSB_CONTROL_SETUP_SIZE
Definition: libusb.h:538
int libusb_set_interface_alt_setting(libusb_device_handle *dev, int interface_number, int alternate_setting)
void libusb_exit(libusb_context *ctx)
unsigned char endpoint
Definition: libusb.h:724
uint8_t flags
Definition: libusb.h:721
short events
Definition: libusb.h:1206
#define NULL