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 */
66 static const struct libusb_version libusb_version = {
72 .describe = "http://www.freebsd.org"
75 const struct libusb_version *
76 libusb_get_version(void)
79 return (&libusb_version);
83 libusb_set_debug(libusb_context *ctx, int level)
85 ctx = GET_CONTEXT(ctx);
91 libusb_set_nonblocking(int f)
96 * We ignore any failures in this function, hence the
97 * non-blocking flag is not critical to the operation of
98 * libUSB. We use F_GETFL and F_SETFL to be compatible with
102 flags = fcntl(f, F_GETFL, NULL);
106 fcntl(f, F_SETFL, flags);
110 libusb10_wakeup_event_loop(libusb_context *ctx)
115 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
116 if (err < (int)sizeof(dummy)) {
117 /* ignore error, if any */
118 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
123 libusb_init(libusb_context **context)
125 struct libusb_context *ctx;
126 pthread_condattr_t attr;
130 ctx = malloc(sizeof(*ctx));
132 return (LIBUSB_ERROR_INVALID_PARAM);
134 memset(ctx, 0, sizeof(*ctx));
136 debug = getenv("LIBUSB_DEBUG");
138 ctx->debug = atoi(debug);
140 ctx->debug_fixed = 1;
142 TAILQ_INIT(&ctx->pollfds);
143 TAILQ_INIT(&ctx->tr_done);
144 TAILQ_INIT(&ctx->hotplug_cbh);
145 TAILQ_INIT(&ctx->hotplug_devs);
147 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
149 return (LIBUSB_ERROR_NO_MEM);
151 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
152 pthread_mutex_destroy(&ctx->ctx_lock);
154 return (LIBUSB_ERROR_NO_MEM);
156 if (pthread_condattr_init(&attr) != 0) {
157 pthread_mutex_destroy(&ctx->ctx_lock);
158 pthread_mutex_destroy(&ctx->hotplug_lock);
160 return (LIBUSB_ERROR_NO_MEM);
162 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
163 pthread_mutex_destroy(&ctx->ctx_lock);
164 pthread_mutex_destroy(&ctx->hotplug_lock);
165 pthread_condattr_destroy(&attr);
167 return (LIBUSB_ERROR_OTHER);
169 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
170 pthread_mutex_destroy(&ctx->ctx_lock);
171 pthread_mutex_destroy(&ctx->hotplug_lock);
172 pthread_condattr_destroy(&attr);
174 return (LIBUSB_ERROR_NO_MEM);
176 pthread_condattr_destroy(&attr);
178 ctx->ctx_handler = NO_THREAD;
179 ctx->hotplug_handler = NO_THREAD;
181 ret = pipe(ctx->ctrl_pipe);
183 pthread_mutex_destroy(&ctx->ctx_lock);
184 pthread_mutex_destroy(&ctx->hotplug_lock);
185 pthread_cond_destroy(&ctx->ctx_cond);
187 return (LIBUSB_ERROR_OTHER);
189 /* set non-blocking mode on the control pipe to avoid deadlock */
190 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
191 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
193 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
195 pthread_mutex_lock(&default_context_lock);
196 if (usbi_default_context == NULL) {
197 usbi_default_context = ctx;
199 pthread_mutex_unlock(&default_context_lock);
204 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
210 libusb_exit(libusb_context *ctx)
212 ctx = GET_CONTEXT(ctx);
217 /* stop hotplug thread, if any */
219 if (ctx->hotplug_handler != NO_THREAD) {
224 td = ctx->hotplug_handler;
225 ctx->hotplug_handler = NO_THREAD;
228 pthread_join(td, &ptr);
231 /* XXX cleanup devices */
233 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
234 close(ctx->ctrl_pipe[0]);
235 close(ctx->ctrl_pipe[1]);
236 pthread_mutex_destroy(&ctx->ctx_lock);
237 pthread_mutex_destroy(&ctx->hotplug_lock);
238 pthread_cond_destroy(&ctx->ctx_cond);
240 pthread_mutex_lock(&default_context_lock);
241 if (ctx == usbi_default_context) {
242 usbi_default_context = NULL;
244 pthread_mutex_unlock(&default_context_lock);
249 /* Device handling and initialisation. */
252 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
254 struct libusb20_backend *usb_backend;
255 struct libusb20_device *pdev;
256 struct libusb_device *dev;
259 ctx = GET_CONTEXT(ctx);
262 return (LIBUSB_ERROR_INVALID_PARAM);
265 return (LIBUSB_ERROR_INVALID_PARAM);
267 usb_backend = libusb20_be_alloc_default();
268 if (usb_backend == NULL)
269 return (LIBUSB_ERROR_NO_MEM);
271 /* figure out how many USB devices are present */
274 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
277 /* allocate device pointer list */
278 *list = malloc((i + 1) * sizeof(void *));
280 libusb20_be_free(usb_backend);
281 return (LIBUSB_ERROR_NO_MEM);
283 /* create libusb v1.0 compliant devices */
285 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
287 dev = malloc(sizeof(*dev));
290 libusb_unref_device((*list)[i - 1]);
295 libusb20_be_free(usb_backend);
296 return (LIBUSB_ERROR_NO_MEM);
298 /* get device into libUSB v1.0 list */
299 libusb20_be_dequeue_device(usb_backend, pdev);
301 memset(dev, 0, sizeof(*dev));
303 /* init transfer queues */
304 TAILQ_INIT(&dev->tr_head);
306 /* set context we belong to */
309 /* link together the two structures */
311 pdev->privLuData = dev;
313 (*list)[i] = libusb_ref_device(dev);
318 libusb20_be_free(usb_backend);
323 libusb_free_device_list(libusb_device **list, int unref_devices)
328 return; /* be NULL safe */
331 for (i = 0; list[i] != NULL; i++)
332 libusb_unref_device(list[i]);
338 libusb_get_bus_number(libusb_device *dev)
341 return (0); /* should not happen */
342 return (libusb20_dev_get_bus_number(dev->os_priv));
346 libusb_get_port_number(libusb_device *dev)
349 return (0); /* should not happen */
350 return (libusb20_dev_get_parent_port(dev->os_priv));
354 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
356 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
360 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
363 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
367 libusb_get_device_address(libusb_device *dev)
370 return (0); /* should not happen */
371 return (libusb20_dev_get_address(dev->os_priv));
375 libusb_get_device_speed(libusb_device *dev)
378 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
380 switch (libusb20_dev_get_speed(dev->os_priv)) {
381 case LIBUSB20_SPEED_LOW:
382 return (LIBUSB_SPEED_LOW);
383 case LIBUSB20_SPEED_FULL:
384 return (LIBUSB_SPEED_FULL);
385 case LIBUSB20_SPEED_HIGH:
386 return (LIBUSB_SPEED_HIGH);
387 case LIBUSB20_SPEED_SUPER:
388 return (LIBUSB_SPEED_SUPER);
392 return (LIBUSB_SPEED_UNKNOWN);
396 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
398 struct libusb_config_descriptor *pdconf;
399 struct libusb_interface *pinf;
400 struct libusb_interface_descriptor *pdinf;
401 struct libusb_endpoint_descriptor *pdend;
408 return (LIBUSB_ERROR_NO_DEVICE);
410 ret = libusb_get_active_config_descriptor(dev, &pdconf);
414 ret = LIBUSB_ERROR_NOT_FOUND;
415 for (i = 0; i < pdconf->bNumInterfaces; i++) {
416 pinf = &pdconf->interface[i];
417 for (j = 0; j < pinf->num_altsetting; j++) {
418 pdinf = &pinf->altsetting[j];
419 for (k = 0; k < pdinf->bNumEndpoints; k++) {
420 pdend = &pdinf->endpoint[k];
421 if (pdend->bEndpointAddress == endpoint) {
422 ret = pdend->wMaxPacketSize;
430 libusb_free_config_descriptor(pdconf);
435 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
440 ret = libusb_get_max_packet_size(dev, endpoint);
442 switch (libusb20_dev_get_speed(dev->os_priv)) {
443 case LIBUSB20_SPEED_LOW:
444 case LIBUSB20_SPEED_FULL:
448 multiplier = (1 + ((ret >> 11) & 3));
451 ret = (ret & 0x7FF) * multiplier;
459 libusb_ref_device(libusb_device *dev)
462 return (NULL); /* be NULL safe */
466 CTX_UNLOCK(dev->ctx);
472 libusb_unref_device(libusb_device *dev)
475 return; /* be NULL safe */
479 CTX_UNLOCK(dev->ctx);
481 if (dev->refcnt == 0) {
482 libusb20_dev_free(dev->os_priv);
488 libusb_open(libusb_device *dev, libusb_device_handle **devh)
490 libusb_context *ctx = dev->ctx;
491 struct libusb20_device *pdev = dev->os_priv;
495 return (LIBUSB_ERROR_INVALID_PARAM);
497 /* set default device handle value */
500 dev = libusb_ref_device(dev);
502 return (LIBUSB_ERROR_INVALID_PARAM);
504 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
506 libusb_unref_device(dev);
507 return (LIBUSB_ERROR_NO_MEM);
509 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
510 POLLOUT | POLLRDNORM | POLLWRNORM);
512 /* make sure our event loop detects the new device */
513 libusb10_wakeup_event_loop(ctx);
520 libusb_device_handle *
521 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
524 struct libusb_device **devs;
525 struct libusb20_device *pdev;
526 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
530 ctx = GET_CONTEXT(ctx);
532 return (NULL); /* be NULL safe */
534 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
536 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
540 for (j = 0; j < i; j++) {
541 struct libusb20_device *tdev;
543 tdev = devs[j]->os_priv;
544 pdesc = libusb20_dev_get_device_desc(tdev);
546 * NOTE: The USB library will automatically swap the
547 * fields in the device descriptor to be of host
550 if (pdesc->idVendor == vendor_id &&
551 pdesc->idProduct == product_id) {
552 libusb_open(devs[j], &pdev);
557 libusb_free_device_list(devs, 1);
558 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
563 libusb_close(struct libusb20_device *pdev)
566 struct libusb_device *dev;
569 return; /* be NULL safe */
571 dev = libusb_get_device(pdev);
574 libusb10_remove_pollfd(ctx, &dev->dev_poll);
576 libusb20_dev_close(pdev);
578 /* unref will free the "pdev" when the refcount reaches zero */
579 libusb_unref_device(dev);
581 /* make sure our event loop detects the closed device */
582 libusb10_wakeup_event_loop(ctx);
586 libusb_get_device(struct libusb20_device *pdev)
590 return ((libusb_device *)pdev->privLuData);
594 libusb_get_configuration(struct libusb20_device *pdev, int *config)
596 struct libusb20_config *pconf;
598 if (pdev == NULL || config == NULL)
599 return (LIBUSB_ERROR_INVALID_PARAM);
601 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
603 return (LIBUSB_ERROR_NO_MEM);
605 *config = pconf->desc.bConfigurationValue;
613 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
615 struct libusb20_config *pconf;
616 struct libusb_device *dev;
620 dev = libusb_get_device(pdev);
622 return (LIBUSB_ERROR_INVALID_PARAM);
624 if (configuration < 1) {
628 for (i = 0; i != 255; i++) {
631 pconf = libusb20_dev_alloc_config(pdev, i);
633 return (LIBUSB_ERROR_INVALID_PARAM);
634 found = (pconf->desc.bConfigurationValue
641 return (LIBUSB_ERROR_INVALID_PARAM);
646 libusb10_cancel_all_transfer(dev);
648 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
650 err = libusb20_dev_set_config_index(pdev, i);
652 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
653 POLLOUT | POLLRDNORM | POLLWRNORM);
655 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
659 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
664 dev = libusb_get_device(pdev);
666 return (LIBUSB_ERROR_INVALID_PARAM);
668 if (interface_number < 0 || interface_number > 31)
669 return (LIBUSB_ERROR_INVALID_PARAM);
671 if (pdev->auto_detach != 0) {
672 err = libusb_detach_kernel_driver(pdev, interface_number);
678 dev->claimed_interfaces |= (1 << interface_number);
679 CTX_UNLOCK(dev->ctx);
685 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
690 dev = libusb_get_device(pdev);
692 return (LIBUSB_ERROR_INVALID_PARAM);
694 if (interface_number < 0 || interface_number > 31)
695 return (LIBUSB_ERROR_INVALID_PARAM);
697 if (pdev->auto_detach != 0) {
698 err = libusb_attach_kernel_driver(pdev, interface_number);
704 if (!(dev->claimed_interfaces & (1 << interface_number)))
705 err = LIBUSB_ERROR_NOT_FOUND;
707 dev->claimed_interfaces &= ~(1 << interface_number);
708 CTX_UNLOCK(dev->ctx);
714 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
715 int interface_number, int alternate_setting)
720 dev = libusb_get_device(pdev);
722 return (LIBUSB_ERROR_INVALID_PARAM);
724 if (interface_number < 0 || interface_number > 31)
725 return (LIBUSB_ERROR_INVALID_PARAM);
728 if (!(dev->claimed_interfaces & (1 << interface_number)))
729 err = LIBUSB_ERROR_NOT_FOUND;
730 CTX_UNLOCK(dev->ctx);
735 libusb10_cancel_all_transfer(dev);
737 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
739 err = libusb20_dev_set_alt_index(pdev,
740 interface_number, alternate_setting);
742 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
743 pdev, libusb20_dev_get_fd(pdev),
744 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
746 return (err ? LIBUSB_ERROR_OTHER : 0);
749 static struct libusb20_transfer *
750 libusb10_get_transfer(struct libusb20_device *pdev,
751 uint8_t endpoint, uint8_t xfer_index)
753 xfer_index &= 1; /* double buffering */
755 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
757 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
758 /* this is an IN endpoint */
761 return (libusb20_tr_get_pointer(pdev, xfer_index));
765 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
767 struct libusb20_transfer *xfer;
768 struct libusb_device *dev;
771 xfer = libusb10_get_transfer(pdev, endpoint, 0);
773 return (LIBUSB_ERROR_INVALID_PARAM);
775 dev = libusb_get_device(pdev);
777 return (LIBUSB_ERROR_INVALID_PARAM);
780 err = libusb20_tr_open(xfer, 0, 1, endpoint);
781 CTX_UNLOCK(dev->ctx);
783 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
784 return (LIBUSB_ERROR_OTHER);
786 libusb20_tr_clear_stall_sync(xfer);
788 /* check if we opened the transfer */
791 libusb20_tr_close(xfer);
792 CTX_UNLOCK(dev->ctx);
794 return (0); /* success */
798 libusb_reset_device(struct libusb20_device *pdev)
803 dev = libusb_get_device(pdev);
805 return (LIBUSB_ERROR_INVALID_PARAM);
807 libusb10_cancel_all_transfer(dev);
809 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
811 err = libusb20_dev_reset(pdev);
813 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
814 pdev, libusb20_dev_get_fd(pdev),
815 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
817 return (err ? LIBUSB_ERROR_OTHER : 0);
821 libusb_check_connected(struct libusb20_device *pdev)
826 dev = libusb_get_device(pdev);
828 return (LIBUSB_ERROR_INVALID_PARAM);
830 err = libusb20_dev_check_connected(pdev);
832 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
836 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
839 return (LIBUSB_ERROR_INVALID_PARAM);
841 if (libusb20_dev_kernel_driver_active(pdev, interface))
842 return (0); /* no kernel driver is active */
844 return (1); /* kernel driver is active */
848 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
849 char *name, int namelen)
851 return (libusb_get_driver(pdev, interface, name, namelen));
855 libusb_get_driver(struct libusb20_device *pdev, int interface,
856 char *name, int namelen)
862 return (LIBUSB_ERROR_INVALID_PARAM);
864 return (LIBUSB_ERROR_INVALID_PARAM);
868 err = libusb20_dev_get_iface_desc(
869 pdev, interface, name, namelen);
872 return (LIBUSB_ERROR_OTHER);
874 /* we only want the driver name */
875 ptr = strstr(name, ":");
883 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
885 return (libusb_detach_kernel_driver(pdev, interface));
889 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
894 return (LIBUSB_ERROR_INVALID_PARAM);
896 err = libusb20_dev_detach_kernel_driver(
899 return (err ? LIBUSB_ERROR_OTHER : 0);
903 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
906 return (LIBUSB_ERROR_INVALID_PARAM);
907 /* stub - currently not supported by libusb20 */
912 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
914 dev->auto_detach = (enable ? 1 : 0);
918 /* Asynchronous device I/O */
920 struct libusb_transfer *
921 libusb_alloc_transfer(int iso_packets)
923 struct libusb_transfer *uxfer;
924 struct libusb_super_transfer *sxfer;
927 len = sizeof(struct libusb_transfer) +
928 sizeof(struct libusb_super_transfer) +
929 (iso_packets * sizeof(libusb_iso_packet_descriptor));
935 memset(sxfer, 0, len);
937 uxfer = (struct libusb_transfer *)(
938 ((uint8_t *)sxfer) + sizeof(*sxfer));
940 /* set default value */
941 uxfer->num_iso_packets = iso_packets;
947 libusb_free_transfer(struct libusb_transfer *uxfer)
949 struct libusb_super_transfer *sxfer;
952 return; /* be NULL safe */
954 /* check if we should free the transfer buffer */
955 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
958 sxfer = (struct libusb_super_transfer *)(
959 (uint8_t *)uxfer - sizeof(*sxfer));
965 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
969 switch (xfer->type) {
970 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
971 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
973 case LIBUSB_TRANSFER_TYPE_CONTROL:
984 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
989 usb_speed = libusb20_dev_get_speed(pdev);
991 switch (xfer->type) {
992 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
993 ret = 0; /* kernel will auto-select */
995 case LIBUSB_TRANSFER_TYPE_CONTROL:
1000 case LIBUSB20_SPEED_LOW:
1003 case LIBUSB20_SPEED_FULL:
1006 case LIBUSB20_SPEED_SUPER:
1019 libusb10_convert_error(uint8_t status)
1024 case LIBUSB20_TRANSFER_START:
1025 case LIBUSB20_TRANSFER_COMPLETED:
1026 return (LIBUSB_TRANSFER_COMPLETED);
1027 case LIBUSB20_TRANSFER_OVERFLOW:
1028 return (LIBUSB_TRANSFER_OVERFLOW);
1029 case LIBUSB20_TRANSFER_NO_DEVICE:
1030 return (LIBUSB_TRANSFER_NO_DEVICE);
1031 case LIBUSB20_TRANSFER_STALL:
1032 return (LIBUSB_TRANSFER_STALL);
1033 case LIBUSB20_TRANSFER_CANCELLED:
1034 return (LIBUSB_TRANSFER_CANCELLED);
1035 case LIBUSB20_TRANSFER_TIMED_OUT:
1036 return (LIBUSB_TRANSFER_TIMED_OUT);
1038 return (LIBUSB_TRANSFER_ERROR);
1042 /* This function must be called locked */
1045 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1046 struct libusb_super_transfer *sxfer, int status)
1048 struct libusb_transfer *uxfer;
1049 struct libusb_device *dev;
1051 uxfer = (struct libusb_transfer *)(
1052 ((uint8_t *)sxfer) + sizeof(*sxfer));
1055 libusb20_tr_set_priv_sc1(pxfer, NULL);
1057 /* set transfer status */
1058 uxfer->status = status;
1060 /* update super transfer state */
1061 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1063 dev = libusb_get_device(uxfer->dev_handle);
1065 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1068 /* This function must be called locked */
1071 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1073 struct libusb_super_transfer *sxfer;
1074 struct libusb_transfer *uxfer;
1076 uint16_t iso_packets;
1081 status = libusb20_tr_get_status(pxfer);
1082 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1083 actlen = libusb20_tr_get_actual_length(pxfer);
1084 iso_packets = libusb20_tr_get_max_frames(pxfer);
1087 return; /* cancelled - nothing to do */
1089 uxfer = (struct libusb_transfer *)(
1090 ((uint8_t *)sxfer) + sizeof(*sxfer));
1092 if (iso_packets > uxfer->num_iso_packets)
1093 iso_packets = uxfer->num_iso_packets;
1095 if (iso_packets == 0)
1096 return; /* nothing to do */
1098 /* make sure that the number of ISOCHRONOUS packets is valid */
1099 uxfer->num_iso_packets = iso_packets;
1101 flags = uxfer->flags;
1104 case LIBUSB20_TRANSFER_COMPLETED:
1106 /* update actual length */
1107 uxfer->actual_length = actlen;
1108 for (i = 0; i != iso_packets; i++) {
1109 uxfer->iso_packet_desc[i].actual_length =
1110 libusb20_tr_get_length(pxfer, i);
1112 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1115 case LIBUSB20_TRANSFER_START:
1117 /* setup length(s) */
1119 for (i = 0; i != iso_packets; i++) {
1120 libusb20_tr_setup_isoc(pxfer,
1121 &uxfer->buffer[actlen],
1122 uxfer->iso_packet_desc[i].length, i);
1123 actlen += uxfer->iso_packet_desc[i].length;
1129 libusb20_tr_set_total_frames(pxfer, iso_packets);
1130 libusb20_tr_submit(pxfer);
1132 /* fork another USB transfer, if any */
1133 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1137 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1142 /* This function must be called locked */
1145 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1147 struct libusb_super_transfer *sxfer;
1148 struct libusb_transfer *uxfer;
1154 status = libusb20_tr_get_status(pxfer);
1155 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1156 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1157 actlen = libusb20_tr_get_actual_length(pxfer);
1160 return; /* cancelled - nothing to do */
1162 uxfer = (struct libusb_transfer *)(
1163 ((uint8_t *)sxfer) + sizeof(*sxfer));
1165 flags = uxfer->flags;
1168 case LIBUSB20_TRANSFER_COMPLETED:
1170 uxfer->actual_length += actlen;
1172 /* check for short packet */
1173 if (sxfer->last_len != actlen) {
1174 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1175 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1177 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1181 /* check for end of data */
1182 if (sxfer->rem_len == 0) {
1183 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1188 case LIBUSB20_TRANSFER_START:
1189 if (max_bulk > sxfer->rem_len) {
1190 max_bulk = sxfer->rem_len;
1192 /* setup new BULK or INTERRUPT transaction */
1193 libusb20_tr_setup_bulk(pxfer,
1194 sxfer->curr_data, max_bulk, uxfer->timeout);
1196 /* update counters */
1197 sxfer->last_len = max_bulk;
1198 sxfer->curr_data += max_bulk;
1199 sxfer->rem_len -= max_bulk;
1201 libusb20_tr_submit(pxfer);
1203 /* check if we can fork another USB transfer */
1204 if (sxfer->rem_len == 0)
1205 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1209 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1214 /* This function must be called locked */
1217 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1219 struct libusb_super_transfer *sxfer;
1220 struct libusb_transfer *uxfer;
1226 status = libusb20_tr_get_status(pxfer);
1227 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1228 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1229 actlen = libusb20_tr_get_actual_length(pxfer);
1232 return; /* cancelled - nothing to do */
1234 uxfer = (struct libusb_transfer *)(
1235 ((uint8_t *)sxfer) + sizeof(*sxfer));
1237 flags = uxfer->flags;
1240 case LIBUSB20_TRANSFER_COMPLETED:
1242 uxfer->actual_length += actlen;
1244 /* subtract length of SETUP packet, if any */
1245 actlen -= libusb20_tr_get_length(pxfer, 0);
1247 /* check for short packet */
1248 if (sxfer->last_len != actlen) {
1249 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1250 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1252 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1256 /* check for end of data */
1257 if (sxfer->rem_len == 0) {
1258 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1263 case LIBUSB20_TRANSFER_START:
1264 if (max_bulk > sxfer->rem_len) {
1265 max_bulk = sxfer->rem_len;
1267 /* setup new CONTROL transaction */
1268 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1269 /* next fragment - don't send SETUP packet */
1270 libusb20_tr_set_length(pxfer, 0, 0);
1272 /* first fragment - send SETUP packet */
1273 libusb20_tr_set_length(pxfer, 8, 0);
1274 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1277 if (max_bulk != 0) {
1278 libusb20_tr_set_length(pxfer, max_bulk, 1);
1279 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1280 libusb20_tr_set_total_frames(pxfer, 2);
1282 libusb20_tr_set_total_frames(pxfer, 1);
1285 /* update counters */
1286 sxfer->last_len = max_bulk;
1287 sxfer->curr_data += max_bulk;
1288 sxfer->rem_len -= max_bulk;
1290 libusb20_tr_submit(pxfer);
1292 /* check if we can fork another USB transfer */
1293 if (sxfer->rem_len == 0)
1294 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1298 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1303 /* The following function must be called locked */
1306 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1308 struct libusb20_transfer *pxfer0;
1309 struct libusb20_transfer *pxfer1;
1310 struct libusb_super_transfer *sxfer;
1311 struct libusb_transfer *uxfer;
1312 struct libusb_device *dev;
1318 dev = libusb_get_device(pdev);
1320 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1321 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1323 if (pxfer0 == NULL || pxfer1 == NULL)
1324 return; /* shouldn't happen */
1327 if (libusb20_tr_pending(pxfer0))
1329 if (libusb20_tr_pending(pxfer1))
1334 /* wait till one of the transfers complete */
1337 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1339 return; /* cancelling */
1341 return; /* cannot queue another one */
1342 /* swap transfers */
1346 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1348 return; /* cancelling */
1350 return; /* cannot queue another one */
1351 /* swap transfers */
1358 /* find next transfer on same endpoint */
1359 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1361 uxfer = (struct libusb_transfer *)(
1362 ((uint8_t *)sxfer) + sizeof(*sxfer));
1364 if (uxfer->endpoint == endpoint) {
1365 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1366 sxfer->entry.tqe_prev = NULL;
1370 return; /* success */
1374 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1375 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1377 /* reset super transfer state */
1378 sxfer->rem_len = uxfer->length;
1379 sxfer->curr_data = uxfer->buffer;
1380 uxfer->actual_length = 0;
1382 switch (uxfer->type) {
1383 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1384 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1386 case LIBUSB_TRANSFER_TYPE_BULK:
1387 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1388 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1390 case LIBUSB_TRANSFER_TYPE_CONTROL:
1391 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1392 if (sxfer->rem_len < 8)
1395 /* remove SETUP packet from data */
1396 sxfer->rem_len -= 8;
1397 sxfer->curr_data += 8;
1403 buffsize = libusb10_get_buffsize(pdev, uxfer);
1404 maxframe = libusb10_get_maxframe(pdev, uxfer);
1406 /* make sure the transfer is opened */
1407 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1408 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1411 libusb20_tr_start(pxfer0);
1415 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1416 /* make sure our event loop spins the done handler */
1417 libusb10_wakeup_event_loop(dev->ctx);
1420 /* The following function must be called unlocked */
1423 libusb_submit_transfer(struct libusb_transfer *uxfer)
1425 struct libusb20_transfer *pxfer0;
1426 struct libusb20_transfer *pxfer1;
1427 struct libusb_super_transfer *sxfer;
1428 struct libusb_device *dev;
1433 return (LIBUSB_ERROR_INVALID_PARAM);
1435 if (uxfer->dev_handle == NULL)
1436 return (LIBUSB_ERROR_INVALID_PARAM);
1438 endpoint = uxfer->endpoint;
1441 return (LIBUSB_ERROR_INVALID_PARAM);
1443 dev = libusb_get_device(uxfer->dev_handle);
1445 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1447 sxfer = (struct libusb_super_transfer *)(
1448 (uint8_t *)uxfer - sizeof(*sxfer));
1452 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1453 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1455 if (pxfer0 == NULL || pxfer1 == NULL) {
1456 err = LIBUSB_ERROR_OTHER;
1457 } else if ((sxfer->entry.tqe_prev != NULL) ||
1458 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1459 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1460 err = LIBUSB_ERROR_BUSY;
1461 } else if (dev->device_is_gone != 0) {
1462 err = LIBUSB_ERROR_NO_DEVICE;
1465 /* set pending state */
1466 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1468 /* insert transfer into transfer head list */
1469 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1471 /* start work transfers */
1472 libusb10_submit_transfer_sub(
1473 uxfer->dev_handle, endpoint);
1475 err = 0; /* success */
1478 CTX_UNLOCK(dev->ctx);
1480 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1485 /* Asynchronous transfer cancel */
1488 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1490 struct libusb20_transfer *pxfer0;
1491 struct libusb20_transfer *pxfer1;
1492 struct libusb_super_transfer *sxfer;
1493 struct libusb_device *dev;
1494 struct libusb_device_handle *devh;
1499 return (LIBUSB_ERROR_INVALID_PARAM);
1501 /* check if not initialised */
1502 if ((devh = uxfer->dev_handle) == NULL)
1503 return (LIBUSB_ERROR_NOT_FOUND);
1505 endpoint = uxfer->endpoint;
1508 return (LIBUSB_ERROR_INVALID_PARAM);
1510 dev = libusb_get_device(devh);
1512 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1514 sxfer = (struct libusb_super_transfer *)(
1515 (uint8_t *)uxfer - sizeof(*sxfer));
1521 pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1522 pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1524 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1525 /* only update the transfer status */
1526 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1527 retval = LIBUSB_ERROR_NOT_FOUND;
1528 } else if (sxfer->entry.tqe_prev != NULL) {
1529 /* we are lucky - transfer is on a queue */
1530 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1531 sxfer->entry.tqe_prev = NULL;
1532 libusb10_complete_transfer(NULL,
1533 sxfer, LIBUSB_TRANSFER_CANCELLED);
1534 /* make sure our event loop spins the done handler */
1535 libusb10_wakeup_event_loop(dev->ctx);
1536 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1538 retval = LIBUSB_ERROR_NOT_FOUND;
1539 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1540 libusb10_complete_transfer(pxfer0,
1541 sxfer, LIBUSB_TRANSFER_CANCELLED);
1542 if (dev->device_is_gone != 0) {
1543 /* clear transfer pointer */
1544 libusb20_tr_set_priv_sc1(pxfer0, NULL);
1545 /* make sure our event loop spins the done handler */
1546 libusb10_wakeup_event_loop(dev->ctx);
1548 libusb20_tr_stop(pxfer0);
1549 /* make sure the queue doesn't stall */
1550 libusb10_submit_transfer_sub(devh, endpoint);
1552 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1553 libusb10_complete_transfer(pxfer1,
1554 sxfer, LIBUSB_TRANSFER_CANCELLED);
1555 /* check if handle is still active */
1556 if (dev->device_is_gone != 0) {
1557 /* clear transfer pointer */
1558 libusb20_tr_set_priv_sc1(pxfer1, NULL);
1559 /* make sure our event loop spins the done handler */
1560 libusb10_wakeup_event_loop(dev->ctx);
1562 libusb20_tr_stop(pxfer1);
1563 /* make sure the queue doesn't stall */
1564 libusb10_submit_transfer_sub(devh, endpoint);
1568 retval = LIBUSB_ERROR_NOT_FOUND;
1571 CTX_UNLOCK(dev->ctx);
1573 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1579 libusb10_cancel_all_transfer(libusb_device *dev)
1581 struct libusb20_device *pdev = dev->os_priv;
1584 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1585 struct libusb20_transfer *xfer;
1587 xfer = libusb20_tr_get_pointer(pdev, x);
1590 libusb20_tr_close(xfer);
1595 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1597 struct libusb_super_transfer *sxfer;
1600 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1601 struct libusb20_transfer *xfer;
1603 xfer = libusb20_tr_get_pointer(pdev, x);
1606 if (libusb20_tr_pending(xfer) == 0)
1608 sxfer = libusb20_tr_get_priv_sc1(xfer);
1611 /* complete pending transfer */
1612 libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1615 while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1616 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1618 /* complete pending transfer */
1619 libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1624 libusb_cpu_to_le16(uint16_t x)
1626 return (htole16(x));
1630 libusb_le16_to_cpu(uint16_t x)
1632 return (le16toh(x));
1636 libusb_strerror(int code)
1639 case LIBUSB_SUCCESS:
1641 case LIBUSB_ERROR_IO:
1642 return ("I/O error");
1643 case LIBUSB_ERROR_INVALID_PARAM:
1644 return ("Invalid parameter");
1645 case LIBUSB_ERROR_ACCESS:
1646 return ("Permissions error");
1647 case LIBUSB_ERROR_NO_DEVICE:
1648 return ("No device");
1649 case LIBUSB_ERROR_NOT_FOUND:
1650 return ("Not found");
1651 case LIBUSB_ERROR_BUSY:
1652 return ("Device busy");
1653 case LIBUSB_ERROR_TIMEOUT:
1655 case LIBUSB_ERROR_OVERFLOW:
1656 return ("Overflow");
1657 case LIBUSB_ERROR_PIPE:
1658 return ("Pipe error");
1659 case LIBUSB_ERROR_INTERRUPTED:
1660 return ("Interrupted");
1661 case LIBUSB_ERROR_NO_MEM:
1662 return ("Out of memory");
1663 case LIBUSB_ERROR_NOT_SUPPORTED:
1664 return ("Not supported");
1665 case LIBUSB_ERROR_OTHER:
1666 return ("Other error");
1668 return ("Unknown error");
1673 libusb_error_name(int code)
1676 case LIBUSB_SUCCESS:
1677 return ("LIBUSB_SUCCESS");
1678 case LIBUSB_ERROR_IO:
1679 return ("LIBUSB_ERROR_IO");
1680 case LIBUSB_ERROR_INVALID_PARAM:
1681 return ("LIBUSB_ERROR_INVALID_PARAM");
1682 case LIBUSB_ERROR_ACCESS:
1683 return ("LIBUSB_ERROR_ACCESS");
1684 case LIBUSB_ERROR_NO_DEVICE:
1685 return ("LIBUSB_ERROR_NO_DEVICE");
1686 case LIBUSB_ERROR_NOT_FOUND:
1687 return ("LIBUSB_ERROR_NOT_FOUND");
1688 case LIBUSB_ERROR_BUSY:
1689 return ("LIBUSB_ERROR_BUSY");
1690 case LIBUSB_ERROR_TIMEOUT:
1691 return ("LIBUSB_ERROR_TIMEOUT");
1692 case LIBUSB_ERROR_OVERFLOW:
1693 return ("LIBUSB_ERROR_OVERFLOW");
1694 case LIBUSB_ERROR_PIPE:
1695 return ("LIBUSB_ERROR_PIPE");
1696 case LIBUSB_ERROR_INTERRUPTED:
1697 return ("LIBUSB_ERROR_INTERRUPTED");
1698 case LIBUSB_ERROR_NO_MEM:
1699 return ("LIBUSB_ERROR_NO_MEM");
1700 case LIBUSB_ERROR_NOT_SUPPORTED:
1701 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1702 case LIBUSB_ERROR_OTHER:
1703 return ("LIBUSB_ERROR_OTHER");
1705 return ("LIBUSB_ERROR_UNKNOWN");