Blame PORTING

Packit 0a8e54
PORTING LIBUSB TO OTHER PLATFORMS
Packit 0a8e54
Packit 0a8e54
Introduction
Packit 0a8e54
============
Packit 0a8e54
Packit 0a8e54
This document is aimed at developers wishing to port libusb to unsupported
Packit 0a8e54
platforms. I believe the libusb API is OS-independent, so by supporting
Packit 0a8e54
multiple operating systems we pave the way for cross-platform USB device
Packit 0a8e54
drivers.
Packit 0a8e54
Packit 0a8e54
Implementation-wise, the basic idea is that you provide an interface to
Packit 0a8e54
libusb's internal "backend" API, which performs the appropriate operations on
Packit 0a8e54
your target platform.
Packit 0a8e54
Packit 0a8e54
In terms of USB I/O, your backend provides functionality to submit
Packit 0a8e54
asynchronous transfers (synchronous transfers are implemented in the higher
Packit 0a8e54
layers, based on the async interface). Your backend must also provide
Packit 0a8e54
functionality to cancel those transfers.
Packit 0a8e54
Packit 0a8e54
Your backend must also provide an event handling function to "reap" ongoing
Packit 0a8e54
transfers and process their results.
Packit 0a8e54
Packit 0a8e54
The backend must also provide standard functions for other USB operations,
Packit 0a8e54
e.g. setting configuration, obtaining descriptors, etc.
Packit 0a8e54
Packit 0a8e54
Packit 0a8e54
File descriptors for I/O polling
Packit 0a8e54
================================
Packit 0a8e54
Packit 0a8e54
For libusb to work, your event handling function obviously needs to be called
Packit 0a8e54
at various points in time. Your backend must provide a set of file descriptors
Packit 0a8e54
which libusb and its users can pass to poll() or select() to determine when
Packit 0a8e54
it is time to call the event handling function.
Packit 0a8e54
Packit 0a8e54
On Linux, this is easy: the usbfs kernel interface exposes a file descriptor
Packit 0a8e54
which can be passed to poll(). If something similar is not true for your
Packit 0a8e54
platform, you can emulate this using an internal library thread to reap I/O as
Packit 0a8e54
necessary, and a pipe() with the main library to raise events. The file
Packit 0a8e54
descriptor of the pipe can then be provided to libusb as an event source.
Packit 0a8e54
Packit 0a8e54
Packit 0a8e54
Interface semantics and documentation
Packit 0a8e54
=====================================
Packit 0a8e54
Packit 0a8e54
Documentation of the backend interface can be found in libusbi.h inside the
Packit 0a8e54
usbi_os_backend structure definition.
Packit 0a8e54
Packit 0a8e54
Your implementations of these functions will need to call various internal
Packit 0a8e54
libusb functions, prefixed with "usbi_". Documentation for these functions
Packit 0a8e54
can be found in the .c files where they are implemented.
Packit 0a8e54
Packit 0a8e54
You probably want to skim over *all* the documentation before starting your
Packit 0a8e54
implementation. For example, you probably need to allocate and store private
Packit 0a8e54
OS-specific data for device handles, but the documentation for the mechanism
Packit 0a8e54
for doing so is probably not the first thing you will see.
Packit 0a8e54
Packit 0a8e54
The Linux backend acts as a good example - view it as a reference
Packit 0a8e54
implementation which you should try to match the behaviour of.
Packit 0a8e54
Packit 0a8e54
Packit 0a8e54
Getting started
Packit 0a8e54
===============
Packit 0a8e54
Packit 0a8e54
1. Modify configure.ac to detect your platform appropriately (see the OS_LINUX
Packit 0a8e54
stuff for an example).
Packit 0a8e54
Packit 0a8e54
2. Implement your backend in the libusb/os/ directory, modifying
Packit 0a8e54
libusb/os/Makefile.am appropriately.
Packit 0a8e54
Packit 0a8e54
3. Add preprocessor logic to the top of libusb/core.c to statically assign the
Packit 0a8e54
right usbi_backend for your platform.
Packit 0a8e54
Packit 0a8e54
4. Produce and test your implementation.
Packit 0a8e54
Packit 0a8e54
5. Send your implementation to libusb-devel mailing list.
Packit 0a8e54
Packit 0a8e54
Packit 0a8e54
Implementation difficulties? Questions?
Packit 0a8e54
=======================================
Packit 0a8e54
Packit 0a8e54
If you encounter difficulties porting libusb to your platform, please raise
Packit 0a8e54
these issues on the libusb-devel mailing list. Where possible and sensible, I
Packit 0a8e54
am interested in solving problems preventing libusb from operating on other
Packit 0a8e54
platforms.
Packit 0a8e54
Packit 0a8e54
The libusb-devel mailing list is also a good place to ask questions and
Packit 0a8e54
make suggestions about the internal API. Hopefully we can produce some
Packit 0a8e54
better documentation based on your questions and other input.
Packit 0a8e54
Packit 0a8e54
You are encouraged to get involved in the process; if the library needs
Packit 0a8e54
some infrastructure additions/modifications to better support your platform,
Packit 0a8e54
you are encouraged to make such changes (in cleanly distinct patch
Packit 0a8e54
submissions). Even if you do not make such changes yourself, please do raise
Packit 0a8e54
the issues on the mailing list at the very minimum.