kernel/if_wi: Unlock before returning.
[dragonfly.git] / lib / libusb / libusb10.c
blob7c80c2487866743dcd068b11140ce64b56be1d43
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);
120 if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
121 free(ctx);
122 return (LIBUSB_ERROR_NO_MEM);
124 if (pthread_condattr_init(&attr) != 0) {
125 pthread_mutex_destroy(&ctx->ctx_lock);
126 free(ctx);
127 return (LIBUSB_ERROR_NO_MEM);
129 if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
130 pthread_mutex_destroy(&ctx->ctx_lock);
131 pthread_condattr_destroy(&attr);
132 free(ctx);
133 return (LIBUSB_ERROR_OTHER);
135 if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
136 pthread_mutex_destroy(&ctx->ctx_lock);
137 pthread_condattr_destroy(&attr);
138 free(ctx);
139 return (LIBUSB_ERROR_NO_MEM);
141 pthread_condattr_destroy(&attr);
143 ctx->ctx_handler = NO_THREAD;
145 ret = pipe(ctx->ctrl_pipe);
146 if (ret < 0) {
147 pthread_mutex_destroy(&ctx->ctx_lock);
148 pthread_cond_destroy(&ctx->ctx_cond);
149 free(ctx);
150 return (LIBUSB_ERROR_OTHER);
152 /* set non-blocking mode on the control pipe to avoid deadlock */
153 libusb_set_nonblocking(ctx->ctrl_pipe[0]);
154 libusb_set_nonblocking(ctx->ctrl_pipe[1]);
156 libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
158 pthread_mutex_lock(&default_context_lock);
159 if (usbi_default_context == NULL) {
160 usbi_default_context = ctx;
162 pthread_mutex_unlock(&default_context_lock);
164 if (context)
165 *context = ctx;
167 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
169 return (0);
172 void
173 libusb_exit(libusb_context *ctx)
175 ctx = GET_CONTEXT(ctx);
177 if (ctx == NULL)
178 return;
180 /* XXX cleanup devices */
182 libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
183 close(ctx->ctrl_pipe[0]);
184 close(ctx->ctrl_pipe[1]);
185 pthread_mutex_destroy(&ctx->ctx_lock);
186 pthread_cond_destroy(&ctx->ctx_cond);
188 pthread_mutex_lock(&default_context_lock);
189 if (ctx == usbi_default_context) {
190 usbi_default_context = NULL;
192 pthread_mutex_unlock(&default_context_lock);
194 free(ctx);
197 /* Device handling and initialisation. */
199 ssize_t
200 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
202 struct libusb20_backend *usb_backend;
203 struct libusb20_device *pdev;
204 struct libusb_device *dev;
205 int i;
207 ctx = GET_CONTEXT(ctx);
209 if (ctx == NULL)
210 return (LIBUSB_ERROR_INVALID_PARAM);
212 if (list == NULL)
213 return (LIBUSB_ERROR_INVALID_PARAM);
215 usb_backend = libusb20_be_alloc_default();
216 if (usb_backend == NULL)
217 return (LIBUSB_ERROR_NO_MEM);
219 /* figure out how many USB devices are present */
220 pdev = NULL;
221 i = 0;
222 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
223 i++;
225 /* allocate device pointer list */
226 *list = malloc((i + 1) * sizeof(void *));
227 if (*list == NULL) {
228 libusb20_be_free(usb_backend);
229 return (LIBUSB_ERROR_NO_MEM);
231 /* create libusb v1.0 compliant devices */
232 i = 0;
233 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
235 dev = malloc(sizeof(*dev));
236 if (dev == NULL) {
237 while (i != 0) {
238 libusb_unref_device((*list)[i - 1]);
239 i--;
241 free(*list);
242 *list = NULL;
243 libusb20_be_free(usb_backend);
244 return (LIBUSB_ERROR_NO_MEM);
246 /* get device into libUSB v1.0 list */
247 libusb20_be_dequeue_device(usb_backend, pdev);
249 memset(dev, 0, sizeof(*dev));
251 /* init transfer queues */
252 TAILQ_INIT(&dev->tr_head);
254 /* set context we belong to */
255 dev->ctx = ctx;
257 /* link together the two structures */
258 dev->os_priv = pdev;
259 pdev->privLuData = dev;
261 (*list)[i] = libusb_ref_device(dev);
262 i++;
264 (*list)[i] = NULL;
266 libusb20_be_free(usb_backend);
267 return (i);
270 void
271 libusb_free_device_list(libusb_device **list, int unref_devices)
273 int i;
275 if (list == NULL)
276 return; /* be NULL safe */
278 if (unref_devices) {
279 for (i = 0; list[i] != NULL; i++)
280 libusb_unref_device(list[i]);
282 free(list);
285 uint8_t
286 libusb_get_bus_number(libusb_device *dev)
288 if (dev == NULL)
289 return (0); /* should not happen */
290 return (libusb20_dev_get_bus_number(dev->os_priv));
294 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
296 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
300 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
301 uint8_t bufsize)
303 return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
306 uint8_t
307 libusb_get_device_address(libusb_device *dev)
309 if (dev == NULL)
310 return (0); /* should not happen */
311 return (libusb20_dev_get_address(dev->os_priv));
314 enum libusb_speed
315 libusb_get_device_speed(libusb_device *dev)
317 if (dev == NULL)
318 return (LIBUSB_SPEED_UNKNOWN); /* should not happen */
320 switch (libusb20_dev_get_speed(dev->os_priv)) {
321 case LIBUSB20_SPEED_LOW:
322 return (LIBUSB_SPEED_LOW);
323 case LIBUSB20_SPEED_FULL:
324 return (LIBUSB_SPEED_FULL);
325 case LIBUSB20_SPEED_HIGH:
326 return (LIBUSB_SPEED_HIGH);
327 case LIBUSB20_SPEED_SUPER:
328 return (LIBUSB_SPEED_SUPER);
329 default:
330 break;
332 return (LIBUSB_SPEED_UNKNOWN);
336 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
338 struct libusb_config_descriptor *pdconf;
339 struct libusb_interface *pinf;
340 struct libusb_interface_descriptor *pdinf;
341 struct libusb_endpoint_descriptor *pdend;
342 int i;
343 int j;
344 int k;
345 int ret;
347 if (dev == NULL)
348 return (LIBUSB_ERROR_NO_DEVICE);
350 ret = libusb_get_active_config_descriptor(dev, &pdconf);
351 if (ret < 0)
352 return (ret);
354 ret = LIBUSB_ERROR_NOT_FOUND;
355 for (i = 0; i < pdconf->bNumInterfaces; i++) {
356 pinf = &pdconf->interface[i];
357 for (j = 0; j < pinf->num_altsetting; j++) {
358 pdinf = &pinf->altsetting[j];
359 for (k = 0; k < pdinf->bNumEndpoints; k++) {
360 pdend = &pdinf->endpoint[k];
361 if (pdend->bEndpointAddress == endpoint) {
362 ret = pdend->wMaxPacketSize;
363 goto out;
369 out:
370 libusb_free_config_descriptor(pdconf);
371 return (ret);
375 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
377 int multiplier;
378 int ret;
380 ret = libusb_get_max_packet_size(dev, endpoint);
382 switch (libusb20_dev_get_speed(dev->os_priv)) {
383 case LIBUSB20_SPEED_LOW:
384 case LIBUSB20_SPEED_FULL:
385 break;
386 default:
387 if (ret > -1) {
388 multiplier = (1 + ((ret >> 11) & 3));
389 if (multiplier > 3)
390 multiplier = 3;
391 ret = (ret & 0x7FF) * multiplier;
393 break;
395 return (ret);
398 libusb_device *
399 libusb_ref_device(libusb_device *dev)
401 if (dev == NULL)
402 return (NULL); /* be NULL safe */
404 CTX_LOCK(dev->ctx);
405 dev->refcnt++;
406 CTX_UNLOCK(dev->ctx);
408 return (dev);
411 void
412 libusb_unref_device(libusb_device *dev)
414 if (dev == NULL)
415 return; /* be NULL safe */
417 CTX_LOCK(dev->ctx);
418 dev->refcnt--;
419 CTX_UNLOCK(dev->ctx);
421 if (dev->refcnt == 0) {
422 libusb20_dev_free(dev->os_priv);
423 free(dev);
428 libusb_open(libusb_device *dev, libusb_device_handle **devh)
430 libusb_context *ctx = dev->ctx;
431 struct libusb20_device *pdev = dev->os_priv;
432 uint8_t dummy;
433 int err;
435 if (devh == NULL)
436 return (LIBUSB_ERROR_INVALID_PARAM);
438 /* set default device handle value */
439 *devh = NULL;
441 dev = libusb_ref_device(dev);
442 if (dev == NULL)
443 return (LIBUSB_ERROR_INVALID_PARAM);
445 err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
446 if (err) {
447 libusb_unref_device(dev);
448 return (LIBUSB_ERROR_NO_MEM);
450 libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
451 POLLOUT | POLLRDNORM | POLLWRNORM);
453 /* make sure our event loop detects the new device */
454 dummy = 0;
455 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
456 if (err < (int)sizeof(dummy)) {
457 /* ignore error, if any */
458 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
460 *devh = pdev;
462 return (0);
465 libusb_device_handle *
466 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
467 uint16_t product_id)
469 struct libusb_device **devs;
470 struct libusb20_device *pdev;
471 struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
472 int i;
473 int j;
475 ctx = GET_CONTEXT(ctx);
476 if (ctx == NULL)
477 return (NULL); /* be NULL safe */
479 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
481 if ((i = libusb_get_device_list(ctx, &devs)) < 0)
482 return (NULL);
484 pdev = NULL;
485 for (j = 0; j < i; j++) {
486 struct libusb20_device *tdev;
488 tdev = devs[j]->os_priv;
489 pdesc = libusb20_dev_get_device_desc(tdev);
491 * NOTE: The USB library will automatically swap the
492 * fields in the device descriptor to be of host
493 * endian type!
495 if (pdesc->idVendor == vendor_id &&
496 pdesc->idProduct == product_id) {
497 libusb_open(devs[j], &pdev);
498 break;
502 libusb_free_device_list(devs, 1);
503 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
504 return (pdev);
507 void
508 libusb_close(struct libusb20_device *pdev)
510 libusb_context *ctx;
511 struct libusb_device *dev;
512 uint8_t dummy;
513 int err;
515 if (pdev == NULL)
516 return; /* be NULL safe */
518 dev = libusb_get_device(pdev);
519 ctx = dev->ctx;
521 libusb10_remove_pollfd(ctx, &dev->dev_poll);
523 libusb20_dev_close(pdev);
525 /* unref will free the "pdev" when the refcount reaches zero */
526 libusb_unref_device(dev);
528 /* make sure our event loop detects the closed device */
529 dummy = 0;
530 err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
531 if (err < (int)sizeof(dummy)) {
532 /* ignore error, if any */
533 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
537 libusb_device *
538 libusb_get_device(struct libusb20_device *pdev)
540 if (pdev == NULL)
541 return (NULL);
542 return ((libusb_device *)pdev->privLuData);
546 libusb_get_configuration(struct libusb20_device *pdev, int *config)
548 struct libusb20_config *pconf;
550 if (pdev == NULL || config == NULL)
551 return (LIBUSB_ERROR_INVALID_PARAM);
553 pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
554 if (pconf == NULL)
555 return (LIBUSB_ERROR_NO_MEM);
557 *config = pconf->desc.bConfigurationValue;
559 free(pconf);
561 return (0);
565 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
567 struct libusb20_config *pconf;
568 struct libusb_device *dev;
569 int err;
570 uint8_t i;
572 dev = libusb_get_device(pdev);
573 if (dev == NULL)
574 return (LIBUSB_ERROR_INVALID_PARAM);
576 if (configuration < 1) {
577 /* unconfigure */
578 i = 255;
579 } else {
580 for (i = 0; i != 255; i++) {
581 uint8_t found;
583 pconf = libusb20_dev_alloc_config(pdev, i);
584 if (pconf == NULL)
585 return (LIBUSB_ERROR_INVALID_PARAM);
586 found = (pconf->desc.bConfigurationValue
587 == configuration);
588 free(pconf);
590 if (found)
591 goto set_config;
593 return (LIBUSB_ERROR_INVALID_PARAM);
596 set_config:
598 libusb10_cancel_all_transfer(dev);
600 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
602 err = libusb20_dev_set_config_index(pdev, i);
604 libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
605 POLLOUT | POLLRDNORM | POLLWRNORM);
607 return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
611 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
613 libusb_device *dev;
615 dev = libusb_get_device(pdev);
616 if (dev == NULL)
617 return (LIBUSB_ERROR_INVALID_PARAM);
619 if (interface_number < 0 || interface_number > 31)
620 return (LIBUSB_ERROR_INVALID_PARAM);
622 CTX_LOCK(dev->ctx);
623 dev->claimed_interfaces |= (1 << interface_number);
624 CTX_UNLOCK(dev->ctx);
626 return (0);
630 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
632 libusb_device *dev;
633 int err = 0;
635 dev = libusb_get_device(pdev);
636 if (dev == NULL)
637 return (LIBUSB_ERROR_INVALID_PARAM);
639 if (interface_number < 0 || interface_number > 31)
640 return (LIBUSB_ERROR_INVALID_PARAM);
642 CTX_LOCK(dev->ctx);
643 if (!(dev->claimed_interfaces & (1 << interface_number)))
644 err = LIBUSB_ERROR_NOT_FOUND;
646 if (!err)
647 dev->claimed_interfaces &= ~(1 << interface_number);
648 CTX_UNLOCK(dev->ctx);
649 return (err);
653 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
654 int interface_number, int alternate_setting)
656 libusb_device *dev;
657 int err = 0;
659 dev = libusb_get_device(pdev);
660 if (dev == NULL)
661 return (LIBUSB_ERROR_INVALID_PARAM);
663 if (interface_number < 0 || interface_number > 31)
664 return (LIBUSB_ERROR_INVALID_PARAM);
666 CTX_LOCK(dev->ctx);
667 if (!(dev->claimed_interfaces & (1 << interface_number)))
668 err = LIBUSB_ERROR_NOT_FOUND;
669 CTX_UNLOCK(dev->ctx);
671 if (err)
672 return (err);
674 libusb10_cancel_all_transfer(dev);
676 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
678 err = libusb20_dev_set_alt_index(pdev,
679 interface_number, alternate_setting);
681 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
682 pdev, libusb20_dev_get_fd(pdev),
683 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
685 return (err ? LIBUSB_ERROR_OTHER : 0);
688 static struct libusb20_transfer *
689 libusb10_get_transfer(struct libusb20_device *pdev,
690 uint8_t endpoint, uint8_t xfer_index)
692 xfer_index &= 1; /* double buffering */
694 xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
696 if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
697 /* this is an IN endpoint */
698 xfer_index |= 2;
700 return (libusb20_tr_get_pointer(pdev, xfer_index));
704 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
706 struct libusb20_transfer *xfer;
707 struct libusb_device *dev;
708 int err;
710 xfer = libusb10_get_transfer(pdev, endpoint, 0);
711 if (xfer == NULL)
712 return (LIBUSB_ERROR_INVALID_PARAM);
714 dev = libusb_get_device(pdev);
715 if (dev == NULL)
716 return (LIBUSB_ERROR_INVALID_PARAM);
718 CTX_LOCK(dev->ctx);
719 err = libusb20_tr_open(xfer, 0, 1, endpoint);
720 CTX_UNLOCK(dev->ctx);
722 if (err != 0 && err != LIBUSB20_ERROR_BUSY)
723 return (LIBUSB_ERROR_OTHER);
725 libusb20_tr_clear_stall_sync(xfer);
727 /* check if we opened the transfer */
728 if (err == 0) {
729 CTX_LOCK(dev->ctx);
730 libusb20_tr_close(xfer);
731 CTX_UNLOCK(dev->ctx);
733 return (0); /* success */
737 libusb_reset_device(struct libusb20_device *pdev)
739 libusb_device *dev;
740 int err;
742 dev = libusb_get_device(pdev);
743 if (dev == NULL)
744 return (LIBUSB_ERROR_INVALID_PARAM);
746 libusb10_cancel_all_transfer(dev);
748 libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
750 err = libusb20_dev_reset(pdev);
752 libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
753 pdev, libusb20_dev_get_fd(pdev),
754 POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
756 return (err ? LIBUSB_ERROR_OTHER : 0);
760 libusb_check_connected(struct libusb20_device *pdev)
762 libusb_device *dev;
763 int err;
765 dev = libusb_get_device(pdev);
766 if (dev == NULL)
767 return (LIBUSB_ERROR_INVALID_PARAM);
769 err = libusb20_dev_check_connected(pdev);
771 return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
775 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
777 if (pdev == NULL)
778 return (LIBUSB_ERROR_INVALID_PARAM);
780 if (libusb20_dev_kernel_driver_active(pdev, interface))
781 return (0); /* no kernel driver is active */
782 else
783 return (1); /* kernel driver is active */
787 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
788 char *name, int namelen)
790 return (libusb_get_driver(pdev, interface, name, namelen));
794 libusb_get_driver(struct libusb20_device *pdev, int interface,
795 char *name, int namelen)
797 char *ptr;
798 int err;
800 if (pdev == NULL)
801 return (LIBUSB_ERROR_INVALID_PARAM);
802 if (namelen < 1)
803 return (LIBUSB_ERROR_INVALID_PARAM);
804 if (namelen > 255)
805 namelen = 255;
807 err = libusb20_dev_get_iface_desc(
808 pdev, interface, name, namelen);
810 if (err != 0)
811 return (LIBUSB_ERROR_OTHER);
813 /* we only want the driver name */
814 ptr = strstr(name, ":");
815 if (ptr != NULL)
816 *ptr = 0;
818 return (0);
822 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
824 return (libusb_detach_kernel_driver(pdev, interface));
828 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
830 int err;
832 if (pdev == NULL)
833 return (LIBUSB_ERROR_INVALID_PARAM);
835 err = libusb20_dev_detach_kernel_driver(
836 pdev, interface);
838 return (err ? LIBUSB_ERROR_OTHER : 0);
842 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
844 if (pdev == NULL)
845 return (LIBUSB_ERROR_INVALID_PARAM);
846 /* stub - currently not supported by libusb20 */
847 return (0);
850 /* Asynchronous device I/O */
852 struct libusb_transfer *
853 libusb_alloc_transfer(int iso_packets)
855 struct libusb_transfer *uxfer;
856 struct libusb_super_transfer *sxfer;
857 int len;
859 len = sizeof(struct libusb_transfer) +
860 sizeof(struct libusb_super_transfer) +
861 (iso_packets * sizeof(libusb_iso_packet_descriptor));
863 sxfer = malloc(len);
864 if (sxfer == NULL)
865 return (NULL);
867 memset(sxfer, 0, len);
869 uxfer = (struct libusb_transfer *)(
870 ((uint8_t *)sxfer) + sizeof(*sxfer));
872 /* set default value */
873 uxfer->num_iso_packets = iso_packets;
875 return (uxfer);
878 void
879 libusb_free_transfer(struct libusb_transfer *uxfer)
881 struct libusb_super_transfer *sxfer;
883 if (uxfer == NULL)
884 return; /* be NULL safe */
886 /* check if we should free the transfer buffer */
887 if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
888 free(uxfer->buffer);
890 sxfer = (struct libusb_super_transfer *)(
891 (uint8_t *)uxfer - sizeof(*sxfer));
893 free(sxfer);
896 static uint32_t
897 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
899 uint32_t ret;
901 switch (xfer->type) {
902 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
903 ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */
904 break;
905 case LIBUSB_TRANSFER_TYPE_CONTROL:
906 ret = 2;
907 break;
908 default:
909 ret = 1;
910 break;
912 return (ret);
915 static int
916 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
918 int ret;
919 int usb_speed;
921 usb_speed = libusb20_dev_get_speed(pdev);
923 switch (xfer->type) {
924 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
925 ret = 0; /* kernel will auto-select */
926 break;
927 case LIBUSB_TRANSFER_TYPE_CONTROL:
928 ret = 1024;
929 break;
930 default:
931 switch (usb_speed) {
932 case LIBUSB20_SPEED_LOW:
933 ret = 256;
934 break;
935 case LIBUSB20_SPEED_FULL:
936 ret = 4096;
937 break;
938 case LIBUSB20_SPEED_SUPER:
939 ret = 65536;
940 break;
941 default:
942 ret = 16384;
943 break;
945 break;
947 return (ret);
950 static int
951 libusb10_convert_error(uint8_t status)
953 ; /* indent fix */
955 switch (status) {
956 case LIBUSB20_TRANSFER_START:
957 case LIBUSB20_TRANSFER_COMPLETED:
958 return (LIBUSB_TRANSFER_COMPLETED);
959 case LIBUSB20_TRANSFER_OVERFLOW:
960 return (LIBUSB_TRANSFER_OVERFLOW);
961 case LIBUSB20_TRANSFER_NO_DEVICE:
962 return (LIBUSB_TRANSFER_NO_DEVICE);
963 case LIBUSB20_TRANSFER_STALL:
964 return (LIBUSB_TRANSFER_STALL);
965 case LIBUSB20_TRANSFER_CANCELLED:
966 return (LIBUSB_TRANSFER_CANCELLED);
967 case LIBUSB20_TRANSFER_TIMED_OUT:
968 return (LIBUSB_TRANSFER_TIMED_OUT);
969 default:
970 return (LIBUSB_TRANSFER_ERROR);
974 /* This function must be called locked */
976 static void
977 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
978 struct libusb_super_transfer *sxfer, int status)
980 struct libusb_transfer *uxfer;
981 struct libusb_device *dev;
983 uxfer = (struct libusb_transfer *)(
984 ((uint8_t *)sxfer) + sizeof(*sxfer));
986 if (pxfer != NULL)
987 libusb20_tr_set_priv_sc1(pxfer, NULL);
989 /* set transfer status */
990 uxfer->status = status;
992 /* update super transfer state */
993 sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
995 dev = libusb_get_device(uxfer->dev_handle);
997 TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1000 /* This function must be called locked */
1002 static void
1003 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1005 struct libusb_super_transfer *sxfer;
1006 struct libusb_transfer *uxfer;
1007 uint32_t actlen;
1008 uint16_t iso_packets;
1009 uint16_t i;
1010 uint8_t status;
1011 uint8_t flags;
1013 status = libusb20_tr_get_status(pxfer);
1014 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1015 actlen = libusb20_tr_get_actual_length(pxfer);
1016 iso_packets = libusb20_tr_get_max_frames(pxfer);
1018 if (sxfer == NULL)
1019 return; /* cancelled - nothing to do */
1021 uxfer = (struct libusb_transfer *)(
1022 ((uint8_t *)sxfer) + sizeof(*sxfer));
1024 if (iso_packets > uxfer->num_iso_packets)
1025 iso_packets = uxfer->num_iso_packets;
1027 if (iso_packets == 0)
1028 return; /* nothing to do */
1030 /* make sure that the number of ISOCHRONOUS packets is valid */
1031 uxfer->num_iso_packets = iso_packets;
1033 flags = uxfer->flags;
1035 switch (status) {
1036 case LIBUSB20_TRANSFER_COMPLETED:
1038 /* update actual length */
1039 uxfer->actual_length = actlen;
1040 for (i = 0; i != iso_packets; i++) {
1041 uxfer->iso_packet_desc[i].actual_length =
1042 libusb20_tr_get_length(pxfer, i);
1044 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1045 break;
1047 case LIBUSB20_TRANSFER_START:
1049 /* setup length(s) */
1050 actlen = 0;
1051 for (i = 0; i != iso_packets; i++) {
1052 libusb20_tr_setup_isoc(pxfer,
1053 &uxfer->buffer[actlen],
1054 uxfer->iso_packet_desc[i].length, i);
1055 actlen += uxfer->iso_packet_desc[i].length;
1058 /* no remainder */
1059 sxfer->rem_len = 0;
1061 libusb20_tr_set_total_frames(pxfer, iso_packets);
1062 libusb20_tr_submit(pxfer);
1064 /* fork another USB transfer, if any */
1065 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1066 break;
1068 default:
1069 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1070 break;
1074 /* This function must be called locked */
1076 static void
1077 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1079 struct libusb_super_transfer *sxfer;
1080 struct libusb_transfer *uxfer;
1081 uint32_t max_bulk;
1082 uint32_t actlen;
1083 uint8_t status;
1084 uint8_t flags;
1086 status = libusb20_tr_get_status(pxfer);
1087 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1088 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1089 actlen = libusb20_tr_get_actual_length(pxfer);
1091 if (sxfer == NULL)
1092 return; /* cancelled - nothing to do */
1094 uxfer = (struct libusb_transfer *)(
1095 ((uint8_t *)sxfer) + sizeof(*sxfer));
1097 flags = uxfer->flags;
1099 switch (status) {
1100 case LIBUSB20_TRANSFER_COMPLETED:
1102 uxfer->actual_length += actlen;
1104 /* check for short packet */
1105 if (sxfer->last_len != actlen) {
1106 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1107 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1108 } else {
1109 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1111 break;
1113 /* check for end of data */
1114 if (sxfer->rem_len == 0) {
1115 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1116 break;
1118 /* FALLTHROUGH */
1120 case LIBUSB20_TRANSFER_START:
1121 if (max_bulk > sxfer->rem_len) {
1122 max_bulk = sxfer->rem_len;
1124 /* setup new BULK or INTERRUPT transaction */
1125 libusb20_tr_setup_bulk(pxfer,
1126 sxfer->curr_data, max_bulk, uxfer->timeout);
1128 /* update counters */
1129 sxfer->last_len = max_bulk;
1130 sxfer->curr_data += max_bulk;
1131 sxfer->rem_len -= max_bulk;
1133 libusb20_tr_submit(pxfer);
1135 /* check if we can fork another USB transfer */
1136 if (sxfer->rem_len == 0)
1137 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1138 break;
1140 default:
1141 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1142 break;
1146 /* This function must be called locked */
1148 static void
1149 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1151 struct libusb_super_transfer *sxfer;
1152 struct libusb_transfer *uxfer;
1153 uint32_t max_bulk;
1154 uint32_t actlen;
1155 uint8_t status;
1156 uint8_t flags;
1158 status = libusb20_tr_get_status(pxfer);
1159 sxfer = libusb20_tr_get_priv_sc1(pxfer);
1160 max_bulk = libusb20_tr_get_max_total_length(pxfer);
1161 actlen = libusb20_tr_get_actual_length(pxfer);
1163 if (sxfer == NULL)
1164 return; /* cancelled - nothing to do */
1166 uxfer = (struct libusb_transfer *)(
1167 ((uint8_t *)sxfer) + sizeof(*sxfer));
1169 flags = uxfer->flags;
1171 switch (status) {
1172 case LIBUSB20_TRANSFER_COMPLETED:
1174 uxfer->actual_length += actlen;
1176 /* subtract length of SETUP packet, if any */
1177 actlen -= libusb20_tr_get_length(pxfer, 0);
1179 /* check for short packet */
1180 if (sxfer->last_len != actlen) {
1181 if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1182 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1183 } else {
1184 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1186 break;
1188 /* check for end of data */
1189 if (sxfer->rem_len == 0) {
1190 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1191 break;
1193 /* FALLTHROUGH */
1195 case LIBUSB20_TRANSFER_START:
1196 if (max_bulk > sxfer->rem_len) {
1197 max_bulk = sxfer->rem_len;
1199 /* setup new CONTROL transaction */
1200 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1201 /* next fragment - don't send SETUP packet */
1202 libusb20_tr_set_length(pxfer, 0, 0);
1203 } else {
1204 /* first fragment - send SETUP packet */
1205 libusb20_tr_set_length(pxfer, 8, 0);
1206 libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1209 if (max_bulk != 0) {
1210 libusb20_tr_set_length(pxfer, max_bulk, 1);
1211 libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1212 libusb20_tr_set_total_frames(pxfer, 2);
1213 } else {
1214 libusb20_tr_set_total_frames(pxfer, 1);
1217 /* update counters */
1218 sxfer->last_len = max_bulk;
1219 sxfer->curr_data += max_bulk;
1220 sxfer->rem_len -= max_bulk;
1222 libusb20_tr_submit(pxfer);
1224 /* check if we can fork another USB transfer */
1225 if (sxfer->rem_len == 0)
1226 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1227 break;
1229 default:
1230 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1231 break;
1235 /* The following function must be called locked */
1237 static void
1238 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1240 struct libusb20_transfer *pxfer0;
1241 struct libusb20_transfer *pxfer1;
1242 struct libusb_super_transfer *sxfer;
1243 struct libusb_transfer *uxfer;
1244 struct libusb_device *dev;
1245 int err;
1246 int buffsize;
1247 int maxframe;
1248 int temp;
1249 uint8_t dummy;
1251 dev = libusb_get_device(pdev);
1253 pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1254 pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1256 if (pxfer0 == NULL || pxfer1 == NULL)
1257 return; /* shouldn't happen */
1259 temp = 0;
1260 if (libusb20_tr_pending(pxfer0))
1261 temp |= 1;
1262 if (libusb20_tr_pending(pxfer1))
1263 temp |= 2;
1265 switch (temp) {
1266 case 3:
1267 /* wait till one of the transfers complete */
1268 return;
1269 case 2:
1270 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1271 if (sxfer == NULL)
1272 return; /* cancelling */
1273 if (sxfer->rem_len)
1274 return; /* cannot queue another one */
1275 /* swap transfers */
1276 pxfer1 = pxfer0;
1277 break;
1278 case 1:
1279 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1280 if (sxfer == NULL)
1281 return; /* cancelling */
1282 if (sxfer->rem_len)
1283 return; /* cannot queue another one */
1284 /* swap transfers */
1285 pxfer0 = pxfer1;
1286 break;
1287 default:
1288 break;
1291 /* find next transfer on same endpoint */
1292 TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1294 uxfer = (struct libusb_transfer *)(
1295 ((uint8_t *)sxfer) + sizeof(*sxfer));
1297 if (uxfer->endpoint == endpoint) {
1298 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1299 sxfer->entry.tqe_prev = NULL;
1300 goto found;
1303 return; /* success */
1305 found:
1307 libusb20_tr_set_priv_sc0(pxfer0, pdev);
1308 libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1310 /* reset super transfer state */
1311 sxfer->rem_len = uxfer->length;
1312 sxfer->curr_data = uxfer->buffer;
1313 uxfer->actual_length = 0;
1315 switch (uxfer->type) {
1316 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1317 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1318 break;
1319 case LIBUSB_TRANSFER_TYPE_BULK:
1320 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1321 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1322 break;
1323 case LIBUSB_TRANSFER_TYPE_CONTROL:
1324 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1325 if (sxfer->rem_len < 8)
1326 goto failure;
1328 /* remove SETUP packet from data */
1329 sxfer->rem_len -= 8;
1330 sxfer->curr_data += 8;
1331 break;
1332 default:
1333 goto failure;
1336 buffsize = libusb10_get_buffsize(pdev, uxfer);
1337 maxframe = libusb10_get_maxframe(pdev, uxfer);
1339 /* make sure the transfer is opened */
1340 err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1341 if (err && (err != LIBUSB20_ERROR_BUSY)) {
1342 goto failure;
1344 libusb20_tr_start(pxfer0);
1345 return;
1347 failure:
1348 libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1350 /* make sure our event loop spins the done handler */
1351 dummy = 0;
1352 err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1355 /* The following function must be called unlocked */
1358 libusb_submit_transfer(struct libusb_transfer *uxfer)
1360 struct libusb20_transfer *pxfer0;
1361 struct libusb20_transfer *pxfer1;
1362 struct libusb_super_transfer *sxfer;
1363 struct libusb_device *dev;
1364 uint8_t endpoint;
1365 int err;
1367 if (uxfer == NULL)
1368 return (LIBUSB_ERROR_INVALID_PARAM);
1370 if (uxfer->dev_handle == NULL)
1371 return (LIBUSB_ERROR_INVALID_PARAM);
1373 endpoint = uxfer->endpoint;
1375 dev = libusb_get_device(uxfer->dev_handle);
1377 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1379 sxfer = (struct libusb_super_transfer *)(
1380 (uint8_t *)uxfer - sizeof(*sxfer));
1382 CTX_LOCK(dev->ctx);
1384 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1385 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1387 if (pxfer0 == NULL || pxfer1 == NULL) {
1388 err = LIBUSB_ERROR_OTHER;
1389 } else if ((sxfer->entry.tqe_prev != NULL) ||
1390 (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1391 (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1392 err = LIBUSB_ERROR_BUSY;
1393 } else {
1395 /* set pending state */
1396 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1398 /* insert transfer into transfer head list */
1399 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1401 /* start work transfers */
1402 libusb10_submit_transfer_sub(
1403 uxfer->dev_handle, endpoint);
1405 err = 0; /* success */
1408 CTX_UNLOCK(dev->ctx);
1410 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1412 return (err);
1415 /* Asynchronous transfer cancel */
1418 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1420 struct libusb20_transfer *pxfer0;
1421 struct libusb20_transfer *pxfer1;
1422 struct libusb_super_transfer *sxfer;
1423 struct libusb_device *dev;
1424 uint8_t endpoint;
1425 int retval;
1427 if (uxfer == NULL)
1428 return (LIBUSB_ERROR_INVALID_PARAM);
1430 /* check if not initialised */
1431 if (uxfer->dev_handle == NULL)
1432 return (LIBUSB_ERROR_NOT_FOUND);
1434 endpoint = uxfer->endpoint;
1436 dev = libusb_get_device(uxfer->dev_handle);
1438 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1440 sxfer = (struct libusb_super_transfer *)(
1441 (uint8_t *)uxfer - sizeof(*sxfer));
1443 retval = 0;
1445 CTX_LOCK(dev->ctx);
1447 pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1448 pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1450 if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1451 /* only update the transfer status */
1452 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1453 retval = LIBUSB_ERROR_NOT_FOUND;
1454 } else if (sxfer->entry.tqe_prev != NULL) {
1455 /* we are lucky - transfer is on a queue */
1456 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1457 sxfer->entry.tqe_prev = NULL;
1458 libusb10_complete_transfer(NULL,
1459 sxfer, LIBUSB_TRANSFER_CANCELLED);
1460 } else if (pxfer0 == NULL || pxfer1 == NULL) {
1461 /* not started */
1462 retval = LIBUSB_ERROR_NOT_FOUND;
1463 } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1464 libusb10_complete_transfer(pxfer0,
1465 sxfer, LIBUSB_TRANSFER_CANCELLED);
1466 libusb20_tr_stop(pxfer0);
1467 /* make sure the queue doesn't stall */
1468 libusb10_submit_transfer_sub(
1469 uxfer->dev_handle, endpoint);
1470 } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1471 libusb10_complete_transfer(pxfer1,
1472 sxfer, LIBUSB_TRANSFER_CANCELLED);
1473 libusb20_tr_stop(pxfer1);
1474 /* make sure the queue doesn't stall */
1475 libusb10_submit_transfer_sub(
1476 uxfer->dev_handle, endpoint);
1477 } else {
1478 /* not started */
1479 retval = LIBUSB_ERROR_NOT_FOUND;
1482 CTX_UNLOCK(dev->ctx);
1484 DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1486 return (retval);
1489 UNEXPORTED void
1490 libusb10_cancel_all_transfer(libusb_device *dev)
1492 /* TODO */
1495 uint16_t
1496 libusb_cpu_to_le16(uint16_t x)
1498 return (htole16(x));
1501 uint16_t
1502 libusb_le16_to_cpu(uint16_t x)
1504 return (le16toh(x));
1507 const char *
1508 libusb_strerror(int code)
1510 switch (code) {
1511 case LIBUSB_SUCCESS:
1512 return ("Success");
1513 case LIBUSB_ERROR_IO:
1514 return ("I/O error");
1515 case LIBUSB_ERROR_INVALID_PARAM:
1516 return ("Invalid parameter");
1517 case LIBUSB_ERROR_ACCESS:
1518 return ("Permissions error");
1519 case LIBUSB_ERROR_NO_DEVICE:
1520 return ("No device");
1521 case LIBUSB_ERROR_NOT_FOUND:
1522 return ("Not found");
1523 case LIBUSB_ERROR_BUSY:
1524 return ("Device busy");
1525 case LIBUSB_ERROR_TIMEOUT:
1526 return ("Timeout");
1527 case LIBUSB_ERROR_OVERFLOW:
1528 return ("Overflow");
1529 case LIBUSB_ERROR_PIPE:
1530 return ("Pipe error");
1531 case LIBUSB_ERROR_INTERRUPTED:
1532 return ("Interrupted");
1533 case LIBUSB_ERROR_NO_MEM:
1534 return ("Out of memory");
1535 case LIBUSB_ERROR_NOT_SUPPORTED:
1536 return ("Not supported");
1537 case LIBUSB_ERROR_OTHER:
1538 return ("Other error");
1539 default:
1540 return ("Unknown error");
1544 const char *
1545 libusb_error_name(int code)
1547 switch (code) {
1548 case LIBUSB_SUCCESS:
1549 return ("LIBUSB_SUCCESS");
1550 case LIBUSB_ERROR_IO:
1551 return ("LIBUSB_ERROR_IO");
1552 case LIBUSB_ERROR_INVALID_PARAM:
1553 return ("LIBUSB_ERROR_INVALID_PARAM");
1554 case LIBUSB_ERROR_ACCESS:
1555 return ("LIBUSB_ERROR_ACCESS");
1556 case LIBUSB_ERROR_NO_DEVICE:
1557 return ("LIBUSB_ERROR_NO_DEVICE");
1558 case LIBUSB_ERROR_NOT_FOUND:
1559 return ("LIBUSB_ERROR_NOT_FOUND");
1560 case LIBUSB_ERROR_BUSY:
1561 return ("LIBUSB_ERROR_BUSY");
1562 case LIBUSB_ERROR_TIMEOUT:
1563 return ("LIBUSB_ERROR_TIMEOUT");
1564 case LIBUSB_ERROR_OVERFLOW:
1565 return ("LIBUSB_ERROR_OVERFLOW");
1566 case LIBUSB_ERROR_PIPE:
1567 return ("LIBUSB_ERROR_PIPE");
1568 case LIBUSB_ERROR_INTERRUPTED:
1569 return ("LIBUSB_ERROR_INTERRUPTED");
1570 case LIBUSB_ERROR_NO_MEM:
1571 return ("LIBUSB_ERROR_NO_MEM");
1572 case LIBUSB_ERROR_NOT_SUPPORTED:
1573 return ("LIBUSB_ERROR_NOT_SUPPORTED");
1574 case LIBUSB_ERROR_OTHER:
1575 return ("LIBUSB_ERROR_OTHER");
1576 default:
1577 return ("LIBUSB_ERROR_UNKNOWN");