Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
658bad0
Merge remote-tracking branch 'libusb/master'
Apr 11, 2018
790b20a
Core: Prevent potential null-pointer access violation
Jul 7, 2013
e7c2e64
darwin: Prevent possible modulo-zero error
Jul 7, 2013
cbff1a1
darwin: Only open device when requesting descriptor if necessary
Jul 7, 2013
a1e77e0
darwin: Allow building for 10.6 from newer versions of the OS
Jul 8, 2013
7d85fec
android: Attempt to duplicate open file descriptor when opening devic…
Jan 30, 2014
4869c41
darwin: Minimize changes with master branch
Feb 6, 2014
064cbe0
android: Another workaround for Android security restrictions
Dec 9, 2014
5db8114
Add support of non Hotplug devices
hham Sep 17, 2015
c6c6d77
Add #ifdef __ANDROID__ to avoid warnings
hham Sep 17, 2015
20f2f50
add libusb_get_device2
Jul 25, 2016
ab798c3
get rid of op_get_device_list for android
Jul 26, 2016
8f8f42b
not support hotplug for android
Jul 26, 2016
b791170
add libusb_open2 for android n
Jul 26, 2016
ff6571a
fix reconnect
Aug 2, 2016
2983563
fix logging
Aug 2, 2016
d43b8cb
fix typo
Aug 2, 2016
7895f8e
add ifdef __ANDROID__ for op_open2
Aug 2, 2016
02fa1c3
blocking close(fd) on Android
hham Aug 17, 2016
1700872
fix .mk
Sep 14, 2017
c734946
remove __android_log_write
Sep 14, 2017
826c39e
fix get_device_list segfault
Sep 14, 2017
2a0d774
fix LOCAL_C_INCLUDES
Sep 14, 2017
366e211
revert android_log_write
Sep 14, 2017
0bd6dfb
fix ctrl buffer of UVC_GET_CUR is not transferred to host
hham Feb 24, 2018
3b1634d
Fix a build error
hham Sep 2, 2016
9ae0710
Fix missing android header
Jan 19, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions android/jni/examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/listdevs.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= listdevs

Expand All @@ -44,9 +44,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/xusb.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= xusb

Expand All @@ -60,9 +60,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/hotplugtest.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= hotplugtest

Expand All @@ -77,9 +77,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/ezusb.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= fxload

Expand All @@ -93,9 +93,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/sam3u_benchmark.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= sam3u_benchmark

Expand All @@ -109,9 +109,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/dpfp.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= dpfp

Expand All @@ -125,9 +125,9 @@ LOCAL_SRC_FILES := \
$(LIBUSB_ROOT_REL)/examples/dpfp_threaded.c

LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)
$(LIBUSB_ROOT_ABS)/libusb

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0

LOCAL_MODULE:= dpfp_threaded

Expand Down
2 changes: 1 addition & 1 deletion android/jni/libusb.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ LOCAL_EXPORT_C_INCLUDES := \

LOCAL_LDLIBS := -llog

LOCAL_MODULE := libusb1.0
LOCAL_MODULE := libusb-1.0.0

include $(BUILD_SHARED_LIBRARY)
2 changes: 1 addition & 1 deletion android/jni/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ LOCAL_SRC_FILES := \
LOCAL_C_INCLUDES += \
$(LIBUSB_ROOT_ABS)

LOCAL_SHARED_LIBRARIES += libusb1.0
LOCAL_SHARED_LIBRARIES += libusb-1.0.0
LOCAL_STATIC_LIBRARIES += testlib

LOCAL_MODULE:= stress
Expand Down
100 changes: 99 additions & 1 deletion libusb/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,10 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context *ctx,
usbi_mutex_unlock(&ctx->usb_devs_lock);
} else {
/* backend does not provide hotplug support */
if (!usbi_backend.get_device_list) {
len = -1;
goto out;
}
r = usbi_backend.get_device_list(ctx, &discdevs);
}

Expand Down Expand Up @@ -1276,6 +1280,74 @@ int API_EXPORTED libusb_open(libusb_device *dev,
}

