a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync#! /usr/bin/perl -w
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# get-pci-ids: extract pci vendor/device ids from linux net drivers
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Copyright (C) 2003 Georg Baum <gbaum@users.sf.net>
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# This program is free software; you can redistribute it and/or modify
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# it under the terms of the GNU General Public License as published by
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# the Free Software Foundation; either version 2 of the License, or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# (at your option) any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# This program is distributed in the hope that it will be useful,
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# but WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# GNU General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Known bugs/limitations:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# - Does not recognize all drivers because some require special cflags.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Fails also on some drivers that do belong to other architectures
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# than the one of the machine this script is running on.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# This is currently not so important because all drivers that have an
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Etherboot counterpart are recognized.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncuse strict;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncuse File::Basename "dirname";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncuse POSIX "uname";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Where to find the kernel sources
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy $kernel_src = "/usr/src/linux";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncif($#ARGV >= 0) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $kernel_src = shift;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Sanity checks
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncif($#ARGV >= 0) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print STDERR "Too many arguments.\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print STDERR "Usage: get-pci-ids [path to kernel sources]\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print STDERR " /usr/src/linux is assumed if no path is given.\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync exit 1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncunless(-f "$kernel_src/include/linux/version.h") {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print STDERR "Could not find $kernel_src/include/linux/version.h.\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print STDERR "$kernel_src is probably no Linux kernel source tree.\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync exit 1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Flags that are needed to preprocess the drivers.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Some drivers need optimization
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy $cflags="-D__KERNEL__ -I$kernel_src/include -I$kernel_src/net/inet -O2";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# The C preprocessor. It needs to spit out the preprocessed source on stdout.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy $cpp="gcc -E";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# List of drivers. We parse every .c file. It does not harm if it does not contain a driver.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy @drivers = split /\s+/, `find $kernel_src/drivers/net -name '*.c' | sort`;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Kernel version
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy $version = `grep UTS_RELEASE $kernel_src/include/linux/version.h`;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncchomp $version;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync$version =~ s/\s*#define\s+UTS_RELEASE\s+"(\S+)".*$/$1/g;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Architecture
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy @uname = uname();
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Print header
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncprint "# PCI vendor/device ids extracted from Linux $version on $uname[4] at " . gmtime() . "\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncmy $driver;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync# Process the drivers
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncforeach $driver (@drivers) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Preprocess to expand macros
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my $command = "$cpp $cflags -I" . dirname($driver) . " $driver";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync open DRIVER, "$command |" or die "Could not execute\n\"$command\".\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Extract the pci_device_id structure
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my $found = 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my $line = "";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my @lines;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync while(<DRIVER>) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if(/^\s*static\s+struct\s+pci_device_id/) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # This file contains a driver. Print the name.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $driver =~ s!$kernel_src/drivers/net/!!g;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print "\n$driver\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $found = 1;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync next;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if($found == 1){
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if(/\};/ or /{\s*0\s*,?\s*}/) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # End of struct
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $found = 0;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync } else {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync chomp;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if(/\}\s*,?\s*\n?$/) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # This line contains a full entry or the last part of it.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $_ = $line . $_;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $line = "";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync s/[,\{\};\(\)]//g; # Strip punctuation
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync s/^\s+//g; # Eat whitespace at beginning of line
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync tr[A-Z][a-z]; # Convert to lowercase
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Push the vendor and device id to @lines if this line is not empty.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # We ignore everything else that might be there
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my ($vendor_id, $device_id, $remainder) = split /\W+/, $_, 3;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync push @lines, "$vendor_id $device_id\n" if($vendor_id && $device_id);
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync } else {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # This line does contain a partial entry. Remember it.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $line .= "$_ ";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync close DRIVER; # No "or die", because $cpp fails on some files
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Now print out the sorted values
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync @lines = sort @lines;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync my $lastline = "";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync foreach(@lines) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Print each vendor/device id combination only once.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync # Some drivers (e.g. e100) do contain subfamilies
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print if($_ ne $lastline);
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $lastline = $_;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync }
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync}
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync