From patchwork Wed Aug 7 14:20:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Lespiau, Damien" X-Patchwork-Id: 2840318 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id AD5EE9F479 for ; Wed, 7 Aug 2013 14:27:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 96E292028F for ; Wed, 7 Aug 2013 14:27:23 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 00D9120255 for ; Wed, 7 Aug 2013 14:27:19 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EA63FE71C9 for ; Wed, 7 Aug 2013 07:27:18 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by gabe.freedesktop.org (Postfix) with ESMTP id B8A06E7171; Wed, 7 Aug 2013 07:20:34 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP; 07 Aug 2013 07:20:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.89,833,1367996400"; d="scan'208";a="382691141" Received: from unknown (HELO strange.amr.corp.intel.com) ([10.255.14.166]) by fmsmga002.fm.intel.com with ESMTP; 07 Aug 2013 07:20:33 -0700 From: Damien Lespiau To: ajax@redhat.com Subject: [edid-decode] Add a small framework to decode fields generically Date: Wed, 7 Aug 2013 15:20:32 +0100 Message-Id: <1375885232-3508-1-git-send-email-damien.lespiau@intel.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1375883903-702-7-git-send-email-damien.lespiau@intel.com> References: <1375883903-702-7-git-send-email-damien.lespiau@intel.com> Cc: intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=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 v2: Fix rebase fail that removed a necessary hunk Signed-off-by: Damien Lespiau --- edid-decode.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/edid-decode.c b/edid-decode.c index 5061228..083ddd9 100644 --- a/edid-decode.c +++ b/edid-decode.c @@ -65,6 +65,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];