Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
drivers/bluetooth: step one for the WCN6750's Bluetooth half, a read-only look at its three pins (bt=1)
shimoku5cdcd8839496
diff --git a/configs/common_defconfig b/configs/common_defconfigindex 5eb98ef..5424115 100644--- a/configs/common_defconfig+++ b/configs/common_defconfig@@ -2,7 +2,7 @@# HakiOS common defconfig: every setting there is, with what it does.# Made by tools/hakios-config.py listing common; the values are the ones that file sets, and tests/CI check that it names every symbol.#-# 313 symbols in 17 groups; 9 are set.+# 313 symbols in 17 groups; 2 are set.##@@ -16,14 +16,14 @@# CONFIG_SOC is not set# "device": options may use firmware pulled from your own phone (fw-*/); "none": the open configuration, nothing of ...-CONFIG_FIRMWARE="device"+CONFIG_FIRMWARE="none"## CPU, memory and boot (21)## CPU cores the kernel uses, 1 to 8; one if not set-# CONFIG_CPUS is not set+CONFIG_CPUS=1# the core the kernel runs on, 0 to 7; 7 is the biggest core (the loader keeps core 0)# CONFIG_BOOTCPU is not set@@ -90,7 +90,7 @@ CONFIG_FIRMWARE="device"## the phone's storage: 1 a survey, read only; 2 the loader's block service, through which the kernel reads it (the ...-CONFIG_UFS=2+# CONFIG_UFS is not set# let the kernel write, to the partition `darwin` and nowhere else (writes to the phone's storage: it is the one ... [requires CONFIG_UFS=2]# CONFIG_UFS_WRITE is not set@@ -226,7 +226,7 @@ CONFIG_UFS=2## the touch screen, and the power and volume buttons-CONFIG_TOUCHSCREEN=y+# CONFIG_TOUCHSCREEN is not set# read by hakios.buttons.c (kernel)# CONFIG_BUTTONS is not set@@ -254,7 +254,7 @@ CONFIG_TOUCHSCREEN=y## rendering on Adreno (an image built with GL=1; the GPU's firmware from your phone) [needs fw-gpu/ from the phone]-CONFIG_GPU=y+# CONFIG_GPU is not set# read by hakios.adreno.c (kernel)# CONFIG_ADRENO is not set@@ -459,10 +459,10 @@ CONFIG_GPU=y## the audio DSP; the battery level in the UI and charging state come through it [needs fw-audio/ from the phone]-CONFIG_ADSP=y+# CONFIG_ADSP is not set# the speakers (4 is what the README's phone line uses) [needs fw-audio/ from the phone] [requires CONFIG_ADSP]-CONFIG_SOUND=4+# CONFIG_SOUND is not set# the microphone (4 is what the README's phone line uses) [needs fw-audio/ from the phone] [requires CONFIG_ADSP]# CONFIG_MIC is not set@@ -559,7 +559,7 @@ CONFIG_SOUND=4## Wi-Fi: the radio, its driver, a connection (also fw-wifi/mac and fw-wifi/network) [needs fw-wifi/ from the phone]-CONFIG_WIFI=y+# CONFIG_WIFI is not set# start the DSP although the one before it was shut down without having acknowledged a stop: THAT HAS RESET THE PHONE, ...# CONFIG_WIFI_AGAIN is not set@@ -656,10 +656,10 @@ CONFIG_WIFI=y## the modem beside the system: its firmware, file systems, IPA, SIM, NAS [needs fw-modem/ from the phone] [requires CONFIG_UFS=2]-CONFIG_MODEM=y+# CONFIG_MODEM is not set# start the modem although a kernel before this one left it shut down [requires CONFIG_MODEM]-CONFIG_MODEM_AGAIN=y+# CONFIG_MODEM_AGAIN is not set# the modem's RMTFS buffer, an address below 4 GB (0xa8000000 on the reference phone) [requires CONFIG_MODEM]# CONFIG_MODEM_BUFFER is not setdiff --git a/drivers/README.md b/drivers/README.mdindex 1b3ad12..c968257 100644--- a/drivers/README.md+++ b/drivers/README.md@@ -26,6 +26,7 @@ has to be in `docs/boot-args.md` (`python3 tools/boot-args.py --check`).| `audio/` | `hakios.adsp.c`, `hakios.audio.c`, `hakios.sound.c`, `hakios.speaker.c`, `hakios.mic.c`, `hakios.virtio_sound.c` | `adsp=`, `sound=`, `mic=` | the audio DSP loader, `/dev/hakios-audio`, the speaker amplifiers and their guard, the microphone, QEMU's virtio sound || `power/` | `hakios.battery.c/.cpp`, `hakios.cpufreq.c`, `hakios.thermal.c`, `hakios.pmic.c`, `hakios.battguard.c` | `cpufreq=`, `battmgr=`, `thermal=`, `pmic=`, `battery_stop=` | battery readings (through the DSP), CPU frequency, temperature limits that hand control back to the loader, the PMIC, and an end to an unwatched run before the battery is flat (compiled only: see its head for what it was checked against) || `wifi/` | `hakios.wifi.c`, `hakios.wlfw.c`, `hakios.lx.c`, `hakios.sta.c`, `hakios.apple80211.c`, `hakios.wlan.cpp` | `wifi=`, `wifi_step=` | the Wi-Fi subsystem's bring-up, the WLAN firmware service, the Linux-driver layer (`wifi/lx/`) and the surface the iOS Wi-Fi service expects |+| `bluetooth/` | `hakios.bt.c` | `bt=` | Bluetooth of the WCN6750 radio chip: step one, a look at its three pins, read only (compiled only; the rest is written down in the file's head as what it needs) || `modem/` | `hakios.mpss.c`, `hakios.cell.c`, `hakios.qmi.c`, `hakios.nas.c`, `hakios.ipa.c` | `modem=` | the cellular modem path (research; not reviewed in `docs/driver-review/`) || `net/` | `hakios.net.c`, `hakios.virtio_net.c` | | the `enN` network interface and QEMU's virtio-net || `core/` | `hakios.version.c`, `hakios.socinfo.c` | `hakios_kver=`, `socinfo=` | `kern.hakios.version`; the SoC's identity as the hardware reports it |diff --git a/drivers/bluetooth/hakios.bt.c b/drivers/bluetooth/hakios.bt.cnew file mode 100644index 0000000..555dd6f--- /dev/null+++ b/drivers/bluetooth/hakios.bt.c@@ -0,0 +1,143 @@+/*+ * Bluetooth of the POCO X5 Pro (redwood): STEP ONE, a look. Only with boot-arg bt=1, and it only READS.+ *+ * The phone's radio chip is a Qualcomm WCN6750, one chip for Wi-Fi and Bluetooth (the phone's device tree: the node+ * bt_wcn6750, compatible qcom,wcn6750-bt; the Wi-Fi half is drivers/wifi/hakios.wifi.c). What the device tree says+ * about the Bluetooth half, and what this file takes from it (numbers, not text):+ *+ * qcom,bt-reset-gpio GPIO 85 the Bluetooth half's reset / enable line+ * qcom,bt-sw-ctrl-gpio GPIO 86 the line that tells the radio's switch which half has the antenna+ * qcom,wl-reset-gpio GPIO 84 the Wi-Fi half's reset line, listed here too: the chip comes up as a whole+ * the supplies (qcom,bt-vdd-*-supply, each with a -config of minimum and maximum microvolts; read out of the+ * device tree by script, not by eye), the same for both halves:+ * io 1800000..2000000 aon, dig, rfacmn, rfa-0p8 824000..1120000 rfa1 1872000..2040000+ * rfa2 1256000..1500000 ipa-2p2 2200000..2210000 asd 2800000..3544000+ * (which regulator each supply is, is a phandle in the tree; nothing here writes any of them)+ *+ * What this does. At boot-arg bt=1 it reads the three pins' configuration and level out of the TLMM (the same+ * registers the haptic driver reads for its pins) and prints them, once at the start and once bt_again seconds later+ * (default 30), to see whether the Wi-Fi bring-up (wifi=1) moved the reset lines in between. It writes NOTHING: no+ * pin, no supply, no clock, no serial engine. Reading a pin's configuration is a plain load from the TLMM, which the+ * haptic and thermal drivers do on this phone without trouble.+ *+ * What is NOT here, and what it needs. Bluetooth on this chip, as Android and Linux (hci_qca, btqca) do it:+ * 1. the supplies on (they are shared with Wi-Fi: wifi=1 already raises what the Wi-Fi half needs),+ * 2. the reset line high, and the chip given a moment,+ * 3. the UART of the Bluetooth half opened at 115200 baud (a Qualcomm serial engine, "hsuart0" in the device tree:+ * its clocks and its pins have to be set up first, and an access to an engine whose clock is off hangs the bus),+ * 4. the chip's own firmware loaded over that UART with Qualcomm's vendor commands (a rampatch and an NVM file out of+ * /vendor/firmware on the phone: NOT in the repository, and not pulled by devices/redwood/pull-firmware.sh yet; Linux+ * names them qca/msbtfw*.mbn and qca/msnv*.bin for the WCN6750, as I remember it, which is NOT verified here),+ * 5. the baud rate raised, then H4 packets (HCI) in both directions.+ * None of 1 to 5 is written, because none of it could be checked: the phone was not on the desk when this was written, and+ * the register numbers and command bytes for a chip are not the kind of thing to write from memory and ship. Each step comes+ * with a boot-arg level the way haptic=N and wifi_step=N did: the next one is bt=2 (supplies and reset line, seen on the+ * pins), after this one has been run and its output compared with a phone that is running Android.+ *+ * Status: COMPILED ONLY. Not run in QEMU (it has no such TLMM, and bt=1 must not be given there: a load from an address it+ * does not map faults) and not run on the phone.+ *+ * Boot-args:+ * bt=1 the look+ * bt_again=<s> the second look this many seconds later (0: no second look; 30)+ */+#include <stdint.h>+#include <kern/clock.h>+#include <kern/startup.h>+#include <kern/thread_call.h>+#include <libkern/libkern.h>+#include <mach/mach_types.h>+#include <pexpert/pexpert.h>+#include <sys/systm.h>++extern vm_offset_t ml_io_map(vm_offset_t phys_addr, vm_size_t size);+extern void loader_log_putc(char c);++static int say(const char *format, ...) __printflike(1, 2);++static int+say(const char *format, ...)+{+ char line[200];+ va_list list;++ va_start(list, format);+ vsnprintf(line, sizeof(line), format, list);+ va_end(list);+ for (const char *c = line; *c != 0; c++) {+ loader_log_putc(*c);+ }+ return printf("%s", line);+}++#define TLMM_PHYS 0x0f100000UL+#define TLMM_SIZE 0x100000UL+#define GPIO_CFG(n) (tlmm + 0x1000UL * (n)) /* [1:0] pull, [5:2] function, [8:6] drive, [9] output */+#define GPIO_IO(n) (tlmm + 0x1000UL * (n) + 4) /* bit 0 in, bit 1 out */+#define CFG_OUTPUT (1u << 9)++static vm_offset_t tlmm;+static uint32_t again_seconds = 30;+static thread_call_t again_call;++/* The three pins of the device tree's bt_wcn6750 node. */+static const struct {+ unsigned pin;+ const char *what;+} pins[] = {+ { 84, "qcom,wl-reset-gpio: the Wi-Fi half's reset" },+ { 85, "qcom,bt-reset-gpio: the Bluetooth half's reset" },+ { 86, "qcom,bt-sw-ctrl-gpio: the antenna switch" },+};++static inline uint32_t+rd(vm_offset_t address)+{+ return *(volatile uint32_t *)address;+}++/* One look at the pins: what the TLMM says each is set to, and the level it reads. Nothing is written. */+static void+look(const char *when)+{+ for (unsigned i = 0; i < sizeof(pins) / sizeof(pins[0]); i++) {+ uint32_t cfg = rd(GPIO_CFG(pins[i].pin)), io = rd(GPIO_IO(pins[i].pin));++ say("bt: %s: gpio%u (%s): function %u, pull %u, %u mA, %s, reads %u\n", when, pins[i].pin, pins[i].what, (cfg >> 2) & 15,+ cfg & 3, (((cfg >> 6) & 7) + 1) * 2, (cfg & CFG_OUTPUT) ? (io & 2 ? "drives high" : "drives low") : "input", io & 1);+ }+}++static void+look_again(__unused thread_call_param_t p0, __unused thread_call_param_t p1)+{+ look("again");+}++__startup_func+static void+bt_init(void)+{+ uint32_t enabled = 0, value;+ uint64_t deadline;++ if (!PE_parse_boot_argn("bt", &enabled, sizeof(enabled)) || !enabled) {+ return;+ }+ if (PE_parse_boot_argn("bt_again", &value, sizeof(value)) && value <= 3600) {+ again_seconds = value;+ }+ tlmm = ml_io_map(TLMM_PHYS, TLMM_SIZE);+ if (tlmm == 0) {+ say("bt: could not map the TLMM; off\n");+ return;+ }+ say("bt: level 1: a look, reading only (WCN6750; nothing is written)\n");+ look("at start");+ if (again_seconds != 0) {+ again_call = thread_call_allocate(look_again, NULL);+ clock_interval_to_deadline(again_seconds, NSEC_PER_SEC, &deadline);+ thread_call_enter_delayed(again_call, deadline);+ }+}+STARTUP(EARLY_BOOT, STARTUP_RANK_LAST, bt_init);diff --git a/drivers/map b/drivers/mapindex 7ef9d03..3040d67 100644--- a/drivers/map+++ b/drivers/map@@ -6,7 +6,7 @@# xnu/export.sh copies them back, tools/drivers-check.py (and CI) checks that this list and the directories agree.# A new hakios.* file in bsd/dev/ or iokit/Kernel/ has to be listed here: export.sh refuses to guess.#-# Subsystems: input storage gpu camera haptic audio power wifi modem net core+# Subsystems: input storage gpu camera haptic audio power wifi bluetooth modem net coreinput bsd/dev/hakios.goodix.cinput bsd/dev/hakios.focaltech_fw.h@@ -63,3 +63,4 @@ core bsd/dev/hakios.socinfo.cpower bsd/dev/hakios.thermal.cpower bsd/dev/hakios.pmic.cpower bsd/dev/hakios.battguard.c+bluetooth bsd/dev/hakios.bt.cdiff --git a/tools/drivers-check.py b/tools/drivers-check.pyindex df57991..26f54b7 100755--- a/tools/drivers-check.py+++ b/tools/drivers-check.py@@ -13,7 +13,7 @@ usage: tools/drivers-check.py [--root DIR] exit 0 if all is well, 1 and theimport osimport sys-SUBSYSTEMS = {"input", "storage", "gpu", "camera", "haptic", "audio", "power", "wifi", "modem", "net", "core"}+SUBSYSTEMS = {"input", "storage", "gpu", "camera", "haptic", "audio", "power", "wifi", "bluetooth", "modem", "net", "core"}TREE_DIRS = ("bsd/dev/", "iokit/Kernel/")diff --git a/xnu/hakios.patch b/xnu/hakios.patchindex ef9c3a8..8261520 100644--- a/xnu/hakios.patch+++ b/xnu/hakios.patch@@ -62,10 +62,10 @@ index a4d25040f38f..aad72993d139 100644kern_debug.o_INCFLAGS += -I$(SRCROOT)/osfmkdiff --git a/bsd/conf/files b/bsd/conf/files-index 44274016a4cf..0897299f8fa2 100644+index 44274016a4cf..5f815c4379c0 100644--- a/bsd/conf/files+++ b/bsd/conf/files-@@ -109,6 +109,46 @@ bsd/dev/munge.c optional config_requires_u32_munging+@@ -109,6 +109,47 @@ bsd/dev/munge.c optional config_requires_u32_mungingbsd/dev/unix_startup.c standardbsd/dev/monotonic.c optional monotonic@@ -108,11 +108,12 @@ index 44274016a4cf..0897299f8fa2 100644+bsd/dev/hakios.battguard.c standard+bsd/dev/hakios.pmic.c standard+bsd/dev/hakios.thermal.c standard++bsd/dev/hakios.bt.c standard+bsd/dev/hakios.socinfo.c standardbsd/dev/dev_perfmon.c optional config_perfmonbsd/libkern/crc32.c standard-@@ -155,6 +195,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfs+@@ -155,6 +196,14 @@ bsd/miscfs/devfs/devfs_vfsops.c optional devfsbsd/miscfs/mockfs/mockfs_fsnode.c optional mockfsbsd/miscfs/mockfs/mockfs_vfsops.c optional mockfsbsd/miscfs/mockfs/mockfs_vnops.c optional mockfs@@ -127,7 +128,7 @@ index 44274016a4cf..0897299f8fa2 100644bsd/miscfs/routefs/routefs_ops.c optional routefs-@@ -361,6 +409,7 @@ bsd/security/audit/audit_syscalls.c standard+@@ -361,6 +410,7 @@ bsd/security/audit/audit_syscalls.c standardbsd/security/audit/audit_worker.c optional config_auditbsd/pthread/pthread_shims.c standard@@ -135,7 +136,7 @@ index 44274016a4cf..0897299f8fa2 100644bsd/pthread/pthread_priority.c standardbsd/pthread/pthread_workqueue.c standard-@@ -383,6 +432,7 @@ bsd/kern/kern_debug.c standard+@@ -383,6 +433,7 @@ bsd/kern/kern_debug.c standardbsd/kern/kern_symfile.c standardbsd/kern/kern_descrip.c standardbsd/kern/kern_guarded.c standard