[PATCH 5/5] usbutils: convert to libusb-1.0

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



We need to eat our own dog food.

Signed-off-by: Mike Frysinger <vapier@xxxxxxxxxx>
---
note: ive tested it on my one laptop, but seems to work

 configure.ac |    2 +-
 lsusb.c      |  532 ++++++++++++++++++++++++++++++----------------------------
 usbmisc.c    |   30 ++--
 usbmisc.h    |    4 +-
 4 files changed, 295 insertions(+), 273 deletions(-)

diff --git a/configure.ac b/configure.ac
index 7b771b2..82553e2 100644
--- a/configure.ac
+++ b/configure.ac
@@ -27,7 +27,7 @@ if test "$USE_ZLIB" = "yes" ; then
 fi
 AM_CONDITIONAL(HAVE_ZLIB, test x$HAVE_ZLIB = xyes)
 
-PKG_CHECK_MODULES(LIBUSB, libusb >= 0.1.12)
+PKG_CHECK_MODULES(LIBUSB, libusb-1.0 >= 1.0.0)
 AC_SUBST(LIBUSB_CFLAGS)
 AC_SUBST(LIBUSB_LIBS)
 
diff --git a/lsusb.c b/lsusb.c
index ea9bdb9..3c7e299 100644
--- a/lsusb.c
+++ b/lsusb.c
@@ -38,7 +38,7 @@
 #include <byteswap.h>
 #endif
 
-#include <usb.h>
+#include <libusb.h>
 #include <unistd.h>
 
 #include "names.h"
@@ -47,70 +47,64 @@
 
 #include <getopt.h>
 
-#if (__BYTE_ORDER == __LITTLE_ENDIAN)
-  #define le16_to_cpu(x) (x)
-#elif (__BYTE_ORDER == __BIG_ENDIAN)
-  #define le16_to_cpu(x) bswap_16(x)
-#else
-  #error missing BYTE_ORDER
-#endif
+#define le16_to_cpu(x) libusb_cpu_to_le16(libusb_cpu_to_le16(x))
 
 /* from USB 2.0 spec and updates */
-#define USB_DT_DEVICE_QUALIFIER		0x06
-#define USB_DT_OTHER_SPEED_CONFIG	0x07
-#define USB_DT_OTG			0x09
-#define USB_DT_DEBUG			0x0a
-#define USB_DT_INTERFACE_ASSOCIATION	0x0b
-#define USB_DT_SECURITY			0x0c
-#define USB_DT_KEY			0x0d
-#define USB_DT_ENCRYPTION_TYPE		0x0e
-#define USB_DT_BOS			0x0f
-#define USB_DT_DEVICE_CAPABILITY	0x10
-#define USB_DT_WIRELESS_ENDPOINT_COMP	0x11
-#define USB_DT_WIRE_ADAPTER		0x21
-#define USB_DT_RPIPE			0x22
-#define USB_DT_RC_INTERFACE		0x23
-#define USB_DT_SS_ENDPOINT_COMP		0x30
+#define LIBUSB_DT_DEVICE_QUALIFIER		0x06
+#define LIBUSB_DT_OTHER_SPEED_CONFIG		0x07
+#define LIBUSB_DT_OTG				0x09
+#define LIBUSB_DT_DEBUG				0x0a
+#define LIBUSB_DT_INTERFACE_ASSOCIATION		0x0b
+#define LIBUSB_DT_SECURITY			0x0c
+#define LIBUSB_DT_KEY				0x0d
+#define LIBUSB_DT_ENCRYPTION_TYPE		0x0e
+#define LIBUSB_DT_BOS				0x0f
+#define LIBUSB_DT_DEVICE_CAPABILITY		0x10
+#define LIBUSB_DT_WIRELESS_ENDPOINT_COMP	0x11
+#define LIBUSB_DT_WIRE_ADAPTER			0x21
+#define LIBUSB_DT_RPIPE				0x22
+#define LIBUSB_DT_RC_INTERFACE			0x23
+#define LIBUSB_DT_SS_ENDPOINT_COMP		0x30
 
 /* Device Capability Type Codes (Wireless USB spec and USB 3.0 bus spec) */
-#define USB_DC_WIRELESS_USB		0x01
-#define USB_DC_20_EXTENSION		0x02
-#define USB_DC_SUPERSPEED		0x03
-#define USB_DC_CONTAINER_ID		0x04
+#define LIBUSB_DC_WIRELESS_USB		0x01
+#define LIBUSB_DC_20_EXTENSION		0x02
+#define LIBUSB_DC_SUPERSPEED		0x03
+#define LIBUSB_DC_CONTAINER_ID		0x04
 
 /* Conventional codes for class-specific descriptors.  The convention is
  * defined in the USB "Common Class" Spec (3.11).  Individual class specs
  * are authoritative for their usage, not the "common class" writeup.
  */
-#define USB_DT_CS_DEVICE		(USB_TYPE_CLASS | USB_DT_DEVICE)
-#define USB_DT_CS_CONFIG		(USB_TYPE_CLASS | USB_DT_CONFIG)
-#define USB_DT_CS_STRING		(USB_TYPE_CLASS | USB_DT_STRING)
-#define USB_DT_CS_INTERFACE		(USB_TYPE_CLASS | USB_DT_INTERFACE)
-#define USB_DT_CS_ENDPOINT		(USB_TYPE_CLASS | USB_DT_ENDPOINT)
-
-#ifndef USB_CLASS_CCID
-#define USB_CLASS_CCID			0x0b
+#define LIBUSB_DT_CS_DEVICE		(LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_DEVICE)
+#define LIBUSB_DT_CS_CONFIG		(LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_CONFIG)
+#define LIBUSB_DT_CS_STRING		(LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_STRING)
+#define LIBUSB_DT_CS_INTERFACE		(LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_INTERFACE)
+#define LIBUSB_DT_CS_ENDPOINT		(LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_DT_ENDPOINT)
+
+#ifndef LIBUSB_CLASS_CCID
+#define LIBUSB_CLASS_CCID		0x0b
 #endif
 
-#ifndef USB_CLASS_VIDEO
-#define USB_CLASS_VIDEO			0x0e
+#ifndef LIBUSB_CLASS_VIDEO
+#define LIBUSB_CLASS_VIDEO		0x0e
 #endif
 
-#ifndef USB_CLASS_APPLICATION
-#define USB_CLASS_APPLICATION	       	0xfe
+#ifndef LIBUSB_CLASS_APPLICATION
+#define LIBUSB_CLASS_APPLICATION	0xfe
 #endif
 
-#ifndef USB_AUDIO_CLASS_1
-#define USB_AUDIO_CLASS_1		0x00
+#ifndef LIBUSB_AUDIO_CLASS_1
+#define LIBUSB_AUDIO_CLASS_1		0x00
 #endif
 
-#ifndef USB_AUDIO_CLASS_2
-#define USB_AUDIO_CLASS_2		0x20
+#ifndef LIBUSB_AUDIO_CLASS_2
+#define LIBUSB_AUDIO_CLASS_2		0x20
 #endif
 
 #define VERBLEVEL_DEFAULT 0	/* 0 gives lspci behaviour; 1, lsusb-0.9 */
 
-#define CTRL_RETRIES	 2
+#define CTRL_RETRIES	2
 #define CTRL_TIMEOUT	(5*1000)	/* milliseconds */
 
 #define	HUB_STATUS_BYTELEN	3	/* max 3 bytes status = hub + 23 ports */
@@ -127,16 +121,16 @@ static const char * const encryption_type[] = {
 	"RESERVED"
 };
 
