summaryrefslogtreecommitdiff
path: root/hw/xfree86/getconfig/getconfig.pl
blob: 85caf81f5e3753788e2fb46b33c8338d1adb3c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/perl

# $DHD: xc/programs/Xserver/hw/xfree86/getconfig/getconfig.pl,v 1.13 2003/09/23 05:12:07 dawes Exp $

#
# Copyright 2003 by David H. Dawes.
# Copyright 2003 by X-Oz Technologies.
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# 
# Except as contained in this notice, the name of the copyright holder(s)
# and author(s) shall not be used in advertising or otherwise to promote
# the sale, use or other dealings in this Software without prior written
# authorization from the copyright holder(s) and author(s).
# 
# Author: David Dawes <dawes@XFree86.Org>.
#


#
# This script takes PCI id information, compares it against an ordered list
# of rules, and prints out the configuration information specified by the
# last matching rule.
#
# This script is called by xf86AutoConfig().
#

# Command line processing.

$GetconfigVersion = v1.0;

$debug = 0;

$myname = $0;
$myname =~ s/.*\///;

$signature = "Xorg Foundation getconfig rules file.  Version: ";

while (@ARGV[0] =~ /^-[A-Za-z]$/) {
    $f = shift;
    SWITCH: {
	if ($f eq "-D") {
	    $debug = 1;
	    last SWITCH;
	}
	if ($f eq "-I") {
	    push(@searchPaths, split(/,/, shift));
	    last SWITCH;
	}
	if ($f eq "-V") {
	    printf STDERR "$myname: Version %vd.\n", $GetconfigVersion;
	    exit 0;
	}
	if ($f eq "-X") {
	    $XorgVersionNumeric = shift;
	    if (!defined($XorgVersionNumeric)) {
		print STDERR "$myname: -X requires the Xorg version.\n";
		exit 1;
	    }
	}
	if ($f eq "-b") {
	    $subsys = oct(shift);
	    if (!defined($subsys)) {
		print STDERR "$myname: -b requires the subsys id.\n";
		exit 1;
	    }
	    last SWITCH;
	}
	if ($f eq "-c") {
	    $class = oct(shift);
	    if (!defined($class)) {
		print STDERR "$myname: -c requires the class value.\n";
		exit 1;
	    }
	    last SWITCH;
	}
	if ($f eq "-d") {
	    $device = oct(shift);
	    if (!defined($device)) {
		print STDERR "$myname: -d requires the device id.\n";
		exit 1;
	    }
	    last SWITCH;
	}
	if ($f eq "-r") {
	    $revision = oct(shift);
	    if (!defined($revision)) {
		print STDERR "$myname: -r requires the device revision.\n";
		exit 1;
	    }
	    last SWITCH;
	}
	if ($f eq "-s") {
	    $subsysVendor = oct(shift);
	    if (!defined($subsysVendor)) {
		print STDERR "$myname: -s requires the subsysVendor id.\n";
		exit 1;
	    }
	    last SWITCH;
	}
	if ($f eq "-v") {
	    $vendor = oct(shift);
	    if (!defined($vendor)) {
		print STDERR "$myname: -v requires the vendor id.\n";
		exit 1;
	    }
	    last SWITCH;
	}
    }
}

printf STDERR "$myname: Version %vd.\n", $GetconfigVersion;

if (defined($XorgVersionNumeric)) {
    $XorgVersionMajor = $XorgVersionNumeric / 10000000;
    $XorgVersionMinor = ($XorgVersionNumeric % 10000000) / 100000;
    $XorgVersionPatch = ($XorgVersionNumeric % 100000) / 1000;
    $XorgVersionSnapshot = $XorgVersionNumeric % 1000;
    $XorgVersion = chr($XorgVersionMajor) . chr($XorgVersionMinor) .
		chr($XorgVersionPatch) . chr($XorgVersionSnapshot);
}

if ($debug) {
    printf STDERR "$myname: Xorg Version: %d, %d.%d.%d.%d, %vd.\n",
	$XorgVersionNumeric, $XorgVersionMajor, $XorgVersionMinor,
	$XorgVersionPatch, $XorgVersionSnapshot, $XorgVersion;
} else {
    printf STDERR "$myname: Xorg Version: %vd.\n", $XorgVersion;
}
  

# The rules here are just basic vendor ID to driver mappings.
# Ideally this is all that would be required.  More complicated configuration
# rules will be provided in external files.

# XXX This set of basic rules isn't complete yet.

