使用 Linux 控制 USB 电源(开/关)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4702216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:09:05  来源:igfitidea点击:

Controlling a USB power supply (on/off) with Linux

linuxusb

提问by kamziro

Is it possible to turn on/off power supplies from USB manually with Linux?

是否可以使用 Linux 手动打开/关闭 USB 电源?

There's this external USB cooling fan (the kind you use to cool yourself off, not the PC), and it would be nice to be able to control it from the terminal, because I want to position the fan somewhere far away.

有这个外部 USB 冷却风扇(您用来冷却自己的那种,而不是 PC),能够从终端控制它会很好,因为我想将风扇放置在远处。

I suppose this could also be useful for a variety of other things as well, because there's a lot of USB toys out there. Maybe air purifiers, etc. (I heard they don't really work though).

我想这对其他各种事情也很有用,因为那里有很多 USB 玩具。也许是空气净化器等(我听说它们实际上不起作用)。

采纳答案by Roman Cheplyaka

Note.The information in this answer is relevant for the older kernels (up to 2.6.32). See tlwhitec's answerfor the information on the newer kernels.

笔记。此答案中的信息与较旧的内核(最高 2.6.32)相关。有关较新内核的信息,请参阅tlwhitec 的回答

# disable external wake-up; do this only once
echo disabled > /sys/bus/usb/devices/usb1/power/wakeup 

echo on > /sys/bus/usb/devices/usb1/power/level       # turn on
echo suspend > /sys/bus/usb/devices/usb1/power/level  # turn off

(You may need to change usb1 to usb n)

