当前位置: 首页>>技术教程>>正文


当我暂停笔记本电脑时,如何关闭鼠标的 USB 电源?

, , , ,

问题描述

睡觉时鼠标的 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

最佳办法

当前 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 端口

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

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

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

次佳办法

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

文件中的注释(以 # 开头的行)不相关,可以安全地忽略。

注意:我通过桌面上的“暂停”对此进行了测试,它的工作原理与广告中的一样。我无法访问与您相同的硬件,但我必须通过电源按钮恢复系统,所以我相当确定这对您有用。

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

看来这是您继续操作所需的信息:\n Bus 006 Device 006: ID 046d:c52b Logitech, Inc. Unifying Receiver

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14389.html,未经允许,请勿转载。