Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1467782rwd; Thu, 25 May 2023 13:06:06 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4ItT4ed05ICNkqqnY0PdVIGN3zOOCJwkZNNSDhZ9WnojfcclxTLDu9ZJpizuT0DpJHgL5Z X-Received: by 2002:a17:90b:38cd:b0:24d:f159:d28b with SMTP id nn13-20020a17090b38cd00b0024df159d28bmr2689563pjb.47.1685045166524; Thu, 25 May 2023 13:06:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685045166; cv=none; d=google.com; s=arc-20160816; b=DOoMTHlYz6aUwL20n1a1ng9FrUmA26S0PKF2ri4Y/1YD83KWznFi89GT/pvAzUi9VM 8FiAnJo//6IlRxz6L0hdu1Q5s7C3v0DDt9Z6hHgtm2jHKrZrPAw4l4KCyRTYD5vRp2PI kDTFSYHtVNOcuGCtsz5pOWnpmjWbDNyJb9Gawt1kvdY/IsEAIC1BDB0VgV8jxH3L6pR+ dNakymfIafxLPB36YMZpOIjJnDY3vOwWRwQoyqbMP7scyg5wiC9hlZV0voCtOHHlrNh+ QrVtlF4X58ithEaRcdZcV0zoUC+hkeHpbCs+QBYME/7GP6dqvd6wZpuNAGg6DsYc6rU4 oVjw== 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=K4TyW3F7pnvio/+Mfhq8AWNdmZyUuT7pRHhynq/9Tkg=; b=FU5qbllZOxFGqk5pw5kSU+r7eq/ZXIU4nbR9PviOK9LNTpGFL7H2EtVFyxVxsjPRVw GXpyWBmQ4Hhatyxf7+nrKUBcwj6DsgyxdugvCLuZcdd3xcTurlTBWi7YJhjKWOKkTF7V xtuCutnwSPGVZSxDgSEfCdPE4wJ1TcFzAq6kNoMXMYBlfgZhOmf2Gljv16+cWkR3CFIa c4RNoEJUgQIiatzyegHVZP54ZjGKuVXt1bwXjR8/36YWnERUslpZ9v6yxwwbnUoJBomB uielLKQtYV1zGbQnU9/JwaS/gKRt/BGzEClv0FYiWz3f8DP9AuBMFqfh03GkPXi7+Ap+ PV8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="UU6/pwsL"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z194-20020a6333cb000000b0053468421910si1787636pgz.620.2023.05.25.13.05.53; Thu, 25 May 2023 13:06:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="UU6/pwsL"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S242374AbjEYT21 (ORCPT + 99 others); Thu, 25 May 2023 15:28:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242036AbjEYT2K (ORCPT ); Thu, 25 May 2023 15:28:10 -0400 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B3AD9B for ; Thu, 25 May 2023 12:27:22 -0700 (PDT) Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-3f6dfc4e01fso7934995e9.0 for ; Thu, 25 May 2023 12:27:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685042744; x=1687634744; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=K4TyW3F7pnvio/+Mfhq8AWNdmZyUuT7pRHhynq/9Tkg=; b=UU6/pwsLMHpaU5AyFKe4m8NfGGfrbXiOq7ozubgQdQpLm8Ay4qm7QfKefuQMRxheM5 squEU/t5DsGljg3WHkDX8FT8h/ovSdWEURORkPLeXvCsEimABwxjgzoOHQ2wbw2rBxc/ SM5Xerkl6yGXyUKkeTXJ5uHftKVq0r6XTQpdJVRYe85doW/EPN6ygxUIX0Y/dHB7q74C c/6qpAPf56WEv7qJv0bmW/jrtk99d/s0E9FmJCN4Ey/9vzRRH4fYuO3jFtYdKpc9VM8F QHlIJzIkChvAK/sqtvs7WmzOPe7CBMYKAvIb0xxrq6bmz9oOj7w4A4M/1zUNpld5JUgY anng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685042744; x=1687634744; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K4TyW3F7pnvio/+Mfhq8AWNdmZyUuT7pRHhynq/9Tkg=; b=MhztR5uVuvFa1Q7brYBza/k62hHhOPlB/ltnywE/Ur5QdyZNWijFg82FFXx5Lbl42a ZS8DPrSqHQxSdTX3QMFETA4Nd0IiRBpa00px24ISE5L0BH4NM55PjcIDv8tj1R1ggPRl 94P4xH+upWdKQxrlI61Qz5B50LEsH8BmB1VB3odRvTytL5amNjXfTJ6/56TnBYkwVO8R sIllBDpn+6wfbdLJaxPgk8o63dnI8i6BMMEJjIJvU4EzcudgZaQIpBAl7Pd3ZLXMSgxC pJKwCbnlNM48bvjMrXNkJfni3AuyT/Xtvy1SXJMT1fjn1iWJVXVDoVMzmdkUUW1aWcLw cCgA== X-Gm-Message-State: AC+VfDyiUmOZb0jhuBR+u3V5fhbpdrHu8zF9wcAco+UeQ7GcI9O6bitj 1CPislZdKwIdbUfjz38CLDqgJH4qm6Xbj1Nt6+y74oA0Ak/it0vQhqk= X-Received: by 2002:a1c:7307:0:b0:3f6:3ad:16a with SMTP id d7-20020a1c7307000000b003f603ad016amr3007271wmb.31.1685040419721; Thu, 25 May 2023 11:46:59 -0700 (PDT) MIME-Version: 1.0 References: <20230525173818.219633-1-royluo@google.com> <408575c0-2967-4cdb-92c7-1b2845038d20@rowland.harvard.edu> In-Reply-To: <408575c0-2967-4cdb-92c7-1b2845038d20@rowland.harvard.edu> From: Roy Luo Date: Thu, 25 May 2023 11:46:23 -0700 Message-ID: Subject: Re: [RFC PATCH v1] usb: core: add sysfs entry for usb device state To: Alan Stern Cc: raychi@google.com, badhri@google.com, Greg Kroah-Hartman , Benjamin Tissoires , Michael Grzeschik , Bastien Nocera , Mathias Nyman , Matthias Kaehlcke , Flavio Suligoi , Douglas Anderson , Christophe JAILLET , linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 25, 2023 at 11:02=E2=80=AFAM Alan Stern wrote: > > On Thu, May 25, 2023 at 05:38:18PM +0000, Roy Luo wrote: > > Expose usb device state to userland as the information is useful in > > detecting non-compliant setups and diagnosing enumeration failures. > > For example: > > - End-to-end signal integrity issues: the device would fail port reset > > repeatedly and thus be stuck in POWERED state. > > - Charge-only cables (missing D+/D- lines): the device would never ente= r > > POWERED state as the HC would not see any pullup. > > > > What's the status quo? > > We do have error logs such as "Cannot enable. Maybe the USB cable is ba= d?" > > to flag potential setup issues, but there's no good way to expose them = to > > userspace. > > > > Why add a sysfs entry in struct usb_port instead of struct usb_device? > > The struct usb_device is not device_add() to the system until it's in > > ADDRESS state hence we would miss the first two states. The struct > > usb_port is a better place to keep the information because its life > > cycle is longer than the struct usb_device that is attached to the port= . > > > > Signed-off-by: Roy Luo > > --- > > > diff --git a/drivers/usb/core/hub.h b/drivers/usb/core/hub.h > > index e23833562e4f..110143568c77 100644 > > --- a/drivers/usb/core/hub.h > > +++ b/drivers/usb/core/hub.h > > @@ -84,8 +84,10 @@ struct usb_hub { > > * @peer: related usb2 and usb3 ports (share the same connector) > > * @req: default pm qos request for hubs without port power control > > * @connect_type: port's connect type > > + * @state: device state of the usb device attached to the port > > This member is essentially a duplicate of the .child member of the > usb_port structure. That is, it points to the .state member of the > child device instead of to the child device itself, but this is pretty > much the same thing. You could replace *(port_dev->state) with > port_dev->child->state. > Alan, thanks for the quick response! Yes, port_dev->state is indeed the same as port_dev->child->state. However, I still add port_dev->state because port_dev->child won't be assigned until the corresponding usb_device is in ADDRESS state. I wish I can assign get port_dev->child assigned earlier, but I think the current design - assign port_dev->child and device_add() after ADDRESS state - also makes sense because there are many ways that the enumeration could fail in the early stage. By adding port_dev->state, I can link usb_device->state to usb_port as soon as the usb_device is created to get around the limitation of port_dev->child. I would be very happy to hear other ideas. > > diff --git a/drivers/usb/core/port.c b/drivers/usb/core/port.c > > index 06a8f1f84f6f..7f3430170115 100644 > > --- a/drivers/usb/core/port.c > > +++ b/drivers/usb/core/port.c > > @@ -160,6 +160,19 @@ static ssize_t connect_type_show(struct device *de= v, > > } > > static DEVICE_ATTR_RO(connect_type); > > > > +static ssize_t state_show(struct device *dev, > > + struct device_attribute *attr, char *buf) > > +{ > > + struct usb_port *port_dev =3D to_usb_port(dev); > > + enum usb_device_state state =3D USB_STATE_NOTATTACHED; > > + > > + if (port_dev->state) > > + state =3D *port_dev->state; > > + > > + return sprintf(buf, "%s\n", usb_state_string(state)); > > This races with device addition and removal (and with device state > changes). To prevent these races, you have to hold the > device_state_lock spinlock while accessing the child device and its > state. > > Unfortunately that spinlock is private to hub.c, so you will have to > make it public before you can use it here. > > Alan Stern