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