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