3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/queue.h>
40 #define libusb_device_handle libusb20_device
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
48 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
49 struct libusb_context *usbi_default_context = NULL;
53 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
54 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_convert_error(uint8_t status);
56 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57 static void libusb10_isoc_proxy(struct libusb20_transfer *);
58 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
62 /* Library initialisation / deinitialisation */
65 libusb_set_debug(libusb_context *ctx, int level)
67 ctx = GET_CONTEXT(ctx);
73 libusb_set_nonblocking(int f)
78 * We ignore any failures in this function, hence the
79 * non-blocking flag is not critical to the operation of
80 * libUSB. We use F_GETFL and F_SETFL to be compatible with
84 flags = fcntl(f, F_GETFL, NULL);
88 fcntl(f, F_SETFL, flags);
92 libusb_init(libusb_context **context)
94 struct libusb_context *ctx;
98 ctx = malloc(sizeof(*ctx));
100 return (LIBUSB_ERROR_INVALID_PARAM);
102 memset(ctx, 0, sizeof(*ctx));
104 debug = getenv("LIBUSB_DEBUG");
106 ctx->debug = atoi(debug);
108 ctx->debug_fixed = 1;
110 TAILQ_INIT(&ctx->pollfds);
111 TAILQ_INIT(&ctx->tr_done);
113 pthread_mutex_init(&ctx->ctx_lock, NULL);
114 pthread_cond_init(&ctx->ctx_cond, NULL);
116 ctx->ctx_handler = NO_THREAD;
118 ret = pipe(ctx->ctrl_pipe);
120 pthread_mutex_destroy(&ctx->ctx_lock);
121 pthread_cond_destroy(&ctx->ctx_cond);
123 return (LIBUSB_ERROR_OTHER);
125 /* set non-blocking mode on the control pipe to avoid deadlock */
126 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
127 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
129 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
131 pthread_mutex_lock(&default_context_lock);
132 if (usbi_default_context == NULL) {
133 usbi_default_context = ctx;
135 pthread_mutex_unlock(&default_context_lock);
140 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
146 libusb_exit(libusb_context *ctx)
148 ctx = GET_CONTEXT(ctx);
153 /* XXX cleanup devices */
155 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
156 close(ctx->ctrl_pipe[0]);
157 close(ctx->ctrl_pipe[1]);
158 pthread_mutex_destroy(&ctx->ctx_lock);
159 pthread_cond_destroy(&ctx->ctx_cond);
161 pthread_mutex_lock(&default_context_lock);
162 if (ctx == usbi_default_context) {
163 usbi_default_context = NULL;
165 pthread_mutex_unlock(&default_context_lock);
170 /* Device handling and initialisation. */
173 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
175 struct libusb20_backend *usb_backend;
176 struct libusb20_device *pdev;
177 struct libusb_device *dev;
180 ctx = GET_CONTEXT(ctx);
183 return (LIBUSB_ERROR_INVALID_PARAM);
186 return (LIBUSB_ERROR_INVALID_PARAM);
188 usb_backend = libusb20_be_alloc_default();
189 if (usb_backend == NULL)
190 return (LIBUSB_ERROR_NO_MEM);
192 /* figure out how many USB devices are present */
195 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
198 /* allocate device pointer list */
199 *list = malloc((i + 1) * sizeof(void *));
201 libusb20_be_free(usb_backend);
202 return (LIBUSB_ERROR_NO_MEM);
204 /* create libusb v1.0 compliant devices */
206 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
208 dev = malloc(sizeof(*dev));
211 libusb_unref_device((*list)[i - 1]);
216 libusb20_be_free(usb_backend);
217 return (LIBUSB_ERROR_NO_MEM);
220 /* get device into libUSB v1.0 list */
221 libusb20_be_dequeue_device(usb_backend, pdev);
223 memset(dev, 0, sizeof(*dev));
225 /* init transfer queues */
226 TAILQ_INIT(&dev->tr_head);
228 /* set context we belong to */
231 /* link together the two structures */
233 pdev->privLuData = dev;
235 (*list)[i] = libusb_ref_device(dev);
240 libusb20_be_free(usb_backend);
245 libusb_free_device_list(libusb_device **list, int unref_devices)
250 return; /* be NULL safe */
253 for (i = 0; list[i] != NULL; i++)
254 libusb_unref_device(list[i]);
260 libusb_get_bus_number(libusb_device *dev)
263 return (0); /* should not happen */
264 return (libusb20_dev_get_bus_number(dev->os_priv));
268 libusb_get_device_address(libusb_device *dev)
271 return (0); /* should not happen */
272 return (libusb20_dev_get_address(dev->os_priv));
276 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
278 struct libusb_config_descriptor *pdconf;
279 struct libusb_interface *pinf;
280 struct libusb_interface_descriptor *pdinf;
281 struct libusb_endpoint_descriptor *pdend;
288 return (LIBUSB_ERROR_NO_DEVICE);
290 ret = libusb_get_active_config_descriptor(dev, &pdconf);
294 ret = LIBUSB_ERROR_NOT_FOUND;
295 for (i = 0; i < pdconf->bNumInterfaces; i++) {
296 pinf = &pdconf->interface[i];
297 for (j = 0; j < pinf->num_altsetting; j++) {
298 pdinf = &pinf->altsetting[j];
299 for (k = 0; k < pdinf->bNumEndpoints; k++) {
300 pdend = &pdinf->endpoint[k];
301 if (pdend->bEndpointAddress == endpoint) {
302 ret = pdend->wMaxPacketSize;
310 libusb_free_config_descriptor(pdconf);
315 libusb_ref_device(libusb_device *dev)
318 return (NULL); /* be NULL safe */
322 CTX_UNLOCK(dev->ctx);
328 libusb_unref_device(libusb_device *dev)
331 return; /* be NULL safe */
335 CTX_UNLOCK(dev->ctx);
337 if (dev->refcnt == 0) {
338 libusb20_dev_free(dev->os_priv);
344 libusb_open(libusb_device *dev, libusb_device_handle **devh)
346 libusb_context *ctx = dev->ctx;
347 struct libusb20_device *pdev = dev->os_priv;
352 return (LIBUSB_ERROR_INVALID_PARAM);
354 /* set default device handle value */
357 dev = libusb_ref_device(dev);
359 return (LIBUSB_ERROR_INVALID_PARAM);
361 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
363 libusb_unref_device(dev);
364 return (LIBUSB_ERROR_NO_MEM);
366 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
367 POLLOUT | POLLRDNORM | POLLWRNORM);
369 /* make sure our event loop detects the new device */
371 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
372 if (err < (int)sizeof(dummy)) {
373 /* ignore error, if any */
374 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
381 libusb_device_handle *
382 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
385 struct libusb_device **devs;
386 struct libusb20_device *pdev;
387 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
391 ctx = GET_CONTEXT(ctx);
393 return (NULL); /* be NULL safe */
395 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
397 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
400 for (j = 0; j < i; j++) {
401 pdev = devs[j]->os_priv;
402 pdesc = libusb20_dev_get_device_desc(pdev);
404 * NOTE: The USB library will automatically swap the
405 * fields in the device descriptor to be of host
408 if (pdesc->idVendor == vendor_id &&
409 pdesc->idProduct == product_id) {
410 if (libusb_open(devs[j], &pdev) < 0)
418 libusb_free_device_list(devs, 1);
419 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
424 libusb_close(struct libusb20_device *pdev)
427 struct libusb_device *dev;
432 return; /* be NULL safe */
434 dev = libusb_get_device(pdev);
437 libusb10_remove_pollfd(ctx, &dev->dev_poll);
439 libusb20_dev_close(pdev);
441 /* unref will free the "pdev" when the refcount reaches zero */
442 libusb_unref_device(dev);
444 /* make sure our event loop detects the closed device */
446 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
447 if (err < (int)sizeof(dummy)) {
448 /* ignore error, if any */
449 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
454 libusb_get_device(struct libusb20_device *pdev)
458 return ((libusb_device *)pdev->privLuData);
462 libusb_get_configuration(struct libusb20_device *pdev, int *config)
464 struct libusb20_config *pconf;
466 if (pdev == NULL || config == NULL)
467 return (LIBUSB_ERROR_INVALID_PARAM);
469 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
471 return (LIBUSB_ERROR_NO_MEM);
473 *config = pconf->desc.bConfigurationValue;
481 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
483 struct libusb20_config *pconf;
484 struct libusb_device *dev;
488 dev = libusb_get_device(pdev);
490 return (LIBUSB_ERROR_INVALID_PARAM);
492 if (configuration < 1) {
496 for (i = 0; i != 255; i++) {
499 pconf = libusb20_dev_alloc_config(pdev, i);
501 return (LIBUSB_ERROR_INVALID_PARAM);
502 found = (pconf->desc.bConfigurationValue
509 return (LIBUSB_ERROR_INVALID_PARAM);
514 libusb10_cancel_all_transfer(dev);
516 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
518 err = libusb20_dev_set_config_index(pdev, i);
520 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
521 POLLOUT | POLLRDNORM | POLLWRNORM);
523 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
527 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
532 dev = libusb_get_device(pdev);
534 return (LIBUSB_ERROR_INVALID_PARAM);
536 if (interface_number < 0 || interface_number > 31)
537 return (LIBUSB_ERROR_INVALID_PARAM);
540 if (dev->claimed_interfaces & (1 << interface_number))
541 err = LIBUSB_ERROR_BUSY;
544 dev->claimed_interfaces |= (1 << interface_number);
545 CTX_UNLOCK(dev->ctx);
550 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
555 dev = libusb_get_device(pdev);
557 return (LIBUSB_ERROR_INVALID_PARAM);
559 if (interface_number < 0 || interface_number > 31)
560 return (LIBUSB_ERROR_INVALID_PARAM);
563 if (!(dev->claimed_interfaces & (1 << interface_number)))
564 err = LIBUSB_ERROR_NOT_FOUND;
567 dev->claimed_interfaces &= ~(1 << interface_number);
568 CTX_UNLOCK(dev->ctx);
573 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
574 int interface_number, int alternate_setting)
579 dev = libusb_get_device(pdev);
581 return (LIBUSB_ERROR_INVALID_PARAM);
583 if (interface_number < 0 || interface_number > 31)
584 return (LIBUSB_ERROR_INVALID_PARAM);
587 if (!(dev->claimed_interfaces & (1 << interface_number)))
588 err = LIBUSB_ERROR_NOT_FOUND;
589 CTX_UNLOCK(dev->ctx);
594 libusb10_cancel_all_transfer(dev);
596 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
598 err = libusb20_dev_set_alt_index(pdev,
599 interface_number, alternate_setting);
601 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
602 pdev, libusb20_dev_get_fd(pdev),
603 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
605 return (err ? LIBUSB_ERROR_OTHER : 0);
608 static struct libusb20_transfer *
609 libusb10_get_transfer(struct libusb20_device *pdev,
610 uint8_t endpoint, uint8_t index)
612 index &= 1; /* double buffering */
614 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
616 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
617 /* this is an IN endpoint */
620 return (libusb20_tr_get_pointer(pdev, index));
624 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
626 struct libusb20_transfer *xfer;
627 struct libusb_device *dev;
630 xfer = libusb10_get_transfer(pdev, endpoint, 0);
632 return (LIBUSB_ERROR_INVALID_PARAM);
634 dev = libusb_get_device(pdev);
636 return (LIBUSB_ERROR_INVALID_PARAM);
639 err = libusb20_tr_open(xfer, 0, 0, endpoint);
640 CTX_UNLOCK(dev->ctx);
642 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
643 return (LIBUSB_ERROR_OTHER);
645 libusb20_tr_clear_stall_sync(xfer);
647 /* check if we opened the transfer */
650 libusb20_tr_close(xfer);
651 CTX_UNLOCK(dev->ctx);
653 return (0); /* success */
657 libusb_reset_device(struct libusb20_device *pdev)
662 dev = libusb_get_device(pdev);
664 return (LIBUSB_ERROR_INVALID_PARAM);
666 libusb10_cancel_all_transfer(dev);
668 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
670 err = libusb20_dev_reset(pdev);
672 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
673 pdev, libusb20_dev_get_fd(pdev),
674 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
676 return (err ? LIBUSB_ERROR_OTHER : 0);
680 libusb_check_connected(struct libusb20_device *pdev)
685 dev = libusb_get_device(pdev);
687 return (LIBUSB_ERROR_INVALID_PARAM);
689 err = libusb20_dev_check_connected(pdev);
691 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
695 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
698 return (LIBUSB_ERROR_INVALID_PARAM);
700 return (libusb20_dev_kernel_driver_active(
705 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
706 char *name, int namelen)
708 return (libusb_get_driver(pdev, interface, name, namelen));
712 libusb_get_driver(struct libusb20_device *pdev, int interface,
713 char *name, int namelen)
719 return (LIBUSB_ERROR_INVALID_PARAM);
721 return (LIBUSB_ERROR_INVALID_PARAM);
723 err = libusb20_dev_get_iface_desc(
724 pdev, interface, name, namelen);
727 return (LIBUSB_ERROR_OTHER);
729 /* we only want the driver name */
730 ptr = strstr(name, ":");
738 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
740 return (libusb_detach_kernel_driver(pdev, interface));
744 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
749 return (LIBUSB_ERROR_INVALID_PARAM);
751 err = libusb20_dev_detach_kernel_driver(
754 return (err ? LIBUSB_ERROR_OTHER : 0);
758 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
761 return (LIBUSB_ERROR_INVALID_PARAM);
762 /* stub - currently not supported by libusb20 */
766 /* Asynchronous device I/O */
768 struct libusb_transfer *
769 libusb_alloc_transfer(int iso_packets)
771 struct libusb_transfer *uxfer;
772 struct libusb_super_transfer *sxfer;
775 len = sizeof(struct libusb_transfer) +
776 sizeof(struct libusb_super_transfer) +
777 (iso_packets * sizeof(libusb_iso_packet_descriptor));
783 memset(sxfer, 0, len);
785 uxfer = (struct libusb_transfer *)(
786 ((uint8_t *)sxfer) + sizeof(*sxfer));
788 /* set default value */
789 uxfer->num_iso_packets = iso_packets;
795 libusb_free_transfer(struct libusb_transfer *uxfer)
797 struct libusb_super_transfer *sxfer;
800 return; /* be NULL safe */
802 /* check if we should free the transfer buffer */
803 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
806 sxfer = (struct libusb_super_transfer *)(
807 (uint8_t *)uxfer - sizeof(*sxfer));
813 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
817 switch (xfer->type) {
818 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
819 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
821 case LIBUSB_TRANSFER_TYPE_CONTROL:
832 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
837 usb_speed = libusb20_dev_get_speed(pdev);
839 switch (xfer->type) {
840 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
841 ret = 0; /* kernel will auto-select */
843 case LIBUSB_TRANSFER_TYPE_CONTROL:
848 case LIBUSB20_SPEED_LOW:
851 case LIBUSB20_SPEED_FULL:
864 libusb10_convert_error(uint8_t status)
869 case LIBUSB20_TRANSFER_START:
870 case LIBUSB20_TRANSFER_COMPLETED:
871 return (LIBUSB_TRANSFER_COMPLETED);
872 case LIBUSB20_TRANSFER_OVERFLOW:
873 return (LIBUSB_TRANSFER_OVERFLOW);
874 case LIBUSB20_TRANSFER_NO_DEVICE:
875 return (LIBUSB_TRANSFER_NO_DEVICE);
876 case LIBUSB20_TRANSFER_STALL:
877 return (LIBUSB_TRANSFER_STALL);
878 case LIBUSB20_TRANSFER_CANCELLED:
879 return (LIBUSB_TRANSFER_CANCELLED);
880 case LIBUSB20_TRANSFER_TIMED_OUT:
881 return (LIBUSB_TRANSFER_TIMED_OUT);
883 return (LIBUSB_TRANSFER_ERROR);
887 /* This function must be called locked */
890 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
891 struct libusb_super_transfer *sxfer, int status)
893 struct libusb_transfer *uxfer;
894 struct libusb_device *dev;
896 uxfer = (struct libusb_transfer *)(
897 ((uint8_t *)sxfer) + sizeof(*sxfer));
900 libusb20_tr_set_priv_sc1(pxfer, NULL);
902 /* set transfer status */
903 uxfer->status = status;
905 /* update super transfer state */
906 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
908 dev = libusb_get_device(uxfer->dev_handle);
910 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
913 /* This function must be called locked */
916 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
918 struct libusb_super_transfer *sxfer;
919 struct libusb_transfer *uxfer;
921 uint16_t iso_packets;
926 status = libusb20_tr_get_status(pxfer);
927 sxfer = libusb20_tr_get_priv_sc1(pxfer);
928 actlen = libusb20_tr_get_actual_length(pxfer);
929 iso_packets = libusb20_tr_get_max_frames(pxfer);
932 return; /* cancelled - nothing to do */
934 uxfer = (struct libusb_transfer *)(
935 ((uint8_t *)sxfer) + sizeof(*sxfer));
937 if (iso_packets > uxfer->num_iso_packets)
938 iso_packets = uxfer->num_iso_packets;
940 if (iso_packets == 0)
941 return; /* nothing to do */
943 /* make sure that the number of ISOCHRONOUS packets is valid */
944 uxfer->num_iso_packets = iso_packets;
946 flags = uxfer->flags;
949 case LIBUSB20_TRANSFER_COMPLETED:
951 /* update actual length */
952 uxfer->actual_length = actlen;
953 for (i = 0; i != iso_packets; i++) {
954 uxfer->iso_packet_desc[i].actual_length =
955 libusb20_tr_get_length(pxfer, i);
957 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
960 case LIBUSB20_TRANSFER_START:
962 /* setup length(s) */
964 for (i = 0; i != iso_packets; i++) {
965 libusb20_tr_setup_isoc(pxfer,
966 &uxfer->buffer[actlen],
967 uxfer->iso_packet_desc[i].length, i);
968 actlen += uxfer->iso_packet_desc[i].length;
974 libusb20_tr_set_total_frames(pxfer, iso_packets);
975 libusb20_tr_submit(pxfer);
977 /* fork another USB transfer, if any */
978 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
982 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
987 /* This function must be called locked */
990 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
992 struct libusb_super_transfer *sxfer;
993 struct libusb_transfer *uxfer;
999 status = libusb20_tr_get_status(pxfer);
1000 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1001 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1002 actlen = libusb20_tr_get_actual_length(pxfer);
1005 return; /* cancelled - nothing to do */
1007 uxfer = (struct libusb_transfer *)(
1008 ((uint8_t *)sxfer) + sizeof(*sxfer));
1010 flags = uxfer->flags;
1013 case LIBUSB20_TRANSFER_COMPLETED:
1015 uxfer->actual_length += actlen;
1017 /* check for short packet */
1018 if (sxfer->last_len != actlen) {
1019 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1020 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1022 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1026 /* check for end of data */
1027 if (sxfer->rem_len == 0) {
1028 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1033 case LIBUSB20_TRANSFER_START:
1034 if (max_bulk > sxfer->rem_len) {
1035 max_bulk = sxfer->rem_len;
1037 /* setup new BULK or INTERRUPT transaction */
1038 libusb20_tr_setup_bulk(pxfer,
1039 sxfer->curr_data, max_bulk, uxfer->timeout);
1041 /* update counters */
1042 sxfer->last_len = max_bulk;
1043 sxfer->curr_data += max_bulk;
1044 sxfer->rem_len -= max_bulk;
1046 libusb20_tr_submit(pxfer);
1048 /* check if we can fork another USB transfer */
1049 if (sxfer->rem_len == 0)
1050 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1054 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1059 /* This function must be called locked */
1062 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1064 struct libusb_super_transfer *sxfer;
1065 struct libusb_transfer *uxfer;
1071 status = libusb20_tr_get_status(pxfer);
1072 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1073 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1074 actlen = libusb20_tr_get_actual_length(pxfer);
1077 return; /* cancelled - nothing to do */
1079 uxfer = (struct libusb_transfer *)(
1080 ((uint8_t *)sxfer) + sizeof(*sxfer));
1082 flags = uxfer->flags;
1085 case LIBUSB20_TRANSFER_COMPLETED:
1087 uxfer->actual_length += actlen;
1089 /* subtract length of SETUP packet, if any */
1090 actlen -= libusb20_tr_get_length(pxfer, 0);
1092 /* check for short packet */
1093 if (sxfer->last_len != actlen) {
1094 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1095 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1097 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1101 /* check for end of data */
1102 if (sxfer->rem_len == 0) {
1103 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1108 case LIBUSB20_TRANSFER_START:
1109 if (max_bulk > sxfer->rem_len) {
1110 max_bulk = sxfer->rem_len;
1112 /* setup new CONTROL transaction */
1113 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1114 /* next fragment - don't send SETUP packet */
1115 libusb20_tr_set_length(pxfer, 0, 0);
1117 /* first fragment - send SETUP packet */
1118 libusb20_tr_set_length(pxfer, 8, 0);
1119 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1122 if (max_bulk != 0) {
1123 libusb20_tr_set_length(pxfer, max_bulk, 1);
1124 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1125 libusb20_tr_set_total_frames(pxfer, 2);
1127 libusb20_tr_set_total_frames(pxfer, 1);
1130 /* update counters */
1131 sxfer->last_len = max_bulk;
1132 sxfer->curr_data += max_bulk;
1133 sxfer->rem_len -= max_bulk;
1135 libusb20_tr_submit(pxfer);
1137 /* check if we can fork another USB transfer */
1138 if (sxfer->rem_len == 0)
1139 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1143 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1148 /* The following function must be called locked */
1151 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1153 struct libusb20_transfer *pxfer0;
1154 struct libusb20_transfer *pxfer1;
1155 struct libusb_super_transfer *sxfer;
1156 struct libusb_transfer *uxfer;
1157 struct libusb_device *dev;
1164 dev = libusb_get_device(pdev);
1166 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1167 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1169 if (pxfer0 == NULL || pxfer1 == NULL)
1170 return; /* shouldn't happen */
1173 if (libusb20_tr_pending(pxfer0))
1175 if (libusb20_tr_pending(pxfer1))
1180 /* wait till one of the transfers complete */
1183 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1185 return; /* cancelling */
1187 return; /* cannot queue another one */
1188 /* swap transfers */
1192 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1194 return; /* cancelling */
1196 return; /* cannot queue another one */
1197 /* swap transfers */
1204 /* find next transfer on same endpoint */
1205 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1207 uxfer = (struct libusb_transfer *)(
1208 ((uint8_t *)sxfer) + sizeof(*sxfer));
1210 if (uxfer->endpoint == endpoint) {
1211 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1212 sxfer->entry.tqe_prev = NULL;
1216 return; /* success */
1220 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1221 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1223 /* reset super transfer state */
1224 sxfer->rem_len = uxfer->length;
1225 sxfer->curr_data = uxfer->buffer;
1226 uxfer->actual_length = 0;
1228 switch (uxfer->type) {
1229 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1230 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1232 case LIBUSB_TRANSFER_TYPE_BULK:
1233 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1234 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1236 case LIBUSB_TRANSFER_TYPE_CONTROL:
1237 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1238 if (sxfer->rem_len < 8)
1241 /* remove SETUP packet from data */
1242 sxfer->rem_len -= 8;
1243 sxfer->curr_data += 8;
1249 buffsize = libusb10_get_buffsize(pdev, uxfer);
1250 maxframe = libusb10_get_maxframe(pdev, uxfer);
1252 /* make sure the transfer is opened */
1253 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1254 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1257 libusb20_tr_start(pxfer0);
1261 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1263 /* make sure our event loop spins the done handler */
1265 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1268 /* The following function must be called unlocked */
1271 libusb_submit_transfer(struct libusb_transfer *uxfer)
1273 struct libusb20_transfer *pxfer0;
1274 struct libusb20_transfer *pxfer1;
1275 struct libusb_super_transfer *sxfer;
1276 struct libusb_device *dev;
1281 return (LIBUSB_ERROR_INVALID_PARAM);
1283 if (uxfer->dev_handle == NULL)
1284 return (LIBUSB_ERROR_INVALID_PARAM);
1286 endpoint = uxfer->endpoint;
1289 return (LIBUSB_ERROR_INVALID_PARAM);
1291 dev = libusb_get_device(uxfer->dev_handle);
1293 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1295 sxfer = (struct libusb_super_transfer *)(
1296 (uint8_t *)uxfer - sizeof(*sxfer));
1300 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1301 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1303 if (pxfer0 == NULL || pxfer1 == NULL) {
1304 err = LIBUSB_ERROR_OTHER;
1305 } else if ((sxfer->entry.tqe_prev != NULL) ||
1306 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1307 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1308 err = LIBUSB_ERROR_BUSY;
1311 /* set pending state */
1312 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1314 /* insert transfer into transfer head list */
1315 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1317 /* start work transfers */
1318 libusb10_submit_transfer_sub(
1319 uxfer->dev_handle, endpoint);
1321 err = 0; /* success */
1324 CTX_UNLOCK(dev->ctx);
1326 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1331 /* Asynchronous transfer cancel */
1334 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1336 struct libusb20_transfer *pxfer0;
1337 struct libusb20_transfer *pxfer1;
1338 struct libusb_super_transfer *sxfer;
1339 struct libusb_device *dev;
1344 return (LIBUSB_ERROR_INVALID_PARAM);
1346 /* check if not initialised */
1347 if (uxfer->dev_handle == NULL)
1348 return (LIBUSB_ERROR_NOT_FOUND);
1350 endpoint = uxfer->endpoint;
1353 return (LIBUSB_ERROR_INVALID_PARAM);
1355 dev = libusb_get_device(uxfer->dev_handle);
1357 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1359 sxfer = (struct libusb_super_transfer *)(
1360 (uint8_t *)uxfer - sizeof(*sxfer));
1366 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1367 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1369 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1370 /* only update the transfer status */
1371 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1372 retval = LIBUSB_ERROR_NOT_FOUND;
1373 } else if (sxfer->entry.tqe_prev != NULL) {
1374 /* we are lucky - transfer is on a queue */
1375 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1376 sxfer->entry.tqe_prev = NULL;
1377 libusb10_complete_transfer(NULL,
1378 sxfer, LIBUSB_TRANSFER_CANCELLED);
1379 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1381 retval = LIBUSB_ERROR_NOT_FOUND;
1382 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1383 libusb10_complete_transfer(pxfer0,
1384 sxfer, LIBUSB_TRANSFER_CANCELLED);
1385 libusb20_tr_stop(pxfer0);
1386 /* make sure the queue doesn't stall */
1387 libusb10_submit_transfer_sub(
1388 uxfer->dev_handle, endpoint);
1389 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1390 libusb10_complete_transfer(pxfer1,
1391 sxfer, LIBUSB_TRANSFER_CANCELLED);
1392 libusb20_tr_stop(pxfer1);
1393 /* make sure the queue doesn't stall */
1394 libusb10_submit_transfer_sub(
1395 uxfer->dev_handle, endpoint);
1398 retval = LIBUSB_ERROR_NOT_FOUND;
1401 CTX_UNLOCK(dev->ctx);
1403 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1409 libusb10_cancel_all_transfer(libusb_device *dev)
1415 libusb_cpu_to_le16(uint16_t x)
1417 return (htole16(x));
1421 libusb_le16_to_cpu(uint16_t x)
1423 return (le16toh(x));
1427 libusb_strerror(int code)
1430 return ("Unknown error");