| Index: third_party/libusb/src/libusb/core.c
|
| diff --git a/third_party/libusb/src/libusb/core.c b/third_party/libusb/src/libusb/core.c
|
| index 767dcbfabf160c48c0851d61cab7ca7d8483a92a..e29e8df254bbcf2b683ca7fb7ad1a404c7e79ffe 100644
|
| --- a/third_party/libusb/src/libusb/core.c
|
| +++ b/third_party/libusb/src/libusb/core.c
|
| @@ -1,7 +1,9 @@
|
| +/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
|
| /*
|
| - * Core functions for libusb
|
| - * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
|
| - * Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
|
| + * Core functions for libusbx
|
| + * Copyright © 2012-2013 Nathan Hjelm <hjelmn@cs.unm.edu>
|
| + * Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
|
| + * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
|
| *
|
| * This library is free software; you can redistribute it and/or
|
| * modify it under the terms of the GNU Lesser General Public
|
| @@ -18,20 +20,26 @@
|
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
| */
|
|
|
| -#include <config.h>
|
| +#include "config.h"
|
|
|
| #include <errno.h>
|
| #include <stdarg.h>
|
| #include <stdio.h>
|
| #include <stdlib.h>
|
| #include <string.h>
|
| +#ifdef HAVE_SYS_TYPES_H
|
| #include <sys/types.h>
|
| -
|
| +#endif
|
| #ifdef HAVE_SYS_TIME_H
|
| #include <sys/time.h>
|
| #endif
|
|
|
| +#ifdef __ANDROID__
|
| +#include <android/log.h>
|
| +#endif
|
| +
|
| #include "libusbi.h"
|
| +#include "hotplug.h"
|
|
|
| #if defined(OS_LINUX)
|
| const struct usbi_os_backend * const usbi_backend = &linux_usbfs_backend;
|
| @@ -41,38 +49,41 @@ const struct usbi_os_backend * const usbi_backend = &darwin_backend;
|
| const struct usbi_os_backend * const usbi_backend = &openbsd_backend;
|
| #elif defined(OS_WINDOWS)
|
| const struct usbi_os_backend * const usbi_backend = &windows_backend;
|
| +#elif defined(OS_WINCE)
|
| +const struct usbi_os_backend * const usbi_backend = &wince_backend;
|
| #else
|
| #error "Unsupported OS"
|
| #endif
|
|
|
| -const struct libusb_version libusb_version_internal = {
|
| - LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO, LIBUSB_RC,
|
| - LIBUSB_DESCRIBE
|
| -};
|
| -
|
| struct libusb_context *usbi_default_context = NULL;
|
| +const struct libusb_version libusb_version_internal =
|
| + { LIBUSB_MAJOR, LIBUSB_MINOR, LIBUSB_MICRO, LIBUSB_NANO,
|
| + LIBUSB_RC, "http://libusbx.org" };
|
| static int default_context_refcnt = 0;
|
| static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
| +static struct timeval timestamp_origin = { 0, 0 };
|
| +
|
| +usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
|
| +struct list_head active_contexts_list;
|
|
|
| /**
|
| - * \mainpage libusb-1.0 API Reference
|
| + * \mainpage libusbx-1.0 API Reference
|
| *
|
| * \section intro Introduction
|
| *
|
| - * libusb is an open source library that allows you to communicate with USB
|
| + * libusbx is an open source library that allows you to communicate with USB
|
| * devices from userspace. For more info, see the
|
| - * <a href="http://libusb.sourceforge.net">libusb homepage</a>.
|
| + * <a href="http://libusbx.org">libusbx homepage</a>.
|
| *
|
| * This documentation is aimed at application developers wishing to
|
| * communicate with USB peripherals from their own software. After reviewing
|
| * this documentation, feedback and questions can be sent to the
|
| - * <a href="http://sourceforge.net/mail/?group_id=1674">libusb-devel mailing
|
| - * list</a>.
|
| + * <a href="http://mailing-list.libusbx.org">libusbx-devel mailing list</a>.
|
| *
|
| * This documentation assumes knowledge of how to operate USB devices from
|
| * a software standpoint (descriptors, configurations, interfaces, endpoints,
|
| * control/bulk/interrupt/isochronous transfers, etc). Full information
|
| - * can be found in the <a href="http://www.usb.org/developers/docs/">USB 2.0
|
| + * can be found in the <a href="http://www.usb.org/developers/docs/">USB 3.0
|
| * Specification</a> which is available for free download. You can probably
|
| * find less verbose introductions by searching the web.
|
| *
|
| @@ -86,67 +97,71 @@ static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
| * usually won't need to thread)
|
| * - Lightweight with lean API
|
| * - Compatible with libusb-0.1 through the libusb-compat-0.1 translation layer
|
| + * - Hotplug support (on some platforms). See \ref hotplug.
|
| *
|
| * \section gettingstarted Getting Started
|
| *
|
| * To begin reading the API documentation, start with the Modules page which
|
| - * links to the different categories of libusb's functionality.
|
| + * links to the different categories of libusbx's functionality.
|
| *
|
| * One decision you will have to make is whether to use the synchronous
|
| * or the asynchronous data transfer interface. The \ref io documentation
|
| * provides some insight into this topic.
|
| *
|
| - * Some example programs can be found in the libusb source distribution under
|
| - * the "examples" subdirectory. The libusb homepage includes a list of
|
| - * real-life project examples which use libusb.
|
| + * Some example programs can be found in the libusbx source distribution under
|
| + * the "examples" subdirectory. The libusbx homepage includes a list of
|
| + * real-life project examples which use libusbx.
|
| *
|
| * \section errorhandling Error handling
|
| *
|
| - * libusb functions typically return 0 on success or a negative error code
|
| + * libusbx functions typically return 0 on success or a negative error code
|
| * on failure. These negative error codes relate to LIBUSB_ERROR constants
|
| * which are listed on the \ref misc "miscellaneous" documentation page.
|
| *
|
| * \section msglog Debug message logging
|
| *
|
| - * libusb does not log any messages by default. Your application is therefore
|
| - * free to close stdout/stderr and those descriptors may be reused without
|
| - * worry.
|
| + * libusbx uses stderr for all logging. By default, logging is set to NONE,
|
| + * which means that no output will be produced. However, unless the library
|
| + * has been compiled with logging disabled, then any application calls to
|
| + * libusb_set_debug(), or the setting of the environmental variable
|
| + * LIBUSB_DEBUG outside of the application, can result in logging being
|
| + * produced. Your application should therefore not close stderr, but instead
|
| + * direct it to the null device if its output is undesireable.
|
| *
|
| - * The libusb_set_debug() function can be used to enable stdout/stderr logging
|
| - * of certain messages. Under standard configuration, libusb doesn't really
|
| - * log much at all, so you are advised to use this function to enable all
|
| - * error/warning/informational messages. It will help you debug problems with
|
| - * your software.
|
| + * The libusb_set_debug() function can be used to enable logging of certain
|
| + * messages. Under standard configuration, libusbx doesn't really log much
|
| + * so you are advised to use this function to enable all error/warning/
|
| + * informational messages. It will help debug problems with your software.
|
| *
|
| * The logged messages are unstructured. There is no one-to-one correspondence
|
| * between messages being logged and success or failure return codes from
|
| - * libusb functions. There is no format to the messages, so you should not
|
| + * libusbx functions. There is no format to the messages, so you should not
|
| * try to capture or parse them. They are not and will not be localized.
|
| - * These messages are not suitable for being passed to your application user;
|
| - * instead, you should interpret the error codes returned from libusb functions
|
| + * These messages are not intended to being passed to your application user;
|
| + * instead, you should interpret the error codes returned from libusbx functions
|
| * and provide appropriate notification to the user. The messages are simply
|
| * there to aid you as a programmer, and if you're confused because you're
|
| - * getting a strange error code from a libusb function, enabling message
|
| + * getting a strange error code from a libusbx function, enabling message
|
| * logging may give you a suitable explanation.
|
| *
|
| * The LIBUSB_DEBUG environment variable can be used to enable message logging
|
| - * at run-time. This environment variable should be set to a number, which is
|
| - * interpreted the same as the libusb_set_debug() parameter. When this
|
| + * at run-time. This environment variable should be set to a log level number,
|
| + * which is interpreted the same as the libusb_set_debug() parameter. When this
|
| * environment variable is set, the message logging verbosity level is fixed
|
| * and libusb_set_debug() effectively does nothing.
|
| *
|
| - * libusb can be compiled without any logging functions, useful for embedded
|
| + * libusbx can be compiled without any logging functions, useful for embedded
|
| * systems. In this case, libusb_set_debug() and the LIBUSB_DEBUG environment
|
| * variable have no effects.
|
| *
|
| - * libusb can also be compiled with verbose debugging messages. When the
|
| - * library is compiled in this way, all messages of all verbosities are always
|
| - * logged. libusb_set_debug() and the LIBUSB_DEBUG environment variable have
|
| - * no effects.
|
| + * libusbx can also be compiled with verbose debugging messages always. When
|
| + * the library is compiled in this way, all messages of all verbosities are
|
| + * always logged. libusb_set_debug() and the LIBUSB_DEBUG environment variable
|
| + * have no effects.
|
| *
|
| * \section remarks Other remarks
|
| *
|
| - * libusb does have imperfections. The \ref caveats "caveats" page attempts
|
| + * libusbx does have imperfections. The \ref caveats "caveats" page attempts
|
| * to document these.
|
| */
|
|
|
| @@ -161,7 +176,7 @@ static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
| * reset).
|
| *
|
| * The problem is that any other program could reset the device your program
|
| - * is working with, at any time. libusb does not offer a mechanism to inform
|
| + * is working with, at any time. libusbx does not offer a mechanism to inform
|
| * you when this has happened, so if someone else resets your device it will
|
| * not be clear to your own program why the device state has changed.
|
| *
|
| @@ -184,22 +199,9 @@ static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
| * - Clearing of halt/stall condition (libusb_clear_halt())
|
| * - Device resets (libusb_reset_device())
|
| *
|
| - * \section nohotplug No hotplugging
|
| - *
|
| - * libusb-1.0 lacks functionality for providing notifications of when devices
|
| - * are added or removed. This functionality is planned to be implemented
|
| - * for libusb-1.1.
|
| - *
|
| - * That said, there is basic disconnection handling for open device handles:
|
| - * - If there are ongoing transfers, libusb's handle_events loop will detect
|
| - * disconnections and complete ongoing transfers with the
|
| - * LIBUSB_TRANSFER_NO_DEVICE status code.
|
| - * - Many functions such as libusb_set_configuration() return the special
|
| - * LIBUSB_ERROR_NO_DEVICE error code when the device has been disconnected.
|
| - *
|
| * \section configsel Configuration selection and handling
|
| *
|
| - * When libusb presents a device handle to an application, there is a chance
|
| + * When libusbx presents a device handle to an application, there is a chance
|
| * that the corresponding device may be in unconfigured state. For devices
|
| * with multiple configurations, there is also a chance that the configuration
|
| * currently selected is not the one that the application wants to use.
|
| @@ -210,13 +212,13 @@ static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
|
| * -# If the device is already in the desired configuration, calling
|
| * libusb_set_configuration() using the same configuration value will cause
|
| * a lightweight device reset. This may not be desirable behaviour.
|
| - * -# libusb will be unable to change configuration if the device is in
|
| + * -# libusbx will be unable to change configuration if the device is in
|
| * another configuration and other programs or drivers have claimed
|
| * interfaces under that configuration.
|
| - * -# In the case where the desired configuration is already active, libusb
|
| + * -# In the case where the desired configuration is already active, libusbx
|
| * may not even be able to perform a lightweight device reset. For example,
|
| * take my USB keyboard with fingerprint reader: I'm interested in driving
|
| - * the fingerprint reader interface through libusb, but the kernel's
|
| + * the fingerprint reader interface through libusbx, but the kernel's
|
| * USB-HID driver will almost always have claimed the keyboard interface.
|
| * Because the kernel has claimed an interface, it is not even possible to
|
| * perform the lightweight device reset, so libusb_set_configuration() will
|
| @@ -256,50 +258,23 @@ if (cfg != desired)
|
| * considerations apply to Darwin or other platforms.
|
| *
|
| * When a transfer completes early (i.e. when less data is received/sent in
|
| - * any one packet than the transfer buffer allows for) then libusb is designed
|
| + * any one packet than the transfer buffer allows for) then libusbx is designed
|
| * to terminate the transfer immediately, not transferring or receiving any
|
| * more data unless other transfers have been queued by the user.
|
| *
|
| - * On legacy platforms, libusb is unable to do this in all situations. After
|
| - * the incomplete packet occurs, "surplus" data may be transferred. Prior to
|
| - * libusb v1.0.2, this information was lost (and for device-to-host transfers,
|
| - * the corresponding data was discarded). As of libusb v1.0.3, this information
|
| - * is kept (the data length of the transfer is updated) and, for device-to-host
|
| - * transfers, any surplus data was added to the buffer. Still, this is not
|
| - * a nice solution because it loses the information about the end of the short
|
| - * packet, and the user probably wanted that surplus data to arrive in the next
|
| - * logical transfer.
|
| - *
|
| - * A previous workaround was to only ever submit transfers of size 16kb or
|
| - * less.
|
| - *
|
| - * As of libusb v1.0.4 and Linux v2.6.32, this is fixed. A technical
|
| - * explanation of this issue follows.
|
| - *
|
| - * When you ask libusb to submit a bulk transfer larger than 16kb in size,
|
| - * libusb breaks it up into a number of smaller subtransfers. This is because
|
| - * the usbfs kernel interface only accepts transfers of up to 16kb in size.
|
| - * The subtransfers are submitted all at once so that the kernel can queue
|
| - * them at the hardware level, therefore maximizing bus throughput.
|
| - *
|
| - * On legacy platforms, this caused problems when transfers completed early.
|
| - * Upon this event, the kernel would terminate all further packets in that
|
| - * subtransfer (but not any following ones). libusb would note this event and
|
| - * immediately cancel any following subtransfers that had been queued,
|
| - * but often libusb was not fast enough, and the following subtransfers had
|
| - * started before libusb got around to cancelling them.
|
| - *
|
| - * Thanks to an API extension to usbfs, this is fixed with recent kernel and
|
| - * libusb releases. The solution was to allow libusb to communicate to the
|
| - * kernel where boundaries occur between logical libusb-level transfers. When
|
| - * a short transfer (or other error) occurs, the kernel will cancel all the
|
| - * subtransfers until the boundary without allowing those transfers to start.
|
| + * On legacy platforms, libusbx is unable to do this in all situations. After
|
| + * the incomplete packet occurs, "surplus" data may be transferred. For recent
|
| + * versions of libusbx, this information is kept (the data length of the
|
| + * transfer is updated) and, for device-to-host transfers, any surplus data was
|
| + * added to the buffer. Still, this is not a nice solution because it loses the
|
| + * information about the end of the short packet, and the user probably wanted
|
| + * that surplus data to arrive in the next logical transfer.
|
| + *
|
| *
|
| * \section zlp Zero length packets
|
| *
|
| - * - libusb is able to send a packet of zero length to an endpoint simply by
|
| - * submitting a transfer of zero length. On Linux, this did not work with
|
| - * libusb versions prior to 1.0.3 and kernel versions prior to 2.6.31.
|
| + * - libusbx is able to send a packet of zero length to an endpoint simply by
|
| + * submitting a transfer of zero length.
|
| * - The \ref libusb_transfer_flags::LIBUSB_TRANSFER_ADD_ZERO_PACKET
|
| * "LIBUSB_TRANSFER_ADD_ZERO_PACKET" flag is currently only supported on Linux.
|
| */
|
| @@ -307,24 +282,24 @@ if (cfg != desired)
|
| /**
|
| * \page contexts Contexts
|
| *
|
| - * It is possible that libusb may be used simultaneously from two independent
|
| + * It is possible that libusbx may be used simultaneously from two independent
|
| * libraries linked into the same executable. For example, if your application
|
| * has a plugin-like system which allows the user to dynamically load a range
|
| * of modules into your program, it is feasible that two independently
|
| - * developed modules may both use libusb.
|
| + * developed modules may both use libusbx.
|
| *
|
| - * libusb is written to allow for these multiple user scenarios. The two
|
| - * "instances" of libusb will not interfere: libusb_set_debug() calls
|
| + * libusbx is written to allow for these multiple user scenarios. The two
|
| + * "instances" of libusbx will not interfere: libusb_set_debug() calls
|
| * from one user will not affect the same settings for other users, other
|
| - * users can continue using libusb after one of them calls libusb_exit(), etc.
|
| + * users can continue using libusbx after one of them calls libusb_exit(), etc.
|
| *
|
| - * This is made possible through libusb's <em>context</em> concept. When you
|
| + * This is made possible through libusbx's <em>context</em> concept. When you
|
| * call libusb_init(), you are (optionally) given a context. You can then pass
|
| - * this context pointer back into future libusb functions.
|
| + * this context pointer back into future libusbx functions.
|
| *
|
| * In order to keep things simple for more simplistic applications, it is
|
| * legal to pass NULL to all functions requiring a context pointer (as long as
|
| - * you're sure no other code will attempt to use libusb from the same process).
|
| + * you're sure no other code will attempt to use libusbx from the same process).
|
| * When you pass NULL, the default context will be used. The default context
|
| * is created the first time a process calls libusb_init() when no other
|
| * context is alive. Contexts are destroyed during libusb_exit().
|
| @@ -337,17 +312,17 @@ if (cfg != desired)
|
| * reference count goes from 0 to 1, and is deinitialized and destroyed when
|
| * its reference count goes from 1 to 0.
|
| *
|
| - * You may be wondering why only a subset of libusb functions require a
|
| - * context pointer in their function definition. Internally, libusb stores
|
| + * You may be wondering why only a subset of libusbx functions require a
|
| + * context pointer in their function definition. Internally, libusbx stores
|
| * context pointers in other objects (e.g. libusb_device instances) and hence
|
| * can infer the context from those objects.
|
| */
|
|
|
| /**
|
| * @defgroup lib Library initialization/deinitialization
|
| - * This page details how to initialize and deinitialize libusb. Initialization
|
| - * must be performed before using any libusb functionality, and similarly you
|
| - * must not call any libusb functions after deinitialization.
|
| + * This page details how to initialize and deinitialize libusbx. Initialization
|
| + * must be performed before using any libusbx functionality, and similarly you
|
| + * must not call any libusbx functions after deinitialization.
|
| */
|
|
|
| /**
|
| @@ -404,7 +379,7 @@ libusb_free_device_list(list, 1);
|
| * device.
|
| *
|
| * \section devshandles Devices and device handles
|
| - * libusb has a concept of a USB device, represented by the
|
| + * libusbx has a concept of a USB device, represented by the
|
| * \ref libusb_device opaque type. A device represents a USB device that
|
| * is currently or was previously connected to the system. Using a reference
|
| * to a device, you can determine certain information about the device (e.g.
|
| @@ -420,8 +395,8 @@ libusb_free_device_list(list, 1);
|
| * using the device.
|
| *
|
| * When you've found a device that you'd like to operate, you must ask
|
| - * libusb to open the device using the libusb_open() function. Assuming
|
| - * success, libusb then returns you a <em>device handle</em>
|
| + * libusbx to open the device using the libusb_open() function. Assuming
|
| + * success, libusbx then returns you a <em>device handle</em>
|
| * (a \ref libusb_device_handle pointer). All "real" I/O operations then
|
| * operate on the handle rather than the original device pointer.
|
| *
|
| @@ -429,10 +404,10 @@ libusb_free_device_list(list, 1);
|
| *
|
| * Device discovery (i.e. calling libusb_get_device_list()) returns a
|
| * freshly-allocated list of devices. The list itself must be freed when
|
| - * you are done with it. libusb also needs to know when it is OK to free
|
| + * you are done with it. libusbx also needs to know when it is OK to free
|
| * the contents of the list - the devices themselves.
|
| *
|
| - * To handle these issues, libusb provides you with two separate items:
|
| + * To handle these issues, libusbx provides you with two separate items:
|
| * - A function to free the list itself
|
| * - A reference counting system for the devices inside
|
| *
|
| @@ -500,7 +475,7 @@ struct discovered_devs *discovered_devs_append(
|
| /* exceeded capacity, need to grow */
|
| usbi_dbg("need to increase capacity");
|
| capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
|
| - discdevs = realloc(discdevs,
|
| + discdevs = usbi_reallocf(discdevs,
|
| sizeof(*discdevs) + (sizeof(void *) * capacity));
|
| if (discdevs) {
|
| discdevs->capacity = capacity;
|
| @@ -543,12 +518,66 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
|
| dev->refcnt = 1;
|
| dev->session_data = session_id;
|
| dev->speed = LIBUSB_SPEED_UNKNOWN;
|
| - memset(&dev->os_priv, 0, priv_size);
|
| +
|
| + if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
| + usbi_connect_device (dev);
|
| + }
|
| +
|
| + return dev;
|
| +}
|
| +
|
| +void usbi_connect_device(struct libusb_device *dev)
|
| +{
|
| + libusb_hotplug_message message;
|
| + ssize_t ret;
|
| +
|
| + memset(&message, 0, sizeof(message));
|
| + message.event = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED;
|
| + message.device = dev;
|
| + dev->attached = 1;
|
| +
|
| + usbi_mutex_lock(&dev->ctx->usb_devs_lock);
|
| + list_add(&dev->list, &dev->ctx->usb_devs);
|
| + usbi_mutex_unlock(&dev->ctx->usb_devs_lock);
|
| +
|
| + /* Signal that an event has occurred for this device if we support hotplug AND
|
| + * the hotplug pipe is ready. This prevents an event from getting raised during
|
| + * initial enumeration. */
|
| + if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_pipe[1] > 0) {
|
| + ret = usbi_write(dev->ctx->hotplug_pipe[1], &message, sizeof(message));
|
| + if (sizeof (message) != ret) {
|
| + usbi_err(DEVICE_CTX(dev), "error writing hotplug message");
|
| + }
|
| + }
|
| +}
|
| +
|
| +void usbi_disconnect_device(struct libusb_device *dev)
|
| +{
|
| + libusb_hotplug_message message;
|
| + struct libusb_context *ctx = dev->ctx;
|
| + ssize_t ret;
|
| +
|
| + memset(&message, 0, sizeof(message));
|
| + message.event = LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
|
| + message.device = dev;
|
| + usbi_mutex_lock(&dev->lock);
|
| + dev->attached = 0;
|
| + usbi_mutex_unlock(&dev->lock);
|
| +
|
| + /* Signal that an event has occurred for this device if we support hotplug AND
|
| + * the hotplug pipe is ready. This prevents an event from getting raised during
|
| + * initial enumeration. libusb_handle_events will take care of dereferencing the
|
| + * device. */
|
| + if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) && dev->ctx->hotplug_pipe[1] > 0) {
|
| + ret = usbi_write(dev->ctx->hotplug_pipe[1], &message, sizeof(message));
|
| + if (sizeof(message) != ret) {
|
| + usbi_err(DEVICE_CTX(dev), "error writing hotplug message");
|
| + }
|
| + }
|
|
|
| usbi_mutex_lock(&ctx->usb_devs_lock);
|
| - list_add(&dev->list, &ctx->usb_devs);
|
| + list_del(&dev->list);
|
| usbi_mutex_unlock(&ctx->usb_devs_lock);
|
| - return dev;
|
| }
|
|
|
| /* Perform some final sanity checks on a newly discovered device. If this
|
| @@ -557,15 +586,13 @@ struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
|
| int usbi_sanitize_device(struct libusb_device *dev)
|
| {
|
| int r;
|
| - unsigned char raw_desc[DEVICE_DESC_LENGTH];
|
| uint8_t num_configurations;
|
| - int host_endian;
|
|
|
| - r = usbi_backend->get_device_descriptor(dev, raw_desc, &host_endian);
|
| + r = usbi_device_cache_descriptor(dev);
|
| if (r < 0)
|
| return r;
|
|
|
| - num_configurations = raw_desc[DEVICE_DESC_LENGTH - 1];
|
| + num_configurations = dev->device_descriptor.bNumConfigurations;
|
| if (num_configurations > USB_MAXCONFIG) {
|
| usbi_err(DEVICE_CTX(dev), "too many configurations");
|
| return LIBUSB_ERROR_IO;
|
| @@ -576,7 +603,7 @@ int usbi_sanitize_device(struct libusb_device *dev)
|
| return 0;
|
| }
|
|
|
| -/* Examine libusb's internal list of known devices, looking for one with
|
| +/* Examine libusbx's internal list of known devices, looking for one with
|
| * a specific session ID. Returns the matching device if it was found, and
|
| * NULL otherwise. */
|
| struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
|
| @@ -613,7 +640,7 @@ struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
|
| * \param ctx the context to operate on, or NULL for the default context
|
| * \param list output location for a list of devices. Must be later freed with
|
| * libusb_free_device_list().
|
| - * \returns The number of devices in the outputted list, or any
|
| + * \returns the number of devices in the outputted list, or any
|
| * \ref libusb_error according to errors encountered by the backend.
|
| */
|
| ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
| @@ -629,7 +656,28 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
| if (!discdevs)
|
| return LIBUSB_ERROR_NO_MEM;
|
|
|
| - r = usbi_backend->get_device_list(ctx, &discdevs);
|
| + if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
| + /* backend provides hotplug support */
|
| + struct libusb_device *dev;
|
| +
|
| + if (usbi_backend->hotplug_poll)
|
| + usbi_backend->hotplug_poll();
|
| +
|
| + usbi_mutex_lock(&ctx->usb_devs_lock);
|
| + list_for_each_entry(dev, &ctx->usb_devs, list, struct libusb_device) {
|
| + discdevs = discovered_devs_append(discdevs, dev);
|
| +
|
| + if (!discdevs) {
|
| + r = LIBUSB_ERROR_NO_MEM;
|
| + break;
|
| + }
|
| + }
|
| + usbi_mutex_unlock(&ctx->usb_devs_lock);
|
| + } else {
|
| + /* backend does not provide hotplug support */
|
| + r = usbi_backend->get_device_list(ctx, &discdevs);
|
| + }
|
| +
|
| if (r < 0) {
|
| len = r;
|
| goto out;
|
| @@ -637,7 +685,7 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
|
|
|
| /* convert discovered_devs into a list */
|
| len = discdevs->len;
|
| - ret = malloc(sizeof(void *) * (len + 1));
|
| + ret = calloc(len + 1, sizeof(struct libusb_device *));
|
| if (!ret) {
|
| len = LIBUSB_ERROR_NO_MEM;
|
| goto out;
|
| @@ -689,6 +737,87 @@ uint8_t API_EXPORTED libusb_get_bus_number(libusb_device *dev)
|
| }
|
|
|
| /** \ingroup dev
|
| + * Get the number of the port that a device is connected to.
|
| + * Unless the OS does something funky, or you are hot-plugging USB extension cards,
|
| + * the port number returned by this call is usually guaranteed to be uniquely tied
|
| + * to a physical port, meaning that different devices plugged on the same physical
|
| + * port should return the same port number.
|
| + *
|
| + * But outside of this, there is no guarantee that the port number returned by this
|
| + * call will remain the same, or even match the order in which ports have been
|
| + * numbered by the HUB/HCD manufacturer.
|
| + *
|
| + * \param dev a device
|
| + * \returns the port number (0 if not available)
|
| + */
|
| +uint8_t API_EXPORTED libusb_get_port_number(libusb_device *dev)
|
| +{
|
| + return dev->port_number;
|
| +}
|
| +
|
| +/** \ingroup dev
|
| + * Get the list of all port numbers from root for the specified device
|
| + *
|
| + * Since version 1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102
|
| + * \param dev a device
|
| + * \param port_numbers the array that should contain the port numbers
|
| + * \param port_numbers_len the maximum length of the array. As per the USB 3.0
|
| + * specs, the current maximum limit for the depth is 7.
|
| + * \returns the number of elements filled
|
| + * \returns LIBUSB_ERROR_OVERFLOW if the array is too small
|
| + */
|
| +int API_EXPORTED libusb_get_port_numbers(libusb_device *dev,
|
| + uint8_t* port_numbers, int port_numbers_len)
|
| +{
|
| + int i = port_numbers_len;
|
| +
|
| + while(dev) {
|
| + // HCDs can be listed as devices and would have port #0
|
| + // TODO: see how the other backends want to implement HCDs as parents
|
| + if (dev->port_number == 0)
|
| + break;
|
| + i--;
|
| + if (i < 0) {
|
| + usbi_warn(DEVICE_CTX(dev),
|
| + "port numbers array too small");
|
| + return LIBUSB_ERROR_OVERFLOW;
|
| + }
|
| + port_numbers[i] = dev->port_number;
|
| + dev = dev->parent_dev;
|
| + }
|
| + memmove(port_numbers, &port_numbers[i], port_numbers_len - i);
|
| + return port_numbers_len - i;
|
| +}
|
| +
|
| +/** \ingroup dev
|
| + * Deprecated please use libusb_get_port_numbers instead.
|
| + */
|
| +int API_EXPORTED libusb_get_port_path(libusb_context *ctx, libusb_device *dev,
|
| + uint8_t* port_numbers, uint8_t port_numbers_len)
|
| +{
|
| + UNUSED(ctx);
|
| +
|
| + return libusb_get_port_numbers(dev, port_numbers, port_numbers_len);
|
| +}
|
| +
|
| +/** \ingroup dev
|
| + * Get the the parent from the specified device.
|
| + * \param dev a device
|
| + * \returns the device parent or NULL if not available
|
| + * You should issue a \ref libusb_get_device_list() before calling this
|
| + * function and make sure that you only access the parent before issuing
|
| + * \ref libusb_free_device_list(). The reason is that libusbx currently does
|
| + * not maintain a permanent list of device instances, and therefore can
|
| + * only guarantee that parents are fully instantiated within a
|
| + * libusb_get_device_list() - libusb_free_device_list() block.
|
| + */
|
| +DEFAULT_VISIBILITY
|
| +libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev)
|
| +{
|
| + return dev->parent_dev;
|
| +}
|
| +
|
| +/** \ingroup dev
|
| * Get the address of the device on the bus it is connected to.
|
| * \param dev a device
|
| * \returns the device address
|
| @@ -777,7 +906,7 @@ int API_EXPORTED libusb_get_max_packet_size(libusb_device *dev,
|
| * Calculate the maximum packet size which a specific endpoint is capable is
|
| * sending or receiving in the duration of 1 microframe
|
| *
|
| - * Only the active configution is examined. The calculation is based on the
|
| + * Only the active configuration is examined. The calculation is based on the
|
| * wMaxPacketSize field in the endpoint descriptor as described in section
|
| * 9.6.6 in the USB 2.0 specifications.
|
| *
|
| @@ -820,7 +949,7 @@ int API_EXPORTED libusb_get_max_iso_packet_size(libusb_device *dev,
|
| return LIBUSB_ERROR_NOT_FOUND;
|
|
|
| val = ep->wMaxPacketSize;
|
| - ep_type = ep->bmAttributes & 0x3;
|
| + ep_type = (enum libusb_transfer_type) (ep->bmAttributes & 0x3);
|
| libusb_free_config_descriptor(config);
|
|
|
| r = val & 0x07ff;
|
| @@ -863,12 +992,15 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev)
|
| if (refcnt == 0) {
|
| usbi_dbg("destroy device %d.%d", dev->bus_number, dev->device_address);
|
|
|
| + libusb_unref_device(dev->parent_dev);
|
| +
|
| if (usbi_backend->destroy_device)
|
| usbi_backend->destroy_device(dev);
|
|
|
| - usbi_mutex_lock(&dev->ctx->usb_devs_lock);
|
| - list_del(&dev->list);
|
| - usbi_mutex_unlock(&dev->ctx->usb_devs_lock);
|
| + if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
| + /* backend does not support hotplug */
|
| + usbi_disconnect_device(dev);
|
| + }
|
|
|
| usbi_mutex_destroy(&dev->lock);
|
| free(dev);
|
| @@ -947,6 +1079,10 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
| int r;
|
| usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
|
|
|
| + if (!dev->attached) {
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| + }
|
| +
|
| _handle = malloc(sizeof(*_handle) + priv_size);
|
| if (!_handle)
|
| return LIBUSB_ERROR_NO_MEM;
|
| @@ -958,6 +1094,7 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
| }
|
|
|
| _handle->dev = libusb_ref_device(dev);
|
| + _handle->auto_detach_kernel_driver = 0;
|
| _handle->claimed_interfaces = 0;
|
| memset(&_handle->os_priv, 0, priv_size);
|
|
|
| @@ -978,7 +1115,7 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
| /* At this point, we want to interrupt any existing event handlers so
|
| * that they realise the addition of the new device's poll fd. One
|
| * example when this is desirable is if the user is running a separate
|
| - * dedicated libusb events handling thread, which is running with a long
|
| + * dedicated libusbx events handling thread, which is running with a long
|
| * or infinite timeout. We want to interrupt that iteration of the loop,
|
| * so that it picks up the new fd, and then continues. */
|
| usbi_fd_notification(ctx);
|
| @@ -989,7 +1126,7 @@ int API_EXPORTED libusb_open(libusb_device *dev,
|
| /** \ingroup dev
|
| * Convenience function for finding a device with a particular
|
| * <tt>idVendor</tt>/<tt>idProduct</tt> combination. This function is intended
|
| - * for those scenarios where you are using libusb to knock up a quick test
|
| + * for those scenarios where you are using libusbx to knock up a quick test
|
| * application - it allows you to avoid calling libusb_get_device_list() and
|
| * worrying about traversing/freeing the list.
|
| *
|
| @@ -1052,7 +1189,7 @@ static void do_close(struct libusb_context *ctx,
|
| /* safe iteration because transfers may be being deleted */
|
| list_for_each_entry_safe(itransfer, tmp, &ctx->flying_transfers, list, struct usbi_transfer) {
|
| struct libusb_transfer *transfer =
|
| - USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
| + USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
|
|
|
| if (transfer->dev_handle != dev_handle)
|
| continue;
|
| @@ -1240,7 +1377,14 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
|
| * endpoint halts cleared, toggles reset).
|
| *
|
| * You cannot change/reset configuration if your application has claimed
|
| - * interfaces - you should free them with libusb_release_interface() first.
|
| + * interfaces. It is advised to set the desired configuration before claiming
|
| + * interfaces.
|
| + *
|
| + * Alternatively you can call libusb_release_interface() first. Note if you
|
| + * do things this way you must ensure that auto_detach_kernel_driver for
|
| + * <tt>dev</tt> is 0, otherwise the kernel driver will be re-attached when you
|
| + * release the interface(s).
|
| + *
|
| * You cannot change/reset configuration if other applications or drivers have
|
| * claimed interfaces.
|
| *
|
| @@ -1262,6 +1406,7 @@ int API_EXPORTED libusb_get_configuration(libusb_device_handle *dev,
|
| * \returns LIBUSB_ERROR_BUSY if interfaces are currently claimed
|
| * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
| * \returns another LIBUSB_ERROR code on other failure
|
| + * \see libusb_set_auto_detach_kernel_driver()
|
| */
|
| int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
|
| int configuration)
|
| @@ -1275,7 +1420,10 @@ int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
|
| * you wish to use before you can perform I/O on any of its endpoints.
|
| *
|
| * It is legal to attempt to claim an already-claimed interface, in which
|
| - * case libusb just returns 0 without doing anything.
|
| + * case libusbx just returns 0 without doing anything.
|
| + *
|
| + * If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel driver
|
| + * will be detached if necessary, on failure the detach error is returned.
|
| *
|
| * Claiming of interfaces is a purely logical operation; it does not cause
|
| * any requests to be sent over the bus. Interface claiming is used to
|
| @@ -1293,6 +1441,7 @@ int API_EXPORTED libusb_set_configuration(libusb_device_handle *dev,
|
| * interface
|
| * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
| * \returns a LIBUSB_ERROR code on other failure
|
| + * \see libusb_set_auto_detach_kernel_driver()
|
| */
|
| int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev,
|
| int interface_number)
|
| @@ -1303,6 +1452,9 @@ int API_EXPORTED libusb_claim_interface(libusb_device_handle *dev,
|
| if (interface_number >= USB_MAXINTERFACES)
|
| return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| usbi_mutex_lock(&dev->lock);
|
| if (dev->claimed_interfaces & (1 << interface_number))
|
| goto out;
|
| @@ -1323,6 +1475,9 @@ out:
|
| * This is a blocking function. A SET_INTERFACE control request will be sent
|
| * to the device, resetting interface state to the first alternate setting.
|
| *
|
| + * If auto_detach_kernel_driver is set to 1 for <tt>dev</tt>, the kernel
|
| + * driver will be re-attached after releasing the interface.
|
| + *
|
| * \param dev a device handle
|
| * \param interface_number the <tt>bInterfaceNumber</tt> of the
|
| * previously-claimed interface
|
| @@ -1330,6 +1485,7 @@ out:
|
| * \returns LIBUSB_ERROR_NOT_FOUND if the interface was not claimed
|
| * \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
|
| * \returns another LIBUSB_ERROR code on other failure
|
| + * \see libusb_set_auto_detach_kernel_driver()
|
| */
|
| int API_EXPORTED libusb_release_interface(libusb_device_handle *dev,
|
| int interface_number)
|
| @@ -1385,6 +1541,11 @@ int API_EXPORTED libusb_set_interface_alt_setting(libusb_device_handle *dev,
|
| return LIBUSB_ERROR_INVALID_PARAM;
|
|
|
| usbi_mutex_lock(&dev->lock);
|
| + if (!dev->dev->attached) {
|
| + usbi_mutex_unlock(&dev->lock);
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| + }
|
| +
|
| if (!(dev->claimed_interfaces & (1 << interface_number))) {
|
| usbi_mutex_unlock(&dev->lock);
|
| return LIBUSB_ERROR_NOT_FOUND;
|
| @@ -1415,6 +1576,9 @@ int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev,
|
| unsigned char endpoint)
|
| {
|
| usbi_dbg("endpoint %x", endpoint);
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| return usbi_backend->clear_halt(dev, endpoint);
|
| }
|
|
|
| @@ -1440,12 +1604,15 @@ int API_EXPORTED libusb_clear_halt(libusb_device_handle *dev,
|
| int API_EXPORTED libusb_reset_device(libusb_device_handle *dev)
|
| {
|
| usbi_dbg("");
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| return usbi_backend->reset_device(dev);
|
| }
|
|
|
| /** \ingroup dev
|
| * Determine if a kernel driver is active on an interface. If a kernel driver
|
| - * is active, you cannot claim the interface, and libusb will be unable to
|
| + * is active, you cannot claim the interface, and libusbx will be unable to
|
| * perform I/O.
|
| *
|
| * This functionality is not available on Windows.
|
| @@ -1464,6 +1631,10 @@ int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev,
|
| int interface_number)
|
| {
|
| usbi_dbg("interface %d", interface_number);
|
| +
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| if (usbi_backend->kernel_driver_active)
|
| return usbi_backend->kernel_driver_active(dev, interface_number);
|
| else
|
| @@ -1476,6 +1647,10 @@ int API_EXPORTED libusb_kernel_driver_active(libusb_device_handle *dev,
|
| *
|
| * This functionality is not available on Darwin or Windows.
|
| *
|
| + * Note that libusbx itself also talks to the device through a special kernel
|
| + * driver, if this driver is already attached to the device, this call will
|
| + * not detach it and return LIBUSB_ERROR_NOT_FOUND.
|
| + *
|
| * \param dev a device handle
|
| * \param interface_number the interface to detach the driver from
|
| * \returns 0 on success
|
| @@ -1491,6 +1666,10 @@ int API_EXPORTED libusb_detach_kernel_driver(libusb_device_handle *dev,
|
| int interface_number)
|
| {
|
| usbi_dbg("interface %d", interface_number);
|
| +
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| if (usbi_backend->detach_kernel_driver)
|
| return usbi_backend->detach_kernel_driver(dev, interface_number);
|
| else
|
| @@ -1521,36 +1700,68 @@ int API_EXPORTED libusb_attach_kernel_driver(libusb_device_handle *dev,
|
| int interface_number)
|
| {
|
| usbi_dbg("interface %d", interface_number);
|
| +
|
| + if (!dev->dev->attached)
|
| + return LIBUSB_ERROR_NO_DEVICE;
|
| +
|
| if (usbi_backend->attach_kernel_driver)
|
| return usbi_backend->attach_kernel_driver(dev, interface_number);
|
| else
|
| return LIBUSB_ERROR_NOT_SUPPORTED;
|
| }
|
|
|
| +/** \ingroup dev
|
| + * Enable/disable libusbx's automatic kernel driver detachment. When this is
|
| + * enabled libusbx will automatically detach the kernel driver on an interface
|
| + * when claiming the interface, and attach it when releasing the interface.
|
| + *
|
| + * Automatic kernel driver detachment is disabled on newly opened device
|
| + * handles by default.
|
| + *
|
| + * On platforms which do not have LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER
|
| + * this function will return LIBUSB_ERROR_NOT_SUPPORTED, and libusbx will
|
| + * continue as if this function was never called.
|
| + *
|
| + * \param dev a device handle
|
| + * \param enable whether to enable or disable auto kernel driver detachment
|
| + *
|
| + * \returns LIBUSB_SUCCESS on success
|
| + * \returns LIBUSB_ERROR_NOT_SUPPORTED on platforms where the functionality
|
| + * is not available
|
| + * \see libusb_claim_interface()
|
| + * \see libusb_release_interface()
|
| + * \see libusb_set_configuration()
|
| + */
|
| +int API_EXPORTED libusb_set_auto_detach_kernel_driver(
|
| + libusb_device_handle *dev, int enable)
|
| +{
|
| + if (!(usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER))
|
| + return LIBUSB_ERROR_NOT_SUPPORTED;
|
| +
|
| + dev->auto_detach_kernel_driver = enable;
|
| + return LIBUSB_SUCCESS;
|
| +}
|
| +
|
| /** \ingroup lib
|
| - * Set message verbosity.
|
| - * - Level 0: no messages ever printed by the library (default)
|
| - * - Level 1: error messages are printed to stderr
|
| - * - Level 2: warning and error messages are printed to stderr
|
| - * - Level 3: informational messages are printed to stdout, warning and error
|
| - * messages are printed to stderr
|
| - *
|
| - * The default level is 0, which means no messages are ever printed. If you
|
| - * choose to increase the message verbosity level, ensure that your
|
| - * application does not close the stdout/stderr file descriptors.
|
| - *
|
| - * You are advised to set level 3. libusb is conservative with its message
|
| - * logging and most of the time, will only log messages that explain error
|
| - * conditions and other oddities. This will help you debug your software.
|
| - *
|
| - * If the LIBUSB_DEBUG environment variable was set when libusb was
|
| + * Set log message verbosity.
|
| + *
|
| + * The default level is LIBUSB_LOG_LEVEL_NONE, which means no messages are ever
|
| + * printed. If you choose to increase the message verbosity level, ensure
|
| + * that your application does not close the stdout/stderr file descriptors.
|
| + *
|
| + * You are advised to use level LIBUSB_LOG_LEVEL_WARNING. libusbx is conservative
|
| + * with its message logging and most of the time, will only log messages that
|
| + * explain error conditions and other oddities. This will help you debug
|
| + * your software.
|
| + *
|
| + * If the LIBUSB_DEBUG environment variable was set when libusbx was
|
| * initialized, this function does nothing: the message verbosity is fixed
|
| * to the value in the environment variable.
|
| *
|
| - * If libusb was compiled without any message logging, this function does
|
| + * If libusbx was compiled without any message logging, this function does
|
| * nothing: you'll never get any messages.
|
| *
|
| - * If libusb was compiled with verbose debug message logging, this function
|
| + * If libusbx was compiled with verbose debug message logging, this function
|
| * does nothing: you'll always get messages from all levels.
|
| *
|
| * \param ctx the context to operate on, or NULL for the default context
|
| @@ -1565,7 +1776,7 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
|
|
|
| /** \ingroup lib
|
| * Initialize libusb. This function must be called before calling any other
|
| - * libusb function.
|
| + * libusbx function.
|
| *
|
| * If you do not provide an output location for a context pointer, a default
|
| * context will be created. If there was already a default context, it will
|
| @@ -1578,11 +1789,18 @@ void API_EXPORTED libusb_set_debug(libusb_context *ctx, int level)
|
| */
|
| int API_EXPORTED libusb_init(libusb_context **context)
|
| {
|
| + struct libusb_device *dev, *next;
|
| char *dbg = getenv("LIBUSB_DEBUG");
|
| struct libusb_context *ctx;
|
| + static int first_init = 1;
|
| int r = 0;
|
|
|
| usbi_mutex_static_lock(&default_context_lock);
|
| +
|
| + if (!timestamp_origin.tv_sec) {
|
| + usbi_gettimeofday(×tamp_origin, NULL);
|
| + }
|
| +
|
| if (!context && usbi_default_context) {
|
| usbi_dbg("reusing default context");
|
| default_context_refcnt++;
|
| @@ -1590,12 +1808,15 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
| return 0;
|
| }
|
|
|
| - ctx = malloc(sizeof(*ctx));
|
| + ctx = calloc(1, sizeof(*ctx));
|
| if (!ctx) {
|
| r = LIBUSB_ERROR_NO_MEM;
|
| goto err_unlock;
|
| }
|
| - memset(ctx, 0, sizeof(*ctx));
|
| +
|
| +#ifdef ENABLE_DEBUG_LOGGING
|
| + ctx->debug = LIBUSB_LOG_LEVEL_DEBUG;
|
| +#endif
|
|
|
| if (dbg) {
|
| ctx->debug = atoi(dbg);
|
| @@ -1603,47 +1824,70 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
| ctx->debug_fixed = 1;
|
| }
|
|
|
| - usbi_dbg("libusb-%d.%d.%d%s%s%s",
|
| - libusb_version_internal.major,
|
| - libusb_version_internal.minor,
|
| - libusb_version_internal.micro,
|
| - libusb_version_internal.rc,
|
| - libusb_version_internal.describe[0] ? " git:" : "",
|
| - libusb_version_internal.describe);
|
| -
|
| - if (usbi_backend->init) {
|
| - r = usbi_backend->init(ctx);
|
| - if (r)
|
| - goto err_free_ctx;
|
| + /* default context should be initialized before calling usbi_dbg */
|
| + if (!usbi_default_context) {
|
| + usbi_default_context = ctx;
|
| + default_context_refcnt++;
|
| + usbi_dbg("created default context");
|
| }
|
|
|
| + usbi_dbg("libusbx v%d.%d.%d.%d", libusb_version_internal.major, libusb_version_internal.minor,
|
| + libusb_version_internal.micro, libusb_version_internal.nano);
|
| +
|
| usbi_mutex_init(&ctx->usb_devs_lock, NULL);
|
| usbi_mutex_init(&ctx->open_devs_lock, NULL);
|
| + usbi_mutex_init(&ctx->hotplug_cbs_lock, NULL);
|
| list_init(&ctx->usb_devs);
|
| list_init(&ctx->open_devs);
|
| + list_init(&ctx->hotplug_cbs);
|
|
|
| - r = usbi_io_init(ctx);
|
| - if (r < 0) {
|
| - if (usbi_backend->exit)
|
| - usbi_backend->exit();
|
| - goto err_destroy_mutex;
|
| + usbi_mutex_static_lock(&active_contexts_lock);
|
| + if (first_init) {
|
| + first_init = 0;
|
| + list_init (&active_contexts_list);
|
| }
|
| + list_add (&ctx->list, &active_contexts_list);
|
| + usbi_mutex_static_unlock(&active_contexts_lock);
|
|
|
| - if (context) {
|
| - *context = ctx;
|
| - } else if (!usbi_default_context) {
|
| - usbi_dbg("created default context");
|
| - usbi_default_context = ctx;
|
| - default_context_refcnt++;
|
| + if (usbi_backend->init) {
|
| + r = usbi_backend->init(ctx);
|
| + if (r)
|
| + goto err_free_ctx;
|
| }
|
| +
|
| + r = usbi_io_init(ctx);
|
| + if (r < 0)
|
| + goto err_backend_exit;
|
| +
|
| usbi_mutex_static_unlock(&default_context_lock);
|
|
|
| + if (context)
|
| + *context = ctx;
|
| +
|
| return 0;
|
|
|
| -err_destroy_mutex:
|
| +err_backend_exit:
|
| + if (usbi_backend->exit)
|
| + usbi_backend->exit();
|
| +err_free_ctx:
|
| + if (ctx == usbi_default_context)
|
| + usbi_default_context = NULL;
|
| +
|
| usbi_mutex_destroy(&ctx->open_devs_lock);
|
| usbi_mutex_destroy(&ctx->usb_devs_lock);
|
| -err_free_ctx:
|
| + usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
|
| +
|
| + usbi_mutex_static_lock(&active_contexts_lock);
|
| + list_del (&ctx->list);
|
| + usbi_mutex_static_unlock(&active_contexts_lock);
|
| +
|
| + usbi_mutex_lock(&ctx->usb_devs_lock);
|
| + list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
|
| + list_del(&dev->list);
|
| + libusb_unref_device(dev);
|
| + }
|
| + usbi_mutex_unlock(&ctx->usb_devs_lock);
|
| +
|
| free(ctx);
|
| err_unlock:
|
| usbi_mutex_static_unlock(&default_context_lock);
|
| @@ -1657,13 +1901,15 @@ err_unlock:
|
| */
|
| void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
| {
|
| + struct libusb_device *dev, *next;
|
| +
|
| usbi_dbg("");
|
| USBI_GET_CONTEXT(ctx);
|
|
|
| /* if working with default context, only actually do the deinitialization
|
| * if we're the last user */
|
| + usbi_mutex_static_lock(&default_context_lock);
|
| if (ctx == usbi_default_context) {
|
| - usbi_mutex_static_lock(&default_context_lock);
|
| if (--default_context_refcnt > 0) {
|
| usbi_dbg("not destroying default context");
|
| usbi_mutex_static_unlock(&default_context_lock);
|
| @@ -1671,11 +1917,27 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
| }
|
| usbi_dbg("destroying default context");
|
| usbi_default_context = NULL;
|
| - usbi_mutex_static_unlock(&default_context_lock);
|
| }
|
| + usbi_mutex_static_unlock(&default_context_lock);
|
|
|
| - /* a little sanity check. doesn't bother with open_devs locking because
|
| - * unless there is an application bug, nobody will be accessing this. */
|
| + usbi_mutex_static_lock(&active_contexts_lock);
|
| + list_del (&ctx->list);
|
| + usbi_mutex_static_unlock(&active_contexts_lock);
|
| +
|
| + if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
| + usbi_hotplug_deregister_all(ctx);
|
| + usbi_mutex_lock(&ctx->usb_devs_lock);
|
| + list_for_each_entry_safe(dev, next, &ctx->usb_devs, list, struct libusb_device) {
|
| + list_del(&dev->list);
|
| + libusb_unref_device(dev);
|
| + }
|
| + usbi_mutex_unlock(&ctx->usb_devs_lock);
|
| + }
|
| +
|
| + /* a few sanity checks. don't bother with locking because unless
|
| + * there is an application bug, nobody will be accessing these. */
|
| + if (!list_empty(&ctx->usb_devs))
|
| + usbi_warn(ctx, "some libusb_devices were leaked");
|
| if (!list_empty(&ctx->open_devs))
|
| usbi_warn(ctx, "application left some devices open");
|
|
|
| @@ -1685,21 +1947,29 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
|
|
| usbi_mutex_destroy(&ctx->open_devs_lock);
|
| usbi_mutex_destroy(&ctx->usb_devs_lock);
|
| + usbi_mutex_destroy(&ctx->hotplug_cbs_lock);
|
| free(ctx);
|
| }
|
|
|
| /** \ingroup misc
|
| * Check at runtime if the loaded library has a given capability.
|
| + * This call should be performed after \ref libusb_init(), to ensure the
|
| + * backend has updated its capability set.
|
| *
|
| * \param capability the \ref libusb_capability to check for
|
| - * \returns 1 if the running library has the capability, 0 otherwise
|
| + * \returns nonzero if the running library has the capability, 0 otherwise
|
| */
|
| int API_EXPORTED libusb_has_capability(uint32_t capability)
|
| {
|
| - enum libusb_capability cap = capability;
|
| - switch (cap) {
|
| + switch (capability) {
|
| case LIBUSB_CAP_HAS_CAPABILITY:
|
| return 1;
|
| + case LIBUSB_CAP_HAS_HOTPLUG:
|
| + return !(usbi_backend->get_device_list);
|
| + case LIBUSB_CAP_HAS_HID_ACCESS:
|
| + return (usbi_backend->caps & USBI_CAP_HAS_HID_ACCESS);
|
| + case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
|
| + return (usbi_backend->caps & USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER);
|
| }
|
| return 0;
|
| }
|
| @@ -1732,85 +2002,152 @@ int API_EXPORTED libusb_has_capability(uint32_t capability)
|
| #define _W32_FT_OFFSET (116444736000000000)
|
|
|
| int usbi_gettimeofday(struct timeval *tp, void *tzp)
|
| - {
|
| - union {
|
| - unsigned __int64 ns100; /*time since 1 Jan 1601 in 100ns units */
|
| - FILETIME ft;
|
| - } _now;
|
| -
|
| - if(tp)
|
| - {
|
| - GetSystemTimeAsFileTime (&_now.ft);
|
| - tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
|
| - tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
|
| - }
|
| - /* Always return 0 as per Open Group Base Specifications Issue 6.
|
| - Do not set errno on error. */
|
| - return 0;
|
| +{
|
| + union {
|
| + unsigned __int64 ns100; /* Time since 1 Jan 1601, in 100ns units */
|
| + FILETIME ft;
|
| + } _now;
|
| + UNUSED(tzp);
|
| +
|
| + if(tp) {
|
| +#if defined(OS_WINCE)
|
| + SYSTEMTIME st;
|
| + GetSystemTime(&st);
|
| + SystemTimeToFileTime(&st, &_now.ft);
|
| +#else
|
| + GetSystemTimeAsFileTime (&_now.ft);
|
| +#endif
|
| + tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
|
| + tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
|
| + }
|
| + /* Always return 0 as per Open Group Base Specifications Issue 6.
|
| + Do not set errno on error. */
|
| + return 0;
|
| }
|
| #endif
|
|
|
| -void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
|
| +static void usbi_log_str(struct libusb_context *ctx, const char * str)
|
| +{
|
| + UNUSED(ctx);
|
| + fputs(str, stderr);
|
| +}
|
| +
|
| +void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
|
| const char *function, const char *format, va_list args)
|
| {
|
| - FILE *stream = stdout;
|
| - const char *prefix;
|
| + const char *prefix = "";
|
| + char buf[USBI_MAX_LOG_LEN];
|
| struct timeval now;
|
| - static struct timeval first = { 0, 0 };
|
| + int global_debug, header_len, text_len;
|
| + static int has_debug_header_been_displayed = 0;
|
|
|
| -#ifndef ENABLE_DEBUG_LOGGING
|
| +#ifdef ENABLE_DEBUG_LOGGING
|
| + global_debug = 1;
|
| + UNUSED(ctx);
|
| +#else
|
| USBI_GET_CONTEXT(ctx);
|
| + if (ctx == NULL)
|
| + return;
|
| + global_debug = (ctx->debug == LIBUSB_LOG_LEVEL_DEBUG);
|
| if (!ctx->debug)
|
| return;
|
| - if (level == LOG_LEVEL_WARNING && ctx->debug < 2)
|
| + if (level == LIBUSB_LOG_LEVEL_WARNING && ctx->debug < LIBUSB_LOG_LEVEL_WARNING)
|
| + return;
|
| + if (level == LIBUSB_LOG_LEVEL_INFO && ctx->debug < LIBUSB_LOG_LEVEL_INFO)
|
| return;
|
| - if (level == LOG_LEVEL_INFO && ctx->debug < 3)
|
| + if (level == LIBUSB_LOG_LEVEL_DEBUG && ctx->debug < LIBUSB_LOG_LEVEL_DEBUG)
|
| return;
|
| #endif
|
|
|
| +#ifdef __ANDROID__
|
| + int prio;
|
| + switch (level) {
|
| + case LOG_LEVEL_INFO:
|
| + prio = ANDROID_LOG_INFO;
|
| + break;
|
| + case LOG_LEVEL_WARNING:
|
| + prio = ANDROID_LOG_WARN;
|
| + break;
|
| + case LOG_LEVEL_ERROR:
|
| + prio = ANDROID_LOG_ERROR;
|
| + break;
|
| + case LOG_LEVEL_DEBUG:
|
| + prio = ANDROID_LOG_DEBUG;
|
| + break;
|
| + default:
|
| + prio = ANDROID_LOG_UNKNOWN;
|
| + break;
|
| + }
|
| +
|
| + __android_log_vprint(prio, "LibUsb", format, args);
|
| +#else
|
| usbi_gettimeofday(&now, NULL);
|
| - if (!first.tv_sec) {
|
| - first.tv_sec = now.tv_sec;
|
| - first.tv_usec = now.tv_usec;
|
| + if ((global_debug) && (!has_debug_header_been_displayed)) {
|
| + has_debug_header_been_displayed = 1;
|
| + usbi_log_str(ctx, "[timestamp] [threadID] facility level [function call] <message>\n");
|
| + usbi_log_str(ctx, "--------------------------------------------------------------------------------\n");
|
| }
|
| - if (now.tv_usec < first.tv_usec) {
|
| + if (now.tv_usec < timestamp_origin.tv_usec) {
|
| now.tv_sec--;
|
| now.tv_usec += 1000000;
|
| }
|
| - now.tv_sec -= first.tv_sec;
|
| - now.tv_usec -= first.tv_usec;
|
| + now.tv_sec -= timestamp_origin.tv_sec;
|
| + now.tv_usec -= timestamp_origin.tv_usec;
|
|
|
| switch (level) {
|
| - case LOG_LEVEL_INFO:
|
| + case LIBUSB_LOG_LEVEL_INFO:
|
| prefix = "info";
|
| break;
|
| - case LOG_LEVEL_WARNING:
|
| - stream = stderr;
|
| + case LIBUSB_LOG_LEVEL_WARNING:
|
| prefix = "warning";
|
| break;
|
| - case LOG_LEVEL_ERROR:
|
| - stream = stderr;
|
| + case LIBUSB_LOG_LEVEL_ERROR:
|
| prefix = "error";
|
| break;
|
| - case LOG_LEVEL_DEBUG:
|
| - stream = stderr;
|
| + case LIBUSB_LOG_LEVEL_DEBUG:
|
| prefix = "debug";
|
| break;
|
| + case LIBUSB_LOG_LEVEL_NONE:
|
| + break;
|
| default:
|
| - stream = stderr;
|
| prefix = "unknown";
|
| break;
|
| }
|
|
|
| - fprintf(stream, "libusb: %d.%06d %s [%s] ",
|
| - (int)now.tv_sec, (int)now.tv_usec, prefix, function);
|
| + if (global_debug) {
|
| + header_len = snprintf(buf, sizeof(buf),
|
| + "[%2d.%06d] [%08x] libusbx: %s [%s] ",
|
| + (int)now.tv_sec, (int)now.tv_usec, usbi_get_tid(), prefix, function);
|
| + } else {
|
| + header_len = snprintf(buf, sizeof(buf),
|
| + "libusbx: %s [%s] ", prefix, function);
|
| + }
|
|
|
| - vfprintf(stream, format, args);
|
| + if (header_len < 0 || header_len >= sizeof(buf)) {
|
| + /* Somehow snprintf failed to write to the buffer,
|
| + * remove the header so something useful is output. */
|
| + header_len = 0;
|
| + }
|
| + /* Make sure buffer is NUL terminated */
|
| + buf[header_len] = '\0';
|
| + text_len = vsnprintf(buf + header_len, sizeof(buf) - header_len,
|
| + format, args);
|
| + if (text_len < 0 || text_len + header_len >= sizeof(buf)) {
|
| + /* Truncated log output. On some platforms a -1 return value means
|
| + * that the output was truncated. */
|
| + text_len = sizeof(buf) - header_len;
|
| + }
|
| + if (header_len + text_len + sizeof(USBI_LOG_LINE_END) >= sizeof(buf)) {
|
| + /* Need to truncate the text slightly to fit on the terminator. */
|
| + text_len -= (header_len + text_len + sizeof(USBI_LOG_LINE_END)) - sizeof(buf);
|
| + }
|
| + strcpy(buf + header_len + text_len, USBI_LOG_LINE_END);
|
|
|
| - fprintf(stream, "\n");
|
| + usbi_log_str(ctx, buf);
|
| +#endif
|
| }
|
|
|
| -void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
|
| +void usbi_log(struct libusb_context *ctx, enum libusb_log_level level,
|
| const char *function, const char *format, ...)
|
| {
|
| va_list args;
|
| @@ -1821,19 +2158,18 @@ void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
|
| }
|
|
|
| /** \ingroup misc
|
| - * Returns a constant NULL-terminated string with the ASCII name of a libusb
|
| - * error code. The caller must not free() the returned string.
|
| + * Returns a constant NULL-terminated string with the ASCII name of a libusbx
|
| + * error or transfer status code. The caller must not free() the returned
|
| + * string.
|
| *
|
| - * \param error_code The \ref libusb_error code to return the name of.
|
| + * \param error_code The \ref libusb_error or libusb_transfer_status code to
|
| + * return the name of.
|
| * \returns The error name, or the string **UNKNOWN** if the value of
|
| - * error_code is not a known error code.
|
| + * error_code is not a known error / status code.
|
| */
|
| DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
|
| {
|
| - enum libusb_error error = error_code;
|
| - switch (error) {
|
| - case LIBUSB_SUCCESS:
|
| - return "LIBUSB_SUCCESS";
|
| + switch (error_code) {
|
| case LIBUSB_ERROR_IO:
|
| return "LIBUSB_ERROR_IO";
|
| case LIBUSB_ERROR_INVALID_PARAM:
|
| @@ -1860,13 +2196,30 @@ DEFAULT_VISIBILITY const char * LIBUSB_CALL libusb_error_name(int error_code)
|
| return "LIBUSB_ERROR_NOT_SUPPORTED";
|
| case LIBUSB_ERROR_OTHER:
|
| return "LIBUSB_ERROR_OTHER";
|
| +
|
| + case LIBUSB_TRANSFER_ERROR:
|
| + return "LIBUSB_TRANSFER_ERROR";
|
| + case LIBUSB_TRANSFER_TIMED_OUT:
|
| + return "LIBUSB_TRANSFER_TIMED_OUT";
|
| + case LIBUSB_TRANSFER_CANCELLED:
|
| + return "LIBUSB_TRANSFER_CANCELLED";
|
| + case LIBUSB_TRANSFER_STALL:
|
| + return "LIBUSB_TRANSFER_STALL";
|
| + case LIBUSB_TRANSFER_NO_DEVICE:
|
| + return "LIBUSB_TRANSFER_NO_DEVICE";
|
| + case LIBUSB_TRANSFER_OVERFLOW:
|
| + return "LIBUSB_TRANSFER_OVERFLOW";
|
| +
|
| + case 0:
|
| + return "LIBUSB_SUCCESS / LIBUSB_TRANSFER_COMPLETED";
|
| + default:
|
| + return "**UNKNOWN**";
|
| }
|
| - return "**UNKNOWN**";
|
| }
|
|
|
| /** \ingroup misc
|
| * Returns a pointer to const struct libusb_version with the version
|
| - * (major, minor, micro, rc, and nano) of the running library.
|
| + * (major, minor, micro, nano and rc) of the running library.
|
| */
|
| DEFAULT_VISIBILITY
|
| const struct libusb_version * LIBUSB_CALL libusb_get_version(void)
|
|
|