public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Cc: "Andrei Kuchynski" <akuchynski@chromium.org>,
	"Abhishek Pandit-Subedi" <abhishekpandit@chromium.org>,
	"Benson Leung" <bleung@chromium.org>,
	"Jameson Thies" <jthies@google.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	chrome-platform@lists.linux.dev,
	"Tzung-Bi Shih" <tzungbi@kernel.org>,
	"Guenter Roeck" <groeck@chromium.org>,
	"Dmitry Baryshkov" <dmitry.baryshkov@oss.qualcomm.com>,
	"Łukasz Bartosik" <ukaszb@chromium.org>,
	"Abel Vesa" <abel.vesa@linaro.org>,
	"Pooja Katiyar" <pooja.katiyar@intel.com>,
	"Johan Hovold" <johan@kernel.org>,
	"Hsin-Te Yuan" <yuanhsinte@chromium.org>,
	"Madhu M" <madhu.m@intel.com>,
	"Venkat Jayaraman" <venkat.jayaraman@intel.com>
Subject: Re: [PATCH v4 4/8] usb: typec: Expose alternate mode priority via sysfs
Date: Fri, 16 Jan 2026 12:21:37 +0100	[thread overview]
Message-ID: <2026011625-demanding-gap-8815@gregkh> (raw)
In-Reply-To: <aWj6vLt3iHXjuYjz@kuha>

On Thu, Jan 15, 2026 at 04:33:32PM +0200, Heikki Krogerus wrote:
> Hi Andrei,
> 
> Tue, Jan 13, 2026 at 01:05:32PM +0000, Andrei Kuchynski kirjoitti:
> > This patch introduces a priority sysfs attribute to the USB Type-C
> > alternate mode port interface. This new attribute allows user-space to
> > configure the numeric priority of alternate modes managing their preferred
> > order of operation. 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>
> > Reviewed-by: Benson Leung <bleung@chromium.org>
> > ---
> >  Documentation/ABI/testing/sysfs-class-typec | 11 +++
> >  drivers/usb/typec/class.c                   | 90 ++++++++++++++++++++-
> >  include/linux/usb/typec_altmode.h           |  1 +
> >  3 files changed, 101 insertions(+), 1 deletion(-)
> > 
> > diff --git a/Documentation/ABI/testing/sysfs-class-typec b/Documentation/ABI/testing/sysfs-class-typec
> > index 38e101c17a004..737b76828b509 100644
> > --- a/Documentation/ABI/testing/sysfs-class-typec
> > +++ b/Documentation/ABI/testing/sysfs-class-typec
> > @@ -162,6 +162,17 @@ Description:	Lists the supported USB Modes. The default USB mode that is used
> >  		- usb3 (USB 3.2)
> >  		- usb4 (USB4)
> >  
> > +What:		/sys/class/typec/<port>/<alt-mode>/priority
> > +Date:		July 2025
> > +Contact:	Andrei Kuchynski <akuchynski@chromium.org>
> > +Description:
> > +		Displays and allows setting the priority for a specific alternate mode.
> > +		The priority is an integer in the range 0-255. A lower numerical value
> > +		indicates a higher priority (0 is the highest).
> > +		If the new value is already in use by another mode, the priority of the
> > +		conflicting mode and any subsequent modes will be incremented until they
> > +		are all unique.
> 
> Greg already told you to show the range somehow with this, so the
> total number of (so far known?) alternate modes. Maybe something like
> <index> / <total_num_altmodes> ?
> 
> >  USB Type-C partner devices (eg. /sys/class/typec/port0-partner/)
> >  
> >  What:		/sys/class/typec/<port>-partner/accessory_mode
> > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> > index 0f12d6120511b..1fb5450c0a2f2 100644
> > --- a/drivers/usb/typec/class.c
> > +++ b/drivers/usb/typec/class.c
> > @@ -445,11 +445,88 @@ svid_show(struct device *dev, struct device_attribute *attr, char *buf)
> >  }
> >  static DEVICE_ATTR_RO(svid);
> >  
> > +static int increment_duplicated_priority(struct device *dev, void *data)
> > +{
> > +	if (is_typec_port_altmode(dev)) {
> > +		struct typec_altmode **alt_target = (struct typec_altmode **)data;
> > +		struct typec_altmode *alt = to_typec_altmode(dev);
> > +
> > +		if (alt != *alt_target && alt->priority == (*alt_target)->priority) {
> > +			alt->priority++;
> > +			*alt_target = alt;
> > +			return 1;
> > +		}
> > +	}
> > +	return 0;
> > +}
> > +
> > +static int find_duplicated_priority(struct device *dev, void *data)
> > +{
> > +	if (is_typec_port_altmode(dev)) {
> > +		struct typec_altmode **alt_target = (struct typec_altmode **)data;
> > +		struct typec_altmode *alt = to_typec_altmode(dev);
> > +
> > +		if (alt != *alt_target && alt->priority == (*alt_target)->priority)
> > +			return 1;
> > +	}
> > +	return 0;
> > +}
> > +
> > +static int typec_mode_set_priority(struct typec_altmode *alt, const u8 priority)
> > +{
> > +	struct typec_port *port = to_typec_port(alt->dev.parent);
> > +	const u8 old_priority = alt->priority;
> > +	int res = 1;
> > +
> > +	alt->priority = priority;
> > +	while (res) {
> > +		res = device_for_each_child(&port->dev, &alt, find_duplicated_priority);
> > +		if (res) {
> > +			alt->priority++;
> > +			if (alt->priority == 0) {
> > +				alt->priority = old_priority;
> > +				return -EOVERFLOW;
> > +			}
> > +		}
> > +	}
> > +
> > +	res = 1;
> > +	alt->priority = priority;
> > +	while (res)
> > +		res = device_for_each_child(&port->dev, &alt,
> > +				increment_duplicated_priority);
> 
> Please align the code properly.
> 
> > +	return 0;
> > +}
> > +
> > +static ssize_t priority_store(struct device *dev,
> > +			       struct device_attribute *attr,
> > +			       const char *buf, size_t size)
> > +{
> > +	u8 val;
> > +	int err = kstrtou8(buf, 10, &val);
> > +
> > +	if (!err)
> > +		err = typec_mode_set_priority(to_typec_altmode(dev), val);
> > +
> > +	if (!err)
> > +		return size;
> > +	return err;
> 
> I know not everybody likes the ternary operator, but I would just
> 
>         return err ?: size;

I hate it, so this is fine as is :)

