libusb Function Description - good

 Taken https://blog.csdn.net/wince_lover/article/details/70337809

 

1 libusb_init
Function Prototype: int libusb_init (libusb_context ** ctx) ;
Function Description: This function initializes libusb must first call.
Parameters: CTX usually set NULL
Return Value: 0 success, otherwise fail


2 libusb_exit
function prototype: void libusb_exit (libusb_context * ctx) ;
Function Description: libusb_init and used in pairs, release the corresponding resources.
Parameters: ctx usually set NULL


3 libusb_has_capability
Function Prototype: int libusb_has_capability (uint32_t capability);
Function Description: determine whether the current library supports a function
capability in the range enum libusb_capability defined: Parameter Description.
          LIBUSB_CAP_HAS_CAPABILITY libus library API is valid, that is usually always returns 1
          LIBUSB_CAP_HAS_HOTPLUG hot swappable
          LIBUSB_CAP_HAS_HID_ACCESS whether to support the access to HID device, without the need for user intervention
          LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER whether to support the release of default driver is in the kernel can call to release libusb_detach_kernel_driver kernel driver
return value: non-0 support, does not support 0


4 libusb_hotplug_register_callback
Function Prototype: int LIBUSB_CALL libusb_hotplug_register_callback (libusb_context * CTX, libusb_hotplug_event Events, libusb_hotplug_flag the flags, int VENDOR_ID, int the product_id,
                                                int dev_class, libusb_hotplug_callback_fn cb_fn, void * the user_data, libusb_hotplug_callback_handle * handle);
Function Description: callback function is registered, in response to hot plug pulling events.
Parameters: ctx usually NULL
          event to respond to events, the parameter is inserted into the device LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED event LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT device removal event, it can also LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT expressed simultaneously incident response plug
          flags If 0, occurs only when the plug will be called registered callback function, if LIBUSB_HOTPLUG_ENUMERATE, the device has been inserted before the initialization, also called registered destroy function.
          vendor_id need to monitor the VID, only the specified device VID plug, will call the callback function. Set LIBUSB_HOTPLUG_MATCH_ANY, VID is determined not
          PID need to monitor the product_id, if set to LIBUSB_HOTPLUG_MATCH_ANY, no PID determination
          device class dev_class need to be monitored, if set to LIBUSB_HOTPLUG_MATCH_ANY, not determined class. Note that the class is matched class libusb_device_descriptor instead libusb_interface_descriptor the class
          definition of pointer callback function cb_fn callback function is int LIBUSB_CALL hotplug_callback (libusb_context * ctx, libusb_device * dev, libusb_hotplug_event event, void * user_data)
          pointer user_data user data corresponds to user_data callback function
          handle handle 
return value: 0 successful non-zero failure
Note: Do not call potentially blocking operations in the callback function, or may cause other functions libusb execution fails, do not call libusb_claim_interface and other operations in the callback function, there may fail


5 libusb_hotplug_deregister_callback
function prototype: void LIBUSB_CALL libusb_hotplug_deregister_callback (libusb_context * ctx , libusb_hotplug_callback_handle handle);
Parameters: libusb_hotplug_register_callback cancellation callback function registered
parameters: CTX usually NULL          
          handle handle libusb_hotplug_register_callback return
Return Value: None


6 libusb_handle_events  
function prototype: int LIBUSB_CALL libusb_handle_events (libusb_context * ctx   );
Function: to process any pending events in blocking mode.
Parameters: ctx usually NULL  
return value: 0 successful Non-zero failure
Note: 1 libusb_handle_events calling thread, do not perform the operation blocking, or may cause other function fails libusb
2 if they are registered hotplug events must be in this function is called a loop such as how the following code as plug on the USB device will not print "device INSERT"
static int LIBUSB_CALL hotplug_callback (libusb_context * ctx, libusb_device * dev, libusb_hotplug_event Event, the user_data void *)
{
        printf ( "device INSERT \ n-");   
}  
int main (int argc, char ** the argv)
{
        libusb_hotplug_callback_handle HP;
        libusb_init (NULL);
        libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY,
                LIBUSB_HOTPLUG_MATCH_ANY, 0, hotplug_callback, NULL, &hp);
        while(1);
        libusb_hotplug_deregister_callback(hp);
}  
必须要改为下面的代码,插拔USB才会有“device insert” 的信息  
static int LIBUSB_CALL hotplug_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
        printf("device insert  \n");   
}  
int main(int argc, char **argv)
{
        libusb_hotplug_callback_handle hp;
        libusb_init (NULL);
        libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_ENUMERATE, LIBUSB_HOTPLUG_MATCH_ANY,
                LIBUSB_HOTPLUG_MATCH_ANY, 0, hotplug_callback, NULL, & HP);
        the while (. 1)
        {
                libusb_handle_events (NULL);
        }
        libusb_hotplug_deregister_callback (HP);
}  
 
