56

I was looking for hours, maybe more, for a solution and i just found it. I want to share it with everyone. This way it will be easier googling this problem to find the solution faster. So, just like the title says, below is the solution.

Related question for PCI devices

Daniel T
  • 5,339

5 Answers5

74

In terminal:

grep . /sys/bus/usb/devices/*/power/wakeup

The result, for me, was to find out that all usb were disabled.

  1. So now, type:

     sudo su
    

    we have now root privillages.

  2. I have 8 usb ports (you do that for as many usb ports you have) ,so:

     echo enabled > /sys/bus/usb/devices/usb1/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb2/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb3/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb4/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb5/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb6/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb7/power/wakeup
     echo enabled > /sys/bus/usb/devices/usb8/power/wakeup
    

    Go ahead and test it. Now you can wake up from any wireless or wired usb keyboard and mouse.

    So then, the reason we must enable all of them, is because in the next step, we will write this in rc.local to execute the command after every reboot, and after reboot some linux distros (maybe all) change the usb ports. We don't have to worry for anything going wrong by enabling all of them, since linux is in suspend or hibernation, it can't use the wifi to download anything, so it won't wake up without we wake it up on purpose.

  3. Run

     sudo nano /etc/rc.local
    

    and we paste everything from step 2. in there (before the exit 0 of course).

That's it. From now on we can use our wireless usb and mouse to wake up from suspend.

I hope it works for all of you. This guide was made after testing all other possible solutions around the internet.

Pablo Bianchi
  • 17,371
37

In addition to my guide above i want to add this information, because i recently discovered that some wireless usb devices after waking up from sleep, they revert back to disable. I repeat, only some usb devices do that, not all. That's why i didn't add this small guide up on my guide.

So you did as i instructed above and your pc successfully wakes up, but later in the day suddenly it doesn't wake up again.

Solution:

Open a terminal and do :

lsusb

At your keyboard device id information the 4 first digits are the vendor id and the 4 next digits are the product id (see screenshot) enter image description here

Next do:

sudo nano /etc/udev/rules.d/10-wakeup.rules

Where "wakeup" enter your desired name of the script. Number 10 is the priority in case you have many other udev rules, the lower the number the 'rule' will be executed before the others.

Copy paste this and replace the vendor id and product id with your own wireless keyboard vendor id and product id.

ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="062a", ATTRS{idProduct}=="4101" RUN+="/bin/sh -c 'echo enabled > /sys/bus/usb/devices/usb8/power/wakeup'"

*usb8 for me is my wireless keyboard (you can also see that in the screenshot (Bus 008)), replace it with your own.

Ctrl + O to save , Ctrl + X to exit and reboot.

13

None of the mentioned answers helped me. That's why I post here my own one.

Enabling/disabling wake up from suspend for USB devices

Tested in KDE neon 5.16.2, based on Ubuntu 18.04 but should work in any distribution.

  1. Show the list of USB devices to identify the one you want to enable / disable:

    grep . /sys/bus/usb/devices/*/product
    

    you should obtain something like this:

    /sys/bus/usb/devices/3-3/product:Cord Optical Mouse
    /sys/bus/usb/devices/3-4.3/product:802.11n WLAN Adapter
    /sys/bus/usb/devices/3-4.4/product:USB Receiver
    /sys/bus/usb/devices/3-4/product:USB2.0 Hub
    /sys/bus/usb/devices/4-4/product:USB3.0 Hub
    /sys/bus/usb/devices/usb1/product:EHCI Host Controller
    /sys/bus/usb/devices/usb2/product:EHCI Host Controller
    /sys/bus/usb/devices/usb3/product:xHCI Host Controller
    /sys/bus/usb/devices/usb4/product:xHCI Host Controller
    
  2. Check wake up status of all USB devices:

    grep . /sys/bus/usb/devices/*/power/wakeup
    

    the result should be something like this:

    /sys/bus/usb/devices/1-1/power/wakeup:disabled
    /sys/bus/usb/devices/2-1/power/wakeup:disabled
    /sys/bus/usb/devices/3-11/power/wakeup:disabled
    /sys/bus/usb/devices/3-3/power/wakeup:enabled
    /sys/bus/usb/devices/3-4.4/power/wakeup:disabled
    /sys/bus/usb/devices/3-4/power/wakeup:disabled
    /sys/bus/usb/devices/4-4/power/wakeup:disabled
    /sys/bus/usb/devices/usb1/power/wakeup:disabled
    /sys/bus/usb/devices/usb2/power/wakeup:disabled
    /sys/bus/usb/devices/usb3/power/wakeup:disabled
    /sys/bus/usb/devices/usb4/power/wakeup:disabled
    

    In my case wake up is only enabled for the USB device 3-3 that, according to the previous list, is "Cord Optical Mouse".

    I want to enable wake up from suspend only for wireless keyboard and disable it for the rest. The keyboard USB receiver is connected to a HUB and corresponds to 3-4.4 on the previous list ("USB Receiver").

  3. Create a script that does the work:

    To have root permissions:

     sudo su
    

    Then we create the file for the script (the file can be called "usbwakeup" or whatever descriptive you want):

     nano /etc/init.d/usbwakeup 
    

    Contents of the file. Just adapt it to your needs by changing the "SCRIPT ACTIONS" section (I like adding comments to scripts in order to know what they actually do if I open them again someday):

    #!/bin/bash
    #
    #######################
    # GENERAL INFORMATION #
    #######################
    #
    # - This script enables / disables wake up from suspend# for USB devices.
    #
    # - The script needs execution permissions.
    #
    # - For this script to be executed when the system starts, there must be
    #   a symbolic link to it in /etc/rc3.d/ with priority S01
    #   (for example: /etc/rc3.d/S01usbwakeup)
    #
    ###########################
    # RELATED USEFUL COMMANDS #
    ###########################
    #
    # - Identify USB devices:
    #   grep . /sys/bus/usb/devices/*/product
    #
    # - Check the status of "wake up of the suspension" of USB devices:
    #   grep . /sys/bus/usb/devices/*/power/wakeup
    #
    #
    ##################
    # SCRIPT ACTIONS #
    ##################
    #
    # Disables wake up from suspend for optical mouse
    echo disabled > /sys/bus/usb/devices/3-3/power/wakeup
    #
    # Enables wake up from suspend for the wireless keyboard
    that I have connected to a USB 3.0 HUB in the USB port nr.4
    echo enabled > /sys/bus/usb/devices/3-4.4/power/wakeup
    
  4. Give it execution permissions:

    chmod +x /etc/init.d/usbwakeup
    
  5. Create a symbolic link in /etc/rc3.d/ for it to start on boot up:

    sudo ln -s /etc/init.d/usbwakeup /etc/rc3.d/S01usbwakeup
    

