Repository navigation
Fix BCM4350 Bluetooth coexistence on MacBookPro14,1 #13298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Clowdyffs
wants to merge
3
commits into
omacom:quattro
Choose a base branch
from
Clowdyffs:fix/macbookpro14-1-bluetooth-coexistence
base: quattro
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
etc/NetworkManager/dispatcher.d/90-omarchy-bcm4350-coexistence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #!/bin/bash | ||
|
|
||
| # The hybrid policy starves A2DP on this Mac's BCM4350 with 2.4 GHz Wi-Fi. | ||
| # Measurements and scope: https://github.com/omacom/omarchy/issues/13297 | ||
| # Keep other machines out of the vendor-command path entirely. | ||
| case "${2:-}" in | ||
| up|reapply|dhcp4-change|dhcp6-change|down) ;; | ||
| *) exit 0 ;; | ||
| esac | ||
| [[ -r /sys/class/dmi/id/product_name ]] || exit 0 | ||
| [[ $(</sys/class/dmi/id/product_name) == "MacBookPro14,1" ]] || exit 0 | ||
|
|
||
| /usr/bin/python3 -I - "$@" <<'PY' | ||
| from pathlib import Path | ||
| import re | ||
| import struct | ||
| import subprocess | ||
| import sys | ||
|
|
||
| STATE = Path('/run/omarchy-bcm4350-coexistence') | ||
|
|
||
|
|
||
| def run(*args, data=None): | ||
| return subprocess.run(args, input=data, stdout=subprocess.PIPE, | ||
| stderr=subprocess.PIPE, check=True, timeout=2).stdout | ||
|
|
||
|
|
||
| def iovar(iface, name, value=None): | ||
| # brcmfmac's BRCMF_VNDR_CMDS_DCMD: native 20-byte header, LE firmware values. | ||
| payload = name.encode('ascii') + b'\0' | ||
| if value is not None: | ||
| payload += struct.pack('<I', value) | ||
| request = struct.pack('=IiIII', 262 if value is None else 263, | ||
| 256 if value is None else len(payload), | ||
| 20, int(value is not None), 0) + payload | ||
| response = run('/usr/bin/iw', 'dev', iface, 'vendor', 'recvbin', | ||
| '0x001018', '0x1', '-', data=request) | ||
| if value is not None: | ||
| return | ||
| # iw returns aligned netlink attributes: DATA=2, LEN=1. | ||
| result = bytearray() | ||
| offset = 0 | ||
| while offset < len(response): | ||
| length, kind = struct.unpack_from('=HH', response, offset) | ||
| if length < 4 or offset + length > len(response): | ||
| raise ValueError('invalid vendor response') | ||
| if kind & 0x3fff == 2: | ||
| result.extend(response[offset + 4:offset + length]) | ||
| offset += (length + 3) & ~3 | ||
| if not result: | ||
| raise ValueError('empty vendor response') | ||
| return bytes(result) | ||
|
|
||
|
|
||
| def mode(iface): | ||
| return struct.unpack_from('<I', iovar(iface, 'btc_mode'))[0] | ||
|
|
||
|
|
||
| def apply(iface): | ||
| interface = Path('/sys/class/net') / iface | ||
| state = STATE / iface | ||
| try: | ||
| ifindex = (interface / 'ifindex').read_text().strip() | ||
| except FileNotFoundError: | ||
| state.unlink(missing_ok=True) | ||
| return | ||
| # A re-probe reuses the name but creates a new interface. An old marker | ||
| # (including the previous empty format) does not own that interface's policy. | ||
| if state.exists() and state.read_text().strip() != ifindex: | ||
| state.unlink() | ||
| device = interface / 'device' | ||
| if not (device / 'vendor').is_file(): | ||
| return | ||
| if ((device / 'vendor').read_text().strip() != '0x14e4' | ||
| or (device / 'device').read_text().strip() != '0x43a3' | ||
| or (device / 'revision').read_text().strip() != '0x05' | ||
| or (device / 'driver').resolve().name != 'brcmfmac'): | ||
| return | ||
| version = iovar(iface, 'ver').split(b'\0', 1)[0] | ||
| if not re.search(rb'\bversion 7\.35\.180\.133\b.*\bFWID:? 01-c45b39d6\b', version): | ||
| return | ||
|
|
||
| # Dispatcher events can be stale by the time they run; inspect the live link. | ||
| link = run('/usr/bin/iw', 'dev', iface, 'link').decode(errors='replace') | ||
| frequency = re.search(r'^\s*freq:\s*(\d+)', link, re.MULTILINE) | ||
| on_2ghz = frequency is not None and 2400 <= int(frequency[1]) <= 2500 | ||
| current = mode(iface) | ||
|
|
||
| if on_2ghz and current == 5: | ||
| # Record ownership before writing so a failed readback remains recoverable. | ||
| STATE.mkdir(mode=0o755, exist_ok=True) | ||
| state.write_text(ifindex + '\n') | ||
| iovar(iface, 'btc_mode', 4) | ||
| if mode(iface) != 4: | ||
| # A definite nonparallel readback is not an override we own. Exceptions | ||
| # leave the marker intact because the write's outcome is still unknown. | ||
| state.unlink() | ||
| raise ValueError('parallel policy readback failed') | ||
| print('BCM4350: enabled parallel coexistence on 2.4 GHz', file=sys.stderr) | ||
| elif not on_2ghz and state.exists(): | ||
| # Restore only our recorded override; leave a different current policy alone. | ||
| if current == 4: | ||
| iovar(iface, 'btc_mode', 5) | ||
| if mode(iface) != 5: | ||
| raise ValueError('hybrid policy readback failed') | ||
| state.unlink() | ||
| elif current != 4: | ||
| state.unlink(missing_ok=True) | ||
|
|
||
|
|
||
| def main(argv): | ||
| if (len(argv) != 3 | ||
| or not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9_.:-]{0,14}', argv[1])): | ||
| return | ||
| try: | ||
| apply(argv[1]) | ||
| except (OSError, ValueError, struct.error, subprocess.SubprocessError) as error: | ||
| # Device removal or unavailable firmware must not fail network activation. | ||
| print(f'BCM4350: coexistence quirk skipped ({type(error).__name__})', file=sys.stderr) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main(sys.argv) | ||
| PY | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ifindexis checked before the firmware queries, but everyiwcommand uses the interface name. If driver recovery replaceswlan0during those queries, this branch can set the new interface to mode 4 while recording the old interface's index. The next event deletes that marker, leaving parallel mode active without permission to restore it on 5 GHz or disconnect.Use the saved interface index for firmware commands, or stop when the interface changes. Add a test that replaces the interface during a query.