1*86b64dcbSAndroid Build Coastguard WorkerPORTING LIBUSB TO OTHER PLATFORMS 2*86b64dcbSAndroid Build Coastguard Worker 3*86b64dcbSAndroid Build Coastguard WorkerIntroduction 4*86b64dcbSAndroid Build Coastguard Worker============ 5*86b64dcbSAndroid Build Coastguard Worker 6*86b64dcbSAndroid Build Coastguard WorkerThis document is aimed at developers wishing to port libusb to unsupported 7*86b64dcbSAndroid Build Coastguard Workerplatforms. I believe the libusb API is OS-independent, so by supporting 8*86b64dcbSAndroid Build Coastguard Workermultiple operating systems we pave the way for cross-platform USB device 9*86b64dcbSAndroid Build Coastguard Workerdrivers. 10*86b64dcbSAndroid Build Coastguard Worker 11*86b64dcbSAndroid Build Coastguard WorkerImplementation-wise, the basic idea is that you provide an interface to 12*86b64dcbSAndroid Build Coastguard Workerlibusb's internal "backend" API, which performs the appropriate operations on 13*86b64dcbSAndroid Build Coastguard Workeryour target platform. 14*86b64dcbSAndroid Build Coastguard Worker 15*86b64dcbSAndroid Build Coastguard WorkerIn terms of USB I/O, your backend provides functionality to submit 16*86b64dcbSAndroid Build Coastguard Workerasynchronous transfers (synchronous transfers are implemented in the higher 17*86b64dcbSAndroid Build Coastguard Workerlayers, based on the async interface). Your backend must also provide 18*86b64dcbSAndroid Build Coastguard Workerfunctionality to cancel those transfers. 19*86b64dcbSAndroid Build Coastguard Worker 20*86b64dcbSAndroid Build Coastguard WorkerYour backend must also provide an event handling function to "reap" ongoing 21*86b64dcbSAndroid Build Coastguard Workertransfers and process their results. 22*86b64dcbSAndroid Build Coastguard Worker 23*86b64dcbSAndroid Build Coastguard WorkerThe backend must also provide standard functions for other USB operations, 24*86b64dcbSAndroid Build Coastguard Workere.g. setting configuration, obtaining descriptors, etc. 25*86b64dcbSAndroid Build Coastguard Worker 26*86b64dcbSAndroid Build Coastguard Worker 27*86b64dcbSAndroid Build Coastguard WorkerFile descriptors for I/O polling 28*86b64dcbSAndroid Build Coastguard Worker================================ 29*86b64dcbSAndroid Build Coastguard Worker 30*86b64dcbSAndroid Build Coastguard WorkerFor libusb to work, your event handling function obviously needs to be called 31*86b64dcbSAndroid Build Coastguard Workerat various points in time. Your backend must provide a set of file descriptors 32*86b64dcbSAndroid Build Coastguard Workerwhich libusb and its users can pass to poll() or select() to determine when 33*86b64dcbSAndroid Build Coastguard Workerit is time to call the event handling function. 34*86b64dcbSAndroid Build Coastguard Worker 35*86b64dcbSAndroid Build Coastguard WorkerOn Linux, this is easy: the usbfs kernel interface exposes a file descriptor 36*86b64dcbSAndroid Build Coastguard Workerwhich can be passed to poll(). If something similar is not true for your 37*86b64dcbSAndroid Build Coastguard Workerplatform, you can emulate this using an internal library thread to reap I/O as 38*86b64dcbSAndroid Build Coastguard Workernecessary, and a pipe() with the main library to raise events. The file 39*86b64dcbSAndroid Build Coastguard Workerdescriptor of the pipe can then be provided to libusb as an event source. 40*86b64dcbSAndroid Build Coastguard Worker 41*86b64dcbSAndroid Build Coastguard Worker 42*86b64dcbSAndroid Build Coastguard WorkerInterface semantics and documentation 43*86b64dcbSAndroid Build Coastguard Worker===================================== 44*86b64dcbSAndroid Build Coastguard Worker 45*86b64dcbSAndroid Build Coastguard WorkerDocumentation of the backend interface can be found in libusbi.h inside the 46*86b64dcbSAndroid Build Coastguard Workerusbi_os_backend structure definition. 47*86b64dcbSAndroid Build Coastguard Worker 48*86b64dcbSAndroid Build Coastguard WorkerYour implementations of these functions will need to call various internal 49*86b64dcbSAndroid Build Coastguard Workerlibusb functions, prefixed with "usbi_". Documentation for these functions 50*86b64dcbSAndroid Build Coastguard Workercan be found in the .c files where they are implemented. 51*86b64dcbSAndroid Build Coastguard Worker 52*86b64dcbSAndroid Build Coastguard WorkerYou probably want to skim over *all* the documentation before starting your 53*86b64dcbSAndroid Build Coastguard Workerimplementation. For example, you probably need to allocate and store private 54*86b64dcbSAndroid Build Coastguard WorkerOS-specific data for device handles, but the documentation for the mechanism 55*86b64dcbSAndroid Build Coastguard Workerfor doing so is probably not the first thing you will see. 56*86b64dcbSAndroid Build Coastguard Worker 57*86b64dcbSAndroid Build Coastguard WorkerThe Linux backend acts as a good example - view it as a reference 58*86b64dcbSAndroid Build Coastguard Workerimplementation which you should try to match the behaviour of. 59*86b64dcbSAndroid Build Coastguard Worker 60*86b64dcbSAndroid Build Coastguard Worker 61*86b64dcbSAndroid Build Coastguard WorkerGetting started 62*86b64dcbSAndroid Build Coastguard Worker=============== 63*86b64dcbSAndroid Build Coastguard Worker 64*86b64dcbSAndroid Build Coastguard Worker1. Modify configure.ac to detect your platform appropriately (see the OS_LINUX 65*86b64dcbSAndroid Build Coastguard Workerstuff for an example). 66*86b64dcbSAndroid Build Coastguard Worker 67*86b64dcbSAndroid Build Coastguard Worker2. Implement your backend in the libusb/os/ directory, modifying 68*86b64dcbSAndroid Build Coastguard Workerlibusb/os/Makefile.am appropriately. 69*86b64dcbSAndroid Build Coastguard Worker 70*86b64dcbSAndroid Build Coastguard Worker3. Add preprocessor logic to the top of libusb/core.c to statically assign the 71*86b64dcbSAndroid Build Coastguard Workerright usbi_backend for your platform. 72*86b64dcbSAndroid Build Coastguard Worker 73*86b64dcbSAndroid Build Coastguard Worker4. Produce and test your implementation. 74*86b64dcbSAndroid Build Coastguard Worker 75*86b64dcbSAndroid Build Coastguard Worker5. Send your implementation to libusb-devel mailing list. 76*86b64dcbSAndroid Build Coastguard Worker 77*86b64dcbSAndroid Build Coastguard Worker 78*86b64dcbSAndroid Build Coastguard WorkerImplementation difficulties? Questions? 79*86b64dcbSAndroid Build Coastguard Worker======================================= 80*86b64dcbSAndroid Build Coastguard Worker 81*86b64dcbSAndroid Build Coastguard WorkerIf you encounter difficulties porting libusb to your platform, please raise 82*86b64dcbSAndroid Build Coastguard Workerthese issues on the libusb-devel mailing list. Where possible and sensible, I 83*86b64dcbSAndroid Build Coastguard Workeram interested in solving problems preventing libusb from operating on other 84*86b64dcbSAndroid Build Coastguard Workerplatforms. 85*86b64dcbSAndroid Build Coastguard Worker 86*86b64dcbSAndroid Build Coastguard WorkerThe libusb-devel mailing list is also a good place to ask questions and 87*86b64dcbSAndroid Build Coastguard Workermake suggestions about the internal API. Hopefully we can produce some 88*86b64dcbSAndroid Build Coastguard Workerbetter documentation based on your questions and other input. 89*86b64dcbSAndroid Build Coastguard Worker 90*86b64dcbSAndroid Build Coastguard WorkerYou are encouraged to get involved in the process; if the library needs 91*86b64dcbSAndroid Build Coastguard Workersome infrastructure additions/modifications to better support your platform, 92*86b64dcbSAndroid Build Coastguard Workeryou are encouraged to make such changes (in cleanly distinct patch 93*86b64dcbSAndroid Build Coastguard Workersubmissions). Even if you do not make such changes yourself, please do raise 94*86b64dcbSAndroid Build Coastguard Workerthe issues on the mailing list at the very minimum. 95