From patchwork Thu Jun 16 21:45:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16255 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 631F5BD808 for ; Thu, 16 Jun 2022 21:50:51 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9D03E65635; Thu, 16 Jun 2022 23:50:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655416250; bh=XXisAqophaX41zQ3wKFENlm9rR98ixYqCcVtvGB1lYQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=e/TJ4IGhxQ2FroePKMHrh5o0NjvUbB0H2XLnE61fLY5ZWzAFJgaLMj2Ry8jZarMcI Rdyr88gIse5Hzu0R7/z+y56fkndARR//ygu6kLY7dlfk6HnG2m24RhuqX25khxQ9BV +0jjQCimNFSSDx/IZxywLmguULkqhs1QSmmdH4KKebVBMg7zOG7AOC6bXTCoh1nzXt XT9TRcjDCR8Fxnx1Nrj5IBecg2yOjxaEDkq2PZEXLpvbi3k20Gik0kHR32Ceh3xEdU YM+5MIhK7XR3iwLKmv4ghlsER3r5hHXsS9ArTvHakCQEobLNp89PCFS9a/+JnRytcl NzUhqip0os7lA== 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 356D8600F0 for ; Thu, 16 Jun 2022 23:50:49 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="YL61fQ2c"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655416248; 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=dYygmMS+INX72Q6FKkLNuh8UB1FFiQwARL9yH7EzZ2g=; b=YL61fQ2c/Wa+frheW4ujiFNzkblZ3pkzSDBYs85AMWCOjOSvVIuamGlk+6EhuM5e/JybqZ SM+ECx8/nriCAg/d+WmbXf0UDJAq7jPmvfEm6tmdRQOz0OGu+OdcdVggw/ugWISQKk2j7G Qv2q2PHS5eW8vZ4Di87iitYHJe8JTCQ= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-642-dl5OChZ1N_idHkZN9Z_6IA-1; Thu, 16 Jun 2022 17:50:45 -0400 X-MC-Unique: dl5OChZ1N_idHkZN9Z_6IA-1 Received: by mail-wm1-f72.google.com with SMTP id v125-20020a1cac83000000b0039c832fbd02so1706113wme.4 for ; Thu, 16 Jun 2022 14:50:44 -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=dYygmMS+INX72Q6FKkLNuh8UB1FFiQwARL9yH7EzZ2g=; b=4JMtYHWW6qQ3ghthN25V5N+Hn+HRm1e6TcEutvFJYOXNpGUePJvQWLHWA4mbLLXgGn g5TB2/J/BkTYdJ4SzzSf22T+pdqyd+rMNLJTW/9mU8/K74uQQPa1CE0zUR9MxpqH6yTa Vt3WPCURcf7uVUpDqNgEpNBgXTsrDCgSauEabwNnYAfuxNQU6/D/iLQH7928etAwrrZ0 A1Jp9IZI05WcbfcYKm55/8AiwCYXERHOXP2BVv11fPUmS1e1VdQOgifyIzdsTRraaB3X /IMdZnLvE3cziG/aB4EmxzatNLCCbJIz1ZrUupyqmrdSKxiw+Ye+SnXEj7TfTVcBncKM qXmA== X-Gm-Message-State: AJIora8Q7xw+DZ22EqyzeNGzCUcd5vooAgy3jrxMRob/QB5RpZmZv0ku 6gRiSOUldFKY752u+KjFkCCiEeKq+Xg2d9qyRUopfVb+JoSzVLs52GDn3/76FfL/HBkQjWSRaGM SWLkAJs1DDdeQW6EjnE6vpBXeGfAfNMsHbLClbUBqiQHgH9TJDBRBMYCQGLnemxh8eOlKGKcjBc EM9BVvswcg X-Received: by 2002:a5d:6887:0:b0:21a:33c1:eea7 with SMTP id h7-20020a5d6887000000b0021a33c1eea7mr5039150wru.49.1655416243571; Thu, 16 Jun 2022 14:50:43 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vCr9SS9AWIBuQVmAwczrkP+yBKGxdHDyAQiBqU5Mrz4mY8careDAyEkID4E/INeN5khiKfvA== X-Received: by 2002:a5d:6887:0:b0:21a:33c1:eea7 with SMTP id h7-20020a5d6887000000b0021a33c1eea7mr5039131wru.49.1655416243299; Thu, 16 Jun 2022 14:50:43 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id l3-20020a5d5263000000b0020ff7246934sm2743998wrc.95.2022.06.16.14.50.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jun 2022 14:50:42 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 16 Jun 2022 22:45:52 +0100 Message-Id: <20220616214551.43630-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 v5] 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 v5: - Split into openCard function - ret initialized to -ENOENT, in case directory is open with no card* - Add another string to simplify 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 | 62 ++++++++++++++++++++++++++++++++++++------------- src/cam/drm.h | 2 +- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 42c5a3b1..39c19aa8 100644 --- a/src/cam/drm.cpp +++ b/src/cam/drm.cpp @@ -8,6 +8,7 @@ #include "drm.h" #include +#include #include #include #include @@ -391,26 +392,55 @@ Device::~Device() drmClose(fd_); } -int Device::init() +int Device::openCard() { - constexpr size_t NODE_NAME_MAX = sizeof("/dev/dri/card255"); - char name[NODE_NAME_MAX]; - int ret; + const std::string dirName = "/dev/dri/"; + int ret = -ENOENT; /* - * Open the first DRM/KMS device. The libdrm drmOpen*() functions - * require either a module name or a bus ID, which we don't have, so - * bypass them. The automatic module loading and device node creation - * 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) { + * Open the first DRM/KMS device beginning with /dev/dri/card. The + * libdrm drmOpen*() functions require either a module name or a bus ID, + * which we don't have, so bypass them. The automatic module loading and + * device node creation from drmOpen() is of no practical use as any + * modern system will handle that through udev or an equivalent + * component. + */ + DIR *folder = opendir(dirName.c_str()); + if (!folder) { ret = -errno; - std::cerr - << "Failed to open DRM/KMS device " << name << ": " - << strerror(-ret) << std::endl; + std::cerr << "Failed to open " << dirName + << " directory: " << strerror(-ret) << std::endl; + return ret; + } + + for (struct dirent *res; (res = readdir(folder));) { + if (strncmp(res->d_name, "card", 4)) { + continue; + } + + const std::string devName = dirName + res->d_name; + fd_ = open(devName.c_str(), O_RDWR | O_CLOEXEC); + if (fd_ >= 0) { + ret = 0; + break; + } + + ret = -errno; + std::cerr << "Failed to open DRM/KMS device " << devName << ": " + << strerror(-ret) << std::endl; + } + + closedir(folder); + + return ret; +} + +int Device::init() +{ + int ret = openCard(); + if (ret < 0) { + std::cerr << "Failed to open any DRM/KMS device: " + << strerror(-ret) << std::endl; return ret; } diff --git a/src/cam/drm.h b/src/cam/drm.h index de57e445..1817d053 100644 --- a/src/cam/drm.h +++ b/src/cam/drm.h @@ -312,7 +312,7 @@ private: Device &operator=(const Device &&) = delete; int getResources(); - + int openCard(); void drmEvent(); static void pageFlipComplete(int fd, unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec,