From nobody Tue Dec 2 00:46:27 2025 Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 D47D6303A04 for ; Mon, 24 Nov 2025 12:46:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763988416; cv=none; b=VLiKttydBJolFs0xGSabBDD/3ivZECaP01jY9KyGGQVru8NQtdj5ejeksDUpDXSuImx+tgIOeViAbmoTTYyQXDxqGbzYaT/VBfX+S7V32wtA5i20mwcR66bmHS+qFtQQ5dwNHcmcTM1CIfbaV8NotY7xpggPIiXyE6kvB4Nvrks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763988416; c=relaxed/simple; bh=ZcWRPSvnJY7j9sksuKnADdwDhkWT5l383JVkKi48Y4A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gW12jrf1Ns+u6nUe3s+DmSRn7sjGdg4gqmaC70vPwsSFVZB3XqUh5gmXoV0kB9YYlap/nmyF+lklQ3uuGKT4eRmnUugjnJA2YMeLk3lzMYp7FwrPs/wbcB7d6G73xq/cZOFzLQb+qPI6M4cTZm433RPZKySXNsNSkmZOtDswmXE= 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=JVkuaqrt; arc=none smtp.client-ip=209.85.208.43 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="JVkuaqrt" Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-640ca678745so7075095a12.2 for ; Mon, 24 Nov 2025 04:46:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1763988413; x=1764593213; 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=+tksLNCM9hMN6EA+LRxmgTQx4fghasKcuZYm2c3+N8w=; b=JVkuaqrtODvaUwGJgAO0HPOUNap9RmXpJluCO6w+k4HMPc7gWOJb7G722EJNrTw2Ip 4nyxGF90iNj8KJb4drruzTwJwruiCm+OJLe5iLA3m+GfKw4WzkFtX5q7s1LbUaak1p0v 3y+Aoo5IAWVAGfpXBr+BtIPkrzjYBF91njHqk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763988413; x=1764593213; 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=+tksLNCM9hMN6EA+LRxmgTQx4fghasKcuZYm2c3+N8w=; b=ModjCvA5tapoMYHcPWLYteRCFR/81Mh+MJ7AV76MW2iAfcfVGib4vR/d//7y+rRklH SzQKUg/bKcIlYxqceYrUz9ylJR8BsjeNqx98C5oLfQHCsDQzCX+MHe1ht8Rn4VfA/SH9 NtY7/1HVJVuIEcAX4D4GLlED8pNtmaCCCWV463QngpWy74QFjayi5R58TCdSaAInW3tL pHKxTm0nMT0sSGwXYCzQy6eXbeWWUsxWVs1fDsLapJLUvkDV2iuDut0ghI7PWfRoKque 39q/XlsKWXi0+l0CBUnJWB1MBWn9fadD9zZdsbVQaDH15RAcnIfVnsJlYHG3aEQlb3hU 6QUQ== X-Forwarded-Encrypted: i=1; AJvYcCVlHvkr4i3zU/7NhAU/AXyjf6jXRn8drkDUb3lP/N0+/6skI4KbnCer2mr3xapQIjxRMEqV1gFauatYemE=@vger.kernel.org X-Gm-Message-State: AOJu0YzGCn0d3QE/5Q0rUf7ZQc1Z+MZcnaZpuIyybvbe3C25WrRP/8el +B2Rk1ibNBcv45EBAK1Ea/Lcj8y1lo66LdSkApMCS8DhQ3emM9RUZOSBi87AIsmMww== X-Gm-Gg: ASbGncsM5FV5Ocmq/MN4Bx2AL23In2XHbsrdU4ufIGALpZs86SPv95aAzPqWJDR2T2u SsOn99OTsAoDoKL3OcovQBOtX1CmY5OAat0wUEb2NYrIJ/z8P0eqy5HiZfT0sxGWW93lMyVcmn1 WqfLoZP3jurrkpKZhYlsDxNCblnW0Y4tL4evpwC+deImMe00wGHjPRuTUS58tNMmlCu8zQqQv/h 2n90T26sFHGrX7FfitilzYBVvkwtJxHesccZ60aMLsOjTZkU+DkEIVsnrRSGfJSzUxqrkyYCRdm jTSy+NfNKutd7Hyy84XGkazuCutecItdeKQwfAHo6KhCAm6aws7PrqawzHjYEycPKb3g/mg93Sz GHHKURMHVbDauYrjvW09xHm878HYiNELUILDqKUhthfuCXMS6rJH3c4z1SH5YytfCXXt4qBnlUG xaVD+vFkJkr/rstAGyAwoxYrNgxf9AgMCOWPpVrIIL5xxXHJOjJfIzPol92AitJZY82njAh8WYO rsy5/uv/0c= X-Google-Smtp-Source: AGHT+IEfcpbzsOCnT/x6NbrGmqhHYN1EwoeFU1i5kVHhF/kjyV0FZnqLTamKpVXxQGhmoo7XnU917g== X-Received: by 2002:a05:6402:13ce:b0:640:947e:70ce with SMTP id 4fb4d7f45d1cf-64555b85acamr10495350a12.5.1763988413169; Mon, 24 Nov 2025 04:46:53 -0800 (PST) Received: from akuchynski.c.googlers.com.com (224.138.204.35.bc.googleusercontent.com. [35.204.138.224]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64536460ea9sm13342599a12.35.2025.11.24.04.46.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Nov 2025 04:46:52 -0800 (PST) From: Andrei Kuchynski To: Heikki Krogerus , Abhishek Pandit-Subedi , Benson Leung , Jameson Thies , Tzung-Bi Shih , linux-usb@vger.kernel.org, chrome-platform@lists.linux.dev Cc: Guenter Roeck , Greg Kroah-Hartman , Dmitry Baryshkov , "Christian A. Ehrhardt" , Abel Vesa , Pooja Katiyar , Venkat Jayaraman , linux-kernel@vger.kernel.org, Andrei Kuchynski Subject: [PATCH v5 4/4] usb: typec: Expose alternate mode priority via sysfs Date: Mon, 24 Nov 2025 12:46:39 +0000 Message-ID: <20251124124639.1101335-5-akuchynski@chromium.org> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog In-Reply-To: <20251124124639.1101335-1-akuchynski@chromium.org> References: <20251124124639.1101335-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 38e101c17a00..737b76828b50 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 a5327e444265..049d1829be98 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_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_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; } @@ -2484,6 +2565,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)) @@ -2502,6 +2584,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 f7db3bd4c90e..2c3b6bec2eca 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; + u8 priority; =20 char *desc; const struct typec_altmode_ops *ops; --=20 2.52.0.rc2.455.g230fcf2819-goog