挂起笔记本计算机时,如何关闭鼠标的USB电源?


19

睡觉时,鼠标的LED灯使我神魂颠倒。我通常在晚上关闭笔记本电脑,然后打电话给Ubuntu pm-suspend。无论如何,仍然可以为USB插头供电。我什至可以给智能手机充电。这对我来说很不好,原因有两个。我必须拔掉鼠标的电源,笔记本电池[akku]的使用寿命要短一些。当笔记本计算机进入挂起模式时,是否可以自动关闭USB设备或所有USB设备的电源?

如何配置呢?

编辑:根据注释中的要求,我添加此信息:我正在使用Lenovo G550。

编辑:根据要求在这里我的lsusb输出

Bus 002 Device 019: ID 1058:25a3 Western Digital Technologies, Inc. 
Bus 002 Device 017: ID 1b1a:7001  
Bus 002 Device 009: ID 2109:2812 VIA Labs, Inc. VL812 Hub
Bus 002 Device 008: ID 2109:2812 VIA Labs, Inc. VL812 Hub
Bus 002 Device 003: ID 0402:5608 ALi Corp. 
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

编辑:我认为这些信息也导致答案。

root@localhost:/sys/bus/usb/devices# ls -al
insgesamt 0
drwxr-xr-x 2 root root 0 Okt  6 10:56 .
drwxr-xr-x 4 root root 0 Okt  6 10:56 ..
lrwxrwxrwx 1 root root 0 Okt  6 10:56 1-0:1.0 -> ../../../devices/pci0000:00/0000:00:1a.7/usb1/1-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-3 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-3
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-3:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-3/2-3:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-3:1.1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-3/2-3:1.1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.1/2-5.1:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.1.2 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.1/2-5.1.2
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.1.2:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.1/2-5.1.2/2-5.1.2:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.1.2:1.1 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.1/2-5.1.2/2-5.1.2:1.1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.2 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.2
lrwxrwxrwx 1 root root 0 Okt  6 10:56 2-5.2:1.0 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2/2-5/2-5.2/2-5.2:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 3-0:1.0 -> ../../../devices/pci0000:00/0000:00:1a.0/usb3/3-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 4-0:1.0 -> ../../../devices/pci0000:00/0000:00:1a.1/usb4/4-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 5-0:1.0 -> ../../../devices/pci0000:00/0000:00:1a.2/usb5/5-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 6-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6/6-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 6-1 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6/6-1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 6-1:1.0 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 6-1:1.1 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 6-1:1.2 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6/6-1/6-1:1.2
lrwxrwxrwx 1 root root 0 Okt  6 10:56 7-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.1/usb7/7-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 8-0:1.0 -> ../../../devices/pci0000:00/0000:00:1d.2/usb8/8-0:1.0
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb1 -> ../../../devices/pci0000:00/0000:00:1a.7/usb1
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb2 -> ../../../devices/pci0000:00/0000:00:1d.7/usb2
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb3 -> ../../../devices/pci0000:00/0000:00:1a.0/usb3
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb4 -> ../../../devices/pci0000:00/0000:00:1a.1/usb4
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb5 -> ../../../devices/pci0000:00/0000:00:1a.2/usb5
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb6 -> ../../../devices/pci0000:00/0000:00:1d.0/usb6
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb7 -> ../../../devices/pci0000:00/0000:00:1d.1/usb7
lrwxrwxrwx 1 root root 0 Okt  6 10:56 usb8 -> ../../../devices/pci0000:00/0000:00:1d.2/usb8

编辑:

tokam@localhost:~$ lsusb -t
/:  Bus 08.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 07.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 06.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
    |__ Port 1: Dev 2, If 0, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 1: Dev 2, If 1, Class=Human Interface Device, Driver=usbhid, 12M
    |__ Port 1: Dev 2, If 2, Class=Human Interface Device, Driver=usbhid, 12M
/:  Bus 05.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 04.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 03.Port 1: Dev 1, Class=root_hub, Driver=uhci_hcd/2p, 12M
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M
    |__ Port 3: Dev 3, If 0, Class=Video, Driver=uvcvideo, 480M
    |__ Port 3: Dev 3, If 1, Class=Video, Driver=uvcvideo, 480M
    |__ Port 5: Dev 51, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 52, If 0, Class=Hub, Driver=hub/4p, 480M
            |__ Port 2: Dev 54, If 0, Class=Human Interface Device, Driver=usbhid, 1.5M
            |__ Port 2: Dev 54, If 1, Class=Human Interface Device, Driver=usbhid, 1.5M
        |__ Port 2: Dev 53, If 0, Class=Mass Storage, Driver=usb-storage, 480M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-pci/6p, 480M

