Linux has a more complete support for USB, but look at the principle of the code, or feel confused. Someone recommended Libusb, search the internet for a bit of information, well, it feels really much simpler.
Let's introduce LIBUSB:
USB driver Development on Linux platform, mainly with kernel-driven development and LIBUSB-based non-drive design.
1, why to develop LIBUSB
For most of the kernel-driven devices, such as HID devices with USB interfaces, Linux itself has its own driver, and we can do most of the work on the device by manipulating the device files, while others, such as the hardware products of their own design, These drivers require our driver engineers to develop the relevant drivers. The kernel driver has its advantages, but in some cases the kernel driver encounters some of the following problems:
1 when customers using our products have 2.4 core platform, but also have 2.6 core platform, we want to design the driver is to be compatible with two platforms, even makefile we have to write two.
2 When we want to transplant Linux into the embedded platform, you will find that the original Linux comes with a drive that is quite large, my kernel is of course the smaller the better pull, so it is necessary. This is not the most depressing place, if the embedded platform is the customer, customers want to buy your products, you suddenly found that the customer device system and your environment is not the same, it does not have the driver you want, your program can not run, you will first think: "It's OK, I write a kernel driver to load a bit of the line." But found that the customer even Insmod loading module tools are not ported, then you look at God, say I how so unlucky ah, the customer does not want you to move he spent n time the kernel of the transplant OH
3 took some effort to write a new product driver, quite a sense of accomplishment ah, code quality is also quite standard AH. Just as you indulge in your code, customer service constantly mail, "customer needs 2.6.5 kernel Driver, config file I have sent you" "Customer needs dual-core 2.6.18-SMP driver" "Customer's platform is their own custom is 2.6.12-xxx" You would like to give the driver source code to the customer, so as not to compile. Part of your work time compile kernel, custom drive
There is a problem, there will be a way to solve the problem of the people, the emergence of LIBUSB brought us some convenience, that is to save our time, but also reduce the cost of the company. So in some cases, you can consider using Libusb's non-flooding design.
2, how to use LIBUSB for development
LIBUSB is a USB library based on user space. Libusb designed a series of external APIs called by the application, through which the API applications can manipulate the hardware, from the source code of LIBUSB can be seen, these APIs call the kernel of the underlying interface, and kernel driver used in the function of the implementation of the same function, Just libusb closer to the USB spec. This makes the use of libusb much easier than developing kernel drivers.
2.0 Some important data structures
struct Usb_dev_handle {
int FD;
struct Usb_bus *bus;
struct Usb_device *device;
int config;
int interface;
int altsetting;
void *impl_info;
};
struct Usb_device {
struct Usb_device *next, *prev;
Char Filename[path_max + 1];
struct Usb_bus *bus;
struct Usb_device_descriptor descriptor;
struct Usb_config_descriptor *config;
void *dev; /* Darwin Support */
};
struct Usb_bus {
struct Usb_bus *next, *prev;
Char Dirname[path_max + 1];
struct Usb_device *devices;
};
2.1 Initializing the Device interface
These interfaces can also be called core functions, which are primarily used to initialize and find related devices.
Usb_init
function definition: void usb_init (void);
From the function name you can see that this function is used to initialize the relevant data, this function everyone just remember must call on the line, and is the beginning of the call.
Usb_find_busses
function definition: int usb_find_busses (void);
Looking for a USB bus on the system, any USB device communicates via USB bus and computer bus. And then communicate with other devices. This function returns the number of buses.
Usb_find_devices
function definition: int usb_find_devices (void);
Looking for a USB device on the bus, this function must be used after calling Usb_find_busses (). The three functions above are all used at the beginning, and this function returns the number of devices.
Usb_get_busses
function definition: struct Usb_bus *usb_get_busses (void);
This function returns the list of buses, which are not available in the higher versions, as explained in the following example
2.2 Operating Device Interface
Usb_open
function definition: usb_dev_handle *usb_open (struct *usb_device dev);
Open the device you want to use, you must call Usb_open to open the device before you operate on the hardware, here we see that there are two structures usb_dev_handle and usb_device are often encountered in the development, it is necessary to look at their structure. There are definitions in usb.h and usbi.h in Libusb.
Here we may as well understand that the return usb_dev_handle pointer is a handle to the device, and the input in the row parameter is the device that needs to be opened.
Usb_close
function definition: int usb_close (Usb_dev_handle *dev);
As opposed to Usb_open, shutting down the device, which must be called, returns 0 success, <0 failed.
Usb_set_configuration
function definition: int usb_set_configuration (usb_dev_handle *dev, int configuration);
Sets the configuration used by the current device, the parameter configuration is the Bconfigurationvalue in the Configurtation descriptoes to be used, returns 0 success, <0 failure ( A device may contain more than one configuration, such as a device that supports both high-speed and low-speed devices, with a corresponding two configuration, with detailed view of the USB standard.
Usb_set_altinterface
function definition: int usb_set_altinterface (usb_dev_handle *dev, int alternate);
As the name implies, this function sets the interface descriptor for the current device configuration, and the parameter alternate refers to descriptor in interface balternatesetting. Returns 0 successful, <0 failed
Usb_resetep
function definition: int usb_resetep (usb_dev_handle *dev, unsigned int EP);
Resets the specified endpoint, the parameter EP refers to the Bendpointaddress,. This function is not used frequently and is replaced by the following Usb_clear_halt function.
Usb_clear_halt
function definition: int usb_clear_halt (usb_dev_handle *dev, unsigned int EP);
Resets the specified endpoint, the parameter EP refers to the bendpointaddress. This function is used to replace Usb_resetep
Usb_reset
function definition: int usb_reset (Usb_dev_handle *dev);
This function is basically not how to use, but here I also say, and the name of the meaning, the function reset device, because after restarting the device or to re-open the device, so with Usb_close can already meet the requirements.
Usb_claim_interface
function definition: int usb_claim_interface (usb_dev_handle *dev, int interface);
Registers the interface with the operating system communication, this function must be called, because only registers the interface, can do the corresponding operation. Interface refers to Binterfacenumber. (The usb_release_interface described below corresponds to, and is also a function that must be called)
Usb_release_interface
function definition: int usb_release_interface (usb_dev_handle *dev, int interface);
Unregisters the interface after being called by the Usb_claim_interface function, releasing the resource, and usb_claim_interface corresponding to use.
2.3 Control Transmission Interface
Usb_control_msg
function definition: int usb_control_msg (usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, I NT timeout);
Send and receive control data from the default pipeline
Usb_get_string
function definition: int usb_get_string (usb_dev_handle *dev, int index, int langid, char *buf, size_t Buflen);
Usb_get_string_simple
function definition: int usb_get_string_simple (usb_dev_handle *dev, int index, char *buf, size_t Buflen);
Usb_get_descriptor
function definition: int usb_get_descriptor (Usb_dev_handle *dev, unsigned char type, unsigned char index, void *buf, int size);
Usb_get_descriptor_by_endpoint
function definition: int usb_get_descriptor_by_endpoint (Usb_dev_handle *dev, int EP, unsigned char type, unsigned char index, void *buf, int size);