PERFORCE change 160823 for review

Hans Petter Selasky hselasky at FreeBSD.org
Sun Apr 19 17:24:59 UTC 2009


http://perforce.freebsd.org/chv.cgi?CH=160823

Change 160823 by hselasky at hselasky_laptop001 on 2009/04/19 17:24:09

	
	Patches from HPS for libusb v1.0 compat layer.

Affected files ...

.. //depot/projects/usb/src/lib/libusb/libusb.h#2 edit
.. //depot/projects/usb/src/lib/libusb/libusb10.c#2 edit

Differences ...

==== //depot/projects/usb/src/lib/libusb/libusb.h#2 (text+ko) ====

@@ -222,7 +222,7 @@
 
 	struct list_head list;
 	unsigned long session_data;
-	unsigned char os_priv[0];
+	void   *os_priv;
 }	libusb_device;
 
 typedef struct libusb_device_handle {
@@ -231,7 +231,7 @@
 
 	struct list_head list;
 	struct libusb_device *dev;
-	unsigned char os_priv[0];
+	void   *os_priv;
 }	libusb_device_handle;
 
 typedef struct libusb_device_descriptor {

==== //depot/projects/usb/src/lib/libusb/libusb10.c#2 (text+ko) ====

@@ -54,9 +54,6 @@
 	(entry)->next->prev = (entry)->prev; \
 	(entry)->prev->next = (entry)->next;
 
-
-static struct libusb20_backend *usb_backend = NULL;
-
 /*  Library initialisation / deinitialisation */
 
 void
@@ -64,7 +61,6 @@
 {
 	if (ctx)
 		ctx->debug = level;
-	return;
 }
 
 int
@@ -75,6 +71,7 @@
 	ctx = malloc(sizeof(*ctx));
 	if (!ctx)
 		return (LIBUSB_ERROR_INVALID_PARAM);
+
 	memset(ctx, 0, sizeof(*ctx));
 
 	pthread_mutex_init(&ctx->usb_devs_lock, NULL);
@@ -103,43 +100,56 @@
 	struct libusb20_device *pdev;
 	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
 	struct libusb_device *dev;
-	int i = 0;
+	struct libusb20_backend *usb_backend;
+	int i;
 
 	usb_backend = libusb20_be_alloc_default();
-	if (usb_backend == NULL) {
+	if (usb_backend == NULL)
 		return (-1);
-	}
+
 	pdev = NULL;
+	i = 0;
 	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
 		i++;
 
-	if (!list)
+	if (!list) {
+		libusb20_be_free(usb_backend);
 		return (LIBUSB_ERROR_INVALID_PARAM);
+	}
 	*list = malloc((i + 1) * sizeof(void *));
-	if (*list == NULL)
-		return LIBUSB_ERROR_NO_MEM;
+	if (*list == NULL) {
+		libusb20_be_free(usb_backend);
+		return (LIBUSB_ERROR_NO_MEM);
+	}
+	i = 0;
+	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
+		/* get device into libUSB v1.0 list */
+		libusb20_be_dequeue_device(usb_backend, pdev);
 
-	pdev = NULL;
-	i = 0;
-	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
 		ddesc = libusb20_dev_get_device_desc(pdev);
-
-		dev = malloc(sizeof(*dev) + sizeof(*pdev));
+		dev = malloc(sizeof(*dev));
+		if (dev == NULL) {
+			free(*list);
+			libusb20_be_free(usb_backend);
+			return (LIBUSB_ERROR_NO_MEM);
+		}
+		memset(dev, 0, sizeof(*dev));
 
 		pthread_mutex_init(&dev->lock, NULL);
-		dev->refcnt = 0;
-
 		dev->ctx = ctx;
 		dev->bus_number = pdev->bus_number;
 		dev->device_address = pdev->device_address;
 		dev->num_configurations = ddesc->bNumConfigurations;
-		memcpy(dev->os_priv, pdev, sizeof(pdev));
+
+		/* link together the two structures */
+		dev->os_priv = pdev;
 
 		(*list)[i] = libusb_ref_device(dev);
 		i++;
 	}
 	(*list)[i] = NULL;
 
+	libusb20_be_free(usb_backend);
 	return (i);
 }
 
@@ -200,27 +210,43 @@
 	pthread_mutex_lock(&dev->lock);
 	dev->refcnt--;
 	pthread_mutex_unlock(&dev->lock);
+	if (dev->refcnt == 0) {
+		libusb20_dev_free(dev->os_priv);
+		free(dev);
+	}
 }
 
 int
 libusb_open(libusb_device * dev, libusb_device_handle ** handle)
 {
 	libusb_context *ctx = dev->ctx;
+	struct libusb20_device *pdev = dev->os_priv;
 	libusb_device_handle *hdl;
+	int err;
 
+	if (handle == NULL)
+		return (LIBUSB_ERROR_INVALID_PARAM);
+
 	hdl = malloc(sizeof(*hdl));
-	if (!handle || hdl)
-		return LIBUSB_ERROR_NO_MEM;
+	if (hdl == NULL)
+		return (LIBUSB_ERROR_NO_MEM);
 
+	err = libusb20_dev_open(pdev, 16 * 2 /* number of endpoints */ );
+	if (err) {
+		free(hdl);
+		return (LIBUSB_ERROR_NO_MEM);
+	}
 	memset(hdl, 0, sizeof(*hdl));
 	pthread_mutex_init(&hdl->lock, NULL);
+
 	hdl->dev = libusb_ref_device(dev);
 	hdl->claimed_interfaces = 0;
-
+	hdl->os_priv = dev->os_priv;
 
 	pthread_mutex_lock(&ctx->open_devs_lock);
 	LIST_ADD(&hdl->list, &ctx->open_devs);
 	pthread_mutex_unlock(&ctx->open_devs_lock);
+
 	*handle = hdl;
 
 	/*
@@ -257,13 +283,19 @@
 void
 libusb_close(libusb_device_handle * devh)
 {
-	pthread_mutex_lock(&(devh->dev->ctx->open_devs_lock));
+	libusb_context *ctx;
+	struct libusb20_device *pdev;
+	int err;
+
+	ctx = devh->dev->ctx;
+	pdev = devh->os_priv;
+
+	pthread_mutex_lock(&(ctx->open_devs_lock));
+	err = libusb20_dev_close(pdev);
 	LIST_DEL(&(devh->list));
-	pthread_mutex_unlock(&(devh->dev->ctx->open_devs_lock));
+	pthread_mutex_unlock(&(ctx->open_devs_lock));
 	libusb_unref_device(devh->dev);
 	free(devh);
-
-	return;
 }
 
 libusb_device *


More information about the p4-projects mailing list