summaryrefslogtreecommitdiff
path: root/src/libcamera/device_enumerator_sysfs.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_sysfs.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_sysfs.cpp')
-rw-r--r--src/libcamera/device_enumerator_sysfs.cpp35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/libcamera/device_enumerator_sysfs.cpp b/src/libcamera/device_enumerator_sysfs.cpp
index f3054d5e..78a7da8d 100644
--- a/src/libcamera/device_enumerator_sysfs.cpp
+++ b/src/libcamera/device_enumerator_sysfs.cpp
@@ -18,6 +18,7 @@
#include <unistd.h>
#include "log.h"
+#include "media_device.h"
namespace libcamera {
@@ -32,6 +33,7 @@ int DeviceEnumeratorSysfs::enumerate()
{
struct dirent *ent;
DIR *dir;
+ int ret = 0;
static const char * const sysfs_dirs[] = {
"/sys/subsystem/media/devices",
@@ -71,11 +73,42 @@ int DeviceEnumeratorSysfs::enumerate()
continue;
}
- addDevice(devnode);
+ std::shared_ptr<MediaDevice> media = createDevice(devnode);
+ if (!media) {
+ ret = -ENODEV;
+ break;
+ }
+
+ if (populateMediaDevice(media) < 0) {
+ ret = -ENODEV;
+ break;
+ }
+
+ addDevice(media);
}
closedir(dir);
+ return ret;
+}
+
+int DeviceEnumeratorSysfs::populateMediaDevice(const std::shared_ptr<MediaDevice> &media)
+{
+ /* 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;
+
+ int ret = entity->setDeviceNode(deviceNode);
+ if (ret)
+ return ret;
+ }
+
return 0;
}