libusb10.c revision 301956
1/* $FreeBSD: head/lib/libusb/libusb10.c 301956 2016-06-16 07:48:21Z hselasky $ */
2/*-
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29#include LIBUSB_GLOBAL_INCLUDE_FILE
30#else
31#include <assert.h>
32#include <errno.h>
33#include <poll.h>
34#include <pthread.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include <time.h>
40#include <sys/fcntl.h>
41#include <sys/ioctl.h>
42#include <sys/queue.h>
43#include <sys/endian.h>
44#endif
45
46#define	libusb_device_handle libusb20_device
47
48#include "libusb20.h"
49#include "libusb20_desc.h"
50#include "libusb20_int.h"
51#include "libusb.h"
52#include "libusb10.h"
53
54#define	LIBUSB_NUM_SW_ENDPOINTS	(16 * 4)
55
56static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
57struct libusb_context *usbi_default_context = NULL;
58
59/* Prototypes */
60
61static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
62static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
63static int libusb10_convert_error(uint8_t status);
64static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
65static void libusb10_isoc_proxy(struct libusb20_transfer *);
66static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
67static void libusb10_ctrl_proxy(struct libusb20_transfer *);
68static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
69
70/*  Library initialisation / deinitialisation */
71
72static const struct libusb_version libusb_version = {
73	.major = 1,
74	.minor = 0,
75	.micro = 0,
76	.nano = 2016,
77	.rc = "",
78	.describe = "http://www.freebsd.org"
79};
80
81const struct libusb_version *
82libusb_get_version(void)
83{
84
85	return (&libusb_version);
86}
87
88void
89libusb_set_debug(libusb_context *ctx, int level)
90{
91	ctx = GET_CONTEXT(ctx);
92	if (ctx)
93		ctx->debug = level;
94}
95
96static void
97libusb_set_nonblocking(int f)
98{
99	int flags;
100
101	/*
102	 * We ignore any failures in this function, hence the
103	 * non-blocking flag is not critical to the operation of
104	 * libUSB. We use F_GETFL and F_SETFL to be compatible with
105	 * Linux.
106	 */
107
108	flags = fcntl(f, F_GETFL, NULL);
109	if (flags == -1)
110		return;
111	flags |= O_NONBLOCK;
112	fcntl(f, F_SETFL, flags);
113}
114
115int
116libusb_init(libusb_context **context)
117{
118	struct libusb_context *ctx;
119	pthread_condattr_t attr;
120	char *debug;
121	int ret;
122
123	ctx = malloc(sizeof(*ctx));
124	if (!ctx)
125		return (LIBUSB_ERROR_INVALID_PARAM);
126
127	memset(ctx, 0, sizeof(*ctx));
128
129	debug = getenv("LIBUSB_DEBUG");
130	if (debug != NULL) {
131		ctx->debug = atoi(debug);
132		if (ctx->debug != 0)
133			ctx->debug_fixed = 1;
134	}
135	TAILQ_INIT(&ctx->pollfds);
136	TAILQ_INIT(&ctx->tr_done);
137
138	if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
139		free(ctx);
140		return (LIBUSB_ERROR_NO_MEM);
141	}
142	if (pthread_condattr_init(&attr) != 0) {
143		pthread_mutex_destroy(&ctx->ctx_lock);
144		free(ctx);
145		return (LIBUSB_ERROR_NO_MEM);
146	}
147	if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
148		pthread_mutex_destroy(&ctx->ctx_lock);
149		pthread_condattr_destroy(&attr);
150		free(ctx);
151		return (LIBUSB_ERROR_OTHER);
152	}
153	if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
154		pthread_mutex_destroy(&ctx->ctx_lock);
155		pthread_condattr_destroy(&attr);
156		free(ctx);
157		return (LIBUSB_ERROR_NO_MEM);
158	}
159	pthread_condattr_destroy(&attr);
160
161	ctx->ctx_handler = NO_THREAD;
162
163	ret = pipe(ctx->ctrl_pipe);
164	if (ret < 0) {
165		pthread_mutex_destroy(&ctx->ctx_lock);
166		pthread_cond_destroy(&ctx->ctx_cond);
167		free(ctx);
168		return (LIBUSB_ERROR_OTHER);
169	}
170	/* set non-blocking mode on the control pipe to avoid deadlock */
171	libusb_set_nonblocking(ctx->ctrl_pipe[0]);
172	libusb_set_nonblocking(ctx->ctrl_pipe[1]);
173
174	libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
175
176	pthread_mutex_lock(&default_context_lock);
177	if (usbi_default_context == NULL) {
178		usbi_default_context = ctx;
179	}
180	pthread_mutex_unlock(&default_context_lock);
181
182	if (context)
183		*context = ctx;
184
185	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
186
187	return (0);
188}
189
190void
191libusb_exit(libusb_context *ctx)
192{
193	ctx = GET_CONTEXT(ctx);
194
195	if (ctx == NULL)
196		return;
197
198	/* XXX cleanup devices */
199
200	libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
201	close(ctx->ctrl_pipe[0]);
202	close(ctx->ctrl_pipe[1]);
203	pthread_mutex_destroy(&ctx->ctx_lock);
204	pthread_cond_destroy(&ctx->ctx_cond);
205
206	pthread_mutex_lock(&default_context_lock);
207	if (ctx == usbi_default_context) {
208		usbi_default_context = NULL;
209	}
210	pthread_mutex_unlock(&default_context_lock);
211
212	free(ctx);
213}
214
215/* Device handling and initialisation. */
216
217ssize_t
218libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
219{
220	struct libusb20_backend *usb_backend;
221	struct libusb20_device *pdev;
222	struct libusb_device *dev;
223	int i;
224
225	ctx = GET_CONTEXT(ctx);
226
227	if (ctx == NULL)
228		return (LIBUSB_ERROR_INVALID_PARAM);
229
230	if (list == NULL)
231		return (LIBUSB_ERROR_INVALID_PARAM);
232
233	usb_backend = libusb20_be_alloc_default();
234	if (usb_backend == NULL)
235		return (LIBUSB_ERROR_NO_MEM);
236
237	/* figure out how many USB devices are present */
238	pdev = NULL;
239	i = 0;
240	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
241		i++;
242
243	/* allocate device pointer list */
244	*list = malloc((i + 1) * sizeof(void *));
245	if (*list == NULL) {
246		libusb20_be_free(usb_backend);
247		return (LIBUSB_ERROR_NO_MEM);
248	}
249	/* create libusb v1.0 compliant devices */
250	i = 0;
251	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
252
253		dev = malloc(sizeof(*dev));
254		if (dev == NULL) {
255			while (i != 0) {
256				libusb_unref_device((*list)[i - 1]);
257				i--;
258			}
259			free(*list);
260			*list = NULL;
261			libusb20_be_free(usb_backend);
262			return (LIBUSB_ERROR_NO_MEM);
263		}
264		/* get device into libUSB v1.0 list */
265		libusb20_be_dequeue_device(usb_backend, pdev);
266
267		memset(dev, 0, sizeof(*dev));
268
269		/* init transfer queues */
270		TAILQ_INIT(&dev->tr_head);
271
272		/* set context we belong to */
273		dev->ctx = ctx;
274
275		/* link together the two structures */
276		dev->os_priv = pdev;
277		pdev->privLuData = dev;
278
279		(*list)[i] = libusb_ref_device(dev);
280		i++;
281	}
282	(*list)[i] = NULL;
283
284	libusb20_be_free(usb_backend);
285	return (i);
286}
287
288void
289libusb_free_device_list(libusb_device **list, int unref_devices)
290{
291	int i;
292
293	if (list == NULL)
294		return;			/* be NULL safe */
295
296	if (unref_devices) {
297		for (i = 0; list[i] != NULL; i++)
298			libusb_unref_device(list[i]);
299	}
300	free(list);
301}
302
303uint8_t
304libusb_get_bus_number(libusb_device *dev)
305{
306	if (dev == NULL)
307		return (0);		/* should not happen */
308	return (libusb20_dev_get_bus_number(dev->os_priv));
309}
310
311int
312libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
313{
314	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
315}
316
317int
318libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
319    uint8_t bufsize)
320{
321	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
322}
323
324uint8_t
325libusb_get_device_address(libusb_device *dev)
326{
327	if (dev == NULL)
328		return (0);		/* should not happen */
329	return (libusb20_dev_get_address(dev->os_priv));
330}
331
332enum libusb_speed
333libusb_get_device_speed(libusb_device *dev)
334{
335	if (dev == NULL)
336		return (LIBUSB_SPEED_UNKNOWN);	/* should not happen */
337
338	switch (libusb20_dev_get_speed(dev->os_priv)) {
339	case LIBUSB20_SPEED_LOW:
340		return (LIBUSB_SPEED_LOW);
341	case LIBUSB20_SPEED_FULL:
342		return (LIBUSB_SPEED_FULL);
343	case LIBUSB20_SPEED_HIGH:
344		return (LIBUSB_SPEED_HIGH);
345	case LIBUSB20_SPEED_SUPER:
346		return (LIBUSB_SPEED_SUPER);
347	default:
348		break;
349	}
350	return (LIBUSB_SPEED_UNKNOWN);
351}
352
353int
354libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
355{
356	struct libusb_config_descriptor *pdconf;
357	struct libusb_interface *pinf;
358	struct libusb_interface_descriptor *pdinf;
359	struct libusb_endpoint_descriptor *pdend;
360	int i;
361	int j;
362	int k;
363	int ret;
364
365	if (dev == NULL)
366		return (LIBUSB_ERROR_NO_DEVICE);
367
368	ret = libusb_get_active_config_descriptor(dev, &pdconf);
369	if (ret < 0)
370		return (ret);
371
372	ret = LIBUSB_ERROR_NOT_FOUND;
373	for (i = 0; i < pdconf->bNumInterfaces; i++) {
374		pinf = &pdconf->interface[i];
375		for (j = 0; j < pinf->num_altsetting; j++) {
376			pdinf = &pinf->altsetting[j];
377			for (k = 0; k < pdinf->bNumEndpoints; k++) {
378				pdend = &pdinf->endpoint[k];
379				if (pdend->bEndpointAddress == endpoint) {
380					ret = pdend->wMaxPacketSize;
381					goto out;
382				}
383			}
384		}
385	}
386
387out:
388	libusb_free_config_descriptor(pdconf);
389	return (ret);
390}
391
392int
393libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
394{
395	int multiplier;
396	int ret;
397
398	ret = libusb_get_max_packet_size(dev, endpoint);
399
400	switch (libusb20_dev_get_speed(dev->os_priv)) {
401	case LIBUSB20_SPEED_LOW:
402	case LIBUSB20_SPEED_FULL:
403		break;
404	default:
405		if (ret > -1) {
406			multiplier = (1 + ((ret >> 11) & 3));
407			if (multiplier > 3)
408				multiplier = 3;
409			ret = (ret & 0x7FF) * multiplier;
410		}
411		break;
412	}
413	return (ret);
414}
415
416libusb_device *
417libusb_ref_device(libusb_device *dev)
418{
419	if (dev == NULL)
420		return (NULL);		/* be NULL safe */
421
422	CTX_LOCK(dev->ctx);
423	dev->refcnt++;
424	CTX_UNLOCK(dev->ctx);
425
426	return (dev);
427}
428
429void
430libusb_unref_device(libusb_device *dev)
431{
432	if (dev == NULL)
433		return;			/* be NULL safe */
434
435	CTX_LOCK(dev->ctx);
436	dev->refcnt--;
437	CTX_UNLOCK(dev->ctx);
438
439	if (dev->refcnt == 0) {
440		libusb20_dev_free(dev->os_priv);
441		free(dev);
442	}
443}
444
445int
446libusb_open(libusb_device *dev, libusb_device_handle **devh)
447{
448	libusb_context *ctx = dev->ctx;
449	struct libusb20_device *pdev = dev->os_priv;
450	uint8_t dummy;
451	int err;
452
453	if (devh == NULL)
454		return (LIBUSB_ERROR_INVALID_PARAM);
455
456	/* set default device handle value */
457	*devh = NULL;
458
459	dev = libusb_ref_device(dev);
460	if (dev == NULL)
461		return (LIBUSB_ERROR_INVALID_PARAM);
462
463	err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
464	if (err) {
465		libusb_unref_device(dev);
466		return (LIBUSB_ERROR_NO_MEM);
467	}
468	libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
469	    POLLOUT | POLLRDNORM | POLLWRNORM);
470
471	/* make sure our event loop detects the new device */
472	dummy = 0;
473	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
474	if (err < (int)sizeof(dummy)) {
475		/* ignore error, if any */
476		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
477	}
478	*devh = pdev;
479
480	return (0);
481}
482
483libusb_device_handle *
484libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
485    uint16_t product_id)
486{
487	struct libusb_device **devs;
488	struct libusb20_device *pdev;
489	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
490	int i;
491	int j;
492
493	ctx = GET_CONTEXT(ctx);
494	if (ctx == NULL)
495		return (NULL);		/* be NULL safe */
496
497	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
498
499	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
500		return (NULL);
501
502	pdev = NULL;
503	for (j = 0; j < i; j++) {
504		struct libusb20_device *tdev;
505
506		tdev = devs[j]->os_priv;
507		pdesc = libusb20_dev_get_device_desc(tdev);
508		/*
509		 * NOTE: The USB library will automatically swap the
510		 * fields in the device descriptor to be of host
511		 * endian type!
512		 */
513		if (pdesc->idVendor == vendor_id &&
514		    pdesc->idProduct == product_id) {
515			libusb_open(devs[j], &pdev);
516			break;
517		}
518	}
519
520	libusb_free_device_list(devs, 1);
521	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
522	return (pdev);
523}
524
525void
526libusb_close(struct libusb20_device *pdev)
527{
528	libusb_context *ctx;
529	struct libusb_device *dev;
530	uint8_t dummy;
531	int err;
532
533	if (pdev == NULL)
534		return;			/* be NULL safe */
535
536	dev = libusb_get_device(pdev);
537	ctx = dev->ctx;
538
539	libusb10_remove_pollfd(ctx, &dev->dev_poll);
540
541	libusb20_dev_close(pdev);
542
543	/* unref will free the "pdev" when the refcount reaches zero */
544	libusb_unref_device(dev);
545
546	/* make sure our event loop detects the closed device */
547	dummy = 0;
548	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
549	if (err < (int)sizeof(dummy)) {
550		/* ignore error, if any */
551		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
552	}
553}
554
555libusb_device *
556libusb_get_device(struct libusb20_device *pdev)
557{
558	if (pdev == NULL)
559		return (NULL);
560	return ((libusb_device *)pdev->privLuData);
561}
562
563int
564libusb_get_configuration(struct libusb20_device *pdev, int *config)
565{
566	struct libusb20_config *pconf;
567
568	if (pdev == NULL || config == NULL)
569		return (LIBUSB_ERROR_INVALID_PARAM);
570
571	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
572	if (pconf == NULL)
573		return (LIBUSB_ERROR_NO_MEM);
574
575	*config = pconf->desc.bConfigurationValue;
576
577	free(pconf);
578
579	return (0);
580}
581
582int
583libusb_set_configuration(struct libusb20_device *pdev, int configuration)
584{
585	struct libusb20_config *pconf;
586	struct libusb_device *dev;
587	int err;
588	uint8_t i;
589
590	dev = libusb_get_device(pdev);
591	if (dev == NULL)
592		return (LIBUSB_ERROR_INVALID_PARAM);
593
594	if (configuration < 1) {
595		/* unconfigure */
596		i = 255;
597	} else {
598		for (i = 0; i != 255; i++) {
599			uint8_t found;
600
601			pconf = libusb20_dev_alloc_config(pdev, i);
602			if (pconf == NULL)
603				return (LIBUSB_ERROR_INVALID_PARAM);
604			found = (pconf->desc.bConfigurationValue
605			    == configuration);
606			free(pconf);
607
608			if (found)
609				goto set_config;
610		}
611		return (LIBUSB_ERROR_INVALID_PARAM);
612	}
613
614set_config:
615
616	libusb10_cancel_all_transfer(dev);
617
618	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
619
620	err = libusb20_dev_set_config_index(pdev, i);
621
622	libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
623	    POLLOUT | POLLRDNORM | POLLWRNORM);
624
625	return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
626}
627
628int
629libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
630{
631	libusb_device *dev;
632
633	dev = libusb_get_device(pdev);
634	if (dev == NULL)
635		return (LIBUSB_ERROR_INVALID_PARAM);
636
637	if (interface_number < 0 || interface_number > 31)
638		return (LIBUSB_ERROR_INVALID_PARAM);
639
640	CTX_LOCK(dev->ctx);
641	dev->claimed_interfaces |= (1 << interface_number);
642	CTX_UNLOCK(dev->ctx);
643
644	return (0);
645}
646
647int
648libusb_release_interface(struct libusb20_device *pdev, int interface_number)
649{
650	libusb_device *dev;
651	int err = 0;
652
653	dev = libusb_get_device(pdev);
654	if (dev == NULL)
655		return (LIBUSB_ERROR_INVALID_PARAM);
656
657	if (interface_number < 0 || interface_number > 31)
658		return (LIBUSB_ERROR_INVALID_PARAM);
659
660	CTX_LOCK(dev->ctx);
661	if (!(dev->claimed_interfaces & (1 << interface_number)))
662		err = LIBUSB_ERROR_NOT_FOUND;
663
664	if (!err)
665		dev->claimed_interfaces &= ~(1 << interface_number);
666	CTX_UNLOCK(dev->ctx);
667	return (err);
668}
669
670int
671libusb_set_interface_alt_setting(struct libusb20_device *pdev,
672    int interface_number, int alternate_setting)
673{
674	libusb_device *dev;
675	int err = 0;
676
677	dev = libusb_get_device(pdev);
678	if (dev == NULL)
679		return (LIBUSB_ERROR_INVALID_PARAM);
680
681	if (interface_number < 0 || interface_number > 31)
682		return (LIBUSB_ERROR_INVALID_PARAM);
683
684	CTX_LOCK(dev->ctx);
685	if (!(dev->claimed_interfaces & (1 << interface_number)))
686		err = LIBUSB_ERROR_NOT_FOUND;
687	CTX_UNLOCK(dev->ctx);
688
689	if (err)
690		return (err);
691
692	libusb10_cancel_all_transfer(dev);
693
694	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
695
696	err = libusb20_dev_set_alt_index(pdev,
697	    interface_number, alternate_setting);
698
699	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
700	    pdev, libusb20_dev_get_fd(pdev),
701	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
702
703	return (err ? LIBUSB_ERROR_OTHER : 0);
704}
705
706static struct libusb20_transfer *
707libusb10_get_transfer(struct libusb20_device *pdev,
708    uint8_t endpoint, uint8_t xfer_index)
709{
710	xfer_index &= 1;	/* double buffering */
711
712	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
713
714	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
715		/* this is an IN endpoint */
716		xfer_index |= 2;
717	}
718	return (libusb20_tr_get_pointer(pdev, xfer_index));
719}
720
721int
722libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
723{
724	struct libusb20_transfer *xfer;
725	struct libusb_device *dev;
726	int err;
727
728	xfer = libusb10_get_transfer(pdev, endpoint, 0);
729	if (xfer == NULL)
730		return (LIBUSB_ERROR_INVALID_PARAM);
731
732	dev = libusb_get_device(pdev);
733	if (dev == NULL)
734		return (LIBUSB_ERROR_INVALID_PARAM);
735
736	CTX_LOCK(dev->ctx);
737	err = libusb20_tr_open(xfer, 0, 1, endpoint);
738	CTX_UNLOCK(dev->ctx);
739
740	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
741		return (LIBUSB_ERROR_OTHER);
742
743	libusb20_tr_clear_stall_sync(xfer);
744
745	/* check if we opened the transfer */
746	if (err == 0) {
747		CTX_LOCK(dev->ctx);
748		libusb20_tr_close(xfer);
749		CTX_UNLOCK(dev->ctx);
750	}
751	return (0);			/* success */
752}
753
754int
755libusb_reset_device(struct libusb20_device *pdev)
756{
757	libusb_device *dev;
758	int err;
759
760	dev = libusb_get_device(pdev);
761	if (dev == NULL)
762		return (LIBUSB_ERROR_INVALID_PARAM);
763
764	libusb10_cancel_all_transfer(dev);
765
766	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
767
768	err = libusb20_dev_reset(pdev);
769
770	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
771	    pdev, libusb20_dev_get_fd(pdev),
772	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
773
774	return (err ? LIBUSB_ERROR_OTHER : 0);
775}
776
777int
778libusb_check_connected(struct libusb20_device *pdev)
779{
780	libusb_device *dev;
781	int err;
782
783	dev = libusb_get_device(pdev);
784	if (dev == NULL)
785		return (LIBUSB_ERROR_INVALID_PARAM);
786
787	err = libusb20_dev_check_connected(pdev);
788
789	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
790}
791
792int
793libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
794{
795	if (pdev == NULL)
796		return (LIBUSB_ERROR_INVALID_PARAM);
797
798	if (libusb20_dev_kernel_driver_active(pdev, interface))
799		return (0);		/* no kernel driver is active */
800	else
801		return (1);		/* kernel driver is active */
802}
803
804int
805libusb_get_driver_np(struct libusb20_device *pdev, int interface,
806    char *name, int namelen)
807{
808	return (libusb_get_driver(pdev, interface, name, namelen));
809}
810
811int
812libusb_get_driver(struct libusb20_device *pdev, int interface,
813    char *name, int namelen)
814{
815	char *ptr;
816	int err;
817
818	if (pdev == NULL)
819		return (LIBUSB_ERROR_INVALID_PARAM);
820	if (namelen < 1)
821		return (LIBUSB_ERROR_INVALID_PARAM);
822	if (namelen > 255)
823		namelen = 255;
824
825	err = libusb20_dev_get_iface_desc(
826	    pdev, interface, name, namelen);
827
828	if (err != 0)
829		return (LIBUSB_ERROR_OTHER);
830
831	/* we only want the driver name */
832	ptr = strstr(name, ":");
833	if (ptr != NULL)
834		*ptr = 0;
835
836	return (0);
837}
838
839int
840libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
841{
842	return (libusb_detach_kernel_driver(pdev, interface));
843}
844
845int
846libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
847{
848	int err;
849
850	if (pdev == NULL)
851		return (LIBUSB_ERROR_INVALID_PARAM);
852
853	err = libusb20_dev_detach_kernel_driver(
854	    pdev, interface);
855
856	return (err ? LIBUSB_ERROR_OTHER : 0);
857}
858
859int
860libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
861{
862	if (pdev == NULL)
863		return (LIBUSB_ERROR_INVALID_PARAM);
864	/* stub - currently not supported by libusb20 */
865	return (0);
866}
867
868/* Asynchronous device I/O */
869
870struct libusb_transfer *
871libusb_alloc_transfer(int iso_packets)
872{
873	struct libusb_transfer *uxfer;
874	struct libusb_super_transfer *sxfer;
875	int len;
876
877	len = sizeof(struct libusb_transfer) +
878	    sizeof(struct libusb_super_transfer) +
879	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
880
881	sxfer = malloc(len);
882	if (sxfer == NULL)
883		return (NULL);
884
885	memset(sxfer, 0, len);
886
887	uxfer = (struct libusb_transfer *)(
888	    ((uint8_t *)sxfer) + sizeof(*sxfer));
889
890	/* set default value */
891	uxfer->num_iso_packets = iso_packets;
892
893	return (uxfer);
894}
895
896void
897libusb_free_transfer(struct libusb_transfer *uxfer)
898{
899	struct libusb_super_transfer *sxfer;
900
901	if (uxfer == NULL)
902		return;			/* be NULL safe */
903
904	/* check if we should free the transfer buffer */
905	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
906		free(uxfer->buffer);
907
908	sxfer = (struct libusb_super_transfer *)(
909	    (uint8_t *)uxfer - sizeof(*sxfer));
910
911	free(sxfer);
912}
913
914static uint32_t
915libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
916{
917	uint32_t ret;
918
919	switch (xfer->type) {
920	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
921		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
922		break;
923	case LIBUSB_TRANSFER_TYPE_CONTROL:
924		ret = 2;
925		break;
926	default:
927		ret = 1;
928		break;
929	}
930	return (ret);
931}
932
933static int
934libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
935{
936	int ret;
937	int usb_speed;
938
939	usb_speed = libusb20_dev_get_speed(pdev);
940
941	switch (xfer->type) {
942	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
943		ret = 0;		/* kernel will auto-select */
944		break;
945	case LIBUSB_TRANSFER_TYPE_CONTROL:
946		ret = 1024;
947		break;
948	default:
949		switch (usb_speed) {
950		case LIBUSB20_SPEED_LOW:
951			ret = 256;
952			break;
953		case LIBUSB20_SPEED_FULL:
954			ret = 4096;
955			break;
956		case LIBUSB20_SPEED_SUPER:
957			ret = 65536;
958			break;
959		default:
960			ret = 16384;
961			break;
962		}
963		break;
964	}
965	return (ret);
966}
967
968static int
969libusb10_convert_error(uint8_t status)
970{
971	;				/* indent fix */
972
973	switch (status) {
974	case LIBUSB20_TRANSFER_START:
975	case LIBUSB20_TRANSFER_COMPLETED:
976		return (LIBUSB_TRANSFER_COMPLETED);
977	case LIBUSB20_TRANSFER_OVERFLOW:
978		return (LIBUSB_TRANSFER_OVERFLOW);
979	case LIBUSB20_TRANSFER_NO_DEVICE:
980		return (LIBUSB_TRANSFER_NO_DEVICE);
981	case LIBUSB20_TRANSFER_STALL:
982		return (LIBUSB_TRANSFER_STALL);
983	case LIBUSB20_TRANSFER_CANCELLED:
984		return (LIBUSB_TRANSFER_CANCELLED);
985	case LIBUSB20_TRANSFER_TIMED_OUT:
986		return (LIBUSB_TRANSFER_TIMED_OUT);
987	default:
988		return (LIBUSB_TRANSFER_ERROR);
989	}
990}
991
992/* This function must be called locked */
993
994static void
995libusb10_complete_transfer(struct libusb20_transfer *pxfer,
996    struct libusb_super_transfer *sxfer, int status)
997{
998	struct libusb_transfer *uxfer;
999	struct libusb_device *dev;
1000
1001	uxfer = (struct libusb_transfer *)(
1002	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1003
1004	if (pxfer != NULL)
1005		libusb20_tr_set_priv_sc1(pxfer, NULL);
1006
1007	/* set transfer status */
1008	uxfer->status = status;
1009
1010	/* update super transfer state */
1011	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1012
1013	dev = libusb_get_device(uxfer->dev_handle);
1014
1015	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1016}
1017
1018/* This function must be called locked */
1019
1020static void
1021libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1022{
1023	struct libusb_super_transfer *sxfer;
1024	struct libusb_transfer *uxfer;
1025	uint32_t actlen;
1026	uint16_t iso_packets;
1027	uint16_t i;
1028	uint8_t status;
1029
1030	status = libusb20_tr_get_status(pxfer);
1031	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1032	actlen = libusb20_tr_get_actual_length(pxfer);
1033	iso_packets = libusb20_tr_get_max_frames(pxfer);
1034
1035	if (sxfer == NULL)
1036		return; /* cancelled - nothing to do */
1037
1038	uxfer = (struct libusb_transfer *)(
1039	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1040
1041	if (iso_packets > uxfer->num_iso_packets)
1042		iso_packets = uxfer->num_iso_packets;
1043
1044	if (iso_packets == 0)
1045		return; /* nothing to do */
1046
1047	/* make sure that the number of ISOCHRONOUS packets is valid */
1048	uxfer->num_iso_packets = iso_packets;
1049
1050	switch (status) {
1051	case LIBUSB20_TRANSFER_COMPLETED:
1052		/* update actual length */
1053		uxfer->actual_length = actlen;
1054		for (i = 0; i != iso_packets; i++) {
1055			uxfer->iso_packet_desc[i].actual_length =
1056			    libusb20_tr_get_length(pxfer, i);
1057		}
1058		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1059		break;
1060	case LIBUSB20_TRANSFER_START:
1061		/* setup length(s) */
1062		actlen = 0;
1063		for (i = 0; i != iso_packets; i++) {
1064			libusb20_tr_setup_isoc(pxfer,
1065			    &uxfer->buffer[actlen],
1066			    uxfer->iso_packet_desc[i].length, i);
1067			actlen += uxfer->iso_packet_desc[i].length;
1068		}
1069
1070		/* no remainder */
1071		sxfer->rem_len = 0;
1072
1073		libusb20_tr_set_total_frames(pxfer, iso_packets);
1074		libusb20_tr_submit(pxfer);
1075
1076		/* fork another USB transfer, if any */
1077		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1078		break;
1079	default:
1080		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1081		break;
1082	}
1083}
1084
1085/* This function must be called locked */
1086
1087static void
1088libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1089{
1090	struct libusb_super_transfer *sxfer;
1091	struct libusb_transfer *uxfer;
1092	uint32_t max_bulk;
1093	uint32_t actlen;
1094	uint8_t status;
1095	uint8_t flags;
1096
1097	status = libusb20_tr_get_status(pxfer);
1098	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1099	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1100	actlen = libusb20_tr_get_actual_length(pxfer);
1101
1102	if (sxfer == NULL)
1103		return;			/* cancelled - nothing to do */
1104
1105	uxfer = (struct libusb_transfer *)(
1106	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1107
1108	flags = uxfer->flags;
1109
1110	switch (status) {
1111	case LIBUSB20_TRANSFER_COMPLETED:
1112
1113		uxfer->actual_length += actlen;
1114
1115		/* check for short packet */
1116		if (sxfer->last_len != actlen) {
1117			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1118				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1119			} else {
1120				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1121			}
1122			break;
1123		}
1124		/* check for end of data */
1125		if (sxfer->rem_len == 0) {
1126			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1127			break;
1128		}
1129		/* FALLTHROUGH */
1130
1131	case LIBUSB20_TRANSFER_START:
1132		if (max_bulk > sxfer->rem_len) {
1133			max_bulk = sxfer->rem_len;
1134		}
1135		/* setup new BULK or INTERRUPT transaction */
1136		libusb20_tr_setup_bulk(pxfer,
1137		    sxfer->curr_data, max_bulk, uxfer->timeout);
1138
1139		/* update counters */
1140		sxfer->last_len = max_bulk;
1141		sxfer->curr_data += max_bulk;
1142		sxfer->rem_len -= max_bulk;
1143
1144		libusb20_tr_submit(pxfer);
1145
1146		/* check if we can fork another USB transfer */
1147		if (sxfer->rem_len == 0)
1148			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1149		break;
1150
1151	default:
1152		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1153		break;
1154	}
1155}
1156
1157/* This function must be called locked */
1158
1159static void
1160libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1161{
1162	struct libusb_super_transfer *sxfer;
1163	struct libusb_transfer *uxfer;
1164	uint32_t max_bulk;
1165	uint32_t actlen;
1166	uint8_t status;
1167	uint8_t flags;
1168
1169	status = libusb20_tr_get_status(pxfer);
1170	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1171	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1172	actlen = libusb20_tr_get_actual_length(pxfer);
1173
1174	if (sxfer == NULL)
1175		return;			/* cancelled - nothing to do */
1176
1177	uxfer = (struct libusb_transfer *)(
1178	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1179
1180	flags = uxfer->flags;
1181
1182	switch (status) {
1183	case LIBUSB20_TRANSFER_COMPLETED:
1184
1185		uxfer->actual_length += actlen;
1186
1187		/* subtract length of SETUP packet, if any */
1188		actlen -= libusb20_tr_get_length(pxfer, 0);
1189
1190		/* check for short packet */
1191		if (sxfer->last_len != actlen) {
1192			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1193				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1194			} else {
1195				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1196			}
1197			break;
1198		}
1199		/* check for end of data */
1200		if (sxfer->rem_len == 0) {
1201			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1202			break;
1203		}
1204		/* FALLTHROUGH */
1205
1206	case LIBUSB20_TRANSFER_START:
1207		if (max_bulk > sxfer->rem_len) {
1208			max_bulk = sxfer->rem_len;
1209		}
1210		/* setup new CONTROL transaction */
1211		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1212			/* next fragment - don't send SETUP packet */
1213			libusb20_tr_set_length(pxfer, 0, 0);
1214		} else {
1215			/* first fragment - send SETUP packet */
1216			libusb20_tr_set_length(pxfer, 8, 0);
1217			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1218		}
1219
1220		if (max_bulk != 0) {
1221			libusb20_tr_set_length(pxfer, max_bulk, 1);
1222			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1223			libusb20_tr_set_total_frames(pxfer, 2);
1224		} else {
1225			libusb20_tr_set_total_frames(pxfer, 1);
1226		}
1227
1228		/* update counters */
1229		sxfer->last_len = max_bulk;
1230		sxfer->curr_data += max_bulk;
1231		sxfer->rem_len -= max_bulk;
1232
1233		libusb20_tr_submit(pxfer);
1234
1235		/* check if we can fork another USB transfer */
1236		if (sxfer->rem_len == 0)
1237			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1238		break;
1239
1240	default:
1241		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1242		break;
1243	}
1244}
1245
1246/* The following function must be called locked */
1247
1248static void
1249libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1250{
1251	struct libusb20_transfer *pxfer0;
1252	struct libusb20_transfer *pxfer1;
1253	struct libusb_super_transfer *sxfer;
1254	struct libusb_transfer *uxfer;
1255	struct libusb_device *dev;
1256	int err;
1257	int buffsize;
1258	int maxframe;
1259	int temp;
1260	uint8_t dummy;
1261
1262	dev = libusb_get_device(pdev);
1263
1264	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1265	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1266
1267	if (pxfer0 == NULL || pxfer1 == NULL)
1268		return;			/* shouldn't happen */
1269
1270	temp = 0;
1271	if (libusb20_tr_pending(pxfer0))
1272		temp |= 1;
1273	if (libusb20_tr_pending(pxfer1))
1274		temp |= 2;
1275
1276	switch (temp) {
1277	case 3:
1278		/* wait till one of the transfers complete */
1279		return;
1280	case 2:
1281		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1282		if (sxfer == NULL)
1283			return;		/* cancelling */
1284		if (sxfer->rem_len)
1285			return;		/* cannot queue another one */
1286		/* swap transfers */
1287		pxfer1 = pxfer0;
1288		break;
1289	case 1:
1290		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1291		if (sxfer == NULL)
1292			return;		/* cancelling */
1293		if (sxfer->rem_len)
1294			return;		/* cannot queue another one */
1295		/* swap transfers */
1296		pxfer0 = pxfer1;
1297		break;
1298	default:
1299		break;
1300	}
1301
1302	/* find next transfer on same endpoint */
1303	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1304
1305		uxfer = (struct libusb_transfer *)(
1306		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1307
1308		if (uxfer->endpoint == endpoint) {
1309			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1310			sxfer->entry.tqe_prev = NULL;
1311			goto found;
1312		}
1313	}
1314	return;				/* success */
1315
1316found:
1317
1318	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1319	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1320
1321	/* reset super transfer state */
1322	sxfer->rem_len = uxfer->length;
1323	sxfer->curr_data = uxfer->buffer;
1324	uxfer->actual_length = 0;
1325
1326	switch (uxfer->type) {
1327	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1328		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1329		break;
1330	case LIBUSB_TRANSFER_TYPE_BULK:
1331	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1332		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1333		break;
1334	case LIBUSB_TRANSFER_TYPE_CONTROL:
1335		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1336		if (sxfer->rem_len < 8)
1337			goto failure;
1338
1339		/* remove SETUP packet from data */
1340		sxfer->rem_len -= 8;
1341		sxfer->curr_data += 8;
1342		break;
1343	default:
1344		goto failure;
1345	}
1346
1347	buffsize = libusb10_get_buffsize(pdev, uxfer);
1348	maxframe = libusb10_get_maxframe(pdev, uxfer);
1349
1350	/* make sure the transfer is opened */
1351	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1352	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1353		goto failure;
1354	}
1355	libusb20_tr_start(pxfer0);
1356	return;
1357
1358failure:
1359	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1360
1361	/* make sure our event loop spins the done handler */
1362	dummy = 0;
1363	err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1364}
1365
1366/* The following function must be called unlocked */
1367
1368int
1369libusb_submit_transfer(struct libusb_transfer *uxfer)
1370{
1371	struct libusb20_transfer *pxfer0;
1372	struct libusb20_transfer *pxfer1;
1373	struct libusb_super_transfer *sxfer;
1374	struct libusb_device *dev;
1375	uint8_t endpoint;
1376	int err;
1377
1378	if (uxfer == NULL)
1379		return (LIBUSB_ERROR_INVALID_PARAM);
1380
1381	if (uxfer->dev_handle == NULL)
1382		return (LIBUSB_ERROR_INVALID_PARAM);
1383
1384	endpoint = uxfer->endpoint;
1385
1386	dev = libusb_get_device(uxfer->dev_handle);
1387
1388	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1389
1390	sxfer = (struct libusb_super_transfer *)(
1391	    (uint8_t *)uxfer - sizeof(*sxfer));
1392
1393	CTX_LOCK(dev->ctx);
1394
1395	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1396	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1397
1398	if (pxfer0 == NULL || pxfer1 == NULL) {
1399		err = LIBUSB_ERROR_OTHER;
1400	} else if ((sxfer->entry.tqe_prev != NULL) ||
1401	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1402	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1403		err = LIBUSB_ERROR_BUSY;
1404	} else {
1405
1406		/* set pending state */
1407		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1408
1409		/* insert transfer into transfer head list */
1410		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1411
1412		/* start work transfers */
1413		libusb10_submit_transfer_sub(
1414		    uxfer->dev_handle, endpoint);
1415
1416		err = 0;		/* success */
1417	}
1418
1419	CTX_UNLOCK(dev->ctx);
1420
1421	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1422
1423	return (err);
1424}
1425
1426/* Asynchronous transfer cancel */
1427
1428int
1429libusb_cancel_transfer(struct libusb_transfer *uxfer)
1430{
1431	struct libusb20_transfer *pxfer0;
1432	struct libusb20_transfer *pxfer1;
1433	struct libusb_super_transfer *sxfer;
1434	struct libusb_device *dev;
1435	uint8_t endpoint;
1436	int retval;
1437
1438	if (uxfer == NULL)
1439		return (LIBUSB_ERROR_INVALID_PARAM);
1440
1441	/* check if not initialised */
1442	if (uxfer->dev_handle == NULL)
1443		return (LIBUSB_ERROR_NOT_FOUND);
1444
1445	endpoint = uxfer->endpoint;
1446
1447	dev = libusb_get_device(uxfer->dev_handle);
1448
1449	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1450
1451	sxfer = (struct libusb_super_transfer *)(
1452	    (uint8_t *)uxfer - sizeof(*sxfer));
1453
1454	retval = 0;
1455
1456	CTX_LOCK(dev->ctx);
1457
1458	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1459	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1460
1461	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1462		/* only update the transfer status */
1463		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1464		retval = LIBUSB_ERROR_NOT_FOUND;
1465	} else if (sxfer->entry.tqe_prev != NULL) {
1466		/* we are lucky - transfer is on a queue */
1467		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1468		sxfer->entry.tqe_prev = NULL;
1469		libusb10_complete_transfer(NULL,
1470		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1471	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1472		/* not started */
1473		retval = LIBUSB_ERROR_NOT_FOUND;
1474	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1475		libusb10_complete_transfer(pxfer0,
1476		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1477		libusb20_tr_stop(pxfer0);
1478		/* make sure the queue doesn't stall */
1479		libusb10_submit_transfer_sub(
1480		    uxfer->dev_handle, endpoint);
1481	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1482		libusb10_complete_transfer(pxfer1,
1483		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1484		libusb20_tr_stop(pxfer1);
1485		/* make sure the queue doesn't stall */
1486		libusb10_submit_transfer_sub(
1487		    uxfer->dev_handle, endpoint);
1488	} else {
1489		/* not started */
1490		retval = LIBUSB_ERROR_NOT_FOUND;
1491	}
1492
1493	CTX_UNLOCK(dev->ctx);
1494
1495	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1496
1497	return (retval);
1498}
1499
1500UNEXPORTED void
1501libusb10_cancel_all_transfer(libusb_device *dev)
1502{
1503	struct libusb20_device *pdev = dev->os_priv;
1504	unsigned x;
1505
1506	for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1507		struct libusb20_transfer *xfer;
1508
1509		xfer = libusb20_tr_get_pointer(pdev, x);
1510		if (xfer == NULL)
1511			continue;
1512		libusb20_tr_close(xfer);
1513	}
1514}
1515
1516uint16_t
1517libusb_cpu_to_le16(uint16_t x)
1518{
1519	return (htole16(x));
1520}
1521
1522uint16_t
1523libusb_le16_to_cpu(uint16_t x)
1524{
1525	return (le16toh(x));
1526}
1527
1528const char *
1529libusb_strerror(int code)
1530{
1531	switch (code) {
1532	case LIBUSB_SUCCESS:
1533		return ("Success");
1534	case LIBUSB_ERROR_IO:
1535		return ("I/O error");
1536	case LIBUSB_ERROR_INVALID_PARAM:
1537		return ("Invalid parameter");
1538	case LIBUSB_ERROR_ACCESS:
1539		return ("Permissions error");
1540	case LIBUSB_ERROR_NO_DEVICE:
1541		return ("No device");
1542	case LIBUSB_ERROR_NOT_FOUND:
1543		return ("Not found");
1544	case LIBUSB_ERROR_BUSY:
1545		return ("Device busy");
1546	case LIBUSB_ERROR_TIMEOUT:
1547		return ("Timeout");
1548	case LIBUSB_ERROR_OVERFLOW:
1549		return ("Overflow");
1550	case LIBUSB_ERROR_PIPE:
1551		return ("Pipe error");
1552	case LIBUSB_ERROR_INTERRUPTED:
1553		return ("Interrupted");
1554	case LIBUSB_ERROR_NO_MEM:
1555		return ("Out of memory");
1556	case LIBUSB_ERROR_NOT_SUPPORTED:
1557		return ("Not supported");
1558	case LIBUSB_ERROR_OTHER:
1559		return ("Other error");
1560	default:
1561		return ("Unknown error");
1562	}
1563}
1564
1565const char *
1566libusb_error_name(int code)
1567{
1568	switch (code) {
1569	case LIBUSB_SUCCESS:
1570		return ("LIBUSB_SUCCESS");
1571	case LIBUSB_ERROR_IO:
1572		return ("LIBUSB_ERROR_IO");
1573	case LIBUSB_ERROR_INVALID_PARAM:
1574		return ("LIBUSB_ERROR_INVALID_PARAM");
1575	case LIBUSB_ERROR_ACCESS:
1576		return ("LIBUSB_ERROR_ACCESS");
1577	case LIBUSB_ERROR_NO_DEVICE:
1578		return ("LIBUSB_ERROR_NO_DEVICE");
1579	case LIBUSB_ERROR_NOT_FOUND:
1580		return ("LIBUSB_ERROR_NOT_FOUND");
1581	case LIBUSB_ERROR_BUSY:
1582		return ("LIBUSB_ERROR_BUSY");
1583	case LIBUSB_ERROR_TIMEOUT:
1584		return ("LIBUSB_ERROR_TIMEOUT");
1585	case LIBUSB_ERROR_OVERFLOW:
1586		return ("LIBUSB_ERROR_OVERFLOW");
1587	case LIBUSB_ERROR_PIPE:
1588		return ("LIBUSB_ERROR_PIPE");
1589	case LIBUSB_ERROR_INTERRUPTED:
1590		return ("LIBUSB_ERROR_INTERRUPTED");
1591	case LIBUSB_ERROR_NO_MEM:
1592		return ("LIBUSB_ERROR_NO_MEM");
1593	case LIBUSB_ERROR_NOT_SUPPORTED:
1594		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1595	case LIBUSB_ERROR_OTHER:
1596		return ("LIBUSB_ERROR_OTHER");
1597	default:
1598		return ("LIBUSB_ERROR_UNKNOWN");
1599	}
1600}
1601