Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1475120rwd; Thu, 1 Jun 2023 16:41:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4TgQOKPHndgFUP7hj2rCqEVBI3Ovg3E7Jb6I2+9+pHL7cFRIRfA4D9BgAMGOxwTQ3cfag0 X-Received: by 2002:a17:902:e88e:b0:1b0:236e:e5ed with SMTP id w14-20020a170902e88e00b001b0236ee5edmr640707plg.14.1685662859649; Thu, 01 Jun 2023 16:40:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685662859; cv=none; d=google.com; s=arc-20160816; b=EYycsnUGbHU1WdgLh9lr7XHUMucFutMgPY548J+/HaPr9ZK1fEpSKpi9lDXpMtbzX3 iLapg1bAOKsM5bUvFcyTpM5K8MpdkLaPoNOSCTTEGjIylAKV1mhrc0MwiqMQ1fi9Jo6c JzIZv5Xj4CipbfTiNliQNDwdKPpheOPlYXI1wGqW8NEWbmOHLEp4NKmJPtC69kMuwL3p cXr67iZ0RHKTrYR0THiBQcG+IKm7u0o/c9C0dzWSJdCyCwboNKPupTWIM3Ej5RGjmuFL o4dyeKWfS9jH6+VDuDEjVBGL1zVQ50g96P+4uDfG4U+pP1W/TOHLIgr2CbtBJ8V5iePF TK4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=lhLnsMBqslFGPzfwwgNhQr0KBzOeKMxdfpUcCDl14Bs=; b=D6y9muokVADdw59MJE7Q9ksZprQpsVA5+zfpoUPpDhhp4qiTfZHWKBnWc/3+GvSj/B ThJSOmSnTAhQqA8WOmAFEDhdPIpoxkkmCTDAwdnclrYsQd7MRRYayZ6EYNptNbrNS2ZK iSAChx8EZwvu3SITbpN13otwV9mgLrgH5UxvVfmB3rDx9kboWixfHIvDlZYA/YdY0Fzq 2tB18Oq0Dox1VbBCdCh+4uVl9Gehk7wwJ8ireCRGqPb+JBc2kAd80Q7rj4/nWnTx11tq UCt53WJb+pEaJfRE4B2H5h5FqH7anx33VOISvSPRq4UmqcI1iZWLqbbolQxXvlBKYSyE aDsw== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q4-20020a17090311c400b001aaf6228a70si2352621plh.325.2023.06.01.16.40.45; Thu, 01 Jun 2023 16:40:59 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232822AbjFAXVj (ORCPT + 99 others); Thu, 1 Jun 2023 19:21:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231815AbjFAXVh (ORCPT ); Thu, 1 Jun 2023 19:21:37 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id BE839184 for ; Thu, 1 Jun 2023 16:21:34 -0700 (PDT) Received: (qmail 20719 invoked by uid 1000); 1 Jun 2023 19:21:33 -0400 Date: Thu, 1 Jun 2023 19:21:33 -0400 From: Alan Stern To: Roy Luo 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 Subject: Re: [PATCH v1] usb: core: add sysfs entry for usb device state Message-ID: <4f049d62-d103-4491-9c7e-16c7cf94eac1@rowland.harvard.edu> References: <20230601230456.2234972-1-royluo@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230601230456.2234972-1-royluo@google.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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, Jun 01, 2023 at 11:04:56PM +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 enter > 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 bad?" > 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. > > Reviewed-by: Alan Stern > Signed-off-by: Roy Luo > --- > diff --git a/drivers/usb/core/hub.c b/drivers/usb/core/hub.c > index 97a0f8faea6e..35d94288726b 100644 > --- a/drivers/usb/core/hub.c > +++ b/drivers/usb/core/hub.c > @@ -2018,6 +2018,23 @@ bool usb_device_is_owned(struct usb_device *udev) > return !!hub->ports[udev->portnum - 1]->port_owner; > } > > +static void update_port_device_state(struct usb_device *udev) > +{ > + struct usb_port *port_dev = NULL; > + struct usb_hub *hub = NULL; > + struct kernfs_node *state_node = NULL; > + > + if (udev->parent) { > + hub = usb_hub_to_struct_hub(udev->parent); > + port_dev = hub->ports[udev->portnum - 1]; > + WRITE_ONCE(port_dev->state, udev->state); > + state_node = sysfs_get_dirent(port_dev->dev.kobj.sd, "state"); > + if (state_node) { > + sysfs_notify_dirent(state_node); > + } > + } > +} I didn't notice the "= NULL" initializers before. You might want to remove them, since they are completely unnecessary. Alan Stern