forked from linrunner/TLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlp-pcilist
executable file
·64 lines (58 loc) · 1.91 KB
/
tlp-pcilist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl
# tlp-pcilist - list pci devices with runtime pm mode and device class
#
# Copyright (c) 2016 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.
package tlp_pcilist;
use strict;
use warnings;
# Read content from a sysfile
# $_[0]: input file
# return: content / empty string if nonexistent or not readable
sub catsysf {
my $sysval = "";
if (open SYSF, "$_[0]") {
chomp ($sysval = <SYSF>);
close SYSF;
}
return $sysval;
}
# Read device driver from DEVICE/uevent
# $_[0]: (sub)device base path
# return: driver / empty string if uevent nonexistent or not readable
sub getdriver {
my $driver = "";
if ( open (SYSF, "$_[0]/uevent") ) {
# read file line by line
while (<SYSF>) {
# match line content and return DRIVER= value
if ( s/^DRIVER=(.*)/$1/ ) {
chomp ($driver = $_);
last; # break loop
}
}
close (SYSF);
}
return $driver
}
# Output device list with Runtime PM mode and device class
foreach (`lspci -m`) {
# parse lspci output: get short PCI(e) id and long description of device
my ($dev, $classdesc) = /(\S+) \"(.+?)\"/;
# join device path
my $devp = "/sys/bus/pci/devices/0000:$dev";
# control file for Runtime PM
my $devc = "$devp/power/control";
# get device class
my $class = catsysf ("$devp/class");
# get device driver
my $driver = getdriver ("$devp") || "no driver";
if (-f $devc) { # control file exists
# get Runtime PM mode
my $pmode = catsysf ("$devc");
# output pm mode and device data
printf "%s/power/control = %-4s (%s, %s, %s)\n", $devp, $pmode, $class, $classdesc, $driver;
} else { # control file missing --> output device data only
printf "%s/power/control = (not available) (%s, %s, %s)\n", $devp, $class, $classdesc, $driver;
}
}