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_maxframe(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
56 static int libusb10_convert_error(uint8_t status);
57 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
58 static void libusb10_isoc_proxy(struct libusb20_transfer *);
59 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
60 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
61 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
63 /* Library initialisation / deinitialisation */
66 libusb_set_debug(libusb_context *ctx, int level)
68 ctx = GET_CONTEXT(ctx);
74 libusb_set_nonblocking(int f)
79 * We ignore any failures in this function, hence the
80 * non-blocking flag is not critical to the operation of
81 * libUSB. We use F_GETFL and F_SETFL to be compatible with
85 flags = fcntl(f, F_GETFL, NULL);
89 fcntl(f, F_SETFL, flags);
93 libusb_init(libusb_context **context)
95 struct libusb_context *ctx;
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 pthread_mutex_init(&ctx->ctx_lock, NULL);
115 pthread_cond_init(&ctx->ctx_cond, NULL);
117 ctx->ctx_handler = NO_THREAD;
119 ret = pipe(ctx->ctrl_pipe);
121 pthread_mutex_destroy(&ctx->ctx_lock);
122 pthread_cond_destroy(&ctx->ctx_cond);
124 return (LIBUSB_ERROR_OTHER);
126 /* set non-blocking mode on the control pipe to avoid deadlock */
127 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
128 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
130 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
132 pthread_mutex_lock(&default_context_lock);
133 if (usbi_default_context == NULL) {
134 usbi_default_context = ctx;
136 pthread_mutex_unlock(&default_context_lock);
141 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
147 libusb_exit(libusb_context *ctx)
149 ctx = GET_CONTEXT(ctx);
154 /* XXX cleanup devices */
156 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
157 close(ctx->ctrl_pipe[0]);
158 close(ctx->ctrl_pipe[1]);
159 pthread_mutex_destroy(&ctx->ctx_lock);
160 pthread_cond_destroy(&ctx->ctx_cond);
162 pthread_mutex_lock(&default_context_lock);
163 if (ctx == usbi_default_context) {
164 usbi_default_context = NULL;
166 pthread_mutex_unlock(&default_context_lock);
171 /* Device handling and initialisation. */
174 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
176 struct libusb20_backend *usb_backend;
177 struct libusb20_device *pdev;
178 struct libusb_device *dev;
181 ctx = GET_CONTEXT(ctx);
184 return (LIBUSB_ERROR_INVALID_PARAM);
187 return (LIBUSB_ERROR_INVALID_PARAM);
189 usb_backend = libusb20_be_alloc_default();
190 if (usb_backend == NULL)
191 return (LIBUSB_ERROR_NO_MEM);
193 /* figure out how many USB devices are present */
196 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
199 /* allocate device pointer list */
200 *list = malloc((i + 1) * sizeof(void *));
202 libusb20_be_free(usb_backend);
203 return (LIBUSB_ERROR_NO_MEM);
205 /* create libusb v1.0 compliant devices */
207 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
209 dev = malloc(sizeof(*dev));
212 libusb_unref_device((*list)[i - 1]);
217 libusb20_be_free(usb_backend);
218 return (LIBUSB_ERROR_NO_MEM);
221 /* get device into libUSB v1.0 list */
222 libusb20_be_dequeue_device(usb_backend, pdev);
224 memset(dev, 0, sizeof(*dev));
226 /* init transfer queues */
227 TAILQ_INIT(&dev->tr_head);
229 /* set context we belong to */
232 /* link together the two structures */
234 pdev->privLuData = dev;
236 (*list)[i] = libusb_ref_device(dev);
241 libusb20_be_free(usb_backend);
246 libusb_free_device_list(libusb_device **list, int unref_devices)
251 return; /* be NULL safe */
254 for (i = 0; list[i] != NULL; i++)
255 libusb_unref_device(list[i]);
261 libusb_get_bus_number(libusb_device *dev)
264 return (0); /* should not happen */
265 return (libusb20_dev_get_bus_number(dev->os_priv));
269 libusb_get_device_address(libusb_device *dev)
272 return (0); /* should not happen */
273 return (libusb20_dev_get_address(dev->os_priv));
277 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
279 struct libusb_config_descriptor *pdconf;
280 struct libusb_interface *pinf;
281 struct libusb_interface_descriptor *pdinf;
282 struct libusb_endpoint_descriptor *pdend;
289 return (LIBUSB_ERROR_NO_DEVICE);
291 ret = libusb_get_active_config_descriptor(dev, &pdconf);
295 ret = LIBUSB_ERROR_NOT_FOUND;
296 for (i = 0; i < pdconf->bNumInterfaces; i++) {
297 pinf = &pdconf->interface[i];
298 for (j = 0; j < pinf->num_altsetting; j++) {
299 pdinf = &pinf->altsetting[j];
300 for (k = 0; k < pdinf->bNumEndpoints; k++) {
301 pdend = &pdinf->endpoint[k];
302 if (pdend->bEndpointAddress == endpoint) {
303 ret = pdend->wMaxPacketSize;
311 libusb_free_config_descriptor(pdconf);
316 libusb_ref_device(libusb_device *dev)
319 return (NULL); /* be NULL safe */
323 CTX_UNLOCK(dev->ctx);
329 libusb_unref_device(libusb_device *dev)
332 return; /* be NULL safe */
336 CTX_UNLOCK(dev->ctx);
338 if (dev->refcnt == 0) {
339 libusb20_dev_free(dev->os_priv);
345 libusb_open(libusb_device *dev, libusb_device_handle **devh)
347 libusb_context *ctx = dev->ctx;
348 struct libusb20_device *pdev = dev->os_priv;
353 return (LIBUSB_ERROR_INVALID_PARAM);
355 /* set default device handle value */
358 dev = libusb_ref_device(dev);
360 return (LIBUSB_ERROR_INVALID_PARAM);
362 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
364 libusb_unref_device(dev);
365 return (LIBUSB_ERROR_NO_MEM);
367 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
368 POLLOUT | POLLRDNORM | POLLWRNORM);
370 /* make sure our event loop detects the new device */
372 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
373 if (err < (int)sizeof(dummy)) {
374 /* ignore error, if any */
375 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
382 libusb_device_handle *
383 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
386 struct libusb_device **devs;
387 struct libusb20_device *pdev;
388 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
392 ctx = GET_CONTEXT(ctx);
394 return (NULL); /* be NULL safe */
396 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
398 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
401 for (j = 0; j < i; j++) {
402 pdev = devs[j]->os_priv;
403 pdesc = libusb20_dev_get_device_desc(pdev);
405 * NOTE: The USB library will automatically swap the
406 * fields in the device descriptor to be of host
409 if (pdesc->idVendor == vendor_id &&
410 pdesc->idProduct == product_id) {
411 if (libusb_open(devs[j], &pdev) < 0)
419 libusb_free_device_list(devs, 1);
420 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
425 libusb_close(struct libusb20_device *pdev)
428 struct libusb_device *dev;
433 return; /* be NULL safe */
435 dev = libusb_get_device(pdev);
438 libusb10_remove_pollfd(ctx, &dev->dev_poll);
440 libusb20_dev_close(pdev);
442 /* unref will free the "pdev" when the refcount reaches zero */
443 libusb_unref_device(dev);
445 /* make sure our event loop detects the closed device */
447 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
448 if (err < (int)sizeof(dummy)) {
449 /* ignore error, if any */
450 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
455 libusb_get_device(struct libusb20_device *pdev)
459 return ((libusb_device *)pdev->privLuData);
463 libusb_get_configuration(struct libusb20_device *pdev, int *config)
465 struct libusb20_config *pconf;
467 if (pdev == NULL || config == NULL)
468 return (LIBUSB_ERROR_INVALID_PARAM);
470 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
472 return (LIBUSB_ERROR_NO_MEM);
474 *config = pconf->desc.bConfigurationValue;
482 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
484 struct libusb20_config *pconf;
485 struct libusb_device *dev;
489 dev = libusb_get_device(pdev);
491 return (LIBUSB_ERROR_INVALID_PARAM);
493 if (configuration < 1) {
497 for (i = 0; i != 255; i++) {
500 pconf = libusb20_dev_alloc_config(pdev, i);
502 return (LIBUSB_ERROR_INVALID_PARAM);
503 found = (pconf->desc.bConfigurationValue
510 return (LIBUSB_ERROR_INVALID_PARAM);
515 libusb10_cancel_all_transfer(dev);
517 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
519 err = libusb20_dev_set_config_index(pdev, i);
521 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
522 POLLOUT | POLLRDNORM | POLLWRNORM);
524 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
528 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
533 dev = libusb_get_device(pdev);
535 return (LIBUSB_ERROR_INVALID_PARAM);
537 if (interface_number < 0 || interface_number > 31)
538 return (LIBUSB_ERROR_INVALID_PARAM);
541 if (dev->claimed_interfaces & (1 << interface_number))
542 err = LIBUSB_ERROR_BUSY;
545 dev->claimed_interfaces |= (1 << interface_number);
546 CTX_UNLOCK(dev->ctx);
551 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
556 dev = libusb_get_device(pdev);
558 return (LIBUSB_ERROR_INVALID_PARAM);
560 if (interface_number < 0 || interface_number > 31)
561 return (LIBUSB_ERROR_INVALID_PARAM);
564 if (!(dev->claimed_interfaces & (1 << interface_number)))
565 err = LIBUSB_ERROR_NOT_FOUND;
568 dev->claimed_interfaces &= ~(1 << interface_number);
569 CTX_UNLOCK(dev->ctx);
574 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
575 int interface_number, int alternate_setting)
580 dev = libusb_get_device(pdev);
582 return (LIBUSB_ERROR_INVALID_PARAM);
584 if (interface_number < 0 || interface_number > 31)
585 return (LIBUSB_ERROR_INVALID_PARAM);
588 if (!(dev->claimed_interfaces & (1 << interface_number)))
589 err = LIBUSB_ERROR_NOT_FOUND;
590 CTX_UNLOCK(dev->ctx);
595 libusb10_cancel_all_transfer(dev);
597 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
599 err = libusb20_dev_set_alt_index(pdev,
600 interface_number, alternate_setting);
602 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
603 pdev, libusb20_dev_get_fd(pdev),
604 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
606 return (err ? LIBUSB_ERROR_OTHER : 0);
609 static struct libusb20_transfer *
610 libusb10_get_transfer(struct libusb20_device *pdev,
611 uint8_t endpoint, uint8_t index)
613 index &= 1; /* double buffering */
615 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
617 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
618 /* this is an IN endpoint */
621 return (libusb20_tr_get_pointer(pdev, index));
625 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
627 struct libusb20_transfer *xfer;
628 struct libusb_device *dev;
631 xfer = libusb10_get_transfer(pdev, endpoint, 0);
633 return (LIBUSB_ERROR_INVALID_PARAM);
635 dev = libusb_get_device(pdev);
637 return (LIBUSB_ERROR_INVALID_PARAM);
640 err = libusb20_tr_open(xfer, 0, 0, endpoint);
641 CTX_UNLOCK(dev->ctx);
643 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
644 return (LIBUSB_ERROR_OTHER);
646 libusb20_tr_clear_stall_sync(xfer);
648 /* check if we opened the transfer */
651 libusb20_tr_close(xfer);
652 CTX_UNLOCK(dev->ctx);
654 return (0); /* success */
658 libusb_reset_device(struct libusb20_device *pdev)
663 dev = libusb_get_device(pdev);
665 return (LIBUSB_ERROR_INVALID_PARAM);
667 libusb10_cancel_all_transfer(dev);
669 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
671 err = libusb20_dev_reset(pdev);
673 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
674 pdev, libusb20_dev_get_fd(pdev),
675 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
677 return (err ? LIBUSB_ERROR_OTHER : 0);
681 libusb_check_connected(struct libusb20_device *pdev)
686 dev = libusb_get_device(pdev);
688 return (LIBUSB_ERROR_INVALID_PARAM);
690 err = libusb20_dev_check_connected(pdev);
692 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
696 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
699 return (LIBUSB_ERROR_INVALID_PARAM);
701 return (libusb20_dev_kernel_driver_active(
706 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
707 char *name, int namelen)
709 return (libusb_get_driver(pdev, interface, name, namelen));
713 libusb_get_driver(struct libusb20_device *pdev, int interface,
714 char *name, int namelen)
720 return (LIBUSB_ERROR_INVALID_PARAM);
722 return (LIBUSB_ERROR_INVALID_PARAM);
724 err = libusb20_dev_get_iface_desc(
725 pdev, interface, name, namelen);
728 return (LIBUSB_ERROR_OTHER);
730 /* we only want the driver name */
731 ptr = strstr(name, ":");
739 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
741 return (libusb_detach_kernel_driver(pdev, interface));
745 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
750 return (LIBUSB_ERROR_INVALID_PARAM);
752 err = libusb20_dev_detach_kernel_driver(
755 return (err ? LIBUSB_ERROR_OTHER : 0);
759 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
762 return (LIBUSB_ERROR_INVALID_PARAM);
763 /* stub - currently not supported by libusb20 */
767 /* Asynchronous device I/O */
769 struct libusb_transfer *
770 libusb_alloc_transfer(int iso_packets)
772 struct libusb_transfer *uxfer;
773 struct libusb_super_transfer *sxfer;
776 len = sizeof(struct libusb_transfer) +
777 sizeof(struct libusb_super_transfer) +
778 (iso_packets * sizeof(libusb_iso_packet_descriptor));
784 memset(sxfer, 0, len);
786 uxfer = (struct libusb_transfer *)(
787 ((uint8_t *)sxfer) + sizeof(*sxfer));
789 /* set default value */
790 uxfer->num_iso_packets = iso_packets;
796 libusb_free_transfer(struct libusb_transfer *uxfer)
798 struct libusb_super_transfer *sxfer;
801 return; /* be NULL safe */
803 sxfer = (struct libusb_super_transfer *)(
804 (uint8_t *)uxfer - sizeof(*sxfer));
810 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
815 usb_speed = libusb20_dev_get_speed(pdev);
817 switch (xfer->type) {
818 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
820 case LIBUSB20_SPEED_LOW:
821 case LIBUSB20_SPEED_FULL:
829 case LIBUSB_TRANSFER_TYPE_CONTROL:
840 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
845 usb_speed = libusb20_dev_get_speed(pdev);
847 switch (xfer->type) {
848 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
849 ret = 0; /* kernel will auto-select */
851 case LIBUSB_TRANSFER_TYPE_CONTROL:
856 case LIBUSB20_SPEED_LOW:
859 case LIBUSB20_SPEED_FULL:
872 libusb10_convert_error(uint8_t status)
877 case LIBUSB20_TRANSFER_START:
878 case LIBUSB20_TRANSFER_COMPLETED:
879 return (LIBUSB_TRANSFER_COMPLETED);
880 case LIBUSB20_TRANSFER_OVERFLOW:
881 return (LIBUSB_TRANSFER_OVERFLOW);
882 case LIBUSB20_TRANSFER_NO_DEVICE:
883 return (LIBUSB_TRANSFER_NO_DEVICE);
884 case LIBUSB20_TRANSFER_STALL:
885 return (LIBUSB_TRANSFER_STALL);
886 case LIBUSB20_TRANSFER_CANCELLED:
887 return (LIBUSB_TRANSFER_CANCELLED);
888 case LIBUSB20_TRANSFER_TIMED_OUT:
889 return (LIBUSB_TRANSFER_TIMED_OUT);
891 return (LIBUSB_TRANSFER_ERROR);
895 /* This function must be called locked */
898 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
899 struct libusb_super_transfer *sxfer, int status)
901 struct libusb_transfer *uxfer;
902 struct libusb_device *dev;
904 uxfer = (struct libusb_transfer *)(
905 ((uint8_t *)sxfer) + sizeof(*sxfer));
908 libusb20_tr_set_priv_sc1(pxfer, NULL);
910 /* set transfer status */
911 uxfer->status = status;
913 /* update super transfer state */
914 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
916 dev = libusb_get_device(uxfer->dev_handle);
918 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
921 /* This function must be called locked */
924 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
926 struct libusb_super_transfer *sxfer;
927 struct libusb_transfer *uxfer;
929 uint16_t iso_packets;
934 status = libusb20_tr_get_status(pxfer);
935 sxfer = libusb20_tr_get_priv_sc1(pxfer);
936 actlen = libusb20_tr_get_actual_length(pxfer);
937 iso_packets = libusb20_tr_get_max_frames(pxfer);
940 return; /* cancelled - nothing to do */
942 uxfer = (struct libusb_transfer *)(
943 ((uint8_t *)sxfer) + sizeof(*sxfer));
945 if (iso_packets > uxfer->num_iso_packets)
946 iso_packets = uxfer->num_iso_packets;
948 if (iso_packets == 0)
949 return; /* nothing to do */
951 /* make sure that the number of ISOCHRONOUS packets is valid */
952 uxfer->num_iso_packets = iso_packets;
954 flags = uxfer->flags;
957 case LIBUSB20_TRANSFER_COMPLETED:
959 /* update actual length */
960 uxfer->actual_length = actlen;
961 for (i = 0; i != iso_packets; i++) {
962 uxfer->iso_packet_desc[i].actual_length =
963 libusb20_tr_get_length(pxfer, i);
965 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
968 case LIBUSB20_TRANSFER_START:
970 /* setup length(s) */
972 for (i = 0; i != iso_packets; i++) {
973 libusb20_tr_setup_isoc(pxfer,
974 &uxfer->buffer[actlen],
975 uxfer->iso_packet_desc[i].length, i);
976 actlen += uxfer->iso_packet_desc[i].length;
982 libusb20_tr_set_total_frames(pxfer, iso_packets);
983 libusb20_tr_submit(pxfer);
985 /* fork another USB transfer, if any */
986 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
990 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
995 /* This function must be called locked */
998 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1000 struct libusb_super_transfer *sxfer;
1001 struct libusb_transfer *uxfer;
1007 status = libusb20_tr_get_status(pxfer);
1008 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1009 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1010 actlen = libusb20_tr_get_actual_length(pxfer);
1013 return; /* cancelled - nothing to do */
1015 uxfer = (struct libusb_transfer *)(
1016 ((uint8_t *)sxfer) + sizeof(*sxfer));
1018 flags = uxfer->flags;
1021 case LIBUSB20_TRANSFER_COMPLETED:
1023 uxfer->actual_length += actlen;
1025 /* check for short packet */
1026 if (sxfer->last_len != actlen) {
1027 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1028 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1030 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1034 /* check for end of data */
1035 if (sxfer->rem_len == 0) {
1036 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1041 case LIBUSB20_TRANSFER_START:
1042 if (max_bulk > sxfer->rem_len) {
1043 max_bulk = sxfer->rem_len;
1045 /* setup new BULK or INTERRUPT transaction */
1046 libusb20_tr_setup_bulk(pxfer,
1047 sxfer->curr_data, max_bulk, uxfer->timeout);
1049 /* update counters */
1050 sxfer->last_len = max_bulk;
1051 sxfer->curr_data += max_bulk;
1052 sxfer->rem_len -= max_bulk;
1054 libusb20_tr_submit(pxfer);
1056 /* check if we can fork another USB transfer */
1057 if (sxfer->rem_len == 0)
1058 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1062 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1067 /* This function must be called locked */
1070 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1072 struct libusb_super_transfer *sxfer;
1073 struct libusb_transfer *uxfer;
1079 status = libusb20_tr_get_status(pxfer);
1080 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1081 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1082 actlen = libusb20_tr_get_actual_length(pxfer);
1085 return; /* cancelled - nothing to do */
1087 uxfer = (struct libusb_transfer *)(
1088 ((uint8_t *)sxfer) + sizeof(*sxfer));
1090 flags = uxfer->flags;
1093 case LIBUSB20_TRANSFER_COMPLETED:
1095 uxfer->actual_length += actlen;
1097 /* subtract length of SETUP packet, if any */
1098 actlen -= libusb20_tr_get_length(pxfer, 0);
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 CONTROL transaction */
1121 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1122 /* next fragment - don't send SETUP packet */
1123 libusb20_tr_set_length(pxfer, 0, 0);
1125 /* first fragment - send SETUP packet */
1126 libusb20_tr_set_length(pxfer, 8, 0);
1127 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1130 if (max_bulk != 0) {
1131 libusb20_tr_set_length(pxfer, max_bulk, 1);
1132 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1133 libusb20_tr_set_total_frames(pxfer, 2);
1135 libusb20_tr_set_total_frames(pxfer, 1);
1138 /* update counters */
1139 sxfer->last_len = max_bulk;
1140 sxfer->curr_data += max_bulk;
1141 sxfer->rem_len -= max_bulk;
1143 libusb20_tr_submit(pxfer);
1145 /* check if we can fork another USB transfer */
1146 if (sxfer->rem_len == 0)
1147 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1151 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1156 /* The following function must be called locked */
1159 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1161 struct libusb20_transfer *pxfer0;
1162 struct libusb20_transfer *pxfer1;
1163 struct libusb_super_transfer *sxfer;
1164 struct libusb_transfer *uxfer;
1165 struct libusb_device *dev;
1172 dev = libusb_get_device(pdev);
1174 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1175 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1177 if (pxfer0 == NULL || pxfer1 == NULL)
1178 return; /* shouldn't happen */
1181 if (libusb20_tr_pending(pxfer0))
1183 if (libusb20_tr_pending(pxfer1))
1188 /* wait till one of the transfers complete */
1191 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1193 return; /* cancelling */
1195 return; /* cannot queue another one */
1196 /* swap transfers */
1200 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1202 return; /* cancelling */
1204 return; /* cannot queue another one */
1205 /* swap transfers */
1212 /* find next transfer on same endpoint */
1213 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1215 uxfer = (struct libusb_transfer *)(
1216 ((uint8_t *)sxfer) + sizeof(*sxfer));
1218 if (uxfer->endpoint == endpoint) {
1219 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1220 sxfer->entry.tqe_prev = NULL;
1224 return; /* success */
1228 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1229 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1231 /* reset super transfer state */
1232 sxfer->rem_len = uxfer->length;
1233 sxfer->curr_data = uxfer->buffer;
1234 uxfer->actual_length = 0;
1236 switch (uxfer->type) {
1237 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1238 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1240 case LIBUSB_TRANSFER_TYPE_BULK:
1241 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1242 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1244 case LIBUSB_TRANSFER_TYPE_CONTROL:
1245 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1246 if (sxfer->rem_len < 8)
1249 /* remove SETUP packet from data */
1250 sxfer->rem_len -= 8;
1251 sxfer->curr_data += 8;
1257 buffsize = libusb10_get_buffsize(pdev, uxfer);
1258 maxframe = libusb10_get_maxframe(pdev, uxfer);
1260 /* make sure the transfer is opened */
1261 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1262 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1265 libusb20_tr_start(pxfer0);
1269 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1271 /* make sure our event loop spins the done handler */
1273 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1276 /* The following function must be called unlocked */
1279 libusb_submit_transfer(struct libusb_transfer *uxfer)
1281 struct libusb20_transfer *pxfer0;
1282 struct libusb20_transfer *pxfer1;
1283 struct libusb_super_transfer *sxfer;
1284 struct libusb_device *dev;
1289 return (LIBUSB_ERROR_INVALID_PARAM);
1291 if (uxfer->dev_handle == NULL)
1292 return (LIBUSB_ERROR_INVALID_PARAM);
1294 endpoint = uxfer->endpoint;
1297 return (LIBUSB_ERROR_INVALID_PARAM);
1299 dev = libusb_get_device(uxfer->dev_handle);
1301 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1303 sxfer = (struct libusb_super_transfer *)(
1304 (uint8_t *)uxfer - sizeof(*sxfer));
1308 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1309 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1311 if (pxfer0 == NULL || pxfer1 == NULL) {
1312 err = LIBUSB_ERROR_OTHER;
1313 } else if ((sxfer->entry.tqe_prev != NULL) ||
1314 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1315 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1316 err = LIBUSB_ERROR_BUSY;
1319 /* set pending state */
1320 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1322 /* insert transfer into transfer head list */
1323 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1325 /* start work transfers */
1326 libusb10_submit_transfer_sub(
1327 uxfer->dev_handle, endpoint);
1329 err = 0; /* success */
1332 CTX_UNLOCK(dev->ctx);
1334 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1339 /* Asynchronous transfer cancel */
1342 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1344 struct libusb20_transfer *pxfer0;
1345 struct libusb20_transfer *pxfer1;
1346 struct libusb_super_transfer *sxfer;
1347 struct libusb_device *dev;
1352 return (LIBUSB_ERROR_INVALID_PARAM);
1354 /* check if not initialised */
1355 if (uxfer->dev_handle == NULL)
1356 return (LIBUSB_ERROR_NOT_FOUND);
1358 endpoint = uxfer->endpoint;
1361 return (LIBUSB_ERROR_INVALID_PARAM);
1363 dev = libusb_get_device(uxfer->dev_handle);
1365 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1367 sxfer = (struct libusb_super_transfer *)(
1368 (uint8_t *)uxfer - sizeof(*sxfer));
1374 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1375 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1377 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1378 /* only update the transfer status */
1379 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1380 retval = LIBUSB_ERROR_NOT_FOUND;
1381 } else if (sxfer->entry.tqe_prev != NULL) {
1382 /* we are lucky - transfer is on a queue */
1383 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1384 sxfer->entry.tqe_prev = NULL;
1385 libusb10_complete_transfer(NULL,
1386 sxfer, LIBUSB_TRANSFER_CANCELLED);
1387 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1389 retval = LIBUSB_ERROR_NOT_FOUND;
1390 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1391 libusb10_complete_transfer(pxfer0,
1392 sxfer, LIBUSB_TRANSFER_CANCELLED);
1393 libusb20_tr_stop(pxfer0);
1394 /* make sure the queue doesn't stall */
1395 libusb10_submit_transfer_sub(
1396 uxfer->dev_handle, endpoint);
1397 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1398 libusb10_complete_transfer(pxfer1,
1399 sxfer, LIBUSB_TRANSFER_CANCELLED);
1400 libusb20_tr_stop(pxfer1);
1401 /* make sure the queue doesn't stall */
1402 libusb10_submit_transfer_sub(
1403 uxfer->dev_handle, endpoint);
1406 retval = LIBUSB_ERROR_NOT_FOUND;
1409 CTX_UNLOCK(dev->ctx);
1411 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1417 libusb10_cancel_all_transfer(libusb_device *dev)
1423 libusb_cpu_to_le16(uint16_t x)
1425 return (htole16(x));
1429 libusb_le16_to_cpu(uint16_t x)
1431 return (le16toh(x));
1435 libusb_strerror(int code)
1438 return ("Unknown error");