/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2021, Ideas on Board Oy * * drm.cpp - DRM/KMS Helpers */ #include "drm.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include "event_loop.h" namespace DRM { Object::Object(Device *dev, uint32_t id, Type type) : id_(id), dev_(dev), type_(type) { /* Retrieve properties from the objects that support them. */ if (type != TypeConnector && type != TypeCrtc && type != TypeEncoder && type != TypePlane) return; /* * We can't distinguish between failures due to the object having no * property and failures due to other conditions. Assume we use the API * correctly and consider the object has no property. */ drmModeObjectProperties *properties = drmModeObjectGetProperties(dev->fd(), id, type); if (!properties) return; properties_.reserve(properties->count_props); for (uint32_t i = 0; i < properties->count_props; ++i) properties_.emplace_back(properties->props[i], properties->prop_values[i]); drmModeFreeObjectProperties(properties); } Object::~Object() { } const Property *Object::property(const std::string &name) const { for (const PropertyValue &pv : properties_) { const Property *property = static_cast(dev_->object(pv.id())); if (property && property->name() == name) return property; } return nullptr; } const PropertyValue *Object::propertyValue(const std::string &name) const { for (const PropertyValue &pv : properties_) { const Property *property = static_cast(dev_->object(pv.id())); if (property && property->name() == name) return &pv; } return nullptr; } Property::Property(Device *dev, drmModePropertyRes *property) : Object(dev, property->prop_id, TypeProperty), name_(property->name), flags_(property->flags), values_(property->values, property->values + property->count_values), blobs_(property->blob_ids, property->blob_ids + property->count_blobs) { if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) type_ = TypeRange; else if (drm_property_type_is(property, DRM_MODE_PROP_ENUM)) type_ = TypeEnum; else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) type_ = TypeBlob; else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) type_ = TypeBitmask; else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) type_ = TypeObject; else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) type_ = TypeSignedRange; else type_ = TypeUnknown; for (int i = 0; i < property->count_enums; ++i) enums_[property->enums[i].value] = property->enums[i].name; } Blob::Blob(Device *dev, const libcamera::Span &data) : Object(dev, 0, Object::TypeBlob) { drmModeCreatePropertyBlob(dev->fd(), data.data(), data.size(), &id_); } Blob::~Blob() { if (isValid()) drmModeDestroyPropertyBlob(device()->fd(), id()); } Mode::Mode(const drmModeModeInfo &mode) : drmModeModeInfo(mode) { } std::unique_ptr Mode::toBlob(Device *dev) const { libcamera::Span data{ reinterpret_cast(this), sizeof(*this) }; return std::make_unique(dev, data); } Crtc::Crtc(Device *dev, const drmModeCrtc *crtc, unsigned int index) : Object(dev, crtc->crtc_id, Object::TypeCrtc), index_(index) { } Encoder::Encoder(Device *dev, const drmModeEncoder *encoder) : Object(dev, encoder->encoder_id, Object::TypeEncoder), type_(encoder->encoder_type) { const std::list &crtcs = dev->crtcs(); possibleCrtcs_.reserve(crtcs.size()); for (const Crtc &crtc : crtcs) { if (encoder->possible_crtcs & (1 << crtc.index())) possibleCrtcs_.push_back(&crtc); } possibleCrtcs_.shrink_to_fit(); } namespace { const std::map connectorTypeNames{ { DRM_MODE_CONNECTOR_Unknown, "Unknown" }, { DRM_MODE_CONNECTOR_VGA, "VGA" }, { DRM_MODE_CONNECTOR_DVII, "DVI-I" }, { DRM_MODE_CONNECTOR_DVID, "DVI-D" }, { DRM_MODE_CONNECTOR_DVIA, "DVI-A" }, { DRM_MODE_CONNECTOR_Composite, "Composite" }, { DRM_MODE_CONNECTOR_SVIDEO, "S-Video" }, { DRM_MODE_CONNECTOR_LVDS, "LVDS" }, { DRM_MODE_CONNECTOR_Component, "Component" }, { DRM_MODE_CONNECTOR_9PinDIN, "9-Pin-DIN" }, { DRM_MODE_CONNECTOR_DisplayPort, "DP" }, { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" }, { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" }, { DRM_MODE_CONNECTOR_TV, "TV" }, { DRM_MODE_CONNECTOR_eDP, "eDP" }, { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" }, { DRM_MODE_CONNECTOR_DSI, "DSI" }, { DRM_MODE_CONNECTOR_DPI, "DPI" }, }; } /* namespace */ Connector::Connector(Device *dev, const drmModeConnector *connector) : Object(dev, connector->connector_id, Object::TypeConnector), type_(connector->connector_type) { auto typeName = connectorTypeNames.find(connector->connector_type); if (typeName == connectorTypeNames.end()) { std::cerr << "Invalid connector type " << connector->connector_type << std::endl; typeName = connectorTypeNames.find(DRM_MODE_CONNECTOR_Unknown); } name_ = std::string(typeName->second) + "-" + std::to_string(connector->connector_type_id); switch (connector->connection) { case DRM_MODE_CONNECTED: status_ = Status::Connected; break; case DRM_MODE_DISCONNECTED: status_ = Status::Disconnected; break; case DRM_MODE_UNKNOWNCONNECTION: default: status_ = Status::Unknown; break; } const std::list &encoders = dev->encoders(); encoders_.reserve(connector->count_encoders); for (int i = 0; i < connector->count_encoders; ++i) { uint32_t encoderId = connector->encoders[i]; auto encoder = std::find_if(encoders.begin(), encoders.end(), [=](const Encoder &e) { return e.id() == encoderId; }); if (encoder == encoders.end()) { std::cerr << "Encoder " << encoderId << " not found" << std::endl; continue; } encoders_.push_back(&*encoder); } encoders_.shrink_to_fit(); modes_ = { connector->modes, connector->modes + connector->count_modes }; } Plane::Plane(Device *dev, const drmModePlane *plane) : Object(dev, plane->plane_id, Object::TypePlane), possibleCrtcsMask_(plane->possible_crtcs) { formats_ = { plane->formats, plane->formats + plane->count_formats }; const std::list &crtcs = dev->crtcs(); possibleCrtcs_.reserve(crtcs.size()); for (const Crtc &crtc : crtcs) { if (plane->possible_crtcs & (1 << crtc.index())) possibleCrtcs_.push_back(&crtc); } possibleCrtcs_.shrink_to_fit(); } bool Plane::supportsFormat(const libcamera::PixelFormat &format) const { return std::find(formats_.begin(), formats_.end(), format.fourcc()) != formats_.end(); } int Plane::setup() { const PropertyValue *pv = propertyValue("type"); if (!pv) return -EINVAL; switch (pv->value()) { case DRM_PLANE_TYPE_OVERLAY: type_ = TypeOverlay; break; case DRM_PLANE_TYPE_PRIMARY: type_ = TypePrimary; break; case DRM_PLANE_TYPE_CURSOR: type_ = TypeCursor; break; default: return -EINVAL; } return 0; } FrameBuffer::FrameBuffer(Device *dev) : Object(dev, 0, Object::TypeFb) { } FrameBuffer::~FrameBuffer() { for (const auto &plane : planes_) { struct drm_gem_close gem_close = { .handle = plane.second.handle, .pad = 0, }; int ret; do { ret = ioctl(device()->fd(), DRM_IOCTL_GEM_CLOSE, &gem_close); } while (ret == -1 && (errno == EINTR || errno == EAGAIN)); if (ret == -1) { ret = -errno; std::cerr << "Failed to close GEM object: " << strerror(-ret) << std::endl; } } drmModeRmFB(device()->fd(), id()); } AtomicRequest::AtomicRequest(Device *dev) : dev_(dev), valid_(true) { request_ = drmModeAtomicAlloc(); if (!request_) va/* SPDX-License-Identifier: GPL-2.0-or-later */ /* * Copyright (C) 2019, Google Inc. * * controls.cpp - V4L2 device controls handling test */ #include <algorithm> #include <array> #include <iostream> #include <limits.h> #include "libcamera/internal/v4l2_videodevice.h" #include "v4l2_videodevice_test.h" /* These come from the vivid driver. */ #define VIVID_CID_CUSTOM_BASE (V4L2_CID_USER_BASE | 0xf000) #define VIVID_CID_INTEGER64 (VIVID_CID_CUSTOM_BASE + 3) #define VIVID_CID_U8_4D_ARRAY (VIVID_CID_CUSTOM_BASE + 10) /* Helper for VIVID_CID_U8_4D_ARRAY control array size: not from kernel. */ #define VIVID_CID_U8_ARRAY_SIZE (2 * 3 * 4 * 5) using namespace std; using namespace libcamera; class V4L2ControlTest : public V4L2VideoDeviceTest { public: V4L2ControlTest() : V4L2VideoDeviceTest("vivid", "vivid-000-vid-cap") { } protected: int run() { const ControlInfoMap &infoMap = capture_->controls(); /* Test control enumeration. */ if (infoMap.empty()) { cerr << "Failed to enumerate controls" << endl; return TestFail; } if (infoMap.find(V4L2_CID_BRIGHTNESS) == infoMap.end() || infoMap.find(V4L2_CID_CONTRAST) == infoMap.end() || infoMap.find(V4L2_CID_SATURATION) == infoMap.end() || infoMap.find(VIVID_CID_INTEGER64) == infoMap.end() || infoMap.find(VIVID_CID_U8_4D_ARRAY) == infoMap.end()) { cerr << "Missing controls" << endl; return TestFail; } const ControlInfo &brightness = infoMap.find(V4L2_CID_BRIGHTNESS)->second; const ControlInfo &contrast = infoMap.find(V4L2_CID_CONTRAST)->second; const ControlInfo &saturation = infoMap.find(V4L2_CID_SATURATION)->second; const ControlInfo &int64 = infoMap.find(VIVID_CID_INTEGER64)->second; const ControlInfo &u8 = infoMap.find(VIVID_CID_U8_4D_ARRAY)->second; /* Test getting controls. */ ControlList ctrls = capture_->getControls({ V4L2_CID_BRIGHTNESS, V4L2_CID_CONTRAST, V4L2_CID_SATURATION, VIVID_CID_INTEGER64, VIVID_CID_U8_4D_ARRAY }); if (ctrls.empty()) { cerr << "Failed to get controls" << endl; return TestFail; } if (ctrls.infoMap() != &infoMap) { cerr << "Incorrect infoMap for retrieved controls" << endl; return TestFail; } if (ctrls.get(V4L2_CID_BRIGHTNESS).get<int32_t>() == -1 || ctrls.get(V4L2_CID_CONTRAST).get<int32_t>() == -1 || ctrls.get(V4L2_CID_SATURATION).get<int32_t>() == -1) { cerr << "Incorrect value for retrieved controls" << endl; return TestFail; } /* * The VIVID_CID_INTEGER64 control can take any value, just test * that its value can be retrieved and has the right type. */ ctrls.get(VIVID_CID_INTEGER64).get<int64_t>(); uint8_t u8Min = u8.min().get<uint8_t>(); uint8_t u8Max = u8.max().get<uint8_t>(); Span<const uint8_t> u8Span = ctrls.get(VIVID_CID_U8_4D_ARRAY).get<Span<const uint8_t>>(); bool valid = std::all_of(u8Span.begin(), u8Span.end(), [&](uint8_t v) { return v >= u8Min && v <= u8Max; }); if (!valid) { cerr << "Incorrect value for retrieved array control" << endl; return TestFail; } /* Test setting controls. */ ctrls.set(V4L2_CID_BRIGHTNESS, brightness.min()); ctrls.set(V4L2_CID_CONTRAST, contrast.max()); ctrls.set(V4L2_CID_SATURATION, saturation.min()); ctrls.set(VIVID_CID_INTEGER64, int64.min()); std::array<uint8_t, VIVID_CID_U8_ARRAY_SIZE> u8Values; std::fill(u8Values.begin(), u8Values.end(), u8.min().get<uint8_t>()); ctrls.set(VIVID_CID_U8_4D_ARRAY, Span<const uint8_t>(u8Values)); int ret = capture_->setControls(&ctrls); if (ret) { cerr << "Failed to set controls" << endl; return TestFail; } /* Test setting controls outside of range. */ ctrls.set(V4L2_CID_BRIGHTNESS, brightness.min().get<int32_t>() - 1); ctrls.set(V4L2_CID_CONTRAST, contrast.max().get<int32_t>() + 1); ctrls.set(V4L2_CID_SATURATION, saturation.min().get<int32_t>() + 1); ret = capture_->setControls(&ctrls); if (ret) { cerr << "Failed to set controls (out of range)" << endl; return TestFail; } if (ctrls.get(V4L2_CID_BRIGHTNESS) != brightness.min() || ctrls.get(V4L2_CID_CONTRAST) != contrast.max() || ctrls.get(V4L2_CID_SATURATION) != saturation.min().get<int32_t>() + 1) { cerr << "Controls not updated when set" << endl; return TestFail; } return TestPass; } }; TEST_REGISTER(V4L2ControlTest)