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 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
55 struct libusb_context *usbi_default_context = NULL;
59 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
60 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
61 static int libusb10_convert_error(uint8_t status);
62 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
63 static void libusb10_isoc_proxy(struct libusb20_transfer *);
64 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
65 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
66 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
68 /* Library initialisation / deinitialisation */
71 libusb_set_debug(libusb_context *ctx, int level)
73 ctx = GET_CONTEXT(ctx);
79 libusb_set_nonblocking(int f)
84 * We ignore any failures in this function, hence the
85 * non-blocking flag is not critical to the operation of
86 * libUSB. We use F_GETFL and F_SETFL to be compatible with
90 flags = fcntl(f, F_GETFL, NULL);
94 fcntl(f, F_SETFL, flags);
98 libusb_init(libusb_context **context)
100 struct libusb_context *ctx;
101 pthread_condattr_t attr;
105 ctx = malloc(sizeof(*ctx));
107 return (LIBUSB_ERROR_INVALID_PARAM);
109 memset(ctx, 0, sizeof(*ctx));
111 debug = getenv("LIBUSB_DEBUG");
113 ctx->debug = atoi(debug);
115 ctx->debug_fixed = 1;
117 TAILQ_INIT(&ctx->pollfds);
118 TAILQ_INIT(&ctx->tr_done);
120 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
122 return (LIBUSB_ERROR_NO_MEM);
124 if (pthread_condattr_init(&attr) != 0) {
125 pthread_mutex_destroy(&ctx->ctx_lock);
127 return (LIBUSB_ERROR_NO_MEM);
129 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
130 pthread_mutex_destroy(&ctx->ctx_lock);
131 pthread_condattr_destroy(&attr);
133 return (LIBUSB_ERROR_OTHER);
135 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
136 pthread_mutex_destroy(&ctx->ctx_lock);
137 pthread_condattr_destroy(&attr);
139 return (LIBUSB_ERROR_NO_MEM);
141 pthread_condattr_destroy(&attr);
143 ctx->ctx_handler = NO_THREAD;
145 ret = pipe(ctx->ctrl_pipe);
147 pthread_mutex_destroy(&ctx->ctx_lock);
148 pthread_cond_destroy(&ctx->ctx_cond);
150 return (LIBUSB_ERROR_OTHER);
152 /* set non-blocking mode on the control pipe to avoid deadlock */
153 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
154 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
156 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
158 pthread_mutex_lock(&default_context_lock);
159 if (usbi_default_context == NULL) {
160 usbi_default_context = ctx;
162 pthread_mutex_unlock(&default_context_lock);
167 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
173 libusb_exit(libusb_context *ctx)
175 ctx = GET_CONTEXT(ctx);
180 /* XXX cleanup devices */
182 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
183 close(ctx->ctrl_pipe[0]);
184 close(ctx->ctrl_pipe[1]);
185 pthread_mutex_destroy(&ctx->ctx_lock);
186 pthread_cond_destroy(&ctx->ctx_cond);
188 pthread_mutex_lock(&default_context_lock);
189 if (ctx == usbi_default_context) {
190 usbi_default_context = NULL;
192 pthread_mutex_unlock(&default_context_lock);
197 /* Device handling and initialisation. */
200 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
202 struct libusb20_backend *usb_backend;
203 struct libusb20_device *pdev;
204 struct libusb_device *dev;
207 ctx = GET_CONTEXT(ctx);
210 return (LIBUSB_ERROR_INVALID_PARAM);
213 return (LIBUSB_ERROR_INVALID_PARAM);
215 usb_backend = libusb20_be_alloc_default();
216 if (usb_backend == NULL)
217 return (LIBUSB_ERROR_NO_MEM);
219 /* figure out how many USB devices are present */
222 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
225 /* allocate device pointer list */
226 *list = malloc((i + 1) * sizeof(void *));
228 libusb20_be_free(usb_backend);
229 return (LIBUSB_ERROR_NO_MEM);
231 /* create libusb v1.0 compliant devices */
233 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
235 dev = malloc(sizeof(*dev));
238 libusb_unref_device((*list)[i - 1]);
243 libusb20_be_free(usb_backend);
244 return (LIBUSB_ERROR_NO_MEM);
246 /* get device into libUSB v1.0 list */
247 libusb20_be_dequeue_device(usb_backend, pdev);
249 memset(dev, 0, sizeof(*dev));
251 /* init transfer queues */
252 TAILQ_INIT(&dev->tr_head);
254 /* set context we belong to */
257 /* link together the two structures */
259 pdev->privLuData = dev;
261 (*list)[i] = libusb_ref_device(dev);
266 libusb20_be_free(usb_backend);
271 libusb_free_device_list(libusb_device **list, int unref_devices)
276 return; /* be NULL safe */
279 for (i = 0; list[i] != NULL; i++)
280 libusb_unref_device(list[i]);
286 libusb_get_bus_number(libusb_device *dev)
289 return (0); /* should not happen */
290 return (libusb20_dev_get_bus_number(dev->os_priv));
294 libusb_get_device_address(libusb_device *dev)
297 return (0); /* should not happen */
298 return (libusb20_dev_get_address(dev->os_priv));
302 libusb_get_device_speed(libusb_device *dev)
305 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
307 switch (libusb20_dev_get_speed(dev->os_priv)) {
308 case LIBUSB20_SPEED_LOW:
309 return (LIBUSB_SPEED_LOW);
310 case LIBUSB20_SPEED_FULL:
311 return (LIBUSB_SPEED_FULL);
312 case LIBUSB20_SPEED_HIGH:
313 return (LIBUSB_SPEED_HIGH);
314 case LIBUSB20_SPEED_SUPER:
315 return (LIBUSB_SPEED_SUPER);
319 return (LIBUSB_SPEED_UNKNOWN);
323 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
325 struct libusb_config_descriptor *pdconf;
326 struct libusb_interface *pinf;
327 struct libusb_interface_descriptor *pdinf;
328 struct libusb_endpoint_descriptor *pdend;
335 return (LIBUSB_ERROR_NO_DEVICE);
337 ret = libusb_get_active_config_descriptor(dev, &pdconf);
341 ret = LIBUSB_ERROR_NOT_FOUND;
342 for (i = 0; i < pdconf->bNumInterfaces; i++) {
343 pinf = &pdconf->interface[i];
344 for (j = 0; j < pinf->num_altsetting; j++) {
345 pdinf = &pinf->altsetting[j];
346 for (k = 0; k < pdinf->bNumEndpoints; k++) {
347 pdend = &pdinf->endpoint[k];
348 if (pdend->bEndpointAddress == endpoint) {
349 ret = pdend->wMaxPacketSize;
357 libusb_free_config_descriptor(pdconf);
362 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
367 ret = libusb_get_max_packet_size(dev, endpoint);
369 switch (libusb20_dev_get_speed(dev->os_priv)) {
370 case LIBUSB20_SPEED_LOW:
371 case LIBUSB20_SPEED_FULL:
375 multiplier = (1 + ((ret >> 11) & 3));
378 ret = (ret & 0x7FF) * multiplier;
386 libusb_ref_device(libusb_device *dev)
389 return (NULL); /* be NULL safe */
393 CTX_UNLOCK(dev->ctx);
399 libusb_unref_device(libusb_device *dev)
402 return; /* be NULL safe */
406 CTX_UNLOCK(dev->ctx);
408 if (dev->refcnt == 0) {
409 libusb20_dev_free(dev->os_priv);
415 libusb_open(libusb_device *dev, libusb_device_handle **devh)
417 libusb_context *ctx = dev->ctx;
418 struct libusb20_device *pdev = dev->os_priv;
423 return (LIBUSB_ERROR_INVALID_PARAM);
425 /* set default device handle value */
428 dev = libusb_ref_device(dev);
430 return (LIBUSB_ERROR_INVALID_PARAM);
432 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
434 libusb_unref_device(dev);
435 return (LIBUSB_ERROR_NO_MEM);
437 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
438 POLLOUT | POLLRDNORM | POLLWRNORM);
440 /* make sure our event loop detects the new device */
442 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
443 if (err < (int)sizeof(dummy)) {
444 /* ignore error, if any */
445 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
452 libusb_device_handle *
453 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
456 struct libusb_device **devs;
457 struct libusb20_device *pdev;
458 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
462 ctx = GET_CONTEXT(ctx);
464 return (NULL); /* be NULL safe */
466 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
468 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
472 for (j = 0; j < i; j++) {
473 struct libusb20_device *tdev;
475 tdev = devs[j]->os_priv;
476 pdesc = libusb20_dev_get_device_desc(tdev);
478 * NOTE: The USB library will automatically swap the
479 * fields in the device descriptor to be of host
482 if (pdesc->idVendor == vendor_id &&
483 pdesc->idProduct == product_id) {
484 libusb_open(devs[j], &pdev);
489 libusb_free_device_list(devs, 1);
490 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
495 libusb_close(struct libusb20_device *pdev)
498 struct libusb_device *dev;
503 return; /* be NULL safe */
505 dev = libusb_get_device(pdev);
508 libusb10_remove_pollfd(ctx, &dev->dev_poll);
510 libusb20_dev_close(pdev);
512 /* unref will free the "pdev" when the refcount reaches zero */
513 libusb_unref_device(dev);
515 /* make sure our event loop detects the closed device */
517 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
518 if (err < (int)sizeof(dummy)) {
519 /* ignore error, if any */
520 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
525 libusb_get_device(struct libusb20_device *pdev)
529 return ((libusb_device *)pdev->privLuData);
533 libusb_get_configuration(struct libusb20_device *pdev, int *config)
535 struct libusb20_config *pconf;
537 if (pdev == NULL || config == NULL)
538 return (LIBUSB_ERROR_INVALID_PARAM);
540 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
542 return (LIBUSB_ERROR_NO_MEM);
544 *config = pconf->desc.bConfigurationValue;
552 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
554 struct libusb20_config *pconf;
555 struct libusb_device *dev;
559 dev = libusb_get_device(pdev);
561 return (LIBUSB_ERROR_INVALID_PARAM);
563 if (configuration < 1) {
567 for (i = 0; i != 255; i++) {
570 pconf = libusb20_dev_alloc_config(pdev, i);
572 return (LIBUSB_ERROR_INVALID_PARAM);
573 found = (pconf->desc.bConfigurationValue
580 return (LIBUSB_ERROR_INVALID_PARAM);
585 libusb10_cancel_all_transfer(dev);
587 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
589 err = libusb20_dev_set_config_index(pdev, i);
591 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
592 POLLOUT | POLLRDNORM | POLLWRNORM);
594 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
598 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
603 dev = libusb_get_device(pdev);
605 return (LIBUSB_ERROR_INVALID_PARAM);
607 if (interface_number < 0 || interface_number > 31)
608 return (LIBUSB_ERROR_INVALID_PARAM);
611 if (dev->claimed_interfaces & (1 << interface_number))
612 err = LIBUSB_ERROR_BUSY;
615 dev->claimed_interfaces |= (1 << interface_number);
616 CTX_UNLOCK(dev->ctx);
621 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
626 dev = libusb_get_device(pdev);
628 return (LIBUSB_ERROR_INVALID_PARAM);
630 if (interface_number < 0 || interface_number > 31)
631 return (LIBUSB_ERROR_INVALID_PARAM);
634 if (!(dev->claimed_interfaces & (1 << interface_number)))
635 err = LIBUSB_ERROR_NOT_FOUND;
638 dev->claimed_interfaces &= ~(1 << interface_number);
639 CTX_UNLOCK(dev->ctx);
644 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
645 int interface_number, int alternate_setting)
650 dev = libusb_get_device(pdev);
652 return (LIBUSB_ERROR_INVALID_PARAM);
654 if (interface_number < 0 || interface_number > 31)
655 return (LIBUSB_ERROR_INVALID_PARAM);
658 if (!(dev->claimed_interfaces & (1 << interface_number)))
659 err = LIBUSB_ERROR_NOT_FOUND;
660 CTX_UNLOCK(dev->ctx);
665 libusb10_cancel_all_transfer(dev);
667 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
669 err = libusb20_dev_set_alt_index(pdev,
670 interface_number, alternate_setting);
672 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
673 pdev, libusb20_dev_get_fd(pdev),
674 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
676 return (err ? LIBUSB_ERROR_OTHER : 0);
679 static struct libusb20_transfer *
680 libusb10_get_transfer(struct libusb20_device *pdev,
681 uint8_t endpoint, uint8_t xfer_index)
683 xfer_index &= 1; /* double buffering */
685 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
687 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
688 /* this is an IN endpoint */
691 return (libusb20_tr_get_pointer(pdev, xfer_index));
695 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
697 struct libusb20_transfer *xfer;
698 struct libusb_device *dev;
701 xfer = libusb10_get_transfer(pdev, endpoint, 0);
703 return (LIBUSB_ERROR_INVALID_PARAM);
705 dev = libusb_get_device(pdev);
707 return (LIBUSB_ERROR_INVALID_PARAM);
710 err = libusb20_tr_open(xfer, 0, 1, endpoint);
711 CTX_UNLOCK(dev->ctx);
713 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
714 return (LIBUSB_ERROR_OTHER);
716 libusb20_tr_clear_stall_sync(xfer);
718 /* check if we opened the transfer */
721 libusb20_tr_close(xfer);
722 CTX_UNLOCK(dev->ctx);
724 return (0); /* success */
728 libusb_reset_device(struct libusb20_device *pdev)
733 dev = libusb_get_device(pdev);
735 return (LIBUSB_ERROR_INVALID_PARAM);
737 libusb10_cancel_all_transfer(dev);
739 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
741 err = libusb20_dev_reset(pdev);
743 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
744 pdev, libusb20_dev_get_fd(pdev),
745 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
747 return (err ? LIBUSB_ERROR_OTHER : 0);
751 libusb_check_connected(struct libusb20_device *pdev)
756 dev = libusb_get_device(pdev);
758 return (LIBUSB_ERROR_INVALID_PARAM);
760 err = libusb20_dev_check_connected(pdev);
762 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
766 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
769 return (LIBUSB_ERROR_INVALID_PARAM);
771 if (libusb20_dev_kernel_driver_active(pdev, interface))
772 return (0); /* no kernel driver is active */
774 return (1); /* kernel driver is active */
778 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
779 char *name, int namelen)
781 return (libusb_get_driver(pdev, interface, name, namelen));
785 libusb_get_driver(struct libusb20_device *pdev, int interface,
786 char *name, int namelen)
792 return (LIBUSB_ERROR_INVALID_PARAM);
794 return (LIBUSB_ERROR_INVALID_PARAM);
798 err = libusb20_dev_get_iface_desc(
799 pdev, interface, name, namelen);
802 return (LIBUSB_ERROR_OTHER);
804 /* we only want the driver name */
805 ptr = strstr(name, ":");
813 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
815 return (libusb_detach_kernel_driver(pdev, interface));
819 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
824 return (LIBUSB_ERROR_INVALID_PARAM);
826 err = libusb20_dev_detach_kernel_driver(
829 return (err ? LIBUSB_ERROR_OTHER : 0);
833 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
836 return (LIBUSB_ERROR_INVALID_PARAM);
837 /* stub - currently not supported by libusb20 */
841 /* Asynchronous device I/O */
843 struct libusb_transfer *
844 libusb_alloc_transfer(int iso_packets)
846 struct libusb_transfer *uxfer;
847 struct libusb_super_transfer *sxfer;
850 len = sizeof(struct libusb_transfer) +
851 sizeof(struct libusb_super_transfer) +
852 (iso_packets * sizeof(libusb_iso_packet_descriptor));
858 memset(sxfer, 0, len);
860 uxfer = (struct libusb_transfer *)(
861 ((uint8_t *)sxfer) + sizeof(*sxfer));
863 /* set default value */
864 uxfer->num_iso_packets = iso_packets;
870 libusb_free_transfer(struct libusb_transfer *uxfer)
872 struct libusb_super_transfer *sxfer;
875 return; /* be NULL safe */
877 /* check if we should free the transfer buffer */
878 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
881 sxfer = (struct libusb_super_transfer *)(
882 (uint8_t *)uxfer - sizeof(*sxfer));
888 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
892 switch (xfer->type) {
893 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
894 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
896 case LIBUSB_TRANSFER_TYPE_CONTROL:
907 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
912 usb_speed = libusb20_dev_get_speed(pdev);
914 switch (xfer->type) {
915 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
916 ret = 0; /* kernel will auto-select */
918 case LIBUSB_TRANSFER_TYPE_CONTROL:
923 case LIBUSB20_SPEED_LOW:
926 case LIBUSB20_SPEED_FULL:
939 libusb10_convert_error(uint8_t status)
944 case LIBUSB20_TRANSFER_START:
945 case LIBUSB20_TRANSFER_COMPLETED:
946 return (LIBUSB_TRANSFER_COMPLETED);
947 case LIBUSB20_TRANSFER_OVERFLOW:
948 return (LIBUSB_TRANSFER_OVERFLOW);
949 case LIBUSB20_TRANSFER_NO_DEVICE:
950 return (LIBUSB_TRANSFER_NO_DEVICE);
951 case LIBUSB20_TRANSFER_STALL:
952 return (LIBUSB_TRANSFER_STALL);
953 case LIBUSB20_TRANSFER_CANCELLED:
954 return (LIBUSB_TRANSFER_CANCELLED);
955 case LIBUSB20_TRANSFER_TIMED_OUT:
956 return (LIBUSB_TRANSFER_TIMED_OUT);
958 return (LIBUSB_TRANSFER_ERROR);
962 /* This function must be called locked */
965 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
966 struct libusb_super_transfer *sxfer, int status)
968 struct libusb_transfer *uxfer;
969 struct libusb_device *dev;
971 uxfer = (struct libusb_transfer *)(
972 ((uint8_t *)sxfer) + sizeof(*sxfer));
975 libusb20_tr_set_priv_sc1(pxfer, NULL);
977 /* set transfer status */
978 uxfer->status = status;
980 /* update super transfer state */
981 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
983 dev = libusb_get_device(uxfer->dev_handle);
985 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
988 /* This function must be called locked */
991 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
993 struct libusb_super_transfer *sxfer;
994 struct libusb_transfer *uxfer;
996 uint16_t iso_packets;
1001 status = libusb20_tr_get_status(pxfer);
1002 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1003 actlen = libusb20_tr_get_actual_length(pxfer);
1004 iso_packets = libusb20_tr_get_max_frames(pxfer);
1007 return; /* cancelled - nothing to do */
1009 uxfer = (struct libusb_transfer *)(
1010 ((uint8_t *)sxfer) + sizeof(*sxfer));
1012 if (iso_packets > uxfer->num_iso_packets)
1013 iso_packets = uxfer->num_iso_packets;
1015 if (iso_packets == 0)
1016 return; /* nothing to do */
1018 /* make sure that the number of ISOCHRONOUS packets is valid */
1019 uxfer->num_iso_packets = iso_packets;
1021 flags = uxfer->flags;
1024 case LIBUSB20_TRANSFER_COMPLETED:
1026 /* update actual length */
1027 uxfer->actual_length = actlen;
1028 for (i = 0; i != iso_packets; i++) {
1029 uxfer->iso_packet_desc[i].actual_length =
1030 libusb20_tr_get_length(pxfer, i);
1032 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1035 case LIBUSB20_TRANSFER_START:
1037 /* setup length(s) */
1039 for (i = 0; i != iso_packets; i++) {
1040 libusb20_tr_setup_isoc(pxfer,
1041 &uxfer->buffer[actlen],
1042 uxfer->iso_packet_desc[i].length, i);
1043 actlen += uxfer->iso_packet_desc[i].length;
1049 libusb20_tr_set_total_frames(pxfer, iso_packets);
1050 libusb20_tr_submit(pxfer);
1052 /* fork another USB transfer, if any */
1053 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1057 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1062 /* This function must be called locked */
1065 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1067 struct libusb_super_transfer *sxfer;
1068 struct libusb_transfer *uxfer;
1074 status = libusb20_tr_get_status(pxfer);
1075 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1076 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1077 actlen = libusb20_tr_get_actual_length(pxfer);
1080 return; /* cancelled - nothing to do */
1082 uxfer = (struct libusb_transfer *)(
1083 ((uint8_t *)sxfer) + sizeof(*sxfer));
1085 flags = uxfer->flags;
1088 case LIBUSB20_TRANSFER_COMPLETED:
1090 uxfer->actual_length += actlen;
1092 /* check for short packet */
1093 if (sxfer->last_len != actlen) {
1094 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1095 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1097 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1101 /* check for end of data */
1102 if (sxfer->rem_len == 0) {
1103 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1108 case LIBUSB20_TRANSFER_START:
1109 if (max_bulk > sxfer->rem_len) {
1110 max_bulk = sxfer->rem_len;
1112 /* setup new BULK or INTERRUPT transaction */
1113 libusb20_tr_setup_bulk(pxfer,
1114 sxfer->curr_data, max_bulk, uxfer->timeout);
1116 /* update counters */
1117 sxfer->last_len = max_bulk;
1118 sxfer->curr_data += max_bulk;
1119 sxfer->rem_len -= max_bulk;
1121 libusb20_tr_submit(pxfer);
1123 /* check if we can fork another USB transfer */
1124 if (sxfer->rem_len == 0)
1125 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1129 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1134 /* This function must be called locked */
1137 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1139 struct libusb_super_transfer *sxfer;
1140 struct libusb_transfer *uxfer;
1146 status = libusb20_tr_get_status(pxfer);
1147 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1148 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1149 actlen = libusb20_tr_get_actual_length(pxfer);
1152 return; /* cancelled - nothing to do */
1154 uxfer = (struct libusb_transfer *)(
1155 ((uint8_t *)sxfer) + sizeof(*sxfer));
1157 flags = uxfer->flags;
1160 case LIBUSB20_TRANSFER_COMPLETED:
1162 uxfer->actual_length += actlen;
1164 /* subtract length of SETUP packet, if any */
1165 actlen -= libusb20_tr_get_length(pxfer, 0);
1167 /* check for short packet */
1168 if (sxfer->last_len != actlen) {
1169 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1170 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1172 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1176 /* check for end of data */
1177 if (sxfer->rem_len == 0) {
1178 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1183 case LIBUSB20_TRANSFER_START:
1184 if (max_bulk > sxfer->rem_len) {
1185 max_bulk = sxfer->rem_len;
1187 /* setup new CONTROL transaction */
1188 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1189 /* next fragment - don't send SETUP packet */
1190 libusb20_tr_set_length(pxfer, 0, 0);
1192 /* first fragment - send SETUP packet */
1193 libusb20_tr_set_length(pxfer, 8, 0);
1194 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1197 if (max_bulk != 0) {
1198 libusb20_tr_set_length(pxfer, max_bulk, 1);
1199 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1200 libusb20_tr_set_total_frames(pxfer, 2);
1202 libusb20_tr_set_total_frames(pxfer, 1);
1205 /* update counters */
1206 sxfer->last_len = max_bulk;
1207 sxfer->curr_data += max_bulk;
1208 sxfer->rem_len -= max_bulk;
1210 libusb20_tr_submit(pxfer);
1212 /* check if we can fork another USB transfer */
1213 if (sxfer->rem_len == 0)
1214 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1218 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1223 /* The following function must be called locked */
1226 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1228 struct libusb20_transfer *pxfer0;
1229 struct libusb20_transfer *pxfer1;
1230 struct libusb_super_transfer *sxfer;
1231 struct libusb_transfer *uxfer;
1232 struct libusb_device *dev;
1239 dev = libusb_get_device(pdev);
1241 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1242 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1244 if (pxfer0 == NULL || pxfer1 == NULL)
1245 return; /* shouldn't happen */
1248 if (libusb20_tr_pending(pxfer0))
1250 if (libusb20_tr_pending(pxfer1))
1255 /* wait till one of the transfers complete */
1258 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1260 return; /* cancelling */
1262 return; /* cannot queue another one */
1263 /* swap transfers */
1267 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1269 return; /* cancelling */
1271 return; /* cannot queue another one */
1272 /* swap transfers */
1279 /* find next transfer on same endpoint */
1280 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1282 uxfer = (struct libusb_transfer *)(
1283 ((uint8_t *)sxfer) + sizeof(*sxfer));
1285 if (uxfer->endpoint == endpoint) {
1286 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1287 sxfer->entry.tqe_prev = NULL;
1291 return; /* success */
1295 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1296 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1298 /* reset super transfer state */
1299 sxfer->rem_len = uxfer->length;
1300 sxfer->curr_data = uxfer->buffer;
1301 uxfer->actual_length = 0;
1303 switch (uxfer->type) {
1304 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1305 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1307 case LIBUSB_TRANSFER_TYPE_BULK:
1308 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1309 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1311 case LIBUSB_TRANSFER_TYPE_CONTROL:
1312 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1313 if (sxfer->rem_len < 8)
1316 /* remove SETUP packet from data */
1317 sxfer->rem_len -= 8;
1318 sxfer->curr_data += 8;
1324 buffsize = libusb10_get_buffsize(pdev, uxfer);
1325 maxframe = libusb10_get_maxframe(pdev, uxfer);
1327 /* make sure the transfer is opened */
1328 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1329 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1332 libusb20_tr_start(pxfer0);
1336 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1338 /* make sure our event loop spins the done handler */
1340 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1343 /* The following function must be called unlocked */
1346 libusb_submit_transfer(struct libusb_transfer *uxfer)
1348 struct libusb20_transfer *pxfer0;
1349 struct libusb20_transfer *pxfer1;
1350 struct libusb_super_transfer *sxfer;
1351 struct libusb_device *dev;
1356 return (LIBUSB_ERROR_INVALID_PARAM);
1358 if (uxfer->dev_handle == NULL)
1359 return (LIBUSB_ERROR_INVALID_PARAM);
1361 endpoint = uxfer->endpoint;
1363 dev = libusb_get_device(uxfer->dev_handle);
1365 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1367 sxfer = (struct libusb_super_transfer *)(
1368 (uint8_t *)uxfer - sizeof(*sxfer));
1372 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1373 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1375 if (pxfer0 == NULL || pxfer1 == NULL) {
1376 err = LIBUSB_ERROR_OTHER;
1377 } else if ((sxfer->entry.tqe_prev != NULL) ||
1378 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1379 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1380 err = LIBUSB_ERROR_BUSY;
1383 /* set pending state */
1384 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1386 /* insert transfer into transfer head list */
1387 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1389 /* start work transfers */
1390 libusb10_submit_transfer_sub(
1391 uxfer->dev_handle, endpoint);
1393 err = 0; /* success */
1396 CTX_UNLOCK(dev->ctx);
1398 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1403 /* Asynchronous transfer cancel */
1406 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1408 struct libusb20_transfer *pxfer0;
1409 struct libusb20_transfer *pxfer1;
1410 struct libusb_super_transfer *sxfer;
1411 struct libusb_device *dev;
1416 return (LIBUSB_ERROR_INVALID_PARAM);
1418 /* check if not initialised */
1419 if (uxfer->dev_handle == NULL)
1420 return (LIBUSB_ERROR_NOT_FOUND);
1422 endpoint = uxfer->endpoint;
1424 dev = libusb_get_device(uxfer->dev_handle);
1426 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1428 sxfer = (struct libusb_super_transfer *)(
1429 (uint8_t *)uxfer - sizeof(*sxfer));
1435 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1436 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1438 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1439 /* only update the transfer status */
1440 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1441 retval = LIBUSB_ERROR_NOT_FOUND;
1442 } else if (sxfer->entry.tqe_prev != NULL) {
1443 /* we are lucky - transfer is on a queue */
1444 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1445 sxfer->entry.tqe_prev = NULL;
1446 libusb10_complete_transfer(NULL,
1447 sxfer, LIBUSB_TRANSFER_CANCELLED);
1448 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1450 retval = LIBUSB_ERROR_NOT_FOUND;
1451 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1452 libusb10_complete_transfer(pxfer0,
1453 sxfer, LIBUSB_TRANSFER_CANCELLED);
1454 libusb20_tr_stop(pxfer0);
1455 /* make sure the queue doesn't stall */
1456 libusb10_submit_transfer_sub(
1457 uxfer->dev_handle, endpoint);
1458 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1459 libusb10_complete_transfer(pxfer1,
1460 sxfer, LIBUSB_TRANSFER_CANCELLED);
1461 libusb20_tr_stop(pxfer1);
1462 /* make sure the queue doesn't stall */
1463 libusb10_submit_transfer_sub(
1464 uxfer->dev_handle, endpoint);
1467 retval = LIBUSB_ERROR_NOT_FOUND;
1470 CTX_UNLOCK(dev->ctx);
1472 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1478 libusb10_cancel_all_transfer(libusb_device *dev)
1484 libusb_cpu_to_le16(uint16_t x)
1486 return (htole16(x));
1490 libusb_le16_to_cpu(uint16_t x)
1492 return (le16toh(x));
1496 libusb_strerror(int code)
1499 case LIBUSB_SUCCESS:
1501 case LIBUSB_ERROR_IO:
1502 return ("I/O error");
1503 case LIBUSB_ERROR_INVALID_PARAM:
1504 return ("Invalid parameter");
1505 case LIBUSB_ERROR_ACCESS:
1506 return ("Permissions error");
1507 case LIBUSB_ERROR_NO_DEVICE:
1508 return ("No device");
1509 case LIBUSB_ERROR_NOT_FOUND:
1510 return ("Not found");
1511 case LIBUSB_ERROR_BUSY:
1512 return ("Device busy");
1513 case LIBUSB_ERROR_TIMEOUT:
1515 case LIBUSB_ERROR_OVERFLOW:
1516 return ("Overflow");
1517 case LIBUSB_ERROR_PIPE:
1518 return ("Pipe error");
1519 case LIBUSB_ERROR_INTERRUPTED:
1520 return ("Interrupted");
1521 case LIBUSB_ERROR_NO_MEM:
1522 return ("Out of memory");
1523 case LIBUSB_ERROR_NOT_SUPPORTED:
1524 return ("Not supported");
1525 case LIBUSB_ERROR_OTHER:
1526 return ("Other error");
1528 return ("Unknown error");
1533 libusb_error_name(int code)
1536 case LIBUSB_SUCCESS:
1537 return ("LIBUSB_SUCCESS");
1538 case LIBUSB_ERROR_IO:
1539 return ("LIBUSB_ERROR_IO");
1540 case LIBUSB_ERROR_INVALID_PARAM:
1541 return ("LIBUSB_ERROR_INVALID_PARAM");
1542 case LIBUSB_ERROR_ACCESS:
1543 return ("LIBUSB_ERROR_ACCESS");
1544 case LIBUSB_ERROR_NO_DEVICE:
1545 return ("LIBUSB_ERROR_NO_DEVICE");
1546 case LIBUSB_ERROR_NOT_FOUND:
1547 return ("LIBUSB_ERROR_NOT_FOUND");
1548 case LIBUSB_ERROR_BUSY:
1549 return ("LIBUSB_ERROR_BUSY");
1550 case LIBUSB_ERROR_TIMEOUT:
1551 return ("LIBUSB_ERROR_TIMEOUT");
1552 case LIBUSB_ERROR_OVERFLOW:
1553 return ("LIBUSB_ERROR_OVERFLOW");
1554 case LIBUSB_ERROR_PIPE:
1555 return ("LIBUSB_ERROR_PIPE");
1556 case LIBUSB_ERROR_INTERRUPTED:
1557 return ("LIBUSB_ERROR_INTERRUPTED");
1558 case LIBUSB_ERROR_NO_MEM:
1559 return ("LIBUSB_ERROR_NO_MEM");
1560 case LIBUSB_ERROR_NOT_SUPPORTED:
1561 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1562 case LIBUSB_ERROR_OTHER:
1563 return ("LIBUSB_ERROR_OTHER");
1565 return ("LIBUSB_ERROR_UNKNOWN");