3 # get-pci-ids: extract pci vendor/device ids from linux net drivers
5 # Copyright (C) 2003 Georg Baum <gbaum@users.sf.net>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 # Known bugs/limitations:
24 # - Does not recognize all drivers because some require special cflags.
25 # Fails also on some drivers that do belong to other architectures
26 # than the one of the machine this script is running on.
27 # This is currently not so important because all drivers that have an
28 # Etherboot counterpart are recognized.
32 use File::Basename "dirname";
35 # Where to find the kernel sources
36 my $kernel_src = "/usr/src/linux";
44 print STDERR "Too many arguments.\n";
45 print STDERR "Usage: get-pci-ids [path to kernel sources]\n";
46 print STDERR " /usr/src/linux is assumed if no path is given.\n";
50 unless(-f "$kernel_src/include/linux/version.h") {
51 print STDERR "Could not find $kernel_src/include/linux/version.h.\n";
52 print STDERR "$kernel_src is probably no Linux kernel source tree.\n";
56 # Flags that are needed to preprocess the drivers.
57 # Some drivers need optimization
58 my $cflags="-D__KERNEL__ -I$kernel_src/include -I$kernel_src/net/inet -O2";
60 # The C preprocessor. It needs to spit out the preprocessed source on stdout.
63 # List of drivers. We parse every .c file. It does not harm if it does not contain a driver.
64 my @drivers = split /\s+/, `find $kernel_src/drivers/net -name '*.c' | sort`;
67 my $version = `grep UTS_RELEASE $kernel_src/include/linux/version.h`;
69 $version =~ s/\s*#define\s+UTS_RELEASE\s+"(\S+)".*$/$1/g;
76 print "# PCI vendor/device ids extracted from Linux $version on $uname[4] at " . gmtime() . "\n";
81 foreach $driver (@drivers) {
83 # Preprocess to expand macros
84 my $command = "$cpp $cflags -I" . dirname($driver) . " $driver";
85 open DRIVER, "$command |" or die "Could not execute\n\"$command\".\n";
87 # Extract the pci_device_id structure
92 if(/^\s*static\s+struct\s+pci_device_id/) {
93 # This file contains a driver. Print the name.
94 $driver =~ s!$kernel_src/drivers/net/!!g;
100 if(/\};/ or /{\s*0\s*,?\s*}/) {
105 if(/\}\s*,?\s*\n?$/) {
106 # This line contains a full entry or the last part of it.
109 s/[,\{\};\(\)]//g; # Strip punctuation
110 s/^\s+//g; # Eat whitespace at beginning of line
111 tr[A-Z][a-z]; # Convert to lowercase
112 # Push the vendor and device id to @lines if this line is not empty.
113 # We ignore everything else that might be there
114 my ($vendor_id, $device_id, $remainder) = split /\W+/, $_, 3;
115 push @lines, "$vendor_id $device_id\n" if($vendor_id && $device_id);
117 # This line does contain a partial entry. Remember it.
123 close DRIVER; # No "or die", because $cpp fails on some files
125 # Now print out the sorted values
126 @lines = sort @lines;
129 # Print each vendor/device id combination only once.
130 # Some drivers (e.g. e100) do contain subfamilies
131 print if($_ ne $lastline);