From e744b02375e939e853b4c83979f170bfc89e482c Mon Sep 17 00:00:00 2001 From: Thierry Reding Date: Wed, 9 Dec 2015 18:37:45 +0100 Subject: tests: Add helper to open a device/module 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 Reviewed-by: Emil Velikov --- tests/util/kms.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/util/kms.h | 2 ++ 2 files changed, 57 insertions(+) diff --git a/tests/util/kms.c b/tests/util/kms.c index 687b3c3c..57b0191b 100644 --- a/tests/util/kms.c +++ b/tests/util/kms.c @@ -41,9 +41,13 @@ #include "config.h" #endif +#include #include +#include #include +#include +#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; +} diff --git a/tests/util/kms.h b/tests/util/kms.h index fa9ab699..dde2ed2c 100644 --- a/tests/util/kms.h +++ b/tests/util/kms.h @@ -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 */ -- cgit v1.2.3