3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
6 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
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 struct libusb_context *ctx;
140 pthread_condattr_t attr;
144 ctx = malloc(sizeof(*ctx));
146 return (LIBUSB_ERROR_INVALID_PARAM);
148 memset(ctx, 0, sizeof(*ctx));
150 debug = getenv("LIBUSB_DEBUG");
153 * If LIBUSB_DEBUG is set, we'll honor that and use it to
154 * override libusb_set_debug calls.
157 ctx->debug = strtol(debug, &ep, 10);
158 if (errno == 0 && *ep == '\0') {
159 ctx->debug_fixed = 1;
162 * LIBUSB_DEBUG conversion failed for some reason, but
163 * we don't care about the specifics all that much. We
164 * can't use it either way. Force it to the default,
165 * 0, in case we had a partial number.
170 TAILQ_INIT(&ctx->pollfds);
171 TAILQ_INIT(&ctx->tr_done);
172 TAILQ_INIT(&ctx->hotplug_cbh);
173 TAILQ_INIT(&ctx->hotplug_devs);
175 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
177 return (LIBUSB_ERROR_NO_MEM);
179 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
180 pthread_mutex_destroy(&ctx->ctx_lock);
182 return (LIBUSB_ERROR_NO_MEM);
184 if (pthread_condattr_init(&attr) != 0) {
185 pthread_mutex_destroy(&ctx->ctx_lock);
186 pthread_mutex_destroy(&ctx->hotplug_lock);
188 return (LIBUSB_ERROR_NO_MEM);
190 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
191 pthread_mutex_destroy(&ctx->ctx_lock);
192 pthread_mutex_destroy(&ctx->hotplug_lock);
193 pthread_condattr_destroy(&attr);
195 return (LIBUSB_ERROR_OTHER);
197 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
198 pthread_mutex_destroy(&ctx->ctx_lock);
199 pthread_mutex_destroy(&ctx->hotplug_lock);
200 pthread_condattr_destroy(&attr);
202 return (LIBUSB_ERROR_NO_MEM);
204 pthread_condattr_destroy(&attr);
206 ctx->ctx_handler = NO_THREAD;
207 ctx->hotplug_handler = NO_THREAD;
209 ret = pipe(ctx->ctrl_pipe);
211 pthread_mutex_destroy(&ctx->ctx_lock);
212 pthread_mutex_destroy(&ctx->hotplug_lock);
213 pthread_cond_destroy(&ctx->ctx_cond);
215 return (LIBUSB_ERROR_OTHER);
217 /* set non-blocking mode on the control pipe to avoid deadlock */
218 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
219 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
221 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
223 pthread_mutex_lock(&default_context_lock);
224 if (usbi_default_context == NULL) {
225 usbi_default_context = ctx;
227 pthread_mutex_unlock(&default_context_lock);
232 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
234 signal(SIGPIPE, SIG_IGN);
240 libusb_exit(libusb_context *ctx)
242 ctx = GET_CONTEXT(ctx);
247 /* stop hotplug thread, if any */
249 if (ctx->hotplug_handler != NO_THREAD) {
254 td = ctx->hotplug_handler;
255 ctx->hotplug_handler = NO_THREAD;
258 pthread_join(td, &ptr);
261 /* XXX cleanup devices */
263 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
264 close(ctx->ctrl_pipe[0]);
265 close(ctx->ctrl_pipe[1]);
266 pthread_mutex_destroy(&ctx->ctx_lock);
267 pthread_mutex_destroy(&ctx->hotplug_lock);
268 pthread_cond_destroy(&ctx->ctx_cond);
270 pthread_mutex_lock(&default_context_lock);
271 if (ctx == usbi_default_context) {
272 usbi_default_context = NULL;
274 pthread_mutex_unlock(&default_context_lock);
279 /* Device handling and initialisation. */
282 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
284 struct libusb20_backend *usb_backend;
285 struct libusb20_device *pdev;
286 struct libusb_device *dev;
289 ctx = GET_CONTEXT(ctx);
292 return (LIBUSB_ERROR_INVALID_PARAM);
295 return (LIBUSB_ERROR_INVALID_PARAM);
297 usb_backend = libusb20_be_alloc_default();
298 if (usb_backend == NULL)
299 return (LIBUSB_ERROR_NO_MEM);
301 /* figure out how many USB devices are present */
304 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
307 /* allocate device pointer list */
308 *list = malloc((i + 1) * sizeof(void *));
310 libusb20_be_free(usb_backend);
311 return (LIBUSB_ERROR_NO_MEM);
313 /* create libusb v1.0 compliant devices */
315 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
317 dev = malloc(sizeof(*dev));
320 libusb_unref_device((*list)[i - 1]);
325 libusb20_be_free(usb_backend);
326 return (LIBUSB_ERROR_NO_MEM);
328 /* get device into libUSB v1.0 list */
329 libusb20_be_dequeue_device(usb_backend, pdev);
331 memset(dev, 0, sizeof(*dev));
333 /* init transfer queues */
334 TAILQ_INIT(&dev->tr_head);
336 /* set context we belong to */
339 /* link together the two structures */
341 pdev->privLuData = dev;
343 (*list)[i] = libusb_ref_device(dev);
348 libusb20_be_free(usb_backend);
353 libusb_free_device_list(libusb_device **list, int unref_devices)
358 return; /* be NULL safe */
361 for (i = 0; list[i] != NULL; i++)
362 libusb_unref_device(list[i]);
368 libusb_get_bus_number(libusb_device *dev)
371 return (0); /* should not happen */
372 return (libusb20_dev_get_bus_number(dev->os_priv));
376 libusb_get_port_number(libusb_device *dev)
379 return (0); /* should not happen */
380 return (libusb20_dev_get_parent_port(dev->os_priv));
384 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
386 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
390 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
393 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
397 libusb_get_device_address(libusb_device *dev)
400 return (0); /* should not happen */
401 return (libusb20_dev_get_address(dev->os_priv));
405 libusb_get_device_speed(libusb_device *dev)
408 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
410 switch (libusb20_dev_get_speed(dev->os_priv)) {
411 case LIBUSB20_SPEED_LOW:
412 return (LIBUSB_SPEED_LOW);
413 case LIBUSB20_SPEED_FULL:
414 return (LIBUSB_SPEED_FULL);
415 case LIBUSB20_SPEED_HIGH:
416 return (LIBUSB_SPEED_HIGH);
417 case LIBUSB20_SPEED_SUPER:
418 return (LIBUSB_SPEED_SUPER);
422 return (LIBUSB_SPEED_UNKNOWN);
426 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
428 struct libusb_config_descriptor *pdconf;
429 struct libusb_interface *pinf;
430 struct libusb_interface_descriptor *pdinf;
431 struct libusb_endpoint_descriptor *pdend;
438 return (LIBUSB_ERROR_NO_DEVICE);
440 ret = libusb_get_active_config_descriptor(dev, &pdconf);
444 ret = LIBUSB_ERROR_NOT_FOUND;
445 for (i = 0; i < pdconf->bNumInterfaces; i++) {
446 pinf = &pdconf->interface[i];
447 for (j = 0; j < pinf->num_altsetting; j++) {
448 pdinf = &pinf->altsetting[j];
449 for (k = 0; k < pdinf->bNumEndpoints; k++) {
450 pdend = &pdinf->endpoint[k];
451 if (pdend->bEndpointAddress == endpoint) {
452 ret = pdend->wMaxPacketSize;
460 libusb_free_config_descriptor(pdconf);
465 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
470 ret = libusb_get_max_packet_size(dev, endpoint);
472 switch (libusb20_dev_get_speed(dev->os_priv)) {
473 case LIBUSB20_SPEED_LOW:
474 case LIBUSB20_SPEED_FULL:
478 multiplier = (1 + ((ret >> 11) & 3));
481 ret = (ret & 0x7FF) * multiplier;
489 libusb_ref_device(libusb_device *dev)
492 return (NULL); /* be NULL safe */
496 CTX_UNLOCK(dev->ctx);
502 libusb_unref_device(libusb_device *dev)
505 return; /* be NULL safe */
509 CTX_UNLOCK(dev->ctx);
511 if (dev->refcnt == 0) {
512 libusb20_dev_free(dev->os_priv);
518 libusb_open(libusb_device *dev, libusb_device_handle **devh)
520 libusb_context *ctx = dev->ctx;
521 struct libusb20_device *pdev = dev->os_priv;
525 return (LIBUSB_ERROR_INVALID_PARAM);
527 /* set default device handle value */
530 dev = libusb_ref_device(dev);
532 return (LIBUSB_ERROR_INVALID_PARAM);
534 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
536 libusb_unref_device(dev);
537 return (LIBUSB_ERROR_NO_MEM);
541 * Clear the device gone flag, in case the device was opened
542 * after a re-attach, to allow new transaction:
545 dev->device_is_gone = 0;
548 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
549 POLLOUT | POLLRDNORM | POLLWRNORM);
551 /* make sure our event loop detects the new device */
552 libusb_interrupt_event_handler(ctx);
559 libusb_device_handle *
560 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
563 struct libusb_device **devs;
564 struct libusb20_device *pdev;
565 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
569 ctx = GET_CONTEXT(ctx);
571 return (NULL); /* be NULL safe */
573 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter");
575 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
579 for (j = 0; j < i; j++) {
580 struct libusb20_device *tdev;
582 tdev = devs[j]->os_priv;
583 pdesc = libusb20_dev_get_device_desc(tdev);
585 * NOTE: The USB library will automatically swap the
586 * fields in the device descriptor to be of host
589 if (pdesc->idVendor == vendor_id &&
590 pdesc->idProduct == product_id) {
591 libusb_open(devs[j], &pdev);
596 libusb_free_device_list(devs, 1);
597 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave");
602 libusb_close(struct libusb20_device *pdev)
605 struct libusb_device *dev;
608 return; /* be NULL safe */
610 dev = libusb_get_device(pdev);
613 libusb10_remove_pollfd(ctx, &dev->dev_poll);
615 libusb20_dev_close(pdev);
617 /* unref will free the "pdev" when the refcount reaches zero */
618 libusb_unref_device(dev);
620 /* make sure our event loop detects the closed device */
621 libusb_interrupt_event_handler(ctx);
625 libusb_get_device(struct libusb20_device *pdev)
629 return ((libusb_device *)pdev->privLuData);
633 libusb_get_configuration(struct libusb20_device *pdev, int *config)
635 struct libusb20_config *pconf;
637 if (pdev == NULL || config == NULL)
638 return (LIBUSB_ERROR_INVALID_PARAM);
640 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
642 return (LIBUSB_ERROR_NO_MEM);
644 *config = pconf->desc.bConfigurationValue;
652 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
654 struct libusb20_config *pconf;
655 struct libusb_device *dev;
659 dev = libusb_get_device(pdev);
661 return (LIBUSB_ERROR_INVALID_PARAM);
663 if (configuration < 1) {
667 for (i = 0; i != 255; i++) {
670 pconf = libusb20_dev_alloc_config(pdev, i);
672 return (LIBUSB_ERROR_INVALID_PARAM);
673 found = (pconf->desc.bConfigurationValue
680 return (LIBUSB_ERROR_INVALID_PARAM);
685 libusb10_cancel_all_transfer(dev);
687 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
689 err = libusb20_dev_set_config_index(pdev, i);
691 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
692 POLLOUT | POLLRDNORM | POLLWRNORM);
694 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
698 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
703 dev = libusb_get_device(pdev);
705 return (LIBUSB_ERROR_INVALID_PARAM);
707 if (interface_number < 0 || interface_number > 31)
708 return (LIBUSB_ERROR_INVALID_PARAM);
710 if (pdev->auto_detach != 0) {
711 err = libusb_detach_kernel_driver(pdev, interface_number);
717 dev->claimed_interfaces |= (1 << interface_number);
718 CTX_UNLOCK(dev->ctx);
724 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
729 dev = libusb_get_device(pdev);
731 return (LIBUSB_ERROR_INVALID_PARAM);
733 if (interface_number < 0 || interface_number > 31)
734 return (LIBUSB_ERROR_INVALID_PARAM);
736 if (pdev->auto_detach != 0) {
737 err = libusb_attach_kernel_driver(pdev, interface_number);
743 if (!(dev->claimed_interfaces & (1 << interface_number)))
744 err = LIBUSB_ERROR_NOT_FOUND;
746 dev->claimed_interfaces &= ~(1 << interface_number);
747 CTX_UNLOCK(dev->ctx);
753 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
754 int interface_number, int alternate_setting)
759 dev = libusb_get_device(pdev);
761 return (LIBUSB_ERROR_INVALID_PARAM);
763 if (interface_number < 0 || interface_number > 31)
764 return (LIBUSB_ERROR_INVALID_PARAM);
767 if (!(dev->claimed_interfaces & (1 << interface_number)))
768 err = LIBUSB_ERROR_NOT_FOUND;
769 CTX_UNLOCK(dev->ctx);
774 libusb10_cancel_all_transfer(dev);
776 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
778 err = libusb20_dev_set_alt_index(pdev,
779 interface_number, alternate_setting);
781 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
782 pdev, libusb20_dev_get_fd(pdev),
783 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
785 return (err ? LIBUSB_ERROR_OTHER : 0);
788 static struct libusb20_transfer *
789 libusb10_get_transfer(struct libusb20_device *pdev,
790 uint8_t endpoint, uint8_t xfer_index)
792 xfer_index &= 1; /* double buffering */
794 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
796 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
797 /* this is an IN endpoint */
800 return (libusb20_tr_get_pointer(pdev, xfer_index));
804 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
806 struct libusb20_transfer *xfer;
807 struct libusb_device *dev;
810 xfer = libusb10_get_transfer(pdev, endpoint, 0);
812 return (LIBUSB_ERROR_INVALID_PARAM);
814 dev = libusb_get_device(pdev);
816 return (LIBUSB_ERROR_INVALID_PARAM);
819 err = libusb20_tr_open(xfer, 0, 1, endpoint);
820 CTX_UNLOCK(dev->ctx);
822 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
823 return (LIBUSB_ERROR_OTHER);
825 libusb20_tr_clear_stall_sync(xfer);
827 /* check if we opened the transfer */
830 libusb20_tr_close(xfer);
831 CTX_UNLOCK(dev->ctx);
833 return (0); /* success */
837 libusb_reset_device(struct libusb20_device *pdev)
842 dev = libusb_get_device(pdev);
844 return (LIBUSB_ERROR_INVALID_PARAM);
846 libusb10_cancel_all_transfer(dev);
848 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
850 err = libusb20_dev_reset(pdev);
852 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
853 pdev, libusb20_dev_get_fd(pdev),
854 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
856 return (err ? LIBUSB_ERROR_OTHER : 0);
860 libusb_check_connected(struct libusb20_device *pdev)
865 dev = libusb_get_device(pdev);
867 return (LIBUSB_ERROR_INVALID_PARAM);
869 err = libusb20_dev_check_connected(pdev);
871 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
875 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
878 return (LIBUSB_ERROR_INVALID_PARAM);
880 if (libusb20_dev_kernel_driver_active(pdev, interface))
881 return (0); /* no kernel driver is active */
883 return (1); /* kernel driver is active */
887 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
888 char *name, int namelen)
890 return (libusb_get_driver(pdev, interface, name, namelen));
894 libusb_get_driver(struct libusb20_device *pdev, int interface,
895 char *name, int namelen)
901 return (LIBUSB_ERROR_INVALID_PARAM);
903 return (LIBUSB_ERROR_INVALID_PARAM);
907 err = libusb20_dev_get_iface_desc(
908 pdev, interface, name, namelen);
911 return (LIBUSB_ERROR_OTHER);
913 /* we only want the driver name */
914 ptr = strstr(name, ":");
922 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
924 return (libusb_detach_kernel_driver(pdev, interface));
928 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
933 return (LIBUSB_ERROR_INVALID_PARAM);
935 err = libusb20_dev_detach_kernel_driver(
938 return (err ? LIBUSB_ERROR_OTHER : 0);
942 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
945 return (LIBUSB_ERROR_INVALID_PARAM);
946 /* stub - currently not supported by libusb20 */
951 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
953 dev->auto_detach = (enable ? 1 : 0);
957 /* Asynchronous device I/O */
959 struct libusb_transfer *
960 libusb_alloc_transfer(int iso_packets)
962 struct libusb_transfer *uxfer;
963 struct libusb_super_transfer *sxfer;
966 len = sizeof(struct libusb_transfer) +
967 sizeof(struct libusb_super_transfer) +
968 (iso_packets * sizeof(libusb_iso_packet_descriptor));
974 memset(sxfer, 0, len);
976 uxfer = (struct libusb_transfer *)(
977 ((uint8_t *)sxfer) + sizeof(*sxfer));
979 /* set default value */
980 uxfer->num_iso_packets = iso_packets;
986 libusb_free_transfer(struct libusb_transfer *uxfer)
988 struct libusb_super_transfer *sxfer;
991 return; /* be NULL safe */
993 /* check if we should free the transfer buffer */
994 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
997 sxfer = (struct libusb_super_transfer *)(
998 (uint8_t *)uxfer - sizeof(*sxfer));
1004 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
1008 switch (xfer->type) {
1009 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1010 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
1012 case LIBUSB_TRANSFER_TYPE_CONTROL:
1023 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
1028 usb_speed = libusb20_dev_get_speed(pdev);
1030 switch (xfer->type) {
1031 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1032 ret = 0; /* kernel will auto-select */
1034 case LIBUSB_TRANSFER_TYPE_CONTROL:
1038 switch (usb_speed) {
1039 case LIBUSB20_SPEED_LOW:
1042 case LIBUSB20_SPEED_FULL:
1045 case LIBUSB20_SPEED_SUPER:
1058 libusb10_convert_error(uint8_t status)
1063 case LIBUSB20_TRANSFER_START:
1064 case LIBUSB20_TRANSFER_COMPLETED:
1065 return (LIBUSB_TRANSFER_COMPLETED);
1066 case LIBUSB20_TRANSFER_OVERFLOW:
1067 return (LIBUSB_TRANSFER_OVERFLOW);
1068 case LIBUSB20_TRANSFER_NO_DEVICE:
1069 return (LIBUSB_TRANSFER_NO_DEVICE);
1070 case LIBUSB20_TRANSFER_STALL:
1071 return (LIBUSB_TRANSFER_STALL);
1072 case LIBUSB20_TRANSFER_CANCELLED:
1073 return (LIBUSB_TRANSFER_CANCELLED);
1074 case LIBUSB20_TRANSFER_TIMED_OUT:
1075 return (LIBUSB_TRANSFER_TIMED_OUT);
1077 return (LIBUSB_TRANSFER_ERROR);
1081 /* This function must be called locked */
1084 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1085 struct libusb_super_transfer *sxfer, int status)
1087 struct libusb_transfer *uxfer;
1088 struct libusb_device *dev;
1090 uxfer = (struct libusb_transfer *)(
1091 ((uint8_t *)sxfer) + sizeof(*sxfer));
1094 libusb20_tr_set_priv_sc1(pxfer, NULL);
1096 /* set transfer status */
1097 uxfer->status = status;
1099 /* update super transfer state */
1100 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1102 dev = libusb_get_device(uxfer->dev_handle);
1104 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1107 /* This function must be called locked */
1110 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1112 struct libusb_super_transfer *sxfer;
1113 struct libusb_transfer *uxfer;
1115 uint16_t iso_packets;
1119 status = libusb20_tr_get_status(pxfer);
1120 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1121 actlen = libusb20_tr_get_actual_length(pxfer);
1122 iso_packets = libusb20_tr_get_max_frames(pxfer);
1125 return; /* cancelled - nothing to do */
1127 uxfer = (struct libusb_transfer *)(
1128 ((uint8_t *)sxfer) + sizeof(*sxfer));
1130 if (iso_packets > uxfer->num_iso_packets)
1131 iso_packets = uxfer->num_iso_packets;
1133 if (iso_packets == 0)
1134 return; /* nothing to do */
1136 /* make sure that the number of ISOCHRONOUS packets is valid */
1137 uxfer->num_iso_packets = iso_packets;
1140 case LIBUSB20_TRANSFER_COMPLETED:
1141 /* update actual length */
1142 uxfer->actual_length = actlen;
1143 for (i = 0; i != iso_packets; i++) {
1144 uxfer->iso_packet_desc[i].actual_length =
1145 libusb20_tr_get_length(pxfer, i);
1147 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1149 case LIBUSB20_TRANSFER_START:
1150 /* setup length(s) */
1152 for (i = 0; i != iso_packets; i++) {
1153 libusb20_tr_setup_isoc(pxfer,
1154 &uxfer->buffer[actlen],
1155 uxfer->iso_packet_desc[i].length, i);
1156 actlen += uxfer->iso_packet_desc[i].length;
1162 libusb20_tr_set_total_frames(pxfer, iso_packets);
1163 libusb20_tr_submit(pxfer);
1165 /* fork another USB transfer, if any */
1166 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1169 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1174 /* This function must be called locked */
1177 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1179 struct libusb_super_transfer *sxfer;
1180 struct libusb_transfer *uxfer;
1186 status = libusb20_tr_get_status(pxfer);
1187 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1188 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1189 actlen = libusb20_tr_get_actual_length(pxfer);
1192 return; /* cancelled - nothing to do */
1194 uxfer = (struct libusb_transfer *)(
1195 ((uint8_t *)sxfer) + sizeof(*sxfer));
1197 flags = uxfer->flags;
1200 case LIBUSB20_TRANSFER_COMPLETED:
1202 uxfer->actual_length += actlen;
1204 /* check for short packet */
1205 if (sxfer->last_len != actlen) {
1206 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1207 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1209 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1213 /* check for end of data */
1214 if (sxfer->rem_len == 0) {
1215 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1220 case LIBUSB20_TRANSFER_START:
1221 if (max_bulk > sxfer->rem_len) {
1222 max_bulk = sxfer->rem_len;
1224 /* setup new BULK or INTERRUPT transaction */
1225 libusb20_tr_setup_bulk(pxfer,
1226 sxfer->curr_data, max_bulk, uxfer->timeout);
1228 /* update counters */
1229 sxfer->last_len = max_bulk;
1230 sxfer->curr_data += max_bulk;
1231 sxfer->rem_len -= max_bulk;
1233 libusb20_tr_submit(pxfer);
1235 /* check if we can fork another USB transfer */
1236 if (sxfer->rem_len == 0)
1237 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1241 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1246 /* This function must be called locked */
1249 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1251 struct libusb_super_transfer *sxfer;
1252 struct libusb_transfer *uxfer;
1258 status = libusb20_tr_get_status(pxfer);
1259 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1260 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1261 actlen = libusb20_tr_get_actual_length(pxfer);
1264 return; /* cancelled - nothing to do */
1266 uxfer = (struct libusb_transfer *)(
1267 ((uint8_t *)sxfer) + sizeof(*sxfer));
1269 flags = uxfer->flags;
1272 case LIBUSB20_TRANSFER_COMPLETED:
1274 uxfer->actual_length += actlen;
1276 /* subtract length of SETUP packet, if any */
1277 actlen -= libusb20_tr_get_length(pxfer, 0);
1279 /* check for short packet */
1280 if (sxfer->last_len != actlen) {
1281 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1282 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1284 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1288 /* check for end of data */
1289 if (sxfer->rem_len == 0) {
1290 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1295 case LIBUSB20_TRANSFER_START:
1296 if (max_bulk > sxfer->rem_len) {
1297 max_bulk = sxfer->rem_len;
1299 /* setup new CONTROL transaction */
1300 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1301 /* next fragment - don't send SETUP packet */
1302 libusb20_tr_set_length(pxfer, 0, 0);
1304 /* first fragment - send SETUP packet */
1305 libusb20_tr_set_length(pxfer, 8, 0);
1306 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1309 if (max_bulk != 0) {
1310 libusb20_tr_set_length(pxfer, max_bulk, 1);
1311 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1312 libusb20_tr_set_total_frames(pxfer, 2);
1314 libusb20_tr_set_total_frames(pxfer, 1);
1317 /* update counters */
1318 sxfer->last_len = max_bulk;
1319 sxfer->curr_data += max_bulk;
1320 sxfer->rem_len -= max_bulk;
1322 libusb20_tr_submit(pxfer);
1324 /* check if we can fork another USB transfer */
1325 if (sxfer->rem_len == 0)
1326 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1330 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1335 /* The following function must be called locked */
1338 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1340 struct libusb20_transfer *pxfer0;
1341 struct libusb20_transfer *pxfer1;
1342 struct libusb_super_transfer *sxfer;
1343 struct libusb_transfer *uxfer;
1344 struct libusb_device *dev;
1350 dev = libusb_get_device(pdev);
1352 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1353 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1355 if (pxfer0 == NULL || pxfer1 == NULL)
1356 return; /* shouldn't happen */
1359 if (libusb20_tr_pending(pxfer0))
1361 if (libusb20_tr_pending(pxfer1))
1366 /* wait till one of the transfers complete */
1369 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1371 return; /* cancelling */
1373 return; /* cannot queue another one */
1374 /* swap transfers */
1378 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1380 return; /* cancelling */
1382 return; /* cannot queue another one */
1383 /* swap transfers */
1390 /* find next transfer on same endpoint */
1391 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1393 uxfer = (struct libusb_transfer *)(
1394 ((uint8_t *)sxfer) + sizeof(*sxfer));
1396 if (uxfer->endpoint == endpoint) {
1397 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1398 sxfer->entry.tqe_prev = NULL;
1402 return; /* success */
1406 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1407 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1409 /* reset super transfer state */
1410 sxfer->rem_len = uxfer->length;
1411 sxfer->curr_data = uxfer->buffer;
1412 uxfer->actual_length = 0;
1414 switch (uxfer->type) {
1415 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1416 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1418 case LIBUSB_TRANSFER_TYPE_BULK:
1419 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1420 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1422 case LIBUSB_TRANSFER_TYPE_CONTROL:
1423 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1424 if (sxfer->rem_len < 8)
1427 /* remove SETUP packet from data */
1428 sxfer->rem_len -= 8;
1429 sxfer->curr_data += 8;
1435 buffsize = libusb10_get_buffsize(pdev, uxfer);
1436 maxframe = libusb10_get_maxframe(pdev, uxfer);
1438 /* make sure the transfer is opened */
1439 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1440 endpoint, sxfer->stream_id);
1441 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1444 libusb20_tr_start(pxfer0);
1448 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1449 /* make sure our event loop spins the done handler */
1450 libusb_interrupt_event_handler(dev->ctx);
1453 /* The following function must be called unlocked */
1456 libusb_submit_transfer(struct libusb_transfer *uxfer)
1458 struct libusb20_transfer *pxfer0;
1459 struct libusb20_transfer *pxfer1;
1460 struct libusb_super_transfer *sxfer;
1461 struct libusb_device *dev;
1466 return (LIBUSB_ERROR_INVALID_PARAM);
1468 if (uxfer->dev_handle == NULL)
1469 return (LIBUSB_ERROR_INVALID_PARAM);
1471 endpoint = uxfer->endpoint;
1473 dev = libusb_get_device(uxfer->dev_handle);
1475 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1477 sxfer = (struct libusb_super_transfer *)(
1478 (uint8_t *)uxfer - sizeof(*sxfer));
1482 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1483 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1485 if (pxfer0 == NULL || pxfer1 == NULL) {
1486 err = LIBUSB_ERROR_OTHER;
1487 } else if ((sxfer->entry.tqe_prev != NULL) ||
1488 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1489 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1490 err = LIBUSB_ERROR_BUSY;
1491 } else if (dev->device_is_gone != 0) {
1492 err = LIBUSB_ERROR_NO_DEVICE;
1495 /* set pending state */
1496 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1498 /* insert transfer into transfer head list */
1499 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1501 /* start work transfers */
1502 libusb10_submit_transfer_sub(
1503 uxfer->dev_handle, endpoint);
1505 err = 0; /* success */
1508 CTX_UNLOCK(dev->ctx);
1510 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1515 /* Asynchronous transfer cancel */
1518 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1520 struct libusb20_transfer *pxfer0;
1521 struct libusb20_transfer *pxfer1;
1522 struct libusb_super_transfer *sxfer;
1523 struct libusb_device *dev;
1524 struct libusb_device_handle *devh;
1529 return (LIBUSB_ERROR_INVALID_PARAM);
1531 /* check if not initialised */
1532 if ((devh = uxfer->dev_handle) == NULL)
1533 return (LIBUSB_ERROR_NOT_FOUND);
1535 endpoint = uxfer->endpoint;
1537 dev = libusb_get_device(devh);
1539 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1541 sxfer = (struct libusb_super_transfer *)(
1542 (uint8_t *)uxfer - sizeof(*sxfer));
1548 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1549 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1551 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1552 /* only update the transfer status */
1553 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1554 retval = LIBUSB_ERROR_NOT_FOUND;
1555 } else if (sxfer->entry.tqe_prev != NULL) {
1556 /* we are lucky - transfer is on a queue */
1557 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1558 sxfer->entry.tqe_prev = NULL;
1559 libusb10_complete_transfer(NULL,
1560 sxfer, LIBUSB_TRANSFER_CANCELLED);
1561 /* make sure our event loop spins the done handler */
1562 libusb_interrupt_event_handler(dev->ctx);
1563 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1565 retval = LIBUSB_ERROR_NOT_FOUND;
1566 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1567 libusb10_complete_transfer(pxfer0,
1568 sxfer, LIBUSB_TRANSFER_CANCELLED);
1569 if (dev->device_is_gone != 0) {
1570 /* clear transfer pointer */
1571 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1572 /* make sure our event loop spins the done handler */
1573 libusb_interrupt_event_handler(dev->ctx);
1575 libusb20_tr_stop(pxfer0);
1576 /* make sure the queue doesn't stall */
1577 libusb10_submit_transfer_sub(devh, endpoint);
1579 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1580 libusb10_complete_transfer(pxfer1,
1581 sxfer, LIBUSB_TRANSFER_CANCELLED);
1582 /* check if handle is still active */
1583 if (dev->device_is_gone != 0) {
1584 /* clear transfer pointer */
1585 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1586 /* make sure our event loop spins the done handler */
1587 libusb_interrupt_event_handler(dev->ctx);
1589 libusb20_tr_stop(pxfer1);
1590 /* make sure the queue doesn't stall */
1591 libusb10_submit_transfer_sub(devh, endpoint);
1595 retval = LIBUSB_ERROR_NOT_FOUND;
1598 CTX_UNLOCK(dev->ctx);
1600 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1606 libusb10_cancel_all_transfer(libusb_device *dev)
1608 struct libusb20_device *pdev = dev->os_priv;
1611 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1612 struct libusb20_transfer *xfer;
1614 xfer = libusb20_tr_get_pointer(pdev, x);
1617 libusb20_tr_close(xfer);
1622 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1624 struct libusb_super_transfer *sxfer;
1627 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1628 struct libusb20_transfer *xfer;
1630 xfer = libusb20_tr_get_pointer(pdev, x);
1633 if (libusb20_tr_pending(xfer) == 0)
1635 sxfer = libusb20_tr_get_priv_sc1(xfer);
1638 /* complete pending transfer */
1639 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1642 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1643 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1645 /* complete pending transfer */
1646 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1651 libusb_cpu_to_le16(uint16_t x)
1653 return (htole16(x));
1657 libusb_le16_to_cpu(uint16_t x)
1659 return (le16toh(x));
1663 libusb_strerror(int code)
1666 case LIBUSB_SUCCESS:
1668 case LIBUSB_ERROR_IO:
1669 return ("I/O error");
1670 case LIBUSB_ERROR_INVALID_PARAM:
1671 return ("Invalid parameter");
1672 case LIBUSB_ERROR_ACCESS:
1673 return ("Permissions error");
1674 case LIBUSB_ERROR_NO_DEVICE:
1675 return ("No device");
1676 case LIBUSB_ERROR_NOT_FOUND:
1677 return ("Not found");
1678 case LIBUSB_ERROR_BUSY:
1679 return ("Device busy");
1680 case LIBUSB_ERROR_TIMEOUT:
1682 case LIBUSB_ERROR_OVERFLOW:
1683 return ("Overflow");
1684 case LIBUSB_ERROR_PIPE:
1685 return ("Pipe error");
1686 case LIBUSB_ERROR_INTERRUPTED:
1687 return ("Interrupted");
1688 case LIBUSB_ERROR_NO_MEM:
1689 return ("Out of memory");
1690 case LIBUSB_ERROR_NOT_SUPPORTED:
1691 return ("Not supported");
1692 case LIBUSB_ERROR_OTHER:
1693 return ("Other error");
1695 return ("Unknown error");
1700 libusb_error_name(int code)
1703 case LIBUSB_SUCCESS:
1704 return ("LIBUSB_SUCCESS");
1705 case LIBUSB_ERROR_IO:
1706 return ("LIBUSB_ERROR_IO");
1707 case LIBUSB_ERROR_INVALID_PARAM:
1708 return ("LIBUSB_ERROR_INVALID_PARAM");
1709 case LIBUSB_ERROR_ACCESS:
1710 return ("LIBUSB_ERROR_ACCESS");
1711 case LIBUSB_ERROR_NO_DEVICE:
1712 return ("LIBUSB_ERROR_NO_DEVICE");
1713 case LIBUSB_ERROR_NOT_FOUND:
1714 return ("LIBUSB_ERROR_NOT_FOUND");
1715 case LIBUSB_ERROR_BUSY:
1716 return ("LIBUSB_ERROR_BUSY");
1717 case LIBUSB_ERROR_TIMEOUT:
1718 return ("LIBUSB_ERROR_TIMEOUT");
1719 case LIBUSB_ERROR_OVERFLOW:
1720 return ("LIBUSB_ERROR_OVERFLOW");
1721 case LIBUSB_ERROR_PIPE:
1722 return ("LIBUSB_ERROR_PIPE");
1723 case LIBUSB_ERROR_INTERRUPTED:
1724 return ("LIBUSB_ERROR_INTERRUPTED");
1725 case LIBUSB_ERROR_NO_MEM:
1726 return ("LIBUSB_ERROR_NO_MEM");
1727 case LIBUSB_ERROR_NOT_SUPPORTED:
1728 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1729 case LIBUSB_ERROR_OTHER:
1730 return ("LIBUSB_ERROR_OTHER");
1732 return ("LIBUSB_ERROR_UNKNOWN");
1737 libusb_has_capability(uint32_t capability)
1740 switch (capability) {
1741 case LIBUSB_CAP_HAS_CAPABILITY:
1742 case LIBUSB_CAP_HAS_HOTPLUG:
1743 case LIBUSB_CAP_HAS_HID_ACCESS:
1744 case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: