hdmi.h revision 1.16 1 1.16 riastrad /* $NetBSD: hdmi.h,v 1.16 2022/07/09 17:13:04 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.1 riastrad #ifndef _LINUX_HDMI_H_
33 1.1 riastrad #define _LINUX_HDMI_H_
34 1.1 riastrad
35 1.1 riastrad #include <sys/types.h>
36 1.1 riastrad #include <sys/param.h>
37 1.1 riastrad #include <sys/errno.h>
38 1.1 riastrad #include <sys/systm.h>
39 1.1 riastrad
40 1.16 riastrad #include <lib/libkern/libkern.h>
41 1.16 riastrad
42 1.1 riastrad enum hdmi_3d_structure {
43 1.14 riastrad HDMI_3D_STRUCTURE_INVALID = -1,
44 1.14 riastrad HDMI_3D_STRUCTURE_FRAME_PACKING = 0,
45 1.14 riastrad HDMI_3D_STRUCTURE_FIELD_ALTERNATIVE = 1,
46 1.14 riastrad HDMI_3D_STRUCTURE_LINE_ALTERNATIVE = 2,
47 1.14 riastrad HDMI_3D_STRUCTURE_SIDE_BY_SIDE_FULL = 3,
48 1.14 riastrad HDMI_3D_STRUCTURE_L_DEPTH = 4,
49 1.14 riastrad HDMI_3D_STRUCTURE_L_DEPTH_GFX_GFX_DEPTH = 5,
50 1.14 riastrad HDMI_3D_STRUCTURE_TOP_AND_BOTTOM = 6,
51 1.14 riastrad HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF = 8,
52 1.1 riastrad };
53 1.1 riastrad
54 1.1 riastrad enum hdmi_active_aspect {
55 1.14 riastrad HDMI_ACTIVE_ASPECT_NONE = 0,
56 1.14 riastrad HDMI_ACTIVE_ASPECT_16_9_TOP = 2,
57 1.14 riastrad HDMI_ACTIVE_ASPECT_14_9_TOP = 3,
58 1.14 riastrad HDMI_ACTIVE_ASPECT_16_9_CENTER = 4,
59 1.14 riastrad HDMI_ACTIVE_ASPECT_PICTURE = 8,
60 1.14 riastrad HDMI_ACTIVE_ASPECT_4_3 = 9,
61 1.14 riastrad HDMI_ACTIVE_ASPECT_16_9 = 10,
62 1.14 riastrad HDMI_ACTIVE_ASPECT_14_9 = 11,
63 1.14 riastrad HDMI_ACTIVE_ASPECT_4_3_SP_14_9 = 13,
64 1.14 riastrad HDMI_ACTIVE_ASPECT_16_9_SP_14_9 = 14,
65 1.14 riastrad HDMI_ACTIVE_ASPECT_16_9_SP_4_3 = 15,
66 1.1 riastrad };
67 1.1 riastrad
68 1.2 riastrad enum hdmi_audio_coding_type {
69 1.2 riastrad HDMI_AUDIO_CODING_TYPE_STREAM = 0,
70 1.2 riastrad HDMI_AUDIO_CODING_TYPE_PCM = 1,
71 1.2 riastrad HDMI_AUDIO_CODING_TYPE_AC3 = 2,
72 1.2 riastrad HDMI_AUDIO_CODING_TYPE_MPEG1 = 3,
73 1.2 riastrad HDMI_AUDIO_CODING_TYPE_MP3 = 4,
74 1.2 riastrad HDMI_AUDIO_CODING_TYPE_MPEG2 = 5,
75 1.2 riastrad HDMI_AUDIO_CODING_TYPE_AAC_LC = 6,
76 1.2 riastrad HDMI_AUDIO_CODING_TYPE_DTS = 7,
77 1.2 riastrad HDMI_AUDIO_CODING_TYPE_ATRAC = 8,
78 1.2 riastrad HDMI_AUDIO_CODING_TYPE_DSD = 9,
79 1.2 riastrad HDMI_AUDIO_CODING_TYPE_EAC3 = 10,
80 1.2 riastrad HDMI_AUDIO_CODING_TYPE_DTS_HD = 11,
81 1.2 riastrad HDMI_AUDIO_CODING_TYPE_MLP = 12,
82 1.2 riastrad HDMI_AUDIO_CODING_TYPE_DST = 13,
83 1.2 riastrad HDMI_AUDIO_CODING_TYPE_WMA_PRO = 14,
84 1.2 riastrad };
85 1.2 riastrad
86 1.2 riastrad enum hdmi_audio_coding_type_ext {
87 1.2 riastrad HDMI_AUDIO_CODING_TYPE_EXT_STREAM = 0,
88 1.2 riastrad HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC = 1,
89 1.2 riastrad HDMI_AUDIO_CODING_TYPE_EXT_HE_AAC_V2 = 2,
90 1.2 riastrad HDMI_AUDIO_CODING_TYPE_EXT_MPEG_SURROUND = 3,
91 1.2 riastrad };
92 1.2 riastrad
93 1.2 riastrad enum hdmi_audio_sample_frequency {
94 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM = 0,
95 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_32000 = 1,
96 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_44100 = 2,
97 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_48000 = 3,
98 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_88200 = 4,
99 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_96000 = 5,
100 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_176400 = 6,
101 1.2 riastrad HDMI_AUDIO_SAMPLE_FREQUENCY_192000 = 7,
102 1.2 riastrad };
103 1.2 riastrad
104 1.2 riastrad enum hdmi_audio_sample_size {
105 1.2 riastrad HDMI_AUDIO_SAMPLE_SIZE_STREAM = 0,
106 1.2 riastrad HDMI_AUDIO_SAMPLE_SIZE_16 = 1,
107 1.2 riastrad HDMI_AUDIO_SAMPLE_SIZE_20 = 2,
108 1.2 riastrad HDMI_AUDIO_SAMPLE_SIZE_24 = 3,
109 1.2 riastrad };
110 1.2 riastrad
111 1.1 riastrad enum hdmi_colorimetry {
112 1.1 riastrad HDMI_COLORIMETRY_NONE = 0,
113 1.1 riastrad HDMI_COLORIMETRY_ITU_601 = 1,
114 1.1 riastrad HDMI_COLORIMETRY_ITU_709 = 2,
115 1.1 riastrad HDMI_COLORIMETRY_EXTENDED = 3,
116 1.1 riastrad };
117 1.1 riastrad
118 1.1 riastrad enum hdmi_colorspace {
119 1.1 riastrad HDMI_COLORSPACE_RGB = 0,
120 1.1 riastrad HDMI_COLORSPACE_YUV422 = 1,
121 1.1 riastrad HDMI_COLORSPACE_YUV444 = 2,
122 1.14 riastrad HDMI_COLORSPACE_YUV420 = 3,
123 1.14 riastrad HDMI_COLORSPACE_IDO_DEFINED = 7,
124 1.1 riastrad };
125 1.1 riastrad
126 1.1 riastrad enum hdmi_content_type {
127 1.7 riastrad HDMI_CONTENT_TYPE_GRAPHICS = 0,
128 1.1 riastrad HDMI_CONTENT_TYPE_PHOTO = 1,
129 1.1 riastrad HDMI_CONTENT_TYPE_CINEMA = 2,
130 1.1 riastrad HDMI_CONTENT_TYPE_GAME = 3,
131 1.1 riastrad };
132 1.1 riastrad
133 1.1 riastrad enum hdmi_extended_colorimetry {
134 1.1 riastrad HDMI_EXTENDED_COLORIMETRY_XV_YCC_601 = 0,
135 1.1 riastrad HDMI_EXTENDED_COLORIMETRY_XV_YCC_709 = 1,
136 1.1 riastrad HDMI_EXTENDED_COLORIMETRY_S_YCC_601 = 2,
137 1.14 riastrad HDMI_EXTENDED_COLORIMETRY_OPYCC_601 = 3,
138 1.14 riastrad HDMI_EXTENDED_COLORIMETRY_OPRGB = 4,
139 1.1 riastrad };
140 1.1 riastrad
141 1.1 riastrad enum hdmi_nups {
142 1.1 riastrad HDMI_NUPS_UNKNOWN = 0,
143 1.1 riastrad HDMI_NUPS_HORIZONTAL = 1,
144 1.1 riastrad HDMI_NUPS_VERTICAL = 2,
145 1.1 riastrad HDMI_NUPS_BOTH = 3,
146 1.1 riastrad };
147 1.1 riastrad
148 1.1 riastrad enum hdmi_picture_aspect {
149 1.1 riastrad HDMI_PICTURE_ASPECT_NONE = 0,
150 1.1 riastrad HDMI_PICTURE_ASPECT_4_3 = 1,
151 1.1 riastrad HDMI_PICTURE_ASPECT_16_9 = 2,
152 1.7 riastrad HDMI_PICTURE_ASPECT_64_27 = 3,
153 1.7 riastrad HDMI_PICTURE_ASPECT_256_135 = 4,
154 1.7 riastrad HDMI_PICTURE_ASPECT_RESERVED = 5,
155 1.1 riastrad };
156 1.1 riastrad
157 1.1 riastrad enum hdmi_quantization_range {
158 1.1 riastrad HDMI_QUANTIZATION_RANGE_DEFAULT = 0,
159 1.1 riastrad HDMI_QUANTIZATION_RANGE_LIMITED = 1,
160 1.1 riastrad HDMI_QUANTIZATION_RANGE_FULL = 2,
161 1.1 riastrad };
162 1.1 riastrad
163 1.1 riastrad enum hdmi_scan_mode {
164 1.1 riastrad HDMI_SCAN_MODE_NONE = 0,
165 1.1 riastrad HDMI_SCAN_MODE_OVERSCAN = 1,
166 1.1 riastrad HDMI_SCAN_MODE_UNDERSCAN = 2,
167 1.1 riastrad };
168 1.1 riastrad
169 1.1 riastrad enum hdmi_ycc_quantization_range {
170 1.1 riastrad HDMI_YCC_QUANTIZATION_RANGE_LIMITED = 0,
171 1.1 riastrad HDMI_YCC_QUANTIZATION_RANGE_FULL = 1,
172 1.1 riastrad };
173 1.1 riastrad
174 1.10 riastrad enum hdmi_packet_type {
175 1.10 riastrad HDMI_PACKET_TYPE_NULL = 0x00,
176 1.10 riastrad HDMI_PACKET_TYPE_AUDIO_CLOCK_REGEN = 0x01,
177 1.10 riastrad HDMI_PACKET_TYPE_AUDIO_SAMPLE = 0x02,
178 1.10 riastrad HDMI_PACKET_TYPE_GENERAL_CONTROL = 0x03,
179 1.10 riastrad HDMI_PACKET_TYPE_ACP = 0x04,
180 1.10 riastrad HDMI_PACKET_TYPE_ISRC1 = 0x05,
181 1.10 riastrad HDMI_PACKET_TYPE_ISRC2 = 0x06,
182 1.10 riastrad HDMI_PACKET_TYPE_ONE_BIT_AUDIO_SAMPLE = 0x07,
183 1.10 riastrad HDMI_PACKET_TYPE_DST_AUDIO = 0x08,
184 1.10 riastrad HDMI_PACKET_TYPE_HBR_AUDIO_STREAM = 0x09,
185 1.10 riastrad HDMI_PACKET_TYPE_GAMUT_METADATA = 0x0a,
186 1.10 riastrad };
187 1.10 riastrad
188 1.1 riastrad enum hdmi_infoframe_type {
189 1.1 riastrad HDMI_INFOFRAME_TYPE_VENDOR = 0x81,
190 1.1 riastrad HDMI_INFOFRAME_TYPE_AVI = 0x82,
191 1.1 riastrad HDMI_INFOFRAME_TYPE_SPD = 0x83,
192 1.1 riastrad HDMI_INFOFRAME_TYPE_AUDIO = 0x84,
193 1.10 riastrad HDMI_INFOFRAME_TYPE_DRM = 0x87,
194 1.1 riastrad };
195 1.1 riastrad
196 1.9 riastrad enum hdmi_eotf {
197 1.14 riastrad HDMI_EOTF_TRADITIONAL_GAMMA_SDR = 0,
198 1.14 riastrad HDMI_EOTF_TRADITIONAL_GAMMA_HDR = 1,
199 1.14 riastrad HDMI_EOTF_SMPTE_ST2084 = 2,
200 1.14 riastrad HDMI_EOTF_BT_2100_HLG = 3,
201 1.9 riastrad };
202 1.9 riastrad
203 1.9 riastrad enum hdmi_metadata_type {
204 1.14 riastrad HDMI_STATIC_METADATA_TYPE1 = 1,
205 1.9 riastrad };
206 1.9 riastrad
207 1.11 riastrad struct hdmi_type1 {
208 1.11 riastrad enum hdmi_eotf eotf;
209 1.11 riastrad enum hdmi_metadata_type metadata_type;
210 1.11 riastrad uint16_t min_cll;
211 1.11 riastrad uint16_t max_cll;
212 1.11 riastrad uint16_t max_fall;
213 1.11 riastrad };
214 1.11 riastrad
215 1.11 riastrad struct hdr_sink_metadata {
216 1.11 riastrad struct hdmi_type1 hdmi_type1;
217 1.11 riastrad };
218 1.11 riastrad
219 1.1 riastrad #define HDMI_INFOFRAME_SIZE(TYPE) \
220 1.1 riastrad (HDMI_INFOFRAME_HEADER_SIZE + HDMI_##TYPE##_INFOFRAME_SIZE)
221 1.1 riastrad
222 1.1 riastrad #define HDMI_INFOFRAME_HEADER_SIZE 4
223 1.1 riastrad struct hdmi_infoframe_header {
224 1.1 riastrad enum hdmi_infoframe_type type;
225 1.1 riastrad uint8_t version;
226 1.1 riastrad uint8_t length;
227 1.1 riastrad /* checksum */
228 1.1 riastrad };
229 1.1 riastrad
230 1.1 riastrad static inline void
231 1.1 riastrad hdmi_infoframe_header_init(struct hdmi_infoframe_header *header,
232 1.1 riastrad enum hdmi_infoframe_type type, uint8_t vers, uint8_t length)
233 1.1 riastrad {
234 1.1 riastrad
235 1.1 riastrad header->type = type;
236 1.1 riastrad header->version = vers;
237 1.1 riastrad header->length = length;
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad static inline int
241 1.14 riastrad hdmi_infoframe_header_check(const struct hdmi_infoframe_header *header,
242 1.14 riastrad enum hdmi_infoframe_type type, uint8_t vers, uint8_t length)
243 1.14 riastrad {
244 1.14 riastrad
245 1.14 riastrad if (header->type != type ||
246 1.14 riastrad header->version != vers ||
247 1.14 riastrad header->length != length)
248 1.14 riastrad return -EINVAL;
249 1.14 riastrad return 0;
250 1.14 riastrad }
251 1.14 riastrad
252 1.14 riastrad static inline int
253 1.1 riastrad hdmi_infoframe_header_pack(const struct hdmi_infoframe_header *header,
254 1.1 riastrad uint8_t length, void *buf, size_t size)
255 1.1 riastrad {
256 1.1 riastrad uint8_t *const p = buf;
257 1.1 riastrad
258 1.5 riastrad if (length < HDMI_INFOFRAME_HEADER_SIZE)
259 1.5 riastrad return -ENOSPC;
260 1.5 riastrad if (size < length)
261 1.1 riastrad return -ENOSPC;
262 1.1 riastrad
263 1.1 riastrad p[0] = header->type;
264 1.1 riastrad p[1] = header->version;
265 1.4 riastrad p[2] = (length - HDMI_INFOFRAME_HEADER_SIZE);
266 1.1 riastrad p[3] = 0; /* checksum */
267 1.1 riastrad
268 1.1 riastrad return HDMI_INFOFRAME_HEADER_SIZE;
269 1.1 riastrad }
270 1.1 riastrad
271 1.14 riastrad static inline uint8_t
272 1.14 riastrad hdmi_infoframe_checksum(const void *buf, size_t length)
273 1.1 riastrad {
274 1.14 riastrad const uint8_t *p = buf;
275 1.1 riastrad uint8_t checksum = 0;
276 1.1 riastrad
277 1.1 riastrad while (length--)
278 1.6 jmcneill checksum += *p++;
279 1.1 riastrad
280 1.14 riastrad return 256 - checksum;
281 1.14 riastrad }
282 1.14 riastrad
283 1.14 riastrad static inline int
284 1.14 riastrad hdmi_infoframe_header_unpack(struct hdmi_infoframe_header *header,
285 1.14 riastrad const void *buf, size_t size)
286 1.14 riastrad {
287 1.14 riastrad const uint8_t *const p = buf;
288 1.14 riastrad
289 1.14 riastrad if (size < HDMI_INFOFRAME_HEADER_SIZE)
290 1.14 riastrad return -EINVAL;
291 1.14 riastrad if (p[2] > size - HDMI_INFOFRAME_HEADER_SIZE)
292 1.14 riastrad return -EINVAL;
293 1.14 riastrad if (hdmi_infoframe_checksum(buf, p[2] + HDMI_INFOFRAME_HEADER_SIZE))
294 1.14 riastrad return -EINVAL;
295 1.14 riastrad
296 1.14 riastrad hdmi_infoframe_header_init(header, p[0], p[1], p[2]);
297 1.14 riastrad return HDMI_INFOFRAME_HEADER_SIZE;
298 1.14 riastrad }
299 1.14 riastrad
300 1.14 riastrad static inline void
301 1.14 riastrad hdmi_infoframe_set_checksum(void *buf, size_t length)
302 1.14 riastrad {
303 1.14 riastrad uint8_t *p = buf;
304 1.14 riastrad
305 1.14 riastrad p[3] = hdmi_infoframe_checksum(buf, length);
306 1.1 riastrad }
307 1.1 riastrad
308 1.2 riastrad #define HDMI_AUDIO_INFOFRAME_SIZE 10
309 1.2 riastrad struct hdmi_audio_infoframe {
310 1.2 riastrad struct hdmi_infoframe_header header;
311 1.2 riastrad uint8_t channels;
312 1.2 riastrad enum hdmi_audio_coding_type coding_type;
313 1.2 riastrad enum hdmi_audio_sample_size sample_size;
314 1.2 riastrad enum hdmi_audio_sample_frequency sample_frequency;
315 1.2 riastrad enum hdmi_audio_coding_type_ext coding_type_ext;
316 1.2 riastrad uint8_t channel_allocation;
317 1.2 riastrad uint8_t level_shift_value;
318 1.2 riastrad bool downmix_inhibit;
319 1.2 riastrad };
320 1.2 riastrad
321 1.2 riastrad static inline int
322 1.2 riastrad hdmi_audio_infoframe_init(struct hdmi_audio_infoframe *frame)
323 1.2 riastrad {
324 1.2 riastrad static const struct hdmi_audio_infoframe zero_frame;
325 1.2 riastrad
326 1.2 riastrad *frame = zero_frame;
327 1.2 riastrad
328 1.2 riastrad hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_AUDIO,
329 1.2 riastrad 1, HDMI_AUDIO_INFOFRAME_SIZE);
330 1.2 riastrad
331 1.2 riastrad return 0;
332 1.2 riastrad }
333 1.2 riastrad
334 1.2 riastrad static inline ssize_t
335 1.2 riastrad hdmi_audio_infoframe_pack(const struct hdmi_audio_infoframe *frame, void *buf,
336 1.2 riastrad size_t size)
337 1.2 riastrad {
338 1.2 riastrad const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
339 1.2 riastrad HDMI_AUDIO_INFOFRAME_SIZE;
340 1.2 riastrad uint8_t channels = 0;
341 1.2 riastrad uint8_t *p = buf;
342 1.2 riastrad int ret;
343 1.2 riastrad
344 1.2 riastrad KASSERT(frame->header.length == HDMI_AUDIO_INFOFRAME_SIZE);
345 1.2 riastrad
346 1.4 riastrad ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
347 1.2 riastrad if (ret < 0)
348 1.2 riastrad return ret;
349 1.3 riastrad KASSERT(ret == HDMI_INFOFRAME_HEADER_SIZE);
350 1.2 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
351 1.2 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
352 1.2 riastrad
353 1.2 riastrad if (frame->channels >= 2)
354 1.2 riastrad channels = frame->channels - 1;
355 1.2 riastrad
356 1.2 riastrad p[0] = __SHIFTIN(frame->coding_type, __BITS(7,4));
357 1.2 riastrad p[0] |= __SHIFTIN(channels, __BITS(2,0));
358 1.2 riastrad
359 1.2 riastrad p[1] = __SHIFTIN(frame->sample_frequency, __BITS(4,2));
360 1.2 riastrad p[1] |= __SHIFTIN(frame->sample_size, __BITS(1,0));
361 1.2 riastrad
362 1.2 riastrad p[2] = __SHIFTIN(frame->coding_type_ext, __BITS(5,0));
363 1.2 riastrad
364 1.14 riastrad p[3] = __SHIFTIN(frame->level_shift_value, __BITS(6,3));
365 1.2 riastrad
366 1.2 riastrad p[4] = __SHIFTIN(frame->downmix_inhibit? 1 : 0, __BIT(7));
367 1.2 riastrad
368 1.6 jmcneill /* PB6 to PB10 are reserved */
369 1.6 jmcneill p[5] = 0;
370 1.6 jmcneill p[6] = 0;
371 1.6 jmcneill p[7] = 0;
372 1.6 jmcneill p[8] = 0;
373 1.6 jmcneill p[9] = 0;
374 1.6 jmcneill
375 1.2 riastrad CTASSERT(HDMI_AUDIO_INFOFRAME_SIZE == 10);
376 1.2 riastrad
377 1.14 riastrad hdmi_infoframe_set_checksum(buf, length);
378 1.2 riastrad
379 1.2 riastrad return length;
380 1.2 riastrad }
381 1.2 riastrad
382 1.14 riastrad static inline int
383 1.14 riastrad hdmi_audio_infoframe_unpack(struct hdmi_audio_infoframe *frame,
384 1.14 riastrad const void *buf, size_t size)
385 1.14 riastrad {
386 1.14 riastrad const uint8_t *p = buf;
387 1.14 riastrad int ret;
388 1.14 riastrad
389 1.14 riastrad memset(frame, 0, sizeof(*frame));
390 1.14 riastrad
391 1.14 riastrad ret = hdmi_infoframe_header_unpack(&frame->header, p, size);
392 1.14 riastrad if (ret)
393 1.14 riastrad return ret;
394 1.14 riastrad if (frame->header.length != HDMI_AUDIO_INFOFRAME_SIZE)
395 1.14 riastrad return -EINVAL;
396 1.14 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
397 1.14 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
398 1.14 riastrad
399 1.14 riastrad frame->coding_type = __SHIFTOUT(p[0], __BITS(7,4));
400 1.14 riastrad frame->channels = __SHIFTOUT(p[0], __BITS(2,0));
401 1.14 riastrad
402 1.14 riastrad frame->sample_frequency = __SHIFTOUT(p[1], __BITS(4,2));
403 1.14 riastrad frame->sample_size = __SHIFTOUT(p[1], __BITS(1,0));
404 1.14 riastrad
405 1.14 riastrad frame->coding_type_ext = __SHIFTOUT(p[2], __BITS(5,0));
406 1.14 riastrad
407 1.14 riastrad frame->level_shift_value = __SHIFTOUT(p[3], __BITS(6,3));
408 1.14 riastrad
409 1.14 riastrad frame->downmix_inhibit = __SHIFTOUT(p[4], __BIT(7));
410 1.14 riastrad
411 1.14 riastrad return 0;
412 1.14 riastrad }
413 1.14 riastrad
414 1.1 riastrad #define HDMI_AVI_INFOFRAME_SIZE 13
415 1.1 riastrad struct hdmi_avi_infoframe {
416 1.1 riastrad struct hdmi_infoframe_header header;
417 1.1 riastrad enum hdmi_colorspace colorspace;
418 1.1 riastrad enum hdmi_scan_mode scan_mode;
419 1.1 riastrad enum hdmi_colorimetry colorimetry;
420 1.1 riastrad enum hdmi_picture_aspect picture_aspect;
421 1.1 riastrad enum hdmi_active_aspect active_aspect;
422 1.1 riastrad bool itc;
423 1.1 riastrad enum hdmi_extended_colorimetry extended_colorimetry;
424 1.1 riastrad enum hdmi_quantization_range quantization_range;
425 1.1 riastrad enum hdmi_nups nups;
426 1.1 riastrad uint8_t video_code;
427 1.1 riastrad enum hdmi_ycc_quantization_range ycc_quantization_range;
428 1.1 riastrad enum hdmi_content_type content_type;
429 1.1 riastrad uint8_t pixel_repeat;
430 1.1 riastrad uint16_t top_bar;
431 1.1 riastrad uint16_t bottom_bar;
432 1.1 riastrad uint16_t left_bar;
433 1.1 riastrad uint16_t right_bar;
434 1.1 riastrad };
435 1.1 riastrad
436 1.1 riastrad static inline int
437 1.1 riastrad hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
438 1.1 riastrad {
439 1.1 riastrad static const struct hdmi_avi_infoframe zero_frame;
440 1.1 riastrad
441 1.1 riastrad *frame = zero_frame;
442 1.1 riastrad
443 1.1 riastrad hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_AVI, 2,
444 1.1 riastrad HDMI_AVI_INFOFRAME_SIZE);
445 1.1 riastrad
446 1.1 riastrad return 0;
447 1.1 riastrad }
448 1.1 riastrad
449 1.14 riastrad static inline int
450 1.14 riastrad hdmi_avi_infoframe_check(const struct hdmi_avi_infoframe *frame)
451 1.14 riastrad {
452 1.14 riastrad int ret;
453 1.14 riastrad
454 1.14 riastrad ret = hdmi_infoframe_header_check(&frame->header,
455 1.14 riastrad HDMI_INFOFRAME_TYPE_AVI, 2, HDMI_AVI_INFOFRAME_SIZE);
456 1.14 riastrad if (ret)
457 1.14 riastrad return ret;
458 1.14 riastrad
459 1.14 riastrad return 0;
460 1.14 riastrad }
461 1.14 riastrad
462 1.1 riastrad static inline ssize_t
463 1.1 riastrad hdmi_avi_infoframe_pack(const struct hdmi_avi_infoframe *frame, void *buf,
464 1.1 riastrad size_t size)
465 1.1 riastrad {
466 1.1 riastrad const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
467 1.1 riastrad HDMI_AVI_INFOFRAME_SIZE;
468 1.1 riastrad uint8_t *p = buf;
469 1.1 riastrad int ret;
470 1.1 riastrad
471 1.1 riastrad KASSERT(frame->header.length == HDMI_AVI_INFOFRAME_SIZE);
472 1.1 riastrad
473 1.4 riastrad ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
474 1.1 riastrad if (ret < 0)
475 1.1 riastrad return ret;
476 1.3 riastrad KASSERT(ret == HDMI_INFOFRAME_HEADER_SIZE);
477 1.1 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
478 1.1 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
479 1.1 riastrad
480 1.1 riastrad p[0] = __SHIFTIN(frame->colorspace, __BITS(6,5));
481 1.1 riastrad p[0] |= __SHIFTIN(frame->active_aspect & 0xf? 1 : 0, __BIT(4));
482 1.1 riastrad p[0] |= __SHIFTIN(frame->top_bar || frame->bottom_bar, __BIT(3));
483 1.1 riastrad p[0] |= __SHIFTIN(frame->left_bar || frame->right_bar, __BIT(2));
484 1.1 riastrad p[0] |= __SHIFTIN(frame->scan_mode, __BITS(1,0));
485 1.1 riastrad
486 1.1 riastrad p[1] = __SHIFTIN(frame->colorimetry, __BITS(7,6));
487 1.1 riastrad p[1] |= __SHIFTIN(frame->picture_aspect, __BITS(5,4));
488 1.1 riastrad p[1] |= __SHIFTIN(frame->active_aspect, __BITS(3,0));
489 1.1 riastrad
490 1.1 riastrad p[2] = __SHIFTIN(frame->itc? 1 : 0, __BIT(7));
491 1.1 riastrad p[2] |= __SHIFTIN(frame->extended_colorimetry, __BITS(6,4));
492 1.1 riastrad p[2] |= __SHIFTIN(frame->quantization_range, __BITS(3,2));
493 1.1 riastrad p[2] |= __SHIFTIN(frame->nups, __BITS(1,0));
494 1.1 riastrad
495 1.1 riastrad p[3] = frame->video_code;
496 1.1 riastrad
497 1.1 riastrad p[4] = __SHIFTIN(frame->ycc_quantization_range, __BITS(7,6));
498 1.1 riastrad p[4] |= __SHIFTIN(frame->content_type, __BITS(5,4));
499 1.1 riastrad p[4] |= __SHIFTIN(frame->pixel_repeat, __BITS(3,0));
500 1.1 riastrad
501 1.1 riastrad le16enc(&p[5], frame->top_bar);
502 1.1 riastrad le16enc(&p[7], frame->bottom_bar);
503 1.1 riastrad le16enc(&p[9], frame->left_bar);
504 1.1 riastrad le16enc(&p[11], frame->right_bar);
505 1.1 riastrad CTASSERT(HDMI_AVI_INFOFRAME_SIZE == 13);
506 1.1 riastrad
507 1.14 riastrad hdmi_infoframe_set_checksum(buf, length);
508 1.14 riastrad
509 1.14 riastrad return length;
510 1.14 riastrad }
511 1.14 riastrad
512 1.14 riastrad static inline int
513 1.14 riastrad hdmi_avi_infoframe_unpack(struct hdmi_avi_infoframe *frame, const void *buf,
514 1.14 riastrad size_t size)
515 1.14 riastrad {
516 1.14 riastrad const uint8_t *p = buf;
517 1.14 riastrad int ret;
518 1.14 riastrad
519 1.14 riastrad memset(frame, 0, sizeof(*frame));
520 1.14 riastrad
521 1.14 riastrad ret = hdmi_infoframe_header_unpack(&frame->header, p, size);
522 1.14 riastrad if (ret)
523 1.14 riastrad return ret;
524 1.14 riastrad if (frame->header.length != HDMI_AVI_INFOFRAME_SIZE)
525 1.14 riastrad return -EINVAL;
526 1.14 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
527 1.14 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
528 1.14 riastrad
529 1.14 riastrad frame->colorspace = __SHIFTOUT(p[0], __BITS(6,5));
530 1.14 riastrad frame->scan_mode = __SHIFTOUT(p[0], __BITS(1,0));
531 1.14 riastrad
532 1.14 riastrad frame->colorimetry = __SHIFTOUT(p[1], __BITS(7,6));
533 1.14 riastrad frame->picture_aspect = __SHIFTOUT(p[1], __BITS(5,4));
534 1.14 riastrad if (p[0] & __BIT(4))
535 1.14 riastrad frame->active_aspect = __SHIFTOUT(p[1], __BITS(3,0));
536 1.14 riastrad
537 1.14 riastrad frame->itc = __SHIFTOUT(p[2], __BIT(7));
538 1.14 riastrad frame->extended_colorimetry = __SHIFTOUT(p[2], __BITS(6,4));
539 1.14 riastrad frame->quantization_range = __SHIFTOUT(p[2], __BITS(3,2));
540 1.14 riastrad frame->nups = __SHIFTOUT(p[2], __BITS(1,0));
541 1.14 riastrad
542 1.14 riastrad frame->video_code = p[3];
543 1.14 riastrad
544 1.14 riastrad frame->ycc_quantization_range = __SHIFTOUT(p[4], __BITS(7,6));
545 1.14 riastrad frame->content_type = __SHIFTOUT(p[4], __BITS(5,4));
546 1.14 riastrad frame->pixel_repeat = __SHIFTOUT(p[4], __BITS(3,0));
547 1.14 riastrad
548 1.14 riastrad if (p[0] & __BIT(3)) {
549 1.14 riastrad frame->top_bar = le16dec(&p[5]);
550 1.14 riastrad frame->bottom_bar = le16dec(&p[7]);
551 1.14 riastrad }
552 1.14 riastrad if (p[0] & __BIT(2)) {
553 1.14 riastrad frame->left_bar = le16dec(&p[9]);
554 1.14 riastrad frame->right_bar = le16dec(&p[11]);
555 1.14 riastrad }
556 1.14 riastrad
557 1.14 riastrad return 0;
558 1.14 riastrad }
559 1.14 riastrad
560 1.14 riastrad #define HDMI_DRM_INFOFRAME_SIZE 26
561 1.14 riastrad struct hdmi_drm_infoframe {
562 1.14 riastrad struct hdmi_infoframe_header header;
563 1.14 riastrad enum hdmi_eotf eotf;
564 1.14 riastrad enum hdmi_metadata_type metadata_type;
565 1.14 riastrad struct {
566 1.14 riastrad uint16_t x, y;
567 1.14 riastrad } display_primaries[3];
568 1.14 riastrad struct {
569 1.14 riastrad uint16_t x, y;
570 1.14 riastrad } white_point;
571 1.14 riastrad uint16_t max_display_mastering_luminance;
572 1.14 riastrad uint16_t min_display_mastering_luminance;
573 1.14 riastrad uint16_t max_cll;
574 1.14 riastrad uint16_t max_fall;
575 1.14 riastrad };
576 1.14 riastrad
577 1.14 riastrad static inline int
578 1.14 riastrad hdmi_drm_infoframe_init(struct hdmi_drm_infoframe *frame)
579 1.14 riastrad {
580 1.14 riastrad static const struct hdmi_drm_infoframe zero_frame;
581 1.14 riastrad
582 1.14 riastrad *frame = zero_frame;
583 1.14 riastrad
584 1.14 riastrad hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_DRM,
585 1.14 riastrad 1, HDMI_DRM_INFOFRAME_SIZE);
586 1.14 riastrad
587 1.14 riastrad return 0;
588 1.14 riastrad }
589 1.14 riastrad
590 1.14 riastrad static inline int
591 1.14 riastrad hdmi_drm_infoframe_check(const struct hdmi_drm_infoframe *frame)
592 1.14 riastrad {
593 1.14 riastrad int ret;
594 1.14 riastrad
595 1.14 riastrad ret = hdmi_infoframe_header_check(&frame->header,
596 1.14 riastrad HDMI_INFOFRAME_TYPE_DRM, 1, HDMI_DRM_INFOFRAME_SIZE);
597 1.14 riastrad if (ret)
598 1.14 riastrad return ret;
599 1.14 riastrad
600 1.14 riastrad return 0;
601 1.14 riastrad }
602 1.14 riastrad
603 1.14 riastrad #define hdmi_drm_infoframe_pack_only hdmi_drm_infoframe_pack /* XXX */
604 1.14 riastrad
605 1.14 riastrad static inline int
606 1.14 riastrad hdmi_drm_infoframe_pack(const struct hdmi_drm_infoframe *frame,
607 1.14 riastrad void *buf, size_t size)
608 1.14 riastrad {
609 1.14 riastrad const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
610 1.14 riastrad HDMI_DRM_INFOFRAME_SIZE;
611 1.14 riastrad uint8_t *p = buf;
612 1.14 riastrad unsigned i;
613 1.14 riastrad int ret;
614 1.14 riastrad
615 1.14 riastrad KASSERT(frame->header.length == HDMI_DRM_INFOFRAME_SIZE);
616 1.14 riastrad
617 1.14 riastrad ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
618 1.14 riastrad if (ret < 0)
619 1.14 riastrad return ret;
620 1.14 riastrad KASSERT(ret == HDMI_INFOFRAME_HEADER_SIZE);
621 1.14 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
622 1.14 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
623 1.14 riastrad
624 1.14 riastrad p[0] = frame->eotf;
625 1.14 riastrad p[1] = frame->metadata_type;
626 1.14 riastrad for (i = 0; i < __arraycount(frame->display_primaries); i++) {
627 1.14 riastrad le16enc(&p[2 + 4*i], frame->display_primaries[i].x);
628 1.14 riastrad le16enc(&p[2 + 4*i + 2], frame->display_primaries[i].y);
629 1.14 riastrad }
630 1.14 riastrad le16enc(&p[14], frame->white_point.x);
631 1.14 riastrad le16enc(&p[16], frame->white_point.y);
632 1.14 riastrad le16enc(&p[18], frame->min_display_mastering_luminance);
633 1.14 riastrad le16enc(&p[20], frame->max_display_mastering_luminance);
634 1.14 riastrad le16enc(&p[22], frame->max_cll);
635 1.14 riastrad le16enc(&p[24], frame->max_fall);
636 1.14 riastrad CTASSERT(HDMI_DRM_INFOFRAME_SIZE == 26);
637 1.14 riastrad
638 1.14 riastrad hdmi_infoframe_set_checksum(buf, length);
639 1.1 riastrad
640 1.1 riastrad return length;
641 1.1 riastrad }
642 1.1 riastrad
643 1.14 riastrad static inline int
644 1.14 riastrad hdmi_drm_infoframe_unpack(struct hdmi_drm_infoframe *frame, const void *buf,
645 1.14 riastrad size_t size)
646 1.14 riastrad {
647 1.14 riastrad const uint8_t *p = buf;
648 1.14 riastrad unsigned i;
649 1.14 riastrad int ret;
650 1.14 riastrad
651 1.14 riastrad memset(frame, 0, sizeof(*frame));
652 1.14 riastrad
653 1.14 riastrad ret = hdmi_infoframe_header_unpack(&frame->header, p, size);
654 1.14 riastrad if (ret)
655 1.14 riastrad return ret;
656 1.14 riastrad if (frame->header.length != HDMI_DRM_INFOFRAME_SIZE)
657 1.14 riastrad return -EINVAL;
658 1.14 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
659 1.14 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
660 1.14 riastrad
661 1.14 riastrad frame->eotf = p[0];
662 1.14 riastrad frame->metadata_type = p[1];
663 1.14 riastrad for (i = 0; i < __arraycount(frame->display_primaries); i++) {
664 1.14 riastrad frame->display_primaries[i].x = le16dec(&p[2 + 4*i]);
665 1.14 riastrad frame->display_primaries[i].y = le16dec(&p[2 + 4*i + 2]);
666 1.14 riastrad }
667 1.14 riastrad frame->white_point.x = le16dec(&p[14]);
668 1.14 riastrad frame->white_point.y = le16dec(&p[16]);
669 1.14 riastrad frame->min_display_mastering_luminance = le16dec(&p[18]);
670 1.14 riastrad frame->max_display_mastering_luminance = le16dec(&p[20]);
671 1.14 riastrad frame->max_cll = le16dec(&p[22]);
672 1.14 riastrad frame->max_fall = le16dec(&p[24]);
673 1.14 riastrad
674 1.14 riastrad return 0;
675 1.14 riastrad }
676 1.14 riastrad
677 1.1 riastrad #define HDMI_SPD_INFOFRAME_SIZE 25
678 1.1 riastrad struct hdmi_spd_infoframe {
679 1.1 riastrad struct hdmi_infoframe_header header;
680 1.1 riastrad char vendor[8];
681 1.1 riastrad char product[16];
682 1.1 riastrad enum hdmi_spd_sdi {
683 1.1 riastrad HDMI_SPD_SDI_UNKNOWN = 0,
684 1.1 riastrad HDMI_SPD_SDI_DSTB = 1,
685 1.1 riastrad HDMI_SPD_SDI_DVDP = 2,
686 1.1 riastrad HDMI_SPD_SDI_DVHS = 3,
687 1.1 riastrad HDMI_SPD_SDI_HDDVR = 4,
688 1.1 riastrad HDMI_SPD_SDI_DVC = 5,
689 1.1 riastrad HDMI_SPD_SDI_DSC = 6,
690 1.1 riastrad HDMI_SPD_SDI_VCD = 7,
691 1.1 riastrad HDMI_SPD_SDI_GAME = 8,
692 1.1 riastrad HDMI_SPD_SDI_PC = 9,
693 1.1 riastrad HDMI_SPD_SDI_BD = 10,
694 1.1 riastrad HDMI_SPD_SDI_SACD = 11,
695 1.1 riastrad HDMI_SPD_SDI_HDDVD = 12,
696 1.1 riastrad HDMI_SPD_SDI_PMP = 13,
697 1.1 riastrad } sdi;
698 1.1 riastrad };
699 1.1 riastrad
700 1.1 riastrad static inline int
701 1.1 riastrad hdmi_spd_infoframe_init(struct hdmi_spd_infoframe *frame, const char *vendor,
702 1.1 riastrad const char *product)
703 1.1 riastrad {
704 1.1 riastrad static const struct hdmi_spd_infoframe zero_frame;
705 1.1 riastrad
706 1.1 riastrad *frame = zero_frame;
707 1.1 riastrad
708 1.1 riastrad hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_SPD,
709 1.1 riastrad 1, HDMI_SPD_INFOFRAME_SIZE);
710 1.1 riastrad
711 1.12 riastrad strncpy(frame->vendor, vendor, sizeof(frame->vendor));
712 1.12 riastrad strncpy(frame->product, product, sizeof(frame->product));
713 1.1 riastrad
714 1.1 riastrad return 0;
715 1.1 riastrad }
716 1.1 riastrad
717 1.14 riastrad static inline int
718 1.14 riastrad hdmi_spd_infoframe_check(const struct hdmi_spd_infoframe *frame)
719 1.14 riastrad {
720 1.14 riastrad int ret;
721 1.14 riastrad
722 1.14 riastrad ret = hdmi_infoframe_header_check(&frame->header,
723 1.15 riastrad HDMI_INFOFRAME_TYPE_SPD, 1, HDMI_SPD_INFOFRAME_SIZE);
724 1.14 riastrad if (ret)
725 1.14 riastrad return ret;
726 1.14 riastrad
727 1.14 riastrad return 0;
728 1.14 riastrad }
729 1.14 riastrad
730 1.1 riastrad static inline ssize_t
731 1.14 riastrad hdmi_spd_infoframe_pack(const struct hdmi_spd_infoframe *frame, void *buf,
732 1.1 riastrad size_t size)
733 1.1 riastrad {
734 1.1 riastrad const size_t length = HDMI_INFOFRAME_HEADER_SIZE +
735 1.1 riastrad HDMI_SPD_INFOFRAME_SIZE;
736 1.1 riastrad uint8_t *p = buf;
737 1.1 riastrad int ret;
738 1.1 riastrad
739 1.1 riastrad KASSERT(frame->header.length == HDMI_SPD_INFOFRAME_SIZE);
740 1.1 riastrad
741 1.4 riastrad ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
742 1.1 riastrad if (ret < 0)
743 1.1 riastrad return ret;
744 1.3 riastrad KASSERT(ret == HDMI_INFOFRAME_HEADER_SIZE);
745 1.1 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
746 1.1 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
747 1.1 riastrad
748 1.14 riastrad memcpy(&p[0], frame->vendor, 8);
749 1.14 riastrad memcpy(&p[8], frame->product, 16);
750 1.1 riastrad p[24] = frame->sdi;
751 1.11 riastrad CTASSERT(HDMI_SPD_INFOFRAME_SIZE == 25);
752 1.1 riastrad
753 1.14 riastrad hdmi_infoframe_set_checksum(buf, length);
754 1.1 riastrad
755 1.1 riastrad return length;
756 1.1 riastrad }
757 1.1 riastrad
758 1.14 riastrad static inline int
759 1.14 riastrad hdmi_spd_infoframe_unpack(struct hdmi_spd_infoframe *frame, const void *buf,
760 1.14 riastrad size_t size)
761 1.14 riastrad {
762 1.14 riastrad const uint8_t *p = buf;
763 1.14 riastrad int ret;
764 1.14 riastrad
765 1.14 riastrad memset(frame, 0, sizeof(*frame));
766 1.14 riastrad
767 1.14 riastrad ret = hdmi_infoframe_header_unpack(&frame->header, p, size);
768 1.14 riastrad if (ret)
769 1.14 riastrad return ret;
770 1.14 riastrad if (frame->header.length != HDMI_SPD_INFOFRAME_SIZE)
771 1.14 riastrad return -EINVAL;
772 1.14 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
773 1.14 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
774 1.14 riastrad
775 1.14 riastrad memcpy(frame->vendor, &p[0], 8);
776 1.14 riastrad memcpy(frame->product, &p[8], 8);
777 1.14 riastrad frame->sdi = p[24];
778 1.14 riastrad
779 1.14 riastrad return 0;
780 1.14 riastrad }
781 1.14 riastrad
782 1.7 riastrad #define HDMI_IEEE_OUI 0x000c03
783 1.7 riastrad #define HDMI_FORUM_IEEE_OUI 0xc45dd8
784 1.1 riastrad
785 1.1 riastrad struct hdmi_vendor_infoframe {
786 1.1 riastrad struct hdmi_infoframe_header header;
787 1.1 riastrad uint32_t oui;
788 1.1 riastrad uint8_t vic;
789 1.1 riastrad enum hdmi_3d_structure s3d_struct;
790 1.1 riastrad unsigned s3d_ext_data;
791 1.1 riastrad };
792 1.1 riastrad
793 1.1 riastrad union hdmi_vendor_any_infoframe {
794 1.1 riastrad struct {
795 1.1 riastrad struct hdmi_infoframe_header header;
796 1.1 riastrad uint32_t oui;
797 1.1 riastrad } any;
798 1.1 riastrad struct hdmi_vendor_infoframe hdmi;
799 1.1 riastrad };
800 1.1 riastrad
801 1.1 riastrad static inline int
802 1.1 riastrad hdmi_vendor_infoframe_init(struct hdmi_vendor_infoframe *frame)
803 1.1 riastrad {
804 1.1 riastrad static const struct hdmi_vendor_infoframe zero_frame;
805 1.1 riastrad
806 1.1 riastrad *frame = zero_frame;
807 1.1 riastrad
808 1.1 riastrad hdmi_infoframe_header_init(&frame->header, HDMI_INFOFRAME_TYPE_VENDOR,
809 1.1 riastrad 1, 0 /* depends on s3d_struct */);
810 1.1 riastrad
811 1.1 riastrad frame->oui = HDMI_IEEE_OUI;
812 1.1 riastrad frame->s3d_struct = HDMI_3D_STRUCTURE_INVALID;
813 1.1 riastrad
814 1.1 riastrad return 0;
815 1.1 riastrad }
816 1.1 riastrad
817 1.14 riastrad static inline size_t
818 1.14 riastrad hdmi_vendor_infoframe_length(const struct hdmi_vendor_infoframe *frame)
819 1.14 riastrad {
820 1.14 riastrad
821 1.14 riastrad if (frame->vic) {
822 1.14 riastrad return 5;
823 1.14 riastrad } else if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
824 1.14 riastrad if (frame->s3d_struct < HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
825 1.14 riastrad return 5;
826 1.14 riastrad else
827 1.14 riastrad return 6;
828 1.14 riastrad } else {
829 1.14 riastrad return 4;
830 1.14 riastrad }
831 1.14 riastrad }
832 1.14 riastrad
833 1.14 riastrad static inline int
834 1.14 riastrad hdmi_vendor_infoframe_check(const struct hdmi_vendor_infoframe *frame)
835 1.14 riastrad {
836 1.14 riastrad
837 1.14 riastrad if (frame->header.type != HDMI_INFOFRAME_TYPE_VENDOR ||
838 1.14 riastrad frame->header.version != 1)
839 1.14 riastrad return -EINVAL;
840 1.14 riastrad /* frame->header.length not used when packing */
841 1.14 riastrad
842 1.14 riastrad /* At most one may be supplied. */
843 1.14 riastrad if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
844 1.14 riastrad return -EINVAL;
845 1.14 riastrad
846 1.14 riastrad return 0;
847 1.14 riastrad }
848 1.14 riastrad
849 1.1 riastrad static inline int
850 1.1 riastrad hdmi_vendor_infoframe_pack(const struct hdmi_vendor_infoframe *frame,
851 1.1 riastrad void *buf, size_t size)
852 1.1 riastrad {
853 1.1 riastrad uint8_t *p = buf;
854 1.1 riastrad size_t length;
855 1.1 riastrad int ret;
856 1.1 riastrad
857 1.14 riastrad /* At most one may be supplied. */
858 1.14 riastrad if (frame->vic != 0 && frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID)
859 1.1 riastrad return -EINVAL;
860 1.1 riastrad
861 1.14 riastrad length = HDMI_INFOFRAME_HEADER_SIZE;
862 1.14 riastrad length += hdmi_vendor_infoframe_length(frame);
863 1.1 riastrad
864 1.4 riastrad ret = hdmi_infoframe_header_pack(&frame->header, length, p, size);
865 1.1 riastrad if (ret < 0)
866 1.1 riastrad return ret;
867 1.3 riastrad KASSERT(ret == HDMI_INFOFRAME_HEADER_SIZE);
868 1.1 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
869 1.1 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
870 1.1 riastrad
871 1.1 riastrad p[0] = 0x03;
872 1.1 riastrad p[1] = 0x0c;
873 1.1 riastrad p[2] = 0x00;
874 1.1 riastrad
875 1.14 riastrad if (frame->vic) {
876 1.14 riastrad p[3] = __SHIFTIN(0x1, __BITS(6,5));
877 1.14 riastrad p[4] = frame->vic;
878 1.14 riastrad } else if (frame->s3d_struct != HDMI_3D_STRUCTURE_INVALID) {
879 1.1 riastrad p[3] = __SHIFTIN(0x2, __BITS(6,5));
880 1.1 riastrad p[4] = __SHIFTIN(frame->s3d_struct, __BITS(7,4));
881 1.1 riastrad if (frame->s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)
882 1.13 riastrad p[5] = __SHIFTIN(frame->s3d_ext_data, __BITS(7,4));
883 1.1 riastrad } else {
884 1.14 riastrad p[3] = __SHIFTIN(0x0, __BITS(6,5));
885 1.1 riastrad }
886 1.1 riastrad
887 1.14 riastrad hdmi_infoframe_set_checksum(buf, length);
888 1.1 riastrad
889 1.1 riastrad return length;
890 1.1 riastrad }
891 1.1 riastrad
892 1.11 riastrad static inline int
893 1.14 riastrad hdmi_vendor_infoframe_unpack(struct hdmi_vendor_infoframe *frame,
894 1.14 riastrad const void *buf, size_t size)
895 1.11 riastrad {
896 1.14 riastrad const uint8_t *p = buf;
897 1.11 riastrad int ret;
898 1.11 riastrad
899 1.14 riastrad memset(frame, 0, sizeof(*frame));
900 1.11 riastrad
901 1.14 riastrad ret = hdmi_infoframe_header_unpack(&frame->header, p, size);
902 1.14 riastrad if (ret)
903 1.11 riastrad return ret;
904 1.14 riastrad if (frame->header.length < 4)
905 1.14 riastrad return -EINVAL;
906 1.11 riastrad p += HDMI_INFOFRAME_HEADER_SIZE;
907 1.11 riastrad size -= HDMI_INFOFRAME_HEADER_SIZE;
908 1.11 riastrad
909 1.14 riastrad if (p[0] != 0x03 || p[1] != 0x0c || p[2] != 0x00)
910 1.14 riastrad return -EINVAL;
911 1.14 riastrad
912 1.14 riastrad switch (__SHIFTOUT(p[3], __BITS(6,5))) {
913 1.14 riastrad case 0x0:
914 1.14 riastrad if (frame->header.length != 4)
915 1.14 riastrad return -EINVAL;
916 1.14 riastrad break;
917 1.14 riastrad case 0x1:
918 1.14 riastrad if (frame->header.length != 5)
919 1.14 riastrad return -EINVAL;
920 1.14 riastrad frame->vic = p[4];
921 1.14 riastrad break;
922 1.14 riastrad case 0x2:
923 1.14 riastrad if (frame->header.length < 5)
924 1.14 riastrad return -EINVAL;
925 1.14 riastrad frame->s3d_struct = __SHIFTOUT(p[4], __BITS(7,4));
926 1.14 riastrad if (frame->s3d_struct < HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF) {
927 1.14 riastrad if (frame->header.length != 5)
928 1.14 riastrad return -EINVAL;
929 1.14 riastrad } else {
930 1.14 riastrad if (frame->header.length != 6)
931 1.14 riastrad return -EINVAL;
932 1.14 riastrad frame->s3d_ext_data = __SHIFTOUT(p[5], __BITS(7,4));
933 1.14 riastrad }
934 1.14 riastrad break;
935 1.14 riastrad default:
936 1.14 riastrad return -EINVAL;
937 1.11 riastrad }
938 1.11 riastrad
939 1.14 riastrad return 0;
940 1.11 riastrad }
941 1.11 riastrad
942 1.1 riastrad union hdmi_infoframe {
943 1.1 riastrad struct hdmi_infoframe_header any;
944 1.1 riastrad struct hdmi_avi_infoframe avi;
945 1.11 riastrad struct hdmi_drm_infoframe drm;
946 1.1 riastrad struct hdmi_spd_infoframe spd;
947 1.1 riastrad union hdmi_vendor_any_infoframe vendor;
948 1.1 riastrad };
949 1.1 riastrad
950 1.14 riastrad #define hdmi_infoframe_pack_only hdmi_infoframe_pack /* XXX */
951 1.14 riastrad
952 1.1 riastrad static inline ssize_t
953 1.14 riastrad hdmi_infoframe_pack(const union hdmi_infoframe *frame, void *buf, size_t size)
954 1.1 riastrad {
955 1.1 riastrad
956 1.1 riastrad switch (frame->any.type) {
957 1.1 riastrad case HDMI_INFOFRAME_TYPE_AVI:
958 1.1 riastrad return hdmi_avi_infoframe_pack(&frame->avi, buf, size);
959 1.11 riastrad case HDMI_INFOFRAME_TYPE_DRM:
960 1.11 riastrad return hdmi_drm_infoframe_pack(&frame->drm, buf, size);
961 1.1 riastrad case HDMI_INFOFRAME_TYPE_SPD:
962 1.1 riastrad return hdmi_spd_infoframe_pack(&frame->spd, buf, size);
963 1.1 riastrad case HDMI_INFOFRAME_TYPE_VENDOR:
964 1.1 riastrad return hdmi_vendor_infoframe_pack(&frame->vendor.hdmi, buf,
965 1.1 riastrad size);
966 1.1 riastrad default:
967 1.1 riastrad return -EINVAL;
968 1.1 riastrad }
969 1.1 riastrad }
970 1.1 riastrad
971 1.14 riastrad static inline int
972 1.14 riastrad hdmi_infoframe_unpack(union hdmi_infoframe *frame, const void *buf,
973 1.14 riastrad size_t size)
974 1.14 riastrad {
975 1.14 riastrad struct hdmi_infoframe_header header;
976 1.14 riastrad int ret;
977 1.14 riastrad
978 1.14 riastrad ret = hdmi_infoframe_header_unpack(&header, buf, size);
979 1.14 riastrad if (ret)
980 1.14 riastrad return ret;
981 1.14 riastrad switch (header.type) {
982 1.14 riastrad case HDMI_INFOFRAME_TYPE_AVI:
983 1.14 riastrad return hdmi_avi_infoframe_unpack(&frame->avi, buf, size);
984 1.14 riastrad case HDMI_INFOFRAME_TYPE_DRM:
985 1.14 riastrad return hdmi_drm_infoframe_unpack(&frame->drm, buf, size);
986 1.14 riastrad case HDMI_INFOFRAME_TYPE_SPD:
987 1.14 riastrad return hdmi_spd_infoframe_unpack(&frame->spd, buf, size);
988 1.14 riastrad case HDMI_INFOFRAME_TYPE_VENDOR:
989 1.14 riastrad return hdmi_vendor_infoframe_unpack(&frame->vendor.hdmi, buf,
990 1.14 riastrad size);
991 1.14 riastrad default:
992 1.14 riastrad return -EINVAL;
993 1.14 riastrad }
994 1.14 riastrad }
995 1.14 riastrad
996 1.10 riastrad static inline void
997 1.10 riastrad hdmi_infoframe_log(const char *level, struct device *device,
998 1.10 riastrad const union hdmi_infoframe *frame)
999 1.10 riastrad {
1000 1.16 riastrad
1001 1.16 riastrad hexdump(printf, device_xname(device), frame, sizeof(*frame));
1002 1.10 riastrad }
1003 1.10 riastrad
1004 1.1 riastrad #endif /* _LINUX_HDMI_H_ */
1005