[PATCH 3/3] Use libusb-1.0 for USB communication

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/simtrace@lists.osmocom.org/.

Peter Stuge peter at stuge.se
Sat Aug 13 19:15:04 UTC 2011


---
 host/Makefile     |    6 ++--
 host/main.c       |   55 +++++++++++++++++++++++----------
 host/usb.c        |   87 -----------------------------------------------------
 host/usb_helper.c |   76 ----------------------------------------------
 host/usb_helper.h |   26 ----------------
 5 files changed, 41 insertions(+), 209 deletions(-)
 delete mode 100644 host/usb.c
 delete mode 100644 host/usb_helper.c
 delete mode 100644 host/usb_helper.h

diff --git a/host/Makefile b/host/Makefile
index 03120c4..247cabc 100644
--- a/host/Makefile
+++ b/host/Makefile
@@ -1,12 +1,12 @@
-LDFLAGS=-lusb `pkg-config --libs libosmocore` -losmocore
+LDFLAGS=`pkg-config --libs libusb-1.0` `pkg-config --libs libosmocore` -losmocore
 
 all: simtrace
 
-simtrace: main.o usb_helper.o usb.o apdu_split.o
+simtrace: main.o apdu_split.o
 	$(CC) -o $@ $^ $(LDFLAGS)
 
 %.o: %.c
-	$(CC) $(CFLAGS) `pkg-config --cflags libosmocore` -o $@ -c $^
+	$(CC) $(CFLAGS) `pkg-config --cflags libusb-1.0` `pkg-config --cflags libosmocore` -o $@ -c $^
 
 install: simtrace
 	install -d $(DESTDIR)/usr/bin/
diff --git a/host/main.c b/host/main.c
index 8e63448..4fbac8d 100644
--- a/host/main.c
+++ b/host/main.c
@@ -32,9 +32,8 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include <usb.h>
+#include <libusb.h>
 
-#include "usb_helper.h"
 #include "simtrace.h"
 #include "simtrace_usb.h"
 #include "apdu_split.h"
@@ -43,7 +42,7 @@
 #include <osmocom/core/gsmtap_util.h>
 #include <osmocom/core/utils.h>
 
-static struct usb_dev_handle *udev;
+static struct libusb_device_handle *devh;
 static struct apdu_split *as;
 static struct gsmtap_inst *g_gti;
 
@@ -140,8 +139,9 @@ int main(int argc, char **argv)
 {
 	char buf[16*265];
 	char *gsmtap_host = "127.0.0.1";
-	int rc, c;
+	int rc, c, ret = 1;
 	int skip_atr = 0;
+	int xfer_len;
 	unsigned int msg_count, byte_count = 0;
 
 	print_welcome();
@@ -166,35 +166,56 @@ int main(int argc, char **argv)
 		}
 	}
 
+	rc = libusb_init(NULL);
+	if (rc < 0) {
+		fprintf(stderr, "libusb initialization failed\n");
+		goto close_exit;
+	}
+
 	g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
 	if (!g_gti) {
 		perror("unable to open GSMTAP");
-		exit(1);
+		goto close_exit;
 	}
 	gsmtap_source_add_sink(g_gti);
 
