All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heikki Krogerus <heikki.krogerus@linux.intel.com>
To: Andrei Kuchynski <akuchynski@chromium.org>
Cc: Abhishek Pandit-Subedi <abhishekpandit@chromium.org>,
	Benson Leung <bleung@chromium.org>,
	Jameson Thies <jthies@google.com>,
	Tzung-Bi Shih <tzungbi@kernel.org>,
	linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev,
	Guenter Roeck <groeck@chromium.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Dmitry Baryshkov <dmitry.baryshkov@oss.qualcomm.com>,
	"Christian A. Ehrhardt" <lk@c--e.de>,
	Venkat Jayaraman <venkat.jayaraman@intel.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 4/5] usb: typec: Implement alternate mode priority handling
Date: Fri, 5 Sep 2025 13:25:36 +0300	[thread overview]
Message-ID: <aLq6oNcA_yoMZyrf@kuha.fi.intel.com> (raw)
In-Reply-To: <20250825145750.58820-5-akuchynski@chromium.org>

On Mon, Aug 25, 2025 at 02:57:49PM +0000, Andrei Kuchynski wrote:
> This patch introduces APIs to manage the priority of USB Type-C alternate
> modes. These APIs allow for setting and retrieving a priority number for
> each mode. If a new priority value conflicts with an existing mode's
> priority, the priorities of the conflicting mode and all subsequent modes
> are automatically incremented to ensure uniqueness.
> 
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> ---
>  drivers/usb/typec/Makefile         |  2 +-
>  drivers/usb/typec/mode_selection.c | 38 ++++++++++++++++++++++++++++++
>  drivers/usb/typec/mode_selection.h |  6 +++++
>  include/linux/usb/typec_altmode.h  |  1 +
>  4 files changed, 46 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/usb/typec/mode_selection.c
>  create mode 100644 drivers/usb/typec/mode_selection.h
> 
> diff --git a/drivers/usb/typec/Makefile b/drivers/usb/typec/Makefile
> index 7a368fea61bc..8a6a1c663eb6 100644
> --- a/drivers/usb/typec/Makefile
> +++ b/drivers/usb/typec/Makefile
> @@ -1,6 +1,6 @@
>  # SPDX-License-Identifier: GPL-2.0
>  obj-$(CONFIG_TYPEC)		+= typec.o
> -typec-y				:= class.o mux.o bus.o pd.o retimer.o
> +typec-y				:= class.o mux.o bus.o pd.o retimer.o mode_selection.o
>  typec-$(CONFIG_ACPI)		+= port-mapper.o
>  obj-$(CONFIG_TYPEC)		+= altmodes/
>  obj-$(CONFIG_TYPEC_TCPM)	+= tcpm/
> diff --git a/drivers/usb/typec/mode_selection.c b/drivers/usb/typec/mode_selection.c
> new file mode 100644
> index 000000000000..2179bf25f5d4
> --- /dev/null
> +++ b/drivers/usb/typec/mode_selection.c
> @@ -0,0 +1,38 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright 2025 Google LLC.
> + */
> +
> +#include "mode_selection.h"
> +#include "class.h"
> +#include "bus.h"
> +
> +static int increment_duplicated_priority(struct device *dev, void *data)
> +{
> +	struct typec_altmode **alt_target = (struct typec_altmode **)data;
> +
> +	if (is_typec_altmode(dev)) {
> +		struct typec_altmode *alt = to_typec_altmode(dev);
> +
> +		if (alt != *alt_target && alt->priority == (*alt_target)->priority) {
> +			alt->priority++;
> +			*alt_target = alt;
> +			return 1;

Couldn't you just always return 0?

> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +void typec_mode_set_priority(struct typec_altmode *alt,
> +		const unsigned int priority)
> +{
> +	struct typec_port *port = to_typec_port(alt->dev.parent);
> +	int res = 1;
> +
> +	alt->priority = priority;
> +
> +	while (res)
> +		res = device_for_each_child(&port->dev, &alt,
> +				increment_duplicated_priority);

Then this could be:

        device_for_each_child(&port->dev, &alt, increment_duplicated_priority);

right?

> +}
> diff --git a/drivers/usb/typec/mode_selection.h b/drivers/usb/typec/mode_selection.h
> new file mode 100644
> index 000000000000..cbf5a37e6404
> --- /dev/null
> +++ b/drivers/usb/typec/mode_selection.h
> @@ -0,0 +1,6 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +
> +#include <linux/usb/typec_altmode.h>
> +
> +void typec_mode_set_priority(struct typec_altmode *alt,
> +		const unsigned int priority);
> diff --git a/include/linux/usb/typec_altmode.h b/include/linux/usb/typec_altmode.h
> index b3c0866ea70f..571c6e00b54f 100644
> --- a/include/linux/usb/typec_altmode.h
> +++ b/include/linux/usb/typec_altmode.h
> @@ -28,6 +28,7 @@ struct typec_altmode {
>  	int				mode;
>  	u32				vdo;
>  	unsigned int			active:1;
> +	unsigned int			priority;
>  
>  	char				*desc;
>  	const struct typec_altmode_ops	*ops;
> -- 
> 2.51.0.rc2.233.g662b1ed5c5-goog
> 

-- 
heikki

  reply	other threads:[~2025-09-05 10:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-25 14:57 [PATCH v2 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
2025-08-25 14:57 ` [PATCH v2 1/5] usb: typec: Add alt_mode_override field to port property Andrei Kuchynski
2025-09-05  9:57   ` Heikki Krogerus
2025-09-05 11:53     ` Andrei Kuchynski
2025-08-25 14:57 ` [PATCH v2 2/5] platform/chrome: cros_ec_typec: Set alt_mode_override flag Andrei Kuchynski
2025-09-05  9:58   ` Heikki Krogerus
2025-08-25 14:57 ` [PATCH v2 3/5] usb: typec: ucsi: " Andrei Kuchynski
2025-09-05  9:59   ` Heikki Krogerus
2025-09-05 11:57     ` Andrei Kuchynski
2025-08-25 14:57 ` [PATCH v2 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
2025-09-05 10:25   ` Heikki Krogerus [this message]
2025-09-05 11:59     ` Andrei Kuchynski
2025-08-25 14:57 ` [PATCH v2 5/5] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
2025-09-05 10:34   ` Heikki Krogerus
2025-09-05 12:22     ` Andrei Kuchynski
2025-09-05 11:57 ` [PATCH v2 0/5] USB Type-C alternate mode priorities Heikki Krogerus
2025-09-05 12:41   ` Andrei Kuchynski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aLq6oNcA_yoMZyrf@kuha.fi.intel.com \
    --to=heikki.krogerus@linux.intel.com \
    --cc=abhishekpandit@chromium.org \
    --cc=akuchynski@chromium.org \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=dmitry.baryshkov@oss.qualcomm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=groeck@chromium.org \
    --cc=jthies@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=lk@c--e.de \
    --cc=tzungbi@kernel.org \
    --cc=venkat.jayaraman@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.