Skip to content

usb: typec: tipd: reset a controller through debugfs, and set the port up again - #4057

Open
DjDeveloperr wants to merge 5 commits into
mainfrom
feat/tipd-debugfs-controller-reset
Open

DjDeveloperr wants to merge 5 commits into
mainfrom
feat/tipd-debugfs-controller-reset

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What: tuxvdmtool reset-controller can reset a stuck port controller on M3 and later Macs. The debugfs command file now takes Gaid, and the driver sets the port up again afterwards. Also fixes a port that stayed dead after the driver's own reset of a controller that had lost its contract.

Tested: J516s (SN201202x) at this head with #4056: one Gaid with a partner on the port, controller set up again, partner and peer back, other ports untouched (PASS). KUnit on UML passes with five new cases, the tipd directory builds at W=1 on arm64 without a new warning, and the host tests that build code from this driver pass.

Risk: two-way · all Apple silicon Macs. A reset happens only when root writes Gaid. The fix changes what follows the driver's own reset, on ports with a debug link after a sleep (M5 Pro).

Not yet: One CD321x Mac (M2 Pro J414s) with Gaid on a port with nothing plugged in. What the controllers report in the first second after the reset is taken from the driver's two existing resets, not measured here.

Details

What a reset loses, and how the driver restores it. After Gaid the controller has its default interrupt mask and power state, is locked, and has dropped its partner and its liquid mitigation. The driver already handles a reset that it finds out about from a stray interrupt (cd321x_check_setup()): back to S0, the mask again, the liquid detector's state invalidated, and the cable state read afresh. The new cd321x_cold_reset() uses that path:

  1. It holds the port lock over the whole reset (about 1 s to 6 s), so no poll or interrupt reads a restarting controller, and keeps runtime PM from putting it to sleep meanwhile.
  2. It writes Gaid. An error up to and including that write is returned, and nothing is changed. Only after the write may reads fail, as a controller that restarts need not answer. Then it waits until the mode is APP and no command is left in CMD1 (1 s, then up to 2 s).
  3. It marks the setup pending with a whole budget of attempts, asks for a forced reconnect (an old USB4, DP or USB3 session is taken down before the new one), and queues the port's work. macOS's no-partner state is applied again as at boot, on ports that use it.

A controller that is not up in time gives -ETIMEDOUT, and is still set up once it answers.

Refused with -EBUSY: debug mode (DBMa, including one set up before Linux), a command still running, a replug in progress, system sleep. A firmware without the command gives -EINVAL, one that rejects it -EPERM, one that gives up on it -EIO, a bus that times out before the write -EIO (so -ETIMEDOUT always means a reset was sent), a controller that is not a CD321x or SN201202x -EOPNOTSUPP. Nothing is changed in those cases.

The fix. cd321x_rearm_recover_contract() resets the controller too, but only marked its setup pending. The attempts were at zero by then, because the setup right after the wake had used or cleared them. So the next run failed with controller setup recovery exhausted without reading the controller, and no cable update was applied until the next sleep. Both resets now go through cd321x_setup_restart().

