1073fd033Sopenharmony_ciPORTING LIBUSB TO OTHER PLATFORMS 2073fd033Sopenharmony_ci 3073fd033Sopenharmony_ciIntroduction 4073fd033Sopenharmony_ci============ 5073fd033Sopenharmony_ci 6073fd033Sopenharmony_ciThis document is aimed at developers wishing to port libusb to unsupported 7073fd033Sopenharmony_ciplatforms. I believe the libusb API is OS-independent, so by supporting 8073fd033Sopenharmony_cimultiple operating systems we pave the way for cross-platform USB device 9073fd033Sopenharmony_cidrivers. 10073fd033Sopenharmony_ci 11073fd033Sopenharmony_ciImplementation-wise, the basic idea is that you provide an interface to 12073fd033Sopenharmony_cilibusb's internal "backend" API, which performs the appropriate operations on 13073fd033Sopenharmony_ciyour target platform. 14073fd033Sopenharmony_ci 15073fd033Sopenharmony_ciIn terms of USB I/O, your backend provides functionality to submit 16073fd033Sopenharmony_ciasynchronous transfers (synchronous transfers are implemented in the higher 17073fd033Sopenharmony_cilayers, based on the async interface). Your backend must also provide 18073fd033Sopenharmony_cifunctionality to cancel those transfers. 19073fd033Sopenharmony_ci 20073fd033Sopenharmony_ciYour backend must also provide an event handling function to "reap" ongoing 21073fd033Sopenharmony_citransfers and process their results. 22073fd033Sopenharmony_ci 23073fd033Sopenharmony_ciThe backend must also provide standard functions for other USB operations, 24073fd033Sopenharmony_cie.g. setting configuration, obtaining descriptors, etc. 25073fd033Sopenharmony_ci 26073fd033Sopenharmony_ci 27073fd033Sopenharmony_ciFile descriptors for I/O polling 28073fd033Sopenharmony_ci================================ 29073fd033Sopenharmony_ci 30073fd033Sopenharmony_ciFor libusb to work, your event handling function obviously needs to be called 31073fd033Sopenharmony_ciat various points in time. Your backend must provide a set of file descriptors 32073fd033Sopenharmony_ciwhich libusb and its users can pass to poll() or select() to determine when 33073fd033Sopenharmony_ciit is time to call the event handling function. 34073fd033Sopenharmony_ci 35073fd033Sopenharmony_ciOn Linux, this is easy: the usbfs kernel interface exposes a file descriptor 36073fd033Sopenharmony_ciwhich can be passed to poll(). If something similar is not true for your 37073fd033Sopenharmony_ciplatform, you can emulate this using an internal library thread to reap I/O as 38073fd033Sopenharmony_cinecessary, and a pipe() with the main library to raise events. The file 39073fd033Sopenharmony_cidescriptor of the pipe can then be provided to libusb as an event source. 40073fd033Sopenharmony_ci 41073fd033Sopenharmony_ci 42073fd033Sopenharmony_ciInterface semantics and documentation 43073fd033Sopenharmony_ci===================================== 44073fd033Sopenharmony_ci 45073fd033Sopenharmony_ciDocumentation of the backend interface can be found in libusbi.h inside the 46073fd033Sopenharmony_ciusbi_os_backend structure definition. 47073fd033Sopenharmony_ci 48073fd033Sopenharmony_ciYour implementations of these functions will need to call various internal 49073fd033Sopenharmony_cilibusb functions, prefixed with "usbi_". Documentation for these functions 50073fd033Sopenharmony_cican be found in the .c files where they are implemented. 51073fd033Sopenharmony_ci 52073fd033Sopenharmony_ciYou probably want to skim over *all* the documentation before starting your 53073fd033Sopenharmony_ciimplementation. For example, you probably need to allocate and store private 54073fd033Sopenharmony_ciOS-specific data for device handles, but the documentation for the mechanism 55073fd033Sopenharmony_cifor doing so is probably not the first thing you will see. 56073fd033Sopenharmony_ci 57073fd033Sopenharmony_ciThe Linux backend acts as a good example - view it as a reference 58073fd033Sopenharmony_ciimplementation which you should try to match the behaviour of. 59073fd033Sopenharmony_ci 60073fd033Sopenharmony_ci 61073fd033Sopenharmony_ciGetting started 62073fd033Sopenharmony_ci=============== 63073fd033Sopenharmony_ci 64073fd033Sopenharmony_ci1. Modify configure.ac to detect your platform appropriately (see the OS_LINUX 65073fd033Sopenharmony_cistuff for an example). 66073fd033Sopenharmony_ci 67073fd033Sopenharmony_ci2. Implement your backend in the libusb/os/ directory, modifying 68073fd033Sopenharmony_cilibusb/os/Makefile.am appropriately. 69073fd033Sopenharmony_ci 70073fd033Sopenharmony_ci3. Add preprocessor logic to the top of libusb/core.c to statically assign the 71073fd033Sopenharmony_ciright usbi_backend for your platform. 72073fd033Sopenharmony_ci 73073fd033Sopenharmony_ci4. Produce and test your implementation. 74073fd033Sopenharmony_ci 75073fd033Sopenharmony_ci5. Send your implementation to libusb-devel mailing list. 76073fd033Sopenharmony_ci 77073fd033Sopenharmony_ci 78073fd033Sopenharmony_ciImplementation difficulties? Questions? 79073fd033Sopenharmony_ci======================================= 80073fd033Sopenharmony_ci 81073fd033Sopenharmony_ciIf you encounter difficulties porting libusb to your platform, please raise 82073fd033Sopenharmony_cithese issues on the libusb-devel mailing list. Where possible and sensible, I 83073fd033Sopenharmony_ciam interested in solving problems preventing libusb from operating on other 84073fd033Sopenharmony_ciplatforms. 85073fd033Sopenharmony_ci 86073fd033Sopenharmony_ciThe libusb-devel mailing list is also a good place to ask questions and 87073fd033Sopenharmony_cimake suggestions about the internal API. Hopefully we can produce some 88073fd033Sopenharmony_cibetter documentation based on your questions and other input. 89073fd033Sopenharmony_ci 90073fd033Sopenharmony_ciYou are encouraged to get involved in the process; if the library needs 91073fd033Sopenharmony_cisome infrastructure additions/modifications to better support your platform, 92073fd033Sopenharmony_ciyou are encouraged to make such changes (in cleanly distinct patch 93073fd033Sopenharmony_cisubmissions). Even if you do not make such changes yourself, please do raise 94073fd033Sopenharmony_cithe issues on the mailing list at the very minimum. 95