Re: [RFC PATCH v2 2/3] usb: typec: Bus type for alternate modes
From: Heikki Krogerus
Date: Mon Mar 19 2018 - 07:42:51 EST
Hi Guenter,
On Fri, Mar 16, 2018 at 02:33:36PM -0700, Guenter Roeck wrote:
> On Fri, Mar 09, 2018 at 06:19:17PM +0300, Heikki Krogerus wrote:
> > diff --git a/drivers/usb/typec/bus.c b/drivers/usb/typec/bus.c
> > new file mode 100644
> > index 000000000000..92944aaf3d6a
> > --- /dev/null
> > +++ b/drivers/usb/typec/bus.c
> > @@ -0,0 +1,421 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/**
> > + * Bus for USB Type-C Alternate Modes
> > + *
> > + * Copyright (C) 2018 Intel Corporation
> > + * Author: Heikki Krogerus <heikki.krogerus@xxxxxxxxxxxxxxx>
> > + */
> > +
> > +#include "bus.h"
> > +
> > +/* -------------------------------------------------------------------------- */
> > +/* Common API */
> > +
> > +/**
> > + * typec_altmode_notify - Communication between the OS and alternate mode driver
> > + * @adev: Handle to the alternate mode
> > + * @conf: Alternate mode specific configuration value
> > + * @data: Alternate mode specific data
> > + *
> > + * The primary purpose for this function is to allow the alternate mode drivers
> > + * to tell which pin configuration has been negotiated with the partner. That
> > + * information will then be used for example to configure the muxes.
> > + * Communication to the other direction is also possible, and low level device
> > + * drivers can also send notifications to the alternate mode drivers. The actual
> > + * communication will be specific for every SVID.
> > + */
> > +int typec_altmode_notify(struct typec_altmode *adev,
> > + unsigned long conf, void *data)
> > +{
> > + struct altmode *altmode;
> > + struct altmode *partner;
> > + int ret;
> > +
> > + /*
> > + * All SVID specific configuration values must start from
> > + * TYPEC_STATE_MODAL. The first values are reserved for the pin states
> > + * defined in USB Type-C specification: TYPEC_STATE_USB and
> > + * TYPEC_STATE_SAFE. We'll follow this rule even with modes that do not
> > + * require pin reconfiguration for the sake of simplicity.
> > + */
> > + if (conf < TYPEC_STATE_MODAL)
> > + return -EINVAL;
> > +
> > + if (!adev)
> > + return 0;
> > +
> > + altmode = to_altmode(adev);
> > +
> > + if (!altmode->partner)
> > + return -ENODEV;
> > +
> > + ret = typec_set_mode(typec_altmode2port(adev), (int)conf);
> > + if (ret)
> > + return ret;
> > +
> > + partner = altmode->partner;
> > +
> > + blocking_notifier_call_chain(is_typec_port(adev->dev.parent) ?
> > + &altmode->nh : &partner->nh,
> > + conf, data);
> > +
> > + if (partner->ops && partner->ops->notify)
> > + return partner->ops->notify(&partner->adev, conf, data);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_notify);
> > +
> > +/**
> > + * typec_altmode_enter - Enter Mode
> > + * @adev: The alternate mode
> > + *
> > + * The alternate mode drivers use this function to enter mode. The port drivers
> > + * use this to inform the alternate mode drivers that their mode has been
> > + * entered successfully.
> > + */
> > +int typec_altmode_enter(struct typec_altmode *adev)
> > +{
> > + struct altmode *partner = to_altmode(adev)->partner;
> > + struct typec_altmode *pdev = &partner->adev;
> > + int ret;
> > +
> > + if (is_typec_port(adev->dev.parent)) {
> > + typec_altmode_update_active(pdev, pdev->mode, true);
> > + sysfs_notify(&pdev->dev.kobj, NULL, "active");
> > + goto enter_mode;
> > + }
> > +
> > + if (!pdev->active)
> > + return -EPERM;
> > +
> > + /* First moving to USB Safe State */
> > + ret = typec_set_mode(typec_altmode2port(adev), TYPEC_STATE_SAFE);
> > + if (ret)
> > + return ret;
> > +
> > + blocking_notifier_call_chain(&partner->nh, TYPEC_STATE_SAFE, NULL);
> > +
> > +enter_mode:
> > + /* Enter Mode command */
> > + if (partner->ops && partner->ops->enter)
> > + partner->ops->enter(pdev);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_enter);
> > +
> > +/**
> > + * typec_altmode_enter - Exit Mode
>
> typec_altmode_exit
Yes.
> > + * @adev: The alternate mode
> > + *
> > + * The alternate mode drivers use this function to exit mode. The port drivers
> > + * can also inform the alternate mode drivers with this function that the mode
> > + * was successfully exited.
> > + */
> > +int typec_altmode_exit(struct typec_altmode *adev)
> > +{
> > + struct altmode *partner = to_altmode(adev)->partner;
> > + struct typec_port *port = typec_altmode2port(adev);
> > + struct typec_altmode *pdev = &partner->adev;
> > + int ret;
> > +
> > + /* In case of port, just calling the driver and exiting */
> > + if (is_typec_port(adev->dev.parent)) {
> > + typec_altmode_update_active(pdev, pdev->mode, false);
> > + sysfs_notify(&pdev->dev.kobj, NULL, "active");
> > +
> > + if (partner->ops && partner->ops->exit)
> > + partner->ops->exit(pdev);
> > + return 0;
> > + }
> > +
> > + /* Moving to USB Safe State */
> > + ret = typec_set_mode(port, TYPEC_STATE_SAFE);
> > + if (ret)
> > + return ret;
> > +
> > + blocking_notifier_call_chain(&partner->nh, TYPEC_STATE_SAFE, NULL);
> > +
> > + /* Exit Mode command */
> > + if (partner->ops && partner->ops->exit)
> > + partner->ops->exit(pdev);
> > +
> > + /* Back to USB operation */
> > + ret = typec_set_mode(port, TYPEC_STATE_USB);
> > + if (ret)
> > + return ret;
> > +
> > + blocking_notifier_call_chain(&partner->nh, TYPEC_STATE_USB, NULL);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_exit);
> > +
> > +/**
> > + * typec_altmode_attention - Attention command
> > + * @adev: The alternate mode
> > + * @vdo: VDO for the Attention command
> > + *
> > + * Notifies the partner of @adev about Attention command.
> > + */
> > +void typec_altmode_attention(struct typec_altmode *adev, const u32 vdo)
> > +{
> > + struct altmode *partner = to_altmode(adev)->partner;
> > +
> > + if (partner && partner->ops && partner->ops->attention)
> > + partner->ops->attention(&partner->adev, vdo);
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_attention);
> > +
> > +/**
> > + * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
> > + * @adev: Alternate mode handle
> > + * @header: VDM Header
> > + * @vdo: Array of Vendor Defined Data Objects
> > + * @count: Number of Data Objects
> > + *
> > + * The alternate mode drivers use this function for SVID specific communication
> > + * with the partner. The port drivers use it to deliver the Structured VDMs
> > + * received from the partners to the alternate mode drivers.
> > + */
> > +int typec_altmode_vdm(struct typec_altmode *adev,
> > + const u32 header, const u32 *vdo, int count)
> > +{
> > + struct altmode *altmode;
> > + struct altmode *partner;
> > +
> > + if (!adev)
> > + return 0;
> > +
> > + altmode = to_altmode(adev);
> > +
> > + if (!altmode->partner)
> > + return -ENODEV;
> > +
> > + partner = altmode->partner;
> > +
> > + if (partner->ops && partner->ops->vdm)
> > + return partner->ops->vdm(&partner->adev, header, vdo, count);
> > +
> > + return 0;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_vdm);
> > +
> > +void typec_altmode_register_ops(struct typec_altmode *adev,
> > + const struct typec_altmode_ops *ops)
> > +{
> > + to_altmode(adev)->ops = ops;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_register_ops);
> > +
> > +/* -------------------------------------------------------------------------- */
> > +/* API for the alternate mode drivers */
> > +
> > +/**
> > + * typec_altmode_get_plug - Find cable plug alternate mode
> > + * @adev: Handle to partner alternate mode
> > + * @index: Cable plug index
> > + *
> > + * Increment reference count for cable plug alternate mode device. Returns
> > + * handle to the cable plug alternate mode, or NULL if none is found.
> > + */
> > +struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
> > + int index)
> > +{
> > + struct altmode *port = to_altmode(adev)->partner;
> > +
> > + if (port->plug[index]) {
> > + get_device(&port->plug[index]->adev.dev);
> > + return &port->plug[index]->adev;
> > + }
> > +
> > + return NULL;
> > +}
> > +EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
> > +
> > +/**
> > + * typec_altmode_get_plug - Decrement cable plug alternate mode reference count
>
> typec_altmode_put_plug
Yes.
Thanks,
--
heikki