1 /* $FreeBSD: head/lib/libusb/libusb10.c 264344 2014-04-11 14:11:55Z hselasky $ */
3 * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
29 #include LIBUSB_GLOBAL_INCLUDE_FILE
40 #include <sys/fcntl.h>
41 #include <sys/ioctl.h>
42 #include <sys/queue.h>
43 #include <sys/endian.h>
46 #define libusb_device_handle libusb20_device
49 #include "libusb20_desc.h"
50 #include "libusb20_int.h"
54 static pthread_mutex_t default_context_lock
= PTHREAD_MUTEX_INITIALIZER
;
55 struct libusb_context
*usbi_default_context
= NULL
;
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 */
71 libusb_set_debug(libusb_context
*ctx
, int level
)
73 ctx
= GET_CONTEXT(ctx
);
79 libusb_set_nonblocking(int f
)
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
90 flags
= fcntl(f
, F_GETFL
, NULL
);
94 fcntl(f
, F_SETFL
, flags
);
98 libusb_init(libusb_context
**context
)
100 struct libusb_context
*ctx
;
101 pthread_condattr_t attr
;
105 ctx
= malloc(sizeof(*ctx
));
107 return (LIBUSB_ERROR_INVALID_PARAM
);
109 memset(ctx
, 0, sizeof(*ctx
));
111 debug
= getenv("LIBUSB_DEBUG");
113 ctx
->debug
= atoi(debug
);
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) {
124 return (LIBUSB_ERROR_NO_MEM
);
126 if (pthread_mutex_init(&ctx
->hotplug_lock
, NULL
) != 0) {
127 pthread_mutex_destroy(&ctx
->ctx_lock
);
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
);
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
);
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
);
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
);
158 pthread_mutex_destroy(&ctx
->ctx_lock
);
159 pthread_mutex_destroy(&ctx
->hotplug_lock
);
160 pthread_cond_destroy(&ctx
->ctx_cond
);
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
);
179 DPRINTF(ctx
, LIBUSB_DEBUG_FUNCTION
, "libusb_init complete");
185 libusb_exit(libusb_context
*ctx
)
187 ctx
= GET_CONTEXT(ctx
);
192 /* stop hotplug thread, if any */
194 if (ctx
->hotplug_handler
!= NO_THREAD
) {
199 td
= ctx
->hotplug_handler
;
200 ctx
->hotplug_handler
= NO_THREAD
;
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
);
224 /* Device handling and initialisation. */
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
;
234 ctx
= GET_CONTEXT(ctx
);
237 return (LIBUSB_ERROR_INVALID_PARAM
);
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 */
249 while ((pdev
= libusb20_be_device_foreach(usb_backend
, pdev
)))
252 /* allocate device pointer list */
253 *list
= malloc((i
+ 1) * sizeof(void *));
255 libusb20_be_free(usb_backend
);
256 return (LIBUSB_ERROR_NO_MEM
);
258 /* create libusb v1.0 compliant devices */
260 while ((pdev
= libusb20_be_device_foreach(usb_backend
, NULL
))) {
262 dev
= malloc(sizeof(*dev
));
265 libusb_unref_device((*list
)[i
- 1]);
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 */
284 /* link together the two structures */
286 pdev
->privLuData
= dev
;
288 (*list
)[i
] = libusb_ref_device(dev
);
293 libusb20_be_free(usb_backend
);
298 libusb_free_device_list(libusb_device
**list
, int unref_devices
)
303 return; /* be NULL safe */
306 for (i
= 0; list
[i
] != NULL
; i
++)
307 libusb_unref_device(list
[i
]);
313 libusb_get_bus_number(libusb_device
*dev
)
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
,
330 return (libusb20_dev_get_port_path(dev
->os_priv
, buf
, bufsize
));
334 libusb_get_device_address(libusb_device
*dev
)
337 return (0); /* should not happen */
338 return (libusb20_dev_get_address(dev
->os_priv
));
342 libusb_get_device_speed(libusb_device
*dev
)
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
);
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
;
375 return (LIBUSB_ERROR_NO_DEVICE
);
377 ret
= libusb_get_active_config_descriptor(dev
, &pdconf
);
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
;
397 libusb_free_config_descriptor(pdconf
);
402 libusb_get_max_iso_packet_size(libusb_device
*dev
, uint8_t endpoint
)
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
:
415 multiplier
= (1 + ((ret
>> 11) & 3));
418 ret
= (ret
& 0x7FF) * multiplier
;
426 libusb_ref_device(libusb_device
*dev
)
429 return (NULL
); /* be NULL safe */
433 CTX_UNLOCK(dev
->ctx
);
439 libusb_unref_device(libusb_device
*dev
)
442 return; /* be NULL safe */
446 CTX_UNLOCK(dev
->ctx
);
448 if (dev
->refcnt
== 0) {
449 libusb20_dev_free(dev
->os_priv
);
455 libusb_open(libusb_device
*dev
, libusb_device_handle
**devh
)
457 libusb_context
*ctx
= dev
->ctx
;
458 struct libusb20_device
*pdev
= dev
->os_priv
;
463 return (LIBUSB_ERROR_INVALID_PARAM
);
465 /* set default device handle value */
468 dev
= libusb_ref_device(dev
);
470 return (LIBUSB_ERROR_INVALID_PARAM
);
472 err
= libusb20_dev_open(pdev
, 16 * 4 /* number of endpoints */ );
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 */
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!");
492 libusb_device_handle
*
493 libusb_open_device_with_vid_pid(libusb_context
*ctx
, uint16_t vendor_id
,
496 struct libusb_device
**devs
;
497 struct libusb20_device
*pdev
;
498 struct LIBUSB20_DEVICE_DESC_DECODED
*pdesc
;
502 ctx
= GET_CONTEXT(ctx
);
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)
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
522 if (pdesc
->idVendor
== vendor_id
&&
523 pdesc
->idProduct
== product_id
) {
524 libusb_open(devs
[j
], &pdev
);
529 libusb_free_device_list(devs
, 1);
530 DPRINTF(ctx
, LIBUSB_DEBUG_FUNCTION
, "libusb_open_device_width_vid_pid leave");
535 libusb_close(struct libusb20_device
*pdev
)
538 struct libusb_device
*dev
;
543 return; /* be NULL safe */
545 dev
= libusb_get_device(pdev
);
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 */
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!");
565 libusb_get_device(struct libusb20_device
*pdev
)
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
));
582 return (LIBUSB_ERROR_NO_MEM
);
584 *config
= pconf
->desc
.bConfigurationValue
;
592 libusb_set_configuration(struct libusb20_device
*pdev
, int configuration
)
594 struct libusb20_config
*pconf
;
595 struct libusb_device
*dev
;
599 dev
= libusb_get_device(pdev
);
601 return (LIBUSB_ERROR_INVALID_PARAM
);
603 if (configuration
< 1) {
607 for (i
= 0; i
!= 255; i
++) {
610 pconf
= libusb20_dev_alloc_config(pdev
, i
);
612 return (LIBUSB_ERROR_INVALID_PARAM
);
613 found
= (pconf
->desc
.bConfigurationValue
620 return (LIBUSB_ERROR_INVALID_PARAM
);
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
)
642 dev
= libusb_get_device(pdev
);
644 return (LIBUSB_ERROR_INVALID_PARAM
);
646 if (interface_number
< 0 || interface_number
> 31)
647 return (LIBUSB_ERROR_INVALID_PARAM
);
650 dev
->claimed_interfaces
|= (1 << interface_number
);
651 CTX_UNLOCK(dev
->ctx
);
657 libusb_release_interface(struct libusb20_device
*pdev
, int interface_number
)
662 dev
= libusb_get_device(pdev
);
664 return (LIBUSB_ERROR_INVALID_PARAM
);
666 if (interface_number
< 0 || interface_number
> 31)
667 return (LIBUSB_ERROR_INVALID_PARAM
);
670 if (!(dev
->claimed_interfaces
& (1 << interface_number
)))
671 err
= LIBUSB_ERROR_NOT_FOUND
;
674 dev
->claimed_interfaces
&= ~(1 << interface_number
);
675 CTX_UNLOCK(dev
->ctx
);
680 libusb_set_interface_alt_setting(struct libusb20_device
*pdev
,
681 int interface_number
, int alternate_setting
)
686 dev
= libusb_get_device(pdev
);
688 return (LIBUSB_ERROR_INVALID_PARAM
);
690 if (interface_number
< 0 || interface_number
> 31)
691 return (LIBUSB_ERROR_INVALID_PARAM
);
694 if (!(dev
->claimed_interfaces
& (1 << interface_number
)))
695 err
= LIBUSB_ERROR_NOT_FOUND
;
696 CTX_UNLOCK(dev
->ctx
);
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 */
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
;
737 xfer
= libusb10_get_transfer(pdev
, endpoint
, 0);
739 return (LIBUSB_ERROR_INVALID_PARAM
);
741 dev
= libusb_get_device(pdev
);
743 return (LIBUSB_ERROR_INVALID_PARAM
);
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 */
757 libusb20_tr_close(xfer
);
758 CTX_UNLOCK(dev
->ctx
);
760 return (0); /* success */
764 libusb_reset_device(struct libusb20_device
*pdev
)
769 dev
= libusb_get_device(pdev
);
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
)
792 dev
= libusb_get_device(pdev
);
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
)
805 return (LIBUSB_ERROR_INVALID_PARAM
);
807 if (libusb20_dev_kernel_driver_active(pdev
, interface
))
808 return (0); /* no kernel driver is active */
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
)
828 return (LIBUSB_ERROR_INVALID_PARAM
);
830 return (LIBUSB_ERROR_INVALID_PARAM
);
834 err
= libusb20_dev_get_iface_desc(
835 pdev
, interface
, name
, namelen
);
838 return (LIBUSB_ERROR_OTHER
);
840 /* we only want the driver name */
841 ptr
= strstr(name
, ":");
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
)
860 return (LIBUSB_ERROR_INVALID_PARAM
);
862 err
= libusb20_dev_detach_kernel_driver(
865 return (err
? LIBUSB_ERROR_OTHER
: 0);
869 libusb_attach_kernel_driver(struct libusb20_device
*pdev
, int interface
)
872 return (LIBUSB_ERROR_INVALID_PARAM
);
873 /* stub - currently not supported by libusb20 */
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
;
886 len
= sizeof(struct libusb_transfer
) +
887 sizeof(struct libusb_super_transfer
) +
888 (iso_packets
* sizeof(libusb_iso_packet_descriptor
));
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
;
906 libusb_free_transfer(struct libusb_transfer
*uxfer
)
908 struct libusb_super_transfer
*sxfer
;
911 return; /* be NULL safe */
913 /* check if we should free the transfer buffer */
914 if (uxfer
->flags
& LIBUSB_TRANSFER_FREE_BUFFER
)
917 sxfer
= (struct libusb_super_transfer
*)(
918 (uint8_t *)uxfer
- sizeof(*sxfer
));
924 libusb10_get_maxframe(struct libusb20_device
*pdev
, libusb_transfer
*xfer
)
928 switch (xfer
->type
) {
929 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
:
930 ret
= 60 | LIBUSB20_MAX_FRAME_PRE_SCALE
; /* 60ms */
932 case LIBUSB_TRANSFER_TYPE_CONTROL
:
943 libusb10_get_buffsize(struct libusb20_device
*pdev
, libusb_transfer
*xfer
)
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 */
954 case LIBUSB_TRANSFER_TYPE_CONTROL
:
959 case LIBUSB20_SPEED_LOW
:
962 case LIBUSB20_SPEED_FULL
:
965 case LIBUSB20_SPEED_SUPER
:
978 libusb10_convert_error(uint8_t 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
);
997 return (LIBUSB_TRANSFER_ERROR
);
1001 /* This function must be called locked */
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
));
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 */
1030 libusb10_isoc_proxy(struct libusb20_transfer
*pxfer
)
1032 struct libusb_super_transfer
*sxfer
;
1033 struct libusb_transfer
*uxfer
;
1035 uint16_t iso_packets
;
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
);
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
;
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
);
1074 case LIBUSB20_TRANSFER_START
:
1076 /* setup length(s) */
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
;
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
);
1096 libusb10_complete_transfer(pxfer
, sxfer
, libusb10_convert_error(status
));
1101 /* This function must be called locked */
1104 libusb10_bulk_intr_proxy(struct libusb20_transfer
*pxfer
)
1106 struct libusb_super_transfer
*sxfer
;
1107 struct libusb_transfer
*uxfer
;
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
);
1119 return; /* cancelled - nothing to do */
1121 uxfer
= (struct libusb_transfer
*)(
1122 ((uint8_t *)sxfer
) + sizeof(*sxfer
));
1124 flags
= uxfer
->flags
;
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
);
1136 libusb10_complete_transfer(pxfer
, sxfer
, LIBUSB_TRANSFER_COMPLETED
);
1140 /* check for end of data */
1141 if (sxfer
->rem_len
== 0) {
1142 libusb10_complete_transfer(pxfer
, sxfer
, LIBUSB_TRANSFER_COMPLETED
);
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
);
1168 libusb10_complete_transfer(pxfer
, sxfer
, libusb10_convert_error(status
));
1173 /* This function must be called locked */
1176 libusb10_ctrl_proxy(struct libusb20_transfer
*pxfer
)
1178 struct libusb_super_transfer
*sxfer
;
1179 struct libusb_transfer
*uxfer
;
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
);
1191 return; /* cancelled - nothing to do */
1193 uxfer
= (struct libusb_transfer
*)(
1194 ((uint8_t *)sxfer
) + sizeof(*sxfer
));
1196 flags
= uxfer
->flags
;
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
);
1211 libusb10_complete_transfer(pxfer
, sxfer
, LIBUSB_TRANSFER_COMPLETED
);
1215 /* check for end of data */
1216 if (sxfer
->rem_len
== 0) {
1217 libusb10_complete_transfer(pxfer
, sxfer
, LIBUSB_TRANSFER_COMPLETED
);
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);
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);
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
);
1257 libusb10_complete_transfer(pxfer
, sxfer
, libusb10_convert_error(status
));
1262 /* The following function must be called locked */
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
;
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 */
1287 if (libusb20_tr_pending(pxfer0
))
1289 if (libusb20_tr_pending(pxfer1
))
1294 /* wait till one of the transfers complete */
1297 sxfer
= libusb20_tr_get_priv_sc1(pxfer1
);
1299 return; /* cancelling */
1301 return; /* cannot queue another one */
1302 /* swap transfers */
1306 sxfer
= libusb20_tr_get_priv_sc1(pxfer0
);
1308 return; /* cancelling */
1310 return; /* cannot queue another one */
1311 /* swap transfers */
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
;
1330 return; /* success */
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
);
1346 case LIBUSB_TRANSFER_TYPE_BULK
:
1347 case LIBUSB_TRANSFER_TYPE_INTERRUPT
:
1348 libusb20_tr_set_callback(pxfer0
, libusb10_bulk_intr_proxy
);
1350 case LIBUSB_TRANSFER_TYPE_CONTROL
:
1351 libusb20_tr_set_callback(pxfer0
, libusb10_ctrl_proxy
);
1352 if (sxfer
->rem_len
< 8)
1355 /* remove SETUP packet from data */
1356 sxfer
->rem_len
-= 8;
1357 sxfer
->curr_data
+= 8;
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
)) {
1371 libusb20_tr_start(pxfer0
);
1375 libusb10_complete_transfer(pxfer0
, sxfer
, LIBUSB_TRANSFER_ERROR
);
1377 /* make sure our event loop spins the done handler */
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
;
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
));
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
;
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
);
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
;
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
));
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
) {
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
);
1506 retval
= LIBUSB_ERROR_NOT_FOUND
;
1509 CTX_UNLOCK(dev
->ctx
);
1511 DPRINTF(dev
->ctx
, LIBUSB_DEBUG_FUNCTION
, "libusb_cancel_transfer leave");
1517 libusb10_cancel_all_transfer(libusb_device
*dev
)
1523 libusb_cpu_to_le16(uint16_t x
)
1525 return (htole16(x
));
1529 libusb_le16_to_cpu(uint16_t x
)
1531 return (le16toh(x
));
1535 libusb_strerror(int code
)
1538 case LIBUSB_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
:
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");
1567 return ("Unknown error");
1572 libusb_error_name(int 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");
1604 return ("LIBUSB_ERROR_UNKNOWN");