Just a few a days ago, I wrote a blog post about Pairing Bluetooth Mouse on Command Line on a Fedora laptop. So why do I need to write another post about doing the same thing on Gentoo? Well, as it turned out, I need a bit more than just turning on the bluetooth service and pairing the mouse. My Gentoo’s installation is very minimal, and I think this gives me an excuse to document on how I’d solve or get this working on Gentoo.

Let’s get started by trying to enable the bluetooth.service.

mathbook ~ # systemctl status bluetooth
Unit bluetooth.service could not be found.

Hmm… interesting, there is no such Systemd unit file called bluetooth.service. From my other blog post, I can use the e-file tool to search for the name of the package providing a file.

mathbook ~ # e-file bluetooth.service
 *  net-wireless/bluez
        Available Versions:     5.51 5.51-r1 5.52 5.52-r1 5.54 5.55
        Homepage:               http://www.bluez.org
        Description:            Bluetooth Tools and System Daemons for Linux
        Matched Files:          /usr/lib/systemd/system/bluetooth.service; /lib/systemd/system/bluetooth.service;

From the above output, the package net-wireless/bluez wasn’t installed on my machine. So let’s do it.

mathbook ~ # emerge -av net-wireless/bluez

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] dev-libs/ell-0.32::gentoo  USE="-glib -pie -test" ABI_X86="(64) -32 (-x32)" 468 KiB
[ebuild   R    ] sys-apps/dbus-1.12.18::gentoo  USE="X systemd -debug -doc (-elogind) (-selinux) -static-libs -test -user-session*" ABI_X86="(64) -32 (-x32)" 0 KiB
[ebuild  N     ] dev-libs/libical-3.0.8:0/3::gentoo  USE="berkdb glib introspection -doc -examples -static-libs -test -vala" 861 KiB
[ebuild  N     ] net-wireless/bluez-5.54:0/3::gentoo  USE="mesh obex readline systemd udev -btpclient -cups -debug -deprecated -doc -experimental -extra-tools -midi (-selinux) -test -test-programs -user-session" ABI_X86="(64) -32 (-x32)" PYTHON_SINGLE_TARGET="python3_8 -python3_6 -python3_7" 1,957 KiB

Total: 4 packages (3 new, 1 reinstall), Size of downloads: 3,286 KiB

The following USE changes are necessary to proceed:
 (see "package.use" in the portage(5) man page for more details)
# required by net-wireless/bluez-5.54::gentoo[systemd]
# required by net-wireless/bluez (argument)
>=sys-apps/dbus-1.12.18 -user-session

Would you like to add these changes to your config files? [Yes/No] No

Wait, why did I select “No” instead of proceeding with the installation? Well, if we look closely in the above output, net-wireless/bluez depends on sys-apps/dbus, and it requires that sys-apps/dbus Reinstall to remove the USE flag -user-sessin. This happens due to the net-wireless/bluez will be installed without the USE flag user-session enabled. I have 2 choices: either reinstall the sys-apps/dbu with -user-session, or enable user-session with the net-wireless/bluez. I’m going to pick the later option.

Let instruct emerge to install bluez with user-session USE flag turned on.

mathbook ~ # echo 'net-wireless/bluez user-session' > /etc/portage/package.use/bluez

mathbook ~ # emerge -av net-wireless/bluez

These are the packages that would be merged, in order:

Calculating dependencies... done!
[ebuild  N     ] dev-libs/ell-0.32::gentoo  USE="-glib -pie -test" ABI_X86="(64) -32 (-x32)" 468 KiB
[ebuild  N     ] dev-libs/libical-3.0.8:0/3::gentoo  USE="berkdb glib introspection -doc -examples -static-libs -test -vala" 861 KiB
[ebuild  N     ] net-wireless/bluez-5.54:0/3::gentoo  USE="mesh obex readline systemd udev user-session -btpclient -cups -debug -deprecated -doc -experimental -extra-tools -midi (-selinux) -test -test-programs" ABI_X86="(64) -32 (-x32)" PYTHON_SINGLE_TARGET="python3_8 -python3_6 -python3_7" 1,957 KiB

Total: 3 packages (3 new), Size of downloads: 3,286 KiB

Would you like to merge these packages? [Yes/No]
...omitted...

Now that the package bluez has been installed, I should be able to start and enable the bluetooth service.

mathbook ~ # systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; disabled; vendor preset: disabled)
     Active: inactive (dead)
       Docs: man:bluetoothd(8)
mathbook ~ # systemctl enable bluetooth --now
Created symlink /etc/systemd/system/dbus-org.bluez.service → /lib/systemd/system/bluetooth.service.
Created symlink /etc/systemd/system/bluetooth.target.wants/bluetooth.service → /lib/systemd/system/bluetooth.service.
mathbook ~ # systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: disabled)
     Active: inactive (dead)
       Docs: man:bluetoothd(8)

