I want to design a prioritized reverse USB hub, where a USB flash drive is connected by default to a device but switches to another when a connection is present.
The setup of my product uses a Raspberry Pi and a USB flash drive inside a box. On the outside of the box I have 2 USB ports, one for power and the other for data, neither of them directly connect to the Raspberry.
By default, the USB flash drive is connected to the Raspberry, but I when I connect a USB cable to the data port of the box, the Raspberry notices it and switches the USB flash drive from it to the USB data port of the box. When I remove the cable, the Raspberry switches again the USB flash drive to one of its internal USB ports.
The schematic for this is the following:
So, I read here in stack exchange that the 4 USB pins must be switched and not only the D+ and D- pins. These are switched via a FSUSB30 USB switch IC, on the top of the schematic. By default, even if USB_SEL is a high impedance, the switch connects the D+D- pins of USB_PEN_DATA (Flash drive) to the D+D- pins of USB_INT_DATA (RPi USB).
For the power side (this is where I have my doupts), I use a two MOSFET IC to switch GROUNDS. Basically, I read that two computers may have slightly different 5V between the VCC and GND of its USBs, so two USB ports of different POWERED devices should not be connected together. Ok, so I just connected the positive terminals in a "VCC_USB_COMMON" net, and I switch the ground of the USB flash drive "MAGIC_USB_GROUND" to the RPi ground "GND" or to the computer ground "PE".
The grounds of the computer and the RPi are isolated through the use of a schmitt trigger output optocoupler that drives the MOSFET that switches the computer side PE ground. On the RPi side, the ground is switched directly with RPI_GPIO_PIN_OUT_X.
Finally, for the RPi to know if a USB cable is connected to the external data port, I've put a second optocoupler that has the IR LED powered by the computer and the schmitt trigger powered by the Raspberry and its output is read by an IO pin "RPI_GPIO_PIN_IN_X".
This all seems like overenginnering, but I think it gives me perfect controll over the timings of the switching of data and power lines. Somewhere I also read that the order mattered.
Is this circuit sound? I didn't check for the typical resistor values yet, I just put something that I thought would work as a ballpark value, but I will check that in detail if this circuit can do what I want. Also, decoupling caps aren't present but they will be on the final design. Are ferrite beads REALLY necessary? I'm only going to require USB2.0 at 480Mbps so...
EDIT: Ok, if I understood correctly from Ali Chen's answer, I am overthinking things and only D+ and D- should be switched. The updated circuit is as follows: