diff --git a/Makefile b/Makefile index a5a62bb..9ca3433 100644 --- a/Makefile +++ b/Makefile @@ -50,8 +50,8 @@ ifeq ($(OS),Windows_NT) RM = del endif ifneq (, $findstring(MINGW, $(UNAME_S))) - PORTS_CFLAGS := $(shell pkg-config --cflags libusb) - PORTS_LDFLAGS := $(shell pkg-config --libs libusb) + PORTS_CFLAGS := $(shell pkg-config --cflags libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs libusb-1.0) RM = rm -rf endif else @@ -72,14 +72,14 @@ else ifeq ($(UNAME_S),Darwin) # Mac OS X/MacPorts stuff ifeq ($(shell fink -V > /dev/null 2>&1 && echo ok),ok) - PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb) - PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb) -ltermcap -pthread + PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb-1.0) -ltermcap -pthread else ifeq ($(shell brew --version > /dev/null 2>&1 && echo ok),ok) - PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb) - PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb) -framework IOKit -framework CoreFoundation + PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb-1.0) -framework IOKit -framework CoreFoundation else ifeq ($(shell port version > /dev/null 2>&1 && echo ok),ok) - PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb) - PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb) -framework IOKit -framework CoreFoundation + PORTS_CFLAGS := $(shell pkg-config --cflags hidapi libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs hidapi libusb-1.0) -framework IOKit -framework CoreFoundation else PORTS_CFLAGS := -I/opt/local/include PORTS_LDFLAGS := -L/opt/local/lib -lhidapi -framework IOKit -framework CoreFoundation @@ -88,11 +88,11 @@ else RF25000_OBJ += transport/rf2500hidapi.o LDFLAGS = else ifneq ($(filter $(UNAME_S),OpenBSD NetBSD DragonFly),) - PORTS_CFLAGS := $(shell pkg-config --cflags libusb) - PORTS_LDFLAGS := $(shell pkg-config --libs libusb) -ltermcap -pthread + PORTS_CFLAGS := $(shell pkg-config --cflags libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs libusb-1.0) -ltermcap -pthread else - PORTS_CFLAGS := - PORTS_LDFLAGS := + PORTS_CFLAGS := $(shell pkg-config --cflags hidapi-hidraw libusb-1.0) + PORTS_LDFLAGS := $(shell pkg-config --libs hidapi-hidraw libusb-1.0) endif endif @@ -101,7 +101,7 @@ GCC_CFLAGS = -O1 -Wall -Wno-char-subscripts -ggdb CONFIG_CFLAGS = -DLIB_DIR=\"$(LIBDIR)\" MSPDEBUG_LDFLAGS = $(LDFLAGS) $(PORTS_LDFLAGS) -MSPDEBUG_LIBS = -L. -lusb $(READLINE_LIBS) $(OS_LIBS) +MSPDEBUG_LIBS = $(READLINE_LIBS) $(OS_LIBS) MSPDEBUG_CFLAGS = $(CFLAGS) $(READLINE_CFLAGS) $(PORTS_CFLAGS)\ $(GCC_CFLAGS) $(INCLUDES) $(CONFIG_CFLAGS) $(OS_CFLAGS) diff --git a/transport/bslhid.c b/transport/bslhid.c index b670e2d..f95aae9 100644 --- a/transport/bslhid.c +++ b/transport/bslhid.c @@ -16,11 +16,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif +#include #include #include #include @@ -32,7 +28,7 @@ #define BSLHID_VID 0x2047 #define BSLHID_PID 0x0200 -#define BSLHID_CLASS USB_CLASS_HID +#define BSLHID_CLASS LIBUSB_CLASS_HID #define BSLHID_XFER_SIZE 64 #define BSLHID_MTU (BSLHID_XFER_SIZE - 2) @@ -45,108 +41,99 @@ struct bslhid_transport { int cfg_number; int int_number; - struct usb_dev_handle *handle; + libusb_device_handle *handle; int in_ep; int out_ep; - - char bus_name[PATH_MAX + 1]; }; -static int find_interface(struct bslhid_transport *tr, - const struct usb_device *dev) +static int find_interface_endpoints(struct bslhid_transport *tr, libusb_device *dev) { - int c; + int c, rv = -1; + struct libusb_device_descriptor desc; + + if (libusb_get_device_descriptor(dev, &desc)) + return -1; - for (c = 0; c < dev->descriptor.bNumConfigurations; c++) { - const struct usb_config_descriptor *cfg = &dev->config[c]; + for (c = 0; c < desc.bNumConfigurations && rv; c++) { + struct libusb_config_descriptor *cfg; int i; + if (libusb_get_config_descriptor(dev, c, &cfg)) + continue; + for (i = 0; i < cfg->bNumInterfaces; i++) { - const struct usb_interface *intf = &cfg->interface[i]; - const struct usb_interface_descriptor *desc = - &intf->altsetting[0]; + const struct libusb_interface_descriptor *desc = + &cfg->interface[i].altsetting[0]; if (desc->bInterfaceClass == BSLHID_CLASS) { - tr->cfg_number = c; - tr->int_number = i; - return 0; + int j; + + tr->in_ep = -1; + tr->out_ep = -1; + + for (j = 0; j < desc->bNumEndpoints; j++) { + int addr = desc->endpoint[j].bEndpointAddress; + + if (addr & LIBUSB_ENDPOINT_DIR_MASK) + tr->in_ep = addr; + else + tr->out_ep = addr; + } + + if (tr->in_ep < 0 || tr->out_ep < 0) { + printc_err("bslhid: can't find suitable endpoints\n"); + } else { + tr->cfg_number = c; + tr->int_number = i; + rv = 0; + break; + } } } - } - - printc_err("bslhid: can't find a matching interface\n"); - return -1; -} - -static int find_endpoints(struct bslhid_transport *tr, - const struct usb_device *dev) -{ - const struct usb_interface_descriptor *desc = - &dev->config[tr->cfg_number].interface[tr->int_number]. - altsetting[0]; - int i; - - tr->in_ep = -1; - tr->out_ep = -1; - for (i = 0; i < desc->bNumEndpoints; i++) { - int addr = desc->endpoint[i].bEndpointAddress; - - if (addr & USB_ENDPOINT_DIR_MASK) - tr->in_ep = addr; - else - tr->out_ep = addr; - } - - if (tr->in_ep < 0 || tr->out_ep < 0) { - printc_err("bslhid: can't find suitable endpoints\n"); - return -1; + libusb_free_config_descriptor(cfg); } - return 0; + if (rv) + printc_err("bslhid: can't find a matching interface\n"); + return rv; } -static int open_device(struct bslhid_transport *tr, struct usb_device *dev) +static int open_device(struct bslhid_transport *tr, libusb_device *dev) { - if (find_interface(tr, dev) < 0) - return -1; + printc_dbg("Opening interface...\n"); - printc_dbg("Opening interface %d (config %d)...\n", - tr->int_number, tr->cfg_number); - - if (find_endpoints(tr, dev) < 0) + if (find_interface_endpoints(tr, dev) < 0) return -1; - printc_dbg("Found endpoints: IN: 0x%02x, OUT: 0x%02x\n", - tr->in_ep, tr->out_ep); + printc_dbg("Interface %d Config %d Endpoints: IN: 0x%02x, OUT: 0x%02x\n", + tr->int_number, tr->cfg_number, tr->in_ep, tr->out_ep); - tr->handle = usb_open(dev); - if (!tr->handle) { + if (libusb_open(dev, &tr->handle)) { pr_error("bslhid: can't open device"); return -1; } #ifdef __Windows__ - if (usb_set_configuration(tr->handle, tr->cfg_number) < 0) + if (libusb_set_configuration(tr->handle, tr->cfg_number) < 0) pr_error("warning: bslhid: can't set configuration"); #endif #ifdef __linux__ - if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0) - pr_error("warning: bslhid: can't detach kernel driver"); + if (libusb_kernel_driver_active(tr->handle, tr->int_number) > 0) { + if (libusb_detach_kernel_driver(tr->handle, + tr->int_number)) + pr_error("warning: bslhid: can't detach kernel driver"); + } #endif - if (usb_claim_interface(tr->handle, tr->int_number) < 0) { + if (libusb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error("bslhid: can't claim interface"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } - /* Save the bus path for a future suspend/resume */ - strncpy(tr->bus_name, dev->bus->dirname, sizeof(tr->bus_name)); - tr->bus_name[sizeof(tr->bus_name) - 1] = 0; - return 0; } @@ -155,8 +142,8 @@ static void bslhid_destroy(transport_t base) struct bslhid_transport *tr = (struct bslhid_transport *)base; if (tr->handle) { - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); + libusb_release_interface(tr->handle, tr->int_number); + libusb_close(tr->handle); } free(tr); @@ -167,12 +154,13 @@ static int bslhid_flush(transport_t base) #ifndef __APPLE__ struct bslhid_transport *tr = (struct bslhid_transport *)base; uint8_t inbuf[BSLHID_XFER_SIZE]; + int rlen; if (!tr->handle) return 0; - while (usb_bulk_read(tr->handle, tr->in_ep, - (char *)inbuf, sizeof(inbuf), 100) > 0); + while (!libusb_bulk_transfer(tr->handle, tr->in_ep, + inbuf, sizeof(inbuf), &rlen, 100) && rlen > 0); #endif return 0; @@ -182,6 +170,7 @@ static int bslhid_send(transport_t base, const uint8_t *data, int len) { struct bslhid_transport *tr = (struct bslhid_transport *)base; uint8_t outbuf[BSLHID_XFER_SIZE]; + int rv; if (!tr->handle) { printc_err("bslhid: send on suspended device\n"); @@ -203,10 +192,10 @@ static int bslhid_send(transport_t base, const uint8_t *data, int len) debug_hexdump("bslhid_send", outbuf, sizeof(outbuf)); #endif - if (usb_bulk_write(tr->handle, tr->out_ep, - (char *)outbuf, sizeof(outbuf), - BSLHID_TIMEOUT) < 0) { - printc_err("bslhid: usb_bulk_write: %s\n", usb_strerror()); + if ((rv = libusb_bulk_transfer(tr->handle, tr->out_ep, + outbuf, sizeof(outbuf), NULL, + BSLHID_TIMEOUT) < 0)) { + printc_err("bslhid: usb_bulk_write: %s\n", libusb_strerror(rv)); return -1; } @@ -217,7 +206,7 @@ static int bslhid_recv(transport_t base, uint8_t *data, int max_len) { struct bslhid_transport *tr = (struct bslhid_transport *)base; uint8_t inbuf[BSLHID_XFER_SIZE]; - int r; + int r, rv; int len; if (!tr->handle) { @@ -225,10 +214,9 @@ static int bslhid_recv(transport_t base, uint8_t *data, int max_len) return -1; } - r = usb_bulk_read(tr->handle, tr->in_ep, - (char *)inbuf, sizeof(inbuf), BSLHID_TIMEOUT); - if (r <= 0) { - printc_err("bslhid_recv: usb_bulk_read: %s\n", usb_strerror()); + if ((rv = libusb_bulk_transfer(tr->handle, tr->in_ep, + inbuf, sizeof(inbuf), &r, BSLHID_TIMEOUT))) { + printc_err("bslhid_recv: usb_bulk_read: %s\n", libusb_strerror(rv)); return -1; } @@ -263,92 +251,18 @@ static int bslhid_set_modem(transport_t base, transport_modem_t state) return -1; } -static int bslhid_suspend(transport_t base) -{ - struct bslhid_transport *tr = (struct bslhid_transport *)base; - - if (tr->handle) { - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); - tr->handle = NULL; - } - - return 0; -} - -static struct usb_bus *find_by_name(const char *name) -{ - struct usb_bus *b; - - for (b = usb_get_busses(); b; b = b->next) - if (!strcmp(name, b->dirname)) - return b; - - return NULL; -} - -static struct usb_device *find_first_bsl(struct usb_bus *bus) -{ - struct usb_device *d; - - for (d = bus->devices; d; d = d->next) - if ((d->descriptor.idVendor == BSLHID_VID) && - (d->descriptor.idProduct == BSLHID_PID)) - return d; - - return NULL; -} - -static int bslhid_resume(transport_t base) -{ - struct bslhid_transport *tr = (struct bslhid_transport *)base; - struct usb_bus *bus; - struct usb_device *dev; - - if (tr->handle) - return 0; - - usb_init(); - usb_find_busses(); - usb_find_devices(); - - bus = find_by_name(tr->bus_name); - if (!bus) { - printc_err("bslhid: can't find bus to resume from\n"); - return -1; - } - - /* We can't portably distinguish physical locations, so this - * will have to do. - */ - dev = find_first_bsl(bus); - if (!dev) { - printc_err("bslhid: can't find a BSL HID on this bus\n"); - return -1; - } - - if (open_device(tr, dev) < 0) { - printc_err("bslhid: failed to resume BSL HID device\n"); - return -1; - } - - return 0; -} - static const struct transport_class bslhid_transport_class = { .destroy = bslhid_destroy, .send = bslhid_send, .recv = bslhid_recv, .flush = bslhid_flush, .set_modem = bslhid_set_modem, - .suspend = bslhid_suspend, - .resume = bslhid_resume }; transport_t bslhid_open(const char *dev_path, const char *requested_serial) { struct bslhid_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; if (!tr) { pr_error("bslhid: can't allocate memory"); @@ -358,9 +272,7 @@ transport_t bslhid_open(const char *dev_path, const char *requested_serial) memset(tr, 0, sizeof(*tr)); tr->base.ops = &bslhid_transport_class; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (dev_path) dev = usbutil_find_by_loc(dev_path); @@ -374,11 +286,13 @@ transport_t bslhid_open(const char *dev_path, const char *requested_serial) } if (open_device(tr, dev) < 0) { + libusb_unref_device(dev); printc_err("bslhid: failed to open BSL HID device\n"); free(tr); return NULL; } + libusb_unref_device(dev); bslhid_flush(&tr->base); return &tr->base; } diff --git a/transport/bslosx.c b/transport/bslosx.c index 2295edf..0909950 100644 --- a/transport/bslosx.c +++ b/transport/bslosx.c @@ -17,7 +17,7 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include #include #include #include @@ -185,9 +185,9 @@ static struct usb_bus *find_by_name(const char *name) return NULL; } -static struct usb_device *find_first_bsl(struct usb_bus *bus) +static libusb_device *find_first_bsl(struct usb_bus *bus) { - struct usb_device *d; + libusb_device *d; for (d = bus->devices; d; d = d->next) if ((d->descriptor.idVendor == BSLHID_VID) && diff --git a/transport/cdc_acm.c b/transport/cdc_acm.c index c1117d3..b4d395c 100644 --- a/transport/cdc_acm.c +++ b/transport/cdc_acm.c @@ -19,13 +19,7 @@ #include #include #include - -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif - +#include #include "cdc_acm.h" #include "util.h" #include "usbutil.h" @@ -37,7 +31,7 @@ struct cdc_acm_transport { struct transport base; int int_number; - struct usb_dev_handle *handle; + libusb_device_handle *handle; int in_ep; int out_ep; @@ -47,7 +41,7 @@ struct cdc_acm_transport { */ int rbuf_len; int rbuf_ptr; - char rbuf[READ_BUFFER_SIZE]; + unsigned char rbuf[READ_BUFFER_SIZE]; }; #define CDC_INTERFACE_CLASS 10 @@ -72,9 +66,8 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len) debug_hexdump(__FILE__ ": USB transfer out", data, len); #endif while (len) { - sent = usb_bulk_write(tr->handle, tr->out_ep, - (char *)data, len, TIMEOUT); - if (sent <= 0) { + if (libusb_bulk_transfer(tr->handle, tr->out_ep, + (unsigned char *) data, len, &sent, TIMEOUT)) { pr_error(__FILE__": can't send data"); return -1; } @@ -92,12 +85,12 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int len) if (tr->rbuf_ptr >= tr->rbuf_len) { tr->rbuf_ptr = 0; - tr->rbuf_len = usb_bulk_read(tr->handle, tr->in_ep, - tr->rbuf, sizeof(tr->rbuf), - TIMEOUT); - if (tr->rbuf_len <= 0) { + if (libusb_bulk_transfer(tr->handle, tr->in_ep, + tr->rbuf, sizeof(tr->rbuf), &tr->rbuf_len, + TIMEOUT)) { pr_error(__FILE__": can't receive data"); + tr->rbuf_len = -1; return -1; } @@ -120,20 +113,20 @@ static void usbtr_destroy(transport_t tr_base) { struct cdc_acm_transport *tr = (struct cdc_acm_transport *)tr_base; - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); + libusb_release_interface(tr->handle, tr->int_number); + libusb_close(tr->handle); free(tr); } static int usbtr_flush(transport_t tr_base) { struct cdc_acm_transport *tr = (struct cdc_acm_transport *)tr_base; - char buf[64]; + unsigned char buf[64]; + int rlen; /* Flush out lingering data */ - while (usb_bulk_read(tr->handle, tr->in_ep, - buf, sizeof(buf), - 100) > 0); + while (!libusb_bulk_transfer(tr->handle, tr->in_ep, + buf, sizeof(buf), &rlen, 100) && rlen > 0); tr->rbuf_len = 0; tr->rbuf_ptr = 0; @@ -154,7 +147,7 @@ static int usbtr_set_modem(transport_t tr_base, transport_modem_t state) printc(__FILE__": modem ctrl = 0x%x\n", value); #endif - if (usb_control_msg(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, + if (libusb_control_transfer(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, CDC_SET_CONTROL, value, 0, NULL, 0, 300) < 0) { pr_error("cdc_acm: failed to set modem control lines\n"); @@ -173,76 +166,79 @@ static const struct transport_class cdc_acm_class = { }; static int find_interface(struct cdc_acm_transport *tr, - struct usb_device *dev) + libusb_device *dev) { - struct usb_config_descriptor *c = &dev->config[0]; - int i; - - for (i = 0; i < c->bNumInterfaces; i++) { - struct usb_interface *intf = &c->interface[i]; - struct usb_interface_descriptor *desc = &intf->altsetting[0]; - int j; + struct libusb_config_descriptor *c; + int rv = -1; - if (desc->bInterfaceClass != CDC_INTERFACE_CLASS) - continue; + if (!libusb_get_active_config_descriptor(dev, &c)) { + int i; - /* Look for bulk in/out endpoints */ - tr->in_ep = -1; - tr->out_ep = -1; + for (i = 0; i < c->bNumInterfaces; i++) { + const struct libusb_interface *intf = &c->interface[i]; + const struct libusb_interface_descriptor *desc = &intf->altsetting[0]; + int j; - for (j = 0; j < desc->bNumEndpoints; j++) { - struct usb_endpoint_descriptor *ep = - &desc->endpoint[j]; - const int type = - ep->bmAttributes & USB_ENDPOINT_TYPE_MASK; - const int addr = ep->bEndpointAddress; - - if (type != USB_ENDPOINT_TYPE_BULK) + if (desc->bInterfaceClass != CDC_INTERFACE_CLASS) continue; - if (addr & USB_ENDPOINT_DIR_MASK) - tr->in_ep = addr; - else - tr->out_ep = addr; - } - - if (tr->in_ep >= 0 && tr->out_ep >= 0) { - tr->int_number = i; - return 0; + /* Look for bulk in/out endpoints */ + tr->in_ep = -1; + tr->out_ep = -1; + + for (j = 0; j < desc->bNumEndpoints; j++) { + const struct libusb_endpoint_descriptor *ep = + &desc->endpoint[j]; + const int type = + ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK; + const int addr = ep->bEndpointAddress; + + if (type != LIBUSB_ENDPOINT_TRANSFER_TYPE_BULK) + continue; + + if (addr & LIBUSB_ENDPOINT_DIR_MASK) + tr->in_ep = addr; + else + tr->out_ep = addr; + } + + if (tr->in_ep >= 0 && tr->out_ep >= 0) { + tr->int_number = i; + rv = 0; + break; + } } } - return -1; + return rv; } static int open_interface(struct cdc_acm_transport *tr, - struct usb_device *dev) + libusb_device *dev) { #if defined(__linux__) int drv; - char drName[256]; #endif - tr->handle = usb_open(dev); - if (!tr->handle) { + if (libusb_open(dev, &tr->handle)) { pr_error(__FILE__": can't open device"); return -1; } #if defined(__linux__) - drv = usb_get_driver_np(tr->handle, tr->int_number, drName, - sizeof(drName)); - if (drv >= 0) { - if (usb_detach_kernel_driver_np(tr->handle, - tr->int_number) < 0) + drv = libusb_kernel_driver_active(tr->handle, tr->int_number); + printc(__FILE__" : driver %d\n", drv); + if (drv > 0) { + if (libusb_detach_kernel_driver(tr->handle, + tr->int_number)) pr_error(__FILE__": warning: can't detach " "kernel driver"); } #endif - if (usb_claim_interface(tr->handle, tr->int_number) < 0) { + if (libusb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error(__FILE__": can't claim interface"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } @@ -261,14 +257,14 @@ static int configure_port(struct cdc_acm_transport *tr, int baud_rate) line_coding[5] = 0; /* no parity */ line_coding[6] = 8; /* 8 data bits */ - if (usb_control_msg(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, + if (libusb_control_transfer(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, CDC_SET_LINE_CODING, 0, 0, - (char *)line_coding, 7, 300) < 0) { + line_coding, 7, 300) < 0) { pr_error("cdc_acm: failed to set line coding\n"); return -1; } - if (usb_control_msg(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, + if (libusb_control_transfer(tr->handle, CDC_REQTYPE_HOST_TO_DEVICE, CDC_SET_CONTROL, 0, 0, NULL, 0, 300) < 0) { pr_error("cdc_acm: failed to set modem control lines\n"); return -1; @@ -281,7 +277,7 @@ transport_t cdc_acm_open(const char *devpath, const char *requested_serial, int baud_rate, uint16_t vendor, uint16_t product) { struct cdc_acm_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; if (!tr) { pr_error(__FILE__": can't allocate memory"); @@ -291,9 +287,7 @@ transport_t cdc_acm_open(const char *devpath, const char *requested_serial, memset(tr, 0, sizeof(*tr)); tr->base.ops = &cdc_acm_class; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); @@ -318,8 +312,8 @@ transport_t cdc_acm_open(const char *devpath, const char *requested_serial, } if (configure_port(tr, baud_rate) < 0) { - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); + libusb_release_interface(tr->handle, tr->int_number); + libusb_close(tr->handle); free(tr); return NULL; } diff --git a/transport/cp210x.c b/transport/cp210x.c index 0fc5134..734bd40 100644 --- a/transport/cp210x.c +++ b/transport/cp210x.c @@ -20,15 +20,8 @@ #include #include #include - -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif - +#include #include - #include "cp210x.h" #include "util.h" #include "usbutil.h" @@ -37,7 +30,7 @@ struct cp210x_transport { struct transport base; - struct usb_dev_handle *handle; + libusb_device_handle *handle; int int_number; }; @@ -80,7 +73,7 @@ static int configure_port(struct cp210x_transport *tr, int baud_rate) { int ret; - ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, + ret = libusb_control_transfer(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_IFC_ENABLE, 0x1, 0, NULL, 0, 300); #ifdef DEBUG_CP210X printc("%s: %s: Sending control message " @@ -93,7 +86,7 @@ static int configure_port(struct cp210x_transport *tr, int baud_rate) } /* Set the baud rate to 500000 bps */ - ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, + ret = libusb_control_transfer(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_SET_BAUDDIV, CP210X_CLOCK / baud_rate, 0, NULL, 0, 300); #ifdef DEBUG_CP210X @@ -109,7 +102,7 @@ static int configure_port(struct cp210x_transport *tr, int baud_rate) /* Set the modem control settings. * Clear RTS, DTR and WRITE_DTR, WRITE_RTS */ - ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, + ret = libusb_control_transfer(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_SET_MHS, 0x303, 0, NULL, 0, 300); #ifdef DEBUG_CP210X printc("%s: %s: Sending control message " @@ -125,77 +118,81 @@ static int configure_port(struct cp210x_transport *tr, int baud_rate) } static int open_interface(struct cp210x_transport *tr, - struct usb_device *dev, int ino, + libusb_device *dev, int ino, int baud_rate) { #if defined(__linux__) int drv; - char drName[256]; #endif - printc_dbg(__FILE__": Trying to open interface %d on %s\n", - ino, dev->filename); + printc_dbg(__FILE__": Trying to open interface %d\n", + ino); tr->int_number = ino; - tr->handle = usb_open(dev); - if (!tr->handle) { + if (libusb_open(dev, &tr->handle)) { pr_error(__FILE__": can't open device"); return -1; } #if defined(__linux__) - drv = usb_get_driver_np(tr->handle, tr->int_number, drName, - sizeof(drName)); + drv = libusb_kernel_driver_active(tr->handle, tr->int_number); printc(__FILE__" : driver %d\n", drv); - if (drv >= 0) { - if (usb_detach_kernel_driver_np(tr->handle, - tr->int_number) < 0) + if (drv > 0) { + if (libusb_detach_kernel_driver(tr->handle, + tr->int_number)) pr_error(__FILE__": warning: can't detach " "kernel driver"); } #endif #ifdef __Windows__ - if (usb_set_configuration(tr->handle, 1) < 0) { + if (libusb_set_configuration(tr->handle, 1)) { pr_error(__FILE__": can't set configuration 1"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } #endif - if (usb_claim_interface(tr->handle, tr->int_number) < 0) { + if (libusb_claim_interface(tr->handle, tr->int_number)) { pr_error(__FILE__": can't claim interface"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } if (configure_port(tr, baud_rate) < 0) { printc_err("Failed to configure for V1 device\n"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } return 0; } -static int open_device(struct cp210x_transport *tr, struct usb_device *dev, +static int open_device(struct cp210x_transport *tr, libusb_device *dev, int baud_rate) { - struct usb_config_descriptor *c = &dev->config[0]; - int i; + struct libusb_config_descriptor *c; + int rv = -1; - for (i = 0; i < c->bNumInterfaces; i++) { - struct usb_interface *intf = &c->interface[i]; - struct usb_interface_descriptor *desc = &intf->altsetting[0]; + if (!libusb_get_active_config_descriptor(dev, &c)) { + int i; - if (desc->bInterfaceClass == V1_INTERFACE_CLASS && - !open_interface(tr, dev, desc->bInterfaceNumber, - baud_rate)) - return 0; + for (i = 0; i < c->bNumInterfaces && rv; i++) { + const struct libusb_interface *intf = &c->interface[i]; + const struct libusb_interface_descriptor *desc = &intf->altsetting[0]; + + if (desc->bInterfaceClass == V1_INTERFACE_CLASS && + !open_interface(tr, dev, desc->bInterfaceNumber, + baud_rate)) { + rv = 0; + } + } + + libusb_free_config_descriptor(c); } - return -1; + return rv; } static int usbtr_send(transport_t tr_base, const uint8_t *data, int len) @@ -207,9 +204,8 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len) debug_hexdump(__FILE__ ": USB transfer out", data, len); #endif while (len) { - sent = usb_bulk_write(tr->handle, V1_OUT_EP, - (char *)data, len, TIMEOUT_S * 1000); - if (sent <= 0) { + if (libusb_bulk_transfer(tr->handle, V1_OUT_EP, (unsigned char *) data, + len, &sent, TIMEOUT_S * 1000)) { pr_error(__FILE__": can't send data"); return -1; } @@ -232,18 +228,16 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len) #endif while (time(NULL) < deadline) { - rlen = usb_bulk_read(tr->handle, V1_IN_EP, (char *)databuf, - max_len, TIMEOUT_S * 1000); + if (libusb_bulk_transfer(tr->handle, V1_IN_EP, databuf, + max_len, &rlen, TIMEOUT_S * 1000) < 0) { + pr_error(__FILE__": can't receive data"); + return -1; + } #ifdef DEBUG_CP210X printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen); #endif - if (rlen < 0) { - pr_error(__FILE__": can't receive data"); - return -1; - } - if (rlen > 0) { #ifdef DEBUG_CP210X debug_hexdump(__FILE__": USB transfer in", databuf, rlen); @@ -260,20 +254,20 @@ static void usbtr_destroy(transport_t tr_base) { struct cp210x_transport *tr = (struct cp210x_transport *)tr_base; - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); + libusb_release_interface(tr->handle, tr->int_number); + libusb_close(tr->handle); free(tr); } static int usbtr_flush(transport_t tr_base) { struct cp210x_transport *tr = (struct cp210x_transport *)tr_base; - char buf[64]; + unsigned char buf[64]; + int rlen; /* Flush out lingering data */ - while (usb_bulk_read(tr->handle, V1_IN_EP, - buf, sizeof(buf), - 100) > 0); + while (!libusb_bulk_transfer(tr->handle, V1_IN_EP, buf, sizeof(buf), &rlen, + 100) && rlen > 0); return 0; } @@ -289,7 +283,7 @@ static int usbtr_set_modem(transport_t tr_base, transport_modem_t state) if (!(state & TRANSPORT_MODEM_RTS)) value |= CP210X_RTS; - if (usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, + if (libusb_control_transfer(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_SET_MHS, value, 0, NULL, 0, 300) < 0) { pr_error("cp210x: failed to set modem control lines\n"); return -1; @@ -310,7 +304,7 @@ transport_t cp210x_open(const char *devpath, const char *requested_serial, int baud_rate, uint16_t product, uint16_t vendor) { struct cp210x_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; if (!tr) { pr_error(__FILE__": can't allocate memory"); @@ -319,9 +313,7 @@ transport_t cp210x_open(const char *devpath, const char *requested_serial, tr->base.ops = &cp210x_class; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); diff --git a/transport/ftdi.c b/transport/ftdi.c index 52c634a..d14bbbd 100644 --- a/transport/ftdi.c +++ b/transport/ftdi.c @@ -27,7 +27,7 @@ struct ftdi_transport { struct transport base; - struct usb_dev_handle *handle; + libusb_device_handle *handle; }; #define USB_INTERFACE 0 @@ -67,20 +67,22 @@ struct ftdi_transport { #define FTDI_WRITE_DTR 0x0100 #define FTDI_WRITE_RTS 0x0200 -static int do_cfg(struct usb_dev_handle *handle, const char *what, +static int do_cfg(libusb_device_handle *handle, const char *what, int request, int value) { - if (usb_control_msg(handle, REQTYPE_HOST_TO_DEVICE, + int rv = libusb_control_transfer(handle, REQTYPE_HOST_TO_DEVICE, request, value, 0, - NULL, 0, REQ_TIMEOUT_MS)) { - printc_err("ftdi: %s failed: %s\n", what, usb_strerror()); + NULL, 0, REQ_TIMEOUT_MS); + + if (rv) { + printc_err("ftdi: %s failed: %s\n", what, libusb_strerror(rv)); return -1; } return 0; } -int configure_ftdi(struct usb_dev_handle *h, int baud_rate) +int configure_ftdi(libusb_device_handle *h, int baud_rate) { if (do_cfg(h, "reset FTDI", FTDI_SIO_RESET, FTDI_SIO_RESET_SIO) < 0 || @@ -103,53 +105,51 @@ int configure_ftdi(struct usb_dev_handle *h, int baud_rate) return 0; } -static int open_device(struct ftdi_transport *tr, struct usb_device *dev, +static int open_device(struct ftdi_transport *tr, libusb_device *dev, int baud_rate) { #ifdef __linux__ - int driver; - char drv_name[128]; + int drv; #endif + int rv; - printc_dbg("ftdi: trying to open %s\n", dev->filename); - tr->handle = usb_open(dev); - if (!tr->handle) { + printc_dbg("ftdi: trying to open device\n"); + if ((rv = libusb_open(dev, &tr->handle))) { printc_err("ftdi: can't open device: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } #ifdef __linux__ - driver = usb_get_driver_np(tr->handle, USB_INTERFACE, - drv_name, sizeof(drv_name)); - if (driver >= 0) { - printc_dbg("Detaching kernel driver \"%s\"\n", drv_name); - if (usb_detach_kernel_driver_np(tr->handle, USB_INTERFACE) < 0) - printc_err("warning: ftdi: can't detach " - "kernel driver: %s\n", usb_strerror()); + drv = libusb_kernel_driver_active(tr->handle, USB_INTERFACE); + printc(__FILE__" : driver %d\n", drv); + if (drv > 0) { + if (libusb_detach_kernel_driver(tr->handle, + USB_INTERFACE)) + pr_error(__FILE__": warning: can't detach " + "kernel driver"); } #endif #ifdef __Windows__ - if (usb_set_configuration(tr->handle, USB_CONFIG) < 0) { + if ((rv = libusb_set_configuration(tr->handle, USB_CONFIG))) { printc_err("ftdi: can't set configuration: %s\n", - usb_strerror()); - usb_close(tr->handle); + libusb_strerror(rv)); + libusb_close(tr->handle); return -1; } #endif - if (usb_claim_interface(tr->handle, USB_INTERFACE) < 0) { + if ((rv = libusb_claim_interface(tr->handle, USB_INTERFACE))) { printc_err("ftdi: can't claim interface: %s\n", - usb_strerror()); - usb_close(tr->handle); + libusb_strerror(rv)); + libusb_close(tr->handle); return -1; } - if (configure_ftdi(tr->handle, baud_rate) < 0) { - printc_err("ftdi: failed to configure device: %s\n", - usb_strerror()); - usb_close(tr->handle); + if (configure_ftdi(tr->handle, baud_rate)) { + printc_err("ftdi: failed to configure device\n"); + libusb_close(tr->handle); return -1; } @@ -160,7 +160,7 @@ static void tr_destroy(transport_t tr_base) { struct ftdi_transport *tr = (struct ftdi_transport *)tr_base; - usb_close(tr->handle); + libusb_close(tr->handle); free(tr); } @@ -168,19 +168,18 @@ static int tr_recv(transport_t tr_base, uint8_t *databuf, int max_len) { struct ftdi_transport *tr = (struct ftdi_transport *)tr_base; time_t deadline = time(NULL) + TIMEOUT_S; - char tmpbuf[FTDI_PACKET_SIZE]; + unsigned char tmpbuf[FTDI_PACKET_SIZE]; if (max_len > FTDI_PACKET_SIZE - 2) max_len = FTDI_PACKET_SIZE - 2; while(time(NULL) < deadline) { - int r = usb_bulk_read(tr->handle, EP_IN, - tmpbuf, max_len + 2, - TIMEOUT_S * 1000); + int r, rv; - if (r <= 0) { + if ((rv = libusb_bulk_transfer(tr->handle, EP_IN, tmpbuf, max_len + 2, + &r, TIMEOUT_S * 1000))) { printc_err("ftdi: usb_bulk_read: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } @@ -207,13 +206,12 @@ static int tr_send(transport_t tr_base, const uint8_t *databuf, int len) debug_hexdump("ftdi: tr_send", databuf, len); #endif while (len) { - int r = usb_bulk_write(tr->handle, EP_OUT, - (char *)databuf, len, - TIMEOUT_S * 1000); + int r, rv; - if (r <= 0) { + if ((rv = libusb_bulk_transfer(tr->handle, EP_OUT, (unsigned char *) databuf, + len, &r, TIMEOUT_S * 1000))) { printc_err("ftdi: usb_bulk_write: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } @@ -261,7 +259,7 @@ transport_t ftdi_open(const char *devpath, int baud_rate) { struct ftdi_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; if (!tr) { pr_error("ftdi: can't allocate memory"); @@ -270,9 +268,7 @@ transport_t ftdi_open(const char *devpath, tr->base.ops = &ftdi_class; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); diff --git a/transport/mehfet_xport.c b/transport/mehfet_xport.c index f5e282e..7cb9c1f 100644 --- a/transport/mehfet_xport.c +++ b/transport/mehfet_xport.c @@ -29,7 +29,7 @@ struct mehfet_transport { struct transport base; - struct usb_dev_handle *handle; + libusb_device_handle *handle; int epin, epout; int buf_size; }; @@ -39,40 +39,49 @@ struct mehfet_transport { #define REQ_TIMEOUT_MS 100 -static int open_device(struct mehfet_transport *tr, struct usb_device *dev) +static int open_device(struct mehfet_transport *tr, libusb_device *dev) { #ifdef __linux__ - int driver; - char drv_name[128]; + int drv; #endif + int rv; // first, find the right interface (and associated endpoints) of the USB device - int config = 0, itf = 0; + int config, itf; bool has = false; + struct libusb_device_descriptor desc; - for (config = 0; config < dev->descriptor.bNumConfigurations; ++config) { - struct usb_config_descriptor* cd = &dev->config[config]; + if (libusb_get_device_descriptor(dev, &desc)) + return -1; + + for (config = 0; config < desc.bNumConfigurations && !has; ++config) { + struct libusb_config_descriptor *cd; + + if (libusb_get_config_descriptor(dev, config, &cd)) + continue; for (itf = 0; itf < cd->bNumInterfaces; ++itf) { - struct usb_interface_descriptor* id = &cd->interface[itf].altsetting[0]; + const struct libusb_interface_descriptor* id = &cd->interface[itf].altsetting[0]; - if (id->bInterfaceClass == USB_CLASS_VENDOR_SPEC + if (id->bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC && id->bInterfaceSubClass == '4' && id->bInterfaceProtocol == '3') { + unsigned i; // here I'd like to check for the "MehFET" substring in the // interface's iInterface string, but I can't really figure out // how to do that, so I'll just assume this is enough checking. - if (id->bNumEndpoints != 2) continue; // this should be 2 - for (size_t i = 0; i < id->bNumEndpoints; ++i) { - struct usb_endpoint_descriptor* ed = &id->endpoint[i]; + if (id->bNumEndpoints != 2) + continue; // this should be 2 + for (i = 0; i < id->bNumEndpoints; ++i) { + const struct libusb_endpoint_descriptor* ed = &id->endpoint[i]; - if ((ed->bmAttributes & USB_ENDPOINT_TYPE_MASK) - != USB_ENDPOINT_TYPE_BULK) + if ((ed->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) + != LIBUSB_TRANSFER_TYPE_BULK) break; - if (ed->bEndpointAddress & USB_ENDPOINT_DIR_MASK) + if (ed->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) tr->epin = ed->bEndpointAddress; // input endpoint else tr->epout = ed->bEndpointAddress; // output endpoint @@ -80,50 +89,49 @@ static int open_device(struct mehfet_transport *tr, struct usb_device *dev) if (tr->epin != 0 && tr->epout != 0) { has = true; - goto break_outer; + break; } } } + + libusb_free_config_descriptor(cd); } -break_outer: if (!has) { - printc_err("mehfet transport: USB device %s has no MehFET interface.\n", - dev->filename); + printc_err("mehfet transport: USB device has no MehFET interface.\n"); return -1; } - printc_dbg("mehfet transport: trying to open %s\n", dev->filename); - tr->handle = usb_open(dev); - if (!tr->handle) { + printc_dbg("mehfet transport: trying to open device\n"); + if ((rv = libusb_open(dev, &tr->handle))) { printc_err("mehfet transport: can't open device: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } #ifdef __linux__ - driver = usb_get_driver_np(tr->handle, itf, drv_name, sizeof(drv_name)); - if (driver >= 0) { - printc_dbg("Detaching kernel driver \"%s\"\n", drv_name); - if (usb_detach_kernel_driver_np(tr->handle, itf) < 0) - printc_err("warning: mehfet transport: can't detach " - "kernel driver: %s\n", usb_strerror()); + drv = libusb_kernel_driver_active(tr->handle, itf); + printc(__FILE__" : driver %d\n", drv); + if (drv > 0) { + if (libusb_detach_kernel_driver(tr->handle, itf)) + pr_error(__FILE__": warning: can't detach " + "kernel driver"); } #endif #ifdef __Windows__ - if (usb_set_configuration(tr->handle, config) < 0) { + if ((rv = libusb_set_configuration(tr->handle, config))) { printc_err("mehfet transport: can't set configuration: %s\n", - usb_strerror()); - usb_close(tr->handle); + libusb_strerror(rv)); + libusb_close(tr->handle); return -1; } #endif - if (usb_claim_interface(tr->handle, itf) < 0) { + if ((rv = libusb_claim_interface(tr->handle, itf))) { printc_err("mehfet transport: can't claim interface: %s\n", - usb_strerror()); - usb_close(tr->handle); + libusb_strerror(rv)); + libusb_close(tr->handle); return -1; } @@ -134,7 +142,7 @@ static void tr_destroy(transport_t tr_base) { struct mehfet_transport *tr = (struct mehfet_transport *)tr_base; - usb_close(tr->handle); + libusb_close(tr->handle); free(tr); } @@ -142,19 +150,18 @@ static int tr_recv(transport_t tr_base, uint8_t *databuf, int max_len) { struct mehfet_transport *tr = (struct mehfet_transport *)tr_base; time_t deadline = time(NULL) + TIMEOUT_S; - char tmpbuf[tr->buf_size]; + unsigned char tmpbuf[tr->buf_size]; if (max_len > tr->buf_size) max_len = tr->buf_size; while (time(NULL) < deadline) { - int r = usb_bulk_read(tr->handle, tr->epin, - tmpbuf, max_len, - TIMEOUT_S * 1000); + int r, rv; - if (r <= 0) { + if ((rv = libusb_bulk_transfer(tr->handle, tr->epin, tmpbuf, max_len, &r, + TIMEOUT_S * 1000))) { printc_err("mehfet transport: usb_bulk_read: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } @@ -179,13 +186,13 @@ static int tr_send(transport_t tr_base, const uint8_t *databuf, int len) debug_hexdump("mehfet transport: tr_send", databuf, len); #endif while (len) { - int r = usb_bulk_write(tr->handle, tr->epout, - (char *)databuf, len, - TIMEOUT_S * 1000); + int r, rv; - if (r <= 0) { + if ((rv = libusb_bulk_transfer(tr->handle, tr->epout, + (unsigned char *) databuf, len, &r, + TIMEOUT_S * 1000))) { printc_err("mehfet transport: usb_bulk_write: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } @@ -221,7 +228,7 @@ transport_t mehfet_transport_open(const char *devpath, const char *requested_serial) { struct mehfet_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev = NULL; + libusb_device *dev = NULL; if (!tr) { pr_error("mehfet transport: can't allocate memory"); @@ -230,9 +237,7 @@ transport_t mehfet_transport_open(const char *devpath, tr->base.ops = &mehfet_class; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); diff --git a/transport/rf2500.c b/transport/rf2500.c index 54301d6..adb1a9b 100644 --- a/transport/rf2500.c +++ b/transport/rf2500.c @@ -19,12 +19,7 @@ #include #include #include -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif - +#include #include "rf2500.h" #include "util.h" #include "usbutil.h" @@ -34,7 +29,7 @@ struct rf2500_transport { struct transport base; int int_number; - struct usb_dev_handle *handle; + libusb_device_handle *handle; uint8_t buf[64]; int len; @@ -60,35 +55,37 @@ struct rf2500_transport { #define USB_FET_OUT_EP 0x01 static int open_interface(struct rf2500_transport *tr, - struct usb_device *dev, int ino) + libusb_device *dev, int ino) { - printc_dbg("Trying to open interface %d on %s\n", ino, dev->filename); + printc_dbg("Trying to open interface %d\n", ino); tr->int_number = ino; - tr->handle = usb_open(dev); - if (!tr->handle) { + if (libusb_open(dev, &tr->handle)) { pr_error("rf2500: can't open device"); return -1; } #if defined(__linux__) - if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0) - pr_error("rf2500: warning: can't " - "detach kernel driver"); + if (libusb_kernel_driver_active(tr->handle, tr->int_number) > 0) { + if (libusb_detach_kernel_driver(tr->handle, + tr->int_number)) + pr_error("rf2500: warning: can't " + "detach kernel driver"); + } #endif #ifdef __Windows__ - if (usb_set_configuration(tr->handle, 1) < 0) { + if (libusb_set_configuration(tr->handle, 1) < 0) { pr_error("rf2500: can't set configuration 1"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } #endif - if (usb_claim_interface(tr->handle, tr->int_number) < 0) { + if (libusb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error("rf2500: can't claim interface"); - usb_close(tr->handle); + libusb_close(tr->handle); return -1; } @@ -96,21 +93,28 @@ static int open_interface(struct rf2500_transport *tr, } static int open_device(struct rf2500_transport *tr, - struct usb_device *dev) + libusb_device *dev) { - struct usb_config_descriptor *c = &dev->config[0]; - int i; + struct libusb_config_descriptor *c; + int rv = -1; + + if (!libusb_get_active_config_descriptor(dev, &c)) { + int i; - for (i = 0; i < c->bNumInterfaces; i++) { - struct usb_interface *intf = &c->interface[i]; - struct usb_interface_descriptor *desc = &intf->altsetting[0]; + for (i = 0; i < c->bNumInterfaces && rv; i++) { + const struct libusb_interface *intf = &c->interface[i]; + const struct libusb_interface_descriptor *desc = &intf->altsetting[0]; - if (desc->bInterfaceClass == USB_FET_INTERFACE_CLASS && - !open_interface(tr, dev, desc->bInterfaceNumber)) - return 0; + if (desc->bInterfaceClass == USB_FET_INTERFACE_CLASS && + !open_interface(tr, dev, desc->bInterfaceNumber)) { + rv = 0; + } + } + + libusb_free_config_descriptor(c); } - return -1; + return rv; } static int usbtr_send(transport_t tr_base, const uint8_t *data, int len) @@ -138,8 +142,8 @@ static int usbtr_send(transport_t tr_base, const uint8_t *data, int len) #ifdef DEBUG_USBTR debug_hexdump("USB transfer out", pbuf, txlen); #endif - if (usb_bulk_write(tr->handle, USB_FET_OUT_EP, - (char *)pbuf, txlen, 10000) < 0) { + if (libusb_bulk_transfer(tr->handle, USB_FET_OUT_EP, + pbuf, txlen, NULL, 10000)) { pr_error("rf2500: can't send data"); return -1; } @@ -157,8 +161,8 @@ static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len) int rlen; if (tr->offset >= tr->len) { - if (usb_bulk_read(tr->handle, USB_FET_IN_EP, - (char *)tr->buf, sizeof(tr->buf), + if (libusb_bulk_transfer(tr->handle, USB_FET_IN_EP, + tr->buf, sizeof(tr->buf), NULL, 10000) < 0) { pr_error("rf2500: can't receive data"); return -1; @@ -187,8 +191,8 @@ static void usbtr_destroy(transport_t tr_base) { struct rf2500_transport *tr = (struct rf2500_transport *)tr_base; - usb_release_interface(tr->handle, tr->int_number); - usb_close(tr->handle); + libusb_release_interface(tr->handle, tr->int_number); + libusb_close(tr->handle); free(tr); } @@ -197,16 +201,17 @@ static int usbtr_flush(transport_t tr_base) struct rf2500_transport *tr = (struct rf2500_transport *)tr_base; #if !defined(__APPLE__) && !defined(__sun__) - char buf[64]; + unsigned char buf[64]; + int rlen; /* Flush out lingering data. * * The timeout apparently doesn't work on OS/X, and this loop * just hangs once the endpoint buffer empties. */ - while (usb_bulk_read(tr->handle, USB_FET_IN_EP, - buf, sizeof(buf), - 100) > 0); + while (!libusb_bulk_transfer(tr->handle, USB_FET_IN_EP, + buf, sizeof(buf), &rlen, + 100) && rlen > 0); #endif tr->len = 0; @@ -232,7 +237,7 @@ transport_t rf2500_open(const char *devpath, const char *requested_serial, int has_vid_pid, uint16_t vid, uint16_t pid) { struct rf2500_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; if (!tr) { pr_error("rf2500: can't allocate memory"); @@ -242,9 +247,7 @@ transport_t rf2500_open(const char *devpath, const char *requested_serial, memset(tr, 0, sizeof(*tr)); tr->base.ops = &rf2500_transport; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); diff --git a/transport/ti3410.c b/transport/ti3410.c index de51388..5deef25 100644 --- a/transport/ti3410.c +++ b/transport/ti3410.c @@ -20,14 +20,8 @@ #include #include #include -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif - +#include #include - #include "ti3410.h" #include "util.h" #include "usbutil.h" @@ -123,7 +117,7 @@ struct ti3410_transport { struct transport base; - struct usb_dev_handle *hnd; + libusb_device_handle *hnd; }; #define USB_FET_VENDOR 0x0451 @@ -141,39 +135,54 @@ struct ti3410_transport { #define READ_TIMEOUT 5000 static int open_device(struct ti3410_transport *tr, - struct usb_device *dev) + libusb_device *dev) { - struct usb_dev_handle *hnd; + libusb_device_handle *hnd; + struct libusb_config_descriptor *c; +#if defined(__linux__) + int drv; +#endif + int config; - hnd = usb_open(dev); - if (!hnd) { + if (libusb_open(dev, &hnd)) { pr_error("ti3410: failed to open USB device"); return -1; } #if defined(__linux__) - if (usb_detach_kernel_driver_np(hnd, USB_FET_INTERFACE) < 0) - pr_error("ti3410: warning: can't " - "detach kernel driver"); + drv = libusb_kernel_driver_active(hnd, USB_FET_INTERFACE); + printc(__FILE__" : driver %d\n", drv); + if (drv > 0) { + if (libusb_detach_kernel_driver(hnd, USB_FET_INTERFACE)) + pr_error("ti3410: warning: can't " + "detach kernel driver"); + } #endif /* This device has two configurations -- we need the one which * has two bulk endpoints and a control. */ - if (dev->config->bConfigurationValue == TI_BOOT_CONFIG) { + if (!libusb_get_active_config_descriptor(dev, &c)) { + config = c->bConfigurationValue; + libusb_free_config_descriptor(c); + } else { + config = TI_BOOT_CONFIG; + } + + if (config == TI_BOOT_CONFIG) { printc_dbg("TI3410 device is in boot config, " "setting active\n"); - if (usb_set_configuration(hnd, TI_ACTIVE_CONFIG) < 0) { + if (libusb_set_configuration(hnd, TI_ACTIVE_CONFIG) < 0) { pr_error("ti3410: failed to set active config"); - usb_close(hnd); + libusb_close(hnd); return -1; } } - if (usb_claim_interface(hnd, USB_FET_INTERFACE) < 0) { + if (libusb_claim_interface(hnd, USB_FET_INTERFACE) < 0) { pr_error("ti3410: can't claim interface"); - usb_close(hnd); + libusb_close(hnd); return -1; } @@ -194,11 +203,11 @@ static int set_termios(struct ti3410_transport *tr) 0x00 /* UART mode = RS232 */ }; - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_SET_CONFIG, 0, - TI_UART1_PORT, (char *)tios_data, sizeof(tios_data), TIMEOUT) < 0) { + TI_UART1_PORT, (unsigned char *) tios_data, sizeof(tios_data), TIMEOUT) < 0) { pr_error("ti3410: TI_SET_CONFIG failed"); return -1; } @@ -217,11 +226,11 @@ static int set_mcr(struct ti3410_transport *tr) TI_MCR_RTS | TI_MCR_DTR /* data */ }; - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_WRITE_DATA, 0, - TI_RAM_PORT, (char *)wb_data, sizeof(wb_data), TIMEOUT) < 0) { + TI_RAM_PORT, (unsigned char *) wb_data, sizeof(wb_data), TIMEOUT) < 0) { pr_error("ti3410: TI_SET_CONFIG failed"); return -1; } @@ -237,8 +246,8 @@ static int do_open_start(struct ti3410_transport *tr) if (set_mcr(tr) < 0) return -1; - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_OPEN_PORT, TI_PIPE_MODE_CONTINOUS | TI_PIPE_TIMEOUT_ENABLE | (TI_TRANSFER_TIMEOUT << 2), @@ -247,8 +256,8 @@ static int do_open_start(struct ti3410_transport *tr) return -1; } - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_START_PORT, 0, TI_UART1_PORT, NULL, 0, TIMEOUT) < 0) { @@ -262,9 +271,10 @@ static int do_open_start(struct ti3410_transport *tr) static int interrupt_flush(struct ti3410_transport *tr) { uint8_t buf[2]; + int rlen; - return usb_interrupt_read(tr->hnd, USB_FET_INT_EP, - (char *)buf, 2, TIMEOUT); + return libusb_interrupt_transfer(tr->hnd, USB_FET_INT_EP, + buf, 2, &rlen, TIMEOUT) ? 0 : rlen; } static int setup_port(struct ti3410_transport *tr) @@ -274,8 +284,8 @@ static int setup_port(struct ti3410_transport *tr) if (do_open_start(tr) < 0) return -1; - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_PURGE_PORT, TI_PURGE_INPUT, TI_UART1_PORT, NULL, 0, TIMEOUT) < 0) { @@ -286,8 +296,8 @@ static int setup_port(struct ti3410_transport *tr) interrupt_flush(tr); interrupt_flush(tr); - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_PURGE_PORT, TI_PURGE_OUTPUT, TI_UART1_PORT, NULL, 0, TIMEOUT) < 0) { @@ -297,8 +307,8 @@ static int setup_port(struct ti3410_transport *tr) interrupt_flush(tr); - if (usb_clear_halt(tr->hnd, USB_FET_IN_EP) < 0 || - usb_clear_halt(tr->hnd, USB_FET_OUT_EP) < 0) { + if (libusb_clear_halt(tr->hnd, USB_FET_IN_EP) < 0 || + libusb_clear_halt(tr->hnd, USB_FET_OUT_EP) < 0) { pr_error("ti3410: failed to clear halt status"); return -1; } @@ -311,8 +321,8 @@ static int setup_port(struct ti3410_transport *tr) static void teardown_port(struct ti3410_transport *tr) { - if (usb_control_msg(tr->hnd, - USB_TYPE_VENDOR | USB_RECIP_DEVICE, + if (libusb_control_transfer(tr->hnd, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, TI_CLOSE_PORT, 0, TI_UART1_PORT, NULL, 0, TIMEOUT) < 0) @@ -325,9 +335,8 @@ static int ti3410_send(transport_t tr_base, const uint8_t *data, int len) int sent; while (len) { - sent = usb_bulk_write(tr->hnd, USB_FET_OUT_EP, - (char *)data, len, TIMEOUT); - if (sent <= 0) { + if (libusb_bulk_transfer(tr->hnd, USB_FET_OUT_EP, + (unsigned char *) data, len, &sent, TIMEOUT)) { pr_error("ti3410: can't send data"); return -1; } @@ -343,20 +352,19 @@ static int ti3410_recv(transport_t tr_base, uint8_t *databuf, int max_len) { struct ti3410_transport *tr = (struct ti3410_transport *)tr_base; time_t deadline = time(NULL) + READ_TIMEOUT / 1000; - int rlen; while (time(NULL) < deadline) { - rlen = usb_bulk_read(tr->hnd, USB_FET_IN_EP, (char *)databuf, - max_len, READ_TIMEOUT); + int rlen, rv; - if (rlen > 0) - return rlen; - - if (rlen < 0) { + if ((rv = libusb_bulk_transfer(tr->hnd, USB_FET_IN_EP, databuf, + max_len, &rlen, READ_TIMEOUT))) { printc_err("ti3410: usb_bulk_read: %s\n", - usb_strerror()); + libusb_strerror(rv)); return -1; } + + if (rlen > 0) + return rlen; } printc_err("ti3410: read timeout\n"); @@ -471,28 +479,29 @@ static void prepare_firmware(struct firmware *f) f->size, cksum); } -static int do_download(struct usb_device *dev, const struct firmware *f) +static int do_download(libusb_device *dev, const struct firmware *f) { - struct usb_dev_handle *hnd; + libusb_device_handle *hnd; int offset = 0; printc_dbg("Starting download...\n"); - hnd = usb_open(dev); - if (!hnd) { + if (libusb_open(dev, &hnd)) { pr_error("ti3410: failed to open USB device"); return -1; } #if defined(__linux__) - if (usb_detach_kernel_driver_np(hnd, USB_FDL_INTERFACE) < 0) - pr_error("ti3410: warning: can't " - "detach kernel driver"); + if (libusb_kernel_driver_active(hnd, USB_FET_INTERFACE) > 0) { + if (libusb_detach_kernel_driver(hnd, USB_FET_INTERFACE)) + pr_error("ti3410: warning: can't " + "detach kernel driver"); + } #endif - if (usb_claim_interface(hnd, USB_FDL_INTERFACE) < 0) { + if (libusb_claim_interface(hnd, USB_FDL_INTERFACE) < 0) { pr_error("ti3410: can't claim interface"); - usb_close(hnd); + libusb_close(hnd); return -1; } @@ -503,11 +512,9 @@ static int do_download(struct usb_device *dev, const struct firmware *f) if (plen > TI_DOWNLOAD_MAX_PACKET_SIZE) plen = TI_DOWNLOAD_MAX_PACKET_SIZE; - r = usb_bulk_write(hnd, USB_FDL_OUT_EP, - (char *)f->buf + offset, plen, TIMEOUT); - if (r < 0) { + if (libusb_bulk_transfer(hnd, USB_FDL_OUT_EP, (unsigned char *) f->buf + offset, plen, &r, TIMEOUT)) { pr_error("ti3410: bulk write failed"); - usb_close(hnd); + libusb_close(hnd); return -1; } @@ -515,14 +522,14 @@ static int do_download(struct usb_device *dev, const struct firmware *f) } delay_ms(100); - if (usb_reset(hnd) < 0) + if (libusb_reset_device(hnd) < 0) pr_error("ti3410: warning: reset failed"); - usb_close(hnd); + libusb_close(hnd); return 0; } -static int download_firmware(struct usb_device *dev) +static int download_firmware(libusb_device *dev) { struct firmware frm; @@ -563,7 +570,8 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial, int has_vid_pid, uint16_t vid, uint16_t pid) { struct ti3410_transport *tr = malloc(sizeof(*tr)); - struct usb_device *dev; + libusb_device *dev; + struct libusb_device_descriptor desc; if (!tr) { pr_error("ti3410: can't allocate memory"); @@ -572,9 +580,7 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial, tr->base.ops = &ti3410_transport; - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); if (devpath) dev = usbutil_find_by_loc(devpath); @@ -588,14 +594,16 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial, return NULL; } - if (dev->descriptor.bNumConfigurations == 1) { + if (libusb_get_device_descriptor(dev, &desc)) + return NULL; + + if (desc.bNumConfigurations == 1) { if (download_firmware(dev) < 0) { printc_err("ti3410: firmware download failed\n"); free(tr); return NULL; } - - usb_find_devices(); + libusb_unref_device(dev); if (devpath) dev = usbutil_find_by_loc(devpath); @@ -619,7 +627,7 @@ transport_t ti3410_open(const char *devpath, const char *requested_serial, if (setup_port(tr) < 0) { printc_err("ti3410: failed to set up port\n"); teardown_port(tr); - usb_close(tr->hnd); + libusb_close(tr->hnd); free(tr); return NULL; } diff --git a/ui/main.c b/ui/main.c index 073c848..dac9043 100644 --- a/ui/main.c +++ b/ui/main.c @@ -383,9 +383,7 @@ static int parse_cmdline_args(int argc, char **argv, break; case LOPT_USB_LIST: - usb_init(); - usb_find_busses(); - usb_find_devices(); + libusb_init(NULL); usbutil_list(); exit(0); diff --git a/util/usbutil.c b/util/usbutil.c index 32831d1..01f28c0 100644 --- a/util/usbutil.c +++ b/util/usbutil.c @@ -23,7 +23,7 @@ #include "util.h" #include "output.h" -static const char *device_help(const struct usb_device *dev) +static const char *device_help(const struct libusb_device_descriptor *desc) { static const struct { int vendor; @@ -42,98 +42,109 @@ static const char *device_help(const struct usb_device *dev) int i; for (i = 0; i < ARRAY_LEN(info); i++) - if (dev->descriptor.idProduct == info[i].product && - dev->descriptor.idVendor == info[i].vendor) + if (desc->idProduct == info[i].product && + desc->idVendor == info[i].vendor) return info[i].help; return ""; } -static int read_serial(struct usb_device *dev, char *buf, int max_len) +static int read_serial(struct libusb_device *dev, const struct libusb_device_descriptor *desc, unsigned char *buf, int max_len) { - struct usb_dev_handle *dh = usb_open(dev); + libusb_device_handle *dh; + int rv; - if (!dh) + if (libusb_open(dev, &dh)) return -1; - if (usb_get_string_simple(dh, dev->descriptor.iSerialNumber, - buf, max_len) < 0) { - usb_close(dh); - return -1; - } - - usb_close(dh); - return 0; + rv = libusb_get_string_descriptor_ascii(dh, desc->iSerialNumber, buf, max_len); + libusb_close(dh); + return rv; } void usbutil_list(void) { - const struct usb_bus *bus; + libusb_device **list; + ssize_t cnt = libusb_get_device_list(NULL, &list); + ssize_t i = 0; - for (bus = usb_get_busses(); bus; bus = bus->next) { - struct usb_device *dev; - int busnum = atoi(bus->dirname); + for (i = 0; i < cnt; i++) { + libusb_device *dev = list[i]; + struct libusb_device_descriptor desc; - printc("Devices on bus %03d:\n", busnum); + printc("%03d:%03d", + libusb_get_bus_number(dev), libusb_get_device_address(dev)); - for (dev = bus->devices; dev; dev = dev->next) { - int devnum = atoi(dev->filename); - char serial[128]; + if (!libusb_get_device_descriptor(dev, &desc)) { + unsigned char serial[128]; - printc(" %03d:%03d %04x:%04x %s", - busnum, devnum, - dev->descriptor.idVendor, - dev->descriptor.idProduct, - device_help(dev)); + printc(" %04x:%04x %s", + desc.idVendor, + desc.idProduct, + device_help(&desc)); - if (!read_serial(dev, serial, sizeof(serial))) + if (!read_serial(dev, &desc, serial, sizeof(serial))) printc(" [serial: %s]\n", serial); - else - printc("\n"); } + + printc("\n"); } + + libusb_free_device_list(list, 1); } -struct usb_device *usbutil_find_by_id(int vendor, int product, +libusb_device *usbutil_find_by_id(int vendor, int product, const char *requested_serial) { - struct usb_bus *bus; + libusb_device **list; + libusb_device *found = NULL; + ssize_t cnt = libusb_get_device_list(NULL, &list); + ssize_t i = 0; - for (bus = usb_get_busses(); bus; bus = bus->next) { - struct usb_device *dev; + for (i = 0; i < cnt; i++) { + libusb_device *dev = list[i]; + struct libusb_device_descriptor desc; - for (dev = bus->devices; dev; dev = dev->next) { - if (dev->descriptor.idVendor == vendor && - dev->descriptor.idProduct == product) { - char buf[128]; + if (!libusb_get_device_descriptor(dev, &desc)) { + if (desc.idVendor == vendor && desc.idProduct == product) { + unsigned char buf[128]; if (!requested_serial || - (!read_serial(dev, buf, sizeof(buf)) && - !strcasecmp(requested_serial, buf))) - return dev; + (!read_serial(dev, &desc, buf, sizeof(buf)) && + !strcasecmp(requested_serial, (char *) buf))) { + found = dev; + libusb_ref_device(found); + break; + } } } } - if(requested_serial) - printc_err("usbutil: unable to find device matching " - "%04x:%04x with serial %s\n", vendor, product, - requested_serial); - else - printc_err("usbutil: unable to find a device matching " - "%04x:%04x\n", vendor, product); + libusb_free_device_list(list, 1); - return NULL; + if (!found) { + if (requested_serial) + printc_err("usbutil: unable to find device matching " + "%04x:%04x with serial %s\n", vendor, product, + requested_serial); + else + printc_err("usbutil: unable to find a device matching " + "%04x:%04x\n", vendor, product); + } + + return found; } -struct usb_device *usbutil_find_by_loc(const char *loc) +libusb_device *usbutil_find_by_loc(const char *loc) { char buf[64]; char *bus_text; char *dev_text; int target_bus; int target_dev; - struct usb_bus *bus; + libusb_device **list; + libusb_device *found = NULL; + ssize_t cnt, i = 0; strncpy(buf, loc, sizeof(buf)); buf[sizeof(buf) - 1] = 0; @@ -150,22 +161,24 @@ struct usb_device *usbutil_find_by_loc(const char *loc) target_bus = atoi(bus_text); target_dev = atoi(dev_text); - for (bus = usb_get_busses(); bus; bus = bus->next) { - struct usb_device *dev; - int busnum = atoi(bus->dirname); - - if (busnum != target_bus) - continue; + cnt = libusb_get_device_list(NULL, &list); - for (dev = bus->devices; dev; dev = dev->next) { - int devnum = atoi(dev->filename); + for (i = 0; i < cnt; i++) { + libusb_device *dev = list[i]; - if (devnum == target_dev) - return dev; + if (target_bus == libusb_get_bus_number(dev) && + target_dev == libusb_get_device_address(dev)) { + found = dev; + libusb_ref_device(found); + break; } } - printc_err("usbutil: unable to find %03d:%03d\n", - target_bus, target_dev); - return NULL; + libusb_free_device_list(list, 1); + + if (!found) + printc_err("usbutil: unable to find %03d:%03d\n", + target_bus, target_dev); + + return found; } diff --git a/util/usbutil.h b/util/usbutil.h index d88f1a5..4d93a63 100644 --- a/util/usbutil.h +++ b/util/usbutil.h @@ -19,20 +19,16 @@ #ifndef USBUTIL_H_ #define USBUTIL_H_ -#if !defined(__Windows__) || defined(__MINGW32__) -#include -#else -#include -#endif +#include /* List all available USB devices. */ void usbutil_list(void); /* Search for the first device matching the given Vendor:Product */ -struct usb_device *usbutil_find_by_id(int vendor, int product, +libusb_device *usbutil_find_by_id(int vendor, int product, const char *requested_serial); /* Search for a device using a bus:dev location string */ -struct usb_device *usbutil_find_by_loc(const char *loc); +libusb_device *usbutil_find_by_loc(const char *loc); #endif