[PATCH libdrm 07/10] tests: Add helper to open a device/module

Thierry Reding thierry.reding at gmail.com
Wed Dec 9 09:37:45 PST 2015


From: Thierry Reding <treding at nvidia.com>

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 at nvidia.com>
---
 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 687b3c3c1cf3..57b0191b2655 100644
--- a/tests/util/kms.c
+++ b/tests/util/kms.c
@@ -41,9 +41,13 @@
 #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;
+}
diff --git a/tests/util/kms.h b/tests/util/kms.h
index fa9ab69983ac..dde2ed2c5636 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 */
-- 
2.5.0



More information about the dri-devel mailing list