diff --git a/include/f32xbase/usb.h b/include/f32xbase/usb.h new file mode 100644 index 0000000..1864eb8 --- /dev/null +++ b/include/f32xbase/usb.h @@ -0,0 +1,23 @@ +/* + * include/f32xbase/usb.h - Common USB code for F32Xbase tools + * + * Written 2008-2010 by Werner Almesberger + * Copyright 2008-2010 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#ifndef F32XBASE_USB_H +#define F32XBASE_USB_H + +#include + + +usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product); +void parse_usb_id(const char *id); + +#endif /* !F32XBASE_USB_H */ diff --git a/lib/Makefile.common b/lib/Makefile.common new file mode 100644 index 0000000..f75e939 --- /dev/null +++ b/lib/Makefile.common @@ -0,0 +1,37 @@ +# +# Makefile.common - Common rules and definitions for all the tools +# +# Written 2008 by Werner Almesberger +# Copyright 2008 Werner Almesberger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# + + +INSTALL_PREFIX=/usr/local + +CFLAGS=-Wall +LDFLAGS=-lusb + +.PHONY: all install uninstall clean spotless + +all: $(MAIN) + +$(MAIN): $(MAIN).o $(OBJS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(MAIN).o $(OBJS) + +install: $(MAIN) + install $(MAIN) $(INSTALL_PREFIX)/bin/ + +uninstall: + rm -f $(INSTALL_PREFIX)/bin/$(MAIN) + +clean: + rm -f $(MAIN).o $(OBJS) + +spotless: clean + rm -f $(MAIN) + diff --git a/lib/usb.c b/lib/usb.c new file mode 100644 index 0000000..07475da --- /dev/null +++ b/lib/usb.c @@ -0,0 +1,72 @@ +/* + * lib/usb.c - Common USB code for F32Xbase tools + * + * Written 2008-2010 by Werner Almesberger + * Copyright 2008-2010 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + + +#include +#include +#include +#include + + +static uint16_t vendor = 0; +static uint16_t product = 0; + + +usb_dev_handle *open_usb(uint16_t default_vendor, uint16_t default_product) +{ + const struct usb_bus *bus; + struct usb_device *dev; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + for (bus = usb_get_busses(); bus; bus = bus->next) + for (dev = bus->devices; dev; dev = dev->next) { + if (dev->descriptor.idVendor != + vendor ? vendor : default_vendor) + continue; + if (dev->descriptor.idProduct != + product ? product : default_product) + continue; + return usb_open(dev); + } + return NULL; +} + + +static void bad_id(const char *id) +{ + fprintf(stderr, "\"%s\" is not a valid vendor:product ID\n", id); + exit(1); +} + + +void parse_usb_id(const char *id) +{ + unsigned long tmp; + char *end; + + tmp = strtoul(id, &end, 16); + if (*end != ':') + bad_id(id); + if (tmp > 0xffff) + bad_id(id); + vendor = tmp; + tmp = strtoul(end+1, &end, 16); + if (*end) + bad_id(id); + if (tmp > 0xffff) + bad_id(id); + product = tmp; +}