Keyboard stops working on Ubuntu 16.04 (Xenial Xerus)

My keyboard stops working for no apparent reason.

After using Ubuntu for 2.5 years as a virtual machine on a Windows host (VirtualBox) I finally took the plunge, reformatted my hard disk and built the system with Ubuntu yesterday. This morning I my keyboard stopped working.

I see many posts about Ubuntu and a USB keyboard or mouse ceasing to work randomly. They indicate that it is autosuspend turning off power to USB devices to save power, and when the USB device is not compatible in some, the USB does not 'wake-up' as it should.

I tried the suggestion posted in Fixing USB Autosuspend.

But no luck. I also tried another suggestion which was to boot without the keyboard and then plug it in once Ubuntu had started.

Here are some details:

  • I used the same Kinesis keyboard with Ubuntu as a virtual machine for the past two years, no problem.
  • The Kinesis has macros and triggering a macro triggers the problem 100% of the time. However, it happens when I am just typing (no macros) as well.
  • I tried the Kinesis on a laptop just now. This is a one-year-old machine that I put Ubuntu 16.04 on two weeks ago. Using a Kinesis keyboard macro did not reproduce the problem.

Problem Machine

  • Dell XPS, 6 years old, desktop
  • Installed Ubuntu 16.04 yesterday
  • Using Kinesis Advantage USB keyboard
  • Unplugging/re-plugging the keyboard has worked 9 out of 10 times
  • Rebooting works as well
  • I don't think it is a USB power problem as an LED on the keyboard stays lit and other USB devices keep working.
  • I have both a USB and a Bluetooth mouse attached to the machine. Both keep working even when the Kinesis stops.
  • I normally have a second regular Dell branded keyboard plugged in for non-Kinesis folks who need to use my computer. Earlier today this keyboard stopped working each time the Kinesis stopped working. However now, triggering the problem with a macro, the Dell keyboard keeps working.

The Kinesis, which I have had for 8 year now, has made it possible for me to keep keyboarding. If I can't resolve this I'll have to go back to Windows which would be a real bummer.

Any suggestions on things to try or ways to go about debugging this would be greatly appreciated.

I am digging into USB drivers, which on Linux I know nothing about. However, I have found that the laptop has both USB 2.0 and 3.0 ports. The desktop (with the problem) is USB 2.0 only. Still digging...


Solution 1:

I had the same problem with Ubuntu 16.04 (Xenial Xerus) and searched so much for any solution...

The solution is at the end of this answer if you don't care about the context story. Context could look like unuseful blabla (maybe it is), but I think that it could help readers to check if the problem I've solved on my PC is the same as their one.

CONTEXT

I desperately have unsuccessfully tested near every tips I've found on the web, rebooting after each try:

  • '/etc/default/grub' kernel boot usbcore.autosuspend=-1 parameter
  • laptop-mode's 'runtime-pm.conf' CONTROL_RUNTIME_AUTOSUSPEND=0
  • fully disabling Laptop Mode Tools from '/etc/laptop-mode/laptop-mode.conf' file, confirming that the problem isn't related to this kernel feature. I've checked different ways that laptop_mode wasn't running anymore to be sure. (Ref: man laptop-mode.conf & man laptop_mode)
  • I've read some tricks about '/etc/laptop-mode/conf.d/usb-autosuspend.conf', but this file doesn't exist in my version of Ubuntu 16.04 with laptop-mode-tools v1.68
  • powertop application uninstallation. It's often suspected in forums to cause autosuspend issues, and today I know that it was not true in my case as uninstalling it didn't fix anything.
  • options usbcore autosuspend=1 in '/etc/modprobe.d/usb.conf' has been inefficient too
  • '/etc/udev/rules.d/60-power.rules' script provided by Gentoo official Wiki
  • device-specific rules file for udev '/etc/udev/rules.d/usb-power.rules' recommended by Serge Y. Stroobandt

After having read all this technical stuff I only understood that the cause was related to my '/sys/module/usbcore/parameters/autosuspend' value stuck at two seconds, no matter what change I do to my configuration. You can check it with cat command:

$ cat /sys/module/usbcore/parameters/autosuspend
2

And yes, my mouse was disabled exactly after two seconds, so I knew I was on the right way. Of course I had tested the echo -1 command that I've seen here and there, -1 intended to disable autosuspend... but:

sudo echo -1 > sys/module/usbcore/parameters/autosuspend
bash: /sys/module/usbcore/parameters/autosuspend: Permission denied

I didn't understand why permission was denied even in sudo mode. I didn't try as root, because I just didn't think that it could be a problem. Here was the mistake :(

Thanks to Alex Dekker & Bryce comments, I've learnt how to disable autosuspend only for my mouse. But of course my first tries have failed with this bash: Permission denied error, so I had to wait for understanding of my root/sudo confusion before the echo -1 command suggested by Alex finally work.

I've also found a better way to execute a command as the root user without login as root. The difference between root shell and root commands usage is well explained in the official Ubuntu's RootSudo wiki page. I love to learn to these kind of things, but hey! Why does it have to be always learnt the painful way? LOL

OMG, it was a real headache, and it took lot of time for a noob like me to understand, as I usually say "no brain no gain". Mine visibly needs more training.

So let's stop talking about myself and go fix it! Please continue below...


I highly recommend the second solution for your idle external HDD to don't drain your laptop battery for nothing. But I give you both solutions, because who loves his/her own freedom should love each others freedom too :)

SOLUTION 1

To disable autosuspend on every USB devices/ports

  1. Open Terminal and execute:

    sudo sh -c "echo -1 > /sys/module/usbcore/parameters/autosuspend"
    
  2. This is indeed a temporary solution that allows you to instantly test if this command fixes your mouse or keyboard autosuspend issue.

  3. If your USB device(s) work fine now, just edit your '/etc/rc.local' script so it will run that command whenever the system is booted. This way you'll make the fix kind of permanent. Edit the file, for example with nano:

    sudo nano /etc/rc.local
    
  4. and put the echo -1 command in your '/etc/rc.local' file before the exit 0 line

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    (...)
    # By default this script does nothing.
    echo -1 > /sys/module/usbcore/parameters/autosuspend
    exit 0
    
  5. Restart your computer

(Please check the Important note at the end.)

.

SOLUTION 2

To disable autosuspend for a specific USB device/port

  1. The power options for a USB device are in /sys/bus/usb/devices/n-n/power. What n-n is for a given device requires a few manipulation to find out, but it's not complicated. The example below shows how I've determined that the USB port number of my wireless mouse receiver is 3-1, so please adapt this to your own situation:

    • Unplug your USB device, open Terminal and execute the sudo dmesg command

    • Connect your USB device back, and then execute sudo dmesg again in Terminal to check for changes

    • Here's what the end of my dmesg command output looks like after having plugged my mouse back in a slot:

      [40208.575249] usb 3-1: new full-speed USB device number 8 using xhci_hcd
      [40208.705233] usb 3-1: New USB device found, idVendor=062a, idProduct=4102
      [40208.705240] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
      [40208.705245] usb 3-1: Product: 2.4G Wireless Mouse
      [40208.705248] usb 3-1: Manufacturer: MOSART Semi.
      [40208.707679] input: MOSART Semi. 2.4G Wireless Mouse as /devices/pci0000:00/0000:00:14.0/usb3/3-1/3-1:1.0/0003:062A:4102.0007/input/input24
      [40208.763946] hid-generic 0003:062A:4102.0007: input,hiddev0,hidraw0: USB HID v1.10 Mouse [MOSART Semi. 2.4G Wireless Mouse] on usb-0000:00:14.0-1/input0
      

    Now we can see that the port where my USB device has been plugged-in is identified as usb 3-1.

    **Note:** this trick concerns the USB port ID, not a device ID. For example, if I plug my mouse to the next USB port, it becomes identified as `usb 3-2` in the `dmesg` result. But this solution #2 remains very useful if you take the habit to always plug your mice or keyboard to the same port, and it's generally what people use to do unconsciously ;)
    
  2. Execute the following command in your Terminal, changing my port number 3-1 with the one you've found in your own dmesg result:

    sudo sh -c "echo -1 > /sys/bus/usb/devices/3-1/power/autosuspend_delay_ms"
    

    Note that autosuspend_delay_ms has replaced the deprecated autosuspend since kernel version 2.6.38, according to the Linux official documentation.

  3. This is indeed a temporary solution, but you can now easily test if this command fixed your mouse or keyboard autosuspend issue.

  4. If your USB device work fine now, just edit your '/etc/rc.local' script so it will run that command whenever the system is booted. This way you'll make the fix kind of permanent. Edit the file, for example with nano:

    sudo nano /etc/rc.local
    
  5. and put the echo -1 command in your '/etc/rc.local' file before the exit 0 line, once again don't forget to change port number n-n in the command:

        #!/bin/sh -e
        #
        # rc.local
        #
        # This script is executed at the end of each multiuser runlevel.
        (...)
        # By default this script does nothing.
        echo -1 > /sys/bus/usb/devices/3-1/power/autosuspend_delay_ms
        exit 0
    
  6. Restart your computer

.

Important: don't forget that you'll have to execute the sudo sh -c "temporary" command again manually if you disconnect/reconnect your USB device while your desktop environment is already started, even if you plug it back on the same USB plug. Normal, just because the '/etc/rc.local' file is called only once at system startup.

Anyway, workarounds are possible. Better skilled peoples may have more clever methods, but I've configured port-specific aliases in Ubuntu. It allows me to use the simple usb1 or usb2 commands selectively if I have to unplug/replug my USB device here or there. You too! You could also create commands aliases to play with short words instead of typing the full command. It's easy to set in less than two minutes and also works with Alt + F2 :)

More ideas to make it easier would be also very welcome.

Solution 2:

I had the exact same issue (Kinesis keyboard, stopped working after a reboot).

I eventually figured out it was related to a kernel upgrade

  • before the reboot I was on version 4.4.0-31-generic
  • after the reboot it was 4.4.0-38-generic

Booting using the older 4.4.0-31 kernel fixed the issue for me.

This post includes instructions on how to boot using an older kernel (allowing you to see if the older kernel does the trick for you).