kernel - Revert part of the contig allocation work
[dragonfly.git] / lib / libusb / libusb10.c
blob45e7204dbba0888d344cc3fec8e1a8f8188009f8
1 /* $FreeBSD: head/lib/libusb/libusb10.c 264344 2014-04-11 14:11:55Z hselasky $ */
2 /*-
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
8 * are met:
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
25 * SUCH DAMAGE.
28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
30 #else
31 #include <assert.h>
32 #include <errno.h>
33 #include <poll.h>
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <time.h>
40 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #include <sys/endian.h>
44 #endif
46 #define libusb_device_handle libusb20_device
48 #include "libusb20.h"
49 #include "libusb20_desc.h"
50 #include "libusb20_int.h"
51 #include "libusb.h"
52 #include "libusb10.h"
54 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
55 struct libusb_context *usbi_default_context = NULL;
57 /* Prototypes */
59 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
60 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
61 static int libusb10_convert_error(uint8_t status);
62 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
63 static void libusb10_isoc_proxy(struct libusb20_transfer *);
64 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
65 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
66 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
68 /* Library initialisation / deinitialisation */
70 void
71 libusb_set_debug(libusb_context *ctx, int level)
73 ctx = GET_CONTEXT(ctx);
74 if (ctx)
75 ctx->debug = level;
78 static void
79 libusb_set_nonblocking(int f)
81 int flags;
84 * We ignore any failures in this function, hence the
85 * non-blocking flag is not critical to the operation of
86 * libUSB. We use F_GETFL and F_SETFL to be compatible with
87 * Linux.
90 flags = fcntl(f, F_GETFL, NULL);
91 if (flags == -1)
92 return;
93 flags |= O_NONBLOCK;
94 fcntl(f, F_SETFL, flags);
97 int
98 libusb_init(libusb_context **context)
100 struct libusb_context *ctx;
101 pthread_condattr_t attr;
102 char *debug;
103 int ret;
105 ctx = malloc(sizeof(*ctx));
106 if (!ctx)
107 return (LIBUSB_ERROR_INVALID_PARAM);
109 memset(ctx, 0, sizeof(*ctx));
111 debug = getenv("LIBUSB_DEBUG");
112 if (debug != NULL) {
113 ctx->debug = atoi(debug);
114 if (ctx->debug != 0)
115 ctx->debug_fixed = 1;
117 TAILQ_INIT(&ctx->pollfds);
118 TAILQ_INIT(&ctx->tr_done);
119 TAILQ_INIT(&ctx->hotplug_cbh);
120 TAILQ_INIT(&ctx->hotplug_devs);
122 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
123 free(ctx);
124 return (LIBUSB_ERROR_NO_MEM);
126 if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
127 pthread_mutex_destroy(&ctx->ctx_lock);
128 free(ctx);
129 return (LIBUSB_ERROR_NO_MEM);
131 if (pthread_condattr_init(&attr) != 0) {
132 pthread_mutex_destroy(&ctx->ctx_lock);
133 pthread_mutex_destroy(&ctx->hotplug_lock);
134 free(ctx);
135 return (LIBUSB_ERROR_NO_MEM);
137 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
138 pthread_mutex_destroy(&ctx->ctx_lock);
139 pthread_mutex_destroy(&ctx->hotplug_lock);
140 pthread_condattr_destroy(&attr);
141 free(ctx);
142 return (LIBUSB_ERROR_OTHER);
144 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
145 pthread_mutex_destroy(&ctx->ctx_lock);
146 pthread_mutex_destroy(&ctx->hotplug_lock);
147 pthread_condattr_destroy(&attr);
148 free(ctx);
149 return (LIBUSB_ERROR_NO_MEM);
151 pthread_condattr_destroy(&attr);
153 ctx->ctx_handler = NO_THREAD;
154 ctx->hotplug_handler = NO_THREAD;
156 ret = pipe(ctx->ctrl_pipe);
157 if (ret < 0) {
158 pthread_mutex_destroy(&ctx->ctx_lock);
159 pthread_mutex_destroy(&ctx->hotplug_lock);
160 pthread_cond_destroy(&ctx->ctx_cond);
161 free(ctx);
162 return (LIBUSB_ERROR_OTHER);
164 /* set non-blocking mode on the control pipe to avoid deadlock */
165 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
166 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
168 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
170 pthread_mutex_lock(&default_context_lock);
171 if (usbi_default_context == NULL) {
172 usbi_default_context = ctx;
174 pthread_mutex_unlock(&default_context_lock);
176 if (context)
177 *context = ctx;
179 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
181 return (0);
184 void
185 libusb_exit(libusb_context *ctx)
187 ctx = GET_CONTEXT(ctx);
189 if (ctx == NULL)
190 return;
192 /* stop hotplug thread, if any */
194 if (ctx->hotplug_handler != NO_THREAD) {
195 pthread_t td;
196 void *ptr;
198 HOTPLUG_LOCK(ctx);
199 td = ctx->hotplug_handler;
200 ctx->hotplug_handler = NO_THREAD;
201 HOTPLUG_UNLOCK(ctx);
203 pthread_join(td, &ptr);
206 /* XXX cleanup devices */
208 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
209 close(ctx->ctrl_pipe[0]);
210 close(ctx->ctrl_pipe[1]);
211 pthread_mutex_destroy(&ctx->ctx_lock);
212 pthread_mutex_destroy(&ctx->hotplug_lock);
213 pthread_cond_destroy(&ctx->ctx_cond);
215 pthread_mutex_lock(&default_context_lock);
216 if (ctx == usbi_default_context) {
217 usbi_default_context = NULL;
219 pthread_mutex_unlock(&default_context_lock);
221 free(ctx);
224 /* Device handling and initialisation. */
226 ssize_t
227 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
229 struct libusb20_backend *usb_backend;
230 struct libusb20_device *pdev;
231 struct libusb_device *dev;
232 int i;
234 ctx = GET_CONTEXT(ctx);
236 if (ctx == NULL)
237 return (LIBUSB_ERROR_INVALID_PARAM);
239 if (list == NULL)
240 return (LIBUSB_ERROR_INVALID_PARAM);
242 usb_backend = libusb20_be_alloc_default();
243 if (usb_backend == NULL)
244 return (LIBUSB_ERROR_NO_MEM);
246 /* figure out how many USB devices are present */
247 pdev = NULL;
248 i = 0;
249 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
250 i++;
252 /* allocate device pointer list */
253 *list = malloc((i + 1) * sizeof(void *));
254 if (*list == NULL) {
255 libusb20_be_free(usb_backend);
256 return (LIBUSB_ERROR_NO_MEM);
258 /* create libusb v1.0 compliant devices */
259 i = 0;
260 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
262 dev = malloc(sizeof(*dev));
263 if (dev == NULL) {
264 while (i != 0) {
265 libusb_unref_device((*list)[i - 1]);
266 i--;
268 free(*list);
269 *list = NULL;
270 libusb20_be_free(usb_backend);
271 return (LIBUSB_ERROR_NO_MEM);
273 /* get device into libUSB v1.0 list */
274 libusb20_be_dequeue_device(usb_backend, pdev);
276 memset(dev, 0, sizeof(*dev));
278 /* init transfer queues */
279 TAILQ_INIT(&dev->tr_head);
281 /* set context we belong to */
282 dev->ctx = ctx;
284 /* link together the two structures */
285 dev->os_priv = pdev;
286 pdev->privLuData = dev;
288 (*list)[i] = libusb_ref_device(dev);
289 i++;
291 (*list)[i] = NULL;
293 libusb20_be_free(usb_backend);
294 return (i);
297 void
298 libusb_free_device_list(libusb_device **list, int unref_devices)
300 int i;
302 if (list == NULL)
303 return; /* be NULL safe */
305 if (unref_devices) {
306 for (i = 0; list[i] != NULL; i++)
307 libusb_unref_device(list[i]);
309 free(list);
312 uint8_t
313 libusb_get_bus_number(libusb_device *dev)
315 if (dev == NULL)
316 return (0); /* should not happen */
317 return (libusb20_dev_get_bus_number(dev->os_priv));
321 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
323 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
327 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
328 uint8_t bufsize)
330 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
333 uint8_t
334 libusb_get_device_address(libusb_device *dev)
336 if (dev == NULL)
337 return (0); /* should not happen */
338 return (libusb20_dev_get_address(dev->os_priv));
341 enum libusb_speed
342 libusb_get_device_speed(libusb_device *dev)
344 if (dev == NULL)
345 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
347 switch (libusb20_dev_get_speed(dev->os_priv)) {
348 case LIBUSB20_SPEED_LOW:
349 return (LIBUSB_SPEED_LOW);
350 case LIBUSB20_SPEED_FULL:
351 return (LIBUSB_SPEED_FULL);
352 case LIBUSB20_SPEED_HIGH:
353 return (LIBUSB_SPEED_HIGH);
354 case LIBUSB20_SPEED_SUPER:
355 return (LIBUSB_SPEED_SUPER);
356 default:
357 break;
359 return (LIBUSB_SPEED_UNKNOWN);
363 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
365 struct libusb_config_descriptor *pdconf;
366 struct libusb_interface *pinf;
367 struct libusb_interface_descriptor *pdinf;
368 struct libusb_endpoint_descriptor *pdend;
369 int i;
370 int j;
371 int k;
372 int ret;
374 if (dev == NULL)
375 return (LIBUSB_ERROR_NO_DEVICE);
377 ret = libusb_get_active_config_descriptor(dev, &pdconf);
378 if (ret < 0)
379 return (ret);
381 ret = LIBUSB_ERROR_NOT_FOUND;
382 for (i = 0; i < pdconf->bNumInterfaces; i++) {
383 pinf = &pdconf->interface[i];
384 for (j = 0; j < pinf->num_altsetting; j++) {
385 pdinf = &pinf->altsetting[j];
386 for (k = 0; k < pdinf->bNumEndpoints; k++) {
387 pdend = &pdinf->endpoint[k];
388 if (pdend->bEndpointAddress == endpoint) {
389 ret = pdend->wMaxPacketSize;
390 goto out;
396 out:
397 libusb_free_config_descriptor(pdconf);
398 return (ret);
402 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
404 int multiplier;
405 int ret;
407 ret = libusb_get_max_packet_size(dev, endpoint);
409 switch (libusb20_dev_get_speed(dev->os_priv)) {
410 case LIBUSB20_SPEED_LOW:
411 case LIBUSB20_SPEED_FULL:
412 break;
413 default:
414 if (ret > -1) {
415 multiplier = (1 + ((ret >> 11) & 3));
416 if (multiplier > 3)
417 multiplier = 3;
418 ret = (ret & 0x7FF) * multiplier;
420 break;
422 return (ret);
425 libusb_device *
426 libusb_ref_device(libusb_device *dev)
428 if (dev == NULL)
429 return (NULL); /* be NULL safe */
431 CTX_LOCK(dev->ctx);
432 dev->refcnt++;
433 CTX_UNLOCK(dev->ctx);
435 return (dev);
438 void
439 libusb_unref_device(libusb_device *dev)
441 if (dev == NULL)
442 return; /* be NULL safe */
444 CTX_LOCK(dev->ctx);
445 dev->refcnt--;
446 CTX_UNLOCK(dev->ctx);
448 if (dev->refcnt == 0) {
449 libusb20_dev_free(dev->os_priv);
450 free(dev);
455 libusb_open(libusb_device *dev, libusb_device_handle **devh)
457 libusb_context *ctx = dev->ctx;
458 struct libusb20_device *pdev = dev->os_priv;
459 uint8_t dummy;
460 int err;
462 if (devh == NULL)
463 return (LIBUSB_ERROR_INVALID_PARAM);
465 /* set default device handle value */
466 *devh = NULL;
468 dev = libusb_ref_device(dev);
469 if (dev == NULL)
470 return (LIBUSB_ERROR_INVALID_PARAM);
472 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
473 if (err) {
474 libusb_unref_device(dev);
475 return (LIBUSB_ERROR_NO_MEM);
477 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
478 POLLOUT | POLLRDNORM | POLLWRNORM);
480 /* make sure our event loop detects the new device */
481 dummy = 0;
482 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
483 if (err < (int)sizeof(dummy)) {
484 /* ignore error, if any */
485 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
487 *devh = pdev;
489 return (0);
492 libusb_device_handle *
493 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
494 uint16_t product_id)
496 struct libusb_device **devs;
497 struct libusb20_device *pdev;
498 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
499 int i;
500 int j;
502 ctx = GET_CONTEXT(ctx);
503 if (ctx == NULL)
504 return (NULL); /* be NULL safe */
506 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
508 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
509 return (NULL);
511 pdev = NULL;
512 for (j = 0; j < i; j++) {
513 struct libusb20_device *tdev;
515 tdev = devs[j]->os_priv;
516 pdesc = libusb20_dev_get_device_desc(tdev);
518 * NOTE: The USB library will automatically swap the
519 * fields in the device descriptor to be of host
520 * endian type!
522 if (pdesc->idVendor == vendor_id &&
523 pdesc->idProduct == product_id) {
524 libusb_open(devs[j], &pdev);
525 break;
529 libusb_free_device_list(devs, 1);
530 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
531 return (pdev);
534 void
535 libusb_close(struct libusb20_device *pdev)
537 libusb_context *ctx;
538 struct libusb_device *dev;
539 uint8_t dummy;
540 int err;
542 if (pdev == NULL)
543 return; /* be NULL safe */
545 dev = libusb_get_device(pdev);
546 ctx = dev->ctx;
548 libusb10_remove_pollfd(ctx, &dev->dev_poll);
550 libusb20_dev_close(pdev);
552 /* unref will free the "pdev" when the refcount reaches zero */
553 libusb_unref_device(dev);
555 /* make sure our event loop detects the closed device */
556 dummy = 0;
557 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
558 if (err < (int)sizeof(dummy)) {
559 /* ignore error, if any */
560 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
564 libusb_device *
565 libusb_get_device(struct libusb20_device *pdev)
567 if (pdev == NULL)
568 return (NULL);
569 return ((libusb_device *)pdev->privLuData);
573 libusb_get_configuration(struct libusb20_device *pdev, int *config)
575 struct libusb20_config *pconf;
577 if (pdev == NULL || config == NULL)
578 return (LIBUSB_ERROR_INVALID_PARAM);
580 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
581 if (pconf == NULL)
582 return (LIBUSB_ERROR_NO_MEM);
584 *config = pconf->desc.bConfigurationValue;
586 free(pconf);
588 return (0);
592 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
594 struct libusb20_config *pconf;
595 struct libusb_device *dev;
596 int err;
597 uint8_t i;
599 dev = libusb_get_device(pdev);
600 if (dev == NULL)
601 return (LIBUSB_ERROR_INVALID_PARAM);
603 if (configuration < 1) {
604 /* unconfigure */
605 i = 255;
606 } else {
607 for (i = 0; i != 255; i++) {
608 uint8_t found;
610 pconf = libusb20_dev_alloc_config(pdev, i);
611 if (pconf == NULL)
612 return (LIBUSB_ERROR_INVALID_PARAM);
613 found = (pconf->desc.bConfigurationValue
614 == configuration);
615 free(pconf);
617 if (found)
618 goto set_config;
620 return (LIBUSB_ERROR_INVALID_PARAM);
623 set_config:
625 libusb10_cancel_all_transfer(dev);
627 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
629 err = libusb20_dev_set_config_index(pdev, i);
631 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
632 POLLOUT | POLLRDNORM | POLLWRNORM);
634 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
638 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
640 libusb_device *dev;
642 dev = libusb_get_device(pdev);
643 if (dev == NULL)
644 return (LIBUSB_ERROR_INVALID_PARAM);
646 if (interface_number < 0 || interface_number > 31)
647 return (LIBUSB_ERROR_INVALID_PARAM);
649 CTX_LOCK(dev->ctx);
650 dev->claimed_interfaces |= (1 << interface_number);
651 CTX_UNLOCK(dev->ctx);
653 return (0);
657 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
659 libusb_device *dev;
660 int err = 0;
662 dev = libusb_get_device(pdev);
663 if (dev == NULL)
664 return (LIBUSB_ERROR_INVALID_PARAM);
666 if (interface_number < 0 || interface_number > 31)
667 return (LIBUSB_ERROR_INVALID_PARAM);
669 CTX_LOCK(dev->ctx);
670 if (!(dev->claimed_interfaces & (1 << interface_number)))
671 err = LIBUSB_ERROR_NOT_FOUND;
673 if (!err)
674 dev->claimed_interfaces &= ~(1 << interface_number);
675 CTX_UNLOCK(dev->ctx);
676 return (err);
680 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
681 int interface_number, int alternate_setting)
683 libusb_device *dev;
684 int err = 0;
686 dev = libusb_get_device(pdev);
687 if (dev == NULL)
688 return (LIBUSB_ERROR_INVALID_PARAM);
690 if (interface_number < 0 || interface_number > 31)
691 return (LIBUSB_ERROR_INVALID_PARAM);
693 CTX_LOCK(dev->ctx);
694 if (!(dev->claimed_interfaces & (1 << interface_number)))
695 err = LIBUSB_ERROR_NOT_FOUND;
696 CTX_UNLOCK(dev->ctx);
698 if (err)
699 return (err);
701 libusb10_cancel_all_transfer(dev);
703 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
705 err = libusb20_dev_set_alt_index(pdev,
706 interface_number, alternate_setting);
708 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
709 pdev, libusb20_dev_get_fd(pdev),
710 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
712 return (err ? LIBUSB_ERROR_OTHER : 0);
715 static struct libusb20_transfer *
716 libusb10_get_transfer(struct libusb20_device *pdev,
717 uint8_t endpoint, uint8_t xfer_index)
719 xfer_index &= 1; /* double buffering */
721 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
723 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
724 /* this is an IN endpoint */
725 xfer_index |= 2;
727 return (libusb20_tr_get_pointer(pdev, xfer_index));
731 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
733 struct libusb20_transfer *xfer;
734 struct libusb_device *dev;
735 int err;
737 xfer = libusb10_get_transfer(pdev, endpoint, 0);
738 if (xfer == NULL)
739 return (LIBUSB_ERROR_INVALID_PARAM);
741 dev = libusb_get_device(pdev);
742 if (dev == NULL)
743 return (LIBUSB_ERROR_INVALID_PARAM);
745 CTX_LOCK(dev->ctx);
746 err = libusb20_tr_open(xfer, 0, 1, endpoint);
747 CTX_UNLOCK(dev->ctx);
749 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
750 return (LIBUSB_ERROR_OTHER);
752 libusb20_tr_clear_stall_sync(xfer);
754 /* check if we opened the transfer */
755 if (err == 0) {
756 CTX_LOCK(dev->ctx);
757 libusb20_tr_close(xfer);
758 CTX_UNLOCK(dev->ctx);
760 return (0); /* success */
764 libusb_reset_device(struct libusb20_device *pdev)
766 libusb_device *dev;
767 int err;
769 dev = libusb_get_device(pdev);
770 if (dev == NULL)
771 return (LIBUSB_ERROR_INVALID_PARAM);
773 libusb10_cancel_all_transfer(dev);
775 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
777 err = libusb20_dev_reset(pdev);
779 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
780 pdev, libusb20_dev_get_fd(pdev),
781 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
783 return (err ? LIBUSB_ERROR_OTHER : 0);
787 libusb_check_connected(struct libusb20_device *pdev)
789 libusb_device *dev;
790 int err;
792 dev = libusb_get_device(pdev);
793 if (dev == NULL)
794 return (LIBUSB_ERROR_INVALID_PARAM);
796 err = libusb20_dev_check_connected(pdev);
798 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
802 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
804 if (pdev == NULL)
805 return (LIBUSB_ERROR_INVALID_PARAM);
807 if (libusb20_dev_kernel_driver_active(pdev, interface))
808 return (0); /* no kernel driver is active */
809 else
810 return (1); /* kernel driver is active */
814 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
815 char *name, int namelen)
817 return (libusb_get_driver(pdev, interface, name, namelen));
821 libusb_get_driver(struct libusb20_device *pdev, int interface,
822 char *name, int namelen)
824 char *ptr;
825 int err;
827 if (pdev == NULL)
828 return (LIBUSB_ERROR_INVALID_PARAM);
829 if (namelen < 1)
830 return (LIBUSB_ERROR_INVALID_PARAM);
831 if (namelen > 255)
832 namelen = 255;
834 err = libusb20_dev_get_iface_desc(
835 pdev, interface, name, namelen);
837 if (err != 0)
838 return (LIBUSB_ERROR_OTHER);
840 /* we only want the driver name */
841 ptr = strstr(name, ":");
842 if (ptr != NULL)
843 *ptr = 0;
845 return (0);
849 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
851 return (libusb_detach_kernel_driver(pdev, interface));
855 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
857 int err;
859 if (pdev == NULL)
860 return (LIBUSB_ERROR_INVALID_PARAM);
862 err = libusb20_dev_detach_kernel_driver(
863 pdev, interface);
865 return (err ? LIBUSB_ERROR_OTHER : 0);
869 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
871 if (pdev == NULL)
872 return (LIBUSB_ERROR_INVALID_PARAM);
873 /* stub - currently not supported by libusb20 */
874 return (0);
877 /* Asynchronous device I/O */
879 struct libusb_transfer *
880 libusb_alloc_transfer(int iso_packets)
882 struct libusb_transfer *uxfer;
883 struct libusb_super_transfer *sxfer;
884 int len;
886 len = sizeof(struct libusb_transfer) +
887 sizeof(struct libusb_super_transfer) +
888 (iso_packets * sizeof(libusb_iso_packet_descriptor));
890 sxfer = malloc(len);
891 if (sxfer == NULL)
892 return (NULL);
894 memset(sxfer, 0, len);
896 uxfer = (struct libusb_transfer *)(
897 ((uint8_t *)sxfer) + sizeof(*sxfer));
899 /* set default value */
900 uxfer->num_iso_packets = iso_packets;
902 return (uxfer);
905 void
906 libusb_free_transfer(struct libusb_transfer *uxfer)
908 struct libusb_super_transfer *sxfer;
910 if (uxfer == NULL)
911 return; /* be NULL safe */
913 /* check if we should free the transfer buffer */
914 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
915 free(uxfer->buffer);
917 sxfer = (struct libusb_super_transfer *)(
918 (uint8_t *)uxfer - sizeof(*sxfer));
920 free(sxfer);
923 static uint32_t
924 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
926 uint32_t ret;
928 switch (xfer->type) {
929 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
930 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
931 break;
932 case LIBUSB_TRANSFER_TYPE_CONTROL:
933 ret = 2;
934 break;
935 default:
936 ret = 1;
937 break;
939 return (ret);
942 static int
943 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
945 int ret;
946 int usb_speed;
948 usb_speed = libusb20_dev_get_speed(pdev);
950 switch (xfer->type) {
951 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
952 ret = 0; /* kernel will auto-select */
953 break;
954 case LIBUSB_TRANSFER_TYPE_CONTROL:
955 ret = 1024;
956 break;
957 default:
958 switch (usb_speed) {
959 case LIBUSB20_SPEED_LOW:
960 ret = 256;
961 break;
962 case LIBUSB20_SPEED_FULL:
963 ret = 4096;
964 break;
965 case LIBUSB20_SPEED_SUPER:
966 ret = 65536;
967 break;
968 default:
969 ret = 16384;
970 break;
972 break;
974 return (ret);
977 static int
978 libusb10_convert_error(uint8_t status)
980 ; /* indent fix */
982 switch (status) {
983 case LIBUSB20_TRANSFER_START:
984 case LIBUSB20_TRANSFER_COMPLETED:
985 return (LIBUSB_TRANSFER_COMPLETED);
986 case LIBUSB20_TRANSFER_OVERFLOW:
987 return (LIBUSB_TRANSFER_OVERFLOW);
988 case LIBUSB20_TRANSFER_NO_DEVICE:
989 return (LIBUSB_TRANSFER_NO_DEVICE);
990 case LIBUSB20_TRANSFER_STALL:
991 return (LIBUSB_TRANSFER_STALL);
992 case LIBUSB20_TRANSFER_CANCELLED:
993 return (LIBUSB_TRANSFER_CANCELLED);
994 case LIBUSB20_TRANSFER_TIMED_OUT:
995 return (LIBUSB_TRANSFER_TIMED_OUT);
996 default:
997 return (LIBUSB_TRANSFER_ERROR);
1001 /* This function must be called locked */
1003 static void
1004 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1005 struct libusb_super_transfer *sxfer, int status)
1007 struct libusb_transfer *uxfer;
1008 struct libusb_device *dev;
1010 uxfer = (struct libusb_transfer *)(
1011 ((uint8_t *)sxfer) + sizeof(*sxfer));
1013 if (pxfer != NULL)
1014 libusb20_tr_set_priv_sc1(pxfer, NULL);
1016 /* set transfer status */
1017 uxfer->status = status;
1019 /* update super transfer state */
1020 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1022 dev = libusb_get_device(uxfer->dev_handle);
1024 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1027 /* This function must be called locked */
1029 static void
1030 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1032 struct libusb_super_transfer *sxfer;
1033 struct libusb_transfer *uxfer;
1034 uint32_t actlen;
1035 uint16_t iso_packets;
1036 uint16_t i;
1037 uint8_t status;
1038 uint8_t flags;
1040 status = libusb20_tr_get_status(pxfer);
1041 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1042 actlen = libusb20_tr_get_actual_length(pxfer);
1043 iso_packets = libusb20_tr_get_max_frames(pxfer);
1045 if (sxfer == NULL)
1046 return; /* cancelled - nothing to do */
1048 uxfer = (struct libusb_transfer *)(
1049 ((uint8_t *)sxfer) + sizeof(*sxfer));
1051 if (iso_packets > uxfer->num_iso_packets)
1052 iso_packets = uxfer->num_iso_packets;
1054 if (iso_packets == 0)
1055 return; /* nothing to do */
1057 /* make sure that the number of ISOCHRONOUS packets is valid */
1058 uxfer->num_iso_packets = iso_packets;
1060 flags = uxfer->flags;
1062 switch (status) {
1063 case LIBUSB20_TRANSFER_COMPLETED:
1065 /* update actual length */
1066 uxfer->actual_length = actlen;
1067 for (i = 0; i != iso_packets; i++) {
1068 uxfer->iso_packet_desc[i].actual_length =
1069 libusb20_tr_get_length(pxfer, i);
1071 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1072 break;
1074 case LIBUSB20_TRANSFER_START:
1076 /* setup length(s) */
1077 actlen = 0;
1078 for (i = 0; i != iso_packets; i++) {
1079 libusb20_tr_setup_isoc(pxfer,
1080 &uxfer->buffer[actlen],
1081 uxfer->iso_packet_desc[i].length, i);
1082 actlen += uxfer->iso_packet_desc[i].length;
1085 /* no remainder */
1086 sxfer->rem_len = 0;
1088 libusb20_tr_set_total_frames(pxfer, iso_packets);
1089 libusb20_tr_submit(pxfer);
1091 /* fork another USB transfer, if any */
1092 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1093 break;
1095 default:
1096 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1097 break;
1101 /* This function must be called locked */
1103 static void
1104 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1106 struct libusb_super_transfer *sxfer;
1107 struct libusb_transfer *uxfer;
1108 uint32_t max_bulk;
1109 uint32_t actlen;
1110 uint8_t status;
1111 uint8_t flags;
1113 status = libusb20_tr_get_status(pxfer);
1114 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1115 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1116 actlen = libusb20_tr_get_actual_length(pxfer);
1118 if (sxfer == NULL)
1119 return; /* cancelled - nothing to do */
1121 uxfer = (struct libusb_transfer *)(
1122 ((uint8_t *)sxfer) + sizeof(*sxfer));
1124 flags = uxfer->flags;
1126 switch (status) {
1127 case LIBUSB20_TRANSFER_COMPLETED:
1129 uxfer->actual_length += actlen;
1131 /* check for short packet */
1132 if (sxfer->last_len != actlen) {
1133 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1134 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1135 } else {
1136 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1138 break;
1140 /* check for end of data */
1141 if (sxfer->rem_len == 0) {
1142 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1143 break;
1145 /* FALLTHROUGH */
1147 case LIBUSB20_TRANSFER_START:
1148 if (max_bulk > sxfer->rem_len) {
1149 max_bulk = sxfer->rem_len;
1151 /* setup new BULK or INTERRUPT transaction */
1152 libusb20_tr_setup_bulk(pxfer,
1153 sxfer->curr_data, max_bulk, uxfer->timeout);
1155 /* update counters */
1156 sxfer->last_len = max_bulk;
1157 sxfer->curr_data += max_bulk;
1158 sxfer->rem_len -= max_bulk;
1160 libusb20_tr_submit(pxfer);
1162 /* check if we can fork another USB transfer */
1163 if (sxfer->rem_len == 0)
1164 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1165 break;
1167 default:
1168 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1169 break;
1173 /* This function must be called locked */
1175 static void
1176 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1178 struct libusb_super_transfer *sxfer;
1179 struct libusb_transfer *uxfer;
1180 uint32_t max_bulk;
1181 uint32_t actlen;
1182 uint8_t status;
1183 uint8_t flags;
1185 status = libusb20_tr_get_status(pxfer);
1186 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1187 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1188 actlen = libusb20_tr_get_actual_length(pxfer);
1190 if (sxfer == NULL)
1191 return; /* cancelled - nothing to do */
1193 uxfer = (struct libusb_transfer *)(
1194 ((uint8_t *)sxfer) + sizeof(*sxfer));
1196 flags = uxfer->flags;
1198 switch (status) {
1199 case LIBUSB20_TRANSFER_COMPLETED:
1201 uxfer->actual_length += actlen;
1203 /* subtract length of SETUP packet, if any */
1204 actlen -= libusb20_tr_get_length(pxfer, 0);
1206 /* check for short packet */
1207 if (sxfer->last_len != actlen) {
1208 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1209 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1210 } else {
1211 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1213 break;
1215 /* check for end of data */
1216 if (sxfer->rem_len == 0) {
1217 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1218 break;
1220 /* FALLTHROUGH */
1222 case LIBUSB20_TRANSFER_START:
1223 if (max_bulk > sxfer->rem_len) {
1224 max_bulk = sxfer->rem_len;
1226 /* setup new CONTROL transaction */
1227 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1228 /* next fragment - don't send SETUP packet */
1229 libusb20_tr_set_length(pxfer, 0, 0);
1230 } else {
1231 /* first fragment - send SETUP packet */
1232 libusb20_tr_set_length(pxfer, 8, 0);
1233 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1236 if (max_bulk != 0) {
1237 libusb20_tr_set_length(pxfer, max_bulk, 1);
1238 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1239 libusb20_tr_set_total_frames(pxfer, 2);
1240 } else {
1241 libusb20_tr_set_total_frames(pxfer, 1);
1244 /* update counters */
1245 sxfer->last_len = max_bulk;
1246 sxfer->curr_data += max_bulk;
1247 sxfer->rem_len -= max_bulk;
1249 libusb20_tr_submit(pxfer);
1251 /* check if we can fork another USB transfer */
1252 if (sxfer->rem_len == 0)
1253 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1254 break;
1256 default:
1257 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1258 break;
1262 /* The following function must be called locked */
1264 static void
1265 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1267 struct libusb20_transfer *pxfer0;
1268 struct libusb20_transfer *pxfer1;
1269 struct libusb_super_transfer *sxfer;
1270 struct libusb_transfer *uxfer;
1271 struct libusb_device *dev;
1272 int err;
1273 int buffsize;
1274 int maxframe;
1275 int temp;
1276 uint8_t dummy;
1278 dev = libusb_get_device(pdev);
1280 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1281 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1283 if (pxfer0 == NULL || pxfer1 == NULL)
1284 return; /* shouldn't happen */
1286 temp = 0;
1287 if (libusb20_tr_pending(pxfer0))
1288 temp |= 1;
1289 if (libusb20_tr_pending(pxfer1))
1290 temp |= 2;
1292 switch (temp) {
1293 case 3:
1294 /* wait till one of the transfers complete */
1295 return;
1296 case 2:
1297 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1298 if (sxfer == NULL)
1299 return; /* cancelling */
1300 if (sxfer->rem_len)
1301 return; /* cannot queue another one */
1302 /* swap transfers */
1303 pxfer1 = pxfer0;
1304 break;
1305 case 1:
1306 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1307 if (sxfer == NULL)
1308 return; /* cancelling */
1309 if (sxfer->rem_len)
1310 return; /* cannot queue another one */
1311 /* swap transfers */
1312 pxfer0 = pxfer1;
1313 break;
1314 default:
1315 break;
1318 /* find next transfer on same endpoint */
1319 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1321 uxfer = (struct libusb_transfer *)(
1322 ((uint8_t *)sxfer) + sizeof(*sxfer));
1324 if (uxfer->endpoint == endpoint) {
1325 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1326 sxfer->entry.tqe_prev = NULL;
1327 goto found;
1330 return; /* success */
1332 found:
1334 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1335 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1337 /* reset super transfer state */
1338 sxfer->rem_len = uxfer->length;
1339 sxfer->curr_data = uxfer->buffer;
1340 uxfer->actual_length = 0;
1342 switch (uxfer->type) {
1343 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1344 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1345 break;
1346 case LIBUSB_TRANSFER_TYPE_BULK:
1347 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1348 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1349 break;
1350 case LIBUSB_TRANSFER_TYPE_CONTROL:
1351 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1352 if (sxfer->rem_len < 8)
1353 goto failure;
1355 /* remove SETUP packet from data */
1356 sxfer->rem_len -= 8;
1357 sxfer->curr_data += 8;
1358 break;
1359 default:
1360 goto failure;
1363 buffsize = libusb10_get_buffsize(pdev, uxfer);
1364 maxframe = libusb10_get_maxframe(pdev, uxfer);
1366 /* make sure the transfer is opened */
1367 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1368 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1369 goto failure;
1371 libusb20_tr_start(pxfer0);
1372 return;
1374 failure:
1375 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1377 /* make sure our event loop spins the done handler */
1378 dummy = 0;
1379 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1382 /* The following function must be called unlocked */
1385 libusb_submit_transfer(struct libusb_transfer *uxfer)
1387 struct libusb20_transfer *pxfer0;
1388 struct libusb20_transfer *pxfer1;
1389 struct libusb_super_transfer *sxfer;
1390 struct libusb_device *dev;
1391 uint8_t endpoint;
1392 int err;
1394 if (uxfer == NULL)
1395 return (LIBUSB_ERROR_INVALID_PARAM);
1397 if (uxfer->dev_handle == NULL)
1398 return (LIBUSB_ERROR_INVALID_PARAM);
1400 endpoint = uxfer->endpoint;
1402 dev = libusb_get_device(uxfer->dev_handle);
1404 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1406 sxfer = (struct libusb_super_transfer *)(
1407 (uint8_t *)uxfer - sizeof(*sxfer));
1409 CTX_LOCK(dev->ctx);
1411 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1412 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1414 if (pxfer0 == NULL || pxfer1 == NULL) {
1415 err = LIBUSB_ERROR_OTHER;
1416 } else if ((sxfer->entry.tqe_prev != NULL) ||
1417 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1418 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1419 err = LIBUSB_ERROR_BUSY;
1420 } else {
1422 /* set pending state */
1423 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1425 /* insert transfer into transfer head list */
1426 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1428 /* start work transfers */
1429 libusb10_submit_transfer_sub(
1430 uxfer->dev_handle, endpoint);
1432 err = 0; /* success */
1435 CTX_UNLOCK(dev->ctx);
1437 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1439 return (err);
1442 /* Asynchronous transfer cancel */
1445 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1447 struct libusb20_transfer *pxfer0;
1448 struct libusb20_transfer *pxfer1;
1449 struct libusb_super_transfer *sxfer;
1450 struct libusb_device *dev;
1451 uint8_t endpoint;
1452 int retval;
1454 if (uxfer == NULL)
1455 return (LIBUSB_ERROR_INVALID_PARAM);
1457 /* check if not initialised */
1458 if (uxfer->dev_handle == NULL)
1459 return (LIBUSB_ERROR_NOT_FOUND);
1461 endpoint = uxfer->endpoint;
1463 dev = libusb_get_device(uxfer->dev_handle);
1465 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1467 sxfer = (struct libusb_super_transfer *)(
1468 (uint8_t *)uxfer - sizeof(*sxfer));
1470 retval = 0;
1472 CTX_LOCK(dev->ctx);
1474 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1475 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1477 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1478 /* only update the transfer status */
1479 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1480 retval = LIBUSB_ERROR_NOT_FOUND;
1481 } else if (sxfer->entry.tqe_prev != NULL) {
1482 /* we are lucky - transfer is on a queue */
1483 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1484 sxfer->entry.tqe_prev = NULL;
1485 libusb10_complete_transfer(NULL,
1486 sxfer, LIBUSB_TRANSFER_CANCELLED);
1487 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1488 /* not started */
1489 retval = LIBUSB_ERROR_NOT_FOUND;
1490 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1491 libusb10_complete_transfer(pxfer0,
1492 sxfer, LIBUSB_TRANSFER_CANCELLED);
1493 libusb20_tr_stop(pxfer0);
1494 /* make sure the queue doesn't stall */
1495 libusb10_submit_transfer_sub(
1496 uxfer->dev_handle, endpoint);
1497 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1498 libusb10_complete_transfer(pxfer1,
1499 sxfer, LIBUSB_TRANSFER_CANCELLED);
1500 libusb20_tr_stop(pxfer1);
1501 /* make sure the queue doesn't stall */
1502 libusb10_submit_transfer_sub(
1503 uxfer->dev_handle, endpoint);
1504 } else {
1505 /* not started */
1506 retval = LIBUSB_ERROR_NOT_FOUND;
1509 CTX_UNLOCK(dev->ctx);
1511 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1513 return (retval);
1516 UNEXPORTED void
1517 libusb10_cancel_all_transfer(libusb_device *dev)
1519 /* TODO */
1522 uint16_t
1523 libusb_cpu_to_le16(uint16_t x)
1525 return (htole16(x));
1528 uint16_t
1529 libusb_le16_to_cpu(uint16_t x)
1531 return (le16toh(x));
1534 const char *
1535 libusb_strerror(int code)
1537 switch (code) {
1538 case LIBUSB_SUCCESS:
1539 return ("Success");
1540 case LIBUSB_ERROR_IO:
1541 return ("I/O error");
1542 case LIBUSB_ERROR_INVALID_PARAM:
1543 return ("Invalid parameter");
1544 case LIBUSB_ERROR_ACCESS:
1545 return ("Permissions error");
1546 case LIBUSB_ERROR_NO_DEVICE:
1547 return ("No device");
1548 case LIBUSB_ERROR_NOT_FOUND:
1549 return ("Not found");
1550 case LIBUSB_ERROR_BUSY:
1551 return ("Device busy");
1552 case LIBUSB_ERROR_TIMEOUT:
1553 return ("Timeout");
1554 case LIBUSB_ERROR_OVERFLOW:
1555 return ("Overflow");
1556 case LIBUSB_ERROR_PIPE:
1557 return ("Pipe error");
1558 case LIBUSB_ERROR_INTERRUPTED:
1559 return ("Interrupted");
1560 case LIBUSB_ERROR_NO_MEM:
1561 return ("Out of memory");
1562 case LIBUSB_ERROR_NOT_SUPPORTED:
1563 return ("Not supported");
1564 case LIBUSB_ERROR_OTHER:
1565 return ("Other error");
1566 default:
1567 return ("Unknown error");
1571 const char *
1572 libusb_error_name(int code)
1574 switch (code) {
1575 case LIBUSB_SUCCESS:
1576 return ("LIBUSB_SUCCESS");
1577 case LIBUSB_ERROR_IO:
1578 return ("LIBUSB_ERROR_IO");
1579 case LIBUSB_ERROR_INVALID_PARAM:
1580 return ("LIBUSB_ERROR_INVALID_PARAM");
1581 case LIBUSB_ERROR_ACCESS:
1582 return ("LIBUSB_ERROR_ACCESS");
1583 case LIBUSB_ERROR_NO_DEVICE:
1584 return ("LIBUSB_ERROR_NO_DEVICE");
1585 case LIBUSB_ERROR_NOT_FOUND:
1586 return ("LIBUSB_ERROR_NOT_FOUND");
1587 case LIBUSB_ERROR_BUSY:
1588 return ("LIBUSB_ERROR_BUSY");
1589 case LIBUSB_ERROR_TIMEOUT:
1590 return ("LIBUSB_ERROR_TIMEOUT");
1591 case LIBUSB_ERROR_OVERFLOW:
1592 return ("LIBUSB_ERROR_OVERFLOW");
1593 case LIBUSB_ERROR_PIPE:
1594 return ("LIBUSB_ERROR_PIPE");
1595 case LIBUSB_ERROR_INTERRUPTED:
1596 return ("LIBUSB_ERROR_INTERRUPTED");
1597 case LIBUSB_ERROR_NO_MEM:
1598 return ("LIBUSB_ERROR_NO_MEM");
1599 case LIBUSB_ERROR_NOT_SUPPORTED:
1600 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1601 case LIBUSB_ERROR_OTHER:
1602 return ("LIBUSB_ERROR_OTHER");
1603 default:
1604 return ("LIBUSB_ERROR_UNKNOWN");