3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
40 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #include <sys/endian.h>
46 #define libusb_device_handle libusb20_device
49 #include "libusb20_desc.h"
50 #include "libusb20_int.h"
54 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
56 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
57 struct libusb_context *usbi_default_context = NULL;
61 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
62 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
63 static int libusb10_convert_error(uint8_t status);
64 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
65 static void libusb10_isoc_proxy(struct libusb20_transfer *);
66 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
67 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
68 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
70 /* Library initialisation / deinitialisation */
72 static const struct libusb_version libusb_version = {
78 .describe = "http://www.freebsd.org"
81 const struct libusb_version *
82 libusb_get_version(void)
85 return (&libusb_version);
89 libusb_set_debug(libusb_context *ctx, int level)
91 ctx = GET_CONTEXT(ctx);
97 libusb_set_nonblocking(int f)
102 * We ignore any failures in this function, hence the
103 * non-blocking flag is not critical to the operation of
104 * libUSB. We use F_GETFL and F_SETFL to be compatible with
108 flags = fcntl(f, F_GETFL, NULL);
112 fcntl(f, F_SETFL, flags);
116 libusb10_wakeup_event_loop(libusb_context *ctx)
121 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
122 if (err < (int)sizeof(dummy)) {
123 /* ignore error, if any */
124 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
129 libusb_init(libusb_context **context)
131 struct libusb_context *ctx;
132 pthread_condattr_t attr;
136 ctx = malloc(sizeof(*ctx));
138 return (LIBUSB_ERROR_INVALID_PARAM);
140 memset(ctx, 0, sizeof(*ctx));
142 debug = getenv("LIBUSB_DEBUG");
144 ctx->debug = atoi(debug);
146 ctx->debug_fixed = 1;
148 TAILQ_INIT(&ctx->pollfds);
149 TAILQ_INIT(&ctx->tr_done);
150 TAILQ_INIT(&ctx->hotplug_cbh);
151 TAILQ_INIT(&ctx->hotplug_devs);
153 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
155 return (LIBUSB_ERROR_NO_MEM);
157 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
158 pthread_mutex_destroy(&ctx->ctx_lock);
160 return (LIBUSB_ERROR_NO_MEM);
162 if (pthread_condattr_init(&attr) != 0) {
163 pthread_mutex_destroy(&ctx->ctx_lock);
164 pthread_mutex_destroy(&ctx->hotplug_lock);
166 return (LIBUSB_ERROR_NO_MEM);
168 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
169 pthread_mutex_destroy(&ctx->ctx_lock);
170 pthread_mutex_destroy(&ctx->hotplug_lock);
171 pthread_condattr_destroy(&attr);
173 return (LIBUSB_ERROR_OTHER);
175 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
176 pthread_mutex_destroy(&ctx->ctx_lock);
177 pthread_mutex_destroy(&ctx->hotplug_lock);
178 pthread_condattr_destroy(&attr);
180 return (LIBUSB_ERROR_NO_MEM);
182 pthread_condattr_destroy(&attr);
184 ctx->ctx_handler = NO_THREAD;
185 ctx->hotplug_handler = NO_THREAD;
187 ret = pipe(ctx->ctrl_pipe);
189 pthread_mutex_destroy(&ctx->ctx_lock);
190 pthread_mutex_destroy(&ctx->hotplug_lock);
191 pthread_cond_destroy(&ctx->ctx_cond);
193 return (LIBUSB_ERROR_OTHER);
195 /* set non-blocking mode on the control pipe to avoid deadlock */
196 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
197 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
199 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
201 pthread_mutex_lock(&default_context_lock);
202 if (usbi_default_context == NULL) {
203 usbi_default_context = ctx;
205 pthread_mutex_unlock(&default_context_lock);
210 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
216 libusb_exit(libusb_context *ctx)
218 ctx = GET_CONTEXT(ctx);
223 /* stop hotplug thread, if any */
225 if (ctx->hotplug_handler != NO_THREAD) {
230 td = ctx->hotplug_handler;
231 ctx->hotplug_handler = NO_THREAD;
234 pthread_join(td, &ptr);
237 /* XXX cleanup devices */
239 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
240 close(ctx->ctrl_pipe[0]);
241 close(ctx->ctrl_pipe[1]);
242 pthread_mutex_destroy(&ctx->ctx_lock);
243 pthread_mutex_destroy(&ctx->hotplug_lock);
244 pthread_cond_destroy(&ctx->ctx_cond);
246 pthread_mutex_lock(&default_context_lock);
247 if (ctx == usbi_default_context) {
248 usbi_default_context = NULL;
250 pthread_mutex_unlock(&default_context_lock);
255 /* Device handling and initialisation. */
258 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
260 struct libusb20_backend *usb_backend;
261 struct libusb20_device *pdev;
262 struct libusb_device *dev;
265 ctx = GET_CONTEXT(ctx);
268 return (LIBUSB_ERROR_INVALID_PARAM);
271 return (LIBUSB_ERROR_INVALID_PARAM);
273 usb_backend = libusb20_be_alloc_default();
274 if (usb_backend == NULL)
275 return (LIBUSB_ERROR_NO_MEM);
277 /* figure out how many USB devices are present */
280 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
283 /* allocate device pointer list */
284 *list = malloc((i + 1) * sizeof(void *));
286 libusb20_be_free(usb_backend);
287 return (LIBUSB_ERROR_NO_MEM);
289 /* create libusb v1.0 compliant devices */
291 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
293 dev = malloc(sizeof(*dev));
296 libusb_unref_device((*list)[i - 1]);
301 libusb20_be_free(usb_backend);
302 return (LIBUSB_ERROR_NO_MEM);
304 /* get device into libUSB v1.0 list */
305 libusb20_be_dequeue_device(usb_backend, pdev);
307 memset(dev, 0, sizeof(*dev));
309 /* init transfer queues */
310 TAILQ_INIT(&dev->tr_head);
312 /* set context we belong to */
315 /* link together the two structures */
317 pdev->privLuData = dev;
319 (*list)[i] = libusb_ref_device(dev);
324 libusb20_be_free(usb_backend);
329 libusb_free_device_list(libusb_device **list, int unref_devices)
334 return; /* be NULL safe */
337 for (i = 0; list[i] != NULL; i++)
338 libusb_unref_device(list[i]);
344 libusb_get_bus_number(libusb_device *dev)
347 return (0); /* should not happen */
348 return (libusb20_dev_get_bus_number(dev->os_priv));
352 libusb_get_port_number(libusb_device *dev)
355 return (0); /* should not happen */
356 return (libusb20_dev_get_parent_port(dev->os_priv));
360 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
362 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
366 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
369 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
373 libusb_get_device_address(libusb_device *dev)
376 return (0); /* should not happen */
377 return (libusb20_dev_get_address(dev->os_priv));
381 libusb_get_device_speed(libusb_device *dev)
384 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
386 switch (libusb20_dev_get_speed(dev->os_priv)) {
387 case LIBUSB20_SPEED_LOW:
388 return (LIBUSB_SPEED_LOW);
389 case LIBUSB20_SPEED_FULL:
390 return (LIBUSB_SPEED_FULL);
391 case LIBUSB20_SPEED_HIGH:
392 return (LIBUSB_SPEED_HIGH);
393 case LIBUSB20_SPEED_SUPER:
394 return (LIBUSB_SPEED_SUPER);
398 return (LIBUSB_SPEED_UNKNOWN);
402 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
404 struct libusb_config_descriptor *pdconf;
405 struct libusb_interface *pinf;
406 struct libusb_interface_descriptor *pdinf;
407 struct libusb_endpoint_descriptor *pdend;
414 return (LIBUSB_ERROR_NO_DEVICE);
416 ret = libusb_get_active_config_descriptor(dev, &pdconf);
420 ret = LIBUSB_ERROR_NOT_FOUND;
421 for (i = 0; i < pdconf->bNumInterfaces; i++) {
422 pinf = &pdconf->interface[i];
423 for (j = 0; j < pinf->num_altsetting; j++) {
424 pdinf = &pinf->altsetting[j];
425 for (k = 0; k < pdinf->bNumEndpoints; k++) {
426 pdend = &pdinf->endpoint[k];
427 if (pdend->bEndpointAddress == endpoint) {
428 ret = pdend->wMaxPacketSize;
436 libusb_free_config_descriptor(pdconf);
441 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
446 ret = libusb_get_max_packet_size(dev, endpoint);
448 switch (libusb20_dev_get_speed(dev->os_priv)) {
449 case LIBUSB20_SPEED_LOW:
450 case LIBUSB20_SPEED_FULL:
454 multiplier = (1 + ((ret >> 11) & 3));
457 ret = (ret & 0x7FF) * multiplier;
465 libusb_ref_device(libusb_device *dev)
468 return (NULL); /* be NULL safe */
472 CTX_UNLOCK(dev->ctx);
478 libusb_unref_device(libusb_device *dev)
481 return; /* be NULL safe */
485 CTX_UNLOCK(dev->ctx);
487 if (dev->refcnt == 0) {
488 libusb20_dev_free(dev->os_priv);
494 libusb_open(libusb_device *dev, libusb_device_handle **devh)
496 libusb_context *ctx = dev->ctx;
497 struct libusb20_device *pdev = dev->os_priv;
501 return (LIBUSB_ERROR_INVALID_PARAM);
503 /* set default device handle value */
506 dev = libusb_ref_device(dev);
508 return (LIBUSB_ERROR_INVALID_PARAM);
510 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
512 libusb_unref_device(dev);
513 return (LIBUSB_ERROR_NO_MEM);
515 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
516 POLLOUT | POLLRDNORM | POLLWRNORM);
518 /* make sure our event loop detects the new device */
519 libusb10_wakeup_event_loop(ctx);
526 libusb_device_handle *
527 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
530 struct libusb_device **devs;
531 struct libusb20_device *pdev;
532 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
536 ctx = GET_CONTEXT(ctx);
538 return (NULL); /* be NULL safe */
540 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid enter");
542 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
546 for (j = 0; j < i; j++) {
547 struct libusb20_device *tdev;
549 tdev = devs[j]->os_priv;
550 pdesc = libusb20_dev_get_device_desc(tdev);
552 * NOTE: The USB library will automatically swap the
553 * fields in the device descriptor to be of host
556 if (pdesc->idVendor == vendor_id &&
557 pdesc->idProduct == product_id) {
558 libusb_open(devs[j], &pdev);
563 libusb_free_device_list(devs, 1);
564 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_with_vid_pid leave");
569 libusb_close(struct libusb20_device *pdev)
572 struct libusb_device *dev;
575 return; /* be NULL safe */
577 dev = libusb_get_device(pdev);
580 libusb10_remove_pollfd(ctx, &dev->dev_poll);
582 libusb20_dev_close(pdev);
584 /* unref will free the "pdev" when the refcount reaches zero */
585 libusb_unref_device(dev);
587 /* make sure our event loop detects the closed device */
588 libusb10_wakeup_event_loop(ctx);
592 libusb_get_device(struct libusb20_device *pdev)
596 return ((libusb_device *)pdev->privLuData);
600 libusb_get_configuration(struct libusb20_device *pdev, int *config)
602 struct libusb20_config *pconf;
604 if (pdev == NULL || config == NULL)
605 return (LIBUSB_ERROR_INVALID_PARAM);
607 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
609 return (LIBUSB_ERROR_NO_MEM);
611 *config = pconf->desc.bConfigurationValue;
619 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
621 struct libusb20_config *pconf;
622 struct libusb_device *dev;
626 dev = libusb_get_device(pdev);
628 return (LIBUSB_ERROR_INVALID_PARAM);
630 if (configuration < 1) {
634 for (i = 0; i != 255; i++) {
637 pconf = libusb20_dev_alloc_config(pdev, i);
639 return (LIBUSB_ERROR_INVALID_PARAM);
640 found = (pconf->desc.bConfigurationValue
647 return (LIBUSB_ERROR_INVALID_PARAM);
652 libusb10_cancel_all_transfer(dev);
654 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
656 err = libusb20_dev_set_config_index(pdev, i);
658 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
659 POLLOUT | POLLRDNORM | POLLWRNORM);
661 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
665 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
670 dev = libusb_get_device(pdev);
672 return (LIBUSB_ERROR_INVALID_PARAM);
674 if (interface_number < 0 || interface_number > 31)
675 return (LIBUSB_ERROR_INVALID_PARAM);
677 if (pdev->auto_detach != 0) {
678 err = libusb_detach_kernel_driver(pdev, interface_number);
684 dev->claimed_interfaces |= (1 << interface_number);
685 CTX_UNLOCK(dev->ctx);
691 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
696 dev = libusb_get_device(pdev);
698 return (LIBUSB_ERROR_INVALID_PARAM);
700 if (interface_number < 0 || interface_number > 31)
701 return (LIBUSB_ERROR_INVALID_PARAM);
703 if (pdev->auto_detach != 0) {
704 err = libusb_attach_kernel_driver(pdev, interface_number);
710 if (!(dev->claimed_interfaces & (1 << interface_number)))
711 err = LIBUSB_ERROR_NOT_FOUND;
713 dev->claimed_interfaces &= ~(1 << interface_number);
714 CTX_UNLOCK(dev->ctx);
720 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
721 int interface_number, int alternate_setting)
726 dev = libusb_get_device(pdev);
728 return (LIBUSB_ERROR_INVALID_PARAM);
730 if (interface_number < 0 || interface_number > 31)
731 return (LIBUSB_ERROR_INVALID_PARAM);
734 if (!(dev->claimed_interfaces & (1 << interface_number)))
735 err = LIBUSB_ERROR_NOT_FOUND;
736 CTX_UNLOCK(dev->ctx);
741 libusb10_cancel_all_transfer(dev);
743 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
745 err = libusb20_dev_set_alt_index(pdev,
746 interface_number, alternate_setting);
748 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
749 pdev, libusb20_dev_get_fd(pdev),
750 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
752 return (err ? LIBUSB_ERROR_OTHER : 0);
755 static struct libusb20_transfer *
756 libusb10_get_transfer(struct libusb20_device *pdev,
757 uint8_t endpoint, uint8_t xfer_index)
759 xfer_index &= 1; /* double buffering */
761 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
763 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
764 /* this is an IN endpoint */
767 return (libusb20_tr_get_pointer(pdev, xfer_index));
771 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
773 struct libusb20_transfer *xfer;
774 struct libusb_device *dev;
777 xfer = libusb10_get_transfer(pdev, endpoint, 0);
779 return (LIBUSB_ERROR_INVALID_PARAM);
781 dev = libusb_get_device(pdev);
783 return (LIBUSB_ERROR_INVALID_PARAM);
786 err = libusb20_tr_open(xfer, 0, 1, endpoint);
787 CTX_UNLOCK(dev->ctx);
789 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
790 return (LIBUSB_ERROR_OTHER);
792 libusb20_tr_clear_stall_sync(xfer);
794 /* check if we opened the transfer */
797 libusb20_tr_close(xfer);
798 CTX_UNLOCK(dev->ctx);
800 return (0); /* success */
804 libusb_reset_device(struct libusb20_device *pdev)
809 dev = libusb_get_device(pdev);
811 return (LIBUSB_ERROR_INVALID_PARAM);
813 libusb10_cancel_all_transfer(dev);
815 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
817 err = libusb20_dev_reset(pdev);
819 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
820 pdev, libusb20_dev_get_fd(pdev),
821 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
823 return (err ? LIBUSB_ERROR_OTHER : 0);
827 libusb_check_connected(struct libusb20_device *pdev)
832 dev = libusb_get_device(pdev);
834 return (LIBUSB_ERROR_INVALID_PARAM);
836 err = libusb20_dev_check_connected(pdev);
838 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
842 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
845 return (LIBUSB_ERROR_INVALID_PARAM);
847 if (libusb20_dev_kernel_driver_active(pdev, interface))
848 return (0); /* no kernel driver is active */
850 return (1); /* kernel driver is active */
854 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
855 char *name, int namelen)
857 return (libusb_get_driver(pdev, interface, name, namelen));
861 libusb_get_driver(struct libusb20_device *pdev, int interface,
862 char *name, int namelen)
868 return (LIBUSB_ERROR_INVALID_PARAM);
870 return (LIBUSB_ERROR_INVALID_PARAM);
874 err = libusb20_dev_get_iface_desc(
875 pdev, interface, name, namelen);
878 return (LIBUSB_ERROR_OTHER);
880 /* we only want the driver name */
881 ptr = strstr(name, ":");
889 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
891 return (libusb_detach_kernel_driver(pdev, interface));
895 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
900 return (LIBUSB_ERROR_INVALID_PARAM);
902 err = libusb20_dev_detach_kernel_driver(
905 return (err ? LIBUSB_ERROR_OTHER : 0);
909 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
912 return (LIBUSB_ERROR_INVALID_PARAM);
913 /* stub - currently not supported by libusb20 */
918 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
920 dev->auto_detach = (enable ? 1 : 0);
924 /* Asynchronous device I/O */
926 struct libusb_transfer *
927 libusb_alloc_transfer(int iso_packets)
929 struct libusb_transfer *uxfer;
930 struct libusb_super_transfer *sxfer;
933 len = sizeof(struct libusb_transfer) +
934 sizeof(struct libusb_super_transfer) +
935 (iso_packets * sizeof(libusb_iso_packet_descriptor));
941 memset(sxfer, 0, len);
943 uxfer = (struct libusb_transfer *)(
944 ((uint8_t *)sxfer) + sizeof(*sxfer));
946 /* set default value */
947 uxfer->num_iso_packets = iso_packets;
953 libusb_free_transfer(struct libusb_transfer *uxfer)
955 struct libusb_super_transfer *sxfer;
958 return; /* be NULL safe */
960 /* check if we should free the transfer buffer */
961 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
964 sxfer = (struct libusb_super_transfer *)(
965 (uint8_t *)uxfer - sizeof(*sxfer));
971 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
975 switch (xfer->type) {
976 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
977 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
979 case LIBUSB_TRANSFER_TYPE_CONTROL:
990 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
995 usb_speed = libusb20_dev_get_speed(pdev);
997 switch (xfer->type) {
998 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
999 ret = 0; /* kernel will auto-select */
1001 case LIBUSB_TRANSFER_TYPE_CONTROL:
1005 switch (usb_speed) {
1006 case LIBUSB20_SPEED_LOW:
1009 case LIBUSB20_SPEED_FULL:
1012 case LIBUSB20_SPEED_SUPER:
1025 libusb10_convert_error(uint8_t status)
1030 case LIBUSB20_TRANSFER_START:
1031 case LIBUSB20_TRANSFER_COMPLETED:
1032 return (LIBUSB_TRANSFER_COMPLETED);
1033 case LIBUSB20_TRANSFER_OVERFLOW:
1034 return (LIBUSB_TRANSFER_OVERFLOW);
1035 case LIBUSB20_TRANSFER_NO_DEVICE:
1036 return (LIBUSB_TRANSFER_NO_DEVICE);
1037 case LIBUSB20_TRANSFER_STALL:
1038 return (LIBUSB_TRANSFER_STALL);
1039 case LIBUSB20_TRANSFER_CANCELLED:
1040 return (LIBUSB_TRANSFER_CANCELLED);
1041 case LIBUSB20_TRANSFER_TIMED_OUT:
1042 return (LIBUSB_TRANSFER_TIMED_OUT);
1044 return (LIBUSB_TRANSFER_ERROR);
1048 /* This function must be called locked */
1051 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1052 struct libusb_super_transfer *sxfer, int status)
1054 struct libusb_transfer *uxfer;
1055 struct libusb_device *dev;
1057 uxfer = (struct libusb_transfer *)(
1058 ((uint8_t *)sxfer) + sizeof(*sxfer));
1061 libusb20_tr_set_priv_sc1(pxfer, NULL);
1063 /* set transfer status */
1064 uxfer->status = status;
1066 /* update super transfer state */
1067 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1069 dev = libusb_get_device(uxfer->dev_handle);
1071 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1074 /* This function must be called locked */
1077 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1079 struct libusb_super_transfer *sxfer;
1080 struct libusb_transfer *uxfer;
1082 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;
1107 flags = uxfer->flags;
1110 case LIBUSB20_TRANSFER_COMPLETED:
1112 /* update actual length */
1113 uxfer->actual_length = actlen;
1114 for (i = 0; i != iso_packets; i++) {
1115 uxfer->iso_packet_desc[i].actual_length =
1116 libusb20_tr_get_length(pxfer, i);
1118 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1121 case LIBUSB20_TRANSFER_START:
1123 /* setup length(s) */
1125 for (i = 0; i != iso_packets; i++) {
1126 libusb20_tr_setup_isoc(pxfer,
1127 &uxfer->buffer[actlen],
1128 uxfer->iso_packet_desc[i].length, i);
1129 actlen += uxfer->iso_packet_desc[i].length;
1135 libusb20_tr_set_total_frames(pxfer, iso_packets);
1136 libusb20_tr_submit(pxfer);
1138 /* fork another USB transfer, if any */
1139 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1143 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1148 /* This function must be called locked */
1151 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1153 struct libusb_super_transfer *sxfer;
1154 struct libusb_transfer *uxfer;
1160 status = libusb20_tr_get_status(pxfer);
1161 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1162 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1163 actlen = libusb20_tr_get_actual_length(pxfer);
1166 return; /* cancelled - nothing to do */
1168 uxfer = (struct libusb_transfer *)(
1169 ((uint8_t *)sxfer) + sizeof(*sxfer));
1171 flags = uxfer->flags;
1174 case LIBUSB20_TRANSFER_COMPLETED:
1176 uxfer->actual_length += actlen;
1178 /* check for short packet */
1179 if (sxfer->last_len != actlen) {
1180 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1181 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1183 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1187 /* check for end of data */
1188 if (sxfer->rem_len == 0) {
1189 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1194 case LIBUSB20_TRANSFER_START:
1195 if (max_bulk > sxfer->rem_len) {
1196 max_bulk = sxfer->rem_len;
1198 /* setup new BULK or INTERRUPT transaction */
1199 libusb20_tr_setup_bulk(pxfer,
1200 sxfer->curr_data, max_bulk, uxfer->timeout);
1202 /* update counters */
1203 sxfer->last_len = max_bulk;
1204 sxfer->curr_data += max_bulk;
1205 sxfer->rem_len -= max_bulk;
1207 libusb20_tr_submit(pxfer);
1209 /* check if we can fork another USB transfer */
1210 if (sxfer->rem_len == 0)
1211 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1215 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1220 /* This function must be called locked */
1223 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1225 struct libusb_super_transfer *sxfer;
1226 struct libusb_transfer *uxfer;
1232 status = libusb20_tr_get_status(pxfer);
1233 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1234 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1235 actlen = libusb20_tr_get_actual_length(pxfer);
1238 return; /* cancelled - nothing to do */
1240 uxfer = (struct libusb_transfer *)(
1241 ((uint8_t *)sxfer) + sizeof(*sxfer));
1243 flags = uxfer->flags;
1246 case LIBUSB20_TRANSFER_COMPLETED:
1248 uxfer->actual_length += actlen;
1250 /* subtract length of SETUP packet, if any */
1251 actlen -= libusb20_tr_get_length(pxfer, 0);
1253 /* check for short packet */
1254 if (sxfer->last_len != actlen) {
1255 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1256 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1258 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1262 /* check for end of data */
1263 if (sxfer->rem_len == 0) {
1264 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1269 case LIBUSB20_TRANSFER_START:
1270 if (max_bulk > sxfer->rem_len) {
1271 max_bulk = sxfer->rem_len;
1273 /* setup new CONTROL transaction */
1274 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1275 /* next fragment - don't send SETUP packet */
1276 libusb20_tr_set_length(pxfer, 0, 0);
1278 /* first fragment - send SETUP packet */
1279 libusb20_tr_set_length(pxfer, 8, 0);
1280 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1283 if (max_bulk != 0) {
1284 libusb20_tr_set_length(pxfer, max_bulk, 1);
1285 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1286 libusb20_tr_set_total_frames(pxfer, 2);
1288 libusb20_tr_set_total_frames(pxfer, 1);
1291 /* update counters */
1292 sxfer->last_len = max_bulk;
1293 sxfer->curr_data += max_bulk;
1294 sxfer->rem_len -= max_bulk;
1296 libusb20_tr_submit(pxfer);
1298 /* check if we can fork another USB transfer */
1299 if (sxfer->rem_len == 0)
1300 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1304 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1309 /* The following function must be called locked */
1312 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1314 struct libusb20_transfer *pxfer0;
1315 struct libusb20_transfer *pxfer1;
1316 struct libusb_super_transfer *sxfer;
1317 struct libusb_transfer *uxfer;
1318 struct libusb_device *dev;
1324 dev = libusb_get_device(pdev);
1326 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1327 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1329 if (pxfer0 == NULL || pxfer1 == NULL)
1330 return; /* shouldn't happen */
1333 if (libusb20_tr_pending(pxfer0))
1335 if (libusb20_tr_pending(pxfer1))
1340 /* wait till one of the transfers complete */
1343 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1345 return; /* cancelling */
1347 return; /* cannot queue another one */
1348 /* swap transfers */
1352 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1354 return; /* cancelling */
1356 return; /* cannot queue another one */
1357 /* swap transfers */
1364 /* find next transfer on same endpoint */
1365 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1367 uxfer = (struct libusb_transfer *)(
1368 ((uint8_t *)sxfer) + sizeof(*sxfer));
1370 if (uxfer->endpoint == endpoint) {
1371 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1372 sxfer->entry.tqe_prev = NULL;
1376 return; /* success */
1380 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1381 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1383 /* reset super transfer state */
1384 sxfer->rem_len = uxfer->length;
1385 sxfer->curr_data = uxfer->buffer;
1386 uxfer->actual_length = 0;
1388 switch (uxfer->type) {
1389 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1390 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1392 case LIBUSB_TRANSFER_TYPE_BULK:
1393 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1394 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1396 case LIBUSB_TRANSFER_TYPE_CONTROL:
1397 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1398 if (sxfer->rem_len < 8)
1401 /* remove SETUP packet from data */
1402 sxfer->rem_len -= 8;
1403 sxfer->curr_data += 8;
1409 buffsize = libusb10_get_buffsize(pdev, uxfer);
1410 maxframe = libusb10_get_maxframe(pdev, uxfer);
1412 /* make sure the transfer is opened */
1413 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1414 endpoint, sxfer->stream_id);
1415 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1418 libusb20_tr_start(pxfer0);
1422 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1423 /* make sure our event loop spins the done handler */
1424 libusb10_wakeup_event_loop(dev->ctx);
1427 /* The following function must be called unlocked */
1430 libusb_submit_transfer(struct libusb_transfer *uxfer)
1432 struct libusb20_transfer *pxfer0;
1433 struct libusb20_transfer *pxfer1;
1434 struct libusb_super_transfer *sxfer;
1435 struct libusb_device *dev;
1440 return (LIBUSB_ERROR_INVALID_PARAM);
1442 if (uxfer->dev_handle == NULL)
1443 return (LIBUSB_ERROR_INVALID_PARAM);
1445 endpoint = uxfer->endpoint;
1447 dev = libusb_get_device(uxfer->dev_handle);
1449 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1451 sxfer = (struct libusb_super_transfer *)(
1452 (uint8_t *)uxfer - sizeof(*sxfer));
1456 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1457 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1459 if (pxfer0 == NULL || pxfer1 == NULL) {
1460 err = LIBUSB_ERROR_OTHER;
1461 } else if ((sxfer->entry.tqe_prev != NULL) ||
1462 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1463 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1464 err = LIBUSB_ERROR_BUSY;
1465 } else if (dev->device_is_gone != 0) {
1466 err = LIBUSB_ERROR_NO_DEVICE;
1469 /* set pending state */
1470 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1472 /* insert transfer into transfer head list */
1473 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1475 /* start work transfers */
1476 libusb10_submit_transfer_sub(
1477 uxfer->dev_handle, endpoint);
1479 err = 0; /* success */
1482 CTX_UNLOCK(dev->ctx);
1484 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1489 /* Asynchronous transfer cancel */
1492 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1494 struct libusb20_transfer *pxfer0;
1495 struct libusb20_transfer *pxfer1;
1496 struct libusb_super_transfer *sxfer;
1497 struct libusb_device *dev;
1498 struct libusb_device_handle *devh;
1503 return (LIBUSB_ERROR_INVALID_PARAM);
1505 /* check if not initialised */
1506 if ((devh = uxfer->dev_handle) == NULL)
1507 return (LIBUSB_ERROR_NOT_FOUND);
1509 endpoint = uxfer->endpoint;
1511 dev = libusb_get_device(devh);
1513 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1515 sxfer = (struct libusb_super_transfer *)(
1516 (uint8_t *)uxfer - sizeof(*sxfer));
1522 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1523 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1525 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1526 /* only update the transfer status */
1527 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1528 retval = LIBUSB_ERROR_NOT_FOUND;
1529 } else if (sxfer->entry.tqe_prev != NULL) {
1530 /* we are lucky - transfer is on a queue */
1531 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1532 sxfer->entry.tqe_prev = NULL;
1533 libusb10_complete_transfer(NULL,
1534 sxfer, LIBUSB_TRANSFER_CANCELLED);
1535 /* make sure our event loop spins the done handler */
1536 libusb10_wakeup_event_loop(dev->ctx);
1537 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1539 retval = LIBUSB_ERROR_NOT_FOUND;
1540 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1541 libusb10_complete_transfer(pxfer0,
1542 sxfer, LIBUSB_TRANSFER_CANCELLED);
1543 if (dev->device_is_gone != 0) {
1544 /* clear transfer pointer */
1545 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1546 /* make sure our event loop spins the done handler */
1547 libusb10_wakeup_event_loop(dev->ctx);
1549 libusb20_tr_stop(pxfer0);
1550 /* make sure the queue doesn't stall */
1551 libusb10_submit_transfer_sub(devh, endpoint);
1553 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1554 libusb10_complete_transfer(pxfer1,
1555 sxfer, LIBUSB_TRANSFER_CANCELLED);
1556 /* check if handle is still active */
1557 if (dev->device_is_gone != 0) {
1558 /* clear transfer pointer */
1559 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1560 /* make sure our event loop spins the done handler */
1561 libusb10_wakeup_event_loop(dev->ctx);
1563 libusb20_tr_stop(pxfer1);
1564 /* make sure the queue doesn't stall */
1565 libusb10_submit_transfer_sub(devh, endpoint);
1569 retval = LIBUSB_ERROR_NOT_FOUND;
1572 CTX_UNLOCK(dev->ctx);
1574 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1580 libusb10_cancel_all_transfer(libusb_device *dev)
1582 struct libusb20_device *pdev = dev->os_priv;
1585 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1586 struct libusb20_transfer *xfer;
1588 xfer = libusb20_tr_get_pointer(pdev, x);
1591 libusb20_tr_close(xfer);
1596 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1598 struct libusb_super_transfer *sxfer;
1601 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1602 struct libusb20_transfer *xfer;
1604 xfer = libusb20_tr_get_pointer(pdev, x);
1607 if (libusb20_tr_pending(xfer) == 0)
1609 sxfer = libusb20_tr_get_priv_sc1(xfer);
1612 /* complete pending transfer */
1613 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1616 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1617 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1619 /* complete pending transfer */
1620 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1625 libusb_cpu_to_le16(uint16_t x)
1627 return (htole16(x));
1631 libusb_le16_to_cpu(uint16_t x)
1633 return (le16toh(x));
1637 libusb_strerror(int code)
1640 case LIBUSB_SUCCESS:
1642 case LIBUSB_ERROR_IO:
1643 return ("I/O error");
1644 case LIBUSB_ERROR_INVALID_PARAM:
1645 return ("Invalid parameter");
1646 case LIBUSB_ERROR_ACCESS:
1647 return ("Permissions error");
1648 case LIBUSB_ERROR_NO_DEVICE:
1649 return ("No device");
1650 case LIBUSB_ERROR_NOT_FOUND:
1651 return ("Not found");
1652 case LIBUSB_ERROR_BUSY:
1653 return ("Device busy");
1654 case LIBUSB_ERROR_TIMEOUT:
1656 case LIBUSB_ERROR_OVERFLOW:
1657 return ("Overflow");
1658 case LIBUSB_ERROR_PIPE:
1659 return ("Pipe error");
1660 case LIBUSB_ERROR_INTERRUPTED:
1661 return ("Interrupted");
1662 case LIBUSB_ERROR_NO_MEM:
1663 return ("Out of memory");
1664 case LIBUSB_ERROR_NOT_SUPPORTED:
1665 return ("Not supported");
1666 case LIBUSB_ERROR_OTHER:
1667 return ("Other error");
1669 return ("Unknown error");
1674 libusb_error_name(int code)
1677 case LIBUSB_SUCCESS:
1678 return ("LIBUSB_SUCCESS");
1679 case LIBUSB_ERROR_IO:
1680 return ("LIBUSB_ERROR_IO");
1681 case LIBUSB_ERROR_INVALID_PARAM:
1682 return ("LIBUSB_ERROR_INVALID_PARAM");
1683 case LIBUSB_ERROR_ACCESS:
1684 return ("LIBUSB_ERROR_ACCESS");
1685 case LIBUSB_ERROR_NO_DEVICE:
1686 return ("LIBUSB_ERROR_NO_DEVICE");
1687 case LIBUSB_ERROR_NOT_FOUND:
1688 return ("LIBUSB_ERROR_NOT_FOUND");
1689 case LIBUSB_ERROR_BUSY:
1690 return ("LIBUSB_ERROR_BUSY");
1691 case LIBUSB_ERROR_TIMEOUT:
1692 return ("LIBUSB_ERROR_TIMEOUT");
1693 case LIBUSB_ERROR_OVERFLOW:
1694 return ("LIBUSB_ERROR_OVERFLOW");
1695 case LIBUSB_ERROR_PIPE:
1696 return ("LIBUSB_ERROR_PIPE");
1697 case LIBUSB_ERROR_INTERRUPTED:
1698 return ("LIBUSB_ERROR_INTERRUPTED");
1699 case LIBUSB_ERROR_NO_MEM:
1700 return ("LIBUSB_ERROR_NO_MEM");
1701 case LIBUSB_ERROR_NOT_SUPPORTED:
1702 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1703 case LIBUSB_ERROR_OTHER:
1704 return ("LIBUSB_ERROR_OTHER");
1706 return ("LIBUSB_ERROR_UNKNOWN");