| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
# lst2xml: Transform old (pci.lst) Discover data to new XML-based format |
| 4 |
|
| 5 |
# AUTHORS: Eric Gillespie <[email protected]> |
| 6 |
# John R. Daily <[email protected]> |
| 7 |
# Josh Bressers <[email protected]> |
| 8 |
# |
| 9 |
# Copyright 2002 Progeny Linux Systems, Inc. |
| 10 |
# Copyright 2002 Hewlett-Packard Company |
| 11 |
# |
| 12 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 13 |
# copy of this software and associated documentation files (the "Software"), |
| 14 |
# to deal in the Software without restriction, including without limitation |
| 15 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 16 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 17 |
# Software is furnished to do so, subject to the following conditions: |
| 18 |
# |
| 19 |
# The above copyright notice and this permission notice shall be included in |
| 20 |
# all copies or substantial portions of the Software. |
| 21 |
# |
| 22 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 25 |
# THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 28 |
# DEALINGS IN THE SOFTWARE. |
| 29 |
|
| 30 |
# $Progeny$ |
| 31 |
|
| 32 |
use strict; |
| 33 |
use warnings; |
| 34 |
|
| 35 |
my %CLASSES = ( |
| 36 |
'isdn'=>'isdn', |
| 37 |
'bridge'=>'bridge', |
| 38 |
'ethernet'=>'network', |
| 39 |
'unknown'=>'unknown', |
| 40 |
'video'=>'video', |
| 41 |
'ide'=>'ide', |
| 42 |
'scsi'=>'scsi', |
| 43 |
'usb'=>'usb', |
| 44 |
'sound'=>'sound', |
| 45 |
'modem'=>'modem', |
| 46 |
'tvcard'=>'multimedia', |
| 47 |
'joystick'=>'gameport', |
| 48 |
'scanner'=>'scanner', |
| 49 |
'printer'=>'printer', |
| 50 |
'webcam'=>'webcam', |
| 51 |
'floppy'=>'floppy', |
| 52 |
'mouse'=>0 # We don't care about this in Discover |
| 53 |
); |
| 54 |
|
| 55 |
my %PCI_CLASSES = ( |
| 56 |
'isdn'=>'0204', |
| 57 |
'bridge'=>'0600', |
| 58 |
'network'=>'0200', |
| 59 |
'unknown'=>'0000', |
| 60 |
'video'=>'0300', |
| 61 |
'ide'=>'0101', |
| 62 |
'scsi'=>'0100', |
| 63 |
'usb'=>'0c03', |
| 64 |
'sound'=>'0401', |
| 65 |
'modem'=>'0703', |
| 66 |
'multimedia'=>'0400', # Used to be 'tvcard' |
| 67 |
'gameport'=>'0904' # Used to be 'joystick' |
| 68 |
); |
| 69 |
|
| 70 |
my %PCMCIA_CLASSES = ( |
| 71 |
'isdn'=>'isdn', |
| 72 |
'bridge'=>'bridge', |
| 73 |
'network'=>'network', |
| 74 |
'unknown'=>'unknown', |
| 75 |
'video'=>'video', |
| 76 |
'sound'=>'sound', |
| 77 |
'modem'=>'modem', |
| 78 |
'multimedia'=>'multimedia', |
| 79 |
'gameport'=>'gameport' |
| 80 |
); |
| 81 |
|
| 82 |
my %USB_CLASSES = ( |
| 83 |
'unknown'=>'0000', |
| 84 |
'scanner'=>'ffff', # XXX ??? |
| 85 |
'printer'=>'0101', |
| 86 |
'isdn'=>'1030', |
| 87 |
'webcam'=>'ff00', |
| 88 |
'network'=>'0206', |
| 89 |
'modem'=>'0202', |
| 90 |
'gameport'=>'0300', |
| 91 |
'floppy'=>'0804' |
| 92 |
); |
| 93 |
|
| 94 |
my %REMAP_CLASSES = ( |
| 95 |
'isdn'=>'broadband', |
| 96 |
'bridge'=>'bridge', |
| 97 |
'ethernet'=>'network', |
| 98 |
'unknown'=>'miscellaneous', |
| 99 |
'video'=>'display', |
| 100 |
'ide'=>'bridge', |
| 101 |
'scsi'=>'bridge', |
| 102 |
'usb'=>'bridge', |
| 103 |
'sound'=>'audio', |
| 104 |
'modem'=>'modem', |
| 105 |
'multimedia'=>'video', |
| 106 |
'tvcard'=>'video', |
| 107 |
'joystick'=>'bridge', |
| 108 |
'gameport'=>'bridge', |
| 109 |
'scanner'=>'imaging', |
| 110 |
'printer'=>'printer', |
| 111 |
'webcam'=>'video', |
| 112 |
'floppy'=>'removabledisk', |
| 113 |
'network'=>'network' |
| 114 |
); |
| 115 |
|
| 116 |
sub encode_non_ascii { |
| 117 |
my ($value) = @_; |
| 118 |
my ($i, $newval, @chars); |
| 119 |
|
| 120 |
@chars = unpack('C*', $value); |
| 121 |
|
| 122 |
for($i = 0; $i < @chars; $i++) { |
| 123 |
if ($chars[$i] > 127) { |
| 124 |
$newval = sprintf("&#x%X;", $chars[$i]); |
| 125 |
|
| 126 |
if ($i == 0) { |
| 127 |
$value = $newval . substr($value, 1); |
| 128 |
} elsif ($i == @chars - 1) { |
| 129 |
$value = substr($value, 0, $i) . $newval; |
| 130 |
} else { |
| 131 |
$value = substr($value, 0, $i) . $newval . |
| 132 |
substr($value, $i+1); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
return $value; |
| 137 |
} |
| 138 |
|
| 139 |
sub make_busclass { |
| 140 |
my $bus = shift; |
| 141 |
my $bus_upper = shift; |
| 142 |
my $id; |
| 143 |
my $class; |
| 144 |
my $try; |
| 145 |
my @keys; |
| 146 |
|
| 147 |
if (not open(BUSCLASSFOO, ">${bus}-busclass.xml")) { |
| 148 |
die("Could not open ${bus}-busclass.xml\n"); |
| 149 |
} |
| 150 |
|
| 151 |
print(BUSCLASSFOO <<EOF); |
| 152 |
<?xml version='1.0' encoding='UTF-8'?> |
| 153 |
|
| 154 |
<busclass_list bus="$bus"> |
| 155 |
EOF |
| 156 |
|
| 157 |
$try = '@keys = keys(%'; |
| 158 |
$try .= $bus_upper; |
| 159 |
$try .= '_CLASSES);'; |
| 160 |
eval $try; |
| 161 |
|
| 162 |
foreach $class (@keys) { |
| 163 |
$try = '$id = $'; # 'cperl-mode is sub-optimal |
| 164 |
$try .= $bus_upper; |
| 165 |
$try .= '_CLASSES{$class};'; |
| 166 |
eval $try; |
| 167 |
|
| 168 |
print(BUSCLASSFOO " <busclass id=\"" . $id . |
| 169 |
"\" name=\'$REMAP_CLASSES{$class}\'/>\n"); |
| 170 |
} |
| 171 |
|
| 172 |
print(BUSCLASSFOO "</busclass_list>\n"); |
| 173 |
|
| 174 |
close(BUSCLASSFOO); |
| 175 |
} |
| 176 |
|
| 177 |
unless (@ARGV) { |
| 178 |
die "Need valid file as first argument\n"; |
| 179 |
} |
| 180 |
|
| 181 |
MAIN: { |
| 182 |
my @data; |
| 183 |
my $busclass; |
| 184 |
my $bus; |
| 185 |
my $bus_upper; |
| 186 |
my $class; |
| 187 |
my $new_class; |
| 188 |
my $class_name; |
| 189 |
my $filename; |
| 190 |
my $interface_type; |
| 191 |
my $model_id; |
| 192 |
my $module; |
| 193 |
my $serverbin; |
| 194 |
my $text; |
| 195 |
my $try; |
| 196 |
my $vendor; |
| 197 |
my $vendor_id; |
| 198 |
|
| 199 |
foreach $filename (@ARGV) { |
| 200 |
if (not -e $filename) { |
| 201 |
die "Need valid file as first argument\n"; |
| 202 |
} |
| 203 |
|
| 204 |
if (not $filename =~ /\/?(\w+)\.lst/) { |
| 205 |
die "Need file with name <foo>.lst, where <foo> is a type " |
| 206 |
. "of hardware\n"; |
| 207 |
} |
| 208 |
|
| 209 |
$bus = $1; |
| 210 |
$bus_upper = $1; |
| 211 |
$bus_upper =~ tr/a-z/A-Z/; |
| 212 |
|
| 213 |
make_busclass($bus, $bus_upper); |
| 214 |
|
| 215 |
if (not open(VENDORS, ">${bus}-vendor.xml")) { |
| 216 |
die "Must be able to open ${bus}-vendor.xml " |
| 217 |
. "for writing\n"; |
| 218 |
} |
| 219 |
|
| 220 |
if (not open(HARDWARE, ">${bus}-device.xml")) { |
| 221 |
die "Must be able to open ${bus}-device.xml " |
| 222 |
. "for writing\n"; |
| 223 |
} |
| 224 |
|
| 225 |
if (not open(LST, $filename)) { |
| 226 |
die "Must be able to open $filename for reading\n"; |
| 227 |
} |
| 228 |
|
| 229 |
print HARDWARE <<EOF; |
| 230 |
<?xml version='1.0' encoding='UTF-8'?> |
| 231 |
<device_list bus='$bus'> |
| 232 |
EOF |
| 233 |
|
| 234 |
print VENDORS <<EOF; |
| 235 |
<?xml version='1.0' encoding='UTF-8'?> |
| 236 |
<vendor_list bus='$bus'> |
| 237 |
EOF |
| 238 |
|
| 239 |
while(<LST>) { |
| 240 |
$class = $model_id = $vendor_id = $class_name = $module = |
| 241 |
$text = $serverbin = ''; |
| 242 |
|
| 243 |
s/&/&/g; |
| 244 |
s/</</g; |
| 245 |
s/>/>/g; |
| 246 |
s/"/"/g; |
| 247 |
s/'/'/g; |
| 248 |
|
| 249 |
#"cperl-mode is suboptimal |
| 250 |
|
| 251 |
$_ = encode_non_ascii($_); |
| 252 |
|
| 253 |
if (/^(\w+) (.*)$/) { |
| 254 |
$vendor_id = $1; |
| 255 |
$vendor = $2; |
| 256 |
|
| 257 |
print(VENDORS " <vendor id=\'$vendor_id\' name=\'$vendor\'/>\n"); |
| 258 |
} elsif (/^\s+\w/) { |
| 259 |
s/^\s+//; |
| 260 |
s/\s+$//; |
| 261 |
@data = split(/\t+/); |
| 262 |
if (@data != 4) { |
| 263 |
print(STDERR "Error: Wrong number of fields in $_\n"); |
| 264 |
next; |
| 265 |
} |
| 266 |
$model_id = $data[0]; |
| 267 |
$class_name = $data[1]; |
| 268 |
$module = $data[2]; |
| 269 |
$text = $data[3]; |
| 270 |
|
| 271 |
if ($class_name eq 'pmc') { |
| 272 |
next; |
| 273 |
} |
| 274 |
|
| 275 |
$new_class = $CLASSES{$class_name}; |
| 276 |
if (not defined($new_class)) { |
| 277 |
die("Couldn't find $class_name in CLASSES\n"); |
| 278 |
} |
| 279 |
|
| 280 |
if (not $new_class) { |
| 281 |
# A device we don't care about, such as mice |
| 282 |
next; |
| 283 |
} |
| 284 |
|
| 285 |
$try = '$busclass = $'; # 'cperl-mode is sub-optimal |
| 286 |
$try .= $bus_upper; |
| 287 |
$try .= '_CLASSES{$new_class};'; |
| 288 |
eval $try; |
| 289 |
|
| 290 |
if (not defined($busclass)) { |
| 291 |
die("Couldn't find $new_class " . |
| 292 |
"in ${bus_upper}_CLASSES\n"); |
| 293 |
} |
| 294 |
|
| 295 |
$vendor_id = substr($model_id, 0, 4); |
| 296 |
$model_id = substr($model_id, 4); |
| 297 |
|
| 298 |
if ($module =~ /:/) { |
| 299 |
@data = split(/:/, $module); |
| 300 |
$interface_type = "xfree86"; |
| 301 |
$module = $data[1]; |
| 302 |
|
| 303 |
if ($module =~ /(.*)\((.*)\)/) { |
| 304 |
$module = $2; |
| 305 |
$serverbin = $1; |
| 306 |
} else { |
| 307 |
$serverbin = $module; |
| 308 |
$module = ''; |
| 309 |
} |
| 310 |
} elsif ($module eq 'unknown' || $module eq 'ignore') { |
| 311 |
$interface_type = ""; |
| 312 |
} else { |
| 313 |
$interface_type = "linux"; |
| 314 |
} |
| 315 |
|
| 316 |
print HARDWARE <<EOF; |
| 317 |
<device model='$model_id' vendor='$vendor_id' model_name='$text'> |
| 318 |
EOF |
| 319 |
if ($interface_type) { |
| 320 |
print HARDWARE <<EOF; |
| 321 |
<data class='$interface_type'> |
| 322 |
EOF |
| 323 |
} |
| 324 |
|
| 325 |
if ($interface_type eq 'linux') { |
| 326 |
print HARDWARE <<EOF; |
| 327 |
<data class='module'> |
| 328 |
<data class='name'>$module</data> |
| 329 |
</data> |
| 330 |
EOF |
| 331 |
} elsif ($interface_type eq 'xfree86') { |
| 332 |
if ($serverbin eq 'XFree86') { |
| 333 |
print HARDWARE <<EOF; |
| 334 |
<data version='[4, inf)' class='server'> |
| 335 |
<data class='name'>$serverbin</data> |
| 336 |
<data class='device'> |
| 337 |
<data class='driver'>$module</data> |
| 338 |
</data> |
| 339 |
</data> |
| 340 |
EOF |
| 341 |
} else { |
| 342 |
print HARDWARE <<EOF; |
| 343 |
<data class='server' version="[3, 4)"> |
| 344 |
<data class='name'>$serverbin</data> |
| 345 |
</data> |
| 346 |
EOF |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if ($interface_type) { |
| 351 |
print HARDWARE <<EOF; |
| 352 |
</data> |
| 353 |
EOF |
| 354 |
} |
| 355 |
print HARDWARE <<EOF; |
| 356 |
</device> |
| 357 |
EOF |
| 358 |
|
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
print HARDWARE "</device_list>\n"; |
| 363 |
print VENDORS "</vendor_list>\n"; |
| 364 |
|
| 365 |
close(LST); |
| 366 |
close(HARDWARE); |
| 367 |
close(VENDORS); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
# vim:set ai et sts=4 sw=4: |