10
\$\begingroup\$

I know google's answer and wikipedia's answer for the above question. But I have a more specific question on hand. USB hubs have many incoming and port and just one outgoing port, I am able to use say n-number of devices together because of this. But, I do not understand how can one USB port do data transfers with n-number of USB ports?
how can it send different data to all USB ports at the same time?

*

schematic

simulate this circuit – Schematic created using CircuitLab

*.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Pretty much the same way that your network router can connect one LAN port (the internet) to multiple devices. Each endpoint (USB device) is assigned an address, and the hubs simply route requests from the host to the correct device by keeping track of what address is on what port. It's much simpler for USB in the sense that there is only one master (the host port) which initiates all of the transactions. \$\endgroup\$ – Tom Carpenter May 29 '16 at 3:34
  • \$\begingroup\$ @TomCarpenter Then How would the USB port send different data's together? \$\endgroup\$ – Aaditya Sahay May 29 '16 at 3:35
  • 1
    \$\begingroup\$ The host port asks a specific endpoint to talk. Endpoints only talk back when asked to by the host. \$\endgroup\$ – Tom Carpenter May 29 '16 at 3:36
  • \$\begingroup\$ @TomCarpenter But they all are being used simultaneously and not one by one, but all together. \$\endgroup\$ – Aaditya Sahay May 29 '16 at 3:37
  • 3
    \$\begingroup\$ Remember that the USB bus can run at up to 480Mbps on USB 2.0. The host only ever talks to one endpoint at a time, but it talks to all of them sequentially and switching between them so fast you couldn't tell. The same thing networks do. "Hey, mouse on port 1, tell me if you've moved. Ok now keyboard on port 2 have you got any key presses to report? Now you there on port 3, flash drive, store this data for me. Anyone else I need to talk to? nope, ok then, mouse on port 1, tell me if you've moved..." \$\endgroup\$ – Tom Carpenter May 29 '16 at 3:40
19
\$\begingroup\$

It's all to do with arbitration. Any system which requires multiple devices to be connected needs some way of determining who should talk when. There are different schemes as you would expect depending on the application.

A common example - in networking we have many nodes all talking to each other. This is done by each node having an address (e.g. IP address), and when a node wants to talk to another node, it sends out a packet to that address. You then have devices such as routers which take packets coming in on multiple ports and forward them on to the correct port. The arbitration is done using memory to store packets until the destination port is free.


Now on to USB. This is actually much simpler than networking because not all nodes are made equal. You have two sorts, a host, and an endpoint. There is only ever one host, but can be many endpoints. In this case arbitration is much easier because only the host port is allowed to talk at will. Endpoints are only allowed to talk when asked to by the host, and the host only ever talks to one endpoint at a time.

For host->endpoint packets, the USB hubs simply pass the request from the host to all of the endpoints. Because all endpoints have an address, only the one to which the request was addressed will do anything with it (e.g. respond), all others will ignore the packet.

For endpoint->host packets, the host first sends a packet to a specific endpoint by address to say "you can talk now", and then that endpoint must immediately send a response. Because only one endpoint is allowed to talk at any given time, the USB hub will simply route the packet from whichever port responds to a request from the host.


In terms of how the host works out what devices are attached, and how endpoint get their address, this is achieved through enumeration.

All host and hub ports have pull-down resistors (15kOhm) on the D+ and D- lines. These put the data lines of that port into a known state when there is no device attached, a state in which the port will not send any data over D+/D- lines at all.

When a device is attached, it makes itself known by connecting either the D+ (full-speed) or D- (low speed) data line to VCC using a 1.5kOhm resistor. This triggers an enumeration event. The port will then begin the process of configuring the device and assigning an address. If you were to plug in two devices simultaneously, they will be enumerated one at a time.

If there are no hubs, the host simply talks to the new device and sets it up. If there are hubs in the system, it is the hub which reports the new device is attached. If a hub reports a new device is connected, the host will instruct the hub to reset the new device and start up communications. During the reset, the endpoint is given a default address of 0 (*). The host can then talk to the endpoint using the default address, and configure it with a unique non-zero address that will allow it to know when it is being talked to.

(*) Because only one device is ever enumerated at a time, the address 0 will always be unique to the newly attached device.


You might then ask, "well how can I then have multiple devices all talking at the same time?". Say you have a mouse, a keyboard, and a flash drive all connected to the same USB hub. We all know you can use your mouse and keyboard at the same time while also copying files to/from your flash drive, but if only one device can talk at a time, how can that be possible?

Well, it all comes down to the fact that the few hundred milliseconds it takes for your brain to notice that you have pressed a key and expect the screen to update is an eternity to the computer. A USB 2.0 interface can run at up to 480Mbps (USB 3.1 can run at up to 10Gbps!), which means that even though the host is only ever talking to one endpoint at any given time, it cycles between them so fast that you can't tell it's doing it.

USB Host: "Hey, mouse on port 1, tell me if you've moved. Ok now keyboard on port 2 have you got any key presses to report? Now you there on port 3, flash drive, store this data for me. Anyone else I need to talk to? nope, ok then, mouse on port 1, tell me if you've moved..."

Human: "Oh look, the computer noticed I just moved my mouse, pressed a key on my keyboard, and copied a picture to the flash drive, all at the same time!"

The host device keeps track of which endpoint addresses are used and will send packets to each one sequentially or as needed (i.e. when the OS request access to a specific device). So while it is not all happening simultaneously, the arbitration is so fast that the computers pet human can't tell the difference.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ To add: USB devices (only “interrupt” endpoints, to be precise) can request to be polled at a certain interval down to 1 ms. As long as you don’t have hundreds of input devices you’ll never notice a delay. \$\endgroup\$ – Michael May 29 '16 at 7:22
  • \$\begingroup\$ How does the host know which endpoints are connected? Also, this doesn't actually seem to answer the original question about hubs. How do they interact in this process? How do the interact in enumeration? \$\endgroup\$ – Yona Appletree Jun 21 '18 at 17:08
  • 1
    \$\begingroup\$ @YonaAppletree enumeration. Hosts and hubs scan all of their ports for device connections - they detect a 1.5kOhm pull-up resistor on either D+ (Full Speed) or D- (Low speed). I'll add some info on how endpoints are assigned an address. \$\endgroup\$ – Tom Carpenter Jun 21 '18 at 19:48
  • \$\begingroup\$ @YonaAppletree in terms of hubs during operation, the answer does talk about it already. For host to endpoint: "USB hubs simply pass the request from the host to all of the endpoints". For endpoint to host: "Because only one endpoint is allowed to talk at any given time, the USB hub will simply route the packet from whichever port responds " \$\endgroup\$ – Tom Carpenter Jun 21 '18 at 20:04
1
\$\begingroup\$

Shorter answer: the host sends data that are addressed to a particular device (which was preliminary "enumerated"), one transaction at a time, sequentially. The hub broadcasts all the packets to all devices. A device responds only to transactions that are addressed to it. That's all, true for HS devices.

For FS and LS devices the process is a little bit more complicated. It uses "transaction translators" that are built in every hub for every port, who translate so-called "split transactions" into LS or FS traffic.

\$\endgroup\$
2
  • \$\begingroup\$ What about USB 3? \$\endgroup\$ – jbarlow Jul 26 '16 at 7:31
  • \$\begingroup\$ USB3 hubs are more intelligent for Super-Speed link, and use explicit routing to device/endpoints. USB2 section stays the same. See Section 3.1 "Architectural Summary" of most recent USB3.1 specifications, usb.org/developers/docs/usb_31_052016.zip \$\endgroup\$ – Ale..chenski Jul 26 '16 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.