. 7 libusb_open_device_with_vid_pid
function prototype: libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid (libusb_context * ctx, uint16_t vendor_id, uint16_t product_id)  ;
function: open a USB device VID and PID, and returns a pointer to the device handle libusb_device_handle
parameters: CTX usually NULL 
          VID VENDOR_ID device
          of PID devices product_id
Return value: the successful return pointer libusb_device_handle, failed to return NULL


8 libusb_open
Function Prototype: int LIBUSB_CALL libusb_open (libusb_device * dev , libusb_device_handle ** handle);
Function: a USB device opens libusb_device the pointer, and returns a pointer to the device handle libusb_device_handle
Parameters: dev libusb_device pointer
          handle to return the device handle libusb_device_handle pointer
return value: 0 successful non-zero failure


9 libusb_close
function prototype: void LIBUSB_CALL libusb_close (libusb_device_handle * dev_handle );
Function: libusb_open_device_with_vid_pid open or close the device libusb_open
Parameters: dev_handle libusb_open call or return a pointer libusb_open_device_with_vid_pid libusb_device_handle the device handle
Return Value: None


10 libusb_get_device_list
function prototype: ssize_t LIBUSB_CALL libusb_get_device_list (libusb_context * ctx , libusb_device *** list);
Function: Get the current list of devices
function features: ctx usually NULL 
          List the USB device list
Return value: 0 successful Non-zero failure


11 libusb_free_device_list
function prototype: void LIBUSB_CALL libusb_free_device_list (libusb_device ** list , int unref_devices);
Function: releasing device list previously used
parameters: a pointer to a list of device list released
          unref_devices If a list of the parameter set for each device the reference count by 1
return value: None
Note: the following is sample code:
        libusb_device ** devs;
        an ssize_t CNT;
        int I;
        libusb_init (NULL); 
        CNT = libusb_get_device_list (NULL, & devs);
        for (I = 0; I <CNT ; I ++)
        {
                PrintUsbDec (devs [I]);
        }
        libusb_free_device_list (devs,. 1);
        
12 is libusb_get_device_descriptor
Function Prototype: int LIBUSB_CALL libusb_get_device_descriptor (libusb_device * dev         , struct libusb_device_descriptor * desc);
Function: Get Device Descriptor USB device
Parameters: dev libusb_device pointer is a device to read
                  pointer descriptor desc apparatus for back device descriptor structure
return value: 0 success, otherwise fail
Note: this function is to copy the structure of the device descriptor pointing to the address desc
Therefore, the following syntax error
struct * desc libusb_device_descriptor;
libusb_get_device_descriptor (dev, desc);
this is the correct
struct libusb_device_descriptor desc;
libusb_get_device_descriptor (dev, & desc);        


13 libusb_get_config_descriptor
Function Prototype: int LIBUSB_CALL libusb_get_config_descriptor (libusb_device * dev , uint8_t config_index, struct libusb_config_descriptor ** config);
Function: Get the specified device configuration descriptor
Parameters: dev libusb_device pointer is a device to read
          config_index configuration described character index (USB device may have a plurality of configuration)
          pointers config configuration descriptor, the descriptor for the device back
return value: 0 success, otherwise fail


14 libusb_free_config_descriptor  
function prototype: void LIBUSB_CALL libusb_free_config_descriptor (struct libusb_config_descriptor *  config);
Function: release configuration descriptor
Parameters: config to release configuration descriptor
Return Value: None
Note: After obtaining the configuration descriptor with libusb_get_config_descriptor must call libusb_free_config_descriptor release otherwise it will cause a memory leak.


15 libusb_control_transfer
Function Prototype: int LIBUSB_CALL libusb_control_transfer (libusb_device_handle * dev_handle, uint8_t Request_type, uint8_t the bRequest, uint16_t wValue, uint16_t the wIndex,
              unsigned char * Data, uint16_t wLength field, unsigned int timeout);  
Parameter Description: dev_handle libusb_device_handle pointer
                  request_type D7 = 0 Host the device to the host device = 1;
                            D6D5 = 00 standard request command, the request command type 01, user-defined commands 10, 11 retention
                            D4D3D2D1D0 = 0 represents that the receiver of the device, the interface 1 represents a receiver, the receiver 2 represents endpoint, represents 3 other recipients, other values are reserved
                            this parameter consists enum libusb_request_recipient, enum libusb_request_type, enum libusb_endpoint_direction combination
          number (commands actually) the bRequest command; all commands are encoded in a manner different values passed to the device , bRequest means the code value USB command. USB standard commands can also be user-defined command
                        Request command is defined in the standard libusb_standard_request enum
          the Value 2 bytes used to transmit the parameters of the current request, requests with different variations.
          Index index field is also a 2-byte, the interface number is described
          pointer data to data to be transmitted, no data transmission, is set to NULL
          length wLength data. When the command data transmission is not required, this field is set to 0
          timeout provided milliseconds for timeout, if set to 0, the timer never expires
