Author: Dong Yuxin, Zhang Donglai Source: Micro-Computer Information
1. Introduction
Hardware driver development is the foundation of embedded development. With the popularization of USB devices, the development of USB device drivers has become increasingly important in embedded development.
To support different types of platform peripherals that can be connected to Windows CE, Microsoft provides a stream interface driver model with custom interfaces. Most USB peripheral devices are more suitable for the structure of the stream interface driver. They can use the stream interface driver model to develop their own drivers. This article analyzes and designs the driver development of USB peripheral devices on the Windows CE platform based on the Flow interface driver.
2. Analysis of USB (Universal Serial Bus) system under Windows CE
The USB system software under Windows CE is composed of two layers: The driver layer of the higher USB device and the USB function layer. The lower USB function layer consists of two parts: the higher general serial bus driver (usbd) module and the lower master controller driver (HCD) module. Based on the functions provided by the HCD module, the usbd module implements high-level usbd interface functions. USB device drivers use the usbd interface function to communicate with their peripheral devices.
During data transmission, the operation process is generally performed in the following order:
1. the USB device driver initializes data transmission, that is, it sends a data transmission request to the usbd module by using the usbd interface function.
2. Based on the characteristics of the USB device connected to the bus, the usbd module divides the request into several separate transactions.
3. the HCD module arranges the order of transactions through the bus.
4. The master controller hardware executes or completes these transactions.
All the transactions on the bus are sent from the host side, and the peripheral devices are completely dependent.
3. Entry point function of the USB driver
All USB device drivers must present certain entry points in their DLL libraries to interact with the usbd module properly. These entry-point functions not only enable the usbd module to connect to the driver of the peripheral device, but also enable the driver to create and manage any registration keys that may be required:
1. USB deviceattach
When the USB device is connected to the master computer, the usbd module calls this function. This function is mainly used to initialize a USB device, obtain information about the USB device, configure the USB device, and apply for required resources. When the Device Manager initializes the device, it can write the device driver handle and other information to the HKLM/Drivers/active/XX directory of the Registry as the second parameter of the activatedevice () function, and pass the Registry address as a parameter to the xxx_init () function for use by the application.
2. usbinstalldriver
This function is called first when the driver of a USB device is loaded for the first time. It is mainly used to set the registry information required by a driver, for example, the device name is written to the HKEY_LOCAL_MACHINE/Drivers/USB/clientdrivers directory. Note that the USB device driver uses the registerclientdriverid () and registerclientsettings () functions instead of the standard registry function to register the corresponding device information. The method is as follows:
Bool
Usbinstalldriver (lpcwstr szdriverlibfile)
{
// Set the description of the USB device. If the information is null, set it to usb_no_info.
Usb_driver_settings usbdriversettings = {driver_settings };
// Write the Registry
BRC = registerclientdriverid (wsusbdeviceid );
If (! BRC)
{
Return false;
}
BRC = registerclientsettings (szdriverlibfile,
Wsusbdeviceid,
Null,
& Usbdriversettings );
If (! BRC)
{
Return false;
}
.................................
}
3. usbuninstalldriver
This function is called when you delete a driver from a Windows CE-based platform. It is used to call the unregisterclientsettings () and unregisterclientdriverid () functions to delete the USB installdriver () of the driver () all registration keys created by the function.
4. Design of flow interface driver for USB devices
The existence of the USB driver allows the peripheral device to provide services for the application, indicating the relationship between the flow interface driver and the usbd module and other system components of Windows CE.
Figure 1 system structure of the stream interface driver
Fig.1 the frame of Stream interface drivers
Each stream interface driver must implement a set of standard functions, such as xxx_init (), xxx_iocontrol (), and xxx_powerup (), to complete standard file I/O functions and power management, after a DLL is generated, replace xxx with the prefix of the device file name.
The following uses xxx_init () as an example to briefly introduce the use of these functions.
The xxx_init () function is called by the activedeviceex () function provided by the Device Manager instead of by the application. When the Device Manager initializes the device, we have used the activatedevice () function to write the device handle and other information to drivers/active, therefore, during application initialization, we only need to pass the Registry address as the context parameter to xxx_init (), and then use functions such as regopenkeyex () and regqueryvalueex () to open and read/write the registry, after the execution is successful, the USB device handle and other information are returned.
The following is part of the original code in the USB camera driver cam_ini () function:
Cam_init (pvoid dwcontext)
{
................
Lptstr activepath = (lptstr) dwcontext; // HKLM/Drivers/active/xx
Hkey;
Long lstatus;
................
// Open the Registry
Lstatus = regopenkeyex (HKEY_LOCAL_MACHINE,
Activepath,
0,
0,
& Hkey );
// Obtain data in the Registry
Lstatus = regqueryvalueex (hkey,
Text ("clientinfo "),
Null,
& Dwtype,
(Lpbyte) (& dwval ),
& Dwvallen );
.................
Pusbprn = (pusbcam_context) dwval;
................
Return (pusbprn );
}
Before reading a device, run the creatfile () function to call xxx_open () to open the device () the first parameter is the device handle returned by xxx_init () during app initialization. The first parameter required by xxx_read () is the driver reference instance handle returned after creatfile () is successfully executed, the second and third parameters are the buffer address and length used to read data from the driver. The application calls xxx_read () through the readfile () function. In xxx_read (), it calls different transmission functions provided by the usbd module to communicate with different USB peripheral devices, for example, in a printer device, the function issuebulktransfer () needs to be transferred in batches ().
The usage of the xxx_write () function is roughly the same as that of xxx_read (). The functions of other stream interface functions are shown in the following table.
Table 1 streaming interface driver entry point function
Table 1 the entry function of Stream interface drivers
Function Name |
Description |
Xxx_init |
This function is called when the Device Manager initializes a USB device. |
Xxx_deinit |
This function is called when the Device Manager uninstalls a USB driver. |
Xxx_open |
When a USB driver is enabled, the application calls this function through the creatfile () function. |
Xxx_close |
When the USB driver is closed, the application calls this function through the closehandle () function. |
Xxx_iocontrol |
The upper-layer software can call this function through the deviceiocontrol () function. |
Xxx_read |
When the USB device driver is enabled, the application calls it through the readfile () function. |
Xxx_seek |
The Data Pointer of the USB device is called by the application through the setfilepointer () function. |
Xxx_write |
When a USB device driver is enabled, the application calls it through writefile (). |
Xxx_powerup |
Call this function before the system restarts. |
Xxx_powerdown |
Call this function before the system suspends. |
After obtaining these compiled stream interface functions, you must create one. def file, tell the linking program what kind of function to output, and finally compile the driver into the kernel, so that the flow interface driver of the USB device can be called by the application.
5. Conclusion
In Windows CE, Microsoft provides a general serial bus driver (usbd) module, a complete set of usbd interface functions, and a sample host controller driver (HCD) module, therefore, we only need to use different functions provided by usbd Based on the hardware features of the USB device to realize the interaction between the streaming interface function and the peripheral device. This greatly reduces the development time and enables faster embedded development.