{"id":845,"url":"https://patchwork.libcamera.org/api/patches/845/?format=json","web_url":"https://patchwork.libcamera.org/patch/845/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20190402162134.3894-4-jacopo@jmondi.org>","date":"2019-04-02T16:21:25","name":"[libcamera-devel,v6,03/12] libcamera: ipu3: Create CIO2Device class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"00d3222b11135842a61bc10c9e1dad12716e63ef","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/?format=json","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/845/mbox/","series":[{"id":229,"url":"https://patchwork.libcamera.org/api/series/229/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=229","date":"2019-04-02T16:21:22","name":"libcamera: ipu3: Add ImgU support","version":6,"mbox":"https://patchwork.libcamera.org/series/229/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/845/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/845/checks/","tags":{},"headers":{"Return-Path":"<jacopo@jmondi.org>","Received":["from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net\n\t[217.70.183.200])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 86CA5610BF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  2 Apr 2019 18:20:56 +0200 (CEST)","from uno.lan (2-224-242-101.ip172.fastwebnet.it [2.224.242.101])\n\t(Authenticated sender: jacopo@jmondi.org)\n\tby relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 115952000A;\n\tTue,  2 Apr 2019 16:20:55 +0000 (UTC)"],"X-Originating-IP":"2.224.242.101","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  2 Apr 2019 18:21:25 +0200","Message-Id":"<20190402162134.3894-4-jacopo@jmondi.org>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190402162134.3894-1-jacopo@jmondi.org>","References":"<20190402162134.3894-1-jacopo@jmondi.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v6 03/12] libcamera: ipu3: Create\n\tCIO2Device class","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":"Tue, 02 Apr 2019 16:20:56 -0000"},"content":"Group CIO2 components (cio2, csi2 and image sensor) in a class\nassociated with the CameraData, to ease management and initialization of\nCIO2 unit at camera registration time. A CIO2 unit will always be\nassociated with a single Camera only.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\n---\n src/libcamera/pipeline/ipu3/ipu3.cpp | 294 +++++++++++++++++----------\n 1 file changed, 185 insertions(+), 109 deletions(-)","diff":"diff --git a/src/libcamera/pipeline/ipu3/ipu3.cpp b/src/libcamera/pipeline/ipu3/ipu3.cpp\nindex 55489c31df2d..1e315048738b 100644\n--- a/src/libcamera/pipeline/ipu3/ipu3.cpp\n+++ b/src/libcamera/pipeline/ipu3/ipu3.cpp\n@@ -8,6 +8,8 @@\n #include <memory>\n #include <vector>\n \n+#include <linux/media-bus-format.h>\n+\n #include <libcamera/camera.h>\n #include <libcamera/request.h>\n #include <libcamera/stream.h>\n@@ -24,6 +26,48 @@ namespace libcamera {\n \n LOG_DEFINE_CATEGORY(IPU3)\n \n+static int mediaBusToCIO2Format(unsigned int code)\n+{\n+\tswitch (code) {\n+\tcase MEDIA_BUS_FMT_SBGGR10_1X10:\n+\t\treturn V4L2_PIX_FMT_IPU3_SBGGR10;\n+\tcase MEDIA_BUS_FMT_SGBRG10_1X10:\n+\t\treturn V4L2_PIX_FMT_IPU3_SGBRG10;\n+\tcase MEDIA_BUS_FMT_SGRBG10_1X10:\n+\t\treturn V4L2_PIX_FMT_IPU3_SGRBG10;\n+\tcase MEDIA_BUS_FMT_SRGGB10_1X10:\n+\t\treturn V4L2_PIX_FMT_IPU3_SRGGB10;\n+\tdefault:\n+\t\treturn -EINVAL;\n+\t}\n+}\n+\n+class CIO2Device\n+{\n+public:\n+\tCIO2Device()\n+\t\t: output_(nullptr), csi2_(nullptr), sensor_(nullptr)\n+\t{\n+\t}\n+\n+\t~CIO2Device()\n+\t{\n+\t\tdelete output_;\n+\t\tdelete csi2_;\n+\t\tdelete sensor_;\n+\t}\n+\n+\tint init(const MediaDevice *media, unsigned int index);\n+\n+\tV4L2Device *output_;\n+\tV4L2Subdevice *csi2_;\n+\tV4L2Subdevice *sensor_;\n+\n+\t/* Maximum sizes and the mbus code used to produce them. */\n+\tunsigned int mbusCode_;\n+\tSize maxSize_;\n+};\n+\n class PipelineHandlerIPU3 : public PipelineHandler\n {\n public:\n@@ -51,23 +95,13 @@ private:\n \t{\n \tpublic:\n \t\tIPU3CameraData(PipelineHandler *pipe)\n-\t\t\t: CameraData(pipe), cio2_(nullptr), csi2_(nullptr),\n-\t\t\t  sensor_(nullptr)\n-\t\t{\n-\t\t}\n-\n-\t\t~IPU3CameraData()\n+\t\t\t: CameraData(pipe)\n \t\t{\n-\t\t\tdelete cio2_;\n-\t\t\tdelete csi2_;\n-\t\t\tdelete sensor_;\n \t\t}\n \n \t\tvoid bufferReady(Buffer *buffer);\n \n-\t\tV4L2Device *cio2_;\n-\t\tV4L2Subdevice *csi2_;\n-\t\tV4L2Subdevice *sensor_;\n+\t\tCIO2Device cio2_;\n \n \t\tStream stream_;\n \t};\n@@ -80,22 +114,22 @@ private:\n \n \tvoid registerCameras();\n \n-\tstd::shared_ptr<MediaDevice> cio2_;\n-\tstd::shared_ptr<MediaDevice> imgu_;\n+\tstd::shared_ptr<MediaDevice> cio2MediaDev_;\n+\tstd::shared_ptr<MediaDevice> imguMediaDev_;\n };\n \n PipelineHandlerIPU3::PipelineHandlerIPU3(CameraManager *manager)\n-\t: PipelineHandler(manager), cio2_(nullptr), imgu_(nullptr)\n+\t: PipelineHandler(manager), cio2MediaDev_(nullptr), imguMediaDev_(nullptr)\n {\n }\n \n PipelineHandlerIPU3::~PipelineHandlerIPU3()\n {\n-\tif (cio2_)\n-\t\tcio2_->release();\n+\tif (cio2MediaDev_)\n+\t\tcio2MediaDev_->release();\n \n-\tif (imgu_)\n-\t\timgu_->release();\n+\tif (imguMediaDev_)\n+\t\timguMediaDev_->release();\n }\n \n std::map<Stream *, StreamConfiguration>\n@@ -110,7 +144,7 @@ PipelineHandlerIPU3::streamConfiguration(Camera *camera,\n \t * FIXME: As of now, return the image format reported by the sensor.\n \t * In future good defaults should be provided for each stream.\n \t */\n-\tif (data->sensor_->getFormat(0, &format)) {\n+\tif (data->cio2_.sensor_->getFormat(0, &format)) {\n \t\tLOG(IPU3, Error) << \"Failed to create stream configurations\";\n \t\treturn configs;\n \t}\n@@ -131,9 +165,9 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,\n {\n \tIPU3CameraData *data = cameraData(camera);\n \tStreamConfiguration *cfg = &config[&data->stream_];\n-\tV4L2Subdevice *sensor = data->sensor_;\n-\tV4L2Subdevice *csi2 = data->csi2_;\n-\tV4L2Device *cio2 = data->cio2_;\n+\tV4L2Subdevice *sensor = data->cio2_.sensor_;\n+\tV4L2Subdevice *csi2 = data->cio2_.csi2_;\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \tV4L2SubdeviceFormat subdevFormat = {};\n \tV4L2DeviceFormat devFormat = {};\n \tint ret;\n@@ -190,13 +224,14 @@ int PipelineHandlerIPU3::configureStreams(Camera *camera,\n \n int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)\n {\n-\tIPU3CameraData *data = cameraData(camera);\n \tconst StreamConfiguration &cfg = stream->configuration();\n+\tIPU3CameraData *data = cameraData(camera);\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \n \tif (!cfg.bufferCount)\n \t\treturn -EINVAL;\n \n-\tint ret = data->cio2_->exportBuffers(&stream->bufferPool());\n+\tint ret = cio2->exportBuffers(&stream->bufferPool());\n \tif (ret) {\n \t\tLOG(IPU3, Error) << \"Failed to request memory\";\n \t\treturn ret;\n@@ -208,8 +243,9 @@ int PipelineHandlerIPU3::allocateBuffers(Camera *camera, Stream *stream)\n int PipelineHandlerIPU3::freeBuffers(Camera *camera, Stream *stream)\n {\n \tIPU3CameraData *data = cameraData(camera);\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \n-\tint ret = data->cio2_->releaseBuffers();\n+\tint ret = cio2->releaseBuffers();\n \tif (ret) {\n \t\tLOG(IPU3, Error) << \"Failed to release memory\";\n \t\treturn ret;\n@@ -221,9 +257,10 @@ int PipelineHandlerIPU3::freeBuffers(Camera *camera, Stream *stream)\n int PipelineHandlerIPU3::start(Camera *camera)\n {\n \tIPU3CameraData *data = cameraData(camera);\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \tint ret;\n \n-\tret = data->cio2_->streamOn();\n+\tret = cio2->streamOn();\n \tif (ret) {\n \t\tLOG(IPU3, Info) << \"Failed to start camera \" << camera->name();\n \t\treturn ret;\n@@ -235,8 +272,9 @@ int PipelineHandlerIPU3::start(Camera *camera)\n void PipelineHandlerIPU3::stop(Camera *camera)\n {\n \tIPU3CameraData *data = cameraData(camera);\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \n-\tif (data->cio2_->streamOff())\n+\tif (cio2->streamOff())\n \t\tLOG(IPU3, Info) << \"Failed to stop camera \" << camera->name();\n \n \tPipelineHandler::stop(camera);\n@@ -245,6 +283,7 @@ void PipelineHandlerIPU3::stop(Camera *camera)\n int PipelineHandlerIPU3::queueRequest(Camera *camera, Request *request)\n {\n \tIPU3CameraData *data = cameraData(camera);\n+\tV4L2Device *cio2 = data->cio2_.output_;\n \tStream *stream = &data->stream_;\n \n \tBuffer *buffer = request->findBuffer(stream);\n@@ -254,7 +293,7 @@ int PipelineHandlerIPU3::queueRequest(Camera *camera, Request *request)\n \t\treturn -ENOENT;\n \t}\n \n-\tint ret = data->cio2_->queueBuffer(buffer);\n+\tint ret = cio2->queueBuffer(buffer);\n \tif (ret < 0)\n \t\treturn ret;\n \n@@ -293,17 +332,17 @@ bool PipelineHandlerIPU3::match(DeviceEnumerator *enumerator)\n \t * It is safe to acquire both media devices at this point as\n \t * DeviceEnumerator::search() skips the busy ones for us.\n \t */\n-\tcio2_ = enumerator->search(cio2_dm);\n-\tif (!cio2_)\n+\tcio2MediaDev_ = enumerator->search(cio2_dm);\n+\tif (!cio2MediaDev_)\n \t\treturn false;\n \n-\tcio2_->acquire();\n+\tcio2MediaDev_->acquire();\n \n-\timgu_ = enumerator->search(imgu_dm);\n-\tif (!imgu_)\n+\timguMediaDev_ = enumerator->search(imgu_dm);\n+\tif (!imguMediaDev_)\n \t\treturn false;\n \n-\timgu_->acquire();\n+\timguMediaDev_->acquire();\n \n \t/*\n \t * Disable all links that are enabled by default on CIO2, as camera\n@@ -312,17 +351,17 @@ bool PipelineHandlerIPU3::match(DeviceEnumerator *enumerator)\n \t * Close the CIO2 media device after, as links are enabled and should\n \t * not need to be changed after.\n \t */\n-\tif (cio2_->open())\n+\tif (cio2MediaDev_->open())\n \t\treturn false;\n \n-\tif (cio2_->disableLinks()) {\n-\t\tcio2_->close();\n+\tif (cio2MediaDev_->disableLinks()) {\n+\t\tcio2MediaDev_->close();\n \t\treturn false;\n \t}\n \n \tregisterCameras();\n \n-\tcio2_->close();\n+\tcio2MediaDev_->close();\n \n \treturn true;\n }\n@@ -336,85 +375,28 @@ void PipelineHandlerIPU3::registerCameras()\n {\n \t/*\n \t * For each CSI-2 receiver on the IPU3, create a Camera if an\n-\t * image sensor is connected to it.\n+\t * image sensor is connected to it and the sensor can produce images\n+\t * in a compatible format.\n \t */\n \tunsigned int numCameras = 0;\n \tfor (unsigned int id = 0; id < 4; ++id) {\n-\t\tstd::string csi2Name = \"ipu3-csi2 \" + std::to_string(id);\n-\t\tMediaEntity *csi2 = cio2_->getEntityByName(csi2Name);\n-\t\tint ret;\n-\n-\t\t/*\n-\t\t * This shall not happen, as the device enumerator matched\n-\t\t * all entities described in the cio2_dm DeviceMatch.\n-\t\t *\n-\t\t * As this check is basically free, better stay safe than sorry.\n-\t\t */\n-\t\tif (!csi2)\n-\t\t\tcontinue;\n-\n-\t\tconst std::vector<MediaPad *> &pads = csi2->pads();\n-\t\tif (pads.empty())\n-\t\t\tcontinue;\n-\n-\t\t/* IPU3 CSI-2 receivers have a single sink pad at index 0. */\n-\t\tMediaPad *sink = pads[0];\n-\t\tconst std::vector<MediaLink *> &links = sink->links();\n-\t\tif (links.empty())\n-\t\t\tcontinue;\n-\n-\t\t/*\n-\t\t * Verify that the receiver is connected to a sensor, enable\n-\t\t * the media link between the two, and create a Camera with\n-\t\t * a unique name.\n-\t\t */\n-\t\tMediaLink *link = links[0];\n-\t\tMediaEntity *sensor = link->source()->entity();\n-\t\tif (sensor->function() != MEDIA_ENT_F_CAM_SENSOR)\n-\t\t\tcontinue;\n-\n-\t\tif (link->setEnabled(true))\n-\t\t\tcontinue;\n-\n-\t\tstd::unique_ptr<IPU3CameraData> data = utils::make_unique<IPU3CameraData>(this);\n-\n-\t\tstd::string cameraName = sensor->name() + \" \" + std::to_string(id);\n+\t\tstd::unique_ptr<IPU3CameraData> data =\n+\t\t\tutils::make_unique<IPU3CameraData>(this);\n \t\tstd::set<Stream *> streams{ &data->stream_ };\n-\t\tstd::shared_ptr<Camera> camera = Camera::create(this, cameraName, streams);\n-\n-\t\t/*\n-\t\t * Create and open video devices and subdevices associated with\n-\t\t * the camera.\n-\t\t *\n-\t\t * If any of these operations fails, the Camera instance won't\n-\t\t * be registered. The 'camera' shared pointer and the 'data'\n-\t\t * unique pointers go out of scope and delete the objects they\n-\t\t * manage.\n-\t\t */\n-\t\tstd::string cio2Name = \"ipu3-cio2 \" + std::to_string(id);\n-\t\tMediaEntity *cio2 = cio2_->getEntityByName(cio2Name);\n-\t\tif (!cio2) {\n-\t\t\tLOG(IPU3, Error)\n-\t\t\t\t<< \"Failed to get entity '\" << cio2Name << \"'\";\n-\t\t\tcontinue;\n-\t\t}\n+\t\tCIO2Device *cio2 = &data->cio2_;\n \n-\t\tdata->cio2_ = new V4L2Device(cio2);\n-\t\tret = data->cio2_->open();\n+\t\tint ret = cio2->init(cio2MediaDev_.get(), id);\n \t\tif (ret)\n \t\t\tcontinue;\n \n-\t\tdata->cio2_->bufferReady.connect(data.get(), &IPU3CameraData::bufferReady);\n+\t\tstd::string cameraName = cio2->sensor_->entityName() + \" \"\n+\t\t\t\t       + std::to_string(id);\n+\t\tstd::shared_ptr<Camera> camera = Camera::create(this,\n+\t\t\t\t\t\t\t\tcameraName,\n+\t\t\t\t\t\t\t\tstreams);\n \n-\t\tdata->sensor_ = new V4L2Subdevice(sensor);\n-\t\tret = data->sensor_->open();\n-\t\tif (ret)\n-\t\t\tcontinue;\n-\n-\t\tdata->csi2_ = new V4L2Subdevice(csi2);\n-\t\tret = data->csi2_->open();\n-\t\tif (ret)\n-\t\t\tcontinue;\n+\t\tcio2->output_->bufferReady.connect(data.get(),\n+\t\t\t\t\t\t   &IPU3CameraData::bufferReady);\n \n \t\tregisterCamera(std::move(camera), std::move(data));\n \n@@ -435,6 +417,100 @@ void PipelineHandlerIPU3::IPU3CameraData::bufferReady(Buffer *buffer)\n \tpipe_->completeRequest(camera_, request);\n }\n \n+/*------------------------------------------------------------------------------\n+ * CIO2 Device\n+ */\n+\n+/**\n+ * \\brief Initialize components of the CIO2 device with \\a index\n+ * \\param[in] media The CIO2 media device\n+ * \\param[in] index The CIO2 device index\n+ *\n+ * Create and open the video device and subdevices in the CIO2 instance at \\a\n+ * index, if an image sensor is connected to the CSI-2 receiver of this CIO2\n+ * instance.  Enable the media links connecting the CIO2 components to prepare\n+ * for capture operations and cache the sensor maximum size.\n+ *\n+ * \\return 0 on success or a negative error code otherwise\n+ * \\retval -ENODEV No supported image sensor is connected to this CIO2 instance\n+ */\n+int CIO2Device::init(const MediaDevice *media, unsigned int index)\n+{\n+\tint ret;\n+\n+\t/*\n+\t * Verify that a sensor subdevice is connected to this CIO2 instance\n+\t * and enable link between the two.\n+\t */\n+\tstd::string csi2Name = \"ipu3-csi2 \" + std::to_string(index);\n+\tMediaEntity *csi2Entity = media->getEntityByName(csi2Name);\n+\tconst std::vector<MediaPad *> &pads = csi2Entity->pads();\n+\tif (pads.empty())\n+\t\treturn -ENODEV;\n+\n+\t/* IPU3 CSI-2 receivers have a single sink pad at index 0. */\n+\tMediaPad *sink = pads[0];\n+\tconst std::vector<MediaLink *> &links = sink->links();\n+\tif (links.empty())\n+\t\treturn -ENODEV;\n+\n+\tMediaLink *link = links[0];\n+\tMediaEntity *sensorEntity = link->source()->entity();\n+\tif (sensorEntity->function() != MEDIA_ENT_F_CAM_SENSOR)\n+\t\treturn -ENODEV;\n+\n+\tret = link->setEnabled(true);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/*\n+\t * Now that we're sure a sensor subdevice is connected, make sure it\n+\t * produces at least one image format compatible with CIO2 requirements\n+\t * and cache the camera maximum size.\n+\t *\n+\t * \\todo Define when to open and close video device nodes, as they\n+\t * might impact on power consumption.\n+\t */\n+\tsensor_ = new V4L2Subdevice(sensorEntity);\n+\tret = sensor_->open();\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tfor (auto it : sensor_->formats(0)) {\n+\t\tint mbusCode = mediaBusToCIO2Format(it.first);\n+\t\tif (mbusCode < 0)\n+\t\t\tcontinue;\n+\n+\t\tfor (const SizeRange &size : it.second) {\n+\t\t\tif (maxSize_.width < size.maxWidth &&\n+\t\t\t    maxSize_.height < size.maxHeight) {\n+\t\t\t\tmaxSize_.width = size.maxWidth;\n+\t\t\t\tmaxSize_.height = size.maxHeight;\n+\t\t\t\tmbusCode_ = mbusCode;\n+\t\t\t}\n+\t\t}\n+\t}\n+\tif (maxSize_.width == 0) {\n+\t\tLOG(IPU3, Info) << \"Sensor '\" << sensor_->entityName()\n+\t\t\t\t<< \"' detected, but no supported image format \"\n+\t\t\t\t<< \" found: skip camera creation\";\n+\t\treturn -ENODEV;\n+\t}\n+\n+\tcsi2_ = new V4L2Subdevice(csi2Entity);\n+\tret = csi2_->open();\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tstd::string cio2Name = \"ipu3-cio2 \" + std::to_string(index);\n+\toutput_ = V4L2Device::fromEntityName(media, cio2Name);\n+\tret = output_->open();\n+\tif (ret)\n+\t\treturn ret;\n+\n+\treturn 0;\n+}\n+\n REGISTER_PIPELINE_HANDLER(PipelineHandlerIPU3);\n \n } /* namespace libcamera */\n","prefixes":["libcamera-devel","v6","03/12"]}