diff mbox

[2/2] libdrm: add drmGetNodeType() helper

Message ID 1422836095-8547-3-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
The add a simple helper which returns the node type of the opened fd.
Likely to be used in conjunction with the previous two helpers.

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 | 43 +++++++++++++++++++++++++++++++++++++++----
 xf86drm.h |  7 +++++++
 2 files changed, 46 insertions(+), 4 deletions(-)

Comments

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

On 02/02/15 00:14, Emil Velikov wrote:
> The add a simple helper which returns the node type of the opened fd.
> Likely to be used in conjunction with the previous two helpers.
>
> 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 | 43 +++++++++++++++++++++++++++++++++++++++----
>  xf86drm.h |  7 +++++++
>  2 files changed, 46 insertions(+), 4 deletions(-)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index 6af7ac0..a70c0dd 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -85,10 +85,6 @@
>  
>  #define DRM_MSG_VERBOSITY 3
>  
> -#define DRM_NODE_CONTROL 0
> -#define DRM_NODE_PRIMARY 1
> -#define DRM_NODE_RENDER 2
> -
>  static drmServerInfoPtr drm_server_info;
>  
>  void drmSetServerInfo(drmServerInfoPtr info)
> @@ -2607,6 +2603,45 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
>  	return 0;
>  }
>  
> +int drmGetNodeType(int fd, drmNodeType *type)
> +{
> +	struct stat sbuf;
> +	char name[64];
> +	dev_t d;
> +	int i;
> +
> +	if (!fstat(fd, &sbuf))
> +		return -errno;
> +
> +	d = sbuf.st_rdev;
> +
> +	for (i = 0; i < DRM_MAX_MINOR; i++) {
> +		snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
> +		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> +			*type = DRM_NODE_PRIMARY;
> +			return 0;
> +		}
> +	}
> +
> +	for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
> +		snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
> +		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> +			*type = DRM_NODE_CONTROL;
> +			return 0;
> +		}
> +	}
> +
> +	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
> +		snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
> +		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> +			*type = DRM_NODE_RENDER;
> +			return 0;
> +		}
> +	}
It would seem easier (and faster) to use minor(sbuf.st_rdev) to
determine the type of node.

I'm not sure what the etiquette is here but I've prepared a patch that
does it this way instead, which will follow this email. Please feel free
to add your own signed-off-by, suggested-by, etc as I don't want to
steal all your credit :)

Thanks
Frank

> +
> +	return -EINVAL;
> +}
> +
>  char *drmGetDeviceNameFromRenderFD(int fd)
>  {
>  
> diff --git a/xf86drm.h b/xf86drm.h
> index bca5887..7d67df9 100644
> --- a/xf86drm.h
> +++ b/xf86drm.h
> @@ -740,6 +740,13 @@ 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);
>  
> +typedef enum _drmNodeType {
> +    DRM_NODE_CONTROL = 0,
> +    DRM_NODE_PRIMARY = 1,
> +    DRM_NODE_RENDER  = 2
> +} drmNodeType, *drmNodeTypePtr;
> +
> +extern int drmGetNodeType(int fd, drmNodeType *type);
>  extern char *drmGetRenderNameFromDeviceFD(int fd);
>  extern char *drmGetDeviceNameFromRenderFD(int fd);
>
diff mbox

Patch

diff --git a/xf86drm.c b/xf86drm.c
index 6af7ac0..a70c0dd 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -85,10 +85,6 @@ 
 
 #define DRM_MSG_VERBOSITY 3
 
-#define DRM_NODE_CONTROL 0
-#define DRM_NODE_PRIMARY 1
-#define DRM_NODE_RENDER 2
-
 static drmServerInfoPtr drm_server_info;
 
 void drmSetServerInfo(drmServerInfoPtr info)
@@ -2607,6 +2603,45 @@  int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+int drmGetNodeType(int fd, drmNodeType *type)
+{
+	struct stat sbuf;
+	char name[64];
+	dev_t d;
+	int i;
+
+	if (!fstat(fd, &sbuf))
+		return -errno;
+
+	d = sbuf.st_rdev;
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_PRIMARY;
+			return 0;
+		}
+	}
+
+	for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_CONTROL;
+			return 0;
+		}
+	}
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_RENDER;
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
 char *drmGetDeviceNameFromRenderFD(int fd)
 {
 
diff --git a/xf86drm.h b/xf86drm.h
index bca5887..7d67df9 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -740,6 +740,13 @@  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);
 
+typedef enum _drmNodeType {
+    DRM_NODE_CONTROL = 0,
+    DRM_NODE_PRIMARY = 1,
+    DRM_NODE_RENDER  = 2
+} drmNodeType, *drmNodeTypePtr;
+
+extern int drmGetNodeType(int fd, drmNodeType *type);
 extern char *drmGetRenderNameFromDeviceFD(int fd);
 extern char *drmGetDeviceNameFromRenderFD(int fd);