Electronic – How does a computer (or USB device) detect that there is a new/lost connection

usb

I have two computers, a USB keyboard and a USB mouse. I want to build something that lets me switch the USB devices between the two computers. My approach would be to buy a new powered USB hub and plug the keyboard and the mouse into it. Then, I would use a microcontroller to control transistors for the DATA- and DATA+ lines to the two PCs (so four transistors total). I would just short all the GND wires (from the powered hub and the PCs) – that shouldn't do any harm, right? – and leave the +5V wires from the PCs unconnected. As far as I can see, that should work fine for switching.

However, how would the PCs (or the powered hub?) know when to do a new handshake and so on? Is it sufficient to have the +5V wire to the USB hub high by default and pull it to GND while switching? Would the USB hub initiate a handshake or something when its USB +5V wire goes up?

Also, how would the other PC find out that it has just lost the connection? By checking whether there's a voltage between Data+ and Data-?

Best Answer

What you're looking for is called a "KVM switch" (keyboard, video and mouse), you just don't need the video part. These are commonly available and easy to find once you know the correct term.

But to your specific question about USB enumeration: Every USB device has a pullup resistor on one of the Data lines (one for low-speed devices, the other for all others). A USB host or hub recognizes the presence of this resistor and initiates enumeration when it appears.

Related Topic