Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp409432pxk; Thu, 24 Sep 2020 08:30:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwXyVVhdMSuD1FUNq7pOqaO6QT+86tgkF0B509tj2DgeaDVLE3FpaK3RM18cjqbfFsmPrLU X-Received: by 2002:aa7:c419:: with SMTP id j25mr451180edq.109.1600961405328; Thu, 24 Sep 2020 08:30:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600961405; cv=none; d=google.com; s=arc-20160816; b=P2BjkomNRKaXkseunQH/+PBt//82I2n/Vuf8r3igFZzDZ5MEqzebM0YuzmL7JI6BOB +q32/3yLjehFcNr/zXuOurKHnZGtb5Q4+c0bulNAJUF0yzlepkkHGNo2ETwbbJV5mUq7 I7k85w5HP6Y5qryqhmsnAS8Z4D68+UbZUV9/nI3Ew1qINDhNunrjS4rEmeDp3ZOQzNeg R+z5d3qm29bqDIyJ83iCKh2r4VFFzzPpnhg1wC1w2bVigCgf6+8shWc0o89/bQr3oo+p uZkyE9a5Xd74ZDiM2mFJm5HkrlE3yGGSaWrbf8g1Mt0mdkkBX2nlFV0FDGLiqFD/ngjV fYHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=vmnqGncA5GDusAVUeiXfHpwf6UbxmQD+0VhGVVHiA/0=; b=UOXyRU8ntSWaaQ94ZFo2f86P4nAp547fDJfFkg2SfdeFGM0OezMcRj1ksdMmguVG3p EXbw2ZHvxTfzU+zKw4ZkLRtNb5y5YCdiq3wD1UymTHeuTnspRH7KEJqhm2W+5O1RzwQA 9hMI7Z04WPZtalcNRbzGlQoLFxgy5OuIRgMao5ZVvi0QKOSwpECAdi1pV7ycCtzCZQdb wx2ZeKsVFnjrFWoOamZiNBl8vYn293aFgJzlfSWiyEP7vUNjnlv9OvKq0QM1Ugu5ilTJ dTCsgUzmAxcc4IBRh50avXcE1GOB+6Pm2sqzPmQ/4MERZyh5ZJFT6Hz/qMITFKGJsDoU J/LQ== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n21si2408126edt.580.2020.09.24.08.29.41; Thu, 24 Sep 2020 08:30:05 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728421AbgIXP2A (ORCPT + 99 others); Thu, 24 Sep 2020 11:28:00 -0400 Received: from netrider.rowland.org ([192.131.102.5]:39601 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1728285AbgIXP2A (ORCPT ); Thu, 24 Sep 2020 11:28:00 -0400 Received: (qmail 1340421 invoked by uid 1000); 24 Sep 2020 11:27:58 -0400 Date: Thu, 24 Sep 2020 11:27:58 -0400 From: Alan Stern To: Matthias Kaehlcke Cc: Greg Kroah-Hartman , Rob Herring , Frank Rowand , linux-usb@vger.kernel.org, Douglas Anderson , Bastien Nocera , Ravi Chandra Sadineni , Peter Chen , Krzysztof Kozlowski , devicetree@vger.kernel.org, Stephen Boyd , linux-kernel@vger.kernel.org, "Alexander A. Klimov" , Masahiro Yamada Subject: Re: [PATCH v3 2/2] USB: misc: Add onboard_usb_hub driver Message-ID: <20200924152758.GA1337044@rowland.harvard.edu> References: <20200923180952.v3.1.I248292623d3d0f6a4f0c5bc58478ca3c0062b49a@changeid> <20200923180952.v3.2.I7c9a1f1d6ced41dd8310e8a03da666a32364e790@changeid> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200923180952.v3.2.I7c9a1f1d6ced41dd8310e8a03da666a32364e790@changeid> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 23, 2020 at 06:10:12PM -0700, Matthias Kaehlcke wrote: > The main issue this driver addresses is that a USB hub needs to be > powered before it can be discovered. For discrete onboard hubs (an > example for such a hub is the Realtek RTS5411) this is often solved > by supplying the hub with an 'always-on' regulator, which is kind > of a hack. Some onboard hubs may require further initialization > steps, like changing the state of a GPIO or enabling a clock, which > requires even more hacks. This driver creates a platform device > representing the hub which performs the necessary initialization. > Currently it only supports switching on a single regulator, support > for multiple regulators or other actions can be added as needed. > Different initialization sequences can be supported based on the > compatible string. > > Besides performing the initialization the driver can be configured > to power the hub off during system suspend. This can help to extend > battery life on battery powered devices which have no requirements > to keep the hub powered during suspend. The driver can also be > configured to leave the hub powered when a wakeup capable USB device > is connected when suspending, and power it off otherwise. > > Technically the driver consists of two drivers, the platform driver > described above and a very thin USB driver that subclasses the > generic driver. The purpose of this driver is to provide the platform > driver with the USB devices corresponding to the hub(s) (a hub > controller may provide multiple 'logical' hubs, e.g. one to support > USB 2.0 and another for USB 3.x). > > Co-developed-by: Ravi Chandra Sadineni > Signed-off-by: Ravi Chandra Sadineni > Signed-off-by: Matthias Kaehlcke > --- > --- a/drivers/usb/misc/Kconfig > +++ b/drivers/usb/misc/Kconfig > @@ -275,3 +275,19 @@ config USB_CHAOSKEY > > To compile this driver as a module, choose M here: the > module will be called chaoskey. > + > +config USB_ONBOARD_HUB > + tristate "Onboard USB hub support" > + depends on OF || COMPILE_TEST > + help > + Say Y here if you want to support discrete onboard USB hubs that > + don't require an additional control bus for initialization (an ... but does require nontrivial form of initialization, such as enabling a power regulator. > +static void onboard_hub_remove_usbdev(struct onboard_hub *hub, struct usb_device *udev) > +{ > + struct udev_node *node; > + > + smp_rmb(); > + if (hub->going_away) { > + /* > + * We are most likely being called as a result of unbinding a USB device from > + * onboard_hub_remove(). This function also holds the lock and iterates over > + * 'udev_list'. Skip deleting the node in this case to avoid a self deadlock, > + * keeping the node in the list isn't a problem, since the device is about to go > + * away. > + */ > + return; > + } This part has a suspicious look. For one thing, there's no comment explaining the purpose of the smp_rmb(). For another, that barrier doesn't seem to pair with any other memory barrier in the driver. I get that you want to avoid self-deadlock here. But there must be a better way. See below. > +static int onboard_hub_remove(struct platform_device *pdev) > +{ > + struct onboard_hub *hub = dev_get_drvdata(&pdev->dev); > + struct udev_node *node; > + > + hub->going_away = true; > + > + mutex_lock(&hub->lock); > + > + /* unbind the USB devices to avoid dangling references to this device */ > + list_for_each_entry(node, &hub->udev_list, list) > + device_release_driver(&node->udev->dev); > + > + mutex_unlock(&hub->lock); Alternative approach: /* unbind the USB devices to avoid dangling references to this device */ mutex_lock(&hub->lock); while (!list_empty(&hub->udev_list)) { node = list_first_entry(&hub->udev_list, struct udev_node, list); udev = node->udev; /* * Unbinding the driver will call onboard_hub_remove_usbdev(), * which acquires hub->lock. We must release the lock first. */ usb_get_device(udev); mutex_unlock(&hub->lock); device_release_driver(&udev->dev); usb_put_device(udev); mutex_lock(&hub->lock); } mutex_unlock(&hub->lock); > +static int onboard_hub_usbdev_probe(struct usb_device *udev) > +{ > + struct device *dev = &udev->dev; > + struct onboard_hub *hub; > + > + /* ignore supported hubs without device tree node */ > + if (!dev->of_node) > + return -ENODEV; > + > + hub = _find_onboard_hub(dev); > + if (IS_ERR(hub)) > + return PTR_ERR(dev); hub, not dev. Alan Stern