From nobody Mon Feb 9 19:25:54 2026 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A657435CB98 for ; Mon, 19 Jan 2026 13:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768828736; cv=none; b=qM8kkjKTdVgMCsTgEciG+OL/Akvuo6PThDOhn3/RftKJ+A5MZaJNwQueMmle6MVTuzcDSdt6uwLkELUIW/qF29qpkfZ5B7/JsaP4V4VWhtf8/fYVPxVQ/ygTTivcff1pGzaX6levbjqcu0l3yjfbOb3XzXxB+NW9g1LOyIvxv5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768828736; c=relaxed/simple; bh=DfrSPWYURzwp3WwkWbtpXKx1iOauYrzcxYjkJqvZmck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VpUuJ/JNKrJ/H83VV/4US97gB9uXdoT8p0erWxAN6OJiJAEyZ8XHl9r1MxZ7ms88TYEbdj13hLEZgsaCKfB7xlzoQcQ9FKuY42mWi1TBx2IG86t/IrwrITXXG0OMcgINdgsZnNnzp6d8baZCnvS0fgBaM0qITemBg2y7pCSehiE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=GHNGjgj8; arc=none smtp.client-ip=209.85.218.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="GHNGjgj8" Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-b86ed375d37so562685166b.3 for ; Mon, 19 Jan 2026 05:18:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1768828727; x=1769433527; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JVZyoDBgCg278jS1vSrSjkeNL7wqN0touFgdWqJTr8k=; b=GHNGjgj8w6I55uTlodXaKq9PAWxY95pp/P/hMEDToyZqTogEKzKxywzPsQbInR/L8M S7uPHghylTaNUGPt3ZB1nrxqoXFsKO/Y/fGNEnyXnCqZwtzsySG0RYaoDLLwDV1JEUpn btVhI+HJoJkFhKXpWaFQu+sDpa+edXDGWU9Hc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768828727; x=1769433527; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JVZyoDBgCg278jS1vSrSjkeNL7wqN0touFgdWqJTr8k=; b=UHZmhPO+q9KlLv82/fBh55ooIUDTy8KSS6U9uHGtu4nq6jK/dDPv0ORkNyxFQaMxM8 wEKGZi2kblILq8/PUfyFUbqIdb8HRTK3OZZsx6zkuHfP1WdKe6L0wzdi05bgDVhcdfdS 47mm1E2popfGjj2KHUW4Nn7HLCjNBKWDuBw9wiSeZFFVwzRW4N9oUM5WDbVn+h68+AZ8 IxhAwDr5OmR1X4OdtJwBSH6FcBanb0vHZY9r04ZUTrjcsMpnh366JCLAY8ZP5NdYoHMD A9n51BY1vRcMPzA8gHGvk4pUn5BVt968Q/mxsjBopy6teaK2Za7C9qLQrONBoy0BFA1y VmrQ== X-Forwarded-Encrypted: i=1; AJvYcCU1RZGRk73DkTgyuIIOVRQErB8xreN04tcA95Wxw79Yvw7SBoujHx/11TldO89T8iHwmD3fmhuH7NcOfCU=@vger.kernel.org X-Gm-Message-State: AOJu0YxVTwaNoOSNuRNcZaaASuJE6zw8KS+SZghxwzvuKwUJVqz0tdny 3dhvsVlHzCRr63pXwjOUnIdXONlNBs9HHAXDIcIp1nXUXVPFAf9legWlv4dVaysVEA== X-Gm-Gg: AY/fxX5x9A7bR38Lvuu/e3LFLp3mZupXmtt3asxjF3neUNS2MKxdtopXfdWQ6nCE6sq bMqLh1HN/74Ix1lGU+wR1BQ2vR8yo0gQ0W1Anp0HYMR2o4smYFA8LcIoToaKCKAtGotOQ5STod0 R6JJhdCu7//ehND3nfPf1ZN0i+FR+FQ9k0qAukj774q/96Wfyl4TmCFXYmdLtsmQEBKs+IRVheO hQxvWSfvgT4r3k2reZxWuCr6fRiC5JyJw4tzoGN9xG7mwutsYJcpcF5wdTFETtVhItRUKOw7TW1 WOKDhLV168yuq5CrzRnX4z47qTD87NZ6CEGwYSIZ6T70FSI8qAeIf0KA+tPHxEmlksW8afo9dYN qau8zkc5JKzboQOIjwUzgtkKuYorKXRz1u4G3XfhobLjW3fgfb4I4YeIV74SS8Ge8AWJSWhsxlG Nf9tSaNCdqOVJcgEaDDhourtlv8AVuEmaU+qTagDAXfoWEVGIaSkYKU1BGUW9j2WZhjJWP0IEIW +tAgadltvYzrZCtPZg= X-Received: by 2002:a17:906:7305:b0:b86:fd46:724 with SMTP id a640c23a62f3a-b8792d489b0mr1082509366b.13.1768828726748; Mon, 19 Jan 2026 05:18:46 -0800 (PST) Received: from akuchynski.c.googlers.com.com (150.230.32.34.bc.googleusercontent.com. [34.32.230.150]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b87959c9a14sm1078801866b.45.2026.01.19.05.18.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jan 2026 05:18:46 -0800 (PST) From: Andrei Kuchynski To: Heikki Krogerus , Abhishek Pandit-Subedi , Benson Leung , Jameson Thies , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, chrome-platform@lists.linux.dev Cc: Tzung-Bi Shih , Guenter Roeck , Greg Kroah-Hartman , Dmitry Baryshkov , =?UTF-8?q?=C5=81ukasz=20Bartosik?= , Pooja Katiyar , Johan Hovold , Hsin-Te Yuan , Madhu M , Venkat Jayaraman , Andrei Kuchynski Subject: [PATCH v5 3/7] usb: typec: Expose alternate mode priority via sysfs Date: Mon, 19 Jan 2026 13:18:20 +0000 Message-ID: <20260119131824.2529334-4-akuchynski@chromium.org> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog In-Reply-To: <20260119131824.2529334-1-akuchynski@chromium.org> References: <20260119131824.2529334-1-akuchynski@chromium.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Benson Leung Reviewed-by: Heikki Krogerus --- Changes in V5: - Clean up code style and alignment issues 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/AB= I/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 defaul= t USB mode that is used - usb3 (USB 3.2) - usb4 (USB4) =20 +What: /sys/class/typec///priority +Date: July 2025 +Contact: Andrei Kuchynski +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. + USB Type-C partner devices (eg. /sys/class/typec/port0-partner/) =20 What: /sys/class/typec/-partner/accessory_mode diff --git a/drivers/usb/typec/class.c b/drivers/usb/typec/class.c index 0f12d6120511b..a48c447125184 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); =20 +static int increment_duplicated_priority(struct device *dev, void *data) +{ + if (is_typec_port_altmode(dev)) { + struct typec_altmode **alt_target =3D (struct typec_altmode **)data; + struct typec_altmode *alt =3D to_typec_altmode(dev); + + if (alt !=3D *alt_target && alt->priority =3D=3D (*alt_target)->priority= ) { + alt->priority++; + *alt_target =3D 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 =3D (struct typec_altmode **)data; + struct typec_altmode *alt =3D to_typec_altmode(dev); + + if (alt !=3D *alt_target && alt->priority =3D=3D (*alt_target)->priority) + return 1; + } + return 0; +} + +static int typec_mode_set_priority(struct typec_altmode *alt, const u8 pri= ority) +{ + struct typec_port *port =3D to_typec_port(alt->dev.parent); + const u8 old_priority =3D alt->priority; + int res =3D 1; + + alt->priority =3D priority; + while (res) { + res =3D device_for_each_child(&port->dev, &alt, find_duplicated_priority= ); + if (res) { + alt->priority++; + if (alt->priority =3D=3D 0) { + alt->priority =3D old_priority; + return -EOVERFLOW; + } + } + } + + res =3D 1; + alt->priority =3D priority; + while (res) + res =3D device_for_each_child(&port->dev, &alt, + increment_duplicated_priority); + + return 0; +} + +static ssize_t priority_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + u8 val; + int err =3D kstrtou8(buf, 10, &val); + + if (!err) + err =3D typec_mode_set_priority(to_typec_altmode(dev), val); + + if (!err) + return size; + return err; +} + +static ssize_t priority_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", to_typec_altmode(dev)->priority); +} +static DEVICE_ATTR_RW(priority); + static struct attribute *typec_altmode_attrs[] =3D { &dev_attr_active.attr, &dev_attr_mode.attr, &dev_attr_svid.attr, &dev_attr_vdo.attr, + &dev_attr_priority.attr, NULL }; =20 @@ -459,11 +536,15 @@ static umode_t typec_altmode_attr_is_visible(struct k= object *kobj, struct typec_altmode *adev =3D to_typec_altmode(kobj_to_dev(kobj)); struct typec_port *port =3D typec_altmode2port(adev); =20 - if (attr =3D=3D &dev_attr_active.attr) + if (attr =3D=3D &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 =3D=3D &dev_attr_priority.attr) { + if (!is_typec_port(adev->dev.parent) || !port->mode_control) + return 0; + } =20 return attr->mode; } @@ -2498,6 +2579,7 @@ typec_port_register_altmode(struct typec_port *port, struct typec_altmode *adev; struct typec_mux *mux; struct typec_retimer *retimer; + int ret; =20 mux =3D typec_mux_get(&port->dev); if (IS_ERR(mux)) @@ -2516,6 +2598,12 @@ typec_port_register_altmode(struct typec_port *port, } else { to_altmode(adev)->mux =3D mux; to_altmode(adev)->retimer =3D retimer; + + ret =3D typec_mode_set_priority(adev, 0); + if (ret) { + typec_unregister_altmode(adev); + return ERR_PTR(ret); + } } =20 return adev; diff --git a/include/linux/usb/typec_altmode.h b/include/linux/usb/typec_al= tmode.h index 9197a4637a938..7e6c02d74b54f 100644 --- a/include/linux/usb/typec_altmode.h +++ b/include/linux/usb/typec_altmode.h @@ -36,6 +36,7 @@ struct typec_altmode { int mode; u32 vdo; unsigned int active:1; + u8 priority; =20 char *desc; const struct typec_altmode_ops *ops; --=20 2.52.0.457.g6b5491de43-goog