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