OSDN Git Service

tests: Add helper to open a device/module
authorThierry Reding <treding@nvidia.com>
Wed, 9 Dec 2015 17:37:45 +0000 (18:37 +0100)
committerEmil Velikov <emil.l.velikov@gmail.com>
Fri, 18 Dec 2015 17:44:13 +0000 (17:44 +0000)
The new function util_open() encapsulates the standard method employed
by tests to open a device or module. There is a verbatim copy of this in
almost all test programs, with slight variations in the list of modules.
Moving this code into a common helper allows code reuse and makes tests
more consistent.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
tests/util/kms.c
tests/util/kms.h

index 687b3c3..57b0191 100644 (file)
 #include "config.h"
 #endif
 
+#include <errno.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
+#include "xf86drm.h"
 #include "xf86drmMode.h"
 
 #include "common.h"
@@ -120,3 +124,54 @@ const char *util_lookup_connector_type_name(unsigned int type)
        return util_lookup_type_name(type, connector_type_names,
                                     ARRAY_SIZE(connector_type_names));
 }
+
+static const char * const modules[] = {
+       "i915",
+       "radeon",
+       "nouveau",
+       "vmwgfx",
+       "omapdrm",
+       "exynos",
+       "tilcdc",
+       "msm",
+       "sti",
+       "tegra",
+       "imx-drm",
+       "rockchip",
+       "atmel-hlcdc",
+};
+
+int util_open(const char *device, const char *module)
+{
+       int fd;
+
+       if (module) {
+               fd = drmOpen(module, device);
+               if (fd < 0) {
+                       fprintf(stderr, "failed to open device '%s': %s\n",
+                               module, strerror(errno));
+                       return -errno;
+               }
+       } else {
+               unsigned int i;
+
+               for (i = 0; i < ARRAY_SIZE(modules); i++) {
+                       printf("trying to open device '%s'...", modules[i]);
+
+                       fd = drmOpen(modules[i], device);
+                       if (fd < 0) {
+                               printf("failed\n");
+                       } else {
+                               printf("done\n");
+                               break;
+                       }
+               }
+
+               if (fd < 0) {
+                       fprintf(stderr, "no device found\n");
+                       return -ENODEV;
+               }
+       }
+
+       return fd;
+}
index fa9ab69..dde2ed2 100644 (file)
@@ -30,4 +30,6 @@ const char *util_lookup_encoder_type_name(unsigned int type);
 const char *util_lookup_connector_status_name(unsigned int type);
 const char *util_lookup_connector_type_name(unsigned int type);
 
+int util_open(const char *device, const char *module);
+
 #endif /* UTIL_KMS_H */