[PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers

Emil Velikov emil.l.velikov at gmail.com
Sun Feb 1 16:14:54 PST 2015


Currently most places assume reliable primary <> render node mapping.
Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and safes all the
guesswork for the user.

Cc: Daniel Vetter <daniel.vetter at ffwll.ch>
Cc: David Herrmann <dh.herrmann at googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
---
 xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 57 insertions(+)

diff --git a/xf86drm.c b/xf86drm.c
index 345325a..6af7ac0 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2607,3 +2607,57 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+char *drmGetDeviceNameFromRenderFD(int fd)
+{
+
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}
+
+char *drmGetRenderNameFromDeviceFD(int fd)
+{
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}
diff --git a/xf86drm.h b/xf86drm.h
index bfd0670..bca5887 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -740,6 +740,9 @@ extern char *drmGetDeviceNameFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetRenderNameFromDeviceFD(int fd);
+extern char *drmGetDeviceNameFromRenderFD(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
-- 
2.2.2



More information about the dri-devel mailing list