[PATCH i-g-t v7 04/48] lib/igt_kms: Add helper to obtain a connector by its name or MST path

Louis Chauvet
2026-02-23T12:08:52Z

Introduce the functions igt_get_connector_from_name() and
igt_get_connector_id_from_mst_path(). These functions serve to retrieve
the connector object using a connector name and a connector ID from its
MST path, respectively.

Given that the connector id may not be consistent, especially with MST
connectors, these functions are essential to recognize each connector even
after system reboots and plug/unplug events.

The function igt_get_connector_id_from_name() is a convenient wrapper for
igt_get_connector_from_name() to streamline its usage when the caller only
requires the connector id.

Signed-off-by: Louis Chauvet <louis.chauvet@bootlin.com>
Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com>
Reviewed-by: Kory Maincent <kory.maincent@bootlin.com>
---
lib/igt_kms.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_kms.h | 3 ++
2 files changed, 111 insertions(+)

diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index 1f9f30f7d8b1..8876ee8a11f7 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -95,6 +95,10 @@
#define MAX_EDID 2
#define DISPLAY_TILE_BLOCK 0x12
#define MAX_NUM_COLOROPS 256
+/**
+ * IGT_KMS_CONNECTOR_NAME_SIZE - Size used when a connector name is needed
+ */
+#define IGT_KMS_CONNECTOR_NAME_SIZE 50

typedef bool (*igt_connector_attr_set)(int dir, const char *attr, const char *value);

@@ -8062,3 +8066,107 @@ int igt_get_connected_connectors(int drm_fd, uint32_t **connector_ids)

return connected_count;
}
+
+/**
+ * igt_get_connector_from_name:
+ * @drm_fd: DRM file descriptor
+ * @port_name: Port name to search
+ *
+ * Returns: The connector if found, NULL otherwise. The pointer must
+ * be freed with drmModeFreeConnector()
+ */
+drmModeConnectorPtr igt_get_connector_from_name(int drm_fd, const char *port_name)
+{
+ double timeout = igt_default_display_detect_timeout();
+ drmModeResPtr res = drmModeGetResources(drm_fd);
+ int i;
+
+ if (!res)
+ return NULL;
+
+ for (i = 0; i < res->count_connectors; i++) {
+ char name[IGT_KMS_CONNECTOR_NAME_SIZE];
+ int len;
+
+ drmModeConnectorPtr connector = igt_wait_for_connector(drm_fd, res->connectors[i],
+ timeout);
+
+ if (connector) {
+ /* We have to generate the connector name on our own */
+ len = snprintf(name, sizeof(name), "%s-%u",
+ kmstest_connector_type_str(connector->connector_type),
+ connector->connector_type_id);
+ name[len] = 0;
+
+ if (strcmp(port_name, name) == 0) {
+ drmModeFreeResources(res);
+
+ return connector;
+ }
+
+ drmModeFreeConnector(connector);
+ }
+ }
+
+ drmModeFreeResources(res);
+
+ return NULL;
+}
+
+/**
+ * igt_get_connector_id_from_name:
+ * @drm_fd: DRM file descriptor
+ * @port_name: Port name to find in the connector
+ *
+ * Returns: The connector id if the port is found, 0 if the port is not found
+ */
+uint32_t igt_get_connector_id_from_name(int drm_fd, const char *port_name)
+{
+ drmModeConnectorPtr connector = igt_get_connector_from_name(drm_fd, port_name);
+
+ if (connector) {
+ uint32_t connector_id = connector->connector_id;
+
+ drmModeFreeConnector(connector);
+
+ return connector_id;
+ }
+
+ return 0;
+}
+
+/**
+ * igt_get_connector_id_from_mst_path:
+ * @drm_fd: DRM file descriptor
+ * @mst_path: MST path to find in the connector
+ *
+ * Returns: 0 when no connector is found with the correct mst path
+ */
+uint32_t igt_get_connector_id_from_mst_path(int drm_fd, const void *mst_path)
+{
+ drmModeResPtr res = drmModeGetResources(drm_fd);
+ int i;
+
+ if (!res)
+ return 0;
+
+ for (i = 0; i < res->count_connectors; i++) {
+ uint32_t connector_id = res->connectors[i];
+
+ drmModePropertyBlobPtr path_blob = kmstest_get_path_blob(drm_fd, connector_id);
+
+ if (path_blob) {
+ if (memcmp(path_blob->data, mst_path, path_blob->length) == 0) {
+ drmModeFreePropertyBlob(path_blob);
+ drmModeFreeResources(res);
+ return connector_id;
+ }
+
+ drmModeFreePropertyBlob(path_blob);
+ }
+ }
+
+ drmModeFreeResources(res);
+
+ return 0;
+}
diff --git a/lib/igt_kms.h b/lib/igt_kms.h
index 86e0235728c4..52b62252683b 100644
--- a/lib/igt_kms.h
+++ b/lib/igt_kms.h
@@ -1322,5 +1322,8 @@ igt_colorop_t *igt_find_colorop(igt_display_t *display, uint32_t id);
bool igt_wait_for_connector_status(int drm_fd, unsigned int connector_id, double timeout,
int drm_mode);
int igt_get_connected_connectors(int drm_fd, uint32_t **connector_ids);
+drmModeConnectorPtr igt_get_connector_from_name(int drm_fd, const char *port_name);
+uint32_t igt_get_connector_id_from_name(int drm_fd, const char *port_name);
+uint32_t igt_get_connector_id_from_mst_path(int drm_fd, const void *mst_path);

#endif /* __IGT_KMS_H__ */

--
2.52.0