Disable specific device from waking up the system

Solution 1:

Writing a value disabled instead of enabled into the file /sys/bus/usb/devices/3-5/power/wakeup is working correctly: when the value is disabled, a corresponding mouse or keyboard doesn't wake up the computer, but if the value is enabled - they wake up it. This helper script do it for a device with particular vendorId and productId:

#!/bin/bash
### Put it here: /usr/bin/usb-resume-control

while getopts v:p:s: flag
do
    case "${flag}" in
      v) vendor=${OPTARG};;
      p) product=${OPTARG};;
      s) state=${OPTARG};;
    esac
done
if [ -z "$vendor" -o -z "$product" ]; then
        echo -en "Usage: $0 -v vendorId -p productId -s state\nin any order, where vendorId and productId are both from [lsusb] and state can be enable or disable\n"
        exit 1;
fi
if [ -z $state ]; then
        stateTo="disabled"
fi
DEVICES=/sys/bus/usb/devices

for a in `ls $DEVICES`; do
  if [ -f "$DEVICES/$a/idVendor" -a -f "$DEVICES/$a/idProduct" ]; then
    idVendor=`cat "$DEVICES/$a/idVendor"`
    idProduct=`cat "$DEVICES/$a/idProduct"`
    if [ -f "$DEVICES/$a/product" ]; then 
      productName=`cat "$DEVICES/$a/product"`
    fi
    WAKEUPFILE="$DEVICES/$a/power/wakeup"
    if [ $idVendor = $vendor -a $idProduct = $product -a -f "$WAKEUPFILE" ]; then
      oldState=`cat "$WAKEUPFILE"`
      echo "$state" > "$WAKEUPFILE"
      newState=`cat "$WAKEUPFILE"`
      echo Bus-port:$a vendor=$idVendor product=$idProduct name=$productName WakeUp: old=$oldState new=$newState
    fi
  fi
done

To automate process create a systemd unit file /etc/systemd/system/control-usb-wakeup-mouse.service with the following content:

[Unit]
Description=Control wakeup of USB device before sleep so they will or not resume the computer
Before=sleep.target

[Service]
Type=oneshot
ExecStart=/usr/bin/usb-resume-control -v 045e -p 0745 -s disabled
StandardOutput=journal

[Install]
WantedBy=sleep.target

Update vendorId and productId to your USB device (see output of lsusb). After that run those commands against a unit:

chmod 755 /etc/systemd/system/control-usb-wakeup-mouse.service
systemctl daemon-reload
systemctl enable control-usb-wakeup-mouse.service

Now the device mentioned cannot wake up the computer after sleep.