Add libusb

This commit is contained in:
Skia 2016-10-14 15:07:59 +02:00
parent 1e8c61fb98
commit 4970eb0ff5
18 changed files with 10851 additions and 0 deletions

4
.gitignore vendored
View file

@ -1,3 +1,7 @@
# Custom
*.tar.bz2
# ---> Android
# Built application files
*.apk

40
libusbx-1.0.9/AUTHORS Normal file
View file

@ -0,0 +1,40 @@
Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
Copyright © 2007-2009 Daniel Drake <dsd@gentoo.org>
Copyright © 2008-2011 Nathan Hjelm <hjelmn@users.sourceforge.net>
Copyright © 2009-2012 Pete Batard <pete@akeo.ie>
Copyright © 2010-2012 Michael Plante <michael.plante@gmail.com>
Copyright © 2011-2012 Hans de Goede <hdegoede@redhat.com>
Copyright © 2012 Martin Pieuchot <mpi@openbsd.org>
Other contributors:
Alan Ott
Alan Stern
Alex Vatchenko
Artem Egorkine
Aurelien Jarno
Bastien Nocera
Dave Camarillo
David Engraf
David Moore
Felipe Balbi
Francesco Montorsi
Graeme Gill
Hans Ulrich Niedermann
Hector Martin
Hoi-Ho Chan
Konrad Rzepecki
Ludovic Rousseau
Martin Koegler
Mike Frysinger
Mikhail Gusarov
Pekka Nikander
Peter Stuge
Rob Walker
Sean McBride
Sebastian Pipping
Thomas Röfer
Toby Peterson
Trygve Laugstøl
Vasily Khoruzhick
Vitali Lovich
Xiaofan Chen

28
libusbx-1.0.9/Android.mk Normal file
View file

@ -0,0 +1,28 @@
# Android.mk for libusb
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
# Sources
LOCAL_SRC_FILES:= \
libusb/core.c \
libusb/descriptor.c \
libusb/io.c \
libusb/sync.c \
libusb/os/linux_usbfs.c \
libusb/os/threads_posix.c
# Flags
LOCAL_CFLAGS += $(OPENNI2_CFLAGS) -DOPENNI2_EXPORT -fvisibility=default
# Headers
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/libusb/ \
$(LOCAL_PATH)/libusb/os/
LOCAL_LDLIBS += -llog
LOCAL_MODULE := libusb
include $(BUILD_SHARED_LIBRARY)

504
libusbx-1.0.9/COPYING Normal file
View file

@ -0,0 +1,504 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

5
libusbx-1.0.9/ChangeLog Normal file
View file

@ -0,0 +1,5 @@
For the latest change log, please visit:
http://libusbx.git.sourceforge.net/git/gitweb.cgi?p=libusbx/libusbx;a=log
Alternatively, if you have retrieved the libusbx git repository, you can simply
navigate to it and issue the command 'git log'.

23
libusbx-1.0.9/README Normal file
View file

@ -0,0 +1,23 @@
libusb
======
libusb is a library for USB device access from Linux, Mac OS X and
Windows userspace.
It is written in C and licensed under the LGPL-2.1 (see COPYING).
libusb is abstracted internally in such a way that it can hopefully
be ported to other operating systems. See the PORTING file for some
information, if you fancy a challenge :)
libusb homepage:
http://libusb.org/
Developers will wish to consult the API documentation:
http://libusb.sourceforge.net/api-1.0/
Use the mailing list for questions, comments, etc:
https://sourceforge.net/mailarchive/forum.php?forum_name=libusb-devel
- Daniel Drake <dsd@gentoo.org>
- Peter Stuge <peter@stuge.se>
(use the mailing list rather than mailing developers directly)

7
libusbx-1.0.9/THANKS Normal file
View file

@ -0,0 +1,7 @@
Development contributors are listed in the AUTHORS file. Community members who
have also made significant contributions in other areas are listed below:
Alan Stern
Ludovic Rousseau
Tim Roberts
Xiaofan Chen

