From patchwork Wed Nov 9 21:56:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 44415 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id C319E27BBEB; Wed, 9 Nov 2022 21:58:28 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id BEC3D27BBE9 for ; Wed, 9 Nov 2022 21:58:27 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ost5E-0006Bs-7W; Wed, 09 Nov 2022 16:58:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ost5A-0006A1-O0 for guix-patches@gnu.org; Wed, 09 Nov 2022 16:58:04 -0500 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ost5A-0004UL-Ft for guix-patches@gnu.org; Wed, 09 Nov 2022 16:58:04 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ost5A-0003jj-B9 for guix-patches@gnu.org; Wed, 09 Nov 2022 16:58:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#59003] [PATCH v2 3/6] linux-modules: Add 'load-pci-device-database'. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Wed, 09 Nov 2022 21:58:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59003 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 59003@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by 59003-submit@debbugs.gnu.org id=B59003.166803103414253 (code B ref 59003); Wed, 09 Nov 2022 21:58:04 +0000 Received: (at 59003) by debbugs.gnu.org; 9 Nov 2022 21:57:14 +0000 Received: from localhost ([127.0.0.1]:41200 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ost4M-0003hk-6O for submit@debbugs.gnu.org; Wed, 09 Nov 2022 16:57:14 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46920) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ost4G-0003gM-5G for 59003@debbugs.gnu.org; Wed, 09 Nov 2022 16:57:09 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ost48-00018c-1d; Wed, 09 Nov 2022 16:57:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=7Aed3jnUd50+ZQc08YclTlx5MAYdpPY3hhZfxXuM9bw=; b=hV0pJQED0eWpWbHQG/mY lij4m9WmI+jXrP4UAlDomXjrb1imaygNmX1S3RDjggZhgv8KqtVSee1DKs8NSSlifB3pmPmHRxV0g gtK6R5m1503/1zZMm2VxsfgFaNyvsY5mAvGKiUw7LkbB15ROIMQR1pMsRs8uwGigKxUM15nuSGm5r L4S2gQc4xQby1xXlnet4cGiRkUQom0qnH5mynflJ2u+Hpvss7NZ2KsMfYC80lOAir5DQLWNNjRSHM b28mBHlfzTZr4Tt00H4gFwxIYVMUhUauwcK2rVwC1X01zV2jVQGSt5E+eNRR6zufuO6EMiR5tDcyW N/aEPCeO/ei3cQ==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201] helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ost47-0000ES-Ak; Wed, 09 Nov 2022 16:56:59 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Wed, 9 Nov 2022 22:56:34 +0100 Message-Id: <20221109215637.22445-4-ludo@gnu.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221109215637.22445-1-ludo@gnu.org> References: <20221103191756.16265-1-ludo@gnu.org> <20221109215637.22445-1-ludo@gnu.org> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * gnu/build/linux-modules.scm (read-pci-device-database) (load-pci-device-database): New procedures. --- gnu/build/linux-modules.scm | 74 +++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/gnu/build/linux-modules.scm b/gnu/build/linux-modules.scm index 09cf752bef..3b1f512663 100644 --- a/gnu/build/linux-modules.scm +++ b/gnu/build/linux-modules.scm @@ -60,6 +60,7 @@ (define-module (gnu build linux-modules) storage-pci-device? network-pci-device? display-pci-device? + load-pci-device-database current-module-debugging-port @@ -488,6 +489,79 @@ (define class (find-files "/sys/bus/pci/devices" #:stat lstat))) +(define (read-pci-device-database port) + "Parse the 'pci.ids' database that ships with the pciutils package and is +maintained at ." + (define (comment? str) + (string-prefix? "#" (string-trim str))) + (define (blank? str) + (string-null? (string-trim-both str))) + (define (device? str) + (eqv? #\tab (string-ref str 0))) + (define (subvendor? str) + (string-prefix? "\t\t" str)) + (define (class? str) + (string-prefix? "C " str)) + (define (parse-id-line str) + (let* ((str (string-trim-both str)) + (space (string-index str char-set:whitespace))) + (values (string->number (string-take str space) 16) + (string-trim (string-drop str (+ 1 space)))))) + (define (finish vendor vendor-id devices table) + (fold (lambda (device table) + (match device + ((device-id . name) + (vhash-consv (logior (ash vendor-id 16) device-id) + (cons vendor name) + table)))) + table + devices)) + + (let loop ((table vlist-null) + (vendor-id #f) + (vendor #f) + (devices '())) + (match (read-line port) + ((? eof-object?) + (let ((table (if (and vendor vendor-id) + (finish vendor vendor-id devices table) + table))) + (lambda (vendor device) + (match (vhash-assv (logior (ash vendor 16) device) table) + (#f + (values #f #f)) + ((_ . (vendor . name)) + (values vendor name)))))) + ((? comment?) + (loop table vendor-id vendor devices)) + ((? blank?) + (loop table vendor-id vendor devices)) + ((? subvendor?) ;currently ignored + (loop table vendor-id vendor devices)) + ((? class?) ;currently ignored + (loop table vendor-id vendor devices)) + ((? device? line) + (let-values (((id name) (parse-id-line line))) + (loop table vendor-id vendor + (if (and vendor-id vendor) ;class or device? + (alist-cons id name devices) + devices)))) + (line + (let ((table (if (and vendor vendor-id) + (finish vendor vendor-id devices table) + table))) + (let-values (((vendor-id vendor) (parse-id-line line))) + (loop table vendor-id vendor '()))))))) + +(define (load-pci-device-database file) + "Read the 'pci.ids' database at FILE (get it from the pciutils package or +from ) and return a lookup procedure that takes a PCI +vendor ID and a device ID (two integers) and returns the vendor name and +device name as two values." + (let ((port (open-file file "r0"))) + (call-with-gzip-input-port port + read-pci-device-database))) + (define (device-module-aliases device) "Return the list of module aliases required by DEVICE, a /dev file name, as in this example: