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);
219 /* get device into libUSB v1.0 list */
220 libusb20_be_dequeue_device(usb_backend, pdev);
222 memset(dev, 0, sizeof(*dev));
224 /* init transfer queues */
225 TAILQ_INIT(&dev->tr_head);
227 /* set context we belong to */
230 /* link together the two structures */
232 pdev->privLuData = dev;
234 (*list)[i] = libusb_ref_device(dev);
239 libusb20_be_free(usb_backend);
244 libusb_free_device_list(libusb_device **list, int unref_devices)
249 return; /* be NULL safe */
252 for (i = 0; list[i] != NULL; i++)
253 libusb_unref_device(list[i]);
259 libusb_get_bus_number(libusb_device *dev)
262 return (0); /* should not happen */
263 return (libusb20_dev_get_bus_number(dev->os_priv));
267 libusb_get_device_address(libusb_device *dev)
270 return (0); /* should not happen */
271 return (libusb20_dev_get_address(dev->os_priv));
275 libusb_get_device_speed(libusb_device *dev)
278 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
280 switch (libusb20_dev_get_speed(dev->os_priv)) {
281 case LIBUSB20_SPEED_LOW:
282 return (LIBUSB_SPEED_LOW);
283 case LIBUSB20_SPEED_FULL:
284 return (LIBUSB_SPEED_FULL);
285 case LIBUSB20_SPEED_HIGH:
286 return (LIBUSB_SPEED_HIGH);
287 case LIBUSB20_SPEED_SUPER:
288 return (LIBUSB_SPEED_SUPER);
292 return (LIBUSB_SPEED_UNKNOWN);
296 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
298 struct libusb_config_descriptor *pdconf;
299 struct libusb_interface *pinf;
300 struct libusb_interface_descriptor *pdinf;
301 struct libusb_endpoint_descriptor *pdend;
308 return (LIBUSB_ERROR_NO_DEVICE);
310 ret = libusb_get_active_config_descriptor(dev, &pdconf);
314 ret = LIBUSB_ERROR_NOT_FOUND;
315 for (i = 0; i < pdconf->bNumInterfaces; i++) {
316 pinf = &pdconf->interface[i];
317 for (j = 0; j < pinf->num_altsetting; j++) {
318 pdinf = &pinf->altsetting[j];
319 for (k = 0; k < pdinf->bNumEndpoints; k++) {
320 pdend = &pdinf->endpoint[k];
321 if (pdend->bEndpointAddress == endpoint) {
322 ret = pdend->wMaxPacketSize;
330 libusb_free_config_descriptor(pdconf);
335 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
340 ret = libusb_get_max_packet_size(dev, endpoint);
342 switch (libusb20_dev_get_speed(dev->os_priv)) {
343 case LIBUSB20_SPEED_LOW:
344 case LIBUSB20_SPEED_FULL:
348 multiplier = (1 + ((ret >> 11) & 3));
351 ret = (ret & 0x7FF) * multiplier;
359 libusb_ref_device(libusb_device *dev)
362 return (NULL); /* be NULL safe */
366 CTX_UNLOCK(dev->ctx);
372 libusb_unref_device(libusb_device *dev)
375 return; /* be NULL safe */
379 CTX_UNLOCK(dev->ctx);
381 if (dev->refcnt == 0) {
382 libusb20_dev_free(dev->os_priv);
388 libusb_open(libusb_device *dev, libusb_device_handle **devh)
390 libusb_context *ctx = dev->ctx;
391 struct libusb20_device *pdev = dev->os_priv;
396 return (LIBUSB_ERROR_INVALID_PARAM);
398 /* set default device handle value */
401 dev = libusb_ref_device(dev);
403 return (LIBUSB_ERROR_INVALID_PARAM);
405 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
407 libusb_unref_device(dev);
408 return (LIBUSB_ERROR_NO_MEM);
410 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
411 POLLOUT | POLLRDNORM | POLLWRNORM);
413 /* make sure our event loop detects the new device */
415 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
416 if (err < (int)sizeof(dummy)) {
417 /* ignore error, if any */
418 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
425 libusb_device_handle *
426 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
429 struct libusb_device **devs;
430 struct libusb20_device *pdev;
431 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
435 ctx = GET_CONTEXT(ctx);
437 return (NULL); /* be NULL safe */
439 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
441 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
445 for (j = 0; j < i; j++) {
446 struct libusb20_device *tdev;
448 tdev = devs[j]->os_priv;
449 pdesc = libusb20_dev_get_device_desc(tdev);
451 * NOTE: The USB library will automatically swap the
452 * fields in the device descriptor to be of host
455 if (pdesc->idVendor == vendor_id &&
456 pdesc->idProduct == product_id) {
457 libusb_open(devs[j], &pdev);
462 libusb_free_device_list(devs, 1);
463 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
468 libusb_close(struct libusb20_device *pdev)
471 struct libusb_device *dev;
476 return; /* be NULL safe */
478 dev = libusb_get_device(pdev);
481 libusb10_remove_pollfd(ctx, &dev->dev_poll);
483 libusb20_dev_close(pdev);
485 /* unref will free the "pdev" when the refcount reaches zero */
486 libusb_unref_device(dev);
488 /* make sure our event loop detects the closed device */
490 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
491 if (err < (int)sizeof(dummy)) {
492 /* ignore error, if any */
493 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
498 libusb_get_device(struct libusb20_device *pdev)
502 return ((libusb_device *)pdev->privLuData);
506 libusb_get_configuration(struct libusb20_device *pdev, int *config)
508 struct libusb20_config *pconf;
510 if (pdev == NULL || config == NULL)
511 return (LIBUSB_ERROR_INVALID_PARAM);
513 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
515 return (LIBUSB_ERROR_NO_MEM);
517 *config = pconf->desc.bConfigurationValue;
525 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
527 struct libusb20_config *pconf;
528 struct libusb_device *dev;
532 dev = libusb_get_device(pdev);
534 return (LIBUSB_ERROR_INVALID_PARAM);
536 if (configuration < 1) {
540 for (i = 0; i != 255; i++) {
543 pconf = libusb20_dev_alloc_config(pdev, i);
545 return (LIBUSB_ERROR_INVALID_PARAM);
546 found = (pconf->desc.bConfigurationValue
553 return (LIBUSB_ERROR_INVALID_PARAM);
558 libusb10_cancel_all_transfer(dev);
560 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
562 err = libusb20_dev_set_config_index(pdev, i);
564 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
565 POLLOUT | POLLRDNORM | POLLWRNORM);
567 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
571 libusb_claim_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_BUSY;
588 dev->claimed_interfaces |= (1 << interface_number);
589 CTX_UNLOCK(dev->ctx);
594 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
599 dev = libusb_get_device(pdev);
601 return (LIBUSB_ERROR_INVALID_PARAM);
603 if (interface_number < 0 || interface_number > 31)
604 return (LIBUSB_ERROR_INVALID_PARAM);
607 if (!(dev->claimed_interfaces & (1 << interface_number)))
608 err = LIBUSB_ERROR_NOT_FOUND;
611 dev->claimed_interfaces &= ~(1 << interface_number);
612 CTX_UNLOCK(dev->ctx);
617 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
618 int interface_number, int alternate_setting)
623 dev = libusb_get_device(pdev);
625 return (LIBUSB_ERROR_INVALID_PARAM);
627 if (interface_number < 0 || interface_number > 31)
628 return (LIBUSB_ERROR_INVALID_PARAM);
631 if (!(dev->claimed_interfaces & (1 << interface_number)))
632 err = LIBUSB_ERROR_NOT_FOUND;
633 CTX_UNLOCK(dev->ctx);
638 libusb10_cancel_all_transfer(dev);
640 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
642 err = libusb20_dev_set_alt_index(pdev,
643 interface_number, alternate_setting);
645 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
646 pdev, libusb20_dev_get_fd(pdev),
647 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
649 return (err ? LIBUSB_ERROR_OTHER : 0);
652 static struct libusb20_transfer *
653 libusb10_get_transfer(struct libusb20_device *pdev,
654 uint8_t endpoint, uint8_t xfer_index)
656 xfer_index &= 1; /* double buffering */
658 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
660 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
661 /* this is an IN endpoint */
664 return (libusb20_tr_get_pointer(pdev, xfer_index));
668 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
670 struct libusb20_transfer *xfer;
671 struct libusb_device *dev;
674 xfer = libusb10_get_transfer(pdev, endpoint, 0);
676 return (LIBUSB_ERROR_INVALID_PARAM);
678 dev = libusb_get_device(pdev);
680 return (LIBUSB_ERROR_INVALID_PARAM);
683 err = libusb20_tr_open(xfer, 0, 1, endpoint);
684 CTX_UNLOCK(dev->ctx);
686 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
687 return (LIBUSB_ERROR_OTHER);
689 libusb20_tr_clear_stall_sync(xfer);
691 /* check if we opened the transfer */
694 libusb20_tr_close(xfer);
695 CTX_UNLOCK(dev->ctx);
697 return (0); /* success */
701 libusb_reset_device(struct libusb20_device *pdev)
706 dev = libusb_get_device(pdev);
708 return (LIBUSB_ERROR_INVALID_PARAM);
710 libusb10_cancel_all_transfer(dev);
712 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
714 err = libusb20_dev_reset(pdev);
716 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
717 pdev, libusb20_dev_get_fd(pdev),
718 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
720 return (err ? LIBUSB_ERROR_OTHER : 0);
724 libusb_check_connected(struct libusb20_device *pdev)
729 dev = libusb_get_device(pdev);
731 return (LIBUSB_ERROR_INVALID_PARAM);
733 err = libusb20_dev_check_connected(pdev);
735 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
739 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
742 return (LIBUSB_ERROR_INVALID_PARAM);
744 if (libusb20_dev_kernel_driver_active(pdev, interface))
745 return (0); /* no kernel driver is active */
747 return (1); /* kernel driver is active */
751 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
752 char *name, int namelen)
754 return (libusb_get_driver(pdev, interface, name, namelen));
758 libusb_get_driver(struct libusb20_device *pdev, int interface,
759 char *name, int namelen)
765 return (LIBUSB_ERROR_INVALID_PARAM);
767 return (LIBUSB_ERROR_INVALID_PARAM);
771 err = libusb20_dev_get_iface_desc(
772 pdev, interface, name, namelen);
775 return (LIBUSB_ERROR_OTHER);
777 /* we only want the driver name */
778 ptr = strstr(name, ":");
786 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
788 return (libusb_detach_kernel_driver(pdev, interface));
792 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
797 return (LIBUSB_ERROR_INVALID_PARAM);
799 err = libusb20_dev_detach_kernel_driver(
802 return (err ? LIBUSB_ERROR_OTHER : 0);
806 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
809 return (LIBUSB_ERROR_INVALID_PARAM);
810 /* stub - currently not supported by libusb20 */
814 /* Asynchronous device I/O */
816 struct libusb_transfer *
817 libusb_alloc_transfer(int iso_packets)
819 struct libusb_transfer *uxfer;
820 struct libusb_super_transfer *sxfer;
823 len = sizeof(struct libusb_transfer) +
824 sizeof(struct libusb_super_transfer) +
825 (iso_packets * sizeof(libusb_iso_packet_descriptor));
831 memset(sxfer, 0, len);
833 uxfer = (struct libusb_transfer *)(
834 ((uint8_t *)sxfer) + sizeof(*sxfer));
836 /* set default value */
837 uxfer->num_iso_packets = iso_packets;
843 libusb_free_transfer(struct libusb_transfer *uxfer)
845 struct libusb_super_transfer *sxfer;
848 return; /* be NULL safe */
850 /* check if we should free the transfer buffer */
851 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
854 sxfer = (struct libusb_super_transfer *)(
855 (uint8_t *)uxfer - sizeof(*sxfer));
861 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
865 switch (xfer->type) {
866 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
867 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
869 case LIBUSB_TRANSFER_TYPE_CONTROL:
880 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
885 usb_speed = libusb20_dev_get_speed(pdev);
887 switch (xfer->type) {
888 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
889 ret = 0; /* kernel will auto-select */
891 case LIBUSB_TRANSFER_TYPE_CONTROL:
896 case LIBUSB20_SPEED_LOW:
899 case LIBUSB20_SPEED_FULL:
912 libusb10_convert_error(uint8_t status)
917 case LIBUSB20_TRANSFER_START:
918 case LIBUSB20_TRANSFER_COMPLETED:
919 return (LIBUSB_TRANSFER_COMPLETED);
920 case LIBUSB20_TRANSFER_OVERFLOW:
921 return (LIBUSB_TRANSFER_OVERFLOW);
922 case LIBUSB20_TRANSFER_NO_DEVICE:
923 return (LIBUSB_TRANSFER_NO_DEVICE);
924 case LIBUSB20_TRANSFER_STALL:
925 return (LIBUSB_TRANSFER_STALL);
926 case LIBUSB20_TRANSFER_CANCELLED:
927 return (LIBUSB_TRANSFER_CANCELLED);
928 case LIBUSB20_TRANSFER_TIMED_OUT:
929 return (LIBUSB_TRANSFER_TIMED_OUT);
931 return (LIBUSB_TRANSFER_ERROR);
935 /* This function must be called locked */
938 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
939 struct libusb_super_transfer *sxfer, int status)
941 struct libusb_transfer *uxfer;
942 struct libusb_device *dev;
944 uxfer = (struct libusb_transfer *)(
945 ((uint8_t *)sxfer) + sizeof(*sxfer));
948 libusb20_tr_set_priv_sc1(pxfer, NULL);
950 /* set transfer status */
951 uxfer->status = status;
953 /* update super transfer state */
954 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
956 dev = libusb_get_device(uxfer->dev_handle);
958 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
961 /* This function must be called locked */
964 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
966 struct libusb_super_transfer *sxfer;
967 struct libusb_transfer *uxfer;
969 uint16_t iso_packets;
974 status = libusb20_tr_get_status(pxfer);
975 sxfer = libusb20_tr_get_priv_sc1(pxfer);
976 actlen = libusb20_tr_get_actual_length(pxfer);
977 iso_packets = libusb20_tr_get_max_frames(pxfer);
980 return; /* cancelled - nothing to do */
982 uxfer = (struct libusb_transfer *)(
983 ((uint8_t *)sxfer) + sizeof(*sxfer));
985 if (iso_packets > uxfer->num_iso_packets)
986 iso_packets = uxfer->num_iso_packets;
988 if (iso_packets == 0)
989 return; /* nothing to do */
991 /* make sure that the number of ISOCHRONOUS packets is valid */
992 uxfer->num_iso_packets = iso_packets;
994 flags = uxfer->flags;
997 case LIBUSB20_TRANSFER_COMPLETED:
999 /* update actual length */
1000 uxfer->actual_length = actlen;
1001 for (i = 0; i != iso_packets; i++) {
1002 uxfer->iso_packet_desc[i].actual_length =
1003 libusb20_tr_get_length(pxfer, i);
1005 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1008 case LIBUSB20_TRANSFER_START:
1010 /* setup length(s) */
1012 for (i = 0; i != iso_packets; i++) {
1013 libusb20_tr_setup_isoc(pxfer,
1014 &uxfer->buffer[actlen],
1015 uxfer->iso_packet_desc[i].length, i);
1016 actlen += uxfer->iso_packet_desc[i].length;
1022 libusb20_tr_set_total_frames(pxfer, iso_packets);
1023 libusb20_tr_submit(pxfer);
1025 /* fork another USB transfer, if any */
1026 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1030 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1035 /* This function must be called locked */
1038 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1040 struct libusb_super_transfer *sxfer;
1041 struct libusb_transfer *uxfer;
1047 status = libusb20_tr_get_status(pxfer);
1048 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1049 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1050 actlen = libusb20_tr_get_actual_length(pxfer);
1053 return; /* cancelled - nothing to do */
1055 uxfer = (struct libusb_transfer *)(
1056 ((uint8_t *)sxfer) + sizeof(*sxfer));
1058 flags = uxfer->flags;
1061 case LIBUSB20_TRANSFER_COMPLETED:
1063 uxfer->actual_length += actlen;
1065 /* check for short packet */
1066 if (sxfer->last_len != actlen) {
1067 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1068 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1070 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1074 /* check for end of data */
1075 if (sxfer->rem_len == 0) {
1076 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1081 case LIBUSB20_TRANSFER_START:
1082 if (max_bulk > sxfer->rem_len) {
1083 max_bulk = sxfer->rem_len;
1085 /* setup new BULK or INTERRUPT transaction */
1086 libusb20_tr_setup_bulk(pxfer,
1087 sxfer->curr_data, max_bulk, uxfer->timeout);
1089 /* update counters */
1090 sxfer->last_len = max_bulk;
1091 sxfer->curr_data += max_bulk;
1092 sxfer->rem_len -= max_bulk;
1094 libusb20_tr_submit(pxfer);
1096 /* check if we can fork another USB transfer */
1097 if (sxfer->rem_len == 0)
1098 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1102 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1107 /* This function must be called locked */
1110 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1112 struct libusb_super_transfer *sxfer;
1113 struct libusb_transfer *uxfer;
1119 status = libusb20_tr_get_status(pxfer);
1120 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1121 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1122 actlen = libusb20_tr_get_actual_length(pxfer);
1125 return; /* cancelled - nothing to do */
1127 uxfer = (struct libusb_transfer *)(
1128 ((uint8_t *)sxfer) + sizeof(*sxfer));
1130 flags = uxfer->flags;
1133 case LIBUSB20_TRANSFER_COMPLETED:
1135 uxfer->actual_length += actlen;
1137 /* subtract length of SETUP packet, if any */
1138 actlen -= libusb20_tr_get_length(pxfer, 0);
1140 /* check for short packet */
1141 if (sxfer->last_len != actlen) {
1142 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1143 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1145 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1149 /* check for end of data */
1150 if (sxfer->rem_len == 0) {
1151 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1156 case LIBUSB20_TRANSFER_START:
1157 if (max_bulk > sxfer->rem_len) {
1158 max_bulk = sxfer->rem_len;
1160 /* setup new CONTROL transaction */
1161 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1162 /* next fragment - don't send SETUP packet */
1163 libusb20_tr_set_length(pxfer, 0, 0);
1165 /* first fragment - send SETUP packet */
1166 libusb20_tr_set_length(pxfer, 8, 0);
1167 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1170 if (max_bulk != 0) {
1171 libusb20_tr_set_length(pxfer, max_bulk, 1);
1172 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1173 libusb20_tr_set_total_frames(pxfer, 2);
1175 libusb20_tr_set_total_frames(pxfer, 1);
1178 /* update counters */
1179 sxfer->last_len = max_bulk;
1180 sxfer->curr_data += max_bulk;
1181 sxfer->rem_len -= max_bulk;
1183 libusb20_tr_submit(pxfer);
1185 /* check if we can fork another USB transfer */
1186 if (sxfer->rem_len == 0)
1187 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1191 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1196 /* The following function must be called locked */
1199 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1201 struct libusb20_transfer *pxfer0;
1202 struct libusb20_transfer *pxfer1;
1203 struct libusb_super_transfer *sxfer;
1204 struct libusb_transfer *uxfer;
1205 struct libusb_device *dev;
1212 dev = libusb_get_device(pdev);
1214 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1215 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1217 if (pxfer0 == NULL || pxfer1 == NULL)
1218 return; /* shouldn't happen */
1221 if (libusb20_tr_pending(pxfer0))
1223 if (libusb20_tr_pending(pxfer1))
1228 /* wait till one of the transfers complete */
1231 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1233 return; /* cancelling */
1235 return; /* cannot queue another one */
1236 /* swap transfers */
1240 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1242 return; /* cancelling */
1244 return; /* cannot queue another one */
1245 /* swap transfers */
1252 /* find next transfer on same endpoint */
1253 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1255 uxfer = (struct libusb_transfer *)(
1256 ((uint8_t *)sxfer) + sizeof(*sxfer));
1258 if (uxfer->endpoint == endpoint) {
1259 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1260 sxfer->entry.tqe_prev = NULL;
1264 return; /* success */
1268 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1269 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1271 /* reset super transfer state */
1272 sxfer->rem_len = uxfer->length;
1273 sxfer->curr_data = uxfer->buffer;
1274 uxfer->actual_length = 0;
1276 switch (uxfer->type) {
1277 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1278 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1280 case LIBUSB_TRANSFER_TYPE_BULK:
1281 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1282 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1284 case LIBUSB_TRANSFER_TYPE_CONTROL:
1285 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1286 if (sxfer->rem_len < 8)
1289 /* remove SETUP packet from data */
1290 sxfer->rem_len -= 8;
1291 sxfer->curr_data += 8;
1297 buffsize = libusb10_get_buffsize(pdev, uxfer);
1298 maxframe = libusb10_get_maxframe(pdev, uxfer);
1300 /* make sure the transfer is opened */
1301 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1302 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1305 libusb20_tr_start(pxfer0);
1309 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1311 /* make sure our event loop spins the done handler */
1313 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1316 /* The following function must be called unlocked */
1319 libusb_submit_transfer(struct libusb_transfer *uxfer)
1321 struct libusb20_transfer *pxfer0;
1322 struct libusb20_transfer *pxfer1;
1323 struct libusb_super_transfer *sxfer;
1324 struct libusb_device *dev;
1329 return (LIBUSB_ERROR_INVALID_PARAM);
1331 if (uxfer->dev_handle == NULL)
1332 return (LIBUSB_ERROR_INVALID_PARAM);
1334 endpoint = uxfer->endpoint;
1337 return (LIBUSB_ERROR_INVALID_PARAM);
1339 dev = libusb_get_device(uxfer->dev_handle);
1341 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1343 sxfer = (struct libusb_super_transfer *)(
1344 (uint8_t *)uxfer - sizeof(*sxfer));
1348 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1349 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1351 if (pxfer0 == NULL || pxfer1 == NULL) {
1352 err = LIBUSB_ERROR_OTHER;
1353 } else if ((sxfer->entry.tqe_prev != NULL) ||
1354 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1355 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1356 err = LIBUSB_ERROR_BUSY;
1359 /* set pending state */
1360 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1362 /* insert transfer into transfer head list */
1363 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1365 /* start work transfers */
1366 libusb10_submit_transfer_sub(
1367 uxfer->dev_handle, endpoint);
1369 err = 0; /* success */
1372 CTX_UNLOCK(dev->ctx);
1374 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1379 /* Asynchronous transfer cancel */
1382 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1384 struct libusb20_transfer *pxfer0;
1385 struct libusb20_transfer *pxfer1;
1386 struct libusb_super_transfer *sxfer;
1387 struct libusb_device *dev;
1392 return (LIBUSB_ERROR_INVALID_PARAM);
1394 /* check if not initialised */
1395 if (uxfer->dev_handle == NULL)
1396 return (LIBUSB_ERROR_NOT_FOUND);
1398 endpoint = uxfer->endpoint;
1401 return (LIBUSB_ERROR_INVALID_PARAM);
1403 dev = libusb_get_device(uxfer->dev_handle);
1405 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1407 sxfer = (struct libusb_super_transfer *)(
1408 (uint8_t *)uxfer - sizeof(*sxfer));
1414 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1415 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1417 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1418 /* only update the transfer status */
1419 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1420 retval = LIBUSB_ERROR_NOT_FOUND;
1421 } else if (sxfer->entry.tqe_prev != NULL) {
1422 /* we are lucky - transfer is on a queue */
1423 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1424 sxfer->entry.tqe_prev = NULL;
1425 libusb10_complete_transfer(NULL,
1426 sxfer, LIBUSB_TRANSFER_CANCELLED);
1427 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1429 retval = LIBUSB_ERROR_NOT_FOUND;
1430 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1431 libusb10_complete_transfer(pxfer0,
1432 sxfer, LIBUSB_TRANSFER_CANCELLED);
1433 libusb20_tr_stop(pxfer0);
1434 /* make sure the queue doesn't stall */
1435 libusb10_submit_transfer_sub(
1436 uxfer->dev_handle, endpoint);
1437 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1438 libusb10_complete_transfer(pxfer1,
1439 sxfer, LIBUSB_TRANSFER_CANCELLED);
1440 libusb20_tr_stop(pxfer1);
1441 /* make sure the queue doesn't stall */
1442 libusb10_submit_transfer_sub(
1443 uxfer->dev_handle, endpoint);
1446 retval = LIBUSB_ERROR_NOT_FOUND;
1449 CTX_UNLOCK(dev->ctx);
1451 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1457 libusb10_cancel_all_transfer(libusb_device *dev)
1463 libusb_cpu_to_le16(uint16_t x)
1465 return (htole16(x));
1469 libusb_le16_to_cpu(uint16_t x)
1471 return (le16toh(x));
1475 libusb_strerror(int code)
1478 case LIBUSB_SUCCESS:
1480 case LIBUSB_ERROR_IO:
1481 return ("I/O error");
1482 case LIBUSB_ERROR_INVALID_PARAM:
1483 return ("Invalid parameter");
1484 case LIBUSB_ERROR_ACCESS:
1485 return ("Permissions error");
1486 case LIBUSB_ERROR_NO_DEVICE:
1487 return ("No device");
1488 case LIBUSB_ERROR_NOT_FOUND:
1489 return ("Not found");
1490 case LIBUSB_ERROR_BUSY:
1491 return ("Device busy");
1492 case LIBUSB_ERROR_TIMEOUT:
1494 case LIBUSB_ERROR_OVERFLOW:
1495 return ("Overflow");
1496 case LIBUSB_ERROR_PIPE:
1497 return ("Pipe error");
1498 case LIBUSB_ERROR_INTERRUPTED:
1499 return ("Interrupted");
1500 case LIBUSB_ERROR_NO_MEM:
1501 return ("Out of memory");
1502 case LIBUSB_ERROR_NOT_SUPPORTED:
1503 return ("Not supported");
1504 case LIBUSB_ERROR_OTHER:
1505 return ("Other error");
1507 return ("Unknown error");
1512 libusb_error_name(int code)
1515 case LIBUSB_SUCCESS:
1516 return ("LIBUSB_SUCCESS");
1517 case LIBUSB_ERROR_IO:
1518 return ("LIBUSB_ERROR_IO");
1519 case LIBUSB_ERROR_INVALID_PARAM:
1520 return ("LIBUSB_ERROR_INVALID_PARAM");
1521 case LIBUSB_ERROR_ACCESS:
1522 return ("LIBUSB_ERROR_ACCESS");
1523 case LIBUSB_ERROR_NO_DEVICE:
1524 return ("LIBUSB_ERROR_NO_DEVICE");
1525 case LIBUSB_ERROR_NOT_FOUND:
1526 return ("LIBUSB_ERROR_NOT_FOUND");
1527 case LIBUSB_ERROR_BUSY:
1528 return ("LIBUSB_ERROR_BUSY");
1529 case LIBUSB_ERROR_TIMEOUT:
1530 return ("LIBUSB_ERROR_TIMEOUT");
1531 case LIBUSB_ERROR_OVERFLOW:
1532 return ("LIBUSB_ERROR_OVERFLOW");
1533 case LIBUSB_ERROR_PIPE:
1534 return ("LIBUSB_ERROR_PIPE");
1535 case LIBUSB_ERROR_INTERRUPTED:
1536 return ("LIBUSB_ERROR_INTERRUPTED");
1537 case LIBUSB_ERROR_NO_MEM:
1538 return ("LIBUSB_ERROR_NO_MEM");
1539 case LIBUSB_ERROR_NOT_SUPPORTED:
1540 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1541 case LIBUSB_ERROR_OTHER:
1542 return ("LIBUSB_ERROR_OTHER");
1544 return ("LIBUSB_ERROR_UNKNOWN");