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