/** \ingroup libusb_dev
* Open a device with a file descriptor and obtain a device handle.
* A handle allows you to perform I/O on the device in question.
*
* UseCase: Android, after permission granted from Android Device Manager.
* The fd can be obtained by calling UsbDeviceConnection.getFileDescriptor().
*
* Internally, this function adds a reference to the device and makes it
* available to you through libusb_get_device(). This reference is removed
* during libusb_close().
*
* This is a non-blocking function; no requests are sent over the bus.
*
* \param dev the device to open
* \param fd the file descriptor for the device
* \param handle output location for the returned device handle pointer. Only
* populated when the return code is 0.
* \returns 0 on success
* \returns LIBUSB_ERROR_NO_MEM on memory allocation failure
* \returns LIBUSB_ERROR_ACCESS if the user has insufficient permissions
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
* \returns another LIBUSB_ERROR code on other failure
*/
int API_EXPORTED libusb_open2(libusb_device *dev, int fd,
libusb_device_handle **handle)
{
if (usbi_backend.open2 == NULL) {
return LIBUSB_ERROR_NOT_SUPPORTED;
}

struct libusb_context *ctx = DEVICE_CTX(dev);
struct libusb_device_handle *_handle;
size_t priv_size = usbi_backend.device_handle_priv_size;
int r;
usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);

_handle = malloc(sizeof(*_handle) + priv_size);
if (!_handle)
return LIBUSB_ERROR_NO_MEM;

r = usbi_mutex_init(&_handle->lock);
if (r) {
free(_handle);
return LIBUSB_ERROR_OTHER;
}

_handle->dev = libusb_ref_device(dev);
_handle->auto_detach_kernel_driver = 0;
_handle->claimed_interfaces = 0;
memset(&_handle->os_priv, 0, priv_size);

r = usbi_backend.open2(_handle, fd);
if (r < 0) {
usbi_dbg("open2 %d.%d returns %d", dev->bus_number, dev->device_address, r);
libusb_unref_device(dev);
usbi_mutex_destroy(&_handle->lock);
free(_handle);
return r;
}

usbi_mutex_lock(&ctx->open_devs_lock);
list_add(&_handle->list, &ctx->open_devs);
usbi_mutex_unlock(&ctx->open_devs_lock);
*handle = _handle;

return 0;
}

/** \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
Expand Down Expand Up @@ -1345,6 +1417,7 @@ static void do_close(struct libusb_context *ctx,
continue;

usbi_mutex_lock(&itransfer->lock);

if (!(itransfer->state_flags & USBI_TRANSFER_DEVICE_DISAPPEARED)) {
usbi_err(ctx, "Device handle closed while transfer was still being processed, but the device is still connected as far as we know");

Expand Down Expand Up @@ -1459,6 +1532,27 @@ libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle)
}

/** \ingroup libusb_dev
* Get the underlying device for a dev_node.
* UseCase: Android
* \param ctx the context to operate on, or NULL for the default context
* \param dev_node device path
* \param descriptors the raw USB descriptors for the device
* \param descriptors_size the size of the descriptors array
* \returns the underlying device
*/
DEFAULT_VISIBILITY
libusb_device * LIBUSB_CALL libusb_get_device2(libusb_context *ctx, const char *dev_node,
const char* descriptors, size_t descriptors_size)
{
if (usbi_backend.get_device2 == NULL) {
/* Not supported on this platform */
return NULL;
}

return usbi_backend.get_device2(ctx, dev_node, descriptors, descriptors_size);
}

/** \ingroup dev
* Determine the bConfigurationValue of the currently active configuration.
*
* You could formulate your own control request to obtain this information,
Expand Down Expand Up @@ -2306,7 +2400,11 @@ int API_EXPORTED libusb_has_capability(uint32_t capability)
case LIBUSB_CAP_HAS_CAPABILITY:
return 1;
case LIBUSB_CAP_HAS_HOTPLUG:
#ifdef __ANDROID__
return 0;
#else
return !(usbi_backend.get_device_list);
#endif
case LIBUSB_CAP_HAS_HID_ACCESS:
return (usbi_backend.caps & USBI_CAP_HAS_HID_ACCESS);
case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
Expand Down Expand Up @@ -2377,7 +2475,7 @@ static void usbi_log_str(enum libusb_log_level level, const char *str)
case LIBUSB_LOG_LEVEL_INFO: priority = ANDROID_LOG_INFO; break;
case LIBUSB_LOG_LEVEL_DEBUG: priority = ANDROID_LOG_DEBUG; break;
}
__android_log_write(priority, "libusb", str);
__android_log_print(priority, "libusb", str);
#elif defined(HAVE_SYSLOG_FUNC)
int syslog_level = LOG_INFO;
switch (level) {
Expand Down
3 changes: 3 additions & 0 deletions libusb/libusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1363,8 +1363,11 @@ int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev,
unsigned char endpoint);

int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **dev_handle);
int LIBUSB_CALL libusb_open2(libusb_device *dev, int fd, libusb_device_handle **handle);
void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle);
libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle);
libusb_device * LIBUSB_CALL libusb_get_device2(libusb_context *ctx, const char *dev_node,
const char* descriptors, size_t descriptors_len);

int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev_handle,
int configuration);
Expand Down
13 changes: 10 additions & 3 deletions libusb/libusbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ static inline void list_add_tail(struct list_head *entry,

static inline void list_del(struct list_head *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = entry->prev = NULL;
if (entry->next && entry->prev) {
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
entry->next = entry->prev = NULL;
}
}

static inline void list_cut(struct list_head *list, struct list_head *head)
Expand Down Expand Up @@ -701,6 +703,9 @@ struct usbi_os_backend {
*/
void (*hotplug_poll)(void);

