Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-18 Thread Heikki Krogerus
On Wed, Aug 17, 2016 at 10:58:40AM -0700, Guenter Roeck wrote:
> On Wed, Aug 17, 2016 at 01:34:40PM +0300, Heikki Krogerus wrote:
> > The purpose of USB Type-C connector class is to provide
> > unified interface for the user space to get the status and
> > basic information about USB Type-C connectors on a system,
> > control over data role swapping, and when the port supports
> > USB Power Delivery, also control over power role swapping
> > and Alternate Modes.
> > 
> > Signed-off-by: Heikki Krogerus 
> > ---
> [ ... ]
> 
> > +
> > +static ssize_t current_power_role_store(struct device *dev,
> > +   struct device_attribute *attr,
> > +   const char *buf, size_t size)
> > +{
> > +   struct typec_port *port = to_typec_port(dev);
> > +   enum typec_role role;
> > +   int ret = size;
> > +
> > +   if (!port->cap->usb_pd) {
> > +   dev_dbg(dev, "power role swap only supported with USB PD\n");
> > +   return -EOPNOTSUPP;
> > +   }
> > +
> > +   if (!port->cap->pr_set) {
> > +   dev_dbg(dev, "power role swapping not supported\n");
> > +   return -EOPNOTSUPP;
> > +   }
> > +
> > +   if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
> > +   dev_dbg(dev, "partner unable to swap power role\n");
> > +   return -EIO;
> > +   }
> > +
> > +   if (!port->connected)
> > +   return size;
> > +
> > +   ret = sysfs_strmatch(typec_roles, ARRAY_SIZE(typec_roles), buf);
> > +   if (ret < 0)
> > +   return ret;
> > +
> > +   role = ret;
> > +
> > +   ret = port->cap->pr_set(port->cap, role);
> 
>   if (ret)
> missing.

It also seems to be missing from current_vconn_role_store().. How have
I managed to do that?

Thanks for catching this.

-- 
heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-18 Thread Heikki Krogerus
On Wed, Aug 17, 2016 at 10:53:11AM -0700, Guenter Roeck wrote:
> On Wed, Aug 17, 2016 at 01:34:40PM +0300, Heikki Krogerus wrote:
> > The purpose of USB Type-C connector class is to provide
> > unified interface for the user space to get the status and
> > basic information about USB Type-C connectors on a system,
> > control over data role swapping, and when the port supports
> > USB Power Delivery, also control over power role swapping
> > and Alternate Modes.
> > 
> > Signed-off-by: Heikki Krogerus 
> > ---
> [ ... ]
> > +
> > +static ssize_t
> > +current_data_role_store(struct device *dev, struct device_attribute *attr,
> > +   const char *buf, size_t size)
> > +{
> > +   struct typec_port *port = to_typec_port(dev);
> > +   enum typec_role role;
> > +   int ret;
> > +
> > +   if (port->cap->type != TYPEC_PORT_DRP) {
> > +   dev_dbg(dev, "data role swap only supported with DRP ports\n");
> > +   return -EOPNOTSUPP;
> > +   }
> > +
> > +   if (!port->cap->dr_set) {
> > +   dev_dbg(dev, "data role swapping not supported\n");
> > +   return -EOPNOTSUPP;
> > +   }
> > +
> > +   if (!port->connected)
> > +   return size;
> 
> I don't think this check should be here. The connection status can change 
> after
> the connection status was checked. We should leave it up to the driver to
> perform the necessary checks.
> 
> This also applies to the other role change store functions.

OK.

Thanks,

-- 
heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Guenter Roeck
On Wed, Aug 17, 2016 at 01:34:40PM +0300, Heikki Krogerus wrote:
> The purpose of USB Type-C connector class is to provide
> unified interface for the user space to get the status and
> basic information about USB Type-C connectors on a system,
> control over data role swapping, and when the port supports
> USB Power Delivery, also control over power role swapping
> and Alternate Modes.
> 
> Signed-off-by: Heikki Krogerus 
> ---
[ ... ]

> +
> +static ssize_t current_power_role_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t size)
> +{
> + struct typec_port *port = to_typec_port(dev);
> + enum typec_role role;
> + int ret = size;
> +
> + if (!port->cap->usb_pd) {
> + dev_dbg(dev, "power role swap only supported with USB PD\n");
> + return -EOPNOTSUPP;
> + }
> +
> + if (!port->cap->pr_set) {
> + dev_dbg(dev, "power role swapping not supported\n");
> + return -EOPNOTSUPP;
> + }
> +
> + if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
> + dev_dbg(dev, "partner unable to swap power role\n");
> + return -EIO;
> + }
> +
> + if (!port->connected)
> + return size;
> +
> + ret = sysfs_strmatch(typec_roles, ARRAY_SIZE(typec_roles), buf);
> + if (ret < 0)
> + return ret;
> +
> + role = ret;
> +
> + ret = port->cap->pr_set(port->cap, role);

if (ret)
missing.

> + return ret;
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Guenter Roeck
On Wed, Aug 17, 2016 at 01:34:40PM +0300, Heikki Krogerus wrote:
> The purpose of USB Type-C connector class is to provide
> unified interface for the user space to get the status and
> basic information about USB Type-C connectors on a system,
> control over data role swapping, and when the port supports
> USB Power Delivery, also control over power role swapping
> and Alternate Modes.
> 
> Signed-off-by: Heikki Krogerus 
> ---
[ ... ]
> +
> +static ssize_t
> +current_data_role_store(struct device *dev, struct device_attribute *attr,
> + const char *buf, size_t size)
> +{
> + struct typec_port *port = to_typec_port(dev);
> + enum typec_role role;
> + int ret;
> +
> + if (port->cap->type != TYPEC_PORT_DRP) {
> + dev_dbg(dev, "data role swap only supported with DRP ports\n");
> + return -EOPNOTSUPP;
> + }
> +
> + if (!port->cap->dr_set) {
> + dev_dbg(dev, "data role swapping not supported\n");
> + return -EOPNOTSUPP;
> + }
> +
> + if (!port->connected)
> + return size;

I don't think this check should be here. The connection status can change after
the connection status was checked. We should leave it up to the driver to
perform the necessary checks.

This also applies to the other role change store functions.

Thanks,
Guenter
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Heikki Krogerus
Hi,

On Wed, Aug 17, 2016 at 03:14:03PM +0200, Frans Klaver wrote:
> On Wed, Aug 17, 2016 at 12:34 PM, Heikki Krogerus
> > +static const char * const typec_partner_types[] = {
> > +   [TYPEC_PARTNER_USB] = "USB",
> > +   [TYPEC_PARTNER_CHARGER] = "Charger",
> > +   [TYPEC_PARTNER_ALTMODE] = "Alternate Mode",
> > +   [TYPEC_PARTNER_ACCESSORY]   = "Accessory",
> > +};
> > +
> > +static ssize_t partner_type_show(struct device *dev,
> > +struct device_attribute *attr, char *buf)
> > +{
> > +   struct typec_partner *partner = container_of(dev, struct 
> > typec_partner,
> > +dev);
> > +
> > +   return sprintf(buf, "%s\n", typec_partner_types[partner->type]);
> > +}
> > +
> > +static struct device_attribute dev_attr_partner_type = {
> > +   .attr = {
> > +   .name = "type",
> > +   .mode = S_IRUGO,
> > +   },
> > +   .show = partner_type_show,
> > +};
> 
> Why not use DEVICE_ATTR_RO() for this?

Because I don't want to tie the attribute names to the function names
in this case. There are other *type* attributes being created in the
driver, so type_show() is not good, and we can't name the attribute
"partner_type". The attribute will be placed in group named "partner".

> > +
> > +static ssize_t
> > +partner_accessory_mode_show(struct device *dev, struct device_attribute 
> > *attr,
> > +   char *buf)
> > +{
> > +   struct typec_partner *partner = container_of(dev, struct 
> > typec_partner,
> > +dev);
> > +
> > +   return sprintf(buf, "%s\n", 
> > typec_accessory_modes[partner->accessory]);
> > +}
> > +
> > +static struct device_attribute dev_attr_partner_accessory = {
> > +   .attr = {
> > +   .name = "accessory",
> > +   .mode = S_IRUGO,
> > +   },
> > +   .show = partner_accessory_mode_show,
> > +};
> 
> And this

Ditto.


Thanks,

-- 
heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Frans Klaver
On Wed, Aug 17, 2016 at 3:53 PM, Heikki Krogerus
 wrote:
> Hi,
>
> On Wed, Aug 17, 2016 at 03:14:03PM +0200, Frans Klaver wrote:
>> On Wed, Aug 17, 2016 at 12:34 PM, Heikki Krogerus
>> > +static const char * const typec_partner_types[] = {
>> > +   [TYPEC_PARTNER_USB] = "USB",
>> > +   [TYPEC_PARTNER_CHARGER] = "Charger",
>> > +   [TYPEC_PARTNER_ALTMODE] = "Alternate Mode",
>> > +   [TYPEC_PARTNER_ACCESSORY]   = "Accessory",
>> > +};
>> > +
>> > +static ssize_t partner_type_show(struct device *dev,
>> > +struct device_attribute *attr, char *buf)
>> > +{
>> > +   struct typec_partner *partner = container_of(dev, struct 
>> > typec_partner,
>> > +dev);
>> > +
>> > +   return sprintf(buf, "%s\n", typec_partner_types[partner->type]);
>> > +}
>> > +
>> > +static struct device_attribute dev_attr_partner_type = {
>> > +   .attr = {
>> > +   .name = "type",
>> > +   .mode = S_IRUGO,
>> > +   },
>> > +   .show = partner_type_show,
>> > +};
>>
>> Why not use DEVICE_ATTR_RO() for this?
>
> Because I don't want to tie the attribute names to the function names
> in this case. There are other *type* attributes being created in the
> driver, so type_show() is not good, and we can't name the attribute
> "partner_type". The attribute will be placed in group named "partner".
>

Ah, makes sense.

Thanks,
Frans
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Heikki Krogerus
Hi,

On Wed, Aug 17, 2016 at 06:30:35AM -0700, Guenter Roeck wrote:
> On 08/17/2016 03:34 AM, Heikki Krogerus wrote:
> > The purpose of USB Type-C connector class is to provide
> > unified interface for the user space to get the status and
> > basic information about USB Type-C connectors on a system,
> > control over data role swapping, and when the port supports
> > USB Power Delivery, also control over power role swapping
> > and Alternate Modes.
> > 
> > Signed-off-by: Heikki Krogerus 
> > ---
> >  Documentation/ABI/testing/sysfs-class-typec |  199 +
> >  Documentation/usb/typec.txt |  103 +++
> >  MAINTAINERS |9 +
> >  drivers/usb/Kconfig |2 +
> >  drivers/usb/Makefile|2 +
> >  drivers/usb/typec/Kconfig   |7 +
> >  drivers/usb/typec/Makefile  |1 +
> >  drivers/usb/typec/typec.c   | 1104 
> > +++
> >  include/linux/usb/typec.h   |  260 +++
> >  9 files changed, 1687 insertions(+)
> >  create mode 100644 Documentation/ABI/testing/sysfs-class-typec
> >  create mode 100644 Documentation/usb/typec.txt
> >  create mode 100644 drivers/usb/typec/Kconfig
> >  create mode 100644 drivers/usb/typec/Makefile
> >  create mode 100644 drivers/usb/typec/typec.c
> >  create mode 100644 include/linux/usb/typec.h
> > 
> > diff --git a/Documentation/ABI/testing/sysfs-class-typec 
> > b/Documentation/ABI/testing/sysfs-class-typec
> > new file mode 100644
> > index 000..e6179d3
> > --- /dev/null
> > +++ b/Documentation/ABI/testing/sysfs-class-typec
> > @@ -0,0 +1,199 @@
> > +USB Type-C port devices (eg. /sys/class/typec/usbc0/)
> > +
> > +What:  /sys/class/typec//current_data_role
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   The current USB data role the port is operating in. This
> > +   attribute can be used for requesting data role swapping on the
> > +   port.
> > +
> > +   Valid values:
> > +   - host
> > +   - device
> > +
> > +What:  /sys/class/typec//current_power_role
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   The current power role of the port. This attribute can be used
> > +   to request power role swap on the port when the port supports
> > +   USB Power Delivery.
> > +
> > +   Valid values:
> > +   - source
> > +   - sink
> > +
> > +What:  /sys/class/typec//current_vconn_role
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   Shows the current VCONN role of the port. This attribute can be
> > +   used to request VCONN role swap on the port when the port
> > +   supports USB Power Delivery.
> > +
> > +   Valid values are:
> > +   - source
> > +   - sink
> > +
> > +What:  /sys/class/typec//power_operation_mode
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   Shows the current power operational mode the port is in.
> > +
> > +   Valid values:
> > +   - USB - Normal power levels defined in USB specifications
> > +   - BC1.2 - Power levels defined in Battery Charging Specification
> > + v1.2
> > +   - USB Type-C 1.5A - Higher 1.5A current defined in USB Type-C
> > +   specification.
> > +   - USB Type-C 3.0A - Higher 3A current defined in USB Type-C
> > +   specification.
> > +- USB Power Delivery - The voltages and currents defined 
> > in USB
> > +  Power Delivery specification
> > +
> > +What:  /sys/class/typec//preferred_role
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   The user space can notify the driver about the preferred role.
> > +   It should be handled as enabling of Try.SRC or Try.SNK, as
> > +   defined in USB Type-C specification, in the port drivers. By
> > +   default there is no preferred role.
> > +
> > +   Valid values:
> > +   - host
> > +   - device
> > +   - For example "none" to remove preference (anything else except
> > + "host" or "device")
> > +
> > +What:  /sys/class/typec//supported_accessory_modes
> > +Date:  June 2016
> > +Contact:   Heikki Krogerus 
> > +Description:
> > +   Lists the Accessory Modes, defined in the USB Type-C
> > +  

Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Guenter Roeck

On 08/17/2016 03:34 AM, Heikki Krogerus wrote:

The purpose of USB Type-C connector class is to provide
unified interface for the user space to get the status and
basic information about USB Type-C connectors on a system,
control over data role swapping, and when the port supports
USB Power Delivery, also control over power role swapping
and Alternate Modes.

Signed-off-by: Heikki Krogerus 
---
 Documentation/ABI/testing/sysfs-class-typec |  199 +
 Documentation/usb/typec.txt |  103 +++
 MAINTAINERS |9 +
 drivers/usb/Kconfig |2 +
 drivers/usb/Makefile|2 +
 drivers/usb/typec/Kconfig   |7 +
 drivers/usb/typec/Makefile  |1 +
 drivers/usb/typec/typec.c   | 1104 +++
 include/linux/usb/typec.h   |  260 +++
 9 files changed, 1687 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-class-typec
 create mode 100644 Documentation/usb/typec.txt
 create mode 100644 drivers/usb/typec/Kconfig
 create mode 100644 drivers/usb/typec/Makefile
 create mode 100644 drivers/usb/typec/typec.c
 create mode 100644 include/linux/usb/typec.h

diff --git a/Documentation/ABI/testing/sysfs-class-typec 
b/Documentation/ABI/testing/sysfs-class-typec
new file mode 100644
index 000..e6179d3
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-typec
@@ -0,0 +1,199 @@
+USB Type-C port devices (eg. /sys/class/typec/usbc0/)
+
+What:  /sys/class/typec//current_data_role
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   The current USB data role the port is operating in. This
+   attribute can be used for requesting data role swapping on the
+   port.
+
+   Valid values:
+   - host
+   - device
+
+What:  /sys/class/typec//current_power_role
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   The current power role of the port. This attribute can be used
+   to request power role swap on the port when the port supports
+   USB Power Delivery.
+
+   Valid values:
+   - source
+   - sink
+
+What:  /sys/class/typec//current_vconn_role
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   Shows the current VCONN role of the port. This attribute can be
+   used to request VCONN role swap on the port when the port
+   supports USB Power Delivery.
+
+   Valid values are:
+   - source
+   - sink
+
+What:  /sys/class/typec//power_operation_mode
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   Shows the current power operational mode the port is in.
+
+   Valid values:
+   - USB - Normal power levels defined in USB specifications
+   - BC1.2 - Power levels defined in Battery Charging Specification
+ v1.2
+   - USB Type-C 1.5A - Higher 1.5A current defined in USB Type-C
+   specification.
+   - USB Type-C 3.0A - Higher 3A current defined in USB Type-C
+   specification.
+- USB Power Delivery - The voltages and currents defined in USB
+  Power Delivery specification
+
+What:  /sys/class/typec//preferred_role
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   The user space can notify the driver about the preferred role.
+   It should be handled as enabling of Try.SRC or Try.SNK, as
+   defined in USB Type-C specification, in the port drivers. By
+   default there is no preferred role.
+
+   Valid values:
+   - host
+   - device
+   - For example "none" to remove preference (anything else except
+ "host" or "device")
+
+What:  /sys/class/typec//supported_accessory_modes
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   Lists the Accessory Modes, defined in the USB Type-C
+   specification, the port supports.
+
+What:  /sys/class/typec//supported_data_roles
+Date:  June 2016
+Contact:   Heikki Krogerus 
+Description:
+   Lists the USB data roles the port is capable of supporting.
+
+   Valid values:
+   - device
+   - host
+   - device, 

Re: [PATCH v5 1/2] usb: USB Type-C connector class

2016-08-17 Thread Frans Klaver
On Wed, Aug 17, 2016 at 12:34 PM, Heikki Krogerus
 wrote:
> The purpose of USB Type-C connector class is to provide
> unified interface for the user space to get the status and
> basic information about USB Type-C connectors on a system,
> control over data role swapping, and when the port supports
> USB Power Delivery, also control over power role swapping
> and Alternate Modes.
>
> Signed-off-by: Heikki Krogerus 
> ---
>  Documentation/ABI/testing/sysfs-class-typec |  199 +
>  Documentation/usb/typec.txt |  103 +++
>  MAINTAINERS |9 +
>  drivers/usb/Kconfig |2 +
>  drivers/usb/Makefile|2 +
>  drivers/usb/typec/Kconfig   |7 +
>  drivers/usb/typec/Makefile  |1 +
>  drivers/usb/typec/typec.c   | 1104 
> +++
>  include/linux/usb/typec.h   |  260 +++
>  9 files changed, 1687 insertions(+)
>  create mode 100644 Documentation/ABI/testing/sysfs-class-typec
>  create mode 100644 Documentation/usb/typec.txt
>  create mode 100644 drivers/usb/typec/Kconfig
>  create mode 100644 drivers/usb/typec/Makefile
>  create mode 100644 drivers/usb/typec/typec.c
>  create mode 100644 include/linux/usb/typec.h
>
> diff --git a/Documentation/ABI/testing/sysfs-class-typec 
> b/Documentation/ABI/testing/sysfs-class-typec
> new file mode 100644
> index 000..e6179d3
> --- /dev/null
> +++ b/Documentation/ABI/testing/sysfs-class-typec
> @@ -0,0 +1,199 @@
> +USB Type-C port devices (eg. /sys/class/typec/usbc0/)
> +
> +What:  /sys/class/typec//current_data_role
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   The current USB data role the port is operating in. This
> +   attribute can be used for requesting data role swapping on the
> +   port.
> +
> +   Valid values:
> +   - host
> +   - device
> +
> +What:  /sys/class/typec//current_power_role
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   The current power role of the port. This attribute can be used
> +   to request power role swap on the port when the port supports
> +   USB Power Delivery.
> +
> +   Valid values:
> +   - source
> +   - sink
> +
> +What:  /sys/class/typec//current_vconn_role
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   Shows the current VCONN role of the port. This attribute can 
> be
> +   used to request VCONN role swap on the port when the port
> +   supports USB Power Delivery.
> +
> +   Valid values are:
> +   - source
> +   - sink
> +
> +What:  /sys/class/typec//power_operation_mode
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   Shows the current power operational mode the port is in.
> +
> +   Valid values:
> +   - USB - Normal power levels defined in USB specifications
> +   - BC1.2 - Power levels defined in Battery Charging 
> Specification
> + v1.2
> +   - USB Type-C 1.5A - Higher 1.5A current defined in USB Type-C
> +   specification.
> +   - USB Type-C 3.0A - Higher 3A current defined in USB Type-C
> +   specification.
> +- USB Power Delivery - The voltages and currents defined in 
> USB
> +  Power Delivery specification
> +
> +What:  /sys/class/typec//preferred_role
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   The user space can notify the driver about the preferred role.
> +   It should be handled as enabling of Try.SRC or Try.SNK, as
> +   defined in USB Type-C specification, in the port drivers. By
> +   default there is no preferred role.
> +
> +   Valid values:
> +   - host
> +   - device
> +   - For example "none" to remove preference (anything else 
> except
> + "host" or "device")
> +
> +What:  /sys/class/typec//supported_accessory_modes
> +Date:  June 2016
> +Contact:   Heikki Krogerus 
> +Description:
> +   Lists the Accessory Modes, defined in the USB Type-C
> +   specification, the port supports.
> +
> +What:  /sys/class/typec//supported_data_roles
> +Date: