summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Peres <martin.peres@labri.fr>2012-12-16 01:44:02 +0100
committerMartin Peres <martin.peres@labri.fr>2012-12-16 01:47:20 +0100
commit2282114cad1fc8e16a43e07ba8c7b94d21acda88 (patch)
treec0ab3aeee71baaaec5fb17f3700e07af35be4276
parent4a86cbbed0c6d74a8b68ec72b8de8e48905d4015 (diff)
drm: generate the path of a different device type for the same drm device
Signed-off-by: Martin Peres <martin.peres@labri.fr>
-rw-r--r--.gitignore1
-rw-r--r--tests/Makefile.am1
-rw-r--r--tests/same_device_but_type.c52
-rw-r--r--xf86drm.c44
-rw-r--r--xf86drm.h1
5 files changed, 99 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 28c77c53..318e129c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@ tests/gem_readwrite
tests/openclose
tests/setversion
tests/updatedraw
+tests/same_device_but_type
tests/modeprint/modeprint
tests/modetest/modetest
tests/kmstest/kmstest
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a3a59bd5..ddc73ca5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -46,6 +46,7 @@ TESTS = \
setversion \
updatedraw \
name_from_fd \
+ same_device_but_type \
$(NULL)
SUBDIRS += vbltest $(NULL)
diff --git a/tests/same_device_but_type.c b/tests/same_device_but_type.c
new file mode 100644
index 00000000..f35951f7
--- /dev/null
+++ b/tests/same_device_but_type.c
@@ -0,0 +1,52 @@
+/*
+ * Copyright © 2012
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ * Martin Peres <martin.peres@labri.fr>
+ *
+ */
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <limits.h>
+#include "drmtest.h"
+
+/**
+ * Checks drmGetSameDeviceButType
+ */
+int main(int argc, char **argv)
+{
+ int fd, ret;
+ drm_set_version_t sv, version;
+ const char *name = "/dev/dri/card0";
+ char *render;
+ int i;
+
+ render = drmGetSameDeviceButType(name, DRM_NODE_RENDER);
+ assert(strcmp(render, name) == 0);
+ drmFree(render);
+
+ render = drmGetSameDeviceButType("/no_device", DRM_NODE_RENDER);
+ assert(render == NULL);
+
+ return 0;
+}
diff --git a/xf86drm.c b/xf86drm.c
index eb0549d6..cca8d8a3 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2602,6 +2602,50 @@ char *drmGetDeviceNameFromFd(int fd)
return NULL;
}
+/**
+ * Generate the path to a different type (render node, control node, etc...)
+ * but for the same actual drm device.
+ *
+ * \param device a path to an existing drm device type.
+ * \param type the new type that should be returned.
+ *
+ * \return the path to the same drm device but to a different type if success.
+ * NULL otherwise.
+ */
+char *drmGetSameDeviceButType(const char *device, int type)
+{
+ char base_path[64], name[64], *path, *device_name;
+ struct stat sbuf;
+ int i;
+
+ /* get the device name (ie /card0) */
+ device_name=strrchr(device, '/');
+ if (!device_name)
+ return NULL;
+
+ /* get the device's drm folder (ie /sys/class/drm/card0/device/drm */
+ snprintf(base_path, sizeof(base_path), "/sys/class/drm/%s/device/drm",
+ device_name + 1);
+
+ /* search for the type in the base path */
+ device_name = NULL;
+ for (i = 0; i < DRM_MAX_MINOR; i++) {
+ drmDevicePath(name, sizeof(name), base_path, type, i);
+ if (stat(name, &sbuf) == 0) {
+ device_name = strrchr(name, '/');
+ break;
+ }
+ }
+ if (!device_name)
+ return NULL;
+
+ /* create a more appropriate path for the device (/dev/dri/card0) */
+ path = malloc(64);
+ snprintf(path, 64, "%s/%s", DRM_DIR_NAME, device_name + 1);
+
+ return path;
+}
+
int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
{
struct drm_prime_handle args;
diff --git a/xf86drm.h b/xf86drm.h
index d727ce1b..658dfd73 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -733,6 +733,7 @@ typedef struct _drmEventContext {
extern int drmHandleEvent(int fd, drmEventContextPtr evctx);
extern char *drmGetDeviceNameFromFd(int fd);
+extern char *drmGetSameDeviceButType(const char *device, int type);
extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);