Linux + libusb development user USB driver)

Source: Internet
Author: User

Note: The user-level USB driver is well written. It seems that QT embedded porting is also based on this.
Kernel-level USB drivers have problems in the following aspects:

1. When the customers who use our products have a 2.4 kernel platform and a 2.6 kernel platform, we need to design a driver that is compatible with the two platforms, we have to write two makefiles.
2. If we want to port Linux to the embedded platform, you will find that the original Linux driver is still quite large. Of course, the smaller the kernel, the better the driver. Is this necessary. This is not the most depressing aspect. If the embedded platform is for the customer and the customer wants to buy your product, you suddenly find that the system on the customer's device is different from your environment, it does not have the driver you want. If your program cannot run, you will first think: "It doesn't matter. I will write a kernel driver and load it." But I found that the client didn't even port the insmod tool to load the module. At that time, you should look at God and say how I am so unlucky. The customer doesn't want you to touch the kernel that he spent n time porting.

3. I spent some time writing a new product driver. It was a sense of accomplishment and the code quality was quite good. As you indulge in your code, the Customer Service keeps sending emails, "the customer needs a 2.6.5 kernel driver, the config file has been sent to you. "The customer needs the 2.6.18-SMP drive with dual-core." "The customer's platform is customized by 2.6.12-xxx." You wish to give the source code of the driver to the customer., this saves compilation effort. Some of your working hours compile the kernel and customize the driver

If there is a problem, there will inevitably be people who want to solve the problem. The emergence of libusb has brought us some convenience, that is, saving our time and reducing the company's cost. Therefore, in some cases, you can consider using the libusb diskless design.

From: http://blog.sina.com.cn/s/blog_8a84e6d301015uh7.html
I bought a game joystick and buttons on the Internet. They connected to the computer through a USB port, so I wanted to develop my own USB driver for the handle and buttons, in order to be happy in the future, haha.
   Libusb is a user-side driver encapsulation library and a set of functions used by USB hosts to operate USB devices. With libusb, we do not need to modify the complicated Linux kernel driver for a certain type of USB device, this facilitates the use and debugging of devices. My computer's programming environment is ubuntu12.04 + geany (an integrated development tool in Linux, I personally feel quite useful ). Next we will share the Code:
# Include <stdio. h>
# Include <string. h>
# Include <USB. h>

# Define idvendor0x0079     // This is the vendor ID of my device
# Define idproduct0x0006    // This is the product number of my device. When your USB device is connected to your computer, run the lsusb-V command to view the device information. See 0079: 0006 in the third picture below.
// # Define printdev

Char enp_num [8], buf8 [8], is_change = 0;

// Endpoint Descriptor
Static void print_endpoint (struct usb_endpoint_descriptor * endpoint)
{
 Printf ("    Bendpointaddress: XH \ n ", endpoint-> bendpointaddress );
 Printf ("    Bmattributes:   XH \ n ", endpoint-> bmattributes );
 Printf ("    Wmaxpacketsize: % D \ n ", endpoint-> wmaxpacketsize );
 Printf ("    Binterval:      % D \ n ", endpoint-> binterval );
 Printf ("    Brefresh:       % D \ n ", endpoint-> brefresh );
 Printf ("    Bsynchaddress:  % D \ n ", endpoint-> bsynchaddress );
}

Static void print_altsetting (struct usb_interface_descriptor * interface)
{
 Int I;

 Printf ("  Binterfacenumber: % D \ n ", interface-> binterfacenumber );
 Printf ("  Balternatesetting:% D \ n ", interface-> balternatesetting );
 Printf ("  Bnumendpoints:    % D \ n ", interface-> bnumendpoints );
 Printf ("  Binterfaceclass:  % D \ n ", interface-> binterfaceclass );
 Printf ("  Binterfacesubclass: % d \ n ", interface-> binterfacesubclass );
 Printf ("  Binterfaceprotocol: % d \ n ", interface-> binterfaceprotocol );
 Printf ("  Iinterface:       % D \ n ", interface-> iinterface );

 For (I = 0; I <interface-> bnumendpoints; I ++)
   Print_endpoint (& Interface-> endpoint [I]);
}

// Display the descriptors of all interfaces owned by the device
Static void print_interface (struct usb_interface * interface)
{
 Int I;

 For (I = 0; I <interface-> num_altsetting; I ++)
   Print_altsetting (& Interface-> altsetting [I]);
}

// Configure the descriptor
Static void print_configuration (struct usb_config_descriptor * config)
{
 Int I;

 Printf ("Wtotallength:       % D \ n ", config-> wtotallength );
 Printf ("Bnuminterfaces:     % D \ n ", config-> bnuminterfaces );
 Printf ("Bconfigurationvalue:% D \ n ", config-> bconfigurationvalue );
 Printf ("Iconfiguration:     % D \ n ", config-> iconfiguration );
 Printf ("Bmattributes:       XH \ n ", config-> bmattributes );
 Printf ("Maxpower:           % D \ n ", config-> maxpower );

 For (I = 0; I <config-> bnuminterfaces; I ++)
   Print_interface (& config-> interface [I]);
}

Static void print_device (struct usb_device * Dev)
{
   // Read the descriptor
   Int I;
   Chardescription [256];
   Snprintf (description, sizeof (description), "X-X", Dev-> descriptor. idvendor, Dev-> descriptor. idproduct );
   Printf ("Dev # % d: % s \ n", Dev-> devnum, description );
   For (I = 0; I <Dev-> descriptor. bnumconfigurations; I ++)
     Print_configuration (& Dev-> config [I]);

}

// Because my game device belongs to the hid type, it transmits data with the computer through the interrupt endpoint. Read the device interrupt endpoint value when the joystick position changes or the key is pressed.
Static int read_interupt (struct usb_device * Dev)
{
   Usb_dev_handle * udev;
   
   Charbuf [2, 256];
   Intret, I;
   Udev = usb_open (Dev );
   
   // # Ifdefprintdev
      // Print_device (Dev );
   // # Endif
 
   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.