Linux USB:打开和关闭电源?

Mar*_*son 60 linux macos usb

如何以编程方式启用和禁用Linux上特定USB端口的电源?这样的事情甚至可能吗?Mac的回答也很受欢迎!

我正在尝试一个BOC(不要假装你也没有试图获得一个!)并最终得到其中一个,并希望通过将其连接到我们的服务器监视器来获得一些用处.

mix*_*nic 24

在Linux中有一个sys条目.来自Documentation/usb/power-management.txt:

能量等级

This file contains one of three words: "on", "auto",
or "suspend".  You can write those words to the file
to change the device's setting.

"on" means that the device should be resumed and
autosuspend is not allowed.  (Of course, system
suspends are still allowed.)

"auto" is the normal state in which the kernel is
allowed to autosuspend and autoresume the device.

"suspend" means that the device should remain
suspended, and autoresume is not allowed.  (But remote
wakeup may still be allowed, since it is controlled
separately by the power/wakeup attribute.)
Run Code Online (Sandbox Code Playgroud)

就像是: echo on > /sys/bus/usb/devices/usb5/power/level

您可能还需要使用自动暂停设置.在没有告诉内核停止尝试的情况下,它可能会自动挂起端口.

祝好运!

  • 从内核 v2.6.32 开始,挂起选项已被删除 - 请参阅此答案中链接的文档 (2认同)

Ste*_*ski 15

自从这个问题最初被回答以来,usbfs交互似乎已经改变了很多次.所以,这就是我如何从Bash shell循环Ubuntu Oneiric Ocelot上的集线器端口电源.

搜索总线和设备号:

sudo lsusb -v|less
Run Code Online (Sandbox Code Playgroud)

使用总线和设备编号在总线/集线器端口层次结构中找到设备:

sudo lsusb -t|less
Run Code Online (Sandbox Code Playgroud)

语法似乎是'bus-port.port.port.port.port ...'例如,我的鼠标连接到外部集线器,该集线器连接到我的计算机集线器,该集线器内部连接到根集线器:

/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/2p, 480M
    |__ Port 1: Dev 2, If 0, Class=hub, Driver=hub/6p, 480M
        |__ Port 1: Dev 3, If 0, Class=hub, Driver=hub/3p, 480M
            |__ Port 1: Dev 6, If 0, Class=HID, Driver=usbhid, 1.5M
Run Code Online (Sandbox Code Playgroud)

所以,在上述情况下,"2-1.1.1".最后,循环端口电源:

echo '2-1.1.1'|sudo tee /sys/bus/usb/drivers/usb/unbind
sleep 1
echo '2-1.1.1'|sudo tee /sys/bus/usb/drivers/usb/bind
Run Code Online (Sandbox Code Playgroud)

我没有连接协议分析器来查看总线上实际发生了什么,但我知道当我取消绑定时我的鼠标指示灯会熄灭.我猜测在较低层,这与EHCI主机控制器交互,实际上关闭了端口上的电源.这对于嵌入式设备尤其有用,例如UVC网络摄像头,它似乎永远不能正常工作,否则需要重启系统才能重置.

另请参见udevadm命令.


小智 7

通过书签进行挖掘

http://blog.andrew.net.au/2009/01/01#usb_power_control

您似乎需要将其连接到集线器并控制集线器的电源.我见过的根集线器似乎都不能支持电源控制.


mvp*_*mvp 5

您可以使用uhubctl - 命令行实用程序来控制兼容USB集线器的每个端口的USB电源.

它仅适用于支持每端口电源切换的集线器,但请注意,许多现代主板都具有支持此功能的USB集线器.此外,uhubctl的最新版本支持USB 3.0集线器,好消息是有相当多的新USB 3.0集线器支持此功能.

编译:

git clone https://github.com/mvp/uhubctl
cd uhubctl
make
Run Code Online (Sandbox Code Playgroud)

要列出uhubctl可以控制的所有集线器和端口的状态:

uhubctl
Run Code Online (Sandbox Code Playgroud)

要关闭单个兼容集线器的端口5的电源:

uhubctl -a 0 -p 5
Run Code Online (Sandbox Code Playgroud)

要为所有兼容集线器的所有端口打开电源:

uhubctl -a 1
Run Code Online (Sandbox Code Playgroud)

要关闭电源然后再打开电源:

uhubctl -a 2 -p 5
Run Code Online (Sandbox Code Playgroud)

在这里阅读更多.

披露 - 我是uhubctl的作者.