From patchwork Fri Feb 13 10:51:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Binns X-Patchwork-Id: 5824081 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 8A435BF440 for ; Fri, 13 Feb 2015 10:51:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id C749820219 for ; Fri, 13 Feb 2015 10:51:29 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id F1E192020E for ; Fri, 13 Feb 2015 10:51:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 742936E494; Fri, 13 Feb 2015 02:51:28 -0800 (PST) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mailapp01.imgtec.com (mailapp01.imgtec.com [195.59.15.196]) by gabe.freedesktop.org (Postfix) with ESMTP id 9469A6E494 for ; Fri, 13 Feb 2015 02:51:26 -0800 (PST) Received: from KLMAIL01.kl.imgtec.org (unknown [192.168.5.35]) by Websense Email Security Gateway with ESMTPS id F113C5DE27836; Fri, 13 Feb 2015 10:51:22 +0000 (GMT) Received: from frankbinns-dev.kl.imgtec.org (192.168.169.99) by KLMAIL01.kl.imgtec.org (192.168.5.35) with Microsoft SMTP Server (TLS) id 14.3.195.1; Fri, 13 Feb 2015 10:51:25 +0000 From: Frank Binns To: , Subject: [PATCH libdrm] Add new drmGetNodeTypeFromFd function Date: Fri, 13 Feb 2015 10:51:15 +0000 Message-ID: <1423824675-12460-1-git-send-email-frank.binns@imgtec.com> X-Mailer: git-send-email 1.8.5.4.gfdaaaa2 MIME-Version: 1.0 X-Originating-IP: [192.168.169.99] Cc: rufus.hamade@imgtec.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: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, 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 Add a helper function that returns the type of device node from an fd. Signed-off-by: Frank Binns Reviewed-by: Emil Velikov --- xf86drm.c | 39 +++++++++++++++++++++++++++++++++++++++ xf86drm.h | 1 + 2 files changed, 40 insertions(+) diff --git a/xf86drm.c b/xf86drm.c index d85115c..e117bc6 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -505,6 +505,23 @@ static int drmGetMinorBase(int type) }; } +static int drmGetMinorType(int minor) +{ + int type = minor >> 6; + + if (minor < 0) + return -1; + + switch (type) { + case DRM_NODE_PRIMARY: + case DRM_NODE_CONTROL: + case DRM_NODE_RENDER: + return type; + default: + return -1; + } +} + /** * Open the device by bus ID. * @@ -2667,6 +2684,28 @@ char *drmGetDeviceNameFromFd(int fd) return strdup(name); } +int drmGetNodeTypeFromFd(int fd) +{ + struct stat sbuf; + int maj, min, type; + + if (fstat(fd, &sbuf)) + return -1; + + maj = major(sbuf.st_rdev); + min = minor(sbuf.st_rdev); + + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) { + errno = EINVAL; + return -1; + } + + type = drmGetMinorType(min); + if (type == -1) + errno = ENODEV; + return type; +} + 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 77937eb..afd38a1 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -744,6 +744,7 @@ typedef struct _drmEventContext { extern int drmHandleEvent(int fd, drmEventContextPtr evctx); extern char *drmGetDeviceNameFromFd(int fd); +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);