10 minutes complete a USB driver (recommended)

zhaozj2021-02-16  56

10 minutes to complete a USB driver writers: Rayyang2000, as reproduced please ensure the integrity of this document, and to indicate the source. Welcome to C Builder Research, http://www.ccrun.com/doc/go.asp? Id = 438 Many developers who wrote Windows Device Driver are basically developed using Windows DDK. However, there are still many people who have begun to with some auxiliary tools. The author started to come into contact with RiverStudio last year, found that it is really a good development tool, not only writing code, but also is very good with DDK. Of course, there are many people feel that DriversTudio is not authentic, or it is unable to understand the architecture of Windows Device Driver. I feel that this is a bit like the relationship between MFC and SDK. About this issue has arguments in many places. For example, in thousands of news groups, it will be nearly 2 months. Everyone has their own favorite, all have their own habits, as long as you can do things well, what method I want to use should be the same. If you are used to develop using DDK, you can continue to use it; if you think DriverStudio is good, try to use a tool that can be programmed to program the OOP concept? On the drive development network, I often see someone asking some questions about DriverStudio. I am very fortunate to use it for several drivers, including VXD, KMD, and WDM, a little bit of experience, so I want to write down to make a small reference. If you have any errors, you are welcome to point me, thank you. Let me introduce the process of developing a USB driver with DriverStudio. This USB device has 3 two-way endpoints, each endpoint is configured as follows: EP Type Address Buffer (Bytes) 0 In / Out Control 0x80 / ​​0x00 16/161 In / Out Bulk 0x81 / 0x01 16/162 In / Out Bulk 0x82 / 0x02 64/64 Our driver needs to be implemented to control the brightness and destruction of the LED light on the device, and read and write the device via EndPoint 2. Since DRIVESTUDIO consists of several parts, we write this driver as long as DriverWorks, so we will refer to it as DW. Here, we assume that the reader has installed DW correctly and has compiled each library file. 1. First, we start the VC IDE by shortcut "Setup DDK and Start MSVC". The program pointed to by this shortcut will make some necessary settings, then launch VC IDE so that our program can use DDK and DW header files and libraries.

2. Select "DriverWizard" from the VC IDE menu "driverstudio", in the dialog shown in Figure 1, write the project name. Here, we call this item as: Test, the directory of D: / TEST. Then click "Next>". Figure 13. In the next dialog (as shown in Figure 2), we need to select the type of driver. Since the USB device driver is the WDM type, we choose Two items and point buttons "Next>". Figure 24. In the third dialog box (as shown in Figure 3), select the bus type operating in our driver. Here, we choose USB. In USB Vendor ID and USB PRODUCT The ID and PID of the USB device are filled in the ID. Assume that our VID and PIDs of our USB devices are 16-based 0471 and 1801. Then click "Next>". For the regulations for VID and PID, please refer to USB-IF Specifications. Figure 35. In the next dialog box (Figure 4), we need to join the definition of endpoint 1 and endpoint 2. Since endpoint 0 must exist in USB, we do not need to define EndPoint 0 Point "Add ..." button, pop up a dialog box shown in Figure 5. We modified it as shown in Figure 6. Among them, according to USB regulations, for endpoints, its address is 1; according to front The characteristics of the device are characterized, the maximum package size of EndPoint 1 is 16 bytes, so it is filled in "MAX TRANSER SIZE"; EndPoint Name can be obtained by "suggest name". Follow these principles, continue to set other configurations, To make the dialog 4 becomes as shown in Figure 7. Next, continue press the "Next>" button. Figure 4 Figure 6 Figure 6 Figure 76. In the dialog shown in Figure 8, it can be filled in the need. Driver class and file name. Generally we do not need to change. Continue to press the "Next>" button. Figure 87. In the dialog shown in Figure 9, it is not necessary because there is no need to provide an interface to other drivers. Provide a Flush function, so you don't need any modifications, press the "Next>" button directly. Figure 98. In the dialog shown in Figure 10, we select the code to generate the Bulk READ, and press the "Next>" button. DW will give us a set of code to endpoint 2. You can use it directly without modification. Figure 109. In the dialog box as shown in Figure 11, we choose to generate the Bulk Write code to endpoint 2, and And press the "next" button. In this way, DW will also generate a set of code written to endpoint 2. You can use it directly without modification. Figure 1110. For dialog box as shown in Figure 12, we press "Next" directly "Buttons. Here is the setting to queue I / O requests, here, we don't need to queue. Figure 1211. In the dialog shown in Figure 13, we do not need to create any registry key, so press" Next> button. Figure 1312. Dialog box shown in Figure 14, which is to set some of the properties of some drivers, such as interfaces, buffers. Generally, you can use default settings. Continue to press "Next" "Button. Figure 1413. In the dialog shown in Figure 15, let's add some IOCTL interfaces to the driver. We only add an ioctl as shown in Figure 16 to control the LED lights of the USB device. And press" " Next> button. Figure 15 Figure 1614. In the last one shown in Figure 17, some drivers can be set, generating a CONSOLE test program. Press the "Finish" button to end Wizard. Figure 17. We create a basic driver. Let's take a look at what you have to do can communicate with our devices and the upper-level appDevice :: test_iocTl_Lled_Handler (Kirp i) is changed to the following Side of the surface:

