From b2c987945e6a6a4112fbc0ef297d0bf13b550ad7 Mon Sep 17 00:00:00 2001
From: Jakob Bornecrantz <jakob@collabora.com>
Date: Sat, 25 Apr 2020 11:29:28 +0100
Subject: [PATCH] st/prober: Use and expose usb serial and manufacturer

---
 src/xrt/state_trackers/prober/p_dump.c   | 43 +++++++++++++++---------
 src/xrt/state_trackers/prober/p_prober.c | 13 ++++++-
 src/xrt/state_trackers/prober/p_prober.h |  2 ++
 src/xrt/state_trackers/prober/p_udev.c   | 13 ++++++-
 4 files changed, 53 insertions(+), 18 deletions(-)

diff --git a/src/xrt/state_trackers/prober/p_dump.c b/src/xrt/state_trackers/prober/p_dump.c
index 42e24dfd6..749e2824f 100644
--- a/src/xrt/state_trackers/prober/p_dump.c
+++ b/src/xrt/state_trackers/prober/p_dump.c
@@ -74,27 +74,36 @@ p_dump_device(struct prober *p, struct prober_device *pdev, int id)
 
 	printf("\t% 3i: 0x%04x:0x%04x\n", id, pdev->base.vendor_id,
 	       pdev->base.product_id);
-	printf("\t\tptr:           %p\n", (void *)pdev);
-	printf("\t\tusb_dev_class: %02x\n", pdev->base.usb_dev_class);
+	printf("\t\tptr:              %p\n", (void *)pdev);
+	printf("\t\tusb_dev_class:    %02x\n", pdev->base.usb_dev_class);
+
+
+	if (pdev->usb.serial != NULL || pdev->usb.product != NULL ||
+	    pdev->usb.manufacturer != NULL) {
+		printf("\t\tusb.product:      %s\n", pdev->usb.product);
+		printf("\t\tusb.manufacturer: %s\n", pdev->usb.manufacturer);
+		printf("\t\tusb.serial:       %s\n", pdev->usb.serial);
+	}
 
 	if (pdev->usb.bus != 0 || pdev->usb.addr != 0) {
-		printf("\t\tusb.bus:       %i\n", pdev->usb.bus);
-		printf("\t\tusb.addr:      %i\n", pdev->usb.addr);
+		printf("\t\tusb.bus:          %i\n", pdev->usb.bus);
+		printf("\t\tusb.addr:         %i\n", pdev->usb.addr);
 	}
 
 	if (pdev->bluetooth.id != 0) {
-		printf("\t\tbluetooth.id:  %012" PRIx64 "\n",
+		printf("\t\tbluetooth.id:     %012" PRIx64 "\n",
 		       pdev->bluetooth.id);
 	}
 
 	int num = pdev->usb.num_ports;
 	if (print_ports(tmp, ARRAY_SIZE(tmp), pdev->usb.ports, num)) {
-		printf("\t\tport%s         %s\n", num > 1 ? "s:" : ": ", tmp);
+		printf("\t\tport%s            %s\n", num > 1 ? "s:" : ": ",
+		       tmp);
 	}
 
 #ifdef XRT_HAVE_LIBUSB
 	if (pdev->usb.dev != NULL) {
-		printf("\t\tlibusb:        %p\n", (void *)pdev->usb.dev);
+		printf("\t\tlibusb:           %p\n", (void *)pdev->usb.dev);
 	}
 #endif
 
@@ -103,21 +112,23 @@ p_dump_device(struct prober *p, struct prober_device *pdev, int id)
 	if (uvc_dev != NULL) {
 		struct uvc_device_descriptor *desc;
 
-		printf("\t\tlibuvc:        %p\n", (void *)uvc_dev);
+		printf("\t\tlibuvc:           %p\n", (void *)uvc_dev);
 
 		uvc_get_device_descriptor(uvc_dev, &desc);
 
 		if (desc->product != NULL) {
 
-			printf("\t\tproduct:       '%s'\n", desc->product);
+			printf("\t\tproduct:          '%s'\n", desc->product);
 		}
 		if (desc->manufacturer != NULL) {
 
-			printf("\t\tmanufacturer:  '%s'\n", desc->manufacturer);
+			printf("\t\tmanufacturer:     '%s'\n",
+			       desc->manufacturer);
 		}
 		if (desc->serialNumber != NULL) {
 
-			printf("\t\tserial:        '%s'\n", desc->serialNumber);
+			printf("\t\tserial:           '%s'\n",
+			       desc->serialNumber);
 		}
 
 		uvc_free_device_descriptor(desc);
@@ -129,9 +140,9 @@ p_dump_device(struct prober *p, struct prober_device *pdev, int id)
 	for (size_t j = 0; j < pdev->num_v4ls; j++) {
 		struct prober_v4l *v4l = &pdev->v4ls[j];
 
-		printf("\t\tv4l.iface:     %i\n", (int)v4l->usb_iface);
-		printf("\t\tv4l.index:     %i\n", (int)v4l->v4l_index);
-		printf("\t\tv4l.path:      '%s'\n", v4l->path);
+		printf("\t\tv4l.iface:        %i\n", (int)v4l->usb_iface);
+		printf("\t\tv4l.index:        %i\n", (int)v4l->v4l_index);
+		printf("\t\tv4l.path:         '%s'\n", v4l->path);
 	}
 #endif
 
@@ -139,8 +150,8 @@ p_dump_device(struct prober *p, struct prober_device *pdev, int id)
 	for (size_t j = 0; j < pdev->num_hidraws; j++) {
 		struct prober_hidraw *hidraw = &pdev->hidraws[j];
 
-		printf("\t\thidraw.iface:  %i\n", (int)hidraw->interface);
-		printf("\t\thidraw.path:   '%s'\n", hidraw->path);
+		printf("\t\thidraw.iface:     %i\n", (int)hidraw->interface);
+		printf("\t\thidraw.path:      '%s'\n", hidraw->path);
 	}
 #endif
 }
diff --git a/src/xrt/state_trackers/prober/p_prober.c b/src/xrt/state_trackers/prober/p_prober.c
index e02006934..a4ec82308 100644
--- a/src/xrt/state_trackers/prober/p_prober.c
+++ b/src/xrt/state_trackers/prober/p_prober.c
@@ -378,6 +378,16 @@ teardown_devices(struct prober *p)
 			pdev->usb.product = NULL;
 		}
 