-	udev = usb_find_open(SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
-	if (!udev) {
-		perror("opening USB device");
-		exit(1);
+	devh = libusb_open_device_with_vid_pid(NULL, SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
+	if (!devh) {
+		fprintf(stderr, "can't open USB device\n");
+		goto close_exit;
+	}
+
+	rc = libusb_claim_interface(devh, 0);
+	if (rc < 0) {
+		fprintf(stderr, "can't claim interface; rc=%d\n", rc);
+		goto close_exit;
 	}
 
 	as = apdu_split_init(&apdu_out_cb, NULL);
 	if (!as)
-		exit(1);
+		goto release_exit;
 
 	printf("Entering main loop\n");
 	while (1) {
-		rc = usb_bulk_read(udev, SIMTRACE_IN_EP, buf, sizeof(buf), 100000);
-		if (rc < 0 && rc != -EAGAIN) {
-			fprintf(stderr, "Error submitting BULK IN urb: %s\n", usb_strerror());
-			exit(1);
+		rc = libusb_bulk_transfer(devh, SIMTRACE_IN_EP, buf, sizeof(buf), &xfer_len, 100000);
+		if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT) {
+			fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
+			goto release_exit;
 		}
-		if (rc > 0) {
+		if (xfer_len > 0) {
 			//printf("URB: %s\n", osmo_hexdump(buf, rc));
-			process_usb_msg(buf, rc);
+			process_usb_msg(buf, xfer_len);
 			msg_count++;
-			byte_count += rc;
+			byte_count += xfer_len;
 		}
 	}
+	ret = 0;
+
+release_exit:
+	libusb_release_interface(devh, 0);
+close_exit:
+	if (devh)
+		libusb_close(devh);
+	libusb_exit(NULL);
+	return ret;
 }
diff --git a/host/usb.c b/host/usb.c
deleted file mode 100644
index 1d3aaf4..0000000
--- a/host/usb.c
+++ /dev/null
@@ -1,87 +0,0 @@
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <usb.h>
-#include <sys/ioctl.h>
-#include "usb.h"
-#include <linux/usbdevice_fs.h>
-
-#define MAX_READ_WRITE	4096
-
-#define USB_ERROR_STR(ret, x, args...)	return ret
-
-static int usb_get_fd(usb_dev_handle *uh)
-{
-	return *((int *)uh);
-}
-
-int __usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int length,
-		     int timeout)
-{
-	struct usbdevfs_bulktransfer bulk;
-	int ret, sent = 0;
-
-	/* Ensure the endpoint address is correct */
-	ep &= ~USB_ENDPOINT_IN;
-
-	do {
-		bulk.ep = ep;
-		bulk.len = length - sent;
-		if (bulk.len > MAX_READ_WRITE)
-			bulk.len = MAX_READ_WRITE;
-		bulk.timeout = timeout;
-		bulk.data = (unsigned char *)bytes + sent;
-
-		ret = ioctl(usb_get_fd(dev), USBDEVFS_BULK, &bulk);
-		if (ret < 0)
-			USB_ERROR_STR(ret,
-				      "error writing to bulk endpoint %d: %s",
-				      ep, strerror(errno));
-
-		sent += ret;
-	} while (ret > 0 && sent < length);
-
-	return sent;
-}
-
-int __usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size,
-		    int timeout)
-{
-	struct usbdevfs_bulktransfer bulk;
-	int ret, retrieved = 0, requested;
-
-	/* Ensure the endpoint address is correct */
-	ep |= USB_ENDPOINT_IN;
-
-	do {
-		bulk.ep = ep;
-		requested = size - retrieved;
-		if (requested > MAX_READ_WRITE)
-			requested = MAX_READ_WRITE;
-		bulk.len = requested;
-		bulk.timeout = timeout;
-		bulk.data = (unsigned char *)bytes + retrieved;
-
-		ret = ioctl(usb_get_fd(dev), USBDEVFS_BULK, &bulk);
-		if (ret < 0)
-			USB_ERROR_STR(ret,
-				      "error reading from bulk endpoint 0x%x: %s",
-				      ep, strerror(errno));
-
-		retrieved += ret;
-	} while (ret > 0 && retrieved < size && ret == requested);
-
-	return retrieved;
-}
-
-int __usb_reattach_kernel_driver_np(usb_dev_handle *dev, int interface)
-{
-	struct usbdevfs_ioctl command;
-
-	command.ifno = interface;
-	command.ioctl_code = USBDEVFS_CONNECT;
-	command.data = NULL;
-
-	return ioctl(usb_get_fd(dev), USBDEVFS_IOCTL, &command);
-}
diff --git a/host/usb_helper.c b/host/usb_helper.c
deleted file mode 100644
index 231363a..0000000
--- a/host/usb_helper.c
+++ /dev/null
@@ -1,76 +0,0 @@
-/* usb_helper - Low-Level USB routines for SimTrace
- *
- * (C) 2006-2010 by Harald Welte <hwelte at hmw-consulting.de>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 2 
- *  as published by the Free Software Foundation
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include <errno.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-#include <time.h>
-#include <sys/time.h>
-
-#include <sys/types.h>
-
-#include <usb.h>
-
-static struct usb_device *find_usb_device (uint16_t vendor_id, uint16_t product_id)
-{
-	struct usb_bus *bus;
-
-	for (bus = usb_busses; bus; bus = bus->next) {
-		struct usb_device *dev;
-		for (dev = bus->devices; dev; dev = dev->next) {
-			if (dev->descriptor.idVendor == vendor_id &&
-			    dev->descriptor.idProduct == product_id)
-				return dev;
-		}
-	}
-	return NULL;
-}
-
-struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t product_id)
-{
-	struct usb_device *dev;
-	struct usb_dev_handle *hdl;
-
-	usb_init();
-	usb_find_busses();
-	usb_find_devices();
-
-	dev = find_usb_device(vendor_id, product_id);
-	if (!dev) {
-		fprintf(stderr, "Cannot find matching USB Device. "
-			"Are you sure it is connected?\n");
-		exit(1);
-	}
-
-	hdl = usb_open(dev);
-	if (!hdl) {
-		fprintf(stderr, "Unable to open usb device: %s\n",
-			usb_strerror());
-		exit(1);
-	}
-
-	if (usb_claim_interface(hdl, 0) < 0) {
-		fprintf(stderr, "Unable to claim usb interface "
-			"1 of device: %s\n", usb_strerror());
-		exit(1);
-	}
-
-	return hdl;
-}
diff --git a/host/usb_helper.h b/host/usb_helper.h
deleted file mode 100644
index 13f96f8..0000000
--- a/host/usb_helper.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#ifndef _USB_HELPER_H
-#define _USB_HELPER_H
-
-/* usb_helper - Low-Level USB routines for SimTrace
- *
- * (C) 2006-2010 by Harald Welte <hwelte at hmw-consulting.de>
- *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License version 2 
- *  as published by the Free Software Foundation
- *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
- */
-
-#include <stdint.h>
-
-struct usb_dev_handle *usb_find_open(uint16_t vendor_id, uint16_t product_id);
-
-#endif
-- 
1.7.4.1.343.ga91df.dirty





More information about the simtrace mailing list