summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Drake <dsd@gentoo.org>2007-11-28 13:48:45 +0000
committerDaniel Drake <dsd@gentoo.org>2007-12-02 22:54:59 +0000
commit852bba4754ec57679c823f33e8feba6e4a564cbe (patch)
treecd0a73c5437d1e05799b7bbef047854afdc9939a
downloadlibusb-852bba4754ec57679c823f33e8feba6e4a564cbe.tar.gz
Initial commit
Basic library structure which supports enumerating detected USB devices
-rw-r--r--.gitignore29
-rw-r--r--AUTHORS1
-rw-r--r--COPYING504
-rw-r--r--INSTALL234
-rw-r--r--Makefile.am21
-rw-r--r--NEWS3
-rw-r--r--README17
-rw-r--r--TODO6
-rwxr-xr-xautogen.sh8
-rw-r--r--configure.ac49
-rw-r--r--examples/Makefile.am9
-rw-r--r--examples/dpfp.c556
-rw-r--r--examples/lsusb.c46
-rw-r--r--fpusb.pc.in11
-rw-r--r--libfpusb/Makefile.am7
-rw-r--r--libfpusb/core.c302
-rw-r--r--libfpusb/fpusb.h199
-rw-r--r--libfpusb/fpusbi.h200
-rw-r--r--libfpusb/io.c700
-rw-r--r--libfpusb/signalfd.h76
-rw-r--r--libfpusb/usbfs.h135
21 files changed, 3113 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5c29739
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,29 @@
+.deps
+.libs
+Makefile
+Makefile.in
+*.la
+*.lo
+*.o
+libtool
+ltmain.sh
+missing
+stamp-h1
+autom4te.cache
+install-sh
+depcomp
+configure
+aclocal.m4
+compile
+config.guess
+config.h*
+config.log
+config.status
+config.sub
+*.swp
+lsusb
+dpfp
+*.pc
+fpusb-*.tar.bz2
+fpusb-*.tar.gz
+ChangeLog
diff --git a/AUTHORS b/AUTHORS
new file mode 100644
index 0000000..e119bbd
--- /dev/null
+++ b/AUTHORS
@@ -0,0 +1 @@
+Daniel Drake <dsd@gentoo.org>
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..5ab7695
--- /dev/null
+++ b/COPYING
@@ -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!
+
+
diff --git a/INSTALL b/INSTALL
new file mode 100644
index 0000000..5458714
--- /dev/null
+++ b/INSTALL
@@ -0,0 +1,234 @@
+Installation Instructions
+*************************
+
+Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005,
+2006 Free Software Foundation, Inc.
+
+This file is free documentation; the Free Software Foundation gives
+unlimited permission to copy, distribute and modify it.
+
+Basic Installation
+==================
+
+Briefly, the shell commands `./configure; make; make install' should
+configure, build, and install this package. The following
+more-detailed instructions are generic; see the `README' file for
+instructions specific to this package.
+
+ The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation. It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions. Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, and a
+file `config.log' containing compiler output (useful mainly for
+debugging `configure').
+
+ It can also use an optional file (typically called `config.cache'
+and enabled with `--cache-file=config.cache' or simply `-C') that saves
+the results of its tests to speed up reconfiguring. Caching is
+disabled by default to prevent problems with accidental use of stale
+cache files.
+
+ If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release. If you are using the cache, and at
+some point `config.cache' contains results you don't want to keep, you
+may remove or edit it.
+
+ The file `configure.ac' (or `configure.in') is used to create
+`configure' by a program called `autoconf'. You need `configure.ac' if
+you want to change it or regenerate `configure' using a newer version
+of `autoconf'.
+
+The simplest way to compile this package is:
+
+ 1. `cd' to the directory containing the package's source code and type
+ `./configure' to configure the package for your system.
+
+ Running `configure' might take a while. While running, it prints
+ some messages telling which features it is checking for.
+
+ 2. Type `make' to compile the package.
+
+ 3. Optionally, type `make check' to run any self-tests that come with
+ the package.
+
+ 4. Type `make install' to install the programs and any data files and
+ documentation.
+
+ 5. You can remove the program binaries and object files from the
+ source code directory by typing `make clean'. To also remove the
+ files that `configure' created (so you can compile the package for
+ a different kind of computer), type `make distclean'. There is
+ also a `make maintainer-clean' target, but that is intended mainly
+ for the package's developers. If you use it, you may have to get
+ all sorts of other programs in order to regenerate files that came
+ with the distribution.
+
+Compilers and Options
+=====================
+
+Some systems require unusual options for compilation or linking that the
+`configure' script does not know about. Run `./configure --help' for
+details on some of the pertinent environment variables.
+
+ You can give `configure' initial values for configuration parameters
+by setting variables in the command line or in the environment. Here
+is an example:
+
+ ./configure CC=c99 CFLAGS=-g LIBS=-lposix
+
+ *Note Defining Variables::, for more details.
+
+Compiling For Multiple Architectures
+====================================
+
+You can compile the package for more than one kind of computer at the
+same time, by placing the object files for each architecture in their
+own directory. To do this, you can use GNU `make'. `cd' to the
+directory where you want the object files and executables to go and run
+the `configure' script. `configure' automatically checks for the
+source code in the directory that `configure' is in and in `..'.
+
+ With a non-GNU `make', it is safer to compile the package for one
+architecture at a time in the source code directory. After you have
+installed the package for one architecture, use `make distclean' before
+reconfiguring for another architecture.
+
+Installation Names
+==================
+
+By default, `make install' installs the package's commands under
+`/usr/local/bin', include files under `/usr/local/include', etc. You
+can specify an installation prefix other than `/usr/local' by giving
+`configure' the option `--prefix=PREFIX'.
+
+ You can specify separate installation prefixes for
+architecture-specific files and architecture-independent files. If you
+pass the option `--exec-prefix=PREFIX' to `configure', the package uses
+PREFIX as the prefix for installing programs and libraries.
+Documentation and other data files still use the regular prefix.
+
+ In addition, if you use an unusual directory layout you can give
+options like `--bindir=DIR' to specify different values for particular
+kinds of files. Run `configure --help' for a list of the directories
+you can set and what kinds of files go in them.
+
+ If the package supports it, you can cause programs to be installed
+with an extra prefix or suffix on their names by giving `configure' the
+option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'.
+
+Optional Features
+=================
+
+Some packages pay attention to `--enable-FEATURE' options to
+`configure', where FEATURE indicates an optional part of the package.
+They may also pay attention to `--with-PACKAGE' options, where PACKAGE
+is something like `gnu-as' or `x' (for the X Window System). The
+`README' should mention any `--enable-' and `--with-' options that the
+package recognizes.
+
+ For packages that use the X Window System, `configure' can usually
+find the X include and library files automatically, but if it doesn't,
+you can use the `configure' options `--x-includes=DIR' and
+`--x-libraries=DIR' to specify their locations.
+
+Specifying the System Type
+==========================
+
+There may be some features `configure' cannot figure out automatically,
+but needs to determine by the type of machine the package will run on.
+Usually, assuming the package is built to be run on the _same_
+architectures, `configure' can figure that out, but if it prints a
+message saying it cannot guess the machine type, give it the
+`--build=TYPE' option. TYPE can either be a short name for the system
+type, such as `sun4', or a canonical name which has the form:
+
+ CPU-COMPANY-SYSTEM
+
+where SYSTEM can have one of these forms:
+
+ OS KERNEL-OS
+
+ See the file `config.sub' for the possible values of each field. If
+`config.sub' isn't included in this package, then this package doesn't
+need to know the machine type.
+
+ If you are _building_ compiler tools for cross-compiling, you should
+use the option `--target=TYPE' to select the type of system they will
+produce code for.
+
+ If you want to _use_ a cross compiler, that generates code for a
+platform different from the build platform, you should specify the
+"host" platform (i.e., that on which the generated programs will
+eventually be run) with `--host=TYPE'.
+
+Sharing Defaults
+================
+
+If you want to set default values for `configure' scripts to share, you
+can create a site shell script called `config.site' that gives default
+values for variables like `CC', `cache_file', and `prefix'.
+`configure' looks for `PREFIX/share/config.site' if it exists, then
+`PREFIX/etc/config.site' if it exists. Or, you can set the
+`CONFIG_SITE' environment variable to the location of the site script.
+A warning: not all `configure' scripts look for a site script.
+
+Defining Variables
+==================
+
+Variables not defined in a site shell script can be set in the
+environment passed to `configure'. However, some packages may run
+configure again during the build, and the customized values of these
+variables may be lost. In order to avoid this problem, you should set
+them in the `configure' command line, using `VAR=value'. For example:
+
+ ./configure CC=/usr/local2/bin/gcc
+
+causes the specified `gcc' to be used as the C compiler (unless it is
+overridden in the site shell script).
+
+Unfortunately, this technique does not work for `CONFIG_SHELL' due to
+an Autoconf bug. Until the bug is fixed you can use this workaround:
+
+ CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash
+
+`configure' Invocation
+======================
+
+`configure' recognizes the following options to control how it operates.
+
+`--help'
+`-h'
+ Print a summary of the options to `configure', and exit.
+
+`--version'
+`-V'
+ Print the version of Autoconf used to generate the `configure'
+ script, and exit.
+
+`--cache-file=FILE'
+ Enable the cache: use and save the results of the tests in FILE,
+ traditionally `config.cache'. FILE defaults to `/dev/null' to
+ disable caching.
+
+`--config-cache'
+`-C'
+ Alias for `--cache-file=config.cache'.
+
+`--quiet'
+`--silent'
+`-q'
+ Do not print messages saying which checks are being made. To
+ suppress all normal output, redirect it to `/dev/null' (any error
+ messages will still be shown).
+
+`--srcdir=DIR'
+ Look for the package's source code in directory DIR. Usually
+ `configure' can determine that directory automatically.
+
+`configure' also accepts some other, not widely useful, options. Run
+`configure --help' for more details.
+
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..870224a
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,21 @@
+AUTOMAKE_OPTIONS = dist-bzip2
+DISTCLEANFILES = ChangeLog fpusb.pc
+EXTRA_DIST = TODO
+SUBDIRS = libfpusb
+
+if BUILD_EXAMPLES
+SUBDIRS += examples
+endif
+
+pkgconfigdir=$(libdir)/pkgconfig
+pkgconfig_DATA=fpusb.pc
+
+.PHONY: ChangeLog dist-up
+ChangeLog:
+ git --git-dir $(top_srcdir)/.git log > ChangeLog || touch ChangeLog
+
+dist-hook: ChangeLog
+
+dist-up: dist
+ ncftpput upload.sourceforge.net incoming $(distdir).tar.bz2
+
diff --git a/NEWS b/NEWS
new file mode 100644
index 0000000..651f65c
--- /dev/null
+++ b/NEWS
@@ -0,0 +1,3 @@
+This file lists notable changes in each release. For the full history of all
+changes, see ChangeLog.
+
diff --git a/README b/README
new file mode 100644
index 0000000..e7df070
--- /dev/null
+++ b/README
@@ -0,0 +1,17 @@
+fpusb
+=====
+
+fpusb is another library for USB device access from Linux userspace.
+It is written in C and licensed under the LGPL-2.1 (see COPYING).
+
+See the homepage for more information:
+http://www.reactivated.net/fprint/wiki/Fpusb
+
+Use the mailing list for questions, comments, etc:
+http://www.reactivated.net/fprint/wiki/Mailing_list
+
+Use the bug tracker for bugs and feature requests:
+http://www.reactivated.net/fprint/bugs/index.php?project=4&switch=1&do=index
+
+- Daniel Drake <dsd@gentoo.org>
+
diff --git a/TODO b/TODO
new file mode 100644
index 0000000..2e20edb
--- /dev/null
+++ b/TODO
@@ -0,0 +1,6 @@
+isochronous endpoint I/O
+optional timerfd support (runtime detection)
+API docs
+notifications of hotplugged/unplugged devices
+thread safety
+signalfd emulation through pipes and sigaction() for older kernels
diff --git a/autogen.sh b/autogen.sh
new file mode 100755
index 0000000..4f5e612
--- /dev/null
+++ b/autogen.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+libtoolize --copy --force || exit 1
+aclocal || exit 1
+autoheader || exit 1
+autoconf || exit 1
+automake -a -c || exit 1
+./configure --enable-maintainer-mode --enable-debug-log \
+ --enable-examples-build $*
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..9829012
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,49 @@
+AC_INIT([fpusb], [0.0])
+AM_INIT_AUTOMAKE
+AC_CONFIG_SRCDIR([libfpusb/core.c])
+AM_CONFIG_HEADER([config.h])
+
+AC_PREREQ([2.50])
+AC_PROG_CC
+AC_PROG_LIBTOOL
+AC_C_INLINE
+AM_PROG_CC_C_O
+
+# Library versioning
+lt_major="0"
+lt_revision="0"
+lt_age="0"
+AC_SUBST(lt_major)
+AC_SUBST(lt_revision)
+AC_SUBST(lt_age)
+
+# Message logging
+AC_ARG_ENABLE([log], [AS_HELP_STRING([--disable-log], [disable all logging])],
+ [log_enabled=$enableval],
+ [log_enabled='yes'])
+if test "x$log_enabled" != "xno"; then
+ AC_DEFINE([ENABLE_LOGGING], 1, [Message logging])
+fi
+
+AC_ARG_ENABLE([debug-log], [AS_HELP_STRING([--enable-debug-log],
+ [enable debug logging (default n)])],
+ [debug_log_enabled=$enableval],
+ [debug_log_enabled='no'])
+if test "x$debug_log_enabled" != "xno"; then
+ AC_DEFINE([ENABLE_DEBUG_LOGGING], 1, [Debug message logging])
+fi
+
+# Examples build
+AC_ARG_ENABLE([examples-build], [AS_HELP_STRING([--enable-examples-build],
+ [build example applications (default n)])],
+ [build_examples=$enableval],
+ [build_examples='no'])
+AM_CONDITIONAL([BUILD_EXAMPLES], [test "x$build_examples" != "xno"])
+
+AC_DEFINE([API_EXPORTED], [__attribute__((visibility("default")))], [Default visibility])
+AM_CFLAGS="-Werror-implicit-function-declaration -Wimplicit-int -Wunreachable-code -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wnonnull -Wreturn-type -Wextra -Wshadow"
+AC_SUBST(AM_CFLAGS)
+
+AC_CONFIG_FILES([fpusb.pc] [Makefile] [libfpusb/Makefile] [examples/Makefile])
+AC_OUTPUT
+
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 0000000..8aa3cc2
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1,9 @@
+INCLUDES = -I$(top_srcdir)
+noinst_PROGRAMS = lsusb dpfp
+
+lsusb_SOURCES = lsusb.c
+lsusb_LDADD = ../libfpusb/libfpusb.la -lfpusb
+
+dpfp_SOURCES = dpfp.c
+dpfp_LDADD = ../libfpusb/libfpusb.la -lfpusb
+
diff --git a/examples/dpfp.c b/examples/dpfp.c
new file mode 100644
index 0000000..e5aed9f
--- /dev/null
+++ b/examples/dpfp.c
@@ -0,0 +1,556 @@
+/*
+ * fpusb example program to manipulate U.are.U 4000B fingerprint scanner.
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Basic image capture program only, does not consider the powerup quirks or
+ * the fact that image encryption may be enabled. Not expected to work
+ * flawlessly all of the time.
+ *
+ * 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 <signal.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <libfpusb/fpusb.h>
+
+#define EP_INTR (1 | USB_ENDPOINT_IN)
+#define EP_DATA (2 | USB_ENDPOINT_IN)
+#define CTRL_IN (USB_TYPE_VENDOR | USB_ENDPOINT_IN)
+#define CTRL_OUT (USB_TYPE_VENDOR | USB_ENDPOINT_OUT)
+#define USB_RQ 0x04
+#define INTR_LENGTH 64
+
+enum {
+ MODE_INIT = 0x00,
+ MODE_AWAIT_FINGER_ON = 0x10,
+ MODE_AWAIT_FINGER_OFF = 0x12,
+ MODE_CAPTURE = 0x20,
+ MODE_SHUT_UP = 0x30,
+ MODE_READY = 0x80,
+};
+
+static int next_state(void);
+static int submit_irq_urb(void);
+static int submit_img_urb(void);
+
+enum {
+ STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
+ STATE_AWAIT_IRQ_FINGER_DETECTED,
+ STATE_AWAIT_MODE_CHANGE_CAPTURE,
+ STATE_AWAIT_IMAGE,
+ STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
+ STATE_AWAIT_IRQ_FINGER_REMOVED,
+};
+
+static int state = 0;
+static struct fpusb_dev_handle *devh = NULL;
+static unsigned char imgbuf[0x1b340];
+static unsigned char irqbuf[INTR_LENGTH];
+static fpusb_urb_handle *img_urbh = NULL;
+static fpusb_urb_handle *irq_urbh = NULL;
+static int img_idx = 0;
+static int do_exit = 0;
+
+static struct fpusb_bulk_msg imgmsg = {
+ .endpoint = EP_DATA,
+ .data = imgbuf,
+ .length = sizeof(imgbuf),
+};
+
+static struct fpusb_bulk_msg irqmsg = {
+ .endpoint = EP_INTR,
+ .data = irqbuf,
+ .length = sizeof(irqbuf),
+};
+
+static struct fpusb_dev *find_dpfp_device(void)
+{
+ struct fpusb_dev *dev;
+
+ fpusb_find_devices();
+
+ for (dev = fpusb_get_devices(); dev; dev = fpusb_dev_next(dev)) {
+ struct usb_dev_descriptor *desc = fpusb_dev_get_descriptor(dev);
+ if (desc->idVendor == 0x05ba && desc->idProduct == 0x000a)
+ return dev;
+ }
+
+ return NULL;
+}
+
+static int print_f0_data(void)
+{
+ unsigned char data[0x10];
+ struct fpusb_ctrl_msg msg = {
+ .requesttype = CTRL_IN,
+ .request = USB_RQ,
+ .value = 0xf0,
+ .index = 0,
+ .length = sizeof(data),
+ .data = data,
+ };
+ int r;
+ unsigned int i;
+
+ r = fpusb_ctrl_msg(devh, &msg, 0);
+ if (r < 0) {
+ fprintf(stderr, "F0 error %d\n", r);
+ return r;
+ }
+ if ((unsigned int) r < sizeof(data)) {
+ fprintf(stderr, "short read (%d)\n", r);
+ return -1;
+ }
+
+ printf("F0 data:");
+ for (i = 0; i < sizeof(data); i++)
+ printf("%02x ", data[i]);
+ printf("\n");
+ return 0;
+}
+
+static int get_hwstat(unsigned char *status)
+{
+ struct fpusb_ctrl_msg msg = {
+ .requesttype = CTRL_IN,
+ .request = USB_RQ,
+ .value = 0x07,
+ .index = 0,
+ .length = 1,
+ .data = status,
+ };
+ int r;
+
+ r = fpusb_ctrl_msg(devh, &msg, 0);
+ if (r < 0) {
+ fprintf(stderr, "read hwstat error %d\n", r);
+ return r;
+ }
+ if ((unsigned int) r < 1) {
+ fprintf(stderr, "short read (%d)\n", r);
+ return -1;
+ }
+
+ printf("hwstat reads %02x\n", *status);
+ return 0;
+}
+
+static int set_hwstat(unsigned char data)
+{
+ int r;
+ struct fpusb_ctrl_msg msg = {
+ .requesttype = CTRL_OUT,
+ .request = USB_RQ,
+ .value = 0x07,
+ .index = 0,
+ .length = 1,
+ .data = &data,
+ };
+
+ printf("set hwstat to %02x\n", data);
+
+ r = fpusb_ctrl_msg(devh, &msg, 0);
+ if (r < 0) {
+ fprintf(stderr, "set hwstat error %d\n", r);
+ return r;
+ }
+ if ((unsigned int) r < 1) {
+ fprintf(stderr, "short write (%d)", r);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int set_mode(unsigned char data)
+{
+ int r;
+ struct fpusb_ctrl_msg msg = {
+ .requesttype = CTRL_OUT,
+ .request = USB_RQ,
+ .value = 0x4e,
+ .index = 0,
+ .length = 1,
+ .data = &data,
+ };
+
+ printf("set mode %02x\n", data);
+
+ r = fpusb_ctrl_msg(devh, &msg, 0);
+ if (r < 0) {
+ fprintf(stderr, "set mode error %d\n", r);
+ return r;
+ }
+ if ((unsigned int) r < 1) {
+ fprintf(stderr, "short write (%d)", r);
+ return -1;
+ }
+
+ return 0;
+}
+
+static void cb_mode_changed(struct fpusb_dev_handle *_devh,
+ struct fpusb_urb_handle *urbh, struct fpusb_ctrl_msg *msg,
+ enum fp_urb_cb_status status, unsigned char *data, int actual_length,
+ void *user_data)
+{
+ if (status != FP_URB_COMPLETED) {
+ fprintf(stderr, "mode change URB not completed!\n");
+ do_exit = 2;
+ }
+
+ printf("async cb_mode_changed\n");
+ if (next_state() < 0)
+ do_exit = 2;
+}
+
+static int set_mode_async(unsigned char data)
+{
+ fpusb_urb_handle *urbh;
+ struct fpusb_ctrl_msg msg = {
+ .requesttype = CTRL_OUT,
+ .request = USB_RQ,
+ .value = 0x4e,
+ .index = 0,
+ .length = 1,
+ .data = &data,
+ };
+
+ printf("async set mode %02x\n", data);
+
+ urbh = fpusb_submit_ctrl_msg(devh, &msg, cb_mode_changed, NULL, 1000);
+ if (!urbh) {
+ fprintf(stderr, "set mode submit error\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+static int do_sync_intr(unsigned char *data)
+{
+ struct fpusb_bulk_msg msg = {
+ .endpoint = EP_INTR,
+ .data = data,
+ .length = INTR_LENGTH,
+ };
+ int r;
+ int transferred;
+
+ r = fpusb_intr_msg(devh, &msg, &transferred, 1000);
+ if (r < 0) {
+ fprintf(stderr, "intr error %d\n", r);
+ return r;
+ }
+ if (transferred < INTR_LENGTH) {
+ fprintf(stderr, "short read (%d)\n", r);
+ return -1;
+ }
+
+ printf("recv interrupt %04x\n", *((uint16_t *) data));
+ return 0;
+}
+
+static int sync_intr(unsigned char type)
+{
+ int r;
+ unsigned char data[INTR_LENGTH];
+
+ while (1) {
+ r = do_sync_intr(data);
+ if (r < 0)
+ return r;
+ if (data[0] == type)
+ return 0;
+ }
+}
+
+static int save_to_file(unsigned char *data)
+{
+ FILE *fd;
+ char filename[64];
+
+ sprintf(filename, "finger%d.pgm", img_idx++);
+ fd = fopen(filename, "w");
+ if (!fd)
+ return -1;
+
+ fputs("P5 384 289 255 ", fd);
+ fwrite(data + 64, 1, 384*289, fd);
+ fclose(fd);
+ printf("saved image to %s\n", filename);
+ return 0;
+}
+
+static int next_state(void)
+{
+ int r = 0;
+ printf("old state: %d\n", state);
+ switch (state) {
+ case STATE_AWAIT_IRQ_FINGER_REMOVED:
+ state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
+ r = set_mode_async(MODE_AWAIT_FINGER_ON);
+ break;
+ case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
+ state = STATE_AWAIT_IRQ_FINGER_DETECTED;
+ break;
+ case STATE_AWAIT_IRQ_FINGER_DETECTED:
+ state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
+ r = set_mode_async(MODE_CAPTURE);
+ break;
+ case STATE_AWAIT_MODE_CHANGE_CAPTURE:
+ state = STATE_AWAIT_IMAGE;
+ break;
+ case STATE_AWAIT_IMAGE:
+ state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
+ r = set_mode_async(MODE_AWAIT_FINGER_OFF);
+ break;
+ case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
+ state = STATE_AWAIT_IRQ_FINGER_REMOVED;
+ break;
+ default:
+ printf("unrecognised state %d\n", state);
+ }
+ if (r < 0) {
+ fprintf(stderr, "error detected changing state");
+ return r;
+ }
+
+ printf("new state: %d\n", state);
+ return 0;
+}
+
+static void cb_irq(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh,
+ struct fpusb_bulk_msg *msg, enum fp_urb_cb_status status,
+ int actual_length, void *user_data)
+{
+ unsigned char irqtype = msg->data[0];
+
+ if (status != FP_URB_COMPLETED) {
+ fprintf(stderr, "irq URB status %d?\n", status);
+ do_exit = 2;
+ return;
+ }
+
+ printf("IRQ callback %02x\n", irqtype);
+ switch (state) {
+ case STATE_AWAIT_IRQ_FINGER_DETECTED:
+ if (irqtype == 0x01) {
+ if (next_state() < 0) {
+ do_exit = 2;
+ return;
+ }
+ } else {
+ printf("finger-on-sensor detected in wrong state!\n");
+ }
+ break;
+ case STATE_AWAIT_IRQ_FINGER_REMOVED:
+ if (irqtype == 0x02) {
+ if (next_state() < 0) {
+ do_exit = 2;
+ return;
+ }
+ } else {
+ printf("finger-on-sensor detected in wrong state!\n");
+ }
+ break;
+ }
+ if (submit_irq_urb() < 0)
+ do_exit = 2;
+}
+
+static void cb_img(fpusb_dev_handle *_devh, fpusb_urb_handle *urbh,
+ struct fpusb_bulk_msg *msg, enum fp_urb_cb_status status,
+ int actual_length, void *user_data)
+{
+ if (status != FP_URB_COMPLETED) {
+ fprintf(stderr, "img URB status %d?\n", status);
+ do_exit = 2;
+ return;
+ }
+
+ printf("Image callback\n");
+ save_to_file(imgbuf);
+ if (next_state() < 0) {
+ do_exit = 2;
+ return;
+ }
+ if (submit_img_urb() < 0)
+ do_exit = 2;
+}
+
+static int submit_irq_urb(void)
+{
+ fpusb_urb_handle_free(irq_urbh);
+ irq_urbh = fpusb_submit_intr_msg(devh, &irqmsg, cb_irq, NULL, 0);
+ return irq_urbh != NULL;
+}
+
+static int submit_img_urb(void)
+{
+ fpusb_urb_handle_free(img_urbh);
+ img_urbh = fpusb_submit_bulk_msg(devh, &imgmsg, cb_img, NULL, 0);
+ return img_urbh != NULL;
+}
+
+static int init_capture(void)
+{
+ int r;
+
+ r = submit_irq_urb();
+ if (r < 0)
+ return r;
+
+ r = submit_img_urb();
+ if (r < 0) {
+ fpusb_urb_handle_cancel_sync(devh, img_urbh);
+ return r;
+ }
+
+ /* start state machine */
+ state = STATE_AWAIT_IRQ_FINGER_REMOVED;
+ return next_state();
+}
+
+static int do_init(void)
+{
+ unsigned char status;
+ int r;
+
+ r = get_hwstat(&status);
+ if (r < 0)
+ return r;
+
+ if (!(status & 0x80)) {
+ r = set_hwstat(status | 0x80);
+ if (r < 0)
+ return r;
+ r = get_hwstat(&status);
+ if (r < 0)
+ return r;
+ }
+
+ status &= ~0x80;
+ r = set_hwstat(status);
+ if (r < 0)
+ return r;
+
+ r = get_hwstat(&status);
+ if (r < 0)
+ return r;
+
+ r = sync_intr(0x56);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
+
+static void sighandler(int signum)
+{
+ do_exit = 1;
+}
+
+int main(void)
+{
+ struct fpusb_dev *dev;
+ struct sigaction sigact;
+ int r = 1;
+
+ r = fpusb_init(0);
+ if (r < 0) {
+ fprintf(stderr, "failed to initialise fpusb\n");
+ exit(1);
+ }
+
+ dev = find_dpfp_device();
+ if (!dev) {
+ fprintf(stderr, "No device found\n");
+ goto out;
+ }
+ printf("found device\n");
+
+ devh = fpusb_dev_open(dev);
+ if (!devh) {
+ fprintf(stderr, "Could not open device\n");
+ goto out;
+ }
+ printf("opened device\n");
+
+ r = fpusb_dev_claim_intf(devh, 0);
+ if (r < 0) {
+ fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
+ goto out;
+ }
+ printf("claimed interface\n");
+
+ r = print_f0_data();
+ if (r < 0)
+ goto out_release;
+
+ r = do_init();
+ if (r < 0)
+ goto out_deinit;
+
+ /* async from here onwards */
+
+ r = init_capture();
+ if (r < 0)
+ goto out_deinit;
+
+ sigact.sa_handler = sighandler;
+ sigemptyset(&sigact.sa_mask);
+ sigact.sa_flags = 0;
+ sigaction(SIGINT, &sigact, NULL);
+ sigaction(SIGTERM, &sigact, NULL);
+ sigaction(SIGQUIT, &sigact, NULL);
+
+ while (!do_exit) {
+ r = fpusb_poll();
+ if (r < 0)
+ goto out_deinit;
+ }
+
+ printf("shutting down...\n");
+
+ r = fpusb_urb_handle_cancel_sync(devh, irq_urbh);
+ if (r < 0)
+ goto out_deinit;
+
+ r = fpusb_urb_handle_cancel_sync(devh, img_urbh);
+ if (r < 0)
+ goto out_deinit;
+
+ if (do_exit == 1)
+ r = 0;
+ else
+ r = 1;
+
+out_deinit:
+ fpusb_urb_handle_free(img_urbh);
+ fpusb_urb_handle_free(irq_urbh);
+ set_mode(0);
+ set_hwstat(0x80);
+out_release:
+ fpusb_dev_release_intf(devh, 0);
+out:
+ fpusb_dev_close(devh);
+ fpusb_exit();
+ return r >= 0 ? r : -r;
+}
+
diff --git a/examples/lsusb.c b/examples/lsusb.c
new file mode 100644
index 0000000..00fb178
--- /dev/null
+++ b/examples/lsusb.c
@@ -0,0 +1,46 @@
+/*
+ * fpusb example program to list devices on the bus
+ * Copyright (C) 2007 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 <stdio.h>
+
+#include <libfpusb/fpusb.h>
+
+void print_devs(fpusb_dev *devs)
+{
+ fpusb_dev *dev;
+
+ for (dev = devs; dev; dev = fpusb_dev_next(dev)) {
+ struct usb_dev_descriptor *desc = fpusb_dev_get_descriptor(dev);
+ printf("%04x:%04x\n", desc->idVendor, desc->idProduct);
+ }
+}
+
+int main(void)
+{
+ fpusb_dev *devs;
+ fpusb_init(0);
+ fpusb_find_devices();
+ devs = fpusb_get_devices();
+
+ print_devs(devs);
+
+ fpusb_exit();
+ return 0;
+}
+
diff --git a/fpusb.pc.in b/fpusb.pc.in
new file mode 100644
index 0000000..8fd7221
--- /dev/null
+++ b/fpusb.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: fpusb
+Description: C API for USB device access from Linux userspace
+Version: @VERSION@
+Libs: -L${libdir} -lfpusb
+Cflags: -I${includedir}/fpusb
+
diff --git a/libfpusb/Makefile.am b/libfpusb/Makefile.am
new file mode 100644
index 0000000..85ee8a8
--- /dev/null
+++ b/libfpusb/Makefile.am
@@ -0,0 +1,7 @@
+lib_LTLIBRARIES = libfpusb.la
+
+libfpusb_la_CFLAGS = -fvisibility=hidden $(AM_CFLAGS)
+libfpusb_la_SOURCES = signalfd.h fpusbi.h usbfs.h core.c io.c
+libfpusb_la_LIBADD = -lrt
+
+pkginclude_HEADERS = fpusb.h
diff --git a/libfpusb/core.c b/libfpusb/core.c
new file mode 100644
index 0000000..edb1de3
--- /dev/null
+++ b/libfpusb/core.c
@@ -0,0 +1,302 @@
+/*
+ * Core functions for libfpusb
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Portions based on libusb-0.1
+ * 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 <config.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <features.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "fpusb.h"
+#include "fpusbi.h"
+
+static struct list_head usb_devs;
+struct list_head open_devs;
+
+static int parse_descriptor(unsigned char *source, char *descriptor, void *dest)
+{
+ unsigned char *sp = source, *dp = dest;
+ uint16_t w;
+ uint32_t d;
+ 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 */
+ w = (sp[1] << 8) | sp[0]; sp += 2;
+ dp += ((unsigned long)dp & 1); /* Align to word boundary */
+ *((uint16_t *)dp) = w; dp += 2;
+ break;
+ case 'd': /* 32-bit dword, convert from little endian to CPU */
+ d = (sp[3] << 24) | (sp[2] << 16) | (sp[1] << 8) | sp[0]; sp += 4;
+ dp += ((unsigned long)dp & 2); /* Align to dword boundary */
+ *((uint32_t *)dp) = d; dp += 4;
+ break;
+ case 'W': /* 16-bit word, keep CPU endianess */
+ dp += ((unsigned long)dp & 1); /* Align to word boundary */
+ memcpy(dp, sp, 2); sp += 2; dp += 2;
+ break;
+ case 'D': /* 32-bit dword, keep CPU endianess */
+ dp += ((unsigned long)dp & 2); /* Align to dword boundary */
+ memcpy(dp, sp, 4); sp += 4; dp += 4;
+ break;
+ }
+ }
+
+ return sp - source;
+}
+
+static int scan_device(char *busdir, const char *devnum)
+{
+ char path[PATH_MAX + 1];
+ unsigned char raw_desc[DEVICE_DESC_LENGTH];
+ struct fpusb_dev *dev = malloc(sizeof(*dev));
+ int fd;
+ int r;
+
+ snprintf(path, PATH_MAX, "%s/%s", busdir, devnum);
+ fp_dbg("%s", path);
+ fd = open(path, O_RDWR);
+ if (!fd) {
+ fp_dbg("open '%s' failed, ret=%d errno=%d", path, fd, errno);
+ return -1;
+ }
+
+ r = read(fd, raw_desc, DEVICE_DESC_LENGTH);
+ if (r < 0) {
+ fp_err("read failed ret=%d errno=%d", r, errno);
+ return r;
+ }
+ /* FIXME: short read handling? */
+
+ parse_descriptor(raw_desc, "bbWbbbbWWWbbbb", &dev->desc);
+ fp_dbg("found device %04x:%04x", dev->desc.idVendor, dev->desc.idProduct);
+ dev->nodepath = strdup(path);
+ list_add(&dev->list, &usb_devs);
+
+ close(fd);
+ return 0;
+}
+
+static int scan_busdir(const char *busnum)
+{
+ DIR *dir;
+ char dirpath[PATH_MAX + 1];
+ struct dirent *entry;
+
+ snprintf(dirpath, PATH_MAX, "%s/%s", USBFS_PATH, busnum);
+ fp_dbg("%s", dirpath);
+ dir = opendir(dirpath);
+ if (!dir) {
+ fp_err("opendir '%s' failed, errno=%d", dirpath, errno);
+ return -1;
+ }
+
+ while (entry = readdir(dir)) {
+ if (entry->d_name[0] == '.')
+ continue;
+ /* deliberately ignoring errors due to valid unplug race conditions */
+ scan_device(dirpath, entry->d_name);
+ }
+
+ return 0;
+}
+
+API_EXPORTED int fpusb_find_devices(void)
+{
+ DIR *busses;
+ struct dirent *entry;
+ fp_dbg("");
+
+ busses = opendir(USBFS_PATH);
+ if (!busses) {
+ fp_err("opendir busses failed errno=%d", errno);
+ return -1;
+ }
+
+ while (entry = readdir(busses)) {
+ if (entry->d_name[0] == '.')
+ continue;
+ /* deliberately ignoring errors, valid race conditions exist
+ * e.g. unplugging of hubs in the middle of this loop*/
+ scan_busdir(entry->d_name);
+ }
+
+ return 0;
+}
+
+API_EXPORTED struct fpusb_dev *fpusb_get_devices(void)
+{
+ if (list_empty(&usb_devs))
+ return NULL;
+ return list_entry(usb_devs.next, struct fpusb_dev, list);
+}
+
+API_EXPORTED struct fpusb_dev *fpusb_dev_next(struct fpusb_dev *dev)
+{
+ struct list_head *head = &dev->list;
+ if (!head || head->next == &usb_devs)
+ return NULL;
+ return list_entry(head->next, struct fpusb_dev, list);
+}
+
+API_EXPORTED struct usb_dev_descriptor *fpusb_dev_get_descriptor(
+ struct fpusb_dev *dev)
+{
+ return &dev->desc;
+}
+
+API_EXPORTED struct fpusb_dev_handle *fpusb_dev_open(struct fpusb_dev *dev)
+{
+ struct fpusb_dev_handle *devh;
+ int fd;
+ fp_dbg("open %04x:%04x", dev->desc.idVendor, dev->desc.idProduct);
+
+ fd = open(dev->nodepath, O_RDWR);
+ if (!fd) {
+ fp_err("open failed, code %d errno %d", fd, errno);
+ return NULL;
+ }
+
+ devh = malloc(sizeof(*devh));
+ if (!devh) {
+ close(fd);
+ return NULL;
+ }
+
+ devh->fd = fd;
+ devh->dev = dev;
+ list_add(&devh->list, &open_devs);
+ return devh;
+}
+
+static void do_close(struct fpusb_dev_handle *devh)
+{
+ close(devh->fd);
+}
+
+API_EXPORTED void fpusb_dev_close(struct fpusb_dev_handle *devh)
+{
+ if (!devh)
+ return;
+ fp_dbg("");
+
+ list_del(&devh->list);
+ do_close(devh);
+ free(devh);
+}
+
+API_EXPORTED int fpusb_dev_claim_intf(struct fpusb_dev_handle *dev,
+ int iface)
+{
+ int r;
+ fp_dbg("interface %d", iface);
+
+ r = ioctl(dev->fd, IOCTL_USB_CLAIMINTF, &iface);
+ if (r < 0)
+ fp_err("claim interface failed, error %d", r);
+ return r;
+}
+
+API_EXPORTED int fpusb_dev_release_intf(struct fpusb_dev_handle *dev,
+ int iface)
+{
+ int r;
+ fp_dbg("interface %d", iface);
+
+ r = ioctl(dev->fd, IOCTL_USB_RELEASEINTF, &iface);
+ if (r < 0)
+ fp_err("release interface failed, error %d", r);
+ return r;
+}
+
+API_EXPORTED int fpusb_init(int signum)
+{
+ /* FIXME: find correct usb node path */
+ fp_dbg("");
+ list_init(&usb_devs);
+ list_init(&open_devs);
+ return fpi_io_init(signum);
+}
+
+API_EXPORTED void fpusb_exit(void)
+{
+ struct fpusb_dev_handle *devh;
+ fp_dbg("");
+ if (!list_empty(&open_devs)) {
+ fp_dbg("naughty app left some devices open!\n");
+ list_for_each_entry(devh, &open_devs, list)
+ do_close(devh);
+ }
+ fpi_io_exit();
+}
+
+void fpi_log(enum fpi_log_level level, const char *function,
+ const char *format, ...)
+{
+ va_list args;
+ FILE *stream = stdout;
+ const char *prefix;
+
+ switch (level) {
+ case LOG_LEVEL_INFO:
+ prefix = "info";
+ break;
+ case LOG_LEVEL_WARNING:
+ stream = stderr;
+ prefix = "warning";
+ break;
+ case LOG_LEVEL_ERROR:
+ stream = stderr;
+ prefix = "error";
+ break;
+ case LOG_LEVEL_DEBUG:
+ stream = stderr;
+ prefix = "debug";
+ break;
+ default:
+ stream = stderr;
+ prefix = "unknown";
+ break;
+ }
+
+ fprintf(stream, "fpusb:%s [%s] ", prefix, function);
+
+ va_start (args, format);
+ vfprintf(stream, format, args);
+ va_end (args);
+
+ fprintf(stream, "\n");
+}
+
diff --git a/libfpusb/fpusb.h b/libfpusb/fpusb.h
new file mode 100644
index 0000000..1968507
--- /dev/null
+++ b/libfpusb/fpusb.h
@@ -0,0 +1,199 @@
+/*
+ * Public libfpusb header file
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Portions based on libusb-0.1
+ * 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 __FPUSB_H__
+#define __FPUSB_H__
+
+#include <stdint.h>
+#include <sys/time.h>
+
+/* standard USB stuff */
+
+/* Device and/or Interface Class codes */
+#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
+#define USB_CLASS_AUDIO 1
+#define USB_CLASS_COMM 2
+#define USB_CLASS_HID 3
+#define USB_CLASS_PRINTER 7
+#define USB_CLASS_PTP 6
+#define USB_CLASS_MASS_STORAGE 8
+#define USB_CLASS_HUB 9
+#define USB_CLASS_DATA 10
+#define USB_CLASS_VENDOR_SPEC 0xff
+
+/* Descriptor types */
+#define USB_DT_DEVICE 0x01
+#define USB_DT_CONFIG 0x02
+#define USB_DT_STRING 0x03
+#define USB_DT_INTERFACE 0x04
+#define USB_DT_ENDPOINT 0x05
+#define USB_DT_HID 0x21
+#define USB_DT_REPORT 0x22
+#define USB_DT_PHYSICAL 0x23
+#define USB_DT_HUB 0x29
+
+/* Descriptor sizes per descriptor type */
+#define USB_DT_DEVICE_SIZE 18
+#define USB_DT_CONFIG_SIZE 9
+#define USB_DT_INTERFACE_SIZE 9
+#define USB_DT_ENDPOINT_SIZE 7
+#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */
+#define USB_DT_HUB_NONVAR_SIZE 7
+
+#define USB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */
+#define USB_ENDPOINT_DIR_MASK 0x80
+
+#define USB_ENDPOINT_IN 0x80
+#define USB_ENDPOINT_OUT 0x00
+
+#define USB_ENDPOINT_TYPE_MASK 0x03 /* in bmAttributes */
+#define USB_ENDPOINT_TYPE_CONTROL 0
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
+#define USB_ENDPOINT_TYPE_BULK 2
+#define USB_ENDPOINT_TYPE_INTERRUPT 3
+
+/* Standard requests */
+#define USB_REQ_GET_STATUS 0x00
+#define USB_REQ_CLEAR_FEATURE 0x01
+/* 0x02 is reserved */
+#define USB_REQ_SET_FEATURE 0x03
+/* 0x04 is reserved */
+#define USB_REQ_SET_ADDRESS 0x05
+#define USB_REQ_GET_DESCRIPTOR 0x06
+#define USB_REQ_SET_DESCRIPTOR 0x07
+#define USB_REQ_GET_CONFIGURATION 0x08
+#define USB_REQ_SET_CONFIGURATION 0x09
+#define USB_REQ_GET_INTERFACE 0x0A
+#define USB_REQ_SET_INTERFACE 0x0B
+#define USB_REQ_SYNCH_FRAME 0x0C
+
+#define USB_TYPE_STANDARD (0x00 << 5)
+#define USB_TYPE_CLASS (0x01 << 5)
+#define USB_TYPE_VENDOR (0x02 << 5)
+#define USB_TYPE_RESERVED (0x03 << 5)
+
+#define USB_RECIP_DEVICE 0x00
+#define USB_RECIP_INTERFACE 0x01
+#define USB_RECIP_ENDPOINT 0x02
+#define USB_RECIP_OTHER 0x03
+
+struct usb_dev_descriptor {
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t bcdUSB;
+ uint8_t bDeviceClass;
+ uint8_t bDeviceSubClass;
+ uint8_t bDeviceProtocol;
+ uint8_t bMaxPacketSize0;
+ uint16_t idVendor;
+ uint16_t idProduct;
+ uint16_t bcdDevice;
+ uint8_t iManufacturer;
+ uint8_t iProduct;
+ uint8_t iSerialNumber;
+ uint8_t bNumConfigurations;
+};
+
+/* fpusb */
+
+struct fpusb_dev;
+typedef struct fpusb_dev fpusb_dev;
+
+struct fpusb_dev_handle;
+typedef struct fpusb_dev_handle fpusb_dev_handle;
+
+struct fpusb_urb_handle;
+typedef struct fpusb_urb_handle fpusb_urb_handle;
+
+enum fp_urb_cb_status {
+ FP_URB_SILENT_COMPLETION = 0,
+ FP_URB_COMPLETED,
+ FP_URB_TIMEOUT,
+ FP_URB_CANCELLED,
+};
+
+struct fpusb_ctrl_msg {
+ uint8_t requesttype;
+ uint8_t request;
+ uint16_t value;
+ uint16_t index;
+ uint16_t length;
+ unsigned char *data;
+};
+
+typedef void (*fpusb_ctrl_cb_fn)(fpusb_dev_handle *devh, fpusb_urb_handle *urbh,
+ struct fpusb_ctrl_msg *msg, enum fp_urb_cb_status status,
+ unsigned char *data, int actual_length, void *user_data);
+
+struct fpusb_bulk_msg {
+ unsigned char endpoint;
+ unsigned char *data;
+ int length;
+};
+
+typedef void (*fpusb_bulk_cb_fn)(fpusb_dev_handle *devh, fpusb_urb_handle *urbh,
+ struct fpusb_bulk_msg *msg, enum fp_urb_cb_status status,
+ int actual_length, void *user_data);
+
+int fpusb_init(int signum);
+void fpusb_exit(void);
+
+int fpusb_find_devices(void);
+fpusb_dev *fpusb_get_devices(void);
+struct usb_dev_descriptor *fpusb_dev_get_descriptor(fpusb_dev *dev);
+fpusb_dev *fpusb_dev_next(fpusb_dev *dev);
+
+fpusb_dev_handle *fpusb_dev_open(fpusb_dev *dev);
+void fpusb_dev_close(fpusb_dev_handle *devh);
+int fpusb_dev_claim_intf(fpusb_dev_handle *dev, int iface);
+int fpusb_dev_release_intf(fpusb_dev_handle *dev, int iface);
+
+/* async I/O */
+
+fpusb_urb_handle *fpusb_submit_ctrl_msg(fpusb_dev_handle *devh,
+ struct fpusb_ctrl_msg *msg, fpusb_ctrl_cb_fn callback, void *user_data,
+ unsigned int timeout);
+fpusb_urb_handle *fpusb_submit_bulk_msg(fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data,
+ unsigned int timeout);
+fpusb_urb_handle *fpusb_submit_intr_msg(fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data,
+ unsigned int timeout);
+
+int fpusb_urb_handle_cancel(fpusb_dev_handle *devh, fpusb_urb_handle *urbh);
+int fpusb_urb_handle_cancel_sync(fpusb_dev_handle *devh,
+ fpusb_urb_handle *urbh);
+void fpusb_urb_handle_free(fpusb_urb_handle *urbh);
+
+int fpusb_poll_timeout(struct timeval *tv);
+int fpusb_poll(void);
+
+/* sync I/O */
+
+int fpusb_ctrl_msg(fpusb_dev_handle *devh, struct fpusb_ctrl_msg *msg,
+ unsigned int timeout);
+int fpusb_bulk_msg(fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
+ int *transferred, unsigned int timeout);
+int fpusb_intr_msg(fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
+ int *transferred, unsigned int timeout);
+
+#endif
diff --git a/libfpusb/fpusbi.h b/libfpusb/fpusbi.h
new file mode 100644
index 0000000..b4287f4
--- /dev/null
+++ b/libfpusb/fpusbi.h
@@ -0,0 +1,200 @@
+/*
+ * Internal header for libfpusb
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Portions based on libusb-0.1
+ * 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 __FPUSBI_H__
+#define __FPUSBI_H__
+
+#include <config.h>
+
+#include <endian.h>
+#include <stddef.h>
+#include <sys/ioctl.h>
+#include <time.h>
+
+#include <fpusb.h>
+#include <usbfs.h>
+
+#define USBFS_PATH "/dev/bus/usb"
+#define DEVICE_DESC_LENGTH 18
+
+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 *)((char *)(ptr) - (unsigned long)(&((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"
+ */
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), 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 bswap16(x) (((x & 0xff) << 8) | (x >> 8))
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define cpu_to_le16(x) (x)
+#define le16_to_cpu(x) (x)
+#elif __BYTE_ORDER == __BIG_ENDIAN
+#define le16_to_cpu(x) bswap16(x)
+#define cpu_to_le16(x) bswap16(x)
+#else
+#error "Unrecognized endianness"
+#endif
+
+#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 fpi_log_level {
+ LOG_LEVEL_DEBUG,
+ LOG_LEVEL_INFO,
+ LOG_LEVEL_WARNING,
+ LOG_LEVEL_ERROR,
+};
+
+void fpi_log(enum fpi_log_level, const char *function, const char *format, ...);
+
+#ifdef ENABLE_LOGGING
+#define _fpi_log(level, fmt...) fpi_log(level, __FUNCTION__, fmt)
+#else
+#define _fpi_log(level, fmt...)
+#endif
+
+#ifdef ENABLE_DEBUG_LOGGING
+#define fp_dbg(fmt...) _fpi_log(LOG_LEVEL_DEBUG, fmt)
+#else
+#define fp_dbg(fmt...)
+#endif
+
+#define fp_info(fmt...) _fpi_log(LOG_LEVEL_INFO, fmt)
+#define fp_warn(fmt...) _fpi_log(LOG_LEVEL_WARNING, fmt)
+#define fp_err(fmt...) _fpi_log(LOG_LEVEL_ERROR, fmt)
+
+struct fpusb_dev {
+ struct list_head list;
+ char *nodepath;
+ struct usb_dev_descriptor desc;
+};
+
+struct fpusb_dev_handle {
+ struct list_head list;
+ struct fpusb_dev *dev;
+ int fd;
+};
+
+enum fpusb_urb_type {
+ FPUSB_URB_CONTROL,
+ FPUSB_URB_BULK,
+};
+
+#define FPUSB_URBH_DATA_BELONGS_TO_USER (1<<0)
+#define FPUSB_URBH_SYNC_CANCELLED (1<<1)
+#define FPUSB_URBH_TIMED_OUT (1<<2)
+
+struct fpusb_urb_handle {
+ struct fpusb_dev_handle *devh;
+ struct usb_urb urb;
+ struct list_head list;
+ struct timespec timeout;
+ timer_t timer;
+ unsigned char urb_type;
+ unsigned char endpoint;
+ void *msg;
+ int transfer_len;
+ int transferred;
+ unsigned char *buffer;
+ void *callback;
+ void *user_data;
+ uint8_t flags;
+};
+
+/* bus structures */
+
+struct usb_ctrl_setup {
+ uint8_t bRequestType;
+ uint8_t bRequest;
+ uint16_t wValue;
+ uint16_t wIndex;
+ uint16_t wLength;
+} __attribute__((packed));
+
+/* shared data and functions */
+
+extern struct list_head open_devs;
+
+int fpi_io_init(int _signum);
+void fpi_io_exit(void);
+
+#endif
+
diff --git a/libfpusb/io.c b/libfpusb/io.c
new file mode 100644
index 0000000..fc802b6
--- /dev/null
+++ b/libfpusb/io.c
@@ -0,0 +1,700 @@
+/*
+ * I/O functions for libfpusb
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Portions based on libusb-0.1
+ * 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 <config.h>
+
+#include <errno.h>
+#include <signal.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <time.h>
+#include <unistd.h>
+
+/* signalfd() support is present in glibc-2.7 onwards, but glibc-2.7 contains
+ * a bug where the header is neither installed or compilable. This will be
+ * fixed for glibc-2.8. */
+#if __GLIBC_PREREQ(2, 8)
+#include <sys/signalfd.h>
+#else
+#include "signalfd.h"
+#endif
+
+#include "fpusbi.h"
+
+static int sigfd;
+static int signum;
+
+/* this is a list of in-flight fp_urb_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. */
+static struct list_head flying_urbs;
+
+static int setup_signalfd(int _signum)
+{
+ sigset_t sigset;
+ if (_signum == 0)
+ _signum = SIGRTMIN;
+ fp_dbg("signal %d", _signum);
+
+ sigemptyset(&sigset);
+ sigaddset(&sigset, _signum);
+ sigfd = signalfd(-1, &sigset, 0);
+ if (sigfd < 0) {
+ fp_err("signalfd failed, code=%d errno=%d", sigfd, errno);
+ return sigfd;
+ }
+ fp_dbg("got signalfd %d", sigfd);
+ signum = _signum;
+
+ sigemptyset(&sigset);
+ sigaddset(&sigset, _signum);
+ return sigprocmask(SIG_BLOCK, &sigset, NULL);
+}
+
+int fpi_io_init(int _signum)
+{
+ list_init(&flying_urbs);
+ return setup_signalfd(signum);
+}
+
+void fpi_io_exit(void)
+{
+ close(sigfd);
+}
+
+static int calculate_timeout(struct fpusb_urb_handle *urbh,
+ unsigned int timeout)
+{
+ int r;
+ struct timespec current_time;
+ struct sigevent sigevt = {
+ .sigev_notify = SIGEV_SIGNAL,
+ .sigev_signo = signum,
+ };
+ struct itimerspec itspec;
+ struct timespec *it_value = &itspec.it_value;
+
+ if (!timeout)
+ return 0;
+
+ r = clock_gettime(CLOCK_MONOTONIC, &current_time);
+ if (r < 0) {
+ fp_err("failed to read monotonic clock, errno=%d", errno);
+ return r;
+ }
+
+ r = timer_create(CLOCK_MONOTONIC, &sigevt, &urbh->timer);
+ if (r < 0) {
+ fp_err("failed to create monotonic timer");
+ return r;
+ }
+
+ memset(&itspec, 0, sizeof(itspec));
+ it_value->tv_sec = current_time.tv_sec + (timeout / 1000);
+ it_value->tv_nsec = current_time.tv_nsec +
+ ((timeout % 1000) * 1000000);
+
+ if (it_value->tv_nsec > 1000000000) {
+ it_value->tv_nsec -= 1000000000;
+ it_value->tv_sec++;
+ }
+
+ r = timer_settime(&urbh->timer, TIMER_ABSTIME, &itspec, NULL);
+ if (r < 0) {
+ fp_err("failed to arm monotonic timer");
+ return r;
+ }
+
+ urbh->timeout = itspec.it_value;
+
+ return 0;
+}
+
+static void add_to_flying_list(struct fpusb_urb_handle *urbh)
+{
+ struct fpusb_urb_handle *cur;
+ struct timespec *timeout = &urbh->timeout;
+
+ /* if we have no other flying urbs, start the list with this one */
+ if (list_empty(&flying_urbs)) {
+ list_add(&urbh->list, &flying_urbs);
+ return;
+ }
+
+ /* if we have infinite timeout, append to end of list */
+ if (!TIMESPEC_IS_SET(timeout)) {
+ list_add_tail(&urbh->list, &flying_urbs);
+ return;
+ }
+
+ /* otherwise, find appropriate place in list */
+ list_for_each_entry(cur, &flying_urbs, list) {
+ /* find first timeout that occurs after the urbh in question */
+ struct timespec *cur_ts = &cur->timeout;
+
+ if (!TIMESPEC_IS_SET(cur_ts) || (cur_ts->tv_sec > timeout->tv_sec) ||
+ (cur_ts->tv_sec == timeout->tv_sec &&
+ cur_ts->tv_nsec > timeout->tv_nsec)) {
+ list_add_tail(&urbh->list, &cur->list);
+ return;
+ }
+ }
+
+ /* otherwise we need to be inserted at the end */
+ list_add_tail(&urbh->list, &flying_urbs);
+}
+
+static int submit_urb(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh)
+{
+ int r;
+ struct usb_urb *urb = &urbh->urb;
+ int to_be_transferred = urbh->transfer_len - urbh->transferred;
+
+ urb->type = urbh->urb_type;
+ urb->endpoint = urbh->endpoint;
+ urb->buffer = urbh->buffer + urbh->transferred;
+ urb->buffer_length = MIN(to_be_transferred, MAX_URB_BUFFER_LENGTH);
+ urb->signr = signum;
+
+ /* FIXME: for requests that we have to split into multiple URBs, we should
+ * submit all the URBs instantly: submit, submit, submit, reap, reap, reap
+ * rather than: submit, reap, submit, reap, submit, reap
+ * this will improve performance and fix bugs concerning behaviour when
+ * the user submits two similar multiple-urb requests */
+ fp_dbg("transferring %d from %d bytes", urb->buffer_length,
+ to_be_transferred);
+
+ r = ioctl(devh->fd, IOCTL_USB_SUBMITURB, &urbh->urb);
+ if (r < 0) {
+ fp_err("submiturb failed error %d errno=%d", r, errno);
+ return r;
+ }
+
+ add_to_flying_list(urbh);
+ return 0;
+}
+
+API_EXPORTED struct fpusb_urb_handle *fpusb_submit_ctrl_msg(
+ struct fpusb_dev_handle *devh, struct fpusb_ctrl_msg *msg,
+ fpusb_ctrl_cb_fn callback, void *user_data, unsigned int timeout)
+{
+ struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh));
+ struct usb_ctrl_setup *setup;
+ unsigned char *urbdata;
+ int urbdata_length = sizeof(struct usb_ctrl_setup) + msg->length;
+ int r;
+
+ if (!urbh)
+ return NULL;
+ memset(urbh, 0, sizeof(*urbh));
+ urbh->devh = devh;
+ urbh->callback = callback;
+ urbh->user_data = user_data;
+ r = calculate_timeout(urbh, timeout);
+ if (r < 0) {
+ free(urbh);
+ return NULL;
+ }
+
+ urbdata = malloc(urbdata_length);
+ if (!urbdata) {
+ free(urbh);
+ return NULL;
+ }
+
+ fp_dbg("RQT=%02x RQ=%02x VAL=%04x IDX=%04x length=%d",
+ msg->requesttype, msg->request, msg->value, msg->index, msg->length);
+
+ setup = (struct usb_ctrl_setup *) urbdata;
+ setup->bRequestType = msg->requesttype;
+ setup->bRequest = msg->request;
+ setup->wValue = cpu_to_le16(msg->value);
+ setup->wIndex = cpu_to_le16(msg->index);
+ setup->wLength = cpu_to_le16(msg->length);
+
+ if ((msg->requesttype & 0x80) == USB_ENDPOINT_OUT)
+ memcpy(urbdata + sizeof(struct usb_ctrl_setup), msg->data, msg->length);
+
+ urbh->urb_type = USB_URB_TYPE_CONTROL;
+ urbh->msg = msg;
+ urbh->buffer = urbdata;
+ urbh->transfer_len = urbdata_length;
+
+ r = submit_urb(devh, urbh);
+ if (r < 0) {
+ free(urbh);
+ free(urbdata);
+ return NULL;
+ }
+
+ return urbh;
+}
+
+static struct fpusb_urb_handle *submit_bulk_msg(struct fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, fpusb_bulk_cb_fn callback, void *user_data,
+ unsigned int timeout, unsigned char urbtype)
+{
+ struct fpusb_urb_handle *urbh = malloc(sizeof(*urbh));
+ int r;
+
+ fp_dbg("length %d timeout %d", msg->length, timeout);
+
+ if (!urbh)
+ return NULL;
+ memset(urbh, 0, sizeof(*urbh));
+ r = calculate_timeout(urbh, timeout);
+ if (r < 0) {
+ free(urbh);
+ return NULL;
+ }
+ urbh->devh = devh;
+ urbh->callback = callback;
+ urbh->user_data = user_data;
+ urbh->flags |= FPUSB_URBH_DATA_BELONGS_TO_USER;
+ urbh->msg = msg;
+ urbh->endpoint = msg->endpoint;
+ urbh->urb_type = urbtype;
+ urbh->buffer = msg->data;
+ urbh->transfer_len = msg->length;
+
+ r = submit_urb(devh, urbh);
+ if (r < 0) {
+ free(urbh);
+ return NULL;
+ }
+
+ return urbh;
+}
+
+API_EXPORTED struct fpusb_urb_handle *fpusb_submit_bulk_msg(
+ struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
+ fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
+{
+ return submit_bulk_msg(devh, msg, callback, user_data, timeout,
+ USB_URB_TYPE_BULK);
+}
+
+API_EXPORTED struct fpusb_urb_handle *fpusb_submit_intr_msg(
+ struct fpusb_dev_handle *devh, struct fpusb_bulk_msg *msg,
+ fpusb_bulk_cb_fn callback, void *user_data, unsigned int timeout)
+{
+ return submit_bulk_msg(devh, msg, callback, user_data, timeout,
+ USB_URB_TYPE_INTERRUPT);
+}
+
+API_EXPORTED int fpusb_urb_handle_cancel(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh)
+{
+ int r;
+ fp_dbg("");
+ r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
+ if (r < 0)
+ fp_err("cancel urb failed error %d", r);
+ return r;
+}
+
+API_EXPORTED int fpusb_urb_handle_cancel_sync(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh)
+{
+ int r;
+ fp_dbg("");
+ r = ioctl(devh->fd, IOCTL_USB_DISCARDURB, &urbh->urb);
+ if (r < 0) {
+ fp_err("cancel urb failed error %d", r);
+ return r;
+ }
+
+ urbh->flags |= FPUSB_URBH_SYNC_CANCELLED;
+ while (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) {
+ r = fpusb_poll();
+ if (r < 0)
+ return r;
+ }
+
+ return 0;
+}
+
+int handle_transfer_completion(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh, enum fp_urb_cb_status status)
+{
+ struct usb_urb *urb = &urbh->urb;
+
+ if (TIMESPEC_IS_SET(&urbh->timeout))
+ timer_delete(urbh->timer);
+
+ if (status == FP_URB_SILENT_COMPLETION)
+ return 0;
+
+ if (urb->type == USB_URB_TYPE_CONTROL) {
+ fpusb_ctrl_cb_fn callback = urbh->callback;
+ if (callback)
+ callback(devh, urbh, urbh->msg, status,
+ urb->buffer + sizeof(struct usb_ctrl_setup), urbh->transferred,
+ urbh->user_data);
+ } else if (urb->type == USB_URB_TYPE_BULK ||
+ urb->type == USB_URB_TYPE_INTERRUPT) {
+ fpusb_bulk_cb_fn callback = urbh->callback;
+ if (callback)
+ callback(devh, urbh, urbh->msg, status, urbh->transferred,
+ urbh->user_data);
+ }
+ return 0;
+}
+
+static int handle_transfer_cancellation(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh)
+{
+ /* if the URB is being cancelled synchronously, raise cancellation
+ * completion event by unsetting flag, and ensure that user callback does
+ * not get called.
+ */
+ if (urbh->flags & FPUSB_URBH_SYNC_CANCELLED) {
+ urbh->flags &= ~FPUSB_URBH_SYNC_CANCELLED;
+ fp_dbg("detected sync. cancel");
+ return handle_transfer_completion(devh, urbh, FP_URB_SILENT_COMPLETION);
+ }
+
+ /* if the URB was cancelled due to timeout, report timeout to the user */
+ if (urbh->flags & FPUSB_URBH_TIMED_OUT) {
+ fp_dbg("detected timeout cancellation");
+ return handle_transfer_completion(devh, urbh, FP_URB_TIMEOUT);
+ }
+
+ /* otherwise its a normal async cancel */
+ return handle_transfer_completion(devh, urbh, FP_URB_CANCELLED);
+}
+
+static int reap_for_devh(struct fpusb_dev_handle *devh)
+{
+ int r;
+ struct usb_urb *urb;
+ struct fpusb_urb_handle *urbh;
+ int trf_requested;
+
+ r = ioctl(devh->fd, IOCTL_USB_REAPURBNDELAY, &urb);
+ if (r == -1 && errno == EAGAIN)
+ return r;
+ if (r < 0) {
+ fp_err("reap failed error %d errno=%d", r, errno);
+ return r;
+ }
+
+ urbh = container_of(urb, struct fpusb_urb_handle, urb);
+
+ fp_dbg("urb type=%d status=%d transferred=%d", urb->type, urb->status,
+ urb->actual_length);
+ list_del(&urbh->list);
+
+ if (urb->status == -2)
+ return handle_transfer_cancellation(devh, urbh);
+ /* FIXME: research what other status codes may exist */
+ if (urb->status != 0)
+ fp_warn("unrecognised urb status %d", urb->status);
+
+ /* determine how much data was asked for */
+ trf_requested = MIN(urbh->transfer_len - urbh->transferred,
+ MAX_URB_BUFFER_LENGTH);
+
+ urbh->transferred += urb->actual_length;
+
+ /* if we were provided less data than requested, then our transfer is
+ * done */
+ if (urb->actual_length < trf_requested) {
+ fp_dbg("less data than requested (%d/%d) --> all done",
+ urb->actual_length, trf_requested);
+ return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
+ }
+
+ /* if we've transferred all data, we're done */
+ if (urbh->transferred == urbh->transfer_len) {
+ fp_dbg("transfer complete --> all done");
+ return handle_transfer_completion(devh, urbh, FP_URB_COMPLETED);
+ }
+
+ /* otherwise, we have more data to transfer */
+ fp_dbg("more data to transfer...");
+ memset(urb, 0, sizeof(*urb));
+ return submit_urb(devh, urbh);
+}
+
+static void handle_timeout(struct fpusb_urb_handle *urbh)
+{
+ /* handling timeouts is tricky, as we may race with the kernel: we may
+ * detect a timeout racing with the condition that the urb has actually
+ * completed. we asynchronously cancel the URB and report timeout
+ * to the user when the URB cancellation completes (or not at all if the
+ * URB actually gets delivered as per this race) */
+ int r;
+
+
+ urbh->flags |= FPUSB_URBH_TIMED_OUT;
+ r = fpusb_urb_handle_cancel(urbh->devh, urbh);
+ if (r < 0)
+ fp_warn("async cancel failed %d errno=%d", r, errno);
+}
+
+static int handle_timeouts(void)
+{
+ struct timespec systime;
+ struct fpusb_urb_handle *urbh;
+ int r;
+
+ if (list_empty(&flying_urbs))
+ return 0;
+
+ /* get current time */
+ r = clock_gettime(CLOCK_MONOTONIC, &systime);
+ if (r < 0)
+ return r;
+
+ /* iterate through flying urbs list, finding all urbs that have expired
+ * timeouts */
+ list_for_each_entry(urbh, &flying_urbs, list) {
+ struct timespec *cur_ts = &urbh->timeout;
+
+ /* if we've reached urbs of infinite timeout, we're all done */
+ if (!TIMESPEC_IS_SET(cur_ts))
+ return 0;
+
+ /* if urb has non-expired timeout, nothing more to do */
+ if ((cur_ts->tv_sec > systime.tv_sec) ||
+ (cur_ts->tv_sec == systime.tv_sec &&
+ cur_ts->tv_nsec > systime.tv_nsec))
+ return 0;
+
+ /* otherwise, we've got an expired timeout to handle */
+ handle_timeout(urbh);
+ }
+
+ return 0;
+}
+
+static int reap(void)
+{
+ struct fpusb_dev_handle *devh;
+ int r;
+
+ list_for_each_entry(devh, &open_devs, list) {
+ r = reap_for_devh(devh);
+ if (r == -1 && errno == EAGAIN)
+ continue;
+ if (r < 0)
+ return r;
+ }
+
+ r = handle_timeouts();
+
+ return 0;
+}
+
+static int flush_sigfd(void)
+{
+ int r;
+ struct signalfd_siginfo siginfo;
+ r = read(sigfd, &siginfo, sizeof(siginfo));
+ if (r < 0) {
+ fp_err("sigfd read failed %d %d", r, errno);
+ return r;
+ }
+ if ((unsigned int) r < sizeof(siginfo)) {
+ fp_err("sigfd short read (%d/%d)", r, sizeof(siginfo));
+ return -1;
+ }
+ return 0;
+}
+
+static int poll_io(struct timeval *tv)
+{
+ int r;
+ fd_set fds;
+
+ FD_ZERO(&fds);
+ FD_SET(sigfd, &fds);
+ r = select(sigfd + 1, &fds, NULL, NULL, tv);
+ if (r == -1 && errno == EINTR)
+ return 0;
+ if (r < 0) {
+ fp_err("select failed %d err=%d\n", r, errno);
+ return r;
+ }
+
+ if (r > 0) {
+ flush_sigfd();
+ return reap();
+ }
+
+ return 0;
+}
+
+API_EXPORTED int fpusb_poll_timeout(struct timeval *tv)
+{
+ return poll_io(tv);
+}
+
+API_EXPORTED int fpusb_poll(void)
+{
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = 500000;
+ return poll_io(&tv);
+}
+
+struct sync_ctrl_handle {
+ enum fp_urb_cb_status status;
+ unsigned char *data;
+ int actual_length;
+};
+
+static void ctrl_msg_cb(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh, struct fpusb_ctrl_msg *msg,
+ enum fp_urb_cb_status status, unsigned char *data, int actual_length,
+ void *user_data)
+{
+ struct sync_ctrl_handle *ctrlh = (struct sync_ctrl_handle *) user_data;
+ fp_dbg("actual_length=%d", actual_length);
+
+ if (status == FP_URB_COMPLETED) {
+ /* copy results into user-defined buffer */
+ if (msg->requesttype & USB_ENDPOINT_IN)
+ memcpy(ctrlh->data, data, actual_length);
+ }
+
+ ctrlh->status = status;
+ ctrlh->actual_length = actual_length;
+ /* caller frees urbh */
+}
+
+API_EXPORTED int fpusb_ctrl_msg(struct fpusb_dev_handle *devh,
+ struct fpusb_ctrl_msg *msg, unsigned int timeout)
+{
+ struct fpusb_urb_handle *urbh;
+ struct sync_ctrl_handle ctrlh;
+
+ memset(&ctrlh, 0, sizeof(ctrlh));
+ ctrlh.data = msg->data;
+
+ urbh = fpusb_submit_ctrl_msg(devh, msg, ctrl_msg_cb, &ctrlh, timeout);
+ if (!urbh)
+ return -1;
+
+ while (!ctrlh.status) {
+ int r = fpusb_poll();
+ if (r < 0) {
+ fpusb_urb_handle_cancel_sync(devh, urbh);
+ fpusb_urb_handle_free(urbh);
+ return r;
+ }
+ }
+
+ fpusb_urb_handle_free(urbh);
+ switch (ctrlh.status) {
+ case FP_URB_COMPLETED:
+ return ctrlh.actual_length;
+ case FP_URB_TIMEOUT:
+ return -ETIMEDOUT;
+ default:
+ fp_warn("unrecognised status code %d", ctrlh.status);
+ return -1;
+ }
+}
+
+struct sync_bulk_handle {
+ enum fp_urb_cb_status status;
+ int actual_length;
+};
+
+static void bulk_msg_cb(struct fpusb_dev_handle *devh,
+ struct fpusb_urb_handle *urbh, struct fpusb_bulk_msg *msg,
+ enum fp_urb_cb_status status, int actual_length, void *user_data)
+{
+ struct sync_bulk_handle *bulkh = (struct sync_bulk_handle *) user_data;
+ fp_dbg("");
+ bulkh->status = status;
+ bulkh->actual_length = actual_length;
+ /* caller frees urbh */
+}
+
+static int do_sync_bulk_msg(struct fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout,
+ unsigned char urbtype)
+{
+ struct fpusb_urb_handle *urbh;
+ struct sync_bulk_handle bulkh;
+
+ memset(&bulkh, 0, sizeof(bulkh));
+
+ urbh = submit_bulk_msg(devh, msg, bulk_msg_cb, &bulkh, timeout, urbtype);
+ if (!urbh)
+ return -1;
+
+ while (!bulkh.status) {
+ int r = fpusb_poll();
+ if (r < 0) {
+ fpusb_urb_handle_cancel_sync(devh, urbh);
+ fpusb_urb_handle_free(urbh);
+ return r;
+ }
+ }
+
+ *transferred = bulkh.actual_length;
+ fpusb_urb_handle_free(urbh);
+
+ switch (bulkh.status) {
+ case FP_URB_COMPLETED:
+ return 0;
+ case FP_URB_TIMEOUT:
+ return -ETIMEDOUT;
+ default:
+ fp_warn("unrecognised status code %d", bulkh.status);
+ return -1;
+ }
+}
+
+API_EXPORTED int fpusb_intr_msg(struct fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout)
+{
+ return do_sync_bulk_msg(devh, msg, transferred, timeout,
+ USB_URB_TYPE_INTERRUPT);
+}
+
+API_EXPORTED int fpusb_bulk_msg(struct fpusb_dev_handle *devh,
+ struct fpusb_bulk_msg *msg, int *transferred, unsigned int timeout)
+{
+ return do_sync_bulk_msg(devh, msg, transferred, timeout,
+ USB_URB_TYPE_BULK);
+}
+
+API_EXPORTED void fpusb_urb_handle_free(struct fpusb_urb_handle *urbh)
+{
+ if (!urbh)
+ return;
+
+ if (!(urbh->flags & FPUSB_URBH_DATA_BELONGS_TO_USER))
+ free(urbh->urb.buffer);
+ free(urbh);
+}
+
diff --git a/libfpusb/signalfd.h b/libfpusb/signalfd.h
new file mode 100644
index 0000000..829bff1
--- /dev/null
+++ b/libfpusb/signalfd.h
@@ -0,0 +1,76 @@
+/*
+ * signalfd header
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Based on glibc header
+ * Copyright (C) 2007 Free Software Foundation, Inc.
+ *
+ * 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 __FPUSB_SIGNALFD_H__
+#define __FPUSB_SIGNALFD_H__
+
+/* FIXME: in future, remove this and unconditionally use glibc directly when
+ * glibc-2.8 is widespread */
+
+#include <signal.h>
+#include <stdint.h>
+
+#ifdef __i386__
+#define __NR_signalfd 321
+#elif defined(__x86_64__)
+#define __NR_signalfd 282
+#else
+#error "signalfd unsupported on this architecture"
+#endif
+
+/* signalfd() implementation was added as of glibc-2.7 */
+#if __GLIBC_PREREQ(2, 7)
+int signalfd(int fd, const sigset_t *mask, int flags);
+#else
+#include <sys/syscall.h>
+
+#define SIZEOF_SIG (_NSIG / 8)
+#define SIZEOF_SIGSET (SIZEOF_SIG > sizeof(sigset_t) ? sizeof(sigset_t): SIZEOF_SIG)
+
+static inline int signalfd(int fd, const sigset_t *mask, int flags)
+{
+ return syscall(__NR_signalfd, fd, mask, SIZEOF_SIGSET);
+}
+#endif
+
+struct signalfd_siginfo {
+ uint32_t ssi_signo;
+ int32_t ssi_errno;
+ int32_t ssi_code;
+ uint32_t ssi_pid;
+ uint32_t ssi_uid;
+ int32_t ssi_fd;
+ uint32_t ssi_tid;
+ uint32_t ssi_band;
+ uint32_t ssi_overrun;
+ uint32_t ssi_trapno;
+ int32_t ssi_status;
+ int32_t ssi_int;
+ uintptr_t ssi_ptr;
+ uint64_t ssi_utime;
+ uint64_t ssi_stime;
+ uint64_t ssi_addr;
+ uint8_t __pad[48];
+};
+
+#endif
+
diff --git a/libfpusb/usbfs.h b/libfpusb/usbfs.h
new file mode 100644
index 0000000..0a2204a
--- /dev/null
+++ b/libfpusb/usbfs.h
@@ -0,0 +1,135 @@
+/*
+ * usbfs header structures
+ * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
+ *
+ * Portions based on libusb-0.1
+ * 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 __FPUSB_USBFS_H__
+#define __FPUSB_USBFS_H__
+
+struct usb_ctrltransfer {
+ /* keep in sync with usbdevice_fs.h:usbdevfs_ctrltransfer */
+ uint8_t bRequestType;
+ uint8_t bRequest;
+ uint16_t wValue;
+ uint16_t wIndex;
+ uint16_t wLength;
+
+ uint32_t timeout; /* in milliseconds */
+
+ /* pointer to data */
+ void *data;
+};
+
+struct usb_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 usb_setinterface {
+ /* keep in sync with usbdevice_fs.h:usbdevfs_setinterface */
+ unsigned int interface;
+ unsigned int altsetting;
+};
+
+#define USB_MAXDRIVERNAME 255
+
+struct usb_getdriver {
+ unsigned int interface;
+ char driver[USB_MAXDRIVERNAME + 1];
+};
+
+#define USB_URB_DISABLE_SPD 1
+#define USB_URB_ISO_ASAP 2
+#define USB_URB_QUEUE_BULK 0x10
+
+enum usb_urb_type {
+ USB_URB_TYPE_ISO = 0,
+ USB_URB_TYPE_INTERRUPT = 1,
+ USB_URB_TYPE_CONTROL = 2,
+ USB_URB_TYPE_BULK = 3,
+};
+
+struct usb_iso_packet_desc {
+ unsigned int length;
+ unsigned int actual_length;
+ unsigned int status;
+};
+
+#define MAX_URB_BUFFER_LENGTH 16384
+
+struct usb_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 usb_iso_packet_desc iso_frame_desc[0];
+};
+
+struct usb_connectinfo {
+ unsigned int devnum;
+ unsigned char slow;
+};
+
+struct usb_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 usb_hub_portinfo {
+ unsigned char numports;
+ unsigned char port[127]; /* port to device num mapping */
+};
+
+#define IOCTL_USB_CONTROL _IOWR('U', 0, struct usb_ctrltransfer)
+#define IOCTL_USB_BULK _IOWR('U', 2, struct usb_bulktransfer)
+#define IOCTL_USB_RESETEP _IOR('U', 3, unsigned int)
+#define IOCTL_USB_SETINTF _IOR('U', 4, struct usb_setinterface)
+#define IOCTL_USB_SETCONFIG _IOR('U', 5, unsigned int)
+#define IOCTL_USB_GETDRIVER _IOW('U', 8, struct usb_getdriver)
+#define IOCTL_USB_SUBMITURB _IOR('U', 10, struct usb_urb)
+#define IOCTL_USB_DISCARDURB _IO('U', 11)
+#define IOCTL_USB_REAPURB _IOW('U', 12, void *)
+#define IOCTL_USB_REAPURBNDELAY _IOW('U', 13, void *)
+#define IOCTL_USB_CLAIMINTF _IOR('U', 15, unsigned int)
+#define IOCTL_USB_RELEASEINTF _IOR('U', 16, unsigned int)
+#define IOCTL_USB_CONNECTINFO _IOW('U', 17, struct usb_connectinfo)
+#define IOCTL_USB_IOCTL _IOWR('U', 18, struct usb_ioctl)
+#define IOCTL_USB_HUB_PORTINFO _IOR('U', 19, struct usb_hub_portinfo)
+#define IOCTL_USB_RESET _IO('U', 20)
+#define IOCTL_USB_CLEAR_HALT _IOR('U', 21, unsigned int)
+#define IOCTL_USB_DISCONNECT _IO('U', 22)
+#define IOCTL_USB_CONNECT _IO('U', 23)
+
+#endif