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