NOTE: USB standard request command:
1. Get state of the Status the Get (00H)
    A: [the To Device] Get device status:
   * bit 0: self-powered (0 bus powered; 1 represents a self-powered).
   * bit 1: remote wakeup (0 indicates not support remote wake-up; 1 represents a remote wake-up).
   * bits 2-1. 15: reserved.
   * select bus-powered ships, do not support remote wake up, the returned data is 0x0000..
   B: [the to Interface] state acquisition interfaces:
   * 16-byte interface and state of all reservations, the returned data is 0x0000. .
   C: [the to endpoint] acquired state of the endpoint:
   * bit 0: the Halt (0 indicating that the endpoint allows; 1 represents endpoint prohibited).
   * bit 1 ~ 15: reserved (reset to 0)
2. Clear Clear Feature characteristics (01H)
   A: [To Device] Clear a remote wake-up function, and returns a null packet.
   B: [the To Endpoint] lifted endpoint.
3. Set the Feature Characteristics of the Set (03H)
   A: [To Device] disposed remote wakeup device, and returns a null packet.
   B: [the To endpoint] Disable endpoint.
4. set the address address the set (05H)
   a:. device address setting
5. obtain the descriptor descriptor the Get (06H)
   a: [the To device] Get device descriptor :
   . * describe the current version of the USB protocol device endpoint FIFO size .USB like device ID number 0.
   B: [the to the configuration] configuration descriptor obtain:
   * number description USB interface device and whether there is the ability to self-powered. and the like.
   C: [the to Interface] obtaining interface descriptor:
   . * describe physical endpoint number and other information than the endpoints 0
   D: [to endpoint] Gets endpoint descriptors:
   * description 0 endpoint transfer type and maximum endpoints. packet size, and transmission direction information endpoint (the iN / OUT).
6. the descriptor is set (optional, can not update) the set the descriptor (07H)
7. the configuration information acquires the configuration the Get (08H)
8. the set configuration set configuration (09H)
A: [To configuration] configuration descriptor is provided.
B:. [To Interface] interface descriptor set
C:. [To Endpoint] provided endpoint descriptor
9. Interface obtaining interface information the Get (0AH)    
10. The interfaces provided Interface the Set (0BH)
11.SYNCH_FRAME (0CH)
device for a frame sync setup and reporting endpoint.


16 libusb_kernel_driver_active
Function Prototype: int LIBUSB_CALL libusb_kernel_driver_active (libusb_device_handle * dev , int interface_number);
Function: Specifies the kernel driver determines whether the interface has been activated. If a kernel driver is active, libusb_claim_interface call fails
Parameters: dev libusb_open call or return libusb_device_handle libusb_open_device_with_vid_pid handle
                  interface_number interface number of the interface descriptor corresponding to bInterfaceNumber
Return Value: 1 has been activated, no non-activated 1


17 libusb_detach_kernel_driver
Function Prototype: int LIBUSB_CALL libusb_detach_kernel_driver (libusb_device_handle * dev , int interface_number);
Function: unload the kernel driver of the specified interface. If a kernel driver is active, must call this function, then call libusb_claim_interface
Parameters: dev call libusb_open or libusb_open_device_with_vid_pid returned libusb_device_handle handle
                  interface_number interface number, this corresponds to an interface descriptor bInterfaceNumber
Return Value: 0 success, otherwise failure


18 libusb_claim_interface
Function Prototype: int LIBUSB_CALL libusb_claim_interface (libusb_device_handle * dev , int interface_number);
Function: specified device application Interface
Parameters: dev call libusb_open or libusb_open_device_with_vid_pid returned libusb_device_handle handle
                  interface_number interface number, this corresponds to an interface descriptor bInterfaceNumber
return value: 0 successful non-zero failure


19 libusb_release_interface
function prototype: int LIBUSB_CALL libusb_release_interface (libusb_device_handle * dev , int interface_number);
Function: Before the release of the specified device application interface Note that this function only release the interface, not reload the kernel driver
Parameters: dev calling libusb_open or libusb_open_device_with_vid_pid libusb_device_handle handle returned
                  interface_number interface number of the interface descriptor corresponding to bInterfaceNumber
return value: 0 success, otherwise fail


