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 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
40 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #include <sys/endian.h>
46 #define libusb_device_handle libusb20_device
49 #include "libusb20_desc.h"
50 #include "libusb20_int.h"
54 #define LIBUSB_NUM_SW_ENDPOINTS (16 * 4)
56 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
57 struct libusb_context *usbi_default_context = NULL;
61 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
62 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
63 static int libusb10_convert_error(uint8_t status);
64 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
65 static void libusb10_isoc_proxy(struct libusb20_transfer *);
66 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
67 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
68 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
70 /* Library initialisation / deinitialisation */
72 static const struct libusb_version libusb_version = {
78 .describe = "http://www.freebsd.org"
81 const struct libusb_version *
82 libusb_get_version(void)
85 return (&libusb_version);
89 libusb_set_debug(libusb_context *ctx, int level)
91 ctx = GET_CONTEXT(ctx);
97 libusb_set_nonblocking(int f)
102 * We ignore any failures in this function, hence the
103 * non-blocking flag is not critical to the operation of
104 * libUSB. We use F_GETFL and F_SETFL to be compatible with
108 flags = fcntl(f, F_GETFL, NULL);
112 fcntl(f, F_SETFL, flags);
116 libusb_init(libusb_context **context)
118 struct libusb_context *ctx;
119 pthread_condattr_t attr;
123 ctx = malloc(sizeof(*ctx));
125 return (LIBUSB_ERROR_INVALID_PARAM);
127 memset(ctx, 0, sizeof(*ctx));
129 debug = getenv("LIBUSB_DEBUG");
131 ctx->debug = atoi(debug);
133 ctx->debug_fixed = 1;
135 TAILQ_INIT(&ctx->pollfds);
136 TAILQ_INIT(&ctx->tr_done);
137 TAILQ_INIT(&ctx->hotplug_cbh);
138 TAILQ_INIT(&ctx->hotplug_devs);
140 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
142 return (LIBUSB_ERROR_NO_MEM);
144 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
145 pthread_mutex_destroy(&ctx->ctx_lock);
147 return (LIBUSB_ERROR_NO_MEM);
149 if (pthread_condattr_init(&attr) != 0) {
150 pthread_mutex_destroy(&ctx->ctx_lock);
151 pthread_mutex_destroy(&ctx->hotplug_lock);
153 return (LIBUSB_ERROR_NO_MEM);
155 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
156 pthread_mutex_destroy(&ctx->ctx_lock);
157 pthread_mutex_destroy(&ctx->hotplug_lock);
158 pthread_condattr_destroy(&attr);
160 return (LIBUSB_ERROR_OTHER);
162 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
163 pthread_mutex_destroy(&ctx->ctx_lock);
164 pthread_mutex_destroy(&ctx->hotplug_lock);
165 pthread_condattr_destroy(&attr);
167 return (LIBUSB_ERROR_NO_MEM);
169 pthread_condattr_destroy(&attr);
171 ctx->ctx_handler = NO_THREAD;
172 ctx->hotplug_handler = NO_THREAD;
174 ret = pipe(ctx->ctrl_pipe);
176 pthread_mutex_destroy(&ctx->ctx_lock);
177 pthread_mutex_destroy(&ctx->hotplug_lock);
178 pthread_cond_destroy(&ctx->ctx_cond);
180 return (LIBUSB_ERROR_OTHER);
182 /* set non-blocking mode on the control pipe to avoid deadlock */
183 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
184 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
186 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
188 pthread_mutex_lock(&default_context_lock);
189 if (usbi_default_context == NULL) {
190 usbi_default_context = ctx;
192 pthread_mutex_unlock(&default_context_lock);
197 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
203 libusb_exit(libusb_context *ctx)
205 ctx = GET_CONTEXT(ctx);
210 /* stop hotplug thread, if any */
212 if (ctx->hotplug_handler != NO_THREAD) {
217 td = ctx->hotplug_handler;
218 ctx->hotplug_handler = NO_THREAD;
221 pthread_join(td, &ptr);
224 /* XXX cleanup devices */
226 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
227 close(ctx->ctrl_pipe[0]);
228 close(ctx->ctrl_pipe[1]);
229 pthread_mutex_destroy(&ctx->ctx_lock);
230 pthread_mutex_destroy(&ctx->hotplug_lock);
231 pthread_cond_destroy(&ctx->ctx_cond);
233 pthread_mutex_lock(&default_context_lock);
234 if (ctx == usbi_default_context) {
235 usbi_default_context = NULL;
237 pthread_mutex_unlock(&default_context_lock);
242 /* Device handling and initialisation. */
245 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
247 struct libusb20_backend *usb_backend;
248 struct libusb20_device *pdev;
249 struct libusb_device *dev;
252 ctx = GET_CONTEXT(ctx);
255 return (LIBUSB_ERROR_INVALID_PARAM);
258 return (LIBUSB_ERROR_INVALID_PARAM);
260 usb_backend = libusb20_be_alloc_default();
261 if (usb_backend == NULL)
262 return (LIBUSB_ERROR_NO_MEM);
264 /* figure out how many USB devices are present */
267 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
270 /* allocate device pointer list */
271 *list = malloc((i + 1) * sizeof(void *));
273 libusb20_be_free(usb_backend);
274 return (LIBUSB_ERROR_NO_MEM);
276 /* create libusb v1.0 compliant devices */
278 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
280 dev = malloc(sizeof(*dev));
283 libusb_unref_device((*list)[i - 1]);
288 libusb20_be_free(usb_backend);
289 return (LIBUSB_ERROR_NO_MEM);
291 /* get device into libUSB v1.0 list */
292 libusb20_be_dequeue_device(usb_backend, pdev);
294 memset(dev, 0, sizeof(*dev));
296 /* init transfer queues */
297 TAILQ_INIT(&dev->tr_head);
299 /* set context we belong to */
302 /* link together the two structures */
304 pdev->privLuData = dev;
306 (*list)[i] = libusb_ref_device(dev);
311 libusb20_be_free(usb_backend);
316 libusb_free_device_list(libusb_device **list, int unref_devices)
321 return; /* be NULL safe */
324 for (i = 0; list[i] != NULL; i++)
325 libusb_unref_device(list[i]);
331 libusb_get_bus_number(libusb_device *dev)
334 return (0); /* should not happen */
335 return (libusb20_dev_get_bus_number(dev->os_priv));
339 libusb_get_port_number(libusb_device *dev)
342 return (0); /* should not happen */
343 return (libusb20_dev_get_parent_port(dev->os_priv));
347 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
349 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
353 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
356 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
360 libusb_get_device_address(libusb_device *dev)
363 return (0); /* should not happen */
364 return (libusb20_dev_get_address(dev->os_priv));
368 libusb_get_device_speed(libusb_device *dev)
371 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
373 switch (libusb20_dev_get_speed(dev->os_priv)) {
374 case LIBUSB20_SPEED_LOW:
375 return (LIBUSB_SPEED_LOW);
376 case LIBUSB20_SPEED_FULL:
377 return (LIBUSB_SPEED_FULL);
378 case LIBUSB20_SPEED_HIGH:
379 return (LIBUSB_SPEED_HIGH);
380 case LIBUSB20_SPEED_SUPER:
381 return (LIBUSB_SPEED_SUPER);
385 return (LIBUSB_SPEED_UNKNOWN);
389 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
391 struct libusb_config_descriptor *pdconf;
392 struct libusb_interface *pinf;
393 struct libusb_interface_descriptor *pdinf;
394 struct libusb_endpoint_descriptor *pdend;
401 return (LIBUSB_ERROR_NO_DEVICE);
403 ret = libusb_get_active_config_descriptor(dev, &pdconf);
407 ret = LIBUSB_ERROR_NOT_FOUND;
408 for (i = 0; i < pdconf->bNumInterfaces; i++) {
409 pinf = &pdconf->interface[i];
410 for (j = 0; j < pinf->num_altsetting; j++) {
411 pdinf = &pinf->altsetting[j];
412 for (k = 0; k < pdinf->bNumEndpoints; k++) {
413 pdend = &pdinf->endpoint[k];
414 if (pdend->bEndpointAddress == endpoint) {
415 ret = pdend->wMaxPacketSize;
423 libusb_free_config_descriptor(pdconf);
428 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
433 ret = libusb_get_max_packet_size(dev, endpoint);
435 switch (libusb20_dev_get_speed(dev->os_priv)) {
436 case LIBUSB20_SPEED_LOW:
437 case LIBUSB20_SPEED_FULL:
441 multiplier = (1 + ((ret >> 11) & 3));
444 ret = (ret & 0x7FF) * multiplier;
452 libusb_ref_device(libusb_device *dev)
455 return (NULL); /* be NULL safe */
459 CTX_UNLOCK(dev->ctx);
465 libusb_unref_device(libusb_device *dev)
468 return; /* be NULL safe */
472 CTX_UNLOCK(dev->ctx);
474 if (dev->refcnt == 0) {
475 libusb20_dev_free(dev->os_priv);
481 libusb_open(libusb_device *dev, libusb_device_handle **devh)
483 libusb_context *ctx = dev->ctx;
484 struct libusb20_device *pdev = dev->os_priv;
489 return (LIBUSB_ERROR_INVALID_PARAM);
491 /* set default device handle value */
494 dev = libusb_ref_device(dev);
496 return (LIBUSB_ERROR_INVALID_PARAM);
498 err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
500 libusb_unref_device(dev);
501 return (LIBUSB_ERROR_NO_MEM);
503 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
504 POLLOUT | POLLRDNORM | POLLWRNORM);
506 /* make sure our event loop detects the new device */
508 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
509 if (err < (int)sizeof(dummy)) {
510 /* ignore error, if any */
511 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
518 libusb_device_handle *
519 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
522 struct libusb_device **devs;
523 struct libusb20_device *pdev;
524 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
528 ctx = GET_CONTEXT(ctx);
530 return (NULL); /* be NULL safe */
532 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
534 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
538 for (j = 0; j < i; j++) {
539 struct libusb20_device *tdev;
541 tdev = devs[j]->os_priv;
542 pdesc = libusb20_dev_get_device_desc(tdev);
544 * NOTE: The USB library will automatically swap the
545 * fields in the device descriptor to be of host
548 if (pdesc->idVendor == vendor_id &&
549 pdesc->idProduct == product_id) {
550 libusb_open(devs[j], &pdev);
555 libusb_free_device_list(devs, 1);
556 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
561 libusb_close(struct libusb20_device *pdev)
564 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 */
583 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
584 if (err < (int)sizeof(dummy)) {
585 /* ignore error, if any */
586 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
591 libusb_get_device(struct libusb20_device *pdev)
595 return ((libusb_device *)pdev->privLuData);
599 libusb_get_configuration(struct libusb20_device *pdev, int *config)
601 struct libusb20_config *pconf;
603 if (pdev == NULL || config == NULL)
604 return (LIBUSB_ERROR_INVALID_PARAM);
606 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
608 return (LIBUSB_ERROR_NO_MEM);
610 *config = pconf->desc.bConfigurationValue;
618 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
620 struct libusb20_config *pconf;
621 struct libusb_device *dev;
625 dev = libusb_get_device(pdev);
627 return (LIBUSB_ERROR_INVALID_PARAM);
629 if (configuration < 1) {
633 for (i = 0; i != 255; i++) {
636 pconf = libusb20_dev_alloc_config(pdev, i);
638 return (LIBUSB_ERROR_INVALID_PARAM);
639 found = (pconf->desc.bConfigurationValue
646 return (LIBUSB_ERROR_INVALID_PARAM);
651 libusb10_cancel_all_transfer(dev);
653 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
655 err = libusb20_dev_set_config_index(pdev, i);
657 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
658 POLLOUT | POLLRDNORM | POLLWRNORM);
660 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
664 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
669 dev = libusb_get_device(pdev);
671 return (LIBUSB_ERROR_INVALID_PARAM);
673 if (interface_number < 0 || interface_number > 31)
674 return (LIBUSB_ERROR_INVALID_PARAM);
676 if (pdev->auto_detach != 0) {
677 err = libusb_detach_kernel_driver(pdev, interface_number);
683 dev->claimed_interfaces |= (1 << interface_number);
684 CTX_UNLOCK(dev->ctx);
690 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
695 dev = libusb_get_device(pdev);
697 return (LIBUSB_ERROR_INVALID_PARAM);
699 if (interface_number < 0 || interface_number > 31)
700 return (LIBUSB_ERROR_INVALID_PARAM);
702 if (pdev->auto_detach != 0) {
703 err = libusb_attach_kernel_driver(pdev, interface_number);
709 if (!(dev->claimed_interfaces & (1 << interface_number)))
710 err = LIBUSB_ERROR_NOT_FOUND;
712 dev->claimed_interfaces &= ~(1 << interface_number);
713 CTX_UNLOCK(dev->ctx);
719 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
720 int interface_number, int alternate_setting)
725 dev = libusb_get_device(pdev);
727 return (LIBUSB_ERROR_INVALID_PARAM);
729 if (interface_number < 0 || interface_number > 31)
730 return (LIBUSB_ERROR_INVALID_PARAM);
733 if (!(dev->claimed_interfaces & (1 << interface_number)))
734 err = LIBUSB_ERROR_NOT_FOUND;
735 CTX_UNLOCK(dev->ctx);
740 libusb10_cancel_all_transfer(dev);
742 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
744 err = libusb20_dev_set_alt_index(pdev,
745 interface_number, alternate_setting);
747 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
748 pdev, libusb20_dev_get_fd(pdev),
749 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
751 return (err ? LIBUSB_ERROR_OTHER : 0);
754 static struct libusb20_transfer *
755 libusb10_get_transfer(struct libusb20_device *pdev,
756 uint8_t endpoint, uint8_t xfer_index)
758 xfer_index &= 1; /* double buffering */
760 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
762 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
763 /* this is an IN endpoint */
766 return (libusb20_tr_get_pointer(pdev, xfer_index));
770 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
772 struct libusb20_transfer *xfer;
773 struct libusb_device *dev;
776 xfer = libusb10_get_transfer(pdev, endpoint, 0);
778 return (LIBUSB_ERROR_INVALID_PARAM);
780 dev = libusb_get_device(pdev);
782 return (LIBUSB_ERROR_INVALID_PARAM);
785 err = libusb20_tr_open(xfer, 0, 1, endpoint);
786 CTX_UNLOCK(dev->ctx);
788 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
789 return (LIBUSB_ERROR_OTHER);
791 libusb20_tr_clear_stall_sync(xfer);
793 /* check if we opened the transfer */
796 libusb20_tr_close(xfer);
797 CTX_UNLOCK(dev->ctx);
799 return (0); /* success */
803 libusb_reset_device(struct libusb20_device *pdev)
808 dev = libusb_get_device(pdev);
810 return (LIBUSB_ERROR_INVALID_PARAM);
812 libusb10_cancel_all_transfer(dev);
814 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
816 err = libusb20_dev_reset(pdev);
818 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
819 pdev, libusb20_dev_get_fd(pdev),
820 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
822 return (err ? LIBUSB_ERROR_OTHER : 0);
826 libusb_check_connected(struct libusb20_device *pdev)
831 dev = libusb_get_device(pdev);
833 return (LIBUSB_ERROR_INVALID_PARAM);
835 err = libusb20_dev_check_connected(pdev);
837 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
841 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
844 return (LIBUSB_ERROR_INVALID_PARAM);
846 if (libusb20_dev_kernel_driver_active(pdev, interface))
847 return (0); /* no kernel driver is active */
849 return (1); /* kernel driver is active */
853 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
854 char *name, int namelen)
856 return (libusb_get_driver(pdev, interface, name, namelen));
860 libusb_get_driver(struct libusb20_device *pdev, int interface,
861 char *name, int namelen)
867 return (LIBUSB_ERROR_INVALID_PARAM);
869 return (LIBUSB_ERROR_INVALID_PARAM);
873 err = libusb20_dev_get_iface_desc(
874 pdev, interface, name, namelen);
877 return (LIBUSB_ERROR_OTHER);
879 /* we only want the driver name */
880 ptr = strstr(name, ":");
888 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
890 return (libusb_detach_kernel_driver(pdev, interface));
894 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
899 return (LIBUSB_ERROR_INVALID_PARAM);
901 err = libusb20_dev_detach_kernel_driver(
904 return (err ? LIBUSB_ERROR_OTHER : 0);
908 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
911 return (LIBUSB_ERROR_INVALID_PARAM);
912 /* stub - currently not supported by libusb20 */
917 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
919 dev->auto_detach = (enable ? 1 : 0);
923 /* Asynchronous device I/O */
925 struct libusb_transfer *
926 libusb_alloc_transfer(int iso_packets)
928 struct libusb_transfer *uxfer;
929 struct libusb_super_transfer *sxfer;
932 len = sizeof(struct libusb_transfer) +
933 sizeof(struct libusb_super_transfer) +
934 (iso_packets * sizeof(libusb_iso_packet_descriptor));
940 memset(sxfer, 0, len);
942 uxfer = (struct libusb_transfer *)(
943 ((uint8_t *)sxfer) + sizeof(*sxfer));
945 /* set default value */
946 uxfer->num_iso_packets = iso_packets;
952 libusb_free_transfer(struct libusb_transfer *uxfer)
954 struct libusb_super_transfer *sxfer;
957 return; /* be NULL safe */
959 /* check if we should free the transfer buffer */
960 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
963 sxfer = (struct libusb_super_transfer *)(
964 (uint8_t *)uxfer - sizeof(*sxfer));
970 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
974 switch (xfer->type) {
975 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
976 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
978 case LIBUSB_TRANSFER_TYPE_CONTROL:
989 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
994 usb_speed = libusb20_dev_get_speed(pdev);
996 switch (xfer->type) {
997 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
998 ret = 0; /* kernel will auto-select */
1000 case LIBUSB_TRANSFER_TYPE_CONTROL:
1004 switch (usb_speed) {
1005 case LIBUSB20_SPEED_LOW:
1008 case LIBUSB20_SPEED_FULL:
1011 case LIBUSB20_SPEED_SUPER:
1024 libusb10_convert_error(uint8_t status)
1029 case LIBUSB20_TRANSFER_START:
1030 case LIBUSB20_TRANSFER_COMPLETED:
1031 return (LIBUSB_TRANSFER_COMPLETED);
1032 case LIBUSB20_TRANSFER_OVERFLOW:
1033 return (LIBUSB_TRANSFER_OVERFLOW);
1034 case LIBUSB20_TRANSFER_NO_DEVICE:
1035 return (LIBUSB_TRANSFER_NO_DEVICE);
1036 case LIBUSB20_TRANSFER_STALL:
1037 return (LIBUSB_TRANSFER_STALL);
1038 case LIBUSB20_TRANSFER_CANCELLED:
1039 return (LIBUSB_TRANSFER_CANCELLED);
1040 case LIBUSB20_TRANSFER_TIMED_OUT:
1041 return (LIBUSB_TRANSFER_TIMED_OUT);
1043 return (LIBUSB_TRANSFER_ERROR);
1047 /* This function must be called locked */
1050 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1051 struct libusb_super_transfer *sxfer, int status)
1053 struct libusb_transfer *uxfer;
1054 struct libusb_device *dev;
1056 uxfer = (struct libusb_transfer *)(
1057 ((uint8_t *)sxfer) + sizeof(*sxfer));
1060 libusb20_tr_set_priv_sc1(pxfer, NULL);
1062 /* set transfer status */
1063 uxfer->status = status;
1065 /* update super transfer state */
1066 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1068 dev = libusb_get_device(uxfer->dev_handle);
1070 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1073 /* This function must be called locked */
1076 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1078 struct libusb_super_transfer *sxfer;
1079 struct libusb_transfer *uxfer;
1081 uint16_t iso_packets;
1086 status = libusb20_tr_get_status(pxfer);
1087 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1088 actlen = libusb20_tr_get_actual_length(pxfer);
1089 iso_packets = libusb20_tr_get_max_frames(pxfer);
1092 return; /* cancelled - nothing to do */
1094 uxfer = (struct libusb_transfer *)(
1095 ((uint8_t *)sxfer) + sizeof(*sxfer));
1097 if (iso_packets > uxfer->num_iso_packets)
1098 iso_packets = uxfer->num_iso_packets;
1100 if (iso_packets == 0)
1101 return; /* nothing to do */
1103 /* make sure that the number of ISOCHRONOUS packets is valid */
1104 uxfer->num_iso_packets = iso_packets;
1106 flags = uxfer->flags;
1109 case LIBUSB20_TRANSFER_COMPLETED:
1111 /* update actual length */
1112 uxfer->actual_length = actlen;
1113 for (i = 0; i != iso_packets; i++) {
1114 uxfer->iso_packet_desc[i].actual_length =
1115 libusb20_tr_get_length(pxfer, i);
1117 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1120 case LIBUSB20_TRANSFER_START:
1122 /* setup length(s) */
1124 for (i = 0; i != iso_packets; i++) {
1125 libusb20_tr_setup_isoc(pxfer,
1126 &uxfer->buffer[actlen],
1127 uxfer->iso_packet_desc[i].length, i);
1128 actlen += uxfer->iso_packet_desc[i].length;
1134 libusb20_tr_set_total_frames(pxfer, iso_packets);
1135 libusb20_tr_submit(pxfer);
1137 /* fork another USB transfer, if any */
1138 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1142 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1147 /* This function must be called locked */
1150 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1152 struct libusb_super_transfer *sxfer;
1153 struct libusb_transfer *uxfer;
1159 status = libusb20_tr_get_status(pxfer);
1160 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1161 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1162 actlen = libusb20_tr_get_actual_length(pxfer);
1165 return; /* cancelled - nothing to do */
1167 uxfer = (struct libusb_transfer *)(
1168 ((uint8_t *)sxfer) + sizeof(*sxfer));
1170 flags = uxfer->flags;
1173 case LIBUSB20_TRANSFER_COMPLETED:
1175 uxfer->actual_length += actlen;
1177 /* check for short packet */
1178 if (sxfer->last_len != actlen) {
1179 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1180 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1182 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1186 /* check for end of data */
1187 if (sxfer->rem_len == 0) {
1188 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1193 case LIBUSB20_TRANSFER_START:
1194 if (max_bulk > sxfer->rem_len) {
1195 max_bulk = sxfer->rem_len;
1197 /* setup new BULK or INTERRUPT transaction */
1198 libusb20_tr_setup_bulk(pxfer,
1199 sxfer->curr_data, max_bulk, uxfer->timeout);
1201 /* update counters */
1202 sxfer->last_len = max_bulk;
1203 sxfer->curr_data += max_bulk;
1204 sxfer->rem_len -= max_bulk;
1206 libusb20_tr_submit(pxfer);
1208 /* check if we can fork another USB transfer */
1209 if (sxfer->rem_len == 0)
1210 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1214 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1219 /* This function must be called locked */
1222 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1224 struct libusb_super_transfer *sxfer;
1225 struct libusb_transfer *uxfer;
1231 status = libusb20_tr_get_status(pxfer);
1232 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1233 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1234 actlen = libusb20_tr_get_actual_length(pxfer);
1237 return; /* cancelled - nothing to do */
1239 uxfer = (struct libusb_transfer *)(
1240 ((uint8_t *)sxfer) + sizeof(*sxfer));
1242 flags = uxfer->flags;
1245 case LIBUSB20_TRANSFER_COMPLETED:
1247 uxfer->actual_length += actlen;
1249 /* subtract length of SETUP packet, if any */
1250 actlen -= libusb20_tr_get_length(pxfer, 0);
1252 /* check for short packet */
1253 if (sxfer->last_len != actlen) {
1254 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1255 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1257 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1261 /* check for end of data */
1262 if (sxfer->rem_len == 0) {
1263 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1268 case LIBUSB20_TRANSFER_START:
1269 if (max_bulk > sxfer->rem_len) {
1270 max_bulk = sxfer->rem_len;
1272 /* setup new CONTROL transaction */
1273 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1274 /* next fragment - don't send SETUP packet */
1275 libusb20_tr_set_length(pxfer, 0, 0);
1277 /* first fragment - send SETUP packet */
1278 libusb20_tr_set_length(pxfer, 8, 0);
1279 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1282 if (max_bulk != 0) {
1283 libusb20_tr_set_length(pxfer, max_bulk, 1);
1284 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1285 libusb20_tr_set_total_frames(pxfer, 2);
1287 libusb20_tr_set_total_frames(pxfer, 1);
1290 /* update counters */
1291 sxfer->last_len = max_bulk;
1292 sxfer->curr_data += max_bulk;
1293 sxfer->rem_len -= max_bulk;
1295 libusb20_tr_submit(pxfer);
1297 /* check if we can fork another USB transfer */
1298 if (sxfer->rem_len == 0)
1299 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1303 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1308 /* The following function must be called locked */
1311 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1313 struct libusb20_transfer *pxfer0;
1314 struct libusb20_transfer *pxfer1;
1315 struct libusb_super_transfer *sxfer;
1316 struct libusb_transfer *uxfer;
1317 struct libusb_device *dev;
1324 dev = libusb_get_device(pdev);
1326 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1327 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1329 if (pxfer0 == NULL || pxfer1 == NULL)
1330 return; /* shouldn't happen */
1333 if (libusb20_tr_pending(pxfer0))
1335 if (libusb20_tr_pending(pxfer1))
1340 /* wait till one of the transfers complete */
1343 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1345 return; /* cancelling */
1347 return; /* cannot queue another one */
1348 /* swap transfers */
1352 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1354 return; /* cancelling */
1356 return; /* cannot queue another one */
1357 /* swap transfers */
1364 /* find next transfer on same endpoint */
1365 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1367 uxfer = (struct libusb_transfer *)(
1368 ((uint8_t *)sxfer) + sizeof(*sxfer));
1370 if (uxfer->endpoint == endpoint) {
1371 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1372 sxfer->entry.tqe_prev = NULL;
1376 return; /* success */
1380 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1381 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1383 /* reset super transfer state */
1384 sxfer->rem_len = uxfer->length;
1385 sxfer->curr_data = uxfer->buffer;
1386 uxfer->actual_length = 0;
1388 switch (uxfer->type) {
1389 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1390 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1392 case LIBUSB_TRANSFER_TYPE_BULK:
1393 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1394 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1396 case LIBUSB_TRANSFER_TYPE_CONTROL:
1397 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1398 if (sxfer->rem_len < 8)
1401 /* remove SETUP packet from data */
1402 sxfer->rem_len -= 8;
1403 sxfer->curr_data += 8;
1409 buffsize = libusb10_get_buffsize(pdev, uxfer);
1410 maxframe = libusb10_get_maxframe(pdev, uxfer);
1412 /* make sure the transfer is opened */
1413 err = libusb20_tr_open_stream(pxfer0, buffsize, maxframe,
1414 endpoint, sxfer->stream_id);
1415 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1418 libusb20_tr_start(pxfer0);
1422 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1424 /* make sure our event loop spins the done handler */
1426 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1429 /* The following function must be called unlocked */
1432 libusb_submit_transfer(struct libusb_transfer *uxfer)
1434 struct libusb20_transfer *pxfer0;
1435 struct libusb20_transfer *pxfer1;
1436 struct libusb_super_transfer *sxfer;
1437 struct libusb_device *dev;
1442 return (LIBUSB_ERROR_INVALID_PARAM);
1444 if (uxfer->dev_handle == NULL)
1445 return (LIBUSB_ERROR_INVALID_PARAM);
1447 endpoint = uxfer->endpoint;
1449 dev = libusb_get_device(uxfer->dev_handle);
1451 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1453 sxfer = (struct libusb_super_transfer *)(
1454 (uint8_t *)uxfer - sizeof(*sxfer));
1458 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1459 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1461 if (pxfer0 == NULL || pxfer1 == NULL) {
1462 err = LIBUSB_ERROR_OTHER;
1463 } else if ((sxfer->entry.tqe_prev != NULL) ||
1464 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1465 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1466 err = LIBUSB_ERROR_BUSY;
1469 /* set pending state */
1470 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1472 /* insert transfer into transfer head list */
1473 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1475 /* start work transfers */
1476 libusb10_submit_transfer_sub(
1477 uxfer->dev_handle, endpoint);
1479 err = 0; /* success */
1482 CTX_UNLOCK(dev->ctx);
1484 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1489 /* Asynchronous transfer cancel */
1492 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1494 struct libusb20_transfer *pxfer0;
1495 struct libusb20_transfer *pxfer1;
1496 struct libusb_super_transfer *sxfer;
1497 struct libusb_device *dev;
1502 return (LIBUSB_ERROR_INVALID_PARAM);
1504 /* check if not initialised */
1505 if (uxfer->dev_handle == NULL)
1506 return (LIBUSB_ERROR_NOT_FOUND);
1508 endpoint = uxfer->endpoint;
1510 dev = libusb_get_device(uxfer->dev_handle);
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(uxfer->dev_handle, endpoint, 0);
1522 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, 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 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1536 retval = LIBUSB_ERROR_NOT_FOUND;
1537 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1538 libusb10_complete_transfer(pxfer0,
1539 sxfer, LIBUSB_TRANSFER_CANCELLED);
1540 libusb20_tr_stop(pxfer0);
1541 /* make sure the queue doesn't stall */
1542 libusb10_submit_transfer_sub(
1543 uxfer->dev_handle, endpoint);
1544 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1545 libusb10_complete_transfer(pxfer1,
1546 sxfer, LIBUSB_TRANSFER_CANCELLED);
1547 libusb20_tr_stop(pxfer1);
1548 /* make sure the queue doesn't stall */
1549 libusb10_submit_transfer_sub(
1550 uxfer->dev_handle, endpoint);
1553 retval = LIBUSB_ERROR_NOT_FOUND;
1556 CTX_UNLOCK(dev->ctx);
1558 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1564 libusb10_cancel_all_transfer(libusb_device *dev)
1566 struct libusb20_device *pdev = dev->os_priv;
1569 for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1570 struct libusb20_transfer *xfer;
1572 xfer = libusb20_tr_get_pointer(pdev, x);
1575 libusb20_tr_close(xfer);
1580 libusb_cpu_to_le16(uint16_t x)
1582 return (htole16(x));
1586 libusb_le16_to_cpu(uint16_t x)
1588 return (le16toh(x));
1592 libusb_strerror(int code)
1595 case LIBUSB_SUCCESS:
1597 case LIBUSB_ERROR_IO:
1598 return ("I/O error");
1599 case LIBUSB_ERROR_INVALID_PARAM:
1600 return ("Invalid parameter");
1601 case LIBUSB_ERROR_ACCESS:
1602 return ("Permissions error");
1603 case LIBUSB_ERROR_NO_DEVICE:
1604 return ("No device");
1605 case LIBUSB_ERROR_NOT_FOUND:
1606 return ("Not found");
1607 case LIBUSB_ERROR_BUSY:
1608 return ("Device busy");
1609 case LIBUSB_ERROR_TIMEOUT:
1611 case LIBUSB_ERROR_OVERFLOW:
1612 return ("Overflow");
1613 case LIBUSB_ERROR_PIPE:
1614 return ("Pipe error");
1615 case LIBUSB_ERROR_INTERRUPTED:
1616 return ("Interrupted");
1617 case LIBUSB_ERROR_NO_MEM:
1618 return ("Out of memory");
1619 case LIBUSB_ERROR_NOT_SUPPORTED:
1620 return ("Not supported");
1621 case LIBUSB_ERROR_OTHER:
1622 return ("Other error");
1624 return ("Unknown error");
1629 libusb_error_name(int code)
1632 case LIBUSB_SUCCESS:
1633 return ("LIBUSB_SUCCESS");
1634 case LIBUSB_ERROR_IO:
1635 return ("LIBUSB_ERROR_IO");
1636 case LIBUSB_ERROR_INVALID_PARAM:
1637 return ("LIBUSB_ERROR_INVALID_PARAM");
1638 case LIBUSB_ERROR_ACCESS:
1639 return ("LIBUSB_ERROR_ACCESS");
1640 case LIBUSB_ERROR_NO_DEVICE:
1641 return ("LIBUSB_ERROR_NO_DEVICE");
1642 case LIBUSB_ERROR_NOT_FOUND:
1643 return ("LIBUSB_ERROR_NOT_FOUND");
1644 case LIBUSB_ERROR_BUSY:
1645 return ("LIBUSB_ERROR_BUSY");
1646 case LIBUSB_ERROR_TIMEOUT:
1647 return ("LIBUSB_ERROR_TIMEOUT");
1648 case LIBUSB_ERROR_OVERFLOW:
1649 return ("LIBUSB_ERROR_OVERFLOW");
1650 case LIBUSB_ERROR_PIPE:
1651 return ("LIBUSB_ERROR_PIPE");
1652 case LIBUSB_ERROR_INTERRUPTED:
1653 return ("LIBUSB_ERROR_INTERRUPTED");
1654 case LIBUSB_ERROR_NO_MEM:
1655 return ("LIBUSB_ERROR_NO_MEM");
1656 case LIBUSB_ERROR_NOT_SUPPORTED:
1657 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1658 case LIBUSB_ERROR_OTHER:
1659 return ("LIBUSB_ERROR_OTHER");
1661 return ("LIBUSB_ERROR_UNKNOWN");