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 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/queue.h>
40 #define libusb_device_handle libusb20_device
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
48 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
50 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
51 struct libusb_context *usbi_default_context = NULL;
55 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
56 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
57 static int libusb10_convert_error(uint8_t status);
58 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
59 static void libusb10_isoc_proxy(struct libusb20_transfer *);
60 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
61 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
62 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
64 /* Library initialisation / deinitialisation */
67 libusb_set_debug(libusb_context *ctx, int level)
69 ctx = GET_CONTEXT(ctx);
75 libusb_set_nonblocking(int f)
80 * We ignore any failures in this function, hence the
81 * non-blocking flag is not critical to the operation of
82 * libUSB. We use F_GETFL and F_SETFL to be compatible with
86 flags = fcntl(f, F_GETFL, NULL);
90 fcntl(f, F_SETFL, flags);
94 libusb_init(libusb_context **context)
96 struct libusb_context *ctx;
97 pthread_condattr_t attr;
101 ctx = malloc(sizeof(*ctx));
103 return (LIBUSB_ERROR_INVALID_PARAM);
105 memset(ctx, 0, sizeof(*ctx));
107 debug = getenv("LIBUSB_DEBUG");
109 ctx->debug = atoi(debug);
111 ctx->debug_fixed = 1;
113 TAILQ_INIT(&ctx->pollfds);
114 TAILQ_INIT(&ctx->tr_done);
116 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
118 return (LIBUSB_ERROR_NO_MEM);
120 if (pthread_condattr_init(&attr) != 0) {
121 pthread_mutex_destroy(&ctx->ctx_lock);
123 return (LIBUSB_ERROR_NO_MEM);
125 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
126 pthread_mutex_destroy(&ctx->ctx_lock);
127 pthread_condattr_destroy(&attr);
129 return (LIBUSB_ERROR_OTHER);
131 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
132 pthread_mutex_destroy(&ctx->ctx_lock);
133 pthread_condattr_destroy(&attr);
135 return (LIBUSB_ERROR_NO_MEM);
137 pthread_condattr_destroy(&attr);
139 ctx->ctx_handler = NO_THREAD;
141 ret = pipe(ctx->ctrl_pipe);
143 pthread_mutex_destroy(&ctx->ctx_lock);
144 pthread_cond_destroy(&ctx->ctx_cond);
146 return (LIBUSB_ERROR_OTHER);
148 /* set non-blocking mode on the control pipe to avoid deadlock */
149 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
150 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
152 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
154 pthread_mutex_lock(&default_context_lock);
155 if (usbi_default_context == NULL) {
156 usbi_default_context = ctx;
158 pthread_mutex_unlock(&default_context_lock);
163 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
169 libusb_exit(libusb_context *ctx)
171 ctx = GET_CONTEXT(ctx);
176 /* XXX cleanup devices */
178 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
179 close(ctx->ctrl_pipe[0]);
180 close(ctx->ctrl_pipe[1]);
181 pthread_mutex_destroy(&ctx->ctx_lock);
182 pthread_cond_destroy(&ctx->ctx_cond);
184 pthread_mutex_lock(&default_context_lock);
185 if (ctx == usbi_default_context) {
186 usbi_default_context = NULL;
188 pthread_mutex_unlock(&default_context_lock);
193 /* Device handling and initialisation. */
196 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
198 struct libusb20_backend *usb_backend;
199 struct libusb20_device *pdev;
200 struct libusb_device *dev;
203 ctx = GET_CONTEXT(ctx);
206 return (LIBUSB_ERROR_INVALID_PARAM);
209 return (LIBUSB_ERROR_INVALID_PARAM);
211 usb_backend = libusb20_be_alloc_default();
212 if (usb_backend == NULL)
213 return (LIBUSB_ERROR_NO_MEM);
215 /* figure out how many USB devices are present */
218 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
221 /* allocate device pointer list */
222 *list = malloc((i + 1) * sizeof(void *));
224 libusb20_be_free(usb_backend);
225 return (LIBUSB_ERROR_NO_MEM);
227 /* create libusb v1.0 compliant devices */
229 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
231 dev = malloc(sizeof(*dev));
234 libusb_unref_device((*list)[i - 1]);
239 libusb20_be_free(usb_backend);
240 return (LIBUSB_ERROR_NO_MEM);
242 /* get device into libUSB v1.0 list */
243 libusb20_be_dequeue_device(usb_backend, pdev);
245 memset(dev, 0, sizeof(*dev));
247 /* init transfer queues */
248 TAILQ_INIT(&dev->tr_head);
250 /* set context we belong to */
253 /* link together the two structures */
255 pdev->privLuData = dev;
257 (*list)[i] = libusb_ref_device(dev);
262 libusb20_be_free(usb_backend);
267 libusb_free_device_list(libusb_device **list, int unref_devices)
272 return; /* be NULL safe */
275 for (i = 0; list[i] != NULL; i++)
276 libusb_unref_device(list[i]);
282 libusb_get_bus_number(libusb_device *dev)
285 return (0); /* should not happen */
286 return (libusb20_dev_get_bus_number(dev->os_priv));
290 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
292 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
296 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
299 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
303 libusb_get_device_address(libusb_device *dev)
306 return (0); /* should not happen */
307 return (libusb20_dev_get_address(dev->os_priv));
311 libusb_get_device_speed(libusb_device *dev)
314 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
316 switch (libusb20_dev_get_speed(dev->os_priv)) {
317 case LIBUSB20_SPEED_LOW:
318 return (LIBUSB_SPEED_LOW);
319 case LIBUSB20_SPEED_FULL:
320 return (LIBUSB_SPEED_FULL);
321 case LIBUSB20_SPEED_HIGH:
322 return (LIBUSB_SPEED_HIGH);
323 case LIBUSB20_SPEED_SUPER:
324 return (LIBUSB_SPEED_SUPER);
328 return (LIBUSB_SPEED_UNKNOWN);
332 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
334 struct libusb_config_descriptor *pdconf;
335 struct libusb_interface *pinf;
336 struct libusb_interface_descriptor *pdinf;
337 struct libusb_endpoint_descriptor *pdend;
344 return (LIBUSB_ERROR_NO_DEVICE);
346 ret = libusb_get_active_config_descriptor(dev, &pdconf);
350 ret = LIBUSB_ERROR_NOT_FOUND;
351 for (i = 0; i < pdconf->bNumInterfaces; i++) {
352 pinf = &pdconf->interface[i];
353 for (j = 0; j < pinf->num_altsetting; j++) {
354 pdinf = &pinf->altsetting[j];
355 for (k = 0; k < pdinf->bNumEndpoints; k++) {
356 pdend = &pdinf->endpoint[k];
357 if (pdend->bEndpointAddress == endpoint) {
358 ret = pdend->wMaxPacketSize;
366 libusb_free_config_descriptor(pdconf);
371 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
376 ret = libusb_get_max_packet_size(dev, endpoint);
378 switch (libusb20_dev_get_speed(dev->os_priv)) {
379 case LIBUSB20_SPEED_LOW:
380 case LIBUSB20_SPEED_FULL:
384 multiplier = (1 + ((ret >> 11) & 3));
387 ret = (ret & 0x7FF) * multiplier;
395 libusb_ref_device(libusb_device *dev)
398 return (NULL); /* be NULL safe */
402 CTX_UNLOCK(dev->ctx);
408 libusb_unref_device(libusb_device *dev)
411 return; /* be NULL safe */
415 CTX_UNLOCK(dev->ctx);
417 if (dev->refcnt == 0) {
418 libusb20_dev_free(dev->os_priv);
424 libusb_open(libusb_device *dev, libusb_device_handle **devh)
426 libusb_context *ctx = dev->ctx;
427 struct libusb20_device *pdev = dev->os_priv;
432 return (LIBUSB_ERROR_INVALID_PARAM);
434 /* set default device handle value */
437 dev = libusb_ref_device(dev);
439 return (LIBUSB_ERROR_INVALID_PARAM);
441 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
443 libusb_unref_device(dev);
444 return (LIBUSB_ERROR_NO_MEM);
446 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
447 POLLOUT | POLLRDNORM | POLLWRNORM);
449 /* make sure our event loop detects the new device */
451 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
452 if (err < (int)sizeof(dummy)) {
453 /* ignore error, if any */
454 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
461 libusb_device_handle *
462 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
465 struct libusb_device **devs;
466 struct libusb20_device *pdev;
467 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
471 ctx = GET_CONTEXT(ctx);
473 return (NULL); /* be NULL safe */
475 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
477 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
481 for (j = 0; j < i; j++) {
482 struct libusb20_device *tdev;
484 tdev = devs[j]->os_priv;
485 pdesc = libusb20_dev_get_device_desc(tdev);
487 * NOTE: The USB library will automatically swap the
488 * fields in the device descriptor to be of host
491 if (pdesc->idVendor == vendor_id &&
492 pdesc->idProduct == product_id) {
493 libusb_open(devs[j], &pdev);
498 libusb_free_device_list(devs, 1);
499 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
504 libusb_close(struct libusb20_device *pdev)
507 struct libusb_device *dev;
512 return; /* be NULL safe */
514 dev = libusb_get_device(pdev);
517 libusb10_remove_pollfd(ctx, &dev->dev_poll);
519 libusb20_dev_close(pdev);
521 /* unref will free the "pdev" when the refcount reaches zero */
522 libusb_unref_device(dev);
524 /* make sure our event loop detects the closed device */
526 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
527 if (err < (int)sizeof(dummy)) {
528 /* ignore error, if any */
529 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
534 libusb_get_device(struct libusb20_device *pdev)
538 return ((libusb_device *)pdev->privLuData);
542 libusb_get_configuration(struct libusb20_device *pdev, int *config)
544 struct libusb20_config *pconf;
546 if (pdev == NULL || config == NULL)
547 return (LIBUSB_ERROR_INVALID_PARAM);
549 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
551 return (LIBUSB_ERROR_NO_MEM);
553 *config = pconf->desc.bConfigurationValue;
561 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
563 struct libusb20_config *pconf;
564 struct libusb_device *dev;
568 dev = libusb_get_device(pdev);
570 return (LIBUSB_ERROR_INVALID_PARAM);
572 if (configuration < 1) {
576 for (i = 0; i != 255; i++) {
579 pconf = libusb20_dev_alloc_config(pdev, i);
581 return (LIBUSB_ERROR_INVALID_PARAM);
582 found = (pconf->desc.bConfigurationValue
589 return (LIBUSB_ERROR_INVALID_PARAM);
594 libusb10_cancel_all_transfer(dev);
596 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
598 err = libusb20_dev_set_config_index(pdev, i);
600 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
601 POLLOUT | POLLRDNORM | POLLWRNORM);
603 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
607 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
611 dev = libusb_get_device(pdev);
613 return (LIBUSB_ERROR_INVALID_PARAM);
615 if (interface_number < 0 || interface_number > 31)
616 return (LIBUSB_ERROR_INVALID_PARAM);
619 dev->claimed_interfaces |= (1 << interface_number);
620 CTX_UNLOCK(dev->ctx);
626 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
631 dev = libusb_get_device(pdev);
633 return (LIBUSB_ERROR_INVALID_PARAM);
635 if (interface_number < 0 || interface_number > 31)
636 return (LIBUSB_ERROR_INVALID_PARAM);
639 if (!(dev->claimed_interfaces & (1 << interface_number)))
640 err = LIBUSB_ERROR_NOT_FOUND;
643 dev->claimed_interfaces &= ~(1 << interface_number);
644 CTX_UNLOCK(dev->ctx);
649 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
650 int interface_number, int alternate_setting)
655 dev = libusb_get_device(pdev);
657 return (LIBUSB_ERROR_INVALID_PARAM);
659 if (interface_number < 0 || interface_number > 31)
660 return (LIBUSB_ERROR_INVALID_PARAM);
663 if (!(dev->claimed_interfaces & (1 << interface_number)))
664 err = LIBUSB_ERROR_NOT_FOUND;
665 CTX_UNLOCK(dev->ctx);
670 libusb10_cancel_all_transfer(dev);
672 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
674 err = libusb20_dev_set_alt_index(pdev,
675 interface_number, alternate_setting);
677 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
678 pdev, libusb20_dev_get_fd(pdev),
679 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
681 return (err ? LIBUSB_ERROR_OTHER : 0);
684 static struct libusb20_transfer *
685 libusb10_get_transfer(struct libusb20_device *pdev,
686 uint8_t endpoint, uint8_t xfer_index)
688 xfer_index &= 1; /* double buffering */
690 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
692 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
693 /* this is an IN endpoint */
696 return (libusb20_tr_get_pointer(pdev, xfer_index));
700 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
702 struct libusb20_transfer *xfer;
703 struct libusb_device *dev;
706 xfer = libusb10_get_transfer(pdev, endpoint, 0);
708 return (LIBUSB_ERROR_INVALID_PARAM);
710 dev = libusb_get_device(pdev);
712 return (LIBUSB_ERROR_INVALID_PARAM);
715 err = libusb20_tr_open(xfer, 0, 1, endpoint);
716 CTX_UNLOCK(dev->ctx);
718 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
719 return (LIBUSB_ERROR_OTHER);
721 libusb20_tr_clear_stall_sync(xfer);
723 /* check if we opened the transfer */
726 libusb20_tr_close(xfer);
727 CTX_UNLOCK(dev->ctx);
729 return (0); /* success */
733 libusb_reset_device(struct libusb20_device *pdev)
738 dev = libusb_get_device(pdev);
740 return (LIBUSB_ERROR_INVALID_PARAM);
742 libusb10_cancel_all_transfer(dev);
744 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
746 err = libusb20_dev_reset(pdev);
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);
756 libusb_check_connected(struct libusb20_device *pdev)
761 dev = libusb_get_device(pdev);
763 return (LIBUSB_ERROR_INVALID_PARAM);
765 err = libusb20_dev_check_connected(pdev);
767 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
771 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
774 return (LIBUSB_ERROR_INVALID_PARAM);
776 if (libusb20_dev_kernel_driver_active(pdev, interface))
777 return (0); /* no kernel driver is active */
779 return (1); /* kernel driver is active */
783 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
784 char *name, int namelen)
786 return (libusb_get_driver(pdev, interface, name, namelen));
790 libusb_get_driver(struct libusb20_device *pdev, int interface,
791 char *name, int namelen)
797 return (LIBUSB_ERROR_INVALID_PARAM);
799 return (LIBUSB_ERROR_INVALID_PARAM);
803 err = libusb20_dev_get_iface_desc(
804 pdev, interface, name, namelen);
807 return (LIBUSB_ERROR_OTHER);
809 /* we only want the driver name */
810 ptr = strstr(name, ":");
818 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
820 return (libusb_detach_kernel_driver(pdev, interface));
824 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
829 return (LIBUSB_ERROR_INVALID_PARAM);
831 err = libusb20_dev_detach_kernel_driver(
834 return (err ? LIBUSB_ERROR_OTHER : 0);
838 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
841 return (LIBUSB_ERROR_INVALID_PARAM);
842 /* stub - currently not supported by libusb20 */
846 /* Asynchronous device I/O */
848 struct libusb_transfer *
849 libusb_alloc_transfer(int iso_packets)
851 struct libusb_transfer *uxfer;
852 struct libusb_super_transfer *sxfer;
855 len = sizeof(struct libusb_transfer) +
856 sizeof(struct libusb_super_transfer) +
857 (iso_packets * sizeof(libusb_iso_packet_descriptor));
863 memset(sxfer, 0, len);
865 uxfer = (struct libusb_transfer *)(
866 ((uint8_t *)sxfer) + sizeof(*sxfer));
868 /* set default value */
869 uxfer->num_iso_packets = iso_packets;
875 libusb_free_transfer(struct libusb_transfer *uxfer)
877 struct libusb_super_transfer *sxfer;
880 return; /* be NULL safe */
882 /* check if we should free the transfer buffer */
883 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
886 sxfer = (struct libusb_super_transfer *)(
887 (uint8_t *)uxfer - sizeof(*sxfer));
893 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
897 switch (xfer->type) {
898 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
899 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
901 case LIBUSB_TRANSFER_TYPE_CONTROL:
912 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
917 usb_speed = libusb20_dev_get_speed(pdev);
919 switch (xfer->type) {
920 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
921 ret = 0; /* kernel will auto-select */
923 case LIBUSB_TRANSFER_TYPE_CONTROL:
928 case LIBUSB20_SPEED_LOW:
931 case LIBUSB20_SPEED_FULL:
934 case LIBUSB20_SPEED_SUPER:
947 libusb10_convert_error(uint8_t status)
952 case LIBUSB20_TRANSFER_START:
953 case LIBUSB20_TRANSFER_COMPLETED:
954 return (LIBUSB_TRANSFER_COMPLETED);
955 case LIBUSB20_TRANSFER_OVERFLOW:
956 return (LIBUSB_TRANSFER_OVERFLOW);
957 case LIBUSB20_TRANSFER_NO_DEVICE:
958 return (LIBUSB_TRANSFER_NO_DEVICE);
959 case LIBUSB20_TRANSFER_STALL:
960 return (LIBUSB_TRANSFER_STALL);
961 case LIBUSB20_TRANSFER_CANCELLED:
962 return (LIBUSB_TRANSFER_CANCELLED);
963 case LIBUSB20_TRANSFER_TIMED_OUT:
964 return (LIBUSB_TRANSFER_TIMED_OUT);
966 return (LIBUSB_TRANSFER_ERROR);
970 /* This function must be called locked */
973 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
974 struct libusb_super_transfer *sxfer, int status)
976 struct libusb_transfer *uxfer;
977 struct libusb_device *dev;
979 uxfer = (struct libusb_transfer *)(
980 ((uint8_t *)sxfer) + sizeof(*sxfer));
983 libusb20_tr_set_priv_sc1(pxfer, NULL);
985 /* set transfer status */
986 uxfer->status = status;
988 /* update super transfer state */
989 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
991 dev = libusb_get_device(uxfer->dev_handle);
993 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
996 /* This function must be called locked */
999 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1001 struct libusb_super_transfer *sxfer;
1002 struct libusb_transfer *uxfer;
1004 uint16_t iso_packets;
1009 status = libusb20_tr_get_status(pxfer);
1010 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1011 actlen = libusb20_tr_get_actual_length(pxfer);
1012 iso_packets = libusb20_tr_get_max_frames(pxfer);
1015 return; /* cancelled - nothing to do */
1017 uxfer = (struct libusb_transfer *)(
1018 ((uint8_t *)sxfer) + sizeof(*sxfer));
1020 if (iso_packets > uxfer->num_iso_packets)
1021 iso_packets = uxfer->num_iso_packets;
1023 if (iso_packets == 0)
1024 return; /* nothing to do */
1026 /* make sure that the number of ISOCHRONOUS packets is valid */
1027 uxfer->num_iso_packets = iso_packets;
1029 flags = uxfer->flags;
1032 case LIBUSB20_TRANSFER_COMPLETED:
1034 /* update actual length */
1035 uxfer->actual_length = actlen;
1036 for (i = 0; i != iso_packets; i++) {
1037 uxfer->iso_packet_desc[i].actual_length =
1038 libusb20_tr_get_length(pxfer, i);
1040 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1043 case LIBUSB20_TRANSFER_START:
1045 /* setup length(s) */
1047 for (i = 0; i != iso_packets; i++) {
1048 libusb20_tr_setup_isoc(pxfer,
1049 &uxfer->buffer[actlen],
1050 uxfer->iso_packet_desc[i].length, i);
1051 actlen += uxfer->iso_packet_desc[i].length;
1057 libusb20_tr_set_total_frames(pxfer, iso_packets);
1058 libusb20_tr_submit(pxfer);
1060 /* fork another USB transfer, if any */
1061 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1065 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1070 /* This function must be called locked */
1073 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1075 struct libusb_super_transfer *sxfer;
1076 struct libusb_transfer *uxfer;
1082 status = libusb20_tr_get_status(pxfer);
1083 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1084 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1085 actlen = libusb20_tr_get_actual_length(pxfer);
1088 return; /* cancelled - nothing to do */
1090 uxfer = (struct libusb_transfer *)(
1091 ((uint8_t *)sxfer) + sizeof(*sxfer));
1093 flags = uxfer->flags;
1096 case LIBUSB20_TRANSFER_COMPLETED:
1098 uxfer->actual_length += actlen;
1100 /* check for short packet */
1101 if (sxfer->last_len != actlen) {
1102 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1103 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1105 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1109 /* check for end of data */
1110 if (sxfer->rem_len == 0) {
1111 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1116 case LIBUSB20_TRANSFER_START:
1117 if (max_bulk > sxfer->rem_len) {
1118 max_bulk = sxfer->rem_len;
1120 /* setup new BULK or INTERRUPT transaction */
1121 libusb20_tr_setup_bulk(pxfer,
1122 sxfer->curr_data, max_bulk, uxfer->timeout);
1124 /* update counters */
1125 sxfer->last_len = max_bulk;
1126 sxfer->curr_data += max_bulk;
1127 sxfer->rem_len -= max_bulk;
1129 libusb20_tr_submit(pxfer);
1131 /* check if we can fork another USB transfer */
1132 if (sxfer->rem_len == 0)
1133 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1137 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1142 /* This function must be called locked */
1145 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1147 struct libusb_super_transfer *sxfer;
1148 struct libusb_transfer *uxfer;
1154 status = libusb20_tr_get_status(pxfer);
1155 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1156 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1157 actlen = libusb20_tr_get_actual_length(pxfer);
1160 return; /* cancelled - nothing to do */
1162 uxfer = (struct libusb_transfer *)(
1163 ((uint8_t *)sxfer) + sizeof(*sxfer));
1165 flags = uxfer->flags;
1168 case LIBUSB20_TRANSFER_COMPLETED:
1170 uxfer->actual_length += actlen;
1172 /* subtract length of SETUP packet, if any */
1173 actlen -= libusb20_tr_get_length(pxfer, 0);
1175 /* check for short packet */
1176 if (sxfer->last_len != actlen) {
1177 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1178 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1180 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1184 /* check for end of data */
1185 if (sxfer->rem_len == 0) {
1186 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1191 case LIBUSB20_TRANSFER_START:
1192 if (max_bulk > sxfer->rem_len) {
1193 max_bulk = sxfer->rem_len;
1195 /* setup new CONTROL transaction */
1196 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1197 /* next fragment - don't send SETUP packet */
1198 libusb20_tr_set_length(pxfer, 0, 0);
1200 /* first fragment - send SETUP packet */
1201 libusb20_tr_set_length(pxfer, 8, 0);
1202 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1205 if (max_bulk != 0) {
1206 libusb20_tr_set_length(pxfer, max_bulk, 1);
1207 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1208 libusb20_tr_set_total_frames(pxfer, 2);
1210 libusb20_tr_set_total_frames(pxfer, 1);
1213 /* update counters */
1214 sxfer->last_len = max_bulk;
1215 sxfer->curr_data += max_bulk;
1216 sxfer->rem_len -= max_bulk;
1218 libusb20_tr_submit(pxfer);
1220 /* check if we can fork another USB transfer */
1221 if (sxfer->rem_len == 0)
1222 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1226 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1231 /* The following function must be called locked */
1234 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1236 struct libusb20_transfer *pxfer0;
1237 struct libusb20_transfer *pxfer1;
1238 struct libusb_super_transfer *sxfer;
1239 struct libusb_transfer *uxfer;
1240 struct libusb_device *dev;
1247 dev = libusb_get_device(pdev);
1249 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1250 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1252 if (pxfer0 == NULL || pxfer1 == NULL)
1253 return; /* shouldn't happen */
1256 if (libusb20_tr_pending(pxfer0))
1258 if (libusb20_tr_pending(pxfer1))
1263 /* wait till one of the transfers complete */
1266 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1268 return; /* cancelling */
1270 return; /* cannot queue another one */
1271 /* swap transfers */
1275 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1277 return; /* cancelling */
1279 return; /* cannot queue another one */
1280 /* swap transfers */
1287 /* find next transfer on same endpoint */
1288 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1290 uxfer = (struct libusb_transfer *)(
1291 ((uint8_t *)sxfer) + sizeof(*sxfer));
1293 if (uxfer->endpoint == endpoint) {
1294 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1295 sxfer->entry.tqe_prev = NULL;
1299 return; /* success */
1303 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1304 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1306 /* reset super transfer state */
1307 sxfer->rem_len = uxfer->length;
1308 sxfer->curr_data = uxfer->buffer;
1309 uxfer->actual_length = 0;
1311 switch (uxfer->type) {
1312 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1313 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1315 case LIBUSB_TRANSFER_TYPE_BULK:
1316 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1317 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1319 case LIBUSB_TRANSFER_TYPE_CONTROL:
1320 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1321 if (sxfer->rem_len < 8)
1324 /* remove SETUP packet from data */
1325 sxfer->rem_len -= 8;
1326 sxfer->curr_data += 8;
1332 buffsize = libusb10_get_buffsize(pdev, uxfer);
1333 maxframe = libusb10_get_maxframe(pdev, uxfer);
1335 /* make sure the transfer is opened */
1336 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1337 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1340 libusb20_tr_start(pxfer0);
1344 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1346 /* make sure our event loop spins the done handler */
1348 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1351 /* The following function must be called unlocked */
1354 libusb_submit_transfer(struct libusb_transfer *uxfer)
1356 struct libusb20_transfer *pxfer0;
1357 struct libusb20_transfer *pxfer1;
1358 struct libusb_super_transfer *sxfer;
1359 struct libusb_device *dev;
1364 return (LIBUSB_ERROR_INVALID_PARAM);
1366 if (uxfer->dev_handle == NULL)
1367 return (LIBUSB_ERROR_INVALID_PARAM);
1369 endpoint = uxfer->endpoint;
1372 return (LIBUSB_ERROR_INVALID_PARAM);
1374 dev = libusb_get_device(uxfer->dev_handle);
1376 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1378 sxfer = (struct libusb_super_transfer *)(
1379 (uint8_t *)uxfer - sizeof(*sxfer));
1383 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1384 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1386 if (pxfer0 == NULL || pxfer1 == NULL) {
1387 err = LIBUSB_ERROR_OTHER;
1388 } else if ((sxfer->entry.tqe_prev != NULL) ||
1389 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1390 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1391 err = LIBUSB_ERROR_BUSY;
1394 /* set pending state */
1395 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1397 /* insert transfer into transfer head list */
1398 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1400 /* start work transfers */
1401 libusb10_submit_transfer_sub(
1402 uxfer->dev_handle, endpoint);
1404 err = 0; /* success */
1407 CTX_UNLOCK(dev->ctx);
1409 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1414 /* Asynchronous transfer cancel */
1417 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1419 struct libusb20_transfer *pxfer0;
1420 struct libusb20_transfer *pxfer1;
1421 struct libusb_super_transfer *sxfer;
1422 struct libusb_device *dev;
1427 return (LIBUSB_ERROR_INVALID_PARAM);
1429 /* check if not initialised */
1430 if (uxfer->dev_handle == NULL)
1431 return (LIBUSB_ERROR_NOT_FOUND);
1433 endpoint = uxfer->endpoint;
1436 return (LIBUSB_ERROR_INVALID_PARAM);
1438 dev = libusb_get_device(uxfer->dev_handle);
1440 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1442 sxfer = (struct libusb_super_transfer *)(
1443 (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 (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1453 /* only update the transfer status */
1454 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1455 retval = LIBUSB_ERROR_NOT_FOUND;
1456 } else if (sxfer->entry.tqe_prev != NULL) {
1457 /* we are lucky - transfer is on a queue */
1458 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1459 sxfer->entry.tqe_prev = NULL;
1460 libusb10_complete_transfer(NULL,
1461 sxfer, LIBUSB_TRANSFER_CANCELLED);
1462 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1464 retval = LIBUSB_ERROR_NOT_FOUND;
1465 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1466 libusb10_complete_transfer(pxfer0,
1467 sxfer, LIBUSB_TRANSFER_CANCELLED);
1468 libusb20_tr_stop(pxfer0);
1469 /* make sure the queue doesn't stall */
1470 libusb10_submit_transfer_sub(
1471 uxfer->dev_handle, endpoint);
1472 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1473 libusb10_complete_transfer(pxfer1,
1474 sxfer, LIBUSB_TRANSFER_CANCELLED);
1475 libusb20_tr_stop(pxfer1);
1476 /* make sure the queue doesn't stall */
1477 libusb10_submit_transfer_sub(
1478 uxfer->dev_handle, endpoint);
1481 retval = LIBUSB_ERROR_NOT_FOUND;
1484 CTX_UNLOCK(dev->ctx);
1486 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1492 libusb10_cancel_all_transfer(libusb_device *dev)
1494 struct libusb20_device *pdev = dev->os_priv;
1497 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1498 struct libusb20_transfer *xfer;
1500 xfer = libusb20_tr_get_pointer(pdev, x);
1503 libusb20_tr_close(xfer);
1508 libusb_cpu_to_le16(uint16_t x)
1510 return (htole16(x));
1514 libusb_le16_to_cpu(uint16_t x)
1516 return (le16toh(x));
1520 libusb_strerror(int code)
1523 case LIBUSB_SUCCESS:
1525 case LIBUSB_ERROR_IO:
1526 return ("I/O error");
1527 case LIBUSB_ERROR_INVALID_PARAM:
1528 return ("Invalid parameter");
1529 case LIBUSB_ERROR_ACCESS:
1530 return ("Permissions error");
1531 case LIBUSB_ERROR_NO_DEVICE:
1532 return ("No device");
1533 case LIBUSB_ERROR_NOT_FOUND:
1534 return ("Not found");
1535 case LIBUSB_ERROR_BUSY:
1536 return ("Device busy");
1537 case LIBUSB_ERROR_TIMEOUT:
1539 case LIBUSB_ERROR_OVERFLOW:
1540 return ("Overflow");
1541 case LIBUSB_ERROR_PIPE:
1542 return ("Pipe error");
1543 case LIBUSB_ERROR_INTERRUPTED:
1544 return ("Interrupted");
1545 case LIBUSB_ERROR_NO_MEM:
1546 return ("Out of memory");
1547 case LIBUSB_ERROR_NOT_SUPPORTED:
1548 return ("Not supported");
1549 case LIBUSB_ERROR_OTHER:
1550 return ("Other error");
1552 return ("Unknown error");
1557 libusb_error_name(int code)
1560 case LIBUSB_SUCCESS:
1561 return ("LIBUSB_SUCCESS");
1562 case LIBUSB_ERROR_IO:
1563 return ("LIBUSB_ERROR_IO");
1564 case LIBUSB_ERROR_INVALID_PARAM:
1565 return ("LIBUSB_ERROR_INVALID_PARAM");
1566 case LIBUSB_ERROR_ACCESS:
1567 return ("LIBUSB_ERROR_ACCESS");
1568 case LIBUSB_ERROR_NO_DEVICE:
1569 return ("LIBUSB_ERROR_NO_DEVICE");
1570 case LIBUSB_ERROR_NOT_FOUND:
1571 return ("LIBUSB_ERROR_NOT_FOUND");
1572 case LIBUSB_ERROR_BUSY:
1573 return ("LIBUSB_ERROR_BUSY");
1574 case LIBUSB_ERROR_TIMEOUT:
1575 return ("LIBUSB_ERROR_TIMEOUT");
1576 case LIBUSB_ERROR_OVERFLOW:
1577 return ("LIBUSB_ERROR_OVERFLOW");
1578 case LIBUSB_ERROR_PIPE:
1579 return ("LIBUSB_ERROR_PIPE");
1580 case LIBUSB_ERROR_INTERRUPTED:
1581 return ("LIBUSB_ERROR_INTERRUPTED");
1582 case LIBUSB_ERROR_NO_MEM:
1583 return ("LIBUSB_ERROR_NO_MEM");
1584 case LIBUSB_ERROR_NOT_SUPPORTED:
1585 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1586 case LIBUSB_ERROR_OTHER:
1587 return ("LIBUSB_ERROR_OTHER");
1589 return ("LIBUSB_ERROR_UNKNOWN");