{"id":1157,"url":"https://patchwork.libcamera.org/api/1.1/patches/1157/?format=json","web_url":"https://patchwork.libcamera.org/patch/1157/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190503153951.9037-1-kieran.bingham@ideasonboard.com>","date":"2019-05-03T15:39:51","name":"[libcamera-devel,RFC] libcamera: v4l2_device: Support M2M devices","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"60dec262adfc2f79b433e74e788760738503f546","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/1157/mbox/","series":[{"id":295,"url":"https://patchwork.libcamera.org/api/1.1/series/295/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=295","date":"2019-05-03T15:39:51","name":"[libcamera-devel,RFC] libcamera: v4l2_device: Support M2M devices","version":1,"mbox":"https://patchwork.libcamera.org/series/295/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1157/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1157/checks/","tags":{},"headers":{"Return-Path":"<kieran.bingham@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0554C60003\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  3 May 2019 17:39:55 +0200 (CEST)","from Q.Home (unknown [IPv6:2a02:c7f:1887:5d00:c990:5ff4:193b:c9b8])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 6669B31E;\n\tFri,  3 May 2019 17:39:54 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1556897994;\n\tbh=yhlBuasFcsgYDiKFonvFmE9CSW5NHdrJ2VPZ5m4CIgY=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=S3ccrUwlK9b0QYW4+qjfOmtVtZedUmFWYRuufSlE3noKMtHAonplxZFKTG0oWpcUW\n\t7ZeTHYVj6Cvp9fWO6S/ea3+tpOaPtPnWu2plnBOLGAlKszchcowT01ZwMXvFUjT7/S\n\thR3H8xHKBHv2YCUo+Oo0IgXAhH4bWqAUWsqoRVLI=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"LibCamera Devel <libcamera-devel@lists.libcamera.org>","Date":"Fri,  3 May 2019 16:39:51 +0100","Message-Id":"<20190503153951.9037-1-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.20.1","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [RFC PATCH] libcamera: v4l2_device: Support M2M\n\tdevices","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","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>","X-List-Received-Date":"Fri, 03 May 2019 15:39:55 -0000"},"content":"V4L2 M2M devices represent a V4L2Device with two queues. One output, and\none capture on a single device node.\n\nRepresent this by instantiating a V4L2Device for each queue type, and\npreparing each device for its queue.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n\nMarking this patch as RFC as yet it does not have a user or test case associated with it.\n\nHowever this patch is part of a development to support a RaspberryPi pipeline\nhandler and is sent independently for some early review and bikeshedding :-)\n\nIt has been used successfully to interact with the RaspberryPi ISP M2M driver.\n\n src/libcamera/include/v4l2_device.h |  13 +++\n src/libcamera/v4l2_device.cpp       | 159 ++++++++++++++++++++++++++++\n 2 files changed, 172 insertions(+)","diff":"diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 689e18dea7a6..4eaf140f452f 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -62,6 +62,11 @@ struct V4L2Capability final : v4l2_capability {\n \t\treturn device_caps() & (V4L2_CAP_VIDEO_OUTPUT |\n \t\t\t\t\tV4L2_CAP_VIDEO_OUTPUT_MPLANE);\n \t}\n+\tbool isM2M() const\n+\t{\n+\t\treturn device_caps() & (V4L2_CAP_VIDEO_M2M |\n+\t\t\t\t\tV4L2_CAP_VIDEO_M2M_MPLANE);\n+\t}\n \tbool isVideo() const\n \t{\n \t\treturn device_caps() & (V4L2_CAP_VIDEO_CAPTURE |\n@@ -117,6 +122,7 @@ public:\n \tV4L2Device &operator=(const V4L2Device &) = delete;\n \n \tint open();\n+\tint setFD(int fd, int type);\n \tbool isOpen() const;\n \tvoid close();\n \n@@ -178,6 +184,13 @@ private:\n \tEventNotifier *fdEvent_;\n };\n \n+struct V4L2M2MDevice {\n+\tV4L2Device *output;\n+\tV4L2Device *capture;\n+};\n+\n+struct V4L2M2MDevice createM2MPair(const std::string &deviceNode);\n+\n } /* namespace libcamera */\n \n #endif /* __LIBCAMERA_V4L2_DEVICE_H__ */\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 916f0360503f..cba1c1799596 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -110,6 +110,12 @@ LOG_DEFINE_CATEGORY(V4L2)\n  * \\return True if the device can output images\n  */\n \n+/**\n+ * \\fn V4L2Capability::isM2M()\n+ * \\brief Identify if the device is an M2M device\n+ * \\return True if the device can capture and output images using the M2M API\n+ */\n+\n /**\n  * \\fn V4L2Capability::isMetaCapture()\n  * \\brief Identify if the device captures image meta-data\n@@ -359,6 +365,74 @@ int V4L2Device::open()\n \treturn 0;\n }\n \n+enum V4L2M2MDeviceType {\n+\tV4L2M2MOutput,\n+\tV4L2M2MCapture,\n+};\n+\n+/**\n+ * \\brief Open a V4L2 M2M device for a specific queue type using an existing fd\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+int V4L2Device::setFD(int fd, int type)\n+{\n+\tint ret;\n+\n+\tfd_ = fd;\n+\n+\tret = ioctl(fd_, VIDIOC_QUERYCAP, &caps_);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Failed to query device capabilities: \"\n+\t\t\t<< strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tLOG(V4L2, Debug)\n+\t\t<< \"Opened device\" << caps_.bus_info() << \": \"\n+\t\t<< caps_.driver() << \": \" << caps_.card();\n+\n+\tif (!caps_.isM2M()) {\n+\t\tLOG(V4L2, Error) << \"Device is not an M2M device\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (!caps_.hasStreaming()) {\n+\t\tLOG(V4L2, Error) << \"Device does not support streaming I/O\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/*\n+\t * Set buffer type and wait for read notifications on CAPTURE devices\n+\t * (POLLIN), and write notifications for OUTPUT devices (POLLOUT).\n+\t */\n+\tswitch(type) {\n+\tcase V4L2M2MOutput:\n+\t\tfdEvent_ = new EventNotifier(fd_, EventNotifier::Write);\n+\t\tbufferType_ = caps_.isMultiplanar()\n+\t\t\t    ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE\n+\t\t\t    : V4L2_BUF_TYPE_VIDEO_OUTPUT;\n+\t\tbreak;\n+\tcase V4L2M2MCapture:\n+\t\tfdEvent_ = new EventNotifier(fd_, EventNotifier::Read);\n+\t\tbufferType_ = caps_.isMultiplanar()\n+\t\t\t    ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE\n+\t\t\t    : V4L2_BUF_TYPE_VIDEO_CAPTURE;\n+\n+\t\tbreak;\n+\tdefault:\n+\t\tLOG(V4L2, Debug) << \"M2M type is not supported\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tfdEvent_->activated.connect(this, &V4L2Device::bufferAvailable);\n+\tfdEvent_->setEnabled(false);\n+\n+\treturn 0;\n+}\n+\n+\n /**\n  * \\brief Check if device is successfully opened\n  * \\return True if the device is open, false otherwise\n@@ -1049,4 +1123,89 @@ V4L2Device *V4L2Device::fromEntityName(const MediaDevice *media,\n \treturn new V4L2Device(mediaEntity);\n }\n \n+/**\n+ * \\struct V4L2M2MDevice\n+ * \\brief Memory2Memory device container\n+ *\n+ * The V4L2M2MDevice structure manages two V4L2Device instances on the same\n+ * deviceNode which operate together using two queues to implement the V4L2\n+ * Memory to Memory API.\n+ *\n+ * V4L2M2MDevices are opened at the point they are created, and will return\n+ * both a capture and an output device or neither in the event of failure.\n+ *\n+ * The two devices should be configured and started as a normal V4L2Device.\n+ * Closing the device will require recreating a new pair.\n+ */\n+\n+/**\n+ * \\var V4L2M2MDevice::output\n+ * \\brief The output V4L2Device to send images to\n+ */\n+\n+/**\n+ * \\var V4L2M2MDevice::capture\n+ * \\brief The capture V4L2Device to receive processed images from\n+ */\n+\n+/**\n+ * \\brief Create a new V4L2M2MDevice from the \\a deviceNode\n+ *\n+ * \\return a new instantiation\n+ */\n+struct V4L2M2MDevice createM2MPair(const std::string &deviceNode)\n+{\n+\tV4L2M2MDevice m2m;\n+\tint ret;\n+\tint fd[2];\n+\n+\t/* Both V4L2Devices will have the same deviceNode */\n+\tret = ::open(deviceNode.c_str(), O_RDWR | O_NONBLOCK);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Failed to open V4L2 M2M device: \" << strerror(-ret);\n+\t\treturn m2m;\n+\t}\n+\tfd[0] = ret;\n+\n+\tret = dup(fd[0]);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Failed to duplicate M2M device: \" << strerror(-ret);\n+\n+\t\tclose(fd[0]);\n+\n+\t\tgoto err_dup;\n+\t}\n+\tfd[1] = ret;\n+\n+\tm2m.output = new V4L2Device(deviceNode);\n+\tm2m.capture = new V4L2Device(deviceNode);\n+\n+\tret = m2m.output->setFD(fd[0], V4L2M2MOutput);\n+\tif (ret)\n+\t\tgoto err_device;\n+\n+\tret = m2m.capture->setFD(fd[1], V4L2M2MCapture);\n+\tif (ret)\n+\t\tgoto err_device;\n+\n+\treturn m2m;\n+\n+err_device:\n+\tdelete m2m.capture;\n+\tdelete m2m.output;\n+\n+\tclose(fd[1]);\n+err_dup:\n+\tclose(fd[0]);\n+\n+\tm2m.capture = nullptr;\n+\tm2m.output = nullptr;\n+\n+\treturn m2m;\n+}\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","RFC"]}