From patchwork Mon Dec 30 18:59:53 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Gundersen X-Patchwork-Id: 305829 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id C71402C00DD for ; Tue, 31 Dec 2013 05:54:01 +1100 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932141Ab3L3SyA (ORCPT ); Mon, 30 Dec 2013 13:54:00 -0500 Received: from mail-wi0-f176.google.com ([209.85.212.176]:55747 "EHLO mail-wi0-f176.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932083Ab3L3Sx7 (ORCPT ); Mon, 30 Dec 2013 13:53:59 -0500 Received: by mail-wi0-f176.google.com with SMTP id hq4so16599652wib.9 for ; Mon, 30 Dec 2013 10:53:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=HhC+DOJNcbFGnu+Br0tljDXWbdKOp4R6MHnuwv8ThWc=; b=cRP5JhVJedSjFJRMf9qaPhHmbVzUikTCCZk4xWB+xxUX7swvccXkvXl4VtAw0Lf4mN 4VHm5YvlKnFod8UZTN5Ebhls20AOfLpsqCtPI4IMnzM0fFY8k9Bs+sJmHSU4gMsvyrjJ 0yV1DHi+zE5K+LKcrsk/UMxc3ZORvLo8akKpXI/zNZyoBvoew/FycNUZ+t3ZeVZ9Wo8P mwpiB0ez2ULjzqrM7npPSI8z+QB5UzziFw4svHwkOlqnhByA2WVRqW1Q1vf0mGQMxAFe 1Jzl1DFqkO7ALauOPl5pZ+E/FPXoTa4W0GW9xRNsYG0c4K98ej+V/fGQ3rrW3xMoT1Sb zZBw== X-Gm-Message-State: ALoCoQm1rUnxLwJPeedoymnxULjlIz/M6k4a3t0t2D2Soc80qKZZWvpXjnkQ/G86+Kd6xDOvYiBh X-Received: by 10.180.19.72 with SMTP id c8mr45507961wie.24.1388429638422; Mon, 30 Dec 2013 10:53:58 -0800 (PST) Received: from localhost.localdomain (ip-51.net-89-2-236.rev.numericable.fr. [89.2.236.51]) by mx.google.com with ESMTPSA id e1sm68424432wij.7.2013.12.30.10.53.57 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 30 Dec 2013 10:53:57 -0800 (PST) From: Tom Gundersen To: linux-pci@vger.kernel.org Cc: kay@vrfy.org, linux-kernel@vger.kernel.org, Tom Gundersen , Martin Mares Subject: [PATCH] libpci: pci_id_lookup - add udev/hwdb support Date: Mon, 30 Dec 2013 19:59:53 +0100 Message-Id: <1388429993-14231-1-git-send-email-teg@jklm.no> X-Mailer: git-send-email 1.8.5.2 In-Reply-To: References: Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org This lets you select hwdb support at compile time. hwdb is an efficient hardware database shipped with recent versions of udev. It contains among other sources pci.ids so querying hwdb rather than reading pci.ids directly should give the same result. Ideally Linux distros using udev could stop shipping pci.ids, but use hwdb as the only source of this information, which this patch allows. Cc: Martin Mares --- lib/configure | 17 ++++++++++++++++ lib/names-hash.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/lib/configure b/lib/configure index 27388bc..778f99f 100755 --- a/lib/configure +++ b/lib/configure @@ -141,6 +141,23 @@ esac echo >>$c '#define PCI_HAVE_PM_DUMP' echo " dump" +echo_n "Checking for udev hwdb support... " +if [ "$HWDB" = yes -o "$HWDB" = no ] ; then + echo "$HWDB (set manually)" +else + if `pkg-config --atleast-version=196 libudev` ; then + HWDB=yes + else + HWDB=no + fi + echo "$HWDB (auto-detected)" +fi +if [ "$HWDB" = yes ] ; then + echo >>$c '#define PCI_HAVE_HWDB' + echo >>$m 'LIBUDEV=-ludev' + echo >>$m 'WITH_LIBS+=$(LIBUDEV)' +fi + echo_n "Checking for zlib support... " if [ "$ZLIB" = yes -o "$ZLIB" = no ] ; then echo "$ZLIB (set manually)" diff --git a/lib/names-hash.c b/lib/names-hash.c index 33f3c11..9661d03 100644 --- a/lib/names-hash.c +++ b/lib/names-hash.c @@ -11,6 +11,11 @@ #include "internal.h" #include "names.h" +#ifdef PCI_HAVE_HWDB +#include +#include +#endif + struct id_bucket { struct id_bucket *next; unsigned int full; @@ -86,8 +91,58 @@ char *pci_id_lookup(struct pci_access *a, int flags, int cat, int id1, int id2, int id3, int id4) { struct id_entry *n, *best; - u32 id12 = id_pair(id1, id2); - u32 id34 = id_pair(id3, id4); + u32 id12, id34; + +#ifdef PCI_HAVE_HWDB + if (!(flags & PCI_LOOKUP_SKIP_LOCAL)) + { + char modalias[64]; + const char *key = NULL; + struct udev *udev = udev_new(); + struct udev_hwdb *hwdb = udev_hwdb_new(udev); + struct udev_list_entry *entry; + + switch(cat) + { + case ID_VENDOR: + sprintf(modalias, "pci:v%08X*", id1); + key = "ID_VENDOR_FROM_DATABASE"; + break; + case ID_DEVICE: + sprintf(modalias, "pci:v%08Xd%08X*", id1, id2); + key = "ID_MODEL_FROM_DATABASE"; + break; + case ID_SUBSYSTEM: + sprintf(modalias, "pci:v%08Xd%08Xsv%08Xsd%08X*", id1, id2, id3, id4); + key = "ID_MODEL_FROM_DATABASE"; + break; + case ID_GEN_SUBSYSTEM: + sprintf(modalias, "pci:v*d*sv%08Xsd%08X*", id1, id2); + key = "ID_MODEL_FROM_DATABASE"; + break; + case ID_CLASS: + sprintf(modalias, "pci:v*d*sv*sd*bc%02X*", id1); + key = "ID_PCI_CLASS_FROM_DATABASE"; + break; + case ID_SUBCLASS: + sprintf(modalias, "pci:v*d*sv*sd*bc%02Xsc%02X*", id1, id2); + key = "ID_PCI_SUBCLASS_FROM_DATABASE"; + break; + case ID_PROGIF: + sprintf(modalias, "pci:v*d*sv*sd*bc%02Xsc%02Xi%02X*", id1, id2, id3); + key = "ID_PCI_INTERFACE_FROM_DATABASE"; + break; + } + + if (key) + udev_list_entry_foreach(entry, udev_hwdb_get_properties_list_entry(hwdb, modalias, 0)) + if (strcmp(udev_list_entry_get_name(entry), key) == 0) + return udev_list_entry_get_value(entry); + } +#endif + + id12 = id_pair(id1, id2); + id34 = id_pair(id3, id4); if (a->id_hash) {