| 1 |
/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */ |
| 2 |
/* |
| 3 |
* Core functions for libusb |
| 4 |
* Copyright © 2012-2023 Nathan Hjelm <[email protected]> |
| 5 |
* Copyright © 2007-2008 Daniel Drake <[email protected]> |
| 6 |
* Copyright © 2001 Johannes Erdfelt <[email protected]> |
| 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 "libusbi.h" |
| 24 |
#include "version.h" |
| 25 |
|
| 26 |
#ifdef __ANDROID__ |
| 27 |
#include <android/log.h> |
| 28 |
#endif |
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
#ifdef HAVE_SYSLOG |
| 32 |
#include <syslog.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
static const struct libusb_version libusb_version_internal = |
| 36 |
{ LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO, |
| 37 |
LIBUSB_RC, "https://libusb.info" }; |
| 38 |
static struct timespec timestamp_origin; |
| 39 |
#if defined(ENABLE_LOGGING) && !defined(USE_SYSTEM_LOGGING_FACILITY) |
| 40 |
static libusb_log_cb log_handler; |
| 41 |
#endif |
| 42 |
|
| 43 |
struct libusb_context *usbi_default_context; |
| 44 |
struct libusb_context *usbi_fallback_context; |
| 45 |
static int default_context_refcnt; |
| 46 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 47 |
static usbi_atomic_t default_debug_level = -1; |
| 48 |
#endif |
| 49 |
static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER; |
| 50 |
static struct usbi_option default_context_options[LIBUSB_OPTION_MAX]; |
| 51 |
|
| 52 |
|
| 53 |
usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER; |
| 54 |
struct list_head active_contexts_list; |
| 55 |
|
| 56 |
/** |
| 57 |
* \mainpage libusb-1.0 API Reference |
| 58 |
* |
| 59 |
* \section intro Introduction |
| 60 |
* |
| 61 |
* libusb is an open source library that allows you to communicate with USB |
| 62 |
* devices from user space. For more info, see the |
| 63 |
* <a href="https://libusb.info">libusb homepage</a>. |
| 64 |
* |
| 65 |
* This documentation is aimed at application developers wishing to |
| 66 |
* communicate with USB peripherals from their own software. After reviewing |
| 67 |
* this documentation, feedback and questions can be sent to the |
| 68 |
* <a href="https://mailing-list.libusb.info">libusb-devel mailing list</a>. |
| 69 |
* |
| 70 |
* This documentation assumes knowledge of how to operate USB devices from |
| 71 |
* a software standpoint (descriptors, configurations, interfaces, endpoints, |
| 72 |
* control/bulk/interrupt/isochronous transfers, etc). Full information |
| 73 |
* can be found in the <a href="http://www.usb.org/developers/docs/">USB 3.0 |
| 74 |
* Specification</a> which is available for free download. You can probably |
| 75 |
* find less verbose introductions by searching the web. |
| 76 |
* |
| 77 |
* \section API Application Programming Interface (API) |
| 78 |
* |
| 79 |
* See the \ref libusb_api page for a complete list of the libusb functions. |
| 80 |
* |
| 81 |
* \section features Library features |
| 82 |
* |
| 83 |
* - All transfer types supported (control/bulk/interrupt/isochronous) |
| 84 |
* - 2 transfer interfaces: |
| 85 |
* -# Synchronous (simple) |
| 86 |
* -# Asynchronous (more complicated, but more powerful) |
| 87 |
* - Thread safe (although the asynchronous interface means that you |
| 88 |
* usually won't need to thread) |
| 89 |
* - Lightweight with lean API |
| 90 |
* - Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer |
| 91 |
* - Hotplug support (on some platforms). See \ref libusb_hotplug. |
| 92 |
* |
| 93 |
* \section gettingstarted Getting Started |
| 94 |
* |
| 95 |
* To begin reading the API documentation, start with the Topics page which |
| 96 |
* links to the different categories of libusb's functionality. |
| 97 |
* |
| 98 |
* One decision you will have to make is whether to use the synchronous |
| 99 |
* or the asynchronous data transfer interface. The \ref libusb_io documentation |
| 100 |
* provides some insight into this topic. |
| 101 |
* |
| 102 |
* Some example programs can be found in the libusb source distribution under |
| 103 |
* the "examples" subdirectory. The libusb homepage includes a list of |
| 104 |
* real-life project examples which use libusb. |
| 105 |
* |
| 106 |
* \section errorhandling Error handling |
| 107 |
* |
| 108 |
* libusb functions typically return 0 on success or a negative error code |
| 109 |
* on failure. These negative error codes relate to LIBUSB_ERROR constants |
| 110 |
* which are listed on the \ref libusb_misc "miscellaneous" documentation page. |
| 111 |
* |
| 112 |
* \section msglog Debug message logging |
| 113 |
* |
| 114 |
* libusb uses stderr for all logging. By default, logging is set to NONE, |
| 115 |
* which means that no output will be produced. However, unless the library |
| 116 |
* has been compiled with logging disabled, then any application calls to |
| 117 |
* libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level), |
| 118 |
* libusb_init_context, or the setting of the environmental variable |
| 119 |
* LIBUSB_DEBUG outside of the application, can result in logging being |
| 120 |
* produced. Your application should therefore not close stderr, but instead |
| 121 |
* direct it to the null device if its output is undesirable. |
| 122 |
* |
| 123 |
* The libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level) or |
| 124 |
* libusb_init_context functions can be used to enable logging of certain |
| 125 |
* messages. With the default configuration, libusb will not log much so if |
| 126 |
* you are advised to use one of these functions to enable all |
| 127 |
* error/warning/informational messages. It will help debug problems with your |
| 128 |
* software. |
| 129 |
* |
| 130 |
* The logged messages are unstructured. There is no one-to-one correspondence |
| 131 |
* between messages being logged and success or failure return codes from |
| 132 |
* libusb functions. There is no format to the messages, so you should not |
| 133 |
* try to capture or parse them. They are not and will not be localized. |
| 134 |
* These messages are not intended to being passed to your application user; |
| 135 |
* instead, you should interpret the error codes returned from libusb functions |
| 136 |
* and provide appropriate notification to the user. The messages are simply |
| 137 |
* there to aid you as a programmer, and if you're confused because you're |
| 138 |
* getting a strange error code from a libusb function, enabling message |
| 139 |
* logging may give you a suitable explanation. |
| 140 |
* |
| 141 |
* The LIBUSB_DEBUG environment variable can be used to enable message logging |
| 142 |
* at run-time. This environment variable should be set to a log level number, |
| 143 |
* which is interpreted the same as the |
| 144 |
* libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level), or |
| 145 |
* libusb_init_context(&ctx, &(struct libusb_init_option){.option = LIBUSB_OPTION_LOG_LEVEL, .value = {.ival = level}}, 0). |
| 146 |
* When the environment variable is set, the message logging verbosity level is |
| 147 |
* fixed and setting the LIBUSB_OPTION_LOG_LEVEL option has no effect. |
| 148 |
* |
| 149 |
* libusb can be compiled without any logging functions, useful for embedded |
| 150 |
* systems. In this case, neither the LIBUSB_OPTION_LOG_LEVEL option, nor the |
| 151 |
* LIBUSB_DEBUG environment variable will have any effect. |
| 152 |
* |
| 153 |
* libusb can also be compiled with verbose debugging messages always. When |
| 154 |
* the library is compiled in this way, all messages of all verbosities are |
| 155 |
* always logged. Again, in this case, neither the LIBUSB_OPTION_LOG_LEVEL |
| 156 |
* option, nor the LIBUSB_DEBUG environment variable will have any effect. |
| 157 |
* |
| 158 |
* \section remarks Other remarks |
| 159 |
* |
| 160 |
* libusb does have imperfections. The \ref libusb_caveats "caveats" page attempts |
| 161 |
* to document these. |
| 162 |
*/ |
| 163 |
|
| 164 |
/** |
| 165 |
* \page libusb_caveats Caveats |
| 166 |
* |
| 167 |
* \section threadsafety Thread safety |
| 168 |
* |
| 169 |
* libusb is designed to be completely thread-safe, but as with any API it |
| 170 |
* cannot prevent a user from sabotaging themselves, either intentionally or |
| 171 |
* otherwise. |
| 172 |
* |
| 173 |
* Observe the following general guidelines: |
| 174 |
* |
| 175 |
* - Calls to functions that release a resource (e.g. libusb_close(), |
| 176 |
* libusb_free_config_descriptor()) should not be called concurrently on |
| 177 |
* the same resource. This is no different than concurrently calling free() |
| 178 |
* on the same allocated pointer. |
| 179 |
* - Each individual \ref libusb_transfer should be prepared by a single |
| 180 |
* thread. In other words, no two threads should ever be concurrently |
| 181 |
* filling out the fields of a \ref libusb_transfer. You can liken this to |
| 182 |
* calling sprintf() with the same destination buffer from multiple threads. |
| 183 |
* The results will likely not be what you want unless the input parameters |
| 184 |
* are all the same, but its best to avoid this situation entirely. |
| 185 |
* - Both the \ref libusb_transfer structure and its associated data buffer |
| 186 |
* should not be accessed between the time the transfer is submitted and the |
| 187 |
* time the completion callback is invoked. You can think of "ownership" of |
| 188 |
* these things as being transferred to libusb while the transfer is active. |
| 189 |
* - The various "setter" functions (e.g. libusb_set_log_cb(), |
| 190 |
* libusb_set_pollfd_notifiers()) should not be called concurrently on the |
| 191 |
* resource. Though doing so will not lead to any undefined behavior, it |
| 192 |
* will likely produce results that the application does not expect. |
| 193 |
* |
| 194 |
* Rules for multiple threads and asynchronous I/O are detailed |
| 195 |
* \ref libusb_mtasync "here". |
| 196 |
* |
| 197 |
* \section fork Fork considerations |
| 198 |
* |
| 199 |
* libusb is <em>not</em> designed to work across fork() calls. Depending on |
| 200 |
* the platform, there may be resources in the parent process that are not |
| 201 |
* available to the child (e.g. the hotplug monitor thread on Linux). In |
| 202 |
* addition, since the parent and child will share libusb's internal file |
| 203 |
* descriptors, using libusb in any way from the child could cause the parent |
| 204 |
* process's \ref libusb_context to get into an inconsistent state. |
| 205 |
* |
| 206 |
* On Linux, libusb's file descriptors will be marked as CLOEXEC, which means |
| 207 |
* that it is safe to fork() and exec() without worrying about the child |
| 208 |
* process needing to clean up state or having access to these file descriptors. |
| 209 |
* Other platforms may not be so forgiving, so consider yourself warned! |
| 210 |
* |
| 211 |
* \section devresets Device resets |
| 212 |
* |
| 213 |
* The libusb_reset_device() function allows you to reset a device. If your |
| 214 |
* program has to call such a function, it should obviously be aware that |
| 215 |
* the reset will cause device state to change (e.g. register values may be |
| 216 |
* reset). |
| 217 |
* |
| 218 |
* The problem is that any other program could reset the device your program |
| 219 |
* is working with, at any time. libusb does not offer a mechanism to inform |
| 220 |
* you when this has happened, so if someone else resets your device it will |
| 221 |
* not be clear to your own program why the device state has changed. |
| 222 |
* |
| 223 |
* Ultimately, this is a limitation of writing drivers in user space. |
| 224 |
* Separation from the USB stack in the underlying kernel makes it difficult |
| 225 |
* for the operating system to deliver such notifications to your program. |
| 226 |
* The Linux kernel USB stack allows such reset notifications to be delivered |
| 227 |
* to in-kernel USB drivers, but it is not clear how such notifications could |
| 228 |
* be delivered to second-class drivers that live in user space. |
| 229 |
* |
| 230 |
* \section blockonly Blocking-only functionality |
| 231 |
* |
| 232 |
* The functionality listed below is only available through synchronous, |
| 233 |
* blocking functions. There are no asynchronous/non-blocking alternatives, |
| 234 |
* and no clear ways of implementing these. |
| 235 |
* |
| 236 |
* - Configuration activation (libusb_set_configuration()) |
| 237 |
* - Interface/alternate setting activation (libusb_set_interface_alt_setting()) |
| 238 |
* - Releasing of interfaces (libusb_release_interface()) |
| 239 |
* - Clearing of halt/stall condition (libusb_clear_halt()) |
| 240 |
* - Device resets (libusb_reset_device()) |
| 241 |
* |
| 242 |
* \section configsel Configuration selection and handling |
| 243 |
* |
| 244 |
* When libusb presents a device handle to an application, there is a chance |
| 245 |
* that the corresponding device may be in unconfigured state. For devices |
| 246 |
* with multiple configurations, there is also a chance that the configuration |
| 247 |
* currently selected is not the one that the application wants to use. |
| 248 |
* |
| 249 |
* The obvious solution is to add a call to libusb_set_configuration() early |
| 250 |
* on during your device initialization routines, but there are caveats to |
| 251 |
* be aware of: |
| 252 |
* -# If the device is already in the desired configuration, calling |
| 253 |
* libusb_set_configuration() using the same configuration value will cause |
| 254 |
* a lightweight device reset. This may not be desirable behaviour. |
| 255 |
* -# In the case where the desired configuration is already active, libusb |
| 256 |
* may not even be able to perform a lightweight device reset. For example, |
| 257 |
* take my USB keyboard with fingerprint reader: I'm interested in driving |
| 258 |
* the fingerprint reader interface through libusb, but the kernel's |
| 259 |
* USB-HID driver will almost always have claimed the keyboard interface. |
| 260 |
* Because the kernel has claimed an interface, it is not even possible to |
| 261 |
* perform the lightweight device reset, so libusb_set_configuration() will |
| 262 |
* fail. (Luckily the device in question only has a single configuration.) |
| 263 |
* -# libusb will be unable to set a configuration if other programs or |
| 264 |
* drivers have claimed interfaces. In particular, this means that kernel |
| 265 |
* drivers must be detached from all the interfaces before |
| 266 |
* libusb_set_configuration() may succeed. |
| 267 |
* |
| 268 |
* One solution to some of the above problems is to consider the currently |
| 269 |
* active configuration. If the configuration we want is already active, then |
| 270 |
* we don't have to select any configuration: |
| 271 |
\code |
| 272 |
cfg = -1; |
| 273 |
libusb_get_configuration(dev, &cfg); |
| 274 |
if (cfg != desired) |
| 275 |
libusb_set_configuration(dev, desired); |
| 276 |
\endcode |
| 277 |
* |
| 278 |
* This is probably suitable for most scenarios, but is inherently racy: |
| 279 |
* another application or driver may change the selected configuration |
| 280 |
* <em>after</em> the libusb_get_configuration() call. |
| 281 |
* |
| 282 |
* Even in cases where libusb_set_configuration() succeeds, consider that other |
| 283 |
* applications or drivers may change configuration after your application |
| 284 |
* calls libusb_set_configuration(). |
| 285 |
* |
| 286 |
* One possible way to lock your device into a specific configuration is as |
| 287 |
* follows: |
| 288 |
* -# Set the desired configuration (or use the logic above to realise that |
| 289 |
* it is already in the desired configuration) |
| 290 |
* -# Claim the interface that you wish to use |
| 291 |
* -# Check that the currently active configuration is the one that you want |
| 292 |
* to use. |
| 293 |
* |
| 294 |
* The above method works because once an interface is claimed, no application |
| 295 |
* or driver is able to select another configuration. |
| 296 |
* |
| 297 |
* \section earlycomp Early transfer completion |
| 298 |
* |
| 299 |
* NOTE: This section is currently Linux-centric. I am not sure if any of these |
| 300 |
* considerations apply to Darwin or other platforms. |
| 301 |
* |
| 302 |
* When a transfer completes early (i.e. when less data is received/sent in |
| 303 |
* any one packet than the transfer buffer allows for) then libusb is designed |
| 304 |
* to terminate the transfer immediately, not transferring or receiving any |
| 305 |
* more data unless other transfers have been queued by the user. |
| 306 |
* |
| 307 |
* On legacy platforms, libusb is unable to do this in all situations. After |
| 308 |
* the incomplete packet occurs, "surplus" data may be transferred. For recent |
| 309 |
* versions of libusb, this information is kept (the data length of the |
| 310 |
* transfer is updated) and, for device-to-host transfers, any surplus data was |
| 311 |
* added to the buffer. Still, this is not a nice solution because it loses the |
| 312 |
* information about the end of the short packet, and the user probably wanted |
| 313 |
* that surplus data to arrive in the next logical transfer. |
| 314 |
* |
| 315 |
* \section zlp Zero length packets |
| 316 |
* |
| 317 |
* - libusb is able to send a packet of zero length to an endpoint simply by |
| 318 |
* submitting a transfer of zero length. |
| 319 |
* - The \ref libusb_transfer_flags::LIBUSB_TRANSFER_ADD_ZERO_PACKET |
| 320 |
* "LIBUSB_TRANSFER_ADD_ZERO_PACKET" flag is currently supported on Linux, |
| 321 |
* Darwin and Windows (WinUSB). |
| 322 |
*/ |
| 323 |
|
| 324 |
/** |
| 325 |
* \page libusb_contexts Contexts |
| 326 |
* |
| 327 |
* It is possible that libusb may be used simultaneously from two independent |
| 328 |
* libraries linked into the same executable. For example, if your application |
| 329 |
* has a plugin-like system which allows the user to dynamically load a range |
| 330 |
* of modules into your program, it is feasible that two independently |
| 331 |
* developed modules may both use libusb. |
| 332 |
* |
| 333 |
* libusb is written to allow for these multiple user scenarios. The two |
| 334 |
* "instances" of libusb will not interfere: an option set by one user will have |
| 335 |
* no effect the same option for other users, other users can continue using |
| 336 |
* libusb after one of them calls libusb_exit(), etc. |
| 337 |
* |
| 338 |
* This is made possible through libusb's <em>context</em> concept. When you |
| 339 |
* call libusb_init_context(), you are (optionally) given a context. You can then pass |
| 340 |
* this context pointer back into future libusb functions. |
| 341 |
* |
| 342 |
* In order to keep things simple for more simplistic applications, it is |
| 343 |
* legal to pass NULL to all functions requiring a context pointer (as long as |
| 344 |
* you're sure no other code will attempt to use libusb from the same process). |
| 345 |
* When you pass NULL, the default context will be used. The default context |
| 346 |
* is created the first time a process calls libusb_init_context() when no other |
| 347 |
* context is alive. Contexts are destroyed during libusb_exit(). |
| 348 |
* |
| 349 |
* The default context is reference-counted and can be shared. That means that |
| 350 |
* if libusb_init_context(NULL, x, y) is called twice within the same process, the two |
| 351 |
* users end up sharing the same context. The deinitialization and freeing of |
| 352 |
* the default context will only happen when the last user calls libusb_exit(). |
| 353 |
* In other words, the default context is created and initialized when its |
| 354 |
* reference count goes from 0 to 1, and is deinitialized and destroyed when |
| 355 |
* its reference count goes from 1 to 0. |
| 356 |
* |
| 357 |
* You may be wondering why only a subset of libusb functions require a |
| 358 |
* context pointer in their function definition. Internally, libusb stores |
| 359 |
* context pointers in other objects (e.g. libusb_device instances) and hence |
| 360 |
* can infer the context from those objects. |
| 361 |
*/ |
| 362 |
|
| 363 |
/** |
| 364 |
* \page libusb_api Application Programming Interface |
| 365 |
* |
| 366 |
* This is the complete list of libusb functions, structures and |
| 367 |
* enumerations in alphabetical order. |
| 368 |
* |
| 369 |
* \section Functions |
| 370 |
* - libusb_alloc_streams() |
| 371 |
* - libusb_alloc_transfer() |
| 372 |
* - libusb_attach_kernel_driver() |
| 373 |
* - libusb_bulk_transfer() |
| 374 |
* - libusb_cancel_transfer() |
| 375 |
* - libusb_claim_interface() |
| 376 |
* - libusb_clear_halt() |
| 377 |
* - libusb_close() |
| 378 |
* - libusb_control_transfer() |
| 379 |
* - libusb_control_transfer_get_data() |
| 380 |
* - libusb_control_transfer_get_setup() |
| 381 |
* - libusb_cpu_to_le16() |
| 382 |
* - libusb_detach_kernel_driver() |
| 383 |
* - libusb_dev_mem_alloc() |
| 384 |
* - libusb_dev_mem_free() |
| 385 |
* - libusb_endpoint_set_raw_io() |
| 386 |
* - libusb_endpoint_supports_raw_io() |
| 387 |
* - libusb_error_name() |
| 388 |
* - libusb_event_handler_active() |
| 389 |
* - libusb_event_handling_ok() |
| 390 |
* - libusb_exit() |
| 391 |
* - libusb_fill_bulk_stream_transfer() |
| 392 |
* - libusb_fill_bulk_transfer() |
| 393 |
* - libusb_fill_control_setup() |
| 394 |
* - libusb_fill_control_transfer() |
| 395 |
* - libusb_fill_interrupt_transfer() |
| 396 |
* - libusb_fill_iso_transfer() |
| 397 |
* - libusb_free_bos_descriptor() |
| 398 |
* - libusb_free_config_descriptor() |
| 399 |
* - libusb_free_container_id_descriptor() |
| 400 |
* - libusb_free_device_list() |
| 401 |
* - libusb_free_pollfds() |
| 402 |
* - libusb_free_ss_endpoint_companion_descriptor() |
| 403 |
* - libusb_free_ss_usb_device_capability_descriptor() |
| 404 |
* - libusb_free_streams() |
| 405 |
* - libusb_free_transfer() |
| 406 |
* - libusb_free_usb_2_0_extension_descriptor() |
| 407 |
* - libusb_get_active_config_descriptor() |
| 408 |
* - libusb_get_bos_descriptor() |
| 409 |
* - libusb_get_session_data() |
| 410 |
* - libusb_get_bus_number() |
| 411 |
* - libusb_get_config_descriptor() |
| 412 |
* - libusb_get_config_descriptor_by_value() |
| 413 |
* - libusb_get_configuration() |
| 414 |
* - libusb_get_container_id_descriptor() |
| 415 |
* - libusb_get_descriptor() |
| 416 |
* - libusb_get_device() |
| 417 |
* - libusb_get_device_address() |
| 418 |
* - libusb_get_device_descriptor() |
| 419 |
* - libusb_get_device_list() |
| 420 |
* - libusb_get_device_speed() |
| 421 |
* - libusb_get_device_string() |
| 422 |
* - libusb_get_iso_packet_buffer() |
| 423 |
* - libusb_get_iso_packet_buffer_simple() |
| 424 |
* - libusb_get_max_alt_packet_size() |
| 425 |
* - libusb_get_max_iso_packet_size() |
| 426 |
* - libusb_get_max_raw_io_transfer_size() |
| 427 |
* - libusb_get_max_packet_size() |
| 428 |
* - libusb_get_next_timeout() |
| 429 |
* - libusb_get_parent() |
| 430 |
* - libusb_get_pollfds() |
| 431 |
* - libusb_get_port_number() |
| 432 |
* - libusb_get_port_numbers() |
| 433 |
* - libusb_get_port_path() |
| 434 |
* - libusb_get_ss_endpoint_companion_descriptor() |
| 435 |
* - libusb_get_ss_usb_device_capability_descriptor() |
| 436 |
* - libusb_get_string_descriptor() |
| 437 |
* - libusb_get_string_descriptor_ascii() |
| 438 |
* - libusb_get_usb_2_0_extension_descriptor() |
| 439 |
* - libusb_get_version() |
| 440 |
* - libusb_handle_events() |
| 441 |
* - libusb_handle_events_completed() |
| 442 |
* - libusb_handle_events_locked() |
| 443 |
* - libusb_handle_events_timeout() |
| 444 |
* - libusb_handle_events_timeout_completed() |
| 445 |
* - libusb_has_capability() |
| 446 |
* - libusb_hotplug_deregister_callback() |
| 447 |
* - libusb_hotplug_register_callback() |
| 448 |
* - libusb_init() |
| 449 |
* - libusb_init_context() |
| 450 |
* - libusb_interrupt_event_handler() |
| 451 |
* - libusb_interrupt_transfer() |
| 452 |
* - libusb_kernel_driver_active() |
| 453 |
* - libusb_lock_events() |
| 454 |
* - libusb_lock_event_waiters() |
| 455 |
* - libusb_open() |
| 456 |
* - libusb_open_device_with_vid_pid() |
| 457 |
* - libusb_pollfds_handle_timeouts() |
| 458 |
* - libusb_ref_device() |
| 459 |
* - libusb_release_interface() |
| 460 |
* - libusb_reset_device() |
| 461 |
* - libusb_set_auto_detach_kernel_driver() |
| 462 |
* - libusb_set_configuration() |
| 463 |
* - libusb_set_debug() |
| 464 |
* - libusb_set_log_cb() |
| 465 |
* - libusb_set_interface_alt_setting() |
| 466 |
* - libusb_set_iso_packet_lengths() |
| 467 |
* - libusb_set_option() |
| 468 |
* - libusb_setlocale() |
| 469 |
* - libusb_set_pollfd_notifiers() |
| 470 |
* - libusb_strerror() |
| 471 |
* - libusb_submit_transfer() |
| 472 |
* - libusb_transfer_get_stream_id() |
| 473 |
* - libusb_transfer_set_stream_id() |
| 474 |
* - libusb_try_lock_events() |
| 475 |
* - libusb_unlock_events() |
| 476 |
* - libusb_unlock_event_waiters() |
| 477 |
* - libusb_unref_device() |
| 478 |
* - libusb_wait_for_event() |
| 479 |
* - libusb_wrap_sys_device() |
| 480 |
* |
| 481 |
* \section Structures |
| 482 |
* - libusb_bos_descriptor |
| 483 |
* - libusb_bos_dev_capability_descriptor |
| 484 |
* - libusb_config_descriptor |
| 485 |
* - libusb_container_id_descriptor |
| 486 |
* - \ref libusb_context |
| 487 |
* - libusb_control_setup |
| 488 |
* - \ref libusb_device |
| 489 |
* - libusb_device_descriptor |
| 490 |
* - \ref libusb_device_handle |
| 491 |
* - libusb_endpoint_descriptor |
| 492 |
* - libusb_interface |
| 493 |
* - libusb_interface_descriptor |
| 494 |
* - libusb_iso_packet_descriptor |
| 495 |
* - libusb_pollfd |
| 496 |
* - libusb_ss_endpoint_companion_descriptor |
| 497 |
* - libusb_ss_usb_device_capability_descriptor |
| 498 |
* - libusb_transfer |
| 499 |
* - libusb_usb_2_0_extension_descriptor |
| 500 |
* - libusb_version |
| 501 |
* |
| 502 |
* \section Enums |
| 503 |
* - \ref libusb_bos_type |
| 504 |
* - \ref libusb_capability |
| 505 |
* - \ref libusb_class_code |
| 506 |
* - \ref libusb_descriptor_type |
| 507 |
* - \ref libusb_device_string_type |
| 508 |
* - \ref libusb_endpoint_direction |
| 509 |
* - \ref libusb_endpoint_transfer_type |
| 510 |
* - \ref libusb_error |
| 511 |
* - \ref libusb_iso_sync_type |
| 512 |
* - \ref libusb_iso_usage_type |
| 513 |
* - \ref libusb_log_level |
| 514 |
* - \ref libusb_option |
| 515 |
* - \ref libusb_request_recipient |
| 516 |
* - \ref libusb_request_type |
| 517 |
* - \ref libusb_speed |
| 518 |
* - \ref libusb_ss_usb_device_capability_attributes |
| 519 |
* - \ref libusb_standard_request |
| 520 |
* - \ref libusb_supported_speed |
| 521 |
* - \ref libusb_transfer_flags |
| 522 |
* - \ref libusb_transfer_status |
| 523 |
* - \ref libusb_transfer_type |
| 524 |
* - \ref libusb_usb_2_0_extension_attributes |
| 525 |
*/ |
| 526 |
|
| 527 |
/** |
| 528 |
* @defgroup libusb_lib Library initialization/deinitialization |
| 529 |
* This page details how to initialize and deinitialize libusb. Initialization |
| 530 |
* must be performed before using any libusb functionality, and similarly you |
| 531 |
* must not call any libusb functions after deinitialization. |
| 532 |
*/ |
| 533 |
|
| 534 |
/** |
| 535 |
* @defgroup libusb_dev Device handling and enumeration |
| 536 |
* The functionality documented below is designed to help with the following |
| 537 |
* operations: |
| 538 |
* - Enumerating the USB devices currently attached to the system |
| 539 |
* - Choosing a device to operate from your software |
| 540 |
* - Opening and closing the chosen device |
| 541 |
* |
| 542 |
* \section nutshell In a nutshell... |
| 543 |
* |
| 544 |
* The description below really makes things sound more complicated than they |
| 545 |
* actually are. The following sequence of function calls will be suitable |
| 546 |
* for almost all scenarios and does not require you to have such a deep |
| 547 |
* understanding of the resource management issues: |
| 548 |
* \code |
| 549 |
// discover devices |
| 550 |
libusb_device **list; |
| 551 |
libusb_device *found = NULL; |
| 552 |
ssize_t cnt = libusb_get_device_list(NULL, &list); |
| 553 |
ssize_t i = 0; |
| 554 |
int err = 0; |
| 555 |
if (cnt < 0) |
| 556 |
error(); |
| 557 |
|
| 558 |
for (i = 0; i < cnt; i++) { |
| 559 |
libusb_device *device = list[i]; |
| 560 |
if (is_interesting(device)) { |
| 561 |
found = device; |
| 562 |
break; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
if (found) { |
| 567 |
libusb_device_handle *handle; |
| 568 |
|
| 569 |
err = libusb_open(found, &handle); |
| 570 |
if (err) |
| 571 |
error(); |
| 572 |
// etc |
| 573 |
} |
| 574 |
|
| 575 |
libusb_free_device_list(list, 1); |
| 576 |
\endcode |
| 577 |
* |
| 578 |
* The two important points: |
| 579 |
* - You asked libusb_free_device_list() to unreference the devices (2nd |
| 580 |
* parameter) |
| 581 |
* - You opened the device before freeing the list and unreferencing the |
| 582 |
* devices |
| 583 |
* |
| 584 |
* If you ended up with a handle, you can now proceed to perform I/O on the |
| 585 |
* device. |
| 586 |
* |
| 587 |
* \section devshandles Devices and device handles |
| 588 |
* libusb has a concept of a USB device, represented by the |
| 589 |
* \ref libusb_device opaque type. A device represents a USB device that |
| 590 |
* is currently or was previously connected to the system. Using a reference |
| 591 |
* to a device, you can determine certain information about the device (e.g. |
| 592 |
* you can read the descriptor data). |
| 593 |
* |
| 594 |
* The libusb_get_device_list() function can be used to obtain a list of |
| 595 |
* devices currently connected to the system. This is known as device |
| 596 |
* discovery. Devices can also be discovered with the hotplug mechanism, |
| 597 |
* whereby a callback function registered with libusb_hotplug_register_callback() |
| 598 |
* will be called when a device of interest is connected or disconnected. |
| 599 |
* |
| 600 |
* Just because you have a reference to a device does not mean it is |
| 601 |
* necessarily usable. The device may have been unplugged, you may not have |
| 602 |
* permission to operate such device, or another program or driver may be |
| 603 |
* using the device. |
| 604 |
* |
| 605 |
* When you've found a device that you'd like to operate, you must ask |
| 606 |
* libusb to open the device using the libusb_open() function. Assuming |
| 607 |
* success, libusb then returns you a <em>device handle</em> |
| 608 |
* (a \ref libusb_device_handle pointer). All "real" I/O operations then |
| 609 |
* operate on the handle rather than the original device pointer. |
| 610 |
* |
| 611 |
* \section devref Device discovery and reference counting |
| 612 |
* |
| 613 |
* Device discovery (i.e. calling libusb_get_device_list()) returns a |
| 614 |
* freshly-allocated list of devices. The list itself must be freed when |
| 615 |
* you are done with it. libusb also needs to know when it is OK to free |
| 616 |
* the contents of the list - the devices themselves. |
| 617 |
* |
| 618 |
* To handle these issues, libusb provides you with two separate items: |
| 619 |
* - A function to free the list itself |
| 620 |
* - A reference counting system for the devices inside |
| 621 |
* |
| 622 |
* New devices presented by the libusb_get_device_list() function all have a |
| 623 |
* reference count of 1. You can increase and decrease reference count using |
| 624 |
* libusb_ref_device() and libusb_unref_device(). A device is destroyed when |
| 625 |
* its reference count reaches 0. |
| 626 |
* |
| 627 |
* With the above information in mind, the process of opening a device can |
| 628 |
* be viewed as follows: |
| 629 |
* -# Discover devices using libusb_get_device_list() or libusb_hotplug_register_callback(). |
| 630 |
* -# Choose the device that you want to operate, and call libusb_open(). |
| 631 |
* -# Unref all devices in the discovered device list. |
| 632 |
* -# Free the discovered device list. |
| 633 |
* |
| 634 |
* The order is important - you must not unreference the device before |
| 635 |
* attempting to open it, because unreferencing it may destroy the device. |
| 636 |
* |
| 637 |
* For convenience, the libusb_free_device_list() function includes a |
| 638 |
* parameter to optionally unreference all the devices in the list before |
| 639 |
* freeing the list itself. This combines steps 3 and 4 above. |
| 640 |
* |
| 641 |
* As an implementation detail, libusb_open() actually adds a reference to |
| 642 |
* the device in question. This is because the device remains available |
| 643 |
* through the handle via libusb_get_device(). The reference is deleted during |
| 644 |
* libusb_close(). |
| 645 |
*/ |
| 646 |
|
| 647 |
/** @defgroup libusb_misc Miscellaneous */ |
| 648 |
|
| 649 |
/* we traverse usbfs without knowing how many devices we are going to find. |
| 650 |
* so we create this discovered_devs model which is similar to a linked-list |
| 651 |
* which grows when required. it can be freed once discovery has completed, |
| 652 |
* eliminating the need for a list node in the libusb_device structure |
| 653 |
* itself. */ |
| 654 |
#define DISCOVERED_DEVICES_SIZE_STEP 16 |
| 655 |
|
| 656 |
static struct discovered_devs *discovered_devs_alloc(void) |
| 657 |
{ |
| 658 |
struct discovered_devs *ret = |
| 659 |
malloc(sizeof(*ret) + (sizeof(void *) * DISCOVERED_DEVICES_SIZE_STEP)); |
| 660 |
|
| 661 |
if (ret) { |
| 662 |
ret->len = 0; |
| 663 |
ret->capacity = DISCOVERED_DEVICES_SIZE_STEP; |
| 664 |
} |
| 665 |
return ret; |
| 666 |
} |
| 667 |
|
| 668 |
static void discovered_devs_free(struct discovered_devs *discdevs) |
| 669 |
{ |
| 670 |
size_t i; |
| 671 |
|
| 672 |
for (i = 0; i < discdevs->len; i++) |
| 673 |
libusb_unref_device(discdevs->devices[i]); |
| 674 |
|
| 675 |
free(discdevs); |
| 676 |
} |
| 677 |
|
| 678 |
/* append a device to the discovered devices collection. may realloc itself, |
| 679 |
* returning new discdevs. returns NULL on realloc failure. */ |
| 680 |
struct discovered_devs *discovered_devs_append( |
| 681 |
struct discovered_devs *discdevs, struct libusb_device *dev) |
| 682 |
{ |
| 683 |
size_t len = discdevs->len; |
| 684 |
size_t capacity; |
| 685 |
struct discovered_devs *new_discdevs; |
| 686 |
|
| 687 |
/* if there is space, just append the device */ |
| 688 |
if (len < discdevs->capacity) { |
| 689 |
discdevs->devices[len] = libusb_ref_device(dev); |
| 690 |
discdevs->len++; |
| 691 |
return discdevs; |
| 692 |
} |
| 693 |
|
| 694 |
/* exceeded capacity, need to grow */ |
| 695 |
usbi_dbg(DEVICE_CTX(dev), "need to increase capacity"); |
| 696 |
capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP; |
| 697 |
/* can't use usbi_reallocf here because in failure cases it would |
| 698 |
* free the existing discdevs without unreferencing its devices. */ |
| 699 |
new_discdevs = realloc(discdevs, |
| 700 |
sizeof(*discdevs) + (sizeof(void *) * capacity)); |
| 701 |
if (!new_discdevs) { |
| 702 |
discovered_devs_free(discdevs); |
| 703 |
return NULL; |
| 704 |
} |
| 705 |
|
| 706 |
discdevs = new_discdevs; |
| 707 |
discdevs->capacity = capacity; |
| 708 |
discdevs->devices[len] = libusb_ref_device(dev); |
| 709 |
discdevs->len++; |
| 710 |
|
| 711 |
return discdevs; |
| 712 |
} |
| 713 |
|
| 714 |
/* Allocate a new device with a specific session ID. The returned device has |
| 715 |
* a reference count of 1. */ |
| 716 |
struct libusb_device *usbi_alloc_device(struct libusb_context *ctx, |
| 717 |
unsigned long session_id) |
| 718 |
{ |
| 719 |
size_t priv_size = usbi_backend.device_priv_size; |
| 720 |
struct libusb_device *dev = calloc(1, PTR_ALIGN(sizeof(*dev)) + priv_size); |
| 721 |
|
| 722 |
if (!dev) |
| 723 |
return NULL; |
| 724 |
|
| 725 |
usbi_atomic_store(&dev->refcnt, 1); |
| 726 |
|
| 727 |
dev->ctx = ctx; |
| 728 |
dev->session_data = session_id; |
| 729 |
dev->speed = LIBUSB_SPEED_UNKNOWN; |
| 730 |
|
| 731 |
/* Note: the device is NOT added to ctx->usb_devs here. The caller |
| 732 |
* (backend) must call usbi_connect_device() after fully initializing |
| 733 |
* the device's private data. This prevents a concurrent |
| 734 |
* usbi_get_device_by_session_id() from finding a half-initialized |
| 735 |
* device. */ |
| 736 |
|
| 737 |
return dev; |
| 738 |
} |
| 739 |
|
| 740 |
void usbi_attach_device(struct libusb_device *dev) |
| 741 |
{ |
| 742 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 743 |
|
| 744 |
usbi_atomic_store(&dev->attached, 1); |
| 745 |
|
| 746 |
usbi_mutex_lock(&ctx->usb_devs_lock); |
| 747 |
list_add(&dev->list, &ctx->usb_devs); |
| 748 |
usbi_mutex_unlock(&ctx->usb_devs_lock); |
| 749 |
} |
| 750 |
|
| 751 |
void usbi_connect_device(struct libusb_device *dev) |
| 752 |
{ |
| 753 |
usbi_attach_device(dev); |
| 754 |
usbi_hotplug_notification(DEVICE_CTX(dev), dev, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED); |
| 755 |
} |
| 756 |
|
| 757 |
void usbi_detach_device(struct libusb_device *dev) |
| 758 |
{ |
| 759 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 760 |
|
| 761 |
usbi_atomic_store(&dev->attached, 0); |
| 762 |
|
| 763 |
usbi_mutex_lock(&ctx->usb_devs_lock); |
| 764 |
list_del(&dev->list); |
| 765 |
usbi_mutex_unlock(&ctx->usb_devs_lock); |
| 766 |
} |
| 767 |
|
| 768 |
void usbi_disconnect_device(struct libusb_device *dev) |
| 769 |
{ |
| 770 |
usbi_detach_device(dev); |
| 771 |
usbi_hotplug_notification(DEVICE_CTX(dev), dev, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT); |
| 772 |
} |
| 773 |
|
| 774 |
/* Perform some final sanity checks on a newly discovered device. If this |
| 775 |
* function fails (negative return code), the device should not be added |
| 776 |
* to the discovered device list. */ |
| 777 |
int usbi_sanitize_device(struct libusb_device *dev) |
| 778 |
{ |
| 779 |
uint8_t num_configurations; |
| 780 |
|
| 781 |
if (dev->device_descriptor.bLength != LIBUSB_DT_DEVICE_SIZE || |
| 782 |
dev->device_descriptor.bDescriptorType != LIBUSB_DT_DEVICE) { |
| 783 |
usbi_err(DEVICE_CTX(dev), "invalid device descriptor"); |
| 784 |
return LIBUSB_ERROR_IO; |
| 785 |
} |
| 786 |
|
| 787 |
num_configurations = dev->device_descriptor.bNumConfigurations; |
| 788 |
if (num_configurations > USB_MAXCONFIG) { |
| 789 |
usbi_err(DEVICE_CTX(dev), "too many configurations"); |
| 790 |
return LIBUSB_ERROR_IO; |
| 791 |
} else if (0 == num_configurations) { |
| 792 |
usbi_dbg(DEVICE_CTX(dev), "zero configurations, maybe an unauthorized device"); |
| 793 |
} |
| 794 |
|
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
/* Examine libusb's internal list of known devices, looking for one with |
| 799 |
* a specific session ID. Returns the matching device if it was found, and |
| 800 |
* NULL otherwise. */ |
| 801 |
struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx, |
| 802 |
unsigned long session_id) |
| 803 |
{ |
| 804 |
struct libusb_device *dev; |
| 805 |
struct libusb_device *ret = NULL; |
| 806 |
|
| 807 |
usbi_mutex_lock(&ctx->usb_devs_lock); |
| 808 |
for_each_device(ctx, dev) { |
| 809 |
if (dev->session_data == session_id) { |
| 810 |
ret = libusb_ref_device(dev); |
| 811 |
break; |
| 812 |
} |
| 813 |
} |
| 814 |
usbi_mutex_unlock(&ctx->usb_devs_lock); |
| 815 |
|
| 816 |
return ret; |
| 817 |
} |
| 818 |
|
| 819 |
/** @ingroup libusb_dev |
| 820 |
* Returns a list of USB devices currently attached to the system. This is |
| 821 |
* your entry point into finding a USB device to operate. |
| 822 |
* |
| 823 |
* You are expected to unreference all the devices when you are done with |
| 824 |
* them, and then free the list with libusb_free_device_list(). Note that |
| 825 |
* libusb_free_device_list() can unref all the devices for you. Be careful |
| 826 |
* not to unreference a device you are about to open until after you have |
| 827 |
* opened it. |
| 828 |
* |
| 829 |
* This return value of this function indicates the number of devices in |
| 830 |
* the resultant list. The list is actually one element larger, as it is |
| 831 |
* NULL-terminated. |
| 832 |
* |
| 833 |
* \param ctx the context to operate on, or NULL for the default context |
| 834 |
* \param list output location for a list of devices. Must be later freed with |
| 835 |
* libusb_free_device_list(). |
| 836 |
* \returns the number of devices in the outputted list, or any |
| 837 |
* \ref libusb_error according to errors encountered by the backend. |
| 838 |
*/ |
| 839 |
ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx, |
| 840 |
libusb_device ***list) |
| 841 |
{ |
| 842 |
struct discovered_devs *discdevs = discovered_devs_alloc(); |
| 843 |
struct libusb_device **ret; |
| 844 |
int r = 0; |
| 845 |
ssize_t i, len; |
| 846 |
|
| 847 |
usbi_dbg(ctx, " "); |
| 848 |
|
| 849 |
if (!discdevs) |
| 850 |
return LIBUSB_ERROR_NO_MEM; |
| 851 |
|
| 852 |
ctx = usbi_get_context(ctx); |
| 853 |
|
| 854 |
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { |
| 855 |
/* backend provides hotplug support */ |
| 856 |
struct libusb_device *dev; |
| 857 |
|
| 858 |
if (usbi_backend.hotplug_poll) |
| 859 |
usbi_backend.hotplug_poll(); |
| 860 |
|
| 861 |
usbi_mutex_lock(&ctx->usb_devs_lock); |
| 862 |
for_each_device(ctx, dev) { |
| 863 |
discdevs = discovered_devs_append(discdevs, dev); |
| 864 |
|
| 865 |
if (!discdevs) { |
| 866 |
r = LIBUSB_ERROR_NO_MEM; |
| 867 |
break; |
| 868 |
} |
| 869 |
} |
| 870 |
usbi_mutex_unlock(&ctx->usb_devs_lock); |
| 871 |
} else { |
| 872 |
/* backend does not provide hotplug support */ |
| 873 |
r = usbi_backend.get_device_list(ctx, &discdevs); |
| 874 |
} |
| 875 |
|
| 876 |
if (r < 0) { |
| 877 |
len = r; |
| 878 |
goto out; |
| 879 |
} |
| 880 |
|
| 881 |
/* convert discovered_devs into a list */ |
| 882 |
len = (ssize_t)discdevs->len; |
| 883 |
ret = calloc((size_t)len + 1, sizeof(struct libusb_device *)); |
| 884 |
if (!ret) { |
| 885 |
len = LIBUSB_ERROR_NO_MEM; |
| 886 |
goto out; |
| 887 |
} |
| 888 |
|
| 889 |
ret[len] = NULL; |
| 890 |
for (i = 0; i < len; i++) { |
| 891 |
struct libusb_device *dev = discdevs->devices[i]; |
| 892 |
ret[i] = libusb_ref_device(dev); |
| 893 |
} |
| 894 |
*list = ret; |
| 895 |
|
| 896 |
out: |
| 897 |
if (discdevs) |
| 898 |
discovered_devs_free(discdevs); |
| 899 |
return len; |
| 900 |
} |
| 901 |
|
| 902 |
/** \ingroup libusb_dev |
| 903 |
* Frees a list of devices previously discovered using |
| 904 |
* libusb_get_device_list(). If the unref_devices parameter is set, the |
| 905 |
* reference count of each device in the list is decremented by 1. |
| 906 |
* \param list the list to free |
| 907 |
* \param unref_devices whether to unref the devices in the list |
| 908 |
*/ |
| 909 |
void API_EXPORTED libusb_free_device_list(libusb_device **list, |
| 910 |
int unref_devices) |
| 911 |
{ |
| 912 |
if (!list) |
| 913 |
return; |
| 914 |
|
| 915 |
if (unref_devices) { |
| 916 |
int i = 0; |
| 917 |
struct libusb_device *dev; |
| 918 |
|
| 919 |
while ((dev = list[i++]) != NULL) |
| 920 |
libusb_unref_device(dev); |
| 921 |
} |
| 922 |
free(list); |
| 923 |
} |
| 924 |
|
| 925 |
/** \ingroup libusb_dev |
| 926 |
* Returns the backend-specific identifier of the underlying system device tree |
| 927 |
* node. Can be used to find the corresponding system device and directly query |
| 928 |
* it (or access it otherwise) when and if necessary. |
| 929 |
* |
| 930 |
* Relevant backends: |
| 931 |
* - Darwin: IOKit `sessionID` |
| 932 |
* - Windows WinUSB: `DEVINST` |
| 933 |
* - Linux, BSD: `busnum << 8 | devnum` |
| 934 |
* |
| 935 |
* Since version 1.0.30, \ref LIBUSB_API_VERSION >= 0x0100010C |
| 936 |
* |
| 937 |
* \param dev a device (must not be null) |
| 938 |
* \returns the backend-specific device identifier |
| 939 |
*/ |
| 940 |
unsigned long API_EXPORTED libusb_get_session_data(libusb_device *dev) |
| 941 |
{ |
| 942 |
return dev->session_data; |
| 943 |
} |
| 944 |
|
| 945 |
/** \ingroup libusb_dev |
| 946 |
* Get the number of the bus that a device is connected to. |
| 947 |
* \param dev a device |
| 948 |
* \returns the bus number |
| 949 |
*/ |
| 950 |
uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev) |
| 951 |
{ |
| 952 |
return dev->bus_number; |
| 953 |
} |
| 954 |
|
| 955 |
/** \ingroup libusb_dev |
| 956 |
* Get the number of the port that a device is connected to. |
| 957 |
* Unless the OS does something funky, or you are hot-plugging USB extension cards, |
| 958 |
* the port number returned by this call is usually guaranteed to be uniquely tied |
| 959 |
* to a physical port, meaning that different devices plugged on the same physical |
| 960 |
* port should return the same port number. |
| 961 |
* |
| 962 |
* But outside of this, there is no guarantee that the port number returned by this |
| 963 |
* call will remain the same, or even match the order in which ports have been |
| 964 |
* numbered by the HUB/HCD manufacturer. |
| 965 |
* |
| 966 |
* \param dev a device |
| 967 |
* \returns the port number (0 if not available) |
| 968 |
*/ |
| 969 |
uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev) |
| 970 |
{ |
| 971 |
return dev->port_number; |
| 972 |
} |
| 973 |
|
| 974 |
/** \ingroup libusb_dev |
| 975 |
* Get the list of all port numbers from root for the specified device |
| 976 |
* |
| 977 |
* Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102 |
| 978 |
* \param dev a device |
| 979 |
* \param port_numbers the array that should contain the port numbers |
| 980 |
* \param port_numbers_len the maximum length of the array. As per the USB 3.0 |
| 981 |
* specs, the current maximum limit for the depth is 7. |
| 982 |
* \returns the number of elements filled |
| 983 |
* \returns \ref LIBUSB_ERROR_OVERFLOW if the array is too small |
| 984 |
*/ |
| 985 |
int API_EXPORTED libusb_get_port_numbers(libusb_device *dev, |
| 986 |
uint8_t *port_numbers, int port_numbers_len) |
| 987 |
{ |
| 988 |
int i = port_numbers_len; |
| 989 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 990 |
|
| 991 |
if (port_numbers_len <= 0) |
| 992 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 993 |
|
| 994 |
/* HCDs can be listed as devices with port #0 */ |
| 995 |
while((dev) && (dev->port_number != 0)) { |
| 996 |
if (--i < 0) { |
| 997 |
usbi_warn(ctx, "port numbers array is too small"); |
| 998 |
return LIBUSB_ERROR_OVERFLOW; |
| 999 |
} |
| 1000 |
port_numbers[i] = dev->port_number; |
| 1001 |
dev = dev->parent_dev; |
| 1002 |
} |
| 1003 |
if (i < port_numbers_len) |
| 1004 |
memmove(port_numbers, &port_numbers[i], (size_t)(port_numbers_len - i)); |
| 1005 |
return port_numbers_len - i; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/** \ingroup libusb_dev |
| 1009 |
* \deprecated Please use \ref libusb_get_port_numbers() instead. |
| 1010 |
*/ |
| 1011 |
int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev, |
| 1012 |
uint8_t *port_numbers, uint8_t port_numbers_len) |
| 1013 |
{ |
| 1014 |
UNUSED(ctx); |
| 1015 |
|
| 1016 |
return libusb_get_port_numbers(dev, port_numbers, port_numbers_len); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/** \ingroup libusb_dev |
| 1020 |
* Get the the parent from the specified device. |
| 1021 |
* \param dev a device |
| 1022 |
* \returns the device parent or NULL if not available |
| 1023 |
* You should issue a \ref libusb_get_device_list() before calling this |
| 1024 |
* function and make sure that you only access the parent before issuing |
| 1025 |
* \ref libusb_free_device_list(). The reason is that libusb currently does |
| 1026 |
* not maintain a permanent list of device instances, and therefore can |
| 1027 |
* only guarantee that parents are fully instantiated within a |
| 1028 |
* libusb_get_device_list() - libusb_free_device_list() block. |
| 1029 |
*/ |
| 1030 |
DEFAULT_VISIBILITY |
| 1031 |
libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev) |
| 1032 |
{ |
| 1033 |
return dev->parent_dev; |
| 1034 |
} |
| 1035 |
|
| 1036 |
/** \ingroup libusb_dev |
| 1037 |
* Get the address of the device on the bus it is connected to. |
| 1038 |
* \param dev a device |
| 1039 |
* \returns the device address |
| 1040 |
*/ |
| 1041 |
uint8_t API_EXPORTED libusb_get_device_address(libusb_device *dev) |
| 1042 |
{ |
| 1043 |
return dev->device_address; |
| 1044 |
} |
| 1045 |
|
| 1046 |
/** \ingroup libusb_dev |
| 1047 |
* Get the negotiated connection speed for a device. |
| 1048 |
* \param dev a device |
| 1049 |
* \returns a \ref libusb_speed code, where LIBUSB_SPEED_UNKNOWN means that |
| 1050 |
* the OS doesn't know or doesn't support returning the negotiated speed. |
| 1051 |
*/ |
| 1052 |
int API_EXPORTED libusb_get_device_speed(libusb_device *dev) |
| 1053 |
{ |
| 1054 |
return (int)(dev->speed); |
| 1055 |
} |
| 1056 |
|
| 1057 |
static const struct libusb_endpoint_descriptor *find_endpoint( |
| 1058 |
struct libusb_config_descriptor *config, unsigned char endpoint) |
| 1059 |
{ |
| 1060 |
int iface_idx; |
| 1061 |
for (iface_idx = 0; iface_idx < config->bNumInterfaces; iface_idx++) { |
| 1062 |
const struct libusb_interface *iface = &config->interface[iface_idx]; |
| 1063 |
int altsetting_idx; |
| 1064 |
|
| 1065 |
for (altsetting_idx = 0; altsetting_idx < iface->num_altsetting; |
| 1066 |
altsetting_idx++) { |
| 1067 |
const struct libusb_interface_descriptor *altsetting |
| 1068 |
= &iface->altsetting[altsetting_idx]; |
| 1069 |
int ep_idx; |
| 1070 |
|
| 1071 |
for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) { |
| 1072 |
const struct libusb_endpoint_descriptor *ep = |
| 1073 |
&altsetting->endpoint[ep_idx]; |
| 1074 |
if (ep->bEndpointAddress == endpoint) |
| 1075 |
return ep; |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
return NULL; |
| 1080 |
} |
| 1081 |
|
| 1082 |
/** \ingroup libusb_dev |
| 1083 |
* Convenience function to retrieve the wMaxPacketSize value for a particular |
| 1084 |
* endpoint in the active device configuration. |
| 1085 |
* |
| 1086 |
* This function was originally intended to be of assistance when setting up |
| 1087 |
* isochronous transfers, but a design mistake resulted in this function |
| 1088 |
* instead. It simply returns the wMaxPacketSize value without considering |
| 1089 |
* its contents. If you're dealing with isochronous transfers, you probably |
| 1090 |
* want libusb_get_max_iso_packet_size() instead. |
| 1091 |
* |
| 1092 |
* \param dev a device |
| 1093 |
* \param endpoint address of the endpoint in question |
| 1094 |
* \returns the wMaxPacketSize value |
| 1095 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist |
| 1096 |
* \returns \ref LIBUSB_ERROR_OTHER on other failure |
| 1097 |
*/ |
| 1098 |
int API_EXPORTED libusb_get_max_packet_size(libusb_device *dev, |
| 1099 |
unsigned char endpoint) |
| 1100 |
{ |
| 1101 |
struct libusb_config_descriptor *config; |
| 1102 |
const struct libusb_endpoint_descriptor *ep; |
| 1103 |
int r; |
| 1104 |
|
| 1105 |
r = libusb_get_active_config_descriptor(dev, &config); |
| 1106 |
if (r < 0) { |
| 1107 |
usbi_err(DEVICE_CTX(dev), |
| 1108 |
"could not retrieve active config descriptor"); |
| 1109 |
return LIBUSB_ERROR_OTHER; |
| 1110 |
} |
| 1111 |
|
| 1112 |
ep = find_endpoint(config, endpoint); |
| 1113 |
if (!ep) { |
| 1114 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 1115 |
goto out; |
| 1116 |
} |
| 1117 |
|
| 1118 |
r = ep->wMaxPacketSize; |
| 1119 |
|
| 1120 |
out: |
| 1121 |
libusb_free_config_descriptor(config); |
| 1122 |
return r; |
| 1123 |
} |
| 1124 |
|
| 1125 |
static const struct libusb_endpoint_descriptor *find_alt_endpoint( |
| 1126 |
struct libusb_config_descriptor *config, |
| 1127 |
int iface_idx, int altsetting_idx, unsigned char endpoint) |
| 1128 |
{ |
| 1129 |
if (iface_idx >= config->bNumInterfaces) { |
| 1130 |
return NULL; |
| 1131 |
} |
| 1132 |
|
| 1133 |
const struct libusb_interface *iface = &config->interface[iface_idx]; |
| 1134 |
|
| 1135 |
if (altsetting_idx >= iface->num_altsetting) { |
| 1136 |
return NULL; |
| 1137 |
} |
| 1138 |
|
| 1139 |
const struct libusb_interface_descriptor *altsetting |
| 1140 |
= &iface->altsetting[altsetting_idx]; |
| 1141 |
int ep_idx; |
| 1142 |
|
| 1143 |
for (ep_idx = 0; ep_idx < altsetting->bNumEndpoints; ep_idx++) { |
| 1144 |
const struct libusb_endpoint_descriptor *ep = |
| 1145 |
&altsetting->endpoint[ep_idx]; |
| 1146 |
if (ep->bEndpointAddress == endpoint) |
| 1147 |
return ep; |
| 1148 |
} |
| 1149 |
return NULL; |
| 1150 |
} |
| 1151 |
|
| 1152 |
static int get_endpoint_max_packet_size(libusb_device *dev, |
| 1153 |
const struct libusb_endpoint_descriptor *ep) |
| 1154 |
{ |
| 1155 |
struct libusb_ss_endpoint_companion_descriptor *ss_ep_cmp; |
| 1156 |
enum libusb_endpoint_transfer_type ep_type; |
| 1157 |
uint16_t val; |
| 1158 |
int r = 0; |
| 1159 |
int speed; |
| 1160 |
|
| 1161 |
speed = libusb_get_device_speed(dev); |
| 1162 |
if (speed >= LIBUSB_SPEED_SUPER) { |
| 1163 |
r = libusb_get_ss_endpoint_companion_descriptor(dev->ctx, ep, &ss_ep_cmp); |
| 1164 |
if (r == LIBUSB_SUCCESS) { |
| 1165 |
r = ss_ep_cmp->wBytesPerInterval; |
| 1166 |
libusb_free_ss_endpoint_companion_descriptor(ss_ep_cmp); |
| 1167 |
} |
| 1168 |
} |
| 1169 |
|
| 1170 |
/* If the device isn't a SuperSpeed device or retrieving the SS endpoint didn't worked. */ |
| 1171 |
if (speed < LIBUSB_SPEED_SUPER || r < 0) { |
| 1172 |
val = ep->wMaxPacketSize; |
| 1173 |
ep_type = (enum libusb_endpoint_transfer_type) (ep->bmAttributes & 0x3); |
| 1174 |
|
| 1175 |
r = val & 0x07ff; |
| 1176 |
if (ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_ISOCHRONOUS |
| 1177 |
|| ep_type == LIBUSB_ENDPOINT_TRANSFER_TYPE_INTERRUPT) |
| 1178 |
r *= (1 + ((val >> 11) & 3)); |
| 1179 |
} |
| 1180 |
|
| 1181 |
return r; |
| 1182 |
} |
| 1183 |
|
| 1184 |
/** \ingroup libusb_dev |
| 1185 |
* Calculate the maximum packet size which a specific endpoint is capable is |
| 1186 |
* sending or receiving in the duration of 1 microframe |
| 1187 |
* |
| 1188 |
* Only the active configuration is examined. The calculation is based on the |
| 1189 |
* wMaxPacketSize field in the endpoint descriptor as described in section |
| 1190 |
* 9.6.6 in the USB 2.0 specifications. |
| 1191 |
* |
| 1192 |
* If acting on an isochronous or interrupt endpoint, this function will |
| 1193 |
* multiply the value found in bits 0:10 by the number of transactions per |
| 1194 |
* microframe (determined by bits 11:12). Otherwise, this function just |
| 1195 |
* returns the numeric value found in bits 0:10. For USB 3.0 device, it |
| 1196 |
* will attempts to retrieve the Endpoint Companion Descriptor to return |
| 1197 |
* wBytesPerInterval. |
| 1198 |
* |
| 1199 |
* This function is useful for setting up isochronous transfers, for example |
| 1200 |
* you might pass the return value from this function to |
| 1201 |
* libusb_set_iso_packet_lengths() in order to set the length field of every |
| 1202 |
* isochronous packet in a transfer. |
| 1203 |
* |
| 1204 |
* This function only considers the first alternate setting of the interface. |
| 1205 |
* If the endpoint has different maximum packet sizes for different alternate |
| 1206 |
* settings, you probably want libusb_get_max_alt_packet_size() instead. |
| 1207 |
* |
| 1208 |
* Since v1.0.3. |
| 1209 |
* |
| 1210 |
* \param dev a device |
| 1211 |
* \param endpoint address of the endpoint in question |
| 1212 |
* \returns the maximum packet size which can be sent/received on this endpoint |
| 1213 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist |
| 1214 |
* \returns \ref LIBUSB_ERROR_OTHER on other failure |
| 1215 |
* \see libusb_get_max_alt_packet_size |
| 1216 |
*/ |
| 1217 |
int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev, |
| 1218 |
unsigned char endpoint) |
| 1219 |
{ |
| 1220 |
struct libusb_config_descriptor *config; |
| 1221 |
const struct libusb_endpoint_descriptor *ep; |
| 1222 |
int r; |
| 1223 |
|
| 1224 |
r = libusb_get_active_config_descriptor(dev, &config); |
| 1225 |
if (r < 0) { |
| 1226 |
usbi_err(DEVICE_CTX(dev), |
| 1227 |
"could not retrieve active config descriptor"); |
| 1228 |
return LIBUSB_ERROR_OTHER; |
| 1229 |
} |
| 1230 |
|
| 1231 |
ep = find_endpoint(config, endpoint); |
| 1232 |
if (!ep) { |
| 1233 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 1234 |
goto out; |
| 1235 |
} |
| 1236 |
|
| 1237 |
r = get_endpoint_max_packet_size(dev, ep); |
| 1238 |
|
| 1239 |
out: |
| 1240 |
libusb_free_config_descriptor(config); |
| 1241 |
return r; |
| 1242 |
} |
| 1243 |
|
| 1244 |
/** \ingroup libusb_dev |
| 1245 |
* Calculate the maximum packet size which a specific endpoint is capable of |
| 1246 |
* sending or receiving in the duration of 1 microframe |
| 1247 |
* |
| 1248 |
* Only the active configuration is examined. The calculation is based on the |
| 1249 |
* wMaxPacketSize field in the endpoint descriptor as described in section |
| 1250 |
* 9.6.6 in the USB 2.0 specifications. |
| 1251 |
* |
| 1252 |
* If acting on an isochronous or interrupt endpoint, this function will |
| 1253 |
* multiply the value found in bits 0:10 by the number of transactions per |
| 1254 |
* microframe (determined by bits 11:12). Otherwise, this function just |
| 1255 |
* returns the numeric value found in bits 0:10. For USB 3.0 device, it |
| 1256 |
* will attempts to retrieve the Endpoint Companion Descriptor to return |
| 1257 |
* wBytesPerInterval. |
| 1258 |
* |
| 1259 |
* This function is useful for setting up isochronous transfers, for example |
| 1260 |
* you might pass the return value from this function to |
| 1261 |
* libusb_set_iso_packet_lengths() in order to set the length field of every |
| 1262 |
* isochronous packet in a transfer. |
| 1263 |
* |
| 1264 |
* Since version 1.0.27, \ref LIBUSB_API_VERSION >= 0x0100010A |
| 1265 |
* |
| 1266 |
* \param dev a device |
| 1267 |
* \param interface_number the <tt>bInterfaceNumber</tt> of the interface |
| 1268 |
* the endpoint belongs to |
| 1269 |
* \param alternate_setting the <tt>bAlternateSetting</tt> of the interface |
| 1270 |
* \param endpoint address of the endpoint in question |
| 1271 |
* \returns the maximum packet size which can be sent/received on this endpoint |
| 1272 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist |
| 1273 |
* \returns \ref LIBUSB_ERROR_OTHER on other failure |
| 1274 |
* \see libusb_get_max_iso_packet_size |
| 1275 |
*/ |
| 1276 |
int API_EXPORTED libusb_get_max_alt_packet_size(libusb_device *dev, |
| 1277 |
int interface_number, int alternate_setting, unsigned char endpoint) |
| 1278 |
{ |
| 1279 |
struct libusb_config_descriptor *config; |
| 1280 |
const struct libusb_endpoint_descriptor *ep; |
| 1281 |
int r; |
| 1282 |
|
| 1283 |
r = libusb_get_active_config_descriptor(dev, &config); |
| 1284 |
if (r < 0) { |
| 1285 |
usbi_err(DEVICE_CTX(dev), |
| 1286 |
"could not retrieve active config descriptor"); |
| 1287 |
return LIBUSB_ERROR_OTHER; |
| 1288 |
} |
| 1289 |
|
| 1290 |
ep = find_alt_endpoint(config, interface_number, |
| 1291 |
alternate_setting, endpoint); |
| 1292 |
if (!ep) { |
| 1293 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 1294 |
goto out; |
| 1295 |
} |
| 1296 |
|
| 1297 |
r = get_endpoint_max_packet_size(dev, ep); |
| 1298 |
|
| 1299 |
out: |
| 1300 |
libusb_free_config_descriptor(config); |
| 1301 |
return r; |
| 1302 |
} |
| 1303 |
|
| 1304 |
/** \ingroup libusb_dev |
| 1305 |
* Increment the reference count of a device. |
| 1306 |
* \param dev the device to reference |
| 1307 |
* \returns the same device |
| 1308 |
*/ |
| 1309 |
DEFAULT_VISIBILITY |
| 1310 |
libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev) |
| 1311 |
{ |
| 1312 |
long refcnt; |
| 1313 |
|
| 1314 |
refcnt = usbi_atomic_inc(&dev->refcnt); |
| 1315 |
assert(refcnt >= 2); |
| 1316 |
UNUSED(refcnt); |
| 1317 |
|
| 1318 |
return dev; |
| 1319 |
} |
| 1320 |
|
| 1321 |
/** \ingroup libusb_dev |
| 1322 |
* Decrement the reference count of a device. If the decrement operation |
| 1323 |
* causes the reference count to reach zero, the device shall be destroyed. |
| 1324 |
* \param dev the device to unreference |
| 1325 |
*/ |
| 1326 |
void API_EXPORTED libusb_unref_device(libusb_device *dev) |
| 1327 |
{ |
| 1328 |
long refcnt; |
| 1329 |
|
| 1330 |
if (!dev) |
| 1331 |
return; |
| 1332 |
|
| 1333 |
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { |
| 1334 |
/* Non-hotplug path: the decrement and conditional list removal |
| 1335 |
* must be atomic with respect to usbi_get_device_by_session_id(), |
| 1336 |
* which searches the list and refs the device under the same lock. |
| 1337 |
* Without this, a concurrent search could find a device whose |
| 1338 |
* refcnt has already reached zero and hit the assert in |
| 1339 |
* libusb_ref_device(). */ |
| 1340 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 1341 |
|
| 1342 |
usbi_mutex_lock(&ctx->usb_devs_lock); |
| 1343 |
refcnt = usbi_atomic_dec(&dev->refcnt); |
| 1344 |
assert(refcnt >= 0); |
| 1345 |
if (refcnt == 0 && usbi_atomic_load(&dev->attached)) { |
| 1346 |
list_del(&dev->list); |
| 1347 |
usbi_atomic_store(&dev->attached, 0); |
| 1348 |
} |
| 1349 |
usbi_mutex_unlock(&ctx->usb_devs_lock); |
| 1350 |
} else { |
| 1351 |
refcnt = usbi_atomic_dec(&dev->refcnt); |
| 1352 |
assert(refcnt >= 0); |
| 1353 |
} |
| 1354 |
|
| 1355 |
if (refcnt == 0) { |
| 1356 |
usbi_dbg(DEVICE_CTX(dev), "destroy device %d.%d", dev->bus_number, dev->device_address); |
| 1357 |
|
| 1358 |
libusb_unref_device(dev->parent_dev); |
| 1359 |
|
| 1360 |
if (usbi_backend.destroy_device) |
| 1361 |
usbi_backend.destroy_device(dev); |
| 1362 |
|
| 1363 |
for (int idx = 0; idx < LIBUSB_DEVICE_STRING_COUNT; ++idx) { |
| 1364 |
free(dev->device_strings_utf8[idx]); |
| 1365 |
} |
| 1366 |
|
| 1367 |
free(dev); |
| 1368 |
} |
| 1369 |
} |
| 1370 |
|
| 1371 |
/** \ingroup libusb_dev |
| 1372 |
* Wrap a platform-specific system device handle and obtain a libusb device |
| 1373 |
* handle for the underlying device. The handle allows you to use libusb to |
| 1374 |
* perform I/O on the device in question. |
| 1375 |
* |
| 1376 |
* Call libusb_init_context with the LIBUSB_OPTION_NO_DEVICE_DISCOVERY |
| 1377 |
* option if you want to skip enumeration of USB devices. In particular, this |
| 1378 |
* might be needed on Android if you don't have authority to access USB |
| 1379 |
* devices in general. Setting this option with libusb_set_option is deprecated. |
| 1380 |
* |
| 1381 |
* On Linux, the system device handle must be a valid file descriptor opened |
| 1382 |
* on the device node. |
| 1383 |
* |
| 1384 |
* The system device handle must remain open until libusb_close() is called. |
| 1385 |
* The system device handle will not be closed by libusb_close(). |
| 1386 |
* |
| 1387 |
* Internally, this function creates a temporary device and makes it |
| 1388 |
* available to you through libusb_get_device(). This device is destroyed |
| 1389 |
* during libusb_close(). The device shall not be opened through libusb_open(). |
| 1390 |
* |
| 1391 |
* This is a non-blocking function; no requests are sent over the bus. |
| 1392 |
* |
| 1393 |
* Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107 |
| 1394 |
* |
| 1395 |
* \param ctx the context to operate on, or NULL for the default context |
| 1396 |
* \param sys_dev the platform-specific system device handle |
| 1397 |
* \param dev_handle output location for the returned device handle pointer. Only |
| 1398 |
* populated when the return code is 0. |
| 1399 |
* \returns 0 on success |
| 1400 |
* \returns \ref LIBUSB_ERROR_NO_MEM on memory allocation failure |
| 1401 |
* \returns \ref LIBUSB_ERROR_ACCESS if the user has insufficient permissions |
| 1402 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the operation is not supported on this |
| 1403 |
* platform |
| 1404 |
* \returns another LIBUSB_ERROR code on other failure |
| 1405 |
*/ |
| 1406 |
int API_EXPORTED libusb_wrap_sys_device(libusb_context *ctx, intptr_t sys_dev, |
| 1407 |
libusb_device_handle **dev_handle) |
| 1408 |
{ |
| 1409 |
struct libusb_device_handle *_dev_handle; |
| 1410 |
size_t priv_size = usbi_backend.device_handle_priv_size; |
| 1411 |
int r; |
| 1412 |
|
| 1413 |
usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR, (uintptr_t)sys_dev); |
| 1414 |
|
| 1415 |
ctx = usbi_get_context(ctx); |
| 1416 |
|
| 1417 |
if (!usbi_backend.wrap_sys_device) |
| 1418 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 1419 |
|
| 1420 |
_dev_handle = calloc(1, PTR_ALIGN(sizeof(*_dev_handle)) + priv_size); |
| 1421 |
if (!_dev_handle) |
| 1422 |
return LIBUSB_ERROR_NO_MEM; |
| 1423 |
|
| 1424 |
usbi_mutex_init(&_dev_handle->lock); |
| 1425 |
|
| 1426 |
r = usbi_backend.wrap_sys_device(ctx, _dev_handle, sys_dev); |
| 1427 |
if (r < 0) { |
| 1428 |
usbi_dbg(ctx, "wrap_sys_device 0x%" PRIxPTR " returns %d", (uintptr_t)sys_dev, r); |
| 1429 |
usbi_mutex_destroy(&_dev_handle->lock); |
| 1430 |
free(_dev_handle); |
| 1431 |
return r; |
| 1432 |
} |
| 1433 |
|
| 1434 |
usbi_mutex_lock(&ctx->open_devs_lock); |
| 1435 |
list_add(&_dev_handle->list, &ctx->open_devs); |
| 1436 |
usbi_mutex_unlock(&ctx->open_devs_lock); |
| 1437 |
*dev_handle = _dev_handle; |
| 1438 |
|
| 1439 |
return 0; |
| 1440 |
} |
| 1441 |
|
| 1442 |
/** \ingroup libusb_dev |
| 1443 |
* Open a device and obtain a device handle. A handle allows you to perform |
| 1444 |
* I/O on the device in question. |
| 1445 |
* |
| 1446 |
* Internally, this function adds a reference to the device and makes it |
| 1447 |
* available to you through libusb_get_device(). This reference is removed |
| 1448 |
* during libusb_close(). |
| 1449 |
* |
| 1450 |
* This is a non-blocking function; no requests are sent over the bus. |
| 1451 |
* |
| 1452 |
* \param dev the device to open |
| 1453 |
* \param dev_handle output location for the returned device handle pointer. Only |
| 1454 |
* populated when the return code is 0. |
| 1455 |
* \returns 0 on success |
| 1456 |
* \returns \ref LIBUSB_ERROR_NO_MEM on memory allocation failure |
| 1457 |
* \returns \ref LIBUSB_ERROR_ACCESS if the user has insufficient permissions |
| 1458 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1459 |
* \returns another LIBUSB_ERROR code on other failure |
| 1460 |
*/ |
| 1461 |
int API_EXPORTED libusb_open(libusb_device *dev, |
| 1462 |
libusb_device_handle **dev_handle) |
| 1463 |
{ |
| 1464 |
struct libusb_context *ctx = DEVICE_CTX(dev); |
| 1465 |
struct libusb_device_handle *_dev_handle; |
| 1466 |
size_t priv_size = usbi_backend.device_handle_priv_size; |
| 1467 |
int r; |
| 1468 |
|
| 1469 |
usbi_dbg(DEVICE_CTX(dev), "open %d.%d", dev->bus_number, dev->device_address); |
| 1470 |
|
| 1471 |
if (!usbi_atomic_load(&dev->attached)) |
| 1472 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1473 |
|
| 1474 |
_dev_handle = calloc(1, PTR_ALIGN(sizeof(*_dev_handle)) + priv_size); |
| 1475 |
if (!_dev_handle) |
| 1476 |
return LIBUSB_ERROR_NO_MEM; |
| 1477 |
|
| 1478 |
usbi_mutex_init(&_dev_handle->lock); |
| 1479 |
|
| 1480 |
_dev_handle->dev = libusb_ref_device(dev); |
| 1481 |
|
| 1482 |
r = usbi_backend.open(_dev_handle); |
| 1483 |
if (r < 0) { |
| 1484 |
usbi_dbg(DEVICE_CTX(dev), "open %d.%d returns %d", dev->bus_number, dev->device_address, r); |
| 1485 |
libusb_unref_device(dev); |
| 1486 |
usbi_mutex_destroy(&_dev_handle->lock); |
| 1487 |
free(_dev_handle); |
| 1488 |
return r; |
| 1489 |
} |
| 1490 |
|
| 1491 |
usbi_mutex_lock(&ctx->open_devs_lock); |
| 1492 |
list_add(&_dev_handle->list, &ctx->open_devs); |
| 1493 |
usbi_mutex_unlock(&ctx->open_devs_lock); |
| 1494 |
*dev_handle = _dev_handle; |
| 1495 |
|
| 1496 |
return 0; |
| 1497 |
} |
| 1498 |
|
| 1499 |
/** \ingroup libusb_dev |
| 1500 |
* Convenience function for finding a device with a particular |
| 1501 |
* <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended |
| 1502 |
* for those scenarios where you are using libusb to knock up a quick test |
| 1503 |
* application - it allows you to avoid calling libusb_get_device_list() and |
| 1504 |
* worrying about traversing/freeing the list. |
| 1505 |
* |
| 1506 |
* This function has limitations and is hence not intended for use in real |
| 1507 |
* applications: if multiple devices have the same IDs it will only |
| 1508 |
* give you the first one, etc. |
| 1509 |
* |
| 1510 |
* \param ctx the context to operate on, or NULL for the default context |
| 1511 |
* \param vendor_id the idVendor value to search for |
| 1512 |
* \param product_id the idProduct value to search for |
| 1513 |
* \returns a device handle for the first found device, or NULL on error |
| 1514 |
* or if the device could not be found. */ |
| 1515 |
DEFAULT_VISIBILITY |
| 1516 |
libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( |
| 1517 |
libusb_context *ctx, uint16_t vendor_id, uint16_t product_id) |
| 1518 |
{ |
| 1519 |
struct libusb_device **devs; |
| 1520 |
struct libusb_device *found = NULL; |
| 1521 |
struct libusb_device *dev; |
| 1522 |
struct libusb_device_handle *dev_handle = NULL; |
| 1523 |
size_t i = 0; |
| 1524 |
int r; |
| 1525 |
|
| 1526 |
if (libusb_get_device_list(ctx, &devs) < 0) |
| 1527 |
return NULL; |
| 1528 |
|
| 1529 |
while ((dev = devs[i++]) != NULL) { |
| 1530 |
struct libusb_device_descriptor desc; |
| 1531 |
r = libusb_get_device_descriptor(dev, &desc); |
| 1532 |
if (r < 0) |
| 1533 |
goto out; |
| 1534 |
if (desc.idVendor == vendor_id && desc.idProduct == product_id) { |
| 1535 |
found = dev; |
| 1536 |
break; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
|
| 1540 |
if (found) { |
| 1541 |
r = libusb_open(found, &dev_handle); |
| 1542 |
if (r < 0) |
| 1543 |
dev_handle = NULL; |
| 1544 |
} |
| 1545 |
|
| 1546 |
out: |
| 1547 |
libusb_free_device_list(devs, 1); |
| 1548 |
return dev_handle; |
| 1549 |
} |
| 1550 |
|
| 1551 |
static void do_close(struct libusb_context *ctx, |
| 1552 |
struct libusb_device_handle *dev_handle) |
| 1553 |
{ |
| 1554 |
struct usbi_transfer *itransfer; |
| 1555 |
struct usbi_transfer *tmp; |
| 1556 |
|
| 1557 |
/* remove any transfers in flight that are for this device */ |
| 1558 |
usbi_mutex_lock(&ctx->flying_transfers_lock); |
| 1559 |
|
| 1560 |
/* safe iteration because transfers may be being deleted */ |
| 1561 |
for_each_transfer_safe(ctx, itransfer, tmp) { |
| 1562 |
struct libusb_transfer *transfer = |
| 1563 |
USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 1564 |
uint32_t state_flags; |
| 1565 |
|
| 1566 |
if (transfer->dev_handle != dev_handle) |
| 1567 |
continue; |
| 1568 |
|
| 1569 |
usbi_mutex_lock(&itransfer->lock); |
| 1570 |
state_flags = itransfer->state_flags; |
| 1571 |
usbi_mutex_unlock(&itransfer->lock); |
| 1572 |
if (!(state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) { |
| 1573 |
usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know"); |
| 1574 |
|
| 1575 |
if (state_flags & USBI_TRANSFER_CANCELLING) |
| 1576 |
usbi_warn(ctx, "A cancellation for an in-flight transfer hasn't completed but closing the device handle"); |
| 1577 |
else |
| 1578 |
usbi_err(ctx, "A cancellation hasn't even been scheduled on the transfer for which the device is closing"); |
| 1579 |
} |
| 1580 |
|
| 1581 |
/* remove from the list of in-flight transfers and make sure |
| 1582 |
* we don't accidentally use the device handle in the future |
| 1583 |
* (or that such accesses will be easily caught and identified as a crash) |
| 1584 |
*/ |
| 1585 |
list_del(&itransfer->list); |
| 1586 |
transfer->dev_handle = NULL; |
| 1587 |
|
| 1588 |
/* it is up to the user to free up the actual transfer struct. this is |
| 1589 |
* just making sure that we don't attempt to process the transfer after |
| 1590 |
* the device handle is invalid |
| 1591 |
*/ |
| 1592 |
usbi_dbg(ctx, "Removed transfer %p from the in-flight list because device handle %p closed", |
| 1593 |
(void *) transfer, (void *) dev_handle); |
| 1594 |
} |
| 1595 |
usbi_mutex_unlock(&ctx->flying_transfers_lock); |
| 1596 |
|
| 1597 |
usbi_mutex_lock(&ctx->open_devs_lock); |
| 1598 |
list_del(&dev_handle->list); |
| 1599 |
usbi_mutex_unlock(&ctx->open_devs_lock); |
| 1600 |
|
| 1601 |
usbi_backend.close(dev_handle); |
| 1602 |
libusb_unref_device(dev_handle->dev); |
| 1603 |
usbi_mutex_destroy(&dev_handle->lock); |
| 1604 |
free(dev_handle); |
| 1605 |
} |
| 1606 |
|
| 1607 |
/** \ingroup libusb_dev |
| 1608 |
* Close a device handle. Should be called on all open handles before your |
| 1609 |
* application exits. |
| 1610 |
* |
| 1611 |
* Internally, this function destroys the reference that was added by |
| 1612 |
* libusb_open() on the given device. |
| 1613 |
* |
| 1614 |
* This is a non-blocking function; no requests are sent over the bus. |
| 1615 |
* |
| 1616 |
* \param dev_handle the device handle to close |
| 1617 |
*/ |
| 1618 |
void API_EXPORTED libusb_close(libusb_device_handle *dev_handle) |
| 1619 |
{ |
| 1620 |
struct libusb_context *ctx; |
| 1621 |
unsigned int event_flags; |
| 1622 |
int handling_events; |
| 1623 |
|
| 1624 |
if (!dev_handle) |
| 1625 |
return; |
| 1626 |
ctx = HANDLE_CTX(dev_handle); |
| 1627 |
usbi_dbg(ctx, " "); |
| 1628 |
|
| 1629 |
handling_events = usbi_handling_events(ctx); |
| 1630 |
|
| 1631 |
/* Similarly to libusb_open(), we want to interrupt all event handlers |
| 1632 |
* at this point. More importantly, we want to perform the actual close of |
| 1633 |
* the device while holding the event handling lock (preventing any other |
| 1634 |
* thread from doing event handling) because we will be removing a file |
| 1635 |
* descriptor from the polling loop. If this is being called by the current |
| 1636 |
* event handler, we can bypass the interruption code because we already |
| 1637 |
* hold the event handling lock. */ |
| 1638 |
|
| 1639 |
if (!handling_events) { |
| 1640 |
/* Record that we are closing a device. |
| 1641 |
* Only signal an event if there are no prior pending events. */ |
| 1642 |
usbi_mutex_lock(&ctx->event_data_lock); |
| 1643 |
event_flags = ctx->event_flags; |
| 1644 |
if (!ctx->device_close++) |
| 1645 |
ctx->event_flags |= USBI_EVENT_DEVICE_CLOSE; |
| 1646 |
if (!event_flags) |
| 1647 |
usbi_signal_event(&ctx->event); |
| 1648 |
usbi_mutex_unlock(&ctx->event_data_lock); |
| 1649 |
|
| 1650 |
/* take event handling lock */ |
| 1651 |
libusb_lock_events(ctx); |
| 1652 |
} |
| 1653 |
|
| 1654 |
/* Close the device */ |
| 1655 |
do_close(ctx, dev_handle); |
| 1656 |
|
| 1657 |
if (!handling_events) { |
| 1658 |
/* We're done with closing this device. |
| 1659 |
* Clear the event pipe if there are no further pending events. */ |
| 1660 |
usbi_mutex_lock(&ctx->event_data_lock); |
| 1661 |
if (!--ctx->device_close) |
| 1662 |
ctx->event_flags &= ~USBI_EVENT_DEVICE_CLOSE; |
| 1663 |
if (!ctx->event_flags) |
| 1664 |
usbi_clear_event(&ctx->event); |
| 1665 |
usbi_mutex_unlock(&ctx->event_data_lock); |
| 1666 |
|
| 1667 |
/* Release event handling lock and wake up event waiters */ |
| 1668 |
libusb_unlock_events(ctx); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
/** \ingroup libusb_dev |
| 1673 |
* Get the underlying device for a device handle. This function does not modify |
| 1674 |
* the reference count of the returned device, so do not feel compelled to |
| 1675 |
* unreference it when you are done. |
| 1676 |
* \param dev_handle a device handle |
| 1677 |
* \returns the underlying device |
| 1678 |
*/ |
| 1679 |
DEFAULT_VISIBILITY |
| 1680 |
libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle) |
| 1681 |
{ |
| 1682 |
return dev_handle->dev; |
| 1683 |
} |
| 1684 |
|
| 1685 |
/** \ingroup libusb_dev |
| 1686 |
* Determine the bConfigurationValue of the currently active configuration. |
| 1687 |
* |
| 1688 |
* You could formulate your own control request to obtain this information, |
| 1689 |
* but this function has the advantage that it may be able to retrieve the |
| 1690 |
* information from operating system caches (no I/O involved). |
| 1691 |
* |
| 1692 |
* If the OS does not cache this information, then this function will block |
| 1693 |
* while a control transfer is submitted to retrieve the information. |
| 1694 |
* |
| 1695 |
* This function will return a value of 0 in the <tt>config</tt> output |
| 1696 |
* parameter if the device is in unconfigured state. |
| 1697 |
* |
| 1698 |
* \param dev_handle a device handle |
| 1699 |
* \param config output location for the bConfigurationValue of the active |
| 1700 |
* configuration (only valid for return code 0) |
| 1701 |
* \returns 0 on success |
| 1702 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1703 |
* \returns another LIBUSB_ERROR code on other failure |
| 1704 |
*/ |
| 1705 |
int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev_handle, |
| 1706 |
int *config) |
| 1707 |
{ |
| 1708 |
int r = LIBUSB_ERROR_NOT_SUPPORTED; |
| 1709 |
uint8_t tmp = 0; |
| 1710 |
struct libusb_context *ctx = HANDLE_CTX(dev_handle); |
| 1711 |
|
| 1712 |
usbi_dbg(ctx, " "); |
| 1713 |
if (usbi_backend.get_configuration) |
| 1714 |
r = usbi_backend.get_configuration(dev_handle, &tmp); |
| 1715 |
|
| 1716 |
if (r == LIBUSB_ERROR_NOT_SUPPORTED) { |
| 1717 |
usbi_dbg(ctx, "falling back to control message"); |
| 1718 |
r = libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN, |
| 1719 |
LIBUSB_REQUEST_GET_CONFIGURATION, 0, 0, &tmp, 1, 1000); |
| 1720 |
if (r == 1) { |
| 1721 |
r = 0; |
| 1722 |
} else if (r == 0) { |
| 1723 |
usbi_err(ctx, "zero bytes returned in ctrl transfer?"); |
| 1724 |
r = LIBUSB_ERROR_IO; |
| 1725 |
} else { |
| 1726 |
usbi_dbg(ctx, "control failed, error %d", r); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
|
| 1730 |
if (r == 0) { |
| 1731 |
usbi_dbg(ctx, "active config %u", tmp); |
| 1732 |
*config = (int)tmp; |
| 1733 |
} |
| 1734 |
|
| 1735 |
return r; |
| 1736 |
} |
| 1737 |
|
| 1738 |
/** \ingroup libusb_dev |
| 1739 |
* Set the active configuration for a device. |
| 1740 |
* |
| 1741 |
* The operating system may or may not have already set an active |
| 1742 |
* configuration on the device. It is up to your application to ensure the |
| 1743 |
* correct configuration is selected before you attempt to claim interfaces |
| 1744 |
* and perform other operations. |
| 1745 |
* |
| 1746 |
* If you call this function on a device already configured with the selected |
| 1747 |
* configuration, then this function will act as a lightweight device reset: |
| 1748 |
* it will issue a SET_CONFIGURATION request using the current configuration, |
| 1749 |
* causing most USB-related device state to be reset (altsetting reset to zero, |
| 1750 |
* endpoint halts cleared, toggles reset). |
| 1751 |
* |
| 1752 |
* Not all backends support setting the configuration from user space, which |
| 1753 |
* will be indicated by the return code \ref LIBUSB_ERROR_NOT_SUPPORTED. As this |
| 1754 |
* suggests that the platform is handling the device configuration itself, |
| 1755 |
* this error should generally be safe to ignore. |
| 1756 |
* |
| 1757 |
* You cannot change/reset configuration if your application has claimed |
| 1758 |
* interfaces. It is advised to set the desired configuration before claiming |
| 1759 |
* interfaces. |
| 1760 |
* |
| 1761 |
* Alternatively you can call libusb_release_interface() first. Note if you |
| 1762 |
* do things this way you must ensure that auto_detach_kernel_driver for |
| 1763 |
* <tt>dev</tt> is 0, otherwise the kernel driver will be re-attached when you |
| 1764 |
* release the interface(s). |
| 1765 |
* |
| 1766 |
* You cannot change/reset configuration if other applications or drivers have |
| 1767 |
* claimed interfaces. |
| 1768 |
* |
| 1769 |
* A configuration value of -1 will put the device in unconfigured state. |
| 1770 |
* The USB specifications state that a configuration value of 0 does this, |
| 1771 |
* however buggy devices exist which actually have a configuration 0. |
| 1772 |
* |
| 1773 |
* You should always use this function rather than formulating your own |
| 1774 |
* SET_CONFIGURATION control request. This is because the underlying operating |
| 1775 |
* system needs to know when such changes happen. |
| 1776 |
* |
| 1777 |
* This is a blocking function. |
| 1778 |
* |
| 1779 |
* \param dev_handle a device handle |
| 1780 |
* \param configuration the bConfigurationValue of the configuration you |
| 1781 |
* wish to activate, or -1 if you wish to put the device in an unconfigured |
| 1782 |
* state |
| 1783 |
* \returns 0 on success |
| 1784 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the requested configuration does not exist |
| 1785 |
* \returns \ref LIBUSB_ERROR_BUSY if interfaces are currently claimed |
| 1786 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if setting or changing the configuration |
| 1787 |
* is not supported by the backend |
| 1788 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1789 |
* \returns another LIBUSB_ERROR code on other failure |
| 1790 |
* \see libusb_set_auto_detach_kernel_driver() |
| 1791 |
*/ |
| 1792 |
int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev_handle, |
| 1793 |
int configuration) |
| 1794 |
{ |
| 1795 |
usbi_dbg(HANDLE_CTX(dev_handle), "configuration %d", configuration); |
| 1796 |
if (configuration < -1 || configuration > (int)UINT8_MAX) |
| 1797 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1798 |
return usbi_backend.set_configuration(dev_handle, configuration); |
| 1799 |
} |
| 1800 |
|
| 1801 |
/** \ingroup libusb_dev |
| 1802 |
* Claim an interface on a given device handle. You must claim the interface |
| 1803 |
* you wish to use before you can perform I/O on any of its endpoints. |
| 1804 |
* |
| 1805 |
* It is legal to attempt to claim an already-claimed interface, in which |
| 1806 |
* case libusb just returns 0 without doing anything. |
| 1807 |
* |
| 1808 |
* If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel driver |
| 1809 |
* will be detached if necessary, on failure the detach error is returned. |
| 1810 |
* |
| 1811 |
* Claiming of interfaces is a purely logical operation; it does not cause |
| 1812 |
* any requests to be sent over the bus. Interface claiming is used to |
| 1813 |
* instruct the underlying operating system that your application wishes |
| 1814 |
* to take ownership of the interface. |
| 1815 |
* |
| 1816 |
* This is a non-blocking function. |
| 1817 |
* |
| 1818 |
* \param dev_handle a device handle |
| 1819 |
* \param interface_number the <tt>bInterfaceNumber</tt> of the interface you |
| 1820 |
* wish to claim |
| 1821 |
* \returns 0 on success |
| 1822 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the requested interface does not exist |
| 1823 |
* \returns \ref LIBUSB_ERROR_BUSY if another program or driver has claimed the |
| 1824 |
* interface |
| 1825 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1826 |
* \returns a LIBUSB_ERROR code on other failure |
| 1827 |
* \see libusb_set_auto_detach_kernel_driver() |
| 1828 |
*/ |
| 1829 |
int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev_handle, |
| 1830 |
int interface_number) |
| 1831 |
{ |
| 1832 |
int r = 0; |
| 1833 |
|
| 1834 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number); |
| 1835 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 1836 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1837 |
|
| 1838 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 1839 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1840 |
|
| 1841 |
usbi_mutex_lock(&dev_handle->lock); |
| 1842 |
if (dev_handle->claimed_interfaces & (1U << interface_number)) |
| 1843 |
goto out; |
| 1844 |
|
| 1845 |
r = usbi_backend.claim_interface(dev_handle, (uint8_t)interface_number); |
| 1846 |
if (r == 0) |
| 1847 |
dev_handle->claimed_interfaces |= 1U << interface_number; |
| 1848 |
|
| 1849 |
out: |
| 1850 |
usbi_mutex_unlock(&dev_handle->lock); |
| 1851 |
return r; |
| 1852 |
} |
| 1853 |
|
| 1854 |
/** \ingroup libusb_dev |
| 1855 |
* Release an interface previously claimed with libusb_claim_interface(). You |
| 1856 |
* should release all claimed interfaces before closing a device handle. |
| 1857 |
* |
| 1858 |
* This is a blocking function. A SET_INTERFACE control request will be sent |
| 1859 |
* to the device, resetting interface state to the first alternate setting. |
| 1860 |
* |
| 1861 |
* If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel |
| 1862 |
* driver will be re-attached after releasing the interface. |
| 1863 |
* |
| 1864 |
* \param dev_handle a device handle |
| 1865 |
* \param interface_number the <tt>bInterfaceNumber</tt> of the |
| 1866 |
* previously-claimed interface |
| 1867 |
* \returns 0 on success |
| 1868 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the interface was not claimed |
| 1869 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1870 |
* \returns another LIBUSB_ERROR code on other failure |
| 1871 |
* \see libusb_set_auto_detach_kernel_driver() |
| 1872 |
*/ |
| 1873 |
int API_EXPORTED libusb_release_interface(libusb_device_handle *dev_handle, |
| 1874 |
int interface_number) |
| 1875 |
{ |
| 1876 |
int r; |
| 1877 |
|
| 1878 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number); |
| 1879 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 1880 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1881 |
|
| 1882 |
usbi_mutex_lock(&dev_handle->lock); |
| 1883 |
if (!(dev_handle->claimed_interfaces & (1U << interface_number))) { |
| 1884 |
r = LIBUSB_ERROR_NOT_FOUND; |
| 1885 |
goto out; |
| 1886 |
} |
| 1887 |
|
| 1888 |
r = usbi_backend.release_interface(dev_handle, (uint8_t)interface_number); |
| 1889 |
if (r == 0) |
| 1890 |
dev_handle->claimed_interfaces &= ~(1U << interface_number); |
| 1891 |
|
| 1892 |
out: |
| 1893 |
usbi_mutex_unlock(&dev_handle->lock); |
| 1894 |
return r; |
| 1895 |
} |
| 1896 |
|
| 1897 |
/** \ingroup libusb_dev |
| 1898 |
* Activate an alternate setting for an interface. The interface must have |
| 1899 |
* been previously claimed with libusb_claim_interface(). |
| 1900 |
* |
| 1901 |
* You should always use this function rather than formulating your own |
| 1902 |
* SET_INTERFACE control request. This is because the underlying operating |
| 1903 |
* system needs to know when such changes happen. |
| 1904 |
* |
| 1905 |
* This is a blocking function. |
| 1906 |
* |
| 1907 |
* \param dev_handle a device handle |
| 1908 |
* \param interface_number the <tt>bInterfaceNumber</tt> of the |
| 1909 |
* previously-claimed interface |
| 1910 |
* \param alternate_setting the <tt>bAlternateSetting</tt> of the alternate |
| 1911 |
* setting to activate |
| 1912 |
* \returns 0 on success |
| 1913 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the interface was not claimed, or the |
| 1914 |
* requested alternate setting does not exist |
| 1915 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1916 |
* \returns another LIBUSB_ERROR code on other failure |
| 1917 |
*/ |
| 1918 |
int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev_handle, |
| 1919 |
int interface_number, int alternate_setting) |
| 1920 |
{ |
| 1921 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d altsetting %d", |
| 1922 |
interface_number, alternate_setting); |
| 1923 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 1924 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1925 |
if (alternate_setting < 0 || alternate_setting > (int)UINT8_MAX) |
| 1926 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 1927 |
|
| 1928 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) { |
| 1929 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1930 |
} |
| 1931 |
|
| 1932 |
usbi_mutex_lock(&dev_handle->lock); |
| 1933 |
if (!(dev_handle->claimed_interfaces & (1U << interface_number))) { |
| 1934 |
usbi_mutex_unlock(&dev_handle->lock); |
| 1935 |
return LIBUSB_ERROR_NOT_FOUND; |
| 1936 |
} |
| 1937 |
usbi_mutex_unlock(&dev_handle->lock); |
| 1938 |
|
| 1939 |
return usbi_backend.set_interface_altsetting(dev_handle, |
| 1940 |
(uint8_t)interface_number, (uint8_t)alternate_setting); |
| 1941 |
} |
| 1942 |
|
| 1943 |
/** \ingroup libusb_dev |
| 1944 |
* Clear the halt/stall condition for an endpoint. Endpoints with halt status |
| 1945 |
* are unable to receive or transmit data until the halt condition is stalled. |
| 1946 |
* |
| 1947 |
* You should cancel all pending transfers before attempting to clear the halt |
| 1948 |
* condition. |
| 1949 |
* |
| 1950 |
* This is a blocking function. |
| 1951 |
* |
| 1952 |
* \param dev_handle a device handle |
| 1953 |
* \param endpoint the endpoint to clear halt status |
| 1954 |
* \returns 0 on success |
| 1955 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist |
| 1956 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 1957 |
* \returns another LIBUSB_ERROR code on other failure |
| 1958 |
*/ |
| 1959 |
int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev_handle, |
| 1960 |
unsigned char endpoint) |
| 1961 |
{ |
| 1962 |
usbi_dbg(HANDLE_CTX(dev_handle), "endpoint 0x%x", endpoint); |
| 1963 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 1964 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1965 |
|
| 1966 |
return usbi_backend.clear_halt(dev_handle, endpoint); |
| 1967 |
} |
| 1968 |
|
| 1969 |
/** \ingroup libusb_dev |
| 1970 |
* Perform a USB port reset to reinitialize a device. The system will attempt |
| 1971 |
* to restore the previous configuration and alternate settings after the |
| 1972 |
* reset has completed. |
| 1973 |
* |
| 1974 |
* If the reset fails, the descriptors change, or the previous state cannot be |
| 1975 |
* restored, the device will appear to be disconnected and reconnected. This |
| 1976 |
* means that the device handle is no longer valid (you should close it) and |
| 1977 |
* rediscover the device. A return code of \ref LIBUSB_ERROR_NOT_FOUND indicates |
| 1978 |
* when this is the case. |
| 1979 |
* |
| 1980 |
* This is a blocking function which usually incurs a noticeable delay. |
| 1981 |
* |
| 1982 |
* \param dev_handle a handle of the device to reset |
| 1983 |
* \returns 0 on success |
| 1984 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the |
| 1985 |
* device has been disconnected |
| 1986 |
* \returns another LIBUSB_ERROR code on other failure |
| 1987 |
*/ |
| 1988 |
int API_EXPORTED libusb_reset_device(libusb_device_handle *dev_handle) |
| 1989 |
{ |
| 1990 |
usbi_dbg(HANDLE_CTX(dev_handle), " "); |
| 1991 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 1992 |
return LIBUSB_ERROR_NO_DEVICE; |
| 1993 |
|
| 1994 |
if (usbi_backend.reset_device) |
| 1995 |
return usbi_backend.reset_device(dev_handle); |
| 1996 |
else |
| 1997 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 1998 |
} |
| 1999 |
|
| 2000 |
/** \ingroup libusb_asyncio |
| 2001 |
* Allocate up to num_streams usb bulk streams on the specified endpoints. This |
| 2002 |
* function takes an array of endpoints rather then a single endpoint because |
| 2003 |
* some protocols require that endpoints are setup with similar stream ids. |
| 2004 |
* All endpoints passed in must belong to the same interface. |
| 2005 |
* |
| 2006 |
* Note this function may return less streams then requested. Also note that the |
| 2007 |
* same number of streams are allocated for each endpoint in the endpoint array. |
| 2008 |
* |
| 2009 |
* Stream id 0 is reserved, and should not be used to communicate with devices. |
| 2010 |
* If libusb_alloc_streams() returns with a value of N, you may use stream ids |
| 2011 |
* 1 to N. |
| 2012 |
* |
| 2013 |
* Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103 |
| 2014 |
* |
| 2015 |
* \param dev_handle a device handle |
| 2016 |
* \param num_streams number of streams to try to allocate |
| 2017 |
* \param endpoints array of endpoints to allocate streams on |
| 2018 |
* \param num_endpoints length of the endpoints array |
| 2019 |
* \returns number of streams allocated, or a LIBUSB_ERROR code on failure |
| 2020 |
*/ |
| 2021 |
int API_EXPORTED libusb_alloc_streams(libusb_device_handle *dev_handle, |
| 2022 |
uint32_t num_streams, unsigned char *endpoints, int num_endpoints) |
| 2023 |
{ |
| 2024 |
usbi_dbg(HANDLE_CTX(dev_handle), "streams %u eps %d", (unsigned)num_streams, num_endpoints); |
| 2025 |
|
| 2026 |
if (!num_streams || !endpoints || num_endpoints <= 0) |
| 2027 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2028 |
|
| 2029 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2030 |
return LIBUSB_ERROR_NO_DEVICE; |
| 2031 |
|
| 2032 |
if (usbi_backend.alloc_streams) |
| 2033 |
return usbi_backend.alloc_streams(dev_handle, num_streams, endpoints, |
| 2034 |
num_endpoints); |
| 2035 |
else |
| 2036 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2037 |
} |
| 2038 |
|
| 2039 |
/** \ingroup libusb_asyncio |
| 2040 |
* Free usb bulk streams allocated with libusb_alloc_streams(). |
| 2041 |
* |
| 2042 |
* Note streams are automatically free-ed when releasing an interface. |
| 2043 |
* |
| 2044 |
* Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103 |
| 2045 |
* |
| 2046 |
* \param dev_handle a device handle |
| 2047 |
* \param endpoints array of endpoints to free streams on |
| 2048 |
* \param num_endpoints length of the endpoints array |
| 2049 |
* \returns \ref LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure |
| 2050 |
*/ |
| 2051 |
int API_EXPORTED libusb_free_streams(libusb_device_handle *dev_handle, |
| 2052 |
unsigned char *endpoints, int num_endpoints) |
| 2053 |
{ |
| 2054 |
usbi_dbg(HANDLE_CTX(dev_handle), "eps %d", num_endpoints); |
| 2055 |
|
| 2056 |
if (!endpoints || num_endpoints <= 0) |
| 2057 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2058 |
|
| 2059 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2060 |
return LIBUSB_ERROR_NO_DEVICE; |
| 2061 |
|
| 2062 |
if (usbi_backend.free_streams) |
| 2063 |
return usbi_backend.free_streams(dev_handle, endpoints, |
| 2064 |
num_endpoints); |
| 2065 |
else |
| 2066 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2067 |
} |
| 2068 |
|
| 2069 |
/** \ingroup libusb_asyncio |
| 2070 |
* Attempts to allocate a block of persistent DMA memory suitable for transfers |
| 2071 |
* against the given device. If successful, will return a block of memory |
| 2072 |
* that is suitable for use as "buffer" in \ref libusb_transfer against this |
| 2073 |
* device. Using this memory instead of regular memory means that the host |
| 2074 |
* controller can use DMA directly into the buffer to increase performance, and |
| 2075 |
* also that transfers can no longer fail due to kernel memory fragmentation. |
| 2076 |
* |
| 2077 |
* Note that this means you should not modify this memory (or even data on |
| 2078 |
* the same cache lines) when a transfer is in progress, although it is legal |
| 2079 |
* to have several transfers going on within the same memory block. |
| 2080 |
* |
| 2081 |
* Will return NULL on failure. Many systems do not support such zero-copy |
| 2082 |
* and will always return NULL. Memory allocated with this function must be |
| 2083 |
* freed with \ref libusb_dev_mem_free. Specifically, this means that the |
| 2084 |
* flag \ref LIBUSB_TRANSFER_FREE_BUFFER cannot be used to free memory allocated |
| 2085 |
* with this function. |
| 2086 |
* |
| 2087 |
* Since version 1.0.21, \ref LIBUSB_API_VERSION >= 0x01000105 |
| 2088 |
* |
| 2089 |
* \param dev_handle a device handle |
| 2090 |
* \param length size of desired data buffer |
| 2091 |
* \returns a pointer to the newly allocated memory, or NULL on failure |
| 2092 |
*/ |
| 2093 |
DEFAULT_VISIBILITY |
| 2094 |
unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle, |
| 2095 |
size_t length) |
| 2096 |
{ |
| 2097 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2098 |
return NULL; |
| 2099 |
|
| 2100 |
if (usbi_backend.dev_mem_alloc) |
| 2101 |
return usbi_backend.dev_mem_alloc(dev_handle, length); |
| 2102 |
else |
| 2103 |
return NULL; |
| 2104 |
} |
| 2105 |
|
| 2106 |
/** \ingroup libusb_asyncio |
| 2107 |
* Free device memory allocated with libusb_dev_mem_alloc(). |
| 2108 |
* |
| 2109 |
* \param dev_handle a device handle |
| 2110 |
* \param buffer pointer to the previously allocated memory |
| 2111 |
* \param length size of previously allocated memory |
| 2112 |
* \returns \ref LIBUSB_SUCCESS, or a LIBUSB_ERROR code on failure |
| 2113 |
*/ |
| 2114 |
int API_EXPORTED libusb_dev_mem_free(libusb_device_handle *dev_handle, |
| 2115 |
unsigned char *buffer, size_t length) |
| 2116 |
{ |
| 2117 |
if (usbi_backend.dev_mem_free) |
| 2118 |
return usbi_backend.dev_mem_free(dev_handle, buffer, length); |
| 2119 |
else |
| 2120 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2121 |
} |
| 2122 |
|
| 2123 |
/** \ingroup libusb_dev |
| 2124 |
* Determine if a kernel driver is active on an interface. If a kernel driver |
| 2125 |
* is active, you cannot claim the interface, and libusb will be unable to |
| 2126 |
* perform I/O. |
| 2127 |
* |
| 2128 |
* This functionality is not available on Windows. |
| 2129 |
* |
| 2130 |
* \param dev_handle a device handle |
| 2131 |
* \param interface_number the interface to check |
| 2132 |
* \returns 0 if no kernel driver is active |
| 2133 |
* \returns 1 if a kernel driver is active |
| 2134 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 2135 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality |
| 2136 |
* is not available |
| 2137 |
* \returns another LIBUSB_ERROR code on other failure |
| 2138 |
* \see libusb_detach_kernel_driver() |
| 2139 |
*/ |
| 2140 |
int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev_handle, |
| 2141 |
int interface_number) |
| 2142 |
{ |
| 2143 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number); |
| 2144 |
|
| 2145 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 2146 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2147 |
|
| 2148 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2149 |
return LIBUSB_ERROR_NO_DEVICE; |
| 2150 |
|
| 2151 |
if (usbi_backend.kernel_driver_active) |
| 2152 |
return usbi_backend.kernel_driver_active(dev_handle, (uint8_t)interface_number); |
| 2153 |
else |
| 2154 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2155 |
} |
| 2156 |
|
| 2157 |
/** \ingroup libusb_dev |
| 2158 |
* Detach a kernel driver from an interface. If successful, you will then be |
| 2159 |
* able to claim the interface and perform I/O. |
| 2160 |
* |
| 2161 |
* This functionality is not available on Windows. |
| 2162 |
* |
| 2163 |
* Note that libusb itself also talks to the device through a special kernel |
| 2164 |
* driver, if this driver is already attached to the device, this call will |
| 2165 |
* not detach it and return \ref LIBUSB_ERROR_NOT_FOUND. |
| 2166 |
* |
| 2167 |
* \param dev_handle a device handle |
| 2168 |
* \param interface_number the interface to detach the driver from |
| 2169 |
* \returns 0 on success |
| 2170 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if no kernel driver was active |
| 2171 |
* \returns \ref LIBUSB_ERROR_INVALID_PARAM if the interface does not exist |
| 2172 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 2173 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality |
| 2174 |
* is not available |
| 2175 |
* \returns another LIBUSB_ERROR code on other failure |
| 2176 |
* \see libusb_kernel_driver_active() |
| 2177 |
*/ |
| 2178 |
int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev_handle, |
| 2179 |
int interface_number) |
| 2180 |
{ |
| 2181 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number); |
| 2182 |
|
| 2183 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 2184 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2185 |
|
| 2186 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2187 |
return LIBUSB_ERROR_NO_DEVICE; |
| 2188 |
|
| 2189 |
if (usbi_backend.detach_kernel_driver) |
| 2190 |
return usbi_backend.detach_kernel_driver(dev_handle, (uint8_t)interface_number); |
| 2191 |
else |
| 2192 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2193 |
} |
| 2194 |
|
| 2195 |
/** \ingroup libusb_dev |
| 2196 |
* Re-attach an interface's kernel driver, which was previously detached |
| 2197 |
* using libusb_detach_kernel_driver(). |
| 2198 |
* |
| 2199 |
* This functionality is not available on Windows. |
| 2200 |
* |
| 2201 |
* \param dev_handle a device handle |
| 2202 |
* \param interface_number the interface to attach the driver from |
| 2203 |
* \returns 0 on success |
| 2204 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if no kernel driver was active |
| 2205 |
* \returns \ref LIBUSB_ERROR_INVALID_PARAM if the interface does not exist |
| 2206 |
* \returns \ref LIBUSB_ERROR_NO_DEVICE if the device has been disconnected |
| 2207 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality |
| 2208 |
* is not available |
| 2209 |
* \returns \ref LIBUSB_ERROR_BUSY if the driver cannot be attached because the |
| 2210 |
* interface is claimed by a program or driver |
| 2211 |
* \returns another LIBUSB_ERROR code on other failure |
| 2212 |
* \see libusb_kernel_driver_active() |
| 2213 |
*/ |
| 2214 |
int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev_handle, |
| 2215 |
int interface_number) |
| 2216 |
{ |
| 2217 |
usbi_dbg(HANDLE_CTX(dev_handle), "interface %d", interface_number); |
| 2218 |
|
| 2219 |
if (interface_number < 0 || interface_number >= USB_MAXINTERFACES) |
| 2220 |
return LIBUSB_ERROR_INVALID_PARAM; |
| 2221 |
|
| 2222 |
if (!usbi_atomic_load(&dev_handle->dev->attached)) |
| 2223 |
return LIBUSB_ERROR_NO_DEVICE; |
| 2224 |
|
| 2225 |
if (usbi_backend.attach_kernel_driver) |
| 2226 |
return usbi_backend.attach_kernel_driver(dev_handle, (uint8_t)interface_number); |
| 2227 |
else |
| 2228 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2229 |
} |
| 2230 |
|
| 2231 |
/** \ingroup libusb_dev |
| 2232 |
* Enable/disable libusb's automatic kernel driver detachment. When this is |
| 2233 |
* enabled libusb will automatically detach the kernel driver on an interface |
| 2234 |
* when claiming the interface, and attach it when releasing the interface. |
| 2235 |
* |
| 2236 |
* Automatic kernel driver detachment is disabled on newly opened device |
| 2237 |
* handles by default. |
| 2238 |
* |
| 2239 |
* On platforms which do not have LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER |
| 2240 |
* this function will return \ref LIBUSB_ERROR_NOT_SUPPORTED, and libusb will |
| 2241 |
* continue as if this function was never called. |
| 2242 |
* |
| 2243 |
* \param dev_handle a device handle |
| 2244 |
* \param enable whether to enable or disable auto kernel driver detachment |
| 2245 |
* |
| 2246 |
* \returns \ref LIBUSB_SUCCESS on success |
| 2247 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality |
| 2248 |
* is not available |
| 2249 |
* \see libusb_claim_interface() |
| 2250 |
* \see libusb_release_interface() |
| 2251 |
* \see libusb_set_configuration() |
| 2252 |
*/ |
| 2253 |
int API_EXPORTED libusb_set_auto_detach_kernel_driver( |
| 2254 |
libusb_device_handle *dev_handle, int enable) |
| 2255 |
{ |
| 2256 |
if (!(usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER)) |
| 2257 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2258 |
|
| 2259 |
dev_handle->auto_detach_kernel_driver = enable; |
| 2260 |
return LIBUSB_SUCCESS; |
| 2261 |
} |
| 2262 |
|
| 2263 |
/** \ingroup libusb_dev |
| 2264 |
* Check if the endpoint supports RAW_IO. |
| 2265 |
* \param dev_handle a device handle |
| 2266 |
* \param endpoint the endpoint to check |
| 2267 |
* |
| 2268 |
* \returns 1 if the endpoint supports RAW_IO |
| 2269 |
* \returns 0 if the endpoint does not support RAW_IO |
| 2270 |
* \returns a LIBUSB_ERROR code on failure |
| 2271 |
* |
| 2272 |
* Only endpoints using the WinUSB driver support RAW_IO, |
| 2273 |
* for all other backends/drivers this function will return 0. |
| 2274 |
* |
| 2275 |
* Fails if the interface the endpoint belongs to isn't claimed, |
| 2276 |
* \see libusb_claim_interface(). |
| 2277 |
* |
| 2278 |
* \see libusb_endpoint_set_raw_io() |
| 2279 |
* |
| 2280 |
* Since version 1.0.30, \ref LIBUSB_API_VERSION >= 0x0100010C |
| 2281 |
*/ |
| 2282 |
int API_EXPORTED libusb_endpoint_supports_raw_io(libusb_device_handle* dev_handle, |
| 2283 |
uint8_t endpoint) |
| 2284 |
{ |
| 2285 |
if (usbi_backend.endpoint_supports_raw_io == NULL) |
| 2286 |
{ |
| 2287 |
return 0; |
| 2288 |
} |
| 2289 |
|
| 2290 |
// If the `endpoint_supports_raw_io` function is present, these two should be too: |
| 2291 |
assert(usbi_backend.endpoint_set_raw_io != NULL); |
| 2292 |
assert(usbi_backend.get_max_raw_io_transfer_size != NULL); |
| 2293 |
|
| 2294 |
return usbi_backend.endpoint_supports_raw_io(dev_handle, endpoint); |
| 2295 |
} |
| 2296 |
|
| 2297 |
/** \ingroup libusb_dev |
| 2298 |
* Enable/disable RAW_IO for an endpoint on an open device. |
| 2299 |
* |
| 2300 |
* Only endpoints using the WinUSB driver support RAW_IO, |
| 2301 |
* for all other backends/drivers this function will return an error code. |
| 2302 |
* |
| 2303 |
* Using RAW_IO can greatly improve USB throughput by directly passing |
| 2304 |
* transfer requests to the underlying USB driver instead of queuing them |
| 2305 |
* in WinUSB. This can be particularly useful for high-throughput devices |
| 2306 |
* like cameras or oscilloscopes. |
| 2307 |
* |
| 2308 |
* Transfers submitted to the endpoint while RAW_IO is enabled will fail unless |
| 2309 |
* they adhere to the following rules : |
| 2310 |
* - The buffer length must be a multiple of the maximum endpoint packet size |
| 2311 |
* \see libusb_get_max_packet_size. |
| 2312 |
* - The buffer length must be less than or equal to the value returned by |
| 2313 |
* \ref libusb_get_max_raw_io_transfer_size. |
| 2314 |
* |
| 2315 |
* This option should not be changed when any transfer is in progress on |
| 2316 |
* the specified endpoint. |
| 2317 |
* |
| 2318 |
* Fails if the interface the endpoint belongs to isn't claimed, |
| 2319 |
* \see libusb_claim_interface(). |
| 2320 |
* |
| 2321 |
* \param dev_handle a device handle |
| 2322 |
* \param endpoint the endpoint to set RAW_IO for |
| 2323 |
* \param enable 1 to enable RAW_IO, 0 to disable it |
| 2324 |
* |
| 2325 |
* \returns \ref LIBUSB_SUCCESS on success |
| 2326 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the backend does not support RAW_IO. |
| 2327 |
* \returns another LIBUSB_ERROR code on other failure |
| 2328 |
* |
| 2329 |
* \see libusb_endpoint_supports_raw_io() |
| 2330 |
* \see https://learn.microsoft.com/en-us/windows-hardware/drivers/usbcon/winusb-functions-for-pipe-policy-modification |
| 2331 |
* |
| 2332 |
* Since version 1.0.30, \ref LIBUSB_API_VERSION >= 0x0100010C |
| 2333 |
*/ |
| 2334 |
int API_EXPORTED libusb_endpoint_set_raw_io(libusb_device_handle* dev_handle, |
| 2335 |
uint8_t endpoint, int enable) |
| 2336 |
{ |
| 2337 |
if (!usbi_backend.endpoint_set_raw_io) |
| 2338 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2339 |
|
| 2340 |
return usbi_backend.endpoint_set_raw_io(dev_handle, endpoint, enable); |
| 2341 |
} |
| 2342 |
|
| 2343 |
/** \ingroup libusb_dev |
| 2344 |
* Retrieve the maximum transfer size in bytes supported for WinUSB RAW_IO |
| 2345 |
* for an inbound bulk or interrupt endpoint on an open device. |
| 2346 |
* |
| 2347 |
* Returns a maximum transfer size in bytes, or a negative error code. |
| 2348 |
* If the backend does not support WinUSB RAW_IO, returns |
| 2349 |
* \ref LIBUSB_ERROR_NOT_SUPPORTED. |
| 2350 |
* |
| 2351 |
* Fails if the interface the endpoint belongs to isn't claimed, |
| 2352 |
* \see libusb_claim_interface(). |
| 2353 |
* |
| 2354 |
* \see libusb_endpoint_set_raw_io() |
| 2355 |
* |
| 2356 |
* Since version 1.0.30, \ref LIBUSB_API_VERSION >= 0x0100010C |
| 2357 |
*/ |
| 2358 |
int API_EXPORTED libusb_get_max_raw_io_transfer_size( |
| 2359 |
libusb_device_handle *dev_handle, uint8_t endpoint) |
| 2360 |
{ |
| 2361 |
if (!usbi_backend.get_max_raw_io_transfer_size) |
| 2362 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 2363 |
|
| 2364 |
return usbi_backend.get_max_raw_io_transfer_size( |
| 2365 |
dev_handle, endpoint); |
| 2366 |
} |
| 2367 |
|
| 2368 |
/** \ingroup libusb_lib |
| 2369 |
* Deprecated. Use libusb_set_option() or libusb_init_context() instead, |
| 2370 |
* with the \ref LIBUSB_OPTION_LOG_LEVEL option. |
| 2371 |
*/ |
| 2372 |
void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level) |
| 2373 |
{ |
| 2374 |
libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, level); |
| 2375 |
} |
| 2376 |
|
| 2377 |
static void libusb_set_log_cb_internal(libusb_context *ctx, libusb_log_cb cb, |
| 2378 |
int mode) |
| 2379 |
{ |
| 2380 |
#if defined(ENABLE_LOGGING) && (!defined(ENABLE_DEBUG_LOGGING) || !defined(USE_SYSTEM_LOGGING_FACILITY)) |
| 2381 |
#if !defined(USE_SYSTEM_LOGGING_FACILITY) |
| 2382 |
if (mode & LIBUSB_LOG_CB_GLOBAL) |
| 2383 |
log_handler = cb; |
| 2384 |
#endif |
| 2385 |
#if !defined(ENABLE_DEBUG_LOGGING) |
| 2386 |
if (mode & LIBUSB_LOG_CB_CONTEXT) { |
| 2387 |
ctx = usbi_get_context(ctx); |
| 2388 |
ctx->log_handler = cb; |
| 2389 |
} |
| 2390 |
#else |
| 2391 |
UNUSED(ctx); |
| 2392 |
#endif |
| 2393 |
#else |
| 2394 |
UNUSED(ctx); |
| 2395 |
UNUSED(cb); |
| 2396 |
UNUSED(mode); |
| 2397 |
#endif |
| 2398 |
} |
| 2399 |
|
| 2400 |
/** \ingroup libusb_lib |
| 2401 |
* Set log handler. |
| 2402 |
* |
| 2403 |
* libusb will redirect its log messages to the provided callback function. |
| 2404 |
* libusb supports redirection of per context and global log messages. |
| 2405 |
* Log messages sent to the context will be sent to the global log handler too. |
| 2406 |
* |
| 2407 |
* If libusb is compiled without message logging or USE_SYSTEM_LOGGING_FACILITY |
| 2408 |
* is defined then global callback function will never be called. |
| 2409 |
* If ENABLE_DEBUG_LOGGING is defined then per context callback function will |
| 2410 |
* never be called. |
| 2411 |
* |
| 2412 |
* Since version 1.0.23, \ref LIBUSB_API_VERSION >= 0x01000107 |
| 2413 |
* |
| 2414 |
* \param ctx context on which to assign log handler, or NULL for the default |
| 2415 |
* context. Parameter ignored if only LIBUSB_LOG_CB_GLOBAL mode is requested. |
| 2416 |
* \param cb pointer to the callback function, or NULL to stop log |
| 2417 |
* messages redirection |
| 2418 |
* \param mode mode of callback function operation. Several modes can be |
| 2419 |
* selected for a single callback function, see \ref libusb_log_cb_mode for |
| 2420 |
* a description. |
| 2421 |
* \see libusb_log_cb, libusb_log_cb_mode |
| 2422 |
*/ |
| 2423 |
void API_EXPORTED libusb_set_log_cb(libusb_context *ctx, libusb_log_cb cb, |
| 2424 |
int mode) |
| 2425 |
{ |
| 2426 |
libusb_set_log_cb_internal(ctx, cb, mode); |
| 2427 |
} |
| 2428 |
|
| 2429 |
/** \ingroup libusb_lib |
| 2430 |
* Set an option in the library. |
| 2431 |
* |
| 2432 |
* Use this function to configure a specific option within the library. |
| 2433 |
* |
| 2434 |
* Some options require one or more arguments to be provided. Consult each |
| 2435 |
* option's documentation for specific requirements. |
| 2436 |
* |
| 2437 |
* If the context ctx is NULL, the option will be added to a list of default |
| 2438 |
* options that will be applied to all subsequently created contexts. |
| 2439 |
* |
| 2440 |
* Since version 1.0.22, \ref LIBUSB_API_VERSION >= 0x01000106 |
| 2441 |
* |
| 2442 |
* \param ctx context on which to operate |
| 2443 |
* \param option which option to set |
| 2444 |
* \param ... any required arguments for the specified option |
| 2445 |
* |
| 2446 |
* \returns \ref LIBUSB_SUCCESS on success |
| 2447 |
* \returns \ref LIBUSB_ERROR_INVALID_PARAM if the option or arguments are invalid |
| 2448 |
* \returns \ref LIBUSB_ERROR_NOT_SUPPORTED if the option is valid but not supported |
| 2449 |
* on this platform |
| 2450 |
* \returns \ref LIBUSB_ERROR_NOT_FOUND if LIBUSB_OPTION_USE_USBDK is valid on this platform but UsbDk is not available |
| 2451 |
*/ |
| 2452 |
int API_EXPORTEDV libusb_set_option(libusb_context *ctx, |
| 2453 |
enum libusb_option option, ...) |
| 2454 |
{ |
| 2455 |
int arg = 0, r = LIBUSB_SUCCESS; |
| 2456 |
libusb_log_cb log_cb = NULL; |
| 2457 |
va_list ap; |
| 2458 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2459 |
int is_default_context = (NULL == ctx); |
| 2460 |
#endif |
| 2461 |
|
| 2462 |
va_start(ap, option); |
| 2463 |
|
| 2464 |
if (LIBUSB_OPTION_LOG_LEVEL == option) { |
| 2465 |
arg = va_arg(ap, int); |
| 2466 |
if (arg < LIBUSB_LOG_LEVEL_NONE || arg > LIBUSB_LOG_LEVEL_DEBUG) { |
| 2467 |
r = LIBUSB_ERROR_INVALID_PARAM; |
| 2468 |
} |
| 2469 |
} |
| 2470 |
if (LIBUSB_OPTION_LOG_CB == option) { |
| 2471 |
log_cb = (libusb_log_cb) va_arg(ap, libusb_log_cb); |
| 2472 |
} |
| 2473 |
|
| 2474 |
do { |
| 2475 |
if (LIBUSB_SUCCESS != r) { |
| 2476 |
break; |
| 2477 |
} |
| 2478 |
|
| 2479 |
if (option >= LIBUSB_OPTION_MAX) { |
| 2480 |
r = LIBUSB_ERROR_INVALID_PARAM; |
| 2481 |
break; |
| 2482 |
} |
| 2483 |
|
| 2484 |
if (NULL == ctx) { |
| 2485 |
usbi_mutex_static_lock(&default_context_lock); |
| 2486 |
default_context_options[option].is_set = 1; |
| 2487 |
if (LIBUSB_OPTION_LOG_LEVEL == option) { |
| 2488 |
default_context_options[option].arg.ival = arg; |
| 2489 |
} else if (LIBUSB_OPTION_LOG_CB == option) { |
| 2490 |
default_context_options[option].arg.log_cbval = log_cb; |
| 2491 |
libusb_set_log_cb_internal(NULL, log_cb, LIBUSB_LOG_CB_GLOBAL); |
| 2492 |
} |
| 2493 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2494 |
} |
| 2495 |
|
| 2496 |
ctx = usbi_get_context(ctx); |
| 2497 |
if (NULL == ctx) |
| 2498 |
break; |
| 2499 |
|
| 2500 |
switch (option) { |
| 2501 |
case LIBUSB_OPTION_LOG_LEVEL: |
| 2502 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2503 |
if (!ctx->debug_fixed) { |
| 2504 |
ctx->debug = (enum libusb_log_level)arg; |
| 2505 |
if (is_default_context) |
| 2506 |
usbi_atomic_store(&default_debug_level, CLAMP(arg, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG)); |
| 2507 |
} |
| 2508 |
#endif |
| 2509 |
break; |
| 2510 |
|
| 2511 |
/* Handle all backend-specific options here */ |
| 2512 |
case LIBUSB_OPTION_USE_USBDK: |
| 2513 |
case LIBUSB_OPTION_NO_DEVICE_DISCOVERY: |
| 2514 |
if (usbi_backend.set_option) { |
| 2515 |
r = usbi_backend.set_option(ctx, option, ap); |
| 2516 |
break; |
| 2517 |
} |
| 2518 |
|
| 2519 |
r = LIBUSB_ERROR_NOT_SUPPORTED; |
| 2520 |
break; |
| 2521 |
|
| 2522 |
case LIBUSB_OPTION_LOG_CB: |
| 2523 |
libusb_set_log_cb_internal(ctx, log_cb, LIBUSB_LOG_CB_CONTEXT); |
| 2524 |
break; |
| 2525 |
|
| 2526 |
case LIBUSB_OPTION_MAX: /* unreachable */ |
| 2527 |
default: |
| 2528 |
r = LIBUSB_ERROR_INVALID_PARAM; |
| 2529 |
} |
| 2530 |
} while (0); |
| 2531 |
|
| 2532 |
va_end(ap); |
| 2533 |
|
| 2534 |
return r; |
| 2535 |
} |
| 2536 |
|
| 2537 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2538 |
/* returns the log level as defined in the LIBUSB_DEBUG environment variable. |
| 2539 |
* if LIBUSB_DEBUG is not present or not a number, returns LIBUSB_LOG_LEVEL_NONE. |
| 2540 |
* value is clamped to ensure it is within the valid range of possibilities. |
| 2541 |
*/ |
| 2542 |
static enum libusb_log_level get_env_debug_level(void) |
| 2543 |
{ |
| 2544 |
enum libusb_log_level level = LIBUSB_LOG_LEVEL_NONE; |
| 2545 |
const char *dbg = getenv("LIBUSB_DEBUG"); |
| 2546 |
if (dbg) { |
| 2547 |
long dbg_level = strtol(dbg, NULL, 10); |
| 2548 |
dbg_level = CLAMP(dbg_level, LIBUSB_LOG_LEVEL_NONE, LIBUSB_LOG_LEVEL_DEBUG); |
| 2549 |
level = (enum libusb_log_level)dbg_level; |
| 2550 |
} |
| 2551 |
return level; |
| 2552 |
} |
| 2553 |
#endif |
| 2554 |
|
| 2555 |
/** \ingroup libusb_lib |
| 2556 |
* Deprecated initialization function. Equivalent to calling libusb_init_context with no options. |
| 2557 |
* |
| 2558 |
* \see libusb_init_context |
| 2559 |
*/ |
| 2560 |
int API_EXPORTED libusb_init(libusb_context **ctx) |
| 2561 |
{ |
| 2562 |
return libusb_init_context(ctx, NULL, 0); |
| 2563 |
} |
| 2564 |
|
| 2565 |
/** \ingroup libusb_lib |
| 2566 |
* Initialize libusb. This function must be called before calling any other |
| 2567 |
* libusb function. |
| 2568 |
* |
| 2569 |
* If you do not provide an output location for a context pointer, a default |
| 2570 |
* context will be created. If there was already a default context, it will |
| 2571 |
* be reused (and nothing will be initialized/reinitialized and options will |
| 2572 |
* be ignored). If num_options is 0 then options is ignored and may be NULL. |
| 2573 |
* |
| 2574 |
* Since version 1.0.27, \ref LIBUSB_API_VERSION >= 0x0100010A |
| 2575 |
* |
| 2576 |
* \param ctx Optional output location for context pointer. |
| 2577 |
* Only valid on return code 0. |
| 2578 |
* \param options Optional array of options to set on the new context. |
| 2579 |
* \param num_options Number of elements in the options array. |
| 2580 |
* \returns 0 on success, or a LIBUSB_ERROR code on failure |
| 2581 |
* \see libusb_contexts |
| 2582 |
*/ |
| 2583 |
int API_EXPORTED libusb_init_context(libusb_context **ctx, const struct libusb_init_option options[], int num_options) |
| 2584 |
{ |
| 2585 |
size_t priv_size = usbi_backend.context_priv_size; |
| 2586 |
struct libusb_context *_ctx; |
| 2587 |
int r; |
| 2588 |
|
| 2589 |
usbi_mutex_static_lock(&default_context_lock); |
| 2590 |
|
| 2591 |
if (!ctx && default_context_refcnt > 0) { |
| 2592 |
usbi_dbg(usbi_default_context, "reusing default context"); |
| 2593 |
default_context_refcnt++; |
| 2594 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2595 |
return 0; |
| 2596 |
} |
| 2597 |
|
| 2598 |
/* check for first init */ |
| 2599 |
usbi_mutex_static_lock(&active_contexts_lock); |
| 2600 |
if (!active_contexts_list.next) { |
| 2601 |
list_init(&active_contexts_list); |
| 2602 |
usbi_get_monotonic_time(×tamp_origin); |
| 2603 |
} |
| 2604 |
usbi_mutex_static_unlock(&active_contexts_lock); |
| 2605 |
|
| 2606 |
_ctx = calloc(1, PTR_ALIGN(sizeof(*_ctx)) + priv_size); |
| 2607 |
if (!_ctx) { |
| 2608 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2609 |
return LIBUSB_ERROR_NO_MEM; |
| 2610 |
} |
| 2611 |
|
| 2612 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2613 |
_ctx->debug = LIBUSB_LOG_LEVEL_NONE; |
| 2614 |
if (getenv("LIBUSB_DEBUG")) { |
| 2615 |
_ctx->debug = get_env_debug_level(); |
| 2616 |
_ctx->debug_fixed = 1; |
| 2617 |
} else if (default_context_options[LIBUSB_OPTION_LOG_LEVEL].is_set) { |
| 2618 |
_ctx->debug = (enum libusb_log_level)default_context_options[LIBUSB_OPTION_LOG_LEVEL].arg.ival; |
| 2619 |
} |
| 2620 |
#endif |
| 2621 |
|
| 2622 |
usbi_mutex_init(&_ctx->usb_devs_lock); |
| 2623 |
usbi_mutex_init(&_ctx->open_devs_lock); |
| 2624 |
list_init(&_ctx->usb_devs); |
| 2625 |
list_init(&_ctx->open_devs); |
| 2626 |
|
| 2627 |
/* apply default options to all new contexts */ |
| 2628 |
for (enum libusb_option option = 0 ; option < LIBUSB_OPTION_MAX ; option++) { |
| 2629 |
if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) { |
| 2630 |
continue; |
| 2631 |
} |
| 2632 |
if (LIBUSB_OPTION_LOG_CB != option) { |
| 2633 |
r = libusb_set_option(_ctx, option); |
| 2634 |
} else { |
| 2635 |
r = libusb_set_option(_ctx, option, default_context_options[option].arg.log_cbval); |
| 2636 |
} |
| 2637 |
if (LIBUSB_SUCCESS != r) |
| 2638 |
goto err_free_ctx; |
| 2639 |
} |
| 2640 |
|
| 2641 |
/* apply any options provided by the user */ |
| 2642 |
for (int i = 0 ; i < num_options ; ++i) { |
| 2643 |
switch(options[i].option) { |
| 2644 |
case LIBUSB_OPTION_LOG_CB: |
| 2645 |
r = libusb_set_option(_ctx, options[i].option, options[i].value.log_cbval); |
| 2646 |
break; |
| 2647 |
|
| 2648 |
case LIBUSB_OPTION_LOG_LEVEL: |
| 2649 |
case LIBUSB_OPTION_USE_USBDK: |
| 2650 |
case LIBUSB_OPTION_NO_DEVICE_DISCOVERY: |
| 2651 |
case LIBUSB_OPTION_MAX: |
| 2652 |
default: |
| 2653 |
r = libusb_set_option(_ctx, options[i].option, options[i].value.ival); |
| 2654 |
} |
| 2655 |
if (LIBUSB_SUCCESS != r) |
| 2656 |
goto err_free_ctx; |
| 2657 |
} |
| 2658 |
|
| 2659 |
/* default context must be initialized before calling usbi_dbg */ |
| 2660 |
if (!ctx) { |
| 2661 |
usbi_default_context = _ctx; |
| 2662 |
default_context_refcnt = 1; |
| 2663 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2664 |
usbi_atomic_store(&default_debug_level, _ctx->debug); |
| 2665 |
#endif |
| 2666 |
usbi_dbg(usbi_default_context, "created default context"); |
| 2667 |
} |
| 2668 |
|
| 2669 |
usbi_dbg(_ctx, "libusb v%u.%u.%u.%u%s", libusb_version_internal.major, libusb_version_internal.minor, |
| 2670 |
libusb_version_internal.micro, libusb_version_internal.nano, libusb_version_internal.rc); |
| 2671 |
|
| 2672 |
r = usbi_io_init(_ctx); |
| 2673 |
if (r < 0) |
| 2674 |
goto err_free_ctx; |
| 2675 |
|
| 2676 |
usbi_mutex_static_lock(&active_contexts_lock); |
| 2677 |
list_add(&_ctx->list, &active_contexts_list); |
| 2678 |
usbi_mutex_static_unlock(&active_contexts_lock); |
| 2679 |
|
| 2680 |
if (usbi_backend.init) { |
| 2681 |
r = usbi_backend.init(_ctx); |
| 2682 |
if (r) |
| 2683 |
goto err_io_exit; |
| 2684 |
} |
| 2685 |
|
| 2686 |
/* Initialize hotplug after the initial enumeration is done. */ |
| 2687 |
usbi_hotplug_init(_ctx); |
| 2688 |
|
| 2689 |
if (ctx) { |
| 2690 |
*ctx = _ctx; |
| 2691 |
|
| 2692 |
if (!usbi_fallback_context) { |
| 2693 |
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING) |
| 2694 |
if (usbi_atomic_load(&default_debug_level) == -1) |
| 2695 |
usbi_atomic_store(&default_debug_level, _ctx->debug); |
| 2696 |
#endif |
| 2697 |
usbi_fallback_context = _ctx; |
| 2698 |
usbi_dbg(usbi_fallback_context, "installing new context as implicit default"); |
| 2699 |
} |
| 2700 |
} |
| 2701 |
|
| 2702 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2703 |
|
| 2704 |
return 0; |
| 2705 |
|
| 2706 |
err_io_exit: |
| 2707 |
usbi_mutex_static_lock(&active_contexts_lock); |
| 2708 |
list_del(&_ctx->list); |
| 2709 |
usbi_mutex_static_unlock(&active_contexts_lock); |
| 2710 |
|
| 2711 |
usbi_hotplug_exit(_ctx); |
| 2712 |
usbi_io_exit(_ctx); |
| 2713 |
|
| 2714 |
err_free_ctx: |
| 2715 |
if (!ctx) { |
| 2716 |
/* clear default context that was not fully initialized */ |
| 2717 |
usbi_default_context = NULL; |
| 2718 |
default_context_refcnt = 0; |
| 2719 |
} |
| 2720 |
|
| 2721 |
usbi_mutex_destroy(&_ctx->open_devs_lock); |
| 2722 |
usbi_mutex_destroy(&_ctx->usb_devs_lock); |
| 2723 |
|
| 2724 |
free(_ctx); |
| 2725 |
|
| 2726 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2727 |
|
| 2728 |
return r; |
| 2729 |
} |
| 2730 |
|
| 2731 |
/** \ingroup libusb_lib |
| 2732 |
* Deinitialize libusb. Should be called after closing all open devices and |
| 2733 |
* before your application terminates. |
| 2734 |
* \param ctx the context to deinitialize, or NULL for the default context |
| 2735 |
*/ |
| 2736 |
void API_EXPORTED libusb_exit(libusb_context *ctx) |
| 2737 |
{ |
| 2738 |
struct libusb_context *_ctx; |
| 2739 |
struct libusb_device *dev; |
| 2740 |
|
| 2741 |
usbi_mutex_static_lock(&default_context_lock); |
| 2742 |
|
| 2743 |
/* if working with default context, only actually do the deinitialization |
| 2744 |
* if we're the last user */ |
| 2745 |
if (!ctx) { |
| 2746 |
if (!usbi_default_context) { |
| 2747 |
usbi_dbg(ctx, "no default context, not initialized?"); |
| 2748 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2749 |
return; |
| 2750 |
} |
| 2751 |
|
| 2752 |
if (--default_context_refcnt > 0) { |
| 2753 |
usbi_dbg(ctx, "not destroying default context"); |
| 2754 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2755 |
return; |
| 2756 |
} |
| 2757 |
|
| 2758 |
usbi_dbg(ctx, "destroying default context"); |
| 2759 |
_ctx = usbi_default_context; |
| 2760 |
} else { |
| 2761 |
usbi_dbg(ctx, " "); |
| 2762 |
_ctx = ctx; |
| 2763 |
} |
| 2764 |
|
| 2765 |
usbi_mutex_static_lock(&active_contexts_lock); |
| 2766 |
list_del(&_ctx->list); |
| 2767 |
usbi_mutex_static_unlock(&active_contexts_lock); |
| 2768 |
|
| 2769 |
/* Exit hotplug before backend dependency */ |
| 2770 |
usbi_hotplug_exit(_ctx); |
| 2771 |
|
| 2772 |
if (usbi_backend.exit) |
| 2773 |
usbi_backend.exit(_ctx); |
| 2774 |
|
| 2775 |
if (!ctx) |
| 2776 |
usbi_default_context = NULL; |
| 2777 |
if (ctx == usbi_fallback_context) |
| 2778 |
usbi_fallback_context = NULL; |
| 2779 |
|
| 2780 |
usbi_mutex_static_unlock(&default_context_lock); |
| 2781 |
|
| 2782 |
/* Don't bother with locking after this point because unless there is |
| 2783 |
* an application bug, nobody will be accessing the context. */ |
| 2784 |
|
| 2785 |
usbi_io_exit(_ctx); |
| 2786 |
|
| 2787 |
for_each_device(_ctx, dev) { |
| 2788 |
usbi_warn(_ctx, "device %d.%d still referenced", |
| 2789 |
dev->bus_number, dev->device_address); |
| 2790 |
DEVICE_CTX(dev) = NULL; |
| 2791 |
} |
| 2792 |
|
| 2793 |
if (!list_empty(&_ctx->open_devs)) |
| 2794 |
usbi_warn(_ctx, "application left some devices open"); |
| 2795 |
|
| 2796 |
usbi_mutex_destroy(&_ctx->open_devs_lock); |
| 2797 |
usbi_mutex_destroy(&_ctx->usb_devs_lock); |
| 2798 |
|
| 2799 |
free(_ctx); |
| 2800 |
} |
| 2801 |
|
| 2802 |
/** \ingroup libusb_misc |
| 2803 |
* Check at runtime if the loaded library has a given capability. |
| 2804 |
* This call should be performed after \ref libusb_init_context(), to ensure the |
| 2805 |
* backend has updated its capability set. |
| 2806 |
* |
| 2807 |
* \param capability the \ref libusb_capability to check for |
| 2808 |
* \returns nonzero if the running library has the capability, 0 otherwise |
| 2809 |
*/ |
| 2810 |
int API_EXPORTED libusb_has_capability(uint32_t capability) |
| 2811 |
{ |
| 2812 |
switch (capability) { |
| 2813 |
case LIBUSB_CAP_HAS_CAPABILITY: |
| 2814 |
return 1; |
| 2815 |
case LIBUSB_CAP_HAS_HOTPLUG: |
| 2816 |
return !(usbi_backend.get_device_list); |
| 2817 |
case LIBUSB_CAP_HAS_HID_ACCESS: |
| 2818 |
return (usbi_backend.caps & USBI_CAP_HAS_HID_ACCESS); |
| 2819 |
case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: |
| 2820 |
return (usbi_backend.caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER); |
| 2821 |
} |
| 2822 |
return 0; |
| 2823 |
} |
| 2824 |
|
| 2825 |
#ifdef ENABLE_LOGGING |
| 2826 |
|
| 2827 |
/* this is defined in libusbi.h if needed */ |
| 2828 |
#ifdef LIBUSB_PRINTF_WIN32 |
| 2829 |
/* |
| 2830 |
* Prior to VS2015, Microsoft did not provide the snprintf() function and |
| 2831 |
* provided a vsnprintf() that did not guarantee NUL-terminated output. |
| 2832 |
* Microsoft did provide a _snprintf() function, but again it did not |
| 2833 |
* guarantee NULL-terminated output. |
| 2834 |
* |
| 2835 |
* The below implementations guarantee NUL-terminated output and are |
| 2836 |
* C99 compliant. |
| 2837 |
*/ |
| 2838 |
|
| 2839 |
int usbi_snprintf(char *str, size_t size, const char *format, ...) |
| 2840 |
{ |
| 2841 |
va_list args; |
| 2842 |
int ret; |
| 2843 |
|
| 2844 |
va_start(args, format); |
| 2845 |
ret = usbi_vsnprintf(str, size, format, args); |
| 2846 |
va_end(args); |
| 2847 |
|
| 2848 |
return ret; |
| 2849 |
} |
| 2850 |
|
| 2851 |
int usbi_vsnprintf(char *str, size_t size, const char *format, va_list args) |
| 2852 |
{ |
| 2853 |
int ret; |
| 2854 |
|
| 2855 |
ret = _vsnprintf(str, size, format, args); |
| 2856 |
if (ret < 0 || ret == (int)size) { |
| 2857 |
/* Output is truncated, ensure buffer is NUL-terminated and |
| 2858 |
* determine how many characters would have been written. */ |
| 2859 |
str[size - 1] = '\0'; |
| 2860 |
if (ret < 0) |
| 2861 |
ret = _vsnprintf(NULL, 0, format, args); |
| 2862 |
} |
| 2863 |
|
| 2864 |
return ret; |
| 2865 |
} |
| 2866 |
#endif /* LIBUSB_PRINTF_WIN32 */ |
| 2867 |
|
| 2868 |
static void log_str(enum libusb_log_level level, const char *str) |
| 2869 |
{ |
| 2870 |
#if defined(USE_SYSTEM_LOGGING_FACILITY) |
| 2871 |
#if defined(__ANDROID__) |
| 2872 |
int priority; |
| 2873 |
switch (level) { |
| 2874 |
case LIBUSB_LOG_LEVEL_NONE: return; /* Impossible, but keeps compiler happy */ |
| 2875 |
case LIBUSB_LOG_LEVEL_ERROR: priority = ANDROID_LOG_ERROR; break; |
| 2876 |
case LIBUSB_LOG_LEVEL_WARNING: priority = ANDROID_LOG_WARN; break; |
| 2877 |
case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break; |
| 2878 |
case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break; |
| 2879 |
default: priority = ANDROID_LOG_UNKNOWN; |
| 2880 |
} |
| 2881 |
__android_log_write(priority, "libusb", str); |
| 2882 |
#elif defined(_WIN32) |
| 2883 |
UNUSED(level); |
| 2884 |
OutputDebugStringA(str); |
| 2885 |
#elif defined(HAVE_SYSLOG) |
| 2886 |
int syslog_level; |
| 2887 |
switch (level) { |
| 2888 |
case LIBUSB_LOG_LEVEL_NONE: return; /* Impossible, but keeps compiler happy */ |
| 2889 |
case LIBUSB_LOG_LEVEL_ERROR: syslog_level = LOG_ERR; break; |
| 2890 |
case LIBUSB_LOG_LEVEL_WARNING: syslog_level = LOG_WARNING; break; |
| 2891 |
case LIBUSB_LOG_LEVEL_INFO: syslog_level = LOG_INFO; break; |
| 2892 |
case LIBUSB_LOG_LEVEL_DEBUG: syslog_level = LOG_DEBUG; break; |
| 2893 |
default: syslog_level = LOG_INFO; |
| 2894 |
} |
| 2895 |
syslog(syslog_level, "%s", str); |
| 2896 |
#else /* All of gcc, Clang, Xcode seem to use #warning */ |
| 2897 |
#warning System logging is not supported on this platform. Logging to stderr will be used instead. |
| 2898 |
UNUSED(level); |
| 2899 |
fputs(str, stderr); |
| 2900 |
#endif |
| 2901 |
#else |
| 2902 |
/* Global log handler */ |
| 2903 |
if (log_handler) |
| 2904 |
log_handler(NULL, level, str); |
| 2905 |
else |
| 2906 |
fputs(str, stderr); |
| 2907 |
#endif /* USE_SYSTEM_LOGGING_FACILITY */ |
| 2908 |
} |
| 2909 |
|
| 2910 |
static void log_v(struct libusb_context *ctx, enum libusb_log_level level, |
| 2911 |
const char *function, const char *format, va_list args) |
| 2912 |
{ |
| 2913 |
const char *prefix; |
| 2914 |
char buf[USBI_MAX_LOG_LEN]; |
| 2915 |
int global_debug, header_len, text_len; |
| 2916 |
static int has_debug_header_been_displayed = 0; |
| 2917 |
|
| 2918 |
#ifdef ENABLE_DEBUG_LOGGING |
| 2919 |
global_debug = 1; |
| 2920 |
UNUSED(ctx); |
| 2921 |
#else |
| 2922 |
enum libusb_log_level ctx_level; |
| 2923 |
long default_level_value; |
| 2924 |
|
| 2925 |
if (ctx) { |
| 2926 |
ctx_level = ctx->debug; |
| 2927 |
} else { |
| 2928 |
default_level_value = usbi_atomic_load(&default_debug_level); |
| 2929 |
ctx_level = default_level_value < 0 ? get_env_debug_level() : (enum libusb_log_level)default_level_value; |
| 2930 |
} |
| 2931 |
|
| 2932 |
if (ctx_level < level) |
| 2933 |
return; |
| 2934 |
|
| 2935 |
global_debug = (ctx_level == LIBUSB_LOG_LEVEL_DEBUG); |
| 2936 |
#endif |
| 2937 |
|
| 2938 |
switch (level) { |
| 2939 |
case LIBUSB_LOG_LEVEL_NONE: /* Impossible, but keeps compiler happy */ |
| 2940 |
return; |
| 2941 |
case LIBUSB_LOG_LEVEL_ERROR: |
| 2942 |
prefix = "error"; |
| 2943 |
break; |
| 2944 |
case LIBUSB_LOG_LEVEL_WARNING: |
| 2945 |
prefix = "warning"; |
| 2946 |
break; |
| 2947 |
case LIBUSB_LOG_LEVEL_INFO: |
| 2948 |
prefix = "info"; |
| 2949 |
break; |
| 2950 |
case LIBUSB_LOG_LEVEL_DEBUG: |
| 2951 |
prefix = "debug"; |
| 2952 |
break; |
| 2953 |
default: |
| 2954 |
prefix = "unknown"; |
| 2955 |
break; |
| 2956 |
} |
| 2957 |
|
| 2958 |
if (global_debug) { |
| 2959 |
struct timespec timestamp; |
| 2960 |
|
| 2961 |
if (!has_debug_header_been_displayed) { |
| 2962 |
has_debug_header_been_displayed = 1; |
| 2963 |
log_str(LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END); |
| 2964 |
log_str(LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END); |
| 2965 |
} |
| 2966 |
|
| 2967 |
usbi_get_monotonic_time(×tamp); |
| 2968 |
TIMESPEC_SUB(×tamp, ×tamp_origin, ×tamp); |
| 2969 |
|
| 2970 |
header_len = snprintf(buf, sizeof(buf), |
| 2971 |
"[%2ld.%06ld] [%08lx] libusb: %s [%s] ", |
| 2972 |
(long)timestamp.tv_sec, (long)(timestamp.tv_nsec / 1000L), usbi_get_tid(), prefix, function); |
| 2973 |
} else { |
| 2974 |
header_len = snprintf(buf, sizeof(buf), |
| 2975 |
"libusb: %s [%s] ", prefix, function); |
| 2976 |
} |
| 2977 |
|
| 2978 |
if (header_len < 0 || header_len >= (int)sizeof(buf)) { |
| 2979 |
/* Somehow snprintf() failed to write to the buffer, |
| 2980 |
* remove the header so something useful is output. */ |
| 2981 |
header_len = 0; |
| 2982 |
} |
| 2983 |
|
| 2984 |
text_len = vsnprintf(buf + header_len, sizeof(buf) - (size_t)header_len, |
| 2985 |
format, args); |
| 2986 |
if (text_len < 0 || text_len + header_len >= (int)sizeof(buf)) { |
| 2987 |
/* Truncated log output. On some platforms a -1 return value means |
| 2988 |
* that the output was truncated. */ |
| 2989 |
text_len = (int)sizeof(buf) - header_len; |
| 2990 |
} |
| 2991 |
if (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END) >= (int)sizeof(buf)) { |
| 2992 |
/* Need to truncate the text slightly to fit on the terminator. */ |
| 2993 |
text_len -= (header_len + text_len + (int)sizeof(USBI_LOG_LINE_END)) - (int)sizeof(buf); |
| 2994 |
} |
| 2995 |
strcpy(buf + header_len + text_len, USBI_LOG_LINE_END); |
| 2996 |
|
| 2997 |
log_str(level, buf); |
| 2998 |
|
| 2999 |
/* Per-context log handler */ |
| 3000 |
#ifndef ENABLE_DEBUG_LOGGING |
| 3001 |
if (ctx && ctx->log_handler) |
| 3002 |
ctx->log_handler(ctx, level, buf); |
| 3003 |
#endif |
| 3004 |
} |
| 3005 |
|
| 3006 |
void usbi_log(struct libusb_context *ctx, enum libusb_log_level level, |
| 3007 |
const char *function, const char *format, ...) |
| 3008 |
{ |
| 3009 |
va_list args; |
| 3010 |
|
| 3011 |
va_start(args, format); |
| 3012 |
log_v(ctx, level, function, format, args); |
| 3013 |
va_end(args); |
| 3014 |
} |
| 3015 |
|
| 3016 |
#endif /* ENABLE_LOGGING */ |
| 3017 |
|
| 3018 |
/** \ingroup libusb_misc |
| 3019 |
* Returns a constant NULL-terminated string with the ASCII name of a libusb |
| 3020 |
* error or transfer status code. The caller must not free() the returned |
| 3021 |
* string. |
| 3022 |
* |
| 3023 |
* \param error_code The \ref libusb_error or libusb_transfer_status code to |
| 3024 |
* return the name of. |
| 3025 |
* \returns The error name, or the string **UNKNOWN** if the value of |
| 3026 |
* error_code is not a known error / status code. |
| 3027 |
*/ |
| 3028 |
DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code) |
| 3029 |
{ |
| 3030 |
switch (error_code) { |
| 3031 |
case LIBUSB_ERROR_IO: |
| 3032 |
return "LIBUSB_ERROR_IO"; |
| 3033 |
case LIBUSB_ERROR_INVALID_PARAM: |
| 3034 |
return "LIBUSB_ERROR_INVALID_PARAM"; |
| 3035 |
case LIBUSB_ERROR_ACCESS: |
| 3036 |
return "LIBUSB_ERROR_ACCESS"; |
| 3037 |
case LIBUSB_ERROR_NO_DEVICE: |
| 3038 |
return "LIBUSB_ERROR_NO_DEVICE"; |
| 3039 |
case LIBUSB_ERROR_NOT_FOUND: |
| 3040 |
return "LIBUSB_ERROR_NOT_FOUND"; |
| 3041 |
case LIBUSB_ERROR_BUSY: |
| 3042 |
return "LIBUSB_ERROR_BUSY"; |
| 3043 |
case LIBUSB_ERROR_TIMEOUT: |
| 3044 |
return "LIBUSB_ERROR_TIMEOUT"; |
| 3045 |
case LIBUSB_ERROR_OVERFLOW: |
| 3046 |
return "LIBUSB_ERROR_OVERFLOW"; |
| 3047 |
case LIBUSB_ERROR_PIPE: |
| 3048 |
return "LIBUSB_ERROR_PIPE"; |
| 3049 |
case LIBUSB_ERROR_INTERRUPTED: |
| 3050 |
return "LIBUSB_ERROR_INTERRUPTED"; |
| 3051 |
case LIBUSB_ERROR_NO_MEM: |
| 3052 |
return "LIBUSB_ERROR_NO_MEM"; |
| 3053 |
case LIBUSB_ERROR_NOT_SUPPORTED: |
| 3054 |
return "LIBUSB_ERROR_NOT_SUPPORTED"; |
| 3055 |
case LIBUSB_ERROR_OTHER: |
| 3056 |
return "LIBUSB_ERROR_OTHER"; |
| 3057 |
|
| 3058 |
case LIBUSB_TRANSFER_ERROR: |
| 3059 |
return "LIBUSB_TRANSFER_ERROR"; |
| 3060 |
case LIBUSB_TRANSFER_TIMED_OUT: |
| 3061 |
return "LIBUSB_TRANSFER_TIMED_OUT"; |
| 3062 |
case LIBUSB_TRANSFER_CANCELLED: |
| 3063 |
return "LIBUSB_TRANSFER_CANCELLED"; |
| 3064 |
case LIBUSB_TRANSFER_STALL: |
| 3065 |
return "LIBUSB_TRANSFER_STALL"; |
| 3066 |
case LIBUSB_TRANSFER_NO_DEVICE: |
| 3067 |
return "LIBUSB_TRANSFER_NO_DEVICE"; |
| 3068 |
case LIBUSB_TRANSFER_OVERFLOW: |
| 3069 |
return "LIBUSB_TRANSFER_OVERFLOW"; |
| 3070 |
|
| 3071 |
case 0: |
| 3072 |
return "LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLETED"; |
| 3073 |
default: |
| 3074 |
return "**UNKNOWN**"; |
| 3075 |
} |
| 3076 |
} |
| 3077 |
|
| 3078 |
/** \ingroup libusb_misc |
| 3079 |
* Returns a pointer to const struct libusb_version with the version |
| 3080 |
* (major, minor, micro, nano and rc) of the running library. |
| 3081 |
*/ |
| 3082 |
DEFAULT_VISIBILITY |
| 3083 |
const struct libusb_version * LIBUSB_CALL libusb_get_version(void) |
| 3084 |
{ |
| 3085 |
return &libusb_version_internal; |
| 3086 |
} |