Quick How-to: Automatically Enable Keyboard Wakeup From Suspend

If you like your power usage low, you probably put your computer to sleep (suspend) as much as possible when idle. If you want to wake up the machine by hitting a key on your keyboard you may need to break some sweat. Even in laptops where this feature is enabled by default, sometimes extra keyboards won’t work.

If you’re using a modern Linux kernel (3.2 and above) and have udev installed you can automatically enable wakeup upon connecting a keyboard by adding the following script to your system.
Simply put the following under your udev rules (e.g. /etc/udev/rules.d/90-keyboardwakeup.rules):

#Whenever a keyboard is connected to the machine, enable wakeup on its parent device
#Note: Conditions are *string* match, not numerical. Check your /sys files for the exact string 
#Enable wakeup on any keyboard connected. Keyboards are HID (class 3) protocol 1 devices.
SUBSYSTEM=="usb", ATTRS{bInterfaceClass}=="03", ATTRS{bInterfaceProtocol}=="01" RUN+="/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'" 

The example is based on this post but instead of restricting to a single vendor/product the script should match any USB keyboard (I used a wireless Logitech to test it).

The above should work, but if you need to troubleshoot continue reading.

Note: Accessing device information in Linux (and especially USB devices) can be very confusing, proceed with patience and be ready to go reading around.

To test the script above, you can use udevadm (finding your sysfs /dev/bus/… file is a little tricky. You can go over all of them until you find your device).
Replace the /dev/bus index with the appropriate one:

sudo udevadm test /dev/bus/001/005

If the condition matches you should see in the command output something like (output truncated and highlighted):


run: ‘/bin/sh -c ‘echo enabled > /sys/devices/pci0000:00/0000:00:14.0/usb1/1-5/1-5:1.0/../power/wakeup”
Unload module index
Unloaded link configuration context.

Another test is to restart the udev service, disconnect and reconnect the USB keyboard and run (again, replace device index with appropriate one):

cat /sys/bus/usb/devices/1-5/power/wakeup

Output of the last command should be enabled.

Note that the device itself is 1-5:1.0  but the test is for device 1-5. The reason is that power control is on USB hub rather than the device (keyboard) itself. In this case the ‘hub’ was actually the IR receiver but in wired keyboard it will be one of the computer’s USB hubs

Note that I did not find it necessary to enable wakeup on the parent USB hub as recommended here. You may need to do so.

To do the ultimate test put your computer to sleep (example below assumes you have system.d installed. It takes control over power events over HAL):

sudo dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.Suspend
" boolean:true

The computer should put itsef to sleep. Hit the keyboard and check it comes back online.


Leave a comment

Filed under howto, linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s