[libcamera-devel,v3,1/6] libcamera: v4l2_device: Add setFd()

Message ID 20190813094020.10277-2-kieran.bingham@ideasonboard.com
State Accepted
Headers show
Series
  • V4L2 M2M Support (+RPi PoC)
Related show

Commit Message

Kieran Bingham Aug. 13, 2019, 9:40 a.m. UTC
Provide a means for V4L2 device instances to set the fd_ explicitly.
This allows for V4L2Devices to operate when they must use an external
open() call.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
---
 src/libcamera/include/v4l2_device.h |  1 +
 src/libcamera/v4l2_device.cpp       | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

Patch

diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h
index e7e9829cb05f..75a52c33d821 100644
--- a/src/libcamera/include/v4l2_device.h
+++ b/src/libcamera/include/v4l2_device.h
@@ -35,6 +35,7 @@  protected:
 	~V4L2Device();
 
 	int open(unsigned int flags);
+	int setFd(int fd);
 
 	int ioctl(unsigned long request, void *argp);
 
diff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp
index 9a00566a532b..f89546610ac6 100644
--- a/src/libcamera/v4l2_device.cpp
+++ b/src/libcamera/v4l2_device.cpp
@@ -88,6 +88,32 @@  int V4L2Device::open(unsigned int flags)
 	return 0;
 }
 
+/**
+ * \brief Set the file descriptor of a V4L2 device
+ * \param[in] fd The file descriptor handle
+ *
+ * This method allows a device to provide an already opened file descriptor
+ * referring to the V4L2 device node, instead of opening it with open(). This
+ * can be used for V4L2 M2M devices where a single video device node is used for
+ * both the output and capture devices, or when receiving an open file
+ * descriptor in a context that doesn't have permission to open the device node
+ * itself.
+ *
+ * This method and the open() method are mutually exclusive, only one of the two
+ * shall be used for a V4L2Device instance.
+ *
+ * \return 0 on success or a negative error code otherwise
+ */
+int V4L2Device::setFd(int fd)
+{
+	if (isOpen())
+		return -EBUSY;
+
+	fd_ = fd;
+
+	return 0;
+}
+
 /**
  * \brief Close the device node
  *