2009-06-23 01:04:58 +00:00
|
|
|
/* $FreeBSD$ */
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
|
2009-07-30 00:11:41 +00:00
|
|
|
* Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
|
2009-06-23 01:04:58 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2010-02-13 09:45:50 +00:00
|
|
|
#include <sys/fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
2010-02-11 08:30:43 +00:00
|
|
|
#include <assert.h>
|
2010-02-13 09:45:50 +00:00
|
|
|
#include <errno.h>
|
2009-06-23 01:04:58 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <pthread.h>
|
2010-02-13 09:45:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2010-05-13 00:25:30 +00:00
|
|
|
#define libusb_device_handle libusb20_device
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
#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;
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
/* Prototypes */
|
|
|
|
|
|
|
|
static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
|
|
|
|
static int libusb10_get_maxframe(struct libusb20_device *, libusb_transfer *);
|
|
|
|
static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
|
|
|
|
static int libusb10_convert_error(uint8_t status);
|
|
|
|
static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
|
|
|
|
static void libusb10_isoc_proxy(struct libusb20_transfer *);
|
|
|
|
static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
|
|
|
|
static void libusb10_ctrl_proxy(struct libusb20_transfer *);
|
|
|
|
static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
/* Library initialisation / deinitialisation */
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_set_debug(libusb_context *ctx, int level)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (ctx)
|
|
|
|
ctx->debug = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_init(libusb_context **context)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
struct libusb_context *ctx;
|
2009-07-30 00:11:41 +00:00
|
|
|
char *debug;
|
2010-02-11 08:30:43 +00:00
|
|
|
int flag;
|
2009-06-23 01:04:58 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
TAILQ_INIT(&ctx->pollfds);
|
|
|
|
TAILQ_INIT(&ctx->tr_done);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
pthread_mutex_init(&ctx->ctx_lock, NULL);
|
|
|
|
pthread_cond_init(&ctx->ctx_cond, NULL);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx->ctx_handler = NO_THREAD;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
ret = pipe(ctx->ctrl_pipe);
|
|
|
|
if (ret < 0) {
|
2009-07-30 00:11:41 +00:00
|
|
|
pthread_mutex_destroy(&ctx->ctx_lock);
|
|
|
|
pthread_cond_destroy(&ctx->ctx_cond);
|
2009-06-23 01:04:58 +00:00
|
|
|
free(ctx);
|
|
|
|
return (LIBUSB_ERROR_OTHER);
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
/* set non-blocking mode on the control pipe to avoid deadlock */
|
2010-02-11 08:30:43 +00:00
|
|
|
flag = 1;
|
|
|
|
ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag);
|
|
|
|
assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]");
|
|
|
|
flag = 1;
|
|
|
|
ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag);
|
|
|
|
assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_exit(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* XXX cleanup devices */
|
|
|
|
|
|
|
|
libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
|
2009-06-23 01:04:58 +00:00
|
|
|
close(ctx->ctrl_pipe[0]);
|
|
|
|
close(ctx->ctrl_pipe[1]);
|
2009-07-30 00:11:41 +00:00
|
|
|
pthread_mutex_destroy(&ctx->ctx_lock);
|
|
|
|
pthread_cond_destroy(&ctx->ctx_cond);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
pthread_mutex_lock(&default_context_lock);
|
|
|
|
if (ctx == usbi_default_context) {
|
|
|
|
usbi_default_context = NULL;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&default_context_lock);
|
|
|
|
|
|
|
|
free(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Device handling and initialisation. */
|
|
|
|
|
|
|
|
ssize_t
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb20_backend *usb_backend;
|
2009-06-23 01:04:58 +00:00
|
|
|
struct libusb20_device *pdev;
|
|
|
|
struct libusb_device *dev;
|
|
|
|
int i;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
usb_backend = libusb20_be_alloc_default();
|
|
|
|
if (usb_backend == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* figure out how many USB devices are present */
|
2009-06-23 01:04:58 +00:00
|
|
|
pdev = NULL;
|
|
|
|
i = 0;
|
|
|
|
while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
|
|
|
|
i++;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* allocate device pointer list */
|
2009-06-23 01:04:58 +00:00
|
|
|
*list = malloc((i + 1) * sizeof(void *));
|
|
|
|
if (*list == NULL) {
|
|
|
|
libusb20_be_free(usb_backend);
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
/* create libusb v1.0 compliant devices */
|
2009-06-23 01:04:58 +00:00
|
|
|
i = 0;
|
|
|
|
while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
|
|
|
|
|
|
|
|
dev = malloc(sizeof(*dev));
|
|
|
|
if (dev == NULL) {
|
2009-07-10 14:15:53 +00:00
|
|
|
while (i != 0) {
|
|
|
|
libusb_unref_device((*list)[i - 1]);
|
|
|
|
i--;
|
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
free(*list);
|
2009-07-30 00:11:41 +00:00
|
|
|
*list = NULL;
|
2009-06-23 01:04:58 +00:00
|
|
|
libusb20_be_free(usb_backend);
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
}
|
2009-11-08 20:03:52 +00:00
|
|
|
|
|
|
|
/* get device into libUSB v1.0 list */
|
|
|
|
libusb20_be_dequeue_device(usb_backend, pdev);
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
memset(dev, 0, sizeof(*dev));
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* init transfer queues */
|
|
|
|
TAILQ_INIT(&dev->tr_head);
|
|
|
|
|
|
|
|
/* set context we belong to */
|
2009-06-23 01:04:58 +00:00
|
|
|
dev->ctx = ctx;
|
|
|
|
|
|
|
|
/* link together the two structures */
|
|
|
|
dev->os_priv = pdev;
|
2009-07-30 00:11:41 +00:00
|
|
|
pdev->privLuData = dev;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
(*list)[i] = libusb_ref_device(dev);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
(*list)[i] = NULL;
|
|
|
|
|
|
|
|
libusb20_be_free(usb_backend);
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_free_device_list(libusb_device **list, int unref_devices)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (list == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return; /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
if (unref_devices) {
|
|
|
|
for (i = 0; list[i] != NULL; i++)
|
|
|
|
libusb_unref_device(list[i]);
|
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_bus_number(libusb_device *dev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
if (dev == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return (0); /* should not happen */
|
|
|
|
return (libusb20_dev_get_bus_number(dev->os_priv));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_device_address(libusb_device *dev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
if (dev == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return (0); /* should not happen */
|
|
|
|
return (libusb20_dev_get_address(dev->os_priv));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
struct libusb_config_descriptor *pdconf;
|
|
|
|
struct libusb_interface *pinf;
|
|
|
|
struct libusb_interface_descriptor *pdinf;
|
|
|
|
struct libusb_endpoint_descriptor *pdend;
|
2009-07-30 00:11:41 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
int ret;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB_ERROR_NO_DEVICE);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = libusb_get_active_config_descriptor(dev, &pdconf);
|
|
|
|
if (ret < 0)
|
|
|
|
return (ret);
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
ret = LIBUSB_ERROR_NOT_FOUND;
|
2009-07-30 00:11:41 +00:00
|
|
|
for (i = 0; i < pdconf->bNumInterfaces; i++) {
|
2009-06-23 01:04:58 +00:00
|
|
|
pinf = &pdconf->interface[i];
|
2009-07-30 00:11:41 +00:00
|
|
|
for (j = 0; j < pinf->num_altsetting; j++) {
|
2009-06-23 01:04:58 +00:00
|
|
|
pdinf = &pinf->altsetting[j];
|
2009-07-30 00:11:41 +00:00
|
|
|
for (k = 0; k < pdinf->bNumEndpoints; k++) {
|
2009-06-23 01:04:58 +00:00
|
|
|
pdend = &pdinf->endpoint[k];
|
|
|
|
if (pdend->bEndpointAddress == endpoint) {
|
|
|
|
ret = pdend->wMaxPacketSize;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
libusb_free_config_descriptor(pdconf);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_device *
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_ref_device(libusb_device *dev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
if (dev == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return (NULL); /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
dev->refcnt++;
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_UNLOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
return (dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unref_device(libusb_device *dev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
if (dev == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return; /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
dev->refcnt--;
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_UNLOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
if (dev->refcnt == 0) {
|
|
|
|
libusb20_dev_free(dev->os_priv);
|
|
|
|
free(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_open(libusb_device *dev, libusb_device_handle **devh)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
libusb_context *ctx = dev->ctx;
|
|
|
|
struct libusb20_device *pdev = dev->os_priv;
|
2009-07-30 00:11:41 +00:00
|
|
|
uint8_t dummy;
|
2009-06-23 01:04:58 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (devh == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* set default device handle value */
|
|
|
|
*devh = NULL;
|
|
|
|
|
|
|
|
dev = libusb_ref_device(dev);
|
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
|
|
|
|
if (err) {
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unref_device(dev);
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
|
2009-06-23 01:04:58 +00:00
|
|
|
POLLOUT | POLLRDNORM | POLLWRNORM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* make sure our event loop detects the new device */
|
|
|
|
dummy = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err < sizeof(dummy)) {
|
|
|
|
/* ignore error, if any */
|
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
*devh = pdev;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
libusb_device_handle *
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
|
2009-06-23 01:04:58 +00:00
|
|
|
uint16_t product_id)
|
|
|
|
{
|
|
|
|
struct libusb_device **devs;
|
|
|
|
struct libusb20_device *pdev;
|
|
|
|
struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
|
2009-07-30 00:11:41 +00:00
|
|
|
int i;
|
|
|
|
int j;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
if (ctx == NULL)
|
|
|
|
return (NULL); /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
if ((i = libusb_get_device_list(ctx, &devs)) < 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
for (j = 0; j < i; j++) {
|
2009-07-30 00:11:41 +00:00
|
|
|
pdev = devs[j]->os_priv;
|
2009-06-23 01:04:58 +00:00
|
|
|
pdesc = libusb20_dev_get_device_desc(pdev);
|
2009-07-30 00:11:41 +00:00
|
|
|
/*
|
|
|
|
* NOTE: The USB library will automatically swap the
|
|
|
|
* fields in the device descriptor to be of host
|
|
|
|
* endian type!
|
|
|
|
*/
|
2009-06-23 01:04:58 +00:00
|
|
|
if (pdesc->idVendor == vendor_id &&
|
2009-07-10 14:15:53 +00:00
|
|
|
pdesc->idProduct == product_id) {
|
2009-07-30 00:11:41 +00:00
|
|
|
if (libusb_open(devs[j], &pdev) < 0)
|
|
|
|
pdev = NULL;
|
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-12-12 00:24:30 +00:00
|
|
|
if (j == i)
|
|
|
|
pdev = NULL;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
libusb_free_device_list(devs, 1);
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
|
2009-07-30 00:11:41 +00:00
|
|
|
return (pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_close(struct libusb20_device *pdev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
libusb_context *ctx;
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_device *dev;
|
|
|
|
uint8_t dummy;
|
2009-06-23 01:04:58 +00:00
|
|
|
int err;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL)
|
|
|
|
return; /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
|
|
|
ctx = dev->ctx;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_remove_pollfd(ctx, &dev->dev_poll);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb20_dev_close(pdev);
|
2009-11-08 20:03:52 +00:00
|
|
|
|
|
|
|
/* unref will free the "pdev" when the refcount reaches zero */
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unref_device(dev);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* make sure our event loop detects the closed device */
|
|
|
|
dummy = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err < sizeof(dummy)) {
|
|
|
|
/* ignore error, if any */
|
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
libusb_device *
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_device(struct libusb20_device *pdev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (NULL);
|
2009-07-30 00:11:41 +00:00
|
|
|
return ((libusb_device *)pdev->privLuData);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_configuration(struct libusb20_device *pdev, int *config)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb20_config *pconf;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL || config == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
|
|
|
|
if (pconf == NULL)
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
|
|
|
|
*config = pconf->desc.bConfigurationValue;
|
|
|
|
|
|
|
|
free(pconf);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_set_configuration(struct libusb20_device *pdev, int configuration)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb20_config *pconf;
|
|
|
|
struct libusb_device *dev;
|
|
|
|
int err;
|
|
|
|
uint8_t i;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (dev == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (configuration < 1) {
|
|
|
|
/* unconfigure */
|
|
|
|
i = 255;
|
|
|
|
} else {
|
|
|
|
for (i = 0; i != 255; i++) {
|
|
|
|
uint8_t found;
|
|
|
|
|
|
|
|
pconf = libusb20_dev_alloc_config(pdev, i);
|
|
|
|
if (pconf == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
found = (pconf->desc.bConfigurationValue
|
|
|
|
== configuration);
|
|
|
|
free(pconf);
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
goto set_config;
|
|
|
|
}
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
set_config:
|
|
|
|
|
|
|
|
libusb10_cancel_all_transfer(dev);
|
|
|
|
|
|
|
|
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
|
|
|
|
|
|
|
|
err = libusb20_dev_set_config_index(pdev, i);
|
|
|
|
|
|
|
|
libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
|
|
|
|
POLLOUT | POLLRDNORM | POLLWRNORM);
|
|
|
|
|
|
|
|
return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_device *dev;
|
|
|
|
int err = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (interface_number < 0 || interface_number > 31)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (dev->claimed_interfaces & (1 << interface_number))
|
2009-07-30 00:11:41 +00:00
|
|
|
err = LIBUSB_ERROR_BUSY;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (!err)
|
2009-06-23 01:04:58 +00:00
|
|
|
dev->claimed_interfaces |= (1 << interface_number);
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_release_interface(struct libusb20_device *pdev, int interface_number)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_device *dev;
|
|
|
|
int err = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (interface_number < 0 || interface_number > 31)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (!(dev->claimed_interfaces & (1 << interface_number)))
|
2009-07-30 00:11:41 +00:00
|
|
|
err = LIBUSB_ERROR_NOT_FOUND;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (!err)
|
2009-06-23 01:04:58 +00:00
|
|
|
dev->claimed_interfaces &= ~(1 << interface_number);
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_set_interface_alt_setting(struct libusb20_device *pdev,
|
2009-06-23 01:04:58 +00:00
|
|
|
int interface_number, int alternate_setting)
|
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_device *dev;
|
|
|
|
int err = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (interface_number < 0 || interface_number > 31)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
|
|
|
if (!(dev->claimed_interfaces & (1 << interface_number)))
|
|
|
|
err = LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
CTX_UNLOCK(dev->ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err)
|
|
|
|
return (err);
|
|
|
|
|
|
|
|
libusb10_cancel_all_transfer(dev);
|
|
|
|
|
|
|
|
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
|
|
|
|
|
|
|
|
err = libusb20_dev_set_alt_index(pdev,
|
|
|
|
interface_number, alternate_setting);
|
|
|
|
|
|
|
|
libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
|
|
|
|
pdev, libusb20_dev_get_fd(pdev),
|
|
|
|
POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
|
|
|
|
|
|
|
|
return (err ? LIBUSB_ERROR_OTHER : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct libusb20_transfer *
|
|
|
|
libusb10_get_transfer(struct libusb20_device *pdev,
|
|
|
|
uint8_t endpoint, uint8_t index)
|
|
|
|
{
|
|
|
|
index &= 1; /* double buffering */
|
|
|
|
|
|
|
|
index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
|
|
|
|
|
|
|
|
if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
|
|
|
|
/* this is an IN endpoint */
|
|
|
|
index |= 2;
|
|
|
|
}
|
|
|
|
return (libusb20_tr_get_pointer(pdev, index));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
struct libusb20_transfer *xfer;
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_device *dev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
xfer = libusb10_get_transfer(pdev, endpoint, 0);
|
2009-07-10 14:15:53 +00:00
|
|
|
if (xfer == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
|
|
|
|
|
|
|
CTX_LOCK(dev->ctx);
|
|
|
|
err = libusb20_tr_open(xfer, 0, 0, endpoint);
|
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
|
|
|
|
if (err != 0 && err != LIBUSB20_ERROR_BUSY)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_OTHER);
|
|
|
|
|
|
|
|
libusb20_tr_clear_stall_sync(xfer);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* check if we opened the transfer */
|
|
|
|
if (err == 0) {
|
|
|
|
CTX_LOCK(dev->ctx);
|
|
|
|
libusb20_tr_close(xfer);
|
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
}
|
|
|
|
return (0); /* success */
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_reset_device(struct libusb20_device *pdev)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_device *dev;
|
|
|
|
int err;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(pdev);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (LIBUSB20_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_cancel_all_transfer(dev);
|
|
|
|
|
|
|
|
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
|
|
|
|
|
|
|
|
err = libusb20_dev_reset(pdev);
|
|
|
|
|
|
|
|
libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
|
|
|
|
pdev, libusb20_dev_get_fd(pdev),
|
|
|
|
POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
|
|
|
|
|
|
|
|
return (err ? LIBUSB_ERROR_OTHER : 0);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
return (libusb20_dev_kernel_driver_active(
|
|
|
|
pdev, interface));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
int err;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = libusb20_dev_detach_kernel_driver(
|
|
|
|
pdev, interface);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
return (err ? LIBUSB20_ERROR_OTHER : 0);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pdev == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-07-30 00:11:41 +00:00
|
|
|
/* stub - currently not supported by libusb20 */
|
2009-06-23 01:04:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Asynchronous device I/O */
|
|
|
|
|
|
|
|
struct libusb_transfer *
|
|
|
|
libusb_alloc_transfer(int iso_packets)
|
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
struct libusb_super_transfer *sxfer;
|
2009-06-23 01:04:58 +00:00
|
|
|
int len;
|
|
|
|
|
|
|
|
len = sizeof(struct libusb_transfer) +
|
2009-07-30 00:11:41 +00:00
|
|
|
sizeof(struct libusb_super_transfer) +
|
2009-06-23 01:04:58 +00:00
|
|
|
(iso_packets * sizeof(libusb_iso_packet_descriptor));
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
sxfer = malloc(len);
|
|
|
|
if (sxfer == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (NULL);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
memset(sxfer, 0, len);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* set default value */
|
|
|
|
uxfer->num_iso_packets = iso_packets;
|
|
|
|
|
|
|
|
return (uxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_free_transfer(struct libusb_transfer *uxfer)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_super_transfer *sxfer;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (uxfer == NULL)
|
|
|
|
return; /* be NULL safe */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
sxfer = (struct libusb_super_transfer *)(
|
|
|
|
(uint8_t *)uxfer - sizeof(*sxfer));
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
free(sxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
static int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
|
2009-07-10 14:15:53 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int usb_speed;
|
|
|
|
|
|
|
|
usb_speed = libusb20_dev_get_speed(pdev);
|
|
|
|
|
|
|
|
switch (xfer->type) {
|
|
|
|
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
|
|
|
switch (usb_speed) {
|
|
|
|
case LIBUSB20_SPEED_LOW:
|
|
|
|
case LIBUSB20_SPEED_FULL:
|
|
|
|
ret = 60 * 1;
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-10 14:15:53 +00:00
|
|
|
ret = 60 * 8;
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
|
|
|
ret = 2;
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
default:
|
|
|
|
ret = 1;
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
return (ret);
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
|
2009-07-10 14:15:53 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int usb_speed;
|
|
|
|
|
|
|
|
usb_speed = libusb20_dev_get_speed(pdev);
|
|
|
|
|
|
|
|
switch (xfer->type) {
|
|
|
|
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = 0; /* kernel will auto-select */
|
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = 1024;
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-10 14:15:53 +00:00
|
|
|
switch (usb_speed) {
|
2009-07-30 00:11:41 +00:00
|
|
|
case LIBUSB20_SPEED_LOW:
|
|
|
|
ret = 256;
|
|
|
|
break;
|
|
|
|
case LIBUSB20_SPEED_FULL:
|
|
|
|
ret = 4096;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = 16384;
|
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
libusb10_convert_error(uint8_t status)
|
|
|
|
{
|
|
|
|
; /* indent fix */
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case LIBUSB20_TRANSFER_START:
|
|
|
|
case LIBUSB20_TRANSFER_COMPLETED:
|
|
|
|
return (LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
case LIBUSB20_TRANSFER_OVERFLOW:
|
|
|
|
return (LIBUSB_TRANSFER_OVERFLOW);
|
|
|
|
case LIBUSB20_TRANSFER_NO_DEVICE:
|
|
|
|
return (LIBUSB_TRANSFER_NO_DEVICE);
|
|
|
|
case LIBUSB20_TRANSFER_STALL:
|
|
|
|
return (LIBUSB_TRANSFER_STALL);
|
|
|
|
case LIBUSB20_TRANSFER_CANCELLED:
|
|
|
|
return (LIBUSB_TRANSFER_CANCELLED);
|
|
|
|
case LIBUSB20_TRANSFER_TIMED_OUT:
|
|
|
|
return (LIBUSB_TRANSFER_TIMED_OUT);
|
|
|
|
default:
|
|
|
|
return (LIBUSB_TRANSFER_ERROR);
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* This function must be called locked */
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
static void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_complete_transfer(struct libusb20_transfer *pxfer,
|
|
|
|
struct libusb_super_transfer *sxfer, int status)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
struct libusb_device *dev;
|
|
|
|
|
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
|
|
|
|
|
|
|
if (pxfer != NULL)
|
|
|
|
libusb20_tr_set_priv_sc1(pxfer, NULL);
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
/* set transfer status */
|
2009-07-30 00:11:41 +00:00
|
|
|
uxfer->status = status;
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
/* update super transfer state */
|
|
|
|
sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
dev = libusb_get_device(uxfer->dev_handle);
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function must be called locked */
|
|
|
|
|
|
|
|
static void
|
|
|
|
libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
|
|
|
|
{
|
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
uint32_t actlen;
|
|
|
|
uint16_t iso_packets;
|
|
|
|
uint16_t i;
|
2009-06-23 01:04:58 +00:00
|
|
|
uint8_t status;
|
2009-07-30 00:11:41 +00:00
|
|
|
uint8_t flags;
|
|
|
|
|
|
|
|
status = libusb20_tr_get_status(pxfer);
|
|
|
|
sxfer = libusb20_tr_get_priv_sc1(pxfer);
|
|
|
|
actlen = libusb20_tr_get_actual_length(pxfer);
|
|
|
|
iso_packets = libusb20_tr_get_max_frames(pxfer);
|
|
|
|
|
|
|
|
if (sxfer == NULL)
|
|
|
|
return; /* cancelled - nothing to do */
|
|
|
|
|
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (iso_packets > uxfer->num_iso_packets)
|
|
|
|
iso_packets = uxfer->num_iso_packets;
|
|
|
|
|
|
|
|
if (iso_packets == 0)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
/* make sure that the number of ISOCHRONOUS packets is valid */
|
|
|
|
uxfer->num_iso_packets = iso_packets;
|
|
|
|
|
|
|
|
flags = uxfer->flags;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case LIBUSB20_TRANSFER_COMPLETED:
|
2009-07-10 14:15:53 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* update actual length */
|
|
|
|
uxfer->actual_length = actlen;
|
|
|
|
for (i = 0; i != iso_packets; i++) {
|
|
|
|
uxfer->iso_packet_desc[i].actual_length =
|
|
|
|
libusb20_tr_get_length(pxfer, i);
|
|
|
|
}
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
case LIBUSB20_TRANSFER_START:
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
/* setup length(s) */
|
|
|
|
actlen = 0;
|
|
|
|
for (i = 0; i != iso_packets; i++) {
|
|
|
|
libusb20_tr_setup_isoc(pxfer,
|
|
|
|
&uxfer->buffer[actlen],
|
|
|
|
uxfer->iso_packet_desc[i].length, i);
|
|
|
|
actlen += uxfer->iso_packet_desc[i].length;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
/* no remainder */
|
|
|
|
sxfer->rem_len = 0;
|
|
|
|
|
|
|
|
libusb20_tr_set_total_frames(pxfer, iso_packets);
|
|
|
|
libusb20_tr_submit(pxfer);
|
|
|
|
|
|
|
|
/* fork another USB transfer, if any */
|
|
|
|
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
|
|
|
|
break;
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
default:
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function must be called locked */
|
|
|
|
|
|
|
|
static void
|
|
|
|
libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
|
|
|
|
{
|
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
uint32_t max_bulk;
|
|
|
|
uint32_t actlen;
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t flags;
|
|
|
|
|
|
|
|
status = libusb20_tr_get_status(pxfer);
|
|
|
|
sxfer = libusb20_tr_get_priv_sc1(pxfer);
|
|
|
|
max_bulk = libusb20_tr_get_max_total_length(pxfer);
|
|
|
|
actlen = libusb20_tr_get_actual_length(pxfer);
|
|
|
|
|
|
|
|
if (sxfer == NULL)
|
|
|
|
return; /* cancelled - nothing to do */
|
|
|
|
|
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
|
|
|
|
|
|
|
flags = uxfer->flags;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case LIBUSB20_TRANSFER_COMPLETED:
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
uxfer->actual_length += actlen;
|
|
|
|
|
|
|
|
/* check for short packet */
|
|
|
|
if (sxfer->last_len != actlen) {
|
|
|
|
if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
|
|
|
|
} else {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* check for end of data */
|
|
|
|
if (sxfer->rem_len == 0) {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case LIBUSB20_TRANSFER_START:
|
|
|
|
if (max_bulk > sxfer->rem_len) {
|
|
|
|
max_bulk = sxfer->rem_len;
|
|
|
|
}
|
|
|
|
/* setup new BULK or INTERRUPT transaction */
|
|
|
|
libusb20_tr_setup_bulk(pxfer,
|
|
|
|
sxfer->curr_data, max_bulk, uxfer->timeout);
|
|
|
|
|
|
|
|
/* update counters */
|
|
|
|
sxfer->last_len = max_bulk;
|
|
|
|
sxfer->curr_data += max_bulk;
|
|
|
|
sxfer->rem_len -= max_bulk;
|
|
|
|
|
|
|
|
libusb20_tr_submit(pxfer);
|
|
|
|
|
|
|
|
/* check if we can fork another USB transfer */
|
|
|
|
if (sxfer->rem_len == 0)
|
|
|
|
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* This function must be called locked */
|
|
|
|
|
|
|
|
static void
|
|
|
|
libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
uint32_t max_bulk;
|
|
|
|
uint32_t actlen;
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t flags;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
status = libusb20_tr_get_status(pxfer);
|
|
|
|
sxfer = libusb20_tr_get_priv_sc1(pxfer);
|
|
|
|
max_bulk = libusb20_tr_get_max_total_length(pxfer);
|
|
|
|
actlen = libusb20_tr_get_actual_length(pxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (sxfer == NULL)
|
|
|
|
return; /* cancelled - nothing to do */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
|
|
|
|
|
|
|
flags = uxfer->flags;
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case LIBUSB20_TRANSFER_COMPLETED:
|
|
|
|
|
|
|
|
uxfer->actual_length += actlen;
|
|
|
|
|
|
|
|
/* subtract length of SETUP packet, if any */
|
|
|
|
actlen -= libusb20_tr_get_length(pxfer, 0);
|
|
|
|
|
|
|
|
/* check for short packet */
|
|
|
|
if (sxfer->last_len != actlen) {
|
|
|
|
if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
|
|
|
|
} else {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
}
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
/* check for end of data */
|
|
|
|
if (sxfer->rem_len == 0) {
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
/* FALLTHROUGH */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
case LIBUSB20_TRANSFER_START:
|
|
|
|
if (max_bulk > sxfer->rem_len) {
|
|
|
|
max_bulk = sxfer->rem_len;
|
|
|
|
}
|
|
|
|
/* setup new CONTROL transaction */
|
|
|
|
if (status == LIBUSB20_TRANSFER_COMPLETED) {
|
|
|
|
/* next fragment - don't send SETUP packet */
|
|
|
|
libusb20_tr_set_length(pxfer, 0, 0);
|
|
|
|
} else {
|
|
|
|
/* first fragment - send SETUP packet */
|
|
|
|
libusb20_tr_set_length(pxfer, 8, 0);
|
|
|
|
libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
|
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (max_bulk != 0) {
|
|
|
|
libusb20_tr_set_length(pxfer, max_bulk, 1);
|
|
|
|
libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
|
|
|
|
libusb20_tr_set_total_frames(pxfer, 2);
|
|
|
|
} else {
|
|
|
|
libusb20_tr_set_total_frames(pxfer, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update counters */
|
|
|
|
sxfer->last_len = max_bulk;
|
|
|
|
sxfer->curr_data += max_bulk;
|
|
|
|
sxfer->rem_len -= max_bulk;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb20_tr_submit(pxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* check if we can fork another USB transfer */
|
|
|
|
if (sxfer->rem_len == 0)
|
|
|
|
libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
default:
|
|
|
|
libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
|
|
|
|
break;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The following function must be called locked */
|
|
|
|
|
|
|
|
static void
|
|
|
|
libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
|
|
|
|
{
|
|
|
|
struct libusb20_transfer *pxfer0;
|
|
|
|
struct libusb20_transfer *pxfer1;
|
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_transfer *uxfer;
|
|
|
|
struct libusb_device *dev;
|
|
|
|
int err;
|
|
|
|
int buffsize;
|
|
|
|
int maxframe;
|
|
|
|
int temp;
|
|
|
|
uint8_t dummy;
|
|
|
|
|
|
|
|
dev = libusb_get_device(pdev);
|
|
|
|
|
|
|
|
pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
|
|
|
|
pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
|
|
|
|
|
|
|
|
if (pxfer0 == NULL || pxfer1 == NULL)
|
|
|
|
return; /* shouldn't happen */
|
|
|
|
|
|
|
|
temp = 0;
|
|
|
|
if (libusb20_tr_pending(pxfer0))
|
|
|
|
temp |= 1;
|
|
|
|
if (libusb20_tr_pending(pxfer1))
|
|
|
|
temp |= 2;
|
|
|
|
|
|
|
|
switch (temp) {
|
|
|
|
case 3:
|
|
|
|
/* wait till one of the transfers complete */
|
|
|
|
return;
|
|
|
|
case 2:
|
|
|
|
sxfer = libusb20_tr_get_priv_sc1(pxfer1);
|
2009-11-20 08:57:25 +00:00
|
|
|
if (sxfer == NULL)
|
|
|
|
return; /* cancelling */
|
2009-07-30 00:11:41 +00:00
|
|
|
if (sxfer->rem_len)
|
|
|
|
return; /* cannot queue another one */
|
|
|
|
/* swap transfers */
|
|
|
|
pxfer1 = pxfer0;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sxfer = libusb20_tr_get_priv_sc1(pxfer0);
|
2009-11-20 08:57:25 +00:00
|
|
|
if (sxfer == NULL)
|
|
|
|
return; /* cancelling */
|
2009-07-30 00:11:41 +00:00
|
|
|
if (sxfer->rem_len)
|
|
|
|
return; /* cannot queue another one */
|
|
|
|
/* swap transfers */
|
|
|
|
pxfer0 = pxfer1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find next transfer on same endpoint */
|
|
|
|
TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
|
|
|
|
|
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
|
|
|
|
|
|
|
if (uxfer->endpoint == endpoint) {
|
|
|
|
TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
|
|
|
|
sxfer->entry.tqe_prev = NULL;
|
|
|
|
goto found;
|
2009-07-10 14:15:53 +00:00
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
return; /* success */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
found:
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb20_tr_set_priv_sc0(pxfer0, pdev);
|
|
|
|
libusb20_tr_set_priv_sc1(pxfer0, sxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* reset super transfer state */
|
|
|
|
sxfer->rem_len = uxfer->length;
|
|
|
|
sxfer->curr_data = uxfer->buffer;
|
|
|
|
uxfer->actual_length = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
switch (uxfer->type) {
|
|
|
|
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
|
|
|
|
libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
|
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_TYPE_BULK:
|
|
|
|
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
|
|
|
|
libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
|
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_TYPE_CONTROL:
|
|
|
|
libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
|
|
|
|
if (sxfer->rem_len < 8)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/* remove SETUP packet from data */
|
|
|
|
sxfer->rem_len -= 8;
|
|
|
|
sxfer->curr_data += 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffsize = libusb10_get_buffsize(pdev, uxfer);
|
|
|
|
maxframe = libusb10_get_maxframe(pdev, uxfer);
|
|
|
|
|
|
|
|
/* make sure the transfer is opened */
|
|
|
|
err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
|
|
|
|
if (err && (err != LIBUSB20_ERROR_BUSY)) {
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
libusb20_tr_start(pxfer0);
|
|
|
|
return;
|
|
|
|
|
|
|
|
failure:
|
|
|
|
libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
|
|
|
|
|
|
|
|
/* make sure our event loop spins the done handler */
|
|
|
|
dummy = 0;
|
|
|
|
write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* The following function must be called unlocked */
|
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_submit_transfer(struct libusb_transfer *uxfer)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb20_transfer *pxfer0;
|
|
|
|
struct libusb20_transfer *pxfer1;
|
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_device *dev;
|
2009-11-08 20:03:52 +00:00
|
|
|
uint32_t endpoint;
|
2009-07-30 00:11:41 +00:00
|
|
|
int err;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (uxfer == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (uxfer->dev_handle == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
endpoint = uxfer->endpoint;
|
|
|
|
|
|
|
|
if (endpoint > 255)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
|
|
|
dev = libusb_get_device(uxfer->dev_handle);
|
|
|
|
|
|
|
|
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
|
|
|
|
|
|
|
|
sxfer = (struct libusb_super_transfer *)(
|
|
|
|
(uint8_t *)uxfer - sizeof(*sxfer));
|
|
|
|
|
|
|
|
CTX_LOCK(dev->ctx);
|
|
|
|
|
|
|
|
pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
|
|
|
|
pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
|
|
|
|
|
|
|
|
if (pxfer0 == NULL || pxfer1 == NULL) {
|
|
|
|
err = LIBUSB_ERROR_OTHER;
|
|
|
|
} else if ((sxfer->entry.tqe_prev != NULL) ||
|
2009-11-20 08:57:25 +00:00
|
|
|
(libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
|
2009-07-30 00:11:41 +00:00
|
|
|
(libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
|
|
|
|
err = LIBUSB_ERROR_BUSY;
|
|
|
|
} else {
|
2009-11-20 08:57:25 +00:00
|
|
|
|
|
|
|
/* set pending state */
|
|
|
|
sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
|
|
|
|
|
|
|
|
/* insert transfer into transfer head list */
|
2009-07-30 00:11:41 +00:00
|
|
|
TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
/* start work transfers */
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_submit_transfer_sub(
|
|
|
|
uxfer->dev_handle, endpoint);
|
|
|
|
|
|
|
|
err = 0; /* success */
|
|
|
|
}
|
|
|
|
|
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
|
|
|
|
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
|
|
|
|
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Asynchronous transfer cancel */
|
|
|
|
|
|
|
|
int
|
|
|
|
libusb_cancel_transfer(struct libusb_transfer *uxfer)
|
|
|
|
{
|
|
|
|
struct libusb20_transfer *pxfer0;
|
|
|
|
struct libusb20_transfer *pxfer1;
|
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_device *dev;
|
2009-11-08 20:03:52 +00:00
|
|
|
uint32_t endpoint;
|
2009-11-20 08:57:25 +00:00
|
|
|
int retval;
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
if (uxfer == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
/* check if not initialised */
|
2009-07-30 00:11:41 +00:00
|
|
|
if (uxfer->dev_handle == NULL)
|
2009-11-20 08:57:25 +00:00
|
|
|
return (LIBUSB_ERROR_NOT_FOUND);
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
endpoint = uxfer->endpoint;
|
|
|
|
|
|
|
|
if (endpoint > 255)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
|
|
|
dev = libusb_get_device(uxfer->dev_handle);
|
|
|
|
|
|
|
|
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
|
|
|
|
|
|
|
|
sxfer = (struct libusb_super_transfer *)(
|
|
|
|
(uint8_t *)uxfer - sizeof(*sxfer));
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
retval = 0;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(dev->ctx);
|
|
|
|
|
|
|
|
pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
|
|
|
|
pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
|
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
|
|
|
|
/* only update the transfer status */
|
|
|
|
uxfer->status = LIBUSB_TRANSFER_CANCELLED;
|
|
|
|
retval = LIBUSB_ERROR_NOT_FOUND;
|
|
|
|
} else if (sxfer->entry.tqe_prev != NULL) {
|
2009-07-30 00:11:41 +00:00
|
|
|
/* we are lucky - transfer is on a queue */
|
|
|
|
TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
|
|
|
|
sxfer->entry.tqe_prev = NULL;
|
2009-11-20 08:57:25 +00:00
|
|
|
libusb10_complete_transfer(NULL,
|
|
|
|
sxfer, LIBUSB_TRANSFER_CANCELLED);
|
2009-07-30 00:11:41 +00:00
|
|
|
} else if (pxfer0 == NULL || pxfer1 == NULL) {
|
|
|
|
/* not started */
|
2009-11-20 08:57:25 +00:00
|
|
|
retval = LIBUSB_ERROR_NOT_FOUND;
|
2009-07-30 00:11:41 +00:00
|
|
|
} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
|
2009-11-20 08:57:25 +00:00
|
|
|
libusb10_complete_transfer(pxfer0,
|
|
|
|
sxfer, LIBUSB_TRANSFER_CANCELLED);
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb20_tr_stop(pxfer0);
|
|
|
|
/* make sure the queue doesn't stall */
|
|
|
|
libusb10_submit_transfer_sub(
|
|
|
|
uxfer->dev_handle, endpoint);
|
|
|
|
} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
|
2009-11-20 08:57:25 +00:00
|
|
|
libusb10_complete_transfer(pxfer1,
|
|
|
|
sxfer, LIBUSB_TRANSFER_CANCELLED);
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb20_tr_stop(pxfer1);
|
|
|
|
/* make sure the queue doesn't stall */
|
|
|
|
libusb10_submit_transfer_sub(
|
|
|
|
uxfer->dev_handle, endpoint);
|
|
|
|
} else {
|
|
|
|
/* not started */
|
2009-11-20 08:57:25 +00:00
|
|
|
retval = LIBUSB_ERROR_NOT_FOUND;
|
2009-07-30 00:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CTX_UNLOCK(dev->ctx);
|
|
|
|
|
|
|
|
DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-11-20 08:57:25 +00:00
|
|
|
return (retval);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
UNEXPORTED void
|
|
|
|
libusb10_cancel_all_transfer(libusb_device *dev)
|
|
|
|
{
|
|
|
|
/* TODO */
|
|
|
|
}
|
2009-11-08 20:03:52 +00:00
|
|
|
|
|
|
|
uint16_t
|
|
|
|
libusb_cpu_to_le16(uint16_t x)
|
|
|
|
{
|
|
|
|
return (htole16(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
libusb_le16_to_cpu(uint16_t x)
|
|
|
|
{
|
|
|
|
return (le16toh(x));
|
|
|
|
}
|
|
|
|
|