By restart the system everything should work as specified in the script. The changes will be persistent as they will be executed with each system boot.

Reference

Pablo Bianchi
  • 17,371
call0fcode
  • 541
  • 6
  • 7
3

Pavlos Theodorou's answer is very helpful. I would like to add that you can find the usb device that your mouse/keyboard is connected to at boot-up by piping dmesg through grep a couple of times, then egrep once, and finally using tail to make sure it was the most recent entry.

Using this method, you don't have to enable wake on ALL usb ports.

I put the following in my /etc/rc.local file and it finds my logitech receiver every time, even if moved from one usb port to another. Just substitute the name of your keyboard or mouse from dmesg in place of "Logitech K270"

KB="$(dmesg | grep "Logitech K270 as" | grep -o -P "usb.{0,5}" | egrep -o ".{0,3}$" | tail -1)"
echo enabled > /sys/bus/usb/devices/${KB}/power/wakeup

This works on boot, but doesn't seem to run on wake from suspend, so I had to put a script file in /lib/systemd/system-sleep/. Create it, set it as globally executable and give it a name that starts with a double digit number between 00 and 99. My script is as follows, again substite your keyboard/mouse verbage from dmesg:

#!/bin/sh

Action script to enable wake after suspend by keyboard or mouse

if [ $1 = post ] then KB="$(dmesg | grep "Logitech K270 as" | grep -o -P "usb.{0,5}" | egrep -o ".{0,3}$" | tail -1)" echo enabled > /sys/bus/usb/devices/${KB}/power/wakeup fi

if [ $1 = pre ] then KB="$(dmesg | grep "Logitech K270 as" | grep -o -P "usb.{0,5}" | egrep -o ".{0,3}$" | tail -1)" echo enabled > /sys/bus/usb/devices/${KB}/power/wakeup fi

For some reason I can't explain, the 'post' sleep if statement only works every other wake-up... but the 'pre' sleep if statement seems to work every time.

Pablo Bianchi
  • 17,371
jcn1
  • 31
3

This can all be done with just one udev rule and no scripts, since udev rules allow modifying attributes as part of the rule itself. All you need is the vendor/product id from lsusb. This is also more robust and will work between boots:

# /etc/udev/rules.d/10-wakeup.rules
ACTION=="add", \
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="239a", ATTRS{idProduct}=="8120", \
ATTR{power/wakeup}="enabled"
Ben Davis
  • 231