2009-06-23 01:04:58 +00:00
|
|
|
/* $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.
|
|
|
|
*/
|
|
|
|
|
2010-02-13 09:45:50 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
|
|
|
|
#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>
|
2009-06-23 01:04:58 +00:00
|
|
|
#include <time.h>
|
2010-02-13 09:45:50 +00:00
|
|
|
#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"
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
UNEXPORTED void
|
|
|
|
libusb10_add_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd,
|
|
|
|
struct libusb20_device *pdev, int fd, short events)
|
2009-07-10 14:15:53 +00:00
|
|
|
{
|
|
|
|
if (ctx == NULL)
|
2009-07-30 00:11:41 +00:00
|
|
|
return; /* invalid */
|
|
|
|
|
|
|
|
if (pollfd->entry.tqe_prev != NULL)
|
|
|
|
return; /* already queued */
|
2009-07-10 14:15:53 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return; /* invalid */
|
|
|
|
|
|
|
|
pollfd->pdev = pdev;
|
2009-07-10 14:15:53 +00:00
|
|
|
pollfd->pollfd.fd = fd;
|
|
|
|
pollfd->pollfd.events = events;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(ctx);
|
|
|
|
TAILQ_INSERT_TAIL(&ctx->pollfds, pollfd, entry);
|
|
|
|
CTX_UNLOCK(ctx);
|
2009-07-10 14:15:53 +00:00
|
|
|
|
|
|
|
if (ctx->fd_added_cb)
|
|
|
|
ctx->fd_added_cb(fd, events, ctx->fd_cb_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNEXPORTED void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_remove_pollfd(libusb_context *ctx, struct libusb_super_pollfd *pollfd)
|
2009-07-10 14:15:53 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
if (ctx == NULL)
|
|
|
|
return; /* invalid */
|
2009-07-10 14:15:53 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (pollfd->entry.tqe_prev == NULL)
|
|
|
|
return; /* already dequeued */
|
2009-07-10 14:15:53 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(ctx);
|
|
|
|
TAILQ_REMOVE(&ctx->pollfds, pollfd, entry);
|
|
|
|
pollfd->entry.tqe_prev = NULL;
|
|
|
|
CTX_UNLOCK(ctx);
|
2009-07-10 14:15:53 +00:00
|
|
|
|
|
|
|
if (ctx->fd_removed_cb)
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx->fd_removed_cb(pollfd->pollfd.fd, ctx->fd_cb_user_data);
|
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
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
static int
|
|
|
|
libusb10_handle_events_sub(struct libusb_context *ctx, struct timeval *tv)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_device *dev;
|
|
|
|
struct libusb20_device **ppdev;
|
|
|
|
struct libusb_super_pollfd *pfd;
|
2009-06-23 01:04:58 +00:00
|
|
|
struct pollfd *fds;
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_super_transfer *sxfer;
|
|
|
|
struct libusb_transfer *uxfer;
|
2009-06-23 01:04:58 +00:00
|
|
|
nfds_t nfds;
|
|
|
|
int timeout;
|
|
|
|
int i;
|
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
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb10_handle_events_sub enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
nfds = 0;
|
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(pfd, &ctx->pollfds, entry)
|
|
|
|
nfds++;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
fds = alloca(sizeof(*fds) * nfds);
|
2009-06-23 01:04:58 +00:00
|
|
|
if (fds == NULL)
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ppdev = alloca(sizeof(*ppdev) * nfds);
|
|
|
|
if (ppdev == NULL)
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
|
|
|
|
TAILQ_FOREACH(pfd, &ctx->pollfds, entry) {
|
|
|
|
fds[i].fd = pfd->pollfd.fd;
|
|
|
|
fds[i].events = pfd->pollfd.events;
|
2009-06-23 01:04:58 +00:00
|
|
|
fds[i].revents = 0;
|
2009-07-30 00:11:41 +00:00
|
|
|
ppdev[i] = pfd->pdev;
|
|
|
|
if (pfd->pdev != NULL)
|
|
|
|
libusb_get_device(pfd->pdev)->refcnt++;
|
|
|
|
i++;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (tv == NULL)
|
|
|
|
timeout = -1;
|
|
|
|
else
|
|
|
|
timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
|
|
|
|
|
|
|
|
CTX_UNLOCK(ctx);
|
|
|
|
err = poll(fds, nfds, timeout);
|
|
|
|
CTX_LOCK(ctx);
|
|
|
|
|
|
|
|
if ((err == -1) && (errno == EINTR))
|
|
|
|
err = LIBUSB_ERROR_INTERRUPTED;
|
|
|
|
else if (err < 0)
|
|
|
|
err = LIBUSB_ERROR_IO;
|
|
|
|
|
|
|
|
if (err < 1) {
|
2010-10-14 20:18:39 +00:00
|
|
|
for (i = 0; i != (int)nfds; i++) {
|
2009-07-30 00:11:41 +00:00
|
|
|
if (ppdev[i] != NULL) {
|
|
|
|
CTX_UNLOCK(ctx);
|
|
|
|
libusb_unref_device(libusb_get_device(ppdev[i]));
|
|
|
|
CTX_LOCK(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto do_done;
|
|
|
|
}
|
2010-10-14 20:18:39 +00:00
|
|
|
for (i = 0; i != (int)nfds; i++) {
|
2009-07-30 00:11:41 +00:00
|
|
|
if (ppdev[i] != NULL) {
|
|
|
|
dev = libusb_get_device(ppdev[i]);
|
|
|
|
|
2009-11-08 20:03:52 +00:00
|
|
|
if (fds[i].revents == 0)
|
|
|
|
err = 0; /* nothing to do */
|
|
|
|
else
|
|
|
|
err = libusb20_dev_process(ppdev[i]);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err) {
|
|
|
|
/* cancel all transfers - device is gone */
|
|
|
|
libusb10_cancel_all_transfer(dev);
|
2009-11-08 20:03:52 +00:00
|
|
|
|
|
|
|
/* remove USB device from polling loop */
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
|
|
|
|
}
|
|
|
|
CTX_UNLOCK(ctx);
|
|
|
|
libusb_unref_device(dev);
|
|
|
|
CTX_LOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
} else {
|
2009-07-30 00:11:41 +00:00
|
|
|
uint8_t dummy;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (read(fds[i].fd, &dummy, 1) != 1)
|
|
|
|
break;
|
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
do_done:
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* Do all done callbacks */
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
while ((sxfer = TAILQ_FIRST(&ctx->tr_done))) {
|
2010-11-13 19:25:11 +00:00
|
|
|
uint8_t flags;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
TAILQ_REMOVE(&ctx->tr_done, sxfer, entry);
|
|
|
|
sxfer->entry.tqe_prev = NULL;
|
|
|
|
|
|
|
|
ctx->tr_done_ref++;
|
|
|
|
|
|
|
|
CTX_UNLOCK(ctx);
|
|
|
|
|
|
|
|
uxfer = (struct libusb_transfer *)(
|
|
|
|
((uint8_t *)sxfer) + sizeof(*sxfer));
|
|
|
|
|
2010-11-13 19:25:11 +00:00
|
|
|
/* Allow the callback to free the transfer itself. */
|
|
|
|
flags = uxfer->flags;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (uxfer->callback != NULL)
|
|
|
|
(uxfer->callback) (uxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2010-11-13 19:25:11 +00:00
|
|
|
/* Check if the USB transfer should be automatically freed. */
|
|
|
|
if (flags & LIBUSB_TRANSFER_FREE_TRANSFER)
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_free_transfer(uxfer);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
CTX_LOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx->tr_done_ref--;
|
|
|
|
ctx->tr_done_gen++;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/* Wakeup other waiters */
|
|
|
|
pthread_cond_broadcast(&ctx->ctx_cond);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Polling and timing */
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_try_lock_events(libusb_context *ctx)
|
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
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
if (ctx == NULL)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (1);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = CTX_TRYLOCK(ctx);
|
|
|
|
if (err)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (1);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = (ctx->ctx_handler != NO_THREAD);
|
|
|
|
if (err)
|
|
|
|
CTX_UNLOCK(ctx);
|
|
|
|
else
|
|
|
|
ctx->ctx_handler = pthread_self();
|
|
|
|
|
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_lock_events(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
CTX_LOCK(ctx);
|
|
|
|
if (ctx->ctx_handler == NO_THREAD)
|
|
|
|
ctx->ctx_handler = pthread_self();
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unlock_events(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
if (ctx->ctx_handler == pthread_self()) {
|
|
|
|
ctx->ctx_handler = NO_THREAD;
|
|
|
|
pthread_cond_broadcast(&ctx->ctx_cond);
|
|
|
|
}
|
|
|
|
CTX_UNLOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_event_handling_ok(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
return (ctx->ctx_handler == pthread_self());
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_event_handler_active(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
return (ctx->ctx_handler != NO_THREAD);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_lock_event_waiters(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
CTX_LOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unlock_event_waiters(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
CTX_UNLOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_wait_for_event(libusb_context *ctx, struct timeval *tv)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
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
|
|
|
ctx = GET_CONTEXT(ctx);
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_wait_for_event enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
if (tv == NULL) {
|
2009-07-30 00:11:41 +00:00
|
|
|
pthread_cond_wait(&ctx->ctx_cond,
|
|
|
|
&ctx->ctx_lock);
|
2009-06-23 01:04:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
err = clock_gettime(CLOCK_REALTIME, &ts);
|
|
|
|
if (err < 0)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (LIBUSB_ERROR_OTHER);
|
|
|
|
|
|
|
|
ts.tv_sec = tv->tv_sec;
|
|
|
|
ts.tv_nsec = tv->tv_usec * 1000;
|
2009-07-30 00:11:41 +00:00
|
|
|
if (ts.tv_nsec >= 1000000000) {
|
2009-06-23 01:04:58 +00:00
|
|
|
ts.tv_nsec -= 1000000000;
|
|
|
|
ts.tv_sec++;
|
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
err = pthread_cond_timedwait(&ctx->ctx_cond,
|
|
|
|
&ctx->ctx_lock, &ts);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err == ETIMEDOUT)
|
2009-06-23 01:04:58 +00:00
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_handle_events_timeout(libusb_context *ctx, struct timeval *tv)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_lock_events(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = libusb_handle_events_locked(ctx, tv);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_unlock_events(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_handle_events_timeout leave");
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_handle_events(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
return (libusb_handle_events_timeout(ctx, NULL));
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_handle_events_locked(libusb_context *ctx, struct timeval *tv)
|
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
|
|
|
ctx = GET_CONTEXT(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (libusb_event_handling_ok(ctx)) {
|
|
|
|
err = libusb10_handle_events_sub(ctx, tv);
|
|
|
|
} else {
|
|
|
|
libusb_wait_for_event(ctx, tv);
|
|
|
|
err = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
2009-07-30 00:11:41 +00:00
|
|
|
return (err);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_next_timeout(libusb_context *ctx, struct timeval *tv)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
/* all timeouts are currently being done by the kernel */
|
|
|
|
timerclear(tv);
|
|
|
|
return (0);
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_set_pollfd_notifiers(libusb_context *ctx,
|
2009-06-23 01:04:58 +00:00
|
|
|
libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
ctx->fd_added_cb = added_cb;
|
|
|
|
ctx->fd_removed_cb = removed_cb;
|
|
|
|
ctx->fd_cb_user_data = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct libusb_pollfd **
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_get_pollfds(libusb_context *ctx)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_super_pollfd *pollfd;
|
2009-06-23 01:04:58 +00:00
|
|
|
libusb_pollfd **ret;
|
|
|
|
int i;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(ctx);
|
|
|
|
|
|
|
|
CTX_LOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
i = 0;
|
2009-07-30 00:11:41 +00:00
|
|
|
TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
|
|
|
|
i++;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = calloc(i + 1, sizeof(struct libusb_pollfd *));
|
|
|
|
if (ret == NULL)
|
|
|
|
goto done;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
i = 0;
|
2009-07-30 00:11:41 +00:00
|
|
|
TAILQ_FOREACH(pollfd, &ctx->pollfds, entry)
|
|
|
|
ret[i++] = &pollfd->pollfd;
|
2009-06-23 01:04:58 +00:00
|
|
|
ret[i] = NULL;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
done:
|
|
|
|
CTX_UNLOCK(ctx);
|
2009-06-23 01:04:58 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Synchronous device I/O */
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_control_transfer(libusb_device_handle *devh,
|
2009-06-23 01:04:58 +00:00
|
|
|
uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
|
2009-07-30 00:11:41 +00:00
|
|
|
uint8_t *data, uint16_t wLength, unsigned int timeout)
|
2009-06-23 01:04:58 +00:00
|
|
|
{
|
2009-07-30 00:11:41 +00:00
|
|
|
struct LIBUSB20_CONTROL_SETUP_DECODED req;
|
|
|
|
int err;
|
|
|
|
uint16_t actlen;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (devh == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if ((wLength != 0) && (data == NULL))
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
req.bmRequestType = bmRequestType;
|
|
|
|
req.bRequest = bRequest;
|
|
|
|
req.wValue = wValue;
|
|
|
|
req.wIndex = wIndex;
|
|
|
|
req.wLength = wLength;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
err = libusb20_dev_request_sync(devh, &req, data,
|
|
|
|
&actlen, timeout, 0);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (err == LIBUSB20_ERROR_PIPE)
|
|
|
|
return (LIBUSB_ERROR_PIPE);
|
|
|
|
else if (err == LIBUSB20_ERROR_TIMEOUT)
|
|
|
|
return (LIBUSB_ERROR_TIMEOUT);
|
|
|
|
else if (err)
|
|
|
|
return (LIBUSB_ERROR_NO_DEVICE);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
return (actlen);
|
|
|
|
}
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
static void
|
|
|
|
libusb10_do_transfer_cb(struct libusb_transfer *transfer)
|
|
|
|
{
|
|
|
|
libusb_context *ctx;
|
|
|
|
int *pdone;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(NULL);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_TRANSFER, "sync I/O done");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
pdone = transfer->user_data;
|
|
|
|
*pdone = 1;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
/*
|
|
|
|
* TODO: Replace the following function. Allocating and freeing on a
|
|
|
|
* per-transfer basis is slow. --HPS
|
|
|
|
*/
|
2009-06-23 01:04:58 +00:00
|
|
|
static int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb10_do_transfer(libusb_device_handle *devh,
|
|
|
|
uint8_t endpoint, uint8_t *data, int length,
|
2009-06-23 01:04:58 +00:00
|
|
|
int *transferred, unsigned int timeout, int type)
|
|
|
|
{
|
|
|
|
libusb_context *ctx;
|
2009-07-30 00:11:41 +00:00
|
|
|
struct libusb_transfer *xfer;
|
|
|
|
volatile int complet;
|
2009-06-23 01:04:58 +00:00
|
|
|
int ret;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
if (devh == NULL)
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
|
|
|
|
|
|
|
if ((length != 0) && (data == NULL))
|
|
|
|
return (LIBUSB_ERROR_INVALID_PARAM);
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
xfer = libusb_alloc_transfer(0);
|
|
|
|
if (xfer == NULL)
|
|
|
|
return (LIBUSB_ERROR_NO_MEM);
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = libusb_get_device(devh)->ctx;
|
2009-06-23 01:04:58 +00:00
|
|
|
|
|
|
|
xfer->dev_handle = devh;
|
|
|
|
xfer->endpoint = endpoint;
|
|
|
|
xfer->type = type;
|
|
|
|
xfer->timeout = timeout;
|
|
|
|
xfer->buffer = data;
|
|
|
|
xfer->length = length;
|
2009-07-30 00:11:41 +00:00
|
|
|
xfer->user_data = (void *)&complet;
|
|
|
|
xfer->callback = libusb10_do_transfer_cb;
|
2009-06-23 01:04:58 +00:00
|
|
|
complet = 0;
|
|
|
|
|
|
|
|
if ((ret = libusb_submit_transfer(xfer)) < 0) {
|
|
|
|
libusb_free_transfer(xfer);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
while (complet == 0) {
|
|
|
|
if ((ret = libusb_handle_events(ctx)) < 0) {
|
|
|
|
libusb_cancel_transfer(xfer);
|
2009-07-30 00:11:41 +00:00
|
|
|
usleep(1000); /* nice it */
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*transferred = xfer->actual_length;
|
2009-07-30 00:11:41 +00:00
|
|
|
|
2009-06-23 01:04:58 +00:00
|
|
|
switch (xfer->status) {
|
|
|
|
case LIBUSB_TRANSFER_COMPLETED:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = 0;
|
2009-06-23 01:04:58 +00:00
|
|
|
break;
|
|
|
|
case LIBUSB_TRANSFER_TIMED_OUT:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = LIBUSB_ERROR_TIMEOUT;
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
case LIBUSB_TRANSFER_OVERFLOW:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = LIBUSB_ERROR_OVERFLOW;
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
case LIBUSB_TRANSFER_STALL:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = LIBUSB_ERROR_PIPE;
|
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
case LIBUSB_TRANSFER_NO_DEVICE:
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = LIBUSB_ERROR_NO_DEVICE;
|
2009-06-23 01:04:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = LIBUSB_ERROR_OTHER;
|
2009-07-30 00:11:41 +00:00
|
|
|
break;
|
2009-06-23 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
libusb_free_transfer(xfer);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_bulk_transfer(libusb_device_handle *devh,
|
|
|
|
uint8_t endpoint, uint8_t *data, int length,
|
2009-06-23 01:04:58 +00:00
|
|
|
int *transferred, unsigned int timeout)
|
|
|
|
{
|
|
|
|
libusb_context *ctx;
|
|
|
|
int ret;
|
2009-07-30 00:11:41 +00:00
|
|
|
|
|
|
|
ctx = GET_CONTEXT(NULL);
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
|
2009-06-23 01:04:58 +00:00
|
|
|
timeout, LIBUSB_TRANSFER_TYPE_BULK);
|
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_bulk_transfer leave");
|
2009-06-23 01:04:58 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-07-30 00:11:41 +00:00
|
|
|
libusb_interrupt_transfer(libusb_device_handle *devh,
|
|
|
|
uint8_t endpoint, uint8_t *data, int length,
|
2009-06-23 01:04:58 +00:00
|
|
|
int *transferred, unsigned int timeout)
|
|
|
|
{
|
|
|
|
libusb_context *ctx;
|
|
|
|
int ret;
|
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ctx = GET_CONTEXT(NULL);
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer enter");
|
2009-06-23 01:04:58 +00:00
|
|
|
|
2009-07-30 00:11:41 +00:00
|
|
|
ret = libusb10_do_transfer(devh, endpoint, data, length, transferred,
|
2009-06-23 01:04:58 +00:00
|
|
|
timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
|
|
|
|
|
2009-07-10 14:15:53 +00:00
|
|
|
DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_interrupt_transfer leave");
|
2009-06-23 01:04:58 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
2009-11-08 20:03:52 +00:00
|
|
|
|
|
|
|
uint8_t *
|
|
|
|
libusb_get_iso_packet_buffer(struct libusb_transfer *transfer, uint32_t index)
|
|
|
|
{
|
|
|
|
uint8_t *ptr;
|
|
|
|
uint32_t n;
|
|
|
|
|
|
|
|
if (transfer->num_iso_packets < 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (index >= (uint32_t)transfer->num_iso_packets)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
ptr = transfer->buffer;
|
|
|
|
if (ptr == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
for (n = 0; n != index; n++) {
|
|
|
|
ptr += transfer->iso_packet_desc[n].length;
|
|
|
|
}
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *
|
|
|
|
libusb_get_iso_packet_buffer_simple(struct libusb_transfer *transfer, uint32_t index)
|
|
|
|
{
|
|
|
|
uint8_t *ptr;
|
|
|
|
|
|
|
|
if (transfer->num_iso_packets < 0)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
if (index >= (uint32_t)transfer->num_iso_packets)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
ptr = transfer->buffer;
|
|
|
|
if (ptr == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
ptr += transfer->iso_packet_desc[0].length * index;
|
|
|
|
|
|
|
|
return (ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_set_iso_packet_lengths(struct libusb_transfer *transfer, uint32_t length)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (transfer->num_iso_packets < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (n = 0; n != transfer->num_iso_packets; n++)
|
|
|
|
transfer->iso_packet_desc[n].length = length;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *
|
|
|
|
libusb_control_transfer_get_data(struct libusb_transfer *transfer)
|
|
|
|
{
|
|
|
|
if (transfer->buffer == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
return (transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct libusb_control_setup *
|
|
|
|
libusb_control_transfer_get_setup(struct libusb_transfer *transfer)
|
|
|
|
{
|
|
|
|
return ((struct libusb_control_setup *)transfer->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_fill_control_setup(uint8_t *buf, uint8_t bmRequestType,
|
|
|
|
uint8_t bRequest, uint16_t wValue,
|
|
|
|
uint16_t wIndex, uint16_t wLength)
|
|
|
|
{
|
|
|
|
struct libusb_control_setup *req = (struct libusb_control_setup *)buf;
|
|
|
|
|
|
|
|
/* The alignment is OK for all fields below. */
|
|
|
|
req->bmRequestType = bmRequestType;
|
|
|
|
req->bRequest = bRequest;
|
|
|
|
req->wValue = htole16(wValue);
|
|
|
|
req->wIndex = htole16(wIndex);
|
|
|
|
req->wLength = htole16(wLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_fill_control_transfer(struct libusb_transfer *transfer,
|
|
|
|
libusb_device_handle *devh, uint8_t *buf,
|
|
|
|
libusb_transfer_cb_fn callback, void *user_data,
|
|
|
|
uint32_t timeout)
|
|
|
|
{
|
|
|
|
struct libusb_control_setup *setup = (struct libusb_control_setup *)buf;
|
|
|
|
|
|
|
|
transfer->dev_handle = devh;
|
|
|
|
transfer->endpoint = 0;
|
|
|
|
transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL;
|
|
|
|
transfer->timeout = timeout;
|
|
|
|
transfer->buffer = buf;
|
|
|
|
if (setup != NULL)
|
|
|
|
transfer->length = LIBUSB_CONTROL_SETUP_SIZE
|
|
|
|
+ le16toh(setup->wLength);
|
|
|
|
else
|
|
|
|
transfer->length = 0;
|
|
|
|
transfer->user_data = user_data;
|
|
|
|
transfer->callback = callback;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_fill_bulk_transfer(struct libusb_transfer *transfer,
|
|
|
|
libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
|
|
|
|
int length, libusb_transfer_cb_fn callback, void *user_data,
|
|
|
|
uint32_t timeout)
|
|
|
|
{
|
|
|
|
transfer->dev_handle = devh;
|
|
|
|
transfer->endpoint = endpoint;
|
|
|
|
transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
|
|
|
|
transfer->timeout = timeout;
|
|
|
|
transfer->buffer = buf;
|
|
|
|
transfer->length = length;
|
|
|
|
transfer->user_data = user_data;
|
|
|
|
transfer->callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_fill_interrupt_transfer(struct libusb_transfer *transfer,
|
|
|
|
libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
|
|
|
|
int length, libusb_transfer_cb_fn callback, void *user_data,
|
|
|
|
uint32_t timeout)
|
|
|
|
{
|
|
|
|
transfer->dev_handle = devh;
|
|
|
|
transfer->endpoint = endpoint;
|
|
|
|
transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
|
|
|
|
transfer->timeout = timeout;
|
|
|
|
transfer->buffer = buf;
|
|
|
|
transfer->length = length;
|
|
|
|
transfer->user_data = user_data;
|
|
|
|
transfer->callback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
libusb_fill_iso_transfer(struct libusb_transfer *transfer,
|
|
|
|
libusb_device_handle *devh, uint8_t endpoint, uint8_t *buf,
|
|
|
|
int length, int npacket, libusb_transfer_cb_fn callback,
|
|
|
|
void *user_data, uint32_t timeout)
|
|
|
|
{
|
|
|
|
transfer->dev_handle = devh;
|
|
|
|
transfer->endpoint = endpoint;
|
|
|
|
transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS;
|
|
|
|
transfer->timeout = timeout;
|
|
|
|
transfer->buffer = buf;
|
|
|
|
transfer->length = length;
|
|
|
|
transfer->num_iso_packets = npacket;
|
|
|
|
transfer->user_data = user_data;
|
|
|
|
transfer->callback = callback;
|
|
|
|
}
|
|
|
|
|