intel: use the common DRM subsystem scanner
diff --git a/icd/intel/intel.c b/icd/intel/intel.c
index b9860c2..2f65585 100644
--- a/icd/intel/intel.c
+++ b/icd/intel/intel.c
@@ -26,20 +26,7 @@
  *   Chia-I Wu <olv@lunarg.com>
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <fnmatch.h>
-
-#include <libudev.h>
-
+#include "icd-enumerate-drm.h"
 #include "gpu.h"
 #include "intel.h"
 
@@ -82,35 +69,16 @@
     }
 }
 
-static int is_render_node(int fd, struct stat *st)
+ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(
+    const XGL_APPLICATION_INFO*                 pAppInfo,
+    const XGL_ALLOC_CALLBACKS*                  pAllocCb,
+    XGL_UINT                                    maxGpus,
+    XGL_UINT*                                   pGpuCount,
+    XGL_PHYSICAL_GPU*                           pGpus)
 {
-    if (fstat(fd, st))
-        return 0;
-
-    if (!S_ISCHR(st->st_mode))
-        return 0;
-
-    return st->st_rdev & 0x80;
-}
-
-ICD_EXPORT XGL_RESULT XGLAPI xglInitAndEnumerateGpus(const XGL_APPLICATION_INFO *
-                                            pAppInfo,
-                                            const XGL_ALLOC_CALLBACKS *
-                                            pAllocCb, XGL_UINT maxGpus,
-                                            XGL_UINT * pGpuCount,
-                                            XGL_PHYSICAL_GPU * pGpus)
-{
-    struct udev *udev;
-    struct udev_enumerate *e;
-    struct udev_device *device, *parent;
-    struct udev_list_entry *entry;
-    const char *pci_id, *path;
-    const char *usub, *dnode;
-    int fd;
-    struct stat st;
-    char *pci_glob = "*:*";
+    struct icd_drm_device *devices, *dev;
     XGL_RESULT ret;
-    XGL_UINT count = 0;
+    XGL_UINT count;
 
     intel_debug_init();
 
@@ -119,8 +87,8 @@
         return ret;
 
     /*
-     * xglInitAndEnumerateGpus() can be called multiple times. Calling it more than once
-     * forces driver reinitialization.
+     * xglInitAndEnumerateGpus() can be called multiple times. Calling it more
+     * than once forces driver reinitialization.
      */
     intel_gpu_remove_all();
 
@@ -129,73 +97,29 @@
         return XGL_SUCCESS;
     }
 
-    // TODO: Do we need any other validation for incoming pointers?
+    devices = icd_drm_enumerate(0x8086);
 
-    udev = udev_new();
-    if (udev == NULL) {
-        icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0,
-                XGL_NULL_HANDLE, 0, 0, "failed to initialize udev context");
-        return XGL_ERROR_OUT_OF_MEMORY;
-    }
-
-    fd = -1;
-    e = udev_enumerate_new(udev);
-    udev_enumerate_add_match_subsystem(e, "drm");
-    udev_enumerate_scan_devices(e);
-    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
-        unsigned int ven_id, dev_id;
+    count = 0;
+    dev = devices;
+    while (dev) {
+        const char *devnode;
         struct intel_gpu *gpu;
 
-        path = udev_list_entry_get_name(entry);
-        device = udev_device_new_from_syspath(udev, path);
-        parent = udev_device_get_parent(device);
-        usub = udev_device_get_subsystem(parent);
-        /* Filter out KMS output devices. */
-        if (!usub || (strcmp(usub, "pci") != 0)) {
-            udev_device_unref(device);
+        devnode = icd_drm_get_devnode(dev, ICD_DRM_MINOR_RENDER);
+        if (!devnode)
             continue;
-        }
-        pci_id = udev_device_get_property_value(parent, "PCI_ID");
-        if (fnmatch(pci_glob, pci_id, 0) != 0) {
-            udev_device_unref(device);
-            continue;
-        }
-        sscanf(pci_id, "%x:%x", &ven_id, &dev_id);
-        if (ven_id != 0x8086) {
-            udev_device_unref(device);
-            continue;
-        }
 
-        dnode = udev_device_get_devnode(device);
-        /* TODO do not open the device at this point */
-        fd = open(dnode, O_RDWR);
-        if (fd < 0) {
-            udev_device_unref(device);
-            continue;
-        }
-        if (!is_render_node(fd, &st)) {
-            close(fd);
-            fd = -1;
-            udev_device_unref(device);
-            continue;
-        }
-        close(fd);
-
-        if (intel_devid_override)
-            dev_id = intel_devid_override;
-        ret = intel_gpu_add(dev_id, dnode, &gpu);
-
-        udev_device_unref(device);
-
+        ret = intel_gpu_add(dev->devid, devnode, &gpu);
         if (ret == XGL_SUCCESS) {
             pGpus[count++] = (XGL_PHYSICAL_GPU) gpu;
             if (count >= maxGpus)
                 break;
         }
+
+        dev = dev->next;
     }
 
-    udev_enumerate_unref(e);
-    udev_unref(udev);
+    icd_drm_release(devices);
 
     *pGpuCount = count;