Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

get-pci-ids 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. # Known bugs/limitations:
  16. # - Does not recognize all drivers because some require special cflags.
  17. # Fails also on some drivers that do belong to other architectures
  18. # than the one of the machine this script is running on.
  19. # This is currently not so important because all drivers that have an
  20. # Etherboot counterpart are recognized.
  21. use strict;
  22. use File::Basename "dirname";
  23. use POSIX "uname";
  24. # Where to find the kernel sources
  25. my $kernel_src = "/usr/src/linux";
  26. if($#ARGV >= 0) {
  27. $kernel_src = shift;
  28. }
  29. # Sanity checks
  30. if($#ARGV >= 0) {
  31. print STDERR "Too many arguments.\n";
  32. print STDERR "Usage: get-pci-ids [path to kernel sources]\n";
  33. print STDERR " /usr/src/linux is assumed if no path is given.\n";
  34. exit 1;
  35. }
  36. unless(-f "$kernel_src/include/linux/version.h") {
  37. print STDERR "Could not find $kernel_src/include/linux/version.h.\n";
  38. print STDERR "$kernel_src is probably no Linux kernel source tree.\n";
  39. exit 1;
  40. }
  41. # Flags that are needed to preprocess the drivers.
  42. # Some drivers need optimization
  43. my $cflags="-D__KERNEL__ -I$kernel_src/include -I$kernel_src/net/inet -O2";
  44. # The C preprocessor. It needs to spit out the preprocessed source on stdout.
  45. my $cpp="gcc -E";
  46. # List of drivers. We parse every .c file. It does not harm if it does not contain a driver.
  47. my @drivers = split /\s+/, `find $kernel_src/drivers/net -name '*.c' | sort`;
  48. # Kernel version
  49. my $version = `grep UTS_RELEASE $kernel_src/include/linux/version.h`;
  50. chomp $version;
  51. $version =~ s/\s*#define\s+UTS_RELEASE\s+"(\S+)".*$/$1/g;
  52. # Architecture
  53. my @uname = uname();
  54. # Print header
  55. print "# PCI vendor/device ids extracted from Linux $version on $uname[4] at " . gmtime() . "\n";
  56. my $driver;
  57. # Process the drivers
  58. foreach $driver (@drivers) {
  59. # Preprocess to expand macros
  60. my $command = "$cpp $cflags -I" . dirname($driver) . " $driver";
  61. open DRIVER, "$command |" or die "Could not execute\n\"$command\".\n";
  62. # Extract the pci_device_id structure
  63. my $found = 0;
  64. my $line = "";
  65. my @lines;
  66. while(<DRIVER>) {
  67. if(/^\s*static\s+struct\s+pci_device_id/) {
  68. # This file contains a driver. Print the name.
  69. $driver =~ s!$kernel_src/drivers/net/!!g;
  70. print "\n$driver\n";
  71. $found = 1;
  72. next;
  73. }
  74. if($found == 1){
  75. if(/\};/ or /{\s*0\s*,?\s*}/) {
  76. # End of struct
  77. $found = 0;
  78. } else {
  79. chomp;
  80. if(/\}\s*,?\s*\n?$/) {
  81. # This line contains a full entry or the last part of it.
  82. $_ = $line . $_;
  83. $line = "";
  84. s/[,\{\};\(\)]//g; # Strip punctuation
  85. s/^\s+//g; # Eat whitespace at beginning of line
  86. tr[A-Z][a-z]; # Convert to lowercase
  87. # Push the vendor and device id to @lines if this line is not empty.
  88. # We ignore everything else that might be there
  89. my ($vendor_id, $device_id, $remainder) = split /\W+/, $_, 3;
  90. push @lines, "$vendor_id $device_id\n" if($vendor_id && $device_id);
  91. } else {
  92. # This line does contain a partial entry. Remember it.
  93. $line .= "$_ ";
  94. }
  95. }
  96. }
  97. }
  98. close DRIVER; # No "or die", because $cpp fails on some files
  99. # Now print out the sorted values
  100. @lines = sort @lines;
  101. my $lastline = "";
  102. foreach(@lines) {
  103. # Print each vendor/device id combination only once.
  104. # Some drivers (e.g. e100) do contain subfamilies
  105. print if($_ ne $lastline);
  106. $lastline = $_;
  107. }
  108. }