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