33

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.

46

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.

9
  • 6
    If you want to figure out which device is which, you can look in /sys/bus/usb/devices/<device id>/product for a text description.
    – ntc2
    Dec 12 '17 at 18:01
  • I enabled all devices in "grep . /sys/bus/usb/devices/*/power/wakeup" but still didn't work. Perhaps it's because the keyboard and mouse are connected indirectly - wirelessly to a dongle which resides on the screen? Screen is connected via USB to the laptop. Mar 21 '18 at 20:42
  • 2
    as @ntc2 said, use this to see the description of each: grep . /sys/bus/usb/devices/*/product helped me to figure out what i'm looking at May 5 '18 at 9:46
  • If this still doesn't work, make sure your device (keyboard / mouse) still has power when the PC is in sleep mode. If there is no LED on your device, you can use a USB power monitor (can find them about $10 online) to check for power on the USB port.
    – J Smith
    May 7 '18 at 21:40
  • 1
    The permanent part of this question no longer works with at least ubuntu 18.04. The addition (askubuntu.com/a/874701/516072) to this answer provided by pavlos-theodorou helped for the permanent part in Ubuntu 18.04
    – Laurens
    Oct 14 '18 at 13:33
28

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.

5
  • 5
    Better to incorporate this into your first answer.
    – ntc2
    Dec 12 '17 at 17:59
  • 2
    they are 2 different things. the 1st one is the answer to the wake up problem. the 2nd one is more like "How to create a rule to execute no boot" and it's just an "extra" since not everyone knows how to do that and there is no simple amateur guide for that. Apr 14 '18 at 10:34
  • 4
    This is indeed the right way to setup a device after connection; any other solution or script is just a dirty hack. Improvement: use $env{DEVPATH} as device path in your RUN script. For example, this is the line for my Intel Bluetooth adapter: ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="8087", ATTRS{idProduct}=="0aaa" RUN+="/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/power/wakeup'"
    – dav.garcia
    Feb 24 '20 at 18:21
  • This is great and kind of works on my system. However, my MCE remote IR receiver seems to cause the system to wake up from suspend even when there is no button pressed. Any ideas what might be going on?
    – Vik
    Jun 16 '20 at 4:59
  • 1
    I used simpler udev rule: ACTION=="add", SUBSYSTEM=="usb", DRIVERS=="usb", ATTRS{idVendor}=="046d", ATTR{power/wakeup}="disabled". See this answer: unix.stackexchange.com/a/532839/422471
    – user570605
    May 3 at 11:50
7

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

2
  • I just have found and interesting post where it's explained how to create a simple GUI for the same purpose. I haven't tested it but it could perhaps help someone trying to make it graphicaly and with the ease of a single click to enable/disable: bernaerts-nicolas.fr/linux/74-ubuntu/220-ubuntu-resume-usb-hid
    – call0fcode
    Jul 3 '19 at 12:08
  • Thanks man, very helpful.
    – Aby
    Dec 13 '20 at 12:51
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.