libusb10.c revision 234193
1184610Salfred/* $FreeBSD: head/lib/libusb/libusb10.c 234193 2012-04-12 18:06:30Z hselasky $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4184610Salfred * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5184610Salfred *
6184610Salfred * Redistribution and use in source and binary forms, with or without
7184610Salfred * modification, are permitted provided that the following conditions
8184610Salfred * are met:
9184610Salfred * 1. Redistributions of source code must retain the above copyright
10184610Salfred *    notice, this list of conditions and the following disclaimer.
11184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
12184610Salfred *    notice, this list of conditions and the following disclaimer in the
13184610Salfred *    documentation and/or other materials provided with the distribution.
14184610Salfred *
15184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25184610Salfred * SUCH DAMAGE.
26184610Salfred */
27184610Salfred
28184610Salfred#include <sys/fcntl.h>
29184610Salfred#include <sys/ioctl.h>
30184610Salfred#include <sys/queue.h>
31184610Salfred
32184610Salfred#include <assert.h>
33184610Salfred#include <errno.h>
34184610Salfred#include <poll.h>
35184610Salfred#include <pthread.h>
36184610Salfred#include <stdio.h>
37184610Salfred#include <stdlib.h>
38184610Salfred#include <unistd.h>
39184610Salfred
40184610Salfred#define	libusb_device_handle libusb20_device
41184610Salfred
42184610Salfred#include "libusb20.h"
43184610Salfred#include "libusb20_desc.h"
44184610Salfred#include "libusb20_int.h"
45184610Salfred#include "libusb.h"
46184610Salfred#include "libusb10.h"
47184610Salfred
48184610Salfredstatic pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49184610Salfredstruct libusb_context *usbi_default_context = NULL;
50184610Salfred
51184610Salfred/* Prototypes */
52184610Salfred
53184610Salfredstatic struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54184610Salfredstatic int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55184610Salfredstatic int libusb10_convert_error(uint8_t status);
56184610Salfredstatic void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57184610Salfredstatic void libusb10_isoc_proxy(struct libusb20_transfer *);
58184610Salfredstatic void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59184610Salfredstatic void libusb10_ctrl_proxy(struct libusb20_transfer *);
60184610Salfredstatic void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
61184610Salfred
62184610Salfred/*  Library initialisation / deinitialisation */
63184610Salfred
64184610Salfredvoid
65184610Salfredlibusb_set_debug(libusb_context *ctx, int level)
66184610Salfred{
67184610Salfred	ctx = GET_CONTEXT(ctx);
68184610Salfred	if (ctx)
69184610Salfred		ctx->debug = level;
70184610Salfred}
71184610Salfred
72184610Salfredstatic void
73184610Salfredlibusb_set_nonblocking(int f)
74184610Salfred{
75184610Salfred	int flags;
76184610Salfred
77184610Salfred	/*
78184610Salfred	 * We ignore any failures in this function, hence the
79184610Salfred	 * non-blocking flag is not critical to the operation of
80184610Salfred	 * libUSB. We use F_GETFL and F_SETFL to be compatible with
81184610Salfred	 * Linux.
82184610Salfred	 */
83184610Salfred
84184610Salfred	flags = fcntl(f, F_GETFL, NULL);
85184610Salfred	if (flags == -1)
86184610Salfred		return;
87184610Salfred	flags |= O_NONBLOCK;
88184610Salfred	fcntl(f, F_SETFL, flags);
89184610Salfred}
90184610Salfred
91184610Salfredint
92184610Salfredlibusb_init(libusb_context **context)
93184610Salfred{
94184610Salfred	struct libusb_context *ctx;
95215969Syongari	char *debug;
96215969Syongari	int ret;
97215969Syongari
98215969Syongari	ctx = malloc(sizeof(*ctx));
99224020Syongari	if (!ctx)
100226743Syongari		return (LIBUSB_ERROR_INVALID_PARAM);
101226743Syongari
102226743Syongari	memset(ctx, 0, sizeof(*ctx));
103226743Syongari
104224020Syongari	debug = getenv("LIBUSB_DEBUG");
105184610Salfred	if (debug != NULL) {
106184610Salfred		ctx->debug = atoi(debug);
107184610Salfred		if (ctx->debug != 0)
108184610Salfred			ctx->debug_fixed = 1;
109184610Salfred	}
110184610Salfred	TAILQ_INIT(&ctx->pollfds);
111184610Salfred	TAILQ_INIT(&ctx->tr_done);
112184610Salfred
113184610Salfred	pthread_mutex_init(&ctx->ctx_lock, NULL);
114184610Salfred	pthread_cond_init(&ctx->ctx_cond, NULL);
115184610Salfred
116184610Salfred	ctx->ctx_handler = NO_THREAD;
117184610Salfred
118184610Salfred	ret = pipe(ctx->ctrl_pipe);
119184610Salfred	if (ret < 0) {
120184610Salfred		pthread_mutex_destroy(&ctx->ctx_lock);
121184610Salfred		pthread_cond_destroy(&ctx->ctx_cond);
122184610Salfred		free(ctx);
123184610Salfred		return (LIBUSB_ERROR_OTHER);
124184610Salfred	}
125184610Salfred	/* set non-blocking mode on the control pipe to avoid deadlock */
126184610Salfred	libusb_set_nonblocking(ctx->ctrl_pipe[0]);
127184610Salfred	libusb_set_nonblocking(ctx->ctrl_pipe[1]);
128184610Salfred
129184610Salfred	libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
130184610Salfred
131184610Salfred	pthread_mutex_lock(&default_context_lock);
132184610Salfred	if (usbi_default_context == NULL) {
133184610Salfred		usbi_default_context = ctx;
134184610Salfred	}
135184610Salfred	pthread_mutex_unlock(&default_context_lock);
136184610Salfred
137184610Salfred	if (context)
138184610Salfred		*context = ctx;
139184610Salfred
140184610Salfred	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
141224020Syongari
142184610Salfred	return (0);
143184610Salfred}
144184610Salfred
145184610Salfredvoid
146184610Salfredlibusb_exit(libusb_context *ctx)
147184610Salfred{
148224020Syongari	ctx = GET_CONTEXT(ctx);
149224020Syongari
150224020Syongari	if (ctx == NULL)
151224020Syongari		return;
152224020Syongari
153184610Salfred	/* XXX cleanup devices */
154186730Salfred
155186730Salfred	libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
156186730Salfred	close(ctx->ctrl_pipe[0]);
157186730Salfred	close(ctx->ctrl_pipe[1]);
158186730Salfred	pthread_mutex_destroy(&ctx->ctx_lock);
159186730Salfred	pthread_cond_destroy(&ctx->ctx_cond);
160184610Salfred
161186730Salfred	pthread_mutex_lock(&default_context_lock);
162186730Salfred	if (ctx == usbi_default_context) {
163186730Salfred		usbi_default_context = NULL;
164186730Salfred	}
165186730Salfred	pthread_mutex_unlock(&default_context_lock);
166186730Salfred
167186730Salfred	free(ctx);
168186730Salfred}
169186730Salfred
170186730Salfred/* Device handling and initialisation. */
171186730Salfred
172212130Sthompsassize_t
173212130Sthompsalibusb_get_device_list(libusb_context *ctx, libusb_device ***list)
174212130Sthompsa{
175212130Sthompsa	struct libusb20_backend *usb_backend;
176212130Sthompsa	struct libusb20_device *pdev;
177212130Sthompsa	struct libusb_device *dev;
178212130Sthompsa	int i;
179212130Sthompsa
180212130Sthompsa	ctx = GET_CONTEXT(ctx);
181212130Sthompsa
182212130Sthompsa	if (ctx == NULL)
183212130Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
184212130Sthompsa
185212130Sthompsa	if (list == NULL)
186212130Sthompsa		return (LIBUSB_ERROR_INVALID_PARAM);
187212130Sthompsa
188212130Sthompsa	usb_backend = libusb20_be_alloc_default();
189212130Sthompsa	if (usb_backend == NULL)
190212130Sthompsa		return (LIBUSB_ERROR_NO_MEM);
191224020Syongari
192215969Syongari	/* figure out how many USB devices are present */
193215969Syongari	pdev = NULL;
194215969Syongari	i = 0;
195215969Syongari	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
196215969Syongari		i++;
197215969Syongari
198215969Syongari	/* allocate device pointer list */
199215969Syongari	*list = malloc((i + 1) * sizeof(void *));
200215969Syongari	if (*list == NULL) {
201215969Syongari		libusb20_be_free(usb_backend);
202215969Syongari		return (LIBUSB_ERROR_NO_MEM);
203215969Syongari	}
204215969Syongari	/* create libusb v1.0 compliant devices */
205215969Syongari	i = 0;
206226743Syongari	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
207226743Syongari
208226743Syongari		dev = malloc(sizeof(*dev));
209226743Syongari		if (dev == NULL) {
210226743Syongari			while (i != 0) {
211226743Syongari				libusb_unref_device((*list)[i - 1]);
212226743Syongari				i--;
213226743Syongari			}
214226743Syongari			free(*list);
215226743Syongari			*list = NULL;
216226743Syongari			libusb20_be_free(usb_backend);
217226743Syongari			return (LIBUSB_ERROR_NO_MEM);
218226743Syongari		}
219226743Syongari		/* get device into libUSB v1.0 list */
220226743Syongari		libusb20_be_dequeue_device(usb_backend, pdev);
221226743Syongari
222226743Syongari		memset(dev, 0, sizeof(*dev));
223226743Syongari
224226743Syongari		/* init transfer queues */
225226743Syongari		TAILQ_INIT(&dev->tr_head);
226226743Syongari
227226743Syongari		/* set context we belong to */
228226743Syongari		dev->ctx = ctx;
229226743Syongari
230226743Syongari		/* link together the two structures */
231226743Syongari		dev->os_priv = pdev;
232226743Syongari		pdev->privLuData = dev;
233226743Syongari
234226743Syongari		(*list)[i] = libusb_ref_device(dev);
235226743Syongari		i++;
236226743Syongari	}
237226743Syongari	(*list)[i] = NULL;
238226743Syongari
239226743Syongari	libusb20_be_free(usb_backend);
240184610Salfred	return (i);
241184610Salfred}
242184610Salfred
243184610Salfredvoid
244184610Salfredlibusb_free_device_list(libusb_device **list, int unref_devices)
245184610Salfred{
246184610Salfred	int i;
247184610Salfred
248224020Syongari	if (list == NULL)
249224020Syongari		return;			/* be NULL safe */
250224020Syongari
251224020Syongari	if (unref_devices) {
252224020Syongari		for (i = 0; list[i] != NULL; i++)
253224020Syongari			libusb_unref_device(list[i]);
254224020Syongari	}
255224020Syongari	free(list);
256224020Syongari}
257224020Syongari
258224020Syongariuint8_t
259224020Syongarilibusb_get_bus_number(libusb_device *dev)
260224020Syongari{
261224020Syongari	if (dev == NULL)
262224020Syongari		return (0);		/* should not happen */
263224020Syongari	return (libusb20_dev_get_bus_number(dev->os_priv));
264224020Syongari}
265224020Syongari
266184610Salfreduint8_t
267184610Salfredlibusb_get_device_address(libusb_device *dev)
268226743Syongari{
269184610Salfred	if (dev == NULL)
270184610Salfred		return (0);		/* should not happen */
271184610Salfred	return (libusb20_dev_get_address(dev->os_priv));
272226743Syongari}
273226743Syongari
274226743Syongarienum libusb_speed
275226743Syongarilibusb_get_device_speed(libusb_device *dev)
276226743Syongari{
277226743Syongari	if (dev == NULL)
278226743Syongari		return (LIBUSB_SPEED_UNKNOWN);	/* should not happen */
279226743Syongari
280226743Syongari	switch (libusb20_dev_get_speed(dev->os_priv)) {
281226743Syongari	case LIBUSB20_SPEED_LOW:
282226743Syongari		return (LIBUSB_SPEED_LOW);
283226743Syongari	case LIBUSB20_SPEED_FULL:
284226743Syongari		return (LIBUSB_SPEED_FULL);
285226743Syongari	case LIBUSB20_SPEED_HIGH:
286226743Syongari		return (LIBUSB_SPEED_HIGH);
287226743Syongari	case LIBUSB20_SPEED_SUPER:
288226743Syongari		return (LIBUSB_SPEED_SUPER);
289226743Syongari	default:
290226743Syongari		break;
291226743Syongari	}
292226743Syongari	return (LIBUSB_SPEED_UNKNOWN);
293226743Syongari}
294226743Syongari
295226743Syongariint
296226743Syongarilibusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
297226743Syongari{
298226743Syongari	struct libusb_config_descriptor *pdconf;
299226743Syongari	struct libusb_interface *pinf;
300226743Syongari	struct libusb_interface_descriptor *pdinf;
301226743Syongari	struct libusb_endpoint_descriptor *pdend;
302226743Syongari	int i;
303226743Syongari	int j;
304226743Syongari	int k;
305226743Syongari	int ret;
306226743Syongari
307226743Syongari	if (dev == NULL)
308226743Syongari		return (LIBUSB_ERROR_NO_DEVICE);
309226743Syongari
310226743Syongari	ret = libusb_get_active_config_descriptor(dev, &pdconf);
311226743Syongari	if (ret < 0)
312226743Syongari		return (ret);
313226743Syongari
314226743Syongari	ret = LIBUSB_ERROR_NOT_FOUND;
315226743Syongari	for (i = 0; i < pdconf->bNumInterfaces; i++) {
316226743Syongari		pinf = &pdconf->interface[i];
317226743Syongari		for (j = 0; j < pinf->num_altsetting; j++) {
318226743Syongari			pdinf = &pinf->altsetting[j];
319226743Syongari			for (k = 0; k < pdinf->bNumEndpoints; k++) {
320226743Syongari				pdend = &pdinf->endpoint[k];
321226743Syongari				if (pdend->bEndpointAddress == endpoint) {
322226743Syongari					ret = pdend->wMaxPacketSize;
323226743Syongari					goto out;
324194228Sthompsa				}
325184610Salfred			}
326187259Sthompsa		}
327187259Sthompsa	}
328187259Sthompsa
329187259Sthompsaout:
330188412Sthompsa	libusb_free_config_descriptor(pdconf);
331187259Sthompsa	return (ret);
332187259Sthompsa}
333184610Salfred
334192984Sthompsaint
335188412Sthompsalibusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
336192984Sthompsa{
337188412Sthompsa	int multiplier;
338184610Salfred	int ret;
339188412Sthompsa
340186730Salfred	ret = libusb_get_max_packet_size(dev, endpoint);
341226743Syongari
342226743Syongari	switch (libusb20_dev_get_speed(dev->os_priv)) {
343188412Sthompsa	case LIBUSB20_SPEED_LOW:
344215968Syongari	case LIBUSB20_SPEED_FULL:
345215968Syongari		break;
346215968Syongari	default:
347184610Salfred		if (ret > -1) {
348188412Sthompsa			multiplier = (1 + ((ret >> 11) & 3));
349188412Sthompsa			if (multiplier > 3)
350224020Syongari				multiplier = 3;
351226743Syongari			ret = (ret & 0x7FF) * multiplier;
352188412Sthompsa		}
353184610Salfred		break;
354215968Syongari	}
355215968Syongari	return (ret);
356215968Syongari}
357215968Syongari
358215968Syongarilibusb_device *
359215968Syongarilibusb_ref_device(libusb_device *dev)
360215968Syongari{
361188412Sthompsa	if (dev == NULL)
362188412Sthompsa		return (NULL);		/* be NULL safe */
363188412Sthompsa
364	CTX_LOCK(dev->ctx);
365	dev->refcnt++;
366	CTX_UNLOCK(dev->ctx);
367
368	return (dev);
369}
370
371void
372libusb_unref_device(libusb_device *dev)
373{
374	if (dev == NULL)
375		return;			/* be NULL safe */
376
377	CTX_LOCK(dev->ctx);
378	dev->refcnt--;
379	CTX_UNLOCK(dev->ctx);
380
381	if (dev->refcnt == 0) {
382		libusb20_dev_free(dev->os_priv);
383		free(dev);
384	}
385}
386
387int
388libusb_open(libusb_device *dev, libusb_device_handle **devh)
389{
390	libusb_context *ctx = dev->ctx;
391	struct libusb20_device *pdev = dev->os_priv;
392	uint8_t dummy;
393	int err;
394
395	if (devh == NULL)
396		return (LIBUSB_ERROR_INVALID_PARAM);
397
398	/* set default device handle value */
399	*devh = NULL;
400
401	dev = libusb_ref_device(dev);
402	if (dev == NULL)
403		return (LIBUSB_ERROR_INVALID_PARAM);
404
405	err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
406	if (err) {
407		libusb_unref_device(dev);
408		return (LIBUSB_ERROR_NO_MEM);
409	}
410	libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
411	    POLLOUT | POLLRDNORM | POLLWRNORM);
412
413	/* make sure our event loop detects the new device */
414	dummy = 0;
415	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
416	if (err < (int)sizeof(dummy)) {
417		/* ignore error, if any */
418		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
419	}
420	*devh = pdev;
421
422	return (0);
423}
424
425libusb_device_handle *
426libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
427    uint16_t product_id)
428{
429	struct libusb_device **devs;
430	struct libusb20_device *pdev;
431	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
432	int i;
433	int j;
434
435	ctx = GET_CONTEXT(ctx);
436	if (ctx == NULL)
437		return (NULL);		/* be NULL safe */
438
439	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
440
441	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
442		return (NULL);
443
444	pdev = NULL;
445	for (j = 0; j < i; j++) {
446		struct libusb20_device *tdev;
447
448		tdev = devs[j]->os_priv;
449		pdesc = libusb20_dev_get_device_desc(tdev);
450		/*
451		 * NOTE: The USB library will automatically swap the
452		 * fields in the device descriptor to be of host
453		 * endian type!
454		 */
455		if (pdesc->idVendor == vendor_id &&
456		    pdesc->idProduct == product_id) {
457			libusb_open(devs[j], &pdev);
458			break;
459		}
460	}
461
462	libusb_free_device_list(devs, 1);
463	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
464	return (pdev);
465}
466
467void
468libusb_close(struct libusb20_device *pdev)
469{
470	libusb_context *ctx;
471	struct libusb_device *dev;
472	uint8_t dummy;
473	int err;
474
475	if (pdev == NULL)
476		return;			/* be NULL safe */
477
478	dev = libusb_get_device(pdev);
479	ctx = dev->ctx;
480
481	libusb10_remove_pollfd(ctx, &dev->dev_poll);
482
483	libusb20_dev_close(pdev);
484
485	/* unref will free the "pdev" when the refcount reaches zero */
486	libusb_unref_device(dev);
487
488	/* make sure our event loop detects the closed device */
489	dummy = 0;
490	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
491	if (err < (int)sizeof(dummy)) {
492		/* ignore error, if any */
493		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
494	}
495}
496
497libusb_device *
498libusb_get_device(struct libusb20_device *pdev)
499{
500	if (pdev == NULL)
501		return (NULL);
502	return ((libusb_device *)pdev->privLuData);
503}
504
505int
506libusb_get_configuration(struct libusb20_device *pdev, int *config)
507{
508	struct libusb20_config *pconf;
509
510	if (pdev == NULL || config == NULL)
511		return (LIBUSB_ERROR_INVALID_PARAM);
512
513	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
514	if (pconf == NULL)
515		return (LIBUSB_ERROR_NO_MEM);
516
517	*config = pconf->desc.bConfigurationValue;
518
519	free(pconf);
520
521	return (0);
522}
523
524int
525libusb_set_configuration(struct libusb20_device *pdev, int configuration)
526{
527	struct libusb20_config *pconf;
528	struct libusb_device *dev;
529	int err;
530	uint8_t i;
531
532	dev = libusb_get_device(pdev);
533	if (dev == NULL)
534		return (LIBUSB_ERROR_INVALID_PARAM);
535
536	if (configuration < 1) {
537		/* unconfigure */
538		i = 255;
539	} else {
540		for (i = 0; i != 255; i++) {
541			uint8_t found;
542
543			pconf = libusb20_dev_alloc_config(pdev, i);
544			if (pconf == NULL)
545				return (LIBUSB_ERROR_INVALID_PARAM);
546			found = (pconf->desc.bConfigurationValue
547			    == configuration);
548			free(pconf);
549
550			if (found)
551				goto set_config;
552		}
553		return (LIBUSB_ERROR_INVALID_PARAM);
554	}
555
556set_config:
557
558	libusb10_cancel_all_transfer(dev);
559
560	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
561
562	err = libusb20_dev_set_config_index(pdev, i);
563
564	libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
565	    POLLOUT | POLLRDNORM | POLLWRNORM);
566
567	return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
568}
569
570int
571libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
572{
573	libusb_device *dev;
574	int err = 0;
575
576	dev = libusb_get_device(pdev);
577	if (dev == NULL)
578		return (LIBUSB_ERROR_INVALID_PARAM);
579
580	if (interface_number < 0 || interface_number > 31)
581		return (LIBUSB_ERROR_INVALID_PARAM);
582
583	CTX_LOCK(dev->ctx);
584	if (dev->claimed_interfaces & (1 << interface_number))
585		err = LIBUSB_ERROR_BUSY;
586
587	if (!err)
588		dev->claimed_interfaces |= (1 << interface_number);
589	CTX_UNLOCK(dev->ctx);
590	return (err);
591}
592
593int
594libusb_release_interface(struct libusb20_device *pdev, int interface_number)
595{
596	libusb_device *dev;
597	int err = 0;
598
599	dev = libusb_get_device(pdev);
600	if (dev == NULL)
601		return (LIBUSB_ERROR_INVALID_PARAM);
602
603	if (interface_number < 0 || interface_number > 31)
604		return (LIBUSB_ERROR_INVALID_PARAM);
605
606	CTX_LOCK(dev->ctx);
607	if (!(dev->claimed_interfaces & (1 << interface_number)))
608		err = LIBUSB_ERROR_NOT_FOUND;
609
610	if (!err)
611		dev->claimed_interfaces &= ~(1 << interface_number);
612	CTX_UNLOCK(dev->ctx);
613	return (err);
614}
615
616int
617libusb_set_interface_alt_setting(struct libusb20_device *pdev,
618    int interface_number, int alternate_setting)
619{
620	libusb_device *dev;
621	int err = 0;
622
623	dev = libusb_get_device(pdev);
624	if (dev == NULL)
625		return (LIBUSB_ERROR_INVALID_PARAM);
626
627	if (interface_number < 0 || interface_number > 31)
628		return (LIBUSB_ERROR_INVALID_PARAM);
629
630	CTX_LOCK(dev->ctx);
631	if (!(dev->claimed_interfaces & (1 << interface_number)))
632		err = LIBUSB_ERROR_NOT_FOUND;
633	CTX_UNLOCK(dev->ctx);
634
635	if (err)
636		return (err);
637
638	libusb10_cancel_all_transfer(dev);
639
640	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
641
642	err = libusb20_dev_set_alt_index(pdev,
643	    interface_number, alternate_setting);
644
645	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
646	    pdev, libusb20_dev_get_fd(pdev),
647	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
648
649	return (err ? LIBUSB_ERROR_OTHER : 0);
650}
651
652static struct libusb20_transfer *
653libusb10_get_transfer(struct libusb20_device *pdev,
654    uint8_t endpoint, uint8_t index)
655{
656	index &= 1;			/* double buffering */
657
658	index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
659
660	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
661		/* this is an IN endpoint */
662		index |= 2;
663	}
664	return (libusb20_tr_get_pointer(pdev, index));
665}
666
667int
668libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
669{
670	struct libusb20_transfer *xfer;
671	struct libusb_device *dev;
672	int err;
673
674	xfer = libusb10_get_transfer(pdev, endpoint, 0);
675	if (xfer == NULL)
676		return (LIBUSB_ERROR_INVALID_PARAM);
677
678	dev = libusb_get_device(pdev);
679	if (dev == NULL)
680		return (LIBUSB_ERROR_INVALID_PARAM);
681
682	CTX_LOCK(dev->ctx);
683	err = libusb20_tr_open(xfer, 0, 1, endpoint);
684	CTX_UNLOCK(dev->ctx);
685
686	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
687		return (LIBUSB_ERROR_OTHER);
688
689	libusb20_tr_clear_stall_sync(xfer);
690
691	/* check if we opened the transfer */
692	if (err == 0) {
693		CTX_LOCK(dev->ctx);
694		libusb20_tr_close(xfer);
695		CTX_UNLOCK(dev->ctx);
696	}
697	return (0);			/* success */
698}
699
700int
701libusb_reset_device(struct libusb20_device *pdev)
702{
703	libusb_device *dev;
704	int err;
705
706	dev = libusb_get_device(pdev);
707	if (dev == NULL)
708		return (LIBUSB_ERROR_INVALID_PARAM);
709
710	libusb10_cancel_all_transfer(dev);
711
712	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
713
714	err = libusb20_dev_reset(pdev);
715
716	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
717	    pdev, libusb20_dev_get_fd(pdev),
718	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
719
720	return (err ? LIBUSB_ERROR_OTHER : 0);
721}
722
723int
724libusb_check_connected(struct libusb20_device *pdev)
725{
726	libusb_device *dev;
727	int err;
728
729	dev = libusb_get_device(pdev);
730	if (dev == NULL)
731		return (LIBUSB_ERROR_INVALID_PARAM);
732
733	err = libusb20_dev_check_connected(pdev);
734
735	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
736}
737
738int
739libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
740{
741	if (pdev == NULL)
742		return (LIBUSB_ERROR_INVALID_PARAM);
743
744	if (libusb20_dev_kernel_driver_active(pdev, interface))
745		return (0);		/* no kernel driver is active */
746	else
747		return (1);		/* kernel driver is active */
748}
749
750int
751libusb_get_driver_np(struct libusb20_device *pdev, int interface,
752    char *name, int namelen)
753{
754	return (libusb_get_driver(pdev, interface, name, namelen));
755}
756
757int
758libusb_get_driver(struct libusb20_device *pdev, int interface,
759    char *name, int namelen)
760{
761	char *ptr;
762	int err;
763
764	if (pdev == NULL)
765		return (LIBUSB_ERROR_INVALID_PARAM);
766	if (namelen < 1)
767		return (LIBUSB_ERROR_INVALID_PARAM);
768	if (namelen > 255)
769		namelen = 255;
770
771	err = libusb20_dev_get_iface_desc(
772	    pdev, interface, name, namelen);
773
774	if (err != 0)
775		return (LIBUSB_ERROR_OTHER);
776
777	/* we only want the driver name */
778	ptr = strstr(name, ":");
779	if (ptr != NULL)
780		*ptr = 0;
781
782	return (0);
783}
784
785int
786libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
787{
788	return (libusb_detach_kernel_driver(pdev, interface));
789}
790
791int
792libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
793{
794	int err;
795
796	if (pdev == NULL)
797		return (LIBUSB_ERROR_INVALID_PARAM);
798
799	err = libusb20_dev_detach_kernel_driver(
800	    pdev, interface);
801
802	return (err ? LIBUSB_ERROR_OTHER : 0);
803}
804
805int
806libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
807{
808	if (pdev == NULL)
809		return (LIBUSB_ERROR_INVALID_PARAM);
810	/* stub - currently not supported by libusb20 */
811	return (0);
812}
813
814/* Asynchronous device I/O */
815
816struct libusb_transfer *
817libusb_alloc_transfer(int iso_packets)
818{
819	struct libusb_transfer *uxfer;
820	struct libusb_super_transfer *sxfer;
821	int len;
822
823	len = sizeof(struct libusb_transfer) +
824	    sizeof(struct libusb_super_transfer) +
825	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
826
827	sxfer = malloc(len);
828	if (sxfer == NULL)
829		return (NULL);
830
831	memset(sxfer, 0, len);
832
833	uxfer = (struct libusb_transfer *)(
834	    ((uint8_t *)sxfer) + sizeof(*sxfer));
835
836	/* set default value */
837	uxfer->num_iso_packets = iso_packets;
838
839	return (uxfer);
840}
841
842void
843libusb_free_transfer(struct libusb_transfer *uxfer)
844{
845	struct libusb_super_transfer *sxfer;
846
847	if (uxfer == NULL)
848		return;			/* be NULL safe */
849
850	/* check if we should free the transfer buffer */
851	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
852		free(uxfer->buffer);
853
854	sxfer = (struct libusb_super_transfer *)(
855	    (uint8_t *)uxfer - sizeof(*sxfer));
856
857	free(sxfer);
858}
859
860static uint32_t
861libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
862{
863	uint32_t ret;
864
865	switch (xfer->type) {
866	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
867		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
868		break;
869	case LIBUSB_TRANSFER_TYPE_CONTROL:
870		ret = 2;
871		break;
872	default:
873		ret = 1;
874		break;
875	}
876	return (ret);
877}
878
879static int
880libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
881{
882	int ret;
883	int usb_speed;
884
885	usb_speed = libusb20_dev_get_speed(pdev);
886
887	switch (xfer->type) {
888	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
889		ret = 0;		/* kernel will auto-select */
890		break;
891	case LIBUSB_TRANSFER_TYPE_CONTROL:
892		ret = 1024;
893		break;
894	default:
895		switch (usb_speed) {
896		case LIBUSB20_SPEED_LOW:
897			ret = 256;
898			break;
899		case LIBUSB20_SPEED_FULL:
900			ret = 4096;
901			break;
902		default:
903			ret = 16384;
904			break;
905		}
906		break;
907	}
908	return (ret);
909}
910
911static int
912libusb10_convert_error(uint8_t status)
913{
914	;				/* indent fix */
915
916	switch (status) {
917	case LIBUSB20_TRANSFER_START:
918	case LIBUSB20_TRANSFER_COMPLETED:
919		return (LIBUSB_TRANSFER_COMPLETED);
920	case LIBUSB20_TRANSFER_OVERFLOW:
921		return (LIBUSB_TRANSFER_OVERFLOW);
922	case LIBUSB20_TRANSFER_NO_DEVICE:
923		return (LIBUSB_TRANSFER_NO_DEVICE);
924	case LIBUSB20_TRANSFER_STALL:
925		return (LIBUSB_TRANSFER_STALL);
926	case LIBUSB20_TRANSFER_CANCELLED:
927		return (LIBUSB_TRANSFER_CANCELLED);
928	case LIBUSB20_TRANSFER_TIMED_OUT:
929		return (LIBUSB_TRANSFER_TIMED_OUT);
930	default:
931		return (LIBUSB_TRANSFER_ERROR);
932	}
933}
934
935/* This function must be called locked */
936
937static void
938libusb10_complete_transfer(struct libusb20_transfer *pxfer,
939    struct libusb_super_transfer *sxfer, int status)
940{
941	struct libusb_transfer *uxfer;
942	struct libusb_device *dev;
943
944	uxfer = (struct libusb_transfer *)(
945	    ((uint8_t *)sxfer) + sizeof(*sxfer));
946
947	if (pxfer != NULL)
948		libusb20_tr_set_priv_sc1(pxfer, NULL);
949
950	/* set transfer status */
951	uxfer->status = status;
952
953	/* update super transfer state */
954	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
955
956	dev = libusb_get_device(uxfer->dev_handle);
957
958	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
959}
960
961/* This function must be called locked */
962
963static void
964libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
965{
966	struct libusb_super_transfer *sxfer;
967	struct libusb_transfer *uxfer;
968	uint32_t actlen;
969	uint16_t iso_packets;
970	uint16_t i;
971	uint8_t status;
972	uint8_t flags;
973
974	status = libusb20_tr_get_status(pxfer);
975	sxfer = libusb20_tr_get_priv_sc1(pxfer);
976	actlen = libusb20_tr_get_actual_length(pxfer);
977	iso_packets = libusb20_tr_get_max_frames(pxfer);
978
979	if (sxfer == NULL)
980		return;			/* cancelled - nothing to do */
981
982	uxfer = (struct libusb_transfer *)(
983	    ((uint8_t *)sxfer) + sizeof(*sxfer));
984
985	if (iso_packets > uxfer->num_iso_packets)
986		iso_packets = uxfer->num_iso_packets;
987
988	if (iso_packets == 0)
989		return;			/* nothing to do */
990
991	/* make sure that the number of ISOCHRONOUS packets is valid */
992	uxfer->num_iso_packets = iso_packets;
993
994	flags = uxfer->flags;
995
996	switch (status) {
997	case LIBUSB20_TRANSFER_COMPLETED:
998
999		/* update actual length */
1000		uxfer->actual_length = actlen;
1001		for (i = 0; i != iso_packets; i++) {
1002			uxfer->iso_packet_desc[i].actual_length =
1003			    libusb20_tr_get_length(pxfer, i);
1004		}
1005		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1006		break;
1007
1008	case LIBUSB20_TRANSFER_START:
1009
1010		/* setup length(s) */
1011		actlen = 0;
1012		for (i = 0; i != iso_packets; i++) {
1013			libusb20_tr_setup_isoc(pxfer,
1014			    &uxfer->buffer[actlen],
1015			    uxfer->iso_packet_desc[i].length, i);
1016			actlen += uxfer->iso_packet_desc[i].length;
1017		}
1018
1019		/* no remainder */
1020		sxfer->rem_len = 0;
1021
1022		libusb20_tr_set_total_frames(pxfer, iso_packets);
1023		libusb20_tr_submit(pxfer);
1024
1025		/* fork another USB transfer, if any */
1026		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1027		break;
1028
1029	default:
1030		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1031		break;
1032	}
1033}
1034
1035/* This function must be called locked */
1036
1037static void
1038libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1039{
1040	struct libusb_super_transfer *sxfer;
1041	struct libusb_transfer *uxfer;
1042	uint32_t max_bulk;
1043	uint32_t actlen;
1044	uint8_t status;
1045	uint8_t flags;
1046
1047	status = libusb20_tr_get_status(pxfer);
1048	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1049	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1050	actlen = libusb20_tr_get_actual_length(pxfer);
1051
1052	if (sxfer == NULL)
1053		return;			/* cancelled - nothing to do */
1054
1055	uxfer = (struct libusb_transfer *)(
1056	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1057
1058	flags = uxfer->flags;
1059
1060	switch (status) {
1061	case LIBUSB20_TRANSFER_COMPLETED:
1062
1063		uxfer->actual_length += actlen;
1064
1065		/* check for short packet */
1066		if (sxfer->last_len != actlen) {
1067			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1068				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1069			} else {
1070				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1071			}
1072			break;
1073		}
1074		/* check for end of data */
1075		if (sxfer->rem_len == 0) {
1076			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1077			break;
1078		}
1079		/* FALLTHROUGH */
1080
1081	case LIBUSB20_TRANSFER_START:
1082		if (max_bulk > sxfer->rem_len) {
1083			max_bulk = sxfer->rem_len;
1084		}
1085		/* setup new BULK or INTERRUPT transaction */
1086		libusb20_tr_setup_bulk(pxfer,
1087		    sxfer->curr_data, max_bulk, uxfer->timeout);
1088
1089		/* update counters */
1090		sxfer->last_len = max_bulk;
1091		sxfer->curr_data += max_bulk;
1092		sxfer->rem_len -= max_bulk;
1093
1094		libusb20_tr_submit(pxfer);
1095
1096		/* check if we can fork another USB transfer */
1097		if (sxfer->rem_len == 0)
1098			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1099		break;
1100
1101	default:
1102		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1103		break;
1104	}
1105}
1106
1107/* This function must be called locked */
1108
1109static void
1110libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1111{
1112	struct libusb_super_transfer *sxfer;
1113	struct libusb_transfer *uxfer;
1114	uint32_t max_bulk;
1115	uint32_t actlen;
1116	uint8_t status;
1117	uint8_t flags;
1118
1119	status = libusb20_tr_get_status(pxfer);
1120	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1121	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1122	actlen = libusb20_tr_get_actual_length(pxfer);
1123
1124	if (sxfer == NULL)
1125		return;			/* cancelled - nothing to do */
1126
1127	uxfer = (struct libusb_transfer *)(
1128	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1129
1130	flags = uxfer->flags;
1131
1132	switch (status) {
1133	case LIBUSB20_TRANSFER_COMPLETED:
1134
1135		uxfer->actual_length += actlen;
1136
1137		/* subtract length of SETUP packet, if any */
1138		actlen -= libusb20_tr_get_length(pxfer, 0);
1139
1140		/* check for short packet */
1141		if (sxfer->last_len != actlen) {
1142			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1143				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1144			} else {
1145				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1146			}
1147			break;
1148		}
1149		/* check for end of data */
1150		if (sxfer->rem_len == 0) {
1151			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1152			break;
1153		}
1154		/* FALLTHROUGH */
1155
1156	case LIBUSB20_TRANSFER_START:
1157		if (max_bulk > sxfer->rem_len) {
1158			max_bulk = sxfer->rem_len;
1159		}
1160		/* setup new CONTROL transaction */
1161		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1162			/* next fragment - don't send SETUP packet */
1163			libusb20_tr_set_length(pxfer, 0, 0);
1164		} else {
1165			/* first fragment - send SETUP packet */
1166			libusb20_tr_set_length(pxfer, 8, 0);
1167			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1168		}
1169
1170		if (max_bulk != 0) {
1171			libusb20_tr_set_length(pxfer, max_bulk, 1);
1172			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1173			libusb20_tr_set_total_frames(pxfer, 2);
1174		} else {
1175			libusb20_tr_set_total_frames(pxfer, 1);
1176		}
1177
1178		/* update counters */
1179		sxfer->last_len = max_bulk;
1180		sxfer->curr_data += max_bulk;
1181		sxfer->rem_len -= max_bulk;
1182
1183		libusb20_tr_submit(pxfer);
1184
1185		/* check if we can fork another USB transfer */
1186		if (sxfer->rem_len == 0)
1187			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1188		break;
1189
1190	default:
1191		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1192		break;
1193	}
1194}
1195
1196/* The following function must be called locked */
1197
1198static void
1199libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1200{
1201	struct libusb20_transfer *pxfer0;
1202	struct libusb20_transfer *pxfer1;
1203	struct libusb_super_transfer *sxfer;
1204	struct libusb_transfer *uxfer;
1205	struct libusb_device *dev;
1206	int err;
1207	int buffsize;
1208	int maxframe;
1209	int temp;
1210	uint8_t dummy;
1211
1212	dev = libusb_get_device(pdev);
1213
1214	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1215	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1216
1217	if (pxfer0 == NULL || pxfer1 == NULL)
1218		return;			/* shouldn't happen */
1219
1220	temp = 0;
1221	if (libusb20_tr_pending(pxfer0))
1222		temp |= 1;
1223	if (libusb20_tr_pending(pxfer1))
1224		temp |= 2;
1225
1226	switch (temp) {
1227	case 3:
1228		/* wait till one of the transfers complete */
1229		return;
1230	case 2:
1231		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1232		if (sxfer == NULL)
1233			return;		/* cancelling */
1234		if (sxfer->rem_len)
1235			return;		/* cannot queue another one */
1236		/* swap transfers */
1237		pxfer1 = pxfer0;
1238		break;
1239	case 1:
1240		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1241		if (sxfer == NULL)
1242			return;		/* cancelling */
1243		if (sxfer->rem_len)
1244			return;		/* cannot queue another one */
1245		/* swap transfers */
1246		pxfer0 = pxfer1;
1247		break;
1248	default:
1249		break;
1250	}
1251
1252	/* find next transfer on same endpoint */
1253	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1254
1255		uxfer = (struct libusb_transfer *)(
1256		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1257
1258		if (uxfer->endpoint == endpoint) {
1259			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1260			sxfer->entry.tqe_prev = NULL;
1261			goto found;
1262		}
1263	}
1264	return;				/* success */
1265
1266found:
1267
1268	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1269	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1270
1271	/* reset super transfer state */
1272	sxfer->rem_len = uxfer->length;
1273	sxfer->curr_data = uxfer->buffer;
1274	uxfer->actual_length = 0;
1275
1276	switch (uxfer->type) {
1277	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1278		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1279		break;
1280	case LIBUSB_TRANSFER_TYPE_BULK:
1281	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1282		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1283		break;
1284	case LIBUSB_TRANSFER_TYPE_CONTROL:
1285		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1286		if (sxfer->rem_len < 8)
1287			goto failure;
1288
1289		/* remove SETUP packet from data */
1290		sxfer->rem_len -= 8;
1291		sxfer->curr_data += 8;
1292		break;
1293	default:
1294		goto failure;
1295	}
1296
1297	buffsize = libusb10_get_buffsize(pdev, uxfer);
1298	maxframe = libusb10_get_maxframe(pdev, uxfer);
1299
1300	/* make sure the transfer is opened */
1301	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1302	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1303		goto failure;
1304	}
1305	libusb20_tr_start(pxfer0);
1306	return;
1307
1308failure:
1309	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1310
1311	/* make sure our event loop spins the done handler */
1312	dummy = 0;
1313	write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1314}
1315
1316/* The following function must be called unlocked */
1317
1318int
1319libusb_submit_transfer(struct libusb_transfer *uxfer)
1320{
1321	struct libusb20_transfer *pxfer0;
1322	struct libusb20_transfer *pxfer1;
1323	struct libusb_super_transfer *sxfer;
1324	struct libusb_device *dev;
1325	uint32_t endpoint;
1326	int err;
1327
1328	if (uxfer == NULL)
1329		return (LIBUSB_ERROR_INVALID_PARAM);
1330
1331	if (uxfer->dev_handle == NULL)
1332		return (LIBUSB_ERROR_INVALID_PARAM);
1333
1334	endpoint = uxfer->endpoint;
1335
1336	if (endpoint > 255)
1337		return (LIBUSB_ERROR_INVALID_PARAM);
1338
1339	dev = libusb_get_device(uxfer->dev_handle);
1340
1341	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1342
1343	sxfer = (struct libusb_super_transfer *)(
1344	    (uint8_t *)uxfer - sizeof(*sxfer));
1345
1346	CTX_LOCK(dev->ctx);
1347
1348	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1349	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1350
1351	if (pxfer0 == NULL || pxfer1 == NULL) {
1352		err = LIBUSB_ERROR_OTHER;
1353	} else if ((sxfer->entry.tqe_prev != NULL) ||
1354	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1355	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1356		err = LIBUSB_ERROR_BUSY;
1357	} else {
1358
1359		/* set pending state */
1360		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1361
1362		/* insert transfer into transfer head list */
1363		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1364
1365		/* start work transfers */
1366		libusb10_submit_transfer_sub(
1367		    uxfer->dev_handle, endpoint);
1368
1369		err = 0;		/* success */
1370	}
1371
1372	CTX_UNLOCK(dev->ctx);
1373
1374	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1375
1376	return (err);
1377}
1378
1379/* Asynchronous transfer cancel */
1380
1381int
1382libusb_cancel_transfer(struct libusb_transfer *uxfer)
1383{
1384	struct libusb20_transfer *pxfer0;
1385	struct libusb20_transfer *pxfer1;
1386	struct libusb_super_transfer *sxfer;
1387	struct libusb_device *dev;
1388	uint32_t endpoint;
1389	int retval;
1390
1391	if (uxfer == NULL)
1392		return (LIBUSB_ERROR_INVALID_PARAM);
1393
1394	/* check if not initialised */
1395	if (uxfer->dev_handle == NULL)
1396		return (LIBUSB_ERROR_NOT_FOUND);
1397
1398	endpoint = uxfer->endpoint;
1399
1400	if (endpoint > 255)
1401		return (LIBUSB_ERROR_INVALID_PARAM);
1402
1403	dev = libusb_get_device(uxfer->dev_handle);
1404
1405	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1406
1407	sxfer = (struct libusb_super_transfer *)(
1408	    (uint8_t *)uxfer - sizeof(*sxfer));
1409
1410	retval = 0;
1411
1412	CTX_LOCK(dev->ctx);
1413
1414	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1415	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1416
1417	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1418		/* only update the transfer status */
1419		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1420		retval = LIBUSB_ERROR_NOT_FOUND;
1421	} else if (sxfer->entry.tqe_prev != NULL) {
1422		/* we are lucky - transfer is on a queue */
1423		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1424		sxfer->entry.tqe_prev = NULL;
1425		libusb10_complete_transfer(NULL,
1426		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1427	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1428		/* not started */
1429		retval = LIBUSB_ERROR_NOT_FOUND;
1430	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1431		libusb10_complete_transfer(pxfer0,
1432		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1433		libusb20_tr_stop(pxfer0);
1434		/* make sure the queue doesn't stall */
1435		libusb10_submit_transfer_sub(
1436		    uxfer->dev_handle, endpoint);
1437	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1438		libusb10_complete_transfer(pxfer1,
1439		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1440		libusb20_tr_stop(pxfer1);
1441		/* make sure the queue doesn't stall */
1442		libusb10_submit_transfer_sub(
1443		    uxfer->dev_handle, endpoint);
1444	} else {
1445		/* not started */
1446		retval = LIBUSB_ERROR_NOT_FOUND;
1447	}
1448
1449	CTX_UNLOCK(dev->ctx);
1450
1451	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1452
1453	return (retval);
1454}
1455
1456UNEXPORTED void
1457libusb10_cancel_all_transfer(libusb_device *dev)
1458{
1459	/* TODO */
1460}
1461
1462uint16_t
1463libusb_cpu_to_le16(uint16_t x)
1464{
1465	return (htole16(x));
1466}
1467
1468uint16_t
1469libusb_le16_to_cpu(uint16_t x)
1470{
1471	return (le16toh(x));
1472}
1473
1474const char *
1475libusb_strerror(int code)
1476{
1477	switch (code) {
1478	case LIBUSB_SUCCESS:
1479		return ("Success");
1480	case LIBUSB_ERROR_IO:
1481		return ("I/O error");
1482	case LIBUSB_ERROR_INVALID_PARAM:
1483		return ("Invalid parameter");
1484	case LIBUSB_ERROR_ACCESS:
1485		return ("Permissions error");
1486	case LIBUSB_ERROR_NO_DEVICE:
1487		return ("No device");
1488	case LIBUSB_ERROR_NOT_FOUND:
1489		return ("Not found");
1490	case LIBUSB_ERROR_BUSY:
1491		return ("Device busy");
1492	case LIBUSB_ERROR_TIMEOUT:
1493		return ("Timeout");
1494	case LIBUSB_ERROR_OVERFLOW:
1495		return ("Overflow");
1496	case LIBUSB_ERROR_PIPE:
1497		return ("Pipe error");
1498	case LIBUSB_ERROR_INTERRUPTED:
1499		return ("Interrupted");
1500	case LIBUSB_ERROR_NO_MEM:
1501		return ("Out of memory");
1502	case LIBUSB_ERROR_NOT_SUPPORTED:
1503		return ("Not supported");
1504	case LIBUSB_ERROR_OTHER:
1505		return ("Other error");
1506	default:
1507		return ("Unknown error");
1508	}
1509}
1510
1511const char *
1512libusb_error_name(int code)
1513{
1514	switch (code) {
1515	case LIBUSB_SUCCESS:
1516		return ("LIBUSB_SUCCESS");
1517	case LIBUSB_ERROR_IO:
1518		return ("LIBUSB_ERROR_IO");
1519	case LIBUSB_ERROR_INVALID_PARAM:
1520		return ("LIBUSB_ERROR_INVALID_PARAM");
1521	case LIBUSB_ERROR_ACCESS:
1522		return ("LIBUSB_ERROR_ACCESS");
1523	case LIBUSB_ERROR_NO_DEVICE:
1524		return ("LIBUSB_ERROR_NO_DEVICE");
1525	case LIBUSB_ERROR_NOT_FOUND:
1526		return ("LIBUSB_ERROR_NOT_FOUND");
1527	case LIBUSB_ERROR_BUSY:
1528		return ("LIBUSB_ERROR_BUSY");
1529	case LIBUSB_ERROR_TIMEOUT:
1530		return ("LIBUSB_ERROR_TIMEOUT");
1531	case LIBUSB_ERROR_OVERFLOW:
1532		return ("LIBUSB_ERROR_OVERFLOW");
1533	case LIBUSB_ERROR_PIPE:
1534		return ("LIBUSB_ERROR_PIPE");
1535	case LIBUSB_ERROR_INTERRUPTED:
1536		return ("LIBUSB_ERROR_INTERRUPTED");
1537	case LIBUSB_ERROR_NO_MEM:
1538		return ("LIBUSB_ERROR_NO_MEM");
1539	case LIBUSB_ERROR_NOT_SUPPORTED:
1540		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1541	case LIBUSB_ERROR_OTHER:
1542		return ("LIBUSB_ERROR_OTHER");
1543	default:
1544		return ("LIBUSB_ERROR_UNKNOWN");
1545	}
1546}
1547