1789
libusbx-1.0.9/libusb/core.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,730 @@
/*
* USB descriptor handling functions for libusb
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
* Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libusbi.h"
#define DESC_HEADER_LENGTH 2
#define DEVICE_DESC_LENGTH 18
#define CONFIG_DESC_LENGTH 9
#define INTERFACE_DESC_LENGTH 9
#define ENDPOINT_DESC_LENGTH 7
#define ENDPOINT_AUDIO_DESC_LENGTH 9
/** @defgroup desc USB descriptors
* This page details how to examine the various standard USB descriptors
* for detected devices
*/
/* set host_endian if the w values are already in host endian format,
* as opposed to bus endian. */
int usbi_parse_descriptor(unsigned char *source, const char *descriptor,
void *dest, int host_endian)
{
unsigned char *sp = source, *dp = dest;
uint16_t w;
const char *cp;
for (cp = descriptor; *cp; cp++) {
switch (*cp) {
case 'b': /* 8-bit byte */
*dp++ = *sp++;
break;
case 'w': /* 16-bit word, convert from little endian to CPU */
dp += ((uintptr_t)dp & 1); /* Align to word boundary */
if (host_endian) {
memcpy(dp, sp, 2);
} else {
w = (sp[1] << 8) | sp[0];
*((uint16_t *)dp) = w;
}
sp += 2;
dp += 2;
break;
}
}
return (int) (sp - source);
}
static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
{
if (endpoint->extra)
free((unsigned char *) endpoint->extra);
}
static int parse_endpoint(struct libusb_context *ctx,
struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
int size, int host_endian)
{
struct usb_descriptor_header header;
unsigned char *extra;
unsigned char *begin;
int parsed = 0;
int len;
usbi_parse_descriptor(buffer, "bb", &header, 0);
/* Everything should be fine being passed into here, but we sanity */
/* check JIC */
if (header.bLength > size) {
usbi_err(ctx, "ran out of descriptors parsing");
return -1;
}
if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
usbi_err(ctx, "unexpected descriptor %x (expected %x)",
header.bDescriptorType, LIBUSB_DT_ENDPOINT);
return parsed;
}
if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
else if (header.bLength >= ENDPOINT_DESC_LENGTH)
usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
buffer += header.bLength;
size -= header.bLength;
parsed += header.bLength;
/* Skip over the rest of the Class Specific or Vendor Specific */
/* descriptors */
begin = buffer;
while (size >= DESC_HEADER_LENGTH) {
usbi_parse_descriptor(buffer, "bb", &header, 0);
if (header.bLength < 2) {
usbi_err(ctx, "invalid descriptor length %d", header.bLength);
return -1;
}
/* If we find another "proper" descriptor then we're done */
if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
(header.bDescriptorType == LIBUSB_DT_DEVICE))
break;
usbi_dbg("skipping descriptor %x", header.bDescriptorType);
buffer += header.bLength;
size -= header.bLength;
parsed += header.bLength;
}
/* Copy any unknown descriptors into a storage area for drivers */
/* to later parse */
len = (int)(buffer - begin);
if (!len) {
endpoint->extra = NULL;
endpoint->extra_length = 0;
return parsed;
}
extra = malloc(len);
endpoint->extra = extra;
if (!extra) {
endpoint->extra_length = 0;
return LIBUSB_ERROR_NO_MEM;
}
memcpy(extra, begin, len);
endpoint->extra_length = len;
return parsed;
}
static void clear_interface(struct libusb_interface *usb_interface)
{
int i;
int j;
if (usb_interface->altsetting) {
for (i = 0; i < usb_interface->num_altsetting; i++) {
struct libusb_interface_descriptor *ifp =
(struct libusb_interface_descriptor *)
usb_interface->altsetting + i;
if (ifp->extra)
free((void *) ifp->extra);
if (ifp->endpoint) {
for (j = 0; j < ifp->bNumEndpoints; j++)
clear_endpoint((struct libusb_endpoint_descriptor *)
ifp->endpoint + j);
free((void *) ifp->endpoint);
}
}
free((void *) usb_interface->altsetting);
usb_interface->altsetting = NULL;
}
}
static int parse_interface(libusb_context *ctx,
struct libusb_interface *usb_interface, unsigned char *buffer, int size,
int host_endian)
{
int i;
int len;
int r;
int parsed = 0;
size_t tmp;
struct usb_descriptor_header header;
struct libusb_interface_descriptor *ifp;
unsigned char *begin;
usb_interface->num_altsetting = 0;
while (size >= INTERFACE_DESC_LENGTH) {
struct libusb_interface_descriptor *altsetting =
(struct libusb_interface_descriptor *) usb_interface->altsetting;
altsetting = realloc(altsetting,
sizeof(struct libusb_interface_descriptor) *
(usb_interface->num_altsetting + 1));
if (!altsetting) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
usb_interface->altsetting = altsetting;
ifp = altsetting + usb_interface->num_altsetting;
usb_interface->num_altsetting++;
usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
ifp->extra = NULL;
ifp->extra_length = 0;
ifp->endpoint = NULL;
/* Skip over the interface */
buffer += ifp->bLength;
parsed += ifp->bLength;
size -= ifp->bLength;
begin = buffer;
/* Skip over any interface, class or vendor descriptors */
while (size >= DESC_HEADER_LENGTH) {
usbi_parse_descriptor(buffer, "bb", &header, 0);
if (header.bLength < 2) {
usbi_err(ctx, "invalid descriptor of length %d",
header.bLength);
r = LIBUSB_ERROR_IO;
goto err;
}
/* If we find another "proper" descriptor then we're done */
if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
(header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
(header.bDescriptorType == LIBUSB_DT_DEVICE))
break;
buffer += header.bLength;
parsed += header.bLength;
size -= header.bLength;
}
/* Copy any unknown descriptors into a storage area for */
/* drivers to later parse */
len = (int)(buffer - begin);
if (len) {
ifp->extra = malloc(len);
if (!ifp->extra) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
memcpy((unsigned char *) ifp->extra, begin, len);
ifp->extra_length = len;
}
/* Did we hit an unexpected descriptor? */
if (size >= DESC_HEADER_LENGTH) {
usbi_parse_descriptor(buffer, "bb", &header, 0);
if ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
(header.bDescriptorType == LIBUSB_DT_DEVICE)) {
return parsed;
}
}
if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
r = LIBUSB_ERROR_IO;
goto err;
}
if (ifp->bNumEndpoints > 0) {
struct libusb_endpoint_descriptor *endpoint;
tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
endpoint = malloc(tmp);
ifp->endpoint = endpoint;
if (!endpoint) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
memset(endpoint, 0, tmp);
for (i = 0; i < ifp->bNumEndpoints; i++) {
usbi_parse_descriptor(buffer, "bb", &header, 0);
if (header.bLength > size) {
usbi_err(ctx, "ran out of descriptors parsing");
r = LIBUSB_ERROR_IO;
goto err;
}
r = parse_endpoint(ctx, endpoint + i, buffer, size,
host_endian);
if (r < 0)
goto err;
buffer += r;
parsed += r;
size -= r;
}
}
/* We check to see if it's an alternate to this one */
ifp = (struct libusb_interface_descriptor *) buffer;
if (size < LIBUSB_DT_INTERFACE_SIZE ||
ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
!ifp->bAlternateSetting)
return parsed;
}
return parsed;
err:
clear_interface(usb_interface);
return r;
}
static void clear_configuration(struct libusb_config_descriptor *config)
{
if (config->interface) {
int i;
for (i = 0; i < config->bNumInterfaces; i++)
clear_interface((struct libusb_interface *)
config->interface + i);
free((void *) config->interface);
}
if (config->extra)
free((void *) config->extra);
}
static int parse_configuration(struct libusb_context *ctx,
struct libusb_config_descriptor *config, unsigned char *buffer,
int host_endian)
{
int i;
int r;
int size;
size_t tmp;
struct usb_descriptor_header header;
struct libusb_interface *usb_interface;
usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
size = config->wTotalLength;
if (config->bNumInterfaces > USB_MAXINTERFACES) {
usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
return LIBUSB_ERROR_IO;
}
tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
usb_interface = malloc(tmp);
config->interface = usb_interface;
if (!config->interface)
return LIBUSB_ERROR_NO_MEM;
memset(usb_interface, 0, tmp);
buffer += config->bLength;
size -= config->bLength;
config->extra = NULL;
config->extra_length = 0;
for (i = 0; i < config->bNumInterfaces; i++) {
int len;
unsigned char *begin;
/* Skip over the rest of the Class Specific or Vendor */
/* Specific descriptors */
begin = buffer;
while (size >= DESC_HEADER_LENGTH) {
usbi_parse_descriptor(buffer, "bb", &header, 0);
if ((header.bLength > size) ||
(header.bLength < DESC_HEADER_LENGTH)) {
usbi_err(ctx, "invalid descriptor length of %d",
header.bLength);
r = LIBUSB_ERROR_IO;
goto err;
}
/* If we find another "proper" descriptor then we're done */
if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
(header.bDescriptorType == LIBUSB_DT_DEVICE))
break;
usbi_dbg("skipping descriptor 0x%x\n", header.bDescriptorType);
buffer += header.bLength;
size -= header.bLength;
}
/* Copy any unknown descriptors into a storage area for */
/* drivers to later parse */
len = (int)(buffer - begin);
if (len) {
/* FIXME: We should realloc and append here */
if (!config->extra_length) {
config->extra = malloc(len);
if (!config->extra) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
memcpy((unsigned char *) config->extra, begin, len);
config->extra_length = len;
}
}
r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
if (r < 0)
goto err;
buffer += r;
size -= r;
}
return size;
err:
clear_configuration(config);
return r;
}
/** \ingroup desc
* Get the USB device descriptor for a given device.
*
* This is a non-blocking function; the device descriptor is cached in memory.
*
* \param dev the device
* \param desc output location for the descriptor data
* \returns 0 on success or a LIBUSB_ERROR code on failure
*/
int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
struct libusb_device_descriptor *desc)
{
unsigned char raw_desc[DEVICE_DESC_LENGTH];
int host_endian = 0;
int r;
usbi_dbg("");
r = usbi_backend->get_device_descriptor(dev, raw_desc, &host_endian);
if (r < 0)
return r;
memcpy((unsigned char *) desc, raw_desc, sizeof(raw_desc));
if (!host_endian) {
desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
}
return 0;
}
/** \ingroup desc
* Get the USB configuration descriptor for the currently active configuration.
* This is a non-blocking function which does not involve any requests being
* sent to the device.
*
* \param dev a device
* \param config output location for the USB configuration descriptor. Only
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
* after use.
* \returns 0 on success
* \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
* \returns another LIBUSB_ERROR code on error
* \see libusb_get_config_descriptor
*/
int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
struct libusb_config_descriptor **config)
{
struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
unsigned char tmp[8];
unsigned char *buf = NULL;
int host_endian = 0;
int r;
usbi_dbg("");
if (!_config)
return LIBUSB_ERROR_NO_MEM;
r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp),
&host_endian);
if (r < 0)
goto err;
usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
buf = malloc(_config->wTotalLength);
if (!buf) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
r = usbi_backend->get_active_config_descriptor(dev, buf,
_config->wTotalLength, &host_endian);
if (r < 0)
goto err;
r = parse_configuration(dev->ctx, _config, buf, host_endian);
if (r < 0) {
usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
goto err;
} else if (r > 0) {
usbi_warn(dev->ctx, "descriptor data still left");
}
free(buf);
*config = _config;
return 0;
err:
free(_config);
if (buf)
free(buf);
return r;
}
/** \ingroup desc
* Get a USB configuration descriptor based on its index.
* This is a non-blocking function which does not involve any requests being
* sent to the device.
*
* \param dev a device
* \param config_index the index of the configuration you wish to retrieve
* \param config output location for the USB configuration descriptor. Only
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
* after use.
* \returns 0 on success
* \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
* \returns another LIBUSB_ERROR code on error
* \see libusb_get_active_config_descriptor()
* \see libusb_get_config_descriptor_by_value()
*/
int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
uint8_t config_index, struct libusb_config_descriptor **config)
{
struct libusb_config_descriptor *_config;
unsigned char tmp[8];
unsigned char *buf = NULL;
int host_endian = 0;
int r;
usbi_dbg("index %d", config_index);
if (config_index >= dev->num_configurations)
return LIBUSB_ERROR_NOT_FOUND;
_config = malloc(sizeof(*_config));
if (!_config)
return LIBUSB_ERROR_NO_MEM;
r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
sizeof(tmp), &host_endian);
if (r < 0)
goto err;
usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
buf = malloc(_config->wTotalLength);
if (!buf) {
r = LIBUSB_ERROR_NO_MEM;
goto err;
}
host_endian = 0;
r = usbi_backend->get_config_descriptor(dev, config_index, buf,
_config->wTotalLength, &host_endian);
if (r < 0)
goto err;
r = parse_configuration(dev->ctx, _config, buf, host_endian);
if (r < 0) {
usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
goto err;
} else if (r > 0) {
usbi_warn(dev->ctx, "descriptor data still left");
}
free(buf);
*config = _config;
return 0;
err:
free(_config);
if (buf)
free(buf);
return r;
}
/* iterate through all configurations, returning the index of the configuration
* matching a specific bConfigurationValue in the idx output parameter, or -1
* if the config was not found.
* returns 0 or a LIBUSB_ERROR code
*/
int usbi_get_config_index_by_value(struct libusb_device *dev,
uint8_t bConfigurationValue, int *idx)
{
uint8_t i;
usbi_dbg("value %d", bConfigurationValue);
for (i = 0; i < dev->num_configurations; i++) {
unsigned char tmp[6];
int host_endian;
int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
&host_endian);
if (r < 0)
return r;
if (tmp[5] == bConfigurationValue) {
*idx = i;
return 0;
}
}
*idx = -1;
return 0;
}
/** \ingroup desc
* Get a USB configuration descriptor with a specific bConfigurationValue.
* This is a non-blocking function which does not involve any requests being
* sent to the device.
*
* \param dev a device
* \param bConfigurationValue the bConfigurationValue of the configuration you
* wish to retrieve
* \param config output location for the USB configuration descriptor. Only
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
* after use.
* \returns 0 on success
* \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
* \returns another LIBUSB_ERROR code on error
* \see libusb_get_active_config_descriptor()
* \see libusb_get_config_descriptor()
*/
int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
{
int idx;
int r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
if (r < 0)
return r;
else if (idx == -1)
return LIBUSB_ERROR_NOT_FOUND;
else
return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
}
/** \ingroup desc
* Free a configuration descriptor obtained from
* libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
* It is safe to call this function with a NULL config parameter, in which
* case the function simply returns.
*
* \param config the configuration descriptor to free
*/
void API_EXPORTED libusb_free_config_descriptor(
struct libusb_config_descriptor *config)
{
if (!config)
return;
clear_configuration(config);
free(config);
}
/** \ingroup desc
* Retrieve a string descriptor in C style ASCII.
*
* Wrapper around libusb_get_string_descriptor(). Uses the first language
* supported by the device.
*
* \param dev a device handle
* \param desc_index the index of the descriptor to retrieve
* \param data output buffer for ASCII string descriptor
* \param length size of data buffer
* \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
*/
int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
uint8_t desc_index, unsigned char *data, int length)
{
unsigned char tbuf[255]; /* Some devices choke on size > 255 */
int r, si, di;
uint16_t langid;
/* Asking for the zero'th index is special - it returns a string
* descriptor that contains all the language IDs supported by the
* device. Typically there aren't many - often only one. Language
* IDs are 16 bit numbers, and they start at the third byte in the
* descriptor. There's also no point in trying to read descriptor 0
* with this function. See USB 2.0 specification section 9.6.7 for
* more information.
*/
if (desc_index == 0)
return LIBUSB_ERROR_INVALID_PARAM;
r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
if (r < 0)
return r;
if (r < 4)
return LIBUSB_ERROR_IO;
langid = tbuf[2] | (tbuf[3] << 8);
r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
sizeof(tbuf));
if (r < 0)
return r;
if (tbuf[1] != LIBUSB_DT_STRING)
return LIBUSB_ERROR_IO;
if (tbuf[0] > r)
return LIBUSB_ERROR_IO;
for (di = 0, si = 2; si < tbuf[0]; si += 2) {
if (di >= (length - 1))
break;
if (tbuf[si + 1]) /* high byte */
data[di++] = '?';
else
data[di++] = tbuf[si];
}
data[di] = 0;
return di;
}

