Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Lu Baolu


On 03/04/2016 12:15 AM, Greg Kroah-Hartman wrote:
> On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
>> +#if IS_ENABLED(CONFIG_USB_MUX)
>> +extern int usb_mux_register(struct usb_mux_dev *mux);
>> +extern int usb_mux_unregister(struct device *dev);
>> +extern struct usb_mux_dev *usb_mux_get_dev(struct device *dev);
> This api needs a lot of work, for such a "tiny" api, it's obviously
> pretty incorrect.  Please fix it up to be sane...

Sure.

>
>
>



Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Lu Baolu


On 03/04/2016 12:15 AM, Greg Kroah-Hartman wrote:
> On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
>> +#if IS_ENABLED(CONFIG_USB_MUX)
>> +extern int usb_mux_register(struct usb_mux_dev *mux);
>> +extern int usb_mux_unregister(struct device *dev);
>> +extern struct usb_mux_dev *usb_mux_get_dev(struct device *dev);
> This api needs a lot of work, for such a "tiny" api, it's obviously
> pretty incorrect.  Please fix it up to be sane...

Sure.

>
>
>



Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Lu Baolu


On 03/04/2016 12:10 AM, Greg Kroah-Hartman wrote:
> On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
>> Several Intel PCHs and SOCs have an internal mux that is used to
>> share one USB port between device controller and host controller.
>>
>> A usb port mux could be abstracted as the following elements:
>> 1) mux state: HOST or PERIPHERAL;
>> 2) an extcon cable which triggers the change of mux state between
>>HOST and PERIPHERAL;
>> 3) The required action to do the real port switch.
>>
>> This patch adds the common code to handle usb port mux. With this
>> common code, the individual mux driver, which always is platform
>> dependent, could focus on the real operation of mux switch.
>>
>> Signed-off-by: Lu Baolu 
>> Reviewed-by: Heikki Krogerus 
>> Reviewed-by: Felipe Balbi 
>> ---
>>  MAINTAINERS   |   7 ++
>>  drivers/usb/misc/Kconfig  |   4 ++
>>  drivers/usb/misc/Makefile |   2 +
>>  drivers/usb/misc/mux.c| 172 
>> ++
>>  include/linux/usb/mux.h   |  71 +++
>>  5 files changed, 256 insertions(+)
>>  create mode 100644 drivers/usb/misc/mux.c
>>  create mode 100644 include/linux/usb/mux.h
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index d894ee2..45f1e1e 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -11389,6 +11389,13 @@ T:  git 
>> git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
>>  S:  Maintained
>>  F:  drivers/usb/phy/
>>  
>> +USB PORT MUX DRIVER
>> +M:  Lu Baolu 
>> +L:  linux-...@vger.kernel.org
>> +S:  Supported
>> +F:  drivers/usb/misc/mux.c
>> +F:  include/linux/usb/mux.h
>> +
>>  USB PRINTER DRIVER (usblp)
>>  M:  Pete Zaitcev 
>>  L:  linux-...@vger.kernel.org
>> diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
>> index f7a7fc2..6496d17 100644
>> --- a/drivers/usb/misc/Kconfig
>> +++ b/drivers/usb/misc/Kconfig
>> @@ -3,6 +3,10 @@
>>  #
>>  comment "USB Miscellaneous drivers"
>>  
>> +config USB_MUX
>> +select EXTCON
>> +def_bool n
>> +
>>  config USB_EMI62
>>  tristate "EMI 6|2m USB Audio interface support"
>>  ---help---
>> diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
>> index 45fd4ac..fd79dd5 100644
>> --- a/drivers/usb/misc/Makefile
>> +++ b/drivers/usb/misc/Makefile
>> @@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY) += chaoskey.o
>>  
>>  obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/
>>  obj-$(CONFIG_USB_LINK_LAYER_TEST)   += lvstest.o
>> +
>> +obj-$(CONFIG_USB_MUX)   += mux.o
> "mux.ko" is a _VERY_ generic name of a kernel module.  Please make this
> much more specific to your chip as this will not work for any other
> platform that has this type of functionality.
>
> Same goes for all of your exported symbols.

