diff mbox series

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

Message ID 20210601201702.23316-3-andrey.grodzovsky@amd.com (mailing list archive)
State New, archived
Headers show
Series libdrm tests for hot-unplug feature | expand

Commit Message

Andrey Grodzovsky June 1, 2021, 8:16 p.m. UTC
Used to access device controls

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

Comments

Simon Ser June 2, 2021, 9:16 a.m. UTC | #1
Do we really need to make this a public API?
Andrey Grodzovsky June 2, 2021, 2:25 p.m. UTC | #2
It calls drmNodeIsDRM which is private function itself so - if i 
implement it in amdgpu part
I still need to expose drmNodeIsDRM. Note that this function is 
basically a subset of drmGetDeviceNameFromFd2

Andrey

On 2021-06-02 5:16 a.m., Simon Ser wrote:
> Do we really need to make this a public API?
diff mbox series

Patch

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.
  *