2453
libusbx-1.0.9/libusb/io.c Normal file

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,896 @@
/*
* Internal header for libusb
* Copyright (C) 2007-2009 Daniel Drake <dsd@gentoo.org>
* Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBUSBI_H
#define LIBUSBI_H
#include <config.h>
#include <stddef.h>
#include <stdint.h>
#include <time.h>
#include <stdarg.h>
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#include <libusb.h>
/* Inside the libusb code, mark all public functions as follows:
* return_type API_EXPORTED function_name(params) { ... }
* But if the function returns a pointer, mark it as follows:
* DEFAULT_VISIBILITY return_type * LIBUSB_CALL function_name(params) { ... }
* In the libusb public header, mark all declarations as:
* return_type LIBUSB_CALL function_name(params);
*/
#define API_EXPORTED LIBUSB_CALL DEFAULT_VISIBILITY
#define DEVICE_DESC_LENGTH 18
#define USB_MAXENDPOINTS 32
#define USB_MAXINTERFACES 32
#define USB_MAXCONFIG 8
struct list_head {
struct list_head *prev, *next;
};
/* Get an entry from the list
* ptr - the address of this list_head element in "type"
* type - the data type that contains "member"
* member - the list_head element in "type"
*/
#define list_entry(ptr, type, member) \
((type *)((uintptr_t)(ptr) - (uintptr_t)(&((type *)0L)->member)))
/* Get each entry from a list
* pos - A structure pointer has a "member" element
* head - list head
* member - the list_head element in "pos"
* type - the type of the first parameter
*/
#define list_for_each_entry(pos, head, member, type) \
for (pos = list_entry((head)->next, type, member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, type, member))
#define list_for_each_entry_safe(pos, n, head, member, type) \
for (pos = list_entry((head)->next, type, member), \
n = list_entry(pos->member.next, type, member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, type, member))
#define list_empty(entry) ((entry)->next == (entry))
static inline void list_init(struct list_head *entry)
{
entry->prev = entry->next = entry;
}
static inline void list_add(struct list_head *entry, struct list_head *head)
{
entry->next = head->next;
entry->prev = head;
head->next->prev = entry;
head->next = entry;
}
static inline void list_add_tail(struct list_head *entry,
struct list_head *head)
{
entry->next = head;
entry->prev = head->prev;
head->prev->next = entry;
head->prev = entry;
}
static inline void list_del(struct list_head *entry)
{
entry->next->prev = entry->prev;
entry->prev->next = entry->next;
}
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *mptr = (ptr); \
(type *)( (char *)mptr - offsetof(type,member) );})
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define TIMESPEC_IS_SET(ts) ((ts)->tv_sec != 0 || (ts)->tv_nsec != 0)
enum usbi_log_level {
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
};
void usbi_log(struct libusb_context *ctx, enum usbi_log_level level,
const char *function, const char *format, ...);
void usbi_log_v(struct libusb_context *ctx, enum usbi_log_level level,
const char *function, const char *format, va_list args);
#if !defined(_MSC_VER) || _MSC_VER > 1200
#ifdef ENABLE_LOGGING
#define _usbi_log(ctx, level, ...) usbi_log(ctx, level, __FUNCTION__, __VA_ARGS__)
#else
#define _usbi_log(ctx, level, ...) do { (void)(ctx); } while(0)
#endif
#ifdef ENABLE_DEBUG_LOGGING
#define usbi_dbg(...) _usbi_log(NULL, LOG_LEVEL_DEBUG, __VA_ARGS__)
#else
#define usbi_dbg(...) do {} while(0)
#endif
#define usbi_info(ctx, ...) _usbi_log(ctx, LOG_LEVEL_INFO, __VA_ARGS__)
#define usbi_warn(ctx, ...) _usbi_log(ctx, LOG_LEVEL_WARNING, __VA_ARGS__)
#define usbi_err(ctx, ...) _usbi_log(ctx, LOG_LEVEL_ERROR, __VA_ARGS__)
#else /* !defined(_MSC_VER) || _MSC_VER > 1200 */
#ifdef ENABLE_LOGGING
#define LOG_BODY(ctxt, level) \
{ \
va_list args; \
va_start (args, format); \
usbi_log_v(ctxt, level, "", format, args); \
va_end(args); \
}
#else
#define LOG_BODY(ctxt, level) do { (void)(ctxt); } while(0)
#endif
static inline void usbi_info(struct libusb_context *ctx, const char *format,
...)
LOG_BODY(ctx,LOG_LEVEL_INFO)
static inline void usbi_warn(struct libusb_context *ctx, const char *format,
...)
LOG_BODY(ctx,LOG_LEVEL_WARNING)
static inline void usbi_err( struct libusb_context *ctx, const char *format,
...)
LOG_BODY(ctx,LOG_LEVEL_ERROR)
static inline void usbi_dbg(const char *format, ...)
#ifdef ENABLE_DEBUG_LOGGING
LOG_BODY(NULL,LOG_LEVEL_DEBUG)
#else
{ }
#endif
#endif /* !defined(_MSC_VER) || _MSC_VER > 1200 */
#define USBI_GET_CONTEXT(ctx) if (!(ctx)) (ctx) = usbi_default_context
#define DEVICE_CTX(dev) ((dev)->ctx)
#define HANDLE_CTX(handle) (DEVICE_CTX((handle)->dev))
#define TRANSFER_CTX(transfer) (HANDLE_CTX((transfer)->dev_handle))
#define ITRANSFER_CTX(transfer) \
(TRANSFER_CTX(USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer)))
#define IS_EPIN(ep) (0 != ((ep) & LIBUSB_ENDPOINT_IN))
#define IS_EPOUT(ep) (!IS_EPIN(ep))
#define IS_XFERIN(xfer) (0 != ((xfer)->endpoint & LIBUSB_ENDPOINT_IN))
#define IS_XFEROUT(xfer) (!IS_XFERIN(xfer))
/* Internal abstractions for thread synchronization and poll */
#if defined(THREADS_POSIX)
#include <os/threads_posix.h>
#elif defined(OS_WINDOWS)
#include <os/threads_windows.h>
#endif
#if defined(OS_LINUX) || defined(OS_DARWIN) || defined(OS_OPENBSD)
#include <unistd.h>
#include <os/poll_posix.h>
#elif defined(OS_WINDOWS)
#include <os/poll_windows.h>
#endif
extern struct libusb_context *usbi_default_context;
struct libusb_context {
int debug;
int debug_fixed;
/* internal control pipe, used for interrupting event handling when
* something needs to modify poll fds. */
int ctrl_pipe[2];
struct list_head usb_devs;
usbi_mutex_t usb_devs_lock;
/* A list of open handles. Backends are free to traverse this if required.
*/
struct list_head open_devs;
usbi_mutex_t open_devs_lock;
/* this is a list of in-flight transfer handles, sorted by timeout
* expiration. URBs to timeout the soonest are placed at the beginning of
* the list, URBs that will time out later are placed after, and urbs with
* infinite timeout are always placed at the very end. */
struct list_head flying_transfers;
usbi_mutex_t flying_transfers_lock;
/* list of poll fds */
struct list_head pollfds;
usbi_mutex_t pollfds_lock;
/* a counter that is set when we want to interrupt event handling, in order
* to modify the poll fd set. and a lock to protect it. */
unsigned int pollfd_modify;
usbi_mutex_t pollfd_modify_lock;
/* user callbacks for pollfd changes */
libusb_pollfd_added_cb fd_added_cb;
libusb_pollfd_removed_cb fd_removed_cb;
void *fd_cb_user_data;
/* ensures that only one thread is handling events at any one time */
usbi_mutex_t events_lock;
/* used to see if there is an active thread doing event handling */
int event_handler_active;
/* used to wait for event completion in threads other than the one that is
* event handling */
usbi_mutex_t event_waiters_lock;
usbi_cond_t event_waiters_cond;
#ifdef USBI_TIMERFD_AVAILABLE
/* used for timeout handling, if supported by OS.
* this timerfd is maintained to trigger on the next pending timeout */
int timerfd;
#endif
};
#ifdef USBI_TIMERFD_AVAILABLE
#define usbi_using_timerfd(ctx) ((ctx)->timerfd >= 0)
#else
#define usbi_using_timerfd(ctx) (0)
#endif
struct libusb_device {
/* lock protects refcnt, everything else is finalized at initialization
* time */
usbi_mutex_t lock;
int refcnt;
struct libusb_context *ctx;
uint8_t bus_number;
uint8_t device_address;
uint8_t num_configurations;
enum libusb_speed speed;
struct list_head list;
unsigned long session_data;
unsigned char os_priv[0];
};
struct libusb_device_handle {
/* lock protects claimed_interfaces */
usbi_mutex_t lock;
unsigned long claimed_interfaces;
struct list_head list;
struct libusb_device *dev;
unsigned char os_priv[0];
};
enum {
USBI_CLOCK_MONOTONIC,
USBI_CLOCK_REALTIME
};
/* in-memory transfer layout:
*
* 1. struct usbi_transfer
* 2. struct libusb_transfer (which includes iso packets) [variable size]
* 3. os private data [variable size]
*
* from a libusb_transfer, you can get the usbi_transfer by rewinding the
* appropriate number of bytes.
* the usbi_transfer includes the number of allocated packets, so you can
* determine the size of the transfer and hence the start and length of the
* OS-private data.
*/
struct usbi_transfer {
int num_iso_packets;
struct list_head list;
struct timeval timeout;
int transferred;
uint8_t flags;
/* this lock is held during libusb_submit_transfer() and
* libusb_cancel_transfer() (allowing the OS backend to prevent duplicate
* cancellation, submission-during-cancellation, etc). the OS backend
* should also take this lock in the handle_events path, to prevent the user
* cancelling the transfer from another thread while you are processing
* its completion (presumably there would be races within your OS backend
* if this were possible). */
usbi_mutex_t lock;
};
enum usbi_transfer_flags {
/* The transfer has timed out */
USBI_TRANSFER_TIMED_OUT = 1 << 0,
/* Set by backend submit_transfer() if the OS handles timeout */
USBI_TRANSFER_OS_HANDLES_TIMEOUT = 1 << 1,
/* Cancellation was requested via libusb_cancel_transfer() */
USBI_TRANSFER_CANCELLING = 1 << 2,
/* Operation on the transfer failed because the device disappeared */
USBI_TRANSFER_DEVICE_DISAPPEARED = 1 << 3,
};
#define USBI_TRANSFER_TO_LIBUSB_TRANSFER(transfer) \
((struct libusb_transfer *)(((unsigned char *)(transfer)) \
+ sizeof(struct usbi_transfer)))
#define LIBUSB_TRANSFER_TO_USBI_TRANSFER(transfer) \
((struct usbi_transfer *)(((unsigned char *)(transfer)) \
- sizeof(struct usbi_transfer)))
static inline void *usbi_transfer_get_os_priv(struct usbi_transfer *transfer)
{
return ((unsigned char *)transfer) + sizeof(struct usbi_transfer)
+ sizeof(struct libusb_transfer)
+ (transfer->num_iso_packets
* sizeof(struct libusb_iso_packet_descriptor));
}
/* bus structures */
/* All standard descriptors have these 2 fields in common */
struct usb_descriptor_header {
uint8_t bLength;
uint8_t bDescriptorType;
};
/* shared data and functions */
int usbi_io_init(struct libusb_context *ctx);
void usbi_io_exit(struct libusb_context *ctx);
struct libusb_device *usbi_alloc_device(struct libusb_context *ctx,
unsigned long session_id);
struct libusb_device *usbi_get_device_by_session_id(struct libusb_context *ctx,
unsigned long session_id);
int usbi_sanitize_device(struct libusb_device *dev);
void usbi_handle_disconnect(struct libusb_device_handle *handle);
int usbi_handle_transfer_completion(struct usbi_transfer *itransfer,
enum libusb_transfer_status status);
int usbi_handle_transfer_cancellation(struct usbi_transfer *transfer);
int usbi_parse_descriptor(unsigned char *source, const char *descriptor,
void *dest, int host_endian);
int usbi_get_config_index_by_value(struct libusb_device *dev,
uint8_t bConfigurationValue, int *idx);
/* polling */
struct usbi_pollfd {
/* must come first */
struct libusb_pollfd pollfd;
struct list_head list;
};
int usbi_add_pollfd(struct libusb_context *ctx, int fd, short events);
void usbi_remove_pollfd(struct libusb_context *ctx, int fd);
void usbi_fd_notification(struct libusb_context *ctx);
/* device discovery */
/* we traverse usbfs without knowing how many devices we are going to find.
* so we create this discovered_devs model which is similar to a linked-list
* which grows when required. it can be freed once discovery has completed,
* eliminating the need for a list node in the libusb_device structure
* itself. */
struct discovered_devs {
size_t len;
size_t capacity;
struct libusb_device *devices[0];
};
struct discovered_devs *discovered_devs_append(
struct discovered_devs *discdevs, struct libusb_device *dev);
/* OS abstraction */
/* This is the interface that OS backends need to implement.
* All fields are mandatory, except ones explicitly noted as optional. */
struct usbi_os_backend {
/* A human-readable name for your backend, e.g. "Linux usbfs" */
const char *name;
/* Perform initialization of your backend. You might use this function
* to determine specific capabilities of the system, allocate required
* data structures for later, etc.
*
* This function is called when a libusb user initializes the library
* prior to use.
*
* Return 0 on success, or a LIBUSB_ERROR code on failure.
*/
int (*init)(struct libusb_context *ctx);
/* Deinitialization. Optional. This function should destroy anything
* that was set up by init.
*
* This function is called when the user deinitializes the library.
*/
void (*exit)(void);
/* Enumerate all the USB devices on the system, returning them in a list
* of discovered devices.
*
* Your implementation should enumerate all devices on the system,
* regardless of whether they have been seen before or not.
*
* When you have found a device, compute a session ID for it. The session
* ID should uniquely represent that particular device for that particular
* connection session since boot (i.e. if you disconnect and reconnect a
* device immediately after, it should be assigned a different session ID).
* If your OS cannot provide a unique session ID as described above,
* presenting a session ID of (bus_number << 8 | device_address) should
* be sufficient. Bus numbers and device addresses wrap and get reused,
* but that is an unlikely case.
*
* After computing a session ID for a device, call
* usbi_get_device_by_session_id(). This function checks if libusb already
* knows about the device, and if so, it provides you with a libusb_device
* structure for it.
*
* If usbi_get_device_by_session_id() returns NULL, it is time to allocate
* a new device structure for the device. Call usbi_alloc_device() to
* obtain a new libusb_device structure with reference count 1. Populate
* the bus_number and device_address attributes of the new device, and
* perform any other internal backend initialization you need to do. At
* this point, you should be ready to provide device descriptors and so
* on through the get_*_descriptor functions. Finally, call
* usbi_sanitize_device() to perform some final sanity checks on the
* device. Assuming all of the above succeeded, we can now continue.
* If any of the above failed, remember to unreference the device that
* was returned by usbi_alloc_device().
*
* At this stage we have a populated libusb_device structure (either one
* that was found earlier, or one that we have just allocated and
* populated). This can now be added to the discovered devices list
* using discovered_devs_append(). Note that discovered_devs_append()
* may reallocate the list, returning a new location for it, and also
* note that reallocation can fail. Your backend should handle these
* error conditions appropriately.
*
* This function should not generate any bus I/O and should not block.
* If I/O is required (e.g. reading the active configuration value), it is
* OK to ignore these suggestions :)
*
* This function is executed when the user wishes to retrieve a list
* of USB devices connected to the system.
*
* Return 0 on success, or a LIBUSB_ERROR code on failure.
*/
int (*get_device_list)(struct libusb_context *ctx,
struct discovered_devs **discdevs);
/* Open a device for I/O and other USB operations. The device handle
* is preallocated for you, you can retrieve the device in question
* through handle->dev.
*
* Your backend should allocate any internal resources required for I/O
* and other operations so that those operations can happen (hopefully)
* without hiccup. This is also a good place to inform libusb that it
* should monitor certain file descriptors related to this device -
* see the usbi_add_pollfd() function.
*
* This function should not generate any bus I/O and should not block.
*
* This function is called when the user attempts to obtain a device
* handle for a device.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_ACCESS if the user has insufficient permissions
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since
* discovery
* - another LIBUSB_ERROR code on other failure
*
* Do not worry about freeing the handle on failed open, the upper layers
* do this for you.
*/
int (*open)(struct libusb_device_handle *handle);
/* Close a device such that the handle cannot be used again. Your backend
* should destroy any resources that were allocated in the open path.
* This may also be a good place to call usbi_remove_pollfd() to inform
* libusb of any file descriptors associated with this device that should
* no longer be monitored.
*
* This function is called when the user closes a device handle.
*/
void (*close)(struct libusb_device_handle *handle);
/* Retrieve the device descriptor from a device.
*
* The descriptor should be retrieved from memory, NOT via bus I/O to the
* device. This means that you may have to cache it in a private structure
* during get_device_list enumeration. Alternatively, you may be able
* to retrieve it from a kernel interface (some Linux setups can do this)
* still without generating bus I/O.
*
* This function is expected to write DEVICE_DESC_LENGTH (18) bytes into
* buffer, which is guaranteed to be big enough.
*
* This function is called when sanity-checking a device before adding
* it to the list of discovered devices, and also when the user requests
* to read the device descriptor.
*
* This function is expected to return the descriptor in bus-endian format
* (LE). If it returns the multi-byte values in host-endian format,
* set the host_endian output parameter to "1".
*
* Return 0 on success or a LIBUSB_ERROR code on failure.
*/
int (*get_device_descriptor)(struct libusb_device *device,
unsigned char *buffer, int *host_endian);
/* Get the ACTIVE configuration descriptor for a device.
*
* The descriptor should be retrieved from memory, NOT via bus I/O to the
* device. This means that you may have to cache it in a private structure
* during get_device_list enumeration. You may also have to keep track
* of which configuration is active when the user changes it.
*
* This function is expected to write len bytes of data into buffer, which
* is guaranteed to be big enough. If you can only do a partial write,
* return an error code.
*
* This function is expected to return the descriptor in bus-endian format
* (LE). If it returns the multi-byte values in host-endian format,
* set the host_endian output parameter to "1".
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
* - another LIBUSB_ERROR code on other failure
*/
int (*get_active_config_descriptor)(struct libusb_device *device,
unsigned char *buffer, size_t len, int *host_endian);
/* Get a specific configuration descriptor for a device.
*
* The descriptor should be retrieved from memory, NOT via bus I/O to the
* device. This means that you may have to cache it in a private structure
* during get_device_list enumeration.
*
* The requested descriptor is expressed as a zero-based index (i.e. 0
* indicates that we are requesting the first descriptor). The index does
* not (necessarily) equal the bConfigurationValue of the configuration
* being requested.
*
* This function is expected to write len bytes of data into buffer, which
* is guaranteed to be big enough. If you can only do a partial write,
* return an error code.
*
* This function is expected to return the descriptor in bus-endian format
* (LE). If it returns the multi-byte values in host-endian format,
* set the host_endian output parameter to "1".
*
* Return 0 on success or a LIBUSB_ERROR code on failure.
*/
int (*get_config_descriptor)(struct libusb_device *device,
uint8_t config_index, unsigned char *buffer, size_t len,
int *host_endian);
/* Get the bConfigurationValue for the active configuration for a device.
* Optional. This should only be implemented if you can retrieve it from
* cache (don't generate I/O).
*
* If you cannot retrieve this from cache, either do not implement this
* function, or return LIBUSB_ERROR_NOT_SUPPORTED. This will cause
* libusb to retrieve the information through a standard control transfer.
*
* This function must be non-blocking.
* Return:
* - 0 on success
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - LIBUSB_ERROR_NOT_SUPPORTED if the value cannot be retrieved without
* blocking
* - another LIBUSB_ERROR code on other failure.
*/
int (*get_configuration)(struct libusb_device_handle *handle, int *config);
/* Set the active configuration for a device.
*
* A configuration value of -1 should put the device in unconfigured state.
*
* This function can block.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
* - LIBUSB_ERROR_BUSY if interfaces are currently claimed (and hence
* configuration cannot be changed)
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure.
*/
int (*set_configuration)(struct libusb_device_handle *handle, int config);
/* Claim an interface. When claimed, the application can then perform
* I/O to an interface's endpoints.
*
* This function should not generate any bus I/O and should not block.
* Interface claiming is a logical operation that simply ensures that
* no other drivers/applications are using the interface, and after
* claiming, no other drivers/applicatiosn can use the interface because
* we now "own" it.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if the interface does not exist
* - LIBUSB_ERROR_BUSY if the interface is in use by another driver/app
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*claim_interface)(struct libusb_device_handle *handle, int interface_number);
/* Release a previously claimed interface.
*
* This function should also generate a SET_INTERFACE control request,
* resetting the alternate setting of that interface to 0. It's OK for
* this function to block as a result.
*
* You will only ever be asked to release an interface which was
* successfully claimed earlier.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*release_interface)(struct libusb_device_handle *handle, int interface_number);
/* Set the alternate setting for an interface.
*
* You will only ever be asked to set the alternate setting for an
* interface which was successfully claimed earlier.
*
* It's OK for this function to block.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if the alternate setting does not exist
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*set_interface_altsetting)(struct libusb_device_handle *handle,
int interface_number, int altsetting);
/* Clear a halt/stall condition on an endpoint.
*
* It's OK for this function to block.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if the endpoint does not exist
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*clear_halt)(struct libusb_device_handle *handle,
unsigned char endpoint);
/* Perform a USB port reset to reinitialize a device.
*
* If possible, the handle should still be usable after the reset
* completes, assuming that the device descriptors did not change during
* reset and all previous interface state can be restored.
*
* If something changes, or you cannot easily locate/verify the resetted
* device, return LIBUSB_ERROR_NOT_FOUND. This prompts the application
* to close the old handle and re-enumerate the device.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if re-enumeration is required, or if the device
* has been disconnected since it was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*reset_device)(struct libusb_device_handle *handle);
/* Determine if a kernel driver is active on an interface. Optional.
*
* The presence of a kernel driver on an interface indicates that any
* calls to claim_interface would fail with the LIBUSB_ERROR_BUSY code.
*
* Return:
* - 0 if no driver is active
* - 1 if a driver is active
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*kernel_driver_active)(struct libusb_device_handle *handle,
int interface_number);
/* Detach a kernel driver from an interface. Optional.
*
* After detaching a kernel driver, the interface should be available
* for claim.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
* - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - another LIBUSB_ERROR code on other failure
*/
int (*detach_kernel_driver)(struct libusb_device_handle *handle,
int interface_number);
/* Attach a kernel driver to an interface. Optional.
*
* Reattach a kernel driver to the device.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NOT_FOUND if no kernel driver was active
* - LIBUSB_ERROR_INVALID_PARAM if the interface does not exist
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected since it
* was opened
* - LIBUSB_ERROR_BUSY if a program or driver has claimed the interface,
* preventing reattachment
* - another LIBUSB_ERROR code on other failure
*/
int (*attach_kernel_driver)(struct libusb_device_handle *handle,
int interface_number);
/* Destroy a device. Optional.
*
* This function is called when the last reference to a device is
* destroyed. It should free any resources allocated in the get_device_list
* path.
*/
void (*destroy_device)(struct libusb_device *dev);
/* Submit a transfer. Your implementation should take the transfer,
* morph it into whatever form your platform requires, and submit it
* asynchronously.
*
* This function must not block.
*
* Return:
* - 0 on success
* - LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
* - another LIBUSB_ERROR code on other failure
*/
int (*submit_transfer)(struct usbi_transfer *itransfer);
/* Cancel a previously submitted transfer.
*
* This function must not block. The transfer cancellation must complete
* later, resulting in a call to usbi_handle_transfer_cancellation()
* from the context of handle_events.
*/
int (*cancel_transfer)(struct usbi_transfer *itransfer);
/* Clear a transfer as if it has completed or cancelled, but do not
* report any completion/cancellation to the library. You should free
* all private data from the transfer as if you were just about to report
* completion or cancellation.
*
* This function might seem a bit out of place. It is used when libusb
* detects a disconnected device - it calls this function for all pending
* transfers before reporting completion (with the disconnect code) to
* the user. Maybe we can improve upon this internal interface in future.
*/
void (*clear_transfer_priv)(struct usbi_transfer *itransfer);
/* Handle any pending events. This involves monitoring any active
* transfers and processing their completion or cancellation.
*
* The function is passed an array of pollfd structures (size nfds)
* as a result of the poll() system call. The num_ready parameter
* indicates the number of file descriptors that have reported events
* (i.e. the poll() return value). This should be enough information
* for you to determine which actions need to be taken on the currently
* active transfers.
*
* For any cancelled transfers, call usbi_handle_transfer_cancellation().
* For completed transfers, call usbi_handle_transfer_completion().
* For control/bulk/interrupt transfers, populate the "transferred"
* element of the appropriate usbi_transfer structure before calling the
* above functions. For isochronous transfers, populate the status and
* transferred fields of the iso packet descriptors of the transfer.
*
* This function should also be able to detect disconnection of the
* device, reporting that situation with usbi_handle_disconnect().
*
* When processing an event related to a transfer, you probably want to
* take usbi_transfer.lock to prevent races. See the documentation for
* the usbi_transfer structure.
*
* Return 0 on success, or a LIBUSB_ERROR code on failure.
*/
int (*handle_events)(struct libusb_context *ctx,
struct pollfd *fds, POLL_NFDS_TYPE nfds, int num_ready);
/* Get time from specified clock. At least two clocks must be implemented
by the backend: USBI_CLOCK_REALTIME, and USBI_CLOCK_MONOTONIC.
Description of clocks:
USBI_CLOCK_REALTIME : clock returns time since system epoch.
USBI_CLOCK_MONOTONIC: clock returns time since unspecified start
time (usually boot).
*/
int (*clock_gettime)(int clkid, struct timespec *tp);
#ifdef USBI_TIMERFD_AVAILABLE
/* clock ID of the clock that should be used for timerfd */
clockid_t (*get_timerfd_clockid)(void);
#endif
/* Number of bytes to reserve for per-device private backend data.
* This private data area is accessible through the "os_priv" field of
* struct libusb_device. */
size_t device_priv_size;
/* Number of bytes to reserve for per-handle private backend data.
* This private data area is accessible through the "os_priv" field of
* struct libusb_device. */
size_t device_handle_priv_size;
/* Number of bytes to reserve for per-transfer private backend data.
* This private data area is accessible by calling
* usbi_transfer_get_os_priv() on the appropriate usbi_transfer instance.
*/
size_t transfer_priv_size;
/* Mumber of additional bytes for os_priv for each iso packet.
* Can your backend use this? */
/* FIXME: linux can't use this any more. if other OS's cannot either,
* then remove this */
size_t add_iso_packet_size;
};
extern const struct usbi_os_backend * const usbi_backend;
extern const struct usbi_os_backend linux_usbfs_backend;
extern const struct usbi_os_backend darwin_backend;
extern const struct usbi_os_backend openbsd_backend;
extern const struct usbi_os_backend windows_backend;
#endif

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,139 @@
/*
* usbfs header structures
* Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
* Copyright (c) 2001 Johannes Erdfelt <johannes@erdfelt.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBUSB_USBFS_H
#define LIBUSB_USBFS_H
#define SYSFS_DEVICE_PATH "/sys/bus/usb/devices"
struct usbfs_ctrltransfer {
/* keep in sync with usbdevice_fs.h:usbdevfs_ctrltransfer */
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
uint32_t timeout; /* in milliseconds */
/* pointer to data */
void *data;
};
struct usbfs_bulktransfer {
/* keep in sync with usbdevice_fs.h:usbdevfs_bulktransfer */
unsigned int ep;
unsigned int len;
unsigned int timeout; /* in milliseconds */
/* pointer to data */
void *data;
};
struct usbfs_setinterface {
/* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */
unsigned int interface;
unsigned int altsetting;
};
#define USBFS_MAXDRIVERNAME 255
struct usbfs_getdriver {
unsigned int interface;
char driver[USBFS_MAXDRIVERNAME + 1];
};
#define USBFS_URB_SHORT_NOT_OK 0x01
#define USBFS_URB_ISO_ASAP 0x02
#define USBFS_URB_BULK_CONTINUATION 0x04
#define USBFS_URB_QUEUE_BULK 0x10
#define USBFS_URB_ZERO_PACKET 0x40
enum usbfs_urb_type {
USBFS_URB_TYPE_ISO = 0,
USBFS_URB_TYPE_INTERRUPT = 1,
USBFS_URB_TYPE_CONTROL = 2,
USBFS_URB_TYPE_BULK = 3,
};
struct usbfs_iso_packet_desc {
unsigned int length;
unsigned int actual_length;
unsigned int status;
};
#define MAX_ISO_BUFFER_LENGTH 32768
#define MAX_BULK_BUFFER_LENGTH 16384
#define MAX_CTRL_BUFFER_LENGTH 4096
struct usbfs_urb {
unsigned char type;
unsigned char endpoint;
int status;
unsigned int flags;
void *buffer;
int buffer_length;
int actual_length;
int start_frame;
int number_of_packets;
int error_count;
unsigned int signr;
void *usercontext;
struct usbfs_iso_packet_desc iso_frame_desc[0];
};
struct usbfs_connectinfo {
unsigned int devnum;
unsigned char slow;
};
struct usbfs_ioctl {
int ifno; /* interface 0..N ; negative numbers reserved */
int ioctl_code; /* MUST encode size + direction of data so the
* macros in <asm/ioctl.h> give correct values */
void *data; /* param buffer (in, or out) */
};
struct usbfs_hub_portinfo {
unsigned char numports;
unsigned char port[127]; /* port to device num mapping */
};
#define IOCTL_USBFS_CONTROL _IOWR('U', 0, struct usbfs_ctrltransfer)
#define IOCTL_USBFS_BULK _IOWR('U', 2, struct usbfs_bulktransfer)
#define IOCTL_USBFS_RESETEP _IOR('U', 3, unsigned int)
#define IOCTL_USBFS_SETINTF _IOR('U', 4, struct usbfs_setinterface)
#define IOCTL_USBFS_SETCONFIG _IOR('U', 5, unsigned int)
#define IOCTL_USBFS_GETDRIVER _IOW('U', 8, struct usbfs_getdriver)
#define IOCTL_USBFS_SUBMITURB _IOR('U', 10, struct usbfs_urb)
#define IOCTL_USBFS_DISCARDURB _IO('U', 11)
#define IOCTL_USBFS_REAPURB _IOW('U', 12, void *)
#define IOCTL_USBFS_REAPURBNDELAY _IOW('U', 13, void *)
#define IOCTL_USBFS_CLAIMINTF _IOR('U', 15, unsigned int)
#define IOCTL_USBFS_RELEASEINTF _IOR('U', 16, unsigned int)
#define IOCTL_USBFS_CONNECTINFO _IOW('U', 17, struct usbfs_connectinfo)
#define IOCTL_USBFS_IOCTL _IOWR('U', 18, struct usbfs_ioctl)
#define IOCTL_USBFS_HUB_PORTINFO _IOR('U', 19, struct usbfs_hub_portinfo)
#define IOCTL_USBFS_RESET _IO('U', 20)
#define IOCTL_USBFS_CLEAR_HALT _IOR('U', 21, unsigned int)
#define IOCTL_USBFS_DISCONNECT _IO('U', 22)
#define IOCTL_USBFS_CONNECT _IO('U', 23)
#endif

