diff --git a/device/tegu/CALL-AUDIO-TUNING.md b/device/tegu/CALL-AUDIO-TUNING.md new file mode 100644 index 000000000..4fe8eaaba --- /dev/null +++ b/device/tegu/CALL-AUDIO-TUNING.md @@ -0,0 +1,385 @@ + +# Pixel 9a: call tuning and the first earpiece route + +Status, 2026-11-13: the trial has run, and the tuning doesn't cancel the +echo. On the daily kernel +(`7.3.0-rc6-omamobile-tegu-s2mpg15-m1-g991042e2eff3-dirty`), speakerphone +calls with `tegu_aec = yes`: the AoC accepts both uploads, its APMg3 +processor runs with stock's tuning and has a reference signal, and the far +end still hears its own voice. Measured on calls to a talking clock +(results 8 to 11), the uplink with the tuning is quiet most of the time and +the echo comes through in bursts louder than anything the unprocessed +uplink had; the echo is acoustic, speakers to microphones. The "two seconds +without echo" of the early calls were a silent uplink after the answer, not +cancellation. Why the cancellation doesn't hold isn't known ("Next for the +echo"). So `tegu_aec` stays off by default and is not a setting to turn on +for better calls; call audio still defaults to `card = none`. What is +proven is the transport (the tuning service takes the files and the AoC +confirms them), the extractor and this record. The earpiece mode below is a +conservative top-speaker route, not a reproduction of every stock codec +profile setting, and it has not been tried in a call. + +## Sources and the tuning transport + +The command and its fields come from our GPL kernel on `omamobile`: +`struct CMD_AUDIO_OUTPUT_SET_PARAMETER_BULK` (ID 210) and `struct CMD_HDR` +in `drivers/soc/google/aoc/aoc-interface-gs201.h`, the character services +in `aoc_char_dev.c`, and the `Audio DSP Mode` and `FT AEC Reference Source` +controls in `alsa/aoc_alsa_ctl.c`. The tuning files themselves are the +owner's, copied unchanged from the factory image's `/vendor/etc/aoc`; no +vendor program is run or copied into the repository. + +Each protobuf goes to the AoC wrapped in a 12-byte little-endian envelope, +the form stock's telephony tuning sends and the palmtop's AoC accepts +(status 0): + +| Offset | Type | Meaning | +| --- | --- | --- | +| 0 | u16 | 2 (a tag; its name isn't known) | +| 2 | u16 | Total envelope plus protobuf length | +| 4 | u32 | 19 for uplink tuning, 20 for downlink tuning | +| 8 | u32 | Protobuf length | +| 12 | bytes | The protobuf, unchanged | + +Both operations use AoC **block 19, component 24** on the output tuning +service (`audio_output_tuning`), not the input one. Write the envelope padded to a full +**16384-byte block** to `/dev/acd-audio_bulk_tx`, then send a **22-byte** +CMD_AUDIO_OUTPUT_SET_PARAMETER_BULK (ID 210 / `0x00d2`) record on +`/dev/acd-audio_output_tuning`: + +| Bytes | Field | +| --- | --- | +| 0, 1 | `CONTAINER_HDR` type 0, counter (the AoC rewrites the counter in its reply) | +| 2–3 | u16 length 22 | +| 4–5, 6–7 | u16 command ID 210, signed i16 reply initially 0 | +| 8, 9 | u8 block 19, component 24 | +| 10–13, 14–17 | u32 TX offset 0, TX address 0 | +| 18–21 | u32 `tx_size`: the envelope length, not the padding length | + +The input side has its own command, `CMD_AUDIO_INPUT_SET_PARAMETER_BULK` +(ID 234), on `audio_input_tuning`; it isn't used here. This upload needs no +bulk RX read: the reply comes back on the tuning service. + +`aoc_tuning.py` implements only the two uplink/downlink operations, with +bounded nonblocking writes, one-second readiness/acknowledgement deadlines, +exact record lengths, and a reply that matches the command's type, length +and ID with a zero status. The second header byte, the counter, isn't +compared: the AoC answers with its own (0x01 sent, 0x3e back on the +palmtop), which the first version took for an unknown outcome (0.3.19). +It never resumes a short record, retries an unknown command, maps SRAM or +opens the CP/AoC shared voice window. `aoc_char_dev` makes the service +opens exclusive. An uncertain write/reply leaves the private RAM marker +`/run/omamobile-sit/aoc-voice-tuning-uncertain`; later tuning attempts refuse. +The unit already preserves its runtime directory. Do not delete the marker +to retry on the same AoC session. + +**Echo reference is a separate ALSA step.** +`VoiceCall::EchoReferencePath::Set` selects **FT AEC Reference Source = +SPEAKER** for the speaker and handset paths. Our kernel's setter caches +that choice; `aoc_audio_modem_mic_input()` uses it as the reference input +when constructing MODEM_INPUT_START_2. Set it before EP4 capture starts. +Its saved value is restored after the PCMs stop. There is no additional SIT +request. The existing EP4/EP5 preparation starts the modem voice path. + +`SendCommonCommand` is not the AEC protobuf transport. Its observed caller +here toggles **microphone compensation** with input-family command 355/356, +using an eight-byte CMD_HDR. Sending those would enable a different feature; +the implementation does not send them. The complete stock hybrid tuner's +band, volume and processing-mode policy is outside this first loader. +Actual echo cancellation still needs a far-end listening test. + +## Files and lifecycle + +`tools/extract-vendor-firmware -d tegu` now copies these six owner files +unchanged into **`usr/lib/firmware/updates/omamobile/tegu/aoc/`** in its +output tree, included in `vendor-firmware.sha256`: + +| Route | Uplink on | Uplink cleanup | Downlink | +| --- | --- | --- | --- | +| Speakerphone | `uplink_speaker_aec_on_config.pb` | `uplink_speaker_aec_off_config.pb` | `downlink_speaker_config.pb` | +| Earpiece | `uplink_handset_aec_on_config.pb` | `uplink_handset_aec_off_config.pb` | `downlink_handset_config.pb` | + +The source is `/vendor/etc/aoc/` in the owner's own 9a image. Do not use +another device's protobufs. HAC, wired, Bluetooth, dock and external-speaker +configs are unnecessary for these two routes. The six are one optional set: +when any of them is missing from the image, extraction warns, leaves the +whole set out and extracts the rest. The loader requires the selected route's on, off and +downlink files before any upload. Files are not shipped in the package. + +With `[audio] tegu_aec = yes`, the route first saves the mixer state and +verifies sink mute, sets the echo reference, uploads uplink-on and downlink, +and starts capture/playback once with the existing voice parameters. It +then applies and reads back the amplifier attenuation before unmuting. +Hang-up mutes, stops both PCMs, sends the same route's uplink-off package, +closes the tuning services and restores the mixers. **This turns AEC off; +it does not claim to restore an unknown previous protobuf**, and leaves the +selected downlink tuning resident in the inactive telephony component. + +A missing or refused tuning setup refuses audio while call signalling +continues. Unknown setup or failed cleanup leaves the sink muted; the CP +is never stopped or reset. Tests cover cleanup, stale/mismatched/negative +acknowledgements, short writes and an unknown outcome blocking another call. + +## Earpiece mapping and the limit of the stock comparison + +The stock GPL TAS parser is +[Google amplifiers `33976d763bc0ace11603e7ecb207fc2b6e3f2608`](https://android.googlesource.com/kernel/google-modules/amplifiers/+/33976d763bc0ace11603e7ecb207fc2b6e3f2608/tas25xx/src/tas25xx-regbin-parser.c). +It builds playback-volume controls from the image's KCNTR table; values +are table indices, not decibels. A bounded private inspection of the owner's +table, following that parser, established: + +| Stock setting | Mainline mapping / implementation | +| --- | --- | +| PLAYBACK_VOLUME_LEFT = 55 | Its coefficient matches **tas2x20 DVC index 220 (unity)**, not index 55. Use a lower first-test level: **Top Speaker Digital Volume = 160 (-30 dB)** | +| Top amplifier only | **Bottom Speaker Digital Volume = 0**, read back before unmuting. This is the table's minimum (-110 dB), not power-off or a hardware mute | +| CODEC PROFILE = voice | Mainline exposes per-amplifier **Speaker Profile Id**, not the vendor enum. The examined native RCA's index **4** is `case05-voice-handset`, but that case contains only one power-state write. It lacks profile 0's full initialization. Selecting 4 on a fresh, uninitialized amplifier is not a complete voice route | +| EM LEFT/RIGHT = OFF | Mainline has **no EM mixer control**. The stock driver writes bits 7:6 of register 0:0:3. The examined full native profile 0 writes EM off; no userspace register write or invented control is added | +| AoC Modem Sink Channel Bitmap | **Read-only**: its SOC_SINGLE_EXT has a get handler and NULL put. It reports a sink's active channel bitmap; it is not a writable TDM-slot selector in this kernel | + +For the first usable top-only mode, retain the existing **full profile 0** +on both amplifiers, load handset AEC/downlink tuning, and attenuate/mute as +above. Refuse earpiece audio if the selected amplifier profiles are not 0, +or AEC was not enabled. This keeps startup on the already working native +initialization instead of treating a profile label as sufficient evidence. +Only the top is intended to be audible; the bottom still powers up. +Exact stock voice-profile and per-amplifier EM control remain a separate +firmware/kernel task if needed after this route is qualified. Gains have +not been acoustically qualified at the ear. + +Configuration (changes apply on the next **fresh CP** session): + +```ini +[audio] +card = none +tegu_aec = no +tegu_route = speaker +``` + +The attended trial chooses `card = auto`, `tegu_aec = yes`, then `speaker` +or `earpiece`. The earpiece mode does not affect the 7 Pro's CS35L41 route; +there is no mid-call route switch in this change. Do not enable it globally. + +## Verification and the attended gate + +Laptop checks, risk **low**, with synthetic service records and an ext4 +fixture containing fictional data only: + +```sh +tests/packages omamobile-modem-sit +tests/extract-tegu-call-tuning +tools/pr-check d826388 +``` + +Expect: all package tests, including the existing cheetah route and sit-sim +calls/SMS, pass; six synthetic tuning files are copied byte-for-byte and +checksummed; missing cleanup tuning fails; the PR gate passes. Stop on a +failure. Rollback is the parent branch or removal of this isolated +worktree. Nothing contacts the phone or executes the saved HAL. + +Before any phone trial, the human reviews a device-specific runbook and +approves the install and fresh boot. First **read-only**, confirm the NV +backup checksums, exact daily kernel/modules, mixer control availability, +profile files and their checksums, and no active call/PCMs. Keep the modem's +sleep inhibitor and once-per-boot rule. Record the actual `uname -r` with +results; no phone kernel release is claimed for these offline checks. + +Then test **speaker AEC only first**, with live kmsg streamed to the laptop, +a watchdog and the human ready to recover. Check command replies, gain +readbacks, two-way audio and the far end's echo before trying the earpiece +on another approved fresh session. Risk **medium**: new AoC tuning and an +unqualified acoustic path. Begin the earpiece test away from the ear at the +fixed attenuation. Stop on an AoC timeout/reset, any NV/UFS error, +unexpected gain, feedback, missing bottom attenuation or unconfirmed +cleanup. Hang up and retain the mute/uncertainty state. Never retry or reset +the running CP in place. Rollback is the previous package/config on an +attended fresh boot; no automatic unit restart, warm modem restart or +manual marker deletion. + +## Results on the palmtop, 2026-11-02 + +The daily kernel, `7.3.0-rc6-omamobile-tegu-s2mpg15-m1-g991042e2eff3-dirty`, +the six files from the owner's factory image (BP3A.250905.014, matching the +palmtop's AoC firmware), `tegu_aec = yes`, `tegu_route = speaker`, calls to +the owner's iPhone, which the owner answered: + +1. sitd 0.3.18 (before the counter fix): the uplink upload was accepted + with status 0, but the reply's counter byte differed, sitd took it for an + unknown outcome and kept the call silent, as designed. +2. 0.3.19: both uploads confirmed, audio both ways. The far end heard its + own voice come back, as without the tuning. +3. 0.3.20, with `Audio DSP Mode` set to `Telephony_APMG3` before the + uploads: the far end heard no echo for the first few seconds and a new + low hiss, then the echo came back with the hiss still there. + +So the uploads and the DSP mode take effect, but the cancellation doesn't +hold yet. The earpiece route hasn't been tried. + +## Results on the palmtop, 2026-11-03 and 2026-11-11 + +Two more changes, each tried on calls to the owner's iPhone on the +speakerphone, the owner listening on the far end, `tegu_aec = yes`, +`tegu_route = speaker`, the same six files, the daily kernel as above: + +4. 2026-11-03, `AoC Modem Mixer ASP Mode` set to `ASP_ON` before the + uploads (and, in another call, stock's microphone gain): no echo for + 2 to 3 s, then the echo again, in both. +5. 2026-11-11, the tuning sent after both voice streams have started + instead of before, with the DSP mode and `ASP_ON` as in 4 (sitd built + on the palmtop from this stack's head merged onto that day's main, as + 0.3.37): two calls, 25 s and 14 s. The owner heard no echo for about + the first 2 s of each and his own voice for the rest. sitd logged + "AoC tegu speakerphone prepared at conservative TAS gain" at each + call's start and "AoC call routes restored" at its end, and no refusal, + so both uploads were acknowledged (an unconfirmed one keeps the call + silent). + +So nothing tried so far makes the cancellation hold: with the tuning +before or after the streams' start, with ASP on or off, the far end hears +no echo for about 2 s and then hears it. Results 3 to 5 read those first +seconds as cancellation that then stopped. The calls of 2026-11-12 below +show they are not that. + +## Results on the palmtop, 2026-11-12: the first seconds are a silent uplink + +The same setup (the daily kernel, sitd 0.3.37 built from this stack's +head, `tegu_aec = yes`, `tegu_route = speaker`), with +`bringup/call-audio-watch.py` running on the palmtop through each call. It +is read-only: it prints the AoC's own log (`/dev/acd-logging`) as it +arrives and, unless `CTL=0`, reads fifteen of the call's mixer controls +every half second and prints the changes. `bringup/call-levels.py` reads +its output on the laptop. + +6. Four calls to the owner's iPhone, the owner in another room, and in + the last in a far one; the watcher ran through the first three. He + heard no echo for 3 to 4 s after answering and then his own voice, as + before. + - **The controls are set once and stay.** At the call's start: + `Audio DSP Mode` `Telephony_APMG3`, `AoC Modem Mixer ASP Mode` + `ASP_ON`, `FT AEC Reference Source` `SPEAKER`, `MIC HW Gain (cB)` 130, + microphones 0 and 1 on. Nothing changed until the hang-up. + - **The AoC has the tuning.** Its log has both uploads right after + "PCIe Modem Telephony Started" (command 0x00d2 on the tuning service, + rc 0, then "APMG3 uplink config received" and "APMG3 downlink config + received"), with "Telephony Voice Processor enabled" and "Mixer 0 + active (APMg3)" just before. Nothing in the log changes a few + seconds into the call. + - **The answer is a new session.** The audio path first gets one modem + session (the ringing; its configuration arrives about 2 s after the + path starts, with "Modem Path Active but no configuration received" + until then), and at the answer a second one: "Session ID changed but + no handoff performed. Forcing FadeOut!", a new "PCIe Modem + Configuration" with the next session number, both resamplers reset. + - **The uplink is silent for the first 5 s of the answered session.** + The AoC prints one uplink and one downlink sample about three times + a second ("PCIe Session N ... Tx: ... Rx: ..."). In the first 5 s + after each answer all sixteen uplink samples were below 80 (16-bit + values), in the three calls, while the downlink carried the owner's voice + at several thousand; after that the uplink had samples in the + hundreds and thousands. +7. The same with a voice next to the palmtop: the laptop beside it counted + aloud through the call while the owner, in the far room, said nothing. + He heard the counting only in the room at first and through the phone + a few counts later. The uplink samples agree: below 80 for the first + 5 s after the answer with the counting going on, then up to 2,560. So + the uplink carries nothing in those seconds, the palmtop's own side + included, and the "no echo" of results 3 to 6 is that silence. +8. A call with no listener: the palmtop dialled a talking clock (NIST's + time line), nobody near it, 80 s. In the 50 s of ticks alone the uplink + stayed below 100 in all but 5 of 156 samples; in the two voice + announcements, a minute apart, it had samples of 21,188 and 27,913. So the echo is in the uplink the AoC sends, and it can be seen + without anyone listening. + +9. The same call without the tuning (`tegu_aec = no`, a fresh boot; the + AoC logs "PostProcessing Config: BYPASS" and "Mixer 0 active + (bypass)"), 110 s. While the clock's tone played, at 700 to 2,100 on + the downlink, the uplink's median per 5 s window was 113 to 144 from + 5 s to 35 s and 4 to 66 later, and no sample in the call was above 460. (The tone lands on nearly the + same phase at every sample, so these follow the tone's phase as much + as its level.) +10. With the tuning again, twice (110 s each; in the second the uplink + tuning had one field changed, 2.8.2 set to 0, and the owner's file was + put back afterwards): in the windows with the tone the uplink's median + was 90 and 65, as low as without the tuning, but one sample in ten was + over 4,000 and the largest were 8,868 and 6,680. The changed field made no difference. So with stock's + tuning the uplink is quiet most of the time and breaks through in + bursts far louder than anything the unprocessed uplink had. +11. The same call with both speakers' volume at 0 for 14 s in the middle + (180 before and after), the tone at about 2,000 on the downlink + throughout: all 32 uplink samples of the two whole 5 s windows inside + those seconds were below 70; in the windows before and after the largest were 821 to + 7,309. So the echo is sound from the speakers reaching the + microphones, and nothing passes inside the AoC. +12. The processor's reference, recorded during a call from the AoC's + `audio_android_aec` capture (`hw:0,27`): it delivers 32-bit stereo + whatever format is asked for, and both channels carried the clock. + The recording had gaps and ended in a read error ("Bad address"), and + the call ended 48 s in, so it says the reference is there and no more: + not its level, and not whether it is in step with the microphones. + +What this says: the APMg3 processor runs and has stock's tuning, the echo +is acoustic, and with the tuning it comes through in bursts rather than +being cancelled; the processor has a reference signal. What it doesn't say +is why the cancellation doesn't hold, or what the 5 s of silence are. One +sample every 0.32 s is a rough level and no more. + +## Rules for the tests that are left + +- **An AoC capture that hasn't run during a call before is a first probe.** + One per boot, attended, with the kernel log streamed to the laptop before + it starts, as the 7 Pro's AoC notes ask: there a wrong capture route + during audio took the AoC down until a reboot. Two tries here broke that: + result 12's capture (`hw:0,27`) ended in "Bad address" 48 s into the call + and the call ended then, where the other clock calls ran 80 to 110 s, so + the capture may have ended it; and an in-call capture (`audio_incall_cap_0`) + ended in EIO with no kernel log kept, and whether the AoC was online + after it wasn't recorded. After result 12 the AoC was still online: the + next call, four minutes later on the same boot, had call audio and uplink + samples (result 11). Result 8 had nobody near the palmtop, and nobody was + asked to be there for results 9 to 12 either, against this plan's own + attended gate. +- **Only uplink tuning files are edited, never a downlink one.** The downlink + file is the speaker's path, and a changed one could drive the speakers + outside what stock's tuning allows. An edited uplink file is in place for + one call, the owner's file goes back right after and is checked against + `vendor-firmware.sha256`, and an edited file is never committed. + +## Next for the echo + +A call to a talking clock with `call-audio-watch.py` running is now the +test, with no listener: the uplink's samples during the announcements +against those during the ticks. To do, each on a fresh boot since the +configuration is read at sitd's start: + +- A better level than one sample in 0.32 s: stock's in-call capture + (`Incall Capture Stream0` at `UL_DL`, the `audio_incall_cap_0` PCM). + One try during a call ended in a read error (EIO) with no audio; the + kernel log wasn't kept, so the next try reads it. +- A clean recording of the reference (result 12), with a period small + enough for the AoC's ring, beside the speakers' own output: its level + and its delay against the microphones. +- The uplink tuning with one part off at a time (the canceller's switch, field + 1.6.1; the blocks at 1.4 and 1.5), for which part makes the bursts. +- The speaker tuning's own layout, read from the owner's file with + `protoc --decode_raw`: it declares a two-channel reference and three + capture channels (the handset one a one-channel reference). Whether the + reference the processor gets on our route is what that layout expects, + both speakers and in step with the microphones, isn't checked. + + +From our kernel's AoC driver and the owner's stock configuration +(`vendor/etc/audio_platform_configuration.xml`, `mixer_paths_aidl.xml`): + +- `FT AEC Reference Source` doesn't reach the AoC when set; it goes out + with the modem microphone's start (`CMD_AUDIO_INPUT_MODEM_INPUT_START_2`, + `ref_input_source`). With the built-in microphone, `SPEAKER` and `Default` + both send index 0, so this control changes nothing for our calls. +- `AoC Modem Mixer ASP Mode` (block 19, component 15, the block the tuning + goes to) read `ASP_OFF` during our calls. Stock's mixer file doesn't set + it, so stock's HAL sets it in code if at all. `ASP_ON` during the call is + the next thing to try, with the tuning and the DSP mode. +- Stock's telephony tuner applies the wideband tuning to narrowband voice + (`APPLY_WB_TUNING_FOR_NB_VOICE`), so which section of the tuning applies + may depend on the call's codec band. +- Stock also runs a telephony speaker compensation tuner with + `compens_spk_l.conf` and `compens_spk_r.conf` (text tables of value + pairs in the factory image's `/vendor/etc`), which we don't apply. diff --git a/device/tegu/bringup/call-audio-watch.py b/device/tegu/bringup/call-audio-watch.py new file mode 100755 index 000000000..6e05969a4 --- /dev/null +++ b/device/tegu/bringup/call-audio-watch.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +# On the palmtop: watch the AoC through a phone call, read-only. For D seconds it reads +# the call's mixer controls every half second and prints one line whenever a value +# changes, and it reads the AoC's own log (/dev/acd-logging) and prints its text as it +# arrives, every line stamped with the seconds since the start and the wall clock. Start +# it, place the call, hang up, and read where the lines change about 2 s into the call. +# Nothing is written to the mixer or the AoC. CTL=0 leaves the controls unread (the log +# alone), in case reading them changes what the call does. +# python3 call-audio-watch.py [D s, default 120] > /run/call-audio-watch.txt +import os, re, subprocess, sys, threading, time + +D = float(sys.argv[1]) if len(sys.argv) > 1 else 120 +CARD = "0" +CTLS = ["Audio DSP Mode", "AoC Modem Mixer ASP Mode", "AoC Speaker Mixer ASP Mode", + "AoC Modem Downlink ASRC Mode", "AoC Modem Sink Channel Bitmap", "Incall Mic Gain (dB)", + "Incall Mic Mute", "Incall Sink Mute", "MIC HW Gain (cB)", "BUILTIN MIC Process Mode", + "FT AEC Reference Source", "Audio Capture Mic Source", "BUILDIN MIC POWER STATE", + "Incall Capture Stream0", "Incall Playback Stream0"] +t0 = time.monotonic() +lock = threading.Lock() + + +def say(kind, text): + with lock: + print("%7.2f %s %s %s" % (time.monotonic() - t0, time.strftime("%H:%M:%S"), kind, text), flush=True) + + +def get(name): + r = subprocess.run(["amixer", "-c", CARD, "cget", "name=" + name], capture_output=True, text=True) + m = re.search(r": values=(.*)", r.stdout) + return m.group(1).strip() if m else "?" + + +def aoclog(): + try: + fd = os.open("/dev/acd-logging", os.O_RDONLY) + except OSError as e: + say("log", "can't open /dev/acd-logging: %s" % e); return + while time.monotonic() - t0 < D: + try: + b = os.read(fd, 65536) + except OSError as e: + say("log", "read failed: %s" % e); return + for s in re.findall(rb"[\x20-\x7e]{6,}", b): + say("log", s.decode()) + + +threading.Thread(target=aoclog, daemon=True).start() +say("start", open("/proc/sys/kernel/osrelease").read().strip()) +last = {} +while time.monotonic() - t0 < D: + if os.environ.get("CTL", "1") != "0": + for c in CTLS: + v = get(c) + if last.get(c) != v: + say("ctl", "%s: %s%s" % (c, v, "" if c not in last else " (was %s)" % last[c])) + last[c] = v + time.sleep(0.5) +say("end", "") diff --git a/device/tegu/bringup/call-levels.py b/device/tegu/bringup/call-levels.py new file mode 100755 index 000000000..31be263ea --- /dev/null +++ b/device/tegu/bringup/call-levels.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: MIT +# Laptop: from a call-audio-watch log, the uplink (Tx) and downlink (Rx) sample values the +# AoC prints about three times a second, per session and in 5 s windows after its start: +# how many, the median and the largest magnitude of each. One sample per 0.32 s is a rough +# level, enough to tell a silent uplink from one that carries speech. +# call-levels.py call-watch-N.txt +import re, sys, statistics +s = lambda h: (int(h, 16) ^ 0x8000) - 0x8000 +rows = {} +for l in open(sys.argv[1]): + m = re.match(r"\s*([\d.]+) \S+ log #### PCIe Session (\d+) .*Tx:\s*\d+ \(0x([0-9a-f]+)\), Rx:\s*\d+ \(0x([0-9a-f]+)\)", l) + if m: + rows.setdefault(m.group(2), []).append((float(m.group(1)), abs(s(m.group(3))), abs(s(m.group(4))))) +for sid, r in rows.items(): + t0 = r[0][0] + print("session %s: %d samples over %.0f s from %.1f" % (sid, len(r), r[-1][0] - t0, t0)) + w = 0 + while True: + x = [(a, b) for t, a, b in r if w <= t - t0 < w + 5] + if not x: + if w > r[-1][0] - t0: break + w += 5; continue + tx = [a for a, b in x]; rx = [b for a, b in x] + print(" %3d-%3d s: n %2d uplink median %5d max %6d over100 %2d | downlink median %5d max %6d over100 %2d" % ( + w, w + 5, len(x), statistics.median(tx), max(tx), sum(a > 100 for a in tx), statistics.median(rx), max(rx), sum(b > 100 for b in rx))) + w += 5 diff --git a/docs/TELEPHONY-APPS-PLAN.md b/docs/TELEPHONY-APPS-PLAN.md index 1e4011606..e71d65364 100644 --- a/docs/TELEPHONY-APPS-PLAN.md +++ b/docs/TELEPHONY-APPS-PLAN.md @@ -207,6 +207,9 @@ On gs201 the voice path never passes through the application processor. **The Ao Beyond the mixer paths, the working stack on this phone model sets `Voice Call Audio Enable`, `Voice Call Mic Source` 1, `Incall Sink Mute` 0, `AoC Modem Downlink ASRC Mode` 1 and a mic gain of 130 cB plus 6 dB. It opens **PCM 4 playback (EP5)** and **PCM 11 capture (EP4)** (MODEM-PLAN 6.1). Our card numbers its PCMs as stock does (AGENTS.md), so those numbers hold. `omamobile-sitd`'s `voice_audio.py` does exactly this and restores every control after the call. Our sound card already describes the back ends these paths need: `BT_RX`/`BT_TX`, `USB_RX`/`USB_TX` and `INCALL_RX`/`INCALL_TX` (`device/cheetah/overlays/aoc-sound-test.dtso`). +The 9a's opt-in AEC upload and conservative earpiece route have a separate +[offline implementation and qualification note](../device/tegu/CALL-AUDIO-TUNING.md). + ### 2.2 The work, per audio route | Route | What's needed | Depends on | Effort | diff --git a/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf b/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf index 8379e6d9b..31d261b41 100644 --- a/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf +++ b/packages/omamobile-modem-sit/etc/omamobile/modem-sit.conf @@ -158,3 +158,16 @@ earpiece = yes # louder and clean in calls on the 7 Pro) or 0 (stock). The 9a always uses # stock's 130 cB, with no boost. mic_boost_db = 6 + +# Experimental 9a call tuning, from this phone's own factory image. +# Keep off: in the trials (2026-11-02 to 11-12) the AoC took the tuning and +# the echo was not cancelled; with it the echo comes through in louder +# bursts (device/tegu/CALL-AUDIO-TUNING.md). card = none still disables all +# audio. +# AEC uploads owner files from /usr/lib/firmware/updates/omamobile/tegu/aoc. +tegu_aec = no +# speaker: both amplifiers at -20 dB; earpiece: top at -30 dB, bottom at +# the minimum digital gain. Earpiece requires tegu_aec = yes and the full +# amplifier profile 0; the stock voice profile has no standalone native +# equivalent. Changing routes mid-call is not implemented. +tegu_route = speaker diff --git a/packages/omamobile-modem-sit/lib/aoc_tuning.py b/packages/omamobile-modem-sit/lib/aoc_tuning.py new file mode 100644 index 000000000..9bf659e15 --- /dev/null +++ b/packages/omamobile-modem-sit/lib/aoc_tuning.py @@ -0,0 +1,124 @@ +# SPDX-License-Identifier: MIT +"""Bounded AoC telephony tuning upload, using the owner's tegu protobufs. + +Only the APMg3 telephony component on audio_output_tuning is addressed. +The GPL AoC interface defines CMD_HDR / SET_PARAMETER_BULK; the route +adapter adds an envelope with tag 2 around each unchanged protobuf. No +vendor executable, shared memory mapping, or arbitrary command API. +""" +import os +from pathlib import Path +import select +import struct + +from paths import RUN + +ROOT = Path('/usr/lib/firmware/updates/omamobile/tegu/aoc') +BULK_SIZE = 16384 +HEADER = struct.Struct(', ..."). PROTECTION = Path(os.environ.get('OMAMOBILE_SPK_STATUS', '/run/omamobile/speaker-protection')) STAGES = ('media', 'c8') @@ -50,6 +52,7 @@ # tas2x20_dvc_table in include/sound/tas2x20-tlv.h: index 220 is unity, # index 180 is -20 dB. Use the index, not the control's misleading dB TLV. TEGU_GAIN = '180' +TEGU_EARPIECE_GAIN = '160' # -30 dB; stock index 55 maps to unity (220) TEGU_GAINS = ('Top Speaker Digital Volume', 'Bottom Speaker Digital Volume') TEGU_ROUTE = ( ('Incall Sink Mute', '1'), @@ -106,7 +109,7 @@ class VoiceAudio: # without earpiece mode (no service installed, or [audio] earpiece = no) earpiece_cmd = None - def __init__(self, *, mic_boost_db=6, card=None, earpiece=True): + def __init__(self, *, mic_boost_db=6, card=None, earpiece=True, tegu_route='speaker', tegu_aec=False): if mic_boost_db not in (0, 6): raise ValueError('unsupported voice microphone boost') self.mic_boost_db = mic_boost_db @@ -118,6 +121,13 @@ def __init__(self, *, mic_boost_db=6, card=None, earpiece=True): # The protection service's earpiece mode is the 7 Pro's CS35L41 # route; the 9a's TAS2572 route sets its own gains. self.earpiece_cmd = None + # Only the 9a's route reads this key: a typo in it must not cost + # another phone its call audio. + if self.tegu and tegu_route not in ('speaker', 'earpiece'): + raise ValueError('unsupported tegu call route') + self.tegu_route = tegu_route + self.tegu_aec = tegu_aec + self.tuning = None self.saved = [] self.pcms = [] self.alsa = C.CDLL('libasound.so.2') @@ -267,11 +277,37 @@ def tegu_enter(self): return self # Read everything before changing anything. Mute is first so its # original value is restored last, after closing the voice PCMs. - names = [name for name, _ in TEGU_ROUTE] + list(TEGU_GAINS) - saved = [(name, self.mixer_value(name)) for name in names] + route = list(TEGU_ROUTE) + aec = getattr(self, 'tegu_aec', False) + earpiece = getattr(self, 'tegu_route', 'speaker') == 'earpiece' + if earpiece: + if not aec: + raise AudioRefused('tegu earpiece needs the handset AEC tuning enabled') + # The supplied RCA voice-handset case is only a delta, not a + # standalone initialization. Retain the full profile 0 for + # this conservative top-only route, never guess profile 4. + try: + profiles = [self.mixer_value(amp + ' Speaker Profile Id') for amp in ('Top', 'Bottom')] + except (OSError, RuntimeError, subprocess.TimeoutExpired) as error: + raise AudioRefused('tegu earpiece amplifier profile unavailable') from error + if profiles != ['0', '0']: + raise AudioRefused('tegu earpiece requires the full amplifier profile 0') + if aec: + route.insert(1, ('FT AEC Reference Source', 'SPEAKER')) + # The output DSP stays in Ambient unless told otherwise, and the + # uploaded tuning is for its APMg3 telephony mode. + route.insert(2, ('Audio DSP Mode', 'Telephony_APMG3')) + # The modem mixer's signal processing (block 19, where the tuning + # goes) read ASP_OFF during our calls, with the echo coming back. + route.insert(3, ('AoC Modem Mixer ASP Mode', 'ASP_ON')) + names = [name for name, _ in route] + list(TEGU_GAINS) + try: + saved = [(name, self.mixer_value(name)) for name in names] + except (OSError, RuntimeError, subprocess.TimeoutExpired) as error: + raise AudioRefused('tegu call mixer state unavailable') from error self.saved = saved try: - for name, value in TEGU_ROUTE: + for name, value in route: self.mixer('cset', name, value) if name == 'Incall Sink Mute' and self.mixer_value(name) not in ('1', 'on'): raise AudioRefused('tegu call audio refused: sink mute read-back mismatch') @@ -283,22 +319,40 @@ def tegu_enter(self): self.pcms.append(pcm) self.tegu_params(pcm) self.check(self.alsa.snd_pcm_start(pcm), 'start') + # The tuning goes up once the call's streams have started: the + # AoC starts its voice path at the playback start ("Start voice + # call"), and with the tuning sent before that the echo was gone + # for 2-3 s only (2026-11-03). The sink is still muted here. + if aec: + try: + self.tuning = VoiceTuning(self.tegu_route) + self.tuning.start() + except (OSError, TuningError) as error: + raise AudioRefused('tegu call audio refused: AoC tuning unavailable or unconfirmed') from error # The codec profile resets gain at playback start. These must # happen afterwards, while the modem downlink remains muted. - for name in TEGU_GAINS: - self.mixer('cset', name, TEGU_GAIN) - for name in TEGU_GAINS: + gains = ((TEGU_GAINS[1], '0'), (TEGU_GAINS[0], TEGU_EARPIECE_GAIN)) if earpiece else tuple( + (name, TEGU_GAIN) for name in TEGU_GAINS) + for name, gain in gains: + self.mixer('cset', name, gain) + for name, expected in gains: try: gain = self.mixer_value(name) except (OSError, RuntimeError, subprocess.TimeoutExpired) as error: raise AudioRefused('tegu call audio refused: TAS gain read-back unavailable') from error - if gain != TEGU_GAIN: + if gain != expected: raise AudioRefused('tegu call audio refused: TAS gain read-back mismatch') self.mixer('cset', 'Incall Sink Mute', '0') - print('AoC tegu speakerphone prepared at conservative TAS gain', flush=True) + print('AoC tegu ' + ('earpiece' if earpiece else 'speakerphone') + + ' prepared at conservative TAS gain', flush=True) return self - except BaseException: - self.__exit__(None, None, None) + except BaseException as error: + try: + self.__exit__(None, None, None) + except RuntimeError as cleanup: + if isinstance(error, AudioRefused): + raise AudioRefused('tegu call audio refused; cleanup unconfirmed, sink remains muted') from cleanup + raise raise def __exit__(self, *args): @@ -314,6 +368,13 @@ def __exit__(self, *args): if self.alsa.snd_pcm_close(pcm) < 0: failures.append('PCM close') self.pcms.clear() + if getattr(self, 'tuning', None) is not None: + try: + self.tuning.close() + except (OSError, TuningError): + failures.append('AoC tuning cleanup') + finally: + self.tuning = None for name, value in reversed(self.saved): if getattr(self, 'tegu', False) and name == 'Incall Sink Mute' and failures: continue # uncertain cleanup must leave the call sink muted diff --git a/packages/omamobile-modem-sit/test/test_aoc_tuning.py b/packages/omamobile-modem-sit/test/test_aoc_tuning.py new file mode 100644 index 000000000..95e3cecb4 --- /dev/null +++ b/packages/omamobile-modem-sit/test/test_aoc_tuning.py @@ -0,0 +1,175 @@ +# SPDX-License-Identifier: MIT +"""Synthetic AoC service records only; never open a device or use a blob.""" +import env # noqa: F401 +from pathlib import Path +import struct +import tempfile +import unittest +from unittest.mock import patch + +import aoc_tuning as tuning + + +class Services: + def __init__(self): + self.opened = [] + self.closed = [] + self.written = [] + self.reply = None + self.pending = False + self.timeout = False + self.short_fd = None + self.refuse = False + self.bad_reply = False + + def open(self, path, flags): + self.opened.append((path, flags)) + return 10 if path.endswith('output_tuning') else 11 + + def close(self, fd): + self.closed.append(fd) + + def select(self, read, write, errors, timeout): + if write: + return [], write, [] + return (read if (self.pending if timeout == 0 else not self.timeout) else []), [], [] + + def write(self, fd, data): + self.written.append((fd, data)) + if fd == 10: + reply = bytearray(data) + # As the palmtop's AoC does: the second header byte comes back + # rewritten, the rest of the header as sent. + reply[1] = 0x3e + if self.refuse: + struct.pack_into('/dev/null || { echo "ok - tegu call tuning extraction # SKIP missing $tool"; exit 0; } +done +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT +python3 - "$t/vendor" <<'PY' +from pathlib import Path +import sys +r = Path(sys.argv[1]) +files = { + 'build.prop': b'ro.product.vendor.device=tegu\nro.vendor.build.fingerprint=synthetic-test\n', + 'firmware/fw_bcmdhd.bin': b'4383a3-roml Version=1.2.3 (synthetic)', + 'firmware/bcmdhd.cal_4383_a3': b'NVRAMRev=synthetic\n', + 'firmware/bcmdhd_clm.blob_4383_a3': b'synthetic', + 'firmware/bcmdhd_txcap.blob_4383_a3': b'synthetic', + 'firmware/brcm/BCM.hcd': b'BCM4383A3 synthetic[test]', + 'firmware/aoc.bin': b'synthetic', + 'etc/sensors/registry/fictional.json': b'{}\n', +} +for name in ('cs40l26.wmfw', 'cs40l26-calib.wmfw'): + files['firmware/' + name] = b'WMFW' + bytes(6) + b'\x04FIRMWARE_CS40L26 BUZZ_EFFECTS2_BUZZ_LEVEL' +for name in ('cs40l26.bin', 'cs40l26-svc.bin', 'cs40l26-dvl.bin'): + files['firmware/' + name] = b'WMDR-synthetic' +for route in ('speaker', 'handset'): + for name in (f'uplink_{route}_aec_on_config.pb', f'uplink_{route}_aec_off_config.pb', + f'downlink_{route}_config.pb'): + files['etc/aoc/' + name] = b'synthetic-' + name.encode() +for name, data in files.items(): + path = r / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) +PY +truncate -s 16M "$t/vendor.img" +mkfs.ext4 -q -F -d "$t/vendor" "$t/vendor.img" +tools/extract-vendor-firmware -d tegu -o "$t/out" "$t/vendor.img" > "$t/log" 2>&1 || { cat "$t/log"; exit 1; } +for f in "$t/vendor/etc/aoc/"*.pb; do + cmp "$f" "$t/out/usr/lib/firmware/updates/omamobile/tegu/aoc/$(basename "$f")" +done +(cd "$t/out/usr/lib/firmware/updates" && sha256sum -c "$t/out/vendor-firmware.sha256" >/dev/null) +echo 'ok - all six tegu call tuning files copied unchanged and checksummed' +rm "$t/vendor/etc/aoc/uplink_handset_aec_off_config.pb" +mkfs.ext4 -q -F -d "$t/vendor" "$t/vendor.img" +tools/extract-vendor-firmware -d tegu -o "$t/missing" "$t/vendor.img" > "$t/log" 2>&1 || { cat "$t/log"; exit 1; } +grep -qF 'warning: /vendor/etc/aoc/uplink_handset_aec_off_config.pb is missing' "$t/log" +[[ ! -e $t/missing/usr/lib/firmware/updates/omamobile/tegu/aoc ]] || { echo 'not ok - part of the call tuning set was kept' >&2; exit 1; } +[[ -s $t/missing/usr/lib/firmware/updates/aoc-tegu.bin ]] || { echo 'not ok - the other files were not extracted' >&2; exit 1; } +(cd "$t/missing/usr/lib/firmware/updates" && sha256sum -c "$t/missing/vendor-firmware.sha256" >/dev/null) +echo 'ok - a missing call tuning file is a warning: the set is left out whole and the rest is extracted' diff --git a/tools/extract-vendor-firmware b/tools/extract-vendor-firmware index 425f2e66d..afaa7bfd0 100755 --- a/tools/extract-vendor-firmware +++ b/tools/extract-vendor-firmware @@ -55,6 +55,8 @@ # vendor/firmware/bcmdhd_clm.blob_4383_a3 -> brcm/brcmfmac4383-pcie..clm_blob (regulatory) # vendor/firmware/bcmdhd_txcap.blob_4383_a3 -> brcm/brcmfmac4383-pcie..txcap_blob (TX capabilities) # vendor/firmware/brcm/BCM.hcd -> brcm/BCM..hcd (btbcm) +# vendor/etc/aoc/{uplink_{speaker,handset}_aec_{on,off},downlink_{speaker,handset}}_config.pb +# -> omamobile/tegu/aoc/ (call tuning, unchanged owner files) # vendor/firmware/aoc.bin -> aoc-tegu.bin (the AoC DSP, under the 9a's own name) # vendor/etc/sensors/registry/ -> /usr/share/omamobile/sensors/registry/ # vendor/firmware/cs40l26*.{wmfw,bin} -> the same five files as cheetah's @@ -245,6 +247,12 @@ case $DEVICE_OPT in "firmware/bcmdhd_txcap.blob_4383_a3|brcm/$FW_BASE.$BOARD.txcap_blob" "firmware/brcm/BCM.hcd|brcm/BCM.$BOARD.hcd" "firmware/aoc.bin|aoc-tegu.bin" + "?etc/aoc/uplink_speaker_aec_on_config.pb|omamobile/tegu/aoc/uplink_speaker_aec_on_config.pb" + "?etc/aoc/uplink_speaker_aec_off_config.pb|omamobile/tegu/aoc/uplink_speaker_aec_off_config.pb" + "?etc/aoc/downlink_speaker_config.pb|omamobile/tegu/aoc/downlink_speaker_config.pb" + "?etc/aoc/uplink_handset_aec_on_config.pb|omamobile/tegu/aoc/uplink_handset_aec_on_config.pb" + "?etc/aoc/uplink_handset_aec_off_config.pb|omamobile/tegu/aoc/uplink_handset_aec_off_config.pb" + "?etc/aoc/downlink_handset_config.pb|omamobile/tegu/aoc/downlink_handset_config.pb" "firmware/cs40l26.wmfw|cs40l26.wmfw" "firmware/cs40l26.bin|cs40l26.bin" "firmware/cs40l26-svc.bin|cs40l26-svc.bin" @@ -442,13 +450,30 @@ mkdir -p "$OUT" stage=$(mktemp -d "$OUT/.extract-vendor-firmware.XXXXXX") new="$stage/new" dest="$new/$DEST_REL" +# An entry that starts with "?" is optional as a set: the 9a's six call tuning +# files are wanted only by an experimental, opt-in feature and have been seen +# on one factory build, so an image without one of them still gives every +# other file; the set is then left out whole, since the loader needs a +# route's three files together. +optional_missing=() optional_dirs=() for entry in "${MAP[@]}"; do from=${entry%%|*} to=${entry#*|} - mkdir -p "$dest/$(dirname "$to")" - get_file "$from" "$dest/$to" || die "/vendor/$from is missing from this image or could not be copied" + if [[ $from == \?* ]]; then + from=${from#\?} + optional_dirs+=("$(dirname "$to")") + mkdir -p "$dest/$(dirname "$to")" + if ! get_file "$from" "$dest/$to"; then optional_missing+=("/vendor/$from"); continue; fi + else + mkdir -p "$dest/$(dirname "$to")" + get_file "$from" "$dest/$to" || die "/vendor/$from is missing from this image or could not be copied" + fi chmod 0644 "$dest/$to" printf ' /vendor/%-28s -> /%s/%s\n' "$from" "$DEST_REL" "$to" done +if (( ${#optional_missing[@]} )); then + for d in "${optional_dirs[@]}"; do rm -rf -- "${dest:?}/$d"; done + printf 'warning: %s is missing from this image or could not be copied: the call tuning files are left out (calls work without them; the opt-in tegu_aec setting needs all six)\n' "${optional_missing[*]}" >&2 +fi # The AoC's sensor registry (which sensors this board has, their buses and # settings), read by omamobile-sensor-proxy; without it only virtual sensors. @@ -534,7 +559,11 @@ bt="" [[ -z $BT_CHIP ]] || bt=$(grep -ao "$BT_CHIP [^[]*" "$brcm/BCM.$BOARD.hcd" | head -1) (cd "$dest" && find . -type f -printf '%P\n' | LC_ALL=C sort | xargs sha256sum -- > "$new/vendor-firmware.sha256") # The checksum list names exactly the mapped files -for entry in "${MAP[@]}"; do printf '%s\n' "${entry#*|}"; done | LC_ALL=C sort > "$stage/expected" +for entry in "${MAP[@]}"; do + # an optional set that was left out is not expected + [[ $entry == \?* ]] && (( ${#optional_missing[@]} )) && continue + printf '%s\n' "${entry#*|}" +done | LC_ALL=C sort > "$stage/expected" cut -c67- "$new/vendor-firmware.sha256" | cmp -s - "$stage/expected" || die "the staged firmware is not exactly the mapped files"