From owner-p4-projects@FreeBSD.ORG Sun Apr 19 17:24:58 2009 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 2CB091065674; Sun, 19 Apr 2009 17:24:58 +0000 (UTC) Delivered-To: perforce@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C8301106564A for ; Sun, 19 Apr 2009 17:24:57 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repoman.freebsd.org (repoman.freebsd.org [IPv6:2001:4f8:fff6::29]) by mx1.freebsd.org (Postfix) with ESMTP id B60088FC16 for ; Sun, 19 Apr 2009 17:24:57 +0000 (UTC) (envelope-from hselasky@FreeBSD.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.14.3/8.14.3) with ESMTP id n3JHOvTe077421 for ; Sun, 19 Apr 2009 17:24:57 GMT (envelope-from hselasky@FreeBSD.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.14.3/8.14.3/Submit) id n3JHOvtq077419 for perforce@freebsd.org; Sun, 19 Apr 2009 17:24:57 GMT (envelope-from hselasky@FreeBSD.org) Date: Sun, 19 Apr 2009 17:24:57 GMT Message-Id: <200904191724.n3JHOvtq077419@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to hselasky@FreeBSD.org using -f From: Hans Petter Selasky To: Perforce Change Reviews Cc: Subject: PERFORCE change 160823 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 19 Apr 2009 17:24:59 -0000 http://perforce.freebsd.org/chv.cgi?CH=160823 Change 160823 by hselasky@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 *