I was reading on how USB hubs work and since there is only 1 master, master sends a command to an endpoint. However, how does a USB hub work when there are 2 same devices plugged into a USB hub? How does the device know which is being addressed?
USB has a process called “enumeration” where each device, including hubs, is assigned an 7-bit number used to identify it to the host. When a hub is attached, it is enumerated and then each device downstream is enumerated.
This is the reason that no more than 127 devices may be attached at any time.
Therefore, each of your identical devices receives a different number. Which one gets what number depends on the order they are enumerated.
If you need to programmatically tell the difference, you must use the devices’ serial numbers, if they have them.
You can distinguish them by the port they are connected to, otherwise they will appear identical. The way we distinguish between two devices is with a serial number in the embedded software, if the embedded software has no distinguishing fields you can read, then you can only distinguish them by port.
Reading the port is dependent on the software of the operating system you are using.
I think the question comes from not very-well described behavior of USB hubs when various devices are connected to its downstream ports. The process is as follows:
Initially the hub is in reset state and all downstream (DFP, downstream facing port) ports are disabled. So any traffic coming to upstream port of the hub from host (most packets in HS hubs are broadcasted, and can be visible on all root ports) is initially blocked to DFPs, and no device sees any activity. Then the hub gets "enumerated" by receiving its own unique device address. The DFPs are still disabled.
Each DFP however has a hardwired ability to detect connect event (D+ or D- gets pulled up by device). The connect status gets reported to USB host via a dedicated hub control pipe (the hub is already enumerated and has an assigned address). So the host knows that something is connected, but it doesn't know yet what it is.
Upon getting hub status, the host enables only one DFP at a time, starting in arbitrary order (usually the lowest port that reports new connection).
Then the host starts to communicate with the connected device using so-called "default control pipe", at endpoint 0 address 0. Every device must have one ready after power-up or reset. Since there is only one new port enabled, only one device will be responding. All other devices already would have individual addresses assigned to them, and therefore wouldn't respond to this default (0,0) pipe.
In the process of enumeration this device receives new available device address, and will stop responding to default pipe. All communication with this particular device will go to this new address.
The Host then enables the next port that reports the connected status, and repeats steps (4 - 5).
The host repeats step (6) until all ports with connect status are enabled and all devices behind them are enumerated (got individual device addresses), so the host can address their pipes individually.
This concludes the device enumeration behind hub's DFPs. In the process each device ends up with unique device address, and host knows who is who and where to address it, even if the devices are physically identical.