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 libusb_init(libusb_context **context)
112 struct libusb_context *ctx;
113 pthread_condattr_t attr;
117 ctx = malloc(sizeof(*ctx));
119 return (LIBUSB_ERROR_INVALID_PARAM);
121 memset(ctx, 0, sizeof(*ctx));
123 debug = getenv("LIBUSB_DEBUG");
125 ctx->debug = atoi(debug);
127 ctx->debug_fixed = 1;
129 TAILQ_INIT(&ctx->pollfds);
130 TAILQ_INIT(&ctx->tr_done);
131 TAILQ_INIT(&ctx->hotplug_cbh);
132 TAILQ_INIT(&ctx->hotplug_devs);
134 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
136 return (LIBUSB_ERROR_NO_MEM);
138 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
139 pthread_mutex_destroy(&ctx->ctx_lock);
141 return (LIBUSB_ERROR_NO_MEM);
143 if (pthread_condattr_init(&attr) != 0) {
144 pthread_mutex_destroy(&ctx->ctx_lock);
145 pthread_mutex_destroy(&ctx->hotplug_lock);
147 return (LIBUSB_ERROR_NO_MEM);
149 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
150 pthread_mutex_destroy(&ctx->ctx_lock);
151 pthread_mutex_destroy(&ctx->hotplug_lock);
152 pthread_condattr_destroy(&attr);
154 return (LIBUSB_ERROR_OTHER);
156 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
157 pthread_mutex_destroy(&ctx->ctx_lock);
158 pthread_mutex_destroy(&ctx->hotplug_lock);
159 pthread_condattr_destroy(&attr);
161 return (LIBUSB_ERROR_NO_MEM);
163 pthread_condattr_destroy(&attr);
165 ctx->ctx_handler = NO_THREAD;
166 ctx->hotplug_handler = NO_THREAD;
168 ret = pipe(ctx->ctrl_pipe);
170 pthread_mutex_destroy(&ctx->ctx_lock);
171 pthread_mutex_destroy(&ctx->hotplug_lock);
172 pthread_cond_destroy(&ctx->ctx_cond);
174 return (LIBUSB_ERROR_OTHER);
176 /* set non-blocking mode on the control pipe to avoid deadlock */
177 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
178 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
180 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
182 pthread_mutex_lock(&default_context_lock);
183 if (usbi_default_context == NULL) {
184 usbi_default_context = ctx;
186 pthread_mutex_unlock(&default_context_lock);
191 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
197 libusb_exit(libusb_context *ctx)
199 ctx = GET_CONTEXT(ctx);
204 /* stop hotplug thread, if any */
206 if (ctx->hotplug_handler != NO_THREAD) {
211 td = ctx->hotplug_handler;
212 ctx->hotplug_handler = NO_THREAD;
215 pthread_join(td, &ptr);
218 /* XXX cleanup devices */
220 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
221 close(ctx->ctrl_pipe[0]);
222 close(ctx->ctrl_pipe[1]);
223 pthread_mutex_destroy(&ctx->ctx_lock);
224 pthread_mutex_destroy(&ctx->hotplug_lock);
225 pthread_cond_destroy(&ctx->ctx_cond);
227 pthread_mutex_lock(&default_context_lock);
228 if (ctx == usbi_default_context) {
229 usbi_default_context = NULL;
231 pthread_mutex_unlock(&default_context_lock);
236 /* Device handling and initialisation. */
239 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
241 struct libusb20_backend *usb_backend;
242 struct libusb20_device *pdev;
243 struct libusb_device *dev;
246 ctx = GET_CONTEXT(ctx);
249 return (LIBUSB_ERROR_INVALID_PARAM);
252 return (LIBUSB_ERROR_INVALID_PARAM);
254 usb_backend = libusb20_be_alloc_default();
255 if (usb_backend == NULL)
256 return (LIBUSB_ERROR_NO_MEM);
258 /* figure out how many USB devices are present */
261 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
264 /* allocate device pointer list */
265 *list = malloc((i + 1) * sizeof(void *));
267 libusb20_be_free(usb_backend);
268 return (LIBUSB_ERROR_NO_MEM);
270 /* create libusb v1.0 compliant devices */
272 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
274 dev = malloc(sizeof(*dev));
277 libusb_unref_device((*list)[i - 1]);
282 libusb20_be_free(usb_backend);
283 return (LIBUSB_ERROR_NO_MEM);
285 /* get device into libUSB v1.0 list */
286 libusb20_be_dequeue_device(usb_backend, pdev);
288 memset(dev, 0, sizeof(*dev));
290 /* init transfer queues */
291 TAILQ_INIT(&dev->tr_head);
293 /* set context we belong to */
296 /* link together the two structures */
298 pdev->privLuData = dev;
300 (*list)[i] = libusb_ref_device(dev);
305 libusb20_be_free(usb_backend);
310 libusb_free_device_list(libusb_device **list, int unref_devices)
315 return; /* be NULL safe */
318 for (i = 0; list[i] != NULL; i++)
319 libusb_unref_device(list[i]);
325 libusb_get_bus_number(libusb_device *dev)
328 return (0); /* should not happen */
329 return (libusb20_dev_get_bus_number(dev->os_priv));
333 libusb_get_port_number(libusb_device *dev)
336 return (0); /* should not happen */
337 return (libusb20_dev_get_parent_port(dev->os_priv));
341 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
343 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
347 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
350 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
354 libusb_get_device_address(libusb_device *dev)
357 return (0); /* should not happen */
358 return (libusb20_dev_get_address(dev->os_priv));
362 libusb_get_device_speed(libusb_device *dev)
365 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
367 switch (libusb20_dev_get_speed(dev->os_priv)) {
368 case LIBUSB20_SPEED_LOW:
369 return (LIBUSB_SPEED_LOW);
370 case LIBUSB20_SPEED_FULL:
371 return (LIBUSB_SPEED_FULL);
372 case LIBUSB20_SPEED_HIGH:
373 return (LIBUSB_SPEED_HIGH);
374 case LIBUSB20_SPEED_SUPER:
375 return (LIBUSB_SPEED_SUPER);
379 return (LIBUSB_SPEED_UNKNOWN);
383 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
385 struct libusb_config_descriptor *pdconf;
386 struct libusb_interface *pinf;
387 struct libusb_interface_descriptor *pdinf;
388 struct libusb_endpoint_descriptor *pdend;
395 return (LIBUSB_ERROR_NO_DEVICE);
397 ret = libusb_get_active_config_descriptor(dev, &pdconf);
401 ret = LIBUSB_ERROR_NOT_FOUND;
402 for (i = 0; i < pdconf->bNumInterfaces; i++) {
403 pinf = &pdconf->interface[i];
404 for (j = 0; j < pinf->num_altsetting; j++) {
405 pdinf = &pinf->altsetting[j];
406 for (k = 0; k < pdinf->bNumEndpoints; k++) {
407 pdend = &pdinf->endpoint[k];
408 if (pdend->bEndpointAddress == endpoint) {
409 ret = pdend->wMaxPacketSize;
417 libusb_free_config_descriptor(pdconf);
422 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
427 ret = libusb_get_max_packet_size(dev, endpoint);
429 switch (libusb20_dev_get_speed(dev->os_priv)) {
430 case LIBUSB20_SPEED_LOW:
431 case LIBUSB20_SPEED_FULL:
435 multiplier = (1 + ((ret >> 11) & 3));
438 ret = (ret & 0x7FF) * multiplier;
446 libusb_ref_device(libusb_device *dev)
449 return (NULL); /* be NULL safe */
453 CTX_UNLOCK(dev->ctx);
459 libusb_unref_device(libusb_device *dev)
462 return; /* be NULL safe */
466 CTX_UNLOCK(dev->ctx);
468 if (dev->refcnt == 0) {
469 libusb20_dev_free(dev->os_priv);
475 libusb_open(libusb_device *dev, libusb_device_handle **devh)
477 libusb_context *ctx = dev->ctx;
478 struct libusb20_device *pdev = dev->os_priv;
483 return (LIBUSB_ERROR_INVALID_PARAM);
485 /* set default device handle value */
488 dev = libusb_ref_device(dev);
490 return (LIBUSB_ERROR_INVALID_PARAM);
492 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
494 libusb_unref_device(dev);
495 return (LIBUSB_ERROR_NO_MEM);
497 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
498 POLLOUT | POLLRDNORM | POLLWRNORM);
500 /* make sure our event loop detects the new device */
502 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
503 if (err < (int)sizeof(dummy)) {
504 /* ignore error, if any */
505 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
512 libusb_device_handle *
513 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
516 struct libusb_device **devs;
517 struct libusb20_device *pdev;
518 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
522 ctx = GET_CONTEXT(ctx);
524 return (NULL); /* be NULL safe */
526 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
528 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
532 for (j = 0; j < i; j++) {
533 struct libusb20_device *tdev;
535 tdev = devs[j]->os_priv;
536 pdesc = libusb20_dev_get_device_desc(tdev);
538 * NOTE: The USB library will automatically swap the
539 * fields in the device descriptor to be of host
542 if (pdesc->idVendor == vendor_id &&
543 pdesc->idProduct == product_id) {
544 libusb_open(devs[j], &pdev);
549 libusb_free_device_list(devs, 1);
550 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
555 libusb_close(struct libusb20_device *pdev)
558 struct libusb_device *dev;
563 return; /* be NULL safe */
565 dev = libusb_get_device(pdev);
568 libusb10_remove_pollfd(ctx, &dev->dev_poll);
570 libusb20_dev_close(pdev);
572 /* unref will free the "pdev" when the refcount reaches zero */
573 libusb_unref_device(dev);
575 /* make sure our event loop detects the closed device */
577 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
578 if (err < (int)sizeof(dummy)) {
579 /* ignore error, if any */
580 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
585 libusb_get_device(struct libusb20_device *pdev)
589 return ((libusb_device *)pdev->privLuData);
593 libusb_get_configuration(struct libusb20_device *pdev, int *config)
595 struct libusb20_config *pconf;
597 if (pdev == NULL || config == NULL)
598 return (LIBUSB_ERROR_INVALID_PARAM);
600 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
602 return (LIBUSB_ERROR_NO_MEM);
604 *config = pconf->desc.bConfigurationValue;
612 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
614 struct libusb20_config *pconf;
615 struct libusb_device *dev;
619 dev = libusb_get_device(pdev);
621 return (LIBUSB_ERROR_INVALID_PARAM);
623 if (configuration < 1) {
627 for (i = 0; i != 255; i++) {
630 pconf = libusb20_dev_alloc_config(pdev, i);
632 return (LIBUSB_ERROR_INVALID_PARAM);
633 found = (pconf->desc.bConfigurationValue
640 return (LIBUSB_ERROR_INVALID_PARAM);
645 libusb10_cancel_all_transfer(dev);
647 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
649 err = libusb20_dev_set_config_index(pdev, i);
651 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
652 POLLOUT | POLLRDNORM | POLLWRNORM);
654 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
658 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
663 dev = libusb_get_device(pdev);
665 return (LIBUSB_ERROR_INVALID_PARAM);
667 if (interface_number < 0 || interface_number > 31)
668 return (LIBUSB_ERROR_INVALID_PARAM);
670 if (pdev->auto_detach != 0) {
671 err = libusb_detach_kernel_driver(pdev, interface_number);
677 dev->claimed_interfaces |= (1 << interface_number);
678 CTX_UNLOCK(dev->ctx);
684 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
689 dev = libusb_get_device(pdev);
691 return (LIBUSB_ERROR_INVALID_PARAM);
693 if (interface_number < 0 || interface_number > 31)
694 return (LIBUSB_ERROR_INVALID_PARAM);
696 if (pdev->auto_detach != 0) {
697 err = libusb_attach_kernel_driver(pdev, interface_number);
703 if (!(dev->claimed_interfaces & (1 << interface_number)))
704 err = LIBUSB_ERROR_NOT_FOUND;
706 dev->claimed_interfaces &= ~(1 << interface_number);
707 CTX_UNLOCK(dev->ctx);
713 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
714 int interface_number, int alternate_setting)
719 dev = libusb_get_device(pdev);
721 return (LIBUSB_ERROR_INVALID_PARAM);
723 if (interface_number < 0 || interface_number > 31)
724 return (LIBUSB_ERROR_INVALID_PARAM);
727 if (!(dev->claimed_interfaces & (1 << interface_number)))
728 err = LIBUSB_ERROR_NOT_FOUND;
729 CTX_UNLOCK(dev->ctx);
734 libusb10_cancel_all_transfer(dev);
736 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
738 err = libusb20_dev_set_alt_index(pdev,
739 interface_number, alternate_setting);
741 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
742 pdev, libusb20_dev_get_fd(pdev),
743 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
745 return (err ? LIBUSB_ERROR_OTHER : 0);
748 static struct libusb20_transfer *
749 libusb10_get_transfer(struct libusb20_device *pdev,
750 uint8_t endpoint, uint8_t xfer_index)
752 xfer_index &= 1; /* double buffering */
754 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
756 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
757 /* this is an IN endpoint */
760 return (libusb20_tr_get_pointer(pdev, xfer_index));
764 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
766 struct libusb20_transfer *xfer;
767 struct libusb_device *dev;
770 xfer = libusb10_get_transfer(pdev, endpoint, 0);
772 return (LIBUSB_ERROR_INVALID_PARAM);
774 dev = libusb_get_device(pdev);
776 return (LIBUSB_ERROR_INVALID_PARAM);
779 err = libusb20_tr_open(xfer, 0, 1, endpoint);
780 CTX_UNLOCK(dev->ctx);
782 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
783 return (LIBUSB_ERROR_OTHER);
785 libusb20_tr_clear_stall_sync(xfer);
787 /* check if we opened the transfer */
790 libusb20_tr_close(xfer);
791 CTX_UNLOCK(dev->ctx);
793 return (0); /* success */
797 libusb_reset_device(struct libusb20_device *pdev)
802 dev = libusb_get_device(pdev);
804 return (LIBUSB_ERROR_INVALID_PARAM);
806 libusb10_cancel_all_transfer(dev);
808 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
810 err = libusb20_dev_reset(pdev);
812 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
813 pdev, libusb20_dev_get_fd(pdev),
814 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
816 return (err ? LIBUSB_ERROR_OTHER : 0);
820 libusb_check_connected(struct libusb20_device *pdev)
825 dev = libusb_get_device(pdev);
827 return (LIBUSB_ERROR_INVALID_PARAM);
829 err = libusb20_dev_check_connected(pdev);
831 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
835 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
838 return (LIBUSB_ERROR_INVALID_PARAM);
840 if (libusb20_dev_kernel_driver_active(pdev, interface))
841 return (0); /* no kernel driver is active */
843 return (1); /* kernel driver is active */
847 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
848 char *name, int namelen)
850 return (libusb_get_driver(pdev, interface, name, namelen));
854 libusb_get_driver(struct libusb20_device *pdev, int interface,
855 char *name, int namelen)
861 return (LIBUSB_ERROR_INVALID_PARAM);
863 return (LIBUSB_ERROR_INVALID_PARAM);
867 err = libusb20_dev_get_iface_desc(
868 pdev, interface, name, namelen);
871 return (LIBUSB_ERROR_OTHER);
873 /* we only want the driver name */
874 ptr = strstr(name, ":");
882 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
884 return (libusb_detach_kernel_driver(pdev, interface));
888 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
893 return (LIBUSB_ERROR_INVALID_PARAM);
895 err = libusb20_dev_detach_kernel_driver(
898 return (err ? LIBUSB_ERROR_OTHER : 0);
902 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
905 return (LIBUSB_ERROR_INVALID_PARAM);
906 /* stub - currently not supported by libusb20 */
911 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
913 dev->auto_detach = (enable ? 1 : 0);
917 /* Asynchronous device I/O */
919 struct libusb_transfer *
920 libusb_alloc_transfer(int iso_packets)
922 struct libusb_transfer *uxfer;
923 struct libusb_super_transfer *sxfer;
926 len = sizeof(struct libusb_transfer) +
927 sizeof(struct libusb_super_transfer) +
928 (iso_packets * sizeof(libusb_iso_packet_descriptor));
934 memset(sxfer, 0, len);
936 uxfer = (struct libusb_transfer *)(
937 ((uint8_t *)sxfer) + sizeof(*sxfer));
939 /* set default value */
940 uxfer->num_iso_packets = iso_packets;
946 libusb_free_transfer(struct libusb_transfer *uxfer)
948 struct libusb_super_transfer *sxfer;
951 return; /* be NULL safe */
953 /* check if we should free the transfer buffer */
954 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
957 sxfer = (struct libusb_super_transfer *)(
958 (uint8_t *)uxfer - sizeof(*sxfer));
964 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
968 switch (xfer->type) {
969 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
970 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
972 case LIBUSB_TRANSFER_TYPE_CONTROL:
983 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
988 usb_speed = libusb20_dev_get_speed(pdev);
990 switch (xfer->type) {
991 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
992 ret = 0; /* kernel will auto-select */
994 case LIBUSB_TRANSFER_TYPE_CONTROL:
999 case LIBUSB20_SPEED_LOW:
1002 case LIBUSB20_SPEED_FULL:
1005 case LIBUSB20_SPEED_SUPER:
1018 libusb10_convert_error(uint8_t status)
1023 case LIBUSB20_TRANSFER_START:
1024 case LIBUSB20_TRANSFER_COMPLETED:
1025 return (LIBUSB_TRANSFER_COMPLETED);
1026 case LIBUSB20_TRANSFER_OVERFLOW:
1027 return (LIBUSB_TRANSFER_OVERFLOW);
1028 case LIBUSB20_TRANSFER_NO_DEVICE:
1029 return (LIBUSB_TRANSFER_NO_DEVICE);
1030 case LIBUSB20_TRANSFER_STALL:
1031 return (LIBUSB_TRANSFER_STALL);
1032 case LIBUSB20_TRANSFER_CANCELLED:
1033 return (LIBUSB_TRANSFER_CANCELLED);
1034 case LIBUSB20_TRANSFER_TIMED_OUT:
1035 return (LIBUSB_TRANSFER_TIMED_OUT);
1037 return (LIBUSB_TRANSFER_ERROR);
1041 /* This function must be called locked */
1044 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1045 struct libusb_super_transfer *sxfer, int status)
1047 struct libusb_transfer *uxfer;
1048 struct libusb_device *dev;
1050 uxfer = (struct libusb_transfer *)(
1051 ((uint8_t *)sxfer) + sizeof(*sxfer));
1054 libusb20_tr_set_priv_sc1(pxfer, NULL);
1056 /* set transfer status */
1057 uxfer->status = status;
1059 /* update super transfer state */
1060 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1062 dev = libusb_get_device(uxfer->dev_handle);
1064 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1067 /* This function must be called locked */
1070 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1072 struct libusb_super_transfer *sxfer;
1073 struct libusb_transfer *uxfer;
1075 uint16_t iso_packets;
1080 status = libusb20_tr_get_status(pxfer);
1081 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1082 actlen = libusb20_tr_get_actual_length(pxfer);
1083 iso_packets = libusb20_tr_get_max_frames(pxfer);
1086 return; /* cancelled - nothing to do */
1088 uxfer = (struct libusb_transfer *)(
1089 ((uint8_t *)sxfer) + sizeof(*sxfer));
1091 if (iso_packets > uxfer->num_iso_packets)
1092 iso_packets = uxfer->num_iso_packets;
1094 if (iso_packets == 0)
1095 return; /* nothing to do */
1097 /* make sure that the number of ISOCHRONOUS packets is valid */
1098 uxfer->num_iso_packets = iso_packets;
1100 flags = uxfer->flags;
1103 case LIBUSB20_TRANSFER_COMPLETED:
1105 /* update actual length */
1106 uxfer->actual_length = actlen;
1107 for (i = 0; i != iso_packets; i++) {
1108 uxfer->iso_packet_desc[i].actual_length =
1109 libusb20_tr_get_length(pxfer, i);
1111 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1114 case LIBUSB20_TRANSFER_START:
1116 /* setup length(s) */
1118 for (i = 0; i != iso_packets; i++) {
1119 libusb20_tr_setup_isoc(pxfer,
1120 &uxfer->buffer[actlen],
1121 uxfer->iso_packet_desc[i].length, i);
1122 actlen += uxfer->iso_packet_desc[i].length;
1128 libusb20_tr_set_total_frames(pxfer, iso_packets);
1129 libusb20_tr_submit(pxfer);
1131 /* fork another USB transfer, if any */
1132 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1136 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1141 /* This function must be called locked */
1144 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1146 struct libusb_super_transfer *sxfer;
1147 struct libusb_transfer *uxfer;
1153 status = libusb20_tr_get_status(pxfer);
1154 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1155 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1156 actlen = libusb20_tr_get_actual_length(pxfer);
1159 return; /* cancelled - nothing to do */
1161 uxfer = (struct libusb_transfer *)(
1162 ((uint8_t *)sxfer) + sizeof(*sxfer));
1164 flags = uxfer->flags;
1167 case LIBUSB20_TRANSFER_COMPLETED:
1169 uxfer->actual_length += actlen;
1171 /* check for short packet */
1172 if (sxfer->last_len != actlen) {
1173 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1174 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1176 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1180 /* check for end of data */
1181 if (sxfer->rem_len == 0) {
1182 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1187 case LIBUSB20_TRANSFER_START:
1188 if (max_bulk > sxfer->rem_len) {
1189 max_bulk = sxfer->rem_len;
1191 /* setup new BULK or INTERRUPT transaction */
1192 libusb20_tr_setup_bulk(pxfer,
1193 sxfer->curr_data, max_bulk, uxfer->timeout);
1195 /* update counters */
1196 sxfer->last_len = max_bulk;
1197 sxfer->curr_data += max_bulk;
1198 sxfer->rem_len -= max_bulk;
1200 libusb20_tr_submit(pxfer);
1202 /* check if we can fork another USB transfer */
1203 if (sxfer->rem_len == 0)
1204 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1208 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1213 /* This function must be called locked */
1216 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1218 struct libusb_super_transfer *sxfer;
1219 struct libusb_transfer *uxfer;
1225 status = libusb20_tr_get_status(pxfer);
1226 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1227 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1228 actlen = libusb20_tr_get_actual_length(pxfer);
1231 return; /* cancelled - nothing to do */
1233 uxfer = (struct libusb_transfer *)(
1234 ((uint8_t *)sxfer) + sizeof(*sxfer));
1236 flags = uxfer->flags;
1239 case LIBUSB20_TRANSFER_COMPLETED:
1241 uxfer->actual_length += actlen;
1243 /* subtract length of SETUP packet, if any */
1244 actlen -= libusb20_tr_get_length(pxfer, 0);
1246 /* check for short packet */
1247 if (sxfer->last_len != actlen) {
1248 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1249 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1251 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1255 /* check for end of data */
1256 if (sxfer->rem_len == 0) {
1257 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1262 case LIBUSB20_TRANSFER_START:
1263 if (max_bulk > sxfer->rem_len) {
1264 max_bulk = sxfer->rem_len;
1266 /* setup new CONTROL transaction */
1267 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1268 /* next fragment - don't send SETUP packet */
1269 libusb20_tr_set_length(pxfer, 0, 0);
1271 /* first fragment - send SETUP packet */
1272 libusb20_tr_set_length(pxfer, 8, 0);
1273 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1276 if (max_bulk != 0) {
1277 libusb20_tr_set_length(pxfer, max_bulk, 1);
1278 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1279 libusb20_tr_set_total_frames(pxfer, 2);
1281 libusb20_tr_set_total_frames(pxfer, 1);
1284 /* update counters */
1285 sxfer->last_len = max_bulk;
1286 sxfer->curr_data += max_bulk;
1287 sxfer->rem_len -= max_bulk;
1289 libusb20_tr_submit(pxfer);
1291 /* check if we can fork another USB transfer */
1292 if (sxfer->rem_len == 0)
1293 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1297 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1302 /* The following function must be called locked */
1305 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1307 struct libusb20_transfer *pxfer0;
1308 struct libusb20_transfer *pxfer1;
1309 struct libusb_super_transfer *sxfer;
1310 struct libusb_transfer *uxfer;
1311 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);
1417 /* make sure our event loop spins the done handler */
1419 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1422 /* The following function must be called unlocked */
1425 libusb_submit_transfer(struct libusb_transfer *uxfer)
1427 struct libusb20_transfer *pxfer0;
1428 struct libusb20_transfer *pxfer1;
1429 struct libusb_super_transfer *sxfer;
1430 struct libusb_device *dev;
1435 return (LIBUSB_ERROR_INVALID_PARAM);
1437 if (uxfer->dev_handle == NULL)
1438 return (LIBUSB_ERROR_INVALID_PARAM);
1440 endpoint = uxfer->endpoint;
1443 return (LIBUSB_ERROR_INVALID_PARAM);
1445 dev = libusb_get_device(uxfer->dev_handle);
1447 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1449 sxfer = (struct libusb_super_transfer *)(
1450 (uint8_t *)uxfer - sizeof(*sxfer));
1454 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1455 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1457 if (pxfer0 == NULL || pxfer1 == NULL) {
1458 err = LIBUSB_ERROR_OTHER;
1459 } else if ((sxfer->entry.tqe_prev != NULL) ||
1460 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1461 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1462 err = LIBUSB_ERROR_BUSY;
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;
1498 return (LIBUSB_ERROR_INVALID_PARAM);
1500 /* check if not initialised */
1501 if (uxfer->dev_handle == NULL)
1502 return (LIBUSB_ERROR_NOT_FOUND);
1504 endpoint = uxfer->endpoint;
1507 return (LIBUSB_ERROR_INVALID_PARAM);
1509 dev = libusb_get_device(uxfer->dev_handle);
1511 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1513 sxfer = (struct libusb_super_transfer *)(
1514 (uint8_t *)uxfer - sizeof(*sxfer));
1520 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1521 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1523 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1524 /* only update the transfer status */
1525 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1526 retval = LIBUSB_ERROR_NOT_FOUND;
1527 } else if (sxfer->entry.tqe_prev != NULL) {
1528 /* we are lucky - transfer is on a queue */
1529 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1530 sxfer->entry.tqe_prev = NULL;
1531 libusb10_complete_transfer(NULL,
1532 sxfer, LIBUSB_TRANSFER_CANCELLED);
1533 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1535 retval = LIBUSB_ERROR_NOT_FOUND;
1536 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1537 libusb10_complete_transfer(pxfer0,
1538 sxfer, LIBUSB_TRANSFER_CANCELLED);
1539 libusb20_tr_stop(pxfer0);
1540 /* make sure the queue doesn't stall */
1541 libusb10_submit_transfer_sub(
1542 uxfer->dev_handle, endpoint);
1543 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1544 libusb10_complete_transfer(pxfer1,
1545 sxfer, LIBUSB_TRANSFER_CANCELLED);
1546 libusb20_tr_stop(pxfer1);
1547 /* make sure the queue doesn't stall */
1548 libusb10_submit_transfer_sub(
1549 uxfer->dev_handle, endpoint);
1552 retval = LIBUSB_ERROR_NOT_FOUND;
1555 CTX_UNLOCK(dev->ctx);
1557 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1563 libusb10_cancel_all_transfer(libusb_device *dev)
1565 struct libusb20_device *pdev = dev->os_priv;
1568 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1569 struct libusb20_transfer *xfer;
1571 xfer = libusb20_tr_get_pointer(pdev, x);
1574 libusb20_tr_close(xfer);
1579 libusb_cpu_to_le16(uint16_t x)
1581 return (htole16(x));
1585 libusb_le16_to_cpu(uint16_t x)
1587 return (le16toh(x));
1591 libusb_strerror(int code)
1594 case LIBUSB_SUCCESS:
1596 case LIBUSB_ERROR_IO:
1597 return ("I/O error");
1598 case LIBUSB_ERROR_INVALID_PARAM:
1599 return ("Invalid parameter");
1600 case LIBUSB_ERROR_ACCESS:
1601 return ("Permissions error");
1602 case LIBUSB_ERROR_NO_DEVICE:
1603 return ("No device");
1604 case LIBUSB_ERROR_NOT_FOUND:
1605 return ("Not found");
1606 case LIBUSB_ERROR_BUSY:
1607 return ("Device busy");
1608 case LIBUSB_ERROR_TIMEOUT:
1610 case LIBUSB_ERROR_OVERFLOW:
1611 return ("Overflow");
1612 case LIBUSB_ERROR_PIPE:
1613 return ("Pipe error");
1614 case LIBUSB_ERROR_INTERRUPTED:
1615 return ("Interrupted");
1616 case LIBUSB_ERROR_NO_MEM:
1617 return ("Out of memory");
1618 case LIBUSB_ERROR_NOT_SUPPORTED:
1619 return ("Not supported");
1620 case LIBUSB_ERROR_OTHER:
1621 return ("Other error");
1623 return ("Unknown error");
1628 libusb_error_name(int code)
1631 case LIBUSB_SUCCESS:
1632 return ("LIBUSB_SUCCESS");
1633 case LIBUSB_ERROR_IO:
1634 return ("LIBUSB_ERROR_IO");
1635 case LIBUSB_ERROR_INVALID_PARAM:
1636 return ("LIBUSB_ERROR_INVALID_PARAM");
1637 case LIBUSB_ERROR_ACCESS:
1638 return ("LIBUSB_ERROR_ACCESS");
1639 case LIBUSB_ERROR_NO_DEVICE:
1640 return ("LIBUSB_ERROR_NO_DEVICE");
1641 case LIBUSB_ERROR_NOT_FOUND:
1642 return ("LIBUSB_ERROR_NOT_FOUND");
1643 case LIBUSB_ERROR_BUSY:
1644 return ("LIBUSB_ERROR_BUSY");
1645 case LIBUSB_ERROR_TIMEOUT:
1646 return ("LIBUSB_ERROR_TIMEOUT");
1647 case LIBUSB_ERROR_OVERFLOW:
1648 return ("LIBUSB_ERROR_OVERFLOW");
1649 case LIBUSB_ERROR_PIPE:
1650 return ("LIBUSB_ERROR_PIPE");
1651 case LIBUSB_ERROR_INTERRUPTED:
1652 return ("LIBUSB_ERROR_INTERRUPTED");
1653 case LIBUSB_ERROR_NO_MEM:
1654 return ("LIBUSB_ERROR_NO_MEM");
1655 case LIBUSB_ERROR_NOT_SUPPORTED:
1656 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1657 case LIBUSB_ERROR_OTHER:
1658 return ("LIBUSB_ERROR_OTHER");
1660 return ("LIBUSB_ERROR_UNKNOWN");