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)
596 dev = libusb_get_device(pdev);
598 return (LIBUSB_ERROR_INVALID_PARAM);
600 if (interface_number < 0 || interface_number > 31)
601 return (LIBUSB_ERROR_INVALID_PARAM);
604 dev->claimed_interfaces |= (1 << interface_number);
605 CTX_UNLOCK(dev->ctx);
611 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
616 dev = libusb_get_device(pdev);
618 return (LIBUSB_ERROR_INVALID_PARAM);
620 if (interface_number < 0 || interface_number > 31)
621 return (LIBUSB_ERROR_INVALID_PARAM);
624 if (!(dev->claimed_interfaces & (1 << interface_number)))
625 err = LIBUSB_ERROR_NOT_FOUND;
628 dev->claimed_interfaces &= ~(1 << interface_number);
629 CTX_UNLOCK(dev->ctx);
634 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
635 int interface_number, int alternate_setting)
640 dev = libusb_get_device(pdev);
642 return (LIBUSB_ERROR_INVALID_PARAM);
644 if (interface_number < 0 || interface_number > 31)
645 return (LIBUSB_ERROR_INVALID_PARAM);
648 if (!(dev->claimed_interfaces & (1 << interface_number)))
649 err = LIBUSB_ERROR_NOT_FOUND;
650 CTX_UNLOCK(dev->ctx);
655 libusb10_cancel_all_transfer(dev);
657 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
659 err = libusb20_dev_set_alt_index(pdev,
660 interface_number, alternate_setting);
662 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
663 pdev, libusb20_dev_get_fd(pdev),
664 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
666 return (err ? LIBUSB_ERROR_OTHER : 0);
669 static struct libusb20_transfer *
670 libusb10_get_transfer(struct libusb20_device *pdev,
671 uint8_t endpoint, uint8_t xfer_index)
673 xfer_index &= 1; /* double buffering */
675 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
677 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
678 /* this is an IN endpoint */
681 return (libusb20_tr_get_pointer(pdev, xfer_index));
685 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
687 struct libusb20_transfer *xfer;
688 struct libusb_device *dev;
691 xfer = libusb10_get_transfer(pdev, endpoint, 0);
693 return (LIBUSB_ERROR_INVALID_PARAM);
695 dev = libusb_get_device(pdev);
697 return (LIBUSB_ERROR_INVALID_PARAM);
700 err = libusb20_tr_open(xfer, 0, 1, endpoint);
701 CTX_UNLOCK(dev->ctx);
703 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
704 return (LIBUSB_ERROR_OTHER);
706 libusb20_tr_clear_stall_sync(xfer);
708 /* check if we opened the transfer */
711 libusb20_tr_close(xfer);
712 CTX_UNLOCK(dev->ctx);
714 return (0); /* success */
718 libusb_reset_device(struct libusb20_device *pdev)
723 dev = libusb_get_device(pdev);
725 return (LIBUSB_ERROR_INVALID_PARAM);
727 libusb10_cancel_all_transfer(dev);
729 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
731 err = libusb20_dev_reset(pdev);
733 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
734 pdev, libusb20_dev_get_fd(pdev),
735 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
737 return (err ? LIBUSB_ERROR_OTHER : 0);
741 libusb_check_connected(struct libusb20_device *pdev)
746 dev = libusb_get_device(pdev);
748 return (LIBUSB_ERROR_INVALID_PARAM);
750 err = libusb20_dev_check_connected(pdev);
752 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
756 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
759 return (LIBUSB_ERROR_INVALID_PARAM);
761 if (libusb20_dev_kernel_driver_active(pdev, interface))
762 return (0); /* no kernel driver is active */
764 return (1); /* kernel driver is active */
768 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
769 char *name, int namelen)
771 return (libusb_get_driver(pdev, interface, name, namelen));
775 libusb_get_driver(struct libusb20_device *pdev, int interface,
776 char *name, int namelen)
782 return (LIBUSB_ERROR_INVALID_PARAM);
784 return (LIBUSB_ERROR_INVALID_PARAM);
788 err = libusb20_dev_get_iface_desc(
789 pdev, interface, name, namelen);
792 return (LIBUSB_ERROR_OTHER);
794 /* we only want the driver name */
795 ptr = strstr(name, ":");
803 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
805 return (libusb_detach_kernel_driver(pdev, interface));
809 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
814 return (LIBUSB_ERROR_INVALID_PARAM);
816 err = libusb20_dev_detach_kernel_driver(
819 return (err ? LIBUSB_ERROR_OTHER : 0);
823 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
826 return (LIBUSB_ERROR_INVALID_PARAM);
827 /* stub - currently not supported by libusb20 */
831 /* Asynchronous device I/O */
833 struct libusb_transfer *
834 libusb_alloc_transfer(int iso_packets)
836 struct libusb_transfer *uxfer;
837 struct libusb_super_transfer *sxfer;
840 len = sizeof(struct libusb_transfer) +
841 sizeof(struct libusb_super_transfer) +
842 (iso_packets * sizeof(libusb_iso_packet_descriptor));
848 memset(sxfer, 0, len);
850 uxfer = (struct libusb_transfer *)(
851 ((uint8_t *)sxfer) + sizeof(*sxfer));
853 /* set default value */
854 uxfer->num_iso_packets = iso_packets;
860 libusb_free_transfer(struct libusb_transfer *uxfer)
862 struct libusb_super_transfer *sxfer;
865 return; /* be NULL safe */
867 /* check if we should free the transfer buffer */
868 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
871 sxfer = (struct libusb_super_transfer *)(
872 (uint8_t *)uxfer - sizeof(*sxfer));
878 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
882 switch (xfer->type) {
883 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
884 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
886 case LIBUSB_TRANSFER_TYPE_CONTROL:
897 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
902 usb_speed = libusb20_dev_get_speed(pdev);
904 switch (xfer->type) {
905 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
906 ret = 0; /* kernel will auto-select */
908 case LIBUSB_TRANSFER_TYPE_CONTROL:
913 case LIBUSB20_SPEED_LOW:
916 case LIBUSB20_SPEED_FULL:
929 libusb10_convert_error(uint8_t status)
934 case LIBUSB20_TRANSFER_START:
935 case LIBUSB20_TRANSFER_COMPLETED:
936 return (LIBUSB_TRANSFER_COMPLETED);
937 case LIBUSB20_TRANSFER_OVERFLOW:
938 return (LIBUSB_TRANSFER_OVERFLOW);
939 case LIBUSB20_TRANSFER_NO_DEVICE:
940 return (LIBUSB_TRANSFER_NO_DEVICE);
941 case LIBUSB20_TRANSFER_STALL:
942 return (LIBUSB_TRANSFER_STALL);
943 case LIBUSB20_TRANSFER_CANCELLED:
944 return (LIBUSB_TRANSFER_CANCELLED);
945 case LIBUSB20_TRANSFER_TIMED_OUT:
946 return (LIBUSB_TRANSFER_TIMED_OUT);
948 return (LIBUSB_TRANSFER_ERROR);
952 /* This function must be called locked */
955 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
956 struct libusb_super_transfer *sxfer, int status)
958 struct libusb_transfer *uxfer;
959 struct libusb_device *dev;
961 uxfer = (struct libusb_transfer *)(
962 ((uint8_t *)sxfer) + sizeof(*sxfer));
965 libusb20_tr_set_priv_sc1(pxfer, NULL);
967 /* set transfer status */
968 uxfer->status = status;
970 /* update super transfer state */
971 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
973 dev = libusb_get_device(uxfer->dev_handle);
975 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
978 /* This function must be called locked */
981 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
983 struct libusb_super_transfer *sxfer;
984 struct libusb_transfer *uxfer;
986 uint16_t iso_packets;
991 status = libusb20_tr_get_status(pxfer);
992 sxfer = libusb20_tr_get_priv_sc1(pxfer);
993 actlen = libusb20_tr_get_actual_length(pxfer);
994 iso_packets = libusb20_tr_get_max_frames(pxfer);
997 return; /* cancelled - nothing to do */
999 uxfer = (struct libusb_transfer *)(
1000 ((uint8_t *)sxfer) + sizeof(*sxfer));
1002 if (iso_packets > uxfer->num_iso_packets)
1003 iso_packets = uxfer->num_iso_packets;
1005 if (iso_packets == 0)
1006 return; /* nothing to do */
1008 /* make sure that the number of ISOCHRONOUS packets is valid */
1009 uxfer->num_iso_packets = iso_packets;
1011 flags = uxfer->flags;
1014 case LIBUSB20_TRANSFER_COMPLETED:
1016 /* update actual length */
1017 uxfer->actual_length = actlen;
1018 for (i = 0; i != iso_packets; i++) {
1019 uxfer->iso_packet_desc[i].actual_length =
1020 libusb20_tr_get_length(pxfer, i);
1022 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1025 case LIBUSB20_TRANSFER_START:
1027 /* setup length(s) */
1029 for (i = 0; i != iso_packets; i++) {
1030 libusb20_tr_setup_isoc(pxfer,
1031 &uxfer->buffer[actlen],
1032 uxfer->iso_packet_desc[i].length, i);
1033 actlen += uxfer->iso_packet_desc[i].length;
1039 libusb20_tr_set_total_frames(pxfer, iso_packets);
1040 libusb20_tr_submit(pxfer);
1042 /* fork another USB transfer, if any */
1043 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1047 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1052 /* This function must be called locked */
1055 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1057 struct libusb_super_transfer *sxfer;
1058 struct libusb_transfer *uxfer;
1064 status = libusb20_tr_get_status(pxfer);
1065 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1066 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1067 actlen = libusb20_tr_get_actual_length(pxfer);
1070 return; /* cancelled - nothing to do */
1072 uxfer = (struct libusb_transfer *)(
1073 ((uint8_t *)sxfer) + sizeof(*sxfer));
1075 flags = uxfer->flags;
1078 case LIBUSB20_TRANSFER_COMPLETED:
1080 uxfer->actual_length += actlen;
1082 /* check for short packet */
1083 if (sxfer->last_len != actlen) {
1084 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1085 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1087 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1091 /* check for end of data */
1092 if (sxfer->rem_len == 0) {
1093 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1098 case LIBUSB20_TRANSFER_START:
1099 if (max_bulk > sxfer->rem_len) {
1100 max_bulk = sxfer->rem_len;
1102 /* setup new BULK or INTERRUPT transaction */
1103 libusb20_tr_setup_bulk(pxfer,
1104 sxfer->curr_data, max_bulk, uxfer->timeout);
1106 /* update counters */
1107 sxfer->last_len = max_bulk;
1108 sxfer->curr_data += max_bulk;
1109 sxfer->rem_len -= max_bulk;
1111 libusb20_tr_submit(pxfer);
1113 /* check if we can fork another USB transfer */
1114 if (sxfer->rem_len == 0)
1115 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1119 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1124 /* This function must be called locked */
1127 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1129 struct libusb_super_transfer *sxfer;
1130 struct libusb_transfer *uxfer;
1136 status = libusb20_tr_get_status(pxfer);
1137 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1138 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1139 actlen = libusb20_tr_get_actual_length(pxfer);
1142 return; /* cancelled - nothing to do */
1144 uxfer = (struct libusb_transfer *)(
1145 ((uint8_t *)sxfer) + sizeof(*sxfer));
1147 flags = uxfer->flags;
1150 case LIBUSB20_TRANSFER_COMPLETED:
1152 uxfer->actual_length += actlen;
1154 /* subtract length of SETUP packet, if any */
1155 actlen -= libusb20_tr_get_length(pxfer, 0);
1157 /* check for short packet */
1158 if (sxfer->last_len != actlen) {
1159 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1160 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1162 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1166 /* check for end of data */
1167 if (sxfer->rem_len == 0) {
1168 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1173 case LIBUSB20_TRANSFER_START:
1174 if (max_bulk > sxfer->rem_len) {
1175 max_bulk = sxfer->rem_len;
1177 /* setup new CONTROL transaction */
1178 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1179 /* next fragment - don't send SETUP packet */
1180 libusb20_tr_set_length(pxfer, 0, 0);
1182 /* first fragment - send SETUP packet */
1183 libusb20_tr_set_length(pxfer, 8, 0);
1184 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1187 if (max_bulk != 0) {
1188 libusb20_tr_set_length(pxfer, max_bulk, 1);
1189 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1190 libusb20_tr_set_total_frames(pxfer, 2);
1192 libusb20_tr_set_total_frames(pxfer, 1);
1195 /* update counters */
1196 sxfer->last_len = max_bulk;
1197 sxfer->curr_data += max_bulk;
1198 sxfer->rem_len -= max_bulk;
1200 libusb20_tr_submit(pxfer);
1202 /* check if we can fork another USB transfer */
1203 if (sxfer->rem_len == 0)
1204 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1208 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1213 /* The following function must be called locked */
1216 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1218 struct libusb20_transfer *pxfer0;
1219 struct libusb20_transfer *pxfer1;
1220 struct libusb_super_transfer *sxfer;
1221 struct libusb_transfer *uxfer;
1222 struct libusb_device *dev;
1229 dev = libusb_get_device(pdev);
1231 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1232 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1234 if (pxfer0 == NULL || pxfer1 == NULL)
1235 return; /* shouldn't happen */
1238 if (libusb20_tr_pending(pxfer0))
1240 if (libusb20_tr_pending(pxfer1))
1245 /* wait till one of the transfers complete */
1248 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1250 return; /* cancelling */
1252 return; /* cannot queue another one */
1253 /* swap transfers */
1257 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1259 return; /* cancelling */
1261 return; /* cannot queue another one */
1262 /* swap transfers */
1269 /* find next transfer on same endpoint */
1270 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1272 uxfer = (struct libusb_transfer *)(
1273 ((uint8_t *)sxfer) + sizeof(*sxfer));
1275 if (uxfer->endpoint == endpoint) {
1276 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1277 sxfer->entry.tqe_prev = NULL;
1281 return; /* success */
1285 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1286 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1288 /* reset super transfer state */
1289 sxfer->rem_len = uxfer->length;
1290 sxfer->curr_data = uxfer->buffer;
1291 uxfer->actual_length = 0;
1293 switch (uxfer->type) {
1294 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1295 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1297 case LIBUSB_TRANSFER_TYPE_BULK:
1298 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1299 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1301 case LIBUSB_TRANSFER_TYPE_CONTROL:
1302 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1303 if (sxfer->rem_len < 8)
1306 /* remove SETUP packet from data */
1307 sxfer->rem_len -= 8;
1308 sxfer->curr_data += 8;
1314 buffsize = libusb10_get_buffsize(pdev, uxfer);
1315 maxframe = libusb10_get_maxframe(pdev, uxfer);
1317 /* make sure the transfer is opened */
1318 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1319 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1322 libusb20_tr_start(pxfer0);
1326 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1328 /* make sure our event loop spins the done handler */
1330 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1333 /* The following function must be called unlocked */
1336 libusb_submit_transfer(struct libusb_transfer *uxfer)
1338 struct libusb20_transfer *pxfer0;
1339 struct libusb20_transfer *pxfer1;
1340 struct libusb_super_transfer *sxfer;
1341 struct libusb_device *dev;
1346 return (LIBUSB_ERROR_INVALID_PARAM);
1348 if (uxfer->dev_handle == NULL)
1349 return (LIBUSB_ERROR_INVALID_PARAM);
1351 endpoint = uxfer->endpoint;
1354 return (LIBUSB_ERROR_INVALID_PARAM);
1356 dev = libusb_get_device(uxfer->dev_handle);
1358 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1360 sxfer = (struct libusb_super_transfer *)(
1361 (uint8_t *)uxfer - sizeof(*sxfer));
1365 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1366 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1368 if (pxfer0 == NULL || pxfer1 == NULL) {
1369 err = LIBUSB_ERROR_OTHER;
1370 } else if ((sxfer->entry.tqe_prev != NULL) ||
1371 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1372 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1373 err = LIBUSB_ERROR_BUSY;
1376 /* set pending state */
1377 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1379 /* insert transfer into transfer head list */
1380 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1382 /* start work transfers */
1383 libusb10_submit_transfer_sub(
1384 uxfer->dev_handle, endpoint);
1386 err = 0; /* success */
1389 CTX_UNLOCK(dev->ctx);
1391 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1396 /* Asynchronous transfer cancel */
1399 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1401 struct libusb20_transfer *pxfer0;
1402 struct libusb20_transfer *pxfer1;
1403 struct libusb_super_transfer *sxfer;
1404 struct libusb_device *dev;
1409 return (LIBUSB_ERROR_INVALID_PARAM);
1411 /* check if not initialised */
1412 if (uxfer->dev_handle == NULL)
1413 return (LIBUSB_ERROR_NOT_FOUND);
1415 endpoint = uxfer->endpoint;
1418 return (LIBUSB_ERROR_INVALID_PARAM);
1420 dev = libusb_get_device(uxfer->dev_handle);
1422 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1424 sxfer = (struct libusb_super_transfer *)(
1425 (uint8_t *)uxfer - sizeof(*sxfer));
1431 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1432 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1434 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1435 /* only update the transfer status */
1436 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1437 retval = LIBUSB_ERROR_NOT_FOUND;
1438 } else if (sxfer->entry.tqe_prev != NULL) {
1439 /* we are lucky - transfer is on a queue */
1440 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1441 sxfer->entry.tqe_prev = NULL;
1442 libusb10_complete_transfer(NULL,
1443 sxfer, LIBUSB_TRANSFER_CANCELLED);
1444 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1446 retval = LIBUSB_ERROR_NOT_FOUND;
1447 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1448 libusb10_complete_transfer(pxfer0,
1449 sxfer, LIBUSB_TRANSFER_CANCELLED);
1450 libusb20_tr_stop(pxfer0);
1451 /* make sure the queue doesn't stall */
1452 libusb10_submit_transfer_sub(
1453 uxfer->dev_handle, endpoint);
1454 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1455 libusb10_complete_transfer(pxfer1,
1456 sxfer, LIBUSB_TRANSFER_CANCELLED);
1457 libusb20_tr_stop(pxfer1);
1458 /* make sure the queue doesn't stall */
1459 libusb10_submit_transfer_sub(
1460 uxfer->dev_handle, endpoint);
1463 retval = LIBUSB_ERROR_NOT_FOUND;
1466 CTX_UNLOCK(dev->ctx);
1468 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1474 libusb10_cancel_all_transfer(libusb_device *dev)
1480 libusb_cpu_to_le16(uint16_t x)
1482 return (htole16(x));
1486 libusb_le16_to_cpu(uint16_t x)
1488 return (le16toh(x));
1492 libusb_strerror(int code)
1495 case LIBUSB_SUCCESS:
1497 case LIBUSB_ERROR_IO:
1498 return ("I/O error");
1499 case LIBUSB_ERROR_INVALID_PARAM:
1500 return ("Invalid parameter");
1501 case LIBUSB_ERROR_ACCESS:
1502 return ("Permissions error");
1503 case LIBUSB_ERROR_NO_DEVICE:
1504 return ("No device");
1505 case LIBUSB_ERROR_NOT_FOUND:
1506 return ("Not found");
1507 case LIBUSB_ERROR_BUSY:
1508 return ("Device busy");
1509 case LIBUSB_ERROR_TIMEOUT:
1511 case LIBUSB_ERROR_OVERFLOW:
1512 return ("Overflow");
1513 case LIBUSB_ERROR_PIPE:
1514 return ("Pipe error");
1515 case LIBUSB_ERROR_INTERRUPTED:
1516 return ("Interrupted");
1517 case LIBUSB_ERROR_NO_MEM:
1518 return ("Out of memory");
1519 case LIBUSB_ERROR_NOT_SUPPORTED:
1520 return ("Not supported");
1521 case LIBUSB_ERROR_OTHER:
1522 return ("Other error");
1524 return ("Unknown error");
1529 libusb_error_name(int code)
1532 case LIBUSB_SUCCESS:
1533 return ("LIBUSB_SUCCESS");
1534 case LIBUSB_ERROR_IO:
1535 return ("LIBUSB_ERROR_IO");
1536 case LIBUSB_ERROR_INVALID_PARAM:
1537 return ("LIBUSB_ERROR_INVALID_PARAM");
1538 case LIBUSB_ERROR_ACCESS:
1539 return ("LIBUSB_ERROR_ACCESS");
1540 case LIBUSB_ERROR_NO_DEVICE:
1541 return ("LIBUSB_ERROR_NO_DEVICE");
1542 case LIBUSB_ERROR_NOT_FOUND:
1543 return ("LIBUSB_ERROR_NOT_FOUND");
1544 case LIBUSB_ERROR_BUSY:
1545 return ("LIBUSB_ERROR_BUSY");
1546 case LIBUSB_ERROR_TIMEOUT:
1547 return ("LIBUSB_ERROR_TIMEOUT");
1548 case LIBUSB_ERROR_OVERFLOW:
1549 return ("LIBUSB_ERROR_OVERFLOW");
1550 case LIBUSB_ERROR_PIPE:
1551 return ("LIBUSB_ERROR_PIPE");
1552 case LIBUSB_ERROR_INTERRUPTED:
1553 return ("LIBUSB_ERROR_INTERRUPTED");
1554 case LIBUSB_ERROR_NO_MEM:
1555 return ("LIBUSB_ERROR_NO_MEM");
1556 case LIBUSB_ERROR_NOT_SUPPORTED:
1557 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1558 case LIBUSB_ERROR_OTHER:
1559 return ("LIBUSB_ERROR_OTHER");
1561 return ("LIBUSB_ERROR_UNKNOWN");