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
35 #include <sys/ioctl.h>
36 #include <sys/filio.h>
37 #include <sys/queue.h>
38 #include <sys/endian.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_maxframe(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
56 static int libusb10_convert_error(uint8_t status);
57 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
58 static void libusb10_isoc_proxy(struct libusb20_transfer *);
59 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
60 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
61 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
63 /* Library initialisation / deinitialisation */
66 libusb_set_debug(libusb_context *ctx, int level)
68 ctx = GET_CONTEXT(ctx);
74 libusb_init(libusb_context **context)
76 struct libusb_context *ctx;
80 ctx = malloc(sizeof(*ctx));
82 return (LIBUSB_ERROR_INVALID_PARAM);
84 memset(ctx, 0, sizeof(*ctx));
86 debug = getenv("LIBUSB_DEBUG");
88 ctx->debug = atoi(debug);
92 TAILQ_INIT(&ctx->pollfds);
93 TAILQ_INIT(&ctx->tr_done);
95 pthread_mutex_init(&ctx->ctx_lock, NULL);
96 pthread_cond_init(&ctx->ctx_cond, NULL);
98 ctx->ctx_handler = NO_THREAD;
100 ret = pipe(ctx->ctrl_pipe);
102 pthread_mutex_destroy(&ctx->ctx_lock);
103 pthread_cond_destroy(&ctx->ctx_cond);
105 return (LIBUSB_ERROR_OTHER);
107 /* set non-blocking mode on the control pipe to avoid deadlock */
109 ioctl(ctx->ctrl_pipe[0], FIONBIO, &ret);
111 ioctl(ctx->ctrl_pipe[1], FIONBIO, &ret);
113 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
115 pthread_mutex_lock(&default_context_lock);
116 if (usbi_default_context == NULL) {
117 usbi_default_context = ctx;
119 pthread_mutex_unlock(&default_context_lock);
124 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
130 libusb_exit(libusb_context *ctx)
132 ctx = GET_CONTEXT(ctx);
137 /* XXX cleanup devices */
139 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
140 close(ctx->ctrl_pipe[0]);
141 close(ctx->ctrl_pipe[1]);
142 pthread_mutex_destroy(&ctx->ctx_lock);
143 pthread_cond_destroy(&ctx->ctx_cond);
145 pthread_mutex_lock(&default_context_lock);
146 if (ctx == usbi_default_context) {
147 usbi_default_context = NULL;
149 pthread_mutex_unlock(&default_context_lock);
154 /* Device handling and initialisation. */
157 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
159 struct libusb20_backend *usb_backend;
160 struct libusb20_device *pdev;
161 struct libusb_device *dev;
164 ctx = GET_CONTEXT(ctx);
167 return (LIBUSB_ERROR_INVALID_PARAM);
170 return (LIBUSB_ERROR_INVALID_PARAM);
172 usb_backend = libusb20_be_alloc_default();
173 if (usb_backend == NULL)
174 return (LIBUSB_ERROR_NO_MEM);
176 /* figure out how many USB devices are present */
179 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
182 /* allocate device pointer list */
183 *list = malloc((i + 1) * sizeof(void *));
185 libusb20_be_free(usb_backend);
186 return (LIBUSB_ERROR_NO_MEM);
188 /* create libusb v1.0 compliant devices */
190 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
192 dev = malloc(sizeof(*dev));
195 libusb_unref_device((*list)[i - 1]);
200 libusb20_be_free(usb_backend);
201 return (LIBUSB_ERROR_NO_MEM);
204 /* get device into libUSB v1.0 list */
205 libusb20_be_dequeue_device(usb_backend, pdev);
207 memset(dev, 0, sizeof(*dev));
209 /* init transfer queues */
210 TAILQ_INIT(&dev->tr_head);
212 /* set context we belong to */
215 /* link together the two structures */
217 pdev->privLuData = dev;
219 (*list)[i] = libusb_ref_device(dev);
224 libusb20_be_free(usb_backend);
229 libusb_free_device_list(libusb_device **list, int unref_devices)
234 return; /* be NULL safe */
237 for (i = 0; list[i] != NULL; i++)
238 libusb_unref_device(list[i]);
244 libusb_get_bus_number(libusb_device *dev)
247 return (0); /* should not happen */
248 return (libusb20_dev_get_bus_number(dev->os_priv));
252 libusb_get_device_address(libusb_device *dev)
255 return (0); /* should not happen */
256 return (libusb20_dev_get_address(dev->os_priv));
260 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
262 struct libusb_config_descriptor *pdconf;
263 struct libusb_interface *pinf;
264 struct libusb_interface_descriptor *pdinf;
265 struct libusb_endpoint_descriptor *pdend;
272 return (LIBUSB_ERROR_NO_DEVICE);
274 ret = libusb_get_active_config_descriptor(dev, &pdconf);
278 ret = LIBUSB_ERROR_NOT_FOUND;
279 for (i = 0; i < pdconf->bNumInterfaces; i++) {
280 pinf = &pdconf->interface[i];
281 for (j = 0; j < pinf->num_altsetting; j++) {
282 pdinf = &pinf->altsetting[j];
283 for (k = 0; k < pdinf->bNumEndpoints; k++) {
284 pdend = &pdinf->endpoint[k];
285 if (pdend->bEndpointAddress == endpoint) {
286 ret = pdend->wMaxPacketSize;
294 libusb_free_config_descriptor(pdconf);
299 libusb_ref_device(libusb_device *dev)
302 return (NULL); /* be NULL safe */
306 CTX_UNLOCK(dev->ctx);
312 libusb_unref_device(libusb_device *dev)
315 return; /* be NULL safe */
319 CTX_UNLOCK(dev->ctx);
321 if (dev->refcnt == 0) {
322 libusb20_dev_free(dev->os_priv);
328 libusb_open(libusb_device *dev, libusb_device_handle **devh)
330 libusb_context *ctx = dev->ctx;
331 struct libusb20_device *pdev = dev->os_priv;
336 return (LIBUSB_ERROR_INVALID_PARAM);
338 /* set default device handle value */
341 dev = libusb_ref_device(dev);
343 return (LIBUSB_ERROR_INVALID_PARAM);
345 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
347 libusb_unref_device(dev);
348 return (LIBUSB_ERROR_NO_MEM);
350 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
351 POLLOUT | POLLRDNORM | POLLWRNORM);
353 /* make sure our event loop detects the new device */
355 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
356 if (err < sizeof(dummy)) {
357 /* ignore error, if any */
358 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
365 libusb_device_handle *
366 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
369 struct libusb_device **devs;
370 struct libusb20_device *pdev;
371 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
375 ctx = GET_CONTEXT(ctx);
377 return (NULL); /* be NULL safe */
379 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
381 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
384 for (j = 0; j < i; j++) {
385 pdev = devs[j]->os_priv;
386 pdesc = libusb20_dev_get_device_desc(pdev);
388 * NOTE: The USB library will automatically swap the
389 * fields in the device descriptor to be of host
392 if (pdesc->idVendor == vendor_id &&
393 pdesc->idProduct == product_id) {
394 if (libusb_open(devs[j], &pdev) < 0)
402 libusb_free_device_list(devs, 1);
403 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
408 libusb_close(struct libusb20_device *pdev)
411 struct libusb_device *dev;
416 return; /* be NULL safe */
418 dev = libusb_get_device(pdev);
421 libusb10_remove_pollfd(ctx, &dev->dev_poll);
423 libusb20_dev_close(pdev);
425 /* unref will free the "pdev" when the refcount reaches zero */
426 libusb_unref_device(dev);
428 /* make sure our event loop detects the closed device */
430 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
431 if (err < sizeof(dummy)) {
432 /* ignore error, if any */
433 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
438 libusb_get_device(struct libusb20_device *pdev)
442 return ((libusb_device *)pdev->privLuData);
446 libusb_get_configuration(struct libusb20_device *pdev, int *config)
448 struct libusb20_config *pconf;
450 if (pdev == NULL || config == NULL)
451 return (LIBUSB_ERROR_INVALID_PARAM);
453 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
455 return (LIBUSB_ERROR_NO_MEM);
457 *config = pconf->desc.bConfigurationValue;
465 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
467 struct libusb20_config *pconf;
468 struct libusb_device *dev;
472 dev = libusb_get_device(pdev);
475 return (LIBUSB_ERROR_INVALID_PARAM);
477 if (configuration < 1) {
481 for (i = 0; i != 255; i++) {
484 pconf = libusb20_dev_alloc_config(pdev, i);
486 return (LIBUSB_ERROR_INVALID_PARAM);
487 found = (pconf->desc.bConfigurationValue
494 return (LIBUSB_ERROR_INVALID_PARAM);
499 libusb10_cancel_all_transfer(dev);
501 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
503 err = libusb20_dev_set_config_index(pdev, i);
505 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
506 POLLOUT | POLLRDNORM | POLLWRNORM);
508 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
512 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
517 dev = libusb_get_device(pdev);
519 return (LIBUSB_ERROR_INVALID_PARAM);
521 if (interface_number < 0 || interface_number > 31)
522 return (LIBUSB_ERROR_INVALID_PARAM);
525 if (dev->claimed_interfaces & (1 << interface_number))
526 err = LIBUSB_ERROR_BUSY;
529 dev->claimed_interfaces |= (1 << interface_number);
530 CTX_UNLOCK(dev->ctx);
535 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
540 dev = libusb_get_device(pdev);
542 return (LIBUSB_ERROR_INVALID_PARAM);
544 if (interface_number < 0 || interface_number > 31)
545 return (LIBUSB_ERROR_INVALID_PARAM);
548 if (!(dev->claimed_interfaces & (1 << interface_number)))
549 err = LIBUSB_ERROR_NOT_FOUND;
552 dev->claimed_interfaces &= ~(1 << interface_number);
553 CTX_UNLOCK(dev->ctx);
558 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
559 int interface_number, int alternate_setting)
564 dev = libusb_get_device(pdev);
566 return (LIBUSB_ERROR_INVALID_PARAM);
568 if (interface_number < 0 || interface_number > 31)
569 return (LIBUSB_ERROR_INVALID_PARAM);
572 if (!(dev->claimed_interfaces & (1 << interface_number)))
573 err = LIBUSB_ERROR_NOT_FOUND;
574 CTX_UNLOCK(dev->ctx);
579 libusb10_cancel_all_transfer(dev);
581 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
583 err = libusb20_dev_set_alt_index(pdev,
584 interface_number, alternate_setting);
586 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
587 pdev, libusb20_dev_get_fd(pdev),
588 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
590 return (err ? LIBUSB_ERROR_OTHER : 0);
593 static struct libusb20_transfer *
594 libusb10_get_transfer(struct libusb20_device *pdev,
595 uint8_t endpoint, uint8_t index)
597 index &= 1; /* double buffering */
599 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
601 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
602 /* this is an IN endpoint */
605 return (libusb20_tr_get_pointer(pdev, index));
609 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
611 struct libusb20_transfer *xfer;
612 struct libusb_device *dev;
615 xfer = libusb10_get_transfer(pdev, endpoint, 0);
617 return (LIBUSB_ERROR_INVALID_PARAM);
619 dev = libusb_get_device(pdev);
622 err = libusb20_tr_open(xfer, 0, 0, endpoint);
623 CTX_UNLOCK(dev->ctx);
625 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
626 return (LIBUSB_ERROR_OTHER);
628 libusb20_tr_clear_stall_sync(xfer);
630 /* check if we opened the transfer */
633 libusb20_tr_close(xfer);
634 CTX_UNLOCK(dev->ctx);
636 return (0); /* success */
640 libusb_reset_device(struct libusb20_device *pdev)
645 dev = libusb_get_device(pdev);
647 return (LIBUSB20_ERROR_INVALID_PARAM);
649 libusb10_cancel_all_transfer(dev);
651 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
653 err = libusb20_dev_reset(pdev);
655 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
656 pdev, libusb20_dev_get_fd(pdev),
657 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
659 return (err ? LIBUSB_ERROR_OTHER : 0);
663 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
666 return (LIBUSB_ERROR_INVALID_PARAM);
668 return (libusb20_dev_kernel_driver_active(
673 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
678 return (LIBUSB_ERROR_INVALID_PARAM);
680 err = libusb20_dev_detach_kernel_driver(
683 return (err ? LIBUSB20_ERROR_OTHER : 0);
687 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
690 return (LIBUSB_ERROR_INVALID_PARAM);
691 /* stub - currently not supported by libusb20 */
695 /* Asynchronous device I/O */
697 struct libusb_transfer *
698 libusb_alloc_transfer(int iso_packets)
700 struct libusb_transfer *uxfer;
701 struct libusb_super_transfer *sxfer;
704 len = sizeof(struct libusb_transfer) +
705 sizeof(struct libusb_super_transfer) +
706 (iso_packets * sizeof(libusb_iso_packet_descriptor));
712 memset(sxfer, 0, len);
714 uxfer = (struct libusb_transfer *)(
715 ((uint8_t *)sxfer) + sizeof(*sxfer));
717 /* set default value */
718 uxfer->num_iso_packets = iso_packets;
724 libusb_free_transfer(struct libusb_transfer *uxfer)
726 struct libusb_super_transfer *sxfer;
729 return; /* be NULL safe */
731 sxfer = (struct libusb_super_transfer *)(
732 (uint8_t *)uxfer - sizeof(*sxfer));
738 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
743 usb_speed = libusb20_dev_get_speed(pdev);
745 switch (xfer->type) {
746 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
748 case LIBUSB20_SPEED_LOW:
749 case LIBUSB20_SPEED_FULL:
757 case LIBUSB_TRANSFER_TYPE_CONTROL:
768 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
773 usb_speed = libusb20_dev_get_speed(pdev);
775 switch (xfer->type) {
776 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
777 ret = 0; /* kernel will auto-select */
779 case LIBUSB_TRANSFER_TYPE_CONTROL:
784 case LIBUSB20_SPEED_LOW:
787 case LIBUSB20_SPEED_FULL:
800 libusb10_convert_error(uint8_t status)
805 case LIBUSB20_TRANSFER_START:
806 case LIBUSB20_TRANSFER_COMPLETED:
807 return (LIBUSB_TRANSFER_COMPLETED);
808 case LIBUSB20_TRANSFER_OVERFLOW:
809 return (LIBUSB_TRANSFER_OVERFLOW);
810 case LIBUSB20_TRANSFER_NO_DEVICE:
811 return (LIBUSB_TRANSFER_NO_DEVICE);
812 case LIBUSB20_TRANSFER_STALL:
813 return (LIBUSB_TRANSFER_STALL);
814 case LIBUSB20_TRANSFER_CANCELLED:
815 return (LIBUSB_TRANSFER_CANCELLED);
816 case LIBUSB20_TRANSFER_TIMED_OUT:
817 return (LIBUSB_TRANSFER_TIMED_OUT);
819 return (LIBUSB_TRANSFER_ERROR);
823 /* This function must be called locked */
826 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
827 struct libusb_super_transfer *sxfer, int status)
829 struct libusb_transfer *uxfer;
830 struct libusb_device *dev;
832 uxfer = (struct libusb_transfer *)(
833 ((uint8_t *)sxfer) + sizeof(*sxfer));
836 libusb20_tr_set_priv_sc1(pxfer, NULL);
838 /* set transfer status */
839 uxfer->status = status;
841 /* update super transfer state */
842 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
844 dev = libusb_get_device(uxfer->dev_handle);
846 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
849 /* This function must be called locked */
852 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
854 struct libusb_super_transfer *sxfer;
855 struct libusb_transfer *uxfer;
857 uint16_t iso_packets;
862 status = libusb20_tr_get_status(pxfer);
863 sxfer = libusb20_tr_get_priv_sc1(pxfer);
864 actlen = libusb20_tr_get_actual_length(pxfer);
865 iso_packets = libusb20_tr_get_max_frames(pxfer);
868 return; /* cancelled - nothing to do */
870 uxfer = (struct libusb_transfer *)(
871 ((uint8_t *)sxfer) + sizeof(*sxfer));
873 if (iso_packets > uxfer->num_iso_packets)
874 iso_packets = uxfer->num_iso_packets;
876 if (iso_packets == 0)
877 return; /* nothing to do */
879 /* make sure that the number of ISOCHRONOUS packets is valid */
880 uxfer->num_iso_packets = iso_packets;
882 flags = uxfer->flags;
885 case LIBUSB20_TRANSFER_COMPLETED:
887 /* update actual length */
888 uxfer->actual_length = actlen;
889 for (i = 0; i != iso_packets; i++) {
890 uxfer->iso_packet_desc[i].actual_length =
891 libusb20_tr_get_length(pxfer, i);
893 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
896 case LIBUSB20_TRANSFER_START:
898 /* setup length(s) */
900 for (i = 0; i != iso_packets; i++) {
901 libusb20_tr_setup_isoc(pxfer,
902 &uxfer->buffer[actlen],
903 uxfer->iso_packet_desc[i].length, i);
904 actlen += uxfer->iso_packet_desc[i].length;
910 libusb20_tr_set_total_frames(pxfer, iso_packets);
911 libusb20_tr_submit(pxfer);
913 /* fork another USB transfer, if any */
914 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
918 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
923 /* This function must be called locked */
926 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
928 struct libusb_super_transfer *sxfer;
929 struct libusb_transfer *uxfer;
935 status = libusb20_tr_get_status(pxfer);
936 sxfer = libusb20_tr_get_priv_sc1(pxfer);
937 max_bulk = libusb20_tr_get_max_total_length(pxfer);
938 actlen = libusb20_tr_get_actual_length(pxfer);
941 return; /* cancelled - nothing to do */
943 uxfer = (struct libusb_transfer *)(
944 ((uint8_t *)sxfer) + sizeof(*sxfer));
946 flags = uxfer->flags;
949 case LIBUSB20_TRANSFER_COMPLETED:
951 uxfer->actual_length += actlen;
953 /* check for short packet */
954 if (sxfer->last_len != actlen) {
955 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
956 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
958 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
962 /* check for end of data */
963 if (sxfer->rem_len == 0) {
964 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
969 case LIBUSB20_TRANSFER_START:
970 if (max_bulk > sxfer->rem_len) {
971 max_bulk = sxfer->rem_len;
973 /* setup new BULK or INTERRUPT transaction */
974 libusb20_tr_setup_bulk(pxfer,
975 sxfer->curr_data, max_bulk, uxfer->timeout);
977 /* update counters */
978 sxfer->last_len = max_bulk;
979 sxfer->curr_data += max_bulk;
980 sxfer->rem_len -= max_bulk;
982 libusb20_tr_submit(pxfer);
984 /* check if we can fork another USB transfer */
985 if (sxfer->rem_len == 0)
986 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
990 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
995 /* This function must be called locked */
998 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1000 struct libusb_super_transfer *sxfer;
1001 struct libusb_transfer *uxfer;
1007 status = libusb20_tr_get_status(pxfer);
1008 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1009 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1010 actlen = libusb20_tr_get_actual_length(pxfer);
1013 return; /* cancelled - nothing to do */
1015 uxfer = (struct libusb_transfer *)(
1016 ((uint8_t *)sxfer) + sizeof(*sxfer));
1018 flags = uxfer->flags;
1021 case LIBUSB20_TRANSFER_COMPLETED:
1023 uxfer->actual_length += actlen;
1025 /* subtract length of SETUP packet, if any */
1026 actlen -= libusb20_tr_get_length(pxfer, 0);
1028 /* check for short packet */
1029 if (sxfer->last_len != actlen) {
1030 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1031 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1033 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1037 /* check for end of data */
1038 if (sxfer->rem_len == 0) {
1039 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1044 case LIBUSB20_TRANSFER_START:
1045 if (max_bulk > sxfer->rem_len) {
1046 max_bulk = sxfer->rem_len;
1048 /* setup new CONTROL transaction */
1049 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1050 /* next fragment - don't send SETUP packet */
1051 libusb20_tr_set_length(pxfer, 0, 0);
1053 /* first fragment - send SETUP packet */
1054 libusb20_tr_set_length(pxfer, 8, 0);
1055 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1058 if (max_bulk != 0) {
1059 libusb20_tr_set_length(pxfer, max_bulk, 1);
1060 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1061 libusb20_tr_set_total_frames(pxfer, 2);
1063 libusb20_tr_set_total_frames(pxfer, 1);
1066 /* update counters */
1067 sxfer->last_len = max_bulk;
1068 sxfer->curr_data += max_bulk;
1069 sxfer->rem_len -= max_bulk;
1071 libusb20_tr_submit(pxfer);
1073 /* check if we can fork another USB transfer */
1074 if (sxfer->rem_len == 0)
1075 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1079 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1084 /* The following function must be called locked */
1087 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1089 struct libusb20_transfer *pxfer0;
1090 struct libusb20_transfer *pxfer1;
1091 struct libusb_super_transfer *sxfer;
1092 struct libusb_transfer *uxfer;
1093 struct libusb_device *dev;
1100 dev = libusb_get_device(pdev);
1102 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1103 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1105 if (pxfer0 == NULL || pxfer1 == NULL)
1106 return; /* shouldn't happen */
1109 if (libusb20_tr_pending(pxfer0))
1111 if (libusb20_tr_pending(pxfer1))
1116 /* wait till one of the transfers complete */
1119 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1121 return; /* cancelling */
1123 return; /* cannot queue another one */
1124 /* swap transfers */
1128 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1130 return; /* cancelling */
1132 return; /* cannot queue another one */
1133 /* swap transfers */
1140 /* find next transfer on same endpoint */
1141 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1143 uxfer = (struct libusb_transfer *)(
1144 ((uint8_t *)sxfer) + sizeof(*sxfer));
1146 if (uxfer->endpoint == endpoint) {
1147 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1148 sxfer->entry.tqe_prev = NULL;
1152 return; /* success */
1156 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1157 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1159 /* reset super transfer state */
1160 sxfer->rem_len = uxfer->length;
1161 sxfer->curr_data = uxfer->buffer;
1162 uxfer->actual_length = 0;
1164 switch (uxfer->type) {
1165 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1166 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1168 case LIBUSB_TRANSFER_TYPE_BULK:
1169 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1170 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1172 case LIBUSB_TRANSFER_TYPE_CONTROL:
1173 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1174 if (sxfer->rem_len < 8)
1177 /* remove SETUP packet from data */
1178 sxfer->rem_len -= 8;
1179 sxfer->curr_data += 8;
1185 buffsize = libusb10_get_buffsize(pdev, uxfer);
1186 maxframe = libusb10_get_maxframe(pdev, uxfer);
1188 /* make sure the transfer is opened */
1189 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1190 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1193 libusb20_tr_start(pxfer0);
1197 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1199 /* make sure our event loop spins the done handler */
1201 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1204 /* The following function must be called unlocked */
1207 libusb_submit_transfer(struct libusb_transfer *uxfer)
1209 struct libusb20_transfer *pxfer0;
1210 struct libusb20_transfer *pxfer1;
1211 struct libusb_super_transfer *sxfer;
1212 struct libusb_device *dev;
1217 return (LIBUSB_ERROR_INVALID_PARAM);
1219 if (uxfer->dev_handle == NULL)
1220 return (LIBUSB_ERROR_INVALID_PARAM);
1222 endpoint = uxfer->endpoint;
1225 return (LIBUSB_ERROR_INVALID_PARAM);
1227 dev = libusb_get_device(uxfer->dev_handle);
1229 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1231 sxfer = (struct libusb_super_transfer *)(
1232 (uint8_t *)uxfer - sizeof(*sxfer));
1236 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1237 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1239 if (pxfer0 == NULL || pxfer1 == NULL) {
1240 err = LIBUSB_ERROR_OTHER;
1241 } else if ((sxfer->entry.tqe_prev != NULL) ||
1242 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1243 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1244 err = LIBUSB_ERROR_BUSY;
1247 /* set pending state */
1248 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1250 /* insert transfer into transfer head list */
1251 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1253 /* start work transfers */
1254 libusb10_submit_transfer_sub(
1255 uxfer->dev_handle, endpoint);
1257 err = 0; /* success */
1260 CTX_UNLOCK(dev->ctx);
1262 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1267 /* Asynchronous transfer cancel */
1270 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1272 struct libusb20_transfer *pxfer0;
1273 struct libusb20_transfer *pxfer1;
1274 struct libusb_super_transfer *sxfer;
1275 struct libusb_device *dev;
1280 return (LIBUSB_ERROR_INVALID_PARAM);
1282 /* check if not initialised */
1283 if (uxfer->dev_handle == NULL)
1284 return (LIBUSB_ERROR_NOT_FOUND);
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_cancel_transfer enter");
1295 sxfer = (struct libusb_super_transfer *)(
1296 (uint8_t *)uxfer - sizeof(*sxfer));
1302 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1303 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1305 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1306 /* only update the transfer status */
1307 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1308 retval = LIBUSB_ERROR_NOT_FOUND;
1309 } else if (sxfer->entry.tqe_prev != NULL) {
1310 /* we are lucky - transfer is on a queue */
1311 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1312 sxfer->entry.tqe_prev = NULL;
1313 libusb10_complete_transfer(NULL,
1314 sxfer, LIBUSB_TRANSFER_CANCELLED);
1315 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1317 retval = LIBUSB_ERROR_NOT_FOUND;
1318 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1319 libusb10_complete_transfer(pxfer0,
1320 sxfer, LIBUSB_TRANSFER_CANCELLED);
1321 libusb20_tr_stop(pxfer0);
1322 /* make sure the queue doesn't stall */
1323 libusb10_submit_transfer_sub(
1324 uxfer->dev_handle, endpoint);
1325 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1326 libusb10_complete_transfer(pxfer1,
1327 sxfer, LIBUSB_TRANSFER_CANCELLED);
1328 libusb20_tr_stop(pxfer1);
1329 /* make sure the queue doesn't stall */
1330 libusb10_submit_transfer_sub(
1331 uxfer->dev_handle, endpoint);
1334 retval = LIBUSB_ERROR_NOT_FOUND;
1337 CTX_UNLOCK(dev->ctx);
1339 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1345 libusb10_cancel_all_transfer(libusb_device *dev)
1351 libusb_cpu_to_le16(uint16_t x)
1353 return (htole16(x));
1357 libusb_le16_to_cpu(uint16_t x)
1359 return (le16toh(x));