On Wed, Nov 13, 2024 at 08:01:57PM -0800, Abhishek Pandit-Subedi wrote: > On Fri, Nov 8, 2024 at 10:41 PM Dmitry Baryshkov > <dmitry.baryshkov@xxxxxxxxxx> wrote: > > > > On Thu, Nov 07, 2024 at 11:29:59AM -0800, Abhishek Pandit-Subedi wrote: > > > Add support for entering and exiting Thunderbolt alt-mode using AP > > > driven alt-mode. > > > > > > Signed-off-by: Abhishek Pandit-Subedi <abhishekpandit@xxxxxxxxxxxx> > > > --- > > > > > > Changes in v3: > > > - Fix usage of TBT sid and mode. > > > - Removed unused vdm operations during altmode registration > > > > > > Changes in v2: > > > - Refactored thunderbolt support into cros_typec_altmode.c > > > > > > drivers/platform/chrome/Makefile | 3 + > > > drivers/platform/chrome/cros_ec_typec.c | 23 +++--- > > > drivers/platform/chrome/cros_typec_altmode.c | 85 ++++++++++++++++++++ > > > drivers/platform/chrome/cros_typec_altmode.h | 14 ++++ > > > 4 files changed, 114 insertions(+), 11 deletions(-) > > > > > > diff --git a/drivers/platform/chrome/Makefile b/drivers/platform/chrome/Makefile > > > index 2f90d4db8099..b9b1281de063 100644 > > > --- a/drivers/platform/chrome/Makefile > > > +++ b/drivers/platform/chrome/Makefile > > > @@ -21,6 +21,9 @@ cros-ec-typec-objs := cros_ec_typec.o cros_typec_vdm.o > > > ifneq ($(CONFIG_TYPEC_DP_ALTMODE),) > > > cros-ec-typec-objs += cros_typec_altmode.o > > > endif > > > +ifneq ($(CONFIG_TYPEC_TBT_ALTMODE),) > > > + cros-ec-typec-objs += cros_typec_altmode.o > > > +endif > > > > Doesn't this also result in the object file being included twice and > > thus in a duplicate symbols declaration? > > I was trying to figure out how to add this file if either of those > config options existed in > https://docs.kernel.org/kbuild/makefiles.html#built-in-object-goals-obj-y > and it says, "Duplicates in the lists are allowed: the first instance > will be linked into built-in.a and succeeding instances will be > ignored." > > Is there a preferred way of doing the following in the Makefile: > if (defined(CONFIG_TYPEC_TBT_ALTMODE) || > defined(CONFIG_TYPEC_DP_ALTMODE)) {...} > > I briefly considered the following and dropped it because it is > terrible readability-wise: > ifneq ($(CONFIG_TYPEC_TBT_ALTMODE)$(CONFIG_TYPEC_DP_ALTMODE),) The usual way would to define new Kconfig symbol: config CROS_EC_TYPEC_ALTMODES bool # Note, no description here, don't show in menuconfig help Selectable symbol to enable altmodes config CROS_EC_TYPEC ... select CROS_EC_TYPEC_ALTMODES if CONFIG_TYPEC_DP_ALTMODE select CROS_EC_TYPEC_ALTMODES if CONFIG_TYPEC_TBT_ALTMODE ... ---- cros-ec-typec-$(CONFIG_CROS_EC_TYPEC_ALTMODES) += cros_typec_altmode.o > > > > > > obj-$(CONFIG_CROS_EC_TYPEC) += cros-ec-typec.o > > > > > > obj-$(CONFIG_CROS_EC_LPC) += cros_ec_lpcs.o > > > diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c > > > index 3a6f5f2717b9..558b618df63c 100644 > > > --- a/drivers/platform/chrome/cros_ec_typec.c > > > +++ b/drivers/platform/chrome/cros_ec_typec.c > > > @@ -302,18 +302,19 @@ static int cros_typec_register_port_altmodes(struct cros_typec_data *typec, > > > > > > /* > > > * Register TBT compatibility alt mode. The EC will not enter the mode > > > - * if it doesn't support it, so it's safe to register it unconditionally > > > - * here for now. > > > + * if it doesn't support it and it will not enter automatically by > > > + * design so we can use the |ap_driven_altmode| feature to check if we > > > + * should register it. > > > */ > > > - memset(&desc, 0, sizeof(desc)); > > > - desc.svid = USB_TYPEC_TBT_SID; > > > - desc.mode = TYPEC_ANY_MODE; > > > - amode = typec_port_register_altmode(port->port, &desc); > > > - if (IS_ERR(amode)) > > > - return PTR_ERR(amode); > > > - port->port_altmode[CROS_EC_ALTMODE_TBT] = amode; > > > - typec_altmode_set_drvdata(amode, port); > > > - amode->ops = &port_amode_ops; > > > + if (typec->ap_driven_altmode) { > > > + memset(&desc, 0, sizeof(desc)); > > > + desc.svid = USB_TYPEC_TBT_SID; > > > + desc.mode = TBT_MODE; > > > + amode = cros_typec_register_thunderbolt(port, &desc); > > > + if (IS_ERR(amode)) > > > + return PTR_ERR(amode); > > > + port->port_altmode[CROS_EC_ALTMODE_TBT] = amode; > > > + } > > > > > > port->state.alt = NULL; > > > port->state.mode = TYPEC_STATE_USB; > > > diff --git a/drivers/platform/chrome/cros_typec_altmode.c b/drivers/platform/chrome/cros_typec_altmode.c > > > index 3598b8a6ceee..9cf2cef6c277 100644 > > > --- a/drivers/platform/chrome/cros_typec_altmode.c > > > +++ b/drivers/platform/chrome/cros_typec_altmode.c > > > @@ -8,6 +8,7 @@ > > > #include "cros_ec_typec.h" > > > > > > #include <linux/usb/typec_dp.h> > > > +#include <linux/usb/typec_tbt.h> > > > #include <linux/usb/pd_vdo.h> > > > > > > #include "cros_typec_altmode.h" > > > @@ -67,6 +68,8 @@ static int cros_typec_altmode_enter(struct typec_altmode *alt, u32 *vdo) > > > > > > if (data->sid == USB_TYPEC_DP_SID) > > > req.mode_to_enter = CROS_EC_ALTMODE_DP; > > > + else if (data->sid == USB_TYPEC_TBT_SID) > > > + req.mode_to_enter = CROS_EC_ALTMODE_TBT; > > > else > > > return -EOPNOTSUPP; > > > > > > @@ -196,6 +199,53 @@ static int cros_typec_displayport_vdm(struct typec_altmode *alt, u32 header, > > > return 0; > > > } > > > > > > +static int cros_typec_thunderbolt_vdm(struct typec_altmode *alt, u32 header, > > > + const u32 *data, int count) > > > +{ > > > + struct cros_typec_altmode_data *adata = typec_altmode_get_drvdata(alt); > > > + > > > + int cmd_type = PD_VDO_CMDT(header); > > > + int cmd = PD_VDO_CMD(header); > > > + int svdm_version; > > > > I suppose that with the current approach this misses the ap_mode_entry > > check. If it gets moved to cros_typec_altmode_vdm(), then it should be > > okay. > > We don't register the thunderbolt port driver if ap_mode_entry is > false so it's an unnecessary check. Why don't you register it? It would allow userspace to understand, what is happening, e.g. that the Type-C has switched to the TBT mode. > > > > > > + > > > + svdm_version = typec_altmode_get_svdm_version(alt); > > > + if (svdm_version < 0) > > > + return svdm_version; > > > + > > > + switch (cmd_type) { > > > + case CMDT_INIT: > > > + if (PD_VDO_SVDM_VER(header) < svdm_version) { > > > + typec_partner_set_svdm_version(adata->port->partner, > > > + PD_VDO_SVDM_VER(header)); > > > + svdm_version = PD_VDO_SVDM_VER(header); > > > + } > > > + > > > + adata->header = VDO(adata->sid, 1, svdm_version, cmd); > > > + adata->header |= VDO_OPOS(adata->mode); > > > + > > > + switch (cmd) { > > > + case CMD_ENTER_MODE: > > > + /* Don't respond to the enter mode vdm because it > > > + * triggers mux configuration. This is handled directly > > > + * by the cros_ec_typec driver so the Thunderbolt driver > > > + * doesn't need to be involved. > > > + */ > > > + break; > > > + default: > > > + adata->header |= VDO_CMDT(CMDT_RSP_ACK); > > > + schedule_work(&adata->work); > > > + break; > > > + } > > > + > > > + break; > > > + default: > > > + break; > > > + } > > > + > > > + return 0; > > > +} > > > + > > > + > > > static int cros_typec_altmode_vdm(struct typec_altmode *alt, u32 header, > > > const u32 *data, int count) > > > { > > > @@ -204,6 +254,9 @@ static int cros_typec_altmode_vdm(struct typec_altmode *alt, u32 header, > > > if (adata->sid == USB_TYPEC_DP_SID) > > > return cros_typec_displayport_vdm(alt, header, data, count); > > > > > > + if (adata->sid == USB_TYPEC_TBT_SID) > > > + return cros_typec_thunderbolt_vdm(alt, header, data, count); > > > + > > > return -EINVAL; > > > } > > > > > > @@ -273,3 +326,35 @@ cros_typec_register_displayport(struct cros_typec_port *port, > > > return alt; > > > } > > > #endif > > > + > > > +#if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE) > > > +struct typec_altmode * > > > +cros_typec_register_thunderbolt(struct cros_typec_port *port, > > > + struct typec_altmode_desc *desc) > > > +{ > > > + struct typec_altmode *alt; > > > + struct cros_typec_altmode_data *data; > > > + > > > + alt = typec_port_register_altmode(port->port, desc); > > > + if (IS_ERR(alt)) > > > + return alt; > > > + > > > + data = devm_kzalloc(&alt->dev, sizeof(*data), GFP_KERNEL); > > > + if (!data) { > > > + typec_unregister_altmode(alt); > > > + return ERR_PTR(-ENOMEM); > > > + } > > > + > > > + INIT_WORK(&data->work, cros_typec_altmode_work); > > > + data->alt = alt; > > > + data->port = port; > > > + data->ap_mode_entry = true; > > > + data->sid = desc->svid; > > > + data->mode = desc->mode; > > > + > > > + typec_altmode_set_ops(alt, &cros_typec_altmode_ops); > > > + typec_altmode_set_drvdata(alt, data); > > > + > > > + return alt; > > > +} > > > +#endif > > > diff --git a/drivers/platform/chrome/cros_typec_altmode.h b/drivers/platform/chrome/cros_typec_altmode.h > > > index c6f8fb02c99c..810b553ddcd8 100644 > > > --- a/drivers/platform/chrome/cros_typec_altmode.h > > > +++ b/drivers/platform/chrome/cros_typec_altmode.h > > > @@ -31,4 +31,18 @@ static inline int cros_typec_displayport_status_update(struct typec_altmode *alt > > > return 0; > > > } > > > #endif > > > + > > > +#if IS_ENABLED(CONFIG_TYPEC_TBT_ALTMODE) > > > +struct typec_altmode * > > > +cros_typec_register_thunderbolt(struct cros_typec_port *port, > > > + struct typec_altmode_desc *desc); > > > +#else > > > +static inline struct typec_altmode * > > > +cros_typec_register_thunderbolt(struct cros_typec_port *port, > > > + struct typec_altmode_desc *desc) > > > +{ > > > + return typec_port_register_altmode(port->port, desc); > > > +} > > > +#endif > > > + > > > #endif /* __CROS_TYPEC_ALTMODE_H__ */ > > > -- > > > 2.47.0.277.g8800431eea-goog > > > > > > > -- > > With best wishes > > Dmitry -- With best wishes Dmitry