From patchwork Wed Aug 7 13:58:22 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 2840313 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 581CBBF535 for ; Wed, 7 Aug 2013 14:13:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2FD5720250 for ; Wed, 7 Aug 2013 14:13:37 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 2109E201FE for ; Wed, 7 Aug 2013 14:13:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 0FF0CE7165 for ; Wed, 7 Aug 2013 07:13:36 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by gabe.freedesktop.org (Postfix) with ESMTP id C8C48E70C4; Wed, 7 Aug 2013 06:58:33 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP; 07 Aug 2013 06:58:33 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.89,833,1367996400"; d="scan'208";a="382678370" Received: from unknown (HELO strange.amr.corp.intel.com) ([10.255.14.166]) by fmsmga002.fm.intel.com with ESMTP; 07 Aug 2013 06:58:32 -0700 From: Damien Lespiau To: ajax@redhat.com Date: Wed, 7 Aug 2013 14:58:22 +0100 Message-Id: <1375883903-702-7-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1375883903-702-1-git-send-email-damien.lespiau@intel.com> References: <1375883903-702-1-git-send-email-damien.lespiau@intel.com> Cc: intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: [Intel-gfx] [edid-decode 6/7] Add a small framework to decode fields generically X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: intel-gfx-bounces+patchwork-intel-gfx=patchwork.kernel.org@lists.freedesktop.org Errors-To: intel-gfx-bounces+patchwork-intel-gfx=patchwork.kernel.org@lists.freedesktop.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Damien Lespiau --- edid-decode.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/edid-decode.c b/edid-decode.c index 5061228..7aed3c6 100644 --- a/edid-decode.c +++ b/edid-decode.c @@ -32,8 +32,6 @@ #include #include -#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) - static int claims_one_point_oh = 0; static int claims_one_point_two = 0; static int claims_one_point_three = 0; @@ -65,6 +63,73 @@ static int warning_zero_preferred_refresh = 0; static int conformant = 1; +struct value { + int value; + const char *description; +}; + +struct field { + const char *name; + int start, end; + struct value *values; + int n_values; +}; + +#define DEFINE_FIELD(n, var, s, e, ...) \ + static struct value var##_values[] = { \ + __VA_ARGS__ \ + }; \ + static struct field var = { \ + .name = n, \ + .start = s, \ + .end = e, \ + .values = var##_values, \ + .n_values = ARRAY_SIZE(var##_values), \ + } + +static void +decode_value(struct field *field, int val, const char *prefix) +{ + struct value *v; + int i; + + for (i = 0; i < field->n_values; i++) { + v = &field->values[i]; + + if (v->value == val) + break; + } + + if (i == field->n_values) { + printf("%s%s: %d\n", prefix, field->name, val); + return; + } + + printf("%s%s: %s (%d)\n", prefix, field->name, v->description, val); +} + +static void +_decode(struct field **fields, int n_fields, int data, const char *prefix) +{ + int i; + + for (i = 0; i < n_fields; i++) { + struct field *f = fields[i]; + int field_length = f->end - f->start + 1; + int val; + + if (field_length == 32) + val = data; + else + val = (data >> f->start) & ((1 << field_length) - 1); + + decode_value(f, val, prefix); + } +} + +#define decode(fields, data, prefix) \ + _decode(fields, ARRAY_SIZE(fields), data, prefix) + static char *manufacturer_name(unsigned char *x) { static char name[4];