root/i2c-tools/trunk/eeprom/ddcmon @ 5162

Revision 5162, 13.0 KB (checked in by khali, 5 years ago)

Drop the trailing .pl from all perl script names.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2#
3# Copyright (C) 2004-2005  Jean Delvare <khali@linux-fr.org>
4#
5# Parts inspired from decode-edid.
6# Copyright (C) 2003-2004  Jean Delvare <khali@linux-fr.org>
7#
8# Parts inspired from the ddcmon driver and sensors' print_ddcmon function.
9# Copyright (C) 1998-2004  Mark D. Studebaker
10#
11# Parts inspired from the fbmon driver (Linux 2.6.10).
12# Copyright (C) 2002  James Simmons <jsimmons@users.sf.net>
13#
14#    This program is free software; you can redistribute it and/or modify
15#    it under the terms of the GNU General Public License as published by
16#    the Free Software Foundation; either version 2 of the License, or
17#    (at your option) any later version.
18#
19#    This program is distributed in the hope that it will be useful,
20#    but WITHOUT ANY WARRANTY; without even the implied warranty of
21#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22#    GNU General Public License for more details.
23#
24#    You should have received a copy of the GNU General Public License
25#    along with this program; if not, write to the Free Software
26#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27#
28# Version 1.0  2005-01-04  Jean Delvare <khali@linux-fr.org>
29#
30# This script is a replacement for the now deprecated ddcmon kernel driver.
31# Instead of having a dedicated driver, it is better to reuse the standard
32# eeprom driver and implement the EDID-specific code in user-space.
33#
34# EDID (Extended Display Identification Data) is a VESA standard which
35# allows storing (on manufacturer's side) and retrieving (on user's side)
36# of configuration information about displays, such as manufacturer,
37# serial number, physical dimensions and allowed horizontal and vertical
38# refresh rates.
39#
40# Syntax: ddcmon [bus [address]]
41# Address can be given in decimal or hexadecimal (with a 0x prefix).
42# If no address is given, default is 0x50.
43# Bus number must be given in decimal. If no bus number is given,
44# try them all.
45
46use strict;
47use Fcntl qw(:DEFAULT :seek);
48use vars qw(@standard_scales);
49
50@standard_scales = ([1, 1], [3, 4], [4, 5], [16, 9]);
51
52# Make sure the eeprom module is loaded.
53# For non-modular kernels, we can't help.
54if (-r '/proc/modules')
55{
56        my $found = 0;
57        open(MODULES, '/proc/modules');
58        while (!$found && defined ($_ = <MODULES>))
59        {
60                $found++ if m/^eeprom\s+/;
61        }
62        close(MODULES);
63
64        unless ($found)
65        {
66                print STDERR
67                        "This script requires the eeprom module to be loaded.\n";
68                exit 1;
69        }
70}
71
72# Only used for sysfs
73sub rawread
74{
75        my $filename = shift;
76        my $length = shift;
77        my $offset = shift || 0;
78        my $bytes = '';
79       
80        sysopen(FH, $filename, O_RDONLY)
81                or die "Can't open $filename";
82        if ($offset)
83        {
84                sysseek(FH, $offset, SEEK_SET)
85                        or die "Can't seek in $filename";
86        }
87
88        $offset = 0;
89        while ($length)
90        {
91                my $r = sysread(FH, $bytes, $length, $offset);
92                die "Can't read $filename"
93                        unless defined($r);
94                die "Unexpected EOF in $filename"
95                        unless $r;
96                $offset += $r;
97                $length -= $r;
98        }
99        close(FH);
100
101        return $bytes;
102}
103
104sub get_edid_sysfs
105{
106        my ($bus, $addr) = @_;
107
108        my @bytes = unpack("C*", rawread("/sys/bus/i2c/devices/$bus-00$addr/eeprom", 128));
109
110        return \@bytes;
111}
112
113sub get_edid_procfs
114{
115        my ($bus, $addr) = @_;
116
117        my @bytes;
118
119        for (my $i = 0 ; $i < 0x80; $i += 0x10)
120        {
121                my $filename = sprintf("/proc/sys/dev/sensors/eeprom-i2c-\%s-\%s/\%02x",
122                                       $bus, $addr, $i);
123                open(EEDATA, $filename)
124                        or die "Can't read $filename";
125                push @bytes, split(/\s+/, <EEDATA>);
126                close(EEDATA);
127        }
128
129        return \@bytes;
130}
131
132sub print_line
133{
134        my $label = shift;
135        my $pattern = shift;
136
137        printf("\%-24s$pattern\n", $label.':', @_);
138}
139
140sub extract_byte
141{
142        my ($bytes, $offset) = @_;
143       
144        return $bytes->[$offset];
145}
146
147sub extract_word
148{
149        my ($bytes, $offset) = @_;
150       
151        return ($bytes->[$offset]
152              | ($bytes->[$offset+1] << 8));
153}
154
155sub extract_manufacturer
156{
157        my ($bytes, $offset) = @_;
158        my $i = ($bytes->[$offset+1] | ($bytes->[$offset] << 8));
159
160        return sprintf('%c%c%c',
161                       (($i >> 10) & 0x1f) + ord('A') - 1,
162                       (($i >> 5) & 0x1f) + ord('A') - 1,
163                       ($i & 0x1f) + ord('A') - 1);
164}
165
166sub extract_sesquiword
167{
168        my ($bytes, $offset) = @_;
169       
170        return ($bytes->[$offset]
171              | ($bytes->[$offset+1] << 8)
172              | ($bytes->[$offset+2] << 16));
173}
174
175sub extract_dword
176{
177        my ($bytes, $offset) = @_;
178       
179        return ($bytes->[$offset]
180              | ($bytes->[$offset+1] << 8)
181              | ($bytes->[$offset+2] << 16)
182              | ($bytes->[$offset+3] << 24));
183}
184
185sub extract_display_input
186{
187        my ($bytes, $offset) = @_;
188
189        my @voltage = ('0.700V/0.300V', '0.714V/0.286V',
190                       '1.000V/0.400V', '0.700V/0.000V');
191
192        return 'Digital'
193                if ($bytes->[$offset] & 0x80);
194
195        return 'Analog ('.$voltage[($bytes->[$offset] & 0x60) >> 5].')';
196}
197
198sub extract_dpms
199{
200        my ($bytes, $offset) = @_;
201
202        my @supported;
203
204        push @supported, 'Active Off' if ($bytes->[$offset] & 0x20);
205        push @supported, 'Suspend' if ($bytes->[$offset] & 0x40);
206        push @supported, 'Standby' if ($bytes->[$offset] & 0x80);
207
208        return join(', ', @supported)
209                if (@supported);
210
211        return 'None supported';
212}
213
214sub extract_color_mode
215{
216        my ($bytes, $offset) = @_;
217
218        my @mode = ('Monochrome', 'RGB Multicolor', 'Non-RGB Multicolor');
219
220        return $mode[($bytes->[$offset] >> 3) & 0x03];
221}
222 
223sub good_signature
224{
225        my $bytes = shift;
226       
227        return $bytes->[0] == 0x00
228            && $bytes->[1] == 0xff
229            && $bytes->[2] == 0xff
230            && $bytes->[3] == 0xff
231            && $bytes->[4] == 0xff
232            && $bytes->[5] == 0xff
233            && $bytes->[6] == 0xff
234            && $bytes->[7] == 0x00;
235}
236
237sub verify_checksum
238{
239        my $bytes = shift;
240        my $cs;
241
242        for (my $i = 0, $cs = 0; $i < 0x80; $i++)
243        {
244                $cs += $bytes->[$i];
245        }
246       
247        return (($cs & 0xff) == 0 ? 'OK' : 'Not OK');
248}
249
250sub add_timing
251{
252        my ($timings, $new) = @_;
253
254        my $mode = sprintf('%ux%u@%u%s', $new->[0], $new->[1],
255                        $new->[2], defined ($new->[3]) &&
256                        $new->[3] eq 'interlaced' ? 'i' : '');
257
258        $timings->{$mode} = $new;
259}
260
261sub add_standard_timing
262{
263        my ($timings, $byte0, $byte1) = @_;
264
265        # Unused slot
266        return if ($byte0 == $byte1)
267               && ($byte0 == 0x01 || $byte0 == 0x00 || $byte0 == 0x20);
268
269        my $width = ($byte0 + 31) * 8;
270        my $height = $width * $standard_scales[$byte1 >> 6]->[0]
271                            / $standard_scales[$byte1 >> 6]->[1];
272        my $refresh = 60 + ($byte1 & 0x3f);
273
274        add_timing($timings, [$width, $height, $refresh]);
275}
276
277sub sort_timings
278{
279        # First order by width
280        return -1 if  $a->[0] < $b->[0];
281        return 1 if  $a->[0] > $b->[0];
282
283        # Second by height
284        return -1 if  $a->[1] < $b->[1];
285        return 1 if  $a->[1] > $b->[1];
286
287        # Third by frequency
288        # Interlaced modes count for half their frequency
289        my $freq_a = $a->[2];
290        my $freq_b = $b->[2];
291        $freq_a /= 2 if defined $a->[3] && $a->[3] eq 'interlaced';
292        $freq_b /= 2 if defined $b->[3] && $b->[3] eq 'interlaced';
293        return -1 if $freq_a < $freq_b;
294        return 1 if $freq_a > $freq_b;
295
296        return 0;
297}
298
299sub print_timings
300{
301        my ($bytes, $timings) = @_;
302
303        # Established Timings
304        my @established =
305        (
306                [720, 400, 70],
307                [720, 400, 88],
308                [640, 480, 60],
309                [640, 480, 67],
310                [640, 480, 72],
311                [640, 480, 75],
312                [800, 600, 56],
313                [800, 600, 60],
314                [800, 600, 72],
315                [800, 600, 75],
316                [832, 624, 75],
317                [1024, 768, 87, 'interlaced'],
318                [1024, 768, 60],
319                [1024, 768, 70],
320                [1024, 768, 75],
321                [1280, 1024, 75],
322                undef, undef, undef,
323                [1152, 870, 75],
324        );
325        my $temp = extract_sesquiword($bytes, 0x23);
326        for (my $i = 0; $i < 24; $i++)
327        {
328                next unless defined($established[$i]);
329                add_timing($timings, $established[$i])
330                        if ($temp & (1 << $i));
331        }
332
333        # Standard Timings
334        for (my $i = 0x26; $i < 0x36; $i += 2)
335        {
336                add_standard_timing($timings, $bytes->[$i], $bytes->[$i+1]);
337        }
338
339        foreach my $v (sort sort_timings values(%{$timings}))
340        {
341                print_line("Timing", '%ux%u @ %u Hz%s',
342                           $v->[0], $v->[1], $v->[2],
343                           defined($v->[3]) ? ' ('.$v->[3].')' : '');
344        }
345}
346
347sub extract_string
348{
349        my ($bytes, $offset) = @_;
350        my $string = '';
351
352        for (my $i = 5; $i < 18; $i++)
353        {
354                last if $bytes->[$offset+$i] == 0x0a
355                     || $bytes->[$offset+$i] == 0x00;
356                $string .= chr($bytes->[$offset+$i])
357                        if ($bytes->[$offset+$i] >= 32
358                        && $bytes->[$offset+$i] < 127);
359        }
360        $string =~ s/\s+$//;
361
362        return $string;
363}
364
365# Some blocks contain different information:
366#   0x00, 0x00, 0x00, 0xfa: Additional standard timings block
367#   0x00, 0x00, 0x00, 0xfc: Monitor block
368#   0x00, 0x00, 0x00, 0xfd: Limits block
369#   0x00, 0x00, 0x00, 0xfe: Ascii block
370#   0x00, 0x00, 0x00, 0xff: Serial block
371# Return a reference to a hash containing all information.
372sub extract_detailed_timings
373{
374        my ($bytes) = @_;
375
376        my %info = ('timings' => {});
377
378        for (my $offset = 0x36; $offset < 0x7e; $offset += 18)
379        {
380                if ($bytes->[$offset] == 0x00
381                 && $bytes->[$offset+1] == 0x00
382                 && $bytes->[$offset+2] == 0x00
383                 && $bytes->[$offset+4] == 0x00)
384                {
385                        if ($bytes->[$offset+3] == 0xfa)
386                        {
387                                for (my $i = $offset + 5; $i < $offset + 17; $i += 2)
388                                {
389                                        add_standard_timing($info{'timings'},
390                                                            $bytes->[$i],
391                                                            $bytes->[$i+1]);
392                                }
393                        }
394
395                        elsif ($bytes->[$offset+3] == 0xfc)
396                        {
397                                $info{'monitor'} .= extract_string($bytes, $offset);
398                        }
399
400                        elsif ($bytes->[$offset+3] == 0xfd)
401                        {
402                                $info{'limits'}{'vsync_min'} = $bytes->[$offset+5];
403                                $info{'limits'}{'vsync_max'} = $bytes->[$offset+6];
404                                $info{'limits'}{'hsync_min'} = $bytes->[$offset+7];
405                                $info{'limits'}{'hsync_max'} = $bytes->[$offset+8];
406                                $info{'limits'}{'clock_max'} = $bytes->[$offset+9];
407                        }
408
409                        elsif ($bytes->[$offset+3] == 0xfe)
410                        {
411                                $info{'ascii'} .= extract_string($bytes, $offset);
412                        }
413
414                        elsif ($bytes->[$offset+3] == 0xff)
415                        {
416                                $info{'serial'} .= extract_string($bytes, $offset);
417                        }
418
419                        next;
420                }
421
422                # Detailed Timing
423                my $width = $bytes->[$offset+2] + (($bytes->[$offset+4] & 0xf0) << 4);
424                my $height = $bytes->[$offset+5] + (($bytes->[$offset+7] & 0xf0) << 4);
425                my $clock = extract_word($bytes, $offset) * 10000;
426                my $hblank = $bytes->[$offset+3] + (($bytes->[$offset+4] & 0x0f) << 8);
427                my $vblank = $bytes->[$offset+6] + (($bytes->[$offset+7] & 0x0f) << 8);
428                my $area = ($width + $hblank) * ($height + $vblank);
429                next unless $area; # Should not happen, but...
430                my $refresh = ($clock + $area / 2) / $area; # Proper rounding
431                add_timing($info{'timings'}, [$width, $height, $refresh]);
432        }
433
434        return \%info;
435}
436
437sub print_edid
438{
439        my ($bus, $address) = @_;
440        my $bytes;
441
442        if (-r "/sys/bus/i2c/devices/$bus-00$address/eeprom")
443        {
444                $bytes = get_edid_sysfs($bus, $address);
445        }
446        elsif (-r "/proc/sys/dev/sensors/eeprom-i2c-$bus-$address/00")
447        {
448                $bytes = get_edid_procfs($bus, $address);
449        }
450
451        return 1 unless defined $bytes;
452        return 2 unless good_signature($bytes);
453
454        print_line('Checksum', '%s', verify_checksum($bytes));
455        my $edid_version = extract_byte($bytes, 0x12);
456        my $edid_revision = extract_byte($bytes, 0x13);
457        print_line('EDID Version', '%u.%u', $edid_version,
458                   $edid_revision);
459        if ($edid_version > 1 || $edid_revision > 2)
460        {
461                $standard_scales[0][0] = 16;
462                $standard_scales[0][1] = 10;
463        }
464        else
465        {
466                $standard_scales[0][0] = 1;
467                $standard_scales[0][1] = 1;
468        }
469
470        my $info = extract_detailed_timings($bytes);
471
472        print_line('Manufacturer ID', '%s', extract_manufacturer($bytes, 0x08));
473        print_line('Model Number', '0x%04X', extract_word($bytes, 0x0A));
474        print_line('Model Name', '%s', $info->{'monitor'})
475                if defined $info->{'monitor'};
476
477        if ($info->{'serial'})
478        {
479                print_line('Serial Number', '%s', $info->{'serial'})
480        }
481        elsif ((my $temp = extract_dword($bytes, 0x0C)))
482        {
483                print_line('Serial Number', '%u', $temp)
484        }
485
486        print_line('Manufacture Time', '%u-W%02u',
487                   1990 + extract_byte($bytes, 0x11),
488                   extract_byte($bytes, 0x10));
489        print_line('Display Input', '%s', extract_display_input($bytes, 0x14));
490        print_line('Monitor Size (cm)', '%ux%u', extract_byte($bytes, 0x15),
491                   extract_byte($bytes, 0x16));
492        print_line('Gamma Factor', '%.2f',
493                   1 + extract_byte($bytes, 0x17) / 100.0);
494        print_line('DPMS Modes', '%s', extract_dpms($bytes, 0x18));
495        print_line('Color Mode', '%s', extract_color_mode($bytes, 0x18))
496                if (($bytes->[0x18] & 0x18) != 0x18);
497        print_line('Additional Info', '%s', $info->{'ascii'})
498                if $info->{'ascii'};
499
500        if (defined($info->{'limits'}))
501        {
502                print_line('Vertical Sync (Hz)', '%u-%u',
503                           $info->{'limits'}{'vsync_min'},
504                           $info->{'limits'}{'vsync_max'});
505                print_line('Horizontal Sync (kHz)', '%u-%u',
506                           $info->{'limits'}{'hsync_min'},
507                           $info->{'limits'}{'hsync_max'});
508                print_line('Max Pixel Clock (MHz)', '%u',
509                           $info->{'limits'}{'clock_max'} * 10)
510                        unless $info->{'limits'}{'clock_max'} == 0xff;
511        }
512
513        print_timings($bytes, $info->{'timings'});
514        print("\n");
515        return 0;
516}
517
518# Get the address. Default to 0x50 if not given.
519my $address;
520if (defined($ARGV[1]))
521{
522        $address = $ARGV[1];
523        # Convert to decimal, whatever the value.
524        $address = oct $address if $address =~ m/^0/;
525        # Convert to an hexadecimal string.
526        $address = sprintf '%02x', $address;
527}
528else
529{
530        $address = '50';
531}
532
533if (defined($ARGV[0]))
534{
535        my $error = print_edid($ARGV[0], $address);
536       
537        if ($error == 1)
538        {
539                print STDERR
540                        "No EEPROM found at 0x$address on bus $ARGV[0].\n";
541                exit 1;
542        }
543        elsif ($error == 2)
544        {
545                print STDERR
546                        "EEPROM found at 0x$address on bus $ARGV[0], but is not an EDID EEPROM.\n";
547                exit 1;
548        }
549}
550# If no bus is given, try them all.
551else
552{
553        my $total = 0;
554
555        for (my $i = 0; $i < 16; $i++)
556        {
557                $total++ unless print_edid($i, $address);
558        }
559
560        unless ($total)
561        {
562                print STDERR
563                        "No EDID EEPROM found.\n";
564                exit 1;
565        }
566}
Note: See TracBrowser for help on using the browser.