1 // SPDX-License-Identifier: GPL-2.0-only
3 * cec-edid - HDMI Consumer Electronics Control EDID & CEC helper functions
5 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/types.h>
11 #include <media/cec.h>
14 * This EDID is expected to be a CEA-861 compliant, which means that there are
15 * at least two blocks and one or more of the extensions blocks are CEA-861
18 * The returned location is guaranteed to be < size - 1.
20 static unsigned int cec_get_edid_spa_location(const u8
*edid
, unsigned int size
)
22 unsigned int blocks
= size
/ 128;
26 /* Sanity check: at least 2 blocks and a multiple of the block size */
27 if (blocks
< 2 || size
% 128)
31 * If there are fewer extension blocks than the size, then update
32 * 'blocks'. It is allowed to have more extension blocks than the size,
33 * since some hardware can only read e.g. 256 bytes of the EDID, even
34 * though more blocks are present. The first CEA-861 extension block
35 * should normally be in block 1 anyway.
37 if (edid
[0x7e] + 1 < blocks
)
38 blocks
= edid
[0x7e] + 1;
40 for (block
= 1; block
< blocks
; block
++) {
41 unsigned int offset
= block
* 128;
43 /* Skip any non-CEA-861 extension blocks */
44 if (edid
[offset
] != 0x02 || edid
[offset
+ 1] != 0x03)
47 /* search Vendor Specific Data Block (tag 3) */
48 d
= edid
[offset
+ 2] & 0x7f;
49 /* Check if there are Data Blocks */
53 unsigned int i
= offset
+ 4;
54 unsigned int end
= offset
+ d
;
56 /* Note: 'end' is always < 'size' */
58 u8 tag
= edid
[i
] >> 5;
59 u8 len
= edid
[i
] & 0x1f;
61 if (tag
== 3 && len
>= 5 && i
+ len
<= end
&&
62 edid
[i
+ 1] == 0x03 &&
63 edid
[i
+ 2] == 0x0c &&
73 u16
cec_get_edid_phys_addr(const u8
*edid
, unsigned int size
,
76 unsigned int loc
= cec_get_edid_spa_location(edid
, size
);
81 return CEC_PHYS_ADDR_INVALID
;
82 return (edid
[loc
] << 8) | edid
[loc
+ 1];
84 EXPORT_SYMBOL_GPL(cec_get_edid_phys_addr
);
86 void cec_set_edid_phys_addr(u8
*edid
, unsigned int size
, u16 phys_addr
)
88 unsigned int loc
= cec_get_edid_spa_location(edid
, size
);
94 edid
[loc
] = phys_addr
>> 8;
95 edid
[loc
+ 1] = phys_addr
& 0xff;
98 /* update the checksum */
99 for (i
= loc
; i
< loc
+ 127; i
++)
103 EXPORT_SYMBOL_GPL(cec_set_edid_phys_addr
);
105 u16
cec_phys_addr_for_input(u16 phys_addr
, u8 input
)
107 /* Check if input is sane */
108 if (WARN_ON(input
== 0 || input
> 0xf))
109 return CEC_PHYS_ADDR_INVALID
;
114 if ((phys_addr
& 0x0fff) == 0)
115 return phys_addr
| (input
<< 8);
117 if ((phys_addr
& 0x00ff) == 0)
118 return phys_addr
| (input
<< 4);
120 if ((phys_addr
& 0x000f) == 0)
121 return phys_addr
| input
;
124 * All nibbles are used so no valid physical addresses can be assigned
127 return CEC_PHYS_ADDR_INVALID
;
129 EXPORT_SYMBOL_GPL(cec_phys_addr_for_input
);
131 int cec_phys_addr_validate(u16 phys_addr
, u16
*parent
, u16
*port
)
139 if (phys_addr
== CEC_PHYS_ADDR_INVALID
)
141 for (i
= 0; i
< 16; i
+= 4)
142 if (phys_addr
& (0xf << i
))
147 *parent
= phys_addr
& (0xfff0 << i
);
149 *port
= (phys_addr
>> i
) & 0xf;
150 for (i
+= 4; i
< 16; i
+= 4)
151 if ((phys_addr
& (0xf << i
)) == 0)
155 EXPORT_SYMBOL_GPL(cec_phys_addr_validate
);