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