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