[PATCH i-g-t 1/6] lib/igt_edid: add EDID serial extraction helpers

Kunal Joshi
2026-04-09T04:16:20Z

Add helpers to extract serial identification from EDID data.

- edid_get_serial_string(): extract ASCII serial from descriptor 0xFF
- edid_get_serial_number(): extract 32-bit serial from EDID header
- edid_get_any_serial(): convenience helper that tries ASCII serial
first, then falls back to 32-bit header serial formatted as hex

These are needed by the connector helper and USB4 switch test suite
for EDID-based display verification after dock/undock events.

Signed-off-by: Kunal Joshi <kunal1.joshi@intel.com>
Reviewed-by: Arun R Murthy <arun.r.murthy@gmail.com>
---
lib/igt_edid.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/igt_edid.h | 5 +++
2 files changed, 96 insertions(+)

diff --git a/lib/igt_edid.c b/lib/igt_edid.c
index c68ccf671..e7fcbf0b9 100644
--- a/lib/igt_edid.c
+++ b/lib/igt_edid.c
@@ -249,6 +249,97 @@ void edid_get_monitor_name(const struct edid *edid, char *name, size_t name_size
name[0] = '\0';
}

+/**
+ * edid_get_serial_string:
+ * @edid: EDID structure
+ * @serial: buffer to write serial string
+ * @serial_size: size of serial buffer; must be > 1
+ *
+ * Extract the serial string from EDID descriptor block type 0xFF.
+ * The descriptor payload is a 13-byte fixed-width field; trailing
+ * whitespace is stripped and the result is NUL-terminated.
+ * @serial is left empty when no 0xFF descriptor is found.
+ */
+void edid_get_serial_string(const struct edid *edid,
+ char *serial, size_t serial_size)
+{
+ size_t copy_len, i, n;
+
+ igt_assert(edid);
+ igt_assert(serial);
+ igt_assert(serial_size > 1);
+ serial[0] = '\0';
+
+ for (i = 0; i < DETAILED_TIMINGS_LEN; i++) {
+ const uint8_t *d = (const uint8_t *)&edid->detailed_timings[i];
+
+ if (d[0] != 0x00 || d[1] != 0x00 || d[2] != 0x00)
+ continue;
+
+ if (d[3] != EDID_DETAIL_MONITOR_SERIAL || d[4] != 0x00)
+ continue;
+
+ /* Descriptor string payload is 13 bytes starting at d[5] */
+ copy_len = serial_size - 1 < 13 ? serial_size - 1 : 13;
+ memcpy(serial, &d[5], copy_len);
+ serial[copy_len] = '\0';
+
+ for (n = strlen(serial);
+ n > 0 && isspace((unsigned char)serial[n - 1]); n--)
+ serial[n - 1] = '\0';
+
+ return;
+ }
+}
+
+/**
+ * edid_get_serial_number:
+ * @edid: EDID structure
+ *
+ * Extract the 32-bit serial number from the EDID base block header
+ * (bytes 12–15, the manufacturer/product section). This is different
+ * from the ASCII serial string in descriptor block 0xFF.
+ *
+ * Returns: 32-bit serial number (little-endian), 0 if absent.
+ */
+uint32_t edid_get_serial_number(const struct edid *edid)
+{
+ return (uint32_t)edid->serial[0] |
+ ((uint32_t)edid->serial[1] << 8) |
+ ((uint32_t)edid->serial[2] << 16) |
+ ((uint32_t)edid->serial[3] << 24);
+}
+
+/**
+ * edid_get_any_serial:
+ * @edid: EDID structure
+ * @serial: buffer to write serial string
+ * @serial_size: size of serial buffer; must be > 1
+ *
+ * Convenience helper that tries to extract a serial identifier from EDID.
+ * First tries the ASCII serial string descriptor (0xFF). If that is empty,
+ * falls back to the 32-bit header serial number formatted as 8-char hex.
+ * If neither source provides a usable serial (no 0xFF descriptor and the
+ * header serial is 0), @serial is left empty.
+ */
+void edid_get_any_serial(const struct edid *edid,
+ char *serial, size_t serial_size)
+{
+ uint32_t serial_num;
+
+ igt_assert(edid);
+ igt_assert(serial);
+ igt_assert(serial_size > 1);
+
+ edid_get_serial_string(edid, serial, serial_size);
+ if (serial[0])
+ return;
+
+ serial_num = edid_get_serial_number(edid);
+ if (serial_num != 0)
+ snprintf(serial, serial_size, "%08X", serial_num);
+}
+
static void edid_set_mfg(struct edid *edid, const char mfg[static 3])
{
edid->mfg_id[0] = (mfg[0] - '@') << 2 | (mfg[1] - '@') >> 3;
diff --git a/lib/igt_edid.h b/lib/igt_edid.h
index be0ccf529..d9e396065 100644
--- a/lib/igt_edid.h
+++ b/lib/igt_edid.h
@@ -457,5 +457,10 @@ void *dispid_block_tiled(void *ptr,
int hsize, int vsize,
const char *topology_id);
void edid_get_monitor_name(const struct edid *edid, char *name, size_t name_size);
+void edid_get_serial_string(const struct edid *edid,
+ char *serial, size_t serial_size);
+uint32_t edid_get_serial_number(const struct edid *edid);
+void edid_get_any_serial(const struct edid *edid,
+ char *serial, size_t serial_size);

#endif
--
2.25.1