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