From patchwork Thu Jun 2 08:55:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16134 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 BEEA2BD160 for ; Thu, 2 Jun 2022 08:56:06 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 1702665638; Thu, 2 Jun 2022 10:56:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654160166; bh=2Lnwh1BQO0z8DQF053R24ZL2BCsDYuYv0VMseletmj8=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=kYXOsPopsGE1gKs9mvtvXxlzpnl1nzV548SEYUJH2EoB9fvwIitHCOogLEKNwjNSr B3YjOGQzQUgjSg3Z6sTe3WumLmvTs90BcGj+R3OyF94Ol7Ww2CwxvxoF5GCwdGby5m IZgugJAr/4klzEhyxFKsgN3dr87ArC1mRO/u9lqYFHzJaO4EuCMpQ6fj+FFgQbOwqo C/Mgf41BByX9NiNPSyPYsb5TYNhTpfc51HLk1wpxC4Nydp80+p6rN8gGHkechw3vxC O1ir0DzKjocBgJFbD/CV7ZPchobSyf8xod/BbxbGaiUOqQ/tcCwOsmoyw+TgdWxl+a 0IXue/8rdEhwA== Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A7C4165632 for ; Thu, 2 Jun 2022 10:56:04 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="g/wANHav"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654160163; 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=EBunYZ86J1L10zUlRGAMD4SCoHnge2SSMW9zjMQdNK0=; b=g/wANHavJpOWKbw5qN6cL9zXLUXuVZNwNJ95FBLMVpzaHb/X7iax26Er0VZGpSd4wenstB G6YLL6mPuCuyWjXmvOo9+0N+WDQOcRa4tdfTNbApG5NsTIH53NOvB5XFSKhc+2pKMPfKaU crteTSIgZGh/iJ1b4DfaCPQ9dy8AAL8= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-543-AW3VN2LpM4ap5zv9V43CVA-1; Thu, 02 Jun 2022 04:56:02 -0400 X-MC-Unique: AW3VN2LpM4ap5zv9V43CVA-1 Received: by mail-wr1-f70.google.com with SMTP id e7-20020adfa747000000b0020fe61b0c62so640659wrd.22 for ; Thu, 02 Jun 2022 01:56:02 -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=EBunYZ86J1L10zUlRGAMD4SCoHnge2SSMW9zjMQdNK0=; b=Mtdo67Bu4YVx95Hb/h8K+KC0dDrGj70sGWJYrjYIIYD2+8A5PBPyhksY1ARwnLdsZw 5Xu8jVRJ2TbJOgQ80lkFe6v3epyW703QbUcjj6MKXXvGJV+kcRcLOZQYZ1Aw2Ejhwrno NxXrRKwmk+DNs9WeICZhTEkA2eb8yYp4tsyd65bU2A4+7UqTkujZGciiXBWakxDnSwmz /2ursoNEYy4NQv6Be8uK160yKPQWD9MXV8X/QuDCG4DGA7f+zjn9QonCRxq5ewkrcsqV +RO+L2pHqIoVlBdjkN6cD9Kh86s5p+PwMqDEdm7exttgErZb43DHDI1eOakCW3vomYna E1wQ== X-Gm-Message-State: AOAM533KUWaQbqDKE7WmlfAislNvfRU+HhiLeu0+IZYlWcCGR3enyOUv FWjw7ycZ6pVaINzhWq4U+Fn18mipJoazUhjjw36ED7T6mfNbZqOFX6al7fYmtPlmP4Jgq3Y4Fgd rpFk4IzpFsytInNRk+M5/cXgyHaouYaquBpz0B7iy5XCfjW0f28GzMdidy8MLT7d/+JT4iLqZB6 mk6XyAFdck X-Received: by 2002:a5d:4e48:0:b0:210:18bb:6aa1 with SMTP id r8-20020a5d4e48000000b0021018bb6aa1mr2825490wrt.62.1654160161020; Thu, 02 Jun 2022 01:56:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDf2Z6Z93ZJlpPCXS82EsUhOjjy+z0Lcw0dzG7oMCCEXyLdFb7JtJHS8/kGvym2dBO1OiUAw== X-Received: by 2002:a5d:4e48:0:b0:210:18bb:6aa1 with SMTP id r8-20020a5d4e48000000b0021018bb6aa1mr2825475wrt.62.1654160160785; Thu, 02 Jun 2022 01:56:00 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id m129-20020a1ca387000000b003948f4e750fsm7533135wme.23.2022.06.02.01.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 01:56:00 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Jun 2022 09:55:48 +0100 Message-Id: <20220602085548.13630-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 v2] 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 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 Signed-off-by: Eric Curtin --- 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, 36 insertions(+), 10 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 42c5a3b1..47d9d50c 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,13 @@ 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/"); + constexpr size_t PRE_NODE_NAME_MAX = sizeof("card"); + constexpr size_t POST_NODE_NAME_MAX = sizeof("255"); + constexpr size_t NODE_NAME_MAX = + DIR_NAME_MAX + PRE_NODE_NAME_MAX + POST_NODE_NAME_MAX - 2; + char name[NODE_NAME_MAX] = "/dev/dri/"; + int ret = 0; /* * Open the first DRM/KMS device. The libdrm drmOpen*() functions @@ -404,14 +409,35 @@ 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); + DIR *folder = opendir(name); + strncpy(name + DIR_NAME_MAX - 1, "card", PRE_NODE_NAME_MAX); + if (folder) { + for (struct dirent *res; (res = readdir(folder));) { + if (!strncmp(res->d_name, "card", 4)) { + strncpy(name + DIR_NAME_MAX + + PRE_NODE_NAME_MAX - 2, + res->d_name + PRE_NODE_NAME_MAX - 1, + POST_NODE_NAME_MAX); + fd_ = open(name, O_RDWR | O_CLOEXEC); + if (fd_ < 0) { + ret = -errno; + std::cerr + << "Failed to open DRM/KMS device " + << name << ": " + << strerror(-ret) << std::endl; + continue; + } + + break; + } + } + + closedir(folder); + } + if (fd_ < 0) { - ret = -errno; - std::cerr - << "Failed to open DRM/KMS device " << name << ": " - << strerror(-ret) << std::endl; - return ret; + std::cerr << "Unable to open any DRM/KMS device" << std::endl; + return ret ? ret : -ENOENT; } /*