mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Prashant Malani <pmalani@chromium.org>
To: linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org
Cc: wonchung@google.com, bleung@chromium.org,
	heikki.krogerus@linux.intel.com,
	Prashant Malani <pmalani@chromium.org>,
	Alan Stern <stern@rowland.harvard.edu>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Chunfeng Yun <chunfeng.yun@mediatek.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
	Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Rajat Jain <rajatja@google.com>,
	Rikard Falkeborn <rikard.falkeborn@gmail.com>,
	Thomas Zimmermann <tzimmermann@suse.de>
Subject: [PATCH 2/4] usb: Use notifier to link Type C ports
Date: Wed, 24 Nov 2021 15:10:10 -0800	[thread overview]
Message-ID: <20211124231028.696982-3-pmalani@chromium.org> (raw)
In-Reply-To: <20211124231028.696982-1-pmalani@chromium.org>

Instead of using a helper function to register pre-existing USB ports to
a new type C port, have each port register a notifier with the Type C
connector class, and use that to perform the linking when a new Type C
port is registered.

This avoid a cyclic dependency from a later change which is needed to
link a new USB port to a pre-existing Type C port. Some context on this
is in here [1]; in summary, typec_link_ports() introduces a dependency
from typec -> usbcore. However, commit 63cd78617350 ("usb: Link the
ports to the connectors they are attached to") then introduces a
dependency from usbcore -> typec. In order to allow that commit without
creating the cyclic dependency, we use a notifier here instead of
typec_link_ports().

Since we don't need the helper typec_link_ports() function anymore,
remove it and its related functions from port-mapper code.

[1]:
https://lore.kernel.org/all/20210412213655.3776e15e@canb.auug.org.au/

Cc: Benson Leung <bleung@chromium.org>
Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Prashant Malani <pmalani@chromium.org>
---
 drivers/usb/core/hub.h          |  3 +++
 drivers/usb/core/port.c         | 18 +++++++++++++++
 drivers/usb/typec/class.c       |  5 +---
 drivers/usb/typec/class.h       |  1 -
 drivers/usb/typec/port-mapper.c | 41 ---------------------------------
 5 files changed, 22 insertions(+), 46 deletions(-)

diff --git a/drivers/usb/core/hub.h b/drivers/usb/core/hub.h
index 22ea1f4f2d66..d09a8c9c1b4e 100644
--- a/drivers/usb/core/hub.h
+++ b/drivers/usb/core/hub.h
@@ -11,6 +11,7 @@
  *  move struct usb_hub to this file.
  */
 
+#include <linux/notifier.h>
 #include <linux/usb.h>
 #include <linux/usb/ch11.h>
 #include <linux/usb/hcd.h>
@@ -89,6 +90,7 @@ struct usb_hub {
  * @is_superspeed cache super-speed status
  * @usb3_lpm_u1_permit: whether USB3 U1 LPM is permitted.
  * @usb3_lpm_u2_permit: whether USB3 U2 LPM is permitted.
+ * @typec_nb: notifier called when a Type C port is registered.
  */
 struct usb_port {
 	struct usb_device *child;
@@ -105,6 +107,7 @@ struct usb_port {
 	unsigned int is_superspeed:1;
 	unsigned int usb3_lpm_u1_permit:1;
 	unsigned int usb3_lpm_u2_permit:1;
+	struct notifier_block typec_nb;
 };
 
 #define to_usb_port(_dev) \
diff --git a/drivers/usb/core/port.c b/drivers/usb/core/port.c
index dfcca9c876c7..53a64ce76183 100644
--- a/drivers/usb/core/port.c
+++ b/drivers/usb/core/port.c
@@ -9,6 +9,7 @@
 
 #include <linux/slab.h>
 #include <linux/pm_qos.h>
+#include <linux/usb/typec.h>
 
 #include "hub.h"
 
@@ -528,6 +529,14 @@ static void find_and_link_peer(struct usb_hub *hub, int port1)
 		link_peers_report(port_dev, peer);
 }
 
+static int usb_port_link_typec_port(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+	struct usb_port *port_dev = container_of(nb, struct usb_port, typec_nb);
+
+	typec_link_port(&port_dev->dev);
+	return NOTIFY_OK;
+}
+
 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 {
 	struct usb_port *port_dev;
@@ -577,6 +586,14 @@ int usb_hub_create_port_device(struct usb_hub *hub, int port1)
 
 	find_and_link_peer(hub, port1);
 
+	/*
+	 * In some cases, the Type C port gets registered later, so
+	 * register a Type C notifier so that we can link the ports
+	 * later too.
+	 */
+	port_dev->typec_nb.notifier_call = usb_port_link_typec_port;
+	typec_port_registration_register_notify(&port_dev->typec_nb);
+
 	/*
 	 * Enable runtime pm and hold a refernce that hub_configure()
 	 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
@@ -616,6 +633,7 @@ void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
 	struct usb_port *port_dev = hub->ports[port1 - 1];
 	struct usb_port *peer;
 
+	typec_port_registration_unregister_notify(&port_dev->typec_nb);
 	peer = port_dev->peer;
 	if (peer)
 		unlink_peers(port_dev, peer);
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 14b82109b0f5..92d03eb65f12 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -2110,10 +2110,7 @@ struct typec_port *typec_register_port(struct device *parent,
 		return ERR_PTR(ret);
 	}
 
-	ret = typec_link_ports(port);
-	if (ret)
-		dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
-
+	/* Used to allow USB ports to register to this Type C port */
 	blocking_notifier_call_chain(&typec_port_registration_notifier, 0, port);
 
 	return port;
diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
index aef03eb7e152..517236935a55 100644
--- a/drivers/usb/typec/class.h
+++ b/drivers/usb/typec/class.h
@@ -79,7 +79,6 @@ extern const struct device_type typec_port_dev_type;
 extern struct class typec_mux_class;
 extern struct class typec_class;
 
-int typec_link_ports(struct typec_port *connector);
 void typec_unlink_ports(struct typec_port *connector);
 
 #endif /* __USB_TYPEC_CLASS__ */
diff --git a/drivers/usb/typec/port-mapper.c b/drivers/usb/typec/port-mapper.c
index 9b0991bdf391..5597291bd769 100644
--- a/drivers/usb/typec/port-mapper.c
+++ b/drivers/usb/typec/port-mapper.c
@@ -7,7 +7,6 @@
  */
 
 #include <linux/acpi.h>
-#include <linux/usb.h>
 #include <linux/usb/typec.h>
 
 #include "class.h"
@@ -220,46 +219,6 @@ void typec_unlink_port(struct device *port)
 }
 EXPORT_SYMBOL_GPL(typec_unlink_port);
 
-static int each_port(struct device *port, void *connector)
-{
-	struct port_node *node;
-	int ret;
-
-	node = create_port_node(port);
-	if (IS_ERR(node))
-		return PTR_ERR(node);
-
-	if (!connector_match(connector, node)) {
-		remove_port_node(node);
-		return 0;
-	}
-
-	ret = link_port(to_typec_port(connector), node);
-	if (ret) {
-		remove_port_node(node->pld);
-		return ret;
-	}
-
-	get_device(connector);
-
-	return 0;
-}
-
-int typec_link_ports(struct typec_port *con)
-{
-	int ret = 0;
-
-	con->pld = get_pld(&con->dev);
-	if (!con->pld)
-		return 0;
-
-	ret = usb_for_each_port(&con->dev, each_port);
-	if (ret)
-		typec_unlink_ports(con);
-
-	return ret;
-}
-
 void typec_unlink_ports(struct typec_port *con)
 {
 	struct port_node *node;
-- 
2.34.0.rc2.393.gf8c9666880-goog


  parent reply	other threads:[~2021-11-24 23:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-24 23:10 [PATCH 0/4] usb: Use notifier for linking " Prashant Malani
2021-11-24 23:10 ` [PATCH 1/4] usb: typec: Add port registration notifier Prashant Malani
2021-11-25  3:20   ` Prashant Malani
2021-11-24 23:10 ` Prashant Malani [this message]
2021-11-25  2:15   ` [PATCH 2/4] usb: Use notifier to link Type C ports kernel test robot
2021-11-24 23:10 ` [PATCH 3/4] usb: Link the ports to the connectors they are attached to Prashant Malani
2021-11-24 23:10 ` [PATCH 4/4] Revert "usb: Iterator for ports" Prashant Malani
2021-11-26  9:40 ` [PATCH 0/4] usb: Use notifier for linking Type C ports Heikki Krogerus
2021-11-30 11:03   ` Heikki Krogerus
2021-11-30 19:27     ` Prashant Malani
2021-12-01  9:55       ` Heikki Krogerus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211124231028.696982-3-pmalani@chromium.org \
    --to=pmalani@chromium.org \
    --cc=bhelgaas@google.com \
    --cc=bleung@chromium.org \
    --cc=chunfeng.yun@mediatek.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mchehab+huawei@kernel.org \
    --cc=rajatja@google.com \
    --cc=rikard.falkeborn@gmail.com \
    --cc=stern@rowland.harvard.edu \
    --cc=tzimmermann@suse.de \
    --cc=wonchung@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®