Sure. I will change the module name to "intel-mux", and change
the exported symbols to "intel_usb_mux_*".

>
>
>> diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
>> new file mode 100644
>> index 000..e353fff
>> --- /dev/null
>> +++ b/drivers/usb/misc/mux.c
>> @@ -0,0 +1,172 @@
>> +/**
>> + * mux.c - USB Port Mux support
>> + *
>> + * Copyright (C) 2016 Intel Corporation
>> + *
>> + * Author: Lu Baolu 
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License version 2 as
>> + * published by the Free Software Foundation.
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +static int usb_mux_change_state(struct usb_mux *mux, int state)
>> +{
>> +int ret;
>> +struct usb_mux_dev *umdev = mux->umdev;
>> +
>> +dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
>> +"mutex is unlocked\n");
>> +
>> +mux->mux_state = state;
>> +
>> +if (mux->mux_state)
>> +ret = umdev->cable_set_cb(umdev);
>> +else
>> +ret = umdev->cable_unset_cb(umdev);
>> +
>> +return ret;
>> +}
>> +
>> +static int usb_mux_notifier(struct notifier_block *nb,
>> +unsigned long event, void *ptr)
>> +{
>> +struct usb_mux *mux;
>> +int state;
>> +int ret = NOTIFY_DONE;
>> +
>> +mux = container_of(nb, struct usb_mux, nb);
>> +
>> +state = extcon_get_cable_state(mux->obj.edev,
>> +mux->umdev->cable_name);
>> +
>> +if (mux->mux_state == -1 || mux->mux_state != state) {
>> +mutex_lock(>mux_mutex);
>> +ret = usb_mux_change_state(mux, state);
>> +mutex_unlock(>mux_mutex);
>> +}
>> +
>> +return ret;
>> +}
>> +
>> +static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
>> +size_t len, loff_t *offset)
>> +{
>> +struct usb_mux *mux = file->private_data;
>> +char output_buf[16];
>> +
>> +memset(output_buf, 0, sizeof(output_buf));
>> +if (mux->mux_state)

Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Lu Baolu


On 03/04/2016 12:10 AM, Greg Kroah-Hartman wrote:
> On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
>> Several Intel PCHs and SOCs have an internal mux that is used to
>> share one USB port between device controller and host controller.
>>
>> A usb port mux could be abstracted as the following elements:
>> 1) mux state: HOST or PERIPHERAL;
>> 2) an extcon cable which triggers the change of mux state between
>>HOST and PERIPHERAL;
>> 3) The required action to do the real port switch.
>>
>> This patch adds the common code to handle usb port mux. With this
>> common code, the individual mux driver, which always is platform
>> dependent, could focus on the real operation of mux switch.
>>
>> Signed-off-by: Lu Baolu 
>> Reviewed-by: Heikki Krogerus 
>> Reviewed-by: Felipe Balbi 
>> ---
>>  MAINTAINERS   |   7 ++
>>  drivers/usb/misc/Kconfig  |   4 ++
>>  drivers/usb/misc/Makefile |   2 +
>>  drivers/usb/misc/mux.c| 172 
>> ++
>>  include/linux/usb/mux.h   |  71 +++
>>  5 files changed, 256 insertions(+)
>>  create mode 100644 drivers/usb/misc/mux.c
>>  create mode 100644 include/linux/usb/mux.h
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index d894ee2..45f1e1e 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -11389,6 +11389,13 @@ T:  git 
>> git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
>>  S:  Maintained
>>  F:  drivers/usb/phy/
>>  
>> +USB PORT MUX DRIVER
>> +M:  Lu Baolu 
>> +L:  linux-...@vger.kernel.org
>> +S:  Supported
>> +F:  drivers/usb/misc/mux.c
>> +F:  include/linux/usb/mux.h
>> +
>>  USB PRINTER DRIVER (usblp)
>>  M:  Pete Zaitcev 
>>  L:  linux-...@vger.kernel.org
>> diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
>> index f7a7fc2..6496d17 100644
>> --- a/drivers/usb/misc/Kconfig
>> +++ b/drivers/usb/misc/Kconfig
>> @@ -3,6 +3,10 @@
>>  #
>>  comment "USB Miscellaneous drivers"
>>  
>> +config USB_MUX
>> +select EXTCON
>> +def_bool n
>> +
>>  config USB_EMI62
>>  tristate "EMI 6|2m USB Audio interface support"
>>  ---help---
>> diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
>> index 45fd4ac..fd79dd5 100644
>> --- a/drivers/usb/misc/Makefile
>> +++ b/drivers/usb/misc/Makefile
>> @@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY) += chaoskey.o
>>  
>>  obj-$(CONFIG_USB_SISUSBVGA) += sisusbvga/
>>  obj-$(CONFIG_USB_LINK_LAYER_TEST)   += lvstest.o
>> +
>> +obj-$(CONFIG_USB_MUX)   += mux.o
> "mux.ko" is a _VERY_ generic name of a kernel module.  Please make this
> much more specific to your chip as this will not work for any other
> platform that has this type of functionality.
>
> Same goes for all of your exported symbols.

Sure. I will change the module name to "intel-mux", and change
the exported symbols to "intel_usb_mux_*".

>
>
>> diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
>> new file mode 100644
>> index 000..e353fff
>> --- /dev/null
>> +++ b/drivers/usb/misc/mux.c
>> @@ -0,0 +1,172 @@
>> +/**
>> + * mux.c - USB Port Mux support
>> + *
>> + * Copyright (C) 2016 Intel Corporation
>> + *
>> + * Author: Lu Baolu 
>> + *
>> + * This program is free software; you can redistribute it and/or modify
>> + * it under the terms of the GNU General Public License version 2 as
>> + * published by the Free Software Foundation.
>> + */
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +static int usb_mux_change_state(struct usb_mux *mux, int state)
>> +{
>> +int ret;
>> +struct usb_mux_dev *umdev = mux->umdev;
>> +
>> +dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
>> +"mutex is unlocked\n");
>> +
>> +mux->mux_state = state;
>> +
>> +if (mux->mux_state)
>> +ret = umdev->cable_set_cb(umdev);
>> +else
>> +ret = umdev->cable_unset_cb(umdev);
>> +
>> +return ret;
>> +}
>> +
>> +static int usb_mux_notifier(struct notifier_block *nb,
>> +unsigned long event, void *ptr)
>> +{
>> +struct usb_mux *mux;
>> +int state;
>> +int ret = NOTIFY_DONE;
>> +
>> +mux = container_of(nb, struct usb_mux, nb);
>> +
>> +state = extcon_get_cable_state(mux->obj.edev,
>> +mux->umdev->cable_name);
>> +
>> +if (mux->mux_state == -1 || mux->mux_state != state) {
>> +mutex_lock(>mux_mutex);
>> +ret = usb_mux_change_state(mux, state);
>> +mutex_unlock(>mux_mutex);
>> +}
>> +
>> +return ret;
>> +}
>> +
>> +static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
>> +size_t len, loff_t *offset)
>> +{
>> +struct usb_mux *mux = file->private_data;
>> +char output_buf[16];
>> +
>> +memset(output_buf, 0, sizeof(output_buf));
>> +if (mux->mux_state)
>> +strcpy(output_buf, "host\n");
>> +else
>> +strcpy(output_buf, "peripheral\n");
>> +
>> +return 

Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Greg Kroah-Hartman
On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
> +#if IS_ENABLED(CONFIG_USB_MUX)
> +extern int usb_mux_register(struct usb_mux_dev *mux);
> +extern int usb_mux_unregister(struct device *dev);
> +extern struct usb_mux_dev *usb_mux_get_dev(struct device *dev);

This api needs a lot of work, for such a "tiny" api, it's obviously
pretty incorrect.  Please fix it up to be sane...




Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Greg Kroah-Hartman
On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
> +#if IS_ENABLED(CONFIG_USB_MUX)
> +extern int usb_mux_register(struct usb_mux_dev *mux);
> +extern int usb_mux_unregister(struct device *dev);
> +extern struct usb_mux_dev *usb_mux_get_dev(struct device *dev);

This api needs a lot of work, for such a "tiny" api, it's obviously
pretty incorrect.  Please fix it up to be sane...




Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Greg Kroah-Hartman
On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
> Several Intel PCHs and SOCs have an internal mux that is used to
> share one USB port between device controller and host controller.
> 
> A usb port mux could be abstracted as the following elements:
> 1) mux state: HOST or PERIPHERAL;
> 2) an extcon cable which triggers the change of mux state between
>HOST and PERIPHERAL;
> 3) The required action to do the real port switch.
> 
> This patch adds the common code to handle usb port mux. With this
> common code, the individual mux driver, which always is platform
> dependent, could focus on the real operation of mux switch.
> 
> Signed-off-by: Lu Baolu 
> Reviewed-by: Heikki Krogerus 
> Reviewed-by: Felipe Balbi 
> ---
>  MAINTAINERS   |   7 ++
>  drivers/usb/misc/Kconfig  |   4 ++
>  drivers/usb/misc/Makefile |   2 +
>  drivers/usb/misc/mux.c| 172 
> ++
>  include/linux/usb/mux.h   |  71 +++
>  5 files changed, 256 insertions(+)
>  create mode 100644 drivers/usb/misc/mux.c
>  create mode 100644 include/linux/usb/mux.h
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index d894ee2..45f1e1e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -11389,6 +11389,13 @@ T:   git 
> git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
>  S:   Maintained
>  F:   drivers/usb/phy/
>  
> +USB PORT MUX DRIVER
> +M:   Lu Baolu 
> +L:   linux-...@vger.kernel.org
> +S:   Supported
> +F:   drivers/usb/misc/mux.c
> +F:   include/linux/usb/mux.h
> +
>  USB PRINTER DRIVER (usblp)
>  M:   Pete Zaitcev 
>  L:   linux-...@vger.kernel.org
> diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
> index f7a7fc2..6496d17 100644
> --- a/drivers/usb/misc/Kconfig
> +++ b/drivers/usb/misc/Kconfig
> @@ -3,6 +3,10 @@
>  #
>  comment "USB Miscellaneous drivers"
>  
> +config USB_MUX
> + select EXTCON
> + def_bool n
> +
>  config USB_EMI62
>   tristate "EMI 6|2m USB Audio interface support"
>   ---help---
> diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
> index 45fd4ac..fd79dd5 100644
> --- a/drivers/usb/misc/Makefile
> +++ b/drivers/usb/misc/Makefile
> @@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY)  += chaoskey.o
>  
>  obj-$(CONFIG_USB_SISUSBVGA)  += sisusbvga/
>  obj-$(CONFIG_USB_LINK_LAYER_TEST)+= lvstest.o
> +
> +obj-$(CONFIG_USB_MUX)+= mux.o

