svn commit: r194676 - head/lib/libusb

Andrew Thompson thompsa at FreeBSD.org
Tue Jun 23 01:04:59 UTC 2009


Author: thompsa
Date: Tue Jun 23 01:04:58 2009
New Revision: 194676
URL: http://svn.freebsd.org/changeset/base/194676

Log:
  Add files missed in r194674.
  
  Add libusb 1.0 support which is compatible with the latest revision on
  Sourceforge. Libusb 1.0 is a portable usb api released December 2008 and
  supersedes the original libusb released 10 years ago, it supports isochronous
  endpoints and asynchronous I/O.  Many applications have already started using
  the interfaces.
  
  This has been developed as part of Google Summer of Code this year by Sylvestre
  Gallon and has been cribbed early due to it being desirable in FreeBSD 8.0
  
  Submitted by: Sylvestre Gallon
  Sponsored by: Google Summer of Code 2009
  Reviewed by:  Hans Petter Selasky

Added:
  head/lib/libusb/libusb.h   (contents, props changed)
  head/lib/libusb/libusb10.c   (contents, props changed)
  head/lib/libusb/libusb10.h   (contents, props changed)
  head/lib/libusb/libusb10_desc.c   (contents, props changed)
  head/lib/libusb/libusb10_io.c   (contents, props changed)

Added: head/lib/libusb/libusb.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libusb/libusb.h	Tue Jun 23 01:04:58 2009	(r194676)
@@ -0,0 +1,427 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef __LIBUSB_H__
+#define	__LIBUSB_H__
+
+#include <stdint.h>
+#include <time.h>
+#include <string.h>
+#include <pthread.h>
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/endian.h>
+
+#ifdef __cplusplus
+extern	"C" {
+#endif
+#if 0
+}					/* indent fix */
+
+#endif
+
+struct list_head {
+	struct list_head *prev, *next;
+};
+
+/* libusb enums */
+
+enum libusb_class_code {
+	LIBUSB_CLASS_PER_INTERFACE = 0,
+	LIBUSB_CLASS_AUDIO = 1,
+	LIBUSB_CLASS_COMM = 2,
+	LIBUSB_CLASS_HID = 3,
+	LIBUSB_CLASS_PTP = 6,
+	LIBUSB_CLASS_PRINTER = 7,
+	LIBUSB_CLASS_MASS_STORAGE = 8,
+	LIBUSB_CLASS_HUB = 9,
+	LIBUSB_CLASS_DATA = 10,
+	LIBUSB_CLASS_VENDOR_SPEC = 0xff,
+};
+
+enum libusb_descriptor_type {
+	LIBUSB_DT_DEVICE = 0x01,
+	LIBUSB_DT_CONFIG = 0x02,
+	LIBUSB_DT_STRING = 0x03,
+	LIBUSB_DT_INTERFACE = 0x04,
+	LIBUSB_DT_ENDPOINT = 0x05,
+	LIBUSB_DT_HID = 0x21,
+	LIBUSB_DT_REPORT = 0x22,
+	LIBUSB_DT_PHYSICAL = 0x23,
+	LIBUSB_DT_HUB = 0x29,
+};
+
+#define	LIBUSB_DT_DEVICE_SIZE		18
+#define	LIBUSB_DT_CONFIG_SIZE		9
+#define	LIBUSB_DT_INTERFACE_SIZE	9
+#define	LIBUSB_DT_ENDPOINT_SIZE		7
+#define	LIBUSB_DT_ENDPOINT_AUDIO_SIZE	9
+#define	LIBUSB_DT_HUB_NONVAR_SIZE	7
+
+#define	LIBUSB_ENDPOINT_ADDRESS_MASK	0x0f
+#define	LIBUSB_ENDPOINT_DIR_MASK	0x80
+
+enum libusb_endpoint_direction {
+	LIBUSB_ENDPOINT_IN = 0x80,
+	LIBUSB_ENDPOINT_OUT = 0x00,
+};
+
+#define	LIBUSB_TRANSFER_TYPE_MASK	0x03
+
+enum libusb_transfer_type {
+	LIBUSB_TRANSFER_TYPE_CONTROL = 0,
+	LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1,
+	LIBUSB_TRANSFER_TYPE_BULK = 2,
+	LIBUSB_TRANSFER_TYPE_INTERRUPT = 3,
+};
+
+enum libusb_standard_request {
+	LIBUSB_REQUEST_GET_STATUS = 0x00,
+	LIBUSB_REQUEST_CLEAR_FEATURE = 0x01,
+	LIBUSB_REQUEST_SET_FEATURE = 0x03,
+	LIBUSB_REQUEST_SET_ADDRESS = 0x05,
+	LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06,
+	LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07,
+	LIBUSB_REQUEST_GET_CONFIGURATION = 0x08,
+	LIBUSB_REQUEST_SET_CONFIGURATION = 0x09,
+	LIBUSB_REQUEST_GET_INTERFACE = 0x0A,
+	LIBUSB_REQUEST_SET_INTERFACE = 0x0B,
+	LIBUSB_REQUEST_SYNCH_FRAME = 0x0C,
+};
+
+enum libusb_request_type {
+	LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5),
+	LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5),
+	LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5),
+	LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5),
+};
+
+enum libusb_request_recipient {
+	LIBUSB_RECIPIENT_DEVICE = 0x00,
+	LIBUSB_RECIPIENT_INTERFACE = 0x01,
+	LIBUSB_RECIPIENT_ENDPOINT = 0x02,
+	LIBUSB_RECIPIENT_OTHER = 0x03,
+};
+
+#define	LIBUSB_ISO_SYNC_TYPE_MASK	0x0C
+
+enum libusb_iso_sync_type {
+	LIBUSB_ISO_SYNC_TYPE_NONE = 0,
+	LIBUSB_ISO_SYNC_TYPE_ASYNC = 1,
+	LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2,
+	LIBUSB_ISO_SYNC_TYPE_SYNC = 3,
+};
+
+#define	LIBUSB_ISO_USAGE_TYPE_MASK 0x30
+
+enum libusb_iso_usage_type {
+	LIBUSB_ISO_USAGE_TYPE_DATA = 0,
+	LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1,
+	LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2,
+};
+
+enum libusb_error {
+	LIBUSB_SUCCESS = 0,
+	LIBUSB_ERROR_IO = -1,
+	LIBUSB_ERROR_INVALID_PARAM = -2,
+	LIBUSB_ERROR_ACCESS = -3,
+	LIBUSB_ERROR_NO_DEVICE = -4,
+	LIBUSB_ERROR_NOT_FOUND = -5,
+	LIBUSB_ERROR_BUSY = -6,
+	LIBUSB_ERROR_TIMEOUT = -7,
+	LIBUSB_ERROR_OVERFLOW = -8,
+	LIBUSB_ERROR_PIPE = -9,
+	LIBUSB_ERROR_INTERRUPTED = -10,
+	LIBUSB_ERROR_NO_MEM = -11,
+	LIBUSB_ERROR_NOT_SUPPORTED = -12,
+	LIBUSB_ERROR_OTHER = -99,
+};
+
+enum libusb_transfer_status {
+	LIBUSB_TRANSFER_COMPLETED,
+	LIBUSB_TRANSFER_ERROR,
+	LIBUSB_TRANSFER_TIMED_OUT,
+	LIBUSB_TRANSFER_CANCELLED,
+	LIBUSB_TRANSFER_STALL,
+	LIBUSB_TRANSFER_NO_DEVICE,
+	LIBUSB_TRANSFER_OVERFLOW,
+};
+
+enum libusb_transfer_flags {
+	LIBUSB_TRANSFER_SHORT_NOT_OK = 1 << 0,
+	LIBUSB_TRANSFER_FREE_BUFFER = 1 << 1,
+	LIBUSB_TRANSFER_FREE_TRANSFER = 1 << 2,
+};
+
+enum libusb_debug_level {
+	LIBUSB_DEBUG_NO=0,
+	LIBUSB_DEBUG_FUNCTION=1,
+	LIBUSB_DEBUG_TRANSFER=2,
+};
+
+/* libusb structures */
+
+typedef void (*libusb_pollfd_added_cb) (int fd, short events, void *user_data);
+typedef void (*libusb_pollfd_removed_cb) (int fd, void *user_data);
+
+typedef struct libusb_context {
+	int	debug;
+	int	debug_fixed;
+
+	int	ctrl_pipe[2];
+
+	struct list_head usb_devs;
+	pthread_mutex_t usb_devs_lock;
+
+	struct list_head open_devs;
+	pthread_mutex_t open_devs_lock;
+
+	struct list_head flying_transfers;
+	pthread_mutex_t flying_transfers_lock;
+
+	struct list_head pollfds;
+	pthread_mutex_t pollfds_lock;
+
+	unsigned int pollfd_modify;
+	pthread_mutex_t pollfd_modify_lock;
+
+	libusb_pollfd_added_cb fd_added_cb;
+	libusb_pollfd_removed_cb fd_removed_cb;
+	void   *fd_cb_user_data;
+
+	pthread_mutex_t events_lock;
+	int	event_handler_active;
+
+	pthread_mutex_t event_waiters_lock;
+	pthread_cond_t event_waiters_cond;
+}	libusb_context;
+
+typedef struct libusb_device {
+	pthread_mutex_t lock;
+	int	refcnt;
+
+	struct libusb_context *ctx;
+
+	uint8_t	bus_number;
+	uint8_t	device_address;
+	uint8_t	num_configurations;
+
+	struct list_head list;
+	unsigned long session_data;
+	void   *os_priv;
+}	libusb_device;
+
+typedef struct libusb_device_handle {
+	pthread_mutex_t lock;
+	unsigned long claimed_interfaces;
+
+	struct list_head list;
+	struct libusb_device *dev;
+	void   *os_priv;
+}	libusb_device_handle;
+
+typedef struct libusb_device_descriptor {
+	uint8_t	bLength;
+	uint8_t	bDescriptorType;
+	uint16_t bcdUSB;
+	uint8_t	bDeviceClass;
+	uint8_t	bDeviceSubClass;
+	uint8_t	bDeviceProtocol;
+	uint8_t	bMaxPacketSize0;
+	uint16_t idVendor;
+	uint16_t idProduct;
+	uint16_t bcdDevice;
+	uint8_t	iManufacturer;
+	uint8_t	iProduct;
+	uint8_t	iSerialNumber;
+	uint8_t	bNumConfigurations;
+}	libusb_device_descriptor;
+
+typedef struct libusb_endpoint_descriptor {
+	uint8_t	bLength;
+	uint8_t	bDescriptorType;
+	uint8_t	bEndpointAddress;
+	uint8_t	bmAttributes;
+	uint16_t wMaxPacketSize;
+	uint8_t	bInterval;
+	uint8_t	bRefresh;
+	uint8_t	bSynchAddress;
+	unsigned char *extra;
+	int	extra_length;
+}	libusb_endpoint_descriptor __aligned(sizeof(void *));
+
+typedef struct libusb_interface_descriptor {
+	uint8_t	bLength;
+	uint8_t	bDescriptorType;
+	uint8_t	bInterfaceNumber;
+	uint8_t	bAlternateSetting;
+	uint8_t	bNumEndpoints;
+	uint8_t	bInterfaceClass;
+	uint8_t	bInterfaceSubClass;
+	uint8_t	bInterfaceProtocol;
+	uint8_t	iInterface;
+	struct libusb_endpoint_descriptor *endpoint;
+	unsigned char *extra;
+	int	extra_length;
+}	libusb_interface_descriptor __aligned(sizeof(void *));
+
+typedef struct libusb_interface {
+	struct libusb_interface_descriptor *altsetting;
+	int	num_altsetting;
+}	libusb_interface __aligned(sizeof(void *));
+
+typedef struct libusb_config_descriptor {
+	uint8_t	bLength;
+	uint8_t	bDescriptorType;
+	uint16_t wTotalLength;
+	uint8_t	bNumInterfaces;
+	uint8_t	bConfigurationValue;
+	uint8_t	iConfiguration;
+	uint8_t	bmAttributes;
+	uint8_t	MaxPower;
+	struct libusb_interface *interface;
+	unsigned char *extra;
+	int	extra_length;
+}	libusb_config_descriptor __aligned(sizeof(void *));
+
+typedef struct libusb_control_setup {
+	uint8_t	bmRequestType;
+	uint8_t	bRequest;
+	uint16_t wValue;
+	uint16_t wIndex;
+	uint16_t wLength;
+}	libusb_control_setup;
+
+typedef struct libusb_iso_packet_descriptor {
+	unsigned int length;
+	unsigned int actual_length;
+	enum libusb_transfer_status status;
+}	libusb_iso_packet_descriptor __aligned(sizeof(void *));
+
+struct libusb_transfer;
+
+typedef void (*libusb_transfer_cb_fn) (struct libusb_transfer *transfer);
+
+typedef struct libusb_transfer {
+	libusb_device_handle *dev_handle;
+	uint8_t	flags;
+	unsigned int endpoint;
+	unsigned char type;
+	unsigned int timeout;
+	enum libusb_transfer_status status;
+	int	length;
+	int	actual_length;
+	libusb_transfer_cb_fn callback;
+	void   *user_data;
+	unsigned char *buffer;
+	void *os_priv;
+	int	num_iso_packets;
+	struct libusb_iso_packet_descriptor iso_packet_desc[0];
+}	libusb_transfer __aligned(sizeof(void *));
+
+typedef struct libusb_pollfd {
+	int	fd;
+	short	events;
+}	libusb_pollfd;
+
+/* Library initialisation */
+
+void	libusb_set_debug(libusb_context * ctx, int level);
+int	libusb_init(libusb_context ** context);
+void	libusb_exit(struct libusb_context *ctx);
+
+/* Device handling and enumeration */
+
+ssize_t libusb_get_device_list(libusb_context * ctx, libusb_device *** list);
+void	libusb_free_device_list(libusb_device ** list, int unref_devices);
+uint8_t	libusb_get_bus_number(libusb_device * dev);
+uint8_t	libusb_get_device_address(libusb_device * dev);
+int	libusb_get_max_packet_size(libusb_device * dev, unsigned char endpoint);
+libusb_device *libusb_ref_device(libusb_device * dev);
+void	libusb_unref_device(libusb_device * dev);
+int	libusb_open(libusb_device * dev, libusb_device_handle ** devh);
+libusb_device_handle *libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id, uint16_t product_id);
+void	libusb_close(libusb_device_handle * devh);
+libusb_device *libusb_get_device(libusb_device_handle * devh);
+int	libusb_get_configuration(libusb_device_handle * devh, int *config);
+int	libusb_set_configuration(libusb_device_handle * devh, int configuration);
+int	libusb_claim_interface(libusb_device_handle * devh, int interface_number);
+int	libusb_release_interface(libusb_device_handle * devh, int interface_number);
+int 	libusb_kernel_driver_active(libusb_device_handle * devh, int interface);
+int 	libusb_detach_kernel_driver(libusb_device_handle * devh, int interface);
+int 	libusb_attach_kernel_driver(libusb_device_handle * devh, int interface);
+int	libusb_set_interface_alt_setting(libusb_device_handle * devh, int interface_number, int alternate_setting);
+
+/* USB Descriptors */
+
+int	libusb_get_device_descriptor(libusb_device * dev, struct libusb_device_descriptor *desc);
+int	libusb_get_active_config_descriptor(libusb_device * dev, struct libusb_config_descriptor **config);
+int	libusb_get_config_descriptor(libusb_device * dev, uint8_t config_index, struct libusb_config_descriptor **config);
+int	libusb_get_config_descriptor_by_value(libusb_device * dev, uint8_t bConfigurationValue, struct libusb_config_descriptor **config);
+void	libusb_free_config_descriptor(struct libusb_config_descriptor *config);
+int	libusb_get_string_descriptor_ascii(libusb_device_handle * dev, uint8_t desc_index, unsigned char *data, int length);
+
+/* Asynchronous device I/O*/
+
+struct libusb_transfer *libusb_alloc_transfer(int iso_packets);
+void	libusb_free_transfer(struct libusb_transfer *transfer);
+int	libusb_submit_transfer(struct libusb_transfer *transfer);
+int	libusb_cancel_transfer(struct libusb_transfer *transfer);
+unsigned char *libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, unsigned int packet);
+
+/* Polling and timing */
+
+int	libusb_try_lock_events(libusb_context * ctx);
+void	libusb_lock_events(libusb_context * ctx);
+void	libusb_unlock_events(libusb_context * ctx);
+int	libusb_event_handling_ok(libusb_context * ctx);
+int	libusb_event_handler_active(libusb_context * ctx);
+void	libusb_lock_event_waiters(libusb_context * ctx);
+void	libusb_unlock_event_waiters(libusb_context * ctx);
+int	libusb_wait_for_event(libusb_context * ctx, struct timeval *tv);
+int	libusb_handle_events_timeout(libusb_context * ctx, struct timeval *tv);
+int	libusb_handle_events(libusb_context * ctx);
+int	libusb_handle_events_locked(libusb_context * ctx, struct timeval *tv);
+int	libusb_get_next_timeout(libusb_context * ctx, struct timeval *tv);
+void	libusb_set_pollfd_notifiers(libusb_context * ctx, libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, void *user_data);
+struct libusb_pollfd **libusb_get_pollfds(libusb_context * ctx);
+
+/* Synchronous device I/O */
+
+int	libusb_control_transfer(libusb_device_handle * devh, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, unsigned int timeout);
+int	libusb_bulk_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
+int	libusb_interrupt_transfer(struct libusb_device_handle *devh, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned int timeout);
+
+#if 0
+{					/* indent fix */
+#endif
+#ifdef __cplusplus
+}
+
+#endif
+
+#endif					/* __LIBUSB_H__ */

