libusb 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -2
  3. data/Gemfile +2 -0
  4. data/History.md +12 -0
  5. data/README.md +25 -14
  6. data/Rakefile +64 -109
  7. data/appveyor.yml +23 -0
  8. data/ext/extconf.rb +10 -12
  9. data/ext/libusb_recipe.rb +29 -0
  10. data/lib/libusb/call.rb +14 -11
  11. data/lib/libusb/compat.rb +9 -9
  12. data/lib/libusb/context.rb +16 -1
  13. data/lib/libusb/dependencies.rb +7 -0
  14. data/lib/libusb/dev_handle.rb +13 -3
  15. data/lib/libusb/eventmachine.rb +4 -4
  16. data/lib/libusb/transfer.rb +71 -10
  17. data/lib/libusb/version_gem.rb +1 -1
  18. data/libusb.gemspec +7 -5
  19. data/ports/archives/libusb-1.0.21.tar.bz2 +0 -0
  20. data/test/test_libusb_bulk_stream_transfer.rb +1 -1
  21. data/test/test_libusb_descriptors.rb +4 -4
  22. data/test/test_libusb_event_machine.rb +7 -7
  23. data/test/test_libusb_hotplug.rb +15 -3
  24. data/test/test_libusb_iso_transfer.rb +1 -1
  25. data/test/test_libusb_mass_storage.rb +19 -19
  26. data/test/test_libusb_mass_storage2.rb +1 -1
  27. data/test/test_libusb_structs.rb +13 -0
  28. data/test/test_libusb_threads.rb +2 -2
  29. data/wireshark-usb-sniffer.png +0 -0
  30. metadata +32 -156
  31. checksums.yaml.gz.sig +0 -0
  32. data.tar.gz.sig +0 -0
  33. data/ext/libusb-1.0.20/AUTHORS +0 -89
  34. data/ext/libusb-1.0.20/COPYING +0 -504
  35. data/ext/libusb-1.0.20/ChangeLog +0 -227
  36. data/ext/libusb-1.0.20/INSTALL +0 -234
  37. data/ext/libusb-1.0.20/Makefile.am +0 -28
  38. data/ext/libusb-1.0.20/Makefile.in +0 -897
  39. data/ext/libusb-1.0.20/NEWS +0 -2
  40. data/ext/libusb-1.0.20/PORTING +0 -94
  41. data/ext/libusb-1.0.20/README +0 -29
  42. data/ext/libusb-1.0.20/TODO +0 -2
  43. data/ext/libusb-1.0.20/Xcode/common.xcconfig +0 -49
  44. data/ext/libusb-1.0.20/Xcode/config.h +0 -28
  45. data/ext/libusb-1.0.20/Xcode/debug.xcconfig +0 -29
  46. data/ext/libusb-1.0.20/Xcode/libusb.xcconfig +0 -21
  47. data/ext/libusb-1.0.20/Xcode/libusb.xcodeproj/project.pbxproj +0 -865
  48. data/ext/libusb-1.0.20/Xcode/libusb_debug.xcconfig +0 -21
  49. data/ext/libusb-1.0.20/Xcode/libusb_release.xcconfig +0 -21
  50. data/ext/libusb-1.0.20/Xcode/release.xcconfig +0 -30
  51. data/ext/libusb-1.0.20/aclocal.m4 +0 -1193
  52. data/ext/libusb-1.0.20/android/README +0 -114
  53. data/ext/libusb-1.0.20/android/config.h +0 -81
  54. data/ext/libusb-1.0.20/android/jni/Android.mk +0 -23
  55. data/ext/libusb-1.0.20/android/jni/Application.mk +0 -24
  56. data/ext/libusb-1.0.20/android/jni/examples.mk +0 -134
  57. data/ext/libusb-1.0.20/android/jni/libusb.mk +0 -54
  58. data/ext/libusb-1.0.20/android/jni/tests.mk +0 -56
  59. data/ext/libusb-1.0.20/compile +0 -347
  60. data/ext/libusb-1.0.20/config.guess +0 -1421
  61. data/ext/libusb-1.0.20/config.h.in +0 -155
  62. data/ext/libusb-1.0.20/config.sub +0 -1807
  63. data/ext/libusb-1.0.20/configure +0 -15466
  64. data/ext/libusb-1.0.20/configure.ac +0 -326
  65. data/ext/libusb-1.0.20/depcomp +0 -791
  66. data/ext/libusb-1.0.20/doc/Makefile.am +0 -9
  67. data/ext/libusb-1.0.20/doc/Makefile.in +0 -456
  68. data/ext/libusb-1.0.20/doc/doxygen.cfg.in +0 -2334
  69. data/ext/libusb-1.0.20/examples/Makefile.am +0 -19
  70. data/ext/libusb-1.0.20/examples/Makefile.in +0 -713
  71. data/ext/libusb-1.0.20/examples/dpfp.c +0 -506
  72. data/ext/libusb-1.0.20/examples/dpfp_threaded.c +0 -549
  73. data/ext/libusb-1.0.20/examples/ezusb.c +0 -831
  74. data/ext/libusb-1.0.20/examples/ezusb.h +0 -120
  75. data/ext/libusb-1.0.20/examples/fxload.c +0 -308
  76. data/ext/libusb-1.0.20/examples/getopt/getopt.c +0 -1060
  77. data/ext/libusb-1.0.20/examples/getopt/getopt.h +0 -180
  78. data/ext/libusb-1.0.20/examples/getopt/getopt1.c +0 -188
  79. data/ext/libusb-1.0.20/examples/hotplugtest.c +0 -122
  80. data/ext/libusb-1.0.20/examples/listdevs.c +0 -71
  81. data/ext/libusb-1.0.20/examples/sam3u_benchmark.c +0 -193
  82. data/ext/libusb-1.0.20/examples/xusb.c +0 -1130
  83. data/ext/libusb-1.0.20/install-sh +0 -501
  84. data/ext/libusb-1.0.20/libusb-1.0.pc.in +0 -11
  85. data/ext/libusb-1.0.20/libusb/Makefile.am +0 -90
  86. data/ext/libusb-1.0.20/libusb/Makefile.in +0 -1053
  87. data/ext/libusb-1.0.20/libusb/core.c +0 -2452
  88. data/ext/libusb-1.0.20/libusb/descriptor.c +0 -1201
  89. data/ext/libusb-1.0.20/libusb/hotplug.c +0 -344
  90. data/ext/libusb-1.0.20/libusb/hotplug.h +0 -90
  91. data/ext/libusb-1.0.20/libusb/io.c +0 -2760
  92. data/ext/libusb-1.0.20/libusb/libusb-1.0.def +0 -168
  93. data/ext/libusb-1.0.20/libusb/libusb-1.0.rc +0 -61
  94. data/ext/libusb-1.0.20/libusb/libusb.h +0 -1999
  95. data/ext/libusb-1.0.20/libusb/libusbi.h +0 -1102
  96. data/ext/libusb-1.0.20/libusb/os/darwin_usb.c +0 -1969
  97. data/ext/libusb-1.0.20/libusb/os/darwin_usb.h +0 -158
  98. data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.am +0 -5
  99. data/ext/libusb-1.0.20/libusb/os/haiku/Makefile.in +0 -810
  100. data/ext/libusb-1.0.20/libusb/os/haiku/aclocal.m4 +0 -1193
  101. data/ext/libusb-1.0.20/libusb/os/haiku/compile +0 -347
  102. data/ext/libusb-1.0.20/libusb/os/haiku/config.guess +0 -1421
  103. data/ext/libusb-1.0.20/libusb/os/haiku/config.sub +0 -1807
  104. data/ext/libusb-1.0.20/libusb/os/haiku/configure +0 -17579
  105. data/ext/libusb-1.0.20/libusb/os/haiku/configure.ac +0 -8
  106. data/ext/libusb-1.0.20/libusb/os/haiku/depcomp +0 -791
  107. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_pollfs.cpp +0 -378
  108. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb.h +0 -112
  109. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_backend.cpp +0 -550
  110. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.cpp +0 -255
  111. data/ext/libusb-1.0.20/libusb/os/haiku/haiku_usb_raw.h +0 -180
  112. data/ext/libusb-1.0.20/libusb/os/haiku/install-sh +0 -501
  113. data/ext/libusb-1.0.20/libusb/os/haiku/ltmain.sh +0 -9655
  114. data/ext/libusb-1.0.20/libusb/os/haiku/m4/libtool.m4 +0 -7992
  115. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltoptions.m4 +0 -384
  116. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltsugar.m4 +0 -123
  117. data/ext/libusb-1.0.20/libusb/os/haiku/m4/ltversion.m4 +0 -23
  118. data/ext/libusb-1.0.20/libusb/os/haiku/m4/lt~obsolete.m4 +0 -98
  119. data/ext/libusb-1.0.20/libusb/os/haiku/missing +0 -215
  120. data/ext/libusb-1.0.20/libusb/os/linux_netlink.c +0 -369
  121. data/ext/libusb-1.0.20/libusb/os/linux_udev.c +0 -306
  122. data/ext/libusb-1.0.20/libusb/os/linux_usbfs.c +0 -2692
  123. data/ext/libusb-1.0.20/libusb/os/linux_usbfs.h +0 -192
  124. data/ext/libusb-1.0.20/libusb/os/netbsd_usb.c +0 -674
  125. data/ext/libusb-1.0.20/libusb/os/openbsd_usb.c +0 -768
  126. data/ext/libusb-1.0.20/libusb/os/poll_posix.c +0 -53
  127. data/ext/libusb-1.0.20/libusb/os/poll_posix.h +0 -11
  128. data/ext/libusb-1.0.20/libusb/os/poll_windows.c +0 -728
  129. data/ext/libusb-1.0.20/libusb/os/poll_windows.h +0 -131
  130. data/ext/libusb-1.0.20/libusb/os/threads_posix.c +0 -82
  131. data/ext/libusb-1.0.20/libusb/os/threads_posix.h +0 -50
  132. data/ext/libusb-1.0.20/libusb/os/threads_windows.c +0 -214
  133. data/ext/libusb-1.0.20/libusb/os/threads_windows.h +0 -87
  134. data/ext/libusb-1.0.20/libusb/os/wince_usb.c +0 -869
  135. data/ext/libusb-1.0.20/libusb/os/wince_usb.h +0 -131
  136. data/ext/libusb-1.0.20/libusb/os/windows_common.h +0 -108
  137. data/ext/libusb-1.0.20/libusb/os/windows_usb.c +0 -4643
  138. data/ext/libusb-1.0.20/libusb/os/windows_usb.h +0 -973
  139. data/ext/libusb-1.0.20/libusb/strerror.c +0 -202
  140. data/ext/libusb-1.0.20/libusb/sync.c +0 -308
  141. data/ext/libusb-1.0.20/libusb/version.h +0 -18
  142. data/ext/libusb-1.0.20/libusb/version_nano.h +0 -1
  143. data/ext/libusb-1.0.20/ltmain.sh +0 -9655
  144. data/ext/libusb-1.0.20/m4/libtool.m4 +0 -7992
  145. data/ext/libusb-1.0.20/m4/ltoptions.m4 +0 -384
  146. data/ext/libusb-1.0.20/m4/ltsugar.m4 +0 -123
  147. data/ext/libusb-1.0.20/m4/ltversion.m4 +0 -23
  148. data/ext/libusb-1.0.20/m4/lt~obsolete.m4 +0 -98
  149. data/ext/libusb-1.0.20/missing +0 -215
  150. data/ext/libusb-1.0.20/tests/Makefile.am +0 -6
  151. data/ext/libusb-1.0.20/tests/Makefile.in +0 -596
  152. data/ext/libusb-1.0.20/tests/libusb_testlib.h +0 -107
  153. data/ext/libusb-1.0.20/tests/stress.c +0 -160
  154. data/ext/libusb-1.0.20/tests/testlib.c +0 -277
  155. metadata.gz.sig +0 -0