8
我不认为此睡眠收费功能与Ubuntu有关。Windows和Ubuntu在笔记本电脑上为我完成此操作。相反,应该在BIOS中进行设置以在睡眠期间关闭USB端口的电源。
TheWanderer's

1
你确定吗?Google向我显示了一些有关“ usbsuspend ubuntu”关键字的信息。但是我还不确定100%如何正确地做。 hamwaves.com/usb.autosuspend/en askubuntu.com/questions/80638/...
tokam

1
好吧,您可以禁用它们,但这是Ubuntu本身正在运行的时候。如果您想让脚本在笔记本电脑休眠以挂起端口之前运行,则可以,但是至少检查一下该选项的BIOS设置会更容易。
TheWanderer '16

1
不幸的是,我的BIOS不支持该选项。
tokam

1
@tokam,您可以编辑问题并添加笔记本电脑的品牌和型号吗?您还可以确认它不是USB共享端口吗,如此处所述:dell.com/support/article/ro/ro/robsdt1/SLN155147/EN(闪电图标)
user.dz

Answers:


4

当前带有OP链接,后续注释和建议答案的绊脚石是产品ID是静态的,1b1a:7001但总线和设备号一直在变化。

解决方案

在目录中创建一个脚本(任意名称)/etc/pm/sleep.d/,并将以下内容放入其中:

#!/bin/bash
ZeroBUS=$(lsusb | grep 1b1a:7001 | cut -c  5-7 )

# Strip leading zeros
BUS=$(echo $ZeroBUS | sed 's/^0*//')

# Build "usbX" usb number
USB=usb$BUS

case $1 in
     suspend|suspend_hybrid|hibernate)
     echo "Powering off: " $USB
        echo $USB | sudo tee /sys/bus/usb/drivers/usb/unbind
        ;;
     resume|thaw)
        # No need to do anything here, kernel unsuspends USB devices
        # Show how to power on for interest sake but since device is
        # powered off the usb number will be blank.
        echo "Powering on: " $USB
        echo $USB | sudo tee /sys/bus/usb/drivers/usb/bind
        ;;
esac

将文件标记为可执行文件,sudo chmod +x file_name其中“ file_name”是您选择的名称。

说明

该解决方案关闭了整个USB集线器的电源,在我的情况下,这意味着电话,无线鼠标,无线键盘等。从终端调用脚本时,需要输入sudo密码。希望在从systemd sudo调用时继承。我无法测试,因为我没有有线鼠标。因此,对于sudo功率可能需要其他改进。

当BIOS提供恒定的5V电源时,不会物理切断电源,而是告诉总线上的设备自行关闭电源。在我的情况下,无线键盘和鼠标停止工作,不得不求助于笔记本电脑键盘和触摸板以重新打开电源。

您可以通过调用脚本并传递参数“ suspend”来手动测试。传递参数“ resume”不会完成任何操作,因为设备已关闭电源,没有设备ID可以将其重新打开。


更轻松的解决方案关闭所有USB端口的电源

我最近遇到了一个问题,其中未知端口阻止笔记本电脑挂起。我找到了我修改过的解决方案(代码中的信用)。

/lib/systemd/system-sleep/custom-xhci_hcd使用sudopowers 创建文件并插入以下代码:

#!/bin/bash

# Original script was using /bin/sh but shellcheck reporting warnings.

# NAME: custom-xhci_hcd
# PATH: /lib/systemd/system-sleep
# CALL: Called from SystemD automatically
# DESC: Suspend broken for USB3.0 as of Oct 25/2018 various kernels all at once

# DATE: Oct 28 2018.

# NOTE: From comment #61 at: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/522998

TMPLIST=/tmp/xhci-dev-list

# Original script was: case "${1}" in hibernate|suspend)

