libusb10.c revision 250342
1194676Sthompsa/* $FreeBSD: head/lib/libusb/libusb10.c 250342 2013-05-08 00:55:29Z emaste $ */
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
294250342Semastelibusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
295250342Semaste    uint8_t bufsize)
296250342Semaste{
297250342Semaste	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
298250342Semaste}
299250342Semaste
300194676Sthompsauint8_t
301195957Salfredlibusb_get_device_address(libusb_device *dev)
302194676Sthompsa{
303194676Sthompsa	if (dev == NULL)
304195957Salfred		return (0);		/* should not happen */
305195957Salfred	return (libusb20_dev_get_address(dev->os_priv));
306194676Sthompsa}
307194676Sthompsa
308224903Shselaskyenum libusb_speed
309224903Shselaskylibusb_get_device_speed(libusb_device *dev)
310224903Shselasky{
311224903Shselasky	if (dev == NULL)
312225035Shselasky		return (LIBUSB_SPEED_UNKNOWN);	/* should not happen */
313224903Shselasky
314224903Shselasky	switch (libusb20_dev_get_speed(dev->os_priv)) {
315224903Shselasky	case LIBUSB20_SPEED_LOW:
316224903Shselasky		return (LIBUSB_SPEED_LOW);
317224903Shselasky	case LIBUSB20_SPEED_FULL:
318224903Shselasky		return (LIBUSB_SPEED_FULL);
319224903Shselasky	case LIBUSB20_SPEED_HIGH:
320224903Shselasky		return (LIBUSB_SPEED_HIGH);
321224903Shselasky	case LIBUSB20_SPEED_SUPER:
322224903Shselasky		return (LIBUSB_SPEED_SUPER);
323224903Shselasky	default:
324224903Shselasky		break;
325224903Shselasky	}
326224903Shselasky	return (LIBUSB_SPEED_UNKNOWN);
327224903Shselasky}
328224903Shselasky
329194676Sthompsaint
330195957Salfredlibusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
331194676Sthompsa{
332194676Sthompsa	struct libusb_config_descriptor *pdconf;
333194676Sthompsa	struct libusb_interface *pinf;
334194676Sthompsa	struct libusb_interface_descriptor *pdinf;
335194676Sthompsa	struct libusb_endpoint_descriptor *pdend;
336195957Salfred	int i;
337195957Salfred	int j;
338195957Salfred	int k;
339195957Salfred	int ret;
340194676Sthompsa
341194676Sthompsa	if (dev == NULL)
342194676Sthompsa		return (LIBUSB_ERROR_NO_DEVICE);
343194676Sthompsa
344195957Salfred	ret = libusb_get_active_config_descriptor(dev, &pdconf);
345195957Salfred	if (ret < 0)
346195957Salfred		return (ret);
347195957Salfred
348194676Sthompsa	ret = LIBUSB_ERROR_NOT_FOUND;
349195957Salfred	for (i = 0; i < pdconf->bNumInterfaces; i++) {
350194676Sthompsa		pinf = &pdconf->interface[i];
351195957Salfred		for (j = 0; j < pinf->num_altsetting; j++) {
352194676Sthompsa			pdinf = &pinf->altsetting[j];
353195957Salfred			for (k = 0; k < pdinf->bNumEndpoints; k++) {
354194676Sthompsa				pdend = &pdinf->endpoint[k];
355194676Sthompsa				if (pdend->bEndpointAddress == endpoint) {
356194676Sthompsa					ret = pdend->wMaxPacketSize;
357194676Sthompsa					goto out;
358194676Sthompsa				}
359194676Sthompsa			}
360194676Sthompsa		}
361194676Sthompsa	}
362194676Sthompsa
363194676Sthompsaout:
364194676Sthompsa	libusb_free_config_descriptor(pdconf);
365194676Sthompsa	return (ret);
366194676Sthompsa}
367194676Sthompsa
368234193Shselaskyint
369234193Shselaskylibusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
370234193Shselasky{
371234193Shselasky	int multiplier;
372234193Shselasky	int ret;
373234193Shselasky
374234193Shselasky	ret = libusb_get_max_packet_size(dev, endpoint);
375234193Shselasky
376234193Shselasky	switch (libusb20_dev_get_speed(dev->os_priv)) {
377234193Shselasky	case LIBUSB20_SPEED_LOW:
378234193Shselasky	case LIBUSB20_SPEED_FULL:
379234193Shselasky		break;
380234193Shselasky	default:
381234193Shselasky		if (ret > -1) {
382234193Shselasky			multiplier = (1 + ((ret >> 11) & 3));
383234193Shselasky			if (multiplier > 3)
384234193Shselasky				multiplier = 3;
385234193Shselasky			ret = (ret & 0x7FF) * multiplier;
386234193Shselasky		}
387234193Shselasky		break;
388234193Shselasky	}
389234193Shselasky	return (ret);
390234193Shselasky}
391234193Shselasky
392194676Sthompsalibusb_device *
393195957Salfredlibusb_ref_device(libusb_device *dev)
394194676Sthompsa{
395194676Sthompsa	if (dev == NULL)
396195957Salfred		return (NULL);		/* be NULL safe */
397194676Sthompsa
398195957Salfred	CTX_LOCK(dev->ctx);
399194676Sthompsa	dev->refcnt++;
400195957Salfred	CTX_UNLOCK(dev->ctx);
401194676Sthompsa
402194676Sthompsa	return (dev);
403194676Sthompsa}
404194676Sthompsa
405194676Sthompsavoid
406195957Salfredlibusb_unref_device(libusb_device *dev)
407194676Sthompsa{
408194676Sthompsa	if (dev == NULL)
409195957Salfred		return;			/* be NULL safe */
410194676Sthompsa
411195957Salfred	CTX_LOCK(dev->ctx);
412194676Sthompsa	dev->refcnt--;
413195957Salfred	CTX_UNLOCK(dev->ctx);
414194676Sthompsa
415194676Sthompsa	if (dev->refcnt == 0) {
416194676Sthompsa		libusb20_dev_free(dev->os_priv);
417194676Sthompsa		free(dev);
418194676Sthompsa	}
419194676Sthompsa}
420194676Sthompsa
421194676Sthompsaint
422195957Salfredlibusb_open(libusb_device *dev, libusb_device_handle **devh)
423194676Sthompsa{
424194676Sthompsa	libusb_context *ctx = dev->ctx;
425194676Sthompsa	struct libusb20_device *pdev = dev->os_priv;
426195957Salfred	uint8_t dummy;
427194676Sthompsa	int err;
428194676Sthompsa
429194676Sthompsa	if (devh == NULL)
430194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
431194676Sthompsa
432195957Salfred	/* set default device handle value */
433195957Salfred	*devh = NULL;
434194676Sthompsa
435195957Salfred	dev = libusb_ref_device(dev);
436195957Salfred	if (dev == NULL)
437195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
438195957Salfred
439194676Sthompsa	err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
440194676Sthompsa	if (err) {
441195957Salfred		libusb_unref_device(dev);
442194676Sthompsa		return (LIBUSB_ERROR_NO_MEM);
443194676Sthompsa	}
444195957Salfred	libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
445194676Sthompsa	    POLLOUT | POLLRDNORM | POLLWRNORM);
446194676Sthompsa
447195957Salfred	/* make sure our event loop detects the new device */
448195957Salfred	dummy = 0;
449194676Sthompsa	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
450213853Shselasky	if (err < (int)sizeof(dummy)) {
451195957Salfred		/* ignore error, if any */
452195957Salfred		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
453194676Sthompsa	}
454195957Salfred	*devh = pdev;
455194676Sthompsa
456194676Sthompsa	return (0);
457194676Sthompsa}
458194676Sthompsa
459194676Sthompsalibusb_device_handle *
460195957Salfredlibusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
461194676Sthompsa    uint16_t product_id)
462194676Sthompsa{
463194676Sthompsa	struct libusb_device **devs;
464194676Sthompsa	struct libusb20_device *pdev;
465194676Sthompsa	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
466195957Salfred	int i;
467195957Salfred	int j;
468194676Sthompsa
469195957Salfred	ctx = GET_CONTEXT(ctx);
470195957Salfred	if (ctx == NULL)
471195957Salfred		return (NULL);		/* be NULL safe */
472195957Salfred
473195560Sthompsa	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
474194676Sthompsa
475194676Sthompsa	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
476194676Sthompsa		return (NULL);
477194676Sthompsa
478228236Shselasky	pdev = NULL;
479194676Sthompsa	for (j = 0; j < i; j++) {
480228236Shselasky		struct libusb20_device *tdev;
481228236Shselasky
482228236Shselasky		tdev = devs[j]->os_priv;
483228236Shselasky		pdesc = libusb20_dev_get_device_desc(tdev);
484195957Salfred		/*
485195957Salfred		 * NOTE: The USB library will automatically swap the
486195957Salfred		 * fields in the device descriptor to be of host
487195957Salfred		 * endian type!
488195957Salfred		 */
489194676Sthompsa		if (pdesc->idVendor == vendor_id &&
490195560Sthompsa		    pdesc->idProduct == product_id) {
491228235Shselasky			libusb_open(devs[j], &pdev);
492195957Salfred			break;
493195560Sthompsa		}
494194676Sthompsa	}
495194676Sthompsa
496194676Sthompsa	libusb_free_device_list(devs, 1);
497195560Sthompsa	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
498195957Salfred	return (pdev);
499194676Sthompsa}
500194676Sthompsa
501194676Sthompsavoid
502195957Salfredlibusb_close(struct libusb20_device *pdev)
503194676Sthompsa{
504194676Sthompsa	libusb_context *ctx;
505195957Salfred	struct libusb_device *dev;
506195957Salfred	uint8_t dummy;
507194676Sthompsa	int err;
508194676Sthompsa
509195957Salfred	if (pdev == NULL)
510195957Salfred		return;			/* be NULL safe */
511194676Sthompsa
512195957Salfred	dev = libusb_get_device(pdev);
513195957Salfred	ctx = dev->ctx;
514194676Sthompsa
515195957Salfred	libusb10_remove_pollfd(ctx, &dev->dev_poll);
516194676Sthompsa
517195957Salfred	libusb20_dev_close(pdev);
518199055Sthompsa
519199055Sthompsa	/* unref will free the "pdev" when the refcount reaches zero */
520195957Salfred	libusb_unref_device(dev);
521194676Sthompsa
522195957Salfred	/* make sure our event loop detects the closed device */
523195957Salfred	dummy = 0;
524194676Sthompsa	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
525213853Shselasky	if (err < (int)sizeof(dummy)) {
526195957Salfred		/* ignore error, if any */
527195957Salfred		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
528194676Sthompsa	}
529194676Sthompsa}
530194676Sthompsa
531194676Sthompsalibusb_device *
532195957Salfredlibusb_get_device(struct libusb20_device *pdev)
533194676Sthompsa{
534195957Salfred	if (pdev == NULL)
535194676Sthompsa		return (NULL);
536195957Salfred	return ((libusb_device *)pdev->privLuData);
537194676Sthompsa}
538194676Sthompsa
539194676Sthompsaint
540195957Salfredlibusb_get_configuration(struct libusb20_device *pdev, int *config)
541194676Sthompsa{
542195957Salfred	struct libusb20_config *pconf;
543194676Sthompsa
544195957Salfred	if (pdev == NULL || config == NULL)
545194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
546194676Sthompsa
547195957Salfred	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
548195957Salfred	if (pconf == NULL)
549195957Salfred		return (LIBUSB_ERROR_NO_MEM);
550194676Sthompsa
551195957Salfred	*config = pconf->desc.bConfigurationValue;
552195957Salfred
553195957Salfred	free(pconf);
554195957Salfred
555194676Sthompsa	return (0);
556194676Sthompsa}
557194676Sthompsa
558194676Sthompsaint
559195957Salfredlibusb_set_configuration(struct libusb20_device *pdev, int configuration)
560194676Sthompsa{
561195957Salfred	struct libusb20_config *pconf;
562195957Salfred	struct libusb_device *dev;
563195957Salfred	int err;
564195957Salfred	uint8_t i;
565194676Sthompsa
566195957Salfred	dev = libusb_get_device(pdev);
567195957Salfred	if (dev == NULL)
568194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
569194676Sthompsa
570195957Salfred	if (configuration < 1) {
571195957Salfred		/* unconfigure */
572195957Salfred		i = 255;
573195957Salfred	} else {
574195957Salfred		for (i = 0; i != 255; i++) {
575195957Salfred			uint8_t found;
576194676Sthompsa
577195957Salfred			pconf = libusb20_dev_alloc_config(pdev, i);
578195957Salfred			if (pconf == NULL)
579195957Salfred				return (LIBUSB_ERROR_INVALID_PARAM);
580195957Salfred			found = (pconf->desc.bConfigurationValue
581195957Salfred			    == configuration);
582195957Salfred			free(pconf);
583195957Salfred
584195957Salfred			if (found)
585195957Salfred				goto set_config;
586195957Salfred		}
587195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
588195957Salfred	}
589195957Salfred
590195957Salfredset_config:
591195957Salfred
592195957Salfred	libusb10_cancel_all_transfer(dev);
593195957Salfred
594195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
595195957Salfred
596195957Salfred	err = libusb20_dev_set_config_index(pdev, i);
597195957Salfred
598195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
599195957Salfred	    POLLOUT | POLLRDNORM | POLLWRNORM);
600195957Salfred
601195957Salfred	return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
602194676Sthompsa}
603194676Sthompsa
604194676Sthompsaint
605195957Salfredlibusb_claim_interface(struct libusb20_device *pdev, int interface_number)
606194676Sthompsa{
607195957Salfred	libusb_device *dev;
608195957Salfred	int err = 0;
609194676Sthompsa
610195957Salfred	dev = libusb_get_device(pdev);
611194676Sthompsa	if (dev == NULL)
612194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
613194676Sthompsa
614195957Salfred	if (interface_number < 0 || interface_number > 31)
615194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
616194676Sthompsa
617195957Salfred	CTX_LOCK(dev->ctx);
618194676Sthompsa	if (dev->claimed_interfaces & (1 << interface_number))
619195957Salfred		err = LIBUSB_ERROR_BUSY;
620194676Sthompsa
621195957Salfred	if (!err)
622194676Sthompsa		dev->claimed_interfaces |= (1 << interface_number);
623195957Salfred	CTX_UNLOCK(dev->ctx);
624195957Salfred	return (err);
625194676Sthompsa}
626194676Sthompsa
627194676Sthompsaint
628195957Salfredlibusb_release_interface(struct libusb20_device *pdev, int interface_number)
629194676Sthompsa{
630195957Salfred	libusb_device *dev;
631195957Salfred	int err = 0;
632194676Sthompsa
633195957Salfred	dev = libusb_get_device(pdev);
634194676Sthompsa	if (dev == NULL)
635194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
636194676Sthompsa
637195957Salfred	if (interface_number < 0 || interface_number > 31)
638194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
639194676Sthompsa
640195957Salfred	CTX_LOCK(dev->ctx);
641194676Sthompsa	if (!(dev->claimed_interfaces & (1 << interface_number)))
642195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
643194676Sthompsa
644195957Salfred	if (!err)
645194676Sthompsa		dev->claimed_interfaces &= ~(1 << interface_number);
646195957Salfred	CTX_UNLOCK(dev->ctx);
647195957Salfred	return (err);
648194676Sthompsa}
649194676Sthompsa
650194676Sthompsaint
651195957Salfredlibusb_set_interface_alt_setting(struct libusb20_device *pdev,
652194676Sthompsa    int interface_number, int alternate_setting)
653194676Sthompsa{
654195957Salfred	libusb_device *dev;
655195957Salfred	int err = 0;
656194676Sthompsa
657195957Salfred	dev = libusb_get_device(pdev);
658194676Sthompsa	if (dev == NULL)
659194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
660194676Sthompsa
661195957Salfred	if (interface_number < 0 || interface_number > 31)
662194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
663194676Sthompsa
664195957Salfred	CTX_LOCK(dev->ctx);
665195957Salfred	if (!(dev->claimed_interfaces & (1 << interface_number)))
666195957Salfred		err = LIBUSB_ERROR_NOT_FOUND;
667195957Salfred	CTX_UNLOCK(dev->ctx);
668194676Sthompsa
669195957Salfred	if (err)
670195957Salfred		return (err);
671195957Salfred
672195957Salfred	libusb10_cancel_all_transfer(dev);
673195957Salfred
674195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
675195957Salfred
676195957Salfred	err = libusb20_dev_set_alt_index(pdev,
677195957Salfred	    interface_number, alternate_setting);
678195957Salfred
679195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
680195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
681195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
682195957Salfred
683195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
684194676Sthompsa}
685194676Sthompsa
686195957Salfredstatic struct libusb20_transfer *
687195957Salfredlibusb10_get_transfer(struct libusb20_device *pdev,
688234491Shselasky    uint8_t endpoint, uint8_t xfer_index)
689195957Salfred{
690234491Shselasky	xfer_index &= 1;	/* double buffering */
691195957Salfred
692234491Shselasky	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
693195957Salfred
694195957Salfred	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
695195957Salfred		/* this is an IN endpoint */
696234491Shselasky		xfer_index |= 2;
697195957Salfred	}
698234491Shselasky	return (libusb20_tr_get_pointer(pdev, xfer_index));
699195957Salfred}
700195957Salfred
701194676Sthompsaint
702195957Salfredlibusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
703194676Sthompsa{
704194676Sthompsa	struct libusb20_transfer *xfer;
705195957Salfred	struct libusb_device *dev;
706195957Salfred	int err;
707194676Sthompsa
708195957Salfred	xfer = libusb10_get_transfer(pdev, endpoint, 0);
709195560Sthompsa	if (xfer == NULL)
710195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
711194676Sthompsa
712195957Salfred	dev = libusb_get_device(pdev);
713213853Shselasky	if (dev == NULL)
714213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
715195957Salfred
716195957Salfred	CTX_LOCK(dev->ctx);
717223642Shselasky	err = libusb20_tr_open(xfer, 0, 1, endpoint);
718195957Salfred	CTX_UNLOCK(dev->ctx);
719195957Salfred
720195957Salfred	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
721194676Sthompsa		return (LIBUSB_ERROR_OTHER);
722194676Sthompsa
723194676Sthompsa	libusb20_tr_clear_stall_sync(xfer);
724195957Salfred
725195957Salfred	/* check if we opened the transfer */
726195957Salfred	if (err == 0) {
727195957Salfred		CTX_LOCK(dev->ctx);
728194676Sthompsa		libusb20_tr_close(xfer);
729195957Salfred		CTX_UNLOCK(dev->ctx);
730195957Salfred	}
731195957Salfred	return (0);			/* success */
732194676Sthompsa}
733194676Sthompsa
734194676Sthompsaint
735195957Salfredlibusb_reset_device(struct libusb20_device *pdev)
736194676Sthompsa{
737195957Salfred	libusb_device *dev;
738195957Salfred	int err;
739194676Sthompsa
740195957Salfred	dev = libusb_get_device(pdev);
741194676Sthompsa	if (dev == NULL)
742213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
743194676Sthompsa
744195957Salfred	libusb10_cancel_all_transfer(dev);
745195957Salfred
746195957Salfred	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
747195957Salfred
748195957Salfred	err = libusb20_dev_reset(pdev);
749195957Salfred
750195957Salfred	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
751195957Salfred	    pdev, libusb20_dev_get_fd(pdev),
752195957Salfred	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
753195957Salfred
754195957Salfred	return (err ? LIBUSB_ERROR_OTHER : 0);
755194676Sthompsa}
756194676Sthompsa
757194676Sthompsaint
758213848Shselaskylibusb_check_connected(struct libusb20_device *pdev)
759213848Shselasky{
760213848Shselasky	libusb_device *dev;
761213848Shselasky	int err;
762213848Shselasky
763213848Shselasky	dev = libusb_get_device(pdev);
764213848Shselasky	if (dev == NULL)
765213848Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
766213848Shselasky
767213848Shselasky	err = libusb20_dev_check_connected(pdev);
768213848Shselasky
769213848Shselasky	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
770213848Shselasky}
771213848Shselasky
772213848Shselaskyint
773195957Salfredlibusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
774194676Sthompsa{
775195957Salfred	if (pdev == NULL)
776194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
777194676Sthompsa
778226220Shselasky	if (libusb20_dev_kernel_driver_active(pdev, interface))
779226220Shselasky		return (0);		/* no kernel driver is active */
780226220Shselasky	else
781226220Shselasky		return (1);		/* kernel driver is active */
782194676Sthompsa}
783194676Sthompsa
784194676Sthompsaint
785213853Shselaskylibusb_get_driver_np(struct libusb20_device *pdev, int interface,
786213853Shselasky    char *name, int namelen)
787213853Shselasky{
788213853Shselasky	return (libusb_get_driver(pdev, interface, name, namelen));
789213853Shselasky}
790213853Shselasky
791213853Shselaskyint
792213853Shselaskylibusb_get_driver(struct libusb20_device *pdev, int interface,
793213853Shselasky    char *name, int namelen)
794213853Shselasky{
795213853Shselasky	char *ptr;
796213853Shselasky	int err;
797213853Shselasky
798213853Shselasky	if (pdev == NULL)
799213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
800213853Shselasky	if (namelen < 1)
801213853Shselasky		return (LIBUSB_ERROR_INVALID_PARAM);
802224085Shselasky	if (namelen > 255)
803224085Shselasky		namelen = 255;
804213853Shselasky
805213853Shselasky	err = libusb20_dev_get_iface_desc(
806213853Shselasky	    pdev, interface, name, namelen);
807213853Shselasky
808213853Shselasky	if (err != 0)
809213853Shselasky		return (LIBUSB_ERROR_OTHER);
810213853Shselasky
811213853Shselasky	/* we only want the driver name */
812213853Shselasky	ptr = strstr(name, ":");
813213853Shselasky	if (ptr != NULL)
814213853Shselasky		*ptr = 0;
815213853Shselasky
816213853Shselasky	return (0);
817213853Shselasky}
818213853Shselasky
819213853Shselaskyint
820213853Shselaskylibusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
821213853Shselasky{
822213853Shselasky	return (libusb_detach_kernel_driver(pdev, interface));
823213853Shselasky}
824213853Shselasky
825213853Shselaskyint
826195957Salfredlibusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
827194676Sthompsa{
828195957Salfred	int err;
829194676Sthompsa
830195957Salfred	if (pdev == NULL)
831194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
832194676Sthompsa
833195957Salfred	err = libusb20_dev_detach_kernel_driver(
834195957Salfred	    pdev, interface);
835194676Sthompsa
836213853Shselasky	return (err ? LIBUSB_ERROR_OTHER : 0);
837194676Sthompsa}
838194676Sthompsa
839194676Sthompsaint
840195957Salfredlibusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
841194676Sthompsa{
842195957Salfred	if (pdev == NULL)
843194676Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
844195957Salfred	/* stub - currently not supported by libusb20 */
845194676Sthompsa	return (0);
846194676Sthompsa}
847194676Sthompsa
848194676Sthompsa/* Asynchronous device I/O */
849194676Sthompsa
850194676Sthompsastruct libusb_transfer *
851194676Sthompsalibusb_alloc_transfer(int iso_packets)
852194676Sthompsa{
853195957Salfred	struct libusb_transfer *uxfer;
854195957Salfred	struct libusb_super_transfer *sxfer;
855194676Sthompsa	int len;
856194676Sthompsa
857194676Sthompsa	len = sizeof(struct libusb_transfer) +
858195957Salfred	    sizeof(struct libusb_super_transfer) +
859194676Sthompsa	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
860194676Sthompsa
861195957Salfred	sxfer = malloc(len);
862195957Salfred	if (sxfer == NULL)
863194676Sthompsa		return (NULL);
864194676Sthompsa
865195957Salfred	memset(sxfer, 0, len);
866194676Sthompsa
867195957Salfred	uxfer = (struct libusb_transfer *)(
868195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
869194676Sthompsa
870195957Salfred	/* set default value */
871195957Salfred	uxfer->num_iso_packets = iso_packets;
872195957Salfred
873195957Salfred	return (uxfer);
874194676Sthompsa}
875194676Sthompsa
876194676Sthompsavoid
877195957Salfredlibusb_free_transfer(struct libusb_transfer *uxfer)
878194676Sthompsa{
879195957Salfred	struct libusb_super_transfer *sxfer;
880194676Sthompsa
881195957Salfred	if (uxfer == NULL)
882195957Salfred		return;			/* be NULL safe */
883194676Sthompsa
884215253Shselasky	/* check if we should free the transfer buffer */
885215253Shselasky	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
886215253Shselasky		free(uxfer->buffer);
887215253Shselasky
888195957Salfred	sxfer = (struct libusb_super_transfer *)(
889195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
890194676Sthompsa
891195957Salfred	free(sxfer);
892194676Sthompsa}
893194676Sthompsa
894219100Shselaskystatic uint32_t
895195957Salfredlibusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
896195560Sthompsa{
897219100Shselasky	uint32_t ret;
898195560Sthompsa
899195560Sthompsa	switch (xfer->type) {
900195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
901219100Shselasky		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
902195957Salfred		break;
903195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
904195560Sthompsa		ret = 2;
905195957Salfred		break;
906195560Sthompsa	default:
907195560Sthompsa		ret = 1;
908195957Salfred		break;
909195560Sthompsa	}
910195957Salfred	return (ret);
911195560Sthompsa}
912195560Sthompsa
913195560Sthompsastatic int
914195957Salfredlibusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
915195560Sthompsa{
916195560Sthompsa	int ret;
917195560Sthompsa	int usb_speed;
918195560Sthompsa
919195560Sthompsa	usb_speed = libusb20_dev_get_speed(pdev);
920195560Sthompsa
921195560Sthompsa	switch (xfer->type) {
922195560Sthompsa	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
923195957Salfred		ret = 0;		/* kernel will auto-select */
924195957Salfred		break;
925195560Sthompsa	case LIBUSB_TRANSFER_TYPE_CONTROL:
926195957Salfred		ret = 1024;
927195957Salfred		break;
928195957Salfred	default:
929195560Sthompsa		switch (usb_speed) {
930195957Salfred		case LIBUSB20_SPEED_LOW:
931195957Salfred			ret = 256;
932195957Salfred			break;
933195957Salfred		case LIBUSB20_SPEED_FULL:
934195957Salfred			ret = 4096;
935195957Salfred			break;
936195957Salfred		default:
937195957Salfred			ret = 16384;
938195957Salfred			break;
939195560Sthompsa		}
940195957Salfred		break;
941195560Sthompsa	}
942195957Salfred	return (ret);
943195560Sthompsa}
944195560Sthompsa
945195957Salfredstatic int
946195957Salfredlibusb10_convert_error(uint8_t status)
947195957Salfred{
948195957Salfred	;				/* indent fix */
949195957Salfred
950195957Salfred	switch (status) {
951195957Salfred	case LIBUSB20_TRANSFER_START:
952195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
953195957Salfred		return (LIBUSB_TRANSFER_COMPLETED);
954195957Salfred	case LIBUSB20_TRANSFER_OVERFLOW:
955195957Salfred		return (LIBUSB_TRANSFER_OVERFLOW);
956195957Salfred	case LIBUSB20_TRANSFER_NO_DEVICE:
957195957Salfred		return (LIBUSB_TRANSFER_NO_DEVICE);
958195957Salfred	case LIBUSB20_TRANSFER_STALL:
959195957Salfred		return (LIBUSB_TRANSFER_STALL);
960195957Salfred	case LIBUSB20_TRANSFER_CANCELLED:
961195957Salfred		return (LIBUSB_TRANSFER_CANCELLED);
962195957Salfred	case LIBUSB20_TRANSFER_TIMED_OUT:
963195957Salfred		return (LIBUSB_TRANSFER_TIMED_OUT);
964195957Salfred	default:
965195957Salfred		return (LIBUSB_TRANSFER_ERROR);
966195957Salfred	}
967195957Salfred}
968195957Salfred
969195957Salfred/* This function must be called locked */
970195957Salfred
971194676Sthompsastatic void
972195957Salfredlibusb10_complete_transfer(struct libusb20_transfer *pxfer,
973195957Salfred    struct libusb_super_transfer *sxfer, int status)
974194676Sthompsa{
975195957Salfred	struct libusb_transfer *uxfer;
976195957Salfred	struct libusb_device *dev;
977195957Salfred
978195957Salfred	uxfer = (struct libusb_transfer *)(
979195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
980195957Salfred
981195957Salfred	if (pxfer != NULL)
982195957Salfred		libusb20_tr_set_priv_sc1(pxfer, NULL);
983195957Salfred
984199575Sthompsa	/* set transfer status */
985195957Salfred	uxfer->status = status;
986195957Salfred
987199575Sthompsa	/* update super transfer state */
988199575Sthompsa	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
989199575Sthompsa
990195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
991195957Salfred
992195957Salfred	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
993195957Salfred}
994195957Salfred
995195957Salfred/* This function must be called locked */
996195957Salfred
997195957Salfredstatic void
998195957Salfredlibusb10_isoc_proxy(struct libusb20_transfer *pxfer)
999195957Salfred{
1000195957Salfred	struct libusb_super_transfer *sxfer;
1001195957Salfred	struct libusb_transfer *uxfer;
1002195957Salfred	uint32_t actlen;
1003195957Salfred	uint16_t iso_packets;
1004195957Salfred	uint16_t i;
1005194676Sthompsa	uint8_t status;
1006195957Salfred	uint8_t flags;
1007194676Sthompsa
1008195957Salfred	status = libusb20_tr_get_status(pxfer);
1009195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1010195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1011195957Salfred	iso_packets = libusb20_tr_get_max_frames(pxfer);
1012194676Sthompsa
1013195957Salfred	if (sxfer == NULL)
1014195957Salfred		return;			/* cancelled - nothing to do */
1015195957Salfred
1016195957Salfred	uxfer = (struct libusb_transfer *)(
1017195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1018195957Salfred
1019195957Salfred	if (iso_packets > uxfer->num_iso_packets)
1020195957Salfred		iso_packets = uxfer->num_iso_packets;
1021195957Salfred
1022195957Salfred	if (iso_packets == 0)
1023195957Salfred		return;			/* nothing to do */
1024195957Salfred
1025195957Salfred	/* make sure that the number of ISOCHRONOUS packets is valid */
1026195957Salfred	uxfer->num_iso_packets = iso_packets;
1027195957Salfred
1028195957Salfred	flags = uxfer->flags;
1029195957Salfred
1030194676Sthompsa	switch (status) {
1031194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1032195560Sthompsa
1033195957Salfred		/* update actual length */
1034195957Salfred		uxfer->actual_length = actlen;
1035195957Salfred		for (i = 0; i != iso_packets; i++) {
1036195957Salfred			uxfer->iso_packet_desc[i].actual_length =
1037195957Salfred			    libusb20_tr_get_length(pxfer, i);
1038195957Salfred		}
1039195957Salfred		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1040195957Salfred		break;
1041195560Sthompsa
1042194676Sthompsa	case LIBUSB20_TRANSFER_START:
1043195957Salfred
1044195957Salfred		/* setup length(s) */
1045195957Salfred		actlen = 0;
1046195957Salfred		for (i = 0; i != iso_packets; i++) {
1047195957Salfred			libusb20_tr_setup_isoc(pxfer,
1048195957Salfred			    &uxfer->buffer[actlen],
1049195957Salfred			    uxfer->iso_packet_desc[i].length, i);
1050195957Salfred			actlen += uxfer->iso_packet_desc[i].length;
1051194676Sthompsa		}
1052195957Salfred
1053195957Salfred		/* no remainder */
1054195957Salfred		sxfer->rem_len = 0;
1055195957Salfred
1056195957Salfred		libusb20_tr_set_total_frames(pxfer, iso_packets);
1057195957Salfred		libusb20_tr_submit(pxfer);
1058195957Salfred
1059195957Salfred		/* fork another USB transfer, if any */
1060195957Salfred		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1061195957Salfred		break;
1062195957Salfred
1063194676Sthompsa	default:
1064195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1065195957Salfred		break;
1066194676Sthompsa	}
1067195957Salfred}
1068194676Sthompsa
1069195957Salfred/* This function must be called locked */
1070195957Salfred
1071195957Salfredstatic void
1072195957Salfredlibusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1073195957Salfred{
1074195957Salfred	struct libusb_super_transfer *sxfer;
1075195957Salfred	struct libusb_transfer *uxfer;
1076195957Salfred	uint32_t max_bulk;
1077195957Salfred	uint32_t actlen;
1078195957Salfred	uint8_t status;
1079195957Salfred	uint8_t flags;
1080195957Salfred
1081195957Salfred	status = libusb20_tr_get_status(pxfer);
1082195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1083195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1084195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1085195957Salfred
1086195957Salfred	if (sxfer == NULL)
1087195957Salfred		return;			/* cancelled - nothing to do */
1088195957Salfred
1089195957Salfred	uxfer = (struct libusb_transfer *)(
1090195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1091195957Salfred
1092195957Salfred	flags = uxfer->flags;
1093195957Salfred
1094194676Sthompsa	switch (status) {
1095194676Sthompsa	case LIBUSB20_TRANSFER_COMPLETED:
1096195957Salfred
1097195957Salfred		uxfer->actual_length += actlen;
1098195957Salfred
1099195957Salfred		/* check for short packet */
1100195957Salfred		if (sxfer->last_len != actlen) {
1101195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1102195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1103195957Salfred			} else {
1104195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1105195957Salfred			}
1106195957Salfred			break;
1107195957Salfred		}
1108195957Salfred		/* check for end of data */
1109195957Salfred		if (sxfer->rem_len == 0) {
1110195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1111195957Salfred			break;
1112195957Salfred		}
1113195957Salfred		/* FALLTHROUGH */
1114195957Salfred
1115195957Salfred	case LIBUSB20_TRANSFER_START:
1116195957Salfred		if (max_bulk > sxfer->rem_len) {
1117195957Salfred			max_bulk = sxfer->rem_len;
1118195957Salfred		}
1119195957Salfred		/* setup new BULK or INTERRUPT transaction */
1120195957Salfred		libusb20_tr_setup_bulk(pxfer,
1121195957Salfred		    sxfer->curr_data, max_bulk, uxfer->timeout);
1122195957Salfred
1123195957Salfred		/* update counters */
1124195957Salfred		sxfer->last_len = max_bulk;
1125195957Salfred		sxfer->curr_data += max_bulk;
1126195957Salfred		sxfer->rem_len -= max_bulk;
1127195957Salfred
1128195957Salfred		libusb20_tr_submit(pxfer);
1129195957Salfred
1130195957Salfred		/* check if we can fork another USB transfer */
1131195957Salfred		if (sxfer->rem_len == 0)
1132195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1133195957Salfred		break;
1134195957Salfred
1135195957Salfred	default:
1136195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1137195957Salfred		break;
1138194676Sthompsa	}
1139194676Sthompsa}
1140194676Sthompsa
1141195957Salfred/* This function must be called locked */
1142195957Salfred
1143195957Salfredstatic void
1144195957Salfredlibusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1145194676Sthompsa{
1146195957Salfred	struct libusb_super_transfer *sxfer;
1147195957Salfred	struct libusb_transfer *uxfer;
1148195957Salfred	uint32_t max_bulk;
1149195957Salfred	uint32_t actlen;
1150195957Salfred	uint8_t status;
1151195957Salfred	uint8_t flags;
1152194676Sthompsa
1153195957Salfred	status = libusb20_tr_get_status(pxfer);
1154195957Salfred	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1155195957Salfred	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1156195957Salfred	actlen = libusb20_tr_get_actual_length(pxfer);
1157194676Sthompsa
1158195957Salfred	if (sxfer == NULL)
1159195957Salfred		return;			/* cancelled - nothing to do */
1160194676Sthompsa
1161195957Salfred	uxfer = (struct libusb_transfer *)(
1162195957Salfred	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1163194676Sthompsa
1164195957Salfred	flags = uxfer->flags;
1165194676Sthompsa
1166195957Salfred	switch (status) {
1167195957Salfred	case LIBUSB20_TRANSFER_COMPLETED:
1168194676Sthompsa
1169195957Salfred		uxfer->actual_length += actlen;
1170195957Salfred
1171195957Salfred		/* subtract length of SETUP packet, if any */
1172195957Salfred		actlen -= libusb20_tr_get_length(pxfer, 0);
1173195957Salfred
1174195957Salfred		/* check for short packet */
1175195957Salfred		if (sxfer->last_len != actlen) {
1176195957Salfred			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1177195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1178195957Salfred			} else {
1179195957Salfred				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1180195957Salfred			}
1181195957Salfred			break;
1182194676Sthompsa		}
1183195957Salfred		/* check for end of data */
1184195957Salfred		if (sxfer->rem_len == 0) {
1185195957Salfred			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1186195957Salfred			break;
1187195957Salfred		}
1188195957Salfred		/* FALLTHROUGH */
1189194676Sthompsa
1190195957Salfred	case LIBUSB20_TRANSFER_START:
1191195957Salfred		if (max_bulk > sxfer->rem_len) {
1192195957Salfred			max_bulk = sxfer->rem_len;
1193195957Salfred		}
1194195957Salfred		/* setup new CONTROL transaction */
1195195957Salfred		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1196195957Salfred			/* next fragment - don't send SETUP packet */
1197195957Salfred			libusb20_tr_set_length(pxfer, 0, 0);
1198195957Salfred		} else {
1199195957Salfred			/* first fragment - send SETUP packet */
1200195957Salfred			libusb20_tr_set_length(pxfer, 8, 0);
1201195957Salfred			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1202195957Salfred		}
1203195957Salfred
1204195957Salfred		if (max_bulk != 0) {
1205195957Salfred			libusb20_tr_set_length(pxfer, max_bulk, 1);
1206195957Salfred			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1207195957Salfred			libusb20_tr_set_total_frames(pxfer, 2);
1208195957Salfred		} else {
1209195957Salfred			libusb20_tr_set_total_frames(pxfer, 1);
1210195957Salfred		}
1211195957Salfred
1212195957Salfred		/* update counters */
1213195957Salfred		sxfer->last_len = max_bulk;
1214195957Salfred		sxfer->curr_data += max_bulk;
1215195957Salfred		sxfer->rem_len -= max_bulk;
1216195957Salfred
1217195957Salfred		libusb20_tr_submit(pxfer);
1218195957Salfred
1219195957Salfred		/* check if we can fork another USB transfer */
1220195957Salfred		if (sxfer->rem_len == 0)
1221195957Salfred			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1222195957Salfred		break;
1223195957Salfred
1224195957Salfred	default:
1225195957Salfred		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1226195957Salfred		break;
1227194676Sthompsa	}
1228195957Salfred}
1229195957Salfred
1230195957Salfred/* The following function must be called locked */
1231195957Salfred
1232195957Salfredstatic void
1233195957Salfredlibusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1234195957Salfred{
1235195957Salfred	struct libusb20_transfer *pxfer0;
1236195957Salfred	struct libusb20_transfer *pxfer1;
1237195957Salfred	struct libusb_super_transfer *sxfer;
1238195957Salfred	struct libusb_transfer *uxfer;
1239195957Salfred	struct libusb_device *dev;
1240195957Salfred	int err;
1241195957Salfred	int buffsize;
1242195957Salfred	int maxframe;
1243195957Salfred	int temp;
1244195957Salfred	uint8_t dummy;
1245195957Salfred
1246195957Salfred	dev = libusb_get_device(pdev);
1247195957Salfred
1248195957Salfred	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1249195957Salfred	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1250195957Salfred
1251195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL)
1252195957Salfred		return;			/* shouldn't happen */
1253195957Salfred
1254195957Salfred	temp = 0;
1255195957Salfred	if (libusb20_tr_pending(pxfer0))
1256195957Salfred		temp |= 1;
1257195957Salfred	if (libusb20_tr_pending(pxfer1))
1258195957Salfred		temp |= 2;
1259195957Salfred
1260195957Salfred	switch (temp) {
1261195957Salfred	case 3:
1262195957Salfred		/* wait till one of the transfers complete */
1263195957Salfred		return;
1264195957Salfred	case 2:
1265195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1266199575Sthompsa		if (sxfer == NULL)
1267199575Sthompsa			return;		/* cancelling */
1268195957Salfred		if (sxfer->rem_len)
1269195957Salfred			return;		/* cannot queue another one */
1270195957Salfred		/* swap transfers */
1271195957Salfred		pxfer1 = pxfer0;
1272195957Salfred		break;
1273195957Salfred	case 1:
1274195957Salfred		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1275199575Sthompsa		if (sxfer == NULL)
1276199575Sthompsa			return;		/* cancelling */
1277195957Salfred		if (sxfer->rem_len)
1278195957Salfred			return;		/* cannot queue another one */
1279195957Salfred		/* swap transfers */
1280195957Salfred		pxfer0 = pxfer1;
1281195957Salfred		break;
1282195957Salfred	default:
1283195957Salfred		break;
1284194676Sthompsa	}
1285195957Salfred
1286195957Salfred	/* find next transfer on same endpoint */
1287195957Salfred	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1288195957Salfred
1289195957Salfred		uxfer = (struct libusb_transfer *)(
1290195957Salfred		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1291195957Salfred
1292195957Salfred		if (uxfer->endpoint == endpoint) {
1293195957Salfred			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1294195957Salfred			sxfer->entry.tqe_prev = NULL;
1295195957Salfred			goto found;
1296194676Sthompsa		}
1297195957Salfred	}
1298195957Salfred	return;				/* success */
1299194676Sthompsa
1300195957Salfredfound:
1301194676Sthompsa
1302195957Salfred	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1303195957Salfred	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1304194676Sthompsa
1305195957Salfred	/* reset super transfer state */
1306195957Salfred	sxfer->rem_len = uxfer->length;
1307195957Salfred	sxfer->curr_data = uxfer->buffer;
1308195957Salfred	uxfer->actual_length = 0;
1309194676Sthompsa
1310195957Salfred	switch (uxfer->type) {
1311195957Salfred	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1312195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1313195957Salfred		break;
1314195957Salfred	case LIBUSB_TRANSFER_TYPE_BULK:
1315195957Salfred	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1316195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1317195957Salfred		break;
1318195957Salfred	case LIBUSB_TRANSFER_TYPE_CONTROL:
1319195957Salfred		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1320195957Salfred		if (sxfer->rem_len < 8)
1321195957Salfred			goto failure;
1322194676Sthompsa
1323195957Salfred		/* remove SETUP packet from data */
1324195957Salfred		sxfer->rem_len -= 8;
1325195957Salfred		sxfer->curr_data += 8;
1326195957Salfred		break;
1327195957Salfred	default:
1328195957Salfred		goto failure;
1329195560Sthompsa	}
1330195957Salfred
1331195957Salfred	buffsize = libusb10_get_buffsize(pdev, uxfer);
1332195957Salfred	maxframe = libusb10_get_maxframe(pdev, uxfer);
1333195957Salfred
1334195957Salfred	/* make sure the transfer is opened */
1335195957Salfred	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1336195957Salfred	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1337195957Salfred		goto failure;
1338194676Sthompsa	}
1339195957Salfred	libusb20_tr_start(pxfer0);
1340195957Salfred	return;
1341194676Sthompsa
1342195957Salfredfailure:
1343195957Salfred	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1344194676Sthompsa
1345195957Salfred	/* make sure our event loop spins the done handler */
1346195957Salfred	dummy = 0;
1347248236Shselasky	err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1348195957Salfred}
1349194676Sthompsa
1350195957Salfred/* The following function must be called unlocked */
1351194676Sthompsa
1352195957Salfredint
1353195957Salfredlibusb_submit_transfer(struct libusb_transfer *uxfer)
1354195957Salfred{
1355195957Salfred	struct libusb20_transfer *pxfer0;
1356195957Salfred	struct libusb20_transfer *pxfer1;
1357195957Salfred	struct libusb_super_transfer *sxfer;
1358195957Salfred	struct libusb_device *dev;
1359234684Shselasky	uint8_t endpoint;
1360195957Salfred	int err;
1361195957Salfred
1362195957Salfred	if (uxfer == NULL)
1363195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1364195957Salfred
1365195957Salfred	if (uxfer->dev_handle == NULL)
1366195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1367195957Salfred
1368195957Salfred	endpoint = uxfer->endpoint;
1369195957Salfred
1370195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1371195957Salfred
1372195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1373195957Salfred
1374195957Salfred	sxfer = (struct libusb_super_transfer *)(
1375195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1376195957Salfred
1377195957Salfred	CTX_LOCK(dev->ctx);
1378195957Salfred
1379195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1380195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1381195957Salfred
1382195957Salfred	if (pxfer0 == NULL || pxfer1 == NULL) {
1383195957Salfred		err = LIBUSB_ERROR_OTHER;
1384195957Salfred	} else if ((sxfer->entry.tqe_prev != NULL) ||
1385199575Sthompsa	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1386195957Salfred	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1387195957Salfred		err = LIBUSB_ERROR_BUSY;
1388195957Salfred	} else {
1389199575Sthompsa
1390199575Sthompsa		/* set pending state */
1391199575Sthompsa		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1392199575Sthompsa
1393199575Sthompsa		/* insert transfer into transfer head list */
1394195957Salfred		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1395195957Salfred
1396199575Sthompsa		/* start work transfers */
1397195957Salfred		libusb10_submit_transfer_sub(
1398195957Salfred		    uxfer->dev_handle, endpoint);
1399195957Salfred
1400195957Salfred		err = 0;		/* success */
1401195957Salfred	}
1402195957Salfred
1403195957Salfred	CTX_UNLOCK(dev->ctx);
1404195957Salfred
1405195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1406195957Salfred
1407195957Salfred	return (err);
1408194676Sthompsa}
1409194676Sthompsa
1410195957Salfred/* Asynchronous transfer cancel */
1411195957Salfred
1412194676Sthompsaint
1413195957Salfredlibusb_cancel_transfer(struct libusb_transfer *uxfer)
1414194676Sthompsa{
1415195957Salfred	struct libusb20_transfer *pxfer0;
1416195957Salfred	struct libusb20_transfer *pxfer1;
1417195957Salfred	struct libusb_super_transfer *sxfer;
1418195957Salfred	struct libusb_device *dev;
1419234684Shselasky	uint8_t endpoint;
1420199575Sthompsa	int retval;
1421194676Sthompsa
1422195957Salfred	if (uxfer == NULL)
1423195957Salfred		return (LIBUSB_ERROR_INVALID_PARAM);
1424194676Sthompsa
1425199575Sthompsa	/* check if not initialised */
1426195957Salfred	if (uxfer->dev_handle == NULL)
1427199575Sthompsa		return (LIBUSB_ERROR_NOT_FOUND);
1428194676Sthompsa
1429195957Salfred	endpoint = uxfer->endpoint;
1430194676Sthompsa
1431195957Salfred	dev = libusb_get_device(uxfer->dev_handle);
1432195957Salfred
1433195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1434195957Salfred
1435195957Salfred	sxfer = (struct libusb_super_transfer *)(
1436195957Salfred	    (uint8_t *)uxfer - sizeof(*sxfer));
1437195957Salfred
1438199575Sthompsa	retval = 0;
1439199575Sthompsa
1440195957Salfred	CTX_LOCK(dev->ctx);
1441195957Salfred
1442195957Salfred	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1443195957Salfred	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1444195957Salfred
1445199575Sthompsa	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1446199575Sthompsa		/* only update the transfer status */
1447199575Sthompsa		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1448199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1449199575Sthompsa	} else if (sxfer->entry.tqe_prev != NULL) {
1450195957Salfred		/* we are lucky - transfer is on a queue */
1451195957Salfred		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1452195957Salfred		sxfer->entry.tqe_prev = NULL;
1453199575Sthompsa		libusb10_complete_transfer(NULL,
1454199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1455195957Salfred	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1456195957Salfred		/* not started */
1457199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1458195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1459199575Sthompsa		libusb10_complete_transfer(pxfer0,
1460199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1461195957Salfred		libusb20_tr_stop(pxfer0);
1462195957Salfred		/* make sure the queue doesn't stall */
1463195957Salfred		libusb10_submit_transfer_sub(
1464195957Salfred		    uxfer->dev_handle, endpoint);
1465195957Salfred	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1466199575Sthompsa		libusb10_complete_transfer(pxfer1,
1467199575Sthompsa		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1468195957Salfred		libusb20_tr_stop(pxfer1);
1469195957Salfred		/* make sure the queue doesn't stall */
1470195957Salfred		libusb10_submit_transfer_sub(
1471195957Salfred		    uxfer->dev_handle, endpoint);
1472195957Salfred	} else {
1473195957Salfred		/* not started */
1474199575Sthompsa		retval = LIBUSB_ERROR_NOT_FOUND;
1475195957Salfred	}
1476195957Salfred
1477195957Salfred	CTX_UNLOCK(dev->ctx);
1478195957Salfred
1479195957Salfred	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1480195957Salfred
1481199575Sthompsa	return (retval);
1482194676Sthompsa}
1483194676Sthompsa
1484195957SalfredUNEXPORTED void
1485195957Salfredlibusb10_cancel_all_transfer(libusb_device *dev)
1486195957Salfred{
1487195957Salfred	/* TODO */
1488195957Salfred}
1489199055Sthompsa
1490199055Sthompsauint16_t
1491199055Sthompsalibusb_cpu_to_le16(uint16_t x)
1492199055Sthompsa{
1493199055Sthompsa	return (htole16(x));
1494199055Sthompsa}
1495199055Sthompsa
1496199055Sthompsauint16_t
1497199055Sthompsalibusb_le16_to_cpu(uint16_t x)
1498199055Sthompsa{
1499199055Sthompsa	return (le16toh(x));
1500199055Sthompsa}
1501199055Sthompsa
1502213853Shselaskyconst char *
1503213853Shselaskylibusb_strerror(int code)
1504213853Shselasky{
1505225659Shselasky	switch (code) {
1506225659Shselasky	case LIBUSB_SUCCESS:
1507225659Shselasky		return ("Success");
1508225659Shselasky	case LIBUSB_ERROR_IO:
1509225659Shselasky		return ("I/O error");
1510225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1511225659Shselasky		return ("Invalid parameter");
1512225659Shselasky	case LIBUSB_ERROR_ACCESS:
1513225659Shselasky		return ("Permissions error");
1514225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1515225659Shselasky		return ("No device");
1516225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1517225659Shselasky		return ("Not found");
1518225659Shselasky	case LIBUSB_ERROR_BUSY:
1519225659Shselasky		return ("Device busy");
1520225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1521225659Shselasky		return ("Timeout");
1522225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1523225659Shselasky		return ("Overflow");
1524225659Shselasky	case LIBUSB_ERROR_PIPE:
1525225659Shselasky		return ("Pipe error");
1526225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1527225659Shselasky		return ("Interrupted");
1528225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1529225659Shselasky		return ("Out of memory");
1530225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1531225659Shselasky		return ("Not supported");
1532225659Shselasky	case LIBUSB_ERROR_OTHER:
1533225659Shselasky		return ("Other error");
1534225659Shselasky	default:
1535225659Shselasky		return ("Unknown error");
1536225659Shselasky	}
1537213853Shselasky}
1538225659Shselasky
1539225659Shselaskyconst char *
1540225659Shselaskylibusb_error_name(int code)
1541225659Shselasky{
1542225659Shselasky	switch (code) {
1543225659Shselasky	case LIBUSB_SUCCESS:
1544225659Shselasky		return ("LIBUSB_SUCCESS");
1545225659Shselasky	case LIBUSB_ERROR_IO:
1546225659Shselasky		return ("LIBUSB_ERROR_IO");
1547225659Shselasky	case LIBUSB_ERROR_INVALID_PARAM:
1548225659Shselasky		return ("LIBUSB_ERROR_INVALID_PARAM");
1549225659Shselasky	case LIBUSB_ERROR_ACCESS:
1550225659Shselasky		return ("LIBUSB_ERROR_ACCESS");
1551225659Shselasky	case LIBUSB_ERROR_NO_DEVICE:
1552225659Shselasky		return ("LIBUSB_ERROR_NO_DEVICE");
1553225659Shselasky	case LIBUSB_ERROR_NOT_FOUND:
1554225659Shselasky		return ("LIBUSB_ERROR_NOT_FOUND");
1555225659Shselasky	case LIBUSB_ERROR_BUSY:
1556225659Shselasky		return ("LIBUSB_ERROR_BUSY");
1557225659Shselasky	case LIBUSB_ERROR_TIMEOUT:
1558225659Shselasky		return ("LIBUSB_ERROR_TIMEOUT");
1559225659Shselasky	case LIBUSB_ERROR_OVERFLOW:
1560225659Shselasky		return ("LIBUSB_ERROR_OVERFLOW");
1561225659Shselasky	case LIBUSB_ERROR_PIPE:
1562225659Shselasky		return ("LIBUSB_ERROR_PIPE");
1563225659Shselasky	case LIBUSB_ERROR_INTERRUPTED:
1564225659Shselasky		return ("LIBUSB_ERROR_INTERRUPTED");
1565225659Shselasky	case LIBUSB_ERROR_NO_MEM:
1566225659Shselasky		return ("LIBUSB_ERROR_NO_MEM");
1567225659Shselasky	case LIBUSB_ERROR_NOT_SUPPORTED:
1568225659Shselasky		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1569225659Shselasky	case LIBUSB_ERROR_OTHER:
1570225659Shselasky		return ("LIBUSB_ERROR_OTHER");
1571225659Shselasky	default:
1572225659Shselasky		return ("LIBUSB_ERROR_UNKNOWN");
1573225659Shselasky	}
1574225659Shselasky}
1575