You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

get-pci-ids 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #! /usr/bin/perl -w
  2. # get-pci-ids: extract pci vendor/device ids from linux net drivers
  3. # Copyright (C) 2003 Georg Baum <gbaum@users.sf.net>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. # 02110-1301, USA.
  16. # Known bugs/limitations:
  17. # - Does not recognize all drivers because some require special cflags.
  18. # Fails also on some drivers that do belong to other architectures
  19. # than the one of the machine this script is running on.
  20. # This is currently not so important because all drivers that have an
  21. # Etherboot counterpart are recognized.
  22. use strict;
  23. use File::Basename "dirname";
  24. use POSIX "uname";
  25. # Where to find the kernel sources
  26. my $kernel_src = "/usr/src/linux";
  27. if($#ARGV >= 0) {
  28. $kernel_src = shift;
  29. }
  30. # Sanity checks
  31. if($#ARGV >= 0) {
  32. print STDERR "Too many arguments.\n";
  33. print STDERR "Usage: get-pci-ids [path to kernel sources]\n";
  34. print STDERR " /usr/src/linux is assumed if no path is given.\n";
  35. exit 1;
  36. }
  37. unless(-f "$kernel_src/include/linux/version.h") {
  38. print STDERR "Could not find $kernel_src/include/linux/version.h.\n";
  39. print STDERR "$kernel_src is probably no Linux kernel source tree.\n";
  40. exit 1;
  41. }
  42. # Flags that are needed to preprocess the drivers.
  43. # Some drivers need optimization
  44. my $cflags="-D__KERNEL__ -I$kernel_src/include -I$kernel_src/net/inet -O2";
  45. # The C preprocessor. It needs to spit out the preprocessed source on stdout.
  46. my $cpp="gcc -E";
  47. # List of drivers. We parse every .c file. It does not harm if it does not contain a driver.
  48. my @drivers = split /\s+/, `find $kernel_src/drivers/net -name '*.c' | sort`;
  49. # Kernel version
  50. my $version = `grep UTS_RELEASE $kernel_src/include/linux/version.h`;
  51. chomp $version;
  52. $version =~ s/\s*#define\s+UTS_RELEASE\s+"(\S+)".*$/$1/g;
  53. # Architecture
  54. my @uname = uname();
  55. # Print header
  56. print "# PCI vendor/device ids extracted from Linux $version on $uname[4] at " . gmtime() . "\n";
  57. my $driver;
  58. # Process the drivers
  59. foreach $driver (@drivers) {
  60. # Preprocess to expand macros
  61. my $command = "$cpp $cflags -I" . dirname($driver) . " $driver";
  62. open DRIVER, "$command |" or die "Could not execute\n\"$command\".\n";
  63. # Extract the pci_device_id structure
  64. my $found = 0;
  65. my $line = "";
  66. my @lines;
  67. while(<DRIVER>) {
  68. if(/^\s*static\s+struct\s+pci_device_id/) {
  69. # This file contains a driver. Print the name.
  70. $driver =~ s!$kernel_src/drivers/net/!!g;
  71. print "\n$driver\n";
  72. $found = 1;
  73. next;
  74. }
  75. if($found == 1){
  76. if(/\};/ or /{\s*0\s*,?\s*}/) {
  77. # End of struct
  78. $found = 0;
  79. } else {
  80. chomp;
  81. if(/\}\s*,?\s*\n?$/) {
  82. # This line contains a full entry or the last part of it.
  83. $_ = $line . $_;
  84. $line = "";
  85. s/[,\{\};\(\)]//g; # Strip punctuation
  86. s/^\s+//g; # Eat whitespace at beginning of line
  87. tr[A-Z][a-z]; # Convert to lowercase
  88. # Push the vendor and device id to @lines if this line is not empty.
  89. # We ignore everything else that might be there
  90. my ($vendor_id, $device_id, $remainder) = split /\W+/, $_, 3;
  91. push @lines, "$vendor_id $device_id\n" if($vendor_id && $device_id);
  92. } else {
  93. # This line does contain a partial entry. Remember it.
  94. $line .= "$_ ";
  95. }
  96. }
  97. }
  98. }
  99. close DRIVER; # No "or die", because $cpp fails on some files
  100. # Now print out the sorted values
  101. @lines = sort @lines;
  102. my $lastline = "";
  103. foreach(@lines) {
  104. # Print each vendor/device id combination only once.
  105. # Some drivers (e.g. e100) do contain subfamilies
  106. print if($_ ne $lastline);
  107. $lastline = $_;
  108. }
  109. }