[PATCH 2/7] xf86drm: Add function to retrieve char device path

Andrey Grodzovsky andrey.grodzovsky at amd.com
Tue Jun 1 20:16:57 UTC 2021


Used to access device controls

Signed-off-by: Andrey Grodzovsky <andrey.grodzovsky at amd.com>
---
 xf86drm.c | 23 +++++++++++++++++++++++
 xf86drm.h |  1 +
 2 files changed, 24 insertions(+)

diff --git a/xf86drm.c b/xf86drm.c
index edfeb347..a5ecd323 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -4361,6 +4361,29 @@ drm_public char *drmGetDeviceNameFromFd2(int fd)
 #endif
 }
 
+drm_public char *drmGetCharDeviceFromFd(int fd)
+{
+#ifdef __linux__
+    struct stat sbuf;
+    char path[PATH_MAX + 1];
+    unsigned int maj, min;
+
+    if (fstat(fd, &sbuf))
+        return NULL;
+
+    maj = major(sbuf.st_rdev);
+    min = minor(sbuf.st_rdev);
+
+    if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
+        return NULL;
+
+    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
+    return strdup(path);
+#else
+    return NULL;
+#endif
+}
+
 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
 {
     struct drm_syncobj_create args;
diff --git a/xf86drm.h b/xf86drm.h
index 9fc06ab8..c172dbc1 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -812,6 +812,7 @@ extern char *drmGetDeviceNameFromFd(int fd);
  */
 extern char *drmGetDeviceNameFromFd2(int fd);
 extern int drmGetNodeTypeFromFd(int fd);
+extern char *drmGetCharDeviceFromFd(int fd);
 
 /* Convert between GEM handles and DMA-BUF file descriptors.
  *
-- 
2.25.1



More information about the amd-gfx mailing list