summaryrefslogtreecommitdiff
path: root/src/libcamera/device_enumerator.cpp
diff options
context:
space:
mode:
authorPaul Elder <paul.elder@ideasonboard.com>2019-09-01 23:53:02 -0400
committerPaul Elder <paul.elder@ideasonboard.com>2019-09-08 19:50:11 -0400
commit6e620349009de675250a7e3e753480b85a6df10b (patch)
tree3fa87d45d3518ba49906f0da4df0afb3bb95a30a /src/libcamera/device_enumerator.cpp
parent19f85f43ff2ad4da255aebe8440a0aa6a3650632 (diff)
libcamera: device_enumerator: fix udev media graph loading dependency
When a MediaDevice is enumerated and populated by the DeviceEnumeratorUdev, there is a possibility that the member device nodes of the media graph would not be ready (either not created, or without proper permissions set by udev yet). The MediaDevice is still passed up to the pipeline handler, where an attempt to access the device nodes will fail in EPERM. This whole issue is especially likely to happen when libcamera is run at system init time. To fix this, we first split DeviceEnumerator::addDevice() into three methods: - createDevice() to simply create the MediaDevice - populateMediaDevice() to populate the MediaDevice - addDevice() to pass the MediaDevice up to the pipeline handler DeviceEnumeratorSysfs calls these methods in succession, similar to what it did before when they were all together as addDevice(). DeviceEnumeratorUdev additionally keeps a map of MediaDevices to a list of pending device nodes (plus some other auxillary maps), and a simple list of orphan device nodes. If a v4l device node is ready and there does not exist any MediaDevice node for it, then it goes to the orphan list, otherwise it is initialized and removed from the pending list of the corresponding MediaDevice in the dependency map. When a MediaDevice is populated via DeviceEnumeratorUdev::populateMediaDevice(), it first checks the orphan list to see if the device nodes it needs are there, otherwise it tries to initialize the device nodes and if it fails, then it adds the device nodes it wants to its list in the dependency map. This allows MediaDevice instances to be created and initialized properly with udev when v4l device nodes in the media graph may not be ready when the MediaDevice is populated. Signed-off-by: Paul Elder <paul.elder@ideasonboard.com> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/libcamera/device_enumerator.cpp')
-rw-r--r--src/libcamera/device_enumerator.cpp54
1 files changed, 28 insertions, 26 deletions
diff --git a/src/libcamera/device_enumerator.cpp b/src/libcamera/device_enumerator.cpp
index 60c918f0..e76438af 100644
--- a/src/libcamera/device_enumerator.cpp
+++ b/src/libcamera/device_enumerator.cpp
@@ -195,16 +195,21 @@ DeviceEnumerator::~DeviceEnumerator()
*/
/**
- * \brief Add a media device to the enumerator
- * \param[in] deviceNode path to the media device to add
+ * \brief Create a media device instance
+ * \param[in] deviceNode path to the media device to create
*
- * Create a media device for the \a deviceNode, open it, populate its media graph,
- * and look up device nodes associated with all entities. Store the media device
- * in the internal list for later matching with pipeline handlers.
+ * Create a media device for the \a deviceNode, open it, and populate its
+ * media graph. The device enumerator shall then populate the media device by
+ * associating device nodes with entities using MediaEntity::setDeviceNode().
+ * This process is specific to each device enumerator, and the device enumerator
+ * shall ensure that device nodes are ready to be used (for instance, if
+ * applicable, by waiting for device nodes to be created and access permissions
+ * to be set by the system). Once done, it shall add the media device to the
+ * system with addDevice().
*
- * \return 0 on success or a negative error code otherwise
+ * \return Created media device instance on success, or nullptr otherwise
*/
-int DeviceEnumerator::addDevice(const std::string &deviceNode)
+std::shared_ptr<MediaDevice> DeviceEnumerator::createDevice(const std::string &deviceNode)
{
std::shared_ptr<MediaDevice> media = std::make_shared<MediaDevice>(deviceNode);
@@ -213,34 +218,31 @@ int DeviceEnumerator::addDevice(const std::string &deviceNode)
LOG(DeviceEnumerator, Info)
<< "Unable to populate media device " << deviceNode
<< " (" << strerror(-ret) << "), skipping";
- return ret;
+ return nullptr;
}
LOG(DeviceEnumerator, Debug)
<< "New media device \"" << media->driver()
<< "\" created from " << deviceNode;
- /* Associate entities to device node paths. */
- for (MediaEntity *entity : media->entities()) {
- if (entity->deviceMajor() == 0 && entity->deviceMinor() == 0)
- continue;
-
- std::string deviceNode = lookupDeviceNode(entity->deviceMajor(),
- entity->deviceMinor());
- if (deviceNode.empty())
- return -EINVAL;
-
- ret = entity->setDeviceNode(deviceNode);
- if (ret)
- return ret;
- }
+ return media;
+}
+/**
+ * \brief Add a media device to the enumerator
+ * \param[in] media media device instance to add
+ *
+ * Store the media device in the internal list for later matching with
+ * pipeline handlers. \a media shall be created with createDevice() first.
+ * This method shall be called after all members of the entities of the
+ * media graph have been confirmed to be initialized.
+ */
+void DeviceEnumerator::addDevice(const std::shared_ptr<MediaDevice> &media)
+{
LOG(DeviceEnumerator, Debug)
- << "Added device " << deviceNode << ": " << media->driver();
-
- devices_.push_back(std::move(media));
+ << "Added device " << media->deviceNode() << ": " << media->driver();
- return 0;
+ devices_.push_back(media);
}
/**