From patchwork Mon Jun 25 17:36:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emil Velikov X-Patchwork-Id: 10487083 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 6CE5560230 for ; Mon, 25 Jun 2018 17:38:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 587E120182 for ; Mon, 25 Jun 2018 17:38:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4D86F22362; Mon, 25 Jun 2018 17:38:33 +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=-5.2 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM, MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED 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 0A57320182 for ; Mon, 25 Jun 2018 17:38:33 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 751556E36D; Mon, 25 Jun 2018 17:38:30 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-wr0-x241.google.com (mail-wr0-x241.google.com [IPv6:2a00:1450:400c:c0c::241]) by gabe.freedesktop.org (Postfix) with ESMTPS id D7D6A6E369 for ; Mon, 25 Jun 2018 17:38:28 +0000 (UTC) Received: by mail-wr0-x241.google.com with SMTP id k16-v6so14540235wro.0 for ; Mon, 25 Jun 2018 10:38:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=pL4cterXdYuhS8wDx67XObTNnMzLjoENRt2EJmHaFGs=; b=lnYHy8LOscXwnPMBxy6U4QL0FE+eCsmjxFeSPKD1WGYOxv+TK02yLGxoZK1F8+bP4f hxbxc+Sm6J52285Ofl7G23LurX0UgTxwpdZDRp3euvNsyka2lzFztjoNXrHL4PzyZoLG 25SA3LA7sXQgEGcmmTTyqOprrvu8RYv/mTYxMkRmpBXV0xzpQBSJ+lS0X9P6MabvStVp dZLNr6K4Q/5ONcbg6R8ky+DOKm/4PUIhADBxg9czCiW6tl+3Z+6P5zhltL4gO8TnVDrF L3wrVQZ8L9BhNzUp8szD/aI8H/LX0mON4AnpzufaZD3Tq5F44qUQsWAsN6/4Rs8gOWGB 2+ig== X-Gm-Message-State: APt69E20GUG51e+3NfngPp304BuFBSgIX/tEsvYcdy5T+3Qk56AnOLUh qonOApKvPmYKMZFZKWpp8+6s/A== X-Google-Smtp-Source: AAOMgpfPuhL2nZGOqTqg/rfqq+QS6nDnp5FjDg69ZY8u3eshVnQ0qnqipVEvB2vSgHuH8OAYVStgzw== X-Received: by 2002:adf:ea87:: with SMTP id s7-v6mr4594198wrm.150.1529948307355; Mon, 25 Jun 2018 10:38:27 -0700 (PDT) Received: from arch-x1c3.cbg.collabora.co.uk ([2a00:5f00:102:0:9665:9cff:feee:aa4d]) by smtp.gmail.com with ESMTPSA id p8-v6sm6581019wrq.70.2018.06.25.10.38.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Jun 2018 10:38:26 -0700 (PDT) From: Emil Velikov To: dri-devel@lists.freedesktop.org Subject: [PATCH libdrm 02/10] xf86drm: introduce drm_device_has_rdev() helper Date: Mon, 25 Jun 2018 18:36:18 +0100 Message-Id: <20180625173626.8046-2-emil.l.velikov@gmail.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180625173626.8046-1-emil.l.velikov@gmail.com> References: <20180625173626.8046-1-emil.l.velikov@gmail.com> X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: emil.l.velikov@gmail.com MIME-Version: 1.0 Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Emil Velikov Currently we match the opened drmDevice fd with each drmDevice we process. Move that after all the devices are processed and folded, via the drm_device_has_rdev(). This makes the code easier to follow and allows us to unify the massive process loop across drmGetDevice2 and drmGetDevices2. That in itself is coming with a later commit. Signed-off-by: Emil Velikov --- xf86drm.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/xf86drm.c b/xf86drm.c index e1bbbe99..cbc0a408 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -3705,6 +3705,21 @@ drm_device_validate_flags(uint32_t flags) return (flags & ~DRM_DEVICE_GET_PCI_REVISION); } +static bool +drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev) +{ + struct stat sbuf; + + for (int i = 0; i < DRM_NODE_MAX; i++) { + if (device->available_nodes & 1 << i) { + if (stat(device->nodes[i], &sbuf) == 0 && + sbuf.st_rdev == find_rdev) + return true; + } + } + return false; +} + /** * Get information about the opened drm device * @@ -3889,21 +3904,22 @@ int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) local_devices = temp; } - /* store target at local_devices[0] for ease to use below */ - if (find_rdev == sbuf.st_rdev && i) { - local_devices[i] = local_devices[0]; - local_devices[0] = d; - } - else - local_devices[i] = d; + local_devices[i] = d; i++; } node_count = i; drmFoldDuplicatedDevices(local_devices, node_count); - *device = local_devices[0]; - drmFreeDevices(&local_devices[1], node_count - 1); + for (i = 0; i < node_count; i++) { + if (!local_devices[i]) + continue; + + if (drm_device_has_rdev(local_devices[i], find_rdev)) + *device = local_devices[i]; + else + drmFreeDevice(&local_devices[i]); + } closedir(sysdir); free(local_devices);