Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / util / get-pci-ids
1 #! /usr/bin/perl -w
2
3 # get-pci-ids: extract pci vendor/device ids from linux net drivers
4
5 # Copyright (C) 2003 Georg Baum <gbaum@users.sf.net>
6
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.
11
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.
16
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
20 # 02110-1301, USA.
21
22
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.
29
30
31 use strict;
32 use File::Basename "dirname";
33 use POSIX "uname";
34
35 # Where to find the kernel sources
36 my $kernel_src = "/usr/src/linux";
37
38 if($#ARGV >= 0) {
39         $kernel_src = shift;
40 }
41
42 # Sanity checks
43 if($#ARGV >= 0) {
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";
47         exit 1;
48 }
49
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";
53         exit 1;
54 }
55
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";
59
60 # The C preprocessor. It needs to spit out the preprocessed source on stdout.
61 my $cpp="gcc -E";
62
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`;
65
66 # Kernel version
67 my $version = `grep UTS_RELEASE $kernel_src/include/linux/version.h`;
68 chomp $version;
69 $version =~ s/\s*#define\s+UTS_RELEASE\s+"(\S+)".*$/$1/g;
70
71 # Architecture
72 my @uname = uname();
73
74
75 # Print header
76 print "# PCI vendor/device ids extracted from Linux $version on $uname[4] at " . gmtime() . "\n";
77
78 my $driver;
79
80 # Process the drivers
81 foreach $driver (@drivers) {
82
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";
86
87         # Extract the pci_device_id structure
88         my $found = 0;
89         my $line = "";
90         my @lines;
91         while(<DRIVER>) {
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;
95                         print "\n$driver\n";
96                         $found = 1;
97                         next;
98                 }
99                 if($found == 1){
100                         if(/\};/ or /{\s*0\s*,?\s*}/) {
101                                 # End of struct
102                                 $found = 0;
103                         } else {
104                                 chomp;
105                                 if(/\}\s*,?\s*\n?$/) {
106                                         # This line contains a full entry or the last part of it.
107                                         $_ = $line . $_;
108                                         $line = "";
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);
116                                 } else {
117                                         # This line does contain a partial entry. Remember it.
118                                         $line .= "$_ ";
119                                 }
120                         }
121                 }
122         }
123         close DRIVER;           # No "or die", because $cpp fails on some files
124
125         # Now print out the sorted values
126         @lines = sort @lines;
127         my $lastline = "";
128         foreach(@lines) {
129                 # Print each vendor/device id combination only once.
130                 # Some drivers (e.g. e100) do contain subfamilies
131                 print if($_ ne $lastline);
132                 $lastline = $_;
133         }
134 }
135
136