| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Author: Petter Reinholdtsen <[email protected]> |
| 4 |
# Date: 2006-08-12 |
| 5 |
# License: GNU GPL |
| 6 |
# |
| 7 |
# Read pciutils pci.ids and write it out as discover XML |
| 8 |
# |
| 9 |
# pciutils (card names) (Latest on http://pciids.sourceforge.net/pci.ids.gz) |
| 10 |
# /usr/share/misc/pci.ids |
| 11 |
|
| 12 |
use strict; |
| 13 |
use warnings; |
| 14 |
|
| 15 |
use Getopt::Std; |
| 16 |
|
| 17 |
use vars qw($bus %pci $debug); |
| 18 |
|
| 19 |
$debug = 1; |
| 20 |
|
| 21 |
sub load_pciutils_names { |
| 22 |
my ($filename) = @_; |
| 23 |
|
| 24 |
my $type; # card or device |
| 25 |
|
| 26 |
my $tmp; # Store the current superclass/vendor |
| 27 |
my $lastid; |
| 28 |
|
| 29 |
open(PCIFILE, "<$filename") || die "Unable to read $filename"; |
| 30 |
while (<PCIFILE>) { |
| 31 |
chomp; |
| 32 |
|
| 33 |
last if ('usb' eq $bus && m/List of known device classes/); |
| 34 |
|
| 35 |
# Remove comments |
| 36 |
s/^\#.+$//; |
| 37 |
|
| 38 |
# Skip empty lines |
| 39 |
next if /^\s*$/; |
| 40 |
|
| 41 |
if (m/^(\S{2,})\s+(.+)$/) { |
| 42 |
# Vendor info |
| 43 |
my ($id, $desc) = ($1, $2); |
| 44 |
$type = 'card'; |
| 45 |
|
| 46 |
if ( ! defined $pci{'vendor'}{$id}{'desc'} && defined $desc) { |
| 47 |
$pci{'vendor'}{$id}{'desc'} = $desc; |
| 48 |
} |
| 49 |
|
| 50 |
$tmp = $id; |
| 51 |
} elsif (m/^(\S) (..)\s+(.+)$/) { |
| 52 |
# Device classes |
| 53 |
my ($foo, $bar, $desc) = ($1, $2, $3); |
| 54 |
$type = 'device'; |
| 55 |
|
| 56 |
my $id = "$foo|$bar"; |
| 57 |
|
| 58 |
$pci{'class'}{$id} = $desc; |
| 59 |
|
| 60 |
$tmp = $id; |
| 61 |
} elsif (m/^\t(\S+)\s+(.+)$/) { |
| 62 |
# Card info - using the vendor ID or |
| 63 |
# Device subclass using the device class id |
| 64 |
my ($id, $desc) = ($1, $2); |
| 65 |
|
| 66 |
# Add the vendor id |
| 67 |
$id = "$tmp$id"; |
| 68 |
$lastid = $id; |
| 69 |
|
| 70 |
if ("card" eq $type) { |
| 71 |
$pci{'vendor'}{$tmp}{'cards'}{"$id"} = $desc; |
| 72 |
|
| 73 |
if ( ! defined $pci{'card'}{"$id"}{'desc'} && defined $desc) { |
| 74 |
$pci{'card'}{"$id"}{'desc'} = $desc; |
| 75 |
} |
| 76 |
} elsif ("device" eq $type) { |
| 77 |
} else { |
| 78 |
die "Unknown type"; |
| 79 |
} |
| 80 |
} elsif (m/^\t\t(\S+)\s(\S+)\s+(.+)$/) { # Handle subtypes |
| 81 |
if ("card" eq $type) { |
| 82 |
# print "Subtype: $1 $2 = $3\n"; |
| 83 |
$pci{'card'}{"$lastid"}{'subtype'}{"$1$2"} = $3; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
close(PCIFILE); |
| 88 |
} |
| 89 |
|
| 90 |
sub to_xml_entity { |
| 91 |
my $string = shift; |
| 92 |
return unless defined $string; |
| 93 |
$string =~ s/&/&/g; |
| 94 |
$string =~ s/</</g; |
| 95 |
$string =~ s/>/>/g; |
| 96 |
$string =~ s/'/'/g; |
| 97 |
return $string; |
| 98 |
} |
| 99 |
|
| 100 |
sub save_xml_pci { |
| 101 |
my ($vendorfile, $devicefile) = @_; |
| 102 |
open(V, ">$vendorfile") || die $!; |
| 103 |
print V <<EOF; |
| 104 |
<?xml version='1.0' encoding='UTF-8'?> |
| 105 |
<vendor_list bus='$bus'> |
| 106 |
EOF |
| 107 |
for my $vendorid (sort keys %{$pci{'vendor'}}) { |
| 108 |
my $name = to_xml_entity($pci{'vendor'}{$vendorid}{'desc'}); |
| 109 |
print V " <vendor id='$vendorid' name='$name'/>\n"; |
| 110 |
} |
| 111 |
print V <<EOF; |
| 112 |
</vendor_list> |
| 113 |
EOF |
| 114 |
close(V); |
| 115 |
|
| 116 |
open(V, ">$devicefile") || die $!; |
| 117 |
print V <<EOF; |
| 118 |
<?xml version='1.0' encoding='UTF-8'?> |
| 119 |
<device_list bus='$bus'> |
| 120 |
EOF |
| 121 |
for my $id (sort keys %{$pci{'card'}}) { |
| 122 |
my ($vendorid, $modelid) = $id =~ m/^(....)(....)/; |
| 123 |
my $name = to_xml_entity($pci{'card'}{"$id"}{'desc'}); |
| 124 |
print V " <device vendor='$vendorid' model='$modelid' model_name='$name'/>\n"; |
| 125 |
if (exists $pci{'card'}{"$id"}{'subtype'}) { |
| 126 |
for my $subid (sort keys %{$pci{'card'}{"$id"}{'subtype'}}) { |
| 127 |
my ($subvendorid, $submodelid) = $subid =~ m/^(....)(....)/; |
| 128 |
my $subname = |
| 129 |
to_xml_entity($pci{'card'}{"$id"}{'subtype'}{$subid}); |
| 130 |
print V " <device vendor='$vendorid' model='$modelid' subvendor='$subvendorid' subdevice='$submodelid' model_name='$name' subsystem_name='$subname'/>\n"; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
print V <<EOF; |
| 136 |
</device_list> |
| 137 |
EOF |
| 138 |
} |
| 139 |
|
| 140 |
my %opts; |
| 141 |
getopts("b:i:v:d:", \%opts); |
| 142 |
|
| 143 |
$bus = $opts{b} || 'pci'; |
| 144 |
|
| 145 |
load_pciutils_names($opts{i} || "pci.ids"); |
| 146 |
|
| 147 |
save_xml_pci($opts{v} || "new-pci-vendor.xml", $opts{d} || "new-pci-device.xml"); |