Adventures of the Retired Guy

Adventures of the Retired Guy

Stuff and nonsense from a retired guy

25 Feb 2021

Hacking wakeup for USB and Bluetooth

Getting reliable wakeup from suspend from bluetooth keyboard or USB mouse.

Article incomplete; don't know where to put enable to make it sticky across reboot.

I've configured my desktop Ubuntu machine to avoid low-power suspend so the USB attached printer stays available on the network for my spouse. Despite this, I occasionally see the desktop won't resume from idle from keyboard or mouse activity. This recently happened after applying a system update that rebuilt the kernel. Screen is blank, but desktop is running (evidence, disk activity every few seconds). I recover by reboot, which is harsh. Purpose of this post is to document the tools and learning to reconfigure.

Device naming

PCI bus is top level on desktop, /sys/devices/pcixxxx. It includes native USB devices...

$ ll  /sys/bus/usb/devices/*
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-0:1.0 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-1 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-10 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-10/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-10:1.0 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-10/1-10:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-10:1.1 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-10/1-10:1.1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-1:1.0 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-1:1.1 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-3 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-3/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/1-3:1.0 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-3/1-3:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/2-0:1.0 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb2/2-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-0:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-2 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-2/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-2:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-2/3-2:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-2:1.1 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-2/3-2:1.1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-2:1.2 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-2/3-2:1.2/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-4 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-4/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-4:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-4/3-4:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-4:1.1 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-4/3-4:1.1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-4:1.2 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-4/3-4:1.2/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/3-4:1.3 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/3-4/3-4:1.3/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/4-0:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb4/4-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/5-0:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.4/usb5/5-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/6-0:1.0 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.4/usb6/6-0:1.0/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb1 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb2 -> ../../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb2/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb3 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb3/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb4 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.3/usb4/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb5 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.4/usb5/
lrwxrwxrwx 1 root root 0 Feb 25 08:37 /sys/bus/usb/devices/usb6 -> ../../../devices/pci0000:00/0000:00:08.1/0000:07:00.4/usb6/

And also Bluetooth devices mapped to USB. But bluetooth devices aren't on a bus, they are dynamic network connections via the installed Bluetooth adapter. These you have to find via:

$ ll /sys/class/bluetooth
total 0
drwxr-xr-x  2 root root 0 Feb 25 08:37 ./
drwxr-xr-x 79 root root 0 Feb 25 08:37 ../
lrwxrwxrwx  1 root root 0 Feb 25 08:37 hci0 ->      ../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.0/bluetooth/hci0/
lrwxrwxrwx  1 root root 0 Feb 25 08:45 hci0:3585 -> ../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.0/bluetooth/hci0/hci0:3585

With the first 0000:00:01.2 as a hint, you can find the bluetooth receiver (Bus1, Device 2):

---------- bob@bob-desktop:~ ----------
$ lsusb
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 003: ID 045e:075d Microsoft Corp. LifeCam Cinema
Bus 003 Device 002: ID 04a9:26a3 Canon, Inc. MF4100 series
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 062a:4102 MosArt Semiconductor Corp. Wireless Mouse
Bus 001 Device 004: ID 046d:c534 Logitech, Inc. Unifying Receiver
Bus 001 Device 002: ID 8087:0aa7 Intel Corp. Wireless-AC 3168 Bluetooth
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

To find (currently active) devices:

$ ll /sys/class/bluetooth
total 0
drwxr-xr-x  2 root root 0 Feb 25 08:37 ./
drwxr-xr-x 79 root root 0 Feb 25 08:37 ../
lrwxrwxrwx  1 root root 0 Feb 25 08:37 hci0 ->      ../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.0/bluetooth/hci0/
lrwxrwxrwx  1 root root 0 Feb 25 08:45 hci0:3585 -> ../../devices/pci0000:00/0000:00:01.2/0000:01:00.0/usb1/1-1/1-1:1.0/bluetooth/hci0/hci0:3585/

Bluetooth are /sys/devices/hcixxx. Can manage bluetooth via:

$ hciconfig
hci0:	Type: Primary  Bus: USB
	BD Address: 58:96:1D:BF:D0:FA  ACL MTU: 1021:4  SCO MTU: 96:6
	UP RUNNING PSCAN ISCAN 
	RX bytes:79559 acl:4269 sco:0 events:238 errors:0
	TX bytes:5287 acl:87 sco:0 commands:130 errors:0

This is the bluetooth receiver. Where are the individual devices, like keyboard and speakers?

;tl;dr

Can enumerate bluetooth receiver via bluetoothctl list/show; can list (visible) devices via bluetoothctl devices; can query individual device via bluetoothctl info <MAC>. But none of this shows /sys/devices or /sys/bus paths.

Checking current status

First check whether low power suspend is enabled?

Then, which devices can awaken:

---------- bob@bob-desktop:~ ----------
$ grep . /sys/bus/usb/devices/*/power/wakeup
/sys/bus/usb/devices/1-10/power/wakeup:enabled
/sys/bus/usb/devices/1-1/power/wakeup:disabled
/sys/bus/usb/devices/1-3/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
/sys/bus/usb/devices/usb5/power/wakeup:disabled
/sys/bus/usb/devices/usb6/power/wakeup:disabled
---------- bob@bob-desktop:~ ----------
$ grep . /sys/bus/usb/devices/*/product
/sys/bus/usb/devices/1-10/product:USB Receiver
/sys/bus/usb/devices/1-3/product:2.4G Wireless Mouse
/sys/bus/usb/devices/3-2/product:MF4100
/sys/bus/usb/devices/3-4/product:Microsoft® LifeCam Cinema(TM)
/sys/bus/usb/devices/usb1/product:xHCI Host Controller
/sys/bus/usb/devices/usb2/product:xHCI Host Controller
/sys/bus/usb/devices/usb3/product:xHCI Host Controller
/sys/bus/usb/devices/usb4/product:xHCI Host Controller
/sys/bus/usb/devices/usb5/product:xHCI Host Controller
/sys/bus/usb/devices/usb6/product:xHCI Host Controller
---------- bob@bob-desktop:~ ----------

So we see 1-10 is only usb device currently configured to wake up from low power, and that device is USB Receiver. 1-3, Wireless Mouse, is not.

Next steps

The guessing is /sys/bus/usb/devices/1-2 (bus 1 device 2) needs a power/wakeup file. Not sure why it's not provided by default.

And, again, the question is how to make the files and file contents persist across system boot. Guidance on the web is to put

echo 'enabled' > /sys/bus/usb/.../power/wakeup

into /etc/rc.local. But I don't have that file at all, just a family of distribution-provided /etc/rcN.d/ . Seems hackish to create rc.local, investigate whether there's a more standard way.