cec-edid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * cec-edid - HDMI Consumer Electronics Control EDID & CEC helper functions
  3. *
  4. * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  5. *
  6. * This program is free software; you may redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  14. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  15. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/types.h>
  22. #include <media/cec-edid.h>
  23. /*
  24. * This EDID is expected to be a CEA-861 compliant, which means that there are
  25. * at least two blocks and one or more of the extensions blocks are CEA-861
  26. * blocks.
  27. *
  28. * The returned location is guaranteed to be < size - 1.
  29. */
  30. static unsigned int cec_get_edid_spa_location(const u8 *edid, unsigned int size)
  31. {
  32. unsigned int blocks = size / 128;
  33. unsigned int block;
  34. u8 d;
  35. /* Sanity check: at least 2 blocks and a multiple of the block size */
  36. if (blocks < 2 || size % 128)
  37. return 0;
  38. /*
  39. * If there are fewer extension blocks than the size, then update
  40. * 'blocks'. It is allowed to have more extension blocks than the size,
  41. * since some hardware can only read e.g. 256 bytes of the EDID, even
  42. * though more blocks are present. The first CEA-861 extension block
  43. * should normally be in block 1 anyway.
  44. */
  45. if (edid[0x7e] + 1 < blocks)
  46. blocks = edid[0x7e] + 1;
  47. for (block = 1; block < blocks; block++) {
  48. unsigned int offset = block * 128;
  49. /* Skip any non-CEA-861 extension blocks */
  50. if (edid[offset] != 0x02 || edid[offset + 1] != 0x03)
  51. continue;
  52. /* search Vendor Specific Data Block (tag 3) */
  53. d = edid[offset + 2] & 0x7f;
  54. /* Check if there are Data Blocks */
  55. if (d <= 4)
  56. continue;
  57. if (d > 4) {
  58. unsigned int i = offset + 4;
  59. unsigned int end = offset + d;
  60. /* Note: 'end' is always < 'size' */
  61. do {
  62. u8 tag = edid[i] >> 5;
  63. u8 len = edid[i] & 0x1f;
  64. if (tag == 3 && len >= 5 && i + len <= end)
  65. return i + 4;
  66. i += len + 1;
  67. } while (i < end);
  68. }
  69. }
  70. return 0;
  71. }
  72. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  73. unsigned int *offset)
  74. {
  75. unsigned int loc = cec_get_edid_spa_location(edid, size);
  76. if (offset)
  77. *offset = loc;
  78. if (loc == 0)
  79. return CEC_PHYS_ADDR_INVALID;
  80. return (edid[loc] << 8) | edid[loc + 1];
  81. }
  82. EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
  83. void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
  84. {
  85. unsigned int loc = cec_get_edid_spa_location(edid, size);
  86. u8 sum = 0;
  87. unsigned int i;
  88. if (loc == 0)
  89. return;
  90. edid[loc] = phys_addr >> 8;
  91. edid[loc + 1] = phys_addr & 0xff;
  92. loc &= ~0x7f;
  93. /* update the checksum */
  94. for (i = loc; i < loc + 127; i++)
  95. sum += edid[i];
  96. edid[i] = 256 - sum;
  97. }
  98. EXPORT_SYMBOL_GPL(cec_set_edid_phys_addr);
  99. u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  100. {
  101. /* Check if input is sane */
  102. if (WARN_ON(input == 0 || input > 0xf))
  103. return CEC_PHYS_ADDR_INVALID;
  104. if (phys_addr == 0)
  105. return input << 12;
  106. if ((phys_addr & 0x0fff) == 0)
  107. return phys_addr | (input << 8);
  108. if ((phys_addr & 0x00ff) == 0)
  109. return phys_addr | (input << 4);
  110. if ((phys_addr & 0x000f) == 0)
  111. return phys_addr | input;
  112. /*
  113. * All nibbles are used so no valid physical addresses can be assigned
  114. * to the input.
  115. */
  116. return CEC_PHYS_ADDR_INVALID;
  117. }
  118. EXPORT_SYMBOL_GPL(cec_phys_addr_for_input);
  119. int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  120. {
  121. int i;
  122. if (parent)
  123. *parent = phys_addr;
  124. if (port)
  125. *port = 0;
  126. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  127. return 0;
  128. for (i = 0; i < 16; i += 4)
  129. if (phys_addr & (0xf << i))
  130. break;
  131. if (i == 16)
  132. return 0;
  133. if (parent)
  134. *parent = phys_addr & (0xfff0 << i);
  135. if (port)
  136. *port = (phys_addr >> i) & 0xf;
  137. for (i += 4; i < 16; i += 4)
  138. if ((phys_addr & (0xf << i)) == 0)
  139. return -EINVAL;
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(cec_phys_addr_validate);
  143. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  144. MODULE_DESCRIPTION("CEC EDID helper functions");
  145. MODULE_LICENSE("GPL");