From patchwork Fri Jun 17 10:00:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16264 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 371A7BD808 for ; Fri, 17 Jun 2022 10:02:46 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 4DE0965635; Fri, 17 Jun 2022 12:02:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1655460165; bh=FkYc3rHd2SEJ/8ybFh47CTVIH4c8D8z1W7YUxKGyMKg=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=bFlOkBaZBo7NgiMFLHVdPT8XD5TlLiJXycMxXXEibrb2wbGNuhZuxyCVHunLrKv/W rVPRzeXuf84LWJLq2nb1K/Tecg/jSvQRn799syyTvp31FhC3nJBMXf1quuPGuJOy8E Hl6exclzpxw5zfL4MuQ0EEDP/Br35NYQYYZx056A+usRnYuCCA1tvWZP1wxWWzgke+ vKUZbGBrOE9Jdng8ENasXT55G0urxW0ioKEwDACQg5xDCy2sqnOW+wpkeRyPf7Ap9e eYKypPvf4eGV37YycMP5Jk09im4V4G3PF+j6IBVDyWc3fIBAoSTUbh1Pfv2E3DfHxX /dINq9N7y6OnQ== 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 E6B5365632 for ; Fri, 17 Jun 2022 12:02:43 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="aCE5V0dU"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655460162; 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=VpmpGN1K62idhWJk67mFQyaFLQhc85Gh2DFui/OlZJU=; b=aCE5V0dUk0V9HX4OZHoS3J0LMAsiJ6VZuXu2ySDqTtVQW/iD4agoSkkMK/OAqRdJyviGDl IdC9qxgT/cvyHOZJCBLFrVGDKU/8RQ7uwrmZwaBSaCZ2lOLkAeqJEhSJEb9OvQXYJb6WFx slStPG2h5rBctqwuIme6wysb6TxuDbg= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-284-OT12oY8xNa65JgyOFl18uw-1; Fri, 17 Jun 2022 06:02:41 -0400 X-MC-Unique: OT12oY8xNa65JgyOFl18uw-1 Received: by mail-wm1-f70.google.com with SMTP id c187-20020a1c35c4000000b003970013833aso1412980wma.1 for ; Fri, 17 Jun 2022 03:02:41 -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=VpmpGN1K62idhWJk67mFQyaFLQhc85Gh2DFui/OlZJU=; b=2dTEbu/YY5K6HyPthxcKBOv+2pQ00iDDZGkU81EMGunVl6e+c8kLALbQqsBemiauuj PgyA5vbjQAlS9qCfv4zoisbIouUyUYcVQPRlakfkAYVZ8bnnSDyO1n5DMRcR1ySNx3Z/ vzPudBDeIjgr0uQk4+cI4TndTQMj/nD4hPlBHwfyAAssp5+K6lFwEaGFnrENNPaqDmzN ORzNDpmrXQZU558KFp3341D4ut6gw6KuLupF06yjn5mWw0ADHGwRTx+O50k5tX6jvMRi f5BnWhxMoetNiF7hx+IJ3WfQ5jzeh4NbKY2a4FAfb8kq3x4VRUdkTf3pW/dDIfO/L69g mOzQ== X-Gm-Message-State: AJIora/if3C++bFWpfnMiJlWYnTqnO+oHO5xk1SUv+aIKYDxQqI/Zu7z Kd1KWPmo8tx5edlEJRQXQmsP5PdhhtjCGCjtFQnwz9umIXnhLsePrD8sdlEekJe4V1rxWKnSZiQ mMKUh59meLkIEGgCuUCAPwVhBG5K+JJ8YEPVz5pZ8AEE3Qjh41YdzOk7ip7wwRpQmJyNUSKnMQ9 BcN6v7qnFN X-Received: by 2002:adf:db8e:0:b0:21b:4e5b:a438 with SMTP id u14-20020adfdb8e000000b0021b4e5ba438mr1711454wri.247.1655460160077; Fri, 17 Jun 2022 03:02:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vtvoyQTS2OU4xM10CIC+WIKJw6ByWwLF76x+cc16JbEnFJ+TW7CD6pqvXgjsaOEfh5OzUpNA== X-Received: by 2002:adf:db8e:0:b0:21b:4e5b:a438 with SMTP id u14-20020adfdb8e000000b0021b4e5ba438mr1711432wri.247.1655460159743; Fri, 17 Jun 2022 03:02:39 -0700 (PDT) Received: from p1.Home (bl4-23-148.dsl.telepac.pt. [81.193.23.148]) by smtp.gmail.com with ESMTPSA id r64-20020a1c4443000000b003942a244f39sm10604166wma.18.2022.06.17.03.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jun 2022 03:02:39 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Fri, 17 Jun 2022 11:00:44 +0100 Message-Id: <20220617100043.8753-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 v6] 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 v6: - Spaces to tabs - Remove a brace for single line if statement 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 | 59 ++++++++++++++++++++++++++++++++++++------------- src/cam/drm.h | 2 +- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 42c5a3b1..41fd042b 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,54 @@ 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. + * 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. */ - snprintf(name, sizeof(name), "/dev/dri/card%u", 0); - fd_ = open(name, O_RDWR | O_CLOEXEC); - if (fd_ < 0) { + 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,