1194676Sthompsa/* $FreeBSD$ */
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;
614195957Salfred	int err = 0;
615194676Sthompsa
616195957Salfred	dev = libusb_get_device(pdev);
617194676Sthompsa	if (dev == NULL)
618194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
619194676Sthompsa
620195957Salfred	if (interface_number < 0 || interface_number > 31)
621194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
622194676Sthompsa
623195957Salfred	CTX_LOCK(dev->ctx);
624194676Sthompsa	if (dev->claimed_interfaces & (1 << interface_number))
625195957Salfred		err = LIBUSB_ERROR_BUSY;
626194676Sthompsa
627195957Salfred	if (!err)
628194676Sthompsa		dev->claimed_interfaces |= (1 << interface_number);
629195957Salfred	CTX_UNLOCK(dev->ctx);
630195957Salfred	return (err);
631194676Sthompsa}
632194676Sthompsa
633194676Sthompsaint
634195957Salfredlibusb_release_interface(struct libusb20_device *pdev, int interface_number)
635194676Sthompsa{
636195957Salfred	libusb_device *dev;
637195957Salfred	int err = 0;
638194676Sthompsa
639195957Salfred	dev = libusb_get_device(pdev);
640194676Sthompsa	if (dev == NULL)
641194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
642194676Sthompsa
643195957Salfred	if (interface_number < 0 || interface_number > 31)
644194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
645194676Sthompsa
646195957Salfred	CTX_LOCK(dev->ctx);
647194676Sthompsa	if (!(dev->claimed_interfaces & (1 << interface_number)))
648195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
649194676Sthompsa
650195957Salfred	if (!err)
651194676Sthompsa		dev->claimed_interfaces &= ~(1 << interface_number);
652195957Salfred	CTX_UNLOCK(dev->ctx);
653195957Salfred	return (err);
654194676Sthompsa}
655194676Sthompsa
656194676Sthompsaint
657195957Salfredlibusb_set_interface_alt_setting(struct libusb20_device *pdev,
658194676Sthompsa    int interface_number, int alternate_setting)
659194676Sthompsa{
660195957Salfred	libusb_device *dev;
661195957Salfred	int err = 0;
662194676Sthompsa
663195957Salfred	dev = libusb_get_device(pdev);
664194676Sthompsa	if (dev == NULL)
665194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
666194676Sthompsa
667195957Salfred	if (interface_number < 0 || interface_number > 31)
668194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
669194676Sthompsa
670195957Salfred	CTX_LOCK(dev->ctx);
671195957Salfred	if (!(dev->claimed_interfaces & (1 << interface_number)))
672195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
673195957Salfred	CTX_UNLOCK(dev->ctx);
674194676Sthompsa
675195957Salfred	if (err)
676195957Salfred		return (err);
677195957Salfred
678195957Salfred	libusb10_cancel_all_transfer(dev);
679195957Salfred
680195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
681195957Salfred
682195957Salfred	err = libusb20_dev_set_alt_index(pdev,
683195957Salfred	    interface_number, alternate_setting);
684195957Salfred
685195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
686195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
687195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
688195957Salfred
689195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
690194676Sthompsa}
691194676Sthompsa
692195957Salfredstatic struct libusb20_transfer *
693195957Salfredlibusb10_get_transfer(struct libusb20_device *pdev,
694234491Shselasky    uint8_t endpoint, uint8_t xfer_index)
695195957Salfred{
696234491Shselasky	xfer_index &= 1;	/* double buffering */
697195957Salfred
698234491Shselasky	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
699195957Salfred
700195957Salfred	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
701195957Salfred		/* this is an IN endpoint */
702234491Shselasky		xfer_index |= 2;
703195957Salfred	}
704234491Shselasky	return (libusb20_tr_get_pointer(pdev, xfer_index));
705195957Salfred}
706195957Salfred
707194676Sthompsaint
708195957Salfredlibusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
709194676Sthompsa{
710194676Sthompsa	struct libusb20_transfer *xfer;
711195957Salfred	struct libusb_device *dev;
712195957Salfred	int err;
713194676Sthompsa
714195957Salfred	xfer = libusb10_get_transfer(pdev, endpoint, 0);
715195560Sthompsa	if (xfer == NULL)
716195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
717194676Sthompsa
718195957Salfred	dev = libusb_get_device(pdev);
719213853Shselasky	if (dev == NULL)
720213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
721195957Salfred
722195957Salfred	CTX_LOCK(dev->ctx);
723223642Shselasky	err = libusb20_tr_open(xfer, 0, 1, endpoint);
724195957Salfred	CTX_UNLOCK(dev->ctx);
725195957Salfred
726195957Salfred	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
727194676Sthompsa		return (LIBUSB_ERROR_OTHER);
728194676Sthompsa
729194676Sthompsa	libusb20_tr_clear_stall_sync(xfer);
730195957Salfred
731195957Salfred	/* check if we opened the transfer */
732195957Salfred	if (err == 0) {
733195957Salfred		CTX_LOCK(dev->ctx);
734194676Sthompsa		libusb20_tr_close(xfer);
735195957Salfred		CTX_UNLOCK(dev->ctx);
736195957Salfred	}
737195957Salfred	return (0);			/* success */
738194676Sthompsa}
739194676Sthompsa
740194676Sthompsaint
741195957Salfredlibusb_reset_device(struct libusb20_device *pdev)
742194676Sthompsa{
743195957Salfred	libusb_device *dev;
744195957Salfred	int err;
745194676Sthompsa
746195957Salfred	dev = libusb_get_device(pdev);
747194676Sthompsa	if (dev == NULL)
748213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
749194676Sthompsa
750195957Salfred	libusb10_cancel_all_transfer(dev);
751195957Salfred
752195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
753195957Salfred
754195957Salfred	err = libusb20_dev_reset(pdev);
755195957Salfred
756195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
757195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
758195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
759195957Salfred
760195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
761194676Sthompsa}
762194676Sthompsa
763194676Sthompsaint
764213848Shselaskylibusb_check_connected(struct libusb20_device *pdev)
765213848Shselasky{
766213848Shselasky	libusb_device *dev;
767213848Shselasky	int err;
768213848Shselasky
769213848Shselasky	dev = libusb_get_device(pdev);
770213848Shselasky	if (dev == NULL)
771213848Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
772213848Shselasky
773213848Shselasky	err = libusb20_dev_check_connected(pdev);
774213848Shselasky
775213848Shselasky	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
776213848Shselasky}
777213848Shselasky
778213848Shselaskyint
779195957Salfredlibusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
780194676Sthompsa{
781195957Salfred	if (pdev == NULL)
782194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
783194676Sthompsa
784226220Shselasky	if (libusb20_dev_kernel_driver_active(pdev, interface))
785226220Shselasky		return (0);		/* no kernel driver is active */
786226220Shselasky	else
787226220Shselasky		return (1);		/* kernel driver is active */
788194676Sthompsa}
789194676Sthompsa
790194676Sthompsaint
791213853Shselaskylibusb_get_driver_np(struct libusb20_device *pdev, int interface,
792213853Shselasky    char *name, int namelen)
793213853Shselasky{
794213853Shselasky	return (libusb_get_driver(pdev, interface, name, namelen));
795213853Shselasky}
796213853Shselasky
797213853Shselaskyint
798213853Shselaskylibusb_get_driver(struct libusb20_device *pdev, int interface,
799213853Shselasky    char *name, int namelen)
800213853Shselasky{
801213853Shselasky	char *ptr;
802213853Shselasky	int err;
803213853Shselasky
804213853Shselasky	if (pdev == NULL)
805213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
806213853Shselasky	if (namelen < 1)
807213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
808224085Shselasky	if (namelen > 255)
809224085Shselasky		namelen = 255;
810213853Shselasky
811213853Shselasky	err = libusb20_dev_get_iface_desc(
812213853Shselasky	    pdev, interface, name, namelen);
813213853Shselasky
814213853Shselasky	if (err != 0)
815213853Shselasky		return (LIBUSB_ERROR_OTHER);
816213853Shselasky
817213853Shselasky	/* we only want the driver name */
818213853Shselasky	ptr = strstr(name, ":");
819213853Shselasky	if (ptr != NULL)
820213853Shselasky		*ptr = 0;
821213853Shselasky
822213853Shselasky	return (0);
823213853Shselasky}
824213853Shselasky
825213853Shselaskyint
826213853Shselaskylibusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
827213853Shselasky{
828213853Shselasky	return (libusb_detach_kernel_driver(pdev, interface));
829213853Shselasky}
830213853Shselasky
831213853Shselaskyint
832195957Salfredlibusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
833194676Sthompsa{
834195957Salfred	int err;
835194676Sthompsa
836195957Salfred	if (pdev == NULL)
837194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
838194676Sthompsa
839195957Salfred	err = libusb20_dev_detach_kernel_driver(
840195957Salfred	    pdev, interface);
841194676Sthompsa
842213853Shselasky	return (err ? LIBUSB_ERROR_OTHER : 0);
843194676Sthompsa}
844194676Sthompsa
845194676Sthompsaint
846195957Salfredlibusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
847194676Sthompsa{
848195957Salfred	if (pdev == NULL)
849194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
850195957Salfred	/* stub - currently not supported by libusb20 */
851194676Sthompsa	return (0);
852194676Sthompsa}
853194676Sthompsa
854194676Sthompsa/* Asynchronous device I/O */
855194676Sthompsa
856194676Sthompsastruct libusb_transfer *
857194676Sthompsalibusb_alloc_transfer(int iso_packets)
858194676Sthompsa{
859195957Salfred	struct libusb_transfer *uxfer;
860195957Salfred	struct libusb_super_transfer *sxfer;
861194676Sthompsa	int len;
862194676Sthompsa
863194676Sthompsa	len = sizeof(struct libusb_transfer) +
864195957Salfred	    sizeof(struct libusb_super_transfer) +
865194676Sthompsa	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
866194676Sthompsa
867195957Salfred	sxfer = malloc(len);
868195957Salfred	if (sxfer == NULL)
869194676Sthompsa		return (NULL);
870194676Sthompsa
871195957Salfred	memset(sxfer, 0, len);
872194676Sthompsa
873195957Salfred	uxfer = (struct libusb_transfer *)(
874195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
875194676Sthompsa
876195957Salfred	/* set default value */
877195957Salfred	uxfer->num_iso_packets = iso_packets;
878195957Salfred
879195957Salfred	return (uxfer);
880194676Sthompsa}
881194676Sthompsa
882194676Sthompsavoid
883195957Salfredlibusb_free_transfer(struct libusb_transfer *uxfer)
884194676Sthompsa{
885195957Salfred	struct libusb_super_transfer *sxfer;
886194676Sthompsa
887195957Salfred	if (uxfer == NULL)
888195957Salfred		return;			/* be NULL safe */
889194676Sthompsa
890215253Shselasky	/* check if we should free the transfer buffer */
891215253Shselasky	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
892215253Shselasky		free(uxfer->buffer);
893215253Shselasky
894195957Salfred	sxfer = (struct libusb_super_transfer *)(
895195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
896194676Sthompsa
897195957Salfred	free(sxfer);
898194676Sthompsa}
899194676Sthompsa
900219100Shselaskystatic uint32_t
901195957Salfredlibusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
902195560Sthompsa{
903219100Shselasky	uint32_t ret;
904195560Sthompsa
905195560Sthompsa	switch (xfer->type) {
906195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
907219100Shselasky		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
908195957Salfred		break;
909195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
910195560Sthompsa		ret = 2;
911195957Salfred		break;
912195560Sthompsa	default:
913195560Sthompsa		ret = 1;
914195957Salfred		break;
915195560Sthompsa	}
916195957Salfred	return (ret);
917195560Sthompsa}
918195560Sthompsa
919195560Sthompsastatic int
920195957Salfredlibusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
921195560Sthompsa{
922195560Sthompsa	int ret;
923195560Sthompsa	int usb_speed;
924195560Sthompsa
925195560Sthompsa	usb_speed = libusb20_dev_get_speed(pdev);
926195560Sthompsa
927195560Sthompsa	switch (xfer->type) {
928195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
929195957Salfred		ret = 0;		/* kernel will auto-select */
930195957Salfred		break;
931195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
932195957Salfred		ret = 1024;
933195957Salfred		break;
934195957Salfred	default:
935195560Sthompsa		switch (usb_speed) {
936195957Salfred		case LIBUSB20_SPEED_LOW:
937195957Salfred			ret = 256;
938195957Salfred			break;
939195957Salfred		case LIBUSB20_SPEED_FULL:
940195957Salfred			ret = 4096;
941195957Salfred			break;
942195957Salfred		default:
943195957Salfred			ret = 16384;
944195957Salfred			break;
945195560Sthompsa		}
946195957Salfred		break;
947195560Sthompsa	}
948195957Salfred	return (ret);
949195560Sthompsa}
950195560Sthompsa
951195957Salfredstatic int
952195957Salfredlibusb10_convert_error(uint8_t status)
953195957Salfred{
954195957Salfred	;				/* indent fix */
955195957Salfred
956195957Salfred	switch (status) {
957195957Salfred	case LIBUSB20_TRANSFER_START:
958195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
959195957Salfred		return (LIBUSB_TRANSFER_COMPLETED);
960195957Salfred	case LIBUSB20_TRANSFER_OVERFLOW:
961195957Salfred		return (LIBUSB_TRANSFER_OVERFLOW);
962195957Salfred	case LIBUSB20_TRANSFER_NO_DEVICE:
963195957Salfred		return (LIBUSB_TRANSFER_NO_DEVICE);
964195957Salfred	case LIBUSB20_TRANSFER_STALL:
965195957Salfred		return (LIBUSB_TRANSFER_STALL);
966195957Salfred	case LIBUSB20_TRANSFER_CANCELLED:
967195957Salfred		return (LIBUSB_TRANSFER_CANCELLED);
968195957Salfred	case LIBUSB20_TRANSFER_TIMED_OUT:
969195957Salfred		return (LIBUSB_TRANSFER_TIMED_OUT);
970195957Salfred	default:
971195957Salfred		return (LIBUSB_TRANSFER_ERROR);
972195957Salfred	}
973195957Salfred}
974195957Salfred
975195957Salfred/* This function must be called locked */
976195957Salfred
977194676Sthompsastatic void
978195957Salfredlibusb10_complete_transfer(struct libusb20_transfer *pxfer,
979195957Salfred    struct libusb_super_transfer *sxfer, int status)
980194676Sthompsa{
981195957Salfred	struct libusb_transfer *uxfer;
982195957Salfred	struct libusb_device *dev;
983195957Salfred
984195957Salfred	uxfer = (struct libusb_transfer *)(
985195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
986195957Salfred
987195957Salfred	if (pxfer != NULL)
988195957Salfred		libusb20_tr_set_priv_sc1(pxfer, NULL);
989195957Salfred
990199575Sthompsa	/* set transfer status */
991195957Salfred	uxfer->status = status;
992195957Salfred
993199575Sthompsa	/* update super transfer state */
994199575Sthompsa	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
995199575Sthompsa
996195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
997195957Salfred
998195957Salfred	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
999195957Salfred}
1000195957Salfred
1001195957Salfred/* This function must be called locked */
1002195957Salfred
1003195957Salfredstatic void
1004195957Salfredlibusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1005195957Salfred{
1006195957Salfred	struct libusb_super_transfer *sxfer;
1007195957Salfred	struct libusb_transfer *uxfer;
1008195957Salfred	uint32_t actlen;
1009195957Salfred	uint16_t iso_packets;
1010195957Salfred	uint16_t i;
1011194676Sthompsa	uint8_t status;
1012195957Salfred	uint8_t flags;
1013194676Sthompsa
1014195957Salfred	status = libusb20_tr_get_status(pxfer);
1015195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1016195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1017195957Salfred	iso_packets = libusb20_tr_get_max_frames(pxfer);
1018194676Sthompsa
1019195957Salfred	if (sxfer == NULL)
1020195957Salfred		return;			/* cancelled - nothing to do */
1021195957Salfred
1022195957Salfred	uxfer = (struct libusb_transfer *)(
1023195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1024195957Salfred
1025195957Salfred	if (iso_packets > uxfer->num_iso_packets)
1026195957Salfred		iso_packets = uxfer->num_iso_packets;
1027195957Salfred
1028195957Salfred	if (iso_packets == 0)
1029195957Salfred		return;			/* nothing to do */
1030195957Salfred
1031195957Salfred	/* make sure that the number of ISOCHRONOUS packets is valid */
1032195957Salfred	uxfer->num_iso_packets = iso_packets;
1033195957Salfred
1034195957Salfred	flags = uxfer->flags;
1035195957Salfred
1036194676Sthompsa	switch (status) {
1037194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1038195560Sthompsa
1039195957Salfred		/* update actual length */
1040195957Salfred		uxfer->actual_length = actlen;
1041195957Salfred		for (i = 0; i != iso_packets; i++) {
1042195957Salfred			uxfer->iso_packet_desc[i].actual_length =
1043195957Salfred			    libusb20_tr_get_length(pxfer, i);
1044195957Salfred		}
1045195957Salfred		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1046195957Salfred		break;
1047195560Sthompsa
1048194676Sthompsa	case LIBUSB20_TRANSFER_START:
1049195957Salfred
1050195957Salfred		/* setup length(s) */
1051195957Salfred		actlen = 0;
1052195957Salfred		for (i = 0; i != iso_packets; i++) {
1053195957Salfred			libusb20_tr_setup_isoc(pxfer,
1054195957Salfred			    &uxfer->buffer[actlen],
1055195957Salfred			    uxfer->iso_packet_desc[i].length, i);
1056195957Salfred			actlen += uxfer->iso_packet_desc[i].length;
1057194676Sthompsa		}
1058195957Salfred
1059195957Salfred		/* no remainder */
1060195957Salfred		sxfer->rem_len = 0;
1061195957Salfred
1062195957Salfred		libusb20_tr_set_total_frames(pxfer, iso_packets);
1063195957Salfred		libusb20_tr_submit(pxfer);
1064195957Salfred
1065195957Salfred		/* fork another USB transfer, if any */
1066195957Salfred		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1067195957Salfred		break;
1068195957Salfred
1069194676Sthompsa	default:
1070195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1071195957Salfred		break;
1072194676Sthompsa	}
1073195957Salfred}
1074194676Sthompsa
1075195957Salfred/* This function must be called locked */
1076195957Salfred
1077195957Salfredstatic void
1078195957Salfredlibusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1079195957Salfred{
1080195957Salfred	struct libusb_super_transfer *sxfer;
1081195957Salfred	struct libusb_transfer *uxfer;
1082195957Salfred	uint32_t max_bulk;
1083195957Salfred	uint32_t actlen;
1084195957Salfred	uint8_t status;
1085195957Salfred	uint8_t flags;
1086195957Salfred
1087195957Salfred	status = libusb20_tr_get_status(pxfer);
1088195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1089195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1090195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1091195957Salfred
1092195957Salfred	if (sxfer == NULL)
1093195957Salfred		return;			/* cancelled - nothing to do */
1094195957Salfred
1095195957Salfred	uxfer = (struct libusb_transfer *)(
1096195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1097195957Salfred
1098195957Salfred	flags = uxfer->flags;
1099195957Salfred
1100194676Sthompsa	switch (status) {
1101194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1102195957Salfred
1103195957Salfred		uxfer->actual_length += actlen;
1104195957Salfred
1105195957Salfred		/* check for short packet */
1106195957Salfred		if (sxfer->last_len != actlen) {
1107195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1108195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1109195957Salfred			} else {
1110195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1111195957Salfred			}
1112195957Salfred			break;
1113195957Salfred		}
1114195957Salfred		/* check for end of data */
1115195957Salfred		if (sxfer->rem_len == 0) {
1116195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1117195957Salfred			break;
1118195957Salfred		}
1119195957Salfred		/* FALLTHROUGH */
1120195957Salfred
1121195957Salfred	case LIBUSB20_TRANSFER_START:
1122195957Salfred		if (max_bulk > sxfer->rem_len) {
1123195957Salfred			max_bulk = sxfer->rem_len;
1124195957Salfred		}
1125195957Salfred		/* setup new BULK or INTERRUPT transaction */
1126195957Salfred		libusb20_tr_setup_bulk(pxfer,
1127195957Salfred		    sxfer->curr_data, max_bulk, uxfer->timeout);
1128195957Salfred
1129195957Salfred		/* update counters */
1130195957Salfred		sxfer->last_len = max_bulk;
1131195957Salfred		sxfer->curr_data += max_bulk;
1132195957Salfred		sxfer->rem_len -= max_bulk;
1133195957Salfred
1134195957Salfred		libusb20_tr_submit(pxfer);
1135195957Salfred
1136195957Salfred		/* check if we can fork another USB transfer */
1137195957Salfred		if (sxfer->rem_len == 0)
1138195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1139195957Salfred		break;
1140195957Salfred
1141195957Salfred	default:
1142195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1143195957Salfred		break;
1144194676Sthompsa	}
1145194676Sthompsa}
1146194676Sthompsa
1147195957Salfred/* This function must be called locked */
1148195957Salfred
1149195957Salfredstatic void
1150195957Salfredlibusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1151194676Sthompsa{
1152195957Salfred	struct libusb_super_transfer *sxfer;
1153195957Salfred	struct libusb_transfer *uxfer;
1154195957Salfred	uint32_t max_bulk;
1155195957Salfred	uint32_t actlen;
1156195957Salfred	uint8_t status;
1157195957Salfred	uint8_t flags;
1158194676Sthompsa
1159195957Salfred	status = libusb20_tr_get_status(pxfer);
1160195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1161195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1162195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1163194676Sthompsa
1164195957Salfred	if (sxfer == NULL)
1165195957Salfred		return;			/* cancelled - nothing to do */
1166194676Sthompsa
1167195957Salfred	uxfer = (struct libusb_transfer *)(
1168195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1169194676Sthompsa
1170195957Salfred	flags = uxfer->flags;
1171194676Sthompsa
1172195957Salfred	switch (status) {
1173195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
1174194676Sthompsa
1175195957Salfred		uxfer->actual_length += actlen;
1176195957Salfred
1177195957Salfred		/* subtract length of SETUP packet, if any */
1178195957Salfred		actlen -= libusb20_tr_get_length(pxfer, 0);
1179195957Salfred
1180195957Salfred		/* check for short packet */
1181195957Salfred		if (sxfer->last_len != actlen) {
1182195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1183195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1184195957Salfred			} else {
1185195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1186195957Salfred			}
1187195957Salfred			break;
1188194676Sthompsa		}
1189195957Salfred		/* check for end of data */
1190195957Salfred		if (sxfer->rem_len == 0) {
1191195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1192195957Salfred			break;
1193195957Salfred		}
1194195957Salfred		/* FALLTHROUGH */
1195194676Sthompsa
1196195957Salfred	case LIBUSB20_TRANSFER_START:
1197195957Salfred		if (max_bulk > sxfer->rem_len) {
1198195957Salfred			max_bulk = sxfer->rem_len;
1199195957Salfred		}
1200195957Salfred		/* setup new CONTROL transaction */
1201195957Salfred		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1202195957Salfred			/* next fragment - don't send SETUP packet */
1203195957Salfred			libusb20_tr_set_length(pxfer, 0, 0);
1204195957Salfred		} else {
1205195957Salfred			/* first fragment - send SETUP packet */
1206195957Salfred			libusb20_tr_set_length(pxfer, 8, 0);
1207195957Salfred			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1208195957Salfred		}
1209195957Salfred
1210195957Salfred		if (max_bulk != 0) {
1211195957Salfred			libusb20_tr_set_length(pxfer, max_bulk, 1);
1212195957Salfred			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1213195957Salfred			libusb20_tr_set_total_frames(pxfer, 2);
1214195957Salfred		} else {
1215195957Salfred			libusb20_tr_set_total_frames(pxfer, 1);
1216195957Salfred		}
1217195957Salfred
1218195957Salfred		/* update counters */
1219195957Salfred		sxfer->last_len = max_bulk;
1220195957Salfred		sxfer->curr_data += max_bulk;
1221195957Salfred		sxfer->rem_len -= max_bulk;
1222195957Salfred
1223195957Salfred		libusb20_tr_submit(pxfer);
1224195957Salfred
1225195957Salfred		/* check if we can fork another USB transfer */
1226195957Salfred		if (sxfer->rem_len == 0)
1227195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1228195957Salfred		break;
1229195957Salfred
1230195957Salfred	default:
1231195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1232195957Salfred		break;
1233194676Sthompsa	}
1234195957Salfred}
1235195957Salfred
1236195957Salfred/* The following function must be called locked */
1237195957Salfred
1238195957Salfredstatic void
1239195957Salfredlibusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1240195957Salfred{
1241195957Salfred	struct libusb20_transfer *pxfer0;
1242195957Salfred	struct libusb20_transfer *pxfer1;
1243195957Salfred	struct libusb_super_transfer *sxfer;
1244195957Salfred	struct libusb_transfer *uxfer;
1245195957Salfred	struct libusb_device *dev;
1246195957Salfred	int err;
1247195957Salfred	int buffsize;
1248195957Salfred	int maxframe;
1249195957Salfred	int temp;
1250195957Salfred	uint8_t dummy;
1251195957Salfred
1252195957Salfred	dev = libusb_get_device(pdev);
1253195957Salfred
1254195957Salfred	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1255195957Salfred	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1256195957Salfred
1257195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL)
1258195957Salfred		return;			/* shouldn't happen */
1259195957Salfred
1260195957Salfred	temp = 0;
1261195957Salfred	if (libusb20_tr_pending(pxfer0))
1262195957Salfred		temp |= 1;
1263195957Salfred	if (libusb20_tr_pending(pxfer1))
1264195957Salfred		temp |= 2;
1265195957Salfred
1266195957Salfred	switch (temp) {
1267195957Salfred	case 3:
1268195957Salfred		/* wait till one of the transfers complete */
1269195957Salfred		return;
1270195957Salfred	case 2:
1271195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1272199575Sthompsa		if (sxfer == NULL)
1273199575Sthompsa			return;		/* cancelling */
1274195957Salfred		if (sxfer->rem_len)
1275195957Salfred			return;		/* cannot queue another one */
1276195957Salfred		/* swap transfers */
1277195957Salfred		pxfer1 = pxfer0;
1278195957Salfred		break;
1279195957Salfred	case 1:
1280195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1281199575Sthompsa		if (sxfer == NULL)
1282199575Sthompsa			return;		/* cancelling */
1283195957Salfred		if (sxfer->rem_len)
1284195957Salfred			return;		/* cannot queue another one */
1285195957Salfred		/* swap transfers */
1286195957Salfred		pxfer0 = pxfer1;
1287195957Salfred		break;
1288195957Salfred	default:
1289195957Salfred		break;
1290194676Sthompsa	}
1291195957Salfred
1292195957Salfred	/* find next transfer on same endpoint */
1293195957Salfred	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1294195957Salfred
1295195957Salfred		uxfer = (struct libusb_transfer *)(
1296195957Salfred		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1297195957Salfred
1298195957Salfred		if (uxfer->endpoint == endpoint) {
1299195957Salfred			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1300195957Salfred			sxfer->entry.tqe_prev = NULL;
1301195957Salfred			goto found;
1302194676Sthompsa		}
1303195957Salfred	}
1304195957Salfred	return;				/* success */
1305194676Sthompsa
1306195957Salfredfound:
1307194676Sthompsa
1308195957Salfred	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1309195957Salfred	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1310194676Sthompsa
1311195957Salfred	/* reset super transfer state */
1312195957Salfred	sxfer->rem_len = uxfer->length;
1313195957Salfred	sxfer->curr_data = uxfer->buffer;
1314195957Salfred	uxfer->actual_length = 0;
1315194676Sthompsa
1316195957Salfred	switch (uxfer->type) {
1317195957Salfred	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1318195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1319195957Salfred		break;
1320195957Salfred	case LIBUSB_TRANSFER_TYPE_BULK:
1321195957Salfred	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1322195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1323195957Salfred		break;
1324195957Salfred	case LIBUSB_TRANSFER_TYPE_CONTROL:
1325195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1326195957Salfred		if (sxfer->rem_len < 8)
1327195957Salfred			goto failure;
1328194676Sthompsa
1329195957Salfred		/* remove SETUP packet from data */
1330195957Salfred		sxfer->rem_len -= 8;
1331195957Salfred		sxfer->curr_data += 8;
1332195957Salfred		break;
1333195957Salfred	default:
1334195957Salfred		goto failure;
1335195560Sthompsa	}
1336195957Salfred
1337195957Salfred	buffsize = libusb10_get_buffsize(pdev, uxfer);
1338195957Salfred	maxframe = libusb10_get_maxframe(pdev, uxfer);
1339195957Salfred
1340195957Salfred	/* make sure the transfer is opened */
1341195957Salfred	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1342195957Salfred	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1343195957Salfred		goto failure;
1344194676Sthompsa	}
1345195957Salfred	libusb20_tr_start(pxfer0);
1346195957Salfred	return;
1347194676Sthompsa
1348195957Salfredfailure:
1349195957Salfred	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1350194676Sthompsa
1351195957Salfred	/* make sure our event loop spins the done handler */
1352195957Salfred	dummy = 0;
1353248236Shselasky	err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1354195957Salfred}
1355194676Sthompsa
1356195957Salfred/* The following function must be called unlocked */
1357194676Sthompsa
1358195957Salfredint
1359195957Salfredlibusb_submit_transfer(struct libusb_transfer *uxfer)
1360195957Salfred{
1361195957Salfred	struct libusb20_transfer *pxfer0;
1362195957Salfred	struct libusb20_transfer *pxfer1;
1363195957Salfred	struct libusb_super_transfer *sxfer;
1364195957Salfred	struct libusb_device *dev;
1365234684Shselasky	uint8_t endpoint;
1366195957Salfred	int err;
1367195957Salfred
1368195957Salfred	if (uxfer == NULL)
1369195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1370195957Salfred
1371195957Salfred	if (uxfer->dev_handle == NULL)
1372195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1373195957Salfred
1374195957Salfred	endpoint = uxfer->endpoint;
1375195957Salfred
1376195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1377195957Salfred
1378195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1379195957Salfred
1380195957Salfred	sxfer = (struct libusb_super_transfer *)(
1381195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1382195957Salfred
1383195957Salfred	CTX_LOCK(dev->ctx);
1384195957Salfred
1385195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1386195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1387195957Salfred
1388195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL) {
1389195957Salfred		err = LIBUSB_ERROR_OTHER;
1390195957Salfred	} else if ((sxfer->entry.tqe_prev != NULL) ||
1391199575Sthompsa	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1392195957Salfred	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1393195957Salfred		err = LIBUSB_ERROR_BUSY;
1394195957Salfred	} else {
1395199575Sthompsa
1396199575Sthompsa		/* set pending state */
1397199575Sthompsa		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1398199575Sthompsa
1399199575Sthompsa		/* insert transfer into transfer head list */
1400195957Salfred		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1401195957Salfred
1402199575Sthompsa		/* start work transfers */
1403195957Salfred		libusb10_submit_transfer_sub(
1404195957Salfred		    uxfer->dev_handle, endpoint);
1405195957Salfred
1406195957Salfred		err = 0;		/* success */
1407195957Salfred	}
1408195957Salfred
1409195957Salfred	CTX_UNLOCK(dev->ctx);
1410195957Salfred
1411195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1412195957Salfred
1413195957Salfred	return (err);
1414194676Sthompsa}
1415194676Sthompsa
1416195957Salfred/* Asynchronous transfer cancel */
1417195957Salfred
1418194676Sthompsaint
1419195957Salfredlibusb_cancel_transfer(struct libusb_transfer *uxfer)
1420194676Sthompsa{
1421195957Salfred	struct libusb20_transfer *pxfer0;
1422195957Salfred	struct libusb20_transfer *pxfer1;
1423195957Salfred	struct libusb_super_transfer *sxfer;
1424195957Salfred	struct libusb_device *dev;
1425234684Shselasky	uint8_t endpoint;
1426199575Sthompsa	int retval;
1427194676Sthompsa
1428195957Salfred	if (uxfer == NULL)
1429195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1430194676Sthompsa
1431199575Sthompsa	/* check if not initialised */
1432195957Salfred	if (uxfer->dev_handle == NULL)
1433199575Sthompsa		return (LIBUSB_ERROR_NOT_FOUND);
1434194676Sthompsa
1435195957Salfred	endpoint = uxfer->endpoint;
1436194676Sthompsa
1437195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1438195957Salfred
1439195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1440195957Salfred
1441195957Salfred	sxfer = (struct libusb_super_transfer *)(
1442195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1443195957Salfred
1444199575Sthompsa	retval = 0;
1445199575Sthompsa
1446195957Salfred	CTX_LOCK(dev->ctx);
1447195957Salfred
1448195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1449195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1450195957Salfred
1451199575Sthompsa	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1452199575Sthompsa		/* only update the transfer status */
1453199575Sthompsa		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1454199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1455199575Sthompsa	} else if (sxfer->entry.tqe_prev != NULL) {
1456195957Salfred		/* we are lucky - transfer is on a queue */
1457195957Salfred		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1458195957Salfred		sxfer->entry.tqe_prev = NULL;
1459199575Sthompsa		libusb10_complete_transfer(NULL,
1460199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1461195957Salfred	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1462195957Salfred		/* not started */
1463199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1464195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1465199575Sthompsa		libusb10_complete_transfer(pxfer0,
1466199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1467195957Salfred		libusb20_tr_stop(pxfer0);
1468195957Salfred		/* make sure the queue doesn't stall */
1469195957Salfred		libusb10_submit_transfer_sub(
1470195957Salfred		    uxfer->dev_handle, endpoint);
1471195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1472199575Sthompsa		libusb10_complete_transfer(pxfer1,
1473199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1474195957Salfred		libusb20_tr_stop(pxfer1);
1475195957Salfred		/* make sure the queue doesn't stall */
1476195957Salfred		libusb10_submit_transfer_sub(
1477195957Salfred		    uxfer->dev_handle, endpoint);
1478195957Salfred	} else {
1479195957Salfred		/* not started */
1480199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1481195957Salfred	}
1482195957Salfred
1483195957Salfred	CTX_UNLOCK(dev->ctx);
1484195957Salfred
1485195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1486195957Salfred
1487199575Sthompsa	return (retval);
1488194676Sthompsa}
1489194676Sthompsa
1490195957SalfredUNEXPORTED void
1491195957Salfredlibusb10_cancel_all_transfer(libusb_device *dev)
1492195957Salfred{
1493195957Salfred	/* TODO */
1494195957Salfred}
1495199055Sthompsa
1496199055Sthompsauint16_t
1497199055Sthompsalibusb_cpu_to_le16(uint16_t x)
1498199055Sthompsa{
1499199055Sthompsa	return (htole16(x));
1500199055Sthompsa}
1501199055Sthompsa
1502199055Sthompsauint16_t
1503199055Sthompsalibusb_le16_to_cpu(uint16_t x)
1504199055Sthompsa{
1505199055Sthompsa	return (le16toh(x));
1506199055Sthompsa}
1507199055Sthompsa
1508213853Shselaskyconst char *
1509213853Shselaskylibusb_strerror(int code)
1510213853Shselasky{
1511225659Shselasky	switch (code) {
1512225659Shselasky	case LIBUSB_SUCCESS:
1513225659Shselasky		return ("Success");
1514225659Shselasky	case LIBUSB_ERROR_IO:
1515225659Shselasky		return ("I/O error");
1516225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1517225659Shselasky		return ("Invalid parameter");
1518225659Shselasky	case LIBUSB_ERROR_ACCESS:
1519225659Shselasky		return ("Permissions error");
1520225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1521225659Shselasky		return ("No device");
1522225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1523225659Shselasky		return ("Not found");
1524225659Shselasky	case LIBUSB_ERROR_BUSY:
1525225659Shselasky		return ("Device busy");
1526225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1527225659Shselasky		return ("Timeout");
1528225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1529225659Shselasky		return ("Overflow");
1530225659Shselasky	case LIBUSB_ERROR_PIPE:
1531225659Shselasky		return ("Pipe error");
1532225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1533225659Shselasky		return ("Interrupted");
1534225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1535225659Shselasky		return ("Out of memory");
1536225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1537225659Shselasky		return ("Not supported");
1538225659Shselasky	case LIBUSB_ERROR_OTHER:
1539225659Shselasky		return ("Other error");
1540225659Shselasky	default:
1541225659Shselasky		return ("Unknown error");
1542225659Shselasky	}
1543213853Shselasky}
1544225659Shselasky
1545225659Shselaskyconst char *
1546225659Shselaskylibusb_error_name(int code)
1547225659Shselasky{
1548225659Shselasky	switch (code) {
1549225659Shselasky	case LIBUSB_SUCCESS:
1550225659Shselasky		return ("LIBUSB_SUCCESS");
1551225659Shselasky	case LIBUSB_ERROR_IO:
1552225659Shselasky		return ("LIBUSB_ERROR_IO");
1553225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1554225659Shselasky		return ("LIBUSB_ERROR_INVALID_PARAM");
1555225659Shselasky	case LIBUSB_ERROR_ACCESS:
1556225659Shselasky		return ("LIBUSB_ERROR_ACCESS");
1557225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1558225659Shselasky		return ("LIBUSB_ERROR_NO_DEVICE");
1559225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1560225659Shselasky		return ("LIBUSB_ERROR_NOT_FOUND");
1561225659Shselasky	case LIBUSB_ERROR_BUSY:
1562225659Shselasky		return ("LIBUSB_ERROR_BUSY");
1563225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1564225659Shselasky		return ("LIBUSB_ERROR_TIMEOUT");
1565225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1566225659Shselasky		return ("LIBUSB_ERROR_OVERFLOW");
1567225659Shselasky	case LIBUSB_ERROR_PIPE:
1568225659Shselasky		return ("LIBUSB_ERROR_PIPE");
1569225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1570225659Shselasky		return ("LIBUSB_ERROR_INTERRUPTED");
1571225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1572225659Shselasky		return ("LIBUSB_ERROR_NO_MEM");
1573225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1574225659Shselasky		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1575225659Shselasky	case LIBUSB_ERROR_OTHER:
1576225659Shselasky		return ("LIBUSB_ERROR_OTHER");
1577225659Shselasky	default:
1578225659Shselasky		return ("LIBUSB_ERROR_UNKNOWN");
1579225659Shselasky	}
1580225659Shselasky}
1581