Added: head/lib/libusb/libusb10.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libusb/libusb10.c	Tue Jun 23 01:04:58 2009	(r194676)
@@ -0,0 +1,1140 @@
+/* $FreeBSD$ */
+/*-
+ * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/queue.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <poll.h>
+#include <pthread.h>
+#include <time.h>
+#include <errno.h>
+
+#include "libusb20.h"
+#include "libusb20_desc.h"
+#include "libusb20_int.h"
+#include "libusb.h"
+#include "libusb10.h"
+
+static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
+struct libusb_context *usbi_default_context = NULL;
+pthread_mutex_t libusb20_lock = PTHREAD_MUTEX_INITIALIZER;
+
+/*  Library initialisation / deinitialisation */
+
+void
+libusb_set_debug(libusb_context * ctx, int level)
+{
+	GET_CONTEXT(ctx);
+	if (ctx)
+		ctx->debug = level;
+}
+
+int
+libusb_init(libusb_context ** context)
+{
+	struct libusb_context *ctx;
+	char * debug;
+	int ret;
+
+	ctx = malloc(sizeof(*ctx));
+	if (!ctx)
+		return (LIBUSB_ERROR_INVALID_PARAM);
+
+	memset(ctx, 0, sizeof(*ctx));
+
+	debug = getenv("LIBUSB_DEBUG");
+	if (debug != NULL) {
+		ctx->debug = atoi(debug);
+		if (ctx->debug != 0)
+			ctx->debug_fixed = 1;
+	}
+
+	pthread_mutex_init(&ctx->usb_devs_lock, NULL);
+	pthread_mutex_init(&ctx->open_devs_lock, NULL);
+	USB_LIST_INIT(&ctx->usb_devs);
+	USB_LIST_INIT(&ctx->open_devs);
+
+	pthread_mutex_init(&ctx->flying_transfers_lock, NULL);
+	pthread_mutex_init(&ctx->pollfds_lock, NULL);
+	pthread_mutex_init(&ctx->pollfd_modify_lock, NULL);
+	pthread_mutex_init(&ctx->events_lock, NULL);
+	pthread_mutex_init(&ctx->event_waiters_lock, NULL);
+	pthread_cond_init(&ctx->event_waiters_cond, NULL);
+
+	USB_LIST_INIT(&ctx->flying_transfers);
+	USB_LIST_INIT(&ctx->pollfds);
+
+	ret = pipe(ctx->ctrl_pipe);
+	if (ret < 0) {
+		usb_remove_pollfd(ctx, ctx->ctrl_pipe[0]);
+		close(ctx->ctrl_pipe[0]);
+		close(ctx->ctrl_pipe[1]);
+		free(ctx);
+		return (LIBUSB_ERROR_OTHER);
+	}
+
+	ret = usb_add_pollfd(ctx, ctx->ctrl_pipe[0], POLLIN);
+	if (ret < 0) {
+		usb_remove_pollfd(ctx, ctx->ctrl_pipe[0]);
+		close(ctx->ctrl_pipe[0]);
+		close(ctx->ctrl_pipe[1]);
+		free(ctx);
+		return ret;
+	}
+
+	pthread_mutex_lock(&default_context_lock);
+	if (usbi_default_context == NULL) {
+		usbi_default_context = ctx;
+	}
+	pthread_mutex_unlock(&default_context_lock);
+
+	if (context)
+		*context = ctx;
+
+	return (0);
+}
+
+void
+libusb_exit(libusb_context * ctx)
+{
+	GET_CONTEXT(ctx);
+
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_exit enter");
+	usb_remove_pollfd(ctx, ctx->ctrl_pipe[0]);
+	close(ctx->ctrl_pipe[0]);
+	close(ctx->ctrl_pipe[1]);
+
+	pthread_mutex_lock(&default_context_lock);
+	if (ctx == usbi_default_context) {
+		usbi_default_context = NULL;
+	}
+	pthread_mutex_unlock(&default_context_lock);
+
+	free(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_exit leave");
+}
+
+/* Device handling and initialisation. */
+
+ssize_t
+libusb_get_device_list(libusb_context * ctx, libusb_device *** list)
+{
+	struct libusb20_device *pdev;
+	struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
+	struct libusb_device *dev;
+	struct libusb20_backend *usb_backend;
+	int i;
+
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device_list enter");
+
+	usb_backend = libusb20_be_alloc_default();
+	if (usb_backend == NULL)
+		return (-1);
+
+	pdev = NULL;
+	i = 0;
+	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
+		i++;
+
+	if (list == NULL) {
+		libusb20_be_free(usb_backend);
+		return (LIBUSB_ERROR_INVALID_PARAM);
+	}
+	*list = malloc((i + 1) * sizeof(void *));
+	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);
+
+		ddesc = libusb20_dev_get_device_desc(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->ctx = ctx;
+		dev->bus_number = pdev->bus_number;
+		dev->device_address = pdev->device_address;
+		dev->num_configurations = ddesc->bNumConfigurations;
+
+		/* link together the two structures */
+		dev->os_priv = pdev;
+
+		pthread_mutex_lock(&ctx->usb_devs_lock);
+		LIST_ADD(&dev->list, &ctx->usb_devs);
+		pthread_mutex_unlock(&ctx->usb_devs_lock);
+
+		(*list)[i] = libusb_ref_device(dev);
+		i++;
+	}
+	(*list)[i] = NULL;
+
+	libusb20_be_free(usb_backend);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device_list leave");
+	return (i);
+}
+
+/*
+ * In this function we cant free all the device contained into list because
+ * open_with_pid_vid use some node of list after the free_device_list.
+ */
+void
+libusb_free_device_list(libusb_device **list, int unref_devices)
+{
+	int i;
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_free_device_list enter");
+
+	if (list == NULL)
+		return ;
+
+	if (unref_devices) {
+		for (i = 0; list[i] != NULL; i++)
+			libusb_unref_device(list[i]);
+	}
+	free(list);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_free_device_list leave");
+}
+
+uint8_t
+libusb_get_bus_number(libusb_device * dev)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_bus_number enter");
+
+	if (dev == NULL)
+		return (LIBUSB_ERROR_NO_DEVICE);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_bus_number leave");
+	return (dev->bus_number);
+}
+
+uint8_t
+libusb_get_device_address(libusb_device * dev)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device_address enter");
+
+	if (dev == NULL)
+		return (LIBUSB_ERROR_NO_DEVICE);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device_address leave");
+	return (dev->device_address);
+}
+
+int
+libusb_get_max_packet_size(libusb_device *dev, unsigned char endpoint)
+{
+	struct libusb_config_descriptor *pdconf;
+	struct libusb_interface *pinf;
+	struct libusb_interface_descriptor *pdinf;
+	struct libusb_endpoint_descriptor *pdend;
+	libusb_context *ctx;
+	int i, j, k, ret;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_max_packet_size enter");
+
+	if (dev == NULL)
+		return (LIBUSB_ERROR_NO_DEVICE);
+
+	if (libusb_get_active_config_descriptor(dev, &pdconf) < 0) 
+		return (LIBUSB_ERROR_OTHER);
+ 
+	ret = LIBUSB_ERROR_NOT_FOUND;
+	for (i = 0 ; i < pdconf->bNumInterfaces ; i++) {
+		pinf = &pdconf->interface[i];
+		for (j = 0 ; j < pinf->num_altsetting ; j++) {
+			pdinf = &pinf->altsetting[j];
+			for (k = 0 ; k < pdinf->bNumEndpoints ; k++) {
+				pdend = &pdinf->endpoint[k];
+				if (pdend->bEndpointAddress == endpoint) {
+					ret = pdend->wMaxPacketSize;
+					goto out;
+				}
+			}
+		}
+	}
+
+out:
+	libusb_free_config_descriptor(pdconf);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_max_packet_size leave");
+	return (ret);
+}
+
+libusb_device *
+libusb_ref_device(libusb_device * dev)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_ref_device enter");
+
+	if (dev == NULL)
+		return (NULL);
+
+	pthread_mutex_lock(&dev->lock);
+	dev->refcnt++;
+	pthread_mutex_unlock(&dev->lock);
+
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_ref_device leave");
+	return (dev);
+}
+
+void
+libusb_unref_device(libusb_device * dev)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unref_device enter");
+
+	if (dev == NULL)
+		return;
+
+	pthread_mutex_lock(&dev->lock);
+	dev->refcnt--;
+	pthread_mutex_unlock(&dev->lock);
+
+	if (dev->refcnt == 0) {
+		pthread_mutex_lock(&dev->ctx->usb_devs_lock);
+		LIST_DEL(&dev->list);
+		pthread_mutex_unlock(&dev->ctx->usb_devs_lock);
+
+		libusb20_dev_free(dev->os_priv);
+		free(dev);
+	}
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_unref_device leave");
+}
+
+int
+libusb_open(libusb_device * dev, libusb_device_handle **devh)
+{
+	libusb_context *ctx = dev->ctx;
+	struct libusb20_device *pdev = dev->os_priv;
+	libusb_device_handle *hdl;
+	unsigned char dummy;
+	int err;
+
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open enter");
+
+	dummy = 1;
+	if (devh == NULL)
+		return (LIBUSB_ERROR_INVALID_PARAM);
+
+	hdl = malloc(sizeof(*hdl));
+	if (hdl == NULL)
+		return (LIBUSB_ERROR_NO_MEM);
+
+	err = libusb20_dev_open(pdev, 16 * 4 /* 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;
+	err = usb_add_pollfd(ctx, libusb20_dev_get_fd(pdev), POLLIN |
+	    POLLOUT | POLLRDNORM | POLLWRNORM);
+	if (err < 0) {
+		libusb_unref_device(dev);
+		free(hdl);
+		return (err);
+	}
+
+	pthread_mutex_lock(&ctx->open_devs_lock);
+	LIST_ADD(&hdl->list, &ctx->open_devs);
+	pthread_mutex_unlock(&ctx->open_devs_lock);
+
+	*devh = hdl;
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ctx->pollfd_modify++;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);	
+
+	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
+	if (err <= 0) {
+		pthread_mutex_lock(&ctx->pollfd_modify_lock);
+		ctx->pollfd_modify--;
+		pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+		return 0;
+	}
+
+	libusb_lock_events(ctx);
+	read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy));
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ctx->pollfd_modify--;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+	libusb_unlock_events(ctx);
+
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open leave");
+	return (0);
+}
+
+libusb_device_handle *
+libusb_open_device_with_vid_pid(libusb_context * ctx, uint16_t vendor_id,
+    uint16_t product_id)
+{
+	struct libusb_device **devs;
+	struct libusb_device_handle *devh;
+	struct libusb20_device *pdev;
+	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
+	int i, j;
+
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
+
+	devh = NULL;
+
+	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
+		return (NULL);
+
+	for (j = 0; j < i; j++) {
+		pdev = (struct libusb20_device *)devs[j]->os_priv;
+		pdesc = libusb20_dev_get_device_desc(pdev);
+		if (pdesc->idVendor == vendor_id &&
+		    pdesc->idProduct == product_id)
+			if (libusb_open(devs[j], &devh) < 0)
+				devh = NULL;
+	}
+
+	libusb_free_device_list(devs, 1);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
+	return (devh);
+}
+
+void
+libusb_close(libusb_device_handle * devh)
+{
+	libusb_context *ctx;
+	struct libusb20_device *pdev;
+	unsigned char dummy = 1;
+	int err;
+
+	if (devh == NULL)
+		return ;
+
+	ctx = devh->dev->ctx;
+	pdev = devh->os_priv;
+
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close enter");
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ctx->pollfd_modify++;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+
+	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
+	
+	if (err <= 0) {
+		pthread_mutex_lock(&ctx->open_devs_lock);
+		LIST_DEL(&devh->list);
+		pthread_mutex_unlock(&ctx->open_devs_lock);
+
+		usb_remove_pollfd(ctx, libusb20_dev_get_fd(pdev));
+		libusb_unref_device(devh->dev);
+		libusb20_dev_close(pdev);
+		free(devh);
+
+		pthread_mutex_lock(&ctx->pollfd_modify_lock);
+		ctx->pollfd_modify--;
+		pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+		return ;
+	}
+	libusb_lock_events(ctx);
+
+	read(ctx->ctrl_pipe[0], &dummy, sizeof(dummy));
+	pthread_mutex_lock(&ctx->open_devs_lock);
+	LIST_DEL(&devh->list);
+	pthread_mutex_unlock(&ctx->open_devs_lock);
+
+	usb_remove_pollfd(ctx, libusb20_dev_get_fd(pdev));
+	libusb_unref_device(devh->dev);
+	libusb20_dev_close(pdev);
+	free(devh);
+
+	pthread_mutex_lock(&ctx->pollfd_modify_lock);
+	ctx->pollfd_modify--;
+	pthread_mutex_unlock(&ctx->pollfd_modify_lock);
+
+	libusb_unlock_events(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close leave");
+}
+
+libusb_device *
+libusb_get_device(libusb_device_handle * devh)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device enter");
+
+	if (devh == NULL)
+		return (NULL);
+
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_device leave");
+	return (devh->dev);
+}
+
+int
+libusb_get_configuration(libusb_device_handle * devh, int *config)
+{
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_configuration enter");
+
+	if (devh == NULL || config == NULL)
+		return (LIBUSB_ERROR_INVALID_PARAM);
+
+	*config = libusb20_dev_get_config_index((struct libusb20_device *)
+	    devh->dev->os_priv);
+
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_get_configuration leave");
+	return (0);
+}
+
+/*
+ *	XXX this code is wrong. need update.
+ */
+
+int
+libusb_set_configuration(libusb_device_handle * devh, int configuration)
+{
+	struct libusb20_device *pdev;
+	libusb_context *ctx;
+
+	ctx = NULL;
+	GET_CONTEXT(ctx);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_configuration enter");
+
+	if (devh == NULL)
+		return (LIBUSB_ERROR_INVALID_PARAM);
+
+	pdev = (struct libusb20_device *)devh->dev->os_priv;
+
+	libusb20_dev_set_alt_index(pdev, libusb20_dev_get_config_index(pdev),
+	    configuration);
+	dprintf(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_set_configuration leave");

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***


More information about the svn-src-all mailing list