From patchwork Thu Jun 2 10:00:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Curtin X-Patchwork-Id: 16135 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 D713BBD160 for ; Thu, 2 Jun 2022 10:00:29 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 719BB65635; Thu, 2 Jun 2022 12:00:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1654164029; bh=ORhX90oO6yHDqotqCkeXZCAj0s7D7SN8zoPSChOy3fA=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=hRxxOCyP4UhU0s6/HVeqHKqNE9YLFabCWYbBqr/H8+yvRitGbIpsQCTSE+a02QQXc 5GrfrJiGYTwm2qS96GrOKL/niIGAg+bYlcpyfgaqAOV4g80bSEvRUcKv5N9MOY35Je NoQry9i/979QN2MfC2rIXkGT6FQ0CTKI9DuLdUiqP8l2iaYzxdNC0FqYSTBGov8k4h 7wJlp0t4wBdlYzGo5RZKiNvYMDB5yYKI6Z9NS64Gcqk4uYxZeZNAZR5X0krCXO9Ako PPKayMllBS8BDDFXOpzOjFsYbvNCgXs2rtVDIM3rm5PRE/3AZbMcH2PCsx2GMnmLvp vvOvlgtWROSvA== 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 88E02633A7 for ; Thu, 2 Jun 2022 12:00:27 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="GApJglPo"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1654164026; 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=kwMK0gDjxJBGBavll6W7NXHanlMiYF6Z5+Ii/2fUkAM=; b=GApJglPo8RDWp0g/kYVm78ogttPEXgBQAq9BYlH1ZM80zIGoFK7Con69ulw0BLAfEa30uG f/ev2yL9utbpwMeMyTm/IjT1SUXyFAJlzEMeWeLmz5R+Qdoo2jaCsclcx8gSUOiFcND978 bE7WJRVMKdI9LPAcwDG7+cyBqMUq9ck= 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-212-la6_YWNVNXmH_aQ2ry5PIQ-1; Thu, 02 Jun 2022 06:00:25 -0400 X-MC-Unique: la6_YWNVNXmH_aQ2ry5PIQ-1 Received: by mail-wm1-f69.google.com with SMTP id i1-20020a05600c354100b003976fc71579so2579818wmq.8 for ; Thu, 02 Jun 2022 03:00:25 -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=kwMK0gDjxJBGBavll6W7NXHanlMiYF6Z5+Ii/2fUkAM=; b=wuxAVsm3LGyIC0XgzbZfskbGWlpKKBQRFqgwrLWir0x0CgRg+hB/K52YGx3J5obGjY H7pOJD75KtRhU6oik9Kovihi2nSvFC3d+L5FpC5fZqOYxotSZWsqM6T82xtuelyqijIr IFpTzEclpVvPzMLzuNyEc5cDCRHG2Njm+JQvzdDvqNAgDIAqVVh42UFsl+s05a1BO0+c vBuWz8pwjprkEjUtdS4E4Qg7lUODwTF9032dFN0i12IUurhl2L03yPzbguCfPjCG4T3W ke0++UqRPwQf8t5OWLJHa2LVA3Z010ha5bZt9jBF3Pi3htaqJLkPgH6YxJnk5A4LOxPo mtzw== X-Gm-Message-State: AOAM531ASprO/wQ93C0kJNYSrlKQTM4BmNDZvG3fD26p+YaSLUwSfwsF XfXaGD3eET8XBW5r2jm3YGL5mRqY6p1Ca4xmO5kcAycADRk9tMCYh2zW1SHjZse1V13PBOPq3pK 7l3bB5xL+G70Kp4ZeB5bBc7gWBxRLVzdm+CC/yFPacrxBteJUP6feTECL276+z7tigpAz7o8sLa EdZVqZPUjx X-Received: by 2002:a7b:c4ca:0:b0:397:3bac:9b2a with SMTP id g10-20020a7bc4ca000000b003973bac9b2amr32102324wmk.154.1654164024163; Thu, 02 Jun 2022 03:00:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2q7oWC04x69LDoKsKDh5tLxrSKrb2EWgBU7saOy6JAQnhYJaZRTYbiwoLQCuLANFpfd1PYA== X-Received: by 2002:a7b:c4ca:0:b0:397:3bac:9b2a with SMTP id g10-20020a7bc4ca000000b003973bac9b2amr32102293wmk.154.1654164023830; Thu, 02 Jun 2022 03:00:23 -0700 (PDT) Received: from p1.Home ([2001:8a0:6724:4500:a69c:e66f:828e:b340]) by smtp.gmail.com with ESMTPSA id i6-20020adffc06000000b0020c5253d8dasm3817399wrr.38.2022.06.02.03.00.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jun 2022 03:00:23 -0700 (PDT) To: libcamera-devel@lists.libcamera.org Date: Thu, 2 Jun 2022 11:00:17 +0100 Message-Id: <20220602100017.22272-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 v3] 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 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, 36 insertions(+), 10 deletions(-) diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp index 42c5a3b1..7975a502 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); + memcpy(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)) { + memcpy(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; } /*