From nobody Tue Nov 24 12:36:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.38.64.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795267241; b=uCuMFngAlyqOvbUjgiBSvSH3kSDoNrD3y8k0aBjrKXqujVGzHSJM/1mrXOovH8RTtGW2 2VjgfucIpRcyU5yYJc8fxVZ/HE7JExfdxCea/CwwwoxVAl73Cqpo9gbuv3vivGaSgiMCs iI2S2zz5zFJwrIvn/z3jbbVBuRVFGE7lL4zEuh4t3dIXSVubjMFvATwBSgAVOueAJ/XpA SDC8QD+sipldkNsYjQPSvMl6W995CpZBpOpAfA5Wgb6xsDRGneTG1cf3LeRjH8uT3uLWj 75QkH+c9JYVNNt2DQHyvb9jldyTSsJRWomvEcDRW/B5oMdwfuCkSTq3D27A2xKpnNiQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795267241; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=4lLIbWKR0g2AGGIyo3/1zIWFjXUvApnp2wJj76lYKIM=; b=RrEVh34SWivBlDsLn7dyELyxjGxr6bUF/OBATu6GpnEGkw0qd88gE66/LQRhYSQmhDOq 2aGFOUu2/bQ0RUZzichXsSXFyrlSpE1m4sX4KLioHe/XnrMEf3yRIb7kosRmm2yGf7GIO lUMePKOehtTLPJogZZILdYiZN50EVJrXflBJWCqbFm37dWihvdKWgN3doczAMFNb5SkLn WQa+IfyDgVSceS9wadDV0CIEtOHLQQlmnwGlDuzrjYfM0lyMZSBoDVSj0LWfV+aKHOgph BHfFXHQx5bgz2FPx23uSwcCQu92g28LmOEywp+JDALDVBnxtGOMB5CXpKQnZbYhdYkw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mail.ustc.edu.cn; spf=pass smtp.mailfrom=mail.ustc.edu.cn; dkim=pass header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b=P3HUQ+KU; arc=none smtp.remote-ip=202.38.64.46 Received: from ustc.edu.cn (smtp.ustc.edu.cn [202.38.64.46]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 914C1606A337; Sat, 21 Nov 2026 13:20:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 914C1606A337 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b=P3HUQ+KU DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ustc.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=4lLIbWKR0g2AGGIyo3/1zIWFjXUvApnp2w Jj76lYKIM=; b=P3HUQ+KUYclzOLduPCTM2KRCfw4uoHevsLvc8h9M/QkDZzheGt OCHGN4z6VUoaJNKeCkpv5IAvmAWEMOp3TjIUwm0udzPkV7e4eDoweMzIu405HiB6 VIU/MO0DxYi0md2knwsW/4wDd9mln7m+0P6JsI8FAbXLf/YL7tl5pUoEE= Received: from xuqianhui.ustc.edu.cn (unknown [114.214.187.147]) by mailimap2024 (Coremail) with SMTP id 3pYKCgCnKT6dmgFr9DfkAA--.3095S3; Sat, 21 Nov 2026 21:20:31 +0800 (CST) From: Qianhui Xu To: Thinh Nguyen , linux-usb@vger.kernel.org Cc: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Greg Kroah-Hartman , devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v11 1/3] dt-bindings: usb: snps,dwc3: describe two connectors on one HS data bus Date: Sat, 21 Nov 2026 21:20:27 +0800 Message-ID: <20261121132029.2379449-2-xiaqinghuan@mail.ustc.edu.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> References: <20261121022104.1507987-1-xiaqinghuan@mail.ustc.edu.cn> <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: 3pYKCgCnKT6dmgFr9DfkAA--.3095S3 X-Coremail-Antispam: 1UD129KBjvJXoWxtFyrXF4xCw1rtw1rAr1fWFg_yoWxGw1DpF ZxtFs7tF4vgryfZ3s2y3W8Cwna9F4kJrnrKryDGw1UZa13WryrXa13Kw1FgFyUCF92va4a 934DW3W8X3ZrCFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPYb7Iv0xC_Kw4lb4IE77IF4wAFF20E14v26ryj6rWUM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI 8067AKxVWUGwA2048vs2IY020Ec7CjxVAFwI0_JFI_Gr1l8cAvFVAK0II2c7xJM28CjxkF 64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcV CY1x0267AKxVW8JVWxJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY 1x0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4 xG6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCa FVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI48JMxkF7I0En4kS14v26r1q6r43MxkIecxEwV AFwVW5GwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwC20s026c02F40E 14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAFwI0_Jw0_GFylIx kGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjxv20xvEc7CjxVAF wI0_Gr0_Cr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r1j6r 4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07bzb1nU UUUU= X-CM-SenderInfo: 50ld1x5qjk3t3q6ptxnoox23vfohv3gofq/1tbiARESB2sAn9smxQBLsC Content-Type: text/plain; charset="utf-8" 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 and the device tree has to say which role-switch input and which mux state belongs to each of them. A mux'ed data stream on one bus is an endpoint of that port in the OF graph, not a port of its own, so the two connectors become endpoint@0 and endpoint@1 of the High Speed port, next to the Super Speed port@1. Each endpoint carries "usb-role-switch" and the "mux-state" that reaches it, and the multiplexer is referenced with "mux-controls", so an existing "gpio-mux" controller is used as is instead of a private property. The High Speed port definition stays optional: a single-connector controller keeps describing one plain "endpoint", the two endpoints are required only when "mux-controls" is present, and the two states must be opposite so that the two connectors cannot both claim the same data path. *** 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 --- Changes since v10: - the version posted as v10 still applied the two-connector definition to every user of "port@0" through an unconditional "$ref", and required "dr_mode" from a mux user. Both are gone: "port@0" keeps the generic graph port at the top level, and the two endpoints, their "usb-role-switch"/"mux-state" and the address/size cells are required by the "mux-controls" branch only, so existing single-connector controllers such as qcom/sm8250.dtsi validate as before. - the High Speed endpoint schema is inlined into "port@0" through patternProperties; the local "$defs" section and the "$ref" to it are dropped, as Rob asked for in v9. - keep driver behaviour out of the binding: the "mux-controls" branch only states what the device tree must contain, without naming driver functions. - do not list "dr_mode" in "required": omitting it already means "otg" (usb-drd.yaml, and dwc3_get_dr_mode() for a role-switch controller), so requiring it rejected trees that rely on the default. It stays "const: otg" when present, so "host" and "peripheral" are still rejected. - require both "usb-role-switch" and "mux-state" on both endpoints, so a tree cannot satisfy one branch of the oneOf while omitting the properties the driver needs. - describe the binding relative to the baseline tree instead of as a diff against v8; the v8 changelog referred to a "port@2" and a "port-select-gpios" property that never existed upstream. .../devicetree/bindings/usb/snps,dwc3-common.yaml | 99 ++++++++++++++++= ++++ 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml b/= Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml index 6c0b8b653824..7a9cb5a115a6 100644 --- a/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml +++ b/Documentation/devicetree/bindings/usb/snps,dwc3-common.yaml @@ -26,8 +26,49 @@ allOf: $ref: usb.yaml# else: $ref: usb-xhci.yaml# + - if: + required: [mux-controls] + then: + # A multiplexed High Speed bus only makes sense when the controller = is + # dual-role and both connectors can switch their role independently. + required: [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] + patternProperties: + '^endpoint@[01]$': + required: [usb-role-switch, mux-state] + else: + # Without a mux the High Speed port keeps the generic graph port + # binding, so single-connector controllers are described as before a= nd + # the per-connector properties have no meaning. + properties: + mux-controls: false + ports: + properties: + port@0: + patternProperties: + '^endpoint@[01]$': + properties: + usb-role-switch: false + mux-state: false =20 properties: + mux-controls: + $ref: /schemas/types.yaml#/definitions/phandle-array + 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,9 +452,65 @@ properties: =20 properties: port@0: - $ref: /schemas/graph.yaml#/properties/port + # The High Speed port carries the controller's HS data stream. Two + # receptacles sharing one HS D+/D- pair are two mux'ed data stream= s of + # that one stream, so they are two endpoints of this port and not = two + # ports. A single-connector controller describes the plain "endpo= int" + # of the graph binding and is unaffected. + $ref: /schemas/graph.yaml#/$defs/port-base + unevaluatedProperties: false description: High Speed (HS) data bus. =20 + patternProperties: + '^endpoint@[01]$': + $ref: /schemas/graph.yaml#/$defs/endpoint-base + unevaluatedProperties: false + + properties: + usb-role-switch: + type: boolean + description: + This connector has its own USB role switch, registered + against this endpoint. + + mux-state: + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [0, 1] + description: + State of the High Speed data bus multiplexer which conne= cts + this connector to the controller. The two connectors of= one + controller must use different states. + + required: [reg] + + dependentRequired: + usb-role-switch: [mux-state] + mux-state: [usb-role-switch] + + if: + required: [endpoint@0, endpoint@1] + then: + # The two connectors must claim opposite mux states. + oneOf: + - properties: + endpoint@0: + properties: + mux-state: + const: 0 + endpoint@1: + properties: + mux-state: + const: 1 + - properties: + endpoint@0: + properties: + mux-state: + const: 1 + endpoint@1: + properties: + mux-state: + const: 0 + port@1: $ref: /schemas/graph.yaml#/properties/port description: Super Speed (SS) data bus. From nobody Tue Nov 24 12:36:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.38.64.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795267241; b=qqVTkeM9C5fPWaJDRFlC4U+ow98/o69OzhaWnW3GqzgUqUplbcCTgC1juH6sbdyeZGBk W1zPgZ+3Wyip9wXAZxfPC/4A1pI7SmcS0B8FtnwHCc61DuG+v3a56lo9eBaNi5I4IpqMX 5rlEtbJFCyBT7shffzzGYrelZ7ztSfzyxeNG/SSLTaXNSDQCgIhXO+DrC98+mIMsWAWYq mszVGCVYUjAiKWq6NEdjSQUHec+TJFlQ+/2AgHgHD6gnjSkdkUHGcy1Wyp2sfbaKGuIhj rMEmPk5IXsR4gjkcjV5aL5p/C4hl2nouBUJ/9ZUl/Fp4jCQvdfoYs3kWXU7ZM2SuBEQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795267241; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=D/C5WVldHqbUUcW5BkEw06TdZ/AWEE83/rEG7g3uqtM=; b=sGHzXH8CQEbwX5mrmvoFvW0odkI1wStWtcuPRvAbkO0LOu7SSirZAusGoyJ2a2Byk8z7 rO8JKEsEXXwAvlzRliWneaKsbrxBhoD0Y3U/pvTD4KIraPnyNZG9xPc1DsVq0OqXhqgdt ZwUP13ddxtKwqVq3NjNQ5+sDhkWPsD/qefh8eCbgggaXnG4E9gALQcyxDFnfrS2F2Vrvm 5i5ZMoYh2Pt6kHHThIrtS/IOMUDqipnnGIHstCW1OIt9xszdhUPX3AuJUdTiGcXzAWVt5 dvlOmzHsD+bMuQxCVlXR94W9KnXDPndpcIFWGIrpOZlw2p1K9GqdCiAGm2vU/7qaxaA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mail.ustc.edu.cn; spf=pass smtp.mailfrom=mail.ustc.edu.cn; dkim=pass header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b=gSmusYx9; arc=none smtp.remote-ip=202.38.64.46 Received: from ustc.edu.cn (smtp.ustc.edu.cn [202.38.64.46]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 914CF606A338; Sat, 21 Nov 2026 13:20:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 914CF606A338 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b=gSmusYx9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ustc.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=D/C5WVldHqbUUcW5BkEw06TdZ/AWEE83/r EG7g3uqtM=; b=gSmusYx9zsTJlugFuiBYrdFTgbx+lf8uaLsQcHVJsdGGMrgz7s FqzcE3KSfCT42zzLJWZjO3qW9EKwXuWq7UjxGXL/ciJLo/4SqBhYwl0jnHXZBHcu 9A5+nO44P5kuc/KIOBZn3P73RmWA1nhJl5+A7chnhIM0NpovqSWDu5SEc= Received: from xuqianhui.ustc.edu.cn (unknown [114.214.187.147]) by mailimap2024 (Coremail) with SMTP id 3pYKCgCnKT6dmgFr9DfkAA--.3095S4; Sat, 21 Nov 2026 21:20:32 +0800 (CST) From: Qianhui Xu To: Thinh Nguyen , linux-usb@vger.kernel.org Cc: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Greg Kroah-Hartman , devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v11 2/3] usb: roles: look up role switches attached to a graph endpoint Date: Sat, 21 Nov 2026 21:20:28 +0800 Message-ID: <20261121132029.2379449-3-xiaqinghuan@mail.ustc.edu.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> References: <20261121022104.1507987-1-xiaqinghuan@mail.ustc.edu.cn> <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: 3pYKCgCnKT6dmgFr9DfkAA--.3095S4 X-Coremail-Antispam: 1UD129KBjvJXoWxJF4fZFy3ur1DCryDZr1kXwb_yoWrJw4kpr y5KF4Fqr4DJFya9w4fKa1DAFWrur4fGFWUJF4Iqw1fC3ZaqrWfJr18ta4ayr15CanFgw1Y v348A3409a4DA3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUPFb7Iv0xC_tr1lb4IE77IF4wAFF20E14v26rWj6s0DM7CY07I2 0VC2zVCF04k26cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28IrcIa0xkI8VA2jI 8067AKxVWUXwA2048vs2IY020Ec7CjxVAFwI0_Gr0_Xr1l8cAvFVAK0II2c7xJM28CjxkF 64kEwVA0rcxSw2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcV CY1x0267AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVW0oVCq3wA2z4x0Y4vEx4A2jsIE c7CjxVAFwI0_GcCE3s1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqx4xG64xvF2IEw4CE5I 8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Jr0_Jr4lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCF s4IE7xkEbVWUJVW8JwACjcxG0xvY0x0EwIxGrwCY1x0262kKe7AKxVWUtVW8ZwCY02Avz4 vE14v_Xr4l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWUJV W8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUslks DUUUU X-CM-SenderInfo: 50ld1x5qjk3t3q6ptxnoox23vfohv3gofq/1tbiARESB2sAn9smxQBMsF Content-Type: text/plain; charset="utf-8" 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. *** 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 --- 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); } =20 +/* + * 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 sha= res 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 swit= ch. + */ +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 =3D fwnode_graph_get_remote_port_parent(ep); + if (!parent) + continue; + available =3D fwnode_device_is_available(parent); + fwnode_handle_put(parent); + if (!available) + continue; + + remote =3D fwnode_graph_get_remote_endpoint(ep); + if (!remote) + continue; + sw =3D 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 devi= ce *dev) struct usb_role_switch *sw; =20 sw =3D usb_role_switch_is_parent(dev_fwnode(dev)); + if (!sw) + sw =3D usb_role_switch_at_endpoint(dev_fwnode(dev)); if (!sw) sw =3D 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(stru= ct fwnode_handle *fwnode) struct usb_role_switch *sw; =20 sw =3D usb_role_switch_is_parent(fwnode); + if (!sw) + sw =3D usb_role_switch_at_endpoint(fwnode); if (!sw) sw =3D fwnode_connection_find_match(fwnode, "usb-role-switch", NULL, usb_role_switch_match); --=20 2.53.0 From nobody Tue Nov 24 12:36:13 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.38.64.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795267241; b=wzU5UCVTyzKWx3A2eS/4qzS0IyKXPx7bnCR3qvMBkmrJmZJjGA3jmVBBc6neu229yI5q g4UmTz3DcrcCf20xXrKcd0ix7VU0/KUVDekSQEJVO/Izai1rwcS/w6QWIlsPDWXNr0bRh k6zHltqG8QSjgAH1gDcF+yGOUSxc3X+D5kHMiDy/AWUxL/zlNcdznj3J9U0E7x5zzs/n8 OWjh7Uyjt93tIgNVUk2dcjvniK729Dm04HG0m8OBptn95vB5XM8/VOitmT8VoO8vJJBLf tOTtRhYw2T+cdZaM6NkndHxM9Rm/KMpYZZUmXaOXrpnBZFraYOxYBV9MoZB2hVgDZgw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795267241; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-CM-TRANSID:X-Coremail-Antispam: X-CM-SenderInfo; bh=TKFNmcnMSZ96ipJtSqqM6wzSusZxu9YRub7ESWPuOXY=; b=g7sxx9bEGM6Txkg95XBgxXmyxBz9uzclI4LIgzEeX/7MrisKbEy+2BNJvQkwHBpLjcjI o4p9x5cdNQ1ddgby/UVDaLhcaf+Jy1WOWzlX3eiOATxefLsYpceo1ap0GjRNXyu7qr/dL NZDtqnVhckyUAUkitqEBveqdFarU4ma9z8FYDGCalt5sp4XhkkzsttO7bDRk+gPO4J+VB Ritoe9l0WjRXhezbSbL2Ccb/nxfyPhBNdbBuqx8p74+8gvhnec8I4A9q6YhdkhtCdIbVO TVsYaATzdLfUaTzvyqFz91U7n06MVhgkgIkngf0SRpmlqM++SYufcMis+vD5qDYnjfw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mail.ustc.edu.cn; spf=pass smtp.mailfrom=mail.ustc.edu.cn; dkim=pass header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b="WU4K+9/m"; arc=none smtp.remote-ip=202.38.64.46 Received: from ustc.edu.cn (smtp.ustc.edu.cn [202.38.64.46]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9149C606A332; Sat, 21 Nov 2026 13:20:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9149C606A332 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mail.ustc.edu.cn Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mail.ustc.edu.cn header.i=@mail.ustc.edu.cn header.a=rsa-sha256 header.s=dkim header.b=WU4K+9/m DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mail.ustc.edu.cn; s=dkim; h=Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=TKFNmcnMSZ96ipJtSqqM6wzSusZxu9YRub 7ESWPuOXY=; b=WU4K+9/mRAhtymzkU5zNHAxngtbIICpQaqOgqLeKg2PDH7fVUr Sjx3uyaoYekHvtNGEKd7pK5Km/xYVxe39qvh7meRLHbw3qwQGPFDU/SgXlxFmCWF V6zo+gJ3CNnPYa2bhbvVTQcb7WpwR4hGrLj48OFaAMkLZhTTqR5OF6bOI= Received: from xuqianhui.ustc.edu.cn (unknown [114.214.187.147]) by mailimap2024 (Coremail) with SMTP id 3pYKCgCnKT6dmgFr9DfkAA--.3095S5; Sat, 21 Nov 2026 21:20:32 +0800 (CST) From: Qianhui Xu To: Thinh Nguyen , linux-usb@vger.kernel.org Cc: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Greg Kroah-Hartman , devicetree@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v11 3/3] usb: dwc3: select the shared HS data bus with a mux controller Date: Sat, 21 Nov 2026 21:20:29 +0800 Message-ID: <20261121132029.2379449-4-xiaqinghuan@mail.ustc.edu.cn> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> References: <20261121022104.1507987-1-xiaqinghuan@mail.ustc.edu.cn> <20261121132029.2379449-1-xiaqinghuan@mail.ustc.edu.cn> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: 3pYKCgCnKT6dmgFr9DfkAA--.3095S5 X-Coremail-Antispam: 1UD129KBjvAXoWfZF45tr4xZrWrJryxWr4DArb_yoW8Kw1fCo WfX3ykZw48KryrWrykCFn7Jry8Za92krW7Ar1rZrsrZF18XrWFg3srGF4jqFW3JF4avr15 XayxAr4FgFWIyr95n29KB7ZKAUJUUUU8529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UjIYCTnIWjp_UUUO17k0a2IF6w4kM7kC6x804xWl14x267AKxVWrJVCq3wAFc2x0 x2IEx4CE42xK8VAvwI8IcIk0rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2048vs2IY020E87 I2jVAFwI0_JrWl82xGYIkIc2x26xkF7I0E14v26ryj6s0DM28lY4IEw2IIxxk0rwA2F7IY 1VAKz4vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20x vEc7CjxVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv 6xkF7I0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c 02F40Ex7xfMcIj6xIIjxv20xvE14v26r1j6r18McIj6I8E87Iv67AKxVWUJVW8JwAm72CE 4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lc7CjxVAaw2AFwI0_Jw0_GFylc2xSY4 AK67AK6ry8MxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMI8I3I0E5I8C rVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8Zw CIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1j6r1xMIIF0xvE2Ix0cI8IcVCY1x02 67AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Jr 0_Gr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU5Gt C3UUUUU== X-CM-SenderInfo: 50ld1x5qjk3t3q6ptxnoox23vfohv3gofq/1tbiARESB2sAn9smxQBNsE Content-Type: text/plain; charset="utf-8" Two connectors can share one High Speed D+/D- pair only if a multiplexer selects which receptacle drives the controller, and that selection has to follow the USB mode: leaving it to a board level GPIO means the controller can end up driving the data lines of the connector that is not in use, and a host or gadget instance cannot be torn down while its own lines are still selected. A mux controller cannot be read back, so the connector in use at boot cannot be detected: the first mode request claims the mux for state 0 instead. A move of the data path to the other connector is a mode switch even when the role does not change, which is why it goes through the same worker. mux_control_select() sleeps while waiting for the controller and mux_control_deselect() may sleep as well, so both are only called from __dwc3_set_mode() 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. The mux stays selected between switches, since the controller always drives exactly one of the two connectors, and it is deselected on unbind. The arbitration, publication ordering and teardown rules of the per-connector role switches are unchanged. *** 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 --- Changes since v10: - deselect the mux in dwc3_drd_exit() after the active role has been torn down instead of before: mux_control_deselect() switches the shared High Speed lines back to the mux' idle state, so deselecting first cuts the data path off underneath a host or gadget instance that is still running. This is the order __dwc3_set_mode() already used: role exit, then the data path switch. --- drivers/usb/dwc3/core.c | 116 +++++++++++++++- drivers/usb/dwc3/core.h | 43 ++++++ drivers/usb/dwc3/debugfs.c | 4 + drivers/usb/dwc3/drd.c | 329 ++++++++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 489 insertions(+), 3 deletions(-) diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c index 372737f4750b..ce309792864b 100644 --- a/drivers/usb/dwc3/core.c +++ b/drivers/usb/dwc3/core.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -165,7 +166,11 @@ EXPORT_SYMBOL_GPL(dwc3_set_prtcap); static void __dwc3_set_mode(struct work_struct *work) { struct dwc3 *dwc =3D work_to_dwc(work); + struct dwc3_role_mux *mux =3D dwc->role_mux; unsigned long flags; + bool pending, port_switch; + int mux_port =3D 0; + int active_port =3D -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 =3D dwc->desired_dr_role; + if (mux) { + mux_port =3D mux->desired_port; + active_port =3D mux->active_port; + } spin_unlock_irqrestore(&dwc->lock, flags); =20 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; =20 - if (desired_dr_role =3D=3D dwc->current_dr_role) + /* + * Moving the shared data path to the other connector is a mode switch ev= en + * 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 =3D mux && mux_port !=3D active_port; + + if (desired_dr_role =3D=3D dwc->current_dr_role && !port_switch) goto out; =20 if (desired_dr_role =3D=3D 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); } =20 + /* + * 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 >=3D 0) + mux_control_deselect(mux->ctrl); + ret =3D 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 =3D -1; + } else { + mux->active_port =3D mux_port; + } + spin_unlock_irqrestore(&dwc->lock, flags); + } + spin_lock_irqsave(&dwc->lock, flags); =20 + /* + * 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); =20 spin_unlock_irqrestore(&dwc->lock, flags); @@ -283,24 +351,68 @@ static void __dwc3_set_mode(struct work_struct *work) } =20 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. P= ick + * 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 swi= tch + * that failed is not retried in a loop. + */ + if (mux) { + spin_lock_irqsave(&dwc->lock, flags); + pending =3D dwc->desired_dr_role !=3D desired_dr_role || + mux->desired_port !=3D 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); } =20 -void dwc3_set_mode(struct dwc3 *dwc, u32 mode) +static void dwc3_queue_mode(struct dwc3 *dwc, u32 mode, int port) { unsigned long flags; =20 if (dwc->dr_mode !=3D USB_DR_MODE_OTG) return; =20 + /* + * 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 =3D mode; + if (dwc->role_mux && port >=3D 0) + dwc->role_mux->desired_port =3D port; spin_unlock_irqrestore(&dwc->lock, flags); =20 queue_work(system_freezable_wq, &dwc->drd_work); } =20 +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 dat= a 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 publish= ed + * 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 =3D dep->dwc; diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h index f3dabb37f806..e6cf7b240b23 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); }; =20 +/* 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 da= ta 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 wh= ile + * keeping their Super Speed lanes separate. Each connector then needs it= s 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 switch= es * @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; =20 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); =20 #define DWC3_IP_IS(_ip) \ diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c index 01ebd73198e0..82e7a334b135 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 =3D 0; char buf[32]; =20 + /* 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; =20 diff --git a/drivers/usb/dwc3/drd.c b/drivers/usb/dwc3/drd.c index f3e37d383627..906cac4f3e6e 100644 --- a/drivers/usb/dwc3/drd.c +++ b/drivers/usb/dwc3/drd.c @@ -8,6 +8,7 @@ */ =20 #include +#include #include #include #include @@ -501,10 +502,319 @@ static enum usb_role dwc3_usb_role_switch_get(struct= usb_role_switch *sw) return role; } =20 +/* + * Index of the connector that should own the shared data path: the oldest + * pending request wins, and the last selection is kept when neither conne= ctor + * requests a role. Requests are ordered by a monotonic counter rather th= an 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 =3D mux->desired_port; + int i; + + for (i =3D 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].role =3D=3D USB_ROLE_NONE) + continue; + if (mux->ports[sel].role =3D=3D USB_ROLE_NONE || + mux->ports[i].seq < mux->ports[sel].seq) + sel =3D i; + } + + return sel; +} + +static u32 dwc3_role_mux_mode(struct dwc3 *dwc, enum usb_role role) +{ + if (role =3D=3D USB_ROLE_HOST) + return DWC3_GCTL_PRTCAP_HOST; + if (role =3D=3D USB_ROLE_DEVICE) + return DWC3_GCTL_PRTCAP_DEVICE; + if (dwc->role_switch_default_mode =3D=3D 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 rol= e) +{ + struct dwc3_role_port *port =3D usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc =3D port->dwc; + struct dwc3_role_mux *mux =3D 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 pa= th + * 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 =3D=3D USB_ROLE_NONE && role !=3D USB_ROLE_NONE) + port->seq =3D ++mux->seq; + port->role =3D role; + selected =3D dwc3_role_mux_pick(mux); + selected_role =3D 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 toget= her + * 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 =3D usb_role_switch_get_drvdata(sw); + struct dwc3 *dwc =3D 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 =3D port->role; + spin_unlock_irqrestore(&dwc->lock, flags); + + return role; +} + +static void dwc3_role_mux_exit(struct dwc3 *dwc) +{ + struct dwc3_role_mux *mux =3D 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 =3D false; + mutex_unlock(&mux->lock); + + for (i =3D 0; i < ARRAY_SIZE(mux->ports); i++) { + if (mux->ports[i].sw) + usb_role_switch_unregister(mux->ports[i].sw); + mux->ports[i].sw =3D NULL; + fwnode_handle_put(mux->ports[i].fwnode); + mux->ports[i].fwnode =3D NULL; + } +} + +/* + * Release the mux when this controller still owns it. devm_mux_control_g= et() + * 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 d= rained + * dwc->drd_work, otherwise a switch could select it again. + */ +static void dwc3_role_mux_deselect(struct dwc3 *dwc) +{ + struct dwc3_role_mux *mux =3D dwc->role_mux; + unsigned long flags; + int active_port; + + if (!mux) + return; + + spin_lock_irqsave(&dwc->lock, flags); + active_port =3D mux->active_port; + mux->active_port =3D -1; + spin_unlock_irqrestore(&dwc->lock, flags); + + if (active_port >=3D 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 =3D {}; + 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 =3D devm_kzalloc(dwc->dev, sizeof(*mux), GFP_KERNEL); + if (!mux) + return -ENOMEM; + + mux->ctrl =3D 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 =3D=3D -1 and + * claims the mux for endpoint 0). + */ + mux->desired_port =3D 0; + mux->active_port =3D -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 =3D mux; + + fwnode_graph_for_each_endpoint(dev_fwnode(dwc->dev), ep) { + if (!fwnode_property_present(ep, "usb-role-switch")) + continue; + + ret =3D fwnode_property_read_u32(ep, "mux-state", &state); + if (ret || state >=3D ARRAY_SIZE(mux->ports)) { + ret =3D -EINVAL; + goto err_put_ep; + } + if (mux->ports[state].fwnode) { + ret =3D -EINVAL; + goto err_put_ep; + } + + mux->ports[state].fwnode =3D fwnode_handle_get(ep); + mux->ports[state].state =3D state; + mux->ports[state].dwc =3D dwc; + } + + for (i =3D 0; i < ARRAY_SIZE(mux->ports); i++) { + if (!mux->ports[i].fwnode) { + ret =3D -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 =3D 0; i < ARRAY_SIZE(mux->ports); i++) { + snprintf(name, sizeof(name), "%s-port%u", dev_name(dwc->dev), i); + desc.name =3D name; + desc.fwnode =3D mux->ports[i].fwnode; + desc.set =3D dwc3_mux_role_set; + desc.get =3D dwc3_mux_role_get; + desc.driver_data =3D &mux->ports[i]; + mux->ports[i].sw =3D usb_role_switch_register(dwc->dev, &desc); + if (IS_ERR(mux->ports[i].sw)) { + ret =3D PTR_ERR(mux->ports[i].sw); + mux->ports[i].sw =3D 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 =3D 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 dra= in + * 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 =3D {NULL}; u32 mode; + int ret; =20 dwc->role_switch_default_mode =3D usb_get_role_switch_default_mode(dwc->d= ev); if (dwc->role_switch_default_mode =3D=3D USB_DR_MODE_HOST) { @@ -513,6 +823,11 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) dwc->role_switch_default_mode =3D USB_DR_MODE_PERIPHERAL; mode =3D DWC3_GCTL_PRTCAP_DEVICE; } + + ret =3D dwc3_role_mux_init(dwc, mode); + if (ret) + return ret < 0 ? ret : 0; + dwc3_set_mode(dwc, mode); =20 dwc3_role_switch.fwnode =3D dev_fwnode(dwc->dev); @@ -526,7 +841,7 @@ static int dwc3_setup_role_switch(struct dwc3 *dwc) =20 if (dwc->dev->of_node) { /* populate connector entry */ - int ret =3D devm_of_platform_populate(dwc->dev); + ret =3D devm_of_platform_populate(dwc->dev); =20 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 =20 int dwc3_drd_init(struct dwc3 *dwc) @@ -597,6 +914,7 @@ void dwc3_drd_exit(struct dwc3 *dwc) { unsigned long flags; =20 + dwc3_role_mux_exit(dwc); if (dwc->role_sw) usb_role_switch_unregister(dwc->role_sw); =20 @@ -626,6 +944,15 @@ void dwc3_drd_exit(struct dwc3 *dwc) break; } =20 + /* + * Deselect only after the active role has been torn down, in the same + * order as __dwc3_set_mode(): mux_control_deselect() switches the shared + * High Speed lines back to the mux' idle state, which would cut the data + * path off underneath a host or gadget instance that is still running. + * The mode worker is drained, so nothing can select the mux again. + */ + dwc3_role_mux_deselect(dwc); + if (dwc->otg_irq) free_irq(dwc->otg_irq, dwc); }