Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
00e821d
Core: Prevent potential null-pointer access violation
Jul 7, 2013
30d9282
darwin: Prevent possible modulo-zero error
Jul 7, 2013
4c0c2cc
darwin: Only open device when requesting descriptor if necessary
Jul 7, 2013
606ce56
darwin: Allow building for 10.6 from newer versions of the OS
Jul 8, 2013
de28ca9
android: Attempt to duplicate open file descriptor when opening devic…
Jan 30, 2014
470c98d
darwin: Minimize changes with master branch
Feb 6, 2014
652cb79
android: Another workaround for Android security restrictions
Dec 9, 2014
b3b00e0
Merge pull request #2 from leapmotion/android-security-workaround
hham Dec 9, 2014
f24ca66
android: Don't fail sysfs scan when no devices found
Apr 9, 2015
100264f
Merge pull request #1 from leapmotion/android-empty-sysfs
hham Apr 9, 2015
a844926
Add support of non Hotplug devices
hham Sep 17, 2015
f26feeb
Add #ifdef __ANDROID__ to avoid warnings
hham Sep 17, 2015
049a3ce
Merge pull request #2 from leapmotion/non-hotplug-linux
Sep 17, 2015
18c0254
add libusb_get_device2
Jul 25, 2016
2998ec4
get rid of op_get_device_list for android
Jul 26, 2016
4feea6b
not support hotplug for android
Jul 26, 2016
0b68902
add libusb_open2 for android n
Jul 26, 2016
8162c79
fix reconnect
Aug 2, 2016
646cd5d
fix logging
Aug 2, 2016
b4171ec
fix typo
Aug 2, 2016
9aba58a
add ifdef __ANDROID__ for op_open2
Aug 2, 2016
c6f9ef3
Merge pull request #3 from leapmotion/fix/androidn
Aug 3, 2016
85a11c9
blocking close(fd) on Android
hham Aug 17, 2016
e738572
Merge pull request #4 from leapmotion/fix-android-close
veronicaz41 Aug 18, 2016
488a7a9
fix .mk
Sep 14, 2017
9c13486
remove __android_log_write
Sep 14, 2017
521cdc5
fix get_device_list segfault
Sep 14, 2017
df1ced5
fix LOCAL_C_INCLUDES
Sep 14, 2017
b5299e2
revert android_log_write
Sep 14, 2017
fb6579f
Merge pull request #5 from leapmotion/build_android64
Sep 15, 2017
21b0f28
Merge tag 'v1.0.22-rc1' into leap-2.2.x
Jan 23, 2018
184e06d
fix missing android header
Jan 23, 2018
876f632
Merge remote-tracking branch 'origin/master' into merge-leap
Apr 11, 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
97 changes: 97 additions & 0 deletions 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 @@ -1459,6 +1531,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 +2399,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
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