cec-edid.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. edid[i + 1] == 0x03 &&
  66. edid[i + 2] == 0x0c &&
  67. edid[i + 3] == 0x00)
  68. return i + 4;
  69. i += len + 1;
  70. } while (i < end);
  71. }
  72. }
  73. return 0;
  74. }
  75. u16 cec_get_edid_phys_addr(const u8 *edid, unsigned int size,
  76. unsigned int *offset)
  77. {
  78. unsigned int loc = cec_get_edid_spa_location(edid, size);
  79. if (offset)
  80. *offset = loc;
  81. if (loc == 0)
  82. return CEC_PHYS_ADDR_INVALID;
  83. return (edid[loc] << 8) | edid[loc + 1];
  84. }
  85. EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr);
  86. void cec_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr)
  87. {
  88. unsigned int loc = cec_get_edid_spa_location(edid, size);
  89. u8 sum = 0;
  90. unsigned int i;
  91. if (loc == 0)
  92. return;
  93. edid[loc] = phys_addr >> 8;
  94. edid[loc + 1] = phys_addr & 0xff;
  95. loc &= ~0x7f;
  96. /* update the checksum */
  97. for (i = loc; i < loc + 127; i++)
  98. sum += edid[i];
  99. edid[i] = 256 - sum;
  100. }
  101. EXPORT_SYMBOL_GPL(cec_set_edid_phys_addr);
  102. u16 cec_phys_addr_for_input(u16 phys_addr, u8 input)
  103. {
  104. /* Check if input is sane */
  105. if (WARN_ON(input == 0 || input > 0xf))
  106. return CEC_PHYS_ADDR_INVALID;
  107. if (phys_addr == 0)
  108. return input << 12;
  109. if ((phys_addr & 0x0fff) == 0)
  110. return phys_addr | (input << 8);
  111. if ((phys_addr & 0x00ff) == 0)
  112. return phys_addr | (input << 4);
  113. if ((phys_addr & 0x000f) == 0)
  114. return phys_addr | input;
  115. /*
  116. * All nibbles are used so no valid physical addresses can be assigned
  117. * to the input.
  118. */
  119. return CEC_PHYS_ADDR_INVALID;
  120. }
  121. EXPORT_SYMBOL_GPL(cec_phys_addr_for_input);
  122. int cec_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port)
  123. {
  124. int i;
  125. if (parent)
  126. *parent = phys_addr;
  127. if (port)
  128. *port = 0;
  129. if (phys_addr == CEC_PHYS_ADDR_INVALID)
  130. return 0;
  131. for (i = 0; i < 16; i += 4)
  132. if (phys_addr & (0xf << i))
  133. break;
  134. if (i == 16)
  135. return 0;
  136. if (parent)
  137. *parent = phys_addr & (0xfff0 << i);
  138. if (port)
  139. *port = (phys_addr >> i) & 0xf;
  140. for (i += 4; i < 16; i += 4)
  141. if ((phys_addr & (0xf << i)) == 0)
  142. return -EINVAL;
  143. return 0;
  144. }
  145. EXPORT_SYMBOL_GPL(cec_phys_addr_validate);
  146. MODULE_AUTHOR("Hans Verkuil <hans.verkuil@cisco.com>");
  147. MODULE_DESCRIPTION("CEC EDID helper functions");
  148. MODULE_LICENSE("GPL");