From nobody Sat Oct 4 17:31:22 2025 Received: from mail-ed1-f54.google.com (mail-ed1-f54.google.com [209.85.208.54]) (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 5FB0129D290 for ; Thu, 14 Aug 2025 18:45:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755197110; cv=none; b=ivuXjWeppaznBxmHH+Vp3x6rD72Fq3C0SdFGeOssKdMEhrs26e57aHR6Uhv9QF2P6i9oQWlRdungMXpTkyzUbkzytDWEDi0CBxbdU7uDbnD1qJlI+cL6AuoMDpZ3AiKMreNFvWqPrx1+ksQ9QXLTW7BLznLX+DigaETKqj9QSYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755197110; c=relaxed/simple; bh=JYJuJJtKbSbwoGNn2vGBU0HeE/q6/qLRN8ZG3a0DukM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pd7MQwor3/otBz3yfuco6/R1PWC7fPHohCG4bimzD55bTNV0qitXIEbzl7KnwI52LE2j+M1q+Fkb5/MWYdZBqcUYGuHPk1YNcjiCNsoZs+MoVKspfABJVQhp2YjQySkVhJ1MWX5zJ2lVtp/HhMnL4mVxzytud9k1U6qdBE4+a4o= 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=ETowu+CQ; arc=none smtp.client-ip=209.85.208.54 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="ETowu+CQ" Received: by mail-ed1-f54.google.com with SMTP id 4fb4d7f45d1cf-6188b793d21so2018411a12.3 for ; Thu, 14 Aug 2025 11:45:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1755197107; x=1755801907; 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=qG0lIEyNnJu30xpAJGFwUH4NJAVmrpYlXCdXMGkL4NE=; b=ETowu+CQkhF9YTbVDcVyDmgazi+zp4AncQEHJT5XDpYRRFI3WTmluLo+UFRT5yIqJc DY67AG5sgiOuu0ddH9itw40kbMBFxYJZGizhYvtXMKuE9+QPU2wyYuJqQur6WbocGm5N wg89DL8op4agFGWeIypV3P6rAFPHYe0LnmB5k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755197107; x=1755801907; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qG0lIEyNnJu30xpAJGFwUH4NJAVmrpYlXCdXMGkL4NE=; b=oIgS0HD/7xlJy+lfPCPqIIbZ+bvAKUZatnxfgoeX60///UfcFvBe8aqSzNiCTYZOh6 wn9VjCnHNS4kbE9d7Q0BId+tiDDuv6xizEsHJSI1onWVZ/eRFfQYGZ5RxFtVlCV9yOwl t0joGHnNP+PoiwczV1lMiNax7KFpkdCY6WvhKK7Oc/g5QHAEBUr/9X/OkGxCoPJjvvRt 3dOa+Nigz+eoa0RFIQBLVuJGZ6643YRvXX2vkKvjmZSwGvGMo4lYex4hfyDiOZ86s3mt 9Gnm6QW8K9FBERmEF8R0Cc/0OlA1y7n3bYeEM4xMiURGXnB1ph4y+4dlpG3Wc+q8bNAR lJMA== X-Forwarded-Encrypted: i=1; AJvYcCXKXh3POqdmLkYqIu5Knz2TpTjWrl3oUafO1PJqV8pQr6pFAEvjYFcl5D5mvdXLou54j6Esaqm71sZNtyo=@vger.kernel.org X-Gm-Message-State: AOJu0YzvTrvP2goaBitQVBVfHHmbZzNUzDMcCNyhmDBNQcuU4y72ux8o xWvwEBOEzwb/IPNVJRKIE9T8mOS1XJVA2Nddmz/MBgmz69qaqF2JDUKy8DWPaHRoYQ== X-Gm-Gg: ASbGncuzOUy+bvRZRr514kHgB/Gfvwwc7Kmta4BdHGYATp7/coGaC14x+NHhSGhbjHi OHyMSNxsTBT0hi2JpBksArEcZq2bCQuKwS16Mw4CJhr+0wENQggsKpE646IGjs2E3/d1W8UAu0x zUM6LhawN0HpjIASESpLTD6q1UEUKeSw7xTpHfgCSJzNg1kxazffvfq4gbyVTq9ilF6rmTP2S+Q JF7wvh2DVMHand2u6uQhRO7J6u1/Ms9WZ6Lhzv/HB0yxIVcDTRHduIvrZREA8wpqTEOD4EFQ0zi evQPE4iIyYOPE/okdGDLNVPTetAUmKt7zspT9jYEgTB3CjTCpUhvGvPShwKXs9PabI7uxG5N8qD z3QHlYEOPO57ld3ZXwdQYkJGWeG5riuH4DkI+DhVv9Q+ZLSFTJfjZ4KsNNOJrv5p0MGxZhDffiQ vPpLzMl2ObCUwsF8Z3TDPzirGCSJhEK+VAuQ== X-Google-Smtp-Source: AGHT+IF5+9Rm5CQWAnwcUIzR8+1SMRvn/532XJx42aw5G3W3xEpn8yRTB8aPidZXpklreIMFUWH/CA== X-Received: by 2002:a17:907:6d12:b0:af9:3ad2:6930 with SMTP id a640c23a62f3a-afcb97cb1f3mr385072666b.24.1755197106660; Thu, 14 Aug 2025 11:45:06 -0700 (PDT) Received: from akuchynski.c.googlers.com.com (37.247.91.34.bc.googleusercontent.com. [34.91.247.37]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-af91a21c0f4sm2614772466b.106.2025.08.14.11.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Aug 2025 11:45:06 -0700 (PDT) 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" , Venkat Jayaraman , linux-kernel@vger.kernel.org, Andrei Kuchynski Subject: [PATCH v1 5/5] usb: typec: Expose alternate mode priority via sysfs Date: Thu, 14 Aug 2025 18:44:55 +0000 Message-ID: <20250814184455.723170-6-akuchynski@chromium.org> X-Mailer: git-send-email 2.51.0.rc1.163.g2494970778-goog In-Reply-To: <20250814184455.723170-1-akuchynski@chromium.org> References: <20250814184455.723170-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. Signed-off-by: Andrei Kuchynski --- Documentation/ABI/testing/sysfs-class-typec | 12 ++++++ drivers/usb/typec/class.c | 47 ++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Documentation/ABI/testing/sysfs-class-typec b/Documentation/AB= I/testing/sysfs-class-typec index 38e101c17a00..001202d651fa 100644 --- a/Documentation/ABI/testing/sysfs-class-typec +++ b/Documentation/ABI/testing/sysfs-class-typec @@ -162,6 +162,18 @@ 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 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. + This attribute is visible only if the kernel supports mode selection. + 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 a72325ff099a..708f3487222a 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" =20 static DEFINE_IDA(typec_index_ida); =20 @@ -445,11 +446,41 @@ svid_show(struct device *dev, struct device_attribute= *attr, char *buf) } static DEVICE_ATTR_RO(svid); =20 +static ssize_t priority_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t size) +{ + unsigned int val; + int err =3D kstrtouint(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) +{ + int val; + const int err =3D typec_mode_get_priority(to_typec_altmode(dev), &val); + + if (err) + return err; + + return sprintf(buf, "%d\n", val); +} +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 @@ -458,7 +489,7 @@ static umode_t typec_altmode_attr_is_visible(struct kob= ject *kobj, { struct typec_altmode *adev =3D to_typec_altmode(kobj_to_dev(kobj)); =20 - if (attr =3D=3D &dev_attr_active.attr) + if (attr =3D=3D &dev_attr_active.attr) { if (!is_typec_port(adev->dev.parent)) { struct typec_partner *partner =3D to_typec_partner(adev->dev.parent); @@ -469,6 +500,15 @@ static umode_t typec_altmode_attr_is_visible(struct ko= bject *kobj, !adev->ops->activate) return 0444; } + } else if (attr =3D=3D &dev_attr_priority.attr) { + if (is_typec_port(adev->dev.parent)) { + struct typec_port *port =3D to_typec_port(adev->dev.parent); + + if (!port->alt_mode_override) + return 0; + } else + return 0; + } =20 return attr->mode; } @@ -2029,6 +2069,7 @@ static void typec_release(struct device *dev) typec_mux_put(port->mux); typec_retimer_put(port->retimer); kfree(port->cap); + typec_mode_selection_destroy(port); kfree(port); } =20 @@ -2496,6 +2537,8 @@ typec_port_register_altmode(struct typec_port *port, to_altmode(adev)->retimer =3D retimer; } =20 + typec_mode_set_priority(adev, -1); + return adev; } EXPORT_SYMBOL_GPL(typec_port_register_altmode); @@ -2645,6 +2688,8 @@ struct typec_port *typec_register_port(struct device = *parent, port->con.attach =3D typec_partner_attach; port->con.deattach =3D typec_partner_deattach; =20 + INIT_LIST_HEAD(&port->mode_list); + if (cap->usb_capability & USB_CAPABILITY_USB4) port->usb_mode =3D USB_MODE_USB4; else if (cap->usb_capability & USB_CAPABILITY_USB3) --=20 2.51.0.rc0.215.g125493bb4a-goog