From patchwork Mon Dec 17 02:02:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Martin Peres X-Patchwork-Id: 1885791 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by patchwork2.kernel.org (Postfix) with ESMTP id 4F182DFAC4 for ; Mon, 17 Dec 2012 02:03:29 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 1EDCCE609C for ; Sun, 16 Dec 2012 18:03:29 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from ensi-bourges.fr (mail.ensi-bourges.fr [195.221.38.25]) by gabe.freedesktop.org (Postfix) with ESMTP id 6962CE5C7A for ; Sun, 16 Dec 2012 18:03:14 -0800 (PST) Received: (qmail 5560 invoked from network); 17 Dec 2012 02:00:11 -0000 Received: from unknown (HELO cathaou.dartybox.com) (Authenticated?user?:martin.peres@[89.159.199.232]) (envelope-sender ) by mail.ensi-bourges.fr (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 17 Dec 2012 02:00:11 -0000 From: martin.peres@free.fr To: dri-devel@lists.freedesktop.org Subject: =?UTF-8?q?=5BPATCH=202/5=5D=20drm=3A=20generate=20the=20path=20of=20a=20different=20device=20type=20for=20the=20same=20drm=20device?= Date: Mon, 17 Dec 2012 03:02:52 +0100 Message-Id: <1355709775-15674-2-git-send-email-martin.peres@free.fr> X-Mailer: git-send-email 1.8.0.2 In-Reply-To: <1355709775-15674-1-git-send-email-martin.peres@free.fr> References: <50CE7868.4040205@free.fr> <1355709775-15674-1-git-send-email-martin.peres@free.fr> MIME-Version: 1.0 Cc: Martin Peres X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org From: Martin Peres Signed-off-by: Martin Peres --- .gitignore | 1 + tests/Makefile.am | 1 + tests/same_device_but_type.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ xf86drm.c | 44 +++++++++++++++++++++++++++++++++++++ xf86drm.h | 1 + 5 files changed, 99 insertions(+) create mode 100644 tests/same_device_but_type.c diff --git a/.gitignore b/.gitignore index 28c77c5..318e129 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 a3a59bd..ddc73ca 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 0000000..f35951f --- /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 + * + */ + +#include +#include +#include +#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 eb0549d..cca8d8a 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 d727ce1..658dfd7 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);