19 libusb_attach_kernel_driver
Function Prototype: int LIBUSB_CALL libusb_attach_kernel_driver (libusb_device_handle * dev , int interface_number);
Function: loading a kernel driver for the specified interface
Parameters: dev call libusb_open or libusb_open_device_with_vid_pid returned libusb_device_handle handle
                  interface_number interface number, this corresponds to an interface descriptor bInterfaceNumber
return value: 0 successful non-zero failure 


The following sample code:
        IF (libusb_kernel_driver_active (handle, bInterfaceNumber) ==. 1) is determined when the kernel driver is loaded // 
        { 
             
                IF (libusb_detach_kernel_driver (handle, bInterfaceNumber) == 0) // unload the driver, for example, a U-disk operations our , this device is executed to a disk file disappears inside the U
                {
                         the printf ( "Kernel Driver of type Detached!");  
                }
         }  
         libusb_claim_interface (handle, bInterfaceNumber);  
         .......... read and write operations of the apparatus
         libusb_release_interface (handle, bInterfaceNumber); // release the interface request 
         
         libusb_attach_kernel_driver (handle, bInterfaceNumber); // load the kernel driver, U disk and will reappear in the device file
        
20 libusb_set_auto_detach_kernel_driver
Function prototype: int LIBUSB_CALL libusb_set_auto_detach_kernel_driver (libusb_device_handle * dev , int enable);
Function: Set automatically unload the kernel driver, attention will not unload the kernel driver when this function call, but marking. When you call libusb_claim_interface unloading kernel driver
          loaded automatically when you call libusb_release_interface kernel driver
Parameters: dev call handler libusb_open or libusb_open_device_with_vid_pid returned libusb_device_handle the
                  enable 1 to enable automatic unloading function 0 Close
Return value: 0 successful Non-zero failure 
the above example code and the following code equivalent effect        
        libusb_set_auto_detach_kernel_driver (handle,. 1); 
        libusb_claim_interface (handle, bInterfaceNumber); // prior to unload the kernel driver request interface
        .......... read and write operations of the apparatus
        libusb_release_interface // automatically loaded after release request interface kernel driver; (handle, bInterfaceNumber) 
        
21 is libusb_bulk_transfer
Function Prototype: int LIBUSB_CALL libusb_bulk_transfer (libusb_device_handle * dev_handle , unsigned char endpoint, unsigned char * data, int length, int * actual_length, unsigned int timeout);
Function: perform USB bulk transfer. This function may process input and output, depending on the direction bit endpoint address of inferred transmission direction, the function synchronous mode, the data transfer is complete before returning
Parameters: Handle libusb_device_handle of dev_handle call libusb_open or libusb_open_device_with_vid_pid returned
                  endpoint endpoint address of the highest bit is 1, input
                  data transmission or reception buffer pointer
                  length buffer length
                  actual_length back length of the actual transmission
                  timeout milliseconds for timeout, the timeout never 0
return value: 0 success, otherwise fail 


22 libusb_clear_halt
Function Prototype: int LIBUSB_CALL libusb_clear_halt (libusb_device_handle * dev , unsigned char endpoint);
Function: clear endpoint halt / stall state, libusb_bulk_transfer possible to return LIBUSB_ERROR_PIPE, which is required to call this function
Parameters: dev call libusb_open or libusb_open_device_with_vid_pid return the libusb_device_handle handle
          endpoint endpoint address error
return value: 0 successful non-zero failure   


23 libusb_interrupt_transfer
function prototype: int LIBUSB_CALL libusb_interrupt_transfer (libusb_device_handle * dev_handle  , unsigned char endpoint, unsigned char * data, int length, int * actual_length, unsigned int timeout);
Function: perform USB interrupt transfer. This function may process input and output, depending on the direction bit endpoint address of inferred transmission direction, the function synchronous mode, the data transfer is complete before returning
Parameters: Handle libusb_device_handle of dev_handle call libusb_open or libusb_open_device_with_vid_pid returned
                  endpoint endpoint address of the highest bit is 1, input
                  data transmission or reception buffer pointer
                  length buffer length
                  actual_length back length of the actual transmission
                  timeout milliseconds for timeout, the timeout never 0
return value: 0 success, otherwise fail                        


24 libusb_set_configuration
function prototype: int LIBUSB_CALL libusb_set_configuration (libusb_device_handle * dev , int configuration);
Function: a configuration parameter set for the device, most of the equipment is only one configuration is usually not necessary to call this function. When a USB device has a plurality of time need to set the configuration
Parameters: dev libusb_open the call handler or return libusb_device_handle libusb_open_device_with_vid_pid the
                  configuration parameters configuration, this corresponds to the configuration descriptor inside bConfigurationValue
Return Value: 0 success, otherwise fail 

Guess you like

Origin www.cnblogs.com/LiuYanYGZ/p/12341927.html