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 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
50 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
51 struct libusb_context *usbi_default_context = NULL;
55 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
56 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
57 static int libusb10_convert_error(uint8_t status);
58 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
59 static void libusb10_isoc_proxy(struct libusb20_transfer *);
60 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
61 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
62 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
64 /* Library initialisation / deinitialisation */
67 libusb_set_debug(libusb_context *ctx, int level)
69 ctx = GET_CONTEXT(ctx);
75 libusb_set_nonblocking(int f)
80 * We ignore any failures in this function, hence the
81 * non-blocking flag is not critical to the operation of
82 * libUSB. We use F_GETFL and F_SETFL to be compatible with
86 flags = fcntl(f, F_GETFL, NULL);
90 fcntl(f, F_SETFL, flags);
94 libusb_init(libusb_context **context)
96 struct libusb_context *ctx;
97 pthread_condattr_t attr;
101 ctx = malloc(sizeof(*ctx));
103 return (LIBUSB_ERROR_INVALID_PARAM);
105 memset(ctx, 0, sizeof(*ctx));
107 debug = getenv("LIBUSB_DEBUG");
109 ctx->debug = atoi(debug);
111 ctx->debug_fixed = 1;
113 TAILQ_INIT(&ctx->pollfds);
114 TAILQ_INIT(&ctx->tr_done);
116 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
118 return (LIBUSB_ERROR_NO_MEM);
120 if (pthread_condattr_init(&attr) != 0) {
121 pthread_mutex_destroy(&ctx->ctx_lock);
123 return (LIBUSB_ERROR_NO_MEM);
125 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
126 pthread_mutex_destroy(&ctx->ctx_lock);
127 pthread_condattr_destroy(&attr);
129 return (LIBUSB_ERROR_OTHER);
131 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
132 pthread_mutex_destroy(&ctx->ctx_lock);
133 pthread_condattr_destroy(&attr);
135 return (LIBUSB_ERROR_NO_MEM);
137 pthread_condattr_destroy(&attr);
139 ctx->ctx_handler = NO_THREAD;
141 ret = pipe(ctx->ctrl_pipe);
143 pthread_mutex_destroy(&ctx->ctx_lock);
144 pthread_cond_destroy(&ctx->ctx_cond);
146 return (LIBUSB_ERROR_OTHER);
148 /* set non-blocking mode on the control pipe to avoid deadlock */
149 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
150 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
152 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
154 pthread_mutex_lock(&default_context_lock);
155 if (usbi_default_context == NULL) {
156 usbi_default_context = ctx;
158 pthread_mutex_unlock(&default_context_lock);
163 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
169 libusb_exit(libusb_context *ctx)
171 ctx = GET_CONTEXT(ctx);
176 /* XXX cleanup devices */
178 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
179 close(ctx->ctrl_pipe[0]);
180 close(ctx->ctrl_pipe[1]);
181 pthread_mutex_destroy(&ctx->ctx_lock);
182 pthread_cond_destroy(&ctx->ctx_cond);
184 pthread_mutex_lock(&default_context_lock);
185 if (ctx == usbi_default_context) {
186 usbi_default_context = NULL;
188 pthread_mutex_unlock(&default_context_lock);
193 /* Device handling and initialisation. */
196 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
198 struct libusb20_backend *usb_backend;
199 struct libusb20_device *pdev;
200 struct libusb_device *dev;
203 ctx = GET_CONTEXT(ctx);
206 return (LIBUSB_ERROR_INVALID_PARAM);
209 return (LIBUSB_ERROR_INVALID_PARAM);
211 usb_backend = libusb20_be_alloc_default();
212 if (usb_backend == NULL)
213 return (LIBUSB_ERROR_NO_MEM);
215 /* figure out how many USB devices are present */
218 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
221 /* allocate device pointer list */
222 *list = malloc((i + 1) * sizeof(void *));
224 libusb20_be_free(usb_backend);
225 return (LIBUSB_ERROR_NO_MEM);
227 /* create libusb v1.0 compliant devices */
229 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
231 dev = malloc(sizeof(*dev));
234 libusb_unref_device((*list)[i - 1]);
239 libusb20_be_free(usb_backend);
240 return (LIBUSB_ERROR_NO_MEM);
242 /* get device into libUSB v1.0 list */
243 libusb20_be_dequeue_device(usb_backend, pdev);
245 memset(dev, 0, sizeof(*dev));
247 /* init transfer queues */
248 TAILQ_INIT(&dev->tr_head);
250 /* set context we belong to */
253 /* link together the two structures */
255 pdev->privLuData = dev;
257 (*list)[i] = libusb_ref_device(dev);
262 libusb20_be_free(usb_backend);
267 libusb_free_device_list(libusb_device **list, int unref_devices)
272 return; /* be NULL safe */
275 for (i = 0; list[i] != NULL; i++)
276 libusb_unref_device(list[i]);
282 libusb_get_bus_number(libusb_device *dev)
285 return (0); /* should not happen */
286 return (libusb20_dev_get_bus_number(dev->os_priv));
290 libusb_get_device_address(libusb_device *dev)
293 return (0); /* should not happen */
294 return (libusb20_dev_get_address(dev->os_priv));
298 libusb_get_device_speed(libusb_device *dev)
301 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
303 switch (libusb20_dev_get_speed(dev->os_priv)) {
304 case LIBUSB20_SPEED_LOW:
305 return (LIBUSB_SPEED_LOW);
306 case LIBUSB20_SPEED_FULL:
307 return (LIBUSB_SPEED_FULL);
308 case LIBUSB20_SPEED_HIGH:
309 return (LIBUSB_SPEED_HIGH);
310 case LIBUSB20_SPEED_SUPER:
311 return (LIBUSB_SPEED_SUPER);
315 return (LIBUSB_SPEED_UNKNOWN);
319 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
321 struct libusb_config_descriptor *pdconf;
322 struct libusb_interface *pinf;
323 struct libusb_interface_descriptor *pdinf;
324 struct libusb_endpoint_descriptor *pdend;
331 return (LIBUSB_ERROR_NO_DEVICE);
333 ret = libusb_get_active_config_descriptor(dev, &pdconf);
337 ret = LIBUSB_ERROR_NOT_FOUND;
338 for (i = 0; i < pdconf->bNumInterfaces; i++) {
339 pinf = &pdconf->interface[i];
340 for (j = 0; j < pinf->num_altsetting; j++) {
341 pdinf = &pinf->altsetting[j];
342 for (k = 0; k < pdinf->bNumEndpoints; k++) {
343 pdend = &pdinf->endpoint[k];
344 if (pdend->bEndpointAddress == endpoint) {
345 ret = pdend->wMaxPacketSize;
353 libusb_free_config_descriptor(pdconf);
358 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
363 ret = libusb_get_max_packet_size(dev, endpoint);
365 switch (libusb20_dev_get_speed(dev->os_priv)) {
366 case LIBUSB20_SPEED_LOW:
367 case LIBUSB20_SPEED_FULL:
371 multiplier = (1 + ((ret >> 11) & 3));
374 ret = (ret & 0x7FF) * multiplier;
382 libusb_ref_device(libusb_device *dev)
385 return (NULL); /* be NULL safe */
389 CTX_UNLOCK(dev->ctx);
395 libusb_unref_device(libusb_device *dev)
398 return; /* be NULL safe */
402 CTX_UNLOCK(dev->ctx);
404 if (dev->refcnt == 0) {
405 libusb20_dev_free(dev->os_priv);
411 libusb_open(libusb_device *dev, libusb_device_handle **devh)
413 libusb_context *ctx = dev->ctx;
414 struct libusb20_device *pdev = dev->os_priv;
419 return (LIBUSB_ERROR_INVALID_PARAM);
421 /* set default device handle value */
424 dev = libusb_ref_device(dev);
426 return (LIBUSB_ERROR_INVALID_PARAM);
428 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
430 libusb_unref_device(dev);
431 return (LIBUSB_ERROR_NO_MEM);
433 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
434 POLLOUT | POLLRDNORM | POLLWRNORM);
436 /* make sure our event loop detects the new device */
438 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
439 if (err < (int)sizeof(dummy)) {
440 /* ignore error, if any */
441 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
448 libusb_device_handle *
449 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
452 struct libusb_device **devs;
453 struct libusb20_device *pdev;
454 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
458 ctx = GET_CONTEXT(ctx);
460 return (NULL); /* be NULL safe */
462 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
464 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
468 for (j = 0; j < i; j++) {
469 struct libusb20_device *tdev;
471 tdev = devs[j]->os_priv;
472 pdesc = libusb20_dev_get_device_desc(tdev);
474 * NOTE: The USB library will automatically swap the
475 * fields in the device descriptor to be of host
478 if (pdesc->idVendor == vendor_id &&
479 pdesc->idProduct == product_id) {
480 libusb_open(devs[j], &pdev);
485 libusb_free_device_list(devs, 1);
486 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
491 libusb_close(struct libusb20_device *pdev)
494 struct libusb_device *dev;
499 return; /* be NULL safe */
501 dev = libusb_get_device(pdev);
504 libusb10_remove_pollfd(ctx, &dev->dev_poll);
506 libusb20_dev_close(pdev);
508 /* unref will free the "pdev" when the refcount reaches zero */
509 libusb_unref_device(dev);
511 /* make sure our event loop detects the closed device */
513 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
514 if (err < (int)sizeof(dummy)) {
515 /* ignore error, if any */
516 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
521 libusb_get_device(struct libusb20_device *pdev)
525 return ((libusb_device *)pdev->privLuData);
529 libusb_get_configuration(struct libusb20_device *pdev, int *config)
531 struct libusb20_config *pconf;
533 if (pdev == NULL || config == NULL)
534 return (LIBUSB_ERROR_INVALID_PARAM);
536 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
538 return (LIBUSB_ERROR_NO_MEM);
540 *config = pconf->desc.bConfigurationValue;
548 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
550 struct libusb20_config *pconf;
551 struct libusb_device *dev;
555 dev = libusb_get_device(pdev);
557 return (LIBUSB_ERROR_INVALID_PARAM);
559 if (configuration < 1) {
563 for (i = 0; i != 255; i++) {
566 pconf = libusb20_dev_alloc_config(pdev, i);
568 return (LIBUSB_ERROR_INVALID_PARAM);
569 found = (pconf->desc.bConfigurationValue
576 return (LIBUSB_ERROR_INVALID_PARAM);
581 libusb10_cancel_all_transfer(dev);
583 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
585 err = libusb20_dev_set_config_index(pdev, i);
587 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
588 POLLOUT | POLLRDNORM | POLLWRNORM);
590 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
594 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
598 dev = libusb_get_device(pdev);
600 return (LIBUSB_ERROR_INVALID_PARAM);
602 if (interface_number < 0 || interface_number > 31)
603 return (LIBUSB_ERROR_INVALID_PARAM);
606 dev->claimed_interfaces |= (1 << interface_number);
607 CTX_UNLOCK(dev->ctx);
613 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
618 dev = libusb_get_device(pdev);
620 return (LIBUSB_ERROR_INVALID_PARAM);
622 if (interface_number < 0 || interface_number > 31)
623 return (LIBUSB_ERROR_INVALID_PARAM);
626 if (!(dev->claimed_interfaces & (1 << interface_number)))
627 err = LIBUSB_ERROR_NOT_FOUND;
630 dev->claimed_interfaces &= ~(1 << interface_number);
631 CTX_UNLOCK(dev->ctx);
636 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
637 int interface_number, int alternate_setting)
642 dev = libusb_get_device(pdev);
644 return (LIBUSB_ERROR_INVALID_PARAM);
646 if (interface_number < 0 || interface_number > 31)
647 return (LIBUSB_ERROR_INVALID_PARAM);
650 if (!(dev->claimed_interfaces & (1 << interface_number)))
651 err = LIBUSB_ERROR_NOT_FOUND;
652 CTX_UNLOCK(dev->ctx);
657 libusb10_cancel_all_transfer(dev);
659 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
661 err = libusb20_dev_set_alt_index(pdev,
662 interface_number, alternate_setting);
664 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
665 pdev, libusb20_dev_get_fd(pdev),
666 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
668 return (err ? LIBUSB_ERROR_OTHER : 0);
671 static struct libusb20_transfer *
672 libusb10_get_transfer(struct libusb20_device *pdev,
673 uint8_t endpoint, uint8_t xfer_index)
675 xfer_index &= 1; /* double buffering */
677 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
679 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
680 /* this is an IN endpoint */
683 return (libusb20_tr_get_pointer(pdev, xfer_index));
687 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
689 struct libusb20_transfer *xfer;
690 struct libusb_device *dev;
693 xfer = libusb10_get_transfer(pdev, endpoint, 0);
695 return (LIBUSB_ERROR_INVALID_PARAM);
697 dev = libusb_get_device(pdev);
699 return (LIBUSB_ERROR_INVALID_PARAM);
702 err = libusb20_tr_open(xfer, 0, 1, endpoint);
703 CTX_UNLOCK(dev->ctx);
705 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
706 return (LIBUSB_ERROR_OTHER);
708 libusb20_tr_clear_stall_sync(xfer);
710 /* check if we opened the transfer */
713 libusb20_tr_close(xfer);
714 CTX_UNLOCK(dev->ctx);
716 return (0); /* success */
720 libusb_reset_device(struct libusb20_device *pdev)
725 dev = libusb_get_device(pdev);
727 return (LIBUSB_ERROR_INVALID_PARAM);
729 libusb10_cancel_all_transfer(dev);
731 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
733 err = libusb20_dev_reset(pdev);
735 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
736 pdev, libusb20_dev_get_fd(pdev),
737 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
739 return (err ? LIBUSB_ERROR_OTHER : 0);
743 libusb_check_connected(struct libusb20_device *pdev)
748 dev = libusb_get_device(pdev);
750 return (LIBUSB_ERROR_INVALID_PARAM);
752 err = libusb20_dev_check_connected(pdev);
754 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
758 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
761 return (LIBUSB_ERROR_INVALID_PARAM);
763 if (libusb20_dev_kernel_driver_active(pdev, interface))
764 return (0); /* no kernel driver is active */
766 return (1); /* kernel driver is active */
770 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
771 char *name, int namelen)
773 return (libusb_get_driver(pdev, interface, name, namelen));
777 libusb_get_driver(struct libusb20_device *pdev, int interface,
778 char *name, int namelen)
784 return (LIBUSB_ERROR_INVALID_PARAM);
786 return (LIBUSB_ERROR_INVALID_PARAM);
790 err = libusb20_dev_get_iface_desc(
791 pdev, interface, name, namelen);
794 return (LIBUSB_ERROR_OTHER);
796 /* we only want the driver name */
797 ptr = strstr(name, ":");
805 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
807 return (libusb_detach_kernel_driver(pdev, interface));
811 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
816 return (LIBUSB_ERROR_INVALID_PARAM);
818 err = libusb20_dev_detach_kernel_driver(
821 return (err ? LIBUSB_ERROR_OTHER : 0);
825 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
828 return (LIBUSB_ERROR_INVALID_PARAM);
829 /* stub - currently not supported by libusb20 */
833 /* Asynchronous device I/O */
835 struct libusb_transfer *
836 libusb_alloc_transfer(int iso_packets)
838 struct libusb_transfer *uxfer;
839 struct libusb_super_transfer *sxfer;
842 len = sizeof(struct libusb_transfer) +
843 sizeof(struct libusb_super_transfer) +
844 (iso_packets * sizeof(libusb_iso_packet_descriptor));
850 memset(sxfer, 0, len);
852 uxfer = (struct libusb_transfer *)(
853 ((uint8_t *)sxfer) + sizeof(*sxfer));
855 /* set default value */
856 uxfer->num_iso_packets = iso_packets;
862 libusb_free_transfer(struct libusb_transfer *uxfer)
864 struct libusb_super_transfer *sxfer;
867 return; /* be NULL safe */
869 /* check if we should free the transfer buffer */
870 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
873 sxfer = (struct libusb_super_transfer *)(
874 (uint8_t *)uxfer - sizeof(*sxfer));
880 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
884 switch (xfer->type) {
885 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
886 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
888 case LIBUSB_TRANSFER_TYPE_CONTROL:
899 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
904 usb_speed = libusb20_dev_get_speed(pdev);
906 switch (xfer->type) {
907 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
908 ret = 0; /* kernel will auto-select */
910 case LIBUSB_TRANSFER_TYPE_CONTROL:
915 case LIBUSB20_SPEED_LOW:
918 case LIBUSB20_SPEED_FULL:
921 case LIBUSB20_SPEED_SUPER:
934 libusb10_convert_error(uint8_t status)
939 case LIBUSB20_TRANSFER_START:
940 case LIBUSB20_TRANSFER_COMPLETED:
941 return (LIBUSB_TRANSFER_COMPLETED);
942 case LIBUSB20_TRANSFER_OVERFLOW:
943 return (LIBUSB_TRANSFER_OVERFLOW);
944 case LIBUSB20_TRANSFER_NO_DEVICE:
945 return (LIBUSB_TRANSFER_NO_DEVICE);
946 case LIBUSB20_TRANSFER_STALL:
947 return (LIBUSB_TRANSFER_STALL);
948 case LIBUSB20_TRANSFER_CANCELLED:
949 return (LIBUSB_TRANSFER_CANCELLED);
950 case LIBUSB20_TRANSFER_TIMED_OUT:
951 return (LIBUSB_TRANSFER_TIMED_OUT);
953 return (LIBUSB_TRANSFER_ERROR);
957 /* This function must be called locked */
960 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
961 struct libusb_super_transfer *sxfer, int status)
963 struct libusb_transfer *uxfer;
964 struct libusb_device *dev;
966 uxfer = (struct libusb_transfer *)(
967 ((uint8_t *)sxfer) + sizeof(*sxfer));
970 libusb20_tr_set_priv_sc1(pxfer, NULL);
972 /* set transfer status */
973 uxfer->status = status;
975 /* update super transfer state */
976 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
978 dev = libusb_get_device(uxfer->dev_handle);
980 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
983 /* This function must be called locked */
986 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
988 struct libusb_super_transfer *sxfer;
989 struct libusb_transfer *uxfer;
991 uint16_t iso_packets;
996 status = libusb20_tr_get_status(pxfer);
997 sxfer = libusb20_tr_get_priv_sc1(pxfer);
998 actlen = libusb20_tr_get_actual_length(pxfer);
999 iso_packets = libusb20_tr_get_max_frames(pxfer);
1002 return; /* cancelled - nothing to do */
1004 uxfer = (struct libusb_transfer *)(
1005 ((uint8_t *)sxfer) + sizeof(*sxfer));
1007 if (iso_packets > uxfer->num_iso_packets)
1008 iso_packets = uxfer->num_iso_packets;
1010 if (iso_packets == 0)
1011 return; /* nothing to do */
1013 /* make sure that the number of ISOCHRONOUS packets is valid */
1014 uxfer->num_iso_packets = iso_packets;
1016 flags = uxfer->flags;
1019 case LIBUSB20_TRANSFER_COMPLETED:
1021 /* update actual length */
1022 uxfer->actual_length = actlen;
1023 for (i = 0; i != iso_packets; i++) {
1024 uxfer->iso_packet_desc[i].actual_length =
1025 libusb20_tr_get_length(pxfer, i);
1027 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1030 case LIBUSB20_TRANSFER_START:
1032 /* setup length(s) */
1034 for (i = 0; i != iso_packets; i++) {
1035 libusb20_tr_setup_isoc(pxfer,
1036 &uxfer->buffer[actlen],
1037 uxfer->iso_packet_desc[i].length, i);
1038 actlen += uxfer->iso_packet_desc[i].length;
1044 libusb20_tr_set_total_frames(pxfer, iso_packets);
1045 libusb20_tr_submit(pxfer);
1047 /* fork another USB transfer, if any */
1048 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1052 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1057 /* This function must be called locked */
1060 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1062 struct libusb_super_transfer *sxfer;
1063 struct libusb_transfer *uxfer;
1069 status = libusb20_tr_get_status(pxfer);
1070 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1071 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1072 actlen = libusb20_tr_get_actual_length(pxfer);
1075 return; /* cancelled - nothing to do */
1077 uxfer = (struct libusb_transfer *)(
1078 ((uint8_t *)sxfer) + sizeof(*sxfer));
1080 flags = uxfer->flags;
1083 case LIBUSB20_TRANSFER_COMPLETED:
1085 uxfer->actual_length += actlen;
1087 /* check for short packet */
1088 if (sxfer->last_len != actlen) {
1089 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1090 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1092 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1096 /* check for end of data */
1097 if (sxfer->rem_len == 0) {
1098 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1103 case LIBUSB20_TRANSFER_START:
1104 if (max_bulk > sxfer->rem_len) {
1105 max_bulk = sxfer->rem_len;
1107 /* setup new BULK or INTERRUPT transaction */
1108 libusb20_tr_setup_bulk(pxfer,
1109 sxfer->curr_data, max_bulk, uxfer->timeout);
1111 /* update counters */
1112 sxfer->last_len = max_bulk;
1113 sxfer->curr_data += max_bulk;
1114 sxfer->rem_len -= max_bulk;
1116 libusb20_tr_submit(pxfer);
1118 /* check if we can fork another USB transfer */
1119 if (sxfer->rem_len == 0)
1120 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1124 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1129 /* This function must be called locked */
1132 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1134 struct libusb_super_transfer *sxfer;
1135 struct libusb_transfer *uxfer;
1141 status = libusb20_tr_get_status(pxfer);
1142 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1143 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1144 actlen = libusb20_tr_get_actual_length(pxfer);
1147 return; /* cancelled - nothing to do */
1149 uxfer = (struct libusb_transfer *)(
1150 ((uint8_t *)sxfer) + sizeof(*sxfer));
1152 flags = uxfer->flags;
1155 case LIBUSB20_TRANSFER_COMPLETED:
1157 uxfer->actual_length += actlen;
1159 /* subtract length of SETUP packet, if any */
1160 actlen -= libusb20_tr_get_length(pxfer, 0);
1162 /* check for short packet */
1163 if (sxfer->last_len != actlen) {
1164 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1165 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1167 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1171 /* check for end of data */
1172 if (sxfer->rem_len == 0) {
1173 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1178 case LIBUSB20_TRANSFER_START:
1179 if (max_bulk > sxfer->rem_len) {
1180 max_bulk = sxfer->rem_len;
1182 /* setup new CONTROL transaction */
1183 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1184 /* next fragment - don't send SETUP packet */
1185 libusb20_tr_set_length(pxfer, 0, 0);
1187 /* first fragment - send SETUP packet */
1188 libusb20_tr_set_length(pxfer, 8, 0);
1189 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1192 if (max_bulk != 0) {
1193 libusb20_tr_set_length(pxfer, max_bulk, 1);
1194 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1195 libusb20_tr_set_total_frames(pxfer, 2);
1197 libusb20_tr_set_total_frames(pxfer, 1);
1200 /* update counters */
1201 sxfer->last_len = max_bulk;
1202 sxfer->curr_data += max_bulk;
1203 sxfer->rem_len -= max_bulk;
1205 libusb20_tr_submit(pxfer);
1207 /* check if we can fork another USB transfer */
1208 if (sxfer->rem_len == 0)
1209 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1213 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1218 /* The following function must be called locked */
1221 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1223 struct libusb20_transfer *pxfer0;
1224 struct libusb20_transfer *pxfer1;
1225 struct libusb_super_transfer *sxfer;
1226 struct libusb_transfer *uxfer;
1227 struct libusb_device *dev;
1234 dev = libusb_get_device(pdev);
1236 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1237 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1239 if (pxfer0 == NULL || pxfer1 == NULL)
1240 return; /* shouldn't happen */
1243 if (libusb20_tr_pending(pxfer0))
1245 if (libusb20_tr_pending(pxfer1))
1250 /* wait till one of the transfers complete */
1253 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1255 return; /* cancelling */
1257 return; /* cannot queue another one */
1258 /* swap transfers */
1262 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1264 return; /* cancelling */
1266 return; /* cannot queue another one */
1267 /* swap transfers */
1274 /* find next transfer on same endpoint */
1275 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1277 uxfer = (struct libusb_transfer *)(
1278 ((uint8_t *)sxfer) + sizeof(*sxfer));
1280 if (uxfer->endpoint == endpoint) {
1281 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1282 sxfer->entry.tqe_prev = NULL;
1286 return; /* success */
1290 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1291 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1293 /* reset super transfer state */
1294 sxfer->rem_len = uxfer->length;
1295 sxfer->curr_data = uxfer->buffer;
1296 uxfer->actual_length = 0;
1298 switch (uxfer->type) {
1299 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1300 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1302 case LIBUSB_TRANSFER_TYPE_BULK:
1303 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1304 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1306 case LIBUSB_TRANSFER_TYPE_CONTROL:
1307 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1308 if (sxfer->rem_len < 8)
1311 /* remove SETUP packet from data */
1312 sxfer->rem_len -= 8;
1313 sxfer->curr_data += 8;
1319 buffsize = libusb10_get_buffsize(pdev, uxfer);
1320 maxframe = libusb10_get_maxframe(pdev, uxfer);
1322 /* make sure the transfer is opened */
1323 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1324 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1327 libusb20_tr_start(pxfer0);
1331 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1333 /* make sure our event loop spins the done handler */
1335 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1338 /* The following function must be called unlocked */
1341 libusb_submit_transfer(struct libusb_transfer *uxfer)
1343 struct libusb20_transfer *pxfer0;
1344 struct libusb20_transfer *pxfer1;
1345 struct libusb_super_transfer *sxfer;
1346 struct libusb_device *dev;
1351 return (LIBUSB_ERROR_INVALID_PARAM);
1353 if (uxfer->dev_handle == NULL)
1354 return (LIBUSB_ERROR_INVALID_PARAM);
1356 endpoint = uxfer->endpoint;
1359 return (LIBUSB_ERROR_INVALID_PARAM);
1361 dev = libusb_get_device(uxfer->dev_handle);
1363 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1365 sxfer = (struct libusb_super_transfer *)(
1366 (uint8_t *)uxfer - sizeof(*sxfer));
1370 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1371 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1373 if (pxfer0 == NULL || pxfer1 == NULL) {
1374 err = LIBUSB_ERROR_OTHER;
1375 } else if ((sxfer->entry.tqe_prev != NULL) ||
1376 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1377 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1378 err = LIBUSB_ERROR_BUSY;
1381 /* set pending state */
1382 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1384 /* insert transfer into transfer head list */
1385 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1387 /* start work transfers */
1388 libusb10_submit_transfer_sub(
1389 uxfer->dev_handle, endpoint);
1391 err = 0; /* success */
1394 CTX_UNLOCK(dev->ctx);
1396 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1401 /* Asynchronous transfer cancel */
1404 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1406 struct libusb20_transfer *pxfer0;
1407 struct libusb20_transfer *pxfer1;
1408 struct libusb_super_transfer *sxfer;
1409 struct libusb_device *dev;
1414 return (LIBUSB_ERROR_INVALID_PARAM);
1416 /* check if not initialised */
1417 if (uxfer->dev_handle == NULL)
1418 return (LIBUSB_ERROR_NOT_FOUND);
1420 endpoint = uxfer->endpoint;
1423 return (LIBUSB_ERROR_INVALID_PARAM);
1425 dev = libusb_get_device(uxfer->dev_handle);
1427 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1429 sxfer = (struct libusb_super_transfer *)(
1430 (uint8_t *)uxfer - sizeof(*sxfer));
1436 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1437 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1439 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1440 /* only update the transfer status */
1441 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1442 retval = LIBUSB_ERROR_NOT_FOUND;
1443 } else if (sxfer->entry.tqe_prev != NULL) {
1444 /* we are lucky - transfer is on a queue */
1445 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1446 sxfer->entry.tqe_prev = NULL;
1447 libusb10_complete_transfer(NULL,
1448 sxfer, LIBUSB_TRANSFER_CANCELLED);
1449 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1451 retval = LIBUSB_ERROR_NOT_FOUND;
1452 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1453 libusb10_complete_transfer(pxfer0,
1454 sxfer, LIBUSB_TRANSFER_CANCELLED);
1455 libusb20_tr_stop(pxfer0);
1456 /* make sure the queue doesn't stall */
1457 libusb10_submit_transfer_sub(
1458 uxfer->dev_handle, endpoint);
1459 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1460 libusb10_complete_transfer(pxfer1,
1461 sxfer, LIBUSB_TRANSFER_CANCELLED);
1462 libusb20_tr_stop(pxfer1);
1463 /* make sure the queue doesn't stall */
1464 libusb10_submit_transfer_sub(
1465 uxfer->dev_handle, endpoint);
1468 retval = LIBUSB_ERROR_NOT_FOUND;
1471 CTX_UNLOCK(dev->ctx);
1473 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1479 libusb10_cancel_all_transfer(libusb_device *dev)
1481 struct libusb20_device *pdev = dev->os_priv;
1484 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1485 struct libusb20_transfer *xfer;
1487 xfer = libusb20_tr_get_pointer(pdev, x);
1490 libusb20_tr_close(xfer);
1495 libusb_cpu_to_le16(uint16_t x)
1497 return (htole16(x));
1501 libusb_le16_to_cpu(uint16_t x)
1503 return (le16toh(x));
1507 libusb_strerror(int code)
1510 case LIBUSB_SUCCESS:
1512 case LIBUSB_ERROR_IO:
1513 return ("I/O error");
1514 case LIBUSB_ERROR_INVALID_PARAM:
1515 return ("Invalid parameter");
1516 case LIBUSB_ERROR_ACCESS:
1517 return ("Permissions error");
1518 case LIBUSB_ERROR_NO_DEVICE:
1519 return ("No device");
1520 case LIBUSB_ERROR_NOT_FOUND:
1521 return ("Not found");
1522 case LIBUSB_ERROR_BUSY:
1523 return ("Device busy");
1524 case LIBUSB_ERROR_TIMEOUT:
1526 case LIBUSB_ERROR_OVERFLOW:
1527 return ("Overflow");
1528 case LIBUSB_ERROR_PIPE:
1529 return ("Pipe error");
1530 case LIBUSB_ERROR_INTERRUPTED:
1531 return ("Interrupted");
1532 case LIBUSB_ERROR_NO_MEM:
1533 return ("Out of memory");
1534 case LIBUSB_ERROR_NOT_SUPPORTED:
1535 return ("Not supported");
1536 case LIBUSB_ERROR_OTHER:
1537 return ("Other error");
1539 return ("Unknown error");
1544 libusb_error_name(int code)
1547 case LIBUSB_SUCCESS:
1548 return ("LIBUSB_SUCCESS");
1549 case LIBUSB_ERROR_IO:
1550 return ("LIBUSB_ERROR_IO");
1551 case LIBUSB_ERROR_INVALID_PARAM:
1552 return ("LIBUSB_ERROR_INVALID_PARAM");
1553 case LIBUSB_ERROR_ACCESS:
1554 return ("LIBUSB_ERROR_ACCESS");
1555 case LIBUSB_ERROR_NO_DEVICE:
1556 return ("LIBUSB_ERROR_NO_DEVICE");
1557 case LIBUSB_ERROR_NOT_FOUND:
1558 return ("LIBUSB_ERROR_NOT_FOUND");
1559 case LIBUSB_ERROR_BUSY:
1560 return ("LIBUSB_ERROR_BUSY");
1561 case LIBUSB_ERROR_TIMEOUT:
1562 return ("LIBUSB_ERROR_TIMEOUT");
1563 case LIBUSB_ERROR_OVERFLOW:
1564 return ("LIBUSB_ERROR_OVERFLOW");
1565 case LIBUSB_ERROR_PIPE:
1566 return ("LIBUSB_ERROR_PIPE");
1567 case LIBUSB_ERROR_INTERRUPTED:
1568 return ("LIBUSB_ERROR_INTERRUPTED");
1569 case LIBUSB_ERROR_NO_MEM:
1570 return ("LIBUSB_ERROR_NO_MEM");
1571 case LIBUSB_ERROR_NOT_SUPPORTED:
1572 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1573 case LIBUSB_ERROR_OTHER:
1574 return ("LIBUSB_ERROR_OTHER");
1576 return ("LIBUSB_ERROR_UNKNOWN");