From patchwork Thu Nov 10 17:43:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 9421861 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F3EA060512 for ; Thu, 10 Nov 2016 17:53:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD9BC29783 for ; Thu, 10 Nov 2016 17:53:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D1DB4297EB; Thu, 10 Nov 2016 17:53:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BD5A129783 for ; Thu, 10 Nov 2016 17:53:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id A61B16E7C6; Thu, 10 Nov 2016 17:53:44 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wm0-x241.google.com (mail-wm0-x241.google.com [IPv6:2a00:1450:400c:c09::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4D2A96E7C6 for ; Thu, 10 Nov 2016 17:53:43 +0000 (UTC) Received: by mail-wm0-x241.google.com with SMTP id g23so4623596wme.1 for ; Thu, 10 Nov 2016 09:53:43 -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; bh=OdPpRZL0IgzRTlv9BIjAnlzkYj97zkjsH3Ui+ezDi/g=; b=tcSGVxLU9yZvz6HaIIK2bR0fwUjNFVD15W34tsxB/zv+UgccIkLXXYZ/za5NDJrU4Y Zy4YL9je3oUS03xql363nyqJK276uLwh9Yw9Xg/u8tgwL7u+peVi0sDmWd7gcLo/DAK4 E48oknkqcuQZTNyfYqnzo3qhTSIQ493hyS1urk2/gx2mlK+QrYWtbMR4J5HR6e9AaN0J vABN5Jjg0g2YsTdvOkMQmFPHUjveo3vtdDbj1EZa0Fpc0urIbqfLCvw+ehBKlPD5Y/cC bGdUYFjc+NgVBaO54evOIEZtUis/n/s7bPrtGzfB7qWN+vDFOMPbtIsQ8dVy8VFb3okx S97A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=OdPpRZL0IgzRTlv9BIjAnlzkYj97zkjsH3Ui+ezDi/g=; b=k8djA53jjvxMdaXtKqHxJehX2Gz2so3E1C6U0Jnk0QOzNf65lBZ8iFfh6acXdYI4og DzXwiCKH764mhwLR7gGXnz97+c3u07whqt/KU3kNgiLIwY01Iy344+0pwLLDuo3ytoaU 1XoAZlfqLoZx921A5DuTbcYxoKKOTmuKYfyGFNIhRKBNK0EPdarXaBY5V5op5t7tkxC+ ttDsRn2r2GUpDd/TmtWAapDGOQ8ACuTTkyF0yDpr5G8pdnoS2J1XSCe49SW0nAV8DKaG TlJ6MkF8/8Zv3ckIqC/4u8Gk00WJHD23JZNzdQoWbqAahypaTQpbVs2y6eEnF9O4F1yP TyoQ== X-Gm-Message-State: ABUngvdK3eDJqlQ0WSM9Msfk4rSw/P2CbilOeEgj0eLFIDb3s+nRxfIfi9ydMSR0V+yieA== X-Received: by 10.28.229.72 with SMTP id c69mr7476223wmh.110.1478800421691; Thu, 10 Nov 2016 09:53:41 -0800 (PST) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:1098:5:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id s204sm7461438wmd.1.2016.11.10.09.53.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Nov 2016 09:53:40 -0800 (PST) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm] xf86drm: introduce drmGetDeviceNameFromFd2 Date: Thu, 10 Nov 2016 17:43:05 +0000 Message-Id: <20161110174305.31525-1-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.10.2 Cc: emil.l.velikov@gmail.com 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-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov The original version considered only card devices, while this will pick the device/node name regardless - card, control, renderD, other... Current implementation is "linux" specific, in such that it relies on sysfs/uevent file. At the same time this gives us the flexibility to support any nodes even future ones, as long as they're within DRM_MAJOR. Shamelessly copied from mesa, latter by: Gary Wong Signed-off-by: Emil Velikov Reviewed-by: Nicolai Hähnle --- There's quite a bit of [subtle chaos] in mesa about this. Sometimes we explicitly request the render node, sometimes we use an open-coded version of drmGetDeviceNameFromFd, which due to sheer luck just works. One could apply some polish here, but I'd choose to keep things as close to original. --- xf86drm.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ xf86drm.h | 5 +++++ 2 files changed, 56 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index 80e2f27..0bca43a 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3367,3 +3367,54 @@ free_locals: free(local_devices); return ret; } + +char *drmGetDeviceNameFromFd2(int fd) +{ +#ifdef __linux__ + struct stat sbuf; + char *device_name = NULL; + unsigned int maj, min; + FILE *f; + char buf[512]; + static const char match[9] = "\nDEVNAME="; + int expected = 1; + + + 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/uevent", maj, min); + if (!(f = fopen(buf, "r"))) + return NULL; + + while (expected < sizeof(match)) { + int c = getc(f); + + if (c == EOF) { + fclose(f); + return NULL; + } else if (c == match[expected] ) + expected++; + else + expected = 0; + } + + strcpy(buf, "/dev/"); + if (fgets(buf + 5, sizeof(buf) - 5, f)) { + buf[strcspn(buf, "\n")] = '\0'; + device_name = strdup(buf); + } + + fclose(f); + return device_name; +#else +#warning "Missing implementation of drmGetDeviceNameFromFd2" + return NULL; +#endif +} diff --git a/xf86drm.h b/xf86drm.h index d1ebc32..4fa395e 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -753,6 +753,11 @@ typedef struct _drmEventContext { extern int drmHandleEvent(int fd, drmEventContextPtr evctx); extern char *drmGetDeviceNameFromFd(int fd); + +/* Improved version of drmGetDeviceNameFromFd which attributes for any type of + * device/node - card, control or renderD. + */ +extern char *drmGetDeviceNameFromFd2(int fd); extern int drmGetNodeTypeFromFd(int fd); extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);