public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [3/5] usb: roles: add API to get usb_role_switch by node
@ 2019-03-11  5:36 Chunfeng Yun
  0 siblings, 0 replies; 3+ messages in thread
From: Chunfeng Yun @ 2019-03-11  5:36 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Rob Herring, Greg Kroah-Hartman, Heikki Krogerus, Mark Rutland,
	Matthias Brugger, Adam Thomson, Li Jun, Badhri Jagan Sridharan,
	Hans de Goede, Min Guo, devicetree, Linux Kernel Mailing List,
	USB, linux-arm Mailing List,
	moderated list:ARM/Mediatek SoC support

Hi,

On Fri, 2019-03-08 at 08:52 +0200, Andy Shevchenko wrote:
> On Fri, Mar 8, 2019 at 8:14 AM Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
> >
> > Add usb_role_switch_get_by_node() to make easier to get
> > usb_role_switch by node which register it.
> > It's useful when there is not device_connection registered
> > between two drivers and only knows the node which register
> > usb_role_switch.
> 
> > +static int __switch_match_node(struct device *dev, const void *node)
> > +{
> > +       return dev->parent->of_node == (const struct device_node *)node;
> > +}
> 
> Hmm... Shouldn't be slightly better to compare fwnode instead?
> 
Using fwnode is indeed suitable for more cases,
I find that there are many functions named xx_by_node using node, but
not fwnode, is there any rules about choice between device_node and
fwnode_handle?

Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread
* [3/5] usb: roles: add API to get usb_role_switch by node
@ 2019-03-08  6:52 Andy Shevchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2019-03-08  6:52 UTC (permalink / raw)
  To: Chunfeng Yun
  Cc: Rob Herring, Greg Kroah-Hartman, Heikki Krogerus, Mark Rutland,
	Matthias Brugger, Adam Thomson, Li Jun, Badhri Jagan Sridharan,
	Hans de Goede, Min Guo, devicetree, Linux Kernel Mailing List,
	USB, linux-arm Mailing List,
	moderated list:ARM/Mediatek SoC support

On Fri, Mar 8, 2019 at 8:14 AM Chunfeng Yun <chunfeng.yun@mediatek.com> wrote:
>
> Add usb_role_switch_get_by_node() to make easier to get
> usb_role_switch by node which register it.
> It's useful when there is not device_connection registered
> between two drivers and only knows the node which register
> usb_role_switch.

> +static int __switch_match_node(struct device *dev, const void *node)
> +{
> +       return dev->parent->of_node == (const struct device_node *)node;
> +}

Hmm... Shouldn't be slightly better to compare fwnode instead?

^ permalink raw reply	[flat|nested] 3+ messages in thread
* [3/5] usb: roles: add API to get usb_role_switch by node
@ 2019-03-08  6:13 Chunfeng Yun
  0 siblings, 0 replies; 3+ messages in thread
From: Chunfeng Yun @ 2019-03-08  6:13 UTC (permalink / raw)
  To: Rob Herring, Greg Kroah-Hartman, Heikki Krogerus
  Cc: Mark Rutland, Chunfeng Yun, Matthias Brugger, Adam Thomson,
	Li Jun, Badhri Jagan Sridharan, Hans de Goede, Andy Shevchenko,
	Min Guo, devicetree, linux-kernel, linux-usb, linux-arm-kernel,
	linux-mediatek

Add usb_role_switch_get_by_node() to make easier to get
usb_role_switch by node which register it.
It's useful when there is not device_connection registered
between two drivers and only knows the node which register
usb_role_switch.

Signed-off-by: Chunfeng Yun <chunfeng.yun@mediatek.com>
---
 drivers/usb/roles/class.c | 30 ++++++++++++++++++++++++++++++
 include/linux/usb/role.h  |  1 +
 2 files changed, 31 insertions(+)

diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c
index 99116af07f1d..284b19856dc4 100644
--- a/drivers/usb/roles/class.c
+++ b/drivers/usb/roles/class.c
@@ -11,6 +11,7 @@
 #include <linux/device.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
+#include <linux/of.h>
 #include <linux/slab.h>
 
 static struct class *role_class;
@@ -121,6 +122,35 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(usb_role_switch_get);
 
+static int __switch_match_node(struct device *dev, const void *node)
+{
+	return dev->parent->of_node == (const struct device_node *)node;
+}
+
+/**
+ * usb_role_switch_get_by_node - Find USB role switch by it's parent node
+ * @node: The node that register USB role switch
+ *
+ * Finds and returns role switch registered by @node. The reference count
+ * for the found switch is incremented.
+ */
+struct usb_role_switch *usb_role_switch_get_by_node(struct device_node *node)
+{
+	struct usb_role_switch *sw;
+	struct device *dev;
+
+	dev = class_find_device(role_class, NULL, node,
+				__switch_match_node);
+	if (!dev)
+		return ERR_PTR(-EPROBE_DEFER);
+
+	sw = to_role_switch(dev);
+	WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
+
+	return sw;
+}
+EXPORT_SYMBOL_GPL(usb_role_switch_get_by_node);
+
 /**
  * usb_role_switch_put - Release handle to a switch
  * @sw: USB Role Switch
diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h
index edc51be4a77c..056498b83dee 100644
--- a/include/linux/usb/role.h
+++ b/include/linux/usb/role.h
@@ -42,6 +42,7 @@ struct usb_role_switch_desc {
 
 int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role);
 enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw);
+struct usb_role_switch *usb_role_switch_get_by_node(struct device_node *node);
 struct usb_role_switch *usb_role_switch_get(struct device *dev);
 void usb_role_switch_put(struct usb_role_switch *sw);
 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-03-11  5:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-11  5:36 [3/5] usb: roles: add API to get usb_role_switch by node Chunfeng Yun
  -- strict thread matches above, loose matches on Subject: below --
2019-03-08  6:52 Andy Shevchenko
2019-03-08  6:13 Chunfeng Yun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox