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_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
290 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
294 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
297 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
301 libusb_get_device_address(libusb_device *dev)
304 return (0); /* should not happen */
305 return (libusb20_dev_get_address(dev->os_priv));
309 libusb_get_device_speed(libusb_device *dev)
312 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
314 switch (libusb20_dev_get_speed(dev->os_priv)) {
315 case LIBUSB20_SPEED_LOW:
316 return (LIBUSB_SPEED_LOW);
317 case LIBUSB20_SPEED_FULL:
318 return (LIBUSB_SPEED_FULL);
319 case LIBUSB20_SPEED_HIGH:
320 return (LIBUSB_SPEED_HIGH);
321 case LIBUSB20_SPEED_SUPER:
322 return (LIBUSB_SPEED_SUPER);
326 return (LIBUSB_SPEED_UNKNOWN);
330 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
332 struct libusb_config_descriptor *pdconf;
333 struct libusb_interface *pinf;
334 struct libusb_interface_descriptor *pdinf;
335 struct libusb_endpoint_descriptor *pdend;
342 return (LIBUSB_ERROR_NO_DEVICE);
344 ret = libusb_get_active_config_descriptor(dev, &pdconf);
348 ret = LIBUSB_ERROR_NOT_FOUND;
349 for (i = 0; i < pdconf->bNumInterfaces; i++) {
350 pinf = &pdconf->interface[i];
351 for (j = 0; j < pinf->num_altsetting; j++) {
352 pdinf = &pinf->altsetting[j];
353 for (k = 0; k < pdinf->bNumEndpoints; k++) {
354 pdend = &pdinf->endpoint[k];
355 if (pdend->bEndpointAddress == endpoint) {
356 ret = pdend->wMaxPacketSize;
364 libusb_free_config_descriptor(pdconf);
369 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
374 ret = libusb_get_max_packet_size(dev, endpoint);
376 switch (libusb20_dev_get_speed(dev->os_priv)) {
377 case LIBUSB20_SPEED_LOW:
378 case LIBUSB20_SPEED_FULL:
382 multiplier = (1 + ((ret >> 11) & 3));
385 ret = (ret & 0x7FF) * multiplier;
393 libusb_ref_device(libusb_device *dev)
396 return (NULL); /* be NULL safe */
400 CTX_UNLOCK(dev->ctx);
406 libusb_unref_device(libusb_device *dev)
409 return; /* be NULL safe */
413 CTX_UNLOCK(dev->ctx);
415 if (dev->refcnt == 0) {
416 libusb20_dev_free(dev->os_priv);
422 libusb_open(libusb_device *dev, libusb_device_handle **devh)
424 libusb_context *ctx = dev->ctx;
425 struct libusb20_device *pdev = dev->os_priv;
430 return (LIBUSB_ERROR_INVALID_PARAM);
432 /* set default device handle value */
435 dev = libusb_ref_device(dev);
437 return (LIBUSB_ERROR_INVALID_PARAM);
439 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
441 libusb_unref_device(dev);
442 return (LIBUSB_ERROR_NO_MEM);
444 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
445 POLLOUT | POLLRDNORM | POLLWRNORM);
447 /* make sure our event loop detects the new device */
449 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
450 if (err < (int)sizeof(dummy)) {
451 /* ignore error, if any */
452 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
459 libusb_device_handle *
460 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
463 struct libusb_device **devs;
464 struct libusb20_device *pdev;
465 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
469 ctx = GET_CONTEXT(ctx);
471 return (NULL); /* be NULL safe */
473 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
475 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
479 for (j = 0; j < i; j++) {
480 struct libusb20_device *tdev;
482 tdev = devs[j]->os_priv;
483 pdesc = libusb20_dev_get_device_desc(tdev);
485 * NOTE: The USB library will automatically swap the
486 * fields in the device descriptor to be of host
489 if (pdesc->idVendor == vendor_id &&
490 pdesc->idProduct == product_id) {
491 libusb_open(devs[j], &pdev);
496 libusb_free_device_list(devs, 1);
497 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
502 libusb_close(struct libusb20_device *pdev)
505 struct libusb_device *dev;
510 return; /* be NULL safe */
512 dev = libusb_get_device(pdev);
515 libusb10_remove_pollfd(ctx, &dev->dev_poll);
517 libusb20_dev_close(pdev);
519 /* unref will free the "pdev" when the refcount reaches zero */
520 libusb_unref_device(dev);
522 /* make sure our event loop detects the closed device */
524 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
525 if (err < (int)sizeof(dummy)) {
526 /* ignore error, if any */
527 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
532 libusb_get_device(struct libusb20_device *pdev)
536 return ((libusb_device *)pdev->privLuData);
540 libusb_get_configuration(struct libusb20_device *pdev, int *config)
542 struct libusb20_config *pconf;
544 if (pdev == NULL || config == NULL)
545 return (LIBUSB_ERROR_INVALID_PARAM);
547 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
549 return (LIBUSB_ERROR_NO_MEM);
551 *config = pconf->desc.bConfigurationValue;
559 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
561 struct libusb20_config *pconf;
562 struct libusb_device *dev;
566 dev = libusb_get_device(pdev);
568 return (LIBUSB_ERROR_INVALID_PARAM);
570 if (configuration < 1) {
574 for (i = 0; i != 255; i++) {
577 pconf = libusb20_dev_alloc_config(pdev, i);
579 return (LIBUSB_ERROR_INVALID_PARAM);
580 found = (pconf->desc.bConfigurationValue
587 return (LIBUSB_ERROR_INVALID_PARAM);
592 libusb10_cancel_all_transfer(dev);
594 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
596 err = libusb20_dev_set_config_index(pdev, i);
598 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
599 POLLOUT | POLLRDNORM | POLLWRNORM);
601 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
605 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
609 dev = libusb_get_device(pdev);
611 return (LIBUSB_ERROR_INVALID_PARAM);
613 if (interface_number < 0 || interface_number > 31)
614 return (LIBUSB_ERROR_INVALID_PARAM);
617 dev->claimed_interfaces |= (1 << interface_number);
618 CTX_UNLOCK(dev->ctx);
624 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
629 dev = libusb_get_device(pdev);
631 return (LIBUSB_ERROR_INVALID_PARAM);
633 if (interface_number < 0 || interface_number > 31)
634 return (LIBUSB_ERROR_INVALID_PARAM);
637 if (!(dev->claimed_interfaces & (1 << interface_number)))
638 err = LIBUSB_ERROR_NOT_FOUND;
641 dev->claimed_interfaces &= ~(1 << interface_number);
642 CTX_UNLOCK(dev->ctx);
647 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
648 int interface_number, int alternate_setting)
653 dev = libusb_get_device(pdev);
655 return (LIBUSB_ERROR_INVALID_PARAM);
657 if (interface_number < 0 || interface_number > 31)
658 return (LIBUSB_ERROR_INVALID_PARAM);
661 if (!(dev->claimed_interfaces & (1 << interface_number)))
662 err = LIBUSB_ERROR_NOT_FOUND;
663 CTX_UNLOCK(dev->ctx);
668 libusb10_cancel_all_transfer(dev);
670 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
672 err = libusb20_dev_set_alt_index(pdev,
673 interface_number, alternate_setting);
675 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
676 pdev, libusb20_dev_get_fd(pdev),
677 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
679 return (err ? LIBUSB_ERROR_OTHER : 0);
682 static struct libusb20_transfer *
683 libusb10_get_transfer(struct libusb20_device *pdev,
684 uint8_t endpoint, uint8_t xfer_index)
686 xfer_index &= 1; /* double buffering */
688 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
690 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
691 /* this is an IN endpoint */
694 return (libusb20_tr_get_pointer(pdev, xfer_index));
698 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
700 struct libusb20_transfer *xfer;
701 struct libusb_device *dev;
704 xfer = libusb10_get_transfer(pdev, endpoint, 0);
706 return (LIBUSB_ERROR_INVALID_PARAM);
708 dev = libusb_get_device(pdev);
710 return (LIBUSB_ERROR_INVALID_PARAM);
713 err = libusb20_tr_open(xfer, 0, 1, endpoint);
714 CTX_UNLOCK(dev->ctx);
716 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
717 return (LIBUSB_ERROR_OTHER);
719 libusb20_tr_clear_stall_sync(xfer);
721 /* check if we opened the transfer */
724 libusb20_tr_close(xfer);
725 CTX_UNLOCK(dev->ctx);
727 return (0); /* success */
731 libusb_reset_device(struct libusb20_device *pdev)
736 dev = libusb_get_device(pdev);
738 return (LIBUSB_ERROR_INVALID_PARAM);
740 libusb10_cancel_all_transfer(dev);
742 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
744 err = libusb20_dev_reset(pdev);
746 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
747 pdev, libusb20_dev_get_fd(pdev),
748 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
750 return (err ? LIBUSB_ERROR_OTHER : 0);
754 libusb_check_connected(struct libusb20_device *pdev)
759 dev = libusb_get_device(pdev);
761 return (LIBUSB_ERROR_INVALID_PARAM);
763 err = libusb20_dev_check_connected(pdev);
765 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
769 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
772 return (LIBUSB_ERROR_INVALID_PARAM);
774 if (libusb20_dev_kernel_driver_active(pdev, interface))
775 return (0); /* no kernel driver is active */
777 return (1); /* kernel driver is active */
781 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
782 char *name, int namelen)
784 return (libusb_get_driver(pdev, interface, name, namelen));
788 libusb_get_driver(struct libusb20_device *pdev, int interface,
789 char *name, int namelen)
795 return (LIBUSB_ERROR_INVALID_PARAM);
797 return (LIBUSB_ERROR_INVALID_PARAM);
801 err = libusb20_dev_get_iface_desc(
802 pdev, interface, name, namelen);
805 return (LIBUSB_ERROR_OTHER);
807 /* we only want the driver name */
808 ptr = strstr(name, ":");
816 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
818 return (libusb_detach_kernel_driver(pdev, interface));
822 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
827 return (LIBUSB_ERROR_INVALID_PARAM);
829 err = libusb20_dev_detach_kernel_driver(
832 return (err ? LIBUSB_ERROR_OTHER : 0);
836 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
839 return (LIBUSB_ERROR_INVALID_PARAM);
840 /* stub - currently not supported by libusb20 */
844 /* Asynchronous device I/O */
846 struct libusb_transfer *
847 libusb_alloc_transfer(int iso_packets)
849 struct libusb_transfer *uxfer;
850 struct libusb_super_transfer *sxfer;
853 len = sizeof(struct libusb_transfer) +
854 sizeof(struct libusb_super_transfer) +
855 (iso_packets * sizeof(libusb_iso_packet_descriptor));
861 memset(sxfer, 0, len);
863 uxfer = (struct libusb_transfer *)(
864 ((uint8_t *)sxfer) + sizeof(*sxfer));
866 /* set default value */
867 uxfer->num_iso_packets = iso_packets;
873 libusb_free_transfer(struct libusb_transfer *uxfer)
875 struct libusb_super_transfer *sxfer;
878 return; /* be NULL safe */
880 /* check if we should free the transfer buffer */
881 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
884 sxfer = (struct libusb_super_transfer *)(
885 (uint8_t *)uxfer - sizeof(*sxfer));
891 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
895 switch (xfer->type) {
896 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
897 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
899 case LIBUSB_TRANSFER_TYPE_CONTROL:
910 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
915 usb_speed = libusb20_dev_get_speed(pdev);
917 switch (xfer->type) {
918 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
919 ret = 0; /* kernel will auto-select */
921 case LIBUSB_TRANSFER_TYPE_CONTROL:
926 case LIBUSB20_SPEED_LOW:
929 case LIBUSB20_SPEED_FULL:
932 case LIBUSB20_SPEED_SUPER:
945 libusb10_convert_error(uint8_t status)
950 case LIBUSB20_TRANSFER_START:
951 case LIBUSB20_TRANSFER_COMPLETED:
952 return (LIBUSB_TRANSFER_COMPLETED);
953 case LIBUSB20_TRANSFER_OVERFLOW:
954 return (LIBUSB_TRANSFER_OVERFLOW);
955 case LIBUSB20_TRANSFER_NO_DEVICE:
956 return (LIBUSB_TRANSFER_NO_DEVICE);
957 case LIBUSB20_TRANSFER_STALL:
958 return (LIBUSB_TRANSFER_STALL);
959 case LIBUSB20_TRANSFER_CANCELLED:
960 return (LIBUSB_TRANSFER_CANCELLED);
961 case LIBUSB20_TRANSFER_TIMED_OUT:
962 return (LIBUSB_TRANSFER_TIMED_OUT);
964 return (LIBUSB_TRANSFER_ERROR);
968 /* This function must be called locked */
971 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
972 struct libusb_super_transfer *sxfer, int status)
974 struct libusb_transfer *uxfer;
975 struct libusb_device *dev;
977 uxfer = (struct libusb_transfer *)(
978 ((uint8_t *)sxfer) + sizeof(*sxfer));
981 libusb20_tr_set_priv_sc1(pxfer, NULL);
983 /* set transfer status */
984 uxfer->status = status;
986 /* update super transfer state */
987 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
989 dev = libusb_get_device(uxfer->dev_handle);
991 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
994 /* This function must be called locked */
997 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
999 struct libusb_super_transfer *sxfer;
1000 struct libusb_transfer *uxfer;
1002 uint16_t iso_packets;
1007 status = libusb20_tr_get_status(pxfer);
1008 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1009 actlen = libusb20_tr_get_actual_length(pxfer);
1010 iso_packets = libusb20_tr_get_max_frames(pxfer);
1013 return; /* cancelled - nothing to do */
1015 uxfer = (struct libusb_transfer *)(
1016 ((uint8_t *)sxfer) + sizeof(*sxfer));
1018 if (iso_packets > uxfer->num_iso_packets)
1019 iso_packets = uxfer->num_iso_packets;
1021 if (iso_packets == 0)
1022 return; /* nothing to do */
1024 /* make sure that the number of ISOCHRONOUS packets is valid */
1025 uxfer->num_iso_packets = iso_packets;
1027 flags = uxfer->flags;
1030 case LIBUSB20_TRANSFER_COMPLETED:
1032 /* update actual length */
1033 uxfer->actual_length = actlen;
1034 for (i = 0; i != iso_packets; i++) {
1035 uxfer->iso_packet_desc[i].actual_length =
1036 libusb20_tr_get_length(pxfer, i);
1038 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1041 case LIBUSB20_TRANSFER_START:
1043 /* setup length(s) */
1045 for (i = 0; i != iso_packets; i++) {
1046 libusb20_tr_setup_isoc(pxfer,
1047 &uxfer->buffer[actlen],
1048 uxfer->iso_packet_desc[i].length, i);
1049 actlen += uxfer->iso_packet_desc[i].length;
1055 libusb20_tr_set_total_frames(pxfer, iso_packets);
1056 libusb20_tr_submit(pxfer);
1058 /* fork another USB transfer, if any */
1059 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1063 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1068 /* This function must be called locked */
1071 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1073 struct libusb_super_transfer *sxfer;
1074 struct libusb_transfer *uxfer;
1080 status = libusb20_tr_get_status(pxfer);
1081 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1082 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1083 actlen = libusb20_tr_get_actual_length(pxfer);
1086 return; /* cancelled - nothing to do */
1088 uxfer = (struct libusb_transfer *)(
1089 ((uint8_t *)sxfer) + sizeof(*sxfer));
1091 flags = uxfer->flags;
1094 case LIBUSB20_TRANSFER_COMPLETED:
1096 uxfer->actual_length += actlen;
1098 /* check for short packet */
1099 if (sxfer->last_len != actlen) {
1100 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1101 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1103 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1107 /* check for end of data */
1108 if (sxfer->rem_len == 0) {
1109 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1114 case LIBUSB20_TRANSFER_START:
1115 if (max_bulk > sxfer->rem_len) {
1116 max_bulk = sxfer->rem_len;
1118 /* setup new BULK or INTERRUPT transaction */
1119 libusb20_tr_setup_bulk(pxfer,
1120 sxfer->curr_data, max_bulk, uxfer->timeout);
1122 /* update counters */
1123 sxfer->last_len = max_bulk;
1124 sxfer->curr_data += max_bulk;
1125 sxfer->rem_len -= max_bulk;
1127 libusb20_tr_submit(pxfer);
1129 /* check if we can fork another USB transfer */
1130 if (sxfer->rem_len == 0)
1131 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1135 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1140 /* This function must be called locked */
1143 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1145 struct libusb_super_transfer *sxfer;
1146 struct libusb_transfer *uxfer;
1152 status = libusb20_tr_get_status(pxfer);
1153 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1154 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1155 actlen = libusb20_tr_get_actual_length(pxfer);
1158 return; /* cancelled - nothing to do */
1160 uxfer = (struct libusb_transfer *)(
1161 ((uint8_t *)sxfer) + sizeof(*sxfer));
1163 flags = uxfer->flags;
1166 case LIBUSB20_TRANSFER_COMPLETED:
1168 uxfer->actual_length += actlen;
1170 /* subtract length of SETUP packet, if any */
1171 actlen -= libusb20_tr_get_length(pxfer, 0);
1173 /* check for short packet */
1174 if (sxfer->last_len != actlen) {
1175 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1176 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1178 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1182 /* check for end of data */
1183 if (sxfer->rem_len == 0) {
1184 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1189 case LIBUSB20_TRANSFER_START:
1190 if (max_bulk > sxfer->rem_len) {
1191 max_bulk = sxfer->rem_len;
1193 /* setup new CONTROL transaction */
1194 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1195 /* next fragment - don't send SETUP packet */
1196 libusb20_tr_set_length(pxfer, 0, 0);
1198 /* first fragment - send SETUP packet */
1199 libusb20_tr_set_length(pxfer, 8, 0);
1200 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1203 if (max_bulk != 0) {
1204 libusb20_tr_set_length(pxfer, max_bulk, 1);
1205 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1206 libusb20_tr_set_total_frames(pxfer, 2);
1208 libusb20_tr_set_total_frames(pxfer, 1);
1211 /* update counters */
1212 sxfer->last_len = max_bulk;
1213 sxfer->curr_data += max_bulk;
1214 sxfer->rem_len -= max_bulk;
1216 libusb20_tr_submit(pxfer);
1218 /* check if we can fork another USB transfer */
1219 if (sxfer->rem_len == 0)
1220 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1224 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1229 /* The following function must be called locked */
1232 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1234 struct libusb20_transfer *pxfer0;
1235 struct libusb20_transfer *pxfer1;
1236 struct libusb_super_transfer *sxfer;
1237 struct libusb_transfer *uxfer;
1238 struct libusb_device *dev;
1245 dev = libusb_get_device(pdev);
1247 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1248 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1250 if (pxfer0 == NULL || pxfer1 == NULL)
1251 return; /* shouldn't happen */
1254 if (libusb20_tr_pending(pxfer0))
1256 if (libusb20_tr_pending(pxfer1))
1261 /* wait till one of the transfers complete */
1264 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1266 return; /* cancelling */
1268 return; /* cannot queue another one */
1269 /* swap transfers */
1273 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1275 return; /* cancelling */
1277 return; /* cannot queue another one */
1278 /* swap transfers */
1285 /* find next transfer on same endpoint */
1286 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1288 uxfer = (struct libusb_transfer *)(
1289 ((uint8_t *)sxfer) + sizeof(*sxfer));
1291 if (uxfer->endpoint == endpoint) {
1292 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1293 sxfer->entry.tqe_prev = NULL;
1297 return; /* success */
1301 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1302 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1304 /* reset super transfer state */
1305 sxfer->rem_len = uxfer->length;
1306 sxfer->curr_data = uxfer->buffer;
1307 uxfer->actual_length = 0;
1309 switch (uxfer->type) {
1310 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1311 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1313 case LIBUSB_TRANSFER_TYPE_BULK:
1314 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1315 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1317 case LIBUSB_TRANSFER_TYPE_CONTROL:
1318 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1319 if (sxfer->rem_len < 8)
1322 /* remove SETUP packet from data */
1323 sxfer->rem_len -= 8;
1324 sxfer->curr_data += 8;
1330 buffsize = libusb10_get_buffsize(pdev, uxfer);
1331 maxframe = libusb10_get_maxframe(pdev, uxfer);
1333 /* make sure the transfer is opened */
1334 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1335 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1338 libusb20_tr_start(pxfer0);
1342 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1344 /* make sure our event loop spins the done handler */
1346 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1349 /* The following function must be called unlocked */
1352 libusb_submit_transfer(struct libusb_transfer *uxfer)
1354 struct libusb20_transfer *pxfer0;
1355 struct libusb20_transfer *pxfer1;
1356 struct libusb_super_transfer *sxfer;
1357 struct libusb_device *dev;
1362 return (LIBUSB_ERROR_INVALID_PARAM);
1364 if (uxfer->dev_handle == NULL)
1365 return (LIBUSB_ERROR_INVALID_PARAM);
1367 endpoint = uxfer->endpoint;
1370 return (LIBUSB_ERROR_INVALID_PARAM);
1372 dev = libusb_get_device(uxfer->dev_handle);
1374 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1376 sxfer = (struct libusb_super_transfer *)(
1377 (uint8_t *)uxfer - sizeof(*sxfer));
1381 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1382 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1384 if (pxfer0 == NULL || pxfer1 == NULL) {
1385 err = LIBUSB_ERROR_OTHER;
1386 } else if ((sxfer->entry.tqe_prev != NULL) ||
1387 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1388 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1389 err = LIBUSB_ERROR_BUSY;
1392 /* set pending state */
1393 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1395 /* insert transfer into transfer head list */
1396 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1398 /* start work transfers */
1399 libusb10_submit_transfer_sub(
1400 uxfer->dev_handle, endpoint);
1402 err = 0; /* success */
1405 CTX_UNLOCK(dev->ctx);
1407 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1412 /* Asynchronous transfer cancel */
1415 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1417 struct libusb20_transfer *pxfer0;
1418 struct libusb20_transfer *pxfer1;
1419 struct libusb_super_transfer *sxfer;
1420 struct libusb_device *dev;
1425 return (LIBUSB_ERROR_INVALID_PARAM);
1427 /* check if not initialised */
1428 if (uxfer->dev_handle == NULL)
1429 return (LIBUSB_ERROR_NOT_FOUND);
1431 endpoint = uxfer->endpoint;
1434 return (LIBUSB_ERROR_INVALID_PARAM);
1436 dev = libusb_get_device(uxfer->dev_handle);
1438 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1440 sxfer = (struct libusb_super_transfer *)(
1441 (uint8_t *)uxfer - sizeof(*sxfer));
1447 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1448 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1450 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1451 /* only update the transfer status */
1452 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1453 retval = LIBUSB_ERROR_NOT_FOUND;
1454 } else if (sxfer->entry.tqe_prev != NULL) {
1455 /* we are lucky - transfer is on a queue */
1456 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1457 sxfer->entry.tqe_prev = NULL;
1458 libusb10_complete_transfer(NULL,
1459 sxfer, LIBUSB_TRANSFER_CANCELLED);
1460 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1462 retval = LIBUSB_ERROR_NOT_FOUND;
1463 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1464 libusb10_complete_transfer(pxfer0,
1465 sxfer, LIBUSB_TRANSFER_CANCELLED);
1466 libusb20_tr_stop(pxfer0);
1467 /* make sure the queue doesn't stall */
1468 libusb10_submit_transfer_sub(
1469 uxfer->dev_handle, endpoint);
1470 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1471 libusb10_complete_transfer(pxfer1,
1472 sxfer, LIBUSB_TRANSFER_CANCELLED);
1473 libusb20_tr_stop(pxfer1);
1474 /* make sure the queue doesn't stall */
1475 libusb10_submit_transfer_sub(
1476 uxfer->dev_handle, endpoint);
1479 retval = LIBUSB_ERROR_NOT_FOUND;
1482 CTX_UNLOCK(dev->ctx);
1484 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1490 libusb10_cancel_all_transfer(libusb_device *dev)
1496 libusb_cpu_to_le16(uint16_t x)
1498 return (htole16(x));
1502 libusb_le16_to_cpu(uint16_t x)
1504 return (le16toh(x));
1508 libusb_strerror(int code)
1511 case LIBUSB_SUCCESS:
1513 case LIBUSB_ERROR_IO:
1514 return ("I/O error");
1515 case LIBUSB_ERROR_INVALID_PARAM:
1516 return ("Invalid parameter");
1517 case LIBUSB_ERROR_ACCESS:
1518 return ("Permissions error");
1519 case LIBUSB_ERROR_NO_DEVICE:
1520 return ("No device");
1521 case LIBUSB_ERROR_NOT_FOUND:
1522 return ("Not found");
1523 case LIBUSB_ERROR_BUSY:
1524 return ("Device busy");
1525 case LIBUSB_ERROR_TIMEOUT:
1527 case LIBUSB_ERROR_OVERFLOW:
1528 return ("Overflow");
1529 case LIBUSB_ERROR_PIPE:
1530 return ("Pipe error");
1531 case LIBUSB_ERROR_INTERRUPTED:
1532 return ("Interrupted");
1533 case LIBUSB_ERROR_NO_MEM:
1534 return ("Out of memory");
1535 case LIBUSB_ERROR_NOT_SUPPORTED:
1536 return ("Not supported");
1537 case LIBUSB_ERROR_OTHER:
1538 return ("Other error");
1540 return ("Unknown error");
1545 libusb_error_name(int code)
1548 case LIBUSB_SUCCESS:
1549 return ("LIBUSB_SUCCESS");
1550 case LIBUSB_ERROR_IO:
1551 return ("LIBUSB_ERROR_IO");
1552 case LIBUSB_ERROR_INVALID_PARAM:
1553 return ("LIBUSB_ERROR_INVALID_PARAM");
1554 case LIBUSB_ERROR_ACCESS:
1555 return ("LIBUSB_ERROR_ACCESS");
1556 case LIBUSB_ERROR_NO_DEVICE:
1557 return ("LIBUSB_ERROR_NO_DEVICE");
1558 case LIBUSB_ERROR_NOT_FOUND:
1559 return ("LIBUSB_ERROR_NOT_FOUND");
1560 case LIBUSB_ERROR_BUSY:
1561 return ("LIBUSB_ERROR_BUSY");
1562 case LIBUSB_ERROR_TIMEOUT:
1563 return ("LIBUSB_ERROR_TIMEOUT");
1564 case LIBUSB_ERROR_OVERFLOW:
1565 return ("LIBUSB_ERROR_OVERFLOW");
1566 case LIBUSB_ERROR_PIPE:
1567 return ("LIBUSB_ERROR_PIPE");
1568 case LIBUSB_ERROR_INTERRUPTED:
1569 return ("LIBUSB_ERROR_INTERRUPTED");
1570 case LIBUSB_ERROR_NO_MEM:
1571 return ("LIBUSB_ERROR_NO_MEM");
1572 case LIBUSB_ERROR_NOT_SUPPORTED:
1573 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1574 case LIBUSB_ERROR_OTHER:
1575 return ("LIBUSB_ERROR_OTHER");
1577 return ("LIBUSB_ERROR_UNKNOWN");