From patchwork Thu Nov 3 19:19:30 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: 44276 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 129E127BBEB; Thu, 3 Nov 2022 19:21:27 +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 0D50127BBEA for ; Thu, 3 Nov 2022 19:21:26 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oqflx-0004zN-2M; Thu, 03 Nov 2022 15:21:05 -0400 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 1oqflv-0004xr-MV for guix-patches@gnu.org; Thu, 03 Nov 2022 15:21:03 -0400 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 1oqflv-0004mG-Dj for guix-patches@gnu.org; Thu, 03 Nov 2022 15:21:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oqflv-0004pW-8H for guix-patches@gnu.org; Thu, 03 Nov 2022 15:21:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#59003] [PATCH 2/7] 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: Thu, 03 Nov 2022 19:21:03 +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.166750320418418 (code B ref 59003); Thu, 03 Nov 2022 19:21:03 +0000 Received: (at 59003) by debbugs.gnu.org; 3 Nov 2022 19:20:04 +0000 Received: from localhost ([127.0.0.1]:50767 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oqfkx-0004mu-LW for submit@debbugs.gnu.org; Thu, 03 Nov 2022 15:20:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]:46728) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oqfkn-0004lE-NB for 59003@debbugs.gnu.org; Thu, 03 Nov 2022 15:19:54 -0400 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 1oqfki-0004Tm-BS; Thu, 03 Nov 2022 15:19:48 -0400 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=EhvMBkCIAyFVJ2v3wkr/ TETUwDwBoapfeYwOCSFMg3Xgi0iegHvIarhh9OUEgvltloMVxAKF+/t5JqnKQ2Szz1hRYF/0y4SNO ipJfgRDDTDSq36/yZVVVVGNoQLxLPNwoth+v/f+ZaFpIOgDSpWySVt07rs34FZVpDH9XG/D0M3fMb tyAFElPfyJ+aKr2Fzf+emdmrdoze0pnluhsZafoZTSpDJ2g8rgtgPueTaw6iu2RRAh9Ni+Lt9DH19 nZLIBVb/SaN5qlpgCwn/SwrIbRC4nrr63gSY+3jG6DZLxt+wuVNEU5VYKoi6hNaz4ll5csQWI8Dhb uq1jiMgl6jqtXQ==; 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 1oqfkh-0001MS-Ok; Thu, 03 Nov 2022 15:19:47 -0400 From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Thu, 3 Nov 2022 20:19:30 +0100 Message-Id: <20221103191935.16336-2-ludo@gnu.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221103191935.16336-1-ludo@gnu.org> References: <20221103191935.16336-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: , Sender: "Guix-patches" Errors-To: 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: