From nobody Tue Feb 10 01:59:38 2026 Received: from mail-ej1-f44.google.com (mail-ej1-f44.google.com [209.85.218.44]) (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 45CA338FF0B for ; Tue, 13 Jan 2026 13:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768309557; cv=none; b=BKz0XXEOY9z8UWLlhFn8WBeYVB5xtDetWN985UZHuNljelipuLUpbpRlTbSJRR2OGS6RqyOTW0jAaT/I7UxkRNoPyTxMsN7MhJZ+EEEbvQzz+W1QTvjV9Q1/tUvAPDU/15tBl1Qr5fBJ2Ky9x1FrPLqnhPmhjbUxamz+iswuwjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768309557; c=relaxed/simple; bh=bVUhc7VpDUnDxfDtkHva6EQ2YfFZC4bpqm5zztp0T64=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VKD+aeJR0oVqBFPLYOPXFmhzfZRdHeBf5Uq+ejuY5YL8ULnLxwc7gJNwStMxBvHUAweiaDpjqmHQM2wSR5K+RBS2WKcUdkviJqtgUKwF4PUyJA7kz8z1tYdHDVDOQAth8LE7ngcLP85IqNDkFk663WyVpgJdwNj/GFIhn1XaS6o= 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=TN34hk0P; arc=none smtp.client-ip=209.85.218.44 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="TN34hk0P" Received: by mail-ej1-f44.google.com with SMTP id a640c23a62f3a-b8718187eb6so312178666b.2 for ; Tue, 13 Jan 2026 05:05:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1768309552; x=1768914352; 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=afo50+aNolz/eyc+FXJYc+MI5yPdVLIkEoTPV8JfhIk=; b=TN34hk0PfI2JYr960/J31pLy3pTkNcdlemMfMfu+4REc+BioKJf3Vv0xhY8hF8Od07 H5CXZQVKFeCrHmBQGNjsPTuos5zWryYjlK0vDTcHQP5yVq2+I9TwY9VdnuGeMaOFZ2b/ 304XuDOcTfWmILX5QzZk6BFDCRwo+0Sh7uF8M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768309552; x=1768914352; 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=afo50+aNolz/eyc+FXJYc+MI5yPdVLIkEoTPV8JfhIk=; b=fQihT1demEp2ULkWUU22zMhYMtR+RD35pl0KEcPm5npTCNIVCfTLOmVGo3or78X2A+ zn54j36OjQQMvsc4RpnEGdOTmoUUnw0zWLAnUpdMlYx2o+W06LttfTLVfYLvAjks0ldH uJzELgue1rjS7mMkVm9EIQ7QWa8Zjdi1Wlvntd81dVm2NAxUtNfR0togI/0iuBhi5rln jnUvG/D9VxGoXLoVYC+u19xt7RXM+6kj3jaze+1eHW7i3WXZwwpPDl+ybUp5r1N1kF8D yBgIWs+/H5DDQCYIZYpPbSffUULlLN7OSodBralfKd/pjiOihk8Eb8VJL9wRq3Hjsod9 L/ww== X-Forwarded-Encrypted: i=1; AJvYcCUcGDSV1lOE+EBosElLg5XqVTIhmTaEUCEPztYTHKzyVzVT/Xg76FtgQ9IU57N1hXJJXSWqSblSue8+rMg=@vger.kernel.org X-Gm-Message-State: AOJu0Yy9Qj3eakoaPZwgtf9uk6AAlYiPhbuAYl2GDnbfh4CVtJI/anfp KreWN3Oe2R48jnnz679YkFV15tBI5HbOeaM23RywZr468+CCSkG6LvBlZEBs5M76yQ== X-Gm-Gg: AY/fxX7kMi5G/FLFt1xiDENZHOsVoFa/9d/uj7jRJvxT/QcwGca/RuLCVt8FO4xCz3D M2Hxb26HCsq0ILVGMN+t/yUoaNudFC35mnHiQx+Yb4PVjCWwIJHFy77/g6dfI2YhZYVQYD9sHMj ozArfcoczon5AiDPzzfeCPunskCydv52ByBbSty3pR3t7wpFLZadqmfxMcn8VEGTvyqc5PLuf1r aL/TFdx/TOaNV20IxsI94C+0jUaiSVj74qHh/3E7Vr4SxvNHmFUDGjj3s9Fl1JUeuGn1VltFigC eILFxs4wRsXbyc3++eZzJc63JlxCeOM7N0ui4aq6tCONeSuEhDf0X69IM9VqLlD/loTUDzXR2au 2pGmIoRK23Q2q2NRhChhxlwRESSAnvC0qr8fxOYKpwqxUteoElFhWFI9cuKEsASOqNHx+F7MGnI xyIX4ZlRvNqsl1ExwV8f4Bl42YWkdS4IbfDFXVhPX5VBgdjZtRXnasn1Ni8otvCRvD/Utr5HQk8 7/u5j/n X-Google-Smtp-Source: AGHT+IGIDWcZoX/igewBnXumY61TFpoC2nj6Rw8tc/gyULQtX23Kl7iOBicSZMG61hMYbEOewkNWZw== X-Received: by 2002:a17:907:6e92:b0:b87:1a26:3672 with SMTP id a640c23a62f3a-b871a265b10mr652150866b.57.1768309551621; Tue, 13 Jan 2026 05:05:51 -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-b871b5e60dasm586217266b.63.2026.01.13.05.05.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jan 2026 05:05:51 -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?= , Abel Vesa , Pooja Katiyar , Johan Hovold , Hsin-Te Yuan , Madhu M , Venkat Jayaraman , Andrei Kuchynski Subject: [PATCH v4 4/8] usb: typec: Expose alternate mode priority via sysfs Date: Tue, 13 Jan 2026 13:05:32 +0000 Message-ID: <20260113130536.3068311-5-akuchynski@chromium.org> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog In-Reply-To: <20260113130536.3068311-1-akuchynski@chromium.org> References: <20260113130536.3068311-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 --- 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..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); =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