changeset 10:fd3fcba5a8ac

libuwrap: implement locating by bus/dev
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 07 Sep 2023 04:18:30 +0000
parents ab506f6aa57c
children fe4231326fb2
files libuwrap/Makefile libuwrap/find_busdev.c
diffstat 2 files changed, 42 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libuwrap/Makefile	Thu Sep 07 04:00:56 2023 +0000
+++ b/libuwrap/Makefile	Thu Sep 07 04:18:30 2023 +0000
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	find_matchspec.o prelim_init.o
+OBJS=	find_busdev.o find_matchspec.o prelim_init.o
 LIB=	libuwrap.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libuwrap/find_busdev.c	Thu Sep 07 04:18:30 2023 +0000
@@ -0,0 +1,41 @@
+/*
+ * In this module we implement the function that locates a USB device
+ * by bus and device number/filename strings.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <usb.h>
+#include "find_dev.h"
+#include "prelim_init.h"
+
+struct usb_device *
+find_usbdev_by_busdev(const char *bus_dev_spec)
+{
+	const char *slash;
+	unsigned bus_name_len;
+	struct usb_bus *bus;
+	struct usb_device *dev;
+
+	slash = index(bus_dev_spec, '/');
+	if (!slash) {
+		fprintf(stderr,
+			"error: bus/dev path expected, but no slash found\n");
+		exit(1);
+	}
+	bus_name_len = slash - bus_dev_spec;
+	libusb_prelim_init();
+	for (bus = usb_get_busses(); bus; bus = bus->next) {
+		if (strlen(bus->dirname) != bus_name_len)
+			continue;
+		if (strncmp(bus->dirname, bus_dev_spec, bus_name_len))
+			continue;
+		for (dev = bus->devices; dev; dev = dev->next) {
+			if (!strcmp(dev->filename, slash + 1))
+				return dev;
+		}
+	}
+	return 0;
+}