]> sigrok.org Git - libsigrokdecode.git/blob - decoders/edid/pd.py
eded: Add all sections.
[libsigrokdecode.git] / decoders / edid / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 ##
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
18 ##
19
20 # TODO:
21 #    - EDID < 1.3
22 #    - add short annotations
23 #    - Signal level standard field in basic display parameters block
24 #    - Additional color point descriptors
25 #    - Additional standard timing descriptors
26 #    - Extensions
27
28 import sigrokdecode as srd
29 import os
30
31 EDID_HEADER = [0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00]
32 OFF_VENDOR = 8
33 OFF_VERSION = 18
34 OFF_BASIC = 20
35 OFF_CHROM = 25
36 OFF_EST_TIMING = 35
37 OFF_STD_TIMING = 38
38 OFF_DET_TIMING = 54
39 OFF_NUM_EXT = 126
40 OFF_CHECKSUM = 127
41
42 # Pre-EDID established timing modes
43 est_modes = [
44     '720x400@70Hz',
45     '720x400@88Hz',
46     '640x480@60Hz',
47     '640x480@67Hz',
48     '640x480@72Hz',
49     '640x480@75Hz',
50     '800x600@56Hz',
51     '800x600@60Hz',
52     '800x600@72Hz',
53     '800x600@75Hz',
54     '832x624@75Hz',
55     '1024x768@87Hz(i)',
56     '1024x768@60Hz',
57     '1024x768@70Hz',
58     '1024x768@75Hz',
59     '1280x1024@75Hz',
60     '1152x870@75Hz',
61 ]
62
63 # X:Y display aspect ratios, as used in standard timing modes
64 xy_ratio = [
65     (16, 10),
66     (4, 3),
67     (5, 4),
68     (16, 9),
69 ]
70
71 # Annotation classes
72 ANN_FIELDS = 0
73 ANN_SECTIONS = 1
74
75 class Decoder(srd.Decoder):
76     api_version = 2
77     id = 'edid'
78     name = 'EDID'
79     longname = 'Extended Display Identification Data'
80     desc = 'Data structure describing display device capabilities.'
81     license = 'gplv3+'
82     inputs = ['i2c']
83     outputs = ['edid']
84     annotations = (
85         ('fields', 'EDID structure fields'),
86         ('sections', 'EDID structure sections'),
87     )
88
89     def __init__(self, **kwargs):
90         self.state = None
91         # Received data items, used as an index into samplenum/data
92         self.cnt = 0
93         # Start/end sample numbers per data item
94         self.sn = []
95         # Received data
96         self.cache = []
97
98     def start(self):
99         self.out_ann = self.register(srd.OUTPUT_ANN)
100
101     def decode(self, ss, es, data):
102         cmd, data = data
103
104         # We only care about actual data bytes that are read (for now).
105         if cmd != 'DATA READ':
106             return
107
108         self.cnt += 1
109         self.sn.append([ss, es])
110         self.cache.append(data)
111         # debug
112
113         if self.state is None:
114             # Wait for the EDID header
115             if self.cnt >= OFF_VENDOR:
116                 if self.cache[-8:] == EDID_HEADER:
117                     # Throw away any garbage before the header
118                     self.sn = self.sn[-8:]
119                     self.cache = self.cache[-8:]
120                     self.cnt = 8
121                     self.state = 'edid'
122                     self.put(self.sn[0][0], es, self.out_ann,
123                             [ANN_SECTIONS, ['Header']])
124                     self.put(self.sn[0][0], es, self.out_ann,
125                             [ANN_FIELDS, ['Header pattern']])
126         elif self.state == 'edid':
127             if self.cnt == OFF_VERSION:
128                 self.decode_vid(-10)
129                 self.decode_pid(-8)
130                 self.decode_serial(-6)
131                 self.decode_mfrdate(-2)
132                 self.put(self.sn[OFF_VENDOR][0], es, self.out_ann,
133                         [ANN_SECTIONS, ['Vendor/product']])
134             elif self.cnt == OFF_BASIC:
135                 self.put(self.sn[OFF_VERSION][0], es, self.out_ann,
136                         [ANN_SECTIONS, ['EDID Version']])
137                 self.put(self.sn[OFF_VERSION][0], self.sn[OFF_VERSION][1],
138                         self.out_ann, [ANN_FIELDS,
139                             ["Version %d" % self.cache[-2]]])
140                 self.put(self.sn[OFF_VERSION+1][0], self.sn[OFF_VERSION+1][1],
141                         self.out_ann, [ANN_FIELDS,
142                             [ "Revision %d" % self.cache[-1]]])
143             elif self.cnt == OFF_CHROM:
144                 self.put(self.sn[OFF_BASIC][0], es, self.out_ann,
145                         [ANN_SECTIONS, ['Basic display']])
146                 self.decode_basicdisplay(-5)
147             elif self.cnt == OFF_EST_TIMING:
148                 self.put(self.sn[OFF_CHROM][0], es, self.out_ann,
149                         [ANN_SECTIONS, ['Color characteristics']])
150                 self.decode_chromaticity(-10)
151             elif self.cnt == OFF_STD_TIMING:
152                 self.put(self.sn[OFF_EST_TIMING][0], es, self.out_ann,
153                         [ANN_SECTIONS, ['Established timings']])
154                 self.decode_est_timing(-3)
155             elif self.cnt == OFF_DET_TIMING:
156                 self.put(self.sn[OFF_STD_TIMING][0], es, self.out_ann,
157                         [ANN_SECTIONS, ['Standard timings']])
158                 self.decode_std_timing(self.cnt - 16)
159             elif self.cnt == OFF_NUM_EXT:
160                 self.decode_descriptors(-72)
161             elif self.cnt == OFF_CHECKSUM:
162                 self.put(ss, es, self.out_ann,
163                     [0, ['Extensions present: %d' % self.cache[self.cnt-1]]])
164             elif self.cnt == OFF_CHECKSUM+1:
165                 checksum = 0
166                 for i in range(128):
167                     checksum += self.cache[i]
168                 if checksum % 256 == 0:
169                     csstr = 'OK'
170                 else:
171                     csstr = 'WRONG!'
172                 self.put(ss, es, self.out_ann, [0, ['Checksum: %d (%s)' % (
173                          self.cache[self.cnt-1], csstr)]])
174                 self.state = 'extensions'
175         elif self.state == 'extensions':
176             pass
177
178     def ann_field(self, start, end, annotation):
179         self.put(self.sn[start][0], self.sn[end][1],
180                  self.out_ann, [ANN_FIELDS, [annotation]])
181
182     def lookup_pnpid(self, pnpid):
183         pnpid_file = os.path.join(os.path.dirname(__file__), 'pnpids.txt')
184         if os.path.exists(pnpid_file):
185             for line in open(pnpid_file).readlines():
186                 if line.find(pnpid + ';') == 0:
187                     return line[4:].strip()
188         return ''
189
190     def decode_vid(self, offset):
191         pnpid = chr(64 + ((self.cache[offset] & 0x7c) >> 2))
192         pnpid += chr(64 + (((self.cache[offset] & 0x03) << 3)
193                            | ((self.cache[offset+1] & 0xe0) >> 5)))
194         pnpid += chr(64 + (self.cache[offset+1] & 0x1f))
195         vendor = self.lookup_pnpid(pnpid)
196         if vendor:
197             pnpid += ' (%s)' % vendor
198         self.ann_field(offset, offset+1, pnpid)
199
200     def decode_pid(self, offset):
201         pidstr = 'Product 0x%.2x%.2x' % (self.cache[offset+1], self.cache[offset])
202         self.ann_field(offset, offset+1, pidstr)
203
204     def decode_serial(self, offset):
205         serialnum = (self.cache[offset+3] << 24) \
206                 + (self.cache[offset+2] << 16) \
207                 + (self.cache[offset+1] << 8) \
208                 + self.cache[offset]
209         serialstr = ''
210         is_alnum = True
211         for i in range(4):
212             if not chr(self.cache[offset+3-i]).isalnum():
213                 is_alnum = False
214                 break
215             serialstr += chr(self.cache[offset+3-i])
216         serial = serialstr if is_alnum else str(serialnum)
217         self.ann_field(offset, offset+3, 'Serial ' + serial)
218
219     def decode_mfrdate(self, offset):
220         datestr = ''
221         if self.cache[offset]:
222             datestr += 'week %d, ' % self.cache[offset]
223         datestr += str(1990 + self.cache[offset+1])
224         if datestr:
225             self.ann_field(offset, offset+1, 'Manufactured ' + datestr)
226
227     def decode_basicdisplay(self, offset):
228         # Video input definition
229         vid = self.cache[offset]
230         if vid & 0x80:
231             # Digital
232             self.ann_field(offset, offset, 'Video input: VESA DFP 1.')
233         else:
234             # Analog
235             sls = (vid & 60) >> 5
236             self.ann_field(offset, offset, 'Signal level standard: %.2x' % sls)
237             if vid & 0x10:
238                 self.ann_field(offset, offset, 'Blank-to-black setup expected')
239             syncs = ''
240             if vid & 0x08:
241                 syncs += 'separate syncs, '
242             if vid & 0x04:
243                 syncs += 'composite syncs, '
244             if vid & 0x02:
245                 syncs += 'sync on green, '
246             if vid & 0x01:
247                 syncs += 'Vsync serration required, '
248             if syncs:
249                 self.ann_field(offset, offset, 'Supported syncs: %s' % syncs[:-2])
250         # Max horizontal/vertical image size
251         if self.cache[offset+1] != 0 and self.cache[offset+2] != 0:
252             # Projectors have this set to 0
253             sizestr = '%dx%dcm' % (self.cache[offset+1], self.cache[offset+2])
254             self.ann_field(offset+1, offset+2, 'Physical size: ' + sizestr)
255         # Display transfer characteristic (gamma)
256         if self.cache[offset+3] != 0xff:
257             gamma = (self.cache[offset+3] + 100) / 100
258             self.ann_field(offset+3, offset+3, 'Gamma: %1.2f' % gamma)
259         # Feature support
260         fs = self.cache[offset+4]
261         dpms = ''
262         if fs & 0x80:
263             dpms += 'standby, '
264         if fs & 0x40:
265             dpms += 'suspend, '
266         if fs & 0x20:
267             dpms += 'active off, '
268         if dpms:
269             self.ann_field(offset+4, offset+4, 'DPMS support: %s' % dpms[:-2])
270         dt = (fs & 0x18) >> 3
271         dtstr = ''
272         if dt == 0:
273             dtstr = 'Monochrome'
274         elif dt == 1:
275             dtstr = 'RGB color'
276         elif dt == 2:
277             dtstr = 'non-RGB multicolor'
278         if dtstr:
279             self.ann_field(offset+4, offset+4, 'Display type: %s' % dtstr)
280         if fs & 0x04:
281             self.ann_field(offset+4, offset+4, 'Color space: standard sRGB')
282         # Save this for when we decode the first detailed timing descriptor
283         self.have_preferred_timing = (fs & 0x02) == 0x02
284         if fs & 0x01:
285             gft = ''
286         else:
287             gft = 'not '
288         self.ann_field(offset+4, offset+4,
289                        'Generalized timing formula: %ssupported' % gft)
290
291     def convert_color(self, value):
292         # Convert from 10-bit packet format to float
293         outval = 0.0
294         for i in range(10):
295             if value & 0x01:
296                 outval += 2 ** -(10-i)
297             value >>= 1
298         return outval
299
300     def decode_chromaticity(self, offset):
301         redx = (self.cache[offset+2] << 2) + ((self.cache[offset] & 0xc0) >> 6)
302         redy = (self.cache[offset+3] << 2) + ((self.cache[offset] & 0x30) >> 4)
303         self.ann_field(offset, offset+9, 'Chromacity red: X %1.3f, Y %1.3f' % (
304                        self.convert_color(redx), self.convert_color(redy)))
305
306         greenx = (self.cache[offset+4] << 2) + ((self.cache[offset] & 0x0c) >> 6)
307         greeny = (self.cache[offset+5] << 2) + ((self.cache[offset] & 0x03) >> 4)
308         self.ann_field(offset, offset+9, 'Chromacity green: X %1.3f, Y %1.3f' % (
309                        self.convert_color(greenx), self.convert_color(greeny)))
310
311         bluex = (self.cache[offset+6] << 2) + ((self.cache[offset+1] & 0xc0) >> 6)
312         bluey = (self.cache[offset+7] << 2) + ((self.cache[offset+1] & 0x30) >> 4)
313         self.ann_field(offset, offset+9, 'Chromacity blue: X %1.3f, Y %1.3f' % (
314                        self.convert_color(bluex), self.convert_color(bluey)))
315
316         whitex = (self.cache[offset+8] << 2) + ((self.cache[offset+1] & 0x0c) >> 6)
317         whitey = (self.cache[offset+9] << 2) + ((self.cache[offset+1] & 0x03) >> 4)
318         self.ann_field(offset, offset+9, 'Chromacity white: X %1.3f, Y %1.3f' % (
319                        self.convert_color(whitex), self.convert_color(whitey)))
320
321     def decode_est_timing(self, offset):
322         # Pre-EDID modes
323         bitmap = (self.cache[offset] << 9) \
324             + (self.cache[offset+1] << 1) \
325             + ((self.cache[offset+2] & 0x80) >> 7)
326         modestr = ''
327         for i in range(17):
328                 if bitmap & (1 << (16-i)):
329                     modestr += est_modes[i] + ', '
330         if modestr:
331             self.ann_field(offset, offset+2,
332                            'Supported establised modes: %s' % modestr[:-2])
333
334     def decode_std_timing(self, offset):
335         modestr = ''
336         for i in range(0, 16, 2):
337             if self.cache[offset+i] == 0x01 and self.cache[offset+i+1] == 0x01:
338                 # Unused field
339                 continue
340             x = (self.cache[offset+i] + 31) * 8
341             ratio = (self.cache[offset+i+1] & 0xc0) >> 6
342             ratio_x, ratio_y = xy_ratio[ratio]
343             y = x / ratio_x * ratio_y
344             refresh = (self.cache[offset+i+1] & 0x3f) + 60
345             modestr += '%dx%d@%dHz, ' % (x, y, refresh)
346         if modestr:
347             self.ann_field(offset, offset + 15,
348                     'Supported standard modes: %s' % modestr[:-2])
349
350     def decode_detailed_timing(self, offset):
351         if offset == -72 and self.have_preferred_timing:
352             # Only on first detailed timing descriptor
353             section = 'Preferred'
354         else:
355             section = 'Detailed'
356         section += ' timing descriptor'
357         self.put(self.sn[offset][0], self.sn[offset+17][1],
358              self.out_ann, [ANN_SECTIONS, [section]])
359
360         pixclock = float((self.cache[offset+1] << 8) + self.cache[offset]) / 100
361         self.ann_field(offset, offset+1, 'Pixel clock: %.2f MHz' % pixclock)
362
363         horiz_active = ((self.cache[offset+4] & 0xf0) << 4) + self.cache[offset+2]
364         self.ann_field(offset+2, offset+4, 'Horizontal active: %d' % horiz_active)
365
366         horiz_blank = ((self.cache[offset+4] & 0x0f) << 8) + self.cache[offset+3]
367         self.ann_field(offset+2, offset+4, 'Horizontal blanking: %d' % horiz_blank)
368
369         vert_active = ((self.cache[offset+7] & 0xf0) << 4) + self.cache[offset+5]
370         self.ann_field(offset+5, offset+7, 'Vertical active: %d' % vert_active)
371
372         vert_blank = ((self.cache[offset+7] & 0x0f) << 8) + self.cache[offset+6]
373         self.ann_field(offset+5, offset+7, 'Vertical blanking: %d' % vert_blank)
374
375         horiz_sync_off = ((self.cache[offset+11] & 0xc0) << 2) + self.cache[offset+8]
376         self.ann_field(offset+8, offset+11, 'Horizontal sync offset: %d' % horiz_sync_off)
377
378         horiz_sync_pw = ((self.cache[offset+11] & 0x30) << 4) + self.cache[offset+9]
379         self.ann_field(offset+8, offset+11, 'Horizontal sync pulse width: %d' % horiz_sync_pw)
380
381         vert_sync_off = ((self.cache[offset+11] & 0x0c) << 2) \
382                     + ((self.cache[offset+10] & 0xf0) >> 4)
383         self.ann_field(offset+8, offset+11, 'Vertical sync offset: %d' % vert_sync_off)
384
385         vert_sync_pw = ((self.cache[offset+11] & 0x03) << 4) \
386                     + (self.cache[offset+10] & 0x0f)
387         self.ann_field(offset+8, offset+11, 'Vertical sync pulse width: %d' % vert_sync_pw)
388
389         horiz_size = ((self.cache[offset+14] & 0xf0) << 4) + self.cache[offset+12]
390         vert_size = ((self.cache[offset+14] & 0x0f) << 8) + self.cache[offset+13]
391         self.ann_field(offset+12, offset+14, 'Physical size: %dx%dmm' % (horiz_size, vert_size))
392
393         horiz_border = self.cache[offset+15]
394         self.ann_field(offset+15, offset+15, 'Horizontal border: %d pixels' % horiz_border)
395         vert_border = self.cache[offset+16]
396         self.ann_field(offset+16, offset+16, 'Vertical border: %d lines' % vert_border)
397
398         features = 'Flags: '
399         if self.cache[offset+17] & 0x80:
400             features += 'interlaced, '
401         stereo = (self.cache[offset+17] & 0x60) >> 5
402         if stereo:
403             if self.cache[offset+17] & 0x01:
404                 features += '2-way interleaved stereo ('
405                 features += ['right image on even lines',
406                              'left image on even lines',
407                              'side-by-side'][stereo-1]
408                 features += '), '
409             else:
410                 features += 'field sequential stereo ('
411                 features += ['right image on sync=1', 'left image on sync=1',
412                              '4-way interleaved'][stereo-1]
413                 features += '), '
414         sync = (self.cache[offset+17] & 0x18) >> 3
415         sync2 = (self.cache[offset+17] & 0x06) >> 1
416         posneg = ['negative', 'positive']
417         features += 'sync type '
418         if sync == 0x00:
419             features += 'analog composite (serrate on RGB)'
420         elif sync == 0x01:
421             features += 'bipolar analog composite (serrate on RGB)'
422         elif sync == 0x02:
423             features += 'digital composite (serrate on composite polarity ' \
424                         + (posneg[sync2 & 0x01]) + ')'
425         elif sync == 0x03:
426             features += 'digital separate ('
427             features += 'Vsync polarity ' + (posneg[(sync2 & 0x02) >> 1])
428             features += ', Hsync polarity ' + (posneg[sync2 & 0x01])
429             features += ')'
430         features += ', '
431         self.ann_field(offset+17, offset+17, features[:-2])
432
433     def decode_descriptor(self, offset):
434         tag = self.cache[offset+3]
435         if tag == 0xff:
436             # Monitor serial number
437             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
438                      [ANN_SECTIONS, ['Serial number']])
439             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
440             self.ann_field(offset, offset+17, text.strip())
441         elif tag == 0xfe:
442             # Text
443             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
444                      [ANN_SECTIONS, ['Text']])
445             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
446             self.ann_field(offset, offset+17, text.strip())
447         elif tag == 0xfc:
448             # Monitor name
449             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
450                      [ANN_SECTIONS, ['Monitor name']])
451             text = bytes(self.cache[offset+5:][:13]).decode(encoding='cp437', errors='replace')
452             self.ann_field(offset, offset+17, text.strip())
453         elif tag == 0xfd:
454             # Monitor range limits
455             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
456                      [ANN_SECTIONS, ['Monitor range limits']])
457             self.ann_field(offset+5, offset+5, 'Minimum vertical rate: %dHz' %
458                            self.cache[offset+5])
459             self.ann_field(offset+6, offset+6, 'Maximum vertical rate: %dHz' %
460                            self.cache[offset+6])
461             self.ann_field(offset+7, offset+7, 'Minimum horizontal rate: %dkHz' %
462                            self.cache[offset+7])
463             self.ann_field(offset+8, offset+8, 'Maximum horizontal rate: %dkHz' %
464                            self.cache[offset+8])
465             self.ann_field(offset+9, offset+9, 'Maximum pixel clock: %dMHz' %
466                            (self.cache[offset+9] * 10))
467             if self.cache[offset+10] == 0x02:
468                 # Secondary GTF curve supported
469                 self.ann_field(offset+10, offset+17, 'Secondary timing formula supported')
470         elif tag == 0xfb:
471             # Additional color point data
472             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
473                      [ANN_SECTIONS, ['Additional color point data']])
474         elif tag == 0xfa:
475             # Additional standard timing definitions
476             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
477                      [ANN_SECTIONS, ['Additional standard timing definitions']])
478         else:
479             self.put(self.sn[offset][0], self.sn[offset+17][1], self.out_ann,
480                      [ANN_SECTIONS, ['Unknown descriptor']])
481
482     def decode_descriptors(self, offset):
483         # 4 consecutive 18-byte descriptor blocks
484         for i in range(offset, 0, 18):
485             if self.cache[i] != 0 and self.cache[i+1] != 0:
486                 self.decode_detailed_timing(i)
487             else:
488                 if self.cache[i+2] == 0 or self.cache[i+4] == 0:
489                     self.decode_descriptor(i)