aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEmil Velikov2016-11-10 11:26:50 -0600
committerEmil Velikov2016-11-22 07:54:23 -0600
commit37d790f7d449874d0bf199d9ca9871d12b4d599a (patch)
tree1522aa7863dc4e8cc9ff02f690978cb88dfcd419 /xf86drm.c
parent7e0bc3bf1c247e1d53733d0e2e2ada52d29b5327 (diff)
downloadexternal-libgbm-37d790f7d449874d0bf199d9ca9871d12b4d599a.tar.gz
external-libgbm-37d790f7d449874d0bf199d9ca9871d12b4d599a.tar.xz
external-libgbm-37d790f7d449874d0bf199d9ca9871d12b4d599a.zip
xf86drm: introduce drmGetDeviceNameFromFd2
The original version considered only card devices, while this will pick the device/node name regardless - card, control, renderD, other... Current implementation is "linux" specific, in such that it relies on sysfs/uevent file. At the same time this gives us the flexibility to support any nodes even future ones, as long as they're within DRM_MAJOR. Shamelessly copied from mesa, latter by: Gary Wong <gtw@gnu.org> Signed-off-by: Emil Velikov <emil.velikov@collabora.com> Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com>
Diffstat (limited to 'xf86drm.c')
-rw-r--r--xf86drm.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/xf86drm.c b/xf86drm.c
index 9b97bbb6..ed924a74 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -3303,3 +3303,54 @@ free_locals:
3303 free(local_devices); 3303 free(local_devices);
3304 return ret; 3304 return ret;
3305} 3305}
3306
3307char *drmGetDeviceNameFromFd2(int fd)
3308{
3309#ifdef __linux__
3310 struct stat sbuf;
3311 char *device_name = NULL;
3312 unsigned int maj, min;
3313 FILE *f;
3314 char buf[512];
3315 static const char match[9] = "\nDEVNAME=";
3316 int expected = 1;
3317
3318
3319 if (fstat(fd, &sbuf))
3320 return NULL;
3321
3322 maj = major(sbuf.st_rdev);
3323 min = minor(sbuf.st_rdev);
3324
3325 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3326 return NULL;
3327
3328 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
3329 if (!(f = fopen(buf, "r")))
3330 return NULL;
3331
3332 while (expected < sizeof(match)) {
3333 int c = getc(f);
3334
3335 if (c == EOF) {
3336 fclose(f);
3337 return NULL;
3338 } else if (c == match[expected] )
3339 expected++;
3340 else
3341 expected = 0;
3342 }
3343
3344 strcpy(buf, "/dev/");
3345 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
3346 buf[strcspn(buf, "\n")] = '\0';
3347 device_name = strdup(buf);
3348 }
3349
3350 fclose(f);
3351 return device_name;
3352#else
3353#warning "Missing implementation of drmGetDeviceNameFromFd2"
3354 return NULL;
3355#endif
3356}