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/fcntl.h>
37 #include <sys/queue.h>
39 #define libusb_device_handle libusb20_device
42 #include "libusb20_desc.h"
43 #include "libusb20_int.h"
47 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
48 struct libusb_context *usbi_default_context = NULL;
52 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
53 static int libusb10_get_maxframe(struct libusb20_device *, libusb_transfer *);
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_init(libusb_context **context)
75 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 ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag);
110 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]");
112 ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag);
113 assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]");
115 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
117 pthread_mutex_lock(&default_context_lock);
118 if (usbi_default_context == NULL) {
119 usbi_default_context = ctx;
121 pthread_mutex_unlock(&default_context_lock);
126 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
132 libusb_exit(libusb_context *ctx)
134 ctx = GET_CONTEXT(ctx);
139 /* XXX cleanup devices */
141 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
142 close(ctx->ctrl_pipe[0]);
143 close(ctx->ctrl_pipe[1]);
144 pthread_mutex_destroy(&ctx->ctx_lock);
145 pthread_cond_destroy(&ctx->ctx_cond);
147 pthread_mutex_lock(&default_context_lock);
148 if (ctx == usbi_default_context) {
149 usbi_default_context = NULL;
151 pthread_mutex_unlock(&default_context_lock);
156 /* Device handling and initialisation. */
159 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
161 struct libusb20_backend *usb_backend;
162 struct libusb20_device *pdev;
163 struct libusb_device *dev;
166 ctx = GET_CONTEXT(ctx);
169 return (LIBUSB_ERROR_INVALID_PARAM);
172 return (LIBUSB_ERROR_INVALID_PARAM);
174 usb_backend = libusb20_be_alloc_default();
175 if (usb_backend == NULL)
176 return (LIBUSB_ERROR_NO_MEM);
178 /* figure out how many USB devices are present */
181 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
184 /* allocate device pointer list */
185 *list = malloc((i + 1) * sizeof(void *));
187 libusb20_be_free(usb_backend);
188 return (LIBUSB_ERROR_NO_MEM);
190 /* create libusb v1.0 compliant devices */
192 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
194 dev = malloc(sizeof(*dev));
197 libusb_unref_device((*list)[i - 1]);
202 libusb20_be_free(usb_backend);
203 return (LIBUSB_ERROR_NO_MEM);
206 /* get device into libUSB v1.0 list */
207 libusb20_be_dequeue_device(usb_backend, pdev);
209 memset(dev, 0, sizeof(*dev));
211 /* init transfer queues */
212 TAILQ_INIT(&dev->tr_head);
214 /* set context we belong to */
217 /* link together the two structures */
219 pdev->privLuData = dev;
221 (*list)[i] = libusb_ref_device(dev);
226 libusb20_be_free(usb_backend);
231 libusb_free_device_list(libusb_device **list, int unref_devices)
236 return; /* be NULL safe */
239 for (i = 0; list[i] != NULL; i++)
240 libusb_unref_device(list[i]);
246 libusb_get_bus_number(libusb_device *dev)
249 return (0); /* should not happen */
250 return (libusb20_dev_get_bus_number(dev->os_priv));
254 libusb_get_device_address(libusb_device *dev)
257 return (0); /* should not happen */
258 return (libusb20_dev_get_address(dev->os_priv));
262 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
264 struct libusb_config_descriptor *pdconf;
265 struct libusb_interface *pinf;
266 struct libusb_interface_descriptor *pdinf;
267 struct libusb_endpoint_descriptor *pdend;
274 return (LIBUSB_ERROR_NO_DEVICE);
276 ret = libusb_get_active_config_descriptor(dev, &pdconf);
280 ret = LIBUSB_ERROR_NOT_FOUND;
281 for (i = 0; i < pdconf->bNumInterfaces; i++) {
282 pinf = &pdconf->interface[i];
283 for (j = 0; j < pinf->num_altsetting; j++) {
284 pdinf = &pinf->altsetting[j];
285 for (k = 0; k < pdinf->bNumEndpoints; k++) {
286 pdend = &pdinf->endpoint[k];
287 if (pdend->bEndpointAddress == endpoint) {
288 ret = pdend->wMaxPacketSize;
296 libusb_free_config_descriptor(pdconf);
301 libusb_ref_device(libusb_device *dev)
304 return (NULL); /* be NULL safe */
308 CTX_UNLOCK(dev->ctx);
314 libusb_unref_device(libusb_device *dev)
317 return; /* be NULL safe */
321 CTX_UNLOCK(dev->ctx);
323 if (dev->refcnt == 0) {
324 libusb20_dev_free(dev->os_priv);
330 libusb_open(libusb_device *dev, libusb_device_handle **devh)
332 libusb_context *ctx = dev->ctx;
333 struct libusb20_device *pdev = dev->os_priv;
338 return (LIBUSB_ERROR_INVALID_PARAM);
340 /* set default device handle value */
343 dev = libusb_ref_device(dev);
345 return (LIBUSB_ERROR_INVALID_PARAM);
347 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
349 libusb_unref_device(dev);
350 return (LIBUSB_ERROR_NO_MEM);
352 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
353 POLLOUT | POLLRDNORM | POLLWRNORM);
355 /* make sure our event loop detects the new device */
357 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
358 if (err < sizeof(dummy)) {
359 /* ignore error, if any */
360 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
367 libusb_device_handle *
368 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
371 struct libusb_device **devs;
372 struct libusb20_device *pdev;
373 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
377 ctx = GET_CONTEXT(ctx);
379 return (NULL); /* be NULL safe */
381 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
383 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
386 for (j = 0; j < i; j++) {
387 pdev = devs[j]->os_priv;
388 pdesc = libusb20_dev_get_device_desc(pdev);
390 * NOTE: The USB library will automatically swap the
391 * fields in the device descriptor to be of host
394 if (pdesc->idVendor == vendor_id &&
395 pdesc->idProduct == product_id) {
396 if (libusb_open(devs[j], &pdev) < 0)
404 libusb_free_device_list(devs, 1);
405 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
410 libusb_close(struct libusb20_device *pdev)
413 struct libusb_device *dev;
418 return; /* be NULL safe */
420 dev = libusb_get_device(pdev);
423 libusb10_remove_pollfd(ctx, &dev->dev_poll);
425 libusb20_dev_close(pdev);
427 /* unref will free the "pdev" when the refcount reaches zero */
428 libusb_unref_device(dev);
430 /* make sure our event loop detects the closed device */
432 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
433 if (err < sizeof(dummy)) {
434 /* ignore error, if any */
435 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
440 libusb_get_device(struct libusb20_device *pdev)
444 return ((libusb_device *)pdev->privLuData);
448 libusb_get_configuration(struct libusb20_device *pdev, int *config)
450 struct libusb20_config *pconf;
452 if (pdev == NULL || config == NULL)
453 return (LIBUSB_ERROR_INVALID_PARAM);
455 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
457 return (LIBUSB_ERROR_NO_MEM);
459 *config = pconf->desc.bConfigurationValue;
467 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
469 struct libusb20_config *pconf;
470 struct libusb_device *dev;
474 dev = libusb_get_device(pdev);
477 return (LIBUSB_ERROR_INVALID_PARAM);
479 if (configuration < 1) {
483 for (i = 0; i != 255; i++) {
486 pconf = libusb20_dev_alloc_config(pdev, i);
488 return (LIBUSB_ERROR_INVALID_PARAM);
489 found = (pconf->desc.bConfigurationValue
496 return (LIBUSB_ERROR_INVALID_PARAM);
501 libusb10_cancel_all_transfer(dev);
503 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
505 err = libusb20_dev_set_config_index(pdev, i);
507 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
508 POLLOUT | POLLRDNORM | POLLWRNORM);
510 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
514 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
519 dev = libusb_get_device(pdev);
521 return (LIBUSB_ERROR_INVALID_PARAM);
523 if (interface_number < 0 || interface_number > 31)
524 return (LIBUSB_ERROR_INVALID_PARAM);
527 if (dev->claimed_interfaces & (1 << interface_number))
528 err = LIBUSB_ERROR_BUSY;
531 dev->claimed_interfaces |= (1 << interface_number);
532 CTX_UNLOCK(dev->ctx);
537 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
542 dev = libusb_get_device(pdev);
544 return (LIBUSB_ERROR_INVALID_PARAM);
546 if (interface_number < 0 || interface_number > 31)
547 return (LIBUSB_ERROR_INVALID_PARAM);
550 if (!(dev->claimed_interfaces & (1 << interface_number)))
551 err = LIBUSB_ERROR_NOT_FOUND;
554 dev->claimed_interfaces &= ~(1 << interface_number);
555 CTX_UNLOCK(dev->ctx);
560 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
561 int interface_number, int alternate_setting)
566 dev = libusb_get_device(pdev);
568 return (LIBUSB_ERROR_INVALID_PARAM);
570 if (interface_number < 0 || interface_number > 31)
571 return (LIBUSB_ERROR_INVALID_PARAM);
574 if (!(dev->claimed_interfaces & (1 << interface_number)))
575 err = LIBUSB_ERROR_NOT_FOUND;
576 CTX_UNLOCK(dev->ctx);
581 libusb10_cancel_all_transfer(dev);
583 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
585 err = libusb20_dev_set_alt_index(pdev,
586 interface_number, alternate_setting);
588 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
589 pdev, libusb20_dev_get_fd(pdev),
590 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
592 return (err ? LIBUSB_ERROR_OTHER : 0);
595 static struct libusb20_transfer *
596 libusb10_get_transfer(struct libusb20_device *pdev,
597 uint8_t endpoint, uint8_t index)
599 index &= 1; /* double buffering */
601 index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
603 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
604 /* this is an IN endpoint */
607 return (libusb20_tr_get_pointer(pdev, index));
611 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
613 struct libusb20_transfer *xfer;
614 struct libusb_device *dev;
617 xfer = libusb10_get_transfer(pdev, endpoint, 0);
619 return (LIBUSB_ERROR_INVALID_PARAM);
621 dev = libusb_get_device(pdev);
624 err = libusb20_tr_open(xfer, 0, 0, endpoint);
625 CTX_UNLOCK(dev->ctx);
627 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
628 return (LIBUSB_ERROR_OTHER);
630 libusb20_tr_clear_stall_sync(xfer);
632 /* check if we opened the transfer */
635 libusb20_tr_close(xfer);
636 CTX_UNLOCK(dev->ctx);
638 return (0); /* success */
642 libusb_reset_device(struct libusb20_device *pdev)
647 dev = libusb_get_device(pdev);
649 return (LIBUSB20_ERROR_INVALID_PARAM);
651 libusb10_cancel_all_transfer(dev);
653 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
655 err = libusb20_dev_reset(pdev);
657 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
658 pdev, libusb20_dev_get_fd(pdev),
659 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
661 return (err ? LIBUSB_ERROR_OTHER : 0);
665 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
668 return (LIBUSB_ERROR_INVALID_PARAM);
670 return (libusb20_dev_kernel_driver_active(
675 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
680 return (LIBUSB_ERROR_INVALID_PARAM);
682 err = libusb20_dev_detach_kernel_driver(
685 return (err ? LIBUSB20_ERROR_OTHER : 0);
689 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
692 return (LIBUSB_ERROR_INVALID_PARAM);
693 /* stub - currently not supported by libusb20 */
697 /* Asynchronous device I/O */
699 struct libusb_transfer *
700 libusb_alloc_transfer(int iso_packets)
702 struct libusb_transfer *uxfer;
703 struct libusb_super_transfer *sxfer;
706 len = sizeof(struct libusb_transfer) +
707 sizeof(struct libusb_super_transfer) +
708 (iso_packets * sizeof(libusb_iso_packet_descriptor));
714 memset(sxfer, 0, len);
716 uxfer = (struct libusb_transfer *)(
717 ((uint8_t *)sxfer) + sizeof(*sxfer));
719 /* set default value */
720 uxfer->num_iso_packets = iso_packets;
726 libusb_free_transfer(struct libusb_transfer *uxfer)
728 struct libusb_super_transfer *sxfer;
731 return; /* be NULL safe */
733 sxfer = (struct libusb_super_transfer *)(
734 (uint8_t *)uxfer - sizeof(*sxfer));
740 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
745 usb_speed = libusb20_dev_get_speed(pdev);
747 switch (xfer->type) {
748 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
750 case LIBUSB20_SPEED_LOW:
751 case LIBUSB20_SPEED_FULL:
759 case LIBUSB_TRANSFER_TYPE_CONTROL:
770 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
775 usb_speed = libusb20_dev_get_speed(pdev);
777 switch (xfer->type) {
778 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
779 ret = 0; /* kernel will auto-select */
781 case LIBUSB_TRANSFER_TYPE_CONTROL:
786 case LIBUSB20_SPEED_LOW:
789 case LIBUSB20_SPEED_FULL:
802 libusb10_convert_error(uint8_t status)
807 case LIBUSB20_TRANSFER_START:
808 case LIBUSB20_TRANSFER_COMPLETED:
809 return (LIBUSB_TRANSFER_COMPLETED);
810 case LIBUSB20_TRANSFER_OVERFLOW:
811 return (LIBUSB_TRANSFER_OVERFLOW);
812 case LIBUSB20_TRANSFER_NO_DEVICE:
813 return (LIBUSB_TRANSFER_NO_DEVICE);
814 case LIBUSB20_TRANSFER_STALL:
815 return (LIBUSB_TRANSFER_STALL);
816 case LIBUSB20_TRANSFER_CANCELLED:
817 return (LIBUSB_TRANSFER_CANCELLED);
818 case LIBUSB20_TRANSFER_TIMED_OUT:
819 return (LIBUSB_TRANSFER_TIMED_OUT);
821 return (LIBUSB_TRANSFER_ERROR);
825 /* This function must be called locked */
828 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
829 struct libusb_super_transfer *sxfer, int status)
831 struct libusb_transfer *uxfer;
832 struct libusb_device *dev;
834 uxfer = (struct libusb_transfer *)(
835 ((uint8_t *)sxfer) + sizeof(*sxfer));
838 libusb20_tr_set_priv_sc1(pxfer, NULL);
840 /* set transfer status */
841 uxfer->status = status;
843 /* update super transfer state */
844 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
846 dev = libusb_get_device(uxfer->dev_handle);
848 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
851 /* This function must be called locked */
854 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
856 struct libusb_super_transfer *sxfer;
857 struct libusb_transfer *uxfer;
859 uint16_t iso_packets;
864 status = libusb20_tr_get_status(pxfer);
865 sxfer = libusb20_tr_get_priv_sc1(pxfer);
866 actlen = libusb20_tr_get_actual_length(pxfer);
867 iso_packets = libusb20_tr_get_max_frames(pxfer);
870 return; /* cancelled - nothing to do */
872 uxfer = (struct libusb_transfer *)(
873 ((uint8_t *)sxfer) + sizeof(*sxfer));
875 if (iso_packets > uxfer->num_iso_packets)
876 iso_packets = uxfer->num_iso_packets;
878 if (iso_packets == 0)
879 return; /* nothing to do */
881 /* make sure that the number of ISOCHRONOUS packets is valid */
882 uxfer->num_iso_packets = iso_packets;
884 flags = uxfer->flags;
887 case LIBUSB20_TRANSFER_COMPLETED:
889 /* update actual length */
890 uxfer->actual_length = actlen;
891 for (i = 0; i != iso_packets; i++) {
892 uxfer->iso_packet_desc[i].actual_length =
893 libusb20_tr_get_length(pxfer, i);
895 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
898 case LIBUSB20_TRANSFER_START:
900 /* setup length(s) */
902 for (i = 0; i != iso_packets; i++) {
903 libusb20_tr_setup_isoc(pxfer,
904 &uxfer->buffer[actlen],
905 uxfer->iso_packet_desc[i].length, i);
906 actlen += uxfer->iso_packet_desc[i].length;
912 libusb20_tr_set_total_frames(pxfer, iso_packets);
913 libusb20_tr_submit(pxfer);
915 /* fork another USB transfer, if any */
916 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
920 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
925 /* This function must be called locked */
928 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
930 struct libusb_super_transfer *sxfer;
931 struct libusb_transfer *uxfer;
937 status = libusb20_tr_get_status(pxfer);
938 sxfer = libusb20_tr_get_priv_sc1(pxfer);
939 max_bulk = libusb20_tr_get_max_total_length(pxfer);
940 actlen = libusb20_tr_get_actual_length(pxfer);
943 return; /* cancelled - nothing to do */
945 uxfer = (struct libusb_transfer *)(
946 ((uint8_t *)sxfer) + sizeof(*sxfer));
948 flags = uxfer->flags;
951 case LIBUSB20_TRANSFER_COMPLETED:
953 uxfer->actual_length += actlen;
955 /* check for short packet */
956 if (sxfer->last_len != actlen) {
957 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
958 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
960 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
964 /* check for end of data */
965 if (sxfer->rem_len == 0) {
966 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
971 case LIBUSB20_TRANSFER_START:
972 if (max_bulk > sxfer->rem_len) {
973 max_bulk = sxfer->rem_len;
975 /* setup new BULK or INTERRUPT transaction */
976 libusb20_tr_setup_bulk(pxfer,
977 sxfer->curr_data, max_bulk, uxfer->timeout);
979 /* update counters */
980 sxfer->last_len = max_bulk;
981 sxfer->curr_data += max_bulk;
982 sxfer->rem_len -= max_bulk;
984 libusb20_tr_submit(pxfer);
986 /* check if we can fork another USB transfer */
987 if (sxfer->rem_len == 0)
988 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
992 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
997 /* This function must be called locked */
1000 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1002 struct libusb_super_transfer *sxfer;
1003 struct libusb_transfer *uxfer;
1009 status = libusb20_tr_get_status(pxfer);
1010 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1011 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1012 actlen = libusb20_tr_get_actual_length(pxfer);
1015 return; /* cancelled - nothing to do */
1017 uxfer = (struct libusb_transfer *)(
1018 ((uint8_t *)sxfer) + sizeof(*sxfer));
1020 flags = uxfer->flags;
1023 case LIBUSB20_TRANSFER_COMPLETED:
1025 uxfer->actual_length += actlen;
1027 /* subtract length of SETUP packet, if any */
1028 actlen -= libusb20_tr_get_length(pxfer, 0);
1030 /* check for short packet */
1031 if (sxfer->last_len != actlen) {
1032 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1033 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1035 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1039 /* check for end of data */
1040 if (sxfer->rem_len == 0) {
1041 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1046 case LIBUSB20_TRANSFER_START:
1047 if (max_bulk > sxfer->rem_len) {
1048 max_bulk = sxfer->rem_len;
1050 /* setup new CONTROL transaction */
1051 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1052 /* next fragment - don't send SETUP packet */
1053 libusb20_tr_set_length(pxfer, 0, 0);
1055 /* first fragment - send SETUP packet */
1056 libusb20_tr_set_length(pxfer, 8, 0);
1057 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1060 if (max_bulk != 0) {
1061 libusb20_tr_set_length(pxfer, max_bulk, 1);
1062 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1063 libusb20_tr_set_total_frames(pxfer, 2);
1065 libusb20_tr_set_total_frames(pxfer, 1);
1068 /* update counters */
1069 sxfer->last_len = max_bulk;
1070 sxfer->curr_data += max_bulk;
1071 sxfer->rem_len -= max_bulk;
1073 libusb20_tr_submit(pxfer);
1075 /* check if we can fork another USB transfer */
1076 if (sxfer->rem_len == 0)
1077 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1081 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1086 /* The following function must be called locked */
1089 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1091 struct libusb20_transfer *pxfer0;
1092 struct libusb20_transfer *pxfer1;
1093 struct libusb_super_transfer *sxfer;
1094 struct libusb_transfer *uxfer;
1095 struct libusb_device *dev;
1102 dev = libusb_get_device(pdev);
1104 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1105 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1107 if (pxfer0 == NULL || pxfer1 == NULL)
1108 return; /* shouldn't happen */
1111 if (libusb20_tr_pending(pxfer0))
1113 if (libusb20_tr_pending(pxfer1))
1118 /* wait till one of the transfers complete */
1121 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1123 return; /* cancelling */
1125 return; /* cannot queue another one */
1126 /* swap transfers */
1130 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1132 return; /* cancelling */
1134 return; /* cannot queue another one */
1135 /* swap transfers */
1142 /* find next transfer on same endpoint */
1143 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1145 uxfer = (struct libusb_transfer *)(
1146 ((uint8_t *)sxfer) + sizeof(*sxfer));
1148 if (uxfer->endpoint == endpoint) {
1149 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1150 sxfer->entry.tqe_prev = NULL;
1154 return; /* success */
1158 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1159 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1161 /* reset super transfer state */
1162 sxfer->rem_len = uxfer->length;
1163 sxfer->curr_data = uxfer->buffer;
1164 uxfer->actual_length = 0;
1166 switch (uxfer->type) {
1167 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1168 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1170 case LIBUSB_TRANSFER_TYPE_BULK:
1171 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1172 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1174 case LIBUSB_TRANSFER_TYPE_CONTROL:
1175 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1176 if (sxfer->rem_len < 8)
1179 /* remove SETUP packet from data */
1180 sxfer->rem_len -= 8;
1181 sxfer->curr_data += 8;
1187 buffsize = libusb10_get_buffsize(pdev, uxfer);
1188 maxframe = libusb10_get_maxframe(pdev, uxfer);
1190 /* make sure the transfer is opened */
1191 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1192 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1195 libusb20_tr_start(pxfer0);
1199 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1201 /* make sure our event loop spins the done handler */
1203 write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1206 /* The following function must be called unlocked */
1209 libusb_submit_transfer(struct libusb_transfer *uxfer)
1211 struct libusb20_transfer *pxfer0;
1212 struct libusb20_transfer *pxfer1;
1213 struct libusb_super_transfer *sxfer;
1214 struct libusb_device *dev;
1219 return (LIBUSB_ERROR_INVALID_PARAM);
1221 if (uxfer->dev_handle == NULL)
1222 return (LIBUSB_ERROR_INVALID_PARAM);
1224 endpoint = uxfer->endpoint;
1227 return (LIBUSB_ERROR_INVALID_PARAM);
1229 dev = libusb_get_device(uxfer->dev_handle);
1231 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1233 sxfer = (struct libusb_super_transfer *)(
1234 (uint8_t *)uxfer - sizeof(*sxfer));
1238 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1239 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1241 if (pxfer0 == NULL || pxfer1 == NULL) {
1242 err = LIBUSB_ERROR_OTHER;
1243 } else if ((sxfer->entry.tqe_prev != NULL) ||
1244 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1245 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1246 err = LIBUSB_ERROR_BUSY;
1249 /* set pending state */
1250 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1252 /* insert transfer into transfer head list */
1253 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1255 /* start work transfers */
1256 libusb10_submit_transfer_sub(
1257 uxfer->dev_handle, endpoint);
1259 err = 0; /* success */
1262 CTX_UNLOCK(dev->ctx);
1264 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1269 /* Asynchronous transfer cancel */
1272 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1274 struct libusb20_transfer *pxfer0;
1275 struct libusb20_transfer *pxfer1;
1276 struct libusb_super_transfer *sxfer;
1277 struct libusb_device *dev;
1282 return (LIBUSB_ERROR_INVALID_PARAM);
1284 /* check if not initialised */
1285 if (uxfer->dev_handle == NULL)
1286 return (LIBUSB_ERROR_NOT_FOUND);
1288 endpoint = uxfer->endpoint;
1291 return (LIBUSB_ERROR_INVALID_PARAM);
1293 dev = libusb_get_device(uxfer->dev_handle);
1295 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1297 sxfer = (struct libusb_super_transfer *)(
1298 (uint8_t *)uxfer - sizeof(*sxfer));
1304 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1305 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1307 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1308 /* only update the transfer status */
1309 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1310 retval = LIBUSB_ERROR_NOT_FOUND;
1311 } else if (sxfer->entry.tqe_prev != NULL) {
1312 /* we are lucky - transfer is on a queue */
1313 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1314 sxfer->entry.tqe_prev = NULL;
1315 libusb10_complete_transfer(NULL,
1316 sxfer, LIBUSB_TRANSFER_CANCELLED);
1317 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1319 retval = LIBUSB_ERROR_NOT_FOUND;
1320 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1321 libusb10_complete_transfer(pxfer0,
1322 sxfer, LIBUSB_TRANSFER_CANCELLED);
1323 libusb20_tr_stop(pxfer0);
1324 /* make sure the queue doesn't stall */
1325 libusb10_submit_transfer_sub(
1326 uxfer->dev_handle, endpoint);
1327 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1328 libusb10_complete_transfer(pxfer1,
1329 sxfer, LIBUSB_TRANSFER_CANCELLED);
1330 libusb20_tr_stop(pxfer1);
1331 /* make sure the queue doesn't stall */
1332 libusb10_submit_transfer_sub(
1333 uxfer->dev_handle, endpoint);
1336 retval = LIBUSB_ERROR_NOT_FOUND;
1339 CTX_UNLOCK(dev->ctx);
1341 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1347 libusb10_cancel_all_transfer(libusb_device *dev)
1353 libusb_cpu_to_le16(uint16_t x)
1355 return (htole16(x));
1359 libusb_le16_to_cpu(uint16_t x)
1361 return (le16toh(x));