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);
99 libusb_set_nonblocking(int f)
104 * We ignore any failures in this function, hence the
105 * non-blocking flag is not critical to the operation of
106 * libUSB. We use F_GETFL and F_SETFL to be compatible with
110 flags = fcntl(f, F_GETFL, NULL);
114 fcntl(f, F_SETFL, flags);
118 libusb_init(libusb_context **context)
120 struct libusb_context *ctx;
121 pthread_condattr_t attr;
125 ctx = malloc(sizeof(*ctx));
127 return (LIBUSB_ERROR_INVALID_PARAM);
129 memset(ctx, 0, sizeof(*ctx));
131 debug = getenv("LIBUSB_DEBUG");
133 ctx->debug = atoi(debug);
135 ctx->debug_fixed = 1;
137 TAILQ_INIT(&ctx->pollfds);
138 TAILQ_INIT(&ctx->tr_done);
139 TAILQ_INIT(&ctx->hotplug_cbh);
140 TAILQ_INIT(&ctx->hotplug_devs);
142 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
144 return (LIBUSB_ERROR_NO_MEM);
146 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
147 pthread_mutex_destroy(&ctx->ctx_lock);
149 return (LIBUSB_ERROR_NO_MEM);
151 if (pthread_condattr_init(&attr) != 0) {
152 pthread_mutex_destroy(&ctx->ctx_lock);
153 pthread_mutex_destroy(&ctx->hotplug_lock);
155 return (LIBUSB_ERROR_NO_MEM);
157 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
158 pthread_mutex_destroy(&ctx->ctx_lock);
159 pthread_mutex_destroy(&ctx->hotplug_lock);
160 pthread_condattr_destroy(&attr);
162 return (LIBUSB_ERROR_OTHER);
164 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
165 pthread_mutex_destroy(&ctx->ctx_lock);
166 pthread_mutex_destroy(&ctx->hotplug_lock);
167 pthread_condattr_destroy(&attr);
169 return (LIBUSB_ERROR_NO_MEM);
171 pthread_condattr_destroy(&attr);
173 ctx->ctx_handler = NO_THREAD;
174 ctx->hotplug_handler = NO_THREAD;
176 ret = pipe(ctx->ctrl_pipe);
178 pthread_mutex_destroy(&ctx->ctx_lock);
179 pthread_mutex_destroy(&ctx->hotplug_lock);
180 pthread_cond_destroy(&ctx->ctx_cond);
182 return (LIBUSB_ERROR_OTHER);
184 /* set non-blocking mode on the control pipe to avoid deadlock */
185 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
186 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
188 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
190 pthread_mutex_lock(&default_context_lock);
191 if (usbi_default_context == NULL) {
192 usbi_default_context = ctx;
194 pthread_mutex_unlock(&default_context_lock);
199 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
205 libusb_exit(libusb_context *ctx)
207 ctx = GET_CONTEXT(ctx);
212 /* stop hotplug thread, if any */
214 if (ctx->hotplug_handler != NO_THREAD) {
219 td = ctx->hotplug_handler;
220 ctx->hotplug_handler = NO_THREAD;
223 pthread_join(td, &ptr);
226 /* XXX cleanup devices */
228 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
229 close(ctx->ctrl_pipe[0]);
230 close(ctx->ctrl_pipe[1]);
231 pthread_mutex_destroy(&ctx->ctx_lock);
232 pthread_mutex_destroy(&ctx->hotplug_lock);
233 pthread_cond_destroy(&ctx->ctx_cond);
235 pthread_mutex_lock(&default_context_lock);
236 if (ctx == usbi_default_context) {
237 usbi_default_context = NULL;
239 pthread_mutex_unlock(&default_context_lock);
244 /* Device handling and initialisation. */
247 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
249 struct libusb20_backend *usb_backend;
250 struct libusb20_device *pdev;
251 struct libusb_device *dev;
254 ctx = GET_CONTEXT(ctx);
257 return (LIBUSB_ERROR_INVALID_PARAM);
260 return (LIBUSB_ERROR_INVALID_PARAM);
262 usb_backend = libusb20_be_alloc_default();
263 if (usb_backend == NULL)
264 return (LIBUSB_ERROR_NO_MEM);
266 /* figure out how many USB devices are present */
269 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
272 /* allocate device pointer list */
273 *list = malloc((i + 1) * sizeof(void *));
275 libusb20_be_free(usb_backend);
276 return (LIBUSB_ERROR_NO_MEM);
278 /* create libusb v1.0 compliant devices */
280 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
282 dev = malloc(sizeof(*dev));
285 libusb_unref_device((*list)[i - 1]);
290 libusb20_be_free(usb_backend);
291 return (LIBUSB_ERROR_NO_MEM);
293 /* get device into libUSB v1.0 list */
294 libusb20_be_dequeue_device(usb_backend, pdev);
296 memset(dev, 0, sizeof(*dev));
298 /* init transfer queues */
299 TAILQ_INIT(&dev->tr_head);
301 /* set context we belong to */
304 /* link together the two structures */
306 pdev->privLuData = dev;
308 (*list)[i] = libusb_ref_device(dev);
313 libusb20_be_free(usb_backend);
318 libusb_free_device_list(libusb_device **list, int unref_devices)
323 return; /* be NULL safe */
326 for (i = 0; list[i] != NULL; i++)
327 libusb_unref_device(list[i]);
333 libusb_get_bus_number(libusb_device *dev)
336 return (0); /* should not happen */
337 return (libusb20_dev_get_bus_number(dev->os_priv));
341 libusb_get_port_number(libusb_device *dev)
344 return (0); /* should not happen */
345 return (libusb20_dev_get_parent_port(dev->os_priv));
349 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
351 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
355 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
358 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
362 libusb_get_device_address(libusb_device *dev)
365 return (0); /* should not happen */
366 return (libusb20_dev_get_address(dev->os_priv));
370 libusb_get_device_speed(libusb_device *dev)
373 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
375 switch (libusb20_dev_get_speed(dev->os_priv)) {
376 case LIBUSB20_SPEED_LOW:
377 return (LIBUSB_SPEED_LOW);
378 case LIBUSB20_SPEED_FULL:
379 return (LIBUSB_SPEED_FULL);
380 case LIBUSB20_SPEED_HIGH:
381 return (LIBUSB_SPEED_HIGH);
382 case LIBUSB20_SPEED_SUPER:
383 return (LIBUSB_SPEED_SUPER);
387 return (LIBUSB_SPEED_UNKNOWN);
391 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
393 struct libusb_config_descriptor *pdconf;
394 struct libusb_interface *pinf;
395 struct libusb_interface_descriptor *pdinf;
396 struct libusb_endpoint_descriptor *pdend;
403 return (LIBUSB_ERROR_NO_DEVICE);
405 ret = libusb_get_active_config_descriptor(dev, &pdconf);
409 ret = LIBUSB_ERROR_NOT_FOUND;
410 for (i = 0; i < pdconf->bNumInterfaces; i++) {
411 pinf = &pdconf->interface[i];
412 for (j = 0; j < pinf->num_altsetting; j++) {
413 pdinf = &pinf->altsetting[j];
414 for (k = 0; k < pdinf->bNumEndpoints; k++) {
415 pdend = &pdinf->endpoint[k];
416 if (pdend->bEndpointAddress == endpoint) {
417 ret = pdend->wMaxPacketSize;
425 libusb_free_config_descriptor(pdconf);
430 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
435 ret = libusb_get_max_packet_size(dev, endpoint);
437 switch (libusb20_dev_get_speed(dev->os_priv)) {
438 case LIBUSB20_SPEED_LOW:
439 case LIBUSB20_SPEED_FULL:
443 multiplier = (1 + ((ret >> 11) & 3));
446 ret = (ret & 0x7FF) * multiplier;
454 libusb_ref_device(libusb_device *dev)
457 return (NULL); /* be NULL safe */
461 CTX_UNLOCK(dev->ctx);
467 libusb_unref_device(libusb_device *dev)
470 return; /* be NULL safe */
474 CTX_UNLOCK(dev->ctx);
476 if (dev->refcnt == 0) {
477 libusb20_dev_free(dev->os_priv);
483 libusb_open(libusb_device *dev, libusb_device_handle **devh)
485 libusb_context *ctx = dev->ctx;
486 struct libusb20_device *pdev = dev->os_priv;
491 return (LIBUSB_ERROR_INVALID_PARAM);
493 /* set default device handle value */
496 dev = libusb_ref_device(dev);
498 return (LIBUSB_ERROR_INVALID_PARAM);
500 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
502 libusb_unref_device(dev);
503 return (LIBUSB_ERROR_NO_MEM);
505 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
506 POLLOUT | POLLRDNORM | POLLWRNORM);
508 /* make sure our event loop detects the new device */
510 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
511 if (err < (int)sizeof(dummy)) {
512 /* ignore error, if any */
513 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
520 libusb_device_handle *
521 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
524 struct libusb_device **devs;
525 struct libusb20_device *pdev;
526 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
530 ctx = GET_CONTEXT(ctx);
532 return (NULL); /* be NULL safe */
534 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
536 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
540 for (j = 0; j < i; j++) {
541 struct libusb20_device *tdev;
543 tdev = devs[j]->os_priv;
544 pdesc = libusb20_dev_get_device_desc(tdev);
546 * NOTE: The USB library will automatically swap the
547 * fields in the device descriptor to be of host
550 if (pdesc->idVendor == vendor_id &&
551 pdesc->idProduct == product_id) {
552 libusb_open(devs[j], &pdev);
557 libusb_free_device_list(devs, 1);
558 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
563 libusb_close(struct libusb20_device *pdev)
566 struct libusb_device *dev;
571 return; /* be NULL safe */
573 dev = libusb_get_device(pdev);
576 libusb10_remove_pollfd(ctx, &dev->dev_poll);
578 libusb20_dev_close(pdev);
580 /* unref will free the "pdev" when the refcount reaches zero */
581 libusb_unref_device(dev);
583 /* make sure our event loop detects the closed device */
585 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
586 if (err < (int)sizeof(dummy)) {
587 /* ignore error, if any */
588 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
593 libusb_get_device(struct libusb20_device *pdev)
597 return ((libusb_device *)pdev->privLuData);
601 libusb_get_configuration(struct libusb20_device *pdev, int *config)
603 struct libusb20_config *pconf;
605 if (pdev == NULL || config == NULL)
606 return (LIBUSB_ERROR_INVALID_PARAM);
608 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
610 return (LIBUSB_ERROR_NO_MEM);
612 *config = pconf->desc.bConfigurationValue;
620 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
622 struct libusb20_config *pconf;
623 struct libusb_device *dev;
627 dev = libusb_get_device(pdev);
629 return (LIBUSB_ERROR_INVALID_PARAM);
631 if (configuration < 1) {
635 for (i = 0; i != 255; i++) {
638 pconf = libusb20_dev_alloc_config(pdev, i);
640 return (LIBUSB_ERROR_INVALID_PARAM);
641 found = (pconf->desc.bConfigurationValue
648 return (LIBUSB_ERROR_INVALID_PARAM);
653 libusb10_cancel_all_transfer(dev);
655 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
657 err = libusb20_dev_set_config_index(pdev, i);
659 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
660 POLLOUT | POLLRDNORM | POLLWRNORM);
662 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
666 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
671 dev = libusb_get_device(pdev);
673 return (LIBUSB_ERROR_INVALID_PARAM);
675 if (interface_number < 0 || interface_number > 31)
676 return (LIBUSB_ERROR_INVALID_PARAM);
678 if (pdev->auto_detach != 0) {
679 err = libusb_detach_kernel_driver(pdev, interface_number);
685 dev->claimed_interfaces |= (1 << interface_number);
686 CTX_UNLOCK(dev->ctx);
692 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
697 dev = libusb_get_device(pdev);
699 return (LIBUSB_ERROR_INVALID_PARAM);
701 if (interface_number < 0 || interface_number > 31)
702 return (LIBUSB_ERROR_INVALID_PARAM);
704 if (pdev->auto_detach != 0) {
705 err = libusb_attach_kernel_driver(pdev, interface_number);
711 if (!(dev->claimed_interfaces & (1 << interface_number)))
712 err = LIBUSB_ERROR_NOT_FOUND;
714 dev->claimed_interfaces &= ~(1 << interface_number);
715 CTX_UNLOCK(dev->ctx);
721 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
722 int interface_number, int alternate_setting)
727 dev = libusb_get_device(pdev);
729 return (LIBUSB_ERROR_INVALID_PARAM);
731 if (interface_number < 0 || interface_number > 31)
732 return (LIBUSB_ERROR_INVALID_PARAM);
735 if (!(dev->claimed_interfaces & (1 << interface_number)))
736 err = LIBUSB_ERROR_NOT_FOUND;
737 CTX_UNLOCK(dev->ctx);
742 libusb10_cancel_all_transfer(dev);
744 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
746 err = libusb20_dev_set_alt_index(pdev,
747 interface_number, alternate_setting);
749 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
750 pdev, libusb20_dev_get_fd(pdev),
751 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
753 return (err ? LIBUSB_ERROR_OTHER : 0);
756 static struct libusb20_transfer *
757 libusb10_get_transfer(struct libusb20_device *pdev,
758 uint8_t endpoint, uint8_t xfer_index)
760 xfer_index &= 1; /* double buffering */
762 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
764 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
765 /* this is an IN endpoint */
768 return (libusb20_tr_get_pointer(pdev, xfer_index));
772 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
774 struct libusb20_transfer *xfer;
775 struct libusb_device *dev;
778 xfer = libusb10_get_transfer(pdev, endpoint, 0);
780 return (LIBUSB_ERROR_INVALID_PARAM);
782 dev = libusb_get_device(pdev);
784 return (LIBUSB_ERROR_INVALID_PARAM);
787 err = libusb20_tr_open(xfer, 0, 1, endpoint);
788 CTX_UNLOCK(dev->ctx);
790 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
791 return (LIBUSB_ERROR_OTHER);
793 libusb20_tr_clear_stall_sync(xfer);
795 /* check if we opened the transfer */
798 libusb20_tr_close(xfer);
799 CTX_UNLOCK(dev->ctx);
801 return (0); /* success */
805 libusb_reset_device(struct libusb20_device *pdev)
810 dev = libusb_get_device(pdev);
812 return (LIBUSB_ERROR_INVALID_PARAM);
814 libusb10_cancel_all_transfer(dev);
816 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
818 err = libusb20_dev_reset(pdev);
820 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
821 pdev, libusb20_dev_get_fd(pdev),
822 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
824 return (err ? LIBUSB_ERROR_OTHER : 0);
828 libusb_check_connected(struct libusb20_device *pdev)
833 dev = libusb_get_device(pdev);
835 return (LIBUSB_ERROR_INVALID_PARAM);
837 err = libusb20_dev_check_connected(pdev);
839 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
843 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
846 return (LIBUSB_ERROR_INVALID_PARAM);
848 if (libusb20_dev_kernel_driver_active(pdev, interface))
849 return (0); /* no kernel driver is active */
851 return (1); /* kernel driver is active */
855 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
856 char *name, int namelen)
858 return (libusb_get_driver(pdev, interface, name, namelen));
862 libusb_get_driver(struct libusb20_device *pdev, int interface,
863 char *name, int namelen)
869 return (LIBUSB_ERROR_INVALID_PARAM);
871 return (LIBUSB_ERROR_INVALID_PARAM);
875 err = libusb20_dev_get_iface_desc(
876 pdev, interface, name, namelen);
879 return (LIBUSB_ERROR_OTHER);
881 /* we only want the driver name */
882 ptr = strstr(name, ":");
890 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
892 return (libusb_detach_kernel_driver(pdev, interface));
896 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
901 return (LIBUSB_ERROR_INVALID_PARAM);
903 err = libusb20_dev_detach_kernel_driver(
906 return (err ? LIBUSB_ERROR_OTHER : 0);
910 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
913 return (LIBUSB_ERROR_INVALID_PARAM);
914 /* stub - currently not supported by libusb20 */
919 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
921 dev->auto_detach = (enable ? 1 : 0);
925 /* Asynchronous device I/O */
927 struct libusb_transfer *
928 libusb_alloc_transfer(int iso_packets)
930 struct libusb_transfer *uxfer;
931 struct libusb_super_transfer *sxfer;
934 len = sizeof(struct libusb_transfer) +
935 sizeof(struct libusb_super_transfer) +
936 (iso_packets * sizeof(libusb_iso_packet_descriptor));
942 memset(sxfer, 0, len);
944 uxfer = (struct libusb_transfer *)(
945 ((uint8_t *)sxfer) + sizeof(*sxfer));
947 /* set default value */
948 uxfer->num_iso_packets = iso_packets;
954 libusb_free_transfer(struct libusb_transfer *uxfer)
956 struct libusb_super_transfer *sxfer;
959 return; /* be NULL safe */
961 /* check if we should free the transfer buffer */
962 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
965 sxfer = (struct libusb_super_transfer *)(
966 (uint8_t *)uxfer - sizeof(*sxfer));
972 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
976 switch (xfer->type) {
977 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
978 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
980 case LIBUSB_TRANSFER_TYPE_CONTROL:
991 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
996 usb_speed = libusb20_dev_get_speed(pdev);
998 switch (xfer->type) {
999 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1000 ret = 0; /* kernel will auto-select */
1002 case LIBUSB_TRANSFER_TYPE_CONTROL:
1006 switch (usb_speed) {
1007 case LIBUSB20_SPEED_LOW:
1010 case LIBUSB20_SPEED_FULL:
1013 case LIBUSB20_SPEED_SUPER:
1026 libusb10_convert_error(uint8_t status)
1031 case LIBUSB20_TRANSFER_START:
1032 case LIBUSB20_TRANSFER_COMPLETED:
1033 return (LIBUSB_TRANSFER_COMPLETED);
1034 case LIBUSB20_TRANSFER_OVERFLOW:
1035 return (LIBUSB_TRANSFER_OVERFLOW);
1036 case LIBUSB20_TRANSFER_NO_DEVICE:
1037 return (LIBUSB_TRANSFER_NO_DEVICE);
1038 case LIBUSB20_TRANSFER_STALL:
1039 return (LIBUSB_TRANSFER_STALL);
1040 case LIBUSB20_TRANSFER_CANCELLED:
1041 return (LIBUSB_TRANSFER_CANCELLED);
1042 case LIBUSB20_TRANSFER_TIMED_OUT:
1043 return (LIBUSB_TRANSFER_TIMED_OUT);
1045 return (LIBUSB_TRANSFER_ERROR);
1049 /* This function must be called locked */
1052 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1053 struct libusb_super_transfer *sxfer, int status)
1055 struct libusb_transfer *uxfer;
1056 struct libusb_device *dev;
1058 uxfer = (struct libusb_transfer *)(
1059 ((uint8_t *)sxfer) + sizeof(*sxfer));
1062 libusb20_tr_set_priv_sc1(pxfer, NULL);
1064 /* set transfer status */
1065 uxfer->status = status;
1067 /* update super transfer state */
1068 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1070 dev = libusb_get_device(uxfer->dev_handle);
1072 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1075 /* This function must be called locked */
1078 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1080 struct libusb_super_transfer *sxfer;
1081 struct libusb_transfer *uxfer;
1083 uint16_t iso_packets;
1087 status = libusb20_tr_get_status(pxfer);
1088 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1089 actlen = libusb20_tr_get_actual_length(pxfer);
1090 iso_packets = libusb20_tr_get_max_frames(pxfer);
1093 return; /* cancelled - nothing to do */
1095 uxfer = (struct libusb_transfer *)(
1096 ((uint8_t *)sxfer) + sizeof(*sxfer));
1098 if (iso_packets > uxfer->num_iso_packets)
1099 iso_packets = uxfer->num_iso_packets;
1101 if (iso_packets == 0)
1102 return; /* nothing to do */
1104 /* make sure that the number of ISOCHRONOUS packets is valid */
1105 uxfer->num_iso_packets = iso_packets;
1108 case LIBUSB20_TRANSFER_COMPLETED:
1109 /* update actual length */
1110 uxfer->actual_length = actlen;
1111 for (i = 0; i != iso_packets; i++) {
1112 uxfer->iso_packet_desc[i].actual_length =
1113 libusb20_tr_get_length(pxfer, i);
1115 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1117 case LIBUSB20_TRANSFER_START:
1118 /* setup length(s) */
1120 for (i = 0; i != iso_packets; i++) {
1121 libusb20_tr_setup_isoc(pxfer,
1122 &uxfer->buffer[actlen],
1123 uxfer->iso_packet_desc[i].length, i);
1124 actlen += uxfer->iso_packet_desc[i].length;
1130 libusb20_tr_set_total_frames(pxfer, iso_packets);
1131 libusb20_tr_submit(pxfer);
1133 /* fork another USB transfer, if any */
1134 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1137 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1142 /* This function must be called locked */
1145 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1147 struct libusb_super_transfer *sxfer;
1148 struct libusb_transfer *uxfer;
1154 status = libusb20_tr_get_status(pxfer);
1155 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1156 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1157 actlen = libusb20_tr_get_actual_length(pxfer);
1160 return; /* cancelled - nothing to do */
1162 uxfer = (struct libusb_transfer *)(
1163 ((uint8_t *)sxfer) + sizeof(*sxfer));
1165 flags = uxfer->flags;
1168 case LIBUSB20_TRANSFER_COMPLETED:
1170 uxfer->actual_length += actlen;
1172 /* check for short packet */
1173 if (sxfer->last_len != actlen) {
1174 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1175 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1177 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1181 /* check for end of data */
1182 if (sxfer->rem_len == 0) {
1183 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1188 case LIBUSB20_TRANSFER_START:
1189 if (max_bulk > sxfer->rem_len) {
1190 max_bulk = sxfer->rem_len;
1192 /* setup new BULK or INTERRUPT transaction */
1193 libusb20_tr_setup_bulk(pxfer,
1194 sxfer->curr_data, max_bulk, uxfer->timeout);
1196 /* update counters */
1197 sxfer->last_len = max_bulk;
1198 sxfer->curr_data += max_bulk;
1199 sxfer->rem_len -= max_bulk;
1201 libusb20_tr_submit(pxfer);
1203 /* check if we can fork another USB transfer */
1204 if (sxfer->rem_len == 0)
1205 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1209 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1214 /* This function must be called locked */
1217 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1219 struct libusb_super_transfer *sxfer;
1220 struct libusb_transfer *uxfer;
1226 status = libusb20_tr_get_status(pxfer);
1227 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1228 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1229 actlen = libusb20_tr_get_actual_length(pxfer);
1232 return; /* cancelled - nothing to do */
1234 uxfer = (struct libusb_transfer *)(
1235 ((uint8_t *)sxfer) + sizeof(*sxfer));
1237 flags = uxfer->flags;
1240 case LIBUSB20_TRANSFER_COMPLETED:
1242 uxfer->actual_length += actlen;
1244 /* subtract length of SETUP packet, if any */
1245 actlen -= libusb20_tr_get_length(pxfer, 0);
1247 /* check for short packet */
1248 if (sxfer->last_len != actlen) {
1249 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1250 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1252 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1256 /* check for end of data */
1257 if (sxfer->rem_len == 0) {
1258 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1263 case LIBUSB20_TRANSFER_START:
1264 if (max_bulk > sxfer->rem_len) {
1265 max_bulk = sxfer->rem_len;
1267 /* setup new CONTROL transaction */
1268 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1269 /* next fragment - don't send SETUP packet */
1270 libusb20_tr_set_length(pxfer, 0, 0);
1272 /* first fragment - send SETUP packet */
1273 libusb20_tr_set_length(pxfer, 8, 0);
1274 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1277 if (max_bulk != 0) {
1278 libusb20_tr_set_length(pxfer, max_bulk, 1);
1279 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1280 libusb20_tr_set_total_frames(pxfer, 2);
1282 libusb20_tr_set_total_frames(pxfer, 1);
1285 /* update counters */
1286 sxfer->last_len = max_bulk;
1287 sxfer->curr_data += max_bulk;
1288 sxfer->rem_len -= max_bulk;
1290 libusb20_tr_submit(pxfer);
1292 /* check if we can fork another USB transfer */
1293 if (sxfer->rem_len == 0)
1294 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1298 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1303 /* The following function must be called locked */
1306 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1308 struct libusb20_transfer *pxfer0;
1309 struct libusb20_transfer *pxfer1;
1310 struct libusb_super_transfer *sxfer;
1311 struct libusb_transfer *uxfer;
1312 struct libusb_device *dev;
1319 dev = libusb_get_device(pdev);
1321 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1322 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1324 if (pxfer0 == NULL || pxfer1 == NULL)
1325 return; /* shouldn't happen */
1328 if (libusb20_tr_pending(pxfer0))
1330 if (libusb20_tr_pending(pxfer1))
1335 /* wait till one of the transfers complete */
1338 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1340 return; /* cancelling */
1342 return; /* cannot queue another one */
1343 /* swap transfers */
1347 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1349 return; /* cancelling */
1351 return; /* cannot queue another one */
1352 /* swap transfers */
1359 /* find next transfer on same endpoint */
1360 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1362 uxfer = (struct libusb_transfer *)(
1363 ((uint8_t *)sxfer) + sizeof(*sxfer));
1365 if (uxfer->endpoint == endpoint) {
1366 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1367 sxfer->entry.tqe_prev = NULL;
1371 return; /* success */
1375 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1376 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1378 /* reset super transfer state */
1379 sxfer->rem_len = uxfer->length;
1380 sxfer->curr_data = uxfer->buffer;
1381 uxfer->actual_length = 0;
1383 switch (uxfer->type) {
1384 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1385 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1387 case LIBUSB_TRANSFER_TYPE_BULK:
1388 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1389 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1391 case LIBUSB_TRANSFER_TYPE_CONTROL:
1392 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1393 if (sxfer->rem_len < 8)
1396 /* remove SETUP packet from data */
1397 sxfer->rem_len -= 8;
1398 sxfer->curr_data += 8;
1404 buffsize = libusb10_get_buffsize(pdev, uxfer);
1405 maxframe = libusb10_get_maxframe(pdev, uxfer);
1407 /* make sure the transfer is opened */
1408 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1409 endpoint, sxfer->stream_id);
1410 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1413 libusb20_tr_start(pxfer0);
1417 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1419 /* make sure our event loop spins the done handler */
1421 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1424 /* The following function must be called unlocked */
1427 libusb_submit_transfer(struct libusb_transfer *uxfer)
1429 struct libusb20_transfer *pxfer0;
1430 struct libusb20_transfer *pxfer1;
1431 struct libusb_super_transfer *sxfer;
1432 struct libusb_device *dev;
1437 return (LIBUSB_ERROR_INVALID_PARAM);
1439 if (uxfer->dev_handle == NULL)
1440 return (LIBUSB_ERROR_INVALID_PARAM);
1442 endpoint = uxfer->endpoint;
1444 dev = libusb_get_device(uxfer->dev_handle);
1446 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1448 sxfer = (struct libusb_super_transfer *)(
1449 (uint8_t *)uxfer - sizeof(*sxfer));
1453 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1454 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1456 if (pxfer0 == NULL || pxfer1 == NULL) {
1457 err = LIBUSB_ERROR_OTHER;
1458 } else if ((sxfer->entry.tqe_prev != NULL) ||
1459 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1460 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1461 err = LIBUSB_ERROR_BUSY;
1464 /* set pending state */
1465 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1467 /* insert transfer into transfer head list */
1468 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1470 /* start work transfers */
1471 libusb10_submit_transfer_sub(
1472 uxfer->dev_handle, endpoint);
1474 err = 0; /* success */
1477 CTX_UNLOCK(dev->ctx);
1479 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1484 /* Asynchronous transfer cancel */
1487 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1489 struct libusb20_transfer *pxfer0;
1490 struct libusb20_transfer *pxfer1;
1491 struct libusb_super_transfer *sxfer;
1492 struct libusb_device *dev;
1497 return (LIBUSB_ERROR_INVALID_PARAM);
1499 /* check if not initialised */
1500 if (uxfer->dev_handle == NULL)
1501 return (LIBUSB_ERROR_NOT_FOUND);
1503 endpoint = uxfer->endpoint;
1505 dev = libusb_get_device(uxfer->dev_handle);
1507 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1509 sxfer = (struct libusb_super_transfer *)(
1510 (uint8_t *)uxfer - sizeof(*sxfer));
1516 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1517 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1519 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1520 /* only update the transfer status */
1521 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1522 retval = LIBUSB_ERROR_NOT_FOUND;
1523 } else if (sxfer->entry.tqe_prev != NULL) {
1524 /* we are lucky - transfer is on a queue */
1525 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1526 sxfer->entry.tqe_prev = NULL;
1527 libusb10_complete_transfer(NULL,
1528 sxfer, LIBUSB_TRANSFER_CANCELLED);
1529 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1531 retval = LIBUSB_ERROR_NOT_FOUND;
1532 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1533 libusb10_complete_transfer(pxfer0,
1534 sxfer, LIBUSB_TRANSFER_CANCELLED);
1535 libusb20_tr_stop(pxfer0);
1536 /* make sure the queue doesn't stall */
1537 libusb10_submit_transfer_sub(
1538 uxfer->dev_handle, endpoint);
1539 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1540 libusb10_complete_transfer(pxfer1,
1541 sxfer, LIBUSB_TRANSFER_CANCELLED);
1542 libusb20_tr_stop(pxfer1);
1543 /* make sure the queue doesn't stall */
1544 libusb10_submit_transfer_sub(
1545 uxfer->dev_handle, endpoint);
1548 retval = LIBUSB_ERROR_NOT_FOUND;
1551 CTX_UNLOCK(dev->ctx);
1553 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1559 libusb10_cancel_all_transfer(libusb_device *dev)
1561 struct libusb20_device *pdev = dev->os_priv;
1564 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1565 struct libusb20_transfer *xfer;
1567 xfer = libusb20_tr_get_pointer(pdev, x);
1570 libusb20_tr_close(xfer);
1575 libusb_cpu_to_le16(uint16_t x)
1577 return (htole16(x));
1581 libusb_le16_to_cpu(uint16_t x)
1583 return (le16toh(x));
1587 libusb_strerror(int code)
1590 case LIBUSB_SUCCESS:
1592 case LIBUSB_ERROR_IO:
1593 return ("I/O error");
1594 case LIBUSB_ERROR_INVALID_PARAM:
1595 return ("Invalid parameter");
1596 case LIBUSB_ERROR_ACCESS:
1597 return ("Permissions error");
1598 case LIBUSB_ERROR_NO_DEVICE:
1599 return ("No device");
1600 case LIBUSB_ERROR_NOT_FOUND:
1601 return ("Not found");
1602 case LIBUSB_ERROR_BUSY:
1603 return ("Device busy");
1604 case LIBUSB_ERROR_TIMEOUT:
1606 case LIBUSB_ERROR_OVERFLOW:
1607 return ("Overflow");
1608 case LIBUSB_ERROR_PIPE:
1609 return ("Pipe error");
1610 case LIBUSB_ERROR_INTERRUPTED:
1611 return ("Interrupted");
1612 case LIBUSB_ERROR_NO_MEM:
1613 return ("Out of memory");
1614 case LIBUSB_ERROR_NOT_SUPPORTED:
1615 return ("Not supported");
1616 case LIBUSB_ERROR_OTHER:
1617 return ("Other error");
1619 return ("Unknown error");
1624 libusb_error_name(int code)
1627 case LIBUSB_SUCCESS:
1628 return ("LIBUSB_SUCCESS");
1629 case LIBUSB_ERROR_IO:
1630 return ("LIBUSB_ERROR_IO");
1631 case LIBUSB_ERROR_INVALID_PARAM:
1632 return ("LIBUSB_ERROR_INVALID_PARAM");
1633 case LIBUSB_ERROR_ACCESS:
1634 return ("LIBUSB_ERROR_ACCESS");
1635 case LIBUSB_ERROR_NO_DEVICE:
1636 return ("LIBUSB_ERROR_NO_DEVICE");
1637 case LIBUSB_ERROR_NOT_FOUND:
1638 return ("LIBUSB_ERROR_NOT_FOUND");
1639 case LIBUSB_ERROR_BUSY:
1640 return ("LIBUSB_ERROR_BUSY");
1641 case LIBUSB_ERROR_TIMEOUT:
1642 return ("LIBUSB_ERROR_TIMEOUT");
1643 case LIBUSB_ERROR_OVERFLOW:
1644 return ("LIBUSB_ERROR_OVERFLOW");
1645 case LIBUSB_ERROR_PIPE:
1646 return ("LIBUSB_ERROR_PIPE");
1647 case LIBUSB_ERROR_INTERRUPTED:
1648 return ("LIBUSB_ERROR_INTERRUPTED");
1649 case LIBUSB_ERROR_NO_MEM:
1650 return ("LIBUSB_ERROR_NO_MEM");
1651 case LIBUSB_ERROR_NOT_SUPPORTED:
1652 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1653 case LIBUSB_ERROR_OTHER:
1654 return ("LIBUSB_ERROR_OTHER");
1656 return ("LIBUSB_ERROR_UNKNOWN");