@@ -9,7 +9,9 @@
#include <map>
#include <optional>
+#include <span>
#include <string>
+#include <sys/sysmacros.h>
#include <vector>
#include <linux/media.h>
@@ -50,6 +52,17 @@ public:
unsigned int version() const { return version_; }
unsigned int hwRevision() const { return hwRevision_; }
+ const std::vector<dev_t> entitiesDevnums() const
+ {
+ return utils::map_keys(devnumsMap_);
+ }
+
+ const std::list<unsigned int> &entitiesIdsByDevNum(dev_t devnum) const
+ {
+ ASSERT(devnumsMap_.contains(devnum));
+ return devnumsMap_.at(devnum);
+ }
+
const std::vector<MediaEntity *> &entities() const { return entities_; }
MediaEntity *getEntityByName(const std::string &name) const;
MediaEntity *getEntityByName(const std::regex &name) const;
@@ -128,6 +141,12 @@ private:
delete[] links_;
}
+ std::span<const struct media_v2_entity> entities() const
+ {
+ return { reinterpret_cast<struct media_v2_entity *>
+ (topology_.ptr_entities), topology_.num_entities };
+ }
+
struct media_v2_topology topology_ = {};
private:
@@ -137,6 +156,8 @@ private:
struct media_v2_pad *pads_ = nullptr;
} mediaV2Topology_;
+ std::map<dev_t, std::list<unsigned int>> devnumsMap_;
+
UniqueFD fd_;
bool valid_;
bool acquired_;
@@ -238,6 +238,28 @@ int MediaDevice::populate()
version = mediaV2Topology_.version();
}
+ /*
+ * Create a map of devnums to entity ids. It will be used to inject the
+ * entity devnode with the support of the device enumerators.
+ */
+ for (const auto &ent : mediaV2Topology_.entities()) {
+ struct media_v2_interface *iface =
+ findInterface(mediaV2Topology_.topology_, ent.id);
+ if (!iface)
+ continue;
+
+ dev_t devnum = makedev(iface->devnode.major, iface->devnode.minor);
+
+ /* Multiple entities can refer to the same devnum. */
+ auto it = devnumsMap_.find(devnum);
+ if (it == devnumsMap_.end()) {
+ devnumsMap_[devnum].push_back(ent.id);
+ continue;
+ }
+
+ it->second.push_back(ent.id);
+ }
+
/* Populate entities, pads and links. */
if (populateEntities(mediaV2Topology_.topology_) &&
populatePads(mediaV2Topology_.topology_) &&
@@ -365,6 +387,28 @@ bool MediaDevice::match(const DeviceMatch &device) const
* \return The MediaDevice hardware revision
*/
+/**
+ * \fn MediaDevice::entitiesDevnums()
+ * \brief Return the devnums for all entities in the MediaDevice
+ *
+ * devnums are created for each media entity which register a device node using
+ * mkdev(major, minor).
+ *
+ * \return A vector of devnums
+ */
+
+/**
+ * \fn MediaDevice::entitiesIdsByDevNum(dev_t devnum)
+ * \brief Return a list of entities ids associated with \a devnum
+ * \param[in] devnum The device numer
+ *
+ * Return a list of media entities ids whose device node is associated with
+ * \a devnum. As multiple media entites can refer to the same device node
+ * return a list.
+ *
+ * \return A list of media entities ids
+ */
+
/**
* \fn MediaDevice::entities()
* \brief Retrieve the list of entities in the media graph
After the media topology has been queried and deemed to be stable, create a map of device numbers to entity ids. Multiple entities can have the same devnum as they refer to the same devnode, hence we map the devnum to a list of entities ids. Add two accessors to the MediaDevice class to: - Get all the devnums - Get the list of entities ids given a devnum These helpers will be used to de-couple the device enumerators from the MediaObject class hierarchy, specifically the devnode path injection that requires to know the entities' devnums. Signed-off-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com> --- include/libcamera/internal/media_device.h | 21 +++++++++++++++ src/libcamera/media_device.cpp | 44 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)