HackWeek

Last week was 22nd openSuse HackWeek and first one I was participating in. The initial idea was to package Ada stack for openSuse but it was done already by Vitalii Bondarenko. Then I realized that I’ve spent few bucks on MangoPi MQ Pro boards which are laying around for some time and decided to build my own image for it. Goals were:

  • Build image with working hardware (most importantly Wifi and Bluetooth as I’ve not found working Bluetooth driver in any other image available, the reason(s) will be below)
  • Brush on my Yocto Project skills. I’ve been working with this amazing project for one year and a half now but mostly on toolchain side and almost haven’t touched BSP related topic
  • Build image which can be easily customized for my needs (it’s embedded after all)
  • Have some fun :-)

Thankfully meta-riscv layer for Yocto Project with nezha board definition was available already and I was able to build and boot image from it. While the image was bootable and even somehow worked hdmi output didn’t work as well as wifi and bluetooth. The only communication mechanism was serial console which is not the best option :) The reason is simple: nezha and MangoPi MQ Pro share same SoC but peripheral devices differ a lot.

HDMI

HDMI output has been fixed relatively easy: by choosing proper Device Tree description and putting it into machine configuration.

Wifi

Wireless chip’s (Realtek RTL8723ds) driver is not in the mainline kernel but available as out-of-tree module. Packaging of it was relatively easy (even if it was my first experience of out-of-tree module packaging the Yocto Project documentation covers this topic quite well). After adding

MACHINE_EXTRA_RRECOMMENDS += " kernel-modules kernel-module-rtl8723ds"

into machine configuration file and rebuilding of the image wifi card appeared in the system. Win!

Bluetooth

While bluetooth chip is the same as wifi one it connected via UART interface and driver is different. Funny enough the support for rtl8723ds_bt is in mainline kernel so the impression was it’ll be easy to enable one. This was huge mistake… First of all it required kernel options to be enabled. This was easy task (and yes, Yocto Project documentation covers this topic pretty well). The config has been generated and kernel built. Not a rocket science for gentoo user :)

CONFIG_BT=m
CONFIG_BT_BREDR=y
CONFIG_BT_LE=y
CONFIG_BT_DEBUGFS=y
CONFIG_BT_RTL=m
CONFIG_BT_HCIUART=m
CONFIG_BT_HCIUART_SERDEV=y
CONFIG_BT_HCIUART_3WIRE=y
CONFIG_BT_HCIUART_RTL=y
CONFIG_CFG80211=m
CONFIG_MAC80211=m
CONFIG_RFKILL=m
CONFIG_RFKILL_LEDS=y
CONFIG_BRCMUTIL=m
CONFIG_BRCMFMAC=m
CONFIG_SERIAL_DEV_BUS=y
CONFIG_SERIAL_DEV_CTRL_TTYPORT=y
CONFIG_CRYPTO_KPP=m
CONFIG_CRYPTO_ECC=m
CONFIG_CRYPTO_ECDH=m
CONFIG_CRYPTO_LIB_ARC4=m

Even with the proper config kernel didn’t see the device. I heard words “Device Tree” before and I knew it’s something to do with hardware description in arm/riscv/embedded world and it was all I knew about it :-) . This presentation helped me a lot in understanding of dts/dtb/dtc/device noded and related stuff. Few late night hours spent on debugging but dts and generated dtb looked good. The only clue was the fact that decompilled Device Tree from the device looked differently (and yes, the only difference was bluetooth module node missing). Further investigation showed that changes in dts files didn’t affect decompilled dts on device and sometimes weird dts’es appeared here and there. I’m already mature enough and know that magic exists only in the “Merlin” TV Series so the reason was not so magical: kernel and u-boot had different dts and that caused problem. DTS synced and driver appeared in the dmesg output but… The driver requires firmware blob from the vendor (rtl8723ds_fw and *_conf). While the blobs are available in Armbian or Realtek repository on github the license of the blob in unknown. The blob packaged with “CLOSED” and firmware loaded, however… Yes, new issue appeared and it was:

[   31.823459] Bluetooth: hci0: RTL: examining hci_ver=08 hci_rev=000d lmp_ver=08 lmp_subver=8723
[   31.836205] Bluetooth: hci0: RTL: rom_version status=0 version=2
[   31.842470] Bluetooth: hci0: RTL: loading rtl_bt/rtl8723ds_fw.bin
[   31.866849] Bluetooth: hci0: RTL: loading rtl_bt/rtl8723ds_config.bin
[   31.898736] Bluetooth: hci0: RTL: cfg_sz 25, total sz 34605
[   32.483333] Bluetooth: hci0: RTL: fw version 0xaa897e1b
[   32.553924] Bluetooth: hci0: Opcode 0x c77 failed: -56

Thankfully the patch to fix this issue has been send few days before I hit the issue and while it was for another device it was trivial task to apply the quirck for rtl8723ds as well. and finaly:

[bluetooth]# list
Controller 68:B9:D3:6C:90:DB mangopi-mq-pro [default]

The conclusion

It was really interesting task and I’m very satisfied with the outcome. The results are merged into meta-riscv repository and now owners of the MangoPi MQ Pro board can build their own image for this device (do not forget to put firmware into lib/firmware/rtl_bt) and enjoy working Bluetooth!