* [PATCH v3 0/5] USB Type-C alternate mode priorities
@ 2025-09-05 14:22 Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
` (4 more replies)
0 siblings, 5 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
This patch series introduces a mechanism for setting USB Type-C alternate
mode priorities. It allows the user to specify their preferred order for
mode selection, such as USB4, Thunderbolt, or DisplayPort.
A new sysfs attribute named 'priority' is exposed to provide user-space
control over the mode selection process.
This series was tested on a Android OS device running kernel 6.16.
Changes in v3:
- minor changes related to code style and documentation
Andrei Kuchynski (5):
usb: typec: Add mode_control field to port property
platform/chrome: cros_ec_typec: Set no_mode_control flag
usb: typec: ucsi: Set no_mode_control flag
usb: typec: Implement alternate mode priority handling
usb: typec: Expose alternate mode priority via sysfs
Documentation/ABI/testing/sysfs-class-typec | 11 ++++++
drivers/platform/chrome/cros_ec_typec.c | 1 +
drivers/usb/typec/Makefile | 2 +-
drivers/usb/typec/class.c | 41 +++++++++++++++++++--
drivers/usb/typec/class.h | 2 +
drivers/usb/typec/mode_selection.c | 38 +++++++++++++++++++
drivers/usb/typec/mode_selection.h | 6 +++
drivers/usb/typec/ucsi/ucsi.c | 1 +
include/linux/usb/typec.h | 2 +
include/linux/usb/typec_altmode.h | 1 +
10 files changed, 100 insertions(+), 5 deletions(-)
create mode 100644 drivers/usb/typec/mode_selection.c
create mode 100644 drivers/usb/typec/mode_selection.h
--
2.51.0.355.g5224444f11-goog
^ permalink raw reply [flat|nested] 22+ messages in thread
* [PATCH v3 1/5] usb: typec: Add mode_control field to port property
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
@ 2025-09-05 14:22 ` Andrei Kuchynski
2025-09-09 8:47 ` Heikki Krogerus
2025-09-10 13:30 ` Greg Kroah-Hartman
2025-09-05 14:22 ` [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag Andrei Kuchynski
` (3 subsequent siblings)
4 siblings, 2 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
This new field in the port properties dictates whether the Platform Policy
Manager (PPM) allows the OS Policy Manager (OPM) to change the currently
active, negotiated alternate mode.
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
drivers/usb/typec/class.c | 9 ++++++---
drivers/usb/typec/class.h | 2 ++
include/linux/usb/typec.h | 2 ++
3 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
index 67a533e35150..9f86605ce125 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -457,11 +457,13 @@ static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
struct attribute *attr, int n)
{
struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
+ struct typec_port *port = typec_altmode2port(adev);
if (attr == &dev_attr_active.attr)
- if (!is_typec_port(adev->dev.parent) &&
- (!adev->ops || !adev->ops->activate))
- return 0444;
+ if (!is_typec_port(adev->dev.parent)) {
+ if (!port->mode_control || !adev->ops || !adev->ops->activate)
+ return 0444;
+ }
return attr->mode;
}
@@ -2681,6 +2683,7 @@ struct typec_port *typec_register_port(struct device *parent,
}
port->pd = cap->pd;
+ port->mode_control = !cap->no_mode_control;
ret = device_add(&port->dev);
if (ret) {
diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
index db2fe96c48ff..c53a04b9dc75 100644
--- a/drivers/usb/typec/class.h
+++ b/drivers/usb/typec/class.h
@@ -80,6 +80,8 @@ struct typec_port {
*/
struct device *usb2_dev;
struct device *usb3_dev;
+
+ bool mode_control;
};
#define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
index 252af3f77039..8f51665d44ec 100644
--- a/include/linux/usb/typec.h
+++ b/include/linux/usb/typec.h
@@ -287,6 +287,7 @@ enum usb_pd_svdm_ver {
* @prefer_role: Initial role preference (DRP ports).
* @accessory: Supported Accessory Modes
* @usb_capability: Supported USB Modes
+ * @no_mode_control: Ability to manage Alternate Modes
* @fwnode: Optional fwnode of the port
* @driver_data: Private pointer for driver specific info
* @pd: Optional USB Power Delivery Support
@@ -304,6 +305,7 @@ struct typec_capability {
enum typec_accessory accessory[TYPEC_MAX_ACCESSORY];
unsigned int orientation_aware:1;
u8 usb_capability;
+ bool no_mode_control;
struct fwnode_handle *fwnode;
void *driver_data;
--
2.51.0.355.g5224444f11-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
@ 2025-09-05 14:22 ` Andrei Kuchynski
2025-09-08 3:57 ` Tzung-Bi Shih
2025-09-05 14:22 ` [PATCH v3 3/5] usb: typec: ucsi: " Andrei Kuchynski
` (2 subsequent siblings)
4 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
This flag specifies that the Embedded Controller (EC) must receive explicit
approval from the Application Processor (AP) before initiating Type-C
alternate modes or USB4 mode.
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
drivers/platform/chrome/cros_ec_typec.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/platform/chrome/cros_ec_typec.c b/drivers/platform/chrome/cros_ec_typec.c
index b712bcff6fb2..c0806c562bb9 100644
--- a/drivers/platform/chrome/cros_ec_typec.c
+++ b/drivers/platform/chrome/cros_ec_typec.c
@@ -491,6 +491,7 @@ static int cros_typec_init_ports(struct cros_typec_data *typec)
cap->driver_data = cros_port;
cap->ops = &cros_typec_usb_mode_ops;
+ cap->no_mode_control = !typec->ap_driven_altmode;
cros_port->port = typec_register_port(dev, cap);
if (IS_ERR(cros_port->port)) {
--
2.51.0.355.g5224444f11-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v3 3/5] usb: typec: ucsi: Set no_mode_control flag
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag Andrei Kuchynski
@ 2025-09-05 14:22 ` Andrei Kuchynski
2025-09-09 9:07 ` Heikki Krogerus
2025-09-05 14:22 ` [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
4 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
This flag indicates that the PPM allows the OPM to change the currently
negotiated alternate mode using the SET_NEW_CAM command.
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
drivers/usb/typec/ucsi/ucsi.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index 0d6b0cf5a7cd..9f754344a841 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -1632,6 +1632,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
cap->driver_data = con;
cap->ops = &ucsi_ops;
+ cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
if (ucsi->ops->update_connector)
ucsi->ops->update_connector(con);
--
2.51.0.355.g5224444f11-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
` (2 preceding siblings ...)
2025-09-05 14:22 ` [PATCH v3 3/5] usb: typec: ucsi: " Andrei Kuchynski
@ 2025-09-05 14:22 ` Andrei Kuchynski
2025-09-09 8:50 ` Heikki Krogerus
2025-09-10 13:31 ` Greg Kroah-Hartman
2025-09-05 14:22 ` [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
4 siblings, 2 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
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;
+ }
+ }
+
+ 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);
+}
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.355.g5224444f11-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread
* [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
` (3 preceding siblings ...)
2025-09-05 14:22 ` [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
@ 2025-09-05 14:22 ` Andrei Kuchynski
2025-09-10 13:27 ` Greg Kroah-Hartman
4 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-05 14:22 UTC (permalink / raw)
To: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform
Cc: Guenter Roeck, Greg Kroah-Hartman, Dmitry Baryshkov,
Christian A. Ehrhardt, Venkat Jayaraman, linux-kernel,
Andrei Kuchynski
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.
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
Documentation/ABI/testing/sysfs-class-typec | 11 +++++++
drivers/usb/typec/class.c | 32 ++++++++++++++++++++-
2 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/Documentation/ABI/testing/sysfs-class-typec b/Documentation/ABI/testing/sysfs-class-typec
index 38e101c17a00..dab3e4e727b6 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 alt-mode.
+ When read, it shows the current integer priority value. Lower numerical
+ values indicate higher priority (0 is the highest priority).
+ 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.
+
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 9f86605ce125..aaab2e1e98b4 100644
--- a/drivers/usb/typec/class.c
+++ b/drivers/usb/typec/class.c
@@ -19,6 +19,7 @@
#include "bus.h"
#include "class.h"
#include "pd.h"
+#include "mode_selection.h"
static DEFINE_IDA(typec_index_ida);
@@ -445,11 +446,34 @@ svid_show(struct device *dev, struct device_attribute *attr, char *buf)
}
static DEVICE_ATTR_RO(svid);
+static ssize_t priority_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ unsigned int val;
+ int err = kstrtouint(buf, 10, &val);
+
+ if (!err) {
+ typec_mode_set_priority(to_typec_altmode(dev), val);
+ return size;
+ }
+
+ return err;
+}
+
+static ssize_t priority_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ return sprintf(buf, "%u\n", to_typec_altmode(dev)->priority);
+}
+static DEVICE_ATTR_RW(priority);
+
static struct attribute *typec_altmode_attrs[] = {
&dev_attr_active.attr,
&dev_attr_mode.attr,
&dev_attr_svid.attr,
&dev_attr_vdo.attr,
+ &dev_attr_priority.attr,
NULL
};
@@ -459,11 +483,15 @@ static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
struct typec_port *port = typec_altmode2port(adev);
- if (attr == &dev_attr_active.attr)
+ if (attr == &dev_attr_active.attr) {
if (!is_typec_port(adev->dev.parent)) {
if (!port->mode_control || !adev->ops || !adev->ops->activate)
return 0444;
}
+ } else if (attr == &dev_attr_priority.attr) {
+ if (!is_typec_port(adev->dev.parent) || !port->mode_control)
+ return 0;
+ }
return attr->mode;
}
@@ -2491,6 +2519,8 @@ typec_port_register_altmode(struct typec_port *port,
to_altmode(adev)->retimer = retimer;
}
+ typec_mode_set_priority(adev, 0);
+
return adev;
}
EXPORT_SYMBOL_GPL(typec_port_register_altmode);
--
2.51.0.355.g5224444f11-goog
^ permalink raw reply related [flat|nested] 22+ messages in thread
* Re: [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag
2025-09-05 14:22 ` [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag Andrei Kuchynski
@ 2025-09-08 3:57 ` Tzung-Bi Shih
0 siblings, 0 replies; 22+ messages in thread
From: Tzung-Bi Shih @ 2025-09-08 3:57 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:03PM +0000, Andrei Kuchynski wrote:
> This flag specifies that the Embedded Controller (EC) must receive explicit
> approval from the Application Processor (AP) before initiating Type-C
> alternate modes or USB4 mode.
>
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Acked-by: Tzung-Bi Shih <tzungbi@kernel.org>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 1/5] usb: typec: Add mode_control field to port property
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
@ 2025-09-09 8:47 ` Heikki Krogerus
2025-09-10 13:30 ` Greg Kroah-Hartman
1 sibling, 0 replies; 22+ messages in thread
From: Heikki Krogerus @ 2025-09-09 8:47 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Abhishek Pandit-Subedi, Benson Leung, Jameson Thies,
Tzung-Bi Shih, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:02PM +0000, Andrei Kuchynski wrote:
> This new field in the port properties dictates whether the Platform Policy
> Manager (PPM) allows the OS Policy Manager (OPM) to change the currently
> active, negotiated alternate mode.
>
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> drivers/usb/typec/class.c | 9 ++++++---
> drivers/usb/typec/class.h | 2 ++
> include/linux/usb/typec.h | 2 ++
> 3 files changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 67a533e35150..9f86605ce125 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -457,11 +457,13 @@ static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
> struct attribute *attr, int n)
> {
> struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
> + struct typec_port *port = typec_altmode2port(adev);
>
> if (attr == &dev_attr_active.attr)
> - if (!is_typec_port(adev->dev.parent) &&
> - (!adev->ops || !adev->ops->activate))
> - return 0444;
> + if (!is_typec_port(adev->dev.parent)) {
> + if (!port->mode_control || !adev->ops || !adev->ops->activate)
> + return 0444;
> + }
>
> return attr->mode;
> }
> @@ -2681,6 +2683,7 @@ struct typec_port *typec_register_port(struct device *parent,
> }
>
> port->pd = cap->pd;
> + port->mode_control = !cap->no_mode_control;
>
> ret = device_add(&port->dev);
> if (ret) {
> diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
> index db2fe96c48ff..c53a04b9dc75 100644
> --- a/drivers/usb/typec/class.h
> +++ b/drivers/usb/typec/class.h
> @@ -80,6 +80,8 @@ struct typec_port {
> */
> struct device *usb2_dev;
> struct device *usb3_dev;
> +
> + bool mode_control;
> };
>
> #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
> diff --git a/include/linux/usb/typec.h b/include/linux/usb/typec.h
> index 252af3f77039..8f51665d44ec 100644
> --- a/include/linux/usb/typec.h
> +++ b/include/linux/usb/typec.h
> @@ -287,6 +287,7 @@ enum usb_pd_svdm_ver {
> * @prefer_role: Initial role preference (DRP ports).
> * @accessory: Supported Accessory Modes
> * @usb_capability: Supported USB Modes
> + * @no_mode_control: Ability to manage Alternate Modes
> * @fwnode: Optional fwnode of the port
> * @driver_data: Private pointer for driver specific info
> * @pd: Optional USB Power Delivery Support
> @@ -304,6 +305,7 @@ struct typec_capability {
> enum typec_accessory accessory[TYPEC_MAX_ACCESSORY];
> unsigned int orientation_aware:1;
> u8 usb_capability;
> + bool no_mode_control;
>
> struct fwnode_handle *fwnode;
> void *driver_data;
> --
> 2.51.0.355.g5224444f11-goog
--
heikki
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-05 14:22 ` [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
@ 2025-09-09 8:50 ` Heikki Krogerus
2025-09-10 13:31 ` Greg Kroah-Hartman
1 sibling, 0 replies; 22+ messages in thread
From: Heikki Krogerus @ 2025-09-09 8:50 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Abhishek Pandit-Subedi, Benson Leung, Jameson Thies,
Tzung-Bi Shih, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:05PM +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>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> 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;
> + }
> + }
> +
> + 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);
> +}
> 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.355.g5224444f11-goog
--
heikki
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 3/5] usb: typec: ucsi: Set no_mode_control flag
2025-09-05 14:22 ` [PATCH v3 3/5] usb: typec: ucsi: " Andrei Kuchynski
@ 2025-09-09 9:07 ` Heikki Krogerus
2025-09-09 9:22 ` Andrei Kuchynski
0 siblings, 1 reply; 22+ messages in thread
From: Heikki Krogerus @ 2025-09-09 9:07 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Abhishek Pandit-Subedi, Benson Leung, Jameson Thies,
Tzung-Bi Shih, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:04PM +0000, Andrei Kuchynski wrote:
> This flag indicates that the PPM allows the OPM to change the currently
> negotiated alternate mode using the SET_NEW_CAM command.
>
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> ---
> drivers/usb/typec/ucsi/ucsi.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index 0d6b0cf5a7cd..9f754344a841 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -1632,6 +1632,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
>
> cap->driver_data = con;
> cap->ops = &ucsi_ops;
> + cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
The flag is now used to expose the priority file, but you can't affect
the order in which the UCSI attempts to enter the modes, or can you?
thanks,
--
heikki
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 3/5] usb: typec: ucsi: Set no_mode_control flag
2025-09-09 9:07 ` Heikki Krogerus
@ 2025-09-09 9:22 ` Andrei Kuchynski
2025-09-10 19:36 ` Andrei Kuchynski
0 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-09 9:22 UTC (permalink / raw)
To: Heikki Krogerus
Cc: Abhishek Pandit-Subedi, Benson Leung, Jameson Thies,
Tzung-Bi Shih, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Tue, Sep 9, 2025 at 11:08 AM Heikki Krogerus
<heikki.krogerus@linux.intel.com> wrote:
>
> On Fri, Sep 05, 2025 at 02:22:04PM +0000, Andrei Kuchynski wrote:
> > This flag indicates that the PPM allows the OPM to change the currently
> > negotiated alternate mode using the SET_NEW_CAM command.
> >
> > Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> > ---
> > drivers/usb/typec/ucsi/ucsi.c | 1 +
> > 1 file changed, 1 insertion(+)
> >
> > diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> > index 0d6b0cf5a7cd..9f754344a841 100644
> > --- a/drivers/usb/typec/ucsi/ucsi.c
> > +++ b/drivers/usb/typec/ucsi/ucsi.c
> > @@ -1632,6 +1632,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
> >
> > cap->driver_data = con;
> > cap->ops = &ucsi_ops;
> > + cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
>
> The flag is now used to expose the priority file, but you can't affect
> the order in which the UCSI attempts to enter the modes, or can you?
>
> thanks,
>
> --
> heikki
Hello Heikki,
Thank you for your review!
You are right, this patch does not affect the order in which UCSI
attempts to enter modes. Currently, UCSI implementation only has
DisplayPort alternate mode support. We are preparing to submit patches
for Thunderbolt.
Thanks,
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs
2025-09-05 14:22 ` [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
@ 2025-09-10 13:27 ` Greg Kroah-Hartman
2025-09-10 19:34 ` Andrei Kuchynski
0 siblings, 1 reply; 22+ messages in thread
From: Greg Kroah-Hartman @ 2025-09-10 13:27 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:06PM +0000, Andrei Kuchynski wrote:
> 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.
>
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
> Documentation/ABI/testing/sysfs-class-typec | 11 +++++++
> drivers/usb/typec/class.c | 32 ++++++++++++++++++++-
> 2 files changed, 42 insertions(+), 1 deletion(-)
>
> diff --git a/Documentation/ABI/testing/sysfs-class-typec b/Documentation/ABI/testing/sysfs-class-typec
> index 38e101c17a00..dab3e4e727b6 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 alt-mode.
> + When read, it shows the current integer priority value. Lower numerical
> + values indicate higher priority (0 is the highest priority).
> + 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.
What is the units here? 0 is highest, what is the lowest? What is the
range here? I can't determine this from the text, sorry.
> +
> 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 9f86605ce125..aaab2e1e98b4 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -19,6 +19,7 @@
> #include "bus.h"
> #include "class.h"
> #include "pd.h"
> +#include "mode_selection.h"
>
> static DEFINE_IDA(typec_index_ida);
>
> @@ -445,11 +446,34 @@ svid_show(struct device *dev, struct device_attribute *attr, char *buf)
> }
> static DEVICE_ATTR_RO(svid);
>
> +static ssize_t priority_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t size)
> +{
> + unsigned int val;
> + int err = kstrtouint(buf, 10, &val);
> +
> + if (!err) {
> + typec_mode_set_priority(to_typec_altmode(dev), val);
> + return size;
> + }
> +
> + return err;
> +}
> +
> +static ssize_t priority_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + return sprintf(buf, "%u\n", to_typec_altmode(dev)->priority);
For new sysfs files, please use sysfs_emit(), otherwise someone will
come along with a janitorial patch to fix it up after it lands in the
tree :)
thanks,
greg k-h
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 1/5] usb: typec: Add mode_control field to port property
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
2025-09-09 8:47 ` Heikki Krogerus
@ 2025-09-10 13:30 ` Greg Kroah-Hartman
2025-09-10 19:34 ` Andrei Kuchynski
1 sibling, 1 reply; 22+ messages in thread
From: Greg Kroah-Hartman @ 2025-09-10 13:30 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:02PM +0000, Andrei Kuchynski wrote:
> This new field in the port properties dictates whether the Platform Policy
> Manager (PPM) allows the OS Policy Manager (OPM) to change the currently
> active, negotiated alternate mode.
>
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> ---
> drivers/usb/typec/class.c | 9 ++++++---
> drivers/usb/typec/class.h | 2 ++
> include/linux/usb/typec.h | 2 ++
> 3 files changed, 10 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> index 67a533e35150..9f86605ce125 100644
> --- a/drivers/usb/typec/class.c
> +++ b/drivers/usb/typec/class.c
> @@ -457,11 +457,13 @@ static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
> struct attribute *attr, int n)
> {
> struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
> + struct typec_port *port = typec_altmode2port(adev);
>
> if (attr == &dev_attr_active.attr)
> - if (!is_typec_port(adev->dev.parent) &&
> - (!adev->ops || !adev->ops->activate))
> - return 0444;
> + if (!is_typec_port(adev->dev.parent)) {
> + if (!port->mode_control || !adev->ops || !adev->ops->activate)
> + return 0444;
> + }
>
> return attr->mode;
> }
> @@ -2681,6 +2683,7 @@ struct typec_port *typec_register_port(struct device *parent,
> }
>
> port->pd = cap->pd;
> + port->mode_control = !cap->no_mode_control;
>
> ret = device_add(&port->dev);
> if (ret) {
> diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
> index db2fe96c48ff..c53a04b9dc75 100644
> --- a/drivers/usb/typec/class.h
> +++ b/drivers/usb/typec/class.h
> @@ -80,6 +80,8 @@ struct typec_port {
> */
> struct device *usb2_dev;
> struct device *usb3_dev;
> +
> + bool mode_control;
Shouldn't this go up higher in this structure, to avoid the hole you
created, and to take advantage of the existing hole? Perhaps after
orientation?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-05 14:22 ` [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
2025-09-09 8:50 ` Heikki Krogerus
@ 2025-09-10 13:31 ` Greg Kroah-Hartman
2025-09-10 19:35 ` Andrei Kuchynski
1 sibling, 1 reply; 22+ messages in thread
From: Greg Kroah-Hartman @ 2025-09-10 13:31 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Fri, Sep 05, 2025 at 02:22:05PM +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;
> + }
> + }
> +
> + 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);
> +}
> 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;
What is the range of this? And this value is only incremented, never
decremented?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs
2025-09-10 13:27 ` Greg Kroah-Hartman
@ 2025-09-10 19:34 ` Andrei Kuchynski
0 siblings, 0 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-10 19:34 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Wed, Sep 10, 2025 at 1:28 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Fri, Sep 05, 2025 at 02:22:06PM +0000, Andrei Kuchynski wrote:
> > 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.
> >
> > Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> > Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> > ---
> > Documentation/ABI/testing/sysfs-class-typec | 11 +++++++
> > drivers/usb/typec/class.c | 32 ++++++++++++++++++++-
> > 2 files changed, 42 insertions(+), 1 deletion(-)
> >
> > diff --git a/Documentation/ABI/testing/sysfs-class-typec b/Documentation/ABI/testing/sysfs-class-typec
> > index 38e101c17a00..dab3e4e727b6 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 alt-mode.
> > + When read, it shows the current integer priority value. Lower numerical
> > + values indicate higher priority (0 is the highest priority).
> > + 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.
>
> What is the units here? 0 is highest, what is the lowest? What is the
> range here? I can't determine this from the text, sorry.
>
Hi Greg,
You are right, the lowest range is not specified; the only note is that
the value should be an integer of 0 or higher.
The mode selection algorithm does not impose a maximum value. It
operates with any priority values returned by kstrtouint(). Any MAX
value would require an explanation as to why that specific value is
the maximum, and I do not have such an explanation. The only
restriction is that it must fit within an unsigned int variable, which
is checked by kstrtouint() against UINT_MAX.
> > +
> > 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 9f86605ce125..aaab2e1e98b4 100644
> > --- a/drivers/usb/typec/class.c
> > +++ b/drivers/usb/typec/class.c
> > @@ -19,6 +19,7 @@
> > #include "bus.h"
> > #include "class.h"
> > #include "pd.h"
> > +#include "mode_selection.h"
> >
> > static DEFINE_IDA(typec_index_ida);
> >
> > @@ -445,11 +446,34 @@ svid_show(struct device *dev, struct device_attribute *attr, char *buf)
> > }
> > static DEVICE_ATTR_RO(svid);
> >
> > +static ssize_t priority_store(struct device *dev,
> > + struct device_attribute *attr,
> > + const char *buf, size_t size)
> > +{
> > + unsigned int val;
> > + int err = kstrtouint(buf, 10, &val);
> > +
> > + if (!err) {
> > + typec_mode_set_priority(to_typec_altmode(dev), val);
> > + return size;
> > + }
> > +
> > + return err;
> > +}
> > +
> > +static ssize_t priority_show(struct device *dev,
> > + struct device_attribute *attr, char *buf)
> > +{
> > + return sprintf(buf, "%u\n", to_typec_altmode(dev)->priority);
>
> For new sysfs files, please use sysfs_emit(), otherwise someone will
> come along with a janitorial patch to fix it up after it lands in the
> tree :)
>
> thanks,
>
> greg k-h
I will change it, sysfs_emit instead of sprintf.
Thanks!
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 1/5] usb: typec: Add mode_control field to port property
2025-09-10 13:30 ` Greg Kroah-Hartman
@ 2025-09-10 19:34 ` Andrei Kuchynski
0 siblings, 0 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-10 19:34 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Wed, Sep 10, 2025 at 1:30 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Fri, Sep 05, 2025 at 02:22:02PM +0000, Andrei Kuchynski wrote:
> > This new field in the port properties dictates whether the Platform Policy
> > Manager (PPM) allows the OS Policy Manager (OPM) to change the currently
> > active, negotiated alternate mode.
> >
> > Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> > ---
> > drivers/usb/typec/class.c | 9 ++++++---
> > drivers/usb/typec/class.h | 2 ++
> > include/linux/usb/typec.h | 2 ++
> > 3 files changed, 10 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c
> > index 67a533e35150..9f86605ce125 100644
> > --- a/drivers/usb/typec/class.c
> > +++ b/drivers/usb/typec/class.c
> > @@ -457,11 +457,13 @@ static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
> > struct attribute *attr, int n)
> > {
> > struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
> > + struct typec_port *port = typec_altmode2port(adev);
> >
> > if (attr == &dev_attr_active.attr)
> > - if (!is_typec_port(adev->dev.parent) &&
> > - (!adev->ops || !adev->ops->activate))
> > - return 0444;
> > + if (!is_typec_port(adev->dev.parent)) {
> > + if (!port->mode_control || !adev->ops || !adev->ops->activate)
> > + return 0444;
> > + }
> >
> > return attr->mode;
> > }
> > @@ -2681,6 +2683,7 @@ struct typec_port *typec_register_port(struct device *parent,
> > }
> >
> > port->pd = cap->pd;
> > + port->mode_control = !cap->no_mode_control;
> >
> > ret = device_add(&port->dev);
> > if (ret) {
> > diff --git a/drivers/usb/typec/class.h b/drivers/usb/typec/class.h
> > index db2fe96c48ff..c53a04b9dc75 100644
> > --- a/drivers/usb/typec/class.h
> > +++ b/drivers/usb/typec/class.h
> > @@ -80,6 +80,8 @@ struct typec_port {
> > */
> > struct device *usb2_dev;
> > struct device *usb3_dev;
> > +
> > + bool mode_control;
>
> Shouldn't this go up higher in this structure, to avoid the hole you
> created, and to take advantage of the existing hole? Perhaps after
> orientation?
>
I will replace the variable.
Thanks,
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-10 13:31 ` Greg Kroah-Hartman
@ 2025-09-10 19:35 ` Andrei Kuchynski
2025-09-11 5:07 ` Greg Kroah-Hartman
0 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-10 19:35 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Wed, Sep 10, 2025 at 1:31 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Fri, Sep 05, 2025 at 02:22:05PM +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;
> > + }
> > + }
> > +
> > + 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);
> > +}
> > 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;
>
> What is the range of this? And this value is only incremented, never
> decremented?
>
The range extends from 0 to UINT_MAX. The value is only incremented.
The only exception is that If the user sets UINT_MAX for two alternate
modes in turn, the priority of the first mode becomes 0. This does not
break the algorithm, and the user can check all priorities via
‘priority’ attributes.
I am unsure if a check for this specific case is necessary, as it
would require examining priorities across all modes, not just a simple
'if' statement.
There are a few ideas in this algorithm:
- all priorities must always be valid and unique
- no unnecessary restrictions for the user
- as simple as possible
Thanks,
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 3/5] usb: typec: ucsi: Set no_mode_control flag
2025-09-09 9:22 ` Andrei Kuchynski
@ 2025-09-10 19:36 ` Andrei Kuchynski
0 siblings, 0 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-10 19:36 UTC (permalink / raw)
To: Heikki Krogerus
Cc: Abhishek Pandit-Subedi, Benson Leung, Jameson Thies,
Tzung-Bi Shih, linux-usb, chrome-platform, Guenter Roeck,
Greg Kroah-Hartman, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Tue, Sep 9, 2025 at 9:22 AM Andrei Kuchynski <akuchynski@chromium.org> wrote:
>
> On Tue, Sep 9, 2025 at 11:08 AM Heikki Krogerus
> <heikki.krogerus@linux.intel.com> wrote:
> >
> > On Fri, Sep 05, 2025 at 02:22:04PM +0000, Andrei Kuchynski wrote:
> > > This flag indicates that the PPM allows the OPM to change the currently
> > > negotiated alternate mode using the SET_NEW_CAM command.
> > >
> > > Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> > > ---
> > > drivers/usb/typec/ucsi/ucsi.c | 1 +
> > > 1 file changed, 1 insertion(+)
> > >
> > > diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> > > index 0d6b0cf5a7cd..9f754344a841 100644
> > > --- a/drivers/usb/typec/ucsi/ucsi.c
> > > +++ b/drivers/usb/typec/ucsi/ucsi.c
> > > @@ -1632,6 +1632,7 @@ static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
> > >
> > > cap->driver_data = con;
> > > cap->ops = &ucsi_ops;
> > > + cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
> >
> > The flag is now used to expose the priority file, but you can't affect
> > the order in which the UCSI attempts to enter the modes, or can you?
> >
> > thanks,
> >
> > --
> > heikki
>
> Hello Heikki,
> Thank you for your review!
>
> You are right, this patch does not affect the order in which UCSI
> attempts to enter modes. Currently, UCSI implementation only has
> DisplayPort alternate mode support. We are preparing to submit patches
> for Thunderbolt.
>
Hi Heikki,
should I send this patch later with the Thunderbolt UCSI support
implementation?
Thanks,
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-10 19:35 ` Andrei Kuchynski
@ 2025-09-11 5:07 ` Greg Kroah-Hartman
2025-09-11 8:46 ` Andrei Kuchynski
0 siblings, 1 reply; 22+ messages in thread
From: Greg Kroah-Hartman @ 2025-09-11 5:07 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Wed, Sep 10, 2025 at 07:35:42PM +0000, Andrei Kuchynski wrote:
> On Wed, Sep 10, 2025 at 1:31 PM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Fri, Sep 05, 2025 at 02:22:05PM +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;
> > > + }
> > > + }
> > > +
> > > + 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);
> > > +}
> > > 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;
> >
> > What is the range of this? And this value is only incremented, never
> > decremented?
> >
>
> The range extends from 0 to UINT_MAX. The value is only incremented.
> The only exception is that If the user sets UINT_MAX for two alternate
> modes in turn, the priority of the first mode becomes 0. This does not
> break the algorithm, and the user can check all priorities via
> ‘priority’ attributes.
Why not use u32 to define a sane range? Or u8? How many different
priorities will actually be used in the real world?
And what happens when it wraps?
> I am unsure if a check for this specific case is necessary, as it
> would require examining priorities across all modes, not just a simple
> 'if' statement.
> There are a few ideas in this algorithm:
> - all priorities must always be valid and unique
Is that true? Where is that validated?
> - no unnecessary restrictions for the user
What does the user actually want to do here?
> - as simple as possible
But not too simple :)
thanks,
greg k-h
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-11 5:07 ` Greg Kroah-Hartman
@ 2025-09-11 8:46 ` Andrei Kuchynski
2025-09-11 10:41 ` Greg Kroah-Hartman
0 siblings, 1 reply; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-11 8:46 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Thu, Sep 11, 2025 at 7:07 AM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Wed, Sep 10, 2025 at 07:35:42PM +0000, Andrei Kuchynski wrote:
> > On Wed, Sep 10, 2025 at 1:31 PM Greg Kroah-Hartman
> > <gregkh@linuxfoundation.org> wrote:
> > >
> > > On Fri, Sep 05, 2025 at 02:22:05PM +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;
> > > > + }
> > > > + }
> > > > +
> > > > + 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);
> > > > +}
> > > > 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;
> > >
> > > What is the range of this? And this value is only incremented, never
> > > decremented?
> > >
> >
> > The range extends from 0 to UINT_MAX. The value is only incremented.
> > The only exception is that If the user sets UINT_MAX for two alternate
> > modes in turn, the priority of the first mode becomes 0. This does not
> > break the algorithm, and the user can check all priorities via
> > ‘priority’ attributes.
>
> Why not use u32 to define a sane range? Or u8? How many different
> priorities will actually be used in the real world?
>
The priority can be u32 or u8, but not bool. I use unsigned int as the
precise bit count is not important here.
Three different priorities are enough for DisplayPort, Thunderbolt,
USB4, at least for now. The algorithm is designed to accommodate any
number of modes, as it does not rely on predefined MAX_ALTMODE or
MAX_PRIORITY values.
Priority numbers are used only with the '<' operator.
In fact, we need a list of modes to determine the order of entry.
Previously, a list was used in the 'mode selection' series, but
parsing it is more complex than using kstrtouint().
> And what happens when it wraps?
>
If a user sets all priorities (either 2 or 3) to UINT_MAX, the
resulting mode sequence will not be as expected due to overflow.
> > I am unsure if a check for this specific case is necessary, as it
> > would require examining priorities across all modes, not just a simple
> > 'if' statement.
> > There are a few ideas in this algorithm:
> > - all priorities must always be valid and unique
>
> Is that true? Where is that validated?
>
typec_mode_set_priority() ensures all priorities remain unique. Any
numerical value is valid as long as it can be compared to another,
kstrtouint() will return this value.
> > - no unnecessary restrictions for the user
>
> What does the user actually want to do here?
>
User wants to set the highest priority mode. For this, a single '0'
write operation is required. Also user can prioritize all modes by
assigning '0' to the 'priority' attribute for each mode, beginning
with the lowest prioritized mode and moving upwards.
For example, USB4 is the preferred mode, with priority 0. If a partner
(or port) device does not support USB4, the user can set the next
preferred mode (TBT or DP) based on security policy by assigning
priority 1 for only one mode. This mode is TBT when a user is logged
in, and DP otherwise.
> > - as simple as possible
>
> But not too simple :)
>
> thanks,
>
> greg k-h
Thanks,
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-11 8:46 ` Andrei Kuchynski
@ 2025-09-11 10:41 ` Greg Kroah-Hartman
2025-09-11 13:11 ` Andrei Kuchynski
0 siblings, 1 reply; 22+ messages in thread
From: Greg Kroah-Hartman @ 2025-09-11 10:41 UTC (permalink / raw)
To: Andrei Kuchynski
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Thu, Sep 11, 2025 at 10:46:52AM +0200, Andrei Kuchynski wrote:
> On Thu, Sep 11, 2025 at 7:07 AM Greg Kroah-Hartman
> <gregkh@linuxfoundation.org> wrote:
> >
> > On Wed, Sep 10, 2025 at 07:35:42PM +0000, Andrei Kuchynski wrote:
> > > On Wed, Sep 10, 2025 at 1:31 PM Greg Kroah-Hartman
> > > <gregkh@linuxfoundation.org> wrote:
> > > >
> > > > On Fri, Sep 05, 2025 at 02:22:05PM +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;
> > > > > + }
> > > > > + }
> > > > > +
> > > > > + 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);
> > > > > +}
> > > > > 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;
> > > >
> > > > What is the range of this? And this value is only incremented, never
> > > > decremented?
> > > >
> > >
> > > The range extends from 0 to UINT_MAX. The value is only incremented.
> > > The only exception is that If the user sets UINT_MAX for two alternate
> > > modes in turn, the priority of the first mode becomes 0. This does not
> > > break the algorithm, and the user can check all priorities via
> > > ‘priority’ attributes.
> >
> > Why not use u32 to define a sane range? Or u8? How many different
> > priorities will actually be used in the real world?
> >
>
> The priority can be u32 or u8, but not bool. I use unsigned int as the
> precise bit count is not important here.
But the range is, that needs to be documented.
> Three different priorities are enough for DisplayPort, Thunderbolt,
> USB4, at least for now. The algorithm is designed to accommodate any
> number of modes, as it does not rely on predefined MAX_ALTMODE or
> MAX_PRIORITY values.
Great, then let's use u8 for now.
> > And what happens when it wraps?
> >
>
> If a user sets all priorities (either 2 or 3) to UINT_MAX, the
> resulting mode sequence will not be as expected due to overflow.
That sounds like a bug :(
thanks,
greg k-h
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling
2025-09-11 10:41 ` Greg Kroah-Hartman
@ 2025-09-11 13:11 ` Andrei Kuchynski
0 siblings, 0 replies; 22+ messages in thread
From: Andrei Kuchynski @ 2025-09-11 13:11 UTC (permalink / raw)
To: Greg Kroah-Hartman
Cc: Heikki Krogerus, Abhishek Pandit-Subedi, Benson Leung,
Jameson Thies, Tzung-Bi Shih, linux-usb, chrome-platform,
Guenter Roeck, Dmitry Baryshkov, Christian A. Ehrhardt,
Venkat Jayaraman, linux-kernel
On Thu, Sep 11, 2025 at 12:41 PM Greg Kroah-Hartman
<gregkh@linuxfoundation.org> wrote:
>
> On Thu, Sep 11, 2025 at 10:46:52AM +0200, Andrei Kuchynski wrote:
> > On Thu, Sep 11, 2025 at 7:07 AM Greg Kroah-Hartman
> > <gregkh@linuxfoundation.org> wrote:
> > >
> > > On Wed, Sep 10, 2025 at 07:35:42PM +0000, Andrei Kuchynski wrote:
> > > > On Wed, Sep 10, 2025 at 1:31 PM Greg Kroah-Hartman
> > > > <gregkh@linuxfoundation.org> wrote:
> > > > >
> > > > > On Fri, Sep 05, 2025 at 02:22:05PM +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;
> > > > > > + }
> > > > > > + }
> > > > > > +
> > > > > > + 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);
> > > > > > +}
> > > > > > 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;
> > > > >
> > > > > What is the range of this? And this value is only incremented, never
> > > > > decremented?
> > > > >
> > > >
> > > > The range extends from 0 to UINT_MAX. The value is only incremented.
> > > > The only exception is that If the user sets UINT_MAX for two alternate
> > > > modes in turn, the priority of the first mode becomes 0. This does not
> > > > break the algorithm, and the user can check all priorities via
> > > > ‘priority’ attributes.
> > >
> > > Why not use u32 to define a sane range? Or u8? How many different
> > > priorities will actually be used in the real world?
> > >
> >
> > The priority can be u32 or u8, but not bool. I use unsigned int as the
> > precise bit count is not important here.
>
> But the range is, that needs to be documented.
>
The priority range will be documented in the ABI
> > Three different priorities are enough for DisplayPort, Thunderbolt,
> > USB4, at least for now. The algorithm is designed to accommodate any
> > number of modes, as it does not rely on predefined MAX_ALTMODE or
> > MAX_PRIORITY values.
>
> Great, then let's use u8 for now.
>
I will change the priority type from unsigned int to u8.
> > > And what happens when it wraps?
> > >
> >
> > If a user sets all priorities (either 2 or 3) to UINT_MAX, the
> > resulting mode sequence will not be as expected due to overflow.
>
> That sounds like a bug :(
>
I will add a check to the function and return -EOVERFLOW if the input
causes the priority values to wrap.
Greg, thank you for the review!
Andrei
^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2025-09-11 13:11 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-05 14:22 [PATCH v3 0/5] USB Type-C alternate mode priorities Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 1/5] usb: typec: Add mode_control field to port property Andrei Kuchynski
2025-09-09 8:47 ` Heikki Krogerus
2025-09-10 13:30 ` Greg Kroah-Hartman
2025-09-10 19:34 ` Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 2/5] platform/chrome: cros_ec_typec: Set no_mode_control flag Andrei Kuchynski
2025-09-08 3:57 ` Tzung-Bi Shih
2025-09-05 14:22 ` [PATCH v3 3/5] usb: typec: ucsi: " Andrei Kuchynski
2025-09-09 9:07 ` Heikki Krogerus
2025-09-09 9:22 ` Andrei Kuchynski
2025-09-10 19:36 ` Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 4/5] usb: typec: Implement alternate mode priority handling Andrei Kuchynski
2025-09-09 8:50 ` Heikki Krogerus
2025-09-10 13:31 ` Greg Kroah-Hartman
2025-09-10 19:35 ` Andrei Kuchynski
2025-09-11 5:07 ` Greg Kroah-Hartman
2025-09-11 8:46 ` Andrei Kuchynski
2025-09-11 10:41 ` Greg Kroah-Hartman
2025-09-11 13:11 ` Andrei Kuchynski
2025-09-05 14:22 ` [PATCH v3 5/5] usb: typec: Expose alternate mode priority via sysfs Andrei Kuchynski
2025-09-10 13:27 ` Greg Kroah-Hartman
2025-09-10 19:34 ` Andrei Kuchynski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox