[PATCH libdrm 03/12] xf86drm: move platform details to drmParsePciDeviceInfo()

Emil Velikov emil.l.velikov at gmail.com
Wed Sep 9 10:21:24 PDT 2015


As with previous commit let's try to keep drmGetDevices clean of linux
specifics.

Signed-off-by: Emil Velikov <emil.l.velikov at gmail.com>
---
 xf86drm.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

diff --git a/xf86drm.c b/xf86drm.c
index b4c5aa0..1174a64 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2924,11 +2924,22 @@ static int drmGetNodeType(const char *name)
     return -EINVAL;
 }
 
-static int drmParsePciDeviceInfo(const unsigned char *config,
+static int drmParsePciDeviceInfo(const char *d_name,
                                  drmPciDeviceInfoPtr device)
 {
-    if (config == NULL)
-        return -EINVAL;
+    char path[PATH_MAX + 1];
+    unsigned char config[64];
+    int fd, ret;
+
+    snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name);
+    fd = open(path, O_RDONLY);
+    if (fd < 0)
+        return -errno;
+
+    ret = read(fd, config, sizeof(config));
+    close(fd);
+    if (ret < 0)
+        return -errno;
 
     device->vendor_id = config[0] | (config[1] << 8);
     device->device_id = config[2] | (config[3] << 8);
@@ -2991,10 +3002,8 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
     struct stat sbuf = {0};
     char node[PATH_MAX + 1] = "";
     char path[PATH_MAX + 1] = "";
-    unsigned char config[64] = "";
     int node_type, subsystem_type;
     int maj, min;
-    int fd;
     int ret, i = 0, j, node_count, device_count = 0;
     int max_count = 16;
     int *duplicated = NULL;
@@ -3063,32 +3072,17 @@ int drmGetDevices(drmDevicePtr devices[], int max_devices)
             devs[i].available_nodes = 1 << node_type;
 
             if (devices != NULL) {
-                snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config",
-                         dent->d_name);
-                fd = open(path, O_RDONLY);
-                if (fd < 0) {
-                     ret = -errno;
-                     goto free_locals;
-                }
-                ret = read(fd, config, 64);
-                if (ret < 0) {
-                    ret = -errno;
-                    close(fd);
-                    goto free_locals;
-                }
-
                 pcidevice = calloc(1, sizeof(*pcidevice));
                 if (pcidevice == NULL) {
                     ret = -ENOMEM;
                     goto free_locals;
                 }
 
-                ret = drmParsePciDeviceInfo(config, pcidevice);
+                ret = drmParsePciDeviceInfo(dent->d_name, pcidevice);
                 if (ret)
                     goto free_locals;
 
                 devs[i].deviceinfo.pci = pcidevice;
-                close(fd);
             }
             break;
         default:
-- 
2.5.0



More information about the dri-devel mailing list