diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2022-11-03 14:36:21 +0100 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2022-11-15 12:16:42 +0100 |
| commit | 655fb8feacfcb16da784ad88201663bb6fb566e4 (patch) | |
| tree | 6754f59cee28e982ce02af6063a598b2a5f82450 /gnu/build | |
| parent | 4f7ffb97a4c2f2ee7aa1c54c23a97e7c0447c08c (diff) | |
linux-modules: Add support for listing PCI devices.
* 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.
Diffstat (limited to 'gnu/build')
| -rw-r--r-- | gnu/build/linux-modules.scm | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/gnu/build/linux-modules.scm b/gnu/build/linux-modules.scm index 053720574b1..09cf752bef4 100644 --- a/gnu/build/linux-modules.scm +++ b/gnu/build/linux-modules.scm | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2014, 2016, 2018, 2019 Ludovic Courtès <ludo@gnu.org> | 2 | ;;; Copyright © 2014, 2016, 2018, 2019, 2022 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> | 3 | ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com> |
| 4 | ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org> | 4 | ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org> |
| 5 | ;;; | 5 | ;;; |
| @@ -28,6 +28,7 @@ | |||
| 28 | #:use-module (rnrs io ports) | 28 | #:use-module (rnrs io ports) |
| 29 | #:use-module (rnrs bytevectors) | 29 | #:use-module (rnrs bytevectors) |
| 30 | #:use-module (srfi srfi-1) | 30 | #:use-module (srfi srfi-1) |
| 31 | #:use-module (srfi srfi-9 gnu) | ||
| 31 | #:use-module (srfi srfi-11) | 32 | #:use-module (srfi srfi-11) |
| 32 | #:use-module (srfi srfi-26) | 33 | #:use-module (srfi srfi-26) |
| 33 | #:use-module (ice-9 ftw) | 34 | #:use-module (ice-9 ftw) |
| @@ -50,6 +51,16 @@ | |||
| 50 | load-linux-module* | 51 | load-linux-module* |
| 51 | load-linux-modules-from-directory | 52 | load-linux-modules-from-directory |
| 52 | 53 | ||
| 54 | pci-devices | ||
| 55 | pci-device? | ||
| 56 | pci-device-vendor | ||
| 57 | pci-device-id | ||
| 58 | pci-device-class | ||
| 59 | pci-device-module-alias | ||
| 60 | storage-pci-device? | ||
| 61 | network-pci-device? | ||
| 62 | display-pci-device? | ||
| 63 | |||
| 53 | current-module-debugging-port | 64 | current-module-debugging-port |
| 54 | 65 | ||
| 55 | device-module-aliases | 66 | device-module-aliases |
| @@ -429,6 +440,54 @@ key such as 'MAJOR or 'DEVTYPE and each cdr is the corresponding value." | |||
| 429 | (line | 440 | (line |
| 430 | (loop (cons (key=value->pair line) result)))))) | 441 | (loop (cons (key=value->pair line) result)))))) |
| 431 | 442 | ||
| 443 | ;; PCI device known to the Linux kernel. | ||
| 444 | (define-immutable-record-type <pci-device> | ||
| 445 | (pci-device vendor device class module-alias) | ||
| 446 | pci-device? | ||
| 447 | (vendor pci-device-vendor) ;integer | ||
| 448 | (device pci-device-id) ;integer | ||
| 449 | (class pci-device-class) ;integer | ||
| 450 | (module-alias pci-device-module-alias)) ;string | #f | ||
| 451 | |||
| 452 | (define (pci-device-class-predicate mask bits) | ||
| 453 | (lambda (device) | ||
| 454 | "Return true if DEVICE has the chosen class." | ||
| 455 | (= (logand mask (pci-device-class device)) bits))) | ||
| 456 | |||
| 457 | (define storage-pci-device? ;"Mass storage controller" class | ||
| 458 | (pci-device-class-predicate #xff0000 #x010000)) | ||
| 459 | (define network-pci-device? ;"Network controller" class | ||
| 460 | (pci-device-class-predicate #xff0000 #x020000)) | ||
| 461 | (define display-pci-device? ;"Display controller" class | ||
| 462 | (pci-device-class-predicate #xff0000 #x030000)) | ||
| 463 | |||
| 464 | (define (pci-devices) | ||
| 465 | "Return the list of PCI devices of the system (<pci-device> records)." | ||
| 466 | (define (read-hex port) | ||
| 467 | (let ((line (read-line port))) | ||
| 468 | (and (string? line) | ||
| 469 | (string-prefix? "0x" line) | ||
| 470 | (string->number (string-drop line 2) 16)))) | ||
| 471 | |||
| 472 | (filter-map (lambda (directory) | ||
| 473 | (define properties | ||
| 474 | (call-with-input-file (string-append directory "/uevent") | ||
| 475 | read-uevent)) | ||
| 476 | (define vendor | ||
| 477 | (call-with-input-file (string-append directory "/vendor") | ||
| 478 | read-hex)) | ||
| 479 | (define device | ||
| 480 | (call-with-input-file (string-append directory "/device") | ||
| 481 | read-hex)) | ||
| 482 | (define class | ||
| 483 | (call-with-input-file (string-append directory "/class") | ||
| 484 | read-hex)) | ||
| 485 | |||
| 486 | (pci-device vendor device class | ||
| 487 | (assq-ref properties 'MODALIAS))) | ||
| 488 | (find-files "/sys/bus/pci/devices" | ||
| 489 | #:stat lstat))) | ||
| 490 | |||
| 432 | (define (device-module-aliases device) | 491 | (define (device-module-aliases device) |
| 433 | "Return the list of module aliases required by DEVICE, a /dev file name, as | 492 | "Return the list of module aliases required by DEVICE, a /dev file name, as |
| 434 | in this example: | 493 | in this example: |