+		if (pdev->usb.manufacturer != NULL) {
+			free((char *)pdev->usb.manufacturer);
+			pdev->usb.manufacturer = NULL;
+		}
+
+		if (pdev->usb.serial != NULL) {
+			free((char *)pdev->usb.serial);
+			pdev->usb.serial = NULL;
+		}
+
 		if (pdev->usb.path != NULL) {
 			free((char *)pdev->usb.path);
 			pdev->usb.path = NULL;
@@ -726,7 +736,8 @@ list_video_devices(struct xrt_prober *xp,
 			fill_out_product(p, pdev);
 		}
 
-		cb(xp, &pdev->base, pdev->usb.product, NULL, NULL, ptr);
+		cb(xp, &pdev->base, pdev->usb.product, pdev->usb.manufacturer,
+		   pdev->usb.serial, ptr);
 	}
 
 	return 0;
diff --git a/src/xrt/state_trackers/prober/p_prober.h b/src/xrt/state_trackers/prober/p_prober.h
index 237d7904b..155bcdb1e 100644
--- a/src/xrt/state_trackers/prober/p_prober.h
+++ b/src/xrt/state_trackers/prober/p_prober.h
@@ -93,6 +93,8 @@ struct prober_device
 
 #ifdef XRT_OS_LINUX
 		const char *product;
+		const char *manufacturer;
+		const char *serial;
 		const char *path;
 #endif
 
diff --git a/src/xrt/state_trackers/prober/p_udev.c b/src/xrt/state_trackers/prober/p_udev.c
index 71540ffa6..6cc19a3fa 100644
--- a/src/xrt/state_trackers/prober/p_udev.c
+++ b/src/xrt/state_trackers/prober/p_udev.c
@@ -41,6 +41,8 @@ static void
 p_udev_add_usb(struct prober_device *pdev,
                uint8_t dev_class,
                const char *product,
+               const char *manufacturer,
+               const char *serial,
                const char *path);
 
 static void
@@ -234,7 +236,8 @@ p_udev_enumerate_usb(struct prober *p, struct udev *udev)
 		}
 
 		// Add info to usb device.
-		p_udev_add_usb(pdev, dev_class, product, dev_path);
+		p_udev_add_usb(pdev, dev_class, product, manufacturer, serial,
+		               dev_path);
 
 	next:
 		udev_device_unref(raw_dev);
@@ -247,6 +250,8 @@ static void
 p_udev_add_usb(struct prober_device *pdev,
                uint8_t dev_class,
                const char *product,
+               const char *manufacturer,
+               const char *serial,
                const char *path)
 {
 	pdev->base.usb_dev_class = dev_class;
@@ -254,6 +259,12 @@ p_udev_add_usb(struct prober_device *pdev,
 	if (product != NULL) {
 		pdev->usb.product = strdup(product);
 	}
+	if (manufacturer != NULL) {
+		pdev->usb.manufacturer = strdup(manufacturer);
+	}
+	if (serial != NULL) {
+		pdev->usb.serial = strdup(serial);
+	}
 	if (path != NULL) {
 		pdev->usb.path = strdup(path);
 	}