"mux.ko" is a _VERY_ generic name of a kernel module.  Please make this
much more specific to your chip as this will not work for any other
platform that has this type of functionality.

Same goes for all of your exported symbols.



> diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
> new file mode 100644
> index 000..e353fff
> --- /dev/null
> +++ b/drivers/usb/misc/mux.c
> @@ -0,0 +1,172 @@
> +/**
> + * mux.c - USB Port Mux support
> + *
> + * Copyright (C) 2016 Intel Corporation
> + *
> + * Author: Lu Baolu 
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +static int usb_mux_change_state(struct usb_mux *mux, int state)
> +{
> + int ret;
> + struct usb_mux_dev *umdev = mux->umdev;
> +
> + dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
> + "mutex is unlocked\n");
> +
> + mux->mux_state = state;
> +
> + if (mux->mux_state)
> + ret = umdev->cable_set_cb(umdev);
> + else
> + ret = umdev->cable_unset_cb(umdev);
> +
> + return ret;
> +}
> +
> +static int usb_mux_notifier(struct notifier_block *nb,
> + unsigned long event, void *ptr)
> +{
> + struct usb_mux *mux;
> + int state;
> + int ret = NOTIFY_DONE;
> +
> + mux = container_of(nb, struct usb_mux, nb);
> +
> + state = extcon_get_cable_state(mux->obj.edev,
> + mux->umdev->cable_name);
> +
> + if (mux->mux_state == -1 || mux->mux_state != state) {
> + mutex_lock(>mux_mutex);
> + ret = usb_mux_change_state(mux, state);
> + mutex_unlock(>mux_mutex);
> + }
> +
> + return ret;
> +}
> +
> +static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
> + size_t len, loff_t *offset)
> +{
> + struct usb_mux *mux = file->private_data;
> + char output_buf[16];
> +
> + memset(output_buf, 0, sizeof(output_buf));
> + if (mux->mux_state)
> + strcpy(output_buf, "host\n");
> + else
> + strcpy(output_buf, "peripheral\n");
> +
> + return simple_read_from_buffer(user_buf, len, offset,
> + output_buf, strlen(output_buf));
> +}
> +
> +static ssize_t 

Re: [PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-03 Thread Greg Kroah-Hartman
On Thu, Mar 03, 2016 at 02:37:40PM +0800, Lu Baolu wrote:
> Several Intel PCHs and SOCs have an internal mux that is used to
> share one USB port between device controller and host controller.
> 
> A usb port mux could be abstracted as the following elements:
> 1) mux state: HOST or PERIPHERAL;
> 2) an extcon cable which triggers the change of mux state between
>HOST and PERIPHERAL;
> 3) The required action to do the real port switch.
> 
> This patch adds the common code to handle usb port mux. With this
> common code, the individual mux driver, which always is platform
> dependent, could focus on the real operation of mux switch.
> 
> Signed-off-by: Lu Baolu 
> Reviewed-by: Heikki Krogerus 
> Reviewed-by: Felipe Balbi 
> ---
>  MAINTAINERS   |   7 ++
>  drivers/usb/misc/Kconfig  |   4 ++
>  drivers/usb/misc/Makefile |   2 +
>  drivers/usb/misc/mux.c| 172 
> ++
>  include/linux/usb/mux.h   |  71 +++
>  5 files changed, 256 insertions(+)
>  create mode 100644 drivers/usb/misc/mux.c
>  create mode 100644 include/linux/usb/mux.h
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index d894ee2..45f1e1e 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -11389,6 +11389,13 @@ T:   git 
> git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
>  S:   Maintained
>  F:   drivers/usb/phy/
>  
> +USB PORT MUX DRIVER
> +M:   Lu Baolu 
> +L:   linux-...@vger.kernel.org
> +S:   Supported
> +F:   drivers/usb/misc/mux.c
> +F:   include/linux/usb/mux.h
> +
>  USB PRINTER DRIVER (usblp)
>  M:   Pete Zaitcev 
>  L:   linux-...@vger.kernel.org
> diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
> index f7a7fc2..6496d17 100644
> --- a/drivers/usb/misc/Kconfig
> +++ b/drivers/usb/misc/Kconfig
> @@ -3,6 +3,10 @@
>  #
>  comment "USB Miscellaneous drivers"
>  
> +config USB_MUX
> + select EXTCON
> + def_bool n
> +
>  config USB_EMI62
>   tristate "EMI 6|2m USB Audio interface support"
>   ---help---
> diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
> index 45fd4ac..fd79dd5 100644
> --- a/drivers/usb/misc/Makefile
> +++ b/drivers/usb/misc/Makefile
> @@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY)  += chaoskey.o
>  
>  obj-$(CONFIG_USB_SISUSBVGA)  += sisusbvga/
>  obj-$(CONFIG_USB_LINK_LAYER_TEST)+= lvstest.o
> +
> +obj-$(CONFIG_USB_MUX)+= mux.o

"mux.ko" is a _VERY_ generic name of a kernel module.  Please make this
much more specific to your chip as this will not work for any other
platform that has this type of functionality.

Same goes for all of your exported symbols.



> diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
> new file mode 100644
> index 000..e353fff
> --- /dev/null
> +++ b/drivers/usb/misc/mux.c
> @@ -0,0 +1,172 @@
> +/**
> + * mux.c - USB Port Mux support
> + *
> + * Copyright (C) 2016 Intel Corporation
> + *
> + * Author: Lu Baolu 
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License version 2 as
> + * published by the Free Software Foundation.
> + */
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +static int usb_mux_change_state(struct usb_mux *mux, int state)
> +{
> + int ret;
> + struct usb_mux_dev *umdev = mux->umdev;
> +
> + dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
> + "mutex is unlocked\n");
> +
> + mux->mux_state = state;
> +
> + if (mux->mux_state)
> + ret = umdev->cable_set_cb(umdev);
> + else
> + ret = umdev->cable_unset_cb(umdev);
> +
> + return ret;
> +}
> +
> +static int usb_mux_notifier(struct notifier_block *nb,
> + unsigned long event, void *ptr)
> +{
> + struct usb_mux *mux;
> + int state;
> + int ret = NOTIFY_DONE;
> +
> + mux = container_of(nb, struct usb_mux, nb);
> +
> + state = extcon_get_cable_state(mux->obj.edev,
> + mux->umdev->cable_name);
> +
> + if (mux->mux_state == -1 || mux->mux_state != state) {
> + mutex_lock(>mux_mutex);
> + ret = usb_mux_change_state(mux, state);
> + mutex_unlock(>mux_mutex);
> + }
> +
> + return ret;
> +}
> +
> +static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
> + size_t len, loff_t *offset)
> +{
> + struct usb_mux *mux = file->private_data;
> + char output_buf[16];
> +
> + memset(output_buf, 0, sizeof(output_buf));
> + if (mux->mux_state)
> + strcpy(output_buf, "host\n");
> + else
> + strcpy(output_buf, "peripheral\n");
> +
> + return simple_read_from_buffer(user_buf, len, offset,
> + output_buf, strlen(output_buf));
> +}
> +
> +static ssize_t mux_debug_write(struct file *file, const char __user 
> *user_buf,
> + size_t count, loff_t *offset)
> +{
> + struct usb_mux *mux = 

[PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-02 Thread Lu Baolu
Several Intel PCHs and SOCs have an internal mux that is used to
share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between
   HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this
common code, the individual mux driver, which always is platform
dependent, could focus on the real operation of mux switch.

Signed-off-by: Lu Baolu 
Reviewed-by: Heikki Krogerus 
Reviewed-by: Felipe Balbi 
---
 MAINTAINERS   |   7 ++
 drivers/usb/misc/Kconfig  |   4 ++
 drivers/usb/misc/Makefile |   2 +
 drivers/usb/misc/mux.c| 172 ++
 include/linux/usb/mux.h   |  71 +++
 5 files changed, 256 insertions(+)
 create mode 100644 drivers/usb/misc/mux.c
 create mode 100644 include/linux/usb/mux.h

diff --git a/MAINTAINERS b/MAINTAINERS
index d894ee2..45f1e1e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11389,6 +11389,13 @@ T: git 
git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S: Maintained
 F: drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M: Lu Baolu 
+L: linux-...@vger.kernel.org
+S: Supported
+F: drivers/usb/misc/mux.c
+F: include/linux/usb/mux.h
+
 USB PRINTER DRIVER (usblp)
 M: Pete Zaitcev 
 L: linux-...@vger.kernel.org
diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index f7a7fc2..6496d17 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -3,6 +3,10 @@
 #
 comment "USB Miscellaneous drivers"
 
+config USB_MUX
+   select EXTCON
+   def_bool n
+
 config USB_EMI62
tristate "EMI 6|2m USB Audio interface support"
---help---
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index 45fd4ac..fd79dd5 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY)+= chaoskey.o
 
 obj-$(CONFIG_USB_SISUSBVGA)+= sisusbvga/
 obj-$(CONFIG_USB_LINK_LAYER_TEST)  += lvstest.o
+
+obj-$(CONFIG_USB_MUX)  += mux.o
diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
new file mode 100644
index 000..e353fff
--- /dev/null
+++ b/drivers/usb/misc/mux.c
@@ -0,0 +1,172 @@
+/**
+ * mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include 
+#include 
+#include 
+#include 
+
+static int usb_mux_change_state(struct usb_mux *mux, int state)
+{
+   int ret;
+   struct usb_mux_dev *umdev = mux->umdev;
+
+   dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
+   "mutex is unlocked\n");
+
+   mux->mux_state = state;
+
+   if (mux->mux_state)
+   ret = umdev->cable_set_cb(umdev);
+   else
+   ret = umdev->cable_unset_cb(umdev);
+
+   return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,
+   unsigned long event, void *ptr)
+{
+   struct usb_mux *mux;
+   int state;
+   int ret = NOTIFY_DONE;
+
+   mux = container_of(nb, struct usb_mux, nb);
+
+   state = extcon_get_cable_state(mux->obj.edev,
+   mux->umdev->cable_name);
+
+   if (mux->mux_state == -1 || mux->mux_state != state) {
+   mutex_lock(>mux_mutex);
+   ret = usb_mux_change_state(mux, state);
+   mutex_unlock(>mux_mutex);
+   }
+
+   return ret;
+}
+
+static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
+   size_t len, loff_t *offset)
+{
+   struct usb_mux *mux = file->private_data;
+   char output_buf[16];
+
+   memset(output_buf, 0, sizeof(output_buf));
+   if (mux->mux_state)
+   strcpy(output_buf, "host\n");
+   else
+   strcpy(output_buf, "peripheral\n");
+
+   return simple_read_from_buffer(user_buf, len, offset,
+   output_buf, strlen(output_buf));
+}
+
+static ssize_t mux_debug_write(struct file *file, const char __user *user_buf,
+   size_t count, loff_t *offset)
+{
+   struct usb_mux *mux = file->private_data;
+   char input_buf[16];
+   int size, state;
+
+   size = min(count, sizeof(input_buf) - 1);
+   memset(input_buf, 0, sizeof(input_buf));
+   if (strncpy_from_user(input_buf, user_buf, size) < 0)
+   return -EFAULT;
+
+   if (!strncmp(input_buf, "host", 4))
+   state = 1;
+   else if 

[PATCH 3/7] usb: misc: add common code for Intel dual role port mux

2016-03-02 Thread Lu Baolu
Several Intel PCHs and SOCs have an internal mux that is used to
share one USB port between device controller and host controller.

A usb port mux could be abstracted as the following elements:
1) mux state: HOST or PERIPHERAL;
2) an extcon cable which triggers the change of mux state between
   HOST and PERIPHERAL;
3) The required action to do the real port switch.

This patch adds the common code to handle usb port mux. With this
common code, the individual mux driver, which always is platform
dependent, could focus on the real operation of mux switch.

Signed-off-by: Lu Baolu 
Reviewed-by: Heikki Krogerus 
Reviewed-by: Felipe Balbi 
---
 MAINTAINERS   |   7 ++
 drivers/usb/misc/Kconfig  |   4 ++
 drivers/usb/misc/Makefile |   2 +
 drivers/usb/misc/mux.c| 172 ++
 include/linux/usb/mux.h   |  71 +++
 5 files changed, 256 insertions(+)
 create mode 100644 drivers/usb/misc/mux.c
 create mode 100644 include/linux/usb/mux.h

diff --git a/MAINTAINERS b/MAINTAINERS
index d894ee2..45f1e1e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -11389,6 +11389,13 @@ T: git 
git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb.git
 S: Maintained
 F: drivers/usb/phy/
 
+USB PORT MUX DRIVER
+M: Lu Baolu 
+L: linux-...@vger.kernel.org
+S: Supported
+F: drivers/usb/misc/mux.c
+F: include/linux/usb/mux.h
+
 USB PRINTER DRIVER (usblp)
 M: Pete Zaitcev 
 L: linux-...@vger.kernel.org
diff --git a/drivers/usb/misc/Kconfig b/drivers/usb/misc/Kconfig
index f7a7fc2..6496d17 100644
--- a/drivers/usb/misc/Kconfig
+++ b/drivers/usb/misc/Kconfig
@@ -3,6 +3,10 @@
 #
 comment "USB Miscellaneous drivers"
 
+config USB_MUX
+   select EXTCON
+   def_bool n
+
 config USB_EMI62
tristate "EMI 6|2m USB Audio interface support"
---help---
diff --git a/drivers/usb/misc/Makefile b/drivers/usb/misc/Makefile
index 45fd4ac..fd79dd5 100644
--- a/drivers/usb/misc/Makefile
+++ b/drivers/usb/misc/Makefile
@@ -29,3 +29,5 @@ obj-$(CONFIG_USB_CHAOSKEY)+= chaoskey.o
 
 obj-$(CONFIG_USB_SISUSBVGA)+= sisusbvga/
 obj-$(CONFIG_USB_LINK_LAYER_TEST)  += lvstest.o
+
+obj-$(CONFIG_USB_MUX)  += mux.o
diff --git a/drivers/usb/misc/mux.c b/drivers/usb/misc/mux.c
new file mode 100644
index 000..e353fff
--- /dev/null
+++ b/drivers/usb/misc/mux.c
@@ -0,0 +1,172 @@
+/**
+ * mux.c - USB Port Mux support
+ *
+ * Copyright (C) 2016 Intel Corporation
+ *
+ * Author: Lu Baolu 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#include 
+#include 
+#include 
+#include 
+
+static int usb_mux_change_state(struct usb_mux *mux, int state)
+{
+   int ret;
+   struct usb_mux_dev *umdev = mux->umdev;
+
+   dev_WARN_ONCE(umdev->dev, !mutex_is_locked(>mux_mutex),
+   "mutex is unlocked\n");
+
+   mux->mux_state = state;
+
+   if (mux->mux_state)
+   ret = umdev->cable_set_cb(umdev);
+   else
+   ret = umdev->cable_unset_cb(umdev);
+
+   return ret;
+}
+
+static int usb_mux_notifier(struct notifier_block *nb,
+   unsigned long event, void *ptr)
+{
+   struct usb_mux *mux;
+   int state;
+   int ret = NOTIFY_DONE;
+
+   mux = container_of(nb, struct usb_mux, nb);
+
+   state = extcon_get_cable_state(mux->obj.edev,
+   mux->umdev->cable_name);
+
+   if (mux->mux_state == -1 || mux->mux_state != state) {
+   mutex_lock(>mux_mutex);
+   ret = usb_mux_change_state(mux, state);
+   mutex_unlock(>mux_mutex);
+   }
+
+   return ret;
+}
+
+static ssize_t mux_debug_read(struct file *file, char __user *user_buf,
+   size_t len, loff_t *offset)
+{
+   struct usb_mux *mux = file->private_data;
+   char output_buf[16];
+
+   memset(output_buf, 0, sizeof(output_buf));
+   if (mux->mux_state)
+   strcpy(output_buf, "host\n");
+   else
+   strcpy(output_buf, "peripheral\n");
+
+   return simple_read_from_buffer(user_buf, len, offset,
+   output_buf, strlen(output_buf));
+}
+
+static ssize_t mux_debug_write(struct file *file, const char __user *user_buf,
+   size_t count, loff_t *offset)
+{
+   struct usb_mux *mux = file->private_data;
+   char input_buf[16];
+   int size, state;
+
+   size = min(count, sizeof(input_buf) - 1);
+   memset(input_buf, 0, sizeof(input_buf));
+   if (strncpy_from_user(input_buf, user_buf, size) < 0)
+   return -EFAULT;
+
+   if (!strncmp(input_buf, "host", 4))
+   state = 1;
+   else if (!strncmp(input_buf, "peripheral", 10))
+   state = 0;
+   else
+   state = -1;
+
+   if (state != -1) {
+