[{"id":25160,"web_url":"https://patchwork.libcamera.org/comment/25160/","msgid":"<CAOgh=Fw1Osjc6uN=h4tPcZr_eGEab36fXAyuTityeeMs+BTykQ@mail.gmail.com>","date":"2022-09-28T11:26:24","subject":"Re: [libcamera-devel] [PATCH v2] cam: drm: Skip DRM devices not\n\tcapable of mode setting","submitter":{"id":101,"url":"https://patchwork.libcamera.org/api/people/101/","name":"Eric Curtin","email":"ecurtin@redhat.com"},"content":"On Wed, 28 Sept 2022 at 11:27, Laurent Pinchart\n<laurent.pinchart@ideasonboard.com> wrote:\n>\n> The DRM helper picks the first DRM card that it can open. On platforms\n> that have a standalone GPU, this risks selecting a device corresponding\n> to the GPU instead of the display controller. Fix this by skipping\n> devices that don't support the KMS mode setting API. Some legacy display\n> controllers would be skipped as well, but libcamera doesn't run on those\n> systems anyway.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n> Changes since v1:\n>\n> - Use DRM_CAP_DUMB_BUFFER instead of DRM_CLIENT_CAP_ATOMIC\n> ---\n>  src/cam/drm.cpp | 35 +++++++++++++++++++++++++++--------\n>  1 file changed, 27 insertions(+), 8 deletions(-)\n>\n> diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp\n> index b0602c942853..3bb950fd157a 100644\n> --- a/src/cam/drm.cpp\n> +++ b/src/cam/drm.cpp\n> @@ -430,7 +430,8 @@ int Device::init()\n>  int Device::openCard()\n>  {\n>         const std::string dirName = \"/dev/dri/\";\n> -       int ret = -ENOENT;\n> +       bool found = false;\n> +       int ret;\n>\n>         /*\n>          * Open the first DRM/KMS device beginning with /dev/dri/card. The\n> @@ -449,24 +450,42 @@ int Device::openCard()\n>         }\n>\n>         for (struct dirent *res; (res = readdir(folder));) {\n> +               uint64_t cap;\n> +\n>                 if (strncmp(res->d_name, \"card\", 4))\n>                         continue;\n>\n>                 const std::string devName = dirName + res->d_name;\n>                 fd_ = open(devName.c_str(), O_RDWR | O_CLOEXEC);\n> -               if (fd_ >= 0) {\n> -                       ret = 0;\n> -                       break;\n> +               if (fd_ < 0) {\n> +                       ret = -errno;\n> +                       std::cerr << \"Failed to open DRM/KMS device \" << devName << \": \"\n> +                                 << strerror(-ret) << std::endl;\n> +                       continue;\n>                 }\n>\n> -               ret = -errno;\n> -               std::cerr << \"Failed to open DRM/KMS device \" << devName << \": \"\n> -                         << strerror(-ret) << std::endl;\n> +               /*\n> +                * Skip devices that don't support the modeset API, to avoid\n> +                * selecting a DRM device corresponding to a GPU. There is no\n> +                * modeset capability, but the kernel returns an error for most\n> +                * caps if mode setting isn't support but the driver. The\n> +                * DRM_CAP_DUMB_BUFFER capability is one of those, other would\n> +                * do as well. The capability value itself isn't relevant.\n> +                */\n> +               ret = drmGetCap(fd_, DRM_CAP_DUMB_BUFFER, &cap);\n> +               if (ret < 0) {\n\nSince this ends up being an ioctl I guess this is fine. In modetest.c\nthey do it the `if (ret || cap == 0)` way, but a failed ioctl returns\n-1 so this is fine too. Plus the author of that code in modetest.c is\nLaurent Pinchart in 2014 :)\n\nReviewed-by: Eric Curtin <ecurtin@redhat.com>\n\n\n> +                       drmClose(fd_);\n> +                       fd_ = -1;\n> +                       continue;\n> +               }\n> +\n> +               found = true;\n> +               break;\n>         }\n>\n>         closedir(folder);\n>\n> -       return ret;\n> +       return found ? 0 : -ENOENT;\n>  }\n>\n>  int Device::getResources()\n> --\n> Regards,\n>\n> Laurent Pinchart\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 2C393C0DA4\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 28 Sep 2022 11:26:46 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 85EE7622A5;\n\tWed, 28 Sep 2022 13:26:45 +0200 (CEST)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id F2F286224C\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 28 Sep 2022 13:26:43 +0200 (CEST)","from mail-vs1-f71.google.com (mail-vs1-f71.google.com\n\t[209.85.217.71]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id\n\tus-mta-468--Lm9l6OpN_yiI2om1mQGmA-1; Wed, 28 Sep 2022 07:26:41 -0400","by mail-vs1-f71.google.com with SMTP id\n\ti186-20020a6722c3000000b0039e78fd2daeso2132433vsi.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 28 Sep 2022 04:26:41 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664364405;\n\tbh=VIJslA69x5emOfjV0pGLih004qKbDkeDQZxmK7AjeY8=;\n\th=References:In-Reply-To:Date:To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=F6l7fk5iMoMcQ6989bCw0SLsHjy11G8s5/J+YZ2Pf9dxpqCtgcykLzCcmeGQ/CYWt\n\tr2MZIVJYfkVdpGbnHwya46LzgalK6aZhN0mVCNFjqaWyVjOQmMQkXTCnMETXbVuYW0\n\tLuKJWUQ+hsA3/28A+Igq8iFw4hLsLB4i0ma1ZwaCV3DzFL+x9wJHnYEjoMfd3ekrbv\n\tV7uNAQaPu/vMnbhvNpTEqtYsTrZWh3WBLTcVKBepd6Uah3ZcGOUqS5d7WCYNvQTS7S\n\thyV2q5RN1Zj5pMXxxLRTWj10M2MDUf4lHGuwAsxIXo4KM0u5lQWwigqlBZ7Oj4N4kn\n\tVprXv7ZB1z1AQ==","v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1664364402;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=CA82LgGl+4diJ6n6GlNAlHDwVXEwhYF7CZgiZNtkCbE=;\n\tb=KVy1/tNa0evBANhitN6MWl3xg5aDT7Z4FDveD4xUu1+P92uMX/dd9p9XmoI235OSqB8Yls\n\t89pvhfdZKqN+ha99ll+dp008NwGFfDFpu5GucCogFB7NR/YypFH09l9LLxqCJK28mBDVos\n\tUZp08BO9jEIm18avs+OyvkHAGw7/E/w="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=redhat.com\n\theader.i=@redhat.com header.b=\"KVy1/tNa\"; \n\tdkim-atps=neutral","X-MC-Unique":"-Lm9l6OpN_yiI2om1mQGmA-1","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=cc:to:subject:message-id:date:from:in-reply-to:references\n\t:mime-version:x-gm-message-state:from:to:cc:subject:date;\n\tbh=CA82LgGl+4diJ6n6GlNAlHDwVXEwhYF7CZgiZNtkCbE=;\n\tb=Nqzn78bo91RzSwODWc161FW+L4Ru0nsFySBByK03v3swFIcJhvGkQv9bPPhpAzm1eD\n\tnkTb0+nYd3l3L61mHcuv976ENQ6k7kxLvCOdNTe25GZs6wX/lIYziCkWMAy6xXtufCr+\n\t3G2Cx/aAvPy6kopE7CrxG6gI8EC1P2/4T46wzPlePwtZajtqyx0JLSWhBnCeHTwroO2y\n\tiIY9ta+uuYAqLb69sINO28LTFlglkyFw2kBFI8rMLs+IWfQEVilI0MB4d7MxvKa+RHRh\n\tosX8dYCepg4zXVPWjGX78Ee1JJde86HIOSK/IwMBCEcdgXZfFXwcQpN6wlVedNlxHLyk\n\tTO8Q==","X-Gm-Message-State":"ACrzQf0rOypzOpiA/HZZEjH3T2+tWT3sf4lr60y9Uw+IiQ4MnLav8/nQ\n\ti1SMHyqCMJTKKF5OSqDgeddCzYh/365+ZjBy6laeSmKO9+8G6pcbqly9hiibZ/iP7+oB9/2D7Pa\n\tsbJH5W4qTvPEMRUoYnpZeLher2PN+JmzsrKhDRHPsgIWGOe/D4Q==","X-Received":["by 2002:a67:f855:0:b0:39a:c07e:4233 with SMTP id\n\tb21-20020a67f855000000b0039ac07e4233mr15965500vsp.13.1664364401247; \n\tWed, 28 Sep 2022 04:26:41 -0700 (PDT)","by 2002:a67:f855:0:b0:39a:c07e:4233 with SMTP id\n\tb21-20020a67f855000000b0039ac07e4233mr15965494vsp.13.1664364401032;\n\tWed, 28 Sep 2022 04:26:41 -0700 (PDT)"],"X-Google-Smtp-Source":"AMsMyM6WLqyT/l4aJcJIqo36Ad1+bQgq8xCp4sQwPzhNr5Ps6oNrajLhSRveBTyk9B+4ehE1cr/4KdlPv0fqxnoIhOs=","MIME-Version":"1.0","References":"<20220928102743.1484-1-laurent.pinchart@ideasonboard.com>","In-Reply-To":"<20220928102743.1484-1-laurent.pinchart@ideasonboard.com>","Date":"Wed, 28 Sep 2022 12:26:24 +0100","Message-ID":"<CAOgh=Fw1Osjc6uN=h4tPcZr_eGEab36fXAyuTityeeMs+BTykQ@mail.gmail.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","X-Mimecast-Spam-Score":"0","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=\"UTF-8\"","Subject":"Re: [libcamera-devel] [PATCH v2] cam: drm: Skip DRM devices not\n\tcapable of mode setting","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Eric Curtin via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Eric Curtin <ecurtin@redhat.com>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":25164,"web_url":"https://patchwork.libcamera.org/comment/25164/","msgid":"<20220928132955.biil323gfkvfa2ci@uno.localdomain>","date":"2022-09-28T13:29:55","subject":"Re: [libcamera-devel] [PATCH v2] cam: drm: Skip DRM devices not\n\tcapable of mode setting","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent,\n\nOn Wed, Sep 28, 2022 at 01:27:43PM +0300, Laurent Pinchart via libcamera-devel wrote:\n> The DRM helper picks the first DRM card that it can open. On platforms\n> that have a standalone GPU, this risks selecting a device corresponding\n> to the GPU instead of the display controller. Fix this by skipping\n> devices that don't support the KMS mode setting API. Some legacy display\n> controllers would be skipped as well, but libcamera doesn't run on those\n> systems anyway.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n> Changes since v1:\n>\n> - Use DRM_CAP_DUMB_BUFFER instead of DRM_CLIENT_CAP_ATOMIC\n> ---\n>  src/cam/drm.cpp | 35 +++++++++++++++++++++++++++--------\n>  1 file changed, 27 insertions(+), 8 deletions(-)\n>\n> diff --git a/src/cam/drm.cpp b/src/cam/drm.cpp\n> index b0602c942853..3bb950fd157a 100644\n> --- a/src/cam/drm.cpp\n> +++ b/src/cam/drm.cpp\n> @@ -430,7 +430,8 @@ int Device::init()\n>  int Device::openCard()\n>  {\n>  \tconst std::string dirName = \"/dev/dri/\";\n> -\tint ret = -ENOENT;\n> +\tbool found = false;\n> +\tint ret;\n>\n>  \t/*\n>  \t * Open the first DRM/KMS device beginning with /dev/dri/card. The\n> @@ -449,24 +450,42 @@ int Device::openCard()\n>  \t}\n>\n>  \tfor (struct dirent *res; (res = readdir(folder));) {\n> +\t\tuint64_t cap;\n> +\n>  \t\tif (strncmp(res->d_name, \"card\", 4))\n>  \t\t\tcontinue;\n>\n>  \t\tconst std::string devName = dirName + res->d_name;\n>  \t\tfd_ = open(devName.c_str(), O_RDWR | O_CLOEXEC);\n> -\t\tif (fd_ >= 0) {\n> -\t\t\tret = 0;\n> -\t\t\tbreak;\n> +\t\tif (fd_ < 0) {\n> +\t\t\tret = -errno;\n> +\t\t\tstd::cerr << \"Failed to open DRM/KMS device \" << devName << \": \"\n> +\t\t\t\t  << strerror(-ret) << std::endl;\n> +\t\t\tcontinue;\n>  \t\t}\n>\n> -\t\tret = -errno;\n> -\t\tstd::cerr << \"Failed to open DRM/KMS device \" << devName << \": \"\n> -\t\t\t  << strerror(-ret) << std::endl;\n> +\t\t/*\n> +\t\t * Skip devices that don't support the modeset API, to avoid\n> +\t\t * selecting a DRM device corresponding to a GPU. There is no\n> +\t\t * modeset capability, but the kernel returns an error for most\n> +\t\t * caps if mode setting isn't support but the driver. The\n\ns/but/by ?\n\n> +\t\t * DRM_CAP_DUMB_BUFFER capability is one of those, other would\n> +\t\t * do as well. The capability value itself isn't relevant.\n\nThat's a really awful API :)\n\nFor this patch\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nThanks\n  j\n\n\n> +\t\t */\n> +\t\tret = drmGetCap(fd_, DRM_CAP_DUMB_BUFFER, &cap);\n> +\t\tif (ret < 0) {\n> +\t\t\tdrmClose(fd_);\n> +\t\t\tfd_ = -1;\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tfound = true;\n> +\t\tbreak;\n>  \t}\n>\n>  \tclosedir(folder);\n>\n> -\treturn ret;\n> +\treturn found ? 0 : -ENOENT;\n>  }\n>\n>  int Device::getResources()\n> --\n> Regards,\n>\n> Laurent Pinchart\n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 4DBD3BD16B\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 28 Sep 2022 13:30:00 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id AD65C622B0;\n\tWed, 28 Sep 2022 15:29:59 +0200 (CEST)","from relay10.mail.gandi.net (relay10.mail.gandi.net\n\t[IPv6:2001:4b98:dc4:8::230])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 71348622A6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 28 Sep 2022 15:29:58 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby mail.gandi.net (Postfix) with ESMTPSA id AC94D24000B;\n\tWed, 28 Sep 2022 13:29:57 +0000 (UTC)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1664371799;\n\tbh=BsXA5OoHK+B9SnhsJwGL2nXtxBX325mF02EyXRgUSH0=;\n\th=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc:\n\tFrom;\n\tb=Hzopt6d44MPhLDGJUJcxdPTW0HY5u9Q10PcHMDUkDWvuTFyIJGe9ftH4qwFQovSYF\n\tC/j9mOW3NL6GaiLCkennUpAQrNuClyhzR1P79K6nJ0NYFg+0Na4cF6RDQKwbfmHfTe\n\tuqn1N90Obs1Dpr2l0O22DsyKWGwq2dPHPS8kkX2nilVt8jcZxwuLgw2CIpbZKCuA3I\n\tcRmPZmPL4VwYeEuGO4Jm8a1Dw/JOIjG8JTU1qOEMnsryPeFpjiRcHwj0SB4DY09izW\n\thqQeo6OxkyAspYU7GVdI1oVQuy+XF6Xhk7u8tfGoWjl6h2qutlfqdnekJvUpg7EjAn\n\t5NsKb4/uR5WWw==","Date":"Wed, 28 Sep 2022 15:29:55 +0200","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Message-ID":"<20220928132955.biil323gfkvfa2ci@uno.localdomain>","References":"<20220928102743.1484-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20220928102743.1484-1-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v2] cam: drm: Skip DRM devices not\n\tcapable of mode setting","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Jacopo Mondi via libcamera-devel <libcamera-devel@lists.libcamera.org>","Reply-To":"Jacopo Mondi <jacopo@jmondi.org>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]