@@ -1,2452 +0,0 @@
1
- /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
2
- /*
3
- * Core functions for libusb
4
- * Copyright © 2012-2013 Nathan Hjelm <hjelmn@cs.unm.edu>
5
- * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
6
- * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
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
- #include "config.h"
24
-
25
- #include <errno.h>
26
- #include <stdarg.h>
27
- #include <stdio.h>
28
- #include <stdlib.h>
29
- #include <string.h>
30
- #ifdef HAVE_SYS_TYPES_H
31
- #include <sys/types.h>
32
- #endif
33
- #ifdef HAVE_SYS_TIME_H
34
- #include <sys/time.h>
35
- #endif
36
- #ifdef HAVE_SYSLOG_H
37
- #include <syslog.h>
38
- #endif
39
-
40
- #ifdef __ANDROID__
41
- #include <android/log.h>
42
- #endif
43
-
44
- #include "libusbi.h"
45
- #include "hotplug.h"
46
-
47
- #if defined(OS_LINUX)
48
- const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend;
49
- #elif defined(OS_DARWIN)
50
- const struct usbi_os_backend * const usbi_backend = &darwin_backend;
51
- #elif defined(OS_OPENBSD)
52
- const struct usbi_os_backend * const usbi_backend = &openbsd_backend;
53
- #elif defined(OS_NETBSD)
54
- const struct usbi_os_backend * const usbi_backend = &netbsd_backend;
55
- #elif defined(OS_WINDOWS)
56
- const struct usbi_os_backend * const usbi_backend = &windows_backend;
57
- #elif defined(OS_WINCE)
58
- const struct usbi_os_backend * const usbi_backend = &wince_backend;
59
- #elif defined(OS_HAIKU)
60
- const struct usbi_os_backend * const usbi_backend = &haiku_usb_raw_backend;
61
- #else
62
- #error "Unsupported OS"
63
- #endif
64
-
65
- struct libusb_context *usbi_default_context = NULL;
66
- static const struct libusb_version libusb_version_internal =
67
- { LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
68
- LIBUSB_RC, "http://libusb.info" };
69
- static int default_context_refcnt = 0;
70
- static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
71
- static struct timeval timestamp_origin = { 0, 0 };
72
-
73
- usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
74
- struct list_head active_contexts_list;
75
-
76
- /**
77
- * \mainpage libusb-1.0 API Reference
78
- *
79
- * \section intro Introduction
80
- *
81
- * libusb is an open source library that allows you to communicate with USB
82
- * devices from userspace. For more info, see the
83
- * <a href="http://libusb.info">libusb homepage</a>.
84
- *
85
- * This documentation is aimed at application developers wishing to
86
- * communicate with USB peripherals from their own software. After reviewing
87
- * this documentation, feedback and questions can be sent to the
88
- * <a href="http://mailing-list.libusb.info">libusb-devel mailing list</a>.
89
- *
90
- * This documentation assumes knowledge of how to operate USB devices from
91
- * a software standpoint (descriptors, configurations, interfaces, endpoints,
92
- * control/bulk/interrupt/isochronous transfers, etc). Full information
93
- * can be found in the <a href="http://www.usb.org/developers/docs/">USB 3.0
94
- * Specification</a> which is available for free download. You can probably
95
- * find less verbose introductions by searching the web.
96
- *
97
- * \section API Application Programming Interface (API)
98
- *
99
- * See the \ref api page for a complete list of the libusb functions.
100
- *
101
- * \section features Library features
102
- *
103
- * - All transfer types supported (control/bulk/interrupt/isochronous)
104
- * - 2 transfer interfaces:
105
- * -# Synchronous (simple)
106
- * -# Asynchronous (more complicated, but more powerful)
107
- * - Thread safe (although the asynchronous interface means that you
108
- * usually won't need to thread)
109
- * - Lightweight with lean API
110
- * - Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer
111
- * - Hotplug support (on some platforms). See \ref hotplug.
112
- *
113
- * \section gettingstarted Getting Started
114
- *
115
- * To begin reading the API documentation, start with the Modules page which
116
- * links to the different categories of libusb's functionality.
117
- *
118
- * One decision you will have to make is whether to use the synchronous
119
- * or the asynchronous data transfer interface. The \ref io documentation
120
- * provides some insight into this topic.
121
- *
122
- * Some example programs can be found in the libusb source distribution under
123
- * the "examples" subdirectory. The libusb homepage includes a list of
124
- * real-life project examples which use libusb.
125
- *
126
- * \section errorhandling Error handling
127
- *
128
- * libusb functions typically return 0 on success or a negative error code
129
- * on failure. These negative error codes relate to LIBUSB_ERROR constants
130
- * which are listed on the \ref misc "miscellaneous" documentation page.
131
- *
132
- * \section msglog Debug message logging
133
- *
134
- * libusb uses stderr for all logging. By default, logging is set to NONE,
135
- * which means that no output will be produced. However, unless the library
136
- * has been compiled with logging disabled, then any application calls to
137
- * libusb_set_debug(), or the setting of the environmental variable
138
- * LIBUSB_DEBUG outside of the application, can result in logging being
139
- * produced. Your application should therefore not close stderr, but instead
140
- * direct it to the null device if its output is undesireable.
141
- *
142
- * The libusb_set_debug() function can be used to enable logging of certain
143
- * messages. Under standard configuration, libusb doesn't really log much
144
- * so you are advised to use this function to enable all error/warning/
145
- * informational messages. It will help debug problems with your software.
146
- *
147
- * The logged messages are unstructured. There is no one-to-one correspondence
148
- * between messages being logged and success or failure return codes from
149
- * libusb functions. There is no format to the messages, so you should not
150
- * try to capture or parse them. They are not and will not be localized.
151
- * These messages are not intended to being passed to your application user;
152
- * instead, you should interpret the error codes returned from libusb functions
153
- * and provide appropriate notification to the user. The messages are simply
154
- * there to aid you as a programmer, and if you're confused because you're
155
- * getting a strange error code from a libusb function, enabling message
156
- * logging may give you a suitable explanation.
157
- *
158
- * The LIBUSB_DEBUG environment variable can be used to enable message logging
159
- * at run-time. This environment variable should be set to a log level number,
160
- * which is interpreted the same as the libusb_set_debug() parameter. When this
161
- * environment variable is set, the message logging verbosity level is fixed
162
- * and libusb_set_debug() effectively does nothing.
163
- *
164
- * libusb can be compiled without any logging functions, useful for embedded
165
- * systems. In this case, libusb_set_debug() and the LIBUSB_DEBUG environment
166
- * variable have no effects.
167
- *
168
- * libusb can also be compiled with verbose debugging messages always. When
169
- * the library is compiled in this way, all messages of all verbosities are
170
- * always logged. libusb_set_debug() and the LIBUSB_DEBUG environment variable
171
- * have no effects.
172
- *
173
- * \section remarks Other remarks
174
- *
175
- * libusb does have imperfections. The \ref caveats "caveats" page attempts
176
- * to document these.
177
- */
178
-
179
- /**
180
- * \page caveats Caveats
181
- *
182
- * \section devresets Device resets
183
- *
184
- * The libusb_reset_device() function allows you to reset a device. If your
185
- * program has to call such a function, it should obviously be aware that
186
- * the reset will cause device state to change (e.g. register values may be
187
- * reset).
188
- *
189
- * The problem is that any other program could reset the device your program
190
- * is working with, at any time. libusb does not offer a mechanism to inform
191
- * you when this has happened, so if someone else resets your device it will
192
- * not be clear to your own program why the device state has changed.
193
- *
194
- * Ultimately, this is a limitation of writing drivers in userspace.
195
- * Separation from the USB stack in the underlying kernel makes it difficult
196
- * for the operating system to deliver such notifications to your program.
197
- * The Linux kernel USB stack allows such reset notifications to be delivered
198
- * to in-kernel USB drivers, but it is not clear how such notifications could
199
- * be delivered to second-class drivers that live in userspace.
200
- *
201
- * \section blockonly Blocking-only functionality
202
- *
203
- * The functionality listed below is only available through synchronous,
204
- * blocking functions. There are no asynchronous/non-blocking alternatives,
205
- * and no clear ways of implementing these.
206
- *
207
- * - Configuration activation (libusb_set_configuration())
208
- * - Interface/alternate setting activation (libusb_set_interface_alt_setting())
209
- * - Releasing of interfaces (libusb_release_interface())
210
- * - Clearing of halt/stall condition (libusb_clear_halt())
211
- * - Device resets (libusb_reset_device())
212
- *
213
- * \section configsel Configuration selection and handling
214
- *
215
- * When libusb presents a device handle to an application, there is a chance
216
- * that the corresponding device may be in unconfigured state. For devices
217
- * with multiple configurations, there is also a chance that the configuration
218
- * currently selected is not the one that the application wants to use.
219
- *
220
- * The obvious solution is to add a call to libusb_set_configuration() early
221
- * on during your device initialization routines, but there are caveats to
222
- * be aware of:
223
- * -# If the device is already in the desired configuration, calling
224
- * libusb_set_configuration() using the same configuration value will cause
225
- * a lightweight device reset. This may not be desirable behaviour.
226
- * -# In the case where the desired configuration is already active, libusb
227
- * may not even be able to perform a lightweight device reset. For example,
228
- * take my USB keyboard with fingerprint reader: I'm interested in driving
229
- * the fingerprint reader interface through libusb, but the kernel's
230
- * USB-HID driver will almost always have claimed the keyboard interface.
231
- * Because the kernel has claimed an interface, it is not even possible to
232
- * perform the lightweight device reset, so libusb_set_configuration() will
233
- * fail. (Luckily the device in question only has a single configuration.)
234
- * -# libusb will be unable to set a configuration if other programs or
235
- * drivers have claimed interfaces. In particular, this means that kernel
236
- * drivers must be detached from all the interfaces before
237
- * libusb_set_configuration() may succeed.
238
- *
239
- * One solution to some of the above problems is to consider the currently
240
- * active configuration. If the configuration we want is already active, then
241
- * we don't have to select any configuration:
242
- \code
243
- cfg = -1;
244
- libusb_get_configuration(dev, &cfg);
245
- if (cfg != desired)
246
- libusb_set_configuration(dev, desired);
247
- \endcode
248
- *
249
- * This is probably suitable for most scenarios, but is inherently racy:
250
- * another application or driver may change the selected configuration
251
- * <em>after</em> the libusb_get_configuration() call.
252
- *
253
- * Even in cases where libusb_set_configuration() succeeds, consider that other
254
- * applications or drivers may change configuration after your application
255
- * calls libusb_set_configuration().
256
- *
257
- * One possible way to lock your device into a specific configuration is as
258
- * follows:
259
- * -# Set the desired configuration (or use the logic above to realise that
260
- * it is already in the desired configuration)
261
- * -# Claim the interface that you wish to use
262
- * -# Check that the currently active configuration is the one that you want
263
- * to use.
264
- *
265
- * The above method works because once an interface is claimed, no application
266
- * or driver is able to select another configuration.
267
- *
268
- * \section earlycomp Early transfer completion
269
- *
270
- * NOTE: This section is currently Linux-centric. I am not sure if any of these
271
- * considerations apply to Darwin or other platforms.
272
- *
273
- * When a transfer completes early (i.e. when less data is received/sent in
274
- * any one packet than the transfer buffer allows for) then libusb is designed
275
- * to terminate the transfer immediately, not transferring or receiving any
276
- * more data unless other transfers have been queued by the user.
277
- *
278
- * On legacy platforms, libusb is unable to do this in all situations. After
279
- * the incomplete packet occurs, "surplus" data may be transferred. For recent
280
- * versions of libusb, this information is kept (the data length of the
281
- * transfer is updated) and, for device-to-host transfers, any surplus data was
282
- * added to the buffer. Still, this is not a nice solution because it loses the
283
- * information about the end of the short packet, and the user probably wanted
284
- * that surplus data to arrive in the next logical transfer.
285
- *
286
- *
287
- * \section zlp Zero length packets
288
- *
289
- * - libusb is able to send a packet of zero length to an endpoint simply by
290
- * submitting a transfer of zero length.
291
- * - The \ref libusb_transfer_flags::LIBUSB_TRANSFER_ADD_ZERO_PACKET
292
- * "LIBUSB_TRANSFER_ADD_ZERO_PACKET" flag is currently only supported on Linux.
293
- */
294
-
295
- /**
296
- * \page contexts Contexts
297
- *
298
- * It is possible that libusb may be used simultaneously from two independent
299
- * libraries linked into the same executable. For example, if your application
300
- * has a plugin-like system which allows the user to dynamically load a range
301
- * of modules into your program, it is feasible that two independently
302
- * developed modules may both use libusb.
303
- *
304
- * libusb is written to allow for these multiple user scenarios. The two
305
- * "instances" of libusb will not interfere: libusb_set_debug() calls
306
- * from one user will not affect the same settings for other users, other
307
- * users can continue using libusb after one of them calls libusb_exit(), etc.
308
- *
309
- * This is made possible through libusb's <em>context</em> concept. When you
310
- * call libusb_init(), you are (optionally) given a context. You can then pass
311
- * this context pointer back into future libusb functions.
312
- *
313
- * In order to keep things simple for more simplistic applications, it is
314
- * legal to pass NULL to all functions requiring a context pointer (as long as
315
- * you're sure no other code will attempt to use libusb from the same process).
316
- * When you pass NULL, the default context will be used. The default context
317
- * is created the first time a process calls libusb_init() when no other
318
- * context is alive. Contexts are destroyed during libusb_exit().
319
- *
320
- * The default context is reference-counted and can be shared. That means that
321
- * if libusb_init(NULL) is called twice within the same process, the two
322
- * users end up sharing the same context. The deinitialization and freeing of
323
- * the default context will only happen when the last user calls libusb_exit().
324
- * In other words, the default context is created and initialized when its
325
- * reference count goes from 0 to 1, and is deinitialized and destroyed when
326
- * its reference count goes from 1 to 0.
327
- *
328
- * You may be wondering why only a subset of libusb functions require a
329
- * context pointer in their function definition. Internally, libusb stores
330
- * context pointers in other objects (e.g. libusb_device instances) and hence
331
- * can infer the context from those objects.
332
- */
333
-
334
- /**
335
- * \page api Application Programming Interface
336
- *
337
- * This is the complete list of libusb functions, structures and
338
- * enumerations in alphabetical order.
339
- *
340
- * \section Functions
341
- * - libusb_alloc_streams()
342
- * - libusb_alloc_transfer()
343
- * - libusb_attach_kernel_driver()
344
- * - libusb_bulk_transfer()
345
- * - libusb_cancel_transfer()
346
- * - libusb_claim_interface()
347
- * - libusb_clear_halt()
348
- * - libusb_close()
349
- * - libusb_control_transfer()
350
- * - libusb_control_transfer_get_data()
351
- * - libusb_control_transfer_get_setup()
352
- * - libusb_cpu_to_le16()
353
- * - libusb_detach_kernel_driver()
354
- * - libusb_error_name()
355
- * - libusb_event_handler_active()
356
- * - libusb_event_handling_ok()
357
- * - libusb_exit()
358
- * - libusb_fill_bulk_stream_transfer()
359
- * - libusb_fill_bulk_transfer()
360
- * - libusb_fill_control_setup()
361
- * - libusb_fill_control_transfer()
362
- * - libusb_fill_interrupt_transfer()
363
- * - libusb_fill_iso_transfer()
364
- * - libusb_free_bos_descriptor()
365
- * - libusb_free_config_descriptor()
366
- * - libusb_free_container_id_descriptor()
367
- * - libusb_free_device_list()
368
- * - libusb_free_ss_endpoint_companion_descriptor()
369
- * - libusb_free_ss_usb_device_capability_descriptor()
370
- * - libusb_free_streams()
371
- * - libusb_free_transfer()
372
- * - libusb_free_usb_2_0_extension_descriptor()
373
- * - libusb_get_active_config_descriptor()
374
- * - libusb_get_bos_descriptor()
375
- * - libusb_get_bus_number()
376
- * - libusb_get_config_descriptor()
377
- * - libusb_get_config_descriptor_by_value()
378
- * - libusb_get_configuration()
379
- * - libusb_get_container_id_descriptor()
380
- * - libusb_get_descriptor()
381
- * - libusb_get_device()
382
- * - libusb_get_device_address()
383
- * - libusb_get_device_descriptor()
384
- * - libusb_get_device_list()
385
- * - libusb_get_device_speed()
386
- * - libusb_get_iso_packet_buffer()
387
- * - libusb_get_iso_packet_buffer_simple()
388
- * - libusb_get_max_iso_packet_size()
389
- * - libusb_get_max_packet_size()
390
- * - libusb_get_next_timeout()
391
- * - libusb_get_parent()
392
- * - libusb_get_port_number()
393
- * - libusb_get_port_numbers()
394
- * - libusb_get_ss_endpoint_companion_descriptor()
395
- * - libusb_get_ss_usb_device_capability_descriptor()
396
- * - libusb_get_string_descriptor()
397
- * - libusb_get_string_descriptor_ascii()
398
- * - libusb_get_usb_2_0_extension_descriptor()
399
- * - libusb_get_version()
400
- * - libusb_handle_events()
401
- * - libusb_handle_events_completed()
402
- * - libusb_handle_events_locked()
403
- * - libusb_handle_events_timeout()
404
- * - libusb_handle_events_timeout_completed()
405
- * - libusb_has_capability()
406
- * - libusb_hotplug_deregister_callback()
407
- * - libusb_hotplug_register_callback()
408
- * - libusb_init()
409
- * - libusb_interrupt_transfer()
410
- * - libusb_kernel_driver_active()
411
- * - libusb_lock_events()
412
- * - libusb_lock_event_waiters()
413
- * - libusb_open()
414
- * - libusb_open_device_with_vid_pid()
415
- * - libusb_pollfds_handle_timeouts()
416
- * - libusb_ref_device()
417
- * - libusb_release_interface()
418
- * - libusb_reset_device()
419
- * - libusb_set_auto_detach_kernel_driver()
420
- * - libusb_set_configuration()
421
- * - libusb_set_debug()
422
- * - libusb_set_interface_alt_setting()
423
- * - libusb_set_iso_packet_lengths()
424
- * - libusb_setlocale()
425
- * - libusb_set_pollfd_notifiers()
426
- * - libusb_strerror()
427
- * - libusb_submit_transfer()
428
- * - libusb_transfer_get_stream_id()
429
- * - libusb_transfer_set_stream_id()
430
- * - libusb_try_lock_events()
431
- * - libusb_unlock_events()
432
- * - libusb_unlock_event_waiters()
433
- * - libusb_unref_device()
434
- * - libusb_wait_for_event()
435
- *
436
- * \section Structures
437
- * - libusb_bos_descriptor
438
- * - libusb_bos_dev_capability_descriptor
439
- * - libusb_config_descriptor
440
- * - libusb_container_id_descriptor
441
- * - \ref libusb_context
442
- * - libusb_control_setup
443
- * - \ref libusb_device
444
- * - libusb_device_descriptor
445
- * - \ref libusb_device_handle
446
- * - libusb_endpoint_descriptor
447
- * - libusb_interface
448
- * - libusb_interface_descriptor
449
- * - libusb_iso_packet_descriptor
450
- * - libusb_pollfd
451
- * - libusb_ss_endpoint_companion_descriptor
452
- * - libusb_ss_usb_device_capability_descriptor
453
- * - libusb_transfer
454
- * - libusb_usb_2_0_extension_descriptor
455
- * - libusb_version
456
- *
457
- * \section Enums
458
- * - \ref libusb_bos_type
459
- * - \ref libusb_capability
460
- * - \ref libusb_class_code
461
- * - \ref libusb_descriptor_type
462
- * - \ref libusb_endpoint_direction
463
- * - \ref libusb_error
464
- * - \ref libusb_iso_sync_type
465
- * - \ref libusb_iso_usage_type
466
- * - \ref libusb_log_level
467
- * - \ref libusb_request_recipient
468
- * - \ref libusb_request_type
469
- * - \ref libusb_speed
470
- * - \ref libusb_ss_usb_device_capability_attributes
471
- * - \ref libusb_standard_request
472
- * - \ref libusb_supported_speed
473
- * - \ref libusb_transfer_flags
474
- * - \ref libusb_transfer_status
475
- * - \ref libusb_transfer_type
476
- * - \ref libusb_usb_2_0_extension_attributes
477
- */
478
-
479
- /**
480
- * @defgroup lib Library initialization/deinitialization
481
- * This page details how to initialize and deinitialize libusb. Initialization
482
- * must be performed before using any libusb functionality, and similarly you
483
- * must not call any libusb functions after deinitialization.
484
- */
485
-
486
- /**
487
- * @defgroup dev Device handling and enumeration
488
- * The functionality documented below is designed to help with the following
489
- * operations:
490
- * - Enumerating the USB devices currently attached to the system
491
- * - Choosing a device to operate from your software
492
- * - Opening and closing the chosen device
493
- *
494
- * \section nutshell In a nutshell...
495
- *
496
- * The description below really makes things sound more complicated than they
497
- * actually are. The following sequence of function calls will be suitable
498
- * for almost all scenarios and does not require you to have such a deep
499
- * understanding of the resource management issues:
500
- * \code
501
- // discover devices
502
- libusb_device **list;
503
- libusb_device *found = NULL;
504
- ssize_t cnt = libusb_get_device_list(NULL, &list);
505
- ssize_t i = 0;
506
- int err = 0;
507
- if (cnt < 0)
508
- error();
509
-
510
- for (i = 0; i < cnt; i++) {
511
- libusb_device *device = list[i];
512
- if (is_interesting(device)) {
513
- found = device;
514
- break;
515
- }
516
- }
517
-
518
- if (found) {
519
- libusb_device_handle *handle;
520
-
521
- err = libusb_open(found, &handle);
522
- if (err)
523
- error();
524
- // etc
525
- }
526
-
527
- libusb_free_device_list(list, 1);
528
- \endcode
529
- *
530
- * The two important points:
531
- * - You asked libusb_free_device_list() to unreference the devices (2nd
532
- * parameter)
533
- * - You opened the device before freeing the list and unreferencing the
534
- * devices
535
- *
536
- * If you ended up with a handle, you can now proceed to perform I/O on the
537
- * device.
538
- *
539
- * \section devshandles Devices and device handles
540
- * libusb has a concept of a USB device, represented by the
541
- * \ref libusb_device opaque type. A device represents a USB device that
542
- * is currently or was previously connected to the system. Using a reference
543
- * to a device, you can determine certain information about the device (e.g.
544
- * you can read the descriptor data).
545
- *
546
- * The libusb_get_device_list() function can be used to obtain a list of
547
- * devices currently connected to the system. This is known as device
548
- * discovery.
549
- *
550
- * Just because you have a reference to a device does not mean it is
551
- * necessarily usable. The device may have been unplugged, you may not have
552
- * permission to operate such device, or another program or driver may be
553
- * using the device.
554
- *
555
- * When you've found a device that you'd like to operate, you must ask
556
- * libusb to open the device using the libusb_open() function. Assuming
557
- * success, libusb then returns you a <em>device handle</em>
558
- * (a \ref libusb_device_handle pointer). All "real" I/O operations then
559
- * operate on the handle rather than the original device pointer.
560
- *
561
- * \section devref Device discovery and reference counting
562
- *
563
- * Device discovery (i.e. calling libusb_get_device_list()) returns a
564
- * freshly-allocated list of devices. The list itself must be freed when
565
- * you are done with it. libusb also needs to know when it is OK to free
566
- * the contents of the list - the devices themselves.
567
- *
568
- * To handle these issues, libusb provides you with two separate items:
569
- * - A function to free the list itself
570
- * - A reference counting system for the devices inside
571
- *
572
- * New devices presented by the libusb_get_device_list() function all have a
573
- * reference count of 1. You can increase and decrease reference count using
574
- * libusb_ref_device() and libusb_unref_device(). A device is destroyed when
575
- * its reference count reaches 0.
576
- *
577
- * With the above information in mind, the process of opening a device can
578
- * be viewed as follows:
579
- * -# Discover devices using libusb_get_device_list().
580
- * -# Choose the device that you want to operate, and call libusb_open().
581
- * -# Unref all devices in the discovered device list.
582
- * -# Free the discovered device list.
583
- *
584
- * The order is important - you must not unreference the device before
585
- * attempting to open it, because unreferencing it may destroy the device.
586
- *
587
- * For convenience, the libusb_free_device_list() function includes a
588
- * parameter to optionally unreference all the devices in the list before
589
- * freeing the list itself. This combines steps 3 and 4 above.
590
- *
591
- * As an implementation detail, libusb_open() actually adds a reference to
592
- * the device in question. This is because the device remains available
593
- * through the handle via libusb_get_device(). The reference is deleted during
594
- * libusb_close().
595
- */
596
-
597
- /** @defgroup misc Miscellaneous */
598
-
599
- /* we traverse usbfs without knowing how many devices we are going to find.
600
- * so we create this discovered_devs model which is similar to a linked-list
601
- * which grows when required. it can be freed once discovery has completed,
602
- * eliminating the need for a list node in the libusb_device structure
603
- * itself. */
604
- #define DISCOVERED_DEVICES_SIZE_STEP 8
605
-
606
- static struct discovered_devs *discovered_devs_alloc(void)
607
- {
608
- struct discovered_devs *ret =
609
- malloc(sizeof(*ret) + (sizeof(void *) * DISCOVERED_DEVICES_SIZE_STEP));
610
-
611
- if (ret) {
612
- ret->len = 0;
613
- ret->capacity = DISCOVERED_DEVICES_SIZE_STEP;
614
- }
615
- return ret;
616
- }
617
-
618
- /* append a device to the discovered devices collection. may realloc itself,
619
- * returning new discdevs. returns NULL on realloc failure. */
620
- struct discovered_devs *discovered_devs_append(
621
- struct discovered_devs *discdevs, struct libusb_device *dev)
622
- {
623
- size_t len = discdevs->len;
624
- size_t capacity;
625
-
626
- /* if there is space, just append the device */
627
- if (len < discdevs->capacity) {
628
- discdevs->devices[len] = libusb_ref_device(dev);
629
- discdevs->len++;
630
- return discdevs;
631
- }
632
-
633
- /* exceeded capacity, need to grow */
634
- usbi_dbg("need to increase capacity");
635
- capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
636
- discdevs = usbi_reallocf(discdevs,
637
- sizeof(*discdevs) + (sizeof(void *) * capacity));
638
- if (discdevs) {
639
- discdevs->capacity = capacity;
640
- discdevs->devices[len] = libusb_ref_device(dev);
641
- discdevs->len++;
642
- }
643
-
644
- return discdevs;
645
- }
646
-
647
- static void discovered_devs_free(struct discovered_devs *discdevs)
648
- {
649
- size_t i;
650
-
651
- for (i = 0; i < discdevs->len; i++)
652
- libusb_unref_device(discdevs->devices[i]);
653
-
654
- free(discdevs);
655
- }
656
-
657
- /* Allocate a new device with a specific session ID. The returned device has
658
- * a reference count of 1. */
659
- struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
660
- unsigned long session_id)
661
- {
662
- size_t priv_size = usbi_backend->device_priv_size;
663
- struct libusb_device *dev = calloc(1, sizeof(*dev) + priv_size);
664
- int r;
665
-
666
- if (!dev)
667
- return NULL;
668
-
669
- r = usbi_mutex_init(&dev->lock, NULL);
670
- if (r) {
671
- free(dev);
672
- return NULL;
673
- }
674
-
675
- dev->ctx = ctx;
676
- dev->refcnt = 1;
677
- dev->session_data = session_id;
678
- dev->speed = LIBUSB_SPEED_UNKNOWN;
679
-
680
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
681
- usbi_connect_device (dev);
682
- }
683
-
684
- return dev;
685
- }
686
-
687
- void usbi_connect_device(struct libusb_device *dev)
688
- {
689
- struct libusb_context *ctx = DEVICE_CTX(dev);
690
-
691
- dev->attached = 1;
692
-
693
- usbi_mutex_lock(&dev->ctx->usb_devs_lock);
694
- list_add(&dev->list, &dev->ctx->usb_devs);
695
- usbi_mutex_unlock(&dev->ctx->usb_devs_lock);
696
-
697
- /* Signal that an event has occurred for this device if we support hotplug AND
698
- * the hotplug message list is ready. This prevents an event from getting raised
699
- * during initial enumeration. */
700
- if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_msgs.next) {
701
- usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED);
702
- }
703
- }
704
-
705
- void usbi_disconnect_device(struct libusb_device *dev)
706
- {
707
- struct libusb_context *ctx = DEVICE_CTX(dev);
708
-
709
- usbi_mutex_lock(&dev->lock);
710
- dev->attached = 0;
711
- usbi_mutex_unlock(&dev->lock);
712
-
713
- usbi_mutex_lock(&ctx->usb_devs_lock);
714
- list_del(&dev->list);
715
- usbi_mutex_unlock(&ctx->usb_devs_lock);
716
-
717
- /* Signal that an event has occurred for this device if we support hotplug AND
718
- * the hotplug message list is ready. This prevents an event from getting raised
719
- * during initial enumeration. libusb_handle_events will take care of dereferencing
720
- * the device. */
721
- if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_msgs.next) {
722
- usbi_hotplug_notification(ctx, dev, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT);
723
- }
724
- }
725
-
726
- /* Perform some final sanity checks on a newly discovered device. If this
727
- * function fails (negative return code), the device should not be added
728
- * to the discovered device list. */
729
- int usbi_sanitize_device(struct libusb_device *dev)
730
- {
731
- int r;
732
- uint8_t num_configurations;
733
-
734
- r = usbi_device_cache_descriptor(dev);
735
- if (r < 0)
736
- return r;
737
-
738
- num_configurations = dev->device_descriptor.bNumConfigurations;
739
- if (num_configurations > USB_MAXCONFIG) {
740
- usbi_err(DEVICE_CTX(dev), "too many configurations");
741
- return LIBUSB_ERROR_IO;
742
- } else if (0 == num_configurations)
743
- usbi_dbg("zero configurations, maybe an unauthorized device");
744
-
745
- dev->num_configurations = num_configurations;
746
- return 0;
747
- }
748
-
749
- /* Examine libusb's internal list of known devices, looking for one with
750
- * a specific session ID. Returns the matching device if it was found, and
751
- * NULL otherwise. */
752
- struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
753
- unsigned long session_id)
754
- {
755
- struct libusb_device *dev;
756
- struct libusb_device *ret = NULL;
757
-
758
- usbi_mutex_lock(&ctx->usb_devs_lock);
759
- list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device)
760
- if (dev->session_data == session_id) {
761
- ret = libusb_ref_device(dev);
762
- break;
763
- }
764
- usbi_mutex_unlock(&ctx->usb_devs_lock);
765
-
766
- return ret;
767
- }
768
-
769
- /** @ingroup dev
770
- * Returns a list of USB devices currently attached to the system. This is
771
- * your entry point into finding a USB device to operate.
772
- *
773
- * You are expected to unreference all the devices when you are done with
774
- * them, and then free the list with libusb_free_device_list(). Note that
775
- * libusb_free_device_list() can unref all the devices for you. Be careful
776
- * not to unreference a device you are about to open until after you have
777
- * opened it.
778
- *
779
- * This return value of this function indicates the number of devices in
780
- * the resultant list. The list is actually one element larger, as it is
781
- * NULL-terminated.
782
- *
783
- * \param ctx the context to operate on, or NULL for the default context
784
- * \param list output location for a list of devices. Must be later freed with
785
- * libusb_free_device_list().
786
- * \returns the number of devices in the outputted list, or any
787
- * \ref libusb_error according to errors encountered by the backend.
788
- */
789
- ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
790
- libusb_device ***list)
791
- {
792
- struct discovered_devs *discdevs = discovered_devs_alloc();
793
- struct libusb_device **ret;
794
- int r = 0;
795
- ssize_t i, len;
796
- USBI_GET_CONTEXT(ctx);
797
- usbi_dbg("");
798
-
799
- if (!discdevs)
800
- return LIBUSB_ERROR_NO_MEM;
801
-
802
- if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
803
- /* backend provides hotplug support */
804
- struct libusb_device *dev;
805
-
806
- if (usbi_backend->hotplug_poll)
807
- usbi_backend->hotplug_poll();
808
-
809
- usbi_mutex_lock(&ctx->usb_devs_lock);
810
- list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) {
811
- discdevs = discovered_devs_append(discdevs, dev);
812
-
813
- if (!discdevs) {
814
- r = LIBUSB_ERROR_NO_MEM;
815
- break;
816
- }
817
- }
818
- usbi_mutex_unlock(&ctx->usb_devs_lock);
819
- } else {
820
- /* backend does not provide hotplug support */
821
- r = usbi_backend->get_device_list(ctx, &discdevs);
822
- }
823
-
824
- if (r < 0) {
825
- len = r;
826
- goto out;
827
- }
828
-
829
- /* convert discovered_devs into a list */
830
- len = discdevs->len;
831
- ret = calloc(len + 1, sizeof(struct libusb_device *));
832
- if (!ret) {
833
- len = LIBUSB_ERROR_NO_MEM;
834
- goto out;
835
- }
836
-
837
- ret[len] = NULL;
838
- for (i = 0; i < len; i++) {
839
- struct libusb_device *dev = discdevs->devices[i];
840
- ret[i] = libusb_ref_device(dev);
841
- }
842
- *list = ret;
843
-
844
- out:
845
- discovered_devs_free(discdevs);
846
- return len;
847
- }
848
-
849
- /** \ingroup dev
850
- * Frees a list of devices previously discovered using
851
- * libusb_get_device_list(). If the unref_devices parameter is set, the
852
- * reference count of each device in the list is decremented by 1.
853
- * \param list the list to free
854
- * \param unref_devices whether to unref the devices in the list
855
- */
856
- void API_EXPORTED libusb_free_device_list(libusb_device **list,
857
- int unref_devices)
858
- {
859
- if (!list)
860
- return;
861
-
862
- if (unref_devices) {
863
- int i = 0;
864
- struct libusb_device *dev;
865
-
866
- while ((dev = list[i++]) != NULL)
867
- libusb_unref_device(dev);
868
- }
869
- free(list);
870
- }
871
-
872
- /** \ingroup dev
873
- * Get the number of the bus that a device is connected to.
874
- * \param dev a device
875
- * \returns the bus number
876
- */
877
- uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev)
878
- {
879
- return dev->bus_number;
880
- }
881
-
882
- /** \ingroup dev
883
- * Get the number of the port that a device is connected to.
884
- * Unless the OS does something funky, or you are hot-plugging USB extension cards,
885
- * the port number returned by this call is usually guaranteed to be uniquely tied
886
- * to a physical port, meaning that different devices plugged on the same physical
887
- * port should return the same port number.
888
- *
889
- * But outside of this, there is no guarantee that the port number returned by this
890
- * call will remain the same, or even match the order in which ports have been
891
- * numbered by the HUB/HCD manufacturer.
892
- *
893
- * \param dev a device
894
- * \returns the port number (0 if not available)
895
- */
896
- uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
897
- {
898
- return dev->port_number;
899
- }
900
-
901
- /** \ingroup dev
902
- * Get the list of all port numbers from root for the specified device
903
- *
904
- * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102
905
- * \param dev a device
906
- * \param port_numbers the array that should contain the port numbers
907
- * \param port_numbers_len the maximum length of the array. As per the USB 3.0
908
- * specs, the current maximum limit for the depth is 7.
909
- * \returns the number of elements filled
910
- * \returns LIBUSB_ERROR_OVERFLOW if the array is too small
911
- */
912
- int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
913
- uint8_t* port_numbers, int port_numbers_len)
914
- {
915
- int i = port_numbers_len;
916
- struct libusb_context *ctx = DEVICE_CTX(dev);
917
-
918
- if (port_numbers_len <= 0)
919
- return LIBUSB_ERROR_INVALID_PARAM;
920
-
921
- // HCDs can be listed as devices with port #0
922
- while((dev) && (dev->port_number != 0)) {
923
- if (--i < 0) {
924
- usbi_warn(ctx, "port numbers array is too small");
925
- return LIBUSB_ERROR_OVERFLOW;
926
- }
927
- port_numbers[i] = dev->port_number;
928
- dev = dev->parent_dev;
929
- }
930
- if (i < port_numbers_len)
931
- memmove(port_numbers, &port_numbers[i], port_numbers_len - i);
932
- return port_numbers_len - i;
933
- }
934
-
935
- /** \ingroup dev
936
- * Deprecated please use libusb_get_port_numbers instead.
937
- */
938
- int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev,
939
- uint8_t* port_numbers, uint8_t port_numbers_len)
940
- {
941
- UNUSED(ctx);
942
-
943
- return libusb_get_port_numbers(dev, port_numbers, port_numbers_len);
944
- }
945
-
946
- /** \ingroup dev
947
- * Get the the parent from the specified device.
948
- * \param dev a device
949
- * \returns the device parent or NULL if not available
950
- * You should issue a \ref libusb_get_device_list() before calling this
951
- * function and make sure that you only access the parent before issuing
952
- * \ref libusb_free_device_list(). The reason is that libusb currently does
953
- * not maintain a permanent list of device instances, and therefore can
954
- * only guarantee that parents are fully instantiated within a
955
- * libusb_get_device_list() - libusb_free_device_list() block.
956
- */
957
- DEFAULT_VISIBILITY
958
- libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev)
959
- {
960
- return dev->parent_dev;
961
- }
962
-
963
- /** \ingroup dev
964
- * Get the address of the device on the bus it is connected to.
965
- * \param dev a device
966
- * \returns the device address
967
- */
968
- uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev)
969
- {
970
- return dev->device_address;
971
- }
972
-
973
- /** \ingroup dev
974
- * Get the negotiated connection speed for a device.
975
- * \param dev a device
976
- * \returns a \ref libusb_speed code, where LIBUSB_SPEED_UNKNOWN means that
977
- * the OS doesn't know or doesn't support returning the negotiated speed.
978
- */
979
- int API_EXPORTED libusb_get_device_speed(libusb_device *dev)
980
- {
981
- return dev->speed;
982
- }
983
-
984
- static const struct libusb_endpoint_descriptor *find_endpoint(
985
- struct libusb_config_descriptor *config, unsigned char endpoint)
986
- {
987
- int iface_idx;
988
- for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) {
989
- const struct libusb_interface *iface = &config->interface[iface_idx];
990
- int altsetting_idx;
991
-
992
- for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting;
993
- altsetting_idx++) {
994
- const struct libusb_interface_descriptor *altsetting
995
- = &iface->altsetting[altsetting_idx];
996
- int ep_idx;
997
-
998
- for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) {
999
- const struct libusb_endpoint_descriptor *ep =
1000
- &altsetting->endpoint[ep_idx];
1001
- if (ep->bEndpointAddress == endpoint)
1002
- return ep;
1003
- }
1004
- }
1005
- }
1006
- return NULL;
1007
- }
1008
-
1009
- /** \ingroup dev
1010
- * Convenience function to retrieve the wMaxPacketSize value for a particular
1011
- * endpoint in the active device configuration.
1012
- *
1013
- * This function was originally intended to be of assistance when setting up
1014
- * isochronous transfers, but a design mistake resulted in this function
1015
- * instead. It simply returns the wMaxPacketSize value without considering
1016
- * its contents. If you're dealing with isochronous transfers, you probably
1017
- * want libusb_get_max_iso_packet_size() instead.
1018
- *
1019
- * \param dev a device
1020
- * \param endpoint address of the endpoint in question
1021
- * \returns the wMaxPacketSize value
1022
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1023
- * \returns LIBUSB_ERROR_OTHER on other failure
1024
- */
1025
- int API_EXPORTED libusb_get_max_packet_size(libusb_device *dev,
1026
- unsigned char endpoint)
1027
- {
1028
- struct libusb_config_descriptor *config;
1029
- const struct libusb_endpoint_descriptor *ep;
1030
- int r;
1031
-
1032
- r = libusb_get_active_config_descriptor(dev, &config);
1033
- if (r < 0) {
1034
- usbi_err(DEVICE_CTX(dev),
1035
- "could not retrieve active config descriptor");
1036
- return LIBUSB_ERROR_OTHER;
1037
- }
1038
-
1039
- ep = find_endpoint(config, endpoint);
1040
- if (!ep) {
1041
- r = LIBUSB_ERROR_NOT_FOUND;
1042
- goto out;
1043
- }
1044
-
1045
- r = ep->wMaxPacketSize;
1046
-
1047
- out:
1048
- libusb_free_config_descriptor(config);
1049
- return r;
1050
- }
1051
-
1052
- /** \ingroup dev
1053
- * Calculate the maximum packet size which a specific endpoint is capable is
1054
- * sending or receiving in the duration of 1 microframe
1055
- *
1056
- * Only the active configuration is examined. The calculation is based on the
1057
- * wMaxPacketSize field in the endpoint descriptor as described in section
1058
- * 9.6.6 in the USB 2.0 specifications.
1059
- *
1060
- * If acting on an isochronous or interrupt endpoint, this function will
1061
- * multiply the value found in bits 0:10 by the number of transactions per
1062
- * microframe (determined by bits 11:12). Otherwise, this function just
1063
- * returns the numeric value found in bits 0:10.
1064
- *
1065
- * This function is useful for setting up isochronous transfers, for example
1066
- * you might pass the return value from this function to
1067
- * libusb_set_iso_packet_lengths() in order to set the length field of every
1068
- * isochronous packet in a transfer.
1069
- *
1070
- * Since v1.0.3.
1071
- *
1072
- * \param dev a device
1073
- * \param endpoint address of the endpoint in question
1074
- * \returns the maximum packet size which can be sent/received on this endpoint
1075
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1076
- * \returns LIBUSB_ERROR_OTHER on other failure
1077
- */
1078
- int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
1079
- unsigned char endpoint)
1080
- {
1081
- struct libusb_config_descriptor *config;
1082
- const struct libusb_endpoint_descriptor *ep;
1083
- enum libusb_transfer_type ep_type;
1084
- uint16_t val;
1085
- int r;
1086
-
1087
- r = libusb_get_active_config_descriptor(dev, &config);
1088
- if (r < 0) {
1089
- usbi_err(DEVICE_CTX(dev),
1090
- "could not retrieve active config descriptor");
1091
- return LIBUSB_ERROR_OTHER;
1092
- }
1093
-
1094
- ep = find_endpoint(config, endpoint);
1095
- if (!ep) {
1096
- r = LIBUSB_ERROR_NOT_FOUND;
1097
- goto out;
1098
- }
1099
-
1100
- val = ep->wMaxPacketSize;
1101
- ep_type = (enum libusb_transfer_type) (ep->bmAttributes & 0x3);
1102
-
1103
- r = val & 0x07ff;
1104
- if (ep_type == LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
1105
- || ep_type == LIBUSB_TRANSFER_TYPE_INTERRUPT)
1106
- r *= (1 + ((val >> 11) & 3));
1107
-
1108
- out:
1109
- libusb_free_config_descriptor(config);
1110
- return r;
1111
- }
1112
-
1113
- /** \ingroup dev
1114
- * Increment the reference count of a device.
1115
- * \param dev the device to reference
1116
- * \returns the same device
1117
- */
1118
- DEFAULT_VISIBILITY
1119
- libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev)
1120
- {
1121
- usbi_mutex_lock(&dev->lock);
1122
- dev->refcnt++;
1123
- usbi_mutex_unlock(&dev->lock);
1124
- return dev;
1125
- }
1126
-
1127
- /** \ingroup dev
1128
- * Decrement the reference count of a device. If the decrement operation
1129
- * causes the reference count to reach zero, the device shall be destroyed.
1130
- * \param dev the device to unreference
1131
- */
1132
- void API_EXPORTED libusb_unref_device(libusb_device *dev)
1133
- {
1134
- int refcnt;
1135
-
1136
- if (!dev)
1137
- return;
1138
-
1139
- usbi_mutex_lock(&dev->lock);
1140
- refcnt = --dev->refcnt;
1141
- usbi_mutex_unlock(&dev->lock);
1142
-
1143
- if (refcnt == 0) {
1144
- usbi_dbg("destroy device %d.%d", dev->bus_number, dev->device_address);
1145
-
1146
- libusb_unref_device(dev->parent_dev);
1147
-
1148
- if (usbi_backend->destroy_device)
1149
- usbi_backend->destroy_device(dev);
1150
-
1151
- if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
1152
- /* backend does not support hotplug */
1153
- usbi_disconnect_device(dev);
1154
- }
1155
-
1156
- usbi_mutex_destroy(&dev->lock);
1157
- free(dev);
1158
- }
1159
- }
1160
-
1161
- /*
1162
- * Signal the event pipe so that the event handling thread will be
1163
- * interrupted to process an internal event.
1164
- */
1165
- int usbi_signal_event(struct libusb_context *ctx)
1166
- {
1167
- unsigned char dummy = 1;
1168
- ssize_t r;
1169
-
1170
- /* write some data on event pipe to interrupt event handlers */
1171
- r = usbi_write(ctx->event_pipe[1], &dummy, sizeof(dummy));
1172
- if (r != sizeof(dummy)) {
1173
- usbi_warn(ctx, "internal signalling write failed");
1174
- return LIBUSB_ERROR_IO;
1175
- }
1176
-
1177
- return 0;
1178
- }
1179
-
1180
- /*
1181
- * Clear the event pipe so that the event handling will no longer be
1182
- * interrupted.
1183
- */
1184
- int usbi_clear_event(struct libusb_context *ctx)
1185
- {
1186
- unsigned char dummy;
1187
- ssize_t r;
1188
-
1189
- /* read some data on event pipe to clear it */
1190
- r = usbi_read(ctx->event_pipe[0], &dummy, sizeof(dummy));
1191
- if (r != sizeof(dummy)) {
1192
- usbi_warn(ctx, "internal signalling read failed");
1193
- return LIBUSB_ERROR_IO;
1194
- }
1195
-
1196
- return 0;
1197
- }
1198
-
1199
- /** \ingroup dev
1200
- * Open a device and obtain a device handle. A handle allows you to perform
1201
- * I/O on the device in question.
1202
- *
1203
- * Internally, this function adds a reference to the device and makes it
1204
- * available to you through libusb_get_device(). This reference is removed
1205
- * during libusb_close().
1206
- *
1207
- * This is a non-blocking function; no requests are sent over the bus.
1208
- *
1209
- * \param dev the device to open
1210
- * \param handle output location for the returned device handle pointer. Only
1211
- * populated when the return code is 0.
1212
- * \returns 0 on success
1213
- * \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
1214
- * \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
1215
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1216
- * \returns another LIBUSB_ERROR code on other failure
1217
- */
1218
- int API_EXPORTED libusb_open(libusb_device *dev,
1219
- libusb_device_handle **handle)
1220
- {
1221
- struct libusb_context *ctx = DEVICE_CTX(dev);
1222
- struct libusb_device_handle *_handle;
1223
- size_t priv_size = usbi_backend->device_handle_priv_size;
1224
- int r;
1225
- usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
1226
-
1227
- if (!dev->attached) {
1228
- return LIBUSB_ERROR_NO_DEVICE;
1229
- }
1230
-
1231
- _handle = malloc(sizeof(*_handle) + priv_size);
1232
- if (!_handle)
1233
- return LIBUSB_ERROR_NO_MEM;
1234
-
1235
- r = usbi_mutex_init(&_handle->lock, NULL);
1236
- if (r) {
1237
- free(_handle);
1238
- return LIBUSB_ERROR_OTHER;
1239
- }
1240
-
1241
- _handle->dev = libusb_ref_device(dev);
1242
- _handle->auto_detach_kernel_driver = 0;
1243
- _handle->claimed_interfaces = 0;
1244
- memset(&_handle->os_priv, 0, priv_size);
1245
-
1246
- r = usbi_backend->open(_handle);
1247
- if (r < 0) {
1248
- usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
1249
- libusb_unref_device(dev);
1250
- usbi_mutex_destroy(&_handle->lock);
1251
- free(_handle);
1252
- return r;
1253
- }
1254
-
1255
- usbi_mutex_lock(&ctx->open_devs_lock);
1256
- list_add(&_handle->list, &ctx->open_devs);
1257
- usbi_mutex_unlock(&ctx->open_devs_lock);
1258
- *handle = _handle;
1259
-
1260
- return 0;
1261
- }
1262
-
1263
- /** \ingroup dev
1264
- * Convenience function for finding a device with a particular
1265
- * <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended
1266
- * for those scenarios where you are using libusb to knock up a quick test
1267
- * application - it allows you to avoid calling libusb_get_device_list() and
1268
- * worrying about traversing/freeing the list.
1269
- *
1270
- * This function has limitations and is hence not intended for use in real
1271
- * applications: if multiple devices have the same IDs it will only
1272
- * give you the first one, etc.
1273
- *
1274
- * \param ctx the context to operate on, or NULL for the default context
1275
- * \param vendor_id the idVendor value to search for
1276
- * \param product_id the idProduct value to search for
1277
- * \returns a handle for the first found device, or NULL on error or if the
1278
- * device could not be found. */
1279
- DEFAULT_VISIBILITY
1280
- libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid(
1281
- libusb_context *ctx, uint16_t vendor_id, uint16_t product_id)
1282
- {
1283
- struct libusb_device **devs;
1284
- struct libusb_device *found = NULL;
1285
- struct libusb_device *dev;
1286
- struct libusb_device_handle *handle = NULL;
1287
- size_t i = 0;
1288
- int r;
1289
-
1290
- if (libusb_get_device_list(ctx, &devs) < 0)
1291
- return NULL;
1292
-
1293
- while ((dev = devs[i++]) != NULL) {
1294
- struct libusb_device_descriptor desc;
1295
- r = libusb_get_device_descriptor(dev, &desc);
1296
- if (r < 0)
1297
- goto out;
1298
- if (desc.idVendor == vendor_id && desc.idProduct == product_id) {
1299
- found = dev;
1300
- break;
1301
- }
1302
- }
1303
-
1304
- if (found) {
1305
- r = libusb_open(found, &handle);
1306
- if (r < 0)
1307
- handle = NULL;
1308
- }
1309
-
1310
- out:
1311
- libusb_free_device_list(devs, 1);
1312
- return handle;
1313
- }
1314
-
1315
- static void do_close(struct libusb_context *ctx,
1316
- struct libusb_device_handle *dev_handle)
1317
- {
1318
- struct usbi_transfer *itransfer;
1319
- struct usbi_transfer *tmp;
1320
-
1321
- libusb_lock_events(ctx);
1322
-
1323
- /* remove any transfers in flight that are for this device */
1324
- usbi_mutex_lock(&ctx->flying_transfers_lock);
1325
-
1326
- /* safe iteration because transfers may be being deleted */
1327
- list_for_each_entry_safe(itransfer, tmp, &ctx->flying_transfers, list, struct usbi_transfer) {
1328
- struct libusb_transfer *transfer =
1329
- USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
1330
-
1331
- if (transfer->dev_handle != dev_handle)
1332
- continue;
1333
-
1334
- if (!(itransfer->flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
1335
- usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know");
1336
-
1337
- if (itransfer->flags & USBI_TRANSFER_CANCELLING)
1338
- usbi_warn(ctx, "A cancellation for an in-flight transfer hasn't completed but closing the device handle");
1339
- else
1340
- usbi_err(ctx, "A cancellation hasn't even been scheduled on the transfer for which the device is closing");
1341
- }
1342
-
1343
- /* remove from the list of in-flight transfers and make sure
1344
- * we don't accidentally use the device handle in the future
1345
- * (or that such accesses will be easily caught and identified as a crash)
1346
- */
1347
- usbi_mutex_lock(&itransfer->lock);
1348
- list_del(&itransfer->list);
1349
- transfer->dev_handle = NULL;
1350
- usbi_mutex_unlock(&itransfer->lock);
1351
-
1352
- /* it is up to the user to free up the actual transfer struct. this is
1353
- * just making sure that we don't attempt to process the transfer after
1354
- * the device handle is invalid
1355
- */
1356
- usbi_dbg("Removed transfer %p from the in-flight list because device handle %p closed",
1357
- transfer, dev_handle);
1358
- }
1359
- usbi_mutex_unlock(&ctx->flying_transfers_lock);
1360
-
1361
- libusb_unlock_events(ctx);
1362
-
1363
- usbi_mutex_lock(&ctx->open_devs_lock);
1364
- list_del(&dev_handle->list);
1365
- usbi_mutex_unlock(&ctx->open_devs_lock);
1366
-
1367
- usbi_backend->close(dev_handle);
1368
- libusb_unref_device(dev_handle->dev);
1369
- usbi_mutex_destroy(&dev_handle->lock);
1370
- free(dev_handle);
1371
- }
1372
-
1373
- /** \ingroup dev
1374
- * Close a device handle. Should be called on all open handles before your
1375
- * application exits.
1376
- *
1377
- * Internally, this function destroys the reference that was added by
1378
- * libusb_open() on the given device.
1379
- *
1380
- * This is a non-blocking function; no requests are sent over the bus.
1381
- *
1382
- * \param dev_handle the handle to close
1383
- */
1384
- void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
1385
- {
1386
- struct libusb_context *ctx;
1387
- int pending_events;
1388
-
1389
- if (!dev_handle)
1390
- return;
1391
- usbi_dbg("");
1392
-
1393
- ctx = HANDLE_CTX(dev_handle);
1394
-
1395
- /* Similarly to libusb_open(), we want to interrupt all event handlers
1396
- * at this point. More importantly, we want to perform the actual close of
1397
- * the device while holding the event handling lock (preventing any other
1398
- * thread from doing event handling) because we will be removing a file
1399
- * descriptor from the polling loop. */
1400
-
1401
- /* Record that we are closing a device.
1402
- * Only signal an event if there are no prior pending events. */
1403
- usbi_mutex_lock(&ctx->event_data_lock);
1404
- pending_events = usbi_pending_events(ctx);
1405
- ctx->device_close++;
1406
- if (!pending_events)
1407
- usbi_signal_event(ctx);
1408
- usbi_mutex_unlock(&ctx->event_data_lock);
1409
-
1410
- /* take event handling lock */
1411
- libusb_lock_events(ctx);
1412
-
1413
- /* Close the device */
1414
- do_close(ctx, dev_handle);
1415
-
1416
- /* We're done with closing this device.
1417
- * Clear the event pipe if there are no further pending events. */
1418
- usbi_mutex_lock(&ctx->event_data_lock);
1419
- ctx->device_close--;
1420
- pending_events = usbi_pending_events(ctx);
1421
- if (!pending_events)
1422
- usbi_clear_event(ctx);
1423
- usbi_mutex_unlock(&ctx->event_data_lock);
1424
-
1425
- /* Release event handling lock and wake up event waiters */
1426
- libusb_unlock_events(ctx);
1427
- }
1428
-
1429
- /** \ingroup dev
1430
- * Get the underlying device for a handle. This function does not modify
1431
- * the reference count of the returned device, so do not feel compelled to
1432
- * unreference it when you are done.
1433
- * \param dev_handle a device handle
1434
- * \returns the underlying device
1435
- */
1436
- DEFAULT_VISIBILITY
1437
- libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
1438
- {
1439
- return dev_handle->dev;
1440
- }
1441
-
1442
- /** \ingroup dev
1443
- * Determine the bConfigurationValue of the currently active configuration.
1444
- *
1445
- * You could formulate your own control request to obtain this information,
1446
- * but this function has the advantage that it may be able to retrieve the
1447
- * information from operating system caches (no I/O involved).
1448
- *
1449
- * If the OS does not cache this information, then this function will block
1450
- * while a control transfer is submitted to retrieve the information.
1451
- *
1452
- * This function will return a value of 0 in the <tt>config</tt> output
1453
- * parameter if the device is in unconfigured state.
1454
- *
1455
- * \param dev a device handle
1456
- * \param config output location for the bConfigurationValue of the active
1457
- * configuration (only valid for return code 0)
1458
- * \returns 0 on success
1459
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1460
- * \returns another LIBUSB_ERROR code on other failure
1461
- */
1462
- int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
1463
- int *config)
1464
- {
1465
- int r = LIBUSB_ERROR_NOT_SUPPORTED;
1466
-
1467
- usbi_dbg("");
1468
- if (usbi_backend->get_configuration)
1469
- r = usbi_backend->get_configuration(dev, config);
1470
-
1471
- if (r == LIBUSB_ERROR_NOT_SUPPORTED) {
1472
- uint8_t tmp = 0;
1473
- usbi_dbg("falling back to control message");
1474
- r = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN,
1475
- LIBUSB_REQUEST_GET_CONFIGURATION, 0, 0, &tmp, 1, 1000);
1476
- if (r == 0) {
1477
- usbi_err(HANDLE_CTX(dev), "zero bytes returned in ctrl transfer?");
1478
- r = LIBUSB_ERROR_IO;
1479
- } else if (r == 1) {
1480
- r = 0;
1481
- *config = tmp;
1482
- } else {
1483
- usbi_dbg("control failed, error %d", r);
1484
- }
1485
- }
1486
-
1487
- if (r == 0)
1488
- usbi_dbg("active config %d", *config);
1489
-
1490
- return r;
1491
- }
1492
-
1493
- /** \ingroup dev
1494
- * Set the active configuration for a device.
1495
- *
1496
- * The operating system may or may not have already set an active
1497
- * configuration on the device. It is up to your application to ensure the
1498
- * correct configuration is selected before you attempt to claim interfaces
1499
- * and perform other operations.
1500
- *
1501
- * If you call this function on a device already configured with the selected
1502
- * configuration, then this function will act as a lightweight device reset:
1503
- * it will issue a SET_CONFIGURATION request using the current configuration,
1504
- * causing most USB-related device state to be reset (altsetting reset to zero,
1505
- * endpoint halts cleared, toggles reset).
1506
- *
1507
- * You cannot change/reset configuration if your application has claimed
1508
- * interfaces. It is advised to set the desired configuration before claiming
1509
- * interfaces.
1510
- *
1511
- * Alternatively you can call libusb_release_interface() first. Note if you
1512
- * do things this way you must ensure that auto_detach_kernel_driver for
1513
- * <tt>dev</tt> is 0, otherwise the kernel driver will be re-attached when you
1514
- * release the interface(s).
1515
- *
1516
- * You cannot change/reset configuration if other applications or drivers have
1517
- * claimed interfaces.
1518
- *
1519
- * A configuration value of -1 will put the device in unconfigured state.
1520
- * The USB specifications state that a configuration value of 0 does this,
1521
- * however buggy devices exist which actually have a configuration 0.
1522
- *
1523
- * You should always use this function rather than formulating your own
1524
- * SET_CONFIGURATION control request. This is because the underlying operating
1525
- * system needs to know when such changes happen.
1526
- *
1527
- * This is a blocking function.
1528
- *
1529
- * \param dev a device handle
1530
- * \param configuration the bConfigurationValue of the configuration you
1531
- * wish to activate, or -1 if you wish to put the device in an unconfigured
1532
- * state
1533
- * \returns 0 on success
1534
- * \returns LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist
1535
- * \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
1536
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1537
- * \returns another LIBUSB_ERROR code on other failure
1538
- * \see libusb_set_auto_detach_kernel_driver()
1539
- */
1540
- int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
1541
- int configuration)
1542
- {
1543
- usbi_dbg("configuration %d", configuration);
1544
- return usbi_backend->set_configuration(dev, configuration);
1545
- }
1546
-
1547
- /** \ingroup dev
1548
- * Claim an interface on a given device handle. You must claim the interface
1549
- * you wish to use before you can perform I/O on any of its endpoints.
1550
- *
1551
- * It is legal to attempt to claim an already-claimed interface, in which
1552
- * case libusb just returns 0 without doing anything.
1553
- *
1554
- * If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel driver
1555
- * will be detached if necessary, on failure the detach error is returned.
1556
- *
1557
- * Claiming of interfaces is a purely logical operation; it does not cause
1558
- * any requests to be sent over the bus. Interface claiming is used to
1559
- * instruct the underlying operating system that your application wishes
1560
- * to take ownership of the interface.
1561
- *
1562
- * This is a non-blocking function.
1563
- *
1564
- * \param dev a device handle
1565
- * \param interface_number the <tt>bInterfaceNumber</tt> of the interface you
1566
- * wish to claim
1567
- * \returns 0 on success
1568
- * \returns LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist
1569
- * \returns LIBUSB_ERROR_BUSY if another program or driver has claimed the
1570
- * interface
1571
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1572
- * \returns a LIBUSB_ERROR code on other failure
1573
- * \see libusb_set_auto_detach_kernel_driver()
1574
- */
1575
- int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev,
1576
- int interface_number)
1577
- {
1578
- int r = 0;
1579
-
1580
- usbi_dbg("interface %d", interface_number);
1581
- if (interface_number >= USB_MAXINTERFACES)
1582
- return LIBUSB_ERROR_INVALID_PARAM;
1583
-
1584
- if (!dev->dev->attached)
1585
- return LIBUSB_ERROR_NO_DEVICE;
1586
-
1587
- usbi_mutex_lock(&dev->lock);
1588
- if (dev->claimed_interfaces & (1 << interface_number))
1589
- goto out;
1590
-
1591
- r = usbi_backend->claim_interface(dev, interface_number);
1592
- if (r == 0)
1593
- dev->claimed_interfaces |= 1 << interface_number;
1594
-
1595
- out:
1596
- usbi_mutex_unlock(&dev->lock);
1597
- return r;
1598
- }
1599
-
1600
- /** \ingroup dev
1601
- * Release an interface previously claimed with libusb_claim_interface(). You
1602
- * should release all claimed interfaces before closing a device handle.
1603
- *
1604
- * This is a blocking function. A SET_INTERFACE control request will be sent
1605
- * to the device, resetting interface state to the first alternate setting.
1606
- *
1607
- * If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel
1608
- * driver will be re-attached after releasing the interface.
1609
- *
1610
- * \param dev a device handle
1611
- * \param interface_number the <tt>bInterfaceNumber</tt> of the
1612
- * previously-claimed interface
1613
- * \returns 0 on success
1614
- * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed
1615
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1616
- * \returns another LIBUSB_ERROR code on other failure
1617
- * \see libusb_set_auto_detach_kernel_driver()
1618
- */
1619
- int API_EXPORTED libusb_release_interface(libusb_device_handle *dev,
1620
- int interface_number)
1621
- {
1622
- int r;
1623
-
1624
- usbi_dbg("interface %d", interface_number);
1625
- if (interface_number >= USB_MAXINTERFACES)
1626
- return LIBUSB_ERROR_INVALID_PARAM;
1627
-
1628
- usbi_mutex_lock(&dev->lock);
1629
- if (!(dev->claimed_interfaces & (1 << interface_number))) {
1630
- r = LIBUSB_ERROR_NOT_FOUND;
1631
- goto out;
1632
- }
1633
-
1634
- r = usbi_backend->release_interface(dev, interface_number);
1635
- if (r == 0)
1636
- dev->claimed_interfaces &= ~(1 << interface_number);
1637
-
1638
- out:
1639
- usbi_mutex_unlock(&dev->lock);
1640
- return r;
1641
- }
1642
-
1643
- /** \ingroup dev
1644
- * Activate an alternate setting for an interface. The interface must have
1645
- * been previously claimed with libusb_claim_interface().
1646
- *
1647
- * You should always use this function rather than formulating your own
1648
- * SET_INTERFACE control request. This is because the underlying operating
1649
- * system needs to know when such changes happen.
1650
- *
1651
- * This is a blocking function.
1652
- *
1653
- * \param dev a device handle
1654
- * \param interface_number the <tt>bInterfaceNumber</tt> of the
1655
- * previously-claimed interface
1656
- * \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate
1657
- * setting to activate
1658
- * \returns 0 on success
1659
- * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the
1660
- * requested alternate setting does not exist
1661
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1662
- * \returns another LIBUSB_ERROR code on other failure
1663
- */
1664
- int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev,
1665
- int interface_number, int alternate_setting)
1666
- {
1667
- usbi_dbg("interface %d altsetting %d",
1668
- interface_number, alternate_setting);
1669
- if (interface_number >= USB_MAXINTERFACES)
1670
- return LIBUSB_ERROR_INVALID_PARAM;
1671
-
1672
- usbi_mutex_lock(&dev->lock);
1673
- if (!dev->dev->attached) {
1674
- usbi_mutex_unlock(&dev->lock);
1675
- return LIBUSB_ERROR_NO_DEVICE;
1676
- }
1677
-
1678
- if (!(dev->claimed_interfaces & (1 << interface_number))) {
1679
- usbi_mutex_unlock(&dev->lock);
1680
- return LIBUSB_ERROR_NOT_FOUND;
1681
- }
1682
- usbi_mutex_unlock(&dev->lock);
1683
-
1684
- return usbi_backend->set_interface_altsetting(dev, interface_number,
1685
- alternate_setting);
1686
- }
1687
-
1688
- /** \ingroup dev
1689
- * Clear the halt/stall condition for an endpoint. Endpoints with halt status
1690
- * are unable to receive or transmit data until the halt condition is stalled.
1691
- *
1692
- * You should cancel all pending transfers before attempting to clear the halt
1693
- * condition.
1694
- *
1695
- * This is a blocking function.
1696
- *
1697
- * \param dev a device handle
1698
- * \param endpoint the endpoint to clear halt status
1699
- * \returns 0 on success
1700
- * \returns LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
1701
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1702
- * \returns another LIBUSB_ERROR code on other failure
1703
- */
1704
- int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev,
1705
- unsigned char endpoint)
1706
- {
1707
- usbi_dbg("endpoint %x", endpoint);
1708
- if (!dev->dev->attached)
1709
- return LIBUSB_ERROR_NO_DEVICE;
1710
-
1711
- return usbi_backend->clear_halt(dev, endpoint);
1712
- }
1713
-
1714
- /** \ingroup dev
1715
- * Perform a USB port reset to reinitialize a device. The system will attempt
1716
- * to restore the previous configuration and alternate settings after the
1717
- * reset has completed.
1718
- *
1719
- * If the reset fails, the descriptors change, or the previous state cannot be
1720
- * restored, the device will appear to be disconnected and reconnected. This
1721
- * means that the device handle is no longer valid (you should close it) and
1722
- * rediscover the device. A return code of LIBUSB_ERROR_NOT_FOUND indicates
1723
- * when this is the case.
1724
- *
1725
- * This is a blocking function which usually incurs a noticeable delay.
1726
- *
1727
- * \param dev a handle of the device to reset
1728
- * \returns 0 on success
1729
- * \returns LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the
1730
- * device has been disconnected
1731
- * \returns another LIBUSB_ERROR code on other failure
1732
- */
1733
- int API_EXPORTED libusb_reset_device(libusb_device_handle *dev)
1734
- {
1735
- usbi_dbg("");
1736
- if (!dev->dev->attached)
1737
- return LIBUSB_ERROR_NO_DEVICE;
1738
-
1739
- return usbi_backend->reset_device(dev);
1740
- }
1741
-
1742
- /** \ingroup asyncio
1743
- * Allocate up to num_streams usb bulk streams on the specified endpoints. This
1744
- * function takes an array of endpoints rather then a single endpoint because
1745
- * some protocols require that endpoints are setup with similar stream ids.
1746
- * All endpoints passed in must belong to the same interface.
1747
- *
1748
- * Note this function may return less streams then requested. Also note that the
1749
- * same number of streams are allocated for each endpoint in the endpoint array.
1750
- *
1751
- * Stream id 0 is reserved, and should not be used to communicate with devices.
1752
- * If libusb_alloc_streams() returns with a value of N, you may use stream ids
1753
- * 1 to N.
1754
- *
1755
- * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
1756
- *
1757
- * \param dev a device handle
1758
- * \param num_streams number of streams to try to allocate
1759
- * \param endpoints array of endpoints to allocate streams on
1760
- * \param num_endpoints length of the endpoints array
1761
- * \returns number of streams allocated, or a LIBUSB_ERROR code on failure
1762
- */
1763
- int API_EXPORTED libusb_alloc_streams(libusb_device_handle *dev,
1764
- uint32_t num_streams, unsigned char *endpoints, int num_endpoints)
1765
- {
1766
- usbi_dbg("streams %u eps %d", (unsigned) num_streams, num_endpoints);
1767
-
1768
- if (!dev->dev->attached)
1769
- return LIBUSB_ERROR_NO_DEVICE;
1770
-
1771
- if (usbi_backend->alloc_streams)
1772
- return usbi_backend->alloc_streams(dev, num_streams, endpoints,
1773
- num_endpoints);
1774
- else
1775
- return LIBUSB_ERROR_NOT_SUPPORTED;
1776
- }
1777
-
1778
- /** \ingroup asyncio
1779
- * Free usb bulk streams allocated with libusb_alloc_streams().
1780
- *
1781
- * Note streams are automatically free-ed when releasing an interface.
1782
- *
1783
- * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103
1784
- *
1785
- * \param dev a device handle
1786
- * \param endpoints array of endpoints to free streams on
1787
- * \param num_endpoints length of the endpoints array
1788
- * \returns LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure
1789
- */
1790
- int API_EXPORTED libusb_free_streams(libusb_device_handle *dev,
1791
- unsigned char *endpoints, int num_endpoints)
1792
- {
1793
- usbi_dbg("eps %d", num_endpoints);
1794
-
1795
- if (!dev->dev->attached)
1796
- return LIBUSB_ERROR_NO_DEVICE;
1797
-
1798
- if (usbi_backend->free_streams)
1799
- return usbi_backend->free_streams(dev, endpoints,
1800
- num_endpoints);
1801
- else
1802
- return LIBUSB_ERROR_NOT_SUPPORTED;
1803
- }
1804
-
1805
- /** \ingroup dev
1806
- * Determine if a kernel driver is active on an interface. If a kernel driver
1807
- * is active, you cannot claim the interface, and libusb will be unable to
1808
- * perform I/O.
1809
- *
1810
- * This functionality is not available on Windows.
1811
- *
1812
- * \param dev a device handle
1813
- * \param interface_number the interface to check
1814
- * \returns 0 if no kernel driver is active
1815
- * \returns 1 if a kernel driver is active
1816
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1817
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
1818
- * is not available
1819
- * \returns another LIBUSB_ERROR code on other failure
1820
- * \see libusb_detach_kernel_driver()
1821
- */
1822
- int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev,
1823
- int interface_number)
1824
- {
1825
- usbi_dbg("interface %d", interface_number);
1826
-
1827
- if (!dev->dev->attached)
1828
- return LIBUSB_ERROR_NO_DEVICE;
1829
-
1830
- if (usbi_backend->kernel_driver_active)
1831
- return usbi_backend->kernel_driver_active(dev, interface_number);
1832
- else
1833
- return LIBUSB_ERROR_NOT_SUPPORTED;
1834
- }
1835
-
1836
- /** \ingroup dev
1837
- * Detach a kernel driver from an interface. If successful, you will then be
1838
- * able to claim the interface and perform I/O.
1839
- *
1840
- * This functionality is not available on Darwin or Windows.
1841
- *
1842
- * Note that libusb itself also talks to the device through a special kernel
1843
- * driver, if this driver is already attached to the device, this call will
1844
- * not detach it and return LIBUSB_ERROR_NOT_FOUND.
1845
- *
1846
- * \param dev a device handle
1847
- * \param interface_number the interface to detach the driver from
1848
- * \returns 0 on success
1849
- * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1850
- * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1851
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1852
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
1853
- * is not available
1854
- * \returns another LIBUSB_ERROR code on other failure
1855
- * \see libusb_kernel_driver_active()
1856
- */
1857
- int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev,
1858
- int interface_number)
1859
- {
1860
- usbi_dbg("interface %d", interface_number);
1861
-
1862
- if (!dev->dev->attached)
1863
- return LIBUSB_ERROR_NO_DEVICE;
1864
-
1865
- if (usbi_backend->detach_kernel_driver)
1866
- return usbi_backend->detach_kernel_driver(dev, interface_number);
1867
- else
1868
- return LIBUSB_ERROR_NOT_SUPPORTED;
1869
- }
1870
-
1871
- /** \ingroup dev
1872
- * Re-attach an interface's kernel driver, which was previously detached
1873
- * using libusb_detach_kernel_driver(). This call is only effective on
1874
- * Linux and returns LIBUSB_ERROR_NOT_SUPPORTED on all other platforms.
1875
- *
1876
- * This functionality is not available on Darwin or Windows.
1877
- *
1878
- * \param dev a device handle
1879
- * \param interface_number the interface to attach the driver from
1880
- * \returns 0 on success
1881
- * \returns LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
1882
- * \returns LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
1883
- * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
1884
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
1885
- * is not available
1886
- * \returns LIBUSB_ERROR_BUSY if the driver cannot be attached because the
1887
- * interface is claimed by a program or driver
1888
- * \returns another LIBUSB_ERROR code on other failure
1889
- * \see libusb_kernel_driver_active()
1890
- */
1891
- int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev,
1892
- int interface_number)
1893
- {
1894
- usbi_dbg("interface %d", interface_number);
1895
-
1896
- if (!dev->dev->attached)
1897
- return LIBUSB_ERROR_NO_DEVICE;
1898
-
1899
- if (usbi_backend->attach_kernel_driver)
1900
- return usbi_backend->attach_kernel_driver(dev, interface_number);
1901
- else
1902
- return LIBUSB_ERROR_NOT_SUPPORTED;
1903
- }
1904
-
1905
- /** \ingroup dev
1906
- * Enable/disable libusb's automatic kernel driver detachment. When this is
1907
- * enabled libusb will automatically detach the kernel driver on an interface
1908
- * when claiming the interface, and attach it when releasing the interface.
1909
- *
1910
- * Automatic kernel driver detachment is disabled on newly opened device
1911
- * handles by default.
1912
- *
1913
- * On platforms which do not have LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER
1914
- * this function will return LIBUSB_ERROR_NOT_SUPPORTED, and libusb will
1915
- * continue as if this function was never called.
1916
- *
1917
- * \param dev a device handle
1918
- * \param enable whether to enable or disable auto kernel driver detachment
1919
- *
1920
- * \returns LIBUSB_SUCCESS on success
1921
- * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
1922
- * is not available
1923
- * \see libusb_claim_interface()
1924
- * \see libusb_release_interface()
1925
- * \see libusb_set_configuration()
1926
- */
1927
- int API_EXPORTED libusb_set_auto_detach_kernel_driver(
1928
- libusb_device_handle *dev, int enable)
1929
- {
1930
- if (!(usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
1931
- return LIBUSB_ERROR_NOT_SUPPORTED;
1932
-
1933
- dev->auto_detach_kernel_driver = enable;
1934
- return LIBUSB_SUCCESS;
1935
- }
1936
-
1937
- /** \ingroup lib
1938
- * Set log message verbosity.
1939
- *
1940
- * The default level is LIBUSB_LOG_LEVEL_NONE, which means no messages are ever
1941
- * printed. If you choose to increase the message verbosity level, ensure
1942
- * that your application does not close the stdout/stderr file descriptors.
1943
- *
1944
- * You are advised to use level LIBUSB_LOG_LEVEL_WARNING. libusb is conservative
1945
- * with its message logging and most of the time, will only log messages that
1946
- * explain error conditions and other oddities. This will help you debug
1947
- * your software.
1948
- *
1949
- * If the LIBUSB_DEBUG environment variable was set when libusb was
1950
- * initialized, this function does nothing: the message verbosity is fixed
1951
- * to the value in the environment variable.
1952
- *
1953
- * If libusb was compiled without any message logging, this function does
1954
- * nothing: you'll never get any messages.
1955
- *
1956
- * If libusb was compiled with verbose debug message logging, this function
1957
- * does nothing: you'll always get messages from all levels.
1958
- *
1959
- * \param ctx the context to operate on, or NULL for the default context
1960
- * \param level debug level to set
1961
- */
1962
- void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
1963
- {
1964
- USBI_GET_CONTEXT(ctx);
1965
- if (!ctx->debug_fixed)
1966
- ctx->debug = level;
1967
- }
1968
-
1969
- /** \ingroup lib
1970
- * Initialize libusb. This function must be called before calling any other
1971
- * libusb function.
1972
- *
1973
- * If you do not provide an output location for a context pointer, a default
1974
- * context will be created. If there was already a default context, it will
1975
- * be reused (and nothing will be initialized/reinitialized).
1976
- *
1977
- * \param context Optional output location for context pointer.
1978
- * Only valid on return code 0.
1979
- * \returns 0 on success, or a LIBUSB_ERROR code on failure
1980
- * \see contexts
1981
- */
1982
- int API_EXPORTED libusb_init(libusb_context **context)
1983
- {
1984
- struct libusb_device *dev, *next;
1985
- char *dbg = getenv("LIBUSB_DEBUG");
1986
- struct libusb_context *ctx;
1987
- static int first_init = 1;
1988
- int r = 0;
1989
-
1990
- usbi_mutex_static_lock(&default_context_lock);
1991
-
1992
- if (!timestamp_origin.tv_sec) {
1993
- usbi_gettimeofday(&timestamp_origin, NULL);
1994
- }
1995
-
1996
- if (!context && usbi_default_context) {
1997
- usbi_dbg("reusing default context");
1998
- default_context_refcnt++;
1999
- usbi_mutex_static_unlock(&default_context_lock);
2000
- return 0;
2001
- }
2002
-
2003
- ctx = calloc(1, sizeof(*ctx));
2004
- if (!ctx) {
2005
- r = LIBUSB_ERROR_NO_MEM;
2006
- goto err_unlock;
2007
- }
2008
-
2009
- #ifdef ENABLE_DEBUG_LOGGING
2010
- ctx->debug = LIBUSB_LOG_LEVEL_DEBUG;
2011
- #endif
2012
-
2013
- if (dbg) {
2014
- ctx->debug = atoi(dbg);
2015
- if (ctx->debug)
2016
- ctx->debug_fixed = 1;
2017
- }
2018
-
2019
- /* default context should be initialized before calling usbi_dbg */
2020
- if (!usbi_default_context) {
2021
- usbi_default_context = ctx;
2022
- default_context_refcnt++;
2023
- usbi_dbg("created default context");
2024
- }
2025
-
2026
- usbi_dbg("libusb v%u.%u.%u.%u%s", libusb_version_internal.major, libusb_version_internal.minor,
2027
- libusb_version_internal.micro, libusb_version_internal.nano, libusb_version_internal.rc);
2028
-
2029
- usbi_mutex_init(&ctx->usb_devs_lock, NULL);
2030
- usbi_mutex_init(&ctx->open_devs_lock, NULL);
2031
- usbi_mutex_init(&ctx->hotplug_cbs_lock, NULL);
2032
- list_init(&ctx->usb_devs);
2033
- list_init(&ctx->open_devs);
2034
- list_init(&ctx->hotplug_cbs);
2035
-
2036
- usbi_mutex_static_lock(&active_contexts_lock);
2037
- if (first_init) {
2038
- first_init = 0;
2039
- list_init (&active_contexts_list);
2040
- }
2041
- list_add (&ctx->list, &active_contexts_list);
2042
- usbi_mutex_static_unlock(&active_contexts_lock);
2043
-
2044
- if (usbi_backend->init) {
2045
- r = usbi_backend->init(ctx);
2046
- if (r)
2047
- goto err_free_ctx;
2048
- }
2049
-
2050
- r = usbi_io_init(ctx);
2051
- if (r < 0)
2052
- goto err_backend_exit;
2053
-
2054
- usbi_mutex_static_unlock(&default_context_lock);
2055
-
2056
- if (context)
2057
- *context = ctx;
2058
-
2059
- return 0;
2060
-
2061
- err_backend_exit:
2062
- if (usbi_backend->exit)
2063
- usbi_backend->exit();
2064
- err_free_ctx:
2065
- if (ctx == usbi_default_context) {
2066
- usbi_default_context = NULL;
2067
- default_context_refcnt--;
2068
- }
2069
-
2070
- usbi_mutex_static_lock(&active_contexts_lock);
2071
- list_del (&ctx->list);
2072
- usbi_mutex_static_unlock(&active_contexts_lock);
2073
-
2074
- usbi_mutex_lock(&ctx->usb_devs_lock);
2075
- list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
2076
- list_del(&dev->list);
2077
- libusb_unref_device(dev);
2078
- }
2079
- usbi_mutex_unlock(&ctx->usb_devs_lock);
2080
-
2081
- usbi_mutex_destroy(&ctx->open_devs_lock);
2082
- usbi_mutex_destroy(&ctx->usb_devs_lock);
2083
- usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
2084
-
2085
- free(ctx);
2086
- err_unlock:
2087
- usbi_mutex_static_unlock(&default_context_lock);
2088
- return r;
2089
- }
2090
-
2091
- /** \ingroup lib
2092
- * Deinitialize libusb. Should be called after closing all open devices and
2093
- * before your application terminates.
2094
- * \param ctx the context to deinitialize, or NULL for the default context
2095
- */
2096
- void API_EXPORTED libusb_exit(struct libusb_context *ctx)
2097
- {
2098
- struct libusb_device *dev, *next;
2099
- struct timeval tv = { 0, 0 };
2100
-
2101
- usbi_dbg("");
2102
- USBI_GET_CONTEXT(ctx);
2103
-
2104
- /* if working with default context, only actually do the deinitialization
2105
- * if we're the last user */
2106
- usbi_mutex_static_lock(&default_context_lock);
2107
- if (ctx == usbi_default_context) {
2108
- if (--default_context_refcnt > 0) {
2109
- usbi_dbg("not destroying default context");
2110
- usbi_mutex_static_unlock(&default_context_lock);
2111
- return;
2112
- }
2113
- usbi_dbg("destroying default context");
2114
- usbi_default_context = NULL;
2115
- }
2116
- usbi_mutex_static_unlock(&default_context_lock);
2117
-
2118
- usbi_mutex_static_lock(&active_contexts_lock);
2119
- list_del (&ctx->list);
2120
- usbi_mutex_static_unlock(&active_contexts_lock);
2121
-
2122
- if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
2123
- usbi_hotplug_deregister_all(ctx);
2124
-
2125
- /*
2126
- * Ensure any pending unplug events are read from the hotplug
2127
- * pipe. The usb_device-s hold in the events are no longer part
2128
- * of usb_devs, but the events still hold a reference!
2129
- *
2130
- * Note we don't do this if the application has left devices
2131
- * open (which implies a buggy app) to avoid packet completion
2132
- * handlers running when the app does not expect them to run.
2133
- */
2134
- if (list_empty(&ctx->open_devs))
2135
- libusb_handle_events_timeout(ctx, &tv);
2136
-
2137
- usbi_mutex_lock(&ctx->usb_devs_lock);
2138
- list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
2139
- list_del(&dev->list);
2140
- libusb_unref_device(dev);
2141
- }
2142
- usbi_mutex_unlock(&ctx->usb_devs_lock);
2143
- }
2144
-
2145
- /* a few sanity checks. don't bother with locking because unless
2146
- * there is an application bug, nobody will be accessing these. */
2147
- if (!list_empty(&ctx->usb_devs))
2148
- usbi_warn(ctx, "some libusb_devices were leaked");
2149
- if (!list_empty(&ctx->open_devs))
2150
- usbi_warn(ctx, "application left some devices open");
2151
-
2152
- usbi_io_exit(ctx);
2153
- if (usbi_backend->exit)
2154
- usbi_backend->exit();
2155
-
2156
- usbi_mutex_destroy(&ctx->open_devs_lock);
2157
- usbi_mutex_destroy(&ctx->usb_devs_lock);
2158
- usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
2159
- free(ctx);
2160
- }
2161
-
2162
- /** \ingroup misc
2163
- * Check at runtime if the loaded library has a given capability.
2164
- * This call should be performed after \ref libusb_init(), to ensure the
2165
- * backend has updated its capability set.
2166
- *
2167
- * \param capability the \ref libusb_capability to check for
2168
- * \returns nonzero if the running library has the capability, 0 otherwise
2169
- */
2170
- int API_EXPORTED libusb_has_capability(uint32_t capability)
2171
- {
2172
- switch (capability) {
2173
- case LIBUSB_CAP_HAS_CAPABILITY:
2174
- return 1;
2175
- case LIBUSB_CAP_HAS_HOTPLUG:
2176
- return !(usbi_backend->get_device_list);
2177
- case LIBUSB_CAP_HAS_HID_ACCESS:
2178
- return (usbi_backend->caps & USBI_CAP_HAS_HID_ACCESS);
2179
- case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
2180
- return (usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
2181
- }
2182
- return 0;
2183
- }
2184
-
2185
- /* this is defined in libusbi.h if needed */
2186
- #ifdef LIBUSB_GETTIMEOFDAY_WIN32
2187
- /*
2188
- * gettimeofday
2189
- * Implementation according to:
2190
- * The Open Group Base Specifications Issue 6
2191
- * IEEE Std 1003.1, 2004 Edition
2192
- */
2193
-
2194
- /*
2195
- * THIS SOFTWARE IS NOT COPYRIGHTED
2196
- *
2197
- * This source code is offered for use in the public domain. You may
2198
- * use, modify or distribute it freely.
2199
- *
2200
- * This code is distributed in the hope that it will be useful but
2201
- * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
2202
- * DISCLAIMED. This includes but is not limited to warranties of
2203
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
2204
- *
2205
- * Contributed by:
2206
- * Danny Smith <dannysmith@users.sourceforge.net>
2207
- */
2208
-
2209
- /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
2210
- #define _W32_FT_OFFSET (116444736000000000)
2211
-
2212
- int usbi_gettimeofday(struct timeval *tp, void *tzp)
2213
- {
2214
- union {
2215
- unsigned __int64 ns100; /* Time since 1 Jan 1601, in 100ns units */
2216
- FILETIME ft;
2217
- } _now;
2218
- UNUSED(tzp);
2219
-
2220
- if(tp) {
2221
- #if defined(OS_WINCE)
2222
- SYSTEMTIME st;
2223
- GetSystemTime(&st);
2224
- SystemTimeToFileTime(&st, &_now.ft);
2225
- #else
2226
- GetSystemTimeAsFileTime (&_now.ft);
2227
- #endif
2228
- tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
2229
- tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
2230
- }
2231
- /* Always return 0 as per Open Group Base Specifications Issue 6.
2232
- Do not set errno on error. */
2233
- return 0;
2234
- }
2235
- #endif
2236
-
2237
- static void usbi_log_str(struct libusb_context *ctx,
2238
- enum libusb_log_level level, const char * str)
2239
- {
2240
- #if defined(USE_SYSTEM_LOGGING_FACILITY)
2241
- #if defined(OS_WINDOWS) || defined(OS_WINCE)
2242
- /* Windows CE only supports the Unicode version of OutputDebugString. */
2243
- WCHAR wbuf[USBI_MAX_LOG_LEN];
2244
- MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, sizeof(wbuf));
2245
- OutputDebugStringW(wbuf);
2246
- #elif defined(__ANDROID__)
2247
- int priority = ANDROID_LOG_UNKNOWN;
2248
- switch (level) {
2249
- case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
2250
- case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break;
2251
- case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break;
2252
- case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
2253
- }
2254
- __android_log_write(priority, "libusb", str);
2255
- #elif defined(HAVE_SYSLOG_FUNC)
2256
- int syslog_level = LOG_INFO;
2257
- switch (level) {
2258
- case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break;
2259
- case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break;
2260
- case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break;
2261
- case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break;
2262
- }
2263
- syslog(syslog_level, "%s", str);
2264
- #else /* All of gcc, Clang, XCode seem to use #warning */
2265
- #warning System logging is not supported on this platform. Logging to stderr will be used instead.
2266
- fputs(str, stderr);
2267
- #endif
2268
- #else
2269
- fputs(str, stderr);
2270
- #endif /* USE_SYSTEM_LOGGING_FACILITY */
2271
- UNUSED(ctx);
2272
- UNUSED(level);
2273
- }
2274
-
2275
- void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
2276
- const char *function, const char *format, va_list args)
2277
- {
2278
- const char *prefix = "";
2279
- char buf[USBI_MAX_LOG_LEN];
2280
- struct timeval now;
2281
- int global_debug, header_len, text_len;
2282
- static int has_debug_header_been_displayed = 0;
2283
-
2284
- #ifdef ENABLE_DEBUG_LOGGING
2285
- global_debug = 1;
2286
- UNUSED(ctx);
2287
- #else
2288
- int ctx_level = 0;
2289
-
2290
- USBI_GET_CONTEXT(ctx);
2291
- if (ctx) {
2292
- ctx_level = ctx->debug;
2293
- } else {
2294
- char *dbg = getenv("LIBUSB_DEBUG");
2295
- if (dbg)
2296
- ctx_level = atoi(dbg);
2297
- }
2298
- global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG);
2299
- if (!ctx_level)
2300
- return;
2301
- if (level == LIBUSB_LOG_LEVEL_WARNING && ctx_level < LIBUSB_LOG_LEVEL_WARNING)
2302
- return;
2303
- if (level == LIBUSB_LOG_LEVEL_INFO && ctx_level < LIBUSB_LOG_LEVEL_INFO)
2304
- return;
2305
- if (level == LIBUSB_LOG_LEVEL_DEBUG && ctx_level < LIBUSB_LOG_LEVEL_DEBUG)
2306
- return;
2307
- #endif
2308
-
2309
- usbi_gettimeofday(&now, NULL);
2310
- if ((global_debug) && (!has_debug_header_been_displayed)) {
2311
- has_debug_header_been_displayed = 1;
2312
- usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
2313
- usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
2314
- }
2315
- if (now.tv_usec < timestamp_origin.tv_usec) {
2316
- now.tv_sec--;
2317
- now.tv_usec += 1000000;
2318
- }
2319
- now.tv_sec -= timestamp_origin.tv_sec;
2320
- now.tv_usec -= timestamp_origin.tv_usec;
2321
-
2322
- switch (level) {
2323
- case LIBUSB_LOG_LEVEL_INFO:
2324
- prefix = "info";
2325
- break;
2326
- case LIBUSB_LOG_LEVEL_WARNING:
2327
- prefix = "warning";
2328
- break;
2329
- case LIBUSB_LOG_LEVEL_ERROR:
2330
- prefix = "error";
2331
- break;
2332
- case LIBUSB_LOG_LEVEL_DEBUG:
2333
- prefix = "debug";
2334
- break;
2335
- case LIBUSB_LOG_LEVEL_NONE:
2336
- return;
2337
- default:
2338
- prefix = "unknown";
2339
- break;
2340
- }
2341
-
2342
- if (global_debug) {
2343
- header_len = snprintf(buf, sizeof(buf),
2344
- "[%2d.%06d] [%08x] libusb: %s [%s] ",
2345
- (int)now.tv_sec, (int)now.tv_usec, usbi_get_tid(), prefix, function);
2346
- } else {
2347
- header_len = snprintf(buf, sizeof(buf),
2348
- "libusb: %s [%s] ", prefix, function);
2349
- }
2350
-
2351
- if (header_len < 0 || header_len >= sizeof(buf)) {
2352
- /* Somehow snprintf failed to write to the buffer,
2353
- * remove the header so something useful is output. */
2354
- header_len = 0;
2355
- }
2356
- /* Make sure buffer is NUL terminated */
2357
- buf[header_len] = '\0';
2358
- text_len = vsnprintf(buf + header_len, sizeof(buf) - header_len,
2359
- format, args);
2360
- if (text_len < 0 || text_len + header_len >= sizeof(buf)) {
2361
- /* Truncated log output. On some platforms a -1 return value means
2362
- * that the output was truncated. */
2363
- text_len = sizeof(buf) - header_len;
2364
- }
2365
- if (header_len + text_len + sizeof(USBI_LOG_LINE_END) >= sizeof(buf)) {
2366
- /* Need to truncate the text slightly to fit on the terminator. */
2367
- text_len -= (header_len + text_len + sizeof(USBI_LOG_LINE_END)) - sizeof(buf);
2368
- }
2369
- strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
2370
-
2371
- usbi_log_str(ctx, level, buf);
2372
- }
2373
-
2374
- void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
2375
- const char *function, const char *format, ...)
2376
- {
2377
- va_list args;
2378
-
2379
- va_start (args, format);
2380
- usbi_log_v(ctx, level, function, format, args);
2381
- va_end (args);
2382
- }
2383
-
2384
- /** \ingroup misc
2385
- * Returns a constant NULL-terminated string with the ASCII name of a libusb
2386
- * error or transfer status code. The caller must not free() the returned
2387
- * string.
2388
- *
2389
- * \param error_code The \ref libusb_error or libusb_transfer_status code to
2390
- * return the name of.
2391
- * \returns The error name, or the string **UNKNOWN** if the value of
2392
- * error_code is not a known error / status code.
2393
- */
2394
- DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
2395
- {
2396
- switch (error_code) {
2397
- case LIBUSB_ERROR_IO:
2398
- return "LIBUSB_ERROR_IO";
2399
- case LIBUSB_ERROR_INVALID_PARAM:
2400
- return "LIBUSB_ERROR_INVALID_PARAM";
2401
- case LIBUSB_ERROR_ACCESS:
2402
- return "LIBUSB_ERROR_ACCESS";
2403
- case LIBUSB_ERROR_NO_DEVICE:
2404
- return "LIBUSB_ERROR_NO_DEVICE";
2405
- case LIBUSB_ERROR_NOT_FOUND:
2406
- return "LIBUSB_ERROR_NOT_FOUND";
2407
- case LIBUSB_ERROR_BUSY:
2408
- return "LIBUSB_ERROR_BUSY";
2409
- case LIBUSB_ERROR_TIMEOUT:
2410
- return "LIBUSB_ERROR_TIMEOUT";
2411
- case LIBUSB_ERROR_OVERFLOW:
2412
- return "LIBUSB_ERROR_OVERFLOW";
2413
- case LIBUSB_ERROR_PIPE:
2414
- return "LIBUSB_ERROR_PIPE";
2415
- case LIBUSB_ERROR_INTERRUPTED:
2416
- return "LIBUSB_ERROR_INTERRUPTED";
2417
- case LIBUSB_ERROR_NO_MEM:
2418
- return "LIBUSB_ERROR_NO_MEM";
2419
- case LIBUSB_ERROR_NOT_SUPPORTED:
2420
- return "LIBUSB_ERROR_NOT_SUPPORTED";
2421
- case LIBUSB_ERROR_OTHER:
2422
- return "LIBUSB_ERROR_OTHER";
2423
-
2424
- case LIBUSB_TRANSFER_ERROR:
2425
- return "LIBUSB_TRANSFER_ERROR";
2426
- case LIBUSB_TRANSFER_TIMED_OUT:
2427
- return "LIBUSB_TRANSFER_TIMED_OUT";
2428
- case LIBUSB_TRANSFER_CANCELLED:
2429
- return "LIBUSB_TRANSFER_CANCELLED";
2430
- case LIBUSB_TRANSFER_STALL:
2431
- return "LIBUSB_TRANSFER_STALL";
2432
- case LIBUSB_TRANSFER_NO_DEVICE:
2433
- return "LIBUSB_TRANSFER_NO_DEVICE";
2434
- case LIBUSB_TRANSFER_OVERFLOW:
2435
- return "LIBUSB_TRANSFER_OVERFLOW";
2436
-
2437
- case 0:
2438
- return "LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLETED";
2439
- default:
2440
- return "**UNKNOWN**";
2441
- }
2442
- }
2443
-
2444
- /** \ingroup misc
2445
- * Returns a pointer to const struct libusb_version with the version
2446
- * (major, minor, micro, nano and rc) of the running library.
2447
- */
2448
- DEFAULT_VISIBILITY
2449
- const struct libusb_version * LIBUSB_CALL libusb_get_version(void)
2450
- {
2451
- return &libusb_version_internal;
2452
- }