1 /* $FreeBSD: src/lib/libusb/libusb10.c,v 1.25 2012/06/12 07:28:25 hselasky Exp $ */
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;
95 pthread_condattr_t attr;
99 ctx = malloc(sizeof(*ctx));
101 return (LIBUSB_ERROR_INVALID_PARAM);
103 memset(ctx, 0, sizeof(*ctx));
105 debug = getenv("LIBUSB_DEBUG");
107 ctx->debug = atoi(debug);
109 ctx->debug_fixed = 1;
111 TAILQ_INIT(&ctx->pollfds);
112 TAILQ_INIT(&ctx->tr_done);
114 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
116 return (LIBUSB_ERROR_NO_MEM);
118 if (pthread_condattr_init(&attr) != 0) {
119 pthread_mutex_destroy(&ctx->ctx_lock);
121 return (LIBUSB_ERROR_NO_MEM);
123 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
124 pthread_mutex_destroy(&ctx->ctx_lock);
125 pthread_condattr_destroy(&attr);
127 return (LIBUSB_ERROR_OTHER);
129 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
130 pthread_mutex_destroy(&ctx->ctx_lock);
131 pthread_condattr_destroy(&attr);
133 return (LIBUSB_ERROR_NO_MEM);
135 pthread_condattr_destroy(&attr);
137 ctx->ctx_handler = NO_THREAD;
139 ret = pipe(ctx->ctrl_pipe);
141 pthread_mutex_destroy(&ctx->ctx_lock);
142 pthread_cond_destroy(&ctx->ctx_cond);
144 return (LIBUSB_ERROR_OTHER);
146 /* set non-blocking mode on the control pipe to avoid deadlock */
147 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
148 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
150 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
152 pthread_mutex_lock(&default_context_lock);
153 if (usbi_default_context == NULL) {
154 usbi_default_context = ctx;
156 pthread_mutex_unlock(&default_context_lock);
161 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
167 libusb_exit(libusb_context *ctx)
169 ctx = GET_CONTEXT(ctx);
174 /* XXX cleanup devices */
176 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
177 close(ctx->ctrl_pipe[0]);
178 close(ctx->ctrl_pipe[1]);
179 pthread_mutex_destroy(&ctx->ctx_lock);
180 pthread_cond_destroy(&ctx->ctx_cond);
182 pthread_mutex_lock(&default_context_lock);
183 if (ctx == usbi_default_context) {
184 usbi_default_context = NULL;
186 pthread_mutex_unlock(&default_context_lock);
191 /* Device handling and initialisation. */
194 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
196 struct libusb20_backend *usb_backend;
197 struct libusb20_device *pdev;
198 struct libusb_device *dev;
201 ctx = GET_CONTEXT(ctx);
204 return (LIBUSB_ERROR_INVALID_PARAM);
207 return (LIBUSB_ERROR_INVALID_PARAM);
209 usb_backend = libusb20_be_alloc_default();
210 if (usb_backend == NULL)
211 return (LIBUSB_ERROR_NO_MEM);
213 /* figure out how many USB devices are present */
216 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
219 /* allocate device pointer list */
220 *list = malloc((i + 1) * sizeof(void *));
222 libusb20_be_free(usb_backend);
223 return (LIBUSB_ERROR_NO_MEM);
225 /* create libusb v1.0 compliant devices */
227 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
229 dev = malloc(sizeof(*dev));
232 libusb_unref_device((*list)[i - 1]);
237 libusb20_be_free(usb_backend);
238 return (LIBUSB_ERROR_NO_MEM);
240 /* get device into libUSB v1.0 list */
241 libusb20_be_dequeue_device(usb_backend, pdev);
243 memset(dev, 0, sizeof(*dev));
245 /* init transfer queues */
246 TAILQ_INIT(&dev->tr_head);
248 /* set context we belong to */
251 /* link together the two structures */
253 pdev->privLuData = dev;
255 (*list)[i] = libusb_ref_device(dev);
260 libusb20_be_free(usb_backend);
265 libusb_free_device_list(libusb_device **list, int unref_devices)
270 return; /* be NULL safe */
273 for (i = 0; list[i] != NULL; i++)
274 libusb_unref_device(list[i]);
280 libusb_get_bus_number(libusb_device *dev)
283 return (0); /* should not happen */
284 return (libusb20_dev_get_bus_number(dev->os_priv));
288 libusb_get_device_address(libusb_device *dev)
291 return (0); /* should not happen */
292 return (libusb20_dev_get_address(dev->os_priv));
296 libusb_get_device_speed(libusb_device *dev)
299 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
301 switch (libusb20_dev_get_speed(dev->os_priv)) {
302 case LIBUSB20_SPEED_LOW:
303 return (LIBUSB_SPEED_LOW);
304 case LIBUSB20_SPEED_FULL:
305 return (LIBUSB_SPEED_FULL);
306 case LIBUSB20_SPEED_HIGH:
307 return (LIBUSB_SPEED_HIGH);
308 case LIBUSB20_SPEED_SUPER:
309 return (LIBUSB_SPEED_SUPER);
313 return (LIBUSB_SPEED_UNKNOWN);
317 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
319 struct libusb_config_descriptor *pdconf;
320 struct libusb_interface *pinf;
321 struct libusb_interface_descriptor *pdinf;
322 struct libusb_endpoint_descriptor *pdend;
329 return (LIBUSB_ERROR_NO_DEVICE);
331 ret = libusb_get_active_config_descriptor(dev, &pdconf);
335 ret = LIBUSB_ERROR_NOT_FOUND;
336 for (i = 0; i < pdconf->bNumInterfaces; i++) {
337 pinf = &pdconf->interface[i];
338 for (j = 0; j < pinf->num_altsetting; j++) {
339 pdinf = &pinf->altsetting[j];
340 for (k = 0; k < pdinf->bNumEndpoints; k++) {
341 pdend = &pdinf->endpoint[k];
342 if (pdend->bEndpointAddress == endpoint) {
343 ret = pdend->wMaxPacketSize;
351 libusb_free_config_descriptor(pdconf);
356 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
361 ret = libusb_get_max_packet_size(dev, endpoint);
363 switch (libusb20_dev_get_speed(dev->os_priv)) {
364 case LIBUSB20_SPEED_LOW:
365 case LIBUSB20_SPEED_FULL:
369 multiplier = (1 + ((ret >> 11) & 3));
372 ret = (ret & 0x7FF) * multiplier;
380 libusb_ref_device(libusb_device *dev)
383 return (NULL); /* be NULL safe */
387 CTX_UNLOCK(dev->ctx);
393 libusb_unref_device(libusb_device *dev)
396 return; /* be NULL safe */
400 CTX_UNLOCK(dev->ctx);
402 if (dev->refcnt == 0) {
403 libusb20_dev_free(dev->os_priv);
409 libusb_open(libusb_device *dev, libusb_device_handle **devh)
411 libusb_context *ctx = dev->ctx;
412 struct libusb20_device *pdev = dev->os_priv;
417 return (LIBUSB_ERROR_INVALID_PARAM);
419 /* set default device handle value */
422 dev = libusb_ref_device(dev);
424 return (LIBUSB_ERROR_INVALID_PARAM);
426 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
428 libusb_unref_device(dev);
429 return (LIBUSB_ERROR_NO_MEM);
431 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
432 POLLOUT | POLLRDNORM | POLLWRNORM);
434 /* make sure our event loop detects the new device */
436 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
437 if (err < (int)sizeof(dummy)) {
438 /* ignore error, if any */
439 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
446 libusb_device_handle *
447 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
450 struct libusb_device **devs;
451 struct libusb20_device *pdev;
452 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
456 ctx = GET_CONTEXT(ctx);
458 return (NULL); /* be NULL safe */
460 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
462 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
466 for (j = 0; j < i; j++) {
467 struct libusb20_device *tdev;
469 tdev = devs[j]->os_priv;
470 pdesc = libusb20_dev_get_device_desc(tdev);
472 * NOTE: The USB library will automatically swap the
473 * fields in the device descriptor to be of host
476 if (pdesc->idVendor == vendor_id &&
477 pdesc->idProduct == product_id) {
478 libusb_open(devs[j], &pdev);
483 libusb_free_device_list(devs, 1);
484 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
489 libusb_close(struct libusb20_device *pdev)
492 struct libusb_device *dev;
497 return; /* be NULL safe */
499 dev = libusb_get_device(pdev);
502 libusb10_remove_pollfd(ctx, &dev->dev_poll);
504 libusb20_dev_close(pdev);
506 /* unref will free the "pdev" when the refcount reaches zero */
507 libusb_unref_device(dev);
509 /* make sure our event loop detects the closed device */
511 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
512 if (err < (int)sizeof(dummy)) {
513 /* ignore error, if any */
514 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
519 libusb_get_device(struct libusb20_device *pdev)
523 return ((libusb_device *)pdev->privLuData);
527 libusb_get_configuration(struct libusb20_device *pdev, int *config)
529 struct libusb20_config *pconf;
531 if (pdev == NULL || config == NULL)
532 return (LIBUSB_ERROR_INVALID_PARAM);
534 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
536 return (LIBUSB_ERROR_NO_MEM);
538 *config = pconf->desc.bConfigurationValue;
546 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
548 struct libusb20_config *pconf;
549 struct libusb_device *dev;
553 dev = libusb_get_device(pdev);
555 return (LIBUSB_ERROR_INVALID_PARAM);
557 if (configuration < 1) {
561 for (i = 0; i != 255; i++) {
564 pconf = libusb20_dev_alloc_config(pdev, i);
566 return (LIBUSB_ERROR_INVALID_PARAM);
567 found = (pconf->desc.bConfigurationValue
574 return (LIBUSB_ERROR_INVALID_PARAM);
579 libusb10_cancel_all_transfer(dev);
581 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
583 err = libusb20_dev_set_config_index(pdev, i);
585 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
586 POLLOUT | POLLRDNORM | POLLWRNORM);
588 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
592 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
597 dev = libusb_get_device(pdev);
599 return (LIBUSB_ERROR_INVALID_PARAM);
601 if (interface_number < 0 || interface_number > 31)
602 return (LIBUSB_ERROR_INVALID_PARAM);
605 if (dev->claimed_interfaces & (1 << interface_number))
606 err = LIBUSB_ERROR_BUSY;
609 dev->claimed_interfaces |= (1 << interface_number);
610 CTX_UNLOCK(dev->ctx);
615 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
620 dev = libusb_get_device(pdev);
622 return (LIBUSB_ERROR_INVALID_PARAM);
624 if (interface_number < 0 || interface_number > 31)
625 return (LIBUSB_ERROR_INVALID_PARAM);
628 if (!(dev->claimed_interfaces & (1 << interface_number)))
629 err = LIBUSB_ERROR_NOT_FOUND;
632 dev->claimed_interfaces &= ~(1 << interface_number);
633 CTX_UNLOCK(dev->ctx);
638 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
639 int interface_number, int alternate_setting)
644 dev = libusb_get_device(pdev);
646 return (LIBUSB_ERROR_INVALID_PARAM);
648 if (interface_number < 0 || interface_number > 31)
649 return (LIBUSB_ERROR_INVALID_PARAM);
652 if (!(dev->claimed_interfaces & (1 << interface_number)))
653 err = LIBUSB_ERROR_NOT_FOUND;
654 CTX_UNLOCK(dev->ctx);
659 libusb10_cancel_all_transfer(dev);
661 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
663 err = libusb20_dev_set_alt_index(pdev,
664 interface_number, alternate_setting);
666 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
667 pdev, libusb20_dev_get_fd(pdev),
668 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
670 return (err ? LIBUSB_ERROR_OTHER : 0);
673 static struct libusb20_transfer *
674 libusb10_get_transfer(struct libusb20_device *pdev,
675 uint8_t endpoint, uint8_t xfer_index)
677 xfer_index &= 1; /* double buffering */
679 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
681 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
682 /* this is an IN endpoint */
685 return (libusb20_tr_get_pointer(pdev, xfer_index));
689 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
691 struct libusb20_transfer *xfer;
692 struct libusb_device *dev;
695 xfer = libusb10_get_transfer(pdev, endpoint, 0);
697 return (LIBUSB_ERROR_INVALID_PARAM);
699 dev = libusb_get_device(pdev);
701 return (LIBUSB_ERROR_INVALID_PARAM);
704 err = libusb20_tr_open(xfer, 0, 1, endpoint);
705 CTX_UNLOCK(dev->ctx);
707 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
708 return (LIBUSB_ERROR_OTHER);
710 libusb20_tr_clear_stall_sync(xfer);
712 /* check if we opened the transfer */
715 libusb20_tr_close(xfer);
716 CTX_UNLOCK(dev->ctx);
718 return (0); /* success */
722 libusb_reset_device(struct libusb20_device *pdev)
727 dev = libusb_get_device(pdev);
729 return (LIBUSB_ERROR_INVALID_PARAM);
731 libusb10_cancel_all_transfer(dev);
733 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
735 err = libusb20_dev_reset(pdev);
737 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
738 pdev, libusb20_dev_get_fd(pdev),
739 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
741 return (err ? LIBUSB_ERROR_OTHER : 0);
745 libusb_check_connected(struct libusb20_device *pdev)
750 dev = libusb_get_device(pdev);
752 return (LIBUSB_ERROR_INVALID_PARAM);
754 err = libusb20_dev_check_connected(pdev);
756 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
760 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
763 return (LIBUSB_ERROR_INVALID_PARAM);
765 if (libusb20_dev_kernel_driver_active(pdev, interface))
766 return (0); /* no kernel driver is active */
768 return (1); /* kernel driver is active */
772 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
773 char *name, int namelen)
775 return (libusb_get_driver(pdev, interface, name, namelen));
779 libusb_get_driver(struct libusb20_device *pdev, int interface,
780 char *name, int namelen)
786 return (LIBUSB_ERROR_INVALID_PARAM);
788 return (LIBUSB_ERROR_INVALID_PARAM);
792 err = libusb20_dev_get_iface_desc(
793 pdev, interface, name, namelen);
796 return (LIBUSB_ERROR_OTHER);
798 /* we only want the driver name */
799 ptr = strstr(name, ":");
807 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
809 return (libusb_detach_kernel_driver(pdev, interface));
813 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
818 return (LIBUSB_ERROR_INVALID_PARAM);
820 err = libusb20_dev_detach_kernel_driver(
823 return (err ? LIBUSB_ERROR_OTHER : 0);
827 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
830 return (LIBUSB_ERROR_INVALID_PARAM);
831 /* stub - currently not supported by libusb20 */
835 /* Asynchronous device I/O */
837 struct libusb_transfer *
838 libusb_alloc_transfer(int iso_packets)
840 struct libusb_transfer *uxfer;
841 struct libusb_super_transfer *sxfer;
844 len = sizeof(struct libusb_transfer) +
845 sizeof(struct libusb_super_transfer) +
846 (iso_packets * sizeof(libusb_iso_packet_descriptor));
852 memset(sxfer, 0, len);
854 uxfer = (struct libusb_transfer *)(
855 ((uint8_t *)sxfer) + sizeof(*sxfer));
857 /* set default value */
858 uxfer->num_iso_packets = iso_packets;
864 libusb_free_transfer(struct libusb_transfer *uxfer)
866 struct libusb_super_transfer *sxfer;
869 return; /* be NULL safe */
871 /* check if we should free the transfer buffer */
872 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
875 sxfer = (struct libusb_super_transfer *)(
876 (uint8_t *)uxfer - sizeof(*sxfer));
882 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
886 switch (xfer->type) {
887 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
888 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
890 case LIBUSB_TRANSFER_TYPE_CONTROL:
901 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
906 usb_speed = libusb20_dev_get_speed(pdev);
908 switch (xfer->type) {
909 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
910 ret = 0; /* kernel will auto-select */
912 case LIBUSB_TRANSFER_TYPE_CONTROL:
917 case LIBUSB20_SPEED_LOW:
920 case LIBUSB20_SPEED_FULL:
933 libusb10_convert_error(uint8_t status)
938 case LIBUSB20_TRANSFER_START:
939 case LIBUSB20_TRANSFER_COMPLETED:
940 return (LIBUSB_TRANSFER_COMPLETED);
941 case LIBUSB20_TRANSFER_OVERFLOW:
942 return (LIBUSB_TRANSFER_OVERFLOW);
943 case LIBUSB20_TRANSFER_NO_DEVICE:
944 return (LIBUSB_TRANSFER_NO_DEVICE);
945 case LIBUSB20_TRANSFER_STALL:
946 return (LIBUSB_TRANSFER_STALL);
947 case LIBUSB20_TRANSFER_CANCELLED:
948 return (LIBUSB_TRANSFER_CANCELLED);
949 case LIBUSB20_TRANSFER_TIMED_OUT:
950 return (LIBUSB_TRANSFER_TIMED_OUT);
952 return (LIBUSB_TRANSFER_ERROR);
956 /* This function must be called locked */
959 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
960 struct libusb_super_transfer *sxfer, int status)
962 struct libusb_transfer *uxfer;
963 struct libusb_device *dev;
965 uxfer = (struct libusb_transfer *)(
966 ((uint8_t *)sxfer) + sizeof(*sxfer));
969 libusb20_tr_set_priv_sc1(pxfer, NULL);
971 /* set transfer status */
972 uxfer->status = status;
974 /* update super transfer state */
975 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
977 dev = libusb_get_device(uxfer->dev_handle);
979 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
982 /* This function must be called locked */
985 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
987 struct libusb_super_transfer *sxfer;
988 struct libusb_transfer *uxfer;
990 uint16_t iso_packets;
994 status = libusb20_tr_get_status(pxfer);
995 sxfer = libusb20_tr_get_priv_sc1(pxfer);
996 actlen = libusb20_tr_get_actual_length(pxfer);
997 iso_packets = libusb20_tr_get_max_frames(pxfer);
1000 return; /* cancelled - nothing to do */
1002 uxfer = (struct libusb_transfer *)(
1003 ((uint8_t *)sxfer) + sizeof(*sxfer));
1005 if (iso_packets > uxfer->num_iso_packets)
1006 iso_packets = uxfer->num_iso_packets;
1008 if (iso_packets == 0)
1009 return; /* nothing to do */
1011 /* make sure that the number of ISOCHRONOUS packets is valid */
1012 uxfer->num_iso_packets = iso_packets;
1015 case LIBUSB20_TRANSFER_COMPLETED:
1017 /* update actual length */
1018 uxfer->actual_length = actlen;
1019 for (i = 0; i != iso_packets; i++) {
1020 uxfer->iso_packet_desc[i].actual_length =
1021 libusb20_tr_get_length(pxfer, i);
1023 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1026 case LIBUSB20_TRANSFER_START:
1028 /* setup length(s) */
1030 for (i = 0; i != iso_packets; i++) {
1031 libusb20_tr_setup_isoc(pxfer,
1032 &uxfer->buffer[actlen],
1033 uxfer->iso_packet_desc[i].length, i);
1034 actlen += uxfer->iso_packet_desc[i].length;
1040 libusb20_tr_set_total_frames(pxfer, iso_packets);
1041 libusb20_tr_submit(pxfer);
1043 /* fork another USB transfer, if any */
1044 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1048 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1053 /* This function must be called locked */
1056 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1058 struct libusb_super_transfer *sxfer;
1059 struct libusb_transfer *uxfer;
1065 status = libusb20_tr_get_status(pxfer);
1066 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1067 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1068 actlen = libusb20_tr_get_actual_length(pxfer);
1071 return; /* cancelled - nothing to do */
1073 uxfer = (struct libusb_transfer *)(
1074 ((uint8_t *)sxfer) + sizeof(*sxfer));
1076 flags = uxfer->flags;
1079 case LIBUSB20_TRANSFER_COMPLETED:
1081 uxfer->actual_length += actlen;
1083 /* check for short packet */
1084 if (sxfer->last_len != actlen) {
1085 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1086 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1088 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1092 /* check for end of data */
1093 if (sxfer->rem_len == 0) {
1094 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1099 case LIBUSB20_TRANSFER_START:
1100 if (max_bulk > sxfer->rem_len) {
1101 max_bulk = sxfer->rem_len;
1103 /* setup new BULK or INTERRUPT transaction */
1104 libusb20_tr_setup_bulk(pxfer,
1105 sxfer->curr_data, max_bulk, uxfer->timeout);
1107 /* update counters */
1108 sxfer->last_len = max_bulk;
1109 sxfer->curr_data += max_bulk;
1110 sxfer->rem_len -= max_bulk;
1112 libusb20_tr_submit(pxfer);
1114 /* check if we can fork another USB transfer */
1115 if (sxfer->rem_len == 0)
1116 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1120 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1125 /* This function must be called locked */
1128 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1130 struct libusb_super_transfer *sxfer;
1131 struct libusb_transfer *uxfer;
1137 status = libusb20_tr_get_status(pxfer);
1138 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1139 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1140 actlen = libusb20_tr_get_actual_length(pxfer);
1143 return; /* cancelled - nothing to do */
1145 uxfer = (struct libusb_transfer *)(
1146 ((uint8_t *)sxfer) + sizeof(*sxfer));
1148 flags = uxfer->flags;
1151 case LIBUSB20_TRANSFER_COMPLETED:
1153 uxfer->actual_length += actlen;
1155 /* subtract length of SETUP packet, if any */
1156 actlen -= libusb20_tr_get_length(pxfer, 0);
1158 /* check for short packet */
1159 if (sxfer->last_len != actlen) {
1160 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1161 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1163 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1167 /* check for end of data */
1168 if (sxfer->rem_len == 0) {
1169 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1174 case LIBUSB20_TRANSFER_START:
1175 if (max_bulk > sxfer->rem_len) {
1176 max_bulk = sxfer->rem_len;
1178 /* setup new CONTROL transaction */
1179 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1180 /* next fragment - don't send SETUP packet */
1181 libusb20_tr_set_length(pxfer, 0, 0);
1183 /* first fragment - send SETUP packet */
1184 libusb20_tr_set_length(pxfer, 8, 0);
1185 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1188 if (max_bulk != 0) {
1189 libusb20_tr_set_length(pxfer, max_bulk, 1);
1190 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1191 libusb20_tr_set_total_frames(pxfer, 2);
1193 libusb20_tr_set_total_frames(pxfer, 1);
1196 /* update counters */
1197 sxfer->last_len = max_bulk;
1198 sxfer->curr_data += max_bulk;
1199 sxfer->rem_len -= max_bulk;
1201 libusb20_tr_submit(pxfer);
1203 /* check if we can fork another USB transfer */
1204 if (sxfer->rem_len == 0)
1205 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1209 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1214 /* The following function must be called locked */
1217 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1219 struct libusb20_transfer *pxfer0;
1220 struct libusb20_transfer *pxfer1;
1221 struct libusb_super_transfer *sxfer;
1222 struct libusb_transfer *uxfer;
1223 struct libusb_device *dev;
1230 dev = libusb_get_device(pdev);
1232 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1233 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1235 if (pxfer0 == NULL || pxfer1 == NULL)
1236 return; /* shouldn't happen */
1239 if (libusb20_tr_pending(pxfer0))
1241 if (libusb20_tr_pending(pxfer1))
1246 /* wait till one of the transfers complete */
1249 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1251 return; /* cancelling */
1253 return; /* cannot queue another one */
1254 /* swap transfers */
1258 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1260 return; /* cancelling */
1262 return; /* cannot queue another one */
1263 /* swap transfers */
1270 /* find next transfer on same endpoint */
1271 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1273 uxfer = (struct libusb_transfer *)(
1274 ((uint8_t *)sxfer) + sizeof(*sxfer));
1276 if (uxfer->endpoint == endpoint) {
1277 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1278 sxfer->entry.tqe_prev = NULL;
1282 return; /* success */
1286 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1287 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1289 /* reset super transfer state */
1290 sxfer->rem_len = uxfer->length;
1291 sxfer->curr_data = uxfer->buffer;
1292 uxfer->actual_length = 0;
1294 switch (uxfer->type) {
1295 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1296 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1298 case LIBUSB_TRANSFER_TYPE_BULK:
1299 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1300 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1302 case LIBUSB_TRANSFER_TYPE_CONTROL:
1303 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1304 if (sxfer->rem_len < 8)
1307 /* remove SETUP packet from data */
1308 sxfer->rem_len -= 8;
1309 sxfer->curr_data += 8;
1315 buffsize = libusb10_get_buffsize(pdev, uxfer);
1316 maxframe = libusb10_get_maxframe(pdev, uxfer);
1318 /* make sure the transfer is opened */
1319 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1320 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1323 libusb20_tr_start(pxfer0);
1327 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1329 /* make sure our event loop spins the done handler */
1331 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1334 /* The following function must be called unlocked */
1337 libusb_submit_transfer(struct libusb_transfer *uxfer)
1339 struct libusb20_transfer *pxfer0;
1340 struct libusb20_transfer *pxfer1;
1341 struct libusb_super_transfer *sxfer;
1342 struct libusb_device *dev;
1347 return (LIBUSB_ERROR_INVALID_PARAM);
1349 if (uxfer->dev_handle == NULL)
1350 return (LIBUSB_ERROR_INVALID_PARAM);
1352 endpoint = uxfer->endpoint;
1354 dev = libusb_get_device(uxfer->dev_handle);
1356 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1358 sxfer = (struct libusb_super_transfer *)(
1359 (uint8_t *)uxfer - sizeof(*sxfer));
1363 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1364 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1366 if (pxfer0 == NULL || pxfer1 == NULL) {
1367 err = LIBUSB_ERROR_OTHER;
1368 } else if ((sxfer->entry.tqe_prev != NULL) ||
1369 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1370 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1371 err = LIBUSB_ERROR_BUSY;
1374 /* set pending state */
1375 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1377 /* insert transfer into transfer head list */
1378 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1380 /* start work transfers */
1381 libusb10_submit_transfer_sub(
1382 uxfer->dev_handle, endpoint);
1384 err = 0; /* success */
1387 CTX_UNLOCK(dev->ctx);
1389 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1394 /* Asynchronous transfer cancel */
1397 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1399 struct libusb20_transfer *pxfer0;
1400 struct libusb20_transfer *pxfer1;
1401 struct libusb_super_transfer *sxfer;
1402 struct libusb_device *dev;
1407 return (LIBUSB_ERROR_INVALID_PARAM);
1409 /* check if not initialised */
1410 if (uxfer->dev_handle == NULL)
1411 return (LIBUSB_ERROR_NOT_FOUND);
1413 endpoint = uxfer->endpoint;
1415 dev = libusb_get_device(uxfer->dev_handle);
1417 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1419 sxfer = (struct libusb_super_transfer *)(
1420 (uint8_t *)uxfer - sizeof(*sxfer));
1426 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1427 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1429 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1430 /* only update the transfer status */
1431 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1432 retval = LIBUSB_ERROR_NOT_FOUND;
1433 } else if (sxfer->entry.tqe_prev != NULL) {
1434 /* we are lucky - transfer is on a queue */
1435 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1436 sxfer->entry.tqe_prev = NULL;
1437 libusb10_complete_transfer(NULL,
1438 sxfer, LIBUSB_TRANSFER_CANCELLED);
1439 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1441 retval = LIBUSB_ERROR_NOT_FOUND;
1442 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1443 libusb10_complete_transfer(pxfer0,
1444 sxfer, LIBUSB_TRANSFER_CANCELLED);
1445 libusb20_tr_stop(pxfer0);
1446 /* make sure the queue doesn't stall */
1447 libusb10_submit_transfer_sub(
1448 uxfer->dev_handle, endpoint);
1449 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1450 libusb10_complete_transfer(pxfer1,
1451 sxfer, LIBUSB_TRANSFER_CANCELLED);
1452 libusb20_tr_stop(pxfer1);
1453 /* make sure the queue doesn't stall */
1454 libusb10_submit_transfer_sub(
1455 uxfer->dev_handle, endpoint);
1458 retval = LIBUSB_ERROR_NOT_FOUND;
1461 CTX_UNLOCK(dev->ctx);
1463 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1469 libusb10_cancel_all_transfer(libusb_device *dev)
1475 libusb_cpu_to_le16(uint16_t x)
1477 return (htole16(x));
1481 libusb_le16_to_cpu(uint16_t x)
1483 return (le16toh(x));
1487 libusb_strerror(int code)
1490 case LIBUSB_SUCCESS:
1492 case LIBUSB_ERROR_IO:
1493 return ("I/O error");
1494 case LIBUSB_ERROR_INVALID_PARAM:
1495 return ("Invalid parameter");
1496 case LIBUSB_ERROR_ACCESS:
1497 return ("Permissions error");
1498 case LIBUSB_ERROR_NO_DEVICE:
1499 return ("No device");
1500 case LIBUSB_ERROR_NOT_FOUND:
1501 return ("Not found");
1502 case LIBUSB_ERROR_BUSY:
1503 return ("Device busy");
1504 case LIBUSB_ERROR_TIMEOUT:
1506 case LIBUSB_ERROR_OVERFLOW:
1507 return ("Overflow");
1508 case LIBUSB_ERROR_PIPE:
1509 return ("Pipe error");
1510 case LIBUSB_ERROR_INTERRUPTED:
1511 return ("Interrupted");
1512 case LIBUSB_ERROR_NO_MEM:
1513 return ("Out of memory");
1514 case LIBUSB_ERROR_NOT_SUPPORTED:
1515 return ("Not supported");
1516 case LIBUSB_ERROR_OTHER:
1517 return ("Other error");
1519 return ("Unknown error");
1524 libusb_error_name(int code)
1527 case LIBUSB_SUCCESS:
1528 return ("LIBUSB_SUCCESS");
1529 case LIBUSB_ERROR_IO:
1530 return ("LIBUSB_ERROR_IO");
1531 case LIBUSB_ERROR_INVALID_PARAM:
1532 return ("LIBUSB_ERROR_INVALID_PARAM");
1533 case LIBUSB_ERROR_ACCESS:
1534 return ("LIBUSB_ERROR_ACCESS");
1535 case LIBUSB_ERROR_NO_DEVICE:
1536 return ("LIBUSB_ERROR_NO_DEVICE");
1537 case LIBUSB_ERROR_NOT_FOUND:
1538 return ("LIBUSB_ERROR_NOT_FOUND");
1539 case LIBUSB_ERROR_BUSY:
1540 return ("LIBUSB_ERROR_BUSY");
1541 case LIBUSB_ERROR_TIMEOUT:
1542 return ("LIBUSB_ERROR_TIMEOUT");
1543 case LIBUSB_ERROR_OVERFLOW:
1544 return ("LIBUSB_ERROR_OVERFLOW");
1545 case LIBUSB_ERROR_PIPE:
1546 return ("LIBUSB_ERROR_PIPE");
1547 case LIBUSB_ERROR_INTERRUPTED:
1548 return ("LIBUSB_ERROR_INTERRUPTED");
1549 case LIBUSB_ERROR_NO_MEM:
1550 return ("LIBUSB_ERROR_NO_MEM");
1551 case LIBUSB_ERROR_NOT_SUPPORTED:
1552 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1553 case LIBUSB_ERROR_OTHER:
1554 return ("LIBUSB_ERROR_OTHER");
1556 return ("LIBUSB_ERROR_UNKNOWN");