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
35 #include <sys/ioctl.h>
36 #include <sys/filio.h>
37 #include <sys/queue.h>
40 #include "libusb20_desc.h"
41 #include "libusb20_int.h"
45 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
46 struct libusb_context *usbi_default_context = NULL;
50 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
51 static int libusb10_get_maxframe(struct libusb20_device *, libusb_transfer *);
52 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
53 static int libusb10_convert_error(uint8_t status);
54 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
55 static void libusb10_isoc_proxy(struct libusb20_transfer *);
56 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
57 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
58 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
60 /* Library initialisation / deinitialisation */
63 libusb_set_debug(libusb_context *ctx, int level)
65 ctx = GET_CONTEXT(ctx);
71 libusb_init(libusb_context **context)
73 struct libusb_context *ctx;
77 ctx = malloc(sizeof(*ctx));
79 return (LIBUSB_ERROR_INVALID_PARAM);
81 memset(ctx, 0, sizeof(*ctx));
83 debug = getenv("LIBUSB_DEBUG");
85 ctx->debug = atoi(debug);
89 TAILQ_INIT(&ctx->pollfds);
90 TAILQ_INIT(&ctx->tr_done);
92 pthread_mutex_init(&ctx->ctx_lock, NULL);
93 pthread_cond_init(&ctx->ctx_cond, NULL);
95 ctx->ctx_handler = NO_THREAD;
97 ret = pipe(ctx->ctrl_pipe);
99 pthread_mutex_destroy(&ctx->ctx_lock);
100 pthread_cond_destroy(&ctx->ctx_cond);
102 return (LIBUSB_ERROR_OTHER);
104 /* set non-blocking mode on the control pipe to avoid deadlock */
106 ioctl(ctx->ctrl_pipe[0], FIONBIO, &ret);
108 ioctl(ctx->ctrl_pipe[1], FIONBIO, &ret);
110 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
112 pthread_mutex_lock(&default_context_lock);
113 if (usbi_default_context == NULL) {
114 usbi_default_context = ctx;
116 pthread_mutex_unlock(&default_context_lock);
121 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
127 libusb_exit(libusb_context *ctx)
129 ctx = GET_CONTEXT(ctx);
134 /* XXX cleanup devices */
136 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
137 close(ctx->ctrl_pipe[0]);
138 close(ctx->ctrl_pipe[1]);
139 pthread_mutex_destroy(&ctx->ctx_lock);
140 pthread_cond_destroy(&ctx->ctx_cond);
142 pthread_mutex_lock(&default_context_lock);
143 if (ctx == usbi_default_context) {
144 usbi_default_context = NULL;
146 pthread_mutex_unlock(&default_context_lock);
151 /* Device handling and initialisation. */
154 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
156 struct libusb20_backend *usb_backend;
157 struct libusb20_device *pdev;
158 struct libusb_device *dev;
161 ctx = GET_CONTEXT(ctx);
164 return (LIBUSB_ERROR_INVALID_PARAM);
167 return (LIBUSB_ERROR_INVALID_PARAM);
169 usb_backend = libusb20_be_alloc_default();
170 if (usb_backend == NULL)
171 return (LIBUSB_ERROR_NO_MEM);
173 /* figure out how many USB devices are present */
176 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
179 /* allocate device pointer list */
180 *list = malloc((i + 1) * sizeof(void *));
182 libusb20_be_free(usb_backend);
183 return (LIBUSB_ERROR_NO_MEM);
185 /* create libusb v1.0 compliant devices */
187 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
188 /* get device into libUSB v1.0 list */
189 libusb20_be_dequeue_device(usb_backend, pdev);
191 dev = malloc(sizeof(*dev));
194 libusb_unref_device((*list)[i - 1]);
199 libusb20_be_free(usb_backend);
200 return (LIBUSB_ERROR_NO_MEM);
202 memset(dev, 0, sizeof(*dev));
204 /* init transfer queues */
205 TAILQ_INIT(&dev->tr_head);
207 /* set context we belong to */
210 /* link together the two structures */
212 pdev->privLuData = dev;
214 (*list)[i] = libusb_ref_device(dev);
219 libusb20_be_free(usb_backend);
224 libusb_free_device_list(libusb_device **list, int unref_devices)
229 return; /* be NULL safe */
232 for (i = 0; list[i] != NULL; i++)
233 libusb_unref_device(list[i]);
239 libusb_get_bus_number(libusb_device *dev)
242 return (0); /* should not happen */
243 return (libusb20_dev_get_bus_number(dev->os_priv));
247 libusb_get_device_address(libusb_device *dev)
250 return (0); /* should not happen */
251 return (libusb20_dev_get_address(dev->os_priv));
255 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
257 struct libusb_config_descriptor *pdconf;
258 struct libusb_interface *pinf;
259 struct libusb_interface_descriptor *pdinf;
260 struct libusb_endpoint_descriptor *pdend;
267 return (LIBUSB_ERROR_NO_DEVICE);
269 ret = libusb_get_active_config_descriptor(dev, &pdconf);
273 ret = LIBUSB_ERROR_NOT_FOUND;
274 for (i = 0; i < pdconf->bNumInterfaces; i++) {
275 pinf = &pdconf->interface[i];
276 for (j = 0; j < pinf->num_altsetting; j++) {
277 pdinf = &pinf->altsetting[j];
278 for (k = 0; k < pdinf->bNumEndpoints; k++) {
279 pdend = &pdinf->endpoint[k];
280 if (pdend->bEndpointAddress == endpoint) {
281 ret = pdend->wMaxPacketSize;
289 libusb_free_config_descriptor(pdconf);
294 libusb_ref_device(libusb_device *dev)
297 return (NULL); /* be NULL safe */
301 CTX_UNLOCK(dev->ctx);
307 libusb_unref_device(libusb_device *dev)
310 return; /* be NULL safe */
314 CTX_UNLOCK(dev->ctx);
316 if (dev->refcnt == 0) {
317 libusb20_dev_free(dev->os_priv);
323 libusb_open(libusb_device *dev, libusb_device_handle **devh)
325 libusb_context *ctx = dev->ctx;
326 struct libusb20_device *pdev = dev->os_priv;
331 return (LIBUSB_ERROR_INVALID_PARAM);
333 /* set default device handle value */
336 dev = libusb_ref_device(dev);
338 return (LIBUSB_ERROR_INVALID_PARAM);
340 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
342 libusb_unref_device(dev);
343 return (LIBUSB_ERROR_NO_MEM);
345 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
346 POLLOUT | POLLRDNORM | POLLWRNORM);
348 /* make sure our event loop detects the new device */
350 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
351 if (err < sizeof(dummy)) {
352 /* ignore error, if any */
353 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
360 libusb_device_handle *
361 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
364 struct libusb_device **devs;
365 struct libusb20_device *pdev;
366 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
370 ctx = GET_CONTEXT(ctx);
372 return (NULL); /* be NULL safe */
374 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
376 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
381 for (j = 0; j < i; j++) {
382 pdev = devs[j]->os_priv;
383 pdesc = libusb20_dev_get_device_desc(pdev);
385 * NOTE: The USB library will automatically swap the
386 * fields in the device descriptor to be of host
389 if (pdesc->idVendor == vendor_id &&
390 pdesc->idProduct == product_id) {
391 if (libusb_open(devs[j], &pdev) < 0)
397 libusb_free_device_list(devs, 1);
398 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
403 libusb_close(struct libusb20_device *pdev)
406 struct libusb_device *dev;
411 return; /* be NULL safe */
413 dev = libusb_get_device(pdev);
416 libusb10_remove_pollfd(ctx, &dev->dev_poll);
418 libusb20_dev_close(pdev);
419 libusb_unref_device(dev);
421 /* make sure our event loop detects the closed device */
423 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
424 if (err < sizeof(dummy)) {
425 /* ignore error, if any */
426 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
431 libusb_get_device(struct libusb20_device *pdev)
435 return ((libusb_device *)pdev->privLuData);
439 libusb_get_configuration(struct libusb20_device *pdev, int *config)
441 struct libusb20_config *pconf;
443 if (pdev == NULL || config == NULL)
444 return (LIBUSB_ERROR_INVALID_PARAM);
446 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
448 return (LIBUSB_ERROR_NO_MEM);
450 *config = pconf->desc.bConfigurationValue;
458 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
460 struct libusb20_config *pconf;
461 struct libusb_device *dev;
465 dev = libusb_get_device(pdev);
468 return (LIBUSB_ERROR_INVALID_PARAM);
470 if (configuration < 1) {
474 for (i = 0; i != 255; i++) {
477 pconf = libusb20_dev_alloc_config(pdev, i);
479 return (LIBUSB_ERROR_INVALID_PARAM);
480 found = (pconf->desc.bConfigurationValue
487 return (LIBUSB_ERROR_INVALID_PARAM);
492 libusb10_cancel_all_transfer(dev);
494 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
496 err = libusb20_dev_set_config_index(pdev, i);
498 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
499 POLLOUT | POLLRDNORM | POLLWRNORM);
501 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
505 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
510 dev = libusb_get_device(pdev);
512 return (LIBUSB_ERROR_INVALID_PARAM);
514 if (interface_number < 0 || interface_number > 31)
515 return (LIBUSB_ERROR_INVALID_PARAM);
518 if (dev->claimed_interfaces & (1 << interface_number))
519 err = LIBUSB_ERROR_BUSY;
522 dev->claimed_interfaces |= (1 << interface_number);
523 CTX_UNLOCK(dev->ctx);
528 libusb_release_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_NOT_FOUND;
545 dev->claimed_interfaces &= ~(1 << interface_number);
546 CTX_UNLOCK(dev->ctx);
551 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
552 int interface_number, int alternate_setting)
557 dev = libusb_get_device(pdev);
559 return (LIBUSB_ERROR_INVALID_PARAM);
561 if (interface_number < 0 || interface_number > 31)
562 return (LIBUSB_ERROR_INVALID_PARAM);
565 if (!(dev->claimed_interfaces & (1 << interface_number)))
566 err = LIBUSB_ERROR_NOT_FOUND;
567 CTX_UNLOCK(dev->ctx);
572 libusb10_cancel_all_transfer(dev);
574 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
576 err = libusb20_dev_set_alt_index(pdev,
577 interface_number, alternate_setting);
579 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
580 pdev, libusb20_dev_get_fd(pdev),
581 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
583 return (err ? LIBUSB_ERROR_OTHER : 0);
586 static struct libusb20_transfer *
587 libusb10_get_transfer(struct libusb20_device *pdev,
588 uint8_t endpoint, uint8_t index)
590 index &= 1; /* double buffering */
592 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
594 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
595 /* this is an IN endpoint */
598 return (libusb20_tr_get_pointer(pdev, index));
602 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
604 struct libusb20_transfer *xfer;
605 struct libusb_device *dev;
608 xfer = libusb10_get_transfer(pdev, endpoint, 0);
610 return (LIBUSB_ERROR_INVALID_PARAM);
612 dev = libusb_get_device(pdev);
615 err = libusb20_tr_open(xfer, 0, 0, endpoint);
616 CTX_UNLOCK(dev->ctx);
618 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
619 return (LIBUSB_ERROR_OTHER);
621 libusb20_tr_clear_stall_sync(xfer);
623 /* check if we opened the transfer */
626 libusb20_tr_close(xfer);
627 CTX_UNLOCK(dev->ctx);
629 return (0); /* success */
633 libusb_reset_device(struct libusb20_device *pdev)
638 dev = libusb_get_device(pdev);
640 return (LIBUSB20_ERROR_INVALID_PARAM);
642 libusb10_cancel_all_transfer(dev);
644 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
646 err = libusb20_dev_reset(pdev);
648 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
649 pdev, libusb20_dev_get_fd(pdev),
650 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
652 return (err ? LIBUSB_ERROR_OTHER : 0);
656 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
659 return (LIBUSB_ERROR_INVALID_PARAM);
661 return (libusb20_dev_kernel_driver_active(
666 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
671 return (LIBUSB_ERROR_INVALID_PARAM);
673 err = libusb20_dev_detach_kernel_driver(
676 return (err ? LIBUSB20_ERROR_OTHER : 0);
680 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
683 return (LIBUSB_ERROR_INVALID_PARAM);
684 /* stub - currently not supported by libusb20 */
688 /* Asynchronous device I/O */
690 struct libusb_transfer *
691 libusb_alloc_transfer(int iso_packets)
693 struct libusb_transfer *uxfer;
694 struct libusb_super_transfer *sxfer;
697 len = sizeof(struct libusb_transfer) +
698 sizeof(struct libusb_super_transfer) +
699 (iso_packets * sizeof(libusb_iso_packet_descriptor));
705 memset(sxfer, 0, len);
707 uxfer = (struct libusb_transfer *)(
708 ((uint8_t *)sxfer) + sizeof(*sxfer));
710 /* set default value */
711 uxfer->num_iso_packets = iso_packets;
717 libusb_free_transfer(struct libusb_transfer *uxfer)
719 struct libusb_super_transfer *sxfer;
722 return; /* be NULL safe */
724 sxfer = (struct libusb_super_transfer *)(
725 (uint8_t *)uxfer - sizeof(*sxfer));
731 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
736 usb_speed = libusb20_dev_get_speed(pdev);
738 switch (xfer->type) {
739 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
741 case LIBUSB20_SPEED_LOW:
742 case LIBUSB20_SPEED_FULL:
750 case LIBUSB_TRANSFER_TYPE_CONTROL:
761 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
766 usb_speed = libusb20_dev_get_speed(pdev);
768 switch (xfer->type) {
769 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
770 ret = 0; /* kernel will auto-select */
772 case LIBUSB_TRANSFER_TYPE_CONTROL:
777 case LIBUSB20_SPEED_LOW:
780 case LIBUSB20_SPEED_FULL:
793 libusb10_convert_error(uint8_t status)
798 case LIBUSB20_TRANSFER_START:
799 case LIBUSB20_TRANSFER_COMPLETED:
800 return (LIBUSB_TRANSFER_COMPLETED);
801 case LIBUSB20_TRANSFER_OVERFLOW:
802 return (LIBUSB_TRANSFER_OVERFLOW);
803 case LIBUSB20_TRANSFER_NO_DEVICE:
804 return (LIBUSB_TRANSFER_NO_DEVICE);
805 case LIBUSB20_TRANSFER_STALL:
806 return (LIBUSB_TRANSFER_STALL);
807 case LIBUSB20_TRANSFER_CANCELLED:
808 return (LIBUSB_TRANSFER_CANCELLED);
809 case LIBUSB20_TRANSFER_TIMED_OUT:
810 return (LIBUSB_TRANSFER_TIMED_OUT);
812 return (LIBUSB_TRANSFER_ERROR);
816 /* This function must be called locked */
819 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
820 struct libusb_super_transfer *sxfer, int status)
822 struct libusb_transfer *uxfer;
823 struct libusb_device *dev;
825 uxfer = (struct libusb_transfer *)(
826 ((uint8_t *)sxfer) + sizeof(*sxfer));
829 libusb20_tr_set_priv_sc1(pxfer, NULL);
831 uxfer->status = status;
833 dev = libusb_get_device(uxfer->dev_handle);
835 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
838 /* This function must be called locked */
841 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
843 struct libusb_super_transfer *sxfer;
844 struct libusb_transfer *uxfer;
846 uint16_t iso_packets;
851 status = libusb20_tr_get_status(pxfer);
852 sxfer = libusb20_tr_get_priv_sc1(pxfer);
853 actlen = libusb20_tr_get_actual_length(pxfer);
854 iso_packets = libusb20_tr_get_max_frames(pxfer);
857 return; /* cancelled - nothing to do */
859 uxfer = (struct libusb_transfer *)(
860 ((uint8_t *)sxfer) + sizeof(*sxfer));
862 if (iso_packets > uxfer->num_iso_packets)
863 iso_packets = uxfer->num_iso_packets;
865 if (iso_packets == 0)
866 return; /* nothing to do */
868 /* make sure that the number of ISOCHRONOUS packets is valid */
869 uxfer->num_iso_packets = iso_packets;
871 flags = uxfer->flags;
874 case LIBUSB20_TRANSFER_COMPLETED:
876 /* update actual length */
877 uxfer->actual_length = actlen;
878 for (i = 0; i != iso_packets; i++) {
879 uxfer->iso_packet_desc[i].actual_length =
880 libusb20_tr_get_length(pxfer, i);
882 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
885 case LIBUSB20_TRANSFER_START:
887 /* setup length(s) */
889 for (i = 0; i != iso_packets; i++) {
890 libusb20_tr_setup_isoc(pxfer,
891 &uxfer->buffer[actlen],
892 uxfer->iso_packet_desc[i].length, i);
893 actlen += uxfer->iso_packet_desc[i].length;
899 libusb20_tr_set_total_frames(pxfer, iso_packets);
900 libusb20_tr_submit(pxfer);
902 /* fork another USB transfer, if any */
903 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
907 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
912 /* This function must be called locked */
915 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
917 struct libusb_super_transfer *sxfer;
918 struct libusb_transfer *uxfer;
924 status = libusb20_tr_get_status(pxfer);
925 sxfer = libusb20_tr_get_priv_sc1(pxfer);
926 max_bulk = libusb20_tr_get_max_total_length(pxfer);
927 actlen = libusb20_tr_get_actual_length(pxfer);
930 return; /* cancelled - nothing to do */
932 uxfer = (struct libusb_transfer *)(
933 ((uint8_t *)sxfer) + sizeof(*sxfer));
935 flags = uxfer->flags;
938 case LIBUSB20_TRANSFER_COMPLETED:
940 uxfer->actual_length += actlen;
942 /* check for short packet */
943 if (sxfer->last_len != actlen) {
944 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
945 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
947 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
951 /* check for end of data */
952 if (sxfer->rem_len == 0) {
953 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
958 case LIBUSB20_TRANSFER_START:
959 if (max_bulk > sxfer->rem_len) {
960 max_bulk = sxfer->rem_len;
962 /* setup new BULK or INTERRUPT transaction */
963 libusb20_tr_setup_bulk(pxfer,
964 sxfer->curr_data, max_bulk, uxfer->timeout);
966 /* update counters */
967 sxfer->last_len = max_bulk;
968 sxfer->curr_data += max_bulk;
969 sxfer->rem_len -= max_bulk;
971 libusb20_tr_submit(pxfer);
973 /* check if we can fork another USB transfer */
974 if (sxfer->rem_len == 0)
975 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
979 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
984 /* This function must be called locked */
987 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
989 struct libusb_super_transfer *sxfer;
990 struct libusb_transfer *uxfer;
996 status = libusb20_tr_get_status(pxfer);
997 sxfer = libusb20_tr_get_priv_sc1(pxfer);
998 max_bulk = libusb20_tr_get_max_total_length(pxfer);
999 actlen = libusb20_tr_get_actual_length(pxfer);
1002 return; /* cancelled - nothing to do */
1004 uxfer = (struct libusb_transfer *)(
1005 ((uint8_t *)sxfer) + sizeof(*sxfer));
1007 flags = uxfer->flags;
1010 case LIBUSB20_TRANSFER_COMPLETED:
1012 uxfer->actual_length += actlen;
1014 /* subtract length of SETUP packet, if any */
1015 actlen -= libusb20_tr_get_length(pxfer, 0);
1017 /* check for short packet */
1018 if (sxfer->last_len != actlen) {
1019 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1020 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1022 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1026 /* check for end of data */
1027 if (sxfer->rem_len == 0) {
1028 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1033 case LIBUSB20_TRANSFER_START:
1034 if (max_bulk > sxfer->rem_len) {
1035 max_bulk = sxfer->rem_len;
1037 /* setup new CONTROL transaction */
1038 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1039 /* next fragment - don't send SETUP packet */
1040 libusb20_tr_set_length(pxfer, 0, 0);
1042 /* first fragment - send SETUP packet */
1043 libusb20_tr_set_length(pxfer, 8, 0);
1044 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1047 if (max_bulk != 0) {
1048 libusb20_tr_set_length(pxfer, max_bulk, 1);
1049 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1050 libusb20_tr_set_total_frames(pxfer, 2);
1052 libusb20_tr_set_total_frames(pxfer, 1);
1055 /* update counters */
1056 sxfer->last_len = max_bulk;
1057 sxfer->curr_data += max_bulk;
1058 sxfer->rem_len -= max_bulk;
1060 libusb20_tr_submit(pxfer);
1062 /* check if we can fork another USB transfer */
1063 if (sxfer->rem_len == 0)
1064 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1068 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1073 /* The following function must be called locked */
1076 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1078 struct libusb20_transfer *pxfer0;
1079 struct libusb20_transfer *pxfer1;
1080 struct libusb_super_transfer *sxfer;
1081 struct libusb_transfer *uxfer;
1082 struct libusb_device *dev;
1089 dev = libusb_get_device(pdev);
1091 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1092 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1094 if (pxfer0 == NULL || pxfer1 == NULL)
1095 return; /* shouldn't happen */
1098 if (libusb20_tr_pending(pxfer0))
1100 if (libusb20_tr_pending(pxfer1))
1105 /* wait till one of the transfers complete */
1108 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1110 return; /* cannot queue another one */
1111 /* swap transfers */
1115 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1117 return; /* cannot queue another one */
1118 /* swap transfers */
1125 /* find next transfer on same endpoint */
1126 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1128 uxfer = (struct libusb_transfer *)(
1129 ((uint8_t *)sxfer) + sizeof(*sxfer));
1131 if (uxfer->endpoint == endpoint) {
1132 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1133 sxfer->entry.tqe_prev = NULL;
1137 return; /* success */
1141 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1142 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1144 /* reset super transfer state */
1145 sxfer->rem_len = uxfer->length;
1146 sxfer->curr_data = uxfer->buffer;
1147 uxfer->actual_length = 0;
1149 switch (uxfer->type) {
1150 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1151 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1153 case LIBUSB_TRANSFER_TYPE_BULK:
1154 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1155 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1157 case LIBUSB_TRANSFER_TYPE_CONTROL:
1158 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1159 if (sxfer->rem_len < 8)
1162 /* remove SETUP packet from data */
1163 sxfer->rem_len -= 8;
1164 sxfer->curr_data += 8;
1170 buffsize = libusb10_get_buffsize(pdev, uxfer);
1171 maxframe = libusb10_get_maxframe(pdev, uxfer);
1173 /* make sure the transfer is opened */
1174 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1175 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1178 libusb20_tr_start(pxfer0);
1182 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1184 /* make sure our event loop spins the done handler */
1186 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1189 /* The following function must be called unlocked */
1192 libusb_submit_transfer(struct libusb_transfer *uxfer)
1194 struct libusb20_transfer *pxfer0;
1195 struct libusb20_transfer *pxfer1;
1196 struct libusb_super_transfer *sxfer;
1197 struct libusb_device *dev;
1198 unsigned int endpoint;
1202 return (LIBUSB_ERROR_INVALID_PARAM);
1204 if (uxfer->dev_handle == NULL)
1205 return (LIBUSB_ERROR_INVALID_PARAM);
1207 endpoint = uxfer->endpoint;
1210 return (LIBUSB_ERROR_INVALID_PARAM);
1212 dev = libusb_get_device(uxfer->dev_handle);
1214 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1216 sxfer = (struct libusb_super_transfer *)(
1217 (uint8_t *)uxfer - sizeof(*sxfer));
1221 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1222 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1224 if (pxfer0 == NULL || pxfer1 == NULL) {
1225 err = LIBUSB_ERROR_OTHER;
1226 } else if ((sxfer->entry.tqe_prev != NULL) ||
1227 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1228 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1229 err = LIBUSB_ERROR_BUSY;
1231 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1233 libusb10_submit_transfer_sub(
1234 uxfer->dev_handle, endpoint);
1236 err = 0; /* success */
1239 CTX_UNLOCK(dev->ctx);
1241 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1246 /* Asynchronous transfer cancel */
1249 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1251 struct libusb20_transfer *pxfer0;
1252 struct libusb20_transfer *pxfer1;
1253 struct libusb_super_transfer *sxfer;
1254 struct libusb_device *dev;
1255 unsigned int endpoint;
1258 return (LIBUSB_ERROR_INVALID_PARAM);
1260 if (uxfer->dev_handle == NULL)
1261 return (LIBUSB_ERROR_INVALID_PARAM);
1263 endpoint = uxfer->endpoint;
1266 return (LIBUSB_ERROR_INVALID_PARAM);
1268 dev = libusb_get_device(uxfer->dev_handle);
1270 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1272 sxfer = (struct libusb_super_transfer *)(
1273 (uint8_t *)uxfer - sizeof(*sxfer));
1277 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1278 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1280 if (sxfer->entry.tqe_prev != NULL) {
1281 /* we are lucky - transfer is on a queue */
1282 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1283 sxfer->entry.tqe_prev = NULL;
1284 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_CANCELLED);
1285 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1287 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1288 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_CANCELLED);
1289 libusb20_tr_stop(pxfer0);
1290 /* make sure the queue doesn't stall */
1291 libusb10_submit_transfer_sub(
1292 uxfer->dev_handle, endpoint);
1293 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1294 libusb10_complete_transfer(pxfer1, sxfer, LIBUSB_TRANSFER_CANCELLED);
1295 libusb20_tr_stop(pxfer1);
1296 /* make sure the queue doesn't stall */
1297 libusb10_submit_transfer_sub(
1298 uxfer->dev_handle, endpoint);
1303 CTX_UNLOCK(dev->ctx);
1305 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1311 libusb10_cancel_all_transfer(libusb_device *dev)