Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1336659rwd; Thu, 25 May 2023 11:04:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6V6DmXJ97InwPjmV17Rz369uQNZCKZoExjP09jteKfJEtwQgjTSLMvIhpEUyztGFmcnFn/ X-Received: by 2002:a05:6a00:392a:b0:643:53b6:d841 with SMTP id fh42-20020a056a00392a00b0064353b6d841mr10671371pfb.2.1685037897294; Thu, 25 May 2023 11:04:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685037897; cv=none; d=google.com; s=arc-20160816; b=Z209oFWlzbaFXUQ/dqHvULcaHWIt/rPKbliIeMt8lzgeBEmMWXQSC3SkseMxMYLt3Y ktR2qOXbrMqMw4w/L2zBTY0eVGa2VGr2U3sQEhpPdpI913IztsgJi726pTlCUVjSHdJ1 8ZFQYqKT/IffGpGd7FVnrlGchY0lkgu7z8k1jV4RRzaXAgAOSaA+FVLsxybE2ZnNVNJ9 ZKepAcn0vIHsraCVJVJFLL7U0d/ws2mwAvmsqTcGHt4tVA0ssE6johxfAOjf9SHJNrak /6UASh+XTf8Hylbza2JdJcOmdsr+cCqNyYnq4I3BVwaTfryrcCmTx1ZZ6tPRubKu8m8b 1WBA== 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=TBIyXlygXkmOj2rbN4qSSnwQZ7BPKRfaualrgmmRoWM=; b=nYy2GQLDXwLquap4hz8PFgaopy0fhpQSdnW7HsUkC8haeLLbayGFO7AZNeeKBeWb4I NYsuCwSXUf9jUPvg7vScOJaD0WMa21V/gJv4WJ86ypGoJs1GwwCXknbGztTKr5vfyE17 13d2TU4NBIzKraqwTHselH9hOuYx5dIcG+N8PJIK5V7eO4teSz0mWifLeozgwlakDrEq xFAEX6y5VZ+Q9vKkyfFJZDjyt0+91LOT/QTHBLGrgZLOWAJ1jIDC4R4AqdhSI0+SGuGa o5ZWr7JUosWBuFA7NjGsV0Zw+PsP9tel0iwcKapdTuzNSmqdb5+i5exEuawfmyU+hq9J tD1g== 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 c66-20020a633545000000b00534792ee482si1574670pga.758.2023.05.25.11.04.43; Thu, 25 May 2023 11:04:57 -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 S241186AbjEYSD3 (ORCPT + 99 others); Thu, 25 May 2023 14:03:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240639AbjEYSDL (ORCPT ); Thu, 25 May 2023 14:03:11 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id C122CE70 for ; Thu, 25 May 2023 11:02:57 -0700 (PDT) Received: (qmail 260359 invoked by uid 1000); 25 May 2023 14:02:56 -0400 Date: Thu, 25 May 2023 14:02:56 -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: [RFC PATCH v1] usb: core: add sysfs entry for usb device state Message-ID: <408575c0-2967-4cdb-92c7-1b2845038d20@rowland.harvard.edu> References: <20230525173818.219633-1-royluo@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230525173818.219633-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, 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 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. > > 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. > 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 *dev, > } > 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 = to_usb_port(dev); > + enum usb_device_state state = USB_STATE_NOTATTACHED; > + > + if (port_dev->state) > + state = *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