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_init(libusb_context **context)
76 struct libusb_context *ctx;
81 ctx = malloc(sizeof(*ctx));
83 return (LIBUSB_ERROR_INVALID_PARAM);
85 memset(ctx, 0, sizeof(*ctx));
87 debug = getenv("LIBUSB_DEBUG");
89 ctx->debug = atoi(debug);
93 TAILQ_INIT(&ctx->pollfds);
94 TAILQ_INIT(&ctx->tr_done);
96 pthread_mutex_init(&ctx->ctx_lock, NULL);
97 pthread_cond_init(&ctx->ctx_cond, NULL);
99 ctx->ctx_handler = NO_THREAD;
101 ret = pipe(ctx->ctrl_pipe);
103 pthread_mutex_destroy(&ctx->ctx_lock);
104 pthread_cond_destroy(&ctx->ctx_cond);
106 return (LIBUSB_ERROR_OTHER);
108 /* set non-blocking mode on the control pipe to avoid deadlock */
110 ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag);
111 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]");
113 ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag);
114 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]");
116 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
118 pthread_mutex_lock(&default_context_lock);
119 if (usbi_default_context == NULL) {
120 usbi_default_context = ctx;
122 pthread_mutex_unlock(&default_context_lock);
127 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
133 libusb_exit(libusb_context *ctx)
135 ctx = GET_CONTEXT(ctx);
140 /* XXX cleanup devices */
142 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
143 close(ctx->ctrl_pipe[0]);
144 close(ctx->ctrl_pipe[1]);
145 pthread_mutex_destroy(&ctx->ctx_lock);
146 pthread_cond_destroy(&ctx->ctx_cond);
148 pthread_mutex_lock(&default_context_lock);
149 if (ctx == usbi_default_context) {
150 usbi_default_context = NULL;
152 pthread_mutex_unlock(&default_context_lock);
157 /* Device handling and initialisation. */
160 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
162 struct libusb20_backend *usb_backend;
163 struct libusb20_device *pdev;
164 struct libusb_device *dev;
167 ctx = GET_CONTEXT(ctx);
170 return (LIBUSB_ERROR_INVALID_PARAM);
173 return (LIBUSB_ERROR_INVALID_PARAM);
175 usb_backend = libusb20_be_alloc_default();
176 if (usb_backend == NULL)
177 return (LIBUSB_ERROR_NO_MEM);
179 /* figure out how many USB devices are present */
182 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
185 /* allocate device pointer list */
186 *list = malloc((i + 1) * sizeof(void *));
188 libusb20_be_free(usb_backend);
189 return (LIBUSB_ERROR_NO_MEM);
191 /* create libusb v1.0 compliant devices */
193 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
195 dev = malloc(sizeof(*dev));
198 libusb_unref_device((*list)[i - 1]);
203 libusb20_be_free(usb_backend);
204 return (LIBUSB_ERROR_NO_MEM);
207 /* get device into libUSB v1.0 list */
208 libusb20_be_dequeue_device(usb_backend, pdev);
210 memset(dev, 0, sizeof(*dev));
212 /* init transfer queues */
213 TAILQ_INIT(&dev->tr_head);
215 /* set context we belong to */
218 /* link together the two structures */
220 pdev->privLuData = dev;
222 (*list)[i] = libusb_ref_device(dev);
227 libusb20_be_free(usb_backend);
232 libusb_free_device_list(libusb_device **list, int unref_devices)
237 return; /* be NULL safe */
240 for (i = 0; list[i] != NULL; i++)
241 libusb_unref_device(list[i]);
247 libusb_get_bus_number(libusb_device *dev)
250 return (0); /* should not happen */
251 return (libusb20_dev_get_bus_number(dev->os_priv));
255 libusb_get_device_address(libusb_device *dev)
258 return (0); /* should not happen */
259 return (libusb20_dev_get_address(dev->os_priv));
263 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
265 struct libusb_config_descriptor *pdconf;
266 struct libusb_interface *pinf;
267 struct libusb_interface_descriptor *pdinf;
268 struct libusb_endpoint_descriptor *pdend;
275 return (LIBUSB_ERROR_NO_DEVICE);
277 ret = libusb_get_active_config_descriptor(dev, &pdconf);
281 ret = LIBUSB_ERROR_NOT_FOUND;
282 for (i = 0; i < pdconf->bNumInterfaces; i++) {
283 pinf = &pdconf->interface[i];
284 for (j = 0; j < pinf->num_altsetting; j++) {
285 pdinf = &pinf->altsetting[j];
286 for (k = 0; k < pdinf->bNumEndpoints; k++) {
287 pdend = &pdinf->endpoint[k];
288 if (pdend->bEndpointAddress == endpoint) {
289 ret = pdend->wMaxPacketSize;
297 libusb_free_config_descriptor(pdconf);
302 libusb_ref_device(libusb_device *dev)
305 return (NULL); /* be NULL safe */
309 CTX_UNLOCK(dev->ctx);
315 libusb_unref_device(libusb_device *dev)
318 return; /* be NULL safe */
322 CTX_UNLOCK(dev->ctx);
324 if (dev->refcnt == 0) {
325 libusb20_dev_free(dev->os_priv);
331 libusb_open(libusb_device *dev, libusb_device_handle **devh)
333 libusb_context *ctx = dev->ctx;
334 struct libusb20_device *pdev = dev->os_priv;
339 return (LIBUSB_ERROR_INVALID_PARAM);
341 /* set default device handle value */
344 dev = libusb_ref_device(dev);
346 return (LIBUSB_ERROR_INVALID_PARAM);
348 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
350 libusb_unref_device(dev);
351 return (LIBUSB_ERROR_NO_MEM);
353 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
354 POLLOUT | POLLRDNORM | POLLWRNORM);
356 /* make sure our event loop detects the new device */
358 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
359 if (err < sizeof(dummy)) {
360 /* ignore error, if any */
361 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
368 libusb_device_handle *
369 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
372 struct libusb_device **devs;
373 struct libusb20_device *pdev;
374 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
378 ctx = GET_CONTEXT(ctx);
380 return (NULL); /* be NULL safe */
382 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
384 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
387 for (j = 0; j < i; j++) {
388 pdev = devs[j]->os_priv;
389 pdesc = libusb20_dev_get_device_desc(pdev);
391 * NOTE: The USB library will automatically swap the
392 * fields in the device descriptor to be of host
395 if (pdesc->idVendor == vendor_id &&
396 pdesc->idProduct == product_id) {
397 if (libusb_open(devs[j], &pdev) < 0)
405 libusb_free_device_list(devs, 1);
406 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
411 libusb_close(struct libusb20_device *pdev)
414 struct libusb_device *dev;
419 return; /* be NULL safe */
421 dev = libusb_get_device(pdev);
424 libusb10_remove_pollfd(ctx, &dev->dev_poll);
426 libusb20_dev_close(pdev);
428 /* unref will free the "pdev" when the refcount reaches zero */
429 libusb_unref_device(dev);
431 /* make sure our event loop detects the closed device */
433 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
434 if (err < sizeof(dummy)) {
435 /* ignore error, if any */
436 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
441 libusb_get_device(struct libusb20_device *pdev)
445 return ((libusb_device *)pdev->privLuData);
449 libusb_get_configuration(struct libusb20_device *pdev, int *config)
451 struct libusb20_config *pconf;
453 if (pdev == NULL || config == NULL)
454 return (LIBUSB_ERROR_INVALID_PARAM);
456 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
458 return (LIBUSB_ERROR_NO_MEM);
460 *config = pconf->desc.bConfigurationValue;
468 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
470 struct libusb20_config *pconf;
471 struct libusb_device *dev;
475 dev = libusb_get_device(pdev);
478 return (LIBUSB_ERROR_INVALID_PARAM);
480 if (configuration < 1) {
484 for (i = 0; i != 255; i++) {
487 pconf = libusb20_dev_alloc_config(pdev, i);
489 return (LIBUSB_ERROR_INVALID_PARAM);
490 found = (pconf->desc.bConfigurationValue
497 return (LIBUSB_ERROR_INVALID_PARAM);
502 libusb10_cancel_all_transfer(dev);
504 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
506 err = libusb20_dev_set_config_index(pdev, i);
508 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
509 POLLOUT | POLLRDNORM | POLLWRNORM);
511 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
515 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
520 dev = libusb_get_device(pdev);
522 return (LIBUSB_ERROR_INVALID_PARAM);
524 if (interface_number < 0 || interface_number > 31)
525 return (LIBUSB_ERROR_INVALID_PARAM);
528 if (dev->claimed_interfaces & (1 << interface_number))
529 err = LIBUSB_ERROR_BUSY;
532 dev->claimed_interfaces |= (1 << interface_number);
533 CTX_UNLOCK(dev->ctx);
538 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
543 dev = libusb_get_device(pdev);
545 return (LIBUSB_ERROR_INVALID_PARAM);
547 if (interface_number < 0 || interface_number > 31)
548 return (LIBUSB_ERROR_INVALID_PARAM);
551 if (!(dev->claimed_interfaces & (1 << interface_number)))
552 err = LIBUSB_ERROR_NOT_FOUND;
555 dev->claimed_interfaces &= ~(1 << interface_number);
556 CTX_UNLOCK(dev->ctx);
561 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
562 int interface_number, int alternate_setting)
567 dev = libusb_get_device(pdev);
569 return (LIBUSB_ERROR_INVALID_PARAM);
571 if (interface_number < 0 || interface_number > 31)
572 return (LIBUSB_ERROR_INVALID_PARAM);
575 if (!(dev->claimed_interfaces & (1 << interface_number)))
576 err = LIBUSB_ERROR_NOT_FOUND;
577 CTX_UNLOCK(dev->ctx);
582 libusb10_cancel_all_transfer(dev);
584 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
586 err = libusb20_dev_set_alt_index(pdev,
587 interface_number, alternate_setting);
589 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
590 pdev, libusb20_dev_get_fd(pdev),
591 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
593 return (err ? LIBUSB_ERROR_OTHER : 0);
596 static struct libusb20_transfer *
597 libusb10_get_transfer(struct libusb20_device *pdev,
598 uint8_t endpoint, uint8_t index)
600 index &= 1; /* double buffering */
602 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
604 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
605 /* this is an IN endpoint */
608 return (libusb20_tr_get_pointer(pdev, index));
612 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
614 struct libusb20_transfer *xfer;
615 struct libusb_device *dev;
618 xfer = libusb10_get_transfer(pdev, endpoint, 0);
620 return (LIBUSB_ERROR_INVALID_PARAM);
622 dev = libusb_get_device(pdev);
625 err = libusb20_tr_open(xfer, 0, 0, endpoint);
626 CTX_UNLOCK(dev->ctx);
628 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
629 return (LIBUSB_ERROR_OTHER);
631 libusb20_tr_clear_stall_sync(xfer);
633 /* check if we opened the transfer */
636 libusb20_tr_close(xfer);
637 CTX_UNLOCK(dev->ctx);
639 return (0); /* success */
643 libusb_reset_device(struct libusb20_device *pdev)
648 dev = libusb_get_device(pdev);
650 return (LIBUSB20_ERROR_INVALID_PARAM);
652 libusb10_cancel_all_transfer(dev);
654 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
656 err = libusb20_dev_reset(pdev);
658 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
659 pdev, libusb20_dev_get_fd(pdev),
660 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
662 return (err ? LIBUSB_ERROR_OTHER : 0);
666 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
669 return (LIBUSB_ERROR_INVALID_PARAM);
671 return (libusb20_dev_kernel_driver_active(
676 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
681 return (LIBUSB_ERROR_INVALID_PARAM);
683 err = libusb20_dev_detach_kernel_driver(
686 return (err ? LIBUSB20_ERROR_OTHER : 0);
690 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
693 return (LIBUSB_ERROR_INVALID_PARAM);
694 /* stub - currently not supported by libusb20 */
698 /* Asynchronous device I/O */
700 struct libusb_transfer *
701 libusb_alloc_transfer(int iso_packets)
703 struct libusb_transfer *uxfer;
704 struct libusb_super_transfer *sxfer;
707 len = sizeof(struct libusb_transfer) +
708 sizeof(struct libusb_super_transfer) +
709 (iso_packets * sizeof(libusb_iso_packet_descriptor));
715 memset(sxfer, 0, len);
717 uxfer = (struct libusb_transfer *)(
718 ((uint8_t *)sxfer) + sizeof(*sxfer));
720 /* set default value */
721 uxfer->num_iso_packets = iso_packets;
727 libusb_free_transfer(struct libusb_transfer *uxfer)
729 struct libusb_super_transfer *sxfer;
732 return; /* be NULL safe */
734 sxfer = (struct libusb_super_transfer *)(
735 (uint8_t *)uxfer - sizeof(*sxfer));
741 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
746 usb_speed = libusb20_dev_get_speed(pdev);
748 switch (xfer->type) {
749 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
751 case LIBUSB20_SPEED_LOW:
752 case LIBUSB20_SPEED_FULL:
760 case LIBUSB_TRANSFER_TYPE_CONTROL:
771 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
776 usb_speed = libusb20_dev_get_speed(pdev);
778 switch (xfer->type) {
779 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
780 ret = 0; /* kernel will auto-select */
782 case LIBUSB_TRANSFER_TYPE_CONTROL:
787 case LIBUSB20_SPEED_LOW:
790 case LIBUSB20_SPEED_FULL:
803 libusb10_convert_error(uint8_t status)
808 case LIBUSB20_TRANSFER_START:
809 case LIBUSB20_TRANSFER_COMPLETED:
810 return (LIBUSB_TRANSFER_COMPLETED);
811 case LIBUSB20_TRANSFER_OVERFLOW:
812 return (LIBUSB_TRANSFER_OVERFLOW);
813 case LIBUSB20_TRANSFER_NO_DEVICE:
814 return (LIBUSB_TRANSFER_NO_DEVICE);
815 case LIBUSB20_TRANSFER_STALL:
816 return (LIBUSB_TRANSFER_STALL);
817 case LIBUSB20_TRANSFER_CANCELLED:
818 return (LIBUSB_TRANSFER_CANCELLED);
819 case LIBUSB20_TRANSFER_TIMED_OUT:
820 return (LIBUSB_TRANSFER_TIMED_OUT);
822 return (LIBUSB_TRANSFER_ERROR);
826 /* This function must be called locked */
829 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
830 struct libusb_super_transfer *sxfer, int status)
832 struct libusb_transfer *uxfer;
833 struct libusb_device *dev;
835 uxfer = (struct libusb_transfer *)(
836 ((uint8_t *)sxfer) + sizeof(*sxfer));
839 libusb20_tr_set_priv_sc1(pxfer, NULL);
841 /* set transfer status */
842 uxfer->status = status;
844 /* update super transfer state */
845 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
847 dev = libusb_get_device(uxfer->dev_handle);
849 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
852 /* This function must be called locked */
855 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
857 struct libusb_super_transfer *sxfer;
858 struct libusb_transfer *uxfer;
860 uint16_t iso_packets;
865 status = libusb20_tr_get_status(pxfer);
866 sxfer = libusb20_tr_get_priv_sc1(pxfer);
867 actlen = libusb20_tr_get_actual_length(pxfer);
868 iso_packets = libusb20_tr_get_max_frames(pxfer);
871 return; /* cancelled - nothing to do */
873 uxfer = (struct libusb_transfer *)(
874 ((uint8_t *)sxfer) + sizeof(*sxfer));
876 if (iso_packets > uxfer->num_iso_packets)
877 iso_packets = uxfer->num_iso_packets;
879 if (iso_packets == 0)
880 return; /* nothing to do */
882 /* make sure that the number of ISOCHRONOUS packets is valid */
883 uxfer->num_iso_packets = iso_packets;
885 flags = uxfer->flags;
888 case LIBUSB20_TRANSFER_COMPLETED:
890 /* update actual length */
891 uxfer->actual_length = actlen;
892 for (i = 0; i != iso_packets; i++) {
893 uxfer->iso_packet_desc[i].actual_length =
894 libusb20_tr_get_length(pxfer, i);
896 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
899 case LIBUSB20_TRANSFER_START:
901 /* setup length(s) */
903 for (i = 0; i != iso_packets; i++) {
904 libusb20_tr_setup_isoc(pxfer,
905 &uxfer->buffer[actlen],
906 uxfer->iso_packet_desc[i].length, i);
907 actlen += uxfer->iso_packet_desc[i].length;
913 libusb20_tr_set_total_frames(pxfer, iso_packets);
914 libusb20_tr_submit(pxfer);
916 /* fork another USB transfer, if any */
917 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
921 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
926 /* This function must be called locked */
929 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
931 struct libusb_super_transfer *sxfer;
932 struct libusb_transfer *uxfer;
938 status = libusb20_tr_get_status(pxfer);
939 sxfer = libusb20_tr_get_priv_sc1(pxfer);
940 max_bulk = libusb20_tr_get_max_total_length(pxfer);
941 actlen = libusb20_tr_get_actual_length(pxfer);
944 return; /* cancelled - nothing to do */
946 uxfer = (struct libusb_transfer *)(
947 ((uint8_t *)sxfer) + sizeof(*sxfer));
949 flags = uxfer->flags;
952 case LIBUSB20_TRANSFER_COMPLETED:
954 uxfer->actual_length += actlen;
956 /* check for short packet */
957 if (sxfer->last_len != actlen) {
958 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
959 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
961 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
965 /* check for end of data */
966 if (sxfer->rem_len == 0) {
967 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
972 case LIBUSB20_TRANSFER_START:
973 if (max_bulk > sxfer->rem_len) {
974 max_bulk = sxfer->rem_len;
976 /* setup new BULK or INTERRUPT transaction */
977 libusb20_tr_setup_bulk(pxfer,
978 sxfer->curr_data, max_bulk, uxfer->timeout);
980 /* update counters */
981 sxfer->last_len = max_bulk;
982 sxfer->curr_data += max_bulk;
983 sxfer->rem_len -= max_bulk;
985 libusb20_tr_submit(pxfer);
987 /* check if we can fork another USB transfer */
988 if (sxfer->rem_len == 0)
989 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
993 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
998 /* This function must be called locked */
1001 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1003 struct libusb_super_transfer *sxfer;
1004 struct libusb_transfer *uxfer;
1010 status = libusb20_tr_get_status(pxfer);
1011 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1012 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1013 actlen = libusb20_tr_get_actual_length(pxfer);
1016 return; /* cancelled - nothing to do */
1018 uxfer = (struct libusb_transfer *)(
1019 ((uint8_t *)sxfer) + sizeof(*sxfer));
1021 flags = uxfer->flags;
1024 case LIBUSB20_TRANSFER_COMPLETED:
1026 uxfer->actual_length += actlen;
1028 /* subtract length of SETUP packet, if any */
1029 actlen -= libusb20_tr_get_length(pxfer, 0);
1031 /* check for short packet */
1032 if (sxfer->last_len != actlen) {
1033 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1034 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1036 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1040 /* check for end of data */
1041 if (sxfer->rem_len == 0) {
1042 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1047 case LIBUSB20_TRANSFER_START:
1048 if (max_bulk > sxfer->rem_len) {
1049 max_bulk = sxfer->rem_len;
1051 /* setup new CONTROL transaction */
1052 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1053 /* next fragment - don't send SETUP packet */
1054 libusb20_tr_set_length(pxfer, 0, 0);
1056 /* first fragment - send SETUP packet */
1057 libusb20_tr_set_length(pxfer, 8, 0);
1058 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1061 if (max_bulk != 0) {
1062 libusb20_tr_set_length(pxfer, max_bulk, 1);
1063 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1064 libusb20_tr_set_total_frames(pxfer, 2);
1066 libusb20_tr_set_total_frames(pxfer, 1);
1069 /* update counters */
1070 sxfer->last_len = max_bulk;
1071 sxfer->curr_data += max_bulk;
1072 sxfer->rem_len -= max_bulk;
1074 libusb20_tr_submit(pxfer);
1076 /* check if we can fork another USB transfer */
1077 if (sxfer->rem_len == 0)
1078 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1082 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1087 /* The following function must be called locked */
1090 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1092 struct libusb20_transfer *pxfer0;
1093 struct libusb20_transfer *pxfer1;
1094 struct libusb_super_transfer *sxfer;
1095 struct libusb_transfer *uxfer;
1096 struct libusb_device *dev;
1103 dev = libusb_get_device(pdev);
1105 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1106 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1108 if (pxfer0 == NULL || pxfer1 == NULL)
1109 return; /* shouldn't happen */
1112 if (libusb20_tr_pending(pxfer0))
1114 if (libusb20_tr_pending(pxfer1))
1119 /* wait till one of the transfers complete */
1122 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1124 return; /* cancelling */
1126 return; /* cannot queue another one */
1127 /* swap transfers */
1131 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1133 return; /* cancelling */
1135 return; /* cannot queue another one */
1136 /* swap transfers */
1143 /* find next transfer on same endpoint */
1144 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1146 uxfer = (struct libusb_transfer *)(
1147 ((uint8_t *)sxfer) + sizeof(*sxfer));
1149 if (uxfer->endpoint == endpoint) {
1150 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1151 sxfer->entry.tqe_prev = NULL;
1155 return; /* success */
1159 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1160 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1162 /* reset super transfer state */
1163 sxfer->rem_len = uxfer->length;
1164 sxfer->curr_data = uxfer->buffer;
1165 uxfer->actual_length = 0;
1167 switch (uxfer->type) {
1168 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1169 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1171 case LIBUSB_TRANSFER_TYPE_BULK:
1172 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1173 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1175 case LIBUSB_TRANSFER_TYPE_CONTROL:
1176 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1177 if (sxfer->rem_len < 8)
1180 /* remove SETUP packet from data */
1181 sxfer->rem_len -= 8;
1182 sxfer->curr_data += 8;
1188 buffsize = libusb10_get_buffsize(pdev, uxfer);
1189 maxframe = libusb10_get_maxframe(pdev, uxfer);
1191 /* make sure the transfer is opened */
1192 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1193 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1196 libusb20_tr_start(pxfer0);
1200 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1202 /* make sure our event loop spins the done handler */
1204 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1207 /* The following function must be called unlocked */
1210 libusb_submit_transfer(struct libusb_transfer *uxfer)
1212 struct libusb20_transfer *pxfer0;
1213 struct libusb20_transfer *pxfer1;
1214 struct libusb_super_transfer *sxfer;
1215 struct libusb_device *dev;
1220 return (LIBUSB_ERROR_INVALID_PARAM);
1222 if (uxfer->dev_handle == NULL)
1223 return (LIBUSB_ERROR_INVALID_PARAM);
1225 endpoint = uxfer->endpoint;
1228 return (LIBUSB_ERROR_INVALID_PARAM);
1230 dev = libusb_get_device(uxfer->dev_handle);
1232 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1234 sxfer = (struct libusb_super_transfer *)(
1235 (uint8_t *)uxfer - sizeof(*sxfer));
1239 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1240 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1242 if (pxfer0 == NULL || pxfer1 == NULL) {
1243 err = LIBUSB_ERROR_OTHER;
1244 } else if ((sxfer->entry.tqe_prev != NULL) ||
1245 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1246 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1247 err = LIBUSB_ERROR_BUSY;
1250 /* set pending state */
1251 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1253 /* insert transfer into transfer head list */
1254 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1256 /* start work transfers */
1257 libusb10_submit_transfer_sub(
1258 uxfer->dev_handle, endpoint);
1260 err = 0; /* success */
1263 CTX_UNLOCK(dev->ctx);
1265 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1270 /* Asynchronous transfer cancel */
1273 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1275 struct libusb20_transfer *pxfer0;
1276 struct libusb20_transfer *pxfer1;
1277 struct libusb_super_transfer *sxfer;
1278 struct libusb_device *dev;
1283 return (LIBUSB_ERROR_INVALID_PARAM);
1285 /* check if not initialised */
1286 if (uxfer->dev_handle == NULL)
1287 return (LIBUSB_ERROR_NOT_FOUND);
1289 endpoint = uxfer->endpoint;
1292 return (LIBUSB_ERROR_INVALID_PARAM);
1294 dev = libusb_get_device(uxfer->dev_handle);
1296 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1298 sxfer = (struct libusb_super_transfer *)(
1299 (uint8_t *)uxfer - sizeof(*sxfer));
1305 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1306 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1308 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1309 /* only update the transfer status */
1310 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1311 retval = LIBUSB_ERROR_NOT_FOUND;
1312 } else if (sxfer->entry.tqe_prev != NULL) {
1313 /* we are lucky - transfer is on a queue */
1314 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1315 sxfer->entry.tqe_prev = NULL;
1316 libusb10_complete_transfer(NULL,
1317 sxfer, LIBUSB_TRANSFER_CANCELLED);
1318 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1320 retval = LIBUSB_ERROR_NOT_FOUND;
1321 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1322 libusb10_complete_transfer(pxfer0,
1323 sxfer, LIBUSB_TRANSFER_CANCELLED);
1324 libusb20_tr_stop(pxfer0);
1325 /* make sure the queue doesn't stall */
1326 libusb10_submit_transfer_sub(
1327 uxfer->dev_handle, endpoint);
1328 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1329 libusb10_complete_transfer(pxfer1,
1330 sxfer, LIBUSB_TRANSFER_CANCELLED);
1331 libusb20_tr_stop(pxfer1);
1332 /* make sure the queue doesn't stall */
1333 libusb10_submit_transfer_sub(
1334 uxfer->dev_handle, endpoint);
1337 retval = LIBUSB_ERROR_NOT_FOUND;
1340 CTX_UNLOCK(dev->ctx);
1342 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1348 libusb10_cancel_all_transfer(libusb_device *dev)
1354 libusb_cpu_to_le16(uint16_t x)
1356 return (htole16(x));
1360 libusb_le16_to_cpu(uint16_t x)
1362 return (le16toh(x));