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