@rules = (

# Set the weight for the built-in rules.
['$weight = 500'],

# APM
['$vendor == 0x1142',
	'apm'],

# ARK
['$vendor == 0xedd8',
	'apm'],

# ATI
['$vendor == 0x1002',
	'ati'],

# Chips & Technologies
['$vendor == 0x102c',
	'chips'],

# Cirrus
['$vendor == 0x1013',
	'cirrus'],

# Intel
['$vendor == 0x8086',
	'i810'],
['$vendor == 0x8086 && ($chipType == 0x00d1 || $chipType == 0x7800)',
	'i740'],

# Matrox
['$vendor == 0x102b',
	'mga'],

# Neomagic
['$vendor == 0x10c8',
	'neomagic'],

# Number Nine
['$vendor == 0x105d',
	'i128'],

# NVIDIA
['$vendor == 0x10de || $vendor == 0x12d2',
	'nv'],

# S3
['$vendor == 0x5333 && ($device == 0x88d0 ||' .
			'$device == 0x88d1 ||' .
			'$device == 0x88f0 ||' .
			'$device == 0x8811 ||' .
			'$device == 0x8812 ||' .
			'$device == 0x8814 ||' .
			'$device == 0x8901)',
	's3'],

# S3 virge
['$vendor == 0x5333 && ($device == 0x5631 ||' .
			'$device == 0x883d ||' .
			'$device == 0x8a01 ||' .
			'$device == 0x8a10 ||' .
			'$device == 0x8c01 ||' .
			'$device == 0x8c03 ||' .
			'$device == 0x8904 ||' .
			'$device == 0x8a13)',
	's3virge'],

# S3 Savage
['$vendor == 0x5333 && ($device >= 0x8a20 && $device <= 0x8a22 ||' .
			'$device == 0x9102 ||' .
			'$device >= 0x8c10 && $device <= 0x8c13 ||' .
			'$device == 0x8a25 ||' .
			'$device == 0x8a26 ||' .
			'$device >= 0x8d01 && $device <= 0x8d04 ||' .
			'$device >= 0x8c2a && $device <= 0x8c2f ||' .
			'$device == 0x8c22 ||' .
			'$device == 0x8c24 ||' .
			'$device == 0x8c26)',
	'savage'],

# SIS
['$vendor == 0x1039',
	'sis'],

# SMI
['$vendor == 0x126f',
	'siliconmotion'],

# 3Dfx
['$vendor == 0x121a',
	'tdfx'],

# 3Dlabs
['$vendor == 0x3d3d',
	'glint'],

# Trident
['$vendor == 0x1023',
	'trident'],

# Tseng Labs
['$vendor == 0x100c',
	'tseng'],

# VIA
['$vendor == 0x1106',
	'via'],

# VMware
['$vendor == 0x15ad',
	'vmware'],

);

# Reverse the search path list, since the later rules have higher priority
# than earlier ones (weighting being equal).

@searchPaths = reverse(@searchPaths);

if ($debug) {
    $i = 0;
    for $path (@searchPaths) {
	print STDERR "$myname: Search path $i is: \"$path\".\n";
	$i++;
    }
}

print STDERR "$myname: ", $#rules + 1, " built-in rule", plural($#rules + 1),
			".\n";

for $path (@searchPaths) {
    while (<$path/*.cfg>) {
	@tmp = readRulesFile($_);
	if (defined(@tmp[0])) {
	    push @rules, @tmp;
	}
    }
}

if ($debug) {
    $i = 0;
    for $r (@rules) {
	print STDERR "$myname: rule $i is: \'@$r\'.\n";
	$i++
    }
}

$i = 0;
$e = 0;
$weight = 0;
$w = 0;
for $r (@rules) {
    ($cond, $d, @o) = @$r;
    $result = eval $cond;
    if ($@) {
	print STDERR "$myname: Error evaluating rule $i \'$cond\': $@";
	$e++;
    }
    if ($debug) {
	print STDERR "$myname: rule $i \'$cond\' evaluates to \'$result\'.\n";
    }
    if ($result && defined($d) && $weight >= $w) {
	$driver = $d;
	@opts = @o;
	$w = $weight;
    }
    $i++;
}

print STDERR "$myname: Evaluated $i rule", plural($i),
		" with $e error", plural($e), ".\n";

print STDERR "$myname: Weight of result is $w.\n";

if ($debug) {
    if (defined($driver)) {
	print STDERR "$myname: Driver is \'$driver\'.\n";
    } else {
	print STDERR "$myname: No driver.\n";
    }
    if (defined(@opts)) {
	print STDERR "$myname: options are:\n";
	for $opt (@opts) {
	    print STDERR "\t$opt\n";
	}
    } else {
	print STDERR "$myname: No options.\n";
    }
}

print "$driver\n";
for $opt (@opts) {
    print "$opt\n";
}

exit 0;

# Subroutines.

sub readRulesFile {
    my ($file) = @_;
    my $signatureOK = 0;
    my @r, @tmp;
    my $line, $cont, $prevcont, $fileversion;

    undef @tmp;
    undef @r;

    if (open(RF, "<$file")) {
	$prevcont = 0;
	while (<RF>) {
	    chop;
	    $line = $_;
	    next if ($line =~ /^#/);
	    next if ($line =~ /^\s*$/);
	    if (!$signatureOK) {
		if ($line  =~ /^$signature(.*)$/) {
		    $fileversion = $1;
		    $signatureOK = 1;
		    print STDERR
			"$myname: rules file \'$file\' has version $fileversion.\n";
		    next;
		}
	    }
	    if (!$signatureOK) {
		print STDERR "$myname: file \'$file\' has bad signature.\n";
		close(RF);
		last;
	    }
	    $cont = 0;
	    if ($line =~ s/\\\s*$//) {
		$cont = 1;
	    }
	    if (!$prevcont && $line =~ /^\S+/) {
		if (defined(@tmp[0])) {
		    push(@r,[@tmp]);
		}
		undef @tmp;
	    }
	    if ($prevcont) {
		push(@tmp, pop(@tmp) . $line);
	    } else {
		push(@tmp, $line);
	    }
	    $prevcont = $cont;
	}
	if (defined(@tmp[0])) {
	    push(@r,[@tmp]);
	}
	if (!defined(@r[0])) {
	    print STDERR "$myname: no rules in file \'$file\'.\n";
	} else {
	    print STDERR "$myname: ", $#r + 1,
			" rule", plural($#r + 1),
			" added from file \'$file\'.\n";
	}
    } else {
	print STDERR "$myname: cannot open file \'$file\'.\n";
    }

    return @r;
}

sub plural {
    my ($count) = @_;

    if ($count != 1) {
	return "s";
    } else {
	return "";
    }
}