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