This idea has been in the head before, recently the company's products to use, so more to do some understanding.
1. Introduction to USB
USB is Universal Serial bus abbreviation, literally, is the meaning of universal serial bus. Physically, it is a pair of differential lines, connected to two devices, data transmission between each other. With the other two power supplies (5V and GND), there are altogether 4 lines.
So, since there is only a pair of differential lines, then how to decide who to pass to whom (if both sides of the line at the same time to establish level, the level on the line will be indeterminate, so that can not communicate)? This is an important basis for USB transmission: The "ask-answer" mechanism--device (slave) device is typically in a wait state, and only the host side device initiates an inquiry, request, it will use the data cable to send data to host in the next time slice.
So, who is HOST, who is SLAVE and what is the decision? The answer is hardware. In other words, if the drive chip behind your USB is host, then this USB can only be used as host. Conversely, SLAVE. As we often see, the USB HOST on the PC is connected to a USB stick, mouse, keyboard, these SLAVE devices.
Of course, some people later think that such a device can only be HOST or only SLAVE is too rigid, so invented the USB OTG. USB OTG (on-the-go, to the effect of switching identities in use) is based on the original 4 lines, plus a line, ID. The drive chip behind the USB can be used to choose whether to play host or SLAVE in this line: When this line is dangling, keep yourself as a SLAVE, and when the line is grounded, it switches itself to host. This feature is a great boon for devices with limited number of interfaces! Mobile phones, for example, now have basic Android support for OTG features. For such a mobile phone, you can choose to connect it to the PC with a USB cable, so that the phone will simulate itself as a USB flash drive, you can use a USB flash drive to copy the data on the phone, or you can directly use the OTG cable to connect the USB stick with the phone, directly using the mobile phone to operate the U disk.
In addition, because of the use of a pair of differential lines for data transmission, USB also uses a hub-based star topology (including the root controller, up to 7-layer topology, and 7 layers have no mount HUB capability, can only be functional devices). So, more specifically, "Host-slave" is the transport mechanism on top of the physical link supported by the HUB. At the same time, the HUB itself is a USB SLAVE device.
2. Overview of the USB connection process
A good explanation of the USB connection process. Note that 1) as shown in the topology, there can be only one HOST in each network. 2) between host and SLAVE, can be a pair has been connected, also can through the HUB, a host corresponding to multiple SLAVE.
Basic status includes: device insertion, device power-up, device reset, device acquisition address, device configuration complete, pending status . Here's a little explanation.
Device Insertion : The host side (either on the HUB or directly on the host controller) determines if a device is plugged in according to the impedance changes of the d+ and D-ports, and determines the type of speed at which the devices are inserted.
device Power on : Because the USB Port has a power supply function (5V DC, more than 500-1000 MA), the device is also divided into Bus powered and self powerd. Of course, even if the device is powering itself, we think it will only enter the powered state if the device is connected. Once the device is re-power on, the subsequent connection operation is performed again.
device Reset : Because only a pair of differential lines, if both sides of the operation, the line level will be indeterminate state, simply unable to communicate. Therefore, after power-on, USB device (slave) defaults to a wait state, without any action, until the HOST sends it a RESET request. Reset is done, which means that the low speed/full speed/high speed physical path has been established.
device get address : After the device reset is successful, an address assigned by HOST is obtained. The conversation assigned to the address will be completed on the device's endpoint0.
Device Configuration Complete : handshake? Configuration? This phase is complete anyway, which means the device is ready.
Pending status : All USB devices (slave) must suspend themselves and remain in their own state, regardless of whether the device has been assigned an address or no assigned address, after a period of inactivity.
3. OTG
OTG added a data cable that can be dynamically configured as HOST or DEVICE (slave), with a micro USB connector as an example with the following PIN assignments:
Because the traditional USB cable is 4 wires, the OTG device needs to be configured (hardware short) to access it:
1. When the OTG device is configured as a USB device (slave), the ID foot is left dangling.
2. When the OTG device is configured as a USB host, the ID foot is grounded.
So, here, we need to have OTG feet dangling to configure them as Slave devices. On top of the hardware, the OTG adapter purchased is set to HOST the OTG USB device by default, and their ID feet are grounded. And we need to use the OTG device as device (slave), so we finally chose to cut the ID line on the board and leave it dangling.
4. G_serial.ko
The current kernel 3.0.8 supports the Gadget Serial interface, that is, if we have a hardware USB SLAVE (which can be supported by OTG), this driver can enable us to implement a USB port on this USB channel; it's like having PL2303 or HM340 hard Supported by USB. Of course, our device is in the presence of USB slave, the driver support situation, this link looks like a normal serial connection. Its source code is in/DRIVERS/USB/GADGET/SERIAL.C, and there is also document Documentation/usb/gadget_serial.txt. You can read it yourself. (In fact, Google's ADB tools and this is the same thing, maybe even only the driver number is different.) )
For specific applications, we don't need to make too many changes ... Only need to configure, compile is enough. I've compiled it into module, so I need to do a modprobe after the filesystem is up.
As for the HOST side, it is said that there is no need to drive, but, I use on Windows, or installed gadget serial v2.4 (said not to support 64-bit system, not verified), UBUNTU Plug and Play.
Using USB to simulate ACM serial device under Linux