struct libusb_device* (*get_device2)(struct libusb_context *ctx, const char *dev_node,
const char* descriptors, size_t descriptors_len);

/* Open a device for I/O and other USB operations. The device handle
* is preallocated for you, you can retrieve the device in question
* through handle->dev.
Expand Down Expand Up @@ -728,6 +733,8 @@ struct usbi_os_backend {
*/
int (*open)(struct libusb_device_handle *dev_handle);

int (*open2)(struct libusb_device_handle *handle, int fd);

/* Close a device such that the handle cannot be used again. Your backend
* should destroy any resources that were allocated in the open path.
* This may also be a good place to call usbi_remove_pollfd() to inform
Expand Down
16 changes: 9 additions & 7 deletions libusb/os/darwin_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ static int darwin_request_descriptor (usb_device_t **device, UInt8 desc, UInt8 d

static int darwin_cache_device_descriptor (struct libusb_context *ctx, struct darwin_cached_device *dev) {
usb_device_t **device = dev->device;
int retries = 1, delay = 30000;
int retries = 2, delay = 30000;
int unsuspended = 0, try_unsuspend = 1, try_reconfigure = 1;
int is_open = 0;
int ret = 0, ret2;
Expand All @@ -756,11 +756,6 @@ static int darwin_cache_device_descriptor (struct libusb_context *ctx, struct da
(*device)->GetDeviceProduct (device, &idProduct);
(*device)->GetDeviceVendor (device, &idVendor);

/* According to Apple's documentation the device must be open for DeviceRequest but we may not be able to open some
* devices and Apple's USB Prober doesn't bother to open the device before issuing a descriptor request. Still,
* to follow the spec as closely as possible, try opening the device */
is_open = ((*device)->USBDeviceOpenSeize(device) == kIOReturnSuccess);

do {
/**** retrieve device descriptor ****/
ret = darwin_request_descriptor (device, kUSBDeviceDesc, 0, &dev->dev_descriptor, sizeof(dev->dev_descriptor));
Expand Down Expand Up @@ -790,6 +785,13 @@ static int darwin_cache_device_descriptor (struct libusb_context *ctx, struct da
ret = kIOUSBPipeStalled;
}

if (kIOReturnSuccess != ret && !is_open) {
/* if the device descriptor request failed on the unopened device, try again with the device opened */
is_open = ((*device)->USBDeviceOpenSeize(device) == kIOReturnSuccess);
if (is_open)
continue;
}

if (kIOReturnSuccess != ret && is_open && try_unsuspend) {
/* device may be suspended. unsuspend it and try again */
#if DeviceVersion >= 320
Expand Down Expand Up @@ -1618,7 +1620,7 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
return darwin_to_libusb (ret);
}

if (0 != (transfer->length % maxPacketSize)) {
if (0 != maxPacketSize && 0 != (transfer->length % maxPacketSize)) {
/* do not need a zero packet */
transfer->flags &= ~LIBUSB_TRANSFER_ADD_ZERO_PACKET;
}
Expand Down
3 changes: 3 additions & 0 deletions libusb/os/darwin_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/IOCFPlugIn.h>

/* Include support for Mac OS X 10.6 */
#undef kIOUSBDeviceInterfaceID500

/* IOUSBInterfaceInferface */

/* New in OS 10.12.0. */
Expand Down
Loading