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