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 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49 struct libusb_context *usbi_default_context = NULL;
53 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_convert_error(uint8_t status);
56 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57 static void libusb10_isoc_proxy(struct libusb20_transfer *);
58 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
62 /* Library initialisation / deinitialisation */
65 libusb_set_debug(libusb_context *ctx, int level)
67 ctx = GET_CONTEXT(ctx);
73 libusb_set_nonblocking(int f)
78 * We ignore any failures in this function, hence the
79 * non-blocking flag is not critical to the operation of
80 * libUSB. We use F_GETFL and F_SETFL to be compatible with
84 flags = fcntl(f, F_GETFL, NULL);
88 fcntl(f, F_SETFL, flags);
92 libusb_init(libusb_context **context)
94 struct libusb_context *ctx;
95 pthread_condattr_t attr;
99 ctx = malloc(sizeof(*ctx));
101 return (LIBUSB_ERROR_INVALID_PARAM);
103 memset(ctx, 0, sizeof(*ctx));
105 debug = getenv("LIBUSB_DEBUG");
107 ctx->debug = atoi(debug);
109 ctx->debug_fixed = 1;
111 TAILQ_INIT(&ctx->pollfds);
112 TAILQ_INIT(&ctx->tr_done);
114 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
116 return (LIBUSB_ERROR_NO_MEM);
118 if (pthread_condattr_init(&attr) != 0) {
119 pthread_mutex_destroy(&ctx->ctx_lock);
121 return (LIBUSB_ERROR_NO_MEM);
123 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
124 pthread_mutex_destroy(&ctx->ctx_lock);
125 pthread_condattr_destroy(&attr);
127 return (LIBUSB_ERROR_OTHER);
129 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
130 pthread_mutex_destroy(&ctx->ctx_lock);
131 pthread_condattr_destroy(&attr);
133 return (LIBUSB_ERROR_NO_MEM);
135 pthread_condattr_destroy(&attr);
137 ctx->ctx_handler = NO_THREAD;
139 ret = pipe(ctx->ctrl_pipe);
141 pthread_mutex_destroy(&ctx->ctx_lock);
142 pthread_cond_destroy(&ctx->ctx_cond);
144 return (LIBUSB_ERROR_OTHER);
146 /* set non-blocking mode on the control pipe to avoid deadlock */
147 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
148 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
150 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
152 pthread_mutex_lock(&default_context_lock);
153 if (usbi_default_context == NULL) {
154 usbi_default_context = ctx;
156 pthread_mutex_unlock(&default_context_lock);
161 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
167 libusb_exit(libusb_context *ctx)
169 ctx = GET_CONTEXT(ctx);
174 /* XXX cleanup devices */
176 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
177 close(ctx->ctrl_pipe[0]);
178 close(ctx->ctrl_pipe[1]);
179 pthread_mutex_destroy(&ctx->ctx_lock);
180 pthread_cond_destroy(&ctx->ctx_cond);
182 pthread_mutex_lock(&default_context_lock);
183 if (ctx == usbi_default_context) {
184 usbi_default_context = NULL;
186 pthread_mutex_unlock(&default_context_lock);
191 /* Device handling and initialisation. */
194 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
196 struct libusb20_backend *usb_backend;
197 struct libusb20_device *pdev;
198 struct libusb_device *dev;
201 ctx = GET_CONTEXT(ctx);
204 return (LIBUSB_ERROR_INVALID_PARAM);
207 return (LIBUSB_ERROR_INVALID_PARAM);
209 usb_backend = libusb20_be_alloc_default();
210 if (usb_backend == NULL)
211 return (LIBUSB_ERROR_NO_MEM);
213 /* figure out how many USB devices are present */
216 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
219 /* allocate device pointer list */
220 *list = malloc((i + 1) * sizeof(void *));
222 libusb20_be_free(usb_backend);
223 return (LIBUSB_ERROR_NO_MEM);
225 /* create libusb v1.0 compliant devices */
227 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
229 dev = malloc(sizeof(*dev));
232 libusb_unref_device((*list)[i - 1]);
237 libusb20_be_free(usb_backend);
238 return (LIBUSB_ERROR_NO_MEM);
240 /* get device into libUSB v1.0 list */
241 libusb20_be_dequeue_device(usb_backend, pdev);
243 memset(dev, 0, sizeof(*dev));
245 /* init transfer queues */
246 TAILQ_INIT(&dev->tr_head);
248 /* set context we belong to */
251 /* link together the two structures */
253 pdev->privLuData = dev;
255 (*list)[i] = libusb_ref_device(dev);
260 libusb20_be_free(usb_backend);
265 libusb_free_device_list(libusb_device **list, int unref_devices)
270 return; /* be NULL safe */
273 for (i = 0; list[i] != NULL; i++)
274 libusb_unref_device(list[i]);
280 libusb_get_bus_number(libusb_device *dev)
283 return (0); /* should not happen */
284 return (libusb20_dev_get_bus_number(dev->os_priv));
288 libusb_get_device_address(libusb_device *dev)
291 return (0); /* should not happen */
292 return (libusb20_dev_get_address(dev->os_priv));
296 libusb_get_device_speed(libusb_device *dev)
299 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
301 switch (libusb20_dev_get_speed(dev->os_priv)) {
302 case LIBUSB20_SPEED_LOW:
303 return (LIBUSB_SPEED_LOW);
304 case LIBUSB20_SPEED_FULL:
305 return (LIBUSB_SPEED_FULL);
306 case LIBUSB20_SPEED_HIGH:
307 return (LIBUSB_SPEED_HIGH);
308 case LIBUSB20_SPEED_SUPER:
309 return (LIBUSB_SPEED_SUPER);
313 return (LIBUSB_SPEED_UNKNOWN);
317 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
319 struct libusb_config_descriptor *pdconf;
320 struct libusb_interface *pinf;
321 struct libusb_interface_descriptor *pdinf;
322 struct libusb_endpoint_descriptor *pdend;
329 return (LIBUSB_ERROR_NO_DEVICE);
331 ret = libusb_get_active_config_descriptor(dev, &pdconf);
335 ret = LIBUSB_ERROR_NOT_FOUND;
336 for (i = 0; i < pdconf->bNumInterfaces; i++) {
337 pinf = &pdconf->interface[i];
338 for (j = 0; j < pinf->num_altsetting; j++) {
339 pdinf = &pinf->altsetting[j];
340 for (k = 0; k < pdinf->bNumEndpoints; k++) {
341 pdend = &pdinf->endpoint[k];
342 if (pdend->bEndpointAddress == endpoint) {
343 ret = pdend->wMaxPacketSize;
351 libusb_free_config_descriptor(pdconf);
356 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
361 ret = libusb_get_max_packet_size(dev, endpoint);
363 switch (libusb20_dev_get_speed(dev->os_priv)) {
364 case LIBUSB20_SPEED_LOW:
365 case LIBUSB20_SPEED_FULL:
369 multiplier = (1 + ((ret >> 11) & 3));
372 ret = (ret & 0x7FF) * multiplier;
380 libusb_ref_device(libusb_device *dev)
383 return (NULL); /* be NULL safe */
387 CTX_UNLOCK(dev->ctx);
393 libusb_unref_device(libusb_device *dev)
396 return; /* be NULL safe */
400 CTX_UNLOCK(dev->ctx);
402 if (dev->refcnt == 0) {
403 libusb20_dev_free(dev->os_priv);
409 libusb_open(libusb_device *dev, libusb_device_handle **devh)
411 libusb_context *ctx = dev->ctx;
412 struct libusb20_device *pdev = dev->os_priv;
417 return (LIBUSB_ERROR_INVALID_PARAM);
419 /* set default device handle value */
422 dev = libusb_ref_device(dev);
424 return (LIBUSB_ERROR_INVALID_PARAM);
426 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
428 libusb_unref_device(dev);
429 return (LIBUSB_ERROR_NO_MEM);
431 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
432 POLLOUT | POLLRDNORM | POLLWRNORM);
434 /* make sure our event loop detects the new device */
436 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
437 if (err < (int)sizeof(dummy)) {
438 /* ignore error, if any */
439 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
446 libusb_device_handle *
447 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
450 struct libusb_device **devs;
451 struct libusb20_device *pdev;
452 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
456 ctx = GET_CONTEXT(ctx);
458 return (NULL); /* be NULL safe */
460 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
462 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
466 for (j = 0; j < i; j++) {
467 struct libusb20_device *tdev;
469 tdev = devs[j]->os_priv;
470 pdesc = libusb20_dev_get_device_desc(tdev);
472 * NOTE: The USB library will automatically swap the
473 * fields in the device descriptor to be of host
476 if (pdesc->idVendor == vendor_id &&
477 pdesc->idProduct == product_id) {
478 libusb_open(devs[j], &pdev);
483 libusb_free_device_list(devs, 1);
484 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
489 libusb_close(struct libusb20_device *pdev)
492 struct libusb_device *dev;
497 return; /* be NULL safe */
499 dev = libusb_get_device(pdev);
502 libusb10_remove_pollfd(ctx, &dev->dev_poll);
504 libusb20_dev_close(pdev);
506 /* unref will free the "pdev" when the refcount reaches zero */
507 libusb_unref_device(dev);
509 /* make sure our event loop detects the closed device */
511 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
512 if (err < (int)sizeof(dummy)) {
513 /* ignore error, if any */
514 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
519 libusb_get_device(struct libusb20_device *pdev)
523 return ((libusb_device *)pdev->privLuData);
527 libusb_get_configuration(struct libusb20_device *pdev, int *config)
529 struct libusb20_config *pconf;
531 if (pdev == NULL || config == NULL)
532 return (LIBUSB_ERROR_INVALID_PARAM);
534 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
536 return (LIBUSB_ERROR_NO_MEM);
538 *config = pconf->desc.bConfigurationValue;
546 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
548 struct libusb20_config *pconf;
549 struct libusb_device *dev;
553 dev = libusb_get_device(pdev);
555 return (LIBUSB_ERROR_INVALID_PARAM);
557 if (configuration < 1) {
561 for (i = 0; i != 255; i++) {
564 pconf = libusb20_dev_alloc_config(pdev, i);
566 return (LIBUSB_ERROR_INVALID_PARAM);
567 found = (pconf->desc.bConfigurationValue
574 return (LIBUSB_ERROR_INVALID_PARAM);
579 libusb10_cancel_all_transfer(dev);
581 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
583 err = libusb20_dev_set_config_index(pdev, i);
585 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
586 POLLOUT | POLLRDNORM | POLLWRNORM);
588 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
592 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
597 dev = libusb_get_device(pdev);
599 return (LIBUSB_ERROR_INVALID_PARAM);
601 if (interface_number < 0 || interface_number > 31)
602 return (LIBUSB_ERROR_INVALID_PARAM);
605 if (dev->claimed_interfaces & (1 << interface_number))
606 err = LIBUSB_ERROR_BUSY;
609 dev->claimed_interfaces |= (1 << interface_number);
610 CTX_UNLOCK(dev->ctx);
615 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
620 dev = libusb_get_device(pdev);
622 return (LIBUSB_ERROR_INVALID_PARAM);
624 if (interface_number < 0 || interface_number > 31)
625 return (LIBUSB_ERROR_INVALID_PARAM);
628 if (!(dev->claimed_interfaces & (1 << interface_number)))
629 err = LIBUSB_ERROR_NOT_FOUND;
632 dev->claimed_interfaces &= ~(1 << interface_number);
633 CTX_UNLOCK(dev->ctx);
638 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
639 int interface_number, int alternate_setting)
644 dev = libusb_get_device(pdev);
646 return (LIBUSB_ERROR_INVALID_PARAM);
648 if (interface_number < 0 || interface_number > 31)
649 return (LIBUSB_ERROR_INVALID_PARAM);
652 if (!(dev->claimed_interfaces & (1 << interface_number)))
653 err = LIBUSB_ERROR_NOT_FOUND;
654 CTX_UNLOCK(dev->ctx);
659 libusb10_cancel_all_transfer(dev);
661 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
663 err = libusb20_dev_set_alt_index(pdev,
664 interface_number, alternate_setting);
666 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
667 pdev, libusb20_dev_get_fd(pdev),
668 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
670 return (err ? LIBUSB_ERROR_OTHER : 0);
673 static struct libusb20_transfer *
674 libusb10_get_transfer(struct libusb20_device *pdev,
675 uint8_t endpoint, uint8_t xfer_index)
677 xfer_index &= 1; /* double buffering */
679 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
681 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
682 /* this is an IN endpoint */
685 return (libusb20_tr_get_pointer(pdev, xfer_index));
689 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
691 struct libusb20_transfer *xfer;
692 struct libusb_device *dev;
695 xfer = libusb10_get_transfer(pdev, endpoint, 0);
697 return (LIBUSB_ERROR_INVALID_PARAM);
699 dev = libusb_get_device(pdev);
701 return (LIBUSB_ERROR_INVALID_PARAM);
704 err = libusb20_tr_open(xfer, 0, 1, endpoint);
705 CTX_UNLOCK(dev->ctx);
707 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
708 return (LIBUSB_ERROR_OTHER);
710 libusb20_tr_clear_stall_sync(xfer);
712 /* check if we opened the transfer */
715 libusb20_tr_close(xfer);
716 CTX_UNLOCK(dev->ctx);
718 return (0); /* success */
722 libusb_reset_device(struct libusb20_device *pdev)
727 dev = libusb_get_device(pdev);
729 return (LIBUSB_ERROR_INVALID_PARAM);
731 libusb10_cancel_all_transfer(dev);
733 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
735 err = libusb20_dev_reset(pdev);
737 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
738 pdev, libusb20_dev_get_fd(pdev),
739 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
741 return (err ? LIBUSB_ERROR_OTHER : 0);
745 libusb_check_connected(struct libusb20_device *pdev)
750 dev = libusb_get_device(pdev);
752 return (LIBUSB_ERROR_INVALID_PARAM);
754 err = libusb20_dev_check_connected(pdev);
756 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
760 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
763 return (LIBUSB_ERROR_INVALID_PARAM);
765 if (libusb20_dev_kernel_driver_active(pdev, interface))
766 return (0); /* no kernel driver is active */
768 return (1); /* kernel driver is active */
772 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
773 char *name, int namelen)
775 return (libusb_get_driver(pdev, interface, name, namelen));
779 libusb_get_driver(struct libusb20_device *pdev, int interface,
780 char *name, int namelen)
786 return (LIBUSB_ERROR_INVALID_PARAM);
788 return (LIBUSB_ERROR_INVALID_PARAM);
792 err = libusb20_dev_get_iface_desc(
793 pdev, interface, name, namelen);
796 return (LIBUSB_ERROR_OTHER);
798 /* we only want the driver name */
799 ptr = strstr(name, ":");
807 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
809 return (libusb_detach_kernel_driver(pdev, interface));
813 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
818 return (LIBUSB_ERROR_INVALID_PARAM);
820 err = libusb20_dev_detach_kernel_driver(
823 return (err ? LIBUSB_ERROR_OTHER : 0);
827 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
830 return (LIBUSB_ERROR_INVALID_PARAM);
831 /* stub - currently not supported by libusb20 */
835 /* Asynchronous device I/O */
837 struct libusb_transfer *
838 libusb_alloc_transfer(int iso_packets)
840 struct libusb_transfer *uxfer;
841 struct libusb_super_transfer *sxfer;
844 len = sizeof(struct libusb_transfer) +
845 sizeof(struct libusb_super_transfer) +
846 (iso_packets * sizeof(libusb_iso_packet_descriptor));
852 memset(sxfer, 0, len);
854 uxfer = (struct libusb_transfer *)(
855 ((uint8_t *)sxfer) + sizeof(*sxfer));
857 /* set default value */
858 uxfer->num_iso_packets = iso_packets;
864 libusb_free_transfer(struct libusb_transfer *uxfer)
866 struct libusb_super_transfer *sxfer;
869 return; /* be NULL safe */
871 /* check if we should free the transfer buffer */
872 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
875 sxfer = (struct libusb_super_transfer *)(
876 (uint8_t *)uxfer - sizeof(*sxfer));
882 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
886 switch (xfer->type) {
887 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
888 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
890 case LIBUSB_TRANSFER_TYPE_CONTROL:
901 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
906 usb_speed = libusb20_dev_get_speed(pdev);
908 switch (xfer->type) {
909 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
910 ret = 0; /* kernel will auto-select */
912 case LIBUSB_TRANSFER_TYPE_CONTROL:
917 case LIBUSB20_SPEED_LOW:
920 case LIBUSB20_SPEED_FULL:
933 libusb10_convert_error(uint8_t status)
938 case LIBUSB20_TRANSFER_START:
939 case LIBUSB20_TRANSFER_COMPLETED:
940 return (LIBUSB_TRANSFER_COMPLETED);
941 case LIBUSB20_TRANSFER_OVERFLOW:
942 return (LIBUSB_TRANSFER_OVERFLOW);
943 case LIBUSB20_TRANSFER_NO_DEVICE:
944 return (LIBUSB_TRANSFER_NO_DEVICE);
945 case LIBUSB20_TRANSFER_STALL:
946 return (LIBUSB_TRANSFER_STALL);
947 case LIBUSB20_TRANSFER_CANCELLED:
948 return (LIBUSB_TRANSFER_CANCELLED);
949 case LIBUSB20_TRANSFER_TIMED_OUT:
950 return (LIBUSB_TRANSFER_TIMED_OUT);
952 return (LIBUSB_TRANSFER_ERROR);
956 /* This function must be called locked */
959 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
960 struct libusb_super_transfer *sxfer, int status)
962 struct libusb_transfer *uxfer;
963 struct libusb_device *dev;
965 uxfer = (struct libusb_transfer *)(
966 ((uint8_t *)sxfer) + sizeof(*sxfer));
969 libusb20_tr_set_priv_sc1(pxfer, NULL);
971 /* set transfer status */
972 uxfer->status = status;
974 /* update super transfer state */
975 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
977 dev = libusb_get_device(uxfer->dev_handle);
979 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
982 /* This function must be called locked */
985 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
987 struct libusb_super_transfer *sxfer;
988 struct libusb_transfer *uxfer;
990 uint16_t iso_packets;
995 status = libusb20_tr_get_status(pxfer);
996 sxfer = libusb20_tr_get_priv_sc1(pxfer);
997 actlen = libusb20_tr_get_actual_length(pxfer);
998 iso_packets = libusb20_tr_get_max_frames(pxfer);
1001 return; /* cancelled - nothing to do */
1003 uxfer = (struct libusb_transfer *)(
1004 ((uint8_t *)sxfer) + sizeof(*sxfer));
1006 if (iso_packets > uxfer->num_iso_packets)
1007 iso_packets = uxfer->num_iso_packets;
1009 if (iso_packets == 0)
1010 return; /* nothing to do */
1012 /* make sure that the number of ISOCHRONOUS packets is valid */
1013 uxfer->num_iso_packets = iso_packets;
1015 flags = uxfer->flags;
1018 case LIBUSB20_TRANSFER_COMPLETED:
1020 /* update actual length */
1021 uxfer->actual_length = actlen;
1022 for (i = 0; i != iso_packets; i++) {
1023 uxfer->iso_packet_desc[i].actual_length =
1024 libusb20_tr_get_length(pxfer, i);
1026 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1029 case LIBUSB20_TRANSFER_START:
1031 /* setup length(s) */
1033 for (i = 0; i != iso_packets; i++) {
1034 libusb20_tr_setup_isoc(pxfer,
1035 &uxfer->buffer[actlen],
1036 uxfer->iso_packet_desc[i].length, i);
1037 actlen += uxfer->iso_packet_desc[i].length;
1043 libusb20_tr_set_total_frames(pxfer, iso_packets);
1044 libusb20_tr_submit(pxfer);
1046 /* fork another USB transfer, if any */
1047 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1051 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1056 /* This function must be called locked */
1059 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1061 struct libusb_super_transfer *sxfer;
1062 struct libusb_transfer *uxfer;
1068 status = libusb20_tr_get_status(pxfer);
1069 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1070 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1071 actlen = libusb20_tr_get_actual_length(pxfer);
1074 return; /* cancelled - nothing to do */
1076 uxfer = (struct libusb_transfer *)(
1077 ((uint8_t *)sxfer) + sizeof(*sxfer));
1079 flags = uxfer->flags;
1082 case LIBUSB20_TRANSFER_COMPLETED:
1084 uxfer->actual_length += actlen;
1086 /* check for short packet */
1087 if (sxfer->last_len != actlen) {
1088 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1089 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1091 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1095 /* check for end of data */
1096 if (sxfer->rem_len == 0) {
1097 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1102 case LIBUSB20_TRANSFER_START:
1103 if (max_bulk > sxfer->rem_len) {
1104 max_bulk = sxfer->rem_len;
1106 /* setup new BULK or INTERRUPT transaction */
1107 libusb20_tr_setup_bulk(pxfer,
1108 sxfer->curr_data, max_bulk, uxfer->timeout);
1110 /* update counters */
1111 sxfer->last_len = max_bulk;
1112 sxfer->curr_data += max_bulk;
1113 sxfer->rem_len -= max_bulk;
1115 libusb20_tr_submit(pxfer);
1117 /* check if we can fork another USB transfer */
1118 if (sxfer->rem_len == 0)
1119 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1123 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1128 /* This function must be called locked */
1131 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1133 struct libusb_super_transfer *sxfer;
1134 struct libusb_transfer *uxfer;
1140 status = libusb20_tr_get_status(pxfer);
1141 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1142 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1143 actlen = libusb20_tr_get_actual_length(pxfer);
1146 return; /* cancelled - nothing to do */
1148 uxfer = (struct libusb_transfer *)(
1149 ((uint8_t *)sxfer) + sizeof(*sxfer));
1151 flags = uxfer->flags;
1154 case LIBUSB20_TRANSFER_COMPLETED:
1156 uxfer->actual_length += actlen;
1158 /* subtract length of SETUP packet, if any */
1159 actlen -= libusb20_tr_get_length(pxfer, 0);
1161 /* check for short packet */
1162 if (sxfer->last_len != actlen) {
1163 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1164 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1166 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1170 /* check for end of data */
1171 if (sxfer->rem_len == 0) {
1172 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1177 case LIBUSB20_TRANSFER_START:
1178 if (max_bulk > sxfer->rem_len) {
1179 max_bulk = sxfer->rem_len;
1181 /* setup new CONTROL transaction */
1182 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1183 /* next fragment - don't send SETUP packet */
1184 libusb20_tr_set_length(pxfer, 0, 0);
1186 /* first fragment - send SETUP packet */
1187 libusb20_tr_set_length(pxfer, 8, 0);
1188 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1191 if (max_bulk != 0) {
1192 libusb20_tr_set_length(pxfer, max_bulk, 1);
1193 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1194 libusb20_tr_set_total_frames(pxfer, 2);
1196 libusb20_tr_set_total_frames(pxfer, 1);
1199 /* update counters */
1200 sxfer->last_len = max_bulk;
1201 sxfer->curr_data += max_bulk;
1202 sxfer->rem_len -= max_bulk;
1204 libusb20_tr_submit(pxfer);
1206 /* check if we can fork another USB transfer */
1207 if (sxfer->rem_len == 0)
1208 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1212 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1217 /* The following function must be called locked */
1220 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1222 struct libusb20_transfer *pxfer0;
1223 struct libusb20_transfer *pxfer1;
1224 struct libusb_super_transfer *sxfer;
1225 struct libusb_transfer *uxfer;
1226 struct libusb_device *dev;
1233 dev = libusb_get_device(pdev);
1235 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1236 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1238 if (pxfer0 == NULL || pxfer1 == NULL)
1239 return; /* shouldn't happen */
1242 if (libusb20_tr_pending(pxfer0))
1244 if (libusb20_tr_pending(pxfer1))
1249 /* wait till one of the transfers complete */
1252 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1254 return; /* cancelling */
1256 return; /* cannot queue another one */
1257 /* swap transfers */
1261 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1263 return; /* cancelling */
1265 return; /* cannot queue another one */
1266 /* swap transfers */
1273 /* find next transfer on same endpoint */
1274 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1276 uxfer = (struct libusb_transfer *)(
1277 ((uint8_t *)sxfer) + sizeof(*sxfer));
1279 if (uxfer->endpoint == endpoint) {
1280 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1281 sxfer->entry.tqe_prev = NULL;
1285 return; /* success */
1289 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1290 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1292 /* reset super transfer state */
1293 sxfer->rem_len = uxfer->length;
1294 sxfer->curr_data = uxfer->buffer;
1295 uxfer->actual_length = 0;
1297 switch (uxfer->type) {
1298 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1299 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1301 case LIBUSB_TRANSFER_TYPE_BULK:
1302 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1303 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1305 case LIBUSB_TRANSFER_TYPE_CONTROL:
1306 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1307 if (sxfer->rem_len < 8)
1310 /* remove SETUP packet from data */
1311 sxfer->rem_len -= 8;
1312 sxfer->curr_data += 8;
1318 buffsize = libusb10_get_buffsize(pdev, uxfer);
1319 maxframe = libusb10_get_maxframe(pdev, uxfer);
1321 /* make sure the transfer is opened */
1322 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1323 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1326 libusb20_tr_start(pxfer0);
1330 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1332 /* make sure our event loop spins the done handler */
1334 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1337 /* The following function must be called unlocked */
1340 libusb_submit_transfer(struct libusb_transfer *uxfer)
1342 struct libusb20_transfer *pxfer0;
1343 struct libusb20_transfer *pxfer1;
1344 struct libusb_super_transfer *sxfer;
1345 struct libusb_device *dev;
1350 return (LIBUSB_ERROR_INVALID_PARAM);
1352 if (uxfer->dev_handle == NULL)
1353 return (LIBUSB_ERROR_INVALID_PARAM);
1355 endpoint = uxfer->endpoint;
1357 dev = libusb_get_device(uxfer->dev_handle);
1359 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1361 sxfer = (struct libusb_super_transfer *)(
1362 (uint8_t *)uxfer - sizeof(*sxfer));
1366 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1367 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1369 if (pxfer0 == NULL || pxfer1 == NULL) {
1370 err = LIBUSB_ERROR_OTHER;
1371 } else if ((sxfer->entry.tqe_prev != NULL) ||
1372 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1373 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1374 err = LIBUSB_ERROR_BUSY;
1377 /* set pending state */
1378 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1380 /* insert transfer into transfer head list */
1381 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1383 /* start work transfers */
1384 libusb10_submit_transfer_sub(
1385 uxfer->dev_handle, endpoint);
1387 err = 0; /* success */
1390 CTX_UNLOCK(dev->ctx);
1392 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1397 /* Asynchronous transfer cancel */
1400 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1402 struct libusb20_transfer *pxfer0;
1403 struct libusb20_transfer *pxfer1;
1404 struct libusb_super_transfer *sxfer;
1405 struct libusb_device *dev;
1410 return (LIBUSB_ERROR_INVALID_PARAM);
1412 /* check if not initialised */
1413 if (uxfer->dev_handle == NULL)
1414 return (LIBUSB_ERROR_NOT_FOUND);
1416 endpoint = uxfer->endpoint;
1418 dev = libusb_get_device(uxfer->dev_handle);
1420 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1422 sxfer = (struct libusb_super_transfer *)(
1423 (uint8_t *)uxfer - sizeof(*sxfer));
1429 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1430 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1432 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1433 /* only update the transfer status */
1434 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1435 retval = LIBUSB_ERROR_NOT_FOUND;
1436 } else if (sxfer->entry.tqe_prev != NULL) {
1437 /* we are lucky - transfer is on a queue */
1438 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1439 sxfer->entry.tqe_prev = NULL;
1440 libusb10_complete_transfer(NULL,
1441 sxfer, LIBUSB_TRANSFER_CANCELLED);
1442 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1444 retval = LIBUSB_ERROR_NOT_FOUND;
1445 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1446 libusb10_complete_transfer(pxfer0,
1447 sxfer, LIBUSB_TRANSFER_CANCELLED);
1448 libusb20_tr_stop(pxfer0);
1449 /* make sure the queue doesn't stall */
1450 libusb10_submit_transfer_sub(
1451 uxfer->dev_handle, endpoint);
1452 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1453 libusb10_complete_transfer(pxfer1,
1454 sxfer, LIBUSB_TRANSFER_CANCELLED);
1455 libusb20_tr_stop(pxfer1);
1456 /* make sure the queue doesn't stall */
1457 libusb10_submit_transfer_sub(
1458 uxfer->dev_handle, endpoint);
1461 retval = LIBUSB_ERROR_NOT_FOUND;
1464 CTX_UNLOCK(dev->ctx);
1466 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1472 libusb10_cancel_all_transfer(libusb_device *dev)
1478 libusb_cpu_to_le16(uint16_t x)
1480 return (htole16(x));
1484 libusb_le16_to_cpu(uint16_t x)
1486 return (le16toh(x));
1490 libusb_strerror(int code)
1493 case LIBUSB_SUCCESS:
1495 case LIBUSB_ERROR_IO:
1496 return ("I/O error");
1497 case LIBUSB_ERROR_INVALID_PARAM:
1498 return ("Invalid parameter");
1499 case LIBUSB_ERROR_ACCESS:
1500 return ("Permissions error");
1501 case LIBUSB_ERROR_NO_DEVICE:
1502 return ("No device");
1503 case LIBUSB_ERROR_NOT_FOUND:
1504 return ("Not found");
1505 case LIBUSB_ERROR_BUSY:
1506 return ("Device busy");
1507 case LIBUSB_ERROR_TIMEOUT:
1509 case LIBUSB_ERROR_OVERFLOW:
1510 return ("Overflow");
1511 case LIBUSB_ERROR_PIPE:
1512 return ("Pipe error");
1513 case LIBUSB_ERROR_INTERRUPTED:
1514 return ("Interrupted");
1515 case LIBUSB_ERROR_NO_MEM:
1516 return ("Out of memory");
1517 case LIBUSB_ERROR_NOT_SUPPORTED:
1518 return ("Not supported");
1519 case LIBUSB_ERROR_OTHER:
1520 return ("Other error");
1522 return ("Unknown error");
1527 libusb_error_name(int code)
1530 case LIBUSB_SUCCESS:
1531 return ("LIBUSB_SUCCESS");
1532 case LIBUSB_ERROR_IO:
1533 return ("LIBUSB_ERROR_IO");
1534 case LIBUSB_ERROR_INVALID_PARAM:
1535 return ("LIBUSB_ERROR_INVALID_PARAM");
1536 case LIBUSB_ERROR_ACCESS:
1537 return ("LIBUSB_ERROR_ACCESS");
1538 case LIBUSB_ERROR_NO_DEVICE:
1539 return ("LIBUSB_ERROR_NO_DEVICE");
1540 case LIBUSB_ERROR_NOT_FOUND:
1541 return ("LIBUSB_ERROR_NOT_FOUND");
1542 case LIBUSB_ERROR_BUSY:
1543 return ("LIBUSB_ERROR_BUSY");
1544 case LIBUSB_ERROR_TIMEOUT:
1545 return ("LIBUSB_ERROR_TIMEOUT");
1546 case LIBUSB_ERROR_OVERFLOW:
1547 return ("LIBUSB_ERROR_OVERFLOW");
1548 case LIBUSB_ERROR_PIPE:
1549 return ("LIBUSB_ERROR_PIPE");
1550 case LIBUSB_ERROR_INTERRUPTED:
1551 return ("LIBUSB_ERROR_INTERRUPTED");
1552 case LIBUSB_ERROR_NO_MEM:
1553 return ("LIBUSB_ERROR_NO_MEM");
1554 case LIBUSB_ERROR_NOT_SUPPORTED:
1555 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1556 case LIBUSB_ERROR_OTHER:
1557 return ("LIBUSB_ERROR_OTHER");
1559 return ("LIBUSB_ERROR_UNKNOWN");