-static void dump_interface(struct usb_dev_handle *dev, const struct usb_interface *interface);
-static void dump_endpoint(struct usb_dev_handle *dev, const struct usb_interface_descriptor *interface, const struct usb_endpoint_descriptor *endpoint);
-static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsigned char *buf, int protocol);
-static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsigned char *buf, int protocol);
-static void dump_midistreaming_interface(struct usb_dev_handle *dev, const unsigned char *buf);
-static void dump_videocontrol_interface(struct usb_dev_handle *dev, const unsigned char *buf);
+static void dump_interface(libusb_device_handle *dev, const struct libusb_interface *interface);
+static void dump_endpoint(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const struct libusb_endpoint_descriptor *endpoint);
+static void dump_audiocontrol_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol);
+static void dump_audiostreaming_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol);
+static void dump_midistreaming_interface(libusb_device_handle *dev, const unsigned char *buf);
+static void dump_videocontrol_interface(libusb_device_handle *dev, const unsigned char *buf);
 static void dump_videostreaming_interface(const unsigned char *buf);
 static void dump_dfu_interface(const unsigned char *buf);
-static char *dump_comm_descriptor(struct usb_dev_handle *dev, const unsigned char *buf, char *indent);
-static void dump_hid_device(struct usb_dev_handle *dev, const struct usb_interface_descriptor *interface, const unsigned char *buf);
+static char *dump_comm_descriptor(libusb_device_handle *dev, const unsigned char *buf, char *indent);
+static void dump_hid_device(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const unsigned char *buf);
 static void dump_audiostreaming_endpoint(const unsigned char *buf, int protocol);
 static void dump_midistreaming_endpoint(const unsigned char *buf);
 static void dump_hub(const char *prefix, const unsigned char *p, int tt_type);
@@ -155,13 +149,18 @@ static unsigned int convert_le_u32 (const unsigned char *buf)
  * using "char" is trouble.  Likewise, sizes should never be negative.
  */
 
