# Turn USB port on/off

## neked

Hi all,

I am trying to write a script to turn a USB port on and off... I am using the USB port to power a device that needs to be occassionally reset, so the idea is to cut power off the USB and then back on. How do I do that with Shell programming? Furthermore, I have many USB ports on my computer, how do I know which port I am controlling?

I've been doing some searching in the linux file system, and found this file /sys/bus/usb/devices/usb1/power/state which seem like it says whether a USB port is on or off. The problem is I know for sure that there is at least one USB port that is on, yet all the power/state files have the value 0, does that mean the power is "on" on all the devices?

Thanks,

Neked

----------

## luckyman

so after some search i found this:

The kernel offers an experimental option to enable suspension of USB devices through driver calls or one of the power/state files in /sys

Device Drivers

  USB support

    [*]   Support for Host-side USB

      [*]   USB selective suspend/resume (EXPERIMENTAL)

the detail in kernel says

  │ CONFIG_USB_SUSPEND:                                                                 │

  │                                                                                     │

  │ If you say Y here, you can use driver calls or the sysfs                            │

  │ "power/state" file to suspend or resume individual USB                              │

  │ peripherals.                                                                        │

  │                                                                                     │

  │ Also, USB "remote wakeup" signaling is supported, whereby some                      │

  │ USB devices (like keyboards and network adapters) can wake up                       │

  │ their parent hub.  That wakeup cascades up the USB tree, and                        │

  │ could wake the system from states like suspend-to-RAM.

and this I found about a patch on one forum

Access to those calls through sysfs, such as

  echo -n 2 > power/state

  echo -n 0 > power/state

That can be used to reduce the power consumption of any given USB device,

then re-activate it later.  Eventually, most USB device drivers should

probably suspend idle USB devices.

One problem with this patch:  USB drivers without suspend() callbacks

may badly misbehave.  Right now only hub drivers know suspend().  If the

driver core didn't self-deadlock when we try it, unbinding those drivers

from those devices (then re-enumerating on resume) would be perfect...

the current compromise is just to emit a warning message.

In conjunction with host controller driver support (already merged for

OHCI and EHCI), PCI host controllers will issue the PME# wakeup signal

when a USB keyboard starts remote wakeup signaling.  (But the keyboard

wasn't usable later, since HID doesn't try to suspend.)

I understand some ACPI patches are circulating, and maybe already in

the MM tree, to make a suspended system wake up given PME# signaling.

It'd be great if someone made that work transparently with USB, but

for now I'm told it'll need some sysfs setup first.

that's all i can help for now, see how you get on

----------

## neked

 *luckyman wrote:*   

> 
> 
> the detail in kernel says
> 
>   │ CONFIG_USB_SUSPEND:                                                                 │
> ...

 

Thanks luckyman for your answer, I have tried using the "echo -n 0 > power/state" method, but it would not work. I think that means probably that the kernel is not configured to to suspend the USB. The question is, how do you reach the "CONFIG_USB_SUSPEND" detail? do you have to recompile the kernel?

----------

## yabbadabbadont

Yes, you will have to recompile the kernel.  It is one of the kernel options under the usb section.

----------

## neked

Hi again,

I recompiled the kernel with CONFIG_USB_SUSPEND turned on, and I tried doing echo on the power/state file but to no avail. Any ideas?

Thanks,

neked

----------

## gregy

Hi,

I have a question regarding USB. Is it possible to control power (5V) in the usb port? Here http://www.gniibe.org/ac-power-by-usb/ac-power-control.html it says it is if you have hub with per port power control and I have one. But unfortunetaly I was not able to use the code sample there becouse I am not skilled enough and it requires changes (I think) to work on newer kernels. Anyone interested in this and able to help?

----------

## desultory

Merged the above post.

----------

