解决方案

解决方案

睡觉时,鼠标的 LED 灯让我很烦。我通常在晚上合上笔记本电脑,Ubuntu 就会来电pm-suspend。无论如何,USB 插头仍然有电。我甚至可以为智能手机充电。这对我来说很糟糕,原因有二。我必须拔下鼠标,而且笔记本电脑的电池组 [akku] 续航时间更短。当笔记本电脑进入挂起模式时,有没有办法自动关闭我的 USB 设备或所有 USB 设备?

如何配置?

编辑:根据评论中的要求,我添加了以下信息:我正在使用联想 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

答案1

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使用权力创建文件sudo并插入以下代码:

#!/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

答案2

通过发出命令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

答案3

我最终对@WinEunuuchs2Unix 的脚本做了一些小修改

case $1 in
    find_port)  
        ZeroBUS=$(lsusb | grep $2 | cut -c  5-7 )
        # Strip leading zeros
        BUS=$(echo $ZeroBUS | sed 's/^0*//')

        if [ -z "$BUS" ]; then
               echo "port not found"
               exit 1
        fi

        # Build "usbX" usb number
        USB=usb$BUS
        echo "port found:" $USB $2
        ;;
    off)
        echo "Powering off: " $2
        echo $2 | sudo tee /sys/bus/usb/drivers/usb/unbind
        ;;
    on)
        echo "Powering on: " $2
        echo $2 | sudo tee /sys/bus/usb/drivers/usb/bind
        ;;
esac```

相关内容