From e59713c68678f3eb6b6ebe97cabdc88c7042567f Mon Sep 17 00:00:00 2001
From: Dan Scally <djrscally@googlemail.com>
Date: Fri, 28 Aug 2020 07:25:20 +0100
Subject: libcamera: media_object: Make MediaLink::setEnabled() account for
 existing flags

The MediaDevice::setupLink() function fails (ioctl returns EINVAL) when
it passes only the MEDIA_LNK_FL_ENABLE flag to a link that is already
flagged with MEDIA_LNK_FL_ENABLE and MEDIA_LNK_FL_IMMUTABLE. Contrast to
media-ctl's equivalent media_setup_link() which ORs the new flags with
the existing values. Fix this by preserving all flags but
MEDIA_LNK_FL_ENABLED in MediaLink::setEnabled().

Signed-off-by: Dan Scally <djrscally@gmail.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/libcamera/media_object.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/libcamera/media_object.cpp b/src/libcamera/media_object.cpp
index ce77a727..b7bf048c 100644
--- a/src/libcamera/media_object.cpp
+++ b/src/libcamera/media_object.cpp
@@ -115,7 +115,8 @@ LOG_DECLARE_CATEGORY(MediaDevice)
  */
 int MediaLink::setEnabled(bool enable)
 {
-	unsigned int flags = enable ? MEDIA_LNK_FL_ENABLED : 0;
+	unsigned int flags = (flags_ & ~MEDIA_LNK_FL_ENABLED)
+			   | (enable ? MEDIA_LNK_FL_ENABLED : 0);
 
 	int ret = dev_->setupLink(this, flags);
 	if (ret)
-- 
cgit v1.2.1