| 1 | #!/usr/bin/perl -w |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2003-2006 Jean Delvare <khali@linux-fr.org> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | # |
|---|
| 19 | # Version 0.1 2003-07-17 Jean Delvare <khali@linux-fr.org> |
|---|
| 20 | # Version 0.2 2003-07-22 Jean Delvare <khali@linux-fr.org> |
|---|
| 21 | # Use print instead of syswrite. |
|---|
| 22 | # Version 0.3 2003-08-24 Jean Delvare <khali@linux-fr.org> |
|---|
| 23 | # Fix data block length (128 bytes instead of 256). |
|---|
| 24 | # Version 1.0 2004-02-08 Jean Delvare <khali@linux-fr.org> |
|---|
| 25 | # Added support for Linux 2.5/2.6 (i.e. sysfs). |
|---|
| 26 | # Version 1.1 2006-09-01 Jean Delvare <khali@linux-fr.org> |
|---|
| 27 | # Append /usr/sbin or /usr/local/sbin to $PATH if needed. |
|---|
| 28 | # |
|---|
| 29 | # EEPROM data decoding for EDID. EDID (Extended Display Identification |
|---|
| 30 | # Data) is a VESA standard which allows storing (on manufacturer's side) |
|---|
| 31 | # and retrieving (on user's side) of configuration information about |
|---|
| 32 | # displays, such as manufacturer, serial number, physical dimensions and |
|---|
| 33 | # allowed horizontal and vertical refresh rates. |
|---|
| 34 | # |
|---|
| 35 | # Using the eeprom kernel driver, you have two possibilities to |
|---|
| 36 | # make use of these data: |
|---|
| 37 | # 1* The ddcmon script. |
|---|
| 38 | # 2* This script. |
|---|
| 39 | # Both solutions will return a different kind of information. The first |
|---|
| 40 | # method will report user-interesting information, such as the model number |
|---|
| 41 | # or the year of manufacturing. The second method will report video-card- |
|---|
| 42 | # interesting information, such as video modes and refresh rates. |
|---|
| 43 | # |
|---|
| 44 | # Note that this script does almost nothing by itself. It simply converts |
|---|
| 45 | # what it finds in /proc to binary data to feed the parse-edid program. |
|---|
| 46 | # The parse-edid program was written by John Fremlin and is available at |
|---|
| 47 | # the following address: |
|---|
| 48 | # http://john.fremlin.de/programs/linux/read-edid/ |
|---|
| 49 | |
|---|
| 50 | use strict; |
|---|
| 51 | use Fcntl qw(:DEFAULT :seek); |
|---|
| 52 | use vars qw($bus $address); |
|---|
| 53 | use constant PROCFS => 1; |
|---|
| 54 | use constant SYSFS => 2; |
|---|
| 55 | |
|---|
| 56 | # parse-edid will typically be installed in /usr/sbin or /usr/local/sbin |
|---|
| 57 | # even though regular users can run it |
|---|
| 58 | $ENV{PATH} .= ':/usr/local/sbin' |
|---|
| 59 | if $ENV{PATH} !~ m,(^|:)/usr/local/sbin/?(:|$), |
|---|
| 60 | && -x '/usr/local/sbin/parse-edid'; |
|---|
| 61 | $ENV{PATH} .= ':/usr/sbin' |
|---|
| 62 | if $ENV{PATH} !~ m,(^|:)/usr/sbin/?(:|$), |
|---|
| 63 | && -x '/usr/sbin/parse-edid'; |
|---|
| 64 | |
|---|
| 65 | sub edid_valid_procfs |
|---|
| 66 | { |
|---|
| 67 | my ($bus, $addr) = @_; |
|---|
| 68 | |
|---|
| 69 | open EEDATA, "/proc/sys/dev/sensors/eeprom-i2c-$bus-$addr/00"; |
|---|
| 70 | my $line = <EEDATA>; |
|---|
| 71 | close EEDATA; |
|---|
| 72 | return 1 |
|---|
| 73 | if $line =~ m/^0 255 255 255 255 255 255 0 /; |
|---|
| 74 | return 0; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | # Only used for sysfs |
|---|
| 78 | sub rawread |
|---|
| 79 | { |
|---|
| 80 | my ($filename, $length, $offset) = @_; |
|---|
| 81 | my $bytes = ''; |
|---|
| 82 | |
|---|
| 83 | sysopen(FH, $filename, O_RDONLY) |
|---|
| 84 | or die "Can't open $filename"; |
|---|
| 85 | if ($offset) |
|---|
| 86 | { |
|---|
| 87 | sysseek(FH, $offset, SEEK_SET) |
|---|
| 88 | or die "Can't seek in $filename"; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | $offset = 0; |
|---|
| 92 | while ($length) |
|---|
| 93 | { |
|---|
| 94 | my $r = sysread(FH, $bytes, $length, $offset); |
|---|
| 95 | die "Can't read $filename" |
|---|
| 96 | unless defined($r); |
|---|
| 97 | die "Unexpected EOF in $filename" |
|---|
| 98 | unless $r; |
|---|
| 99 | $offset += $r; |
|---|
| 100 | $length -= $r; |
|---|
| 101 | } |
|---|
| 102 | close(FH); |
|---|
| 103 | |
|---|
| 104 | return $bytes; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | sub edid_valid_sysfs |
|---|
| 108 | { |
|---|
| 109 | my ($bus, $addr) = @_; |
|---|
| 110 | my $bytes = rawread("/sys/bus/i2c/devices/$bus-00$addr/eeprom", 8, 0); |
|---|
| 111 | |
|---|
| 112 | return 1 |
|---|
| 113 | if $bytes eq "\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00"; |
|---|
| 114 | return 0; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | sub bus_detect |
|---|
| 118 | { |
|---|
| 119 | my $max = shift; |
|---|
| 120 | |
|---|
| 121 | for (my $i=0; $i<$max; $i++) |
|---|
| 122 | { |
|---|
| 123 | if (-r "/proc/sys/dev/sensors/eeprom-i2c-$i-50/00") |
|---|
| 124 | { |
|---|
| 125 | if (edid_valid_procfs($i, '50')) |
|---|
| 126 | { |
|---|
| 127 | print STDERR |
|---|
| 128 | "decode-edid: using bus $i (autodetected)\n"; |
|---|
| 129 | return $i; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | elsif (-r "/sys/bus/i2c/devices/$i-0050/eeprom") |
|---|
| 133 | { |
|---|
| 134 | if (edid_valid_sysfs($i, '50')) |
|---|
| 135 | { |
|---|
| 136 | print STDERR |
|---|
| 137 | "decode-edid: using bus $i (autodetected)\n"; |
|---|
| 138 | return $i; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | return; # default |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | sub edid_decode |
|---|
| 147 | { |
|---|
| 148 | my ($bus, $addr, $mode) = @_; |
|---|
| 149 | |
|---|
| 150 | # Make sure it is an EDID EEPROM. |
|---|
| 151 | |
|---|
| 152 | unless (($mode == PROCFS && edid_valid_procfs ($bus, $addr)) |
|---|
| 153 | || ($mode == SYSFS && edid_valid_sysfs ($bus, $addr))) |
|---|
| 154 | { |
|---|
| 155 | print STDERR |
|---|
| 156 | "decode-edid: not an EDID EEPROM at $bus-$addr\n"; |
|---|
| 157 | return; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | $SIG{__WARN__} = sub { }; |
|---|
| 161 | open PIPE, "| parse-edid" |
|---|
| 162 | or die "Can't open parse-edid. Please install read-edid.\n"; |
|---|
| 163 | delete $SIG{__WARN__}; |
|---|
| 164 | binmode PIPE; |
|---|
| 165 | |
|---|
| 166 | if ($mode == PROCFS) |
|---|
| 167 | { |
|---|
| 168 | for (my $i=0; $i<=0x70; $i+=0x10) |
|---|
| 169 | { |
|---|
| 170 | my $file = sprintf '%02x', $i; |
|---|
| 171 | my $output = ''; |
|---|
| 172 | open EEDATA, "/proc/sys/dev/sensors/eeprom-i2c-$bus-$addr/$file" |
|---|
| 173 | or die "Can't read /proc/sys/dev/sensors/eeprom-i2c-$bus-$addr/$file"; |
|---|
| 174 | while(<EEDATA>) |
|---|
| 175 | { |
|---|
| 176 | foreach my $item (split) |
|---|
| 177 | { |
|---|
| 178 | $output .= pack "C", $item; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | close EEDATA; |
|---|
| 182 | print PIPE $output; |
|---|
| 183 | } |
|---|
| 184 | } |
|---|
| 185 | elsif ($mode == SYSFS) |
|---|
| 186 | { |
|---|
| 187 | print PIPE rawread("/sys/bus/i2c/devices/$bus-00$address/eeprom", 128, 0); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | close PIPE; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | # Get the address. Default to 0x50 if not given. |
|---|
| 194 | $address = $ARGV[1] || 0x50; |
|---|
| 195 | # Convert to decimal, whatever the value. |
|---|
| 196 | $address = oct $address if $address =~ m/^0/; |
|---|
| 197 | # Convert to an hexadecimal string. |
|---|
| 198 | $address = sprintf '%02x', $address; |
|---|
| 199 | |
|---|
| 200 | # Get the bus. Try to autodetect if not given. |
|---|
| 201 | $bus = $ARGV[0] if defined $ARGV[0]; |
|---|
| 202 | $bus = bus_detect(8) unless defined $bus; |
|---|
| 203 | |
|---|
| 204 | if(defined $bus) |
|---|
| 205 | { |
|---|
| 206 | print STDERR |
|---|
| 207 | "decode-edid: decode-edid version 1.1\n"; |
|---|
| 208 | if (-r "/proc/sys/dev/sensors/eeprom-i2c-$bus-$address") |
|---|
| 209 | { |
|---|
| 210 | edid_decode ($bus, $address, PROCFS); |
|---|
| 211 | exit 0; |
|---|
| 212 | } |
|---|
| 213 | elsif (-r "/sys/bus/i2c/devices/$bus-00$address") |
|---|
| 214 | { |
|---|
| 215 | edid_decode ($bus, $address, SYSFS); |
|---|
| 216 | exit 0; |
|---|
| 217 | } |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | print STDERR |
|---|
| 221 | "EDID EEPROM not found. Please make sure that the eeprom module is loaded.\n"; |
|---|
| 222 | print STDERR |
|---|
| 223 | "Maybe your EDID EEPROM is on another bus. Try \"decode-edid ".($bus+1)."\".\n" |
|---|
| 224 | if defined $bus; |
|---|