3
\$\begingroup\$

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-?

\$\endgroup\$
  • 2
    \$\begingroup\$ The USB standard is freely available and well worth reading if you're going to attempt this. \$\endgroup\$ – pjc50 Apr 26 '13 at 17:51
  • \$\begingroup\$ Hey, let's replace Stack Exchange with one huge banner with RTFM written on it, shall we? :J This will end all those silly nonsensical questions... \$\endgroup\$ – SasQ Dec 7 '19 at 15:41
5
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ There are also plain USB switch boxes which have no video functionality and might be cheaper than a full KVM switch. \$\endgroup\$ – Kevin Reid Apr 27 '13 at 15:51
  • \$\begingroup\$ Does the Usb hub raises an interrupt? How does the hub notifies about the new USB? \$\endgroup\$ – KYHSGeekCode Mar 8 '19 at 7:48
  • 1
    \$\begingroup\$ @KYHSGeekCode: The USB host is constantly polling for status, every millisecond, IIRC. \$\endgroup\$ – Dave Tweed Mar 8 '19 at 12:22
2
\$\begingroup\$

USB uses polling to enumerate, and detect when a device is lost. If the usb slave device does not respond in a certain time frame, the usb connection is lost, even if the device is not unplugged.

See usbtiny and v-usb for software usb implementations for the amtel brand microcontrollers.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

You can use a FSUSB30MUX. It has 2 x SPST switches to switch the data lines between two ports.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

As you most certainly know, USB uses two data lines, D+ and D–, in differential mode (that is, they switch opposite to each other).

The USB host controller has 15kΩ pull-down resistors on both these data lines, pulling them low when nothing is connected.

When you connect a device, you are supposed to pull [i]one[/i] of those data lines up with a 1.5kΩ resistor, which overpowers the 15kΩ one and pulls that particular data line up, and the host controller uses this to notice that the device has been connected. This also tells the controller whether it is the low-speed device (if the D– data line is pulled up) or the full-speed device (if the D+ data line is pulled up). Or in short: the "plus" one is faster, the "minus" one is slower ;)

(USB 2.0 uses some more complicated "chirping" signal to indicate that it is a high-speed device.)

Bringing both data lines down usually means end of transmission, or "over and out".

The host controller is the bus master here, so it initiates all data transfers and polls the devices. Devices are not allowed to speak unless the host asks them with a request. (There are a few exceptions, e.g. waking up from power saving mode etc.) So the host controller is constantly communicating with devices, sending them request every 1 millisecond (or faster for high-speed devices), so called "frames". If a device does not respond to any requests for a while, and its data lines are both low, it is considered to be disconnected.

|improve this answer|||||
\$\endgroup\$

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.