diff mbox

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

Message ID 1422836095-8547-2-git-send-email-emil.l.velikov@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Emil Velikov Feb. 2, 2015, 12:14 a.m. UTC
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@ffwll.ch>
Cc: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 57 insertions(+)

Comments

Frank Binns Feb. 13, 2015, 11:07 a.m. UTC | #1
Hi Emil,

On 02/02/15 00:14, Emil Velikov wrote:
> 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@ffwll.ch>
> Cc: David Herrmann <dh.herrmann@googlemail.com>
> Signed-off-by: Emil Velikov <emil.l.velikov@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;
> +
fstat returns 0 on success and -1 otherwise so this should be

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;
> +
Same here.

> +	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
diff mbox

Patch

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