Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp340792pxb; Wed, 18 Aug 2021 03:49:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyI5sP28MQjyVjzIT+zVyrcTHGNmcUbKNo1pcupN0dJfad3yf+g2HdGq+L28tvTBB7a1dcc X-Received: by 2002:a02:c61a:: with SMTP id i26mr7592110jan.108.1629283749298; Wed, 18 Aug 2021 03:49:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629283749; cv=none; d=google.com; s=arc-20160816; b=MaToPZpbrFHLbXTe1rN1ov7zz25p05zLWb50R1vxlAOCBU8HcliSiwBuBhesypLDPS lMRacmo5lVmHFfFY723B7NFIykzFilkS0QSSDcLPTxY33mDcsSFfrgkQd6qRzjfQlW8v EMR1puTQD6PgZXkBmI5e+R9VFhRYlxhLGAsBe9odm+SWxNpotqwBJDHN8FtTjPgk5PcW VFwBsjeHeXj595tXHTd3nxEDZBbAwbfnun+0OmgK7vnsXbS9e4uutCZ4vQx81vlTW+Lv Vzf5hUvU0fRF0GmAIRW0bGELy3D4LnofrXXynw0VyDYPnLr3iK0rVwfkCiAQUPHfsBfl V8Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ga1LKRsZo7FK/NouAlLZlaG+OrM/2OOXzxvNw7QH1pY=; b=cUlMlgcExJAJQxrLDeakDDaH3w10rOsZKpqxpqBlsE/JCQqina9pB6qkCJbFT3EyG2 wC6qnycWww6tf/EkR0g8vAvV0GnTKhKmFs/ZsjA/hoQRgVnKuy0v4nzNroQG2T/7x0Jr y0wov80OiKTO8Q8dOF+hKeKZre0qMyGzIQb7whAk/hcPQl4dyGrKL1eoM9A/n7RqaloF U0IWLSFgbMeRYMc//wkq22zEAP7dy1SM/AxOFhNhYb+pWGq12dbTBUoyeHoeNspkPm71 8vOa7JQlNUQbLpLAZZ8f79Qu5qFKkUupZqa2Xg3RNPIZad0EqJg8+sIdBS/PcJhPuT5b I4FA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=F2NorgIH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s25si4631959iov.92.2021.08.18.03.48.56; Wed, 18 Aug 2021 03:49:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=F2NorgIH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234485AbhHRKsR (ORCPT + 99 others); Wed, 18 Aug 2021 06:48:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233848AbhHRKsQ (ORCPT ); Wed, 18 Aug 2021 06:48:16 -0400 Received: from mail-io1-xd35.google.com (mail-io1-xd35.google.com [IPv6:2607:f8b0:4864:20::d35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5125BC0613CF for ; Wed, 18 Aug 2021 03:47:42 -0700 (PDT) Received: by mail-io1-xd35.google.com with SMTP id b200so2085427iof.13 for ; Wed, 18 Aug 2021 03:47:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ga1LKRsZo7FK/NouAlLZlaG+OrM/2OOXzxvNw7QH1pY=; b=F2NorgIHAJhbf6tz6BnTD5Y/gRyF8xmpMTGWjYyvcD7Mmcp9o1gChPKOaAl4fKO2ql H27NJBx0GwIStUxQL/xSf8yr7RQQ9M5PYvosoCFjFmlxJhClCBU2gyTE2BGLMHnnx2kn ti2GyEFhOn/IzyYSqQp9aU6yzj4B9elG3FbY8G505EyirOtoGE4Rl0occszcViz/p5NK RSsnU1vmc5O1ZlwzMxkKjnx2fihbWNBOMAVLVxxBb2NCLXX0v/XfJy7AA2VKnkt13/W6 lJqsE4/5nJAx3yUECdaR9G1cCbwYP7vjWD46WE5bUAyA/wmP5k7uKAGRWFyD3eL4vZZI wsKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ga1LKRsZo7FK/NouAlLZlaG+OrM/2OOXzxvNw7QH1pY=; b=hPBtkR0cE8VcZXFcKgLQJC+FIQWubV2jvqBUojvmssb9kjmhyoEE7pACCKRtT2UGaW UfS0Ho7xiI8fw0CalqdjVOjtSB+lv2lZnBC5pRl3AYj0Ab8zu1Gs4mrupj9orzRK69p6 IsuWdhzDbPx8lfXxJDuM8rhOtfMx+d1mD54ttfOFWBTrEN3/NLqdSL3O5T4d+8EnOyjM 5aW9s/VNP/pZ+3bzYZClGLZnlzYW1jkftzEU+HiRHIJDWhxg6h9p4s7Y/m2EOvNg3IZU hp94NSAZIuFAPwc8nS1IquStbcKr/Fc48Ag+q6j4EFLX7DipDcCI5BOMKQDurmkFyEwn oxXQ== X-Gm-Message-State: AOAM532XJMS23AOxvx1GRvIDjVKXrbLj9kbZWe7hI+npHdT8ErXpoLpq l6Wv4JuvDTmBX8L74VX3FlyhDjW988w+kKaf5Gavsw== X-Received: by 2002:a05:6638:974:: with SMTP id o20mr7277374jaj.10.1629283661292; Wed, 18 Aug 2021 03:47:41 -0700 (PDT) MIME-Version: 1.0 References: <20210813043131.833006-1-icenowy@aosc.io> <58034df4-f18c-ab3e-1fcc-dc85fc35320f@roeck-us.net> In-Reply-To: From: Kyle Tso Date: Wed, 18 Aug 2021 18:47:25 +0800 Message-ID: Subject: Re: [PATCH] usb: typec: tcpm: always rediscover when swapping DR To: Icenowy Zheng Cc: Guenter Roeck , Heikki Krogerus , Greg Kroah-Hartman , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Badhri Jagan Sridharan Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 18, 2021 at 5:27 PM Icenowy Zheng wrote: > > > > =E4=BA=8E 2021=E5=B9=B48=E6=9C=8818=E6=97=A5 GMT+08:00 =E4=B8=8B=E5=8D=88= 4:02:24, Kyle Tso =E5=86=99=E5=88=B0: > >On Tue, Aug 17, 2021 at 11:13 PM Guenter Roeck wrot= e: > >> > >> On 8/17/21 2:36 AM, Heikki Krogerus wrote: > >> > On Fri, Aug 13, 2021 at 12:31:31PM +0800, Icenowy Zheng wrote: > >> >> Currently, TCPM code omits discover when swapping to gadget, and as= sume > >> >> that no altmodes are available when swapping from gadget. However, = we do > >> >> send discover when we get attached as gadget -- this leads to modes= to be > >> >> discovered twice when attached as gadget and then swap to host. > >> >> > >> >> Always re-send discover when swapping DR, regardless of what change= is > >> >> being made; and because of this, the assumption that no altmodes ar= e > >> >> registered with gadget role is broken, and altmodes de-registeratio= n is > >> >> always needed now. > >> >> > >> >> Signed-off-by: Icenowy Zheng > >> >> --- > >> >> drivers/usb/typec/tcpm/tcpm.c | 9 ++++----- > >> >> 1 file changed, 4 insertions(+), 5 deletions(-) > >> >> > >> >> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm= /tcpm.c > >> >> index b9bb63d749ec..ab6d0d51ee1c 100644 > >> >> --- a/drivers/usb/typec/tcpm/tcpm.c > >> >> +++ b/drivers/usb/typec/tcpm/tcpm.c > >> >> @@ -4495,15 +4495,14 @@ static void run_state_machine(struct tcpm_p= ort *port) > >> >> tcpm_set_state(port, ready_state(port), 0); > >> >> break; > >> >> case DR_SWAP_CHANGE_DR: > >> >> - if (port->data_role =3D=3D TYPEC_HOST) { > >> >> - tcpm_unregister_altmodes(port); > >> >> + tcpm_unregister_altmodes(port); > >> >> + if (port->data_role =3D=3D TYPEC_HOST) > >> >> tcpm_set_roles(port, true, port->pwr_role, > >> >> TYPEC_DEVICE); > >> >> - } else { > >> >> + else > >> >> tcpm_set_roles(port, true, port->pwr_role, > >> >> TYPEC_HOST); > >> >> - port->send_discover =3D true; > >> >> - } > >> >> + port->send_discover =3D true; > >> >> tcpm_ams_finish(port); > >> >> tcpm_set_state(port, ready_state(port), 0); > >> >> break; > >> > > >> > Why is it necessary to do discovery with data role swap in general? > >> > > >> > thanks, > >> > > >> > >> Additional question: There are two patches pending related to DR_SWAP > >> and discovery. Are they both needed, or do they both solve the same > >> problem ? > >> > >> Thanks, > >> Guenter > > > >Hi, I just noticed this patch. > > > >Part of this patch and part of my patch > >https://lore.kernel.org/r/20210816075449.2236547-1-kyletso@google.com > >are to solve the same problem that Discover_Identity is not sent in a > >case where the port becomes UFP after DR_SWAP while in PD3. > > > >The difference (for the DR_SWAP part) is that my patch does not set > >the flag "send_discover" if the port becomes UFP after PD2 DR_SWAP. > >That is because in PD2 Spec, UFP is not allowed to be the SVDM > >Initiator. > > > >This patch indeed solves another problem where > >tcpm_unregister_altmodes should be called during PD3 DR_SWAP because > >the port partner may return mode data in the latest Discover_Mode. For > >the PD2 case, I don't think it needs to be called because PD2 DFP will > >always return NAK for Discover_Mode. However it is fine because it is > > It sounds like my dock is doing something against the specification... > > It sends altmodes rather than NAK when my board gets attached as > UFP and send bogus discovers (because of bugs of current TCPM > code). These altmodes are then registered in TCPM and never get > cleaned up within it, which leads to conflict when the dock is > removed and then re-inserted. > Could you provide more details? 1. Is the connection in PD3 ? 2. Could you provide the tcpm logs? > (BTW is it neceesary for data role and power role to be the same > when attaching? My board now gets attached as UFP to drain > power, and then do DR_SWAP to become USB host.) > Either Sink/UFP or Source/DFP in the beginning. Then DR_SWAP is okay when both ports are in ready state. Your dock looks like a Sourcing Device. Type-C Spec: ``` 4.8.4 Sourcing Device A Sourcing Device is a special sub-class of a DRP that is capable of supplying power, but is not capable of acting as a USB host. For example a hub=E2=80=99s UFP or a monitor=E2=80=99s UFP that operates as a d= evice but not as a host. The Sourcing Device shall follow the rules for a DRP (See Section 4.5.1.4 and Figure 4-15). It shall also follow the requirements for the Source as Power Source (See Section 4.8.1). The Sourcing Device shall support USB PD and shall support the DR_Swap command in order to enable the Source to assume the UFP data role. ``` thanks, Kyle > >safe to call tcpm_unregister_altmodes even if there is no mode data. > > > >In fact, when I was tracing the code I found another bug. PD2 UFP is > >not allowed to send Discover_Identity and Discover_Mode. I can send > >another patch to address this problem. > > > >thanks, > >Kyle