From patchwork Sat Mar 7 00:58:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 5958211 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BC39ABF440 for ; Sat, 7 Mar 2015 00:55:41 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D813D2038F for ; Sat, 7 Mar 2015 00:55:40 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 8C1092037E for ; Sat, 7 Mar 2015 00:55:39 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 561616E023; Fri, 6 Mar 2015 16:55:37 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wg0-f44.google.com (mail-wg0-f44.google.com [74.125.82.44]) by gabe.freedesktop.org (Postfix) with ESMTP id 643D06E023 for ; Fri, 6 Mar 2015 16:55:36 -0800 (PST) Received: by wghl18 with SMTP id l18so16106298wgh.5 for ; Fri, 06 Mar 2015 16:55:35 -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=11P5IaDtqK7oBPx4+93hzB0uwhHNe+eJZycvRz6jYSE=; b=WpOZ5WcRancHp4e8/DvGMQ4ALoZ5bHTxykyzuyLqIILUmU9mxWVaBO27RZxL+c57ug nOAIj7BPdAKiAyvtr7IZ7a364b3YENTvHopQoIfY8wPNix+JzcfBYtf1tE9PCbtMLUlf cjS3VX6cgTFcnGnNRE/F8AHI7JjkShSlF8VwzHh12uHDp+8rLCOFQK0q6feU13bZQ87H pqWzIGokahPe5EFXIkLEakXiIHyaR9380R4TonbPOek41YYnwVRS59m+wY8BC6nvBmr4 +rQAdPkCOQW1OCOr4HLgsNeINV64o1VxdcP5o0I/9/KSASzbXnWS2/OaXiOyyjhqkOjJ +Y3w== X-Received: by 10.180.91.79 with SMTP id cc15mr70840535wib.37.1425689735319; Fri, 06 Mar 2015 16:55:35 -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 hs7sm4492920wib.4.2015.03.06.16.55.31 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 06 Mar 2015 16:55:33 -0800 (PST) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm v3] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Date: Sat, 7 Mar 2015 00:58:39 +0000 Message-Id: <1425689919-31409-1-git-send-email-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.3.1 In-Reply-To: <1424694176-3210-1-git-send-email-emil.l.velikov@gmail.com> References: <1424694176-3210-1-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. v3: - Don't segfault if name is NULL. - Update function names, as suggested by Frank Binns. Cc: Frank Binns Cc: Daniel Vetter Cc: David Herrmann Signed-off-by: Emil Velikov Reviewed-by: Frank Binns --- Hi all, I'm planning to push this Monday afternoon. If you have any comments or suggestions please go ahead. Cheers, Emil xf86drm.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xf86drm.h | 3 +++ 2 files changed, 87 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index e117bc6..194cd35 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,71 @@ 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); + int len; + char dev_name[64], buf[64]; + long name_max; + int maj, min; + + if (!name) + return NULL; + + len = strlen(name); + + 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 *drmGetPrimaryDeviceNameFromFd(int fd) +{ + return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY); +} + +char *drmGetRenderDeviceNameFromFd(int fd) +{ + return drmGetMinorNameForFD(fd, DRM_NODE_RENDER); +} diff --git a/xf86drm.h b/xf86drm.h index afd38a1..40c55c9 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 *drmGetPrimaryDeviceNameFromFd(int fd); +extern char *drmGetRenderDeviceNameFromFd(int fd); + #if defined(__cplusplus) || defined(c_plusplus) } #endif