-static inline int typesafe_control_msg(usb_dev_handle *dev,
+static inline int typesafe_control_msg(libusb_device_handle *dev,
 	unsigned char requesttype, unsigned char request,
 	int value, int idx,
 	unsigned char *bytes, unsigned size, int timeout)
 {
-	return usb_control_msg(dev, requesttype, request, value, idx,
-		(char *)bytes, (int)size, timeout);
+	int ret = libusb_control_transfer(dev, requesttype, request, value,
+					idx, bytes, size, timeout);
+
+	if (ret < 0)
+		return -ret;
+	else
+		return ret;
 }
 
 #define usb_control_msg		typesafe_control_msg
@@ -185,7 +184,7 @@ int lprintf(unsigned int vl, const char *format, ...)
 
 /* ---------------------------------------------------------------------- */
 
-static int get_string(struct usb_dev_handle *dev, char* buf, size_t size, u_int8_t id)
+static int get_string(libusb_device_handle *dev, char *buf, size_t size, u_int8_t id)
 {
 	int ret;
 
@@ -195,7 +194,7 @@ static int get_string(struct usb_dev_handle *dev, char* buf, size_t size, u_int8
 	}
 
 	if (id) {
-		ret = usb_get_string_simple(dev, id, buf, size);
+		ret = libusb_get_string_descriptor_ascii(dev, id, (void *)buf, size);
 		if (ret <= 0) {
 			buf[0] = 0;
 			return 0;
@@ -341,8 +340,8 @@ static void dump_junk(const unsigned char *buf, const char *indent, unsigned int
  */
 
 static void dump_device(
-	struct usb_dev_handle *dev,
-	struct usb_device_descriptor *descriptor
+	libusb_device_handle *dev,
+	struct libusb_device_descriptor *descriptor
 )
 {
 	char vendor[128], product[128];
@@ -444,7 +443,7 @@ static void dump_encryption_type(const unsigned char *buf)
 	       encryption_type[b_encryption_type], buf[3], buf[4]);
 }
 
-static void dump_association(struct usb_dev_handle *dev, const unsigned char *buf)
+static void dump_association(libusb_device_handle *dev, const unsigned char *buf)
 {
 	char cls[128], subcls[128], proto[128];
 	char func[128];
@@ -470,7 +469,7 @@ static void dump_association(struct usb_dev_handle *dev, const unsigned char *bu
 	       buf[7], func);
 }
 
-static void dump_config(struct usb_dev_handle *dev, struct usb_config_descriptor *config)
+static void dump_config(libusb_device_handle *dev, struct libusb_config_descriptor *config)
 {
 	char cfg[128];
 	int i;
@@ -502,8 +501,8 @@ static void dump_config(struct usb_dev_handle *dev, struct usb_config_descriptor
 	printf("    MaxPower            %5umA\n", config->MaxPower * 2);
 
 	/* avoid re-ordering or hiding descriptors for display */
-	if (config->extralen) {
-		int		size = config->extralen;
+	if (config->extra_length) {
+		int		size = config->extra_length;
 		const unsigned char	*buf = config->extra;
 
 		while (size >= 2) {
@@ -512,16 +511,16 @@ static void dump_config(struct usb_dev_handle *dev, struct usb_config_descriptor
 				break;
 			}
 			switch (buf[1]) {
-			case USB_DT_OTG:
+			case LIBUSB_DT_OTG:
 				/* handled separately */
 				break;
-			case USB_DT_INTERFACE_ASSOCIATION:
+			case LIBUSB_DT_INTERFACE_ASSOCIATION:
 				dump_association(dev, buf);
 				break;
-			case USB_DT_SECURITY:
+			case LIBUSB_DT_SECURITY:
 				dump_security(buf);
 				break;
-			case USB_DT_ENCRYPTION_TYPE:
+			case LIBUSB_DT_ENCRYPTION_TYPE:
 				dump_encryption_type(buf);
 				break;
 			default:
@@ -538,7 +537,7 @@ static void dump_config(struct usb_dev_handle *dev, struct usb_config_descriptor
 		dump_interface(dev, &config->interface[i]);
 }
 
-static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interface_descriptor *interface)
+static void dump_altsetting(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface)
 {
 	char cls[128], subcls[128], proto[128];
 	char ifstr[128];
@@ -566,8 +565,8 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 	       interface->iInterface, ifstr);
 
 	/* avoid re-ordering or hiding descriptors for display */
-	if (interface->extralen) {
-		size = interface->extralen;
+	if (interface->extra_length) {
+		size = interface->extra_length;
 		buf = interface->extra;
 		while (size >= 2 * sizeof(u_int8_t)) {
 			if (buf[0] < 2) {
@@ -581,10 +580,10 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 			 * descriptors: explicitly tagged, using common class
 			 * spec conventions.
 			 */
-			case USB_DT_CS_DEVICE:
-			case USB_DT_CS_INTERFACE:
+			case LIBUSB_DT_CS_DEVICE:
+			case LIBUSB_DT_CS_INTERFACE:
 				switch (interface->bInterfaceClass) {
-				case USB_CLASS_AUDIO:
+				case LIBUSB_CLASS_AUDIO:
 					switch (interface->bInterfaceSubClass) {
 					case 1:
 						dump_audiocontrol_interface(dev, buf, interface->bInterfaceProtocol);
@@ -599,11 +598,11 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 						goto dump;
 					}
 					break;
-				case USB_CLASS_COMM:
+				case LIBUSB_CLASS_COMM:
 					dump_comm_descriptor(dev, buf,
 						"      ");
 					break;
-				case USB_CLASS_VIDEO:
+				case LIBUSB_CLASS_VIDEO:
 					switch (interface->bInterfaceSubClass) {
 					case 1:
 						dump_videocontrol_interface(dev, buf);
@@ -615,7 +614,7 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 						goto dump;
 					}
 					break;
-				case USB_CLASS_APPLICATION:
+				case LIBUSB_CLASS_APPLICATION:
 					switch (interface->bInterfaceSubClass) {
 					case 1:
 						dump_dfu_interface(buf);
@@ -624,10 +623,10 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 						goto dump;
 					}
 					break;
-				case USB_CLASS_HID:
+				case LIBUSB_CLASS_HID:
 					dump_hid_device(dev, interface, buf);
 					break;
-				case USB_CLASS_CCID:
+				case LIBUSB_CLASS_CCID:
 					dump_ccid_device(buf);
 					break;
 				default:
@@ -640,10 +639,10 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 			 */
 			default:
 				switch (interface->bInterfaceClass) {
-				case USB_CLASS_HID:
+				case LIBUSB_CLASS_HID:
 					dump_hid_device(dev, interface, buf);
 					break;
-				case USB_CLASS_CCID:
+				case LIBUSB_CLASS_CCID:
 					dump_ccid_device(buf);
 					break;
 				case 0xe0:	/* wireless */
@@ -667,10 +666,10 @@ static void dump_altsetting(struct usb_dev_handle *dev, const struct usb_interfa
 				default:
 					/* ... not everything is class-specific */
 					switch (buf[1]) {
-					case USB_DT_OTG:
+					case LIBUSB_DT_OTG:
 						/* handled separately */
 						break;
-					case USB_DT_INTERFACE_ASSOCIATION:
+					case LIBUSB_DT_INTERFACE_ASSOCIATION:
 						dump_association(dev, buf);
 						break;
 					default:
@@ -691,7 +690,7 @@ dump:
 		dump_endpoint(dev, interface, &interface->endpoint[i]);
 }
 
-static void dump_interface(struct usb_dev_handle *dev, const struct usb_interface *interface)
+static void dump_interface(libusb_device_handle *dev, const struct libusb_interface *interface)
 {
 	int i;
 
@@ -699,7 +698,7 @@ static void dump_interface(struct usb_dev_handle *dev, const struct usb_interfac
 		dump_altsetting(dev, &interface->altsetting[i]);
 }
 
-static void dump_endpoint(struct usb_dev_handle *dev, const struct usb_interface_descriptor *interface, const struct usb_endpoint_descriptor *endpoint)
+static void dump_endpoint(libusb_device_handle *dev, const struct libusb_interface_descriptor *interface, const struct libusb_endpoint_descriptor *endpoint)
 {
 	static const char * const typeattr[] = {
 		"Control",
@@ -752,8 +751,8 @@ static void dump_endpoint(struct usb_dev_handle *dev, const struct usb_interface
 		       endpoint->bRefresh, endpoint->bSynchAddress);
 
 	/* avoid re-ordering or hiding descriptors for display */
-	if (endpoint->extralen) {
-		size = endpoint->extralen;
+	if (endpoint->extra_length) {
+		size = endpoint->extra_length;
 		buf = endpoint->extra;
 		while (size >= 2 * sizeof(u_int8_t)) {
 			if (buf[0] < 2) {
@@ -761,17 +760,17 @@ static void dump_endpoint(struct usb_dev_handle *dev, const struct usb_interface
 				break;
 			}
 			switch (buf[1]) {
-			case USB_DT_CS_ENDPOINT:
+			case LIBUSB_DT_CS_ENDPOINT:
 				if (interface->bInterfaceClass == 1 && interface->bInterfaceSubClass == 2)
 					dump_audiostreaming_endpoint(buf, interface->bInterfaceProtocol);
 				else if (interface->bInterfaceClass == 1 && interface->bInterfaceSubClass == 3)
 					dump_midistreaming_endpoint(buf);
 				break;
-			case USB_DT_CS_INTERFACE:
+			case LIBUSB_DT_CS_INTERFACE:
 				/* MISPLACED DESCRIPTOR ... less indent */
 				switch (interface->bInterfaceClass) {
-				case USB_CLASS_COMM:
-				case USB_CLASS_DATA:	/* comm data */
+				case LIBUSB_CLASS_COMM:
+				case LIBUSB_CLASS_DATA:	/* comm data */
 					dump_comm_descriptor(dev, buf,
 						"      ");
 					break;
@@ -780,13 +779,13 @@ static void dump_endpoint(struct usb_dev_handle *dev, const struct usb_interface
 					dump_bytes(buf, buf[0]);
 				}
 				break;
-			case USB_DT_OTG:
+			case LIBUSB_DT_OTG:
 				/* handled separately */
 				break;
-			case USB_DT_INTERFACE_ASSOCIATION:
+			case LIBUSB_DT_INTERFACE_ASSOCIATION:
 				dump_association(dev, buf);
 				break;
-			case USB_DT_SS_ENDPOINT_COMP:
+			case LIBUSB_DT_SS_ENDPOINT_COMP:
 				printf("        bMaxBurst %15u\n", buf[2] + 1);
 				/* Print bulk streams info or isoc "Mult" */
 				if ((endpoint->bmAttributes & 3) == 2 &&
@@ -960,14 +959,14 @@ static void dump_audio_bmcontrols(const char *prefix, int bmcontrols, const stru
 {
 	while (list->name) {
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			if (bmcontrols & (1 << list->bit))
 				printf("%s%s Control\n", prefix, list->name);
 
 			break;
 
-		case USB_AUDIO_CLASS_2: {
-			const char *ctrl_type[] = { "read-only", "ILLEGAL (0b10)", "read/write" };
+		case LIBUSB_AUDIO_CLASS_2: {
+			const char * const ctrl_type[] = { "read-only", "ILLEGAL (0b10)", "read/write" };
 			int ctrl = (bmcontrols >> (list->bit * 2)) & 0x3;
 
 			if (ctrl)
@@ -993,7 +992,7 @@ static const char * const chconfig_uac2[] = {
 	"Back Left of Center (BLC)", "Back Right of Center (BRC)"
 };
 
-static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsigned char *buf, int protocol)
+static void dump_audiocontrol_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol)
 {
 	static const char * const chconfig[] = {
 		"Left Front (L)", "Right Front (R)", "Center Front (C)", "Low Freqency Enhancement (LFE)",
@@ -1006,7 +1005,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 	unsigned int i, chcfg, j, k, N, termt, subtype;
 	char chnames[128], term[128], termts[128];
 
-	if (buf[1] != USB_DT_CS_INTERFACE)
+	if (buf[1] != LIBUSB_DT_CS_INTERFACE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 3)
 		printf("      Warning: Descriptor too short\n");
@@ -1027,7 +1026,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 	 *
 	 */
 
-	if (protocol == USB_AUDIO_CLASS_2)
+	if (protocol == LIBUSB_AUDIO_CLASS_2)
 		switch(buf[2]) {
 		case 0x07: subtype = 0xf0; break; /* effect unit */
 		case 0x08: subtype = 0x07; break; /* processing unit */
@@ -1041,7 +1040,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 	case 0x01:  /* HEADER */
 		printf("(HEADER)\n");
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			if (buf[0] < 8+buf[7])
 				printf("      Warning: Descriptor too short\n");
 			printf("        bcdADC              %2x.%02x\n"
@@ -1052,7 +1051,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 				printf("        baInterfaceNr(%2u)   %5u\n", i, buf[8+i]);
 			dump_junk(buf, "        ", 8+buf[7]);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			if (buf[0] < 9)
 				printf("      Warning: Descriptor too short\n");
 			printf("        bcdADC              %2x.%02x\n"
@@ -1071,7 +1070,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 		get_audioterminal_string(termts, sizeof(termts), termt);
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			get_string(dev, chnames, sizeof(chnames), buf[10]);
 			get_string(dev, term, sizeof(term), buf[11]);
 			if (buf[0] < 12)
@@ -1091,7 +1090,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			       buf[10], chnames, buf[11], term);
 			dump_junk(buf, "        ", 12);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			get_string(dev, chnames, sizeof(chnames), buf[13]);
 			get_string(dev, term, sizeof(term), buf[16]);
 			if (buf[0] < 17)
@@ -1121,7 +1120,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 	case 0x03:  /* OUTPUT_TERMINAL */
 		printf("(OUTPUT_TERMINAL)\n");
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			get_string(dev, term, sizeof(term), buf[8]);
 			termt = buf[4] | (buf[5] << 8);
 			get_audioterminal_string(termts, sizeof(termts), termt);
@@ -1135,7 +1134,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			       buf[3], termt, termts, buf[6], buf[7], buf[8], term);
 			dump_junk(buf, "        ", 9);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			get_string(dev, term, sizeof(term), buf[11]);
 			termt = buf[4] | (buf[5] << 8);
 			get_audioterminal_string(termts, sizeof(termts), termt);
@@ -1160,7 +1159,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 		printf("(MIXER_UNIT)\n");
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			j = buf[4];
 			k = buf[j+5];
 			if (j == 0 || k == 0) {
@@ -1193,7 +1192,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			dump_junk(buf, "        ", 10+j+N);
 			break;
 
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			j = buf[4];
 			k = buf[0] - 13 - j;
 			get_string(dev, chnames, sizeof(chnames), buf[10+j]);
@@ -1230,7 +1229,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 	case 0x05:  /* SELECTOR_UNIT */
 		printf("(SELECTOR_UNIT)\n");
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			if (buf[0] < 6+buf[4])
 				printf("      Warning: Descriptor too short\n");
 			get_string(dev, term, sizeof(term), buf[5+buf[4]]);
@@ -1244,7 +1243,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			       buf[5+buf[4]], term);
 			dump_junk(buf, "        ", 6+buf[4]);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			if (buf[0] < 7+buf[4])
 				printf("      Warning: Descriptor too short\n");
 			get_string(dev, term, sizeof(term), buf[6+buf[4]]);
@@ -1268,7 +1267,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 		printf("(FEATURE_UNIT)\n");
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			j = buf[5];
 			if (!j)
 				j = 1;
@@ -1292,7 +1291,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			printf("        iFeature            %5u %s\n", buf[6+buf[5]*k], term);
 			dump_junk(buf, "        ", 7+buf[5]*k);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			if (buf[0] < 10)
 				printf("      Warning: Descriptor too short\n");
 			k = (buf[0] - 6) / 4;
@@ -1319,7 +1318,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 		printf("(PROCESSING_UNIT)\n");
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			j = buf[6];
 			k = buf[11+j];
 			get_string(dev, chnames, sizeof(chnames), buf[10+j]);
@@ -1348,7 +1347,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			       "        Process-Specific    ", buf[12+j+k], term);
 			dump_bytes(buf+(13+j+k), buf[0]-(13+j+k));
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			j = buf[6];
 			k = buf[0] - 17 - j;
 			get_string(dev, chnames, sizeof(chnames), buf[12+j]);
@@ -1385,7 +1384,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 		printf("(EXTENSION_UNIT)\n");
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			j = buf[6];
 			k = buf[11+j];
 			get_string(dev, chnames, sizeof(chnames), buf[10+j]);
@@ -1414,7 +1413,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 			       buf[12+j+k], term);
 			dump_junk(buf, "        ", 13+j+k);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			j = buf[6];
 			get_string(dev, chnames, sizeof(chnames), buf[13+j]);
 			get_string(dev, term, sizeof(term), buf[15+j]);
@@ -1446,7 +1445,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 
 	case 0x0a:  /* CLOCK_SOURCE */
 		printf ("(CLOCK_SOURCE)\n");
-		if (protocol != USB_AUDIO_CLASS_2)
+		if (protocol != LIBUSB_AUDIO_CLASS_2)
 			printf("      Warning: CLOCK_SOURCE descriptors are illegal for UAC1\n");
 
 		if (buf[0] < 8)
@@ -1468,7 +1467,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 
 	case 0x0b:  /* CLOCK_SELECTOR */
 		printf("(CLOCK_SELECTOR)\n");
-		if (protocol != USB_AUDIO_CLASS_2)
+		if (protocol != LIBUSB_AUDIO_CLASS_2)
 			printf("      Warning: CLOCK_SELECTOR descriptors are illegal for UAC1\n");
 
 		if (buf[0] < 7+buf[4])
@@ -1490,7 +1489,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 
 	case 0x0c:  /* CLOCK_MULTIPLIER */
 		printf("(CLOCK_MULTIPLIER)\n");
-		if (protocol != USB_AUDIO_CLASS_2)
+		if (protocol != LIBUSB_AUDIO_CLASS_2)
 			printf("      Warning: CLOCK_MULTIPLIER descriptors are illegal for UAC1\n");
 
 		if (buf[0] < 7)
@@ -1510,7 +1509,7 @@ static void dump_audiocontrol_interface(struct usb_dev_handle *dev, const unsign
 
 	case 0x0d:  /* SAMPLE_RATE_CONVERTER_UNIT */
 		printf("(SAMPLE_RATE_CONVERTER_UNIT)\n");
-		if (protocol != USB_AUDIO_CLASS_2)
+		if (protocol != LIBUSB_AUDIO_CLASS_2)
 			printf("      Warning: SAMPLE_RATE_CONVERTER_UNIT descriptors are illegal for UAC1\n");
 
 		if (buf[0] < 8)
@@ -1563,7 +1562,7 @@ static const struct bmcontrol uac2_as_interface_bmcontrols[] = {
 	{ NULL }
 };
 
-static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsigned char *buf, int protocol)
+static void dump_audiostreaming_interface(libusb_device_handle *dev, const unsigned char *buf, int protocol)
 {
 	static const char * const fmtItag[] = {
 		"TYPE_I_UNDEFINED", "PCM", "PCM8", "IEEE_FLOAT", "ALAW", "MULAW" };
@@ -1576,7 +1575,7 @@ static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsi
 	const char *fmtptr = "undefined";
 	char name[128];
 
-	if (buf[1] != USB_DT_CS_INTERFACE)
+	if (buf[1] != LIBUSB_DT_CS_INTERFACE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 3)
 		printf("      Warning: Descriptor too short\n");
@@ -1590,7 +1589,7 @@ static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsi
 		printf("(AS_GENERAL)\n");
 
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			if (buf[0] < 7)
 				printf("      Warning: Descriptor too short\n");
 			fmttag = buf[5] | (buf[6] << 8);
@@ -1606,7 +1605,7 @@ static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsi
 			       buf[3], buf[4], fmttag, fmtptr);
 			dump_junk(buf, "        ", 7);
 			break;
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			if (buf[0] < 16)
 				printf("      Warning: Descriptor too short\n");
 			printf("        bTerminalLink       %5u\n"
@@ -1640,7 +1639,7 @@ static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsi
 	case 0x02: /* FORMAT_TYPE */
 		printf("(FORMAT_TYPE)\n");
 		switch (protocol) {
-		case USB_AUDIO_CLASS_1:
+		case LIBUSB_AUDIO_CLASS_1:
 			if (buf[0] < 8)
 				printf("      Warning: Descriptor too short\n");
 			printf("        bFormatType         %5u ", buf[3]);
@@ -1715,7 +1714,7 @@ static void dump_audiostreaming_interface(struct usb_dev_handle *dev, const unsi
 
 			break;
 
-		case USB_AUDIO_CLASS_2:
+		case LIBUSB_AUDIO_CLASS_2:
 			printf("        bFormatType         %5u ", buf[3]);
 			switch (buf[3]) {
 			case 0x01: /* FORMAT_TYPE_I */
@@ -1903,9 +1902,9 @@ static void dump_audiostreaming_endpoint(const unsigned char *buf, int protocol)
 	static const char * const lockdelunits[] = { "Undefined", "Milliseconds", "Decoded PCM samples", "Reserved" };
 	unsigned int lckdelidx;
 
-	if (buf[1] != USB_DT_CS_ENDPOINT)
+	if (buf[1] != LIBUSB_DT_CS_ENDPOINT)
 		printf("      Warning: Invalid descriptor\n");
-	else if (buf[0] < ((protocol == USB_AUDIO_CLASS_1) ? 7 : 8))
+	else if (buf[0] < ((protocol == LIBUSB_AUDIO_CLASS_1) ? 7 : 8))
 		printf("      Warning: Descriptor too short\n");
 	printf("        AudioControl Endpoint Descriptor:\n"
 	       "          bLength             %5u\n"
@@ -1915,7 +1914,7 @@ static void dump_audiostreaming_endpoint(const unsigned char *buf, int protocol)
 	       buf[0], buf[1], buf[2], buf[2] == 1 ? "EP_GENERAL" : "invalid", buf[3]);
 
 	switch (protocol) {
-	case USB_AUDIO_CLASS_1:
+	case LIBUSB_AUDIO_CLASS_1:
 		if (buf[3] & 1)
 			printf("            Sampling Frequency\n");
 		if (buf[3] & 2)
@@ -1931,7 +1930,7 @@ static void dump_audiostreaming_endpoint(const unsigned char *buf, int protocol)
 		dump_junk(buf, "        ", 7);
 		break;
 
-	case USB_AUDIO_CLASS_2:
+	case LIBUSB_AUDIO_CLASS_2:
 		if (buf[3] & 128)
 			printf("            MaxPacketsOnly\n");
 
@@ -1949,14 +1948,14 @@ static void dump_audiostreaming_endpoint(const unsigned char *buf, int protocol)
 	} /* switch protocol */
 }
 
-static void dump_midistreaming_interface(struct usb_dev_handle *dev, const unsigned char *buf)
+static void dump_midistreaming_interface(libusb_device_handle *dev, const unsigned char *buf)
 {
 	static const char * const jacktypes[] = {"Undefined", "Embedded", "External"};
 	char jackstr[128];
 	unsigned int j, tlength, capssize;
 	unsigned long caps;
 
-	if (buf[1] != USB_DT_CS_INTERFACE)
+	if (buf[1] != LIBUSB_DT_CS_INTERFACE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 3)
 		printf("      Warning: Descriptor too short\n");
@@ -2075,7 +2074,7 @@ static void dump_midistreaming_endpoint(const unsigned char *buf)
 {
 	unsigned int j;
 
-	if (buf[1] != USB_DT_CS_ENDPOINT)
+	if (buf[1] != LIBUSB_DT_CS_ENDPOINT)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 5)
 		printf("      Warning: Descriptor too short\n");
@@ -2094,7 +2093,7 @@ static void dump_midistreaming_endpoint(const unsigned char *buf)
  * Video Class descriptor dump
  */
 
-static void dump_videocontrol_interface(struct usb_dev_handle *dev, const unsigned char *buf)
+static void dump_videocontrol_interface(libusb_device_handle *dev, const unsigned char *buf)
 {
 	static const char * const ctrlnames[] = {
 		"Brightness", "Contrast", "Hue", "Saturation", "Sharpness", "Gamma",
@@ -2117,7 +2116,7 @@ static void dump_videocontrol_interface(struct usb_dev_handle *dev, const unsign
 	unsigned int i, ctrls, stds, n, p, termt, freq;
 	char term[128], termts[128];
 
-	if (buf[1] != USB_DT_CS_INTERFACE)
+	if (buf[1] != LIBUSB_DT_CS_INTERFACE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 3)
 		printf("      Warning: Descriptor too short\n");
@@ -2277,7 +2276,7 @@ static void dump_videostreaming_interface(const unsigned char *buf)
 		"FCC", "BT.470-2 (B,G)", "SMPTE 170M (BT.601)", "SMPTE 240M" };
 	unsigned int i, m, n, p, flags, len;
 
-	if (buf[1] != USB_DT_CS_INTERFACE)
+	if (buf[1] != LIBUSB_DT_CS_INTERFACE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 3)
 		printf("      Warning: Descriptor too short\n");
@@ -2500,7 +2499,7 @@ static void dump_videostreaming_interface(const unsigned char *buf)
 
 static void dump_dfu_interface(const unsigned char *buf)
 {
-	if (buf[1] != USB_DT_CS_DEVICE)
+	if (buf[1] != LIBUSB_DT_CS_DEVICE)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 7)
 		printf("      Warning: Descriptor too short\n");
@@ -2862,15 +2861,15 @@ static void dump_report_desc(unsigned char *b, int l)
 	}
 }
 
-static void dump_hid_device(struct usb_dev_handle *dev,
-			    const struct usb_interface_descriptor *interface,
+static void dump_hid_device(libusb_device_handle *dev,
+			    const struct libusb_interface_descriptor *interface,
 			    const unsigned char *buf)
 {
 	unsigned int i, len;
 	unsigned int n;
 	unsigned char dbuf[8192];
 
-	if (buf[1] != USB_DT_HID)
+	if (buf[1] != LIBUSB_DT_HID)
 		printf("      Warning: Invalid descriptor\n");
 	else if (buf[0] < 6+3*buf[5])
 		printf("      Warning: Descriptor too short\n");
@@ -2899,22 +2898,22 @@ static void dump_hid_device(struct usb_dev_handle *dev,
 
 	for (i = 0; i < buf[5]; i++) {
 		/* we are just interested in report descriptors*/
-		if (buf[6+3*i] != USB_DT_REPORT)
+		if (buf[6+3*i] != LIBUSB_DT_REPORT)
 			continue;
 		len = buf[7+3*i] | (buf[8+3*i] << 8);
 		if (len > (unsigned int)sizeof(dbuf)) {
 			printf("report descriptor too long\n");
 			continue;
 		}
-		if (usb_claim_interface(dev, interface->bInterfaceNumber) == 0) {
+		if (libusb_claim_interface(dev, interface->bInterfaceNumber) == 0) {
 			int retries = 4;
 			n = 0;
 			while (n < len && retries--)
 				n = usb_control_msg(dev,
-					 USB_ENDPOINT_IN | USB_TYPE_STANDARD
-						| USB_RECIP_INTERFACE,
-					 USB_REQ_GET_DESCRIPTOR,
-					 (USB_DT_REPORT << 8),
+					 LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+						| LIBUSB_RECIPIENT_INTERFACE,
+					 LIBUSB_REQUEST_GET_DESCRIPTOR,
+					 (LIBUSB_DT_REPORT << 8),
 					 interface->bInterfaceNumber,
 					 dbuf, len,
 					 CTRL_TIMEOUT);
@@ -2924,7 +2923,7 @@ static void dump_hid_device(struct usb_dev_handle *dev,
 					printf("          Warning: incomplete report descriptor\n");
 				dump_report_desc(dbuf, n);
 			}
-			usb_release_interface(dev, interface->bInterfaceNumber);
+			libusb_release_interface(dev, interface->bInterfaceNumber);
 		} else {
 			/* recent Linuxes require claim() for RECIP_INTERFACE,
 			 * so "rmmod hid" will often make these available.
@@ -2936,7 +2935,7 @@ static void dump_hid_device(struct usb_dev_handle *dev,
 }
 
 static char *
-dump_comm_descriptor(struct usb_dev_handle *dev, const unsigned char *buf, char *indent)
+dump_comm_descriptor(libusb_device_handle *dev, const unsigned char *buf, char *indent)
 {
 	int		tmp;
 	char		str[128];
@@ -3155,7 +3154,7 @@ bad:
 
 /* ---------------------------------------------------------------------- */
 
-static void do_hub(struct usb_dev_handle *fd, unsigned tt_type, unsigned speed)
+static void do_hub(libusb_device_handle *fd, unsigned tt_type, unsigned speed)
 {
 	unsigned char buf[7 /* base descriptor */
 			+ 2 /* bitmasks */ * HUB_STATUS_BYTELEN];
@@ -3183,8 +3182,8 @@ static void do_hub(struct usb_dev_handle *fd, unsigned tt_type, unsigned speed)
 		value = 0x29;
 
 	ret = usb_control_msg(fd,
-			USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_DEVICE,
-			USB_REQ_GET_DESCRIPTOR,
+			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_DESCRIPTOR,
 			value << 8, 0,
 			buf, sizeof buf, CTRL_TIMEOUT);
 	if (ret < 9 /* at least one port's bitmasks */) {
@@ -3204,9 +3203,9 @@ static void do_hub(struct usb_dev_handle *fd, unsigned tt_type, unsigned speed)
 		unsigned char status[4];
 
 		ret = usb_control_msg(fd,
-				USB_ENDPOINT_IN | USB_TYPE_CLASS
-					| USB_RECIP_OTHER,
-				USB_REQ_GET_STATUS,
+				LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS
+					| LIBUSB_RECIPIENT_OTHER,
+				LIBUSB_REQUEST_GET_STATUS,
 				0, i + 1,
 				status, sizeof status,
 				CTRL_TIMEOUT);
@@ -3265,16 +3264,16 @@ static void do_hub(struct usb_dev_handle *fd, unsigned tt_type, unsigned speed)
 	}
 }
 
-static void do_dualspeed(struct usb_dev_handle *fd)
+static void do_dualspeed(libusb_device_handle *fd)
 {
 	unsigned char buf[10];
 	char cls[128], subcls[128], proto[128];
 	int ret;
 
 	ret = usb_control_msg(fd,
-			USB_ENDPOINT_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
-			USB_REQ_GET_DESCRIPTOR,
-			USB_DT_DEVICE_QUALIFIER << 8, 0,
+			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_DESCRIPTOR,
+			LIBUSB_DT_DEVICE_QUALIFIER << 8, 0,
 			buf, sizeof buf, CTRL_TIMEOUT);
 	if (ret < 0 && errno != EPIPE)
 		perror("can't get device qualifier");
@@ -3282,7 +3281,7 @@ static void do_dualspeed(struct usb_dev_handle *fd)
 	/* all dual-speed devices have a qualifier */
 	if (ret != sizeof buf
 			|| buf[0] != ret
-			|| buf[1] != USB_DT_DEVICE_QUALIFIER)
+			|| buf[1] != LIBUSB_DT_DEVICE_QUALIFIER)
 		return;
 
 	get_class_string(cls, sizeof(cls),
@@ -3310,15 +3309,15 @@ static void do_dualspeed(struct usb_dev_handle *fd)
 	/* FIXME also show the OTHER_SPEED_CONFIG descriptors */
 }
 
-static void do_debug(struct usb_dev_handle *fd)
+static void do_debug(libusb_device_handle *fd)
 {
 	unsigned char buf[4];
 	int ret;
 
 	ret = usb_control_msg(fd,
-			USB_ENDPOINT_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
-			USB_REQ_GET_DESCRIPTOR,
-			USB_DT_DEBUG << 8, 0,
+			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD | LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_DESCRIPTOR,
+			LIBUSB_DT_DEBUG << 8, 0,
 			buf, sizeof buf, CTRL_TIMEOUT);
 	if (ret < 0 && errno != EPIPE)
 		perror("can't get debug descriptor");
@@ -3329,7 +3328,7 @@ static void do_debug(struct usb_dev_handle *fd)
 	 */
 	if (ret != sizeof buf
 			|| buf[0] != ret
-			|| buf[1] != USB_DT_DEBUG)
+			|| buf[1] != LIBUSB_DT_DEBUG)
 		return;
 
 	printf("Debug descriptor:\n"
@@ -3346,7 +3345,7 @@ static const unsigned char *find_otg(const unsigned char *buf, int buflen)
 	if (!buf)
 		return 0;
 	while (buflen >= 3) {
-		if (buf[0] == 3 && buf[1] == USB_DT_OTG)
+		if (buf[0] == 3 && buf[1] == LIBUSB_DT_OTG)
 			return buf;
 		if (buf[0] > buflen)
 			return 0;
@@ -3356,28 +3355,28 @@ static const unsigned char *find_otg(const unsigned char *buf, int buflen)
 	return 0;
 }
 
-static int do_otg(struct usb_config_descriptor *config)
+static int do_otg(struct libusb_config_descriptor *config)
 {
 	unsigned	i, k;
 	int		j;
 	const unsigned char	*desc;
 
 	/* each config of an otg device has an OTG descriptor */
-	desc = find_otg(config->extra, config->extralen);
+	desc = find_otg(config->extra, config->extra_length);
 	for (i = 0; !desc && i < config->bNumInterfaces; i++) {
-		const struct usb_interface *intf;
+		const struct libusb_interface *intf;
 
 		intf = &config->interface[i];
 		for (j = 0; !desc && j < intf->num_altsetting; j++) {
-			const struct usb_interface_descriptor *alt;
+			const struct libusb_interface_descriptor *alt;
 
 			alt = &intf->altsetting[j];
-			desc = find_otg(alt->extra, alt->extralen);
+			desc = find_otg(alt->extra, alt->extra_length);
 			for (k = 0; !desc && k < alt->bNumEndpoints; k++) {
-				const struct usb_endpoint_descriptor *ep;
+				const struct libusb_endpoint_descriptor *ep;
 
 				ep = &alt->endpoint[k];
-				desc = find_otg(ep->extra, ep->extralen);
+				desc = find_otg(ep->extra, ep->extra_length);
 			}
 		}
 	}
@@ -3398,14 +3397,14 @@ static int do_otg(struct usb_config_descriptor *config)
 }
 
 static void
-dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
+dump_device_status(libusb_device_handle *fd, int otg, int wireless)
 {
 	unsigned char status[8];
 	int ret;
 
-	ret = usb_control_msg(fd, USB_ENDPOINT_IN | USB_TYPE_STANDARD
-				| USB_RECIP_DEVICE,
-			USB_REQ_GET_STATUS,
+	ret = usb_control_msg(fd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+				| LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_STATUS,
 			0, 0,
 			status, 2,
 			CTRL_TIMEOUT);
@@ -3451,9 +3450,9 @@ dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
 	/* Wireless USB exposes FIVE different types of device status,
 	 * accessed by distinct wIndex values.
 	 */
-	ret = usb_control_msg(fd, USB_ENDPOINT_IN | USB_TYPE_STANDARD
-				| USB_RECIP_DEVICE,
-			USB_REQ_GET_STATUS,
+	ret = usb_control_msg(fd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+				| LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_STATUS,
 			0, 1 /* wireless status */,
 			status, 1,
 			CTRL_TIMEOUT);
@@ -3474,9 +3473,9 @@ dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
 	if (status[0] & (1 << 3))
 		printf("  Capture Packet\n");
 
-	ret = usb_control_msg(fd, USB_ENDPOINT_IN | USB_TYPE_STANDARD
-				| USB_RECIP_DEVICE,
-			USB_REQ_GET_STATUS,
+	ret = usb_control_msg(fd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+				| LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_STATUS,
 			0, 2 /* Channel Info */,
 			status, 1,
 			CTRL_TIMEOUT);
@@ -3491,9 +3490,9 @@ dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
 
 	/* 3=Received data: many bytes, for count packets or capture packet */
 
-	ret = usb_control_msg(fd, USB_ENDPOINT_IN | USB_TYPE_STANDARD
-				| USB_RECIP_DEVICE,
-			USB_REQ_GET_STATUS,
+	ret = usb_control_msg(fd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+				| LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_STATUS,
 			0, 3 /* MAS Availability */,
 			status, 8,
 			CTRL_TIMEOUT);
@@ -3507,9 +3506,9 @@ dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
 	printf("MAS Availability:    ");
 	dump_bytes(status, 8);
 
-	ret = usb_control_msg(fd, USB_ENDPOINT_IN | USB_TYPE_STANDARD
-				| USB_RECIP_DEVICE,
-			USB_REQ_GET_STATUS,
+	ret = usb_control_msg(fd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_STANDARD
+				| LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_STATUS,
 			0, 5 /* Current Transmit Power */,
 			status, 2,
 			CTRL_TIMEOUT);
@@ -3525,7 +3524,7 @@ dump_device_status(struct usb_dev_handle *fd, int otg, int wireless)
 	printf(" TxBeacon:     :     0x%02x\n", status[1]);
 }
 
-static int do_wireless(struct usb_dev_handle *fd)
+static int do_wireless(libusb_device_handle *fd)
 {
 	/* FIXME fetch and dump BOS etc */
 	if (fd)
@@ -3620,7 +3619,7 @@ static void dump_container_id_device_capability_desc(unsigned char *buf)
 			get_guid(&buf[4]));
 }
 
-static void dump_bos_descriptor(struct usb_dev_handle *fd)
+static void dump_bos_descriptor(libusb_device_handle *fd)
 {
 	/* Total for all known BOS descriptors is 43 bytes:
 	 * 6 bytes for Wireless USB, 7 bytes for USB 2.0 extension,
@@ -3633,9 +3632,9 @@ static void dump_bos_descriptor(struct usb_dev_handle *fd)
 
 	/* Get the first 5 bytes to get the wTotalLength field */
 	ret = usb_control_msg(fd,
-			USB_ENDPOINT_IN | USB_RECIP_DEVICE,
-			USB_REQ_GET_DESCRIPTOR,
-			USB_DT_BOS << 8, 0,
+			LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_DESCRIPTOR,
+			LIBUSB_DT_BOS << 8, 0,
 			bos_desc, 5, CTRL_TIMEOUT);
 	if (ret < 0)
 		return;
@@ -3662,9 +3661,9 @@ static void dump_bos_descriptor(struct usb_dev_handle *fd)
 	}
 
 	ret = usb_control_msg(fd,
-			USB_ENDPOINT_IN | USB_RECIP_DEVICE,
-			USB_REQ_GET_DESCRIPTOR,
-			USB_DT_BOS << 8, 0,
+			LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_DEVICE,
+			LIBUSB_REQUEST_GET_DESCRIPTOR,
+			LIBUSB_DT_BOS << 8, 0,
 			bos_desc, bos_desc_size, CTRL_TIMEOUT);
 	if (ret < 0) {
 		fprintf(stderr, "Couldn't get device capability descriptors\n");
@@ -3680,16 +3679,16 @@ static void dump_bos_descriptor(struct usb_dev_handle *fd)
 			return;
 		}
 		switch (buf[2]) {
-		case USB_DC_WIRELESS_USB:
+		case LIBUSB_DC_WIRELESS_USB:
 			/* FIXME */
 			break;
-		case USB_DC_20_EXTENSION:
+		case LIBUSB_DC_20_EXTENSION:
 			dump_usb2_device_capability_desc(buf);
 			break;
-		case USB_DC_SUPERSPEED:
+		case LIBUSB_DC_SUPERSPEED:
 			dump_ss_device_capability_desc(buf);
 			break;
-		case USB_DC_CONTAINER_ID:
+		case LIBUSB_DC_CONTAINER_ID:
 			dump_container_id_device_capability_desc(buf);
 			break;
 		default:
@@ -3702,102 +3701,114 @@ static void dump_bos_descriptor(struct usb_dev_handle *fd)
 	}
 }
 
-static void dumpdev(struct usb_device *dev)
+static void dumpdev(libusb_device *dev)
 {
-	struct usb_dev_handle *udev;
-	int i;
+	libusb_device_handle *udev;
+	struct libusb_device_descriptor desc;
+	int i, ret;
 	int otg, wireless;
 
 	otg = wireless = 0;
-	udev = usb_open(dev);
-	if (!udev)
+	ret = libusb_open(dev, &udev);
+	if (ret)
 		fprintf(stderr, "Couldn't open device, some information "
 			"will be missing\n");
 
-	dump_device(udev, &dev->descriptor);
-	if (dev->descriptor.bcdUSB == 0x0250)
+	libusb_get_device_descriptor(dev, &desc);
+	dump_device(udev, &desc);
+	if (desc.bcdUSB == 0x0250)
 		wireless = do_wireless(udev);
-	if (dev->config) {
-		otg = do_otg(&dev->config[0]) || otg;
-		for (i = 0; i < dev->descriptor.bNumConfigurations;
-				i++) {
-			dump_config(udev, &dev->config[i]);
+	if (desc.bNumConfigurations) {
+		struct libusb_config_descriptor *config;
+
+		libusb_get_config_descriptor(dev, 0, &config);
+		otg = do_otg(config) || otg;
+		libusb_free_config_descriptor(config);
+
+		for (i = 0; i < desc.bNumConfigurations; ++i) {
+			libusb_get_config_descriptor(dev, i, &config);
+			dump_config(udev, config);
+			libusb_free_config_descriptor(config);
 		}
 	}
-	if (!udev)
+	if (ret)
 		return;
 
-	if (dev->descriptor.bDeviceClass == USB_CLASS_HUB)
-		do_hub(udev, dev->descriptor.bDeviceProtocol, dev->descriptor.bcdUSB);
-	if (dev->descriptor.bcdUSB >= 0x0200) {
+	if (desc.bDeviceClass == LIBUSB_CLASS_HUB)
+		do_hub(udev, desc.bDeviceProtocol, desc.bcdUSB);
+	if (desc.bcdUSB >= 0x0200) {
 		do_dualspeed(udev);
 		do_debug(udev);
 	}
 	dump_device_status(udev, otg, wireless);
 	dump_bos_descriptor(udev);
-	usb_close(udev);
+	libusb_close(udev);
 }
 
 /* ---------------------------------------------------------------------- */
 
-static int dump_one_device(const char *path)
+static int dump_one_device(libusb_context *ctx, const char *path)
 {
-	struct usb_device *dev;
+	libusb_device *dev;
+	struct libusb_device_descriptor desc;
 	char vendor[128], product[128];
 
-	dev = get_usb_device(path);
+	dev = get_usb_device(ctx, path);
 	if (!dev) {
 		fprintf(stderr, "Cannot open %s\n", path);
 		return 1;
 	}
-	get_vendor_string(vendor, sizeof(vendor), dev->descriptor.idVendor);
-	get_product_string(product, sizeof(product), dev->descriptor.idVendor, dev->descriptor.idProduct);
-	printf("Device: ID %04x:%04x %s %s\n", dev->descriptor.idVendor,
-					       dev->descriptor.idProduct,
+	libusb_get_device_descriptor(dev, &desc);
+	get_vendor_string(vendor, sizeof(vendor), desc.idVendor);
+	get_product_string(product, sizeof(product), desc.idVendor, desc.idProduct);
+	printf("Device: ID %04x:%04x %s %s\n", desc.idVendor,
+					       desc.idProduct,
 					       vendor,
 					       product);
 	dumpdev(dev);
 	return 0;
 }
 
-static int list_devices(int busnum, int devnum, int vendorid, int productid)
+static int list_devices(libusb_context *ctx, int busnum, int devnum, int vendorid, int productid)
 {
-	struct usb_bus *bus;
-	struct usb_device *dev;
+	libusb_device **list;
+	struct libusb_device_descriptor desc;
 	char vendor[128], product[128];
 	int status;
+	ssize_t num_devs, i;
 
 	status = 1; /* 1 device not found, 0 device found */
 
-	for (bus = usb_busses; bus; bus = bus->next) {
-		if (busnum != -1 && strtol(bus->dirname, NULL, 10) != busnum)
+	num_devs = libusb_get_device_list(ctx, &list);
+
+	for (i = 0; i < num_devs; ++i) {
+		libusb_device *dev = list[i];
+		uint8_t bnum = libusb_get_bus_number(dev);
+		uint8_t dnum = libusb_get_device_address(dev);
+
+		if ((busnum != -1 && busnum != bnum) ||
+		    (devnum != -1 && devnum != dnum))
 			continue;
-		for (dev = bus->devices; dev; dev = dev->next) {
-			if (devnum != -1 && strtol(dev->filename, NULL, 10)
-					!= devnum)
-				continue;
-			if ((vendorid != -1 && vendorid
-						!= dev->descriptor.idVendor)
-					|| (productid != -1 && productid
-						!= dev->descriptor.idProduct))
-				continue;
-			status = 0;
-			get_vendor_string(vendor, sizeof(vendor),
-					dev->descriptor.idVendor);
-			get_product_string(product, sizeof(product),
-					dev->descriptor.idVendor,
-					dev->descriptor.idProduct);
-			if (verblevel > 0)
-				printf("\n");
-			printf("Bus %s Device %s: ID %04x:%04x %s %s\n",
-					bus->dirname, dev->filename,
-					dev->descriptor.idVendor,
-					dev->descriptor.idProduct,
-					vendor, product);
-			if (verblevel > 0)
-				dumpdev(dev);
-		}
+		libusb_get_device_descriptor(dev, &desc);
+		if ((vendorid != -1 && vendorid != desc.idVendor) ||
+		    (productid != -1 && productid != desc.idProduct))
+			continue;
+		status = 0;
+		get_vendor_string(vendor, sizeof(vendor), desc.idVendor);
+		get_product_string(product, sizeof(product),
+				desc.idVendor, desc.idProduct);
+		if (verblevel > 0)
+			printf("\n");
+		printf("Bus %03u Device %03u: ID %04x:%04x %s %s\n",
+				bnum, dnum,
+				desc.idVendor,
+				desc.idProduct,
+				vendor, product);
+		if (verblevel > 0)
+			dumpdev(dev);
 	}
+
+	libusb_free_device_list(list, 0);
 	return status;
 }
 
@@ -3850,6 +3861,7 @@ int main(int argc, char *argv[])
 		{ "verbose", 0, 0, 'v' },
 		{ 0, 0, 0, 0 }
 	};
+	libusb_context *ctx;
 	int c, err = 0;
 	unsigned int allowctrlmsg = 0, treemode = 0;
 	int bus = -1, devnum = -1, vendor = -1, product = -1;
@@ -3947,20 +3959,22 @@ int main(int argc, char *argv[])
 				strerror(err));
 	status = 0;
 
-	usb_init();
-
-	usb_find_busses();
-	usb_find_devices();
+	err = libusb_init(&ctx);
+	if (err) {
+		fprintf(stderr, "unable to initialize libusb: %i\n", err);
+		return EXIT_FAILURE;
+	}
 
 	if (treemode) {
 		/* treemode requires at least verblevel 1 */
 		verblevel += 1 - VERBLEVEL_DEFAULT;
 		status = treedump();
 	} else if (devdump)
-		status = dump_one_device(devdump);
+		status = dump_one_device(ctx, devdump);
 	else
-		status = list_devices(bus, devnum, vendor, product);
+		status = list_devices(ctx, bus, devnum, vendor, product);
 
 	names_exit();
+	libusb_exit(ctx);
 	return status;
 }
diff --git a/usbmisc.c b/usbmisc.c
index 84791c9..bafb623 100644
--- a/usbmisc.c
+++ b/usbmisc.c
@@ -119,24 +119,32 @@ static char *get_absolute_path(const char *path, char *result,
 	return result;
 }
 
-struct usb_device *get_usb_device(const char *path)
+libusb_device *get_usb_device(libusb_context *ctx, const char *path)
 {
-	struct usb_bus *bus;
-	struct usb_device *dev;
+	libusb_device **list;
+	libusb_device *dev;
+	ssize_t num_devs, i;
 	char device_path[PATH_MAX + 1];
 	char absolute_path[PATH_MAX + 1];
 
 	readlink_recursive(path, device_path, sizeof(device_path));
 	get_absolute_path(device_path, absolute_path, sizeof(absolute_path));
 
-	for (bus = usb_busses; bus; bus = bus->next) {
-		for (dev = bus->devices; dev; dev = dev->next) {
-			snprintf(device_path, sizeof(device_path), "%s/%s/%s",
-				 devbususb, bus->dirname, dev->filename);
-			if (!strcmp(device_path, absolute_path))
-				return dev;
+	dev = NULL;
+	num_devs = libusb_get_device_list(ctx, &list);
+
+	for (i = 0; i < num_devs; ++i) {
+		uint8_t bnum = libusb_get_bus_number(list[i]);
+		uint8_t dnum = libusb_get_device_address(list[i]);
+
+		snprintf(device_path, sizeof(device_path), "%s/%03u/%03u",
+			 devbususb, bnum, dnum);
+		if (!strcmp(device_path, absolute_path)) {
+			dev = list[i];
+			break;
 		}
 	}
-	return NULL;
-}
 
+	libusb_free_device_list(list, 0);
+	return dev;
+}
diff --git a/usbmisc.h b/usbmisc.h
index 39d4926..8c09431 100644
--- a/usbmisc.h
+++ b/usbmisc.h
@@ -26,11 +26,11 @@
 #ifndef _USBMISC_H
 #define _USBMISC_H
 
-#include <usb.h>
+#include <libusb.h>
 
 /* ---------------------------------------------------------------------- */
 
-extern struct usb_device *get_usb_device(const char *path);
+extern libusb_device *get_usb_device(libusb_context *ctx, const char *path);
 
 /* ---------------------------------------------------------------------- */
 #endif /* _USBMISC_H */
-- 
1.7.3.2

--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux