diff options
author | chris <chris.zhu@xvisiotech.com.cn> | 2020-08-17 10:39:18 +0800 |
---|---|---|
committer | Chris Dickens <christopher.a.dickens@gmail.com> | 2020-09-12 17:19:57 -0700 |
commit | 89b810ec9bde2689b06fdd2b23ccbb3f926d7575 (patch) | |
tree | d9e3705a90d82c499f16454a77185c8df10681e5 | |
parent | 4b64ecc6409ff3533b00153c65e02099c6ca55a9 (diff) | |
download | libusb-89b810ec9bde2689b06fdd2b23ccbb3f926d7575.tar.gz |
Android: Add option LIBUSB_OPTION_WEAK_AUTHORITY to support used in apk
Closes #760
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
-rw-r--r-- | libusb/core.c | 4 | ||||
-rw-r--r-- | libusb/libusb.h | 13 | ||||
-rw-r--r-- | libusb/os/linux_usbfs.c | 29 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
4 files changed, 46 insertions, 2 deletions
diff --git a/libusb/core.c b/libusb/core.c index 58d43fa..bac340b 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -1223,6 +1223,9 @@ void API_EXPORTED libusb_unref_device(libusb_device *dev) * handle for the underlying device. The handle allows you to use libusb to * perform I/O on the device in question. * + * Must call libusb_set_option(NULL, LIBUSB_OPTION_WEAK_AUTHORITY) + * before libusb_init if don't have authority to access the usb device directly. + * * On Linux, the system device handle must be a valid file descriptor opened * on the device node. * @@ -2216,6 +2219,7 @@ int API_EXPORTED libusb_set_option(libusb_context *ctx, /* Handle all backend-specific options here */ case LIBUSB_OPTION_USE_USBDK: + case LIBUSB_OPTION_WEAK_AUTHORITY: if (usbi_backend.set_option) r = usbi_backend.set_option(ctx, option, ap); else diff --git a/libusb/libusb.h b/libusb/libusb.h index 3931d60..2f09afe 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -2087,7 +2087,18 @@ enum libusb_option { * * Only valid on Windows. */ - LIBUSB_OPTION_USE_USBDK = 1 + LIBUSB_OPTION_USE_USBDK = 1, + + /** Set libusb has weak authority. With this option, libusb will skip + * scan devices in libusb_init. + * + * This option should be set before calling libusb_init(), otherwise + * libusb_init will failed. Normally libusb_wrap_sys_device need set + * this option. + * + * Only valid on Linux-based operating system, such as Android. + */ + LIBUSB_OPTION_WEAK_AUTHORITY = 2 }; int LIBUSB_CALL libusb_set_option(libusb_context *ctx, enum libusb_option option, ...); diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c index 61b5b18..27bed33 100644 --- a/libusb/os/linux_usbfs.c +++ b/libusb/os/linux_usbfs.c @@ -95,6 +95,11 @@ static int sysfs_available = -1; /* how many times have we initted (and not exited) ? */ static int init_count = 0; +#ifdef __ANDROID__ +/* have no authority to operate usb device directly */ +static int weak_authority = 0; +#endif + /* Serialize hotplug start/stop */ static usbi_mutex_static_t linux_hotplug_startstop_lock = USBI_MUTEX_INITIALIZER; /* Serialize scan-devices, event-thread, and poll */ @@ -381,6 +386,12 @@ static int op_init(struct libusb_context *ctx) } } +#ifdef __ANDROID__ + if (weak_authority) { + return LIBUSB_SUCCESS; + } +#endif + usbi_mutex_static_lock(&linux_hotplug_startstop_lock); r = LIBUSB_SUCCESS; if (init_count == 0) { @@ -413,6 +424,23 @@ static void op_exit(struct libusb_context *ctx) usbi_mutex_static_unlock(&linux_hotplug_startstop_lock); } +static int op_set_option(struct libusb_context *ctx, enum libusb_option option, va_list ap) +{ + UNUSED(ctx); + UNUSED(ap); + + switch (option) { +#ifdef __ANDROID__ + case LIBUSB_OPTION_WEAK_AUTHORITY: + usbi_dbg("set libusb has weak authority"); + weak_authority = 1; + return LIBUSB_SUCCESS; +#endif + default: + return LIBUSB_ERROR_NOT_SUPPORTED; + } +} + static int linux_scan_devices(struct libusb_context *ctx) { int ret; @@ -2688,6 +2716,7 @@ const struct usbi_os_backend usbi_backend = { .caps = USBI_CAP_HAS_HID_ACCESS|USBI_CAP_SUPPORTS_DETACH_KERNEL_DRIVER, .init = op_init, .exit = op_exit, + .set_option = op_set_option, .hotplug_poll = op_hotplug_poll, .get_active_config_descriptor = op_get_active_config_descriptor, .get_config_descriptor = op_get_config_descriptor, diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 829e7a0..e9e0512 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11551 +#define LIBUSB_NANO 11552 |