FreeCalypso > hg > fc-usbser-tools
changeset 9:ab506f6aa57c
libuwrap started
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 07 Sep 2023 04:00:56 +0000 |
parents | 1415508e7ea2 |
children | fd3fcba5a8ac |
files | libuwrap/Makefile libuwrap/find_dev.h libuwrap/find_matchspec.c libuwrap/prelim_init.c libuwrap/prelim_init.h |
diffstat | 5 files changed, 151 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libuwrap/Makefile Thu Sep 07 04:00:56 2023 +0000 @@ -0,0 +1,13 @@ +CC= gcc +CFLAGS= -O2 +OBJS= find_matchspec.o prelim_init.o +LIB= libuwrap.a + +all: ${LIB} + +${LIB}: ${OBJS} + ar rcu $@ ${OBJS} + ranlib $@ + +clean: + rm -f *.[oa] errs
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libuwrap/find_dev.h Thu Sep 07 04:00:56 2023 +0000 @@ -0,0 +1,22 @@ +/* + * This header file contains definitions and declarations related to + * functions for locating specific FTDI/CP2102/etc USB devices. + */ + +struct usbdev_matchspec { + unsigned usb_vid; + unsigned usb_pid; + char *manuf_string; + char *product_string; + char *serial; + unsigned index; +}; + +extern struct usb_device * +find_usbdev_by_matchspec(const struct usbdev_matchspec *match); + +extern struct usb_device * +find_usbdev_by_busdev(const char *bus_dev_spec); + +extern struct usb_device * +find_usbdev_by_desc_string(const char *desc_string);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libuwrap/find_matchspec.c Thu Sep 07 04:00:56 2023 +0000 @@ -0,0 +1,92 @@ +/* + * In this module we implement the function that locates a USB device + * by matchspec structure: looking for specific VID/PID, possibly qualified + * by strings and/or index. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <usb.h> +#include "find_dev.h" +#include "prelim_init.h" + +static void +get_string(usb_dev_handle *usbh, int index, char *buf, size_t buflen) +{ + int rc; + + rc = usb_get_string_simple(usbh, index, buf, buflen); + if (rc <= 0) { + fprintf(stderr, "error: USB string retrieval failed\n"); + exit(1); + } +} + +static int +is_match(struct usb_device *dev, const struct usbdev_matchspec *match) +{ + struct usb_device_descriptor *desc = &dev->descriptor; + usb_dev_handle *usbh; + char strbuf[1024]; + + if (desc->idVendor != match->usb_vid) + return 0; + if (desc->idProduct != match->usb_pid) + return 0; + if (match->manuf_string || match->product_string || match->serial) { + usbh = usb_open(dev); + if (!usbh) { + fprintf(stderr, "error: usb_open() failed\n"); + exit(1); + } + if (match->manuf_string) { + get_string(usbh, desc->iManufacturer, + strbuf, sizeof strbuf); + if (strncmp(match->manuf_string, + strbuf, sizeof strbuf)) { + usb_close(usbh); + return 0; + } + } + if (match->product_string) { + get_string(usbh, desc->iProduct, strbuf, sizeof strbuf); + if (strncmp(match->product_string, + strbuf, sizeof strbuf)) { + usb_close(usbh); + return 0; + } + } + if (match->serial) { + get_string(usbh, desc->iSerialNumber, + strbuf, sizeof strbuf); + if (strncmp(match->serial, strbuf, sizeof strbuf)) { + usb_close(usbh); + return 0; + } + } + usb_close(usbh); + } + return 1; +} + +struct usb_device * +find_usbdev_by_matchspec(const struct usbdev_matchspec *match) +{ + struct usb_bus *bus; + struct usb_device *dev; + unsigned index = match->index; + + libusb_prelim_init(); + for (bus = usb_get_busses(); bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if (is_match(dev, match)) { + if (!index) + return dev; + index--; + } + } + } + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libuwrap/prelim_init.c Thu Sep 07 04:00:56 2023 +0000 @@ -0,0 +1,23 @@ +/* + * libftdi-0.20 performs these preliminary steps just before it goes + * looking for the device of interest via usb_get_busses() followed + * by traversal of the returned tree - hence we do likewise. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <usb.h> +#include "prelim_init.h" + +void libusb_prelim_init(void) +{ + usb_init(); + if (usb_find_busses() < 0) { + fprintf(stderr, "error: usb_find_busses() failed\n"); + exit(1); + } + if (usb_find_devices() < 0) { + fprintf(stderr, "error: usb_find_devices() failed\n"); + exit(1); + } +}