linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [v1,08/12] usb: roles: Add usb role switch notifier.
@ 2018-12-03 12:38 Heikki Krogerus
  0 siblings, 0 replies; 3+ messages in thread
From: Heikki Krogerus @ 2018-12-03 12:38 UTC (permalink / raw)
  To: Yu Chen
  Cc: linux-usb, devicetree, linux-kernel, suzhuangluan, kongfei,
	Greg Kroah-Hartman, Hans de Goede, Andy Shevchenko, John Stultz

On Mon, Dec 03, 2018 at 11:45:11AM +0800, Yu Chen wrote:
> This patch adds notifier for drivers want to be informed of the usb role switch.

I think in this case it would be good to explain a little for what we
need the notifier for.

> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Cc: Hans de Goede <hdegoede@redhat.com>
> Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
> Cc: John Stultz <john.stultz@linaro.org>
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> Signed-off-by: Yu Chen <chenyu56@huawei.com>
> 
> --
> v0:
> The patch is provided by Heikki Krogerus. I modified and test it
> on Hikey960 platform.

I don't think I actually made a "patch" for this. The "diff" I made
showing the idea does not count, so you should not sign off the patch
with my address.

In this case I believe something like "Suggested-by" should be used
instead of SoB tag:

        Suggested-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

Side note: If this was from me, then there should be "from" line with
my address as well.

> --
> ---
>  drivers/usb/common/roles.c | 20 +++++++++++++++++++-
>  include/linux/usb/role.h   | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 65 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/common/roles.c b/drivers/usb/common/roles.c
> index 0f48090c5c30..bfd7a988b64c 100644
> --- a/drivers/usb/common/roles.c
> +++ b/drivers/usb/common/roles.c
> @@ -21,6 +21,7 @@ struct usb_role_switch {
>  	struct device dev;
>  	struct mutex lock; /* device lock*/
>  	enum usb_role role;
> +	struct blocking_notifier_head nh;
>  
>  	/* From descriptor */
>  	struct device *usb2_port;
> @@ -50,8 +51,10 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
>  	mutex_lock(&sw->lock);
>  
>  	ret = sw->set(sw->dev.parent, role);
> -	if (!ret)
> +	if (!ret) {
>  		sw->role = role;
> +		blocking_notifier_call_chain(&sw->nh, role, NULL);
> +	}
>  
>  	mutex_unlock(&sw->lock);
>  
> @@ -59,6 +62,20 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
>  }
>  EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
>  
> +int usb_role_switch_register_notifier(struct usb_role_switch *sw,
> +				      struct notifier_block *nb)
> +{
> +	return blocking_notifier_chain_register(&sw->nh, nb);
> +}
> +EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier);
> +
> +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
> +					struct notifier_block *nb)
> +{
> +	return blocking_notifier_chain_unregister(&sw->nh, nb);
> +}
> +EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier);
> +
>  /**
>   * usb_role_switch_get_role - Get the USB role for a switch
>   * @sw: USB role switch
> @@ -297,6 +314,7 @@ usb_role_switch_register(struct device *parent,
>  		return ERR_PTR(-ENOMEM);
>  
>  	mutex_init(&sw->lock);
> +	BLOCKING_INIT_NOTIFIER_HEAD(&sw->nh);
>  
>  	sw->allow_userspace_control = desc->allow_userspace_control;
>  	sw->usb2_port = desc->usb2_port;
> diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h
> index edc51be4a77c..c97d4be91ada 100644
> --- a/include/linux/usb/role.h
> +++ b/include/linux/usb/role.h
> @@ -40,6 +40,8 @@ struct usb_role_switch_desc {
>  	bool allow_userspace_control;
>  };
>  
> +
> +#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
>  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(struct device *dev);
> @@ -49,5 +51,49 @@ struct usb_role_switch *
>  usb_role_switch_register(struct device *parent,
>  			 const struct usb_role_switch_desc *desc);
>  void usb_role_switch_unregister(struct usb_role_switch *sw);
> +int usb_role_switch_register_notifier(struct usb_role_switch *sw,
> +				      struct notifier_block *nb);
> +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
> +					struct notifier_block *nb);
> +#else
> +static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
> +		enum usb_role role)
> +{
> +	return 0;
> +}
> +
> +static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
> +{
> +	return USB_ROLE_NONE;
> +}
> +
> +static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
> +{
> +	return ERR_PTR(-ENODEV);
> +}
> +
> +static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
> +
> +static inline struct usb_role_switch *
> +usb_role_switch_register(struct device *parent,
> +			 const struct usb_role_switch_desc *desc)
> +{
> +	return ERR_PTR(-ENODEV);
> +}
> +
> +static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
> +
> +static int usb_role_switch_register_notifier(struct usb_role_switch *sw,
> +				      struct notifier_block *nb)
> +{
> +	return -ENODEV;
> +}
> +
> +static int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
> +					struct notifier_block *nb)
> +{
> +	return -ENODEV;
> +}
> +#endif
>  
>  #endif /* __LINUX_USB_ROLE_H */
> -- 
> 2.15.0-rc2

