3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/queue.h>
40 #define libusb_device_handle libusb20_device
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
48 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49 struct libusb_context *usbi_default_context = NULL;
53 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_convert_error(uint8_t status);
56 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57 static void libusb10_isoc_proxy(struct libusb20_transfer *);
58 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
62 /* Library initialisation / deinitialisation */
65 libusb_set_debug(libusb_context *ctx, int level)
67 ctx = GET_CONTEXT(ctx);
73 libusb_set_nonblocking(int f)
78 * We ignore any failures in this function, hence the
79 * non-blocking flag is not critical to the operation of
80 * libUSB. We use F_GETFL and F_SETFL to be compatible with
84 flags = fcntl(f, F_GETFL, NULL);
88 fcntl(f, F_SETFL, flags);
92 libusb_init(libusb_context **context)
94 struct libusb_context *ctx;
98 ctx = malloc(sizeof(*ctx));
100 return (LIBUSB_ERROR_INVALID_PARAM);
102 memset(ctx, 0, sizeof(*ctx));
104 debug = getenv("LIBUSB_DEBUG");
106 ctx->debug = atoi(debug);
108 ctx->debug_fixed = 1;
110 TAILQ_INIT(&ctx->pollfds);
111 TAILQ_INIT(&ctx->tr_done);
113 pthread_mutex_init(&ctx->ctx_lock, NULL);
114 pthread_cond_init(&ctx->ctx_cond, NULL);
116 ctx->ctx_handler = NO_THREAD;
118 ret = pipe(ctx->ctrl_pipe);
120 pthread_mutex_destroy(&ctx->ctx_lock);
121 pthread_cond_destroy(&ctx->ctx_cond);
123 return (LIBUSB_ERROR_OTHER);
125 /* set non-blocking mode on the control pipe to avoid deadlock */
126 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
127 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
129 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
131 pthread_mutex_lock(&default_context_lock);
132 if (usbi_default_context == NULL) {
133 usbi_default_context = ctx;
135 pthread_mutex_unlock(&default_context_lock);
140 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
146 libusb_exit(libusb_context *ctx)
148 ctx = GET_CONTEXT(ctx);
153 /* XXX cleanup devices */
155 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
156 close(ctx->ctrl_pipe[0]);
157 close(ctx->ctrl_pipe[1]);
158 pthread_mutex_destroy(&ctx->ctx_lock);
159 pthread_cond_destroy(&ctx->ctx_cond);
161 pthread_mutex_lock(&default_context_lock);
162 if (ctx == usbi_default_context) {
163 usbi_default_context = NULL;
165 pthread_mutex_unlock(&default_context_lock);
170 /* Device handling and initialisation. */
173 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
175 struct libusb20_backend *usb_backend;
176 struct libusb20_device *pdev;
177 struct libusb_device *dev;
180 ctx = GET_CONTEXT(ctx);
183 return (LIBUSB_ERROR_INVALID_PARAM);
186 return (LIBUSB_ERROR_INVALID_PARAM);
188 usb_backend = libusb20_be_alloc_default();
189 if (usb_backend == NULL)
190 return (LIBUSB_ERROR_NO_MEM);
192 /* figure out how many USB devices are present */
195 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
198 /* allocate device pointer list */
199 *list = malloc((i + 1) * sizeof(void *));
201 libusb20_be_free(usb_backend);
202 return (LIBUSB_ERROR_NO_MEM);
204 /* create libusb v1.0 compliant devices */
206 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
208 dev = malloc(sizeof(*dev));
211 libusb_unref_device((*list)[i - 1]);
216 libusb20_be_free(usb_backend);
217 return (LIBUSB_ERROR_NO_MEM);
220 /* get device into libUSB v1.0 list */
221 libusb20_be_dequeue_device(usb_backend, pdev);
223 memset(dev, 0, sizeof(*dev));
225 /* init transfer queues */
226 TAILQ_INIT(&dev->tr_head);
228 /* set context we belong to */
231 /* link together the two structures */
233 pdev->privLuData = dev;
235 (*list)[i] = libusb_ref_device(dev);
240 libusb20_be_free(usb_backend);
245 libusb_free_device_list(libusb_device **list, int unref_devices)
250 return; /* be NULL safe */
253 for (i = 0; list[i] != NULL; i++)
254 libusb_unref_device(list[i]);
260 libusb_get_bus_number(libusb_device *dev)
263 return (0); /* should not happen */
264 return (libusb20_dev_get_bus_number(dev->os_priv));
268 libusb_get_device_address(libusb_device *dev)
271 return (0); /* should not happen */
272 return (libusb20_dev_get_address(dev->os_priv));
276 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
278 struct libusb_config_descriptor *pdconf;
279 struct libusb_interface *pinf;
280 struct libusb_interface_descriptor *pdinf;
281 struct libusb_endpoint_descriptor *pdend;
288 return (LIBUSB_ERROR_NO_DEVICE);
290 ret = libusb_get_active_config_descriptor(dev, &pdconf);
294 ret = LIBUSB_ERROR_NOT_FOUND;
295 for (i = 0; i < pdconf->bNumInterfaces; i++) {
296 pinf = &pdconf->interface[i];
297 for (j = 0; j < pinf->num_altsetting; j++) {
298 pdinf = &pinf->altsetting[j];
299 for (k = 0; k < pdinf->bNumEndpoints; k++) {
300 pdend = &pdinf->endpoint[k];
301 if (pdend->bEndpointAddress == endpoint) {
302 ret = pdend->wMaxPacketSize;
310 libusb_free_config_descriptor(pdconf);
315 libusb_ref_device(libusb_device *dev)
318 return (NULL); /* be NULL safe */
322 CTX_UNLOCK(dev->ctx);
328 libusb_unref_device(libusb_device *dev)
331 return; /* be NULL safe */
335 CTX_UNLOCK(dev->ctx);
337 if (dev->refcnt == 0) {
338 libusb20_dev_free(dev->os_priv);
344 libusb_open(libusb_device *dev, libusb_device_handle **devh)
346 libusb_context *ctx = dev->ctx;
347 struct libusb20_device *pdev = dev->os_priv;
352 return (LIBUSB_ERROR_INVALID_PARAM);
354 /* set default device handle value */
357 dev = libusb_ref_device(dev);
359 return (LIBUSB_ERROR_INVALID_PARAM);
361 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
363 libusb_unref_device(dev);
364 return (LIBUSB_ERROR_NO_MEM);
366 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
367 POLLOUT | POLLRDNORM | POLLWRNORM);
369 /* make sure our event loop detects the new device */
371 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
372 if (err < (int)sizeof(dummy)) {
373 /* ignore error, if any */
374 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
381 libusb_device_handle *
382 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
385 struct libusb_device **devs;
386 struct libusb20_device *pdev;
387 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
391 ctx = GET_CONTEXT(ctx);
393 return (NULL); /* be NULL safe */
395 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
397 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
400 for (j = 0; j < i; j++) {
401 pdev = devs[j]->os_priv;
402 pdesc = libusb20_dev_get_device_desc(pdev);
404 * NOTE: The USB library will automatically swap the
405 * fields in the device descriptor to be of host
408 if (pdesc->idVendor == vendor_id &&
409 pdesc->idProduct == product_id) {
410 if (libusb_open(devs[j], &pdev) < 0)
418 libusb_free_device_list(devs, 1);
419 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
424 libusb_close(struct libusb20_device *pdev)
427 struct libusb_device *dev;
432 return; /* be NULL safe */
434 dev = libusb_get_device(pdev);
437 libusb10_remove_pollfd(ctx, &dev->dev_poll);
439 libusb20_dev_close(pdev);
441 /* unref will free the "pdev" when the refcount reaches zero */
442 libusb_unref_device(dev);
444 /* make sure our event loop detects the closed device */
446 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
447 if (err < (int)sizeof(dummy)) {
448 /* ignore error, if any */
449 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
454 libusb_get_device(struct libusb20_device *pdev)
458 return ((libusb_device *)pdev->privLuData);
462 libusb_get_configuration(struct libusb20_device *pdev, int *config)
464 struct libusb20_config *pconf;
466 if (pdev == NULL || config == NULL)
467 return (LIBUSB_ERROR_INVALID_PARAM);
469 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
471 return (LIBUSB_ERROR_NO_MEM);
473 *config = pconf->desc.bConfigurationValue;
481 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
483 struct libusb20_config *pconf;
484 struct libusb_device *dev;
488 dev = libusb_get_device(pdev);
490 return (LIBUSB_ERROR_INVALID_PARAM);
492 if (configuration < 1) {
496 for (i = 0; i != 255; i++) {
499 pconf = libusb20_dev_alloc_config(pdev, i);
501 return (LIBUSB_ERROR_INVALID_PARAM);
502 found = (pconf->desc.bConfigurationValue
509 return (LIBUSB_ERROR_INVALID_PARAM);
514 libusb10_cancel_all_transfer(dev);
516 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
518 err = libusb20_dev_set_config_index(pdev, i);
520 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
521 POLLOUT | POLLRDNORM | POLLWRNORM);
523 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
527 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
532 dev = libusb_get_device(pdev);
534 return (LIBUSB_ERROR_INVALID_PARAM);
536 if (interface_number < 0 || interface_number > 31)
537 return (LIBUSB_ERROR_INVALID_PARAM);
540 if (dev->claimed_interfaces & (1 << interface_number))
541 err = LIBUSB_ERROR_BUSY;
544 dev->claimed_interfaces |= (1 << interface_number);
545 CTX_UNLOCK(dev->ctx);
550 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
555 dev = libusb_get_device(pdev);
557 return (LIBUSB_ERROR_INVALID_PARAM);
559 if (interface_number < 0 || interface_number > 31)
560 return (LIBUSB_ERROR_INVALID_PARAM);
563 if (!(dev->claimed_interfaces & (1 << interface_number)))
564 err = LIBUSB_ERROR_NOT_FOUND;
567 dev->claimed_interfaces &= ~(1 << interface_number);
568 CTX_UNLOCK(dev->ctx);
573 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
574 int interface_number, int alternate_setting)
579 dev = libusb_get_device(pdev);
581 return (LIBUSB_ERROR_INVALID_PARAM);
583 if (interface_number < 0 || interface_number > 31)
584 return (LIBUSB_ERROR_INVALID_PARAM);
587 if (!(dev->claimed_interfaces & (1 << interface_number)))
588 err = LIBUSB_ERROR_NOT_FOUND;
589 CTX_UNLOCK(dev->ctx);
594 libusb10_cancel_all_transfer(dev);
596 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
598 err = libusb20_dev_set_alt_index(pdev,
599 interface_number, alternate_setting);
601 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
602 pdev, libusb20_dev_get_fd(pdev),
603 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
605 return (err ? LIBUSB_ERROR_OTHER : 0);
608 static struct libusb20_transfer *
609 libusb10_get_transfer(struct libusb20_device *pdev,
610 uint8_t endpoint, uint8_t index)
612 index &= 1; /* double buffering */
614 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
616 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
617 /* this is an IN endpoint */
620 return (libusb20_tr_get_pointer(pdev, index));
624 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
626 struct libusb20_transfer *xfer;
627 struct libusb_device *dev;
630 xfer = libusb10_get_transfer(pdev, endpoint, 0);
632 return (LIBUSB_ERROR_INVALID_PARAM);
634 dev = libusb_get_device(pdev);
636 return (LIBUSB_ERROR_INVALID_PARAM);
639 err = libusb20_tr_open(xfer, 0, 1, endpoint);
640 CTX_UNLOCK(dev->ctx);
642 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
643 return (LIBUSB_ERROR_OTHER);
645 libusb20_tr_clear_stall_sync(xfer);
647 /* check if we opened the transfer */
650 libusb20_tr_close(xfer);
651 CTX_UNLOCK(dev->ctx);
653 return (0); /* success */
657 libusb_reset_device(struct libusb20_device *pdev)
662 dev = libusb_get_device(pdev);
664 return (LIBUSB_ERROR_INVALID_PARAM);
666 libusb10_cancel_all_transfer(dev);
668 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
670 err = libusb20_dev_reset(pdev);
672 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
673 pdev, libusb20_dev_get_fd(pdev),
674 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
676 return (err ? LIBUSB_ERROR_OTHER : 0);
680 libusb_check_connected(struct libusb20_device *pdev)
685 dev = libusb_get_device(pdev);
687 return (LIBUSB_ERROR_INVALID_PARAM);
689 err = libusb20_dev_check_connected(pdev);
691 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
695 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
698 return (LIBUSB_ERROR_INVALID_PARAM);
700 return (libusb20_dev_kernel_driver_active(
705 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
706 char *name, int namelen)
708 return (libusb_get_driver(pdev, interface, name, namelen));
712 libusb_get_driver(struct libusb20_device *pdev, int interface,
713 char *name, int namelen)
719 return (LIBUSB_ERROR_INVALID_PARAM);
721 return (LIBUSB_ERROR_INVALID_PARAM);
725 err = libusb20_dev_get_iface_desc(
726 pdev, interface, name, namelen);
729 return (LIBUSB_ERROR_OTHER);
731 /* we only want the driver name */
732 ptr = strstr(name, ":");
740 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
742 return (libusb_detach_kernel_driver(pdev, interface));
746 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
751 return (LIBUSB_ERROR_INVALID_PARAM);
753 err = libusb20_dev_detach_kernel_driver(
756 return (err ? LIBUSB_ERROR_OTHER : 0);
760 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
763 return (LIBUSB_ERROR_INVALID_PARAM);
764 /* stub - currently not supported by libusb20 */
768 /* Asynchronous device I/O */
770 struct libusb_transfer *
771 libusb_alloc_transfer(int iso_packets)
773 struct libusb_transfer *uxfer;
774 struct libusb_super_transfer *sxfer;
777 len = sizeof(struct libusb_transfer) +
778 sizeof(struct libusb_super_transfer) +
779 (iso_packets * sizeof(libusb_iso_packet_descriptor));
785 memset(sxfer, 0, len);
787 uxfer = (struct libusb_transfer *)(
788 ((uint8_t *)sxfer) + sizeof(*sxfer));
790 /* set default value */
791 uxfer->num_iso_packets = iso_packets;
797 libusb_free_transfer(struct libusb_transfer *uxfer)
799 struct libusb_super_transfer *sxfer;
802 return; /* be NULL safe */
804 /* check if we should free the transfer buffer */
805 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
808 sxfer = (struct libusb_super_transfer *)(
809 (uint8_t *)uxfer - sizeof(*sxfer));
815 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
819 switch (xfer->type) {
820 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
821 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
823 case LIBUSB_TRANSFER_TYPE_CONTROL:
834 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
839 usb_speed = libusb20_dev_get_speed(pdev);
841 switch (xfer->type) {
842 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
843 ret = 0; /* kernel will auto-select */
845 case LIBUSB_TRANSFER_TYPE_CONTROL:
850 case LIBUSB20_SPEED_LOW:
853 case LIBUSB20_SPEED_FULL:
866 libusb10_convert_error(uint8_t status)
871 case LIBUSB20_TRANSFER_START:
872 case LIBUSB20_TRANSFER_COMPLETED:
873 return (LIBUSB_TRANSFER_COMPLETED);
874 case LIBUSB20_TRANSFER_OVERFLOW:
875 return (LIBUSB_TRANSFER_OVERFLOW);
876 case LIBUSB20_TRANSFER_NO_DEVICE:
877 return (LIBUSB_TRANSFER_NO_DEVICE);
878 case LIBUSB20_TRANSFER_STALL:
879 return (LIBUSB_TRANSFER_STALL);
880 case LIBUSB20_TRANSFER_CANCELLED:
881 return (LIBUSB_TRANSFER_CANCELLED);
882 case LIBUSB20_TRANSFER_TIMED_OUT:
883 return (LIBUSB_TRANSFER_TIMED_OUT);
885 return (LIBUSB_TRANSFER_ERROR);
889 /* This function must be called locked */
892 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
893 struct libusb_super_transfer *sxfer, int status)
895 struct libusb_transfer *uxfer;
896 struct libusb_device *dev;
898 uxfer = (struct libusb_transfer *)(
899 ((uint8_t *)sxfer) + sizeof(*sxfer));
902 libusb20_tr_set_priv_sc1(pxfer, NULL);
904 /* set transfer status */
905 uxfer->status = status;
907 /* update super transfer state */
908 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
910 dev = libusb_get_device(uxfer->dev_handle);
912 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
915 /* This function must be called locked */
918 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
920 struct libusb_super_transfer *sxfer;
921 struct libusb_transfer *uxfer;
923 uint16_t iso_packets;
928 status = libusb20_tr_get_status(pxfer);
929 sxfer = libusb20_tr_get_priv_sc1(pxfer);
930 actlen = libusb20_tr_get_actual_length(pxfer);
931 iso_packets = libusb20_tr_get_max_frames(pxfer);
934 return; /* cancelled - nothing to do */
936 uxfer = (struct libusb_transfer *)(
937 ((uint8_t *)sxfer) + sizeof(*sxfer));
939 if (iso_packets > uxfer->num_iso_packets)
940 iso_packets = uxfer->num_iso_packets;
942 if (iso_packets == 0)
943 return; /* nothing to do */
945 /* make sure that the number of ISOCHRONOUS packets is valid */
946 uxfer->num_iso_packets = iso_packets;
948 flags = uxfer->flags;
951 case LIBUSB20_TRANSFER_COMPLETED:
953 /* update actual length */
954 uxfer->actual_length = actlen;
955 for (i = 0; i != iso_packets; i++) {
956 uxfer->iso_packet_desc[i].actual_length =
957 libusb20_tr_get_length(pxfer, i);
959 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
962 case LIBUSB20_TRANSFER_START:
964 /* setup length(s) */
966 for (i = 0; i != iso_packets; i++) {
967 libusb20_tr_setup_isoc(pxfer,
968 &uxfer->buffer[actlen],
969 uxfer->iso_packet_desc[i].length, i);
970 actlen += uxfer->iso_packet_desc[i].length;
976 libusb20_tr_set_total_frames(pxfer, iso_packets);
977 libusb20_tr_submit(pxfer);
979 /* fork another USB transfer, if any */
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_bulk_intr_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 /* check for short packet */
1020 if (sxfer->last_len != actlen) {
1021 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1022 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1024 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1028 /* check for end of data */
1029 if (sxfer->rem_len == 0) {
1030 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1035 case LIBUSB20_TRANSFER_START:
1036 if (max_bulk > sxfer->rem_len) {
1037 max_bulk = sxfer->rem_len;
1039 /* setup new BULK or INTERRUPT transaction */
1040 libusb20_tr_setup_bulk(pxfer,
1041 sxfer->curr_data, max_bulk, uxfer->timeout);
1043 /* update counters */
1044 sxfer->last_len = max_bulk;
1045 sxfer->curr_data += max_bulk;
1046 sxfer->rem_len -= max_bulk;
1048 libusb20_tr_submit(pxfer);
1050 /* check if we can fork another USB transfer */
1051 if (sxfer->rem_len == 0)
1052 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1056 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1061 /* This function must be called locked */
1064 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1066 struct libusb_super_transfer *sxfer;
1067 struct libusb_transfer *uxfer;
1073 status = libusb20_tr_get_status(pxfer);
1074 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1075 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1076 actlen = libusb20_tr_get_actual_length(pxfer);
1079 return; /* cancelled - nothing to do */
1081 uxfer = (struct libusb_transfer *)(
1082 ((uint8_t *)sxfer) + sizeof(*sxfer));
1084 flags = uxfer->flags;
1087 case LIBUSB20_TRANSFER_COMPLETED:
1089 uxfer->actual_length += actlen;
1091 /* subtract length of SETUP packet, if any */
1092 actlen -= libusb20_tr_get_length(pxfer, 0);
1094 /* check for short packet */
1095 if (sxfer->last_len != actlen) {
1096 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1097 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1099 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1103 /* check for end of data */
1104 if (sxfer->rem_len == 0) {
1105 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1110 case LIBUSB20_TRANSFER_START:
1111 if (max_bulk > sxfer->rem_len) {
1112 max_bulk = sxfer->rem_len;
1114 /* setup new CONTROL transaction */
1115 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1116 /* next fragment - don't send SETUP packet */
1117 libusb20_tr_set_length(pxfer, 0, 0);
1119 /* first fragment - send SETUP packet */
1120 libusb20_tr_set_length(pxfer, 8, 0);
1121 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1124 if (max_bulk != 0) {
1125 libusb20_tr_set_length(pxfer, max_bulk, 1);
1126 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1127 libusb20_tr_set_total_frames(pxfer, 2);
1129 libusb20_tr_set_total_frames(pxfer, 1);
1132 /* update counters */
1133 sxfer->last_len = max_bulk;
1134 sxfer->curr_data += max_bulk;
1135 sxfer->rem_len -= max_bulk;
1137 libusb20_tr_submit(pxfer);
1139 /* check if we can fork another USB transfer */
1140 if (sxfer->rem_len == 0)
1141 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1145 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1150 /* The following function must be called locked */
1153 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1155 struct libusb20_transfer *pxfer0;
1156 struct libusb20_transfer *pxfer1;
1157 struct libusb_super_transfer *sxfer;
1158 struct libusb_transfer *uxfer;
1159 struct libusb_device *dev;
1166 dev = libusb_get_device(pdev);
1168 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1169 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1171 if (pxfer0 == NULL || pxfer1 == NULL)
1172 return; /* shouldn't happen */
1175 if (libusb20_tr_pending(pxfer0))
1177 if (libusb20_tr_pending(pxfer1))
1182 /* wait till one of the transfers complete */
1185 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1187 return; /* cancelling */
1189 return; /* cannot queue another one */
1190 /* swap transfers */
1194 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1196 return; /* cancelling */
1198 return; /* cannot queue another one */
1199 /* swap transfers */
1206 /* find next transfer on same endpoint */
1207 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1209 uxfer = (struct libusb_transfer *)(
1210 ((uint8_t *)sxfer) + sizeof(*sxfer));
1212 if (uxfer->endpoint == endpoint) {
1213 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1214 sxfer->entry.tqe_prev = NULL;
1218 return; /* success */
1222 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1223 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1225 /* reset super transfer state */
1226 sxfer->rem_len = uxfer->length;
1227 sxfer->curr_data = uxfer->buffer;
1228 uxfer->actual_length = 0;
1230 switch (uxfer->type) {
1231 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1232 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1234 case LIBUSB_TRANSFER_TYPE_BULK:
1235 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1236 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1238 case LIBUSB_TRANSFER_TYPE_CONTROL:
1239 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1240 if (sxfer->rem_len < 8)
1243 /* remove SETUP packet from data */
1244 sxfer->rem_len -= 8;
1245 sxfer->curr_data += 8;
1251 buffsize = libusb10_get_buffsize(pdev, uxfer);
1252 maxframe = libusb10_get_maxframe(pdev, uxfer);
1254 /* make sure the transfer is opened */
1255 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1256 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1259 libusb20_tr_start(pxfer0);
1263 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1265 /* make sure our event loop spins the done handler */
1267 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1270 /* The following function must be called unlocked */
1273 libusb_submit_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 if (uxfer->dev_handle == NULL)
1286 return (LIBUSB_ERROR_INVALID_PARAM);
1288 endpoint = uxfer->endpoint;
1291 return (LIBUSB_ERROR_INVALID_PARAM);
1293 dev = libusb_get_device(uxfer->dev_handle);
1295 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1297 sxfer = (struct libusb_super_transfer *)(
1298 (uint8_t *)uxfer - sizeof(*sxfer));
1302 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1303 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1305 if (pxfer0 == NULL || pxfer1 == NULL) {
1306 err = LIBUSB_ERROR_OTHER;
1307 } else if ((sxfer->entry.tqe_prev != NULL) ||
1308 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1309 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1310 err = LIBUSB_ERROR_BUSY;
1313 /* set pending state */
1314 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1316 /* insert transfer into transfer head list */
1317 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1319 /* start work transfers */
1320 libusb10_submit_transfer_sub(
1321 uxfer->dev_handle, endpoint);
1323 err = 0; /* success */
1326 CTX_UNLOCK(dev->ctx);
1328 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1333 /* Asynchronous transfer cancel */
1336 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1338 struct libusb20_transfer *pxfer0;
1339 struct libusb20_transfer *pxfer1;
1340 struct libusb_super_transfer *sxfer;
1341 struct libusb_device *dev;
1346 return (LIBUSB_ERROR_INVALID_PARAM);
1348 /* check if not initialised */
1349 if (uxfer->dev_handle == NULL)
1350 return (LIBUSB_ERROR_NOT_FOUND);
1352 endpoint = uxfer->endpoint;
1355 return (LIBUSB_ERROR_INVALID_PARAM);
1357 dev = libusb_get_device(uxfer->dev_handle);
1359 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1361 sxfer = (struct libusb_super_transfer *)(
1362 (uint8_t *)uxfer - sizeof(*sxfer));
1368 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1369 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1371 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1372 /* only update the transfer status */
1373 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1374 retval = LIBUSB_ERROR_NOT_FOUND;
1375 } else if (sxfer->entry.tqe_prev != NULL) {
1376 /* we are lucky - transfer is on a queue */
1377 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1378 sxfer->entry.tqe_prev = NULL;
1379 libusb10_complete_transfer(NULL,
1380 sxfer, LIBUSB_TRANSFER_CANCELLED);
1381 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1383 retval = LIBUSB_ERROR_NOT_FOUND;
1384 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1385 libusb10_complete_transfer(pxfer0,
1386 sxfer, LIBUSB_TRANSFER_CANCELLED);
1387 libusb20_tr_stop(pxfer0);
1388 /* make sure the queue doesn't stall */
1389 libusb10_submit_transfer_sub(
1390 uxfer->dev_handle, endpoint);
1391 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1392 libusb10_complete_transfer(pxfer1,
1393 sxfer, LIBUSB_TRANSFER_CANCELLED);
1394 libusb20_tr_stop(pxfer1);
1395 /* make sure the queue doesn't stall */
1396 libusb10_submit_transfer_sub(
1397 uxfer->dev_handle, endpoint);
1400 retval = LIBUSB_ERROR_NOT_FOUND;
1403 CTX_UNLOCK(dev->ctx);
1405 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1411 libusb10_cancel_all_transfer(libusb_device *dev)
1417 libusb_cpu_to_le16(uint16_t x)
1419 return (htole16(x));
1423 libusb_le16_to_cpu(uint16_t x)
1425 return (le16toh(x));
1429 libusb_strerror(int code)
1432 return ("Unknown error");