all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: 59003@debbugs.gnu.org
Cc: "Ludovic Courtès" <ludo@gnu.org>
Subject: [bug#59003] [PATCH v2 2/6] linux-modules: Add support for listing PCI devices.
Date: Wed,  9 Nov 2022 22:56:33 +0100	[thread overview]
Message-ID: <20221109215637.22445-3-ludo@gnu.org> (raw)
In-Reply-To: <20221109215637.22445-1-ludo@gnu.org>

* gnu/build/linux-modules.scm (<pci-device>): 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 <ludo@gnu.org>
+;;; Copyright © 2014, 2016, 2018, 2019, 2022 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
 ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org>
 ;;;
@@ -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>
+  (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 (<pci-device> 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:
-- 
2.38.0





  parent reply	other threads:[~2022-11-09 21:59 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-03 19:17 [bug#59003] [PATCH 0/7] [Installer] Warn about unsupported devices Ludovic Courtès
2022-11-03 19:19 ` [bug#59003] [PATCH 1/7] installer: Warn about hardware support after the welcome page Ludovic Courtès
2022-11-03 19:19   ` [bug#59003] [PATCH 2/7] linux-modules: Add support for listing PCI devices Ludovic Courtès
2022-11-05 15:21     ` pelzflorian (Florian Pelz)
2022-11-03 19:19   ` [bug#59003] [PATCH 3/7] linux-modules: Add 'load-pci-device-database' Ludovic Courtès
2022-11-03 19:19   ` [bug#59003] [PATCH 4/7] installer: Use 'current-guix' for extensions Ludovic Courtès
2022-11-05  9:09     ` pelzflorian (Florian Pelz)
2022-11-05 17:34       ` Ludovic Courtès
2022-11-03 19:19   ` [bug#59003] [PATCH 5/7] installer: Error page width is parameterized Ludovic Courtès
2022-11-03 19:19   ` [bug#59003] [PATCH 6/7] installer: Report known-unsupported PCI devices Ludovic Courtès
2022-11-05 17:55     ` pelzflorian (Florian Pelz)
2022-11-06 11:20       ` Ludovic Courtès
2022-11-06 19:06         ` pelzflorian (Florian Pelz)
2022-11-05 20:51     ` [bug#59003] [PATCH 0/7] [Installer] Warn about unsupported devices Mathieu Othacehe
2022-11-05 21:11       ` Mathieu Othacehe
2022-11-09 20:26         ` Ludovic Courtès
2022-11-03 19:19   ` [bug#59003] [PATCH 7/7] installer: Remove unused variable Ludovic Courtès
2022-11-05  8:52   ` [bug#59003] [PATCH 1/7] installer: Warn about hardware support after the welcome page pelzflorian (Florian Pelz)
2022-11-05 18:02     ` pelzflorian (Florian Pelz)
2022-11-09 21:56 ` [bug#59003] [PATCH v2 0/6] [Installer] Warn about unsupported devices Ludovic Courtès
2022-11-09 21:56   ` [bug#59003] [PATCH v2 1/6] installer: Warn about hardware support after the welcome page Ludovic Courtès
2022-11-09 21:56   ` Ludovic Courtès [this message]
2022-11-09 21:56   ` [bug#59003] [PATCH v2 3/6] linux-modules: Add 'load-pci-device-database' Ludovic Courtès
2022-11-09 21:56   ` [bug#59003] [PATCH v2 4/6] installer: Use 'current-guix' for extensions Ludovic Courtès
2022-11-09 21:56   ` [bug#59003] [PATCH v2 5/6] installer: Error page width is parameterized Ludovic Courtès
2022-11-09 21:56   ` [bug#59003] [PATCH v2 6/6] installer: Report known-unsupported PCI devices Ludovic Courtès
2022-11-11 11:08     ` pelzflorian (Florian Pelz)
2022-11-15 11:24       ` bug#59003: [PATCH 0/7] [Installer] Warn about unsupported devices Ludovic Courtès
2022-11-15 18:28         ` [bug#59003] " pelzflorian (Florian Pelz)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221109215637.22445-3-ludo@gnu.org \
    --to=ludo@gnu.org \
    --cc=59003@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.