Windows USB function driver development Summary

Source: Internet
Author: User

Recently, I made a functional driver for a USB device, analyzed its speed bottleneck, and found out the main reasons that affect the speed of the device. Because of the versatility of the USB device, the Code subject directly uses the sample code provided by DDK: bluckusb.
1. USB driver structure

Views: 173
File Size: 16.8 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 31655 & D = 1253583343 "border =" 0 "alt =" Name: USB device level. jpg
Views: 173
File Size: 16.8 kb ">
Figure 1: USB device hierarchy

The USB master controller is directly connected to the system bus like other I/O devices. The operating system communicates with the main controller using the I/O port or memory register. through normal interrupt signals, the system can receive event notifications from the main controller. The master controller connects to a USB device tree. A device called hub acts as a connection point for other devices. Multiple hubs can be connected in a chrysanthemum mode and can be connected to the maximum depth defined in the USB specification. Other devices, such as cameras, microphones, and keyboards, are directly connected to the hub. To precisely express concepts, USB uses the term function to describe non-hub devices.

2. What do I need to know when writing a USB driver?

A. Driver Compilation Method
All kinds of introductory books have been introduced. When compiling in the DOS environment of DDK, the error information will be displayed on the DOS interface. If the information cannot locate the problem, you can find the log file named buildxx in the Code directory.
B. Relationship between drivers and devices
The association between the driver and the device is specified by the INF file. Simply put, replace XXXX in vid_xxxx and pid_xxxx In the INF file in the USB driver example provided in DDK with the vid and PID values of your device. bulkusb in [sourcedisksfiles. replace sys with your own driver file name to associate the driver with the device. For more information about the INF file, refer to the relevant introduction documents.
C. device-defined commands
Custom devices generally use custom control commands. When writing a driver, you must understand the functions of these commands to locate the problem during debugging.
Iocontrol commands and device commands must be separated here. iocontrol commands are the commands for interaction between applications and drivers. The generation of iocontrol commands has no direct relationship with devices, you can set it as long as it does not conflict with the driver's standard command. The device command is used in the token stage, that is, the setup package. The device command is defined by the hardware designer, device commands are the first step for communication between the host and the device.

Views: 173
File Size: 17.7 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 31656 & D = 1253583343 "border =" 0 "alt =" Name: Setup token. jpg
Views: 173
File Size: 17.7 kb ">
Figure 2: content of the setup token

D. How the USB driver interacts with the device
In Windows, urb is sent to the USB device for communication and backup. The core of the function code is to construct urb and send urb. The information in the setup package (token package) is also included in urb. To facilitate the construction of urb, DDK provides a set of macro functions. Using macro functions, you can easily construct different types of urb, and construct the macro of the setup package as usbbuildvendorrequest, where index, value, request is used in hardware.

3. Key points for correct Data Reading and Writing

The USB device reads and writes data mainly through the endpoint. The data stream direction of one endpoint can only have one in or out. The endpoint exists in the interface and is obtained from the configuration. The configuration is mainly executed in the dispatch function of irp_mj_create. Shows the relationship between configurations, interfaces, and endpoints. In the bulkusb Code provided by DDK, The pipeinfomation settings are incorrect. It is unreasonable to use fileobject to save pipe handle. You need to use deviceextension to save it. In usbinterfaces, the pipes array has at least two elements, one is the handle used for data writing, the other is the handle used for Data Reading, written by pipes [0] In my device, and written by pipes [1.

Views: 173
File Size: 23.2 kb "src =" http://bbs.pediy.com/attachment.php? Attachmentid = 31657 & D = 1253583343 "border =" 0 "alt =" Name: USB configuration. jpg
Views: 173
File Size: 23.2 kb ">
Figure 3: USB device configuration, interface, and Endpoint

4. debug the driver Tool

Dbgview is required to view printed information. To track the driver code, use windbg and serial port lines for dual-machine debugging, download the symbol file of the debugged machine before debugging, and test and view the USB read/write data, use bus hound and usblyzer together; use source insight for driver code editing; Use the compiling environment provided by DDK for code compilation.

Related Article

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.