aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Velikov2015-03-06 18:58:39 -0600
committerEmil Velikov2015-03-10 13:14:40 -0500
commit0ca03a4087a550646de7f26b6b53a932e8546474 (patch)
tree38eb3eca83bdd1260767a437b1482a6d5bd9c736 /xf86drm.c
parentb374a59e0e2ef52fed737f6db9ee1e40caca46ea (diff)
downloadexternal-libgbm-0ca03a4087a550646de7f26b6b53a932e8546474.tar.gz
external-libgbm-0ca03a4087a550646de7f26b6b53a932e8546474.tar.xz
external-libgbm-0ca03a4087a550646de7f26b6b53a932e8546474.zip
drm: add drmGet(Primary|Render)DeviceNameFromFd functions
Currently most places assume reliable primary(master) <> render node mapping. Although this may work in some cases, it is not correct. Add a couple of helpers that hide the details and provide the name of the master or render device name, given an fd. The latter may belong to either the master, control or render node device. v2: - Rename Device and Primary to Master (aka the /dev/dri/cardX device). - Check for the file via readdir_r() rather than stat(). - Wrap the check into a single function. - Return NULL for non-linux platforms. v3: - Don't segfault if name is NULL. - Update function names, as suggested by Frank Binns. v4: - Update commit message to reflect the function name changes. Cc: Frank Binns <frank.binns@imgtec.com> Cc: Daniel Vetter <daniel.vetter@ffwll.ch> Cc: David Herrmann <dh.herrmann@googlemail.com> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com> Reviewed-by: Frank Binns <frank.binns@imgtec.com>
Diffstat (limited to 'xf86drm.c')
-rw-r--r--xf86drm.c84
1 files changed, 84 insertions, 0 deletions
diff --git a/xf86drm.c b/xf86drm.c
index e117bc63..194cd35e 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -40,6 +40,8 @@
40#include <string.h> 40#include <string.h>
41#include <strings.h> 41#include <strings.h>
42#include <ctype.h> 42#include <ctype.h>
43#include <dirent.h>
44#include <stddef.h>
43#include <fcntl.h> 45#include <fcntl.h>
44#include <errno.h> 46#include <errno.h>
45#include <signal.h> 47#include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
522 } 524 }
523} 525}
524 526
527static const char *drmGetMinorName(int type)
528{
529 switch (type) {
530 case DRM_NODE_PRIMARY:
531 return "card";
532 case DRM_NODE_CONTROL:
533 return "controlD";
534 case DRM_NODE_RENDER:
535 return "renderD";
536 default:
537 return NULL;
538 }
539}
540
525/** 541/**
526 * Open the device by bus ID. 542 * Open the device by bus ID.
527 * 543 *
@@ -2736,3 +2752,71 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2736 return 0; 2752 return 0;
2737} 2753}
2738 2754
2755static char *drmGetMinorNameForFD(int fd, int type)
2756{
2757#ifdef __linux__
2758 DIR *sysdir;
2759 struct dirent *pent, *ent;
2760 struct stat sbuf;
2761 const char *name = drmGetMinorName(type);
2762 int len;
2763 char dev_name[64], buf[64];
2764 long name_max;
2765 int maj, min;
2766
2767 if (!name)
2768 return NULL;
2769
2770 len = strlen(name);
2771
2772 if (fstat(fd, &sbuf))
2773 return NULL;
2774
2775 maj = major(sbuf.st_rdev);
2776 min = minor(sbuf.st_rdev);
2777
2778 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2779 return NULL;
2780
2781 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2782
2783 sysdir = opendir(buf);
2784 if (!sysdir)
2785 return NULL;
2786
2787 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2788 if (name_max == -1)
2789 goto out_close_dir;
2790
2791 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2792 if (pent == NULL)
2793 goto out_close_dir;
2794
2795 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2796 if (strncmp(ent->d_name, name, len) == 0) {
2797 free(pent);
2798 closedir(sysdir);
2799
2800 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2801 ent->d_name);
2802 return strdup(dev_name);
2803 }
2804 }
2805
2806 free(pent);
2807
2808out_close_dir:
2809 closedir(sysdir);
2810#endif
2811 return NULL;
2812}
2813
2814char *drmGetPrimaryDeviceNameFromFd(int fd)
2815{
2816 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2817}
2818
2819char *drmGetRenderDeviceNameFromFd(int fd)
2820{
2821 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2822}