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