Limits. A debug route that was armed before Linux but is not in DBMa is lost with the reset, as with any cold reset. The boot-only steps for a controller found outside S0 at the probe (HChk, then USBw after the host's start) are not repeated. A controller whose command register never becomes idle is not reset, as the command could not be sent.

Evidence
  • KUnit, UML, clang: cd321x-irq (reset and set up again; after a setup that gave up; a controller that is late; every refusal, with the failed write, the rejected command and the timeout before the write) and cd321x-pm (attempts after a reset by the driver). 141/141 at this head, 143/143 together with usb: typec: tipd: debugfs commands and replug on every Mac #4056 before the review fix. With the setup only marked pending, as the old code did, all three reset cases fail (setup_pending stays set, power state and mask are not restored). The SN201202x suite needs SUSPEND, which UML does not have, so it was only compiled.
  • Host tests (ubuntu 24.04 image): sn201202x 1/1, j613-mux 6/6, hardware-defaults 1/1, apple-dcp (it builds functions taken from this driver) passes.
  • arm64 W=1 drivers/usb/typec/tipd/ with defconfig + asahi.config and the tipd KUnit modules: the one warning is the existing i2c.c:21 enum cast.
  • The first head failed apple-dcp (its fixture compiles cd321x-pm.h alone and found the new helper unused); the third commit moves the helper to cd321x-setup.h.
  • tuxvdmtool side: tuxvdmtool: replug for a missing partner, check before every VDM, prove reboots tuxvdmtool#10 (merged) sends the command; reset-controller: document what the kernel refuses, test the whole run tuxvdmtool#11 documents and tests it.

cd321x_rearm_recover_contract() resets a controller that lost its partner's
contract after a debug link cut, and only marks its setup pending. The
attempts for that setup are handed out by the resume and by the first
reset event, and the setup right after the wake has used them up, or left
them at zero, by the time of that reset. The next run then fails with
"controller setup recovery exhausted" without reading the controller, and
no cable update is applied until the next sleep.

Give a reset that the driver made its own attempts, with the code that the
reset event uses.

Signed-off-by: DjDeveloperr <hey@djdev.me>
tuxvdmtool's reset-controller writes "Gaid" to the controller's command
register. An SN201202x is only reached through this driver's debugfs file,
which does not take that command, so a stuck controller could only be reset
by a module written for it, with the driver unbound.

Take it there, for a CD321x too. A reset controller has lost its interrupt
mask, its system power state and its partner, so the driver sends the
command itself, waits until the firmware runs, and then sets the port up as
after a reset it finds out about on its own, and connects what is attached
from a fresh read. macOS's no-partner state (apple,cable-change-ssps) is
applied again as at boot. The reset is refused while the controller is in
DBMa, a command or a replug runs, or the system sleeps.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr requested review from a team as code owners December 3, 2026 04:40
@github-actions github-actions Bot added area:usb Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Merge readiness at ca3ddc7f: ready to merge

Check Detail
CI at head ok 11 checks passed
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head ok comment at ca3ddc7f
Conflicts ok none
PR text ok Risk: two-way · all Apple silicon Macs. A reset happens only when root writes Gaid. The fix changes what follows the driver's own reset, on ports with a debug link after a sleep (M5 Pro).

Areas: area:usb
Merge: gh pr merge 4057 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

The helper that hands them out is only used by the controller's setup, so
move it to cd321x-setup.h and name it after that. The host tests build
cd321x-pm.h on its own and refuse a helper that nothing in it uses.

Signed-off-by: DjDeveloperr <hey@djdev.me>

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CMD1 write failure can report a successful controller reset and reconnect a port that never reset. If the initial reads succeed, the command write returns -EIO, and the next mode read still reports APP, cd321x_send_reset() swallows the original error, cd321x_wait_reset() succeeds on the old firmware state, and the driver reports success and forces a reconnect. Preserve the pre-submit error and only tolerate loss of completion reads once the command write succeeded.

…oller

A bus error in the write of "Gaid" was taken for a controller that restarts
and does not answer. Its mode still reads APP, so the reset was reported as
done and the port reconnected, with nothing reset.

Write the command apart from the wait for its end, return an error up to and
including that write, and tolerate failed reads only after it. The controller
counts as up again once its mode is APP and no command is left in CMD1. A
firmware that rejects the command, or does not have it, is reported with
nothing reset as well. -ETIMEDOUT is returned only for a reset whose end was
not seen, which is what tuxvdmtool takes it for; a bus that times out before
the write gives -EIO.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Fixed in 809b331: an error up to and including the write of the command is returned and nothing is set up again; only reads after it may fail. The wait also needs CMD1 to be empty, so a command that was never taken is not a reset. A rejected command is -EPERM, and -ETIMEDOUT is now only returned for a reset that was sent. KUnit has a case for each.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-submit error is fixed. One completion status still slips through: with CMD1 clear, DATA1 reporting TPS_TASK_TIMEOUT, and mode APP, cd321x_wait_reset() returns success and forces a reconnect. The base command helper already treats that reported task status as a timeout. Preserve the timeout status so the active controller is not reported as successfully reset.

A controller that gives up on "Gaid" clears CMD1 with the task status for a
timeout in DATA1 and keeps running in APP, which counted as a reset. Report
it as -EIO, with nothing set up again. Not as -ETIMEDOUT: that stands for a
reset whose end was not seen, and tuxvdmtool then waits for APP and reports
success.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Fixed in the last commit: a task status of "timeout" with CMD1 clear is now -EIO and nothing is set up again (KUnit case added). It is not -ETIMEDOUT because tuxvdmtool takes that for a reset that was sent, waits for APP and would report success.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Dec 3, 2026

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both reviewed failure paths now return before port setup: failure to write CMD1, and a completed task reporting timeout in DATA1. The latter returns EIO so userspace cannot mistake it for a reset whose completion is unknown. The added KUnit cases exercise these distinctions. A live reset row is still pending.

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Dec 3, 2026
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Hardware check on MacBook Pro 16" M3 Pro (J516s, T6030) at ca3ddc7f4568 (merged with main cff875630c61 and #4056 1c71d486f2f5, using tuxvdmtool PR #11 e47195d2cd72): PASS

  • Port 1-0c (USB-C Left Back, SN201202x, partner product 0x731d3115 / id_header 0xd54005ac connected before reset).
  • Ran tuxvdmtool -c "USB-C Left Back" --expect-product 0x731d3115 reset-controller: exited 0, verified partner product 0x731d3115 before issuing Gaid through /sys/kernel/debug/usb/tipd/1-0c/command, and reported controller back in APP mode.
  • Kernel log confirmed clean reset and re-init:
    • sn201202x 1-0c: controller reset
    • sn201202x 1-0c: restored controller power state and interrupt mask
  • Partner re-enumerated cleanly after reset (0x731d3115, id_header 0xd54005ac, PD contract and USB role restored; peer side also kept partner identity 0x73122087), and sibling ports were unaffected.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-hw-row No hardware row at the PR head (bot) ready-to-merge Meets the merge policy: merge it (bot) labels Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:usb Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants