usbdev: Extract libusb1 device discovery into a separate file

Currently there is a TODO-like comment in the dediprog driver: "Might be
useful for other USB devices as well". Act on this comment by collecting
all the device discovery code for libusb1 devices into a separate file.

Change-Id: Idfcc79371241c2c1dea97faf5e532aa971546a79
Signed-off-by: Daniel Thompson <daniel.thompson@linaro.org>
Reviewed-on: https://review.coreboot.org/27443
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
diff --git a/developerbox_spi.c b/developerbox_spi.c
index 8482dea..4ad966e 100644
--- a/developerbox_spi.c
+++ b/developerbox_spi.c
@@ -34,7 +34,6 @@
 #include "platform.h"
 
 #include <stdlib.h>
-#include <string.h>
 #include <libusb.h>
 #include "programmer.h"
 #include "spi.h"
@@ -130,67 +129,6 @@
 	.set_sck_set_mosi = cp210x_bitbang_set_sck_set_mosi,
 };
 
-static struct libusb_device_handle *get_device_by_vid_pid_serial(uint16_t vid, uint16_t pid,
-								 const char *serialno)
-{
-	struct libusb_device **list;
-	ssize_t count = libusb_get_device_list(usb_ctx, &list);
-	if (count < 0) {
-		msg_perr("Getting the USB device list failed (%s)!\n", libusb_error_name(count));
-		return NULL;
-	}
-
-	ssize_t i = 0;
-	for (i = 0; i < count; i++) {
-		struct libusb_device *dev = list[i];
-		struct libusb_device_descriptor desc;
-		struct libusb_device_handle *handle;
-
-		int res = libusb_get_device_descriptor(dev, &desc);
-		if (res != 0) {
-			msg_perr("Reading the USB device descriptor failed (%s)!\n", libusb_error_name(res));
-			continue;
-		}
-
-		if ((desc.idVendor != vid) && (desc.idProduct != pid))
-			continue;
-
-		msg_pdbg("Found USB device %04"PRIx16":%04"PRIx16" at address %d-%d.\n",
-			 desc.idVendor, desc.idProduct,
-			 libusb_get_bus_number(dev), libusb_get_device_address(dev));
-
-		res = libusb_open(dev, &handle);
-		if (res != 0) {
-			msg_perr("Opening the USB device failed (%s)!\n", libusb_error_name(res));
-			continue;
-		}
-
-		if (serialno) {
-			unsigned char myserial[64];
-			res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, myserial,
-								 sizeof(myserial));
-			if (res < 0) {
-				msg_perr("Reading the USB serialno failed (%s)!\n", libusb_error_name(res));
-				libusb_close(handle);
-				continue;
-			}
-			msg_pdbg("Serial number is %s\n", myserial);
-
-			/* Filter out any serial number that does not commence with serialno */
-			if (0 != strncmp(serialno, (char *) myserial, strlen(serialno))) {
-				libusb_close(handle);
-				continue;
-			}
-		}
-
-		libusb_free_device_list(list, 1);
-		return handle;
-	}
-
-	libusb_free_device_list(list, 1);
-	return NULL;
-}
-
 static int developerbox_spi_shutdown(void *data)
 {
 	libusb_close(cp210x_handle);
@@ -210,7 +148,7 @@
 	char *serialno = extract_programmer_param("serial");
 	if (serialno)
 		msg_pdbg("Looking for serial number commencing %s\n", serialno);
-	cp210x_handle = get_device_by_vid_pid_serial(
+	cp210x_handle = usb_dev_get_by_vid_pid_serial(usb_ctx,
 			devs_developerbox_spi[0].vendor_id, devs_developerbox_spi[0].device_id, serialno);
 	free(serialno);
 	if (!cp210x_handle) {