Android: Add option LIBUSB_OPTION_WEAK_AUTHORITY to support used in apk
authorchris <chris.zhu@xvisiotech.com.cn>
Mon, 17 Aug 2020 02:39:18 +0000 (10:39 +0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Sun, 13 Sep 2020 00:19:57 +0000 (17:19 -0700)
Closes #760

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/core.c
libusb/libusb.h
libusb/os/linux_usbfs.c
libusb/version_nano.h

index 58d43fa..bac340b 100644 (file)
@@ -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
index 3931d60..2f09afe 100644 (file)
@@ -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, ...);
index 61b5b18..27bed33 100644 (file)
@@ -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,
index 829e7a0..e9e0512 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11551
+#define LIBUSB_NANO 11552