From patchwork Wed Nov 9 21:56:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 44417 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 C842527BBEA; Wed, 9 Nov 2022 21:58:58 +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=unavailable 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 0183227BBE9 for ; Wed, 9 Nov 2022 21:58:57 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ost5B-0006Ai-VQ; Wed, 09 Nov 2022 16:58:05 -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-00069f-AY 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-0004Tx-1O 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 1ost58-0003jG-Td for guix-patches@gnu.org; Wed, 09 Nov 2022 16:58:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#59003] [PATCH v2 2/6] linux-modules: Add support for listing PCI devices. 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:02 +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.166803103214225 (code B ref 59003); Wed, 09 Nov 2022 21:58:02 +0000 Received: (at 59003) by debbugs.gnu.org; 9 Nov 2022 21:57:12 +0000 Received: from localhost ([127.0.0.1]:41192 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ost4K-0003hL-3u for submit@debbugs.gnu.org; Wed, 09 Nov 2022 16:57:12 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ost4C-0003fy-Qw for 59003@debbugs.gnu.org; Wed, 09 Nov 2022 16:57:05 -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 1ost47-000178-6t; Wed, 09 Nov 2022 16:56:59 -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=LWyzEjVfc27/547JoZbVxC3AnVWjO4OxgG5yepVCsjY=; b=Jj5He8Rlbd3C0EZ7JGJK 7l/ITL3+uLzjBlWIqCpZgVQE5qCeOs8Q4XTyMs3e5gUvt74oHPvHqYJIpbVxzeippC+XPAJ2U3Xi/ 6aoQzt9FSYbDbTUVdlM8rzzzA58OplxD9gK2pUdVBIWCsLRNwf2gPwLM1O0YDy8cVC6qRPuTEuo7n w0PR6v8+m3oar9bUfyFQnaZuokqrFS/H7NH0MpUWeGzqWu5TPqXOBqo7cpjqAsWQoqIsNmWo7FDHF y1Ezdu43bExfqC5kwCpzRddGAuRCeEeFf3zEsry1skDpUT4yJBWqFZhm+inexGYskheNhTCU/pqaW hvcasDEELsoEeQ==; 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 1ost46-0000ES-NJ; Wed, 09 Nov 2022 16:56:58 -0500 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Wed, 9 Nov 2022 22:56:33 +0100 Message-Id: <20221109215637.22445-3-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 (): New record type. (pci-device-class-predicate, storage-pci-device?, network-pci-device?) (display-pci-device?, pci-devices?): New procedures. --- gnu/build/linux-modules.scm | 61 ++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/gnu/build/linux-modules.scm b/gnu/build/linux-modules.scm index 053720574b..09cf752bef 100644 --- a/gnu/build/linux-modules.scm +++ b/gnu/build/linux-modules.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2014, 2016, 2018, 2019 Ludovic Courtès +;;; Copyright © 2014, 2016, 2018, 2019, 2022 Ludovic Courtès ;;; Copyright © 2017 Mathieu Othacehe ;;; Copyright © 2018 Danny Milosavljevic ;;; @@ -28,6 +28,7 @@ (define-module (gnu build linux-modules) #:use-module (rnrs io ports) #:use-module (rnrs bytevectors) #:use-module (srfi srfi-1) + #:use-module (srfi srfi-9 gnu) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (ice-9 ftw) @@ -50,6 +51,16 @@ (define-module (gnu build linux-modules) load-linux-module* load-linux-modules-from-directory + pci-devices + pci-device? + pci-device-vendor + pci-device-id + pci-device-class + pci-device-module-alias + storage-pci-device? + network-pci-device? + display-pci-device? + current-module-debugging-port device-module-aliases @@ -429,6 +440,54 @@ (define (read-uevent port) (line (loop (cons (key=value->pair line) result)))))) +;; PCI device known to the Linux kernel. +(define-immutable-record-type + (pci-device vendor device class module-alias) + pci-device? + (vendor pci-device-vendor) ;integer + (device pci-device-id) ;integer + (class pci-device-class) ;integer + (module-alias pci-device-module-alias)) ;string | #f + +(define (pci-device-class-predicate mask bits) + (lambda (device) + "Return true if DEVICE has the chosen class." + (= (logand mask (pci-device-class device)) bits))) + +(define storage-pci-device? ;"Mass storage controller" class + (pci-device-class-predicate #xff0000 #x010000)) +(define network-pci-device? ;"Network controller" class + (pci-device-class-predicate #xff0000 #x020000)) +(define display-pci-device? ;"Display controller" class + (pci-device-class-predicate #xff0000 #x030000)) + +(define (pci-devices) + "Return the list of PCI devices of the system ( records)." + (define (read-hex port) + (let ((line (read-line port))) + (and (string? line) + (string-prefix? "0x" line) + (string->number (string-drop line 2) 16)))) + + (filter-map (lambda (directory) + (define properties + (call-with-input-file (string-append directory "/uevent") + read-uevent)) + (define vendor + (call-with-input-file (string-append directory "/vendor") + read-hex)) + (define device + (call-with-input-file (string-append directory "/device") + read-hex)) + (define class + (call-with-input-file (string-append directory "/class") + read-hex)) + + (pci-device vendor device class + (assq-ref properties 'MODALIAS))) + (find-files "/sys/bus/pci/devices" + #:stat lstat))) + (define (device-module-aliases device) "Return the list of module aliases required by DEVICE, a /dev file name, as in this example: