Blame PORTING

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