Re: [PATCH v2 1/3] usb: typec: Add helper to check cable altmode support

From: Heikki Krogerus

Date: Tue Jun 23 2026 - 04:30:54 EST


On Mon, Jun 22, 2026 at 02:03:48PM +0200, Andrei Kuchynski wrote:
> Hi Heikki,
>
> On Mon, Jun 22, 2026 at 12:18 PM Heikki Krogerus
> <heikki.krogerus@xxxxxxxxxxxxxxx> wrote:
> >
> > Hi Andrei,
> >
> > > +bool typec_cable_altmode_unsupported(struct typec_altmode *alt)
> > > +{
> > > + return unsupported;
> >
> > So if typec_cable_get() doesn't return a cable, this function will now
> > always return false - i.e. the cable is supported? Is that intentional?
> >
>
> Yes, this is intentional.
> The UCSI GET_CABLE_PROPERTY command is optional. typec_register_cable()
> function can also be called without the desc->identity field.
> Failing to provide cable information shouldn't be a reason to reject the
> alternate mode.
> Also, the cable can be registered after the partner's altmodes. We
> encounter this scenario with the cros_ec_typec driver. I'm planning to fix
> it, but for now, this approach will preserve the previous behavior.
>
> Therefore, the idea is to reject the altmode only if we know the cable
> doesn't support it. That's why the function is called "unsupported".
> It returns true if a limitation is detected. Otherwise, the function
> returns false.

One day and I'm completely confused again :). I opened the code for
myself (not compiled) to get the idea again. Please consider that, or
something like it - the important part for me is the enum. The enum
does not cost that many lines, but it does make the idea more clear,
at least for me.


enum typec_cable_altmode_support {
CABLE_SUPPORT_UNKNOWN,
CABLE_SUPPORTED,
CABLE_NOT_SUPPORTED,
};

static enum typec_cable_altmode_support
typec_cable_check_altmode_support(struct typec_cable *cable,
struct typec_altmode *alt)
{
struct typec_altmode *plug;
const u32 speed;

plug = typec_altmode_get_plug(alt);
if (plug) {
typec_altmode_put_plug(plug);
return CABLE_SUPPORTED;
}

/* Maybe UCSI, or the something else, does no supply the identity */
if (!cable->identity)
return ALTMODE_SUPPORT_UNKNOWN;

/* Non-e-marked cable */
if (!cable->identity->id_header)
return CABLE_NOT_SUPPORTED;

speed = VDO_TYPEC_CABLE_SPEED(cable->identity->vdo[0]);

switch (PD_IDH_PTYPE(cable->identity->id_header)) {
case IDH_PTYPE_PCABLE:
if (speed == CABLE_USB2_ONLY)
return CABLE_NOT_SUPPORTED;
break;
case IDH_PTYPE_ACABLE:
/*
* Active cables must establish an SOP' communication
* node. Since that check failed at the beginning of
* this function, this active cable does not support
* this specific altmode.
*/
return CABLE_NOT_SUPPORTED;
default:
break;
}

return CABLE_SUPPORT_UNKNOWN;
}

bool typec_altmode_cable_not_supported(struct typec_altmode *alt)
{
enum typec_cable_altmode_support support = CABLE_SUPPORT_UNKNOWN;
struct typec_cable *cable;

cable = typec_cable_get(typec_altmode2port(alt));
if (cable) {
support = typec_cable_check_altmode_support(cable, alt);
typec_cable_put(cable);
}

return support == CABLE_NOT_SUPPORTED;
}


thanks,

--
heikki