(您可能需要将 usb1 更改为 usb n

Source: Documentation/usb/power-management.txt.gz

来源:文档/usb/power-management.txt.gz

回答by Carles

I wanted to do this, and with my USB hardware I couldn't. I wrote a hacky way how to do it here:

我想这样做,而我的 USB 硬件却做不到。我在这里写了一个如何做到这一点的hacky方式:

http://pintant.cat/2012/05/12/power-off-usb-device/.

http://pintant.cat/2012/05/12/power-off-usb-device/

In a short way: I used a USB relay to open/close the VCCof another USB cable...

简而言之:我使用 USB 继电器打开/关闭另一条 USB 电缆的 V CC......

回答by tlwhitec

According to the docs, there were several changes to the USB power management from kernels 2.6.32, which seem to settle in 2.6.38. Now you'll need to wait for the device to become idle, which is governed by the particular device driver. The driver needs to support it, otherwise the device will never reach this state. Unluckily, now the user has no chance to force this. However, if you're lucky and your device can become idle, then to turn this off you need to:

根据文档,内核2.6.32对 USB 电源管理进行了几处更改,这些更改似乎已在2.6.38 中解决。现在您需要等待设备变为idle,这由特定的设备驱动程序管理。驱动需要支持,否则设备永远不会达到这个状态。不幸的是,现在用户没有机会强制执行此操作。但是,如果您很幸运并且您的设备可能处于空闲状态,那么要关闭它,您需要:

echo "0" > "/sys/bus/usb/devices/usbX/power/autosuspend"
echo "auto" > "/sys/bus/usb/devices/usbX/power/level"

or, for kernels around 2.6.38 and above:

或者,对于 2.6.38 及更高版本的内核:

echo "0" > "/sys/bus/usb/devices/usbX/power/autosuspend_delay_ms"
echo "auto" > "/sys/bus/usb/devices/usbX/power/control"

This literally means, go suspendat the moment the device becomes idle.

这字面意思是,在设备空闲时暂停

So unless your fan is something "intelligent" that can be seen as a device and controlled by a driver, you probably won't have much luck on current kernels.

因此,除非您的风扇是可以被视为设备并由驱动程序控制的“智能”设备,否则您在当前内核上可能不会有太多运气。

回答by grandrew

I have found these solutions that at least work for properly configured Terminus FE 1.1 USB hub chip:

我发现这些解决方案至少适用于正确配置的 Terminus FE 1.1 USB 集线器芯片:

1.To turn off power on all USB ports of a hub, you may unbind the hub from kernel using:

1.要关闭集线器所有USB端口的电源,您可以使用以下命令解除集线器与内核的绑定:

echo "1-4.4.4" > /sys/bus/usb/drivers/usb/unbind

to turn power back on - you may bind it back using

重新打开电源 - 您可以使用

echo "1-4.4.4" > /sys/bus/usb/drivers/usb/bind

2.Switching power at each port individually is trickier: I was able to use hubpowerto control each port - but it comes with a downside: hubpower first disconnects the usbdevfs wich causes all of the USB devices to disconect from system, at least on ubuntu:

2.在每个端口单独切换电源比较棘手:我能够使用hubpower来控制每个端口 - 但它有一个缺点:hubpower 首先断开 usbdevfs 导致所有 USB 设备与系统断开连接,至少在 ubuntu 上:

usb_ioctl.ioctl_code = USBDEVFS_DISCONNECT;
rc = ioctl(fd, USBDEVFS_IOCTL, &usb_ioctl);

With this ioctldisabled I was able to switch off individual port power without detaching all devices - but the power goes back on immediately (probably due to kernel seeing an uninitialized device) which causes USB device just to do a "cold restart" which is what I generally wanted to do. My patched hubpower is here

ioctl禁用此功能后,我可以在不分离所有设备的情况下关闭单个端口电源 - 但电源会立即恢复(可能是由于内核看到未初始化的设备),这导致 USB 设备只是执行“冷重启”,这就是我一般想做。我的补丁集线器在这里

回答by user3527264

echo '2-1' |sudo tee /sys/bus/usb/drivers/usb/unbind

works for ubuntu

适用于 ubuntu

回答by Dominic Cerisano

PowerTOPfrom Intel allows you to toggle devices such as usb peripherals in real-time. These are called 'tunables'.

英特尔的PowerTOP允许您实时切换 USB 外围设备等设备。这些被称为“可调参数”。

sudo apt install powertop
sudo powertop
  • Tab over to 'tunables'.
  • Scroll down to your device.
  • Hit enter to toggle power saving mode (Good/Bad)
  • 切换到“可调参数”。
  • 向下滚动到您的设备。
  • 按回车键切换省电模式(/

enter image description here

在此处输入图片说明

Note that Badmeans the device is always on. Toggling to Goodwill turn off the device after the preset inactive saving time (default is 2000ms).

请注意,“坏”意味着设备始终处于开启状态。切换到Good将在预设的非活动保存时间(默认为 2000 毫秒)后关闭设备。

See the PowerTOPdocs for details on how to make these changes permanent.
It generates the config scripts for you (pretty much as described by other posters on this thread).

有关如何使这些更改永久化的详细信息,请参阅PowerTOP文档。
它为您生成配置脚本(与此线程上的其他海报非常相似)。

NOTE: These scripts do not affect USB pin power (which is always on).
These only send the driver protocol to activate and deactivate a device.

If you want to control pin power, you could use either a supported smart USB hub, or better yet a microcontroller.

注意:这些脚本不会影响 USB 引脚电源(始终开启)。
这些只发送驱动程序协议来激活和停用设备。

如果您想控制引脚电源,您可以使用受支持的智能 USB 集线器,或者更好的微控制器

回答by mvp

You could use my tool uhubctlto control USB power per port for compatible USB hubs.

您可以使用我的工具uhubctl来控制兼容 USB 集线器的每个端口的 USB 电源。

回答by NoBugs

USB 5v power is always on (even when the computer is turned off, on some computers and on some ports.) You will probably need to program an Arduino with some sort of switch, and control it via Serial library from USB plugged in to the computer.

USB 5v 电源始终打开(即使在某些计算机和某些端口上关闭计算机时也是如此。)您可能需要使用某种开关对 Arduino 进行编程,并通过插入到 USB 的串行库来控制它计算机。

In other words, a combination of thisswitch tutorial and thistutorial on communicating via Serial libary to Arduino plugged in via USB.

换句话说,开关教程和教程的组合通过串行库与通过 USB 插入的 Arduino 进行通信。

回答by WonderLand

I had a problem when connecting my android phone, I couldn't charge my phone because the power switch on and then off ... PowerTop let me find this setting and was useful to fix the issue ( auto value was causing issue):

我在连接我的 android 手机时遇到问题,我无法为手机充电,因为电源打开然后关闭...... PowerTop 让我找到了这个设置并且对解决这个问题很有用(自动值导致了问题):

echo 'on' | sudo tee /sys/bus/usb/devices/1-1/power/control

回答by F1Linux

The reason why folks post questions such as this is due to the dreaded- indeed "EVIL"- USB Auto-Suspend "feature".

人们之所以提出这样的问题,是因为可怕的——确实是“邪恶” ——USB自动暂停“功能”。

Auto suspend winds-down the power to an "idle" USB device and unless the device's driver supports this feature correctly, the device can become uncontactable. So powering a USB port on/off is a symptom of the problem, not the problem in itself.

自动挂起会关闭“空闲”USB 设备的电源,除非设备的驱动程序正确支持此功能,否则设备可能无法连接。因此,打开/关闭 USB 端口是问题的症状,而不是问题本身。

I'll show you how to GLOBALLYdisable auto-suspend, negating the need to manually toggle the USB ports on & off:

我将向您展示如何全局禁用自动挂起,无需手动打开和关闭 USB 端口:

Short Answer:

简答:

You do NOT need to edit "autosuspend_delay_ms" individually: USB autosuspend can be disabled globally and PERSISTENTLYusing the following commands:

您不需要单独编辑“ autosuspend_delay_ms”:可以使用以下命令全局和持久地禁用 USB 自动挂起:

sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/&usbcore.autosuspend=-1 /' /etc/default/grub

update-grub

systemctl reboot

An Ubuntu 18.04screen-grab follows at the end of the "Long Answer" illustrating how my results were achieved.

一个Ubuntu的18.04屏幕抓取遵循在“龙回答”如何说明我的成绩均达到结束。

Long Answer:

长答案:

It's true that the USB Power Management Kernel Documentationstates autosuspend is to be deprecated and in in its' place "autosuspend_delay_ms" used to disable USB autosuspend:

确实,USB 电源管理内核文档声明自动挂起将被弃用,取而代之的是“ autosuspend_delay_ms”用于禁用 USB 自动挂起:

"In 2.6.38 the "autosuspend" file will be deprecated
and replaced by the "autosuspend_delay_ms" file."

HOWEVERmy testing reveals that setting usbcore.autosuspend=-1in /etc/default/grubas below can be used as a GLOBALtoggle for USB autosuspend functionality- you do NOTneed to edit individual "autosuspend_delay_ms" files.

无论其我的测试显示设置usbcore.autosuspend=-1的/ etc /默认/ grub中,如下可以作为一个全球切换为USB autosuspend functionality-你就不要需要编辑个人“ autosuspend_delay_ms”文件。

The same documentlinked above states a value of "0" is ENABLEDand a negativevalue is DISABLED:

上面链接的同一文档指出值“0”是启用负值禁用

power/autosuspend_delay_ms

    <snip> 0 means to autosuspend
    as soon as the device becomes idle, and negative
    values mean never to autosuspend.  You can write a
    number to the file to change the autosuspend
    idle-delay time.

In the annotated Ubuntu 18.04screen-grab below illustrating how my results were achieved (and reproducible), please remark the default is "0" (enabled) in autosuspend_delay_ms.

在下面带注释的Ubuntu 18.04屏幕抓取中,说明了我的结果是如何实现的(和可重现的),请注意autosuspend_delay_ms 中的默认值为“0”(启用)。

Then note that after ONLYsetting usbcore.autosuspend=-1in Grub, these values are now negative (disabled) after reboot. This will save me the bother of editing individual values and can now script disabling USB autosuspend.

然后请注意,在 Grub 中ONLY设置后usbcore.autosuspend=-1,这些值现在在重新启动后为负(禁用)。这将为我省去编辑单个值的麻烦,现在可以编写禁用 USB 自动挂起的脚本。

screengrab: autosuspend values Before and after globally editing

screengrab:全局编辑之前和之后的自动挂起值

Hope this makes disabling USB autosuspend a little easier and more scriptable-

希望这能让禁用 USB 自动挂起更容易,更容易编写脚本-