case $1/$2 in
  pre/*)
    echo "$0: Going to $2..."
    echo -n '' > $TMPLIST
          for i in `ls /sys/bus/pci/drivers/xhci_hcd/ | egrep '[0-9a-z]+\:[0-9a-z]+\:.*$'`; do
              # Unbind xhci_hcd for first device XXXX:XX:XX.X:
               echo -n "$i" | tee /sys/bus/pci/drivers/xhci_hcd/unbind
           echo "$i" >> $TMPLIST
          done
        ;;
  post/*)
    echo "$0: Waking up from $2..."
    for i in `cat $TMPLIST`; do
              # Bind xhci_hcd for first device XXXX:XX:XX.X:
              echo -n "$i" | tee /sys/bus/pci/drivers/xhci_hcd/bind
    done
    rm $TMPLIST
        ;;
esac

在使用非动态echo命令实际挂起笔记本计算机之前,可以以某种方式测试脚本吗?
tokam

您可以成功隔离lsusb中也显示的总线和设备,但是正如您指出的那样,目录不存在,如问题所示,目录的结构与$ {BUS}-$ {DEVICE}不同,即使是知道哪一个是正确的,悬浮式的作品也无法确定
tokam 16-10-16

我拔出并重新插入鼠标的测试有助于识别文件夹。但是请看下一个测试的输出:Keine Berechtigung的意思是:没有权限root @ localhost:〜#echo暂停> /sys/bus/usb/devices/2-5.1.2:1.1/power/level -bash:/ sys /bus/usb/devices/2-5.1.2:1.1/power/level:Keine Berechtigung
tokam

鼠标的外部thouse处有一个LED,底部有一个激光器。通过提供两个答案的组合,我能够关闭鼠标的激光,但不能关闭LED,这不会增加我的睡眠环境的光线。也许我们不能通过内核关闭LED?我在隔离的鼠标USB插头上进行了测试。
tokam

@tokam经过大量的研究和测试,我终于使它可以在我的系统上手动运行,但是没有鼠标可以对其进行测试。希望它能工作!
WinEunuuchs2Unix

5

通过发出命令lsusb(插入和不插入鼠标)来查找鼠标的总线和设备号,您需要一个仅在插入时才存在的命令。记下这些数字。在您的情况下,它们似乎是6和6

发出命令sudo cp /etc/pm/sleep.d/novatel_3g_suspend /etc/pm/sleep.d/mouse_suspend(我们必须这样做,因为gedit当以超级用户身份启动时,不再具有“另存为”选项。)

发出命令gksu gedit /etc/pm/sleep.d/mouse_suspend更改总线和设备号以匹配您之前记下的编号。保存结果文件。当您挂起笔记本电脑时,它将执行。

在您的情况下,应将BUS = X和DEVICE = Y更改为与该鼠标匹配的总线和设备号。这将一直有效,直到将设备移至其他端口,在这种情况下,您将不得不再次定位设备并相应地调整文件。

BUS=X
DEVICE=Y

if [ ! -x /sys/bus/usb/devices/${BUS}-${DEVICE}/power/level ]; then
    exit 0
fi

case $1 in
     suspend|suspend_hybrid|hibernate)
    echo suspend > /sys/bus/usb/devices/${BUS}-${DEVICE}/power/level
        ;;
     resume|thaw)
    # No need to do anything here, kernel unsuspends USB devices
    :
        ;;
esac

文件中的注释(以#开头的行)无关紧要,可以放心地忽略它们。

注意:我通过Suspend在台式机上对其进行了测试,并且其效果与广告中所述相同。我无法使用您拥有的相同硬件,但是我必须通过电源按钮来恢复系统,因此,我相当确定这对您有用。

根据评论和问题扩展进行编辑:

看来这是您需要进行的信息: Bus 006 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver


在我的geditUbuntu 16.04从14.04的原始版本升级中,菜单Save as下有一个选项File。您的配置必须发生了某些事情???PS很好的答案,我稍后再试。
WinEunuuchs2Unix

我也使用Ubuntu Mate保存了文件。
tokam

@ WinEunuuchs2Unix有趣。它似乎不存在于全新安装中。也许这是一个新问题的基础。……
极客长老

@ElderGeek Save As如果键入,gedit我将获得“文件编辑视图搜索工具文档帮助”的整个顶行菜单的更新,但是如果键入gksu gedit,则不会出现菜单列表。HTH。
WinEunuuchs2Unix

1
@tokam是否可以使用平时日常使用的所有设备的输出lsusb -tlsusb插入的设备来更新您的问题。这可能会帮助我们可视化中心,而不会。
WinEunuuchs2Unix
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.