domingo, 20 de febrero de 2022

Preventing keyboard and mouse from waking up suspended computer

 Scenario

Laptop computer, Linux environment, external keyboard, monitor and mouse connected.

After suspension an insignificant movement wakes up the system. Sometimes mices have high resolution, so a very minor movement wakes up the laptop

ACPI

The first solution I found was via ACPI setup, but ditn't like it.

mabeett@nowhere:~$ cat /proc/acpi/wakeup
Device	S-state	  Status   Sysfs node
GLAN	  S4	*disabled
XHC	  S3	*enabled   pci:0000:00:14.0
XDCI	  S4	*disabled
HDAS	  S4	*disabled  pci:0000:00:1f.3
mabeett@nowhere:~$

udev device tailored made

So, again, udev might be my friend. The quick-fast way is generating rule for the device as mentioned in arch wiki.

mabeett@nowhere:~$ lsusb -v   | egrep -i  'Leno|mouse|keyb'
Bus 001 Device 007: ID 17ef:608d Lenovo 
  idVendor           0x17ef Lenovo
      bInterfaceProtocol      2 Mouse
Bus 001 Device 005: ID 17ef:6099 Lenovo 
  idVendor           0x17ef Lenovo
      bInterfaceProtocol      1 Keyboard

mabeett@nowhere:~$ cat 50-non-power-on-mouse.rules 
## mouse 
ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", ATTRS{idVendor}=="17ef", ATTR{idProduct}=="608d", ATTR{power/wakeup}="disabled"
# keyboard
ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", ATTRS{idVendor}=="17ef", ATTR{idProduct}=="6099", ATTR{power/wakeup}="disabled"

But I don't like it since I might change keyboard/mouse and with the change I should add a new rule. Then, I explored.

udev for HID devices

Forgetting the path for the device:

mabeett@nowhere:~$ dmesg  | grep Lenovo |  grep -i mouse | grep -P  "usb.+?:"
[    2.873048] usb 1-3.3: Product: Lenovo USB Optical Mouse
[    2.879777] input: PixArt Lenovo USB Optical Mouse as /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0/0003:17EF:608D.0005/input/input14
[    2.888588] hid-generic 0003:17EF:608D.0005: input,hidraw4: USB HID v1.11 Mouse [PixArt Lenovo USB Optical Mouse] on usb-0000:00:14.0-3.3/input0
mabeett@nowhere:~$ 

There it is a path for exploring dev with udevadm info -a -p ${PATH}

mabeett@nowhere:~$ udevadm info -a -p /devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0/0003:17EF:608D.0005/input/input14

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0/0003:17EF:608D.0005/input/input14':
    KERNEL=="input14"
    SUBSYSTEM=="input"
    DRIVER==""
    ATTR{properties}=="0"
    ATTR{uniq}==""
    ATTR{name}=="PixArt Lenovo USB Optical Mouse"
    ATTR{inhibited}=="0"
    ATTR{phys}=="usb-0000:00:14.0-3.3/input0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0/0003:17EF:608D.0005':
    KERNELS=="0003:17EF:608D.0005"
    SUBSYSTEMS=="hid"
    DRIVERS=="hid-generic"
    ATTRS{country}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0':
    KERNELS=="1-3.3:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="usbhid"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{supports_autosuspend}=="1"
    ATTRS{bInterfaceProtocol}=="02"
    ATTRS{bNumEndpoints}=="01"
    ATTRS{bInterfaceClass}=="03"
    ATTRS{authorized}=="1"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bAlternateSetting}==" 0"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3':
    KERNELS=="1-3.3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bDeviceClass}=="00"
    ATTRS{idProduct}=="608d"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPower}=="100mA"
    ATTRS{quirks}=="0x0"
    ATTRS{speed}=="1.5"
    ATTRS{removable}=="unknown"
    ATTRS{tx_lanes}=="1"
    ATTRS{ltm_capable}=="no"
    ATTRS{bcdDevice}=="0100"
    ATTRS{urbnum}=="42757"
    ATTRS{version}==" 2.00"
    ATTRS{busnum}=="1"
    ATTRS{authorized}=="1"
    ATTRS{product}=="Lenovo USB Optical Mouse"
    ATTRS{devpath}=="3.3"
    ATTRS{devnum}=="7"
    ATTRS{idVendor}=="17ef"
    ATTRS{bmAttributes}=="a0"
    ATTRS{maxchild}=="0"
    ATTRS{manufacturer}=="PixArt"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{rx_lanes}=="1"
    ATTRS{bMaxPacketSize0}=="8"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{configuration}==""
    ATTRS{bDeviceProtocol}=="00"

  looking at parent device '/devices/pci0000:00/0000:00:14.0/usb1/1-3':
    KERNELS=="1-3"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
   [...]

The device
/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3/1-3.3:1.0
indicates being detected as USB-HID device (
DRIVERS=="usbhid"
), but the path with the
power/wakeup
path is one level up:
/devices/pci0000:00/0000:00:14.0/usb1/1-3/1-3.3'
The approach is to invoke ../ in the attribute:
mabeett@nowhere:~$ cat /etc/udev/rules.d/52-non-power-on-hid.rules
# Disables USB HID devices (as mouse/keyboard) wakeup from suspend

ACTION=="add", SUBSYSTEM=="usb", DRIVER=="usbhid", TAG+="usb_hid_donot_wakeup"
ACTION=="add", SUBSYSTEM=="usb", DRIVER=="usbhid", ATTR{../power/wakeup}="disabled"
With this rule any HID connnected device will be disabled for wakeup.
The tag is going to be used for debugging porpouses.
I may see all the mathched/confugured devices at /run interface
mabeett@nowhere:~$ ls /run/udev/tags/usb_hid_donot_wakeup/
+usb:1-3.3:1.0

References