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>
38 #include <sys/endian.h>
41 #include "libusb20_desc.h"
42 #include "libusb20_int.h"
46 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
47 struct libusb_context *usbi_default_context = NULL;
51 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
52 static int libusb10_get_maxframe(struct libusb20_device *, libusb_transfer *);
53 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
54 static int libusb10_convert_error(uint8_t status);
55 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
56 static void libusb10_isoc_proxy(struct libusb20_transfer *);
57 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
58 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
59 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
61 /* Library initialisation / deinitialisation */
64 libusb_set_debug(libusb_context *ctx, int level)
66 ctx = GET_CONTEXT(ctx);
72 libusb_init(libusb_context **context)
74 struct libusb_context *ctx;
78 ctx = malloc(sizeof(*ctx));
80 return (LIBUSB_ERROR_INVALID_PARAM);
82 memset(ctx, 0, sizeof(*ctx));
84 debug = getenv("LIBUSB_DEBUG");
86 ctx->debug = atoi(debug);
90 TAILQ_INIT(&ctx->pollfds);
91 TAILQ_INIT(&ctx->tr_done);
93 pthread_mutex_init(&ctx->ctx_lock, NULL);
94 pthread_cond_init(&ctx->ctx_cond, NULL);
96 ctx->ctx_handler = NO_THREAD;
98 ret = pipe(ctx->ctrl_pipe);
100 pthread_mutex_destroy(&ctx->ctx_lock);
101 pthread_cond_destroy(&ctx->ctx_cond);
103 return (LIBUSB_ERROR_OTHER);
105 /* set non-blocking mode on the control pipe to avoid deadlock */
107 ioctl(ctx->ctrl_pipe[0], FIONBIO, &ret);
109 ioctl(ctx->ctrl_pipe[1], FIONBIO, &ret);
111 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
113 pthread_mutex_lock(&default_context_lock);
114 if (usbi_default_context == NULL) {
115 usbi_default_context = ctx;
117 pthread_mutex_unlock(&default_context_lock);
122 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
128 libusb_exit(libusb_context *ctx)
130 ctx = GET_CONTEXT(ctx);
135 /* XXX cleanup devices */
137 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
138 close(ctx->ctrl_pipe[0]);
139 close(ctx->ctrl_pipe[1]);
140 pthread_mutex_destroy(&ctx->ctx_lock);
141 pthread_cond_destroy(&ctx->ctx_cond);
143 pthread_mutex_lock(&default_context_lock);
144 if (ctx == usbi_default_context) {
145 usbi_default_context = NULL;
147 pthread_mutex_unlock(&default_context_lock);
152 /* Device handling and initialisation. */
155 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
157 struct libusb20_backend *usb_backend;
158 struct libusb20_device *pdev;
159 struct libusb_device *dev;
162 ctx = GET_CONTEXT(ctx);
165 return (LIBUSB_ERROR_INVALID_PARAM);
168 return (LIBUSB_ERROR_INVALID_PARAM);
170 usb_backend = libusb20_be_alloc_default();
171 if (usb_backend == NULL)
172 return (LIBUSB_ERROR_NO_MEM);
174 /* figure out how many USB devices are present */
177 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
180 /* allocate device pointer list */
181 *list = malloc((i + 1) * sizeof(void *));
183 libusb20_be_free(usb_backend);
184 return (LIBUSB_ERROR_NO_MEM);
186 /* create libusb v1.0 compliant devices */
188 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
190 dev = malloc(sizeof(*dev));
193 libusb_unref_device((*list)[i - 1]);
198 libusb20_be_free(usb_backend);
199 return (LIBUSB_ERROR_NO_MEM);
202 /* get device into libUSB v1.0 list */
203 libusb20_be_dequeue_device(usb_backend, pdev);
205 memset(dev, 0, sizeof(*dev));
207 /* init transfer queues */
208 TAILQ_INIT(&dev->tr_head);
210 /* set context we belong to */
213 /* link together the two structures */
215 pdev->privLuData = dev;
217 (*list)[i] = libusb_ref_device(dev);
222 libusb20_be_free(usb_backend);
227 libusb_free_device_list(libusb_device **list, int unref_devices)
232 return; /* be NULL safe */
235 for (i = 0; list[i] != NULL; i++)
236 libusb_unref_device(list[i]);
242 libusb_get_bus_number(libusb_device *dev)
245 return (0); /* should not happen */
246 return (libusb20_dev_get_bus_number(dev->os_priv));
250 libusb_get_device_address(libusb_device *dev)
253 return (0); /* should not happen */
254 return (libusb20_dev_get_address(dev->os_priv));
258 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
260 struct libusb_config_descriptor *pdconf;
261 struct libusb_interface *pinf;
262 struct libusb_interface_descriptor *pdinf;
263 struct libusb_endpoint_descriptor *pdend;
270 return (LIBUSB_ERROR_NO_DEVICE);
272 ret = libusb_get_active_config_descriptor(dev, &pdconf);
276 ret = LIBUSB_ERROR_NOT_FOUND;
277 for (i = 0; i < pdconf->bNumInterfaces; i++) {
278 pinf = &pdconf->interface[i];
279 for (j = 0; j < pinf->num_altsetting; j++) {
280 pdinf = &pinf->altsetting[j];
281 for (k = 0; k < pdinf->bNumEndpoints; k++) {
282 pdend = &pdinf->endpoint[k];
283 if (pdend->bEndpointAddress == endpoint) {
284 ret = pdend->wMaxPacketSize;
292 libusb_free_config_descriptor(pdconf);
297 libusb_ref_device(libusb_device *dev)
300 return (NULL); /* be NULL safe */
304 CTX_UNLOCK(dev->ctx);
310 libusb_unref_device(libusb_device *dev)
313 return; /* be NULL safe */
317 CTX_UNLOCK(dev->ctx);
319 if (dev->refcnt == 0) {
320 libusb20_dev_free(dev->os_priv);
326 libusb_open(libusb_device *dev, libusb_device_handle **devh)
328 libusb_context *ctx = dev->ctx;
329 struct libusb20_device *pdev = dev->os_priv;
334 return (LIBUSB_ERROR_INVALID_PARAM);
336 /* set default device handle value */
339 dev = libusb_ref_device(dev);
341 return (LIBUSB_ERROR_INVALID_PARAM);
343 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
345 libusb_unref_device(dev);
346 return (LIBUSB_ERROR_NO_MEM);
348 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
349 POLLOUT | POLLRDNORM | POLLWRNORM);
351 /* make sure our event loop detects the new device */
353 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
354 if (err < sizeof(dummy)) {
355 /* ignore error, if any */
356 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
363 libusb_device_handle *
364 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
367 struct libusb_device **devs;
368 struct libusb20_device *pdev;
369 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
373 ctx = GET_CONTEXT(ctx);
375 return (NULL); /* be NULL safe */
377 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
379 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
384 for (j = 0; j < i; j++) {
385 pdev = devs[j]->os_priv;
386 pdesc = libusb20_dev_get_device_desc(pdev);
388 * NOTE: The USB library will automatically swap the
389 * fields in the device descriptor to be of host
392 if (pdesc->idVendor == vendor_id &&
393 pdesc->idProduct == product_id) {
394 if (libusb_open(devs[j], &pdev) < 0)
400 libusb_free_device_list(devs, 1);
401 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
406 libusb_close(struct libusb20_device *pdev)
409 struct libusb_device *dev;
414 return; /* be NULL safe */
416 dev = libusb_get_device(pdev);
419 libusb10_remove_pollfd(ctx, &dev->dev_poll);
421 libusb20_dev_close(pdev);
423 /* unref will free the "pdev" when the refcount reaches zero */
424 libusb_unref_device(dev);
426 /* make sure our event loop detects the closed device */
428 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
429 if (err < sizeof(dummy)) {
430 /* ignore error, if any */
431 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
436 libusb_get_device(struct libusb20_device *pdev)
440 return ((libusb_device *)pdev->privLuData);
444 libusb_get_configuration(struct libusb20_device *pdev, int *config)
446 struct libusb20_config *pconf;
448 if (pdev == NULL || config == NULL)
449 return (LIBUSB_ERROR_INVALID_PARAM);
451 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
453 return (LIBUSB_ERROR_NO_MEM);
455 *config = pconf->desc.bConfigurationValue;
463 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
465 struct libusb20_config *pconf;
466 struct libusb_device *dev;
470 dev = libusb_get_device(pdev);
473 return (LIBUSB_ERROR_INVALID_PARAM);
475 if (configuration < 1) {
479 for (i = 0; i != 255; i++) {
482 pconf = libusb20_dev_alloc_config(pdev, i);
484 return (LIBUSB_ERROR_INVALID_PARAM);
485 found = (pconf->desc.bConfigurationValue
492 return (LIBUSB_ERROR_INVALID_PARAM);
497 libusb10_cancel_all_transfer(dev);
499 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
501 err = libusb20_dev_set_config_index(pdev, i);
503 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
504 POLLOUT | POLLRDNORM | POLLWRNORM);
506 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
510 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
515 dev = libusb_get_device(pdev);
517 return (LIBUSB_ERROR_INVALID_PARAM);
519 if (interface_number < 0 || interface_number > 31)
520 return (LIBUSB_ERROR_INVALID_PARAM);
523 if (dev->claimed_interfaces & (1 << interface_number))
524 err = LIBUSB_ERROR_BUSY;
527 dev->claimed_interfaces |= (1 << interface_number);
528 CTX_UNLOCK(dev->ctx);
533 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
538 dev = libusb_get_device(pdev);
540 return (LIBUSB_ERROR_INVALID_PARAM);
542 if (interface_number < 0 || interface_number > 31)
543 return (LIBUSB_ERROR_INVALID_PARAM);
546 if (!(dev->claimed_interfaces & (1 << interface_number)))
547 err = LIBUSB_ERROR_NOT_FOUND;
550 dev->claimed_interfaces &= ~(1 << interface_number);
551 CTX_UNLOCK(dev->ctx);
556 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
557 int interface_number, int alternate_setting)
562 dev = libusb_get_device(pdev);
564 return (LIBUSB_ERROR_INVALID_PARAM);
566 if (interface_number < 0 || interface_number > 31)
567 return (LIBUSB_ERROR_INVALID_PARAM);
570 if (!(dev->claimed_interfaces & (1 << interface_number)))
571 err = LIBUSB_ERROR_NOT_FOUND;
572 CTX_UNLOCK(dev->ctx);
577 libusb10_cancel_all_transfer(dev);
579 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
581 err = libusb20_dev_set_alt_index(pdev,
582 interface_number, alternate_setting);
584 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
585 pdev, libusb20_dev_get_fd(pdev),
586 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
588 return (err ? LIBUSB_ERROR_OTHER : 0);
591 static struct libusb20_transfer *
592 libusb10_get_transfer(struct libusb20_device *pdev,
593 uint8_t endpoint, uint8_t index)
595 index &= 1; /* double buffering */
597 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
599 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
600 /* this is an IN endpoint */
603 return (libusb20_tr_get_pointer(pdev, index));
607 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
609 struct libusb20_transfer *xfer;
610 struct libusb_device *dev;
613 xfer = libusb10_get_transfer(pdev, endpoint, 0);
615 return (LIBUSB_ERROR_INVALID_PARAM);
617 dev = libusb_get_device(pdev);
620 err = libusb20_tr_open(xfer, 0, 0, endpoint);
621 CTX_UNLOCK(dev->ctx);
623 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
624 return (LIBUSB_ERROR_OTHER);
626 libusb20_tr_clear_stall_sync(xfer);
628 /* check if we opened the transfer */
631 libusb20_tr_close(xfer);
632 CTX_UNLOCK(dev->ctx);
634 return (0); /* success */
638 libusb_reset_device(struct libusb20_device *pdev)
643 dev = libusb_get_device(pdev);
645 return (LIBUSB20_ERROR_INVALID_PARAM);
647 libusb10_cancel_all_transfer(dev);
649 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
651 err = libusb20_dev_reset(pdev);
653 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
654 pdev, libusb20_dev_get_fd(pdev),
655 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
657 return (err ? LIBUSB_ERROR_OTHER : 0);
661 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
664 return (LIBUSB_ERROR_INVALID_PARAM);
666 return (libusb20_dev_kernel_driver_active(
671 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
676 return (LIBUSB_ERROR_INVALID_PARAM);
678 err = libusb20_dev_detach_kernel_driver(
681 return (err ? LIBUSB20_ERROR_OTHER : 0);
685 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
688 return (LIBUSB_ERROR_INVALID_PARAM);
689 /* stub - currently not supported by libusb20 */
693 /* Asynchronous device I/O */
695 struct libusb_transfer *
696 libusb_alloc_transfer(int iso_packets)
698 struct libusb_transfer *uxfer;
699 struct libusb_super_transfer *sxfer;
702 len = sizeof(struct libusb_transfer) +
703 sizeof(struct libusb_super_transfer) +
704 (iso_packets * sizeof(libusb_iso_packet_descriptor));
710 memset(sxfer, 0, len);
712 uxfer = (struct libusb_transfer *)(
713 ((uint8_t *)sxfer) + sizeof(*sxfer));
715 /* set default value */
716 uxfer->num_iso_packets = iso_packets;
722 libusb_free_transfer(struct libusb_transfer *uxfer)
724 struct libusb_super_transfer *sxfer;
727 return; /* be NULL safe */
729 sxfer = (struct libusb_super_transfer *)(
730 (uint8_t *)uxfer - sizeof(*sxfer));
736 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
741 usb_speed = libusb20_dev_get_speed(pdev);
743 switch (xfer->type) {
744 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
746 case LIBUSB20_SPEED_LOW:
747 case LIBUSB20_SPEED_FULL:
755 case LIBUSB_TRANSFER_TYPE_CONTROL:
766 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
771 usb_speed = libusb20_dev_get_speed(pdev);
773 switch (xfer->type) {
774 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
775 ret = 0; /* kernel will auto-select */
777 case LIBUSB_TRANSFER_TYPE_CONTROL:
782 case LIBUSB20_SPEED_LOW:
785 case LIBUSB20_SPEED_FULL:
798 libusb10_convert_error(uint8_t status)
803 case LIBUSB20_TRANSFER_START:
804 case LIBUSB20_TRANSFER_COMPLETED:
805 return (LIBUSB_TRANSFER_COMPLETED);
806 case LIBUSB20_TRANSFER_OVERFLOW:
807 return (LIBUSB_TRANSFER_OVERFLOW);
808 case LIBUSB20_TRANSFER_NO_DEVICE:
809 return (LIBUSB_TRANSFER_NO_DEVICE);
810 case LIBUSB20_TRANSFER_STALL:
811 return (LIBUSB_TRANSFER_STALL);
812 case LIBUSB20_TRANSFER_CANCELLED:
813 return (LIBUSB_TRANSFER_CANCELLED);
814 case LIBUSB20_TRANSFER_TIMED_OUT:
815 return (LIBUSB_TRANSFER_TIMED_OUT);
817 return (LIBUSB_TRANSFER_ERROR);
821 /* This function must be called locked */
824 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
825 struct libusb_super_transfer *sxfer, int status)
827 struct libusb_transfer *uxfer;
828 struct libusb_device *dev;
830 uxfer = (struct libusb_transfer *)(
831 ((uint8_t *)sxfer) + sizeof(*sxfer));
834 libusb20_tr_set_priv_sc1(pxfer, NULL);
836 uxfer->status = status;
838 dev = libusb_get_device(uxfer->dev_handle);
840 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
843 /* This function must be called locked */
846 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
848 struct libusb_super_transfer *sxfer;
849 struct libusb_transfer *uxfer;
851 uint16_t iso_packets;
856 status = libusb20_tr_get_status(pxfer);
857 sxfer = libusb20_tr_get_priv_sc1(pxfer);
858 actlen = libusb20_tr_get_actual_length(pxfer);
859 iso_packets = libusb20_tr_get_max_frames(pxfer);
862 return; /* cancelled - nothing to do */
864 uxfer = (struct libusb_transfer *)(
865 ((uint8_t *)sxfer) + sizeof(*sxfer));
867 if (iso_packets > uxfer->num_iso_packets)
868 iso_packets = uxfer->num_iso_packets;
870 if (iso_packets == 0)
871 return; /* nothing to do */
873 /* make sure that the number of ISOCHRONOUS packets is valid */
874 uxfer->num_iso_packets = iso_packets;
876 flags = uxfer->flags;
879 case LIBUSB20_TRANSFER_COMPLETED:
881 /* update actual length */
882 uxfer->actual_length = actlen;
883 for (i = 0; i != iso_packets; i++) {
884 uxfer->iso_packet_desc[i].actual_length =
885 libusb20_tr_get_length(pxfer, i);
887 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
890 case LIBUSB20_TRANSFER_START:
892 /* setup length(s) */
894 for (i = 0; i != iso_packets; i++) {
895 libusb20_tr_setup_isoc(pxfer,
896 &uxfer->buffer[actlen],
897 uxfer->iso_packet_desc[i].length, i);
898 actlen += uxfer->iso_packet_desc[i].length;
904 libusb20_tr_set_total_frames(pxfer, iso_packets);
905 libusb20_tr_submit(pxfer);
907 /* fork another USB transfer, if any */
908 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
912 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
917 /* This function must be called locked */
920 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
922 struct libusb_super_transfer *sxfer;
923 struct libusb_transfer *uxfer;
929 status = libusb20_tr_get_status(pxfer);
930 sxfer = libusb20_tr_get_priv_sc1(pxfer);
931 max_bulk = libusb20_tr_get_max_total_length(pxfer);
932 actlen = libusb20_tr_get_actual_length(pxfer);
935 return; /* cancelled - nothing to do */
937 uxfer = (struct libusb_transfer *)(
938 ((uint8_t *)sxfer) + sizeof(*sxfer));
940 flags = uxfer->flags;
943 case LIBUSB20_TRANSFER_COMPLETED:
945 uxfer->actual_length += actlen;
947 /* check for short packet */
948 if (sxfer->last_len != actlen) {
949 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
950 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
952 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
956 /* check for end of data */
957 if (sxfer->rem_len == 0) {
958 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
963 case LIBUSB20_TRANSFER_START:
964 if (max_bulk > sxfer->rem_len) {
965 max_bulk = sxfer->rem_len;
967 /* setup new BULK or INTERRUPT transaction */
968 libusb20_tr_setup_bulk(pxfer,
969 sxfer->curr_data, max_bulk, uxfer->timeout);
971 /* update counters */
972 sxfer->last_len = max_bulk;
973 sxfer->curr_data += max_bulk;
974 sxfer->rem_len -= max_bulk;
976 libusb20_tr_submit(pxfer);
978 /* check if we can fork another USB transfer */
979 if (sxfer->rem_len == 0)
980 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
984 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
989 /* This function must be called locked */
992 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
994 struct libusb_super_transfer *sxfer;
995 struct libusb_transfer *uxfer;
1001 status = libusb20_tr_get_status(pxfer);
1002 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1003 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1004 actlen = libusb20_tr_get_actual_length(pxfer);
1007 return; /* cancelled - nothing to do */
1009 uxfer = (struct libusb_transfer *)(
1010 ((uint8_t *)sxfer) + sizeof(*sxfer));
1012 flags = uxfer->flags;
1015 case LIBUSB20_TRANSFER_COMPLETED:
1017 uxfer->actual_length += actlen;
1019 /* subtract length of SETUP packet, if any */
1020 actlen -= libusb20_tr_get_length(pxfer, 0);
1022 /* check for short packet */
1023 if (sxfer->last_len != actlen) {
1024 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1025 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1027 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1031 /* check for end of data */
1032 if (sxfer->rem_len == 0) {
1033 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1038 case LIBUSB20_TRANSFER_START:
1039 if (max_bulk > sxfer->rem_len) {
1040 max_bulk = sxfer->rem_len;
1042 /* setup new CONTROL transaction */
1043 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1044 /* next fragment - don't send SETUP packet */
1045 libusb20_tr_set_length(pxfer, 0, 0);
1047 /* first fragment - send SETUP packet */
1048 libusb20_tr_set_length(pxfer, 8, 0);
1049 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1052 if (max_bulk != 0) {
1053 libusb20_tr_set_length(pxfer, max_bulk, 1);
1054 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1055 libusb20_tr_set_total_frames(pxfer, 2);
1057 libusb20_tr_set_total_frames(pxfer, 1);
1060 /* update counters */
1061 sxfer->last_len = max_bulk;
1062 sxfer->curr_data += max_bulk;
1063 sxfer->rem_len -= max_bulk;
1065 libusb20_tr_submit(pxfer);
1067 /* check if we can fork another USB transfer */
1068 if (sxfer->rem_len == 0)
1069 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1073 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1078 /* The following function must be called locked */
1081 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1083 struct libusb20_transfer *pxfer0;
1084 struct libusb20_transfer *pxfer1;
1085 struct libusb_super_transfer *sxfer;
1086 struct libusb_transfer *uxfer;
1087 struct libusb_device *dev;
1094 dev = libusb_get_device(pdev);
1096 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1097 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1099 if (pxfer0 == NULL || pxfer1 == NULL)
1100 return; /* shouldn't happen */
1103 if (libusb20_tr_pending(pxfer0))
1105 if (libusb20_tr_pending(pxfer1))
1110 /* wait till one of the transfers complete */
1113 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1115 return; /* cannot queue another one */
1116 /* swap transfers */
1120 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1122 return; /* cannot queue another one */
1123 /* swap transfers */
1130 /* find next transfer on same endpoint */
1131 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1133 uxfer = (struct libusb_transfer *)(
1134 ((uint8_t *)sxfer) + sizeof(*sxfer));
1136 if (uxfer->endpoint == endpoint) {
1137 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1138 sxfer->entry.tqe_prev = NULL;
1142 return; /* success */
1146 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1147 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1149 /* reset super transfer state */
1150 sxfer->rem_len = uxfer->length;
1151 sxfer->curr_data = uxfer->buffer;
1152 uxfer->actual_length = 0;
1154 switch (uxfer->type) {
1155 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1156 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1158 case LIBUSB_TRANSFER_TYPE_BULK:
1159 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1160 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1162 case LIBUSB_TRANSFER_TYPE_CONTROL:
1163 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1164 if (sxfer->rem_len < 8)
1167 /* remove SETUP packet from data */
1168 sxfer->rem_len -= 8;
1169 sxfer->curr_data += 8;
1175 buffsize = libusb10_get_buffsize(pdev, uxfer);
1176 maxframe = libusb10_get_maxframe(pdev, uxfer);
1178 /* make sure the transfer is opened */
1179 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1180 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1183 libusb20_tr_start(pxfer0);
1187 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1189 /* make sure our event loop spins the done handler */
1191 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1194 /* The following function must be called unlocked */
1197 libusb_submit_transfer(struct libusb_transfer *uxfer)
1199 struct libusb20_transfer *pxfer0;
1200 struct libusb20_transfer *pxfer1;
1201 struct libusb_super_transfer *sxfer;
1202 struct libusb_device *dev;
1207 return (LIBUSB_ERROR_INVALID_PARAM);
1209 if (uxfer->dev_handle == NULL)
1210 return (LIBUSB_ERROR_INVALID_PARAM);
1212 endpoint = uxfer->endpoint;
1215 return (LIBUSB_ERROR_INVALID_PARAM);
1217 dev = libusb_get_device(uxfer->dev_handle);
1219 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1221 sxfer = (struct libusb_super_transfer *)(
1222 (uint8_t *)uxfer - sizeof(*sxfer));
1226 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1227 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1229 if (pxfer0 == NULL || pxfer1 == NULL) {
1230 err = LIBUSB_ERROR_OTHER;
1231 } else if ((sxfer->entry.tqe_prev != NULL) ||
1232 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1233 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1234 err = LIBUSB_ERROR_BUSY;
1236 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1238 libusb10_submit_transfer_sub(
1239 uxfer->dev_handle, endpoint);
1241 err = 0; /* success */
1244 CTX_UNLOCK(dev->ctx);
1246 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1251 /* Asynchronous transfer cancel */
1254 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1256 struct libusb20_transfer *pxfer0;
1257 struct libusb20_transfer *pxfer1;
1258 struct libusb_super_transfer *sxfer;
1259 struct libusb_device *dev;
1263 return (LIBUSB_ERROR_INVALID_PARAM);
1265 if (uxfer->dev_handle == NULL)
1266 return (LIBUSB_ERROR_INVALID_PARAM);
1268 endpoint = uxfer->endpoint;
1271 return (LIBUSB_ERROR_INVALID_PARAM);
1273 dev = libusb_get_device(uxfer->dev_handle);
1275 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1277 sxfer = (struct libusb_super_transfer *)(
1278 (uint8_t *)uxfer - sizeof(*sxfer));
1282 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1283 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1285 if (sxfer->entry.tqe_prev != NULL) {
1286 /* we are lucky - transfer is on a queue */
1287 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1288 sxfer->entry.tqe_prev = NULL;
1289 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_CANCELLED);
1290 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1292 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1293 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_CANCELLED);
1294 libusb20_tr_stop(pxfer0);
1295 /* make sure the queue doesn't stall */
1296 libusb10_submit_transfer_sub(
1297 uxfer->dev_handle, endpoint);
1298 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1299 libusb10_complete_transfer(pxfer1, sxfer, LIBUSB_TRANSFER_CANCELLED);
1300 libusb20_tr_stop(pxfer1);
1301 /* make sure the queue doesn't stall */
1302 libusb10_submit_transfer_sub(
1303 uxfer->dev_handle, endpoint);
1308 CTX_UNLOCK(dev->ctx);
1310 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1316 libusb10_cancel_all_transfer(libusb_device *dev)
1322 libusb_cpu_to_le16(uint16_t x)
1324 return (htole16(x));
1328 libusb_le16_to_cpu(uint16_t x)
1330 return (le16toh(x));