Disconnecting a USB device without unplugging it

11,091

Solution 1

The best way should be to switch the 5V and leave everything else connected. Otherwise you would have to make sure that at least the ground line is connected before the data lines. That's what the standard plugs do when plugging in – the power contacts are longer than the data contacts and connect first.

An important part to know is that the USB upstream (host or hub) recognize a connected device by a pull-up resistor connected by the device to one of the data lines. The device recognizes an active upstream by the presence of the 5V supply.

Now let's look at what happens when switching. There are two cases.

For the bus powered device, it will lose power when the 5V is switched off. That causes the pull-up to drop. No pull-up → upstream sees a disconnect and the host's USB stack will handle that properly. The upstream has weak pull-downs on the data lines so once it stops driving the data lines because it believes the device to be disconnected there will be no voltage above ground on any of the lines still connected. No need to disconnect the data lines therefore.

A self powered device on the other hand will keep running when the bus 5V disappears. That can happen without any switching shenanigans on any regular computer, namely when it powers off or is sent into suspend. The device will recognize a disconnect of the upstream. In that case, the USB standard requires the device to switch off the pull-up on the data line to prevent a steady leakage current from flowing into a powered down upstream. In our case of the 5V switch, the disconnect of the pull-up will be recognized by upstream as a device disconnect. Both upstream and device are still powered but believe they are disconnected from each other.

Reconnecting the 5V will in both cases be recognized as a plug in event when the pull-up appears on the relevant data line.

Solution 2

Are you disconnecting only the 5V or 5V + data lines?
You will need to disconnect both to be sure of a clean removal.

  • A device with only power removed may still operate or operate abnormally in some cases.
  • A device with only data disconnected may leave a 'crashed' peripheral still running rather than resetting it.

In terms of cost and effort it would be easier to add a USB hub at the front of the PC and/or where it is accessible and connect that to the rear sockets. Compared to buying relays and switches and connecting them the hub would be cheaper and easier and more certain to work.

USB2 operates at about 500 Mbps. There is substantial opportunity to decrease system reliability if you add switches and additional wiring asymmetrically to the data lines. Adding capacitance will decrease the maximum allowable physical distance - but that's not liable to be the main issue.

Opinion: I'd expect that switch bouncing is not liable to be a problem. The standard plug and socket can probably achieve a degree of bounce and it's likely that the protocol is designed top be relatively immune to short term bounce.

Share:
11,091

Related videos on Youtube

dodo
Author by

dodo

Updated on September 18, 2022

Comments

  • dodo
    dodo almost 2 years

    I'm modifying my desktop computer case and I want to plug everything into the USB ports in the back of it. However, the ports are not easy to reach and sometimes I have to unplug some devices because the operating system acts funny in certain cases.

    So I thought of this: I want to connect a plug to an USB port, connect the +5V line to a rocker switch, and then collect all the lines into a socket, to which I will plug my device. Now, here is my problem.

    • Would I be limited by cable length due to capacitance etc? I don't want to harm the computer or its USB port by making a stupid mistake.

    • Would I need to disconnect all lines? In that case a set of relays seem much more logical. However, passing the data lines over relays could distort the signals maybe. I don't know anything about high speed cables. Do you think this could affect communication?

    • Also, in case of disconnecting all the lines, in what order should I connect/disconnect the lines?

    • Will I get in trouble because of the bouncing from the switch? I guess the USB connectors also have some bouncing but the USB system is probably engineered for their bouncing characteristics.

    Edit: Thank you all for your answers, I originally asked this question on the electronics forum of the same network but somebody migrated it to here, sorry if I wasted your time.

    A hub would definitely work, but I want to hide the cables and the ports as much as possible, so it's not a solution I'd prefer. This is a case mod after all, aesthetics is somewhat important also.

    The problem with the operating system isn't easily solved, i.e. by ejecting. The OS initializes my Bluetooth dongle in a weird way if it's plugged in during bootup and then I have to recycle it before I can use it. That's one of the devices I'd like to switch. The other one is a webcam which I occasionally use and rather not have connected all the time.

    • Admin
      Admin over 10 years
      What? Your question is really difficult to understand what is your problem or what you want to archive, and has some affirmation that is not true. Just the first point is more "answerable" Yes ofcourse you have limit on cable length, but that depends on a factor of variables, in general 2 meters considering high-speed devices.
    • Admin
      Admin over 10 years
      But just for starting, I don't know why you want to "power" devices out by a mechanic switch. First you need to discover (at least try) why you get in troubles with the devices connected. Are not you trying to get more current than the USB can supply? If so, you should use other USB host (put the front port header to the back, normally its other host controller), or a powered USB hub, but this would reduce bandwidth.
    • Synetech
      Synetech over 10 years
      What kind of USB devices are you talking about? Media can be logically disconnected by simply ejecting them. Are you talking about keyboards and other things?
  • Admin
    Admin over 10 years
    A device with only data disconnected may leave a 'crashed' peripheral still running rather than resetting it. Reference please? It can or not present any problem if it is a "fast removal device", like a pen drive without cache, but even removing the power this still be a concern.
  • Admin
    Admin over 10 years
    If the problem is being excessive current draw from the USB Host plugging a USB powered hub will add more problems than solving.
  • dodo
    dodo over 10 years
    Thank you, this was the kind of information I was looking for. As I understand it, if I simply switch on/off the +5V line, the host controller will handle it gracefully and there is no magic like the controller still providing some current thru data lines (when the switch is off) since they are pulled to ground. I don't think that having the +5V line about a meter longer than the rest will create a problem but I'm new to electronics and everybody is talking about the capacitance of cables etc, which I don't understand. Any thoughts about that?