thanks,

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

* [v1,08/12] usb: roles: Add usb role switch notifier.
@ 2018-12-04  1:07 Yu Chen
  0 siblings, 0 replies; 3+ messages in thread
From: Yu Chen @ 2018-12-04  1:07 UTC (permalink / raw)
  To: Heikki Krogerus
  Cc: wangbinghui, linux-usb, devicetree, linux-kernel, suzhuangluan,
	kongfei, Greg Kroah-Hartman, Hans de Goede, Andy Shevchenko,
	John Stultz

On 2018/12/3 20:38, Heikki Krogerus wrote:
> On Mon, Dec 03, 2018 at 11:45:11AM +0800, Yu Chen wrote:
>> This patch adds notifier for drivers want to be informed of the usb role switch.
> 
> I think in this case it would be good to explain a little for what we
> need the notifier for.
> 
>> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
>> Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
>> Cc: Hans de Goede <hdegoede@redhat.com>
>> Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
>> Cc: John Stultz <john.stultz@linaro.org>
>> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
>> Signed-off-by: Yu Chen <chenyu56@huawei.com>
>>
>> --
>> v0:
>> The patch is provided by Heikki Krogerus. I modified and test it
>> on Hikey960 platform.
> 
> I don't think I actually made a "patch" for this. The "diff" I made
> showing the idea does not count, so you should not sign off the patch
> with my address.
> 
> In this case I believe something like "Suggested-by" should be used
> instead of SoB tag:
> 
>         Suggested-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> 
> Side note: If this was from me, then there should be "from" line with
> my address as well.
> 

