Skip to contentSkip to conversation

Avoid hardcoded strlens in drmParseSubsystemType().

PATCH In review Eric Anholt 2 participants 4 messages Nov 16, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Avoid hardcoded strlens in drmParseSubsystemType().

Having people count characters is error-prone, when we could just have a computer do it.

---
 xf86drm.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
xf86drm.c +16 -15
index 10df682b7870..60fbc49b3d35 100644
@@ -59,6 +59,8 @@
 #endif
 #include <math.h>
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
 /* Not all systems have MAP_FAILED defined */
 #ifndef MAP_FAILED
 #define MAP_FAILED ((void *)-1)
@@ -2984,6 +2986,16 @@ static int drmParseSubsystemType(int maj, int min)
     char path[PATH_MAX + 1];
     char link[PATH_MAX + 1] = "";
     char *name;
+    struct {
+        const char *name;
+        int bus_type;
+    } bus_types[] = {
+        { "/pci", DRM_BUS_PCI },
+        { "/usb", DRM_BUS_USB },
+        { "/platform", DRM_BUS_PLATFORM },
+        { "/host1x", DRM_BUS_HOST1X },
+        { "/virtio", DRM_BUS_VIRTIO },
+    };
 
     snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
              maj, min);
@@ -2995,20 +3007,10 @@ static int drmParseSubsystemType(int maj, int min)
     if (!name)
         return -EINVAL;
 
-    if (strncmp(name, "/pci", 4) == 0)
-        return DRM_BUS_PCI;
-
-    if (strncmp(name, "/usb", 4) == 0)
-        return DRM_BUS_USB;
-
-    if (strncmp(name, "/platform", 9) == 0)
-        return DRM_BUS_PLATFORM;
-
-    if (strncmp(name, "/host1x", 7) == 0)
-        return DRM_BUS_HOST1X;
-
-    if (strncmp(name, "/virtio", 7) == 0)
-        return DRM_BUS_VIRTIO;
+    for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
+        if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
+            return bus_types[i].bus_type;
+    }
 
     return -EINVAL;
 #elif defined(__OpenBSD__)
@@ -3149,7 +3151,6 @@ static int parse_separate_sysfs_files(int maj, int min,
                                       drmPciDeviceInfoPtr device,
                                       bool ignore_revision)
 {
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     static const char *attrs[] = {
       "revision", /* Older kernels are missing the file, so check for it first */
       "vendor",
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 2/2 drm: Attempt to parse SPI devices as platform bus devices.

For ARM systems with tinydrm displays attached to SPI, the bus name is /spi but we have platform device info for the rest. Fixes eglInitialize() failures on hx8357d since the EGL_EXT_device_drm changes.

---
 xf86drm.c | 1 +
 1 file changed, 1 insertion(+)
xf86drm.c +1 -0
index 60fbc49b3d35..71ad54baa5c8 100644
@@ -2993,6 +2993,7 @@ static int drmParseSubsystemType(int maj, int min)
         { "/pci", DRM_BUS_PCI },
         { "/usb", DRM_BUS_USB },
         { "/platform", DRM_BUS_PLATFORM },
+        { "/spi", DRM_BUS_PLATFORM },
         { "/host1x", DRM_BUS_HOST1X },
         { "/virtio", DRM_BUS_VIRTIO },
     };
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH libdrm 2/2] drm: Attempt to parse SPI devices as platform bus devices.

Quoted 4 lines · Eric Anholt

For ARM systems with tinydrm displays attached to SPI, the bus name is /spi but we have platform device info for the rest. Fixes eglInitialize() failures on hx8357d since the EGL_EXT_device_drm changes.

Quoted 23 lines
---
 xf86drm.c | 1 +
 1 file changed, 1 insertion(+)
xf86drm.c +1 -0
index 60fbc49b3d35..71ad54baa5c8 100644
@@ -2993,6 +2993,7 @@ static int drmParseSubsystemType(int maj, int min)
         { "/pci", DRM_BUS_PCI },
         { "/usb", DRM_BUS_USB },
         { "/platform", DRM_BUS_PLATFORM },
+        { "/spi", DRM_BUS_PLATFORM },
         { "/host1x", DRM_BUS_HOST1X },
         { "/virtio", DRM_BUS_VIRTIO },
     };
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Reviewed-by on v1 1/2
#3 lore raw
In reply to Eric Anholt
Quoted 2 lines · Eric Anholt

Having people count characters is error-prone, when we could just have a computer do it.

Quoted 74 lines
---
 xf86drm.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
xf86drm.c +16 -15
index 10df682b7870..60fbc49b3d35 100644
@@ -59,6 +59,8 @@
 #endif
 #include <math.h>
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
 /* Not all systems have MAP_FAILED defined */
 #ifndef MAP_FAILED
 #define MAP_FAILED ((void *)-1)
@@ -2984,6 +2986,16 @@ static int drmParseSubsystemType(int maj, int min)
     char path[PATH_MAX + 1];
     char link[PATH_MAX + 1] = "";
     char *name;
+    struct {
+        const char *name;
+        int bus_type;
+    } bus_types[] = {
+        { "/pci", DRM_BUS_PCI },
+        { "/usb", DRM_BUS_USB },
+        { "/platform", DRM_BUS_PLATFORM },
+        { "/host1x", DRM_BUS_HOST1X },
+        { "/virtio", DRM_BUS_VIRTIO },
+    };
 
     snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
              maj, min);
@@ -2995,20 +3007,10 @@ static int drmParseSubsystemType(int maj, int min)
     if (!name)
         return -EINVAL;
 
-    if (strncmp(name, "/pci", 4) == 0)
-        return DRM_BUS_PCI;
-
-    if (strncmp(name, "/usb", 4) == 0)
-        return DRM_BUS_USB;
-
-    if (strncmp(name, "/platform", 9) == 0)
-        return DRM_BUS_PLATFORM;
-
-    if (strncmp(name, "/host1x", 7) == 0)
-        return DRM_BUS_HOST1X;
-
-    if (strncmp(name, "/virtio", 7) == 0)
-        return DRM_BUS_VIRTIO;
+    for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
+        if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
+            return bus_types[i].bus_type;
+    }
 
     return -EINVAL;
 #elif defined(__OpenBSD__)
@@ -3149,7 +3151,6 @@ static int parse_separate_sysfs_files(int maj, int min,
                                       drmPciDeviceInfoPtr device,
                                       bool ignore_revision)
 {
-#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
     static const char *attrs[] = {
       "revision", /* Older kernels are missing the file, so check for it first */
       "vendor",
Signature · 6 lines
2.19.1

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel