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;
1086 status = libusb20_tr_get_status(pxfer);
1087 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1088 actlen = libusb20_tr_get_actual_length(pxfer);
1089 iso_packets = libusb20_tr_get_max_frames(pxfer);
1092 return; /* cancelled - nothing to do */
1094 uxfer = (struct libusb_transfer *)(
1095 ((uint8_t *)sxfer) + sizeof(*sxfer));
1097 if (iso_packets > uxfer->num_iso_packets)
1098 iso_packets = uxfer->num_iso_packets;
1100 if (iso_packets == 0)
1101 return; /* nothing to do */
1103 /* make sure that the number of ISOCHRONOUS packets is valid */
1104 uxfer->num_iso_packets = iso_packets;
1107 case LIBUSB20_TRANSFER_COMPLETED:
1108 /* update actual length */
1109 uxfer->actual_length = actlen;
1110 for (i = 0; i != iso_packets; i++) {
1111 uxfer->iso_packet_desc[i].actual_length =
1112 libusb20_tr_get_length(pxfer, i);
1114 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1116 case LIBUSB20_TRANSFER_START:
1117 /* setup length(s) */
1119 for (i = 0; i != iso_packets; i++) {
1120 libusb20_tr_setup_isoc(pxfer,
1121 &uxfer->buffer[actlen],
1122 uxfer->iso_packet_desc[i].length, i);
1123 actlen += uxfer->iso_packet_desc[i].length;
1129 libusb20_tr_set_total_frames(pxfer, iso_packets);
1130 libusb20_tr_submit(pxfer);
1132 /* fork another USB transfer, if any */
1133 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1136 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1141 /* This function must be called locked */
1144 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1146 struct libusb_super_transfer *sxfer;
1147 struct libusb_transfer *uxfer;
1153 status = libusb20_tr_get_status(pxfer);
1154 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1155 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1156 actlen = libusb20_tr_get_actual_length(pxfer);
1159 return; /* cancelled - nothing to do */
1161 uxfer = (struct libusb_transfer *)(
1162 ((uint8_t *)sxfer) + sizeof(*sxfer));
1164 flags = uxfer->flags;
1167 case LIBUSB20_TRANSFER_COMPLETED:
1169 uxfer->actual_length += actlen;
1171 /* check for short packet */
1172 if (sxfer->last_len != actlen) {
1173 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1174 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1176 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1180 /* check for end of data */
1181 if (sxfer->rem_len == 0) {
1182 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1187 case LIBUSB20_TRANSFER_START:
1188 if (max_bulk > sxfer->rem_len) {
1189 max_bulk = sxfer->rem_len;
1191 /* setup new BULK or INTERRUPT transaction */
1192 libusb20_tr_setup_bulk(pxfer,
1193 sxfer->curr_data, max_bulk, uxfer->timeout);
1195 /* update counters */
1196 sxfer->last_len = max_bulk;
1197 sxfer->curr_data += max_bulk;
1198 sxfer->rem_len -= max_bulk;
1200 libusb20_tr_submit(pxfer);
1202 /* check if we can fork another USB transfer */
1203 if (sxfer->rem_len == 0)
1204 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1208 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1213 /* This function must be called locked */
1216 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1218 struct libusb_super_transfer *sxfer;
1219 struct libusb_transfer *uxfer;
1225 status = libusb20_tr_get_status(pxfer);
1226 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1227 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1228 actlen = libusb20_tr_get_actual_length(pxfer);
1231 return; /* cancelled - nothing to do */
1233 uxfer = (struct libusb_transfer *)(
1234 ((uint8_t *)sxfer) + sizeof(*sxfer));
1236 flags = uxfer->flags;
1239 case LIBUSB20_TRANSFER_COMPLETED:
1241 uxfer->actual_length += actlen;
1243 /* subtract length of SETUP packet, if any */
1244 actlen -= libusb20_tr_get_length(pxfer, 0);
1246 /* check for short packet */
1247 if (sxfer->last_len != actlen) {
1248 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1249 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1251 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1255 /* check for end of data */
1256 if (sxfer->rem_len == 0) {
1257 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1262 case LIBUSB20_TRANSFER_START:
1263 if (max_bulk > sxfer->rem_len) {
1264 max_bulk = sxfer->rem_len;
1266 /* setup new CONTROL transaction */
1267 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1268 /* next fragment - don't send SETUP packet */
1269 libusb20_tr_set_length(pxfer, 0, 0);
1271 /* first fragment - send SETUP packet */
1272 libusb20_tr_set_length(pxfer, 8, 0);
1273 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1276 if (max_bulk != 0) {
1277 libusb20_tr_set_length(pxfer, max_bulk, 1);
1278 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1279 libusb20_tr_set_total_frames(pxfer, 2);
1281 libusb20_tr_set_total_frames(pxfer, 1);
1284 /* update counters */
1285 sxfer->last_len = max_bulk;
1286 sxfer->curr_data += max_bulk;
1287 sxfer->rem_len -= max_bulk;
1289 libusb20_tr_submit(pxfer);
1291 /* check if we can fork another USB transfer */
1292 if (sxfer->rem_len == 0)
1293 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1297 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1302 /* The following function must be called locked */
1305 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1307 struct libusb20_transfer *pxfer0;
1308 struct libusb20_transfer *pxfer1;
1309 struct libusb_super_transfer *sxfer;
1310 struct libusb_transfer *uxfer;
1311 struct libusb_device *dev;
1317 dev = libusb_get_device(pdev);
1319 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1320 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1322 if (pxfer0 == NULL || pxfer1 == NULL)
1323 return; /* shouldn't happen */
1326 if (libusb20_tr_pending(pxfer0))
1328 if (libusb20_tr_pending(pxfer1))
1333 /* wait till one of the transfers complete */
1336 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1338 return; /* cancelling */
1340 return; /* cannot queue another one */
1341 /* swap transfers */
1345 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1347 return; /* cancelling */
1349 return; /* cannot queue another one */
1350 /* swap transfers */
1357 /* find next transfer on same endpoint */
1358 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1360 uxfer = (struct libusb_transfer *)(
1361 ((uint8_t *)sxfer) + sizeof(*sxfer));
1363 if (uxfer->endpoint == endpoint) {
1364 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1365 sxfer->entry.tqe_prev = NULL;
1369 return; /* success */
1373 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1374 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1376 /* reset super transfer state */
1377 sxfer->rem_len = uxfer->length;
1378 sxfer->curr_data = uxfer->buffer;
1379 uxfer->actual_length = 0;
1381 switch (uxfer->type) {
1382 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1383 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1385 case LIBUSB_TRANSFER_TYPE_BULK:
1386 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1387 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1389 case LIBUSB_TRANSFER_TYPE_CONTROL:
1390 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1391 if (sxfer->rem_len < 8)
1394 /* remove SETUP packet from data */
1395 sxfer->rem_len -= 8;
1396 sxfer->curr_data += 8;
1402 buffsize = libusb10_get_buffsize(pdev, uxfer);
1403 maxframe = libusb10_get_maxframe(pdev, uxfer);
1405 /* make sure the transfer is opened */
1406 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1407 endpoint, sxfer->stream_id);
1408 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1411 libusb20_tr_start(pxfer0);
1415 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1416 /* make sure our event loop spins the done handler */
1417 libusb10_wakeup_event_loop(dev->ctx);
1420 /* The following function must be called unlocked */
1423 libusb_submit_transfer(struct libusb_transfer *uxfer)
1425 struct libusb20_transfer *pxfer0;
1426 struct libusb20_transfer *pxfer1;
1427 struct libusb_super_transfer *sxfer;
1428 struct libusb_device *dev;
1433 return (LIBUSB_ERROR_INVALID_PARAM);
1435 if (uxfer->dev_handle == NULL)
1436 return (LIBUSB_ERROR_INVALID_PARAM);
1438 endpoint = uxfer->endpoint;
1440 dev = libusb_get_device(uxfer->dev_handle);
1442 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1444 sxfer = (struct libusb_super_transfer *)(
1445 (uint8_t *)uxfer - sizeof(*sxfer));
1449 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1450 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1452 if (pxfer0 == NULL || pxfer1 == NULL) {
1453 err = LIBUSB_ERROR_OTHER;
1454 } else if ((sxfer->entry.tqe_prev != NULL) ||
1455 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1456 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1457 err = LIBUSB_ERROR_BUSY;
1458 } else if (dev->device_is_gone != 0) {
1459 err = LIBUSB_ERROR_NO_DEVICE;
1462 /* set pending state */
1463 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1465 /* insert transfer into transfer head list */
1466 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1468 /* start work transfers */
1469 libusb10_submit_transfer_sub(
1470 uxfer->dev_handle, endpoint);
1472 err = 0; /* success */
1475 CTX_UNLOCK(dev->ctx);
1477 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1482 /* Asynchronous transfer cancel */
1485 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1487 struct libusb20_transfer *pxfer0;
1488 struct libusb20_transfer *pxfer1;
1489 struct libusb_super_transfer *sxfer;
1490 struct libusb_device *dev;
1491 struct libusb_device_handle *devh;
1496 return (LIBUSB_ERROR_INVALID_PARAM);
1498 /* check if not initialised */
1499 if ((devh = uxfer->dev_handle) == NULL)
1500 return (LIBUSB_ERROR_NOT_FOUND);
1502 endpoint = uxfer->endpoint;
1504 dev = libusb_get_device(devh);
1506 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1508 sxfer = (struct libusb_super_transfer *)(
1509 (uint8_t *)uxfer - sizeof(*sxfer));
1515 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1516 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1518 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1519 /* only update the transfer status */
1520 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1521 retval = LIBUSB_ERROR_NOT_FOUND;
1522 } else if (sxfer->entry.tqe_prev != NULL) {
1523 /* we are lucky - transfer is on a queue */
1524 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1525 sxfer->entry.tqe_prev = NULL;
1526 libusb10_complete_transfer(NULL,
1527 sxfer, LIBUSB_TRANSFER_CANCELLED);
1528 /* make sure our event loop spins the done handler */
1529 libusb10_wakeup_event_loop(dev->ctx);
1530 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1532 retval = LIBUSB_ERROR_NOT_FOUND;
1533 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1534 libusb10_complete_transfer(pxfer0,
1535 sxfer, LIBUSB_TRANSFER_CANCELLED);
1536 if (dev->device_is_gone != 0) {
1537 /* clear transfer pointer */
1538 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1539 /* make sure our event loop spins the done handler */
1540 libusb10_wakeup_event_loop(dev->ctx);
1542 libusb20_tr_stop(pxfer0);
1543 /* make sure the queue doesn't stall */
1544 libusb10_submit_transfer_sub(devh, endpoint);
1546 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1547 libusb10_complete_transfer(pxfer1,
1548 sxfer, LIBUSB_TRANSFER_CANCELLED);
1549 /* check if handle is still active */
1550 if (dev->device_is_gone != 0) {
1551 /* clear transfer pointer */
1552 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1553 /* make sure our event loop spins the done handler */
1554 libusb10_wakeup_event_loop(dev->ctx);
1556 libusb20_tr_stop(pxfer1);
1557 /* make sure the queue doesn't stall */
1558 libusb10_submit_transfer_sub(devh, endpoint);
1562 retval = LIBUSB_ERROR_NOT_FOUND;
1565 CTX_UNLOCK(dev->ctx);
1567 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1573 libusb10_cancel_all_transfer(libusb_device *dev)
1575 struct libusb20_device *pdev = dev->os_priv;
1578 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1579 struct libusb20_transfer *xfer;
1581 xfer = libusb20_tr_get_pointer(pdev, x);
1584 libusb20_tr_close(xfer);
1589 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1591 struct libusb_super_transfer *sxfer;
1594 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1595 struct libusb20_transfer *xfer;
1597 xfer = libusb20_tr_get_pointer(pdev, x);
1600 if (libusb20_tr_pending(xfer) == 0)
1602 sxfer = libusb20_tr_get_priv_sc1(xfer);
1605 /* complete pending transfer */
1606 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1609 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1610 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1612 /* complete pending transfer */
1613 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1618 libusb_cpu_to_le16(uint16_t x)
1620 return (htole16(x));
1624 libusb_le16_to_cpu(uint16_t x)
1626 return (le16toh(x));
1630 libusb_strerror(int code)
1633 case LIBUSB_SUCCESS:
1635 case LIBUSB_ERROR_IO:
1636 return ("I/O error");
1637 case LIBUSB_ERROR_INVALID_PARAM:
1638 return ("Invalid parameter");
1639 case LIBUSB_ERROR_ACCESS:
1640 return ("Permissions error");
1641 case LIBUSB_ERROR_NO_DEVICE:
1642 return ("No device");
1643 case LIBUSB_ERROR_NOT_FOUND:
1644 return ("Not found");
1645 case LIBUSB_ERROR_BUSY:
1646 return ("Device busy");
1647 case LIBUSB_ERROR_TIMEOUT:
1649 case LIBUSB_ERROR_OVERFLOW:
1650 return ("Overflow");
1651 case LIBUSB_ERROR_PIPE:
1652 return ("Pipe error");
1653 case LIBUSB_ERROR_INTERRUPTED:
1654 return ("Interrupted");
1655 case LIBUSB_ERROR_NO_MEM:
1656 return ("Out of memory");
1657 case LIBUSB_ERROR_NOT_SUPPORTED:
1658 return ("Not supported");
1659 case LIBUSB_ERROR_OTHER:
1660 return ("Other error");
1662 return ("Unknown error");
1667 libusb_error_name(int code)
1670 case LIBUSB_SUCCESS:
1671 return ("LIBUSB_SUCCESS");
1672 case LIBUSB_ERROR_IO:
1673 return ("LIBUSB_ERROR_IO");
1674 case LIBUSB_ERROR_INVALID_PARAM:
1675 return ("LIBUSB_ERROR_INVALID_PARAM");
1676 case LIBUSB_ERROR_ACCESS:
1677 return ("LIBUSB_ERROR_ACCESS");
1678 case LIBUSB_ERROR_NO_DEVICE:
1679 return ("LIBUSB_ERROR_NO_DEVICE");
1680 case LIBUSB_ERROR_NOT_FOUND:
1681 return ("LIBUSB_ERROR_NOT_FOUND");
1682 case LIBUSB_ERROR_BUSY:
1683 return ("LIBUSB_ERROR_BUSY");
1684 case LIBUSB_ERROR_TIMEOUT:
1685 return ("LIBUSB_ERROR_TIMEOUT");
1686 case LIBUSB_ERROR_OVERFLOW:
1687 return ("LIBUSB_ERROR_OVERFLOW");
1688 case LIBUSB_ERROR_PIPE:
1689 return ("LIBUSB_ERROR_PIPE");
1690 case LIBUSB_ERROR_INTERRUPTED:
1691 return ("LIBUSB_ERROR_INTERRUPTED");
1692 case LIBUSB_ERROR_NO_MEM:
1693 return ("LIBUSB_ERROR_NO_MEM");
1694 case LIBUSB_ERROR_NOT_SUPPORTED:
1695 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1696 case LIBUSB_ERROR_OTHER:
1697 return ("LIBUSB_ERROR_OTHER");
1699 return ("LIBUSB_ERROR_UNKNOWN");