cec-edid.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cec-edid - HDMI Consumer Electronics Control EDID & CEC helper functions
  4. *
  5. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/types.h>
  10. #include <media/cec.h>
  11. /*
  12. * This EDID is expected to be a CEA-861 compliant, which means that there are
  13. * at least two blocks and one or more of the extensions blocks are CEA-861
  14. * blocks.
  15. *
  16. * The returned location is guaranteed to be < size - 1.
  17. */
  18. static unsigned int cec_get_edid_spa_location(const u8 *edid, unsigned int size)
  19. {
  20. unsigned int blocks = size / 128;
  21. unsigned int block;
  22. u8 d;
  23. /* Sanity check: at least 2 blocks and a multiple of the block size */
  24. if (blocks < 2 || size % 128)
  25. return 0;
  26. /*
  27. * If there are fewer extension blocks than the size, then update
  28. * 'blocks'. It is allowed to have more extension blocks than the size,
  29. * since some hardware can only read e.g. 256 bytes of the EDID, even
  30. * though more blocks are present. The first CEA-861 extension block
  31. * should normally be in block 1 anyway.
  32. */
  33. if (edid[0x7e] + 1 < blocks)
  34. blocks = edid[0x7e] + 1;
  35. for (block = 1; block < blocks; block++) {
  36. unsigned int offset = block * 128;
  37. /* Skip any non-CEA-861 extension blocks */
  38. if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
  39. continue;
  40. /* search Vendor Specific Data Block (tag 3) */
  41. d = edid[offset + 2] & 0x7f;
  42. /* Check if there are Data Blocks */
  43. if (d <= 4)
  44. continue;
  45. if (d > 4) {
  46. unsigned int i = offset + 4;
  47. unsigned int end = offset + d;
  48. /* Note: 'end' is always < 'size' */
  49. do {
  50. u8 tag = edid[i] >> 5;
  51. u8 len = edid[i] & 0x1f;
  52. if (tag == 3 && len >= 5 && i + len <= end &&
  53. edid[i + 1] == 0x03 &&
  54. edid[i + 2] == 0x0c &&
  55. edid[i + 3] == 0x00)
  56. return i + 4;
  57. i += len + 1;
  58. } while (i < end);
  59. }
  60. }
  61. return 0;
  62. }
  63. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  64. unsigned int *offset)
  65. {
  66. unsigned int loc = cec_get_edid_spa_location(edid, size);
  67. if (offset)
  68. *offset = loc;
  69. if (loc == 0)
  70. return CEC_PHYS_ADDR_INVALID;
  71. return (edid[loc] << 8) | edid[loc + 1];
  72. }
  73. EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
  74. void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
  75. {
  76. unsigned int loc = cec_get_edid_spa_location(edid, size);
  77. u8 sum = 0;
  78. unsigned int i;
  79. if (loc == 0)
  80. return;
  81. edid[loc] = phys_addr >> 8;
  82. edid[loc + 1] = phys_addr & 0xff;
  83. loc &= ~0x7f;
  84. /* update the checksum */
  85. for (i = loc; i < loc + 127; i++)
  86. sum += edid[i];
  87. edid[i] = 256 - sum;
  88. }
  89. EXPORT_SYMBOL_GPL(cec_set_edid_phys_addr);
  90. u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  91. {
  92. /* Check if input is sane */
  93. if (WARN_ON(input == 0 || input > 0xf))
  94. return CEC_PHYS_ADDR_INVALID;
  95. if (phys_addr == 0)
  96. return input << 12;
  97. if ((phys_addr & 0x0fff) == 0)
  98. return phys_addr | (input << 8);
  99. if ((phys_addr & 0x00ff) == 0)
  100. return phys_addr | (input << 4);
  101. if ((phys_addr & 0x000f) == 0)
  102. return phys_addr | input;
  103. /*
  104. * All nibbles are used so no valid physical addresses can be assigned
  105. * to the input.
  106. */
  107. return CEC_PHYS_ADDR_INVALID;
  108. }
  109. EXPORT_SYMBOL_GPL(cec_phys_addr_for_input);
  110. int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  111. {
  112. int i;
  113. if (parent)
  114. *parent = phys_addr;
  115. if (port)
  116. *port = 0;
  117. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  118. return 0;
  119. for (i = 0; i < 16; i += 4)
  120. if (phys_addr & (0xf << i))
  121. break;
  122. if (i == 16)
  123. return 0;
  124. if (parent)
  125. *parent = phys_addr & (0xfff0 << i);
  126. if (port)
  127. *port = (phys_addr >> i) & 0xf;
  128. for (i += 4; i < 16; i += 4)
  129. if ((phys_addr & (0xf << i)) == 0)
  130. return -EINVAL;
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(cec_phys_addr_validate);