Read EDID in DrmBackend

Documentation for the wonderful EDID format can be found at:
http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf

In case the link breaks: wikipedia also has a good section on it.

We read out the following information:
* EISA code (3 characters)
* serial number
* monitor name (up to 12 characters)
* physical size (stored in cm)

Unfortunately the EDID information cannot be trusted at all.
My 24" screen reports a sice of 16x9 cm. So we need to provide users
a way to overwrite the broken data in an easy way through kwinrc

[EdidOverwrite][EISA code/maker name][monitor name][serial number]
PhysicalSize=trueWidthInMM,trueHeightInMM

Unfortunately monitor name is not a sufficient enough identifier, that's
why serial number is also used. This makes automatic distribution of
overwrites difficult. But in the example above the monitor name is
"SyncMaster" which is a rather broad field in Samsung :-(

The extracted information from EDID is also used to set the corresponding
fields in KWayland::Server::OutputInterface.
This commit is contained in:
Martin Gräßlin 2015-04-27 11:06:04 +02:00
parent f4e2e0e2e9
commit 6d3fa98e09
2 changed files with 205 additions and 4 deletions

View file

@ -33,6 +33,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// KWayland
#include <KWayland/Server/display.h>
#include <KWayland/Server/output_interface.h>
// KF5
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
// Qt
#include <QSocketNotifier>
#include <QPainter>
@ -289,7 +293,7 @@ void DrmBackend::queryResources()
drmOutput->m_mode = connector->modes[0];
}
drmOutput->m_connector = connector->connector_id;
drmOutput->init();
drmOutput->init(connector.data());
connectedOutputs << drmOutput;
}
// check for outputs which got removed
@ -600,12 +604,42 @@ void DrmOutput::cleanupBlackBuffer()
}
}
void DrmOutput::init()
void DrmOutput::init(drmModeConnector *connector)
{
initEdid(connector);
m_savedCrtc.reset(drmModeGetCrtc(m_backend->fd(), m_crtcId));
blank();
m_waylandOutput.reset(waylandServer()->display()->createOutput());
m_waylandOutput->setPhysicalSize(size() / 3.8);
if (!m_edid.eisaId.isEmpty()) {
m_waylandOutput->setManufacturer(QString::fromLatin1(m_edid.eisaId));
} else {
m_waylandOutput->setManufacturer(i18n("unknown"));
}
if (!m_edid.monitorName.isEmpty()) {
QString model = QString::fromLatin1(m_edid.monitorName);
if (!m_edid.serialNumber.isEmpty()) {
model.append('/');
model.append(QString::fromLatin1(m_edid.serialNumber));
}
m_waylandOutput->setModel(model);
} else if (!m_edid.serialNumber.isEmpty()) {
m_waylandOutput->setModel(QString::fromLatin1(m_edid.serialNumber));
} else {
m_waylandOutput->setModel(i18n("unknown"));
}
QSize physicalSize = !m_edid.physicalSize.isEmpty() ? m_edid.physicalSize : QSize(connector->mmWidth, connector->mmHeight);
// the size might be completely borked. E.g. Samsung SyncMaster 2494HS reports 160x90 while in truth it's 520x292
// as this information is used to calculate DPI info, it's going to result in everything being huge
KSharedConfig::Ptr config = KSharedConfig::openConfig(KWIN_CONFIG);
KConfigGroup group = config->group("EdidOverwrite").group(m_edid.eisaId).group(m_edid.monitorName).group(m_edid.serialNumber);
if (group.hasKey("PhysicalSize")) {
const QSize overwriteSize = group.readEntry("PhysicalSize", physicalSize);
qCWarning(KWIN_CORE) << "Overwriting monitor physical size for" << m_edid.eisaId << "/" << m_edid.monitorName << "/" << m_edid.serialNumber << " from " << physicalSize << "to " << overwriteSize;
physicalSize = overwriteSize;
}
m_waylandOutput->setPhysicalSize(physicalSize);
m_waylandOutput->addMode(size());
m_waylandOutput->create();
}
@ -631,6 +665,165 @@ bool DrmOutput::setMode(DrmBuffer *buffer)
}
}
static bool verifyEdidHeader(drmModePropertyBlobPtr edid)
{
const uint8_t *data = reinterpret_cast<uint8_t*>(edid->data);
if (data[0] != 0x00) {
return false;
}
for (int i = 1; i < 7; ++i) {
if (data[i] != 0xFF) {
return false;
}
}
if (data[7] != 0x00) {
return false;
}
return true;
}
static QByteArray extractEisaId(drmModePropertyBlobPtr edid)
{
/*
* From EDID standard section 3.4:
* The ID Manufacturer Name field, shown in Table 3.5, contains a 2-byte representation of the monitor's
* manufacturer. This is the same as the EISA ID. It is based on compressed ASCII, 0001=A ... 11010=Z.
*
* The table:
* | Byte | Bit |
* | | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
* ----------------------------------------
* | 1 | 0)| (4| 3 | 2 | 1 | 0)| (4| 3 |
* | | * | Character 1 | Char 2|
* ----------------------------------------
* | 2 | 2 | 1 | 0)| (4| 3 | 2 | 1 | 0)|
* | | Character2| Character 3 |
* ----------------------------------------
**/
const uint8_t *data = reinterpret_cast<uint8_t*>(edid->data);
static const uint offset = 0x8;
char id[4];
if (data[offset] >> 7) {
// bit at position 7 is not a 0
return QByteArray();
}
// shift two bits to right, and with 7 right most bits
id[0] = 'A' + ((data[offset] >> 2) & 0x1f) -1;
// for first byte: take last two bits and shift them 3 to left (000xx000)
// for second byte: shift 5 bits to right and take 3 right most bits (00000xxx)
// or both together
id[1] = 'A' + (((data[offset] & 0x3) << 3) | ((data[offset + 1] >> 5) & 0x7)) - 1;
// take five right most bits
id[2] = 'A' + (data[offset + 1] & 0x1f) - 1;
id[3] = '\0';
return QByteArray(id);
}
static void extractMonitorDescriptorDescription(drmModePropertyBlobPtr blob, DrmOutput::Edid &edid)
{
// see section 3.10.3
const uint8_t *data = reinterpret_cast<uint8_t*>(blob->data);
static const uint offset = 0x36;
static const uint blockLength = 18;
for (int i = 0; i < 5; ++i) {
const uint co = offset + i * blockLength;
// Flag = 0000h when block used as descriptor
if (data[co] != 0) {
continue;
}
if (data[co + 1] != 0) {
continue;
}
// Reserved = 00h when block used as descriptor
if (data[co + 2] != 0) {
continue;
}
/*
* FFh: Monitor Serial Number - Stored as ASCII, code page # 437, 13 bytes.
* FEh: ASCII String - Stored as ASCII, code page # 437, 13 bytes.
* FDh: Monitor range limits, binary coded
* FCh: Monitor name, stored as ASCII, code page # 437
* FBh: Descriptor contains additional color point data
* FAh: Descriptor contains additional Standard Timing Identifications
* F9h - 11h: Currently undefined
* 10h: Dummy descriptor, used to indicate that the descriptor space is unused
* 0Fh - 00h: Descriptor defined by manufacturer.
*/
if (data[co + 3] == 0xfc && edid.monitorName.isEmpty()) {
edid.monitorName = QByteArray((const char *)(&data[co + 5]), 12).trimmed();
}
if (data[co + 3] == 0xfe) {
const QByteArray id = QByteArray((const char *)(&data[co + 5]), 12).trimmed();
if (!id.isEmpty()) {
edid.eisaId = id;
}
}
if (data[co + 3] == 0xff) {
edid.serialNumber = QByteArray((const char *)(&data[co + 5]), 12).trimmed();
}
}
}
static QByteArray extractSerialNumber(drmModePropertyBlobPtr edid)
{
// see section 3.4
const uint8_t *data = reinterpret_cast<uint8_t*>(edid->data);
static const uint offset = 0x0C;
/*
* The ID serial number is a 32-bit serial number used to differentiate between individual instances of the same model
* of monitor. Its use is optional. When used, the bit order for this field follows that shown in Table 3.6. The EDID
* structure Version 1 Revision 1 and later offer a way to represent the serial number of the monitor as an ASCII string
* in a separate descriptor block.
*/
uint32_t serialNumber = 0;
serialNumber = (uint32_t) data[offset + 0];
serialNumber |= (uint32_t) data[offset + 1] << 8;
serialNumber |= (uint32_t) data[offset + 2] << 16;
serialNumber |= (uint32_t) data[offset + 3] << 24;
if (serialNumber == 0) {
return QByteArray();
}
return QByteArray::number(serialNumber);
}
static QSize extractPhysicalSize(drmModePropertyBlobPtr edid)
{
const uint8_t *data = reinterpret_cast<uint8_t*>(edid->data);
return QSize(data[0x15], data[0x16]) * 10;
}
void DrmOutput::initEdid(drmModeConnector *connector)
{
ScopedDrmPointer<_drmModePropertyBlob, &drmModeFreePropertyBlob> edid;
for (int i = 0; i < connector->count_props; ++i) {
ScopedDrmPointer<_drmModeProperty, &drmModeFreeProperty> property(drmModeGetProperty(m_backend->fd(), connector->props[i]));
if (!property) {
continue;
}
if ((property->flags & DRM_MODE_PROP_BLOB) && qstrcmp(property->name, "EDID") == 0) {
edid.reset(drmModeGetPropertyBlob(m_backend->fd(), connector->prop_values[i]));
}
}
if (!edid) {
return;
}
// for documentation see: http://read.pudn.com/downloads110/ebook/456020/E-EDID%20Standard.pdf
if (edid->length < 128) {
return;
}
if (!verifyEdidHeader(edid.data())) {
return;
}
m_edid.eisaId = extractEisaId(edid.data());
m_edid.serialNumber = extractSerialNumber(edid.data());
// parse monitor descriptor description
extractMonitorDescriptorDescription(edid.data(), m_edid);
m_edid.physicalSize = extractPhysicalSize(edid.data());
}
DrmBuffer::DrmBuffer(DrmBackend *backend, const QSize &size)
: m_backend(backend)
, m_size(size)

View file

@ -110,13 +110,19 @@ private:
class DrmOutput
{
public:
struct Edid {
QByteArray eisaId;
QByteArray monitorName;
QByteArray serialNumber;
QSize physicalSize;
};
virtual ~DrmOutput();
void showCursor(DrmBuffer *buffer);
void hideCursor();
void moveCursor(const QPoint &globalPos);
bool present(DrmBuffer *buffer);
void pageFlipped();
void init();
void init(drmModeConnector *connector);
void restoreSaved();
void blank();
@ -128,6 +134,7 @@ private:
DrmOutput(DrmBackend *backend);
void cleanupBlackBuffer();
bool setMode(DrmBuffer *buffer);
void initEdid(drmModeConnector *connector);
DrmBackend *m_backend;
QPoint m_globalPos;
@ -142,6 +149,7 @@ private:
drmModeFreeCrtc(ptr);
}
};
Edid m_edid;
QScopedPointer<_drmModeCrtc, CrtcCleanup> m_savedCrtc;
QScopedPointer<KWayland::Server::OutputInterface> m_waylandOutput;
};