Android: Add option LIBUSB_OPTION_WEAK_AUTHORITY to support used in apk

Closes #760

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
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 @@
  * 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 @@
 
 	/* 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 @@
 	 *
 	 * 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 @@
 /* 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 @@
 		}
 	}
 
+#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 @@
 	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 @@
 	.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