NTSTATUS TESTDevice :: TEST_IOCTL_LED_Handler (KIrp I) {NTSTATUS status = STATUS_INVALID_PARAMETER; t << "Entering TESTDevice :: TEST_IOCTL_LED_Handler," << I << EOL; __ try {// TODO: Verify that the input parameters are correct // If not , return STATUS_INVALID_PARAMETERif (!! I.IoctlOutputBufferSize () || I.IoctlBuffer () || (I.IoctlInputBufferSize () = sizeof (UCHAR))) __ leave; // TODO: Handle the the ZBUARD_IOCTL_LED_ON request, or // defer the processing of the IRP (ie by queuing) and set // status to STATUS_PENDING.PURB pUrb = m_Lower.BuildVendorRequest (NULL, // transfer buffer0, // transfer buffer size0, // request reserved bits (UCHAR) (* (PUCHAR) I.ioctlBuffer (), // Request. 1 = LED_ON, 0 = LED_OFF0 // Value); // TransmitStatus = M_Lower.Submiturb (Purb, Null, NULL, 5000L);} __ finally {// Todo: Assuming That That Request Was Handled Here. Set I.information // TO INDICATE HOW MUCH DATA TO COPY Back to the user.i.information () = 0; I.Status () = status;} Return Status;} This function is to control LED lights , It is through USB Vendor Requale EST is transmitted to the device. Among them, when Request = 1 indicates that the LED is bright, and the request = 0 will let the LED are destroyed. It passes the DeviceIoControl by the upper application. Look at the reading and writing, after checking NTSTATUS TESTDEVICE:: Read (Kirp i) and NTSTATUS TESTDEVICE:: WRITE (Kirp i) can find that DW has written to us a read-write code, we can use it directly. These code is the code generated in steps 8 and 9 above. Finally, modify the test program Test_Test program generated by DriverStudio, we can test our drivers through the command line. For LED control, we can visually see on the device, but you need to cooperate with the Firmware program for read and write operations, which has exceeded the scope of this article, not discussed here. Through the above explanation, we can see that there is DriverStudio, you can quickly generate a driver, and then do some small changes in it. Even if you write a more complex USB driver, we can also use some system IRP processing, as long as you focus on our own specific applications. And it summarizes a driver into a few classes, and DW also comes with some very useful STL classes, with a very clear and intuitive representation in VC IDE.

转载请注明原文地址:https://www.9cbs.com/read-19644.html

New Post(0)