OK. Thanks!
>> --
>> ---
>>  drivers/usb/common/roles.c | 20 +++++++++++++++++++-
>>  include/linux/usb/role.h   | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 65 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/usb/common/roles.c b/drivers/usb/common/roles.c
>> index 0f48090c5c30..bfd7a988b64c 100644
>> --- a/drivers/usb/common/roles.c
>> +++ b/drivers/usb/common/roles.c
>> @@ -21,6 +21,7 @@ struct usb_role_switch {
>>  	struct device dev;
>>  	struct mutex lock; /* device lock*/
>>  	enum usb_role role;
>> +	struct blocking_notifier_head nh;
>>  
>>  	/* From descriptor */
>>  	struct device *usb2_port;
>> @@ -50,8 +51,10 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
>>  	mutex_lock(&sw->lock);
>>  
>>  	ret = sw->set(sw->dev.parent, role);
>> -	if (!ret)
>> +	if (!ret) {
>>  		sw->role = role;
>> +		blocking_notifier_call_chain(&sw->nh, role, NULL);
>> +	}
>>  
>>  	mutex_unlock(&sw->lock);
>>  
>> @@ -59,6 +62,20 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
>>  }
>>  EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
>>  
>> +int usb_role_switch_register_notifier(struct usb_role_switch *sw,
>> +				      struct notifier_block *nb)
>> +{
>> +	return blocking_notifier_chain_register(&sw->nh, nb);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier);
>> +
>> +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
>> +					struct notifier_block *nb)
>> +{
>> +	return blocking_notifier_chain_unregister(&sw->nh, nb);
>> +}
>> +EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier);
>> +
>>  /**
>>   * usb_role_switch_get_role - Get the USB role for a switch
>>   * @sw: USB role switch
>> @@ -297,6 +314,7 @@ usb_role_switch_register(struct device *parent,
>>  		return ERR_PTR(-ENOMEM);
>>  
>>  	mutex_init(&sw->lock);
>> +	BLOCKING_INIT_NOTIFIER_HEAD(&sw->nh);
>>  
>>  	sw->allow_userspace_control = desc->allow_userspace_control;
>>  	sw->usb2_port = desc->usb2_port;
>> diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h
>> index edc51be4a77c..c97d4be91ada 100644
>> --- a/include/linux/usb/role.h
>> +++ b/include/linux/usb/role.h
>> @@ -40,6 +40,8 @@ struct usb_role_switch_desc {
>>  	bool allow_userspace_control;
>>  };
>>  
>> +
>> +#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
>>  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(struct device *dev);
>> @@ -49,5 +51,49 @@ struct usb_role_switch *
>>  usb_role_switch_register(struct device *parent,
>>  			 const struct usb_role_switch_desc *desc);
>>  void usb_role_switch_unregister(struct usb_role_switch *sw);
>> +int usb_role_switch_register_notifier(struct usb_role_switch *sw,
>> +				      struct notifier_block *nb);
>> +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
>> +					struct notifier_block *nb);
>> +#else
>> +static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
>> +		enum usb_role role)
>> +{
>> +	return 0;
>> +}
>> +
>> +static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
>> +{
>> +	return USB_ROLE_NONE;
>> +}
>> +
>> +static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
>> +{
>> +	return ERR_PTR(-ENODEV);
>> +}
>> +
>> +static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
>> +
>> +static inline struct usb_role_switch *
>> +usb_role_switch_register(struct device *parent,
>> +			 const struct usb_role_switch_desc *desc)
>> +{
>> +	return ERR_PTR(-ENODEV);
>> +}
>> +
>> +static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
>> +
>> +static int usb_role_switch_register_notifier(struct usb_role_switch *sw,
>> +				      struct notifier_block *nb)
>> +{
>> +	return -ENODEV;
>> +}
>> +
>> +static int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
>> +					struct notifier_block *nb)
>> +{
>> +	return -ENODEV;
>> +}
>> +#endif
>>  
>>  #endif /* __LINUX_USB_ROLE_H */
>> -- 
>> 2.15.0-rc2
> 
> thanks,
>

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

* [v1,08/12] usb: roles: Add usb role switch notifier.
@ 2018-12-03  3:45 Yu Chen
  0 siblings, 0 replies; 3+ messages in thread
From: Yu Chen @ 2018-12-03  3:45 UTC (permalink / raw)
  To: linux-usb, devicetree, linux-kernel
  Cc: suzhuangluan, kongfei, Yu Chen, Greg Kroah-Hartman,
	Heikki Krogerus, Hans de Goede, Andy Shevchenko, John Stultz

This patch adds notifier for drivers want to be informed of the usb role switch.

Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: John Stultz <john.stultz@linaro.org>
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Yu Chen <chenyu56@huawei.com>
---
v0:
The patch is provided by Heikki Krogerus. I modified and test it
on Hikey960 platform.
--
---
 drivers/usb/common/roles.c | 20 +++++++++++++++++++-
 include/linux/usb/role.h   | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/common/roles.c b/drivers/usb/common/roles.c