Sep 13 14:00:11 mathbook systemd[1]: Condition check resulted in Bluetooth service being skipped.

Hmm… there is a problem. Let’s see if the bluetooth hardware is detected.

mathbook ~ # lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 27c6:5395 Shenzhen Goodix Technology Co.,Ltd. Fingerprint Reader
Bus 001 Device 002: ID 0cf3:e300 Qualcomm Atheros Communications
Bus 001 Device 004: ID 0c45:671d Microdia Integrated_Webcam_HD
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

In deed, the bluetooth hardware is detected: Bus 001 Device 002: ID 0cf3:e300 Qualcomm Atheros Communications. From this Gentoo wiki page, the bluetooth requires btusb. Let’s see if this kernel module has been loaded.

mathbook ~ # lsmod | grep btusb
mathbook ~ # modprobe -r btusb
modprobe: FATAL: Module btusb not found.

mathbook ~ # grep -i btusb  /usr/src/linux/.config
# CONFIG_BT_HCIBTUSB is not set

Apparently, not only that btusb kernel module wasn’t loaded, it wasn’t even built for this running kernel (5.8.8). After 30 minutes later, the kernel has been compiled and installed. It’s time to restart the machine.

mathbook ~ # systemctl status bluetooth
● bluetooth.service - Bluetooth service
     Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: disabled)
     Active: active (running) since Sun 2020-09-13 14:51:14 AEST; 55s ago
       Docs: man:bluetoothd(8)
   Main PID: 2030 (bluetoothd)
     Status: "Running"
     Memory: 2.0M
        CPU: 8ms
     CGroup: /system.slice/bluetooth.service
             └─2030 /usr/libexec/bluetooth/bluetoothd

Sep 13 14:51:14 mathbook systemd[1]: Starting Bluetooth service...
Sep 13 14:51:14 mathbook bluetoothd[2030]: Bluetooth daemon 5.54
Sep 13 14:51:14 mathbook systemd[1]: Started Bluetooth service.
Sep 13 14:51:14 mathbook bluetoothd[2030]: Starting SDP server
Sep 13 14:51:14 mathbook bluetoothd[2030]: Bluetooth management interface 1.17 initialized

There is a progress. The bluetooth module could be started and is running. Let’s verify if the bluetooth device itself is blocked or not.

❯ rfkill
ID TYPE      DEVICE      SOFT      HARD
 0 bluetooth hci0   unblocked unblocked
 1 wlan      phy0   unblocked unblocked

The above output indicates that the bluetooth device is unblocked. Let’s proceed with paring the the MX Anywhere 2S mouse with this Gentoo box.

❯ bluetoothctl
Agent registered
[CHG] Controller 9C:B6:D0:BD:XX:XX Pairable: yes
[bluetooth]# scan on
Discovery started
[CHG] Controller 9C:B6:D0:BD:1E:BA Discovering: yes
[NEW] Device 44:8E:C4:74:A7:EE 44-8E-C4-74-A7-EE
[NEW] Device F9:37:4E:94:XX:XX MX Anywhere 2S
[NEW] Device 6A:B9:36:49:37:64 6A-B9-36-49-37-64
[NEW] Device 6E:1C:40:0D:E4:C3 6E-1C-40-0D-E4-C3
[NEW] Device 4E:91:24:7F:DE:5C 4E-91-24-7F-DE-5C
[NEW] Device 42:0C:C1:35:2F:9C 42-0C-C1-35-2F-9C
[NEW] Device 5D:D2:3E:3A:92:D6 Ace 2
[bluetooth]# scan off
Discovery stopped
[bluetooth]# pair F9:37:4E:94:XX:XX
Attempting to pair with F9:37:4E:94:xx:xx
[CHG] Device F9:37:4E:94:xx:xx Connected: yes
...omitted...
[CHG] Device F9:37:4E:94:xx:xx ServicesResolved: yes
[CHG] Device F9:37:4E:94:xx:xx Paired: yes

The devices could be paired but, unfortunately, the mouse is still not working. Time to go back and read more documents.

According to the Gentoo wiki page about Bluetooth input devices, we need to change the value of UserspaceHID to true in /etc/bluetooth/input.conf to enable user-space HID support.

# Enable HID protocol handling in userspace input profile
# Defaults to false (HIDP handled in HIDP kernel module)
UserspaceHID=true

And the User-space HID support also requires the User-space I/O driver for HID input devices (CONFIG_UHID) to be enabled:

KERNEL Enabling user-space-hid support

Device Drivers --->
    HID support --->
        <*>   User-space I/O driver support for HID subsystem

After spending another 30 minutes to recompile the kernel, installing, and rebooting, this time it works!

References: