Enabling bluetooth on MQ-Quad

I’m trying to get bluetooth working on the armbian build for MQ-Quad. I first figured out that the bt connection is on UART1 which was not enabled in the device tree. I hacked together an overlay to enable it and it now successfully configures UART1 as /dev/ttyS1. I then got source for rtk_hciattach and tried attaching to ttyS1, but it failed since it needed firmware for rtl8723ds, which was not included in the image. Once I tracked that down and placed it properly, rtk_hciattach was able to communicate with the bluetooth chip and successfully send it firmware and complete initialization. Unfortunately, immediately after that, I get a kernel Oops with almost no information. Without kernel source, I’m unable to debug further. Any ideas or ways I can debug this kernel panic with the image I currently have?

Eventually I was able to get more information about the kernel oops from dmesg, though it’s still going to be difficult to track down without kernel source:

[ 1036.076617] Bluetooth: : Non-link packet received in non-active state
[ 1036.077538] Unable to handle kernel NULL pointer dereference at virtual address 00000000000001b0
[ 1036.086470] Mem abort info:
[ 1036.089386] ESR = 0x96000004
[ 1036.092530] EC = 0x25: DABT (current EL), IL = 32 bits
[ 1036.097918] SET = 0, FnV = 0
[ 1036.101044] EA = 0, S1PTW = 0
[ 1036.104216] FSC = 0x04: level 0 translation fault
[ 1036.109164] Data abort info:
[ 1036.112085] ISV = 0, ISS = 0x00000004
[ 1036.115977] CM = 0, WnR = 0
[ 1036.118960] user pgtable: 4k pages, 48-bit VAs, pgdp=000000004a28d000
[ 1036.125451] [00000000000001b0] pgd=0000000000000000, p4d=0000000000000000
[ 1036.132303] Internal error: Oops: 96000004 [#1] SMP
[ 1036.137201] Modules linked in: hci_uart btqca btrtl btbcm btintel bluetooth ecdh_generic ecc sunxi_cir rc_core dw_hdmi_cec sun50iw9_codec sunxi_cedrus(C) videobuf2_dma_contig v4l2_mem2mem videobuf2_memops videobuf2_v4l2 videobuf2_common videodev zram mc panfrost gpu_sched display_connector cpufreq_dt 8723ds cfg80211 fuse dwmac_sun8i i2c_mv64xxx mdio_mux
[ 1036.168525] CPU: 3 PID: 1526 Comm: kworker/u9:1 Tainted: G C 5.16.17-sun50iw9 #3.0.8
[ 1036.177565] Hardware name: OrangePi Zero2 (DT)
[ 1036.182003] Workqueue: hci0 hci_cmd_work [bluetooth]
[ 1036.187025] pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=–)
[ 1036.193979] pc : pm_runtime_drop_link+0x410/0x8f8
[ 1036.198686] lr : h5_enqueue+0x68/0x130 [hci_uart]
[ 1036.203396] sp : ffff8000096dbce0
[ 1036.206704] x29: ffff8000096dbce0 x28: 0000000000000000 x27: 0000000000000000
[ 1036.213833] x26: ffff0000062ea874 x25: ffff00000a00b405 x24: ffff00000631e8b0
[ 1036.220962] x23: 0000000000000000 x22: ffff000005e54a78 x21: ffff000003b0bf70
[ 1036.228091] x20: 0000000000000004 x19: 0000000000000000 x18: 0000000000000000
[ 1036.235219] x17: 537974742f797474 x16: 0000000000000000 x15: 00000000000c8000
[ 1036.242348] x14: 0000000000000000 x13: 0000000000000020 x12: 0101010101010101
[ 1036.249476] x11: 7f7f7f7f7f7f7f7f x10: fefefefefefefeff x9 : 7f7f7f7f7f7f7f7f
[ 1036.256606] x8 : 8080808080808080 x7 : 0000000080808080 x6 : 00000005cf330a07
[ 1036.263735] x5 : ffff000004dad724 x4 : 0000000000000000 x3 : ffff000004dad724
[ 1036.270863] x2 : 00000000000001b0 x1 : 00000000000001b0 x0 : 0000000000000000
[ 1036.277994] Call trace:
[ 1036.280437] pm_runtime_drop_link+0x410/0x8f8
[ 1036.284792] h5_enqueue+0x68/0x130 [hci_uart]
[ 1036.289151] hci_uart_send_frame+0x6c/0x188 [hci_uart]
[ 1036.294289] hci_send_frame+0x70/0x118 [bluetooth]
[ 1036.299103] hci_cmd_work+0xd4/0x180 [bluetooth]
[ 1036.303739] process_one_work+0x1f4/0x3c8
[ 1036.307747] worker_thread+0x140/0x4e0
[ 1036.311491] kthread+0x148/0x158
[ 1036.314716] ret_from_fork+0x10/0x20
[ 1036.318293] Code: 35ffff84 17fff798 d5184600 f9800051 (885f7c40)
[ 1036.324379] —[ end trace 58c63f7af09b49a7 ]—

Further update, it looks like the kernel module hci_uart.ko needs this patch: [PATCH 5.16 0617/1017] Bluetooth: hci_uart: add missing NULL check in h5_enqueue - Greg Kroah-Hartman

Without kernel source though, I’m not sure how I’ll apply it.

Very quick update, I got it working!!

I ended up building a new kernel from GitHub - dumtux/Allwinner-H616: 🔥 Linux SDK for Allwinner H616, based on armbian-build but copying over the config from the debian install on the mango pi and then modifying that a bit plus applying the kernel patch I mentioned previously.

I still need to write a systemd service to run rtk_hciattach on boot, but once that’s complete, I’ll post all of my files here along with some instructions for installation.

Hmm, I can’t upload files here, so I’m providing a link to the zip with all of the required files: mangopi-mq-quad-bt.zip - Google 雲端硬碟

To set this up:

  1. Unzip the files somewhere
  2. Copy rtk_hciattach to /usr/bin/
  3. Copy rtl8723d_config and rtl8723d_fw to /lib/firmware/rtlbt/
  4. Copy sun50i-h616-uart1.dtbo to /boot/dtb/allwinner/overlay/
  5. Copy hciattach.service to /etc/systemd/system
  6. Install the new kernel by running “sudo dpkg -i linux-image-current-sun50iw9_3.0.0_arm64.deb”
  7. Open /boot/orangepiEnv.txt in a text editor and add the following two lines at the bottom:
  8. Reboot
  9. Run “sudo systemctl enable hciattach”
  10. Power off the system and disconnect from power
  11. Reconnect power and boot the system

That should be it! hciconfig should now show hci0 device is UP. bluetoothctl should how a controller and be able to scan and connect to devices.

One last thing. If you reboot the system without removing power in the future, the bluetooth won’t come up. This is because currently, nothing resets the bluetooth chip during reboot, so rtk_hciattach won’t be able to resend it the firmware and the system won’t be able to initialize it.

This isn’t important in my project so I have chosen not to try and fix it, but if anyone has any ideas on how to fix it, I’d be open to collaborating.

Another quick warning, it looks like HDMI may no longer be working with this new kernel I posted above. I didn’t notice as I’m running mostly headless, but figured I’d let people know here in case that’s important to you.

I’ve now got HDMI working with this new kernel as well. I’ve updated the above zip file with sun50i-h616-de-fixup.dtbo. To get HDMI working, copy this file to /boot/dtb/allwinner/overlay and change the following line in /boot/orangepiEnv.txt

overlays=uart1 de-fixup

Then reboot. Note that this should only be done if you’re running or about to run the new kernel. If you use this overlay with the default kernel, it will disable your HDMI.

Hello, thanks for the good work! But for some reason, the system does not determine the entire amount of RAM, but only half. How can this be corrected?

uname -a
Linux debian 5.16.17-sun50iw9 #3.0.0 SMP Wed Mar 8 19:23:56 PST 2023 aarch64 GNU/Linux

Thank you.

That’s strange, I’m not encountering that on my board. Can you post the contents of /proc/meminfo to start? Also, do you know how to increase kernel log buffer size? If not, I can walk you through it. We may need to look at the log at boot.

1 Like

I have EDJ8416E6MB (512M words × 16bits) at the board. (ver 1.2) Chip link
That is 512 MB.

1 Like

Yes, memory J8416E6MB is indeed installed on my board. But it is strange that on the official Debian image 1 GB memory is defined :slightly_smiling_face: .
cat /proc/meminfo | grep “Mem”
MemTotal: 1007736 kB
MemFree: 616316 kB
MemAvailable: 789036 kB

Thanks for clarifying).

1 Like
root@mangopi:/proc# cat /proc/meminfo | grep Mem
MemTotal:         495708 kB
MemFree:          250040 kB
MemAvailable:     403120 kB

root@mangopi:/proc# uname -a
Linux mangopi 5.16.17-sun50iw9 #3.0.0 SMP Wed Jan 11 20:38:13 CET 2023 aarch64 GNU/Linux

Probably you have wrong setting in kernel.

Yes, this error is in the official image.

You are mistaken. 512M words x 16 bits / 8 = 1,073,741,824 bytes, totaling 1GB. :slightly_smiling_face:

Where can I find the version of Armbian that this works on? It almost works on Debian bullseye with the same kernel version, but there’s some service startup issue I haven’t managed to solve yet - the bluetooth service fails on boot, but if I log in and restart the service from the command line, I can see the hci0 device fine.

Fixed! I replaced “RequiredBy=bluetooth.service” with “RequiredBy=multi-user.target” in hciattach.service, and the hci0 device now comes up on boot.

Any idea if this will work on the MQ-Pro as well? (Bluetooth doesn’t work there by default either.)