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_ref_device(libusb_device *dev)
338 return (NULL); /* be NULL safe */
342 CTX_UNLOCK(dev->ctx);
348 libusb_unref_device(libusb_device *dev)
351 return; /* be NULL safe */
355 CTX_UNLOCK(dev->ctx);
357 if (dev->refcnt == 0) {
358 libusb20_dev_free(dev->os_priv);
364 libusb_open(libusb_device *dev, libusb_device_handle **devh)
366 libusb_context *ctx = dev->ctx;
367 struct libusb20_device *pdev = dev->os_priv;
372 return (LIBUSB_ERROR_INVALID_PARAM);
374 /* set default device handle value */
377 dev = libusb_ref_device(dev);
379 return (LIBUSB_ERROR_INVALID_PARAM);
381 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
383 libusb_unref_device(dev);
384 return (LIBUSB_ERROR_NO_MEM);
386 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
387 POLLOUT | POLLRDNORM | POLLWRNORM);
389 /* make sure our event loop detects the new device */
391 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
392 if (err < (int)sizeof(dummy)) {
393 /* ignore error, if any */
394 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
401 libusb_device_handle *
402 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
405 struct libusb_device **devs;
406 struct libusb20_device *pdev;
407 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
411 ctx = GET_CONTEXT(ctx);
413 return (NULL); /* be NULL safe */
415 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
417 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
420 for (j = 0; j < i; j++) {
421 pdev = devs[j]->os_priv;
422 pdesc = libusb20_dev_get_device_desc(pdev);
424 * NOTE: The USB library will automatically swap the
425 * fields in the device descriptor to be of host
428 if (pdesc->idVendor == vendor_id &&
429 pdesc->idProduct == product_id) {
430 if (libusb_open(devs[j], &pdev) < 0)
438 libusb_free_device_list(devs, 1);
439 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
444 libusb_close(struct libusb20_device *pdev)
447 struct libusb_device *dev;
452 return; /* be NULL safe */
454 dev = libusb_get_device(pdev);
457 libusb10_remove_pollfd(ctx, &dev->dev_poll);
459 libusb20_dev_close(pdev);
461 /* unref will free the "pdev" when the refcount reaches zero */
462 libusb_unref_device(dev);
464 /* make sure our event loop detects the closed device */
466 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
467 if (err < (int)sizeof(dummy)) {
468 /* ignore error, if any */
469 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
474 libusb_get_device(struct libusb20_device *pdev)
478 return ((libusb_device *)pdev->privLuData);
482 libusb_get_configuration(struct libusb20_device *pdev, int *config)
484 struct libusb20_config *pconf;
486 if (pdev == NULL || config == NULL)
487 return (LIBUSB_ERROR_INVALID_PARAM);
489 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
491 return (LIBUSB_ERROR_NO_MEM);
493 *config = pconf->desc.bConfigurationValue;
501 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
503 struct libusb20_config *pconf;
504 struct libusb_device *dev;
508 dev = libusb_get_device(pdev);
510 return (LIBUSB_ERROR_INVALID_PARAM);
512 if (configuration < 1) {
516 for (i = 0; i != 255; i++) {
519 pconf = libusb20_dev_alloc_config(pdev, i);
521 return (LIBUSB_ERROR_INVALID_PARAM);
522 found = (pconf->desc.bConfigurationValue
529 return (LIBUSB_ERROR_INVALID_PARAM);
534 libusb10_cancel_all_transfer(dev);
536 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
538 err = libusb20_dev_set_config_index(pdev, i);
540 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
541 POLLOUT | POLLRDNORM | POLLWRNORM);
543 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
547 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
552 dev = libusb_get_device(pdev);
554 return (LIBUSB_ERROR_INVALID_PARAM);
556 if (interface_number < 0 || interface_number > 31)
557 return (LIBUSB_ERROR_INVALID_PARAM);
560 if (dev->claimed_interfaces & (1 << interface_number))
561 err = LIBUSB_ERROR_BUSY;
564 dev->claimed_interfaces |= (1 << interface_number);
565 CTX_UNLOCK(dev->ctx);
570 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
575 dev = libusb_get_device(pdev);
577 return (LIBUSB_ERROR_INVALID_PARAM);
579 if (interface_number < 0 || interface_number > 31)
580 return (LIBUSB_ERROR_INVALID_PARAM);
583 if (!(dev->claimed_interfaces & (1 << interface_number)))
584 err = LIBUSB_ERROR_NOT_FOUND;
587 dev->claimed_interfaces &= ~(1 << interface_number);
588 CTX_UNLOCK(dev->ctx);
593 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
594 int interface_number, int alternate_setting)
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;
609 CTX_UNLOCK(dev->ctx);
614 libusb10_cancel_all_transfer(dev);
616 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
618 err = libusb20_dev_set_alt_index(pdev,
619 interface_number, alternate_setting);
621 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
622 pdev, libusb20_dev_get_fd(pdev),
623 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
625 return (err ? LIBUSB_ERROR_OTHER : 0);
628 static struct libusb20_transfer *
629 libusb10_get_transfer(struct libusb20_device *pdev,
630 uint8_t endpoint, uint8_t index)
632 index &= 1; /* double buffering */
634 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
636 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
637 /* this is an IN endpoint */
640 return (libusb20_tr_get_pointer(pdev, index));
644 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
646 struct libusb20_transfer *xfer;
647 struct libusb_device *dev;
650 xfer = libusb10_get_transfer(pdev, endpoint, 0);
652 return (LIBUSB_ERROR_INVALID_PARAM);
654 dev = libusb_get_device(pdev);
656 return (LIBUSB_ERROR_INVALID_PARAM);
659 err = libusb20_tr_open(xfer, 0, 1, endpoint);
660 CTX_UNLOCK(dev->ctx);
662 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
663 return (LIBUSB_ERROR_OTHER);
665 libusb20_tr_clear_stall_sync(xfer);
667 /* check if we opened the transfer */
670 libusb20_tr_close(xfer);
671 CTX_UNLOCK(dev->ctx);
673 return (0); /* success */
677 libusb_reset_device(struct libusb20_device *pdev)
682 dev = libusb_get_device(pdev);
684 return (LIBUSB_ERROR_INVALID_PARAM);
686 libusb10_cancel_all_transfer(dev);
688 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
690 err = libusb20_dev_reset(pdev);
692 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
693 pdev, libusb20_dev_get_fd(pdev),
694 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
696 return (err ? LIBUSB_ERROR_OTHER : 0);
700 libusb_check_connected(struct libusb20_device *pdev)
705 dev = libusb_get_device(pdev);
707 return (LIBUSB_ERROR_INVALID_PARAM);
709 err = libusb20_dev_check_connected(pdev);
711 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
715 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
718 return (LIBUSB_ERROR_INVALID_PARAM);
720 if (libusb20_dev_kernel_driver_active(pdev, interface))
721 return (0); /* no kernel driver is active */
723 return (1); /* kernel driver is active */
727 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
728 char *name, int namelen)
730 return (libusb_get_driver(pdev, interface, name, namelen));
734 libusb_get_driver(struct libusb20_device *pdev, int interface,
735 char *name, int namelen)
741 return (LIBUSB_ERROR_INVALID_PARAM);
743 return (LIBUSB_ERROR_INVALID_PARAM);
747 err = libusb20_dev_get_iface_desc(
748 pdev, interface, name, namelen);
751 return (LIBUSB_ERROR_OTHER);
753 /* we only want the driver name */
754 ptr = strstr(name, ":");
762 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
764 return (libusb_detach_kernel_driver(pdev, interface));
768 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
773 return (LIBUSB_ERROR_INVALID_PARAM);
775 err = libusb20_dev_detach_kernel_driver(
778 return (err ? LIBUSB_ERROR_OTHER : 0);
782 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
785 return (LIBUSB_ERROR_INVALID_PARAM);
786 /* stub - currently not supported by libusb20 */
790 /* Asynchronous device I/O */
792 struct libusb_transfer *
793 libusb_alloc_transfer(int iso_packets)
795 struct libusb_transfer *uxfer;
796 struct libusb_super_transfer *sxfer;
799 len = sizeof(struct libusb_transfer) +
800 sizeof(struct libusb_super_transfer) +
801 (iso_packets * sizeof(libusb_iso_packet_descriptor));
807 memset(sxfer, 0, len);
809 uxfer = (struct libusb_transfer *)(
810 ((uint8_t *)sxfer) + sizeof(*sxfer));
812 /* set default value */
813 uxfer->num_iso_packets = iso_packets;
819 libusb_free_transfer(struct libusb_transfer *uxfer)
821 struct libusb_super_transfer *sxfer;
824 return; /* be NULL safe */
826 /* check if we should free the transfer buffer */
827 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
830 sxfer = (struct libusb_super_transfer *)(
831 (uint8_t *)uxfer - sizeof(*sxfer));
837 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
841 switch (xfer->type) {
842 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
843 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
845 case LIBUSB_TRANSFER_TYPE_CONTROL:
856 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
861 usb_speed = libusb20_dev_get_speed(pdev);
863 switch (xfer->type) {
864 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
865 ret = 0; /* kernel will auto-select */
867 case LIBUSB_TRANSFER_TYPE_CONTROL:
872 case LIBUSB20_SPEED_LOW:
875 case LIBUSB20_SPEED_FULL:
888 libusb10_convert_error(uint8_t status)
893 case LIBUSB20_TRANSFER_START:
894 case LIBUSB20_TRANSFER_COMPLETED:
895 return (LIBUSB_TRANSFER_COMPLETED);
896 case LIBUSB20_TRANSFER_OVERFLOW:
897 return (LIBUSB_TRANSFER_OVERFLOW);
898 case LIBUSB20_TRANSFER_NO_DEVICE:
899 return (LIBUSB_TRANSFER_NO_DEVICE);
900 case LIBUSB20_TRANSFER_STALL:
901 return (LIBUSB_TRANSFER_STALL);
902 case LIBUSB20_TRANSFER_CANCELLED:
903 return (LIBUSB_TRANSFER_CANCELLED);
904 case LIBUSB20_TRANSFER_TIMED_OUT:
905 return (LIBUSB_TRANSFER_TIMED_OUT);
907 return (LIBUSB_TRANSFER_ERROR);
911 /* This function must be called locked */
914 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
915 struct libusb_super_transfer *sxfer, int status)
917 struct libusb_transfer *uxfer;
918 struct libusb_device *dev;
920 uxfer = (struct libusb_transfer *)(
921 ((uint8_t *)sxfer) + sizeof(*sxfer));
924 libusb20_tr_set_priv_sc1(pxfer, NULL);
926 /* set transfer status */
927 uxfer->status = status;
929 /* update super transfer state */
930 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
932 dev = libusb_get_device(uxfer->dev_handle);
934 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
937 /* This function must be called locked */
940 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
942 struct libusb_super_transfer *sxfer;
943 struct libusb_transfer *uxfer;
945 uint16_t iso_packets;
950 status = libusb20_tr_get_status(pxfer);
951 sxfer = libusb20_tr_get_priv_sc1(pxfer);
952 actlen = libusb20_tr_get_actual_length(pxfer);
953 iso_packets = libusb20_tr_get_max_frames(pxfer);
956 return; /* cancelled - nothing to do */
958 uxfer = (struct libusb_transfer *)(
959 ((uint8_t *)sxfer) + sizeof(*sxfer));
961 if (iso_packets > uxfer->num_iso_packets)
962 iso_packets = uxfer->num_iso_packets;
964 if (iso_packets == 0)
965 return; /* nothing to do */
967 /* make sure that the number of ISOCHRONOUS packets is valid */
968 uxfer->num_iso_packets = iso_packets;
970 flags = uxfer->flags;
973 case LIBUSB20_TRANSFER_COMPLETED:
975 /* update actual length */
976 uxfer->actual_length = actlen;
977 for (i = 0; i != iso_packets; i++) {
978 uxfer->iso_packet_desc[i].actual_length =
979 libusb20_tr_get_length(pxfer, i);
981 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
984 case LIBUSB20_TRANSFER_START:
986 /* setup length(s) */
988 for (i = 0; i != iso_packets; i++) {
989 libusb20_tr_setup_isoc(pxfer,
990 &uxfer->buffer[actlen],
991 uxfer->iso_packet_desc[i].length, i);
992 actlen += uxfer->iso_packet_desc[i].length;
998 libusb20_tr_set_total_frames(pxfer, iso_packets);
999 libusb20_tr_submit(pxfer);
1001 /* fork another USB transfer, if any */
1002 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1006 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1011 /* This function must be called locked */
1014 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1016 struct libusb_super_transfer *sxfer;
1017 struct libusb_transfer *uxfer;
1023 status = libusb20_tr_get_status(pxfer);
1024 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1025 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1026 actlen = libusb20_tr_get_actual_length(pxfer);
1029 return; /* cancelled - nothing to do */
1031 uxfer = (struct libusb_transfer *)(
1032 ((uint8_t *)sxfer) + sizeof(*sxfer));
1034 flags = uxfer->flags;
1037 case LIBUSB20_TRANSFER_COMPLETED:
1039 uxfer->actual_length += actlen;
1041 /* check for short packet */
1042 if (sxfer->last_len != actlen) {
1043 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1044 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1046 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1050 /* check for end of data */
1051 if (sxfer->rem_len == 0) {
1052 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1057 case LIBUSB20_TRANSFER_START:
1058 if (max_bulk > sxfer->rem_len) {
1059 max_bulk = sxfer->rem_len;
1061 /* setup new BULK or INTERRUPT transaction */
1062 libusb20_tr_setup_bulk(pxfer,
1063 sxfer->curr_data, max_bulk, uxfer->timeout);
1065 /* update counters */
1066 sxfer->last_len = max_bulk;
1067 sxfer->curr_data += max_bulk;
1068 sxfer->rem_len -= max_bulk;
1070 libusb20_tr_submit(pxfer);
1072 /* check if we can fork another USB transfer */
1073 if (sxfer->rem_len == 0)
1074 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1078 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1083 /* This function must be called locked */
1086 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1088 struct libusb_super_transfer *sxfer;
1089 struct libusb_transfer *uxfer;
1095 status = libusb20_tr_get_status(pxfer);
1096 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1097 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1098 actlen = libusb20_tr_get_actual_length(pxfer);
1101 return; /* cancelled - nothing to do */
1103 uxfer = (struct libusb_transfer *)(
1104 ((uint8_t *)sxfer) + sizeof(*sxfer));
1106 flags = uxfer->flags;
1109 case LIBUSB20_TRANSFER_COMPLETED:
1111 uxfer->actual_length += actlen;
1113 /* subtract length of SETUP packet, if any */
1114 actlen -= libusb20_tr_get_length(pxfer, 0);
1116 /* check for short packet */
1117 if (sxfer->last_len != actlen) {
1118 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1119 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1121 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1125 /* check for end of data */
1126 if (sxfer->rem_len == 0) {
1127 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1132 case LIBUSB20_TRANSFER_START:
1133 if (max_bulk > sxfer->rem_len) {
1134 max_bulk = sxfer->rem_len;
1136 /* setup new CONTROL transaction */
1137 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1138 /* next fragment - don't send SETUP packet */
1139 libusb20_tr_set_length(pxfer, 0, 0);
1141 /* first fragment - send SETUP packet */
1142 libusb20_tr_set_length(pxfer, 8, 0);
1143 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1146 if (max_bulk != 0) {
1147 libusb20_tr_set_length(pxfer, max_bulk, 1);
1148 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1149 libusb20_tr_set_total_frames(pxfer, 2);
1151 libusb20_tr_set_total_frames(pxfer, 1);
1154 /* update counters */
1155 sxfer->last_len = max_bulk;
1156 sxfer->curr_data += max_bulk;
1157 sxfer->rem_len -= max_bulk;
1159 libusb20_tr_submit(pxfer);
1161 /* check if we can fork another USB transfer */
1162 if (sxfer->rem_len == 0)
1163 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1167 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1172 /* The following function must be called locked */
1175 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1177 struct libusb20_transfer *pxfer0;
1178 struct libusb20_transfer *pxfer1;
1179 struct libusb_super_transfer *sxfer;
1180 struct libusb_transfer *uxfer;
1181 struct libusb_device *dev;
1188 dev = libusb_get_device(pdev);
1190 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1191 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1193 if (pxfer0 == NULL || pxfer1 == NULL)
1194 return; /* shouldn't happen */
1197 if (libusb20_tr_pending(pxfer0))
1199 if (libusb20_tr_pending(pxfer1))
1204 /* wait till one of the transfers complete */
1207 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1209 return; /* cancelling */
1211 return; /* cannot queue another one */
1212 /* swap transfers */
1216 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1218 return; /* cancelling */
1220 return; /* cannot queue another one */
1221 /* swap transfers */
1228 /* find next transfer on same endpoint */
1229 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1231 uxfer = (struct libusb_transfer *)(
1232 ((uint8_t *)sxfer) + sizeof(*sxfer));
1234 if (uxfer->endpoint == endpoint) {
1235 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1236 sxfer->entry.tqe_prev = NULL;
1240 return; /* success */
1244 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1245 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1247 /* reset super transfer state */
1248 sxfer->rem_len = uxfer->length;
1249 sxfer->curr_data = uxfer->buffer;
1250 uxfer->actual_length = 0;
1252 switch (uxfer->type) {
1253 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1254 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1256 case LIBUSB_TRANSFER_TYPE_BULK:
1257 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1258 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1260 case LIBUSB_TRANSFER_TYPE_CONTROL:
1261 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1262 if (sxfer->rem_len < 8)
1265 /* remove SETUP packet from data */
1266 sxfer->rem_len -= 8;
1267 sxfer->curr_data += 8;
1273 buffsize = libusb10_get_buffsize(pdev, uxfer);
1274 maxframe = libusb10_get_maxframe(pdev, uxfer);
1276 /* make sure the transfer is opened */
1277 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1278 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1281 libusb20_tr_start(pxfer0);
1285 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1287 /* make sure our event loop spins the done handler */
1289 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1292 /* The following function must be called unlocked */
1295 libusb_submit_transfer(struct libusb_transfer *uxfer)
1297 struct libusb20_transfer *pxfer0;
1298 struct libusb20_transfer *pxfer1;
1299 struct libusb_super_transfer *sxfer;
1300 struct libusb_device *dev;
1305 return (LIBUSB_ERROR_INVALID_PARAM);
1307 if (uxfer->dev_handle == NULL)
1308 return (LIBUSB_ERROR_INVALID_PARAM);
1310 endpoint = uxfer->endpoint;
1313 return (LIBUSB_ERROR_INVALID_PARAM);
1315 dev = libusb_get_device(uxfer->dev_handle);
1317 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1319 sxfer = (struct libusb_super_transfer *)(
1320 (uint8_t *)uxfer - sizeof(*sxfer));
1324 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1325 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1327 if (pxfer0 == NULL || pxfer1 == NULL) {
1328 err = LIBUSB_ERROR_OTHER;
1329 } else if ((sxfer->entry.tqe_prev != NULL) ||
1330 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1331 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1332 err = LIBUSB_ERROR_BUSY;
1335 /* set pending state */
1336 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1338 /* insert transfer into transfer head list */
1339 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1341 /* start work transfers */
1342 libusb10_submit_transfer_sub(
1343 uxfer->dev_handle, endpoint);
1345 err = 0; /* success */
1348 CTX_UNLOCK(dev->ctx);
1350 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1355 /* Asynchronous transfer cancel */
1358 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1360 struct libusb20_transfer *pxfer0;
1361 struct libusb20_transfer *pxfer1;
1362 struct libusb_super_transfer *sxfer;
1363 struct libusb_device *dev;
1368 return (LIBUSB_ERROR_INVALID_PARAM);
1370 /* check if not initialised */
1371 if (uxfer->dev_handle == NULL)
1372 return (LIBUSB_ERROR_NOT_FOUND);
1374 endpoint = uxfer->endpoint;
1377 return (LIBUSB_ERROR_INVALID_PARAM);
1379 dev = libusb_get_device(uxfer->dev_handle);
1381 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1383 sxfer = (struct libusb_super_transfer *)(
1384 (uint8_t *)uxfer - sizeof(*sxfer));
1390 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1391 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1393 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1394 /* only update the transfer status */
1395 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1396 retval = LIBUSB_ERROR_NOT_FOUND;
1397 } else if (sxfer->entry.tqe_prev != NULL) {
1398 /* we are lucky - transfer is on a queue */
1399 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1400 sxfer->entry.tqe_prev = NULL;
1401 libusb10_complete_transfer(NULL,
1402 sxfer, LIBUSB_TRANSFER_CANCELLED);
1403 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1405 retval = LIBUSB_ERROR_NOT_FOUND;
1406 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1407 libusb10_complete_transfer(pxfer0,
1408 sxfer, LIBUSB_TRANSFER_CANCELLED);
1409 libusb20_tr_stop(pxfer0);
1410 /* make sure the queue doesn't stall */
1411 libusb10_submit_transfer_sub(
1412 uxfer->dev_handle, endpoint);
1413 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1414 libusb10_complete_transfer(pxfer1,
1415 sxfer, LIBUSB_TRANSFER_CANCELLED);
1416 libusb20_tr_stop(pxfer1);
1417 /* make sure the queue doesn't stall */
1418 libusb10_submit_transfer_sub(
1419 uxfer->dev_handle, endpoint);
1422 retval = LIBUSB_ERROR_NOT_FOUND;
1425 CTX_UNLOCK(dev->ctx);
1427 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1433 libusb10_cancel_all_transfer(libusb_device *dev)
1439 libusb_cpu_to_le16(uint16_t x)
1441 return (htole16(x));
1445 libusb_le16_to_cpu(uint16_t x)
1447 return (le16toh(x));
1451 libusb_strerror(int code)
1454 case LIBUSB_SUCCESS:
1456 case LIBUSB_ERROR_IO:
1457 return ("I/O error");
1458 case LIBUSB_ERROR_INVALID_PARAM:
1459 return ("Invalid parameter");
1460 case LIBUSB_ERROR_ACCESS:
1461 return ("Permissions error");
1462 case LIBUSB_ERROR_NO_DEVICE:
1463 return ("No device");
1464 case LIBUSB_ERROR_NOT_FOUND:
1465 return ("Not found");
1466 case LIBUSB_ERROR_BUSY:
1467 return ("Device busy");
1468 case LIBUSB_ERROR_TIMEOUT:
1470 case LIBUSB_ERROR_OVERFLOW:
1471 return ("Overflow");
1472 case LIBUSB_ERROR_PIPE:
1473 return ("Pipe error");
1474 case LIBUSB_ERROR_INTERRUPTED:
1475 return ("Interrupted");
1476 case LIBUSB_ERROR_NO_MEM:
1477 return ("Out of memory");
1478 case LIBUSB_ERROR_NOT_SUPPORTED:
1479 return ("Not supported");
1480 case LIBUSB_ERROR_OTHER:
1481 return ("Other error");
1483 return ("Unknown error");
1488 libusb_error_name(int code)
1491 case LIBUSB_SUCCESS:
1492 return ("LIBUSB_SUCCESS");
1493 case LIBUSB_ERROR_IO:
1494 return ("LIBUSB_ERROR_IO");
1495 case LIBUSB_ERROR_INVALID_PARAM:
1496 return ("LIBUSB_ERROR_INVALID_PARAM");
1497 case LIBUSB_ERROR_ACCESS:
1498 return ("LIBUSB_ERROR_ACCESS");
1499 case LIBUSB_ERROR_NO_DEVICE:
1500 return ("LIBUSB_ERROR_NO_DEVICE");
1501 case LIBUSB_ERROR_NOT_FOUND:
1502 return ("LIBUSB_ERROR_NOT_FOUND");
1503 case LIBUSB_ERROR_BUSY:
1504 return ("LIBUSB_ERROR_BUSY");
1505 case LIBUSB_ERROR_TIMEOUT:
1506 return ("LIBUSB_ERROR_TIMEOUT");
1507 case LIBUSB_ERROR_OVERFLOW:
1508 return ("LIBUSB_ERROR_OVERFLOW");
1509 case LIBUSB_ERROR_PIPE:
1510 return ("LIBUSB_ERROR_PIPE");
1511 case LIBUSB_ERROR_INTERRUPTED:
1512 return ("LIBUSB_ERROR_INTERRUPTED");
1513 case LIBUSB_ERROR_NO_MEM:
1514 return ("LIBUSB_ERROR_NO_MEM");
1515 case LIBUSB_ERROR_NOT_SUPPORTED:
1516 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1517 case LIBUSB_ERROR_OTHER:
1518 return ("LIBUSB_ERROR_OTHER");
1520 return ("LIBUSB_ERROR_UNKNOWN");