thanks,

greg k-h

  reply	other threads:[~2026-01-16 11:21 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-13 13:05 [PATCH v4 0/8] USB Type-C mode selection Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 1/8] usb: typec: Add mode_control field to port property Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 2/8] platform/chrome: cros_ec_typec: Set no_mode_control flag Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 3/8] usb: typec: ucsi: " Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 4/8] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
2026-01-15 14:33   ` Heikki Krogerus
2026-01-16 11:21     ` Greg Kroah-Hartman [this message]
2026-01-17 19:25     ` Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 5/8] usb: typec: Implement mode selection Andrei Kuchynski
2026-01-15 14:52   ` Heikki Krogerus
2026-01-17 19:28     ` Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 6/8] usb: typec: Introduce mode_selection bit Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 7/8] usb: typec: ucsi: Support mode selection to activate altmodes Andrei Kuchynski
2026-01-15 14:59   ` Heikki Krogerus
2026-01-17 19:34     ` Andrei Kuchynski
2026-01-13 13:05 ` [PATCH v4 8/8] usb: typec: ucsi: Enforce mode selection for cros_ec_ucsi Andrei Kuchynski
2026-01-14 14:52 ` [PATCH v4 0/8] USB Type-C mode selection Greg Kroah-Hartman

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=2026011625-demanding-gap-8815@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=abel.vesa@linaro.org \
    --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=groeck@chromium.org \
    --cc=heikki.krogerus@linux.intel.com \
    --cc=johan@kernel.org \
    --cc=jthies@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=madhu.m@intel.com \
    --cc=pooja.katiyar@intel.com \
    --cc=tzungbi@kernel.org \
    --cc=ukaszb@chromium.org \
    --cc=venkat.jayaraman@intel.com \
    --cc=yuanhsinte@chromium.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox