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
42 #include <sys/fcntl.h>
43 #include <sys/ioctl.h>
44 #include <sys/queue.h>
45 #include <sys/endian.h>
48 #define libusb_device_handle libusb20_device
51 #include "libusb20_desc.h"
52 #include "libusb20_int.h"
56 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
58 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
59 struct libusb_context *usbi_default_context = NULL;
63 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
64 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
65 static int libusb10_convert_error(uint8_t status);
66 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
67 static void libusb10_isoc_proxy(struct libusb20_transfer *);
68 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
69 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
70 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
72 /* Library initialisation / deinitialisation */
74 static const struct libusb_version libusb_version = {
80 .describe = "https://www.freebsd.org"
83 const struct libusb_version *
84 libusb_get_version(void)
87 return (&libusb_version);
91 libusb_set_debug(libusb_context *ctx, int level)
93 ctx = GET_CONTEXT(ctx);
94 /* debug_fixed is set when the environment overrides libusb_set_debug */
95 if (ctx && ctx->debug_fixed == 0)
100 libusb_set_nonblocking(int f)
105 * We ignore any failures in this function, hence the
106 * non-blocking flag is not critical to the operation of
107 * libUSB. We use F_GETFL and F_SETFL to be compatible with
111 flags = fcntl(f, F_GETFL, NULL);
115 fcntl(f, F_SETFL, flags);
119 libusb10_wakeup_event_loop(libusb_context *ctx)
124 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
125 if (err < (int)sizeof(dummy)) {
126 /* ignore error, if any */
127 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
132 libusb_init(libusb_context **context)
134 struct libusb_context *ctx;
135 pthread_condattr_t attr;
139 ctx = malloc(sizeof(*ctx));
141 return (LIBUSB_ERROR_INVALID_PARAM);
143 memset(ctx, 0, sizeof(*ctx));
145 debug = getenv("LIBUSB_DEBUG");
148 * If LIBUSB_DEBUG is set, we'll honor that and use it to
149 * override libusb_set_debug calls.
152 ctx->debug = strtol(debug, &ep, 10);
153 if (errno == 0 && *ep == '\0') {
154 ctx->debug_fixed = 1;
157 * LIBUSB_DEBUG conversion failed for some reason, but
158 * we don't care about the specifics all that much. We
159 * can't use it either way. Force it to the default,
160 * 0, in case we had a partial number.
165 TAILQ_INIT(&ctx->pollfds);
166 TAILQ_INIT(&ctx->tr_done);
167 TAILQ_INIT(&ctx->hotplug_cbh);
168 TAILQ_INIT(&ctx->hotplug_devs);
170 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
172 return (LIBUSB_ERROR_NO_MEM);
174 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
175 pthread_mutex_destroy(&ctx->ctx_lock);
177 return (LIBUSB_ERROR_NO_MEM);
179 if (pthread_condattr_init(&attr) != 0) {
180 pthread_mutex_destroy(&ctx->ctx_lock);
181 pthread_mutex_destroy(&ctx->hotplug_lock);
183 return (LIBUSB_ERROR_NO_MEM);
185 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
186 pthread_mutex_destroy(&ctx->ctx_lock);
187 pthread_mutex_destroy(&ctx->hotplug_lock);
188 pthread_condattr_destroy(&attr);
190 return (LIBUSB_ERROR_OTHER);
192 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
193 pthread_mutex_destroy(&ctx->ctx_lock);
194 pthread_mutex_destroy(&ctx->hotplug_lock);
195 pthread_condattr_destroy(&attr);
197 return (LIBUSB_ERROR_NO_MEM);
199 pthread_condattr_destroy(&attr);
201 ctx->ctx_handler = NO_THREAD;
202 ctx->hotplug_handler = NO_THREAD;
204 ret = pipe(ctx->ctrl_pipe);
206 pthread_mutex_destroy(&ctx->ctx_lock);
207 pthread_mutex_destroy(&ctx->hotplug_lock);
208 pthread_cond_destroy(&ctx->ctx_cond);
210 return (LIBUSB_ERROR_OTHER);
212 /* set non-blocking mode on the control pipe to avoid deadlock */
213 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
214 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
216 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
218 pthread_mutex_lock(&default_context_lock);
219 if (usbi_default_context == NULL) {
220 usbi_default_context = ctx;
222 pthread_mutex_unlock(&default_context_lock);
227 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
233 libusb_exit(libusb_context *ctx)
235 ctx = GET_CONTEXT(ctx);
240 /* stop hotplug thread, if any */
242 if (ctx->hotplug_handler != NO_THREAD) {
247 td = ctx->hotplug_handler;
248 ctx->hotplug_handler = NO_THREAD;
251 pthread_join(td, &ptr);
254 /* XXX cleanup devices */
256 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
257 close(ctx->ctrl_pipe[0]);
258 close(ctx->ctrl_pipe[1]);
259 pthread_mutex_destroy(&ctx->ctx_lock);
260 pthread_mutex_destroy(&ctx->hotplug_lock);
261 pthread_cond_destroy(&ctx->ctx_cond);
263 pthread_mutex_lock(&default_context_lock);
264 if (ctx == usbi_default_context) {
265 usbi_default_context = NULL;
267 pthread_mutex_unlock(&default_context_lock);
272 /* Device handling and initialisation. */
275 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
277 struct libusb20_backend *usb_backend;
278 struct libusb20_device *pdev;
279 struct libusb_device *dev;
282 ctx = GET_CONTEXT(ctx);
285 return (LIBUSB_ERROR_INVALID_PARAM);
288 return (LIBUSB_ERROR_INVALID_PARAM);
290 usb_backend = libusb20_be_alloc_default();
291 if (usb_backend == NULL)
292 return (LIBUSB_ERROR_NO_MEM);
294 /* figure out how many USB devices are present */
297 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
300 /* allocate device pointer list */
301 *list = malloc((i + 1) * sizeof(void *));
303 libusb20_be_free(usb_backend);
304 return (LIBUSB_ERROR_NO_MEM);
306 /* create libusb v1.0 compliant devices */
308 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
310 dev = malloc(sizeof(*dev));
313 libusb_unref_device((*list)[i - 1]);
318 libusb20_be_free(usb_backend);
319 return (LIBUSB_ERROR_NO_MEM);
321 /* get device into libUSB v1.0 list */
322 libusb20_be_dequeue_device(usb_backend, pdev);
324 memset(dev, 0, sizeof(*dev));
326 /* init transfer queues */
327 TAILQ_INIT(&dev->tr_head);
329 /* set context we belong to */
332 /* link together the two structures */
334 pdev->privLuData = dev;
336 (*list)[i] = libusb_ref_device(dev);
341 libusb20_be_free(usb_backend);
346 libusb_free_device_list(libusb_device **list, int unref_devices)
351 return; /* be NULL safe */
354 for (i = 0; list[i] != NULL; i++)
355 libusb_unref_device(list[i]);
361 libusb_get_bus_number(libusb_device *dev)
364 return (0); /* should not happen */
365 return (libusb20_dev_get_bus_number(dev->os_priv));
369 libusb_get_port_number(libusb_device *dev)
372 return (0); /* should not happen */
373 return (libusb20_dev_get_parent_port(dev->os_priv));
377 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
379 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
383 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
386 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
390 libusb_get_device_address(libusb_device *dev)
393 return (0); /* should not happen */
394 return (libusb20_dev_get_address(dev->os_priv));
398 libusb_get_device_speed(libusb_device *dev)
401 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
403 switch (libusb20_dev_get_speed(dev->os_priv)) {
404 case LIBUSB20_SPEED_LOW:
405 return (LIBUSB_SPEED_LOW);
406 case LIBUSB20_SPEED_FULL:
407 return (LIBUSB_SPEED_FULL);
408 case LIBUSB20_SPEED_HIGH:
409 return (LIBUSB_SPEED_HIGH);
410 case LIBUSB20_SPEED_SUPER:
411 return (LIBUSB_SPEED_SUPER);
415 return (LIBUSB_SPEED_UNKNOWN);
419 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
421 struct libusb_config_descriptor *pdconf;
422 struct libusb_interface *pinf;
423 struct libusb_interface_descriptor *pdinf;
424 struct libusb_endpoint_descriptor *pdend;
431 return (LIBUSB_ERROR_NO_DEVICE);
433 ret = libusb_get_active_config_descriptor(dev, &pdconf);
437 ret = LIBUSB_ERROR_NOT_FOUND;
438 for (i = 0; i < pdconf->bNumInterfaces; i++) {
439 pinf = &pdconf->interface[i];
440 for (j = 0; j < pinf->num_altsetting; j++) {
441 pdinf = &pinf->altsetting[j];
442 for (k = 0; k < pdinf->bNumEndpoints; k++) {
443 pdend = &pdinf->endpoint[k];
444 if (pdend->bEndpointAddress == endpoint) {
445 ret = pdend->wMaxPacketSize;
453 libusb_free_config_descriptor(pdconf);
458 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
463 ret = libusb_get_max_packet_size(dev, endpoint);
465 switch (libusb20_dev_get_speed(dev->os_priv)) {
466 case LIBUSB20_SPEED_LOW:
467 case LIBUSB20_SPEED_FULL:
471 multiplier = (1 + ((ret >> 11) & 3));
474 ret = (ret & 0x7FF) * multiplier;
482 libusb_ref_device(libusb_device *dev)
485 return (NULL); /* be NULL safe */
489 CTX_UNLOCK(dev->ctx);
495 libusb_unref_device(libusb_device *dev)
498 return; /* be NULL safe */
502 CTX_UNLOCK(dev->ctx);
504 if (dev->refcnt == 0) {
505 libusb20_dev_free(dev->os_priv);
511 libusb_open(libusb_device *dev, libusb_device_handle **devh)
513 libusb_context *ctx = dev->ctx;
514 struct libusb20_device *pdev = dev->os_priv;
518 return (LIBUSB_ERROR_INVALID_PARAM);
520 /* set default device handle value */
523 dev = libusb_ref_device(dev);
525 return (LIBUSB_ERROR_INVALID_PARAM);
527 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
529 libusb_unref_device(dev);
530 return (LIBUSB_ERROR_NO_MEM);
532 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
533 POLLOUT | POLLRDNORM | POLLWRNORM);
535 /* make sure our event loop detects the new device */
536 libusb10_wakeup_event_loop(ctx);
543 libusb_device_handle *
544 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
547 struct libusb_device **devs;
548 struct libusb20_device *pdev;
549 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
553 ctx = GET_CONTEXT(ctx);
555 return (NULL); /* be NULL safe */
557 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter");
559 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
563 for (j = 0; j < i; j++) {
564 struct libusb20_device *tdev;
566 tdev = devs[j]->os_priv;
567 pdesc = libusb20_dev_get_device_desc(tdev);
569 * NOTE: The USB library will automatically swap the
570 * fields in the device descriptor to be of host
573 if (pdesc->idVendor == vendor_id &&
574 pdesc->idProduct == product_id) {
575 libusb_open(devs[j], &pdev);
580 libusb_free_device_list(devs, 1);
581 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave");
586 libusb_close(struct libusb20_device *pdev)
589 struct libusb_device *dev;
592 return; /* be NULL safe */
594 dev = libusb_get_device(pdev);
597 libusb10_remove_pollfd(ctx, &dev->dev_poll);
599 libusb20_dev_close(pdev);
601 /* unref will free the "pdev" when the refcount reaches zero */
602 libusb_unref_device(dev);
604 /* make sure our event loop detects the closed device */
605 libusb10_wakeup_event_loop(ctx);
609 libusb_get_device(struct libusb20_device *pdev)
613 return ((libusb_device *)pdev->privLuData);
617 libusb_get_configuration(struct libusb20_device *pdev, int *config)
619 struct libusb20_config *pconf;
621 if (pdev == NULL || config == NULL)
622 return (LIBUSB_ERROR_INVALID_PARAM);
624 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
626 return (LIBUSB_ERROR_NO_MEM);
628 *config = pconf->desc.bConfigurationValue;
636 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
638 struct libusb20_config *pconf;
639 struct libusb_device *dev;
643 dev = libusb_get_device(pdev);
645 return (LIBUSB_ERROR_INVALID_PARAM);
647 if (configuration < 1) {
651 for (i = 0; i != 255; i++) {
654 pconf = libusb20_dev_alloc_config(pdev, i);
656 return (LIBUSB_ERROR_INVALID_PARAM);
657 found = (pconf->desc.bConfigurationValue
664 return (LIBUSB_ERROR_INVALID_PARAM);
669 libusb10_cancel_all_transfer(dev);
671 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
673 err = libusb20_dev_set_config_index(pdev, i);
675 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
676 POLLOUT | POLLRDNORM | POLLWRNORM);
678 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
682 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
687 dev = libusb_get_device(pdev);
689 return (LIBUSB_ERROR_INVALID_PARAM);
691 if (interface_number < 0 || interface_number > 31)
692 return (LIBUSB_ERROR_INVALID_PARAM);
694 if (pdev->auto_detach != 0) {
695 err = libusb_detach_kernel_driver(pdev, interface_number);
701 dev->claimed_interfaces |= (1 << interface_number);
702 CTX_UNLOCK(dev->ctx);
708 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
713 dev = libusb_get_device(pdev);
715 return (LIBUSB_ERROR_INVALID_PARAM);
717 if (interface_number < 0 || interface_number > 31)
718 return (LIBUSB_ERROR_INVALID_PARAM);
720 if (pdev->auto_detach != 0) {
721 err = libusb_attach_kernel_driver(pdev, interface_number);
727 if (!(dev->claimed_interfaces & (1 << interface_number)))
728 err = LIBUSB_ERROR_NOT_FOUND;
730 dev->claimed_interfaces &= ~(1 << interface_number);
731 CTX_UNLOCK(dev->ctx);
737 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
738 int interface_number, int alternate_setting)
743 dev = libusb_get_device(pdev);
745 return (LIBUSB_ERROR_INVALID_PARAM);
747 if (interface_number < 0 || interface_number > 31)
748 return (LIBUSB_ERROR_INVALID_PARAM);
751 if (!(dev->claimed_interfaces & (1 << interface_number)))
752 err = LIBUSB_ERROR_NOT_FOUND;
753 CTX_UNLOCK(dev->ctx);
758 libusb10_cancel_all_transfer(dev);
760 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
762 err = libusb20_dev_set_alt_index(pdev,
763 interface_number, alternate_setting);
765 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
766 pdev, libusb20_dev_get_fd(pdev),
767 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
769 return (err ? LIBUSB_ERROR_OTHER : 0);
772 static struct libusb20_transfer *
773 libusb10_get_transfer(struct libusb20_device *pdev,
774 uint8_t endpoint, uint8_t xfer_index)
776 xfer_index &= 1; /* double buffering */
778 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
780 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
781 /* this is an IN endpoint */
784 return (libusb20_tr_get_pointer(pdev, xfer_index));
788 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
790 struct libusb20_transfer *xfer;
791 struct libusb_device *dev;
794 xfer = libusb10_get_transfer(pdev, endpoint, 0);
796 return (LIBUSB_ERROR_INVALID_PARAM);
798 dev = libusb_get_device(pdev);
800 return (LIBUSB_ERROR_INVALID_PARAM);
803 err = libusb20_tr_open(xfer, 0, 1, endpoint);
804 CTX_UNLOCK(dev->ctx);
806 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
807 return (LIBUSB_ERROR_OTHER);
809 libusb20_tr_clear_stall_sync(xfer);
811 /* check if we opened the transfer */
814 libusb20_tr_close(xfer);
815 CTX_UNLOCK(dev->ctx);
817 return (0); /* success */
821 libusb_reset_device(struct libusb20_device *pdev)
826 dev = libusb_get_device(pdev);
828 return (LIBUSB_ERROR_INVALID_PARAM);
830 libusb10_cancel_all_transfer(dev);
832 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
834 err = libusb20_dev_reset(pdev);
836 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
837 pdev, libusb20_dev_get_fd(pdev),
838 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
840 return (err ? LIBUSB_ERROR_OTHER : 0);
844 libusb_check_connected(struct libusb20_device *pdev)
849 dev = libusb_get_device(pdev);
851 return (LIBUSB_ERROR_INVALID_PARAM);
853 err = libusb20_dev_check_connected(pdev);
855 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
859 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
862 return (LIBUSB_ERROR_INVALID_PARAM);
864 if (libusb20_dev_kernel_driver_active(pdev, interface))
865 return (0); /* no kernel driver is active */
867 return (1); /* kernel driver is active */
871 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
872 char *name, int namelen)
874 return (libusb_get_driver(pdev, interface, name, namelen));
878 libusb_get_driver(struct libusb20_device *pdev, int interface,
879 char *name, int namelen)
885 return (LIBUSB_ERROR_INVALID_PARAM);
887 return (LIBUSB_ERROR_INVALID_PARAM);
891 err = libusb20_dev_get_iface_desc(
892 pdev, interface, name, namelen);
895 return (LIBUSB_ERROR_OTHER);
897 /* we only want the driver name */
898 ptr = strstr(name, ":");
906 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
908 return (libusb_detach_kernel_driver(pdev, interface));
912 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
917 return (LIBUSB_ERROR_INVALID_PARAM);
919 err = libusb20_dev_detach_kernel_driver(
922 return (err ? LIBUSB_ERROR_OTHER : 0);
926 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
929 return (LIBUSB_ERROR_INVALID_PARAM);
930 /* stub - currently not supported by libusb20 */
935 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
937 dev->auto_detach = (enable ? 1 : 0);
941 /* Asynchronous device I/O */
943 struct libusb_transfer *
944 libusb_alloc_transfer(int iso_packets)
946 struct libusb_transfer *uxfer;
947 struct libusb_super_transfer *sxfer;
950 len = sizeof(struct libusb_transfer) +
951 sizeof(struct libusb_super_transfer) +
952 (iso_packets * sizeof(libusb_iso_packet_descriptor));
958 memset(sxfer, 0, len);
960 uxfer = (struct libusb_transfer *)(
961 ((uint8_t *)sxfer) + sizeof(*sxfer));
963 /* set default value */
964 uxfer->num_iso_packets = iso_packets;
970 libusb_free_transfer(struct libusb_transfer *uxfer)
972 struct libusb_super_transfer *sxfer;
975 return; /* be NULL safe */
977 /* check if we should free the transfer buffer */
978 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
981 sxfer = (struct libusb_super_transfer *)(
982 (uint8_t *)uxfer - sizeof(*sxfer));
988 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
992 switch (xfer->type) {
993 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
994 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
996 case LIBUSB_TRANSFER_TYPE_CONTROL:
1007 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
1012 usb_speed = libusb20_dev_get_speed(pdev);
1014 switch (xfer->type) {
1015 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1016 ret = 0; /* kernel will auto-select */
1018 case LIBUSB_TRANSFER_TYPE_CONTROL:
1022 switch (usb_speed) {
1023 case LIBUSB20_SPEED_LOW:
1026 case LIBUSB20_SPEED_FULL:
1029 case LIBUSB20_SPEED_SUPER:
1042 libusb10_convert_error(uint8_t status)
1047 case LIBUSB20_TRANSFER_START:
1048 case LIBUSB20_TRANSFER_COMPLETED:
1049 return (LIBUSB_TRANSFER_COMPLETED);
1050 case LIBUSB20_TRANSFER_OVERFLOW:
1051 return (LIBUSB_TRANSFER_OVERFLOW);
1052 case LIBUSB20_TRANSFER_NO_DEVICE:
1053 return (LIBUSB_TRANSFER_NO_DEVICE);
1054 case LIBUSB20_TRANSFER_STALL:
1055 return (LIBUSB_TRANSFER_STALL);
1056 case LIBUSB20_TRANSFER_CANCELLED:
1057 return (LIBUSB_TRANSFER_CANCELLED);
1058 case LIBUSB20_TRANSFER_TIMED_OUT:
1059 return (LIBUSB_TRANSFER_TIMED_OUT);
1061 return (LIBUSB_TRANSFER_ERROR);
1065 /* This function must be called locked */
1068 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1069 struct libusb_super_transfer *sxfer, int status)
1071 struct libusb_transfer *uxfer;
1072 struct libusb_device *dev;
1074 uxfer = (struct libusb_transfer *)(
1075 ((uint8_t *)sxfer) + sizeof(*sxfer));
1078 libusb20_tr_set_priv_sc1(pxfer, NULL);
1080 /* set transfer status */
1081 uxfer->status = status;
1083 /* update super transfer state */
1084 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1086 dev = libusb_get_device(uxfer->dev_handle);
1088 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1091 /* This function must be called locked */
1094 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1096 struct libusb_super_transfer *sxfer;
1097 struct libusb_transfer *uxfer;
1099 uint16_t iso_packets;
1103 status = libusb20_tr_get_status(pxfer);
1104 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1105 actlen = libusb20_tr_get_actual_length(pxfer);
1106 iso_packets = libusb20_tr_get_max_frames(pxfer);
1109 return; /* cancelled - nothing to do */
1111 uxfer = (struct libusb_transfer *)(
1112 ((uint8_t *)sxfer) + sizeof(*sxfer));
1114 if (iso_packets > uxfer->num_iso_packets)
1115 iso_packets = uxfer->num_iso_packets;
1117 if (iso_packets == 0)
1118 return; /* nothing to do */
1120 /* make sure that the number of ISOCHRONOUS packets is valid */
1121 uxfer->num_iso_packets = iso_packets;
1124 case LIBUSB20_TRANSFER_COMPLETED:
1125 /* update actual length */
1126 uxfer->actual_length = actlen;
1127 for (i = 0; i != iso_packets; i++) {
1128 uxfer->iso_packet_desc[i].actual_length =
1129 libusb20_tr_get_length(pxfer, i);
1131 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1133 case LIBUSB20_TRANSFER_START:
1134 /* setup length(s) */
1136 for (i = 0; i != iso_packets; i++) {
1137 libusb20_tr_setup_isoc(pxfer,
1138 &uxfer->buffer[actlen],
1139 uxfer->iso_packet_desc[i].length, i);
1140 actlen += uxfer->iso_packet_desc[i].length;
1146 libusb20_tr_set_total_frames(pxfer, iso_packets);
1147 libusb20_tr_submit(pxfer);
1149 /* fork another USB transfer, if any */
1150 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1153 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1158 /* This function must be called locked */
1161 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1163 struct libusb_super_transfer *sxfer;
1164 struct libusb_transfer *uxfer;
1170 status = libusb20_tr_get_status(pxfer);
1171 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1172 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1173 actlen = libusb20_tr_get_actual_length(pxfer);
1176 return; /* cancelled - nothing to do */
1178 uxfer = (struct libusb_transfer *)(
1179 ((uint8_t *)sxfer) + sizeof(*sxfer));
1181 flags = uxfer->flags;
1184 case LIBUSB20_TRANSFER_COMPLETED:
1186 uxfer->actual_length += actlen;
1188 /* check for short packet */
1189 if (sxfer->last_len != actlen) {
1190 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1191 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1193 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1197 /* check for end of data */
1198 if (sxfer->rem_len == 0) {
1199 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1204 case LIBUSB20_TRANSFER_START:
1205 if (max_bulk > sxfer->rem_len) {
1206 max_bulk = sxfer->rem_len;
1208 /* setup new BULK or INTERRUPT transaction */
1209 libusb20_tr_setup_bulk(pxfer,
1210 sxfer->curr_data, max_bulk, uxfer->timeout);
1212 /* update counters */
1213 sxfer->last_len = max_bulk;
1214 sxfer->curr_data += max_bulk;
1215 sxfer->rem_len -= max_bulk;
1217 libusb20_tr_submit(pxfer);
1219 /* check if we can fork another USB transfer */
1220 if (sxfer->rem_len == 0)
1221 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1225 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1230 /* This function must be called locked */
1233 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1235 struct libusb_super_transfer *sxfer;
1236 struct libusb_transfer *uxfer;
1242 status = libusb20_tr_get_status(pxfer);
1243 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1244 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1245 actlen = libusb20_tr_get_actual_length(pxfer);
1248 return; /* cancelled - nothing to do */
1250 uxfer = (struct libusb_transfer *)(
1251 ((uint8_t *)sxfer) + sizeof(*sxfer));
1253 flags = uxfer->flags;
1256 case LIBUSB20_TRANSFER_COMPLETED:
1258 uxfer->actual_length += actlen;
1260 /* subtract length of SETUP packet, if any */
1261 actlen -= libusb20_tr_get_length(pxfer, 0);
1263 /* check for short packet */
1264 if (sxfer->last_len != actlen) {
1265 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1266 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1268 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1272 /* check for end of data */
1273 if (sxfer->rem_len == 0) {
1274 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1279 case LIBUSB20_TRANSFER_START:
1280 if (max_bulk > sxfer->rem_len) {
1281 max_bulk = sxfer->rem_len;
1283 /* setup new CONTROL transaction */
1284 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1285 /* next fragment - don't send SETUP packet */
1286 libusb20_tr_set_length(pxfer, 0, 0);
1288 /* first fragment - send SETUP packet */
1289 libusb20_tr_set_length(pxfer, 8, 0);
1290 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1293 if (max_bulk != 0) {
1294 libusb20_tr_set_length(pxfer, max_bulk, 1);
1295 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1296 libusb20_tr_set_total_frames(pxfer, 2);
1298 libusb20_tr_set_total_frames(pxfer, 1);
1301 /* update counters */
1302 sxfer->last_len = max_bulk;
1303 sxfer->curr_data += max_bulk;
1304 sxfer->rem_len -= max_bulk;
1306 libusb20_tr_submit(pxfer);
1308 /* check if we can fork another USB transfer */
1309 if (sxfer->rem_len == 0)
1310 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1314 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1319 /* The following function must be called locked */
1322 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1324 struct libusb20_transfer *pxfer0;
1325 struct libusb20_transfer *pxfer1;
1326 struct libusb_super_transfer *sxfer;
1327 struct libusb_transfer *uxfer;
1328 struct libusb_device *dev;
1334 dev = libusb_get_device(pdev);
1336 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1337 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1339 if (pxfer0 == NULL || pxfer1 == NULL)
1340 return; /* shouldn't happen */
1343 if (libusb20_tr_pending(pxfer0))
1345 if (libusb20_tr_pending(pxfer1))
1350 /* wait till one of the transfers complete */
1353 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1355 return; /* cancelling */
1357 return; /* cannot queue another one */
1358 /* swap transfers */
1362 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1364 return; /* cancelling */
1366 return; /* cannot queue another one */
1367 /* swap transfers */
1374 /* find next transfer on same endpoint */
1375 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1377 uxfer = (struct libusb_transfer *)(
1378 ((uint8_t *)sxfer) + sizeof(*sxfer));
1380 if (uxfer->endpoint == endpoint) {
1381 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1382 sxfer->entry.tqe_prev = NULL;
1386 return; /* success */
1390 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1391 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1393 /* reset super transfer state */
1394 sxfer->rem_len = uxfer->length;
1395 sxfer->curr_data = uxfer->buffer;
1396 uxfer->actual_length = 0;
1398 switch (uxfer->type) {
1399 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1400 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1402 case LIBUSB_TRANSFER_TYPE_BULK:
1403 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1404 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1406 case LIBUSB_TRANSFER_TYPE_CONTROL:
1407 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1408 if (sxfer->rem_len < 8)
1411 /* remove SETUP packet from data */
1412 sxfer->rem_len -= 8;
1413 sxfer->curr_data += 8;
1419 buffsize = libusb10_get_buffsize(pdev, uxfer);
1420 maxframe = libusb10_get_maxframe(pdev, uxfer);
1422 /* make sure the transfer is opened */
1423 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1424 endpoint, sxfer->stream_id);
1425 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1428 libusb20_tr_start(pxfer0);
1432 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1433 /* make sure our event loop spins the done handler */
1434 libusb10_wakeup_event_loop(dev->ctx);
1437 /* The following function must be called unlocked */
1440 libusb_submit_transfer(struct libusb_transfer *uxfer)
1442 struct libusb20_transfer *pxfer0;
1443 struct libusb20_transfer *pxfer1;
1444 struct libusb_super_transfer *sxfer;
1445 struct libusb_device *dev;
1450 return (LIBUSB_ERROR_INVALID_PARAM);
1452 if (uxfer->dev_handle == NULL)
1453 return (LIBUSB_ERROR_INVALID_PARAM);
1455 endpoint = uxfer->endpoint;
1457 dev = libusb_get_device(uxfer->dev_handle);
1459 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1461 sxfer = (struct libusb_super_transfer *)(
1462 (uint8_t *)uxfer - sizeof(*sxfer));
1466 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1467 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1469 if (pxfer0 == NULL || pxfer1 == NULL) {
1470 err = LIBUSB_ERROR_OTHER;
1471 } else if ((sxfer->entry.tqe_prev != NULL) ||
1472 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1473 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1474 err = LIBUSB_ERROR_BUSY;
1475 } else if (dev->device_is_gone != 0) {
1476 err = LIBUSB_ERROR_NO_DEVICE;
1479 /* set pending state */
1480 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1482 /* insert transfer into transfer head list */
1483 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1485 /* start work transfers */
1486 libusb10_submit_transfer_sub(
1487 uxfer->dev_handle, endpoint);
1489 err = 0; /* success */
1492 CTX_UNLOCK(dev->ctx);
1494 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1499 /* Asynchronous transfer cancel */
1502 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1504 struct libusb20_transfer *pxfer0;
1505 struct libusb20_transfer *pxfer1;
1506 struct libusb_super_transfer *sxfer;
1507 struct libusb_device *dev;
1508 struct libusb_device_handle *devh;
1513 return (LIBUSB_ERROR_INVALID_PARAM);
1515 /* check if not initialised */
1516 if ((devh = uxfer->dev_handle) == NULL)
1517 return (LIBUSB_ERROR_NOT_FOUND);
1519 endpoint = uxfer->endpoint;
1521 dev = libusb_get_device(devh);
1523 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1525 sxfer = (struct libusb_super_transfer *)(
1526 (uint8_t *)uxfer - sizeof(*sxfer));
1532 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1533 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1535 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1536 /* only update the transfer status */
1537 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1538 retval = LIBUSB_ERROR_NOT_FOUND;
1539 } else if (sxfer->entry.tqe_prev != NULL) {
1540 /* we are lucky - transfer is on a queue */
1541 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1542 sxfer->entry.tqe_prev = NULL;
1543 libusb10_complete_transfer(NULL,
1544 sxfer, LIBUSB_TRANSFER_CANCELLED);
1545 /* make sure our event loop spins the done handler */
1546 libusb10_wakeup_event_loop(dev->ctx);
1547 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1549 retval = LIBUSB_ERROR_NOT_FOUND;
1550 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1551 libusb10_complete_transfer(pxfer0,
1552 sxfer, LIBUSB_TRANSFER_CANCELLED);
1553 if (dev->device_is_gone != 0) {
1554 /* clear transfer pointer */
1555 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1556 /* make sure our event loop spins the done handler */
1557 libusb10_wakeup_event_loop(dev->ctx);
1559 libusb20_tr_stop(pxfer0);
1560 /* make sure the queue doesn't stall */
1561 libusb10_submit_transfer_sub(devh, endpoint);
1563 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1564 libusb10_complete_transfer(pxfer1,
1565 sxfer, LIBUSB_TRANSFER_CANCELLED);
1566 /* check if handle is still active */
1567 if (dev->device_is_gone != 0) {
1568 /* clear transfer pointer */
1569 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1570 /* make sure our event loop spins the done handler */
1571 libusb10_wakeup_event_loop(dev->ctx);
1573 libusb20_tr_stop(pxfer1);
1574 /* make sure the queue doesn't stall */
1575 libusb10_submit_transfer_sub(devh, endpoint);
1579 retval = LIBUSB_ERROR_NOT_FOUND;
1582 CTX_UNLOCK(dev->ctx);
1584 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1590 libusb10_cancel_all_transfer(libusb_device *dev)
1592 struct libusb20_device *pdev = dev->os_priv;
1595 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1596 struct libusb20_transfer *xfer;
1598 xfer = libusb20_tr_get_pointer(pdev, x);
1601 libusb20_tr_close(xfer);
1606 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1608 struct libusb_super_transfer *sxfer;
1611 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1612 struct libusb20_transfer *xfer;
1614 xfer = libusb20_tr_get_pointer(pdev, x);
1617 if (libusb20_tr_pending(xfer) == 0)
1619 sxfer = libusb20_tr_get_priv_sc1(xfer);
1622 /* complete pending transfer */
1623 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1626 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1627 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1629 /* complete pending transfer */
1630 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1635 libusb_cpu_to_le16(uint16_t x)
1637 return (htole16(x));
1641 libusb_le16_to_cpu(uint16_t x)
1643 return (le16toh(x));
1647 libusb_strerror(int code)
1650 case LIBUSB_SUCCESS:
1652 case LIBUSB_ERROR_IO:
1653 return ("I/O error");
1654 case LIBUSB_ERROR_INVALID_PARAM:
1655 return ("Invalid parameter");
1656 case LIBUSB_ERROR_ACCESS:
1657 return ("Permissions error");
1658 case LIBUSB_ERROR_NO_DEVICE:
1659 return ("No device");
1660 case LIBUSB_ERROR_NOT_FOUND:
1661 return ("Not found");
1662 case LIBUSB_ERROR_BUSY:
1663 return ("Device busy");
1664 case LIBUSB_ERROR_TIMEOUT:
1666 case LIBUSB_ERROR_OVERFLOW:
1667 return ("Overflow");
1668 case LIBUSB_ERROR_PIPE:
1669 return ("Pipe error");
1670 case LIBUSB_ERROR_INTERRUPTED:
1671 return ("Interrupted");
1672 case LIBUSB_ERROR_NO_MEM:
1673 return ("Out of memory");
1674 case LIBUSB_ERROR_NOT_SUPPORTED:
1675 return ("Not supported");
1676 case LIBUSB_ERROR_OTHER:
1677 return ("Other error");
1679 return ("Unknown error");
1684 libusb_error_name(int code)
1687 case LIBUSB_SUCCESS:
1688 return ("LIBUSB_SUCCESS");
1689 case LIBUSB_ERROR_IO:
1690 return ("LIBUSB_ERROR_IO");
1691 case LIBUSB_ERROR_INVALID_PARAM:
1692 return ("LIBUSB_ERROR_INVALID_PARAM");
1693 case LIBUSB_ERROR_ACCESS:
1694 return ("LIBUSB_ERROR_ACCESS");
1695 case LIBUSB_ERROR_NO_DEVICE:
1696 return ("LIBUSB_ERROR_NO_DEVICE");
1697 case LIBUSB_ERROR_NOT_FOUND:
1698 return ("LIBUSB_ERROR_NOT_FOUND");
1699 case LIBUSB_ERROR_BUSY:
1700 return ("LIBUSB_ERROR_BUSY");
1701 case LIBUSB_ERROR_TIMEOUT:
1702 return ("LIBUSB_ERROR_TIMEOUT");
1703 case LIBUSB_ERROR_OVERFLOW:
1704 return ("LIBUSB_ERROR_OVERFLOW");
1705 case LIBUSB_ERROR_PIPE:
1706 return ("LIBUSB_ERROR_PIPE");
1707 case LIBUSB_ERROR_INTERRUPTED:
1708 return ("LIBUSB_ERROR_INTERRUPTED");
1709 case LIBUSB_ERROR_NO_MEM:
1710 return ("LIBUSB_ERROR_NO_MEM");
1711 case LIBUSB_ERROR_NOT_SUPPORTED:
1712 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1713 case LIBUSB_ERROR_OTHER:
1714 return ("LIBUSB_ERROR_OTHER");
1716 return ("LIBUSB_ERROR_UNKNOWN");
1721 libusb_has_capability(uint32_t capability)
1724 switch (capability) {
1725 case LIBUSB_CAP_HAS_CAPABILITY:
1726 case LIBUSB_CAP_HAS_HOTPLUG:
1727 case LIBUSB_CAP_HAS_HID_ACCESS:
1728 case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER: