summaryrefslogtreecommitdiff
path: root/scripts/drm-scripts-gentree.pl
blob: cbc101751fd55ca60518aa27fb820c9abe29ebf3 (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
#!/usr/bin/perl
#
# Original version were part of Gerd Knorr's v4l scripts.
#
# Several improvements by (c) 2005-2007 Mauro Carvalho Chehab
#
# Largely re-written (C) 2007 Trent Piepho <xyzzy@speakeasy.org>
# Stolen for DRM usage by airlied
#
# Theory of Operation
#
# This acts as a sort of mini version of cpp, which will process
# #if/#elif/#ifdef/etc directives to strip out code used to support
# multiple kernel versions or otherwise not wanted to be sent upstream to
# git.
#
# Conditional compilation directives fall into two catagories,
# "processed" and "other".  The "other" directives are ignored and simply
# output as they come in without changes (see 'keep' exception).  The
# "processed" variaty are evaluated and only the lines in the 'true' part
# are kept, like cpp would do.
#
# If gentree knows the result of an expression, that directive will be
# "processed", otherwise it will be an "other".  gentree knows the value
# of LINUX_VERSION_CODE, BTTV_VERSION_CODE, the KERNEL_VERSION(x,y,z)
# macro, numeric constants like 0 and 1, and a few defines like MM_KERNEL
# and STV0297_CS2.
#
# An exception is if the comment "/*KEEP*/" appears after the expression,
# in which case that directive will be considered an "other" and not
# processed, other than to remove the keep comment.
#
# Known bugs:
# don't specify the root directory e.g. '/' or even '////'
# directives continued with a back-slash will always be ignored
# you can't modify a source tree in-place, i.e. source dir == dest dir

use strict;
use File::Find;
use Fcntl ':mode';

my $VERSION = shift;
my $SRC = shift;
my $DESTDIR = shift;

if (!defined($DESTDIR)) {
	print "Usage:\ngentree.pl\t<version> <source dir> <dest dir>\n\n";
	exit;
}

my $BTTVCODE = KERNEL_VERSION(0,9,17);
my ($LINUXCODE, $extra) = kernel_version($VERSION);
my $DEBUG = 0;

my %defs = (
	'LINUX_VERSION_CODE' => $LINUXCODE,
	'MM_KERNEL' => ($extra =~ /-mm/)?1:0,
	'DRM_ODD_MM_COMPAT' => 0,
	'I915_HAVE_FENCE' => 1,
	'I915_HAVE_BUFFER' => 1,
	'VIA_HAVE_DMABLIT' => 1,
	'VIA_HAVE_CORE_MM' => 1,
	'VIA_HAVE_FENCE' => 1,
        'VIA_HAVE_BUFFER' => 1,
	'SIS_HAVE_CORE_MM' => 1,
        'DRM_FULL_MM_COMPAT' => 1,   
	'__linux__' => 1,
);

#################################################################
# helpers

sub kernel_version($) {
	$_[0] =~ m/(\d+)\.(\d+)\.(\d+)(.*)/;
	return ($1*65536 + $2*256 + $3, $4);
}

# used in eval()
sub KERNEL_VERSION($$$) { return $_[0]*65536 + $_[1]*256 + $_[2]; }

sub evalexp($) {
	local $_ = shift;
	s|/\*.*?\*/||go;	# delete /* */ comments
	s|//.*$||o;		# delete // comments
	s/\bdefined\s*\(/(/go;	# defined(foo) to (foo)
	while (/\b([_A-Za-z]\w*)\b/go) {
		if (exists $defs{$1}) {
			my $id = $1; my $pos = $-[0];
			s/$id/$defs{$id}/;
			pos = $-[0];
		} elsif ($1 ne 'KERNEL_VERSION') {
			return(undef);
		}
	}
	return(eval($_) ? 1 : 0);
}

#################################################################
# filter out version-specific code

sub filter_source ($$) {
	my ($in,$out) = @_;
	my $line;
	my $level=0;
	my %if = ();
	my %state = ();

	my @dbgargs = \($level, %state, %if, $line);
	sub dbgline($\@) {
		my $level = ${$_[1][0]};
		printf STDERR ("/* BP %4d $_[0] state=$_[1][1]->{$level} if=$_[1][2]->{$level} level=$level (${$_[1][3]}) */\n", $.) if $DEBUG;
	}

	open IN, '<', $in or die "Error opening $in: $!\n";
	open OUT, '>', $out or die "Error opening $out: $!\n";

	print STDERR "File: $in, for kernel $VERSION($LINUXCODE)/\n" if $DEBUG;

	while ($line = <IN>) {
		chomp $line;
		next if ($line =~ m/^#include \"compat.h\"/o);
#		next if ($line =~ m/[\$]Id:/);

		# For "#if 0 /*KEEP*/;" the ; should be dropped too
		if ($line =~ m@^\s*#\s*if(n?def)?\s.*?(\s*/\*\s*(?i)keep\s*\*/;?)@) {
			$state{$level} = "ifother";
			$if{$level} = 1;
			dbgline "#if$1 (keep)", @dbgargs;
			$line =~ s/\Q$2\E//;
			$level++;
		}
		# handle all ifdef/ifndef lines
		elsif ($line =~ /^\s*#\s*if(n?)def\s*(\w+)/o) {
			if (exists $defs{$2}) {
				$state{$level} = 'if';
				$if{$level} = ($1 eq 'n') ? !$defs{$2} : $defs{$2};
				dbgline "#if$1def $2", @dbgargs;
				$level++;
				next;
			}
			$state{$level} = "ifother";
			$if{$level} = 1;
			dbgline "#if$1def (other)", @dbgargs;
			$level++;
		}
		# handle all ifs
		elsif ($line =~ /^\s*#\s*if\s+(.*)$/o) {
			my $res = evalexp($1);
			if (defined $res) {
				$state{$level} = 'if';
				$if{$level} = $res;
				dbgline '#if '.($res?'(yes)':'(no)'), @dbgargs;
				$level++;
				next;
			} else {
				$state{$level} = 'ifother';
				$if{$level} = 1;
				dbgline '#if (other)', @dbgargs;
				$level++;
			}
		}
		# handle all elifs
		elsif ($line =~ /^\s*#\s*elif\s+(.*)$/o) {
			my $exp = $1;
			$level--;
			$level < 0 and die "more elifs than ifs";
			$state{$level} =~ /if/ or die "unmatched elif";

			if ($state{$level} eq 'if' && !$if{$level}) {
				my $res = evalexp($exp);
				defined $res or die 'moving from if to ifother';
				$state{$level} = 'if';
				$if{$level} = $res;
				dbgline '#elif1 '.($res?'(yes)':'(no)'), @dbgargs;
				$level++;
				next;
			} elsif ($state{$level} ne 'ifother') {
				$if{$level} = 0;
				$state{$level} = 'elif';
				dbgline '#elif0', @dbgargs;
				$level++;
				next;
			}
			$level++;
		}
		elsif ($line =~ /^\s*#\s*else/o) {
			$level--;
			$level < 0 and die "more elses than ifs";
			$state{$level} =~ /if/ or die "unmatched else";
			$if{$level} = !$if{$level} if ($state{$level} eq 'if');
			$state{$level} =~ s/^if/else/o; # if -> else, ifother -> elseother, elif -> elif
			dbgline '#else', @dbgargs;
			$level++;
			next if $state{$level-1} !~ /other$/o;
		}
		elsif ($line =~ /^\s*#\s*endif/o) {
			$level--;
			$level < 0 and die "more endifs than ifs";
			dbgline '#endif', @dbgargs;
			next if $state{$level} !~ /other$/o;
		}

		my $print = 1;
		for (my $i=0;$i<$level;$i++) {
			next if $state{$i} =~ /other$/o;	# keep code in ifother/elseother blocks
			if (!$if{$i}) {
				$print = 0;
				dbgline 'DEL', @{[\$i, \%state, \%if, \$line]};
				last;
			}
		}
		print OUT "$line\n" if $print;
	}
	close IN;
	close OUT;
}

#################################################################

sub parse_dir {
	my $file = $File::Find::name;

	return if ($file =~ /CVS/);
	return if ($file =~ /~$/);

	my $f2 = $file;
	$f2 =~ s/^\Q$SRC\E/$DESTDIR/;

	my $mode = (stat($file))[2];
	if ($mode & S_IFDIR) {
		print("mkdir -p '$f2'\n");
		system("mkdir -p '$f2'");  # should check for error
		return;
	}
	print "from $file to $f2\n";

	if ($file =~ m/.*\.[ch]$/) {
		filter_source($file, $f2);
	} else {
		system("cp $file $f2");
	}
}


# main

printf "kernel is %s (0x%x)\n",$VERSION,$LINUXCODE;

# remove any trailing slashes from dir names.  don't pass in just '/'
$SRC =~ s|/*$||; $DESTDIR =~ s|/*$||;

print "finding files at $SRC\n";

find({wanted => \&parse_dir, no_chdir => 1}, $SRC);
s="hl kwd">to_intel_output(connector); struct intel_dvo_device *dvo = intel_output->dev_priv; /* We should probably have an i2c driver get_modes function for those * devices which will have a fixed set of modes determined by the chip * (TV-out, for example), but for now with just TMDS and LVDS, * that's not the case. */ intel_ddc_get_modes(intel_output); if (!list_empty(&connector->probed_modes)) return 1; #if 0 if (intel_output->i2c_drv->vid_rec->get_modes) { modes = intel_output->i2c_drv->vid_rec->get_modes (intel_output->i2c_drv->dev_priv); if (modes != NULL) return modes; } #endif if (dvo->panel_fixed_mode != NULL) { struct drm_display_mode *mode; mode = drm_mode_duplicate(connector->dev, dvo->panel_fixed_mode); if (mode) { drm_mode_probed_add(connector, mode); return 1; } } return 0; } static void intel_dvo_destroy (struct drm_connector *connector) { struct intel_output *intel_output = to_intel_output(connector); struct intel_dvo_device *dvo = intel_output->dev_priv; if (dvo) { if (dvo->dev_ops->destroy) dvo->dev_ops->destroy(dvo); if (dvo->panel_fixed_mode) kfree(dvo->panel_fixed_mode); /* no need, in i830_dvoices[] now */ //kfree(dvo); } if (intel_output->i2c_bus) intel_i2c_destroy(intel_output->i2c_bus); if (intel_output->ddc_bus) intel_i2c_destroy(intel_output->ddc_bus); drm_connector_cleanup(connector); kfree(intel_output); } #ifdef RANDR_GET_CRTC_INTERFACE static struct drm_crtc *intel_dvo_get_crtc(struct drm_connector *connector) { struct drm_device *dev = connector->dev; struct drm_i915_private *dev_priv = dev->dev_private; struct intel_output *intel_output = to_intel_output(connector); struct intel_dvo_device *dvo = intel_output->dev_priv; int pipe = !!(I915_READ(dvo->dvo_reg) & SDVO_PIPE_B_SELECT); return intel_pipe_to_crtc(pScrn, pipe); } #endif static const struct drm_encoder_helper_funcs intel_dvo_helper_funcs = { .dpms = intel_dvo_dpms, .mode_fixup = intel_dvo_mode_fixup, .prepare = intel_encoder_prepare, .mode_set = intel_dvo_mode_set, .commit = intel_encoder_commit, }; static const struct drm_connector_funcs intel_dvo_connector_funcs = { .save = intel_dvo_save, .restore = intel_dvo_restore, .detect = intel_dvo_detect, .destroy = intel_dvo_destroy, .fill_modes = drm_helper_probe_single_connector_modes, }; static const struct drm_connector_helper_funcs intel_dvo_connector_helper_funcs = { .mode_valid = intel_dvo_mode_valid, .get_modes = intel_dvo_get_modes, .best_encoder = intel_best_encoder, }; void intel_dvo_enc_destroy(struct drm_encoder *encoder) { drm_encoder_cleanup(encoder); } static const struct drm_encoder_funcs intel_dvo_enc_funcs = { .destroy = intel_dvo_enc_destroy, }; /** * Attempts to get a fixed panel timing for LVDS (currently only the i830). * * Other chips with DVO LVDS will need to extend this to deal with the LVDS * chip being on DVOB/C and having multiple pipes. */ static struct drm_display_mode * intel_dvo_get_current_mode (struct drm_connector *connector) { struct drm_device *dev = connector->dev; struct drm_i915_private *dev_priv = dev->dev_private; struct intel_output *intel_output = to_intel_output(connector); struct intel_dvo_device *dvo = intel_output->dev_priv; uint32_t dvo_reg = dvo->dvo_reg; uint32_t dvo_val = I915_READ(dvo_reg); struct drm_display_mode *mode = NULL; /* If the DVO port is active, that'll be the LVDS, so we can pull out * its timings to get how the BIOS set up the panel. */ if (dvo_val & DVO_ENABLE) { struct drm_crtc *crtc; int pipe = (dvo_val & DVO_PIPE_B_SELECT) ? 1 : 0; crtc = intel_get_crtc_from_pipe(dev, pipe); if (crtc) { mode = intel_crtc_mode_get(dev, crtc); if (mode) { mode->type |= DRM_MODE_TYPE_PREFERRED; if (dvo_val & DVO_HSYNC_ACTIVE_HIGH) mode->flags |= V_PHSYNC; if (dvo_val & DVO_VSYNC_ACTIVE_HIGH) mode->flags |= V_PVSYNC; } } } return mode; } void intel_dvo_init(struct drm_device *dev) { struct intel_output *intel_output; struct intel_dvo_device *dvo; struct intel_i2c_chan *i2cbus = NULL; int ret = 0; int i; int gpio_inited = 0; int encoder_type = DRM_MODE_ENCODER_NONE; intel_output = kzalloc (sizeof(struct intel_output), GFP_KERNEL); if (!intel_output) return; /* Set up the DDC bus */ intel_output->ddc_bus = intel_i2c_create(dev, GPIOD, "DVODDC_D"); if (!intel_output->ddc_bus) goto free_intel; /* Now, try to find a controller */ for (i = 0; i < ARRAY_SIZE(intel_dvo_devices); i++) { struct drm_connector *connector = &intel_output->base; int gpio; dvo = &intel_dvo_devices[i]; /* Allow the I2C driver info to specify the GPIO to be used in * special cases, but otherwise default to what's defined * in the spec. */ if (dvo->gpio != 0) gpio = dvo->gpio; else if (dvo->type == INTEL_DVO_CHIP_LVDS) gpio = GPIOB; else gpio = GPIOE; /* Set up the I2C bus necessary for the chip we're probing. * It appears that everything is on GPIOE except for panels * on i830 laptops, which are on GPIOB (DVOA). */ if (gpio_inited != gpio) { if (i2cbus != NULL) intel_i2c_destroy(i2cbus); if (!(i2cbus = intel_i2c_create(dev, gpio, gpio == GPIOB ? "DVOI2C_B" : "DVOI2C_E"))) { continue; } gpio_inited = gpio; } if (dvo->dev_ops!= NULL) ret = dvo->dev_ops->init(dvo, i2cbus); else ret = false; if (!ret) continue; intel_output->type = INTEL_OUTPUT_DVO; switch (dvo->type) { case INTEL_DVO_CHIP_TMDS: // connector = DRM_MODE_CONNECTOR_DVID; drm_connector_init(dev, connector, &intel_dvo_connector_funcs, DRM_MODE_CONNECTOR_DVII); encoder_type = DRM_MODE_ENCODER_TMDS; break; case INTEL_DVO_CHIP_LVDS: // connector = DRM_MODE_CONNECTOR_LVDS; drm_connector_init(dev, connector, &intel_dvo_connector_funcs, DRM_MODE_CONNECTOR_LVDS); encoder_type = DRM_MODE_ENCODER_LVDS; break; } drm_connector_helper_add(connector, &intel_dvo_connector_helper_funcs); connector->display_info.subpixel_order = SubPixelHorizontalRGB; connector->interlace_allowed = false; connector->doublescan_allowed = false; intel_output->dev_priv = dvo; intel_output->i2c_bus = i2cbus; drm_encoder_init(dev, &intel_output->enc, &intel_dvo_enc_funcs, encoder_type); drm_encoder_helper_add(&intel_output->enc, &intel_dvo_helper_funcs); drm_mode_connector_attach_encoder(&intel_output->base, &intel_output->enc); if (dvo->type == INTEL_DVO_CHIP_LVDS) { /* For our LVDS chipsets, we should hopefully be able * to dig the fixed panel mode out of the BIOS data. * However, it's in a different format from the BIOS * data on chipsets with integrated LVDS (stored in AIM * headers, likely), so for now, just get the current * mode being output through DVO. */ dvo->panel_fixed_mode = intel_dvo_get_current_mode(connector); dvo->panel_wants_dither = true; } drm_sysfs_connector_add(connector); return; } intel_i2c_destroy(intel_output->ddc_bus); /* Didn't find a chip, so tear down. */ if (i2cbus != NULL) intel_i2c_destroy(i2cbus); free_intel: kfree(intel_output); }