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 libusb10_wakeup_event_loop(libusb_context *ctx)
123 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
124 if (err < (int)sizeof(dummy)) {
125 /* ignore error, if any */
126 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
131 libusb_init(libusb_context **context)
133 struct libusb_context *ctx;
134 pthread_condattr_t attr;
138 ctx = malloc(sizeof(*ctx));
140 return (LIBUSB_ERROR_INVALID_PARAM);
142 memset(ctx, 0, sizeof(*ctx));
144 debug = getenv("LIBUSB_DEBUG");
146 ctx->debug = atoi(debug);
148 ctx->debug_fixed = 1;
150 TAILQ_INIT(&ctx->pollfds);
151 TAILQ_INIT(&ctx->tr_done);
152 TAILQ_INIT(&ctx->hotplug_cbh);
153 TAILQ_INIT(&ctx->hotplug_devs);
155 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
157 return (LIBUSB_ERROR_NO_MEM);
159 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
160 pthread_mutex_destroy(&ctx->ctx_lock);
162 return (LIBUSB_ERROR_NO_MEM);
164 if (pthread_condattr_init(&attr) != 0) {
165 pthread_mutex_destroy(&ctx->ctx_lock);
166 pthread_mutex_destroy(&ctx->hotplug_lock);
168 return (LIBUSB_ERROR_NO_MEM);
170 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
171 pthread_mutex_destroy(&ctx->ctx_lock);
172 pthread_mutex_destroy(&ctx->hotplug_lock);
173 pthread_condattr_destroy(&attr);
175 return (LIBUSB_ERROR_OTHER);
177 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
178 pthread_mutex_destroy(&ctx->ctx_lock);
179 pthread_mutex_destroy(&ctx->hotplug_lock);
180 pthread_condattr_destroy(&attr);
182 return (LIBUSB_ERROR_NO_MEM);
184 pthread_condattr_destroy(&attr);
186 ctx->ctx_handler = NO_THREAD;
187 ctx->hotplug_handler = NO_THREAD;
189 ret = pipe(ctx->ctrl_pipe);
191 pthread_mutex_destroy(&ctx->ctx_lock);
192 pthread_mutex_destroy(&ctx->hotplug_lock);
193 pthread_cond_destroy(&ctx->ctx_cond);
195 return (LIBUSB_ERROR_OTHER);
197 /* set non-blocking mode on the control pipe to avoid deadlock */
198 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
199 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
201 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
203 pthread_mutex_lock(&default_context_lock);
204 if (usbi_default_context == NULL) {
205 usbi_default_context = ctx;
207 pthread_mutex_unlock(&default_context_lock);
212 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
218 libusb_exit(libusb_context *ctx)
220 ctx = GET_CONTEXT(ctx);
225 /* stop hotplug thread, if any */
227 if (ctx->hotplug_handler != NO_THREAD) {
232 td = ctx->hotplug_handler;
233 ctx->hotplug_handler = NO_THREAD;
236 pthread_join(td, &ptr);
239 /* XXX cleanup devices */
241 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
242 close(ctx->ctrl_pipe[0]);
243 close(ctx->ctrl_pipe[1]);
244 pthread_mutex_destroy(&ctx->ctx_lock);
245 pthread_mutex_destroy(&ctx->hotplug_lock);
246 pthread_cond_destroy(&ctx->ctx_cond);
248 pthread_mutex_lock(&default_context_lock);
249 if (ctx == usbi_default_context) {
250 usbi_default_context = NULL;
252 pthread_mutex_unlock(&default_context_lock);
257 /* Device handling and initialisation. */
260 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
262 struct libusb20_backend *usb_backend;
263 struct libusb20_device *pdev;
264 struct libusb_device *dev;
267 ctx = GET_CONTEXT(ctx);
270 return (LIBUSB_ERROR_INVALID_PARAM);
273 return (LIBUSB_ERROR_INVALID_PARAM);
275 usb_backend = libusb20_be_alloc_default();
276 if (usb_backend == NULL)
277 return (LIBUSB_ERROR_NO_MEM);
279 /* figure out how many USB devices are present */
282 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
285 /* allocate device pointer list */
286 *list = malloc((i + 1) * sizeof(void *));
288 libusb20_be_free(usb_backend);
289 return (LIBUSB_ERROR_NO_MEM);
291 /* create libusb v1.0 compliant devices */
293 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
295 dev = malloc(sizeof(*dev));
298 libusb_unref_device((*list)[i - 1]);
303 libusb20_be_free(usb_backend);
304 return (LIBUSB_ERROR_NO_MEM);
306 /* get device into libUSB v1.0 list */
307 libusb20_be_dequeue_device(usb_backend, pdev);
309 memset(dev, 0, sizeof(*dev));
311 /* init transfer queues */
312 TAILQ_INIT(&dev->tr_head);
314 /* set context we belong to */
317 /* link together the two structures */
319 pdev->privLuData = dev;
321 (*list)[i] = libusb_ref_device(dev);
326 libusb20_be_free(usb_backend);
331 libusb_free_device_list(libusb_device **list, int unref_devices)
336 return; /* be NULL safe */
339 for (i = 0; list[i] != NULL; i++)
340 libusb_unref_device(list[i]);
346 libusb_get_bus_number(libusb_device *dev)
349 return (0); /* should not happen */
350 return (libusb20_dev_get_bus_number(dev->os_priv));
354 libusb_get_port_number(libusb_device *dev)
357 return (0); /* should not happen */
358 return (libusb20_dev_get_parent_port(dev->os_priv));
362 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
364 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
368 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
371 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
375 libusb_get_device_address(libusb_device *dev)
378 return (0); /* should not happen */
379 return (libusb20_dev_get_address(dev->os_priv));
383 libusb_get_device_speed(libusb_device *dev)
386 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
388 switch (libusb20_dev_get_speed(dev->os_priv)) {
389 case LIBUSB20_SPEED_LOW:
390 return (LIBUSB_SPEED_LOW);
391 case LIBUSB20_SPEED_FULL:
392 return (LIBUSB_SPEED_FULL);
393 case LIBUSB20_SPEED_HIGH:
394 return (LIBUSB_SPEED_HIGH);
395 case LIBUSB20_SPEED_SUPER:
396 return (LIBUSB_SPEED_SUPER);
400 return (LIBUSB_SPEED_UNKNOWN);
404 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
406 struct libusb_config_descriptor *pdconf;
407 struct libusb_interface *pinf;
408 struct libusb_interface_descriptor *pdinf;
409 struct libusb_endpoint_descriptor *pdend;
416 return (LIBUSB_ERROR_NO_DEVICE);
418 ret = libusb_get_active_config_descriptor(dev, &pdconf);
422 ret = LIBUSB_ERROR_NOT_FOUND;
423 for (i = 0; i < pdconf->bNumInterfaces; i++) {
424 pinf = &pdconf->interface[i];
425 for (j = 0; j < pinf->num_altsetting; j++) {
426 pdinf = &pinf->altsetting[j];
427 for (k = 0; k < pdinf->bNumEndpoints; k++) {
428 pdend = &pdinf->endpoint[k];
429 if (pdend->bEndpointAddress == endpoint) {
430 ret = pdend->wMaxPacketSize;
438 libusb_free_config_descriptor(pdconf);
443 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
448 ret = libusb_get_max_packet_size(dev, endpoint);
450 switch (libusb20_dev_get_speed(dev->os_priv)) {
451 case LIBUSB20_SPEED_LOW:
452 case LIBUSB20_SPEED_FULL:
456 multiplier = (1 + ((ret >> 11) & 3));
459 ret = (ret & 0x7FF) * multiplier;
467 libusb_ref_device(libusb_device *dev)
470 return (NULL); /* be NULL safe */
474 CTX_UNLOCK(dev->ctx);
480 libusb_unref_device(libusb_device *dev)
483 return; /* be NULL safe */
487 CTX_UNLOCK(dev->ctx);
489 if (dev->refcnt == 0) {
490 libusb20_dev_free(dev->os_priv);
496 libusb_open(libusb_device *dev, libusb_device_handle **devh)
498 libusb_context *ctx = dev->ctx;
499 struct libusb20_device *pdev = dev->os_priv;
503 return (LIBUSB_ERROR_INVALID_PARAM);
505 /* set default device handle value */
508 dev = libusb_ref_device(dev);
510 return (LIBUSB_ERROR_INVALID_PARAM);
512 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
514 libusb_unref_device(dev);
515 return (LIBUSB_ERROR_NO_MEM);
517 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
518 POLLOUT | POLLRDNORM | POLLWRNORM);
520 /* make sure our event loop detects the new device */
521 libusb10_wakeup_event_loop(ctx);
528 libusb_device_handle *
529 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
532 struct libusb_device **devs;
533 struct libusb20_device *pdev;
534 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
538 ctx = GET_CONTEXT(ctx);
540 return (NULL); /* be NULL safe */
542 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter");
544 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
548 for (j = 0; j < i; j++) {
549 struct libusb20_device *tdev;
551 tdev = devs[j]->os_priv;
552 pdesc = libusb20_dev_get_device_desc(tdev);
554 * NOTE: The USB library will automatically swap the
555 * fields in the device descriptor to be of host
558 if (pdesc->idVendor == vendor_id &&
559 pdesc->idProduct == product_id) {
560 libusb_open(devs[j], &pdev);
565 libusb_free_device_list(devs, 1);
566 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave");
571 libusb_close(struct libusb20_device *pdev)
574 struct libusb_device *dev;
577 return; /* be NULL safe */
579 dev = libusb_get_device(pdev);
582 libusb10_remove_pollfd(ctx, &dev->dev_poll);
584 libusb20_dev_close(pdev);
586 /* unref will free the "pdev" when the refcount reaches zero */
587 libusb_unref_device(dev);
589 /* make sure our event loop detects the closed device */
590 libusb10_wakeup_event_loop(ctx);
594 libusb_get_device(struct libusb20_device *pdev)
598 return ((libusb_device *)pdev->privLuData);
602 libusb_get_configuration(struct libusb20_device *pdev, int *config)
604 struct libusb20_config *pconf;
606 if (pdev == NULL || config == NULL)
607 return (LIBUSB_ERROR_INVALID_PARAM);
609 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
611 return (LIBUSB_ERROR_NO_MEM);
613 *config = pconf->desc.bConfigurationValue;
621 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
623 struct libusb20_config *pconf;
624 struct libusb_device *dev;
628 dev = libusb_get_device(pdev);
630 return (LIBUSB_ERROR_INVALID_PARAM);
632 if (configuration < 1) {
636 for (i = 0; i != 255; i++) {
639 pconf = libusb20_dev_alloc_config(pdev, i);
641 return (LIBUSB_ERROR_INVALID_PARAM);
642 found = (pconf->desc.bConfigurationValue
649 return (LIBUSB_ERROR_INVALID_PARAM);
654 libusb10_cancel_all_transfer(dev);
656 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
658 err = libusb20_dev_set_config_index(pdev, i);
660 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
661 POLLOUT | POLLRDNORM | POLLWRNORM);
663 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
667 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
672 dev = libusb_get_device(pdev);
674 return (LIBUSB_ERROR_INVALID_PARAM);
676 if (interface_number < 0 || interface_number > 31)
677 return (LIBUSB_ERROR_INVALID_PARAM);
679 if (pdev->auto_detach != 0) {
680 err = libusb_detach_kernel_driver(pdev, interface_number);
686 dev->claimed_interfaces |= (1 << interface_number);
687 CTX_UNLOCK(dev->ctx);
693 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
698 dev = libusb_get_device(pdev);
700 return (LIBUSB_ERROR_INVALID_PARAM);
702 if (interface_number < 0 || interface_number > 31)
703 return (LIBUSB_ERROR_INVALID_PARAM);
705 if (pdev->auto_detach != 0) {
706 err = libusb_attach_kernel_driver(pdev, interface_number);
712 if (!(dev->claimed_interfaces & (1 << interface_number)))
713 err = LIBUSB_ERROR_NOT_FOUND;
715 dev->claimed_interfaces &= ~(1 << interface_number);
716 CTX_UNLOCK(dev->ctx);
722 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
723 int interface_number, int alternate_setting)
728 dev = libusb_get_device(pdev);
730 return (LIBUSB_ERROR_INVALID_PARAM);
732 if (interface_number < 0 || interface_number > 31)
733 return (LIBUSB_ERROR_INVALID_PARAM);
736 if (!(dev->claimed_interfaces & (1 << interface_number)))
737 err = LIBUSB_ERROR_NOT_FOUND;
738 CTX_UNLOCK(dev->ctx);
743 libusb10_cancel_all_transfer(dev);
745 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
747 err = libusb20_dev_set_alt_index(pdev,
748 interface_number, alternate_setting);
750 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
751 pdev, libusb20_dev_get_fd(pdev),
752 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
754 return (err ? LIBUSB_ERROR_OTHER : 0);
757 static struct libusb20_transfer *
758 libusb10_get_transfer(struct libusb20_device *pdev,
759 uint8_t endpoint, uint8_t xfer_index)
761 xfer_index &= 1; /* double buffering */
763 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
765 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
766 /* this is an IN endpoint */
769 return (libusb20_tr_get_pointer(pdev, xfer_index));
773 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
775 struct libusb20_transfer *xfer;
776 struct libusb_device *dev;
779 xfer = libusb10_get_transfer(pdev, endpoint, 0);
781 return (LIBUSB_ERROR_INVALID_PARAM);
783 dev = libusb_get_device(pdev);
785 return (LIBUSB_ERROR_INVALID_PARAM);
788 err = libusb20_tr_open(xfer, 0, 1, endpoint);
789 CTX_UNLOCK(dev->ctx);
791 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
792 return (LIBUSB_ERROR_OTHER);
794 libusb20_tr_clear_stall_sync(xfer);
796 /* check if we opened the transfer */
799 libusb20_tr_close(xfer);
800 CTX_UNLOCK(dev->ctx);
802 return (0); /* success */
806 libusb_reset_device(struct libusb20_device *pdev)
811 dev = libusb_get_device(pdev);
813 return (LIBUSB_ERROR_INVALID_PARAM);
815 libusb10_cancel_all_transfer(dev);
817 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
819 err = libusb20_dev_reset(pdev);
821 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
822 pdev, libusb20_dev_get_fd(pdev),
823 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
825 return (err ? LIBUSB_ERROR_OTHER : 0);
829 libusb_check_connected(struct libusb20_device *pdev)
834 dev = libusb_get_device(pdev);
836 return (LIBUSB_ERROR_INVALID_PARAM);
838 err = libusb20_dev_check_connected(pdev);
840 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
844 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
847 return (LIBUSB_ERROR_INVALID_PARAM);
849 if (libusb20_dev_kernel_driver_active(pdev, interface))
850 return (0); /* no kernel driver is active */
852 return (1); /* kernel driver is active */
856 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
857 char *name, int namelen)
859 return (libusb_get_driver(pdev, interface, name, namelen));
863 libusb_get_driver(struct libusb20_device *pdev, int interface,
864 char *name, int namelen)
870 return (LIBUSB_ERROR_INVALID_PARAM);
872 return (LIBUSB_ERROR_INVALID_PARAM);
876 err = libusb20_dev_get_iface_desc(
877 pdev, interface, name, namelen);
880 return (LIBUSB_ERROR_OTHER);
882 /* we only want the driver name */
883 ptr = strstr(name, ":");
891 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
893 return (libusb_detach_kernel_driver(pdev, interface));
897 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
902 return (LIBUSB_ERROR_INVALID_PARAM);
904 err = libusb20_dev_detach_kernel_driver(
907 return (err ? LIBUSB_ERROR_OTHER : 0);
911 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
914 return (LIBUSB_ERROR_INVALID_PARAM);
915 /* stub - currently not supported by libusb20 */
920 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
922 dev->auto_detach = (enable ? 1 : 0);
926 /* Asynchronous device I/O */
928 struct libusb_transfer *
929 libusb_alloc_transfer(int iso_packets)
931 struct libusb_transfer *uxfer;
932 struct libusb_super_transfer *sxfer;
935 len = sizeof(struct libusb_transfer) +
936 sizeof(struct libusb_super_transfer) +
937 (iso_packets * sizeof(libusb_iso_packet_descriptor));
943 memset(sxfer, 0, len);
945 uxfer = (struct libusb_transfer *)(
946 ((uint8_t *)sxfer) + sizeof(*sxfer));
948 /* set default value */
949 uxfer->num_iso_packets = iso_packets;
955 libusb_free_transfer(struct libusb_transfer *uxfer)
957 struct libusb_super_transfer *sxfer;
960 return; /* be NULL safe */
962 /* check if we should free the transfer buffer */
963 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
966 sxfer = (struct libusb_super_transfer *)(
967 (uint8_t *)uxfer - sizeof(*sxfer));
973 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
977 switch (xfer->type) {
978 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
979 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
981 case LIBUSB_TRANSFER_TYPE_CONTROL:
992 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
997 usb_speed = libusb20_dev_get_speed(pdev);
999 switch (xfer->type) {
1000 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1001 ret = 0; /* kernel will auto-select */
1003 case LIBUSB_TRANSFER_TYPE_CONTROL:
1007 switch (usb_speed) {
1008 case LIBUSB20_SPEED_LOW:
1011 case LIBUSB20_SPEED_FULL:
1014 case LIBUSB20_SPEED_SUPER:
1027 libusb10_convert_error(uint8_t status)
1032 case LIBUSB20_TRANSFER_START:
1033 case LIBUSB20_TRANSFER_COMPLETED:
1034 return (LIBUSB_TRANSFER_COMPLETED);
1035 case LIBUSB20_TRANSFER_OVERFLOW:
1036 return (LIBUSB_TRANSFER_OVERFLOW);
1037 case LIBUSB20_TRANSFER_NO_DEVICE:
1038 return (LIBUSB_TRANSFER_NO_DEVICE);
1039 case LIBUSB20_TRANSFER_STALL:
1040 return (LIBUSB_TRANSFER_STALL);
1041 case LIBUSB20_TRANSFER_CANCELLED:
1042 return (LIBUSB_TRANSFER_CANCELLED);
1043 case LIBUSB20_TRANSFER_TIMED_OUT:
1044 return (LIBUSB_TRANSFER_TIMED_OUT);
1046 return (LIBUSB_TRANSFER_ERROR);
1050 /* This function must be called locked */
1053 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1054 struct libusb_super_transfer *sxfer, int status)
1056 struct libusb_transfer *uxfer;
1057 struct libusb_device *dev;
1059 uxfer = (struct libusb_transfer *)(
1060 ((uint8_t *)sxfer) + sizeof(*sxfer));
1063 libusb20_tr_set_priv_sc1(pxfer, NULL);
1065 /* set transfer status */
1066 uxfer->status = status;
1068 /* update super transfer state */
1069 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1071 dev = libusb_get_device(uxfer->dev_handle);
1073 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1076 /* This function must be called locked */
1079 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1081 struct libusb_super_transfer *sxfer;
1082 struct libusb_transfer *uxfer;
1084 uint16_t iso_packets;
1088 status = libusb20_tr_get_status(pxfer);
1089 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1090 actlen = libusb20_tr_get_actual_length(pxfer);
1091 iso_packets = libusb20_tr_get_max_frames(pxfer);
1094 return; /* cancelled - nothing to do */
1096 uxfer = (struct libusb_transfer *)(
1097 ((uint8_t *)sxfer) + sizeof(*sxfer));
1099 if (iso_packets > uxfer->num_iso_packets)
1100 iso_packets = uxfer->num_iso_packets;
1102 if (iso_packets == 0)
1103 return; /* nothing to do */
1105 /* make sure that the number of ISOCHRONOUS packets is valid */
1106 uxfer->num_iso_packets = iso_packets;
1109 case LIBUSB20_TRANSFER_COMPLETED:
1110 /* update actual length */
1111 uxfer->actual_length = actlen;
1112 for (i = 0; i != iso_packets; i++) {
1113 uxfer->iso_packet_desc[i].actual_length =
1114 libusb20_tr_get_length(pxfer, i);
1116 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1118 case LIBUSB20_TRANSFER_START:
1119 /* setup length(s) */
1121 for (i = 0; i != iso_packets; i++) {
1122 libusb20_tr_setup_isoc(pxfer,
1123 &uxfer->buffer[actlen],
1124 uxfer->iso_packet_desc[i].length, i);
1125 actlen += uxfer->iso_packet_desc[i].length;
1131 libusb20_tr_set_total_frames(pxfer, iso_packets);
1132 libusb20_tr_submit(pxfer);
1134 /* fork another USB transfer, if any */
1135 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1138 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1143 /* This function must be called locked */
1146 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1148 struct libusb_super_transfer *sxfer;
1149 struct libusb_transfer *uxfer;
1155 status = libusb20_tr_get_status(pxfer);
1156 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1157 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1158 actlen = libusb20_tr_get_actual_length(pxfer);
1161 return; /* cancelled - nothing to do */
1163 uxfer = (struct libusb_transfer *)(
1164 ((uint8_t *)sxfer) + sizeof(*sxfer));
1166 flags = uxfer->flags;
1169 case LIBUSB20_TRANSFER_COMPLETED:
1171 uxfer->actual_length += actlen;
1173 /* check for short packet */
1174 if (sxfer->last_len != actlen) {
1175 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1176 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1178 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1182 /* check for end of data */
1183 if (sxfer->rem_len == 0) {
1184 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1189 case LIBUSB20_TRANSFER_START:
1190 if (max_bulk > sxfer->rem_len) {
1191 max_bulk = sxfer->rem_len;
1193 /* setup new BULK or INTERRUPT transaction */
1194 libusb20_tr_setup_bulk(pxfer,
1195 sxfer->curr_data, max_bulk, uxfer->timeout);
1197 /* update counters */
1198 sxfer->last_len = max_bulk;
1199 sxfer->curr_data += max_bulk;
1200 sxfer->rem_len -= max_bulk;
1202 libusb20_tr_submit(pxfer);
1204 /* check if we can fork another USB transfer */
1205 if (sxfer->rem_len == 0)
1206 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1210 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1215 /* This function must be called locked */
1218 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1220 struct libusb_super_transfer *sxfer;
1221 struct libusb_transfer *uxfer;
1227 status = libusb20_tr_get_status(pxfer);
1228 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1229 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1230 actlen = libusb20_tr_get_actual_length(pxfer);
1233 return; /* cancelled - nothing to do */
1235 uxfer = (struct libusb_transfer *)(
1236 ((uint8_t *)sxfer) + sizeof(*sxfer));
1238 flags = uxfer->flags;
1241 case LIBUSB20_TRANSFER_COMPLETED:
1243 uxfer->actual_length += actlen;
1245 /* subtract length of SETUP packet, if any */
1246 actlen -= libusb20_tr_get_length(pxfer, 0);
1248 /* check for short packet */
1249 if (sxfer->last_len != actlen) {
1250 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1251 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1253 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1257 /* check for end of data */
1258 if (sxfer->rem_len == 0) {
1259 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1264 case LIBUSB20_TRANSFER_START:
1265 if (max_bulk > sxfer->rem_len) {
1266 max_bulk = sxfer->rem_len;
1268 /* setup new CONTROL transaction */
1269 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1270 /* next fragment - don't send SETUP packet */
1271 libusb20_tr_set_length(pxfer, 0, 0);
1273 /* first fragment - send SETUP packet */
1274 libusb20_tr_set_length(pxfer, 8, 0);
1275 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1278 if (max_bulk != 0) {
1279 libusb20_tr_set_length(pxfer, max_bulk, 1);
1280 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1281 libusb20_tr_set_total_frames(pxfer, 2);
1283 libusb20_tr_set_total_frames(pxfer, 1);
1286 /* update counters */
1287 sxfer->last_len = max_bulk;
1288 sxfer->curr_data += max_bulk;
1289 sxfer->rem_len -= max_bulk;
1291 libusb20_tr_submit(pxfer);
1293 /* check if we can fork another USB transfer */
1294 if (sxfer->rem_len == 0)
1295 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1299 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1304 /* The following function must be called locked */
1307 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1309 struct libusb20_transfer *pxfer0;
1310 struct libusb20_transfer *pxfer1;
1311 struct libusb_super_transfer *sxfer;
1312 struct libusb_transfer *uxfer;
1313 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);
1418 /* make sure our event loop spins the done handler */
1419 libusb10_wakeup_event_loop(dev->ctx);
1422 /* The following function must be called unlocked */
1425 libusb_submit_transfer(struct libusb_transfer *uxfer)
1427 struct libusb20_transfer *pxfer0;
1428 struct libusb20_transfer *pxfer1;
1429 struct libusb_super_transfer *sxfer;
1430 struct libusb_device *dev;
1435 return (LIBUSB_ERROR_INVALID_PARAM);
1437 if (uxfer->dev_handle == NULL)
1438 return (LIBUSB_ERROR_INVALID_PARAM);
1440 endpoint = uxfer->endpoint;
1442 dev = libusb_get_device(uxfer->dev_handle);
1444 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1446 sxfer = (struct libusb_super_transfer *)(
1447 (uint8_t *)uxfer - sizeof(*sxfer));
1451 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1452 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1454 if (pxfer0 == NULL || pxfer1 == NULL) {
1455 err = LIBUSB_ERROR_OTHER;
1456 } else if ((sxfer->entry.tqe_prev != NULL) ||
1457 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1458 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1459 err = LIBUSB_ERROR_BUSY;
1460 } else if (dev->device_is_gone != 0) {
1461 err = LIBUSB_ERROR_NO_DEVICE;
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;
1493 struct libusb_device_handle *devh;
1498 return (LIBUSB_ERROR_INVALID_PARAM);
1500 /* check if not initialised */
1501 if ((devh = uxfer->dev_handle) == NULL)
1502 return (LIBUSB_ERROR_NOT_FOUND);
1504 endpoint = uxfer->endpoint;
1506 dev = libusb_get_device(devh);
1508 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1510 sxfer = (struct libusb_super_transfer *)(
1511 (uint8_t *)uxfer - sizeof(*sxfer));
1517 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1518 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1520 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1521 /* only update the transfer status */
1522 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1523 retval = LIBUSB_ERROR_NOT_FOUND;
1524 } else if (sxfer->entry.tqe_prev != NULL) {
1525 /* we are lucky - transfer is on a queue */
1526 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1527 sxfer->entry.tqe_prev = NULL;
1528 libusb10_complete_transfer(NULL,
1529 sxfer, LIBUSB_TRANSFER_CANCELLED);
1530 /* make sure our event loop spins the done handler */
1531 libusb10_wakeup_event_loop(dev->ctx);
1532 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1534 retval = LIBUSB_ERROR_NOT_FOUND;
1535 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1536 libusb10_complete_transfer(pxfer0,
1537 sxfer, LIBUSB_TRANSFER_CANCELLED);
1538 if (dev->device_is_gone != 0) {
1539 /* clear transfer pointer */
1540 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1541 /* make sure our event loop spins the done handler */
1542 libusb10_wakeup_event_loop(dev->ctx);
1544 libusb20_tr_stop(pxfer0);
1545 /* make sure the queue doesn't stall */
1546 libusb10_submit_transfer_sub(devh, endpoint);
1548 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1549 libusb10_complete_transfer(pxfer1,
1550 sxfer, LIBUSB_TRANSFER_CANCELLED);
1551 /* check if handle is still active */
1552 if (dev->device_is_gone != 0) {
1553 /* clear transfer pointer */
1554 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1555 /* make sure our event loop spins the done handler */
1556 libusb10_wakeup_event_loop(dev->ctx);
1558 libusb20_tr_stop(pxfer1);
1559 /* make sure the queue doesn't stall */
1560 libusb10_submit_transfer_sub(devh, endpoint);
1564 retval = LIBUSB_ERROR_NOT_FOUND;
1567 CTX_UNLOCK(dev->ctx);
1569 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1575 libusb10_cancel_all_transfer(libusb_device *dev)
1577 struct libusb20_device *pdev = dev->os_priv;
1580 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1581 struct libusb20_transfer *xfer;
1583 xfer = libusb20_tr_get_pointer(pdev, x);
1586 libusb20_tr_close(xfer);
1591 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1593 struct libusb_super_transfer *sxfer;
1596 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1597 struct libusb20_transfer *xfer;
1599 xfer = libusb20_tr_get_pointer(pdev, x);
1602 if (libusb20_tr_pending(xfer) == 0)
1604 sxfer = libusb20_tr_get_priv_sc1(xfer);
1607 /* complete pending transfer */
1608 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1611 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1612 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1614 /* complete pending transfer */
1615 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1620 libusb_cpu_to_le16(uint16_t x)
1622 return (htole16(x));
1626 libusb_le16_to_cpu(uint16_t x)
1628 return (le16toh(x));
1632 libusb_strerror(int code)
1635 case LIBUSB_SUCCESS:
1637 case LIBUSB_ERROR_IO:
1638 return ("I/O error");
1639 case LIBUSB_ERROR_INVALID_PARAM:
1640 return ("Invalid parameter");
1641 case LIBUSB_ERROR_ACCESS:
1642 return ("Permissions error");
1643 case LIBUSB_ERROR_NO_DEVICE:
1644 return ("No device");
1645 case LIBUSB_ERROR_NOT_FOUND:
1646 return ("Not found");
1647 case LIBUSB_ERROR_BUSY:
1648 return ("Device busy");
1649 case LIBUSB_ERROR_TIMEOUT:
1651 case LIBUSB_ERROR_OVERFLOW:
1652 return ("Overflow");
1653 case LIBUSB_ERROR_PIPE:
1654 return ("Pipe error");
1655 case LIBUSB_ERROR_INTERRUPTED:
1656 return ("Interrupted");
1657 case LIBUSB_ERROR_NO_MEM:
1658 return ("Out of memory");
1659 case LIBUSB_ERROR_NOT_SUPPORTED:
1660 return ("Not supported");
1661 case LIBUSB_ERROR_OTHER:
1662 return ("Other error");
1664 return ("Unknown error");
1669 libusb_error_name(int code)
1672 case LIBUSB_SUCCESS:
1673 return ("LIBUSB_SUCCESS");
1674 case LIBUSB_ERROR_IO:
1675 return ("LIBUSB_ERROR_IO");
1676 case LIBUSB_ERROR_INVALID_PARAM:
1677 return ("LIBUSB_ERROR_INVALID_PARAM");
1678 case LIBUSB_ERROR_ACCESS:
1679 return ("LIBUSB_ERROR_ACCESS");
1680 case LIBUSB_ERROR_NO_DEVICE:
1681 return ("LIBUSB_ERROR_NO_DEVICE");
1682 case LIBUSB_ERROR_NOT_FOUND:
1683 return ("LIBUSB_ERROR_NOT_FOUND");
1684 case LIBUSB_ERROR_BUSY:
1685 return ("LIBUSB_ERROR_BUSY");
1686 case LIBUSB_ERROR_TIMEOUT:
1687 return ("LIBUSB_ERROR_TIMEOUT");
1688 case LIBUSB_ERROR_OVERFLOW:
1689 return ("LIBUSB_ERROR_OVERFLOW");
1690 case LIBUSB_ERROR_PIPE:
1691 return ("LIBUSB_ERROR_PIPE");
1692 case LIBUSB_ERROR_INTERRUPTED:
1693 return ("LIBUSB_ERROR_INTERRUPTED");
1694 case LIBUSB_ERROR_NO_MEM:
1695 return ("LIBUSB_ERROR_NO_MEM");
1696 case LIBUSB_ERROR_NOT_SUPPORTED:
1697 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1698 case LIBUSB_ERROR_OTHER:
1699 return ("LIBUSB_ERROR_OTHER");
1701 return ("LIBUSB_ERROR_UNKNOWN");