View file

@ -0,0 +1,55 @@
/*
* libusb synchronization using POSIX Threads
*
* Copyright (C) 2011 Vitali Lovich <vlovich@aliph.com>
* Copyright (C) 2011 Peter Stuge <peter@stuge.se>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef _XOPEN_SOURCE
# if _XOPEN_SOURCE < 500
# undef _XOPEN_SOURCE
# define _XOPEN_SOURCE 500
# endif
#else
#define _XOPEN_SOURCE 500
#endif /* _XOPEN_SOURCE */
#include "threads_posix.h"
int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
{
int err;
pthread_mutexattr_t stack_attr;
if (!attr) {
attr = &stack_attr;
err = pthread_mutexattr_init(&stack_attr);
if (err != 0)
return err;
}
err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
if (err != 0)
goto finish;
err = pthread_mutex_init(mutex, attr);
finish:
if (attr == &stack_attr)
pthread_mutexattr_destroy(&stack_attr);
return err;
}

View file

@ -0,0 +1,48 @@
/*
* libusb synchronization using POSIX Threads
*
* Copyright (C) 2010 Peter Stuge <peter@stuge.se>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBUSB_THREADS_POSIX_H
#define LIBUSB_THREADS_POSIX_H
#include <pthread.h>
#define usbi_mutex_static_t pthread_mutex_t
#define USBI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
#define usbi_mutex_static_lock pthread_mutex_lock
#define usbi_mutex_static_unlock pthread_mutex_unlock
#define usbi_mutex_t pthread_mutex_t
#define usbi_mutex_init pthread_mutex_init
#define usbi_mutex_lock pthread_mutex_lock
#define usbi_mutex_unlock pthread_mutex_unlock
#define usbi_mutex_trylock pthread_mutex_trylock
#define usbi_mutex_destroy pthread_mutex_destroy
#define usbi_cond_t pthread_cond_t
#define usbi_cond_init pthread_cond_init
#define usbi_cond_wait pthread_cond_wait
#define usbi_cond_timedwait pthread_cond_timedwait
#define usbi_cond_broadcast pthread_cond_broadcast
#define usbi_cond_destroy pthread_cond_destroy
#define usbi_cond_signal pthread_cond_signal
extern int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
#endif /* LIBUSB_THREADS_POSIX_H */

