libusb10.c revision 261224
1194676Sthompsa/* $FreeBSD: head/lib/libusb/libusb10.c 261224 2014-01-28 07:21:46Z hselasky $ */
2194676Sthompsa/*-
3194676Sthompsa * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4195957Salfred * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5194676Sthompsa *
6194676Sthompsa * Redistribution and use in source and binary forms, with or without
7194676Sthompsa * modification, are permitted provided that the following conditions
8194676Sthompsa * are met:
9194676Sthompsa * 1. Redistributions of source code must retain the above copyright
10194676Sthompsa *    notice, this list of conditions and the following disclaimer.
11194676Sthompsa * 2. Redistributions in binary form must reproduce the above copyright
12194676Sthompsa *    notice, this list of conditions and the following disclaimer in the
13194676Sthompsa *    documentation and/or other materials provided with the distribution.
14194676Sthompsa *
15194676Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16194676Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17194676Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18194676Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19194676Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20194676Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21194676Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22194676Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23194676Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24194676Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25194676Sthompsa * SUCH DAMAGE.
26194676Sthompsa */
27194676Sthompsa
28248236Shselasky#ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29248236Shselasky#include LIBUSB_GLOBAL_INCLUDE_FILE
30248236Shselasky#else
31203774Swkoszek#include <assert.h>
32203815Swkoszek#include <errno.h>
33203815Swkoszek#include <poll.h>
34203815Swkoszek#include <pthread.h>
35203815Swkoszek#include <stdio.h>
36194676Sthompsa#include <stdlib.h>
37248236Shselasky#include <string.h>
38194676Sthompsa#include <unistd.h>
39248236Shselasky#include <time.h>
40248236Shselasky#include <sys/fcntl.h>
41248236Shselasky#include <sys/ioctl.h>
42248236Shselasky#include <sys/queue.h>
43248236Shselasky#include <sys/endian.h>
44248236Shselasky#endif
45194676Sthompsa
46208020Sthompsa#define	libusb_device_handle libusb20_device
47208020Sthompsa
48194676Sthompsa#include "libusb20.h"
49194676Sthompsa#include "libusb20_desc.h"
50194676Sthompsa#include "libusb20_int.h"
51194676Sthompsa#include "libusb.h"
52194676Sthompsa#include "libusb10.h"
53194676Sthompsa
54194676Sthompsastatic pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
55194676Sthompsastruct libusb_context *usbi_default_context = NULL;
56194676Sthompsa
57195957Salfred/* Prototypes */
58195957Salfred
59195957Salfredstatic struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
60195957Salfredstatic int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
61195957Salfredstatic int libusb10_convert_error(uint8_t status);
62195957Salfredstatic void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
63195957Salfredstatic void libusb10_isoc_proxy(struct libusb20_transfer *);
64195957Salfredstatic void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
65195957Salfredstatic void libusb10_ctrl_proxy(struct libusb20_transfer *);
66195957Salfredstatic void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
67195957Salfred
68194676Sthompsa/*  Library initialisation / deinitialisation */
69194676Sthompsa
70194676Sthompsavoid
71195957Salfredlibusb_set_debug(libusb_context *ctx, int level)
72194676Sthompsa{
73195957Salfred	ctx = GET_CONTEXT(ctx);
74194676Sthompsa	if (ctx)
75194676Sthompsa		ctx->debug = level;
76194676Sthompsa}
77194676Sthompsa
78213853Shselaskystatic void
79213853Shselaskylibusb_set_nonblocking(int f)
80213853Shselasky{
81213853Shselasky	int flags;
82213853Shselasky
83213853Shselasky	/*
84213853Shselasky	 * We ignore any failures in this function, hence the
85213853Shselasky	 * non-blocking flag is not critical to the operation of
86213853Shselasky	 * libUSB. We use F_GETFL and F_SETFL to be compatible with
87213853Shselasky	 * Linux.
88213853Shselasky	 */
89213853Shselasky
90213853Shselasky	flags = fcntl(f, F_GETFL, NULL);
91213853Shselasky	if (flags == -1)
92213853Shselasky		return;
93213853Shselasky	flags |= O_NONBLOCK;
94213853Shselasky	fcntl(f, F_SETFL, flags);
95213853Shselasky}
96213853Shselasky
97194676Sthompsaint
98195957Salfredlibusb_init(libusb_context **context)
99194676Sthompsa{
100194676Sthompsa	struct libusb_context *ctx;
101236944Shselasky	pthread_condattr_t attr;
102195957Salfred	char *debug;
103194676Sthompsa	int ret;
104194676Sthompsa
105194676Sthompsa	ctx = malloc(sizeof(*ctx));
106194676Sthompsa	if (!ctx)
107194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
108194676Sthompsa
109194676Sthompsa	memset(ctx, 0, sizeof(*ctx));
110194676Sthompsa
111194676Sthompsa	debug = getenv("LIBUSB_DEBUG");
112194676Sthompsa	if (debug != NULL) {
113194676Sthompsa		ctx->debug = atoi(debug);
114194676Sthompsa		if (ctx->debug != 0)
115194676Sthompsa			ctx->debug_fixed = 1;
116194676Sthompsa	}
117195957Salfred	TAILQ_INIT(&ctx->pollfds);
118195957Salfred	TAILQ_INIT(&ctx->tr_done);
119194676Sthompsa
120236944Shselasky	if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
121236944Shselasky		free(ctx);
122236944Shselasky		return (LIBUSB_ERROR_NO_MEM);
123236944Shselasky	}
124236944Shselasky	if (pthread_condattr_init(&attr) != 0) {
125236944Shselasky		pthread_mutex_destroy(&ctx->ctx_lock);
126236944Shselasky		free(ctx);
127236944Shselasky		return (LIBUSB_ERROR_NO_MEM);
128236944Shselasky	}
129236944Shselasky	if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
130236944Shselasky		pthread_mutex_destroy(&ctx->ctx_lock);
131236944Shselasky		pthread_condattr_destroy(&attr);
132236944Shselasky		free(ctx);
133236944Shselasky		return (LIBUSB_ERROR_OTHER);
134236944Shselasky	}
135236944Shselasky	if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
136236944Shselasky		pthread_mutex_destroy(&ctx->ctx_lock);
137236944Shselasky		pthread_condattr_destroy(&attr);
138236944Shselasky		free(ctx);
139236944Shselasky		return (LIBUSB_ERROR_NO_MEM);
140236944Shselasky	}
141236944Shselasky	pthread_condattr_destroy(&attr);
142194676Sthompsa
143195957Salfred	ctx->ctx_handler = NO_THREAD;
144194676Sthompsa
145194676Sthompsa	ret = pipe(ctx->ctrl_pipe);
146194676Sthompsa	if (ret < 0) {
147195957Salfred		pthread_mutex_destroy(&ctx->ctx_lock);
148195957Salfred		pthread_cond_destroy(&ctx->ctx_cond);
149194676Sthompsa		free(ctx);
150194676Sthompsa		return (LIBUSB_ERROR_OTHER);
151194676Sthompsa	}
152195957Salfred	/* set non-blocking mode on the control pipe to avoid deadlock */
153213853Shselasky	libusb_set_nonblocking(ctx->ctrl_pipe[0]);
154213853Shselasky	libusb_set_nonblocking(ctx->ctrl_pipe[1]);
155194676Sthompsa
156195957Salfred	libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
157194676Sthompsa
158194676Sthompsa	pthread_mutex_lock(&default_context_lock);
159194676Sthompsa	if (usbi_default_context == NULL) {
160194676Sthompsa		usbi_default_context = ctx;
161194676Sthompsa	}
162194676Sthompsa	pthread_mutex_unlock(&default_context_lock);
163194676Sthompsa
164194676Sthompsa	if (context)
165194676Sthompsa		*context = ctx;
166194676Sthompsa
167195957Salfred	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
168195957Salfred
169194676Sthompsa	return (0);
170194676Sthompsa}
171194676Sthompsa
172194676Sthompsavoid
173195957Salfredlibusb_exit(libusb_context *ctx)
174194676Sthompsa{
175195957Salfred	ctx = GET_CONTEXT(ctx);
176194676Sthompsa
177195957Salfred	if (ctx == NULL)
178195957Salfred		return;
179195957Salfred
180195957Salfred	/* XXX cleanup devices */
181195957Salfred
182195957Salfred	libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
183194676Sthompsa	close(ctx->ctrl_pipe[0]);
184194676Sthompsa	close(ctx->ctrl_pipe[1]);
185195957Salfred	pthread_mutex_destroy(&ctx->ctx_lock);
186195957Salfred	pthread_cond_destroy(&ctx->ctx_cond);
187194676Sthompsa
188194676Sthompsa	pthread_mutex_lock(&default_context_lock);
189194676Sthompsa	if (ctx == usbi_default_context) {
190194676Sthompsa		usbi_default_context = NULL;
191194676Sthompsa	}
192194676Sthompsa	pthread_mutex_unlock(&default_context_lock);
193194676Sthompsa
194194676Sthompsa	free(ctx);
195194676Sthompsa}
196194676Sthompsa
197194676Sthompsa/* Device handling and initialisation. */
198194676Sthompsa
199194676Sthompsassize_t
200195957Salfredlibusb_get_device_list(libusb_context *ctx, libusb_device ***list)
201194676Sthompsa{
202195957Salfred	struct libusb20_backend *usb_backend;
203194676Sthompsa	struct libusb20_device *pdev;
204194676Sthompsa	struct libusb_device *dev;
205194676Sthompsa	int i;
206194676Sthompsa
207195957Salfred	ctx = GET_CONTEXT(ctx);
208194676Sthompsa
209195957Salfred	if (ctx == NULL)
210195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
211195957Salfred
212195957Salfred	if (list == NULL)
213195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
214195957Salfred
215194676Sthompsa	usb_backend = libusb20_be_alloc_default();
216194676Sthompsa	if (usb_backend == NULL)
217195957Salfred		return (LIBUSB_ERROR_NO_MEM);
218194676Sthompsa
219195957Salfred	/* figure out how many USB devices are present */
220194676Sthompsa	pdev = NULL;
221194676Sthompsa	i = 0;
222194676Sthompsa	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
223194676Sthompsa		i++;
224194676Sthompsa
225195957Salfred	/* allocate device pointer list */
226194676Sthompsa	*list = malloc((i + 1) * sizeof(void *));
227194676Sthompsa	if (*list == NULL) {
228194676Sthompsa		libusb20_be_free(usb_backend);
229194676Sthompsa		return (LIBUSB_ERROR_NO_MEM);
230194676Sthompsa	}
231195957Salfred	/* create libusb v1.0 compliant devices */
232194676Sthompsa	i = 0;
233194676Sthompsa	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
234194676Sthompsa
235194676Sthompsa		dev = malloc(sizeof(*dev));
236194676Sthompsa		if (dev == NULL) {
237195560Sthompsa			while (i != 0) {
238195560Sthompsa				libusb_unref_device((*list)[i - 1]);
239195560Sthompsa				i--;
240195560Sthompsa			}
241194676Sthompsa			free(*list);
242195957Salfred			*list = NULL;
243194676Sthompsa			libusb20_be_free(usb_backend);
244194676Sthompsa			return (LIBUSB_ERROR_NO_MEM);
245194676Sthompsa		}
246199055Sthompsa		/* get device into libUSB v1.0 list */
247199055Sthompsa		libusb20_be_dequeue_device(usb_backend, pdev);
248199055Sthompsa
249194676Sthompsa		memset(dev, 0, sizeof(*dev));
250194676Sthompsa
251195957Salfred		/* init transfer queues */
252195957Salfred		TAILQ_INIT(&dev->tr_head);
253195957Salfred
254195957Salfred		/* set context we belong to */
255194676Sthompsa		dev->ctx = ctx;
256194676Sthompsa
257194676Sthompsa		/* link together the two structures */
258194676Sthompsa		dev->os_priv = pdev;
259195957Salfred		pdev->privLuData = dev;
260194676Sthompsa
261194676Sthompsa		(*list)[i] = libusb_ref_device(dev);
262194676Sthompsa		i++;
263194676Sthompsa	}
264194676Sthompsa	(*list)[i] = NULL;
265194676Sthompsa
266194676Sthompsa	libusb20_be_free(usb_backend);
267194676Sthompsa	return (i);
268194676Sthompsa}
269194676Sthompsa
270194676Sthompsavoid
271194676Sthompsalibusb_free_device_list(libusb_device **list, int unref_devices)
272194676Sthompsa{
273194676Sthompsa	int i;
274194676Sthompsa
275194676Sthompsa	if (list == NULL)
276195957Salfred		return;			/* be NULL safe */
277194676Sthompsa
278194676Sthompsa	if (unref_devices) {
279194676Sthompsa		for (i = 0; list[i] != NULL; i++)
280194676Sthompsa			libusb_unref_device(list[i]);
281194676Sthompsa	}
282194676Sthompsa	free(list);
283194676Sthompsa}
284194676Sthompsa
285194676Sthompsauint8_t
286195957Salfredlibusb_get_bus_number(libusb_device *dev)
287194676Sthompsa{
288194676Sthompsa	if (dev == NULL)
289195957Salfred		return (0);		/* should not happen */
290195957Salfred	return (libusb20_dev_get_bus_number(dev->os_priv));
291194676Sthompsa}
292194676Sthompsa
293250342Semasteint
294251495Semastelibusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
295251495Semaste{
296251495Semaste	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
297251495Semaste}
298251495Semaste
299251495Semasteint
300250342Semastelibusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
301250342Semaste    uint8_t bufsize)
302250342Semaste{
303250342Semaste	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
304250342Semaste}
305250342Semaste
306194676Sthompsauint8_t
307195957Salfredlibusb_get_device_address(libusb_device *dev)
308194676Sthompsa{
309194676Sthompsa	if (dev == NULL)
310195957Salfred		return (0);		/* should not happen */
311195957Salfred	return (libusb20_dev_get_address(dev->os_priv));
312194676Sthompsa}
313194676Sthompsa
314224903Shselaskyenum libusb_speed
315224903Shselaskylibusb_get_device_speed(libusb_device *dev)
316224903Shselasky{
317224903Shselasky	if (dev == NULL)
318225035Shselasky		return (LIBUSB_SPEED_UNKNOWN);	/* should not happen */
319224903Shselasky
320224903Shselasky	switch (libusb20_dev_get_speed(dev->os_priv)) {
321224903Shselasky	case LIBUSB20_SPEED_LOW:
322224903Shselasky		return (LIBUSB_SPEED_LOW);
323224903Shselasky	case LIBUSB20_SPEED_FULL:
324224903Shselasky		return (LIBUSB_SPEED_FULL);
325224903Shselasky	case LIBUSB20_SPEED_HIGH:
326224903Shselasky		return (LIBUSB_SPEED_HIGH);
327224903Shselasky	case LIBUSB20_SPEED_SUPER:
328224903Shselasky		return (LIBUSB_SPEED_SUPER);
329224903Shselasky	default:
330224903Shselasky		break;
331224903Shselasky	}
332224903Shselasky	return (LIBUSB_SPEED_UNKNOWN);
333224903Shselasky}
334224903Shselasky
335194676Sthompsaint
336195957Salfredlibusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
337194676Sthompsa{
338194676Sthompsa	struct libusb_config_descriptor *pdconf;
339194676Sthompsa	struct libusb_interface *pinf;
340194676Sthompsa	struct libusb_interface_descriptor *pdinf;
341194676Sthompsa	struct libusb_endpoint_descriptor *pdend;
342195957Salfred	int i;
343195957Salfred	int j;
344195957Salfred	int k;
345195957Salfred	int ret;
346194676Sthompsa
347194676Sthompsa	if (dev == NULL)
348194676Sthompsa		return (LIBUSB_ERROR_NO_DEVICE);
349194676Sthompsa
350195957Salfred	ret = libusb_get_active_config_descriptor(dev, &pdconf);
351195957Salfred	if (ret < 0)
352195957Salfred		return (ret);
353195957Salfred
354194676Sthompsa	ret = LIBUSB_ERROR_NOT_FOUND;
355195957Salfred	for (i = 0; i < pdconf->bNumInterfaces; i++) {
356194676Sthompsa		pinf = &pdconf->interface[i];
357195957Salfred		for (j = 0; j < pinf->num_altsetting; j++) {
358194676Sthompsa			pdinf = &pinf->altsetting[j];
359195957Salfred			for (k = 0; k < pdinf->bNumEndpoints; k++) {
360194676Sthompsa				pdend = &pdinf->endpoint[k];
361194676Sthompsa				if (pdend->bEndpointAddress == endpoint) {
362194676Sthompsa					ret = pdend->wMaxPacketSize;
363194676Sthompsa					goto out;
364194676Sthompsa				}
365194676Sthompsa			}
366194676Sthompsa		}
367194676Sthompsa	}
368194676Sthompsa
369194676Sthompsaout:
370194676Sthompsa	libusb_free_config_descriptor(pdconf);
371194676Sthompsa	return (ret);
372194676Sthompsa}
373194676Sthompsa
374234193Shselaskyint
375234193Shselaskylibusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
376234193Shselasky{
377234193Shselasky	int multiplier;
378234193Shselasky	int ret;
379234193Shselasky
380234193Shselasky	ret = libusb_get_max_packet_size(dev, endpoint);
381234193Shselasky
382234193Shselasky	switch (libusb20_dev_get_speed(dev->os_priv)) {
383234193Shselasky	case LIBUSB20_SPEED_LOW:
384234193Shselasky	case LIBUSB20_SPEED_FULL:
385234193Shselasky		break;
386234193Shselasky	default:
387234193Shselasky		if (ret > -1) {
388234193Shselasky			multiplier = (1 + ((ret >> 11) & 3));
389234193Shselasky			if (multiplier > 3)
390234193Shselasky				multiplier = 3;
391234193Shselasky			ret = (ret & 0x7FF) * multiplier;
392234193Shselasky		}
393234193Shselasky		break;
394234193Shselasky	}
395234193Shselasky	return (ret);
396234193Shselasky}
397234193Shselasky
398194676Sthompsalibusb_device *
399195957Salfredlibusb_ref_device(libusb_device *dev)
400194676Sthompsa{
401194676Sthompsa	if (dev == NULL)
402195957Salfred		return (NULL);		/* be NULL safe */
403194676Sthompsa
404195957Salfred	CTX_LOCK(dev->ctx);
405194676Sthompsa	dev->refcnt++;
406195957Salfred	CTX_UNLOCK(dev->ctx);
407194676Sthompsa
408194676Sthompsa	return (dev);
409194676Sthompsa}
410194676Sthompsa
411194676Sthompsavoid
412195957Salfredlibusb_unref_device(libusb_device *dev)
413194676Sthompsa{
414194676Sthompsa	if (dev == NULL)
415195957Salfred		return;			/* be NULL safe */
416194676Sthompsa
417195957Salfred	CTX_LOCK(dev->ctx);
418194676Sthompsa	dev->refcnt--;
419195957Salfred	CTX_UNLOCK(dev->ctx);
420194676Sthompsa
421194676Sthompsa	if (dev->refcnt == 0) {
422194676Sthompsa		libusb20_dev_free(dev->os_priv);
423194676Sthompsa		free(dev);
424194676Sthompsa	}
425194676Sthompsa}
426194676Sthompsa
427194676Sthompsaint
428195957Salfredlibusb_open(libusb_device *dev, libusb_device_handle **devh)
429194676Sthompsa{
430194676Sthompsa	libusb_context *ctx = dev->ctx;
431194676Sthompsa	struct libusb20_device *pdev = dev->os_priv;
432195957Salfred	uint8_t dummy;
433194676Sthompsa	int err;
434194676Sthompsa
435194676Sthompsa	if (devh == NULL)
436194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
437194676Sthompsa
438195957Salfred	/* set default device handle value */
439195957Salfred	*devh = NULL;
440194676Sthompsa
441195957Salfred	dev = libusb_ref_device(dev);
442195957Salfred	if (dev == NULL)
443195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
444195957Salfred
445194676Sthompsa	err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
446194676Sthompsa	if (err) {
447195957Salfred		libusb_unref_device(dev);
448194676Sthompsa		return (LIBUSB_ERROR_NO_MEM);
449194676Sthompsa	}
450195957Salfred	libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
451194676Sthompsa	    POLLOUT | POLLRDNORM | POLLWRNORM);
452194676Sthompsa
453195957Salfred	/* make sure our event loop detects the new device */
454195957Salfred	dummy = 0;
455194676Sthompsa	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
456213853Shselasky	if (err < (int)sizeof(dummy)) {
457195957Salfred		/* ignore error, if any */
458195957Salfred		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
459194676Sthompsa	}
460195957Salfred	*devh = pdev;
461194676Sthompsa
462194676Sthompsa	return (0);
463194676Sthompsa}
464194676Sthompsa
465194676Sthompsalibusb_device_handle *
466195957Salfredlibusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
467194676Sthompsa    uint16_t product_id)
468194676Sthompsa{
469194676Sthompsa	struct libusb_device **devs;
470194676Sthompsa	struct libusb20_device *pdev;
471194676Sthompsa	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
472195957Salfred	int i;
473195957Salfred	int j;
474194676Sthompsa
475195957Salfred	ctx = GET_CONTEXT(ctx);
476195957Salfred	if (ctx == NULL)
477195957Salfred		return (NULL);		/* be NULL safe */
478195957Salfred
479195560Sthompsa	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
480194676Sthompsa
481194676Sthompsa	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
482194676Sthompsa		return (NULL);
483194676Sthompsa
484228236Shselasky	pdev = NULL;
485194676Sthompsa	for (j = 0; j < i; j++) {
486228236Shselasky		struct libusb20_device *tdev;
487228236Shselasky
488228236Shselasky		tdev = devs[j]->os_priv;
489228236Shselasky		pdesc = libusb20_dev_get_device_desc(tdev);
490195957Salfred		/*
491195957Salfred		 * NOTE: The USB library will automatically swap the
492195957Salfred		 * fields in the device descriptor to be of host
493195957Salfred		 * endian type!
494195957Salfred		 */
495194676Sthompsa		if (pdesc->idVendor == vendor_id &&
496195560Sthompsa		    pdesc->idProduct == product_id) {
497228235Shselasky			libusb_open(devs[j], &pdev);
498195957Salfred			break;
499195560Sthompsa		}
500194676Sthompsa	}
501194676Sthompsa
502194676Sthompsa	libusb_free_device_list(devs, 1);
503195560Sthompsa	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
504195957Salfred	return (pdev);
505194676Sthompsa}
506194676Sthompsa
507194676Sthompsavoid
508195957Salfredlibusb_close(struct libusb20_device *pdev)
509194676Sthompsa{
510194676Sthompsa	libusb_context *ctx;
511195957Salfred	struct libusb_device *dev;
512195957Salfred	uint8_t dummy;
513194676Sthompsa	int err;
514194676Sthompsa
515195957Salfred	if (pdev == NULL)
516195957Salfred		return;			/* be NULL safe */
517194676Sthompsa
518195957Salfred	dev = libusb_get_device(pdev);
519195957Salfred	ctx = dev->ctx;
520194676Sthompsa
521195957Salfred	libusb10_remove_pollfd(ctx, &dev->dev_poll);
522194676Sthompsa
523195957Salfred	libusb20_dev_close(pdev);
524199055Sthompsa
525199055Sthompsa	/* unref will free the "pdev" when the refcount reaches zero */
526195957Salfred	libusb_unref_device(dev);
527194676Sthompsa
528195957Salfred	/* make sure our event loop detects the closed device */
529195957Salfred	dummy = 0;
530194676Sthompsa	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
531213853Shselasky	if (err < (int)sizeof(dummy)) {
532195957Salfred		/* ignore error, if any */
533195957Salfred		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
534194676Sthompsa	}
535194676Sthompsa}
536194676Sthompsa
537194676Sthompsalibusb_device *
538195957Salfredlibusb_get_device(struct libusb20_device *pdev)
539194676Sthompsa{
540195957Salfred	if (pdev == NULL)
541194676Sthompsa		return (NULL);
542195957Salfred	return ((libusb_device *)pdev->privLuData);
543194676Sthompsa}
544194676Sthompsa
545194676Sthompsaint
546195957Salfredlibusb_get_configuration(struct libusb20_device *pdev, int *config)
547194676Sthompsa{
548195957Salfred	struct libusb20_config *pconf;
549194676Sthompsa
550195957Salfred	if (pdev == NULL || config == NULL)
551194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
552194676Sthompsa
553195957Salfred	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
554195957Salfred	if (pconf == NULL)
555195957Salfred		return (LIBUSB_ERROR_NO_MEM);
556194676Sthompsa
557195957Salfred	*config = pconf->desc.bConfigurationValue;
558195957Salfred
559195957Salfred	free(pconf);
560195957Salfred
561194676Sthompsa	return (0);
562194676Sthompsa}
563194676Sthompsa
564194676Sthompsaint
565195957Salfredlibusb_set_configuration(struct libusb20_device *pdev, int configuration)
566194676Sthompsa{
567195957Salfred	struct libusb20_config *pconf;
568195957Salfred	struct libusb_device *dev;
569195957Salfred	int err;
570195957Salfred	uint8_t i;
571194676Sthompsa
572195957Salfred	dev = libusb_get_device(pdev);
573195957Salfred	if (dev == NULL)
574194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
575194676Sthompsa
576195957Salfred	if (configuration < 1) {
577195957Salfred		/* unconfigure */
578195957Salfred		i = 255;
579195957Salfred	} else {
580195957Salfred		for (i = 0; i != 255; i++) {
581195957Salfred			uint8_t found;
582194676Sthompsa
583195957Salfred			pconf = libusb20_dev_alloc_config(pdev, i);
584195957Salfred			if (pconf == NULL)
585195957Salfred				return (LIBUSB_ERROR_INVALID_PARAM);
586195957Salfred			found = (pconf->desc.bConfigurationValue
587195957Salfred			    == configuration);
588195957Salfred			free(pconf);
589195957Salfred
590195957Salfred			if (found)
591195957Salfred				goto set_config;
592195957Salfred		}
593195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
594195957Salfred	}
595195957Salfred
596195957Salfredset_config:
597195957Salfred
598195957Salfred	libusb10_cancel_all_transfer(dev);
599195957Salfred
600195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
601195957Salfred
602195957Salfred	err = libusb20_dev_set_config_index(pdev, i);
603195957Salfred
604195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
605195957Salfred	    POLLOUT | POLLRDNORM | POLLWRNORM);
606195957Salfred
607195957Salfred	return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
608194676Sthompsa}
609194676Sthompsa
610194676Sthompsaint
611195957Salfredlibusb_claim_interface(struct libusb20_device *pdev, int interface_number)
612194676Sthompsa{
613195957Salfred	libusb_device *dev;
614194676Sthompsa
615195957Salfred	dev = libusb_get_device(pdev);
616194676Sthompsa	if (dev == NULL)
617194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
618194676Sthompsa
619195957Salfred	if (interface_number < 0 || interface_number > 31)
620194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
621194676Sthompsa
622195957Salfred	CTX_LOCK(dev->ctx);
623261224Shselasky	dev->claimed_interfaces |= (1 << interface_number);
624261224Shselasky	CTX_UNLOCK(dev->ctx);
625194676Sthompsa
626261224Shselasky	return (0);
627194676Sthompsa}
628194676Sthompsa
629194676Sthompsaint
630195957Salfredlibusb_release_interface(struct libusb20_device *pdev, int interface_number)
631194676Sthompsa{
632195957Salfred	libusb_device *dev;
633195957Salfred	int err = 0;
634194676Sthompsa
635195957Salfred	dev = libusb_get_device(pdev);
636194676Sthompsa	if (dev == NULL)
637194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
638194676Sthompsa
639195957Salfred	if (interface_number < 0 || interface_number > 31)
640194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
641194676Sthompsa
642195957Salfred	CTX_LOCK(dev->ctx);
643194676Sthompsa	if (!(dev->claimed_interfaces & (1 << interface_number)))
644195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
645194676Sthompsa
646195957Salfred	if (!err)
647194676Sthompsa		dev->claimed_interfaces &= ~(1 << interface_number);
648195957Salfred	CTX_UNLOCK(dev->ctx);
649195957Salfred	return (err);
650194676Sthompsa}
651194676Sthompsa
652194676Sthompsaint
653195957Salfredlibusb_set_interface_alt_setting(struct libusb20_device *pdev,
654194676Sthompsa    int interface_number, int alternate_setting)
655194676Sthompsa{
656195957Salfred	libusb_device *dev;
657195957Salfred	int err = 0;
658194676Sthompsa
659195957Salfred	dev = libusb_get_device(pdev);
660194676Sthompsa	if (dev == NULL)
661194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
662194676Sthompsa
663195957Salfred	if (interface_number < 0 || interface_number > 31)
664194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
665194676Sthompsa
666195957Salfred	CTX_LOCK(dev->ctx);
667195957Salfred	if (!(dev->claimed_interfaces & (1 << interface_number)))
668195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
669195957Salfred	CTX_UNLOCK(dev->ctx);
670194676Sthompsa
671195957Salfred	if (err)
672195957Salfred		return (err);
673195957Salfred
674195957Salfred	libusb10_cancel_all_transfer(dev);
675195957Salfred
676195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
677195957Salfred
678195957Salfred	err = libusb20_dev_set_alt_index(pdev,
679195957Salfred	    interface_number, alternate_setting);
680195957Salfred
681195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
682195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
683195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
684195957Salfred
685195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
686194676Sthompsa}
687194676Sthompsa
688195957Salfredstatic struct libusb20_transfer *
689195957Salfredlibusb10_get_transfer(struct libusb20_device *pdev,
690234491Shselasky    uint8_t endpoint, uint8_t xfer_index)
691195957Salfred{
692234491Shselasky	xfer_index &= 1;	/* double buffering */
693195957Salfred
694234491Shselasky	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
695195957Salfred
696195957Salfred	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
697195957Salfred		/* this is an IN endpoint */
698234491Shselasky		xfer_index |= 2;
699195957Salfred	}
700234491Shselasky	return (libusb20_tr_get_pointer(pdev, xfer_index));
701195957Salfred}
702195957Salfred
703194676Sthompsaint
704195957Salfredlibusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
705194676Sthompsa{
706194676Sthompsa	struct libusb20_transfer *xfer;
707195957Salfred	struct libusb_device *dev;
708195957Salfred	int err;
709194676Sthompsa
710195957Salfred	xfer = libusb10_get_transfer(pdev, endpoint, 0);
711195560Sthompsa	if (xfer == NULL)
712195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
713194676Sthompsa
714195957Salfred	dev = libusb_get_device(pdev);
715213853Shselasky	if (dev == NULL)
716213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
717195957Salfred
718195957Salfred	CTX_LOCK(dev->ctx);
719223642Shselasky	err = libusb20_tr_open(xfer, 0, 1, endpoint);
720195957Salfred	CTX_UNLOCK(dev->ctx);
721195957Salfred
722195957Salfred	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
723194676Sthompsa		return (LIBUSB_ERROR_OTHER);
724194676Sthompsa
725194676Sthompsa	libusb20_tr_clear_stall_sync(xfer);
726195957Salfred
727195957Salfred	/* check if we opened the transfer */
728195957Salfred	if (err == 0) {
729195957Salfred		CTX_LOCK(dev->ctx);
730194676Sthompsa		libusb20_tr_close(xfer);
731195957Salfred		CTX_UNLOCK(dev->ctx);
732195957Salfred	}
733195957Salfred	return (0);			/* success */
734194676Sthompsa}
735194676Sthompsa
736194676Sthompsaint
737195957Salfredlibusb_reset_device(struct libusb20_device *pdev)
738194676Sthompsa{
739195957Salfred	libusb_device *dev;
740195957Salfred	int err;
741194676Sthompsa
742195957Salfred	dev = libusb_get_device(pdev);
743194676Sthompsa	if (dev == NULL)
744213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
745194676Sthompsa
746195957Salfred	libusb10_cancel_all_transfer(dev);
747195957Salfred
748195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
749195957Salfred
750195957Salfred	err = libusb20_dev_reset(pdev);
751195957Salfred
752195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
753195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
754195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
755195957Salfred
756195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
757194676Sthompsa}
758194676Sthompsa
759194676Sthompsaint
760213848Shselaskylibusb_check_connected(struct libusb20_device *pdev)
761213848Shselasky{
762213848Shselasky	libusb_device *dev;
763213848Shselasky	int err;
764213848Shselasky
765213848Shselasky	dev = libusb_get_device(pdev);
766213848Shselasky	if (dev == NULL)
767213848Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
768213848Shselasky
769213848Shselasky	err = libusb20_dev_check_connected(pdev);
770213848Shselasky
771213848Shselasky	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
772213848Shselasky}
773213848Shselasky
774213848Shselaskyint
775195957Salfredlibusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
776194676Sthompsa{
777195957Salfred	if (pdev == NULL)
778194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
779194676Sthompsa
780226220Shselasky	if (libusb20_dev_kernel_driver_active(pdev, interface))
781226220Shselasky		return (0);		/* no kernel driver is active */
782226220Shselasky	else
783226220Shselasky		return (1);		/* kernel driver is active */
784194676Sthompsa}
785194676Sthompsa
786194676Sthompsaint
787213853Shselaskylibusb_get_driver_np(struct libusb20_device *pdev, int interface,
788213853Shselasky    char *name, int namelen)
789213853Shselasky{
790213853Shselasky	return (libusb_get_driver(pdev, interface, name, namelen));
791213853Shselasky}
792213853Shselasky
793213853Shselaskyint
794213853Shselaskylibusb_get_driver(struct libusb20_device *pdev, int interface,
795213853Shselasky    char *name, int namelen)
796213853Shselasky{
797213853Shselasky	char *ptr;
798213853Shselasky	int err;
799213853Shselasky
800213853Shselasky	if (pdev == NULL)
801213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
802213853Shselasky	if (namelen < 1)
803213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
804224085Shselasky	if (namelen > 255)
805224085Shselasky		namelen = 255;
806213853Shselasky
807213853Shselasky	err = libusb20_dev_get_iface_desc(
808213853Shselasky	    pdev, interface, name, namelen);
809213853Shselasky
810213853Shselasky	if (err != 0)
811213853Shselasky		return (LIBUSB_ERROR_OTHER);
812213853Shselasky
813213853Shselasky	/* we only want the driver name */
814213853Shselasky	ptr = strstr(name, ":");
815213853Shselasky	if (ptr != NULL)
816213853Shselasky		*ptr = 0;
817213853Shselasky
818213853Shselasky	return (0);
819213853Shselasky}
820213853Shselasky
821213853Shselaskyint
822213853Shselaskylibusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
823213853Shselasky{
824213853Shselasky	return (libusb_detach_kernel_driver(pdev, interface));
825213853Shselasky}
826213853Shselasky
827213853Shselaskyint
828195957Salfredlibusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
829194676Sthompsa{
830195957Salfred	int err;
831194676Sthompsa
832195957Salfred	if (pdev == NULL)
833194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
834194676Sthompsa
835195957Salfred	err = libusb20_dev_detach_kernel_driver(
836195957Salfred	    pdev, interface);
837194676Sthompsa
838213853Shselasky	return (err ? LIBUSB_ERROR_OTHER : 0);
839194676Sthompsa}
840194676Sthompsa
841194676Sthompsaint
842195957Salfredlibusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
843194676Sthompsa{
844195957Salfred	if (pdev == NULL)
845194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
846195957Salfred	/* stub - currently not supported by libusb20 */
847194676Sthompsa	return (0);
848194676Sthompsa}
849194676Sthompsa
850194676Sthompsa/* Asynchronous device I/O */
851194676Sthompsa
852194676Sthompsastruct libusb_transfer *
853194676Sthompsalibusb_alloc_transfer(int iso_packets)
854194676Sthompsa{
855195957Salfred	struct libusb_transfer *uxfer;
856195957Salfred	struct libusb_super_transfer *sxfer;
857194676Sthompsa	int len;
858194676Sthompsa
859194676Sthompsa	len = sizeof(struct libusb_transfer) +
860195957Salfred	    sizeof(struct libusb_super_transfer) +
861194676Sthompsa	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
862194676Sthompsa
863195957Salfred	sxfer = malloc(len);
864195957Salfred	if (sxfer == NULL)
865194676Sthompsa		return (NULL);
866194676Sthompsa
867195957Salfred	memset(sxfer, 0, len);
868194676Sthompsa
869195957Salfred	uxfer = (struct libusb_transfer *)(
870195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
871194676Sthompsa
872195957Salfred	/* set default value */
873195957Salfred	uxfer->num_iso_packets = iso_packets;
874195957Salfred
875195957Salfred	return (uxfer);
876194676Sthompsa}
877194676Sthompsa
878194676Sthompsavoid
879195957Salfredlibusb_free_transfer(struct libusb_transfer *uxfer)
880194676Sthompsa{
881195957Salfred	struct libusb_super_transfer *sxfer;
882194676Sthompsa
883195957Salfred	if (uxfer == NULL)
884195957Salfred		return;			/* be NULL safe */
885194676Sthompsa
886215253Shselasky	/* check if we should free the transfer buffer */
887215253Shselasky	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
888215253Shselasky		free(uxfer->buffer);
889215253Shselasky
890195957Salfred	sxfer = (struct libusb_super_transfer *)(
891195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
892194676Sthompsa
893195957Salfred	free(sxfer);
894194676Sthompsa}
895194676Sthompsa
896219100Shselaskystatic uint32_t
897195957Salfredlibusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
898195560Sthompsa{
899219100Shselasky	uint32_t ret;
900195560Sthompsa
901195560Sthompsa	switch (xfer->type) {
902195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
903219100Shselasky		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
904195957Salfred		break;
905195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
906195560Sthompsa		ret = 2;
907195957Salfred		break;
908195560Sthompsa	default:
909195560Sthompsa		ret = 1;
910195957Salfred		break;
911195560Sthompsa	}
912195957Salfred	return (ret);
913195560Sthompsa}
914195560Sthompsa
915195560Sthompsastatic int
916195957Salfredlibusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
917195560Sthompsa{
918195560Sthompsa	int ret;
919195560Sthompsa	int usb_speed;
920195560Sthompsa
921195560Sthompsa	usb_speed = libusb20_dev_get_speed(pdev);
922195560Sthompsa
923195560Sthompsa	switch (xfer->type) {
924195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
925195957Salfred		ret = 0;		/* kernel will auto-select */
926195957Salfred		break;
927195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
928195957Salfred		ret = 1024;
929195957Salfred		break;
930195957Salfred	default:
931195560Sthompsa		switch (usb_speed) {
932195957Salfred		case LIBUSB20_SPEED_LOW:
933195957Salfred			ret = 256;
934195957Salfred			break;
935195957Salfred		case LIBUSB20_SPEED_FULL:
936195957Salfred			ret = 4096;
937195957Salfred			break;
938195957Salfred		default:
939195957Salfred			ret = 16384;
940195957Salfred			break;
941195560Sthompsa		}
942195957Salfred		break;
943195560Sthompsa	}
944195957Salfred	return (ret);
945195560Sthompsa}
946195560Sthompsa
947195957Salfredstatic int
948195957Salfredlibusb10_convert_error(uint8_t status)
949195957Salfred{
950195957Salfred	;				/* indent fix */
951195957Salfred
952195957Salfred	switch (status) {
953195957Salfred	case LIBUSB20_TRANSFER_START:
954195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
955195957Salfred		return (LIBUSB_TRANSFER_COMPLETED);
956195957Salfred	case LIBUSB20_TRANSFER_OVERFLOW:
957195957Salfred		return (LIBUSB_TRANSFER_OVERFLOW);
958195957Salfred	case LIBUSB20_TRANSFER_NO_DEVICE:
959195957Salfred		return (LIBUSB_TRANSFER_NO_DEVICE);
960195957Salfred	case LIBUSB20_TRANSFER_STALL:
961195957Salfred		return (LIBUSB_TRANSFER_STALL);
962195957Salfred	case LIBUSB20_TRANSFER_CANCELLED:
963195957Salfred		return (LIBUSB_TRANSFER_CANCELLED);
964195957Salfred	case LIBUSB20_TRANSFER_TIMED_OUT:
965195957Salfred		return (LIBUSB_TRANSFER_TIMED_OUT);
966195957Salfred	default:
967195957Salfred		return (LIBUSB_TRANSFER_ERROR);
968195957Salfred	}
969195957Salfred}
970195957Salfred
971195957Salfred/* This function must be called locked */
972195957Salfred
973194676Sthompsastatic void
974195957Salfredlibusb10_complete_transfer(struct libusb20_transfer *pxfer,
975195957Salfred    struct libusb_super_transfer *sxfer, int status)
976194676Sthompsa{
977195957Salfred	struct libusb_transfer *uxfer;
978195957Salfred	struct libusb_device *dev;
979195957Salfred
980195957Salfred	uxfer = (struct libusb_transfer *)(
981195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
982195957Salfred
983195957Salfred	if (pxfer != NULL)
984195957Salfred		libusb20_tr_set_priv_sc1(pxfer, NULL);
985195957Salfred
986199575Sthompsa	/* set transfer status */
987195957Salfred	uxfer->status = status;
988195957Salfred
989199575Sthompsa	/* update super transfer state */
990199575Sthompsa	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
991199575Sthompsa
992195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
993195957Salfred
994195957Salfred	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
995195957Salfred}
996195957Salfred
997195957Salfred/* This function must be called locked */
998195957Salfred
999195957Salfredstatic void
1000195957Salfredlibusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1001195957Salfred{
1002195957Salfred	struct libusb_super_transfer *sxfer;
1003195957Salfred	struct libusb_transfer *uxfer;
1004195957Salfred	uint32_t actlen;
1005195957Salfred	uint16_t iso_packets;
1006195957Salfred	uint16_t i;
1007194676Sthompsa	uint8_t status;
1008195957Salfred	uint8_t flags;
1009194676Sthompsa
1010195957Salfred	status = libusb20_tr_get_status(pxfer);
1011195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1012195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1013195957Salfred	iso_packets = libusb20_tr_get_max_frames(pxfer);
1014194676Sthompsa
1015195957Salfred	if (sxfer == NULL)
1016195957Salfred		return;			/* cancelled - nothing to do */
1017195957Salfred
1018195957Salfred	uxfer = (struct libusb_transfer *)(
1019195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1020195957Salfred
1021195957Salfred	if (iso_packets > uxfer->num_iso_packets)
1022195957Salfred		iso_packets = uxfer->num_iso_packets;
1023195957Salfred
1024195957Salfred	if (iso_packets == 0)
1025195957Salfred		return;			/* nothing to do */
1026195957Salfred
1027195957Salfred	/* make sure that the number of ISOCHRONOUS packets is valid */
1028195957Salfred	uxfer->num_iso_packets = iso_packets;
1029195957Salfred
1030195957Salfred	flags = uxfer->flags;
1031195957Salfred
1032194676Sthompsa	switch (status) {
1033194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1034195560Sthompsa
1035195957Salfred		/* update actual length */
1036195957Salfred		uxfer->actual_length = actlen;
1037195957Salfred		for (i = 0; i != iso_packets; i++) {
1038195957Salfred			uxfer->iso_packet_desc[i].actual_length =
1039195957Salfred			    libusb20_tr_get_length(pxfer, i);
1040195957Salfred		}
1041195957Salfred		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1042195957Salfred		break;
1043195560Sthompsa
1044194676Sthompsa	case LIBUSB20_TRANSFER_START:
1045195957Salfred
1046195957Salfred		/* setup length(s) */
1047195957Salfred		actlen = 0;
1048195957Salfred		for (i = 0; i != iso_packets; i++) {
1049195957Salfred			libusb20_tr_setup_isoc(pxfer,
1050195957Salfred			    &uxfer->buffer[actlen],
1051195957Salfred			    uxfer->iso_packet_desc[i].length, i);
1052195957Salfred			actlen += uxfer->iso_packet_desc[i].length;
1053194676Sthompsa		}
1054195957Salfred
1055195957Salfred		/* no remainder */
1056195957Salfred		sxfer->rem_len = 0;
1057195957Salfred
1058195957Salfred		libusb20_tr_set_total_frames(pxfer, iso_packets);
1059195957Salfred		libusb20_tr_submit(pxfer);
1060195957Salfred
1061195957Salfred		/* fork another USB transfer, if any */
1062195957Salfred		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1063195957Salfred		break;
1064195957Salfred
1065194676Sthompsa	default:
1066195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1067195957Salfred		break;
1068194676Sthompsa	}
1069195957Salfred}
1070194676Sthompsa
1071195957Salfred/* This function must be called locked */
1072195957Salfred
1073195957Salfredstatic void
1074195957Salfredlibusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1075195957Salfred{
1076195957Salfred	struct libusb_super_transfer *sxfer;
1077195957Salfred	struct libusb_transfer *uxfer;
1078195957Salfred	uint32_t max_bulk;
1079195957Salfred	uint32_t actlen;
1080195957Salfred	uint8_t status;
1081195957Salfred	uint8_t flags;
1082195957Salfred
1083195957Salfred	status = libusb20_tr_get_status(pxfer);
1084195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1085195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1086195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1087195957Salfred
1088195957Salfred	if (sxfer == NULL)
1089195957Salfred		return;			/* cancelled - nothing to do */
1090195957Salfred
1091195957Salfred	uxfer = (struct libusb_transfer *)(
1092195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1093195957Salfred
1094195957Salfred	flags = uxfer->flags;
1095195957Salfred
1096194676Sthompsa	switch (status) {
1097194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1098195957Salfred
1099195957Salfred		uxfer->actual_length += actlen;
1100195957Salfred
1101195957Salfred		/* check for short packet */
1102195957Salfred		if (sxfer->last_len != actlen) {
1103195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1104195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1105195957Salfred			} else {
1106195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1107195957Salfred			}
1108195957Salfred			break;
1109195957Salfred		}
1110195957Salfred		/* check for end of data */
1111195957Salfred		if (sxfer->rem_len == 0) {
1112195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1113195957Salfred			break;
1114195957Salfred		}
1115195957Salfred		/* FALLTHROUGH */
1116195957Salfred
1117195957Salfred	case LIBUSB20_TRANSFER_START:
1118195957Salfred		if (max_bulk > sxfer->rem_len) {
1119195957Salfred			max_bulk = sxfer->rem_len;
1120195957Salfred		}
1121195957Salfred		/* setup new BULK or INTERRUPT transaction */
1122195957Salfred		libusb20_tr_setup_bulk(pxfer,
1123195957Salfred		    sxfer->curr_data, max_bulk, uxfer->timeout);
1124195957Salfred
1125195957Salfred		/* update counters */
1126195957Salfred		sxfer->last_len = max_bulk;
1127195957Salfred		sxfer->curr_data += max_bulk;
1128195957Salfred		sxfer->rem_len -= max_bulk;
1129195957Salfred
1130195957Salfred		libusb20_tr_submit(pxfer);
1131195957Salfred
1132195957Salfred		/* check if we can fork another USB transfer */
1133195957Salfred		if (sxfer->rem_len == 0)
1134195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1135195957Salfred		break;
1136195957Salfred
1137195957Salfred	default:
1138195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1139195957Salfred		break;
1140194676Sthompsa	}
1141194676Sthompsa}
1142194676Sthompsa
1143195957Salfred/* This function must be called locked */
1144195957Salfred
1145195957Salfredstatic void
1146195957Salfredlibusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1147194676Sthompsa{
1148195957Salfred	struct libusb_super_transfer *sxfer;
1149195957Salfred	struct libusb_transfer *uxfer;
1150195957Salfred	uint32_t max_bulk;
1151195957Salfred	uint32_t actlen;
1152195957Salfred	uint8_t status;
1153195957Salfred	uint8_t flags;
1154194676Sthompsa
1155195957Salfred	status = libusb20_tr_get_status(pxfer);
1156195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1157195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1158195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1159194676Sthompsa
1160195957Salfred	if (sxfer == NULL)
1161195957Salfred		return;			/* cancelled - nothing to do */
1162194676Sthompsa
1163195957Salfred	uxfer = (struct libusb_transfer *)(
1164195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1165194676Sthompsa
1166195957Salfred	flags = uxfer->flags;
1167194676Sthompsa
1168195957Salfred	switch (status) {
1169195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
1170194676Sthompsa
1171195957Salfred		uxfer->actual_length += actlen;
1172195957Salfred
1173195957Salfred		/* subtract length of SETUP packet, if any */
1174195957Salfred		actlen -= libusb20_tr_get_length(pxfer, 0);
1175195957Salfred
1176195957Salfred		/* check for short packet */
1177195957Salfred		if (sxfer->last_len != actlen) {
1178195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1179195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1180195957Salfred			} else {
1181195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1182195957Salfred			}
1183195957Salfred			break;
1184194676Sthompsa		}
1185195957Salfred		/* check for end of data */
1186195957Salfred		if (sxfer->rem_len == 0) {
1187195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1188195957Salfred			break;
1189195957Salfred		}
1190195957Salfred		/* FALLTHROUGH */
1191194676Sthompsa
1192195957Salfred	case LIBUSB20_TRANSFER_START:
1193195957Salfred		if (max_bulk > sxfer->rem_len) {
1194195957Salfred			max_bulk = sxfer->rem_len;
1195195957Salfred		}
1196195957Salfred		/* setup new CONTROL transaction */
1197195957Salfred		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1198195957Salfred			/* next fragment - don't send SETUP packet */
1199195957Salfred			libusb20_tr_set_length(pxfer, 0, 0);
1200195957Salfred		} else {
1201195957Salfred			/* first fragment - send SETUP packet */
1202195957Salfred			libusb20_tr_set_length(pxfer, 8, 0);
1203195957Salfred			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1204195957Salfred		}
1205195957Salfred
1206195957Salfred		if (max_bulk != 0) {
1207195957Salfred			libusb20_tr_set_length(pxfer, max_bulk, 1);
1208195957Salfred			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1209195957Salfred			libusb20_tr_set_total_frames(pxfer, 2);
1210195957Salfred		} else {
1211195957Salfred			libusb20_tr_set_total_frames(pxfer, 1);
1212195957Salfred		}
1213195957Salfred
1214195957Salfred		/* update counters */
1215195957Salfred		sxfer->last_len = max_bulk;
1216195957Salfred		sxfer->curr_data += max_bulk;
1217195957Salfred		sxfer->rem_len -= max_bulk;
1218195957Salfred
1219195957Salfred		libusb20_tr_submit(pxfer);
1220195957Salfred
1221195957Salfred		/* check if we can fork another USB transfer */
1222195957Salfred		if (sxfer->rem_len == 0)
1223195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1224195957Salfred		break;
1225195957Salfred
1226195957Salfred	default:
1227195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1228195957Salfred		break;
1229194676Sthompsa	}
1230195957Salfred}
1231195957Salfred
1232195957Salfred/* The following function must be called locked */
1233195957Salfred
1234195957Salfredstatic void
1235195957Salfredlibusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1236195957Salfred{
1237195957Salfred	struct libusb20_transfer *pxfer0;
1238195957Salfred	struct libusb20_transfer *pxfer1;
1239195957Salfred	struct libusb_super_transfer *sxfer;
1240195957Salfred	struct libusb_transfer *uxfer;
1241195957Salfred	struct libusb_device *dev;
1242195957Salfred	int err;
1243195957Salfred	int buffsize;
1244195957Salfred	int maxframe;
1245195957Salfred	int temp;
1246195957Salfred	uint8_t dummy;
1247195957Salfred
1248195957Salfred	dev = libusb_get_device(pdev);
1249195957Salfred
1250195957Salfred	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1251195957Salfred	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1252195957Salfred
1253195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL)
1254195957Salfred		return;			/* shouldn't happen */
1255195957Salfred
1256195957Salfred	temp = 0;
1257195957Salfred	if (libusb20_tr_pending(pxfer0))
1258195957Salfred		temp |= 1;
1259195957Salfred	if (libusb20_tr_pending(pxfer1))
1260195957Salfred		temp |= 2;
1261195957Salfred
1262195957Salfred	switch (temp) {
1263195957Salfred	case 3:
1264195957Salfred		/* wait till one of the transfers complete */
1265195957Salfred		return;
1266195957Salfred	case 2:
1267195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1268199575Sthompsa		if (sxfer == NULL)
1269199575Sthompsa			return;		/* cancelling */
1270195957Salfred		if (sxfer->rem_len)
1271195957Salfred			return;		/* cannot queue another one */
1272195957Salfred		/* swap transfers */
1273195957Salfred		pxfer1 = pxfer0;
1274195957Salfred		break;
1275195957Salfred	case 1:
1276195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1277199575Sthompsa		if (sxfer == NULL)
1278199575Sthompsa			return;		/* cancelling */
1279195957Salfred		if (sxfer->rem_len)
1280195957Salfred			return;		/* cannot queue another one */
1281195957Salfred		/* swap transfers */
1282195957Salfred		pxfer0 = pxfer1;
1283195957Salfred		break;
1284195957Salfred	default:
1285195957Salfred		break;
1286194676Sthompsa	}
1287195957Salfred
1288195957Salfred	/* find next transfer on same endpoint */
1289195957Salfred	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1290195957Salfred
1291195957Salfred		uxfer = (struct libusb_transfer *)(
1292195957Salfred		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1293195957Salfred
1294195957Salfred		if (uxfer->endpoint == endpoint) {
1295195957Salfred			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1296195957Salfred			sxfer->entry.tqe_prev = NULL;
1297195957Salfred			goto found;
1298194676Sthompsa		}
1299195957Salfred	}
1300195957Salfred	return;				/* success */
1301194676Sthompsa
1302195957Salfredfound:
1303194676Sthompsa
1304195957Salfred	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1305195957Salfred	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1306194676Sthompsa
1307195957Salfred	/* reset super transfer state */
1308195957Salfred	sxfer->rem_len = uxfer->length;
1309195957Salfred	sxfer->curr_data = uxfer->buffer;
1310195957Salfred	uxfer->actual_length = 0;
1311194676Sthompsa
1312195957Salfred	switch (uxfer->type) {
1313195957Salfred	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1314195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1315195957Salfred		break;
1316195957Salfred	case LIBUSB_TRANSFER_TYPE_BULK:
1317195957Salfred	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1318195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1319195957Salfred		break;
1320195957Salfred	case LIBUSB_TRANSFER_TYPE_CONTROL:
1321195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1322195957Salfred		if (sxfer->rem_len < 8)
1323195957Salfred			goto failure;
1324194676Sthompsa
1325195957Salfred		/* remove SETUP packet from data */
1326195957Salfred		sxfer->rem_len -= 8;
1327195957Salfred		sxfer->curr_data += 8;
1328195957Salfred		break;
1329195957Salfred	default:
1330195957Salfred		goto failure;
1331195560Sthompsa	}
1332195957Salfred
1333195957Salfred	buffsize = libusb10_get_buffsize(pdev, uxfer);
1334195957Salfred	maxframe = libusb10_get_maxframe(pdev, uxfer);
1335195957Salfred
1336195957Salfred	/* make sure the transfer is opened */
1337195957Salfred	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1338195957Salfred	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1339195957Salfred		goto failure;
1340194676Sthompsa	}
1341195957Salfred	libusb20_tr_start(pxfer0);
1342195957Salfred	return;
1343194676Sthompsa
1344195957Salfredfailure:
1345195957Salfred	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1346194676Sthompsa
1347195957Salfred	/* make sure our event loop spins the done handler */
1348195957Salfred	dummy = 0;
1349248236Shselasky	err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1350195957Salfred}
1351194676Sthompsa
1352195957Salfred/* The following function must be called unlocked */
1353194676Sthompsa
1354195957Salfredint
1355195957Salfredlibusb_submit_transfer(struct libusb_transfer *uxfer)
1356195957Salfred{
1357195957Salfred	struct libusb20_transfer *pxfer0;
1358195957Salfred	struct libusb20_transfer *pxfer1;
1359195957Salfred	struct libusb_super_transfer *sxfer;
1360195957Salfred	struct libusb_device *dev;
1361234684Shselasky	uint8_t endpoint;
1362195957Salfred	int err;
1363195957Salfred
1364195957Salfred	if (uxfer == NULL)
1365195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1366195957Salfred
1367195957Salfred	if (uxfer->dev_handle == NULL)
1368195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1369195957Salfred
1370195957Salfred	endpoint = uxfer->endpoint;
1371195957Salfred
1372195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1373195957Salfred
1374195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1375195957Salfred
1376195957Salfred	sxfer = (struct libusb_super_transfer *)(
1377195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1378195957Salfred
1379195957Salfred	CTX_LOCK(dev->ctx);
1380195957Salfred
1381195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1382195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1383195957Salfred
1384195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL) {
1385195957Salfred		err = LIBUSB_ERROR_OTHER;
1386195957Salfred	} else if ((sxfer->entry.tqe_prev != NULL) ||
1387199575Sthompsa	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1388195957Salfred	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1389195957Salfred		err = LIBUSB_ERROR_BUSY;
1390195957Salfred	} else {
1391199575Sthompsa
1392199575Sthompsa		/* set pending state */
1393199575Sthompsa		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1394199575Sthompsa
1395199575Sthompsa		/* insert transfer into transfer head list */
1396195957Salfred		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1397195957Salfred
1398199575Sthompsa		/* start work transfers */
1399195957Salfred		libusb10_submit_transfer_sub(
1400195957Salfred		    uxfer->dev_handle, endpoint);
1401195957Salfred
1402195957Salfred		err = 0;		/* success */
1403195957Salfred	}
1404195957Salfred
1405195957Salfred	CTX_UNLOCK(dev->ctx);
1406195957Salfred
1407195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1408195957Salfred
1409195957Salfred	return (err);
1410194676Sthompsa}
1411194676Sthompsa
1412195957Salfred/* Asynchronous transfer cancel */
1413195957Salfred
1414194676Sthompsaint
1415195957Salfredlibusb_cancel_transfer(struct libusb_transfer *uxfer)
1416194676Sthompsa{
1417195957Salfred	struct libusb20_transfer *pxfer0;
1418195957Salfred	struct libusb20_transfer *pxfer1;
1419195957Salfred	struct libusb_super_transfer *sxfer;
1420195957Salfred	struct libusb_device *dev;
1421234684Shselasky	uint8_t endpoint;
1422199575Sthompsa	int retval;
1423194676Sthompsa
1424195957Salfred	if (uxfer == NULL)
1425195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1426194676Sthompsa
1427199575Sthompsa	/* check if not initialised */
1428195957Salfred	if (uxfer->dev_handle == NULL)
1429199575Sthompsa		return (LIBUSB_ERROR_NOT_FOUND);
1430194676Sthompsa
1431195957Salfred	endpoint = uxfer->endpoint;
1432194676Sthompsa
1433195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1434195957Salfred
1435195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1436195957Salfred
1437195957Salfred	sxfer = (struct libusb_super_transfer *)(
1438195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1439195957Salfred
1440199575Sthompsa	retval = 0;
1441199575Sthompsa
1442195957Salfred	CTX_LOCK(dev->ctx);
1443195957Salfred
1444195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1445195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1446195957Salfred
1447199575Sthompsa	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1448199575Sthompsa		/* only update the transfer status */
1449199575Sthompsa		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1450199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1451199575Sthompsa	} else if (sxfer->entry.tqe_prev != NULL) {
1452195957Salfred		/* we are lucky - transfer is on a queue */
1453195957Salfred		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1454195957Salfred		sxfer->entry.tqe_prev = NULL;
1455199575Sthompsa		libusb10_complete_transfer(NULL,
1456199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1457195957Salfred	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1458195957Salfred		/* not started */
1459199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1460195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1461199575Sthompsa		libusb10_complete_transfer(pxfer0,
1462199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1463195957Salfred		libusb20_tr_stop(pxfer0);
1464195957Salfred		/* make sure the queue doesn't stall */
1465195957Salfred		libusb10_submit_transfer_sub(
1466195957Salfred		    uxfer->dev_handle, endpoint);
1467195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1468199575Sthompsa		libusb10_complete_transfer(pxfer1,
1469199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1470195957Salfred		libusb20_tr_stop(pxfer1);
1471195957Salfred		/* make sure the queue doesn't stall */
1472195957Salfred		libusb10_submit_transfer_sub(
1473195957Salfred		    uxfer->dev_handle, endpoint);
1474195957Salfred	} else {
1475195957Salfred		/* not started */
1476199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1477195957Salfred	}
1478195957Salfred
1479195957Salfred	CTX_UNLOCK(dev->ctx);
1480195957Salfred
1481195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1482195957Salfred
1483199575Sthompsa	return (retval);
1484194676Sthompsa}
1485194676Sthompsa
1486195957SalfredUNEXPORTED void
1487195957Salfredlibusb10_cancel_all_transfer(libusb_device *dev)
1488195957Salfred{
1489195957Salfred	/* TODO */
1490195957Salfred}
1491199055Sthompsa
1492199055Sthompsauint16_t
1493199055Sthompsalibusb_cpu_to_le16(uint16_t x)
1494199055Sthompsa{
1495199055Sthompsa	return (htole16(x));
1496199055Sthompsa}
1497199055Sthompsa
1498199055Sthompsauint16_t
1499199055Sthompsalibusb_le16_to_cpu(uint16_t x)
1500199055Sthompsa{
1501199055Sthompsa	return (le16toh(x));
1502199055Sthompsa}
1503199055Sthompsa
1504213853Shselaskyconst char *
1505213853Shselaskylibusb_strerror(int code)
1506213853Shselasky{
1507225659Shselasky	switch (code) {
1508225659Shselasky	case LIBUSB_SUCCESS:
1509225659Shselasky		return ("Success");
1510225659Shselasky	case LIBUSB_ERROR_IO:
1511225659Shselasky		return ("I/O error");
1512225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1513225659Shselasky		return ("Invalid parameter");
1514225659Shselasky	case LIBUSB_ERROR_ACCESS:
1515225659Shselasky		return ("Permissions error");
1516225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1517225659Shselasky		return ("No device");
1518225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1519225659Shselasky		return ("Not found");
1520225659Shselasky	case LIBUSB_ERROR_BUSY:
1521225659Shselasky		return ("Device busy");
1522225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1523225659Shselasky		return ("Timeout");
1524225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1525225659Shselasky		return ("Overflow");
1526225659Shselasky	case LIBUSB_ERROR_PIPE:
1527225659Shselasky		return ("Pipe error");
1528225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1529225659Shselasky		return ("Interrupted");
1530225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1531225659Shselasky		return ("Out of memory");
1532225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1533225659Shselasky		return ("Not supported");
1534225659Shselasky	case LIBUSB_ERROR_OTHER:
1535225659Shselasky		return ("Other error");
1536225659Shselasky	default:
1537225659Shselasky		return ("Unknown error");
1538225659Shselasky	}
1539213853Shselasky}
1540225659Shselasky
1541225659Shselaskyconst char *
1542225659Shselaskylibusb_error_name(int code)
1543225659Shselasky{
1544225659Shselasky	switch (code) {
1545225659Shselasky	case LIBUSB_SUCCESS:
1546225659Shselasky		return ("LIBUSB_SUCCESS");
1547225659Shselasky	case LIBUSB_ERROR_IO:
1548225659Shselasky		return ("LIBUSB_ERROR_IO");
1549225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1550225659Shselasky		return ("LIBUSB_ERROR_INVALID_PARAM");
1551225659Shselasky	case LIBUSB_ERROR_ACCESS:
1552225659Shselasky		return ("LIBUSB_ERROR_ACCESS");
1553225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1554225659Shselasky		return ("LIBUSB_ERROR_NO_DEVICE");
1555225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1556225659Shselasky		return ("LIBUSB_ERROR_NOT_FOUND");
1557225659Shselasky	case LIBUSB_ERROR_BUSY:
1558225659Shselasky		return ("LIBUSB_ERROR_BUSY");
1559225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1560225659Shselasky		return ("LIBUSB_ERROR_TIMEOUT");
1561225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1562225659Shselasky		return ("LIBUSB_ERROR_OVERFLOW");
1563225659Shselasky	case LIBUSB_ERROR_PIPE:
1564225659Shselasky		return ("LIBUSB_ERROR_PIPE");
1565225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1566225659Shselasky		return ("LIBUSB_ERROR_INTERRUPTED");
1567225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1568225659Shselasky		return ("LIBUSB_ERROR_NO_MEM");
1569225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1570225659Shselasky		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1571225659Shselasky	case LIBUSB_ERROR_OTHER:
1572225659Shselasky		return ("LIBUSB_ERROR_OTHER");
1573225659Shselasky	default:
1574225659Shselasky		return ("LIBUSB_ERROR_UNKNOWN");
1575225659Shselasky	}
1576225659Shselasky}
1577