Debian Codegraph @

utils/edid-decode/ddc.cpp

raw
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2024 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
4 *
5 * Author: Hans Verkuil <[email protected]>
6 */
7
8 #include <cctype>
9 #include <cerrno>
10 #include <csignal>
11 #include <cstring>
12 #include <ctime>
13 #include <string>
14
15 #include <fcntl.h>
16 #include <getopt.h>
17 #include <sys/ioctl.h>
18 #include <sys/epoll.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include <math.h>
24 #include <dirent.h>
25
26 #include <linux/i2c-dev.h>
27 #include <linux/i2c.h>
28 #include <linux/types.h>
29
30 #include "edid-decode.h"
31
32 // i2c addresses for edid
33 #define EDID_ADDR 0x50
34 #define SEGMENT_POINTER_ADDR 0x30
35
36 // i2c address for SCDC
37 #define SCDC_ADDR 0x54
38
39 // i2c addresses for HDCP
40 #define HDCP_PRIM_ADDR 0x3a
41 #define HDCP_SEC_ADDR 0x3b
42
43 static struct timespec start_monotonic;
44 static struct timeval start_timeofday;
45 static time_t valid_until_t;
46
47 static std::string ts2s(__u64 ts)
48 {
49 static char buf[64];
50 static unsigned last_secs;
51 static time_t last_t;
52 std::string s;
53 struct timeval sub = {};
54 struct timeval res;
55 unsigned secs;
56 __s64 diff;
57 time_t t;
58
59 diff = ts - start_monotonic.tv_sec * 1000000000ULL - start_monotonic.tv_nsec;
60 if (diff >= 0) {
61 sub.tv_sec = diff / 1000000000ULL;
62 sub.tv_usec = (diff % 1000000000ULL) / 1000;
63 }
64 timeradd(&start_timeofday, &sub, &res);
65 t = res.tv_sec;
66 if (t >= valid_until_t) {
67 struct tm tm = *localtime(&t);
68 last_secs = tm.tm_min * 60 + tm.tm_sec;
69 last_t = t;
70 valid_until_t = t + 60 - last_secs;
71 strftime(buf, sizeof(buf), "%a %b %e %T.000000", &tm);
72 }
73 secs = last_secs + t - last_t;
74 sprintf(buf + 14, "%02u:%02u.%06llu", secs / 60, secs % 60, (__u64)res.tv_usec);
75 return buf;
76 }
77
78 static __u64 current_ts()
79 {
80 struct timespec ts;
81
82 clock_gettime(CLOCK_MONOTONIC, &ts);
83 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
84 }
85
86
87 int request_i2c_adapter(const char *device)
88 {
89 int fd = open(device, O_RDWR);
90
91 if (fd >= 0)
92 return fd;
93 fprintf(stderr, "Error accessing i2c adapter %s\n", device);
94 return fd;
95 }
96
97 static int read_edid_block(int adapter_fd, __u8 *edid,
98 uint8_t segment, uint8_t offset, uint8_t blocks,
99 bool silent)
100 {
101 struct i2c_rdwr_ioctl_data data;
102 struct i2c_msg write_message;
103 struct i2c_msg read_message;
104 struct i2c_msg seg_message;
105 int err;
106
107 seg_message = {
108 .addr = SEGMENT_POINTER_ADDR,
109 .len = 1,
110 .buf = &segment
111 };
112 write_message = {
113 .addr = EDID_ADDR,
114 .len = 1,
115 .buf = &offset
116 };
117 read_message = {
118 .addr = EDID_ADDR,
119 .flags = I2C_M_RD,
120 .len = (__u16)(blocks * EDID_PAGE_SIZE),
121 .buf = edid
122 };
123
124 if (segment) {
125 struct i2c_msg msgs[2] = { seg_message, read_message };
126
127 data.msgs = msgs;
128 data.nmsgs = ARRAY_SIZE(msgs);
129 err = ioctl(adapter_fd, I2C_RDWR, &data);
130 } else {
131 struct i2c_msg msgs[2] = { write_message, read_message };
132
133 data.msgs = msgs;
134 data.nmsgs = ARRAY_SIZE(msgs);
135 err = ioctl(adapter_fd, I2C_RDWR, &data);
136 }
137
138 if (err < 0) {
139 if (!silent)
140 fprintf(stderr, "Unable to read edid: %s\n", strerror(errno));
141 return err;
142 }
143 return 0;
144 }
145
146 int read_edid(int adapter_fd, unsigned char *edid, bool silent)
147 {
148 unsigned n_extension_blocks;
149 int err;
150
151 err = read_edid_block(adapter_fd, edid, 0, 0, 2, silent);
152 if (err)
153 return err;
154 n_extension_blocks = edid[126];
155 if (!n_extension_blocks)
156 return 1;
157
158 // Check for HDMI Forum EDID Extension Override Data Block
159 if (n_extension_blocks >= 1 &&
160 edid[128] == 2 &&
161 edid[133] == 0x78 &&
162 (edid[132] & 0xe0) == 0xe0 &&
163 (edid[132] & 0x1f) >= 2 &&
164 edid[134] > 1)
165 n_extension_blocks = edid[134];
166
167 for (unsigned i = 2; i <= n_extension_blocks; i += 2) {
168 err = read_edid_block(adapter_fd, edid + i * 128, i / 2, 0,
169 (i + 1 > n_extension_blocks ? 1 : 2),
170 silent);
171 if (err)
172 return err;
173 }
174 return n_extension_blocks + 1;
175 }
176
177 int test_reliability(int adapter_fd, unsigned secs, unsigned msleep)
178 {
179 unsigned char edid[EDID_PAGE_SIZE * EDID_MAX_BLOCKS];
180 unsigned char edid_tmp[EDID_PAGE_SIZE * EDID_MAX_BLOCKS];
181 unsigned iter = 0;
182 unsigned blocks;
183 int ret;
184
185 ret = read_edid(adapter_fd, edid);
186 if (ret <= 0) {
187 printf("FAIL: could not read initial EDID.\n");
188 return ret;
189 }
190 blocks = ret;
191
192 if (secs)
193 printf("Read EDID (%u bytes) for %u seconds with %u milliseconds between each read.\n\n",
194 blocks * EDID_PAGE_SIZE, secs, msleep);
195 else
196 printf("Read EDID (%u bytes) forever with %u milliseconds between each read.\n\n",
197 blocks * EDID_PAGE_SIZE, msleep);
198
199 clock_gettime(CLOCK_MONOTONIC, &start_monotonic);
200 gettimeofday(&start_timeofday, nullptr);
201
202 time_t start = time(NULL);
203 time_t start_test = start;
204
205 while (true) {
206 iter++;
207 if (msleep)
208 usleep(msleep * 1000);
209 ret = read_edid(adapter_fd, edid_tmp);
210 if (ret <= 0) {
211 printf("\nFAIL: failed to read EDID (iteration %u).\n", iter);
212 return -1;
213 }
214 if ((unsigned)ret != blocks) {
215 printf("\nFAIL: expected %u blocks, read %d blocks (iteration %u).\n",
216 blocks, ret, iter);
217 return -1;
218 }
219 if (memcmp(edid, edid_tmp, blocks * EDID_PAGE_SIZE)) {
220 printf("Initial EDID:\n\n");
221 for (unsigned i = 0; i < blocks; i++) {
222 hex_block("", edid + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
223 printf("\n");
224 }
225 printf("EDID of iteration %u:\n\n", iter);
226 for (unsigned i = 0; i < blocks; i++) {
227 hex_block("", edid_tmp + i * EDID_PAGE_SIZE, EDID_PAGE_SIZE, false);
228 printf("\n");
229 }
230 printf("FAIL: mismatch between EDIDs (iteration %u).\n", iter);
231 return -1;
232 }
233 time_t cur = time(NULL);
234 if (secs && cur - start_test > (time_t)secs)
235 break;
236 if (cur - start >= 10) {
237 start = cur;
238 printf("At iteration %u...\n", iter);
239 fflush(stdout);
240 }
241 }
242
243 printf("\n%u iterations over %u seconds: PASS\n", iter, secs);
244 return 0;
245 }
246
247 static int read_hdcp_registers(int adapter_fd, __u8 *hdcp_prim, __u8 *hdcp_sec, __u8 *ksv_fifo)
248 {
249 struct i2c_rdwr_ioctl_data data;
250 struct i2c_msg write_message;
251 struct i2c_msg read_message;
252 __u8 offset = 0;
253 __u8 ksv_fifo_offset = 0x43;
254 int err;
255
256 write_message = {
257 .addr = HDCP_PRIM_ADDR,
258 .len = 1,
259 .buf = &offset
260 };
261 read_message = {
262 .addr = HDCP_PRIM_ADDR,
263 .flags = I2C_M_RD,
264 .len = 256,
265 .buf = hdcp_prim
266 };
267
268 struct i2c_msg msgs[2] = { write_message, read_message };
269
270 data.msgs = msgs;
271 data.nmsgs = ARRAY_SIZE(msgs);
272 err = ioctl(adapter_fd, I2C_RDWR, &data);
273
274 if (err < 0) {
275 fprintf(stderr, "Unable to read Primary Link HDCP: %s\n",
276 strerror(errno));
277 return -1;
278 }
279
280 write_message = {
281 .addr = HDCP_PRIM_ADDR,
282 .len = 1,
283 .buf = &ksv_fifo_offset
284 };
285 read_message = {
286 .addr = HDCP_PRIM_ADDR,
287 .flags = I2C_M_RD,
288 .len = (__u16)((hdcp_prim[0x41] & 0x7f) * 5),
289 .buf = ksv_fifo
290 };
291
292 if (read_message.len) {
293 struct i2c_msg ksv_fifo_msgs[2] = { write_message, read_message };
294
295 data.msgs = ksv_fifo_msgs;
296 data.nmsgs = ARRAY_SIZE(msgs);
297 err = ioctl(adapter_fd, I2C_RDWR, &data);
298 if (err < 0) {
299 fprintf(stderr, "Unable to read KSV FIFO: %s\n",
300 strerror(errno));
301 return -1;
302 }
303 }
304
305 write_message = {
306 .addr = HDCP_SEC_ADDR,
307 .len = 1,
308 .buf = &offset
309 };
310 read_message = {
311 .addr = HDCP_SEC_ADDR,
312 .flags = I2C_M_RD,
313 .len = 256,
314 .buf = hdcp_sec
315 };
316
317 struct i2c_msg sec_msgs[2] = { write_message, read_message };
318 data.msgs = sec_msgs;
319 data.nmsgs = ARRAY_SIZE(msgs);
320 ioctl(adapter_fd, I2C_RDWR, &data);
321
322 return 0;
323 }
324
325 int read_hdcp(int adapter_fd)
326 {
327 __u8 hdcp_prim[256];
328 __u8 hdcp_sec[256];
329 __u8 ksv_fifo[128 * 5];
330
331 hdcp_prim[5] = 0xdd;
332 hdcp_sec[5] = 0xdd;
333 if (read_hdcp_registers(adapter_fd, hdcp_prim, hdcp_sec, ksv_fifo))
334 return -1;
335 printf("HDCP Primary Link Hex Dump:\n\n");
336 hex_block("", hdcp_prim, 128, false);
337 printf("\n");
338 hex_block("", hdcp_prim + 128, 128, false);
339 printf("\n");
340 if (hdcp_sec[5] != 0xdd) {
341 printf("HDCP Secondary Link Hex Dump:\n\n");
342 hex_block("", hdcp_sec, 128, false);
343 printf("\n");
344 hex_block("", hdcp_sec + 128, 128, false);
345 printf("\n");
346 }
347 printf("HDCP Primary Link:\n\n");
348 printf("Bksv: %02x %02x %02x %02x %02x\n",
349 hdcp_prim[0], hdcp_prim[1], hdcp_prim[2], hdcp_prim[3], hdcp_prim[4]);
350 printf("Ri': %02x %02x\n", hdcp_prim[9], hdcp_prim[8]);
351 printf("Pj': %02x\n", hdcp_prim[0x0a]);
352 printf("Aksv: %02x %02x %02x %02x %02x\n",
353 hdcp_prim[0x10], hdcp_prim[0x11], hdcp_prim[0x12], hdcp_prim[0x13], hdcp_prim[0x14]);
354 printf("Ainfo: %02x\n", hdcp_prim[0x15]);
355 printf("An: %02x %02x %02x %02x %02x %02x %02x %02x\n",
356 hdcp_prim[0x18], hdcp_prim[0x19], hdcp_prim[0x1a], hdcp_prim[0x1b],
357 hdcp_prim[0x1c], hdcp_prim[0x1d], hdcp_prim[0x1e], hdcp_prim[0x1f]);
358 printf("V'.H0: %02x %02x %02x %02x\n",
359 hdcp_prim[0x20], hdcp_prim[0x21], hdcp_prim[0x22], hdcp_prim[0x23]);
360 printf("V'.H1: %02x %02x %02x %02x\n",
361 hdcp_prim[0x24], hdcp_prim[0x25], hdcp_prim[0x26], hdcp_prim[0x27]);
362 printf("V'.H2: %02x %02x %02x %02x\n",
363 hdcp_prim[0x28], hdcp_prim[0x29], hdcp_prim[0x2a], hdcp_prim[0x2b]);
364 printf("V'.H3: %02x %02x %02x %02x\n",
365 hdcp_prim[0x2c], hdcp_prim[0x2d], hdcp_prim[0x2e], hdcp_prim[0x2f]);
366 printf("V'.H4: %02x %02x %02x %02x\n",
367 hdcp_prim[0x30], hdcp_prim[0x31], hdcp_prim[0x32], hdcp_prim[0x33]);
368 __u8 v = hdcp_prim[0x40];
369 printf("Bcaps: %02x\n", v);
370 if (v & 0x01)
371 printf("\tFAST_REAUTHENTICATION\n");
372 if (v & 0x02)
373 printf("\t1.1_FEATURES\n");
374 if (v & 0x10)
375 printf("\tFAST\n");
376 if (v & 0x20)
377 printf("\tREADY\n");
378 if (v & 0x40)
379 printf("\tREPEATER\n");
380 __u16 vv = hdcp_prim[0x41] | (hdcp_prim[0x42] << 8);
381 printf("Bstatus: %04x\n", vv);
382 printf("\tDEVICE_COUNT: %u\n", vv & 0x7f);
383 if (vv & 0x80)
384 printf("\tMAX_DEVS_EXCEEDED\n");
385 printf("\tDEPTH: %u\n", (vv >> 8) & 0x07);
386 if (vv & 0x800)
387 printf("\tMAX_CASCADE_EXCEEDED\n");
388 if (vv & 0x1000)
389 printf("\tHDMI_MODE\n");
390 if (vv & 0x7f) {
391 printf("KSV FIFO:\n");
392 for (unsigned i = 0; i < (vv & 0x7f); i++)
393 printf("\t%03u: %02x %02x %02x %02x %02x\n", i,
394 ksv_fifo[i * 5], ksv_fifo[i * 5 + 1],
395 ksv_fifo[i * 5 + 2], ksv_fifo[i * 5 + 3],
396 ksv_fifo[i * 5 + 4]);
397 }
398 v = hdcp_prim[0x50];
399 printf("HDCP2Version: %02x\n", v);
400 if (v & 4)
401 printf("\tHDCP2.2\n");
402 vv = hdcp_prim[0x70] | (hdcp_prim[0x71] << 8);
403 printf("RxStatus: %04x\n", vv);
404 if (vv & 0x3ff)
405 printf("\tMessage_Size: %u\n", vv & 0x3ff);
406 if (vv & 0x400)
407 printf("\tREADY\n");
408 if (vv & 0x800)
409 printf("\tREAUTH_REQ\n");
410
411 if (hdcp_sec[5] == 0xdd)
412 return 0;
413
414 printf("HDCP Secondary Link:\n\n");
415 printf("Bksv: %02x %02x %02x %02x %02x\n",
416 hdcp_sec[0], hdcp_sec[1], hdcp_sec[2], hdcp_sec[3], hdcp_sec[4]);
417 printf("Ri': %02x %02x\n", hdcp_sec[9], hdcp_sec[9]);
418 printf("Pj': %02x\n", hdcp_sec[0x0a]);
419 printf("Aksv: %02x %02x %02x %02x %02x\n",
420 hdcp_sec[0x10], hdcp_sec[0x11], hdcp_sec[0x12], hdcp_sec[0x13], hdcp_sec[0x14]);
421 printf("Ainfo: %02x\n", hdcp_sec[0x15]);
422 printf("An: %02x %02x %02x %02x %02x %02x %02x %02x\n",
423 hdcp_sec[0x18], hdcp_sec[0x19], hdcp_sec[0x1a], hdcp_sec[0x1b],
424 hdcp_sec[0x1c], hdcp_sec[0x1d], hdcp_sec[0x1e], hdcp_sec[0x1f]);
425 return 0;
426 }
427
428 static int read_hdcp_ri_register(int adapter_fd, __u16 *v)
429 {
430 struct i2c_rdwr_ioctl_data data;
431 struct i2c_msg write_message;
432 struct i2c_msg read_message;
433 __u8 ri[2];
434 __u8 offset = 8;
435 int err;
436
437 write_message = {
438 .addr = HDCP_PRIM_ADDR,
439 .len = 1,
440 .buf = &offset
441 };
442 read_message = {
443 .addr = HDCP_PRIM_ADDR,
444 .flags = I2C_M_RD,
445 .len = 2,
446 .buf = ri
447 };
448
449 struct i2c_msg msgs[2] = { write_message, read_message };
450
451 data.msgs = msgs;
452 data.nmsgs = ARRAY_SIZE(msgs);
453 err = ioctl(adapter_fd, I2C_RDWR, &data);
454
455 if (err < 0)
456 fprintf(stderr, "Unable to read Ri: %s\n", strerror(errno));
457 else
458 *v = ri[1] << 8 | ri[0];
459
460 return err < 0 ? err : 0;
461 }
462
463 int read_hdcp_ri(int adapter_fd, double ri_time)
464 {
465 __u64 last_ts = current_ts();
466 bool first = true;
467 __u16 ri = 0;
468 __u16 last_ri = 0;
469
470 clock_gettime(CLOCK_MONOTONIC, &start_monotonic);
471 gettimeofday(&start_timeofday, nullptr);
472
473 while (1) {
474 __u64 ts = current_ts();
475
476 printf("Timestamp: %s", ts2s(ts).c_str());
477 if (!read_hdcp_ri_register(adapter_fd, &ri)) {
478 printf(" Ri': 0x%04x", ri);
479 if (!first && ri != last_ri) {
480 printf(" (changed from 0x%04x after %llu ms)",
481 last_ri, (ts - last_ts) / 1000000);
482 last_ts = ts;
483 last_ri = ri;
484 }
485 first = false;
486 }
487 printf("\n");
488 fflush(stdout);
489 usleep(ri_time * 1000000);
490 }
491 return 0;
492 }