i965/rbc: Allow integer formats as advertised in isl_format.c
[mesa.git] / src / loader / loader.c
index 8634f45e78d76ffc1a7e3dded2765c482fc317ff..3e60e4ccf1d1cc9eadef2e9a0b24b45e7452d715 100644 (file)
 #include "xmlpool.h"
 #endif
 #endif
-#ifdef HAVE_SYSFS
-#include <sys/types.h>
+#ifdef MAJOR_IN_MKDEV
+#include <sys/mkdev.h>
+#endif
+#ifdef MAJOR_IN_SYSMACROS
+#include <sys/sysmacros.h>
 #endif
 #include "loader.h"
 
@@ -445,7 +448,7 @@ int loader_get_user_preferred_fd(int default_fd, int *different_device)
 }
 #endif
 
-#if defined(HAVE_SYSFS)
+#if defined(HAVE_SYSFS) || defined(HAVE_LIBDRM)
 static int
 dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
 {
@@ -466,7 +469,9 @@ dev_node_from_fd(int fd, unsigned int *maj, unsigned int *min)
 
    return 0;
 }
+#endif
 
+#if defined(HAVE_SYSFS)
 static int
 sysfs_get_pci_id_for_fd(int fd, int *vendor_id, int *chip_id)
 {
@@ -640,7 +645,7 @@ sysfs_get_device_name_for_fd(int fd)
    unsigned int maj, min;
    FILE *f;
    char buf[0x40];
-   static const char match[9] = "\0DEVNAME=";
+   static const char match[9] = "\nDEVNAME=";
    int expected = 1;
 
    if (dev_node_from_fd(fd, &maj, &min) < 0)
@@ -663,14 +668,34 @@ sysfs_get_device_name_for_fd(int fd)
    }
 
    strcpy(buf, "/dev/");
-   if (fgets(buf + 5, sizeof(buf) - 5, f))
+   if (fgets(buf + 5, sizeof(buf) - 5, f)) {
+      buf[strcspn(buf, "\n")] = '\0';
       device_name = strdup(buf);
+   }
 
    fclose(f);
    return device_name;
 }
 #endif
 
+#if defined(HAVE_LIBDRM)
+static char *
+drm_get_device_name_for_fd(int fd)
+{
+   unsigned int maj, min;
+   char buf[0x40];
+   int n;
+
+   if (dev_node_from_fd(fd, &maj, &min) < 0)
+      return NULL;
+
+   n = snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, min);
+   if (n == -1 || n >= sizeof(buf))
+      return NULL;
+
+   return strdup(buf);
+}
+#endif
 
 char *
 loader_get_device_name_for_fd(int fd)
@@ -684,6 +709,10 @@ loader_get_device_name_for_fd(int fd)
 #if HAVE_SYSFS
    if ((result = sysfs_get_device_name_for_fd(fd)))
       return result;
+#endif
+#if HAVE_LIBDRM
+   if ((result = drm_get_device_name_for_fd(fd)))
+      return result;
 #endif
    return result;
 }