314
libusbx-1.0.9/libusb/sync.c Normal file
View file

@ -0,0 +1,314 @@
/*
* Synchronous I/O functions for libusb
* Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libusbi.h"
/**
* @defgroup syncio Synchronous device I/O
*
* This page documents libusb's synchronous (blocking) API for USB device I/O.
* This interface is easy to use but has some limitations. More advanced users
* may wish to consider using the \ref asyncio "asynchronous I/O API" instead.
*/
static void LIBUSB_CALL ctrl_transfer_cb(struct libusb_transfer *transfer)
{
int *completed = transfer->user_data;
*completed = 1;
usbi_dbg("actual_length=%d", transfer->actual_length);
/* caller interprets result and frees transfer */
}
/** \ingroup syncio
* Perform a USB control transfer.
*
* The direction of the transfer is inferred from the bmRequestType field of
* the setup packet.
*
* The wValue, wIndex and wLength fields values should be given in host-endian
* byte order.
*
* \param dev_handle a handle for the device to communicate with
* \param bmRequestType the request type field for the setup packet
* \param bRequest the request field for the setup packet
* \param wValue the value field for the setup packet
* \param wIndex the index field for the setup packet
* \param data a suitably-sized data buffer for either input or output
* (depending on direction bits within bmRequestType)
* \param wLength the length field for the setup packet. The data buffer should
* be at least this size.
* \param timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use value 0.
* \returns on success, the number of bytes actually transferred
* \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
* \returns LIBUSB_ERROR_PIPE if the control request was not supported by the
* device
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
* \returns another LIBUSB_ERROR code on other failures
*/
int API_EXPORTED libusb_control_transfer(libusb_device_handle *dev_handle,
uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex,
unsigned char *data, uint16_t wLength, unsigned int timeout)
{
struct libusb_transfer *transfer = libusb_alloc_transfer(0);
unsigned char *buffer;
int completed = 0;
int r;
if (!transfer)
return LIBUSB_ERROR_NO_MEM;
buffer = malloc(LIBUSB_CONTROL_SETUP_SIZE + wLength);
if (!buffer) {
libusb_free_transfer(transfer);
return LIBUSB_ERROR_NO_MEM;
}
libusb_fill_control_setup(buffer, bmRequestType, bRequest, wValue, wIndex,
wLength);
if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT)
memcpy(buffer + LIBUSB_CONTROL_SETUP_SIZE, data, wLength);
libusb_fill_control_transfer(transfer, dev_handle, buffer,
ctrl_transfer_cb, &completed, timeout);
transfer->flags = LIBUSB_TRANSFER_FREE_BUFFER;
r = libusb_submit_transfer(transfer);
if (r < 0) {
libusb_free_transfer(transfer);
return r;
}
while (!completed) {
r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
if (r < 0) {
if (r == LIBUSB_ERROR_INTERRUPTED)
continue;
libusb_cancel_transfer(transfer);
while (!completed)
if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
break;
libusb_free_transfer(transfer);
return r;
}
}
if ((bmRequestType & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
memcpy(data, libusb_control_transfer_get_data(transfer),
transfer->actual_length);
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
r = transfer->actual_length;
break;
case LIBUSB_TRANSFER_TIMED_OUT:
r = LIBUSB_ERROR_TIMEOUT;
break;
case LIBUSB_TRANSFER_STALL:
r = LIBUSB_ERROR_PIPE;
break;
case LIBUSB_TRANSFER_NO_DEVICE:
r = LIBUSB_ERROR_NO_DEVICE;
break;
case LIBUSB_TRANSFER_OVERFLOW:
r = LIBUSB_ERROR_OVERFLOW;
break;
default:
usbi_warn(HANDLE_CTX(dev_handle),
"unrecognised status code %d", transfer->status);
r = LIBUSB_ERROR_OTHER;
}
libusb_free_transfer(transfer);
return r;
}
static void LIBUSB_CALL bulk_transfer_cb(struct libusb_transfer *transfer)
{
int *completed = transfer->user_data;
*completed = 1;
usbi_dbg("actual_length=%d", transfer->actual_length);
/* caller interprets results and frees transfer */
}
static int do_sync_bulk_transfer(struct libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *buffer, int length,
int *transferred, unsigned int timeout, unsigned char type)
{
struct libusb_transfer *transfer = libusb_alloc_transfer(0);
int completed = 0;
int r;
if (!transfer)
return LIBUSB_ERROR_NO_MEM;
libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, length,
bulk_transfer_cb, &completed, timeout);
transfer->type = type;
r = libusb_submit_transfer(transfer);
if (r < 0) {
libusb_free_transfer(transfer);
return r;
}
while (!completed) {
r = libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed);
if (r < 0) {
if (r == LIBUSB_ERROR_INTERRUPTED)
continue;
libusb_cancel_transfer(transfer);
while (!completed)
if (libusb_handle_events_completed(HANDLE_CTX(dev_handle), &completed) < 0)
break;
libusb_free_transfer(transfer);
return r;
}
}
*transferred = transfer->actual_length;
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
r = 0;
break;
case LIBUSB_TRANSFER_TIMED_OUT:
r = LIBUSB_ERROR_TIMEOUT;
break;
case LIBUSB_TRANSFER_STALL:
r = LIBUSB_ERROR_PIPE;
break;
case LIBUSB_TRANSFER_OVERFLOW:
r = LIBUSB_ERROR_OVERFLOW;
break;
case LIBUSB_TRANSFER_NO_DEVICE:
r = LIBUSB_ERROR_NO_DEVICE;
break;
default:
usbi_warn(HANDLE_CTX(dev_handle),
"unrecognised status code %d", transfer->status);
r = LIBUSB_ERROR_OTHER;
}
libusb_free_transfer(transfer);
return r;
}
/** \ingroup syncio
* Perform a USB bulk transfer. The direction of the transfer is inferred from
* the direction bits of the endpoint address.
*
* For bulk reads, the <tt>length</tt> field indicates the maximum length of
* data you are expecting to receive. If less data arrives than expected,
* this function will return that data, so be sure to check the
* <tt>transferred</tt> output parameter.
*
* You should also check the <tt>transferred</tt> parameter for bulk writes.
* Not all of the data may have been written.
*
* Also check <tt>transferred</tt> when dealing with a timeout error code.
* libusb may have to split your transfer into a number of chunks to satisfy
* underlying O/S requirements, meaning that the timeout may expire after
* the first few chunks have completed. libusb is careful not to lose any data
* that may have been transferred; do not assume that timeout conditions
* indicate a complete lack of I/O.
*
* \param dev_handle a handle for the device to communicate with
* \param endpoint the address of a valid endpoint to communicate with
* \param data a suitably-sized data buffer for either input or output
* (depending on endpoint)
* \param length for bulk writes, the number of bytes from data to be sent. for
* bulk reads, the maximum number of bytes to receive into the data buffer.
* \param transferred output location for the number of bytes actually
* transferred.
* \param timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use value 0.
*
* \returns 0 on success (and populates <tt>transferred</tt>)
* \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out (and populates
* <tt>transferred</tt>)
* \returns LIBUSB_ERROR_PIPE if the endpoint halted
* \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
* \ref packetoverflow
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
* \returns another LIBUSB_ERROR code on other failures
*/
int API_EXPORTED libusb_bulk_transfer(struct libusb_device_handle *dev_handle,
unsigned char endpoint, unsigned char *data, int length, int *transferred,
unsigned int timeout)
{
return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
transferred, timeout, LIBUSB_TRANSFER_TYPE_BULK);
}
/** \ingroup syncio
* Perform a USB interrupt transfer. The direction of the transfer is inferred
* from the direction bits of the endpoint address.
*
* For interrupt reads, the <tt>length</tt> field indicates the maximum length
* of data you are expecting to receive. If less data arrives than expected,
* this function will return that data, so be sure to check the
* <tt>transferred</tt> output parameter.
*
* You should also check the <tt>transferred</tt> parameter for interrupt
* writes. Not all of the data may have been written.
*
* Also check <tt>transferred</tt> when dealing with a timeout error code.
* libusb may have to split your transfer into a number of chunks to satisfy
* underlying O/S requirements, meaning that the timeout may expire after
* the first few chunks have completed. libusb is careful not to lose any data
* that may have been transferred; do not assume that timeout conditions
* indicate a complete lack of I/O.
*
* The default endpoint bInterval value is used as the polling interval.
*
* \param dev_handle a handle for the device to communicate with
* \param endpoint the address of a valid endpoint to communicate with
* \param data a suitably-sized data buffer for either input or output
* (depending on endpoint)
* \param length for bulk writes, the number of bytes from data to be sent. for
* bulk reads, the maximum number of bytes to receive into the data buffer.
* \param transferred output location for the number of bytes actually
* transferred.
* \param timeout timeout (in millseconds) that this function should wait
* before giving up due to no response being received. For an unlimited
* timeout, use value 0.
*
* \returns 0 on success (and populates <tt>transferred</tt>)
* \returns LIBUSB_ERROR_TIMEOUT if the transfer timed out
* \returns LIBUSB_ERROR_PIPE if the endpoint halted
* \returns LIBUSB_ERROR_OVERFLOW if the device offered more data, see
* \ref packetoverflow
* \returns LIBUSB_ERROR_NO_DEVICE if the device has been disconnected
* \returns another LIBUSB_ERROR code on other error
*/
int API_EXPORTED libusb_interrupt_transfer(
struct libusb_device_handle *dev_handle, unsigned char endpoint,
unsigned char *data, int length, int *transferred, unsigned int timeout)
{
return do_sync_bulk_transfer(dev_handle, endpoint, data, length,
transferred, timeout, LIBUSB_TRANSFER_TYPE_INTERRUPT);
}

View file

@ -0,0 +1,18 @@
/* This file is parsed by m4 and windres and RC.EXE so please keep it simple. */
#ifndef LIBUSB_MAJOR
#define LIBUSB_MAJOR 1
#endif
#ifndef LIBUSB_MINOR
#define LIBUSB_MINOR 0
#endif
#ifndef LIBUSB_MICRO
#define LIBUSB_MICRO 9
#endif
/* LIBUSB_NANO may be used for Windows internal versioning. 0 means unused. */
#ifndef LIBUSB_NANO
#define LIBUSB_NANO 0
#endif
/* LIBUSB_RC is the release candidate suffix. Should normally be empty. */
#ifndef LIBUSB_RC
#define LIBUSB_RC ""
#endif