USB device Driver Development remote Access USB device (i)

Source: Internet
Author: User
Tags virtual environment

by Fanxiushu 2016 05-15 reprint or quote this article, please indicate the original author.


People who have used VMware should know that VMware virtual machines have such a feature,
When a USB device is plugged into the host, the USB device can be accessed in VMware's virtual machine system via setup.
and accessing this USB device is just as true as inserting the USB device into this virtual system, almost indistinguishable from the real one.
Looking at a situation, suppose that there are two machines C and s,c machines that you are using, the S machine at the far end, you can only control s by remote.
s machine configuration and function are very powerful, most of the time you are connected to the S machine through Remote Desktop and so on.
If you have some USB interface devices, such as IPHONE,IPAD,USB cameras, you want to use them.
C machine at your side, you can and can only plug these USB devices into the C machine, but you must be very keen to insert these devices, S machine can also be used normally.
The way to remotely use a more powerful S-machine is what is now known as cloud desktops, virtual cloud desktops and the like.
Therefore, for virtual cloud Desktop developers, the solution of remote access to local devices is also one of the basic and important issues.
Look at an example that is unfamiliar to the average person and familiar to iOS developers,
iOS app installation problem, very annoying, unlike Windows programs, as long as the development, can be copied everywhere, run everywhere.
Self-developed app, need to deploy the Xcode development environment to the mobile phone, before doing such things, still have to spend money to buy accounts, upgrade to Xcode7 only slightly open some.
It's also handy to deploy to your phone through Xcode, but how to deploy it to someone else's phone, and that person is not in the same place and can't get its phone directly to the computer.
So, the remote way to achieve Xcode deployment, the first thing to solve is the problem of remote access to USB,
It is for this reason, but also want to master the USB device driver, only to start research and development of USB device driver, to try to achieve such a function.
(Of course there are other better ways to implement the IOS app Beta, I'm just a little more than to deploy apps through Xcode,
My MacOS is installed into a VMware virtual machine and accesses the host's USB interface via VMware Virtual USB
Try to virtualize the USB device in the Windows host, and then try to turn the virtual USB into MacOS with VMware,
I hope this idea will come true eventually, I don't want to do the USB drive of MacOS System.

The following is a discussion of USB device-driven development based on the Windows platform.

USB is just an interface, which is the protocol interface for data exchange between the device and the host. Data exchange is no more than two directions, from the device to the host and from the host to the device.
Everyone said that various USB devices, in fact, have a USB interface to achieve their own specific functions of the hardware device,
For example USB camera, first this hardware is the camera, it is connected to the computer via USB interface.
The various hardware features of the USB interface are not discussed here, nor is it the scope of software development.

Windows driver according to the kind to divide, roughly divides into the bus drive, the function drive, the filter drives three kinds.
USB is a hardware interface and must be disconnected from the lowest-level bus driver.
The bus driver is responsible for managing all devices connected to a certain type of bus, such as a USB bus,
It is responsible for monitoring the insertion and removal of devices on the bus, creating a PDO (physical device object) for the device, and notifying the PNP manager of new hardware additions, and so on.
Then look at the USB bus, in fact, on the computer basic bus (such as PCI bus, etc.) should have one or more USB controller,
On the hardware, it is the controller chip integrated into the motherboard.
Each USB controller has a unique Roothub (root hub), and the root hub has multiple ports, simply speaking, the USB jack that is seen on the computer.
So we can simply interpret the USB bus driver as USB controller driver and Roothub driver,
Since the USB controller is always first discovered, the next Roothub device is given to the USB controller driver for processing,
Then the Roothub driver then manages its own port and the USB device connected to the port.

Port on the root hub can not only connect to a real USB device, but it can also
Each sub-hub can be connected to a real USB device, or then the grandson hub, which forms a tree rooted in Roothub,
Thanks to this structure, each USB controller can manage up to 127 USB devices (theoretically).
When we plug the USB device into the Roothub, or the sub hub, or the grandson hub, these hubs will report the device insertion notification.
In fact, the USB controller polls these hub port states for notification,
The Roothub driver is responsible for creating the PDO (physical device object) for this device, and notifies the PNP manager that a new device is added.
The PNP Manager is responsible for loading the device's corresponding function driver according to the device's information.
When the corresponding USB device function driver is successfully loaded, the real USB interface communication is started.
For the function driver, it only needs to send the data directly to the PDO created by the Roothub driver to complete the communication.

The so-called function driver is what this USB device is for, such as a USB camera, or a USB keyboard.
function driven on the bus driver above, the USB interface protocol is a standard common protocol, generally with USB interface devices, the underlying communication is the same.
This is the ability to achieve remote sharing of various USB device Foundation.

USB function drives the use of URB (USB Request BLOCK) in communication under the Windows platform,
(This urb is not described in the previous articles in disk-driven communications using the SRB is very similar, almost engraved in the same mold)
Windows has helped us achieve most of the USB bottom-up communication content,
We only need to construct the appropriate URB packet to interact with the USB device (this type of URB package is about 20).
Send the Irp_mj_internal_device_control command directly to PDO,
The command contains the URB package to complete the data interaction with the USB device.

While the PDO of the USB device, after receiving the URB Irp_mj_internal_device_control command,
Start a real data communication with a USB device at the physical level,
It had to transfer the URB data to the Roothub device, Roothub to the real device.
As for how to complete the communication process, specific to the hardware processing process.
This is not what this article discusses unless you want to implement a real USB controller driver.

How to implement remote access USB device?
Through the above simple introduction, should have a general understanding of the USB communication process,
We only need to intercept a USB communication data in the USB bus layer and forward the data to the remote machine via the network.
Virtual USB device on the remote machine, and then input the data to the virtual device, so this is the virtual dongle can be correctly recognized and used.
And because the interception and processing of the USB interface is the underlying data, so that all USB interface devices can be correctly identified,
That is, if the camera is a USB interface, the remote machine's virtual USB is also recognized as the same camera,
If it is a USB keyboard, the remote machine's virtual USB is also considered a USB keyboard.
The principle is not complex, you have to develop a virtual bus driver, virtual USB bus driver to simulate the virtual USB device,
This is similar to the virtual disk that was introduced earlier,
Unfortunately for virtual disk drives, Microsoft offers a dedicated Scsiport or Storport module to perform similar functions.
USB virtual devices Drive our own development of USB bus drivers.
Also described above, USB bus drivers include USB device controller driver and Roothub driver,
According to the level, USB controller Management Roothub,roothub management USB device, USB is the lowest level of employees,
If you really want to follow the real hardware level to achieve virtual USB system, can be too much.
Fortunately, because it is a virtual USB device, rather than the real hardware, in a virtual environment, it is possible to simplify some of the unnecessary things.
You can remove the controller and Roothub, just a virtual USB device,
Although this is not available for some special programs, most of them can be used normally.
(As for how to implement virtual USB controller and virtual Roothub at the same time according to the real hardware level, the following chapters will introduce)

The framework of bus-driven development here does not introduce too much, the following chapter describes how to deal with virtual USB devices, will do some explaining.
You can see the toaster example code in the WDK driver example in detail,
WDK7 provides examples for WDM and WDF, WDK8 and WDK10 have removed WDM, providing only WDF code,
If you want to study what the bus driver is doing, it is best to look at his WDM example, and later on the virtual USB drive provided on the CSDN,
is also the use of WDM development of the project, and is not a copy of the toaster example of WDM in the above to fill their own code,
Instead, the code framework was re-organized according to your own habits.

To access the USB device remotely, first we have to divide it into two modules,
First of all, the acquisition of real USB device data acquisition end, for convenience, hereinafter referred to as the acquisition end or service side.
Second, in the remote virtual out of the USB device, and then enter the collected data, complete the USB device access, hereinafter referred to as the client or virtual USB terminal.

In order to collect USB data, it took some trouble, the first thought is the filter drive to collect USB device data.
We first look at the USB interface communication, the USB interface is used for data communication, communication mode is one of its core content.
A total of 4 communication methods:
One, control transmission, mainly to send various control commands to the USB device, mainly using the default port 0 for transmission, any USB device once connected to the host,
Host will give him a default port of 0, otherwise it will not be able to communicate with the host.
Second, interrupt transmission, see the name as if it is a real hardware interrupt, otherwise, the USB interrupt is a pseudo-interrupt, in fact, the USB controller timed to query the USB state,
See the flag marked as broken, and then the data transfer, the corresponding speed of interruption, it depends on the USB controller polling speed.
Third, bulk transmission, as the name implies, is a large data transmission, for very large data transmission sites, such as U disk.
Four, synchronous transmission, this transmission mode I am more puzzling, the host to a large block of memory, and then set up some chunks (packet),
Each chunk is set at the offset of this large block of memory and the read and write length of each chunk,
Then to the USB device, USB device according to packet, while populating each chunk of data, some chunks may not transmit data or only a subset of data,
This is the synchronous transmission allows, it is not guaranteed to complete the transmission of data, mainly for USB video and other requirements compared to the real-time but relatively low data quality situation,
Like a USB camera.

No matter what kind of transmission, the USB interface communication is always initiated by the host,
Even if the data is transferred from the device to the host, it is still the host that first initiates the transfer command to the USB device,
The USB device then fills in the data to the buffer provided by this command initiated by the host.

At first, I wanted to mount the LowerFilters filter driver on the USB class driver, so that I could intercept all the USB data.
But in this way, I always feel that it is not right (because all URB packages are host-initiated, filtering-driven interception has no meaning for our needs).
Later understand that in fact all URB package, is the host initiative, then why simply do not find a USB device in the system created by the PDO device,
Send him a URB packet directly to collect the data. Thought so, and so did. The result data is collected, of course, the whole system to get blue screen.
Because when I send urb_function_select_configuration to re-select the configuration descriptor,
The driver of his own feature loaded into this USB device is still running, because sending the SELECT command forces the USB device to re-select the configuration descriptor,
The upper function driver does not know, still using its original configuration, the result of natural blue screen strike.
And we want to remotely access the USB device, that is, the remote virtual USB device completely control the local real USB, it must be used exclusively.
Otherwise, if a remote virtual USB device sends a control command that changes the state of the device, such as Select, the feature driver that is loaded on top of the real USB device will not work.

Understand this truth, finally know how to collect USB device data.
is to develop its own function driver for USB devices, and let him replace the original real function driver.
Self-developed this feature-driven, processing all URB packets;
According to USB request data which is transmitted through the network, the URB packet is sent to the USB device for data transmission processing.

As for the USB function driver development, for us, the most important and the only thing to do is how to handle the USB four data transmission methods.
In this example, the WDK example code also provides examples of bulk transfer and synchronous transmission, which we need to assemble and use.
You can also look for application layer-level solutions, but the first thing to think about is WINUSB,
This is called the application layer of the USB drive, in fact, the USB four transmission mode encapsulated to the application layer to the unfamiliar driver development of the programmer to use.
But it is a pity that the old WINUSB does not support synchronous transmission, and the system above win8.1 starts to support synchronous transmission.
If you really use winsub, you can now use a large number of WIN7,WINXP users.
There is also open source Libusb, this project is good, he is mainly active in the Linux platform, also has the corresponding Windows version,
It is also a pity that although he handled the synchronous transmission, but for the synchronous transmission need to pass the Cell block (packet), it is not implemented.
But a general and bulk transmission, just provide a large memory, for our virtual USB device, according to the information returned by packet,
Determine the transmission of each small chunk. Of course, you can modify the appropriate small, let libusb complete such a function.

I am the driver of my own development, of course, thanks to the excellent code provided by LIBUSB, concise and easy to understand.
Otherwise it will not be so soon to master and develop their own drivers to handle the USB device data collection.

The function of USB data acquisition is developed, but there is a very big trouble, how to load our own driver into a variety of USB devices,
Let Windows replace the original driver, and switch back to the original driver when we no longer use our drivers.

Come back and see how the PNP Manager can load a feature driver for a device,
When the bus drive is enumerated to a device that has been plugged in, the PDO is created, and the call to the IoInvalidateDeviceRelations function notifies the PNP manager that the device list has changed,
PNP Management sends irp_mn_query_device_relations to the bus driver to query all PDOs lists, and compares the old and new lists, knowing that a PDO is being added
The PNP manager then sends IRP_MN_QUERY_ID to the PDO to query the hardware ID,
After querying to the hardware ID, the PNP Manager searches the registry for drivers that are already installed under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum,
He looks for the subkey under the enum based on the hardware ID, and then starts loading the driver after it is found. Could not find the driver corresponding to the compatible ID.
If none are found, the prompt to install the driver will pop up.

Obviously, we simply fill in the positive hardware ID in our own feature-driven INF installation file and can be loaded correctly.
However, our goal is to let our drive, can be loaded on the top of various USB devices, the hardware ID of each USB device is different,
It is impossible to create an INF installation file for each device.
Maybe we can generate a USB-compatible ID for our INF,
But the PNP manager first queries the hardware ID, if the driver of the corresponding hardware ID is not installed, we can find our driver and install it,
However, most USB devices are drivers, some of which are owned by Windows, and most of them can be identified by Windows and have their own drivers installed.

How to solve this problem, it is best to find a solution at the application layer,
Fortunately there is VMware, although VMware does not provide source code, but it has done this effect, can be found in its programs in the clues.
The VMware installation directory has a Vmware-usbarbitrator64.exe program that looks at the name of the program that handles USB,
Use depends to see which WIN32 APIs the program uses, and finally, Vmware-usbarbitrator64.exe imported setupapi.dll dynamic libraries,
Using a function setupdisetselecteddriverw, this is definitely about how to install the driver,
Then with Google all over the search for the function of the relevant connection, and finally found http://www.google.com/patents/US8825909.
(Chinese users need to turn over the wall to access), originally they have solved such a problem, incredibly also applied for a patent, it is conceivable that they pay attention to intellectual property.
The general principle is to give the original device hardware ID, in the registry to add our own development of the feature-driven hardware ID (This hardware ID can be arbitrary, as long as it is unique on the line),
Then we use the SetupAPI dynamic library function to reconstruct the driver list, when our own driver is in his list and then uses Setupdisetselecteddriver,
The Setupdisetselecteddevice,installselecteddriver function dynamically loads our drive.
How to add our hardware ID to the registry, the main use of the CM_ADD_ID function, in fact, this function is used at the bottom of the Setupdisetdeviceregistryproperty function.
Setupdisetdeviceregistryproperty you can set the hardware ID using the Spdrp_hardwareid parameter,
Originally this function in Win7,winxp, even win8 can work well, to the WIN10, was banned by Microsoft, not allowed to set the hardware ID,
However, this also means that the hardware ID for each device is unique, do not allow arbitrary modification.
But it's hard for me to find another way to solve the problem of dynamically loading your own drive under WIN10.
Perhaps the VMware approach to do a little bit of modification, but also in win10 normal use, the general idea is to change the hardware ID,
This will trick the PnP manager into loading the drivers we provide.
In writing this article, I was busy researching and developing the functions of virtual USB controller and virtual Roothub,
So there's no time to find out how to solve the problem of dynamic installation drive under WIN10
Anyway, the use of http://www.google.com/patents/US8825909 said the method has been able to deal with the problem under the Win7 normal, and so on later time to solve.

The following chapter continues the development of virtual USB devices, so stay tuned for some of the source code available later on CSDN.
Not to be continued ...

USB device Driver Development remote Access USB device (i)

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.