From patchwork Mon Jun 6 14:27:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16178 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id C2946BD160 for ; Mon, 6 Jun 2022 14:37:24 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 156B96559A; Mon, 6 Jun 2022 16:37:24 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654526244; bh=sWl4DGzQEQnl1S51AcMjzWRVRieIHU4KR80MPi7uYmA=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=cBIvOOek4GL1zVcpD4kMH3wttpahsgv/Cx0akHQpOiUHqyHFvLdlNCommvjzbLu0g sw/orxUUXb8xyz2+EOy0N2hkXDjbxHmbFvfZpwYr/NIJNeDVx1p5JjNh+jIUNuNUU2 XPutqOEGP6QG1yuPfh6u6y73UEnzBGV7HJETG6HR83gUR5/jY5WvvC+HHttdR4xnMc CrHoBccXeKw/TvTV7QrQ5VbX2h/cAQSEBMVJAzPswqN07GIdI3dmpYKl1YSqGriRZ+ lfjdBfj/l+mNpBU8tyzi8WHxUISf07cKVBtWWArwOr9VQ4YxwFihsQ8ksmXNieBfSe KaMwGgFm/NBpQ== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 01891633A7 for ; Mon, 6 Jun 2022 16:37:21 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="Lk5uq8IF"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654526240; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DEYiMRrUYvHvMPOK9LrW7DcjnyOcBu7oUMDbhYTaMgs=; b=Lk5uq8IFTodrKJ29DEj6IZe2zjqBARSYpWvPjGEflyYuWfU3T5MXtWNVE7VtYI8mAyxLi9 ifEwsrwqZOKyNM40KVlkBfJ1enQUZd6npafOi5lPfABhOCJc/HfwiWOYBiQ6OaszBF6sTB LL5aPYVyKIkM4+F4oElA2cA49l7dGn8= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-546-5Z3qzFWaPH2HfqRC3RMaNQ-1; Mon, 06 Jun 2022 10:37:19 -0400 X-MC-Unique: 5Z3qzFWaPH2HfqRC3RMaNQ-1 Received: by mail-wm1-f69.google.com with SMTP id k5-20020a05600c1c8500b003974c5d636dso7807069wms.1 for ; Mon, 06 Jun 2022 07:37:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=DEYiMRrUYvHvMPOK9LrW7DcjnyOcBu7oUMDbhYTaMgs=; b=rWVSYD/F4Jtj/1VOh3cU4y97GkpUkEtdEaAy9fwXCqPwrTk52xFUEGmHsjQ6HFHzz3 vNSxc2zKHFqqFCBRQJ+I65or/n1K+dtJtHc+S4sRVq+o5uqlu7+DR0A/wwm8lRmWCdwF O0v4wixhntOE2Om21EGGYJCaUIee2nx3+nqFXv3BhBJ37YfWI0b9rZmf6VzbGqqKzwty AKFGF3SweMJfjenxwUjLBHFC7z6brBV75J9ivnZU1PjXHUXiSe4VHgrWK7usvEQcWB35 tqRJ52bGcuOFj3BANYRg4DVcDfvpOP7ZbIH0ESrAU3NN9fYsU/dp0iPCxnItHhvP53/J dEfA== X-Gm-Message-State: AOAM532AUqQLFbmNVf0BlKntlOETOfiW+Z6SRwrbW3frfQwD4QbqTSB9 21v8nX50Xwiyzk5AZb8iPWt10U5SujhVh/GFG/jF8s3GfOzYiKz89z7xNZvae7pxG4pPBx/mrkr /z4wfirv/GOT+9ogLy+zVxAiPOQZ2qlzo03a2CeuuNall7oAH4UPGGqvcLv3wi8zPG7fi0O58iH DCm5KKK2yk X-Received: by 2002:a5d:598e:0:b0:20f:c2fd:de44 with SMTP id n14-20020a5d598e000000b0020fc2fdde44mr21991994wri.178.1654526237984; Mon, 06 Jun 2022 07:37:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdNl2xYRmbj8JUOwHwhGj4jfduyVPQJYHGo1/3BaM/kLMaYgI/2xbtyaUOWG8aniKc2XOvtg== X-Received: by 2002:a5d:598e:0:b0:20f:c2fd:de44 with SMTP id n14-20020a5d598e000000b0020fc2fdde44mr21991940wri.178.1654526237509; Mon, 06 Jun 2022 07:37:17 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id f6-20020a05600c154600b00397402ae674sm23454274wmg.11.2022.06.06.07.37.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jun 2022 07:37:17 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Mon, 6 Jun 2022 15:27:45 +0100 Message-Id: <20220606142743.10104-1-ecurtin@redhat.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=ecurtin@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [libcamera-devel] [PATCH v4] cam: drm: Support /dev/dri cards other than 0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Eric Curtin via libcamera-devel From: Eric Curtin Reply-To: Eric Curtin Cc: Ian Mullins , Javier Martinez Canillas Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Existing code is hardcoded to card0. Since recent fedora upgrades, we have noticed on more than one machine that card1 is present as the lowest numbered device, could theoretically be higher. This technique tries every file starting with card and continue only when we have successfully opened one. These devices with card1 as the lowest device were simply failing when they do not see a /dev/dri/card0 file present. Reported-by: Ian Mullins Tested-by: Ian Mullins Signed-off-by: Eric Curtin --- Changes in v4: - added Tested-by tag - std::stringified things - changed if conditions to reduce indentations - constexpr sizes now don't include null terminator - just return -ENOENT if no device was successfully opened. Changes in v3: - switch back to memcpy, strncpy causing false compiler issue Changes in v2: - return if no valid card found, or directory could not be opened etc. - memcpy to strncpy for safety - only adjust the numerical bytes for each iteration of loop since /dev/dri/card won't change - initialize ret to zero --- src/cam/drm.cpp | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 42c5a3b1..d05b7f5f 100644 --- a/src/cam/drm.cpp +++ b/src/cam/drm.cpp @@ -8,6 +8,7 @@ #include "drm.h" #include +#include #include #include #include @@ -393,9 +394,15 @@ Device::~Device() int Device::init() { - constexpr size_t NODE_NAME_MAX = sizeof("/dev/dri/card255"); - char name[NODE_NAME_MAX]; - int ret; + constexpr size_t DIR_NAME_MAX = sizeof("/dev/dri/") - 1; + constexpr size_t PRE_NODE_NAME_MAX = sizeof("card") - 1; + constexpr size_t POST_NODE_NAME_MAX = sizeof("255") - 1; + constexpr size_t NODE_NAME_MAX = + DIR_NAME_MAX + PRE_NODE_NAME_MAX + POST_NODE_NAME_MAX; + std::string name; + name.reserve(NODE_NAME_MAX); + name = "/dev/dri/"; + int ret = 0; /* * Open the first DRM/KMS device. The libdrm drmOpen*() functions @@ -404,16 +411,37 @@ int Device::init() * from drmOpen() is of no practical use as any modern system will * handle that through udev or an equivalent component. */ - snprintf(name, sizeof(name), "/dev/dri/card%u", 0); - fd_ = open(name, O_RDWR | O_CLOEXEC); - if (fd_ < 0) { + DIR *folder = opendir(name.c_str()); + if (!folder) { ret = -errno; - std::cerr - << "Failed to open DRM/KMS device " << name << ": " - << strerror(-ret) << std::endl; + std::cerr << "Failed to open " << name + << " directory: " << strerror(-ret) << std::endl; return ret; } + name += "card"; + for (struct dirent *res; (res = readdir(folder));) { + if (!strncmp(res->d_name, "card", 4)) { + name += res->d_name + PRE_NODE_NAME_MAX; + fd_ = open(name.c_str(), O_RDWR | O_CLOEXEC); + if (fd_ >= 0) { + break; + } + + ret = -errno; + std::cerr << "Failed to open DRM/KMS device " << name + << ": " << strerror(-ret) << std::endl; + name.resize(DIR_NAME_MAX + PRE_NODE_NAME_MAX); + } + } + + closedir(folder); + + if (fd_ < 0) { + std::cerr << "Failed to open any DRM/KMS device" << std::endl; + return -ENOENT; + } + /* * Enable the atomic APIs. This also automatically enables the * universal planes API.