usbc: USB-PD R3.2 policy SM + TCPC abstraction#2211
Open
b1nc0d3x wants to merge 1 commit into
Open
Conversation
Add a new in-tree framework for USB Power Delivery state-machine
logic that is independent of any specific Type-C port controller
chip. The framework lives at sys/dev/iicbus/usb/usbc/, builds as a
kernel module (usbc.ko), and exposes three layers:
1. usbc_pd.h -- base types, enums, and timing constants from USB
PD R3.2: SOP* packet types, control/data message numbers, header
bit layout, PDO/RDO field encoders, protocol timers
(tSenderResponse, tPSTransition, ...), VDM SVID / object position
constants for DisplayPort altmode.
2. usbc_pd_msg.{h,c} -- hardware-independent build/parse for PD
message headers and data objects. No I/O, no locking; callable
from interrupt context:
usbc_pd_build_header() -- pack header fields to u16
usbc_pd_parse_header() -- inverse, with bounds checking
usbc_pd_build_source_cap_pdo_fixed()
usbc_pd_build_request_rdo()
usbc_pd_validate_pdo()
3. usbc_pd_policy.{h,c} -- the protocol/policy engine. Implements
the source-port policy state machine from PD chapter 8 (Power
Negotiation), the soft-reset and hard-reset paths, the VDM
exchange for Discover Identity / Discover SVIDs / Enter Mode /
DP_Status (the DP altmode prerequisites needed for cdn-dp on
RockPro64), and the timer-driven retry/abort logic.
Caller-facing API:
usbc_pd_policy_attach(sc, tcpc_ops)
usbc_pd_policy_detach(sc)
usbc_pd_policy_rx(sc, msg)
usbc_pd_policy_role_change(sc, ...)
usbc_pd_policy_enter_dp_altmode(sc, port_pin_assign)
4. usbc_tcpc.h -- the abstraction the chip driver implements. A
struct of function pointers (transmit a PD msg, set CC state,
set vbus, set role, etc.) that the policy SM invokes. This is
what makes the framework hardware-independent: the FUSB302B
driver in the companion review supplies one implementation; a
future TUSB320 / TPS6598x / MT6360 driver supplies another
without touching policy code.
Why land this separately:
- It is dependency-free against the rest of the tree -- no other
in-tree driver consumes it yet.
- Reviewing the framework in isolation from any specific chip
driver makes the abstraction boundary clearer.
- The companion fusb302 review is materially smaller once it can
refer to "the policy SM" and "the TCPC interface" instead of
containing both itself.
Not in this review:
- No chip driver. The fusb302(4) driver is a separate review.
- No userspace policy hook -- SM is autonomous; the intent is
that a future tcpmctl(8) or libusbc consumer can drive policy
choices from userspace, but that is out of scope here.
- No PD R3.2 fast-role-swap or programmable-power-supply support.
Source PDOs are limited to fixed-supply 5 V; extended-message
handling is reserved for a follow-up.
Tested on Pine64 RockPro64 v2.1 with the companion fusb302 driver
and an XYM W156F1 USB-C portable monitor: the policy SM drives a
full source contract (PD_REQUEST -> ACCEPT -> PS_RDY) and a
DisplayPort altmode entry (Discover Identity -> Discover SVIDs ->
Enter Mode -> DP_Status) within ~1 s of plug insertion. Subsequent
unplug/replug cycles re-run the entire flow without leaking state.
Negative paths verified: forced PD soft-reset and hard-reset both
return the SM to USBC_PD_SRC_STARTUP within tSenderResponseTimer
+ 50 ms.
Signed-off-by: Kyle Crenshaw <B1nc0d3x@gmail.com>
|
There's the slight problem of hps having passed away many years ago. I had the pleasure of meeting him in Warsaw over 2012. |
Author
|
Thank you for the correction - I wasn’t aware of Hans Petter’s passing, and I’m genuinely sorry. I had no intention of being disrespectful and from here on I’ll cross-check before requesting specific reviewers…Thank You,Kyle CrenshawOn May 19, 2026, at 6:15 AM, Bruce M Simpson ***@***.***> wrote:bms left a comment (freebsd/freebsd-src#2211)
There's the slight problem of hps having passed away many years ago. I had the pleasure of meeting him in Warsaw over 2012.
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Add a new in-tree framework for USB Power Delivery state-machine logic that is independent of any specific Type-C port controller chip. The framework lives at
sys/dev/iicbus/usb/usbc/, builds as a kernel module (usbc.ko), and exposes four layers:usbc_pd.h— base types, enums, and timing constants from USB PD R3.2: SOP* packet types, control/data message numbers, header bit layout, PDO/RDO field encoders, protocol timers (tSenderResponse,tPSTransition, ...), VDM SVID / object position constants for DisplayPort altmode.usbc_pd_msg.{h,c}— hardware-independent build/parse for PD message headers and data objects. No I/O, no locking; callable from interrupt context:usbc_pd_build_header()— pack header fields to a u16usbc_pd_parse_header()— inverse, with bounds checkingusbc_pd_build_source_cap_pdo_fixed()— 5V fixed-supply PDOusbc_pd_build_request_rdo()— fixed-supply requestusbc_pd_validate_pdo()— sanity-check before TXusbc_pd_policy.{h,c}— the protocol/policy engine. Implements the source-port policy state machine from PD chapter 8 (Power Negotiation), the soft-reset and hard-reset paths, the VDM exchange for Discover Identity / Discover SVIDs / Enter Mode / DP_Status, and the timer-driven retry/abort logic. Caller-facing API:usbc_pd_policy_attach(sc, tcpc_ops)usbc_pd_policy_detach(sc)usbc_pd_policy_rx(sc, msg)— chip-driver IRQ handler entryusbc_pd_policy_role_change(sc, ...)usbc_pd_policy_enter_dp_altmode(sc, port_pin_assign)usbc_tcpc.h— the abstraction the chip driver implements. A struct of function pointers (transmit a PD msg, set CC state, set vbus, set role, etc.) that the policy SM invokes. This is what makes the framework hardware-independent — the FUSB302B driver in the companion (forthcoming) review supplies one implementation, and a future TUSB320 / TPS6598x / MT6360 driver supplies another without changing a line of policy code.Why land this separately
Not in this review
tcpmctl(8)orlibusbcconsumer can drive policy choices from userspace, but that is out of scope here.Test plan
device usbc: builds clean, no external symbol references,kldload/kldunloadwork.usbc_pd_build_header/parse_headerround-trip 1000 random headers via a kgdb script; all field values preserved. PDO encoders verified by hand-decoding the resulting u32 against PD 6.4.1 Table 6-9.PD_REQUEST→ACCEPT→PS_RDY) and a DisplayPort altmode entry (Discover Identity→Discover SVIDs→Enter Mode→DP_Status) within ~1 s of plug insertion. Subsequent unplug / replug cycles re-run the entire flow without leaking state.USBC_PD_SRC_STARTUPwithintSenderResponseTimer + 50 ms.Suggested reviewers
hps(USB stack),kib,manu,mhorne— based ongit logofsys/dev/iicbus/usb/and related arm64 work. Not @-mentioned to avoid spam; happy to ping on request.Diff scope
sys/dev/iicbus/usb/usbc/usbc_pd.hsys/dev/iicbus/usb/usbc/usbc_pd_msg.csys/dev/iicbus/usb/usbc/usbc_pd_msg.hsys/dev/iicbus/usb/usbc/usbc_pd_policy.csys/dev/iicbus/usb/usbc/usbc_pd_policy.hsys/dev/iicbus/usb/usbc/usbc_tcpc.hsys/modules/usbc/MakefileAll files net-new; no existing FreeBSD code modified. License is BSD-2-Clause throughout; SPDX-License-Identifier headers present on every file.
Notes
Third in a series landing the RockPro64 USB-C DisplayPort + audio path. Predecessors:
rk_gpio: implement PIC masking methods and mask unhandled IRQsdrm_fb_helper: re-probe on late EDID, don't clamp to fallback fb sizeSuccessor patches (
fusb302chip driver,rk_typec_phyDP altmode,rk_cdn_dpframer, DP audio + hdmi codec stub) will follow as their own PRs, paced to avoid swamping reviewer attention.Author: Kyle Crenshaw <B1nc0d3x@gmail.com>