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