From patchwork Mon Feb 23 12:22:56 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 5865221 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A27AC9F269 for ; Mon, 23 Feb 2015 12:20:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A986B20634 for ; Mon, 23 Feb 2015 12:20:05 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 809DE20630 for ; Mon, 23 Feb 2015 12:20:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6C63D6E07B; Mon, 23 Feb 2015 04:20:03 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wg0-f54.google.com (mail-wg0-f54.google.com [74.125.82.54]) by gabe.freedesktop.org (Postfix) with ESMTP id CAB556E07B for ; Mon, 23 Feb 2015 04:20:01 -0800 (PST) Received: by mail-wg0-f54.google.com with SMTP id y19so25140597wgg.13 for ; Mon, 23 Feb 2015 04:20:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=aroBxkFNLlgcNwUWJj+aY8a2LHT7NaoAjYevPb72ULo=; b=szKbJ4nWutt0XfSCJYivp/jHknOaMRX4Cvr1nOm7kT/4IubMR0IGX7OQxCH34+7T24 kBX+VH7TtMcD5qg0WemrRcLf9AwhUSCCVQdO3WcHliUHQ+v6TRuErqgJ2qCe/tZGLW5h 9RjQB70cxq8v25M0cNu4Ua+WF8oMEzk5LulJdCEeqItqkYR9+Ir569+OoCk1PTD7aTgl 9KrOETO072RlBtHhmbQ4lwT6VmcDt7LVVllFCms4VxfGL/XunzweWL6Me7f8kafm3hxF BK5xE+24YXyM/RTO9P26IVN1C9S9MTp/JpC7P1Av0AG2DZj+3HylmkS1p7ClMSHT72m0 skPQ== X-Received: by 10.194.59.112 with SMTP id y16mr22451707wjq.36.1424694000906; Mon, 23 Feb 2015 04:20:00 -0800 (PST) Received: from arch-x220.localdomain (cpc12-croy20-2-0-cust52.croy.cable.virginm.net. [82.44.54.53]) by mx.google.com with ESMTPSA id p1sm15640492wib.23.2015.02.23.04.19.59 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Feb 2015 04:20:00 -0800 (PST) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Date: Mon, 23 Feb 2015 12:22:56 +0000 Message-Id: <1424694176-3210-1-git-send-email-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.3.0 In-Reply-To: <1422836095-8547-2-git-send-email-emil.l.velikov@gmail.com> References: <1422836095-8547-2-git-send-email-emil.l.velikov@gmail.com> Cc: David Herrmann , emil.l.velikov@gmail.com, Daniel Vetter X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct. Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD. v2: - Rename Device and Primary to Master (aka the /dev/dri/cardX device). - Check for the file via readdir_r() rather than stat(). - Wrap the check into a single function. - Return NULL for non-linux platforms. Cc: Frank Binns Cc: Daniel Vetter Cc: David Herrmann Signed-off-by: Emil Velikov Reviewed-by: Frank Binns --- xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xf86drm.h | 3 +++ 2 files changed, 85 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index e117bc6..d4a4dc6 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include #include #include @@ -522,6 +524,20 @@ static int drmGetMinorType(int minor) } } +static const char *drmGetMinorName(int type) +{ + switch (type) { + case DRM_NODE_PRIMARY: + return "card"; + case DRM_NODE_CONTROL: + return "controlD"; + case DRM_NODE_RENDER: + return "renderD"; + default: + return NULL; + } +} + /** * Open the device by bus ID. * @@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) return 0; } +static char *drmGetMinorNameForFD(int fd, int type) +{ +#ifdef __linux__ + DIR *sysdir; + struct dirent *pent, *ent; + struct stat sbuf; + const char *name = drmGetMinorName(type); + const int len = strlen(name); + char dev_name[64], buf[64]; + long name_max; + int maj, min; + + if (!name) + return NULL; + + if (fstat(fd, &sbuf)) + return NULL; + + maj = major(sbuf.st_rdev); + min = minor(sbuf.st_rdev); + + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) + return NULL; + + snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min); + + sysdir = opendir(buf); + if (!sysdir) + return NULL; + + name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX); + if (name_max == -1) + goto out_close_dir; + + pent = malloc(offsetof(struct dirent, d_name) + name_max + 1); + if (pent == NULL) + goto out_close_dir; + + while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) { + if (strncmp(ent->d_name, name, len) == 0) { + free(pent); + closedir(sysdir); + + snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s", + ent->d_name); + return strdup(dev_name); + } + } + + free(pent); + +out_close_dir: + closedir(sysdir); +#endif + return NULL; +} + +char *drmGetMasterNameFromRenderFD(int fd) +{ + return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY); +} + +char *drmGetRenderNameFromMasterFD(int fd) +{ + return drmGetMinorNameForFD(fd, DRM_NODE_RENDER); +} diff --git a/xf86drm.h b/xf86drm.h index afd38a1..5fdf27b 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd); 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); +extern char *drmGetRenderNameFromMasterFD(int fd); +extern char *drmGetMasterNameFromRenderFD(int fd); + #if defined(__cplusplus) || defined(c_plusplus) } #endif