index 0f48090c5c30..bfd7a988b64c 100644
--- a/drivers/usb/common/roles.c
+++ b/drivers/usb/common/roles.c
@@ -21,6 +21,7 @@ struct usb_role_switch {
 	struct device dev;
 	struct mutex lock; /* device lock*/
 	enum usb_role role;
+	struct blocking_notifier_head nh;
 
 	/* From descriptor */
 	struct device *usb2_port;
@@ -50,8 +51,10 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 	mutex_lock(&sw->lock);
 
 	ret = sw->set(sw->dev.parent, role);
-	if (!ret)
+	if (!ret) {
 		sw->role = role;
+		blocking_notifier_call_chain(&sw->nh, role, NULL);
+	}
 
 	mutex_unlock(&sw->lock);
 
@@ -59,6 +62,20 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
 }
 EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
 
+int usb_role_switch_register_notifier(struct usb_role_switch *sw,
+				      struct notifier_block *nb)
+{
+	return blocking_notifier_chain_register(&sw->nh, nb);
+}
+EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier);
+
+int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
+					struct notifier_block *nb)
+{
+	return blocking_notifier_chain_unregister(&sw->nh, nb);
+}
+EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier);
+
 /**
  * usb_role_switch_get_role - Get the USB role for a switch
  * @sw: USB role switch
@@ -297,6 +314,7 @@ usb_role_switch_register(struct device *parent,
 		return ERR_PTR(-ENOMEM);
 
 	mutex_init(&sw->lock);
+	BLOCKING_INIT_NOTIFIER_HEAD(&sw->nh);
 
 	sw->allow_userspace_control = desc->allow_userspace_control;
 	sw->usb2_port = desc->usb2_port;
diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h
index edc51be4a77c..c97d4be91ada 100644
--- a/include/linux/usb/role.h
+++ b/include/linux/usb/role.h
@@ -40,6 +40,8 @@ struct usb_role_switch_desc {
 	bool allow_userspace_control;
 };
 
+
+#if IS_ENABLED(CONFIG_USB_ROLE_SWITCH)
 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(struct device *dev);
@@ -49,5 +51,49 @@ struct usb_role_switch *
 usb_role_switch_register(struct device *parent,
 			 const struct usb_role_switch_desc *desc);
 void usb_role_switch_unregister(struct usb_role_switch *sw);
+int usb_role_switch_register_notifier(struct usb_role_switch *sw,
+				      struct notifier_block *nb);
+int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
+					struct notifier_block *nb);
+#else
+static inline int usb_role_switch_set_role(struct usb_role_switch *sw,
+		enum usb_role role)
+{
+	return 0;
+}
+
+static inline enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
+{
+	return USB_ROLE_NONE;
+}
+
+static inline struct usb_role_switch *usb_role_switch_get(struct device *dev)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+static inline void usb_role_switch_put(struct usb_role_switch *sw) { }
+
+static inline struct usb_role_switch *
+usb_role_switch_register(struct device *parent,
+			 const struct usb_role_switch_desc *desc)
+{
+	return ERR_PTR(-ENODEV);
+}
+
+static inline void usb_role_switch_unregister(struct usb_role_switch *sw) { }
+
+static int usb_role_switch_register_notifier(struct usb_role_switch *sw,
+				      struct notifier_block *nb)
+{
+	return -ENODEV;
+}
+
+static int usb_role_switch_unregister_notifier(struct usb_role_switch *sw,
+					struct notifier_block *nb)
+{
+	return -ENODEV;
+}
+#endif
 
 #endif /* __LINUX_USB_ROLE_H */

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

end of thread, other threads:[~2018-12-04  1:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-03 12:38 [v1,08/12] usb: roles: Add usb role switch notifier Heikki Krogerus
  -- strict thread matches above, loose matches on Subject: below --
2018-12-04  1:07 Yu Chen
2018-12-03  3:45 Yu Chen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).