3 * SPDX-License-Identifier: BSD-2-Clause
5 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
6 * Copyright (c) 2009-2023 Hans Petter Selasky
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
31 #include LIBUSB_GLOBAL_INCLUDE_FILE
43 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/queue.h>
46 #include <sys/endian.h>
49 #define libusb_device_handle libusb20_device
52 #include "libusb20_desc.h"
53 #include "libusb20_int.h"
57 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
59 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
60 struct libusb_context *usbi_default_context = NULL;
64 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
65 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
66 static int libusb10_convert_error(uint8_t status);
67 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
68 static void libusb10_isoc_proxy(struct libusb20_transfer *);
69 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
70 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
71 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
73 /* Library initialisation / deinitialisation */
75 static const struct libusb_version libusb_version = {
81 .describe = "https://www.freebsd.org"
84 const struct libusb_version *
85 libusb_get_version(void)
88 return (&libusb_version);
92 libusb_set_debug(libusb_context *ctx, int level)
94 ctx = GET_CONTEXT(ctx);
95 /* debug_fixed is set when the environment overrides libusb_set_debug */
96 if (ctx && ctx->debug_fixed == 0)
101 libusb_set_nonblocking(int f)
106 * We ignore any failures in this function, hence the
107 * non-blocking flag is not critical to the operation of
108 * libUSB. We use F_GETFL and F_SETFL to be compatible with
112 flags = fcntl(f, F_GETFL, NULL);
116 fcntl(f, F_SETFL, flags);
120 libusb_interrupt_event_handler(libusb_context *ctx)
129 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
130 if (err < (int)sizeof(dummy)) {
131 /* ignore error, if any */
132 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
137 libusb_init(libusb_context **context)
139 return (libusb_init_context(context, NULL, 0));
143 libusb_init_context(libusb_context **context,
144 const struct libusb_init_option option[], int num_options)
146 struct libusb_context *ctx;
147 pthread_condattr_t attr;
152 return (LIBUSB_ERROR_INVALID_PARAM);
154 ctx = malloc(sizeof(*ctx));
156 return (LIBUSB_ERROR_INVALID_PARAM);
158 memset(ctx, 0, sizeof(*ctx));
160 debug = getenv("LIBUSB_DEBUG");
163 * If LIBUSB_DEBUG is set, we'll honor that first and
164 * use it to override any future libusb_set_debug()
165 * calls or init options.
168 ctx->debug = strtol(debug, &ep, 10);
169 if (errno == 0 && *ep == '\0') {
170 ctx->debug_fixed = 1;
173 * LIBUSB_DEBUG conversion failed for some reason, but
174 * we don't care about the specifics all that much. We
175 * can't use it either way. Force it to the default,
176 * 0, in case we had a partial number.
182 * If the LIBUSB_OPTION_LOG_LEVEL is set, honor that.
184 for (int i = 0; i != num_options; i++) {
185 if (option[i].option != LIBUSB_OPTION_LOG_LEVEL)
188 ctx->debug = (int)option[i].value.ival;
189 if ((int64_t)ctx->debug == option[i].value.ival) {
190 ctx->debug_fixed = 1;
193 return (LIBUSB_ERROR_INVALID_PARAM);
198 TAILQ_INIT(&ctx->pollfds);
199 TAILQ_INIT(&ctx->tr_done);
200 TAILQ_INIT(&ctx->hotplug_cbh);
201 TAILQ_INIT(&ctx->hotplug_devs);
203 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
205 return (LIBUSB_ERROR_NO_MEM);
207 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
208 pthread_mutex_destroy(&ctx->ctx_lock);
210 return (LIBUSB_ERROR_NO_MEM);
212 if (pthread_condattr_init(&attr) != 0) {
213 pthread_mutex_destroy(&ctx->ctx_lock);
214 pthread_mutex_destroy(&ctx->hotplug_lock);
216 return (LIBUSB_ERROR_NO_MEM);
218 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
219 pthread_mutex_destroy(&ctx->ctx_lock);
220 pthread_mutex_destroy(&ctx->hotplug_lock);
221 pthread_condattr_destroy(&attr);
223 return (LIBUSB_ERROR_OTHER);
225 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
226 pthread_mutex_destroy(&ctx->ctx_lock);
227 pthread_mutex_destroy(&ctx->hotplug_lock);
228 pthread_condattr_destroy(&attr);
230 return (LIBUSB_ERROR_NO_MEM);
232 pthread_condattr_destroy(&attr);
234 ctx->ctx_handler = NO_THREAD;
235 ctx->hotplug_handler = NO_THREAD;
237 ret = pipe(ctx->ctrl_pipe);
239 pthread_mutex_destroy(&ctx->ctx_lock);
240 pthread_mutex_destroy(&ctx->hotplug_lock);
241 pthread_cond_destroy(&ctx->ctx_cond);
243 return (LIBUSB_ERROR_OTHER);
245 /* set non-blocking mode on the control pipe to avoid deadlock */
246 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
247 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
249 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
251 pthread_mutex_lock(&default_context_lock);
252 if (usbi_default_context == NULL) {
253 usbi_default_context = ctx;
255 pthread_mutex_unlock(&default_context_lock);
260 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
262 signal(SIGPIPE, SIG_IGN);
268 libusb_exit(libusb_context *ctx)
270 ctx = GET_CONTEXT(ctx);
275 /* stop hotplug thread, if any */
277 if (ctx->hotplug_handler != NO_THREAD) {
282 td = ctx->hotplug_handler;
283 ctx->hotplug_handler = NO_THREAD;
286 pthread_join(td, &ptr);
289 /* XXX cleanup devices */
291 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
292 close(ctx->ctrl_pipe[0]);
293 close(ctx->ctrl_pipe[1]);
294 pthread_mutex_destroy(&ctx->ctx_lock);
295 pthread_mutex_destroy(&ctx->hotplug_lock);
296 pthread_cond_destroy(&ctx->ctx_cond);
298 pthread_mutex_lock(&default_context_lock);
299 if (ctx == usbi_default_context) {
300 usbi_default_context = NULL;
302 pthread_mutex_unlock(&default_context_lock);
307 /* Device handling and initialisation. */
310 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
312 struct libusb20_backend *usb_backend;
313 struct libusb20_device *pdev;
314 struct libusb_device *dev;
317 ctx = GET_CONTEXT(ctx);
320 return (LIBUSB_ERROR_INVALID_PARAM);
323 return (LIBUSB_ERROR_INVALID_PARAM);
325 usb_backend = libusb20_be_alloc_default();
326 if (usb_backend == NULL)
327 return (LIBUSB_ERROR_NO_MEM);
329 /* figure out how many USB devices are present */
332 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
335 /* allocate device pointer list */
336 *list = malloc((i + 1) * sizeof(void *));
338 libusb20_be_free(usb_backend);
339 return (LIBUSB_ERROR_NO_MEM);
341 /* create libusb v1.0 compliant devices */
343 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
345 dev = malloc(sizeof(*dev));
348 libusb_unref_device((*list)[i - 1]);
353 libusb20_be_free(usb_backend);
354 return (LIBUSB_ERROR_NO_MEM);
356 /* get device into libUSB v1.0 list */
357 libusb20_be_dequeue_device(usb_backend, pdev);
359 memset(dev, 0, sizeof(*dev));
361 /* init transfer queues */
362 TAILQ_INIT(&dev->tr_head);
364 /* set context we belong to */
367 /* link together the two structures */
369 pdev->privLuData = dev;
371 (*list)[i] = libusb_ref_device(dev);
376 libusb20_be_free(usb_backend);
381 libusb_free_device_list(libusb_device **list, int unref_devices)
386 return; /* be NULL safe */
389 for (i = 0; list[i] != NULL; i++)
390 libusb_unref_device(list[i]);
396 libusb_get_bus_number(libusb_device *dev)
399 return (0); /* should not happen */
400 return (libusb20_dev_get_bus_number(dev->os_priv));
404 libusb_get_port_number(libusb_device *dev)
407 return (0); /* should not happen */
408 return (libusb20_dev_get_parent_port(dev->os_priv));
412 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
414 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
418 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
421 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
425 libusb_get_device_address(libusb_device *dev)
428 return (0); /* should not happen */
429 return (libusb20_dev_get_address(dev->os_priv));
433 libusb_get_device_speed(libusb_device *dev)
436 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
438 switch (libusb20_dev_get_speed(dev->os_priv)) {
439 case LIBUSB20_SPEED_LOW:
440 return (LIBUSB_SPEED_LOW);
441 case LIBUSB20_SPEED_FULL:
442 return (LIBUSB_SPEED_FULL);
443 case LIBUSB20_SPEED_HIGH:
444 return (LIBUSB_SPEED_HIGH);
445 case LIBUSB20_SPEED_SUPER:
446 return (LIBUSB_SPEED_SUPER);
450 return (LIBUSB_SPEED_UNKNOWN);
454 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
456 struct libusb_config_descriptor *pdconf;
457 struct libusb_interface *pinf;
458 struct libusb_interface_descriptor *pdinf;
459 struct libusb_endpoint_descriptor *pdend;
466 return (LIBUSB_ERROR_NO_DEVICE);
468 ret = libusb_get_active_config_descriptor(dev, &pdconf);
472 ret = LIBUSB_ERROR_NOT_FOUND;
473 for (i = 0; i < pdconf->bNumInterfaces; i++) {
474 pinf = &pdconf->interface[i];
475 for (j = 0; j < pinf->num_altsetting; j++) {
476 pdinf = &pinf->altsetting[j];
477 for (k = 0; k < pdinf->bNumEndpoints; k++) {
478 pdend = &pdinf->endpoint[k];
479 if (pdend->bEndpointAddress == endpoint) {
480 ret = pdend->wMaxPacketSize;
488 libusb_free_config_descriptor(pdconf);
493 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
498 ret = libusb_get_max_packet_size(dev, endpoint);
500 switch (libusb20_dev_get_speed(dev->os_priv)) {
501 case LIBUSB20_SPEED_LOW:
502 case LIBUSB20_SPEED_FULL:
506 multiplier = (1 + ((ret >> 11) & 3));
509 ret = (ret & 0x7FF) * multiplier;
517 libusb_ref_device(libusb_device *dev)
520 return (NULL); /* be NULL safe */
524 CTX_UNLOCK(dev->ctx);
530 libusb_unref_device(libusb_device *dev)
533 return; /* be NULL safe */
537 CTX_UNLOCK(dev->ctx);
539 if (dev->refcnt == 0) {
540 libusb20_dev_free(dev->os_priv);
546 libusb_open(libusb_device *dev, libusb_device_handle **devh)
548 libusb_context *ctx = dev->ctx;
549 struct libusb20_device *pdev = dev->os_priv;
553 return (LIBUSB_ERROR_INVALID_PARAM);
555 /* set default device handle value */
558 dev = libusb_ref_device(dev);
560 return (LIBUSB_ERROR_INVALID_PARAM);
562 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
564 libusb_unref_device(dev);
565 return (LIBUSB_ERROR_NO_MEM);
569 * Clear the device gone flag, in case the device was opened
570 * after a re-attach, to allow new transaction:
573 dev->device_is_gone = 0;
576 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
577 POLLOUT | POLLRDNORM | POLLWRNORM);
579 /* make sure our event loop detects the new device */
580 libusb_interrupt_event_handler(ctx);
587 libusb_device_handle *
588 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
591 struct libusb_device **devs;
592 struct libusb20_device *pdev;
593 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
597 ctx = GET_CONTEXT(ctx);
599 return (NULL); /* be NULL safe */
601 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter");
603 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
607 for (j = 0; j < i; j++) {
608 struct libusb20_device *tdev;
610 tdev = devs[j]->os_priv;
611 pdesc = libusb20_dev_get_device_desc(tdev);
613 * NOTE: The USB library will automatically swap the
614 * fields in the device descriptor to be of host
617 if (pdesc->idVendor == vendor_id &&
618 pdesc->idProduct == product_id) {
619 libusb_open(devs[j], &pdev);
624 libusb_free_device_list(devs, 1);
625 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave");
630 libusb_close(struct libusb20_device *pdev)
633 struct libusb_device *dev;
636 return; /* be NULL safe */
638 dev = libusb_get_device(pdev);
641 libusb10_remove_pollfd(ctx, &dev->dev_poll);
643 libusb20_dev_close(pdev);
645 /* unref will free the "pdev" when the refcount reaches zero */
646 libusb_unref_device(dev);
648 /* make sure our event loop detects the closed device */
649 libusb_interrupt_event_handler(ctx);
653 libusb_get_device(struct libusb20_device *pdev)
657 return ((libusb_device *)pdev->privLuData);
661 libusb_get_configuration(struct libusb20_device *pdev, int *config)
663 struct libusb20_config *pconf;
665 if (pdev == NULL || config == NULL)
666 return (LIBUSB_ERROR_INVALID_PARAM);
668 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
670 return (LIBUSB_ERROR_NO_MEM);
672 *config = pconf->desc.bConfigurationValue;
680 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
682 struct libusb20_config *pconf;
683 struct libusb_device *dev;
687 dev = libusb_get_device(pdev);
689 return (LIBUSB_ERROR_INVALID_PARAM);
691 if (configuration < 1) {
695 for (i = 0; i != 255; i++) {
698 pconf = libusb20_dev_alloc_config(pdev, i);
700 return (LIBUSB_ERROR_INVALID_PARAM);
701 found = (pconf->desc.bConfigurationValue
708 return (LIBUSB_ERROR_INVALID_PARAM);
713 libusb10_cancel_all_transfer(dev);
715 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
717 err = libusb20_dev_set_config_index(pdev, i);
719 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
720 POLLOUT | POLLRDNORM | POLLWRNORM);
722 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
726 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
731 dev = libusb_get_device(pdev);
733 return (LIBUSB_ERROR_INVALID_PARAM);
735 if (interface_number < 0 || interface_number > 31)
736 return (LIBUSB_ERROR_INVALID_PARAM);
738 if (pdev->auto_detach != 0) {
739 err = libusb_detach_kernel_driver(pdev, interface_number);
745 dev->claimed_interfaces |= (1 << interface_number);
746 CTX_UNLOCK(dev->ctx);
752 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
757 dev = libusb_get_device(pdev);
759 return (LIBUSB_ERROR_INVALID_PARAM);
761 if (interface_number < 0 || interface_number > 31)
762 return (LIBUSB_ERROR_INVALID_PARAM);
764 if (pdev->auto_detach != 0) {
765 err = libusb_attach_kernel_driver(pdev, interface_number);
771 if (!(dev->claimed_interfaces & (1 << interface_number)))
772 err = LIBUSB_ERROR_NOT_FOUND;
774 dev->claimed_interfaces &= ~(1 << interface_number);
775 CTX_UNLOCK(dev->ctx);
781 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
782 int interface_number, int alternate_setting)
787 dev = libusb_get_device(pdev);
789 return (LIBUSB_ERROR_INVALID_PARAM);
791 if (interface_number < 0 || interface_number > 31)
792 return (LIBUSB_ERROR_INVALID_PARAM);
795 if (!(dev->claimed_interfaces & (1 << interface_number)))
796 err = LIBUSB_ERROR_NOT_FOUND;
797 CTX_UNLOCK(dev->ctx);
802 libusb10_cancel_all_transfer(dev);
804 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
806 err = libusb20_dev_set_alt_index(pdev,
807 interface_number, alternate_setting);
809 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
810 pdev, libusb20_dev_get_fd(pdev),
811 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
813 return (err ? LIBUSB_ERROR_OTHER : 0);
816 static struct libusb20_transfer *
817 libusb10_get_transfer(struct libusb20_device *pdev,
818 uint8_t endpoint, uint8_t xfer_index)
820 xfer_index &= 1; /* double buffering */
822 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
824 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
825 /* this is an IN endpoint */
828 return (libusb20_tr_get_pointer(pdev, xfer_index));
832 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
834 struct libusb20_transfer *xfer;
835 struct libusb_device *dev;
838 xfer = libusb10_get_transfer(pdev, endpoint, 0);
840 return (LIBUSB_ERROR_INVALID_PARAM);
842 dev = libusb_get_device(pdev);
844 return (LIBUSB_ERROR_INVALID_PARAM);
847 err = libusb20_tr_open(xfer, 0, 1, endpoint);
848 CTX_UNLOCK(dev->ctx);
850 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
851 return (LIBUSB_ERROR_OTHER);
853 libusb20_tr_clear_stall_sync(xfer);
855 /* check if we opened the transfer */
858 libusb20_tr_close(xfer);
859 CTX_UNLOCK(dev->ctx);
861 return (0); /* success */
865 libusb_reset_device(struct libusb20_device *pdev)
870 dev = libusb_get_device(pdev);
872 return (LIBUSB_ERROR_INVALID_PARAM);
874 libusb10_cancel_all_transfer(dev);
876 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
878 err = libusb20_dev_reset(pdev);
880 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
881 pdev, libusb20_dev_get_fd(pdev),
882 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
884 return (err ? LIBUSB_ERROR_OTHER : 0);
888 libusb_check_connected(struct libusb20_device *pdev)
893 dev = libusb_get_device(pdev);
895 return (LIBUSB_ERROR_INVALID_PARAM);
897 err = libusb20_dev_check_connected(pdev);
899 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
903 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
906 return (LIBUSB_ERROR_INVALID_PARAM);
908 if (libusb20_dev_kernel_driver_active(pdev, interface))
909 return (0); /* no kernel driver is active */
911 return (1); /* kernel driver is active */
915 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
916 char *name, int namelen)
918 return (libusb_get_driver(pdev, interface, name, namelen));
922 libusb_get_driver(struct libusb20_device *pdev, int interface,
923 char *name, int namelen)
929 return (LIBUSB_ERROR_INVALID_PARAM);
931 return (LIBUSB_ERROR_INVALID_PARAM);
935 err = libusb20_dev_get_iface_desc(
936 pdev, interface, name, namelen);
939 return (LIBUSB_ERROR_OTHER);
941 /* we only want the driver name */
942 ptr = strstr(name, ":");
950 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
952 return (libusb_detach_kernel_driver(pdev, interface));
956 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
961 return (LIBUSB_ERROR_INVALID_PARAM);
963 err = libusb20_dev_detach_kernel_driver(
966 return (err ? LIBUSB_ERROR_OTHER : 0);
970 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
973 return (LIBUSB_ERROR_INVALID_PARAM);
974 /* stub - currently not supported by libusb20 */
979 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
981 dev->auto_detach = (enable ? 1 : 0);
985 /* Asynchronous device I/O */
987 struct libusb_transfer *
988 libusb_alloc_transfer(int iso_packets)
990 struct libusb_transfer *uxfer;
991 struct libusb_super_transfer *sxfer;
994 len = sizeof(struct libusb_transfer) +
995 sizeof(struct libusb_super_transfer) +
996 (iso_packets * sizeof(libusb_iso_packet_descriptor));
1002 memset(sxfer, 0, len);
1004 uxfer = (struct libusb_transfer *)(
1005 ((uint8_t *)sxfer) + sizeof(*sxfer));
1007 /* set default value */
1008 uxfer->num_iso_packets = iso_packets;
1014 libusb_free_transfer(struct libusb_transfer *uxfer)
1016 struct libusb_super_transfer *sxfer;
1019 return; /* be NULL safe */
1021 /* check if we should free the transfer buffer */
1022 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
1023 free(uxfer->buffer);
1025 sxfer = (struct libusb_super_transfer *)(
1026 (uint8_t *)uxfer - sizeof(*sxfer));
1032 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
1036 switch (xfer->type) {
1037 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1038 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
1040 case LIBUSB_TRANSFER_TYPE_CONTROL:
1051 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
1056 usb_speed = libusb20_dev_get_speed(pdev);
1058 switch (xfer->type) {
1059 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1060 ret = 0; /* kernel will auto-select */
1062 case LIBUSB_TRANSFER_TYPE_CONTROL:
1066 switch (usb_speed) {
1067 case LIBUSB20_SPEED_LOW:
1070 case LIBUSB20_SPEED_FULL:
1073 case LIBUSB20_SPEED_SUPER:
1086 libusb10_convert_error(uint8_t status)
1091 case LIBUSB20_TRANSFER_START:
1092 case LIBUSB20_TRANSFER_COMPLETED:
1093 return (LIBUSB_TRANSFER_COMPLETED);
1094 case LIBUSB20_TRANSFER_OVERFLOW:
1095 return (LIBUSB_TRANSFER_OVERFLOW);
1096 case LIBUSB20_TRANSFER_NO_DEVICE:
1097 return (LIBUSB_TRANSFER_NO_DEVICE);
1098 case LIBUSB20_TRANSFER_STALL:
1099 return (LIBUSB_TRANSFER_STALL);
1100 case LIBUSB20_TRANSFER_CANCELLED:
1101 return (LIBUSB_TRANSFER_CANCELLED);
1102 case LIBUSB20_TRANSFER_TIMED_OUT:
1103 return (LIBUSB_TRANSFER_TIMED_OUT);
1105 return (LIBUSB_TRANSFER_ERROR);
1109 /* This function must be called locked */
1112 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1113 struct libusb_super_transfer *sxfer, int status)
1115 struct libusb_transfer *uxfer;
1116 struct libusb_device *dev;
1118 uxfer = (struct libusb_transfer *)(
1119 ((uint8_t *)sxfer) + sizeof(*sxfer));
1122 libusb20_tr_set_priv_sc1(pxfer, NULL);
1124 /* set transfer status */
1125 uxfer->status = status;
1127 /* update super transfer state */
1128 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1130 dev = libusb_get_device(uxfer->dev_handle);
1132 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1135 /* This function must be called locked */
1138 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1140 struct libusb_super_transfer *sxfer;
1141 struct libusb_transfer *uxfer;
1143 uint16_t iso_packets;
1147 status = libusb20_tr_get_status(pxfer);
1148 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1149 actlen = libusb20_tr_get_actual_length(pxfer);
1150 iso_packets = libusb20_tr_get_max_frames(pxfer);
1153 return; /* cancelled - nothing to do */
1155 uxfer = (struct libusb_transfer *)(
1156 ((uint8_t *)sxfer) + sizeof(*sxfer));
1158 if (iso_packets > uxfer->num_iso_packets)
1159 iso_packets = uxfer->num_iso_packets;
1161 if (iso_packets == 0)
1162 return; /* nothing to do */
1164 /* make sure that the number of ISOCHRONOUS packets is valid */
1165 uxfer->num_iso_packets = iso_packets;
1168 case LIBUSB20_TRANSFER_COMPLETED:
1169 /* update actual length */
1170 uxfer->actual_length = actlen;
1171 for (i = 0; i != iso_packets; i++) {
1172 uxfer->iso_packet_desc[i].actual_length =
1173 libusb20_tr_get_length(pxfer, i);
1175 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1177 case LIBUSB20_TRANSFER_START:
1178 /* setup length(s) */
1180 for (i = 0; i != iso_packets; i++) {
1181 libusb20_tr_setup_isoc(pxfer,
1182 &uxfer->buffer[actlen],
1183 uxfer->iso_packet_desc[i].length, i);
1184 actlen += uxfer->iso_packet_desc[i].length;
1190 libusb20_tr_set_total_frames(pxfer, iso_packets);
1191 libusb20_tr_submit(pxfer);
1193 /* fork another USB transfer, if any */
1194 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1197 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1202 /* This function must be called locked */
1205 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1207 struct libusb_super_transfer *sxfer;
1208 struct libusb_transfer *uxfer;
1214 status = libusb20_tr_get_status(pxfer);
1215 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1216 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1217 actlen = libusb20_tr_get_actual_length(pxfer);
1220 return; /* cancelled - nothing to do */
1222 uxfer = (struct libusb_transfer *)(
1223 ((uint8_t *)sxfer) + sizeof(*sxfer));
1225 flags = uxfer->flags;
1228 case LIBUSB20_TRANSFER_COMPLETED:
1230 uxfer->actual_length += actlen;
1232 /* check for short packet */
1233 if (sxfer->last_len != actlen) {
1234 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1235 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1237 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1241 /* check for end of data */
1242 if (sxfer->rem_len == 0) {
1243 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1248 case LIBUSB20_TRANSFER_START:
1249 if (max_bulk > sxfer->rem_len) {
1250 max_bulk = sxfer->rem_len;
1252 /* setup new BULK or INTERRUPT transaction */
1253 libusb20_tr_setup_bulk(pxfer,
1254 sxfer->curr_data, max_bulk, uxfer->timeout);
1256 /* update counters */
1257 sxfer->last_len = max_bulk;
1258 sxfer->curr_data += max_bulk;
1259 sxfer->rem_len -= max_bulk;
1261 libusb20_tr_submit(pxfer);
1263 /* check if we can fork another USB transfer */
1264 if (sxfer->rem_len == 0)
1265 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1269 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1274 /* This function must be called locked */
1277 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1279 struct libusb_super_transfer *sxfer;
1280 struct libusb_transfer *uxfer;
1286 status = libusb20_tr_get_status(pxfer);
1287 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1288 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1289 actlen = libusb20_tr_get_actual_length(pxfer);
1292 return; /* cancelled - nothing to do */
1294 uxfer = (struct libusb_transfer *)(
1295 ((uint8_t *)sxfer) + sizeof(*sxfer));
1297 flags = uxfer->flags;
1300 case LIBUSB20_TRANSFER_COMPLETED:
1302 uxfer->actual_length += actlen;
1304 /* subtract length of SETUP packet, if any */
1305 actlen -= libusb20_tr_get_length(pxfer, 0);
1307 /* check for short packet */
1308 if (sxfer->last_len != actlen) {
1309 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1310 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1312 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1316 /* check for end of data */
1317 if (sxfer->rem_len == 0) {
1318 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1323 case LIBUSB20_TRANSFER_START:
1324 if (max_bulk > sxfer->rem_len) {
1325 max_bulk = sxfer->rem_len;
1327 /* setup new CONTROL transaction */
1328 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1329 /* next fragment - don't send SETUP packet */
1330 libusb20_tr_set_length(pxfer, 0, 0);
1332 /* first fragment - send SETUP packet */
1333 libusb20_tr_set_length(pxfer, 8, 0);
1334 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1337 if (max_bulk != 0) {
1338 libusb20_tr_set_length(pxfer, max_bulk, 1);
1339 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1340 libusb20_tr_set_total_frames(pxfer, 2);
1342 libusb20_tr_set_total_frames(pxfer, 1);
1345 /* update counters */
1346 sxfer->last_len = max_bulk;
1347 sxfer->curr_data += max_bulk;
1348 sxfer->rem_len -= max_bulk;
1350 libusb20_tr_submit(pxfer);
1352 /* check if we can fork another USB transfer */
1353 if (sxfer->rem_len == 0)
1354 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1358 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1363 /* The following function must be called locked */
1366 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1368 struct libusb20_transfer *pxfer0;
1369 struct libusb20_transfer *pxfer1;
1370 struct libusb_super_transfer *sxfer;
1371 struct libusb_transfer *uxfer;
1372 struct libusb_device *dev;
1378 dev = libusb_get_device(pdev);
1380 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1381 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1383 if (pxfer0 == NULL || pxfer1 == NULL)
1384 return; /* shouldn't happen */
1387 if (libusb20_tr_pending(pxfer0))
1389 if (libusb20_tr_pending(pxfer1))
1394 /* wait till one of the transfers complete */
1397 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1399 return; /* cancelling */
1401 return; /* cannot queue another one */
1402 /* swap transfers */
1406 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1408 return; /* cancelling */
1410 return; /* cannot queue another one */
1411 /* swap transfers */
1418 /* find next transfer on same endpoint */
1419 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1421 uxfer = (struct libusb_transfer *)(
1422 ((uint8_t *)sxfer) + sizeof(*sxfer));
1424 if (uxfer->endpoint == endpoint) {
1425 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1426 sxfer->entry.tqe_prev = NULL;
1430 return; /* success */
1434 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1435 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1437 /* reset super transfer state */
1438 sxfer->rem_len = uxfer->length;
1439 sxfer->curr_data = uxfer->buffer;
1440 uxfer->actual_length = 0;
1442 switch (uxfer->type) {
1443 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1444 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1446 case LIBUSB_TRANSFER_TYPE_BULK:
1447 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1448 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1450 case LIBUSB_TRANSFER_TYPE_CONTROL:
1451 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1452 if (sxfer->rem_len < 8)
1455 /* remove SETUP packet from data */
1456 sxfer->rem_len -= 8;
1457 sxfer->curr_data += 8;
1463 buffsize = libusb10_get_buffsize(pdev, uxfer);
1464 maxframe = libusb10_get_maxframe(pdev, uxfer);
1466 /* make sure the transfer is opened */
1467 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1468 endpoint, sxfer->stream_id);
1469 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1472 libusb20_tr_start(pxfer0);
1476 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1477 /* make sure our event loop spins the done handler */
1478 libusb_interrupt_event_handler(dev->ctx);
1481 /* The following function must be called unlocked */
1484 libusb_submit_transfer(struct libusb_transfer *uxfer)
1486 struct libusb20_transfer *pxfer0;
1487 struct libusb20_transfer *pxfer1;
1488 struct libusb_super_transfer *sxfer;
1489 struct libusb_device *dev;
1494 return (LIBUSB_ERROR_INVALID_PARAM);
1496 if (uxfer->dev_handle == NULL)
1497 return (LIBUSB_ERROR_INVALID_PARAM);
1499 endpoint = uxfer->endpoint;
1501 dev = libusb_get_device(uxfer->dev_handle);
1503 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1505 sxfer = (struct libusb_super_transfer *)(
1506 (uint8_t *)uxfer - sizeof(*sxfer));
1510 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1511 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1513 if (pxfer0 == NULL || pxfer1 == NULL) {
1514 err = LIBUSB_ERROR_OTHER;
1515 } else if ((sxfer->entry.tqe_prev != NULL) ||
1516 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1517 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1518 err = LIBUSB_ERROR_BUSY;
1519 } else if (dev->device_is_gone != 0) {
1520 err = LIBUSB_ERROR_NO_DEVICE;
1523 /* set pending state */
1524 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1526 /* insert transfer into transfer head list */
1527 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1529 /* start work transfers */
1530 libusb10_submit_transfer_sub(
1531 uxfer->dev_handle, endpoint);
1533 err = 0; /* success */
1536 CTX_UNLOCK(dev->ctx);
1538 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1543 /* Asynchronous transfer cancel */
1546 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1548 struct libusb20_transfer *pxfer0;
1549 struct libusb20_transfer *pxfer1;
1550 struct libusb_super_transfer *sxfer;
1551 struct libusb_device *dev;
1552 struct libusb_device_handle *devh;
1557 return (LIBUSB_ERROR_INVALID_PARAM);
1559 /* check if not initialised */
1560 if ((devh = uxfer->dev_handle) == NULL)
1561 return (LIBUSB_ERROR_NOT_FOUND);
1563 endpoint = uxfer->endpoint;
1565 dev = libusb_get_device(devh);
1567 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1569 sxfer = (struct libusb_super_transfer *)(
1570 (uint8_t *)uxfer - sizeof(*sxfer));
1576 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1577 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1579 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1580 /* only update the transfer status */
1581 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1582 retval = LIBUSB_ERROR_NOT_FOUND;
1583 } else if (sxfer->entry.tqe_prev != NULL) {
1584 /* we are lucky - transfer is on a queue */
1585 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1586 sxfer->entry.tqe_prev = NULL;
1587 libusb10_complete_transfer(NULL,
1588 sxfer, LIBUSB_TRANSFER_CANCELLED);
1589 /* make sure our event loop spins the done handler */
1590 libusb_interrupt_event_handler(dev->ctx);
1591 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1593 retval = LIBUSB_ERROR_NOT_FOUND;
1594 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1595 libusb10_complete_transfer(pxfer0,
1596 sxfer, LIBUSB_TRANSFER_CANCELLED);
1597 if (dev->device_is_gone != 0) {
1598 /* clear transfer pointer */
1599 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1600 /* make sure our event loop spins the done handler */
1601 libusb_interrupt_event_handler(dev->ctx);
1603 libusb20_tr_stop(pxfer0);
1604 /* make sure the queue doesn't stall */
1605 libusb10_submit_transfer_sub(devh, endpoint);
1607 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1608 libusb10_complete_transfer(pxfer1,
1609 sxfer, LIBUSB_TRANSFER_CANCELLED);
1610 /* check if handle is still active */
1611 if (dev->device_is_gone != 0) {
1612 /* clear transfer pointer */
1613 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1614 /* make sure our event loop spins the done handler */
1615 libusb_interrupt_event_handler(dev->ctx);
1617 libusb20_tr_stop(pxfer1);
1618 /* make sure the queue doesn't stall */
1619 libusb10_submit_transfer_sub(devh, endpoint);
1623 retval = LIBUSB_ERROR_NOT_FOUND;
1626 CTX_UNLOCK(dev->ctx);
1628 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1634 libusb10_cancel_all_transfer(libusb_device *dev)
1636 struct libusb20_device *pdev = dev->os_priv;
1639 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1640 struct libusb20_transfer *xfer;
1642 xfer = libusb20_tr_get_pointer(pdev, x);
1645 libusb20_tr_close(xfer);
1650 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1652 struct libusb_super_transfer *sxfer;
1655 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1656 struct libusb20_transfer *xfer;
1658 xfer = libusb20_tr_get_pointer(pdev, x);
1661 if (libusb20_tr_pending(xfer) == 0)
1663 sxfer = libusb20_tr_get_priv_sc1(xfer);
1666 /* complete pending transfer */
1667 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1670 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1671 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1673 /* complete pending transfer */
1674 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1679 libusb_cpu_to_le16(uint16_t x)
1681 return (htole16(x));
1685 libusb_le16_to_cpu(uint16_t x)
1687 return (le16toh(x));
1691 libusb_strerror(int code)
1694 case LIBUSB_SUCCESS:
1696 case LIBUSB_ERROR_IO:
1697 return ("I/O error");
1698 case LIBUSB_ERROR_INVALID_PARAM:
1699 return ("Invalid parameter");
1700 case LIBUSB_ERROR_ACCESS:
1701 return ("Permissions error");
1702 case LIBUSB_ERROR_NO_DEVICE:
1703 return ("No device");
1704 case LIBUSB_ERROR_NOT_FOUND:
1705 return ("Not found");
1706 case LIBUSB_ERROR_BUSY:
1707 return ("Device busy");
1708 case LIBUSB_ERROR_TIMEOUT:
1710 case LIBUSB_ERROR_OVERFLOW:
1711 return ("Overflow");
1712 case LIBUSB_ERROR_PIPE:
1713 return ("Pipe error");
1714 case LIBUSB_ERROR_INTERRUPTED:
1715 return ("Interrupted");
1716 case LIBUSB_ERROR_NO_MEM:
1717 return ("Out of memory");
1718 case LIBUSB_ERROR_NOT_SUPPORTED:
1719 return ("Not supported");
1720 case LIBUSB_ERROR_OTHER:
1721 return ("Other error");
1723 return ("Unknown error");
1728 libusb_error_name(int code)
1731 case LIBUSB_SUCCESS:
1732 return ("LIBUSB_SUCCESS");
1733 case LIBUSB_ERROR_IO:
1734 return ("LIBUSB_ERROR_IO");
1735 case LIBUSB_ERROR_INVALID_PARAM:
1736 return ("LIBUSB_ERROR_INVALID_PARAM");
1737 case LIBUSB_ERROR_ACCESS:
1738 return ("LIBUSB_ERROR_ACCESS");
1739 case LIBUSB_ERROR_NO_DEVICE:
1740 return ("LIBUSB_ERROR_NO_DEVICE");
1741 case LIBUSB_ERROR_NOT_FOUND:
1742 return ("LIBUSB_ERROR_NOT_FOUND");
1743 case LIBUSB_ERROR_BUSY:
1744 return ("LIBUSB_ERROR_BUSY");
1745 case LIBUSB_ERROR_TIMEOUT:
1746 return ("LIBUSB_ERROR_TIMEOUT");
1747 case LIBUSB_ERROR_OVERFLOW:
1748 return ("LIBUSB_ERROR_OVERFLOW");
1749 case LIBUSB_ERROR_PIPE:
1750 return ("LIBUSB_ERROR_PIPE");
1751 case LIBUSB_ERROR_INTERRUPTED:
1752 return ("LIBUSB_ERROR_INTERRUPTED");
1753 case LIBUSB_ERROR_NO_MEM:
1754 return ("LIBUSB_ERROR_NO_MEM");
1755 case LIBUSB_ERROR_NOT_SUPPORTED:
1756 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1757 case LIBUSB_ERROR_OTHER:
1758 return ("LIBUSB_ERROR_OTHER");
1760 return ("LIBUSB_ERROR_UNKNOWN");
1765 libusb_has_capability(uint32_t capability)
1768 switch (capability) {
1769 case LIBUSB_CAP_HAS_CAPABILITY:
1770 case LIBUSB_CAP_HAS_HOTPLUG:
1771 case LIBUSB_CAP_HAS_HID_ACCESS:
1772 case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: