* [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus
@ 2026-11-17 5:21 Qianhui Xu
2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu
` (3 more replies)
0 siblings, 4 replies; 14+ messages in thread
From: Qianhui Xu @ 2026-11-17 5:21 UTC (permalink / raw)
To: Greg Kroah-Hartman, Thinh Nguyen, Rob Herring,
Krzysztof Kozlowski, Conor Dooley
Cc: linux-usb, devicetree, linux-arm-msm, linux-kernel
Changes since v8 (Rob Herring, 2026-11-10, on the cover):
- dt-bindings: the two connectors are two mux'ed data streams of one
High Speed bus, so port@0 now carries two endpoints and the second
High Speed port (port@2) is gone.
- dt-bindings: the private "port-select-gpios" property is replaced by
a phandle to a mux controller ("mux-controls"), e.g. a "gpio-mux".
- usb: dwc3: the shared data path is driven through the mux controller
API (devm_mux_control_get() / mux_control_select()) instead of a raw
GPIO. mux_control_select() may sleep; it is only called from the
mode worker, never from the role-switch callbacks.
- (board DTS is not part of this posting: port@0 gets two endpoints and the
Lenovo Legion Y700 Gen 4 describes the mux on GPIO 29 in the separate board
DTS series, because that file is not upstream yet.)
Note: the board-level DT change is posted with the "add Lenovo Legion Y700
Gen4" board DTS series ([PATCH v7]) so that every patch here applies to
mainline as-is.
Gate results (tree: 7.4-rc2 line, base f66469d9905b):
- checkpatch --strict: 0 errors / 0 warnings on all three patches.
- make dt_binding_check DT_SCHEMA_FILES=.../snps,dwc3-common.yaml: rc=0.
- CHECK_DTBS=y qcom/sm8750-lenovo-elden.dtb (with the schema): rc=0.
- drivers/usb/dwc3/ builds clean for arm64 with CONFIG_MULTIPLEXER=y.
^ permalink raw reply [flat|nested] 14+ messages in thread* [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port 2026-11-17 5:21 [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu @ 2026-11-17 5:21 ` Qianhui Xu 2026-11-17 5:40 ` sashiko-bot 2026-11-18 15:34 ` Rob Herring 2026-11-17 5:21 ` [PATCH v9 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu ` (2 subsequent siblings) 3 siblings, 2 replies; 14+ messages in thread From: Qianhui Xu @ 2026-11-17 5:21 UTC (permalink / raw) To: Greg Kroah-Hartman, Thinh Nguyen, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-arm-msm, linux-kernel Rework of the v8 binding per Rob Herring's review of 2026-11-10 16:36Z (<20261110163640.GA85824-robh@kernel.org>): "ports are for separate data streams. Endpoints are for mux'ed data streams. This looks like the latter, so port@0 should have 2 endpoints and port@2 dropped." and, for the GPIO property: "Use the gpio mux binding." Two connectors sharing one High Speed D+/D- pair are two mux'ed data streams of one port, so they become endpoint@0 and endpoint@1 of port@0 (the Super Speed lane stays port@1). The GPIO that selects between them is no longer described by a private "port-select-gpios" property but by a "gpio-mux" controller referenced from "mux-controls", and the endpoint of each connector carries the mux state that reaches it. *** AI-assistance disclosure *** This patch was prepared with the help of an LLM coding assistant. The author reviewed every line and ran the gates quoted in the series notes. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- DRAFT: driver (3/3) still uses a GPIO and must be ported to mux_control_* in the same series; DTS (2/3 equivalent) must be rewritten to the new shape. See README.md in this directory. --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml @@ -13,6 +13,84 @@ description: Defines the properties of the DWC3 core as being embedded in either an vendor-specific implementation or as a standalone component. +$defs: + hs-endpoint: + $ref: /schemas/graph.yaml#/$defs/endpoint-base + unevaluatedProperties: false + + properties: + usb-role-switch: + type: boolean + description: + This connector endpoint has its own USB role switch. The switch is + registered against this endpoint and looked up with + usb_role_switch_at_endpoint(). + + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + State of the High Speed data bus multiplexer which connects this + connector to the controller. The two connectors of one controller + must use different states; the driver rejects a device tree where + two connectors claim the same one. + + dependentRequired: + usb-role-switch: [mux-state] + mux-state: [usb-role-switch] + + hs-port: + $ref: /schemas/graph.yaml#/$defs/port-base + unevaluatedProperties: false + + # The two connectors are two mux'ed data streams of the same High Speed + # bus, so they are two endpoints of one port, not two ports: "ports" + # describes the controller's own data streams (HS in port@0, SS in + # port@1), and an endpoint describes one stream multiplexed onto a port. + properties: + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + endpoint@0: + $ref: '#/$defs/hs-endpoint' + required: [reg] + + endpoint@1: + $ref: '#/$defs/hs-endpoint' + required: [reg] + + required: ['#address-cells', '#size-cells', endpoint@0, endpoint@1] + + # The two connectors must claim opposite mux states; requiring both + # endpoints in every branch is what makes the two cases mutually + # exclusive for oneOf. + oneOf: + - properties: + endpoint@0: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 0 + endpoint@1: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 1 + - properties: + endpoint@0: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 1 + endpoint@1: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 0 + allOf: - $ref: usb-drd.yaml# - if: @@ -26,8 +104,47 @@ allOf: $ref: usb.yaml# else: $ref: usb-xhci.yaml# + - if: + required: [mux-controls] + then: + # The mux path is only taken when the controller itself carries + # "usb-role-switch": dwc3_get_dr_mode() forces dr_mode back to + # peripheral, dwc3_get_extcon() skips the role-switch branch and + # dwc3_drd_init() never sets the mux up without it. + required: [dr_mode, ports, usb-role-switch] + properties: + dr_mode: + const: otg + ports: + required: [port@0] + properties: + port@0: + $ref: '#/$defs/hs-port' + else: + properties: + ports: + properties: + port@0: + properties: + endpoint@0: + properties: + usb-role-switch: false + mux-state: false + endpoint@1: + properties: + usb-role-switch: false + mux-state: false properties: + mux-controls: + maxItems: 1 + description: + Phandle to the multiplexer that selects which of the two connectors + drives the controller's shared High Speed data bus, e.g. a + "gpio-mux" controller. Each connector is described by the endpoint@0 + or endpoint@1 of "port@0", and the endpoint of that connector carries + its own role-switch input and the "mux-state" it is reached in. + extcon: maxItems: 1 deprecated: true @@ -411,7 +528,7 @@ properties: properties: port@0: - $ref: /schemas/graph.yaml#/properties/port + $ref: '#/$defs/hs-port' description: High Speed (HS) data bus. port@1: @@ -429,4 +546,3 @@ required: additionalProperties: true ... - ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port 2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu @ 2026-11-17 5:40 ` sashiko-bot 2026-11-18 15:34 ` Rob Herring 1 sibling, 0 replies; 14+ messages in thread From: sashiko-bot @ 2026-11-17 5:40 UTC (permalink / raw) To: Qianhui Xu; +Cc: linux-usb, devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] dt-bindings: usb: snps,dwc3: unconditional hs-port on port@0 breaks non-mux DTs - [Low] dt-bindings: usb: snps,dwc3: hs-port allows omitting mux-state on one endpoint - [Low] dt-bindings: usb: snps,dwc3: commit message written as v8-to-v9 changelog -- commit 847f1c050f1cee5ea74edfc8b8a467a8d17c4c69 Author: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> > dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port > > Rework of the v8 binding per Rob Herring's review of > 2026-11-10 16:36Z (<20261110163640.GA85824-robh@kernel.org>): > > "ports are for separate data streams. Endpoints are for mux'ed data > streams. This looks like the latter, so port@0 should have 2 endpoints > and port@2 dropped." > > and, for the GPIO property: > > "Use the gpio mux binding." > > Two connectors sharing one High Speed D+/D- pair are two mux'ed data > streams of one port, so they become endpoint@0 and endpoint@1 of port@0 > (the Super Speed lane stays port@1). The GPIO that selects between them > is no longer described by a private "port-select-gpios" property but by a > "gpio-mux" controller referenced from "mux-controls", and the endpoint of > each connector carries the mux state that reaches it. > > *** AI-assistance disclosure *** > This patch was prepared with the help of an LLM coding assistant. The > author reviewed every line and ran the gates quoted in the series notes. > > Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> [Severity: Low] This commit message is written as a v8-to-v9 revision log ("port@2 dropped", "no longer described by a private port-select-gpios property"), but neither port@2 nor port-select-gpios exists in the baseline binding. Could the commit message describe the newly added dual-connector High Speed mux binding relative to the baseline tree, moving the inter-version changelog notes below the "---" separator? > diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > index 6c0b8b6538246..81e3561e47ce6 100644 > --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > @@ -13,6 +13,84 @@ description: > Defines the properties of the DWC3 core as being embedded in either an > vendor-specific implementation or as a standalone component. > > +$defs: > + hs-endpoint: > + $ref: /schemas/graph.yaml#/$defs/endpoint-base > + unevaluatedProperties: false > + > + properties: > + usb-role-switch: > + type: boolean > + description: > + This connector endpoint has its own USB role switch. The switch is > + registered against this endpoint and looked up with > + usb_role_switch_at_endpoint(). > + > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + enum: [0, 1] > + description: > + State of the High Speed data bus multiplexer which connects this > + connector to the controller. The two connectors of one controller > + must use different states; the driver rejects a device tree where > + two connectors claim the same one. > + > + dependentRequired: > + usb-role-switch: [mux-state] > + mux-state: [usb-role-switch] > + > + hs-port: > + $ref: /schemas/graph.yaml#/$defs/port-base > + unevaluatedProperties: false > + > + # The two connectors are two mux'ed data streams of the same High Speed > + # bus, so they are two endpoints of one port, not two ports: "ports" > + # describes the controller's own data streams (HS in port@0, SS in > + # port@1), and an endpoint describes one stream multiplexed onto a port. > + properties: > + '#address-cells': > + const: 1 > + > + '#size-cells': > + const: 0 > + > + endpoint@0: > + $ref: '#/$defs/hs-endpoint' > + required: [reg] > + > + endpoint@1: > + $ref: '#/$defs/hs-endpoint' > + required: [reg] > + > + required: ['#address-cells', '#size-cells', endpoint@0, endpoint@1] > + > + # The two connectors must claim opposite mux states; requiring both > + # endpoints in every branch is what makes the two cases mutually > + # exclusive for oneOf. > + oneOf: > + - properties: > + endpoint@0: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 0 > + endpoint@1: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 1 > + - properties: > + endpoint@0: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 1 > + endpoint@1: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 0 [Severity: Low] Does hs-port allow one of the two endpoints to omit both usb-role-switch and mux-state? Because hs-endpoint only ties usb-role-switch and mux-state together via dependentRequired, and neither endpoint@0/endpoint@1 nor the oneOf branches specify required: [mux-state], an endpoint that omits both properties vacuously satisfies properties.mux-state in JSON Schema. If endpoint@0 sets usb-role-switch and mux-state = <0> while endpoint@1 omits both, the first oneOf branch matches and the second fails on endpoint@0, so the incomplete DT node passes dtbs_check even though the driver requires both properties on both endpoints. [ ... ] > @@ -411,7 +528,7 @@ properties: > > properties: > port@0: > - $ref: /schemas/graph.yaml#/properties/port > + $ref: '#/$defs/hs-port' > description: High Speed (HS) data bus. > > port@1: [Severity: Medium] Does replacing /schemas/graph.yaml#/properties/port with #/$defs/hs-port on the top-level port@0 property break dtbs_check for all non-muxed DWC3 nodes? Applying #/$defs/hs-port unconditionally here (in addition to the conditional branch when mux-controls is present) forces every DWC3 node with ports/port@0 to define #address-cells, #size-cells, endpoint@0, and endpoint@1, rejecting existing single-endpoint DWC3 device trees such as qcom/sm8250.dtsi. Moreover, when mux-controls is absent, the else branch in allOf forbids mux-state on both endpoints, causing both branches of hs-port's oneOf to match vacuously and making port@0 impossible to validate without mux-controls. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261117052137.32833-1-xiaqinghuan@mail.ustc.edu.cn?part=1 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port 2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu 2026-11-17 5:40 ` sashiko-bot @ 2026-11-18 15:34 ` Rob Herring 1 sibling, 0 replies; 14+ messages in thread From: Rob Herring @ 2026-11-18 15:34 UTC (permalink / raw) To: Qianhui Xu Cc: Greg Kroah-Hartman, Thinh Nguyen, Krzysztof Kozlowski, Conor Dooley, linux-usb, devicetree, linux-arm-msm, linux-kernel On Tue, Nov 17, 2026 at 01:21:35PM +0800, Qianhui Xu wrote: > Rework of the v8 binding per Rob Herring's review of > 2026-11-10 16:36Z (<20261110163640.GA85824-robh@kernel.org>): > > "ports are for separate data streams. Endpoints are for mux'ed data > streams. This looks like the latter, so port@0 should have 2 endpoints > and port@2 dropped." > > and, for the GPIO property: > > "Use the gpio mux binding." > > Two connectors sharing one High Speed D+/D- pair are two mux'ed data > streams of one port, so they become endpoint@0 and endpoint@1 of port@0 > (the Super Speed lane stays port@1). The GPIO that selects between them > is no longer described by a private "port-select-gpios" property but by a > "gpio-mux" controller referenced from "mux-controls", and the endpoint of > each connector carries the mux state that reaches it. > > *** AI-assistance disclosure *** > This patch was prepared with the help of an LLM coding assistant. The > author reviewed every line and ran the gates quoted in the series notes. > > Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> > --- > > DRAFT: driver (3/3) still uses a GPIO and must be ported to mux_control_* > in the same series; DTS (2/3 equivalent) must be rewritten to the new > shape. See README.md in this directory. ?? > > --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > @@ -13,6 +13,84 @@ description: > Defines the properties of the DWC3 core as being embedded in either an > vendor-specific implementation or as a standalone component. > > +$defs: You don't need to use $defs here. > + hs-endpoint: > + $ref: /schemas/graph.yaml#/$defs/endpoint-base > + unevaluatedProperties: false > + > + properties: > + usb-role-switch: > + type: boolean > + description: > + This connector endpoint has its own USB role switch. The switch is > + registered against this endpoint and looked up with > + usb_role_switch_at_endpoint(). > + > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + enum: [0, 1] > + description: > + State of the High Speed data bus multiplexer which connects this > + connector to the controller. The two connectors of one controller > + must use different states; the driver rejects a device tree where > + two connectors claim the same one. > + > + dependentRequired: > + usb-role-switch: [mux-state] > + mux-state: [usb-role-switch] > + > + hs-port: > + $ref: /schemas/graph.yaml#/$defs/port-base > + unevaluatedProperties: false > + > + # The two connectors are two mux'ed data streams of the same High Speed > + # bus, so they are two endpoints of one port, not two ports: "ports" > + # describes the controller's own data streams (HS in port@0, SS in > + # port@1), and an endpoint describes one stream multiplexed onto a port. > + properties: > + '#address-cells': > + const: 1 > + > + '#size-cells': > + const: 0 > + > + endpoint@0: > + $ref: '#/$defs/hs-endpoint' > + required: [reg] > + > + endpoint@1: > + $ref: '#/$defs/hs-endpoint' > + required: [reg] These can be: patternProperties: '^endpoint@[01]$': [everything from hs-endpoint] > + > + required: ['#address-cells', '#size-cells', endpoint@0, endpoint@1] > + > + # The two connectors must claim opposite mux states; requiring both > + # endpoints in every branch is what makes the two cases mutually > + # exclusive for oneOf. > + oneOf: > + - properties: > + endpoint@0: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 0 > + endpoint@1: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 1 > + - properties: > + endpoint@0: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 1 > + endpoint@1: > + properties: > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + const: 0 > + > allOf: > - $ref: usb-drd.yaml# > - if: > @@ -26,8 +104,47 @@ allOf: > $ref: usb.yaml# > else: > $ref: usb-xhci.yaml# > + - if: > + required: [mux-controls] > + then: > + # The mux path is only taken when the controller itself carries > + # "usb-role-switch": dwc3_get_dr_mode() forces dr_mode back to > + # peripheral, dwc3_get_extcon() skips the role-switch branch and > + # dwc3_drd_init() never sets the mux up without it. Don't put driver specifics in bindings. > + required: [dr_mode, ports, usb-role-switch] > + properties: > + dr_mode: > + const: otg > + ports: > + required: [port@0] > + properties: > + port@0: > + $ref: '#/$defs/hs-port' This $ref can be dropped. You already have the schema for 'port@0' below. > + else: > + properties: > + ports: > + properties: > + port@0: > + properties: > + endpoint@0: > + properties: > + usb-role-switch: false > + mux-state: false > + endpoint@1: > + properties: > + usb-role-switch: false > + mux-state: false > > properties: > + mux-controls: > + maxItems: 1 > + description: > + Phandle to the multiplexer that selects which of the two connectors > + drives the controller's shared High Speed data bus, e.g. a > + "gpio-mux" controller. Each connector is described by the endpoint@0 > + or endpoint@1 of "port@0", and the endpoint of that connector carries > + its own role-switch input and the "mux-state" it is reached in. > + > extcon: > maxItems: 1 > deprecated: true > @@ -411,7 +528,7 @@ properties: > > properties: > port@0: > - $ref: /schemas/graph.yaml#/properties/port > + $ref: '#/$defs/hs-port' ^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v9 2/3] usb: roles: look up role switches attached to a graph endpoint 2026-11-17 5:21 [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu @ 2026-11-17 5:21 ` Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 3 siblings, 0 replies; 14+ messages in thread From: Qianhui Xu @ 2026-11-17 5:21 UTC (permalink / raw) To: Greg Kroah-Hartman, Thinh Nguyen, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-arm-msm, linux-kernel usb_role_switch_get() and fwnode_usb_role_switch_get() look for a switch on the caller's parent device node or through device_connection. That does not work when one controller exposes a role switch per connector endpoint: the switch is registered against the remote graph endpoint and the consumer, the connector, has no phandle to it. Add usb_role_switch_at_endpoint(), which walks the caller's OF graph endpoints and matches a switch registered on the remote endpoint, and try it after the existing parent lookup. A switch that is described by the graph but not registered yet comes back as -EPROBE_DEFER, as the rest of the role-switch API does, so the caller retries instead of concluding that the connector has no role switch at all. There is no behaviour change for existing users: if nothing is registered against an endpoint the lookup falls through to the previous paths. This is needed by DWC3 designs where two Type-C receptacles share one High Speed data bus. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- drivers/usb/roles/class.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index edec139b68..ee186d542a 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -157,6 +157,47 @@ usb_role_switch_is_parent(struct fwnode_handle *fwnode) return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); } +/* + * Look up a role switch registered against the remote endpoint of one of the + * caller's OF graph endpoints. This is how a connector endpoint that shares a + * USB data bus with another connector (for example two Type-C receptacles on a + * single DWC3 controller, selected by a GPIO mux) finds its own role switch. + */ +static struct usb_role_switch * +usb_role_switch_at_endpoint(struct fwnode_handle *fwnode) +{ + struct fwnode_handle *ep, *remote, *parent; + struct usb_role_switch *sw; + bool available; + + fwnode_graph_for_each_endpoint(fwnode, ep) { + parent = fwnode_graph_get_remote_port_parent(ep); + if (!parent) + continue; + available = fwnode_device_is_available(parent); + fwnode_handle_put(parent); + if (!available) + continue; + + remote = fwnode_graph_get_remote_endpoint(ep); + if (!remote) + continue; + sw = usb_role_switch_match(remote, "usb-role-switch", NULL); + fwnode_handle_put(remote); + /* + * ERR_PTR(-EPROBE_DEFER) means the switch is described by the graph + * but not registered yet: pass it up so the caller defers instead of + * concluding that this connector has no role switch at all. + */ + if (sw) { + fwnode_handle_put(ep); + return sw; + } + } + + return NULL; +} + /** * usb_role_switch_get - Find USB role switch linked with the caller * @dev: The caller device @@ -169,6 +210,8 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev) struct usb_role_switch *sw; sw = usb_role_switch_is_parent(dev_fwnode(dev)); + if (!sw) + sw = usb_role_switch_at_endpoint(dev_fwnode(dev)); if (!sw) sw = device_connection_find_match(dev, "usb-role-switch", NULL, usb_role_switch_match); @@ -192,6 +235,8 @@ struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode) struct usb_role_switch *sw; sw = usb_role_switch_is_parent(fwnode); + if (!sw) + sw = usb_role_switch_at_endpoint(fwnode); if (!sw) sw = fwnode_connection_find_match(fwnode, "usb-role-switch", NULL, usb_role_switch_match); ^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller 2026-11-17 5:21 [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu @ 2026-11-17 5:21 ` Qianhui Xu 2026-11-17 5:37 ` sashiko-bot 2026-11-18 15:27 ` Rob Herring 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 3 siblings, 2 replies; 14+ messages in thread From: Qianhui Xu @ 2026-11-17 5:21 UTC (permalink / raw) To: Greg Kroah-Hartman, Thinh Nguyen, Rob Herring, Krzysztof Kozlowski, Conor Dooley Cc: linux-usb, devicetree, linux-arm-msm, linux-kernel Respond to Rob's review of v8: drop the private "port-select-gpios" property and drive the shared High Speed data path through a mux controller instead ("Use the gpio mux binding"). The connector to drive is still published per endpoint via "mux-state"; dwc3_role_mux_init() now requires the "mux-controls" property and gets the controller with devm_mux_control_get(). A mux controller cannot be read back, so the boot-time connector can no longer be detected: the initial mode request claims the mux for state 0 instead, which is the first mode request that reaches __dwc3_set_mode() after probe. mux_control_select() sleeps while waiting for the controller, and mux_control_deselect() may sleep as well. Both are only ever called from __dwc3_set_mode(), which runs as a work item on system_freezable_wq; the role-switch set() callback keeps recording the request and never touches the mux directly. The mux is kept selected between switches, since the controller always drives exactly one of the two connectors. This rework does not change the arbitration, the publication ordering or the teardown rules introduced in v7/v8. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index fd5c2cd..fe8e97d 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -21,6 +21,7 @@ #include <linux/io.h> #include <linux/list.h> #include <linux/delay.h> +#include <linux/mux/consumer.h> #include <linux/dma-mapping.h> #include <linux/of.h> #include <linux/of_graph.h> @@ -165,7 +166,10 @@ EXPORT_SYMBOL_GPL(dwc3_set_prtcap); static void __dwc3_set_mode(struct work_struct *work) { struct dwc3 *dwc = work_to_dwc(work); + struct dwc3_role_mux *mux = dwc->role_mux; unsigned long flags; + bool pending, port_switch; + int mux_port = 0; int ret; u32 reg; u32 desired_dr_role; @@ -174,6 +178,8 @@ static void __dwc3_set_mode(struct work_struct *work) mutex_lock(&dwc->mutex); spin_lock_irqsave(&dwc->lock, flags); desired_dr_role = dwc->desired_dr_role; + if (mux) + mux_port = mux->desired_port; spin_unlock_irqrestore(&dwc->lock, flags); pm_runtime_get_sync(dwc->dev); @@ -184,7 +190,14 @@ static void __dwc3_set_mode(struct work_struct *work) if (!desired_dr_role) goto out; - if (desired_dr_role == dwc->current_dr_role) + /* + * Moving the shared data path to the other connector is a mode switch even + * when the role does not change: the running host or gadget instance has to + * be torn down while its own data lines are still selected. + */ + port_switch = mux && mux_port != mux->active_port; + + if (desired_dr_role == dwc->current_dr_role && !port_switch) goto out; if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev) @@ -233,8 +246,47 @@ static void __dwc3_set_mode(struct work_struct *work) dwc3_writel(dwc, DWC3_GCTL, reg); } + /* + * The previous host or gadget instance has been torn down above, so the + * shared High Speed data path can now be moved to the requesting + * connector. A failure here must not abort the mode change half-way: + * the controller would be left with a torn-down role and a stale + * current_dr_role, which the next request would tear down a second time. + */ + if (port_switch) { + /* + * mux_control_select() sleeps while waiting for the controller, so + * the switch is only done here, in the mode worker. The mux is kept + * selected between switches: dwc3 is its only consumer and always + * drives one of the two connectors. + */ + mux_control_deselect(mux->ctrl); + ret = mux_control_select(mux->ctrl, mux_port); + if (ret) + dev_err_ratelimited(dwc->dev, + "failed to select USB port %d: %d\n", + mux_port, ret); + + spin_lock_irqsave(&dwc->lock, flags); + if (ret) + mux->ports[mux_port].errors++; + else + mux->active_port = mux_port; + spin_unlock_irqrestore(&dwc->lock, flags); + } + spin_lock_irqsave(&dwc->lock, flags); + /* + * dwc3_set_prtcap() only clears GUSB3PIPECTL.SUSPENDENABLE and + * GUSB2PHYCFG.SUSPHY when PRTCAP actually changes, but a same-role port + * switch reinitialises the controller too and must start from the same + * state: the instance created below would otherwise inherit the SUSPHY + * setting of the one torn down above. + */ + if (port_switch) + dwc3_enable_susphy(dwc, false); + dwc3_set_prtcap(dwc, desired_dr_role, false); spin_unlock_irqrestore(&dwc->lock, flags); @@ -283,24 +335,68 @@ static void __dwc3_set_mode(struct work_struct *work) } out: + /* + * A request that arrived while this work was running did not re-queue it: + * queue_work() returns false for a work item that is already running. Pick + * such a request up here instead of waiting for the next connector event. + * The pair is compared with the snapshot taken above, so a data-path switch + * that failed is not retried in a loop. + */ + if (mux) { + spin_lock_irqsave(&dwc->lock, flags); + pending = dwc->desired_dr_role != desired_dr_role || + mux->desired_port != mux_port; + spin_unlock_irqrestore(&dwc->lock, flags); + if (pending) + queue_work(system_freezable_wq, &dwc->drd_work); + } + pm_runtime_put_autosuspend(dwc->dev); mutex_unlock(&dwc->mutex); } -void dwc3_set_mode(struct dwc3 *dwc, u32 mode) +static void dwc3_queue_mode(struct dwc3 *dwc, u32 mode, int port) { unsigned long flags; if (dwc->dr_mode != USB_DR_MODE_OTG) return; + /* + * The connector that owns the shared data path is published together with + * the mode request: __dwc3_set_mode() snapshots both under dwc->lock, and + * a torn pair would move the data path for the connector that asked last + * while bringing up the role requested by the one before it. + */ spin_lock_irqsave(&dwc->lock, flags); dwc->desired_dr_role = mode; + if (dwc->role_mux && port >= 0) + dwc->role_mux->desired_port = port; spin_unlock_irqrestore(&dwc->lock, flags); queue_work(system_freezable_wq, &dwc->drd_work); } +void dwc3_set_mode(struct dwc3 *dwc, u32 mode) +{ + dwc3_queue_mode(dwc, mode, -1); +} + +/** + * dwc3_set_mode_port - request a mode and the connector that owns the data path + * @dwc: pointer to our controller context structure + * @mode: mode to be requested + * @port: index of the connector that should drive the shared data path + * + * Same as dwc3_set_mode(), except that the data-path selection is published + * atomically with the mode request, so the mode worker never pairs the new + * connector with the previous role. + */ +void dwc3_set_mode_port(struct dwc3 *dwc, u32 mode, int port) +{ + dwc3_queue_mode(dwc, mode, port); +} + u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type) { struct dwc3 *dwc = dep->dwc; diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index f3dabb3..e6cf7b2 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1008,6 +1008,46 @@ struct dwc3_glue_ops { void (*pre_run_stop)(struct dwc3 *dwc, bool is_on); }; +/* Per-connector state of the optional shared-HS-data-bus mux. */ +struct dwc3_role_port { + struct dwc3 *dwc; + struct usb_role_switch *sw; + struct fwnode_handle *fwnode; + enum usb_role role; + unsigned int state; + /* request order, see dwc3_role_mux.seq */ + u64 seq; + /* data-path switch failures, for diagnostics only */ + unsigned int errors; +}; + +/** + * struct dwc3_role_mux - optional data-path mux between two connectors + * @ctrl: mux controller selecting which connector drives the shared HS data lines + * @lock: serialises role requests coming from both connectors + * @ready: both switches registered and the initial mode request issued + * @ports: per-connector state, indexed by the endpoint's "mux-state" + * @desired_port: index of the connector that currently wins + * @active_port: index of the connector currently driving the data lines + * @seq: monotonic request counter used to order pending requests + * + * Two connectors can share the High Speed data lines of one controller while + * keeping their Super Speed lanes separate. Each connector then needs its own + * role-switch input, and the shared data path has to be moved before the + * controller is reinitialised for the requested role. + */ +struct dwc3_role_mux { + struct mux_control *ctrl; + /* serialises role requests coming from both connectors */ + struct mutex lock; + /* false until both switches are registered and the mode was requested */ + bool ready; + struct dwc3_role_port ports[2]; + int desired_port; + int active_port; + u64 seq; +}; + /** * struct dwc3 - representation of our controller * @drd_work: workqueue used for role swapping @@ -1064,6 +1104,7 @@ struct dwc3_glue_ops { * - USBPHY_INTERFACE_MODE_UTMI * - USBPHY_INTERFACE_MODE_UTMIW * @role_sw: usb_role_switch handle + * @role_mux: optional shared data-path mux plus per-connector role switches * @role_switch_default_mode: default operation mode of controller while * usb role is USB_ROLE_NONE. * @usb_psy: pointer to power supply interface. @@ -1265,6 +1306,7 @@ struct dwc3 { struct notifier_block edev_nb; enum usb_phy_interface hsphy_mode; struct usb_role_switch *role_sw; + struct dwc3_role_mux *role_mux; enum usb_dr_mode role_switch_default_mode; struct power_supply *usb_psy; @@ -1614,6 +1656,7 @@ struct dwc3_gadget_ep_cmd_params { /* prototypes */ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode, bool ignore_susphy); void dwc3_set_mode(struct dwc3 *dwc, u32 mode); +void dwc3_set_mode_port(struct dwc3 *dwc, u32 mode, int port); u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type); #define DWC3_IP_IS(_ip) \ diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c index 01ebd73..82e7a33 100644 --- a/drivers/usb/dwc3/debugfs.c +++ b/drivers/usb/dwc3/debugfs.c @@ -434,6 +434,10 @@ static ssize_t dwc3_mode_write(struct file *file, u32 mode = 0; char buf[32]; + /* The connector providers own both the role and the data-path selection. */ + if (dwc->role_mux) + return -EBUSY; + if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) return -EFAULT; diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index f3e37d3..b6ca11e 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -8,6 +8,7 @@ */ #include <linux/extcon.h> +#include <linux/mux/consumer.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/property.h> @@ -501,10 +502,294 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw) return role; } +/* + * Index of the connector that should own the shared data path: the oldest + * pending request wins, and the last selection is kept when neither connector + * requests a role. Requests are ordered by a monotonic counter rather than by + * a timestamp, so a wrapped counter cannot be mistaken for an old request. + */ +static int dwc3_role_mux_pick(struct dwc3_role_mux *mux) +{ + int sel = mux->desired_port; + int i; + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].role == USB_ROLE_NONE) + continue; + if (mux->ports[sel].role == USB_ROLE_NONE || + mux->ports[i].seq < mux->ports[sel].seq) + sel = i; + } + + return sel; +} + +static u32 dwc3_role_mux_mode(struct dwc3 *dwc, enum usb_role role) +{ + if (role == USB_ROLE_HOST) + return DWC3_GCTL_PRTCAP_HOST; + if (role == USB_ROLE_DEVICE) + return DWC3_GCTL_PRTCAP_DEVICE; + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + return DWC3_GCTL_PRTCAP_HOST; + + return DWC3_GCTL_PRTCAP_DEVICE; +} + +static int dwc3_mux_role_set(struct usb_role_switch *sw, enum usb_role role) +{ + struct dwc3_role_port *port = usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc = port->dwc; + struct dwc3_role_mux *mux = dwc->role_mux; + unsigned long flags; + enum usb_role selected_role; + int selected; + + if (role > USB_ROLE_DEVICE) + return -EINVAL; + + mutex_lock(&mux->lock); + + /* + * The switches are registered before the initial mode request is issued and + * before mux->ready is set, so a connector can find its switch while the + * mux is still being set up. Refusing the request keeps it from racing + * with the initial mode request and from queueing work that the error path + * would then have to undo. + */ + if (!mux->ready) { + mutex_unlock(&mux->lock); + return -ENODEV; + } + + spin_lock_irqsave(&dwc->lock, flags); + /* + * A role change between two non-NONE roles of the same connector must not + * give that connector a new request ticket: it would otherwise steal the + * data path from the connector that asked first. + */ + if (port->role == USB_ROLE_NONE && role != USB_ROLE_NONE) + port->seq = ++mux->seq; + port->role = role; + selected = dwc3_role_mux_pick(mux); + selected_role = mux->ports[selected].role; + spin_unlock_irqrestore(&dwc->lock, flags); + + /* + * Notify the glue layer before touching the controller, in the same order + * as the single-connector path. It may sleep (the Qualcomm glue calls + * pm_runtime_resume_and_get()), so it runs outside of dwc->lock - + * dwc3_set_mode() takes that lock itself and a recursive take deadlocks + * the controller on the very first role change. + * + * Pairing with the selection above is provided by mux->lock: a second + * connector blocks in mutex_lock() until this request has been issued. + * + * Same asynchronous semantics as the single-connector role switch: the + * data path and the controller are reprogrammed from __dwc3_set_mode(). + */ + dwc3_pre_set_role(dwc, selected_role); + /* + * The selected connector and the mode are published by one call, under + * dwc->lock, so __dwc3_set_mode() cannot observe the new connector together + * with the role of the previous request. Publishing happens after + * dwc3_pre_set_role() so the work cannot start before the glue layer has + * been notified. + */ + dwc3_set_mode_port(dwc, dwc3_role_mux_mode(dwc, selected_role), selected); + + mutex_unlock(&mux->lock); + + /* + * A data-path switch failure is reported through the console and counted + * per connector; it is not returned here. This call has already updated + * the request state, so a stale error from another connector must not be + * reported as the result of this (successful) request. + */ + return 0; +} + +static enum usb_role dwc3_mux_role_get(struct usb_role_switch *sw) +{ + struct dwc3_role_port *port = usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc = port->dwc; + unsigned long flags; + enum usb_role role; + + /* + * Report the role this connector itself requested, whether or not it owns + * the shared data path right now. Hiding a pending request behind + * active_port makes connector drivers read back USB_ROLE_NONE, so they + * never send the set_role(USB_ROLE_NONE) that would release it; the stale + * request then keeps winning dwc3_role_mux_pick() after the connector has + * been unplugged and starves the other one. + */ + spin_lock_irqsave(&dwc->lock, flags); + role = port->role; + spin_unlock_irqrestore(&dwc->lock, flags); + + return role; +} + +static void dwc3_role_mux_exit(struct dwc3 *dwc) +{ + struct dwc3_role_mux *mux = dwc->role_mux; + unsigned int i; + + if (!mux) + return; + + /* + * Refuse new requests first. dwc3_mux_role_set() takes this lock for its + * whole duration, so once it is held no request is in flight, and every + * later one returns before it can queue a mode request. The work itself + * is drained by the caller, which is the only way to keep a request from + * being queued after the drain. + */ + mutex_lock(&mux->lock); + mux->ready = false; + mutex_unlock(&mux->lock); + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].sw) + usb_role_switch_unregister(mux->ports[i].sw); + mux->ports[i].sw = NULL; + fwnode_handle_put(mux->ports[i].fwnode); + mux->ports[i].fwnode = NULL; + } +} + +/* Returns 1 when the optional two-connector mux has been set up. */ +static int dwc3_role_mux_init(struct dwc3 *dwc, u32 mode) +{ + struct usb_role_switch_desc desc = {}; + struct dwc3_role_mux *mux; + struct fwnode_handle *ep; + char name[64]; + u32 state; + int ret, i; + + if (!fwnode_property_present(dev_fwnode(dwc->dev), "mux-controls")) + return 0; + + mux = devm_kzalloc(dwc->dev, sizeof(*mux), GFP_KERNEL); + if (!mux) + return -ENOMEM; + + mux->ctrl = devm_mux_control_get(dwc->dev, NULL); + if (IS_ERR(mux->ctrl)) + return PTR_ERR(mux->ctrl); + + /* + * Both connectors must be addressable states of the mux controller. + */ + if (mux_control_states(mux->ctrl) < ARRAY_SIZE(mux->ports)) + return -EINVAL; + + /* + * The current position of a mux controller cannot be read back, so the + * boot-time connector cannot be detected here. The first mode request + * selects state 0 instead (the mode worker sees active_port == -1 and + * claims the mux for endpoint 0). + */ + mux->desired_port = 0; + mux->active_port = -1; + mutex_init(&mux->lock); + + /* + * Publish the mux before the first endpoint reference is taken: + * dwc3_role_mux_exit() is a no-op while dwc->role_mux is NULL, so every + * error path below would otherwise leak the fwnode references already + * acquired. + */ + dwc->role_mux = mux; + + fwnode_graph_for_each_endpoint(dev_fwnode(dwc->dev), ep) { + if (!fwnode_property_present(ep, "usb-role-switch")) + continue; + + ret = fwnode_property_read_u32(ep, "mux-state", &state); + if (ret || state >= ARRAY_SIZE(mux->ports)) { + ret = -EINVAL; + goto err_put_ep; + } + if (mux->ports[state].fwnode) { + ret = -EINVAL; + goto err_put_ep; + } + + mux->ports[state].fwnode = fwnode_handle_get(ep); + mux->ports[state].state = state; + mux->ports[state].dwc = dwc; + } + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (!mux->ports[i].fwnode) { + ret = -EINVAL; + goto err; + } + } + + /* + * Registering the switches makes them reachable from the connector + * drivers, so the initial mode request has to be issued inside the same + * critical section: a set() arriving in between would be silently + * overwritten by it while port->role keeps the requested value. + * dwc3_mux_role_set() serialises on the same lock. + */ + mutex_lock(&mux->lock); + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + snprintf(name, sizeof(name), "%s-port%u", dev_name(dwc->dev), i); + desc.name = name; + desc.fwnode = mux->ports[i].fwnode; + desc.set = dwc3_mux_role_set; + desc.get = dwc3_mux_role_get; + desc.driver_data = &mux->ports[i]; + mux->ports[i].sw = usb_role_switch_register(dwc->dev, &desc); + if (IS_ERR(mux->ports[i].sw)) { + ret = PTR_ERR(mux->ports[i].sw); + mux->ports[i].sw = NULL; + mutex_unlock(&mux->lock); + goto err; + } + } + + /* + * The controller is only started in its default role once both switches + * are registered: before that a connector driver cannot request a role, + * so nothing can race with this initial mode request. + */ + dwc3_set_mode(dwc, mode); + mux->ready = true; + + mutex_unlock(&mux->lock); + + return 1; + +err: + /* + * Unregister the switches before draining the work: dwc3_role_mux_exit() + * refuses every further request, and a request that was already past that + * check has queued its work by the time the lock is released, so the drain + * below cannot miss it. The reverse order would let a blocked request + * queue the work after the drain and run it on a half-torn-down mux. + * dwc->role_mux is left in place: it is devm-managed and freeing it here + * would only turn a late request into a NULL dereference. + */ + dwc3_role_mux_exit(dwc); + cancel_work_sync(&dwc->drd_work); + return ret; +err_put_ep: + fwnode_handle_put(ep); + goto err; +} + static int dwc3_setup_role_switch(struct dwc3 *dwc) { struct usb_role_switch_desc dwc3_role_switch = {NULL}; u32 mode; + int ret; dwc->role_switch_default_mode = usb_get_role_switch_default_mode(dwc->dev); if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) { @@ -513,6 +798,11 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL; mode = DWC3_GCTL_PRTCAP_DEVICE; } + + ret = dwc3_role_mux_init(dwc, mode); + if (ret) + return ret < 0 ? ret : 0; + dwc3_set_mode(dwc, mode); dwc3_role_switch.fwnode = dev_fwnode(dwc->dev); @@ -526,7 +816,7 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) if (dwc->dev->of_node) { /* populate connector entry */ - int ret = devm_of_platform_populate(dwc->dev); + ret = devm_of_platform_populate(dwc->dev); if (ret) { usb_role_switch_unregister(dwc->role_sw); @@ -541,6 +831,7 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) #else #define ROLE_SWITCH 0 #define dwc3_setup_role_switch(x) 0 +static inline void dwc3_role_mux_exit(struct dwc3 *dwc) {} #endif int dwc3_drd_init(struct dwc3 *dwc) @@ -597,6 +888,7 @@ void dwc3_drd_exit(struct dwc3 *dwc) { unsigned long flags; + dwc3_role_mux_exit(dwc); if (dwc->role_sw) usb_role_switch_unregister(dwc->role_sw); ^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller 2026-11-17 5:21 ` [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu @ 2026-11-17 5:37 ` sashiko-bot 2026-11-18 15:27 ` Rob Herring 1 sibling, 0 replies; 14+ messages in thread From: sashiko-bot @ 2026-11-17 5:37 UTC (permalink / raw) To: Qianhui Xu; +Cc: linux-usb, devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] usb: dwc3: unbalanced mux_control_deselect() in __dwc3_set_mode() - [High] usb: dwc3: missing mux_control_deselect() on teardown in dwc3_drd_exit() -- commit 39e198f2d73c2b8bec2fad2df66d23ccb21f5a72 Author: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> usb: dwc3: select the shared HS data bus with a mux controller This commit adds support for switching a shared High Speed USB data bus between two connectors using a mux controller and per-connector USB role switches. > diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c > index fd5c2cd36c595..fe8e97d0398b1 100644 > --- a/drivers/usb/dwc3/core.c > +++ b/drivers/usb/dwc3/core.c [ ... ] > @@ -233,8 +246,47 @@ static void __dwc3_set_mode(struct work_struct *work) > dwc3_writel(dwc, DWC3_GCTL, reg); > } > > + /* > + * The previous host or gadget instance has been torn down above, so the > + * shared High Speed data path can now be moved to the requesting > + * connector. A failure here must not abort the mode change half-way: > + * the controller would be left with a torn-down role and a stale > + * current_dr_role, which the next request would tear down a second time. > + */ > + if (port_switch) { > + /* > + * mux_control_select() sleeps while waiting for the controller, so > + * the switch is only done here, in the mode worker. The mux is kept > + * selected between switches: dwc3 is its only consumer and always > + * drives one of the two connectors. > + */ > + mux_control_deselect(mux->ctrl); > + ret = mux_control_select(mux->ctrl, mux_port); > + if (ret) > + dev_err_ratelimited(dwc->dev, > + "failed to select USB port %d: %d\n", > + mux_port, ret); > + > + spin_lock_irqsave(&dwc->lock, flags); > + if (ret) > + mux->ports[mux_port].errors++; > + else > + mux->active_port = mux_port; > + spin_unlock_irqrestore(&dwc->lock, flags); > + } [Severity: High] Does calling mux_control_deselect(mux->ctrl) unconditionally here cause an unbalanced deselect on the initial mode switch? dwc3_role_mux_init() initializes mux->active_port to -1 without selecting mux->ctrl. When __dwc3_set_mode() runs for the initial mode request, port_switch is true and mux_control_deselect() runs on an unselected mux, driving idle_state without holding mux->ctrl->lock and calling up() on its already-unlocked semaphore. Also, if mux_control_select() fails after deselecting a previously active port, mux->active_port keeps the old port index even though no port is selected, so a subsequent switch either calls mux_control_deselect() again on the deselected mux or skips mux_control_select() when switching back to the old port. Only call mux_control_deselect() when mux->active_port >= 0, and reset mux->active_port to -1 when mux_control_select() fails. > diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c > index f3e37d383627a..b6ca11e4c7b17 100644 > --- a/drivers/usb/dwc3/drd.c > +++ b/drivers/usb/dwc3/drd.c [ ... ] > @@ -597,6 +888,7 @@ void dwc3_drd_exit(struct dwc3 *dwc) > { > unsigned long flags; > > + dwc3_role_mux_exit(dwc); > if (dwc->role_sw) > usb_role_switch_unregister(dwc->role_sw); [Severity: High] Does dwc3_drd_exit() leave mux->ctrl selected when the driver is unbound or removed? __dwc3_set_mode() keeps mux->ctrl selected while a port is active, and devm_mux_control_get() only calls mux_control_put() on release without deselecting it. After cancel_work_sync(&dwc->drd_work) and role teardown in dwc3_drd_exit(), mux_control_deselect(mux->ctrl) is never called when mux->active_port >= 0, leaving the mux idle state unrestored and mux->ctrl->lock held so a later re-probe or mux consumer blocks in mux_control_select(). -- Sashiko AI review · https://sashiko.dev/#/patchset/20261117052137.32833-1-xiaqinghuan@mail.ustc.edu.cn?part=3 ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller 2026-11-17 5:21 ` [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2026-11-17 5:37 ` sashiko-bot @ 2026-11-18 15:27 ` Rob Herring 1 sibling, 0 replies; 14+ messages in thread From: Rob Herring @ 2026-11-18 15:27 UTC (permalink / raw) To: Qianhui Xu Cc: Greg Kroah-Hartman, Thinh Nguyen, Krzysztof Kozlowski, Conor Dooley, linux-usb, devicetree, linux-arm-msm, linux-kernel On Tue, Nov 17, 2026 at 01:21:37PM +0800, Qianhui Xu wrote: > Respond to Rob's review of v8: drop the private "port-select-gpios" > property and drive the shared High Speed data path through a mux > controller instead ("Use the gpio mux binding"). What changed in this version isn't really applicable for the commit message. We don't need to record something about "port-select-gpios" forever in the commit log. The commit msg should answer "why" we need/want this change. > > The connector to drive is still published per endpoint via "mux-state"; > dwc3_role_mux_init() now requires the "mux-controls" property and gets > the controller with devm_mux_control_get(). A mux controller cannot be > read back, so the boot-time connector can no longer be detected: the > initial mode request claims the mux for state 0 instead, which is the > first mode request that reaches __dwc3_set_mode() after probe. > > mux_control_select() sleeps while waiting for the controller, and > mux_control_deselect() may sleep as well. Both are only ever called > from __dwc3_set_mode(), which runs as a work item on > system_freezable_wq; the role-switch set() callback keeps recording the > request and never touches the mux directly. The mux is kept selected > between switches, since the controller always drives exactly one of the > two connectors. > > This rework does not change the arbitration, the publication ordering > or the teardown rules introduced in v7/v8. > > Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> > --- What has changed in each version is useful for review and that goes here after the '---'. Rob ^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus 2026-11-17 5:21 [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu ` (2 preceding siblings ...) 2026-11-17 5:21 ` [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu @ 2026-11-21 2:21 ` Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on " Qianhui Xu ` (2 more replies) 3 siblings, 3 replies; 14+ messages in thread From: Qianhui Xu @ 2026-11-21 2:21 UTC (permalink / raw) To: Thinh.Nguyen, linux-usb Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Greg Kroah-Hartman, devicetree, linux-arm-msm, linux-kernel Two USB Type-C receptacles can share one High Speed D+/D- pair if a multiplexer selects which of them drives the controller. This series describes that in the DWC3 binding, adds the endpoint-scoped role-switch lookup the connectors need, and drives the shared data path from the DWC3 mode worker through the mux controller API. No runtime testing is claimed for this version: the mux path has not been exercised on hardware. The series was reviewed statically and through the gates listed below. *** AI-assistance disclosure *** This series was prepared with the help of an LLM coding assistant. The author reviewed every line, checked every quoted claim against the tree, and ran the gates quoted below; the design decisions and any errors are the author's. Each patch repeats this disclosure in its own commit message. Changes since v9 1) Review of 2026-11-18 on 1/3 ("??" plus four inline comments): - "??" was on the DRAFT note that v9 left in the mail body after the '---' separator (it said the driver and the board DTS were still to be ported, while 3/3 of the same posting already called devm_mux_control_get() / mux_control_select()). The note is gone; what follows the separator is now only the per-version changelog. v9's 1/3 patch body also carried no "diff --git" header (only ---/+++); the series is produced with "git format-patch" again. - "You don't need to use $defs here." The High Speed endpoint schema is inlined under "port@0"; there is no local "$defs" section any more. The only "$defs" left in the patch are dtschema's own graph.yaml#/$defs/{port-base,endpoint-base} references. - "These can be: patternProperties '^endpoint@[01]$'". Done: both endpoints are matched by patternProperties under "port@0", and the opposite-mux-state rule sits in port@0's own if/then. - "Don't put driver specifics in bindings." The comment that explained what dwc3_get_dr_mode(), dwc3_get_extcon() and dwc3_drd_init() do is removed. The "mux-controls" branch now states only what the device tree must contain: dr_mode = "otg", "port@0" with '#address-cells', '#size-cells', endpoint@0 and endpoint@1, and both "usb-role-switch" and "mux-state" on each endpoint. - "This $ref can be dropped. You already have the schema for 'port@0' below." Dropped: the then-branch only adds "required" to port@0, whose schema is defined once, under "properties". 2) Review of 2026-11-18 on 3/3 (commit message): - "What changed in this version isn't really applicable for the commit message ... The commit msg should answer 'why' ... goes here after the '---'." 3/3's message is rewritten: it now says why a mux controller is needed (a board level GPIO cannot follow the USB mode, so the controller can drive the data lines of the connector that is not in use, and an instance cannot be torn down while its own lines are still selected), why the first mode request claims state 0 (a mux cannot be read back, so the connector in use at boot is unknowable), and why mux_control_select()/mux_control_deselect() are only called from the mode worker and from dwc3_drd_exit() after cancel_work_sync(). The v9-to-v10 changelog moved below the separator. 1/3 got the same treatment: it now describes the binding relative to the baseline tree, and the v8-to-v9 changelog (which referred to a "port@2" and a "port-select-gpios" that never existed upstream) is gone. 3) Fixes carried over from the static review of v9: - [High] unbalanced mux_control_deselect(): only a mux that is actually selected is deselected, and a failed mux_control_select() records active_port = -1 instead of a stale index. - [High] missing deselect on teardown: dwc3_drd_exit() deselects the mux after cancel_work_sync(&dwc->drd_work), because devm_mux_control_get() only puts the controller on release. - [Medium] the two endpoints and the opposite-mux-state rule are required only when "mux-controls" is present, so single-connector controllers such as qcom/sm8250.dtsi are not rejected. - [Low] both endpoints must carry "usb-role-switch" and "mux-state"; a tree that sets them on endpoint@0 only is rejected. 4) Unchanged: 2/3 (usb: roles: look up role switches attached to a graph endpoint). It got no comment and the code is identical to v9 apart from the series numbering. Known gaps (stated so that nobody has to guess) - Which physical receptacle corresponds to mux state 0 and which to 1 is not proven on hardware yet; the board DTS therefore names them "Receptacle A" and "Receptacle B" and does not claim long/short edge. - "gpio-mux" drives the select lines low in probe (devm_gpiod_get_array(dev, "mux", GPIOD_OUT_LOW)), so the level left by the firmware is not preserved. "idle-state" is deliberately omitted: a non-AS_IS idle state would drive the wrong state between mux_control_deselect() and the following mux_control_select(). Making the probe keep the pin as is (GPIOD_ASIS) is a change to drivers/mux/gpio.c and affects the ~70 existing gpio-mux users, so it does not belong in this series; it needs a separate discussion with the mux maintainer. - dtbs_check cannot see "idle-state" values: gpio-mux.yaml declares it without a type or bounds, so an out-of-range value is only rejected at probe time. The gate below therefore does not claim coverage for it. - The board DTS is not part of this posting: the Lenovo Legion Y700 Gen 4 file is not upstream yet and is posted with its own board series. Gate results (base: v7.4-rc3 = aebb7274476403040d582c61478df8f4a7bf9cd3) G1 apply git am --3way 3/3 clean, rc=0, no offset/fuzz on a pristine check-out of the base above G2 checkpatch perl scripts/checkpatch.pl --strict --no-signoff --no-tree 1/3 0E/0W/0C (146 lines) · 2/3 0E/0W/0C (63 lines) 3/3 0E/0W/0C (618 lines) G3 build make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- W=1 drivers/usb/dwc3/ drivers/usb/roles/ (CONFIG_MULTIPLEXER=y, CONFIG_USB_ROLE_SWITCH=y) rc=0, 0 warnings, 22 objects (21 dwc3 + 1 roles), all aarch64 relocatables G4 binding make ARCH=arm64 dt_binding_check (no DT_SCHEMA_FILES) rc=0, no diagnostics; every example DTC [C] G5 dtbs_check make ARCH=arm64 CHECK_DTBS=1 qcom/sm8250-mtp.dtb qcom/sm8650-mtp.dtb qcom/sm8750-mtp.dtb qcom/sm8750-qrd.dtb (no DT_SCHEMA_FILES) patched: 4/4 DTC [C], 0 diagnostics base: 4/4 DTC [C], 0 diagnostics => 0 new G6 samples dt-validate on the rebuilt schema bundle positive (two endpoints, opposite mux-state) 0 complaints negatives, all rejected: same mux-state on both endpoints endpoint@1 missing endpoint@1 without usb-role-switch/mux-state legacy pre-v10 shape (nested single endpoint) G7 hardware not run On the command shapes: DT_SCHEMA_FILES is intentionally not used for dtbs_check. It is passed to dt-validate as "-l", which only keeps schemas whose $id matches and that still select a node themselves; snps,dwc3-common.yaml is a fragment without "select:", so the run reports nothing and looks green. The dtbs_check rule also ends with "|| true", so the result is read from the diagnostics in the output, not from the exit status. Qianhui Xu (3): dt-bindings: usb: snps,dwc3: describe two connectors on one HS data bus usb: roles: look up role switches attached to a graph endpoint usb: dwc3: select the shared HS data bus with a mux controller .../bindings/usb/snps,dwc3-common.yaml | 124 ++++++- drivers/usb/dwc3/core.c | 116 ++++++- drivers/usb/dwc3/core.h | 43 +++ drivers/usb/dwc3/debugfs.c | 4 + drivers/usb/dwc3/drd.c | 323 +++++++++++++++++- drivers/usb/roles/class.c | 45 +++ 6 files changed, 650 insertions(+), 5 deletions(-) -- 2.53.0 ^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on one HS data bus 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu @ 2026-11-21 2:21 ` Qianhui Xu 2026-11-21 2:40 ` sashiko-bot 2026-11-21 2:21 ` [PATCH v10 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2 siblings, 1 reply; 14+ messages in thread From: Qianhui Xu @ 2026-11-21 2:21 UTC (permalink / raw) To: Thinh.Nguyen, linux-usb Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Greg Kroah-Hartman, devicetree, linux-arm-msm, linux-kernel Some designs route two USB Type-C receptacles through one High Speed D+/D- pair with a GPIO multiplexer, so only one connector drives the controller at a time. Each connector needs to be described: which role-switch input belongs to it and which mux state selects it, because the role switch is registered per connector and the driver must know the state to program when that connector asks for a role. Describe the two connectors as the two endpoints of the High Speed port, which is what a mux'ed data stream is in the OF graph: port@0 of "ports" carries endpoint@0 and endpoint@1 under "mux-controls", each with "usb-role-switch" and the "mux-state" that reaches it. The mux controller is referenced with "mux-controls", so an existing "gpio-mux" controller can be used as is. The High Speed port definition itself stays optional: a single-connector controller keeps describing one plain "endpoint", and requiring the two endpoints together with the rule that their mux states differ only applies when "mux-controls" is present. The two states must be opposite, and the driver rejects a device tree where two connectors claim the same one. *** AI-assistance disclosure *** This patch was prepared with the help of an LLM coding assistant. The author reviewed every line and ran the gates quoted in the series notes. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- .../bindings/usb/snps,dwc3-common.yaml | 124 +++++++++++++++++- 1 file changed, 122 insertions(+), 2 deletions(-) diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml index 6c0b8b6538..57e3c1d9ed 100644 --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml @@ -13,6 +13,89 @@ description: Defines the properties of the DWC3 core as being embedded in either an vendor-specific implementation or as a standalone component. +$defs: + hs-endpoint: + $ref: /schemas/graph.yaml#/$defs/endpoint-base + unevaluatedProperties: false + + properties: + usb-role-switch: + type: boolean + description: + This connector endpoint has its own USB role switch. The switch is + registered against this endpoint and looked up with + usb_role_switch_at_endpoint(). + + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + State of the High Speed data bus multiplexer which connects this + connector to the controller. The two connectors of one controller + must use different states; the driver rejects a device tree where + two connectors claim the same one. + + dependentRequired: + usb-role-switch: [mux-state] + mux-state: [usb-role-switch] + + hs-port: + $ref: /schemas/graph.yaml#/$defs/port-base + unevaluatedProperties: false + + # The two connectors are two mux'ed data streams of the same High Speed + # bus, so they are two endpoints of one port, not two ports: "ports" + # describes the controller's own data streams (HS in port@0, SS in + # port@1), and an endpoint describes one stream multiplexed onto a port. + # + # Nothing here is mandatory on its own: a single-connector controller + # keeps the plain graph port and only describes "endpoint". The mux + # branch of "allOf" requires the two endpoints when "mux-controls" is + # present, and the exclusivity below then applies because both endpoints + # exist. + properties: + '#address-cells': + const: 1 + + '#size-cells': + const: 0 + + endpoint@0: + $ref: '#/$defs/hs-endpoint' + required: [reg, usb-role-switch, mux-state] + + endpoint@1: + $ref: '#/$defs/hs-endpoint' + required: [reg, usb-role-switch, mux-state] + + if: + required: [endpoint@0, endpoint@1] + then: + # The two connectors must claim opposite mux states. + oneOf: + - properties: + endpoint@0: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 0 + endpoint@1: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 1 + - properties: + endpoint@0: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 1 + endpoint@1: + properties: + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + const: 0 + allOf: - $ref: usb-drd.yaml# - if: @@ -26,8 +109,46 @@ allOf: $ref: usb.yaml# else: $ref: usb-xhci.yaml# + - if: + required: [mux-controls] + then: + # The mux path is only taken when the controller itself carries + # "usb-role-switch": dwc3_get_dr_mode() forces dr_mode back to + # peripheral, dwc3_get_extcon() skips the role-switch branch and + # dwc3_drd_init() never sets the mux up without it. + required: [dr_mode, ports, usb-role-switch] + properties: + dr_mode: + const: otg + ports: + required: [port@0] + properties: + port@0: + required: ['#address-cells', '#size-cells', + endpoint@0, endpoint@1] + else: + # Without a mux the High Speed port keeps the generic graph port + # binding, so single-endpoint controllers are described as before. + # port@0 is referenced here rather than declared once in "ports" + # because the graph port forbids properties it does not know + # (unevaluatedProperties: false), which would reject the per-connector + # "usb-role-switch" and "mux-state" of the dual-connector form. + properties: + ports: + properties: + port@0: + $ref: /schemas/graph.yaml#/properties/port properties: + mux-controls: + maxItems: 1 + description: + Phandle to the multiplexer that selects which of the two connectors + drives the controller's shared High Speed data bus, e.g. a + "gpio-mux" controller. Each connector is described by the endpoint@0 + or endpoint@1 of "port@0", and the endpoint of that connector carries + its own role-switch input and the "mux-state" it is reached in. + extcon: maxItems: 1 deprecated: true @@ -411,7 +532,7 @@ properties: properties: port@0: - $ref: /schemas/graph.yaml#/properties/port + $ref: '#/$defs/hs-port' description: High Speed (HS) data bus. port@1: @@ -429,4 +550,3 @@ required: additionalProperties: true ... - -- 2.53.0 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on one HS data bus 2026-11-21 2:21 ` [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on " Qianhui Xu @ 2026-11-21 2:40 ` sashiko-bot 0 siblings, 0 replies; 14+ messages in thread From: sashiko-bot @ 2026-11-21 2:40 UTC (permalink / raw) To: Qianhui Xu, linux-usb; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] dt-bindings: usb: snps,dwc3: unconditional hs-port ref breaks non-mux port@0 - [Low] dt-bindings: usb: snps,dwc3: unaddressed maintainer feedback on schema style - [Low] dt-bindings: usb: snps,dwc3: requiring dr_mode overrides usb-drd.yaml default -- commit 3dfd09df6c148c2b5dd730fefdb79828ea834f98 Author: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> dt-bindings: usb: snps,dwc3: describe two connectors on one HS data bus This commit updates the Synopsys DWC3 common DT binding schema to describe two Type-C connectors sharing a High Speed data bus through a multiplexer, adding mux-controls and two endpoint nodes under port@0 with per-connector usb-role-switch and mux-state properties. > diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > index 6c0b8b6538246..57e3c1d9ed038 100644 > --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml > @@ -13,6 +13,89 @@ description: > Defines the properties of the DWC3 core as being embedded in either an > vendor-specific implementation or as a standalone component. > > +$defs: > + hs-endpoint: > + $ref: /schemas/graph.yaml#/$defs/endpoint-base > + unevaluatedProperties: false > + > + properties: > + usb-role-switch: > + type: boolean > + description: > + This connector endpoint has its own USB role switch. The switch is > + registered against this endpoint and looked up with > + usb_role_switch_at_endpoint(). > + > + mux-state: > + $ref: /schemas/types.yaml#/definitions/uint32 > + enum: [0, 1] > + description: > + State of the High Speed data bus multiplexer which connects this > + connector to the controller. The two connectors of one controller > + must use different states; the driver rejects a device tree where > + two connectors claim the same one. > + > + dependentRequired: > + usb-role-switch: [mux-state] > + mux-state: [usb-role-switch] > + > + hs-port: > + $ref: /schemas/graph.yaml#/$defs/port-base > + unevaluatedProperties: false [ ... ] > + endpoint@0: > + $ref: '#/$defs/hs-endpoint' > + required: [reg, usb-role-switch, mux-state] > + > + endpoint@1: > + $ref: '#/$defs/hs-endpoint' > + required: [reg, usb-role-switch, mux-state] [ ... ] > @@ -26,8 +109,46 @@ allOf: > $ref: usb.yaml# > else: > $ref: usb-xhci.yaml# > + - if: > + required: [mux-controls] > + then: > + # The mux path is only taken when the controller itself carries > + # "usb-role-switch": dwc3_get_dr_mode() forces dr_mode back to > + # peripheral, dwc3_get_extcon() skips the role-switch branch and > + # dwc3_drd_init() never sets the mux up without it. [Severity: Low] As reported by Rob Herring on v9 [1], should the local $defs be dropped in favor of defining the endpoint schema under patternProperties for '^endpoint@[01]$', and can the Linux driver implementation details (usb_role_switch_at_endpoint(), dwc3_get_dr_mode(), dwc3_get_extcon(), dwc3_drd_init(), and driver validation behavior) be removed from the binding? > + required: [dr_mode, ports, usb-role-switch] > + properties: > + dr_mode: > + const: otg [Severity: Low] Does listing dr_mode in required reject DWC3 nodes with mux-controls that omit dr_mode to rely on the default "otg" mode from usb-drd.yaml? In usb-drd.yaml (and dwc3_get_dr_mode() when usb-role-switch is present), dr_mode already defaults to "otg" when omitted. Keeping dr_mode: const: otg under properties while dropping dr_mode from required would still reject "host" or "peripheral" without forcing board DTs to specify dr_mode explicitly. > + ports: > + required: [port@0] > + properties: > + port@0: > + required: ['#address-cells', '#size-cells', > + endpoint@0, endpoint@1] > + else: > + # Without a mux the High Speed port keeps the generic graph port > + # binding, so single-endpoint controllers are described as before. > + # port@0 is referenced here rather than declared once in "ports" > + # because the graph port forbids properties it does not know > + # (unevaluatedProperties: false), which would reject the per-connector > + # "usb-role-switch" and "mux-state" of the dual-connector form. > + properties: > + ports: > + properties: > + port@0: > + $ref: /schemas/graph.yaml#/properties/port [ ... ] > @@ -411,7 +532,7 @@ properties: > > properties: > port@0: > - $ref: /schemas/graph.yaml#/properties/port > + $ref: '#/$defs/hs-port' > description: High Speed (HS) data bus. [Severity: Medium] Does applying $ref: '#/$defs/hs-port' unconditionally to port@0 cause a dtbs_check regression for single-connector DWC3 nodes without mux-controls? In JSON Schema, unevaluatedProperties: false inside hs-port only sees properties evaluated within hs-port itself, not properties evaluated by the allOf else branch ($ref: /schemas/graph.yaml#/properties/port). Because /schemas/graph.yaml#/$defs/port-base omits endpoint and hs-port only declares endpoint@0 and endpoint@1 (both requiring usb-role-switch and mux-state), non-mux DWC3 nodes using port@0/endpoint (such as arch/arm64/boot/dts/qcom/sm8250.dtsi) fail validation with endpoint rejected as unevaluated, while non-mux nodes using port@0/endpoint@0 fail because hs-port requires usb-role-switch and mux-state that /schemas/graph.yaml#/properties/port rejects. Should $ref: '#/$defs/hs-port' be applied to port@0 only in the mux-controls then branch of allOf (as described in the else branch comment), leaving top-level port@0 with only its description? [1] https://lore.kernel.org/r/20261118153457.GB1824160-robh@kernel.org -- Sashiko AI review · https://sashiko.dev/#/patchset/20261121022104.1507987-1-xiaqinghuan@mail.ustc.edu.cn?part=1 ^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v10 2/3] usb: roles: look up role switches attached to a graph endpoint 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on " Qianhui Xu @ 2026-11-21 2:21 ` Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2 siblings, 0 replies; 14+ messages in thread From: Qianhui Xu @ 2026-11-21 2:21 UTC (permalink / raw) To: Thinh.Nguyen, linux-usb Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Greg Kroah-Hartman, devicetree, linux-arm-msm, linux-kernel usb_role_switch_get() and fwnode_usb_role_switch_get() look for a switch on the caller's parent device node or through device_connection. That does not work when one controller exposes a role switch per connector endpoint: the switch is registered against the remote graph endpoint and the consumer, the connector, has no phandle to it. Add usb_role_switch_at_endpoint(), which walks the caller's OF graph endpoints and matches a switch registered on the remote endpoint, and try it after the existing parent lookup. A switch that is described by the graph but not registered yet comes back as -EPROBE_DEFER, as the rest of the role-switch API does, so the caller retries instead of concluding that the connector has no role switch at all. There is no behaviour change for existing users: if nothing is registered against an endpoint the lookup falls through to the previous paths. This is needed by DWC3 designs where two Type-C receptacles share one High Speed data bus. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- drivers/usb/roles/class.c | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index edec139b68..ee186d542a 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -157,6 +157,47 @@ usb_role_switch_is_parent(struct fwnode_handle *fwnode) return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER); } +/* + * Look up a role switch registered against the remote endpoint of one of the + * caller's OF graph endpoints. This is how a connector endpoint that shares a + * USB data bus with another connector (for example two Type-C receptacles on a + * single DWC3 controller, selected by a GPIO mux) finds its own role switch. + */ +static struct usb_role_switch * +usb_role_switch_at_endpoint(struct fwnode_handle *fwnode) +{ + struct fwnode_handle *ep, *remote, *parent; + struct usb_role_switch *sw; + bool available; + + fwnode_graph_for_each_endpoint(fwnode, ep) { + parent = fwnode_graph_get_remote_port_parent(ep); + if (!parent) + continue; + available = fwnode_device_is_available(parent); + fwnode_handle_put(parent); + if (!available) + continue; + + remote = fwnode_graph_get_remote_endpoint(ep); + if (!remote) + continue; + sw = usb_role_switch_match(remote, "usb-role-switch", NULL); + fwnode_handle_put(remote); + /* + * ERR_PTR(-EPROBE_DEFER) means the switch is described by the graph + * but not registered yet: pass it up so the caller defers instead of + * concluding that this connector has no role switch at all. + */ + if (sw) { + fwnode_handle_put(ep); + return sw; + } + } + + return NULL; +} + /** * usb_role_switch_get - Find USB role switch linked with the caller * @dev: The caller device @@ -169,6 +210,8 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev) struct usb_role_switch *sw; sw = usb_role_switch_is_parent(dev_fwnode(dev)); + if (!sw) + sw = usb_role_switch_at_endpoint(dev_fwnode(dev)); if (!sw) sw = device_connection_find_match(dev, "usb-role-switch", NULL, usb_role_switch_match); @@ -192,6 +235,8 @@ struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode) struct usb_role_switch *sw; sw = usb_role_switch_is_parent(fwnode); + if (!sw) + sw = usb_role_switch_at_endpoint(fwnode); if (!sw) sw = fwnode_connection_find_match(fwnode, "usb-role-switch", NULL, usb_role_switch_match); -- 2.53.0 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on " Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu @ 2026-11-21 2:21 ` Qianhui Xu 2026-11-21 2:33 ` sashiko-bot 2 siblings, 1 reply; 14+ messages in thread From: Qianhui Xu @ 2026-11-21 2:21 UTC (permalink / raw) To: Thinh.Nguyen, linux-usb Cc: Rob Herring, Krzysztof Kozlowski, Conor Dooley, Greg Kroah-Hartman, devicetree, linux-arm-msm, linux-kernel Two connectors can share one High Speed D+/D- pair when a multiplexer selects which receptacle drives the controller. Drive that multiplexer from the mode worker: "mux-controls" names a "gpio-mux"-style controller, and the endpoint of each connector carries the "mux-state" that reaches it. dwc3_role_mux_init() gets the controller with devm_mux_control_get() and requires both connectors to be described. A mux controller cannot be read back, so the boot-time connector cannot be detected: the initial mode request claims the mux for state 0 instead, which is the first mode request that reaches __dwc3_set_mode() after probe. Moving the data path to the other connector is a mode switch even when the role does not change, because the running host or gadget instance has to be torn down while its own data lines are still selected. mux_control_select() sleeps while waiting for the controller, and mux_control_deselect() may sleep as well. Both are only ever called from __dwc3_set_mode(), which runs as a work item on system_freezable_wq, and from dwc3_drd_exit() after the mode work has been drained; the role-switch set() callback keeps recording the request and never touches the mux directly. The mux stays selected between switches, since the controller always drives exactly one of the two connectors, and it is deselected when the driver is unbound. This does not change the arbitration, the publication ordering or the teardown rules introduced with the per-connector role switches. Signed-off-by: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> --- drivers/usb/dwc3/core.c | 116 ++++++++++++- drivers/usb/dwc3/core.h | 43 +++++ drivers/usb/dwc3/debugfs.c | 4 + drivers/usb/dwc3/drd.c | 323 ++++++++++++++++++++++++++++++++++++- 4 files changed, 483 insertions(+), 3 deletions(-) diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 372737f475..ce30979286 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -21,6 +21,7 @@ #include <linux/io.h> #include <linux/list.h> #include <linux/delay.h> +#include <linux/mux/consumer.h> #include <linux/dma-mapping.h> #include <linux/of.h> #include <linux/of_graph.h> @@ -165,7 +166,11 @@ EXPORT_SYMBOL_GPL(dwc3_set_prtcap); static void __dwc3_set_mode(struct work_struct *work) { struct dwc3 *dwc = work_to_dwc(work); + struct dwc3_role_mux *mux = dwc->role_mux; unsigned long flags; + bool pending, port_switch; + int mux_port = 0; + int active_port = -1; int ret; u32 reg; u32 desired_dr_role; @@ -174,6 +179,10 @@ static void __dwc3_set_mode(struct work_struct *work) mutex_lock(&dwc->mutex); spin_lock_irqsave(&dwc->lock, flags); desired_dr_role = dwc->desired_dr_role; + if (mux) { + mux_port = mux->desired_port; + active_port = mux->active_port; + } spin_unlock_irqrestore(&dwc->lock, flags); pm_runtime_get_sync(dwc->dev); @@ -184,7 +193,14 @@ static void __dwc3_set_mode(struct work_struct *work) if (!desired_dr_role) goto out; - if (desired_dr_role == dwc->current_dr_role) + /* + * Moving the shared data path to the other connector is a mode switch even + * when the role does not change: the running host or gadget instance has to + * be torn down while its own data lines are still selected. + */ + port_switch = mux && mux_port != active_port; + + if (desired_dr_role == dwc->current_dr_role && !port_switch) goto out; if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev) @@ -233,8 +249,60 @@ static void __dwc3_set_mode(struct work_struct *work) dwc3_writel(dwc, DWC3_GCTL, reg); } + /* + * The previous host or gadget instance has been torn down above, so the + * shared High Speed data path can now be moved to the requesting + * connector. A failure here must not abort the mode change half-way: + * the controller would be left with a torn-down role and a stale + * current_dr_role, which the next request would tear down a second time. + */ + if (port_switch) { + /* + * mux_control_select() sleeps while waiting for the controller, so + * the switch is only done here, in the mode worker. The mux is kept + * selected between switches: dwc3 is its only consumer and always + * drives one of the two connectors. + * + * Only deselect a mux that is actually selected: active_port is -1 + * before the first selection and after a failed one, and deselecting + * an unselected mux would release a semaphore that was never taken. + */ + if (active_port >= 0) + mux_control_deselect(mux->ctrl); + ret = mux_control_select(mux->ctrl, mux_port); + if (ret) + dev_err_ratelimited(dwc->dev, + "failed to select USB port %d: %d\n", + mux_port, ret); + + spin_lock_irqsave(&dwc->lock, flags); + if (ret) { + mux->ports[mux_port].errors++; + /* + * No port owns the mux any more: the previous one has been + * deselected above and the new one could not be selected. + * Keeping the old index would make the next request skip + * the select and drive a connector that is not connected. + */ + mux->active_port = -1; + } else { + mux->active_port = mux_port; + } + spin_unlock_irqrestore(&dwc->lock, flags); + } + spin_lock_irqsave(&dwc->lock, flags); + /* + * dwc3_set_prtcap() only clears GUSB3PIPECTL.SUSPENDENABLE and + * GUSB2PHYCFG.SUSPHY when PRTCAP actually changes, but a same-role port + * switch reinitialises the controller too and must start from the same + * state: the instance created below would otherwise inherit the SUSPHY + * setting of the one torn down above. + */ + if (port_switch) + dwc3_enable_susphy(dwc, false); + dwc3_set_prtcap(dwc, desired_dr_role, false); spin_unlock_irqrestore(&dwc->lock, flags); @@ -283,24 +351,68 @@ static void __dwc3_set_mode(struct work_struct *work) } out: + /* + * A request that arrived while this work was running did not re-queue it: + * queue_work() returns false for a work item that is already running. Pick + * such a request up here instead of waiting for the next connector event. + * The pair is compared with the snapshot taken above, so a data-path switch + * that failed is not retried in a loop. + */ + if (mux) { + spin_lock_irqsave(&dwc->lock, flags); + pending = dwc->desired_dr_role != desired_dr_role || + mux->desired_port != mux_port; + spin_unlock_irqrestore(&dwc->lock, flags); + if (pending) + queue_work(system_freezable_wq, &dwc->drd_work); + } + pm_runtime_put_autosuspend(dwc->dev); mutex_unlock(&dwc->mutex); } -void dwc3_set_mode(struct dwc3 *dwc, u32 mode) +static void dwc3_queue_mode(struct dwc3 *dwc, u32 mode, int port) { unsigned long flags; if (dwc->dr_mode != USB_DR_MODE_OTG) return; + /* + * The connector that owns the shared data path is published together with + * the mode request: __dwc3_set_mode() snapshots both under dwc->lock, and + * a torn pair would move the data path for the connector that asked last + * while bringing up the role requested by the one before it. + */ spin_lock_irqsave(&dwc->lock, flags); dwc->desired_dr_role = mode; + if (dwc->role_mux && port >= 0) + dwc->role_mux->desired_port = port; spin_unlock_irqrestore(&dwc->lock, flags); queue_work(system_freezable_wq, &dwc->drd_work); } +void dwc3_set_mode(struct dwc3 *dwc, u32 mode) +{ + dwc3_queue_mode(dwc, mode, -1); +} + +/** + * dwc3_set_mode_port - request a mode and the connector that owns the data path + * @dwc: pointer to our controller context structure + * @mode: mode to be requested + * @port: index of the connector that should drive the shared data path + * + * Same as dwc3_set_mode(), except that the data-path selection is published + * atomically with the mode request, so the mode worker never pairs the new + * connector with the previous role. + */ +void dwc3_set_mode_port(struct dwc3 *dwc, u32 mode, int port) +{ + dwc3_queue_mode(dwc, mode, port); +} + u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type) { struct dwc3 *dwc = dep->dwc; diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index f3dabb37f8..e6cf7b240b 100644 --- a/drivers/usb/dwc3/core.h +++ b/drivers/usb/dwc3/core.h @@ -1008,6 +1008,46 @@ struct dwc3_glue_ops { void (*pre_run_stop)(struct dwc3 *dwc, bool is_on); }; +/* Per-connector state of the optional shared-HS-data-bus mux. */ +struct dwc3_role_port { + struct dwc3 *dwc; + struct usb_role_switch *sw; + struct fwnode_handle *fwnode; + enum usb_role role; + unsigned int state; + /* request order, see dwc3_role_mux.seq */ + u64 seq; + /* data-path switch failures, for diagnostics only */ + unsigned int errors; +}; + +/** + * struct dwc3_role_mux - optional data-path mux between two connectors + * @ctrl: mux controller selecting which connector drives the shared HS data lines + * @lock: serialises role requests coming from both connectors + * @ready: both switches registered and the initial mode request issued + * @ports: per-connector state, indexed by the endpoint's "mux-state" + * @desired_port: index of the connector that currently wins + * @active_port: index of the connector currently driving the data lines + * @seq: monotonic request counter used to order pending requests + * + * Two connectors can share the High Speed data lines of one controller while + * keeping their Super Speed lanes separate. Each connector then needs its own + * role-switch input, and the shared data path has to be moved before the + * controller is reinitialised for the requested role. + */ +struct dwc3_role_mux { + struct mux_control *ctrl; + /* serialises role requests coming from both connectors */ + struct mutex lock; + /* false until both switches are registered and the mode was requested */ + bool ready; + struct dwc3_role_port ports[2]; + int desired_port; + int active_port; + u64 seq; +}; + /** * struct dwc3 - representation of our controller * @drd_work: workqueue used for role swapping @@ -1064,6 +1104,7 @@ struct dwc3_glue_ops { * - USBPHY_INTERFACE_MODE_UTMI * - USBPHY_INTERFACE_MODE_UTMIW * @role_sw: usb_role_switch handle + * @role_mux: optional shared data-path mux plus per-connector role switches * @role_switch_default_mode: default operation mode of controller while * usb role is USB_ROLE_NONE. * @usb_psy: pointer to power supply interface. @@ -1265,6 +1306,7 @@ struct dwc3 { struct notifier_block edev_nb; enum usb_phy_interface hsphy_mode; struct usb_role_switch *role_sw; + struct dwc3_role_mux *role_mux; enum usb_dr_mode role_switch_default_mode; struct power_supply *usb_psy; @@ -1614,6 +1656,7 @@ struct dwc3_gadget_ep_cmd_params { /* prototypes */ void dwc3_set_prtcap(struct dwc3 *dwc, u32 mode, bool ignore_susphy); void dwc3_set_mode(struct dwc3 *dwc, u32 mode); +void dwc3_set_mode_port(struct dwc3 *dwc, u32 mode, int port); u32 dwc3_core_fifo_space(struct dwc3_ep *dep, u8 type); #define DWC3_IP_IS(_ip) \ diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c index 01ebd73198..82e7a334b1 100644 --- a/drivers/usb/dwc3/debugfs.c +++ b/drivers/usb/dwc3/debugfs.c @@ -434,6 +434,10 @@ static ssize_t dwc3_mode_write(struct file *file, u32 mode = 0; char buf[32]; + /* The connector providers own both the role and the data-path selection. */ + if (dwc->role_mux) + return -EBUSY; + if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) return -EFAULT; diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index f3e37d3836..f14686dee0 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -8,6 +8,7 @@ */ #include <linux/extcon.h> +#include <linux/mux/consumer.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/property.h> @@ -501,10 +502,319 @@ static enum usb_role dwc3_usb_role_switch_get(struct usb_role_switch *sw) return role; } +/* + * Index of the connector that should own the shared data path: the oldest + * pending request wins, and the last selection is kept when neither connector + * requests a role. Requests are ordered by a monotonic counter rather than by + * a timestamp, so a wrapped counter cannot be mistaken for an old request. + */ +static int dwc3_role_mux_pick(struct dwc3_role_mux *mux) +{ + int sel = mux->desired_port; + int i; + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].role == USB_ROLE_NONE) + continue; + if (mux->ports[sel].role == USB_ROLE_NONE || + mux->ports[i].seq < mux->ports[sel].seq) + sel = i; + } + + return sel; +} + +static u32 dwc3_role_mux_mode(struct dwc3 *dwc, enum usb_role role) +{ + if (role == USB_ROLE_HOST) + return DWC3_GCTL_PRTCAP_HOST; + if (role == USB_ROLE_DEVICE) + return DWC3_GCTL_PRTCAP_DEVICE; + if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) + return DWC3_GCTL_PRTCAP_HOST; + + return DWC3_GCTL_PRTCAP_DEVICE; +} + +static int dwc3_mux_role_set(struct usb_role_switch *sw, enum usb_role role) +{ + struct dwc3_role_port *port = usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc = port->dwc; + struct dwc3_role_mux *mux = dwc->role_mux; + unsigned long flags; + enum usb_role selected_role; + int selected; + + if (role > USB_ROLE_DEVICE) + return -EINVAL; + + mutex_lock(&mux->lock); + + /* + * The switches are registered before the initial mode request is issued and + * before mux->ready is set, so a connector can find its switch while the + * mux is still being set up. Refusing the request keeps it from racing + * with the initial mode request and from queueing work that the error path + * would then have to undo. + */ + if (!mux->ready) { + mutex_unlock(&mux->lock); + return -ENODEV; + } + + spin_lock_irqsave(&dwc->lock, flags); + /* + * A role change between two non-NONE roles of the same connector must not + * give that connector a new request ticket: it would otherwise steal the + * data path from the connector that asked first. + */ + if (port->role == USB_ROLE_NONE && role != USB_ROLE_NONE) + port->seq = ++mux->seq; + port->role = role; + selected = dwc3_role_mux_pick(mux); + selected_role = mux->ports[selected].role; + spin_unlock_irqrestore(&dwc->lock, flags); + + /* + * Notify the glue layer before touching the controller, in the same order + * as the single-connector path. It may sleep (the Qualcomm glue calls + * pm_runtime_resume_and_get()), so it runs outside of dwc->lock - + * dwc3_set_mode() takes that lock itself and a recursive take deadlocks + * the controller on the very first role change. + * + * Pairing with the selection above is provided by mux->lock: a second + * connector blocks in mutex_lock() until this request has been issued. + * + * Same asynchronous semantics as the single-connector role switch: the + * data path and the controller are reprogrammed from __dwc3_set_mode(). + */ + dwc3_pre_set_role(dwc, selected_role); + /* + * The selected connector and the mode are published by one call, under + * dwc->lock, so __dwc3_set_mode() cannot observe the new connector together + * with the role of the previous request. Publishing happens after + * dwc3_pre_set_role() so the work cannot start before the glue layer has + * been notified. + */ + dwc3_set_mode_port(dwc, dwc3_role_mux_mode(dwc, selected_role), selected); + + mutex_unlock(&mux->lock); + + /* + * A data-path switch failure is reported through the console and counted + * per connector; it is not returned here. This call has already updated + * the request state, so a stale error from another connector must not be + * reported as the result of this (successful) request. + */ + return 0; +} + +static enum usb_role dwc3_mux_role_get(struct usb_role_switch *sw) +{ + struct dwc3_role_port *port = usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc = port->dwc; + unsigned long flags; + enum usb_role role; + + /* + * Report the role this connector itself requested, whether or not it owns + * the shared data path right now. Hiding a pending request behind + * active_port makes connector drivers read back USB_ROLE_NONE, so they + * never send the set_role(USB_ROLE_NONE) that would release it; the stale + * request then keeps winning dwc3_role_mux_pick() after the connector has + * been unplugged and starves the other one. + */ + spin_lock_irqsave(&dwc->lock, flags); + role = port->role; + spin_unlock_irqrestore(&dwc->lock, flags); + + return role; +} + +static void dwc3_role_mux_exit(struct dwc3 *dwc) +{ + struct dwc3_role_mux *mux = dwc->role_mux; + unsigned int i; + + if (!mux) + return; + + /* + * Refuse new requests first. dwc3_mux_role_set() takes this lock for its + * whole duration, so once it is held no request is in flight, and every + * later one returns before it can queue a mode request. The work itself + * is drained by the caller, which is the only way to keep a request from + * being queued after the drain. + */ + mutex_lock(&mux->lock); + mux->ready = false; + mutex_unlock(&mux->lock); + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].sw) + usb_role_switch_unregister(mux->ports[i].sw); + mux->ports[i].sw = NULL; + fwnode_handle_put(mux->ports[i].fwnode); + mux->ports[i].fwnode = NULL; + } +} + +/* + * Release the mux when this controller still owns it. devm_mux_control_get() + * only puts the controller on release and does not restore its idle state, so + * the mux has to be deselected explicitly; the caller must already have drained + * dwc->drd_work, otherwise a switch could select it again. + */ +static void dwc3_role_mux_deselect(struct dwc3 *dwc) +{ + struct dwc3_role_mux *mux = dwc->role_mux; + unsigned long flags; + int active_port; + + if (!mux) + return; + + spin_lock_irqsave(&dwc->lock, flags); + active_port = mux->active_port; + mux->active_port = -1; + spin_unlock_irqrestore(&dwc->lock, flags); + + if (active_port >= 0) + mux_control_deselect(mux->ctrl); +} + +/* Returns 1 when the optional two-connector mux has been set up. */ +static int dwc3_role_mux_init(struct dwc3 *dwc, u32 mode) +{ + struct usb_role_switch_desc desc = {}; + struct dwc3_role_mux *mux; + struct fwnode_handle *ep; + char name[64]; + u32 state; + int ret, i; + + if (!fwnode_property_present(dev_fwnode(dwc->dev), "mux-controls")) + return 0; + + mux = devm_kzalloc(dwc->dev, sizeof(*mux), GFP_KERNEL); + if (!mux) + return -ENOMEM; + + mux->ctrl = devm_mux_control_get(dwc->dev, NULL); + if (IS_ERR(mux->ctrl)) + return PTR_ERR(mux->ctrl); + + /* + * Both connectors must be addressable states of the mux controller. + */ + if (mux_control_states(mux->ctrl) < ARRAY_SIZE(mux->ports)) + return -EINVAL; + + /* + * The current position of a mux controller cannot be read back, so the + * boot-time connector cannot be detected here. The first mode request + * selects state 0 instead (the mode worker sees active_port == -1 and + * claims the mux for endpoint 0). + */ + mux->desired_port = 0; + mux->active_port = -1; + mutex_init(&mux->lock); + + /* + * Publish the mux before the first endpoint reference is taken: + * dwc3_role_mux_exit() is a no-op while dwc->role_mux is NULL, so every + * error path below would otherwise leak the fwnode references already + * acquired. + */ + dwc->role_mux = mux; + + fwnode_graph_for_each_endpoint(dev_fwnode(dwc->dev), ep) { + if (!fwnode_property_present(ep, "usb-role-switch")) + continue; + + ret = fwnode_property_read_u32(ep, "mux-state", &state); + if (ret || state >= ARRAY_SIZE(mux->ports)) { + ret = -EINVAL; + goto err_put_ep; + } + if (mux->ports[state].fwnode) { + ret = -EINVAL; + goto err_put_ep; + } + + mux->ports[state].fwnode = fwnode_handle_get(ep); + mux->ports[state].state = state; + mux->ports[state].dwc = dwc; + } + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + if (!mux->ports[i].fwnode) { + ret = -EINVAL; + goto err; + } + } + + /* + * Registering the switches makes them reachable from the connector + * drivers, so the initial mode request has to be issued inside the same + * critical section: a set() arriving in between would be silently + * overwritten by it while port->role keeps the requested value. + * dwc3_mux_role_set() serialises on the same lock. + */ + mutex_lock(&mux->lock); + + for (i = 0; i < ARRAY_SIZE(mux->ports); i++) { + snprintf(name, sizeof(name), "%s-port%u", dev_name(dwc->dev), i); + desc.name = name; + desc.fwnode = mux->ports[i].fwnode; + desc.set = dwc3_mux_role_set; + desc.get = dwc3_mux_role_get; + desc.driver_data = &mux->ports[i]; + mux->ports[i].sw = usb_role_switch_register(dwc->dev, &desc); + if (IS_ERR(mux->ports[i].sw)) { + ret = PTR_ERR(mux->ports[i].sw); + mux->ports[i].sw = NULL; + mutex_unlock(&mux->lock); + goto err; + } + } + + /* + * The controller is only started in its default role once both switches + * are registered: before that a connector driver cannot request a role, + * so nothing can race with this initial mode request. + */ + dwc3_set_mode(dwc, mode); + mux->ready = true; + + mutex_unlock(&mux->lock); + + return 1; + +err: + /* + * Unregister the switches before draining the work: dwc3_role_mux_exit() + * refuses every further request, and a request that was already past that + * check has queued its work by the time the lock is released, so the drain + * below cannot miss it. The reverse order would let a blocked request + * queue the work after the drain and run it on a half-torn-down mux. + * dwc->role_mux is left in place: it is devm-managed and freeing it here + * would only turn a late request into a NULL dereference. + */ + dwc3_role_mux_exit(dwc); + cancel_work_sync(&dwc->drd_work); + dwc3_role_mux_deselect(dwc); + return ret; +err_put_ep: + fwnode_handle_put(ep); + goto err; +} + static int dwc3_setup_role_switch(struct dwc3 *dwc) { struct usb_role_switch_desc dwc3_role_switch = {NULL}; u32 mode; + int ret; dwc->role_switch_default_mode = usb_get_role_switch_default_mode(dwc->dev); if (dwc->role_switch_default_mode == USB_DR_MODE_HOST) { @@ -513,6 +823,11 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) dwc->role_switch_default_mode = USB_DR_MODE_PERIPHERAL; mode = DWC3_GCTL_PRTCAP_DEVICE; } + + ret = dwc3_role_mux_init(dwc, mode); + if (ret) + return ret < 0 ? ret : 0; + dwc3_set_mode(dwc, mode); dwc3_role_switch.fwnode = dev_fwnode(dwc->dev); @@ -526,7 +841,7 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) if (dwc->dev->of_node) { /* populate connector entry */ - int ret = devm_of_platform_populate(dwc->dev); + ret = devm_of_platform_populate(dwc->dev); if (ret) { usb_role_switch_unregister(dwc->role_sw); @@ -541,6 +856,8 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) #else #define ROLE_SWITCH 0 #define dwc3_setup_role_switch(x) 0 +static inline void dwc3_role_mux_exit(struct dwc3 *dwc) {} +static inline void dwc3_role_mux_deselect(struct dwc3 *dwc) {} #endif int dwc3_drd_init(struct dwc3 *dwc) @@ -597,6 +914,7 @@ void dwc3_drd_exit(struct dwc3 *dwc) { unsigned long flags; + dwc3_role_mux_exit(dwc); if (dwc->role_sw) usb_role_switch_unregister(dwc->role_sw); @@ -606,6 +924,9 @@ void dwc3_drd_exit(struct dwc3 *dwc) cancel_work_sync(&dwc->drd_work); + /* The mode worker is drained: no switch can select the mux any more. */ + dwc3_role_mux_deselect(dwc); + /* debug user might have changed role, clean based on current role */ switch (dwc->current_dr_role) { case DWC3_GCTL_PRTCAP_HOST: -- 2.53.0 ^ permalink raw reply related [flat|nested] 14+ messages in thread
* Re: [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller 2026-11-21 2:21 ` [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu @ 2026-11-21 2:33 ` sashiko-bot 0 siblings, 0 replies; 14+ messages in thread From: sashiko-bot @ 2026-11-21 2:33 UTC (permalink / raw) To: Qianhui Xu, linux-usb; +Cc: devicetree, robh, conor+dt Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] usb: dwc3: premature mux deselection before role teardown in dwc3_drd_exit() -- commit c2bf2129dd204f48fafe772bcfa69b9f05541e6c Author: Qianhui Xu <xiaqinghuan@mail.ustc.edu.cn> usb: dwc3: select the shared HS data bus with a mux controller This commit adds support for switching a shared High Speed D+/D- data bus between two connectors using a mux controller and per-connector USB role switches. > diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c > index f3e37d383627a..f14686dee095e 100644 > --- a/drivers/usb/dwc3/drd.c > +++ b/drivers/usb/dwc3/drd.c [ ... ] > @@ -597,6 +914,7 @@ void dwc3_drd_exit(struct dwc3 *dwc) > { > unsigned long flags; > > + dwc3_role_mux_exit(dwc); > if (dwc->role_sw) > usb_role_switch_unregister(dwc->role_sw); > > @@ -606,6 +924,9 @@ void dwc3_drd_exit(struct dwc3 *dwc) > > cancel_work_sync(&dwc->drd_work); > > + /* The mode worker is drained: no switch can select the mux any more. */ > + dwc3_role_mux_deselect(dwc); > + > /* debug user might have changed role, clean based on current role */ > switch (dwc->current_dr_role) { > case DWC3_GCTL_PRTCAP_HOST: [Severity: Medium] Should dwc3_role_mux_deselect() be called after the switch (dwc->current_dr_role) block tears down the active host or gadget instance? When the mux controller has an idle state other than MUX_IDLE_AS_IS and the active connector is not the idle port, mux_control_deselect() switches the High Speed data lines back to the idle state while the host or gadget instance is still running. In __dwc3_set_mode(), dwc3_host_exit() or dwc3_gadget_exit() runs before mux_control_deselect() so the active role is torn down while its own data lines are still selected. -- Sashiko AI review · https://sashiko.dev/#/patchset/20261121022104.1507987-1-xiaqinghuan@mail.ustc.edu.cn?part=3 ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2026-11-21 2:40 UTC | newest] Thread overview: 14+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-17 5:21 [PATCH v9 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 1/3] dt-bindings: usb: snps,dwc3: two connectors are two endpoints of one port Qianhui Xu 2026-11-17 5:40 ` sashiko-bot 2026-11-18 15:34 ` Rob Herring 2026-11-17 5:21 ` [PATCH v9 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu 2026-11-17 5:21 ` [PATCH v9 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2026-11-17 5:37 ` sashiko-bot 2026-11-18 15:27 ` Rob Herring 2026-11-21 2:21 ` [PATCH v10 0/3] usb: dwc3: support two connectors sharing one HS data bus Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on " Qianhui Xu 2026-11-21 2:40 ` sashiko-bot 2026-11-21 2:21 ` [PATCH v10 2/3] usb: roles: look up role switches attached to a graph endpoint Qianhui Xu 2026-11-21 2:21 ` [PATCH v10 3/3] usb: dwc3: select the shared HS data bus with a mux controller Qianhui Xu 2026-11-21 2:33 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox