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_device_speed(libusb_device *dev)
279 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
281 switch (libusb20_dev_get_speed(dev->os_priv)) {
282 case LIBUSB20_SPEED_LOW:
283 return (LIBUSB_SPEED_LOW);
284 case LIBUSB20_SPEED_FULL:
285 return (LIBUSB_SPEED_FULL);
286 case LIBUSB20_SPEED_HIGH:
287 return (LIBUSB_SPEED_HIGH);
288 case LIBUSB20_SPEED_SUPER:
289 return (LIBUSB_SPEED_SUPER);
293 return (LIBUSB_SPEED_UNKNOWN);
297 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
299 struct libusb_config_descriptor *pdconf;
300 struct libusb_interface *pinf;
301 struct libusb_interface_descriptor *pdinf;
302 struct libusb_endpoint_descriptor *pdend;
309 return (LIBUSB_ERROR_NO_DEVICE);
311 ret = libusb_get_active_config_descriptor(dev, &pdconf);
315 ret = LIBUSB_ERROR_NOT_FOUND;
316 for (i = 0; i < pdconf->bNumInterfaces; i++) {
317 pinf = &pdconf->interface[i];
318 for (j = 0; j < pinf->num_altsetting; j++) {
319 pdinf = &pinf->altsetting[j];
320 for (k = 0; k < pdinf->bNumEndpoints; k++) {
321 pdend = &pdinf->endpoint[k];
322 if (pdend->bEndpointAddress == endpoint) {
323 ret = pdend->wMaxPacketSize;
331 libusb_free_config_descriptor(pdconf);
336 libusb_ref_device(libusb_device *dev)
339 return (NULL); /* be NULL safe */
343 CTX_UNLOCK(dev->ctx);
349 libusb_unref_device(libusb_device *dev)
352 return; /* be NULL safe */
356 CTX_UNLOCK(dev->ctx);
358 if (dev->refcnt == 0) {
359 libusb20_dev_free(dev->os_priv);
365 libusb_open(libusb_device *dev, libusb_device_handle **devh)
367 libusb_context *ctx = dev->ctx;
368 struct libusb20_device *pdev = dev->os_priv;
373 return (LIBUSB_ERROR_INVALID_PARAM);
375 /* set default device handle value */
378 dev = libusb_ref_device(dev);
380 return (LIBUSB_ERROR_INVALID_PARAM);
382 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
384 libusb_unref_device(dev);
385 return (LIBUSB_ERROR_NO_MEM);
387 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
388 POLLOUT | POLLRDNORM | POLLWRNORM);
390 /* make sure our event loop detects the new device */
392 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
393 if (err < (int)sizeof(dummy)) {
394 /* ignore error, if any */
395 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
402 libusb_device_handle *
403 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
406 struct libusb_device **devs;
407 struct libusb20_device *pdev;
408 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
412 ctx = GET_CONTEXT(ctx);
414 return (NULL); /* be NULL safe */
416 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
418 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
421 for (j = 0; j < i; j++) {
422 pdev = devs[j]->os_priv;
423 pdesc = libusb20_dev_get_device_desc(pdev);
425 * NOTE: The USB library will automatically swap the
426 * fields in the device descriptor to be of host
429 if (pdesc->idVendor == vendor_id &&
430 pdesc->idProduct == product_id) {
431 if (libusb_open(devs[j], &pdev) < 0)
439 libusb_free_device_list(devs, 1);
440 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
445 libusb_close(struct libusb20_device *pdev)
448 struct libusb_device *dev;
453 return; /* be NULL safe */
455 dev = libusb_get_device(pdev);
458 libusb10_remove_pollfd(ctx, &dev->dev_poll);
460 libusb20_dev_close(pdev);
462 /* unref will free the "pdev" when the refcount reaches zero */
463 libusb_unref_device(dev);
465 /* make sure our event loop detects the closed device */
467 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
468 if (err < (int)sizeof(dummy)) {
469 /* ignore error, if any */
470 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
475 libusb_get_device(struct libusb20_device *pdev)
479 return ((libusb_device *)pdev->privLuData);
483 libusb_get_configuration(struct libusb20_device *pdev, int *config)
485 struct libusb20_config *pconf;
487 if (pdev == NULL || config == NULL)
488 return (LIBUSB_ERROR_INVALID_PARAM);
490 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
492 return (LIBUSB_ERROR_NO_MEM);
494 *config = pconf->desc.bConfigurationValue;
502 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
504 struct libusb20_config *pconf;
505 struct libusb_device *dev;
509 dev = libusb_get_device(pdev);
511 return (LIBUSB_ERROR_INVALID_PARAM);
513 if (configuration < 1) {
517 for (i = 0; i != 255; i++) {
520 pconf = libusb20_dev_alloc_config(pdev, i);
522 return (LIBUSB_ERROR_INVALID_PARAM);
523 found = (pconf->desc.bConfigurationValue
530 return (LIBUSB_ERROR_INVALID_PARAM);
535 libusb10_cancel_all_transfer(dev);
537 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
539 err = libusb20_dev_set_config_index(pdev, i);
541 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
542 POLLOUT | POLLRDNORM | POLLWRNORM);
544 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
548 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
553 dev = libusb_get_device(pdev);
555 return (LIBUSB_ERROR_INVALID_PARAM);
557 if (interface_number < 0 || interface_number > 31)
558 return (LIBUSB_ERROR_INVALID_PARAM);
561 if (dev->claimed_interfaces & (1 << interface_number))
562 err = LIBUSB_ERROR_BUSY;
565 dev->claimed_interfaces |= (1 << interface_number);
566 CTX_UNLOCK(dev->ctx);
571 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
576 dev = libusb_get_device(pdev);
578 return (LIBUSB_ERROR_INVALID_PARAM);
580 if (interface_number < 0 || interface_number > 31)
581 return (LIBUSB_ERROR_INVALID_PARAM);
584 if (!(dev->claimed_interfaces & (1 << interface_number)))
585 err = LIBUSB_ERROR_NOT_FOUND;
588 dev->claimed_interfaces &= ~(1 << interface_number);
589 CTX_UNLOCK(dev->ctx);
594 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
595 int interface_number, int alternate_setting)
600 dev = libusb_get_device(pdev);
602 return (LIBUSB_ERROR_INVALID_PARAM);
604 if (interface_number < 0 || interface_number > 31)
605 return (LIBUSB_ERROR_INVALID_PARAM);
608 if (!(dev->claimed_interfaces & (1 << interface_number)))
609 err = LIBUSB_ERROR_NOT_FOUND;
610 CTX_UNLOCK(dev->ctx);
615 libusb10_cancel_all_transfer(dev);
617 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
619 err = libusb20_dev_set_alt_index(pdev,
620 interface_number, alternate_setting);
622 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
623 pdev, libusb20_dev_get_fd(pdev),
624 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
626 return (err ? LIBUSB_ERROR_OTHER : 0);
629 static struct libusb20_transfer *
630 libusb10_get_transfer(struct libusb20_device *pdev,
631 uint8_t endpoint, uint8_t index)
633 index &= 1; /* double buffering */
635 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
637 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
638 /* this is an IN endpoint */
641 return (libusb20_tr_get_pointer(pdev, index));
645 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
647 struct libusb20_transfer *xfer;
648 struct libusb_device *dev;
651 xfer = libusb10_get_transfer(pdev, endpoint, 0);
653 return (LIBUSB_ERROR_INVALID_PARAM);
655 dev = libusb_get_device(pdev);
657 return (LIBUSB_ERROR_INVALID_PARAM);
660 err = libusb20_tr_open(xfer, 0, 1, endpoint);
661 CTX_UNLOCK(dev->ctx);
663 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
664 return (LIBUSB_ERROR_OTHER);
666 libusb20_tr_clear_stall_sync(xfer);
668 /* check if we opened the transfer */
671 libusb20_tr_close(xfer);
672 CTX_UNLOCK(dev->ctx);
674 return (0); /* success */
678 libusb_reset_device(struct libusb20_device *pdev)
683 dev = libusb_get_device(pdev);
685 return (LIBUSB_ERROR_INVALID_PARAM);
687 libusb10_cancel_all_transfer(dev);
689 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
691 err = libusb20_dev_reset(pdev);
693 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
694 pdev, libusb20_dev_get_fd(pdev),
695 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
697 return (err ? LIBUSB_ERROR_OTHER : 0);
701 libusb_check_connected(struct libusb20_device *pdev)
706 dev = libusb_get_device(pdev);
708 return (LIBUSB_ERROR_INVALID_PARAM);
710 err = libusb20_dev_check_connected(pdev);
712 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
716 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
719 return (LIBUSB_ERROR_INVALID_PARAM);
721 return (libusb20_dev_kernel_driver_active(
726 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
727 char *name, int namelen)
729 return (libusb_get_driver(pdev, interface, name, namelen));
733 libusb_get_driver(struct libusb20_device *pdev, int interface,
734 char *name, int namelen)
740 return (LIBUSB_ERROR_INVALID_PARAM);
742 return (LIBUSB_ERROR_INVALID_PARAM);
746 err = libusb20_dev_get_iface_desc(
747 pdev, interface, name, namelen);
750 return (LIBUSB_ERROR_OTHER);
752 /* we only want the driver name */
753 ptr = strstr(name, ":");
761 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
763 return (libusb_detach_kernel_driver(pdev, interface));
767 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
772 return (LIBUSB_ERROR_INVALID_PARAM);
774 err = libusb20_dev_detach_kernel_driver(
777 return (err ? LIBUSB_ERROR_OTHER : 0);
781 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
784 return (LIBUSB_ERROR_INVALID_PARAM);
785 /* stub - currently not supported by libusb20 */
789 /* Asynchronous device I/O */
791 struct libusb_transfer *
792 libusb_alloc_transfer(int iso_packets)
794 struct libusb_transfer *uxfer;
795 struct libusb_super_transfer *sxfer;
798 len = sizeof(struct libusb_transfer) +
799 sizeof(struct libusb_super_transfer) +
800 (iso_packets * sizeof(libusb_iso_packet_descriptor));
806 memset(sxfer, 0, len);
808 uxfer = (struct libusb_transfer *)(
809 ((uint8_t *)sxfer) + sizeof(*sxfer));
811 /* set default value */
812 uxfer->num_iso_packets = iso_packets;
818 libusb_free_transfer(struct libusb_transfer *uxfer)
820 struct libusb_super_transfer *sxfer;
823 return; /* be NULL safe */
825 /* check if we should free the transfer buffer */
826 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
829 sxfer = (struct libusb_super_transfer *)(
830 (uint8_t *)uxfer - sizeof(*sxfer));
836 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
840 switch (xfer->type) {
841 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
842 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
844 case LIBUSB_TRANSFER_TYPE_CONTROL:
855 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
860 usb_speed = libusb20_dev_get_speed(pdev);
862 switch (xfer->type) {
863 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
864 ret = 0; /* kernel will auto-select */
866 case LIBUSB_TRANSFER_TYPE_CONTROL:
871 case LIBUSB20_SPEED_LOW:
874 case LIBUSB20_SPEED_FULL:
887 libusb10_convert_error(uint8_t status)
892 case LIBUSB20_TRANSFER_START:
893 case LIBUSB20_TRANSFER_COMPLETED:
894 return (LIBUSB_TRANSFER_COMPLETED);
895 case LIBUSB20_TRANSFER_OVERFLOW:
896 return (LIBUSB_TRANSFER_OVERFLOW);
897 case LIBUSB20_TRANSFER_NO_DEVICE:
898 return (LIBUSB_TRANSFER_NO_DEVICE);
899 case LIBUSB20_TRANSFER_STALL:
900 return (LIBUSB_TRANSFER_STALL);
901 case LIBUSB20_TRANSFER_CANCELLED:
902 return (LIBUSB_TRANSFER_CANCELLED);
903 case LIBUSB20_TRANSFER_TIMED_OUT:
904 return (LIBUSB_TRANSFER_TIMED_OUT);
906 return (LIBUSB_TRANSFER_ERROR);
910 /* This function must be called locked */
913 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
914 struct libusb_super_transfer *sxfer, int status)
916 struct libusb_transfer *uxfer;
917 struct libusb_device *dev;
919 uxfer = (struct libusb_transfer *)(
920 ((uint8_t *)sxfer) + sizeof(*sxfer));
923 libusb20_tr_set_priv_sc1(pxfer, NULL);
925 /* set transfer status */
926 uxfer->status = status;
928 /* update super transfer state */
929 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
931 dev = libusb_get_device(uxfer->dev_handle);
933 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
936 /* This function must be called locked */
939 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
941 struct libusb_super_transfer *sxfer;
942 struct libusb_transfer *uxfer;
944 uint16_t iso_packets;
949 status = libusb20_tr_get_status(pxfer);
950 sxfer = libusb20_tr_get_priv_sc1(pxfer);
951 actlen = libusb20_tr_get_actual_length(pxfer);
952 iso_packets = libusb20_tr_get_max_frames(pxfer);
955 return; /* cancelled - nothing to do */
957 uxfer = (struct libusb_transfer *)(
958 ((uint8_t *)sxfer) + sizeof(*sxfer));
960 if (iso_packets > uxfer->num_iso_packets)
961 iso_packets = uxfer->num_iso_packets;
963 if (iso_packets == 0)
964 return; /* nothing to do */
966 /* make sure that the number of ISOCHRONOUS packets is valid */
967 uxfer->num_iso_packets = iso_packets;
969 flags = uxfer->flags;
972 case LIBUSB20_TRANSFER_COMPLETED:
974 /* update actual length */
975 uxfer->actual_length = actlen;
976 for (i = 0; i != iso_packets; i++) {
977 uxfer->iso_packet_desc[i].actual_length =
978 libusb20_tr_get_length(pxfer, i);
980 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
983 case LIBUSB20_TRANSFER_START:
985 /* setup length(s) */
987 for (i = 0; i != iso_packets; i++) {
988 libusb20_tr_setup_isoc(pxfer,
989 &uxfer->buffer[actlen],
990 uxfer->iso_packet_desc[i].length, i);
991 actlen += uxfer->iso_packet_desc[i].length;
997 libusb20_tr_set_total_frames(pxfer, iso_packets);
998 libusb20_tr_submit(pxfer);
1000 /* fork another USB transfer, if any */
1001 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1005 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1010 /* This function must be called locked */
1013 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1015 struct libusb_super_transfer *sxfer;
1016 struct libusb_transfer *uxfer;
1022 status = libusb20_tr_get_status(pxfer);
1023 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1024 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1025 actlen = libusb20_tr_get_actual_length(pxfer);
1028 return; /* cancelled - nothing to do */
1030 uxfer = (struct libusb_transfer *)(
1031 ((uint8_t *)sxfer) + sizeof(*sxfer));
1033 flags = uxfer->flags;
1036 case LIBUSB20_TRANSFER_COMPLETED:
1038 uxfer->actual_length += actlen;
1040 /* check for short packet */
1041 if (sxfer->last_len != actlen) {
1042 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1043 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1045 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1049 /* check for end of data */
1050 if (sxfer->rem_len == 0) {
1051 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1056 case LIBUSB20_TRANSFER_START:
1057 if (max_bulk > sxfer->rem_len) {
1058 max_bulk = sxfer->rem_len;
1060 /* setup new BULK or INTERRUPT transaction */
1061 libusb20_tr_setup_bulk(pxfer,
1062 sxfer->curr_data, max_bulk, uxfer->timeout);
1064 /* update counters */
1065 sxfer->last_len = max_bulk;
1066 sxfer->curr_data += max_bulk;
1067 sxfer->rem_len -= max_bulk;
1069 libusb20_tr_submit(pxfer);
1071 /* check if we can fork another USB transfer */
1072 if (sxfer->rem_len == 0)
1073 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1077 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1082 /* This function must be called locked */
1085 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1087 struct libusb_super_transfer *sxfer;
1088 struct libusb_transfer *uxfer;
1094 status = libusb20_tr_get_status(pxfer);
1095 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1096 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1097 actlen = libusb20_tr_get_actual_length(pxfer);
1100 return; /* cancelled - nothing to do */
1102 uxfer = (struct libusb_transfer *)(
1103 ((uint8_t *)sxfer) + sizeof(*sxfer));
1105 flags = uxfer->flags;
1108 case LIBUSB20_TRANSFER_COMPLETED:
1110 uxfer->actual_length += actlen;
1112 /* subtract length of SETUP packet, if any */
1113 actlen -= libusb20_tr_get_length(pxfer, 0);
1115 /* check for short packet */
1116 if (sxfer->last_len != actlen) {
1117 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1118 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1120 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1124 /* check for end of data */
1125 if (sxfer->rem_len == 0) {
1126 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1131 case LIBUSB20_TRANSFER_START:
1132 if (max_bulk > sxfer->rem_len) {
1133 max_bulk = sxfer->rem_len;
1135 /* setup new CONTROL transaction */
1136 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1137 /* next fragment - don't send SETUP packet */
1138 libusb20_tr_set_length(pxfer, 0, 0);
1140 /* first fragment - send SETUP packet */
1141 libusb20_tr_set_length(pxfer, 8, 0);
1142 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1145 if (max_bulk != 0) {
1146 libusb20_tr_set_length(pxfer, max_bulk, 1);
1147 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1148 libusb20_tr_set_total_frames(pxfer, 2);
1150 libusb20_tr_set_total_frames(pxfer, 1);
1153 /* update counters */
1154 sxfer->last_len = max_bulk;
1155 sxfer->curr_data += max_bulk;
1156 sxfer->rem_len -= max_bulk;
1158 libusb20_tr_submit(pxfer);
1160 /* check if we can fork another USB transfer */
1161 if (sxfer->rem_len == 0)
1162 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1166 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1171 /* The following function must be called locked */
1174 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1176 struct libusb20_transfer *pxfer0;
1177 struct libusb20_transfer *pxfer1;
1178 struct libusb_super_transfer *sxfer;
1179 struct libusb_transfer *uxfer;
1180 struct libusb_device *dev;
1187 dev = libusb_get_device(pdev);
1189 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1190 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1192 if (pxfer0 == NULL || pxfer1 == NULL)
1193 return; /* shouldn't happen */
1196 if (libusb20_tr_pending(pxfer0))
1198 if (libusb20_tr_pending(pxfer1))
1203 /* wait till one of the transfers complete */
1206 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1208 return; /* cancelling */
1210 return; /* cannot queue another one */
1211 /* swap transfers */
1215 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1217 return; /* cancelling */
1219 return; /* cannot queue another one */
1220 /* swap transfers */
1227 /* find next transfer on same endpoint */
1228 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1230 uxfer = (struct libusb_transfer *)(
1231 ((uint8_t *)sxfer) + sizeof(*sxfer));
1233 if (uxfer->endpoint == endpoint) {
1234 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1235 sxfer->entry.tqe_prev = NULL;
1239 return; /* success */
1243 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1244 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1246 /* reset super transfer state */
1247 sxfer->rem_len = uxfer->length;
1248 sxfer->curr_data = uxfer->buffer;
1249 uxfer->actual_length = 0;
1251 switch (uxfer->type) {
1252 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1253 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1255 case LIBUSB_TRANSFER_TYPE_BULK:
1256 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1257 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1259 case LIBUSB_TRANSFER_TYPE_CONTROL:
1260 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1261 if (sxfer->rem_len < 8)
1264 /* remove SETUP packet from data */
1265 sxfer->rem_len -= 8;
1266 sxfer->curr_data += 8;
1272 buffsize = libusb10_get_buffsize(pdev, uxfer);
1273 maxframe = libusb10_get_maxframe(pdev, uxfer);
1275 /* make sure the transfer is opened */
1276 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1277 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1280 libusb20_tr_start(pxfer0);
1284 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1286 /* make sure our event loop spins the done handler */
1288 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1291 /* The following function must be called unlocked */
1294 libusb_submit_transfer(struct libusb_transfer *uxfer)
1296 struct libusb20_transfer *pxfer0;
1297 struct libusb20_transfer *pxfer1;
1298 struct libusb_super_transfer *sxfer;
1299 struct libusb_device *dev;
1304 return (LIBUSB_ERROR_INVALID_PARAM);
1306 if (uxfer->dev_handle == NULL)
1307 return (LIBUSB_ERROR_INVALID_PARAM);
1309 endpoint = uxfer->endpoint;
1312 return (LIBUSB_ERROR_INVALID_PARAM);
1314 dev = libusb_get_device(uxfer->dev_handle);
1316 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1318 sxfer = (struct libusb_super_transfer *)(
1319 (uint8_t *)uxfer - sizeof(*sxfer));
1323 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1324 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1326 if (pxfer0 == NULL || pxfer1 == NULL) {
1327 err = LIBUSB_ERROR_OTHER;
1328 } else if ((sxfer->entry.tqe_prev != NULL) ||
1329 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1330 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1331 err = LIBUSB_ERROR_BUSY;
1334 /* set pending state */
1335 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1337 /* insert transfer into transfer head list */
1338 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1340 /* start work transfers */
1341 libusb10_submit_transfer_sub(
1342 uxfer->dev_handle, endpoint);
1344 err = 0; /* success */
1347 CTX_UNLOCK(dev->ctx);
1349 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1354 /* Asynchronous transfer cancel */
1357 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1359 struct libusb20_transfer *pxfer0;
1360 struct libusb20_transfer *pxfer1;
1361 struct libusb_super_transfer *sxfer;
1362 struct libusb_device *dev;
1367 return (LIBUSB_ERROR_INVALID_PARAM);
1369 /* check if not initialised */
1370 if (uxfer->dev_handle == NULL)
1371 return (LIBUSB_ERROR_NOT_FOUND);
1373 endpoint = uxfer->endpoint;
1376 return (LIBUSB_ERROR_INVALID_PARAM);
1378 dev = libusb_get_device(uxfer->dev_handle);
1380 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1382 sxfer = (struct libusb_super_transfer *)(
1383 (uint8_t *)uxfer - sizeof(*sxfer));
1389 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1390 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1392 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1393 /* only update the transfer status */
1394 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1395 retval = LIBUSB_ERROR_NOT_FOUND;
1396 } else if (sxfer->entry.tqe_prev != NULL) {
1397 /* we are lucky - transfer is on a queue */
1398 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1399 sxfer->entry.tqe_prev = NULL;
1400 libusb10_complete_transfer(NULL,
1401 sxfer, LIBUSB_TRANSFER_CANCELLED);
1402 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1404 retval = LIBUSB_ERROR_NOT_FOUND;
1405 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1406 libusb10_complete_transfer(pxfer0,
1407 sxfer, LIBUSB_TRANSFER_CANCELLED);
1408 libusb20_tr_stop(pxfer0);
1409 /* make sure the queue doesn't stall */
1410 libusb10_submit_transfer_sub(
1411 uxfer->dev_handle, endpoint);
1412 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1413 libusb10_complete_transfer(pxfer1,
1414 sxfer, LIBUSB_TRANSFER_CANCELLED);
1415 libusb20_tr_stop(pxfer1);
1416 /* make sure the queue doesn't stall */
1417 libusb10_submit_transfer_sub(
1418 uxfer->dev_handle, endpoint);
1421 retval = LIBUSB_ERROR_NOT_FOUND;
1424 CTX_UNLOCK(dev->ctx);
1426 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1432 libusb10_cancel_all_transfer(libusb_device *dev)
1438 libusb_cpu_to_le16(uint16_t x)
1440 return (htole16(x));
1444 libusb_le16_to_cpu(uint16_t x)
1446 return (le16toh(x));
1450 libusb_strerror(int code)
1453 case LIBUSB_SUCCESS:
1455 case LIBUSB_ERROR_IO:
1456 return ("I/O error");
1457 case LIBUSB_ERROR_INVALID_PARAM:
1458 return ("Invalid parameter");
1459 case LIBUSB_ERROR_ACCESS:
1460 return ("Permissions error");
1461 case LIBUSB_ERROR_NO_DEVICE:
1462 return ("No device");
1463 case LIBUSB_ERROR_NOT_FOUND:
1464 return ("Not found");
1465 case LIBUSB_ERROR_BUSY:
1466 return ("Device busy");
1467 case LIBUSB_ERROR_TIMEOUT:
1469 case LIBUSB_ERROR_OVERFLOW:
1470 return ("Overflow");
1471 case LIBUSB_ERROR_PIPE:
1472 return ("Pipe error");
1473 case LIBUSB_ERROR_INTERRUPTED:
1474 return ("Interrupted");
1475 case LIBUSB_ERROR_NO_MEM:
1476 return ("Out of memory");
1477 case LIBUSB_ERROR_NOT_SUPPORTED:
1478 return ("Not supported");
1479 case LIBUSB_ERROR_OTHER:
1480 return ("Other error");
1482 return ("Unknown error");
1487 libusb_error_name(int code)
1490 case LIBUSB_SUCCESS:
1491 return ("LIBUSB_SUCCESS");
1492 case LIBUSB_ERROR_IO:
1493 return ("LIBUSB_ERROR_IO");
1494 case LIBUSB_ERROR_INVALID_PARAM:
1495 return ("LIBUSB_ERROR_INVALID_PARAM");
1496 case LIBUSB_ERROR_ACCESS:
1497 return ("LIBUSB_ERROR_ACCESS");
1498 case LIBUSB_ERROR_NO_DEVICE:
1499 return ("LIBUSB_ERROR_NO_DEVICE");
1500 case LIBUSB_ERROR_NOT_FOUND:
1501 return ("LIBUSB_ERROR_NOT_FOUND");
1502 case LIBUSB_ERROR_BUSY:
1503 return ("LIBUSB_ERROR_BUSY");
1504 case LIBUSB_ERROR_TIMEOUT:
1505 return ("LIBUSB_ERROR_TIMEOUT");
1506 case LIBUSB_ERROR_OVERFLOW:
1507 return ("LIBUSB_ERROR_OVERFLOW");
1508 case LIBUSB_ERROR_PIPE:
1509 return ("LIBUSB_ERROR_PIPE");
1510 case LIBUSB_ERROR_INTERRUPTED:
1511 return ("LIBUSB_ERROR_INTERRUPTED");
1512 case LIBUSB_ERROR_NO_MEM:
1513 return ("LIBUSB_ERROR_NO_MEM");
1514 case LIBUSB_ERROR_NOT_SUPPORTED:
1515 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1516 case LIBUSB_ERROR_OTHER:
1517 return ("LIBUSB_ERROR_OTHER");
1519 return ("LIBUSB_ERROR_UNKNOWN");