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